Release 8.16.
[wine.git] / dlls / gdiplus / pen.c
blob6446b725e4dd2cdabc0adca0005a1f559f419698
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("(%lx, %.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 gp_pen->compound_array = NULL;
175 gp_pen->compound_array_size = 0;
176 GdipSetMatrixElements(&gp_pen->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
178 if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
179 FIXME("UnitWorld, UnitPixel only supported units\n");
180 heap_free(gp_pen);
181 return NotImplemented;
184 GdipCloneBrush(brush, &clone_brush);
185 gp_pen->brush = clone_brush;
187 *pen = gp_pen;
189 TRACE("<-- %p\n", *pen);
191 return Ok;
194 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
196 TRACE("(%p)\n", pen);
198 if(!pen) return InvalidParameter;
200 GdipDeleteBrush(pen->brush);
201 GdipDeleteCustomLineCap(pen->customstart);
202 GdipDeleteCustomLineCap(pen->customend);
203 heap_free(pen->compound_array);
204 heap_free(pen->dashes);
205 heap_free(pen);
207 return Ok;
210 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
212 TRACE("(%p, %p)\n", pen, brush);
214 if(!pen || !brush)
215 return InvalidParameter;
217 return GdipCloneBrush(pen->brush, brush);
220 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
222 TRACE("(%p, %p)\n", pen, argb);
224 if(!pen || !argb)
225 return InvalidParameter;
227 if(pen->brush->bt != BrushTypeSolidColor)
228 return NotImplemented;
230 return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
233 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
235 TRACE("(%p, %p)\n", pen, customCap);
237 if(!pen || !customCap)
238 return InvalidParameter;
240 if(!pen->customend){
241 *customCap = NULL;
242 return Ok;
245 return GdipCloneCustomLineCap(pen->customend, customCap);
248 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
250 TRACE("(%p, %p)\n", pen, customCap);
252 if(!pen || !customCap)
253 return InvalidParameter;
255 if(!pen->customstart){
256 *customCap = NULL;
257 return Ok;
260 return GdipCloneCustomLineCap(pen->customstart, customCap);
263 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
265 TRACE("(%p, %p, %d)\n", pen, dash, count);
267 if(!pen || !dash || count > pen->numdashes)
268 return InvalidParameter;
270 /* note: if you pass a negative value for count, it crashes native gdiplus. */
271 if(count < 0)
272 return GenericError;
274 memcpy(dash, pen->dashes, count * sizeof(REAL));
276 return Ok;
279 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
281 TRACE("(%p, %p)\n", pen, dashCap);
283 if(!pen || !dashCap)
284 return InvalidParameter;
286 *dashCap = pen->dashcap;
288 return Ok;
291 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
293 TRACE("(%p, %p)\n", pen, count);
295 if(!pen || !count)
296 return InvalidParameter;
298 *count = pen->numdashes;
300 return Ok;
303 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
305 TRACE("(%p, %p)\n", pen, offset);
307 if(!pen || !offset)
308 return InvalidParameter;
310 *offset = pen->offset;
312 return Ok;
315 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
317 TRACE("(%p, %p)\n", pen, dash);
319 if(!pen || !dash)
320 return InvalidParameter;
322 *dash = pen->dash;
324 return Ok;
327 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
329 TRACE("(%p, %p)\n", pen, endCap);
331 if(!pen || !endCap)
332 return InvalidParameter;
334 *endCap = pen->endcap;
336 return Ok;
339 GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
341 TRACE("(%p, %p)\n", pen, type);
343 if(!pen || !type)
344 return InvalidParameter;
346 *type = bt_to_pt(pen->brush->bt);
348 return Ok;
351 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
353 TRACE("(%p, %p)\n", pen, lineJoin);
355 if(!pen || !lineJoin)
356 return InvalidParameter;
358 *lineJoin = pen->join;
360 return Ok;
363 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
365 TRACE("(%p, %p)\n", pen, mode);
367 if(!pen || !mode)
368 return InvalidParameter;
370 *mode = pen->align;
372 return Ok;
375 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
377 TRACE("(%p, %p)\n", pen, miterLimit);
379 if(!pen || !miterLimit)
380 return InvalidParameter;
382 *miterLimit = pen->miterlimit;
384 return Ok;
387 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
389 TRACE("(%p, %p)\n", pen, startCap);
391 if(!pen || !startCap)
392 return InvalidParameter;
394 *startCap = pen->startcap;
396 return Ok;
399 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
401 TRACE("(%p, %p)\n", pen, unit);
403 if(!pen || !unit)
404 return InvalidParameter;
406 *unit = pen->unit;
408 return Ok;
411 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
413 TRACE("(%p, %p)\n", pen, width);
415 if(!pen || !width)
416 return InvalidParameter;
418 *width = pen->width;
420 return Ok;
423 GpStatus WINGDIPAPI GdipResetPenTransform(GpPen *pen)
425 TRACE("(%p)\n", pen);
427 if(!pen)
428 return InvalidParameter;
430 GdipSetMatrixElements(&pen->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
432 return Ok;
435 GpStatus WINGDIPAPI GdipSetPenTransform(GpPen *pen, GpMatrix *matrix)
437 static int calls;
439 TRACE("(%p,%s)\n", pen, debugstr_matrix(matrix));
441 if(!pen || !matrix)
442 return InvalidParameter;
444 if(!(calls++))
445 FIXME("(%p,%p) Semi-stub\n", pen, matrix);
447 pen->transform = *matrix;
449 return Ok;
452 GpStatus WINGDIPAPI GdipGetPenTransform(GpPen *pen, GpMatrix *matrix)
454 TRACE("(%p,%s)\n", pen, debugstr_matrix(matrix));
456 if(!pen || !matrix)
457 return InvalidParameter;
459 *matrix = pen->transform;
461 return Ok;
464 GpStatus WINGDIPAPI GdipTranslatePenTransform(GpPen *pen, REAL dx, REAL dy, GpMatrixOrder order)
466 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, dx, dy, order);
468 if(!pen)
469 return InvalidParameter;
471 return GdipTranslateMatrix(&pen->transform, dx, dy, order);
474 GpStatus WINGDIPAPI GdipScalePenTransform(GpPen *pen, REAL sx, REAL sy, GpMatrixOrder order)
476 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, sx, sy, order);
478 if(!pen)
479 return InvalidParameter;
481 return GdipScaleMatrix(&pen->transform, sx, sy, order);
484 GpStatus WINGDIPAPI GdipRotatePenTransform(GpPen *pen, REAL angle, GpMatrixOrder order)
486 TRACE("(%p,%0.2f,%u)\n", pen, angle, order);
488 if(!pen)
489 return InvalidParameter;
491 return GdipRotateMatrix(&pen->transform, angle, order);
494 GpStatus WINGDIPAPI GdipMultiplyPenTransform(GpPen *pen, GDIPCONST GpMatrix *matrix,
495 GpMatrixOrder order)
497 TRACE("(%p,%s,%u)\n", pen, debugstr_matrix(matrix), order);
499 if(!pen)
500 return InvalidParameter;
502 return GdipMultiplyMatrix(&pen->transform, matrix, order);
505 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
507 TRACE("(%p, %p)\n", pen, brush);
509 if(!pen || !brush)
510 return InvalidParameter;
512 GdipDeleteBrush(pen->brush);
513 return GdipCloneBrush(brush, &pen->brush);
516 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
518 TRACE("(%p, %lx)\n", pen, argb);
520 if(!pen)
521 return InvalidParameter;
523 if(pen->brush->bt != BrushTypeSolidColor)
524 return NotImplemented;
526 return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
529 GpStatus WINGDIPAPI GdipGetPenCompoundArray(GpPen *pen, REAL *compoundarray, INT count)
531 TRACE("(%p, %p, %i)\n", pen, compoundarray, count);
533 if (!pen || !compoundarray || count > pen->compound_array_size)
534 return InvalidParameter;
535 if (pen->compound_array && count > 0)
536 memcpy(compoundarray, pen->compound_array, count * sizeof(REAL));
537 return Ok;
540 GpStatus WINGDIPAPI GdipGetPenCompoundCount(GpPen *pen, INT *count)
542 TRACE("(%p, %p)\n", pen, count);
544 if (!pen || !count)
545 return InvalidParameter;
546 *count = pen->compound_array_size;
547 return Ok;
550 GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *compoundarray,
551 INT count)
553 INT i;
554 REAL *tmp;
555 TRACE("(%p, %p, %i)\n", pen, compoundarray, count);
557 if(!pen || !compoundarray || count < 2 || count%2 == 1 || *compoundarray < 0.0 || *compoundarray > 1.0)
558 return InvalidParameter;
560 for(i = 1; i<count; i++)
562 if((compoundarray[i] < compoundarray[i - 1]) || (compoundarray[i] > 1.0))
563 return InvalidParameter;
566 tmp = heap_alloc_zero(count * sizeof(REAL));
567 if(!tmp)
568 return OutOfMemory;
569 heap_free(pen->compound_array);
570 pen->compound_array = tmp;
571 memcpy(pen->compound_array, compoundarray, count * sizeof(REAL));
572 pen->compound_array_size = count;
573 return Ok;
576 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
578 GpCustomLineCap * cap;
579 GpStatus ret;
581 TRACE("(%p, %p)\n", pen, customCap);
583 /* native crashes on pen == NULL, customCap != NULL */
584 if(!customCap) return InvalidParameter;
586 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
587 GdipDeleteCustomLineCap(pen->customend);
588 pen->endcap = LineCapCustom;
589 pen->customend = cap;
592 return ret;
595 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
597 GpCustomLineCap * cap;
598 GpStatus ret;
600 TRACE("(%p, %p)\n", pen, customCap);
602 /* native crashes on pen == NULL, customCap != NULL */
603 if(!customCap) return InvalidParameter;
605 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
606 GdipDeleteCustomLineCap(pen->customstart);
607 pen->startcap = LineCapCustom;
608 pen->customstart = cap;
611 return ret;
614 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
615 INT count)
617 INT i;
618 REAL sum = 0;
620 TRACE("(%p, %p, %d)\n", pen, dash, count);
622 if(!pen || !dash)
623 return InvalidParameter;
625 if(count <= 0)
626 return OutOfMemory;
628 for(i = 0; i < count; i++){
629 sum += dash[i];
630 if(dash[i] <= 0.0)
631 return InvalidParameter;
634 heap_free(pen->dashes);
635 pen->dashes = NULL;
637 if(count > 0)
638 pen->dashes = heap_alloc_zero(count * sizeof(REAL));
639 if(!pen->dashes){
640 pen->numdashes = 0;
641 return OutOfMemory;
644 GdipSetPenDashStyle(pen, DashStyleCustom);
645 memcpy(pen->dashes, dash, count * sizeof(REAL));
646 pen->numdashes = count;
648 return Ok;
651 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
653 TRACE("(%p, %d)\n", pen, dashCap);
655 if(!pen)
656 return InvalidParameter;
658 pen->dashcap = dashCap;
660 return Ok;
663 /* FIXME: dash offset not used */
664 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
666 TRACE("(%p, %.2f)\n", pen, offset);
668 if(!pen)
669 return InvalidParameter;
671 pen->offset = offset;
673 return Ok;
676 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
678 TRACE("(%p, %d)\n", pen, dash);
680 if(!pen)
681 return InvalidParameter;
683 if(dash != DashStyleCustom){
684 heap_free(pen->dashes);
685 pen->dashes = NULL;
686 pen->numdashes = 0;
689 pen->dash = dash;
690 pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
691 PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
692 pen->style |= gdip_to_gdi_dash(dash);
694 return Ok;
697 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
699 TRACE("(%p, %d)\n", pen, cap);
701 if(!pen) return InvalidParameter;
703 /* The old custom cap gets deleted even if the new style is LineCapCustom. */
704 GdipDeleteCustomLineCap(pen->customend);
705 pen->customend = NULL;
706 pen->endcap = cap;
708 return Ok;
711 /* FIXME: startcap, dashcap not used. */
712 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
713 GpLineCap end, GpDashCap dash)
715 TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
717 if(!pen)
718 return InvalidParameter;
720 GdipDeleteCustomLineCap(pen->customend);
721 GdipDeleteCustomLineCap(pen->customstart);
722 pen->customend = NULL;
723 pen->customstart = NULL;
725 pen->startcap = start;
726 pen->endcap = end;
727 pen->dashcap = dash;
729 return Ok;
732 /* FIXME: Miter line joins behave a bit differently than they do in windows.
733 * Both kinds of miter joins clip if the angle is less than 11 degrees. */
734 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
736 TRACE("(%p, %d)\n", pen, join);
738 if(!pen) return InvalidParameter;
740 pen->join = join;
741 pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
742 pen->style |= gdip_to_gdi_join(join);
744 return Ok;
747 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
749 TRACE("(%p, %.2f)\n", pen, limit);
751 if(!pen)
752 return InvalidParameter;
754 pen->miterlimit = limit;
756 return Ok;
759 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
761 TRACE("(%p, %d)\n", pen, cap);
763 if(!pen) return InvalidParameter;
765 GdipDeleteCustomLineCap(pen->customstart);
766 pen->customstart = NULL;
767 pen->startcap = cap;
769 return Ok;
772 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
774 TRACE("(%p, %.2f)\n", pen, width);
776 if(!pen) return InvalidParameter;
778 pen->width = width;
780 return Ok;
783 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
785 TRACE("(%p, %d)\n", pen, mode);
787 if(!pen) return InvalidParameter;
789 pen->align = mode;
791 return Ok;