diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlDecoder.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlDecoder.java index e9d6d88c4a..611eb7ff2f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlDecoder.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlDecoder.java @@ -444,6 +444,26 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder { } float regionTextHeight = 1.0f / cellResolution.rows; + + @Cue.VerticalType int verticalType = Cue.TYPE_UNSET; + @Nullable + String writingDirection = + XmlPullParserUtil.getAttributeValue(xmlParser, TtmlNode.ATTR_TTS_WRITING_MODE); + if (writingDirection != null) { + switch (Util.toLowerInvariant(writingDirection)) { + // TODO: Support horizontal RTL modes. + case TtmlNode.VERTICAL: + case TtmlNode.VERTICAL_LR: + verticalType = Cue.VERTICAL_TYPE_LR; + break; + case TtmlNode.VERTICAL_RL: + verticalType = Cue.VERTICAL_TYPE_RL; + break; + default: + // ignore + break; + } + } return new TtmlRegion( regionId, position, @@ -453,7 +473,8 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder { width, height, /* textSizeType= */ Cue.TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING, - /* textSize= */ regionTextHeight); + /* textSize= */ regionTextHeight, + verticalType); } private static String[] parseStyleIds(String parentStyleIds) { @@ -588,21 +609,6 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder { break; } break; - case TtmlNode.ATTR_TTS_WRITING_MODE: - switch (Util.toLowerInvariant(attributeValue)) { - // TODO: Support horizontal RTL modes. - case TtmlNode.VERTICAL: - case TtmlNode.VERTICAL_LR: - style = createIfNull(style).setVerticalType(Cue.VERTICAL_TYPE_LR); - break; - case TtmlNode.VERTICAL_RL: - style = createIfNull(style).setVerticalType(Cue.VERTICAL_TYPE_RL); - break; - default: - // ignore - break; - } - break; default: // ignore break; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlNode.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlNode.java index 4c51f97247..8e516dedf1 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlNode.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlNode.java @@ -268,6 +268,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; .setLineAnchor(region.lineAnchor) .setSize(region.width) .setBitmapHeight(region.height) + .setVerticalType(region.verticalType) .build()); } @@ -281,6 +282,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; regionOutput.setPosition(region.position); regionOutput.setSize(region.width); regionOutput.setTextSize(region.textSize, region.textSizeType); + regionOutput.setVerticalType(region.verticalType); cues.add(regionOutput.build()); } @@ -380,9 +382,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; } if (resolvedStyle != null) { TtmlRenderUtil.applyStylesToSpan(text, start, end, resolvedStyle, parent, globalStyles); - regionOutput - .setTextAlignment(resolvedStyle.getTextAlign()) - .setVerticalType(resolvedStyle.getVerticalType()); + regionOutput.setTextAlignment(resolvedStyle.getTextAlign()); } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlRegion.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlRegion.java index 3cbc25d4b2..36c862568f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlRegion.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlRegion.java @@ -25,12 +25,13 @@ import com.google.android.exoplayer2.text.Cue; public final String id; public final float position; public final float line; - public final @Cue.LineType int lineType; - public final @Cue.AnchorType int lineAnchor; + @Cue.LineType public final int lineType; + @Cue.AnchorType public final int lineAnchor; public final float width; public final float height; - public final @Cue.TextSizeType int textSizeType; + @Cue.TextSizeType public final int textSizeType; public final float textSize; + @Cue.VerticalType public final int verticalType; public TtmlRegion(String id) { this( @@ -42,7 +43,8 @@ import com.google.android.exoplayer2.text.Cue; /* width= */ Cue.DIMEN_UNSET, /* height= */ Cue.DIMEN_UNSET, /* textSizeType= */ Cue.TYPE_UNSET, - /* textSize= */ Cue.DIMEN_UNSET); + /* textSize= */ Cue.DIMEN_UNSET, + /* verticalType= */ Cue.TYPE_UNSET); } public TtmlRegion( @@ -54,7 +56,8 @@ import com.google.android.exoplayer2.text.Cue; float width, float height, int textSizeType, - float textSize) { + float textSize, + @Cue.VerticalType int verticalType) { this.id = id; this.position = position; this.line = line; @@ -64,6 +67,7 @@ import com.google.android.exoplayer2.text.Cue; this.height = height; this.textSizeType = textSizeType; this.textSize = textSize; + this.verticalType = verticalType; } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlStyle.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlStyle.java index 928af3620c..3ca519660d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlStyle.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ttml/TtmlStyle.java @@ -19,8 +19,6 @@ import android.graphics.Typeface; import android.text.Layout; import androidx.annotation.IntDef; import androidx.annotation.Nullable; -import com.google.android.exoplayer2.text.Cue; -import com.google.android.exoplayer2.text.Cue.VerticalType; import com.google.android.exoplayer2.text.span.RubySpan; import java.lang.annotation.Documented; import java.lang.annotation.Retention; @@ -88,7 +86,6 @@ import java.lang.annotation.RetentionPolicy; @RubySpan.Position private int rubyPosition; @Nullable private Layout.Alignment textAlign; @OptionalBoolean private int textCombine; - @Cue.VerticalType private int verticalType; public TtmlStyle() { linethrough = UNSPECIFIED; @@ -99,7 +96,6 @@ import java.lang.annotation.RetentionPolicy; rubyType = UNSPECIFIED; rubyPosition = RubySpan.POSITION_UNKNOWN; textCombine = UNSPECIFIED; - verticalType = Cue.TYPE_UNSET; } /** @@ -249,9 +245,6 @@ import java.lang.annotation.RetentionPolicy; if (chaining && rubyType == UNSPECIFIED && ancestor.rubyType != UNSPECIFIED) { rubyType = ancestor.rubyType; } - if (chaining && verticalType == Cue.TYPE_UNSET && ancestor.verticalType != Cue.TYPE_UNSET) { - setVerticalType(ancestor.verticalType); - } } return this; } @@ -323,14 +316,4 @@ import java.lang.annotation.RetentionPolicy; public float getFontSize() { return fontSize; } - - public TtmlStyle setVerticalType(@VerticalType int verticalType) { - this.verticalType = verticalType; - return this; - } - - @VerticalType - public int getVerticalType() { - return verticalType; - } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/text/ttml/TtmlStyleTest.java b/library/core/src/test/java/com/google/android/exoplayer2/text/ttml/TtmlStyleTest.java index 4f75c50b12..a3ad1ba599 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/text/ttml/TtmlStyleTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/text/ttml/TtmlStyleTest.java @@ -28,7 +28,6 @@ import android.graphics.Color; import android.text.Layout; import androidx.annotation.ColorInt; import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.span.RubySpan; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,7 +46,6 @@ public final class TtmlStyleTest { private static final int RUBY_POSITION = RubySpan.POSITION_UNDER; private static final Layout.Alignment TEXT_ALIGN = Layout.Alignment.ALIGN_CENTER; private static final boolean TEXT_COMBINE = true; - @Cue.VerticalType private static final int VERTICAL_TYPE = Cue.VERTICAL_TYPE_RL; private final TtmlStyle populatedStyle = new TtmlStyle() @@ -64,8 +62,7 @@ public final class TtmlStyleTest { .setRubyType(RUBY_TYPE) .setRubyPosition(RUBY_POSITION) .setTextAlign(TEXT_ALIGN) - .setTextCombine(TEXT_COMBINE) - .setVerticalType(VERTICAL_TYPE); + .setTextCombine(TEXT_COMBINE); @Test public void inheritStyle() { @@ -89,9 +86,6 @@ public final class TtmlStyleTest { assertWithMessage("backgroundColor should not be inherited") .that(style.hasBackgroundColor()) .isFalse(); - assertWithMessage("verticalType should not be inherited") - .that(style.getVerticalType()) - .isEqualTo(Cue.TYPE_UNSET); } @Test @@ -115,9 +109,6 @@ public final class TtmlStyleTest { .that(style.getBackgroundColor()) .isEqualTo(BACKGROUND_COLOR); assertWithMessage("rubyType should be chained").that(style.getRubyType()).isEqualTo(RUBY_TYPE); - assertWithMessage("verticalType should be chained") - .that(style.getVerticalType()) - .isEqualTo(VERTICAL_TYPE); } @Test diff --git a/testdata/src/test/assets/ttml/vertical_text.xml b/testdata/src/test/assets/ttml/vertical_text.xml index 181e93114c..49bbb3caab 100644 --- a/testdata/src/test/assets/ttml/vertical_text.xml +++ b/testdata/src/test/assets/ttml/vertical_text.xml @@ -3,12 +3,19 @@ xmlns:tts="http://www.w3.org/2006/10/ttaf1#style" xmlns="http://www.w3.org/ns/ttml" xmlns="http://www.w3.org/2006/10/ttaf1"> +
+Vertical right-to-left (e.g. Japanese)
+Vertical right-to-left (e.g. Japanese)
Vertical left-to-right (e.g. Mongolian)
+Vertical left-to-right (e.g. Mongolian)
Horizontal text