Skip to content

Commit

Permalink
Added first step of style propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Feb 27, 2018
1 parent c945cf7 commit 01e5250
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 18 deletions.
8 changes: 5 additions & 3 deletions ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func newColorFromSSAColor(i string) (_ *Color, _ error) {

// newSSAColorFromColor builds a new SSA color based on a color
func newSSAColorFromColor(i *Color) string {
return "&H" + i.String(16)
return "&H" + i.String(16, true)
}

// ssaScriptInfo represents an SSA script info block
Expand Down Expand Up @@ -831,8 +831,8 @@ func (s ssaStyle) string(format []string) string {
}

// style converts ssaStyle to Style
func (s ssaStyle) style() *Style {
return &Style{
func (s ssaStyle) style() (o *Style) {
o = &Style{
ID: s.name,
InlineStyle: &StyleAttributes{
SSAAlignment: s.alignment,
Expand Down Expand Up @@ -860,6 +860,8 @@ func (s ssaStyle) style() *Style {
SSAUnderline: s.underline,
},
}
o.InlineStyle.propagateSSAAttributes()
return
}

// ssaEvent represents an SSA event
Expand Down
4 changes: 4 additions & 0 deletions stl.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ func (s *stlStyler) hasChanged(sa *StyleAttributes) bool {
return s.boxing != sa.STLBoxing || s.italics != sa.STLItalics || s.underline != sa.STLUnderline
}

func (s *stlStyler) propagateStyleAttributes(sa *StyleAttributes) {
sa.propagateSTLAttributes()
}

// TODO Test
func (s *stlStyler) update(sa *StyleAttributes) {
if s.boxing != nil && s.boxing != sa.STLBoxing {
Expand Down
29 changes: 25 additions & 4 deletions subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,22 @@ func newColorFromString(s string, base int) (c *Color, err error) {
}

// String expresses the color as a string for a specific base
func (c *Color) String(base int) string {
var i = uint32(c.Alpha)<<24 | uint32(c.Blue)<<16 | uint32(c.Green)<<8 | uint32(c.Red)
func (c *Color) String(base int, showAlpha bool) string {
var i = uint32(c.Blue)<<16 | uint32(c.Green)<<8 | uint32(c.Red)
if showAlpha {
i |= uint32(c.Alpha) << 24
}
if base == 16 {
return fmt.Sprintf("%.8x", i)
if showAlpha {
return fmt.Sprintf("%.8x", i)
} else {
return fmt.Sprintf("%.6x", i)
}
}
return strconv.Itoa(int(i))
}

// StyleAttributes represents style attributes
// TODO Convert styles+inline styles from different formats as well
type StyleAttributes struct {
SSAAlignment *int
SSAAlphaLevel *float64
Expand Down Expand Up @@ -195,6 +201,7 @@ type StyleAttributes struct {
TeletextDoubleWidth *bool
TeletextSpacesAfter *int
TeletextSpacesBefore *int
// TODO Use pointers with real types below
TTMLBackgroundColor string // https://htmlcolorcodes.com/fr/
TTMLColor string
TTMLDirection string
Expand Down Expand Up @@ -231,6 +238,20 @@ type StyleAttributes struct {
WebVTTWidth string
}

func (sa *StyleAttributes) propagateSSAAttributes() {}

func (sa *StyleAttributes) propagateSTLAttributes() {}

func (sa *StyleAttributes) propagateTeletextAttributes() {
if sa.TeletextColor != nil {
sa.TTMLColor = "#" + sa.TeletextColor.String(16, false)
}
}

func (sa *StyleAttributes) propagateTTMLAttributes() {}

func (sa *StyleAttributes) propagateWebVTTAttributes() {}

// Metadata represents metadata
// TODO Merge attributes
type Metadata struct {
Expand Down
4 changes: 2 additions & 2 deletions subtitles_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func TestColor(t *testing.T) {
c, err := newColorFromString("305419896", 10)
assert.NoError(t, err)
assert.Equal(t, Color{Alpha: 0x12, Blue: 0x34, Green: 0x56, Red: 0x78}, *c)
assert.Equal(t, "305419896", c.String(10))
assert.Equal(t, "305419896", c.String(10, true))
c, err = newColorFromString("12345678", 16)
assert.NoError(t, err)
assert.Equal(t, Color{Alpha: 0x12, Blue: 0x34, Green: 0x56, Red: 0x78}, *c)
assert.Equal(t, "12345678", c.String(16))
assert.Equal(t, "12345678", c.String(16, true))
}

func TestParseDuration(t *testing.T) {
Expand Down
15 changes: 11 additions & 4 deletions teletext.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ type styler interface {
hasBeenSet() bool
hasChanged(s *StyleAttributes) bool
parseSpacingAttribute(i byte)
propagateStyleAttributes(s *StyleAttributes)
update(sa *StyleAttributes)
}

Expand All @@ -851,9 +852,9 @@ func parseTeletextRow(i *Item, d decoder, fs func() styler, row []byte) {
var l = Line{}
var li = LineItem{InlineStyle: &StyleAttributes{}}
var started bool
var s styler
for _, v := range row {
// Create specific styler
var s styler
if fs != nil {
s = fs()
}
Expand Down Expand Up @@ -907,7 +908,7 @@ func parseTeletextRow(i *Item, d decoder, fs func() styler, row []byte) {
// Line has started
if started {
// Append line item
appendTeletextLineItem(&l, li)
appendTeletextLineItem(&l, li, s)

// Create new line item
sa := &StyleAttributes{}
Expand Down Expand Up @@ -939,15 +940,15 @@ func parseTeletextRow(i *Item, d decoder, fs func() styler, row []byte) {
}

// Append line item
appendTeletextLineItem(&l, li)
appendTeletextLineItem(&l, li, s)

// Append line
if len(l.Items) > 0 {
i.Lines = append(i.Lines, l)
}
}

func appendTeletextLineItem(l *Line, li LineItem) {
func appendTeletextLineItem(l *Line, li LineItem, s styler) {
// There's some text
if len(strings.TrimSpace(li.Text)) > 0 {
// Make sure inline style exists
Expand Down Expand Up @@ -975,6 +976,12 @@ func appendTeletextLineItem(l *Line, li LineItem) {
}
}

// Propagate style attributes
li.InlineStyle.propagateTeletextAttributes()
if s != nil {
s.propagateStyleAttributes(li.InlineStyle)
}

// Append line item
li.Text = strings.TrimSpace(li.Text)
l.Items = append(l.Items, li)
Expand Down
16 changes: 14 additions & 2 deletions teletext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,54 +80,64 @@ func TestParseTeletextRow(t *testing.T) {
TeletextColor: ColorBlack,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#000000",
}},
{Text: "red", InlineStyle: &StyleAttributes{
TeletextColor: ColorRed,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#0000ff",
}},
{Text: "green", InlineStyle: &StyleAttributes{
TeletextColor: ColorGreen,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#008000",
}},
{Text: "yellow", InlineStyle: &StyleAttributes{
TeletextColor: ColorYellow,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#00ffff",
}},
{Text: "blue", InlineStyle: &StyleAttributes{
TeletextColor: ColorBlue,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ff0000",
}},
{Text: "magenta", InlineStyle: &StyleAttributes{
TeletextColor: ColorMagenta,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ff00ff",
}},
{Text: "cyan", InlineStyle: &StyleAttributes{
TeletextColor: ColorCyan,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ffff00",
}},
{Text: "white", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ffffff",
}},
{Text: "double height", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
TeletextDoubleHeight: astiptr.Bool(true),
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ffffff",
}},
{Text: "double width", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
TeletextDoubleHeight: astiptr.Bool(true),
TeletextDoubleWidth: astiptr.Bool(true),
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ffffff",
}},
{Text: "double size", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
Expand All @@ -136,6 +146,7 @@ func TestParseTeletextRow(t *testing.T) {
TeletextDoubleSize: astiptr.Bool(true),
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ffffff",
}},
{Text: "reset", InlineStyle: &StyleAttributes{
TeletextColor: ColorWhite,
Expand All @@ -144,6 +155,7 @@ func TestParseTeletextRow(t *testing.T) {
TeletextDoubleSize: astiptr.Bool(false),
TeletextSpacesAfter: astiptr.Int(0),
TeletextSpacesBefore: astiptr.Int(0),
TTMLColor: "#ffffff",
}},
}, i.Lines[0].Items)
}
Expand All @@ -153,11 +165,11 @@ func TestAppendTeletextLineItem(t *testing.T) {
l := Line{}

// Empty
appendTeletextLineItem(&l, LineItem{})
appendTeletextLineItem(&l, LineItem{}, nil)
assert.Equal(t, 0, len(l.Items))

// Not empty
appendTeletextLineItem(&l, LineItem{Text: " test "})
appendTeletextLineItem(&l, LineItem{Text: " test "}, nil)
assert.Equal(t, "test", l.Items[0].Text)
assert.Equal(t, StyleAttributes{
TeletextSpacesAfter: astiptr.Int(2),
Expand Down
6 changes: 4 additions & 2 deletions ttml.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ type TTMLInStyleAttributes struct {
}

// StyleAttributes converts TTMLInStyleAttributes into a StyleAttributes
func (s TTMLInStyleAttributes) styleAttributes() *StyleAttributes {
return &StyleAttributes{
func (s TTMLInStyleAttributes) styleAttributes() (o *StyleAttributes) {
o = &StyleAttributes{
TTMLBackgroundColor: s.BackgroundColor,
TTMLColor: s.Color,
TTMLDirection: s.Direction,
Expand All @@ -121,6 +121,8 @@ func (s TTMLInStyleAttributes) styleAttributes() *StyleAttributes {
TTMLWritingMode: s.WritingMode,
TTMLZIndex: s.ZIndex,
}
o.propagateTTMLAttributes()
return
}

// TTMLInHeader represents an input TTML header
Expand Down
4 changes: 3 additions & 1 deletion webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
r.InlineStyle.WebVTTWidth = split[1]
}
}
r.InlineStyle.propagateWebVTTAttributes()

// Add region
o.Regions[r.ID] = r
Expand Down Expand Up @@ -165,6 +166,7 @@ func ReadFromWebVTT(i io.Reader) (o *Subtitles, err error) {
}
}
}
item.InlineStyle.propagateWebVTTAttributes()

// Reset comments
comments = []string{}
Expand Down Expand Up @@ -257,7 +259,7 @@ func (s Subtitles) WriteToWebVTT(o io.Writer) (err error) {
c = append(c, []byte(strconv.Itoa(index+1))...)
c = append(c, bytesLineSeparator...)
c = append(c, []byte(formatDurationWebVTT(item.StartAt))...)
c = append(c, bytesSRTTimeBoundariesSeparator...)
c = append(c, bytesWebVTTTimeBoundariesSeparator...)
c = append(c, []byte(formatDurationWebVTT(item.EndAt))...)

// Add styles
Expand Down

0 comments on commit 01e5250

Please sign in to comment.