Commit 8c64d188 by ibaker Committed by Oliver Woodman

Add support for Cue and default text sizes in SubtitleWebView

PiperOrigin-RevId: 309243467
parent 7ac024af
...@@ -158,7 +158,8 @@ import java.util.List; ...@@ -158,7 +158,8 @@ import java.util.List;
cue = repositionVerticalCue(cue); cue = repositionVerticalCue(cue);
} }
float cueTextSizePx = float cueTextSizePx =
SubtitleViewUtils.resolveCueTextSize(cue, rawViewHeight, viewHeightMinusPadding); SubtitleViewUtils.resolveTextSize(
cue.textSizeType, cue.textSize, rawViewHeight, viewHeightMinusPadding);
SubtitlePainter painter = painters.get(i); SubtitlePainter painter = painters.get(i);
painter.draw( painter.draw(
cue, cue,
......
...@@ -21,20 +21,20 @@ import com.google.android.exoplayer2.text.Cue; ...@@ -21,20 +21,20 @@ import com.google.android.exoplayer2.text.Cue;
/** Utility class for subtitle layout logic. */ /** Utility class for subtitle layout logic. */
/* package */ final class SubtitleViewUtils { /* package */ final class SubtitleViewUtils {
public static float resolveCueTextSize(Cue cue, int rawViewHeight, int viewHeightMinusPadding) { /**
if (cue.textSizeType == Cue.TYPE_UNSET || cue.textSize == Cue.DIMEN_UNSET) { * Returns the text size in px, derived from {@code textSize} and {@code textSizeType}.
return 0; *
} * <p>Returns {@link Cue#DIMEN_UNSET} if {@code textSize == Cue.DIMEN_UNSET} or {@code
float defaultCueTextSizePx = * textSizeType == Cue.TYPE_UNSET}.
resolveTextSize(cue.textSizeType, cue.textSize, rawViewHeight, viewHeightMinusPadding); */
return Math.max(defaultCueTextSizePx, 0);
}
public static float resolveTextSize( public static float resolveTextSize(
@Cue.TextSizeType int textSizeType, @Cue.TextSizeType int textSizeType,
float textSize, float textSize,
int rawViewHeight, int rawViewHeight,
int viewHeightMinusPadding) { int viewHeightMinusPadding) {
if (textSize == Cue.DIMEN_UNSET) {
return Cue.DIMEN_UNSET;
}
switch (textSizeType) { switch (textSizeType) {
case Cue.TEXT_SIZE_TYPE_ABSOLUTE: case Cue.TEXT_SIZE_TYPE_ABSOLUTE:
return textSize; return textSize;
......
...@@ -24,6 +24,7 @@ import android.graphics.Color; ...@@ -24,6 +24,7 @@ import android.graphics.Color;
import android.text.Layout; import android.text.Layout;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Base64; import android.util.Base64;
import android.util.DisplayMetrics;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.FrameLayout; import android.widget.FrameLayout;
...@@ -50,8 +51,8 @@ import java.util.List; ...@@ -50,8 +51,8 @@ import java.util.List;
private final WebView webView; private final WebView webView;
private List<Cue> cues; private List<Cue> cues;
@Cue.TextSizeType private int textSizeType; @Cue.TextSizeType private int defaultTextSizeType;
private float textSize; private float defaultTextSize;
private boolean applyEmbeddedStyles; private boolean applyEmbeddedStyles;
private boolean applyEmbeddedFontSizes; private boolean applyEmbeddedFontSizes;
private CaptionStyleCompat style; private CaptionStyleCompat style;
...@@ -64,8 +65,8 @@ import java.util.List; ...@@ -64,8 +65,8 @@ import java.util.List;
public SubtitleWebView(Context context, @Nullable AttributeSet attrs) { public SubtitleWebView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
cues = Collections.emptyList(); cues = Collections.emptyList();
textSizeType = Cue.TEXT_SIZE_TYPE_FRACTIONAL; defaultTextSizeType = Cue.TEXT_SIZE_TYPE_FRACTIONAL;
textSize = DEFAULT_TEXT_SIZE_FRACTION; defaultTextSize = DEFAULT_TEXT_SIZE_FRACTION;
applyEmbeddedStyles = true; applyEmbeddedStyles = true;
applyEmbeddedFontSizes = true; applyEmbeddedFontSizes = true;
style = CaptionStyleCompat.DEFAULT; style = CaptionStyleCompat.DEFAULT;
...@@ -99,11 +100,11 @@ import java.util.List; ...@@ -99,11 +100,11 @@ import java.util.List;
@Override @Override
public void setTextSize(@Cue.TextSizeType int textSizeType, float textSize) { public void setTextSize(@Cue.TextSizeType int textSizeType, float textSize) {
if (this.textSizeType == textSizeType && this.textSize == textSize) { if (this.defaultTextSizeType == textSizeType && this.defaultTextSize == textSize) {
return; return;
} }
this.textSizeType = textSizeType; this.defaultTextSizeType = textSizeType;
this.textSize = textSize; this.defaultTextSize = textSize;
updateWebView(); updateWebView();
} }
...@@ -147,17 +148,19 @@ import java.util.List; ...@@ -147,17 +148,19 @@ import java.util.List;
private void updateWebView() { private void updateWebView() {
StringBuilder html = new StringBuilder(); StringBuilder html = new StringBuilder();
html.append("<html><body>") html.append(
.append("<div style=\"") Util.formatInvariant(
.append("-webkit-user-select:none;") "<html><body><div style=\""
.append("position:fixed;") + "-webkit-user-select:none;"
.append("top:0;") + "position:fixed;"
.append("bottom:0;") + "top:0;"
.append("left:0;") + "bottom:0;"
.append("right:0;") + "left:0;"
.append("font-size:20px;") + "right:0;"
.append("color:red;") + "font-size:%s;"
.append("\">"); + "color:red;"
+ "\">",
convertTextSizeToCss(defaultTextSizeType, defaultTextSize)));
for (int i = 0; i < cues.size(); i++) { for (int i = 0; i < cues.size(); i++) {
Cue cue = cues.get(i); Cue cue = cues.get(i);
...@@ -198,8 +201,8 @@ import java.util.List; ...@@ -198,8 +201,8 @@ import java.util.List;
: "fit-content"; : "fit-content";
String textAlign = convertAlignmentToCss(cue.textAlignment); String textAlign = convertAlignmentToCss(cue.textAlignment);
String writingMode = convertVerticalTypeToCss(cue.verticalType); String writingMode = convertVerticalTypeToCss(cue.verticalType);
String cueTextSizeCssPx = convertTextSizeToCss(cue.textSizeType, cue.textSize);
String positionProperty; String positionProperty;
String lineProperty; String lineProperty;
...@@ -240,6 +243,7 @@ import java.util.List; ...@@ -240,6 +243,7 @@ import java.util.List;
+ "%s:%s;" + "%s:%s;"
+ "text-align:%s;" + "text-align:%s;"
+ "writing-mode:%s;" + "writing-mode:%s;"
+ "font-size:%s;"
+ "transform:translate(%s%%,%s%%);" + "transform:translate(%s%%,%s%%);"
+ "\">", + "\">",
positionProperty, positionProperty,
...@@ -250,6 +254,7 @@ import java.util.List; ...@@ -250,6 +254,7 @@ import java.util.List;
size, size,
textAlign, textAlign,
writingMode, writingMode,
cueTextSizeCssPx,
horizontalTranslatePercent, horizontalTranslatePercent,
verticalTranslatePercent)) verticalTranslatePercent))
.append(SpannedToHtmlConverter.convert(cue.text)) .append(SpannedToHtmlConverter.convert(cue.text))
...@@ -265,7 +270,27 @@ import java.util.List; ...@@ -265,7 +270,27 @@ import java.util.List;
"base64"); "base64");
} }
private String convertVerticalTypeToCss(@Cue.VerticalType int verticalType) { /**
* Converts a text size to a CSS px value.
*
* <p>First converts to Android px using {@link SubtitleViewUtils#resolveTextSize(int, float, int,
* int)}.
*
* <p>Then divides by {@link DisplayMetrics#density} to convert from Android px to dp because
* WebView treats one CSS px as one Android dp.
*/
private String convertTextSizeToCss(@Cue.TextSizeType int type, float size) {
float sizePx =
SubtitleViewUtils.resolveTextSize(
type, size, getHeight(), getHeight() - getPaddingTop() - getPaddingBottom());
if (sizePx == Cue.DIMEN_UNSET) {
return "unset";
}
float sizeDp = sizePx / getContext().getResources().getDisplayMetrics().density;
return Util.formatInvariant("%.2fpx", sizeDp);
}
private static String convertVerticalTypeToCss(@Cue.VerticalType int verticalType) {
switch (verticalType) { switch (verticalType) {
case Cue.VERTICAL_TYPE_LR: case Cue.VERTICAL_TYPE_LR:
return "vertical-lr"; return "vertical-lr";
...@@ -277,7 +302,7 @@ import java.util.List; ...@@ -277,7 +302,7 @@ import java.util.List;
} }
} }
private String convertAlignmentToCss(@Nullable Layout.Alignment alignment) { private static String convertAlignmentToCss(@Nullable Layout.Alignment alignment) {
if (alignment == null) { if (alignment == null) {
return "unset"; return "unset";
} }
......
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