gdiplus: Add GdipGetPenCompoundCount implementation.
[wine.git] / dlls / gdiplus / pen.c
blob86e68e517fb8fa771beaece4a5b96cd0719bace6
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,%p)\n", pen, 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,%p)\n", pen, 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,%p,%u)\n", pen, 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 GdipGetPenCompoundCount(GpPen *pen, INT *count)
531 TRACE("(%p, %p)\n", pen, count);
533 if (!pen || !count)
534 return InvalidParameter;
535 *count = pen->compound_array_size;
536 return Ok;
539 GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *compoundarray,
540 INT count)
542 INT i;
543 REAL *tmp;
544 TRACE("(%p, %p, %i)\n", pen, compoundarray, count);
546 if(!pen || !compoundarray || count < 2 || count%2 == 1 || *compoundarray < 0.0 || *compoundarray > 1.0)
547 return InvalidParameter;
549 for(i = 1; i<count; i++)
551 if((compoundarray[i] < compoundarray[i - 1]) || (compoundarray[i] > 1.0))
552 return InvalidParameter;
555 tmp = heap_alloc_zero(count * sizeof(REAL));
556 if(!tmp)
557 return OutOfMemory;
558 heap_free(pen->compound_array);
559 pen->compound_array = tmp;
560 memcpy(pen->compound_array, compoundarray, count * sizeof(REAL));
561 pen->compound_array_size = count;
562 return Ok;
565 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
567 GpCustomLineCap * cap;
568 GpStatus ret;
570 TRACE("(%p, %p)\n", pen, customCap);
572 /* native crashes on pen == NULL, customCap != NULL */
573 if(!customCap) return InvalidParameter;
575 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
576 GdipDeleteCustomLineCap(pen->customend);
577 pen->endcap = LineCapCustom;
578 pen->customend = cap;
581 return ret;
584 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
586 GpCustomLineCap * cap;
587 GpStatus ret;
589 TRACE("(%p, %p)\n", pen, customCap);
591 /* native crashes on pen == NULL, customCap != NULL */
592 if(!customCap) return InvalidParameter;
594 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
595 GdipDeleteCustomLineCap(pen->customstart);
596 pen->startcap = LineCapCustom;
597 pen->customstart = cap;
600 return ret;
603 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
604 INT count)
606 INT i;
607 REAL sum = 0;
609 TRACE("(%p, %p, %d)\n", pen, dash, count);
611 if(!pen || !dash)
612 return InvalidParameter;
614 if(count <= 0)
615 return OutOfMemory;
617 for(i = 0; i < count; i++){
618 sum += dash[i];
619 if(dash[i] <= 0.0)
620 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;