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#if ENABLE(SVG)
24#include "Path.h"
25#include <wtf/Vector.h>
26
27namespace WebCore {
28
29class SVGSubpathData {
30public:
31    SVGSubpathData(Vector<FloatPoint>& zeroLengthSubpathLocations)
32        : m_zeroLengthSubpathLocations(zeroLengthSubpathLocations)
33        , m_haveSeenMoveOnly(true)
34        , m_pathIsZeroLength(true)
35    {
36        m_lastPoint.set(0, 0);
37        m_movePoint.set(0, 0);
38    }
39
40    static void updateFromPathElement(void* info, const PathElement* element)
41    {
42        SVGSubpathData* subpathFinder = static_cast<SVGSubpathData*>(info);
43        switch (element->type) {
44        case PathElementMoveToPoint:
45            if (subpathFinder->m_pathIsZeroLength && !subpathFinder->m_haveSeenMoveOnly)
46                subpathFinder->m_zeroLengthSubpathLocations.append(subpathFinder->m_lastPoint);
47            subpathFinder->m_lastPoint = subpathFinder->m_movePoint = element->points[0];
48            subpathFinder->m_haveSeenMoveOnly = true;
49            subpathFinder->m_pathIsZeroLength = true;
50            break;
51        case PathElementAddLineToPoint:
52            if (subpathFinder->m_lastPoint != element->points[0]) {
53                subpathFinder->m_pathIsZeroLength = false;
54                subpathFinder->m_lastPoint = element->points[0];
55            }
56            subpathFinder->m_haveSeenMoveOnly = false;
57            break;
58        case PathElementAddQuadCurveToPoint:
59            if (subpathFinder->m_lastPoint != element->points[0] || element->points[0] != element->points[1]) {
60                subpathFinder->m_pathIsZeroLength = false;
61                subpathFinder->m_lastPoint = element->points[1];
62            }
63            subpathFinder->m_haveSeenMoveOnly = false;
64            break;
65        case PathElementAddCurveToPoint:
66            if (subpathFinder->m_lastPoint != element->points[0] || element->points[0] != element->points[1] || element->points[1] != element->points[2]) {
67                subpathFinder->m_pathIsZeroLength = false;
68                subpathFinder->m_lastPoint = element->points[2];
69            }
70            subpathFinder->m_haveSeenMoveOnly = false;
71            break;
72        case PathElementCloseSubpath:
73            if (subpathFinder->m_pathIsZeroLength)
74                subpathFinder->m_zeroLengthSubpathLocations.append(subpathFinder->m_lastPoint);
75            subpathFinder->m_haveSeenMoveOnly = true; // This is an implicit move for the next element
76            subpathFinder->m_pathIsZeroLength = true; // A new sub-path also starts here
77            subpathFinder->m_lastPoint = subpathFinder->m_movePoint;
78            break;
79        }
80    }
81
82    void pathIsDone()
83    {
84        if (m_pathIsZeroLength && !m_haveSeenMoveOnly)
85            m_zeroLengthSubpathLocations.append(m_lastPoint);
86    }
87
88private:
89    Vector<FloatPoint>& m_zeroLengthSubpathLocations;
90    FloatPoint m_lastPoint;
91    FloatPoint m_movePoint;
92    bool m_haveSeenMoveOnly;
93    bool m_pathIsZeroLength;
94};
95
96}
97
98#endif // ENABLE(SVG)
99#endif // SVGSubpathData_h
100
101