Commit 987939d3 by Daniele Bonaldo

Add suport for text background color classes

parent fc5dbfeb
...@@ -140,6 +140,7 @@ public final class WebvttCueParser { ...@@ -140,6 +140,7 @@ public final class WebvttCueParser {
private static final String TAG = "WebvttCueParser"; private static final String TAG = "WebvttCueParser";
private static final Map<String, Integer> DEFAULT_COLORS; private static final Map<String, Integer> DEFAULT_COLORS;
private static final Map<String, Integer> DEFAULT_BACKGROUND_COLORS;
static { static {
DEFAULT_COLORS = new HashMap<>(); DEFAULT_COLORS = new HashMap<>();
...@@ -151,6 +152,16 @@ public final class WebvttCueParser { ...@@ -151,6 +152,16 @@ public final class WebvttCueParser {
DEFAULT_COLORS.put("red", 0xFFFF0000); DEFAULT_COLORS.put("red", 0xFFFF0000);
DEFAULT_COLORS.put("white", 0xFFFFFFFF); DEFAULT_COLORS.put("white", 0xFFFFFFFF);
DEFAULT_COLORS.put("yellow", 0xFFFFFF00); 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 { ...@@ -551,9 +562,13 @@ public final class WebvttCueParser {
int start, int end) { int start, int end) {
for (String className : classes) { for (String className : classes) {
if (DEFAULT_COLORS.containsKey(className)) { 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); 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);
}
} }
} }
......
...@@ -96,6 +96,22 @@ public final class WebvttCueParserTest { ...@@ -96,6 +96,22 @@ public final class WebvttCueParserTest {
} }
@Test @Test
public void testBackgroundTextColorClass() throws Exception {
Spanned text = parseCueText("In this sentence <c.bg_red>this text</c> 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 <c.bg_papayawhip>this text</c> 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 { public void testParseWellFormedUnclosedEndAtCueEnd() throws Exception {
Spanned text = parseCueText("An <u some trailing stuff>unclosed u tag with " Spanned text = parseCueText("An <u some trailing stuff>unclosed u tag with "
+ "<i>italic</i> inside"); + "<i>italic</i> inside");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment