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
27 #include "gdiplus_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
32 /* looks-right constants */
33 #define TENSION_CONST (0.3)
34 #define ANCHOR_WIDTH (2.0)
35 #define MAX_ITERS (50)
37 /* Converts angle (in degrees) to x/y coordinates */
38 static void deg2xy(REAL angle
, REAL x_0
, REAL y_0
, REAL
*x
, REAL
*y
)
40 REAL radAngle
, hypotenuse
;
42 radAngle
= deg2rad(angle
);
43 hypotenuse
= 50.0; /* arbitrary */
45 *x
= x_0
+ cos(radAngle
) * hypotenuse
;
46 *y
= y_0
+ sin(radAngle
) * hypotenuse
;
49 /* Converts from gdiplus path point type to gdi path point type. */
50 static BYTE
convert_path_point_type(BYTE type
)
54 switch(type
& PathPointTypePathTypeMask
){
55 case PathPointTypeBezier
:
58 case PathPointTypeLine
:
61 case PathPointTypeStart
:
65 ERR("Bad point type\n");
69 if(type
& PathPointTypeCloseSubpath
)
70 ret
|= PT_CLOSEFIGURE
;
75 static REAL
convert_unit(HDC hdc
, GpUnit unit
)
80 return (REAL
) GetDeviceCaps(hdc
, LOGPIXELSX
);
82 return ((REAL
)GetDeviceCaps(hdc
, LOGPIXELSX
)) / 72.0;
84 return ((REAL
)GetDeviceCaps(hdc
, LOGPIXELSX
)) / 300.0;
86 return ((REAL
)GetDeviceCaps(hdc
, LOGPIXELSX
)) / 25.4;
88 ERR("cannot convert UnitWorld\n");
97 static INT
prepare_dc(GpGraphics
*graphics
, GpPen
*pen
)
101 INT save_state
= SaveDC(graphics
->hdc
);
104 EndPath(graphics
->hdc
);
106 /* Get an estimate for the amount the pen width is affected by the world
107 * transform. (This is similar to what some of the wine drivers do.) */
112 GdipTransformMatrixPoints(graphics
->worldtrans
, pt
, 2);
113 width
= sqrt((pt
[1].X
- pt
[0].X
) * (pt
[1].X
- pt
[0].X
) +
114 (pt
[1].Y
- pt
[0].Y
) * (pt
[1].Y
- pt
[0].Y
)) / sqrt(2.0);
116 width
*= pen
->width
* convert_unit(graphics
->hdc
,
117 pen
->unit
== UnitWorld
? graphics
->unit
: pen
->unit
);
119 gdipen
= ExtCreatePen(pen
->style
, roundr(width
), &pen
->brush
->lb
, 0, NULL
);
120 SelectObject(graphics
->hdc
, gdipen
);
125 static void restore_dc(GpGraphics
*graphics
, INT state
)
127 DeleteObject(SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
)));
128 RestoreDC(graphics
->hdc
, state
);
131 /* This helper applies all the changes that the points listed in ptf need in
132 * order to be drawn on the device context. In the end, this should include at
134 * -scaling by page unit
135 * -applying world transformation
136 * -converting from float to int
137 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
138 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
139 * gdi to draw, and these functions would irreparably mess with line widths.
141 static void transform_and_round_points(GpGraphics
*graphics
, POINT
*pti
,
142 GpPointF
*ptf
, INT count
)
148 unitscale
= convert_unit(graphics
->hdc
, graphics
->unit
);
150 /* apply page scale */
151 if(graphics
->unit
!= UnitDisplay
)
152 unitscale
*= graphics
->scale
;
154 GdipCloneMatrix(graphics
->worldtrans
, &matrix
);
155 GdipScaleMatrix(matrix
, unitscale
, unitscale
, MatrixOrderAppend
);
156 GdipTransformMatrixPoints(matrix
, ptf
, count
);
157 GdipDeleteMatrix(matrix
);
159 for(i
= 0; i
< count
; i
++){
160 pti
[i
].x
= roundr(ptf
[i
].X
);
161 pti
[i
].y
= roundr(ptf
[i
].Y
);
165 /* GdipDrawPie/GdipFillPie helper function */
166 static void draw_pie(GpGraphics
*graphics
, REAL x
, REAL y
, REAL width
,
167 REAL height
, REAL startAngle
, REAL sweepAngle
)
174 ptf
[1].X
= x
+ width
;
175 ptf
[1].Y
= y
+ height
;
177 deg2xy(startAngle
+sweepAngle
, x
+ width
/ 2.0, y
+ width
/ 2.0, &ptf
[2].X
, &ptf
[2].Y
);
178 deg2xy(startAngle
, x
+ width
/ 2.0, y
+ width
/ 2.0, &ptf
[3].X
, &ptf
[3].Y
);
180 transform_and_round_points(graphics
, pti
, ptf
, 4);
182 Pie(graphics
->hdc
, pti
[0].x
, pti
[0].y
, pti
[1].x
, pti
[1].y
, pti
[2].x
,
183 pti
[2].y
, pti
[3].x
, pti
[3].y
);
186 /* GdipDrawCurve helper function.
187 * Calculates Bezier points from cardinal spline points. */
188 static void calc_curve_bezier(CONST GpPointF
*pts
, REAL tension
, REAL
*x1
,
189 REAL
*y1
, REAL
*x2
, REAL
*y2
)
193 /* calculate tangent */
194 xdiff
= pts
[2].X
- pts
[0].X
;
195 ydiff
= pts
[2].Y
- pts
[0].Y
;
197 /* apply tangent to get control points */
198 *x1
= pts
[1].X
- tension
* xdiff
;
199 *y1
= pts
[1].Y
- tension
* ydiff
;
200 *x2
= pts
[1].X
+ tension
* xdiff
;
201 *y2
= pts
[1].Y
+ tension
* ydiff
;
204 /* GdipDrawCurve helper function.
205 * Calculates Bezier points from cardinal spline endpoints. */
206 static void calc_curve_bezier_endp(REAL xend
, REAL yend
, REAL xadj
, REAL yadj
,
207 REAL tension
, REAL
*x
, REAL
*y
)
209 /* tangent at endpoints is the line from the endpoint to the adjacent point */
210 *x
= roundr(tension
* (xadj
- xend
) + xend
);
211 *y
= roundr(tension
* (yadj
- yend
) + yend
);
214 /* Draws the linecap the specified color and size on the hdc. The linecap is in
215 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
216 * should not be called on an hdc that has a path you care about. */
217 static void draw_cap(GpGraphics
*graphics
, COLORREF color
, GpLineCap cap
, REAL size
,
218 const GpCustomLineCap
*custom
, REAL x1
, REAL y1
, REAL x2
, REAL y2
)
220 HGDIOBJ oldbrush
= NULL
, oldpen
= NULL
;
221 GpMatrix
*matrix
= NULL
;
224 PointF ptf
[4], *custptf
= NULL
;
225 POINT pt
[4], *custpt
= NULL
;
227 REAL theta
, dsmall
, dbig
, dx
, dy
= 0.0;
232 if((x1
== x2
) && (y1
== y2
))
235 theta
= gdiplus_atan2(y2
- y1
, x2
- x1
);
237 customstroke
= (cap
== LineCapCustom
) && custom
&& (!custom
->fill
);
239 brush
= CreateSolidBrush(color
);
240 lb
.lbStyle
= BS_SOLID
;
243 pen
= ExtCreatePen(PS_GEOMETRIC
| PS_SOLID
| PS_ENDCAP_FLAT
|
244 PS_JOIN_MITER
, 1, &lb
, 0,
246 oldbrush
= SelectObject(graphics
->hdc
, brush
);
247 oldpen
= SelectObject(graphics
->hdc
, pen
);
254 case LineCapSquareAnchor
:
255 case LineCapDiamondAnchor
:
256 size
= size
* (cap
& LineCapNoAnchor
? ANCHOR_WIDTH
: 1.0) / 2.0;
257 if(cap
== LineCapDiamondAnchor
){
258 dsmall
= cos(theta
+ M_PI_2
) * size
;
259 dbig
= sin(theta
+ M_PI_2
) * size
;
262 dsmall
= cos(theta
+ M_PI_4
) * size
;
263 dbig
= sin(theta
+ M_PI_4
) * size
;
266 ptf
[0].X
= x2
- dsmall
;
267 ptf
[1].X
= x2
+ dbig
;
269 ptf
[0].Y
= y2
- dbig
;
270 ptf
[3].Y
= y2
+ dsmall
;
272 ptf
[1].Y
= y2
- dsmall
;
273 ptf
[2].Y
= y2
+ dbig
;
275 ptf
[3].X
= x2
- dbig
;
276 ptf
[2].X
= x2
+ dsmall
;
278 transform_and_round_points(graphics
, pt
, ptf
, 4);
279 Polygon(graphics
->hdc
, pt
, 4);
282 case LineCapArrowAnchor
:
283 size
= size
* 4.0 / sqrt(3.0);
285 dx
= cos(M_PI
/ 6.0 + theta
) * size
;
286 dy
= sin(M_PI
/ 6.0 + theta
) * size
;
291 dx
= cos(- M_PI
/ 6.0 + theta
) * size
;
292 dy
= sin(- M_PI
/ 6.0 + theta
) * size
;
300 transform_and_round_points(graphics
, pt
, ptf
, 3);
301 Polygon(graphics
->hdc
, pt
, 3);
304 case LineCapRoundAnchor
:
305 dx
= dy
= ANCHOR_WIDTH
* size
/ 2.0;
312 transform_and_round_points(graphics
, pt
, ptf
, 2);
313 Ellipse(graphics
->hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
316 case LineCapTriangle
:
318 dx
= cos(M_PI_2
+ theta
) * size
;
319 dy
= sin(M_PI_2
+ theta
) * size
;
326 dx
= cos(theta
) * size
;
327 dy
= sin(theta
) * size
;
332 transform_and_round_points(graphics
, pt
, ptf
, 3);
333 Polygon(graphics
->hdc
, pt
, 3);
337 dx
= dy
= size
/ 2.0;
344 dx
= -cos(M_PI_2
+ theta
) * size
;
345 dy
= -sin(M_PI_2
+ theta
) * size
;
352 transform_and_round_points(graphics
, pt
, ptf
, 4);
353 Pie(graphics
->hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
, pt
[2].x
,
354 pt
[2].y
, pt
[3].x
, pt
[3].y
);
361 count
= custom
->pathdata
.Count
;
362 custptf
= GdipAlloc(count
* sizeof(PointF
));
363 custpt
= GdipAlloc(count
* sizeof(POINT
));
364 tp
= GdipAlloc(count
);
366 if(!custptf
|| !custpt
|| !tp
|| (GdipCreateMatrix(&matrix
) != Ok
))
369 memcpy(custptf
, custom
->pathdata
.Points
, count
* sizeof(PointF
));
371 GdipScaleMatrix(matrix
, size
, size
, MatrixOrderAppend
);
372 GdipRotateMatrix(matrix
, (180.0 / M_PI
) * (theta
- M_PI_2
),
374 GdipTranslateMatrix(matrix
, x2
, y2
, MatrixOrderAppend
);
375 GdipTransformMatrixPoints(matrix
, custptf
, count
);
377 transform_and_round_points(graphics
, custpt
, custptf
, count
);
379 for(i
= 0; i
< count
; i
++)
380 tp
[i
] = convert_path_point_type(custom
->pathdata
.Types
[i
]);
383 BeginPath(graphics
->hdc
);
384 PolyDraw(graphics
->hdc
, custpt
, tp
, count
);
385 EndPath(graphics
->hdc
);
386 StrokeAndFillPath(graphics
->hdc
);
389 PolyDraw(graphics
->hdc
, custpt
, tp
, count
);
395 GdipDeleteMatrix(matrix
);
402 SelectObject(graphics
->hdc
, oldbrush
);
403 SelectObject(graphics
->hdc
, oldpen
);
409 /* Shortens the line by the given percent by changing x2, y2.
410 * If percent is > 1.0 then the line will change direction.
411 * If percent is negative it can lengthen the line. */
412 static void shorten_line_percent(REAL x1
, REAL y1
, REAL
*x2
, REAL
*y2
, REAL percent
)
414 REAL dist
, theta
, dx
, dy
;
416 if((y1
== *y2
) && (x1
== *x2
))
419 dist
= sqrt((*x2
- x1
) * (*x2
- x1
) + (*y2
- y1
) * (*y2
- y1
)) * -percent
;
420 theta
= gdiplus_atan2((*y2
- y1
), (*x2
- x1
));
421 dx
= cos(theta
) * dist
;
422 dy
= sin(theta
) * dist
;
428 /* Shortens the line by the given amount by changing x2, y2.
429 * If the amount is greater than the distance, the line will become length 0.
430 * If the amount is negative, it can lengthen the line. */
431 static void shorten_line_amt(REAL x1
, REAL y1
, REAL
*x2
, REAL
*y2
, REAL amt
)
433 REAL dx
, dy
, percent
;
437 if(dx
== 0 && dy
== 0)
440 percent
= amt
/ sqrt(dx
* dx
+ dy
* dy
);
447 shorten_line_percent(x1
, y1
, x2
, y2
, percent
);
450 /* Draws lines between the given points, and if caps is true then draws an endcap
451 * at the end of the last line. FIXME: Startcaps not implemented. */
452 static GpStatus
draw_polyline(GpGraphics
*graphics
, GpPen
*pen
,
453 GDIPCONST GpPointF
* pt
, INT count
, BOOL caps
)
456 GpPointF
*ptcopy
= NULL
;
457 GpStatus status
= GenericError
;
462 pti
= GdipAlloc(count
* sizeof(POINT
));
463 ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
466 status
= OutOfMemory
;
470 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
473 if(pen
->endcap
== LineCapArrowAnchor
)
474 shorten_line_amt(ptcopy
[count
-2].X
, ptcopy
[count
-2].Y
,
475 &ptcopy
[count
-1].X
, &ptcopy
[count
-1].Y
, pen
->width
);
476 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
477 shorten_line_amt(ptcopy
[count
-2].X
, ptcopy
[count
-2].Y
,
478 &ptcopy
[count
-1].X
, &ptcopy
[count
-1].Y
,
479 pen
->customend
->inset
* pen
->width
);
481 if(pen
->startcap
== LineCapArrowAnchor
)
482 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
,
483 &ptcopy
[0].X
, &ptcopy
[0].Y
, pen
->width
);
484 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
485 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
,
486 &ptcopy
[0].X
, &ptcopy
[0].Y
,
487 pen
->customend
->inset
* pen
->width
);
489 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
490 pt
[count
- 2].X
, pt
[count
- 2].Y
, pt
[count
- 1].X
, pt
[count
- 1].Y
);
491 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
492 pt
[1].X
, pt
[1].Y
, pt
[0].X
, pt
[0].Y
);\
495 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
497 Polyline(graphics
->hdc
, pti
, count
);
506 /* Conducts a linear search to find the bezier points that will back off
507 * the endpoint of the curve by a distance of amt. Linear search works
508 * better than binary in this case because there are multiple solutions,
509 * and binary searches often find a bad one. I don't think this is what
510 * Windows does but short of rendering the bezier without GDI's help it's
511 * the best we can do. If rev then work from the start of the passed points
512 * instead of the end. */
513 static void shorten_bezier_amt(GpPointF
* pt
, REAL amt
, BOOL rev
)
516 REAL percent
= 0.00, dx
, dy
, origx
, origy
, diff
= -1.0;
517 INT i
, first
= 0, second
= 1, third
= 2, fourth
= 3;
526 origx
= pt
[fourth
].X
;
527 origy
= pt
[fourth
].Y
;
528 memcpy(origpt
, pt
, sizeof(GpPointF
) * 4);
530 for(i
= 0; (i
< MAX_ITERS
) && (diff
< amt
); i
++){
531 /* reset bezier points to original values */
532 memcpy(pt
, origpt
, sizeof(GpPointF
) * 4);
533 /* Perform magic on bezier points. Order is important here.*/
534 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
535 shorten_line_percent(pt
[second
].X
, pt
[second
].Y
, &pt
[third
].X
, &pt
[third
].Y
, percent
);
536 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
537 shorten_line_percent(pt
[first
].X
, pt
[first
].Y
, &pt
[second
].X
, &pt
[second
].Y
, percent
);
538 shorten_line_percent(pt
[second
].X
, pt
[second
].Y
, &pt
[third
].X
, &pt
[third
].Y
, percent
);
539 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
541 dx
= pt
[fourth
].X
- origx
;
542 dy
= pt
[fourth
].Y
- origy
;
544 diff
= sqrt(dx
* dx
+ dy
* dy
);
545 percent
+= 0.0005 * amt
;
549 /* Draws bezier curves between given points, and if caps is true then draws an
550 * endcap at the end of the last line. FIXME: Startcaps not implemented. */
551 static GpStatus
draw_polybezier(GpGraphics
*graphics
, GpPen
*pen
,
552 GDIPCONST GpPointF
* pt
, INT count
, BOOL caps
)
557 GpStatus status
= GenericError
;
562 pti
= GdipAlloc(count
* sizeof(POINT
));
563 ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
566 status
= OutOfMemory
;
570 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
573 if(pen
->endcap
== LineCapArrowAnchor
)
574 shorten_bezier_amt(&ptcopy
[count
-4], pen
->width
, FALSE
);
575 /* FIXME The following is seemingly correct only for baseinset < 0 or
576 * baseinset > ~3. With smaller baseinsets, windows actually
577 * lengthens the bezier line instead of shortening it. */
578 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
){
581 shorten_line_amt(pt
[count
- 2].X
, pt
[count
- 2].Y
, &x
, &y
,
582 pen
->width
* pen
->customend
->inset
);
583 MoveToEx(graphics
->hdc
, roundr(pt
[count
- 1].X
), roundr(pt
[count
- 1].Y
), &curpos
);
584 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
585 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
588 if(pen
->startcap
== LineCapArrowAnchor
)
589 shorten_bezier_amt(ptcopy
, pen
->width
, TRUE
);
590 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
){
593 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
, &x
, &y
,
594 pen
->width
* pen
->customend
->inset
);
595 MoveToEx(graphics
->hdc
, roundr(pt
[0].X
), roundr(pt
[0].Y
), &curpos
);
596 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
597 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
600 /* the direction of the line cap is parallel to the direction at the
601 * end of the bezier (which, if it has been shortened, is not the same
602 * as the direction from pt[count-2] to pt[count-1]) */
603 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
604 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
605 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
606 pt
[count
- 1].X
, pt
[count
- 1].Y
);
608 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
609 pt
[0].X
- (ptcopy
[0].X
- ptcopy
[1].X
),
610 pt
[0].Y
- (ptcopy
[0].Y
- ptcopy
[1].Y
), pt
[0].X
, pt
[0].Y
);
613 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
615 PolyBezier(graphics
->hdc
, pti
, count
);
626 /* Draws a combination of bezier curves and lines between points. */
627 static GpStatus
draw_poly(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST GpPointF
* pt
,
628 GDIPCONST BYTE
* types
, INT count
, BOOL caps
)
630 POINT
*pti
= GdipAlloc(count
* sizeof(POINT
)), curpos
;
631 BYTE
*tp
= GdipAlloc(count
);
632 GpPointF
*ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
633 REAL x
= pt
[count
- 1].X
, y
= pt
[count
- 1].Y
;
635 GpStatus status
= GenericError
;
641 if(!pti
|| !tp
|| !ptcopy
){
642 status
= OutOfMemory
;
646 for(i
= 1; i
< count
; i
++){
647 if((types
[i
] & PathPointTypePathTypeMask
) == PathPointTypeBezier
){
648 if((i
+ 2 >= count
) || !(types
[i
+ 1] & PathPointTypeBezier
)
649 || !(types
[i
+ 1] & PathPointTypeBezier
)){
650 ERR("Bad bezier points\n");
657 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
659 /* If we are drawing caps, go through the points and adjust them accordingly,
660 * and draw the caps. */
662 switch(types
[count
- 1] & PathPointTypePathTypeMask
){
663 case PathPointTypeBezier
:
664 if(pen
->endcap
== LineCapArrowAnchor
)
665 shorten_bezier_amt(&ptcopy
[count
- 4], pen
->width
, FALSE
);
666 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
){
669 shorten_line_amt(pt
[count
- 2].X
, pt
[count
- 2].Y
, &x
, &y
,
670 pen
->width
* pen
->customend
->inset
);
671 MoveToEx(graphics
->hdc
, roundr(pt
[count
- 1].X
),
672 roundr(pt
[count
- 1].Y
), &curpos
);
673 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
674 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
677 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
678 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
679 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
680 pt
[count
- 1].X
, pt
[count
- 1].Y
);
683 case PathPointTypeLine
:
684 if(pen
->endcap
== LineCapArrowAnchor
)
685 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
686 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
688 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
689 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
690 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
691 pen
->customend
->inset
* pen
->width
);
693 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
694 pt
[count
- 2].X
, pt
[count
- 2].Y
, pt
[count
- 1].X
,
699 ERR("Bad path last point\n");
703 /* Find start of points */
704 for(j
= 1; j
< count
&& ((types
[j
] & PathPointTypePathTypeMask
)
705 == PathPointTypeStart
); j
++);
707 switch(types
[j
] & PathPointTypePathTypeMask
){
708 case PathPointTypeBezier
:
709 if(pen
->startcap
== LineCapArrowAnchor
)
710 shorten_bezier_amt(&ptcopy
[j
- 1], pen
->width
, TRUE
);
711 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
){
714 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
, &x
, &y
,
715 pen
->width
* pen
->customstart
->inset
);
716 MoveToEx(graphics
->hdc
, roundr(pt
[j
- 1].X
), roundr(pt
[j
- 1].Y
), &curpos
);
717 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
718 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
721 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
722 pt
[j
- 1].X
- (ptcopy
[j
- 1].X
- ptcopy
[j
].X
),
723 pt
[j
- 1].Y
- (ptcopy
[j
- 1].Y
- ptcopy
[j
].Y
),
724 pt
[j
- 1].X
, pt
[j
- 1].Y
);
727 case PathPointTypeLine
:
728 if(pen
->startcap
== LineCapArrowAnchor
)
729 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
730 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
732 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
733 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
734 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
735 pen
->customstart
->inset
* pen
->width
);
737 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customstart
,
738 pt
[j
].X
, pt
[j
].Y
, pt
[j
- 1].X
,
743 ERR("Bad path points\n");
748 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
750 for(i
= 0; i
< count
; i
++){
751 tp
[i
] = convert_path_point_type(types
[i
]);
754 PolyDraw(graphics
->hdc
, pti
, tp
, count
);
766 GpStatus WINGDIPAPI
GdipCreateFromHDC(HDC hdc
, GpGraphics
**graphics
)
774 return InvalidParameter
;
776 *graphics
= GdipAlloc(sizeof(GpGraphics
));
777 if(!*graphics
) return OutOfMemory
;
779 if((retval
= GdipCreateMatrix(&(*graphics
)->worldtrans
)) != Ok
){
784 (*graphics
)->hdc
= hdc
;
785 (*graphics
)->hwnd
= NULL
;
786 (*graphics
)->smoothing
= SmoothingModeDefault
;
787 (*graphics
)->compqual
= CompositingQualityDefault
;
788 (*graphics
)->interpolation
= InterpolationModeDefault
;
789 (*graphics
)->pixeloffset
= PixelOffsetModeDefault
;
790 (*graphics
)->unit
= UnitDisplay
;
791 (*graphics
)->scale
= 1.0;
796 GpStatus WINGDIPAPI
GdipCreateFromHWND(HWND hwnd
, GpGraphics
**graphics
)
800 if((ret
= GdipCreateFromHDC(GetDC(hwnd
), graphics
)) != Ok
)
803 (*graphics
)->hwnd
= hwnd
;
808 GpStatus WINGDIPAPI
GdipCreateMetafileFromEmf(HENHMETAFILE hemf
, BOOL
delete,
809 GpMetafile
**metafile
)
813 if(!hemf
|| !metafile
)
814 return InvalidParameter
;
817 FIXME("not implemented\n");
819 return NotImplemented
;
822 GpStatus WINGDIPAPI
GdipCreateMetafileFromWmf(HMETAFILE hwmf
, BOOL
delete,
823 GDIPCONST WmfPlaceableFileHeader
* placeable
, GpMetafile
**metafile
)
827 if(!hwmf
|| !metafile
|| !placeable
)
828 return InvalidParameter
;
831 FIXME("not implemented\n");
833 return NotImplemented
;
836 GpStatus WINGDIPAPI
GdipDeleteGraphics(GpGraphics
*graphics
)
838 if(!graphics
) return InvalidParameter
;
840 ReleaseDC(graphics
->hwnd
, graphics
->hdc
);
842 GdipDeleteMatrix(graphics
->worldtrans
);
843 HeapFree(GetProcessHeap(), 0, graphics
);
848 GpStatus WINGDIPAPI
GdipDrawArc(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
849 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
851 INT save_state
, num_pts
;
852 GpPointF points
[MAX_ARC_PTS
];
855 if(!graphics
|| !pen
)
856 return InvalidParameter
;
858 num_pts
= arc2polybezier(points
, x
, y
, width
, height
, startAngle
, sweepAngle
);
860 save_state
= prepare_dc(graphics
, pen
);
862 retval
= draw_polybezier(graphics
, pen
, points
, num_pts
, TRUE
);
864 restore_dc(graphics
, save_state
);
869 GpStatus WINGDIPAPI
GdipDrawBezier(GpGraphics
*graphics
, GpPen
*pen
, REAL x1
,
870 REAL y1
, REAL x2
, REAL y2
, REAL x3
, REAL y3
, REAL x4
, REAL y4
)
876 if(!graphics
|| !pen
)
877 return InvalidParameter
;
888 save_state
= prepare_dc(graphics
, pen
);
890 retval
= draw_polybezier(graphics
, pen
, pt
, 4, TRUE
);
892 restore_dc(graphics
, save_state
);
897 /* Approximates cardinal spline with Bezier curves. */
898 GpStatus WINGDIPAPI
GdipDrawCurve2(GpGraphics
*graphics
, GpPen
*pen
,
899 GDIPCONST GpPointF
*points
, INT count
, REAL tension
)
901 /* PolyBezier expects count*3-2 points. */
902 INT i
, len_pt
= count
*3-2, save_state
;
907 if(!graphics
|| !pen
)
908 return InvalidParameter
;
910 pt
= GdipAlloc(len_pt
* sizeof(GpPointF
));
911 tension
= tension
* TENSION_CONST
;
913 calc_curve_bezier_endp(points
[0].X
, points
[0].Y
, points
[1].X
, points
[1].Y
,
916 pt
[0].X
= points
[0].X
;
917 pt
[0].Y
= points
[0].Y
;
921 for(i
= 0; i
< count
-2; i
++){
922 calc_curve_bezier(&(points
[i
]), tension
, &x1
, &y1
, &x2
, &y2
);
926 pt
[3*i
+3].X
= points
[i
+1].X
;
927 pt
[3*i
+3].Y
= points
[i
+1].Y
;
932 calc_curve_bezier_endp(points
[count
-1].X
, points
[count
-1].Y
,
933 points
[count
-2].X
, points
[count
-2].Y
, tension
, &x1
, &y1
);
937 pt
[len_pt
-1].X
= points
[count
-1].X
;
938 pt
[len_pt
-1].Y
= points
[count
-1].Y
;
940 save_state
= prepare_dc(graphics
, pen
);
942 retval
= draw_polybezier(graphics
, pen
, pt
, len_pt
, TRUE
);
945 restore_dc(graphics
, save_state
);
950 GpStatus WINGDIPAPI
GdipDrawLineI(GpGraphics
*graphics
, GpPen
*pen
, INT x1
,
951 INT y1
, INT x2
, INT y2
)
957 if(!pen
|| !graphics
)
958 return InvalidParameter
;
965 save_state
= prepare_dc(graphics
, pen
);
967 retval
= draw_polyline(graphics
, pen
, pt
, 2, TRUE
);
969 restore_dc(graphics
, save_state
);
974 GpStatus WINGDIPAPI
GdipDrawLines(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST
975 GpPointF
*points
, INT count
)
980 if(!pen
|| !graphics
|| (count
< 2))
981 return InvalidParameter
;
983 save_state
= prepare_dc(graphics
, pen
);
985 retval
= draw_polyline(graphics
, pen
, points
, count
, TRUE
);
987 restore_dc(graphics
, save_state
);
992 GpStatus WINGDIPAPI
GdipDrawPath(GpGraphics
*graphics
, GpPen
*pen
, GpPath
*path
)
997 if(!pen
|| !graphics
)
998 return InvalidParameter
;
1000 save_state
= prepare_dc(graphics
, pen
);
1002 retval
= draw_poly(graphics
, pen
, path
->pathdata
.Points
,
1003 path
->pathdata
.Types
, path
->pathdata
.Count
, TRUE
);
1005 restore_dc(graphics
, save_state
);
1010 GpStatus WINGDIPAPI
GdipDrawPie(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
1011 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
1015 if(!graphics
|| !pen
)
1016 return InvalidParameter
;
1018 save_state
= prepare_dc(graphics
, pen
);
1019 SelectObject(graphics
->hdc
, GetStockObject(NULL_BRUSH
));
1021 draw_pie(graphics
, x
, y
, width
, height
, startAngle
, sweepAngle
);
1023 restore_dc(graphics
, save_state
);
1028 GpStatus WINGDIPAPI
GdipDrawRectangleI(GpGraphics
*graphics
, GpPen
*pen
, INT x
,
1029 INT y
, INT width
, INT height
)
1033 if(!pen
|| !graphics
)
1034 return InvalidParameter
;
1036 save_state
= prepare_dc(graphics
, pen
);
1037 SelectObject(graphics
->hdc
, GetStockObject(NULL_BRUSH
));
1039 Rectangle(graphics
->hdc
, x
, y
, x
+ width
, y
+ height
);
1041 restore_dc(graphics
, save_state
);
1046 GpStatus WINGDIPAPI
GdipFillPath(GpGraphics
*graphics
, GpBrush
*brush
, GpPath
*path
)
1051 if(!brush
|| !graphics
|| !path
)
1052 return InvalidParameter
;
1054 save_state
= SaveDC(graphics
->hdc
);
1055 EndPath(graphics
->hdc
);
1056 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1057 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1060 BeginPath(graphics
->hdc
);
1061 retval
= draw_poly(graphics
, NULL
, path
->pathdata
.Points
,
1062 path
->pathdata
.Types
, path
->pathdata
.Count
, FALSE
);
1067 EndPath(graphics
->hdc
);
1068 FillPath(graphics
->hdc
);
1073 RestoreDC(graphics
->hdc
, save_state
);
1078 GpStatus WINGDIPAPI
GdipFillPie(GpGraphics
*graphics
, GpBrush
*brush
, REAL x
,
1079 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
1083 if(!graphics
|| !brush
)
1084 return InvalidParameter
;
1086 save_state
= SaveDC(graphics
->hdc
);
1087 EndPath(graphics
->hdc
);
1088 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1089 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1091 draw_pie(graphics
, x
, y
, width
, height
, startAngle
, sweepAngle
);
1093 RestoreDC(graphics
->hdc
, save_state
);
1098 GpStatus WINGDIPAPI
GdipFillPolygonI(GpGraphics
*graphics
, GpBrush
*brush
,
1099 GDIPCONST GpPoint
*points
, INT count
, GpFillMode fillMode
)
1102 GpPointF
*ptf
= NULL
;
1104 GpStatus retval
= Ok
;
1106 if(!graphics
|| !brush
|| !points
|| !count
)
1107 return InvalidParameter
;
1109 ptf
= GdipAlloc(count
* sizeof(GpPointF
));
1110 pti
= GdipAlloc(count
* sizeof(POINT
));
1112 retval
= OutOfMemory
;
1116 for(i
= 0; i
< count
; i
++){
1117 ptf
[i
].X
= (REAL
) points
[i
].X
;
1118 ptf
[i
].Y
= (REAL
) points
[i
].Y
;
1121 save_state
= SaveDC(graphics
->hdc
);
1122 EndPath(graphics
->hdc
);
1123 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1124 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1125 SetPolyFillMode(graphics
->hdc
, (fillMode
== FillModeAlternate
? ALTERNATE
1128 transform_and_round_points(graphics
, pti
, ptf
, count
);
1129 Polygon(graphics
->hdc
, pti
, count
);
1131 RestoreDC(graphics
->hdc
, save_state
);
1140 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1141 GpStatus WINGDIPAPI
GdipGetCompositingQuality(GpGraphics
*graphics
,
1142 CompositingQuality
*quality
)
1144 if(!graphics
|| !quality
)
1145 return InvalidParameter
;
1147 *quality
= graphics
->compqual
;
1152 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1153 GpStatus WINGDIPAPI
GdipGetInterpolationMode(GpGraphics
*graphics
,
1154 InterpolationMode
*mode
)
1156 if(!graphics
|| !mode
)
1157 return InvalidParameter
;
1159 *mode
= graphics
->interpolation
;
1164 GpStatus WINGDIPAPI
GdipGetPageScale(GpGraphics
*graphics
, REAL
*scale
)
1166 if(!graphics
|| !scale
)
1167 return InvalidParameter
;
1169 *scale
= graphics
->scale
;
1174 GpStatus WINGDIPAPI
GdipGetPageUnit(GpGraphics
*graphics
, GpUnit
*unit
)
1176 if(!graphics
|| !unit
)
1177 return InvalidParameter
;
1179 *unit
= graphics
->unit
;
1184 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1185 GpStatus WINGDIPAPI
GdipGetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1188 if(!graphics
|| !mode
)
1189 return InvalidParameter
;
1191 *mode
= graphics
->pixeloffset
;
1196 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1197 GpStatus WINGDIPAPI
GdipGetSmoothingMode(GpGraphics
*graphics
, SmoothingMode
*mode
)
1199 if(!graphics
|| !mode
)
1200 return InvalidParameter
;
1202 *mode
= graphics
->smoothing
;
1207 GpStatus WINGDIPAPI
GdipGetWorldTransform(GpGraphics
*graphics
, GpMatrix
*matrix
)
1209 if(!graphics
|| !matrix
)
1210 return InvalidParameter
;
1212 memcpy(matrix
, graphics
->worldtrans
, sizeof(GpMatrix
));
1216 GpStatus WINGDIPAPI
GdipRestoreGraphics(GpGraphics
*graphics
, GraphicsState state
)
1221 return InvalidParameter
;
1224 FIXME("graphics state not implemented\n");
1226 return NotImplemented
;
1229 GpStatus WINGDIPAPI
GdipSaveGraphics(GpGraphics
*graphics
, GraphicsState
*state
)
1233 if(!graphics
|| !state
)
1234 return InvalidParameter
;
1237 FIXME("graphics state not implemented\n");
1239 return NotImplemented
;
1242 GpStatus WINGDIPAPI
GdipSetCompositingQuality(GpGraphics
*graphics
,
1243 CompositingQuality quality
)
1246 return InvalidParameter
;
1248 graphics
->compqual
= quality
;
1253 GpStatus WINGDIPAPI
GdipSetInterpolationMode(GpGraphics
*graphics
,
1254 InterpolationMode mode
)
1257 return InvalidParameter
;
1259 graphics
->interpolation
= mode
;
1264 GpStatus WINGDIPAPI
GdipSetPageScale(GpGraphics
*graphics
, REAL scale
)
1266 if(!graphics
|| (scale
<= 0.0))
1267 return InvalidParameter
;
1269 graphics
->scale
= scale
;
1274 GpStatus WINGDIPAPI
GdipSetPageUnit(GpGraphics
*graphics
, GpUnit unit
)
1276 if(!graphics
|| (unit
== UnitWorld
))
1277 return InvalidParameter
;
1279 graphics
->unit
= unit
;
1284 GpStatus WINGDIPAPI
GdipSetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1288 return InvalidParameter
;
1290 graphics
->pixeloffset
= mode
;
1295 GpStatus WINGDIPAPI
GdipSetSmoothingMode(GpGraphics
*graphics
, SmoothingMode mode
)
1298 return InvalidParameter
;
1300 graphics
->smoothing
= mode
;
1305 GpStatus WINGDIPAPI
GdipSetWorldTransform(GpGraphics
*graphics
, GpMatrix
*matrix
)
1307 if(!graphics
|| !matrix
)
1308 return InvalidParameter
;
1310 GdipDeleteMatrix(graphics
->worldtrans
);
1311 return GdipCloneMatrix(matrix
, &graphics
->worldtrans
);