Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / RenderSVGViewportContainer.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2007 Rob Buis <buis@kde.org>
4  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5  * Copyright (C) 2009 Google, Inc.
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 #include "core/rendering/svg/RenderSVGViewportContainer.h"
26
27 #include "SVGNames.h"
28 #include "core/svg/SVGElementInstance.h"
29 #include "core/svg/SVGSVGElement.h"
30 #include "core/svg/SVGUseElement.h"
31 #include "platform/graphics/GraphicsContext.h"
32
33 namespace WebCore {
34
35 RenderSVGViewportContainer::RenderSVGViewportContainer(SVGElement* node)
36     : RenderSVGContainer(node)
37     , m_didTransformToRootUpdate(false)
38     , m_isLayoutSizeChanged(false)
39     , m_needsTransformUpdate(true)
40 {
41 }
42
43 void RenderSVGViewportContainer::determineIfLayoutSizeChanged()
44 {
45     ASSERT(element());
46     if (!isSVGSVGElement(*element()))
47         return;
48
49     m_isLayoutSizeChanged = toSVGSVGElement(element())->hasRelativeLengths() && selfNeedsLayout();
50 }
51
52 void RenderSVGViewportContainer::applyViewportClip(PaintInfo& paintInfo)
53 {
54     if (SVGRenderSupport::isOverflowHidden(this))
55         paintInfo.context->clip(m_viewport);
56 }
57
58 void RenderSVGViewportContainer::calcViewport()
59 {
60     SVGElement* element = this->element();
61     ASSERT(element);
62     if (!isSVGSVGElement(*element))
63         return;
64     SVGSVGElement* svg = toSVGSVGElement(element);
65     FloatRect oldViewport = m_viewport;
66
67     SVGLengthContext lengthContext(element);
68     m_viewport = FloatRect(svg->x()->currentValue()->value(lengthContext), svg->y()->currentValue()->value(lengthContext), svg->width()->currentValue()->value(lengthContext), svg->height()->currentValue()->value(lengthContext));
69
70     if (oldViewport != m_viewport) {
71         setNeedsBoundariesUpdate();
72         setNeedsTransformUpdate();
73     }
74 }
75
76 bool RenderSVGViewportContainer::calculateLocalTransform()
77 {
78     m_didTransformToRootUpdate = m_needsTransformUpdate || SVGRenderSupport::transformToRootChanged(parent());
79     if (!m_needsTransformUpdate)
80         return false;
81
82     m_localToParentTransform = AffineTransform::translation(m_viewport.x(), m_viewport.y()) * viewportTransform();
83     m_needsTransformUpdate = false;
84     return true;
85 }
86
87 AffineTransform RenderSVGViewportContainer::viewportTransform() const
88 {
89     ASSERT(element());
90     if (isSVGSVGElement(*element())) {
91         SVGSVGElement* svg = toSVGSVGElement(element());
92         return svg->viewBoxToViewTransform(m_viewport.width(), m_viewport.height());
93     }
94
95     return AffineTransform();
96 }
97
98 bool RenderSVGViewportContainer::pointIsInsideViewportClip(const FloatPoint& pointInParent)
99 {
100     // Respect the viewport clip (which is in parent coords)
101     if (!SVGRenderSupport::isOverflowHidden(this))
102         return true;
103
104     return m_viewport.contains(pointInParent);
105 }
106
107 void RenderSVGViewportContainer::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
108 {
109     ASSERT(element());
110     // An empty viewBox disables rendering.
111     if (isSVGSVGElement(*element()) && toSVGSVGElement(*element()).hasEmptyViewBox())
112         return;
113
114     RenderSVGContainer::paint(paintInfo, paintOffset);
115 }
116
117 }