Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / RenderSVGTransformableContainer.cpp
1 /*
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.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
24 #include "core/rendering/svg/RenderSVGTransformableContainer.h"
25
26 #include "SVGNames.h"
27 #include "core/rendering/svg/SVGRenderSupport.h"
28 #include "core/svg/SVGGraphicsElement.h"
29 #include "core/svg/SVGUseElement.h"
30
31 namespace WebCore {
32
33 RenderSVGTransformableContainer::RenderSVGTransformableContainer(SVGGraphicsElement* node)
34     : RenderSVGContainer(node)
35     , m_needsTransformUpdate(true)
36     , m_didTransformToRootUpdate(false)
37 {
38 }
39
40 static bool hasValidPredecessor(const Node* node)
41 {
42     ASSERT(node);
43     while ((node = node->previousSibling())) {
44         if (node->isSVGElement() && toSVGElement(node)->isValid())
45             return true;
46     }
47     return false;
48 }
49
50 bool RenderSVGTransformableContainer::isChildAllowed(RenderObject* child, RenderStyle* style) const
51 {
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())
56             return false;
57         // Reject this child if it isn't the first valid node.
58         if (hasValidPredecessor(node))
59             return false;
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))
64             return false;
65         if (parent() && parent()->isSVG())
66             return parent()->isChildAllowed(child, style);
67     }
68     return RenderSVGContainer::isChildAllowed(child, style);
69 }
70
71 bool RenderSVGTransformableContainer::calculateLocalTransform()
72 {
73     SVGGraphicsElement* element = toSVGGraphicsElement(this->element());
74
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);
85     }
86
87     if (useElement) {
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;
95     }
96
97     m_didTransformToRootUpdate = m_needsTransformUpdate || SVGRenderSupport::transformToRootChanged(parent());
98     if (!m_needsTransformUpdate)
99         return false;
100
101     m_localTransform = element->animatedLocalTransform();
102     m_localTransform.translate(m_lastTranslation.width(), m_lastTranslation.height());
103     m_needsTransformUpdate = false;
104     return true;
105 }
106
107 }