d3dx8: Add a few tests for MatrixStack.
[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
25 #include "objbase.h"
26
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32
33 static DWORD gdip_to_gdi_dash(GpDashStyle dash)
34 {
35     switch(dash){
36         case DashStyleSolid:
37             return PS_SOLID;
38         case DashStyleDash:
39             return PS_DASH;
40         case DashStyleDot:
41             return PS_DOT;
42         case DashStyleDashDot:
43             return PS_DASHDOT;
44         case DashStyleDashDotDot:
45             return PS_DASHDOTDOT;
46         case DashStyleCustom:
47             return PS_USERSTYLE;
48         default:
49             ERR("Not a member of GpDashStyle enumeration\n");
50             return 0;
51     }
52 }
53
54 static DWORD gdip_to_gdi_join(GpLineJoin join)
55 {
56     switch(join){
57         case LineJoinRound:
58             return PS_JOIN_ROUND;
59         case LineJoinBevel:
60             return PS_JOIN_BEVEL;
61         case LineJoinMiter:
62         case LineJoinMiterClipped:
63             return PS_JOIN_MITER;
64         default:
65             ERR("Not a member of GpLineJoin enumeration\n");
66             return 0;
67     }
68 }
69
70 GpStatus WINGDIPAPI GdipClonePen(GpPen *pen, GpPen **clonepen)
71 {
72     TRACE("(%p, %p)\n", pen, clonepen);
73
74     if(!pen || !clonepen)
75         return InvalidParameter;
76
77     *clonepen = GdipAlloc(sizeof(GpPen));
78     if(!*clonepen)  return OutOfMemory;
79
80     **clonepen = *pen;
81
82     GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
83     GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
84     GdipCloneBrush(pen->brush, &(*clonepen)->brush);
85
86     return Ok;
87 }
88
89 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
90     GpPen **pen)
91 {
92     GpBrush *brush;
93     GpStatus status;
94
95     TRACE("(%x, %.2f, %d, %p)\n", color, width, unit, pen);
96
97     GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
98     status = GdipCreatePen2(brush, width, unit, pen);
99     GdipDeleteBrush(brush);
100     return status;
101 }
102
103 GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
104     GpPen **pen)
105 {
106     GpPen *gp_pen;
107     GpBrush *clone_brush;
108
109     TRACE("(%p, %.2f, %d, %p)\n", brush, width, unit, pen);
110
111     if(!pen || !brush)
112         return InvalidParameter;
113
114     gp_pen = GdipAlloc(sizeof(GpPen));
115     if(!gp_pen)    return OutOfMemory;
116
117     gp_pen->style = GP_DEFAULT_PENSTYLE;
118     gp_pen->width = width;
119     gp_pen->unit = unit;
120     gp_pen->endcap = LineCapFlat;
121     gp_pen->join = LineJoinMiter;
122     gp_pen->miterlimit = 10.0;
123     gp_pen->dash = DashStyleSolid;
124     gp_pen->offset = 0.0;
125     gp_pen->customstart = NULL;
126     gp_pen->customend = NULL;
127
128     if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
129         FIXME("UnitWorld, UnitPixel only supported units\n");
130         GdipFree(gp_pen);
131         return NotImplemented;
132     }
133
134     GdipCloneBrush(brush, &clone_brush);
135     gp_pen->brush = clone_brush;
136
137     *pen = gp_pen;
138
139     return Ok;
140 }
141
142 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
143 {
144     TRACE("(%p)\n", pen);
145
146     if(!pen)    return InvalidParameter;
147
148     GdipDeleteBrush(pen->brush);
149     GdipDeleteCustomLineCap(pen->customstart);
150     GdipDeleteCustomLineCap(pen->customend);
151     GdipFree(pen->dashes);
152     GdipFree(pen);
153
154     return Ok;
155 }
156
157 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
158 {
159     TRACE("(%p, %p)\n", pen, brush);
160
161     if(!pen || !brush)
162         return InvalidParameter;
163
164     return GdipCloneBrush(pen->brush, brush);
165 }
166
167 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
168 {
169     TRACE("(%p, %p)\n", pen, argb);
170
171     if(!pen || !argb)
172         return InvalidParameter;
173
174     if(pen->brush->bt != BrushTypeSolidColor)
175         return NotImplemented;
176
177     return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
178 }
179
180 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
181 {
182     TRACE("(%p, %p)\n", pen, customCap);
183
184     if(!pen || !customCap)
185         return InvalidParameter;
186
187     if(!pen->customend){
188         *customCap = NULL;
189         return Ok;
190     }
191
192     return GdipCloneCustomLineCap(pen->customend, customCap);
193 }
194
195 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
196 {
197     TRACE("(%p, %p)\n", pen, customCap);
198
199     if(!pen || !customCap)
200         return InvalidParameter;
201
202     if(!pen->customstart){
203         *customCap = NULL;
204         return Ok;
205     }
206
207     return GdipCloneCustomLineCap(pen->customstart, customCap);
208 }
209
210 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
211 {
212     TRACE("(%p, %p, %d)\n", pen, dash, count);
213
214     if(!pen || !dash || count > pen->numdashes)
215         return InvalidParameter;
216
217     /* note: if you pass a negative value for count, it crashes native gdiplus. */
218     if(count < 0)
219         return GenericError;
220
221     memcpy(dash, pen->dashes, count * sizeof(REAL));
222
223     return Ok;
224 }
225
226 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
227 {
228     TRACE("(%p, %p)\n", pen, dashCap);
229
230     if(!pen || !dashCap)
231         return InvalidParameter;
232
233     *dashCap = pen->dashcap;
234
235     return Ok;
236 }
237
238 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
239 {
240     TRACE("(%p, %p)\n", pen, count);
241
242     if(!pen || !count)
243         return InvalidParameter;
244
245     *count = pen->numdashes;
246
247     return Ok;
248 }
249
250 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
251 {
252     TRACE("(%p, %p)\n", pen, offset);
253
254     if(!pen || !offset)
255         return InvalidParameter;
256
257     *offset = pen->offset;
258
259     return Ok;
260 }
261
262 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
263 {
264     TRACE("(%p, %p)\n", pen, dash);
265
266     if(!pen || !dash)
267         return InvalidParameter;
268
269     *dash = pen->dash;
270
271     return Ok;
272 }
273
274 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
275 {
276     TRACE("(%p, %p)\n", pen, endCap);
277
278     if(!pen || !endCap)
279         return InvalidParameter;
280
281     *endCap = pen->endcap;
282
283     return Ok;
284 }
285
286 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
287 {
288     TRACE("(%p, %p)\n", pen, lineJoin);
289
290     if(!pen || !lineJoin)
291         return InvalidParameter;
292
293     *lineJoin = pen->join;
294
295     return Ok;
296 }
297
298 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
299 {
300     TRACE("(%p, %p)\n", pen, mode);
301
302     if(!pen || !mode)
303         return InvalidParameter;
304
305     *mode = pen->align;
306
307     return Ok;
308 }
309
310 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
311 {
312     TRACE("(%p, %p)\n", pen, miterLimit);
313
314     if(!pen || !miterLimit)
315         return InvalidParameter;
316
317     *miterLimit = pen->miterlimit;
318
319     return Ok;
320 }
321
322 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
323 {
324     TRACE("(%p, %p)\n", pen, startCap);
325
326     if(!pen || !startCap)
327         return InvalidParameter;
328
329     *startCap = pen->startcap;
330
331     return Ok;
332 }
333
334 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
335 {
336     TRACE("(%p, %p)\n", pen, unit);
337
338     if(!pen || !unit)
339         return InvalidParameter;
340
341     *unit = pen->unit;
342
343     return Ok;
344 }
345
346 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
347 {
348     TRACE("(%p, %p)\n", pen, width);
349
350     if(!pen || !width)
351         return InvalidParameter;
352
353     *width = pen->width;
354
355     return Ok;
356 }
357
358 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
359 {
360     TRACE("(%p, %p)\n", pen, brush);
361
362     if(!pen || !brush)
363         return InvalidParameter;
364
365     GdipDeleteBrush(pen->brush);
366     return GdipCloneBrush(brush, &pen->brush);
367 }
368
369 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
370 {
371     TRACE("(%p, %x)\n", pen, argb);
372
373     if(!pen)
374         return InvalidParameter;
375
376     if(pen->brush->bt != BrushTypeSolidColor)
377         return NotImplemented;
378
379     return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
380 }
381
382 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
383 {
384     GpCustomLineCap * cap;
385     GpStatus ret;
386
387     TRACE("(%p, %p)\n", pen, customCap);
388
389     /* native crashes on pen == NULL, customCap != NULL */
390     if(!customCap) return InvalidParameter;
391
392     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
393         GdipDeleteCustomLineCap(pen->customend);
394         pen->endcap = LineCapCustom;
395         pen->customend = cap;
396     }
397
398     return ret;
399 }
400
401 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
402 {
403     GpCustomLineCap * cap;
404     GpStatus ret;
405
406     TRACE("(%p, %p)\n", pen, customCap);
407
408     /* native crashes on pen == NULL, customCap != NULL */
409     if(!customCap) return InvalidParameter;
410
411     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
412         GdipDeleteCustomLineCap(pen->customstart);
413         pen->startcap = LineCapCustom;
414         pen->customstart = cap;
415     }
416
417     return ret;
418 }
419
420 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
421     INT count)
422 {
423     INT i;
424     REAL sum = 0;
425
426     TRACE("(%p, %p, %d)\n", pen, dash, count);
427
428     if(!pen || !dash)
429         return InvalidParameter;
430
431     if(count <= 0)
432         return OutOfMemory;
433
434     for(i = 0; i < count; i++){
435         sum += dash[i];
436         if(dash[i] < 0.0)
437             return InvalidParameter;
438     }
439
440     if(sum == 0.0 && count)
441         return InvalidParameter;
442
443     GdipFree(pen->dashes);
444     pen->dashes = NULL;
445
446     if(count > 0)
447         pen->dashes = GdipAlloc(count * sizeof(REAL));
448     if(!pen->dashes){
449         pen->numdashes = 0;
450         return OutOfMemory;
451     }
452
453     GdipSetPenDashStyle(pen, DashStyleCustom);
454     memcpy(pen->dashes, dash, count * sizeof(REAL));
455     pen->numdashes = count;
456
457     return Ok;
458 }
459
460 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
461 {
462     TRACE("(%p, %d)\n", pen, dashCap);
463
464     if(!pen)
465         return InvalidParameter;
466
467     pen->dashcap = dashCap;
468
469     return Ok;
470 }
471
472 /* FIXME: dash offset not used */
473 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
474 {
475     TRACE("(%p, %.2f)\n", pen, offset);
476
477     if(!pen)
478         return InvalidParameter;
479
480     pen->offset = offset;
481
482     return Ok;
483 }
484
485 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
486 {
487     TRACE("(%p, %d)\n", pen, dash);
488
489     if(!pen)
490         return InvalidParameter;
491
492     if(dash != DashStyleCustom){
493         GdipFree(pen->dashes);
494         pen->dashes = NULL;
495         pen->numdashes = 0;
496     }
497
498     pen->dash = dash;
499     pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
500                     PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
501     pen->style |= gdip_to_gdi_dash(dash);
502
503     return Ok;
504 }
505
506 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
507 {
508     TRACE("(%p, %d)\n", pen, cap);
509
510     if(!pen)    return InvalidParameter;
511
512     /* The old custom cap gets deleted even if the new style is LineCapCustom. */
513     GdipDeleteCustomLineCap(pen->customend);
514     pen->customend = NULL;
515     pen->endcap = cap;
516
517     return Ok;
518 }
519
520 /* FIXME: startcap, dashcap not used. */
521 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
522     GpLineCap end, GpDashCap dash)
523 {
524     TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
525
526     if(!pen)
527         return InvalidParameter;
528
529     GdipDeleteCustomLineCap(pen->customend);
530     GdipDeleteCustomLineCap(pen->customstart);
531     pen->customend = NULL;
532     pen->customstart = NULL;
533
534     pen->startcap = start;
535     pen->endcap = end;
536     pen->dashcap = dash;
537
538     return Ok;
539 }
540
541 /* FIXME: Miter line joins behave a bit differently than they do in windows.
542  * Both kinds of miter joins clip if the angle is less than 11 degrees. */
543 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
544 {
545     TRACE("(%p, %d)\n", pen, join);
546
547     if(!pen)    return InvalidParameter;
548
549     pen->join = join;
550     pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
551     pen->style |= gdip_to_gdi_join(join);
552
553     return Ok;
554 }
555
556 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
557 {
558     TRACE("(%p, %.2f)\n", pen, limit);
559
560     if(!pen)
561         return InvalidParameter;
562
563     pen->miterlimit = limit;
564
565     return Ok;
566 }
567
568 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
569 {
570     TRACE("(%p, %d)\n", pen, cap);
571
572     if(!pen)    return InvalidParameter;
573
574     GdipDeleteCustomLineCap(pen->customstart);
575     pen->customstart = NULL;
576     pen->startcap = cap;
577
578     return Ok;
579 }
580
581 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
582 {
583     TRACE("(%p, %.2f)\n", pen, width);
584
585     if(!pen)    return InvalidParameter;
586
587     pen->width = width;
588
589     return Ok;
590 }
591
592 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
593 {
594     TRACE("(%p, %d)\n", pen, mode);
595
596     if(!pen)    return InvalidParameter;
597
598     pen->align = mode;
599
600     return Ok;
601 }