1 /* 2 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann (at) kde.org> 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis (at) 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/platform/graphics/GraphicsContextStateSaver.h" 27 #include "core/rendering/svg/RenderSVGContainer.h" 28 #include "core/rendering/svg/RenderSVGRoot.h" 29 #include "core/rendering/svg/SVGRenderSupport.h" 30 #include "core/svg/SVGElement.h" 31 #include "core/svg/SVGMarkerElement.h" 32 33 namespace WebCore { 34 35 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 StackStats::LayoutCheckPoint layoutCheckPoint; 49 // Invalidate all resources if our layout changed. 50 if (everHadLayout() && selfNeedsLayout()) 51 RenderSVGRoot::addResourceForClientInvalidation(this); 52 53 // RenderSVGHiddenContainer overwrites layout(). We need the 54 // layouting of RenderSVGContainer for calculating local 55 // transformations and repaint. 56 RenderSVGContainer::layout(); 57 } 58 59 void RenderSVGResourceMarker::removeAllClientsFromCache(bool markForInvalidation) 60 { 61 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation); 62 } 63 64 void RenderSVGResourceMarker::removeClientFromCache(RenderObject* client, bool markForInvalidation) 65 { 66 ASSERT(client); 67 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation); 68 } 69 70 void RenderSVGResourceMarker::applyViewportClip(PaintInfo& paintInfo) 71 { 72 if (SVGRenderSupport::isOverflowHidden(this)) 73 paintInfo.context->clip(m_viewport); 74 } 75 76 FloatRect RenderSVGResourceMarker::markerBoundaries(const AffineTransform& markerTransformation) const 77 { 78 FloatRect coordinates = RenderSVGContainer::repaintRectInLocalCoordinates(); 79 80 // Map repaint rect into parent coordinate space, in which the marker boundaries have to be evaluated 81 coordinates = localToParentTransform().mapRect(coordinates); 82 83 return markerTransformation.mapRect(coordinates); 84 } 85 86 const AffineTransform& RenderSVGResourceMarker::localToParentTransform() const 87 { 88 m_localToParentTransform = AffineTransform::translation(m_viewport.x(), m_viewport.y()) * viewportTransform(); 89 return m_localToParentTransform; 90 // If this class were ever given a localTransform(), then the above would read: 91 // return viewportTranslation * localTransform() * viewportTransform(); 92 } 93 94 FloatPoint RenderSVGResourceMarker::referencePoint() const 95 { 96 SVGMarkerElement* marker = toSVGMarkerElement(node()); 97 ASSERT(marker); 98 99 SVGLengthContext lengthContext(marker); 100 return FloatPoint(marker->refXCurrentValue().value(lengthContext), marker->refYCurrentValue().value(lengthContext)); 101 } 102 103 float RenderSVGResourceMarker::angle() const 104 { 105 SVGMarkerElement* marker = toSVGMarkerElement(node()); 106 ASSERT(marker); 107 108 float angle = -1; 109 if (marker->orientTypeCurrentValue() == SVGMarkerOrientAngle) 110 angle = marker->orientAngleCurrentValue().value(); 111 112 return angle; 113 } 114 115 AffineTransform RenderSVGResourceMarker::markerTransformation(const FloatPoint& origin, float autoAngle, float strokeWidth) const 116 { 117 SVGMarkerElement* marker = toSVGMarkerElement(node()); 118 ASSERT(marker); 119 120 float markerAngle = angle(); 121 bool useStrokeWidth = marker->markerUnitsCurrentValue() == SVGMarkerUnitsStrokeWidth; 122 123 AffineTransform transform; 124 transform.translate(origin.x(), origin.y()); 125 transform.rotate(markerAngle == -1 ? autoAngle : markerAngle); 126 transform = markerContentTransformation(transform, referencePoint(), useStrokeWidth ? strokeWidth : -1); 127 return transform; 128 } 129 130 void RenderSVGResourceMarker::draw(PaintInfo& paintInfo, const AffineTransform& transform) 131 { 132 // An empty viewBox disables rendering. 133 SVGMarkerElement* marker = toSVGMarkerElement(toSVGElement(node())); 134 ASSERT(marker); 135 if (marker->hasAttribute(SVGNames::viewBoxAttr) && marker->viewBoxIsValid() && marker->viewBoxCurrentValue().isEmpty()) 136 return; 137 138 PaintInfo info(paintInfo); 139 GraphicsContextStateSaver stateSaver(*info.context); 140 info.applyTransform(transform); 141 RenderSVGContainer::paint(info, IntPoint()); 142 } 143 144 AffineTransform RenderSVGResourceMarker::markerContentTransformation(const AffineTransform& contentTransformation, const FloatPoint& origin, float strokeWidth) const 145 { 146 // The 'origin' coordinate maps to SVGs refX/refY, given in coordinates relative to the viewport established by the marker 147 FloatPoint mappedOrigin = viewportTransform().mapPoint(origin); 148 149 AffineTransform transformation = contentTransformation; 150 if (strokeWidth != -1) 151 transformation.scaleNonUniform(strokeWidth, strokeWidth); 152 153 transformation.translate(-mappedOrigin.x(), -mappedOrigin.y()); 154 return transformation; 155 } 156 157 AffineTransform RenderSVGResourceMarker::viewportTransform() const 158 { 159 SVGMarkerElement* marker = toSVGMarkerElement(node()); 160 ASSERT(marker); 161 162 return marker->viewBoxToViewTransform(m_viewport.width(), m_viewport.height()); 163 } 164 165 void RenderSVGResourceMarker::calcViewport() 166 { 167 if (!selfNeedsLayout()) 168 return; 169 170 SVGMarkerElement* marker = toSVGMarkerElement(node()); 171 ASSERT(marker); 172 173 SVGLengthContext lengthContext(marker); 174 float w = marker->markerWidthCurrentValue().value(lengthContext); 175 float h = marker->markerHeightCurrentValue().value(lengthContext); 176 m_viewport = FloatRect(0, 0, w, h); 177 } 178 179 } 180