-
Notifications
You must be signed in to change notification settings - Fork 65
/
LoadVectorGraphicsForm.frm
296 lines (272 loc) · 11.8 KB
/
LoadVectorGraphicsForm.frm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} LoadVectorGraphicsForm
Caption = "Load Vector Graphics File"
ClientHeight = 3408
ClientLeft = 96
ClientTop = 324
ClientWidth = 7020
OleObjectBlob = "LoadVectorGraphicsForm.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "LoadVectorGraphicsForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub ComboBoxVectorOutputType_Change()
SetVectorTypeDependencies
End Sub
Private Sub SetVectorTypeDependencies()
If ComboBoxVectorOutputType.ListIndex = 0 Then
CheckBoxCleanUp.value = False
CheckBoxCleanUp.Enabled = False
CheckBoxConvertLines.value = False
CheckBoxConvertLines.Enabled = False
Else
CheckBoxCleanUp.Enabled = True
CheckBoxConvertLines.Enabled = True
CheckBoxCleanUp.value = GetITSetting("LoadVectorFileCleanUp", True)
CheckBoxConvertLines.value = GetITSetting("LoadVectorFileConvertLines", False)
End If
End Sub
Sub CommandButtonSave_Click()
SetITSetting "LoadVectorFileConvertLines", REG_DWORD, BoolToInt(CheckBoxConvertLines.value)
SetITSetting "LoadVectorFileScaling", REG_SZ, textboxScalor.Text
SetITSetting "LoadVectorFileCalibrationX", REG_SZ, TextBoxCalibrationX.Text
SetITSetting "LoadVectorFileCalibrationY", REG_SZ, TextBoxCalibrationY.Text
SetITSetting "LoadVectorFileOutputTypeIdx", REG_DWORD, ComboBoxVectorOutputType.ListIndex
SetITSetting "LoadVectorFileCleanUp", REG_DWORD, BoolToInt(CheckBoxCleanUp.value)
End Sub
Private Sub UserForm_Initialize()
Me.Top = Application.Top + 110
Me.Left = Application.Left + 25
Me.Height = 194
Me.Width = 355
#If Mac Then
Me.LabelInsertPath.Caption = "Insert path of .pdf/.dvi/.xdv/.ps/.eps/.svg file:"
ResizeUserForm Me
ComboBoxVectorOutputType.Enabled = False
#End If
textboxScalor.Text = GetITSetting("LoadVectorFileScaling", "1")
CheckBoxConvertLines.value = GetITSetting("LoadVectorFileConvertLines", False)
TextBoxCalibrationX.Text = GetITSetting("LoadVectorFileCalibrationX", "1")
TextBoxCalibrationY.Text = GetITSetting("LoadVectorFileCalibrationY", "1")
ComboBoxVectorOutputType.List = Array("SVG via PDF w/ dvisvgm", "EMF w/ pdfiumdraw")
ComboBoxVectorOutputType.ListIndex = GetITSetting("LoadVectorFileOutputTypeIdx", 0)
CheckBoxCleanUp.value = GetITSetting("LoadVectorFileCleanUp", True)
SetVectorTypeDependencies
ShowAcceleratorTip Me.ButtonLoadFile
ShowAcceleratorTip Me.ButtonCancel
ShowAcceleratorTip Me.CommandButtonSave
End Sub
Private Sub UserForm_Activate()
#If Mac Then
MacEnableAccelerators Me
#End If
End Sub
Sub ButtonCancel_Click()
Unload LoadVectorGraphicsForm
End Sub
Private Function isInsertableVectorFile(file As String) As Boolean
Dim Ext As String
Ext = GetExtension(file)
#If Mac Then
isInsertableVectorFile = Ext = "pdf" Or Ext = "dvi" Or Ext = "xdv" Or Ext = "ps" Or Ext = "eps" Or Ext = "svg"
#Else
isInsertableVectorFile = Ext = "pdf" Or Ext = "dvi" Or Ext = "xdv" Or Ext = "ps" Or Ext = "eps" Or Ext = "emf" Or Ext = "svg"
#End If
End Function
Sub ButtonPath_Click()
#If Mac Then
TextBoxFile.Text = MacChooseFileOfType("pdf,dvi,xdv,ps,eps,svg")
#Else
TextBoxFile.Text = BrowseFilePath(TextBoxFile.Text, "Vector graphics files", "*.pdf;*.dvi;*.xdv;*.ps;*.eps;*.emf;*.svg", "&Select file")
#End If
TextBoxFile.SetFocus
End Sub
Private Sub TextBoxFile_Change()
Dim path As String, Ext As String
path = TextBoxFile.Text
Ext = GetExtension(path)
ButtonLoadFile.Enabled = FileExists(path) And isInsertableVectorFile(path)
If Ext = "emf" And isInsertableVectorFile(path) Then
ComboBoxVectorOutputType.ListIndex = 1
ComboBoxVectorOutputType.Enabled = False
SetVectorTypeDependencies
ElseIf Ext = "svg" Then
ComboBoxVectorOutputType.ListIndex = 0
ComboBoxVectorOutputType.Enabled = False
SetVectorTypeDependencies
End If
End Sub
Sub ButtonLoadFile_Click()
DoInsertVectorGraphicsFile
Unload LoadVectorGraphicsForm
End Sub
Private Sub DoInsertVectorGraphicsFile()
Dim NewShape As Shape
Dim TimeOutTimeString As String
Dim TimeOutTime As Long
TimeOutTimeString = GetITSetting("TimeOutTime", "20") ' Wait 20 seconds for the processes to complete
TimeOutTime = val(TimeOutTimeString) * 1000
Dim debugMode As Boolean
debugMode = False
#If Mac Then
Dim fs As New MacFileSystemObject
#Else
Dim fs As New FileSystemObject
#End If
Dim StartFolder As String
' The StartFolder doesn't really matter here because everything is relative to the input file,
' we just need any folder from which to launch the commands
'If ActivePresentation.path <> vbNullString Then
' StartFolder = ActivePresentation.path
'Else
If GetTempPath() <> vbNullString Then
StartFolder = GetTempPath()
Else
#If Mac Then
StartFolder = "/"
#Else
StartFolder = "C:\"
#End If
End If
Dim posX As Single, posY As Single, ScalingX As Single, ScalingY As Single
Dim Sel As Selection
Set Sel = Application.ActiveWindow.Selection
If Sel.Type = ppSelectionShapes Then
' if something is selected on a slide, use its position for the new display
posX = Sel.ShapeRange(1).Left
posY = Sel.ShapeRange(1).Top
Else
posX = 200
posY = 200
End If
ScalingX = textboxScalor.value * TextBoxCalibrationX.value
ScalingY = textboxScalor.value * TextBoxCalibrationY.value
' Get the path and extension of the file to be inserted
Dim path As String, Ext As String, pdfPath As String, psPath As String
path = TextBoxFile.Text
Ext = GetExtension(path)
Dim TeXExePath As String, TeXExeExt As String
TeXExePath = GetITSetting("TeXExePath", DEFAULT_TEX_EXE_PATH)
TeXExeExt = vbNullString
Dim VectorOutputTypeList As Variant
VectorOutputTypeList = Array("dvisvgm", "pdfiumdraw")
Dim VectorOutputType As String
VectorOutputType = VectorOutputTypeList(ComboBoxVectorOutputType.ListIndex)
Dim ConvertLines As Boolean
ConvertLines = CheckBoxConvertLines.value
Dim CleanUp As Boolean
CleanUp = CheckBoxCleanUp.value
Dim RetVal As Long
Dim ErrorMessage As String
Dim RunCommand As String
If Ext = "svg" Or Ext = "emf" Then
Set NewShape = AddDisplayShape(path, posX, posY)
Else
If VectorOutputType = "dvisvgm" Then ' Convert to SVG
Dim libgsPath As String
libgsPath = GetITSetting("Libgs", DEFAULT_LIBGS)
Dim libgsString As String
If libgsPath <> vbNullString Then
libgsString = " --libgs=" & ShellEscape(libgsPath)
Else
libgsString = vbNullString
End If
Dim InputTypeSwitch As String
If Ext = "ps" Or Ext = "eps" Then
InputTypeSwitch = " --eps"
ElseIf Ext = "pdf" Then
InputTypeSwitch = " --pdf"
Else
InputTypeSwitch = vbNullString
End If
Dim svgPath As String
svgPath = path & "_tmp.svg"
If fs.FileExists(svgPath) Then fs.DeleteFile svgPath
RunCommand = ShellEscape(TeXExePath & "dvisvgm" & TeXExeExt) & InputTypeSwitch & " -o " & ShellEscape(svgPath) _
& libgsString & " " & ShellEscape(path)
RetVal& = Execute(RunCommand, StartFolder, debugMode, TimeOutTime)
If (RetVal& <> 0 Or Not fs.FileExists(svgPath)) Then
' Error in EPS/PS/PDF to SVG conversion
ErrorMessage = "Error while using dvisvgm to convert input file to SVG."
ShowError ErrorMessage, RunCommand
Exit Sub
End If
Ext = "svg"
Set NewShape = AddDisplayShape(svgPath, posX, posY)
If fs.FileExists(svgPath) Then fs.DeleteFile svgPath
Else 'Convert to EMF
If TeXExePath <> vbNullString Then TeXExeExt = ".exe"
Dim DeleteTmpPDF As Boolean
DeleteTmpPDF = False
' If .dvi/.xdv/.ps/.eps file, convert to .pdf first, using ps2pdf/eps2pdf/dvipdfmx
If Ext = "ps" Or Ext = "eps" Or Ext = "dvi" Or Ext = "xdv" Then
psPath = path
pdfPath = path + "_tmp.pdf"
If fs.FileExists(pdfPath) Then fs.DeleteFile pdfPath
Dim pspdf_command As String
Dim pdfpath_prefix As String
pdfpath_prefix = vbNullString
If Ext = "ps" Then
pspdf_command = "ps2pdf"
ElseIf Ext = "eps" Then
pspdf_command = "epspdf"
Else
pspdf_command = "dvipdfmx"
pdfpath_prefix = "-o "
End If
RunCommand = ShellEscape(TeXExePath & pspdf_command & TeXExeExt) & " " + ShellEscape(psPath) + " " + pdfpath_prefix + ShellEscape(pdfPath)
RetVal& = Execute(RunCommand, StartFolder, debugMode, TimeOutTime)
If (RetVal& <> 0 Or Not fs.FileExists(pdfPath)) Then
ErrorMessage = "DVI/XDV/PS/EPS to PDF conversion failed" _
& vbNewLine & "Make sure " & pspdf_command & " is installed (it comes with, e.g., Tex Live, MikTeX or Ghostscript) and can be run from anywhere via the command line"
ShowError ErrorMessage, RunCommand
Exit Sub
End If
Ext = "pdf"
path = pdfPath
DeleteTmpPDF = True
End If
' Now we're dealing with a .pdf file
' Convert .pdf file to .emf using pdfiumdraw, which is part of TeX2img
If Ext = "pdf" Then
Dim emfPath As String
emfPath = path + "_tmp.emf"
If fs.FileExists(emfPath) Then fs.DeleteFile emfPath
Dim tex2img_command As String
Dim pdfiumdraw_command As String
tex2img_command = GetITSetting("TeX2img Command", DEFAULT_TEX2IMG_COMMAND)
pdfiumdraw_command = GetFolderFromPath(tex2img_command) & "pdfiumdraw.exe"
RunCommand = ShellEscape(pdfiumdraw_command) & " --extent=50 --emf --transparent --pages=1 --output=" & ShellEscape(emfPath) _
& " " & ShellEscape(path)
RetVal& = Execute(RunCommand, StartFolder, debugMode, TimeOutTime)
If (RetVal& <> 0 Or Not fs.FileExists(emfPath)) Then
ErrorMessage = " PDF to EMF conversion failed" _
& vbNewLine & "Make sure to correctly set the path to Tex2imgc.exe in Main Settings." _
& vbNewLine & "IguanaTex uses that path to find pdfiumdraw.exe."
ShowError ErrorMessage, RunCommand
Exit Sub
End If
Ext = "emf"
Set NewShape = AddDisplayShape(emfPath, posX, posY)
If Not debugMode Then
If fs.FileExists(emfPath) Then fs.DeleteFile emfPath
If DeleteTmpPDF Then
If fs.FileExists(pdfPath) Then fs.DeleteFile pdfPath
End If
End If
End If
End If
End If
If Ext = "emf" Then
Set NewShape = ConvertEMF(NewShape, ScalingX, ScalingY, posX, posY, Ext, ConvertLines, CleanUp)
ElseIf Ext = "svg" Then
Set NewShape = convertSVG(NewShape, ScalingX, ScalingY, posX, posY)
Else
MsgBox "We got lost somehow."
End If
NewShape.Select
End Sub