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/svg/RenderSVGContainer.h"
27 #include "core/rendering/svg/SVGRenderSupport.h"
28 #include "platform/graphics/GraphicsContextStateSaver.h"
30 #include "wtf/TemporaryChange.h"
34 const RenderSVGResourceType RenderSVGResourceMarker::s_resourceType = MarkerResourceType;
36 RenderSVGResourceMarker::RenderSVGResourceMarker(SVGMarkerElement* node)
37 : RenderSVGResourceContainer(node)
41 RenderSVGResourceMarker::~RenderSVGResourceMarker()
45 void RenderSVGResourceMarker::layout()
47 ASSERT(needsLayout());
51 TemporaryChange<bool> inLayoutChange(m_isInLayout, true);
53 // RenderSVGHiddenContainer overwrites layout(). We need the
54 // layouting of RenderSVGContainer for calculating local
55 // transformations and repaint.
56 RenderSVGContainer::layout();
58 clearInvalidationMask();
61 void RenderSVGResourceMarker::removeAllClientsFromCache(bool markForInvalidation)
63 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
66 void RenderSVGResourceMarker::removeClientFromCache(RenderObject* client, bool markForInvalidation)
69 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
72 void RenderSVGResourceMarker::applyViewportClip(PaintInfo& paintInfo)
74 if (SVGRenderSupport::isOverflowHidden(this))
75 paintInfo.context->clip(m_viewport);
78 FloatRect RenderSVGResourceMarker::markerBoundaries(const AffineTransform& markerTransformation) const
80 FloatRect coordinates = RenderSVGContainer::repaintRectInLocalCoordinates();
82 // Map repaint rect into parent coordinate space, in which the marker boundaries have to be evaluated
83 coordinates = localToParentTransform().mapRect(coordinates);
85 return markerTransformation.mapRect(coordinates);
88 const AffineTransform& RenderSVGResourceMarker::localToParentTransform() const
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();
96 FloatPoint RenderSVGResourceMarker::referencePoint() const
98 SVGMarkerElement* marker = toSVGMarkerElement(element());
101 SVGLengthContext lengthContext(marker);
102 return FloatPoint(marker->refX()->currentValue()->value(lengthContext), marker->refY()->currentValue()->value(lengthContext));
105 float RenderSVGResourceMarker::angle() const
107 SVGMarkerElement* marker = toSVGMarkerElement(element());
111 if (marker->orientType()->currentValue()->enumValue() == SVGMarkerOrientAngle)
112 angle = marker->orientAngle()->currentValue()->value();
117 AffineTransform RenderSVGResourceMarker::markerTransformation(const FloatPoint& origin, float autoAngle, float strokeWidth) const
119 SVGMarkerElement* marker = toSVGMarkerElement(element());
122 float markerAngle = angle();
123 bool useStrokeWidth = marker->markerUnits()->currentValue()->enumValue() == SVGMarkerUnitsStrokeWidth;
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);
132 void RenderSVGResourceMarker::draw(PaintInfo& paintInfo, const AffineTransform& transform)
134 clearInvalidationMask();
136 // An empty viewBox disables rendering.
137 SVGMarkerElement* marker = toSVGMarkerElement(element());
139 if (marker->hasAttribute(SVGNames::viewBoxAttr) && marker->viewBox()->currentValue()->isValid() && marker->viewBox()->currentValue()->value().isEmpty())
142 PaintInfo info(paintInfo);
143 GraphicsContextStateSaver stateSaver(*info.context, false);
144 if (!transform.isIdentity()) {
146 info.applyTransform(transform, false);
148 RenderSVGContainer::paint(info, IntPoint());
151 AffineTransform RenderSVGResourceMarker::markerContentTransformation(const AffineTransform& contentTransformation, const FloatPoint& origin, float strokeWidth) const
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);
156 AffineTransform transformation = contentTransformation;
157 if (strokeWidth != -1)
158 transformation.scaleNonUniform(strokeWidth, strokeWidth);
160 transformation.translate(-mappedOrigin.x(), -mappedOrigin.y());
161 return transformation;
164 AffineTransform RenderSVGResourceMarker::viewportTransform() const
166 SVGMarkerElement* marker = toSVGMarkerElement(element());
169 return marker->viewBoxToViewTransform(m_viewport.width(), m_viewport.height());
172 void RenderSVGResourceMarker::calcViewport()
174 if (!selfNeedsLayout())
177 SVGMarkerElement* marker = toSVGMarkerElement(element());
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);