OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde
.org> |
| 3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org> |
| 4 * Copyright (C) 2007 Eric Seidel <eric@webkit.org> |
| 5 * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
| 6 * |
| 7 * This library is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Library General Public |
| 9 * License as published by the Free Software Foundation; either |
| 10 * version 2 of the License, or (at your option) any later version. |
| 11 * |
| 12 * This library is distributed in the hope that it will be useful, |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 * Library General Public License for more details. |
| 16 * |
| 17 * You should have received a copy of the GNU Library General Public License |
| 18 * along with this library; see the file COPYING.LIB. If not, write to |
| 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 * Boston, MA 02110-1301, USA. |
| 21 */ |
| 22 |
| 23 #include "config.h" |
| 24 #include "core/svg/SVGPathQuery.h" |
| 25 |
| 26 #include "core/svg/SVGPathByteStreamSource.h" |
| 27 #include "core/svg/SVGPathConsumer.h" |
| 28 #include "core/svg/SVGPathData.h" |
| 29 #include "core/svg/SVGPathParser.h" |
| 30 #include "platform/graphics/PathTraversalState.h" |
| 31 |
| 32 namespace blink { |
| 33 |
| 34 namespace { |
| 35 |
| 36 class SVGPathTraversalState final : public SVGPathConsumer { |
| 37 public: |
| 38 SVGPathTraversalState(PathTraversalState::PathTraversalAction traversalActio
n, float desiredLength = 0) |
| 39 : m_traversalState(traversalAction) |
| 40 , m_segmentIndex(0) |
| 41 { |
| 42 m_traversalState.m_desiredLength = desiredLength; |
| 43 } |
| 44 |
| 45 unsigned segmentIndex() const { return m_segmentIndex; } |
| 46 float totalLength() const { return m_traversalState.m_totalLength; } |
| 47 FloatPoint computedPoint() const { return m_traversalState.m_current; } |
| 48 |
| 49 bool processSegment(bool hasMoreData) |
| 50 { |
| 51 m_traversalState.processSegment(); |
| 52 if (m_traversalState.m_success) |
| 53 return true; |
| 54 if (hasMoreData) |
| 55 m_segmentIndex++; |
| 56 return false; |
| 57 } |
| 58 |
| 59 private: |
| 60 void emitSegment(const PathSegmentData&) override; |
| 61 |
| 62 PathTraversalState m_traversalState; |
| 63 unsigned m_segmentIndex; |
| 64 }; |
| 65 |
| 66 void SVGPathTraversalState::emitSegment(const PathSegmentData& segment) |
| 67 { |
| 68 switch (segment.command) { |
| 69 case PathSegMoveToAbs: |
| 70 m_traversalState.m_totalLength += m_traversalState.moveTo(segment.target
Point); |
| 71 break; |
| 72 case PathSegLineToAbs: |
| 73 m_traversalState.m_totalLength += m_traversalState.lineTo(segment.target
Point); |
| 74 break; |
| 75 case PathSegClosePath: |
| 76 m_traversalState.m_totalLength += m_traversalState.closeSubpath(); |
| 77 break; |
| 78 case PathSegCurveToCubicAbs: |
| 79 m_traversalState.m_totalLength += m_traversalState.cubicBezierTo(segment
.point1, segment.point2, segment.targetPoint); |
| 80 break; |
| 81 default: |
| 82 ASSERT_NOT_REACHED(); |
| 83 } |
| 84 } |
| 85 |
| 86 void executeQuery(const SVGPathByteStream& pathByteStream, SVGPathTraversalState
& traversalState) |
| 87 { |
| 88 SVGPathByteStreamSource source(pathByteStream); |
| 89 SVGPathNormalizer normalizer(&traversalState); |
| 90 |
| 91 bool hasMoreData = source.hasMoreData(); |
| 92 while (hasMoreData) { |
| 93 PathSegmentData segment = source.parseSegment(); |
| 94 ASSERT(segment.command != PathSegUnknown); |
| 95 |
| 96 normalizer.emitSegment(segment); |
| 97 |
| 98 hasMoreData = source.hasMoreData(); |
| 99 if (traversalState.processSegment(hasMoreData)) |
| 100 break; |
| 101 } |
| 102 } |
| 103 |
| 104 } |
| 105 |
| 106 SVGPathQuery::SVGPathQuery(const SVGPathByteStream& pathByteStream) |
| 107 : m_pathByteStream(pathByteStream) |
| 108 { |
| 109 } |
| 110 |
| 111 unsigned SVGPathQuery::getPathSegIndexAtLength(float length) const |
| 112 { |
| 113 SVGPathTraversalState traversalState(PathTraversalState::TraversalSegmentAtL
ength, length); |
| 114 executeQuery(m_pathByteStream, traversalState); |
| 115 return traversalState.segmentIndex(); |
| 116 } |
| 117 |
| 118 float SVGPathQuery::getTotalLength() const |
| 119 { |
| 120 SVGPathTraversalState traversalState(PathTraversalState::TraversalTotalLengt
h); |
| 121 executeQuery(m_pathByteStream, traversalState); |
| 122 return traversalState.totalLength(); |
| 123 } |
| 124 |
| 125 FloatPoint SVGPathQuery::getPointAtLength(float length) const |
| 126 { |
| 127 SVGPathTraversalState traversalState(PathTraversalState::TraversalPointAtLen
gth, length); |
| 128 executeQuery(m_pathByteStream, traversalState); |
| 129 return traversalState.computedPoint(); |
| 130 } |
| 131 |
| 132 } |
OLD | NEW |