gdiplus: Check custom dash array for bad properties.
[wine] / dlls / gdiplus / pen.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 #include "gdiplus.h"
25 #include "gdiplus_private.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
29
30 static DWORD gdip_to_gdi_dash(GpDashStyle dash)
31 {
32     switch(dash){
33         case DashStyleSolid:
34             return PS_SOLID;
35         case DashStyleDash:
36             return PS_DASH;
37         case DashStyleDot:
38             return PS_DOT;
39         case DashStyleDashDot:
40             return PS_DASHDOT;
41         case DashStyleDashDotDot:
42             return PS_DASHDOTDOT;
43         case DashStyleCustom:
44             return PS_USERSTYLE;
45         default:
46             ERR("Not a member of GpDashStyle enumeration\n");
47             return 0;
48     }
49 }
50
51 static DWORD gdip_to_gdi_join(GpLineJoin join)
52 {
53     switch(join){
54         case LineJoinRound:
55             return PS_JOIN_ROUND;
56         case LineJoinBevel:
57             return PS_JOIN_BEVEL;
58         case LineJoinMiter:
59         case LineJoinMiterClipped:
60             return PS_JOIN_MITER;
61         default:
62             ERR("Not a member of GpLineJoin enumeration\n");
63             return 0;
64     }
65 }
66
67 GpStatus WINGDIPAPI GdipClonePen(GpPen *pen, GpPen **clonepen)
68 {
69     if(!pen || !clonepen)
70         return InvalidParameter;
71
72     *clonepen = GdipAlloc(sizeof(GpPen));
73     if(!*clonepen)  return OutOfMemory;
74
75     memcpy(*clonepen, pen, sizeof(GpPen));
76
77     GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
78     GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
79     GdipCloneBrush(pen->brush, &(*clonepen)->brush);
80
81     return Ok;
82 }
83
84 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, FLOAT width, GpUnit unit,
85     GpPen **pen)
86 {
87     GpPen *gp_pen;
88
89     if(!pen)
90         return InvalidParameter;
91
92     gp_pen = GdipAlloc(sizeof(GpPen));
93     if(!gp_pen)    return OutOfMemory;
94
95     gp_pen->style = GP_DEFAULT_PENSTYLE;
96     gp_pen->width = width;
97     gp_pen->unit = unit;
98     gp_pen->endcap = LineCapFlat;
99     gp_pen->join = LineJoinMiter;
100     gp_pen->miterlimit = 10.0;
101     gp_pen->dash = DashStyleSolid;
102     GdipCreateSolidFill(color, (GpSolidFill **)(&gp_pen->brush));
103
104     if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
105         FIXME("UnitWorld, UnitPixel only supported units\n");
106         GdipFree(gp_pen);
107         return NotImplemented;
108     }
109
110     *pen = gp_pen;
111
112     return Ok;
113 }
114
115 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
116 {
117     if(!pen)    return InvalidParameter;
118
119     GdipDeleteBrush(pen->brush);
120     GdipDeleteCustomLineCap(pen->customstart);
121     GdipDeleteCustomLineCap(pen->customend);
122     GdipFree(pen->dashes);
123     GdipFree(pen);
124
125     return Ok;
126 }
127
128 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
129 {
130     if(!pen || !brush)
131         return InvalidParameter;
132
133     return GdipCloneBrush(pen->brush, brush);
134 }
135
136 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
137 {
138     if(!pen || !argb)
139         return InvalidParameter;
140
141     if(pen->brush->bt != BrushTypeSolidColor)
142         return NotImplemented;
143
144     return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
145 }
146
147 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
148 {
149     if(!pen || !dash || count > pen->numdashes)
150         return InvalidParameter;
151
152     /* note: if you pass a negative value for count, it crashes native gdiplus. */
153     if(count < 0)
154         return GenericError;
155
156     memcpy(dash, pen->dashes, count * sizeof(REAL));
157
158     return Ok;
159 }
160
161 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
162 {
163     if(!pen || !dash)
164         return InvalidParameter;
165
166     *dash = pen->dash;
167
168     return Ok;
169 }
170
171 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
172 {
173     if(!pen || !brush)
174         return InvalidParameter;
175
176     GdipDeleteBrush(pen->brush);
177     return GdipCloneBrush(brush, &pen->brush);
178 }
179
180 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
181 {
182     if(!pen)
183         return InvalidParameter;
184
185     if(pen->brush->bt != BrushTypeSolidColor)
186         return NotImplemented;
187
188     return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
189 }
190
191 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
192 {
193     GpCustomLineCap * cap;
194     GpStatus ret;
195
196     if(!pen || !customCap) return InvalidParameter;
197
198     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
199         GdipDeleteCustomLineCap(pen->customend);
200         pen->endcap = LineCapCustom;
201         pen->customend = cap;
202     }
203
204     return ret;
205 }
206
207 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
208 {
209     GpCustomLineCap * cap;
210     GpStatus ret;
211
212     if(!pen || !customCap) return InvalidParameter;
213
214     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
215         GdipDeleteCustomLineCap(pen->customstart);
216         pen->startcap = LineCapCustom;
217         pen->customstart = cap;
218     }
219
220     return ret;
221 }
222
223 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
224     INT count)
225 {
226     INT i;
227     REAL sum = 0;
228
229     if(!pen || !dash)
230         return InvalidParameter;
231
232     for(i = 0; i < count; i++){
233         sum += dash[i];
234         if(dash[i] < 0.0)
235             return InvalidParameter;
236     }
237
238     if(sum == 0.0 && count)
239         return InvalidParameter;
240
241     GdipFree(pen->dashes);
242     pen->dashes = NULL;
243
244     if(count > 0)
245         pen->dashes = GdipAlloc(count * sizeof(REAL));
246     if(!pen->dashes){
247         pen->numdashes = 0;
248         return OutOfMemory;
249     }
250
251     GdipSetPenDashStyle(pen, DashStyleCustom);
252     memcpy(pen->dashes, dash, count * sizeof(REAL));
253     pen->numdashes = count;
254
255     return Ok;
256 }
257
258 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
259 {
260     if(!pen)
261         return InvalidParameter;
262
263     if(dash != DashStyleCustom){
264         GdipFree(pen->dashes);
265         pen->dashes = NULL;
266         pen->numdashes = 0;
267     }
268
269     pen->dash = dash;
270     pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
271                     PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
272     pen->style |= gdip_to_gdi_dash(dash);
273
274     return Ok;
275 }
276
277 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
278 {
279     if(!pen)    return InvalidParameter;
280
281     /* The old custom cap gets deleted even if the new style is LineCapCustom. */
282     GdipDeleteCustomLineCap(pen->customend);
283     pen->customend = NULL;
284     pen->endcap = cap;
285
286     return Ok;
287 }
288
289 /* FIXME: startcap, dashcap not used. */
290 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
291     GpLineCap end, GpDashCap dash)
292 {
293     if(!pen)
294         return InvalidParameter;
295
296     GdipDeleteCustomLineCap(pen->customend);
297     GdipDeleteCustomLineCap(pen->customstart);
298     pen->customend = NULL;
299     pen->customstart = NULL;
300
301     pen->startcap = start;
302     pen->endcap = end;
303     pen->dashcap = dash;
304
305     return Ok;
306 }
307
308 /* FIXME: Miter line joins behave a bit differently than they do in windows.
309  * Both kinds of miter joins clip if the angle is less than 11 degrees. */
310 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
311 {
312     if(!pen)    return InvalidParameter;
313
314     pen->join = join;
315     pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
316     pen->style |= gdip_to_gdi_join(join);
317
318     return Ok;
319 }
320
321 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
322 {
323     if(!pen)
324         return InvalidParameter;
325
326     pen->miterlimit = limit;
327
328     return Ok;
329 }
330
331 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
332 {
333     if(!pen)    return InvalidParameter;
334
335     GdipDeleteCustomLineCap(pen->customstart);
336     pen->customstart = NULL;
337     pen->startcap = cap;
338
339     return Ok;
340 }