Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: third_party/WebKit/Source/core/svg/SVGPathQuery.cpp

Issue 1471943003: Introduce SVGPathQuery to hold SVG path query methods (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Const-qualify. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 void consumedSegment() { m_segmentIndex++; }
50
51 bool processSegment()
52 {
53 m_traversalState.processSegment();
54 return m_traversalState.m_success;
55 }
56
57 private:
58 void emitSegment(const PathSegmentData&) override;
59
60 PathTraversalState m_traversalState;
61 unsigned m_segmentIndex;
62 };
63
64 void SVGPathTraversalState::emitSegment(const PathSegmentData& segment)
65 {
66 switch (segment.command) {
67 case PathSegMoveToAbs:
68 m_traversalState.m_totalLength += m_traversalState.moveTo(segment.target Point);
69 break;
70 case PathSegLineToAbs:
71 m_traversalState.m_totalLength += m_traversalState.lineTo(segment.target Point);
72 break;
73 case PathSegClosePath:
74 m_traversalState.m_totalLength += m_traversalState.closeSubpath();
75 break;
76 case PathSegCurveToCubicAbs:
77 m_traversalState.m_totalLength += m_traversalState.cubicBezierTo(segment .point1, segment.point2, segment.targetPoint);
78 break;
79 default:
80 ASSERT_NOT_REACHED();
81 }
82 }
83
84 void executeQuery(const SVGPathByteStream& pathByteStream, SVGPathTraversalState & traversalState)
85 {
86 SVGPathByteStreamSource source(pathByteStream);
87 SVGPathNormalizer normalizer(&traversalState);
88
89 bool hasMoreData = source.hasMoreData();
90 while (hasMoreData) {
91 PathSegmentData segment = source.parseSegment();
92 ASSERT(segment.command != PathSegUnknown);
93
94 normalizer.emitSegment(segment);
95
96 if (traversalState.processSegment())
97 return;
98
99 hasMoreData = source.hasMoreData();
100 if (hasMoreData)
101 traversalState.consumedSegment();
f(malita) 2015/11/24 14:22:40 Could we get rid of consumedSegment by folding its
fs 2015/11/24 14:51:46 Done (the former, the latter would change semantic
102 }
103 }
104
105 }
106
107 SVGPathQuery::SVGPathQuery(const SVGPathByteStream& pathByteStream)
108 : m_pathByteStream(pathByteStream)
109 {
110 }
111
112 unsigned SVGPathQuery::getPathSegIndexAtLength(float length) const
113 {
114 SVGPathTraversalState traversalState(PathTraversalState::TraversalSegmentAtL ength, length);
115 executeQuery(m_pathByteStream, traversalState);
116 return traversalState.segmentIndex();
117 }
118
119 float SVGPathQuery::getTotalLength() const
120 {
121 SVGPathTraversalState traversalState(PathTraversalState::TraversalTotalLengt h);
122 executeQuery(m_pathByteStream, traversalState);
123 return traversalState.totalLength();
124 }
125
126 FloatPoint SVGPathQuery::getPointAtLength(float length) const
127 {
128 SVGPathTraversalState traversalState(PathTraversalState::TraversalPointAtLen gth, length);
129 executeQuery(m_pathByteStream, traversalState);
130 return traversalState.computedPoint();
131 }
132
133 }
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/svg/SVGPathQuery.h ('k') | third_party/WebKit/Source/core/svg/SVGPathStringBuilder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698