Commit 1862faa9 authored by Rahul Singh's avatar Rahul Singh Committed by Commit Bot

webvtt: Add support for Windows system styling for captions

This change:
1) Adds 4 new properties to CaptionStyle.
2) Adds caption_style_win.cc that contains a Windows implementation for
   CaptionStyle::FromSystemSettings().
3) Adds a new feature flag to enable Windows system styling.
4) Follows the same pattern as the earlier change here:
   https://chromium-review.googlesource.com/c/chromium/src/+/1418399

Next steps:
Add support for background opacity, window opacity, and window color

Bug: 897730

Change-Id: Iefb0092c32b4fdb3d4d6412a2e21b9bdf018887f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1482140Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Commit-Queue: Rahul Singh <rahsin@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#638316}
parent 73ff9e18
...@@ -3326,6 +3326,10 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs( ...@@ -3326,6 +3326,10 @@ void ChromeContentBrowserClient::OverrideWebkitPrefs(
ui::NativeTheme::GetInstanceForWeb()->GetSystemCaptionStyle(); ui::NativeTheme::GetInstanceForWeb()->GetSystemCaptionStyle();
web_prefs->text_track_background_color = style.background_color; web_prefs->text_track_background_color = style.background_color;
web_prefs->text_track_text_color = style.text_color; web_prefs->text_track_text_color = style.text_color;
web_prefs->text_track_text_size = style.text_size;
web_prefs->text_track_text_shadow = style.text_shadow;
web_prefs->text_track_font_family = style.font_family;
web_prefs->text_track_font_variant = style.font_variant;
for (size_t i = 0; i < extra_parts_.size(); ++i) for (size_t i = 0; i < extra_parts_.size(); ++i)
extra_parts_[i]->OverrideWebkitPrefs(rvh, web_prefs); extra_parts_[i]->OverrideWebkitPrefs(rvh, web_prefs);
......
...@@ -196,6 +196,10 @@ IPC_STRUCT_TRAITS_BEGIN(content::WebPreferences) ...@@ -196,6 +196,10 @@ IPC_STRUCT_TRAITS_BEGIN(content::WebPreferences)
IPC_STRUCT_TRAITS_MEMBER(text_track_background_color) IPC_STRUCT_TRAITS_MEMBER(text_track_background_color)
IPC_STRUCT_TRAITS_MEMBER(text_track_margin_percentage) IPC_STRUCT_TRAITS_MEMBER(text_track_margin_percentage)
IPC_STRUCT_TRAITS_MEMBER(text_track_text_color) IPC_STRUCT_TRAITS_MEMBER(text_track_text_color)
IPC_STRUCT_TRAITS_MEMBER(text_track_text_size)
IPC_STRUCT_TRAITS_MEMBER(text_track_text_shadow)
IPC_STRUCT_TRAITS_MEMBER(text_track_font_family)
IPC_STRUCT_TRAITS_MEMBER(text_track_font_variant)
IPC_STRUCT_TRAITS_MEMBER(text_autosizing_enabled) IPC_STRUCT_TRAITS_MEMBER(text_autosizing_enabled)
IPC_STRUCT_TRAITS_MEMBER(double_tap_to_zoom_enabled) IPC_STRUCT_TRAITS_MEMBER(double_tap_to_zoom_enabled)
IPC_STRUCT_TRAITS_MEMBER(web_app_scope) IPC_STRUCT_TRAITS_MEMBER(web_app_scope)
......
...@@ -206,6 +206,15 @@ struct CONTENT_EXPORT WebPreferences { ...@@ -206,6 +206,15 @@ struct CONTENT_EXPORT WebPreferences {
std::string text_track_background_color; std::string text_track_background_color;
std::string text_track_text_color; std::string text_track_text_color;
// These fields specify values for CSS properties used to style WebVTT text
// tracks.
// Specifies CSS font-size property in percentage.
std::string text_track_text_size;
std::string text_track_text_shadow;
std::string text_track_font_family;
// Specifies the value for CSS font-variant property.
std::string text_track_font_variant;
// Specifies the margin for WebVTT text tracks as a percentage of media // Specifies the margin for WebVTT text tracks as a percentage of media
// element height/width (for horizontal/vertical text respectively). // element height/width (for horizontal/vertical text respectively).
// Cues will not be placed in this margin area. // Cues will not be placed in this margin area.
......
...@@ -823,6 +823,14 @@ void RenderView::ApplyWebPreferences(const WebPreferences& prefs, ...@@ -823,6 +823,14 @@ void RenderView::ApplyWebPreferences(const WebPreferences& prefs,
WebString::FromASCII(prefs.text_track_background_color)); WebString::FromASCII(prefs.text_track_background_color));
settings->SetTextTrackTextColor( settings->SetTextTrackTextColor(
WebString::FromASCII(prefs.text_track_text_color)); WebString::FromASCII(prefs.text_track_text_color));
settings->SetTextTrackTextSize(
WebString::FromASCII(prefs.text_track_text_size));
settings->SetTextTrackTextShadow(
WebString::FromASCII(prefs.text_track_text_shadow));
settings->SetTextTrackFontFamily(
WebString::FromASCII(prefs.text_track_font_family));
settings->SetTextTrackFontVariant(
WebString::FromASCII(prefs.text_track_font_variant));
settings->SetTextTrackMarginPercentage(prefs.text_track_margin_percentage); settings->SetTextTrackMarginPercentage(prefs.text_track_margin_percentage);
// Needs to happen before SetDefaultPageScaleLimits below since that'll // Needs to happen before SetDefaultPageScaleLimits below since that'll
......
...@@ -43,6 +43,10 @@ const base::Feature kSettingsShowsPerKeyboardSettings = { ...@@ -43,6 +43,10 @@ const base::Feature kSettingsShowsPerKeyboardSettings = {
const base::Feature kInputMethodSettingsUiUpdate = { const base::Feature kInputMethodSettingsUiUpdate = {
"InputMethodSettingsUiUpdate", base::FEATURE_DISABLED_BY_DEFAULT}; "InputMethodSettingsUiUpdate", base::FEATURE_DISABLED_BY_DEFAULT};
// Allows system caption style for WebVTT Captions.
const base::Feature kSystemCaptionStyle{"SystemCaptionStyle",
base::FEATURE_DISABLED_BY_DEFAULT};
// Allows system keyboard event capture via the keyboard lock API. // Allows system keyboard event capture via the keyboard lock API.
const base::Feature kSystemKeyboardLock{"SystemKeyboardLock", const base::Feature kSystemKeyboardLock{"SystemKeyboardLock",
base::FEATURE_ENABLED_BY_DEFAULT}; base::FEATURE_ENABLED_BY_DEFAULT};
......
...@@ -27,6 +27,8 @@ extern const base::Feature kSettingsShowsPerKeyboardSettings; ...@@ -27,6 +27,8 @@ extern const base::Feature kSettingsShowsPerKeyboardSettings;
COMPONENT_EXPORT(UI_BASE_FEATURES) COMPONENT_EXPORT(UI_BASE_FEATURES)
extern const base::Feature kInputMethodSettingsUiUpdate; extern const base::Feature kInputMethodSettingsUiUpdate;
COMPONENT_EXPORT(UI_BASE_FEATURES) COMPONENT_EXPORT(UI_BASE_FEATURES)
extern const base::Feature kSystemCaptionStyle;
COMPONENT_EXPORT(UI_BASE_FEATURES)
extern const base::Feature kSystemKeyboardLock; extern const base::Feature kSystemKeyboardLock;
COMPONENT_EXPORT(UI_BASE_FEATURES) COMPONENT_EXPORT(UI_BASE_FEATURES)
extern const base::Feature kNotificationIndicator; extern const base::Feature kNotificationIndicator;
......
...@@ -10,6 +10,7 @@ jumbo_component("native_theme") { ...@@ -10,6 +10,7 @@ jumbo_component("native_theme") {
sources = [ sources = [
"caption_style.cc", "caption_style.cc",
"caption_style.h", "caption_style.h",
"caption_style_win.cc",
"common_theme.cc", "common_theme.cc",
"common_theme.h", "common_theme.h",
"native_theme.cc", "native_theme.cc",
...@@ -111,6 +112,10 @@ test("native_theme_unittests") { ...@@ -111,6 +112,10 @@ test("native_theme_unittests") {
sources += [ "native_theme_mac_unittest.cc" ] sources += [ "native_theme_mac_unittest.cc" ]
} }
if (is_win) {
sources += [ "caption_style_win_unittest.cc" ]
}
deps = [ deps = [
":native_theme", ":native_theme",
"//base/test:run_all_unittests", "//base/test:run_all_unittests",
......
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "ui/native_theme/caption_style.h" #include "ui/native_theme/caption_style.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/values.h" #include "base/values.h"
#include "build/build_config.h"
namespace ui { namespace ui {
...@@ -29,4 +29,10 @@ CaptionStyle CaptionStyle::FromSpec(const std::string& spec) { ...@@ -29,4 +29,10 @@ CaptionStyle CaptionStyle::FromSpec(const std::string& spec) {
return style; return style;
} }
#if !defined(OS_WIN)
CaptionStyle CaptionStyle::FromSystemSettings() {
return CaptionStyle();
}
#endif
} // namespace ui } // namespace ui
...@@ -25,10 +25,18 @@ struct NATIVE_THEME_EXPORT CaptionStyle { ...@@ -25,10 +25,18 @@ struct NATIVE_THEME_EXPORT CaptionStyle {
// for testing. // for testing.
static CaptionStyle FromSpec(const std::string& spec); static CaptionStyle FromSpec(const std::string& spec);
// Returns a CaptionStyle populated from the System's Settings.
static CaptionStyle FromSystemSettings();
std::string text_color; std::string text_color;
std::string background_color; std::string background_color;
// Holds text size percentage as a css string.
std::string text_size;
std::string text_shadow;
std::string font_family;
std::string font_variant;
}; };
} // namespace ui } // namespace ui
#endif #endif // UI_NATIVE_THEME_CAPTION_STYLE_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/native_theme/caption_style.h"
#include <Windows.Media.ClosedCaptioning.h>
#include <wrl/client.h>
#include <string>
#include "base/command_line.h"
#include "base/trace_event/trace_event.h"
#include "base/win/core_winrt_util.h"
#include "base/win/windows_version.h"
#include "ui/base/ui_base_features.h"
namespace ui {
using namespace ABI::Windows::Media::ClosedCaptioning;
namespace {
// This is used to add tracing when a COM call fails.
// Param 1 - line is the line number in the caller. This helps locate
// problematic code.
// Param 2 - hr is the failure HRESULT.
void LogCapStyleWinError(int line, HRESULT hr) {
TRACE_EVENT2("ui", "LogWindowsCaptionStyleError", "line", line, "hr", hr);
}
// Translates a Windows::Media::ClosedCaptioning::ClosedCaptionStyle to a
// CSS FontFamily value.
// These fonts were chosen to satisfy the characteristics represented by values
// of ClosedCaptionStyle Enum in Windows Settings.
void GetFontFamilyString(ClosedCaptionStyle closed_caption_style,
std::string* css_font_family,
std::string* css_font_variant) {
*css_font_variant = "normal";
switch (closed_caption_style) {
case ClosedCaptionStyle_MonospacedWithSerifs:
*css_font_family = "Courier New";
break;
case ClosedCaptionStyle_ProportionalWithSerifs:
*css_font_family = "Times New Roman";
break;
case ClosedCaptionStyle_MonospacedWithoutSerifs:
*css_font_family = "Consolas";
break;
case ClosedCaptionStyle_ProportionalWithoutSerifs:
*css_font_family = "Tahoma";
break;
case ClosedCaptionStyle_Casual:
*css_font_family = "Segoe Print";
break;
case ClosedCaptionStyle_Cursive:
*css_font_family = "Segoe Script";
break;
case ClosedCaptionStyle_SmallCapitals:
*css_font_family = "Tahoma";
*css_font_variant = "small-caps";
break;
case ClosedCaptionStyle_Default:
*css_font_family = std::string();
break;
}
}
// Translates a Windows::Media::ClosedCaptioning::ClosedCaptionEdgeEffect to a
// CSS style value.
std::string GetEdgeEffectString(ClosedCaptionEdgeEffect edge_effect) {
switch (edge_effect) {
case ClosedCaptionEdgeEffect_None:
return "none";
case ClosedCaptionEdgeEffect_Raised:
return "-1px 0px 0px silver, 0px -1px 0px silver, 1px 1px 0px black, 2px "
"2px 0px black, 3px 3px 0px black";
case ClosedCaptionEdgeEffect_Depressed:
return "1px 1px 0px silver, 0px 1px 0px silver, -1px -1px 0px black, "
"-1px "
"0px 0px black";
case ClosedCaptionEdgeEffect_Uniform:
return "0px 0px 4px black, 0px 0px 4px black, 0px 0px 4px black, 0px 0px "
"4px black";
case ClosedCaptionEdgeEffect_DropShadow:
return "3px 3px 3px 2px black";
case ClosedCaptionEdgeEffect_Default:
return std::string();
}
}
// Translates a Windows::Media::ClosedCaptioning::ClosedCaptionSize to a CSS
// style value.
std::string GetCaptionSizeString(ClosedCaptionSize caption_size) {
switch (caption_size) {
case ClosedCaptionSize_FiftyPercent:
return "50%";
case ClosedCaptionSize_OneHundredPercent:
return "100%";
case ClosedCaptionSize_OneHundredFiftyPercent:
return "150%";
case ClosedCaptionSize_TwoHundredPercent:
return "200%";
case ClosedCaptionSize_Default:
return std::string();
}
}
// Translates a Windows::Media::ClosedCaptioning::ClosedCaptionColor to a CSS
// color string.
std::string GetCssColor(ClosedCaptionColor caption_color) {
switch (caption_color) {
case ClosedCaptionColor_Black:
return "black";
case ClosedCaptionColor_Red:
return "red";
case ClosedCaptionColor_Green:
return "green";
case ClosedCaptionColor_Blue:
return "blue";
case ClosedCaptionColor_Yellow:
return "yellow";
case ClosedCaptionColor_Magenta:
return "magenta";
case ClosedCaptionColor_Cyan:
return "cyan";
case ClosedCaptionColor_White:
case ClosedCaptionColor_Default:
return "white";
}
}
CaptionStyle InitializeFromSystemSettings() {
DCHECK_GE(base::win::GetVersion(), base::win::VERSION_WIN10);
DCHECK(base::FeatureList::IsEnabled(features::kSystemCaptionStyle));
// Need to do this check before using ScopedHString.
bool can_use_scoped_hstring =
base::win::ResolveCoreWinRTDelayload() &&
base::win::ScopedHString::ResolveCoreWinRTStringDelayload();
if (!can_use_scoped_hstring) {
DLOG(ERROR) << "Failed loading functions from combase.dll";
LogCapStyleWinError(__LINE__, E_FAIL);
return CaptionStyle();
}
base::win::ScopedHString closed_caption_properties_string =
base::win::ScopedHString::Create(
RuntimeClass_Windows_Media_ClosedCaptioning_ClosedCaptionProperties);
Microsoft::WRL::ComPtr<IClosedCaptionPropertiesStatics>
closed_caption_properties_statics;
HRESULT hr = base::win::RoGetActivationFactory(
closed_caption_properties_string.get(),
IID_PPV_ARGS(&closed_caption_properties_statics));
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to Get ActivationFactory for ClosedCaptionProperties"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return CaptionStyle();
}
ClosedCaptionSize font_size = ClosedCaptionSize_Default;
hr = closed_caption_properties_statics->get_FontSize(&font_size);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Font Size"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return CaptionStyle();
}
ClosedCaptionEdgeEffect edge_effect = ClosedCaptionEdgeEffect_Default;
hr = closed_caption_properties_statics->get_FontEffect(&edge_effect);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Font Effect"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return CaptionStyle();
}
ClosedCaptionStyle font_family = ClosedCaptionStyle_Default;
hr = closed_caption_properties_statics->get_FontStyle(&font_family);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Font Family"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return CaptionStyle();
}
ClosedCaptionColor font_color = ClosedCaptionColor_Default;
hr = closed_caption_properties_statics->get_FontColor(&font_color);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Font Color"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return CaptionStyle();
}
ClosedCaptionColor background_color = ClosedCaptionColor_Default;
hr =
closed_caption_properties_statics->get_BackgroundColor(&background_color);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to retrieve Background Color"
<< ", HRESULT: 0x" << std::hex << hr;
LogCapStyleWinError(__LINE__, hr);
return CaptionStyle();
}
CaptionStyle caption_style;
GetFontFamilyString(font_family, &(caption_style.font_family),
&(caption_style.font_variant));
caption_style.text_size = GetCaptionSizeString(font_size);
caption_style.text_shadow = GetEdgeEffectString(edge_effect);
caption_style.text_color = GetCssColor(font_color);
caption_style.background_color = GetCssColor(background_color);
return caption_style;
}
} // namespace
CaptionStyle CaptionStyle::FromSystemSettings() {
CaptionStyle style;
if (base::win::GetVersion() >= base::win::VERSION_WIN10 &&
base::FeatureList::IsEnabled(features::kSystemCaptionStyle)) {
style = InitializeFromSystemSettings();
}
// Return default CaptionStyle for pre Win10 versions since system settings
// don't allow caption styling.
return style;
}
} // namespace ui
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/native_theme/caption_style.h"
#include "base/test/scoped_feature_list.h"
#include "base/win/scoped_com_initializer.h"
#include "base/win/windows_version.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
namespace ui {
// Test to ensure closed caption styling from system settings is used on
// Windows 10.
TEST(CaptionStyleWinTest, TestWinCaptionStyle) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(features::kSystemCaptionStyle);
if (base::win::GetVersion() >= base::win::VERSION_WIN10) {
base::win::ScopedCOMInitializer com_initializer;
ASSERT_TRUE(com_initializer.Succeeded());
ui::CaptionStyle caption_style = ui::CaptionStyle::FromSystemSettings();
// Other caption style properties can be empty and shouldn't be checked.
EXPECT_TRUE(!caption_style.background_color.empty());
EXPECT_TRUE(!caption_style.text_color.empty());
EXPECT_TRUE(!caption_style.font_variant.empty());
}
}
} // namespace ui
...@@ -55,11 +55,14 @@ bool NativeTheme::SystemDarkModeEnabled() const { ...@@ -55,11 +55,14 @@ bool NativeTheme::SystemDarkModeEnabled() const {
} }
CaptionStyle NativeTheme::GetSystemCaptionStyle() const { CaptionStyle NativeTheme::GetSystemCaptionStyle() const {
std::string style = if (base::CommandLine::ForCurrentProcess()->HasSwitch(
::switches::kForceCaptionStyle)) {
return CaptionStyle::FromSpec(
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kForceCaptionStyle); switches::kForceCaptionStyle));
}
return CaptionStyle::FromSpec(style); return CaptionStyle::FromSystemSettings();
} }
} // namespace ui } // namespace ui
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