Apply ColorSpan if subtitle cue startTag contains color class

This commit is contained in:
Daniele Bonaldo 2018-04-24 17:14:31 +01:00
parent 18b1ec7a2f
commit 117239a709
2 changed files with 17 additions and 0 deletions

View File

@ -33,6 +33,7 @@ import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
import android.util.Log; import android.util.Log;
import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.util.ColorParser;
import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.ParsableByteArray;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -380,6 +381,8 @@ public final class WebvttCueParser {
text.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break; break;
case TAG_CLASS: case TAG_CLASS:
applySupportedClasses(text, startTag.classes, start, end);
break;
case TAG_LANG: case TAG_LANG:
case TAG_VOICE: case TAG_VOICE:
case "": // Case of the "whole cue" virtual tag. 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, private static void applyStyleToText(SpannableStringBuilder spannedText, WebvttCssStyle style,
int start, int end) { int start, int end) {
if (style == null) { if (style == null) {

View File

@ -43,6 +43,10 @@ public final class ColorParser {
private static final Map<String, Integer> COLOR_MAP; private static final Map<String, Integer> COLOR_MAP;
public static boolean isNamedColor(String expression) {
return COLOR_MAP.containsKey(expression);
}
/** /**
* Parses a TTML color expression. * Parses a TTML color expression.
* *