OLD | NEW |
| (Empty) |
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 "core/svg/SVGRectElement.h" | |
32 | |
33 namespace blink { | |
34 | |
35 RenderSVGRect::RenderSVGRect(SVGRectElement* node) | |
36 : RenderSVGShape(node) | |
37 , m_usePathFallback(false) | |
38 { | |
39 } | |
40 | |
41 RenderSVGRect::~RenderSVGRect() | |
42 { | |
43 } | |
44 | |
45 void 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 and path-based hit detection if the rect | |
66 // has rounded corners or a non-scaling or non-simple stroke. | |
67 if (rect->rx()->currentValue()->value(lengthContext) > 0 | |
68 || rect->ry()->currentValue()->value(lengthContext) > 0 | |
69 || hasNonScalingStroke() | |
70 || !definitelyHasSimpleStroke()) { | |
71 RenderSVGShape::updateShapeFromElement(); | |
72 m_usePathFallback = true; | |
73 return; | |
74 } | |
75 } | |
76 | |
77 m_fillBoundingBox = FloatRect(FloatPoint(rect->x()->currentValue()->value(le
ngthContext), rect->y()->currentValue()->value(lengthContext)), boundingBoxSize)
; | |
78 | |
79 // To decide if the stroke contains a point we create two rects which repres
ent the inner and | |
80 // the outer stroke borders. A stroke contains the point, if the point is be
tween them. | |
81 m_innerStrokeRect = m_fillBoundingBox; | |
82 m_outerStrokeRect = m_fillBoundingBox; | |
83 | |
84 if (style()->svgStyle().hasStroke()) { | |
85 float strokeWidth = this->strokeWidth(); | |
86 m_innerStrokeRect.inflate(-strokeWidth / 2); | |
87 m_outerStrokeRect.inflate(strokeWidth / 2); | |
88 } | |
89 | |
90 m_strokeBoundingBox = m_outerStrokeRect; | |
91 } | |
92 | |
93 bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point) | |
94 { | |
95 // The optimized code below does not support non-simple strokes so we need | |
96 // to fall back to RenderSVGShape::shapeDependentStrokeContains in these cas
es. | |
97 if (m_usePathFallback || !definitelyHasSimpleStroke()) { | |
98 if (!hasPath()) | |
99 RenderSVGShape::updateShapeFromElement(); | |
100 return RenderSVGShape::shapeDependentStrokeContains(point); | |
101 } | |
102 | |
103 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_
innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke); | |
104 } | |
105 | |
106 bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const Wi
ndRule fillRule) const | |
107 { | |
108 if (m_usePathFallback) | |
109 return RenderSVGShape::shapeDependentFillContains(point, fillRule); | |
110 return m_fillBoundingBox.contains(point.x(), point.y()); | |
111 } | |
112 | |
113 // Returns true if the stroke is continuous and definitely uses miter joins. | |
114 bool RenderSVGRect::definitelyHasSimpleStroke() const | |
115 { | |
116 const SVGLayoutStyle& svgStyle = style()->svgStyle(); | |
117 | |
118 // The four angles of a rect are 90 degrees. Using the formula at: | |
119 // http://www.w3.org/TR/SVG/painting.html#StrokeMiterlimitProperty | |
120 // when the join style of the rect is "miter", the ratio of the miterLength | |
121 // to the stroke-width is found to be | |
122 // miterLength / stroke-width = 1 / sin(45 degrees) | |
123 // = 1 / (1 / sqrt(2)) | |
124 // = sqrt(2) | |
125 // = 1.414213562373095... | |
126 // When sqrt(2) exceeds the miterlimit, then the join style switches to | |
127 // "bevel". When the miterlimit is greater than or equal to sqrt(2) then | |
128 // the join style remains "miter". | |
129 // | |
130 // An approximation of sqrt(2) is used here because at certain precise | |
131 // miterlimits, the join style used might not be correct (e.g. a miterlimit | |
132 // of 1.4142135 should result in bevel joins, but may be drawn using miter | |
133 // joins). | |
134 return svgStyle.strokeDashArray()->isEmpty() | |
135 && svgStyle.joinStyle() == MiterJoin | |
136 && svgStyle.strokeMiterLimit() >= 1.5; | |
137 } | |
138 | |
139 } | |
OLD | NEW |