gdiplus: Stub GdipGetPathGradientTransform.
[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 static GpPenType bt_to_pt(GpBrushType bt)
71 {
72     switch(bt){
73         case BrushTypeSolidColor:
74             return PenTypeSolidColor;
75         case BrushTypeHatchFill:
76             return PenTypeHatchFill;
77         case BrushTypeTextureFill:
78             return PenTypeTextureFill;
79         case BrushTypePathGradient:
80             return PenTypePathGradient;
81         case BrushTypeLinearGradient:
82             return PenTypeLinearGradient;
83         default:
84             return PenTypeUnknown;
85     }
86 }
87
88 GpStatus WINGDIPAPI GdipClonePen(GpPen *pen, GpPen **clonepen)
89 {
90     TRACE("(%p, %p)\n", pen, clonepen);
91
92     if(!pen || !clonepen)
93         return InvalidParameter;
94
95     *clonepen = GdipAlloc(sizeof(GpPen));
96     if(!*clonepen)  return OutOfMemory;
97
98     **clonepen = *pen;
99
100     GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
101     GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
102     GdipCloneBrush(pen->brush, &(*clonepen)->brush);
103
104     TRACE("<-- %p\n", *clonepen);
105
106     return Ok;
107 }
108
109 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
110     GpPen **pen)
111 {
112     GpBrush *brush;
113     GpStatus status;
114
115     TRACE("(%x, %.2f, %d, %p)\n", color, width, unit, pen);
116
117     GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
118     status = GdipCreatePen2(brush, width, unit, pen);
119     GdipDeleteBrush(brush);
120     return status;
121 }
122
123 GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
124     GpPen **pen)
125 {
126     GpPen *gp_pen;
127     GpBrush *clone_brush;
128
129     TRACE("(%p, %.2f, %d, %p)\n", brush, width, unit, pen);
130
131     if(!pen || !brush)
132         return InvalidParameter;
133
134     gp_pen = GdipAlloc(sizeof(GpPen));
135     if(!gp_pen)    return OutOfMemory;
136
137     gp_pen->style = GP_DEFAULT_PENSTYLE;
138     gp_pen->width = width;
139     gp_pen->unit = unit;
140     gp_pen->endcap = LineCapFlat;
141     gp_pen->join = LineJoinMiter;
142     gp_pen->miterlimit = 10.0;
143     gp_pen->dash = DashStyleSolid;
144     gp_pen->offset = 0.0;
145     gp_pen->customstart = NULL;
146     gp_pen->customend = NULL;
147
148     if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
149         FIXME("UnitWorld, UnitPixel only supported units\n");
150         GdipFree(gp_pen);
151         return NotImplemented;
152     }
153
154     GdipCloneBrush(brush, &clone_brush);
155     gp_pen->brush = clone_brush;
156
157     *pen = gp_pen;
158
159     TRACE("<-- %p\n", *pen);
160
161     return Ok;
162 }
163
164 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
165 {
166     TRACE("(%p)\n", pen);
167
168     if(!pen)    return InvalidParameter;
169
170     GdipDeleteBrush(pen->brush);
171     GdipDeleteCustomLineCap(pen->customstart);
172     GdipDeleteCustomLineCap(pen->customend);
173     GdipFree(pen->dashes);
174     GdipFree(pen);
175
176     return Ok;
177 }
178
179 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
180 {
181     TRACE("(%p, %p)\n", pen, brush);
182
183     if(!pen || !brush)
184         return InvalidParameter;
185
186     return GdipCloneBrush(pen->brush, brush);
187 }
188
189 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
190 {
191     TRACE("(%p, %p)\n", pen, argb);
192
193     if(!pen || !argb)
194         return InvalidParameter;
195
196     if(pen->brush->bt != BrushTypeSolidColor)
197         return NotImplemented;
198
199     return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
200 }
201
202 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
203 {
204     TRACE("(%p, %p)\n", pen, customCap);
205
206     if(!pen || !customCap)
207         return InvalidParameter;
208
209     if(!pen->customend){
210         *customCap = NULL;
211         return Ok;
212     }
213
214     return GdipCloneCustomLineCap(pen->customend, customCap);
215 }
216
217 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
218 {
219     TRACE("(%p, %p)\n", pen, customCap);
220
221     if(!pen || !customCap)
222         return InvalidParameter;
223
224     if(!pen->customstart){
225         *customCap = NULL;
226         return Ok;
227     }
228
229     return GdipCloneCustomLineCap(pen->customstart, customCap);
230 }
231
232 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
233 {
234     TRACE("(%p, %p, %d)\n", pen, dash, count);
235
236     if(!pen || !dash || count > pen->numdashes)
237         return InvalidParameter;
238
239     /* note: if you pass a negative value for count, it crashes native gdiplus. */
240     if(count < 0)
241         return GenericError;
242
243     memcpy(dash, pen->dashes, count * sizeof(REAL));
244
245     return Ok;
246 }
247
248 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
249 {
250     TRACE("(%p, %p)\n", pen, dashCap);
251
252     if(!pen || !dashCap)
253         return InvalidParameter;
254
255     *dashCap = pen->dashcap;
256
257     return Ok;
258 }
259
260 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
261 {
262     TRACE("(%p, %p)\n", pen, count);
263
264     if(!pen || !count)
265         return InvalidParameter;
266
267     *count = pen->numdashes;
268
269     return Ok;
270 }
271
272 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
273 {
274     TRACE("(%p, %p)\n", pen, offset);
275
276     if(!pen || !offset)
277         return InvalidParameter;
278
279     *offset = pen->offset;
280
281     return Ok;
282 }
283
284 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
285 {
286     TRACE("(%p, %p)\n", pen, dash);
287
288     if(!pen || !dash)
289         return InvalidParameter;
290
291     *dash = pen->dash;
292
293     return Ok;
294 }
295
296 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
297 {
298     TRACE("(%p, %p)\n", pen, endCap);
299
300     if(!pen || !endCap)
301         return InvalidParameter;
302
303     *endCap = pen->endcap;
304
305     return Ok;
306 }
307
308 GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
309 {
310     TRACE("(%p, %p)\n", pen, type);
311
312     if(!pen || !type)
313         return InvalidParameter;
314
315     *type = bt_to_pt(pen->brush->bt);
316
317     return Ok;
318 }
319
320 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
321 {
322     TRACE("(%p, %p)\n", pen, lineJoin);
323
324     if(!pen || !lineJoin)
325         return InvalidParameter;
326
327     *lineJoin = pen->join;
328
329     return Ok;
330 }
331
332 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
333 {
334     TRACE("(%p, %p)\n", pen, mode);
335
336     if(!pen || !mode)
337         return InvalidParameter;
338
339     *mode = pen->align;
340
341     return Ok;
342 }
343
344 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
345 {
346     TRACE("(%p, %p)\n", pen, miterLimit);
347
348     if(!pen || !miterLimit)
349         return InvalidParameter;
350
351     *miterLimit = pen->miterlimit;
352
353     return Ok;
354 }
355
356 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
357 {
358     TRACE("(%p, %p)\n", pen, startCap);
359
360     if(!pen || !startCap)
361         return InvalidParameter;
362
363     *startCap = pen->startcap;
364
365     return Ok;
366 }
367
368 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
369 {
370     TRACE("(%p, %p)\n", pen, unit);
371
372     if(!pen || !unit)
373         return InvalidParameter;
374
375     *unit = pen->unit;
376
377     return Ok;
378 }
379
380 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
381 {
382     TRACE("(%p, %p)\n", pen, width);
383
384     if(!pen || !width)
385         return InvalidParameter;
386
387     *width = pen->width;
388
389     return Ok;
390 }
391
392 GpStatus WINGDIPAPI GdipResetPenTransform(GpPen *pen)
393 {
394     static int calls;
395
396     TRACE("(%p)\n", pen);
397
398     if(!pen)
399         return InvalidParameter;
400
401     if(!(calls++))
402         FIXME("(%p) stub\n", pen);
403
404     return NotImplemented;
405 }
406
407 GpStatus WINGDIPAPI GdipSetPenTransform(GpPen *pen, GpMatrix *matrix)
408 {
409     static int calls;
410
411     TRACE("(%p,%p)\n", pen, matrix);
412
413     if(!pen || !matrix)
414         return InvalidParameter;
415
416     if(!(calls++))
417         FIXME("not implemented\n");
418
419     return NotImplemented;
420 }
421
422 GpStatus WINGDIPAPI GdipGetPenTransform(GpPen *pen, GpMatrix *matrix)
423 {
424     static int calls;
425
426     TRACE("(%p,%p)\n", pen, matrix);
427
428     if(!pen || !matrix)
429         return InvalidParameter;
430
431     if(!(calls++))
432         FIXME("not implemented\n");
433
434     return NotImplemented;
435 }
436
437 GpStatus WINGDIPAPI GdipScalePenTransform(GpPen *pen, REAL sx, REAL sy, GpMatrixOrder order)
438 {
439     static int calls;
440
441     TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, sx, sy, order);
442
443     if(!pen)
444         return InvalidParameter;
445
446     if(!(calls++))
447         FIXME("(%p, %.2f, %.2f, %d) stub\n", pen, sx, sy, order);
448
449     return NotImplemented;
450 }
451
452 GpStatus WINGDIPAPI GdipRotatePenTransform(GpPen *pen, REAL angle, GpMatrixOrder order)
453 {
454     static int calls;
455
456     TRACE("(%p,%0.2f,%u)\n", pen, angle, order);
457
458     if(!pen)
459         return InvalidParameter;
460
461     if(!(calls++))
462         FIXME("not implemented\n");
463
464     return NotImplemented;
465 }
466
467 GpStatus WINGDIPAPI GdipMultiplyPenTransform(GpPen *pen, GDIPCONST GpMatrix *matrix,
468     GpMatrixOrder order)
469 {
470     static int calls;
471
472     TRACE("(%p,%p,%u)\n", pen, matrix, order);
473
474     if(!pen)
475         return InvalidParameter;
476
477     if(!(calls++))
478         FIXME("not implemented\n");
479
480     return NotImplemented;
481 }
482
483 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
484 {
485     TRACE("(%p, %p)\n", pen, brush);
486
487     if(!pen || !brush)
488         return InvalidParameter;
489
490     GdipDeleteBrush(pen->brush);
491     return GdipCloneBrush(brush, &pen->brush);
492 }
493
494 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
495 {
496     TRACE("(%p, %x)\n", pen, argb);
497
498     if(!pen)
499         return InvalidParameter;
500
501     if(pen->brush->bt != BrushTypeSolidColor)
502         return NotImplemented;
503
504     return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
505 }
506
507 GpStatus WINGDIPAPI GdipGetPenCompoundCount(GpPen *pen, INT *count)
508 {
509     FIXME("(%p, %p): stub\n", pen, count);
510
511     if (!pen || !count)
512         return InvalidParameter;
513
514     return NotImplemented;
515 }
516
517 GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *dash,
518     INT count)
519 {
520     FIXME("(%p, %p, %i): stub\n", pen, dash, count);
521
522     if (!pen || !dash || count < 2 || count%2 == 1)
523         return InvalidParameter;
524
525     return NotImplemented;
526 }
527
528 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
529 {
530     GpCustomLineCap * cap;
531     GpStatus ret;
532
533     TRACE("(%p, %p)\n", pen, customCap);
534
535     /* native crashes on pen == NULL, customCap != NULL */
536     if(!customCap) return InvalidParameter;
537
538     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
539         GdipDeleteCustomLineCap(pen->customend);
540         pen->endcap = LineCapCustom;
541         pen->customend = cap;
542     }
543
544     return ret;
545 }
546
547 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
548 {
549     GpCustomLineCap * cap;
550     GpStatus ret;
551
552     TRACE("(%p, %p)\n", pen, customCap);
553
554     /* native crashes on pen == NULL, customCap != NULL */
555     if(!customCap) return InvalidParameter;
556
557     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
558         GdipDeleteCustomLineCap(pen->customstart);
559         pen->startcap = LineCapCustom;
560         pen->customstart = cap;
561     }
562
563     return ret;
564 }
565
566 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
567     INT count)
568 {
569     INT i;
570     REAL sum = 0;
571
572     TRACE("(%p, %p, %d)\n", pen, dash, count);
573
574     if(!pen || !dash)
575         return InvalidParameter;
576
577     if(count <= 0)
578         return OutOfMemory;
579
580     for(i = 0; i < count; i++){
581         sum += dash[i];
582         if(dash[i] < 0.0)
583             return InvalidParameter;
584     }
585
586     if(sum == 0.0 && count)
587         return InvalidParameter;
588
589     GdipFree(pen->dashes);
590     pen->dashes = NULL;
591
592     if(count > 0)
593         pen->dashes = GdipAlloc(count * sizeof(REAL));
594     if(!pen->dashes){
595         pen->numdashes = 0;
596         return OutOfMemory;
597     }
598
599     GdipSetPenDashStyle(pen, DashStyleCustom);
600     memcpy(pen->dashes, dash, count * sizeof(REAL));
601     pen->numdashes = count;
602
603     return Ok;
604 }
605
606 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
607 {
608     TRACE("(%p, %d)\n", pen, dashCap);
609
610     if(!pen)
611         return InvalidParameter;
612
613     pen->dashcap = dashCap;
614
615     return Ok;
616 }
617
618 /* FIXME: dash offset not used */
619 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
620 {
621     TRACE("(%p, %.2f)\n", pen, offset);
622
623     if(!pen)
624         return InvalidParameter;
625
626     pen->offset = offset;
627
628     return Ok;
629 }
630
631 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
632 {
633     TRACE("(%p, %d)\n", pen, dash);
634
635     if(!pen)
636         return InvalidParameter;
637
638     if(dash != DashStyleCustom){
639         GdipFree(pen->dashes);
640         pen->dashes = NULL;
641         pen->numdashes = 0;
642     }
643
644     pen->dash = dash;
645     pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
646                     PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
647     pen->style |= gdip_to_gdi_dash(dash);
648
649     return Ok;
650 }
651
652 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
653 {
654     TRACE("(%p, %d)\n", pen, cap);
655
656     if(!pen)    return InvalidParameter;
657
658     /* The old custom cap gets deleted even if the new style is LineCapCustom. */
659     GdipDeleteCustomLineCap(pen->customend);
660     pen->customend = NULL;
661     pen->endcap = cap;
662
663     return Ok;
664 }
665
666 /* FIXME: startcap, dashcap not used. */
667 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
668     GpLineCap end, GpDashCap dash)
669 {
670     TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
671
672     if(!pen)
673         return InvalidParameter;
674
675     GdipDeleteCustomLineCap(pen->customend);
676     GdipDeleteCustomLineCap(pen->customstart);
677     pen->customend = NULL;
678     pen->customstart = NULL;
679
680     pen->startcap = start;
681     pen->endcap = end;
682     pen->dashcap = dash;
683
684     return Ok;
685 }
686
687 /* FIXME: Miter line joins behave a bit differently than they do in windows.
688  * Both kinds of miter joins clip if the angle is less than 11 degrees. */
689 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
690 {
691     TRACE("(%p, %d)\n", pen, join);
692
693     if(!pen)    return InvalidParameter;
694
695     pen->join = join;
696     pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
697     pen->style |= gdip_to_gdi_join(join);
698
699     return Ok;
700 }
701
702 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
703 {
704     TRACE("(%p, %.2f)\n", pen, limit);
705
706     if(!pen)
707         return InvalidParameter;
708
709     pen->miterlimit = limit;
710
711     return Ok;
712 }
713
714 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
715 {
716     TRACE("(%p, %d)\n", pen, cap);
717
718     if(!pen)    return InvalidParameter;
719
720     GdipDeleteCustomLineCap(pen->customstart);
721     pen->customstart = NULL;
722     pen->startcap = cap;
723
724     return Ok;
725 }
726
727 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
728 {
729     TRACE("(%p, %.2f)\n", pen, width);
730
731     if(!pen)    return InvalidParameter;
732
733     pen->width = width;
734
735     return Ok;
736 }
737
738 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
739 {
740     TRACE("(%p, %d)\n", pen, mode);
741
742     if(!pen)    return InvalidParameter;
743
744     pen->align = mode;
745
746     return Ok;
747 }