Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Webvtt subtitles class colors #4178

Merged
merged 4 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand Down Expand Up @@ -514,6 +515,8 @@ private static void applySpansForTag(
text.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case TAG_CLASS:
applySupportedClasses(text, startTag.classes, start, end);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests to WebvttCueParserTest for this?

break;
case TAG_LANG:
case TAG_VOICE:
case "": // Case of the "whole cue" virtual tag.
Expand All @@ -529,6 +532,16 @@ private static void applySpansForTag(
}
}

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat code re-use, but because the WebVTT list is so much shorter than the list of CSS colors I think it would be clearer to explicitly list the WebVTT default colors. Otherwise if someone defines a custom class called papayawhip in their style section that maybe has nothing to do with that color, we'll color the cue anyway (which is not in-line with the WebVTT spec).

So I think a private static final Map<String, Integer> DEFAULT_COLORS inside WebvttCueParser would be the best approach here. You'll probably have to populate it in a static initializer block the same as in ColorParser.

That will also make adding background color support less 'different' - we can just add a DEFAULT_BACKGROUND_COLORS map alongside. If you have the time/inclination, feel free to also add that in this same PR! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll take a look and update this soon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the strategy by adding the two maps (adds support for background too) in WebvttCueParser and removing the dependency from the color parser.
Added relevant tests too

text.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}

private static void applyStyleToText(SpannableStringBuilder spannedText, WebvttCssStyle style,
int start, int end) {
if (style == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public final class ColorParser {

private static final Map<String, Integer> COLOR_MAP;

public static boolean isNamedColor(String expression) {
return COLOR_MAP.containsKey(expression);
}

/**
* Parses a TTML color expression.
*
Expand Down