Skip to content

Commit

Permalink
Updated based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arnestorksen committed Dec 7, 2020
1 parent ea4d8f6 commit 4a77ae2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 48 deletions.
28 changes: 16 additions & 12 deletions stl.go
Expand Up @@ -3,6 +3,7 @@ package astisub
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -194,14 +195,14 @@ func ReadFromSTL(i io.Reader) (o *Subtitles, err error) {
// Update metadata
// TODO Add more STL fields to metadata
o.Metadata = &Metadata{
CreationDate: &g.creationDate,
Framerate: g.framerate,
RevisionDate: &g.revisionDate,
STLCountryOfOrigin: g.countryOfOrigin,
STLCreationDate: &g.creationDate,
STLDisplayStandardCode: g.displayStandardCode,
STLMaximumNumberOfDisplayableCharactersInAnyTextRow: astikit.IntPtr(g.maximumNumberOfDisplayableCharactersInAnyTextRow),
STLMaximumNumberOfDisplayableRows: astikit.IntPtr(g.maximumNumberOfDisplayableRows),
STLPublisher: g.publisher,
STLRevisionDate: &g.revisionDate,
STLSubtitleListReferenceCode: g.subtitleListReferenceCode,
Title: g.originalProgramTitle,
}
Expand All @@ -228,7 +229,7 @@ func ReadFromSTL(i io.Reader) (o *Subtitles, err error) {
continue
}

justification := parseJustificationAttribute(t.justificationCode)
justification := parseSTLJustificationCode(t.justificationCode)
rows := bytes.Split(t.text, []byte{stlLineSeparator})

position := STLPosition{
Expand All @@ -253,7 +254,10 @@ func ReadFromSTL(i io.Reader) (o *Subtitles, err error) {
// Loop through rows
for _, text := range bytes.Split(t.text, []byte{stlLineSeparator}) {
if g.displayStandardCode == stlDisplayStandardCodeOpenSubtitling {
parseOpenSubtitleRow(i, ch, func() styler { return newSTLStyler() }, text)
err = parseOpenSubtitleRow(i, ch, func() styler { return newSTLStyler() }, text)
if err != nil {
return nil, err
}
} else {
parseTeletextRow(i, ch, func() styler { return newSTLStyler() }, text)
}
Expand Down Expand Up @@ -343,8 +347,8 @@ func newGSIBlock(s Subtitles) (g *gsiBlock) {

// Add metadata
if s.Metadata != nil {
if s.Metadata.CreationDate != nil {
g.creationDate = *s.Metadata.CreationDate
if s.Metadata.STLCreationDate != nil {
g.creationDate = *s.Metadata.STLCreationDate
}
g.countryOfOrigin = s.Metadata.STLCountryOfOrigin
g.displayStandardCode = s.Metadata.STLDisplayStandardCode
Expand All @@ -360,8 +364,8 @@ func newGSIBlock(s Subtitles) (g *gsiBlock) {
g.maximumNumberOfDisplayableRows = *s.Metadata.STLMaximumNumberOfDisplayableRows
}
g.publisher = s.Metadata.STLPublisher
if s.Metadata.RevisionDate != nil {
g.revisionDate = *s.Metadata.RevisionDate
if s.Metadata.STLRevisionDate != nil {
g.revisionDate = *s.Metadata.STLRevisionDate
}
g.subtitleListReferenceCode = s.Metadata.STLSubtitleListReferenceCode
}
Expand Down Expand Up @@ -638,7 +642,7 @@ func newTTIBlock(i *Item, idx int) (t *ttiBlock) {
subtitleNumber: idx,
timecodeIn: i.StartAt,
timecodeOut: i.EndAt,
verticalPosition: verticalPositionFromStyle(i.InlineStyle),
verticalPosition: stlVerticalPositionFromStyle(i.InlineStyle),
}

// Add text
Expand All @@ -654,7 +658,7 @@ func newTTIBlock(i *Item, idx int) (t *ttiBlock) {
return
}

func verticalPositionFromStyle(sa *StyleAttributes) int {
func stlVerticalPositionFromStyle(sa *StyleAttributes) int {
if sa != nil && sa.STLPosition != nil {
return sa.STLPosition.VerticalPosition
} else {
Expand Down Expand Up @@ -952,7 +956,7 @@ func encodeTextSTL(i string) (o []byte) {
return
}

func parseJustificationAttribute(i byte) Justification {
func parseSTLJustificationCode(i byte) Justification {
switch i {
case 0x00:
return JustificationUnchanged
Expand Down Expand Up @@ -983,7 +987,7 @@ func parseOpenSubtitleRow(i *Item, d decoder, fs func() styler, row []byte) erro
}

if isTeletextControlCode(v) {
return fmt.Errorf("teletext control code in open text")
return errors.New("teletext control code in open text")
}
if s != nil {
s.parseSpacingAttribute(v)
Expand Down
24 changes: 12 additions & 12 deletions stl_test.go
Expand Up @@ -22,17 +22,17 @@ func TestSTL(t *testing.T) {
assertSubtitleItems(t, s)
// Metadata
assert.Equal(t, &astisub.Metadata{
Framerate: 25,
Language: astisub.LanguageFrench,
Framerate: 25,
Language: astisub.LanguageFrench,
STLCreationDate: &creationDate,
STLMaximumNumberOfDisplayableCharactersInAnyTextRow: astikit.IntPtr(40),
STLMaximumNumberOfDisplayableRows: astikit.IntPtr(23),
STLPublisher: "Copyright test",
STLDisplayStandardCode: "1",
STLRevisionDate: &revisionDate,
STLSubtitleListReferenceCode: "12345678",
STLCountryOfOrigin: "FRA",
Title: "Title test",
CreationDate: &creationDate,
RevisionDate: &revisionDate},
Title: "Title test"},
s.Metadata)

// No subtitles to write
Expand All @@ -58,16 +58,16 @@ func TestOPNSTL(t *testing.T) {
assert.NoError(t, err)
// Metadata
assert.Equal(t, &astisub.Metadata{
Framerate: 25,
Language: astisub.LanguageEnglish,
Framerate: 25,
Language: astisub.LanguageEnglish,
STLCountryOfOrigin: "NOR",
STLCreationDate: &creationDate,
STLDisplayStandardCode: "0",
STLMaximumNumberOfDisplayableCharactersInAnyTextRow: astikit.IntPtr(38),
STLMaximumNumberOfDisplayableRows: astikit.IntPtr(11),
STLPublisher: "",
STLDisplayStandardCode: "0",
STLCountryOfOrigin: "NOR",
Title: "",
CreationDate: &creationDate,
RevisionDate: &revisionDate},
STLRevisionDate: &revisionDate,
Title: ""},
s.Metadata)

// No subtitles to write
Expand Down
31 changes: 7 additions & 24 deletions subtitles.go
Expand Up @@ -158,30 +158,13 @@ func (c *Color) TTMLString() string {
return fmt.Sprintf("%.6x", uint32(c.Red)<<16|uint32(c.Green)<<8|uint32(c.Blue))
}

type Justification struct {
value int
}

const (
justificationUnchangedValue = 1
justificationLeftValue = 2
justificationCenterValue = 3
justificationRightValue = 4
)
type Justification int

var (
JustificationUnchanged = Justification{
value: justificationUnchangedValue,
}
JustificationLeft = Justification{
value: justificationLeftValue,
}
JustificationCentered = Justification{
value: justificationCenterValue,
}
JustificationRight = Justification{
value: justificationRightValue,
}
JustificationUnchanged = Justification(1)
JustificationLeft = Justification(2)
JustificationCentered = Justification(3)
JustificationRight = Justification(4)
)

// StyleAttributes represents style attributes
Expand Down Expand Up @@ -279,10 +262,8 @@ func (sa *StyleAttributes) propagateWebVTTAttributes() {}
// TODO Merge attributes
type Metadata struct {
Comments []string
CreationDate *time.Time
Framerate int
Language string
RevisionDate *time.Time
SSACollisions string
SSAOriginalEditing string
SSAOriginalScript string
Expand All @@ -297,10 +278,12 @@ type Metadata struct {
SSAUpdateDetails string
SSAWrapStyle string
STLCountryOfOrigin string
STLCreationDate *time.Time
STLDisplayStandardCode string
STLMaximumNumberOfDisplayableCharactersInAnyTextRow *int
STLMaximumNumberOfDisplayableRows *int
STLPublisher string
STLRevisionDate *time.Time
STLSubtitleListReferenceCode string
Title string
TTMLCopyright string
Expand Down

0 comments on commit 4a77ae2

Please sign in to comment.