Commit d24e7cdf by hoangtc Committed by Oliver Woodman

Fix a bug with TTML using font size as % of cellResolution.

After [] we support default font size for TTML, relative to the cellResolution of the document. However, this introduced a bug that makes TTML font-size in such case always follow the cellResolution font size, even when SubtitleView.setApplyEmbeddedStyles(false) and SubtitleView.setApplyEmbeddedFontSizes(false) were used.

This CL updates the fix so that the default font-size using cellResolution works in the same way as other embedded styles, and can be turned off using setters from SubtitleView.

GitHub: #4491

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=204467033
parent 0a46e741
...@@ -2,13 +2,17 @@ ...@@ -2,13 +2,17 @@
### 2.8.3 ### ### 2.8.3 ###
* Captions:
* TTML: Fix an issue with TTML using font size as % of cell resolution that
makes `SubtitleView.setApplyEmbeddedFontSizes()` not work correctly.
([#4491](https://github.com/google/ExoPlayer/issues/4491)).
* CEA-608: Improve handling of embedded styles
([#4321](https://github.com/google/ExoPlayer/issues/4321)).
* DASH: Exclude text streams from duration calculations * DASH: Exclude text streams from duration calculations
([#4029](https://github.com/google/ExoPlayer/issues/4029)). ([#4029](https://github.com/google/ExoPlayer/issues/4029)).
* DRM: * DRM:
* Allow DrmInitData to carry a license server URL * Allow DrmInitData to carry a license server URL
([#3393](https://github.com/google/ExoPlayer/issues/3393)). ([#3393](https://github.com/google/ExoPlayer/issues/3393)).
* CEA-608: Improve handling of embedded styles
([#4321](https://github.com/google/ExoPlayer/issues/4321)).
* IMA: Fix behavior when creating/releasing the player then releasing * IMA: Fix behavior when creating/releasing the player then releasing
`ImaAdsLoader` ([#3879](https://github.com/google/ExoPlayer/issues/3879)). `ImaAdsLoader` ([#3879](https://github.com/google/ExoPlayer/issues/3879)).
* Fix issue playing DRM protected streams on Asus Zenfone 2 * Fix issue playing DRM protected streams on Asus Zenfone 2
......
...@@ -89,7 +89,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -89,7 +89,8 @@ import com.google.android.exoplayer2.util.Util;
private int edgeColor; private int edgeColor;
@CaptionStyleCompat.EdgeType @CaptionStyleCompat.EdgeType
private int edgeType; private int edgeType;
private float textSizePx; private float defaultTextSizePx;
private float cueTextSizePx;
private float bottomPaddingFraction; private float bottomPaddingFraction;
private int parentLeft; private int parentLeft;
private int parentTop; private int parentTop;
...@@ -130,8 +131,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -130,8 +131,8 @@ import com.google.android.exoplayer2.util.Util;
/** /**
* Draws the provided {@link Cue} into a canvas with the specified styling. * Draws the provided {@link Cue} into a canvas with the specified styling.
* <p> *
* A call to this method is able to use cached results of calculations made during the previous * <p>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.
* *
...@@ -140,7 +141,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -140,7 +141,8 @@ import com.google.android.exoplayer2.util.Util;
* @param applyEmbeddedFontSizes If {@code applyEmbeddedStyles} is true, defines whether font * @param applyEmbeddedFontSizes If {@code applyEmbeddedStyles} is true, defines whether font
* sizes embedded within the cue should be applied. Otherwise, it is ignored. * sizes embedded within the cue should be applied. Otherwise, it is ignored.
* @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 defaultTextSizePx The default text size to use when drawing the text, in pixels.
* @param cueTextSizePx The embedded text size of this cue, 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 canvas The canvas into which to draw.
...@@ -149,9 +151,19 @@ import com.google.android.exoplayer2.util.Util; ...@@ -149,9 +151,19 @@ import com.google.android.exoplayer2.util.Util;
* @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, boolean applyEmbeddedFontSizes, public void draw(
CaptionStyleCompat style, float textSizePx, float bottomPaddingFraction, Canvas canvas, Cue cue,
int cueBoxLeft, int cueBoxTop, int cueBoxRight, int cueBoxBottom) { boolean applyEmbeddedStyles,
boolean applyEmbeddedFontSizes,
CaptionStyleCompat style,
float defaultTextSizePx,
float cueTextSizePx,
float bottomPaddingFraction,
Canvas canvas,
int cueBoxLeft,
int cueBoxTop,
int cueBoxRight,
int cueBoxBottom) {
boolean isTextCue = cue.bitmap == null; boolean isTextCue = cue.bitmap == null;
int windowColor = Color.BLACK; int windowColor = Color.BLACK;
if (isTextCue) { if (isTextCue) {
...@@ -180,7 +192,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -180,7 +192,8 @@ import com.google.android.exoplayer2.util.Util;
&& this.edgeType == style.edgeType && this.edgeType == style.edgeType
&& this.edgeColor == style.edgeColor && this.edgeColor == style.edgeColor
&& Util.areEqual(this.textPaint.getTypeface(), style.typeface) && Util.areEqual(this.textPaint.getTypeface(), style.typeface)
&& this.textSizePx == textSizePx && this.defaultTextSizePx == defaultTextSizePx
&& this.cueTextSizePx == cueTextSizePx
&& this.bottomPaddingFraction == bottomPaddingFraction && this.bottomPaddingFraction == bottomPaddingFraction
&& this.parentLeft == cueBoxLeft && this.parentLeft == cueBoxLeft
&& this.parentTop == cueBoxTop && this.parentTop == cueBoxTop
...@@ -209,7 +222,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -209,7 +222,8 @@ import com.google.android.exoplayer2.util.Util;
this.edgeType = style.edgeType; this.edgeType = style.edgeType;
this.edgeColor = style.edgeColor; this.edgeColor = style.edgeColor;
this.textPaint.setTypeface(style.typeface); this.textPaint.setTypeface(style.typeface);
this.textSizePx = textSizePx; this.defaultTextSizePx = defaultTextSizePx;
this.cueTextSizePx = cueTextSizePx;
this.bottomPaddingFraction = bottomPaddingFraction; this.bottomPaddingFraction = bottomPaddingFraction;
this.parentLeft = cueBoxLeft; this.parentLeft = cueBoxLeft;
this.parentTop = cueBoxTop; this.parentTop = cueBoxTop;
...@@ -228,8 +242,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -228,8 +242,8 @@ import com.google.android.exoplayer2.util.Util;
int parentWidth = parentRight - parentLeft; int parentWidth = parentRight - parentLeft;
int parentHeight = parentBottom - parentTop; int parentHeight = parentBottom - parentTop;
textPaint.setTextSize(textSizePx); textPaint.setTextSize(defaultTextSizePx);
int textPaddingX = (int) (textSizePx * INNER_PADDING_RATIO + 0.5f); int textPaddingX = (int) (defaultTextSizePx * INNER_PADDING_RATIO + 0.5f);
int availableWidth = parentWidth - textPaddingX * 2; int availableWidth = parentWidth - textPaddingX * 2;
if (cueSize != Cue.DIMEN_UNSET) { if (cueSize != Cue.DIMEN_UNSET) {
...@@ -240,14 +254,12 @@ import com.google.android.exoplayer2.util.Util; ...@@ -240,14 +254,12 @@ import com.google.android.exoplayer2.util.Util;
return; return;
} }
CharSequence cueText = this.cueText;
// Remove embedded styling or font size if requested. // Remove embedded styling or font size if requested.
CharSequence cueText; if (!applyEmbeddedStyles) {
if (applyEmbeddedFontSizes && applyEmbeddedStyles) { cueText = cueText.toString(); // Equivalent to erasing all spans.
cueText = this.cueText; } else if (!applyEmbeddedFontSizes) {
} else if (!applyEmbeddedStyles) { SpannableStringBuilder newCueText = new SpannableStringBuilder(cueText);
cueText = this.cueText.toString(); // Equivalent to erasing all spans.
} else {
SpannableStringBuilder newCueText = new SpannableStringBuilder(this.cueText);
int cueLength = newCueText.length(); int cueLength = newCueText.length();
AbsoluteSizeSpan[] absSpans = newCueText.getSpans(0, cueLength, AbsoluteSizeSpan.class); AbsoluteSizeSpan[] absSpans = newCueText.getSpans(0, cueLength, AbsoluteSizeSpan.class);
RelativeSizeSpan[] relSpans = newCueText.getSpans(0, cueLength, RelativeSizeSpan.class); RelativeSizeSpan[] relSpans = newCueText.getSpans(0, cueLength, RelativeSizeSpan.class);
...@@ -258,6 +270,19 @@ import com.google.android.exoplayer2.util.Util; ...@@ -258,6 +270,19 @@ import com.google.android.exoplayer2.util.Util;
newCueText.removeSpan(relSpan); newCueText.removeSpan(relSpan);
} }
cueText = newCueText; cueText = newCueText;
} else {
// Apply embedded styles & font size.
if (cueTextSizePx > 0) {
// Use a SpannableStringBuilder encompassing the whole cue text to apply the default
// cueTextSizePx.
SpannableStringBuilder newCueText = new SpannableStringBuilder(cueText);
newCueText.setSpan(
new AbsoluteSizeSpan((int) cueTextSizePx),
/* start= */ 0,
/* end= */ newCueText.length(),
Spanned.SPAN_PRIORITY);
cueText = newCueText;
}
} }
Alignment textAlignment = cueTextAlignment == null ? Alignment.ALIGN_CENTER : cueTextAlignment; Alignment textAlignment = cueTextAlignment == null ? Alignment.ALIGN_CENTER : cueTextAlignment;
......
...@@ -269,15 +269,15 @@ public final class SubtitleView extends View implements TextOutput { ...@@ -269,15 +269,15 @@ public final class SubtitleView extends View implements TextOutput {
for (int i = 0; i < cueCount; i++) { for (int i = 0; i < cueCount; i++) {
Cue cue = cues.get(i); Cue cue = cues.get(i);
float textSizePx = float cueTextSizePx = resolveCueTextSize(cue, rawViewHeight, viewHeightMinusPadding);
resolveTextSizeForCue(cue, rawViewHeight, viewHeightMinusPadding, defaultViewTextSizePx);
SubtitlePainter painter = painters.get(i); SubtitlePainter painter = painters.get(i);
painter.draw( painter.draw(
cue, cue,
applyEmbeddedStyles, applyEmbeddedStyles,
applyEmbeddedFontSizes, applyEmbeddedFontSizes,
style, style,
textSizePx, defaultViewTextSizePx,
cueTextSizePx,
bottomPaddingFraction, bottomPaddingFraction,
canvas, canvas,
left, left,
...@@ -287,14 +287,13 @@ public final class SubtitleView extends View implements TextOutput { ...@@ -287,14 +287,13 @@ public final class SubtitleView extends View implements TextOutput {
} }
} }
private float resolveTextSizeForCue( private float resolveCueTextSize(Cue cue, int rawViewHeight, int viewHeightMinusPadding) {
Cue cue, int rawViewHeight, int viewHeightMinusPadding, float defaultViewTextSizePx) {
if (cue.textSizeType == Cue.TYPE_UNSET || cue.textSize == Cue.DIMEN_UNSET) { if (cue.textSizeType == Cue.TYPE_UNSET || cue.textSize == Cue.DIMEN_UNSET) {
return defaultViewTextSizePx; return 0;
} }
float defaultCueTextSizePx = float defaultCueTextSizePx =
resolveTextSize(cue.textSizeType, cue.textSize, rawViewHeight, viewHeightMinusPadding); resolveTextSize(cue.textSizeType, cue.textSize, rawViewHeight, viewHeightMinusPadding);
return defaultCueTextSizePx > 0 ? defaultCueTextSizePx : defaultViewTextSizePx; return Math.max(defaultCueTextSizePx, 0);
} }
private float resolveTextSize( private float resolveTextSize(
......
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