diff --git a/lib/media/ttml_text_parser.js b/lib/media/ttml_text_parser.js index bf1070d376..990dfa901b 100644 --- a/lib/media/ttml_text_parser.js +++ b/lib/media/ttml_text_parser.js @@ -154,6 +154,19 @@ shaka.media.TtmlTextParser.timeHMSFormat_ = shaka.media.TtmlTextParser.percentValues_ = /^(\d{1,2}|100)% (\d{1,2}|100)%$/; +/** + * @const + * @private {!Object} + */ +shaka.media.TtmlTextParser.textAlignToLineAlign_ = { + 'left': 'start', + 'center': 'center', + 'right': 'end', + 'start': 'start', + 'end': 'end' +}; + + /** * Gets leaf nodes of the xml node tree. Ignores the text, br elements * and the spans positioned inside paragraphs @@ -274,10 +287,6 @@ shaka.media.TtmlTextParser.addStyle_ = function( var TtmlTextParser = shaka.media.TtmlTextParser; var results = null; - var align = TtmlTextParser.getStyleAttribute_( - cueElement, region, styles, 'tts:textAlign'); - if (align) - cue.lineAlign = align; var extent = TtmlTextParser.getStyleAttribute_( cueElement, region, styles, 'tts:extent'); @@ -317,6 +326,20 @@ shaka.media.TtmlTextParser.addStyle_ = function( } } } + + var align = TtmlTextParser.getStyleAttribute_( + cueElement, region, styles, 'tts:textAlign'); + if (align) { + if (align == 'center') { + // Need to set cue.position to auto to get centered + // subtitles + cue.align = 'middle'; + cue.position = 'auto'; + } else { + cue.align = align; + } + cue.lineAlign = TtmlTextParser.textAlignToLineAlign_[align]; + } }; diff --git a/test/media/ttml_text_parser_unit.js b/test/media/ttml_text_parser_unit.js index c399bbed01..56d5fbadb2 100644 --- a/test/media/ttml_text_parser_unit.js +++ b/test/media/ttml_text_parser_unit.js @@ -399,6 +399,42 @@ describe('TtmlTextParser', function() { 'end="01:02:03.200">Line1
Line2

'); }); + it('parses cue alignment from textAlign attribute', function() { + verifyHelper( + [ + {start: 62.05, end: 3723.2, text: 'Test', lineAlign: 'start', + align: 'left'} + ], + '' + + '' + + '