Add suport for text background color classes

This commit is contained in:
Daniele Bonaldo 2020-02-13 17:37:21 +01:00
parent fc5dbfeba4
commit 987939d306
2 changed files with 32 additions and 1 deletions

View file

@ -140,6 +140,7 @@ public final class WebvttCueParser {
private static final String TAG = "WebvttCueParser";
private static final Map<String, Integer> DEFAULT_COLORS;
private static final Map<String, Integer> 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);
}
}
}

View file

@ -95,6 +95,22 @@ public final class WebvttCueParserTest {
assertThat(text).hasNoSpans();
}
@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 {
Spanned text = parseCueText("An <u some trailing stuff>unclosed u tag with "