OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Google, Inc. | |
3 * All rights reserved. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY | |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR | |
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 */ | |
26 | |
27 #include "config.h" | |
28 | |
29 #include "core/rendering/svg/RenderSVGEllipse.h" | |
30 | |
31 #include "core/svg/SVGCircleElement.h" | |
32 #include "core/svg/SVGEllipseElement.h" | |
33 | |
34 #include <cmath> | |
35 | |
36 namespace blink { | |
37 | |
38 RenderSVGEllipse::RenderSVGEllipse(SVGGraphicsElement* node) | |
39 : RenderSVGShape(node) | |
40 , m_usePathFallback(false) | |
41 { | |
42 } | |
43 | |
44 RenderSVGEllipse::~RenderSVGEllipse() | |
45 { | |
46 } | |
47 | |
48 void RenderSVGEllipse::updateShapeFromElement() | |
49 { | |
50 // Before creating a new object we need to clear the cached bounding box | |
51 // to avoid using garbage. | |
52 m_fillBoundingBox = FloatRect(); | |
53 m_strokeBoundingBox = FloatRect(); | |
54 m_center = FloatPoint(); | |
55 m_radii = FloatSize(); | |
56 m_usePathFallback = false; | |
57 | |
58 calculateRadiiAndCenter(); | |
59 | |
60 // Spec: "A negative value is an error. A value of zero disables rendering o
f the element." | |
61 if (m_radii.width() < 0 || m_radii.height() < 0) | |
62 return; | |
63 | |
64 if (!m_radii.isEmpty()) { | |
65 // Fall back to RenderSVGShape and path-based hit detection if the ellip
se | |
66 // has a non-scaling or discontinuous stroke. | |
67 if (hasNonScalingStroke() || !hasContinuousStroke()) { | |
68 RenderSVGShape::updateShapeFromElement(); | |
69 m_usePathFallback = true; | |
70 return; | |
71 } | |
72 } | |
73 | |
74 clearPath(); | |
75 | |
76 m_fillBoundingBox = FloatRect(m_center.x() - m_radii.width(), m_center.y() -
m_radii.height(), 2 * m_radii.width(), 2 * m_radii.height()); | |
77 m_strokeBoundingBox = m_fillBoundingBox; | |
78 if (style()->svgStyle().hasStroke()) | |
79 m_strokeBoundingBox.inflate(strokeWidth() / 2); | |
80 } | |
81 | |
82 void RenderSVGEllipse::calculateRadiiAndCenter() | |
83 { | |
84 ASSERT(element()); | |
85 if (isSVGCircleElement(*element())) { | |
86 SVGCircleElement& circle = toSVGCircleElement(*element()); | |
87 | |
88 SVGLengthContext lengthContext(&circle); | |
89 float radius = circle.r()->currentValue()->value(lengthContext); | |
90 m_radii = FloatSize(radius, radius); | |
91 m_center = FloatPoint(circle.cx()->currentValue()->value(lengthContext),
circle.cy()->currentValue()->value(lengthContext)); | |
92 return; | |
93 } | |
94 | |
95 SVGEllipseElement& ellipse = toSVGEllipseElement(*element()); | |
96 | |
97 SVGLengthContext lengthContext(&ellipse); | |
98 m_radii = FloatSize(ellipse.rx()->currentValue()->value(lengthContext), elli
pse.ry()->currentValue()->value(lengthContext)); | |
99 m_center = FloatPoint(ellipse.cx()->currentValue()->value(lengthContext), el
lipse.cy()->currentValue()->value(lengthContext)); | |
100 } | |
101 | |
102 bool RenderSVGEllipse::shapeDependentStrokeContains(const FloatPoint& point) | |
103 { | |
104 // The optimized check below for circles does not support non-scaling or | |
105 // discontinuous strokes. | |
106 if (m_usePathFallback | |
107 || !hasContinuousStroke() | |
108 || m_radii.width() != m_radii.height()) { | |
109 if (!hasPath()) | |
110 createPath(); | |
111 return RenderSVGShape::shapeDependentStrokeContains(point); | |
112 } | |
113 | |
114 const FloatPoint center = FloatPoint(m_center.x() - point.x(), m_center.y()
- point.y()); | |
115 const float halfStrokeWidth = strokeWidth() / 2; | |
116 const float r = m_radii.width(); | |
117 return std::abs(center.length() - r) <= halfStrokeWidth; | |
118 } | |
119 | |
120 bool RenderSVGEllipse::shapeDependentFillContains(const FloatPoint& point, const
WindRule fillRule) const | |
121 { | |
122 const FloatPoint center = FloatPoint(m_center.x() - point.x(), m_center.y()
- point.y()); | |
123 | |
124 // This works by checking if the point satisfies the ellipse equation. | |
125 // (x/rX)^2 + (y/rY)^2 <= 1 | |
126 const float xrX = center.x() / m_radii.width(); | |
127 const float yrY = center.y() / m_radii.height(); | |
128 return xrX * xrX + yrY * yrY <= 1.0; | |
129 } | |
130 | |
131 bool RenderSVGEllipse::hasContinuousStroke() const | |
132 { | |
133 const SVGLayoutStyle& svgStyle = style()->svgStyle(); | |
134 return svgStyle.strokeDashArray()->isEmpty(); | |
135 } | |
136 | |
137 } | |
OLD | NEW |