wined3d: Use a separate STATE_VDECL state handler in the GLSL pipeline.
[wine/multimedia.git] / dlls / gdiplus / pen.c
blob9b63db981aa1349e1d3013be5d898a6444d35a4b
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 = GdipAlloc(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 = GdipAlloc(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 = GdipAlloc(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;
175 if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
176 FIXME("UnitWorld, UnitPixel only supported units\n");
177 GdipFree(gp_pen);
178 return NotImplemented;
181 GdipCloneBrush(brush, &clone_brush);
182 gp_pen->brush = clone_brush;
184 *pen = gp_pen;
186 TRACE("<-- %p\n", *pen);
188 return Ok;
191 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
193 TRACE("(%p)\n", pen);
195 if(!pen) return InvalidParameter;
197 GdipDeleteBrush(pen->brush);
198 GdipDeleteCustomLineCap(pen->customstart);
199 GdipDeleteCustomLineCap(pen->customend);
200 GdipFree(pen->dashes);
201 GdipFree(pen);
203 return Ok;
206 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
208 TRACE("(%p, %p)\n", pen, brush);
210 if(!pen || !brush)
211 return InvalidParameter;
213 return GdipCloneBrush(pen->brush, brush);
216 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
218 TRACE("(%p, %p)\n", pen, argb);
220 if(!pen || !argb)
221 return InvalidParameter;
223 if(pen->brush->bt != BrushTypeSolidColor)
224 return NotImplemented;
226 return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
229 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
231 TRACE("(%p, %p)\n", pen, customCap);
233 if(!pen || !customCap)
234 return InvalidParameter;
236 if(!pen->customend){
237 *customCap = NULL;
238 return Ok;
241 return GdipCloneCustomLineCap(pen->customend, customCap);
244 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
246 TRACE("(%p, %p)\n", pen, customCap);
248 if(!pen || !customCap)
249 return InvalidParameter;
251 if(!pen->customstart){
252 *customCap = NULL;
253 return Ok;
256 return GdipCloneCustomLineCap(pen->customstart, customCap);
259 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
261 TRACE("(%p, %p, %d)\n", pen, dash, count);
263 if(!pen || !dash || count > pen->numdashes)
264 return InvalidParameter;
266 /* note: if you pass a negative value for count, it crashes native gdiplus. */
267 if(count < 0)
268 return GenericError;
270 memcpy(dash, pen->dashes, count * sizeof(REAL));
272 return Ok;
275 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
277 TRACE("(%p, %p)\n", pen, dashCap);
279 if(!pen || !dashCap)
280 return InvalidParameter;
282 *dashCap = pen->dashcap;
284 return Ok;
287 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
289 TRACE("(%p, %p)\n", pen, count);
291 if(!pen || !count)
292 return InvalidParameter;
294 *count = pen->numdashes;
296 return Ok;
299 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
301 TRACE("(%p, %p)\n", pen, offset);
303 if(!pen || !offset)
304 return InvalidParameter;
306 *offset = pen->offset;
308 return Ok;
311 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
313 TRACE("(%p, %p)\n", pen, dash);
315 if(!pen || !dash)
316 return InvalidParameter;
318 *dash = pen->dash;
320 return Ok;
323 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
325 TRACE("(%p, %p)\n", pen, endCap);
327 if(!pen || !endCap)
328 return InvalidParameter;
330 *endCap = pen->endcap;
332 return Ok;
335 GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
337 TRACE("(%p, %p)\n", pen, type);
339 if(!pen || !type)
340 return InvalidParameter;
342 *type = bt_to_pt(pen->brush->bt);
344 return Ok;
347 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
349 TRACE("(%p, %p)\n", pen, lineJoin);
351 if(!pen || !lineJoin)
352 return InvalidParameter;
354 *lineJoin = pen->join;
356 return Ok;
359 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
361 TRACE("(%p, %p)\n", pen, mode);
363 if(!pen || !mode)
364 return InvalidParameter;
366 *mode = pen->align;
368 return Ok;
371 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
373 TRACE("(%p, %p)\n", pen, miterLimit);
375 if(!pen || !miterLimit)
376 return InvalidParameter;
378 *miterLimit = pen->miterlimit;
380 return Ok;
383 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
385 TRACE("(%p, %p)\n", pen, startCap);
387 if(!pen || !startCap)
388 return InvalidParameter;
390 *startCap = pen->startcap;
392 return Ok;
395 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
397 TRACE("(%p, %p)\n", pen, unit);
399 if(!pen || !unit)
400 return InvalidParameter;
402 *unit = pen->unit;
404 return Ok;
407 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
409 TRACE("(%p, %p)\n", pen, width);
411 if(!pen || !width)
412 return InvalidParameter;
414 *width = pen->width;
416 return Ok;
419 GpStatus WINGDIPAPI GdipResetPenTransform(GpPen *pen)
421 static int calls;
423 TRACE("(%p)\n", pen);
425 if(!pen)
426 return InvalidParameter;
428 if(!(calls++))
429 FIXME("(%p) stub\n", pen);
431 return NotImplemented;
434 GpStatus WINGDIPAPI GdipSetPenTransform(GpPen *pen, GpMatrix *matrix)
436 static int calls;
438 TRACE("(%p,%p)\n", pen, matrix);
440 if(!pen || !matrix)
441 return InvalidParameter;
443 if(!(calls++))
444 FIXME("not implemented\n");
446 return NotImplemented;
449 GpStatus WINGDIPAPI GdipGetPenTransform(GpPen *pen, GpMatrix *matrix)
451 static int calls;
453 TRACE("(%p,%p)\n", pen, matrix);
455 if(!pen || !matrix)
456 return InvalidParameter;
458 if(!(calls++))
459 FIXME("not implemented\n");
461 return NotImplemented;
464 GpStatus WINGDIPAPI GdipTranslatePenTransform(GpPen *pen, REAL dx, REAL dy, GpMatrixOrder order)
466 static int calls;
468 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, dx, dy, order);
470 if(!pen)
471 return InvalidParameter;
473 if(!(calls++))
474 FIXME("not implemented\n");
476 return NotImplemented;
479 GpStatus WINGDIPAPI GdipScalePenTransform(GpPen *pen, REAL sx, REAL sy, GpMatrixOrder order)
481 static int calls;
483 TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, sx, sy, order);
485 if(!pen)
486 return InvalidParameter;
488 if(!(calls++))
489 FIXME("(%p, %.2f, %.2f, %d) stub\n", pen, sx, sy, order);
491 return NotImplemented;
494 GpStatus WINGDIPAPI GdipRotatePenTransform(GpPen *pen, REAL angle, GpMatrixOrder order)
496 static int calls;
498 TRACE("(%p,%0.2f,%u)\n", pen, angle, order);
500 if(!pen)
501 return InvalidParameter;
503 if(!(calls++))
504 FIXME("not implemented\n");
506 return NotImplemented;
509 GpStatus WINGDIPAPI GdipMultiplyPenTransform(GpPen *pen, GDIPCONST GpMatrix *matrix,
510 GpMatrixOrder order)
512 static int calls;
514 TRACE("(%p,%p,%u)\n", pen, matrix, order);
516 if(!pen)
517 return InvalidParameter;
519 if(!(calls++))
520 FIXME("not implemented\n");
522 return NotImplemented;
525 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
527 TRACE("(%p, %p)\n", pen, brush);
529 if(!pen || !brush)
530 return InvalidParameter;
532 GdipDeleteBrush(pen->brush);
533 return GdipCloneBrush(brush, &pen->brush);
536 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
538 TRACE("(%p, %x)\n", pen, argb);
540 if(!pen)
541 return InvalidParameter;
543 if(pen->brush->bt != BrushTypeSolidColor)
544 return NotImplemented;
546 return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
549 GpStatus WINGDIPAPI GdipGetPenCompoundCount(GpPen *pen, INT *count)
551 FIXME("(%p, %p): stub\n", pen, count);
553 if (!pen || !count)
554 return InvalidParameter;
556 return NotImplemented;
559 GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *dash,
560 INT count)
562 FIXME("(%p, %p, %i): stub\n", pen, dash, count);
564 if (!pen || !dash || count < 2 || count%2 == 1)
565 return InvalidParameter;
567 return NotImplemented;
570 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
572 GpCustomLineCap * cap;
573 GpStatus ret;
575 TRACE("(%p, %p)\n", pen, customCap);
577 /* native crashes on pen == NULL, customCap != NULL */
578 if(!customCap) return InvalidParameter;
580 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
581 GdipDeleteCustomLineCap(pen->customend);
582 pen->endcap = LineCapCustom;
583 pen->customend = cap;
586 return ret;
589 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
591 GpCustomLineCap * cap;
592 GpStatus ret;
594 TRACE("(%p, %p)\n", pen, customCap);
596 /* native crashes on pen == NULL, customCap != NULL */
597 if(!customCap) return InvalidParameter;
599 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
600 GdipDeleteCustomLineCap(pen->customstart);
601 pen->startcap = LineCapCustom;
602 pen->customstart = cap;
605 return ret;
608 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
609 INT count)
611 INT i;
612 REAL sum = 0;
614 TRACE("(%p, %p, %d)\n", pen, dash, count);
616 if(!pen || !dash)
617 return InvalidParameter;
619 if(count <= 0)
620 return OutOfMemory;
622 for(i = 0; i < count; i++){
623 sum += dash[i];
624 if(dash[i] < 0.0)
625 return InvalidParameter;
628 if(sum == 0.0 && count)
629 return InvalidParameter;
631 GdipFree(pen->dashes);
632 pen->dashes = NULL;
634 if(count > 0)
635 pen->dashes = GdipAlloc(count * sizeof(REAL));
636 if(!pen->dashes){
637 pen->numdashes = 0;
638 return OutOfMemory;
641 GdipSetPenDashStyle(pen, DashStyleCustom);
642 memcpy(pen->dashes, dash, count * sizeof(REAL));
643 pen->numdashes = count;
645 return Ok;
648 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
650 TRACE("(%p, %d)\n", pen, dashCap);
652 if(!pen)
653 return InvalidParameter;
655 pen->dashcap = dashCap;
657 return Ok;
660 /* FIXME: dash offset not used */
661 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
663 TRACE("(%p, %.2f)\n", pen, offset);
665 if(!pen)
666 return InvalidParameter;
668 pen->offset = offset;
670 return Ok;
673 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
675 TRACE("(%p, %d)\n", pen, dash);
677 if(!pen)
678 return InvalidParameter;
680 if(dash != DashStyleCustom){
681 GdipFree(pen->dashes);
682 pen->dashes = NULL;
683 pen->numdashes = 0;
686 pen->dash = dash;
687 pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
688 PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
689 pen->style |= gdip_to_gdi_dash(dash);
691 return Ok;
694 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
696 TRACE("(%p, %d)\n", pen, cap);
698 if(!pen) return InvalidParameter;
700 /* The old custom cap gets deleted even if the new style is LineCapCustom. */
701 GdipDeleteCustomLineCap(pen->customend);
702 pen->customend = NULL;
703 pen->endcap = cap;
705 return Ok;
708 /* FIXME: startcap, dashcap not used. */
709 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
710 GpLineCap end, GpDashCap dash)
712 TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
714 if(!pen)
715 return InvalidParameter;
717 GdipDeleteCustomLineCap(pen->customend);
718 GdipDeleteCustomLineCap(pen->customstart);
719 pen->customend = NULL;
720 pen->customstart = NULL;
722 pen->startcap = start;
723 pen->endcap = end;
724 pen->dashcap = dash;
726 return Ok;
729 /* FIXME: Miter line joins behave a bit differently than they do in windows.
730 * Both kinds of miter joins clip if the angle is less than 11 degrees. */
731 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
733 TRACE("(%p, %d)\n", pen, join);
735 if(!pen) return InvalidParameter;
737 pen->join = join;
738 pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
739 pen->style |= gdip_to_gdi_join(join);
741 return Ok;
744 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
746 TRACE("(%p, %.2f)\n", pen, limit);
748 if(!pen)
749 return InvalidParameter;
751 pen->miterlimit = limit;
753 return Ok;
756 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
758 TRACE("(%p, %d)\n", pen, cap);
760 if(!pen) return InvalidParameter;
762 GdipDeleteCustomLineCap(pen->customstart);
763 pen->customstart = NULL;
764 pen->startcap = cap;
766 return Ok;
769 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
771 TRACE("(%p, %.2f)\n", pen, width);
773 if(!pen) return InvalidParameter;
775 pen->width = width;
777 return Ok;
780 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
782 TRACE("(%p, %d)\n", pen, mode);
784 if(!pen) return InvalidParameter;
786 pen->align = mode;
788 return Ok;