Improve consistency of captions rendering on Mac
authorjonlee@apple.com <jonlee@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 2 Mar 2017 18:08:41 +0000 (18:08 +0000)
committerjonlee@apple.com <jonlee@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 2 Mar 2017 18:08:41 +0000 (18:08 +0000)
https://bugs.webkit.org/show_bug.cgi?id=169071

Reviewed by Eric Carlson.

* page/CaptionUserPreferencesMediaAF.h:
(WebCore::CaptionUserPreferencesMediaAF::captionsEdgeColorForTextColor): Deleted.
(WebCore::CaptionUserPreferencesMediaAF::cssPropertyWithTextEdgeColor): Deleted.

* page/CaptionUserPreferencesMediaAF.cpp:
(WebCore::appendCSS): Helper function to add a CSS rule with a provided StringBuilder.
(WebCore::CaptionUserPreferencesMediaAF::windowRoundedCornerRadiusCSS): Refactor to use
appendCSS.
(WebCore::CaptionUserPreferencesMediaAF::colorPropertyCSS): Ditto.
(WebCore::CaptionUserPreferencesMediaAF::captionsTextEdgeCSS): Update text edge styles.
- Add a blur to the shadow
- Drop shadow style includes 1px text border
- Always use black for the text edge color
Also convert the stroke rules to using the CSS property and values instead of straight strings.
(WebCore::CaptionUserPreferencesMediaAF::captionsStyleSheetOverride): Fly-by fix of a typo.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@213285 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/page/CaptionUserPreferencesMediaAF.cpp
Source/WebCore/page/CaptionUserPreferencesMediaAF.h

index bcd1782..396529d 100644 (file)
@@ -1,3 +1,26 @@
+2017-03-02  Jon Lee  <jonlee@apple.com>
+
+        Improve consistency of captions rendering on Mac
+        https://bugs.webkit.org/show_bug.cgi?id=169071
+
+        Reviewed by Eric Carlson.
+
+        * page/CaptionUserPreferencesMediaAF.h:
+        (WebCore::CaptionUserPreferencesMediaAF::captionsEdgeColorForTextColor): Deleted.
+        (WebCore::CaptionUserPreferencesMediaAF::cssPropertyWithTextEdgeColor): Deleted.
+
+        * page/CaptionUserPreferencesMediaAF.cpp:
+        (WebCore::appendCSS): Helper function to add a CSS rule with a provided StringBuilder.
+        (WebCore::CaptionUserPreferencesMediaAF::windowRoundedCornerRadiusCSS): Refactor to use
+        appendCSS.
+        (WebCore::CaptionUserPreferencesMediaAF::colorPropertyCSS): Ditto.
+        (WebCore::CaptionUserPreferencesMediaAF::captionsTextEdgeCSS): Update text edge styles.
+        - Add a blur to the shadow
+        - Drop shadow style includes 1px text border
+        - Always use black for the text edge color
+        Also convert the stroke rules to using the CSS property and values instead of straight strings.
+        (WebCore::CaptionUserPreferencesMediaAF::captionsStyleSheetOverride): Fly-by fix of a typo.
+
 2017-03-02  Chris Dumez  <cdumez@apple.com>
 
         Download attribute set to "*\" save file with name "example.com"
index 117143b..e567ea0 100644 (file)
@@ -327,67 +327,33 @@ String CaptionUserPreferencesMediaAF::captionsTextColorCSS() const
 
     return colorPropertyCSS(CSSPropertyColor, textColor, important);
 }
-    
-String CaptionUserPreferencesMediaAF::windowRoundedCornerRadiusCSS() const
-{
-    MACaptionAppearanceBehavior behavior;
-    CGFloat radius = MACaptionAppearanceGetWindowRoundedCornerRadius(kMACaptionAppearanceDomainUser, &behavior);
-    if (!radius)
-        return emptyString();
-
-    StringBuilder builder;
-    builder.append(getPropertyNameString(CSSPropertyBorderRadius));
-    builder.append(String::format(":%.02fpx", radius));
-    if (behavior == kMACaptionAppearanceBehaviorUseValue)
-        builder.appendLiteral(" !important");
-    builder.append(';');
-
-    return builder.toString();
-}
-    
-Color CaptionUserPreferencesMediaAF::captionsEdgeColorForTextColor(const Color& textColor) const
-{
-    int distanceFromWhite = differenceSquared(textColor, Color::white);
-    int distanceFromBlack = differenceSquared(textColor, Color::black);
-    
-    if (distanceFromWhite < distanceFromBlack)
-        return textColor.dark();
-    
-    return textColor.light();
-}
 
-String CaptionUserPreferencesMediaAF::cssPropertyWithTextEdgeColor(CSSPropertyID id, const String& value, const Color& textColor, bool important) const
+static void appendCSS(StringBuilder& builder, CSSPropertyID id, const String& value, bool important)
 {
-    StringBuilder builder;
-    
     builder.append(getPropertyNameString(id));
     builder.append(':');
     builder.append(value);
-    builder.append(' ');
-    builder.append(captionsEdgeColorForTextColor(textColor).serialized());
     if (important)
         builder.appendLiteral(" !important");
     builder.append(';');
-    if (id == CSSPropertyWebkitTextStroke) {
-        builder.append(" paint-order: stroke;");
-        builder.append(" stroke-linejoin: round;");
-        builder.append(" stroke-linecap: round;");
-    }
+}
     
+String CaptionUserPreferencesMediaAF::windowRoundedCornerRadiusCSS() const
+{
+    MACaptionAppearanceBehavior behavior;
+    CGFloat radius = MACaptionAppearanceGetWindowRoundedCornerRadius(kMACaptionAppearanceDomainUser, &behavior);
+    if (!radius)
+        return emptyString();
+
+    StringBuilder builder;
+    appendCSS(builder, CSSPropertyBorderRadius, String::format("%.02fpx", radius), behavior == kMACaptionAppearanceBehaviorUseValue);
     return builder.toString();
 }
 
 String CaptionUserPreferencesMediaAF::colorPropertyCSS(CSSPropertyID id, const Color& color, bool important) const
 {
     StringBuilder builder;
-    
-    builder.append(getPropertyNameString(id));
-    builder.append(':');
-    builder.append(color.serialized());
-    if (important)
-        builder.appendLiteral(" !important");
-    builder.append(';');
-    
+    appendCSS(builder, id, color.serialized(), important);
     return builder.toString();
 }
 
@@ -417,38 +383,37 @@ String CaptionUserPreferencesMediaAF::strokeWidth() const
 
 String CaptionUserPreferencesMediaAF::captionsTextEdgeCSS() const
 {
-    static NeverDestroyed<const String> edgeStyleRaised(ASCIILiteral(" -.05em -.05em 0 "));
-    static NeverDestroyed<const String> edgeStyleDepressed(ASCIILiteral(" .05em .05em 0 "));
-    static NeverDestroyed<const String> edgeStyleDropShadow(ASCIILiteral(" .075em .075em 0 "));
-
-    bool unused;
-    Color color = captionsTextColor(unused);
-    if (!color.isValid())
-        color = Color { Color::black };
-    color = captionsEdgeColorForTextColor(color);
+    static NeverDestroyed<const String> edgeStyleRaised(ASCIILiteral(" -.1em -.1em .16em "));
+    static NeverDestroyed<const String> edgeStyleDepressed(ASCIILiteral(" .1em .1em .16em "));
+    static NeverDestroyed<const String> edgeStyleDropShadow(ASCIILiteral(" 0 .1em .16em "));
 
     MACaptionAppearanceBehavior behavior;
     MACaptionAppearanceTextEdgeStyle textEdgeStyle = MACaptionAppearanceGetTextEdgeStyle(kMACaptionAppearanceDomainUser, &behavior);
-    switch (textEdgeStyle) {
-    case kMACaptionAppearanceTextEdgeStyleUndefined:
-    case kMACaptionAppearanceTextEdgeStyleNone:
-        return emptyString();
-            
-    case kMACaptionAppearanceTextEdgeStyleRaised:
-        return cssPropertyWithTextEdgeColor(CSSPropertyTextShadow, edgeStyleRaised, color, behavior == kMACaptionAppearanceBehaviorUseValue);
-    case kMACaptionAppearanceTextEdgeStyleDepressed:
-        return cssPropertyWithTextEdgeColor(CSSPropertyTextShadow, edgeStyleDepressed, color, behavior == kMACaptionAppearanceBehaviorUseValue);
-    case kMACaptionAppearanceTextEdgeStyleDropShadow:
-        return cssPropertyWithTextEdgeColor(CSSPropertyTextShadow, edgeStyleDropShadow, color, behavior == kMACaptionAppearanceBehaviorUseValue);
-    case kMACaptionAppearanceTextEdgeStyleUniform:
-        return cssPropertyWithTextEdgeColor(CSSPropertyWebkitTextStroke, strokeWidth(), color, behavior == kMACaptionAppearanceBehaviorUseValue);
     
-    default:
-        ASSERT_NOT_REACHED();
-        break;
+    if (textEdgeStyle == kMACaptionAppearanceTextEdgeStyleUndefined || textEdgeStyle == kMACaptionAppearanceTextEdgeStyleNone)
+        return emptyString();
+
+    StringBuilder builder;
+    bool important = behavior == kMACaptionAppearanceBehaviorUseValue;
+    if (textEdgeStyle == kMACaptionAppearanceTextEdgeStyleRaised)
+        appendCSS(builder, CSSPropertyTextShadow, makeString(edgeStyleRaised.get(), " black"), important);
+    else if (textEdgeStyle == kMACaptionAppearanceTextEdgeStyleDepressed)
+        appendCSS(builder, CSSPropertyTextShadow, makeString(edgeStyleDepressed.get(), " black"), important);
+    else if (textEdgeStyle == kMACaptionAppearanceTextEdgeStyleDropShadow)
+        appendCSS(builder, CSSPropertyTextShadow, makeString(edgeStyleDropShadow.get(), " black"), important);
+
+    if (textEdgeStyle == kMACaptionAppearanceTextEdgeStyleDropShadow || textEdgeStyle == kMACaptionAppearanceTextEdgeStyleUniform) {
+        if (textEdgeStyle == kMACaptionAppearanceTextEdgeStyleDropShadow)
+            appendCSS(builder, CSSPropertyWebkitTextStroke, ".03em black", important);
+        else
+            appendCSS(builder, CSSPropertyWebkitTextStroke, makeString(strokeWidth(), " black"), important);
+
+        appendCSS(builder, CSSPropertyPaintOrder, getValueName(CSSValueStroke), important);
+        appendCSS(builder, CSSPropertyStrokeLinejoin, getValueName(CSSValueRound), important);
+        appendCSS(builder, CSSPropertyStrokeLinecap, getValueName(CSSValueRound), important);
     }
     
-    return emptyString();
+    return builder.toString();
 }
 
 String CaptionUserPreferencesMediaAF::captionsDefaultFontCSS() const
@@ -620,7 +585,7 @@ String CaptionUserPreferencesMediaAF::captionsStyleSheetOverride() const
     }
 #endif // HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
 
-    LOG(Media, "CaptionUserPreferencesMediaAF::captionsStyleSheetOverrideSetting sytle to:\n%s", captionsOverrideStyleSheet.toString().utf8().data());
+    LOG(Media, "CaptionUserPreferencesMediaAF::captionsStyleSheetOverrideSetting style to:\n%s", captionsOverrideStyleSheet.toString().utf8().data());
 
     return captionsOverrideStyleSheet.toString();
 }
index 7616f51..d9027c7 100644 (file)
@@ -79,11 +79,9 @@ private:
     String captionsTextColorCSS() const;
     Color captionsTextColor(bool&) const;
     String captionsDefaultFontCSS() const;
-    Color captionsEdgeColorForTextColor(const Color&) const;
     String windowRoundedCornerRadiusCSS() const;
     String strokeWidth() const;
     String captionsTextEdgeCSS() const;
-    String cssPropertyWithTextEdgeColor(CSSPropertyID, const String&, const Color&, bool) const;
     String colorPropertyCSS(CSSPropertyID, const Color&, bool) const;
     Timer m_updateStyleSheetTimer;