e8943caa2f6c8de445574a578a06a9bacf3f0849
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / RenderSVGResourceMarker.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis@kde.org>
4  * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
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/RenderSVGResourceMarker.h"
25
26 #include "core/rendering/LayoutRectRecorder.h"
27 #include "core/rendering/svg/RenderSVGContainer.h"
28 #include "core/rendering/svg/SVGRenderSupport.h"
29 #include "platform/graphics/GraphicsContextStateSaver.h"
30
31 #include "wtf/TemporaryChange.h"
32
33 namespace WebCore {
34
35 const RenderSVGResourceType RenderSVGResourceMarker::s_resourceType = MarkerResourceType;
36
37 RenderSVGResourceMarker::RenderSVGResourceMarker(SVGMarkerElement* node)
38     : RenderSVGResourceContainer(node)
39 {
40 }
41
42 RenderSVGResourceMarker::~RenderSVGResourceMarker()
43 {
44 }
45
46 void RenderSVGResourceMarker::layout()
47 {
48     ASSERT(needsLayout());
49     if (m_isInLayout)
50         return;
51
52     LayoutRectRecorder recorder(*this);
53     TemporaryChange<bool> inLayoutChange(m_isInLayout, true);
54
55     // RenderSVGHiddenContainer overwrites layout(). We need the
56     // layouting of RenderSVGContainer for calculating  local
57     // transformations and repaint.
58     RenderSVGContainer::layout();
59
60     clearInvalidationMask();
61 }
62
63 void RenderSVGResourceMarker::removeAllClientsFromCache(bool markForInvalidation)
64 {
65     markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
66 }
67
68 void RenderSVGResourceMarker::removeClientFromCache(RenderObject* client, bool markForInvalidation)
69 {
70     ASSERT(client);
71     markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
72 }
73
74 void RenderSVGResourceMarker::applyViewportClip(PaintInfo& paintInfo)
75 {
76     if (SVGRenderSupport::isOverflowHidden(this))
77         paintInfo.context->clip(m_viewport);
78 }
79
80 FloatRect RenderSVGResourceMarker::markerBoundaries(const AffineTransform& markerTransformation) const
81 {
82     FloatRect coordinates = RenderSVGContainer::repaintRectInLocalCoordinates();
83
84     // Map repaint rect into parent coordinate space, in which the marker boundaries have to be evaluated
85     coordinates = localToParentTransform().mapRect(coordinates);
86
87     return markerTransformation.mapRect(coordinates);
88 }
89
90 const AffineTransform& RenderSVGResourceMarker::localToParentTransform() const
91 {
92     m_localToParentTransform = AffineTransform::translation(m_viewport.x(), m_viewport.y()) * viewportTransform();
93     return m_localToParentTransform;
94     // If this class were ever given a localTransform(), then the above would read:
95     // return viewportTranslation * localTransform() * viewportTransform();
96 }
97
98 FloatPoint RenderSVGResourceMarker::referencePoint() const
99 {
100     SVGMarkerElement* marker = toSVGMarkerElement(element());
101     ASSERT(marker);
102
103     SVGLengthContext lengthContext(marker);
104     return FloatPoint(marker->refX()->currentValue()->value(lengthContext), marker->refY()->currentValue()->value(lengthContext));
105 }
106
107 float RenderSVGResourceMarker::angle() const
108 {
109     SVGMarkerElement* marker = toSVGMarkerElement(element());
110     ASSERT(marker);
111
112     float angle = -1;
113     if (marker->orientTypeCurrentValue() == SVGMarkerOrientAngle)
114         angle = marker->orientAngleCurrentValue().value();
115
116     return angle;
117 }
118
119 AffineTransform RenderSVGResourceMarker::markerTransformation(const FloatPoint& origin, float autoAngle, float strokeWidth) const
120 {
121     SVGMarkerElement* marker = toSVGMarkerElement(element());
122     ASSERT(marker);
123
124     float markerAngle = angle();
125     bool useStrokeWidth = marker->markerUnitsCurrentValue() == SVGMarkerUnitsStrokeWidth;
126
127     AffineTransform transform;
128     transform.translate(origin.x(), origin.y());
129     transform.rotate(markerAngle == -1 ? autoAngle : markerAngle);
130     transform = markerContentTransformation(transform, referencePoint(), useStrokeWidth ? strokeWidth : -1);
131     return transform;
132 }
133
134 void RenderSVGResourceMarker::draw(PaintInfo& paintInfo, const AffineTransform& transform)
135 {
136     clearInvalidationMask();
137
138     // An empty viewBox disables rendering.
139     SVGMarkerElement* marker = toSVGMarkerElement(element());
140     ASSERT(marker);
141     if (marker->hasAttribute(SVGNames::viewBoxAttr) && marker->viewBox()->currentValue()->isValid() && marker->viewBox()->currentValue()->value().isEmpty())
142         return;
143
144     PaintInfo info(paintInfo);
145     GraphicsContextStateSaver stateSaver(*info.context, false);
146     if (!transform.isIdentity()) {
147         stateSaver.save();
148         info.applyTransform(transform, false);
149     }
150     RenderSVGContainer::paint(info, IntPoint());
151 }
152
153 AffineTransform RenderSVGResourceMarker::markerContentTransformation(const AffineTransform& contentTransformation, const FloatPoint& origin, float strokeWidth) const
154 {
155     // The 'origin' coordinate maps to SVGs refX/refY, given in coordinates relative to the viewport established by the marker
156     FloatPoint mappedOrigin = viewportTransform().mapPoint(origin);
157
158     AffineTransform transformation = contentTransformation;
159     if (strokeWidth != -1)
160         transformation.scaleNonUniform(strokeWidth, strokeWidth);
161
162     transformation.translate(-mappedOrigin.x(), -mappedOrigin.y());
163     return transformation;
164 }
165
166 AffineTransform RenderSVGResourceMarker::viewportTransform() const
167 {
168     SVGMarkerElement* marker = toSVGMarkerElement(element());
169     ASSERT(marker);
170
171     return marker->viewBoxToViewTransform(m_viewport.width(), m_viewport.height());
172 }
173
174 void RenderSVGResourceMarker::calcViewport()
175 {
176     if (!selfNeedsLayout())
177         return;
178
179     SVGMarkerElement* marker = toSVGMarkerElement(element());
180     ASSERT(marker);
181
182     SVGLengthContext lengthContext(marker);
183     float w = marker->markerWidth()->currentValue()->value(lengthContext);
184     float h = marker->markerHeight()->currentValue()->value(lengthContext);
185     m_viewport = FloatRect(0, 0, w, h);
186 }
187
188 }