<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[281300] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/281300">281300</a></dd>
<dt>Author</dt> <dd>mmaxfield@apple.com</dd>
<dt>Date</dt> <dd>2021-08-20 01:05:02 -0700 (Fri, 20 Aug 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>GlyphBuffer can become inconsistent with its backing string
https://bugs.webkit.org/show_bug.cgi?id=229064

Reviewed by Alan Bujtas.

This is split out from https://bugs.webkit.org/show_bug.cgi?id=215643.

Before shaping, the glyphs in the GlyphBuffer need to match one-to-one with code units in the string.
We iterate over the string, adding glyphs into the GlyphBuffer, but there was one early "continue"
in the loop that skips the add() call.

Because this patch adds ASSERT()s, this is actually covered by existing tests.

* platform/graphics/WidthIterator.cpp:
(WebCore::WidthIterator::commitCurrentFontRange):
(WebCore::addToGlyphBuffer):
(WebCore::WidthIterator::advanceInternal):
* platform/graphics/WidthIterator.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsWidthIteratorcpp">trunk/Source/WebCore/platform/graphics/WidthIterator.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsWidthIteratorh">trunk/Source/WebCore/platform/graphics/WidthIterator.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (281299 => 281300)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-08-20 08:03:55 UTC (rev 281299)
+++ trunk/Source/WebCore/ChangeLog      2021-08-20 08:05:02 UTC (rev 281300)
</span><span class="lines">@@ -1,3 +1,24 @@
</span><ins>+2021-08-20  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        GlyphBuffer can become inconsistent with its backing string
+        https://bugs.webkit.org/show_bug.cgi?id=229064
+
+        Reviewed by Alan Bujtas.
+
+        This is split out from https://bugs.webkit.org/show_bug.cgi?id=215643.
+
+        Before shaping, the glyphs in the GlyphBuffer need to match one-to-one with code units in the string.
+        We iterate over the string, adding glyphs into the GlyphBuffer, but there was one early "continue"
+        in the loop that skips the add() call.
+
+        Because this patch adds ASSERT()s, this is actually covered by existing tests.
+
+        * platform/graphics/WidthIterator.cpp:
+        (WebCore::WidthIterator::commitCurrentFontRange):
+        (WebCore::addToGlyphBuffer):
+        (WebCore::WidthIterator::advanceInternal):
+        * platform/graphics/WidthIterator.h:
+
</ins><span class="cx"> 2021-08-20  Tim Nguyen  <ntim@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Ensure ancestors with opacity don't affect top layer elements
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsWidthIteratorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/WidthIterator.cpp (281299 => 281300)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/WidthIterator.cpp 2021-08-20 08:03:55 UTC (rev 281299)
+++ trunk/Source/WebCore/platform/graphics/WidthIterator.cpp    2021-08-20 08:05:02 UTC (rev 281300)
</span><span class="lines">@@ -222,12 +222,18 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WidthIterator::commitCurrentFontRange(GlyphBuffer& glyphBuffer, unsigned lastGlyphCount, unsigned currentCharacterIndex, const Font& font, const Font& primaryFont, UChar32 character, float widthOfCurrentFontRange, CharactersTreatedAsSpace& charactersTreatedAsSpace)
</del><ins>+void WidthIterator::commitCurrentFontRange(GlyphBuffer& glyphBuffer, unsigned& lastGlyphCount, unsigned currentCharacterIndex, const Font*& font, const Font& newFont, const Font& primaryFont, UChar32 character, float& widthOfCurrentFontRange, float nextCharacterWidth, CharactersTreatedAsSpace& charactersTreatedAsSpace)
</ins><span class="cx"> {
</span><ins>+#if ASSERT_ENABLED
+    ASSERT(font);
+    for (unsigned i = lastGlyphCount; i < glyphBuffer.size(); ++i)
+        ASSERT(&glyphBuffer.fontAt(i) == font);
+#endif
+
</ins><span class="cx">     std::optional<GlyphBufferAdvance> initialAdvance;
</span><span class="cx">     auto transformsType = shouldApplyFontTransforms(glyphBuffer, lastGlyphCount, currentCharacterIndex);
</span><span class="cx">     if (transformsType != TransformsType::None) {
</span><del>-        auto applyFontTransformsResult = applyFontTransforms(glyphBuffer, lastGlyphCount, currentCharacterIndex, font, transformsType == TransformsType::Forced, charactersTreatedAsSpace);
</del><ins>+        auto applyFontTransformsResult = applyFontTransforms(glyphBuffer, lastGlyphCount, currentCharacterIndex, *font, transformsType == TransformsType::Forced, charactersTreatedAsSpace);
</ins><span class="cx">         m_runWidthSoFar += applyFontTransformsResult.additionalAdvance;
</span><span class="cx">         initialAdvance = applyFontTransformsResult.initialAdvance;
</span><span class="cx">     }
</span><span class="lines">@@ -234,11 +240,11 @@
</span><span class="cx">     applyInitialAdvance(glyphBuffer, initialAdvance, lastGlyphCount);
</span><span class="cx">     m_currentCharacterIndex = currentCharacterIndex;
</span><span class="cx"> 
</span><del>-    if (widthOfCurrentFontRange && m_fallbackFonts && &font != &primaryFont) {
</del><ins>+    if (widthOfCurrentFontRange && m_fallbackFonts && font != &primaryFont) {
</ins><span class="cx">         // FIXME: This does a little extra work that could be avoided if
</span><span class="cx">         // glyphDataForCharacter() returned whether it chose to use a small caps font.
</span><span class="cx">         if (!m_font.isSmallCaps() || character == u_toupper(character))
</span><del>-            m_fallbackFonts->add(&font);
</del><ins>+            m_fallbackFonts->add(font);
</ins><span class="cx">         else {
</span><span class="cx">             auto glyphFont = m_font.glyphDataForCharacter(u_toupper(character), m_run.rtl()).font;
</span><span class="cx">             if (glyphFont != &primaryFont)
</span><span class="lines">@@ -245,6 +251,10 @@
</span><span class="cx">                 m_fallbackFonts->add(glyphFont);
</span><span class="cx">         }
</span><span class="cx">     }
</span><ins>+
+    lastGlyphCount = glyphBuffer.size();
+    font = &newFont;
+    widthOfCurrentFontRange = nextCharacterWidth;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool WidthIterator::hasExtraSpacing() const
</span><span class="lines">@@ -252,6 +262,21 @@
</span><span class="cx">     return (m_font.letterSpacing() || m_font.wordSpacing() || m_expansion) && !m_run.spacingDisabled();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void addToGlyphBuffer(GlyphBuffer& glyphBuffer, Glyph glyph, const Font& font, float width, GlyphBufferStringOffset currentCharacterIndex, UChar32 character)
+{
+    glyphBuffer.add(glyph, font, width, currentCharacterIndex);
+#if USE(CTFONTSHAPEGLYPHS)
+    // These 0 glyphs are needed by shapers if the source text has surrogate pairs.
+    // However, CTFontTransformGlyphs() can't delete these 0 glyphs from the shaped text,
+    // so we shouldn't add them in the first place if we're using that shaping routine.
+    // Any other shaping routine should delete these glyphs from the shaped text.
+    if (!U_IS_BMP(character))
+        glyphBuffer.add(0, font, 0, currentCharacterIndex + 1);
+#else
+    UNUSED_PARAM(character);
+#endif
+}
+
</ins><span class="cx"> template <typename TextIterator>
</span><span class="cx"> inline void WidthIterator::advanceInternal(TextIterator& textIterator, GlyphBuffer& glyphBuffer)
</span><span class="cx"> {
</span><span class="lines">@@ -291,6 +316,11 @@
</span><span class="cx">         const GlyphData& glyphData = m_font.glyphDataForCharacter(character, rtl);
</span><span class="cx">         Glyph glyph = glyphData.glyph;
</span><span class="cx">         if (!glyph && !characterMustDrawSomething) {
</span><ins>+            commitCurrentFontRange(glyphBuffer, lastGlyphCount, currentCharacterIndex, lastFontData, primaryFont, primaryFont, character, widthOfCurrentFontRange, width, charactersTreatedAsSpace);
+
+            Glyph deletedGlyph = 0xFFFF;
+            addToGlyphBuffer(glyphBuffer, deletedGlyph, primaryFont, 0, currentCharacterIndex, character);
+
</ins><span class="cx">             textIterator.advance(advanceLength);
</span><span class="cx">             currentCharacterIndex = textIterator.currentIndex();
</span><span class="cx">             continue;
</span><span class="lines">@@ -300,12 +330,9 @@
</span><span class="cx">         previousWidth = width;
</span><span class="cx">         width = font.widthForGlyph(glyph);
</span><span class="cx"> 
</span><del>-        if (&font != lastFontData) {
-            commitCurrentFontRange(glyphBuffer, lastGlyphCount, currentCharacterIndex, *lastFontData, primaryFont, character, widthOfCurrentFontRange, charactersTreatedAsSpace);
-            lastGlyphCount = glyphBuffer.size();
-            lastFontData = &font;
-            widthOfCurrentFontRange = width;
-        } else
</del><ins>+        if (&font != lastFontData)
+            commitCurrentFontRange(glyphBuffer, lastGlyphCount, currentCharacterIndex, lastFontData, font, primaryFont, character, widthOfCurrentFontRange, width, charactersTreatedAsSpace);
+        else
</ins><span class="cx">             widthOfCurrentFontRange += width;
</span><span class="cx"> 
</span><span class="cx">         auto transformsType = shouldApplyFontTransforms(glyphBuffer, lastGlyphCount, currentCharacterIndex);
</span><span class="lines">@@ -326,15 +353,7 @@
</span><span class="cx">         if (m_forTextEmphasis && !FontCascade::canReceiveTextEmphasis(character))
</span><span class="cx">             glyph = 0;
</span><span class="cx"> 
</span><del>-        glyphBuffer.add(glyph, font, width, currentCharacterIndex);
-#if USE(CTFONTSHAPEGLYPHS)
-        // These 0 glyphs are needed by shapers if the source text has surrogate pairs.
-        // However, CTFontTransformGlyphs() can't delete these 0 glyphs from the shaped text,
-        // so we shouldn't add them in the first place if we're using that shaping routine.
-        // Any other shaping routine should delete these glyphs from the shaped text.
-        if (!U_IS_BMP(character))
-            glyphBuffer.add(0, font, 0, currentCharacterIndex + 1);
-#endif
</del><ins>+        addToGlyphBuffer(glyphBuffer, glyph, font, width, currentCharacterIndex, character);
</ins><span class="cx"> 
</span><span class="cx">         // Advance past the character we just dealt with.
</span><span class="cx">         textIterator.advance(advanceLength);
</span><span class="lines">@@ -349,7 +368,7 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    commitCurrentFontRange(glyphBuffer, lastGlyphCount, currentCharacterIndex, *lastFontData, primaryFont, character, widthOfCurrentFontRange, charactersTreatedAsSpace);
</del><ins>+    commitCurrentFontRange(glyphBuffer, lastGlyphCount, currentCharacterIndex, lastFontData, primaryFont, primaryFont, character, widthOfCurrentFontRange, width, charactersTreatedAsSpace);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> auto WidthIterator::calculateAdditionalWidth(GlyphBuffer& glyphBuffer, GlyphBufferStringOffset currentCharacterIndex, unsigned leadingGlyphIndex, unsigned trailingGlyphIndex, float position) const -> AdditionalWidth
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsWidthIteratorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/WidthIterator.h (281299 => 281300)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/WidthIterator.h   2021-08-20 08:03:55 UTC (rev 281299)
+++ trunk/Source/WebCore/platform/graphics/WidthIterator.h      2021-08-20 08:05:02 UTC (rev 281300)
</span><span class="lines">@@ -68,7 +68,7 @@
</span><span class="cx">         GlyphBufferAdvance initialAdvance;
</span><span class="cx">     };
</span><span class="cx">     ApplyFontTransformsResult applyFontTransforms(GlyphBuffer&, unsigned lastGlyphCount, unsigned currentCharacterIndex, const Font&, bool force, CharactersTreatedAsSpace&);
</span><del>-    void commitCurrentFontRange(GlyphBuffer&, unsigned lastGlyphCount, unsigned currentCharacterIndex, const Font&, const Font& primaryFont, UChar32 character, float widthOfCurrentFontRange, CharactersTreatedAsSpace&);
</del><ins>+    void commitCurrentFontRange(GlyphBuffer&, unsigned& lastGlyphCount, unsigned currentCharacterIndex, const Font*&, const Font& newFont, const Font& primaryFont, UChar32 character, float& widthOfCurrentFontRange, float nextCharacterWidth, CharactersTreatedAsSpace&);
</ins><span class="cx">     void applyInitialAdvance(GlyphBuffer&, std::optional<GlyphBufferAdvance> initialAdvance, unsigned lastGlyphCount);
</span><span class="cx"> 
</span><span class="cx">     bool hasExtraSpacing() const;
</span></span></pre>
</div>
</div>

</body>
</html>