Commit 4d18e623 by Rik Heijdens

Split Cue Rendering up into 3 phases

Split Cue Rendering up into a layout, background drawing and foreground
drawing phase so that issues with overlapping 608 captions are being prevented.
parent ad9f76eb
...@@ -89,16 +89,12 @@ import java.util.List; ...@@ -89,16 +89,12 @@ import java.util.List;
this.indent = indent; this.indent = indent;
} }
public void tab(int tabs) {
tabOffset += tabs;
}
/** /**
* Indents the cue position with amountOfTabs. * Indents the Cue.
* @param tabOffset the amount of tabs the cue position should be indented. * @param tabs The amount of tabs to indent the cue with.
*/ */
public void setTabOffset(int tabOffset) { public void tab(int tabs) {
this.tabOffset = tabOffset; tabOffset += tabs;
} }
/** /**
......
...@@ -117,30 +117,30 @@ import android.util.Log; ...@@ -117,30 +117,30 @@ import android.util.Log;
} }
/** /**
* Draws the provided {@link Cue} into a canvas with the specified styling. * Creates layouts so that the provided {@link Cue} can be drawn in a {@link Canvas} by calls to
* {@link #drawBackground(Canvas)} and {@link #drawForeground(Canvas)} with the specified styling.
* <p> * <p>
* A call to this method is able to use cached results of calculations made during the previous * A call to this method is able to use cached results of calculations made during the previous
* call, and so an instance of this class is able to optimize repeated calls to this method in * call, and so an instance of this class is able to optimize repeated calls to this method in
* which the same parameters are passed. * which the same parameters are passed.
* *
* @param cue The cue to draw. * @param cue The cue to layout.
* @param applyEmbeddedStyles Whether styling embedded within the cue should be applied. * @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 textSizePx The text size to use when drawing the cue text, in pixels. * @param textSizePx The text size to use when drawing the cue text, in pixels.
* @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
* {@link Cue#DIMEN_UNSET}, as a fraction of the viewport height * {@link Cue#DIMEN_UNSET}, as a fraction of the viewport height
* @param canvas The canvas into which to draw.
* @param cueBoxLeft The left position of the enclosing cue box. * @param cueBoxLeft The left position of the enclosing cue box.
* @param cueBoxTop The top position of the enclosing cue box. * @param cueBoxTop The top position of the enclosing cue box.
* @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, boolean applyEmbeddedStyles, CaptionStyleCompat style, float textSizePx, public void layout(Cue cue, boolean applyEmbeddedStyles, CaptionStyleCompat style, float textSizePx,
float bottomPaddingFraction, Canvas canvas, int cueBoxLeft, int cueBoxTop, int cueBoxRight, float bottomPaddingFraction, int cueBoxLeft, int cueBoxTop, int cueBoxRight,
int cueBoxBottom) { int cueBoxBottom) {
CharSequence cueText = cue.text; CharSequence cueText = cue.text;
if (TextUtils.isEmpty(cueText)) { if (TextUtils.isEmpty(cueText)) {
// Nothing to draw. // Nothing to layout.
return; return;
} }
if (!applyEmbeddedStyles) { if (!applyEmbeddedStyles) {
...@@ -169,7 +169,6 @@ import android.util.Log; ...@@ -169,7 +169,6 @@ import android.util.Log;
&& this.parentRight == cueBoxRight && this.parentRight == cueBoxRight
&& this.parentBottom == cueBoxBottom) { && this.parentBottom == cueBoxBottom) {
// We can use the cached layout. // We can use the cached layout.
drawLayout(canvas);
return; return;
} }
...@@ -272,19 +271,17 @@ import android.util.Log; ...@@ -272,19 +271,17 @@ import android.util.Log;
this.textLeft = textLeft; this.textLeft = textLeft;
this.textTop = textTop; this.textTop = textTop;
this.textPaddingX = textPaddingX; this.textPaddingX = textPaddingX;
drawLayout(canvas);
} }
/** /**
* Draws {@link #textLayout} into the provided canvas. * Draws the background of the {@link #textLayout} into the provided canvas.
* *
* @param canvas The canvas into which to draw. * @param canvas The canvas into which to layout.
*/ */
private void drawLayout(Canvas canvas) { public void drawBackground(Canvas canvas) {
final StaticLayout layout = textLayout; final StaticLayout layout = textLayout;
if (layout == null) { if (layout == null) {
// Nothing to draw. // Nothing to layout.
return; return;
} }
...@@ -311,6 +308,24 @@ import android.util.Log; ...@@ -311,6 +308,24 @@ import android.util.Log;
} }
} }
canvas.restoreToCount(saveCount);
}
/**
* Draws the foreground of the {@link #textLayout} into the provided canvas.
*
* @param canvas The canvas into which to layout.
*/
public void drawForeground(Canvas canvas) {
final StaticLayout layout = textLayout;
if (layout == null) {
// Nothing to layout.
return;
}
int saveCount = canvas.save();
canvas.translate(textLeft, textTop);
if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) { if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
textPaint.setStrokeJoin(Join.ROUND); textPaint.setStrokeJoin(Join.ROUND);
textPaint.setStrokeWidth(outlineWidth); textPaint.setStrokeWidth(outlineWidth);
......
...@@ -15,6 +15,11 @@ ...@@ -15,6 +15,11 @@
*/ */
package com.google.android.exoplayer2.ui; package com.google.android.exoplayer2.ui;
import com.google.android.exoplayer2.text.CaptionStyleCompat;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.TextRenderer;
import com.google.android.exoplayer2.util.Util;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
...@@ -23,10 +28,7 @@ import android.util.AttributeSet; ...@@ -23,10 +28,7 @@ import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.accessibility.CaptioningManager; import android.view.accessibility.CaptioningManager;
import com.google.android.exoplayer2.text.CaptionStyleCompat;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.TextRenderer;
import com.google.android.exoplayer2.util.Util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -230,7 +232,7 @@ public final class SubtitleView extends View implements TextRenderer.Output { ...@@ -230,7 +232,7 @@ public final class SubtitleView extends View implements TextRenderer.Output {
int right = getRight() + getPaddingRight(); int right = getRight() + getPaddingRight();
int bottom = rawBottom - getPaddingBottom(); int bottom = rawBottom - getPaddingBottom();
if (bottom <= top || right <= left) { if (bottom <= top || right <= left) {
// No space to draw subtitles. // No space to layout subtitles.
return; return;
} }
...@@ -242,8 +244,12 @@ public final class SubtitleView extends View implements TextRenderer.Output { ...@@ -242,8 +244,12 @@ public final class SubtitleView extends View implements TextRenderer.Output {
} }
for (int i = 0; i < cueCount; i++) { for (int i = 0; i < cueCount; i++) {
painters.get(i).draw(cues.get(i), applyEmbeddedStyles, style, textSizePx, painters.get(i).layout(cues.get(i), applyEmbeddedStyles, style, textSizePx,
bottomPaddingFraction, canvas, left, top, right, bottom); bottomPaddingFraction, left, top, right, bottom);
painters.get(i).drawBackground(canvas);
}
for (int i = 0; i < cueCount; i++) {
painters.get(i).drawForeground(canvas);
} }
} }
......
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