Title: [121102] trunk
Revision
121102
Author
[email protected]
Date
2012-06-23 14:23:12 -0700 (Sat, 23 Jun 2012)

Log Message

RenderText’s minimum preferred width is incorrect automatic hyphenation is used
https://bugs.webkit.org/show_bug.cgi?id=89814

Reviewed by Sam Weinig.

Source/WebCore: 

Test: fast/text/hyphen-min-preferred-width.html

* rendering/RenderText.cpp:
(WebCore::maxWordFragmentWidth): Added this helper function. It returns the width of the
widest hyphenated fragment of the word (except for the suffix after the last hyphen) and
the offset of the last hyphenation opportunity.
(WebCore::RenderText::computePreferredLogicalWidths): Changed this function to consider the
widths of hyphenated word fragments rather than entire words when computing the minimum
width. Because hyphenating and measuring all fragments is expensive, hyphenation is only
attempted if the unhyphenated word is wider than the current minimum width.

LayoutTests: 

* fast/text/hyphen-min-preferred-width-expected.html: Added.
* fast/text/hyphen-min-preferred-width.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (121101 => 121102)


--- trunk/LayoutTests/ChangeLog	2012-06-23 19:28:27 UTC (rev 121101)
+++ trunk/LayoutTests/ChangeLog	2012-06-23 21:23:12 UTC (rev 121102)
@@ -1,3 +1,13 @@
+2012-06-23  Dan Bernstein  <[email protected]>
+
+        RenderText’s minimum preferred width is incorrect automatic hyphenation is used
+        https://bugs.webkit.org/show_bug.cgi?id=89814
+
+        Reviewed by Sam Weinig.
+
+        * fast/text/hyphen-min-preferred-width-expected.html: Added.
+        * fast/text/hyphen-min-preferred-width.html: Added.
+
 2012-06-23  Kenneth Russell  <[email protected]>
 
         Unreviewed Chromium gardening. Marked tests flaky per bugs 89812

Added: trunk/LayoutTests/fast/text/hyphen-min-preferred-width-expected.html (0 => 121102)


--- trunk/LayoutTests/fast/text/hyphen-min-preferred-width-expected.html	                        (rev 0)
+++ trunk/LayoutTests/fast/text/hyphen-min-preferred-width-expected.html	2012-06-23 21:23:12 UTC (rev 121102)
@@ -0,0 +1,8 @@
+<div style="outline: thin dashed lightblue; -webkit-hyphens: auto;">
+    I <span id="could-not">couldn&rsquo;t</span> tell if she truly advocated antidisestablishmentarianism.
+</div>
+<script>
+    var couldNot = document.getElementById("could-not");
+    var width = couldNot.getBoundingClientRect().width;
+    couldNot.parentElement.style.width = Math.ceil(width);
+</script>

Added: trunk/LayoutTests/fast/text/hyphen-min-preferred-width.html (0 => 121102)


--- trunk/LayoutTests/fast/text/hyphen-min-preferred-width.html	                        (rev 0)
+++ trunk/LayoutTests/fast/text/hyphen-min-preferred-width.html	2012-06-23 21:23:12 UTC (rev 121102)
@@ -0,0 +1,10 @@
+<table cellspacing=0 cellpadding=0>
+    <tr>
+        <td>
+            <div style="outline: thin dashed lightblue; -webkit-hyphens: auto;">
+                I couldn&rsquo;t tell if she truly advocated antidisestablishmentarianism.
+            </div>
+        </td>
+        <td style="width: 100%"></td>
+    </tr>
+</table>

Modified: trunk/Source/WebCore/ChangeLog (121101 => 121102)


--- trunk/Source/WebCore/ChangeLog	2012-06-23 19:28:27 UTC (rev 121101)
+++ trunk/Source/WebCore/ChangeLog	2012-06-23 21:23:12 UTC (rev 121102)
@@ -1,3 +1,21 @@
+2012-06-23  Dan Bernstein  <[email protected]>
+
+        RenderText’s minimum preferred width is incorrect automatic hyphenation is used
+        https://bugs.webkit.org/show_bug.cgi?id=89814
+
+        Reviewed by Sam Weinig.
+
+        Test: fast/text/hyphen-min-preferred-width.html
+
+        * rendering/RenderText.cpp:
+        (WebCore::maxWordFragmentWidth): Added this helper function. It returns the width of the
+        widest hyphenated fragment of the word (except for the suffix after the last hyphen) and
+        the offset of the last hyphenation opportunity.
+        (WebCore::RenderText::computePreferredLogicalWidths): Changed this function to consider the
+        widths of hyphenated word fragments rather than entire words when computing the minimum
+        width. Because hyphenating and measuring all fragments is expensive, hyphenation is only
+        attempted if the unhyphenated word is wider than the current minimum width.
+
 2012-06-23  Sheriff Bot  <[email protected]>
 
         Unreviewed, rolling out r121058.

Modified: trunk/Source/WebCore/rendering/RenderText.cpp (121101 => 121102)


--- trunk/Source/WebCore/rendering/RenderText.cpp	2012-06-23 19:28:27 UTC (rev 121101)
+++ trunk/Source/WebCore/rendering/RenderText.cpp	2012-06-23 21:23:12 UTC (rev 121102)
@@ -30,6 +30,7 @@
 #include "FloatQuad.h"
 #include "FontTranscoder.h"
 #include "FrameView.h"
+#include "Hyphenation.h"
 #include "InlineTextBox.h"
 #include "Range.h"
 #include "RenderArena.h"
@@ -888,6 +889,46 @@
     return font.width(RenderBlock::constructTextRun(renderer, font, style->hyphenString().string(), style));
 }
 
+static float maxWordFragmentWidth(RenderText* renderer, RenderStyle* style, const Font& font, const UChar* word, int wordLength, int minimumPrefixLength, int minimumSuffixLength, int& suffixStart)
+{
+    suffixStart = 0;
+    if (wordLength <= minimumSuffixLength)
+        return 0;
+
+    Vector<int, 8> hyphenLocations;
+    int hyphenLocation = wordLength - minimumSuffixLength;
+    while ((hyphenLocation = lastHyphenLocation(word, wordLength, hyphenLocation, style->locale())) >= minimumPrefixLength)
+        hyphenLocations.append(hyphenLocation);
+
+    if (hyphenLocations.isEmpty())
+        return 0;
+
+    hyphenLocations.reverse();
+
+    float minimumFragmentWidthToConsider = font.pixelSize() * 5 / 4 + hyphenWidth(renderer, font);
+    float maxFragmentWidth = 0;
+    for (size_t k = 0; k < hyphenLocations.size(); ++k) {
+        int fragmentLength = hyphenLocations[k] - suffixStart;
+        StringBuilder fragmentWithHyphen;
+        fragmentWithHyphen.append(word + suffixStart, fragmentLength);
+        fragmentWithHyphen.append(style->hyphenString());
+
+        TextRun run = RenderBlock::constructTextRun(renderer, font, fragmentWithHyphen.characters(), fragmentWithHyphen.length(), style);
+        run.setCharactersLength(fragmentWithHyphen.length());
+        run.setCharacterScanForCodePath(!renderer->canUseSimpleFontCodePath());
+        float fragmentWidth = font.width(run);
+
+        // Narrow prefixes are ignored. See tryHyphenating in RenderBlockLineLayout.cpp.
+        if (fragmentWidth <= minimumFragmentWidthToConsider)
+            continue;
+
+        suffixStart += fragmentLength;
+        maxFragmentWidth = max(maxFragmentWidth, fragmentWidth);
+    }
+
+    return maxFragmentWidth;
+}
+
 void RenderText::computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow& glyphOverflow)
 {
     ASSERT(m_hasTab || preferredLogicalWidthsDirty() || !m_knownToHaveNoOverflowAndNoFallbackFonts);
@@ -926,6 +967,24 @@
     // space, then subtract its width.
     float wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(RenderBlock::constructTextRun(this, f, &space, 1, styleToUse)) : 0;
 
+    // If automatic hyphenation is allowed, we keep track of the width of the widest word (or word
+    // fragment) encountered so far, and only try hyphenating words that are wider.
+    float maxWordWidth = numeric_limits<float>::max();
+    int minimumPrefixLength = 0;
+    int minimumSuffixLength = 0;
+    if (styleToUse->hyphens() == HyphensAuto && canHyphenate(styleToUse->locale())) {
+        maxWordWidth = 0;
+
+        // Map 'hyphenate-limit-{before,after}: auto;' to 2.
+        minimumPrefixLength = styleToUse->hyphenationLimitBefore();
+        if (minimumPrefixLength < 0)
+            minimumPrefixLength = 2;
+
+        minimumSuffixLength = styleToUse->hyphenationLimitAfter();
+        if (minimumSuffixLength < 0)
+            minimumSuffixLength = 2;
+    }
+
     int firstGlyphLeftOverflow = -1;
 
     bool breakNBSP = styleToUse->autoWrap() && styleToUse->nbspMode() == SPACE;
@@ -1005,6 +1064,25 @@
                     currMinWidth += hyphenWidth(this, f);
             }
 
+            if (w > maxWordWidth) {
+                int suffixStart;
+                float maxFragmentWidth = maxWordFragmentWidth(this, styleToUse, f, txt + i, wordLen, minimumPrefixLength, minimumSuffixLength, suffixStart);
+
+                if (suffixStart) {
+                    float suffixWidth;
+                    if (wordTrailingSpaceWidth && isSpace)
+                        suffixWidth = widthFromCache(f, i + suffixStart, wordLen - suffixStart + 1, leadWidth + currMaxWidth, 0, 0) - wordTrailingSpaceWidth;
+                    else
+                        suffixWidth = widthFromCache(f, i + suffixStart, wordLen - suffixStart, leadWidth + currMaxWidth, 0, 0);
+
+                    maxFragmentWidth = max(maxFragmentWidth, suffixWidth);
+
+                    currMinWidth += maxFragmentWidth - w;
+                    maxWordWidth = max(maxWordWidth, maxFragmentWidth);
+                } else
+                    maxWordWidth = w;
+            }
+
             if (firstGlyphLeftOverflow < 0)
                 firstGlyphLeftOverflow = glyphOverflow.left;
             currMinWidth += w;
@@ -1032,9 +1110,9 @@
                 // being appended to a previous text run when considering the total minimum width of the containing block.
                 if (hasBreak)
                     m_hasBreakableChar = true;
-                m_beginMinWidth = hasBreak ? 0 : w;
+                m_beginMinWidth = hasBreak ? 0 : currMinWidth;
             }
-            m_endMinWidth = w;
+            m_endMinWidth = currMinWidth;
 
             if (currMinWidth > m_minWidth)
                 m_minWidth = currMinWidth;
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to