Cache glyph paths and share underline skipping code between all the ports
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 6 Nov 2018 04:35:50 +0000 (04:35 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 6 Nov 2018 04:35:50 +0000 (04:35 +0000)
https://bugs.webkit.org/show_bug.cgi?id=191239

Reviewed by Alex Christensen.

PerformanceTests:

Measures the performance of drawing a whole lot of underlines

* Layout/underline.html: Added.

Source/WebCore:

I was hoping that caching the glyph paths was going to be a performance progression,
but it turns out that the additional overhead of WebCore::Path compensated for it.
In total, the performance is the same (my testing says that this patch is a 1%
progression, but that's within the noise).

Because the ink skipping logic is now shared among all ports, Windows now gets it for
free.

Test: PerformanceTests/Layout/underline.html

* platform/graphics/Font.cpp:
(WebCore::Font::pathForGlyph const):
* platform/graphics/Font.h:
* platform/graphics/FontCascade.cpp:
(WebCore::computeUnderlineType):
(WebCore::GlyphIterationState::GlyphIterationState):
(WebCore::findIntersectionPoint):
(WebCore::updateX):
(WebCore::findPathIntersections):
(WebCore::GlyphToPathTranslator::GlyphToPathTranslator):
(WebCore::GlyphToPathTranslator::containsMorePaths):
(WebCore::GlyphToPathTranslator::path):
(WebCore::GlyphToPathTranslator::extents):
(WebCore::GlyphToPathTranslator::underlineType):
(WebCore::GlyphToPathTranslator::advance):
(WebCore::FontCascade::dashesForIntersectionsWithRect const):
* platform/graphics/FontCascade.h:
* platform/graphics/GlyphMetricsMap.h:
(WebCore::GlyphMetricsMap::existingMetricsForGlyph):
(WebCore::GlyphMetricsMap::GlyphMetricsPage::existingMetricsForGlyph const):
(WebCore::GlyphMetricsMap<std::optional<Path>>::unknownMetrics):
* platform/graphics/TextRun.h:
* platform/graphics/cairo/FontCairo.cpp:
(WebCore::Font::platformPathForGlyph const):
(WebCore::GlyphIterationState::GlyphIterationState): Deleted.
(WebCore::findIntersectionPoint): Deleted.
(WebCore::updateX): Deleted.
(WebCore::findPathIntersections): Deleted.
(): Deleted.
(WebCore::CairoGlyphToPathTranslator::path): Deleted.
(WebCore::CairoGlyphToPathTranslator::extents): Deleted.
(WebCore::CairoGlyphToPathTranslator::underlineType): Deleted.
(WebCore::CairoGlyphToPathTranslator::advance): Deleted.
(WebCore::FontCascade::dashesForIntersectionsWithRect const): Deleted.
* platform/graphics/cocoa/FontCascadeCocoa.mm:
(WebCore::GlyphIterationState::GlyphIterationState): Deleted.
(WebCore::findIntersectionPoint): Deleted.
(WebCore::updateX): Deleted.
(WebCore::findPathIntersections): Deleted.
(): Deleted.
(WebCore::MacGlyphToPathTranslator::path): Deleted.
(WebCore::MacGlyphToPathTranslator::extents): Deleted.
(WebCore::MacGlyphToPathTranslator::underlineType): Deleted.
(WebCore::MacGlyphToPathTranslator::advance): Deleted.
(WebCore::FontCascade::dashesForIntersectionsWithRect const): Deleted.
* platform/graphics/cocoa/FontCocoa.mm:
(WebCore::Font::platformPathForGlyph const):
* rendering/TextDecorationPainter.cpp:
(WebCore::drawSkipInkUnderline):
(WebCore::TextDecorationPainter::paintTextDecoration):

Source/WTF:

Remove CSS3_TEXT_DECORATION_SKIP_INK. It's now interoperable and part of the Web Platform.

* wtf/Platform.h:

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

17 files changed:
PerformanceTests/ChangeLog
Source/WTF/ChangeLog
Source/WTF/wtf/Platform.h
Source/WebCore/ChangeLog
Source/WebCore/PAL/pal/spi/win/CoreTextSPIWin.h
Source/WebCore/platform/graphics/Font.cpp
Source/WebCore/platform/graphics/Font.h
Source/WebCore/platform/graphics/FontCascade.cpp
Source/WebCore/platform/graphics/FontCascade.h
Source/WebCore/platform/graphics/GlyphMetricsMap.h
Source/WebCore/platform/graphics/TextRun.h
Source/WebCore/platform/graphics/cairo/FontCairo.cpp
Source/WebCore/platform/graphics/cocoa/FontCascadeCocoa.mm
Source/WebCore/platform/graphics/cocoa/FontCocoa.mm
Source/WebCore/platform/graphics/win/SimpleFontDataCGWin.cpp
Source/WebCore/platform/graphics/win/SimpleFontDataDirect2D.cpp
Source/WebCore/rendering/TextDecorationPainter.cpp

index deb1bc9..0455c8a 100644 (file)
@@ -1,3 +1,14 @@
+2018-11-05  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        Cache glyph paths and share underline skipping code between all the ports
+        https://bugs.webkit.org/show_bug.cgi?id=191239
+
+        Reviewed by Alex Christensen.
+
+        Measures the performance of drawing a whole lot of underlines
+
+        * Layout/underline.html: Added.
+
 2018-10-25  Saam Barati  <sbarati@apple.com>
 
         Check in corresponding C code in JetStream 2
index 774ee6e..248b6c7 100644 (file)
@@ -1,3 +1,14 @@
+2018-11-05  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        Cache glyph paths and share underline skipping code between all the ports
+        https://bugs.webkit.org/show_bug.cgi?id=191239
+
+        Reviewed by Alex Christensen.
+
+        Remove CSS3_TEXT_DECORATION_SKIP_INK. It's now interoperable and part of the Web Platform.
+
+        * wtf/Platform.h:
+
 2018-11-05  Dominik Infuehr  <dinfuehr@igalia.com>
 
         Enable LLInt on ARMv7/Linux
index 6e2837a..8b0d9d6 100644 (file)
 #if PLATFORM(GTK) || PLATFORM(WPE)
 #undef ENABLE_OPENTYPE_VERTICAL
 #define ENABLE_OPENTYPE_VERTICAL 1
-#define ENABLE_CSS3_TEXT_DECORATION_SKIP_INK 1
-#endif
-
-#if PLATFORM(COCOA)
-#define ENABLE_CSS3_TEXT_DECORATION_SKIP_INK 1
 #endif
 
 #if COMPILER(MSVC)
index 6296243..573a56f 100644 (file)
@@ -1,5 +1,73 @@
 2018-11-05  Myles C. Maxfield  <mmaxfield@apple.com>
 
+        Cache glyph paths and share underline skipping code between all the ports
+        https://bugs.webkit.org/show_bug.cgi?id=191239
+
+        Reviewed by Alex Christensen.
+
+        I was hoping that caching the glyph paths was going to be a performance progression,
+        but it turns out that the additional overhead of WebCore::Path compensated for it.
+        In total, the performance is the same (my testing says that this patch is a 1%
+        progression, but that's within the noise).
+
+        Because the ink skipping logic is now shared among all ports, Windows now gets it for
+        free.
+
+        Test: PerformanceTests/Layout/underline.html
+
+        * platform/graphics/Font.cpp:
+        (WebCore::Font::pathForGlyph const):
+        * platform/graphics/Font.h:
+        * platform/graphics/FontCascade.cpp:
+        (WebCore::computeUnderlineType):
+        (WebCore::GlyphIterationState::GlyphIterationState):
+        (WebCore::findIntersectionPoint):
+        (WebCore::updateX):
+        (WebCore::findPathIntersections):
+        (WebCore::GlyphToPathTranslator::GlyphToPathTranslator):
+        (WebCore::GlyphToPathTranslator::containsMorePaths):
+        (WebCore::GlyphToPathTranslator::path):
+        (WebCore::GlyphToPathTranslator::extents):
+        (WebCore::GlyphToPathTranslator::underlineType):
+        (WebCore::GlyphToPathTranslator::advance):
+        (WebCore::FontCascade::dashesForIntersectionsWithRect const):
+        * platform/graphics/FontCascade.h:
+        * platform/graphics/GlyphMetricsMap.h:
+        (WebCore::GlyphMetricsMap::existingMetricsForGlyph):
+        (WebCore::GlyphMetricsMap::GlyphMetricsPage::existingMetricsForGlyph const):
+        (WebCore::GlyphMetricsMap<std::optional<Path>>::unknownMetrics):
+        * platform/graphics/TextRun.h:
+        * platform/graphics/cairo/FontCairo.cpp:
+        (WebCore::Font::platformPathForGlyph const):
+        (WebCore::GlyphIterationState::GlyphIterationState): Deleted.
+        (WebCore::findIntersectionPoint): Deleted.
+        (WebCore::updateX): Deleted.
+        (WebCore::findPathIntersections): Deleted.
+        (): Deleted.
+        (WebCore::CairoGlyphToPathTranslator::path): Deleted.
+        (WebCore::CairoGlyphToPathTranslator::extents): Deleted.
+        (WebCore::CairoGlyphToPathTranslator::underlineType): Deleted.
+        (WebCore::CairoGlyphToPathTranslator::advance): Deleted.
+        (WebCore::FontCascade::dashesForIntersectionsWithRect const): Deleted.
+        * platform/graphics/cocoa/FontCascadeCocoa.mm:
+        (WebCore::GlyphIterationState::GlyphIterationState): Deleted.
+        (WebCore::findIntersectionPoint): Deleted.
+        (WebCore::updateX): Deleted.
+        (WebCore::findPathIntersections): Deleted.
+        (): Deleted.
+        (WebCore::MacGlyphToPathTranslator::path): Deleted.
+        (WebCore::MacGlyphToPathTranslator::extents): Deleted.
+        (WebCore::MacGlyphToPathTranslator::underlineType): Deleted.
+        (WebCore::MacGlyphToPathTranslator::advance): Deleted.
+        (WebCore::FontCascade::dashesForIntersectionsWithRect const): Deleted.
+        * platform/graphics/cocoa/FontCocoa.mm:
+        (WebCore::Font::platformPathForGlyph const):
+        * rendering/TextDecorationPainter.cpp:
+        (WebCore::drawSkipInkUnderline):
+        (WebCore::TextDecorationPainter::paintTextDecoration):
+
+2018-11-05  Myles C. Maxfield  <mmaxfield@apple.com>
+
         Clean up text decoration drawing code
         https://bugs.webkit.org/show_bug.cgi?id=191245
 
index 31dcf70..d28b16b 100644 (file)
@@ -57,6 +57,7 @@ CTLineRef CTLineCreateWithAttributedString(CFAttributedStringRef);
 void CTLineDraw(CTLineRef, CGContextRef);
 CFDataRef CTFontCopyTable(CTFontRef, CTFontTableTag, CTFontTableOptions);
 CFArrayRef CTFontCopyAvailableTables(CTFontRef, CTFontTableOptions);
+CGPathRef CTFontCreatePathForGlyph(CTFontRef, CGGlyph, const CGAffineTransform*);
 
 WTF_EXTERN_C_END
 
index 0eacd19..2ddc79c 100644 (file)
@@ -663,4 +663,15 @@ bool Font::canRenderCombiningCharacterSequence(const UChar* characters, size_t l
     }
     return true;
 }
+
+// Don't store the result of this! The hash map is free to rehash at any point, leaving this reference dangling.
+const Path& Font::pathForGlyph(Glyph glyph) const
+{
+    if (const auto& path = m_glyphPathMap.existingMetricsForGlyph(glyph))
+        return *path;
+    auto path = platformPathForGlyph(glyph);
+    m_glyphPathMap.setMetricsForGlyph(glyph, path);
+    return *m_glyphPathMap.existingMetricsForGlyph(glyph);
+}
+
 } // namespace WebCore
index 23a62d1..cd1b955 100644 (file)
@@ -146,8 +146,10 @@ public:
 
     FloatRect boundsForGlyph(Glyph) const;
     float widthForGlyph(Glyph) const;
+    const Path& pathForGlyph(Glyph) const; // Don't store the result of this! The hash map is free to rehash at any point, leaving this reference dangling.
     FloatRect platformBoundsForGlyph(Glyph) const;
     float platformWidthForGlyph(Glyph) const;
+    Path platformPathForGlyph(Glyph) const;
 
     float spaceWidth() const { return m_spaceWidth; }
     float adjustedSpaceWidth() const { return m_adjustedSpaceWidth; }
@@ -253,6 +255,7 @@ private:
     mutable HashMap<unsigned, RefPtr<GlyphPage>> m_glyphPages;
     mutable std::unique_ptr<GlyphMetricsMap<FloatRect>> m_glyphToBoundsMap;
     mutable GlyphMetricsMap<float> m_glyphToWidthMap;
+    mutable GlyphMetricsMap<std::optional<Path>> m_glyphPathMap;
     mutable BitVector m_codePointSupport;
 
     mutable RefPtr<OpenTypeMathData> m_mathData;
index 7438213..db4ea6e 100644 (file)
@@ -1237,18 +1237,25 @@ bool FontCascade::isLoadingCustomFonts() const
 {
     return m_fonts && m_fonts->isLoadingCustomFonts();
 }
+
+enum class GlyphUnderlineType : uint8_t {
+    SkipDescenders,
+    SkipGlyph,
+    DrawOverGlyph
+};
     
-GlyphToPathTranslator::GlyphUnderlineType computeUnderlineType(const TextRun& textRun, const GlyphBuffer& glyphBuffer, unsigned index)
+static GlyphUnderlineType computeUnderlineType(const TextRun& textRun, const GlyphBuffer& glyphBuffer, unsigned index)
 {
     // In general, we want to skip descenders. However, skipping descenders on CJK characters leads to undesirable renderings,
     // so we want to draw through CJK characters (on a character-by-character basis).
+    // FIXME: The CSS spec says this should instead be done by the user-agent stylesheet using the lang= attribute.
     UChar32 baseCharacter;
     unsigned offsetInString = glyphBuffer.offsetInString(index);
 
     if (offsetInString == GlyphBuffer::noOffset || offsetInString >= textRun.length()) {
         // We have no idea which character spawned this glyph. Bail.
         ASSERT_WITH_SECURITY_IMPLICATION(offsetInString < textRun.length());
-        return GlyphToPathTranslator::GlyphUnderlineType::DrawOverGlyph;
+        return GlyphUnderlineType::DrawOverGlyph;
     }
     
     if (textRun.is8Bit())
@@ -1285,9 +1292,9 @@ GlyphToPathTranslator::GlyphUnderlineType computeUnderlineType(const TextRun& te
     case UBLOCK_HANGUL_SYLLABLES:
     case UBLOCK_HANGUL_JAMO_EXTENDED_A:
     case UBLOCK_HANGUL_JAMO_EXTENDED_B:
-        return GlyphToPathTranslator::GlyphUnderlineType::DrawOverGlyph;
+        return GlyphUnderlineType::DrawOverGlyph;
     default:
-        return GlyphToPathTranslator::GlyphUnderlineType::SkipDescenders;
+        return GlyphUnderlineType::SkipDescenders;
     }
 }
 
@@ -1658,4 +1665,178 @@ void FontCascade::drawEmphasisMarksForComplexText(GraphicsContext& context, cons
     drawEmphasisMarks(context, glyphBuffer, mark, FloatPoint(point.x() + initialAdvance, point.y()));
 }
 
+struct GlyphIterationState {
+    FloatPoint startingPoint;
+    FloatPoint currentPoint;
+    float y1;
+    float y2;
+    float minX;
+    float maxX;
+};
+
+static std::optional<float> findIntersectionPoint(float y, FloatPoint p1, FloatPoint p2)
+{
+    if ((p1.y() < y && p2.y() > y) || (p1.y() > y && p2.y() < y))
+        return p1.x() + (y - p1.y()) * (p2.x() - p1.x()) / (p2.y() - p1.y());
+    return std::nullopt;
+}
+
+static void updateX(GlyphIterationState& state, float x)
+{
+    state.minX = std::min(state.minX, x);
+    state.maxX = std::max(state.maxX, x);
+}
+
+// This function is called by CGPathApply and is therefore invoked for each
+// contour in a glyph. This function models each contours as a straight line
+// and calculates the intersections between each pseudo-contour and
+// two horizontal lines (the upper and lower bounds of an underline) found in
+// GlyphIterationState::y1 and GlyphIterationState::y2. It keeps track of the
+// leftmost and rightmost intersection in GlyphIterationState::minX and
+// GlyphIterationState::maxX.
+static void findPathIntersections(GlyphIterationState& state, const PathElement& element)
+{
+    bool doIntersection = false;
+    FloatPoint point = FloatPoint();
+    switch (element.type) {
+    case PathElementMoveToPoint:
+        state.startingPoint = element.points[0];
+        state.currentPoint = element.points[0];
+        break;
+    case PathElementAddLineToPoint:
+        doIntersection = true;
+        point = element.points[0];
+        break;
+    case PathElementAddQuadCurveToPoint:
+        doIntersection = true;
+        point = element.points[1];
+        break;
+    case PathElementAddCurveToPoint:
+        doIntersection = true;
+        point = element.points[2];
+        break;
+    case PathElementCloseSubpath:
+        doIntersection = true;
+        point = state.startingPoint;
+        break;
+    }
+    if (!doIntersection)
+        return;
+    if (auto intersectionPoint = findIntersectionPoint(state.y1, state.currentPoint, point))
+        updateX(state, *intersectionPoint);
+    if (auto intersectionPoint = findIntersectionPoint(state.y2, state.currentPoint, point))
+        updateX(state, *intersectionPoint);
+    if ((state.currentPoint.y() >= state.y1 && state.currentPoint.y() <= state.y2)
+        || (state.currentPoint.y() <= state.y1 && state.currentPoint.y() >= state.y2))
+        updateX(state, state.currentPoint.x());
+    state.currentPoint = point;
+}
+
+class GlyphToPathTranslator {
+public:
+    GlyphToPathTranslator(const TextRun& textRun, const GlyphBuffer& glyphBuffer, const FloatPoint& textOrigin)
+        : m_index(0)
+        , m_textRun(textRun)
+        , m_glyphBuffer(glyphBuffer)
+        , m_fontData(glyphBuffer.fontAt(m_index))
+        , m_translation(AffineTransform::translation(textOrigin.x(), textOrigin.y()).scale(1, -1))
+    {
+    }
+
+    bool containsMorePaths() { return m_index != m_glyphBuffer.size(); }
+    Path path();
+    std::pair<float, float> extents();
+    GlyphUnderlineType underlineType();
+    void advance();
+
+private:
+    unsigned m_index;
+    const TextRun& m_textRun;
+    const GlyphBuffer& m_glyphBuffer;
+    const Font* m_fontData;
+    AffineTransform m_translation;
+};
+
+Path GlyphToPathTranslator::path()
+{
+    Path path = m_fontData->pathForGlyph(m_glyphBuffer.glyphAt(m_index));
+    path.transform(m_translation);
+    return path;
+}
+
+std::pair<float, float> GlyphToPathTranslator::extents()
+{
+    auto beginning = m_translation.mapPoint(FloatPoint(0, 0));
+    auto advance = m_glyphBuffer.advanceAt(m_index);
+    auto end = m_translation.mapSize(FloatSize(advance.width(), advance.height()));
+    return std::make_pair(beginning.x(), beginning.x() + end.width());
+}
+
+auto GlyphToPathTranslator::underlineType() -> GlyphUnderlineType
+{
+    return computeUnderlineType(m_textRun, m_glyphBuffer, m_index);
+}
+
+void GlyphToPathTranslator::advance()
+{
+    GlyphBufferAdvance advance = m_glyphBuffer.advanceAt(m_index);
+    m_translation.translate(FloatSize(advance.width(), advance.height()));
+    ++m_index;
+    if (m_index < m_glyphBuffer.size())
+        m_fontData = m_glyphBuffer.fontAt(m_index);
+}
+
+DashArray FontCascade::dashesForIntersectionsWithRect(const TextRun& run, const FloatPoint& textOrigin, const FloatRect& lineExtents) const
+{
+    if (isLoadingCustomFonts())
+        return DashArray();
+
+    GlyphBuffer glyphBuffer;
+    glyphBuffer.saveOffsetsInString();
+    float deltaX;
+    if (codePath(run) != FontCascade::Complex)
+        deltaX = getGlyphsAndAdvancesForSimpleText(run, 0, run.length(), glyphBuffer);
+    else
+        deltaX = getGlyphsAndAdvancesForComplexText(run, 0, run.length(), glyphBuffer);
+
+    if (!glyphBuffer.size())
+        return DashArray();
+
+    FloatPoint origin = FloatPoint(textOrigin.x() + deltaX, textOrigin.y());
+    GlyphToPathTranslator translator(run, glyphBuffer, origin);
+    DashArray result;
+    for (unsigned index = 0; translator.containsMorePaths(); ++index, translator.advance()) {
+        GlyphIterationState info = { FloatPoint(0, 0), FloatPoint(0, 0), lineExtents.y(), lineExtents.y() + lineExtents.height(), lineExtents.x() + lineExtents.width(), lineExtents.x() };
+        const Font* localFont = glyphBuffer.fontAt(index);
+        if (!localFont) {
+            // The advances will get all messed up if we do anything other than bail here.
+            result.clear();
+            break;
+        }
+        switch (translator.underlineType()) {
+        case GlyphUnderlineType::SkipDescenders: {
+            Path path = translator.path();
+            path.apply([&](const PathElement& element) {
+                findPathIntersections(info, element);
+            });
+            if (info.minX < info.maxX) {
+                result.append(info.minX - lineExtents.x());
+                result.append(info.maxX - lineExtents.x());
+            }
+            break;
+        }
+        case GlyphUnderlineType::SkipGlyph: {
+            std::pair<float, float> extents = translator.extents();
+            result.append(extents.first - lineExtents.x());
+            result.append(extents.second - lineExtents.x());
+            break;
+        }
+        case GlyphUnderlineType::DrawOverGlyph:
+            // Nothing to do
+            break;
+        }
+    }
+    return result;
+}
+
 }
index 970698a..76632cf 100644 (file)
@@ -81,18 +81,6 @@ struct GlyphOverflow {
     bool computeBounds { false };
 };
 
-class GlyphToPathTranslator {
-public:
-    enum class GlyphUnderlineType {SkipDescenders, SkipGlyph, DrawOverGlyph};
-    virtual bool containsMorePaths() = 0;
-    virtual Path path() = 0;
-    virtual std::pair<float, float> extents() = 0;
-    virtual GlyphUnderlineType underlineType() = 0;
-    virtual void advance() = 0;
-    virtual ~GlyphToPathTranslator() = default;
-};
-GlyphToPathTranslator::GlyphUnderlineType computeUnderlineType(const TextRun&, const GlyphBuffer&, unsigned index);
-
 class TextLayoutDeleter {
 public:
     void operator()(TextLayout*) const;
index 4f0f58a..3c92b76 100644 (file)
@@ -30,6 +30,7 @@
 #define GlyphMetricsMap_h
 
 #include "Glyph.h"
+#include "Path.h"
 #include <array>
 #include <wtf/HashMap.h>
 
@@ -45,6 +46,11 @@ public:
         return locatePage(glyph / GlyphMetricsPage::size).metricsForGlyph(glyph);
     }
 
+    const T& existingMetricsForGlyph(Glyph glyph)
+    {
+        return locatePage(glyph / GlyphMetricsPage::size).existingMetricsForGlyph(glyph);
+    }
+
     void setMetricsForGlyph(Glyph glyph, const T& metrics)
     {
         locatePage(glyph / GlyphMetricsPage::size).setMetricsForGlyph(glyph, metrics);
@@ -68,6 +74,7 @@ private:
         }
 
         T metricsForGlyph(Glyph glyph) const { return m_metrics[glyph % size]; }
+        const T& existingMetricsForGlyph(Glyph glyph) const { return m_metrics[glyph % size]; }
         void setMetricsForGlyph(Glyph glyph, const T& metrics)
         {
             setMetricsForIndex(glyph % size, metrics);
@@ -108,6 +115,11 @@ template<> inline FloatRect GlyphMetricsMap<FloatRect>::unknownMetrics()
     return FloatRect(0, 0, cGlyphSizeUnknown, cGlyphSizeUnknown);
 }
 
+template<> inline std::optional<Path> GlyphMetricsMap<std::optional<Path>>::unknownMetrics()
+{
+    return std::nullopt;
+}
+
 template<class T> typename GlyphMetricsMap<T>::GlyphMetricsPage& GlyphMetricsMap<T>::locatePageSlowCase(unsigned pageNumber)
 {
     if (!pageNumber) {
index e2bb4be..80d2817 100644 (file)
@@ -35,7 +35,6 @@ class FloatRect;
 class FontCascade;
 class GraphicsContext;
 class GlyphBuffer;
-class GlyphToPathTranslator;
 class Font;
 
 struct GlyphData;
index 286bc6d..c942b49 100644 (file)
@@ -78,198 +78,23 @@ void FontCascade::drawGlyphs(GraphicsContext& context, const Font& font, const G
         state.textDrawingMode, state.strokeThickness, state.shadowOffset, state.shadowColor);
 }
 
-#if ENABLE(CSS3_TEXT_DECORATION_SKIP_INK)
-struct GlyphIterationState {
-    GlyphIterationState(FloatPoint startingPoint, FloatPoint currentPoint, float centerOfLine, float minX, float maxX)
-        : startingPoint(startingPoint)
-        , currentPoint(currentPoint)
-        , centerOfLine(centerOfLine)
-        , minX(minX)
-        , maxX(maxX)
-    {
-    }
-    FloatPoint startingPoint;
-    FloatPoint currentPoint;
-    float centerOfLine;
-    float minX;
-    float maxX;
-};
-
-static bool findIntersectionPoint(float y, FloatPoint p1, FloatPoint p2, float& x)
-{
-    x = p1.x() + (y - p1.y()) * (p2.x() - p1.x()) / (p2.y() - p1.y());
-    return (p1.y() < y && p2.y() > y) || (p1.y() > y && p2.y() < y);
-}
-
-static void updateX(GlyphIterationState& state, float x)
-{
-    state.minX = std::min(state.minX, x);
-    state.maxX = std::max(state.maxX, x);
-}
-
-// This function is called by Path::apply and is therefore invoked for each contour in a glyph. This
-// function models each contours as a straight line and calculates the intersections between each
-// pseudo-contour and the vertical center of the underline found in GlyphIterationState::centerOfLine.
-// It keeps track of the leftmost and rightmost intersection in  GlyphIterationState::minX and 
-// GlyphIterationState::maxX.
-static void findPathIntersections(GlyphIterationState& state, const PathElement& element)
-{
-    bool doIntersection = false;
-    FloatPoint point = FloatPoint();
-    switch (element.type) {
-    case PathElementMoveToPoint:
-        state.startingPoint = element.points[0];
-        state.currentPoint = element.points[0];
-        break;
-    case PathElementAddLineToPoint:
-        doIntersection = true;
-        point = element.points[0];
-        break;
-    case PathElementAddQuadCurveToPoint:
-        doIntersection = true;
-        point = element.points[1];
-        break;
-    case PathElementAddCurveToPoint:
-        doIntersection = true;
-        point = element.points[2];
-        break;
-    case PathElementCloseSubpath:
-        doIntersection = true;
-        point = state.startingPoint;
-        break;
-    }
-
-    if (!doIntersection)
-        return;
-
-    float x;
-    if (findIntersectionPoint(state.centerOfLine, state.currentPoint, point, x))
-        updateX(state, x);
-
-    state.currentPoint = point;
-}
-
-class CairoGlyphToPathTranslator final : public GlyphToPathTranslator {
-public:
-    CairoGlyphToPathTranslator(const TextRun& textRun, const GlyphBuffer& glyphBuffer, const FloatPoint& textOrigin)
-        : m_index(0)
-        , m_textRun(textRun)
-        , m_glyphBuffer(glyphBuffer)
-        , m_fontData(glyphBuffer.fontAt(m_index))
-        , m_translation(AffineTransform().translate(textOrigin.x(), textOrigin.y()))
-    {
-    }
-
-    bool containsMorePaths() final { return m_index != m_glyphBuffer.size(); }
-    Path path() final;
-    std::pair<float, float> extents() final;
-    GlyphUnderlineType underlineType() final;
-    void advance() final;
-
-private:
-    unsigned m_index;
-    const TextRun& m_textRun;
-    const GlyphBuffer& m_glyphBuffer;
-    const Font* m_fontData;
-    AffineTransform m_translation;
-};
-
-Path CairoGlyphToPathTranslator::path()
+Path Font::platformPathForGlyph(Glyph glyph) const
 {
     Path path;
     path.ensurePlatformPath();
 
-    cairo_glyph_t cairoGlyph = { m_glyphBuffer.glyphAt(m_index), 0, 0 };
-    cairo_set_scaled_font(path.platformPath()->context(), m_fontData->platformData().scaledFont());
+    cairo_glyph_t cairoGlyph = { glyph, 0, 0 };
+    cairo_set_scaled_font(path.platformPath()->context(), platformData().scaledFont());
     cairo_glyph_path(path.platformPath()->context(), &cairoGlyph, 1);
 
-    float syntheticBoldOffset = m_fontData->syntheticBoldOffset();
+    float syntheticBoldOffset = this->syntheticBoldOffset();
     if (syntheticBoldOffset) {
         cairo_translate(path.platformPath()->context(), syntheticBoldOffset, 0);
         cairo_glyph_path(path.platformPath()->context(), &cairoGlyph, 1);
     }
-
-    path.transform(m_translation);
     return path;
 }
 
-std::pair<float, float> CairoGlyphToPathTranslator::extents()
-{
-    FloatPoint beginning = m_translation.mapPoint(FloatPoint());
-    FloatSize end = m_translation.mapSize(m_glyphBuffer.advanceAt(m_index));
-    return std::make_pair(static_cast<float>(beginning.x()), static_cast<float>(beginning.x() + end.width()));
-}
-
-GlyphToPathTranslator::GlyphUnderlineType CairoGlyphToPathTranslator::underlineType()
-{
-    return computeUnderlineType(m_textRun, m_glyphBuffer, m_index);
-}
-
-void CairoGlyphToPathTranslator::advance()
-{
-    GlyphBufferAdvance advance = m_glyphBuffer.advanceAt(m_index);
-    m_translation = m_translation.translate(advance.width(), advance.height());
-    ++m_index;
-    if (m_index < m_glyphBuffer.size())
-        m_fontData = m_glyphBuffer.fontAt(m_index);
-}
-
-DashArray FontCascade::dashesForIntersectionsWithRect(const TextRun& run, const FloatPoint& textOrigin, const FloatRect& lineExtents) const
-{
-    if (isLoadingCustomFonts())
-        return DashArray();
-
-    GlyphBuffer glyphBuffer;
-    glyphBuffer.saveOffsetsInString();
-    float deltaX;
-    if (codePath(run) != FontCascade::Complex)
-        deltaX = getGlyphsAndAdvancesForSimpleText(run, 0, run.length(), glyphBuffer);
-    else
-        deltaX = getGlyphsAndAdvancesForComplexText(run, 0, run.length(), glyphBuffer);
-
-    if (!glyphBuffer.size())
-        return DashArray();
-
-    // FIXME: Handle SVG + non-SVG interleaved runs. https://bugs.webkit.org/show_bug.cgi?id=133778
-    FloatPoint origin = FloatPoint(textOrigin.x() + deltaX, textOrigin.y());
-    CairoGlyphToPathTranslator translator(run, glyphBuffer, origin);
-    DashArray result;
-    for (int index = 0; translator.containsMorePaths(); ++index, translator.advance()) {
-        float centerOfLine = lineExtents.y() + (lineExtents.height() / 2);
-        GlyphIterationState info = GlyphIterationState(FloatPoint(), FloatPoint(), centerOfLine, lineExtents.x() + lineExtents.width(), lineExtents.x());
-        const Font* localFontData = glyphBuffer.fontAt(index);
-        if (!localFontData) {
-            // The advances will get all messed up if we do anything other than bail here.
-            result.clear();
-            break;
-        }
-        switch (translator.underlineType()) {
-        case GlyphToPathTranslator::GlyphUnderlineType::SkipDescenders: {
-            Path path = translator.path();
-            path.apply([&info](const PathElement& pathElement) {
-                findPathIntersections(info, pathElement);
-            });
-            if (info.minX < info.maxX) {
-                result.append(info.minX - lineExtents.x());
-                result.append(info.maxX - lineExtents.x());
-            }
-            break;
-        }
-        case GlyphToPathTranslator::GlyphUnderlineType::SkipGlyph: {
-            std::pair<float, float> extents = translator.extents();
-            result.append(extents.first - lineExtents.x());
-            result.append(extents.second - lineExtents.x());
-            break;
-        }
-        case GlyphToPathTranslator::GlyphUnderlineType::DrawOverGlyph:
-            // Nothing to do
-            break;
-        }
-    }
-    return result;
-}
-#endif // ENABLE(CSS3_TEXT_DECORATION_SKIP_INK)
-
 } // namespace WebCore
 
 #endif // USE(CAIRO)
index 4da5ed5..4a7f538 100644 (file)
@@ -303,188 +303,6 @@ void FontCascade::drawGlyphs(GraphicsContext& context, const Font& font, const G
 #endif
 }
 
-#if ENABLE(CSS3_TEXT_DECORATION_SKIP_INK)
-struct GlyphIterationState {
-    GlyphIterationState(CGPoint startingPoint, CGPoint currentPoint, CGFloat y1, CGFloat y2, CGFloat minX, CGFloat maxX)
-        : startingPoint(startingPoint)
-        , currentPoint(currentPoint)
-        , y1(y1)
-        , y2(y2)
-        , minX(minX)
-        , maxX(maxX)
-    {
-    }
-    CGPoint startingPoint;
-    CGPoint currentPoint;
-    CGFloat y1;
-    CGFloat y2;
-    CGFloat minX;
-    CGFloat maxX;
-};
-
-static bool findIntersectionPoint(float y, CGPoint p1, CGPoint p2, CGFloat& x)
-{
-    x = p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
-    return (p1.y < y && p2.y > y) || (p1.y > y && p2.y < y);
-}
-
-static void updateX(GlyphIterationState& state, CGFloat x)
-{
-    state.minX = std::min(state.minX, x);
-    state.maxX = std::max(state.maxX, x);
-}
-
-// This function is called by CGPathApply and is therefore invoked for each
-// contour in a glyph. This function models each contours as a straight line
-// and calculates the intersections between each pseudo-contour and
-// two horizontal lines (the upper and lower bounds of an underline) found in
-// GlyphIterationState::y1 and GlyphIterationState::y2. It keeps track of the
-// leftmost and rightmost intersection in GlyphIterationState::minX and
-// GlyphIterationState::maxX.
-static void findPathIntersections(void* stateAsVoidPointer, const CGPathElement* e)
-{
-    auto& state = *static_cast<GlyphIterationState*>(stateAsVoidPointer);
-    bool doIntersection = false;
-    CGPoint point = CGPointZero;
-    switch (e->type) {
-    case kCGPathElementMoveToPoint:
-        state.startingPoint = e->points[0];
-        state.currentPoint = e->points[0];
-        break;
-    case kCGPathElementAddLineToPoint:
-        doIntersection = true;
-        point = e->points[0];
-        break;
-    case kCGPathElementAddQuadCurveToPoint:
-        doIntersection = true;
-        point = e->points[1];
-        break;
-    case kCGPathElementAddCurveToPoint:
-        doIntersection = true;
-        point = e->points[2];
-        break;
-    case kCGPathElementCloseSubpath:
-        doIntersection = true;
-        point = state.startingPoint;
-        break;
-    }
-    if (!doIntersection)
-        return;
-    CGFloat x;
-    if (findIntersectionPoint(state.y1, state.currentPoint, point, x))
-        updateX(state, x);
-    if (findIntersectionPoint(state.y2, state.currentPoint, point, x))
-        updateX(state, x);
-    if ((state.currentPoint.y >= state.y1 && state.currentPoint.y <= state.y2)
-        || (state.currentPoint.y <= state.y1 && state.currentPoint.y >= state.y2))
-        updateX(state, state.currentPoint.x);
-    state.currentPoint = point;
-}
-
-class MacGlyphToPathTranslator final : public GlyphToPathTranslator {
-public:
-    MacGlyphToPathTranslator(const TextRun& textRun, const GlyphBuffer& glyphBuffer, const FloatPoint& textOrigin)
-        : m_index(0)
-        , m_textRun(textRun)
-        , m_glyphBuffer(glyphBuffer)
-        , m_fontData(glyphBuffer.fontAt(m_index))
-        , m_translation(CGAffineTransformScale(CGAffineTransformMakeTranslation(textOrigin.x(), textOrigin.y()), 1, -1))
-    {
-    }
-    bool containsMorePaths() final { return m_index != m_glyphBuffer.size(); }
-    Path path() final;
-    std::pair<float, float> extents() final;
-    GlyphUnderlineType underlineType() final;
-    void advance() final;
-
-private:
-    unsigned m_index;
-    const TextRun& m_textRun;
-    const GlyphBuffer& m_glyphBuffer;
-    const Font* m_fontData;
-    CGAffineTransform m_translation;
-};
-
-Path MacGlyphToPathTranslator::path()
-{
-    RetainPtr<CGPathRef> result = adoptCF(CTFontCreatePathForGlyph(m_fontData->platformData().ctFont(), m_glyphBuffer.glyphAt(m_index), &m_translation));
-    return adoptCF(CGPathCreateMutableCopy(result.get()));
-}
-
-std::pair<float, float> MacGlyphToPathTranslator::extents()
-{
-    CGPoint beginning = CGPointApplyAffineTransform(CGPointMake(0, 0), m_translation);
-    CGSize end = CGSizeApplyAffineTransform(m_glyphBuffer.advanceAt(m_index), m_translation);
-    return std::make_pair(static_cast<float>(beginning.x), static_cast<float>(beginning.x + end.width));
-}
-
-auto MacGlyphToPathTranslator::underlineType() -> GlyphUnderlineType
-{
-    return computeUnderlineType(m_textRun, m_glyphBuffer, m_index);
-}
-
-void MacGlyphToPathTranslator::advance()
-{
-    GlyphBufferAdvance advance = m_glyphBuffer.advanceAt(m_index);
-    m_translation = CGAffineTransformTranslate(m_translation, advance.width(), advance.height());
-    ++m_index;
-    if (m_index < m_glyphBuffer.size())
-        m_fontData = m_glyphBuffer.fontAt(m_index);
-}
-
-DashArray FontCascade::dashesForIntersectionsWithRect(const TextRun& run, const FloatPoint& textOrigin, const FloatRect& lineExtents) const
-{
-    if (isLoadingCustomFonts())
-        return DashArray();
-
-    GlyphBuffer glyphBuffer;
-    glyphBuffer.saveOffsetsInString();
-    float deltaX;
-    if (codePath(run) != FontCascade::Complex)
-        deltaX = getGlyphsAndAdvancesForSimpleText(run, 0, run.length(), glyphBuffer);
-    else
-        deltaX = getGlyphsAndAdvancesForComplexText(run, 0, run.length(), glyphBuffer);
-
-    if (!glyphBuffer.size())
-        return DashArray();
-    
-    // FIXME: Handle SVG + non-SVG interleaved runs. https://bugs.webkit.org/show_bug.cgi?id=133778
-    FloatPoint origin = FloatPoint(textOrigin.x() + deltaX, textOrigin.y());
-    MacGlyphToPathTranslator translator(run, glyphBuffer, origin);
-    DashArray result;
-    for (unsigned index = 0; translator.containsMorePaths(); ++index, translator.advance()) {
-        GlyphIterationState info = GlyphIterationState(CGPointMake(0, 0), CGPointMake(0, 0), lineExtents.y(), lineExtents.y() + lineExtents.height(), lineExtents.x() + lineExtents.width(), lineExtents.x());
-        const Font* localFont = glyphBuffer.fontAt(index);
-        if (!localFont) {
-            // The advances will get all messed up if we do anything other than bail here.
-            result.clear();
-            break;
-        }
-        switch (translator.underlineType()) {
-        case GlyphToPathTranslator::GlyphUnderlineType::SkipDescenders: {
-            Path path = translator.path();
-            CGPathApply(path.platformPath(), &info, &findPathIntersections);
-            if (info.minX < info.maxX) {
-                result.append(info.minX - lineExtents.x());
-                result.append(info.maxX - lineExtents.x());
-            }
-            break;
-        }
-        case GlyphToPathTranslator::GlyphUnderlineType::SkipGlyph: {
-            std::pair<float, float> extents = translator.extents();
-            result.append(extents.first - lineExtents.x());
-            result.append(extents.second - lineExtents.x());
-            break;
-        }
-        case GlyphToPathTranslator::GlyphUnderlineType::DrawOverGlyph:
-            // Nothing to do
-            break;
-        }
-    }
-    return result;
-}
-#endif
-
 bool FontCascade::primaryFontIsSystemFont() const
 {
     const auto& fontData = primaryFont();
index cdb7e11..d9a699f 100644 (file)
@@ -597,6 +597,20 @@ float Font::platformWidthForGlyph(Glyph glyph) const
     return advance.width + m_syntheticBoldOffset;
 }
 
+Path Font::platformPathForGlyph(Glyph glyph) const
+{
+    auto result = adoptCF(CTFontCreatePathForGlyph(platformData().ctFont(), glyph, nullptr));
+    auto syntheticBoldOffset = this->syntheticBoldOffset();
+    if (syntheticBoldOffset) {
+        auto newPath = adoptCF(CGPathCreateMutable());
+        CGPathAddPath(newPath.get(), nullptr, result.get());
+        auto translation = CGAffineTransformMakeTranslation(syntheticBoldOffset, 0);
+        CGPathAddPath(newPath.get(), &translation, result.get());
+        return newPath;
+    }
+    return adoptCF(CGPathCreateMutableCopy(result.get()));
+}
+
 bool Font::platformSupportsCodePoint(UChar32 character) const
 {
     UniChar codeUnits[2];
index 4fb6783..3b3ee8e 100644 (file)
@@ -152,6 +152,21 @@ float Font::platformWidthForGlyph(Glyph glyph) const
     return advance.width + m_syntheticBoldOffset;
 }
 
+Path Font::platformPathForGlyph(Glyph glyph) const
+{
+    auto ctFont = adoptCF(CTFontCreateWithGraphicsFont(platformData().cgFont(), platformData().size(), nullptr, nullptr));
+    auto result = adoptCF(CTFontCreatePathForGlyph(ctFont.get(), glyph, nullptr));
+    auto syntheticBoldOffset = this->syntheticBoldOffset();
+    if (syntheticBoldOffset) {
+        auto newPath = adoptCF(CGPathCreateMutable());
+        CGPathAddPath(newPath.get(), nullptr, result.get());
+        auto translation = CGAffineTransformMakeTranslation(syntheticBoldOffset, 0);
+        CGPathAddPath(newPath.get(), &translation, result.get());
+        return newPath;
+    }
+    return adoptCF(CGPathCreateMutableCopy(result.get()));
+}
+
 }
 
 #endif
index b1b390a..a5c8dae 100644 (file)
@@ -34,6 +34,7 @@
 #include "GlyphPage.h"
 #include "GraphicsContext.h"
 #include "HWndDC.h"
+#include "NotImplemented.h"
 #include <comutil.h>
 #include <dwrite.h>
 #include <mlang.h>
@@ -230,6 +231,12 @@ float Font::platformWidthForGlyph(Glyph glyph) const
     return width + m_syntheticBoldOffset;
 }
 
+Path Font::platformPathForGlyph(Glyph) const
+{
+    notImplemented();
+    return Path();
+}
+
 }
 
 #endif
index 9b8595c..858e2f7 100644 (file)
@@ -116,7 +116,6 @@ static void strokeWavyTextDecoration(GraphicsContext& context, const FloatRect&
     context.setStrokeThickness(strokeThickness);
 }
 
-#if ENABLE(CSS3_TEXT_DECORATION_SKIP_INK)
 static bool compareTuples(std::pair<float, float> l, std::pair<float, float> r)
 {
     return l.first < r.first;
@@ -167,7 +166,6 @@ static DashArray translateIntersectionPointsToSkipInkBoundaries(const DashArray&
     
     return result;
 }
-#endif
 
 static StrokeStyle textDecorationStyleToStrokeStyle(TextDecorationStyle decorationStyle)
 {
@@ -212,10 +210,6 @@ TextDecorationPainter::TextDecorationPainter(GraphicsContext& context, OptionSet
 
 void TextDecorationPainter::paintTextDecoration(const TextRun& textRun, const FloatPoint& textOrigin, const FloatPoint& boxOrigin)
 {
-#if !ENABLE(CSS3_TEXT_DECORATION_SKIP_INK)
-    UNUSED_PARAM(textRun);
-    UNUSED_PARAM(textOrigin);
-#endif
     const auto& fontMetrics = m_lineStyle.fontMetrics();
     float textDecorationThickness = textDecorationStrokeThickness(m_lineStyle.computedFontPixelSize());
     FloatPoint localOrigin = boxOrigin;
@@ -228,7 +222,6 @@ void TextDecorationPainter::paintTextDecoration(const TextRun& textRun, const Fl
         if (style == TextDecorationStyle::Wavy)
             strokeWavyTextDecoration(m_context, rect, m_lineStyle.computedFontPixelSize());
         else if (decoration == TextDecoration::Underline || decoration == TextDecoration::Overline) {
-#if ENABLE(CSS3_TEXT_DECORATION_SKIP_INK)
             if ((m_lineStyle.textDecorationSkip() == TextDecorationSkip::Ink || m_lineStyle.textDecorationSkip() == TextDecorationSkip::Auto) && m_isHorizontal) {
                 if (!m_context.paintingDisabled()) {
                     FloatRect underlineBoundingBox = m_context.computeUnderlineBoundsForText(rect, m_isPrinting);
@@ -238,11 +231,10 @@ void TextDecorationPainter::paintTextDecoration(const TextRun& textRun, const Fl
                     // We don't use underlineBoundingBox here because drawLinesForText() will run computeUnderlineBoundsForText() internally.
                     m_context.drawLinesForText(rect.location(), rect.height(), boundaries, m_isPrinting, style == TextDecorationStyle::Double, strokeStyle);
                 }
-            } else
+            } else {
                 // FIXME: Need to support text-decoration-skip: none.
-#endif
                 m_context.drawLineForText(rect, m_isPrinting, style == TextDecorationStyle::Double, strokeStyle);
-            
+            }
         } else {
             ASSERT(decoration == TextDecoration::LineThrough);
             m_context.drawLineForText(rect, m_isPrinting, style == TextDecorationStyle::Double, strokeStyle);