From 9c35d76184c9359e3da80b101791010fd11b6096 Mon Sep 17 00:00:00 2001 From: Arnold Szabo Date: Mon, 22 Feb 2021 20:33:12 +0100 Subject: [PATCH] Parse font size into float, and fallback to Cue.DIMEN_UNSET instead of null. --- demos/main/src/main/assets/media.exolist.json | 7 ------- .../exoplayer2/text/ssa/SsaDecoder.java | 2 +- .../android/exoplayer2/text/ssa/SsaStyle.java | 11 +++++----- .../exoplayer2/text/ssa/SsaDecoderTest.java | 20 +++++++++++++++++-- .../assets/media/ssa/{colors => style_colors} | 0 .../src/test/assets/media/ssa/style_font_size | 18 +++++++++++++++++ 6 files changed, 43 insertions(+), 15 deletions(-) rename testdata/src/test/assets/media/ssa/{colors => style_colors} (100%) create mode 100644 testdata/src/test/assets/media/ssa/style_font_size diff --git a/demos/main/src/main/assets/media.exolist.json b/demos/main/src/main/assets/media.exolist.json index 9a61ba673c..b515eca98a 100644 --- a/demos/main/src/main/assets/media.exolist.json +++ b/demos/main/src/main/assets/media.exolist.json @@ -506,13 +506,6 @@ "subtitle_mime_type": "text/x-ssa", "subtitle_language": "en" }, - { - "name": "SubStation Alpha font size", - "uri": "https://storage.googleapis.com/exoplayer-test-media-1/gen-3/screens/dash-vod-single-segment/video-avc-baseline-480.mp4", - "subtitle_uri": "https://drive.google.com/uc?export=download&id=13EdW4Qru-vQerUlwS_Ht5Cely_Tn0tQe", - "subtitle_mime_type": "text/x-ssa", - "subtitle_language": "en" - }, { "name": "MPEG-4 Timed Text", "uri": "https://storage.googleapis.com/exoplayer-test-media-1/mp4/dizzy-with-tx3g.mp4" diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java index 032df6c2ab..d8d1523b29 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java @@ -314,7 +314,7 @@ public final class SsaDecoder extends SimpleSubtitleDecoder { /* end= */ spannableText.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); } - if (style.fontSize != null && screenHeight != Cue.DIMEN_UNSET) { + if (style.fontSize != Cue.DIMEN_UNSET && screenHeight != Cue.DIMEN_UNSET) { cue.setTextSize( style.fontSize / screenHeight, Cue.TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaStyle.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaStyle.java index 34d5528917..554e576710 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaStyle.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaStyle.java @@ -27,6 +27,7 @@ import androidx.annotation.ColorInt; import androidx.annotation.IntDef; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; @@ -90,13 +91,13 @@ import java.util.regex.Pattern; public final String name; @SsaAlignment public final int alignment; @Nullable @ColorInt public final Integer primaryColor; - public final Integer fontSize; + public final float fontSize; private SsaStyle( String name, @SsaAlignment int alignment, @Nullable @ColorInt Integer primaryColor, - Integer fontSize) { + float fontSize) { this.name = name; this.alignment = alignment; this.primaryColor = primaryColor; @@ -197,12 +198,12 @@ import java.util.regex.Pattern; return Color.argb(a, r, g, b); } - private static Integer parseFontSize(String fontSize) { + private static float parseFontSize(String fontSize) { try { - return Integer.parseInt(fontSize); + return Float.parseFloat(fontSize); } catch (NumberFormatException e) { Log.w(TAG, "Failed to parse font size: '" + fontSize + "'", e); - return null; + return Cue.DIMEN_UNSET; } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java b/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java index a734019d09..8dc98d3335 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java @@ -47,7 +47,8 @@ public final class SsaDecoderTest { private static final String INVALID_TIMECODES = "media/ssa/invalid_timecodes"; private static final String INVALID_POSITIONS = "media/ssa/invalid_positioning"; private static final String POSITIONS_WITHOUT_PLAYRES = "media/ssa/positioning_without_playres"; - private static final String COLORS = "media/ssa/colors"; + private static final String STYLE_COLORS = "media/ssa/style_colors"; + private static final String STYLE_FONT_SIZE = "media/ssa/style_font_size"; @Test public void decodeEmpty() throws IOException { @@ -274,7 +275,7 @@ public final class SsaDecoderTest { @Test public void decodeColors() throws IOException { SsaDecoder decoder = new SsaDecoder(); - byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), COLORS); + byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), STYLE_COLORS); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); assertThat(subtitle.getEventTimeCount()).isEqualTo(14); // &H000000FF (AABBGGRR) -> #FFFF0000 (AARRGGBB) @@ -319,6 +320,21 @@ public final class SsaDecoderTest { .hasNoForegroundColorSpanBetween(0, seventhCueText.length()); } + @Test + public void decodeFontSize() throws IOException{ + SsaDecoder decoder = new SsaDecoder(); + byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), STYLE_FONT_SIZE); + Subtitle subtitle = decoder.decode(bytes, bytes.length, false); + assertThat(subtitle.getEventTimeCount()).isEqualTo(4); + + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.textSize).isEqualTo(30f/720f); + assertThat(firstCue.textSizeType).isEqualTo(Cue.TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2 ))); + assertThat(secondCue.textSize).isEqualTo(72.2f/720f); + assertThat(secondCue.textSizeType).isEqualTo(Cue.TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING); + } + private static void assertTypicalCue1(Subtitle subtitle, int eventIndex) { assertThat(subtitle.getEventTime(eventIndex)).isEqualTo(0); assertThat(subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()) diff --git a/testdata/src/test/assets/media/ssa/colors b/testdata/src/test/assets/media/ssa/style_colors similarity index 100% rename from testdata/src/test/assets/media/ssa/colors rename to testdata/src/test/assets/media/ssa/style_colors diff --git a/testdata/src/test/assets/media/ssa/style_font_size b/testdata/src/test/assets/media/ssa/style_font_size new file mode 100644 index 0000000000..d43a8b4390 --- /dev/null +++ b/testdata/src/test/assets/media/ssa/style_font_size @@ -0,0 +1,18 @@ +[Script Info] +Title: SSA/ASS Test +Original Script: Arnold Szabo +Script Type: V4.00+ +PlayResX: 1280 +PlayResY: 720 + +[V4+ Styles] +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding +Style: FontSizeSmall ,Roboto,30, &H000000FF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 +Style: FontSizeBig ,Roboto,72.2,&H000000FF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 + + + +[Events] +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text +Dialogue: 0,0:00:00.95,0:00:03.11,FontSizeSmall ,Arnold,0,0,0,,First line with font size 30. +Dialogue: 0,0:00:08.50,0:00:11.50,FontSizeBig ,Arnold,0,0,0,,Second line with font size 72.2. \ No newline at end of file