gdiplus: Implemented GdipPathIterNextMarkerPath with tests.
[wine] / dlls / gdiplus / pathiterator.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24
25 #include "objbase.h"
26
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29
30 GpStatus WINGDIPAPI GdipCreatePathIter(GpPathIterator **iterator, GpPath* path)
31 {
32     INT size;
33
34     if(!iterator)
35         return InvalidParameter;
36
37     *iterator = GdipAlloc(sizeof(GpPathIterator));
38     if(!*iterator)  return OutOfMemory;
39
40     if(path){
41         size = path->pathdata.Count;
42
43         (*iterator)->pathdata.Types  = GdipAlloc(size);
44         (*iterator)->pathdata.Points = GdipAlloc(size * sizeof(PointF));
45
46         memcpy((*iterator)->pathdata.Types, path->pathdata.Types, size);
47         memcpy((*iterator)->pathdata.Points, path->pathdata.Points,size * sizeof(PointF));
48         (*iterator)->pathdata.Count = size;
49     }
50     else{
51         (*iterator)->pathdata.Types  = NULL;
52         (*iterator)->pathdata.Points = NULL;
53         (*iterator)->pathdata.Count  = 0;
54     }
55
56     (*iterator)->subpath_pos  = 0;
57     (*iterator)->marker_pos   = 0;
58     (*iterator)->pathtype_pos = 0;
59
60     return Ok;
61 }
62
63 GpStatus WINGDIPAPI GdipDeletePathIter(GpPathIterator *iter)
64 {
65     if(!iter)
66         return InvalidParameter;
67
68     GdipFree(iter->pathdata.Types);
69     GdipFree(iter->pathdata.Points);
70     GdipFree(iter);
71
72     return Ok;
73 }
74
75 GpStatus WINGDIPAPI GdipPathIterCopyData(GpPathIterator* iterator,
76     INT* resultCount, GpPointF* points, BYTE* types, INT startIndex, INT endIndex)
77 {
78     if(!iterator || !types || !points)
79         return InvalidParameter;
80
81     if(endIndex > iterator->pathdata.Count - 1 || startIndex < 0 ||
82         endIndex < startIndex){
83         *resultCount = 0;
84         return Ok;
85     }
86
87     *resultCount = endIndex - startIndex + 1;
88
89     memcpy(types, &(iterator->pathdata.Types[startIndex]), *resultCount);
90     memcpy(points, &(iterator->pathdata.Points[startIndex]),
91         *resultCount * sizeof(PointF));
92
93     return Ok;
94 }
95
96 GpStatus WINGDIPAPI GdipPathIterHasCurve(GpPathIterator* iterator, BOOL* hasCurve)
97 {
98     INT i;
99
100     if(!iterator)
101         return InvalidParameter;
102
103     *hasCurve = FALSE;
104
105     for(i = 0; i < iterator->pathdata.Count; i++)
106         if((iterator->pathdata.Types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
107             *hasCurve = TRUE;
108             break;
109         }
110
111     return Ok;
112 }
113
114 GpStatus WINGDIPAPI GdipPathIterGetSubpathCount(GpPathIterator* iterator, INT* count)
115 {
116     INT i;
117
118     if(!iterator || !count)
119         return InvalidParameter;
120
121     *count = 0;
122     for(i = 0; i < iterator->pathdata.Count; i++){
123         if(iterator->pathdata.Types[i] == PathPointTypeStart)
124             (*count)++;
125     }
126
127     return Ok;
128 }
129
130 GpStatus WINGDIPAPI GdipPathIterNextMarker(GpPathIterator* iterator, INT *resultCount,
131     INT* startIndex, INT* endIndex)
132 {
133     INT i;
134
135     if(!iterator || !startIndex || !endIndex)
136         return InvalidParameter;
137
138     *resultCount = 0;
139
140     /* first call could start with second point as all subsequent, cause
141        path couldn't contain only one */
142     for(i = iterator->marker_pos + 1; i < iterator->pathdata.Count; i++){
143         if((iterator->pathdata.Types[i] & PathPointTypePathMarker) ||
144            (i == iterator->pathdata.Count - 1)){
145             *startIndex = iterator->marker_pos;
146             if(iterator->marker_pos > 0) (*startIndex)++;
147             *endIndex   = iterator->marker_pos = i;
148             *resultCount= *endIndex - *startIndex + 1;
149             break;
150         }
151     }
152
153     return Ok;
154 }
155
156 GpStatus WINGDIPAPI GdipPathIterNextMarkerPath(GpPathIterator* iterator, INT* result,
157     GpPath* path)
158 {
159     INT start, end;
160
161     if(!iterator || !result)
162         return InvalidParameter;
163
164     GdipPathIterNextMarker(iterator, result, &start, &end);
165     /* return path */
166     if(((*result) > 0) && path){
167         GdipResetPath(path);
168
169         if(!lengthen_path(path, *result))
170             return OutOfMemory;
171
172         memcpy(path->pathdata.Points, &(iterator->pathdata.Points[start]), sizeof(GpPointF)*(*result));
173         memcpy(path->pathdata.Types,  &(iterator->pathdata.Types[start]),  sizeof(BYTE)*(*result));
174         path->pathdata.Count = *result;
175     }
176
177     return Ok;
178 }
179
180 GpStatus WINGDIPAPI GdipPathIterNextSubpath(GpPathIterator* iterator,
181     INT *resultCount, INT* startIndex, INT* endIndex, BOOL* isClosed)
182 {
183     INT i, count;
184
185     if(!iterator || !startIndex || !endIndex || !isClosed || !resultCount)
186         return InvalidParameter;
187
188     count = iterator->pathdata.Count;
189
190     /* iterator created with NULL path */
191     if(count == 0){
192         *resultCount = 0;
193         return Ok;
194     }
195
196     if(iterator->subpath_pos == count){
197         *startIndex = *endIndex = *resultCount = 0;
198         *isClosed = 1;
199         return Ok;
200     }
201
202     *startIndex = iterator->subpath_pos;
203
204     for(i = iterator->subpath_pos + 1; i < count &&
205         !(iterator->pathdata.Types[i] == PathPointTypeStart); i++);
206
207     *endIndex = i - 1;
208     iterator->subpath_pos = i;
209
210     *resultCount = *endIndex - *startIndex + 1;
211
212     if(iterator->pathdata.Types[*endIndex] & PathPointTypeCloseSubpath)
213         *isClosed = TRUE;
214     else
215         *isClosed = FALSE;
216
217     return Ok;
218 }
219 GpStatus WINGDIPAPI GdipPathIterRewind(GpPathIterator *iterator)
220 {
221     if(!iterator)
222         return InvalidParameter;
223
224     iterator->subpath_pos = 0;
225     iterator->marker_pos = 0;
226     iterator->pathtype_pos = 0;
227
228     return Ok;
229 }
230
231 GpStatus WINGDIPAPI GdipPathIterGetCount(GpPathIterator* iterator, INT* count)
232 {
233     if(!iterator || !count)
234         return InvalidParameter;
235
236     *count = iterator->pathdata.Count;
237
238     return Ok;
239 }
240
241 GpStatus WINGDIPAPI GdipPathIterEnumerate(GpPathIterator* iterator, INT* resultCount,
242     GpPointF *points, BYTE *types, INT count)
243 {
244     if((count < 0) || !resultCount)
245         return InvalidParameter;
246
247     if(count == 0){
248         *resultCount = 0;
249         return Ok;
250     }
251
252     return GdipPathIterCopyData(iterator, resultCount, points, types, 0, count-1);
253 }
254
255 GpStatus WINGDIPAPI GdipPathIterIsValid(GpPathIterator* iterator, BOOL* valid)
256 {
257     if(!iterator || !valid)
258         return InvalidParameter;
259
260     *valid = TRUE;
261
262     return Ok;
263 }
264
265 GpStatus WINGDIPAPI GdipPathIterNextSubpathPath(GpPathIterator* iter, INT* result,
266     GpPath* path, BOOL* closed)
267 {
268     INT start, end;
269
270     if(!iter || !result || !closed)
271         return InvalidParameter;
272
273     GdipPathIterNextSubpath(iter, result, &start, &end, closed);
274     /* return path */
275     if(((*result) > 0) && path){
276         GdipResetPath(path);
277
278         if(!lengthen_path(path, *result))
279             return OutOfMemory;
280
281         memcpy(path->pathdata.Points, &(iter->pathdata.Points[start]), sizeof(GpPointF)*(*result));
282         memcpy(path->pathdata.Types,  &(iter->pathdata.Types[start]),  sizeof(BYTE)*(*result));
283         path->pathdata.Count = *result;
284     }
285
286     return Ok;
287 }