forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ua.Masquerade.lua
1637 lines (1515 loc) · 58 KB
/
ua.Masquerade.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="Masquerade"
script_description="Dark Elves with Blurry Masks Shifting in Motion"
script_author="unanimated"
script_version="3.0"
script_namespace="ua.Masquerade"
local haveDepCtrl,DependencyControl,depRec=pcall(require,"l0.DependencyControl")
if haveDepCtrl then
script_version="3.0.0"
depRec=DependencyControl{feed="https://raw.githubusercontent.com/unanimated/luaegisub/master/DependencyControl.json"}
end
hlp=[[ Full manual: http://unanimated.hostfree.pw/ts/scripts-manuals.htm#masquerade
>> Masquerade
Creates a mask with the selected shape
"New Line" creates the mask on a new line and raises the layer of the current line by 1
"Remask" only changes an existing mask for another shape without changing tags
"Save/delete mask" lets you save a mask from active line or delete one of your custom masks
- to save a mask, type a name, and the mask from your active line will be saved
(appdata/masquerade.masks)
- to delete a mask, type its name or type 'del', and select the name from the menu on the left
>> Merge Tags
Select lines with the SAME TEXT but different tags,
and they will be merged into one line with tags from all of them.
For example:
{\bord2}AB{\shad3}C
A{\fs55}BC
-> {\bord2}A{\fs55}B{\shad3}C
If two lines have the same tag in the same place,
the value of the later line overrides the earlier one.
>> an8 / q2
Adds selected tags.
>> Motion Blur
Creates motion blur by duplicating the line and using some alpha.
You can set a value for blur or keep the existing blur for each line.
'Dist.' is the distance between the \pos coordinates of the resulting 2 lines.
If you use 3 lines, the 3rd one will be in the original position, i.e. in the middle.
The direction is determined from the first 2 points of a vectorial clip.
>> Shift Tags
For active line (when only one selected):
Allows you to shift tags by character or by word.
For the first block, single tags can be moved right.
For inline tags, each block can be moved left or right.
If there are multiple same inline tags, results will be uncertain.
If a tag is marked with an arrow (>\tag), then one of two things will happen:
1. if there is {•} in the line, the tag is moved to replace •
2. otherwise you get to choose where to move that tag
(Check HYDRA for the usage of Arrow Shifter & Bell Shifter.)
For selection >1:
1. If any line contains >\ and {•}, then the arrow-marked tag replaces the bell.
2. If any line contains >\ and all lines have the same text (and no {•}),
you get to choose where the tag goes.
Different lines can have different tags marked by the arrow.
3. If none of that applies, all inline tags are shifted by one character to the right.
If line ends with {switch} comment, tags move by 1 character to the left.
You can shift by more, using the 'Shift by' option.
(See Cycles script for a macro that adds/removes the {switch} comment.)
>> Alpha Shift
Makes text appear letter by letter on frame-by-frame lines using alpha&HFF& like this:
{alpha&HFF&}text
t{alpha&HFF&}ext
te{alpha&HFF&}xt
tex{alpha&HFF&}t
text
'Shift by' can be used here as well.
If you switch from 'α' to '1a', \alpha tags will be changed into \1a tags instead.
>> Alpha Time
Either select lines that are already timed for alpha timing and need alpha tags,
or just one line that needs to be alpha timed.
In the GUI, split the line by hitting Enter where you want the alpha tags.
If you make no line breaks, text will be split by spaces.
Alpha Text is for when you have the lines already timed and just need the tags.
Alpha Time is for one line. It will be split to equally long lines with alpha tags added.
If you add "@" to your line first, alpha tags will replace the @, and no GUI will pop up.
Example text: This @is @a @test.
>> StrikeAlpha
Replaces strikeout or underline tags with \alpha&H00& or \alpha&HFF&. Also @.
{\s0} -> {\alpha&HFF&}
{\s1} -> {\alpha&H00&}
{\u1} -> {\alpha&HFF&}
{\u0} -> {\alpha&H00&}
@ -> {\alpha&HFF&}
@0 -> {\alpha&H00&}
@E3@ -> {\alpha&HE3&}
1@ -> {\1a&HFF&} (All variations)
If no replacement is made, it will reorder alpha tags in each block so that all 1a-4a go after alpha.
Also uses the Bell to comment sections of text.
0 You can comment out parts of this {•}line with StrikeAlpha and {\i1}Bell Shifter{\i0}.
1 You can comment out parts of this {•line }with StrikeAlpha and {\i1}Bell Shifter{\i0}.
2 You can comment out parts of this {•line with StrikeAlpha and }{\i1}Bell Shifter{\i0}.
3 You can comment out parts of this line with StrikeAlpha and {\i1}Bell Shifter{\i0}.
If there's {~} in the line, you'll get a menu with some options, so try that out.
>> Betastrike
Switches various parts of the text.
{\s1}word1 word2 word3 word4{\s0} --> word4 word3 word2 word1
{\u1}some text1 {\u0}other text2 --> other text2 some text1
[the space before {\u0} will remain there; \s and \u tags can be part of a larger block]
{~}{\tags1}abc def {•}{\tags2}ghi <-> {•}{\tags2}abc def {~}{\tags1}ghi
{~}word1 word2 word3 {•}word4 <-> {•}word4 word2 word3 {~}word1
{~}{\tags1}word1 word2 {•}word3 <-> {•}word1 word2 {~}{\tags1}word3
>> Deltastrike
Define your own replacements for the 6 listed things when saving config.
>> Converter (separate macro)
Define as many conversions as you want for whatever you want.
These are literal (and case sensitive) and don't check for any syntax errors,
so be careful with things like {}.
always/ask - replacements are either made automatically or you get asked which ones you want.
Only ones that exist in selected lines will be offered (so you won't get asked about irrelevant ones).
raw/clean - raw converter uses whole "text", whereas the clean one skips stuff in {}.
If you run Converter and no replacements are made (nothing found or nothing defined),
the GUI appears and you can add new replacements.
Converter gives you lots of options, but you need to be careful not to replace things accidentally.
Converting "bus" to "car" will also convert "busy" to "cary",
so use the "Whole word only" option for that.
Some suggestions for usage:
convert -- to —
(or whatever else you can't remember how to type)
convert embarass to embarrass, english to English, didnt to didn't
(and any other useful corrections)
convert Dammit to Damn it
(and other editing preferences)
convert \xshad0\yshad0 to \xshad1\yshad1
(if you also convert 1 to 2, they have to be in the list in the opposite order,
otherwise they will both run in sequence)
Converting something both ways like \clip to \iclip and \iclip to \clip wouldn't work,
because the second one would revert the first one, but if you set them to "ask",
you can then pick the one of the two that you need at the moment.
]]
re=require'aegisub.re'
function addmask(subs,sel)
nsel={} for z,i in ipairs(sel) do table.insert(nsel,i) end
for z=#sel,1,-1 do
i=sel[z]
l=subs[i]
text=l.text
l.layer=l.layer+1
err=nil
if res.masknew and not res.remask then
if res.mask=="from clip" then
if not text:match("\\clip") then noclip=true err=1
if text:match("\\iclip") then i_clip=true end
end
if not err then l.text=nopar("clip",l.text) end
end
if not err then nsel=shiftsel2(nsel,i,1) subs.insert(i+1,l) end
end
l.layer=l.layer-1
if text:match("\\2c") then mcol="\\c"..text:match("\\2c(&H[%x]+&)") else mcol="" end
-- REMASK
if res.remask then
if text:match("\\p1") then
if res.mask=="from clip" then
if not text:match("\\clip") then noclip=true
else
masklip()
l.text=nopar("clip",l.text)
nmask=ctext2 l.text=re_mask(l.text)
end
else
for k=1,#allmasks do
if allmasks[k].n==res.mask then
nmask=allmasks[k].m l.text=re_mask(l.text)
end
end
end
end
else
-- STANDARD MASK
if res.mask=="from clip" then
if not err then
masklip()
l.text="{\\an7\\blur1\\bord0\\shad0\\fscx100\\fscy100"..mcol..mp..pos.."\\p1}"..ctext2
end
else
atags=""
org=l.text:match("\\org%b()") if org then atags=atags..org end
frz=l.text:match("\\frz[%d%.%-]+") if frz then atags=atags..frz end
frx=l.text:match("\\frx[%d%.%-]+") if frx then atags=atags..frx end
fry=l.text:match("\\fry[%d%.%-]+") if fry then atags=atags..fry end
l.text=l.text:gsub(".*(\\pos%([%d%,%.%-]-%)).*","%1")
if not l.text:match("\\pos") then l.text="" end
for k=1,#allmasks do
if allmasks[k].n==res.mask then
if res.mask:match("alignment grid") then
l.text="{\\an7\\bord0\\shad0\\blur0.6"..l.text..atags.."\\c&H000000&\\alpha&H80&\\p1}"..allmasks[k].m
else
l.text="{\\an7\\bord0\\shad0\\blur1"..l.text..mcol.."\\p1}"..allmasks[k].m
end
if res.mask=="square" then l.text=l.text:gsub("\\an7","\\an5") end
end
end
if not l.text:match("\\pos") then l.text=l.text:gsub("\\p1","\\pos(640,360)\\p1") end
end
end
subs[i]=l
end
if noclip then t_error("Some lines weren't processed - missing \\clip.") noclip=nil end
if i_clip then t_error("Some lines weren't processed - \\iclip ignored.") i_clip=nil end
sel=nsel
return sel
end
function masklip()
oscf=text:match("\\i?clip%((%d+),m")
if oscf then
fact1=2^(oscf-1)
text=text:gsub("(\\i?clip%()(%d*,?)m ([^%)]+)%)",function(a,b,c)
return a.."m "..c:gsub("([%d%.%-]+)",function(d) return round(d/fact1) end)..")" end)
end
text=text:gsub("\\clip%(([%d%.%-]+),([%d%.%-]+),([%d%.%-]+),([%d%.%-]+)","\\clip(m %1 %2 l %3 %2 %3 %4 %1 %4)")
if text:match("\\move") then text=text:gsub("\\move","\\pos") mp="\\move" else mp="\\pos" end
ctext=text:match("clip%(m ([%d%.%a%s%-]+)")
if not ctext then t_error("There's something wrong with the clip here:\n"..text:match("{.-clip.-}"),1) end
if text:match("\\pos") then
pos=text:match("\\pos(%([^%)]+%))")
local xx,yy=text:match("\\pos%(([%d%.%-]+),([%d%.%-]+)")
xx=round(xx) yy=round(yy)
ctext2=ctext:gsub("([%d%.%-]+)%s([%d%.%-]+)",function(a,b) return a-xx.." "..b-yy end)
else pos="(0,0)" ctext2=ctext
end
ctext2="m "..ctext2:gsub("([%d%.]+)",function(a) return round(a) end)
end
function re_mask(text)
text=text
:gsub("\\fsc[xy][^}\\]+","")
:gsub("}m [^{]+","\\fscx100\\fscy100}"..nmask)
if res.mask=="square" then text=text:gsub("\\an7","\\an5") end
return text
end
function savemask(subs,sel,act)
masker=ADP("?user").."\\masquerade.masks"
file=io.open(masker)
if file then masx=file:read("*all") io.close(file) end
if masx==nil then masx="" end
mask_name=res.maskname
masx=masx:gsub(":\n$",":\n\n") :gsub(":$",":\n\n")
-- delete mask
deleted=0
for m=1,#maasks do
if mask_name=="del" then mask_name=res.mask end
if maasks[m]==mask_name then
if m<=10 then t_error("You can't delete a default mask.",1)
else e_name=esc(mask_name) masx=masx:gsub("mask:"..e_name..":.-:\n\n","") t_error("Mask '"..mask_name.."' deleted",false)
end
deleted=1
end
end
-- add new mask
if deleted==0 then
text=subs[act].text
text=text:gsub("{[^}]-}","")
newmask=text:match("m [%d%s%-mbl]+")
if not newmask then t_error("No mask detected.",1) end
newmask=newmask:gsub("%s*$","")
if newmask==nil then t_error("No mask detected on active line.",1) end
if mask_name=="mask name here" or mask_name=="" then
p,rez=ADD({{class="label",label="Enter a proper name for the mask:"},
{y=1,class="edit",name="mname"},},{"OK","Cancel"},{ok='OK',close='Cancel'})
if p=="Cancel" then ak() end
if rez.mname=="" then t_error("Naming fail",1) else mask_name=rez.mname end
for m=1,#maasks do
if maasks[m]==mask_name then
t_error("Mask '"..mask_name.."' already exists.",1)
end
end
end
new_mask="mask:"..mask_name..":"..newmask..":\n\n"
masx=masx..new_mask
end
masx=masx:gsub(":\nmask",":\n\nmask")
file=io.open(masker,"w")
file:write(masx)
file:close()
if deleted==0 then
ADD({{class="label",label="Mask '"..mask_name.."' saved to:\n"..masker}},{"OK"},{close='OK'})
end
end
-- Merge tags -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function merge(subs,sel,act)
tk={}
tg={}
stg=""
for z,i in ipairs(sel) do
progress("Merging "..z.."/"..#sel)
line=subs[i]
text=line.text
text=text:gsub("{\\\\k0}",""):gsub("\\N","{\\N}")
text=tagmerge(text)
vis=text:gsub("%b{}","")
if vis:gsub(" ","")=="" then t_error("No text on line 1.",1) end
if z==1 then rt=vis
C=0
repeat
for ltr in re.gfind(rt,'.') do table.insert(tk,ltr) end
vis2=table.concat(tk,"")
C=C+1
until vis==vis2 or C==1666
end
if vis~=vis2 then t_error("Error. Inconsistent text.\n"..vis.."\n"..vis2,1) end
if vis~=rt then t_error("Error. Inconsistent text.\nAll selected lines must contain the same text.",1) end
stags=text:match("^{([^}]-)}") or ""
stg=stg..stags stg=duplikill(stg)
repeat stg,r=stg:gsub("(\\[ibusqa]n?%d)(.-)%1","%2%1") until r==0
text=text:gsub(STAG,"")
count=0
for seq in text:gmatch("[^{]-{%*?[^}]-}") do
chars,as,tak=seq:match("([^{]-){(%*?)([^}]-)}")
pos=re.find(chars,".")
if pos==nil then ps=0+count else ps=#pos+count end
tgl={p=ps,t=tak,a=as}
table.insert(tg,tgl)
count=ps
end
end
newline=""
for i=1,#tk do
newline=newline..tk[i]
newt=""
for n,t in ipairs(tg) do
if t.p==i then newt=newt.."{"..t.t.."}" end
newt=tagmerge2(newt)
newt=newt:gsub("(\\[^\\})]*)%1","%1"):gsub("(\\[ibusqa]%d)(.-)%1","%2%1")
end
if newt~="" then newline=newline..newt end
end
repeat newline,r=newline:gsub("{\\N","\\N{") until r==0
newtext="{"..stg.."}"..newline:gsub("{}","")
newtext=newtext:gsub("({%*?\\[^}]-})",function(tg) return duplikill(tg) end)
newtext=extrakill(newtext,2)
line=subs[sel[1]]
line.text=newtext
subs[sel[1]]=line
for z=#sel,2,-1 do subs.delete(sel[z]) end
sel={sel[1]}
act=sel[1]
return sel, act
end
function add_an8(subs,sel)
for z,i in ipairs(sel) do
progress("Alien8ing "..z.."/"..#sel)
line=subs[i]
text=line.text
if res.an8=="q2" then
if text:match("\\q2") then text=text:gsub("\\q2",""):gsub("{}","")
else text="{\\q2}"..text text=text:gsub("\\q2}{\\","\\q2\\") end
else
text,r=text:gsub("\\(an%d)","\\"..res.an8)
if r==0 then
text="{\\"..res.an8.."}"..text
text=text:gsub("({\\an%d)}{\\","%1\\")
end
end
line.text=text
subs[i]=line
end
end
-- Motion Blur -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function motionblur(subs,sel)
mblur=res.mblur mbdist=res.mbdist mbalfa=res.mbalfa mb3=res.mb3 keepblur=res.keepblur
for z=#sel,1,-1 do
progress("Blurring "..#sel-z+1 .."/"..#sel)
i=sel[z]
line=subs[i]
text=line.text
if text:match("clip%(m") then
if not text:match("\\pos") and not text:match("\\move") then text=getpos(subs,text) end
if not res.keepblur then text=addtag("\\blur"..mblur,text) end
text=text:gsub("{%*?\\[^}]-}",function(tg) return duplikill(tg) end)
c1,c2,c3,c4=text:match("clip%(m ([%-%d%.]+) ([%-%d%.]+) l ([%-%d%.]+) ([%-%d%.]+)")
if c1==nil then t_error("There seems to be something wrong with your clip.",1) end
text=text:gsub("\\i?clip%b()","")
text=addtag3("\\alpha&H"..mbalfa.."&",text)
cx=c3-c1
cy=c4-c2
cdist=math.sqrt(cx^2+cy^2)
mbratio=cdist/mbdist*2
mbx=round(cx/mbratio*100)/100
mby=round(cy/mbratio*100)/100
text2=text
:gsub("\\pos%(([%-%d%.]+),([%-%d%.]+)",function(a,b) return "\\pos("..a-mbx..","..b-mby end)
:gsub("\\move%(([%-%d%.]+),([%-%d%.]+),([%-%d%.]+),([%-%d%.]+)",function(a,b,c,d) return "\\move("..a-mbx..","..b-mby..","..c-mbx..","..d-mby end)
l2=line
l2.text=text2
subs.insert(i+1,l2)
for s=z+1,#sel do
sel[s]=sel[s]+1
end
table.insert(sel,sel[z]+1)
if res.mb3 then
line.text=text
subs.insert(i+1,line)
for s=z+1,#sel do
sel[s]=sel[s]+1
end
table.insert(sel,sel[z]+1)
end
text=text
:gsub("\\pos%(([%-%d%.]+),([%-%d%.]+)",function(a,b) return "\\pos("..a+mbx..","..b+mby end)
:gsub("\\move%(([%-%d%.]+),([%-%d%.]+),([%-%d%.]+),([%-%d%.]+)",function(a,b,c,d) return "\\move("..a+mbx..","..b+mby..","..c+mbx..","..d+mby end)
else noclip=true
end
line.text=text
subs[i]=line
end
if noclip then t_error("Some lines weren't processed - missing clip.\n(2 points of a vectorial clip are needed for motion direction.)") noclip=nil end
end
-- Shift Tags sel -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function shitfags(subs,sel)
local ref=nobra(subs[sel[1]].text)
sametxt=true
local arrow,arrow2bell
for z,i in ipairs(sel) do
local l=subs[i]
local t=l.text
if nobra(t)~=ref then sametxt=false end
if t:match'>\\' and t:match'{•}' then arrow2bell=true end
if t:match'>\\' then arrow=true end
end
for z,i in ipairs(sel) do
progress("Shifting "..z.."/"..#sel)
local l=subs[i]
local t=l.text
local stags=t:match(STAG) or ""
-- arrow to bell
if arrow2bell then
local tag=t:match(">(\\[^\\}]*)")
if tag then
t=t:gsub(">\\[^\\}]*",""):gsub("{}","")
repeat t,r=t:gsub("({•})({[^}]*})","%2%1") until r==0
t=t:gsub("{•}",wrap(tag))
t=tagmerge(t)
t=t:gsub(ATAG,function(a) return duplikill(a) end)
end
-- arrow to arrow
elseif arrow and sametxt then
local tag=t:match(">(\\[^\\}]*)")
t=t:gsub(">\\[^\\}]*",""):gsub("{}","")
stags=stags:gsub(">\\[^\\}]*",""):gsub("{}","")
if z==1 then -- GUI on line 1
G={
{x=0,y=0,class="label",label="Tag to shift (on line 1): "..tag:gsub("&","&&")},
{x=0,y=1,width=16,class="edit",name="shift",value=ref},
{x=0,y=2,class="label",label="Place a '>' where you want the tag to move. Multiple places are possible."},
}
Pr,rez=ADD(G,{"GO","Leave"},{ok='GO',close='Leave'})
if Pr=="Leave" or not rez.shift:match '>' then ak() end
end
if tag then -- shift
local nvis=rez.shift:gsub(">","")
if nvis~=ref then t_error("Error: Text has changed.\n<- "..vis.."\n-> "..nvis,1) end
t=t:gsub("(\\t)(%b())",function(a,b) return a..b:gsub("\\","/") end)
nt=rez.shift:gsub(">",wrap(tag))
t2=retextmod(t,nt)
t2=stags..t2
t=t2:gsub("(\\t)(%b())",function(a,b) return a..b:gsub("/","\\") end)
:gsub(ATAG,function(s) return duplikill(s) end)
end
-- shift right or left
else
t=t:gsub(STAG,"")
t=tagmerge(t)
t=t:gsub("\\N","\n"):gsub("}{","_||_")
local shi=0
repeat
if not t:match"{switch}$" then
-- forward
t=re.sub(t,"({[^}]*\\\\[^}]*})(\n*.[\n ]*)","\\2\\1")
else
-- backward
t=re.sub(t,"(\n*.[\n ]*)({[^}]*\\\\[^}]*})","\\2\\1")
end
shi=shi+1
until shi==shiftby
t=stags..t:gsub("_||_","}{"):gsub("\n","\\N")
t=tagmerge(t):gsub(ATAG.."$","")
t=t:gsub(STAG,function(s) return duplikill(s) end)
end
l.text=t
subs[i]=l
end
end
-- Shift Arrow Tag -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function shiftarrow(subs,sel,a)
rine=subs[a]
local t=rine.text
local tag=t:match(">(\\[^\\}]*)")
t=t:gsub(">\\[^\\}]*",""):gsub("{}","")
if t:match"{•}" then
t2=t
repeat t2,r=t2:gsub("({•})({[^}]*})","%2%1") until r==0
t2=t2:gsub("{•}",wrap(tag))
t2=tagmerge(t2)
t2=t2:gsub(ATAG,function(a) return duplikill(a) end)
else
t=t:gsub("(\\t)(%b())",function(a,b) return a..b:gsub("\\","/") end)
local stags=t:match(STAG) or ""
local vis=t:gsub("%b{}","")
t=t:gsub(STAG,"")
G={
{x=0,y=0,class="label",label="Tag to shift: "..tag:gsub("&","&&")},
{x=0,y=1,width=16,class="edit",name="shift",value=vis},
{x=0,y=2,class="label",label="Place a '>' where you want the tag to move. Multiple places are possible."},
}
Pr,rez=ADD(G,{"GO","Leave"},{ok='GO',close='Leave'})
if Pr=="Leave" or not rez.shift:match '>' then ak() end
local nvis=rez.shift:gsub(">","")
if nvis~=vis then t_error("Error: Text has changed.\n<- "..vis.."\n-> "..nvis,1) end
nt=rez.shift:gsub(">",wrap(tag))
t2=retextmod(t,nt)
t2=stags..t2
t2=t2:gsub("(\\t)(%b())",function(a,b) return a..b:gsub("/","\\") end)
end
rine.text=t2
subs[a]=rine
return a
end
-- Shift Tags act -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function shiftag(subs,sel,act)
rine=subs[act]
tags=rine.text:match(STAG) or ""
ftags=tags:gsub("[{}]","")
ftags=ftags:gsub("\\%a+%b()","") :gsub("\\an?%d","")
cstext=rine.text:gsub(STAG,"")
if tags:match'\\p1' then t_error("Line contains a drawing.\nYou don't wanna shift tags into that.",1) end
rept={"drept"}
-- build GUI
shiftab={
{x=0,y=0,width=3,class="label",label="[ Start Tags (Shift Right only) ] ",},
{x=3,y=0,class="label",label="[ Inline Tags ] ",},
}
ftw=1
-- regular tags -> GUI
for f in ftags:gmatch("\\[^\\]+") do lab=f:gsub("&","&&")
cb={x=0,y=ftw,width=2,class="checkbox",name="chk"..ftw,label=lab,value=false,realname=f}
table.insert(shiftab,cb) ftw=ftw+1
table.insert(rept,f)
end
table.insert(shiftab,{x=0,y=ftw+1,class="label",label="Shift by letter /"})
table.insert(shiftab,{x=1,y=ftw+1,class="checkbox",name="word",label="word"})
table.insert(shiftab,{x=0,y=ftw+2,width=2,class="intedit",name="rept",value=1,min=1})
table.insert(shiftab,{x=0,y=ftw+3,width=2,class="checkbox",name="nuke",label="remove selected tags"})
itw=1
-- inline tags
if cstext:match(ATAG) then
for f in cstext:gmatch(ATAG) do lab=f:gsub("&","&&")
if itw==31 then lab="Error: 30 tags max" f="" end
if itw==32 then break end
cb={x=3,y=itw,class="checkbox",name="chk2"..itw,label=lab,value=false,realname=f}
drept=0 for r=1,#rept do if f==rept[r] then drept=1 end end
if drept==0 then
table.insert(shiftab,cb) itw=itw+1
table.insert(rept,f)
end
end
end
repeat
if press=="All Inline Tags" then
for key,val in ipairs(shiftab) do
if val.class=="checkbox" and val.x==3 then val.value=true end
if val.class=="checkbox" and val.x<3 then val.value=res[val.name] end
end
end
press,rez=ADD(shiftab,{"Shift Left","Shift Right","All Inline Tags","Cancel"},{ok='Shift Right',close='Cancel'})
until press~="All Inline Tags"
if press=="Cancel" then ak() end
if press=="Shift Right" then R=true else R=false end
if press=="Shift Left" then L=true else L=false end
-- nuke tags
if rez.nuke then
for key,val in ipairs(shiftab) do
if val.class=="checkbox" and rez[val.name] and val.x==0 and val.name~="nuke" then
tags=tags:gsub(esc(val.realname),"")
rez[val.name]=false
end
if val.class=="checkbox" and rez[val.name] and val.x==3 then
cstext=cstext:gsub(esc(val.realname),"")
rez[val.name]=false
end
end
end
-- shift inline tags
cstext=cstext:gsub("\\N","{\\N}")
if R then -- RIGHT
for s=#shiftab,1,-1 do stab=shiftab[s]
if rez[stab.name] and stab.x==3 then stag=stab.realname etag=esc(stag) retag=resc(stag)
rep=0
repeat
if not rez.word then
repeat cstext,r=cstext:gsub(etag.."( *%b{} *)","%1"..stag) until r==0
cstext=re.sub(cstext,retag.."([^ {}] *)","\\1"..retag)
repeat cstext,r=cstext:gsub(etag.."( *%b{} *)","%1"..stag) until r==0
else
repeat cstext,r=cstext:gsub(etag.."(%b{})","%1"..stag) until r==0
cstext=cstext:gsub(etag.."( *%S+ *)","%1"..stag)
repeat cstext,r=cstext:gsub(etag.."(%b{})","%1"..stag) until r==0
end
rep=rep+1
until rep==rez.rept
end
end
elseif L then -- LEFT
for key,val in ipairs(shiftab) do
if rez[val.name]==true and val.x==3 then stag=val.realname etag=esc(stag) retag=resc(stag)
rep=0
repeat
if not rez.word then
repeat cstext,r=cstext:gsub("(%b{} *)"..etag,stag.."%1") until r==0
cstext=re.sub(cstext,"([^ {}] *)"..retag,retag.."\\1")
repeat cstext,r=cstext:gsub("(%b{} *)"..etag,stag.."%1") until r==0
else
repeat cstext,r=cstext:gsub("((%b{}) *)"..etag,stag.."%1") until r==0
cstext=cstext:gsub("(%S+ *)"..etag,stag.."%1")
repeat cstext,r=cstext:gsub("((%b{}) *)"..etag,stag.."%1") until r==0
end
rep=rep+1
until rep==rez.rept
end
end
end
cstext=cstext:gsub("{\\N}","\\N")
cstext=tagmerge(cstext)
cstext=cstext:gsub("(%b{})\\N","\\N%1"):gsub(ATAG,function(tg) return duplikill(tg) end)
-- shift start tags
startags=""
for key,val in ipairs(shiftab) do
if rez[val.name]==true and val.x==0 and val.name~="nuke" then stag=val.realname etag=esc(stag)
if R then
startags=startags..stag
tags=tags:gsub(etag,"")
end
end
end
if startags~="" and R then
cstext="{_T_}"..cstext
REP=0
if not rez.word then
repeat
cstext=re.sub(cstext,"\\{_T_\\}([\\w[:punct:]]\\s*)","\\1\\{_T_\\}")
cstext=cstext
:gsub("{_T_}(\\N%s?)","%1{_T_}")
:gsub("{_T_}(%b{}%s*)","%1{_T_}")
:gsub("{_T_}(\\N%s?)","%1{_T_}")
REP=REP+1
until REP==rez.rept
else
repeat
cstext=cstext
:gsub("{_T_}(%s*[^%s]+%s*)","%1{_T_}")
:gsub("{_T_}(%s?\\N%s?)","%1{_T_}")
:gsub("{_T_}(%b{})","%1{_T_}")
:gsub("{_T_}(%s?\\N%s?)","%1{_T_}")
REP=REP+1
until REP==rez.rept
end
cstext=cstext
:gsub("_T_",startags)
:gsub("{(%*?\\[^}]-)}{(%*?\\[^}]-)}","{%1%2}")
end
text=tags..cstext
text=text:gsub("{(%*?\\[^}]-)}{(%*?\\[^}]-)}","{%1%2}")
:gsub("^{}","")
:gsub(ATAG,function(tg) return duplikill(tg) end) :gsub("^{%*","{")
rine.text=text
subs[act]=rine
end
-- Alpha Shift -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function alfashift(subs,sel)
count=0
for z,i in ipairs(sel) do
progress("Shifting "..z.."/"..#sel)
line=subs[i]
text=line.text
if z>1 then
text=text:gsub("^{([^}]*)(\\alpha&HFF&)([^}]*)}","{%1%3}{%2}"):gsub("{}","")
aa=re.find(text,"{\\\\alpha&HFF&}[^{} ]")
if not aa then t_error("Line #"..i-line0.." does not appear to have \\alpha&&HFF&&",1) end
switch=0
repeat
repeat text,c=text:gsub("({\\alpha&HFF&})(%b{} ?)","%2%1") until c==0
text=re.sub(text,"({\\\\alpha&HFF&})([^{} ])","\\2\\1")
text=text
:gsub("({\\alpha&HFF&}) "," %1")
:gsub("({\\alpha&HFF&})\\N","\\N%1")
:gsub("({\\alpha&HFF&})$","")
switch=switch+1
until switch>=count
end
count=count+1*shiftby
line.text=text
subs[i]=line
end
end
-- Alpha Shift 2 -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function alfashift2(subs,sel)
ash=res.ash
for z,i in ipairs(sel) do
progress("Shifting "..z.."/"..#sel)
line=subs[i]
text=line.text
text=text:gsub("\\alpha","\\"..ash)
line.text=text
subs[i]=line
end
end
-- Alfa Time -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function alfatime(subs,sel)
-- collect / check text
for z,i in ipairs(sel) do
text=subs[i].text
if z==1 then alfatext=text:gsub(STAG,"")
else alfatext2=text:gsub(STAG,"")
if alfatext2~=alfatext then t_error("Text must be the same \nfor all selected lines",1) end
end
if text:match'\\p1' then t_error("Line #"..i-line0.." contains a drawing.\nYou can't alpha time that.",1) end
end
if not alfatext:match("@") then
-- GUI
local note=''
if #sel>1 then note=note..'\n"Alpha Time" will be applined only to first line.' end
atGUI={{x=0,y=0,width=5,height=8,class="textbox",name="alfa",value=alfatext},
{x=0,y=8,class="label",label="Break the text with 'Enter' the way it should be alpha-timed. (lines selected: "..#sel..")"..note},}
ATP,rez=ADD(atGUI,{"Alpha Text","Alpha Time","Cancel"},{ok='Alpha Text',close='Cancel'})
if ATP=="Cancel" then ak() end
data=rez.alfa
else
data=alfatext:gsub("@","\n")
ATP="Alpha Time"
end
if not data:match("\n") then data=data:gsub(" "," \n") end
-- sort data into a table
altab={} data=data.."\n"
ac=1
data2=""
for al in data:gmatch("(.-\n)") do
al2=al:gsub("\n","{"..ac.."}") ac=ac+1 data2=data2..al2
if al~="" then
table.insert(altab,al2)
end
end
-- apply alpha text
if ATP=="Alpha Text" then
if #altab~=#sel then t_error("Mismatch: "..#sel.." selected lines, "..#altab.." alpha text lines",1) end
for z,i in ipairs(sel) do
altxt=""
for a=1,z do altxt=altxt..altab[a] end
line=subs[i]
text=line.text
if altab[z]~=nil then
tags=text:match(STAG) or ""
text=data2
:gsub("\n","")
:gsub(esc(altxt),altxt.."{\\alpha&HFF&}")
:gsub("({\\alpha&HFF&}.-){\\alpha&HFF&}","%1")
:gsub("{\\alpha&HFF&}$","")
:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}")
:gsub("{%d+}","")
:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}")
text=tags..text
end
line.text=text
subs[i]=line
end
end
-- apply alpha text + split line
if ATP=="Alpha Time" then
line=subs[sel[1]]
start=line.start_time
endt=line.end_time
dur=endt-start
f=dur/#altab
for a=#altab,1,-1 do
altxt=""
altxt=altxt..altab[a]
line.text=line.text:gsub("@","")
line2=line
tags=line2.text:match(STAG) or ""
line2.text=data2
:gsub("\n","")
:gsub(esc(altxt),altxt.."{\\alpha&HFF&}")
:gsub("({\\alpha&HFF&}.-){\\alpha&HFF&}","%1")
:gsub("{\\alpha&HFF&}$","")
:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}")
:gsub("{%d+}","")
line2.text=tags..line2.text
line2.start_time=start+f*(a-1)
line2.end_time=start+f+f*(a-1)
subs.insert(sel[1]+1,line2)
end
subs.delete(sel[1])
end
end
-- StrikeAlpha -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function strikealpha(subs,sel)
-- GUI for {~}
local G={
{x=0,y=0,class="floatedit",name="xx",value=0,hint="main value"},
{x=1,y=0,class="checkbox",name="Y",label="diff. Y value",hint="Check if you want a different value for yshad or fscy"},
{x=2,y=0,class="floatedit",name="yy",value=0,hint="optional Y value"},
{x=0,y=1,width=3,class="label",label="Set a value for tags to replace {~}. Choose which tags to use."},
}
local btn={"fscx+fsxy","xshad+yshad","fax","Cancel"}
local SATAG
for z,i in ipairs(sel) do
progress("Striking "..z.."/"..#sel)
l=subs[i]
t=l.text
-- {~} replacer
repeat t,r=t:gsub("{~}(%b{})","%1{~}"):gsub("{~}(\\N)","%1{~}") until r==0
t=t:gsub("{~}",function(a) if SATAG then return SATAG end
SAP,sar=ADD(G,btn,{ok='fscx+fsxy',close='Cancel'})
if SAP=="Cancel" then ak() end
local xx,yy
xx=sar.xx
yy=sar.xx
if sar.Y then yy=sar.yy end
if SAP=="fscx+fsxy" then SATAG="{\\fscx"..xx.."\\fscy"..yy.."}" end
if SAP=="xshad+yshad" then SATAG="{\\xshad"..xx.."\\yshad"..yy.."}" end
if SAP=="fax" then SATAG="{\\fax"..xx.."}" end
return SATAG
end,1)
-- {•} replacer
repeat t,r=t:gsub("{•}({[^}]*})","%1{•}") until r==0
t2=t:gsub("{•([^}]+)}$","%1"):gsub("{•([^}]+)}([^}{]*)({?)","{•%1%2}%3"):gsub("{•}([^ {}]+%s*)","{•%1}")
if t2==t then t=t:gsub("{•([^{}]+)}","%1") else t=t2 end
-- alphas
t=t
:gsub("\\s1","\\alpha&H00&")
:gsub("\\s0","\\alpha&HFF&")
:gsub("\\u1","\\alpha&HFF&")
:gsub("\\u0","\\alpha&H00&")
:gsub("([1234])@(%x%x)@","{\\%1a&H%2&}")
:gsub("@(%x%x)@","{\\alpha&H%1&}")
:gsub("([1234])@0","{\\%1a&H00&}")
:gsub("@0","{\\alpha&H00&}")
:gsub("([1234])@","{\\%1a&HFF&}")
:gsub("@","{\\alpha&HFF&}")
:gsub("{(\\[^}]-)}{(\\[^}]-)}","{%1%2}")
if l.text==t then
t=t:gsub(ATAG,function(a) repeat a,r=a:gsub("(\\[1234]a%b&&)(.-)(\\alpha%b&&)","%2%3%1") until r==0 return a end)
end
l.text=t
subs[i]=l
end
end
-- SvartAlfa -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function svartalfa(subs,sel)
alv={}
elf=''
-- collect tags
for z,i in ipairs(sel) do
local l=subs[i]
local t=l.text
for tag in t:gmatch("\\%w+a%b&&") do
if not elf:match(tag) then table.insert(alv,tag) elf=elf..tag end
end
end
if #alv==0 then t_error("No alpha tags found in selected lines.",1) end
table.sort(alv)
-- GUI
local G={{x=0,y=0,width=2,class='label',label='Set (hex) values for replacements...'}}
for i=1,#alv do
local tg,tp,val
tg=alv[i]
tp,val=tg:match("(\\%w+)&H(%x%x)&")
table.insert(G,{x=0,y=i,class='label',label=tp..': replace '..val..' with:'})
table.insert(G,{x=1,y=i,class='edit',name=tp..val,value=val})
if i==smax then table.insert(G,{x=0,y=i+1,width=2,class='label',label='Maximum of '..smax..' items reached. (Total: '..#alv..')'}) break end
end
local btn={'OK','Nope'}
SP,rez=ADD(G,btn,{ok='OK',close='Nope'})
if SP=='Nope' then ak() end
-- replacements
for z,i in ipairs(sel) do
local l=subs[i]
local t=l.text
if t:match'\\%w+a%b&&' then
for a=1,#alv do
local tg,tp,val,val2
tg=alv[a]
tp,val=tg:match("(\\%w+)&H(%x%x)&")
val2=rez[tp..val]
if val2 and val2:match"^%x%x$" and val2~=val then
t=t:gsub(tg,tp.."&H"..val2.."&")
end
if a==smax then break end
end
l.text=t
subs[i]=l
end
end
end
-- Betastrike -- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function betastrike(subs,sel)
for z,i in ipairs(sel) do
progress("Striking "..z.."/"..#sel)
l=subs[i]
t=l.text
-- reverse words \s1 ... \s0
t,r=t:gsub("({[^}]*\\s1[^}]*} *)(.+ .+)( *{[^}]*\\s0}[^}]*)",function(a,b,c)
tab={}
for w in b:gmatch("%S+") do
table.insert(tab,1,w)
end
d=table.concat(tab,' ')
return a..d..c
end)
if r>0 then t=t:gsub("\\s[01]",""):gsub("{}","") end
-- reverse segments \u1 ... \u0 ...
t,r=t:gsub("(\\u1})(.-)( *)({[^}]*\\u0})(.*)",function(a,b,s,c,d)
return a..d..s..c..b
end)
if r>0 then t=t:gsub("\\u[01]",""):gsub("{}","") end
-- flip {~}... / {•}...
t,r=t:gsub("({•}%b{})(.-)({~}%b{})",function(a,b,c) return c..b..a end)
if r==0 then t=t:gsub("({~}%b{})(.-)({•}%b{})",function(a,b,c) return c..b..a end) end
t,r=t:gsub("({•}[%w']+)(.-)({~}[%w']+)",function(a,b,c) return c..b..a end)
if r==0 then t=t:gsub("({~}[%w']+)(.-)({•}[%w']+)",function(a,b,c) return c..b..a end) end
t,r=t:gsub("({•}[%w']+)(.-)({~}[%w']+)",function(a,b,c) return c..b..a end)
if r==0 then t=t:gsub("({~}[%w']+)(.-)({•}[%w']+)",function(a,b,c) return c..b..a end) end
t,r=t:gsub("({~})([%w']+.-)({•}%b{})",function(a,b,c) return c..b..a end)
if r==0 then t=t:gsub("({•}%b{})(.-)({~})([%w']+)",function(a,b,c,d) return c..b..a..d end) end
t,r=t:gsub("({•})([%w']+.-)({~}%b{})",function(a,b,c) return c..b..a end)