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.
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/RenderSVGResourceMarker.h"
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"
31 #include "wtf/TemporaryChange.h"
35 const RenderSVGResourceType RenderSVGResourceMarker::s_resourceType = MarkerResourceType;
37 RenderSVGResourceMarker::RenderSVGResourceMarker(SVGMarkerElement* node)
38 : RenderSVGResourceContainer(node)
42 RenderSVGResourceMarker::~RenderSVGResourceMarker()
46 void RenderSVGResourceMarker::layout()
48 ASSERT(needsLayout());
52 LayoutRectRecorder recorder(*this);
53 TemporaryChange<bool> inLayoutChange(m_isInLayout, true);
55 // RenderSVGHiddenContainer overwrites layout(). We need the
56 // layouting of RenderSVGContainer for calculating local
57 // transformations and repaint.
58 RenderSVGContainer::layout();
60 clearInvalidationMask();
63 void RenderSVGResourceMarker::removeAllClientsFromCache(bool markForInvalidation)
65 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
68 void RenderSVGResourceMarker::removeClientFromCache(RenderObject* client, bool markForInvalidation)
71 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
74 void RenderSVGResourceMarker::applyViewportClip(PaintInfo& paintInfo)
76 if (SVGRenderSupport::isOverflowHidden(this))
77 paintInfo.context->clip(m_viewport);
80 FloatRect RenderSVGResourceMarker::markerBoundaries(const AffineTransform& markerTransformation) const
82 FloatRect coordinates = RenderSVGContainer::repaintRectInLocalCoordinates();
84 // Map repaint rect into parent coordinate space, in which the marker boundaries have to be evaluated
85 coordinates = localToParentTransform().mapRect(coordinates);
87 return markerTransformation.mapRect(coordinates);
90 const AffineTransform& RenderSVGResourceMarker::localToParentTransform() const
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();
98 FloatPoint RenderSVGResourceMarker::referencePoint() const
100 SVGMarkerElement* marker = toSVGMarkerElement(element());
103 SVGLengthContext lengthContext(marker);
104 return FloatPoint(marker->refX()->currentValue()->value(lengthContext), marker->refY()->currentValue()->value(lengthContext));
107 float RenderSVGResourceMarker::angle() const
109 SVGMarkerElement* marker = toSVGMarkerElement(element());
113 if (marker->orientTypeCurrentValue() == SVGMarkerOrientAngle)
114 angle = marker->orientAngleCurrentValue().value();
119 AffineTransform RenderSVGResourceMarker::markerTransformation(const FloatPoint& origin, float autoAngle, float strokeWidth) const
121 SVGMarkerElement* marker = toSVGMarkerElement(element());
124 float markerAngle = angle();
125 bool useStrokeWidth = marker->markerUnitsCurrentValue() == SVGMarkerUnitsStrokeWidth;
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);
134 void RenderSVGResourceMarker::draw(PaintInfo& paintInfo, const AffineTransform& transform)
136 clearInvalidationMask();
138 // An empty viewBox disables rendering.
139 SVGMarkerElement* marker = toSVGMarkerElement(element());
141 if (marker->hasAttribute(SVGNames::viewBoxAttr) && marker->viewBox()->currentValue()->isValid() && marker->viewBox()->currentValue()->value().isEmpty())
144 PaintInfo info(paintInfo);
145 GraphicsContextStateSaver stateSaver(*info.context, false);
146 if (!transform.isIdentity()) {
148 info.applyTransform(transform, false);
150 RenderSVGContainer::paint(info, IntPoint());
153 AffineTransform RenderSVGResourceMarker::markerContentTransformation(const AffineTransform& contentTransformation, const FloatPoint& origin, float strokeWidth) const
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);
158 AffineTransform transformation = contentTransformation;
159 if (strokeWidth != -1)
160 transformation.scaleNonUniform(strokeWidth, strokeWidth);
162 transformation.translate(-mappedOrigin.x(), -mappedOrigin.y());
163 return transformation;
166 AffineTransform RenderSVGResourceMarker::viewportTransform() const
168 SVGMarkerElement* marker = toSVGMarkerElement(element());
171 return marker->viewBoxToViewTransform(m_viewport.width(), m_viewport.height());
174 void RenderSVGResourceMarker::calcViewport()
176 if (!selfNeedsLayout())
179 SVGMarkerElement* marker = toSVGMarkerElement(element());
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);