1 /* 2 * Copyright (C) 2011 University of Szeged 3 * Copyright (C) 2011 Renata Hodovan <reni (at) webkit.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "config.h" 29 30 #include "core/rendering/svg/RenderSVGRect.h" 31 32 #include "SVGNames.h" 33 34 namespace WebCore { 35 36 RenderSVGRect::RenderSVGRect(SVGRectElement* node) 37 : RenderSVGShape(node) 38 , m_usePathFallback(false) 39 { 40 } 41 42 RenderSVGRect::~RenderSVGRect() 43 { 44 } 45 46 void RenderSVGRect::updateShapeFromElement() 47 { 48 // Before creating a new object we need to clear the cached bounding box 49 // to avoid using garbage. 50 m_fillBoundingBox = FloatRect(); 51 m_innerStrokeRect = FloatRect(); 52 m_outerStrokeRect = FloatRect(); 53 SVGRectElement* rect = toSVGRectElement(node()); 54 ASSERT(rect); 55 56 SVGLengthContext lengthContext(rect); 57 // Fallback to RenderSVGShape if rect has rounded corners or a non-scaling stroke. 58 if (rect->rxCurrentValue().value(lengthContext) > 0 || rect->ryCurrentValue().value(lengthContext) > 0 || hasNonScalingStroke()) { 59 RenderSVGShape::updateShapeFromElement(); 60 m_usePathFallback = true; 61 return; 62 } 63 64 m_usePathFallback = false; 65 FloatSize boundingBoxSize(rect->widthCurrentValue().value(lengthContext), rect->heightCurrentValue().value(lengthContext)); 66 if (boundingBoxSize.isEmpty()) 67 return; 68 69 m_fillBoundingBox = FloatRect(FloatPoint(rect->xCurrentValue().value(lengthContext), rect->yCurrentValue().value(lengthContext)), boundingBoxSize); 70 71 // To decide if the stroke contains a point we create two rects which represent the inner and 72 // the outer stroke borders. A stroke contains the point, if the point is between them. 73 m_innerStrokeRect = m_fillBoundingBox; 74 m_outerStrokeRect = m_fillBoundingBox; 75 76 if (style()->svgStyle()->hasStroke()) { 77 float strokeWidth = this->strokeWidth(); 78 m_innerStrokeRect.inflate(-strokeWidth / 2); 79 m_outerStrokeRect.inflate(strokeWidth / 2); 80 } 81 82 m_strokeBoundingBox = m_outerStrokeRect; 83 } 84 85 void RenderSVGRect::fillShape(GraphicsContext* context) const 86 { 87 if (m_usePathFallback) { 88 RenderSVGShape::fillShape(context); 89 return; 90 } 91 92 context->fillRect(m_fillBoundingBox); 93 } 94 95 void RenderSVGRect::strokeShape(GraphicsContext* context) const 96 { 97 if (!style()->svgStyle()->hasVisibleStroke()) 98 return; 99 100 if (m_usePathFallback) { 101 RenderSVGShape::strokeShape(context); 102 return; 103 } 104 105 context->strokeRect(m_fillBoundingBox, strokeWidth()); 106 } 107 108 bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point) 109 { 110 // The optimized contains code below does not support non-smooth strokes so we need 111 // to fall back to RenderSVGShape::shapeDependentStrokeContains in these cases. 112 if (m_usePathFallback || !hasSmoothStroke()) { 113 if (!hasPath()) 114 RenderSVGShape::updateShapeFromElement(); 115 return RenderSVGShape::shapeDependentStrokeContains(point); 116 } 117 118 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke); 119 } 120 121 bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const WindRule fillRule) const 122 { 123 if (m_usePathFallback) 124 return RenderSVGShape::shapeDependentFillContains(point, fillRule); 125 return m_fillBoundingBox.contains(point.x(), point.y()); 126 } 127 128 } 129