gdiplus: Added GdipAddPathEllipse.
[wine] / dlls / gdiplus / graphicspath.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
20 #include <stdarg.h>
21 #include <math.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32
33 /* make sure path has enough space for len more points */
34 static BOOL lengthen_path(GpPath *path, INT len)
35 {
36     /* initial allocation */
37     if(path->datalen == 0){
38         path->datalen = len * 2;
39
40         path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
41         if(!path->pathdata.Points)   return FALSE;
42
43         path->pathdata.Types = GdipAlloc(path->datalen);
44         if(!path->pathdata.Types){
45             GdipFree(path->pathdata.Points);
46             return FALSE;
47         }
48     }
49     /* reallocation, double size of arrays */
50     else if(path->datalen - path->pathdata.Count < len){
51         while(path->datalen - path->pathdata.Count < len)
52             path->datalen *= 2;
53
54         path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
55             path->pathdata.Points, path->datalen * sizeof(PointF));
56         if(!path->pathdata.Points)  return FALSE;
57
58         path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
59             path->pathdata.Types, path->datalen);
60         if(!path->pathdata.Types)   return FALSE;
61     }
62
63     return TRUE;
64 }
65
66 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
67     REAL y2, REAL startAngle, REAL sweepAngle)
68 {
69     INT count, old_count, i;
70
71     if(!path)
72         return InvalidParameter;
73
74     count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
75
76     if(count == 0)
77         return Ok;
78     if(!lengthen_path(path, count))
79         return OutOfMemory;
80
81     old_count = path->pathdata.Count;
82     arc2polybezier(&path->pathdata.Points[old_count], x1, y1, x2, y2,
83                    startAngle, sweepAngle);
84
85     for(i = 0; i < count; i++){
86         path->pathdata.Types[old_count + i] = PathPointTypeBezier;
87     }
88
89     path->pathdata.Types[old_count] =
90         (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
91     path->newfigure = FALSE;
92     path->pathdata.Count += count;
93
94     return Ok;
95 }
96
97 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
98     INT count)
99 {
100     INT i, old_count;
101
102     if(!path || !points || ((count - 1) % 3))
103         return InvalidParameter;
104
105     if(!lengthen_path(path, count))
106         return OutOfMemory;
107
108     old_count = path->pathdata.Count;
109
110     for(i = 0; i < count; i++){
111         path->pathdata.Points[old_count + i].X = points[i].X;
112         path->pathdata.Points[old_count + i].Y = points[i].Y;
113         path->pathdata.Types[old_count + i] = PathPointTypeBezier;
114     }
115
116     path->pathdata.Types[old_count] =
117         (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
118     path->newfigure = FALSE;
119     path->pathdata.Count += count;
120
121     return Ok;
122 }
123
124 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
125     REAL height)
126 {
127     INT old_count, numpts;
128
129     if(!path)
130         return InvalidParameter;
131
132     if(!lengthen_path(path, MAX_ARC_PTS))
133         return OutOfMemory;
134
135     old_count = path->pathdata.Count;
136     if((numpts = arc2polybezier(&path->pathdata.Points[old_count],  x, y, width,
137                                height, 0.0, 360.0)) != MAX_ARC_PTS){
138         ERR("expected %d points but got %d\n", MAX_ARC_PTS, numpts);
139         return GenericError;
140     }
141
142     memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
143            MAX_ARC_PTS - 1);
144
145     /* An ellipse is an instrinsic figure (always its own subpath). */
146     path->pathdata.Types[old_count] = PathPointTypeStart;
147     path->pathdata.Types[old_count + MAX_ARC_PTS - 1] |= PathPointTypeCloseSubpath;
148     path->newfigure = TRUE;
149     path->pathdata.Count += MAX_ARC_PTS;
150
151     return Ok;
152 }
153
154 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
155     INT count)
156 {
157     INT i, old_count;
158
159     if(!path || !points)
160         return InvalidParameter;
161
162     if(!lengthen_path(path, count))
163         return OutOfMemory;
164
165     old_count = path->pathdata.Count;
166
167     for(i = 0; i < count; i++){
168         path->pathdata.Points[old_count + i].X = points[i].X;
169         path->pathdata.Points[old_count + i].Y = points[i].Y;
170         path->pathdata.Types[old_count + i] = PathPointTypeLine;
171     }
172
173     if(path->newfigure){
174         path->pathdata.Types[old_count] = PathPointTypeStart;
175         path->newfigure = FALSE;
176     }
177
178     path->pathdata.Count += count;
179
180     return Ok;
181 }
182
183 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
184     BOOL connect)
185 {
186     INT old_count, count;
187
188     if(!path || !addingPath)
189         return InvalidParameter;
190
191     old_count = path->pathdata.Count;
192     count = addingPath->pathdata.Count;
193
194     if(!lengthen_path(path, count))
195         return OutOfMemory;
196
197     memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
198            count * sizeof(GpPointF));
199     memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
200
201     if(path->newfigure || !connect)
202         path->pathdata.Types[old_count] = PathPointTypeStart;
203     else
204         path->pathdata.Types[old_count] = PathPointTypeLine;
205
206     path->newfigure = FALSE;
207     path->pathdata.Count += count;
208
209     return Ok;
210 }
211
212 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
213 {
214     if(!path)
215         return InvalidParameter;
216
217     if(path->pathdata.Count > 0){
218         path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
219         path->newfigure = TRUE;
220     }
221
222     return Ok;
223 }
224
225 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
226 {
227     INT i;
228
229     if(!path)
230         return InvalidParameter;
231
232     for(i = 1; i < path->pathdata.Count; i++){
233         if(path->pathdata.Types[i] == PathPointTypeStart)
234             path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
235     }
236
237     path->newfigure = TRUE;
238
239     return Ok;
240 }
241
242 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
243 {
244     if(!path)
245         return InvalidParameter;
246
247     *path = GdipAlloc(sizeof(GpPath));
248     if(!*path)  return OutOfMemory;
249
250     (*path)->fill = fill;
251     (*path)->newfigure = TRUE;
252
253     return Ok;
254 }
255
256 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
257 {
258     if(!path)
259         return InvalidParameter;
260
261     GdipFree(path->pathdata.Points);
262     GdipFree(path->pathdata.Types);
263     GdipFree(path);
264
265     return Ok;
266 }
267
268 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
269 {
270     if(!path || !fillmode)
271         return InvalidParameter;
272
273     *fillmode = path->fill;
274
275     return Ok;
276 }
277
278 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
279 {
280     if(!path)
281         return InvalidParameter;
282
283     if(count < path->pathdata.Count)
284         return InsufficientBuffer;
285
286     memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
287
288     return Ok;
289 }
290
291 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
292 {
293     if(!path)
294         return InvalidParameter;
295
296     if(count < path->pathdata.Count)
297         return InsufficientBuffer;
298
299     memcpy(types, path->pathdata.Types, path->pathdata.Count);
300
301     return Ok;
302 }
303
304 /* Windows expands the bounding box to the maximum possible bounding box
305  * for a given pen.  For example, if a line join can extend past the point
306  * it's joining by x units, the bounding box is extended by x units in every
307  * direction (even though this is too conservative for most cases). */
308 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
309     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
310 {
311     GpPointF * points, temp_pts[4];
312     INT count, i;
313     REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
314
315     /* Matrix and pen can be null. */
316     if(!path || !bounds)
317         return InvalidParameter;
318
319     /* If path is empty just return. */
320     count = path->pathdata.Count;
321     if(count == 0){
322         bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
323         return Ok;
324     }
325
326     points = path->pathdata.Points;
327
328     low_x = high_x = points[0].X;
329     low_y = high_y = points[0].Y;
330
331     for(i = 1; i < count; i++){
332         low_x = min(low_x, points[i].X);
333         low_y = min(low_y, points[i].Y);
334         high_x = max(high_x, points[i].X);
335         high_y = max(high_y, points[i].Y);
336     }
337
338     width = high_x - low_x;
339     height = high_y - low_y;
340
341     /* This looks unusual but it's the only way I can imitate windows. */
342     if(matrix){
343         temp_pts[0].X = low_x;
344         temp_pts[0].Y = low_y;
345         temp_pts[1].X = low_x;
346         temp_pts[1].Y = high_y;
347         temp_pts[2].X = high_x;
348         temp_pts[2].Y = high_y;
349         temp_pts[3].X = high_x;
350         temp_pts[3].Y = low_y;
351
352         GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
353         low_x = temp_pts[0].X;
354         low_y = temp_pts[0].Y;
355
356         for(i = 1; i < 4; i++){
357             low_x = min(low_x, temp_pts[i].X);
358             low_y = min(low_y, temp_pts[i].Y);
359         }
360
361         temp = width;
362         width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
363         height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
364     }
365
366     if(pen){
367         path_width = pen->width / 2.0;
368
369         if(count > 2)
370             path_width = max(path_width,  pen->width * pen->miterlimit / 2.0);
371         /* FIXME: this should probably also check for the startcap */
372         if(pen->endcap & LineCapNoAnchor)
373             path_width = max(path_width,  pen->width * 2.2);
374
375         low_x -= path_width;
376         low_y -= path_width;
377         width += 2.0 * path_width;
378         height += 2.0 * path_width;
379     }
380
381     bounds->X = low_x;
382     bounds->Y = low_y;
383     bounds->Width = width;
384     bounds->Height = height;
385
386     return Ok;
387 }
388
389 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
390 {
391     if(!path)
392         return InvalidParameter;
393
394     *count = path->pathdata.Count;
395
396     return Ok;
397 }
398
399 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
400 {
401     if(!path)
402         return InvalidParameter;
403
404     path->newfigure = TRUE;
405
406     return Ok;
407 }
408
409 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
410 {
411     if(!path)
412         return InvalidParameter;
413
414     path->pathdata.Count = 0;
415     path->newfigure = TRUE;
416     path->fill = FillModeAlternate;
417
418     return Ok;
419 }
420
421 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
422 {
423     if(!path)
424         return InvalidParameter;
425
426     path->fill = fill;
427
428     return Ok;
429 }
430
431 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
432 {
433     if(!path)
434         return InvalidParameter;
435
436     if(path->pathdata.Count == 0)
437         return Ok;
438
439     return GdipTransformMatrixPoints(matrix, (GpPointF*) path->pathdata.Points,
440                                      path->pathdata.Count);
441 }