gdiplus: Added GdipAddPathPath.
[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 GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
98     INT count)
99 {
100     INT i, old_count;
101
102     if(!path || !points)
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] = PathPointTypeLine;
114     }
115
116     if(path->newfigure){
117         path->pathdata.Types[old_count] = PathPointTypeStart;
118         path->newfigure = FALSE;
119     }
120
121     path->pathdata.Count += count;
122
123     return Ok;
124 }
125
126 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
127     BOOL connect)
128 {
129     INT old_count, count;
130
131     if(!path || !addingPath)
132         return InvalidParameter;
133
134     old_count = path->pathdata.Count;
135     count = addingPath->pathdata.Count;
136
137     if(!lengthen_path(path, count))
138         return OutOfMemory;
139
140     memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
141            count * sizeof(GpPointF));
142     memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
143
144     if(path->newfigure || !connect)
145         path->pathdata.Types[old_count] = PathPointTypeStart;
146     else
147         path->pathdata.Types[old_count] = PathPointTypeLine;
148
149     path->newfigure = FALSE;
150     path->pathdata.Count += count;
151
152     return Ok;
153 }
154
155 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
156 {
157     if(!path)
158         return InvalidParameter;
159
160     if(path->pathdata.Count > 0){
161         path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
162         path->newfigure = TRUE;
163     }
164
165     return Ok;
166 }
167
168 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
169 {
170     INT i;
171
172     if(!path)
173         return InvalidParameter;
174
175     for(i = 1; i < path->pathdata.Count; i++){
176         if(path->pathdata.Types[i] == PathPointTypeStart)
177             path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
178     }
179
180     path->newfigure = TRUE;
181
182     return Ok;
183 }
184
185 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
186 {
187     if(!path)
188         return InvalidParameter;
189
190     *path = GdipAlloc(sizeof(GpPath));
191     if(!*path)  return OutOfMemory;
192
193     (*path)->fill = fill;
194     (*path)->newfigure = TRUE;
195
196     return Ok;
197 }
198
199 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
200 {
201     if(!path)
202         return InvalidParameter;
203
204     GdipFree(path->pathdata.Points);
205     GdipFree(path->pathdata.Types);
206     GdipFree(path);
207
208     return Ok;
209 }
210
211 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
212 {
213     if(!path || !fillmode)
214         return InvalidParameter;
215
216     *fillmode = path->fill;
217
218     return Ok;
219 }
220
221 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
222 {
223     if(!path)
224         return InvalidParameter;
225
226     if(count < path->pathdata.Count)
227         return InsufficientBuffer;
228
229     memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
230
231     return Ok;
232 }
233
234 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
235 {
236     if(!path)
237         return InvalidParameter;
238
239     if(count < path->pathdata.Count)
240         return InsufficientBuffer;
241
242     memcpy(types, path->pathdata.Types, path->pathdata.Count);
243
244     return Ok;
245 }
246
247 /* Windows expands the bounding box to the maximum possible bounding box
248  * for a given pen.  For example, if a line join can extend past the point
249  * it's joining by x units, the bounding box is extended by x units in every
250  * direction (even though this is too conservative for most cases). */
251 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
252     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
253 {
254     GpPointF * points, temp_pts[4];
255     INT count, i;
256     REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
257
258     /* Matrix and pen can be null. */
259     if(!path || !bounds)
260         return InvalidParameter;
261
262     /* If path is empty just return. */
263     count = path->pathdata.Count;
264     if(count == 0){
265         bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
266         return Ok;
267     }
268
269     points = path->pathdata.Points;
270
271     low_x = high_x = points[0].X;
272     low_y = high_y = points[0].Y;
273
274     for(i = 1; i < count; i++){
275         low_x = min(low_x, points[i].X);
276         low_y = min(low_y, points[i].Y);
277         high_x = max(high_x, points[i].X);
278         high_y = max(high_y, points[i].Y);
279     }
280
281     width = high_x - low_x;
282     height = high_y - low_y;
283
284     /* This looks unusual but it's the only way I can imitate windows. */
285     if(matrix){
286         temp_pts[0].X = low_x;
287         temp_pts[0].Y = low_y;
288         temp_pts[1].X = low_x;
289         temp_pts[1].Y = high_y;
290         temp_pts[2].X = high_x;
291         temp_pts[2].Y = high_y;
292         temp_pts[3].X = high_x;
293         temp_pts[3].Y = low_y;
294
295         GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
296         low_x = temp_pts[0].X;
297         low_y = temp_pts[0].Y;
298
299         for(i = 1; i < 4; i++){
300             low_x = min(low_x, temp_pts[i].X);
301             low_y = min(low_y, temp_pts[i].Y);
302         }
303
304         temp = width;
305         width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
306         height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
307     }
308
309     if(pen){
310         path_width = pen->width / 2.0;
311
312         if(count > 2)
313             path_width = max(path_width,  pen->width * pen->miterlimit / 2.0);
314         /* FIXME: this should probably also check for the startcap */
315         if(pen->endcap & LineCapNoAnchor)
316             path_width = max(path_width,  pen->width * 2.2);
317
318         low_x -= path_width;
319         low_y -= path_width;
320         width += 2.0 * path_width;
321         height += 2.0 * path_width;
322     }
323
324     bounds->X = low_x;
325     bounds->Y = low_y;
326     bounds->Width = width;
327     bounds->Height = height;
328
329     return Ok;
330 }
331
332 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
333 {
334     if(!path)
335         return InvalidParameter;
336
337     *count = path->pathdata.Count;
338
339     return Ok;
340 }
341
342 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
343 {
344     if(!path)
345         return InvalidParameter;
346
347     path->newfigure = TRUE;
348
349     return Ok;
350 }
351
352 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
353 {
354     if(!path)
355         return InvalidParameter;
356
357     path->pathdata.Count = 0;
358     path->newfigure = TRUE;
359     path->fill = FillModeAlternate;
360
361     return Ok;
362 }
363
364 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
365 {
366     if(!path)
367         return InvalidParameter;
368
369     if(path->pathdata.Count == 0)
370         return Ok;
371
372     return GdipTransformMatrixPoints(matrix, (GpPointF*) path->pathdata.Points,
373                                      path->pathdata.Count);
374 }