Commit 81383f80 by aquilescanta Committed by Oliver Woodman

Add full selector support to CSS in WebVTT

This CL allows near-complete support to CSS selectors (I say near because not every
CSS rule applies to WebVTT).
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=120717498
parent 5cbf75b6
WEBVTT
STYLE
::cue(\n#id ){text-decoration:underline;}
STYLE
::cue(#id.class1.class2 ){ color: violet;}
STYLE
::cue(lang){font-family:Courier}
STYLE
::cue(.class.another ){font-weight: bold;}
STYLE
::cue(v.class[voice="Strider Trancos"] ){ font-weight:bold; }
STYLE
::cue(v#anId.class1.class2[voice="Robert"] ){ font-style:italic; }
id
00:00.000 --> 00:01.001
This should be underlined and <lang.class1.class2> courier and violet.
íd
00:02.000 --> 00:02.001
This <lang.class1.class2>should be just courier.
_id
00:02.500 --> 00:02.501
This <lang.class.another>should be courier and bold.
00:04.000 --> 00:04.001
This <v Strider Trancos> shouldn't be bold.</v>
This <v.class.clazz Strider Trancos> should be bold.
anId
00:05.000 --> 00:05.001
This is <v.class1.class3.class2 Pipo> specific </v>
<v.class1.class3.class2 Robert> But this is more italic</v>
......@@ -19,9 +19,6 @@ import com.google.android.exoplayer.util.ParsableByteArray;
import android.test.InstrumentationTestCase;
import java.util.HashMap;
import java.util.Map;
/**
* Unit test for {@link CssParser}.
*/
......@@ -82,75 +79,46 @@ public final class CssParserTest extends InstrumentationTestCase {
}
public void testParseMethodSimpleInput() {
String styleBlock = " ::cue { color : black; background-color: PapayaWhip }";
// Expected style map construction.
Map<String, WebvttCssStyle> expectedResult = new HashMap<>();
expectedResult.put("", new WebvttCssStyle());
WebvttCssStyle style = expectedResult.get("");
style.setFontColor(0xFF000000);
style.setBackgroundColor(0xFFFFEFD5);
assertCssProducesExpectedMap(expectedResult, new String[] { styleBlock });
}
public void testParseSimpleInputSeparately() {
String styleBlock1 = " ::cue { color : black }\n\n::cue { color : invalid }";
String styleBlock2 = " \n::cue {\n background-color\n:#00fFFe}";
// Expected style map construction.
Map<String, WebvttCssStyle> expectedResult = new HashMap<>();
expectedResult.put("", new WebvttCssStyle());
WebvttCssStyle style = expectedResult.get("");
style.setFontColor(0xFF000000);
style.setBackgroundColor(0xFF00FFFE);
assertCssProducesExpectedMap(expectedResult, new String[] { styleBlock1, styleBlock2 });
}
public void testDifferentSelectors() {
String styleBlock1 = " ::cue(\n#id ){text-decoration:underline;}";
String styleBlock2 = "::cue(elem ){font-family:Courier}";
String styleBlock3 = "::cue(.class ){font-weight: bold;}";
String styleBlock1 = " ::cue { color : black; background-color: PapayaWhip }";
WebvttCssStyle expectedStyle = new WebvttCssStyle();
expectedStyle.setFontColor(0xFF000000);
expectedStyle.setBackgroundColor(0xFFFFEFD5);
assertParserProduces(expectedStyle, styleBlock1);
// Expected style map construction.
Map<String, WebvttCssStyle> expectedResult = new HashMap<>();
expectedResult.put("#id", new WebvttCssStyle().setUnderline(true));
expectedResult.put("elem", new WebvttCssStyle().setFontFamily("courier"));
expectedResult.put(".class", new WebvttCssStyle().setBold(true));
String styleBlock2 = " ::cue { color : black }\n\n::cue { color : invalid }";
expectedStyle = new WebvttCssStyle();
expectedStyle.setFontColor(0xFF000000);
assertParserProduces(expectedStyle, styleBlock2);
assertCssProducesExpectedMap(expectedResult, new String[] { styleBlock1, styleBlock2,
styleBlock3});
String styleBlock3 = " \n::cue {\n background-color\n:#00fFFe}";
expectedStyle = new WebvttCssStyle();
expectedStyle.setBackgroundColor(0xFF00FFFE);
assertParserProduces(expectedStyle, styleBlock3);
}
public void testMultiplePropertiesInBlock() {
String styleBlock = "::cue(#id){text-decoration:underline; background-color:green;"
+ "color:red; font-family:Courier; font-weight:bold}";
// Expected style map construction.
Map<String, WebvttCssStyle> expectedResult = new HashMap<>();
WebvttCssStyle expectedStyle = new WebvttCssStyle();
expectedResult.put("#id", expectedStyle);
expectedStyle.setTargetId("id");
expectedStyle.setUnderline(true);
expectedStyle.setBackgroundColor(0xFF008000);
expectedStyle.setFontColor(0xFFFF0000);
expectedStyle.setFontFamily("courier");
expectedStyle.setBold(true);
assertCssProducesExpectedMap(expectedResult, new String[] { styleBlock });
assertParserProduces(expectedStyle, styleBlock);
}
public void testRgbaColorExpression() {
String styleBlock = "::cue(#rgb){background-color: rgba(\n10/* Ugly color */,11\t, 12\n,.1);"
+ "color:rgb(1,1,\n1)}";
// Expected style map construction.
Map<String, WebvttCssStyle> expectedResult = new HashMap<>();
WebvttCssStyle expectedStyle = new WebvttCssStyle();
expectedResult.put("#rgb", expectedStyle);
expectedStyle.setTargetId("rgb");
expectedStyle.setBackgroundColor(0x190A0B0C);
expectedStyle.setFontColor(0xFF010101);
assertCssProducesExpectedMap(expectedResult, new String[] { styleBlock });
assertParserProduces(expectedStyle, styleBlock);
}
public void testGetNextToken() {
......@@ -181,20 +149,20 @@ public final class CssParserTest extends InstrumentationTestCase {
public void testStyleScoreSystem() {
WebvttCssStyle style = new WebvttCssStyle();
// Universal selector.
assertEquals(1, style.getSpecificityScore(null, null, new String[0], null));
assertEquals(1, style.getSpecificityScore("", "", new String[0], ""));
// Class match without tag match.
style.setTargetClasses(new String[] { "class1", "class2"});
assertEquals(8, style.getSpecificityScore(null, null,
new String[] { "class1", "class2", "class3" }, null));
assertEquals(8, style.getSpecificityScore("", "", new String[] { "class1", "class2", "class3" },
""));
// Class and tag match
style.setTargetTagName("b");
assertEquals(10, style.getSpecificityScore(null, "b",
new String[] { "class1", "class2", "class3" }, null));
assertEquals(10, style.getSpecificityScore("", "b",
new String[] { "class1", "class2", "class3" }, ""));
// Class insufficiency.
assertEquals(0, style.getSpecificityScore(null, "b", new String[] { "class1", "class" }, null));
assertEquals(0, style.getSpecificityScore("", "b", new String[] { "class1", "class" }, ""));
// Voice, classes and tag match.
style.setTargetVoice("Manuel Cráneo");
assertEquals(14, style.getSpecificityScore(null, "b",
assertEquals(14, style.getSpecificityScore("", "b",
new String[] { "class1", "class2", "class3" }, "Manuel Cráneo"));
// Voice mismatch.
assertEquals(0, style.getSpecificityScore(null, "b",
......@@ -205,7 +173,7 @@ public final class CssParserTest extends InstrumentationTestCase {
new String[] { "class1", "class2", "class3" }, "Manuel Cráneo"));
// Id mismatch.
assertEquals(0, style.getSpecificityScore("id1", "b",
new String[] { "class1", "class2", "class3" }, null));
new String[] { "class1", "class2", "class3" }, ""));
}
// Utility methods.
......@@ -222,38 +190,25 @@ public final class CssParserTest extends InstrumentationTestCase {
assertEquals(expectedLine, input.readLine());
}
private void assertCssProducesExpectedMap(Map<String, WebvttCssStyle> expectedResult,
String[] styleBlocks){
Map<String, WebvttCssStyle> actualStyleMap = new HashMap<>();
for (String s : styleBlocks) {
ParsableByteArray input = new ParsableByteArray(s.getBytes());
parser.parseBlock(input, actualStyleMap);
}
assertStyleMapsAreEqual(expectedResult, actualStyleMap);
}
private void assertStyleMapsAreEqual(Map<String, WebvttCssStyle> expected,
Map<String, WebvttCssStyle> actual) {
assertEquals(expected.size(), actual.size());
for (String k : expected.keySet()) {
WebvttCssStyle expectedElem = expected.get(k);
WebvttCssStyle actualElem = actual.get(k);
assertEquals(expectedElem.hasBackgroundColor(), actualElem.hasBackgroundColor());
if (expectedElem.hasBackgroundColor()) {
assertEquals(expectedElem.getBackgroundColor(), actualElem.getBackgroundColor());
}
assertEquals(expectedElem.hasFontColor(), actualElem.hasFontColor());
if (expectedElem.hasFontColor()) {
assertEquals(expectedElem.getFontColor(), actualElem.getFontColor());
}
assertEquals(expectedElem.getFontFamily(), actualElem.getFontFamily());
assertEquals(expectedElem.getFontSize(), actualElem.getFontSize());
assertEquals(expectedElem.getFontSizeUnit(), actualElem.getFontSizeUnit());
assertEquals(expectedElem.getStyle(), actualElem.getStyle());
assertEquals(expectedElem.isLinethrough(), actualElem.isLinethrough());
assertEquals(expectedElem.isUnderline(), actualElem.isUnderline());
assertEquals(expectedElem.getTextAlign(), actualElem.getTextAlign());
}
private void assertParserProduces(WebvttCssStyle expected,
String styleBlock){
ParsableByteArray input = new ParsableByteArray(styleBlock.getBytes());
WebvttCssStyle actualElem = parser.parseBlock(input);
assertEquals(expected.hasBackgroundColor(), actualElem.hasBackgroundColor());
if (expected.hasBackgroundColor()) {
assertEquals(expected.getBackgroundColor(), actualElem.getBackgroundColor());
}
assertEquals(expected.hasFontColor(), actualElem.hasFontColor());
if (expected.hasFontColor()) {
assertEquals(expected.getFontColor(), actualElem.getFontColor());
}
assertEquals(expected.getFontFamily(), actualElem.getFontFamily());
assertEquals(expected.getFontSize(), actualElem.getFontSize());
assertEquals(expected.getFontSizeUnit(), actualElem.getFontSizeUnit());
assertEquals(expected.getStyle(), actualElem.getStyle());
assertEquals(expected.isLinethrough(), actualElem.isLinethrough());
assertEquals(expected.isUnderline(), actualElem.isUnderline());
assertEquals(expected.getTextAlign(), actualElem.getTextAlign());
}
}
......@@ -223,8 +223,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase {
private static Spanned parseCueText(String string) {
WebvttCue.Builder builder = new WebvttCue.Builder();
WebvttCueParser.parseCueText(null, string, builder,
Collections.<String, WebvttCssStyle>emptyMap());
WebvttCueParser.parseCueText(null, string, builder, Collections.<WebvttCssStyle>emptyList());
return (Spanned) builder.build().text;
}
......
......@@ -26,6 +26,7 @@ import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import java.io.IOException;
......@@ -43,6 +44,7 @@ public class WebvttParserTest extends InstrumentationTestCase {
private static final String WITH_BAD_CUE_HEADER_FILE = "webvtt/with_bad_cue_header";
private static final String WITH_TAGS_FILE = "webvtt/with_tags";
private static final String WITH_CSS_STYLES = "webvtt/with_css_styles";
private static final String WITH_CSS_COMPLEX_SELECTORS = "webvtt/with_css_complex_selectors";
private static final String EMPTY_FILE = "webvtt/empty";
public void testParseEmpty() throws IOException {
......@@ -57,9 +59,7 @@ public class WebvttParserTest extends InstrumentationTestCase {
}
public void testParseTypical() throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_FILE);
WebvttSubtitle subtitle = parser.decode(bytes, bytes.length);
WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_FILE);
// Test event count.
assertEquals(4, subtitle.getEventTimeCount());
......@@ -70,9 +70,7 @@ public class WebvttParserTest extends InstrumentationTestCase {
}
public void testParseTypicalWithIds() throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_WITH_IDS_FILE);
WebvttSubtitle subtitle = parser.decode(bytes, bytes.length);
WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_IDS_FILE);
// Test event count.
assertEquals(4, subtitle.getEventTimeCount());
......@@ -83,9 +81,7 @@ public class WebvttParserTest extends InstrumentationTestCase {
}
public void testParseTypicalWithComments() throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_WITH_COMMENTS_FILE);
WebvttSubtitle subtitle = parser.decode(bytes, bytes.length);
WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_COMMENTS_FILE);
// test event count
assertEquals(4, subtitle.getEventTimeCount());
......@@ -96,9 +92,7 @@ public class WebvttParserTest extends InstrumentationTestCase {
}
public void testParseWithTags() throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), WITH_TAGS_FILE);
WebvttSubtitle subtitle = parser.decode(bytes, bytes.length);
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_TAGS_FILE);
// Test event count.
assertEquals(8, subtitle.getEventTimeCount());
......@@ -111,13 +105,9 @@ public class WebvttParserTest extends InstrumentationTestCase {
}
public void testParseWithPositioning() throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), WITH_POSITIONING_FILE);
WebvttSubtitle subtitle = parser.decode(bytes, bytes.length);
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_POSITIONING_FILE);
// Test event count.
assertEquals(12, subtitle.getEventTimeCount());
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.", Alignment.ALIGN_NORMAL,
Cue.DIMEN_UNSET, Cue.TYPE_UNSET, Cue.TYPE_UNSET, 0.1f, Cue.ANCHOR_TYPE_START, 0.35f);
......@@ -139,9 +129,7 @@ public class WebvttParserTest extends InstrumentationTestCase {
}
public void testParseWithBadCueHeader() throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), WITH_BAD_CUE_HEADER_FILE);
WebvttSubtitle subtitle = parser.decode(bytes, bytes.length);
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_BAD_CUE_HEADER_FILE);
// Test event count.
assertEquals(4, subtitle.getEventTimeCount());
......@@ -152,9 +140,7 @@ public class WebvttParserTest extends InstrumentationTestCase {
}
public void testWebvttWithCssStyle() throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), WITH_CSS_STYLES);
WebvttSubtitle subtitle = parser.decode(bytes, bytes.length);
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_STYLES);
// Test event count.
assertEquals(8, subtitle.getEventTimeCount());
......@@ -163,14 +149,10 @@ public class WebvttParserTest extends InstrumentationTestCase {
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
Cue cue1 = subtitle.getCues(0).get(0);
Cue cue2 = subtitle.getCues(2345000).get(0);
Cue cue3 = subtitle.getCues(20000000).get(0);
Cue cue4 = subtitle.getCues(25000000).get(0);
Spanned s1 = (Spanned) cue1.text;
Spanned s2 = (Spanned) cue2.text;
Spanned s3 = (Spanned) cue3.text;
Spanned s4 = (Spanned) cue4.text;
Spanned s1 = getUniqueSpanTextAt(subtitle, 0);
Spanned s2 = getUniqueSpanTextAt(subtitle, 2345000);
Spanned s3 = getUniqueSpanTextAt(subtitle, 20000000);
Spanned s4 = getUniqueSpanTextAt(subtitle, 25000000);
assertEquals(1, s1.getSpans(0, s1.length(), ForegroundColorSpan.class).length);
assertEquals(1, s1.getSpans(0, s1.length(), BackgroundColorSpan.class).length);
assertEquals(2, s2.getSpans(0, s2.length(), ForegroundColorSpan.class).length);
......@@ -180,6 +162,46 @@ public class WebvttParserTest extends InstrumentationTestCase {
assertEquals(Typeface.BOLD, s4.getSpans(17, s4.length(), StyleSpan.class)[0].getStyle());
}
public void testWithComplexCssSelectors() throws IOException {
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_COMPLEX_SELECTORS);
Spanned text = getUniqueSpanTextAt(subtitle, 0);
assertEquals(1, text.getSpans(30, text.length(), ForegroundColorSpan.class).length);
assertEquals(0xFFEE82EE,
text.getSpans(30, text.length(), ForegroundColorSpan.class)[0].getForegroundColor());
assertEquals(1, text.getSpans(30, text.length(), TypefaceSpan.class).length);
assertEquals("courier", text.getSpans(30, text.length(), TypefaceSpan.class)[0].getFamily());
text = getUniqueSpanTextAt(subtitle, 2000000);
assertEquals(1, text.getSpans(5, text.length(), TypefaceSpan.class).length);
assertEquals("courier", text.getSpans(5, text.length(), TypefaceSpan.class)[0].getFamily());
text = getUniqueSpanTextAt(subtitle, 2500000);
assertEquals(1, text.getSpans(5, text.length(), StyleSpan.class).length);
assertEquals(Typeface.BOLD, text.getSpans(5, text.length(), StyleSpan.class)[0].getStyle());
assertEquals(1, text.getSpans(5, text.length(), TypefaceSpan.class).length);
assertEquals("courier", text.getSpans(5, text.length(), TypefaceSpan.class)[0].getFamily());
text = getUniqueSpanTextAt(subtitle, 4000000);
assertEquals(0, text.getSpans(6, 22, StyleSpan.class).length);
assertEquals(1, text.getSpans(30, text.length(), StyleSpan.class).length);
assertEquals(Typeface.BOLD, text.getSpans(30, text.length(), StyleSpan.class)[0].getStyle());
text = getUniqueSpanTextAt(subtitle, 5000000);
assertEquals(0, text.getSpans(9, 17, StyleSpan.class).length);
assertEquals(1, text.getSpans(19, text.length(), StyleSpan.class).length);
assertEquals(Typeface.ITALIC, text.getSpans(19, text.length(), StyleSpan.class)[0].getStyle());
}
private WebvttSubtitle getSubtitleForTestAsset(String asset) throws IOException {
WebvttParser parser = new WebvttParser();
byte[] bytes = TestUtil.getByteArray(getInstrumentation(), asset);
return parser.decode(bytes, bytes.length);
}
private Spanned getUniqueSpanTextAt(WebvttSubtitle sub, long timeUs) {
return (Spanned) sub.getCues(timeUs).get(0).text;
}
private static void assertCue(WebvttSubtitle subtitle, int eventTimeIndex, long startTimeUs,
int endTimeUs, String text) {
assertCue(subtitle, eventTimeIndex, startTimeUs, endTimeUs, text, null, Cue.DIMEN_UNSET,
......
......@@ -20,7 +20,9 @@ import com.google.android.exoplayer.util.ParsableByteArray;
import android.text.TextUtils;
import java.util.Map;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Provides a CSS parser for STYLE blocks in Webvtt files. Supports only a subset of the CSS
......@@ -32,9 +34,14 @@ import java.util.Map;
private static final String PROPERTY_FONT_FAMILY = "font-family";
private static final String PROPERTY_FONT_WEIGHT = "font-weight";
private static final String PROPERTY_TEXT_DECORATION = "text-decoration";
private static final String VALUE_BOLD = "bold";
private static final String VALUE_UNDERLINE = "underline";
private static final String BLOCK_START = "{";
private static final String BLOCK_END = "}";
private static final String PROPERTY_FONT_STYLE = "font-style";
private static final String VALUE_ITALIC = "italic";
private static final Pattern VOICE_NAME_PATTERN = Pattern.compile("\\[voice=\"([^\"]*)\"\\]");
// Temporary utility data structures.
private final ParsableByteArray styleInput;
......@@ -51,57 +58,41 @@ import java.util.Map;
* {@code null} otherwise.
*
* @param input The input from which the style block should be read.
* @param styleMap The map that contains styles accessible by selector.
* @return A {@link WebvttCssStyle} that represents the parsed block.
*/
public void parseBlock(ParsableByteArray input, Map<String, WebvttCssStyle> styleMap) {
public WebvttCssStyle parseBlock(ParsableByteArray input) {
stringBuilder.setLength(0);
int initialInputPosition = input.getPosition();
skipStyleBlock(input);
styleInput.reset(input.data, input.getPosition());
styleInput.setPosition(initialInputPosition);
String selector = parseSelector(styleInput, stringBuilder);
if (selector == null) {
return;
}
String token = parseNextToken(styleInput, stringBuilder);
if (!"{".equals(token)) {
return;
}
if (!styleMap.containsKey(selector)) {
styleMap.put(selector, new WebvttCssStyle());
if (selector == null || !BLOCK_START.equals(parseNextToken(styleInput, stringBuilder))) {
return null;
}
WebvttCssStyle style = styleMap.get(selector);
WebvttCssStyle style = new WebvttCssStyle();
applySelectorToStyle(style, selector);
String token = null;
boolean blockEndFound = false;
while (!blockEndFound) {
int position = styleInput.getPosition();
token = parseNextToken(styleInput, stringBuilder);
if (token == null || "}".equals(token)) {
blockEndFound = true;
} else {
blockEndFound = token == null || BLOCK_END.equals(token);
if (!blockEndFound) {
styleInput.setPosition(position);
parseStyleDeclaration(styleInput, style, stringBuilder);
}
}
// Only one style block may appear after a STYLE line.
return BLOCK_END.equals(token) ? style : null; // Check that the style block ended correctly.
}
/**
* Returns a string containing the selector. {@link WebvttCueParser#UNIVERSAL_CUE_ID} is the
* universal selector, and null means syntax error.
*
* <p>Expected inputs are:
* <ul>
* <li>::cue
* <li>::cue(#id)
* <li>::cue(elem)
* <li>::cue(.class)
* <li>::cue(elem.class)
* <li>::cue(v[voice="Someone"])
* </ul>
* Returns a string containing the selector. The input is expected to have the form
* {@code ::cue(tag#id.class1.class2[voice="someone"]}, where every element is optional.
*
* @param input From which the selector is obtained.
* @return A string containing the target, {@link WebvttCueParser#UNIVERSAL_CUE_ID} if the
* selector is universal (targets all cues) or null if an error was encountered.
* @return A string containing the target, empty string if the selector is universal
* (targets all cues) or null if an error was encountered.
*/
private static String parseSelector(ParsableByteArray input, StringBuilder stringBuilder) {
skipWhitespaceAndComments(input);
......@@ -117,9 +108,9 @@ import java.util.Map;
if (token == null) {
return null;
}
if ("{".equals(token)) {
if (BLOCK_START.equals(token)) {
input.setPosition(position);
return WebvttCueParser.UNIVERSAL_CUE_ID;
return "";
}
String target = null;
if ("(".equals(token)) {
......@@ -166,7 +157,7 @@ import java.util.Map;
String token = parseNextToken(input, stringBuilder);
if (";".equals(token)) {
// The style declaration is well formed.
} else if ("}".equals(token)) {
} else if (BLOCK_END.equals(token)) {
// The style declaration is well formed and we can go on, but the closing bracket had to be
// fed back.
input.setPosition(position);
......@@ -189,6 +180,10 @@ import java.util.Map;
if (VALUE_BOLD.equals(value)) {
style.setBold(true);
}
} else if (PROPERTY_FONT_STYLE.equals(property)) {
if (VALUE_ITALIC.equals(value)) {
style.setItalic(true);
}
}
// TODO: Fill remaining supported styles.
}
......@@ -256,7 +251,7 @@ import java.util.Map;
// Syntax error.
return null;
}
if ("}".equals(token) || ";".equals(token)) {
if (BLOCK_END.equals(token) || ";".equals(token)) {
input.setPosition(position);
expressionEndFound = true;
} else {
......@@ -305,5 +300,34 @@ import java.util.Map;
return stringBuilder.toString();
}
}
/**
* Sets the target of a {@link WebvttCssStyle} by splitting a selector of the form
* {@code ::cue(tag#id.class1.class2[voice="someone"]}, where every element is optional.
*/
private void applySelectorToStyle(WebvttCssStyle style, String selector) {
if ("".equals(selector)) {
return; // Universal selector.
}
int voiceStartPosition = selector.indexOf('[');
if (voiceStartPosition != -1) {
Matcher matcher = VOICE_NAME_PATTERN.matcher(selector.substring(voiceStartPosition));
if (matcher.matches()) {
style.setTargetVoice(matcher.group(1));
}
selector = selector.substring(0, voiceStartPosition);
}
String[] classDivision = selector.split("\\.");
String tagAndIdDivision = classDivision[0];
int idPrefixPosition = tagAndIdDivision.indexOf('#');
if (idPrefixPosition != -1) {
style.setTargetTagName(tagAndIdDivision.substring(0, idPrefixPosition));
style.setTargetId(tagAndIdDivision.substring(idPrefixPosition + 1)); // We discard the '#'.
} else {
style.setTargetTagName(tagAndIdDivision);
}
if (classDivision.length > 1) {
style.setTargetClasses(Arrays.copyOfRange(classDivision, 1, classDivision.length));
}
}
}
......@@ -84,7 +84,7 @@ public final class Mp4WebvttParser extends SubtitleParser {
WebvttCueParser.parseCueSettingsList(boxPayload, builder);
} else if (boxType == TYPE_payl) {
WebvttCueParser.parseCueText(null, boxPayload.trim(), builder,
Collections.<String, WebvttCssStyle>emptyMap());
Collections.<WebvttCssStyle>emptyList());
} else {
// Other VTTCueBox children are still not supported and are ignored.
}
......
......@@ -124,8 +124,9 @@ import java.util.List;
public int getSpecificityScore(String id, String tag, String[] classes, String voice) {
if (targetId.isEmpty() && targetTag.isEmpty() && targetClasses.isEmpty()
&& targetVoice.isEmpty()) {
// The selector is universal. It matches with the minimum score.
return 1;
// The selector is universal. It matches with the minimum score if and only if the given
// element is a whole cue.
return tag.isEmpty() ? 1 : 0;
}
int score = 0;
score = updateScoreForMatch(score, targetId, id, 0x40000000);
......
......@@ -22,7 +22,7 @@ import com.google.android.exoplayer.util.ParsableByteArray;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A simple WebVTT parser.
......@@ -43,14 +43,14 @@ public final class WebvttParser extends SubtitleParser {
private final ParsableByteArray parsableWebvttData;
private final WebvttCue.Builder webvttCueBuilder;
private final CssParser cssParser;
private final HashMap<String, WebvttCssStyle> styleMap;
private final List<WebvttCssStyle> definedStyles;
public WebvttParser() {
cueParser = new WebvttCueParser();
parsableWebvttData = new ParsableByteArray();
webvttCueBuilder = new WebvttCue.Builder();
cssParser = new CssParser();
styleMap = new HashMap<>();
definedStyles = new ArrayList<>();
}
@Override
......@@ -58,7 +58,7 @@ public final class WebvttParser extends SubtitleParser {
parsableWebvttData.reset(bytes, length);
// Initialization for consistent starting state.
webvttCueBuilder.reset();
styleMap.clear();
definedStyles.clear();
// Validate the first line of the header, and skip the remainder.
WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
......@@ -74,9 +74,12 @@ public final class WebvttParser extends SubtitleParser {
throw new ParserException("A style block was found after the first cue.");
}
parsableWebvttData.readLine(); // Consume the "STYLE" header.
cssParser.parseBlock(parsableWebvttData, styleMap);
WebvttCssStyle styleBlock = cssParser.parseBlock(parsableWebvttData);
if (styleBlock != null) {
definedStyles.add(styleBlock);
}
} else if (eventFound == CUE_FOUND) {
if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, styleMap)) {
if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, definedStyles)) {
subtitles.add(webvttCueBuilder.build());
webvttCueBuilder.reset();
}
......
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