makefiles: Always use the global SOURCES variable for .l files.
[wine.git] / dlls / gdiplus / pen.c
blobfac31d6e84ef339c27678d1e4703107da38f05d6
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;
106 (*clonepen)->compound_array = NULL;
108 stat = GdipCloneBrush(pen->brush, &(*clonepen)->brush);
110 if (stat == Ok && pen->customstart)
111 stat = GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
113 if (stat == Ok && pen->customend)
114 stat = GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
116 if (stat == Ok && pen->dashes)
118 (*clonepen)->dashes = heap_alloc_zero(pen->numdashes * sizeof(REAL));
119 if ((*clonepen)->dashes)
120 memcpy((*clonepen)->dashes, pen->dashes, pen->numdashes * sizeof(REAL));
121 else
122 stat = OutOfMemory;
125 if (stat == Ok && pen->compound_array)
127 (*clonepen)->compound_array = heap_alloc_zero(pen->compound_array_size * sizeof(REAL));
128 if ((*clonepen)->compound_array)
129 memcpy((*clonepen)->compound_array, pen->compound_array, pen->compound_array_size * sizeof(REAL));
130 else
131 stat = OutOfMemory;
134 if (stat != Ok)
136 GdipDeletePen(*clonepen);
137 *clonepen = NULL;
138 return stat;
141 TRACE("<-- %p\n", *clonepen);
143 return Ok;
146 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
147 GpPen **pen)
149 GpBrush *brush;
150 GpStatus status;
152 TRACE("(%lx, %.2f, %d, %p)\n", color, width, unit, pen);
154 GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
155 status = GdipCreatePen2(brush, width, unit, pen);
156 GdipDeleteBrush(brush);
157 return status;
160 GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
161 GpPen **pen)
163 GpPen *gp_pen;
164 GpBrush *clone_brush;
166 TRACE("(%p, %.2f, %d, %p)\n", brush, width, unit, pen);
168 if(!pen || !brush)
169 return InvalidParameter;
171 gp_pen = heap_alloc_zero(sizeof(GpPen));
172 if(!gp_pen) return OutOfMemory;
174 gp_pen->style = GP_DEFAULT_PENSTYLE;
175 gp_pen->width = width;
176 gp_pen->unit = unit;
177 gp_pen->endcap = LineCapFlat;
178 gp_pen->join = LineJoinMiter;
179 gp_pen->miterlimit = 10.0;
180 gp_pen->dash = DashStyleSolid;
181 gp_pen->offset = 0.0;
182 gp_pen->customstart = NULL;
183 gp_pen->customend = NULL;
184 gp_pen->compound_array = NULL;
185 gp_pen->compound_array_size = 0;
186 GdipSetMatrixElements(&gp_pen->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
188 if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
189 FIXME("UnitWorld, UnitPixel only supported units\n");
190 heap_free(gp_pen);
191 return NotImplemented;
194 GdipCloneBrush(brush, &clone_brush);
195 gp_pen->brush = clone_brush;
197 *pen = gp_pen;
199 TRACE("<-- %p\n", *pen);
201 return Ok;
204 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
206 TRACE("(%p)\n", pen);
208 if(!pen) return InvalidParameter;
210 GdipDeleteBrush(pen->brush);
211 GdipDeleteCustomLineCap(pen->customstart);
212 GdipDeleteCustomLineCap(pen->customend);
213 heap_free(pen->compound_array);
214 heap_free(pen->dashes);
215 heap_free(pen);
217 return Ok;
220 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
222 TRACE("(%p, %p)\n", pen, brush);
224 if(!pen || !brush)
225 return InvalidParameter;
227 return GdipCloneBrush(pen->brush, brush);
230 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
232 TRACE("(%p, %p)\n", pen, argb);
234 if(!pen || !argb)
235 return InvalidParameter;
237 if(pen->brush->bt != BrushTypeSolidColor)
238 return NotImplemented;
240 return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
243 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
245 TRACE("(%p, %p)\n", pen, customCap);
247 if(!pen || !customCap)
248 return InvalidParameter;
250 if(!pen->customend){
251 *customCap = NULL;
252 return Ok;
255 return GdipCloneCustomLineCap(pen->customend, customCap);
258 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
260 TRACE("(%p, %p)\n", pen, customCap);
262 if(!pen || !customCap)
263 return InvalidParameter;
265 if(!pen->customstart){
266 *customCap = NULL;
267 return Ok;
270 return GdipCloneCustomLineCap(pen->customstart, customCap);
273 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
275 TRACE("(%p, %p, %d)\n", pen, dash, count);
277 if(!pen || !dash || count > pen->numdashes)
278 return InvalidParameter;
280 /* note: if you pass a negative value for count, it crashes native gdiplus. */
281 if(count < 0)
282 return GenericError;
284 memcpy(dash, pen->dashes, count * sizeof(REAL));
286 return Ok;
289 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
291 TRACE("(%p, %p)\n", pen, dashCap);
293 if(!pen || !dashCap)
294 return InvalidParameter;
296 *dashCap = pen->dashcap;
298 return Ok;
301 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
303 TRACE("(%p, %p)\n", pen, count);
305 if(!pen || !count)
306 return InvalidParameter;
308 *count = pen->numdashes;
310 return Ok;
313 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
315 TRACE("(%p, %p)\n", pen, offset);
317 if(!pen || !offset)
318 return InvalidParameter;
320 *offset = pen->offset;
322 return Ok;
325 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
327 TRACE("(%p, %p)\n", pen, dash);
329 if(!pen || !dash)
330 return InvalidParameter;
332 *dash = pen->dash;
334 return Ok;
337 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
339 TRACE("(%p, %p)\n", pen, endCap);
341 if(!pen || !endCap)
342 return InvalidParameter;
344 *endCap = pen->endcap;
346 return Ok;
349 GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
351 TRACE("(%p, %p)\n", pen, type);
353 if(!pen || !type)
354 return InvalidParameter;
356 *type = bt_to_pt(pen->brush->bt);
358 return Ok;
361 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
363 TRACE("(%p, %p)\n", pen, lineJoin);
365 if(!pen || !lineJoin)
366 return InvalidParameter;
368 *lineJoin = pen->join;
370 return Ok;
373 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
375 TRACE("(%p, %p)\n", pen, mode);
377 if(!pen || !mode)
378 return InvalidParameter;
380 *mode = pen->align;
382 return Ok;
385 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
387 TRACE("(%p, %p)\n", pen, miterLimit);
389 if(!pen || !miterLimit)
390 return InvalidParameter;
392 *miterLimit = pen->miterlimit;
394 return Ok;
397 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
399 TRACE("(%p, %p)\n", pen, startCap);
401 if(!pen || !startCap)
402 return InvalidParameter;
404 *startCap = pen->startcap;
406 return Ok;
409 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
411 TRACE("(%p, %p)\n", pen, unit);
413 if(!pen || !unit)
414 return InvalidParameter;
416 *unit = pen->unit;
418 return Ok;
421 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
423 TRACE("(%p, %p)\n", pen, width);
425 if(!pen || !width)
426 return InvalidParameter;
428 *width = pen->width;
430 return Ok;
433 GpStatus WINGDIPAPI GdipResetPenTransform(GpPen *pen)
435 TRACE("(%p)\n", pen);
437 if(!pen)
438 return InvalidParameter;
440 GdipSetMatrixElements(&pen->transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
442 return Ok;
445 GpStatus WINGDIPAPI GdipSetPenTransform(GpPen *pen, GpMatrix *matrix)
447 static int calls;
449 TRACE("(%p,%s)\n", pen, debugstr_matrix(matrix));
451 if(!pen || !matrix)
452 return InvalidParameter;
454 if(!(calls++))
455 FIXME("(%p,%p) Semi-stub\n", pen, matrix);
457 pen->transform = *matrix;
459 return Ok;
462 GpStatus WINGDIPAPI GdipGetPenTransform(GpPen *pen, GpMatrix *matrix)
464 TRACE("(%p,%s)\n", pen, debugstr_matrix(matrix));
466 if(!pen || !matrix)
467 return InvalidParameter;
469 *matrix = pen->transform;
471 return Ok;
474 GpStatus WINGDIPAPI GdipTranslatePenTransform(GpPen *pen, REAL dx, REAL dy, GpMatrixOrder order)
476 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, dx, dy, order);
478 if(!pen)
479 return InvalidParameter;
481 return GdipTranslateMatrix(&pen->transform, dx, dy, order);
484 GpStatus WINGDIPAPI GdipScalePenTransform(GpPen *pen, REAL sx, REAL sy, GpMatrixOrder order)
486 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, sx, sy, order);
488 if(!pen)
489 return InvalidParameter;
491 return GdipScaleMatrix(&pen->transform, sx, sy, order);
494 GpStatus WINGDIPAPI GdipRotatePenTransform(GpPen *pen, REAL angle, GpMatrixOrder order)
496 TRACE("(%p,%0.2f,%u)\n", pen, angle, order);
498 if(!pen)
499 return InvalidParameter;
501 return GdipRotateMatrix(&pen->transform, angle, order);
504 GpStatus WINGDIPAPI GdipMultiplyPenTransform(GpPen *pen, GDIPCONST GpMatrix *matrix,
505 GpMatrixOrder order)
507 TRACE("(%p,%s,%u)\n", pen, debugstr_matrix(matrix), order);
509 if(!pen)
510 return InvalidParameter;
512 return GdipMultiplyMatrix(&pen->transform, matrix, order);
515 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
517 TRACE("(%p, %p)\n", pen, brush);
519 if(!pen || !brush)
520 return InvalidParameter;
522 GdipDeleteBrush(pen->brush);
523 return GdipCloneBrush(brush, &pen->brush);
526 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
528 TRACE("(%p, %lx)\n", pen, argb);
530 if(!pen)
531 return InvalidParameter;
533 if(pen->brush->bt != BrushTypeSolidColor)
534 return NotImplemented;
536 return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
539 GpStatus WINGDIPAPI GdipGetPenCompoundArray(GpPen *pen, REAL *compoundarray, INT count)
541 TRACE("(%p, %p, %i)\n", pen, compoundarray, count);
543 if (!pen || !compoundarray || count > pen->compound_array_size)
544 return InvalidParameter;
545 if (pen->compound_array && count > 0)
546 memcpy(compoundarray, pen->compound_array, count * sizeof(REAL));
547 return Ok;
550 GpStatus WINGDIPAPI GdipGetPenCompoundCount(GpPen *pen, INT *count)
552 TRACE("(%p, %p)\n", pen, count);
554 if (!pen || !count)
555 return InvalidParameter;
556 *count = pen->compound_array_size;
557 return Ok;
560 GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *compoundarray,
561 INT count)
563 INT i;
564 REAL *tmp;
565 TRACE("(%p, %p, %i)\n", pen, compoundarray, count);
567 if(!pen || !compoundarray || count < 2 || count%2 == 1 || *compoundarray < 0.0 || *compoundarray > 1.0)
568 return InvalidParameter;
570 for(i = 1; i<count; i++)
572 if((compoundarray[i] < compoundarray[i - 1]) || (compoundarray[i] > 1.0))
573 return InvalidParameter;
576 tmp = heap_alloc_zero(count * sizeof(REAL));
577 if(!tmp)
578 return OutOfMemory;
579 heap_free(pen->compound_array);
580 pen->compound_array = tmp;
581 memcpy(pen->compound_array, compoundarray, count * sizeof(REAL));
582 pen->compound_array_size = count;
583 return Ok;
586 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
588 GpCustomLineCap * cap;
589 GpStatus ret;
591 TRACE("(%p, %p)\n", pen, customCap);
593 /* native crashes on pen == NULL, customCap != NULL */
594 if(!customCap) return InvalidParameter;
596 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
597 GdipDeleteCustomLineCap(pen->customend);
598 pen->endcap = LineCapCustom;
599 pen->customend = cap;
602 return ret;
605 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
607 GpCustomLineCap * cap;
608 GpStatus ret;
610 TRACE("(%p, %p)\n", pen, customCap);
612 /* native crashes on pen == NULL, customCap != NULL */
613 if(!customCap) return InvalidParameter;
615 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
616 GdipDeleteCustomLineCap(pen->customstart);
617 pen->startcap = LineCapCustom;
618 pen->customstart = cap;
621 return ret;
624 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
625 INT count)
627 INT i;
628 REAL sum = 0;
630 TRACE("(%p, %p, %d)\n", pen, dash, count);
632 if(!pen || !dash)
633 return InvalidParameter;
635 if(count <= 0)
636 return OutOfMemory;
638 for(i = 0; i < count; i++){
639 sum += dash[i];
640 if(dash[i] <= 0.0)
641 return InvalidParameter;
644 heap_free(pen->dashes);
645 pen->dashes = NULL;
647 if(count > 0)
648 pen->dashes = heap_alloc_zero(count * sizeof(REAL));
649 if(!pen->dashes){
650 pen->numdashes = 0;
651 return OutOfMemory;
654 GdipSetPenDashStyle(pen, DashStyleCustom);
655 memcpy(pen->dashes, dash, count * sizeof(REAL));
656 pen->numdashes = count;
658 return Ok;
661 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
663 TRACE("(%p, %d)\n", pen, dashCap);
665 if(!pen)
666 return InvalidParameter;
668 pen->dashcap = dashCap;
670 return Ok;
673 /* FIXME: dash offset not used */
674 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
676 TRACE("(%p, %.2f)\n", pen, offset);
678 if(!pen)
679 return InvalidParameter;
681 pen->offset = offset;
683 return Ok;
686 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
688 TRACE("(%p, %d)\n", pen, dash);
690 if(!pen)
691 return InvalidParameter;
693 if(dash != DashStyleCustom){
694 heap_free(pen->dashes);
695 pen->dashes = NULL;
696 pen->numdashes = 0;
699 pen->dash = dash;
700 pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
701 PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
702 pen->style |= gdip_to_gdi_dash(dash);
704 return Ok;
707 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
709 TRACE("(%p, %d)\n", pen, cap);
711 if(!pen) return InvalidParameter;
713 /* The old custom cap gets deleted even if the new style is LineCapCustom. */
714 GdipDeleteCustomLineCap(pen->customend);
715 pen->customend = NULL;
716 pen->endcap = cap;
718 return Ok;
721 /* FIXME: startcap, dashcap not used. */
722 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
723 GpLineCap end, GpDashCap dash)
725 TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
727 if(!pen)
728 return InvalidParameter;
730 GdipDeleteCustomLineCap(pen->customend);
731 GdipDeleteCustomLineCap(pen->customstart);
732 pen->customend = NULL;
733 pen->customstart = NULL;
735 pen->startcap = start;
736 pen->endcap = end;
737 pen->dashcap = dash;
739 return Ok;
742 /* FIXME: Miter line joins behave a bit differently than they do in windows.
743 * Both kinds of miter joins clip if the angle is less than 11 degrees. */
744 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
746 TRACE("(%p, %d)\n", pen, join);
748 if(!pen) return InvalidParameter;
750 pen->join = join;
751 pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
752 pen->style |= gdip_to_gdi_join(join);
754 return Ok;
757 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
759 TRACE("(%p, %.2f)\n", pen, limit);
761 if(!pen)
762 return InvalidParameter;
764 pen->miterlimit = limit;
766 return Ok;
769 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
771 TRACE("(%p, %d)\n", pen, cap);
773 if(!pen) return InvalidParameter;
775 GdipDeleteCustomLineCap(pen->customstart);
776 pen->customstart = NULL;
777 pen->startcap = cap;
779 return Ok;
782 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
784 TRACE("(%p, %.2f)\n", pen, width);
786 if(!pen) return InvalidParameter;
788 pen->width = width;
790 return Ok;
793 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
795 TRACE("(%p, %d)\n", pen, mode);
797 if(!pen) return InvalidParameter;
799 pen->align = mode;
801 return Ok;