2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2014 Google, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "core/svg/SVGGraphicsElement.h"
26 #include "core/SVGNames.h"
27 #include "core/rendering/svg/RenderSVGPath.h"
28 #include "core/rendering/svg/RenderSVGResource.h"
29 #include "core/rendering/svg/SVGPathData.h"
30 #include "platform/transforms/AffineTransform.h"
34 SVGGraphicsElement::SVGGraphicsElement(const QualifiedName& tagName, Document& document, ConstructionType constructionType)
35 : SVGElement(tagName, document, constructionType)
37 , m_transform(SVGAnimatedTransformList::create(this, SVGNames::transformAttr, SVGTransformList::create()))
39 addToPropertyMap(m_transform);
42 SVGGraphicsElement::~SVGGraphicsElement()
46 PassRefPtr<SVGMatrixTearOff> SVGGraphicsElement::getTransformToElement(SVGElement* target, ExceptionState& exceptionState)
48 AffineTransform ctm = getCTM(AllowStyleUpdate);
50 if (target && target->isSVGGraphicsElement()) {
51 AffineTransform targetCTM = toSVGGraphicsElement(target)->getCTM(AllowStyleUpdate);
52 if (!targetCTM.isInvertible()) {
53 exceptionState.throwDOMException(InvalidStateError, "The target transformation is not invertable.");
56 ctm = targetCTM.inverse() * ctm;
59 return SVGMatrixTearOff::create(ctm);
62 static bool isViewportElement(const Element& element)
64 return (isSVGSVGElement(element)
65 || isSVGSymbolElement(element)
66 || isSVGForeignObjectElement(element)
67 || isSVGImageElement(element));
70 AffineTransform SVGGraphicsElement::computeCTM(SVGElement::CTMScope mode,
71 SVGGraphicsElement::StyleUpdateStrategy styleUpdateStrategy, const SVGGraphicsElement* ancestor) const
73 if (styleUpdateStrategy == AllowStyleUpdate)
74 document().updateLayoutIgnorePendingStylesheets();
79 for (const Element* currentElement = this; currentElement && !done;
80 currentElement = currentElement->parentOrShadowHostElement()) {
81 if (!currentElement->isSVGElement())
84 ctm = toSVGElement(currentElement)->localCoordinateSpaceTransform(mode).multiply(ctm);
87 case NearestViewportScope:
88 // Stop at the nearest viewport ancestor.
89 done = currentElement != this && isViewportElement(*currentElement);
92 // Stop at the designated ancestor.
93 done = currentElement == ancestor;
96 ASSERT(mode == ScreenScope);
104 AffineTransform SVGGraphicsElement::getCTM(StyleUpdateStrategy styleUpdateStrategy)
106 return computeCTM(NearestViewportScope, styleUpdateStrategy);
109 AffineTransform SVGGraphicsElement::getScreenCTM(StyleUpdateStrategy styleUpdateStrategy)
111 return computeCTM(ScreenScope, styleUpdateStrategy);
114 PassRefPtr<SVGMatrixTearOff> SVGGraphicsElement::getCTMFromJavascript()
116 return SVGMatrixTearOff::create(getCTM());
119 PassRefPtr<SVGMatrixTearOff> SVGGraphicsElement::getScreenCTMFromJavascript()
121 return SVGMatrixTearOff::create(getScreenCTM());
124 AffineTransform SVGGraphicsElement::animatedLocalTransform() const
126 AffineTransform matrix;
127 RenderStyle* style = renderer() ? renderer()->style() : 0;
129 // If CSS property was set, use that, otherwise fallback to attribute (if set).
130 if (style && style->hasTransform()) {
131 TransformationMatrix transform;
132 float zoom = style->effectiveZoom();
134 // CSS transforms operate with pre-scaled lengths. To make this work with SVG
135 // (which applies the zoom factor globally, at the root level) we
137 // * pre-scale the bounding box (to bring it into the same space as the other CSS values)
138 // * invert the zoom factor (to effectively compute the CSS transform under a 1.0 zoom)
140 // Note: objectBoundingBox is an emptyRect for elements like pattern or clipPath.
141 // See the "Object bounding box units" section of http://dev.w3.org/csswg/css3-transforms/
143 FloatRect scaledBBox = renderer()->objectBoundingBox();
144 scaledBBox.scale(zoom);
145 transform.scale(1 / zoom);
146 style->applyTransform(transform, scaledBBox);
147 transform.scale(zoom);
149 style->applyTransform(transform, renderer()->objectBoundingBox());
152 // Flatten any 3D transform.
153 matrix = transform.toAffineTransform();
155 m_transform->currentValue()->concatenate(matrix);
158 if (m_supplementalTransform)
159 return *m_supplementalTransform * matrix;
163 AffineTransform* SVGGraphicsElement::supplementalTransform()
165 if (!m_supplementalTransform)
166 m_supplementalTransform = adoptPtr(new AffineTransform);
167 return m_supplementalTransform.get();
170 bool SVGGraphicsElement::isSupportedAttribute(const QualifiedName& attrName)
172 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
173 if (supportedAttributes.isEmpty()) {
174 SVGTests::addSupportedAttributes(supportedAttributes);
175 supportedAttributes.add(SVGNames::transformAttr);
177 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
180 void SVGGraphicsElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
182 parseAttributeNew(name, value);
185 void SVGGraphicsElement::svgAttributeChanged(const QualifiedName& attrName)
187 if (!isSupportedAttribute(attrName)) {
188 SVGElement::svgAttributeChanged(attrName);
192 SVGElement::InvalidationGuard invalidationGuard(this);
194 // Reattach so the isValid() check will be run again during renderer creation.
195 if (SVGTests::isKnownAttribute(attrName)) {
196 lazyReattachIfAttached();
200 RenderObject* object = renderer();
204 if (attrName == SVGNames::transformAttr) {
205 object->setNeedsTransformUpdate();
206 RenderSVGResource::markForLayoutAndParentResourceInvalidation(object);
210 ASSERT_NOT_REACHED();
213 SVGElement* SVGGraphicsElement::nearestViewportElement() const
215 for (Element* current = parentOrShadowHostElement(); current; current = current->parentOrShadowHostElement()) {
216 if (isViewportElement(*current))
217 return toSVGElement(current);
223 SVGElement* SVGGraphicsElement::farthestViewportElement() const
225 SVGElement* farthest = 0;
226 for (Element* current = parentOrShadowHostElement(); current; current = current->parentOrShadowHostElement()) {
227 if (isViewportElement(*current))
228 farthest = toSVGElement(current);
233 FloatRect SVGGraphicsElement::getBBox()
235 document().updateLayoutIgnorePendingStylesheets();
237 // FIXME: Eventually we should support getBBox for detached elements.
241 return renderer()->objectBoundingBox();
244 PassRefPtr<SVGRectTearOff> SVGGraphicsElement::getBBoxFromJavascript()
246 return SVGRectTearOff::create(SVGRect::create(getBBox()), 0, PropertyIsNotAnimVal);
249 RenderObject* SVGGraphicsElement::createRenderer(RenderStyle*)
251 // By default, any subclass is expected to do path-based drawing
252 return new RenderSVGPath(this);
255 void SVGGraphicsElement::toClipPath(Path& path)
257 updatePathFromGraphicsElement(this, path);
258 // FIXME: How do we know the element has done a layout?
259 path.transform(animatedLocalTransform());