Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / SVGSubpathData.h
1 /*
2  * Copyright (C) 2012 Google, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef SVGSubpathData_h
21 #define SVGSubpathData_h
22
23 #include "platform/graphics/Path.h"
24 #include "wtf/Vector.h"
25
26 namespace WebCore {
27
28 class SVGSubpathData {
29 public:
30     SVGSubpathData(Vector<FloatPoint>& zeroLengthSubpathLocations)
31         : m_zeroLengthSubpathLocations(zeroLengthSubpathLocations)
32         , m_haveSeenMoveOnly(true)
33         , m_pathIsZeroLength(true)
34     {
35         m_lastPoint.set(0, 0);
36         m_movePoint.set(0, 0);
37     }
38
39     static void updateFromPathElement(void* info, const PathElement* element)
40     {
41         SVGSubpathData* subpathFinder = static_cast<SVGSubpathData*>(info);
42         switch (element->type) {
43         case PathElementMoveToPoint:
44             if (subpathFinder->m_pathIsZeroLength && !subpathFinder->m_haveSeenMoveOnly)
45                 subpathFinder->m_zeroLengthSubpathLocations.append(subpathFinder->m_lastPoint);
46             subpathFinder->m_lastPoint = subpathFinder->m_movePoint = element->points[0];
47             subpathFinder->m_haveSeenMoveOnly = true;
48             subpathFinder->m_pathIsZeroLength = true;
49             break;
50         case PathElementAddLineToPoint:
51             if (subpathFinder->m_lastPoint != element->points[0]) {
52                 subpathFinder->m_pathIsZeroLength = false;
53                 subpathFinder->m_lastPoint = element->points[0];
54             }
55             subpathFinder->m_haveSeenMoveOnly = false;
56             break;
57         case PathElementAddQuadCurveToPoint:
58             if (subpathFinder->m_lastPoint != element->points[0] || element->points[0] != element->points[1]) {
59                 subpathFinder->m_pathIsZeroLength = false;
60                 subpathFinder->m_lastPoint = element->points[1];
61             }
62             subpathFinder->m_haveSeenMoveOnly = false;
63             break;
64         case PathElementAddCurveToPoint:
65             if (subpathFinder->m_lastPoint != element->points[0] || element->points[0] != element->points[1] || element->points[1] != element->points[2]) {
66                 subpathFinder->m_pathIsZeroLength = false;
67                 subpathFinder->m_lastPoint = element->points[2];
68             }
69             subpathFinder->m_haveSeenMoveOnly = false;
70             break;
71         case PathElementCloseSubpath:
72             if (subpathFinder->m_pathIsZeroLength)
73                 subpathFinder->m_zeroLengthSubpathLocations.append(subpathFinder->m_lastPoint);
74             subpathFinder->m_haveSeenMoveOnly = true; // This is an implicit move for the next element
75             subpathFinder->m_pathIsZeroLength = true; // A new sub-path also starts here
76             subpathFinder->m_lastPoint = subpathFinder->m_movePoint;
77             break;
78         }
79     }
80
81     void pathIsDone()
82     {
83         if (m_pathIsZeroLength && !m_haveSeenMoveOnly)
84             m_zeroLengthSubpathLocations.append(m_lastPoint);
85     }
86
87 private:
88     Vector<FloatPoint>& m_zeroLengthSubpathLocations;
89     FloatPoint m_lastPoint;
90     FloatPoint m_movePoint;
91     bool m_haveSeenMoveOnly;
92     bool m_pathIsZeroLength;
93 };
94
95 }
96
97 #endif // SVGSubpathData_h