Commit 7d38d2ef by Oliver Woodman

CuePainter fixes for caption styling.

- Don't allow "nothing has changed" optimization in the case
  that only styling has changed (TextUtils.equals will return
  true in this case, but we shouldn't optimize).
- Add functionality to suppress embedded styling; seems useful
  to have.
- Added "this." for clarity.
parent 68f2dc59
...@@ -71,6 +71,7 @@ import android.util.Log; ...@@ -71,6 +71,7 @@ import android.util.Log;
private CharSequence cueText; private CharSequence cueText;
private int cuePosition; private int cuePosition;
private Alignment cueAlignment; private Alignment cueAlignment;
private boolean applyEmbeddedStyles;
private int foregroundColor; private int foregroundColor;
private int backgroundColor; private int backgroundColor;
private int windowColor; private int windowColor;
...@@ -121,6 +122,7 @@ import android.util.Log; ...@@ -121,6 +122,7 @@ import android.util.Log;
* which the same parameters are passed. * which the same parameters are passed.
* *
* @param cue The cue to draw. * @param cue The cue to draw.
* @param applyEmbeddedStyles Whether styling embedded within the cue should be applied.
* @param style The style to use when drawing the cue text. * @param style The style to use when drawing the cue text.
* @param fontScale The font scale. * @param fontScale The font scale.
* @param bottomPaddingFraction The bottom padding fraction to apply when {@link Cue#line} is * @param bottomPaddingFraction The bottom padding fraction to apply when {@link Cue#line} is
...@@ -131,48 +133,55 @@ import android.util.Log; ...@@ -131,48 +133,55 @@ import android.util.Log;
* @param cueBoxRight The right position of the enclosing cue box. * @param cueBoxRight The right position of the enclosing cue box.
* @param cueBoxBottom The bottom position of the enclosing cue box. * @param cueBoxBottom The bottom position of the enclosing cue box.
*/ */
public void draw(Cue cue, CaptionStyleCompat style, float fontScale, float bottomPaddingFraction, public void draw(Cue cue, boolean applyEmbeddedStyles, CaptionStyleCompat style, float fontScale,
Canvas canvas, int cueBoxLeft, int cueBoxTop, int cueBoxRight, int cueBoxBottom) { float bottomPaddingFraction, Canvas canvas, int cueBoxLeft, int cueBoxTop, int cueBoxRight,
if (TextUtils.isEmpty(cue.text)) { int cueBoxBottom) {
CharSequence cueText = cue.text;
if (TextUtils.isEmpty(cueText)) {
// Nothing to draw. // Nothing to draw.
return; return;
} }
if (!applyEmbeddedStyles) {
if (TextUtils.equals(cueText, cue.text) // Strip out any embedded styling.
&& cuePosition == cue.position cueText = cueText.toString();
&& Util.areEqual(cueAlignment, cue.alignment) }
&& foregroundColor == style.foregroundColor if (areCharSequencesEqual(this.cueText, cueText)
&& backgroundColor == style.backgroundColor && this.cuePosition == cue.position
&& windowColor == style.windowColor && Util.areEqual(this.cueAlignment, cue.alignment)
&& edgeType == style.edgeType && this.applyEmbeddedStyles == applyEmbeddedStyles
&& edgeColor == style.edgeColor && this.foregroundColor == style.foregroundColor
&& Util.areEqual(textPaint.getTypeface(), style.typeface) && this.backgroundColor == style.backgroundColor
&& this.windowColor == style.windowColor
&& this.edgeType == style.edgeType
&& this.edgeColor == style.edgeColor
&& Util.areEqual(this.textPaint.getTypeface(), style.typeface)
&& this.fontScale == fontScale && this.fontScale == fontScale
&& this.bottomPaddingFraction == bottomPaddingFraction && this.bottomPaddingFraction == bottomPaddingFraction
&& parentLeft == cueBoxLeft && this.parentLeft == cueBoxLeft
&& parentTop == cueBoxTop && this.parentTop == cueBoxTop
&& parentRight == cueBoxRight && this.parentRight == cueBoxRight
&& parentBottom == cueBoxBottom) { && this.parentBottom == cueBoxBottom) {
// We can use the cached layout. // We can use the cached layout.
drawLayout(canvas); drawLayout(canvas);
return; return;
} }
cueText = cue.text; this.cueText = cueText;
cuePosition = cue.position; this.cuePosition = cue.position;
cueAlignment = cue.alignment; this.cueAlignment = cue.alignment;
foregroundColor = style.foregroundColor; this.applyEmbeddedStyles = applyEmbeddedStyles;
backgroundColor = style.backgroundColor; this.foregroundColor = style.foregroundColor;
windowColor = style.windowColor; this.backgroundColor = style.backgroundColor;
edgeType = style.edgeType; this.windowColor = style.windowColor;
edgeColor = style.edgeColor; this.edgeType = style.edgeType;
textPaint.setTypeface(style.typeface); this.edgeColor = style.edgeColor;
this.textPaint.setTypeface(style.typeface);
this.fontScale = fontScale; this.fontScale = fontScale;
this.bottomPaddingFraction = bottomPaddingFraction; this.bottomPaddingFraction = bottomPaddingFraction;
parentLeft = cueBoxLeft; this.parentLeft = cueBoxLeft;
parentTop = cueBoxTop; this.parentTop = cueBoxTop;
parentRight = cueBoxRight; this.parentRight = cueBoxRight;
parentBottom = cueBoxBottom; this.parentBottom = cueBoxBottom;
int parentWidth = parentRight - parentLeft; int parentWidth = parentRight - parentLeft;
int parentHeight = parentBottom - parentTop; int parentHeight = parentBottom - parentTop;
...@@ -297,4 +306,15 @@ import android.util.Log; ...@@ -297,4 +306,15 @@ import android.util.Log;
canvas.restoreToCount(saveCount); canvas.restoreToCount(saveCount);
} }
/**
* This method is used instead of {@link TextUtils#equals(CharSequence, CharSequence)} because the
* latter only checks the text of each sequence, and does not check for equality of styling that
* may be embedded within the {@link CharSequence}s.
*/
private static boolean areCharSequencesEqual(CharSequence first, CharSequence second) {
// Some CharSequence implementations don't perform a cheap referential equality check in their
// equals methods, so we perform one explicitly here.
return first == second || (first != null && first.equals(second));
}
} }
...@@ -38,6 +38,7 @@ public final class SubtitleLayout extends View { ...@@ -38,6 +38,7 @@ public final class SubtitleLayout extends View {
private List<Cue> cues; private List<Cue> cues;
private float fontScale; private float fontScale;
private boolean applyEmbeddedStyles;
private CaptionStyleCompat style; private CaptionStyleCompat style;
private float bottomPaddingFraction; private float bottomPaddingFraction;
...@@ -49,6 +50,7 @@ public final class SubtitleLayout extends View { ...@@ -49,6 +50,7 @@ public final class SubtitleLayout extends View {
super(context, attrs); super(context, attrs);
painters = new ArrayList<>(); painters = new ArrayList<>();
fontScale = 1; fontScale = 1;
applyEmbeddedStyles = true;
style = CaptionStyleCompat.DEFAULT; style = CaptionStyleCompat.DEFAULT;
bottomPaddingFraction = DEFAULT_BOTTOM_PADDING_FRACTION; bottomPaddingFraction = DEFAULT_BOTTOM_PADDING_FRACTION;
} }
...@@ -87,7 +89,21 @@ public final class SubtitleLayout extends View { ...@@ -87,7 +89,21 @@ public final class SubtitleLayout extends View {
} }
/** /**
* Configures the view according to the given style. * Sets whether styling embedded within the cues should be applied. Enabled by default.
*
* @param applyEmbeddedStyles Whether styling embedded within the cues should be applied.
*/
public void setApplyEmbeddedStyles(boolean applyEmbeddedStyles) {
if (this.applyEmbeddedStyles == applyEmbeddedStyles) {
return;
}
this.applyEmbeddedStyles = applyEmbeddedStyles;
// Invalidate to trigger drawing.
invalidate();
}
/**
* Sets the caption style.
* *
* @param style A style for the view. * @param style A style for the view.
*/ */
...@@ -119,8 +135,8 @@ public final class SubtitleLayout extends View { ...@@ -119,8 +135,8 @@ public final class SubtitleLayout extends View {
public void dispatchDraw(Canvas canvas) { public void dispatchDraw(Canvas canvas) {
int cueCount = (cues == null) ? 0 : cues.size(); int cueCount = (cues == null) ? 0 : cues.size();
for (int i = 0; i < cueCount; i++) { for (int i = 0; i < cueCount; i++) {
painters.get(i).draw(cues.get(i), style, fontScale, bottomPaddingFraction, canvas, getLeft(), painters.get(i).draw(cues.get(i), applyEmbeddedStyles, style, fontScale,
getTop(), getRight(), getBottom()); bottomPaddingFraction, canvas, getLeft(), getTop(), getRight(), getBottom());
} }
} }
......
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