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.
25 #include "RenderSVGResourceMarker.h"
27 #include "GraphicsContext.h"
28 #include "RenderSVGContainer.h"
29 #include "SVGElement.h"
30 #include "SVGMarkerElement.h"
31 #include "SVGRenderSupport.h"
32 #include "SVGStyledElement.h"
33 #include "SVGStyledTransformableElement.h"
37 RenderSVGResourceType RenderSVGResourceMarker::s_resourceType = MarkerResourceType;
39 RenderSVGResourceMarker::RenderSVGResourceMarker(SVGMarkerElement* node)
40 : RenderSVGResourceContainer(node)
44 RenderSVGResourceMarker::~RenderSVGResourceMarker()
48 void RenderSVGResourceMarker::layout()
50 // Invalidate all resources if our layout changed.
51 if (m_everHadLayout && selfNeedsLayout())
52 removeAllClientsFromCache();
54 // RenderSVGHiddenContainer overwrites layout(). We need the
55 // layouting of RenderSVGContainer for calculating local
56 // transformations and repaint.
57 RenderSVGContainer::layout();
60 void RenderSVGResourceMarker::removeAllClientsFromCache(bool markForInvalidation)
62 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
65 void RenderSVGResourceMarker::removeClientFromCache(RenderObject* client, bool markForInvalidation)
68 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
71 void RenderSVGResourceMarker::applyViewportClip(PaintInfo& paintInfo)
73 if (SVGRenderSupport::isOverflowHidden(this))
74 paintInfo.context->clip(m_viewport);
77 FloatRect RenderSVGResourceMarker::markerBoundaries(const AffineTransform& markerTransformation) const
79 FloatRect coordinates = RenderSVGContainer::repaintRectInLocalCoordinates();
81 // Map repaint rect into parent coordinate space, in which the marker boundaries have to be evaluated
82 coordinates = localToParentTransform().mapRect(coordinates);
84 return markerTransformation.mapRect(coordinates);
87 const AffineTransform& RenderSVGResourceMarker::localToParentTransform() const
89 m_localToParentTransform = AffineTransform::translation(m_viewport.x(), m_viewport.y()) * viewportTransform();
90 return m_localToParentTransform;
91 // If this class were ever given a localTransform(), then the above would read:
92 // return viewportTranslation * localTransform() * viewportTransform();
95 FloatPoint RenderSVGResourceMarker::referencePoint() const
97 SVGMarkerElement* marker = static_cast<SVGMarkerElement*>(node());
100 SVGLengthContext lengthContext(marker);
101 return FloatPoint(marker->refX().value(lengthContext), marker->refY().value(lengthContext));
104 float RenderSVGResourceMarker::angle() const
106 SVGMarkerElement* marker = static_cast<SVGMarkerElement*>(node());
110 if (marker->orientType() == SVGMarkerOrientAngle)
111 angle = marker->orientAngle().value();
116 AffineTransform RenderSVGResourceMarker::markerTransformation(const FloatPoint& origin, float autoAngle, float strokeWidth) const
118 SVGMarkerElement* marker = static_cast<SVGMarkerElement*>(node());
121 float markerAngle = angle();
122 bool useStrokeWidth = marker->markerUnits() == SVGMarkerUnitsStrokeWidth;
124 AffineTransform transform;
125 transform.translate(origin.x(), origin.y());
126 transform.rotate(markerAngle == -1 ? autoAngle : markerAngle);
127 transform = markerContentTransformation(transform, referencePoint(), useStrokeWidth ? strokeWidth : -1);
131 void RenderSVGResourceMarker::draw(PaintInfo& paintInfo, const AffineTransform& transform)
133 PaintInfo info(paintInfo);
134 GraphicsContextStateSaver stateSaver(*info.context);
135 info.applyTransform(transform);
136 RenderSVGContainer::paint(info, IntPoint());
139 AffineTransform RenderSVGResourceMarker::markerContentTransformation(const AffineTransform& contentTransformation, const FloatPoint& origin, float strokeWidth) const
141 // The 'origin' coordinate maps to SVGs refX/refY, given in coordinates relative to the viewport established by the marker
142 FloatPoint mappedOrigin = viewportTransform().mapPoint(origin);
144 AffineTransform transformation = contentTransformation;
145 if (strokeWidth != -1)
146 transformation.scaleNonUniform(strokeWidth, strokeWidth);
148 transformation.translate(-mappedOrigin.x(), -mappedOrigin.y());
149 return transformation;
152 AffineTransform RenderSVGResourceMarker::viewportTransform() const
154 SVGMarkerElement* marker = static_cast<SVGMarkerElement*>(node());
157 return marker->viewBoxToViewTransform(m_viewport.width(), m_viewport.height());
160 void RenderSVGResourceMarker::calcViewport()
162 if (!selfNeedsLayout())
165 SVGMarkerElement* marker = static_cast<SVGMarkerElement*>(node());
168 SVGLengthContext lengthContext(marker);
169 float w = marker->markerWidth().value(lengthContext);
170 float h = marker->markerHeight().value(lengthContext);
171 m_viewport = FloatRect(0, 0, w, h);
176 #endif // ENABLE(SVG)