mirror of
https://github.com/zoriya/go-astisub.git
synced 2026-08-16 02:33:21 +00:00
TTML: Fixed improper spacing between captions (#43)
* 1. Fixed improper spacing between captions while converting TTML -> VTT 2. Added String To Pointer utility. 3. Converted TTML style attributes to pointers 4. Fixed Webvtt not using parent style attributes in region * 1. Fixed Test cases 2. Fixed SRT and STL strings join * 1. Removed Util 2. Added Regex to remove multiple space 3. In ttml.go update 'getStyleAttributes()' to 'newStyleAttributesFromTTMLInStyleAttributes()' 4. Using go-astikit to convert string to string pointer. * Improper spacing fix * Improper spacing fix * Added Space in TTML out. Added comments * Added space to fix unit test * update go.sum * update go.sum * Update go.sum * Update go.sum
This commit is contained in:
@@ -104,7 +104,7 @@ func ReadFromSRT(i io.Reader) (o *Subtitles, err error) {
|
||||
o.Items = append(o.Items, s)
|
||||
} else {
|
||||
// Add text
|
||||
s.Lines = append(s.Lines, Line{Items: []LineItem{{Text: line}}})
|
||||
s.Lines = append(s.Lines, Line{Items: []LineItem{{Text: strings.TrimSpace(line)}}})
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
@@ -927,7 +927,7 @@ func newSSAEventFromItem(i Item) (e *ssaEvent) {
|
||||
if len(l.VoiceName) > 0 {
|
||||
e.name = l.VoiceName
|
||||
}
|
||||
lines = append(lines, strings.Join(items, ""))
|
||||
lines = append(lines, strings.Join(items, " "))
|
||||
}
|
||||
e.text = strings.Join(lines, "\\n")
|
||||
return
|
||||
@@ -1014,7 +1014,7 @@ func newSSAEventFromString(header, content string, format map[int]string) (e *ss
|
||||
e.style = item
|
||||
}
|
||||
case ssaEventFormatNameText:
|
||||
e.text = item
|
||||
e.text = strings.TrimSpace(item)
|
||||
}
|
||||
// Marked
|
||||
case ssaEventFormatNameMarked:
|
||||
|
||||
+48
-26
@@ -3,6 +3,7 @@ package astisub
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/asticode/go-astikit"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -207,30 +208,30 @@ type StyleAttributes struct {
|
||||
TeletextSpacesAfter *int
|
||||
TeletextSpacesBefore *int
|
||||
// TODO Use pointers with real types below
|
||||
TTMLBackgroundColor string // https://htmlcolorcodes.com/fr/
|
||||
TTMLColor string
|
||||
TTMLDirection string
|
||||
TTMLDisplay string
|
||||
TTMLDisplayAlign string
|
||||
TTMLExtent string
|
||||
TTMLFontFamily string
|
||||
TTMLFontSize string
|
||||
TTMLFontStyle string
|
||||
TTMLFontWeight string
|
||||
TTMLLineHeight string
|
||||
TTMLOpacity string
|
||||
TTMLOrigin string
|
||||
TTMLOverflow string
|
||||
TTMLPadding string
|
||||
TTMLShowBackground string
|
||||
TTMLTextAlign string
|
||||
TTMLTextDecoration string
|
||||
TTMLTextOutline string
|
||||
TTMLUnicodeBidi string
|
||||
TTMLVisibility string
|
||||
TTMLWrapOption string
|
||||
TTMLWritingMode string
|
||||
TTMLZIndex int
|
||||
TTMLBackgroundColor *string // https://htmlcolorcodes.com/fr/
|
||||
TTMLColor *string
|
||||
TTMLDirection *string
|
||||
TTMLDisplay *string
|
||||
TTMLDisplayAlign *string
|
||||
TTMLExtent *string
|
||||
TTMLFontFamily *string
|
||||
TTMLFontSize *string
|
||||
TTMLFontStyle *string
|
||||
TTMLFontWeight *string
|
||||
TTMLLineHeight *string
|
||||
TTMLOpacity *string
|
||||
TTMLOrigin *string
|
||||
TTMLOverflow *string
|
||||
TTMLPadding *string
|
||||
TTMLShowBackground *string
|
||||
TTMLTextAlign *string
|
||||
TTMLTextDecoration *string
|
||||
TTMLTextOutline *string
|
||||
TTMLUnicodeBidi *string
|
||||
TTMLVisibility *string
|
||||
TTMLWrapOption *string
|
||||
TTMLWritingMode *string
|
||||
TTMLZIndex *int
|
||||
WebVTTAlign string
|
||||
WebVTTItalics bool
|
||||
WebVTTLine string
|
||||
@@ -261,11 +262,32 @@ func (sa *StyleAttributes) propagateSTLAttributes() {
|
||||
|
||||
func (sa *StyleAttributes) propagateTeletextAttributes() {
|
||||
if sa.TeletextColor != nil {
|
||||
sa.TTMLColor = "#" + sa.TeletextColor.TTMLString()
|
||||
sa.TTMLColor = astikit.StrPtr("#" + sa.TeletextColor.TTMLString())
|
||||
}
|
||||
}
|
||||
|
||||
func (sa *StyleAttributes) propagateTTMLAttributes() {}
|
||||
func (sa *StyleAttributes) propagateTTMLAttributes() {
|
||||
if sa.TTMLTextAlign != nil {
|
||||
sa.WebVTTAlign = *sa.TTMLTextAlign
|
||||
}
|
||||
}
|
||||
|
||||
func (sa *StyleAttributes) propagateTTMLRegionAttributes() {
|
||||
if sa.TTMLExtent != nil {
|
||||
lineHeight := 5 //assuming height of line as 5.33vh
|
||||
dimensions := strings.Split(*sa.TTMLExtent, " ")
|
||||
sa.WebVTTWidth = dimensions[0]
|
||||
if height, err := strconv.Atoi(strings.ReplaceAll(dimensions[1], "%", "")); err == nil {
|
||||
sa.WebVTTLines = height / lineHeight
|
||||
}
|
||||
|
||||
}
|
||||
if sa.TTMLOrigin != nil {
|
||||
sa.WebVTTRegionAnchor = "0%,0%"
|
||||
sa.WebVTTViewportAnchor = strings.ReplaceAll(strings.TrimSpace(*sa.TTMLOrigin), " ", ",")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (sa *StyleAttributes) propagateWebVTTAttributes() {}
|
||||
|
||||
|
||||
+12
-12
@@ -79,56 +79,56 @@ func TestParseTeletextRow(t *testing.T) {
|
||||
TeletextColor: ColorBlack,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#000000",
|
||||
TTMLColor: astikit.StrPtr("#000000"),
|
||||
}},
|
||||
{Text: "red", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorRed,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ff0000",
|
||||
TTMLColor: astikit.StrPtr("#ff0000"),
|
||||
}},
|
||||
{Text: "green", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorGreen,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#008000",
|
||||
TTMLColor: astikit.StrPtr("#008000"),
|
||||
}},
|
||||
{Text: "yellow", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorYellow,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ffff00",
|
||||
TTMLColor: astikit.StrPtr("#ffff00"),
|
||||
}},
|
||||
{Text: "blue", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorBlue,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#0000ff",
|
||||
TTMLColor: astikit.StrPtr("#0000ff"),
|
||||
}},
|
||||
{Text: "magenta", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorMagenta,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ff00ff",
|
||||
TTMLColor: astikit.StrPtr("#ff00ff"),
|
||||
}},
|
||||
{Text: "cyan", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorCyan,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#00ffff",
|
||||
TTMLColor: astikit.StrPtr("#00ffff"),
|
||||
}},
|
||||
{Text: "white", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorWhite,
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ffffff",
|
||||
TTMLColor: astikit.StrPtr("#ffffff"),
|
||||
}},
|
||||
{Text: "double height", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorWhite,
|
||||
TeletextDoubleHeight: astikit.BoolPtr(true),
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ffffff",
|
||||
TTMLColor: astikit.StrPtr("#ffffff"),
|
||||
}},
|
||||
{Text: "double width", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorWhite,
|
||||
@@ -136,7 +136,7 @@ func TestParseTeletextRow(t *testing.T) {
|
||||
TeletextDoubleWidth: astikit.BoolPtr(true),
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ffffff",
|
||||
TTMLColor: astikit.StrPtr("#ffffff"),
|
||||
}},
|
||||
{Text: "double size", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorWhite,
|
||||
@@ -145,7 +145,7 @@ func TestParseTeletextRow(t *testing.T) {
|
||||
TeletextDoubleSize: astikit.BoolPtr(true),
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ffffff",
|
||||
TTMLColor: astikit.StrPtr("#ffffff"),
|
||||
}},
|
||||
{Text: "reset", InlineStyle: &StyleAttributes{
|
||||
TeletextColor: ColorWhite,
|
||||
@@ -154,7 +154,7 @@ func TestParseTeletextRow(t *testing.T) {
|
||||
TeletextDoubleSize: astikit.BoolPtr(false),
|
||||
TeletextSpacesAfter: astikit.IntPtr(0),
|
||||
TeletextSpacesBefore: astikit.IntPtr(0),
|
||||
TTMLColor: "#ffffff",
|
||||
TTMLColor: astikit.StrPtr("#ffffff"),
|
||||
}},
|
||||
}, i.Lines[0].Items)
|
||||
}
|
||||
|
||||
Vendored
+2
-2
@@ -23,8 +23,8 @@
|
||||
<p begin="00:02:04.080" end="00:02:07.120" region="region_2">
|
||||
<span>MAN:</span>
|
||||
<br></br>
|
||||
<span>How did we</span>
|
||||
<span style="style_1" tts:color="green">end up</span>
|
||||
<span>How did we </span>
|
||||
<span style="style_1" tts:color="green">end up </span>
|
||||
<span>here?</span>
|
||||
</p>
|
||||
<p begin="00:02:12.160" end="00:02:15.200" region="region_1">
|
||||
|
||||
@@ -70,35 +70,48 @@ type TTMLInMetadata struct {
|
||||
|
||||
// TTMLInStyleAttributes represents input TTML style attributes
|
||||
type TTMLInStyleAttributes struct {
|
||||
BackgroundColor string `xml:"backgroundColor,attr,omitempty"`
|
||||
Color string `xml:"color,attr,omitempty"`
|
||||
Direction string `xml:"direction,attr,omitempty"`
|
||||
Display string `xml:"display,attr,omitempty"`
|
||||
DisplayAlign string `xml:"displayAlign,attr,omitempty"`
|
||||
Extent string `xml:"extent,attr,omitempty"`
|
||||
FontFamily string `xml:"fontFamily,attr,omitempty"`
|
||||
FontSize string `xml:"fontSize,attr,omitempty"`
|
||||
FontStyle string `xml:"fontStyle,attr,omitempty"`
|
||||
FontWeight string `xml:"fontWeight,attr,omitempty"`
|
||||
LineHeight string `xml:"lineHeight,attr,omitempty"`
|
||||
Opacity string `xml:"opacity,attr,omitempty"`
|
||||
Origin string `xml:"origin,attr,omitempty"`
|
||||
Overflow string `xml:"overflow,attr,omitempty"`
|
||||
Padding string `xml:"padding,attr,omitempty"`
|
||||
ShowBackground string `xml:"showBackground,attr,omitempty"`
|
||||
TextAlign string `xml:"textAlign,attr,omitempty"`
|
||||
TextDecoration string `xml:"textDecoration,attr,omitempty"`
|
||||
TextOutline string `xml:"textOutline,attr,omitempty"`
|
||||
UnicodeBidi string `xml:"unicodeBidi,attr,omitempty"`
|
||||
Visibility string `xml:"visibility,attr,omitempty"`
|
||||
WrapOption string `xml:"wrapOption,attr,omitempty"`
|
||||
WritingMode string `xml:"writingMode,attr,omitempty"`
|
||||
ZIndex int `xml:"zIndex,attr,omitempty"`
|
||||
BackgroundColor *string `xml:"backgroundColor,attr,omitempty"`
|
||||
Color *string `xml:"color,attr,omitempty"`
|
||||
Direction *string `xml:"direction,attr,omitempty"`
|
||||
Display *string `xml:"display,attr,omitempty"`
|
||||
DisplayAlign *string `xml:"displayAlign,attr,omitempty"`
|
||||
Extent *string `xml:"extent,attr,omitempty"`
|
||||
FontFamily *string `xml:"fontFamily,attr,omitempty"`
|
||||
FontSize *string `xml:"fontSize,attr,omitempty"`
|
||||
FontStyle *string `xml:"fontStyle,attr,omitempty"`
|
||||
FontWeight *string `xml:"fontWeight,attr,omitempty"`
|
||||
LineHeight *string `xml:"lineHeight,attr,omitempty"`
|
||||
Opacity *string `xml:"opacity,attr,omitempty"`
|
||||
Origin *string `xml:"origin,attr,omitempty"`
|
||||
Overflow *string `xml:"overflow,attr,omitempty"`
|
||||
Padding *string `xml:"padding,attr,omitempty"`
|
||||
ShowBackground *string `xml:"showBackground,attr,omitempty"`
|
||||
TextAlign *string `xml:"textAlign,attr,omitempty"`
|
||||
TextDecoration *string `xml:"textDecoration,attr,omitempty"`
|
||||
TextOutline *string `xml:"textOutline,attr,omitempty"`
|
||||
UnicodeBidi *string `xml:"unicodeBidi,attr,omitempty"`
|
||||
Visibility *string `xml:"visibility,attr,omitempty"`
|
||||
WrapOption *string `xml:"wrapOption,attr,omitempty"`
|
||||
WritingMode *string `xml:"writingMode,attr,omitempty"`
|
||||
ZIndex *int `xml:"zIndex,attr,omitempty"`
|
||||
}
|
||||
|
||||
// StyleAttributes converts TTMLInStyleAttributes into a StyleAttributes
|
||||
func (s TTMLInStyleAttributes) styleAttributes() (o *StyleAttributes) {
|
||||
o = &StyleAttributes{
|
||||
o = newStyleAttributesFromTTMLInStyleAttributes(s)
|
||||
o.propagateTTMLAttributes()
|
||||
return
|
||||
}
|
||||
|
||||
// StyleAttributes converts TTMLInStyleAttributes for region into a StyleAttributes
|
||||
func (s TTMLInStyleAttributes) regionStyleAttributes() (o *StyleAttributes) {
|
||||
o = newStyleAttributesFromTTMLInStyleAttributes(s)
|
||||
o.propagateTTMLRegionAttributes()
|
||||
return
|
||||
}
|
||||
|
||||
func newStyleAttributesFromTTMLInStyleAttributes(s TTMLInStyleAttributes) *StyleAttributes {
|
||||
return &StyleAttributes{
|
||||
TTMLBackgroundColor: s.BackgroundColor,
|
||||
TTMLColor: s.Color,
|
||||
TTMLDirection: s.Direction,
|
||||
@@ -124,8 +137,6 @@ func (s TTMLInStyleAttributes) styleAttributes() (o *StyleAttributes) {
|
||||
TTMLWritingMode: s.WritingMode,
|
||||
TTMLZIndex: s.ZIndex,
|
||||
}
|
||||
o.propagateTTMLAttributes()
|
||||
return
|
||||
}
|
||||
|
||||
// TTMLInHeader represents an input TTML header
|
||||
@@ -330,7 +341,7 @@ func ReadFromTTML(i io.Reader) (o *Subtitles, err error) {
|
||||
for _, tr := range ttml.Regions {
|
||||
var r = &Region{
|
||||
ID: tr.ID,
|
||||
InlineStyle: tr.TTMLInStyleAttributes.styleAttributes(),
|
||||
InlineStyle: tr.TTMLInStyleAttributes.regionStyleAttributes(),
|
||||
}
|
||||
if len(tr.Style) > 0 {
|
||||
if _, ok := o.Styles[tr.Style]; !ok {
|
||||
@@ -450,30 +461,30 @@ type TTMLOutMetadata struct {
|
||||
|
||||
// TTMLOutStyleAttributes represents output TTML style attributes
|
||||
type TTMLOutStyleAttributes struct {
|
||||
BackgroundColor string `xml:"tts:backgroundColor,attr,omitempty"`
|
||||
Color string `xml:"tts:color,attr,omitempty"`
|
||||
Direction string `xml:"tts:direction,attr,omitempty"`
|
||||
Display string `xml:"tts:display,attr,omitempty"`
|
||||
DisplayAlign string `xml:"tts:displayAlign,attr,omitempty"`
|
||||
Extent string `xml:"tts:extent,attr,omitempty"`
|
||||
FontFamily string `xml:"tts:fontFamily,attr,omitempty"`
|
||||
FontSize string `xml:"tts:fontSize,attr,omitempty"`
|
||||
FontStyle string `xml:"tts:fontStyle,attr,omitempty"`
|
||||
FontWeight string `xml:"tts:fontWeight,attr,omitempty"`
|
||||
LineHeight string `xml:"tts:lineHeight,attr,omitempty"`
|
||||
Opacity string `xml:"tts:opacity,attr,omitempty"`
|
||||
Origin string `xml:"tts:origin,attr,omitempty"`
|
||||
Overflow string `xml:"tts:overflow,attr,omitempty"`
|
||||
Padding string `xml:"tts:padding,attr,omitempty"`
|
||||
ShowBackground string `xml:"tts:showBackground,attr,omitempty"`
|
||||
TextAlign string `xml:"tts:textAlign,attr,omitempty"`
|
||||
TextDecoration string `xml:"tts:textDecoration,attr,omitempty"`
|
||||
TextOutline string `xml:"tts:textOutline,attr,omitempty"`
|
||||
UnicodeBidi string `xml:"tts:unicodeBidi,attr,omitempty"`
|
||||
Visibility string `xml:"tts:visibility,attr,omitempty"`
|
||||
WrapOption string `xml:"tts:wrapOption,attr,omitempty"`
|
||||
WritingMode string `xml:"tts:writingMode,attr,omitempty"`
|
||||
ZIndex int `xml:"tts:zIndex,attr,omitempty"`
|
||||
BackgroundColor *string `xml:"tts:backgroundColor,attr,omitempty"`
|
||||
Color *string `xml:"tts:color,attr,omitempty"`
|
||||
Direction *string `xml:"tts:direction,attr,omitempty"`
|
||||
Display *string `xml:"tts:display,attr,omitempty"`
|
||||
DisplayAlign *string `xml:"tts:displayAlign,attr,omitempty"`
|
||||
Extent *string `xml:"tts:extent,attr,omitempty"`
|
||||
FontFamily *string `xml:"tts:fontFamily,attr,omitempty"`
|
||||
FontSize *string `xml:"tts:fontSize,attr,omitempty"`
|
||||
FontStyle *string `xml:"tts:fontStyle,attr,omitempty"`
|
||||
FontWeight *string `xml:"tts:fontWeight,attr,omitempty"`
|
||||
LineHeight *string `xml:"tts:lineHeight,attr,omitempty"`
|
||||
Opacity *string `xml:"tts:opacity,attr,omitempty"`
|
||||
Origin *string `xml:"tts:origin,attr,omitempty"`
|
||||
Overflow *string `xml:"tts:overflow,attr,omitempty"`
|
||||
Padding *string `xml:"tts:padding,attr,omitempty"`
|
||||
ShowBackground *string `xml:"tts:showBackground,attr,omitempty"`
|
||||
TextAlign *string `xml:"tts:textAlign,attr,omitempty"`
|
||||
TextDecoration *string `xml:"tts:textDecoration,attr,omitempty"`
|
||||
TextOutline *string `xml:"tts:textOutline,attr,omitempty"`
|
||||
UnicodeBidi *string `xml:"tts:unicodeBidi,attr,omitempty"`
|
||||
Visibility *string `xml:"tts:visibility,attr,omitempty"`
|
||||
WrapOption *string `xml:"tts:wrapOption,attr,omitempty"`
|
||||
WritingMode *string `xml:"tts:writingMode,attr,omitempty"`
|
||||
ZIndex *int `xml:"tts:zIndex,attr,omitempty"`
|
||||
}
|
||||
|
||||
// ttmlOutStyleAttributesFromStyleAttributes converts StyleAttributes into a TTMLOutStyleAttributes
|
||||
@@ -637,13 +648,17 @@ func (s Subtitles) WriteToTTML(o io.Writer) (err error) {
|
||||
// Add lines
|
||||
for _, line := range item.Lines {
|
||||
// Loop through line items
|
||||
for _, lineItem := range line.Items {
|
||||
for idx, lineItem := range line.Items {
|
||||
// Init ttml item
|
||||
var ttmlItem = TTMLOutItem{
|
||||
Text: lineItem.Text,
|
||||
TTMLOutStyleAttributes: ttmlOutStyleAttributesFromStyleAttributes(lineItem.InlineStyle),
|
||||
XMLName: xml.Name{Local: "span"},
|
||||
}
|
||||
// condition to avoid adding space as the last character.
|
||||
if idx < len(line.Items)-1 {
|
||||
ttmlItem.Text = ttmlItem.Text + " "
|
||||
}
|
||||
|
||||
// Add style
|
||||
if lineItem.Style != nil {
|
||||
|
||||
+8
-7
@@ -2,6 +2,7 @@ package astisub_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/asticode/go-astikit"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
@@ -18,20 +19,20 @@ func TestTTML(t *testing.T) {
|
||||
assert.Equal(t, &astisub.Metadata{Framerate: 25, Language: astisub.LanguageFrench, Title: "Title test", TTMLCopyright: "Copyright test"}, s.Metadata)
|
||||
// Styles
|
||||
assert.Equal(t, 3, len(s.Styles))
|
||||
assert.Equal(t, astisub.Style{ID: "style_0", InlineStyle: &astisub.StyleAttributes{TTMLColor: "white", TTMLExtent: "100% 10%", TTMLFontFamily: "sansSerif", TTMLFontStyle: "normal", TTMLOrigin: "0% 90%", TTMLTextAlign: "center"}, Style: s.Styles["style_2"]}, *s.Styles["style_0"])
|
||||
assert.Equal(t, astisub.Style{ID: "style_1", InlineStyle: &astisub.StyleAttributes{TTMLColor: "white", TTMLExtent: "100% 13%", TTMLFontFamily: "sansSerif", TTMLFontStyle: "normal", TTMLOrigin: "0% 87%", TTMLTextAlign: "center"}}, *s.Styles["style_1"])
|
||||
assert.Equal(t, astisub.Style{ID: "style_2", InlineStyle: &astisub.StyleAttributes{TTMLColor: "white", TTMLExtent: "100% 20%", TTMLFontFamily: "sansSerif", TTMLFontStyle: "normal", TTMLOrigin: "0% 80%", TTMLTextAlign: "center"}}, *s.Styles["style_2"])
|
||||
assert.Equal(t, astisub.Style{ID: "style_0", InlineStyle: &astisub.StyleAttributes{TTMLColor: astikit.StrPtr("white"), TTMLExtent: astikit.StrPtr("100% 10%"), TTMLFontFamily: astikit.StrPtr("sansSerif"), TTMLFontStyle: astikit.StrPtr("normal"), TTMLOrigin: astikit.StrPtr("0% 90%"), TTMLTextAlign: astikit.StrPtr("center"), WebVTTAlign: "center"}, Style: s.Styles["style_2"]}, *s.Styles["style_0"])
|
||||
assert.Equal(t, astisub.Style{ID: "style_1", InlineStyle: &astisub.StyleAttributes{TTMLColor: astikit.StrPtr("white"), TTMLExtent: astikit.StrPtr("100% 13%"), TTMLFontFamily: astikit.StrPtr("sansSerif"), TTMLFontStyle: astikit.StrPtr("normal"), TTMLOrigin: astikit.StrPtr("0% 87%"), TTMLTextAlign: astikit.StrPtr("center"), WebVTTAlign: "center"}}, *s.Styles["style_1"])
|
||||
assert.Equal(t, astisub.Style{ID: "style_2", InlineStyle: &astisub.StyleAttributes{TTMLColor: astikit.StrPtr("white"), TTMLExtent: astikit.StrPtr("100% 20%"), TTMLFontFamily: astikit.StrPtr("sansSerif"), TTMLFontStyle: astikit.StrPtr("normal"), TTMLOrigin: astikit.StrPtr("0% 80%"), TTMLTextAlign: astikit.StrPtr("center"), WebVTTAlign: "center"}}, *s.Styles["style_2"])
|
||||
// Regions
|
||||
assert.Equal(t, 3, len(s.Regions))
|
||||
assert.Equal(t, astisub.Region{ID: "region_0", Style: s.Styles["style_0"], InlineStyle: &astisub.StyleAttributes{TTMLColor: "blue"}}, *s.Regions["region_0"])
|
||||
assert.Equal(t, astisub.Region{ID: "region_0", Style: s.Styles["style_0"], InlineStyle: &astisub.StyleAttributes{TTMLColor: astikit.StrPtr("blue")}}, *s.Regions["region_0"])
|
||||
assert.Equal(t, astisub.Region{ID: "region_1", Style: s.Styles["style_1"], InlineStyle: &astisub.StyleAttributes{}}, *s.Regions["region_1"])
|
||||
assert.Equal(t, astisub.Region{ID: "region_2", Style: s.Styles["style_2"], InlineStyle: &astisub.StyleAttributes{}}, *s.Regions["region_2"])
|
||||
// Items
|
||||
assert.Equal(t, s.Regions["region_1"], s.Items[0].Region)
|
||||
assert.Equal(t, s.Styles["style_1"], s.Items[0].Style)
|
||||
assert.Equal(t, &astisub.StyleAttributes{TTMLColor: "red"}, s.Items[0].InlineStyle)
|
||||
assert.Equal(t, []astisub.Line{{Items: []astisub.LineItem{{Style: s.Styles["style_1"], InlineStyle: &astisub.StyleAttributes{TTMLColor: "black"}, Text: "(deep rumbling)"}}}}, s.Items[0].Lines)
|
||||
assert.Equal(t, []astisub.Line{{Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Text: "MAN:"}}}, {Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Text: "How did we"}, {InlineStyle: &astisub.StyleAttributes{TTMLColor: "green"}, Style: s.Styles["style_1"], Text: "end up"}, {InlineStyle: &astisub.StyleAttributes{}, Text: "here?"}}}}, s.Items[1].Lines)
|
||||
assert.Equal(t, &astisub.StyleAttributes{TTMLColor: astikit.StrPtr("red")}, s.Items[0].InlineStyle)
|
||||
assert.Equal(t, []astisub.Line{{Items: []astisub.LineItem{{Style: s.Styles["style_1"], InlineStyle: &astisub.StyleAttributes{TTMLColor: astikit.StrPtr("black")}, Text: "(deep rumbling)"}}}}, s.Items[0].Lines)
|
||||
assert.Equal(t, []astisub.Line{{Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Text: "MAN:"}}}, {Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Text: "How did we"}, {InlineStyle: &astisub.StyleAttributes{TTMLColor: astikit.StrPtr("green")}, Style: s.Styles["style_1"], Text: "end up"}, {InlineStyle: &astisub.StyleAttributes{}, Text: "here?"}}}}, s.Items[1].Lines)
|
||||
assert.Equal(t, []astisub.Line{{Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Style: s.Styles["style_1"], Text: "This place is horrible."}}}}, s.Items[2].Lines)
|
||||
assert.Equal(t, []astisub.Line{{Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Style: s.Styles["style_1"], Text: "Smells like balls."}}}}, s.Items[3].Lines)
|
||||
assert.Equal(t, []astisub.Line{{Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Style: s.Styles["style_2"], Text: "We don't belong"}}}, {Items: []astisub.LineItem{{InlineStyle: &astisub.StyleAttributes{}, Style: s.Styles["style_1"], Text: "in this shithole."}}}}, s.Items[4].Lines)
|
||||
|
||||
@@ -299,22 +299,37 @@ func (s Subtitles) WriteToWebVTT(o io.Writer) (err error) {
|
||||
if s.Regions[id].InlineStyle.WebVTTLines != 0 {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("lines="+strconv.Itoa(s.Regions[id].InlineStyle.WebVTTLines))...)
|
||||
} else if s.Regions[id].Style.InlineStyle.WebVTTLines != 0 {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("lines="+strconv.Itoa(s.Regions[id].Style.InlineStyle.WebVTTLines))...)
|
||||
}
|
||||
if s.Regions[id].InlineStyle.WebVTTRegionAnchor != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("regionanchor="+s.Regions[id].InlineStyle.WebVTTRegionAnchor)...)
|
||||
} else if s.Regions[id].Style.InlineStyle.WebVTTRegionAnchor != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("regionanchor="+s.Regions[id].Style.InlineStyle.WebVTTRegionAnchor)...)
|
||||
}
|
||||
if s.Regions[id].InlineStyle.WebVTTScroll != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("scroll="+s.Regions[id].InlineStyle.WebVTTScroll)...)
|
||||
} else if s.Regions[id].Style.InlineStyle.WebVTTScroll != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("scroll="+s.Regions[id].Style.InlineStyle.WebVTTScroll)...)
|
||||
}
|
||||
if s.Regions[id].InlineStyle.WebVTTViewportAnchor != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("viewportanchor="+s.Regions[id].InlineStyle.WebVTTViewportAnchor)...)
|
||||
} else if s.Regions[id].Style.InlineStyle.WebVTTViewportAnchor != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("viewportanchor="+s.Regions[id].Style.InlineStyle.WebVTTViewportAnchor)...)
|
||||
}
|
||||
if s.Regions[id].InlineStyle.WebVTTWidth != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("width="+s.Regions[id].InlineStyle.WebVTTWidth)...)
|
||||
} else if s.Regions[id].Style.InlineStyle.WebVTTWidth != "" {
|
||||
c = append(c, bytesSpace...)
|
||||
c = append(c, []byte("width="+s.Regions[id].Style.InlineStyle.WebVTTWidth)...)
|
||||
}
|
||||
c = append(c, bytesLineSeparator...)
|
||||
}
|
||||
@@ -396,8 +411,12 @@ func (l Line) webVTTBytes() (c []byte) {
|
||||
if l.VoiceName != "" {
|
||||
c = append(c, []byte("<v "+l.VoiceName+">")...)
|
||||
}
|
||||
for _, li := range l.Items {
|
||||
for idx, li := range l.Items {
|
||||
c = append(c, li.webVTTBytes()...)
|
||||
// condition to avoid adding space as the last character.
|
||||
if idx < len(l.Items)-1 {
|
||||
c = append(c, []byte(" ")...)
|
||||
}
|
||||
}
|
||||
c = append(c, bytesLineSeparator...)
|
||||
return
|
||||
@@ -406,8 +425,8 @@ func (l Line) webVTTBytes() (c []byte) {
|
||||
func (li LineItem) webVTTBytes() (c []byte) {
|
||||
// Get color
|
||||
var color string
|
||||
if li.InlineStyle != nil && li.InlineStyle.TTMLColor != "" {
|
||||
color = cssColor(li.InlineStyle.TTMLColor)
|
||||
if li.InlineStyle != nil && li.InlineStyle.TTMLColor != nil {
|
||||
color = cssColor(*li.InlineStyle.TTMLColor)
|
||||
}
|
||||
|
||||
// Get italics
|
||||
|
||||
Reference in New Issue
Block a user