-
Notifications
You must be signed in to change notification settings - Fork 1
/
effect.go
671 lines (591 loc) · 15.6 KB
/
effect.go
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
// Package eyecandy read/write SSA/ASS Subtitle Script
package eyecandy
import (
"fmt"
"math"
"regexp"
"strings"
"golang.org/x/image/font"
"github.com/Alquimista/eyecandy/asstime"
"github.com/Alquimista/eyecandy/reader"
"github.com/Alquimista/eyecandy/utils"
// "github.com/Alquimista/eyecandy/writer"
writer "github.com/Alquimista/eyecandy/writer2"
)
const (
// AlignBottomLeft Bottom Left SSA numbered Alignment
AlignBottomLeft int = 1 + iota
// AlignBottomCenter Bottom Center SSA numbered Alignment
AlignBottomCenter
// AlignBottomRight Bottom Right SSA numbered Alignment
AlignBottomRight
// AlignMiddleLeft Middle Left SSA numbered Alignment
AlignMiddleLeft
// AlignMiddleCenter Middle Center SSA numbered Alignment
AlignMiddleCenter
// AlignMiddleRight Middle Right SSA numbered Alignment
AlignMiddleRight
// AlignTopLeft Top Left SSA numbered Alignment
AlignTopLeft
// AlignTopCenter Top Left SSA numbered Alignment
AlignTopCenter
// AlignTopRight Top Right SSA numbered Alignment
AlignTopRight
)
var reStripTags = regexp.MustCompile(`({[\s\w\d\\-]+})*`)
var reStripTags2 = regexp.MustCompile(`({[^k]+})*`)
var reKara = regexp.MustCompile(
`{\\k[of]?(?P<duration>\d+)` + // k duration in centiseconds
`(?:\-)*(?P<inline>[\w\d]+)*` + // inline
`}(?P<text>[^\{\}]*)`) //text
func StripSSATags(text string) string {
return strings.TrimSpace(reStripTags.ReplaceAllString(text, ""))
}
func StripSSATagsNotKDur(text string) string {
return strings.TrimSpace(reStripTags2.ReplaceAllString(text, ""))
}
func GetSyls(text string) [][]string {
return reKara.FindAllStringSubmatch(StripSSATagsNotKDur(text), -1)
}
type Dialog struct {
Layer int
StartTime int
EndTime int
StyleName string
Actor string
Effect string
Text string
Tags string
Duration int
MidTime int
Style *reader.Style
Comment bool
Width float64
Height float64
Size [2]float64
XFix float64
X float64
Y float64
Top float64
Middle float64
Bottom float64
Left float64
Center float64
Right float64
}
// Line Represent the subtitle"s lines.
type Line struct {
Dialog
Kara string
SylN int
CharN int
syls [][]string
fontFace font.Face
resolution [2]int
}
// Syl Represent the subtitle"s lines.
type Syl struct {
Dialog
Inline string
}
// Char Represent the subtitle"s lines.
type Char struct {
Dialog
Inline string
SylStartTime int
SylEndTime int
SylMidEndTime int
SylDuration int
}
// Chars list all characters in a Line
func (d *Line) Chars() (chars []*Char) {
start, end, x, dur := 0, 0, 0.0, 0
for _, s := range d.Syls() {
curX := float64(s.Left)
lineStart := s.StartTime
lineEnd := s.EndTime
charN := utils.LenString(s.Text)
// For syls of one char
if charN == 1 || charN == 0 {
dur = s.Duration
} else {
dur = int(s.Duration / charN)
}
for i, c := range s.Text {
text := string(c)
start = lineStart
lineStart += dur
width, _ := utils.MeasureString(d.fontFace, text)
width *= d.Style.Scale[0] / 100.0
// width += 2.0*s.Style.Bord + s.Style.Shadow
middlewidth := float64(width) / 2.0
cleft := float64(curX)
ccenter := cleft + float64(middlewidth)
cright := cleft + float64(width)
align := d.Style.Alignment
switch align {
case 1, 4, 7: // left
x = cleft
case 2, 5, 8: // center
x = ccenter
case 3, 6, 9: // right
x = cright
}
if i == charN-1 {
// Ensure that the end time and the width of the last char
// is the same that the end time and width of the syl
end = lineEnd
} else {
end = lineStart
}
c := &Char{
Dialog: Dialog{
Layer: d.Layer,
Style: d.Style,
StyleName: d.StyleName,
Actor: d.Actor,
Effect: d.Effect,
Tags: d.Tags,
Comment: d.Comment,
StartTime: start,
EndTime: end,
Duration: dur,
MidTime: end - start,
Text: text,
Width: float64(width),
Height: d.Height, //+ 2.0*d.Style.Bord + d.Style.Shadow,
Size: [2]float64{float64(width), d.Height},
X: float64(x),
Y: float64(d.Y),
Top: float64(d.Top),
Middle: float64(d.Middle),
Bottom: float64(d.Bottom),
Left: float64(cleft),
Center: float64(ccenter),
Right: float64(cright),
},
Inline: s.Inline,
SylStartTime: s.StartTime,
SylEndTime: s.EndTime,
SylMidEndTime: s.MidTime,
SylDuration: s.Duration,
}
chars = append(chars, c)
curX += width
}
}
return chars
}
// Syls list all syllables in a Line
func (d *Line) Syls() (syls []*Syl) {
lineStart := d.StartTime
lineEnd := d.EndTime
end := 0
fontFace := d.fontFace
spaceWidth, _ := utils.MeasureString(fontFace, " ")
spaceWidth *= d.Style.Scale[0] / 100.0
//spaceWidth += 2*d.Style.Bord + d.Style.Shadow
curX := d.Left + float64(d.SylN)*-d.XFix/2.0
maxWidth := 0.0
sumHeight := 0.0
resx, resy := float64(d.resolution[0]), float64(d.resolution[1])
for i, dlg := range d.syls {
duration, inline, text := dlg[1], dlg[2], dlg[3]
dur := utils.Str2int(duration) * 10 // cs to ms
// Absolute times
start := lineStart
lineStart += dur
if i == d.SylN-1 {
// Ensure that the end time and the width of the last syl
// is the same that the end time and width of the line
end = lineEnd
} else {
end = lineStart
}
strippedText, preSpace, postSpace := utils.TrimSpaceCount(text)
width, _ := utils.MeasureString(fontFace, strippedText)
width *= d.Style.Scale[0] / 100.0
//width += 2.0*d.Style.Bord + d.Style.Shadow
height := d.Height
middleheight := float64(height) / 2.0
middlewidth := float64(width) / 2.0
align := d.Style.Alignment
curX += float64(preSpace) * spaceWidth
sleft := float64(curX)
scenter := sleft + middlewidth
sright := sleft + width
x := 0.0
y := 0.0
stop := 0.0
smid := 0.0
sbot := 0.0
maxWidth = math.Max(maxWidth, width)
sumHeight += height
// line x
if align > 6 || align < 4 {
switch align {
case 1, 7: // left
x = sleft
case 2, 8: // center
x = scenter
case 3, 9: // right
x = sright
}
curX += width + float64(postSpace)*spaceWidth +
float64(d.Style.Spacing) + d.XFix
} else { // vertical alignment
xFix := (maxWidth - width) / 2.0
switch align {
case 4: // left
sleft = d.Left + xFix
scenter = sleft + middlewidth
sright = sleft + width
x = sleft
case 5: // center
sleft = resx/2.0 - middlewidth
scenter = sleft + middlewidth
sright = sleft + width
x = scenter
case 6: // right
sleft = d.Right - width - xFix
scenter = sleft + middlewidth
sright = sleft + width
x = sright
}
}
curY := resy/2.0 - sumHeight/2.0 + float64(d.Style.Spacing)
// line y
if align > 6 || align < 4 {
stop = d.Top
smid = d.Middle
sbot = d.Bottom
y = d.Y
} else { // vertical alignment
stop = curY
smid = stop + middleheight
sbot = stop + height
y = smid
// curY += height
}
if text != "" {
s := &Syl{
Dialog: Dialog{
Layer: d.Layer,
Style: d.Style,
StyleName: d.StyleName,
Actor: d.Actor,
Effect: d.Effect,
Tags: d.Tags,
Comment: d.Comment,
StartTime: start,
EndTime: end,
Duration: dur,
MidTime: end - start,
Text: strippedText,
Width: float64(width),
Height: float64(height),
Size: [2]float64{float64(width), float64(height)},
X: float64(x),
Y: float64(y),
Top: float64(stop),
Middle: float64(smid),
Bottom: float64(sbot),
Left: float64(sleft),
Center: float64(scenter),
Right: float64(sright),
},
Inline: inline,
}
syls = append(syls, s)
}
}
return syls
}
// Script represent the SSA Script
type Script struct {
Resolution [2]int // WIDTH, HEIGHT
VideoPath string
VideoZoom float64
VideoPosition int
VideoAR float64
MetaFilename string
MetaTitle string
MetaOriginalScript string
MetaTranslation string
MetaTiming string
Audio string
LineN int
Shift int
XFix float64
scriptIn *reader.Script
scriptOut *writer.Script
fontFace map[string]font.Face
}
// Lines List all the lines in a Script
func (fx *Script) Lines() (dialogs []*Line) {
resx, resy := float64(fx.Resolution[0]), float64(fx.Resolution[1])
for _, dlg := range fx.scriptIn.Dialog.NotCommented() {
end := asstime.SSAtoMS(dlg.EndTime)
start := asstime.SSAtoMS(dlg.StartTime)
duration := end - start
text := StripSSATags(dlg.Text)
fontFace := fx.fontFace[dlg.StyleName]
width, height := utils.MeasureString(fontFace, text)
width *= dlg.Style.Scale[0] / 100.0
//width += 2*dlg.Style.Bord + dlg.Style.Shadow
height *= dlg.Style.Scale[1] / 100.0
align := dlg.Style.Alignment
ml, mr, mv := float64(dlg.Style.Margin[0]),
float64(dlg.Style.Margin[1]),
float64(dlg.Style.Margin[2])
// Alignment
middleheight := float64(height) / 2.0
middlewidth := float64(width) / 2.0
x := 0.0
y := 0.0
ltop := 0.0
lmid := 0.0
lbot := 0.0
lleft := 0.0
lcenter := 0.0
lright := 0.0
// line x
switch align {
case 1, 4, 7: // left
lleft = ml
lcenter = lleft + middlewidth
lright = lleft + width
x = lleft
case 2, 5, 8: // center
lleft = resx/2.0 - middlewidth
lcenter = lleft + middlewidth
lright = lleft + width
x = lcenter
case 3, 6, 9: // right
lleft = resx - mr - width
lcenter = lleft + middlewidth
lright = lleft + width
x = lright
}
// line y
switch align {
case 7, 8, 9: // top
ltop = mv
lmid = ltop + middleheight
lbot = ltop + height
y = ltop
case 4, 5, 6: // middle
lmid = resy / 2
ltop = lmid - middleheight
lbot = lmid + middleheight
y = lmid
case 1, 2, 3: // bottom
lbot = resy - mv
lmid = lbot - middleheight
ltop = lbot - height
y = lbot
}
syls := GetSyls(dlg.Text)
charN := 0
for _, s := range syls {
syltext := strings.TrimSpace(s[3])
if syltext != "" {
for range syltext {
charN++
}
}
}
d := &Line{
Dialog: Dialog{
Layer: dlg.Layer,
StartTime: start,
EndTime: end,
Duration: duration,
MidTime: start + int(duration/2),
Style: dlg.Style,
StyleName: dlg.StyleName,
Actor: dlg.Actor,
Effect: dlg.Effect,
Text: text,
Tags: dlg.Tags,
Comment: dlg.Comment,
Width: float64(width),
Height: float64(height),
Size: [2]float64{float64(width), float64(height)},
XFix: fx.XFix,
X: float64(x),
Y: float64(y),
Top: float64(ltop),
Middle: float64(lmid),
Bottom: float64(lbot),
Left: float64(lleft),
Center: float64(lcenter),
Right: float64(lright),
},
Kara: dlg.Text,
SylN: len(syls),
CharN: charN,
syls: syls,
fontFace: fontFace,
resolution: fx.Resolution,
}
dialogs = append(dialogs, d)
}
return dialogs
}
// GetStyle get a style corresponding to name provided
func (fx *Script) GetStyle(name string) (*reader.Style, bool) {
style, ok := fx.Styles()[name]
return style, ok
}
// Styles list styles use the Script
func (fx *Script) Styles() map[string]*reader.Style {
return fx.scriptIn.StyleUsed
}
// AddStyle append a Style to Script
func (fx *Script) AddStyle(sty *writer.Style) {
fx.scriptIn.StyleUsed[sty.Name] = reader.NewStyle(sty.Name)
fx.scriptOut.AddStyle(sty)
}
// CopyLine create a copy of the current Line
func (fx *Script) CopyLine(dialog *Line) Line {
return *dialog
}
// CopySyl create a copy of the current Syl
func (fx *Script) CopySyl(dialog *Syl) Syl {
return *dialog
}
// CopyChar create a copy of the current Char
func (fx *Script) CopyChar(dialog *Char) Char {
return *dialog
}
// Add append a Dialog (Syl, Char, Line) to Script
func (fx *Script) Add(dialog interface{}) {
switch dlg := dialog.(type) {
case Line:
d := NewDialog(dlg.Text)
d.Layer = dlg.Layer
d.Start = asstime.MStoSSA(dlg.StartTime + fx.Shift)
d.End = asstime.MStoSSA(dlg.EndTime + fx.Shift)
d.StyleName = dlg.StyleName
d.Actor = dlg.Actor
d.Effect = dlg.Effect
d.Tags = dlg.Tags
d.Comment = dlg.Comment
fx.scriptOut.AddDialog(d)
case Syl:
d := NewDialog(dlg.Text)
d.Layer = dlg.Layer
d.Start = asstime.MStoSSA(dlg.StartTime + fx.Shift)
d.End = asstime.MStoSSA(dlg.EndTime + fx.Shift)
d.StyleName = dlg.StyleName
d.Actor = dlg.Actor
d.Effect = dlg.Effect
d.Tags = dlg.Tags
d.Comment = dlg.Comment
fx.scriptOut.AddDialog(d)
case Char:
d := NewDialog(dlg.Text)
d.Layer = dlg.Layer
d.Start = asstime.MStoSSA(dlg.StartTime + fx.Shift)
d.End = asstime.MStoSSA(dlg.EndTime + fx.Shift)
d.StyleName = dlg.StyleName
d.Actor = dlg.Actor
d.Effect = dlg.Effect
d.Tags = dlg.Tags
d.Comment = dlg.Comment
fx.scriptOut.AddDialog(d)
default:
fmt.Println("Not admitted object")
}
}
// Save create the final script file (.ass)
func (fx *Script) Save(fn string) {
fx.scriptOut.Resolution = fx.Resolution
fx.scriptOut.VideoPath = fx.VideoPath
fx.scriptOut.VideoZoom = fx.VideoZoom
fx.scriptOut.VideoPosition = fx.VideoPosition
fx.scriptOut.VideoAR = fx.VideoAR
fx.scriptOut.MetaFilename = fx.MetaFilename
fx.scriptOut.MetaTitle = fx.MetaTitle
fx.scriptOut.MetaOriginalScript = fx.MetaOriginalScript
fx.scriptOut.MetaTranslation = fx.MetaTranslation
fx.scriptOut.MetaTiming = fx.MetaTiming
fx.scriptOut.Audio = fx.Audio
fx.scriptOut.Save(fn)
}
// NewEffect create a new script
func NewEffect(inFN string) *Script {
input := reader.Read(inFN)
output := writer.NewScript()
fontFace := make(map[string]font.Face)
ssampling := 1
for _, style := range input.StyleUsed {
s := NewStyle(style.Name)
s.Name = style.Name
s.FontName = style.FontName
s.FontSize = style.FontSize
s.Color = style.Color
s.Bold = style.Bold
s.Italic = style.Italic
s.Scale = style.Scale
s.Angle = style.Angle
s.Bord = style.Bord
s.Shadow = style.Shadow
s.Alignment = style.Alignment
s.Margin = style.Margin
output.AddStyle(s)
ff, err := utils.LoadFont(s.FontName, s.FontSize*ssampling)
if err != nil {
panic(err)
}
fontFace[s.Name] = ff
}
// Add the original karaoke commented by default in the script
// This help to jump to the wanted line in the preview in Aegisub,
// and/or keep a backup of the timed subs
dok := NewDialog("### Original Karaoke ###")
dok.Comment = true
output.AddDialog(dok)
LineN := 0
for _, dlg := range input.Dialog {
d := writer.NewDialog(dlg.Text)
d.Layer = dlg.Layer
d.Start = dlg.StartTime
d.End = dlg.EndTime
d.StyleName = dlg.StyleName
d.Actor = dlg.Actor
d.Effect = dlg.Effect
d.Comment = true
output.AddDialog(d)
LineN++
}
dke := NewDialog("### Karaoke Effect ###")
dke.Comment = true
output.AddDialog(dke)
return &Script{
Resolution: input.Resolution,
VideoPath: input.VideoPath,
VideoZoom: input.VideoZoom,
VideoPosition: input.VideoPosition,
VideoAR: input.VideoAR,
MetaFilename: input.MetaFilename,
MetaTitle: input.MetaTitle,
MetaOriginalScript: input.MetaOriginalScript,
MetaTranslation: input.MetaTranslation,
MetaTiming: input.MetaTiming,
Audio: input.Audio,
LineN: LineN,
fontFace: fontFace,
scriptIn: input,
scriptOut: output,
}
}
// NewStyle create a new Style
func NewStyle(name string) *writer.Style {
return writer.NewStyle(name)
}
// NewDialog create a new Dialog
func NewDialog(text string) *writer.Dialog {
return writer.NewDialog(text)
}