Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8c64d188
authored
Apr 30, 2020
by
ibaker
Committed by
Oliver Woodman
May 01, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add support for Cue and default text sizes in SubtitleWebView
PiperOrigin-RevId: 309243467
parent
7ac024af
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
31 deletions
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleTextView.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleViewUtils.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleTextView.java
View file @
8c64d188
...
...
@@ -158,7 +158,8 @@ import java.util.List;
cue
=
repositionVerticalCue
(
cue
);
}
float
cueTextSizePx
=
SubtitleViewUtils
.
resolveCueTextSize
(
cue
,
rawViewHeight
,
viewHeightMinusPadding
);
SubtitleViewUtils
.
resolveTextSize
(
cue
.
textSizeType
,
cue
.
textSize
,
rawViewHeight
,
viewHeightMinusPadding
);
SubtitlePainter
painter
=
painters
.
get
(
i
);
painter
.
draw
(
cue
,
...
...
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleViewUtils.java
View file @
8c64d188
...
...
@@ -21,20 +21,20 @@ import com.google.android.exoplayer2.text.Cue;
/** Utility class for subtitle layout logic. */
/* 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
)
{
return
0
;
}
float
defaultCueTextSizePx
=
resolveTextSize
(
cue
.
textSizeType
,
cue
.
textSize
,
rawViewHeight
,
viewHeightMinusPadding
);
return
Math
.
max
(
defaultCueTextSizePx
,
0
);
}
/**
* Returns the text size in px, derived from {@code textSize} and {@code textSizeType}.
*
* <p>Returns {@link Cue#DIMEN_UNSET} if {@code textSize == Cue.DIMEN_UNSET} or {@code
* textSizeType == Cue.TYPE_UNSET}.
*/
public
static
float
resolveTextSize
(
@Cue
.
TextSizeType
int
textSizeType
,
float
textSize
,
int
rawViewHeight
,
int
viewHeightMinusPadding
)
{
if
(
textSize
==
Cue
.
DIMEN_UNSET
)
{
return
Cue
.
DIMEN_UNSET
;
}
switch
(
textSizeType
)
{
case
Cue
.
TEXT_SIZE_TYPE_ABSOLUTE
:
return
textSize
;
...
...
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java
View file @
8c64d188
...
...
@@ -24,6 +24,7 @@ import android.graphics.Color;
import
android.text.Layout
;
import
android.util.AttributeSet
;
import
android.util.Base64
;
import
android.util.DisplayMetrics
;
import
android.view.MotionEvent
;
import
android.webkit.WebView
;
import
android.widget.FrameLayout
;
...
...
@@ -50,8 +51,8 @@ import java.util.List;
private
final
WebView
webView
;
private
List
<
Cue
>
cues
;
@Cue
.
TextSizeType
private
int
t
extSizeType
;
private
float
t
extSize
;
@Cue
.
TextSizeType
private
int
defaultT
extSizeType
;
private
float
defaultT
extSize
;
private
boolean
applyEmbeddedStyles
;
private
boolean
applyEmbeddedFontSizes
;
private
CaptionStyleCompat
style
;
...
...
@@ -64,8 +65,8 @@ import java.util.List;
public
SubtitleWebView
(
Context
context
,
@Nullable
AttributeSet
attrs
)
{
super
(
context
,
attrs
);
cues
=
Collections
.
emptyList
();
t
extSizeType
=
Cue
.
TEXT_SIZE_TYPE_FRACTIONAL
;
t
extSize
=
DEFAULT_TEXT_SIZE_FRACTION
;
defaultT
extSizeType
=
Cue
.
TEXT_SIZE_TYPE_FRACTIONAL
;
defaultT
extSize
=
DEFAULT_TEXT_SIZE_FRACTION
;
applyEmbeddedStyles
=
true
;
applyEmbeddedFontSizes
=
true
;
style
=
CaptionStyleCompat
.
DEFAULT
;
...
...
@@ -99,11 +100,11 @@ import java.util.List;
@Override
public
void
setTextSize
(
@Cue
.
TextSizeType
int
textSizeType
,
float
textSize
)
{
if
(
this
.
textSizeType
==
textSizeType
&&
this
.
t
extSize
==
textSize
)
{
if
(
this
.
defaultTextSizeType
==
textSizeType
&&
this
.
defaultT
extSize
==
textSize
)
{
return
;
}
this
.
t
extSizeType
=
textSizeType
;
this
.
t
extSize
=
textSize
;
this
.
defaultT
extSizeType
=
textSizeType
;
this
.
defaultT
extSize
=
textSize
;
updateWebView
();
}
...
...
@@ -147,17 +148,19 @@ import java.util.List;
private
void
updateWebView
()
{
StringBuilder
html
=
new
StringBuilder
();
html
.
append
(
"<html><body>"
)
.
append
(
"<div style=\""
)
.
append
(
"-webkit-user-select:none;"
)
.
append
(
"position:fixed;"
)
.
append
(
"top:0;"
)
.
append
(
"bottom:0;"
)
.
append
(
"left:0;"
)
.
append
(
"right:0;"
)
.
append
(
"font-size:20px;"
)
.
append
(
"color:red;"
)
.
append
(
"\">"
);
html
.
append
(
Util
.
formatInvariant
(
"<html><body><div style=\""
+
"-webkit-user-select:none;"
+
"position:fixed;"
+
"top:0;"
+
"bottom:0;"
+
"left:0;"
+
"right:0;"
+
"font-size:%s;"
+
"color:red;"
+
"\">"
,
convertTextSizeToCss
(
defaultTextSizeType
,
defaultTextSize
)));
for
(
int
i
=
0
;
i
<
cues
.
size
();
i
++)
{
Cue
cue
=
cues
.
get
(
i
);
...
...
@@ -198,8 +201,8 @@ import java.util.List;
:
"fit-content"
;
String
textAlign
=
convertAlignmentToCss
(
cue
.
textAlignment
);
String
writingMode
=
convertVerticalTypeToCss
(
cue
.
verticalType
);
String
cueTextSizeCssPx
=
convertTextSizeToCss
(
cue
.
textSizeType
,
cue
.
textSize
);
String
positionProperty
;
String
lineProperty
;
...
...
@@ -240,6 +243,7 @@ import java.util.List;
+
"%s:%s;"
+
"text-align:%s;"
+
"writing-mode:%s;"
+
"font-size:%s;"
+
"transform:translate(%s%%,%s%%);"
+
"\">"
,
positionProperty
,
...
...
@@ -250,6 +254,7 @@ import java.util.List;
size
,
textAlign
,
writingMode
,
cueTextSizeCssPx
,
horizontalTranslatePercent
,
verticalTranslatePercent
))
.
append
(
SpannedToHtmlConverter
.
convert
(
cue
.
text
))
...
...
@@ -265,7 +270,27 @@ import java.util.List;
"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
)
{
case
Cue
.
VERTICAL_TYPE_LR
:
return
"vertical-lr"
;
...
...
@@ -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
)
{
return
"unset"
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment