From 117239a709410a307d63eb0a7916a24722976b98 Mon Sep 17 00:00:00 2001 From: Daniele Bonaldo Date: Tue, 24 Apr 2018 17:14:31 +0100 Subject: [PATCH 1/3] Apply ColorSpan if subtitle cue startTag contains color class --- .../exoplayer2/text/webvtt/WebvttCueParser.java | 13 +++++++++++++ .../google/android/exoplayer2/util/ColorParser.java | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java index 80ebecdc0e..4ef78ffb87 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java @@ -33,6 +33,7 @@ import android.text.style.TypefaceSpan; import android.text.style.UnderlineSpan; import android.util.Log; import com.google.android.exoplayer2.text.Cue; +import com.google.android.exoplayer2.util.ColorParser; import com.google.android.exoplayer2.util.ParsableByteArray; import java.util.ArrayList; import java.util.Arrays; @@ -380,6 +381,8 @@ public final class WebvttCueParser { text.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); break; case TAG_CLASS: + applySupportedClasses(text, startTag.classes, start, end); + break; case TAG_LANG: case TAG_VOICE: case "": // Case of the "whole cue" virtual tag. @@ -395,6 +398,16 @@ public final class WebvttCueParser { } } + private static void applySupportedClasses(SpannableStringBuilder text, String[] classes, + int start, int end) { + for (String className : classes) { + if (ColorParser.isNamedColor(className)) { + int color = ColorParser.parseCssColor(className); + text.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } + } + private static void applyStyleToText(SpannableStringBuilder spannedText, WebvttCssStyle style, int start, int end) { if (style == null) { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java b/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java index a9df80e9fe..87202663b8 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java @@ -43,6 +43,10 @@ public final class ColorParser { private static final Map COLOR_MAP; + public static boolean isNamedColor(String expression) { + return COLOR_MAP.containsKey(expression); + } + /** * Parses a TTML color expression. * From fc5dbfeba4d009f720aede0ce738def842827be5 Mon Sep 17 00:00:00 2001 From: Daniele Bonaldo Date: Thu, 13 Feb 2020 17:02:13 +0100 Subject: [PATCH 2/3] Add default colors list in WebvttCueParser for text foreground class matching with tests TIL: papayawhip is a color --- .../text/webvtt/WebvttCueParser.java | 23 +++++++++++++++---- .../android/exoplayer2/util/ColorParser.java | 4 ---- .../text/webvtt/WebvttCueParserTest.java | 16 +++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java index 4bc2152036..4b413d2ad6 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java @@ -40,7 +40,6 @@ import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan; import com.google.android.exoplayer2.text.span.RubySpan; import com.google.android.exoplayer2.util.Assertions; -import com.google.android.exoplayer2.util.ColorParser; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.Util; @@ -49,7 +48,9 @@ import java.lang.annotation.Retention; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -138,6 +139,20 @@ public final class WebvttCueParser { private static final String TAG = "WebvttCueParser"; + private static final Map DEFAULT_COLORS; + + static { + DEFAULT_COLORS = new HashMap<>(); + DEFAULT_COLORS.put("black", 0xFF000000); + DEFAULT_COLORS.put("blue", 0xFF0000FF); + DEFAULT_COLORS.put("cyan", 0xFF00FFFF); + DEFAULT_COLORS.put("lime", 0xFF00FF00); + DEFAULT_COLORS.put("magenta", 0xFFFF00FF); + DEFAULT_COLORS.put("red", 0xFFFF0000); + DEFAULT_COLORS.put("white", 0xFFFFFFFF); + DEFAULT_COLORS.put("yellow", 0xFFFFFF00); + } + /** * Parses the next valid WebVTT cue in a parsable array, including timestamps, settings and text. * @@ -535,9 +550,9 @@ public final class WebvttCueParser { private static void applySupportedClasses(SpannableStringBuilder text, String[] classes, int start, int end) { for (String className : classes) { - if (ColorParser.isNamedColor(className)) { - int color = ColorParser.parseCssColor(className); - text.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + if (DEFAULT_COLORS.containsKey(className)) { + int color = DEFAULT_COLORS.get(Util.toLowerInvariant(className)); + text.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java b/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java index 27f1a0c6d3..54f52e0a14 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/util/ColorParser.java @@ -43,10 +43,6 @@ public final class ColorParser { private static final Map COLOR_MAP; - public static boolean isNamedColor(String expression) { - return COLOR_MAP.containsKey(expression); - } - /** * Parses a TTML color expression. * diff --git a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java index aa83fbc8ed..ab8ac4ed9a 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java @@ -79,6 +79,22 @@ public final class WebvttCueParserTest { .withTextAndPosition("", RubySpan.POSITION_OVER); } + @Test + public void testForegroundTextColorClass() throws Exception { + Spanned text = parseCueText("In this sentence this text is red"); + + assertThat(text.toString()).isEqualTo("In this sentence this text is red"); + assertThat(text).hasForegroundColorSpanBetween("In this sentence ".length(), "In this sentence this text".length()); + } + + @Test + public void testUnsupportedColorForForegroundTextColorClass() throws Exception { + Spanned text = parseCueText("In this sentence this text is not papaya"); + + assertThat(text.toString()).isEqualTo("In this sentence this text is not papaya"); + assertThat(text).hasNoSpans(); + } + @Test public void testParseWellFormedUnclosedEndAtCueEnd() throws Exception { Spanned text = parseCueText("An unclosed u tag with " From 987939d306f3a464bbd3629f7c1052d75c81ebd9 Mon Sep 17 00:00:00 2001 From: Daniele Bonaldo Date: Thu, 13 Feb 2020 17:37:21 +0100 Subject: [PATCH 3/3] Add suport for text background color classes --- .../exoplayer2/text/webvtt/WebvttCueParser.java | 17 ++++++++++++++++- .../text/webvtt/WebvttCueParserTest.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java index 4b413d2ad6..1f7f7eddf2 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParser.java @@ -140,6 +140,7 @@ public final class WebvttCueParser { private static final String TAG = "WebvttCueParser"; private static final Map DEFAULT_COLORS; + private static final Map DEFAULT_BACKGROUND_COLORS; static { DEFAULT_COLORS = new HashMap<>(); @@ -151,6 +152,16 @@ public final class WebvttCueParser { DEFAULT_COLORS.put("red", 0xFFFF0000); DEFAULT_COLORS.put("white", 0xFFFFFFFF); DEFAULT_COLORS.put("yellow", 0xFFFFFF00); + + DEFAULT_BACKGROUND_COLORS = new HashMap<>(); + DEFAULT_BACKGROUND_COLORS.put("bg_black", 0xFF000000); + DEFAULT_BACKGROUND_COLORS.put("bg_blue", 0xFF0000FF); + DEFAULT_BACKGROUND_COLORS.put("bg_cyan", 0xFF00FFFF); + DEFAULT_BACKGROUND_COLORS.put("bg_lime", 0xFF00FF00); + DEFAULT_BACKGROUND_COLORS.put("bg_magenta", 0xFFFF00FF); + DEFAULT_BACKGROUND_COLORS.put("bg_red", 0xFFFF0000); + DEFAULT_BACKGROUND_COLORS.put("bg_white", 0xFFFFFFFF); + DEFAULT_BACKGROUND_COLORS.put("bg_yellow", 0xFFFFFF00); } /** @@ -551,9 +562,13 @@ public final class WebvttCueParser { int start, int end) { for (String className : classes) { if (DEFAULT_COLORS.containsKey(className)) { - int color = DEFAULT_COLORS.get(Util.toLowerInvariant(className)); + int color = DEFAULT_COLORS.get(className); text.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } + if (DEFAULT_BACKGROUND_COLORS.containsKey(className)) { + int color = DEFAULT_BACKGROUND_COLORS.get(className); + text.setSpan(new BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java index ab8ac4ed9a..b263ba91c9 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttCueParserTest.java @@ -95,6 +95,22 @@ public final class WebvttCueParserTest { assertThat(text).hasNoSpans(); } + @Test + public void testBackgroundTextColorClass() throws Exception { + Spanned text = parseCueText("In this sentence this text has a red background"); + + assertThat(text.toString()).isEqualTo("In this sentence this text has a red background"); + assertThat(text).hasBackgroundColorSpanBetween("In this sentence ".length(), "In this sentence this text".length()); + } + + @Test + public void testUnsupportedColorForBackgroundTextColorClass() throws Exception { + Spanned text = parseCueText("In this sentence this text doesn't have a papaya background"); + + assertThat(text.toString()).isEqualTo("In this sentence this text doesn't have a papaya background"); + assertThat(text).hasNoSpans(); + } + @Test public void testParseWellFormedUnclosedEndAtCueEnd() throws Exception { Spanned text = parseCueText("An unclosed u tag with "