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

KHTML

  • khtml
  • svg
SVGMaskElement.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 2005 Alexander Kellett <lypanov@kde.org>
5
6 This file is part of the KDE project
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22*/
23
24#include "config.h"
25
26#if ENABLE(SVG)
27#include "SVGMaskElement.h"
28
29#include "CSSStyleSelector.h"
30#include "GraphicsContext.h"
31#include "ImageBuffer.h"
32#include "RenderSVGContainer.h"
33#include "SVGLength.h"
34#include "SVGNames.h"
35#include "SVGRenderSupport.h"
36#include "SVGUnitTypes.h"
37#include <math.h>
38#include <wtf/MathExtras.h>
39#include <wtf/OwnPtr.h>
40
41using namespace std;
42
43namespace WebCore {
44
45SVGMaskElement::SVGMaskElement(const QualifiedName& tagName, Document* doc)
46 : SVGStyledLocatableElement(tagName, doc)
47 , SVGURIReference()
48 , SVGTests()
49 , SVGLangSpace()
50 , SVGExternalResourcesRequired()
51 , m_maskUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
52 , m_maskContentUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
53 , m_x(this, LengthModeWidth)
54 , m_y(this, LengthModeHeight)
55 , m_width(this, LengthModeWidth)
56 , m_height(this, LengthModeHeight)
57{
58 // Spec: If the attribute is not specified, the effect is as if a value of "-10%" were specified.
59 setXBaseValue(SVGLength(this, LengthModeWidth, "-10%"));
60 setYBaseValue(SVGLength(this, LengthModeHeight, "-10%"));
61
62 // Spec: If the attribute is not specified, the effect is as if a value of "120%" were specified.
63 setWidthBaseValue(SVGLength(this, LengthModeWidth, "120%"));
64 setHeightBaseValue(SVGLength(this, LengthModeHeight, "120%"));
65}
66
67SVGMaskElement::~SVGMaskElement()
68{
69}
70
71ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, int, Enumeration, enumeration, MaskUnits, maskUnits, SVGNames::maskUnitsAttr, m_maskUnits)
72ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, int, Enumeration, enumeration, MaskContentUnits, maskContentUnits, SVGNames::maskContentUnitsAttr, m_maskContentUnits)
73ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, X, x, SVGNames::xAttr, m_x)
74ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, Y, y, SVGNames::yAttr, m_y)
75ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, Width, width, SVGNames::widthAttr, m_width)
76ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, Height, height, SVGNames::heightAttr, m_height)
77
78void SVGMaskElement::parseMappedAttribute(MappedAttribute* attr)
79{
80 if (attr->name() == SVGNames::maskUnitsAttr) {
81 if (attr->value() == "userSpaceOnUse")
82 setMaskUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
83 else if (attr->value() == "objectBoundingBox")
84 setMaskUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
85 } else if (attr->name() == SVGNames::maskContentUnitsAttr) {
86 if (attr->value() == "userSpaceOnUse")
87 setMaskContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
88 else if (attr->value() == "objectBoundingBox")
89 setMaskContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
90 } else if (attr->name() == SVGNames::xAttr)
91 setXBaseValue(SVGLength(this, LengthModeWidth, attr->value()));
92 else if (attr->name() == SVGNames::yAttr)
93 setYBaseValue(SVGLength(this, LengthModeHeight, attr->value()));
94 else if (attr->name() == SVGNames::widthAttr)
95 setWidthBaseValue(SVGLength(this, LengthModeWidth, attr->value()));
96 else if (attr->name() == SVGNames::heightAttr)
97 setHeightBaseValue(SVGLength(this, LengthModeHeight, attr->value()));
98 else {
99 if (SVGURIReference::parseMappedAttribute(attr))
100 return;
101 if (SVGTests::parseMappedAttribute(attr))
102 return;
103 if (SVGLangSpace::parseMappedAttribute(attr))
104 return;
105 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
106 return;
107 SVGStyledElement::parseMappedAttribute(attr);
108 }
109}
110
111void SVGMaskElement::svgAttributeChanged(const QualifiedName& attrName)
112{
113 SVGStyledElement::svgAttributeChanged(attrName);
114
115 if (!m_masker)
116 return;
117
118 if (attrName == SVGNames::maskUnitsAttr || attrName == SVGNames::maskContentUnitsAttr ||
119 attrName == SVGNames::xAttr || attrName == SVGNames::yAttr ||
120 attrName == SVGNames::widthAttr || attrName == SVGNames::heightAttr ||
121 SVGURIReference::isKnownAttribute(attrName) ||
122 SVGTests::isKnownAttribute(attrName) ||
123 SVGLangSpace::isKnownAttribute(attrName) ||
124 SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
125 SVGStyledElement::isKnownAttribute(attrName))
126 m_masker->invalidate();
127}
128
129void SVGMaskElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
130{
131 SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
132
133 if (!m_masker)
134 return;
135
136 m_masker->invalidate();
137}
138
139auto_ptr<ImageBuffer> SVGMaskElement::drawMaskerContent(const FloatRect& targetRect, FloatRect& maskDestRect) const
140{
141 // Determine specified mask size
142 float xValue;
143 float yValue;
144 float widthValue;
145 float heightValue;
146
147 if (maskUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
148 xValue = x().valueAsPercentage() * targetRect.width();
149 yValue = y().valueAsPercentage() * targetRect.height();
150 widthValue = width().valueAsPercentage() * targetRect.width();
151 heightValue = height().valueAsPercentage() * targetRect.height();
152 } else {
153 xValue = x().value();
154 yValue = y().value();
155 widthValue = width().value();
156 heightValue = height().value();
157 }
158
159 IntSize imageSize(lroundf(widthValue), lroundf(heightValue));
160 clampImageBufferSizeToViewport(document()->renderer(), imageSize);
161
162 if (imageSize.width() < static_cast<int>(widthValue))
163 widthValue = imageSize.width();
164
165 if (imageSize.height() < static_cast<int>(heightValue))
166 heightValue = imageSize.height();
167
168 auto_ptr<ImageBuffer> maskImage = ImageBuffer::create(imageSize, false);
169 if (!maskImage.get())
170 return maskImage;
171
172 maskDestRect = FloatRect(xValue, yValue, widthValue, heightValue);
173 if (maskUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
174 maskDestRect.move(targetRect.x(), targetRect.y());
175
176 GraphicsContext* maskImageContext = maskImage->context();
177 ASSERT(maskImageContext);
178
179 maskImageContext->save();
180 maskImageContext->translate(-xValue, -yValue);
181
182 if (maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
183 maskImageContext->save();
184 maskImageContext->scale(FloatSize(targetRect.width(), targetRect.height()));
185 }
186
187 // Render subtree into ImageBuffer
188 for (Node* n = firstChild(); n; n = n->nextSibling()) {
189 SVGElement* elem = 0;
190 if (n->isSVGElement())
191 elem = static_cast<SVGElement*>(n);
192 if (!elem || !elem->isStyled())
193 continue;
194
195 SVGStyledElement* e = static_cast<SVGStyledElement*>(elem);
196 RenderObject* item = e->renderer();
197 if (!item)
198 continue;
199
200 renderSubtreeToImage(maskImage.get(), item);
201 }
202
203 if (maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
204 maskImageContext->restore();
205
206 maskImageContext->restore();
207 return maskImage;
208}
209
210RenderObject* SVGMaskElement::createRenderer(RenderArena* arena, RenderStyle*)
211{
212 RenderSVGContainer* maskContainer = new (arena) RenderSVGContainer(this);
213 maskContainer->setDrawsContents(false);
214 return maskContainer;
215}
216
217SVGResource* SVGMaskElement::canvasResource()
218{
219 if (!m_masker)
220 m_masker = SVGResourceMasker::create(this);
221 return m_masker.get();
222}
223
224}
225
226#endif // ENABLE(SVG)
SVGLength.h
SVGMaskElement.h
SVGNames.h
SVGUnitTypes.h
WebCore
Definition: CSSHelper.h:7
X
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