-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaTeX2AI.jsx
732 lines (566 loc) · 22.9 KB
/
LaTeX2AI.jsx
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
* My property description. Like other pieces of your comment blocks,
* this can span multiple lines.
*
* @property propertyName
* @type {Object}
* @default "foo"
*/
//////////////////////////////////////////////////////////////////////////////
// UI //
//////////////////////////////////////////////////////////////////////////////
try {
var l2a_path = 'C:\\Users\\VMware\\Desktop\\illustrator test\\LaTeX2AI';
var template_path = 'C:\\LaTeX2AI\\template\\template.ai';
var temp_path = Folder.temp.fsName;
l2a_document = new L2A_Document();
// Create UI
var dlg = new Window('dialog', 'LaTeX2AI');
dlg.frameLocation = [100,100];
dlg.size = [250,200];
// define text
//dlg.intro = dlg.add('statictext', [20,20,150,40] );
//dlg.intro.text = 'LaTeX2AI';
// define buttons
dlg.create = dlg.add('button', [0,0,250,40], 'new LaTeX2AI item');
dlg.change = dlg.add('button', [0,40,250,80], 'change selected LaTeX2AI item');
dlg.change_pos = dlg.add('button', [0,80,250,120], 'change selected LaTeX2AI placements');
dlg.box = dlg.add('button', [0,120,250,160], 'redo all LaTeX2AI boundary boxes');
dlg.redo = dlg.add('button', [0,160,250,200], 'redo all LaTeX2AI elements');
// add events
dlg.create.addEventListener ('click', create_l2a );
dlg.change.addEventListener ('click', change_l2a );
dlg.change_pos.addEventListener ('click', change_pos_l2a );
dlg.redo.addEventListener ('click', redo_l2a );
dlg.box.addEventListener ('click', redo_bound_l2a );
dlg.show();
}
catch(e) {
alert( e.message, 'LaTeX2AI Error', true);
}
//////////////////////////////////////////////////////////////////////////////
// Main functions //
//////////////////////////////////////////////////////////////////////////////
// creates new LaTeX2AI element
function create_l2a( ) {
try {
l2a_document.l2a_create();
app.redraw(); // redraw the illustrator window
}
catch(e) {
alert( e.message, 'LaTeX2AI Error', true);
}
}
// changes the LaTeX input for a element
function change_l2a( ) {
try {
l2a_document.l2a_change();
app.redraw(); // redraw the illustrator window
}
catch(e) {
alert( e.message, 'LaTeX2AI Error', true);
}
}
// changes the positioning for a LaTeX2AI element
function change_pos_l2a( ) {
try {
l2a_document.l2a_change_pos();
app.redraw(); // redraw the illustrator window
}
catch(e) {
alert( e.message, 'LaTeX2AI Error', true);
}
}
// removes all pdfs and redos every element
function redo_l2a( ) {
try {
l2a_document.l2a_redo();
app.redraw(); // redraw the illustrator window
}
catch(e) {
alert( e.message, 'LaTeX2AI Error', true);
}
}
// redos selected boundary boxes
function redo_bound_l2a( ) {
try {
l2a_document.l2a_redo_bound();
app.redraw(); // redraw the illustrator window
}
catch(e) {
alert( e.message, 'LaTeX2AI Error', true);
}
}
//////////////////////////////////////////////////////////////////////////////
// Classes //
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
// Class for the document to be used with LaTeX2AI.
///////////////////////////////////////////////////////////
function L2A_Document(){
// Creates a new LaTeX2AI object.
this.l2a_create = function(){
new L2A_Element();
}
// Changes selectet LaTeX2AI objects LaTeX code.
this.l2a_change = function(){
this.load_l2a_elements();
if (this.l2a_elements_selected.length == 0) {
throw new Error('No LaTeX2AI elements selected!');
}
for ( i = 0; i < this.l2a_elements_selected.length; i++ ) {
this.l2a_elements_selected[i].l2a_change_element();
}
}
// Changes selectet LaTeX2AI objects positioning.
this.l2a_change_pos = function(){
this.load_l2a_elements();
if (this.l2a_elements_selected.length == 0) {
throw new Error('No LaTeX2AI elements selected!');
}
for ( i = 0; i < this.l2a_elements_selected.length; i++ ) {
this.l2a_elements_selected[i].l2a_change_element_pos();
}
}
// Redos all LaTeX2AI elements.
this.l2a_redo = function(){
this.load_l2a_elements();
if (this.l2a_elements.length == 0) {
throw new Error('No LaTeX2AI elements!');
}
this.delete_pdf();
for ( i = 0; i < this.l2a_elements.length; i++ ) {
this.l2a_elements[i].l2a_redo_element();
}
}
// Changes selectet LaTeX2AI objects.
this.l2a_redo_bound = function(){
this.load_l2a_elements();
if (this.l2a_elements.length == 0) {
throw new Error('No LaTeX2AI elements!');
}
for ( i = 0; i < this.l2a_elements.length; i++ ) {
this.l2a_elements[i].set_bound();
}
}
// Fills the object with all LaTeX2AI items in the document.
this.load_l2a_elements = function(){
this.l2a_elements = [];
this.l2a_elements_selected = [];
// loop over all placed items
for ( i = 0; i < this.document.placedItems.length; i++ ) {
if (this.document.placedItems[i].name.indexOf('LaTeX2AI_') > -1) {
this.l2a_elements.push(new L2A_Element(this.document.placedItems[i]));
if (this.document.placedItems[i].selected == true){
this.l2a_elements_selected.push(new L2A_Element(this.document.placedItems[i]));
}
}
}
}
// Deletes all pdf files for the current document.
this.delete_pdf = function() {
// local variables
var i, l2a_files
// all files corresponding to the current document
l2a_files = Folder(l2a_path).getFiles(this.file_name + '*_LaTeX2AI_*.pdf');
// removes all elements
for ( i = 0; i < l2a_files.length; i++ ) {
l2a_files[i].remove();
}
}
// Reads an array of strings and set the settings acordingly.
this.read_settings = function(path) {
var content = read_file(path);
for (var i = 0; i < content.length; i++){
set = read_setting_line(content[i]);
if (!set[1]){
throw new Error('Empty value on setting "' + set[0] + '"!');
}
switch (set[0].toLowerCase()) {
case 'exe':
this.latex_exe = set[1];
break;
case 'arg':
this.latex_arg = set[1];
break;
default:
throw new Error('Setting "' + set[0] + '" unknown!');
}
}
}
// Reads an array of strings and set the settings acordingly.
this.read_last_input = function() {
var content = read_file(temp_path + '\\LaTeX2AI_last_input.txt');
if (content.length > 1){
this.last_input = content[1];
this.last_pos = content[0];
}
}
// creates the header file
this.create_header = function() {
this.header_file.open('w');
this.header_file.writeln('\\documentclass[11pt]{scrartcl}');
this.header_file.writeln('');
this.header_file.writeln('\\usepackage{amsmath}');
this.header_file.close();
}
///////////////////////////////////////////////////////////////
// Initiate Object.
///////////////////////////////////////////////////////////////
// check if there is an active document
if (app.documents.length > 0 ) {
// check if the file has a valid filepath
if ((' ' + app.documents[0].path + ' ').length < 3) {
throw new Error('File needs to be saved!');
}
l2a_path = app.documents[0].path + '\\LaTeX2AI';
// check if template file exists
this.template_file = File( template_path );
if (this.template_file.exists == false) {
throw new Error('Template file does not exist!');
}
// check if 'LaTeX2AI' folder exists in the active document directory
if (Folder(l2a_path).exists == false) {
Folder(l2a_path).create();
alert('<LaTeX2AI> folder did not exist in document directory, was created!');
}
// check if header file exists in 'LaTeX2AI'
this.header_file = File(l2a_path + '\\_LaTeX2AI_header.tex');
if (this.header_file.exists == false) {
this.create_header();
alert('<LaTeX2AI> folder did not contain <_LaTeX2AI_header.tex> file, was created (with font site 11pt)!');
}
this.document = app.documents[0];
this.file_name = get_filename(this.document.fullName);
this.latex_exe = 'pdflatex';
this.latex_arg = '-interaction nonstopmode -halt-on-error -file-line-error';
this.last_input = '$$';
this.last_pos = 'cc';
this.read_settings(l2a_path + '\\_LaTeX2AI_settings.txt');
this.read_last_input();
}
else{
throw new Error('No open documents!');
}
}
///////////////////////////////////////////////////////////
// Class for a LaTeX2AI element.
///////////////////////////////////////////////////////////
function L2A_Element(placed_item){
// Creates the LaTeX pdf and moves it into the \LaTeX2AI folder.
this.create_latex = function(){
// remove the pdf file if it exists
var pdf_file = File(temp_path + '\\' + this.latex_name + '.pdf');
if (pdf_file.exists == true) {
pdf_file.remove();
}
// create and execute the batch file
var batch_file = this.write_latex();
batch_file.execute();
// wait until batch file is deleted
while (batch_file.exists == true) {
}
// check if the LaTeX file was compilled propperly
if (pdf_file.exists == false) {
throw new Error('No .pdf file produced! LaTeX error?');
}
else{
// create the pdf file object and move it to the latex2AI folder
pdf_file.copy (l2a_path + '\\' + this.latex_name + '.pdf');
this.pdf_file = File(l2a_path + '\\' + this.latex_name + '.pdf');
}
}
// Writes the LaTeX files into the temp directory.
this.write_latex = function(){
// copies the header file into the temp directory
l2a_document.header_file.copy(temp_path + '\\_LaTeX2AI_header.tex')
// creates the latex file in the temp folder
var latex_file = File(temp_path + '\\' + this.latex_name + '.tex');
latex_file.open('w');
latex_file.writeln('\\input{_LaTeX2AI_header}');
latex_file.writeln('\\usepackage[active,tightpage]{preview}');
latex_file.writeln('\\usepackage{varwidth}');
latex_file.writeln('\\AtBeginDocument{\\begin{preview}\\begin{varwidth}{\\linewidth}}');
latex_file.writeln('\\AtEndDocument{\\end{varwidth}\\end{preview}}');
latex_file.writeln('\\pagestyle{empty}');
latex_file.writeln('\\begin{document}');
latex_file.writeln(this.latex_input);
latex_file.writeln('\\end{document}');
latex_file.close();
// creates the batch file
var batch_file = File(temp_path + '\\' + 'LaTeX2AI_batch.bat');
batch_file.open('w');
batch_file.writeln('cd "' + temp_path + '"' );
batch_file.writeln('"' + l2a_document.latex_exe + '" ' + l2a_document.latex_arg + ' "' + this.latex_name + '.tex"' );
batch_file.writeln('del "' + temp_path +'\\LaTeX2AI_batch.bat"');
batch_file.close();
return batch_file;
}
// Writes the last input to the Temp folder.
this.write_last_input = function(){
var last_input = File(temp_path + '\\LaTeX2AI_last_input.txt');
last_input.open('w');
last_input.writeln(this.pos_input);
last_input.writeln(this.latex_input);
last_input.close();
}
// Gets the filename for the next LaTeX2AI item.
this.set_l2a_name = function () {
// loop to find first nonused name for LaTeX2AI element
var i = 1;
while (File(l2a_path + '\\' + l2a_document.file_name + '_LaTeX2AI_' + pad(i,3) + '.pdf').exists) {
i = i+1;
}
this.latex_name = l2a_document.file_name + '_LaTeX2AI_' + pad(i,3);
}
// Get the LaTeX code for the LaTeX2AI element.
this.get_latex_code_input = function() {
if (typeof this.latex_input == 'undefined'){
this.latex_input = l2a_document.last_input;
}
// input form for LaTeX code
this.latex_input = prompt('Please enter the LaTeX code', this.latex_input, 'LaTeX2AI');
// check if there is an input
if(this.latex_input==null) {
throw new Error('No LaTeX code!');
}
// update the last input for the document
l2a_document.last_input = this.latex_input;
}
// Get the position input for the LaTeX2AI element.
this.get_pos_input = function() {
// input form for LaTeX positioning
if (typeof this.pos_input == 'undefined'){
this.pos_input = l2a_document.last_pos;
}
this.pos_input = prompt('Please enter positioning of the text', this.pos_input,'LaTeX2AI');
// check if there position input is valid
this.get_posfac();
// update the last input for the document
l2a_document.last_pos = this.pos_input;
}
// Gets the position factor array from pos input string.
this.get_posfac = function() {
var pos;
switch (this.pos_input) {
case 'tl':
pos = [0.,0.];
break;
case 'tc':
pos = [0.5,0];
break;
case 'tr':
pos = [1.,0.];
break;
case 'cl':
pos = [0.,0.5];
break;
case 'cc':
pos = [0.5,0.5];
break;
case 'cr':
pos = [1.,0.5];
break;
case 'bl':
pos = [0.,1.];
break;
case 'bc':
pos = [0.5,1.];
break;
case 'br':
pos = [1.,1.];
break;
default:
throw new Error('Wrong input for the positioning!');
}
this.posfac = pos;
}
// Gets relevant placed item from template document.
this.get_template = function(){
// open the template file
var template_doc = app.open(l2a_document.template_file);
// copy the placedItem to the destination doc and close the template doc
var template_item = template_doc.placedItems.getByName ('LaTeX2AI_temp_' + this.pos_input);
template_item.selected = true;
app.copy();
template_doc.close(SaveOptions.DONOTSAVECHANGES);
l2a_document.document.activate();
app.paste();
// finish the item
this.placed_item = l2a_document.document.placedItems.getByName('LaTeX2AI_temp_' + this.pos_input);
this.relink_item();
this.placed_item.selected = true;
}
// Relinks the pdf file and updates the the note attribute of the placed item.
this.relink_item = function(){
this.placed_item.file = this.pdf_file;
this.set_attributes();
}
// Updates the the note attribute of the placed item.
this.set_attributes = function(){
this.placed_item.name = this.latex_name;
this.placed_item.note = this.pos_input + this.latex_input;
}
// Check if object is rotated or not.
this.is_rotated = function() {
var err = 0.001; // max error for equality to be detected
if ( Math.abs(this.placed_item.matrix.mValueB) < err){
return false;
}
else {
return true;
}
}
// Sets the boundary box for the LaTeX2AI element.
this.set_bound = function() {
var err = 0.001; // max error for equality to be detected
// only do something if object is not rotated
if ( !this.is_rotated() ){
// variables
var M, X; // matrix
var height, width, height_new, width_new; // numbers
// get the original pdf size
M = this.placed_item.boundingBox;
width_new = Math.abs(M[0]-M[2]);
height_new = Math.abs(M[1]-M[3]);
// get the actual position height and width
width = this.placed_item.width;
height = this.placed_item.height;
X = this.placed_item.position;
// check if AI box is smaller than pdf
if (Math.abs(width - width_new) < err || Math.abs(height - height_new) < err ) {
this.placed_item.width = 2*width_new;
this.placed_item.height = 2*height_new;
}
// set to new values
this.placed_item.width = width_new;
this.placed_item.height = height_new;
this.placed_item.position = [X[0] + this.posfac[0] * (width - width_new) , X[1] - this.posfac[1] * (height - height_new)];
}
}
// Changes the l2a element
this.l2a_change_element = function() {
var old_input = this.latex_input;
this.get_latex_code_input();
if (old_input !== this.latex_input){
this.create_latex();
this.relink_item();
this.set_bound();
}
}
// Changes the l2a element positioning
this.l2a_change_element_pos = function() {
// only do something if object is not rotated
if ( !this.is_rotated() ){
var old_pos = this.pos_input;
this.get_pos_input();
if (old_pos !== this.pos_input){
// add new element to document
var template_doc = app.open(l2a_document.template_file);
var temp_item = template_doc.placedItems.getByName('LaTeX2AI_temp_' + this.pos_input);
var new_item = temp_item.duplicate(this.placed_item.parent);
template_doc.close(SaveOptions.DONOTSAVECHANGES);
l2a_document.document.activate();
// copy attributes
new_item.height = this.placed_item.height;
new_item.width = this.placed_item.width;
new_item.position = this.placed_item.position;
new_item.file = this.placed_item.file;
this.placed_item.remove();
this.placed_item = new_item;
// write arrtibutes to new item
this.set_attributes();
}
}
}
// Redos the l2a element
this.l2a_redo_element = function() {
this.set_l2a_name();
this.create_latex();
this.relink_item();
this.set_bound();
}
///////////////////////////////////////////////////////////////
// Initiate Object.
///////////////////////////////////////////////////////////////
// if object is initialised with placed item the item is added
if (typeof placed_item !== 'undefined'){
this.placed_item = placed_item;
this.latex_name = placed_item.name;
this.pdf_file;
this.pos_input = placed_item.note.substr(0,2);
this.get_posfac();
this.latex_input = placed_item.note.substr(2,placed_item.note.length);
}
// otherwise the input is retrieved and the object is created
else{
this.set_l2a_name();
this.get_pos_input();
this.get_latex_code_input();
this.create_latex();
this.get_template();
this.set_bound();
this.write_last_input();
}
}
//////////////////////////////////////////////////////////////////////////////
// functions //
//////////////////////////////////////////////////////////////////////////////
// Returns the filename of an File object (without the extension).
function get_filename( full_name ) {
var path = 'x' + full_name;
var a = path.lastIndexOf('/');
var b = path.lastIndexOf('.');
return path.substr(a+1,b-a-1);
}
// Padds the number num with zeros so a total length of size is reached.
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
// Reads a file and returns a dictionary with the settings and values.
function read_file(path) {
var comment = "%";
var file = File(path);
var content = [];
if (file.exists){
file.open("r");
while(! file.eof){
var line = file.readln();
if (line.length == 0){
continue;
}
var index = line.indexOf(comment);
if (index == -1){
content.push(line);
}
else if (index > 0){
content.push(line.substring(0, index));
}
}
}
file.close();
return content;
}
// Reads a settings line and returns the key and value.
function read_setting_line(line){
var index = line.indexOf("=");
var key = line.substring(0, index);
var value = line.substring(index+1, line.length);
return [key.trim(), value.trim()];
}
// Returns the keys of a dictionary.
function get_keys(dict){
var keys = [];
for (var key in dict){
keys.push(key);
}
return keys;
}
// Add trim method to strings.
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,'');
}
}