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
caeeae2c
authored
Apr 30, 2020
by
ibaker
Committed by
Oliver Woodman
May 01, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Use system foreground & background color in SubtitleWebView
PiperOrigin-RevId: 309244671
parent
4371617e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
11 deletions
library/ui/src/main/java/com/google/android/exoplayer2/ui/HtmlUtils.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java
library/ui/src/test/java/com/google/android/exoplayer2/ui/HtmlUtilsTest.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/HtmlUtils.java
0 → 100644
View file @
caeeae2c
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer2
.
ui
;
import
android.graphics.Color
;
import
androidx.annotation.ColorInt
;
import
com.google.android.exoplayer2.util.Util
;
/**
* Utility methods for generating HTML and CSS for use with {@link SubtitleWebView} and {@link
* SpannedToHtmlConverter}.
*/
/* package */
final
class
HtmlUtils
{
private
HtmlUtils
()
{}
public
static
String
toCssRgba
(
@ColorInt
int
color
)
{
return
Util
.
formatInvariant
(
"rgba(%d,%d,%d,%.3f)"
,
Color
.
red
(
color
),
Color
.
green
(
color
),
Color
.
blue
(
color
),
Color
.
alpha
(
color
)
/
255.0
);
}
}
library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java
View file @
caeeae2c
...
@@ -16,7 +16,6 @@
...
@@ -16,7 +16,6 @@
*/
*/
package
com
.
google
.
android
.
exoplayer2
.
ui
;
package
com
.
google
.
android
.
exoplayer2
.
ui
;
import
android.graphics.Color
;
import
android.graphics.Typeface
;
import
android.graphics.Typeface
;
import
android.text.Html
;
import
android.text.Html
;
import
android.text.Spanned
;
import
android.text.Spanned
;
...
@@ -25,7 +24,6 @@ import android.text.style.ForegroundColorSpan;
...
@@ -25,7 +24,6 @@ import android.text.style.ForegroundColorSpan;
import
android.text.style.StyleSpan
;
import
android.text.style.StyleSpan
;
import
android.text.style.UnderlineSpan
;
import
android.text.style.UnderlineSpan
;
import
android.util.SparseArray
;
import
android.util.SparseArray
;
import
androidx.annotation.ColorInt
;
import
androidx.annotation.Nullable
;
import
androidx.annotation.Nullable
;
import
com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan
;
import
com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan
;
import
com.google.android.exoplayer2.text.span.RubySpan
;
import
com.google.android.exoplayer2.text.span.RubySpan
;
...
@@ -125,11 +123,12 @@ import java.util.regex.Pattern;
...
@@ -125,11 +123,12 @@ import java.util.regex.Pattern;
if
(
span
instanceof
ForegroundColorSpan
)
{
if
(
span
instanceof
ForegroundColorSpan
)
{
ForegroundColorSpan
colorSpan
=
(
ForegroundColorSpan
)
span
;
ForegroundColorSpan
colorSpan
=
(
ForegroundColorSpan
)
span
;
return
Util
.
formatInvariant
(
return
Util
.
formatInvariant
(
"<span style='color:%s;'>"
,
toCssColor
(
colorSpan
.
getForegroundColor
()));
"<span style='color:%s;'>"
,
HtmlUtils
.
toCssRgba
(
colorSpan
.
getForegroundColor
()));
}
else
if
(
span
instanceof
BackgroundColorSpan
)
{
}
else
if
(
span
instanceof
BackgroundColorSpan
)
{
BackgroundColorSpan
colorSpan
=
(
BackgroundColorSpan
)
span
;
BackgroundColorSpan
colorSpan
=
(
BackgroundColorSpan
)
span
;
return
Util
.
formatInvariant
(
return
Util
.
formatInvariant
(
"<span style='background-color:%s;'>"
,
toCssColor
(
colorSpan
.
getBackgroundColor
()));
"<span style='background-color:%s;'>"
,
HtmlUtils
.
toCssRgba
(
colorSpan
.
getBackgroundColor
()));
}
else
if
(
span
instanceof
HorizontalTextInVerticalContextSpan
)
{
}
else
if
(
span
instanceof
HorizontalTextInVerticalContextSpan
)
{
return
"<span style='text-combine-upright:all;'>"
;
return
"<span style='text-combine-upright:all;'>"
;
}
else
if
(
span
instanceof
StyleSpan
)
{
}
else
if
(
span
instanceof
StyleSpan
)
{
...
@@ -186,12 +185,6 @@ import java.util.regex.Pattern;
...
@@ -186,12 +185,6 @@ import java.util.regex.Pattern;
return
null
;
return
null
;
}
}
private
static
String
toCssColor
(
@ColorInt
int
color
)
{
return
Util
.
formatInvariant
(
"rgba(%d,%d,%d,%.3f)"
,
Color
.
red
(
color
),
Color
.
green
(
color
),
Color
.
blue
(
color
),
Color
.
alpha
(
color
)
/
255.0
);
}
private
static
Transition
getOrCreate
(
SparseArray
<
Transition
>
transitions
,
int
key
)
{
private
static
Transition
getOrCreate
(
SparseArray
<
Transition
>
transitions
,
int
key
)
{
@Nullable
Transition
transition
=
transitions
.
get
(
key
);
@Nullable
Transition
transition
=
transitions
.
get
(
key
);
if
(
transition
==
null
)
{
if
(
transition
==
null
)
{
...
...
library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java
View file @
caeeae2c
...
@@ -157,11 +157,14 @@ import java.util.List;
...
@@ -157,11 +157,14 @@ import java.util.List;
+
"bottom:0;"
+
"bottom:0;"
+
"left:0;"
+
"left:0;"
+
"right:0;"
+
"right:0;"
+
"color:%s;"
+
"font-size:%s;"
+
"font-size:%s;"
+
"color:red;"
+
"\">"
,
+
"\">"
,
HtmlUtils
.
toCssRgba
(
style
.
foregroundColor
),
convertTextSizeToCss
(
defaultTextSizeType
,
defaultTextSize
)));
convertTextSizeToCss
(
defaultTextSizeType
,
defaultTextSize
)));
String
backgroundColorCss
=
HtmlUtils
.
toCssRgba
(
style
.
backgroundColor
);
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
);
float
positionPercent
=
(
cue
.
position
!=
Cue
.
DIMEN_UNSET
)
?
(
cue
.
position
*
100
)
:
50
;
float
positionPercent
=
(
cue
.
position
!=
Cue
.
DIMEN_UNSET
)
?
(
cue
.
position
*
100
)
:
50
;
...
@@ -203,6 +206,9 @@ import java.util.List;
...
@@ -203,6 +206,9 @@ import java.util.List;
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
cueTextSizeCssPx
=
convertTextSizeToCss
(
cue
.
textSizeType
,
cue
.
textSize
);
String
windowCssColor
=
HtmlUtils
.
toCssRgba
(
cue
.
windowColorSet
&&
applyEmbeddedStyles
?
cue
.
windowColor
:
style
.
windowColor
);
String
positionProperty
;
String
positionProperty
;
String
lineProperty
;
String
lineProperty
;
...
@@ -244,6 +250,7 @@ import java.util.List;
...
@@ -244,6 +250,7 @@ import java.util.List;
+
"text-align:%s;"
+
"text-align:%s;"
+
"writing-mode:%s;"
+
"writing-mode:%s;"
+
"font-size:%s;"
+
"font-size:%s;"
+
"background-color:%s;"
+
"transform:translate(%s%%,%s%%);"
+
"transform:translate(%s%%,%s%%);"
+
"\">"
,
+
"\">"
,
positionProperty
,
positionProperty
,
...
@@ -255,9 +262,12 @@ import java.util.List;
...
@@ -255,9 +262,12 @@ import java.util.List;
textAlign
,
textAlign
,
writingMode
,
writingMode
,
cueTextSizeCssPx
,
cueTextSizeCssPx
,
windowCssColor
,
horizontalTranslatePercent
,
horizontalTranslatePercent
,
verticalTranslatePercent
))
verticalTranslatePercent
))
.
append
(
Util
.
formatInvariant
(
"<span style=\"background-color:%s;\">"
,
backgroundColorCss
))
.
append
(
SpannedToHtmlConverter
.
convert
(
cue
.
text
))
.
append
(
SpannedToHtmlConverter
.
convert
(
cue
.
text
))
.
append
(
"</span>"
)
.
append
(
"</div>"
);
.
append
(
"</div>"
);
}
}
...
...
library/ui/src/test/java/com/google/android/exoplayer2/ui/HtmlUtilsTest.java
0 → 100644
View file @
caeeae2c
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer2
.
ui
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
android.graphics.Color
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
/** Tests for {@link HtmlUtils}. */
@RunWith
(
AndroidJUnit4
.
class
)
public
class
HtmlUtilsTest
{
@Test
public
void
toCssRgba_exactAlpha
()
{
String
cssRgba
=
HtmlUtils
.
toCssRgba
(
Color
.
argb
(
51
,
13
,
23
,
37
));
assertThat
(
cssRgba
).
isEqualTo
(
"rgba(13,23,37,0.200)"
);
}
@Test
public
void
toCssRgba_truncatedAlpha
()
{
String
cssRgba
=
HtmlUtils
.
toCssRgba
(
Color
.
argb
(
100
,
13
,
23
,
37
));
assertThat
(
cssRgba
).
isEqualTo
(
"rgba(13,23,37,0.392)"
);
}
}
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