OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 University of Szeged | 2 * Copyright (C) 2011 University of Szeged |
3 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org> | 3 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org> |
4 * All rights reserved. | 4 * All rights reserved. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 11 matching lines...) Expand all Loading... |
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include "config.h" | 28 #include "config.h" |
29 #include "core/layout/svg/LayoutSVGRect.h" | 29 #include "core/layout/svg/LayoutSVGRect.h" |
30 | 30 |
31 #include "core/svg/SVGRectElement.h" | 31 #include "core/svg/SVGRectElement.h" |
| 32 #include "wtf/MathExtras.h" |
32 | 33 |
33 namespace blink { | 34 namespace blink { |
34 | 35 |
35 LayoutSVGRect::LayoutSVGRect(SVGRectElement* node) | 36 LayoutSVGRect::LayoutSVGRect(SVGRectElement* node) |
36 : LayoutSVGShape(node) | 37 : LayoutSVGShape(node) |
37 , m_usePathFallback(false) | 38 , m_usePathFallback(false) |
38 { | 39 { |
39 } | 40 } |
40 | 41 |
41 LayoutSVGRect::~LayoutSVGRect() | 42 LayoutSVGRect::~LayoutSVGRect() |
42 { | 43 { |
43 } | 44 } |
44 | 45 |
45 void LayoutSVGRect::updateShapeFromElement() | 46 void LayoutSVGRect::updateShapeFromElement() |
46 { | 47 { |
47 // Before creating a new object we need to clear the cached bounding box | 48 // Before creating a new object we need to clear the cached bounding box |
48 // to avoid using garbage. | 49 // to avoid using garbage. |
49 m_fillBoundingBox = FloatRect(); | 50 m_fillBoundingBox = FloatRect(); |
50 m_innerStrokeRect = FloatRect(); | 51 m_strokeBoundingBox = FloatRect(); |
51 m_outerStrokeRect = FloatRect(); | |
52 m_usePathFallback = false; | 52 m_usePathFallback = false; |
53 SVGRectElement* rect = toSVGRectElement(element()); | 53 SVGRectElement* rect = toSVGRectElement(element()); |
54 ASSERT(rect); | 54 ASSERT(rect); |
55 | 55 |
56 SVGLengthContext lengthContext(rect); | 56 SVGLengthContext lengthContext(rect); |
57 FloatSize boundingBoxSize( | 57 FloatSize boundingBoxSize( |
58 lengthContext.valueForLength(styleRef().width(), styleRef(), SVGLengthMo
de::Width), | 58 lengthContext.valueForLength(styleRef().width(), styleRef(), SVGLengthMo
de::Width), |
59 lengthContext.valueForLength(styleRef().height(), styleRef(), SVGLengthM
ode::Height)); | 59 lengthContext.valueForLength(styleRef().height(), styleRef(), SVGLengthM
ode::Height)); |
60 | 60 |
61 // Spec: "A negative value is an error." | 61 // Spec: "A negative value is an error." |
(...skipping 12 matching lines...) Expand all Loading... |
74 m_usePathFallback = true; | 74 m_usePathFallback = true; |
75 return; | 75 return; |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 m_fillBoundingBox = FloatRect( | 79 m_fillBoundingBox = FloatRect( |
80 FloatPoint( | 80 FloatPoint( |
81 lengthContext.valueForLength(styleRef().svgStyle().x(), styleRef(),
SVGLengthMode::Width), | 81 lengthContext.valueForLength(styleRef().svgStyle().x(), styleRef(),
SVGLengthMode::Width), |
82 lengthContext.valueForLength(styleRef().svgStyle().y(), styleRef(),
SVGLengthMode::Height)), | 82 lengthContext.valueForLength(styleRef().svgStyle().y(), styleRef(),
SVGLengthMode::Height)), |
83 boundingBoxSize); | 83 boundingBoxSize); |
84 | 84 m_strokeBoundingBox = m_fillBoundingBox; |
85 // To decide if the stroke contains a point we create two rects which repres
ent the inner and | 85 if (style()->svgStyle().hasStroke()) |
86 // the outer stroke borders. A stroke contains the point, if the point is be
tween them. | 86 m_strokeBoundingBox.inflate(strokeWidth() / 2); |
87 m_innerStrokeRect = m_fillBoundingBox; | |
88 m_outerStrokeRect = m_fillBoundingBox; | |
89 | |
90 if (style()->svgStyle().hasStroke()) { | |
91 float strokeWidth = this->strokeWidth(); | |
92 m_innerStrokeRect.inflate(-strokeWidth / 2); | |
93 m_outerStrokeRect.inflate(strokeWidth / 2); | |
94 } | |
95 | |
96 m_strokeBoundingBox = m_outerStrokeRect; | |
97 } | 87 } |
98 | 88 |
99 bool LayoutSVGRect::shapeDependentStrokeContains(const FloatPoint& point) | 89 bool LayoutSVGRect::shapeDependentStrokeContains(const FloatPoint& point) |
100 { | 90 { |
101 // The optimized code below does not support non-simple strokes so we need | 91 // The optimized code below does not support non-simple strokes so we need |
102 // to fall back to LayoutSVGShape::shapeDependentStrokeContains in these cas
es. | 92 // to fall back to LayoutSVGShape::shapeDependentStrokeContains in these cas
es. |
103 if (m_usePathFallback || !definitelyHasSimpleStroke()) { | 93 if (m_usePathFallback || !definitelyHasSimpleStroke()) { |
104 if (!hasPath()) | 94 if (!hasPath()) |
105 LayoutSVGShape::updateShapeFromElement(); | 95 LayoutSVGShape::updateShapeFromElement(); |
106 return LayoutSVGShape::shapeDependentStrokeContains(point); | 96 return LayoutSVGShape::shapeDependentStrokeContains(point); |
107 } | 97 } |
108 | 98 |
109 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_
innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke); | 99 const float halfStrokeWidth = strokeWidth() / 2; |
| 100 const float halfWidth = m_fillBoundingBox.width() / 2; |
| 101 const float halfHeight = m_fillBoundingBox.height() / 2; |
| 102 |
| 103 const FloatPoint fillBoundingBoxCenter = FloatPoint(m_fillBoundingBox.x() +
halfWidth, m_fillBoundingBox.y() + halfHeight); |
| 104 const float absDeltaX = std::abs(point.x() - fillBoundingBoxCenter.x()); |
| 105 const float absDeltaY = std::abs(point.y() - fillBoundingBoxCenter.y()); |
| 106 |
| 107 if (!(absDeltaX <= halfWidth + halfStrokeWidth && absDeltaY <= halfHeight +
halfStrokeWidth)) |
| 108 return false; |
| 109 |
| 110 return (halfWidth - halfStrokeWidth <= absDeltaX) |
| 111 || (halfHeight - halfStrokeWidth <= absDeltaY); |
110 } | 112 } |
111 | 113 |
112 bool LayoutSVGRect::shapeDependentFillContains(const FloatPoint& point, const Wi
ndRule fillRule) const | 114 bool LayoutSVGRect::shapeDependentFillContains(const FloatPoint& point, const Wi
ndRule fillRule) const |
113 { | 115 { |
114 if (m_usePathFallback) | 116 if (m_usePathFallback) |
115 return LayoutSVGShape::shapeDependentFillContains(point, fillRule); | 117 return LayoutSVGShape::shapeDependentFillContains(point, fillRule); |
116 return m_fillBoundingBox.contains(point.x(), point.y()); | 118 return m_fillBoundingBox.contains(point.x(), point.y()); |
117 } | 119 } |
118 | 120 |
119 // Returns true if the stroke is continuous and definitely uses miter joins. | 121 // Returns true if the stroke is continuous and definitely uses miter joins. |
(...skipping 16 matching lines...) Expand all Loading... |
136 // An approximation of sqrt(2) is used here because at certain precise | 138 // An approximation of sqrt(2) is used here because at certain precise |
137 // miterlimits, the join style used might not be correct (e.g. a miterlimit | 139 // miterlimits, the join style used might not be correct (e.g. a miterlimit |
138 // of 1.4142135 should result in bevel joins, but may be drawn using miter | 140 // of 1.4142135 should result in bevel joins, but may be drawn using miter |
139 // joins). | 141 // joins). |
140 return svgStyle.strokeDashArray()->isEmpty() | 142 return svgStyle.strokeDashArray()->isEmpty() |
141 && svgStyle.joinStyle() == MiterJoin | 143 && svgStyle.joinStyle() == MiterJoin |
142 && svgStyle.strokeMiterLimit() >= 1.5; | 144 && svgStyle.strokeMiterLimit() >= 1.5; |
143 } | 145 } |
144 | 146 |
145 } | 147 } |
OLD | NEW |