forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua.FadeWorks.lua
1498 lines (1362 loc) · 58.1 KB
/
ua.FadeWorks.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
script_name="FadeWorkS"
script_description="Makes pictograph overlays fade in and out of existence"
script_author="unanimated"
script_version="5.0"
script_namespace="ua.Fadeworks"
local haveDepCtrl,DependencyControl,depRec=pcall(require,"l0.DependencyControl")
if haveDepCtrl then
script_version="5.0.0"
depRec=DependencyControl{feed="https://raw.githubusercontent.com/unanimated/luaegisub/master/DependencyControl.json"}
end
re=require'aegisub.re'
function fadeconfig(subs,sel)
for i=1,#subs do
if subs[i].class=="dialogue" then line0=i-1 break end
end
GUI={
{x=0,y=0,width=4,class="label",label=script_name.." v"..script_version},
{x=0,y=1,class="label",label="Fade in:"},
{x=0,y=2,class="label",label="Fade out:"},
{x=1,y=1,width=3,class="floatedit",name="fadein",value=0},
{x=1,y=2,width=3,class="floatedit",name="fadeout",value=0},
{x=1,y=3,width=3,class="floatedit",name="inn",value=1,min=0,hint="accel in - <1 starts fast, >1 starts slow"},
{x=4,y=3,width=2,class="floatedit",name="utt",value=1,min=0,hint="accel out - <1 starts fast, >1 starts slow"},
{x=4,y=0,width=2,class="checkbox",name="alf",label="Alpha/&Colour"},
{x=4,y=1,class="checkbox",name="crl",label="&From:"},
{x=4,y=2,class="checkbox",name="clr",label="&To:"},
{x=5,y=1,class="color",name="c1"},
{x=5,y=2,class="color",name="c2"},
{x=0,y=3,class="label",label="Accel:"},
{x=0,y=4,width=5,class="checkbox",name="keepthefade",label="&Keep fade along with colour transforms"},
{x=0,y=5,width=4,class="checkbox",name="mult",label="Fade &across multiple lines"},
{x=4,y=5,width=2,class="checkbox",name="time",label="&Global time"},
{x=6,y=5,class="checkbox",name="ex",label="Clip &+",hint='Expand clip 3px in each direction'},
{x=0,y=6,width=7,class="label",label="\nFades from/to tags will be applied if valid tags && fade in or out are present",},
{x=0,y=7,class="label",label="Tags:",},
{x=1,y=7,width=5,class="edit",name="tags",value="\\blur3",
hint="(transformable) tags to be faded from/to\n(bord, shad, xbord, ybord, xshad, yshad, blur, be,\nfs, fscx, fscy, fsp, frz, frx, fry, fax, fay)"},
{x=6,y=7,class="label",label="e.g. \\blur3\\fs10"},
{x=0,y=8,class="label",label="In/out:",},
{x=1,y=8,width=3,class="floatedit",name="tgin",value=0,min=0,hint="fade in for tags"},
{x=4,y=8,width=2,class="floatedit",name="tgout",value=0,min=0,hint="fade out for tags"},
{x=6,y=8,class="label",label="times like for \\fad"},
{x=0,y=9,class="label",label="Accel:",},
{x=1,y=9,width=3,class="floatedit",name="tai",value=1,min=0,hint="accel for tags in"},
{x=4,y=9,width=2,class="floatedit",name="tao",value=1,min=0,hint="accel for tags out"},
{x=6,y=9,class="label",label="acc<1 slows down"},
{x=6,y=10,class="label",label="acc>1 speeds up"},
{x=0,y=10,class="label",label="By letter:"},
{x=1,y=10,class="dropdown",name="letterfade",items={"40","80","120","160","200","250","300","350","400","450","500","750","1000","1250","1500"},value="120"},
{x=2,y=10,width=2,class="label",label="ms/letter"},
{x=4,y=10,class="checkbox",name="rtl",label="&RTL",hint="right to left"},
{x=5,y=10,class="checkbox",name="del",label="&Delete",hint="delete letter-by-letter"},
{x=0,y=11,width=4,class="checkbox",name="ko",label="Letter by letter using \\&ko"},
{x=4,y=11,width=2,class="checkbox",name="word",label="\\ko b&y word"},
{x=6,y=11,class="checkbox",name="mir",label="&Mirror tags",hint="fade out to negative value (0-x)\nfor frz, fry, fax, xshad"},
{x=0,y=12,width=3,class="checkbox",name="vin",label="Fade &in to current frame"},
{x=4,y=12,width=3,class="checkbox",name="vout",label="Fade &out from current frame"},
{x=6,y=0,class="checkbox",name="hlp",label="[&Help]",hint="Apply Help"},
{x=6,y=1,class="checkbox",name="rem",label="Remember &last"},
{x=6,y=2,class="checkbox",name="rep",label="Re&peat last"},
{x=6,y=3,class="checkbox",name="save",label="[&Save config]"},
}
loadconfig()
if faded and res.rem then
for key,val in ipairs(GUI) do
if val.class=="checkbox" or val.class=="dropdown" or val.class:match"edit" or val.class=="color" then val.value=res[val.name] end
if val.name=='hlp' then val.value=false end
end
end
P,res=ADD(GUI,{"Apply Fade","By L&etter","&By Clip","Fade&works","Fade Away"},{ok='Apply Fade',cancel='Fade Away'})
fr2ms=aegisub.ms_from_frame
ms2fr=aegisub.frame_from_ms
if rep and res.rep then res=rep end
if res.rep and not rep then t_error("Nothing to repeat",1) end
tagcheck()
fadin=res.fadein
fadout=res.fadeout
faded=true
if TAGS and fadin>0 or TAGS and fadout>0 then res.alf=true end
if res.hlp then P="Apply Fade" end
if P=="Apply Fade" then
if res.hlp then fadehelp(subs,sel)
elseif res.save then saveconfig()
elseif res.alf or TAGS or res.clr or res.crl then fadalpha(subs,sel)
elseif res.mult then fadeacross(subs,sel)
elseif res.vin or res.vout then vfade(subs,sel)
else fade(subs,sel) end
end
if P=="By L&etter" then if res.ko or res.word then koko_da(subs,sel) else fade(subs,sel) end end
if P=="Fade&works" then sel=fadeworks(subs,sel) end
if P=="&By Clip" then clipfade(subs,sel) end
rep=res
return sel
end
function tagcheck()
TAGS=nil
ttags=res.tags
if ttags:len()>3 and ttags:match"^\\" then TAGS=1 end
if res.tgin==0 and res.tgout==0 then TAGS=nil end
reftags={"bord","shad","fs","fscx","fscy","fsp","blur","be","frz","frx","fry","fax","fay","xshad","yshad","xbord","ybord"}
checktags="|bord|shad|fs|fscx|fscy|fsp|blur|be|frz|frx|fry|fax|fay|xshad|yshad|xbord|ybord|"
if TAGS then
for tg in ttags:gmatch("\\[^\\]+") do
tg1,tgval=tg:match("\\(%a+)(.*)")
rem=nil
if not checktags:match("|"..tg1.."|") then ttags=ttags:gsub(esc(tg),"") t_error("Wrong tag: "..tg) rem=1 end
if not rem and not tgval:match("^%-?%d+%.?%d*$") then ttags=ttags:gsub(esc(tg),"") t_error("Missing value: "..tg) end
end
if not ttags:match"^\\" then TAGS=nil end
end
end
function def(fadin,fadout)
if fadin<=1 and fadin>0 then fadin=round(dur*fadin) end
if fadout<=1 and fadout>0 then fadout=round(dur*fadout) end
if fadin<0 then fadin=dur+fadin end
if fadout<0 then fadout=dur+fadout end
if fadin<0 then fadin=0 end
if fadout<0 then fadout=0 end
return fadin,fadout
end
function fade(subs,sel)
for z,i in ipairs(sel) do
progress("Processing line #"..i-line0.." ["..z.."/"..#sel.."]")
line=subs[i]
text=line.text
dur=line.end_time-line.start_time
fadin,fadout=def(fadin,fadout)
-- remove existing fade
text=text:gsub("\\fad%b()","")
-- standard fade
if P=="Apply Fade" then
text="{\\fad("..fadin..","..fadout..")}"..text
text=text:gsub("%)}{\\",")\\") :gsub("{}","")
end
-- letter by letter
if P=="By L&etter" then
-- delete old letter-by-letter if present
if text:match("\\t%([%d,]+\\alpha[^%(%)]+%)}%S$") then
text=text:gsub("\\t%([%d,]*\\alpha[^%(%)]+%)","") :gsub("\\alpha&H%x+&","") :gsub("{}","")
end
re_check=0
if text:match "{[^}]*{" or text:match "}[^{]*}" or text:match "^[^{]*}" or text:match "{[^}]*$" then brackets=true end
local tback=text
visible=text:gsub("%b{}",""):gsub("^ *(.-) *$","%1"):gsub(" *\\N *"," "):gsub("^ +$","")
if not res.del and visible~="" and not brackets and not text:match '\\p1' then repeat text=tback
-- fail if letter fade is larger than total fade
lf=tonumber(res.letterfade)
if fadin>0 and fadin<=lf or fadout>0 and fadout<=lf then ADD({{class="label",
label="The fade for each letter must be smaller than overall fade."}},{"Fuck! Sorry, I'm stupid. Won't happen again."})
ak() end
-- mode: in, out, both
if fadout==0 then mode=1 elseif fadin==0 then mode=2 else mode=3 end
-- save linebreak patterns
breaks={}
for br in text:gmatch(" ?\\N") do
table.insert(breaks,br)
end
-- save initial tags; remove other tags/comments
tags=text:match("^({\\[^}]*})") or ""
orig=text:gsub("^({\\[^}]*})","") :gsub("{[^\\}]-}","") :gsub("\\N","{\\N}")
text=text:gsub("%b{}","") :gsub("%s*$","") :gsub("\\N","")
-- letter-by-letter fade happens here
outfade=dur-fadout
count=0
text3=""
al=tags:match("^{[^}]-\\alpha&H(%x%x)&") or "00"
matches=re.find(text,"\\S\\s*")
length=#matches
ftime1=((fadin-lf)/(length-1))
ftime2=((fadout-lf)/(length-1))
for _,match in ipairs(matches) do
ch=match.str
-- aegisub.log(ch)
if res.rtl then fin1=math.floor(ftime1*(#matches-count-1)) else fin1=math.floor(ftime1*count) end
fin2=fin1+lf
if res.rtl then fout1=math.floor(ftime2*(#matches-count-1)+outfade) else fout1=math.floor(ftime2*count+outfade) end
fout2=fout1+lf
if mode==1 then text2m="{\\alpha&HFF&\\t("..fin1..","..fin2..",\\alpha&H"..al.."&)}"..ch end
if mode==2 then text2m="{\\alpha&H"..al.."&\\t("..fout1..","..fout2..",\\alpha&HFF&)}"..ch end
if mode==3 then
text2m="{\\alpha&HFF&\\t("..fin1..","..fin2..",\\alpha&H"..al.."&)\\t("..fout1..","..fout2..",\\alpha&HFF&)}"..ch end
text3=text3..text2m
count=count+1
end
-- join saved tags + new text with transforms
text=tags..text3
text=text:gsub("}{","")
if orig:match("{\\") then text=textmod(orig,text) end
-- fix linebreaks
repeat text,c=text:gsub("{\\N","\\N{") until c==0
text=text:gsub("{([^}]-)\\N([^}]-)}","\\N{%1%2}"):gsub("{%**}",""):gsub("(%S)\\N","%1 \\N")
local b=0
text=text:gsub(" \\N",function() b=b+1 return breaks[b] end)
visible2=text:gsub("%b{}",""):gsub(" *\\N *"," ")
if visible~=visible2 then re_check=re_check+1 end
until visible==visible2 or re_check==256
if visible~=visible2 then
logg("Line #"..i-line0..": It appears that characters have been lost or added. \n If the problem isn't obvious from the two lines below, it's probably a failure of the re module.\n Undo (Ctrl+Z) and try again (Repeat Last might work). If the problem persists, rescan Autoload Dir.\n>> "..visible.."\n--> "..visible2.."\n")
end
end
if brackets then logg("Line #"..i-line0..": Messed up curly brackets. Skipping.\n -> "..text..'\n') end
end
text=text:gsub("\\fad%(0,0%)","") :gsub("{}","")
if line.text~=text and not brackets then
line.text=text
subs[i]=line
end
brackets=nil
end
end
-- ALPHA / COLOURS / TAGS -- ############################################
function fadalpha(subs,sel)
if res.clr or res.crl then res.alf=true end
if res.vin or res.vout then vfcheck() vt=math.floor((fr2ms(vframe+1)+fr2ms(vframe))/2) end
mirrors="|frz|fry|fax|xshad|"
for z,i in ipairs(sel) do
progress("Processing line: "..z.."/"..#sel)
line=subs[i]
text=line.text
ortext=text
sr=stylechk(subs,line.style)
st,et,dur=times()
fadin,fadout=def(fadin,fadout)
if res.vin then fadin=vt-st end
if res.vout then fadout=et-vt end
if not text:match("^{\\[^}]-}") then text="{\\arfa}"..text end
col1=res.c1:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
col2=res.c2:gsub("#(%x%x)(%x%x)(%x%x)","&H%3%2%1&")
SC1=sr.color1:gsub("H%x%x","H")
SC3=sr.color3:gsub("H%x%x","H")
SC4=sr.color4:gsub("H%x%x","H")
text=text:gsub("\\1c","\\c")
notra=text:gsub("\\t%b()","")
primary=notra:match("^{[^}]-\\c(&H%x+&)") or SC1
outline=notra:match("^{[^}]-\\3c(&H%x+&)") or SC3
shadcol=notra:match("^{[^}]-\\4c(&H%x+&)") or SC4
primary2=text:match("^{[^}]*\\c(&H%x+&)[^}]-}") or SC1
outline2=text:match("^{[^}]*\\3c(&H%x+&)[^}]-}") or SC3
shadcol2=text:match("^{[^}]*\\4c(&H%x+&)[^}]-}") or SC4
border=tonumber(text:match("^{[^}]-\\bord([%d%.]+)")) or sr.outline
shadow=tonumber(text:match("^{[^}]-\\shad([%d%.]+)")) or sr.shadow
kolora1="\\c"..col1 kolora3="\\3c"..col1 kolora4="\\4c"..col1 kolora,see1,see3,see4="","","",""
kolorb1="\\c"..col2 kolorb3="\\3c"..col2 kolorb4="\\4c"..col2 kolorb=""
if col1~=primary then kolora=kolora..kolora1 see1="\\c"..primary end
if col1~=outline then kolora=kolora..kolora3 see3="\\3c"..outline end
if col1~=shadcol then kolora=kolora..kolora4 see4="\\4c"..shadcol end
if col2~=primary2 then kolorb=kolorb..kolorb1 end
if col2~=outline2 then kolorb=kolorb..kolorb3 end
if col2~=shadcol2 then kolorb=kolorb..kolorb4 end
a00="\\alpha&H00&" aff="\\alpha&HFF&" lb=""
if res.alf then
if fadin~=0 then
-- fade from colour
if res.crl then
if kolora~="" then
text=text:gsub("^{\\[^}]-}",function(a)
if a:match("\\t") then
nt=""
for n,t in a:gmatch("(.-)(\\t%b())") do nt=nt..n:gsub("\\[34]?c&H%x+&","")..t end
nt=nt..a:match(".*\\t%b()(.-)$"):gsub("\\[34]?c&H%x+&","")
return nt
else return a:gsub("\\[34]?c&H%x+&","") end end)
text=text:gsub("^{}","{\\arfa}")
tfc=kolora.."\\t(0,"..fadin..","..res.inn..","..see1..see3..see4..")"
text=text:gsub("^({\\[^}]-)}",function(a)
if a:match("\\t") then
return a:gsub("^(.-)(\\t.*)","%1"..tfc.."%2}")
else return a..tfc.."}" end end)
if col1==primary and col1~=SC1 then text=addtag3(kolora1,text) end
if col1==outline and col1~=SC3 then text=addtag3(kolora3,text) end
if col1==shadcol and col1~=SC4 then text=addtag3(kolora4,text) end
end
-- inline colour tags
for t in text:gmatch(".({\\[^}]-})") do
det=t:gsub("\\t%b()","")
if det:match("\\[13]?c") then
col1=det:match("(\\c&H%x+&)") or "" if col1==kolora1 then col1="" end
col3=det:match("(\\3c&H%x+&)") or "" if col3==kolora3 then col3="" end
col4=det:match("(\\4c&H%x+&)") or "" if col4==kolora4 then col4="" end
if (col1..col3..col4):len()>0 then
tfic="\\t(0,"..fadin..","..res.inn..","..col1..col3..col4..")"
if t:match("\\t") then
t2=""
for n,tf in t:gmatch("(.-)(\\t%b())") do
t2=t2..n:gsub("\\c&H%x+&",kolora1):gsub("\\3c&H%x+&",kolora3):gsub("\\4c&H%x+&",kolora4)..tf end
t2=t2..t:match(".*\\t%b()(.-)$"):gsub("\\c&H%x+&",kolora1):gsub("\\3c&H%x+&",kolora3):gsub("\\4c&H%x+&",kolora4)
t2=t2:gsub("^(.-)(\\t.*)","%1"..tfic.."%2")
else
t2=t:gsub("\\c&H%x+&",kolora1):gsub("\\3c&H%x+&",kolora3):gsub("\\4c&H%x+&",kolora4)
t2=t2:gsub("({[^}]-)}","%1"..tfic.."}")
end
text=text:gsub(esc(t),t2)
end
end
end
else
-- fade from alpha
subalf=false
st_alf=notra:match("^{[^}]-(\\alpha&H%x%x&)")
st_a1=notra:match("^{[^}]-(\\1a&H%x%x&)")
st_a3=notra:match("^{[^}]-(\\3a&H%x%x&)")
st_a4=notra:match("^{[^}]-(\\4a&H%x%x&)")
if st_alf==nil then toalf=a00 else toalf=st_alf end
tosub=toalf:match("&H%x%x&")
if st_a1==nil then toa1="\\1a"..tosub else subalf=true toa1=st_a1 end
if st_a3==nil then toa3="\\3a"..tosub else subalf=true toa3=st_a3 end
if st_a4==nil then toa4="\\4a"..tosub else subalf=true toa4=st_a4 end
if subalf then toalf=toa1..toa3..toa4 else toalf=toalf end
fromalf=toalf:gsub("&H%x%x&","&HFF&")
text=text:gsub("^{\\[^}]-}",function(a)
if a:match("\\t") then
nt=""
for n,t in a:gmatch("(.-)(\\t%b())") do nt=nt..n:gsub("\\%w+a&H%x+&","")..t end
nt=nt..a:match(".*\\t%b()(.-)$"):gsub("\\%w+a&H%x+&","")
return nt
else return a:gsub("\\%w+a&H%x+&","") end end)
tfa=fromalf.."\\t(0,"..fadin..","..res.inn..","..toalf..")"
text=text:gsub("^({\\[^}]-)}",function(a)
if a:match("\\t") then
return a:gsub("^(.-)(\\t.*)","%1"..tfa.."%2}")
else return a..tfa.."}" end end)
-- inline alpha tags
for t in text:gmatch(".({\\[^}]-})") do
det=t:gsub("\\t%b()","")
arfa=det:match("(\\alpha&H%x+&)") or ""
arf1=det:match("(\\1a&H%x+&)") or ""
arf3=det:match("(\\3a&H%x+&)") or ""
arf4=det:match("(\\4a&H%x+&)") or ""
toarfa=arfa..arf1..arf3..arf4
if toarfa~="" then
fromarfa=toarfa:gsub("&H%x%x&","&HFF&")
tfia=fromarfa.."\\t(0,"..fadin..","..res.inn..","..toarfa..")"
if t:match("\\t") then
t2=""
for n,tf in t:gmatch("(.-)(\\t%b())") do
t2=t2..n:gsub("\\alpha&H%x+&","") :gsub("\\[134]a&H%x+&","")..tf end
t2=t2..t:match(".*\\t%b()(.-)$"):gsub("\\alpha&H%x+&","") :gsub("\\[134]a&H%x+&","")
t2=t2:gsub("^(.-)(\\t.*)","%1"..tfia.."%2")
else
t2=t:gsub("\\alpha&H%x+&","") :gsub("\\[134]a&H%x+&","")
t2=t2:gsub("({[^}]-)}","%1"..tfia.."}")
end
text=text:gsub(esc(t),t2)
end
end
end
end
if fadout~=0 then
-- fade to colour
if res.clr then
if kolorb~="" then
text=text:gsub("^({\\[^}]-)}","%1"..lb.."\\t("..dur-fadout..",0,"..res.utt..","..kolorb..")}")
end
-- inline colour tags
for t in text:gmatch(".({\\[^}]-})") do
if t:match("\\[13]?c") or t:match("\\alpha") then
k_out=""
if t:match(".*(\\c&H%x+&)")~=kolorb1 and t:match("\\c&H%x+&") then k_out=k_out..kolorb1 end
if t:match(".*(\\3c&H%x+&)")~=kolorb3 and t:match("\\3c&H%x+&") then k_out=k_out..kolorb3 end
if t:match(".*(\\4c&H%x+&)")~=kolorb4 and t:match("\\4c&H%x+&") then k_out=k_out..kolorb4 end
t2=t:gsub("({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.utt..","..k_out..")}")
text=text:gsub(esc(t),t2)
end
end
-- fade to alpha
else
if text:match("^{[^}]-(\\[134]a&H%x%x&)") then toarf="\\1a&HFF&".."\\3a&HFF&".."\\4a&HFF&" else toarf=aff end
text=text:gsub("^({\\[^}]-)}","%1"..lb.."\\t("..dur-fadout..",0,"..res.utt..","..toarf..")}")
-- inline alpha tags
for t in text:gmatch(".({\\[^}]-})") do
toarf=""
if t:match("\\alpha") then toarf=toarf..aff end
if t:match("\\1a") then toarf=toarf.."\\1a&HFF&" end
if t:match("\\3a") then toarf=toarf.."\\3a&HFF&" end
if t:match("\\4a") then toarf=toarf.."\\4a&HFF&" end
if toarf~="" then
t2=t:gsub("({\\[^}]-)}","%1\\t("..dur-fadout..",0,"..res.utt..","..toarf..")}")
text=text:gsub(esc(t),t2)
end
end
end
end
if border==0 then text=text:gsub("\\3c&H%x+&","") end
if shadow==0 then text=text:gsub("\\4c&H%x+&","") end
if not text:match("\\fad%(0,0%)") then text=text:gsub("\\fad%(%d+,%d+%)","") end -- nuke fade
if res.keepthefade then
if res.crl then f1=fadin else f1=0 end
if res.clr then f2=fadout else f2=0 end
if f1+f2>0 then text=text:gsub("^{\\","{\\fad("..f1..","..f2..")\\") end
end
text=text:gsub("\\t%([^\\]+%)","")
end
-- TAGS #######################################################################################################
if TAGS then
tgin,tgout=def(res.tgin,res.tgout)
origtags=""
tftagsIN=""
tftagsOUT=""
for tg in ttags:gmatch("\\[^\\]+") do
tag,tgval=tg:match("\\(%a+)(.*)")
midval=0
STAG=text:match("^{\\[^}]-}") or ""
midval=getvalue(tag)
tftagsIN=tftagsIN.."\\"..tag..tgval
if res.mir and mirrors:match("|"..tag.."|") then tgval=0-tgval end
tftagsOUT=tftagsOUT.."\\"..tag..tgval
origtags=origtags.."\\"..tag..midval
STAG=STAG:gsub("\\"..tag.."[^})\\]*","")
end
if not STAG:match("^{\\") then STAG="{\\notarealtag}"..STAG end
if tgin~=0 then intra=tftagsIN.."\\t(0,"..tgin..","..res.tai..","..origtags..")" else intra=origtags end
if tgout~=0 then transfinal=intra.."\\t("..dur-tgout..",0,"..res.tao..","..tftagsOUT..")" else transfinal=intra end
STAG=STAG:gsub("}",transfinal.."}"):gsub("\\notarealtag","")
STAG=duplikill(STAG)
text=text:gsub("^{\\[^}]-}",STAG)
end
text=text:gsub("\\arfa","")
line.text=text
subs[i]=line
end
end
function getvalue(tag)
V=STAG:match("\\"..tag.."(%-?%d+%.?%d*)")
if not V then
if tag=="bord" then V=sr.outline end
if tag=="shad" then V=sr.shadow end
if tag=="fs" then V=sr.fontsize end
if tag=="fsp" then V=sr.spacing end
if tag=="fscx" then V=sr.scale_x end
if tag=="fscy" then V=sr.scale_y end
if tag=="frz" then V=sr.angle end
V=V or "0"
end
return V
end
function koko_da(subs,sel)
if fadin<1 then t_error("Fade in must be at least 1",true) end
for x,i in ipairs(sel) do
progress("Processing line: "..x.."/"..#sel)
line=subs[i]
text=line.text
text=text:gsub("\\ko%d+","") :gsub("{}","")
-- save initial tags; remove other tags/comments
tags=text:match(STAG) or ""
orig=text:gsub("^({\\[^}]*})","")
text=text:gsub("{[^}]*}","") :gsub("%s*$","") :gsub("\\N","*")
--letter
if not res.word then
matches=re.find(text,"[\\w[:punct:]][\\s\\\\*]*")
len=#matches
if fadin>=40 then ko=round(fadin/(len-1))/10 else ko=fadin end
text=re.sub(text,"([\\w[:punct:]])","{\\\\ko"..ko.."}\\1")
else --word
matches=re.find(text,"[\\w[:punct:]]+[\\s\\\\*]*")
len=#matches
if fadin>=40 then ko=round(fadin/(len-1)/10) else ko=fadin end
text=re.sub(text,"([\\w[:punct:]]+)","{\\\\ko"..ko.."}\\1")
end
-- join saved tags + new text with transforms
text=tags..text
if not text:match("\\2a&HFF&") then text=text:gsub("^({.-)}","%1\\2a&HFF&}") end
text=text:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}") :gsub("%*","\\N")
if orig:match("{\\") then text=textmod(orig,text) end
line.text=text
subs[i]=line
end
end
function fadeacross(subs,sel)
if fadin<0 then fadin=0 end
if fadout<0 then fadout=0 end
full=0 war=0
S=subs[sel[1]].start_time
E=subs[sel[#sel]].end_time
-- get total duration
for z,i in ipairs(sel) do
line=subs[i]
dur=line.end_time-line.start_time
full=full+dur
if line.start_time<S then S=line.start_time war=1 end
if line.end_time>E then E=line.end_time war=1 end
end
-- Error if fades too long
if res.time and fadin+fadout>E-S or not res.time and fadin+fadout>full then
t_error("Error. Fades are longer than the duration of lines",true)
end
-- Warning if not sorted by time
if war==1 then t_error("Not sorted by time. \nDeal with the consequences.") end
-- Fade
full2=E-S
if res.time then full=full2 end
durs=0 durs1=0
for z,i in ipairs(sel) do
progress("Processing line: "..z.."/"..#sel)
line=subs[i]
text=line.text
start,endt,dur=times()
startf=ms2fr(start)
endf=ms2fr(endt)
if endf-startf==1 then oneframe=true else oneframe=false end
-- check shadow alpha
sr=stylechk(subs,line.style)
shad=sr.color4:match("H(%x%x)")
if text:match("\\4a") then shad=text:match("\\4a&H(%x%x)") end
if shad~="00" then shade=1-(tonumber(shad,16)/256) end
kill=1
-- fade in
if durs<fadin then
killpha()
durs1=durs1+dur durs2=endt-S
if res.time then durs=durs2 else durs=durs1 end
if durs>fadin then in_end=dur-(durs-fadin) tim="0,"..in_end.."," kill=0 else tim="" end
alfin_s=tohex(255-(round((durs-dur)/fadin*255)))
alfin_e=tohex(255-(round(durs/fadin*255)))
alfin_1=tohex(255-(round((durs-dur/2)/fadin*255)))
if shad~="00" then
shin_s=tohex(255-round((255-tonumber(alfin_s,16))*shade))
shin_e=tohex(255-round((255-tonumber(alfin_e,16))*shade))
shin_1=tohex(255-round((255-tonumber(alfin_1,16))*shade))
instart="\\1a&H"..alfin_s.."&\\3a&H"..alfin_s.."&\\4a&H"..shin_s.."&"
inend="\\1a&H"..alfin_e.."&\\3a&H"..alfin_e.."&\\4a&H"..shin_e.."&"
onefadin="\\1a&H"..alfin_1.."&\\3a&H"..alfin_1.."&\\4a&H"..shin_1.."&"
else
instart="\\alpha&H"..alfin_s.."&" inend="\\alpha&H"..alfin_e.."&"
onefadin="\\alpha&H"..alfin_1.."&"
end
if alfin_s~=alfin_e then
if oneframe then text=text:gsub("^({\\[^}]-)}","%1"..onefadin.."}")
else text=text:gsub("^({\\[^}]-)}","%1"..instart.."\\t("..tim..inend..")}")
end
end
elseif fadin>start-S and z>1 and start==subs[i-1].start_time then killpha()
text=text:gsub("^({\\[^}]-)}","%1\\alpha&H"..alfin_s.."&\\t("..tim.."\\alpha&H"..alfin_e.."&)}")
end
-- fade out
dure1=full
dure2=E-start
if res.time then dure=dure2 full=dure2-dur else dure=dure1 full=full-dur end
if full<fadout then
if kill==1 then killpha() end
if dure>fadout then out_start=dure-fadout tim=out_start..","..dur.."," else tim="" end
alfout_s=tohex(255-(round(dure/fadout*255)))
alfout_e=tohex(255-(round(full/fadout*255)))
alfout_1=tohex(255-(round((dure+full)/2/fadout*255)))
if shad~="00" then
shout_s=tohex(255-round((255-tonumber(alfout_s,16))*shade))
shout_e=tohex(255-round((255-tonumber(alfout_e,16))*shade))
shout_1=tohex(255-round((255-tonumber(alfout_1,16))*shade))
outstart="\\1a&H"..alfout_s.."&\\3a&H"..alfout_s.."&\\4a&H"..shout_s.."&"
outend="\\1a&H"..alfout_e.."&\\3a&H"..alfout_e.."&\\4a&H"..shout_e.."&"
onefadeout="\\1a&H"..alfout_1.."&\\3a&H"..alfout_1.."&\\4a&H"..shout_1.."&"
else
outstart="\\alpha&H"..alfout_s.."&" outend="\\alpha&H"..alfout_e.."&"
onefadeout="\\alpha&H"..alfout_1.."&"
end
if kill==1 then autstart=outstart else autstart="" end
if alfout_s~=alfout_e then
if oneframe then text=text:gsub("^({\\[^}]-)}","%1"..onefadeout.."}")
else text=text:gsub("^({\\[^}]-)}","%1"..autstart.."\\t("..tim..outend..")}")
end
end
end
text=text:gsub("\\fake","") :gsub("{}","")
line.text=text
subs[i]=line
end
end
function vfade(subs,sel)
vfcheck()
for z,i in ipairs(sel) do
line=subs[i]
text=line.text
st,et=times()
vt=math.floor((fr2ms(vframe+1)+fr2ms(vframe))/2)
vfin=vt-st
vfut=et-vt
if not text:match("\\fad%(") then text="{\\fad(0,0)}"..text text=text:gsub("{\\fad%(0,0%)}{\\","{\\fad(0,0)\\") end
if res.vin and vfin>0 then text=text:gsub("\\fad%(%d+,(%d+)%)","\\fad("..vfin..",%1)") end
if res.vout and vfut>0 then text=text:gsub("\\fad%((%d+),%d+%)","\\fad(%1,"..vfut..")") end
text=text:gsub("\\fad%(0,0%)","") :gsub("{}","")
line.text=text
subs[i]=line
end
end
function vfcheck()
if aegisub.project_properties==nil then t_error("Current frame unknown.\nProbably your Aegisub is too old.\nMinimum required: r8374.",true) end
vframe=aegisub.project_properties().video_position
if vframe==nil or fr2ms(1)==nil then t_error("Current frame unknown. Probably no video loaded.",true) end
end
-- Fade with Clip --
function clipfade(subs,sel)
for z,i in ipairs(sel) do
line=subs[i]
text=line.text
dur=line.end_time-line.start_time
tags=text:match(STAG) or ""
vis=nobra(text)
_,poses=text:gsub("\\N","") poses=poses+1
sr=stylechk(subs,line.style)
notra=detra(tags)
if not text:match'\\pos%b()' and not text:match'\\move%b()' then text=getpos(subs,text) end
posX,posY=text:match("\\pos%(([%d.-]+),([%d.-]+)%)")
m1,m2,m3,m4=text:match("\\move%(([%d.-]+),([%d.-]+),([%d.-]+),([%d.-]+)")
if m1 and not posY then posX=m1 posY=m2 end
scx=notra:match("\\fscx([%d%.]+)")
scy=notra:match("\\fscy([%d%.]+)")
fsp=notra:match("\\fsp([%d%.]+)")
fsize=notra:match("\\fs([%d%.]+)")
phont=notra:match("\\fn([^\\}]+)")
bord=notra:match("\\bord([%d%.]+)") or sr.outline
shad=notra:match("\\shad([%d%.]+)") or sr.shadow
xshad=notra:match("\\xshad([%d%.]+)") or shad
yshad=notra:match("\\yshad([%d%.]+)") or shad
bold=notra:match("\\b([01])")
if scx then sr.scale_x=tonumber(scx) end
if scy then sr.scale_y=tonumber(scy) end
if fsp then sr.spacing=tonumber(fsp) end
if fsize then sr.fontsize=tonumber(fsize) end
if phont then sr.fontname=phont end
if bold=='1' then sr.bold=true end
if bold=='0' then sr.bold=false end
w,h,d,el=aegisub.text_extents(sr,vis)
if poses>1 then
vis2=vis:gsub(" *\\N *","\n")
w=0
for vt in vis2:gmatch('[^\n]+') do
w1=aegisub.text_extents(sr,vt)
if w1>w then w=w1 end
end
h=h*poses
end
align=text:match("\\an(%d)") or tostring(sr.align)
x_left=posX
if align:match("[258]") then x_left=round(posX-w/2,1) end
if align:match("[369]") then x_left=round(posX-w,1) end
y_top=posY
if align:match("[456]") then y_top=round(posY-h/2,1) end
if align:match("[123]") then y_top=round(posY-h,1) end
x_right=round(x_left+w,1)
y_bottom=round(y_top+h,1)
ex=0
if res.ex then ex=3 end
x_left=x_left-bord-ex
y_top=y_top-bord-ex
x_right=x_right+bord+xshad+ex
y_bottom=y_bottom+bord+yshad+ex
klip='\\clip'..par({x_left,y_top,x_right,y_bottom})
klip_s='\\clip'..par({x_left,y_top,x_left+2,y_bottom})
klip_e='\\clip'..par({x_right-2,y_top,x_right,y_bottom})
text=text:gsub('\\i?clip%b()','')
if fadin==0 then klip_f=klip else klip_f=klip_s..'\\t(0,'..fadin..','..res.inn..','..klip..')' end
if fadout~=0 then klip_f=klip_f..'\\t('..dur-fadout..','..dur..','..res.utt..','..klip_e..')' end
text=addtag1(klip_f,text)
line.text=text
subs[i]=line
end
end
-- FADEWORKS ###############################################################################################
function fadeworks(subs,sel)
nsel={} for z,i in ipairs(sel) do table.insert(nsel,i) end
fadegui={
{x=1,y=0,class="label",label="Lines before start time"},
{x=2,y=0,class="label",label="Lines after end time"},
{x=0,y=1,class="label",label="Lines to create:"},
{x=1,y=1,class="intedit",name="LF",value=5,min=0},
{x=2,y=1,class="intedit",name="RF",value=5,min=0},
{x=0,y=2,class="label",label="Frames per line:"},
{x=1,y=2,class="intedit",name="LFram",value=1,min=1},
{x=2,y=2,class="intedit",name="RFram",value=1,min=1},
{x=0,y=3,class="label",label="Shift each line by:"},
{x=1,y=3,class="intedit",name="LFshift",value=1,min=1,hint="Should be same/lower than # of frames"},
{x=2,y=3,class="intedit",name="RFshift",value=1,min=1,hint="Should be same/lower than # of frames"},
{x=0,y=4,class="label",label="X distance:"},
{x=1,y=4,class="floatedit",name="LFX",value=0},
{x=2,y=4,class="floatedit",name="RFX",value=0},
{x=0,y=5,class="label",label="Y distance:"},
{x=1,y=5,class="floatedit",name="LFY"},
{x=2,y=5,class="floatedit",name="RFY"},
{x=0,y=6,class="label",label="X acceleration:"},
{x=1,y=6,class="floatedit",name="LFacx",value=1,min=0},
{x=2,y=6,class="floatedit",name="RFacx",value=1,min=0},
{x=0,y=7,class="label",label="Y acceleration:"},
{x=1,y=7,class="floatedit",name="LFacy",value=1,min=0},
{x=2,y=7,class="floatedit",name="RFacy",value=1,min=0},
{x=0,y=8,class="label",label="fbf transform*:"},
{x=1,y=8,class="edit",name="Lfbf",value="\\",hint="tags to transform FROM"},
{x=2,y=8,class="edit",name="Rfbf",value="\\",hint="tags to transform TO"},
{x=0,y=9,class="label",label="fbf alpha tf:"},
{x=1,y=9,class="edit",name="Lalf",hint="start alpha in hexadecimal"},
{x=2,y=9,class="edit",name="Ralf",hint="end alpha in hexadecimal"},
{x=0,y=10,class="label",label="\\t acceleration:"},
{x=1,y=10,class="floatedit",name="LFact",value=1,min=0},
{x=2,y=10,class="floatedit",name="RFact",value=1,min=0},
{x=0,y=11,class="label",label="Flickering:"},
{x=1,y=11,class="checkbox",name="LFad",label="Fade in"},
{x=2,y=11,class="checkbox",name="RFad",label="Fade out"},
{x=0,y=12,class="label",label=" * get colours:"},
{x=1,y=12,class="color",name="col"},
{x=2,y=12,class="edit",name="kol",hint="Paste the result to 'fbf transform'\n(modify the \\c type)"},
{x=0,y=13,class="label",label="Fade mode:"},
{x=1,y=13,width=2,class="checkbox",name="Fade",label="Use existing \\fad for line length and start/end point",hint="experimental feature intended to make overlapping lines"},
{x=0,y=14,class="label",label=" add transform:"},
{x=1,y=14,class="edit",name="LFtra",value="\\",hint="tags to transform FROM"},
{x=2,y=14,class="edit",name="RFtra",value="\\",hint="tags to transform TO"},
{x=3,y=3,class="label",label="frames"},
{x=3,y=4,class="label",label="pixels"},
{x=3,y=5,class="label",label="pixels"},
}
if resfade then
for k,v in ipairs(fadegui) do
if v.name then v.value=rez[v.name] end
end
end
pres=nil
repeat
if pres=="Save/Load" then
fwsafe()
if Pf=="Inhume" then
for k,v in ipairs(fadegui) do
if v.name then v.value=rez[v.name] end
end
end
end
if pres=="Get colour tag" then
K=rez.col:gsub("#(%x%x)(%x%x)(%x%x)","\\c&H%3%2%1&")
for k,v in ipairs(fadegui) do
if v.name=="kol" then v.value=K else v.value=rez[v.name] end
end
end
pres,rez=ADD(fadegui,{"OK","Save/Load","Get colour tag","Cancel"},{ok='OK',close='Cancel'})
until pres=="OK" or pres=="Cancel"
resfade=rez
if pres=="Cancel" then ak() end
if rez.Fade then rez.LFad=false rez.RFad=false end
-- fadeworks lines --
for z=#sel,1,-1 do
i=sel[z]
line=subs[i]
text=line.text
startf=ms2fr(line.start_time)
endf=ms2fr(line.end_time)
styleref=stylechk(subs,line.style)
if not text:match("\\pos%(") then text=getpos(subs,text) end
pX,pY=text:match("\\pos%(([^,]-),([^,]-)%)")
if rez.Lfbf:match 'fs[cp]' or rez.Rfbf:match 'fs[cp]' then text=text:gsub("^{\\","{\\q2\\") end
if rez.Fade then
f1,f2=text:match("\\fad%(([^,]-),([^,]-)%)")
if not f1 then t_error("Abort: No \\fad tag on line #"..i-line0,1) end
rez.LFram=ms2fr(f1)
rez.RFram=ms2fr(f2)
else
if rez.LFad then f1=fr2ms(rez.LFram) else f1=0 end
if rez.RFad then f2=fr2ms(rez.RFram) else f2=0 end
end
redshift=0
blushift=0
if not rez.Fade then
redshift=rez.LFram-rez.LFshift
blushift=rez.RFram-rez.RFshift
end
-- replicating OUT
if rez.RF>0 then
for r=rez.RF,1,-1 do
l2=line
posx=numgrad(pX,pX+rez.RFX,rez.RF+1,r+1,rez.RFacx)
posy=numgrad(pY,pY+rez.RFY,rez.RF+1,r+1,rez.RFacy)
text2=text:gsub("\\pos%b()","\\pos("..posx..","..posy..")"):gsub("\\t%b()","")
stags=text:match("^%b{}") or ""
-- save tags from transforms for OUT lines
tratags=""
for tra in stags:gmatch("\\t%b()") do
tTags=tra:match("\\t%(.-(\\.*)%)")
tratags=tratags..tTags
end
nontra=text2:gsub("\\t%b()","")
if rez.Fade and rez.RFtra:len()>1 then
text2=text2:gsub("^({\\[^}]-)}","%1\\t("..rez.RFtra..")}")
end
if rez.RFad then text2=text2:gsub("\\fad%b()",""):gsub("^{","{\\fad(0,"..f2..")") end
-- tags from orig. line's transforms to text2
for tag in tratags:gmatch("\\[^\\]+") do text2=addtag3(tag,text2) end
-- tags transforming line by line
if rez.Rfbf:len()>1 then
for tag in rez.Rfbf:gmatch("\\[^\\]+") do
tg,val=tag:match("(\\%d?%a+)([%d&%-][^\\}]*)")
-- from \t or from not \t or from style
endval=tratags:match(tg.."([%d&%-][^\\}]*)") or nontra:match(tg.."([%d&%-][^\\}]*)") or styleval(tg)
if tg=="\\c" or tg:match"%d" then
nval=acgrad(val,endval,rez.RF+1,rez.RF-r+1,1/rez.RFact)
else
nval=numgrad(val,endval,rez.RF+1,rez.RF-r+1,1/rez.RFact)
end
text2=addtag3(tg..nval,text2)
end
end
if rez.Ralf:len()>1 then
ralf=rez.Ralf:match("%x%x")
if ralf then
endval=nontra:match("alpha&H(%x%x)&") or "00"
nval=acgrad(ralf,endval,rez.RF+1,rez.RF-r+1,1/rez.RFact)
text2=addtag3("\\alpha"..nval,text2)
end
end
startf2=endf+rez.RFshift*(r-1)
endf2=startf2+rez.RFram
if rez.Fade then
text2=text2:gsub("\\fad%(([^,]+),([^,]+)%)","\\fad(0,%2)")
startf2=startf2-rez.RFram+rez.RFshift endf2=endf2-rez.RFram+rez.RFshift
end
l2.start_time=fr2ms(startf2-blushift)
l2.end_time=fr2ms(endf2-blushift)
l2.text=text2
subs.insert(i+1,l2)
nsel=shiftsel2(nsel,i,1)
end
end
-- main line
line.start_time=fr2ms(startf)
line.end_time=fr2ms(endf)
if rez.LFad or rez.RFad then text=text:gsub("\\fad%b()",""):gsub("^{","{\\fad("..f1..","..f2..")") end
line.text=text
subs.insert(i+1,line)
-- replicating IN
if rez.LF>0 then
for r=1,rez.LF do
l2=line
posx=numgrad(pX,pX+rez.LFX,rez.LF+1,r+1,rez.LFacx)
posy=numgrad(pY,pY+rez.LFY,rez.LF+1,r+1,rez.LFacy)
text2=text:gsub("\\pos%b()","\\pos("..posx..","..posy..")"):gsub("\\t%b()","")
nontra=text2:gsub("\\t%b()","")
if rez.Fade and rez.LFtra:len()>1 then
Ltra="{}"
for tag in rez.LFtra:gmatch("\\[^\\]+") do
tg=tag:match("(\\%d?%a+)[%d&%-]")
if nontra:match(tg.."[%d&%-]") then
Ltra=Ltra:gsub("{","{"..nontra:match(tg.."[%d&%-][^\\}]*"))
else Ltra=fill_in(Ltra,tg) end
text2=addtag3(tag,text2)
end
Ltra=Ltra:gsub("[{}]","")
text2=text2:gsub("^({\\[^}]-)}","%1\\t("..Ltra..")}")
end
if rez.LFad then text2=text2:gsub("\\fad%b()",""):gsub("^{","{\\fad("..f1..",0)") end
if rez.Lfbf:len()>1 then
for tag in rez.Lfbf:gmatch("\\[^\\]+") do
tg,val=tag:match("(\\%d?%a+)([%d&%-][^\\}]*)")
endval=nontra:match(tg.."([%d&%-][^\\}]*)") or styleval(tg)
endval=tostring(endval)
if tg=="\\c" or tg:match"%d" then
nval=acgrad(val,endval,rez.LF+1,rez.LF-r+1,1/rez.LFact)
else
nval=numgrad(val,endval,rez.LF+1,rez.LF-r+1,1/rez.LFact)
end
text2=addtag3(tg..nval,text2)
end
end
if rez.Lalf:len()>1 then
lalf=rez.Lalf:match("%x%x")
if lalf then
endval=nontra:match("alpha&H(%x%x)&") or "00"
nval=acgrad(lalf,endval,rez.LF+1,rez.LF-r+1,1/rez.LFact)
text2=addtag3("\\alpha"..nval,text2)
end
end
endf2=startf-rez.LFshift*(r-1)
startf2=endf2-rez.LFram
if rez.Fade then
text2=text2:gsub("\\fad%(([^,]+),([^,]+)%)","\\fad(%1,0)")
startf2=startf2+rez.LFram-rez.LFshift endf2=endf2+rez.LFram-rez.LFshift
end
l2.start_time=fr2ms(startf2+redshift)
l2.end_time=fr2ms(endf2+redshift)
l2.text=text2
subs.insert(i+1,l2)
nsel=shiftsel2(nsel,i,1)
end
end
subs.delete(i)
sel=nsel
end
return sel
end
-- reanimatools ----------------------------------------
function esc(str) str=str:gsub("[%%%(%)%[%]%.%-%+%*%?%^%$]","%%%1") return str end
function round(n,dec) dec=dec or 0 n=math.floor(n*10^dec+0.5)/10^dec return n end
function wrap(str) return "{"..str.."}" end
function par(tab) return '('..table.concat(tab,',')..')' end
function detra(t) return t:gsub("\\t%b()","") end
function nobra(t) return t:gsub("%b{}","") end
function nobrea(t) return t:gsub("%b{}",""):gsub("\\[Nh]","") end
function nobrea1(t) return t:gsub("%b{}",""):gsub(" *\\[Nh] *"," ") end
function tagmerge(t) repeat t,r=t:gsub("({\\[^}]-)}{(\\[^}]-})","%1%2") until r==0 return t end
function logg(m) m=tf(m) or "nil" aegisub.log("\n "..m) end
function loggtab(m) m=tf(m) or "nil" aegisub.log("\n {"..table.concat(m,';').."}") end
function progress(msg) if aegisub.progress.is_cancelled() then ak() end aegisub.progress.title(msg) end
function t_error(message,cancel) ADD({{class="label",label=message}},{"OK"},{close='OK'}) if cancel then ak() end end
function retextmod(orig,text)
local v1,v2,c,t2
v1=nobrea(orig)
c=0
repeat
t2=textmod(orig,text)
v2=nobrea(text)
c=c+1
until v1==v2 or c==666
if v1~=v2 then logg("Something went wrong with the text...") logg(v1) logg(v2) end
return t2
end
function textmod(orig,text)
if text=="" then return orig end
tk={}
tg={}
text=text:gsub("{\\\\k0}","")
text=tagmerge(text)
vis=nobra(text)
ltrmatches=re.find(vis,".")
if not ltrmatches then logg("text: "..text..'\nvisible: '..vis)
logg("If you're seeing this, something really weird is happening with the re module.\nTry this again or rescan Autoload.")
end
for l=1,#ltrmatches do
table.insert(tk,ltrmatches[l].str)
end
stags=text:match(STAG) or ""
text=text:gsub(STAG,"") :gsub("{[^\\}]-}","")
orig=orig:gsub("{([^\\}]+)}",function(c) return wrap("\\\\"..c.."|||") end)
count=0
for seq in orig:gmatch("[^{]-{%*?\\[^}]-}") do
chars,as,tak=seq:match("([^{]-){(%*?)(\\[^}]-)}")
pos=re.find(chars,".")
if pos==nil then ps=0+count else ps=#pos+count end