1/* 2 * Copyright (C) 2011 University of Szeged 3 * Copyright (C) 2011 Renata Hodovan <reni@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#include "platform/graphics/GraphicsContext.h" 32 33namespace blink { 34 35RenderSVGRect::RenderSVGRect(SVGRectElement* node) 36 : RenderSVGShape(node) 37 , m_usePathFallback(false) 38{ 39} 40 41RenderSVGRect::~RenderSVGRect() 42{ 43} 44 45void RenderSVGRect::updateShapeFromElement() 46{ 47 // Before creating a new object we need to clear the cached bounding box 48 // to avoid using garbage. 49 m_fillBoundingBox = FloatRect(); 50 m_innerStrokeRect = FloatRect(); 51 m_outerStrokeRect = FloatRect(); 52 m_usePathFallback = false; 53 SVGRectElement* rect = toSVGRectElement(element()); 54 ASSERT(rect); 55 56 SVGLengthContext lengthContext(rect); 57 FloatSize boundingBoxSize(rect->width()->currentValue()->value(lengthContext), rect->height()->currentValue()->value(lengthContext)); 58 59 // Spec: "A negative value is an error." 60 if (boundingBoxSize.width() < 0 || boundingBoxSize.height() < 0) 61 return; 62 63 // Spec: "A value of zero disables rendering of the element." 64 if (!boundingBoxSize.isEmpty()) { 65 // Fallback to RenderSVGShape if rect has rounded corners or a non-scaling stroke. 66 if (rect->rx()->currentValue()->value(lengthContext) > 0 || rect->ry()->currentValue()->value(lengthContext) > 0 || hasNonScalingStroke()) { 67 RenderSVGShape::updateShapeFromElement(); 68 m_usePathFallback = true; 69 return; 70 } 71 } 72 73 m_fillBoundingBox = FloatRect(FloatPoint(rect->x()->currentValue()->value(lengthContext), rect->y()->currentValue()->value(lengthContext)), boundingBoxSize); 74 75 // To decide if the stroke contains a point we create two rects which represent the inner and 76 // the outer stroke borders. A stroke contains the point, if the point is between them. 77 m_innerStrokeRect = m_fillBoundingBox; 78 m_outerStrokeRect = m_fillBoundingBox; 79 80 if (style()->svgStyle().hasStroke()) { 81 float strokeWidth = this->strokeWidth(); 82 m_innerStrokeRect.inflate(-strokeWidth / 2); 83 m_outerStrokeRect.inflate(strokeWidth / 2); 84 } 85 86 m_strokeBoundingBox = m_outerStrokeRect; 87} 88 89void RenderSVGRect::fillShape(GraphicsContext* context) const 90{ 91 if (m_usePathFallback) { 92 RenderSVGShape::fillShape(context); 93 return; 94 } 95 96 context->fillRect(m_fillBoundingBox); 97} 98 99void RenderSVGRect::strokeShape(GraphicsContext* context) const 100{ 101 if (!style()->svgStyle().hasVisibleStroke()) 102 return; 103 104 if (m_usePathFallback) { 105 RenderSVGShape::strokeShape(context); 106 return; 107 } 108 109 context->strokeRect(m_fillBoundingBox, strokeWidth()); 110} 111 112bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point) 113{ 114 // The optimized contains code below does not support non-smooth strokes so we need 115 // to fall back to RenderSVGShape::shapeDependentStrokeContains in these cases. 116 if (m_usePathFallback || !hasSmoothStroke()) { 117 if (!hasPath()) 118 RenderSVGShape::updateShapeFromElement(); 119 return RenderSVGShape::shapeDependentStrokeContains(point); 120 } 121 122 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke); 123} 124 125bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const WindRule fillRule) const 126{ 127 if (m_usePathFallback) 128 return RenderSVGShape::shapeDependentFillContains(point, fillRule); 129 return m_fillBoundingBox.contains(point.x(), point.y()); 130} 131 132} 133