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