forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelocator.lua
1697 lines (1543 loc) · 75.4 KB
/
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="Makes things appear different from before"
script_author="reanimated"
script_url1="http://unanimated.xtreemhost.com/ts/relocator.lua"
script_url2="https://raw.githubusercontent.com/unanimated/luaegisub/master/relocator.lua"
script_version="2.9"
include("utils.lua")
re=require'aegisub.re'
function warnopos()
aegisub.dialog.display({{class="label",label="Missing \\pos tag.",height=2}},{"OK"},{close='OK'}) aegisub.cancel()
end
function positron(subs,sel)
ps=res.post
shake={} shaker={}
if res.posi=="shadow layer" then table.sort(sel,function(a,b) return a>b end) end
for x, i in ipairs(sel) do
aegisub.progress.title(string.format("Depositing line %d/%d",x,#sel))
line=subs[i]
text=line.text
-- Align X
if res.posi=="Align X" then
if x==1 and not text:match("\\pos") then warnopos() end
if x==1 and res.first then pxx=text:match("\\pos%(([%d%.%-]+),") ps=pxx end
text=text:gsub("\\pos%(([%d%.%-]+),([%d%.%-]+)%)","\\pos("..ps..",%2)")
end
-- Align Y
if res.posi=="Align Y" then
if x==1 and not text:match("\\pos") then warnopos() end
if x==1 and res.first then pyy=text:match("\\pos%([%d%.%-]+,([%d%.%-]+)") ps=pyy end
text=text:gsub("\\pos%(([%d%.%-]+),([%d%.%-]+)%)","\\pos(%1,"..ps..")")
end
-- Mirrors
if res.posi:match"mirror" then
if not text:match("\\pos") and not text:match("\\move") then
aegisub.dialog.display({{class="label",label="Fail. Some lines are missing \\pos.",width=1,height=2}},{"OK"},{close='OK'})
aegisub.cancel()
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 res.post~=0 and res.post~=nil then resx=2*res.post resy=2*res.post 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.delfbf then
text=text2
else
subs.insert(i+1,l2)
for i=x,#sel do sel[i]=sel[i]+1 end
end
end
-- org to fax
if res.posi=="org to fax" then
if not text:match("\\pos") then warnopos() end
if not text:match("\\org") then
aegisub.dialog.display({{class="label",label="Missing \\org.",width=1,height=2}},{"OK"},{close='OK'})
aegisub.cancel()
end
pox,poy=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)")
orx,ory=text:match("\\org%(([%d%.%-]+),([%d%.%-]+)")
rota=text:match("\\frz([%d%.%-]+)")
if rota==nil then rota=0 end
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*100)/100
text=addtag("\\fax"..faks,text)
text=text:gsub("\\org%([^%)]+%)","")
text=text:gsub("({%*?\\[^}]-})",function(tg) return duplikill(tg) end)
end
-- clip to fax
if res.posi=="clip to fax" then
if not text:match("\\clip") then
aegisub.dialog.display({{class="label",label="Missing \\clip.",width=1,height=2}},{"OK"},{close='OK'})
aegisub.cancel()
end
cx1,cy1,cx2,cy2,cx3,cy3,cx4,cy4=text:match("\\clip%(m ([%d%-]+) ([%d%-]+) l ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+)")
if cx1==nil then cx1,cy1,cx2,cy2=text:match("\\clip%(m ([%d%-]+) ([%d%-]+) l ([%d%-]+) ([%d%-]+)") end
rota=text:match("\\frz([%d%.%-]+)")
if rota==nil then rota=0 end
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*100)/100
text=addtag("\\fax"..faks,text)
if cy4~=nil then
tang2=((cx3-cx4)/(cy3-cy4))
ang3=math.deg(math.atan(tang2))
ang4=ang3-rota
tangf2=math.tan(math.rad(ang4))
faks2=round(tangf2*100)/100
endcom=""
repeat
text=text:gsub("({[^}]-})%s*$",function(ec) endcom=ec..endcom return "" end)
until not text:match("}$")
text=text:gsub("(.)$","{\\fax"..faks2.."}%1")
vis=text:gsub("{[^}]-}","")
orig=text:gsub("^{\\[^}]*}","")
tg=text:match("^{\\[^}]-}")
chars={}
for char in vis:gmatch(".") do table.insert(chars,char) end
faxdiff=(faks2-faks)/(#chars-1)
tt=chars[1]
for c=2,#chars do
if c==#chars then ast="" else ast="*" end
if chars[c]==" " then tt=tt.." " else
tt=tt.."{"..ast.."\\fax"..round((faks+faxdiff*(c-1))*100)/100 .."}"..chars[c]
end
end
text=tg..tt
if orig:match("{%*?\\") then text=textmod(orig,text) end
text=text..endcom
end
text=text:gsub("\\clip%([^%)]+%)","")
:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}")
:gsub("({%*?\\[^}]-})",function(tg) return duplikill(tg) end)
end
-- clip to frz
if res.posi=="clip to frz" then
if not text:match("\\clip") then
aegisub.dialog.display({{class="label",label="Missing \\clip.",width=1,height=2}},{"OK"},{close='OK'})
aegisub.cancel()
end
cx1,cy1,cx2,cy2,cx3,cy3,cx4,cy4=text:match("\\clip%(m ([%d%-]+) ([%d%-]+) l ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+)")
if cx1==nil then cx1,cy1,cx2,cy2=text:match("\\clip%(m ([%d%-]+) ([%d%-]+) l ([%d%-]+) ([%d%-]+)") end
ad=cx2-cx1
op=cy1-cy2
tang=(op/ad)
ang1=math.deg(math.atan(tang))
rota=round(ang1*100)/100
if ad<0 then rota=rota-180 end
if cy4~=nil then
ad2=cx4-cx3
op2=cy3-cy4
tang2=(op2/ad2)
ang2=math.deg(math.atan(tang2))
rota2=round(ang2*100)/100
if ad2<0 then rota2=rota2-180 end
end
rota3=(rota+rota2)/2
text=addtag("\\frz"..rota3,text)
text=text:gsub("\\clip%([^%)]+%)","")
text=text:gsub("({%*?\\[^}]-})",function(tg) return duplikill(tg) end)
end
-- shake
if res.posi=="shake" then
if not text:match("\\pos") then warnopos() end
s=line.start_time
diam=res.post
scax=res.eks*10+100 scax2=10000/scax
scay=res.wai*10+100 scay2=10000/scay
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~=nil then shx=(shx+3*lshx)/4 end
shy=math.random(-100,100)/100*diamy if res.smo and lshy~=nil then shy=(shy+3*lshy)/4 end
shr=math.random(-100,100)/100*diam if res.smo and lshr~=nil then shr=(shr+3*lshr)/4 end
shsx=math.random(scax2,scax)/100 if res.smo and lshsx~=nil then shsx=(shsx+3*lshsx)/4 end
shsy=math.random(scay2,scay)/100 if res.smo and lshsy~=nil then shsy=(shsy+3*lshsy)/4 end
if scax==scay then shsy=shsx 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 text:match("^{[^}]-\\fscx") then text=addtag("\\fscx".. 100*shsx,text) end
if not text:match("^{[^}]-\\fscy") then text=addtag("\\fscy".. 100*shsy,text) end
end
text=text:gsub("([%d%.%-]+)([\\}%),])",function(a,b) return round(a*100)/100 ..b end)
end
-- shake rotation
if res.posi=="shake rotation" then
s=line.start_time
ang=res.post
angx=res.eks
angy=res.wai
angl={ang,angx,angy}
rots={"\\frz","\\frx","\\fry"}
for r=1,3 do ro=rots[r] an=angl[r]
if an~=0 then
shr=math.random(-100,100)/100*an
if res.layers then
ch=0
for p=1,#shaker do sv=shaker[p]
if sv[1]==s and sv[2]==r then ch=1 shr=sv[3] end
end
if ch==0 then rt=r
a={s,rt,shr}
table.insert(shaker,a)
end
end
text=text:gsub(ro.."([%d%.%-]+)",function(z) return ro..z+shr end)
if not text:match("^{[^}]-"..ro) then text=addtag(ro..shr,text) end
end end
end
-- shadow layer
if res.posi=="shadow layer" then
sr=stylechk(subs,line.style)
text=text:gsub("\\1c","\\c")
shadcol=sr.color4:gsub("H%x%x","H")
sc=text:match("^{[^}]-\\4c(&H%x+&)")
if sc~=nil then shadcol=sc end
if res.post~=0 then xsh=res.post ysh=res.post else xsh=res.eks ysh=res.wai end
if xsh==0 and ysh==0 then
stshad=sr.shadow
shad=text:match("^{[^}]-\\shad([%d%.]+)")
if shad~=nil then xs=shad ys=shad end
xshad=text:match("^{[^}]-\\xshad([%d%.%-]+)")
if xshad~=nil then xs=xshad end
yshad=text:match("^{[^}]-\\yshad([%d%.%-]+)")
if yshad~=nil then ys=yshad end
if xs==nil then xs=stshad end
if ys==nil then ys=stshad end
else xs=xsh ys=ysh
end
if not text:match("\\pos") and not text:match("\\move") then text=getpos(subs,text) end
text=text:gsub("\\[xy]?shad([%d%.%-]+)","")
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("{\\[^}]-}",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)
if not text2:match("^{[^}]-\\c&") then text2=addtag("\\c"..shadcol,text2) end
if not text2:match("^{[^}]-\\3c&") then text2=addtag("\\3c"..shadcol,text2) end
l2.text=text2
subs.insert(i+1,l2)
line.layer=line.layer+1
end
line.text=text
subs[i]=line
end
table.sort(sel)
if res.posi=="shadow layer" then for s=1,#sel do sel[s]=sel[s]+s end end
return sel
end
function bilocator(subs, sel)
for i=#sel,1,-1 do
aegisub.progress.title(string.format("Moving through hyperspace... %d/%d",(#sel-i+1),#sel))
line=subs[sel[i]]
text=line.text
if res.move=="transmove" and sel[i]<#subs then
start=line.start_time -- start time
endt=line.end_time -- end time
nextline=subs[sel[i]+1]
text2=nextline.text
text=text:gsub("\\1c","\\c")
text2=text2:gsub("\\1c","\\c")
startf=ms2fr(start) -- startframe
endf=ms2fr(endt) -- endframe
start2=fr2ms(startf)
endt2=fr2ms(endf-1)
tim=fr2ms(1)
movt1=start2-start+tim -- first timecode in \move
movt2=endt2-start+tim -- second timecode in \move
movt=movt1..","..movt2
-- failcheck
if not text:match("\\pos") or not text2:match("\\pos") then
aegisub.dialog.display({{class="label",label="Missing \\pos tags.",x=0,y=0,width=1,height=2}},{"OK"})
aegisub.cancel()
end
-- move
p1=text:match("\\pos%(([^%)]+)%)")
p2=text2:match("\\pos%(([^%)]+)%)")
if p2~=p1 then text=text:gsub("\\pos%(([^%)]+)%)","\\move(%1,"..p2..","..movt..")") end
-- transforms
tf=""
tftags={"fs","fsp","fscx","fscy","blur","bord","shad","fax","fay"}
for tg=1,#tftags do
t=tftags[tg]
if text2:match("\\"..t.."[%d%.%-]+") then tag2=text2:match("(\\"..t.."[%d%.%-]+)")
if text:match("\\"..t.."[%d%.%-]+") then tag1=text:match("(\\"..t.."[%d%.%-]+)") else tag1="" end
if tag1~=tag2 then tf=tf..tag2 end
end
end
tfctags={"c","2c","3c","4c","1a","2a","3a","4a","alpha"}
for tg=1,#tfctags do
t=tfctags[tg]
if text2:match("\\"..t.."&H%x+&") then tag2=text2:match("(\\"..t.."&H%x+&)")
if text:match("\\"..t.."&H%x+&") then tag1=text:match("(\\"..t.."&H%x+&)") else tag1="" end
if tag1~=tag2 then tf=tf..tag2 end
end
end
tfrtags={"frz","frx","fry"}
for tg=1,#tfrtags do
t=tfrtags[tg]
if text2:match("\\"..t.."[%d%.%-]+") then
tag2=text2:match("(\\"..t.."[%d%.%-]+)") rr2=tonumber(text2:match("\\"..t.."([%d%.%-]+)"))
if text:match("\\"..t.."[%d%.%-]+") then
tag1=text:match("(\\"..t.."[%d%.%-]+)") rr1=tonumber(text:match("\\"..t.."([%d%.%-]+)"))
else tag1="" rr1="0" end
if tag1~=tag2 then
if res.rotac and math.abs(rr2-rr1)>180 then
if rr2>rr1 then rr2=rr2-360 tag2="\\frz"..rr2 else
rr1=rr1-360 text=text:gsub("\\frz[%d%.%-]+","\\frz"..rr1)
end
end
tf=tf..tag2 end
end
end
-- apply transform
if tf~="" then
text=text:gsub("^({\\[^}]-)}","%1\\t("..movt..","..tf..")}")
end
-- delete line 2
if res.keep==false then subs.delete(sel[i]+1) end
end -- end of transmove
if res.move=="horizontal" then
text=text:gsub("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)","\\move(%1,%2,%3,%2") end
if res.move=="vertical" then
text=text:gsub("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)","\\move(%1,%2,%1,%4") end
if res.move=="rvrs. move" then
text=text:gsub("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)","\\move(%3,%4,%1,%2")
end
if res.move=="shiftmove" then
xx=res.eks yy=res.wai
text=text:gsub("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)",
function(a,b,c,d) return "\\move("..a..","..b..","..c+xx..","..d+yy end)
text=text:gsub("\\pos%(([%d%.%-]+),([%d%.%-]+)",function(a,b) return "\\move("..a..","..b..","..a+xx..","..b+yy end)
end
if res.move=="shiftstart" then
xx=res.eks yy=res.wai
text=text:gsub("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)",
function(a,b,c,d) return "\\move("..a+xx..","..b+yy..","..c..","..d end)
end
if res.move=="move clip" then
m1,m2,m3,m4=text:match("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)")
mt=text:match("\\move%([^,]+,[^,]+,[^,]+,[^,]+,([%d%.,%-]+)")
if mt==nil then mt="" else mt=mt.."," end
klip=text:match("\\i?clip%([%d%.,%-]+%)")
klip=klip:gsub("(\\i?clip%()([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)",
function(a,b,c,d,e) return a..b+m3-m1..","..c+m4-m2..","..d+m3-m1..","..e+m4-m2 end)
text=addtag("\\t("..mt..klip..")",text)
end
line.text=text
subs[sel[i]]=line
end
end
function multimove(subs, sel)
for x, i in ipairs(sel) do
aegisub.progress.title(string.format("Synchronizing movement... %d/%d",x,#sel))
line=subs[i]
text=subs[i].text
-- error if first line's missing \move tag
if x==1 and text:match("\\move")==nil then aegisub.dialog.display({{class="label",
label="Missing \\move tag on line 1",x=0,y=0,width=1,height=2}},{"OK"})
mc=1
else
-- get coordinates from \move on line 1
if text:match("\\move") then
x1,y1,x2,y2,t,m1,m2=nil
if text:match("\\move%([%d%.%-]+,[%d%.%-]+,[%d%.%-]+,[%d%.%-]+,[%d%.,%-]+%)") then
x1,y1,x2,y2,t=text:match("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.,%-]+)%)")
else
x1,y1,x2,y2=text:match("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)%)")
end
m1=x2-x1 m2=y2-y1 -- difference between start/end position
end
-- error if any of lines 2+ don't have \pos tag
if x~=1 and text:match("\\pos")==nil then poscheck=1
else
-- apply move coordinates to lines 2+
if x~=1 and m2~=nil then
p1,p2=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)%)")
if t~=nil then
text=text:gsub("\\pos%(([%d%.%-]+),([%d%.%-]+)%)","\\move%(%1,%2,"..p1+m1..","..p2+m2..","..t.."%)")
else
text=text:gsub("\\pos%(([%d%.%-]+),([%d%.%-]+)%)","\\move(%1,%2,"..p1+m1..","..p2+m2..")")
end
end
end
end
line.text=text
subs[i]=line
end
if poscheck==1 then aegisub.dialog.display({{class="label",
label="Some lines are missing \\pos tags",x=0,y=0,width=1,height=2}},{"OK"}) end
x1,y1,x2,y2,t,m1,m2=nil
poscheck=0
end
function round4(a,b,c,d)
a=math.floor(a+0.5)
b=math.floor(b+0.5)
c=math.floor(c+0.5)
d=math.floor(d+0.5)
return a,b,c,d
end
function round(a)
a=math.floor(a+0.5)
return a
end
function modifier(subs, sel)
xx=res.eks yy=res.wai
for x, i in ipairs(sel) do
aegisub.progress.title(string.format("Morphing... %d/%d",x,#sel))
line=subs[i]
text=line.text
if res.mod=="fullmovetimes" or res.mod=="fulltranstimes" then
start=line.start_time -- start time
endt=line.end_time -- end time
startf=ms2fr(start) -- startframe
endf=ms2fr(endt) -- endframe
start2=fr2ms(startf)
endt2=fr2ms(endf-1)
tim=fr2ms(1)
movt1=start2-start+tim -- first timecode in \move
movt2=endt2-start+tim -- second timecode in \move
movt=movt1..","..movt2
end
if res.mod=="round numbers" then
if text:match("\\pos") and res.rnd=="all" or text:match("\\pos") and res.rnd=="pos" then
px,py=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)%)")
px,py=round4(px,py,0,0)
text=text:gsub("\\pos%([%d%.%-]+,[%d%.%-]+%)","\\pos("..px..","..py..")")
end
if text:match("\\org") and res.rnd=="all" or text:match("\\org") and res.rnd=="org" then
ox,oy=text:match("\\org%(([%d%.%-]+),([%d%.%-]+)%)")
ox,oy=round4(ox,oy,0,0)
text=text:gsub("\\org%([%d%.%-]+,[%d%.%-]+%)","\\org("..ox..","..oy..")")
end
if text:match("\\move") and res.rnd=="all" or text:match("\\move") and res.rnd=="move" then
mo1,mo2,mo3,mo4=text:match("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)")
mo1,mo2,mo3,mo4=round4(mo1,mo2,mo3,mo4)
text=text:gsub("\\move%([%d%.%-]+,[%d%.%-]+,[%d%.%-]+,[%d%.%-]+","\\move("..mo1..","..mo2..","..mo3..","..mo4)
end
if text:match("\\i?clip") and res.rnd=="all" or text:match("\\i?clip") and res.rnd=="clip" then
for klip in text:gmatch("\\i?clip%([^%)]+%)") do
klip2=klip:gsub("([%d%.%-]+)",function(c) return round(c) end)
klip=esc(klip)
text=text:gsub(klip,klip2)
end
end
if text:match("\\p1") and res.rnd=="all" or text:match("\\p1") and res.rnd=="mask" then
tags=text:match("^{\\[^}]-}")
text=text:gsub("^{\\[^}]-}","") :gsub("([%d%.%-]+)",function(m) return round(m) end)
text=tags..text
end
end
if res.mod=="killmovetimes" then
text=text:gsub("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)","\\move(%1,%2,%3,%4")
end
if res.mod=="fullmovetimes" then
text=text:gsub("\\move%(([%d%.%-]+,[%d%.%-]+,[%d%.%-]+,[%d%.%-]+),([%d%.%-]+),([%d%.%-]+)","\\move(%1,"..movt)
text=text:gsub("\\move%(([%d%.%-]+,[%d%.%-]+,[%d%.%-]+,[%d%.%-]+)%)","\\move(%1,"..movt..")")
end
if res.mod=="fulltranstimes" then
text=text:gsub("\\t%([%d,%.]-\\","\\t("..movt..",\\")
text=text:gsub("\\t%(\\","\\t("..movt..",\\")
end
if res.mod=="move v. clip" then
if x==1 then v1,v2=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)%)")
if v1==nil then
aegisub.dialog.display({{class="label",label="Error. No \\pos tag on line 1.",x=0,y=0,width=1,height=2}},
{"OK"},{close='OK'}) aegisub.cancel() end
end
if x~=1 and text:match("\\pos") then v3,v4=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)%)")
V1=v3-v1 V2=v4-v2
if text:match("clip%(m [%d%a%s%-%.]+%)") then
ctext=text:match("clip%(m ([%d%a%s%-%.]+)%)")
ctext2=ctext:gsub("([%d%-%.]+)%s([%d%-%.]+)",function(a,b) return a+V1.." "..b+V2 end)
ctext=ctext:gsub("%-","%%-")
text=text:gsub("clip%(m "..ctext,"clip(m "..ctext2)
end
if text:match("clip%(%d+,m [%d%a%s%-%.]+%)") then
fac,ctext=text:match("clip%((%d+),m ([%d%a%s%-%.]+)%)")
factor=2^(fac-1)
ctext2=ctext:gsub("([%d%-%.]+)%s([%d%-%.]+)",function(a,b) return a+factor*V1.." "..b+factor*V2 end)
ctext=ctext:gsub("%-","%%-")
text=text:gsub(",m "..ctext,",m "..ctext2)
end
end
end
if res.mod=="set origin" then
text=text:gsub("\\pos%(([%d%.%-]+),([%d%.%-]+)%)",
function(a,b) return "\\pos("..a..","..b..")\\org("..a+res.eks..","..b+res.wai..")" end)
end
if res.mod=="calculate origin" then
local c={}
local c2={}
x1,y1,x2,y2,x3,y3,x4,y4=text:match("clip%(m ([%d%-]+) ([%d%-]+) l ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+) ([%d%-]+)")
cor1={x=tonumber(x1),y=tonumber(y1)} table.insert(c,cor1) table.insert(c2,cor1)
cor2={x=tonumber(x2),y=tonumber(y2)} table.insert(c,cor2) table.insert(c2,cor2)
cor3={x=tonumber(x3),y=tonumber(y3)} table.insert(c,cor3) table.insert(c2,cor3)
cor4={x=tonumber(x4),y=tonumber(y4)} table.insert(c,cor4) table.insert(c2,cor4)
table.sort(c, function(a,b) return tonumber(a.x)<tonumber(b.x) end) -- sorted by x
table.sort(c2, function(a,b) return tonumber(a.y)<tonumber(b.y) end) -- sorted by y
-- i don't even know myself how all this shit works
xx1=c[1].x yy1=c[1].y
xx2=c[4].x yy2=c[4].y
yy3=c2[1].y xx3=c2[1].x
yy4=c2[4].y xx4=c2[4].x
distx1=c[2].x-c[1].x disty1=c[2].y-c[1].y
distx2=c[4].x-c[3].x disty2=c[4].y-c[3].y
distx3=c2[2].x-c2[1].x disty3=c2[2].y-c2[1].y
distx4=c2[4].x-c2[3].x disty4=c2[4].y-c2[3].y
-- x/y factor / angle / whatever
fx1=math.abs(disty1/distx1)
fx2=math.abs(disty2/distx2)
fx3=math.abs(distx3/disty3)
fx4=math.abs(distx4/disty4)
-- determine if y is going up or down
cy=1
if c[2].y>c[1].y then cx1=round(xx1-(yy1-cy)/fx1) else cx1=round(xx1+(yy1-cy)/fx1) end
if c[4].y>c[3].y then cx2=round(xx2-(yy2-cy)/fx2) else cx2=round(xx2+(yy2-cy)/fx2) end
top=cx2-cx1
cy=500
if c[2].y>c[1].y then cx1=round(xx1-(yy1-cy)/fx1) else cx1=round(xx1+(yy1-cy)/fx1) end
if c[4].y>c[3].y then cx2=round(xx2-(yy2-cy)/fx2) else cx2=round(xx2+(yy2-cy)/fx2) end
bot=cx2-cx1
if top>bot then cy=c2[4].y ycalc=1 else cy=c2[1].y ycalc=-1 end
-- LOOK FOR ORG X
repeat
if c[2].y>c[1].y then cx1=round(xx1-(yy1-cy)/fx1) else cx1=round(xx1+(yy1-cy)/fx1) end
if c[4].y>c[3].y then cx2=round(xx2-(yy2-cy)/fx2) else cx2=round(xx2+(yy2-cy)/fx2) end
cy=cy+ycalc
-- aegisub.log("\n cx1: "..cx1.." cx2: "..cx2.." cy: "..cy)
until cx1>=cx2 or math.abs(cy)==50000
org1=cx1
-- determine if x is going left or right
cx=1
if c2[2].x>c2[1].x then cy1=round(yy3-(xx3-cx)/fx3) else cy1=round(yy3+(xx3-cx)/fx3) end
if c2[4].x>c2[3].x then cy2=round(yy4-(xx4-cx)/fx4) else cy2=round(yy4+(xx4-cx)/fx4) end
left=cy2-cy1
cx=500
if c2[2].x>c2[1].x then cy1=round(yy3-(xx3-cx)/fx3) else cy1=round(yy3+(xx3-cx)/fx3) end
if c2[4].x>c2[3].x then cy2=round(yy4-(xx4-cx)/fx4) else cy2=round(yy4+(xx4-cx)/fx4) end
rite=cy2-cy1
if left>rite then cx=c[4].x xcalc=1 else cx=c[1].x xcalc=-1 end
-- LOOK FOR ORG Y
repeat
if c2[2].x>c2[1].x then cy1=round(yy3-(xx3-cx)/fx3) else cy1=round(yy3+(xx3-cx)/fx3) end
if c2[4].x>c2[3].x then cy2=round(yy4-(xx4-cx)/fx4) else cy2=round(yy4+(xx4-cx)/fx4) end
cx=cx+xcalc
-- aegisub.log("\n cy1: "..cy1.." cy2: "..cy2)
until cy1>=cy2 or math.abs(cx)==50000
org2=cy1
text=text:gsub("\\org%([^%)]+%)","")
text=addtag("\\org("..org1..","..org2..")",text)
end
if res.mod=="FReeZe" then
frz=res.freeze
if text:match("^{[^}]*\\frz") then
text=text:gsub("^({[^}]*\\frz)([%d%.%-]+)","%1"..frz)
else
text=addtag("\\frz"..frz,text)
end
end
if res.mod=="rotate 180" then
if text:match("\\frz") then rot="frz" text=flip(rot,text)
else
text=addtag("\\frz180",text)
end
end
if res.mod=="flip hor." then
if text:match("\\fry") then rot="fry" text=flip(rot,text)
else
text=addtag("\\fry180",text)
end
end
if res.mod=="flip vert." then
if text:match("\\frx") then rot="frx" text=flip(rot,text)
else
text=addtag("\\frx180",text)
end
end
if res.mod=="vector2rect." then
text=text:gsub("\\(i?)clip%(m%s(%d-)%s(%d-)%sl%s(%d-)%s(%d-)%s(%d-)%s(%d-)%s(%d-)%s(%d-)%)","\\%1clip(%2,%3,%6,%7)")
end
if res.mod=="rect.2vector" then
text=text:gsub("\\(i?)clip%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)%)",function(ii,a,b,c,d)
a,b,c,d=round4(a,b,c,d) return string.format("\\"..ii.."clip(m %d %d l %d %d %d %d %d %d)",a,b,c,b,c,d,a,d) end)
end
if res.mod=="find centre" then
text=text:gsub("\\pos%([^%)]+%)","") t2=text
text=text:gsub("\\clip%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)%)",function(a,b,c,d)
x=round(a/2+c/2) y=round(b/2+d/2) return "\\pos("..x..","..y..")" end)
if t2==text then
aegisub.dialog.display({{class="label",label="Requires rectangular clip"}},{"OK"},{close='OK'}) aegisub.cancel() end
end
if res.mod=="extend mask" then
if xx==0 and yy==0 then aegisub.dialog.display({{class="label",label="Error. Both given values are 0.\nUse the Teleporter X and Y fields."}},{"OK"},{close='OK'}) aegisub.cancel() end
draw=text:match("}m ([^{]+)")
draw2=draw:gsub("([%d%.%-]+) ([%d%.%-]+)",function(a,b)
if tonumber(a)>0 then ax=xx elseif tonumber(a)<0 then ax=0-xx else ax=0 end
if tonumber(b)>0 then by=yy elseif tonumber(b)<0 then by=0-yy else by=0 end
return a+ax.." "..b+by end)
draw=esc(draw)
text=text:gsub("(}m )"..draw,"%1"..draw2)
end
if res.mod=="expand mask" then
if xx==0 and yy==0 then aegisub.dialog.display({{class="label",label="Error. Both given values are 0.\nUse the Teleporter X and Y fields."}},{"OK"},{close='OK'}) aegisub.cancel() end
if xx==0 then xx=1 end
if yy==0 then yy=1 end
draw=text:match("}m ([^{]+)")
draw2=draw:gsub("([%d%.%-]+) ([%d%.%-]+)",function(a,b) return round(a*xx).." "..round(b*yy) end)
draw=esc(draw)
text=text:gsub("(}m )"..draw,"%1"..draw2)
end
if res.mod=="flip mask" then
draw=text:match("}m ([^{]+)")
draw2=draw:gsub("([%d%.%-]+) ([%d%.%-]+)",function(a,b) return 0-a.." "..b end)
draw=esc(draw)
text=text:gsub("(}m )"..draw,"%1"..draw2)
end
if res.mod=="adjust drawing" then
if not text:match("\\p%d") then aegisub.cancel() end
-- drawing 2 clip
if not text:match("\\i?clip") then
klip="\\clip("..text:match("\\p1[^}]-}(m [^{]*)")..")"
scx=text:match("\\fscx([%d%.]+)") if scx==nil then scx=100 end
scy=text:match("\\fscy([%d%.]+)") if scy==nil then scy=100 end
if text:match("\\pos") then
local xx,yy=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)%)")
xx=round(xx) yy=round(yy)
coord=klip:match("\\clip%(m ([^%)]+)%)")
coord2=coord:gsub("([%d%-]+)%s([%d%-]+)",function(a,b) return round(a*scx/100+xx).." "..round(b*scy/100+yy) end)
coord=coord:gsub("%-","%%-")
klip=klip:gsub(coord,coord2)
end
if not text:match("\\pos") then text=text:gsub("^{","{\\pos(0,0)") end
text=addtag(klip,text)
-- clip 2 drawing
else
text=text:gsub("\\i?clip%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)%)",function(a,b,c,d)
a,b,c,d=round4(a,b,c,d) return string.format("\\clip(m %d %d l %d %d %d %d %d %d)",a,b,c,b,c,d,a,d) end)
klip=text:match("\\i?clip%((m.-)%)")
if text:match("\\pos") then
local xx,yy=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)%)")
xx=round(xx) yy=round(yy)
coord=klip:match("m ([%d%a%s%-]+)")
coord2=coord:gsub("([%d%-]+)%s([%d%-]+)",function(a,b) return a-xx.." "..b-yy end)
coord=coord:gsub("%-","%%-")
klip=klip:gsub(coord,coord2)
end
text=text:gsub("(\\p1[^}]-})(m [^{]*)","%1"..klip)
if not text:match("\\pos") then text=text:gsub("^{","{\\pos(0,0)") end
if text:match("\\an") then text=text:gsub("\\an%d","\\an7") else text=text:gsub("^{","{\\an7") end
if text:match("\\fscx") then text=text:gsub("\\fscx[%d%.]+","\\fscx100") else text=text:gsub("\\p1","\\fscx100\\p1") end
if text:match("\\fscy") then text=text:gsub("\\fscy[%d%.]+","\\fscy100") else text=text:gsub("\\p1","\\fscy100\\p1") end
text=text:gsub("\\i?clip%(.-%)","")
end
end
if res.mod=="randomask" then
draw=text:match("}m ([^{]+)")
draw2=draw:gsub("([%d%.%-]+)",function(a) return a+math.random(0-res.post,res.post) end)
draw=esc(draw)
text=text:gsub("(}m )"..draw,"%1"..draw2)
end
if res.mod=="randomize..." then
if x==1 then
randomgui={
{x=0,y=0,width=1,height=1,class="label",label="randomization value"},
{x=1,y=0,width=1,height=1,class="floatedit",name="random",value=3},
{x=0,y=1,width=1,height=1,class="label",label="rounding"},
{x=1,y=1,width=1,height=1,class="dropdown",name="dec",items={"1","0.1","0.01","0.001"},value="0.1",},
{x=1,y=2,width=1,height=1,class="edit",name="randomtag"},
{x=1,y=3,width=1,height=1,class="edit",name="partag1",hint="pos, move, org, clip, (fad)"},
{x=1,y=4,width=1,height=1,class="edit",name="partag2",hint="pos, move, org, clip, (fad)"},
{x=0,y=2,width=1,height=1,class="checkbox",name="ntag",label="standard type tag - \\",value=true,hint="\\[tag][number]"},
{x=0,y=3,width=1,height=1,class="checkbox",name="ptag1",label="parenthesis tag x - \\",value=false,hint="\\tag(X,y)"},
{x=0,y=4,width=1,height=1,class="checkbox",name="ptag2",label="parenthesis tag y - \\",value=false,hint="\\tag(x,Y)"},
}
press,rez=aegisub.dialog.display(randomgui,{"Randomize","Disintegrate"},{ok='Randomize',close='Disintegrate'})
if press=="Disintegrate" then aegisub.cancel() end
rt=rez.randomtag rtx=rez.partag1 rty=rez.partag2
deci=1/tonumber(rez.dec) rnd=rez.random
end
-- standard tags
if rez.ntag then
for tag in text:gmatch("\\"..rt.."[%d%.%-]+[\\}].") do
tagval,mark=tag:match("([%d%.%-]+)[\\}](.)")
tagval1=esc(tagval)
rndm=math.random(-100,100)/100*rnd
text=text:gsub("\\"..rt..tagval1.."([\\}])"..mark,"\\"..rt..round((tagval+rndm)*deci)/deci.."%1"..mark)
end
end
-- parenthesis tags
if rez.ptag1 or rez.ptag2 then
rndm=math.random(-100,100)/100*rnd
if rez.ptag1 then
text=text:gsub("\\"..rtx.."%(([%d%.%-]+),([%d%.%-]+)",
function(x,y) return "\\"..rtx.."("..round((x+rndm)*deci)/deci..","..y end)
:gsub("\\"..rtx.."%(([%d%.%-]+,[%d%.%-]+,)([%d%.%-]+),([%d%.%-]+)",
function(a,x,y) return "\\"..rtx.."("..a..round((x+rndm)*deci)/deci..","..y end)
end
if rez.ptag2 then
text=text:gsub("\\"..rty.."%(([%d%.%-]+),([%d%.%-]+)",
function(x,y) return "\\"..rty.."("..x..","..round((y+rndm)*deci)/deci end)
:gsub("\\"..rty.."%(([%d%.%-]+,[%d%.%-]+,)([%d%.%-]+),([%d%.%-]+)",
function(a,x,y) return "\\"..rty.."("..a..x..","..round((y+rndm)*deci)/deci end)
end
end
end
if res.mod=="letterbreak" then
if not text:match("^({\\[^}]-})") then
notag1=text:match("^([^{]+)")
local notag2=notag1:gsub("([%a%s%d])","%1\\N")
notag=esc(notag1)
text=text:gsub(notag1,notag2)
end
for notag in text:gmatch("{\\[^}]-}([^{]+)") do
local notag2=notag:gsub("([%a%s%d])","%1\\N")
notag=esc(notag)
text=text:gsub(notag,notag2)
end
text=text:gsub("\\N$","")
end
if res.mod=="wordbreak" then
if not text:match("^({\\[^}]-})") then
notag1=text:match("^([^{]+)")
local notag2=notag1:gsub("%s+"," \\N")
notag=esc(notag1)
text=text:gsub(notag1,notag2)
end
for notag in text:gmatch("{\\[^}]-}([^{]+)") do
local notag2=notag:gsub("%s+"," \\N")
notag=esc(notag)
text=text:gsub(notag,notag2)
end
text=text:gsub("\\N$","")
end
line.text=text
subs[i]=line
end
end
function flip(rot,text)
for rotation in text:gmatch("\\"..rot.."([%d%.%-]+)") do
rotation=tonumber(rotation)
if rotation<180 then newrot=rotation+180 end
if rotation>=180 then newrot=rotation-180 end
text=text:gsub(rot..rotation,rot..newrot)
end
return text
end
function movetofbf(subs, sel)
fra={}
for i=#sel,1,-1 do
line=subs[sel[i]]
text=subs[sel[i]].text
styleref=stylechk(subs,line.style)
start=line.start_time
endt=line.end_time
startf=ms2fr(start)
endf=ms2fr(endt)
frames=endf-1-startf
frnum=frames
table.insert(fra,frnum)
l2=line
for frm=endf-1,startf,-1 do
l2.text=text
-- move
if text:match("\\move") then
m1,m2,m3,m4=text:match("\\move%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)")
mvstart,mvend=text:match("\\move%([%d%.%-]+,[%d%.%-]+,[%d%.%-]+,[%d%.%-]+,([%d%.%-]+),([%d%.%-]+)")
if mvstart==nil then mvstart=fr2ms(startf)-start end
if mvend==nil then mvend=fr2ms(endf-1)-start end
mstartf=ms2fr(start+mvstart) mendf=ms2fr(start+mvend)
moffset=mstartf-startf if moffset<0 then moffset=0 end
mlimit=mendf-startf
mpart=frnum-moffset
mwhole=mlimit-moffset
pos1=math.floor((((m3-m1)/mwhole)*mpart+m1)*100)/100
pos2=math.floor((((m4-m2)/mwhole)*mpart+m2)*100)/100
if mpart<0 then pos1=m1 pos2=m2 end
if mpart>mlimit-moffset then pos1=m3 pos2=m4 end
l2.text=text:gsub("\\move%([^%)]*%)","\\pos("..pos1..","..pos2..")")
end
--fade
if text:match("\\fad%(") then
f1,f2=text:match("\\fad%(([%d%.]+),([%d%.]+)")
fad_in=ms2fr(start+f1)
fad_out=ms2fr(endt-f2)
foffset=fad_out-startf-1
fendf=fad_in-startf
fpart=frnum-foffset
fwhole=endf-fad_out
faf="&HFF&" fa0="&H00&"
-- existing alpha
linealfa=text:match("^{[^}]-\\alpha(&H%x%x&)")
if linealfa~=nil then fa0=linealfa l2.text=l2.text:gsub("^({[^}]-)\\alpha&H%x%x&","%1") end
fa1=interpolate_alpha(1/(fendf+3), faf, fa0)
fa2=interpolate_alpha(1/(fwhole+3), faf, fa0)
val_in=interpolate_alpha(frnum/fendf, fa1, fa0)
val_out=interpolate_alpha(fpart/fwhole, fa0, fa2)
if frnum<fad_in-startf then alfa=val_in
elseif frnum>fad_out-startf then alfa=val_out
else alfa=fa0 end
l2.text=l2.text:gsub("\\fad%([^%)]*%)","\\alpha"..alfa)
-- other alphas
for al=1,4 do
alphax=text:match("^{[^}]-\\"..al.."a(&H%x%x&)")
if alphax~=nil then
val_in=interpolate_alpha(frnum/fendf, fa1, alphax)
val_out=interpolate_alpha(fpart/fwhole, alphax, fa2)
if frnum<fad_in-startf then alfa=val_in
elseif frnum>fad_out-startf then alfa=val_out
else alfa=alphax end
end
l2.text=l2.text:gsub("^({[^}]-)\\"..al.."a&H%x%x&","%1\\"..al.."a"..alfa)
end
end
tags=l2.text:match("^{[^}]*}")
if tags==nil then tags="" end
-- transforms
if tags:match("\\t") then
l2.text=l2.text:gsub("^({\\[^}]-})",function(tg) return cleantr(tg) end)
terraform(tags)
l2.text=l2.text:gsub("(\\t%([^%(%)]-%([^%)]-%)[^%)]-%))","") :gsub("(\\t%([^%(%)]-%))","")
l2.text=l2.text:gsub("^({[^}]*)}","%1"..ftags.."}")
l2.text=l2.text:gsub("({%*?\\[^}]-})",function(tg) return duplikill(tg) end)
end
l2.start_time=fr2ms(frm)
l2.end_time=fr2ms(frm+1)
subs.insert(sel[i]+1,l2) --table.insert(sel,sel[i]+frnum+1)
frnum=frnum-1
end
line.end_time=endt
line.comment=true
line.text=text
subs[sel[i]]=line
--table.sort(sel)
if res.delfbf then subs.delete(sel[i]) end
end
-- selection
sel2={}
if res.delfbf then fakt=0 else fakt=1 end
for s=#sel,1,-1 do
sfr=fra[#sel-s+1]
-- shift new sel
for s2=#sel2,1,-1 do
sel2[s2]=sel2[s2]+sfr+fakt
end
-- add to new sel
for f=1,sfr+fakt do
table.insert(sel2,sel[s]+f)
end
-- add orig line
if res.delfbf then table.insert(sel2,sel[s]) end
end
sel=sel2
return sel
end
function terraform(tags)
tra=tags:match("(\\t%([^%(%)]-%))")
if tra==nil then tra=text:match("(\\t%([^%(%)]-%([^%)]-%)[^%)]-%))") end --aegisub.log("\ntra: "..tra)
trstart,trend=tra:match("\\t%((%d+),(%d+)")
--frdiff=(fr2ms(startf+1)-fr2ms(startf))/2
if trstart==nil or trstart=="0" then trstart=fr2ms(startf)-start end
if trend==nil or trend=="0" then trend=fr2ms(endf-1)-start end
tfstartf=ms2fr(start+trstart) tfendf=ms2fr(start+trend)
toffset=tfstartf-startf if toffset<0 then toffset=0 end
tlimit=tfendf-startf
tpart=frnum-toffset
twhole=tlimit-toffset
nontra=tags:gsub("(\\t%([^%(%)]-%([^%)]-%)[^%)]-%))","") :gsub("(\\t%([^%(%)]-%))","")
ftags=""
-- most tags
for tg, valt in tra:gmatch("\\(%a+)([%d%.%-]+)") do
val1=nil
if nontra:match(tg) then val1=nontra:match("\\"..tg.."([%d%.%-]+)") end
if val1==nil then
if tg=="bord" or tg=="xbord" or tg=="ybord" then val1=styleref.outline end
if tg=="shad" or tg=="xshad" or tg=="yshad" then val1=styleref.shadow end
if tg=="fs" then val1=styleref.fontsize end
if tg=="fsp" then val1=styleref.spacing end