2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
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.
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.
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.
23 #include "SVGPathParserFactory.h"
25 #include "PathTraversalState.h"
26 #include "SVGPathBlender.h"
27 #include "SVGPathBuilder.h"
28 #include "SVGPathByteStreamBuilder.h"
29 #include "SVGPathByteStreamSource.h"
30 #include "SVGPathElement.h"
31 #include "SVGPathParser.h"
32 #include "SVGPathSegListBuilder.h"
33 #include "SVGPathSegListSource.h"
34 #include "SVGPathStringBuilder.h"
35 #include "SVGPathStringSource.h"
36 #include "SVGPathTraversalStateBuilder.h"
40 static SVGPathBuilder* globalSVGPathBuilder(Path& result)
42 static SVGPathBuilder* s_builder = 0;
44 s_builder = new SVGPathBuilder;
46 s_builder->setCurrentPath(&result);
50 static SVGPathSegListBuilder* globalSVGPathSegListBuilder(SVGPathElement* element, SVGPathSegRole role, SVGPathSegList& result)
52 static SVGPathSegListBuilder* s_builder = 0;
54 s_builder = new SVGPathSegListBuilder;
56 s_builder->setCurrentSVGPathElement(element);
57 s_builder->setCurrentSVGPathSegList(result);
58 s_builder->setCurrentSVGPathSegRole(role);
62 static SVGPathByteStreamBuilder* globalSVGPathByteStreamBuilder(SVGPathByteStream* result)
64 static SVGPathByteStreamBuilder* s_builder = 0;
66 s_builder = new SVGPathByteStreamBuilder;
68 s_builder->setCurrentByteStream(result);
72 static SVGPathStringBuilder* globalSVGPathStringBuilder()
74 static SVGPathStringBuilder* s_builder = 0;
76 s_builder = new SVGPathStringBuilder;
81 static SVGPathTraversalStateBuilder* globalSVGPathTraversalStateBuilder(PathTraversalState& traversalState, float length)
83 static SVGPathTraversalStateBuilder* s_builder = 0;
85 s_builder = new SVGPathTraversalStateBuilder;
87 s_builder->setCurrentTraversalState(&traversalState);
88 s_builder->setDesiredLength(length);
92 static SVGPathParser* globalSVGPathParser(SVGPathSource* source, SVGPathConsumer* consumer)
94 static SVGPathParser* s_parser = 0;
96 s_parser = new SVGPathParser;
98 s_parser->setCurrentSource(source);
99 s_parser->setCurrentConsumer(consumer);
103 static SVGPathBlender* globalSVGPathBlender()
105 static SVGPathBlender* s_blender = 0;
107 s_blender = new SVGPathBlender;
112 SVGPathParserFactory* SVGPathParserFactory::self()
114 static SVGPathParserFactory* s_instance = 0;
116 s_instance = new SVGPathParserFactory;
121 SVGPathParserFactory::SVGPathParserFactory()
125 SVGPathParserFactory::~SVGPathParserFactory()
129 bool SVGPathParserFactory::buildPathFromString(const String& d, Path& result)
134 SVGPathBuilder* builder = globalSVGPathBuilder(result);
136 OwnPtr<SVGPathStringSource> source = SVGPathStringSource::create(d);
137 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
138 bool ok = parser->parsePathDataFromSource(NormalizedParsing);
143 bool SVGPathParserFactory::buildSVGPathByteStreamFromSVGPathSegList(const SVGPathSegList& list, OwnPtr<SVGPathByteStream>& result, PathParsingMode parsingMode)
148 result = SVGPathByteStream::create();
153 SVGPathByteStreamBuilder* builder = globalSVGPathByteStreamBuilder(result.get());
155 OwnPtr<SVGPathSegListSource> source = SVGPathSegListSource::create(list);
156 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
157 bool ok = parser->parsePathDataFromSource(parsingMode);
162 bool SVGPathParserFactory::buildPathFromByteStream(SVGPathByteStream* stream, Path& result)
165 if (stream->isEmpty())
168 SVGPathBuilder* builder = globalSVGPathBuilder(result);
170 OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
171 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
172 bool ok = parser->parsePathDataFromSource(NormalizedParsing);
177 bool SVGPathParserFactory::buildSVGPathSegListFromByteStream(SVGPathByteStream* stream, SVGPathElement* element, SVGPathSegList& result, PathParsingMode parsingMode)
180 if (stream->isEmpty())
183 SVGPathSegListBuilder* builder = globalSVGPathSegListBuilder(element, parsingMode == NormalizedParsing ? PathSegNormalizedRole : PathSegUnalteredRole, result);
185 OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
186 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
187 bool ok = parser->parsePathDataFromSource(parsingMode);
192 bool SVGPathParserFactory::buildStringFromByteStream(SVGPathByteStream* stream, String& result, PathParsingMode parsingMode)
195 if (stream->isEmpty())
198 SVGPathStringBuilder* builder = globalSVGPathStringBuilder();
200 OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
201 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
202 bool ok = parser->parsePathDataFromSource(parsingMode);
203 result = builder->result();
208 bool SVGPathParserFactory::buildStringFromSVGPathSegList(const SVGPathSegList& list, String& result, PathParsingMode parsingMode)
214 SVGPathStringBuilder* builder = globalSVGPathStringBuilder();
216 OwnPtr<SVGPathSegListSource> source = SVGPathSegListSource::create(list);
217 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
218 bool ok = parser->parsePathDataFromSource(parsingMode);
219 result = builder->result();
224 bool SVGPathParserFactory::buildSVGPathByteStreamFromString(const String& d, OwnPtr<SVGPathByteStream>& result, PathParsingMode parsingMode)
229 result = SVGPathByteStream::create();
234 SVGPathByteStreamBuilder* builder = globalSVGPathByteStreamBuilder(result.get());
236 OwnPtr<SVGPathStringSource> source = SVGPathStringSource::create(d);
237 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
238 bool ok = parser->parsePathDataFromSource(parsingMode);
243 bool SVGPathParserFactory::buildAnimatedSVGPathByteStream(SVGPathByteStream* fromStream, SVGPathByteStream* toStream, OwnPtr<SVGPathByteStream>& result, float progress)
250 result = SVGPathByteStream::create();
252 if (fromStream->isEmpty() || toStream->isEmpty())
255 SVGPathByteStreamBuilder* builder = globalSVGPathByteStreamBuilder(result.get());
257 OwnPtr<SVGPathByteStreamSource> fromSource = SVGPathByteStreamSource::create(fromStream);
258 OwnPtr<SVGPathByteStreamSource> toSource = SVGPathByteStreamSource::create(toStream);
259 SVGPathBlender* blender = globalSVGPathBlender();
260 bool ok = blender->blendAnimatedPath(progress, fromSource.get(), toSource.get(), builder);
265 bool SVGPathParserFactory::getSVGPathSegAtLengthFromSVGPathByteStream(SVGPathByteStream* stream, float length, unsigned long& pathSeg)
268 if (stream->isEmpty())
271 PathTraversalState traversalState(PathTraversalState::TraversalSegmentAtLength);
272 SVGPathTraversalStateBuilder* builder = globalSVGPathTraversalStateBuilder(traversalState, length);
274 OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
275 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
276 bool ok = parser->parsePathDataFromSource(NormalizedParsing);
277 pathSeg = builder->pathSegmentIndex();
282 bool SVGPathParserFactory::getTotalLengthOfSVGPathByteStream(SVGPathByteStream* stream, float& totalLength)
285 if (stream->isEmpty())
288 PathTraversalState traversalState(PathTraversalState::TraversalTotalLength);
289 SVGPathTraversalStateBuilder* builder = globalSVGPathTraversalStateBuilder(traversalState, 0);
291 OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
292 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
293 bool ok = parser->parsePathDataFromSource(NormalizedParsing);
294 totalLength = builder->totalLength();
299 bool SVGPathParserFactory::getPointAtLengthOfSVGPathByteStream(SVGPathByteStream* stream, float length, FloatPoint& point)
302 if (stream->isEmpty())
305 PathTraversalState traversalState(PathTraversalState::TraversalPointAtLength);
306 SVGPathTraversalStateBuilder* builder = globalSVGPathTraversalStateBuilder(traversalState, length);
308 OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
309 SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
310 bool ok = parser->parsePathDataFromSource(NormalizedParsing);
311 point = builder->currentPoint();