Use system foreground & background color in SubtitleWebView
PiperOrigin-RevId: 309244671
This commit is contained in:
parent
4371617ec9
commit
caeeae2c9e
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
@ -16,7 +16,6 @@
|
||||
*/
|
||||
package com.google.android.exoplayer2.ui;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
import android.text.Html;
|
||||
import android.text.Spanned;
|
||||
@ -25,7 +24,6 @@ import android.text.style.ForegroundColorSpan;
|
||||
import android.text.style.StyleSpan;
|
||||
import android.text.style.UnderlineSpan;
|
||||
import android.util.SparseArray;
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan;
|
||||
import com.google.android.exoplayer2.text.span.RubySpan;
|
||||
@ -125,11 +123,12 @@ import java.util.regex.Pattern;
|
||||
if (span instanceof ForegroundColorSpan) {
|
||||
ForegroundColorSpan colorSpan = (ForegroundColorSpan) span;
|
||||
return Util.formatInvariant(
|
||||
"<span style='color:%s;'>", toCssColor(colorSpan.getForegroundColor()));
|
||||
"<span style='color:%s;'>", HtmlUtils.toCssRgba(colorSpan.getForegroundColor()));
|
||||
} else if (span instanceof BackgroundColorSpan) {
|
||||
BackgroundColorSpan colorSpan = (BackgroundColorSpan) span;
|
||||
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) {
|
||||
return "<span style='text-combine-upright:all;'>";
|
||||
} else if (span instanceof StyleSpan) {
|
||||
@ -186,12 +185,6 @@ import java.util.regex.Pattern;
|
||||
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) {
|
||||
@Nullable Transition transition = transitions.get(key);
|
||||
if (transition == null) {
|
||||
|
@ -157,11 +157,14 @@ import java.util.List;
|
||||
+ "bottom:0;"
|
||||
+ "left:0;"
|
||||
+ "right:0;"
|
||||
+ "color:%s;"
|
||||
+ "font-size:%s;"
|
||||
+ "color:red;"
|
||||
+ "\">",
|
||||
HtmlUtils.toCssRgba(style.foregroundColor),
|
||||
convertTextSizeToCss(defaultTextSizeType, defaultTextSize)));
|
||||
|
||||
String backgroundColorCss = HtmlUtils.toCssRgba(style.backgroundColor);
|
||||
|
||||
for (int i = 0; i < cues.size(); i++) {
|
||||
Cue cue = cues.get(i);
|
||||
float positionPercent = (cue.position != Cue.DIMEN_UNSET) ? (cue.position * 100) : 50;
|
||||
@ -203,6 +206,9 @@ import java.util.List;
|
||||
String textAlign = convertAlignmentToCss(cue.textAlignment);
|
||||
String writingMode = convertVerticalTypeToCss(cue.verticalType);
|
||||
String cueTextSizeCssPx = convertTextSizeToCss(cue.textSizeType, cue.textSize);
|
||||
String windowCssColor =
|
||||
HtmlUtils.toCssRgba(
|
||||
cue.windowColorSet && applyEmbeddedStyles ? cue.windowColor : style.windowColor);
|
||||
|
||||
String positionProperty;
|
||||
String lineProperty;
|
||||
@ -244,6 +250,7 @@ import java.util.List;
|
||||
+ "text-align:%s;"
|
||||
+ "writing-mode:%s;"
|
||||
+ "font-size:%s;"
|
||||
+ "background-color:%s;"
|
||||
+ "transform:translate(%s%%,%s%%);"
|
||||
+ "\">",
|
||||
positionProperty,
|
||||
@ -255,9 +262,12 @@ import java.util.List;
|
||||
textAlign,
|
||||
writingMode,
|
||||
cueTextSizeCssPx,
|
||||
windowCssColor,
|
||||
horizontalTranslatePercent,
|
||||
verticalTranslatePercent))
|
||||
.append(Util.formatInvariant("<span style=\"background-color:%s;\">", backgroundColorCss))
|
||||
.append(SpannedToHtmlConverter.convert(cue.text))
|
||||
.append("</span>")
|
||||
.append("</div>");
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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)");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user