Title: [247792] trunk
Revision
247792
Author
[email protected]
Date
2019-07-24 15:36:13 -0700 (Wed, 24 Jul 2019)

Log Message

Daring Fireball long press highlights are unnecessarily inflated due to false illegibility
https://bugs.webkit.org/show_bug.cgi?id=200064

Reviewed by Geoff Garen.

Source/WebCore:

If we consider text illegible on the given estimated background color,
we bail from doing a tightly fitted selection-only TextIndicator and
instead just paint the page without modification into the indicator,
causing ugly overlap and an excessively inflated indicator.

Change the mechanism we use to determine illegibility to be based on
a standard, instead of a constant chosen by hand 13 years ago.

Test: fast/text-indicator/text-indicator-with-low-contrast-text.html

* platform/graphics/ColorUtilities.cpp:
(WebCore::luminance):
Fix a typo.

(WebCore::contrastRatio):
Add a function that computes the contrast ratio given two colors using
the formula from WCAG.

* platform/graphics/ColorUtilities.h:
* rendering/TextPaintStyle.cpp:
(WebCore::textColorIsLegibleAgainstBackgroundColor):
Make use of WCAG's minimum legible contrast ratio instead of an
arbitrary color difference cutoff for determining whether we consider
text legible. It seems sensible and also considers the text on DF readable
(which it seems to be to me!).

* testing/Internals.cpp:
(WebCore::Internals::TextIndicatorInfo::TextIndicatorInfo):
* testing/Internals.h:
* testing/Internals.idl:
Expose all of the text rects to Internals, not just the bounding rect.
Expose some more TextIndicator options to Internals so that we can
turn on the legibility mechanism.

LayoutTests:

* fast/text-indicator/text-indicator-with-low-contrast-text-expected.txt: Added.
* fast/text-indicator/text-indicator-with-low-contrast-text.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (247791 => 247792)


--- trunk/LayoutTests/ChangeLog	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/LayoutTests/ChangeLog	2019-07-24 22:36:13 UTC (rev 247792)
@@ -1,3 +1,13 @@
+2019-07-24  Tim Horton  <[email protected]>
+
+        Daring Fireball long press highlights are unnecessarily inflated due to false illegibility
+        https://bugs.webkit.org/show_bug.cgi?id=200064
+
+        Reviewed by Geoff Garen.
+
+        * fast/text-indicator/text-indicator-with-low-contrast-text-expected.txt: Added.
+        * fast/text-indicator/text-indicator-with-low-contrast-text.html: Added.
+
 2019-07-24  Devin Rousso  <[email protected]>
 
         Web Inspector: print the target of `console.screenshot` last so the target is the closest item to the image

Added: trunk/LayoutTests/fast/text-indicator/text-indicator-with-low-contrast-text-expected.txt (0 => 247792)


--- trunk/LayoutTests/fast/text-indicator/text-indicator-with-low-contrast-text-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/text-indicator/text-indicator-with-low-contrast-text-expected.txt	2019-07-24 22:36:13 UTC (rev 247792)
@@ -0,0 +1,40 @@
+whiteTextWhiteBackground: NOT legible
+    0 0 68 66
+whiteTextGrayBackground: NOT legible
+    0 0 68 66
+whiteTextBlackBackground: legible
+    0 0 68 18
+    0 48 36 18
+grayTextWhiteBackground: NOT legible
+    0 0 68 66
+grayTextGrayBackground: NOT legible
+    0 0 68 66
+grayTextBlackBackground: legible
+    0 0 68 18
+    0 48 36 18
+lightGrayTextWhiteBackground: NOT legible
+    0 0 68 66
+lightGrayTextGrayBackground: NOT legible
+    0 0 68 66
+lightGrayTextBlackBackground: legible
+    0 0 68 18
+    0 48 36 18
+darkGrayTextWhiteBackground: legible
+    0 0 68 18
+    0 48 36 18
+darkGrayTextGrayBackground: NOT legible
+    0 0 68 66
+darkGrayTextBlackBackground: NOT legible
+    0 0 68 66
+blackTextWhiteBackground: legible
+    0 0 68 18
+    0 48 36 18
+blackTextGrayBackground: legible
+    0 0 68 18
+    0 48 36 18
+blackTextBlackBackground: NOT legible
+    0 0 68 66
+dfTextDfBackground: legible
+    0 0 68 18
+    0 48 36 18
+

Added: trunk/LayoutTests/fast/text-indicator/text-indicator-with-low-contrast-text.html (0 => 247792)


--- trunk/LayoutTests/fast/text-indicator/text-indicator-with-low-contrast-text.html	                        (rev 0)
+++ trunk/LayoutTests/fast/text-indicator/text-indicator-with-low-contrast-text.html	2019-07-24 22:36:13 UTC (rev 247792)
@@ -0,0 +1,106 @@
+<!DOCTYPE html>
+<html>
+<head>
+<style>
+* { box-sizing: border-box; font-family: ahem; }
+body { margin: 0; }
+div { position: absolute; top: 0; left: 0; }
+
+.whiteText { color: white; }
+.grayText { color: gray; }
+.lightGrayText { color: lightgray; }
+.darkGrayText { color: dimgray; }
+.blackText { color: black; }
+.dfGrayText { color: #ddd; }
+
+.whiteBackground { background-color: white; }
+.grayBackground { background-color: gray; }
+.blackBackground { background-color: black; }
+.dfGrayBackground { background-color: #4a525a; }
+</style>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+function output(s)
+{
+    window.log.innerText += s + "\n";
+}
+
+function runTest()
+{
+    if (!window.internals) {
+        output("This test cannot be run outside of WebKitTestRunner.");
+        return;
+    }
+
+    function dumpIndicatorBoundsForElement(el)
+    {
+        var indicatorOptions = {
+            "useBoundingRectAndPaintAllContentForComplexRanges": true,
+            "computeEstimatedBackgroundColor": true,
+            "respectTextColor": true,
+        };
+        var range = internals.rangeFromLocationAndLength(el, 0, 9);
+        var indicator = window.internals.textIndicatorForRange(range, indicatorOptions);
+        var rects = indicator.textRectsInBoundingRectCoordinates;
+
+        output(`${el.id}: ` + (rects.length == 1 ? "NOT legible" : "legible"));
+        for (var rect of rects)
+            output(`    ${rect.x} ${rect.y} ${rect.width} ${rect.height}`);
+    }
+
+    dumpIndicatorBoundsForElement(document.getElementById("whiteTextWhiteBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("whiteTextGrayBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("whiteTextBlackBackground"));
+
+    dumpIndicatorBoundsForElement(document.getElementById("grayTextWhiteBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("grayTextGrayBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("grayTextBlackBackground"));
+
+    dumpIndicatorBoundsForElement(document.getElementById("lightGrayTextWhiteBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("lightGrayTextGrayBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("lightGrayTextBlackBackground"));
+
+    dumpIndicatorBoundsForElement(document.getElementById("darkGrayTextWhiteBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("darkGrayTextGrayBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("darkGrayTextBlackBackground"));
+
+    dumpIndicatorBoundsForElement(document.getElementById("blackTextWhiteBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("blackTextGrayBackground"));
+    dumpIndicatorBoundsForElement(document.getElementById("blackTextBlackBackground"));
+
+    dumpIndicatorBoundsForElement(document.getElementById("dfTextDfBackground"));
+
+    // Remove the test cases so they don't show up in the dump.
+    document.getElementById("testCases").innerText = "";
+}
+</script>
+</head>
+<body _onload_="runTest()">
+<div id="testCases">
+    <div class="whiteText whiteBackground" id="whiteTextWhiteBackground">test<br><br><br>test</div>
+    <div class="whiteText grayBackground" id="whiteTextGrayBackground">test<br><br><br>test</div>
+    <div class="whiteText blackBackground" id="whiteTextBlackBackground">test<br><br><br>test</div>
+
+    <div class="grayText whiteBackground" id="grayTextWhiteBackground">test<br><br><br>test</div>
+    <div class="grayText grayBackground" id="grayTextGrayBackground">test<br><br><br>test</div>
+    <div class="grayText blackBackground" id="grayTextBlackBackground">test<br><br><br>test</div>
+
+    <div class="lightGrayText whiteBackground" id="lightGrayTextWhiteBackground">test<br><br><br>test</div>
+    <div class="lightGrayText grayBackground" id="lightGrayTextGrayBackground">test<br><br><br>test</div>
+    <div class="lightGrayText blackBackground" id="lightGrayTextBlackBackground">test<br><br><br>test</div>
+
+    <div class="darkGrayText whiteBackground" id="darkGrayTextWhiteBackground">test<br><br><br>test</div>
+    <div class="darkGrayText grayBackground" id="darkGrayTextGrayBackground">test<br><br><br>test</div>
+    <div class="darkGrayText blackBackground" id="darkGrayTextBlackBackground">test<br><br><br>test</div>
+
+    <div class="blackText whiteBackground" id="blackTextWhiteBackground">test<br><br><br>test</div>
+    <div class="blackText grayBackground" id="blackTextGrayBackground">test<br><br><br>test</div>
+    <div class="blackText blackBackground" id="blackTextBlackBackground">test<br><br><br>test</div>
+
+    <div class="dfGrayText dfGrayBackground" id="dfTextDfBackground">test<br><br><br>test</div>
+</div>
+<pre id="log"></pre>
+</body>
+</html>
\ No newline at end of file

Modified: trunk/Source/WebCore/ChangeLog (247791 => 247792)


--- trunk/Source/WebCore/ChangeLog	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/Source/WebCore/ChangeLog	2019-07-24 22:36:13 UTC (rev 247792)
@@ -1,3 +1,44 @@
+2019-07-24  Tim Horton  <[email protected]>
+
+        Daring Fireball long press highlights are unnecessarily inflated due to false illegibility
+        https://bugs.webkit.org/show_bug.cgi?id=200064
+
+        Reviewed by Geoff Garen.
+
+        If we consider text illegible on the given estimated background color,
+        we bail from doing a tightly fitted selection-only TextIndicator and
+        instead just paint the page without modification into the indicator,
+        causing ugly overlap and an excessively inflated indicator.
+
+        Change the mechanism we use to determine illegibility to be based on
+        a standard, instead of a constant chosen by hand 13 years ago.
+
+        Test: fast/text-indicator/text-indicator-with-low-contrast-text.html
+
+        * platform/graphics/ColorUtilities.cpp:
+        (WebCore::luminance):
+        Fix a typo.
+
+        (WebCore::contrastRatio):
+        Add a function that computes the contrast ratio given two colors using
+        the formula from WCAG.
+
+        * platform/graphics/ColorUtilities.h:
+        * rendering/TextPaintStyle.cpp:
+        (WebCore::textColorIsLegibleAgainstBackgroundColor):
+        Make use of WCAG's minimum legible contrast ratio instead of an
+        arbitrary color difference cutoff for determining whether we consider
+        text legible. It seems sensible and also considers the text on DF readable
+        (which it seems to be to me!).
+
+        * testing/Internals.cpp:
+        (WebCore::Internals::TextIndicatorInfo::TextIndicatorInfo):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+        Expose all of the text rects to Internals, not just the bounding rect.
+        Expose some more TextIndicator options to Internals so that we can
+        turn on the legibility mechanism.
+
 2019-07-24  Devin Rousso  <[email protected]>
 
         Web Inspector: print the target of `console.screenshot` last so the target is the closest item to the image

Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp (247791 => 247792)


--- trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp	2019-07-24 22:36:13 UTC (rev 247792)
@@ -104,14 +104,27 @@
     return clampTo<float>(std::pow((c + 0.055f) / 1.055f, 2.4f), 0, 1);
 }
 
-float luminance(const FloatComponents& sRGBCompontents)
+float luminance(const FloatComponents& sRGBComponents)
 {
     // Values from https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
-    return 0.2126f * sRGBToLinearColorComponentForLuminance(sRGBCompontents.components[0])
-        + 0.7152f * sRGBToLinearColorComponentForLuminance(sRGBCompontents.components[1])
-        + 0.0722f * sRGBToLinearColorComponentForLuminance(sRGBCompontents.components[2]);
+    return 0.2126f * sRGBToLinearColorComponentForLuminance(sRGBComponents.components[0])
+        + 0.7152f * sRGBToLinearColorComponentForLuminance(sRGBComponents.components[1])
+        + 0.0722f * sRGBToLinearColorComponentForLuminance(sRGBComponents.components[2]);
 }
 
+float contrastRatio(const FloatComponents& componentsA, const FloatComponents& componentsB)
+{
+    // Uses the WCAG 2.0 definition of contrast ratio.
+    // https://www.w3.org/TR/WCAG20/#contrast-ratiodef
+    float lighterLuminance = luminance(componentsA);
+    float darkerLuminance = luminance(componentsB);
+
+    if (lighterLuminance < darkerLuminance)
+        std::swap(lighterLuminance, darkerLuminance);
+
+    return (lighterLuminance + 0.05) / (darkerLuminance + 0.05);
+}
+
 FloatComponents sRGBToHSL(const FloatComponents& sRGBColor)
 {
     // http://en.wikipedia.org/wiki/HSL_color_space.

Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.h (247791 => 247792)


--- trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2019-07-24 22:36:13 UTC (rev 247792)
@@ -162,6 +162,7 @@
 FloatComponents HSLToSRGB(const FloatComponents&);
 
 float luminance(const FloatComponents& sRGBCompontents);
+float contrastRatio(const FloatComponents&, const FloatComponents&);
 
 class ColorMatrix {
 public:

Modified: trunk/Source/WebCore/rendering/TextPaintStyle.cpp (247791 => 247792)


--- trunk/Source/WebCore/rendering/TextPaintStyle.cpp	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/Source/WebCore/rendering/TextPaintStyle.cpp	2019-07-24 22:36:13 UTC (rev 247792)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "TextPaintStyle.h"
 
+#include "ColorUtilities.h"
 #include "FocusController.h"
 #include "Frame.h"
 #include "GraphicsContext.h"
@@ -60,8 +61,9 @@
 
 bool textColorIsLegibleAgainstBackgroundColor(const Color& textColor, const Color& backgroundColor)
 {
-    // Semi-arbitrarily chose 65025 (255^2) value here after a few tests.
-    return differenceSquared(textColor, backgroundColor) > 65025;
+    // Uses the WCAG 2.0 definition of legibility: a contrast ratio of 4.5:1 or greater.
+    // https://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast
+    return contrastRatio(textColor, backgroundColor) > 4.5;
 }
 
 static Color adjustColorForVisibilityOnBackground(const Color& textColor, const Color& backgroundColor)

Modified: trunk/Source/WebCore/testing/Internals.cpp (247791 => 247792)


--- trunk/Source/WebCore/testing/Internals.cpp	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/Source/WebCore/testing/Internals.cpp	2019-07-24 22:36:13 UTC (rev 247792)
@@ -5125,6 +5125,7 @@
 
 Internals::TextIndicatorInfo::TextIndicatorInfo(const WebCore::TextIndicatorData& data)
     : textBoundingRectInRootViewCoordinates(DOMRect::create(data.textBoundingRectInRootViewCoordinates))
+    , textRectsInBoundingRectCoordinates(DOMRectList::create(data.textRectsInBoundingRectCoordinates))
 {
 }
     

Modified: trunk/Source/WebCore/testing/Internals.h (247791 => 247792)


--- trunk/Source/WebCore/testing/Internals.h	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/Source/WebCore/testing/Internals.h	2019-07-24 22:36:13 UTC (rev 247792)
@@ -837,6 +837,7 @@
     
     struct TextIndicatorInfo {
         RefPtr<DOMRectReadOnly> textBoundingRectInRootViewCoordinates;
+        RefPtr<DOMRectList> textRectsInBoundingRectCoordinates;
         
         TextIndicatorInfo();
         TextIndicatorInfo(const WebCore::TextIndicatorData&);
@@ -845,6 +846,8 @@
         
     struct TextIndicatorOptions {
         bool useBoundingRectAndPaintAllContentForComplexRanges { false };
+        bool computeEstimatedBackgroundColor { false };
+        bool respectTextColor { false };
         
         WebCore::TextIndicatorOptions core()
         {
@@ -851,6 +854,10 @@
             WebCore::TextIndicatorOptions options = 0;
             if (useBoundingRectAndPaintAllContentForComplexRanges)
                 options = options | TextIndicatorOptionUseBoundingRectAndPaintAllContentForComplexRanges;
+            if (computeEstimatedBackgroundColor)
+                options = options | TextIndicatorOptionComputeEstimatedBackgroundColor;
+            if (respectTextColor)
+                options = options | TextIndicatorOptionRespectTextColor;
             return options;
         }
     };

Modified: trunk/Source/WebCore/testing/Internals.idl (247791 => 247792)


--- trunk/Source/WebCore/testing/Internals.idl	2019-07-24 21:44:32 UTC (rev 247791)
+++ trunk/Source/WebCore/testing/Internals.idl	2019-07-24 22:36:13 UTC (rev 247792)
@@ -164,6 +164,7 @@
     JSGenerateToJSObject,
 ] dictionary TextIndicatorInfo {
     DOMRectReadOnly textBoundingRectInRootViewCoordinates;
+    DOMRectList textRectsInBoundingRectCoordinates;
 };
 
 [
@@ -171,6 +172,8 @@
     JSGenerateToJSObject,
 ] dictionary TextIndicatorOptions {
     boolean useBoundingRectAndPaintAllContentForComplexRanges = false;
+    boolean computeEstimatedBackgroundColor = false;
+    boolean respectTextColor = false;
 };
 
 [
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to