-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add Text.jsx
1529 lines (1248 loc) · 36.6 KB
/
Add Text.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* - Page match regular expression simplification for CS5 compatibility.
* - Event listeners on delimeter text inputs direct set by attribute for
* CS5 compatibility.
* - List fonts with PostScript name so we can set the font property of text
* types easily.
* - Verify config file exists when selecting.
*
* 20090911
* - File and folder inputs editable
* - Better handling for match input with no end text
* - Fix for error if config select cancelled
* - Fix for default ignore regex not matching properly
* - Page match now ignores : and > after the numbers
*
* 20090129
* - Handles multiple text types on the same line.
*
* 20090124
* - Implemented import/export of settings (doesn't work with weird labels)
* - Updated to work with CS4
* - Units are now restored when toggling between text types
* - Trans test file will be auto-opened after being created
* - Added input for page number match
*
* 20080730
* - Fix for bug in global ignore
*
* 20080727
* - Default global ignore to being set
* - Added color hex value input
*
* 20080726
* - Added page match for <##>
* - Fixed selection of "metrics" kerning
*
* This script is used to add text from a translation onto the separate pages
* in a manga.
*
**
* todo
* - area for info messages
* - option to save to output directory
* - implement embedded text types
* - speed up the adding text to page
* - trans location guessing
* - text type defaults
* - option to remove existing text
* - remember locations of opened files
**
* written by kepp
**/
$.strict = true;
var UND = undefined; // because I don't feel like typing the whole thing out
// need to make sure regex are parsed properly when loaded from saved file
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/**
* Helper used to parse the trans file into a more useable format
**/
function Trans()
{
}
Trans.prototype.init = function( file, types, pageMatch, ignore )
{
this.file = file;
this.setRegExp( types );
this.pageRE = new RegExp( pageMatch, "i" );
this.ignoreRE = new RegExp( ignore );
this.pageText = this.parseText();
}
/**
* RegExp to match against page number lines and extract the page number
* String tests ( match will always be case insensitive ):
* page 1
* page. 2
* pages 3-4
* pages. 5-6
* p 7
* p. 8
* pp 9-10
* pp. 11-12
* pg 13
* pg. 13
* pgs 14-15
* pgs. 16-17
* this page 9 sucks
*
**/
/**
* Regular expression to match against page numbers
* I've found photoshop can die on this if trying to test against <text> if
* p{1,2} is used
**/
Trans.prototype.pageRE =
/^\s*(?:(?:p|pp|page|pg)?[.s]{0,2}|<)\s*([\d-]{1,9})[>:]?(.*)/
/**
* RegExp to filter out parts of the line
**/
Trans.prototype.ignoreRE = /^[\w()?\-'&\s]+?[:>]\s*|^\s+/;
Trans.prototype.hasContentRE = /.*[\w!?.]+.*/;
Trans.prototype.regex = {};
Trans.prototype.file = null;
Trans.prototype.pageText = null;
/**
* Initializes the regexps used to grab the different text blocks
**/
Trans.prototype.setRegExp = function( types )
{
var all = "", nodelim;
this.regex = {};
for ( var type in types )
{
var typeObj = types[ type ];
this.regex[ typeObj.label ] = {};
this.regex[ typeObj.label ].test = new RegExp( typeObj.regexp );
if ( typeObj.replaceChk )
this.regex[ typeObj.label ].replace =
{
match: new RegExp( typeObj.match ),
replace: typeObj.replace
};
}
};
/**
* Function used to parse the trans text file into an array and determine the
* type of text block
**/
Trans.prototype.parseText = function()
{
var trans = new Object(), line = "", page = null;
if ( !this.file.open( "r" ))
{
alert( "Couldn't open trans file" );
return;
}
while ( ( line = this.getNextLine( this.file ) ) )
{
if ( this.pageRE.test( line ) )
{
page = new Array();
trans[ RegExp.$1 ] = page;
// if there's text on the same line as the page number, save it
if ( RegExp.$2 )
this.parseLine( RegExp.$2, page )
}
else if ( page )
{
this.parseLine( line, page )
}
}
var closed = this.file.close();
if ( !closed )
alert( "trans file close error" );
return trans;
};
/**
* Determine the type of line currently being parsed
**/
Trans.prototype.parseLine = function( line, page )
{
var clean = line.replace( this.ignoreRE, "" );
if ( !clean )
return;
// look for the first match and add it to the page
for ( var re in this.regex )
{
var regex = this.regex[ re ];
if ( regex.test.test( clean ) )
{
var extract = RegExp.$1;
var follow = RegExp.$2.replace( this.ignoreRE, "" );
if ( regex.replace )
extract = extract.replace( regex.replace.match,
regex.replace.replace );
page.push( { "orig": line, "type": re, "text": extract } );
// process following text
if ( follow /*&& follow != clean && this.hasContentRE.test( follow )*/ )
this.parseLine( follow, page );
return;
}
}
};
/**
* Get the next non-empty line in the trans file
**/
Trans.prototype.getNextLine = function( file )
{
var line = "";
// keep going until there's a line or it's the end of the file
while ( !line && !file.eof )
{
line = file.readln();
line = line.replace( /^\s+/, "" );
line = line.replace( /\s+$/, "" );
}
return line;
};
/**
* Get the array text lines for the page
**/
Trans.prototype.getPages = function()
{
return this.pageText;
};
/**
* Get the text representation of the trans
**/
Trans.prototype.getText = function()
{
var trans = this.pageText;
var text = "";
for ( var number in trans )
{
var page = trans[number];
text += "\nPage " + number + ":\n";
for ( var i = 0, line; line = page[i]; i++ )
text += line.type + ": " + line.text + " [" + line.orig + "]\n";
}
return text;
};
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/**
* Helper object to create text types
**/
function TypeHolder( inputs, textTypes )
{
this.checkLabel( inputs.label, textTypes );
this.checkSize( inputs.size );
var type = {};
for ( var name in inputs )
{
var ctrl = inputs[ name ];
type[ name ] = ( ctrl.type == "edittext" ) ? ctrl.text :
( ctrl.selection ? ctrl.selection.text : ctrl.value );
}
return type;
}
/**
* Check that the label for the text type being added is valid
**/
TypeHolder.prototype.checkLabel = function( input, textTypes )
{
var label = input.text.replace( /[\W]/g, "_" );
if ( !label )
{
label = "_1";
}
while ( textTypes[ label ] )
{
/(.*?)(_?)([\d]*)$/.test( label );
label = RegExp.$1 + ( (RegExp.$2 ) ?
RegExp.$2 + ( Number( RegExp.$3 ) + 1 ) : "_1" );
}
input.text = label;
};
/**
* Validate the inputted text size
**/
TypeHolder.prototype.checkSize = function( size )
{
if ( size.text && !/^\d+$/.test( size.text ))
{
size.text = prompt( "Text size invalid. Set new value:", 12 );
this.checkSize( size );
}
};
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
/**
* Show the script configuration dialog
**/
function UserInterface()
{
var res =
"dialog { text: 'Add Text Dialog', margins: 5, \
selectGrp: Group {}, \
textPnl: Panel { \
text: 'Text Types', orientation: 'column', alignChildren: 'fill', \
pageMatchGrp: Group { orientation: 'column', alignChildren: 'fill' }, \
ignoreGrp: Group { orientation: 'column', alignChildren: 'fill' }, \
textGrp: Group { alignChildren: 'top', \
listGrp: Group { orientation: 'column' }, \
inputGrp: Group { alignChildren: 'fill', orientation: 'column', \
grp1: { orientation: 'column', alignChildren: 'fill' } \
} \
} \
}, \
buttonGrp: Group { orientation: 'row' } \
}";
var dlg = new Window( res );
dlg.center();
this.trans = new Trans();
this.addControls( dlg );
this.dialog = dlg;
dlg.show();
}
UserInterface.prototype.dialog = null;
UserInterface.prototype.transFile = null;
UserInterface.prototype.imgDir = null;
UserInterface.prototype.typeList = null;
UserInterface.prototype.textTypes = {};
UserInterface.prototype.inputs =
{
label: null,
delimRb: null,
start: null,
end: null,
regexRb: null,
regexp: null,
replaceChk: null,
match: null,
replace: null,
font: null,
size: null,
units: null,
color: null,
kern: null
};
UserInterface.prototype.trans = null;
UserInterface.prototype.ignore = null;
UserInterface.prototype.pageMatch = null;
UserInterface.prototype.bind = function( func )
{
var ui = this;
var args = Array.prototype.slice.call( arguments, 1 );
var b = function()
{
var allArgs = args.concat( Array.prototype.slice.call( arguments ) );
return func.apply( ui, allArgs );
};
return b;
};
/*****************************************************************************/
/**
* Helper to construct resource strings
**/
UserInterface.prototype.getRes = function( props )
{
return ( ( props.grp ) ? props.grp + ": Group" : "group" ) + "{" +
( ( props.lbl ) ? "st: StaticText { text:'" + props.lbl +
"' }," : "" ) +
"el:" + ( props.type || "EditText" ) +
" { name: '" + ( props.name || "" ) +
"', size: [" + ( props.width || 100 ) + ",20]," +
"text:'" + ( props.text || "" ) + "'}}";
};
/**
* Fill out the dialog with controls for user input
**/
UserInterface.prototype.addControls = function( dlg )
{
this.addSelects( dlg.selectGrp ); // file + directory selection
this.addIgnoreInput( dlg.textPnl.ignoreGrp ); // trans text ignore input
this.addPageMatch( dlg.textPnl.pageMatchGrp ); // match against page numbers
this.addTypeInput( dlg.textPnl.textGrp ); // inputs for text types
this.addActionButtons( dlg, dlg.buttonGrp ); // type and dialog management
};
/*****************************************************************************/
/**
* Add UI elements to select the translation file and raw image directory
**/
UserInterface.prototype.addSelects = function( selectGrp )
{
selectGrp.orientation = "column";
var addUI = this.bind( function( text, handler, start )
{
var select = selectGrp.add( "group", UND );
var path = select.add( "edittext", UND, text );
path.size = [ 300, 20 ];
path.addEventListener( "focus", function() {
if ( !this.text || this.text == text )
{
this.oldPath = path.text;
this.text = "";
}
}, false );
path.addEventListener( "blur", function() {
if ( !this.text || this.text == text )
this.text = this.oldPath || "";
}, false );
var btn = select.add( "button", UND, "Browse..." );
btn.addEventListener( "click", this.bind( handler, path, start ), false );
} );
var file, folder;
if ( app.documents.length )
{
file = activeDocument.fullName;
folder = activeDocument.fullName.parent;
}
else
{
folder = Folder.myDocuments;
}
addUI( "Select Translation...", this.selectTrans, ( file || new File() ) );
addUI( "Select Image Directory...", this.selectFolder, folder );
};
/**
* Add fields for the trans text to match the text type against
**/
UserInterface.prototype.ignoreChkValue = true;
UserInterface.prototype.addIgnoreInput = function( ignoreGrp )
{
var grp = ignoreGrp.add(
"group { \
chk: Checkbox { text: 'Global Ignore:' }," +
this.getRes( { name: "ignore", width: 200, grp: "grp1" } ) +
"}" );
this.ignore = grp.grp1.el;
grp.chk.addEventListener( "click", this.bind( this.toggleIgnore ), false );
grp.chk.value = true;
var s = this.trans.ignoreRE.toString();
grp.grp1.el.text = s.substring( 1, s.length - 1 );
ignoreGrp.add( "panel {}" ); // horizontal divider
};
UserInterface.prototype.toggleIgnore = function( event )
{
var value = !this.ignoreChkValue;
var el = this.ignore;
el.parent.enabled = value;
el.enabled = value;
var def = "text to ignore";;
if ( el.text == def )
{
var s = this.trans.ignoreRE.toString();
el.text = s.substring( 1, s.length - 1 );
}
else if ( !el.text )
{
el.text = def;
}
this.ignoreChkValue = value;
};
UserInterface.prototype.addPageMatch = function( pageMatchGrp )
{
var grp = pageMatchGrp.add(
"group {" +
this.getRes( { name: "pagematch", lbl: 'Page # Match:',
width: 350, grp: "grp1" } ) +
"}" );
var el = this.pageMatch = grp.grp1.el;
var s = this.trans.pageRE.toString();
el.text = s.substring( 1, s.length - 1 );
};
/**
* Add elements to the dialog needed for creating new text types and viewing
* already created text types
**/
UserInterface.prototype.addTypeInput = function( textGrp )
{
this.addTypeList( textGrp.listGrp );
this.addConfigButtons( textGrp.listGrp );
var res = this.getRes( { type: "EditText", lbl: "Label:", name: "label" } );
var label = textGrp.inputGrp.add( res ).el;
this.inputs.label = label;
this.addMatchInput( textGrp.inputGrp );
this.addReplaceInput( textGrp.inputGrp );
this.addFontInput( textGrp.inputGrp );
this.addInputButtons( textGrp.inputGrp );
};
/**
* Add buttons to start and cancel processing
**/
UserInterface.prototype.addActionButtons = function( dlg, parent )
{
var group = parent.add( "group" );
group.alignment = "left";
dlg.testButton = group.add( "button", UND, "Test Trans" );
dlg.testButton.enabled = false;
dlg.testButton.addEventListener( "click", this.bind( this.testTrans ),
false );
var act = group.add( "group" );
act.position = "left";
dlg.defaultElement = act.add( "button", UND, "Go" );
dlg.defaultElement.enabled = false;
dlg.defaultElement.addEventListener( "click", this.bind( function( event )
{
this.trans.init( this.transFile, this.textTypes,
this.pageMatch.text, this.ignore.text );
new Processor( this.imgDir, this.trans, this.textTypes ); // it auto starts
this.dialog = this.typeList = this.textTypes = null;
this.inputs = this.ignore = null;
} ), false );
dlg.cancelElement = act.add( "button", UND, "Cancel" );
};
/*****************************************************************************/
/**
* Test parse the trans and output the results
**/
UserInterface.prototype.testTrans = function()
{
this.trans.init( this.transFile, this.textTypes,
this.pageMatch.text, this.ignore.text );
var trans = this.trans.getText();
var file = new File( this.transFile.path + "/test.txt" );
var test = file.saveDlg( "Save Test File" );
if ( !test )
{
return;
}
try {
test.open( "w" );
test.write( trans );
test.close( trans )
test.execute();
}
catch ( e )
{
alert( "Error writing test file: " + e );
}
};
/**
* Add a panel to the dialog to show the current text types
**/
UserInterface.prototype.addTypeList = function( listGrp )
{
var typeList = listGrp.add( "listbox", UND );
typeList.size = [ 100, 200 ];
// assign onChange event handler directory, addEventListener doesn't seem to
// work with CS5
typeList.onChange = this.bind( function()
{
if ( typeList.selection ) // no selection if clicking in empty area
{
var values = this.textTypes[ typeList.selection.text ];
for ( var n in this.inputs )
this.restoreInput( this.inputs, values, n );
}
} );
this.typeList = typeList;
};
/**
* Restore input to a saved value
**/
UserInterface.prototype.restoreInput = function( inputs, values, name )
{
var input = inputs[ name ];
switch ( name )
{
case "font":
case "kern":
case "units":
for ( var i = 0, item; item = input.items[ i ]; i++ )
if ( item.text == values[name] )
{
input.selection = i;
break;
}
break;
case "delimRb":
this.toggleMatch( values[ name ] );
case "regexRb": // only one radio button is stored/needed
break;
case "replaceChk":
this.toggleReplace( values[ name ] );
case "color":
input.value = values[ name ];
break;
default:
input.text = ( values[ name ] || "" );
}
};
/**
* Add buttons for saving and loading config files
**/
UserInterface.prototype.addConfigButtons = function( parent )
{
var group = parent.add( "group { orientation: 'column' }" );
group.add( "button", UND, "Load..." ).
addEventListener( "click", this.bind( this.loadConfig ), false );
group.add( "button", UND, "Save As..." ).
addEventListener( "click", this.bind( this.saveConfig ), false );;
};
/**
* Add fields for the trans text to match the text type against
**/
UserInterface.prototype.addMatchInput = function( inputGrp )
{
var pnl = inputGrp.add(
"panel { text: 'Match', orientation: 'column', \
alignChildren: 'left', \
grpLR: Group { \
grpL: Group { orientation: 'column', \
rb1: RadioButton {}, \
rb2: RadioButton {} \
}, \
grpR: Group { orientation: 'column', \
grp1: Group {" +
this.getRes( { lbl: "Start:", name: "start", grp: "grp1a" }) +
"," +
this.getRes( { lbl: "End:", name: "end", grp: "grp1b" } ) +
"}, \
grp2: Group {" +
this.getRes( { lbl: "RegExp:", name: "regexp", width: 200,
grp: "grp", text: "(.*)" } ) +
"}, \
} \
} \
}" );
this.initMatchInput( pnl );
};
UserInterface.prototype.initMatchInput = function( pnl )
{
var grpR = pnl.grpLR.grpR, grpL = pnl.grpLR.grpL;
this.inputs.delimRb = grpL.rb1;
this.inputs.regexRb = grpL.rb2;
var start = this.inputs.start = grpR.grp1.grp1a.el;
var end = this.inputs.end = grpR.grp1.grp1b.el;
this.inputs.regexp = grpR.grp2.grp.el;
start.onChanging = this.bind( this.updateRegExp );
end.onChanging = this.bind( this.updateRegExp );
grpL.rb1.addEventListener( "click", this.bind( this.toggleMatch ), false );
grpL.rb2.addEventListener( "click", this.bind( this.toggleMatch ), false );
this.setDisabled( grpR.grp1.grp1a );
this.setDisabled( grpR.grp1.grp1b );
this.setDisabled( grpR.grp2.grp, true );
grpL.rb1.value = true;
}
/**
* Update regex field as the delimiter fields are updated
**/
UserInterface.prototype.updateRegExp = function()
{
var sText = this.inputs.start.text.replace( /([^\w])/g, "\\$1" );
var eText = this.inputs.end.text.replace( /([^\w])/g, "\\$1" );
var allDelim = "", tt = this.textTypes;
for ( var delim in tt )
allDelim += tt[ delim ].start + tt[ delim ].end;
var capture1 = "(.+)", capture2 = "";
if ( eText )
{
capture1 = "(.+?)"; // don't grab two instances of a text type in a line
capture2 = "(.*)"; // capture any trailing text
}
// if no delims are set, capture up to the start of another type
// otherwise, capture everything between the delims.
// this should always be the last one added TODO: dynamically update
var captr = ( !sText && !eText ) ?
"([^" + allDelim.replace(/([\^\-\]\\])/g, "\\$1" ) + "]+)" :
capture1;
// capture any text at the end as well for further processing
this.inputs.regexp.text = "^" + sText + captr + eText + capture2;
};
/**
* Toggle enabled status of match inputs, and sets/restores radio button value
**/
// eventlistener fires at different times for cs3/4 so can't use rb.value
// track this on our own
UserInterface.prototype.delimRbValue = true;
UserInterface.prototype.toggleMatch = function( event )
{
var value = this.delimRbValue;
var delimRb = this.inputs.delimRb;
if ( event.target )
{
if ( ( event.target == delimRb && value == false ) ||
( event.target != delimRb && value == true ) )
value = !value;
}
else
{
value = event; // selection changed, restore radio button values
this.inputs.delimRb.value = value;
this.inputs.delimRb.parent.rb2.value = !value;
}
this.delimRbValue = value;
var grp1 = this.inputs.start.parent.parent;
var grp2 = this.inputs.regexp.parent.parent;
grp1.enabled = value;
grp1.grp1a.el.enabled = value;
grp1.grp1b.el.enabled = value;
grp2.grp.el.enabled = !value;
grp2.grp.enabled = !value;
};
/**
* Add fields to input trans text to replace for the text type
**/
UserInterface.prototype.addReplaceInput = function( inputGrp )
{
var grp = inputGrp.add(
"panel { orientation: 'row', text: 'Replace', \
replGrp: Group { orientation: 'column', \
alignChildren: 'left', \
grp: Group { \
chk: Checkbox { name: 'replacechk' }," +
this.getRes( { lbl: "Replace:", name: "match", grp: "grp1" } ) +
this.getRes( { lbl: "With:", name: "replace", grp: "grp2" } ) +
"}," +
"} \
}" ).replGrp.grp;
this.inputs.replaceChk = grp.chk;
this.inputs.match = grp.grp1.el;
this.inputs.replace = grp.grp2.el;
this.setDisabled( grp.grp1, true );
this.setDisabled( grp.grp2, true );
grp.chk.addEventListener( "click", this.bind( this.toggleReplace ), false );
};
/**
* Toggle the enabled state of the replace controls and groups
**/
UserInterface.prototype.replaceChkValue = false;
UserInterface.prototype.toggleReplace = function( setting )
{
var value = ( setting.target ) ? !this.replaceChkValue : setting;
this.inputs.match.enabled = value;
this.inputs.match.parent.enabled = value;
this.inputs.replace.enabled = value;
this.inputs.replace.parent.enabled = value;
this.replaceChkValue = value;
};
/**
* Add fields to input font properties for the text type
**/
UserInterface.prototype.addFontInput = function( inputGrp )
{
var fontPnl = inputGrp.add(
"panel { alignChildren: 'left', text: 'Font' }" );
var font = fontPnl.add( this.getRes( { type: "DropDownList", lbl: "Font:",
name: "font", width: 210 } ) ).el;
this.populateFontList( font );
this.inputs.font = font;
this.addSizeSelect( fontPnl.add( "group {}" ) );
this.addColorSelect( fontPnl.add( "group {}" ) );
this.addKernSelect( fontPnl );
};
/**
* Add buttons for adding and deleting new text types from the list
**/
UserInterface.prototype.addInputButtons = function( inputGrp )
{
var inputBtnGrp = inputGrp.add( "group { orientation: 'row' }" );
var del = inputBtnGrp.add( "button", UND, "Delete" );
del.addEventListener( "click", this.bind( this.delTextType ), false );
var add = inputBtnGrp.add( "button", UND, "Add" );
add.addEventListener( "click", this.bind( this.addTextType ), false );
};
/*****************************************************************************/
/**
* Load config from file in JSON format
**/
UserInterface.prototype.loadConfig = function()
{
var config;
var cfgFile = new File().openDlg( "Select config", "Ini File: *.ini" );
if ( cfgFile && cfgFile.exists )
{
cfgFile.open( "r" );
config = cfgFile.read();
cfgFile.close();
// clear list and overwrite textTypes with saved values
this.typeList.removeAll();
this.textTypes = eval( config );
for ( var type in this.textTypes ) // re-populate the list
this.typeList.add( "item", type );
}
};
/**
* Save config to file in JSON format
**/
UserInterface.prototype.saveConfig = function()
{
var file = new File( "config.ini" );
var config = file.saveDlg( "Save Config File" );
if ( !config )
return;
var JSON = this.getJSON();
try {
config.open( "w" );
config.write( JSON );
config.close( JSON )
// config.execute();
}
catch ( e )
{
alert( "Error writing config file: " + e );
}
};
UserInterface.prototype.getJSON = function()
{
var JSON = "(function(){return{";
var str = "", ta = [];
for ( var t in this.textTypes )
{
var tt = this.textTypes[t];
var pa = [];
for ( var p in tt )
{
var p_str = "";
switch ( typeof tt[p] )
{
case "string":
p_str += "'" + tt[p].replace(/\\/g, "\\\\") + "'";
break;
case "boolean":
p_str += tt[p];
break;
case "object":
p_str += "[" + tt[p] + "]";
break;
}
pa.push( p + ":" + p_str );
}
ta.push( "'" + t + "':{" + pa + "}" );
}
JSON += ta;
JSON += "}})();";
return JSON;
};
/**
* Adds font size and unit selection inputs to the dialog
**/
UserInterface.prototype.addSizeSelect = function( grp )
{
var res = this.getRes( { lbl: "Font Size:", name: "size", width: 50 } );
var size = grp.add( res ).el;
size.text = "12";
res = this.getRes( { type: "DropDownList", name: "units", width: 50 } );
var units = grp.add( res ).el;