push 337eb2e2d902d84a5d689451984c5832d7e04fc4
[wine/hacks.git] / dlls / gdiplus / pen.c
blobf41185a68876d3f71170ec7c36500ac6a5557043
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 TRACE("(%p, %p)\n", pen, clonepen);
92 if(!pen || !clonepen)
93 return InvalidParameter;
95 *clonepen = GdipAlloc(sizeof(GpPen));
96 if(!*clonepen) return OutOfMemory;
98 **clonepen = *pen;
100 GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
101 GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
102 GdipCloneBrush(pen->brush, &(*clonepen)->brush);
104 return Ok;
107 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
108 GpPen **pen)
110 GpBrush *brush;
111 GpStatus status;
113 TRACE("(%x, %.2f, %d, %p)\n", color, width, unit, pen);
115 GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
116 status = GdipCreatePen2(brush, width, unit, pen);
117 GdipDeleteBrush(brush);
118 return status;
121 GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
122 GpPen **pen)
124 GpPen *gp_pen;
125 GpBrush *clone_brush;
127 TRACE("(%p, %.2f, %d, %p)\n", brush, width, unit, pen);
129 if(!pen || !brush)
130 return InvalidParameter;
132 gp_pen = GdipAlloc(sizeof(GpPen));
133 if(!gp_pen) return OutOfMemory;
135 gp_pen->style = GP_DEFAULT_PENSTYLE;
136 gp_pen->width = width;
137 gp_pen->unit = unit;
138 gp_pen->endcap = LineCapFlat;
139 gp_pen->join = LineJoinMiter;
140 gp_pen->miterlimit = 10.0;
141 gp_pen->dash = DashStyleSolid;
142 gp_pen->offset = 0.0;
143 gp_pen->customstart = NULL;
144 gp_pen->customend = NULL;
146 if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
147 FIXME("UnitWorld, UnitPixel only supported units\n");
148 GdipFree(gp_pen);
149 return NotImplemented;
152 GdipCloneBrush(brush, &clone_brush);
153 gp_pen->brush = clone_brush;
155 *pen = gp_pen;
157 return Ok;
160 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
162 TRACE("(%p)\n", pen);
164 if(!pen) return InvalidParameter;
166 GdipDeleteBrush(pen->brush);
167 GdipDeleteCustomLineCap(pen->customstart);
168 GdipDeleteCustomLineCap(pen->customend);
169 GdipFree(pen->dashes);
170 GdipFree(pen);
172 return Ok;
175 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
177 TRACE("(%p, %p)\n", pen, brush);
179 if(!pen || !brush)
180 return InvalidParameter;
182 return GdipCloneBrush(pen->brush, brush);
185 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
187 TRACE("(%p, %p)\n", pen, argb);
189 if(!pen || !argb)
190 return InvalidParameter;
192 if(pen->brush->bt != BrushTypeSolidColor)
193 return NotImplemented;
195 return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
198 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
200 TRACE("(%p, %p)\n", pen, customCap);
202 if(!pen || !customCap)
203 return InvalidParameter;
205 if(!pen->customend){
206 *customCap = NULL;
207 return Ok;
210 return GdipCloneCustomLineCap(pen->customend, customCap);
213 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
215 TRACE("(%p, %p)\n", pen, customCap);
217 if(!pen || !customCap)
218 return InvalidParameter;
220 if(!pen->customstart){
221 *customCap = NULL;
222 return Ok;
225 return GdipCloneCustomLineCap(pen->customstart, customCap);
228 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
230 TRACE("(%p, %p, %d)\n", pen, dash, count);
232 if(!pen || !dash || count > pen->numdashes)
233 return InvalidParameter;
235 /* note: if you pass a negative value for count, it crashes native gdiplus. */
236 if(count < 0)
237 return GenericError;
239 memcpy(dash, pen->dashes, count * sizeof(REAL));
241 return Ok;
244 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
246 TRACE("(%p, %p)\n", pen, dashCap);
248 if(!pen || !dashCap)
249 return InvalidParameter;
251 *dashCap = pen->dashcap;
253 return Ok;
256 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
258 TRACE("(%p, %p)\n", pen, count);
260 if(!pen || !count)
261 return InvalidParameter;
263 *count = pen->numdashes;
265 return Ok;
268 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
270 TRACE("(%p, %p)\n", pen, offset);
272 if(!pen || !offset)
273 return InvalidParameter;
275 *offset = pen->offset;
277 return Ok;
280 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
282 TRACE("(%p, %p)\n", pen, dash);
284 if(!pen || !dash)
285 return InvalidParameter;
287 *dash = pen->dash;
289 return Ok;
292 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
294 TRACE("(%p, %p)\n", pen, endCap);
296 if(!pen || !endCap)
297 return InvalidParameter;
299 *endCap = pen->endcap;
301 return Ok;
304 GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
306 TRACE("(%p, %p)\n", pen, type);
308 if(!pen || !type)
309 return InvalidParameter;
311 *type = bt_to_pt(pen->brush->bt);
313 return Ok;
316 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
318 TRACE("(%p, %p)\n", pen, lineJoin);
320 if(!pen || !lineJoin)
321 return InvalidParameter;
323 *lineJoin = pen->join;
325 return Ok;
328 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
330 TRACE("(%p, %p)\n", pen, mode);
332 if(!pen || !mode)
333 return InvalidParameter;
335 *mode = pen->align;
337 return Ok;
340 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
342 TRACE("(%p, %p)\n", pen, miterLimit);
344 if(!pen || !miterLimit)
345 return InvalidParameter;
347 *miterLimit = pen->miterlimit;
349 return Ok;
352 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
354 TRACE("(%p, %p)\n", pen, startCap);
356 if(!pen || !startCap)
357 return InvalidParameter;
359 *startCap = pen->startcap;
361 return Ok;
364 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
366 TRACE("(%p, %p)\n", pen, unit);
368 if(!pen || !unit)
369 return InvalidParameter;
371 *unit = pen->unit;
373 return Ok;
376 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
378 TRACE("(%p, %p)\n", pen, width);
380 if(!pen || !width)
381 return InvalidParameter;
383 *width = pen->width;
385 return Ok;
388 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
390 TRACE("(%p, %p)\n", pen, brush);
392 if(!pen || !brush)
393 return InvalidParameter;
395 GdipDeleteBrush(pen->brush);
396 return GdipCloneBrush(brush, &pen->brush);
399 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
401 TRACE("(%p, %x)\n", pen, argb);
403 if(!pen)
404 return InvalidParameter;
406 if(pen->brush->bt != BrushTypeSolidColor)
407 return NotImplemented;
409 return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
412 GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *dash,
413 INT count)
415 FIXME("(%p, %p, %i): stub", pen, dash, count);
417 if (!pen || !dash || count < 2 || count%2 == 1)
418 return InvalidParameter;
420 return NotImplemented;
423 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
425 GpCustomLineCap * cap;
426 GpStatus ret;
428 TRACE("(%p, %p)\n", pen, customCap);
430 /* native crashes on pen == NULL, customCap != NULL */
431 if(!customCap) return InvalidParameter;
433 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
434 GdipDeleteCustomLineCap(pen->customend);
435 pen->endcap = LineCapCustom;
436 pen->customend = cap;
439 return ret;
442 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
444 GpCustomLineCap * cap;
445 GpStatus ret;
447 TRACE("(%p, %p)\n", pen, customCap);
449 /* native crashes on pen == NULL, customCap != NULL */
450 if(!customCap) return InvalidParameter;
452 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
453 GdipDeleteCustomLineCap(pen->customstart);
454 pen->startcap = LineCapCustom;
455 pen->customstart = cap;
458 return ret;
461 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
462 INT count)
464 INT i;
465 REAL sum = 0;
467 TRACE("(%p, %p, %d)\n", pen, dash, count);
469 if(!pen || !dash)
470 return InvalidParameter;
472 if(count <= 0)
473 return OutOfMemory;
475 for(i = 0; i < count; i++){
476 sum += dash[i];
477 if(dash[i] < 0.0)
478 return InvalidParameter;
481 if(sum == 0.0 && count)
482 return InvalidParameter;
484 GdipFree(pen->dashes);
485 pen->dashes = NULL;
487 if(count > 0)
488 pen->dashes = GdipAlloc(count * sizeof(REAL));
489 if(!pen->dashes){
490 pen->numdashes = 0;
491 return OutOfMemory;
494 GdipSetPenDashStyle(pen, DashStyleCustom);
495 memcpy(pen->dashes, dash, count * sizeof(REAL));
496 pen->numdashes = count;
498 return Ok;
501 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
503 TRACE("(%p, %d)\n", pen, dashCap);
505 if(!pen)
506 return InvalidParameter;
508 pen->dashcap = dashCap;
510 return Ok;
513 /* FIXME: dash offset not used */
514 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
516 TRACE("(%p, %.2f)\n", pen, offset);
518 if(!pen)
519 return InvalidParameter;
521 pen->offset = offset;
523 return Ok;
526 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
528 TRACE("(%p, %d)\n", pen, dash);
530 if(!pen)
531 return InvalidParameter;
533 if(dash != DashStyleCustom){
534 GdipFree(pen->dashes);
535 pen->dashes = NULL;
536 pen->numdashes = 0;
539 pen->dash = dash;
540 pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
541 PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
542 pen->style |= gdip_to_gdi_dash(dash);
544 return Ok;
547 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
549 TRACE("(%p, %d)\n", pen, cap);
551 if(!pen) return InvalidParameter;
553 /* The old custom cap gets deleted even if the new style is LineCapCustom. */
554 GdipDeleteCustomLineCap(pen->customend);
555 pen->customend = NULL;
556 pen->endcap = cap;
558 return Ok;
561 /* FIXME: startcap, dashcap not used. */
562 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
563 GpLineCap end, GpDashCap dash)
565 TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
567 if(!pen)
568 return InvalidParameter;
570 GdipDeleteCustomLineCap(pen->customend);
571 GdipDeleteCustomLineCap(pen->customstart);
572 pen->customend = NULL;
573 pen->customstart = NULL;
575 pen->startcap = start;
576 pen->endcap = end;
577 pen->dashcap = dash;
579 return Ok;
582 /* FIXME: Miter line joins behave a bit differently than they do in windows.
583 * Both kinds of miter joins clip if the angle is less than 11 degrees. */
584 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
586 TRACE("(%p, %d)\n", pen, join);
588 if(!pen) return InvalidParameter;
590 pen->join = join;
591 pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
592 pen->style |= gdip_to_gdi_join(join);
594 return Ok;
597 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
599 TRACE("(%p, %.2f)\n", pen, limit);
601 if(!pen)
602 return InvalidParameter;
604 pen->miterlimit = limit;
606 return Ok;
609 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
611 TRACE("(%p, %d)\n", pen, cap);
613 if(!pen) return InvalidParameter;
615 GdipDeleteCustomLineCap(pen->customstart);
616 pen->customstart = NULL;
617 pen->startcap = cap;
619 return Ok;
622 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
624 TRACE("(%p, %.2f)\n", pen, width);
626 if(!pen) return InvalidParameter;
628 pen->width = width;
630 return Ok;
633 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
635 TRACE("(%p, %d)\n", pen, mode);
637 if(!pen) return InvalidParameter;
639 pen->align = mode;
641 return Ok;