2 * Copyright (C) 2011 University of Szeged
3 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
30 #include "core/rendering/svg/RenderSVGRect.h"
31 #include "core/svg/SVGRectElement.h"
35 RenderSVGRect::RenderSVGRect(SVGRectElement* node)
36 : RenderSVGShape(node)
37 , m_usePathFallback(false)
41 RenderSVGRect::~RenderSVGRect()
45 void RenderSVGRect::updateShapeFromElement()
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());
56 SVGLengthContext lengthContext(rect);
57 FloatSize boundingBoxSize(rect->width()->currentValue()->value(lengthContext), rect->height()->currentValue()->value(lengthContext));
59 // Spec: "A negative value is an error."
60 if (boundingBoxSize.width() < 0 || boundingBoxSize.height() < 0)
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;
73 m_fillBoundingBox = FloatRect(FloatPoint(rect->x()->currentValue()->value(lengthContext), rect->y()->currentValue()->value(lengthContext)), boundingBoxSize);
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;
80 if (style()->svgStyle().hasStroke()) {
81 float strokeWidth = this->strokeWidth();
82 m_innerStrokeRect.inflate(-strokeWidth / 2);
83 m_outerStrokeRect.inflate(strokeWidth / 2);
86 m_strokeBoundingBox = m_outerStrokeRect;
89 bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point)
91 // The optimized contains code below does not support non-smooth strokes so we need
92 // to fall back to RenderSVGShape::shapeDependentStrokeContains in these cases.
93 if (m_usePathFallback || !hasSmoothStroke()) {
95 RenderSVGShape::updateShapeFromElement();
96 return RenderSVGShape::shapeDependentStrokeContains(point);
99 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke);
102 bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const WindRule fillRule) const
104 if (m_usePathFallback)
105 return RenderSVGShape::shapeDependentFillContains(point, fillRule);
106 return m_fillBoundingBox.contains(point.x(), point.y());