forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua.Relocator.lua
3492 lines (3196 loc) · 140 KB
/
ua.Relocator.lua
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
-- Hyperdimensional Relocator offers a plethora of functions, focusing primarily on \pos, \move, \org, \clip, and rotations.
-- Check Help (Space Travel Guide) for detailed description of all functions.
script_name="Hyperdimensional Relocator"
script_description="Advanced metamorphosis of multidimensional coordinates"
script_author="reanimated"
script_url="http://unanimated.hostfree.pw/ts/relocator.lua"
script_version="4.5.2"
script_namespace="ua.Relocator"
local haveDepCtrl,DependencyControl,depRec=pcall(require,"l0.DependencyControl")
if haveDepCtrl then
script_version="4.5.2"
depRec=DependencyControl{feed="https://raw.githubusercontent.com/unanimated/luaegisub/master/DependencyControl.json"}
end
re=require'aegisub.re'
function cuts(subs,sel)
ADD=aegisub.dialog.display
ADP=aegisub.decode_path
ak=aegisub.cancel
ms2fr=aegisub.frame_from_ms
fr2ms=aegisub.ms_from_frame
keyframes=aegisub.keyframes()
ATAG="{%*?\\[^}]-}"
STAG="^{\\[^}]-}"
failures={}
relocated=0
seln=#sel
for i=1,#subs do if subs[i].class=="dialogue" then line0=i-1 break end end
end
function relocator(subs,sel,act)
cuts(subs,sel)
Repositioning={"Align X","Align Y","org to fax","clip to fax","clip to frz","clip to reposition","clip2pos fbf","frz+org2pos","horizontal mirror","vertical mirror","numbers","shake","fbf retrack","trampoline","sine loop","shadow layer","shadow repos.","space out letters","warp text","replicate","fbf X <--> Y","track by clip"}
Bilocator={"transmove","horizontal","vertical","multimove","clip2move","rvrs. move","shiftstart","shiftmove","move to","move clip","randomove","kill times","full times","set times","zig-zag"}
Morphing={"round numbers","line2fbf","join fbf lines","move v. clip","set origin","calculate origin","transform clip","set rotation","rotate 180","negative rot","spin doctor","vector2rect.","rect.2vector","clip scale","clip2scale fbf","find centre","extend mask","flip mask","adjust drawing","randomask","randomise...","letterbreak","wordbreak","clip info","[un]hide clip"}
Rounding={"all","pos","move","org","clip","mask"}
Freezing={"0","5","10","20","45","70","110","135","160","-5","-10","-20","-45","-70","-110","-135","-160"}
noneg="\\bord\\shad\\xbord\\ybord\\fs\\blur\\be\\fscx\\fscy_bord_shad_xbord_ybord_fs_blur_be_fscx_fscy"
hyperconfig={
{x=12,y=0,width=2,class="label",label="Teleport "},
{x=12,y=1,width=4,class="floatedit",name="eks",hint="X"},
{x=12,y=2,width=4,class="floatedit",name="wai",hint="Y"},
{x=0,y=0,width=3,class="label",label="&Repositioning Field"},
{x=0,y=1,width=2,class="dropdown",name="posi",value="clip to frz",items=Repositioning},
{x=0,y=2,width=2,class="floatedit",name="post",value=0,hint="disPosition\nAlign X/Y; Shake radius; Mirrors centre point;\nShadow layer: shad; Space Out Letters distance; Warp Text distance; fbf retrack accel"},
{x=0,y=3,class="checkbox",name="first",label="by first",value=true,hint="reference point = first line\n(Align X/Y; fbf X <--> Y, track by clip)"},
{x=1,y=3,class="checkbox",name="rota",label="rotate",hint="rotate mirrors\nreverse direction for fbf X <--> Y"},
{x=0,y=4,class="checkbox",name="layers",label="layers",value=true,hint="synchronise for layers\n(shake; fbf retrack)"},
{x=1,y=4,class="checkbox",name="smo",label="smooth",hint="smoothen shaking/retrack"},
{x=1,y=5,class="checkbox",name="sca",label="scaling",hint="add scaling to shake"},
{x=0,y=6,class="label",label=" &Force:"},
{x=1,y=6,width=3,class="floatedit",name="force",value=0,hint="shake: scaling value\nfbf retrack: smoothening force\njoin fbf lines: # of lines\nclip scale: scale factor\nrandomask: randomness factor"},
{x=3,y=0,width=3,class="label",label="Soul &Bilocator"},
{x=3,y=1,width=2,class="dropdown",name="move",value="transmove",items=Bilocator},
{x=3,y=2,width=2,class="checkbox",name="keep",label="keep both",hint="keeps both lines for transmove/mirrors"},
{x=3,y=3,width=4,class="checkbox",name="rotac",label="rotation acceleration",value=true,hint="transmove option"},
{x=3,y=4,class="checkbox",name="times",label="times",hint="set \\move times\n(transmove; clip2move; shiftmove; move to)"},
{x=4,y=4,class="checkbox",name="tra",label="\\&t",hint=""},
{x=3,y=5,width=3,class="checkbox",name="videofr",label="current frame",hint="set relevant timecode\nto current frame\n(shiftstart, shiftmove)"},
{x=6,y=0,width=2,class="label",label="&Morphing Grounds"},
{x=6,y=1,width=2,class="dropdown",name="mod",value="round numbers",items=Morphing},
{x=6,y=2,class="label",label="round:"},
{x=7,y=2,class="dropdown",name="rnd",items=Rounding,value="all"},
{x=7,y=3,class="dropdown",name="rndec",items={"1","0.1","0.01","0.001"},value="1",hint="rounding"},
{x=7,y=4,class="dropdown",name="freeze",items=Freezing,value="0"},
{x=6,y=4,class="checkbox",name="frz",label="frz",value=true,hint=""},
{x=6,y=5,class="checkbox",name="frx",label="frx",hint=""},
{x=7,y=5,class="checkbox",name="fry",label="fry",hint=""},
{x=6,y=6,width=2,class="checkbox",name="delfbf",label="delete orig. line",value=true,hint="delete original line for line2fbf"},
{x=7,y=7,class="checkbox",name="c2fbf",label="clip2fbf",value=true,hint="line2fbf: shift clip along with \\move"},
{x=3,y=7,class="checkbox",name="why",label="why?",hint="provides information about the success/failure of operations performed"},
{x=4,y=7,class="checkbox",name="X",label="X",hint="Apply to X coordinates\n(fbf retrack, track by clip, clip2move)",value=true},
{x=6,y=7,class="checkbox",name="Y",label="Y",hint="Apply to Y coordinates\n(fbf retrack, track by clip, clip2move)",value=true},
{x=8,y=0,width=3,class="label",label="Cloning Laboratory"},
{x=8,y=1,width=2,class="checkbox",name="cpos",label="\\&posimove",value=true},
{x=10,y=1,class="checkbox",name="corg",label="\\&org",value=true},
{x=8,y=2,class="checkbox",name="cclip",label="\\[&i]clip",value=true},
{x=9,y=2,width=2,class="checkbox",name="ctclip",label="\\t(\\[i]c&lip)",value=true},
{x=8,y=6,width=4,class="checkbox",name="cre",label="replicate missing tags",value=true,hint="creates tags if they're not present"},
{x=8,y=3,width=2,class="checkbox",name="stack",label="stack clips",hint="allows stacking of 1 normal\nand 1 vector clip in one line"},
{x=8,y=5,width=3,class="checkbox",name="copyrot",label="copy rotations",hint="copies frz, frx, and fry"},
{x=10,y=3,width=3,class="checkbox",name="klipmatch",label="match type",hint="matches clip type (clip/iclip)"},
{x=8,y=4,width=3,class="checkbox",name="combine",label="combine vectors",hint="vector clips are merged into one\ninstead of replaced"},
{x=13,y=3,width=2,class="checkbox",name="tppos",label="pos",value=true},
{x=13,y=4,width=2,class="checkbox",name="tpmov",label="move",value=true},
{x=15,y=3,class="checkbox",name="tporg",label="org",value=true},
{x=15,y=4,class="checkbox",name="tpclip",label="clip",value=true},
{x=12,y=4,class="checkbox",name="tpc1",label="c1",value=true,hint="affect top left corner of rectangular clip"},
{x=12,y=5,class="checkbox",name="tpc2",label="c2",value=true,hint="affect bottom right corner of rectangular clip"},
{x=15,y=5,class="checkbox",name="tpexp",label="exp",hint="expand rectangular clip in opposite directions"},
{x=13,y=5,width=2,class="checkbox",name="tpmask",label="mask"},
{x=14,y=0,width=2,class="checkbox",name="warp",label="&Warp",hint="Warped Teleport"},
{x=12,y=6,width=4,class="checkbox",name="autopos",label="pos with tags missing",value=true,hint="Teleport position when \\pos tags missing"},
{x=0,y=7,width=3,class="checkbox",name="space",label="SpaceTravel &Guide",
hint="The Typesetter's Guide to the Hyperdimensional Relocator."},
{x=8,y=7,width=2,class="checkbox",name="rpt",label="Repeat",hint="Repeat with last settings (any function)"},
{x=10,y=7,width=3,class="checkbox",name="save",label="Save config",hint="Save current configuration"},
{x=13,y=7,width=3,class="label",label="[Incarnation "..script_version.."]"}
}
loadconfig()
if remember then
for key,val in ipairs(hyperconfig) do
if val.name=="posi" then val.value=lastpos end
if val.name=="move" then val.value=lastmove end
if val.name=="mod" then val.value=lastmod end
end
end
P,res=ADD(hyperconfig,
{"Po&sitron Cannon","Hyperspace Tra&vel","Met&amorphosis","&Cloning Sequence","Tel&eportation","Disintegrate"},{close='Disintegrate'})
if P=="Disintegrate" then ak() end
if imprint and res.rpt then res=imprint end
remember=true imprint=res
lastpos=res.posi lastmove=res.move lastmod=res.mod
if res.save then saveconfig() ak() end
if P=="Po&sitron Cannon" then if res.space then guide(subs,sel) else sel=positron(subs,sel) end end
if P=="Hyperspace Tra&vel" then
if res.move=="multimove" then multimove (subs,sel)
elseif res.move=="randomove" then randomove (subs,sel)
else bilocator(subs,sel) end
end
if P=="Met&amorphosis" then
aegisub.progress.title(string.format("Morphing..."))
if res.mod=="line2fbf" then sel=movetofbf(subs,sel)
elseif res.mod=="transform clip" then transclip(subs,sel,act)
elseif res.mod=="join fbf lines" then joinfbflines(subs,sel)
elseif res.mod=="spin doctor" then spindoc(subs,sel)
else modifier(subs,sel,act) end
end
if P=="&Cloning Sequence" then clone(subs,sel) end
if P=="Tel&eportation" then teleport(subs,sel) end
summary()
return sel
end
function positron(subs,sel)
if not ak then cuts(subs,sel) end
ps=res.post
shake={} shaker={}
count=0
relocated=0
nsel={} for z,i in ipairs(sel) do table.insert(nsel,i) end
if res.posi:match("fbf X") then
XYtab={}
for z,i in ipairs(sel) do
line=subs[i]
text=line.text
local X,Y=text:match("\\pos%(([%d.-]+),([%d.-]+)%)")
if not X then t_error("Line #"..i-line0..": Missing \\pos tag.\nAborting.",1) end
table.insert(XYtab,{x=X,y=Y})
end
if res.first then Xref=XYtab[1].x Yref=XYtab[1].y else Xref=XYtab[#XYtab].x Yref=XYtab[#XYtab].y end
end
if res.posi=="numbers" then LL=999999999
for z,i in ipairs(sel) do
line=subs[i]
lay=line.layer
if lay<LL then LL=lay end
end
ind=0 PNS={}
end
local re_fail
-- Replicate GUI
if res.posi=="replicate" then
rplGUI={
{x=0,y=0,width=2,class="label",label="Replicas:"},
{x=2,y=0,width=3,class="intedit",name="rep",value=repl or 3,min=1,hint="replicas to create"},
{x=5,y=0,width=2,class="label",label=" (This is excluding the original line)"},
{x=0,y=1,width=3,class="label",label="Distances for:"},
{x=3,y=1,width=1,class="dropdown",name="dtype",items={"each replica","last replica"},value="last replica"},
{x=4,y=1,width=2,class="label",label=" last replica"},
{x=6,y=1,width=2,class="label",label="formation curve"},
{x=0,y=2,width=2,class="label",label="X Distance:"},
{x=0,y=3,width=2,class="label",label="Y Distance:"},
{x=2,y=2,width=2,class="floatedit",name="xdist",value=xdist or 0},
{x=2,y=3,width=2,class="floatedit",name="ydist",value=ydist or 0},
{x=4,y=2,width=2,class="dropdown",name="xar",items={"absolute","relative"},value="relative",hint="distance type for 'last replica'"},
{x=4,y=3,width=2,class="dropdown",name="yar",items={"absolute","relative"},value="relative",hint="distance type for 'last replica'"},
{x=6,y=2,width=2,class="floatedit",name="xcel",value=xcel or 1,min=0,hint="acceleration for 'last replica'"},
{x=6,y=3,width=2,class="floatedit",name="ycel",value=ycel or 1,min=0,hint="acceleration for 'last replica'"},
{x=0,y=4,width=6,class="checkbox",name="mov",label="use existing \\move for last replica coordinates"},
{x=0,y=5,width=2,class="checkbox",name="delay",label="Delay:"},
{x=2,y=5,width=3,class="intedit",name="del",value=0,min=0},
{x=5,y=5,width=1,class="label",label="frames"},
{x=6,y=5,width=2,class="checkbox",name="keepend",label="keep end",hint="try to keep end time\nsame as original if possible"},
}
lucid={x=0,y=6,width=8,height=10,class="textbox",value=replika}
repeat
if reprez then
for k,v in ipairs(rplGUI) do
if v.name then v.value=reprez[v.name] end
end
end
btns={"Replicate","Elucidate","Disintegrate"}
if pres=="Elucidate" then table.insert(rplGUI,lucid) table.remove(btns,2) end
pres,rez=ADD(rplGUI,btns,{ok='Replicate',close='Disintegrate'})
reprez=rez
until pres~="Elucidate"
if pres=="Disintegrate" then ak() end
repl=rez.rep+1
xcel=rez.xcel if xcel==0 then xcel=1 end
ycel=rez.ycel if ycel==0 then ycel=1 end
if rez.mov then moverep=true else moverep=false end
if rez.dtype=="each replica" and not moverep then dtype=1 xcel=1 ycel=1 else dtype=0 end
if rez.xar=="absolute" then xabs=true else xabs=false end
if rez.yar=="absolute" then yabs=true else yabs=false end
xdist=rez.xdist ydist=rez.ydist
if rez.delay then replay=rez.del else replay=0 end
if rez.keepend then endrep=true else endrep=false end
end
-- FBF Retrack Data Gathering
if res.posi=="fbf retrack" then
if #sel<3 then t_error("Error: You must select at least 3 lines for 'fbf retrack'.",1) end
retrack={} truck="" posref={}
posx1,posy1=subs[sel[1]].text:match("\\pos%(([%d.-]+),([%d.-]+)%)")
if not posx1 then t_error("Error: Missing \\pos in the first line.",1) end
posxl,posyl=subs[sel[#sel]].text:match("\\pos%(([%d.-]+),([%d.-]+)%)")
if not posxl then t_error("Error: Missing \\pos in the last line.",1) end
-- retrack tab
for z,i in ipairs(sel) do
l=subs[i] fr1=ms2fr(l.start_time)
if not truck:match("|"..fr1.."|") then table.insert(retrack,fr1) truck=truck.."|"..fr1.."|" end
end
table.sort(retrack)
-- posref tab
if res.smo then
if res.force==0 then t_error("Smoothening strength is 0, i.e. this won't do anything.\nUse the Force field for strength.",1) end
for z,i in ipairs(sel) do
l=subs[i] frame=ms2fr(l.start_time)
fpos,total=detrack(z,sel,retrack,frame)
posix,posiy=l.text:match("\\pos%(([%d.-]+),([%d.-]+)%)")
if not posix then t_error("Error: Missing \\pos in line #"..i-line0..".",1) end
if not posref[fpos] then posref[fpos]={x=posix,y=posiy} end
end
end
if res.layers and #retrack==1 then t_error("Error: All lines start on the same frame.\nIf you want to change position of signs\non the same frame, uncheck layers.",1) end
if res.layers then total=#retrack else total=#sel end
xstep=round((posxl-posx1)/(total-1),2)
ystep=round((posyl-posy1)/(total-1),2)
if ps<=0 then ps=1 end
if res.eks>0 then acx=res.eks else acx=ps end
if res.wai>0 then acx=res.wai else acy=ps end
end
-- Warp Text initial calculations
local total,M1,M2,fac,l1,l2,p1,p2,x1,x2,y1,y2,xx,yy,tang,ang,MX,MY,mid
if res.posi=="warp text" then
l1=subs[sel[1]]
l2=subs[sel[#sel]]
p1=l1.text:match'\\pos%b()'
p2=l2.text:match'\\pos%b()'
if not p1 or not p2 then t_error("\\pos tags missing.",1) end
x1,y1=p1:match'([%d.-]+),([%d.-]+)'
x2,y2=p2:match'([%d.-]+),([%d.-]+)'
xx=x2-x1 yy=y2-y1
dist=math.sqrt(xx^2+yy^2)
tang=(yy/xx)
ang=math.deg(math.atan(tang))-90
MX=math.cos(math.rad(ang))*ps
MY=math.sin(math.rad(ang))*ps
MD=dist/2
end
-- Sine Loop / Trampoline
local X,Y,Z,fr,loop,acc
if res.posi=="sine loop" or res.posi=="trampoline" then
X=res.eks
Y=res.wai
fr=round(res.force)
if fr<=0 then fr=10 end
acc=ps
if acc<=0 then acc=2 end
loop=fr+1
tot=fr*2
end
-- Positron Cannon Lines --
if res.posi=="space out letters" or res.posi=="track by clip" or res.posi=="sine loop" or res.posi=="trampoline" then table.sort(sel,function(a,b) return a>b end) end
for z,i in ipairs(sel) do
progress("Depositing line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
nontra=detra(text)
nopos=nil
nopos2=nil
poss=text:match("\\pos%b()")
movie=text:match("\\move%b()")
if not poss then nopos=1 end
if not poss and not movie then nopos2=1 end
-- Align X
if res.posi=="Align X" then
if z==1 and nopos2 then text=getpos(subs,text) end
if z==1 and res.first then
px=text:match("\\pos%(([%d.-]+),")
if not px then px=text:match("\\move%(([%d.-]+),") end
relocated=relocated+1
else
if nopos2 then text=getpos(subs,text) end
text=text:gsub("\\pos%(([%d.-]+),([%d.-]+)%)","\\pos("..px..",%2)")
:gsub("\\move%(([%d.-]+),([%d.-]+),([%d.-]+)",function(x1,y1,x2) m1=px-x1 return "\\move("..px..","..y1..","..x1+m1 end)
end
if z>1 and line.text==text then fail("Some lines already in position.") end
end
-- Align Y
if res.posi=="Align Y" then
if z==1 and nopos2 then text=getpos(subs,text) end
if z==1 and res.first then
py=text:match("\\pos%([%d.-]+,([%d.-]+)")
if not py then py=text:match("\\move%([%d.-]+,([%d.-]+),") end
relocated=relocated+1
else
if nopos2 then text=getpos(subs,text) end
text=text:gsub("\\pos%(([%d.-]+),([%d.-]+)%)","\\pos(%1,"..py..")")
:gsub("(\\move%([%d.-]+),([%d.-]+),([%d.-]+),([%d.-]+)",function(x1,y1,x2,y2) m2=py-y1 return x1..","..py..","..x2..","..y2+m2 end)
end
if z>1 and line.text==text then fail("Some lines already in position.") end
end
-- Mirrors
if res.posi:match"mirror" then
if nopos2 then text=getpos(subs,text) end
info(subs)
if not text:match("^{[^}]-\\an%d") then sr=stylechk(subs,line.style)
text=text:gsub("^","{\\an"..sr.align.."}") :gsub("({\\an%d)}{\\","%1\\")
end
if ps and ps~=0 then resx=2*ps resy=2*ps end
if res.posi=="horizontal mirror" then
mirs={"1","4","7","9","6","3"}
text2=text:gsub("\\pos%(([%d.-]+),([%d.-]+)%)",function(x,y) return "\\pos("..resx-x..","..y..")" end)
:gsub("\\move%(([%d.-]+),([%d.-]+),([%d.-]+),([%d.-]+)",function(x,y,x2,y2) return "\\move("..resx-x..","..y..","..resx-x2..","..y2 end)
:gsub("\\an([147369])",function(a) for m=1,6 do if a==mirs[m] then b=mirs[7-m] end end return "\\an"..b end)
if res.rota then
if not text2:match("^{[^}]-\\fry") then text2=addtag("\\fry0",text2) end text2=flip("fry",text2)
end
else
mirs={"1","2","3","9","8","7"}
text2=text:gsub("\\pos%(([%d.-]+),([%d.-]+)%)",function(x,y) return "\\pos("..x..","..resy-y..")" end)
:gsub("\\move%(([%d.-]+),([%d.-]+),([%d.-]+),([%d.-]+)",function(x,y,x2,y2) return "\\move("..x..","..resy-y..","..x2..","..resy-y2 end)
:gsub("\\an([123789])",function(a) for m=1,6 do if a==mirs[m] then b=mirs[7-m] end end return "\\an"..b end)
if res.rota then
if not text2:match("^{[^}]-\\frx") then text2=addtag("\\frx0",text2) end text2=flip("frx",text2)
end
end
l2=line l2.text=text2
if res.keep then
subs.insert(i+1,l2)
for i=z,#sel do sel[i]=sel[i]+1 end
else
if text~=text2 then relocated=relocated+1 else fail("Mirror reflection appears to be in the same location as the object reflected.") end
text=text2
end
end
-- org to fax
if res.posi=="org to fax" then
if text:match("\\move") then t_error("Line #"..i-line0..": What's \\move doing there??",1) end
if nopos then text=getpos(subs,text) end
if not text:match("\\org") then t_error("Missing \\org on line #"..i-line0..".\nAborting.",1) end
pox,poy=text:match("\\pos%(([%d.-]+),([%d.-]+)")
orx,ory=text:match("\\org%(([%d.-]+),([%d.-]+)")
sr=stylechk(subs,line.style)
rota=nontra:match("^{[^}]-\\frz([%d.-]+)") or sr.angle
scx=nontra:match("^{[^}]-\\fscx([%d%.]+)") or sr.scale_x
scy=nontra:match("^{[^}]-\\fscy([%d%.]+)") or sr.scale_y
scr=scx/scy
ad=pox-orx
op=poy-ory
tang=(ad/op)
ang1=math.deg(math.atan(tang))
ang2=ang1-rota
tangf=math.tan(math.rad(ang2))
faks=round(tangf/scr,2)
text=addtag3("\\fax"..faks,text)
text=text:gsub("\\org%([^%)]+%)","")
:gsub(ATAG,function(tg) return duplikill(tg) end)
end
-- clip to fax
if res.posi=="clip to fax" then
if not text:match("\\i?clip%(m") and not text:match("//i?clip%(m") then t_error("Missing \\clip on line #"..i-line0..".\nAborting.",1) end
cx1,cy1,cx2,cy2,cx3,cy3,cx4,cy4=text:match("clip%(m ([%d.-]+) ([%d.-]+) l ([%d.-]+) ([%d.-]+) ([%d.-]+) ([%d.-]+) ([%d.-]+) ([%d.-]+)")
if not cx1 then cx1,cy1,cx2,cy2=text:match("clip%(m ([%d.-]+) ([%d.-]+) l ([%d.-]+) ([%d.-]+)") end
if not cx1 then t_error("Line #"..i-line0..": Not enough clip points. 2 required.",1) end
sr=stylechk(subs,line.style)
rota=nontra:match("^{[^}]-\\frz([%d.-]+)") or sr.angle
rota2=nontra:match(".*\\frz([%d.-]+)") or sr.angle
scx=nontra:match("^{[^}]-\\fscx([%d%.]+)") or sr.scale_x
scy=nontra:match("^{[^}]-\\fscy([%d%.]+)") or sr.scale_y
scr=scx/scy
ad=cx1-cx2
op=cy1-cy2
tang=(ad/op)
ang1=math.deg(math.atan(tang))
ang2=ang1-rota
tangf=math.tan(math.rad(ang2))
faks=round(tangf/scr,2)
text=addtag3("\\fax"..faks,text)
if cy4 then
tang2=((cx3-cx4)/(cy3-cy4))
ang3=math.deg(math.atan(tang2))
ang4=ang3-rota2
tangf2=math.tan(math.rad(ang4))
faks2=round(tangf2,2)
endcom=""
repeat text=text:gsub("({[^}]-})%s*$",function(ec) endcom=ec..endcom return "" end)
until not text:match("}$")
text=text:gsub("(.)$","{\\fax"..faks2.."}%1")
vis=nobra(text)
orig=text:gsub(STAG,"")
tg=text:match(STAG)
chars={}
for ltr in re.gfind(vis,'.') do table.insert(chars,ltr) end
faxdiff=(faks2-faks)/(#chars-1)
tt=chars[1]
for c=2,#chars do
if chars[c]==" " then tt=tt.." " else tt=tt.."{\\fax"..round((faks+faxdiff*(c-1)),2) .."}"..chars[c] end
end
text=tg..tt
if orig:match("{%*?\\") then text=retextmod(orig,text) end
text=text..endcom
end
text=text:gsub("\\i?clip%b()","")
:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}")
:gsub(ATAG,function(tg) return duplikill(tg) end)
:gsub("%**}","}")
end
-- clip to frz
if res.posi=="clip to frz" then
if not text:match("\\i?clip%(m") and not text:match("//i?clip%(m") then t_error("Missing \\clip on line #"..i-line0..".\nAborting.",1) end
cx1,cy1,cx2,cy2,cx3,cy3,cx4,cy4=text:match("clip%(m ([%d.-]+) ([%d.-]+) l ([%d.-]+) ([%d.-]+) ([%d.-]+) ([%d.-]+) ([%d.-]+) ([%d.-]+)")
if not cx1 then cx1,cy1,cx2,cy2=text:match("clip%(m ([%d.-]+) ([%d.-]+) l ([%d.-]+) ([%d.-]+)") end
if not cx1 then t_error("Line #"..i-line0..": Not enough clip points. 2 required.",1) end
local ad,op,tang,ang1,rota,ad2,op2,tang2,ang2,rota2
ad=cx2-cx1
op=cy1-cy2
tang=(op/ad)
ang1=math.deg(math.atan(tang))
rota=round(ang1,2)
if ad<0 then rota=rota-180 end
if cy4 then
ad2=cx4-cx3
op2=cy3-cy4
tang2=(op2/ad2)
ang2=math.deg(math.atan(tang2))
rota2=round(ang2,2)
if ad2<0 then rota2=rota2-180 end
else rota2=rota
end
rota3=(rota+rota2)/2
text=addtag("\\frz"..rota3,text)
text=text:gsub("\\i?clip%b()",""):gsub(ATAG,function(tg) return duplikill(tg) end)
end
-- clip to reposition
if res.posi=="clip to reposition" then
if not text:match("\\i?clip%(m") and not text:match("//i?clip%(m") then t_error("Missing \\clip on line #"..i-line0..".\nAborting.",1) end
cx1,cy1,cx2,cy2=text:match("clip%(m ([%d.-]+) ([%d.-]+) l ([%d.-]+) ([%d.-]+)")
if not cx1 then t_error("Line #"..i-line0..": Not enough clip points. 2 required.",1) end
repo1=cx2-cx1 repo2=cy2-cy1
text=text:gsub("\\i?clip%b()","")
:gsub("\\pos%(([%d.-]+),([%d.-]+)",function(x,y) return "\\pos("..round(x+repo1,2)..","..round(y+repo2,2) end)
:gsub("\\move%(([%d.-]+),([%d.-]+),([%d.-]+),([%d.-]+)",function(x1,y1,x2,y2)
return "\\move("..round(x1+repo1,2)..","..round(y1+repo2,2)..","..round(x2+repo1,2)..","..round(y2+repo2,2) end)
end
-- clip2pos fbf
if res.posi=="clip2pos fbf" then
local x1,y1=text:match("clip%(m ([%d.-]+) ([%d.-]+)")
if nopos then text=getpos(subs,text) end
local newx,newy
if z==1 then
if not x1 then t_error("Vector clip not detected on line #"..i-line0..".",1) end
posx=text:match"\\pos%((%d+%.?%d*)"
posy=text:match"\\pos%([^,]-,(%d+%.?%d*)"
refx=x1
refy=y1
else
newx=x1-refx+posx
newy=y1-refy+posy
text=text:gsub("\\pos%(([^,]-),([^,]-)%)","\\pos("..newx..","..newy..")")
end
text=text:gsub("\\i?clip%b()","")
end
-- frz+org2pos
if res.posi=="frz+org2pos" then
if nopos then text=getpos(subs,text) end
local px,py,ox,oy,rota,X,Y,x,y,pox,poy,tang,ang,ang1
px,py=text:match("\\pos%((.-),(.-)%)")
ox,oy=text:match("\\org%((.-),(.-)%)")
rota=nontra:match("^{[^}]-\\frz([-%d.]+)")
if px and ox and rota then
h=math.sqrt((ox-px)^2+(oy-py)^2)
pox=ox-px
poy=oy-py
tang=poy/pox
ang1=math.deg(math.atan(tang))
ang=ang1-rota
X=math.cos(math.rad(ang))*h
Y=math.sin(math.rad(ang))*h
if pox<0 then X=0-X Y=0-Y end
x=round(ox-X,1)
y=round(oy-Y,1)
text=text:gsub("\\pos%b()","\\pos("..x..","..y..")"):gsub("\\org%b()","")
end
end
-- numbers
if res.posi=="numbers" and line.layer==LL then
ind=ind+1
PX=text:match("\\pos%(([^,]-),") or 0
PY=text:match("\\pos%([^,]-,([^,]-)%)") or 0
table.insert(PNS,{x=tonumber(PX),y=tonumber(PY)})
end
-- shake
if res.posi=="shake" then
if text:match("\\move") then t_error("Line #"..i-line0..": What's \\move doing there??",1) end
if nopos then text=getpos(subs,text) end
s=line.start_time
diam=ps
scal=res.force
if diam==0 and not res.sca then diamx=res.eks diamy=res.wai else diamx=diam diamy=diam end
shx=math.random(-100,100)/100*diamx if res.smo and lshx then shx=(shx+3*lshx)/4 end
shy=math.random(-100,100)/100*diamy if res.smo and lshy then shy=(shy+3*lshy)/4 end
shr=math.random(-100,100)/100*diam if res.smo and lshr then shr=(shr+3*lshr)/4 end
shsx=math.random(-100,100)/100*scal if res.smo and lshsx then shsx=(shsx+3*lshsx)/4 end
shsy=math.random(-100,100)/100*scal if res.smo and lshsy then shsy=(shsy+3*lshsy)/4 end
if res.layers then
ch=0
for p=1,#shake do sv=shake[p]
if sv[1]==s then ch=1 shx=sv[2] shy=sv[3] shr=sv[4] shsx=sv[5] shsy=sv[6] end
end
if ch==0 then
a={s,shx,shy,shr,shsx,shsy}
table.insert(shake,a)
end
end
lshx=shx lshy=shy lshr=shr lshsx=shsx lshsy=shsy
text=text:gsub("\\pos%(([%d.-]+),([%d.-]+)%)",function(x,y) return "\\pos("..x+shx..","..y+shy..")" end)
if res.rota then
text=text:gsub("\\frz([%d.-]+)",function(z) return "\\frz"..z+shr end)
if not text:match("^{[^}]-\\frz") then text=addtag("\\frz"..shr,text) end
end
if res.sca then
text=text:gsub("(\\fscx)([%d.-]+)",function(x,y) return x..y+shsx end)
text=text:gsub("(\\fscy)([%d.-]+)",function(x,y) return x..y+shsy end)
if not nontra:match("^{[^}]-\\fscx") then text=addtag3("\\fscx"..shsx+100,text) end
if not nontra:match("^{[^}]-\\fscy") then text=addtag3("\\fscy"..shsy+100,text) end
end
text=text:gsub("([%d.-]+%d)([\\}%),])",function(a,b) return round(a,2)..b end)
end
if res.posi=="sine loop" or res.posi=="trampoline" then
local frames,poses,px,py,mx,my,NX,NY
if nopos then text=getpos(subs,text) end
px,py=text:match("\\pos%((.-),(.-)%)")
px2=px+X
py2=py+Y
mx=px+(X/2)
my=py+(Y/2)
start=line.start_time
endt=line.end_time
sfr=ms2fr(start)
efr=ms2fr(endt)
frames=efr-sfr
poses={}
for f=2,frames do
Z=f%tot
if Z==0 then Z=2 end
if Z>loop then Z=2*loop-Z end
table.insert(poses,Z)
if res.posi=="trampoline" then
AF=(Z-1)^acc/(fr)^acc
NX=round(AF*(px2-px)+px,2)
NY=round(AF*(py2-py)+py,2)
end
if res.posi=="sine loop" then
mid=(loop+1)/2
if Z==mid then NX=mx NY=my
elseif Z<mid then
AF=(Z-1)^acc/(mid-1)^acc
NX=round(AF*(mx-px)+px,2)
NY=round(AF*(my-py)+py,2)
else
Z2=loop+1-Z
AF=(Z2-1)^acc/(mid-1)^acc
NX=round(px2-AF*(px2-mx),2)
NY=round(py2-AF*(py2-my),2)
end
end
-- logg(NX..','..NY)
t2=text:gsub("\\pos%(.-,.-%)","\\pos("..NX..","..NY..")")
line.text=t2
line.start_time=fr2ms(sfr+f-1)
line.end_time=fr2ms(sfr+f)
subs.insert(i+f-1,line)
nsel=shiftsel(nsel,i,1)
end
line.start_time=fr2ms(sfr)
line.end_time=fr2ms(sfr+1)
end
-- shadow layer
if res.posi=="shadow layer" then
sr=stylechk(subs,line.style)
text=text:gsub("\\1c","\\c")
shadcol=nontra:match("^{[^}]-\\4c(&H%x+&)") or sr.color4:gsub("H%x%x","H")
if ps~=0 then xsh=ps ysh=ps else xsh=res.eks ysh=res.wai end
if xsh==0 and ysh==0 then
xs=nontra:match("^{[^}]-\\xshad([%d.-]+)") or nontra:match("^{[^}]-\\shad([%d%.]+)") or sr.shadow
ys=nontra:match("^{[^}]-\\yshad([%d.-]+)") or nontra:match("^{[^}]-\\shad([%d%.]+)") or sr.shadow
else xs=xsh ys=ysh
end
if tonumber(xs)>0 or tonumber(ys)>0 then
text=text:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end)
if nopos2 then text=getpos(subs,text) end
text=text:gsub("\\[xy]?shad([%d.-]+)","")
bored=nontra:match("^{[^}]-\\bord([%d%.]+)") or sr.outline
bored=bored-1
if bored<0 then bored=0 end
l2=line text2=text
text2=text2
:gsub("\\pos%(([%d.-]+),([%d.-]+)%)",function(a,b) return "\\pos("..a+xs..","..b+ys..")" end)
:gsub("\\move%(([%d.-]+),([%d.-]+),([%d.-]+),([%d.-]+)",
function(a,b,c,d) return "\\pos("..a+xs..","..b+ys..","..c+xs..","..d+ys end)
:gsub(ATAG,function(tag)
if tag:match("\\4c&H%x+&") then
tag=tag:gsub("\\3?c&H%x+&","") :gsub("\\4c(&H%x+&)","\\c%1\\3c%1")
else tag=tag:gsub("\\3?c&H%x+&","")
end
if tag:match("\\4a&H%x+&") then
tag=tag:gsub("\\alpha&H%x+&","") :gsub("\\4a(&H%x+&)","\\alpha%1")
end
tag=tag:gsub("\\[13]a&H%x+&","") :gsub("{}","")
return tag
end)
text2=addtag3("\\bord"..bored,text2)
text2=addtag3("\\shad0",text2)
text2=addtag3("\\c"..shadcol,text2)
text2=addtag3("\\3c"..shadcol,text2)
text=addtag3("\\shad0",text)
text=text:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
l2.text=text2
subs.insert(i+1,l2)
line.layer=line.layer+1
sel=shiftsel(sel,i,0) nsel=shiftsel(nsel,i,1)
else fail("It appears to cast no shadow...")
end
if z==#sel then sel=nsel end
end
-- Shadow Reposition
if res.posi=="shadow repos." then
sr=stylechk(subs,line.style)
if nopos2 then text=getpos(subs,text) end
xshad=nontra:match("^{[^}]-\\xshad([^}\\]+)") or nontra:match("^{[^}]-\\shad([^}\\]+)") or sr.shadow
yshad=nontra:match("^{[^}]-\\yshad([^}\\]+)") or nontra:match("^{[^}]-\\shad([^}\\]+)") or sr.shadow
text=text:gsub("\\pos%((.-),(.-)%)",function(x,y) return "\\pos("..x-xshad/2 ..","..y-yshad/2 ..")" end)
:gsub("\\move%((.-),(.-),(.-),([%d.-]+)",function(x,y,xx,yy) return "\\move("..x-xshad/2 ..","..y-yshad/2 ..","..xx-xshad/2 ..","..yy-yshad/2 end)
if text==line.text then fail("No shadow.") end
end
-- fbf X <--> Y
if res.posi=="fbf X <--> Y" then
newY=XYtab[z].x-Xref+Yref if res.rota then newY=Yref-(XYtab[z].x-Xref) end
newX=XYtab[z].y-Yref+Xref if res.rota then newX=Xref-(XYtab[z].y-Yref) end
text=text:gsub("\\pos%(([%d.-]+),([%d.-]+)%)","\\pos("..newX..","..newY..")")
if text==line.text then fail("Some lines already in position.") end
end
-- Space out letters
visible=text:gsub("%b{}",""):gsub("%s*\\[Nh]%s*"," ")
letrz=re.find(visible,".")
if res.posi=="space out letters" and not letrz then fail("No text.") end
if res.posi=="space out letters" and letrz and #letrz<2 then fail("Not enough letters to split text.") end
if res.posi=="space out letters" and letrz and #letrz>1 then
sr=stylechk(subs,line.style)
acalign=nil
m1,m2,m3,m4=text:match("\\move%(([%d.-]+),([%d.-]+),([%d.-]+),([%d.-]+)")
if m1 then
text=text:gsub("\\move%(([%d.-]+),([%d.-]+)","\\pos(%1,%2)(")
movX=m3-m1 movY=m4-m2
end
text=text:gsub(" *\\[Nh] *"," ")
if nopos then text=getpos(subs,text) end
tags=text:match(STAG) or ""
after=text:gsub(STAG,""):gsub("{[^\\}]-}","")
local px,py=text:match("\\pos%(([%d.-]+),([%d.-]+)%)")
local x1,width,w,wtotal,let,spacing,avgspac,ltrspac,xpos,lastxpos,spaces,prevlet,scx,k1,k2,k3,bord,off,inwidth,wdiff,pp,tpos
scx=nontra:match("^{[^}]-\\fscx([%d%.]+)") or sr.scale_x
fsp=nontra:match("^{[^}]-\\fsp([%d%.]+)")
if fsp then sr.spacing=tonumber(fsp) end
fsize=nontra:match("^{[^}]-\\fs([%d%.]+)")
if fsize then sr.fontsize=tonumber(fsize) end
phont=nontra:match("^{[^}]-\\fn([^\\}]+)")
if phont then sr.fontname=phont end
bord=nontra:match("^{[^}]-\\bord([%d%.]+)") or sr.outline
k1,k2,k3=text:match("clip%(([%d.-]+),([%d.-]+),([%d.-]+),")
letters={} wtotal=0
for l=1,#letrz do
local ltr=letrz[l].str
w=aegisub.text_extents(sr,ltr)
table.insert(letters,{l=ltr,w=w})
wtotal=wtotal+w
leng=re.find(ltr,'.')
if ltr=="" then
logg("- Line #"..i-line0..": unexpected re module failure: letter lost - #"..l)
fail("Re module failure. Some letters seem to have been lost or added.\nPlease rerun the function or rescan Autoload directory.")
re_fail=true
elseif #leng>1 then
logg("- Line #"..i-line0..": unexpected re module failure: multiple letters matched: "..ltr)
fail("Re module failure. Some letters seem to have been lost or added.\nPlease rerun the function or rescan Autoload directory.")
re_fail=true
end
end
if #letters~=#letrz then
logg(#letrz.." -> "..#letrz)
end
intags={} cnt=0
for chars,tag in after:gmatch("([^}]+)({\\[^}]+})") do
pp=re.find(chars,".")
tpos=#pp+1+cnt
intags[tpos]=tag
cnt=cnt+#pp
end
spacing=ps
avgspac=wtotal/#letters
off=(letters[1].w-letters[#letters].w)/4*scx/100
inwidth=(wtotal-letters[1].w/2-letters[#letters].w/2)*scx/100
if spacing==1 then spacing=round(avgspac*scx)/100 end
width=(#letters-1)*spacing --off
-- klip-based stuff
if k1 then
width=(k3-k1)-letters[1].w/2*(scx/100)-letters[#letters].w/2*(scx/100)-(2*bord)
spacing=(width+2*bord)/(#letters-1)
px=(k1+k3)/2-off
tags=tags:gsub("\\i?clip%b()","")
end
-- find starting x point based on alignment
if not acalign then acalign=text:match("\\an(%d)") or sr.align end
acalign=tostring(acalign)
if acalign:match("[147]") then
tags=tags:gsub("\\an%d","") :gsub("^{","{\\an"..acalign+1)
:gsub("\\pos%(([%d.-]+)",function(p) return "\\pos("..round(p+(wtotal/2)*(scx/100),2) end)
end
if acalign:match("[369]") then
tags=tags:gsub("\\an%d","") :gsub("^{","{\\an"..acalign-1)
:gsub("\\pos%(([%d.-]+)",function(p) return "\\pos("..round(p-(wtotal/2)*(scx/100),2) end)
end
if not k1 then px,py=tags:match("\\pos%(([%d.-]+),([%d.-]+)%)") end
acalign=tags:match("\\an(%d)")
x1=round(px-width/2)
wdiff=(width-inwidth)/(#letters-1)
lastxpos=x1
spaces=0
-- weird letter-width sorcery starts here
for t=1,#letters do
let=letters[t]
if t>1 then
prevlet=letters[t-1]
ltrspac=(let.w+prevlet.w)/2*scx/100+wdiff
ltrspac=round(ltrspac,2)
else
fact1=spacing/(avgspac*scx/100)
fact2=(let.w-letters[#letters].w)/4*scx/100
ltrspac=round(fact1*fact2,2)
end
if intags[t] then tags=tags..intags[t] tags=tagmerge(tags) tags=duplikill(tags) end
t2=tags..let.l
xpos=lastxpos+ltrspac
XP=xpos
notra=detra(t2)
rota=notra:match("^{[^}]-\\frz([-%d.]+)")
if rota then
h=px-xpos
X=math.cos(math.rad(rota))*h
Y=math.sin(math.rad(rota))*h
x=round(px-X,1)
y=round(py+Y,1)
t2=t2:gsub("\\pos%b()","\\pos("..x..","..y..")")
else
t2=t2:gsub("\\pos%(([%d.-]+),([%d.-]+)%)","\\pos("..XP..",%2)")
end
if m1 then
t2=t2:gsub("\\pos%(([%d.-]+),([%d.-]+)%)%(,[%d.-]+,[%d.-]+",function(a,b) return "\\move("..a..","..b..","..a+movX..","..b+movY end)
end
lastxpos=xpos
l2=line
l2.text=t2
if t==1 then text=t2 else
if let.l~=" " then subs.insert(i+t-1-spaces,l2) nsel=shiftsel(nsel,i,1) else count=count-1 spaces=spaces+1 end
end
end
count=count+#letters-1
end
-- Warp Text
if res.posi=="warp text" and z>1 and z<#sel then
if not text:match'\\pos%b()' then t_error("Missing \\pos tag on line #"..i-line0..".",1) end
local xc,yc,CDX,CDY,CD,acc,Mid,MC,DF,D2,NX,NY,AF
xc,yc=text:match'\\pos%(([%d.-]+),([%d.-]+)%)'
CDX=xc-x1 CDY=yc-y1
CD=math.sqrt(CDX^2+CDY^2)
if CD>MD then
CDX=x2-xc CDY=y2-yc
CD=math.sqrt(CDX^2+CDY^2)
end
acc=0.8 -- Curve Shaper 1
if CD<MD then
AF=(CD)^acc/(MD)^acc
else
AF=(dist-CD)^acc/MD^acc
end
Mid=MD/10
DF=Mid/CD -- Curve Shaper 2
if CD>Mid then DF=9*Mid/(MD-CD) end
DF=1.6/DF
D2=math.sqrt(1+DF)
NX=round(MX*AF*D2+xc,5)
NY=round(MY*AF*D2+yc,5)
DF=DF^2.5 -- Curve Shaper 2.5
FF=(6*(ps/dist))^2 -- Random Weird Factor 1
if tonumber(xc)<(x2+x1)/2 then
CDX2=NX-x1
CDY2=NY-y1
if CDX2>0 then XF=DF else XF=0-DF end
if CDY2>0 then YF=DF else YF=0-DF end
XF2=2*XF*math.abs(CDX2)/(math.abs(CDX2)+math.abs(CDY2))*FF
YF2=2*YF*math.abs(CDY2)/(math.abs(CDX2)+math.abs(CDY2))*FF
else
CDX2=x2-NX
CDY2=y2-NY
if CDX2<0 then XF=DF else XF=0-DF end
if CDY2<0 then YF=DF else YF=0-DF end
XF2=2*XF*math.abs(CDX2)/(math.abs(CDX2)+math.abs(CDY2))*FF
YF2=2*YF*math.abs(CDY2)/(math.abs(CDX2)+math.abs(CDY2))*FF
end
text=text:gsub("\\pos%(([%d.-]+),([%d.-]+)%)","\\pos("..round(NX-XF2,2)..","..round(NY-YF2,2)..")")
end
-- Track by Clip
if res.posi=="track by clip" then
if not res.X and not res.Y then t_error("Neither X nor Y is checked. Nothing to track.",1) end
klip=text:match("clip%(m .+%)")
if not klip then t_error("Error: No clip in line #"..i-line0..".",1) end
if nopos then text=getpos(text,subs) end
local fr=1 -- how many frames per line
if ps>=1 then fr=math.floor(ps) end
local x1,y1=text:match("clip%(m ([-%d.]+) ([-%d.]+)")
if not x1 then t_error("Error: This clip seems broken.\n"..klip,1) end
if not res.first then x1,y1=text:match("clip%(m .-([-%d.]+) ([-%d.]+)%)") end -- last frame is default
local klips={}
for a,b in klip:gmatch("([-%d.]+) ([-%d.]+)") do table.insert(klips,{x=a-x1,y=b-y1}) end
start,endt=line.start_time,line.end_time
for t=#klips,2,-1 do
kp=klips[t]
if res.X then x6=kp.x else x6=0 end
if res.Y then y7=kp.y else y7=0 end
t2=text:gsub("\\i?clip%b()","")
:gsub("\\pos%(([^,]+),([^,]+)%)",function(a,b) return "\\pos("..a+x6..","..b+y7..")" end)
local st=fr2ms(ms2fr(start)+(fr*(t-1)))
local et=fr2ms(ms2fr(start)+(fr*(t-1))+fr)
line.start_time=st
line.end_time=et
if t==#klips then line.end_time=fr2ms(ms2fr(endt)) end
line.text=t2
subs.insert(i+1,line) nsel=shiftsel(nsel,i,1)
end
line.start_time=fr2ms(ms2fr(start))
line.end_time=fr2ms(ms2fr(start)+fr)
text=text:gsub("\\i?clip%b()","")
if not res.first then text=text:gsub("\\pos%(([^,]+),([^,]+)%)",function(a,b) return "\\pos("..a+klips[1].x..","..b+klips[1].y..")" end) end
end
-- Replicate
if res.posi=="replicate" then
startf=ms2fr(line.start_time)
endf=ms2fr(line.end_time)
if moverep then
x1,y1,mx,my=text:match("\\move%(([^,]+),([^,]+),([^,]+),([^,)]+)")
if not mx then t_error("Abort: No \\move tag on line #"..i-line0..".",1) end
rxdist=mx rydist=my
text=text:gsub("\\move%b()","\\pos("..x1..","..y1..")")
else
if nopos and not text:match("\\move") then text=getpos(subs,text) end
x1,y1=text:match("\\pos%(([^,]+),([^,]-)%)")
if not x1 then x1,y1=text:match("\\move%(([^,]+),([^,]-),") end
if dtype==1 then rxdist=xdist*(repl-1)+x1 rydist=ydist*(repl-1)+y1
else
if xabs then rxdist=xdist else rxdist=xdist+x1 end
if yabs then rydist=ydist else rydist=ydist+y1 end
end
end
-- replicating
for r=repl,1,-1 do
l2=line
posx=numgrad(x1,rxdist,repl,r,xcel)
posy=numgrad(y1,rydist,repl,r,ycel)
text2=text:gsub("\\pos%b()","\\pos("..posx..","..posy..")")
:gsub("\\move%(([^,]+),([^,]+),([^,]+),([^,)]+)",function(m1,m2,m3,m4)
return "\\move("..posx..","..posy..","..m3-m1+posx..","..m4-m2+posy end)
startf2=startf+replay*(r-1)
start2=fr2ms(startf2) end2=fr2ms(endf)
if endrep then if endf<startf2 then end2=fr2ms(startf2+1) end -- keep end or start+1
else end2=fr2ms(endf+replay*(r-1)) end
l2.start_time=start2 l2.end_time=end2
l2.text=text2
if r==1 then line=l2 else subs.insert(i+1,l2) sel=shiftsel(sel,i,0) nsel=shiftsel(nsel,i,1) end
end
line.start_time=fr2ms(startf)
line.end_time=fr2ms(endf)
if z==#sel then sel=nsel end
end
-- FBF Retrack
if res.posi=="fbf retrack" then
frame=ms2fr(line.start_time)
fpos,total=detrack(z,sel,retrack,frame)
if nopos then text=getpos(subs,text) end
posix,posiy=text:match("\\pos%(([^,]+),([^,]+)%)")
if fpos>1 and fpos<total then
if res.smo then
-- smoothen track, force: 0-100
smf=math.abs(res.force)/100
if smf>1 then smf=1 end
if smf<0 then smf=0 end
ref=round(math.abs(ps))
if ref<1 then ref=1 end
for re=1,ref do
sm=smf*(1/re)
if re<fpos and re<total-fpos+1 then
xref=(posref[fpos-re].x+posref[fpos+re].x)/2
yref=(posref[fpos-re].y+posref[fpos+re].y)/2
diffx=round(posix-xref,2)
diffy=round(posiy-yref,2)
newdiffx=diffx*sm
newdiffy=diffy*sm
newx=round(posix-newdiffx,2)
newy=round(posiy-newdiffy,2)
posix=newx posiy=newy
posref[fpos]={x=newx,y=newy}
end
end
else
-- regular fbf transform
newx=numgrad(posx1,posxl,total,fpos,acx)
newy=numgrad(posy1,posyl,total,fpos,acy)
end
if not res.X then newx=posix end
if not res.Y then newy=posiy end
text=text:gsub("\\pos%b()","\\pos("..newx..","..newy..")")
end
fail("First and last lines won't move.")
if text==line.text and z>1 and z<#sel then fail("Some lines already in position.") end
end
if line.text~=text then relocated=relocated+1 end
line.text=text
subs[i]=line
end
if res.posi=="numbers" then
NUMGUI=
{{x=0,y=0,width=3,class="label",label=ind.." lines of layer "..LL.." selected (showing max. 10); pos X / pos Y"}}
for n=1,#PNS do
if n==1 then
table.insert(NUMGUI,{x=0,y=n,class="label",label="Start point:"})
table.insert(NUMGUI,{x=1,y=n,class="floatedit",value=PNS[n].x})
table.insert(NUMGUI,{x=2,y=n,class="floatedit",value=PNS[n].y})
end
if n>1 and n<11 then
table.insert(NUMGUI,{x=0,y=n,class="label",label="Line "..n.." offset:"})