Commit f91ec3f6 authored by Rahul Singh's avatar Rahul Singh Committed by Commit Bot

WebVTT: Add opacity support, and color support for window on Win10

This CL adds support for text and background opacity on Win10 by
computing the color string with an alpha value corresponding to the
user's choice in Settings.
In addition, it also adds support for window color (with opacity) now
that CaptionStyle also contains a string for window_color.

With this change, all caption styling properties that show up in Win10
Settings are now supported in Chromium on Win10. New Properties added:
1. Caption Text Opacity
2. Background Opacity
3. Window Color
4. Window Opacity

Bug: 1001736
Change-Id: I3e80f88219faa7407bbd015074c486bc4d680e5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1794002
Commit-Queue: Rahul Singh <rahsin@microsoft.com>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695853}
parent f0fcd6c1
...@@ -13,7 +13,10 @@ ...@@ -13,7 +13,10 @@
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "base/win/core_winrt_util.h" #include "base/win/core_winrt_util.h"
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
#include "skia/ext/skia_utils_win.h"
#include "ui/base/ui_base_features.h" #include "ui/base/ui_base_features.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/safe_integer_conversions.h"
namespace ui { namespace ui {
...@@ -120,33 +123,61 @@ std::string GetCaptionSizeString(ClosedCaptionSize caption_size) { ...@@ -120,33 +123,61 @@ std::string GetCaptionSizeString(ClosedCaptionSize caption_size) {
} }
} }
// Translates a Windows::Media::ClosedCaptioning::ClosedCaptionColor to a CSS // Translates a Windows::Media::ClosedCaptioning::ClosedCaptionOpacity to an
// color string. // SkAlpha value.
std::string GetCssColor(ClosedCaptionColor caption_color) { SkAlpha GetCaptionOpacity(ClosedCaptionOpacity caption_opacity) {
switch (caption_opacity) {
case ClosedCaptionOpacity_ZeroPercent:
return SK_AlphaTRANSPARENT;
case ClosedCaptionOpacity_TwentyFivePercent:
return gfx::ToRoundedInt(SK_AlphaOPAQUE * 0.25);
case ClosedCaptionOpacity_SeventyFivePercent:
return gfx::ToRoundedInt(SK_AlphaOPAQUE * 0.75);
case ClosedCaptionOpacity_OneHundredPercent:
case ClosedCaptionOpacity_Default:
default:
return SK_AlphaOPAQUE;
}
}
// Translates a Windows::Media::ClosedCaptioning::ClosedCaptionColor to an
// SkColor value.
SkColor GetCaptionColor(ClosedCaptionColor caption_color) {
switch (caption_color) { switch (caption_color) {
case ClosedCaptionColor_Black: case ClosedCaptionColor_Black:
return "black"; return SK_ColorBLACK;
case ClosedCaptionColor_Red: case ClosedCaptionColor_Red:
return "red"; return SK_ColorRED;
case ClosedCaptionColor_Green: case ClosedCaptionColor_Green:
return "green"; return SK_ColorGREEN;
case ClosedCaptionColor_Blue: case ClosedCaptionColor_Blue:
return "blue"; return SK_ColorBLUE;
case ClosedCaptionColor_Yellow: case ClosedCaptionColor_Yellow:
return "yellow"; return SK_ColorYELLOW;
case ClosedCaptionColor_Magenta: case ClosedCaptionColor_Magenta:
return "magenta"; return SK_ColorMAGENTA;
case ClosedCaptionColor_Cyan: case ClosedCaptionColor_Cyan:
return "cyan"; return SK_ColorCYAN;
case ClosedCaptionColor_White: case ClosedCaptionColor_White:
return "white"; return SK_ColorWHITE;
case ClosedCaptionColor_Default: case ClosedCaptionColor_Default:
default:
// We shouldn't override with OS Styling for Default case. // We shouldn't override with OS Styling for Default case.
NOTREACHED(); NOTREACHED();
return std::string(); return SK_ColorWHITE;
} }
} }
// Translates a Windows::Media::ClosedCaptioning::ClosedCaptionColor and a
// Windows::Media::ClosedCaptioning::ClosedCaptionOpacity to an RGBA CSS color
// string.
std::string GetCssColorWithAlpha(ClosedCaptionColor caption_color,
ClosedCaptionOpacity caption_opacity) {
const SkAlpha opacity = GetCaptionOpacity(caption_opacity);
const SkColor color = GetCaptionColor(caption_color);
return color_utils::SkColorToRgbaString(SkColorSetA(color, opacity));
}
base::Optional<CaptionStyle> InitializeFromSystemSettings() { base::Optional<CaptionStyle> InitializeFromSystemSettings() {
DCHECK_GE(base::win::GetVersion(), base::win::Version::WIN10); DCHECK_GE(base::win::GetVersion(), base::win::Version::WIN10);
DCHECK(base::FeatureList::IsEnabled(features::kSystemCaptionStyle)); DCHECK(base::FeatureList::IsEnabled(features::kSystemCaptionStyle));
...@@ -213,6 +244,15 @@ base::Optional<CaptionStyle> InitializeFromSystemSettings() { ...@@ -213,6 +244,15 @@ base::Optional<CaptionStyle> InitializeFromSystemSettings() {
return base::nullopt; return base::nullopt;
} }
ClosedCaptionOpacity font_opacity = ClosedCaptionOpacity_Default;
hr = closed_caption_properties_statics->get_FontOpacity(&font_opacity);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Font Opacity"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return base::nullopt;
}
ClosedCaptionColor background_color = ClosedCaptionColor_Default; ClosedCaptionColor background_color = ClosedCaptionColor_Default;
hr = hr =
closed_caption_properties_statics->get_BackgroundColor(&background_color); closed_caption_properties_statics->get_BackgroundColor(&background_color);
...@@ -223,6 +263,34 @@ base::Optional<CaptionStyle> InitializeFromSystemSettings() { ...@@ -223,6 +263,34 @@ base::Optional<CaptionStyle> InitializeFromSystemSettings() {
return base::nullopt; return base::nullopt;
} }
ClosedCaptionOpacity background_opacity = ClosedCaptionOpacity_Default;
hr = closed_caption_properties_statics->get_BackgroundOpacity(
&background_opacity);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Background Opacity"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return base::nullopt;
}
ClosedCaptionColor region_color = ClosedCaptionColor_Default;
hr = closed_caption_properties_statics->get_RegionColor(&region_color);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Region Color"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return base::nullopt;
}
ClosedCaptionOpacity region_opacity = ClosedCaptionOpacity_Default;
hr = closed_caption_properties_statics->get_RegionOpacity(&region_opacity);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Region Opacity"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return base::nullopt;
}
CaptionStyle caption_style; CaptionStyle caption_style;
if (font_family != ClosedCaptionStyle_Default) { if (font_family != ClosedCaptionStyle_Default) {
GetFontFamilyString(font_family, &(caption_style.font_family), GetFontFamilyString(font_family, &(caption_style.font_family),
...@@ -239,12 +307,19 @@ base::Optional<CaptionStyle> InitializeFromSystemSettings() { ...@@ -239,12 +307,19 @@ base::Optional<CaptionStyle> InitializeFromSystemSettings() {
AddCSSImportant(GetEdgeEffectString(edge_effect)); AddCSSImportant(GetEdgeEffectString(edge_effect));
} }
if (font_color != ClosedCaptionColor_Default) if (font_color != ClosedCaptionColor_Default) {
caption_style.text_color = AddCSSImportant(GetCssColor(font_color)); caption_style.text_color =
AddCSSImportant(GetCssColorWithAlpha(font_color, font_opacity));
}
if (background_color != ClosedCaptionColor_Default) { if (background_color != ClosedCaptionColor_Default) {
caption_style.background_color = caption_style.background_color = AddCSSImportant(
AddCSSImportant(GetCssColor(background_color)); GetCssColorWithAlpha(background_color, background_opacity));
}
if (region_color != ClosedCaptionColor_Default) {
caption_style.window_color =
AddCSSImportant(GetCssColorWithAlpha(region_color, region_opacity));
} }
return caption_style; return caption_style;
......
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
namespace ui { namespace ui {
// Test to ensure closed caption styling from system settings is used on // Test to ensure closed caption styling from system settings can be obtained
// Windows 10. // (we obtain a CaptionStyle) on Windows 10.
TEST(CaptionStyleWinTest, TestWinCaptionStyle) { TEST(CaptionStyleWinTest, TestWinCaptionStyle) {
base::test::ScopedFeatureList scoped_feature_list; base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(features::kSystemCaptionStyle); scoped_feature_list.InitAndEnableFeature(features::kSystemCaptionStyle);
...@@ -33,6 +33,7 @@ TEST(CaptionStyleWinTest, TestWinCaptionStyle) { ...@@ -33,6 +33,7 @@ TEST(CaptionStyleWinTest, TestWinCaptionStyle) {
EXPECT_TRUE(caption_style->text_color.empty()); EXPECT_TRUE(caption_style->text_color.empty());
EXPECT_TRUE(caption_style->text_shadow.empty()); EXPECT_TRUE(caption_style->text_shadow.empty());
EXPECT_TRUE(caption_style->text_size.empty()); EXPECT_TRUE(caption_style->text_size.empty());
EXPECT_TRUE(caption_style->window_color.empty());
} }
} }
......
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 to comment