forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua.HYDRA.lua
2064 lines (1892 loc) · 85.9 KB
/
ua.HYDRA.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
-- Manual: http://unanimated.hostfree.pw/ts/scripts-manuals.htm#hydra
script_name="HYDRA"
script_description="A multi-headed typesetting beast. Scary as Hell."
script_author="unanimated"
script_url1="http://unanimated.hostfree.pw/ts/hydra.lua"
script_url2="https://raw.githubusercontent.com/unanimated/luaegisub/master/hydra.lua"
script_version="6.1"
script_namespace="ua.HYDRA"
local haveDepCtrl,DependencyControl,depRec=pcall(require,"l0.DependencyControl")
if haveDepCtrl then
script_version="6.1.0"
depRec=DependencyControl{feed="https://raw.githubusercontent.com/unanimated/luaegisub/master/DependencyControl.json"}
end
re=require'aegisub.re'
clipboard=require'aegisub.clipboard'
order="\\r\\fad\\fade\\an\\q\\blur\\be\\bord\\xbord\\ybord\\shad\\xshad\\yshad\\fn\\fs\\fsp\\fscx\\fscy\\frx\\fry\\frz\\fax\\fay\\c\\2c\\3c\\4c\\alpha\\1a\\2a\\3a\\4a\\pos\\move\\org\\clip\\iclip\\b\\i\\u\\s\\p"
noneg2="|bord|shad|xbord|ybord|fs|blur|be|fscx|fscy|"
-- HYDRA HEAD 9 --
function hh9(subs,sel)
-- get colours + tags from input
getcolours()
if res.italix or res.bolt or res.under or res.strike then styleget(subs) end
local shft=res.int or 0
tags=""
tags=gettags(tags)
transform=tags
ortags=tags
ortrans=transform
retags=tags:gsub("\\","\\\\")
z0=-1
-- tag position
pl1,pl2,pl3=nil,nil,nil
place=res.linetext or ""
if place=="*" then t_error("You cannot have only \"*\" in Tag position.",1) end
if place:match("*") then pl1,pl2,pl3=place:match("(.*)(%*)(.*)") pla=1 else pla=0 end
if res.tagpres~="--- presets ---" and res.tagpres~=nil then pla=1 end
for z,i in ipairs(sel) do
progress("Hydralizing line: "..z.."/"..#sel)
prog=math.floor((z+0.5)/#sel*100) aegisub.progress.set(prog)
line=subs[i]
text=line.text
visible=nobrea(text)
linecheck()
if not text:match("^{\\") then text="{\\HYDRA}"..text end
if visible:match("*") and res.tagpres=="--- presets ---" then pla=0 end
-- transforms
if trans==1 and GO then z0=z0+1
tin=res.trin or 0
tout=res.trout or 0
if res.tend then
tin=line.end_time-line.start_time-res.trin
tout=line.end_time-line.start_time-res.trout
end
if tmode==1 then
if res.int~=0 then TF=shft*z0 else TF=0 end
tnorm="\\t("..tin+TF..","..tout+TF..","..res.accel..",\\alltagsgohere)}"
if place:match("*") and pla==1 then
initags=text:match("^{\\[^}]-}") or ""
orig=text
replace=place:gsub("%*","{"..tnorm)
v1=nobra(text)
v2=nobra(replace)
if v1==v2 then text=initags..textmod(orig,replace) end
else
text=text:gsub("^({\\[^}]*)}","%1"..tnorm)
end
end
if tmode==2 then text=text:gsub("^(.-\\t%b())",function(t) return t:gsub("%)$","\\alltagsgohere)") end) end
if tmode==3 then text=text:gsub("(\\t%b())",function(t) return t:gsub("%)$","\\alltagsgohere)") end) end
if tmode==4 then
if res.int~=0 then
tagtab={}
for tt in text:gmatch(".-{\\[^}]*}") do table.insert(tagtab,tt) end
END=text:match("^.*{\\[^}]*}(.-)$")
for t=1,#tagtab do sf=t-1
tagtab[t]=tagtab[t]:gsub("({\\[^}]*)}","%1\\t("..tin+shft*sf..","..tout+shft*sf..","..res.accel..",\\alltagsgohere)}")
end
nt=END
for a=#tagtab,1,-1 do nt=tagtab[a]..nt end
text=nt
else text=text:gsub("({\\[^}]*)}","%1\\t("..tin..","..tout..","..res.accel..",\\alltagsgohere)}")
end
end
if res.add and res.add~=0 then transform=addbyline(transform,ortrans) end
if tmode<4 and res.relative then
stags=text:match(STAG) or ""
for tag,val in transform:gmatch("(\\%a+)([%d%.%-]+)") do
if stags:match(tag) then oldval=stags:match(tag.."([%d%.%-]+)")
transform=transform:gsub(tag..esc(val),tag..oldval+val)
end
end
end
text=text:gsub("\\alltagsgohere",transform)
if tmode==4 and res.relative then
text=text:gsub(ATAG,function(tg)
for tag,val in transform:gmatch("(\\%a+)([%d%.%-]+)") do
if tg:match(tag) then oldval=tg:match(tag.."([%d%.%-]+)")
transform2=transform:gsub(tag..esc(val),tag..oldval+val)
tg=tg:gsub("(.*\\t.-)"..transform,"%1"..transform2)
end
end
return tg end)
end
text=text:gsub("\\t%(0,0,1,","\\t(")
:gsub("\\t(%b())",function(tr) return "\\t"..duplikill(tr) end)
:gsub(ATAG,function(tg) return cleantr(tg) end)
-- non transform, ie. the regular stuff
elseif GO then z0=z0+1
-- temporarily block transforms
text=text:gsub(ATAG,function(tg) return duplikill(tg) end)
:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end)
if res.add and res.add~=0 then tags=addbyline(tags,ortags) end
if pla==1 and z==1 then
if res.strike then tags=tags.."\\s1" end
if res.under then tags=tags.."\\u1" end
if res.bolt then tags=tags.."\\b1" end
if res.italix then tags=tags.."\\i1" end
end
if tags~="" then
drawing=text:match("\\p1")
if drawing then fail("Some lines contain drawings.") end
if visible=="" then fail("No visible text.") end
if pla==1 and not drawing and visible~="" then
initags=text:match(STAG) or ""
text=text:gsub("(\\[Nh])","{%1}")
orig=text
v1=nobra(orig)
-- BEFORE LAST CHARACTER
if res.tagpres=="before last char." then
com_dump=''
repeat
end_com=text:match("(%b{})$")
if end_com then
com_dump=end_com..com_dump
text=text:gsub("%b{}$","")
else break end
until not end_com
text=re.sub(text,"(.)$","{§}\\1")
text=text:gsub("{§}",wrap(tags))..com_dump
-- SOMEWHERE IN THE MIDDLE
elseif res.tagpres=="in the middle" or res.tagpres:match("of text") then
clean=nobrea1(text)
char=re.find(clean,'.')
lngth=math.floor(#char*fak)
text="{·}"..text
text=text:gsub("{·}({\\[^}]-})","%1{·}")
m=0
if lngth>0 then
repeat text=text:gsub("{·}(%b{})","%1{·}") text=re.sub(text,"{·}([^{])","\\1{·}") m=m+1 until m==lngth
end
text=text:gsub("{(·)}",wrap(tags)) :gsub("({"..esc(tags).."})(%b{})","%2%1")
-- PATTERN
elseif res.tagpres=="custom pattern" then
if place=="" then t_error("Custom pattern preset: No text given in Tag position.",1) end
if not pl1 then t_error("Asterisk missing in Tag position. ("..place..")",1) end
pl1=esc(pl1) pl3=esc(pl3)
text=nobrea(text):gsub(pl1..pl3,pl1.."{"..tags.."}"..pl3)
text=initags..retextmod(orig,text)
if text==orig then fail("Pattern '"..place.."' not found.") end
-- SECTION
elseif res.tagpres=="section" then
if place=="" then t_error("Section preset: No text given in Tag position.",1) end
tgs2=""
for tg in tags:gmatch("\\%d?%a+") do
txt1=text:match("^%b{}.-"..esc(place)) or text:match("^.-"..esc(place)) or ""
local tg2=txt1:match("^.*("..tg.."[^\\}]+).-$") or tg
if tg=='\\fs' then tg2=txt1:match("^.*(\\fs%d+).-$") or tg end
tg2=tg2:gsub("(\\[ibus])%d","%10")
tgs2=tgs2..tg2
end
text=nobrea(text):gsub("^(.-)("..esc(place).."%s*)(.*)$","%1{"..tags.."}%2{"..tgs2.."}%3")
text=initags..retextmod(orig,text)
if text==orig then fail("Pattern '"..place.."' not found.") end
-- CHARACTER
elseif res.tagpres=="every char." then
replace=re.sub(nobra(text),"(.)","{"..retags.."}\\1")
v2=nobra(replace)
if visible=="" then fail("No visible text.")
elseif v1==v2 then text=initags..retextmod(orig,replace) end
text=text:gsub("({\\HYDRA})%1","%1")
-- WORD
elseif res.tagpres=="every word" then
replace=nobra(text):gsub("%S+","{"..tags.."}%1"):gsub("(%b{})\\N","\\N%1")
v2=nobra(replace)
if v1==v2 then text=initags..retextmod(orig,replace) end
text=text:gsub("({\\HYDRA})%1","%1")
-- TEXT POSITION
elseif res.tagpres=="text position" then
v2=nobra(text)
pmax=re.find(v2,".") or {}
krktrz=#pmax
pos=tonumber(place:match("^%-?%d+")) or 0
addpos=tonumber(place:match(".([%+%-]%d+)")) or 0
if pos<0 then pos=krktrz+pos end
split=pos+addpos*z0
if split<0 then split=0 end
if split<krktrz then
be4=re.sub(v2,"^(.{"..split.."}).*","\\1")
aft=re.sub(v2,"^.{"..split.."}","")
text=be4..wrap(tags)..aft
if v1==v2 then text=initags..retextmod(orig,text) end
end
if text==orig then fail("Line has fewer than "..place.." visible characters.") end
-- REPLACE BELL
elseif res.tagpres=="replace {•}" then
repeat text,r=text:gsub("{•}({[^}]*})","%1{•}") until r==0
text,r=text:gsub("{•}",wrap(tags))
if r==0 then fail("No {•} in text.") end
-- REPLACE WAVE
elseif res.tagpres=="replace {~}" then
repeat text,r=text:gsub("{~}({[^}]*})","%1{~}") until r==0
text,r=text:gsub("{~}",wrap(tags))
if r==0 then fail("No {~} in text.") end
-- REPLACE LINE BREAK
elseif res.tagpres=="replace \\N" then
text,r=text:gsub("\\N",tags)
if r==0 then fail("No \\N in text.") end
-- AT ASTERISK POINT
else
replace=place:gsub("%*",wrap(tags)):gsub("\\N","")
v2=nobra(replace)
if v1==v2 then text=initags..retextmod(orig,replace) else fail("Different text.") end
end
text=text:gsub("{(\\[Nh])}","%1")
text=tagmerge(text)
elseif pla==0 then
-- REGULAR START TAGS
for t in tags:gmatch("\\%d?%a[^\\]*") do text=addtag3(t,text) end
text=text:gsub(STAG,function(a) repeat a,r=a:gsub("(\\[1234]a%b&&)(.-)(\\alpha%b&&)","%2%3%1") until r==0 return a end)
end
text=text:gsub(ATAG,function(tg) return duplikill(tg) end)
repeat text,r=text:gsub("{([^}]-)(\\[Nh])([^}]-)}","%2{%1%3}") until r==0
text=text:gsub("{}","")
end
-- non-transformable tags
if pla==0 then
-- strikeout/underline/bold/italics
if res.strike then text=bolts(text,"\\s","strikeout") end
if res.under then text=bolts(text,"\\u","underline") end
if res.bolt then text=bolts(text,"\\b","bold") end
if res.italix then text=bolts(text,"\\i","italic") end
-- \fad
if res.fade then
IN=res.fadin OUT=res.fadout fGO=1
if res.glo then
if z<#sel then OUT=0 end
if z>1 then IN=0 end
if IN==0 and OUT==0 then fGO=0 end
end
text=text:gsub("\\fad%([%d%.%,]-%)","")
if fGO==1 then text=text:gsub("^{\\","{\\fad("..IN..","..OUT..")\\") end
end
-- \q2
if res.q2 then
if text:match("\\q2") then text=text:gsub("\\q2","") else text=text:gsub("^{\\","{\\q2\\") end
end
-- \an
if res.an1 then
if text:match("\\an%d") then text=text:gsub("\\an(%d)","\\an"..res.an2) else text=text:gsub("^{\\","{\\an"..res.an2.."\\") end
end
if text==line.text then fail("Most likely tags already present.") end
end
-- unblock transforms
text=text:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
end
-- the end
text=text:gsub("\\HYDRA","") :gsub("\\t%([^\\%)]-%)","") :gsub("{}","")
if line.text~=text then success=success+1 end
line.text=text
subs[i]=line
end
summary()
end
function bolts(text,ttype,srtype)
if text:match("^{[^}]-"..ttype.."[01]") then text=text:gsub(ttype.."([01])",function(a) return ttype..(1-a) end)
else
local t_val=text:match(ttype.."([01])")
if not t_val then
if stylechk(sty)[srtype] then t_val="0" else t_val="1" end
end
text=text:gsub(ttype.."([01])",function(a) return ttype..(1-a) end) :gsub("^({\\[^}]*)}","%1"..ttype..t_val.."}")
end
return text
end
function vis_replace(t,r1,r2)
local nt=''
repeat
seg,t2=t:match("^(%b{})(.*)") --tags/comms
if not seg then seg,t2=t:match("^([^{]+)(.*)") --text
if not seg then break end
seg=seg:gsub(r1,r2)
end
nt=nt..seg
t=t2
until t==''
return nt
end
function getcolours()
col={} alfalfa={}
for c=1,4 do
local colur=res["c"..c]:gsub("#(%x%x)(%x%x)(%x%x).*","&H%3%2%1&")
table.insert(col,colur)
if res.alfas then
local alpa=res["c"..c]:match("#%x%x%x%x%x%x(%x%x)")
if alpa then
table.insert(alfalfa,alpa)
if res["k"..c] then res["arf"..c]=true res["alph"..c]=alfalfa[c] end
end
end
if res.aonly then res["k"..c]=false end
end
end
function gettags(tags)
if res.reuse then
if lastags then
if res.show then showtags(lastags) end
return lastags
else t_error("No tags to reuse",1) end
end
if res.fsc1 then res.fscx1=true res.fscy1=true res.fscx2=res.fsc2 res.fscy2=res.fsc2 end
if res["blur1"] then tags=tags.."\\blur"..res["blur2"] end
if res["be1"] then tags=tags.."\\be"..res["be2"] end
if res["bord1"] then tags=tags.."\\bord"..res["bord2"] end
if res["shad1"] then tags=tags.."\\shad"..res["shad2"] end
if res["fs1"] then tags=tags.."\\fs"..res["fs2"] end
if res["spac1"] then tags=tags.."\\fsp"..res["spac2"] end
if res["fscx1"] then tags=tags.."\\fscx"..res["fscx2"] end
if res["fscy1"] then tags=tags.."\\fscy"..res["fscy2"] end
if res["xbord1"] then tags=tags.."\\xbord"..res["xbord2"] end
if res["ybord1"] then tags=tags.."\\ybord"..res["ybord2"] end
if res["xshad1"] then tags=tags.."\\xshad"..res["xshad2"] end
if res["yshad1"] then tags=tags.."\\yshad"..res["yshad2"] end
if res["frz1"] then tags=tags.."\\frz"..res["frz2"] end
if res["frx1"] then tags=tags.."\\frx"..res["frx2"] end
if res["fry1"] then tags=tags.."\\fry"..res["fry2"] end
if res["fax1"] then tags=tags.."\\fax"..res["fax2"] end
if res["fay1"] then tags=tags.."\\fay"..res["fay2"] end
if res["k1"] then tags=tags.."\\c"..col[1] end
if res["k2"] then tags=tags.."\\2c"..col[2] end
if res["k3"] then tags=tags.."\\3c"..col[3] end
if res["k4"] then tags=tags.."\\4c"..col[4] end
if res["arfa"] then tags=tags.."\\alpha&H"..res["alpha"].."&" end
if res["arf1"] then tags=tags.."\\1a&H"..res["alph1"].."&" end
if res["arf2"] then tags=tags.."\\2a&H"..res["alph2"].."&" end
if res["arf3"] then tags=tags.."\\3a&H"..res["alph3"].."&" end
if res["arf4"] then tags=tags.."\\4a&H"..res["alph4"].."&" end
lastags=tags
if res.show then showtags(tags) end
if res["moretags"] and res["moretags"]~="\\" then tags=tags..res["moretags"] end
return tags
end
function addbyline(tags,ortags)
tags=ortags:gsub("\\(%a%a+)([%d%.%-]+)",function(t,v)
if t~="an" and t~="fn" then
local nv=round(v+res.add*z0,2)
if nv<0 and noneg2:match("|"..t.."|") then nv=0 end
return "\\"..t..nv
else return "\\"..t..v end
end)
return tags
end
function linecheck()
lay=line.layer sty=line.style act=line.actor eff=line.effect
if not res.appltx then res.appltx="" end
GO=nil local lGO,sGO,aGO,eGO,tGO
if res.applay=="All Layers" or not res.exc and tonumber(res.applay)==lay or res.exc and tonumber(res.applay)~=lay then lGO=true end
if res.applst=="All Styles" or not res.exc and res.applst==sty or res.exc and res.applst~=sty then sGO=true end
if res.applac=="All Actors" or not res.exc and res.applac==act or res.exc and res.applac~=act then aGO=true end
if res.applef=="All Effects" or not res.exc and res.applef==eff or res.exc and res.applef~=eff then eGO=true end
if res.appltx=="Text..." or res.appltx=="" or line.text:match(esc(res.appltx)) then tGO=true end
if lGO and sGO and aGO and eGO and tGO then GO=true end
if loaded<3 then GO=true end
if not GO then fail("Some 'Apply to' restrictions set.") end
end
-- GRADIENTS --
function hydradient(subs,sel)
local GT=res.gtype:match("^....")
local strip=res.stripe
local acc=res.accel
styleget(subs)
getcolours()
tags=""
tags=gettags(tags)
if tags=="" then ak() end
ortags=tags
local gcpos=res.linetext gcl=nil
if res.middle and gcpos:match("*") then gc1,gc2=gcpos:match("^(.-)%*(.-)$") gcl=gc1:len() end
local GBCn=tonumber(gcpos:match("^%d$")) or 1
text1=subs[sel[1]].text
tags_1=text1:match(STAG) or ""
tags_1=detra(tags_1)
if GT=="by l" then GBL=0 z1=0
for z,i in ipairs(sel) do line=subs[i] linecheck() if GO then GBL=GBL+1 end end
-- GBL: values from last line
if res.last then
local l=subs[sel[#sel]]
local st=l.text:match(STAG) or ""
for tg in tags:gmatch("\\[^\\]+") do
local tag=tg:match '\\%d?%a+'
local tv=st:match(tag.."([^\\})]+)")
if tv then tg2=tag..tv tags=tags:gsub(esc(tg),tg2) end
end
end
table.sort(sel,function(a,b) return a>b end)
end
local bra={}
for z=#sel,1,-1 do
i=sel[z]
progress("Gradienting line #"..i-line0.." ["..#sel+1-z.."/"..#sel.."]")
line=subs[i]
text=line.text
orig=text
visible=nobrea(text)
text=text:gsub("\\t(%b())",function(t) return "\\tra"..t:gsub("\\","/") end) :gsub("\\1c","\\c")
initags=text:match(STAG) or ""
sr=stylechk(line.style)
linecheck()
-- hori/vert
if GO and GT:match("r") then
x1,y1,x2,y2=initags:match("clip%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)")
if not x1 then
local note=''
if #sel>1 then note='\n(Note: Lines are processed from last to first.)' end
t_error(res.gtype.." gradient:\nMissing rectangular clip on line #"..i-line0..note.."\nAborting.",1)
end
x1=math.floor(x1) y1=math.floor(y1) x2=math.ceil(x2) y2=math.ceil(y2) local c_s
if GT=="vert" then total=math.ceil((y2-y1)/strip) c_s=y2-y1 else total=math.ceil((x2-x1)/strip) c_s=x2-x1 end
if total<2 then t_error("Error on line #"..i-line0..".\nThis won't create any gradient.\nDecrease the pxl/stripe setting.\nStripe: "..strip.."; Clip size: "..c_s.."\nAborting.",1) end
if not initags:match("\\pos") and not initags:match("\\move") then initags=getpos(subs,initags) end
for l=1,total do
LN=l count=total
half=math.ceil(total/2)
if res.middle then count=half
if LN>half then LN=total-LN+1 end
end
stags=initags
text2=text
for tg,V2 in tags:gmatch("(\\%d?%a+)([^\\]+)") do
if tg:match("fr") and res.short then V2=shortrot(V2) end
V1=initags:match("^{[^}]-"..tg.."([%d%-&][^\\}]*)") or tag2style(tg,sr)
if tg:match("fr") and res.short then V1=shortrot(V1) end
if tg:match("\\[fbsxy]") then VC=numgrad(V1,V2,count,LN,acc) end
if tg:match("\\%d?a") then VC=agrad(V1,V2,count,LN,acc) end
if tg:match("\\%d?c") then
if res.hsl then VC=acgradhsl(V1,V2,count,LN,acc) else VC=acgrad(V1,V2,count,LN,acc) end
end
stags=addtag3(tg..VC,stags)
stags=stags:gsub("clip%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)",function(a,b,c,d)
if GT=="vert" then b=y1+(l-1)*strip d=b+strip a=x1 c=x2 end
if GT=="hori" then a=x1+(l-1)*strip c=a+strip b=y1 d=y2 end
return "clip("..a..","..b..","..c..","..d end)
text2=text2:gsub("(.)("..ATAG..")",function(a,tblok)
V1i=tblok:match("^{[^}]-"..tg.."([%d%-&][^\\}]*)")
if V1i and tg:match("\\[fbsxy]") then VC=numgrad(V1i,V2,count,LN,acc) end
if V1i and tg:match("\\%d?a") then VC=agrad(V1i,V2,count,LN,acc) end
if V1i and tg:match("\\%d?c") then
if res.hsl then VC=acgradhsl(V1i,V2,count,LN,acc) else VC=acgrad(V1i,V2,count,LN,acc) end
end
tblok=addtag3(tg..VC,tblok)
return a..tblok end)
end
l2=line
l2.text=text2:gsub(STAG,stags) :gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
if l==1 then text=l2.text
else subs.insert(i+l-1,l2) end
end
if z<#sel then for s=z+1,#sel do sel[s]=sel[s]+total-1 end end
for s=1,total-1 do table.insert(sel,i+s) end
end
-- by character
letrz0=re.find(visible,".") or {}
if GT=="by c" and text:match '\\p1' then GO=nil fail("Some lines contain drawings.") end
if GT=="by c" and GBCn>#letrz0 then GO=nil end
if GO and GT=="by c" and #letrz0>1 then
orig=orig:gsub("(\\[Nh])","{%1}")
if text:match "{[^}]*{" or text:match "}[^{]*}" or text:match "^[^{]*}" or text:match "{[^}]*$" then brackets=true table.insert(bra,1,i-line0) end
re_check=0
repeat
LTR={}
TAG={}
letrz=re.find(visible,".{"..GBCn.."}")
rest=re.sub(visible,".{"..GBCn.."}","")
for l=1,#letrz do
table.insert(LTR,letrz[l].str)
table.insert(TAG,"")
end
if rest~="" then table.insert(LTR,rest) table.insert(TAG,"") end
for tg,V2 in tags:gmatch("(\\%d?%a+)([^\\]+)") do
if tg:match("fr") and res.short then V2=shortrot(V2) end
V1=text:match("^{[^}]-"..tg.."([%d%-&][^\\}]*)") or tag2style(tg,sr)
if tg:match("fr") and res.short then V1=shortrot(V1) end
initags=addtag3(tg..V1,initags)
for l=2,#LTR do
LN=l count=#LTR
half=math.ceil(#LTR/2)
if gcl and gc1..gc2==visible then
if l<=gcl then count=gcl else count=#LTR-gcl LN=#LTR-l+1 end
elseif res.middle then count=half
if LN>half then LN=#LTR-LN+1 end
end
if tg:match("\\[fbsxy]") then VC=numgrad(V1,V2,count,LN,acc) end
if tg:match("\\%d?a") then VC=agrad(V1,V2,count,LN,acc) end
if tg:match("\\%d?c") then
if res.hsl then VC=acgradhsl(V1,V2,count,LN,acc) else VC=acgrad(V1,V2,count,LN,acc) end
end
TAG[l]=TAG[l]..tg..VC
end
end
nt=LTR[1]
for l=2,#LTR do nt=nt.."{"..TAG[l].."}"..LTR[l] end
text=initags..textmod(orig,nt)
text=text:gsub(ATAG,function(tg) return duplikill(tg) end)
repeat text,r=text:gsub("{([^}]-)(\\[Nh])([^}]-)}","%2{%1%3}") until r==0
text=text:gsub("{}","")
visible2=nobrea(text)
if visible~=visible2 then re_check=re_check+1 end
until visible==visible2 or re_check>=100
end
-- by line
if GO and GT=="by l" then z1=z1+1
LN=z1 total=GBL count=GBL
half=math.ceil(total/2)
if res.middle then count=half
if LN>half then LN=total-LN+1 end
end
stags=initags
for tg,V2 in tags:gmatch("(\\%d?%a+)([^\\]+)") do
if tg:match("fr") and res.short then V2=shortrot(V2) end
V1=tags_1:match("^{[^}]-"..tg.."([%d%-&][^\\}]*)") or tag2style(tg,sr)
if tg:match("fr") and res.short then V1=shortrot(V1) end
if tg:match("\\[fbsxy]") then VC=numgrad(V1,V2,count,LN,acc) end
if tg:match("\\%d?a") then VC=agrad(V1,V2,count,LN,acc) end
if tg:match("\\%d?c") then
if res.hsl then VC=acgradhsl(V1,V2,count,LN,acc) else VC=acgrad(V1,V2,count,LN,acc) end
end
stags=addtag3(tg..VC,stags)
end
text=text:gsub(STAG,stags) :gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
if line.text==text then fail("Target values probably already present.") end
end
text=text:gsub("\\tra(%b())",function(t) return "\\t"..t:gsub("/","\\") end)
visible2=nobrea(text)
txt_check(visible,visible2,i)
if line.text~=text and visible==visible2 then success=success+1 end
line.text=text
subs[i]=line
end
if brackets then
local l=''
for k,v in ipairs(bra) do l=l..v..', ' end
l=l:gsub(', $','')
t_error("Some lines contain wrong sets of curly brackets. \nThis probably won't go well with gradients. \nLines # "..l)
brackets=nil
end
summary()
return sel
end
-- SPECIAL FUNCTIONS --
function special(subs,sel)
SF=res.spec
if res.spec=="back and forth transform" and res.int==0 then
BAFT={{class="label",label="Interval for back and forth transform missing.\nGive milliseconds."},
{y=1,name="int2",class="intedit",min=0}}
pres,rez=ADD(BAFT,{"OK","Cancel"},{ok='OK',close='Cancel'})
if pres=="Cancel" or rez.int2==0 then ak() end
res.int=rez.int2
end
if res.spec:match"transform" or res.spec:match"strikeout" then
getcolours()
transphorm=""
transphorm=gettags(transphorm)
end
if res.spec=="split line in 3 parts" then
if res.trin==0 and res.trout==0 then t_error("No times given to split lines by.\nUse Transform t1 && t2 fields.",1) end
nsel={} for z,i in ipairs(sel) do table.insert(nsel,i) end
end
styleget(subs)
if res.spec=="select overlaps" then sel=selover(subs)
else
for z=#sel,1,-1 do
i=sel[z]
progress(res.spec..": "..#sel-z.."/"..#sel)
prog=math.floor((#sel-z+0.5)/#sel*100)
aegisub.progress.set(prog)
line=subs[i]
text=line.text
layer=line.layer
linecheck()
if GO then res.spec=SF else res.spec="nope" end
text=text:gsub("\\1c","\\c")
if res.spec=="fscx -> fscy" then text=text:gsub("\\fscy[%d%.]+",""):gsub("\\fscx([%d%.]+)","\\fscx%1\\fscy%1") end
if res.spec=="fscy -> fscx" then text=text:gsub("\\fscx[%d%.]+",""):gsub("\\fscy([%d%.]+)","\\fscx%1\\fscy%1") end
if res.spec=="shad -> xshad+yshad" then
text=text:gsub("\\shad([%d%.]+)","\\xshad%1\\yshad%1"):gsub(ATAG,function(tg) return duplikill(tg) end)
if text==line.text then fail("No \\shad tag.") end
end
if res.spec=="move colour tag to first block" then
tags=text:match(STAG) or ""
text=text:gsub(STAG,"")
klrs=""
for klr in text:gmatch("\\[1234]?c&H%x+&") do klrs=klrs..klr end
text=text:gsub("(\\[1234]?c&H%x+&)","") :gsub("{}","")
text=tags.."{"..klrs.."}"..text
text=tagmerge(text):gsub("{}","")
:gsub(ATAG,function(tg) return duplikill(tg) end)
end
if res.spec=="convert clip <-> iclip" then
text=text:gsub("\\(i?)clip",function(k) if k=="" then return "\\iclip" else return "\\clip" end end)
if text==line.text then fail("No (i)clip found.") end
end
-- CLEAN UP TAGS
if res.spec=="clean up tags" then
text=text:gsub("{\\\\k0}",""):gsub(">\\","\\"):gsub("{(\\[^}]-)} *\\N *{(\\[^}]-)}","\\N{%1%2}")
text=tagmerge(text)
text=text:gsub("({\\[^}]-){(\\[^}]-})","%1%2"):gsub("{.-\\r","{\\r"):gsub("^{\\r([\\}])","{%1")
text=text:gsub("\\fad%(0,0%)",""):gsub(ATAG.."$",""):gsub("^({\\[^}]-)\\frx0\\fry0","%1")
text=text:gsub(ATAG,function(tgs)
tgs2=tgs
:gsub("\\+([\\}])","%1")
:gsub("(\\[^\\})]+)",function(a) if not a:match'clip' and not a:match'\\fn' and not a:match'\\r' then a=a:gsub(' ','') end return a end)
:gsub("(\\%a+)([%d%-]+%.%d+)",function(a,b) if not a:match("\\fn") then b=round(b,2) end return a..b end)
:gsub("(\\%a+)%(([%d%.%-]+),([%d%.%-]+)%)",function(a,b,c) b=round(b,2) c=round(c,2) return a.."("..b..","..c..")" end)
:gsub("(\\%a+)%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)",function(a,b,c,d,e)
b=round(b,2) c=round(c,2) d=round(d,2) e=round(e,2) return a.."("..b..","..c..","..d..","..e end)
tgs2=duplikill(tgs2)
tgs2=extrakill(tgs2)
tgs2=cleantr(tgs2)
return tgs2
end)
end
-- SORT TAGS
if res.spec=="sort tags in set order" then
text=text:gsub("\\a6","\\an8") :gsub("\\1c","\\c")
-- run for each set of tags
for tags in text:gmatch(ATAG) do
orig=tags
tags=tags:gsub("{.-\\r","{\\r")
-- save & nuke transforms
trnsfrm=""
for t in tags:gmatch("\\t%b()") do trnsfrm=trnsfrm..t end
tags=tags:gsub("\\t%b()","")
ord=""
-- go through tags, save them in order, and delete from tags
for tg in order:gmatch("\\[%a%d]+") do
tag=tags:match("("..tg.."[^\\}]-)[\\}]")
if tg=="\\fs" then tag=tags:match("(\\fs%d[^\\}]-)[\\}]") end
if tg=="\\fad" then tag=tags:match("(\\fad%([^\\}]-)[\\}]") end
if tg=="\\c" then tag=tags:match("(\\c&[^\\}]-)[\\}]") end
if tg=="\\i" then tag=tags:match("(\\i[^%a\\}]-)[\\}]") end
if tg=="\\s" then tag=tags:match("(\\s[^%a\\}]-)[\\}]") end
if tg=="\\p" then tag=tags:match("(\\p[^%a\\}]-)[\\}]") end
if tag then ord=ord..tag etag=esc(tag) tags=tags:gsub(etag,"") end
end
-- attach whatever got left
if tags~="{}" then ord=ord..tags:match("{(.-)}") end
ordered="{"..ord..trnsfrm.."}"
text=text:gsub(esc(orig),ordered)
end
end
-- CLIP TO DRAWING
if res.spec=="convert clip to drawing" and text:match("\\i?clip%(m [%d%a%s%-]+") then
text=text:gsub("\\(i?)clip%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)%)",function(ii,a,b,c,d)
return string.format("\\"..ii.."clip(m %d %d l %d %d %d %d %d %d)",round(a),round(b),round(c),round(b),round(c),round(d),round(a),round(d)) end) -- rect.2vector
text=text:gsub("^({\\[^}]-}).*","%1")
text=text:gsub("^({[^}]*)\\i?clip%(m(.-)%)([^}]*)}","%1%3\\p1}m%2")
if text:match("\\pos") or text:match("\\move") then
ctext=text:match("}m ([%d%a%s%-]+)")
local xx,yy=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)")
if not xx then xx,yy=text:match("\\move%(([%d%.%-]+),([%d%.%-]+)") end
xx=round(xx) yy=round(yy)
ctext2=ctext:gsub("([%d%-]+)%s([%d%-]+)",function(a,b) return a-xx.." "..b-yy end)
ctext=ctext:gsub("%-","%%-")
text=text:gsub(ctext,ctext2)
end
if not text:match("\\pos") and not text:match("\\move") then text=text:gsub("^{","{\\pos(0,0)") end
text=text:gsub("\\fsc[xy][%d%.]+",""):gsub("\\f[ar][xyz][^\\}]*",""):gsub("\\p1","\\fscx100\\fscy100%1"):gsub("\\an%d",""):gsub("^{","{\\an7")
end
-- DRAWING TO CLIP
if res.spec=="convert drawing to clip" and text:match("\\p1") then
draw=text:match("}m ([^{]+)")
rota=text:match("^{[^}]-\\frz([-%d.]+)")
if rota then sr=stylechk(line.style) text=frz_redraw(text,rota,draw,sr) end
text=text:gsub("^({[^}]*)\\p1([^}]-})(m [^{]*)","%1\\clip(%3)%2")
scx=text:match("\\fscx([%d%.]+)") or 100
scy=text:match("\\fscy([%d%.]+)") or 100
if text:match("\\pos") or text:match("\\move") then
local xx,yy=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)")
if not xx then xx,yy=text:match("\\move%(([%d%.%-]+),([%d%.%-]+)") end
xx=round(xx) yy=round(yy)
ctext=text:match("\\clip%(m ([^%)]+)%)")
ctext2=ctext:gsub("([%d%-]+) ([%d%-]+)",function(a,b) return round(a*scx/100+xx).." "..round(b*scy/100+yy) end)
text=text:gsub(esc(ctext),ctext2)
end
if not text:match("\\pos") and not text:match("\\move") then text=text:gsub("^{","{\\pos(0,0)") end
end
-- STRIKEOUT TO SELECTED
if res.spec=="convert strikeout to selected" then
selcheck()
ST1=transphorm ST2=transphorm:gsub("(\\%d?%a+)[^\\]+","%1")
text=text:gsub("\\s1",ST1):gsub("\\s0",ST2)
if text==line.text then fail("No \\s1 tag.") end
text=text:gsub(ATAG,function(tg) return duplikill(tg) end)
end
-- CREATE SHADOW FROM CLIP
if res.spec=="create shadow from clip" then
local KX1,KY1,KX2,KY2=text:match("\\i?clip%(m (%-?[%d%.]+) (%-?[%d%.]+) l (%-?[%d%.]+) (%-?[%d%.]+)")
if not KX1 then t_error("Line #"..i-line0..": Vectorial clip not detected.\nUse two points of a clip to set shadow direction.)",1) end
sr=stylechk(line.style)
stag=text:match(STAG) or "{}"
sha=stag:match("\\shad([%d%.]+)")
if not sha then
sx=stag:match("\\xshad%-?([%d%.]+)")
sy=stag:match("\\yshad%-?([%d%.]+)")
if sx and sy then sha=math.sqrt((sx^2+sy^2)/2) end
if not sha then sha=sr.shadow end
end
if tonumber(sha)==0 then t_error("Line #"..i-line0..": Shadow seems to be 0. Setting to 4.\n(It is preferable to set a \\shad value first.)") sha=4 end
eks=KX2-KX1
wai=KY2-KY1
pyth=math.sqrt(eks^2+wai^2)
shratio=pyth/math.sqrt(2*sha^2)
shX=round(eks/shratio,1)
shY=round(wai/shratio,1)
stag=stag:gsub("\\.?shad([%d%.]+)",""):gsub("\\i?clip%b()",""):gsub("}","\\xshad"..shX.."\\yshad"..shY.."}")
text=stag..text:gsub(STAG,"")
end
-- 3D SHADOW
if res.spec=="create 3D effect from shadow" then
if not text:match("\\[xy]shad") then
text,c=text:gsub("\\shad([%d.]+)","\\xshad%1\\yshad%1")
if c==0 then
sr=stylechk(line.style)
text="{\\xshad"..sr.shadow.."\\yshad"..sr.shadow.."}"..text
text=text:gsub("^({.*)}{","%1")
end
end
xshad=tonumber(text:match("^{[^}]-\\xshad([%d%.%-]+)")) or 0 ax=math.abs(xshad)
yshad=tonumber(text:match("^{[^}]-\\yshad([%d%.%-]+)")) or 0 ay=math.abs(yshad)
if ax>ay then lay=math.floor(ax) else lay=math.floor(ay) end
text2=text:gsub("^({\\[^}]-)}","%1\\3a&HFF&}") :gsub("\\3a&H%x%x&([^}]-)(\\3a&H%x%x&)","%1%2")
for l=lay,1,-1 do
line2=line f=l/lay
text2=addtag3('\\1a&HFE&',text2)
txt=text2 if l==1 then txt=text end
line2.text=txt
:gsub("\\xshad([%d%.%-]+)",function(a) xx=tostring(f*a) xx=xx:gsub("([%d%-]+%.%d%d)%d+","%1") return "\\xshad"..xx end)
:gsub("\\yshad([%d%.%-]+)",function(a) yy=tostring(f*a) yy=yy:gsub("([%d%-]+%.%d%d)%d+","%1") return "\\yshad"..yy end)
line2.layer=layer+(lay-l)
subs.insert(i+1,line2)
end
if math.abs(xshad)>=1 or math.abs(yshad)>=1 then
subs.delete(i)
for s=z+1,#sel do
sel[s]=sel[s]+lay-1
end
success=success+1
else fail("No shadow to work with.")
end
end
-- CLIP GRID
if res.spec=="chequerboard clip" then
cbklip="\\clip(m 100 100 l 140 100 l 140 180 l 180 180 l 180 140 l 100 140 m 180 100 l 220 100 l 220 180 l 260 180 l 260 140 l 180 140 m 260 100 l 300 100 l 300 180 l 340 180 l 340 140 l 260 140 m 340 100 l 380 100 l 380 180 l 420 180 l 420 140 l 340 140 m 420 100 l 460 100 l 460 180 l 500 180 l 500 140 l 420 140 m 500 100 l 540 100 l 540 180 l 580 180 l 580 140 l 500 140 m 580 100 l 620 100 l 620 180 l 660 180 l 660 140 l 580 140 m 660 100 l 700 100 l 700 180 l 740 180 l 740 140 l 660 140 m 740 100 l 780 100 l 780 180 l 820 180 l 820 140 l 740 140 m 820 100 l 860 100 l 860 180 l 900 180 l 900 140 l 820 140 m 900 100 l 940 100 l 940 180 l 980 180 l 980 140 l 900 140 m 980 100 l 1020 100 l 1020 180 l 1060 180 l 1060 140 l 980 140 m 100 180 l 140 180 l 140 260 l 180 260 l 180 220 l 100 220 m 180 180 l 220 180 l 220 260 l 260 260 l 260 220 l 180 220 m 260 180 l 300 180 l 300 260 l 340 260 l 340 220 l 260 220 m 340 180 l 380 180 l 380 260 l 420 260 l 420 220 l 340 220 m 420 180 l 460 180 l 460 260 l 500 260 l 500 220 l 420 220 m 500 180 l 540 180 l 540 260 l 580 260 l 580 220 l 500 220 m 580 180 l 620 180 l 620 260 l 660 260 l 660 220 l 580 220 m 660 180 l 700 180 l 700 260 l 740 260 l 740 220 l 660 220 m 740 180 l 780 180 l 780 260 l 820 260 l 820 220 l 740 220 m 820 180 l 860 180 l 860 260 l 900 260 l 900 220 l 820 220 m 900 180 l 940 180 l 940 260 l 980 260 l 980 220 l 900 220 m 980 180 l 1020 180 l 1020 260 l 1060 260 l 1060 220 l 980 220 m 100 260 l 140 260 l 140 340 l 180 340 l 180 300 l 100 300 m 180 260 l 220 260 l 220 340 l 260 340 l 260 300 l 180 300 m 260 260 l 300 260 l 300 340 l 340 340 l 340 300 l 260 300 m 340 260 l 380 260 l 380 340 l 420 340 l 420 300 l 340 300 m 420 260 l 460 260 l 460 340 l 500 340 l 500 300 l 420 300 m 500 260 l 540 260 l 540 340 l 580 340 l 580 300 l 500 300 m 580 260 l 620 260 l 620 340 l 660 340 l 660 300 l 580 300 m 660 260 l 700 260 l 700 340 l 740 340 l 740 300 l 660 300 m 740 260 l 780 260 l 780 340 l 820 340 l 820 300 l 740 300 m 820 260 l 860 260 l 860 340 l 900 340 l 900 300 l 820 300 m 900 260 l 940 260 l 940 340 l 980 340 l 980 300 l 900 300 m 980 260 l 1020 260 l 1020 340 l 1060 340 l 1060 300 l 980 300 m 100 340 l 140 340 l 140 420 l 180 420 l 180 380 l 100 380 m 180 340 l 220 340 l 220 420 l 260 420 l 260 380 l 180 380 m 260 340 l 300 340 l 300 420 l 340 420 l 340 380 l 260 380 m 340 340 l 380 340 l 380 420 l 420 420 l 420 380 l 340 380 m 420 340 l 460 340 l 460 420 l 500 420 l 500 380 l 420 380 m 500 340 l 540 340 l 540 420 l 580 420 l 580 380 l 500 380 m 580 340 l 620 340 l 620 420 l 660 420 l 660 380 l 580 380 m 660 340 l 700 340 l 700 420 l 740 420 l 740 380 l 660 380 m 740 340 l 780 340 l 780 420 l 820 420 l 820 380 l 740 380 m 820 340 l 860 340 l 860 420 l 900 420 l 900 380 l 820 380 m 900 340 l 940 340 l 940 420 l 980 420 l 980 380 l 900 380 m 980 340 l 1020 340 l 1020 420 l 1060 420 l 1060 380 l 980 380 m 100 420 l 140 420 l 140 500 l 180 500 l 180 460 l 100 460 m 180 420 l 220 420 l 220 500 l 260 500 l 260 460 l 180 460 m 260 420 l 300 420 l 300 500 l 340 500 l 340 460 l 260 460 m 340 420 l 380 420 l 380 500 l 420 500 l 420 460 l 340 460 m 420 420 l 460 420 l 460 500 l 500 500 l 500 460 l 420 460 m 500 420 l 540 420 l 540 500 l 580 500 l 580 460 l 500 460 m 580 420 l 620 420 l 620 500 l 660 500 l 660 460 l 580 460 m 660 420 l 700 420 l 700 500 l 740 500 l 740 460 l 660 460 m 740 420 l 780 420 l 780 500 l 820 500 l 820 460 l 740 460 m 820 420 l 860 420 l 860 500 l 900 500 l 900 460 l 820 460 m 900 420 l 940 420 l 940 500 l 980 500 l 980 460 l 900 460 m 980 420 l 1020 420 l 1020 500 l 1060 500 l 1060 460 l 980 460 m 100 500 l 140 500 l 140 580 l 180 580 l 180 540 l 100 540 m 180 500 l 220 500 l 220 580 l 260 580 l 260 540 l 180 540 m 260 500 l 300 500 l 300 580 l 340 580 l 340 540 l 260 540 m 340 500 l 380 500 l 380 580 l 420 580 l 420 540 l 340 540 m 420 500 l 460 500 l 460 580 l 500 580 l 500 540 l 420 540 m 500 500 l 540 500 l 540 580 l 580 580 l 580 540 l 500 540 m 580 500 l 620 500 l 620 580 l 660 580 l 660 540 l 580 540 m 660 500 l 700 500 l 700 580 l 740 580 l 740 540 l 660 540 m 740 500 l 780 500 l 780 580 l 820 580 l 820 540 l 740 540 m 820 500 l 860 500 l 860 580 l 900 580 l 900 540 l 820 540 m 900 500 l 940 500 l 940 580 l 980 580 l 980 540 l 900 540 m 980 500 l 1020 500 l 1020 580 l 1060 580 l 1060 540 l 980 540)"
text=text:gsub("^({[^}]-)\\i?clip%([^%)]+%)","%1")
:gsub("^({\\[^}]-)}","%1"..cbklip.."}")
if not text:match("^{") then text=wrap(cbklip)..text end
end
-- size transform from clip
if res.spec=="size transform from clip" then
local klip=text:match("\\i?clip%(m %-?[%d%.]+ %-?[%d%.]+ l %-?[%d%.]+ %-?[%d%.]+ %-?[%d%.]+ %-?[%d%.]+ %-?[%d%.]+ %-?[%d%.]+")
if not klip then t_error("Line #"..i-line0..": Vectorial clip with 4 points required.",1) end
local K1x,K1y,K2x,K2y,K3x,K3y,K4x,K4y=klip:match("m (%-?[%d%.]+) (%-?[%d%.]+) l (%-?[%d%.]+) (%-?[%d%.]+) (%-?[%d%.]+) (%-?[%d%.]+) (%-?[%d%.]+) (%-?[%d%.]+)")
if defaref and line.style=="Default" then sr=defaref
else sr=stylechk(line.style) end
-- clean up existing transforms
if text:match("^{[^}]*\\t") then text=text:gsub(STAG,function(tg) return cleantr(tg) end) end
startags=text:match(STAG) or ""
scx=startags:match("\\fscx([%d%.]+)") or sr.scale_x
scy=startags:match("\\fscy([%d%.]+)") or sr.scale_y
xdist1=math.abs(K1x-K2x)
xdist2=math.abs(K3x-K4x)
ydist1=math.abs(K1y-K2y)
ydist2=math.abs(K3y-K4y)
dist1=math.sqrt(xdist1^2+ydist1^2)
dist2=math.sqrt(xdist2^2+ydist2^2)
size_ratio=dist2/dist1
rescx=round(scx*size_ratio,2)
rescy=round(scy*size_ratio,2)
local trans="\\t(\\fscx"..rescx.."\\fscy"..rescy..")"
startags=startags:gsub("}",trans.."}")
startags=cleantr(startags)
text=text:gsub(STAG,startags):gsub("\\i?clip%b()","")
end
-- BACK AND FORTH TRANSFORM
if res.spec=="back and forth transform" and res.int>0 then
selcheck()
if defaref and line.style=="Default" then sr=defaref
else sr=stylechk(line.style) end
-- clean up existing transforms
if text:match("^{[^}]*\\t") then text=text:gsub(STAG,function(tg) return cleantr(tg) end) end
startags=text:match(STAG) or ""
tags_1=""
for tg in transphorm:gmatch("\\[1234]?%a+") do
val1=nil
if not startags:match(tg.."[%d%-&%(]") then
if tg=="\\clip" then val1="(0,0,1280,720)" else val1=tag2style(tg,sr) end
if val1 then tags_1=tags_1..tg..val1 text=text:gsub("^({\\[^}]-)}","%1"..tg..val1.."}") end
else
val1=startags:match(tg.."([^\\}]+)")
tags_1=tags_1..tg..val1
end
end
int=res.int
tgs2=transphorm
dur=line.end_time-line.start_time
count=math.ceil(dur/int)
t=1 tin=0 tout=tin+int
if not text:match("^{\\") then text="{\\}"..text end
-- main function
while t<=math.ceil(count/2) do
text=text:gsub("^({\\[^}]*)}","%1\\t("..tin..","..tout..","..tgs2..")}")
if tin+int<dur then text=text:gsub("^({\\[^}]*)}","%1\\t("..tin+int..","..tout+int..","..tags_1..")}") end
tin=tin+int+int
tout=tin+int
t=t+1
end
text=text:gsub("\\([\\}])","%1")
end
-- SPLIT LINE IN 3 PARTS
if res.spec=="split line in 3 parts" then
start=line.start_time
endt=line.end_time
dur=line.end_time-line.start_time
-- Split Times
ST1=res.trin
ST2=res.trout
if ST1+ST2>=dur then fail("Split times for some lines are longer than duration of line.")
else
effect=line.effect
-- line 3
if ST2>0 then
line3=line
line3.start_time=endt-ST2
line3.effect=effect.." pt.3"
subs.insert(i+1,line3)
nsel=shiftsel2(nsel,i,1)
end
-- line 2
line2=line
line2.start_time=start+ST1
line2.end_time=endt-ST2
line2.effect=effect.." pt.2"
subs.insert(i+1,line2)
nsel=shiftsel2(nsel,i,1)
-- line 1
if ST1>0 then
line.start_time=start
line.end_time=start+ST1
line.effect=effect.." pt.1"
else
subs.delete(i)
for s=#nsel,z,-1 do
if nsel[s]==i then table.remove(nsel,s) end
if nsel[s]>i then nsel[s]=nsel[s]-1 end
end
end
success=success+1
end
end
if res.spec~="create 3D effect from shadow" then
if line.text~=text then
success=success+1
line.text=text
subs[i]=line
end
end
end
end
if res.spec=="split line in 3 parts" then sel=nsel end
summary()
return sel
end
function frz_redraw(t,rota,draw,sr)
local X,Y,x,y,width,height
local ox,oy,xmx,xmn,ymx,ymn,addx,addy=0,0,0,999999,0,999999,0,0
draw1=draw
-- deal with align other than 7
if align~=7 then
for px,py in draw:gmatch("([-%d.]+) ([-%d.]+)") do
px=tonumber(px)
py=tonumber(py)
if px>xmx then xmx=px end
if px<xmn then xmn=px end
if py>ymx then ymx=py end
if py<ymn then ymn=py end
end
width=xmx-xmn
height=ymx-ymn
align=tonumber(t:match'\\an(%d)') or sr.align
hal=align%3
val=math.ceil(align/3)
if hal==2 then addx=width/2 elseif hal==0 then addx=width end
if val==2 then addy=height/2 elseif val==1 then addy=height end
-- change to an7 + adjust coordinates
draw1=draw1:gsub("([-%d.]+) ([-%d.]+)",function(px,py)
return px-addx..' '..py-addy
end)
t,c=t:gsub("\\an%d","\\an7")
if c==0 then t=t:gsub("^{","{\\an7") end
end
-- recalculate coordinates without frz
draw2=draw1:gsub("([-%d.]+) ([-%d.]+)",function(px,py)
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)
y=round(oy-Y)
return x..' '..y
end)
-- replace drawing
t=t:gsub(esc(draw),draw2):gsub("\\frz[-%d.]+","")
return t
end
function selover(subs,sel)
local dialogue={}
for i,line in ipairs(subs) do
if line.class=="dialogue" then line.i=i table.insert(dialogue,line) end
end
table.sort(dialogue,function(a,b) return a.start_time<b.start_time or (a.start_time==b.start_time and a.i<b.i) end)
local end_time=0
local overlaps={}
for i=1,#dialogue do
local line=dialogue[i]
if line.start_time>=end_time then
end_time=line.end_time
else
table.insert(overlaps,line.i)
end
end