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