Skip to content

Commit 7750885

Browse files
committed
Fallback to displayAlign from style for TTML regions
Adds support for using the `displayAlign` from the `style` of a region, if it has no explicit `displayAlign` set. Closes: #2559
1 parent a29e06f commit 7750885

5 files changed

Lines changed: 79 additions & 0 deletions

File tree

RELEASENOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
* Add support for skipping frames that are late during join rather than
2727
dropping in DecoderVideoRenderer.
2828
* Text:
29+
* TTML: Fallback to `displayAlign` from `style` for regions
30+
([#2559](https://github.com/androidx/media/issues/2559)).
2931
* Metadata:
3032
* Image:
3133
* DataSource:

libraries/extractor/src/main/java/androidx/media3/extractor/text/ttml/TtmlParser.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,15 @@ private static TtmlRegion parseRegionAttributes(
462462
@Nullable
463463
String displayAlign =
464464
XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_DISPLAY_ALIGN);
465+
if (displayAlign == null) {
466+
String styleId = XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_STYLE);
467+
if (styleId != null) {
468+
TtmlStyle style = globalStyles.get(styleId);
469+
if (style != null) {
470+
displayAlign = style.getDisplayAlign();
471+
}
472+
}
473+
}
465474
if (displayAlign != null) {
466475
switch (Ascii.toLowerCase(displayAlign)) {
467476
case "center":
@@ -642,6 +651,9 @@ private static String[] parseStyleIds(String parentStyleIds) {
642651
case TtmlNode.ATTR_TTS_EXTENT:
643652
style = createIfNull(style).setExtent(attributeValue);
644653
break;
654+
case TtmlNode.ATTR_TTS_DISPLAY_ALIGN:
655+
style = createIfNull(style).setDisplayAlign(attributeValue);
656+
break;
645657
default:
646658
// ignore
647659
break;

libraries/extractor/src/main/java/androidx/media3/extractor/text/ttml/TtmlStyle.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
private float shearPercentage;
9999
@Nullable private String origin;
100100
@Nullable private String extent;
101+
@Nullable private String displayAlign;
101102

102103
public TtmlStyle() {
103104
linethrough = UNSPECIFIED;
@@ -285,6 +286,9 @@ private TtmlStyle inherit(@Nullable TtmlStyle ancestor, boolean chaining) {
285286
if (extent == null) {
286287
extent = ancestor.extent;
287288
}
289+
if (displayAlign == null) {
290+
displayAlign = ancestor.displayAlign;
291+
}
288292
// attributes not inherited as of http://www.w3.org/TR/ttml1/
289293
if (chaining && !hasBackgroundColor && ancestor.hasBackgroundColor) {
290294
setBackgroundColor(ancestor.backgroundColor);
@@ -412,4 +416,15 @@ public TtmlStyle setExtent(@Nullable String extent) {
412416
public String getExtent() {
413417
return extent;
414418
}
419+
420+
@CanIgnoreReturnValue
421+
public TtmlStyle setDisplayAlign(String displayAlign) {
422+
this.displayAlign = displayAlign;
423+
return this;
424+
}
425+
426+
@Nullable
427+
public String getDisplayAlign() {
428+
return displayAlign;
429+
}
415430
}

libraries/extractor/src/test/java/androidx/media3/extractor/text/ttml/TtmlParserTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public final class TtmlParserTest {
7676
private static final String SHEAR_FILE = "media/ttml/shear.xml";
7777
private static final String REGION_ATTRS_FROM_STYLE_FILE =
7878
"media/ttml/inherit_region_attributes_from_style.xml";
79+
private static final String DISPLAY_ALIGN_ATTR_FROM_STYLE_FILE =
80+
"media/ttml/fallback_display_align_from_style.xml";
7981

8082
@Test
8183
public void simple_allCues() throws Exception {
@@ -1111,6 +1113,26 @@ public void regionAttrsFromStyle() throws Exception {
11111113
assertThat(thirdCue.size).isEqualTo(20f / 100f);
11121114
}
11131115

1116+
@Test
1117+
public void regionDisplayAlignFromStyle() throws Exception {
1118+
ImmutableList<CuesWithTiming> allCues = getAllCues(DISPLAY_ALIGN_ATTR_FROM_STYLE_FILE);
1119+
1120+
Cue firstCue = Iterables.getOnlyElement(allCues.get(0).cues);
1121+
assertThat(firstCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);
1122+
1123+
Cue secondCue = Iterables.getOnlyElement(allCues.get(1).cues);
1124+
assertThat(secondCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);
1125+
1126+
Cue thirdCue = Iterables.getOnlyElement(allCues.get(2).cues);
1127+
assertThat(thirdCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE);
1128+
1129+
Cue fourthCue = Iterables.getOnlyElement(allCues.get(3).cues);
1130+
assertThat(fourthCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
1131+
1132+
Cue fithCue = Iterables.getOnlyElement(allCues.get(4).cues);
1133+
assertThat(fithCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
1134+
}
1135+
11141136
private static Spanned getOnlyCueTextAtIndex(List<CuesWithTiming> allCues, int index) {
11151137
Cue cue = getOnlyCueAtIndex(allCues, index);
11161138
assertThat(cue.text).isInstanceOf(Spanned.class);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<tt xmlns="http://www.w3.org/ns/ttml"
2+
xmlns:tts="http://www.w3.org/2006/10/ttaf1#style">
3+
<head>
4+
<styling>
5+
<style xml:id="style1" />
6+
<style xml:id="style2" tts:displayAlign="before"/>
7+
<style xml:id="style3" tts:displayAlign="center"/>
8+
<style xml:id="style4" tts:displayAlign="after"/>
9+
<style xml:id="style5" style="style4"/>
10+
</styling>
11+
<layout>
12+
<region xml:id="region1" style="style1" />
13+
<region xml:id="region2" style="style2" />
14+
<region xml:id="region3" style="style3" />
15+
<region xml:id="region4" style="style4" />
16+
<region xml:id="region5" style="style5" />
17+
</layout>
18+
</head>
19+
<body>
20+
<div>
21+
<p begin="2s" end="4s" region="region1">text 1</p>
22+
<p begin="4s" end="6s" region="region2">text 2</p>
23+
<p begin="8s" end="10s" region="region3">text 3</p>
24+
<p begin="10s" end="12s" region="region4">text 4</p>
25+
<p begin="12s" end="14s" region="region5">text 5</p>
26+
</div>
27+
</body>
28+
</tt>

0 commit comments

Comments
 (0)