Skip to content

Commit

Permalink
Fix TTML positioning
Browse files Browse the repository at this point in the history
Issue: #2824

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=156781252
  • Loading branch information
ojw28 committed May 23, 2017
1 parent fb3c190 commit bf71ad4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,48 +157,48 @@ public void testMultipleRegions() throws IOException, SubtitleDecoderException {
assertEquals(2, output.size());
Cue ttmlCue = output.get(0);
assertEquals("lorem", ttmlCue.text.toString());
assertEquals(10.f / 100.f, ttmlCue.position);
assertEquals(10.f / 100.f, ttmlCue.line);
assertEquals(20.f / 100.f, ttmlCue.size);
assertEquals(10f / 100f, ttmlCue.position);
assertEquals(10f / 100f, ttmlCue.line);
assertEquals(20f / 100f, ttmlCue.size);

ttmlCue = output.get(1);
assertEquals("amet", ttmlCue.text.toString());
assertEquals(60.f / 100.f, ttmlCue.position);
assertEquals(10.f / 100.f, ttmlCue.line);
assertEquals(20.f / 100.f, ttmlCue.size);
assertEquals(60f / 100f, ttmlCue.position);
assertEquals(10f / 100f, ttmlCue.line);
assertEquals(20f / 100f, ttmlCue.size);

output = subtitle.getCues(5000000);
assertEquals(1, output.size());
ttmlCue = output.get(0);
assertEquals("ipsum", ttmlCue.text.toString());
assertEquals(40.f / 100.f, ttmlCue.position);
assertEquals(40.f / 100.f, ttmlCue.line);
assertEquals(20.f / 100.f, ttmlCue.size);
assertEquals(40f / 100f, ttmlCue.position);
assertEquals(40f / 100f, ttmlCue.line);
assertEquals(20f / 100f, ttmlCue.size);

output = subtitle.getCues(9000000);
assertEquals(1, output.size());
ttmlCue = output.get(0);
assertEquals("dolor", ttmlCue.text.toString());
assertEquals(10.f / 100.f, ttmlCue.position);
assertEquals(80.f / 100.f, ttmlCue.line);
assertEquals(Cue.DIMEN_UNSET, ttmlCue.size);
assertEquals(10f / 100f, ttmlCue.position);
assertEquals(80f / 100f, ttmlCue.line);
assertEquals(1f, ttmlCue.size);

output = subtitle.getCues(21000000);
assertEquals(1, output.size());
ttmlCue = output.get(0);
assertEquals("She first said this", ttmlCue.text.toString());
assertEquals(45.f / 100.f, ttmlCue.position);
assertEquals(45.f / 100.f, ttmlCue.line);
assertEquals(35.f / 100.f, ttmlCue.size);
assertEquals(45f / 100f, ttmlCue.position);
assertEquals(45f / 100f, ttmlCue.line);
assertEquals(35f / 100f, ttmlCue.size);
output = subtitle.getCues(25000000);
ttmlCue = output.get(0);
assertEquals("She first said this\nThen this", ttmlCue.text.toString());
output = subtitle.getCues(29000000);
assertEquals(1, output.size());
ttmlCue = output.get(0);
assertEquals("She first said this\nThen this\nFinally this", ttmlCue.text.toString());
assertEquals(45.f / 100.f, ttmlCue.position);
assertEquals(45.f / 100.f, ttmlCue.line);
assertEquals(45f / 100f, ttmlCue.position);
assertEquals(45f / 100f, ttmlCue.line);
}

public void testEmptyStyleAttribute() throws IOException, SubtitleDecoderException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import android.text.Layout;
import android.util.Log;
import android.util.Pair;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.SimpleSubtitleDecoder;
Expand Down Expand Up @@ -100,7 +99,7 @@ protected TtmlSubtitle decode(byte[] bytes, int length, boolean reset)
XmlPullParser xmlParser = xmlParserFactory.newPullParser();
Map<String, TtmlStyle> globalStyles = new HashMap<>();
Map<String, TtmlRegion> regionMap = new HashMap<>();
regionMap.put(TtmlNode.ANONYMOUS_REGION_ID, new TtmlRegion());
regionMap.put(TtmlNode.ANONYMOUS_REGION_ID, new TtmlRegion(null));
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes, 0, length);
xmlParser.setInput(inputStream, null);
TtmlSubtitle ttmlSubtitle = null;
Expand Down Expand Up @@ -211,51 +210,94 @@ private Map<String, TtmlStyle> parseHeader(XmlPullParser xmlParser,
globalStyles.put(style.getId(), style);
}
} else if (XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_REGION)) {
Pair<String, TtmlRegion> ttmlRegionInfo = parseRegionAttributes(xmlParser);
if (ttmlRegionInfo != null) {
globalRegions.put(ttmlRegionInfo.first, ttmlRegionInfo.second);
TtmlRegion ttmlRegion = parseRegionAttributes(xmlParser);
if (ttmlRegion != null) {
globalRegions.put(ttmlRegion.id, ttmlRegion);
}
}
} while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_HEAD));
return globalStyles;
}

/**
* Parses a region declaration. Supports origin and extent definition but only when defined in
* terms of percentage of the viewport. Regions that do not correctly declare origin are ignored.
* Parses a region declaration.
* <p>
* If the region defines an origin and/or extent, it is required that they're defined as
* percentages of the viewport. Region declarations that define origin and/or extent in other
* formats are unsupported, and null is returned.
*/
private Pair<String, TtmlRegion> parseRegionAttributes(XmlPullParser xmlParser) {
private TtmlRegion parseRegionAttributes(XmlPullParser xmlParser) {
String regionId = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_ID);
String regionOrigin = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_ORIGIN);
String regionExtent = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_EXTENT);
if (regionOrigin == null || regionId == null) {
if (regionId == null) {
return null;
}
float position = Cue.DIMEN_UNSET;
float line = Cue.DIMEN_UNSET;
Matcher originMatcher = PERCENTAGE_COORDINATES.matcher(regionOrigin);
if (originMatcher.matches()) {
try {
position = Float.parseFloat(originMatcher.group(1)) / 100.f;
line = Float.parseFloat(originMatcher.group(2)) / 100.f;
} catch (NumberFormatException e) {
Log.w(TAG, "Ignoring region with malformed origin: '" + regionOrigin + "'", e);
position = Cue.DIMEN_UNSET;

float position;
float line;
String regionOrigin = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_ORIGIN);
if (regionOrigin != null) {
Matcher originMatcher = PERCENTAGE_COORDINATES.matcher(regionOrigin);
if (originMatcher.matches()) {
try {
position = Float.parseFloat(originMatcher.group(1)) / 100f;
line = Float.parseFloat(originMatcher.group(2)) / 100f;
} catch (NumberFormatException e) {
Log.w(TAG, "Ignoring region with malformed origin: " + regionOrigin);
return null;
}
} else {
Log.w(TAG, "Ignoring region with unsupported origin: " + regionOrigin);
return null;
}
} else {
// Origin is omitted. Default to top left.
position = 0;
line = 0;
}
float width = Cue.DIMEN_UNSET;

float width;
float height;
String regionExtent = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_EXTENT);
if (regionExtent != null) {
Matcher extentMatcher = PERCENTAGE_COORDINATES.matcher(regionExtent);
if (extentMatcher.matches()) {
try {
width = Float.parseFloat(extentMatcher.group(1)) / 100.f;
width = Float.parseFloat(extentMatcher.group(1)) / 100f;
height = Float.parseFloat(extentMatcher.group(2)) / 100f;
} catch (NumberFormatException e) {
Log.w(TAG, "Ignoring malformed region extent: '" + regionExtent + "'", e);
Log.w(TAG, "Ignoring region with malformed extent: " + regionOrigin);
return null;
}
} else {
Log.w(TAG, "Ignoring region with unsupported extent: " + regionOrigin);
return null;
}
} else {
// Extent is omitted. Default to extent of parent.
width = 1;
height = 1;
}

@Cue.AnchorType int lineAnchor = Cue.ANCHOR_TYPE_START;
String displayAlign = XmlPullParserUtil.getAttributeValue(xmlParser,
TtmlNode.ATTR_TTS_DISPLAY_ALIGN);
if (displayAlign != null) {
switch (displayAlign.toLowerCase()) {
case "center":
lineAnchor = Cue.ANCHOR_TYPE_MIDDLE;
line += height / 2;
break;
case "after":
lineAnchor = Cue.ANCHOR_TYPE_END;
line += height;
break;
default:
// Default "before" case. Do nothing.
break;
}
}
return position != Cue.DIMEN_UNSET ? new Pair<>(regionId, new TtmlRegion(position, line,
Cue.LINE_TYPE_FRACTION, width)) : null;

return new TtmlRegion(regionId, position, line, Cue.LINE_TYPE_FRACTION, lineAnchor, width);
}

private String[] parseStyleIds(String parentStyleIds) {
Expand All @@ -277,15 +319,15 @@ private TtmlStyle parseStyleAttributes(XmlPullParser parser, TtmlStyle style) {
try {
style.setBackgroundColor(ColorParser.parseTtmlColor(attributeValue));
} catch (IllegalArgumentException e) {
Log.w(TAG, "failed parsing background value: '" + attributeValue + "'");
Log.w(TAG, "Failed parsing background value: " + attributeValue);
}
break;
case TtmlNode.ATTR_TTS_COLOR:
style = createIfNull(style);
try {
style.setFontColor(ColorParser.parseTtmlColor(attributeValue));
} catch (IllegalArgumentException e) {
Log.w(TAG, "failed parsing color value: '" + attributeValue + "'");
Log.w(TAG, "Failed parsing color value: " + attributeValue);
}
break;
case TtmlNode.ATTR_TTS_FONT_FAMILY:
Expand All @@ -296,7 +338,7 @@ private TtmlStyle parseStyleAttributes(XmlPullParser parser, TtmlStyle style) {
style = createIfNull(style);
parseFontSize(attributeValue, style);
} catch (SubtitleDecoderException e) {
Log.w(TAG, "failed parsing fontSize value: '" + attributeValue + "'");
Log.w(TAG, "Failed parsing fontSize value: " + attributeValue);
}
break;
case TtmlNode.ATTR_TTS_FONT_WEIGHT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@

public static final String ANONYMOUS_REGION_ID = "";
public static final String ATTR_ID = "id";
public static final String ATTR_TTS_BACKGROUND_COLOR = "backgroundColor";
public static final String ATTR_TTS_ORIGIN = "origin";
public static final String ATTR_TTS_EXTENT = "extent";
public static final String ATTR_TTS_DISPLAY_ALIGN = "displayAlign";
public static final String ATTR_TTS_BACKGROUND_COLOR = "backgroundColor";
public static final String ATTR_TTS_FONT_STYLE = "fontStyle";
public static final String ATTR_TTS_FONT_SIZE = "fontSize";
public static final String ATTR_TTS_FONT_FAMILY = "fontFamily";
public static final String ATTR_TTS_FONT_WEIGHT = "fontWeight";
public static final String ATTR_TTS_COLOR = "color";
public static final String ATTR_TTS_ORIGIN = "origin";
public static final String ATTR_TTS_TEXT_DECORATION = "textDecoration";
public static final String ATTR_TTS_TEXT_ALIGN = "textAlign";

Expand Down Expand Up @@ -179,7 +180,7 @@ public List<Cue> getCues(long timeUs, Map<String, TtmlStyle> globalStyles,
for (Entry<String, SpannableStringBuilder> entry : regionOutputs.entrySet()) {
TtmlRegion region = regionMap.get(entry.getKey());
cues.add(new Cue(cleanUpText(entry.getValue()), null, region.line, region.lineType,
Cue.TYPE_UNSET, region.position, Cue.TYPE_UNSET, region.width));
region.lineAnchor, region.position, Cue.TYPE_UNSET, region.width));
}
return cues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@
*/
/* package */ final class TtmlRegion {

public final String id;
public final float position;
public final float line;
@Cue.LineType
public final int lineType;
@Cue.LineType public final int lineType;
@Cue.AnchorType public final int lineAnchor;
public final float width;

public TtmlRegion() {
this(Cue.DIMEN_UNSET, Cue.DIMEN_UNSET, Cue.TYPE_UNSET, Cue.DIMEN_UNSET);
public TtmlRegion(String id) {
this(id, Cue.DIMEN_UNSET, Cue.DIMEN_UNSET, Cue.TYPE_UNSET, Cue.TYPE_UNSET, Cue.DIMEN_UNSET);
}

public TtmlRegion(float position, float line, @Cue.LineType int lineType, float width) {
public TtmlRegion(String id, float position, float line, @Cue.LineType int lineType,
@Cue.AnchorType int lineAnchor, float width) {
this.id = id;
this.position = position;
this.line = line;
this.lineType = lineType;
this.lineAnchor = lineAnchor;
this.width = width;
}

Expand Down

0 comments on commit bf71ad4

Please sign in to comment.