Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* Add support for skipping frames that are late during join rather than
dropping in DecoderVideoRenderer.
* Text:
* TTML: Fallback to `displayAlign` from `style` for regions
([#2559](https://github.com/androidx/media/issues/2559)).
* Metadata:
* Image:
* DataSource:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ private static TtmlRegion parseRegionAttributes(
@Nullable
String displayAlign =
XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_DISPLAY_ALIGN);
if (displayAlign == null) {
String styleId = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_STYLE);
if (styleId != null) {
TtmlStyle style = globalStyles.get(styleId);
if (style != null) {
displayAlign = style.getDisplayAlign();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for consistency with the way we handle other attributes, this 'inheritance' logic should be implemented as part of TtmlStyle.chain which is invoked here:

See my comment here: #2559 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, should I also add a test for inheriting displayAlign from another style?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably tweak your existing test file to exercise this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}
}
if (displayAlign != null) {
switch (Ascii.toLowerCase(displayAlign)) {
case "center":
Expand Down Expand Up @@ -642,6 +651,9 @@ private static String[] parseStyleIds(String parentStyleIds) {
case TtmlNode.ATTR_TTS_EXTENT:
style = createIfNull(style).setExtent(attributeValue);
break;
case TtmlNode.ATTR_TTS_DISPLAY_ALIGN:
style = createIfNull(style).setDisplayAlign(attributeValue);
break;
default:
// ignore
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
private float shearPercentage;
@Nullable private String origin;
@Nullable private String extent;
@Nullable private String displayAlign;

public TtmlStyle() {
linethrough = UNSPECIFIED;
Expand Down Expand Up @@ -285,6 +286,9 @@ private TtmlStyle inherit(@Nullable TtmlStyle ancestor, boolean chaining) {
if (extent == null) {
extent = ancestor.extent;
}
if (displayAlign == null) {
displayAlign = ancestor.displayAlign;
}
// attributes not inherited as of http://www.w3.org/TR/ttml1/
if (chaining && !hasBackgroundColor && ancestor.hasBackgroundColor) {
setBackgroundColor(ancestor.backgroundColor);
Expand Down Expand Up @@ -412,4 +416,15 @@ public TtmlStyle setExtent(@Nullable String extent) {
public String getExtent() {
return extent;
}

@CanIgnoreReturnValue
public TtmlStyle setDisplayAlign(String displayAlign) {
this.displayAlign = displayAlign;
return this;
}

@Nullable
public String getDisplayAlign() {
return displayAlign;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public final class TtmlParserTest {
private static final String SHEAR_FILE = "media/ttml/shear.xml";
private static final String REGION_ATTRS_FROM_STYLE_FILE =
"media/ttml/inherit_region_attributes_from_style.xml";
private static final String DISPLAY_ALIGN_ATTR_FROM_STYLE_FILE =
"media/ttml/fallback_display_align_from_style.xml";

@Test
public void simple_allCues() throws Exception {
Expand Down Expand Up @@ -1111,6 +1113,26 @@ public void regionAttrsFromStyle() throws Exception {
assertThat(thirdCue.size).isEqualTo(20f / 100f);
}

@Test
public void regionDisplayAlignFromStyle() throws Exception {
ImmutableList<CuesWithTiming> allCues = getAllCues(DISPLAY_ALIGN_ATTR_FROM_STYLE_FILE);

Cue firstCue = Iterables.getOnlyElement(allCues.get(0).cues);
assertThat(firstCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);

Cue secondCue = Iterables.getOnlyElement(allCues.get(1).cues);
assertThat(secondCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);

Cue thirdCue = Iterables.getOnlyElement(allCues.get(2).cues);
assertThat(thirdCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE);

Cue fourthCue = Iterables.getOnlyElement(allCues.get(3).cues);
assertThat(fourthCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);

Cue fithCue = Iterables.getOnlyElement(allCues.get(4).cues);
assertThat(fithCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
}

private static Spanned getOnlyCueTextAtIndex(List<CuesWithTiming> allCues, int index) {
Cue cue = getOnlyCueAtIndex(allCues, index);
assertThat(cue.text).isInstanceOf(Spanned.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<tt xmlns="http://www.w3.org/ns/ttml"
xmlns:tts="http://www.w3.org/2006/10/ttaf1#style">
<head>
<styling>
<style xml:id="style1" />
<style xml:id="style2" tts:displayAlign="before"/>
<style xml:id="style3" tts:displayAlign="center"/>
<style xml:id="style4" tts:displayAlign="after"/>
<style xml:id="style5" style="style4"/>
</styling>
<layout>
<region xml:id="region1" style="style1" />
<region xml:id="region2" style="style2" />
<region xml:id="region3" style="style3" />
<region xml:id="region4" style="style4" />
<region xml:id="region5" style="style5" />
</layout>
</head>
<body>
<div>
<p begin="2s" end="4s" region="region1">text 1</p>
<p begin="4s" end="6s" region="region2">text 2</p>
<p begin="8s" end="10s" region="region3">text 3</p>
<p begin="10s" end="12s" region="region4">text 4</p>
<p begin="12s" end="14s" region="region5">text 5</p>
</div>
</body>
</tt>