-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text Perspective Compensator.jsx
1384 lines (900 loc) · 38.6 KB
/
Text Perspective Compensator.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
/*!
* Text Perspective Distortion Compensation v.0.0.7
* https://github.com/kefiijrw/Text-Perspective-Distortion-Compensation-for-Ai
*
* Author: Sergey Nikolaev
* kefiijrw.com
*
* Date: 2022-11-02
*
*/
// show the settings panel on startup
var ui = true;
//do we write in the log file
var debug_mode = false;
// ratio millimeters to points
var mm = 2.834645;
// Distance to the target in centimeters
var distance = 250;
// compensation coefficient (vertical stretching) from 0 to 1
// 0 – without compensation
// 1 — the top line is optically exactly like the bottom line
var perspective_coeff = 0.5;
// character shape compensation coefficient (horizontal stretching) from 0 to 1
// 0 – letters are stretched only vertically
// 1 — letters are stretched horizontally as much as vertically
var scale_coeff = 0;
// leading compensation coefficient from 0 to 1
// 0 — leading remains the same
// 1 — leading will increase as much as the height of the characters
var leading_coeff = 0;
// eye level of the viewer, determine by the line in the eyes_level layer, converted to cm
var eyes_level;
// How to make the script increase the vertical scale of the letters:
// true – letters grows only upwards to keep their baselines in place
// false – letters grows in both directions - up and down - and generally stays in its place
var keep_baseline = false;
// when the script runs when nothing is selected:
// false – alert warning
// true – process all text fields
var all_text = true;
// Working with current documents
var doc = app.activeDocument;
// a global variable (sorry) to store the current text being processed
var current_txt;
/*
SOME HELPING FUNTIONS
*/
//write a line in the log file (sorry, bad habit)
function echo(what) {
if (debug_mode)
log_file.write(what + '\n');
}
// rounding values of coefficients to .00 to show in inputs
function roundOff(x) {
var place = 100;
x = Math.round(x * place);
x /= place;
return x;
}
// return the line in the right language, depending on the illustrator's locale
function loc(vars) {
if (app.locale.indexOf('ru_') == 0)
return vars.ru;
else
return vars.en;
}
// Open link.
// Took it from here: https://community.adobe.com/t5/indesign/js-scriptui-url-links-in-dialogues/td-p/4572773?page=1
function GotoLink(url) {
url = url || "https://github.com/kefiijrw/Text-Perspective-Distortion-Compensation-for-Ai";
if (!/^http/.test('' + url)) {
url = 'https://' + url;
}
if (app.version > 6) {
if (File.fs == "Macintosh") {
var body = 'tell application "Finder"\ropen location "' + url + '"\rend tell';
app.doScript(body, ScriptLanguage.APPLESCRIPT_LANGUAGE);
} else {
var body = 'dim objShell\rset objShell = CreateObject("Shell.Application")\rstr = "' + url + '"\robjShell.ShellExecute str, "", "", "open", 1 ';
app.doScript(body, ScriptLanguage.VISUAL_BASIC);
}
}
else {
var linkBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' + url + '"></head><body> <p></body></html>';
var linkJumper = File(Folder.temp.absoluteURI + "/link.html");
linkJumper.open("w");
linkJumper.write(linkBody);
linkJumper.close();
linkJumper.execute();
}
}
/*
CONFIG OPERATION FUNCTIONS
*/
// Read presets from illustrator settings
function init_configs() {
echo('init_configs');
//flag in the illustrator settings saying that the script has already been run before
var was = app.preferences.getIntegerPreference('perspective_compensation_script_first_launch_already_was');
if (was != 6) { //very dumb, sorry. it didn't work to save boolean so 6 is true and 2 is false
//first launch
echo('first_launch!');
//save these values in illustrator settings
from_vars_to_prefs();
//remember that first launch was
app.preferences.setIntegerPreference('perspective_compensation_script_first_launch_already_was', 6);
} else {
echo('not first launch');
//not first launch
//read script settings from illustrator settings
from_prefs_to_vars();
}
}
// saving current state (coefficients, presets, checkboxes) to the illustrator settings, so they can be recovered again after restarting the illustrator
function from_vars_to_prefs() {
echo('from_vars_to_prefs ');
app.preferences.setRealPreference('perspective_compensation_script_current_distance', distance);
app.preferences.setRealPreference('perspective_compensation_script_current_perspective_coeff', perspective_coeff);
app.preferences.setRealPreference('perspective_compensation_script_current_scale_coeff', scale_coeff);
app.preferences.setRealPreference('perspective_compensation_script_current_leading_coeff', leading_coeff);
}
//opposite, restoring the state of the script from the saved illustrator settings
function from_prefs_to_vars() {
echo('from_prefs_to_vars');
distance = app.preferences.getRealPreference('perspective_compensation_script_current_distance');
perspective_coeff = app.preferences.getRealPreference('perspective_compensation_script_current_perspective_coeff');
scale_coeff = app.preferences.getRealPreference('perspective_compensation_script_current_scale_coeff');
leading_coeff = app.preferences.getRealPreference('perspective_compensation_script_current_leading_coeff');
}
//full reset, as if the script had never been run before
function factory_reset() {
echo('factory_reset');
app.preferences.removePreference('perspective_compensation_script_first_launch_already_was');
app.preferences.removePreference('perspective_compensation_script_current_distance');
app.preferences.removePreference('perspective_compensation_script_current_perspective_coeff');
app.preferences.removePreference('perspective_compensation_script_current_scale_coeff');
app.preferences.removePreference('perspective_compensation_script_current_leading_coeff');
}
/*
CORE FUNCTIONS
*/
// root function
function main() {
echo('=====================================');
echo('main with settings ' + distance + '; ' + perspective_coeff + '; ' + scale_coeff + '; ' + leading_coeff);
var where;
// determining eye level
try{
if(doc.layers.getByName('eyes_level').pathItems.length > 0){
var obj = doc.layers.getByName('eyes_level').pathItems[0];
} else if( doc.layers.getByName('eyes_level').groupItems.length > 0){
var obj = doc.layers.getByName('eyes_level').groupItems[0].pathItems[0];
}
eyes_level = (-obj.top) / (10 * mm);
} catch(err){
alert(loc({'ru':'Нет отметки уровня глаз\nДля скрипта нужен слой eyes_level и линия в нем, определяющая уровень глаз относительно макета', 'en':'No eye level marker\nYou need the eyes_level layer and a line in it that determines the level of the eyes relative to the text'}));
return false;
}
//which text fields we process
if (doc.selection.length > 0) {
where = doc.selection;
} else {
if(all_text){
where = doc.textFrames;
} else{
alert(loc({'en':'Nothing selected\nSelect text fields and run the script again', 'ru':'Ничего не выделено\nВыделите текстовые объекты и запустите скрипт снова'}));
return false;
}
}
//recursive walkover of the selection and processing of text fields within it
//get the number of processed text fields
var total = recursive_txt(where);
//check if there were any text fields in the selection at all
if(total == 0){
alert(loc({'en':'Nothing selected\nSelect text fields and run the script again', 'ru':'Ничего не выделено\nВыделите текстовые объекты и запустите скрипт снова'}));
return false;
}
return true;
}
/**
* recursive parsing of where_is content in search of text fields
*/
function recursive_txt(where_is) {
echo('=recursive_txt');
echo(where_is.length);
//number of processed text fields
var found = 0;
/**
* where_is can be a group or a text field or just a set of different selected objects
* for each possible case we try our own methods
*/
try {
// If it is a set of objects
for (var j = 0; j < where_is.length; j++) {
echo(j + ' of ' + where_is.length + '; try 1');
echo(where_is[j].typename);
if (where_is[j].typename == 'TextFrame') {
// text box - let's do it!
calc_txt(where_is[j]);
found++;
} else if (where_is[j].typename == 'GroupItem') {
// group - let's go down a level
found += recursive_txt(where_is[j]);
}
}
} catch (e) { }
//If there are text fields in where_is, run the algorithm on them
try {
for (var j = 0; j < where_is.textFrames.length; j++) {
echo(j + ' try 2');
calc_txt(where_is.textFrames[j]);
found++;
}
} catch (e) { }
//If there are groups in where_is, search through them
try {
for (var j = 0; j < where_is.groupItems.length; j++) {
echo(j + ' try 3');
found += recursive_txt(where_is.groupItems[j]);
}
} catch (e) { }
return found;
}
/**
* Root function of txt text field processing
* Makes a few checks and starts the job
*/
function calc_txt(txt) {
echo('==calc_txt');
echo(' ');
// if the text field is shown...
if (txt.hidden == false && txt.layer.visible == true) {
// ...then let's do it!
try {
current_txt = txt;
compensate_text(txt);
} catch (err) {
echo('ERROR ' + err);
}
}
}
/**
* Compensates perspective distortions of the txt text field
*/
function compensate_text(txt) {
echo('===compensate_text');
// text field type (Point Type, Area Type, Path Text).
// the script works with the first two options, it ignores the Path texts
var kind = txt.kind;
/**
* [y] stores the coordinate of the current line needed to calculate the distortions.
* Illustrator does not provide line coordinates, so we have to count it
* ourselves by scanning the text field line by line and adding leading.
* For the first line we take the coordinate of the whole text field...
*/
var y = txt.top;
/**
* ...and slightly lower so that we land somewhere on the level of the center of the lowercase letters.
* The leading of the first line has no effect on anything, so we take the font size.
* But actually, the position of the typing line depends on the font metrics, and there is no access to them,
* so this is just a approximate targeting of the letters of an average font
*/
y -= txt.lines[0].characterAttributes.size / 2;
if (kind == TextType.AREATEXT) {
/**
* If it is an area type, the text is divided into paragraphs and within them into lines
*/
echo('AREA TYPE');
// go through the list of paragraphs
for (var j = 0; j < txt.paragraphs.length; j++) {
echo(' ');
echo(' ');
echo('paragraph ' + j);
var par = txt.paragraphs[j];
//if it is not the first paragraph, then count the margin before it
if (j != 0) {
y -= par.paragraphAttributes.spaceBefore;
}
// go through the list of lines in the paragraph
for (var i = 0; i < par.lines.length; i++) {
var line = par.lines[i];
// echo(' ');
// echo(' ');
// echo(' >line ' + i + ': ' + line.contents);
// echo(' >line len is ' + line.characters.length);
// process this line, in response we get an updated y-coordinate,
// considering the distortions and the changed position of the line
y = compensate_line(par, i, i + j == 0, y);
// echo(' >new line len is ' + par.lines[i].length);
// echo(' >new line is ' + par.lines[i].contents);
}
// echo('end of paragraph' +y);
//don't forget about the margin at the end
y -= par.paragraphAttributes.spaceAfter;
}
} else if (kind == TextType.POINTTEXT) {
/**
* If it is a point type, it`s stupid: instead of splitting
* into paragraphs the text is split into lines only, but at the end
* of some lines (which are inside the paragraph) there is a forced line break,
* and at the end of other lines (the end lines of the paragraph)
* there is a usual line break character \r.
*/
echo('POINT TYPE');
// We take all the text and divide it by the carriage return character, so we divide it into paragraphs
var cont_par = txt.contents.split('\r');
// Paragraph text is divided to lines by the Force break character
for (var i = 0; i < cont_par.length; i++) {
cont_par[i] = cont_par[i].split('');
}
/**
* So we get cont_par, a 2-dimensional array with the text of the lines inside the paragraphs.
* But we must still interact with the real lines of the illustrator
* So we'll loop through the lines of the text field,
* but at the same time we'll watch where we are in the
* two-dimensional array of lines, and so we will catch
* the beginnings and ends of paragraphs. Hail to the Shitcode!
*/
//the coordinates of the current line in the two-dimensional array cont_par
var j = 0, k = 0;
// flag "this line was the last one in the paragraph"
var was_last = false;
// go through all the lines of the text box
for (var i = 0; i < txt.lines.length; i++) {
var line = txt.lines[i];
echo('>line ' + i);
//if the last line of the paragraph was before it, then add a pre-paragraph margin
if (was_last == true) {
y -= line.paragraphAttributes.spaceBefore;
was_last = false;
}
// process this line, in response we get an updated y-coordinate,
// considering the distortions and the changed position of the line
y = compensate_line(txt, i, i == 0, y);
//synchronize line coordinates in a two-dimensional array
if (line.contents == cont_par[j][k]) {
//if it is the last line in the paragraph,
if (k == cont_par[j].length - 1) {
// echo('LAST');
// then add post-abbreviation access
y -= line.paragraphAttributes.spaceAfter;
j++;
k = 0;
was_last = true;
} else {
k++;
}
} else {
echo('WTF:');
echo(line.contents);
echo(cont_par[j][k]);
}
}
}
}
/**
* The function compensates for distortions for the line.
* The string is not passed directly, but as `line_n` number in the `txt`
* text field to be able to communicate with neighboring lines.
*
* @param {textFrame} txt - the text field or paragraph to which the line belongs
* @param {number} line_n - the line number in this text field
* @param {bool} is_first_line - is this the first line in the text field
* @param {number} y - [y] coordinate of the line
* return the updated [y] coordinate after all distortions
*/
function compensate_line(txt, line_n, is_first_line, y) {
// echo('====compensate_line');
var line = txt.lines[line_n];
// count the original leading of the string before our intervention
var original_leading = calc_leading(line);
// echo(' original leading is '+original_leading);
// if it is not the first line of the first paragraph...
if (!is_first_line) {
// ...then lower the coordinate by the leading of the line being counted,
// so that it indicates the character level
y -= original_leading;
// echo(' down step original leading = ' + original_leading);
} else {
// echo(' first line, ignoring original leading');
}
/**
* Since after calculating the stretch at [y] level the line
* with leading compensation will shift vertically,
* the compensation will be invalid (it was calculated for one position,
* and the line now stands in another), and for this you must recalculate
* for the new level of the line. And then another and another...
* It is determined by experience that in three iterations the process
* converges to an error of 0.06% (at least on the test layouts).
*/
//number of counts
var cicle_count = 1;
/**
* It makes sense to do this only if the leading is compensated,
* which means that the line is moved vertically because of compensation
* for distortion, and if it is not the first line of the text field,
* because its leading has no effect at all on anything
* */
if (leading_coeff > 0 && !is_first_line) {
//TODO: instead of the approximate number iterate until some accuracy is reached
cicle_count = 2;
}
var previus_leading = original_leading;
//approaches to string counting
for (var i = 0; i < cicle_count; i++) {
// echo(' ');
// echo(' trying ' + (i+1) + ', calculate for y = ' + y);
//we apply the distortions and get a new line leading in response
var new_leading = calc_line(txt, line_n, y, original_leading, i);
//if it is not the first line of the first paragraph
if (!is_first_line) {
// echo(' new_leading = ' + new_leading);
/**
* Correct the [y] coordinate by the difference between the previous
* and the new leading, so that it reaches the shifted line
* */
y -= (new_leading - previus_leading);
} else {
// echo(' ITS first line, ignore leading changes');
}
previus_leading = new_leading;
}
return y;
}
/**
* Function to determine the leading of a line.
*/
function calc_leading(line) {
/**
* Since the real vertical offset of a line from the previous line
* depends on the maximum leading of characters of this line,
* we will go through the line symbol by symbol.
* We should also remember about possible auto leading.
* */
var max_leading = -1;
var lead;
// if the line has auto leading
// TODO: but what if different characters of this line have different values?
if (line.characterAttributes.autoLeading == true) {
// echo('autoLeading');
//go around all the characters in the line
for (var i = 0; i < line.characters.length; i++) {
//count through the autoLeading value (percentage of the fontsize). convert it to points
lead = line.characters[i].characterAttributes.size * line.characters[i].paragraphAttributes.autoLeadingAmount / 100;
//Update maximum
if (lead > max_leading) {
max_leading = lead;
}
}
} else {
//if the leading is set directly
// echo('not autoLeading');
for (var i = 0; i < line.characters.length; i++) {
// It's simpler here - we just take it from the properties
lead = line.characters[i].characterAttributes.leading;
if (lead > max_leading) {
max_leading = lead;
}
}
}
// echo(max_leading);
return max_leading;
}
/**
* The function where transformations are directly applied to the line
* The line here is also passed indirectly, as in the parent function compensate_line()
* @param {*} txt - the text field or paragraph to which the line belongs
* @param {*} line_n - the line number in this text field
* @param {*} y - [y] coordinate of the line
* @param {*} original_leading the original line spacing. Since this function can be
* applied to a line several times (see compensate_line),
* the line may already have a compensated leading,
* so the original leading is saved for calculation.
*
* Return the new leading of this line.
*/
function calc_line(txt, line_n, y, original_leading, iteration) {
echo('=====calc_line');
var line = txt.lines[line_n];
/**
* If this is not the first run of the line,
* the leading is already stretched,
* and if some characters are migrated to the next line,
* they will stretch it with their leading.
* So reset the leading - it will still be set later,
* but only to characters that remain on the line.
*/
if(iteration != 0){
line.characterAttributes.leading = original_leading;
}
/**
* we set the coordinates of the line in cm,
* so that it is in the same dimensionality
* as distance and eye_level
*/
var y_in_cm = -y / (10 * mm);
/**
* Calculate the compression ratio by the distance of the line from eye level.
* y_comp ranges from 1 (no distortion) to infinity (super distortion)
*/
var y_comp = compensation(Math.abs(y_in_cm - eyes_level));
/**
* Recalculate with the distortion coefficients
* The set of values is the same, from 1 (no distortion) to infinity
* Ranges the same: from 1 (no distortion) to infinity (super distortion)
*/
var y_perspective_coeff = (y_comp - 1) * perspective_coeff + 1;
var y_scale_coeff = (y_comp - 1) * scale_coeff + 1;
// echo('y_in_cm: '+y_in_cm);
// echo('eyes_level: '+eyes_level);
// echo('d: '+Math.abs(y_in_cm - eyes_level));
echo('coef: '+y_comp.toFixed(2));
// echo('y_perspective_coeff: '+y_perspective_coeff);
// echo('y_scale_coeff: '+y_scale_coeff);
//horizontal scale of line characters
line = set_hor_scale(line, y_perspective_coeff, y_scale_coeff, txt, line_n);
//vertical scale
set_vert_scale(line,y_perspective_coeff, y_scale_coeff);
//leading
var upd_leading = set_leading(line, original_leading, leading_coeff);
return upd_leading;
}
// Calculating the horizontal scale of line characters
function set_hor_scale(line, y_perspective_coeff, scale_coeff, txt, line_n){
/**
* There is a horizontal stretching of characters,
* which can make the text in the Area Type not fit
* and jump from line to line, it is necessary
* to catch all sorts of side effects
*/
//only if the horizontal scale is affected
if (scale_coeff == 0){
return line;
}
// var hor_scale_compensated = 100 + (y_perspective_coeff * 100 - 100) * scale_coeff;
var hor_scale_compensated = 100 * scale_coeff;
// echo('>>>' + line.start + ' -> ' + line.end);
// echo('horizontalScale: ' + hor_scale_compensated);
//If it is a Point text
if(current_txt.kind == TextType.POINTTEXT){
//apply a horizontal stretch to the symbols that corresponds to their position
line.characterAttributes.horizontalScale = hor_scale_compensated;
//If it is a Area Type
} else if(current_txt.kind == TextType.AREATEXT){
// echo('>>>' + line.start + ' -> ' + line.end);
/**
* Now the line may not be on its own line at all:
* some characters may have flowed to the next line,
* some, on the other hand, narrowed and jumped
* to the previous line, where they did not fit
* with the previous level of stretch.
* It is time to hunt for runners.
*/
// the text field or paragraph in which everything happens
// TODO: Is it necessary to create this variable here or can the original be spoiled as well?
var range = txt;
/**
* First, apply the horizontal stretch of the current line
* to all characters below, so that if someone from the next
* line decides that he can now fit on the previous line as well,
* he will do so now.
*/
//set the beginning of this text range to the beginning of our string, preserving the original value
var w = range.start;
range.start = line.start;
range.characterAttributes.horizontalScale = hor_scale_compensated;
//Return the limits of the text range back
range.start = w;
//see what's on this line now after all the characters have potentially escaped to the lines before and after
var line_updated = txt.lines[line_n];
// echo('length before horizontalScale: ' + line.length);
// echo('length after horizontalScale: ' + line_updated.length);
// echo(line.contents + ' -> ' + line_updated.contents);
/**
* The variable line still stores all characters of the original string,
* even if they are spread over neighboring strings after stretching.
* So we can compare the original string with what appeared in its place after the distortion
*/
//where the new line is in the old line
var i1 = line.contents.indexOf(line_updated.contents);
//where the old line is in the new
var i2 = line_updated.contents.indexOf(line.contents);
// echo(i1 + ' & ' + i2);
if (i1 == 0 && i2 == 0) {
// lines are equivalent, i.e. the line remains in its place completely, we can do nothing additionally
// echo('equal');
} else if (i1 == 0 && i2 == -1) {
/**
* The line is reduced, for example:
* Lorem ipsum dolor sit amet, consectetuer adipiscing -> Lorem
* So some characters did not fit and ran to the next line
* Override the line variable so that only what is actually
* left on the line gets the rest of the distortion
*/
// echo('SHORTER, CHANGE LINE');
line = line_updated;
// echo('new line is ' + line.contents);
} else if ( i1 == -1 && i2 == 0 ) {
/**
* The line is extended, for example:
* consec -> consectetuer
* So some characters from the next line used to be even wider,
* and now they've narrowed down a bit and returned to this line
* Same thing, redefine the variable line
*/
// echo('LONGER, CHANGE LINE');
line = line_updated;
// echo('new line is ' + line.contents);
} else if ( i1 != 0 && i2 == -1 ) {
/**
* The beginning of the line moved to the previous line, for example:
* elit, sed diam -> sed diam
* At the end, new characters may also be added because of the empty space.
* Since the characters jumped up there is not appropriate
* (they are there only because they have a horizontal stretch factor
* calculated for the line below, and if you recalculate it for the new level,
* they again will not fit and go back, but again, with another level of
* stretch, etc., etc.), we must bring them back down.
* To do this, the "do not break" style is applied to these escaped
* and following characters, then they will not be able to
* fit on the top line and will return to their homeland.
*/
// echo('LETS TURN BACK TO THIS STRING');
// echo(line.start + ' -> ' + line.end);
// echo(line_updated.start + ' -> ' + line_updated.end);
/**
* We go from the beginning of the original string and
* end with the beginning of the updated one, That is,
* these are just the characters that ran to the previous line
*/
for (var i = line.start; i <= line_updated.start; i++) {
// echo(i+': '+txt.characters[i].contents);
//TODO: would it be faster if not one character at a time, but the range at once?
txt.characters[i].characterAttributes.noBreak = true;
}
// echo('CHANGE LINE');
/**
* Since we put those characters back and now don't know where anything is,
* instead of assigning line_updated we take the string again and give it a stretch
* TODO: Why can't you do that in other places?
*/
line = txt.lines[line_n];
line.characterAttributes.horizontalScale = hor_scale_compensated;
// echo('new line is ' + line.contents);
} else {
echo('WTF');
}
}
return line;
}
// Calculating the vertical scale of line characters
function set_vert_scale(line, y_perspective_coeff, y_scale_coeff){