File: SVGPaintServerHandlingInlines.h

package info (click to toggle)
webkit2gtk 2.51.90-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 484,192 kB
  • sloc: cpp: 3,930,945; javascript: 197,713; ansic: 167,619; python: 53,160; asm: 21,857; ruby: 18,114; perl: 17,149; xml: 4,631; sh: 2,462; yacc: 2,394; java: 2,032; lex: 1,358; pascal: 372; makefile: 215
file content (197 lines) | stat: -rw-r--r-- 8,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2023, 2024 Igalia S.L.
 * Copyright (C) 2025 Samuel Weinig <sam@webkit.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#pragma once

#include "RenderSVGResourceGradient.h"
#include "RenderStyle+GettersInlines.h"
#include "RenderView.h"
#include "SVGPaintServerHandling.h"
#include "SVGRenderSupport.h"
#include "StyleComputedStyle+InitialInlines.h"

namespace WebCore {

template<SVGPaintServerHandling::Operation op>
bool SVGPaintServerHandling::preparePaintOperation(const RenderLayerModelObject& renderer, const RenderStyle& style) const
{
    auto paintServerResult = requestPaintServer<op>(renderer, style);
    if (std::holds_alternative<std::monostate>(paintServerResult))
        return false;

    if (std::holds_alternative<RenderSVGResourcePaintServer*>(paintServerResult)) {
        auto& paintServer = *std::get<RenderSVGResourcePaintServer*>(paintServerResult);

        if constexpr (op == Operation::Fill) {
            if (paintServer.prepareFillOperation(m_context, renderer, style))
                return true;
        } else if constexpr (op == Operation::Stroke) {
            if (paintServer.prepareStrokeOperation(m_context, renderer, style))
                return true;
        }

        // Repeat the paint server request, but explicitly treating the paintServer as invalid/not-existent, to go through the fallback code path.
        paintServerResult = requestPaintServer<op, URIResolving::Disabled>(renderer, style);
        if (std::holds_alternative<std::monostate>(paintServerResult))
            return false;
    }

    ASSERT(std::holds_alternative<Color>(paintServerResult));
    const auto& color = std::get<Color>(paintServerResult);

    if constexpr (op == Operation::Fill)
        prepareFillOperation(renderer, style, color);
    else
        prepareStrokeOperation(renderer, style, color);

    return true;
}

template<SVGPaintServerHandling::Operation op, SVGPaintServerHandling::URIResolving allowPaintServerURIResolving>
SVGPaintServerOrColor SVGPaintServerHandling::requestPaintServer(const RenderLayerModelObject& targetRenderer, const RenderStyle& style)
{
    // When rendering the mask for a RenderSVGResourceClipper, always use the initial fill paint server.
    if (targetRenderer.view().frameView().paintBehavior().contains(PaintBehavior::RenderingSVGClipOrMask)) {
        if constexpr (op == Operation::Fill)
            return Style::ComputedStyle::initialFill().colorDisregardingType().resolvedColor();
        else
            return Style::ComputedStyle::initialStroke().colorDisregardingType().resolvedColor();
    }

    auto& paint = [&] -> const Style::SVGPaint& {
        if constexpr (op == Operation::Fill)
            return style.fill();
        else
            return style.stroke();
    }();

    if (paint.isNone())
        return { };

    if (!paint.isColor()) {
        if (allowPaintServerURIResolving == URIResolving::Disabled) {
            // If we found no paint server, and no fallback is desired, stop here.
            // We can only get here, if we previously requested a paint server, attempted to
            // prepare a fill or stroke operation, which failed. It can fail if, for example,
            // the paint sever is a gradient, gradientUnits are set to 'objectBoundingBox' and
            // the target is an one-dimensional object without a defined 'objectBoundingBox' (<line>).
            if (paint.isURL() || paint.isURLNone())
                return { };
        } else {
            auto paintServerForOperation = [&] {
                if constexpr (op == Operation::Fill)
                    return targetRenderer.svgFillPaintServerResourceFromStyle(style);
                else
                    return targetRenderer.svgStrokePaintServerResourceFromStyle(style);
            };

            // Try resolving URI first.
            if (auto* paintServer = paintServerForOperation())
                return paintServer;

            // If we found no paint server, and no fallback is desired, stop here.
            if (paint.isURL() || paint.isURLNone())
                return { };
        }
    }

    // Color and SVGPaint::URLColor handling.
    auto color = resolveColorFromStyle<op>(style);
    if (inheritColorFromParentStyleIfNeeded<op>(targetRenderer, color))
        return color;
    return { };
}

inline void SVGPaintServerHandling::prepareFillOperation(const RenderLayerModelObject& renderer, const RenderStyle& style, const Color& fillColor) const
{
    if (renderer.view().frameView().paintBehavior().contains(PaintBehavior::RenderingSVGClipOrMask)) {
        m_context.setAlpha(1);
        m_context.setFillRule(style.clipRule());
    } else {
        m_context.setAlpha(style.fillOpacity().value.value);
        m_context.setFillRule(style.fillRule());
    }

    Style::ColorResolver colorResolver { style };
    m_context.setFillColor(colorResolver.colorApplyingColorFilter(fillColor));
}

inline void SVGPaintServerHandling::prepareStrokeOperation(const RenderLayerModelObject& renderer, const RenderStyle& style, const Color& strokeColor) const
{
    m_context.setAlpha(style.strokeOpacity().value.value);

    Style::ColorResolver colorResolver { style };
    m_context.setStrokeColor(colorResolver.colorApplyingColorFilter(strokeColor));
    SVGRenderSupport::applyStrokeStyleToContext(m_context, style, renderer);
}

template<SVGPaintServerHandling::Operation op>
Color SVGPaintServerHandling::resolveColorFromStyle(const RenderStyle& style)
{
    if constexpr (op == Operation::Fill)
        return resolveColorFromStyle(style, style.fill(), style.visitedLinkFill());
    else
        return resolveColorFromStyle(style, style.stroke(), style.visitedLinkStroke());
}

inline Color SVGPaintServerHandling::resolveColorFromStyle(const RenderStyle& style, const Style::SVGPaint& paint, const Style::SVGPaint& visitedLinkPaint)
{
    // All paint types except `none` / `url` / `url none` handle solid colors.
    ASSERT(!paint.isNone());
    ASSERT(!paint.isURL());
    ASSERT(!paint.isURLNone());

    Style::ColorResolver colorResolver { style };

    auto color = colorResolver.colorResolvingCurrentColor(paint.colorDisregardingType());
    if (style.insideLink() == InsideLink::InsideVisited) {
        // FIXME: This code doesn't support the uri component of the visited link paint, https://bugs.webkit.org/show_bug.cgi?id=70006
        // FIXME: This code is resolving the visit link paint color with RenderStyle::color(), rather than the more commonly used RenderStyle::visitedLinkColor(). If this is intentional, we should document that, otherwise, we should use RenderStyle::visitedLinkColor().
        if (auto visitedLinkPaintColor = visitedLinkPaint.tryColor()) {
            if (auto visitedColor = colorResolver.colorResolvingCurrentColor(*visitedLinkPaintColor); visitedColor.isValid())
                color = visitedColor.colorWithAlpha(color.alphaAsFloat());
        }
    }

    return color;
}

template<SVGPaintServerHandling::Operation op>
bool SVGPaintServerHandling::inheritColorFromParentStyleIfNeeded(const RenderLayerModelObject& renderer, Color& color)
{
    if (color.isValid())
        return true;
    if (!renderer.parent())
        return false;

    // FIXME: If this is intentionally using the `renderer` currentColor to resolve colors from `renderer.parent()`, we should document that, otherwise, this should probably use the corresponding style's current color.

    Style::ColorResolver colorResolver { renderer.style() };
    auto& parentStyle = renderer.parent()->style();

    if constexpr (op == Operation::Fill)
        color = colorResolver.colorResolvingCurrentColor(parentStyle.fill().colorDisregardingType());
    else
        color = colorResolver.colorResolvingCurrentColor(parentStyle.stroke().colorDisregardingType());

    return true;
}

} // namespace WebCore