initial import
[vuplus_webkit] / Source / WebCore / svg / SVGPathParserFactory.cpp
1 /*
2  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
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 #include "config.h"
21
22 #if ENABLE(SVG)
23 #include "SVGPathParserFactory.h"
24
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"
37
38 namespace WebCore {
39
40 static SVGPathBuilder* globalSVGPathBuilder(Path& result)
41 {
42     static SVGPathBuilder* s_builder = 0;
43     if (!s_builder)
44         s_builder = new SVGPathBuilder;
45
46     s_builder->setCurrentPath(&result);
47     return s_builder;
48 }
49
50 static SVGPathSegListBuilder* globalSVGPathSegListBuilder(SVGPathElement* element, SVGPathSegRole role, SVGPathSegList& result)
51 {
52     static SVGPathSegListBuilder* s_builder = 0;
53     if (!s_builder)
54         s_builder = new SVGPathSegListBuilder;
55
56     s_builder->setCurrentSVGPathElement(element);
57     s_builder->setCurrentSVGPathSegList(result);
58     s_builder->setCurrentSVGPathSegRole(role);
59     return s_builder;
60 }
61
62 static SVGPathByteStreamBuilder* globalSVGPathByteStreamBuilder(SVGPathByteStream* result)
63 {
64     static SVGPathByteStreamBuilder* s_builder = 0;
65     if (!s_builder)
66         s_builder = new SVGPathByteStreamBuilder;
67
68     s_builder->setCurrentByteStream(result);
69     return s_builder;
70 }
71
72 static SVGPathStringBuilder* globalSVGPathStringBuilder()
73 {
74     static SVGPathStringBuilder* s_builder = 0;
75     if (!s_builder)
76         s_builder = new SVGPathStringBuilder;
77
78     return s_builder;
79 }
80
81 static SVGPathTraversalStateBuilder* globalSVGPathTraversalStateBuilder(PathTraversalState& traversalState, float length)
82 {
83     static SVGPathTraversalStateBuilder* s_builder = 0;
84     if (!s_builder)
85         s_builder = new SVGPathTraversalStateBuilder;
86
87     s_builder->setCurrentTraversalState(&traversalState);
88     s_builder->setDesiredLength(length);
89     return s_builder;
90 }
91
92 static SVGPathParser* globalSVGPathParser(SVGPathSource* source, SVGPathConsumer* consumer)
93 {
94     static SVGPathParser* s_parser = 0;
95     if (!s_parser)
96         s_parser = new SVGPathParser;
97
98     s_parser->setCurrentSource(source);
99     s_parser->setCurrentConsumer(consumer);
100     return s_parser;
101 }
102
103 static SVGPathBlender* globalSVGPathBlender()
104 {
105     static SVGPathBlender* s_blender = 0;
106     if (!s_blender)
107         s_blender = new SVGPathBlender;
108
109     return s_blender;
110 }
111
112 SVGPathParserFactory* SVGPathParserFactory::self()
113 {
114     static SVGPathParserFactory* s_instance = 0;
115     if (!s_instance)
116         s_instance = new SVGPathParserFactory;
117
118     return s_instance;
119 }
120
121 SVGPathParserFactory::SVGPathParserFactory()
122 {
123 }
124
125 SVGPathParserFactory::~SVGPathParserFactory()
126 {
127 }
128
129 bool SVGPathParserFactory::buildPathFromString(const String& d, Path& result)
130 {
131     if (d.isEmpty())
132         return false;
133
134     SVGPathBuilder* builder = globalSVGPathBuilder(result);
135
136     OwnPtr<SVGPathStringSource> source = SVGPathStringSource::create(d);
137     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
138     bool ok = parser->parsePathDataFromSource(NormalizedParsing);
139     parser->cleanup();
140     return ok;
141 }
142
143 bool SVGPathParserFactory::buildSVGPathByteStreamFromSVGPathSegList(const SVGPathSegList& list, OwnPtr<SVGPathByteStream>& result, PathParsingMode parsingMode)
144 {
145     if (result)
146         result->clear();
147     else
148         result = SVGPathByteStream::create();
149
150     if (list.isEmpty())
151         return false;
152
153     SVGPathByteStreamBuilder* builder = globalSVGPathByteStreamBuilder(result.get());
154
155     OwnPtr<SVGPathSegListSource> source = SVGPathSegListSource::create(list);
156     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
157     bool ok = parser->parsePathDataFromSource(parsingMode);
158     parser->cleanup();
159     return ok;
160 }
161
162 bool SVGPathParserFactory::buildPathFromByteStream(SVGPathByteStream* stream, Path& result)
163 {
164     ASSERT(stream);
165     if (stream->isEmpty())
166         return false;
167
168     SVGPathBuilder* builder = globalSVGPathBuilder(result);
169
170     OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
171     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
172     bool ok = parser->parsePathDataFromSource(NormalizedParsing);
173     parser->cleanup();
174     return ok;
175 }
176
177 bool SVGPathParserFactory::buildSVGPathSegListFromByteStream(SVGPathByteStream* stream, SVGPathElement* element, SVGPathSegList& result, PathParsingMode parsingMode)
178 {
179     ASSERT(stream);
180     if (stream->isEmpty())
181         return false; 
182
183     SVGPathSegListBuilder* builder = globalSVGPathSegListBuilder(element, parsingMode == NormalizedParsing ? PathSegNormalizedRole : PathSegUnalteredRole, result);
184
185     OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
186     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
187     bool ok = parser->parsePathDataFromSource(parsingMode);
188     parser->cleanup();
189     return ok;
190 }
191
192 bool SVGPathParserFactory::buildStringFromByteStream(SVGPathByteStream* stream, String& result, PathParsingMode parsingMode)
193 {
194     ASSERT(stream);
195     if (stream->isEmpty())
196         return false; 
197
198     SVGPathStringBuilder* builder = globalSVGPathStringBuilder();
199
200     OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
201     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
202     bool ok = parser->parsePathDataFromSource(parsingMode);
203     result = builder->result();
204     parser->cleanup();
205     return ok;
206 }
207
208 bool SVGPathParserFactory::buildStringFromSVGPathSegList(const SVGPathSegList& list, String& result, PathParsingMode parsingMode)
209 {
210     result = String();
211     if (list.isEmpty())
212         return false;
213
214     SVGPathStringBuilder* builder = globalSVGPathStringBuilder();
215
216     OwnPtr<SVGPathSegListSource> source = SVGPathSegListSource::create(list);
217     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
218     bool ok = parser->parsePathDataFromSource(parsingMode);
219     result = builder->result();
220     parser->cleanup();
221     return ok;
222 }
223
224 bool SVGPathParserFactory::buildSVGPathByteStreamFromString(const String& d, OwnPtr<SVGPathByteStream>& result, PathParsingMode parsingMode)
225 {
226     if (result)
227         result->clear();
228     else
229         result = SVGPathByteStream::create();
230
231     if (d.isEmpty())
232         return false;
233
234     SVGPathByteStreamBuilder* builder = globalSVGPathByteStreamBuilder(result.get());
235
236     OwnPtr<SVGPathStringSource> source = SVGPathStringSource::create(d);
237     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
238     bool ok = parser->parsePathDataFromSource(parsingMode);
239     parser->cleanup();
240     return ok;
241 }
242
243 bool SVGPathParserFactory::buildAnimatedSVGPathByteStream(SVGPathByteStream* fromStream, SVGPathByteStream* toStream, OwnPtr<SVGPathByteStream>& result, float progress)
244 {
245     ASSERT(fromStream);
246     ASSERT(toStream);
247     if (result)
248         result->clear();
249     else
250         result = SVGPathByteStream::create();
251
252     if (fromStream->isEmpty() || toStream->isEmpty())
253         return false;
254
255     SVGPathByteStreamBuilder* builder = globalSVGPathByteStreamBuilder(result.get());
256
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);
261     blender->cleanup();
262     return ok;
263 }
264
265 bool SVGPathParserFactory::getSVGPathSegAtLengthFromSVGPathByteStream(SVGPathByteStream* stream, float length, unsigned long& pathSeg)
266 {
267     ASSERT(stream);
268     if (stream->isEmpty())
269         return false;
270
271     PathTraversalState traversalState(PathTraversalState::TraversalSegmentAtLength);
272     SVGPathTraversalStateBuilder* builder = globalSVGPathTraversalStateBuilder(traversalState, length);
273
274     OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
275     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
276     bool ok = parser->parsePathDataFromSource(NormalizedParsing);
277     pathSeg = builder->pathSegmentIndex();
278     parser->cleanup();
279     return ok;
280 }
281
282 bool SVGPathParserFactory::getTotalLengthOfSVGPathByteStream(SVGPathByteStream* stream, float& totalLength)
283 {
284     ASSERT(stream);
285     if (stream->isEmpty())
286         return false;
287     
288     PathTraversalState traversalState(PathTraversalState::TraversalTotalLength);
289     SVGPathTraversalStateBuilder* builder = globalSVGPathTraversalStateBuilder(traversalState, 0);
290     
291     OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
292     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
293     bool ok = parser->parsePathDataFromSource(NormalizedParsing);
294     totalLength = builder->totalLength();
295     parser->cleanup();
296     return ok;
297 }
298
299 bool SVGPathParserFactory::getPointAtLengthOfSVGPathByteStream(SVGPathByteStream* stream, float length, FloatPoint& point)
300 {
301     ASSERT(stream);
302     if (stream->isEmpty())
303         return false;
304     
305     PathTraversalState traversalState(PathTraversalState::TraversalPointAtLength);
306     SVGPathTraversalStateBuilder* builder = globalSVGPathTraversalStateBuilder(traversalState, length);
307     
308     OwnPtr<SVGPathByteStreamSource> source = SVGPathByteStreamSource::create(stream);
309     SVGPathParser* parser = globalSVGPathParser(source.get(), builder);
310     bool ok = parser->parsePathDataFromSource(NormalizedParsing);
311     point = builder->currentPoint();
312     parser->cleanup();
313     return ok;
314 }
315
316 }
317
318 #endif