From caeeae2c9e636f153450c8007cd30cd79adf96df Mon Sep 17 00:00:00 2001 From: ibaker Date: Thu, 30 Apr 2020 17:51:27 +0100 Subject: [PATCH] Use system foreground & background color in SubtitleWebView PiperOrigin-RevId: 309244671 --- .../android/exoplayer2/ui/HtmlUtils.java | 35 ++++++++++++++++ .../exoplayer2/ui/SpannedToHtmlConverter.java | 13 ++---- .../exoplayer2/ui/SubtitleWebView.java | 12 +++++- .../android/exoplayer2/ui/HtmlUtilsTest.java | 40 +++++++++++++++++++ 4 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 library/ui/src/main/java/com/google/android/exoplayer2/ui/HtmlUtils.java create mode 100644 library/ui/src/test/java/com/google/android/exoplayer2/ui/HtmlUtilsTest.java diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/HtmlUtils.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/HtmlUtils.java new file mode 100644 index 0000000000..0edee287a9 --- /dev/null +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/HtmlUtils.java @@ -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); + } +} diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java index 9be40ddf96..356f44e5b2 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java @@ -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( - "", toCssColor(colorSpan.getForegroundColor())); + "", HtmlUtils.toCssRgba(colorSpan.getForegroundColor())); } else if (span instanceof BackgroundColorSpan) { BackgroundColorSpan colorSpan = (BackgroundColorSpan) span; return Util.formatInvariant( - "", toCssColor(colorSpan.getBackgroundColor())); + "", + HtmlUtils.toCssRgba(colorSpan.getBackgroundColor())); } else if (span instanceof HorizontalTextInVerticalContextSpan) { return ""; } 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 transitions, int key) { @Nullable Transition transition = transitions.get(key); if (transition == null) { diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java index fb251a9aba..8f55c29c84 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SubtitleWebView.java @@ -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("", backgroundColorCss)) .append(SpannedToHtmlConverter.convert(cue.text)) + .append("") .append(""); } diff --git a/library/ui/src/test/java/com/google/android/exoplayer2/ui/HtmlUtilsTest.java b/library/ui/src/test/java/com/google/android/exoplayer2/ui/HtmlUtilsTest.java new file mode 100644 index 0000000000..82ddfb4202 --- /dev/null +++ b/library/ui/src/test/java/com/google/android/exoplayer2/ui/HtmlUtilsTest.java @@ -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)"); + } +}