Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / RenderSVGPath.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2008 Rob Buis <buis@kde.org>
4  * Copyright (C) 2005, 2007 Eric Seidel <eric@webkit.org>
5  * Copyright (C) 2009 Google, Inc.
6  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
7  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
8  * Copyright (C) 2009 Jeff Schiller <codedread@gmail.com>
9  * Copyright (C) 2011 Renata Hodovan <reni@webkit.org>
10  * Copyright (C) 2011 University of Szeged
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public License
23  * along with this library; see the file COPYING.LIB.  If not, write to
24  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  * Boston, MA 02110-1301, USA.
26  */
27
28 #include "config.h"
29
30 #include "core/rendering/svg/RenderSVGPath.h"
31
32 #include "core/rendering/svg/RenderSVGResourceMarker.h"
33 #include "core/rendering/svg/SVGResources.h"
34 #include "core/rendering/svg/SVGResourcesCache.h"
35 #include "core/rendering/svg/SVGSubpathData.h"
36 #include "core/svg/SVGGraphicsElement.h"
37
38 namespace blink {
39
40 RenderSVGPath::RenderSVGPath(SVGGraphicsElement* node)
41     : RenderSVGShape(node)
42 {
43 }
44
45 RenderSVGPath::~RenderSVGPath()
46 {
47 }
48
49 void RenderSVGPath::updateShapeFromElement()
50 {
51     RenderSVGShape::updateShapeFromElement();
52     updateZeroLengthSubpaths();
53
54     m_strokeBoundingBox = calculateUpdatedStrokeBoundingBox();
55 }
56
57 FloatRect RenderSVGPath::calculateUpdatedStrokeBoundingBox() const
58 {
59     FloatRect strokeBoundingBox = m_strokeBoundingBox;
60
61     if (!m_markerPositions.isEmpty())
62         strokeBoundingBox.unite(markerRect(strokeWidth()));
63
64     if (style()->svgStyle().hasStroke()) {
65         // FIXME: zero-length subpaths do not respect vector-effect = non-scaling-stroke.
66         float strokeWidth = this->strokeWidth();
67         for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i)
68             strokeBoundingBox.unite(zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWidth));
69     }
70
71     return strokeBoundingBox;
72 }
73
74 bool RenderSVGPath::shapeDependentStrokeContains(const FloatPoint& point)
75 {
76     if (RenderSVGShape::shapeDependentStrokeContains(point))
77         return true;
78
79     const SVGRenderStyle& svgStyle = style()->svgStyle();
80     for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) {
81         ASSERT(svgStyle.hasStroke());
82         float strokeWidth = this->strokeWidth();
83         if (svgStyle.capStyle() == SquareCap) {
84             if (zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWidth).contains(point))
85                 return true;
86         } else {
87             ASSERT(svgStyle.capStyle() == RoundCap);
88             FloatPoint radiusVector(point.x() - m_zeroLengthLinecapLocations[i].x(), point.y() -  m_zeroLengthLinecapLocations[i].y());
89             if (radiusVector.lengthSquared() < strokeWidth * strokeWidth * .25f)
90                 return true;
91         }
92     }
93     return false;
94 }
95
96 bool RenderSVGPath::shouldStrokeZeroLengthSubpath() const
97 {
98     // Spec(11.4): Any zero length subpath shall not be stroked if the "stroke-linecap" property has a value of butt
99     // but shall be stroked if the "stroke-linecap" property has a value of round or square
100     return style()->svgStyle().hasStroke() && style()->svgStyle().capStyle() != ButtCap;
101 }
102
103 FloatRect RenderSVGPath::zeroLengthSubpathRect(const FloatPoint& linecapPosition, float strokeWidth)
104 {
105     return FloatRect(linecapPosition.x() - strokeWidth / 2, linecapPosition.y() - strokeWidth / 2, strokeWidth, strokeWidth);
106 }
107
108 void RenderSVGPath::updateZeroLengthSubpaths()
109 {
110     m_zeroLengthLinecapLocations.clear();
111
112     if (!strokeWidth() || !shouldStrokeZeroLengthSubpath())
113         return;
114
115     SVGSubpathData subpathData(m_zeroLengthLinecapLocations);
116     path().apply(&subpathData, SVGSubpathData::updateFromPathElement);
117     subpathData.pathIsDone();
118 }
119
120 FloatRect RenderSVGPath::markerRect(float strokeWidth) const
121 {
122     ASSERT(!m_markerPositions.isEmpty());
123
124     SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
125     ASSERT(resources);
126
127     RenderSVGResourceMarker* markerStart = resources->markerStart();
128     RenderSVGResourceMarker* markerMid = resources->markerMid();
129     RenderSVGResourceMarker* markerEnd = resources->markerEnd();
130     ASSERT(markerStart || markerMid || markerEnd);
131
132     FloatRect boundaries;
133     unsigned size = m_markerPositions.size();
134     for (unsigned i = 0; i < size; ++i) {
135         if (RenderSVGResourceMarker* marker = SVGMarkerData::markerForType(m_markerPositions[i].type, markerStart, markerMid, markerEnd))
136             boundaries.unite(marker->markerBoundaries(marker->markerTransformation(m_markerPositions[i].origin, m_markerPositions[i].angle, strokeWidth)));
137     }
138     return boundaries;
139 }
140
141 bool RenderSVGPath::shouldGenerateMarkerPositions() const
142 {
143     if (!style()->svgStyle().hasMarkers())
144         return false;
145
146     if (!SVGResources::supportsMarkers(*toSVGGraphicsElement(element())))
147         return false;
148
149     SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
150     if (!resources)
151         return false;
152
153     return resources->markerStart() || resources->markerMid() || resources->markerEnd();
154 }
155
156 void RenderSVGPath::processMarkerPositions()
157 {
158     m_markerPositions.clear();
159
160     if (!shouldGenerateMarkerPositions())
161         return;
162
163     SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
164     ASSERT(resources);
165
166     RenderSVGResourceMarker* markerStart = resources->markerStart();
167
168     SVGMarkerData markerData(m_markerPositions, markerStart ? markerStart->orientType() == SVGMarkerOrientAutoStartReverse : false);
169     path().apply(&markerData, SVGMarkerData::updateFromPathElement);
170     markerData.pathIsDone();
171 }
172
173 }