• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • khtml
  • svg
SVGPatternElement.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4
5 This file is part of the KDE project
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include "config.h"
24
25#if ENABLE(SVG)
26#include "SVGPatternElement.h"
27
28#include "AffineTransform.h"
29#include "Document.h"
30#include "FloatConversion.h"
31#include "GraphicsContext.h"
32#include "ImageBuffer.h"
33#include "PatternAttributes.h"
34#include "RenderSVGContainer.h"
35#include "SVGLength.h"
36#include "SVGNames.h"
37#include "SVGPaintServerPattern.h"
38#include "SVGRenderSupport.h"
39#include "SVGStyledTransformableElement.h"
40#include "SVGSVGElement.h"
41#include "SVGTransformList.h"
42#include "SVGTransformable.h"
43#include "SVGUnitTypes.h"
44
45#include <math.h>
46#include <wtf/OwnPtr.h>
47#include <wtf/MathExtras.h>
48
49using namespace std;
50
51namespace WebCore {
52
53SVGPatternElement::SVGPatternElement(const QualifiedName& tagName, Document* doc)
54 : SVGStyledElement(tagName, doc)
55 , SVGURIReference()
56 , SVGTests()
57 , SVGLangSpace()
58 , SVGExternalResourcesRequired()
59 , SVGFitToViewBox()
60 , m_x(this, LengthModeWidth)
61 , m_y(this, LengthModeHeight)
62 , m_width(this, LengthModeWidth)
63 , m_height(this, LengthModeHeight)
64 , m_patternUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
65 , m_patternContentUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
66 , m_patternTransform(SVGTransformList::create(SVGNames::patternTransformAttr))
67{
68}
69
70SVGPatternElement::~SVGPatternElement()
71{
72}
73
74ANIMATED_PROPERTY_DEFINITIONS(SVGPatternElement, int, Enumeration, enumeration, PatternUnits, patternUnits, SVGNames::patternUnitsAttr, m_patternUnits)
75ANIMATED_PROPERTY_DEFINITIONS(SVGPatternElement, int, Enumeration, enumeration, PatternContentUnits, patternContentUnits, SVGNames::patternContentUnitsAttr, m_patternContentUnits)
76ANIMATED_PROPERTY_DEFINITIONS(SVGPatternElement, SVGLength, Length, length, X, x, SVGNames::xAttr, m_x)
77ANIMATED_PROPERTY_DEFINITIONS(SVGPatternElement, SVGLength, Length, length, Y, y, SVGNames::yAttr, m_y)
78ANIMATED_PROPERTY_DEFINITIONS(SVGPatternElement, SVGLength, Length, length, Width, width, SVGNames::widthAttr, m_width)
79ANIMATED_PROPERTY_DEFINITIONS(SVGPatternElement, SVGLength, Length, length, Height, height, SVGNames::heightAttr, m_height)
80ANIMATED_PROPERTY_DEFINITIONS(SVGPatternElement, SVGTransformList*, TransformList, transformList, PatternTransform, patternTransform, SVGNames::patternTransformAttr, m_patternTransform.get())
81
82void SVGPatternElement::parseMappedAttribute(MappedAttribute* attr)
83{
84 if (attr->name() == SVGNames::patternUnitsAttr) {
85 if (attr->value() == "userSpaceOnUse")
86 setPatternUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
87 else if (attr->value() == "objectBoundingBox")
88 setPatternUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
89 } else if (attr->name() == SVGNames::patternContentUnitsAttr) {
90 if (attr->value() == "userSpaceOnUse")
91 setPatternContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
92 else if (attr->value() == "objectBoundingBox")
93 setPatternContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
94 } else if (attr->name() == SVGNames::patternTransformAttr) {
95 SVGTransformList* patternTransforms = patternTransformBaseValue();
96 if (!SVGTransformable::parseTransformAttribute(patternTransforms, attr->value())) {
97 ExceptionCode ec = 0;
98 patternTransforms->clear(ec);
99 }
100 } else if (attr->name() == SVGNames::xAttr)
101 setXBaseValue(SVGLength(this, LengthModeWidth, attr->value()));
102 else if (attr->name() == SVGNames::yAttr)
103 setYBaseValue(SVGLength(this, LengthModeHeight, attr->value()));
104 else if (attr->name() == SVGNames::widthAttr) {
105 setWidthBaseValue(SVGLength(this, LengthModeWidth, attr->value()));
106 if (width().value() < 0.0)
107 document()->accessSVGExtensions()->reportError("A negative value for pattern attribute <width> is not allowed");
108 } else if (attr->name() == SVGNames::heightAttr) {
109 setHeightBaseValue(SVGLength(this, LengthModeHeight, attr->value()));
110 if (width().value() < 0.0)
111 document()->accessSVGExtensions()->reportError("A negative value for pattern attribute <height> is not allowed");
112 } else {
113 if (SVGURIReference::parseMappedAttribute(attr))
114 return;
115 if (SVGTests::parseMappedAttribute(attr))
116 return;
117 if (SVGLangSpace::parseMappedAttribute(attr))
118 return;
119 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
120 return;
121 if (SVGFitToViewBox::parseMappedAttribute(attr))
122 return;
123
124 SVGStyledElement::parseMappedAttribute(attr);
125 }
126}
127
128void SVGPatternElement::svgAttributeChanged(const QualifiedName& attrName)
129{
130 SVGStyledElement::svgAttributeChanged(attrName);
131
132 if (!m_resource)
133 return;
134
135 if (attrName == SVGNames::patternUnitsAttr || attrName == SVGNames::patternContentUnitsAttr ||
136 attrName == SVGNames::patternTransformAttr || attrName == SVGNames::xAttr || attrName == SVGNames::yAttr ||
137 attrName == SVGNames::widthAttr || attrName == SVGNames::heightAttr ||
138 SVGURIReference::isKnownAttribute(attrName) ||
139 SVGTests::isKnownAttribute(attrName) ||
140 SVGLangSpace::isKnownAttribute(attrName) ||
141 SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
142 SVGFitToViewBox::isKnownAttribute(attrName) ||
143 SVGStyledElement::isKnownAttribute(attrName))
144 m_resource->invalidate();
145}
146
147void SVGPatternElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
148{
149 SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
150
151 if (!m_resource)
152 return;
153
154 m_resource->invalidate();
155}
156
157void SVGPatternElement::buildPattern(const FloatRect& targetRect) const
158{
159 PatternAttributes attributes = collectPatternProperties();
160
161 // If we didn't find any pattern content, ignore the request.
162 if (!attributes.patternContentElement() || !renderer() || !renderer()->style())
163 return;
164
165 FloatRect patternBoundaries;
166 FloatRect patternContentBoundaries;
167
168 // Determine specified pattern size
169 if (attributes.boundingBoxMode())
170 patternBoundaries = FloatRect(attributes.x().valueAsPercentage() * targetRect.width(),
171 attributes.y().valueAsPercentage() * targetRect.height(),
172 attributes.width().valueAsPercentage() * targetRect.width(),
173 attributes.height().valueAsPercentage() * targetRect.height());
174 else
175 patternBoundaries = FloatRect(attributes.x().value(),
176 attributes.y().value(),
177 attributes.width().value(),
178 attributes.height().value());
179
180 // Clip pattern boundaries to target boundaries
181 if (patternBoundaries.width() > targetRect.width())
182 patternBoundaries.setWidth(targetRect.width());
183
184 if (patternBoundaries.height() > targetRect.height())
185 patternBoundaries.setHeight(targetRect.height());
186
187 IntSize patternSize(patternBoundaries.width(), patternBoundaries.height());
188 clampImageBufferSizeToViewport(document()->renderer(), patternSize);
189
190 if (patternSize.width() < static_cast<int>(patternBoundaries.width()))
191 patternBoundaries.setWidth(patternSize.width());
192
193 if (patternSize.height() < static_cast<int>(patternBoundaries.height()))
194 patternBoundaries.setHeight(patternSize.height());
195
196 // Eventually calculate the pattern content boundaries (only needed with overflow="visible").
197 RenderStyle* style = renderer()->style();
198 if (style->overflowX() == OVISIBLE && style->overflowY() == OVISIBLE) {
199 for (Node* n = attributes.patternContentElement()->firstChild(); n; n = n->nextSibling()) {
200 if (!n->isSVGElement() || !static_cast<SVGElement*>(n)->isStyledTransformable() || !n->renderer())
201 continue;
202 patternContentBoundaries.unite(n->renderer()->relativeBBox(true));
203 }
204 }
205
206 AffineTransform viewBoxCTM = viewBoxToViewTransform(patternBoundaries.width(), patternBoundaries.height());
207 FloatRect patternBoundariesIncludingOverflow = patternBoundaries;
208
209 // Apply objectBoundingBoxMode fixup for patternContentUnits, if viewBox is not set.
210 if (!patternContentBoundaries.isEmpty()) {
211 if (!viewBoxCTM.isIdentity())
212 patternContentBoundaries = viewBoxCTM.mapRect(patternContentBoundaries);
213 else if (attributes.boundingBoxModeContent())
214 patternContentBoundaries = FloatRect(patternContentBoundaries.x() * targetRect.width(),
215 patternContentBoundaries.y() * targetRect.height(),
216 patternContentBoundaries.width() * targetRect.width(),
217 patternContentBoundaries.height() * targetRect.height());
218
219 patternBoundariesIncludingOverflow.unite(patternContentBoundaries);
220 }
221
222 IntSize imageSize(lroundf(patternBoundariesIncludingOverflow.width()), lroundf(patternBoundariesIncludingOverflow.height()));
223 clampImageBufferSizeToViewport(document()->renderer(), imageSize);
224
225 auto_ptr<ImageBuffer> patternImage = ImageBuffer::create(imageSize, false);
226
227 if (!patternImage.get())
228 return;
229
230 GraphicsContext* context = patternImage->context();
231 ASSERT(context);
232
233 context->save();
234
235 // Move to pattern start origin
236 if (patternBoundariesIncludingOverflow.location() != patternBoundaries.location()) {
237 context->translate(patternBoundaries.x() - patternBoundariesIncludingOverflow.x(),
238 patternBoundaries.y() - patternBoundariesIncludingOverflow.y());
239
240 patternBoundaries.setLocation(patternBoundariesIncludingOverflow.location());
241 }
242
243 // Process viewBox or boundingBoxModeContent correction
244 if (!viewBoxCTM.isIdentity())
245 context->concatCTM(viewBoxCTM);
246 else if (attributes.boundingBoxModeContent()) {
247 context->translate(targetRect.x(), targetRect.y());
248 context->scale(FloatSize(targetRect.width(), targetRect.height()));
249 }
250
251 // Render subtree into ImageBuffer
252 for (Node* n = attributes.patternContentElement()->firstChild(); n; n = n->nextSibling()) {
253 if (!n->isSVGElement() || !static_cast<SVGElement*>(n)->isStyled() || !n->renderer())
254 continue;
255 renderSubtreeToImage(patternImage.get(), n->renderer());
256 }
257
258 context->restore();
259
260 m_resource->setPatternTransform(attributes.patternTransform());
261 m_resource->setPatternBoundaries(patternBoundaries);
262 m_resource->setTile(patternImage);
263}
264
265RenderObject* SVGPatternElement::createRenderer(RenderArena* arena, RenderStyle*)
266{
267 RenderSVGContainer* patternContainer = new (arena) RenderSVGContainer(this);
268 patternContainer->setDrawsContents(false);
269 return patternContainer;
270}
271
272SVGResource* SVGPatternElement::canvasResource()
273{
274 if (!m_resource)
275 m_resource = SVGPaintServerPattern::create(this);
276
277 return m_resource.get();
278}
279
280PatternAttributes SVGPatternElement::collectPatternProperties() const
281{
282 PatternAttributes attributes;
283 HashSet<const SVGPatternElement*> processedPatterns;
284
285 const SVGPatternElement* current = this;
286 while (current) {
287 if (!attributes.hasX() && current->hasAttribute(SVGNames::xAttr))
288 attributes.setX(current->x());
289
290 if (!attributes.hasY() && current->hasAttribute(SVGNames::yAttr))
291 attributes.setY(current->y());
292
293 if (!attributes.hasWidth() && current->hasAttribute(SVGNames::widthAttr))
294 attributes.setWidth(current->width());
295
296 if (!attributes.hasHeight() && current->hasAttribute(SVGNames::heightAttr))
297 attributes.setHeight(current->height());
298
299 if (!attributes.hasBoundingBoxMode() && current->hasAttribute(SVGNames::patternUnitsAttr))
300 attributes.setBoundingBoxMode(current->getAttribute(SVGNames::patternUnitsAttr) == "objectBoundingBox");
301
302 if (!attributes.hasBoundingBoxModeContent() && current->hasAttribute(SVGNames::patternContentUnitsAttr))
303 attributes.setBoundingBoxModeContent(current->getAttribute(SVGNames::patternContentUnitsAttr) == "objectBoundingBox");
304
305 if (!attributes.hasPatternTransform() && current->hasAttribute(SVGNames::patternTransformAttr))
306 attributes.setPatternTransform(current->patternTransform()->consolidate().matrix());
307
308 if (!attributes.hasPatternContentElement() && current->hasChildNodes())
309 attributes.setPatternContentElement(current);
310
311 processedPatterns.add(current);
312
313 // Respect xlink:href, take attributes from referenced element
314 Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
315 if (refNode && refNode->hasTagName(SVGNames::patternTag)) {
316 current = static_cast<const SVGPatternElement*>(const_cast<const Node*>(refNode));
317
318 // Cycle detection
319 if (processedPatterns.contains(current))
320 return PatternAttributes();
321 } else
322 current = 0;
323 }
324
325 return attributes;
326}
327
328}
329
330#endif // ENABLE(SVG)
AffineTransform.h
FloatConversion.h
PatternAttributes.h
SVGLength.h
SVGNames.h
SVGPaintServerPattern.h
SVGPatternElement.h
SVGSVGElement.h
SVGStyledTransformableElement.h
SVGTransformList.h
SVGTransformable.h
SVGUnitTypes.h
create
KAction * create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent)
WebCore::SVGNames::patternTransformAttr
DOM::QualifiedName patternTransformAttr
Definition: SVGNames.cpp:250
WebCore
Definition: CSSHelper.h:7
X
khtml::ExceptionCode
unsigned short ExceptionCode
Definition: ExceptionCode.h:37
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal