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 12 matching lines...) Expand all Loading... |
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 | 32 |
| 33 #include <cmath> |
| 34 |
33 namespace blink { | 35 namespace blink { |
34 | 36 |
35 LayoutSVGRect::LayoutSVGRect(SVGRectElement* node) | 37 LayoutSVGRect::LayoutSVGRect(SVGRectElement* node) |
36 : LayoutSVGShape(node) | 38 : LayoutSVGShape(node) |
37 , m_usePathFallback(false) | 39 , m_usePathFallback(false) |
38 { | 40 { |
39 } | 41 } |
40 | 42 |
41 LayoutSVGRect::~LayoutSVGRect() | 43 LayoutSVGRect::~LayoutSVGRect() |
42 { | 44 { |
43 } | 45 } |
44 | 46 |
45 void LayoutSVGRect::updateShapeFromElement() | 47 void LayoutSVGRect::updateShapeFromElement() |
46 { | 48 { |
47 // Before creating a new object we need to clear the cached bounding box | 49 // Before creating a new object we need to clear the cached bounding box |
48 // to avoid using garbage. | 50 // to avoid using garbage. |
49 m_fillBoundingBox = FloatRect(); | 51 m_fillBoundingBox = FloatRect(); |
50 m_innerStrokeRect = FloatRect(); | 52 m_strokeBoundingBox = FloatRect(); |
51 m_outerStrokeRect = FloatRect(); | |
52 m_usePathFallback = false; | 53 m_usePathFallback = false; |
53 SVGRectElement* rect = toSVGRectElement(element()); | 54 SVGRectElement* rect = toSVGRectElement(element()); |
54 ASSERT(rect); | 55 ASSERT(rect); |
55 | 56 |
56 SVGLengthContext lengthContext(rect); | 57 SVGLengthContext lengthContext(rect); |
57 FloatSize boundingBoxSize( | 58 FloatSize boundingBoxSize( |
58 lengthContext.valueForLength(styleRef().width(), styleRef(), SVGLengthMo
de::Width), | 59 lengthContext.valueForLength(styleRef().width(), styleRef(), SVGLengthMo
de::Width), |
59 lengthContext.valueForLength(styleRef().height(), styleRef(), SVGLengthM
ode::Height)); | 60 lengthContext.valueForLength(styleRef().height(), styleRef(), SVGLengthM
ode::Height)); |
60 | 61 |
61 // Spec: "A negative value is an error." | 62 // Spec: "A negative value is an error." |
(...skipping 12 matching lines...) Expand all Loading... |
74 m_usePathFallback = true; | 75 m_usePathFallback = true; |
75 return; | 76 return; |
76 } | 77 } |
77 } | 78 } |
78 | 79 |
79 m_fillBoundingBox = FloatRect( | 80 m_fillBoundingBox = FloatRect( |
80 FloatPoint( | 81 FloatPoint( |
81 lengthContext.valueForLength(styleRef().svgStyle().x(), styleRef(),
SVGLengthMode::Width), | 82 lengthContext.valueForLength(styleRef().svgStyle().x(), styleRef(),
SVGLengthMode::Width), |
82 lengthContext.valueForLength(styleRef().svgStyle().y(), styleRef(),
SVGLengthMode::Height)), | 83 lengthContext.valueForLength(styleRef().svgStyle().y(), styleRef(),
SVGLengthMode::Height)), |
83 boundingBoxSize); | 84 boundingBoxSize); |
84 | 85 m_strokeBoundingBox = m_fillBoundingBox; |
85 // To decide if the stroke contains a point we create two rects which repres
ent the inner and | 86 if (style()->svgStyle().hasStroke()) |
86 // the outer stroke borders. A stroke contains the point, if the point is be
tween them. | 87 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 } | 88 } |
98 | 89 |
99 bool LayoutSVGRect::shapeDependentStrokeContains(const FloatPoint& point) | 90 bool LayoutSVGRect::shapeDependentStrokeContains(const FloatPoint& point) |
100 { | 91 { |
101 // The optimized code below does not support non-simple strokes so we need | 92 // The optimized code below does not support non-simple strokes so we need |
102 // to fall back to LayoutSVGShape::shapeDependentStrokeContains in these cas
es. | 93 // to fall back to LayoutSVGShape::shapeDependentStrokeContains in these cas
es. |
103 if (m_usePathFallback || !definitelyHasSimpleStroke()) { | 94 if (m_usePathFallback || !definitelyHasSimpleStroke()) { |
104 if (!hasPath()) | 95 if (!hasPath()) |
105 LayoutSVGShape::updateShapeFromElement(); | 96 LayoutSVGShape::updateShapeFromElement(); |
106 return LayoutSVGShape::shapeDependentStrokeContains(point); | 97 return LayoutSVGShape::shapeDependentStrokeContains(point); |
107 } | 98 } |
108 | 99 |
109 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_
innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke); | 100 const float halfStrokeWidth = strokeWidth() / 2; |
| 101 const float halfWidth = m_fillBoundingBox.width() / 2; |
| 102 const float halfHeight = m_fillBoundingBox.height() / 2; |
| 103 |
| 104 const FloatPoint fillBoundingBoxCenter = FloatPoint(m_fillBoundingBox.x() +
halfWidth, m_fillBoundingBox.y() + halfHeight); |
| 105 const float absDeltaX = std::abs(point.x() - fillBoundingBoxCenter.x()); |
| 106 const float absDeltaY = std::abs(point.y() - fillBoundingBoxCenter.y()); |
| 107 |
| 108 if (!(absDeltaX <= halfWidth + halfStrokeWidth && absDeltaY <= halfHeight +
halfStrokeWidth)) |
| 109 return false; |
| 110 |
| 111 return (halfWidth - halfStrokeWidth <= absDeltaX) |
| 112 || (halfHeight - halfStrokeWidth <= absDeltaY); |
110 } | 113 } |
111 | 114 |
112 bool LayoutSVGRect::shapeDependentFillContains(const FloatPoint& point, const Wi
ndRule fillRule) const | 115 bool LayoutSVGRect::shapeDependentFillContains(const FloatPoint& point, const Wi
ndRule fillRule) const |
113 { | 116 { |
114 if (m_usePathFallback) | 117 if (m_usePathFallback) |
115 return LayoutSVGShape::shapeDependentFillContains(point, fillRule); | 118 return LayoutSVGShape::shapeDependentFillContains(point, fillRule); |
116 return m_fillBoundingBox.contains(point.x(), point.y()); | 119 return m_fillBoundingBox.contains(point.x(), point.y()); |
117 } | 120 } |
118 | 121 |
119 // Returns true if the stroke is continuous and definitely uses miter joins. | 122 // 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 | 139 // 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 | 140 // 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 | 141 // of 1.4142135 should result in bevel joins, but may be drawn using miter |
139 // joins). | 142 // joins). |
140 return svgStyle.strokeDashArray()->isEmpty() | 143 return svgStyle.strokeDashArray()->isEmpty() |
141 && svgStyle.joinStyle() == MiterJoin | 144 && svgStyle.joinStyle() == MiterJoin |
142 && svgStyle.strokeMiterLimit() >= 1.5; | 145 && svgStyle.strokeMiterLimit() >= 1.5; |
143 } | 146 } |
144 | 147 |
145 } | 148 } |
OLD | NEW |