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