2 * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2009 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/rendering/svg/RenderSVGTransformableContainer.h"
27 #include "core/rendering/svg/SVGRenderSupport.h"
28 #include "core/svg/SVGGraphicsElement.h"
29 #include "core/svg/SVGUseElement.h"
33 RenderSVGTransformableContainer::RenderSVGTransformableContainer(SVGGraphicsElement* node)
34 : RenderSVGContainer(node)
35 , m_needsTransformUpdate(true)
36 , m_didTransformToRootUpdate(false)
40 static bool hasValidPredecessor(const Node* node)
43 while ((node = node->previousSibling())) {
44 if (node->isSVGElement() && toSVGElement(node)->isValid())
50 bool RenderSVGTransformableContainer::isChildAllowed(RenderObject* child, RenderStyle* style) const
52 if (element()->hasTagName(SVGNames::switchTag)) {
53 Node* node = child->node();
54 // Reject non-SVG/non-valid elements.
55 if (!node->isSVGElement() || !toSVGElement(node)->isValid())
57 // Reject this child if it isn't the first valid node.
58 if (hasValidPredecessor(node))
60 } else if (element()->hasTagName(SVGNames::aTag)) {
61 // http://www.w3.org/2003/01/REC-SVG11-20030114-errata#linking-text-environment
62 // The 'a' element may contain any element that its parent may contain, except itself.
63 if (child->node()->hasTagName(SVGNames::aTag))
65 if (parent() && parent()->isSVG())
66 return parent()->isChildAllowed(child, style);
68 return RenderSVGContainer::isChildAllowed(child, style);
71 bool RenderSVGTransformableContainer::calculateLocalTransform()
73 SVGGraphicsElement* element = toSVGGraphicsElement(this->element());
75 // If we're either the renderer for a <use> element, or for any <g> element inside the shadow
76 // tree, that was created during the use/symbol/svg expansion in SVGUseElement. These containers
77 // need to respect the translations induced by their corresponding use elements x/y attributes.
78 SVGUseElement* useElement = 0;
79 if (element->hasTagName(SVGNames::useTag))
80 useElement = toSVGUseElement(element);
81 else if (element->isInShadowTree() && element->hasTagName(SVGNames::gTag)) {
82 SVGElement* correspondingElement = element->correspondingElement();
83 if (correspondingElement && correspondingElement->hasTagName(SVGNames::useTag))
84 useElement = toSVGUseElement(correspondingElement);
88 SVGLengthContext lengthContext(useElement);
89 FloatSize translation(
90 useElement->x()->currentValue()->value(lengthContext),
91 useElement->y()->currentValue()->value(lengthContext));
92 if (translation != m_lastTranslation)
93 m_needsTransformUpdate = true;
94 m_lastTranslation = translation;
97 m_didTransformToRootUpdate = m_needsTransformUpdate || SVGRenderSupport::transformToRootChanged(parent());
98 if (!m_needsTransformUpdate)
101 m_localTransform = element->animatedLocalTransform();
102 m_localTransform.translate(m_lastTranslation.width(), m_lastTranslation.height());
103 m_needsTransformUpdate = false;