-
Notifications
You must be signed in to change notification settings - Fork 18
/
LatexDialog.cs
402 lines (341 loc) · 13.4 KB
/
LatexDialog.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace Latex4CorelDraw
{
public partial class LatexDialog : Form
{
private string m_textColor;
public string TextColor
{
get { return m_textColor; }
}
public string FontSize
{
get { return comboBoxFontSize.Text; }
}
public string LatexFont
{
get { return comboBoxFont.Text; }
}
public string FontSeries
{
get { return comboBoxSeries.Text; }
}
public string FontShape
{
get { return comboBoxShape.Text; }
}
public string LatexCode
{
get { return m_scintilla.Text; }
}
private DialogResult m_result;
public System.Windows.Forms.DialogResult Result
{
get { return m_result; }
}
private LatexEquation m_latexEquation;
public Latex4CorelDraw.LatexEquation LatexEquation
{
get { return m_latexEquation; }
set { m_latexEquation = value; }
}
private bool m_finishedSuccessfully;
private ScintillaNET.Scintilla m_scintilla;
public LatexDialog()
{
InitializeComponent();
// Be sure, there is a language.xml file
AddinUtilities.copyLanguageFile();
this.SuspendLayout();
m_scintilla = new ScintillaNET.Scintilla();
this.groupBoxLatex.Controls.Add(m_scintilla);
m_scintilla.Dock = DockStyle.Fill;
m_scintilla.Margins[0].Width = 20;
m_scintilla.ConfigurationManager.CustomLocation = AddinUtilities.getAppDataLocation() + "\\Language.xml";
m_scintilla.ConfigurationManager.Language = "mytex";
m_scintilla.IsBraceMatching = true;
m_scintilla.TabIndex = 0;
m_scintilla.AutoComplete.DropRestOfWord = true;
this.ResumeLayout(false);
m_scintilla.Focus();
m_scintilla.KeyDown += new KeyEventHandler(m_scintilla_KeyDown);
createFontEntries();
m_finishedSuccessfully = false;
this.FormClosing += new FormClosingEventHandler(LatexDialog_FormClosing);
}
void m_scintilla_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Enter) && (e.Modifiers == Keys.Control))
{
buttonOk.PerformClick();
}
}
private void init(string title)
{
this.Text = title;
m_finishedSuccessfully = false;
m_scintilla.Text = "";
m_scintilla.UndoRedo.EmptyUndoBuffer();
SettingsManager mgr = SettingsManager.getCurrent();
if (!comboBoxFontSize.Items.Contains(mgr.SettingsData.fontSize))
comboBoxFontSize.Items.Add(mgr.SettingsData.fontSize);
comboBoxFontSize.SelectedItem = mgr.SettingsData.fontSize;
comboBoxFont.Text = mgr.SettingsData.font;
comboBoxSeries.Text = mgr.SettingsData.fontSeries;
comboBoxShape.Text = mgr.SettingsData.fontShape;
buttonColor.BackColor = AddinUtilities.stringToColor(mgr.SettingsData.textColor);
m_textColor = mgr.SettingsData.textColor;
pictureBoxPreview.BackColor = Color.White;
panel1.BackColor = Color.White;
}
public void init(LatexEquation eq, string title)
{
init(title);
if (eq != null)
{
m_scintilla.Text = eq.m_code;
m_scintilla.Selection.SelectAll();
comboBoxFontSize.Text = eq.m_fontSize.ToString();
comboBoxFont.Text = eq.m_font.fontName;
comboBoxSeries.Text = eq.m_fontSeries.fontSeries;
comboBoxShape.Text = eq.m_fontShape.fontShape;
try
{
buttonColor.BackColor = AddinUtilities.stringToColor(eq.m_color);
m_textColor = eq.m_color;
}
catch
{
}
}
}
public void init(string template, bool useTemplate, string title)
{
init(title);
if (template != null)
{
m_scintilla.Text = template;
if (useTemplate)
{
int index = m_scintilla.Text.IndexOf("<Enter latex code>", 0);
//m_scintilla.Select(index, 18);
m_scintilla.Selection.Start = index;
m_scintilla.Selection.End = index + 18;
}
m_scintilla.Selection.SelectAll();
}
}
private void createFontEntries()
{
AddinUtilities.initFonts();
comboBoxFont.Items.AddRange(AddinUtilities.LatexFonts.ToArray());
comboBoxSeries.Items.AddRange(AddinUtilities.LatexFontSeries.ToArray());
comboBoxShape.Items.AddRange(AddinUtilities.LatexFontShapes.ToArray());
}
void LatexDialog_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
if ((this.DialogResult == DialogResult.OK) && (!m_finishedSuccessfully))
return;
m_result = this.DialogResult;
// Set the focus => next time we open the dialog it is correct
m_scintilla.Focus();
this.Hide();
if (pictureBoxPreview.Image != null)
pictureBoxPreview.Image.Dispose();
pictureBoxPreview.Image = null;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
Close();
}
private void buttonOk_Click(object sender, EventArgs e)
{
generateEquation(true);
}
private bool generateEquation(bool createShape)
{
// Check paths
SettingsManager mgr = SettingsManager.getCurrent();
// Check font size
string fontSize = comboBoxFontSize.Text;
float size = 12;
try
{
size = Convert.ToSingle(fontSize);
}
catch (Exception ex)
{
MessageBox.Show("Font size exception: \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// Check Dpi
float[] systemDPI = AddinUtilities.getSystemDPI();
float dpiValue = systemDPI[0];
mgr.SettingsData.fontSize = comboBoxFontSize.Text;
mgr.SettingsData.font = comboBoxFont.Text;
mgr.SettingsData.fontSeries = comboBoxSeries.Text;
mgr.SettingsData.fontShape = comboBoxShape.Text;
mgr.SettingsData.textColor = m_textColor;
mgr.saveSettings();
m_latexEquation = new LatexEquation(m_scintilla.Text, size, m_textColor, (LatexFont)comboBoxFont.SelectedItem,
(LatexFontSeries)comboBoxSeries.SelectedItem,
(LatexFontShape)comboBoxShape.SelectedItem);
// Run once with preview to get depth value
m_finishedSuccessfully = true;
if (!createShape)
m_finishedSuccessfully = AddinUtilities.createLatexPng(m_latexEquation, true);
if (m_finishedSuccessfully)
{
// Run once without to get correct tight image
if (!createShape)
m_finishedSuccessfully = AddinUtilities.createLatexPng(m_latexEquation, false);
else
{
m_finishedSuccessfully = AddinUtilities.createLatexPdf(m_latexEquation);
if (m_finishedSuccessfully)
{
string imageFile = Path.Combine(AddinUtilities.getAppDataLocation(), "teximport.pdf");
Corel.Interop.VGCore.StructImportOptions impopt = new Corel.Interop.VGCore.StructImportOptions();
impopt.MaintainLayers = true;
Corel.Interop.VGCore.ImportFilter impflt = DockerUI.Current.CorelApp.ActiveLayer.ImportEx(imageFile, Corel.Interop.VGCore.cdrFilter.cdrPDF, impopt);
impflt.Finish();
m_latexEquation.m_shape = DockerUI.Current.CorelApp.ActiveShape;
ShapeTags.setShapeTags(m_latexEquation);
}
}
}
return m_finishedSuccessfully;
}
private void changeOptionsToolStripMenuItem_Click(object sender, EventArgs e)
{
AddinUtilities.changeOptions();
}
private void buttonColor_Click(object sender, EventArgs e)
{
ColorDialog dialog = new ColorDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
buttonColor.BackColor = dialog.Color;
Color col = buttonColor.BackColor;
float r = (float)col.R / 255.0f;
float g = (float)col.G / 255.0f;
float b = (float)col.B / 255.0f;
string rStr = r.ToString().Replace(',', '.');
string gStr = g.ToString().Replace(',', '.');
string bStr = b.ToString().Replace(',', '.');
m_textColor = rStr + "," + gStr + "," + bStr;
}
}
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
m_scintilla.FindReplace.ShowFind();
}
private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
{
m_scintilla.FindReplace.ShowReplace();
}
private void incrementalSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
m_scintilla.FindReplace.IncrementalSearcher.Show();
}
private void commentLineToolStripMenuItem_Click(object sender, EventArgs e)
{
m_scintilla.Commands.Execute(ScintillaNET.BindableCommand.LineComment);
}
private void uncommentLineToolStripMenuItem_Click(object sender, EventArgs e)
{
m_scintilla.Commands.Execute(ScintillaNET.BindableCommand.LineUncomment);
}
private void addSnippetToolStripMenuItem_Click(object sender, EventArgs e)
{
m_scintilla.Snippets.ShowSnippetList();
}
private void buttonPreview_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
if (pictureBoxPreview.Image != null)
pictureBoxPreview.Image.Dispose();
bool finishedSuccessfully = generateEquation(false);
if (finishedSuccessfully)
{
string imageFile = Path.Combine(AddinUtilities.getAppDataLocation(), "teximport.png");
using (FileStream stream = new FileStream(imageFile, FileMode.Open, FileAccess.Read))
{
pictureBoxPreview.Image = System.Drawing.Image.FromStream(stream);
int sum = buttonColor.BackColor.R + buttonColor.BackColor.G + buttonColor.BackColor.B;
//Color col = Color.FromArgb(255 - buttonColor.BackColor.R, 255 - buttonColor.BackColor.G, 255 - buttonColor.BackColor.B);
System.Drawing.Color col = System.Drawing.Color.White;
if (sum / 3 > 127)
col = System.Drawing.Color.Black;
pictureBoxPreview.BackColor = col;
panel1.BackColor = col;
stream.Close();
}
}
Cursor.Current = Cursors.Default;
}
private void panel1_VisibleChanged(object sender, EventArgs e)
{
if (m_scintilla.Text != "")
buttonPreview.PerformClick();
}
private void openLatexTemplateToolStripMenuItem_Click(object sender, EventArgs e)
{
string templateFileName = AddinUtilities.getAppDataLocation() + "\\LatexTemplate.txt";
System.Diagnostics.Process.Start(templateFileName);
}
}
public class LatexFont
{
public LatexFont(string fn, string lfn)
{
fontName = fn;
latexFontName = lfn;
}
public string fontName;
public string latexFontName;
public override string ToString()
{
return fontName;
}
}
public class LatexFontSeries
{
public LatexFontSeries(string fns, string lfns)
{
fontSeries = fns;
latexFontSeries = lfns;
}
public string fontSeries;
public string latexFontSeries;
public override string ToString()
{
return fontSeries;
}
}
public class LatexFontShape
{
public LatexFontShape(string fns, string lfns)
{
fontShape = fns;
latexFontShape = lfns;
}
public string fontShape;
public string latexFontShape;
public override string ToString()
{
return fontShape;
}
}
}