From 24b16f34195070794bcd98db27d84e8dc716a3d2 Mon Sep 17 00:00:00 2001 From: hoangtc Date: Wed, 16 May 2018 07:09:55 -0700 Subject: [PATCH] Fix a bug with TTML font styling that displays empty lines. If the caption line has no text (empty line or only line break), we should not display its background. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=196823319 --- RELEASENOTES.md | 7 +++++-- .../android/exoplayer2/ui/SubtitlePainter.java | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 1c60071326..0debc6d2d3 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -20,8 +20,11 @@ * Fix playback of livestreams with EXT-X-PROGRAM-DATE-TIME tags ([#4239](https://github.com/google/ExoPlayer/issues/4239)). * Caption: - * Fix a TTML styling issue when there are multiple regions displayed at the - same time that can make text size of each region much smaller than defined. + * TTML: + * Fix a styling issue when there are multiple regions displayed at the same + time that can make text size of each region much smaller than defined. + * Fix an issue when the caption line has no text (empty line or only line + break), and the line's background is still displayed. ### 2.8.0 ### diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java index b6cfc9a6f3..c5d264b310 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitlePainter.java @@ -372,12 +372,22 @@ import com.google.android.exoplayer2.util.Util; float previousBottom = layout.getLineTop(0); int lineCount = layout.getLineCount(); for (int i = 0; i < lineCount; i++) { - lineBounds.left = layout.getLineLeft(i) - textPaddingX; - lineBounds.right = layout.getLineRight(i) + textPaddingX; + float lineTextBoundLeft = layout.getLineLeft(i); + float lineTextBoundRight = layout.getLineRight(i); + lineBounds.left = lineTextBoundLeft - textPaddingX; + lineBounds.right = lineTextBoundRight + textPaddingX; lineBounds.top = previousBottom; lineBounds.bottom = layout.getLineBottom(i); previousBottom = lineBounds.bottom; - canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint); + float lineTextWidth = lineTextBoundRight - lineTextBoundLeft; + if (lineTextWidth > 0) { + // Do not draw a line's background color if it has no text. + // For some reason, calculating the width manually is more reliable than + // layout.getLineWidth(). + // Sometimes, lineTextBoundRight == lineTextBoundLeft, and layout.getLineWidth() still + // returns non-zero value. + canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint); + } } }