File: RenderSVGResourcePattern.cpp

package info (click to toggle)
webkit2gtk 2.44.2-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 367,728 kB
  • sloc: cpp: 2,946,520; javascript: 194,328; ansic: 137,901; python: 45,716; ruby: 18,487; asm: 16,672; perl: 16,476; xml: 4,376; yacc: 2,350; sh: 2,127; java: 1,711; lex: 1,323; pascal: 333; makefile: 323
file content (218 lines) | stat: -rw-r--r-- 9,004 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2021, 2022, 2023, 2024 Igalia S.L.
 *
 * 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.
 */

#include "config.h"
#include "RenderSVGResourcePattern.h"

#if ENABLE(LAYER_BASED_SVG_ENGINE)
#include "ElementChildIteratorInlines.h"
#include "RenderLayer.h"
#include "RenderSVGModelObjectInlines.h"
#include "RenderSVGResourcePatternInlines.h"
#include "RenderSVGShape.h"
#include "SVGElementTypeHelpers.h"
#include "SVGFitToViewBox.h"
#include "SVGRenderStyle.h"
#include <wtf/IsoMallocInlines.h>

namespace WebCore {

WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGResourcePattern);

RenderSVGResourcePattern::RenderSVGResourcePattern(SVGElement& element, RenderStyle&& style)
    : RenderSVGResourcePaintServer(Type::SVGResourcePattern, element, WTFMove(style))
{
}

RenderSVGResourcePattern::~RenderSVGResourcePattern() = default;

void RenderSVGResourcePattern::collectPatternAttributesIfNeeded()
{
    if (m_attributes.has_value())
        return;

    auto attributes = PatternAttributes { };

    SVGPatternElement* current = &patternElement();

    patternElement().synchronizeAllAttributes();

    while (current) {
        if (!current->renderer())
            break;
        current->collectPatternAttributes(attributes);

        auto target = SVGURIReference::targetElementFromIRIString(current->href(), current->treeScopeForSVGReferences());
        current = dynamicDowncast<SVGPatternElement>(target.element.get());
    }

    // If we couldn't determine the pattern content element root, stop here.
    if (!attributes.patternContentElement())
        return;

    // An empty viewBox disables rendering.
    if (attributes.hasViewBox() && attributes.viewBox().isEmpty())
        return;

    m_attributes = WTFMove(attributes);
}

static void clear2DRotation(AffineTransform& transform)
{
    AffineTransform::DecomposedType decomposition;
    transform.decompose(decomposition);
    decomposition.angle = 0;
    transform.recompose(decomposition);
}

RefPtr<Pattern> RenderSVGResourcePattern::buildPattern(GraphicsContext& context, const RenderLayerModelObject& renderer)
{
    collectPatternAttributesIfNeeded();

    if (!m_attributes)
        return nullptr;

    // Spec: When the geometry of the applicable element has no width or height and objectBoundingBox is specified,
    // then the given effect (e.g. a gradient or a filter) will be ignored.
    FloatRect objectBoundingBox = renderer.objectBoundingBox();
    if (m_attributes->patternUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX && objectBoundingBox.isEmpty())
        return nullptr;

    // Compute all necessary transformations to build the tile image & the pattern.
    FloatRect tileBoundaries;
    AffineTransform tileImageTransform;
    if (!buildTileImageTransform(renderer, *m_attributes, patternElement(), tileBoundaries, tileImageTransform))
        return nullptr;

    // Ignore 2D rotation, as it doesn't affect the size of the tile.
    auto absoluteTransformIgnoringRotation = context.getCTM(GraphicsContext::DefinitelyIncludeDeviceScale);
    clear2DRotation(absoluteTransformIgnoringRotation);
    FloatRect absoluteTileBoundaries = absoluteTransformIgnoringRotation.mapRect(tileBoundaries);

    // Scale the tile size to match the scale level of the patternTransform.
    absoluteTileBoundaries.scale(static_cast<float>(m_attributes->patternTransform().xScale()), static_cast<float>(m_attributes->patternTransform().yScale()));

    // Build tile image.
    auto tileImage = createTileImage(*m_attributes, tileBoundaries, absoluteTileBoundaries, tileImageTransform);
    if (!tileImage)
        return nullptr;

    auto tileImageSize = tileImage->logicalSize();

    auto copiedImage = ImageBuffer::sinkIntoNativeImage(WTFMove(tileImage));
    if (!copiedImage)
        return nullptr;

    // Compute pattern space transformation.
    AffineTransform patternSpaceTransform;
    patternSpaceTransform.translate(tileBoundaries.location());
    patternSpaceTransform.scale(tileBoundaries.size() / tileImageSize);

    auto patternTransform = m_attributes->patternTransform();
    if (!patternTransform.isIdentity())
        patternSpaceTransform = patternTransform * patternSpaceTransform;

    // Build pattern.
    return Pattern::create({ copiedImage.releaseNonNull() }, { true, true, patternSpaceTransform });
}

bool RenderSVGResourcePattern::prepareFillOperation(GraphicsContext& context, const RenderLayerModelObject& targetRenderer, const RenderStyle& style)
{
    auto pattern = buildPattern(context, targetRenderer);
    if (!pattern)
        return false;

    const auto& svgStyle = style.svgStyle();
    context.setAlpha(svgStyle.fillOpacity());
    context.setFillRule(svgStyle.fillRule());
    context.setFillPattern(pattern.copyRef().releaseNonNull());
    return true;
}

bool RenderSVGResourcePattern::prepareStrokeOperation(GraphicsContext& context, const RenderLayerModelObject& targetRenderer, const RenderStyle& style)
{
    auto pattern = buildPattern(context, targetRenderer);
    if (!pattern)
        return false;

    const auto& svgStyle = style.svgStyle();

    context.setAlpha(svgStyle.strokeOpacity());
    SVGRenderSupport::applyStrokeStyleToContext(context, style, targetRenderer);
    if (svgStyle.vectorEffect() == VectorEffect::NonScalingStroke) {
        if (auto* shape = dynamicDowncast<RenderSVGShape>(targetRenderer))
            pattern->setPatternSpaceTransform(shape->nonScalingStrokeTransform().multiply(pattern->patternSpaceTransform()));
    }
    context.setStrokePattern(pattern.releaseNonNull());
    return true;
}

bool RenderSVGResourcePattern::buildTileImageTransform(const RenderElement& renderer, const PatternAttributes& attributes, const SVGPatternElement& patternElement, FloatRect& patternBoundaries, AffineTransform& tileImageTransform) const
{
    auto objectBoundingBox = renderer.objectBoundingBox();
    patternBoundaries = calculatePatternBoundaries(attributes, objectBoundingBox, patternElement);
    if (patternBoundaries.width() <= 0 || patternBoundaries.height() <= 0)
        return false;

    auto viewBoxCTM = SVGFitToViewBox::viewBoxToViewTransform(attributes.viewBox(), attributes.preserveAspectRatio(), patternBoundaries.width(), patternBoundaries.height());

    // Apply viewBox/objectBoundingBox transformations.
    if (!viewBoxCTM.isIdentity())
        tileImageTransform = viewBoxCTM;
    else if (attributes.patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
        tileImageTransform.scale(objectBoundingBox.width(), objectBoundingBox.height());

    return true;
}

RefPtr<ImageBuffer> RenderSVGResourcePattern::createTileImage(const PatternAttributes& attributes, const FloatRect& tileBoundaries, const FloatRect& absoluteTileBoundaries, const AffineTransform& tileImageTransform) const
{
    auto* patternRenderer = static_cast<RenderSVGResourcePattern*>(attributes.patternContentElement()->renderer());
    ASSERT(patternRenderer);
    ASSERT(patternRenderer->hasLayer());

    if (SVGHitTestCycleDetectionScope::isVisiting(*patternRenderer))
        return nullptr;

    // FIXME: consider color space handling/'color-interpolation'.
    auto clampedAbsoluteTileBoundaries = ImageBuffer::clampedRect(absoluteTileBoundaries);
    auto tileImage = ImageBuffer::create(roundedIntSize(clampedAbsoluteTileBoundaries.size()), RenderingPurpose::Unspecified, 1, DestinationColorSpace::SRGB(), PixelFormat::BGRA8);
    if (!tileImage)
        return nullptr;

    auto& tileImageContext = tileImage->context();

    GraphicsContextStateSaver stateSaver(tileImageContext);

    FloatSize unclampedSize = roundedIntSize(tileBoundaries.size());

    // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer.
    tileImageContext.scale(unclampedSize / tileBoundaries.size());

    // The image buffer represents the final rendered size, so the content has to be scaled (to avoid pixelation).
    tileImageContext.scale(clampedAbsoluteTileBoundaries.size() / tileBoundaries.size());

    // Draw the content into the ImageBuffer.
    patternRenderer->layer()->paintSVGResourceLayer(tileImageContext, tileImageTransform);
    return tileImage;
}

}

#endif // ENABLE(LAYER_BASED_SVG_ENGINE)