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 /* This helper applies all the changes that the points listed in ptf need in
76 * order to be drawn on the device context. In the end, this should include at
78 * -scaling by page unit
79 * -applying world transformation
80 * -converting from float to int
81 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
82 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
83 * gdi to draw, and these functions would irreparably mess with line widths.
85 static void transform_and_round_points(GpGraphics
*graphics
, POINT
*pti
,
86 GDIPCONST GpPointF
*ptf
, INT count
)
91 switch(graphics
->unit
)
94 unitscale
= GetDeviceCaps(graphics
->hdc
, LOGPIXELSX
);
97 unitscale
= ((REAL
)GetDeviceCaps(graphics
->hdc
, LOGPIXELSX
)) / 72.0;
100 unitscale
= ((REAL
)GetDeviceCaps(graphics
->hdc
, LOGPIXELSX
)) / 300.0;
103 unitscale
= ((REAL
)GetDeviceCaps(graphics
->hdc
, LOGPIXELSX
)) / 25.4;
112 for(i
= 0; i
< count
; i
++){
113 pti
[i
].x
= roundr(unitscale
* ptf
[i
].X
);
114 pti
[i
].y
= roundr(unitscale
* ptf
[i
].Y
);
118 /* GdipDrawPie/GdipFillPie helper function */
119 static GpStatus
draw_pie(GpGraphics
*graphics
, HBRUSH gdibrush
, HPEN gdipen
,
120 REAL x
, REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
127 return InvalidParameter
;
129 save_state
= SaveDC(graphics
->hdc
);
130 EndPath(graphics
->hdc
);
131 SelectObject(graphics
->hdc
, gdipen
);
132 SelectObject(graphics
->hdc
, gdibrush
);
136 ptf
[1].X
= x
+ width
;
137 ptf
[1].Y
= y
+ height
;
139 deg2xy(startAngle
+sweepAngle
, x
+ width
/ 2.0, y
+ width
/ 2.0, &ptf
[2].X
, &ptf
[2].Y
);
140 deg2xy(startAngle
, x
+ width
/ 2.0, y
+ width
/ 2.0, &ptf
[3].X
, &ptf
[3].Y
);
142 transform_and_round_points(graphics
, pti
, ptf
, 4);
144 Pie(graphics
->hdc
, pti
[0].x
, pti
[0].y
, pti
[1].x
, pti
[1].y
, pti
[2].x
,
145 pti
[2].y
, pti
[3].x
, pti
[3].y
);
147 RestoreDC(graphics
->hdc
, save_state
);
152 /* GdipDrawCurve helper function.
153 * Calculates Bezier points from cardinal spline points. */
154 static void calc_curve_bezier(CONST GpPointF
*pts
, REAL tension
, REAL
*x1
,
155 REAL
*y1
, REAL
*x2
, REAL
*y2
)
159 /* calculate tangent */
160 xdiff
= pts
[2].X
- pts
[0].X
;
161 ydiff
= pts
[2].Y
- pts
[0].Y
;
163 /* apply tangent to get control points */
164 *x1
= pts
[1].X
- tension
* xdiff
;
165 *y1
= pts
[1].Y
- tension
* ydiff
;
166 *x2
= pts
[1].X
+ tension
* xdiff
;
167 *y2
= pts
[1].Y
+ tension
* ydiff
;
170 /* GdipDrawCurve helper function.
171 * Calculates Bezier points from cardinal spline endpoints. */
172 static void calc_curve_bezier_endp(REAL xend
, REAL yend
, REAL xadj
, REAL yadj
,
173 REAL tension
, REAL
*x
, REAL
*y
)
175 /* tangent at endpoints is the line from the endpoint to the adjacent point */
176 *x
= roundr(tension
* (xadj
- xend
) + xend
);
177 *y
= roundr(tension
* (yadj
- yend
) + yend
);
180 /* Draws the linecap the specified color and size on the hdc. The linecap is in
181 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
182 * should not be called on an hdc that has a path you care about. */
183 static void draw_cap(GpGraphics
*graphics
, COLORREF color
, GpLineCap cap
, REAL size
,
184 const GpCustomLineCap
*custom
, REAL x1
, REAL y1
, REAL x2
, REAL y2
)
186 HGDIOBJ oldbrush
, oldpen
;
187 GpMatrix
*matrix
= NULL
;
190 PointF ptf
[4], *custptf
= NULL
;
191 POINT pt
[4], *custpt
= NULL
;
193 REAL theta
, dsmall
, dbig
, dx
, dy
= 0.0;
197 if((x1
== x2
) && (y1
== y2
))
200 theta
= gdiplus_atan2(y2
- y1
, x2
- x1
);
202 brush
= CreateSolidBrush(color
);
203 lb
.lbStyle
= BS_SOLID
;
206 pen
= ExtCreatePen(PS_GEOMETRIC
| PS_SOLID
| PS_ENDCAP_FLAT
,
207 ((cap
== LineCapCustom
) && custom
&& (custom
->fill
)) ? size
: 1,
209 oldbrush
= SelectObject(graphics
->hdc
, brush
);
210 oldpen
= SelectObject(graphics
->hdc
, pen
);
216 case LineCapSquareAnchor
:
217 case LineCapDiamondAnchor
:
218 size
= size
* (cap
& LineCapNoAnchor
? ANCHOR_WIDTH
: 1.0) / 2.0;
219 if(cap
== LineCapDiamondAnchor
){
220 dsmall
= cos(theta
+ M_PI_2
) * size
;
221 dbig
= sin(theta
+ M_PI_2
) * size
;
224 dsmall
= cos(theta
+ M_PI_4
) * size
;
225 dbig
= sin(theta
+ M_PI_4
) * size
;
228 ptf
[0].X
= x2
- dsmall
;
229 ptf
[1].X
= x2
+ dbig
;
231 ptf
[0].Y
= y2
- dbig
;
232 ptf
[3].Y
= y2
+ dsmall
;
234 ptf
[1].Y
= y2
- dsmall
;
235 ptf
[2].Y
= y2
+ dbig
;
237 ptf
[3].X
= x2
- dbig
;
238 ptf
[2].X
= x2
+ dsmall
;
240 transform_and_round_points(graphics
, pt
, ptf
, 4);
241 Polygon(graphics
->hdc
, pt
, 4);
244 case LineCapArrowAnchor
:
245 size
= size
* 4.0 / sqrt(3.0);
247 dx
= cos(M_PI
/ 6.0 + theta
) * size
;
248 dy
= sin(M_PI
/ 6.0 + theta
) * size
;
253 dx
= cos(- M_PI
/ 6.0 + theta
) * size
;
254 dy
= sin(- M_PI
/ 6.0 + theta
) * size
;
262 transform_and_round_points(graphics
, pt
, ptf
, 3);
263 Polygon(graphics
->hdc
, pt
, 3);
266 case LineCapRoundAnchor
:
267 dx
= dy
= ANCHOR_WIDTH
* size
/ 2.0;
274 transform_and_round_points(graphics
, pt
, ptf
, 2);
275 Ellipse(graphics
->hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
278 case LineCapTriangle
:
280 dx
= cos(M_PI_2
+ theta
) * size
;
281 dy
= sin(M_PI_2
+ theta
) * size
;
288 dx
= cos(theta
) * size
;
289 dy
= sin(theta
) * size
;
294 transform_and_round_points(graphics
, pt
, ptf
, 3);
295 Polygon(graphics
->hdc
, pt
, 3);
299 dx
= dy
= size
/ 2.0;
306 dx
= -cos(M_PI_2
+ theta
) * size
;
307 dy
= -sin(M_PI_2
+ theta
) * size
;
314 transform_and_round_points(graphics
, pt
, ptf
, 4);
315 Pie(graphics
->hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
, pt
[2].x
,
316 pt
[2].y
, pt
[3].x
, pt
[3].y
);
323 count
= custom
->pathdata
.Count
;
324 custptf
= GdipAlloc(count
* sizeof(PointF
));
325 custpt
= GdipAlloc(count
* sizeof(POINT
));
326 tp
= GdipAlloc(count
);
328 if(!custptf
|| !custpt
|| !tp
|| (GdipCreateMatrix(&matrix
) != Ok
))
331 memcpy(custptf
, custom
->pathdata
.Points
, count
* sizeof(PointF
));
333 GdipScaleMatrix(matrix
, size
, size
, MatrixOrderAppend
);
334 GdipRotateMatrix(matrix
, (180.0 / M_PI
) * (theta
- M_PI_2
),
336 GdipTranslateMatrix(matrix
, x2
, y2
, MatrixOrderAppend
);
337 GdipTransformMatrixPoints(matrix
, custptf
, count
);
339 transform_and_round_points(graphics
, custpt
, custptf
, count
);
341 for(i
= 0; i
< count
; i
++)
342 tp
[i
] = convert_path_point_type(custom
->pathdata
.Types
[i
]);
345 BeginPath(graphics
->hdc
);
346 PolyDraw(graphics
->hdc
, custpt
, tp
, count
);
347 EndPath(graphics
->hdc
);
348 StrokeAndFillPath(graphics
->hdc
);
351 PolyDraw(graphics
->hdc
, custpt
, tp
, count
);
357 GdipDeleteMatrix(matrix
);
363 SelectObject(graphics
->hdc
, oldbrush
);
364 SelectObject(graphics
->hdc
, oldpen
);
369 /* Shortens the line by the given percent by changing x2, y2.
370 * If percent is > 1.0 then the line will change direction.
371 * If percent is negative it can lengthen the line. */
372 static void shorten_line_percent(REAL x1
, REAL y1
, REAL
*x2
, REAL
*y2
, REAL percent
)
374 REAL dist
, theta
, dx
, dy
;
376 if((y1
== *y2
) && (x1
== *x2
))
379 dist
= sqrt((*x2
- x1
) * (*x2
- x1
) + (*y2
- y1
) * (*y2
- y1
)) * -percent
;
380 theta
= gdiplus_atan2((*y2
- y1
), (*x2
- x1
));
381 dx
= cos(theta
) * dist
;
382 dy
= sin(theta
) * dist
;
388 /* Shortens the line by the given amount by changing x2, y2.
389 * If the amount is greater than the distance, the line will become length 0.
390 * If the amount is negative, it can lengthen the line. */
391 static void shorten_line_amt(REAL x1
, REAL y1
, REAL
*x2
, REAL
*y2
, REAL amt
)
393 REAL dx
, dy
, percent
;
397 if(dx
== 0 && dy
== 0)
400 percent
= amt
/ sqrt(dx
* dx
+ dy
* dy
);
407 shorten_line_percent(x1
, y1
, x2
, y2
, percent
);
410 /* Draws lines between the given points, and if caps is true then draws an endcap
411 * at the end of the last line. FIXME: Startcaps not implemented. */
412 static GpStatus
draw_polyline(GpGraphics
*graphics
, GpPen
*pen
,
413 GDIPCONST GpPointF
* pt
, INT count
, BOOL caps
)
416 GpPointF
*ptcopy
= NULL
;
417 GpStatus status
= GenericError
;
422 pti
= GdipAlloc(count
* sizeof(POINT
));
423 ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
426 status
= OutOfMemory
;
431 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
433 if(pen
->endcap
== LineCapArrowAnchor
)
434 shorten_line_amt(ptcopy
[count
-2].X
, ptcopy
[count
-2].Y
,
435 &ptcopy
[count
-1].X
, &ptcopy
[count
-1].Y
, pen
->width
);
436 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
437 shorten_line_amt(ptcopy
[count
-2].X
, ptcopy
[count
-2].Y
,
438 &ptcopy
[count
-1].X
, &ptcopy
[count
-1].Y
,
439 pen
->customend
->inset
* pen
->width
);
441 if(pen
->startcap
== LineCapArrowAnchor
)
442 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
,
443 &ptcopy
[0].X
, &ptcopy
[0].Y
, pen
->width
);
444 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
445 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
,
446 &ptcopy
[0].X
, &ptcopy
[0].Y
,
447 pen
->customend
->inset
* pen
->width
);
449 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
450 pt
[count
- 2].X
, pt
[count
- 2].Y
, pt
[count
- 1].X
, pt
[count
- 1].Y
);
451 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
452 pt
[1].X
, pt
[1].Y
, pt
[0].X
, pt
[0].Y
);\
454 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
457 transform_and_round_points(graphics
, pti
, pt
, count
);
459 Polyline(graphics
->hdc
, pti
, count
);
468 /* Conducts a linear search to find the bezier points that will back off
469 * the endpoint of the curve by a distance of amt. Linear search works
470 * better than binary in this case because there are multiple solutions,
471 * and binary searches often find a bad one. I don't think this is what
472 * Windows does but short of rendering the bezier without GDI's help it's
473 * the best we can do. If rev then work from the start of the passed points
474 * instead of the end. */
475 static void shorten_bezier_amt(GpPointF
* pt
, REAL amt
, BOOL rev
)
478 REAL percent
= 0.00, dx
, dy
, origx
, origy
, diff
= -1.0;
479 INT i
, first
= 0, second
= 1, third
= 2, fourth
= 3;
488 origx
= pt
[fourth
].X
;
489 origy
= pt
[fourth
].Y
;
490 memcpy(origpt
, pt
, sizeof(GpPointF
) * 4);
492 for(i
= 0; (i
< MAX_ITERS
) && (diff
< amt
); i
++){
493 /* reset bezier points to original values */
494 memcpy(pt
, origpt
, sizeof(GpPointF
) * 4);
495 /* Perform magic on bezier points. Order is important here.*/
496 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
497 shorten_line_percent(pt
[second
].X
, pt
[second
].Y
, &pt
[third
].X
, &pt
[third
].Y
, percent
);
498 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
499 shorten_line_percent(pt
[first
].X
, pt
[first
].Y
, &pt
[second
].X
, &pt
[second
].Y
, percent
);
500 shorten_line_percent(pt
[second
].X
, pt
[second
].Y
, &pt
[third
].X
, &pt
[third
].Y
, percent
);
501 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
503 dx
= pt
[fourth
].X
- origx
;
504 dy
= pt
[fourth
].Y
- origy
;
506 diff
= sqrt(dx
* dx
+ dy
* dy
);
507 percent
+= 0.0005 * amt
;
511 /* Draws bezier curves between given points, and if caps is true then draws an
512 * endcap at the end of the last line. FIXME: Startcaps not implemented. */
513 static GpStatus
draw_polybezier(GpGraphics
*graphics
, GpPen
*pen
,
514 GDIPCONST GpPointF
* pt
, INT count
, BOOL caps
)
519 GpStatus status
= GenericError
;
524 pti
= GdipAlloc(count
* sizeof(POINT
));
525 ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
528 status
= OutOfMemory
;
533 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
535 if(pen
->endcap
== LineCapArrowAnchor
)
536 shorten_bezier_amt(&ptcopy
[count
-4], pen
->width
, FALSE
);
537 /* FIXME The following is seemingly correct only for baseinset < 0 or
538 * baseinset > ~3. With smaller baseinsets, windows actually
539 * lengthens the bezier line instead of shortening it. */
540 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
){
543 shorten_line_amt(pt
[count
- 2].X
, pt
[count
- 2].Y
, &x
, &y
,
544 pen
->width
* pen
->customend
->inset
);
545 MoveToEx(graphics
->hdc
, roundr(pt
[count
- 1].X
), roundr(pt
[count
- 1].Y
), &curpos
);
546 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
547 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
550 if(pen
->startcap
== LineCapArrowAnchor
)
551 shorten_bezier_amt(ptcopy
, pen
->width
, TRUE
);
552 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
){
555 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
, &x
, &y
,
556 pen
->width
* pen
->customend
->inset
);
557 MoveToEx(graphics
->hdc
, roundr(pt
[0].X
), roundr(pt
[0].Y
), &curpos
);
558 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
559 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
562 /* the direction of the line cap is parallel to the direction at the
563 * end of the bezier (which, if it has been shortened, is not the same
564 * as the direction from pt[count-2] to pt[count-1]) */
565 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
566 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
567 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
568 pt
[count
- 1].X
, pt
[count
- 1].Y
);
570 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
571 pt
[0].X
- (ptcopy
[0].X
- ptcopy
[1].X
),
572 pt
[0].Y
- (ptcopy
[0].Y
- ptcopy
[1].Y
), pt
[0].X
, pt
[0].Y
);
574 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
577 transform_and_round_points(graphics
, pti
, pt
, count
);
579 PolyBezier(graphics
->hdc
, pti
, count
);
590 /* Draws a combination of bezier curves and lines between points. */
591 static GpStatus
draw_poly(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST GpPointF
* pt
,
592 GDIPCONST BYTE
* types
, INT count
, BOOL caps
)
594 POINT
*pti
= GdipAlloc(count
* sizeof(POINT
)), curpos
;
595 BYTE
*tp
= GdipAlloc(count
);
596 GpPointF
*ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
597 REAL x
= pt
[count
- 1].X
, y
= pt
[count
- 1].Y
;
599 GpStatus status
= GenericError
;
605 if(!pti
|| !tp
|| !ptcopy
){
606 status
= OutOfMemory
;
610 for(i
= 1; i
< count
; i
++){
611 if((types
[i
] & PathPointTypePathTypeMask
) == PathPointTypeBezier
){
612 if((i
+ 2 >= count
) || !(types
[i
+ 1] & PathPointTypeBezier
)
613 || !(types
[i
+ 1] & PathPointTypeBezier
)){
614 ERR("Bad bezier points\n");
621 /* If we are drawing caps, go through the points and adjust them accordingly,
622 * and draw the caps. */
624 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
626 switch(types
[count
- 1] & PathPointTypePathTypeMask
){
627 case PathPointTypeBezier
:
628 if(pen
->endcap
== LineCapArrowAnchor
)
629 shorten_bezier_amt(&ptcopy
[count
- 4], pen
->width
, FALSE
);
630 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
){
633 shorten_line_amt(pt
[count
- 2].X
, pt
[count
- 2].Y
, &x
, &y
,
634 pen
->width
* pen
->customend
->inset
);
635 MoveToEx(graphics
->hdc
, roundr(pt
[count
- 1].X
),
636 roundr(pt
[count
- 1].Y
), &curpos
);
637 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
638 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
641 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
642 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
643 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
644 pt
[count
- 1].X
, pt
[count
- 1].Y
);
647 case PathPointTypeLine
:
648 if(pen
->endcap
== LineCapArrowAnchor
)
649 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
650 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
652 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
653 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
654 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
655 pen
->customend
->inset
* pen
->width
);
657 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
658 pt
[count
- 2].X
, pt
[count
- 2].Y
, pt
[count
- 1].X
,
663 ERR("Bad path last point\n");
667 /* Find start of points */
668 for(j
= 1; j
< count
&& ((types
[j
] & PathPointTypePathTypeMask
)
669 == PathPointTypeStart
); j
++);
671 switch(types
[j
] & PathPointTypePathTypeMask
){
672 case PathPointTypeBezier
:
673 if(pen
->startcap
== LineCapArrowAnchor
)
674 shorten_bezier_amt(&ptcopy
[j
- 1], pen
->width
, TRUE
);
675 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
){
678 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
, &x
, &y
,
679 pen
->width
* pen
->customstart
->inset
);
680 MoveToEx(graphics
->hdc
, roundr(pt
[j
- 1].X
), roundr(pt
[j
- 1].Y
), &curpos
);
681 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
682 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
685 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
686 pt
[j
- 1].X
- (ptcopy
[j
- 1].X
- ptcopy
[j
].X
),
687 pt
[j
- 1].Y
- (ptcopy
[j
- 1].Y
- ptcopy
[j
].Y
),
688 pt
[j
- 1].X
, pt
[j
- 1].Y
);
691 case PathPointTypeLine
:
692 if(pen
->startcap
== LineCapArrowAnchor
)
693 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
694 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
696 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
697 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
698 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
699 pen
->customstart
->inset
* pen
->width
);
701 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customstart
,
702 pt
[j
].X
, pt
[j
].Y
, pt
[j
- 1].X
,
707 ERR("Bad path points\n");
710 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
713 transform_and_round_points(graphics
, pti
, pt
, count
);
715 for(i
= 0; i
< count
; i
++){
716 tp
[i
] = convert_path_point_type(types
[i
]);
719 PolyDraw(graphics
->hdc
, pti
, tp
, count
);
731 GpStatus WINGDIPAPI
GdipCreateFromHDC(HDC hdc
, GpGraphics
**graphics
)
737 return InvalidParameter
;
739 *graphics
= GdipAlloc(sizeof(GpGraphics
));
740 if(!*graphics
) return OutOfMemory
;
742 (*graphics
)->hdc
= hdc
;
743 (*graphics
)->hwnd
= NULL
;
744 (*graphics
)->smoothing
= SmoothingModeDefault
;
745 (*graphics
)->compqual
= CompositingQualityDefault
;
746 (*graphics
)->interpolation
= InterpolationModeDefault
;
747 (*graphics
)->pixeloffset
= PixelOffsetModeDefault
;
748 (*graphics
)->unit
= UnitDisplay
;
753 GpStatus WINGDIPAPI
GdipCreateFromHWND(HWND hwnd
, GpGraphics
**graphics
)
757 if((ret
= GdipCreateFromHDC(GetDC(hwnd
), graphics
)) != Ok
)
760 (*graphics
)->hwnd
= hwnd
;
765 GpStatus WINGDIPAPI
GdipDeleteGraphics(GpGraphics
*graphics
)
767 if(!graphics
) return InvalidParameter
;
769 ReleaseDC(graphics
->hwnd
, graphics
->hdc
);
771 HeapFree(GetProcessHeap(), 0, graphics
);
776 GpStatus WINGDIPAPI
GdipDrawArc(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
777 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
779 INT save_state
, num_pts
;
780 GpPointF points
[MAX_ARC_PTS
];
783 if(!graphics
|| !pen
)
784 return InvalidParameter
;
786 num_pts
= arc2polybezier(points
, x
, y
, width
, height
, startAngle
, sweepAngle
);
788 save_state
= SaveDC(graphics
->hdc
);
789 EndPath(graphics
->hdc
);
790 SelectObject(graphics
->hdc
, pen
->gdipen
);
792 retval
= draw_polybezier(graphics
, pen
, points
, num_pts
, TRUE
);
794 RestoreDC(graphics
->hdc
, save_state
);
799 GpStatus WINGDIPAPI
GdipDrawBezier(GpGraphics
*graphics
, GpPen
*pen
, REAL x1
,
800 REAL y1
, REAL x2
, REAL y2
, REAL x3
, REAL y3
, REAL x4
, REAL y4
)
806 if(!graphics
|| !pen
)
807 return InvalidParameter
;
818 save_state
= SaveDC(graphics
->hdc
);
819 EndPath(graphics
->hdc
);
820 SelectObject(graphics
->hdc
, pen
->gdipen
);
822 retval
= draw_polybezier(graphics
, pen
, pt
, 4, TRUE
);
824 RestoreDC(graphics
->hdc
, save_state
);
829 /* Approximates cardinal spline with Bezier curves. */
830 GpStatus WINGDIPAPI
GdipDrawCurve2(GpGraphics
*graphics
, GpPen
*pen
,
831 GDIPCONST GpPointF
*points
, INT count
, REAL tension
)
833 /* PolyBezier expects count*3-2 points. */
834 INT i
, len_pt
= count
*3-2, save_state
;
839 if(!graphics
|| !pen
)
840 return InvalidParameter
;
842 pt
= GdipAlloc(len_pt
* sizeof(GpPointF
));
843 tension
= tension
* TENSION_CONST
;
845 calc_curve_bezier_endp(points
[0].X
, points
[0].Y
, points
[1].X
, points
[1].Y
,
848 pt
[0].X
= points
[0].X
;
849 pt
[0].Y
= points
[0].Y
;
853 for(i
= 0; i
< count
-2; i
++){
854 calc_curve_bezier(&(points
[i
]), tension
, &x1
, &y1
, &x2
, &y2
);
858 pt
[3*i
+3].X
= points
[i
+1].X
;
859 pt
[3*i
+3].Y
= points
[i
+1].Y
;
864 calc_curve_bezier_endp(points
[count
-1].X
, points
[count
-1].Y
,
865 points
[count
-2].X
, points
[count
-2].Y
, tension
, &x1
, &y1
);
869 pt
[len_pt
-1].X
= points
[count
-1].X
;
870 pt
[len_pt
-1].Y
= points
[count
-1].Y
;
872 save_state
= SaveDC(graphics
->hdc
);
873 EndPath(graphics
->hdc
);
874 SelectObject(graphics
->hdc
, pen
->gdipen
);
876 retval
= draw_polybezier(graphics
, pen
, pt
, len_pt
, TRUE
);
879 RestoreDC(graphics
->hdc
, save_state
);
884 GpStatus WINGDIPAPI
GdipDrawLineI(GpGraphics
*graphics
, GpPen
*pen
, INT x1
,
885 INT y1
, INT x2
, INT y2
)
891 if(!pen
|| !graphics
)
892 return InvalidParameter
;
899 save_state
= SaveDC(graphics
->hdc
);
900 EndPath(graphics
->hdc
);
901 SelectObject(graphics
->hdc
, pen
->gdipen
);
903 retval
= draw_polyline(graphics
, pen
, pt
, 2, TRUE
);
905 RestoreDC(graphics
->hdc
, save_state
);
910 GpStatus WINGDIPAPI
GdipDrawLines(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST
911 GpPointF
*points
, INT count
)
916 if(!pen
|| !graphics
|| (count
< 2))
917 return InvalidParameter
;
919 save_state
= SaveDC(graphics
->hdc
);
920 EndPath(graphics
->hdc
);
921 SelectObject(graphics
->hdc
, pen
->gdipen
);
923 retval
= draw_polyline(graphics
, pen
, points
, count
, TRUE
);
925 RestoreDC(graphics
->hdc
, save_state
);
930 GpStatus WINGDIPAPI
GdipDrawPath(GpGraphics
*graphics
, GpPen
*pen
, GpPath
*path
)
935 if(!pen
|| !graphics
)
936 return InvalidParameter
;
938 save_state
= SaveDC(graphics
->hdc
);
939 EndPath(graphics
->hdc
);
940 SelectObject(graphics
->hdc
, pen
->gdipen
);
942 retval
= draw_poly(graphics
, pen
, path
->pathdata
.Points
,
943 path
->pathdata
.Types
, path
->pathdata
.Count
, TRUE
);
945 RestoreDC(graphics
->hdc
, save_state
);
950 GpStatus WINGDIPAPI
GdipDrawPie(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
951 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
954 return InvalidParameter
;
956 return draw_pie(graphics
, GetStockObject(NULL_BRUSH
), pen
->gdipen
, x
, y
,
957 width
, height
, startAngle
, sweepAngle
);
960 GpStatus WINGDIPAPI
GdipDrawRectangleI(GpGraphics
*graphics
, GpPen
*pen
, INT x
,
961 INT y
, INT width
, INT height
)
965 if(!pen
|| !graphics
)
966 return InvalidParameter
;
968 save_state
= SaveDC(graphics
->hdc
);
969 EndPath(graphics
->hdc
);
970 SelectObject(graphics
->hdc
, pen
->gdipen
);
971 SelectObject(graphics
->hdc
, GetStockObject(NULL_BRUSH
));
973 Rectangle(graphics
->hdc
, x
, y
, x
+ width
, y
+ height
);
975 RestoreDC(graphics
->hdc
, save_state
);
980 GpStatus WINGDIPAPI
GdipFillPath(GpGraphics
*graphics
, GpBrush
*brush
, GpPath
*path
)
985 if(!brush
|| !graphics
|| !path
)
986 return InvalidParameter
;
988 save_state
= SaveDC(graphics
->hdc
);
989 EndPath(graphics
->hdc
);
990 SelectObject(graphics
->hdc
, brush
->gdibrush
);
991 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
994 BeginPath(graphics
->hdc
);
995 retval
= draw_poly(graphics
, NULL
, path
->pathdata
.Points
,
996 path
->pathdata
.Types
, path
->pathdata
.Count
, FALSE
);
1001 EndPath(graphics
->hdc
);
1002 FillPath(graphics
->hdc
);
1007 RestoreDC(graphics
->hdc
, save_state
);
1012 GpStatus WINGDIPAPI
GdipFillPie(GpGraphics
*graphics
, GpBrush
*brush
, REAL x
,
1013 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
1016 return InvalidParameter
;
1018 return draw_pie(graphics
, brush
->gdibrush
, GetStockObject(NULL_PEN
), x
, y
,
1019 width
, height
, startAngle
, sweepAngle
);
1022 GpStatus WINGDIPAPI
GdipFillPolygonI(GpGraphics
*graphics
, GpBrush
*brush
,
1023 GDIPCONST GpPoint
*points
, INT count
, GpFillMode fillMode
)
1026 GpPointF
*ptf
= NULL
;
1028 GpStatus retval
= Ok
;
1030 if(!graphics
|| !brush
|| !points
|| !count
)
1031 return InvalidParameter
;
1033 ptf
= GdipAlloc(count
* sizeof(GpPointF
));
1034 pti
= GdipAlloc(count
* sizeof(POINT
));
1036 retval
= OutOfMemory
;
1040 for(i
= 0; i
< count
; i
++){
1041 ptf
[i
].X
= (REAL
) points
[i
].X
;
1042 ptf
[i
].Y
= (REAL
) points
[i
].Y
;
1045 save_state
= SaveDC(graphics
->hdc
);
1046 EndPath(graphics
->hdc
);
1047 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1048 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1049 SetPolyFillMode(graphics
->hdc
, (fillMode
== FillModeAlternate
? ALTERNATE
1052 transform_and_round_points(graphics
, pti
, ptf
, count
);
1053 Polygon(graphics
->hdc
, pti
, count
);
1055 RestoreDC(graphics
->hdc
, save_state
);
1064 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1065 GpStatus WINGDIPAPI
GdipGetCompositingQuality(GpGraphics
*graphics
,
1066 CompositingQuality
*quality
)
1068 if(!graphics
|| !quality
)
1069 return InvalidParameter
;
1071 *quality
= graphics
->compqual
;
1076 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1077 GpStatus WINGDIPAPI
GdipGetInterpolationMode(GpGraphics
*graphics
,
1078 InterpolationMode
*mode
)
1080 if(!graphics
|| !mode
)
1081 return InvalidParameter
;
1083 *mode
= graphics
->interpolation
;
1088 GpStatus WINGDIPAPI
GdipGetPageUnit(GpGraphics
*graphics
, GpUnit
*unit
)
1090 if(!graphics
|| !unit
)
1091 return InvalidParameter
;
1093 *unit
= graphics
->unit
;
1098 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1099 GpStatus WINGDIPAPI
GdipGetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1102 if(!graphics
|| !mode
)
1103 return InvalidParameter
;
1105 *mode
= graphics
->pixeloffset
;
1110 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1111 GpStatus WINGDIPAPI
GdipGetSmoothingMode(GpGraphics
*graphics
, SmoothingMode
*mode
)
1113 if(!graphics
|| !mode
)
1114 return InvalidParameter
;
1116 *mode
= graphics
->smoothing
;
1121 GpStatus WINGDIPAPI
GdipRestoreGraphics(GpGraphics
*graphics
, GraphicsState state
)
1124 return InvalidParameter
;
1126 FIXME("graphics state not implemented\n");
1128 return NotImplemented
;
1131 GpStatus WINGDIPAPI
GdipSaveGraphics(GpGraphics
*graphics
, GraphicsState
*state
)
1133 if(!graphics
|| !state
)
1134 return InvalidParameter
;
1136 FIXME("graphics state not implemented\n");
1138 return NotImplemented
;
1141 GpStatus WINGDIPAPI
GdipSetCompositingQuality(GpGraphics
*graphics
,
1142 CompositingQuality quality
)
1145 return InvalidParameter
;
1147 graphics
->compqual
= quality
;
1152 GpStatus WINGDIPAPI
GdipSetInterpolationMode(GpGraphics
*graphics
,
1153 InterpolationMode mode
)
1156 return InvalidParameter
;
1158 graphics
->interpolation
= mode
;
1163 GpStatus WINGDIPAPI
GdipSetPageUnit(GpGraphics
*graphics
, GpUnit unit
)
1165 if(!graphics
|| (unit
== UnitWorld
))
1166 return InvalidParameter
;
1168 graphics
->unit
= unit
;
1173 GpStatus WINGDIPAPI
GdipSetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1177 return InvalidParameter
;
1179 graphics
->pixeloffset
= mode
;
1184 GpStatus WINGDIPAPI
GdipSetSmoothingMode(GpGraphics
*graphics
, SmoothingMode mode
)
1187 return InvalidParameter
;
1189 graphics
->smoothing
= mode
;