From fc5dbfeba4d009f720aede0ce738def842827be5 Mon Sep 17 00:00:00 2001 From: Daniele Bonaldo Date: Thu, 13 Feb 2020 17:02:13 +0100 Subject: [PATCH] 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 "