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