1 /*
2 Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007, 2008 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 "SVGClipPathElement.h"
27
28 #include "CSSStyleSelector.h"
29 #include "Document.h"
30 #include "MappedAttribute.h"
31 #include "SVGNames.h"
32 #include "SVGTransformList.h"
33 #include "SVGUnitTypes.h"
34
35 namespace WebCore {
36
SVGClipPathElement(const QualifiedName & tagName,Document * doc)37 SVGClipPathElement::SVGClipPathElement(const QualifiedName& tagName, Document* doc)
38 : SVGStyledTransformableElement(tagName, doc)
39 , SVGTests()
40 , SVGLangSpace()
41 , SVGExternalResourcesRequired()
42 , m_clipPathUnits(this, SVGNames::clipPathUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
43 {
44 }
45
~SVGClipPathElement()46 SVGClipPathElement::~SVGClipPathElement()
47 {
48 }
49
parseMappedAttribute(MappedAttribute * attr)50 void SVGClipPathElement::parseMappedAttribute(MappedAttribute* attr)
51 {
52 if (attr->name() == SVGNames::clipPathUnitsAttr) {
53 if (attr->value() == "userSpaceOnUse")
54 setClipPathUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
55 else if (attr->value() == "objectBoundingBox")
56 setClipPathUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
57 } else {
58 if (SVGTests::parseMappedAttribute(attr))
59 return;
60 if (SVGLangSpace::parseMappedAttribute(attr))
61 return;
62 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
63 return;
64 SVGStyledTransformableElement::parseMappedAttribute(attr);
65 }
66 }
67
svgAttributeChanged(const QualifiedName & attrName)68 void SVGClipPathElement::svgAttributeChanged(const QualifiedName& attrName)
69 {
70 SVGStyledTransformableElement::svgAttributeChanged(attrName);
71
72 if (!m_clipper)
73 return;
74
75 if (attrName == SVGNames::clipPathUnitsAttr ||
76 SVGTests::isKnownAttribute(attrName) ||
77 SVGLangSpace::isKnownAttribute(attrName) ||
78 SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
79 SVGStyledTransformableElement::isKnownAttribute(attrName))
80 m_clipper->invalidate();
81 }
82
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)83 void SVGClipPathElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
84 {
85 SVGStyledTransformableElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
86
87 if (!m_clipper)
88 return;
89
90 m_clipper->invalidate();
91 }
92
canvasResource()93 SVGResource* SVGClipPathElement::canvasResource()
94 {
95 if (!m_clipper)
96 m_clipper = SVGResourceClipper::create();
97 else
98 m_clipper->resetClipData();
99
100 bool bbox = clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX;
101
102 RefPtr<RenderStyle> clipPathStyle = styleForRenderer(); // FIXME: Manual style resolution is a hack
103 for (Node* n = firstChild(); n; n = n->nextSibling()) {
104 if (n->isSVGElement() && static_cast<SVGElement*>(n)->isStyledTransformable()) {
105 SVGStyledTransformableElement* styled = static_cast<SVGStyledTransformableElement*>(n);
106 RefPtr<RenderStyle> pathStyle = document()->styleSelector()->styleForElement(styled, clipPathStyle.get());
107 if (pathStyle->display() != NONE) {
108 Path pathData = styled->toClipPath();
109 if (!pathData.isEmpty())
110 m_clipper->addClipData(pathData, pathStyle->svgStyle()->clipRule(), bbox);
111 }
112 }
113 }
114 if (m_clipper->clipData().isEmpty()) {
115 Path pathData;
116 pathData.addRect(FloatRect());
117 m_clipper->addClipData(pathData, RULE_EVENODD, bbox);
118 }
119 return m_clipper.get();
120 }
121
122 }
123
124 #endif // ENABLE(SVG)
125