d2d1: Add a helper to set error state.
[wine.git] / dlls / gdiplus / pen.c
blobb86808b87e621a7e2f17fc907cad91dfc846fbf8
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
25 #include "objbase.h"
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
33 static DWORD gdip_to_gdi_dash(GpDashStyle dash)
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;
54 static DWORD gdip_to_gdi_join(GpLineJoin join)
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;
70 static GpPenType bt_to_pt(GpBrushType bt)
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;
88 GpStatus WINGDIPAPI GdipClonePen(GpPen *pen, GpPen **clonepen)
90 GpStatus stat;
92 TRACE("(%p, %p)\n", pen, clonepen);
94 if(!pen || !clonepen)
95 return InvalidParameter;
97 *clonepen = heap_alloc_zero(sizeof(GpPen));
98 if(!*clonepen) return OutOfMemory;
100 **clonepen = *pen;
102 (*clonepen)->customstart = NULL;
103 (*clonepen)->customend = NULL;
104 (*clonepen)->brush = NULL;
105 (*clonepen)->dashes = NULL;
107 stat = GdipCloneBrush(pen->brush, &(*clonepen)->brush);
109 if (stat == Ok && pen->customstart)
110 stat = GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
112 if (stat == Ok && pen->customend)
113 stat = GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
115 if (stat == Ok && pen->dashes)
117 (*clonepen)->dashes = heap_alloc_zero(pen->numdashes * sizeof(REAL));
118 if ((*clonepen)->dashes)
119 memcpy((*clonepen)->dashes, pen->dashes, pen->numdashes * sizeof(REAL));
120 else
121 stat = OutOfMemory;
124 if (stat != Ok)
126 GdipDeletePen(*clonepen);
127 *clonepen = NULL;
128 return stat;
131 TRACE("<-- %p\n", *clonepen);
133 return Ok;
136 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
137 GpPen **pen)
139 GpBrush *brush;
140 GpStatus status;
142 TRACE("(%x, %.2f, %d, %p)\n", color, width, unit, pen);
144 GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
145 status = GdipCreatePen2(brush, width, unit, pen);
146 GdipDeleteBrush(brush);
147 return status;
150 GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
151 GpPen **pen)
153 GpPen *gp_pen;
154 GpBrush *clone_brush;
156 TRACE("(%p, %.2f, %d, %p)\n", brush, width, unit, pen);
158 if(!pen || !brush)
159 return InvalidParameter;
161 gp_pen = heap_alloc_zero(sizeof(GpPen));
162 if(!gp_pen) return OutOfMemory;
164 gp_pen->style = GP_DEFAULT_PENSTYLE;
165 gp_pen->width = width;
166 gp_pen->unit = unit;
167 gp_pen->endcap = LineCapFlat;
168 gp_pen->join = LineJoinMiter;
169 gp_pen->miterlimit = 10.0;
170 gp_pen->dash = DashStyleSolid;
171 gp_pen->offset = 0.0;
172 gp_pen->customstart = NULL;
173 gp_pen->customend = NULL;
174 GdipSetMatrixElements(&gp_pen->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
176 if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
177 FIXME("UnitWorld, UnitPixel only supported units\n");
178 heap_free(gp_pen);
179 return NotImplemented;
182 GdipCloneBrush(brush, &clone_brush);
183 gp_pen->brush = clone_brush;
185 *pen = gp_pen;
187 TRACE("<-- %p\n", *pen);
189 return Ok;
192 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
194 TRACE("(%p)\n", pen);
196 if(!pen) return InvalidParameter;
198 GdipDeleteBrush(pen->brush);
199 GdipDeleteCustomLineCap(pen->customstart);
200 GdipDeleteCustomLineCap(pen->customend);
201 heap_free(pen->dashes);
202 heap_free(pen);
204 return Ok;
207 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
209 TRACE("(%p, %p)\n", pen, brush);
211 if(!pen || !brush)
212 return InvalidParameter;
214 return GdipCloneBrush(pen->brush, brush);
217 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
219 TRACE("(%p, %p)\n", pen, argb);
221 if(!pen || !argb)
222 return InvalidParameter;
224 if(pen->brush->bt != BrushTypeSolidColor)
225 return NotImplemented;
227 return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
230 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
232 TRACE("(%p, %p)\n", pen, customCap);
234 if(!pen || !customCap)
235 return InvalidParameter;
237 if(!pen->customend){
238 *customCap = NULL;
239 return Ok;
242 return GdipCloneCustomLineCap(pen->customend, customCap);
245 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
247 TRACE("(%p, %p)\n", pen, customCap);
249 if(!pen || !customCap)
250 return InvalidParameter;
252 if(!pen->customstart){
253 *customCap = NULL;
254 return Ok;
257 return GdipCloneCustomLineCap(pen->customstart, customCap);
260 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
262 TRACE("(%p, %p, %d)\n", pen, dash, count);
264 if(!pen || !dash || count > pen->numdashes)
265 return InvalidParameter;
267 /* note: if you pass a negative value for count, it crashes native gdiplus. */
268 if(count < 0)
269 return GenericError;
271 memcpy(dash, pen->dashes, count * sizeof(REAL));
273 return Ok;
276 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
278 TRACE("(%p, %p)\n", pen, dashCap);
280 if(!pen || !dashCap)
281 return InvalidParameter;
283 *dashCap = pen->dashcap;
285 return Ok;
288 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
290 TRACE("(%p, %p)\n", pen, count);
292 if(!pen || !count)
293 return InvalidParameter;
295 *count = pen->numdashes;
297 return Ok;
300 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
302 TRACE("(%p, %p)\n", pen, offset);
304 if(!pen || !offset)
305 return InvalidParameter;
307 *offset = pen->offset;
309 return Ok;
312 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
314 TRACE("(%p, %p)\n", pen, dash);
316 if(!pen || !dash)
317 return InvalidParameter;
319 *dash = pen->dash;
321 return Ok;
324 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
326 TRACE("(%p, %p)\n", pen, endCap);
328 if(!pen || !endCap)
329 return InvalidParameter;
331 *endCap = pen->endcap;
333 return Ok;
336 GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
338 TRACE("(%p, %p)\n", pen, type);
340 if(!pen || !type)
341 return InvalidParameter;
343 *type = bt_to_pt(pen->brush->bt);
345 return Ok;
348 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
350 TRACE("(%p, %p)\n", pen, lineJoin);
352 if(!pen || !lineJoin)
353 return InvalidParameter;
355 *lineJoin = pen->join;
357 return Ok;
360 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
362 TRACE("(%p, %p)\n", pen, mode);
364 if(!pen || !mode)
365 return InvalidParameter;
367 *mode = pen->align;
369 return Ok;
372 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
374 TRACE("(%p, %p)\n", pen, miterLimit);
376 if(!pen || !miterLimit)
377 return InvalidParameter;
379 *miterLimit = pen->miterlimit;
381 return Ok;
384 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
386 TRACE("(%p, %p)\n", pen, startCap);
388 if(!pen || !startCap)
389 return InvalidParameter;
391 *startCap = pen->startcap;
393 return Ok;
396 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
398 TRACE("(%p, %p)\n", pen, unit);
400 if(!pen || !unit)
401 return InvalidParameter;
403 *unit = pen->unit;
405 return Ok;
408 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
410 TRACE("(%p, %p)\n", pen, width);
412 if(!pen || !width)
413 return InvalidParameter;
415 *width = pen->width;
417 return Ok;
420 GpStatus WINGDIPAPI GdipResetPenTransform(GpPen *pen)
422 TRACE("(%p)\n", pen);
424 if(!pen)
425 return InvalidParameter;
427 GdipSetMatrixElements(&pen->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
429 return Ok;
432 GpStatus WINGDIPAPI GdipSetPenTransform(GpPen *pen, GpMatrix *matrix)
434 static int calls;
436 TRACE("(%p,%p)\n", pen, matrix);
438 if(!pen || !matrix)
439 return InvalidParameter;
441 if(!(calls++))
442 FIXME("(%p,%p) Semi-stub\n", pen, matrix);
444 pen->transform = *matrix;
446 return Ok;
449 GpStatus WINGDIPAPI GdipGetPenTransform(GpPen *pen, GpMatrix *matrix)
451 TRACE("(%p,%p)\n", pen, matrix);
453 if(!pen || !matrix)
454 return InvalidParameter;
456 *matrix = pen->transform;
458 return Ok;
461 GpStatus WINGDIPAPI GdipTranslatePenTransform(GpPen *pen, REAL dx, REAL dy, GpMatrixOrder order)
463 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, dx, dy, order);
465 if(!pen)
466 return InvalidParameter;
468 return GdipTranslateMatrix(&pen->transform, dx, dy, order);
471 GpStatus WINGDIPAPI GdipScalePenTransform(GpPen *pen, REAL sx, REAL sy, GpMatrixOrder order)
473 static int calls;
475 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, sx, sy, order);
477 if(!pen)
478 return InvalidParameter;
480 if(!(calls++))
481 FIXME("(%p, %.2f, %.2f, %d) stub\n", pen, sx, sy, order);
483 return NotImplemented;
486 GpStatus WINGDIPAPI GdipRotatePenTransform(GpPen *pen, REAL angle, GpMatrixOrder order)
488 static int calls;
490 TRACE("(%p,%0.2f,%u)\n", pen, angle, order);
492 if(!pen)
493 return InvalidParameter;
495 if(!(calls++))
496 FIXME("not implemented\n");
498 return NotImplemented;
501 GpStatus WINGDIPAPI GdipMultiplyPenTransform(GpPen *pen, GDIPCONST GpMatrix *matrix,
502 GpMatrixOrder order)
504 static int calls;
506 TRACE("(%p,%p,%u)\n", pen, matrix, order);
508 if(!pen)
509 return InvalidParameter;
511 if(!(calls++))
512 FIXME("not implemented\n");
514 return NotImplemented;
517 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
519 TRACE("(%p, %p)\n", pen, brush);
521 if(!pen || !brush)
522 return InvalidParameter;
524 GdipDeleteBrush(pen->brush);
525 return GdipCloneBrush(brush, &pen->brush);
528 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
530 TRACE("(%p, %x)\n", pen, argb);
532 if(!pen)
533 return InvalidParameter;
535 if(pen->brush->bt != BrushTypeSolidColor)
536 return NotImplemented;
538 return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
541 GpStatus WINGDIPAPI GdipGetPenCompoundCount(GpPen *pen, INT *count)
543 FIXME("(%p, %p): stub\n", pen, count);
545 if (!pen || !count)
546 return InvalidParameter;
548 return NotImplemented;
551 GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *dash,
552 INT count)
554 FIXME("(%p, %p, %i): stub\n", pen, dash, count);
556 if (!pen || !dash || count < 2 || count%2 == 1)
557 return InvalidParameter;
559 return NotImplemented;
562 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
564 GpCustomLineCap * cap;
565 GpStatus ret;
567 TRACE("(%p, %p)\n", pen, customCap);
569 /* native crashes on pen == NULL, customCap != NULL */
570 if(!customCap) return InvalidParameter;
572 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
573 GdipDeleteCustomLineCap(pen->customend);
574 pen->endcap = LineCapCustom;
575 pen->customend = cap;
578 return ret;
581 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
583 GpCustomLineCap * cap;
584 GpStatus ret;
586 TRACE("(%p, %p)\n", pen, customCap);
588 /* native crashes on pen == NULL, customCap != NULL */
589 if(!customCap) return InvalidParameter;
591 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
592 GdipDeleteCustomLineCap(pen->customstart);
593 pen->startcap = LineCapCustom;
594 pen->customstart = cap;
597 return ret;
600 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
601 INT count)
603 INT i;
604 REAL sum = 0;
606 TRACE("(%p, %p, %d)\n", pen, dash, count);
608 if(!pen || !dash)
609 return InvalidParameter;
611 if(count <= 0)
612 return OutOfMemory;
614 for(i = 0; i < count; i++){
615 sum += dash[i];
616 if(dash[i] < 0.0)
617 return InvalidParameter;
620 if(sum == 0.0 && count)
621 return InvalidParameter;
623 heap_free(pen->dashes);
624 pen->dashes = NULL;
626 if(count > 0)
627 pen->dashes = heap_alloc_zero(count * sizeof(REAL));
628 if(!pen->dashes){
629 pen->numdashes = 0;
630 return OutOfMemory;
633 GdipSetPenDashStyle(pen, DashStyleCustom);
634 memcpy(pen->dashes, dash, count * sizeof(REAL));
635 pen->numdashes = count;
637 return Ok;
640 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
642 TRACE("(%p, %d)\n", pen, dashCap);
644 if(!pen)
645 return InvalidParameter;
647 pen->dashcap = dashCap;
649 return Ok;
652 /* FIXME: dash offset not used */
653 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
655 TRACE("(%p, %.2f)\n", pen, offset);
657 if(!pen)
658 return InvalidParameter;
660 pen->offset = offset;
662 return Ok;
665 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
667 TRACE("(%p, %d)\n", pen, dash);
669 if(!pen)
670 return InvalidParameter;
672 if(dash != DashStyleCustom){
673 heap_free(pen->dashes);
674 pen->dashes = NULL;
675 pen->numdashes = 0;
678 pen->dash = dash;
679 pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
680 PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
681 pen->style |= gdip_to_gdi_dash(dash);
683 return Ok;
686 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
688 TRACE("(%p, %d)\n", pen, cap);
690 if(!pen) return InvalidParameter;
692 /* The old custom cap gets deleted even if the new style is LineCapCustom. */
693 GdipDeleteCustomLineCap(pen->customend);
694 pen->customend = NULL;
695 pen->endcap = cap;
697 return Ok;
700 /* FIXME: startcap, dashcap not used. */
701 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
702 GpLineCap end, GpDashCap dash)
704 TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
706 if(!pen)
707 return InvalidParameter;
709 GdipDeleteCustomLineCap(pen->customend);
710 GdipDeleteCustomLineCap(pen->customstart);
711 pen->customend = NULL;
712 pen->customstart = NULL;
714 pen->startcap = start;
715 pen->endcap = end;
716 pen->dashcap = dash;
718 return Ok;
721 /* FIXME: Miter line joins behave a bit differently than they do in windows.
722 * Both kinds of miter joins clip if the angle is less than 11 degrees. */
723 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
725 TRACE("(%p, %d)\n", pen, join);
727 if(!pen) return InvalidParameter;
729 pen->join = join;
730 pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
731 pen->style |= gdip_to_gdi_join(join);
733 return Ok;
736 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
738 TRACE("(%p, %.2f)\n", pen, limit);
740 if(!pen)
741 return InvalidParameter;
743 pen->miterlimit = limit;
745 return Ok;
748 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
750 TRACE("(%p, %d)\n", pen, cap);
752 if(!pen) return InvalidParameter;
754 GdipDeleteCustomLineCap(pen->customstart);
755 pen->customstart = NULL;
756 pen->startcap = cap;
758 return Ok;
761 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
763 TRACE("(%p, %.2f)\n", pen, width);
765 if(!pen) return InvalidParameter;
767 pen->width = width;
769 return Ok;
772 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
774 TRACE("(%p, %d)\n", pen, mode);
776 if(!pen) return InvalidParameter;
778 pen->align = mode;
780 return Ok;