| 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 | 29 |
| 30 #include "core/rendering/svg/RenderSVGRect.h" | 30 #include "core/rendering/svg/RenderSVGRect.h" |
| 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 RenderSVGRect::RenderSVGRect(SVGRectElement* node) | 37 RenderSVGRect::RenderSVGRect(SVGRectElement* node) |
| 36 : RenderSVGShape(node) | 38 : RenderSVGShape(node) |
| 37 , m_usePathFallback(false) | 39 , m_usePathFallback(false) |
| 38 { | 40 { |
| 39 } | 41 } |
| 40 | 42 |
| 41 RenderSVGRect::~RenderSVGRect() | 43 RenderSVGRect::~RenderSVGRect() |
| 42 { | 44 { |
| 43 } | 45 } |
| 44 | 46 |
| 45 void RenderSVGRect::updateShapeFromElement() | 47 void RenderSVGRect::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(); | 53 m_hitTestStrokeBoundingBox = FloatRect(); |
| 52 m_usePathFallback = false; | 54 m_usePathFallback = false; |
| 53 SVGRectElement* rect = toSVGRectElement(element()); | 55 SVGRectElement* rect = toSVGRectElement(element()); |
| 54 ASSERT(rect); | 56 ASSERT(rect); |
| 55 | 57 |
| 56 SVGLengthContext lengthContext(rect); | 58 SVGLengthContext lengthContext(rect); |
| 57 FloatSize boundingBoxSize(rect->width()->currentValue()->value(lengthContext
), rect->height()->currentValue()->value(lengthContext)); | 59 FloatSize boundingBoxSize(rect->width()->currentValue()->value(lengthContext
), rect->height()->currentValue()->value(lengthContext)); |
| 58 | 60 |
| 59 // Spec: "A negative value is an error." | 61 // Spec: "A negative value is an error." |
| 60 if (boundingBoxSize.width() < 0 || boundingBoxSize.height() < 0) | 62 if (boundingBoxSize.width() < 0 || boundingBoxSize.height() < 0) |
| 61 return; | 63 return; |
| 62 | 64 |
| 63 // Spec: "A value of zero disables rendering of the element." | 65 // Spec: "A value of zero disables rendering of the element." |
| 64 if (!boundingBoxSize.isEmpty()) { | 66 if (!boundingBoxSize.isEmpty()) { |
| 65 // Fallback to RenderSVGShape and path-based hit detection if the rect | 67 // Fallback to RenderSVGShape and path-based hit detection if the rect |
| 66 // has rounded corners or a non-scaling or non-simple stroke. | 68 // has rounded corners or a non-scaling or non-simple stroke. |
| 67 if (rect->rx()->currentValue()->value(lengthContext) > 0 | 69 if (rect->rx()->currentValue()->value(lengthContext) > 0 |
| 68 || rect->ry()->currentValue()->value(lengthContext) > 0 | 70 || rect->ry()->currentValue()->value(lengthContext) > 0 |
| 69 || hasNonScalingStroke() | 71 || hasNonScalingStroke() |
| 70 || !definitelyHasSimpleStroke()) { | 72 || !definitelyHasSimpleStroke()) { |
| 71 RenderSVGShape::updateShapeFromElement(); | 73 RenderSVGShape::updateShapeFromElement(); |
| 72 m_usePathFallback = true; | 74 m_usePathFallback = true; |
| 73 return; | 75 return; |
| 74 } | 76 } |
| 75 } | 77 } |
| 76 | 78 |
| 77 m_fillBoundingBox = FloatRect(FloatPoint(rect->x()->currentValue()->value(le
ngthContext), rect->y()->currentValue()->value(lengthContext)), boundingBoxSize)
; | 79 m_fillBoundingBox = FloatRect(FloatPoint(rect->x()->currentValue()->value(le
ngthContext), rect->y()->currentValue()->value(lengthContext)), boundingBoxSize)
; |
| 78 | 80 m_hitTestStrokeBoundingBox = m_fillBoundingBox; |
| 79 // To decide if the stroke contains a point we create two rects which repres
ent the inner and | 81 m_hitTestStrokeBoundingBox.inflate(strokeWidth() / 2); |
| 80 // the outer stroke borders. A stroke contains the point, if the point is be
tween them. | 82 m_strokeBoundingBox = style()->svgStyle().hasStroke() ? m_hitTestStrokeBound
ingBox : m_fillBoundingBox; |
| 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 } | 83 } |
| 92 | 84 |
| 93 bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point) | 85 bool RenderSVGRect::shapeDependentStrokeContains(const FloatPoint& point) |
| 94 { | 86 { |
| 95 // The optimized code below does not support non-simple strokes so we need | 87 // The optimized code below does not support non-simple strokes so we need |
| 96 // to fall back to RenderSVGShape::shapeDependentStrokeContains in these cas
es. | 88 // to fall back to RenderSVGShape::shapeDependentStrokeContains in these cas
es. |
| 97 if (m_usePathFallback || !definitelyHasSimpleStroke()) { | 89 if (m_usePathFallback || !definitelyHasSimpleStroke()) { |
| 98 if (!hasPath()) | 90 if (!hasPath()) |
| 99 RenderSVGShape::updateShapeFromElement(); | 91 RenderSVGShape::updateShapeFromElement(); |
| 100 return RenderSVGShape::shapeDependentStrokeContains(point); | 92 return RenderSVGShape::shapeDependentStrokeContains(point); |
| 101 } | 93 } |
| 102 | 94 |
| 103 return m_outerStrokeRect.contains(point, FloatRect::InsideOrOnStroke) && !m_
innerStrokeRect.contains(point, FloatRect::InsideButNotOnStroke); | 95 const float halfStrokeWidth = strokeWidth() / 2; |
| 96 const float halfWidth = m_fillBoundingBox.width() / 2; |
| 97 const float halfHeight = m_fillBoundingBox.height() / 2; |
| 98 |
| 99 const FloatPoint fillBoundingBoxCenter = FloatPoint(m_fillBoundingBox.x() +
halfWidth, m_fillBoundingBox.y() + halfHeight); |
| 100 const float absDeltaX = std::abs(point.x() - fillBoundingBoxCenter.x()); |
| 101 const float absDeltaY = std::abs(point.y() - fillBoundingBoxCenter.y()); |
| 102 |
| 103 if (!(absDeltaX <= halfWidth + halfStrokeWidth && absDeltaY <= halfHeight +
halfStrokeWidth)) |
| 104 return false; |
| 105 |
| 106 return (halfWidth - halfStrokeWidth <= absDeltaX) |
| 107 || (halfHeight - halfStrokeWidth <= absDeltaY); |
| 104 } | 108 } |
| 105 | 109 |
| 106 bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const Wi
ndRule fillRule) const | 110 bool RenderSVGRect::shapeDependentFillContains(const FloatPoint& point, const Wi
ndRule fillRule) const |
| 107 { | 111 { |
| 108 if (m_usePathFallback) | 112 if (m_usePathFallback) |
| 109 return RenderSVGShape::shapeDependentFillContains(point, fillRule); | 113 return RenderSVGShape::shapeDependentFillContains(point, fillRule); |
| 110 return m_fillBoundingBox.contains(point.x(), point.y()); | 114 return m_fillBoundingBox.contains(point.x(), point.y()); |
| 111 } | 115 } |
| 112 | 116 |
| 113 // Returns true if the stroke is continuous and definitely uses miter joins. | 117 // Returns true if the stroke is continuous and definitely uses miter joins. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 130 // An approximation of sqrt(2) is used here because at certain precise | 134 // 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 | 135 // 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 | 136 // of 1.4142135 should result in bevel joins, but may be drawn using miter |
| 133 // joins). | 137 // joins). |
| 134 return svgStyle.strokeDashArray()->isEmpty() | 138 return svgStyle.strokeDashArray()->isEmpty() |
| 135 && svgStyle.joinStyle() == MiterJoin | 139 && svgStyle.joinStyle() == MiterJoin |
| 136 && svgStyle.strokeMiterLimit() >= 1.5; | 140 && svgStyle.strokeMiterLimit() >= 1.5; |
| 137 } | 141 } |
| 138 | 142 |
| 139 } | 143 } |
| OLD | NEW |