<!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>[178249] 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/178249">178249</a></dd>
<dt>Author</dt> <dd>mmaxfield@apple.com</dd>
<dt>Date</dt> <dd>2015-01-11 14:55:43 -0800 (Sun, 11 Jan 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>[SVG -&gt; OTF Converter] Implement ligatures
https://bugs.webkit.org/show_bug.cgi?id=137094

Reviewed by Dan Bernstein.

Use the &quot;liga&quot; OpenType feature to implement ligatures inside the GSUB table.

Tests: svg/W3C-SVG-1.1/fonts-glyph-04-t.svg
       svg/W3C-SVG-1.1/text-text-06-t.svg
       svg/text/kerning.svg
       svg/text/multichar-glyph.svg

* svg/SVGToOTFFontConversion.cpp:
(WebCore::SVGToOTFFontConverter::grow):
(WebCore::SVGToOTFFontConverter::appendCMAPTable): Use StringView::codePoints().
(WebCore::SVGToOTFFontConverter::firstGlyph): Returns the first element of the input
vector, along with some ASSERTs.
(WebCore::SVGToOTFFontConverter::appendLigatureSubtable):
(WebCore::SVGToOTFFontConverter::appendScriptSubtable): Used inside appendGSUBTable.
(WebCore::SVGToOTFFontConverter::appendGSUBTable): Updating for ligatures.
(WebCore::codepointToString): Wrapper around U16_APPEND().
(WebCore::SVGToOTFFontConverter::glyphsForCodepoint): Call codepointToString and look
in internal map.
(WebCore::SVGToOTFFontConverter::addCodepointRanges): Use glyphsForCodepoint().
(WebCore::SVGToOTFFontConverter::appendLigatureGlyphs): Ligatures are implemented as
mapping a sequence of glyphs to another glyph inside OpenType. However, SVG models
ligatures as mapping a sequence of codepoints to a glyph. This function makes dummy
glyphs for all the codepoints that we don't have glyphs for and appends them to
m_glyphs.
(WebCore::SVGToOTFFontConverter::compareCodepointsLexicographically):
(WebCore::SVGToOTFFontConverter::SVGToOTFFontConverter): Use appendEmptyGlyph() and
call appendLigatureGlyphs().</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoresvgSVGToOTFFontConversioncpp">trunk/Source/WebCore/svg/SVGToOTFFontConversion.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (178248 => 178249)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-01-11 22:39:49 UTC (rev 178248)
+++ trunk/Source/WebCore/ChangeLog        2015-01-11 22:55:43 UTC (rev 178249)
</span><span class="lines">@@ -1,3 +1,38 @@
</span><ins>+2015-01-11  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
+
+        [SVG -&gt; OTF Converter] Implement ligatures
+        https://bugs.webkit.org/show_bug.cgi?id=137094
+
+        Reviewed by Dan Bernstein.
+
+        Use the &quot;liga&quot; OpenType feature to implement ligatures inside the GSUB table.
+
+        Tests: svg/W3C-SVG-1.1/fonts-glyph-04-t.svg
+               svg/W3C-SVG-1.1/text-text-06-t.svg
+               svg/text/kerning.svg
+               svg/text/multichar-glyph.svg
+
+        * svg/SVGToOTFFontConversion.cpp:
+        (WebCore::SVGToOTFFontConverter::grow):
+        (WebCore::SVGToOTFFontConverter::appendCMAPTable): Use StringView::codePoints().
+        (WebCore::SVGToOTFFontConverter::firstGlyph): Returns the first element of the input
+        vector, along with some ASSERTs.
+        (WebCore::SVGToOTFFontConverter::appendLigatureSubtable):
+        (WebCore::SVGToOTFFontConverter::appendScriptSubtable): Used inside appendGSUBTable.
+        (WebCore::SVGToOTFFontConverter::appendGSUBTable): Updating for ligatures.
+        (WebCore::codepointToString): Wrapper around U16_APPEND().
+        (WebCore::SVGToOTFFontConverter::glyphsForCodepoint): Call codepointToString and look
+        in internal map.
+        (WebCore::SVGToOTFFontConverter::addCodepointRanges): Use glyphsForCodepoint().
+        (WebCore::SVGToOTFFontConverter::appendLigatureGlyphs): Ligatures are implemented as
+        mapping a sequence of glyphs to another glyph inside OpenType. However, SVG models
+        ligatures as mapping a sequence of codepoints to a glyph. This function makes dummy
+        glyphs for all the codepoints that we don't have glyphs for and appends them to
+        m_glyphs.
+        (WebCore::SVGToOTFFontConverter::compareCodepointsLexicographically):
+        (WebCore::SVGToOTFFontConverter::SVGToOTFFontConverter): Use appendEmptyGlyph() and
+        call appendLigatureGlyphs().
+
</ins><span class="cx"> 2015-01-11  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Move more SVG CSS properties to the new StyleBuilder
</span></span></pre></div>
<a id="trunkSourceWebCoresvgSVGToOTFFontConversioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/svg/SVGToOTFFontConversion.cpp (178248 => 178249)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/svg/SVGToOTFFontConversion.cpp        2015-01-11 22:39:49 UTC (rev 178248)
+++ trunk/Source/WebCore/svg/SVGToOTFFontConversion.cpp        2015-01-11 22:55:43 UTC (rev 178249)
</span><span class="lines">@@ -40,7 +40,8 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><del>-static inline void append32(Vector&lt;char&gt;&amp; result, uint32_t value)
</del><ins>+template &lt;typename V&gt;
+static inline void append32(V&amp; result, uint32_t value)
</ins><span class="cx"> {
</span><span class="cx">     result.append(value &gt;&gt; 24);
</span><span class="cx">     result.append(value &gt;&gt; 16);
</span><span class="lines">@@ -161,6 +162,11 @@
</span><span class="cx">         m_result.append(value);
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    void grow(size_t delta)
+    {
+        m_result.grow(m_result.size() + delta);
+    }
+
</ins><span class="cx">     void overwrite32(unsigned location, uint32_t value)
</span><span class="cx">     {
</span><span class="cx">         ASSERT(m_result.size() &gt;= location + 4);
</span><span class="lines">@@ -201,6 +207,7 @@
</span><span class="cx">     void appendCFFTable();
</span><span class="cx">     void appendVORGTable();
</span><span class="cx"> 
</span><ins>+    void appendLigatureGlyphs();
</ins><span class="cx">     static bool compareCodepointsLexicographically(const GlyphData&amp;, const GlyphData&amp;);
</span><span class="cx"> 
</span><span class="cx">     void appendValidCFFString(const String&amp;);
</span><span class="lines">@@ -214,12 +221,17 @@
</span><span class="cx">     template&lt;typename T&gt; size_t appendKERNSubtable(bool (T::*buildKerningPair)(SVGKerningPair&amp;) const, uint16_t coverage);
</span><span class="cx">     size_t finishAppendingKERNSubtable(Vector&lt;KerningData&gt;, uint16_t coverage);
</span><span class="cx"> 
</span><ins>+    void appendLigatureSubtable(size_t subtableRecordLocation);
</ins><span class="cx">     void appendArabicReplacementSubtable(size_t subtableRecordLocation, const char arabicForm[]);
</span><ins>+    void appendScriptSubtable(unsigned featureCount);
+    Vector&lt;Glyph, 1&gt; glyphsForCodepoint(UChar32) const;
+    Glyph firstGlyph(const Vector&lt;Glyph, 1&gt;&amp;, UChar32) const;
</ins><span class="cx"> 
</span><span class="cx">     Vector&lt;GlyphData&gt; m_glyphs;
</span><span class="cx">     HashMap&lt;String, Glyph&gt; m_glyphNameToIndexMap; // SVG 1.1: &quot;It is recommended that glyph names be unique within a font.&quot;
</span><span class="cx">     HashMap&lt;String, Vector&lt;Glyph, 1&gt;&gt; m_codepointsToIndicesMap;
</span><span class="cx">     Vector&lt;char&gt; m_result;
</span><ins>+    Vector&lt;char, 17&gt; m_emptyGlyphCharString;
</ins><span class="cx">     FloatRect m_boundingBox;
</span><span class="cx">     const SVGFontElement&amp; m_fontElement;
</span><span class="cx">     const SVGFontFaceElement* m_fontFaceElement;
</span><span class="lines">@@ -229,6 +241,7 @@
</span><span class="cx">     float m_advanceHeightMax;
</span><span class="cx">     float m_minRightSideBearing;
</span><span class="cx">     unsigned m_unitsPerEm;
</span><ins>+    unsigned m_featureCountGSUB;
</ins><span class="cx">     int m_tablesAppendedCount;
</span><span class="cx">     char m_weight;
</span><span class="cx">     bool m_italic;
</span><span class="lines">@@ -273,19 +286,19 @@
</span><span class="cx">     UChar32 previousCodepoint = std::numeric_limits&lt;UChar32&gt;::max();
</span><span class="cx">     for (size_t i = 0; i &lt; m_glyphs.size(); ++i) {
</span><span class="cx">         auto&amp; glyph = m_glyphs[i];
</span><del>-        unsigned codepointSize = 0;
</del><span class="cx">         UChar32 codepoint;
</span><del>-        if (glyph.codepoints.isNull())
</del><ins>+        auto codePoints = StringView(glyph.codepoints).codePoints();
+        auto iterator = codePoints.begin();
+        if (iterator == codePoints.end())
</ins><span class="cx">             codepoint = 0;
</span><del>-        else if (glyph.codepoints.is8Bit())
-            codepoint = glyph.codepoints[codepointSize++];
-        else
-            U16_NEXT(glyph.codepoints.characters16(), codepointSize, glyph.codepoints.length(), codepoint);
</del><ins>+        else {
+            codepoint = *iterator;
+            ++iterator;
+            // Don't map ligatures here.
+            if (iterator != codePoints.end() || codepoint == previousCodepoint)
+                continue;
+        }
</ins><span class="cx"> 
</span><del>-        // Don't map ligatures here.
-        if (glyph.codepoints.length() &gt; codepointSize || codepoint == previousCodepoint)
-            continue;
-
</del><span class="cx">         append32(codepoint); // startCharCode
</span><span class="cx">         append32(codepoint); // endCharCode
</span><span class="cx">         append32(i); // startGlyphCode
</span><span class="lines">@@ -614,6 +627,100 @@
</span><span class="cx">         m_result.appendVector(glyph.charString);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+Glyph SVGToOTFFontConverter::firstGlyph(const Vector&lt;Glyph, 1&gt;&amp; v, UChar32 codepoint) const
+{
+#if ASSERT_DISABLED
+    UNUSED_PARAM(codepoint);
+#endif
+    ASSERT(!v.isEmpty());
+    if (v.isEmpty())
+        return 0;
+#if !ASSERT_DISABLED
+    auto codePoints = StringView(m_glyphs[v[0]].codepoints).codePoints();
+    auto codePointsIterator = codePoints.begin();
+    ASSERT(codePointsIterator != codePoints.end());
+    ASSERT(codepoint = *codePointsIterator);
+#endif
+    return v[0];
+}
+
+void SVGToOTFFontConverter::appendLigatureSubtable(size_t subtableRecordLocation)
+{
+    typedef std::pair&lt;Vector&lt;Glyph, 3&gt;, Glyph&gt; LigaturePair;
+    Vector&lt;LigaturePair&gt; ligaturePairs;
+    for (Glyph glyphIndex = 0; glyphIndex &lt; m_glyphs.size(); ++glyphIndex) {
+        ligaturePairs.append(LigaturePair(Vector&lt;Glyph, 3&gt;(), glyphIndex));
+        Vector&lt;Glyph, 3&gt;&amp; ligatureGlyphs = ligaturePairs.last().first;
+        auto codePoints = StringView(m_glyphs[glyphIndex].codepoints).codePoints();
+        // FIXME: https://bugs.webkit.org/show_bug.cgi?id=138592 This needs to be done in codepoint space, not glyph space
+        for (auto codePoint : codePoints)
+            ligatureGlyphs.append(firstGlyph(glyphsForCodepoint(codePoint), codePoint));
+        if (ligatureGlyphs.size() &lt; 2)
+            ligaturePairs.removeLast();
+    }
+    if (ligaturePairs.size() &gt; std::numeric_limits&lt;uint16_t&gt;::max())
+        ligaturePairs.clear();
+    std::sort(ligaturePairs.begin(), ligaturePairs.end(), [](LigaturePair&amp; lhs, LigaturePair&amp; rhs) {
+        return lhs.first[0] &lt; rhs.first[0];
+    });
+    Vector&lt;size_t&gt; overlappingFirstGlyphSegmentLengths;
+    if (!ligaturePairs.isEmpty()) {
+        Glyph previousFirstGlyph = ligaturePairs[0].first[0];
+        size_t segmentStart = 0;
+        for (size_t i = 0; i &lt; ligaturePairs.size(); ++i) {
+            auto&amp; ligaturePair = ligaturePairs[i];
+            if (ligaturePair.first[0] != previousFirstGlyph) {
+                overlappingFirstGlyphSegmentLengths.append(i - segmentStart);
+                segmentStart = i;
+                previousFirstGlyph = ligaturePairs[0].first[0];
+            }
+        }
+        overlappingFirstGlyphSegmentLengths.append(ligaturePairs.size() - segmentStart);
+    }
+
+    overwrite16(subtableRecordLocation + 6, m_result.size() - subtableRecordLocation);
+    auto subtableLocation = m_result.size();
+    append16(1); // Format 1
+    append16(0); // Placeholder for offset to coverage table, relative to beginning of substitution table
+    append16(ligaturePairs.size()); // Number of LigatureSet tables
+    grow(overlappingFirstGlyphSegmentLengths.size() * 2); // Placeholder for offset to LigatureSet table
+
+    Vector&lt;size_t&gt; ligatureSetTableLocations;
+    for (size_t i = 0; i &lt; overlappingFirstGlyphSegmentLengths.size(); ++i) {
+        overwrite16(subtableLocation + 6 + 2 * i, m_result.size() - subtableLocation);
+        ligatureSetTableLocations.append(m_result.size());
+        append16(overlappingFirstGlyphSegmentLengths[i]); // LigatureCount
+        grow(overlappingFirstGlyphSegmentLengths[i] * 2); // Placeholder for offset to Ligature table
+    }
+    ASSERT(ligatureSetTableLocations.size() == overlappingFirstGlyphSegmentLengths.size());
+
+    size_t ligaturePairIndex = 0;
+    for (size_t i = 0; i &lt; overlappingFirstGlyphSegmentLengths.size(); ++i) {
+        for (size_t j = 0; j &lt; overlappingFirstGlyphSegmentLengths[i]; ++j) {
+            overwrite16(ligatureSetTableLocations[i] + 2 + 2 * j, m_result.size() - ligatureSetTableLocations[i]);
+            auto&amp; ligaturePair = ligaturePairs[ligaturePairIndex];
+            append16(ligaturePair.second);
+            append16(ligaturePair.first.size());
+            for (size_t k = 1; k &lt; ligaturePair.first.size(); ++k)
+                append16(ligaturePair.first[k]);
+            ++ligaturePairIndex;
+        }
+    }
+    ASSERT(ligaturePairIndex == ligaturePairs.size());
+
+    // Coverage table
+    overwrite16(subtableLocation + 2, m_result.size() - subtableLocation);
+    append16(1); // CoverageFormat
+    append16(ligatureSetTableLocations.size()); // GlyphCount
+    ligaturePairIndex = 0;
+    for (auto segmentLength : overlappingFirstGlyphSegmentLengths) {
+        auto&amp; ligaturePair = ligaturePairs[ligaturePairIndex];
+        ASSERT(ligaturePair.first.size() &gt; 1);
+        append16(ligaturePair.first[0]);
+        ligaturePairIndex += segmentLength;
+    }
+}
+
</ins><span class="cx"> void SVGToOTFFontConverter::appendArabicReplacementSubtable(size_t subtableRecordLocation, const char arabicForm[])
</span><span class="cx"> {
</span><span class="cx">     Vector&lt;std::pair&lt;Glyph, Glyph&gt;&gt; arabicFinalReplacements;
</span><span class="lines">@@ -642,6 +749,21 @@
</span><span class="cx">         append16(pair.first);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SVGToOTFFontConverter::appendScriptSubtable(unsigned featureCount)
+{
+    auto dfltScriptTableLocation = m_result.size();
+    append16(0); // Placeholder for offset of default language system table, relative to beginning of Script table
+    append16(0); // Number of following language system tables
+
+    // LangSys table
+    overwrite16(dfltScriptTableLocation, m_result.size() - dfltScriptTableLocation);
+    append16(0); // LookupOrder &quot;= NULL ... reserved&quot;
+    append16(0xFFFF); // No features are required
+    append16(featureCount); // Number of FeatureIndex values
+    for (uint16_t i = 0; i &lt; featureCount; ++i)
+        append16(m_featureCountGSUB++); // Features indices
+}
+
</ins><span class="cx"> void SVGToOTFFontConverter::appendGSUBTable()
</span><span class="cx"> {
</span><span class="cx">     auto tableLocation = m_result.size();
</span><span class="lines">@@ -655,22 +777,18 @@
</span><span class="cx"> 
</span><span class="cx">     // ScriptList
</span><span class="cx">     auto scriptListLocation = m_result.size();
</span><del>-    append16(1); // Number of ScriptRecords
</del><ins>+    append16(2); // Number of ScriptRecords
+    append32BitCode(&quot;DFLT&quot;);
+    append16(0); // Placeholder for offset of Script table, relative to beginning of ScriptList
</ins><span class="cx">     append32BitCode(&quot;arab&quot;);
</span><del>-    append16(m_result.size() + 2 - scriptListLocation); // Offset of Script table, relative to beginning of ScriptList
</del><ins>+    append16(0); // Placeholder for offset of Script table, relative to beginning of ScriptList
</ins><span class="cx"> 
</span><del>-    auto scriptTableLocation = m_result.size();
-    auto sizeOfEmptyScriptTable = 4;
-    append16(sizeOfEmptyScriptTable); // Offset of default language system table, relative to beginning of Script table
-    append16(0); // Number of following language system tables
-    ASSERT_UNUSED(scriptTableLocation, scriptTableLocation + sizeOfEmptyScriptTable == m_result.size());
</del><ins>+    overwrite16(scriptListLocation + 6, m_result.size() - scriptListLocation);
+    appendScriptSubtable(1);
+    overwrite16(scriptListLocation + 12, m_result.size() - scriptListLocation);
+    appendScriptSubtable(4);
</ins><span class="cx"> 
</span><del>-    append16(0); // LookupOrder &quot;= NULL ... reserved&quot;
-    append16(0); // First feature is required
-    uint16_t featureCount = 4;
-    append16(4); // FeatureCount
-    for (uint16_t i = 0; i &lt; featureCount; ++i)
-        append16(i); // Index of our feature into the FeatureList
</del><ins>+    unsigned featureCount = 5;
</ins><span class="cx"> 
</span><span class="cx">     // FeatureList
</span><span class="cx">     toFeatureList.populate();
</span><span class="lines">@@ -678,14 +796,16 @@
</span><span class="cx">     size_t featureListSize = 2 + 6 * featureCount;
</span><span class="cx">     size_t featureTableSize = 6;
</span><span class="cx">     append16(featureCount); // FeatureCount
</span><ins>+    append32BitCode(&quot;liga&quot;);
+    append16(featureListSize + featureTableSize * 0); // Offset of feature table, relative to beginning of FeatureList table
</ins><span class="cx">     append32BitCode(&quot;fina&quot;);
</span><del>-    append16(featureListSize + featureTableSize * 0); // Offset of feature table, relative to beginning of FeatureList table
</del><ins>+    append16(featureListSize + featureTableSize * 1); // Offset of feature table, relative to beginning of FeatureList table
</ins><span class="cx">     append32BitCode(&quot;medi&quot;);
</span><del>-    append16(featureListSize + featureTableSize * 1); // Offset of feature table, relative to beginning of FeatureList table
</del><ins>+    append16(featureListSize + featureTableSize * 2); // Offset of feature table, relative to beginning of FeatureList table
</ins><span class="cx">     append32BitCode(&quot;init&quot;);
</span><del>-    append16(featureListSize + featureTableSize * 2); // Offset of feature table, relative to beginning of FeatureList table
</del><ins>+    append16(featureListSize + featureTableSize * 3); // Offset of feature table, relative to beginning of FeatureList table
</ins><span class="cx">     append32BitCode(&quot;rlig&quot;);
</span><del>-    append16(featureListSize + featureTableSize * 3); // Offset of feature table, relative to beginning of FeatureList table
</del><ins>+    append16(featureListSize + featureTableSize * 4); // Offset of feature table, relative to beginning of FeatureList table
</ins><span class="cx">     ASSERT_UNUSED(featureListLocation, featureListLocation + featureListSize == m_result.size());
</span><span class="cx"> 
</span><span class="cx">     for (unsigned i = 0; i &lt; featureCount; ++i) {
</span><span class="lines">@@ -706,18 +826,29 @@
</span><span class="cx">     for (unsigned i = 0; i &lt; featureCount; ++i) {
</span><span class="cx">         subtableRecordLocations[i] = m_result.size();
</span><span class="cx">         overwrite16(lookupListLocation + 2 + 2 * i, m_result.size() - lookupListLocation);
</span><del>-        append16(i == 3 ? 3 : 1); // Type 1: &quot;Replace one glyph with one glyph&quot; / Type 3: &quot;Replace one glyph with one of many glyphs&quot;
</del><ins>+        switch (i) {
+        case 4:
+            append16(3); // Type 3: &quot;Replace one glyph with one of many glyphs&quot;
+            break;
+        case 0:
+            append16(4); // Type 4: &quot;Replace multiple glyphs with one glyph&quot;
+            break;
+        default:
+            append16(1); // Type 1: &quot;Replace one glyph with one glyph&quot;
+            break;
+        }
</ins><span class="cx">         append16(0); // LookupFlag
</span><span class="cx">         append16(1); // SubTableCount
</span><span class="cx">         append16(0); // Placeholder for offset to subtable, relative to beginning of Lookup table
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    appendArabicReplacementSubtable(subtableRecordLocations[0], &quot;terminal&quot;);
-    appendArabicReplacementSubtable(subtableRecordLocations[1], &quot;medial&quot;);
-    appendArabicReplacementSubtable(subtableRecordLocations[2], &quot;initial&quot;);
</del><ins>+    appendLigatureSubtable(subtableRecordLocations[0]);
+    appendArabicReplacementSubtable(subtableRecordLocations[1], &quot;terminal&quot;);
+    appendArabicReplacementSubtable(subtableRecordLocations[2], &quot;medial&quot;);
+    appendArabicReplacementSubtable(subtableRecordLocations[3], &quot;initial&quot;);
</ins><span class="cx"> 
</span><span class="cx">     // Manually append empty &quot;rlig&quot; subtable
</span><del>-    overwrite16(subtableRecordLocations[3] + 6, m_result.size() - subtableRecordLocations[3]);
</del><ins>+    overwrite16(subtableRecordLocations[4] + 6, m_result.size() - subtableRecordLocations[4]);
</ins><span class="cx">     append16(1); // Format 1
</span><span class="cx">     append16(6); // offset to coverage table, relative to beginning of substitution table
</span><span class="cx">     append16(0); // AlternateSetCount
</span><span class="lines">@@ -778,18 +909,26 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static String codepointToString(UChar32 codepoint)
+{
+    UChar buffer[2];
+    uint8_t length = 0;
+    UBool error = false;
+    U16_APPEND(buffer, length, 2, codepoint, error);
+    return error ? String() : String(buffer, length);
+}
+
+Vector&lt;Glyph, 1&gt; SVGToOTFFontConverter::glyphsForCodepoint(UChar32 codepoint) const
+{
+    return m_codepointsToIndicesMap.get(codepointToString(codepoint));
+}
+
</ins><span class="cx"> void SVGToOTFFontConverter::addCodepointRanges(const UnicodeRanges&amp; unicodeRanges, HashSet&lt;Glyph&gt;&amp; glyphSet) const
</span><span class="cx"> {
</span><span class="cx">     for (auto&amp; unicodeRange : unicodeRanges) {
</span><span class="cx">         for (auto codepoint = unicodeRange.first; codepoint &lt;= unicodeRange.second; ++codepoint) {
</span><del>-            UChar buffer[2];
-            uint8_t length = 0;
-            UBool error = false;
-            U16_APPEND(buffer, length, 2, codepoint, error);
-            if (!error) {
-                for (auto index : m_codepointsToIndicesMap.get(String(buffer, length)))
-                    glyphSet.add(index);
-            }
</del><ins>+            for (auto index : glyphsForCodepoint(codepoint))
+                glyphSet.add(index);
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="lines">@@ -892,7 +1031,8 @@
</span><span class="cx">         m_result.append(0);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void writeCFFEncodedNumber(Vector&lt;char&gt;&amp; vector, float number)
</del><ins>+template &lt;typename V&gt;
+static void writeCFFEncodedNumber(V&amp; vector, float number)
</ins><span class="cx"> {
</span><span class="cx">     vector.append(0xFF);
</span><span class="cx">     append32(vector, number * 0x10000);
</span><span class="lines">@@ -944,7 +1084,7 @@
</span><span class="cx"> 
</span><span class="cx">     virtual void moveTo(const FloatPoint&amp; targetPoint, bool closed, PathCoordinateMode mode) override
</span><span class="cx">     {
</span><del>-        if (closed &amp;&amp; m_cffData.size())
</del><ins>+        if (closed &amp;&amp; !m_cffData.isEmpty())
</ins><span class="cx">             closePath();
</span><span class="cx"> 
</span><span class="cx">         FloatPoint destination = mode == AbsoluteCoordinates ? targetPoint : m_current + targetPoint;
</span><span class="lines">@@ -1058,28 +1198,55 @@
</span><span class="cx">     m_glyphs.append(GlyphData(WTF::move(path), glyphElement, horizontalAdvance, verticalAdvance, m_boundingBox, codepoints));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SVGToOTFFontConverter::appendLigatureGlyphs()
+{
+    HashSet&lt;UChar32&gt; ligatureCodepoints;
+    HashSet&lt;UChar32&gt; nonLigatureCodepoints;
+    for (auto&amp; glyph : m_glyphs) {
+        auto codePoints = StringView(glyph.codepoints).codePoints();
+        auto codePointsIterator = codePoints.begin();
+        if (codePointsIterator == codePoints.end())
+            continue;
+        UChar32 codepoint = *codePointsIterator;
+        ++codePointsIterator;
+        if (codePointsIterator == codePoints.end())
+            nonLigatureCodepoints.add(codepoint);
+        else {
+            ligatureCodepoints.add(codepoint);
+            for (; codePointsIterator != codePoints.end(); ++codePointsIterator)
+                ligatureCodepoints.add(*codePointsIterator);
+        }
+    }
+
+    for (auto codepoint : nonLigatureCodepoints)
+        ligatureCodepoints.remove(codepoint);
+    for (auto codepoint : ligatureCodepoints) {
+        auto codepoints = codepointToString(codepoint);
+        if (!codepoints.isNull())
+            m_glyphs.append(GlyphData(Vector&lt;char, 17&gt;(m_emptyGlyphCharString), nullptr, m_unitsPerEm, m_unitsPerEm, FloatRect(), codepoints));
+    }
+}
+
</ins><span class="cx"> bool SVGToOTFFontConverter::compareCodepointsLexicographically(const GlyphData&amp; data1, const GlyphData&amp; data2)
</span><span class="cx"> {
</span><del>-    unsigned i1 = 0;
-    unsigned i2 = 0;
</del><ins>+    auto codePoints1 = StringView(data1.codepoints).codePoints();
+    auto codePoints2 = StringView(data2.codepoints).codePoints();
+    auto iterator1 = codePoints1.begin();
+    auto iterator2 = codePoints2.begin();
</ins><span class="cx">     unsigned length1 = data1.codepoints.length();
</span><span class="cx">     unsigned length2 = data2.codepoints.length();
</span><del>-    while (i1 &lt; length1 &amp;&amp; i2 &lt; length2) {
</del><ins>+    while (iterator1 != codePoints1.end() &amp;&amp; iterator2 != codePoints2.end()) {
</ins><span class="cx">         UChar32 codepoint1, codepoint2;
</span><del>-        if (data1.codepoints.is8Bit())
-            codepoint1 = data1.codepoints[i1++];
-        else
-            U16_NEXT(data1.codepoints.characters16(), i1, length1, codepoint1);
</del><ins>+        codepoint1 = *iterator1;
+        codepoint2 = *iterator2;
</ins><span class="cx"> 
</span><del>-        if (data2.codepoints.is8Bit())
-            codepoint2 = data2.codepoints[i2++];
-        else
-            U16_NEXT(data2.codepoints.characters16(), i2, length2, codepoint2);
-
</del><span class="cx">         if (codepoint1 &lt; codepoint2)
</span><span class="cx">             return true;
</span><span class="cx">         if (codepoint1 &gt; codepoint2)
</span><span class="cx">             return false;
</span><ins>+
+        ++iterator1;
+        ++iterator2;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (length1 == length2 &amp;&amp; data1.glyphElement
</span><span class="lines">@@ -1088,6 +1255,15 @@
</span><span class="cx">     return length1 &lt; length2;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void populateEmptyGlyphCharString(Vector&lt;char, 17&gt;&amp; o, unsigned unitsPerEm)
+{
+    writeCFFEncodedNumber(o, unitsPerEm);
+    writeCFFEncodedNumber(o, 0);
+    writeCFFEncodedNumber(o, 0);
+    o.append(rMoveTo);
+    o.append(endChar);
+}
+
</ins><span class="cx"> SVGToOTFFontConverter::SVGToOTFFontConverter(const SVGFontElement&amp; fontElement)
</span><span class="cx">     : m_fontElement(fontElement)
</span><span class="cx">     , m_fontFaceElement(childrenOfType&lt;SVGFontFaceElement&gt;(m_fontElement).first())
</span><span class="lines">@@ -1096,10 +1272,12 @@
</span><span class="cx">     , m_advanceHeightMax(0)
</span><span class="cx">     , m_minRightSideBearing(std::numeric_limits&lt;float&gt;::max())
</span><span class="cx">     , m_unitsPerEm(0)
</span><ins>+    , m_featureCountGSUB(0)
</ins><span class="cx">     , m_tablesAppendedCount(0)
</span><span class="cx">     , m_weight(5)
</span><span class="cx">     , m_italic(false)
</span><span class="cx"> {
</span><ins>+
</ins><span class="cx">     float defaultHorizontalAdvance = m_fontFaceElement ? m_fontFaceElement-&gt;horizontalAdvanceX() : 0;
</span><span class="cx">     float defaultVerticalAdvance = m_fontFaceElement ? m_fontFaceElement-&gt;verticalAdvanceY() : 0;
</span><span class="cx">     bool initialGlyph = true;
</span><span class="lines">@@ -1107,17 +1285,12 @@
</span><span class="cx">     if (m_fontFaceElement)
</span><span class="cx">         m_unitsPerEm = m_fontFaceElement-&gt;unitsPerEm();
</span><span class="cx"> 
</span><ins>+    populateEmptyGlyphCharString(m_emptyGlyphCharString, m_unitsPerEm);
+
</ins><span class="cx">     if (m_missingGlyphElement)
</span><span class="cx">         processGlyphElement(*m_missingGlyphElement, nullptr, defaultHorizontalAdvance, defaultVerticalAdvance, String(), initialGlyph);
</span><del>-    else {
-        Vector&lt;char&gt; notdefCharString;
-        writeCFFEncodedNumber(notdefCharString, m_unitsPerEm);
-        writeCFFEncodedNumber(notdefCharString, 0);
-        writeCFFEncodedNumber(notdefCharString, 0);
-        notdefCharString.append(rMoveTo);
-        notdefCharString.append(endChar);
-        m_glyphs.append(GlyphData(WTF::move(notdefCharString), nullptr, m_unitsPerEm, m_unitsPerEm, FloatRect(), String()));
-    }
</del><ins>+    else
+        m_glyphs.append(GlyphData(Vector&lt;char, 17&gt;(m_emptyGlyphCharString), nullptr, m_unitsPerEm, m_unitsPerEm, FloatRect(), String()));
</ins><span class="cx"> 
</span><span class="cx">     for (auto&amp; glyphElement : childrenOfType&lt;SVGGlyphElement&gt;(m_fontElement)) {
</span><span class="cx">         auto&amp; unicodeAttribute = glyphElement.fastGetAttribute(SVGNames::unicodeAttr);
</span><span class="lines">@@ -1125,6 +1298,8 @@
</span><span class="cx">             processGlyphElement(glyphElement, &amp;glyphElement, defaultHorizontalAdvance, defaultVerticalAdvance, unicodeAttribute, initialGlyph);
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    appendLigatureGlyphs();
+
</ins><span class="cx">     if (m_glyphs.size() &gt; std::numeric_limits&lt;Glyph&gt;::max()) {
</span><span class="cx">         m_glyphs.clear();
</span><span class="cx">         return;
</span></span></pre>
</div>
</div>

</body>
</html>