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
34 #include "gdiplus_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
39 /* looks-right constants */
40 #define TENSION_CONST (0.3)
41 #define ANCHOR_WIDTH (2.0)
42 #define MAX_ITERS (50)
44 /* Converts angle (in degrees) to x/y coordinates */
45 static void deg2xy(REAL angle
, REAL x_0
, REAL y_0
, REAL
*x
, REAL
*y
)
47 REAL radAngle
, hypotenuse
;
49 radAngle
= deg2rad(angle
);
50 hypotenuse
= 50.0; /* arbitrary */
52 *x
= x_0
+ cos(radAngle
) * hypotenuse
;
53 *y
= y_0
+ sin(radAngle
) * hypotenuse
;
56 /* Converts from gdiplus path point type to gdi path point type. */
57 static BYTE
convert_path_point_type(BYTE type
)
61 switch(type
& PathPointTypePathTypeMask
){
62 case PathPointTypeBezier
:
65 case PathPointTypeLine
:
68 case PathPointTypeStart
:
72 ERR("Bad point type\n");
76 if(type
& PathPointTypeCloseSubpath
)
77 ret
|= PT_CLOSEFIGURE
;
82 static REAL
convert_unit(HDC hdc
, GpUnit unit
)
87 return (REAL
) GetDeviceCaps(hdc
, LOGPIXELSX
);
89 return ((REAL
)GetDeviceCaps(hdc
, LOGPIXELSX
)) / 72.0;
91 return ((REAL
)GetDeviceCaps(hdc
, LOGPIXELSX
)) / 300.0;
93 return ((REAL
)GetDeviceCaps(hdc
, LOGPIXELSX
)) / 25.4;
95 ERR("cannot convert UnitWorld\n");
104 static INT
prepare_dc(GpGraphics
*graphics
, GpPen
*pen
)
108 INT save_state
= SaveDC(graphics
->hdc
), i
, numdashes
;
110 DWORD dash_array
[MAX_DASHLEN
];
112 EndPath(graphics
->hdc
);
114 /* Get an estimate for the amount the pen width is affected by the world
115 * transform. (This is similar to what some of the wine drivers do.) */
120 GdipTransformMatrixPoints(graphics
->worldtrans
, pt
, 2);
121 width
= sqrt((pt
[1].X
- pt
[0].X
) * (pt
[1].X
- pt
[0].X
) +
122 (pt
[1].Y
- pt
[0].Y
) * (pt
[1].Y
- pt
[0].Y
)) / sqrt(2.0);
124 width
*= pen
->width
* convert_unit(graphics
->hdc
,
125 pen
->unit
== UnitWorld
? graphics
->unit
: pen
->unit
);
127 if(pen
->dash
== DashStyleCustom
){
128 numdashes
= min(pen
->numdashes
, MAX_DASHLEN
);
130 TRACE("dashes are: ");
131 for(i
= 0; i
< numdashes
; i
++){
132 dash_array
[i
] = roundr(width
* pen
->dashes
[i
]);
133 TRACE("%d, ", dash_array
[i
]);
135 TRACE("\n and the pen style is %x\n", pen
->style
);
137 gdipen
= ExtCreatePen(pen
->style
, roundr(width
), &pen
->brush
->lb
,
138 numdashes
, dash_array
);
141 gdipen
= ExtCreatePen(pen
->style
, roundr(width
), &pen
->brush
->lb
, 0, NULL
);
143 SelectObject(graphics
->hdc
, gdipen
);
148 static void restore_dc(GpGraphics
*graphics
, INT state
)
150 DeleteObject(SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
)));
151 RestoreDC(graphics
->hdc
, state
);
154 /* This helper applies all the changes that the points listed in ptf need in
155 * order to be drawn on the device context. In the end, this should include at
157 * -scaling by page unit
158 * -applying world transformation
159 * -converting from float to int
160 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
161 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
162 * gdi to draw, and these functions would irreparably mess with line widths.
164 static void transform_and_round_points(GpGraphics
*graphics
, POINT
*pti
,
165 GpPointF
*ptf
, INT count
)
171 unitscale
= convert_unit(graphics
->hdc
, graphics
->unit
);
173 /* apply page scale */
174 if(graphics
->unit
!= UnitDisplay
)
175 unitscale
*= graphics
->scale
;
177 GdipCloneMatrix(graphics
->worldtrans
, &matrix
);
178 GdipScaleMatrix(matrix
, unitscale
, unitscale
, MatrixOrderAppend
);
179 GdipTransformMatrixPoints(matrix
, ptf
, count
);
180 GdipDeleteMatrix(matrix
);
182 for(i
= 0; i
< count
; i
++){
183 pti
[i
].x
= roundr(ptf
[i
].X
);
184 pti
[i
].y
= roundr(ptf
[i
].Y
);
188 /* GdipDrawPie/GdipFillPie helper function */
189 static void draw_pie(GpGraphics
*graphics
, REAL x
, REAL y
, REAL width
,
190 REAL height
, REAL startAngle
, REAL sweepAngle
)
197 ptf
[1].X
= x
+ width
;
198 ptf
[1].Y
= y
+ height
;
200 deg2xy(startAngle
+sweepAngle
, x
+ width
/ 2.0, y
+ width
/ 2.0, &ptf
[2].X
, &ptf
[2].Y
);
201 deg2xy(startAngle
, x
+ width
/ 2.0, y
+ width
/ 2.0, &ptf
[3].X
, &ptf
[3].Y
);
203 transform_and_round_points(graphics
, pti
, ptf
, 4);
205 Pie(graphics
->hdc
, pti
[0].x
, pti
[0].y
, pti
[1].x
, pti
[1].y
, pti
[2].x
,
206 pti
[2].y
, pti
[3].x
, pti
[3].y
);
209 /* GdipDrawCurve helper function.
210 * Calculates Bezier points from cardinal spline points. */
211 static void calc_curve_bezier(CONST GpPointF
*pts
, REAL tension
, REAL
*x1
,
212 REAL
*y1
, REAL
*x2
, REAL
*y2
)
216 /* calculate tangent */
217 xdiff
= pts
[2].X
- pts
[0].X
;
218 ydiff
= pts
[2].Y
- pts
[0].Y
;
220 /* apply tangent to get control points */
221 *x1
= pts
[1].X
- tension
* xdiff
;
222 *y1
= pts
[1].Y
- tension
* ydiff
;
223 *x2
= pts
[1].X
+ tension
* xdiff
;
224 *y2
= pts
[1].Y
+ tension
* ydiff
;
227 /* GdipDrawCurve helper function.
228 * Calculates Bezier points from cardinal spline endpoints. */
229 static void calc_curve_bezier_endp(REAL xend
, REAL yend
, REAL xadj
, REAL yadj
,
230 REAL tension
, REAL
*x
, REAL
*y
)
232 /* tangent at endpoints is the line from the endpoint to the adjacent point */
233 *x
= roundr(tension
* (xadj
- xend
) + xend
);
234 *y
= roundr(tension
* (yadj
- yend
) + yend
);
237 /* Draws the linecap the specified color and size on the hdc. The linecap is in
238 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
239 * should not be called on an hdc that has a path you care about. */
240 static void draw_cap(GpGraphics
*graphics
, COLORREF color
, GpLineCap cap
, REAL size
,
241 const GpCustomLineCap
*custom
, REAL x1
, REAL y1
, REAL x2
, REAL y2
)
243 HGDIOBJ oldbrush
= NULL
, oldpen
= NULL
;
244 GpMatrix
*matrix
= NULL
;
247 PointF ptf
[4], *custptf
= NULL
;
248 POINT pt
[4], *custpt
= NULL
;
250 REAL theta
, dsmall
, dbig
, dx
, dy
= 0.0;
255 if((x1
== x2
) && (y1
== y2
))
258 theta
= gdiplus_atan2(y2
- y1
, x2
- x1
);
260 customstroke
= (cap
== LineCapCustom
) && custom
&& (!custom
->fill
);
262 brush
= CreateSolidBrush(color
);
263 lb
.lbStyle
= BS_SOLID
;
266 pen
= ExtCreatePen(PS_GEOMETRIC
| PS_SOLID
| PS_ENDCAP_FLAT
|
267 PS_JOIN_MITER
, 1, &lb
, 0,
269 oldbrush
= SelectObject(graphics
->hdc
, brush
);
270 oldpen
= SelectObject(graphics
->hdc
, pen
);
277 case LineCapSquareAnchor
:
278 case LineCapDiamondAnchor
:
279 size
= size
* (cap
& LineCapNoAnchor
? ANCHOR_WIDTH
: 1.0) / 2.0;
280 if(cap
== LineCapDiamondAnchor
){
281 dsmall
= cos(theta
+ M_PI_2
) * size
;
282 dbig
= sin(theta
+ M_PI_2
) * size
;
285 dsmall
= cos(theta
+ M_PI_4
) * size
;
286 dbig
= sin(theta
+ M_PI_4
) * size
;
289 ptf
[0].X
= x2
- dsmall
;
290 ptf
[1].X
= x2
+ dbig
;
292 ptf
[0].Y
= y2
- dbig
;
293 ptf
[3].Y
= y2
+ dsmall
;
295 ptf
[1].Y
= y2
- dsmall
;
296 ptf
[2].Y
= y2
+ dbig
;
298 ptf
[3].X
= x2
- dbig
;
299 ptf
[2].X
= x2
+ dsmall
;
301 transform_and_round_points(graphics
, pt
, ptf
, 4);
302 Polygon(graphics
->hdc
, pt
, 4);
305 case LineCapArrowAnchor
:
306 size
= size
* 4.0 / sqrt(3.0);
308 dx
= cos(M_PI
/ 6.0 + theta
) * size
;
309 dy
= sin(M_PI
/ 6.0 + theta
) * size
;
314 dx
= cos(- M_PI
/ 6.0 + theta
) * size
;
315 dy
= sin(- M_PI
/ 6.0 + theta
) * size
;
323 transform_and_round_points(graphics
, pt
, ptf
, 3);
324 Polygon(graphics
->hdc
, pt
, 3);
327 case LineCapRoundAnchor
:
328 dx
= dy
= ANCHOR_WIDTH
* size
/ 2.0;
335 transform_and_round_points(graphics
, pt
, ptf
, 2);
336 Ellipse(graphics
->hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
339 case LineCapTriangle
:
341 dx
= cos(M_PI_2
+ theta
) * size
;
342 dy
= sin(M_PI_2
+ theta
) * size
;
349 dx
= cos(theta
) * size
;
350 dy
= sin(theta
) * size
;
355 transform_and_round_points(graphics
, pt
, ptf
, 3);
356 Polygon(graphics
->hdc
, pt
, 3);
360 dx
= dy
= size
/ 2.0;
367 dx
= -cos(M_PI_2
+ theta
) * size
;
368 dy
= -sin(M_PI_2
+ theta
) * size
;
375 transform_and_round_points(graphics
, pt
, ptf
, 4);
376 Pie(graphics
->hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
, pt
[2].x
,
377 pt
[2].y
, pt
[3].x
, pt
[3].y
);
384 count
= custom
->pathdata
.Count
;
385 custptf
= GdipAlloc(count
* sizeof(PointF
));
386 custpt
= GdipAlloc(count
* sizeof(POINT
));
387 tp
= GdipAlloc(count
);
389 if(!custptf
|| !custpt
|| !tp
|| (GdipCreateMatrix(&matrix
) != Ok
))
392 memcpy(custptf
, custom
->pathdata
.Points
, count
* sizeof(PointF
));
394 GdipScaleMatrix(matrix
, size
, size
, MatrixOrderAppend
);
395 GdipRotateMatrix(matrix
, (180.0 / M_PI
) * (theta
- M_PI_2
),
397 GdipTranslateMatrix(matrix
, x2
, y2
, MatrixOrderAppend
);
398 GdipTransformMatrixPoints(matrix
, custptf
, count
);
400 transform_and_round_points(graphics
, custpt
, custptf
, count
);
402 for(i
= 0; i
< count
; i
++)
403 tp
[i
] = convert_path_point_type(custom
->pathdata
.Types
[i
]);
406 BeginPath(graphics
->hdc
);
407 PolyDraw(graphics
->hdc
, custpt
, tp
, count
);
408 EndPath(graphics
->hdc
);
409 StrokeAndFillPath(graphics
->hdc
);
412 PolyDraw(graphics
->hdc
, custpt
, tp
, count
);
418 GdipDeleteMatrix(matrix
);
425 SelectObject(graphics
->hdc
, oldbrush
);
426 SelectObject(graphics
->hdc
, oldpen
);
432 /* Shortens the line by the given percent by changing x2, y2.
433 * If percent is > 1.0 then the line will change direction.
434 * If percent is negative it can lengthen the line. */
435 static void shorten_line_percent(REAL x1
, REAL y1
, REAL
*x2
, REAL
*y2
, REAL percent
)
437 REAL dist
, theta
, dx
, dy
;
439 if((y1
== *y2
) && (x1
== *x2
))
442 dist
= sqrt((*x2
- x1
) * (*x2
- x1
) + (*y2
- y1
) * (*y2
- y1
)) * -percent
;
443 theta
= gdiplus_atan2((*y2
- y1
), (*x2
- x1
));
444 dx
= cos(theta
) * dist
;
445 dy
= sin(theta
) * dist
;
451 /* Shortens the line by the given amount by changing x2, y2.
452 * If the amount is greater than the distance, the line will become length 0.
453 * If the amount is negative, it can lengthen the line. */
454 static void shorten_line_amt(REAL x1
, REAL y1
, REAL
*x2
, REAL
*y2
, REAL amt
)
456 REAL dx
, dy
, percent
;
460 if(dx
== 0 && dy
== 0)
463 percent
= amt
/ sqrt(dx
* dx
+ dy
* dy
);
470 shorten_line_percent(x1
, y1
, x2
, y2
, percent
);
473 /* Draws lines between the given points, and if caps is true then draws an endcap
474 * at the end of the last line. */
475 static GpStatus
draw_polyline(GpGraphics
*graphics
, GpPen
*pen
,
476 GDIPCONST GpPointF
* pt
, INT count
, BOOL caps
)
479 GpPointF
*ptcopy
= NULL
;
480 GpStatus status
= GenericError
;
485 pti
= GdipAlloc(count
* sizeof(POINT
));
486 ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
489 status
= OutOfMemory
;
493 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
496 if(pen
->endcap
== LineCapArrowAnchor
)
497 shorten_line_amt(ptcopy
[count
-2].X
, ptcopy
[count
-2].Y
,
498 &ptcopy
[count
-1].X
, &ptcopy
[count
-1].Y
, pen
->width
);
499 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
500 shorten_line_amt(ptcopy
[count
-2].X
, ptcopy
[count
-2].Y
,
501 &ptcopy
[count
-1].X
, &ptcopy
[count
-1].Y
,
502 pen
->customend
->inset
* pen
->width
);
504 if(pen
->startcap
== LineCapArrowAnchor
)
505 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
,
506 &ptcopy
[0].X
, &ptcopy
[0].Y
, pen
->width
);
507 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
508 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
,
509 &ptcopy
[0].X
, &ptcopy
[0].Y
,
510 pen
->customend
->inset
* pen
->width
);
512 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
513 pt
[count
- 2].X
, pt
[count
- 2].Y
, pt
[count
- 1].X
, pt
[count
- 1].Y
);
514 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
515 pt
[1].X
, pt
[1].Y
, pt
[0].X
, pt
[0].Y
);\
518 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
520 Polyline(graphics
->hdc
, pti
, count
);
529 /* Conducts a linear search to find the bezier points that will back off
530 * the endpoint of the curve by a distance of amt. Linear search works
531 * better than binary in this case because there are multiple solutions,
532 * and binary searches often find a bad one. I don't think this is what
533 * Windows does but short of rendering the bezier without GDI's help it's
534 * the best we can do. If rev then work from the start of the passed points
535 * instead of the end. */
536 static void shorten_bezier_amt(GpPointF
* pt
, REAL amt
, BOOL rev
)
539 REAL percent
= 0.00, dx
, dy
, origx
, origy
, diff
= -1.0;
540 INT i
, first
= 0, second
= 1, third
= 2, fourth
= 3;
549 origx
= pt
[fourth
].X
;
550 origy
= pt
[fourth
].Y
;
551 memcpy(origpt
, pt
, sizeof(GpPointF
) * 4);
553 for(i
= 0; (i
< MAX_ITERS
) && (diff
< amt
); i
++){
554 /* reset bezier points to original values */
555 memcpy(pt
, origpt
, sizeof(GpPointF
) * 4);
556 /* Perform magic on bezier points. Order is important here.*/
557 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
558 shorten_line_percent(pt
[second
].X
, pt
[second
].Y
, &pt
[third
].X
, &pt
[third
].Y
, percent
);
559 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
560 shorten_line_percent(pt
[first
].X
, pt
[first
].Y
, &pt
[second
].X
, &pt
[second
].Y
, percent
);
561 shorten_line_percent(pt
[second
].X
, pt
[second
].Y
, &pt
[third
].X
, &pt
[third
].Y
, percent
);
562 shorten_line_percent(pt
[third
].X
, pt
[third
].Y
, &pt
[fourth
].X
, &pt
[fourth
].Y
, percent
);
564 dx
= pt
[fourth
].X
- origx
;
565 dy
= pt
[fourth
].Y
- origy
;
567 diff
= sqrt(dx
* dx
+ dy
* dy
);
568 percent
+= 0.0005 * amt
;
572 /* Draws bezier curves between given points, and if caps is true then draws an
573 * endcap at the end of the last line. */
574 static GpStatus
draw_polybezier(GpGraphics
*graphics
, GpPen
*pen
,
575 GDIPCONST GpPointF
* pt
, INT count
, BOOL caps
)
579 GpStatus status
= GenericError
;
584 pti
= GdipAlloc(count
* sizeof(POINT
));
585 ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
588 status
= OutOfMemory
;
592 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
595 if(pen
->endcap
== LineCapArrowAnchor
)
596 shorten_bezier_amt(&ptcopy
[count
-4], pen
->width
, FALSE
);
597 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
598 shorten_bezier_amt(&ptcopy
[count
-4], pen
->width
* pen
->customend
->inset
,
601 if(pen
->startcap
== LineCapArrowAnchor
)
602 shorten_bezier_amt(ptcopy
, pen
->width
, TRUE
);
603 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
604 shorten_bezier_amt(ptcopy
, pen
->width
* pen
->customend
->inset
, TRUE
);
606 /* the direction of the line cap is parallel to the direction at the
607 * end of the bezier (which, if it has been shortened, is not the same
608 * as the direction from pt[count-2] to pt[count-1]) */
609 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
610 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
611 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
612 pt
[count
- 1].X
, pt
[count
- 1].Y
);
614 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
615 pt
[0].X
- (ptcopy
[0].X
- ptcopy
[1].X
),
616 pt
[0].Y
- (ptcopy
[0].Y
- ptcopy
[1].Y
), pt
[0].X
, pt
[0].Y
);
619 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
621 PolyBezier(graphics
->hdc
, pti
, count
);
632 /* Draws a combination of bezier curves and lines between points. */
633 static GpStatus
draw_poly(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST GpPointF
* pt
,
634 GDIPCONST BYTE
* types
, INT count
, BOOL caps
)
636 POINT
*pti
= GdipAlloc(count
* sizeof(POINT
));
637 BYTE
*tp
= GdipAlloc(count
);
638 GpPointF
*ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
640 GpStatus status
= GenericError
;
646 if(!pti
|| !tp
|| !ptcopy
){
647 status
= OutOfMemory
;
651 for(i
= 1; i
< count
; i
++){
652 if((types
[i
] & PathPointTypePathTypeMask
) == PathPointTypeBezier
){
653 if((i
+ 2 >= count
) || !(types
[i
+ 1] & PathPointTypeBezier
)
654 || !(types
[i
+ 1] & PathPointTypeBezier
)){
655 ERR("Bad bezier points\n");
662 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
664 /* If we are drawing caps, go through the points and adjust them accordingly,
665 * and draw the caps. */
667 switch(types
[count
- 1] & PathPointTypePathTypeMask
){
668 case PathPointTypeBezier
:
669 if(pen
->endcap
== LineCapArrowAnchor
)
670 shorten_bezier_amt(&ptcopy
[count
- 4], pen
->width
, FALSE
);
671 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
672 shorten_bezier_amt(&ptcopy
[count
- 4],
673 pen
->width
* pen
->customend
->inset
, FALSE
);
675 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
676 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
677 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
678 pt
[count
- 1].X
, pt
[count
- 1].Y
);
681 case PathPointTypeLine
:
682 if(pen
->endcap
== LineCapArrowAnchor
)
683 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
684 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
686 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
687 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
688 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
689 pen
->customend
->inset
* pen
->width
);
691 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
692 pt
[count
- 2].X
, pt
[count
- 2].Y
, pt
[count
- 1].X
,
697 ERR("Bad path last point\n");
701 /* Find start of points */
702 for(j
= 1; j
< count
&& ((types
[j
] & PathPointTypePathTypeMask
)
703 == PathPointTypeStart
); j
++);
705 switch(types
[j
] & PathPointTypePathTypeMask
){
706 case PathPointTypeBezier
:
707 if(pen
->startcap
== LineCapArrowAnchor
)
708 shorten_bezier_amt(&ptcopy
[j
- 1], pen
->width
, TRUE
);
709 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
710 shorten_bezier_amt(&ptcopy
[j
- 1],
711 pen
->width
* pen
->customend
->inset
, TRUE
);
713 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
714 pt
[j
- 1].X
- (ptcopy
[j
- 1].X
- ptcopy
[j
].X
),
715 pt
[j
- 1].Y
- (ptcopy
[j
- 1].Y
- ptcopy
[j
].Y
),
716 pt
[j
- 1].X
, pt
[j
- 1].Y
);
719 case PathPointTypeLine
:
720 if(pen
->startcap
== LineCapArrowAnchor
)
721 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
722 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
724 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
725 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
726 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
727 pen
->customstart
->inset
* pen
->width
);
729 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
730 pt
[j
].X
, pt
[j
].Y
, pt
[j
- 1].X
,
735 ERR("Bad path points\n");
740 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
742 for(i
= 0; i
< count
; i
++){
743 tp
[i
] = convert_path_point_type(types
[i
]);
746 PolyDraw(graphics
->hdc
, pti
, tp
, count
);
758 GpStatus WINGDIPAPI
GdipCreateFromHDC(HDC hdc
, GpGraphics
**graphics
)
766 return InvalidParameter
;
768 *graphics
= GdipAlloc(sizeof(GpGraphics
));
769 if(!*graphics
) return OutOfMemory
;
771 if((retval
= GdipCreateMatrix(&(*graphics
)->worldtrans
)) != Ok
){
776 (*graphics
)->hdc
= hdc
;
777 (*graphics
)->hwnd
= NULL
;
778 (*graphics
)->smoothing
= SmoothingModeDefault
;
779 (*graphics
)->compqual
= CompositingQualityDefault
;
780 (*graphics
)->interpolation
= InterpolationModeDefault
;
781 (*graphics
)->pixeloffset
= PixelOffsetModeDefault
;
782 (*graphics
)->unit
= UnitDisplay
;
783 (*graphics
)->scale
= 1.0;
788 GpStatus WINGDIPAPI
GdipCreateFromHWND(HWND hwnd
, GpGraphics
**graphics
)
792 if((ret
= GdipCreateFromHDC(GetDC(hwnd
), graphics
)) != Ok
)
795 (*graphics
)->hwnd
= hwnd
;
800 GpStatus WINGDIPAPI
GdipCreateMetafileFromEmf(HENHMETAFILE hemf
, BOOL
delete,
801 GpMetafile
**metafile
)
805 if(!hemf
|| !metafile
)
806 return InvalidParameter
;
809 FIXME("not implemented\n");
811 return NotImplemented
;
814 GpStatus WINGDIPAPI
GdipCreateMetafileFromWmf(HMETAFILE hwmf
, BOOL
delete,
815 GDIPCONST WmfPlaceableFileHeader
* placeable
, GpMetafile
**metafile
)
817 IStream
*stream
= NULL
;
821 GpStatus retval
= GenericError
;
823 if(!hwmf
|| !metafile
|| !placeable
)
824 return InvalidParameter
;
825 read
= GetMetaFileBitsEx(hwmf
, 0, NULL
);
828 copy
= GdipAlloc(read
);
829 GetMetaFileBitsEx(hwmf
, read
, copy
);
831 hemf
= SetWinMetaFileBits(read
, copy
, NULL
, NULL
);
834 read
= GetEnhMetaFileBits(hemf
, 0, NULL
);
835 copy
= GdipAlloc(read
);
836 GetEnhMetaFileBits(hemf
, read
, copy
);
837 DeleteEnhMetaFile(hemf
);
839 if(CreateStreamOnHGlobal(copy
, TRUE
, &stream
) != S_OK
){
840 ERR("could not make stream\n");
844 *metafile
= GdipAlloc(sizeof(GpMetafile
));
846 retval
= OutOfMemory
;
850 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
851 (LPVOID
*) &((*metafile
)->image
.picture
)) != S_OK
){
856 (*metafile
)->image
.type
= ImageTypeMetafile
;
857 (*metafile
)->bounds
.X
= ((REAL
) placeable
->BoundingBox
.Left
) / ((REAL
) placeable
->Inch
);
858 (*metafile
)->bounds
.Y
= ((REAL
) placeable
->BoundingBox
.Right
) / ((REAL
) placeable
->Inch
);
859 (*metafile
)->bounds
.Width
= ((REAL
) (placeable
->BoundingBox
.Right
860 - placeable
->BoundingBox
.Left
)) / ((REAL
) placeable
->Inch
);
861 (*metafile
)->bounds
.Height
= ((REAL
) (placeable
->BoundingBox
.Bottom
862 - placeable
->BoundingBox
.Top
)) / ((REAL
) placeable
->Inch
);
863 (*metafile
)->unit
= UnitInch
;
866 DeleteMetaFile(hwmf
);
871 IStream_Release(stream
);
876 GpStatus WINGDIPAPI
GdipDeleteGraphics(GpGraphics
*graphics
)
878 if(!graphics
) return InvalidParameter
;
880 ReleaseDC(graphics
->hwnd
, graphics
->hdc
);
882 GdipDeleteMatrix(graphics
->worldtrans
);
883 HeapFree(GetProcessHeap(), 0, graphics
);
888 GpStatus WINGDIPAPI
GdipDrawArc(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
889 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
891 INT save_state
, num_pts
;
892 GpPointF points
[MAX_ARC_PTS
];
895 if(!graphics
|| !pen
)
896 return InvalidParameter
;
898 num_pts
= arc2polybezier(points
, x
, y
, width
, height
, startAngle
, sweepAngle
);
900 save_state
= prepare_dc(graphics
, pen
);
902 retval
= draw_polybezier(graphics
, pen
, points
, num_pts
, TRUE
);
904 restore_dc(graphics
, save_state
);
909 GpStatus WINGDIPAPI
GdipDrawBezier(GpGraphics
*graphics
, GpPen
*pen
, REAL x1
,
910 REAL y1
, REAL x2
, REAL y2
, REAL x3
, REAL y3
, REAL x4
, REAL y4
)
916 if(!graphics
|| !pen
)
917 return InvalidParameter
;
928 save_state
= prepare_dc(graphics
, pen
);
930 retval
= draw_polybezier(graphics
, pen
, pt
, 4, TRUE
);
932 restore_dc(graphics
, save_state
);
937 /* Approximates cardinal spline with Bezier curves. */
938 GpStatus WINGDIPAPI
GdipDrawCurve2(GpGraphics
*graphics
, GpPen
*pen
,
939 GDIPCONST GpPointF
*points
, INT count
, REAL tension
)
941 /* PolyBezier expects count*3-2 points. */
942 INT i
, len_pt
= count
*3-2, save_state
;
947 if(!graphics
|| !pen
)
948 return InvalidParameter
;
950 pt
= GdipAlloc(len_pt
* sizeof(GpPointF
));
951 tension
= tension
* TENSION_CONST
;
953 calc_curve_bezier_endp(points
[0].X
, points
[0].Y
, points
[1].X
, points
[1].Y
,
956 pt
[0].X
= points
[0].X
;
957 pt
[0].Y
= points
[0].Y
;
961 for(i
= 0; i
< count
-2; i
++){
962 calc_curve_bezier(&(points
[i
]), tension
, &x1
, &y1
, &x2
, &y2
);
966 pt
[3*i
+3].X
= points
[i
+1].X
;
967 pt
[3*i
+3].Y
= points
[i
+1].Y
;
972 calc_curve_bezier_endp(points
[count
-1].X
, points
[count
-1].Y
,
973 points
[count
-2].X
, points
[count
-2].Y
, tension
, &x1
, &y1
);
977 pt
[len_pt
-1].X
= points
[count
-1].X
;
978 pt
[len_pt
-1].Y
= points
[count
-1].Y
;
980 save_state
= prepare_dc(graphics
, pen
);
982 retval
= draw_polybezier(graphics
, pen
, pt
, len_pt
, TRUE
);
985 restore_dc(graphics
, save_state
);
990 /* FIXME: partially implemented */
991 GpStatus WINGDIPAPI
GdipDrawImagePointsRect(GpGraphics
*graphics
, GpImage
*image
,
992 GDIPCONST GpPointF
*points
, INT count
, REAL srcx
, REAL srcy
, REAL srcwidth
,
993 REAL srcheight
, GpUnit srcUnit
, GDIPCONST GpImageAttributes
* imageAttributes
,
994 DrawImageAbort callback
, VOID
* callbackData
)
1000 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics
, image
, points
, count
,
1001 srcx
, srcy
, srcwidth
, srcheight
, srcUnit
, imageAttributes
, callback
,
1004 if(!graphics
|| !image
|| !points
|| !imageAttributes
|| count
!= 3)
1005 return InvalidParameter
;
1007 if(srcUnit
== UnitInch
)
1008 dx
= dy
= (REAL
) INCH_HIMETRIC
;
1009 else if(srcUnit
== UnitPixel
){
1010 dx
= ((REAL
) INCH_HIMETRIC
) /
1011 ((REAL
) GetDeviceCaps(graphics
->hdc
, LOGPIXELSX
));
1012 dy
= ((REAL
) INCH_HIMETRIC
) /
1013 ((REAL
) GetDeviceCaps(graphics
->hdc
, LOGPIXELSY
));
1016 return NotImplemented
;
1018 memcpy(ptf
, points
, 3 * sizeof(GpPointF
));
1019 transform_and_round_points(graphics
, pti
, ptf
, 3);
1021 /* IPicture renders bitmaps with the y-axis reversed
1022 * FIXME: flipping for unknown image type might not be correct. */
1023 if(image
->type
!= ImageTypeMetafile
){
1026 pti
[0].y
= pti
[2].y
;
1030 if(IPicture_Render(image
->picture
, graphics
->hdc
,
1031 pti
[0].x
, pti
[0].y
, pti
[1].x
- pti
[0].x
, pti
[2].y
- pti
[0].y
,
1032 srcx
* dx
, srcy
* dy
,
1033 srcwidth
* dx
, srcheight
* dy
,
1036 callback(callbackData
);
1037 return GenericError
;
1043 GpStatus WINGDIPAPI
GdipDrawLine(GpGraphics
*graphics
, GpPen
*pen
, REAL x1
,
1044 REAL y1
, REAL x2
, REAL y2
)
1050 if(!pen
|| !graphics
)
1051 return InvalidParameter
;
1058 save_state
= prepare_dc(graphics
, pen
);
1060 retval
= draw_polyline(graphics
, pen
, pt
, 2, TRUE
);
1062 restore_dc(graphics
, save_state
);
1067 GpStatus WINGDIPAPI
GdipDrawLineI(GpGraphics
*graphics
, GpPen
*pen
, INT x1
,
1068 INT y1
, INT x2
, INT y2
)
1074 if(!pen
|| !graphics
)
1075 return InvalidParameter
;
1082 save_state
= prepare_dc(graphics
, pen
);
1084 retval
= draw_polyline(graphics
, pen
, pt
, 2, TRUE
);
1086 restore_dc(graphics
, save_state
);
1091 GpStatus WINGDIPAPI
GdipDrawLines(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST
1092 GpPointF
*points
, INT count
)
1097 if(!pen
|| !graphics
|| (count
< 2))
1098 return InvalidParameter
;
1100 save_state
= prepare_dc(graphics
, pen
);
1102 retval
= draw_polyline(graphics
, pen
, points
, count
, TRUE
);
1104 restore_dc(graphics
, save_state
);
1109 GpStatus WINGDIPAPI
GdipDrawPath(GpGraphics
*graphics
, GpPen
*pen
, GpPath
*path
)
1114 if(!pen
|| !graphics
)
1115 return InvalidParameter
;
1117 save_state
= prepare_dc(graphics
, pen
);
1119 retval
= draw_poly(graphics
, pen
, path
->pathdata
.Points
,
1120 path
->pathdata
.Types
, path
->pathdata
.Count
, TRUE
);
1122 restore_dc(graphics
, save_state
);
1127 GpStatus WINGDIPAPI
GdipDrawPie(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
1128 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
1132 if(!graphics
|| !pen
)
1133 return InvalidParameter
;
1135 save_state
= prepare_dc(graphics
, pen
);
1136 SelectObject(graphics
->hdc
, GetStockObject(NULL_BRUSH
));
1138 draw_pie(graphics
, x
, y
, width
, height
, startAngle
, sweepAngle
);
1140 restore_dc(graphics
, save_state
);
1145 GpStatus WINGDIPAPI
GdipDrawRectangleI(GpGraphics
*graphics
, GpPen
*pen
, INT x
,
1146 INT y
, INT width
, INT height
)
1150 if(!pen
|| !graphics
)
1151 return InvalidParameter
;
1153 save_state
= prepare_dc(graphics
, pen
);
1154 SelectObject(graphics
->hdc
, GetStockObject(NULL_BRUSH
));
1156 Rectangle(graphics
->hdc
, x
, y
, x
+ width
, y
+ height
);
1158 restore_dc(graphics
, save_state
);
1163 GpStatus WINGDIPAPI
GdipFillPath(GpGraphics
*graphics
, GpBrush
*brush
, GpPath
*path
)
1168 if(!brush
|| !graphics
|| !path
)
1169 return InvalidParameter
;
1171 save_state
= SaveDC(graphics
->hdc
);
1172 EndPath(graphics
->hdc
);
1173 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1174 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1177 BeginPath(graphics
->hdc
);
1178 retval
= draw_poly(graphics
, NULL
, path
->pathdata
.Points
,
1179 path
->pathdata
.Types
, path
->pathdata
.Count
, FALSE
);
1184 EndPath(graphics
->hdc
);
1185 FillPath(graphics
->hdc
);
1190 RestoreDC(graphics
->hdc
, save_state
);
1195 GpStatus WINGDIPAPI
GdipFillPie(GpGraphics
*graphics
, GpBrush
*brush
, REAL x
,
1196 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
1200 if(!graphics
|| !brush
)
1201 return InvalidParameter
;
1203 save_state
= SaveDC(graphics
->hdc
);
1204 EndPath(graphics
->hdc
);
1205 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1206 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1208 draw_pie(graphics
, x
, y
, width
, height
, startAngle
, sweepAngle
);
1210 RestoreDC(graphics
->hdc
, save_state
);
1215 GpStatus WINGDIPAPI
GdipFillPolygon(GpGraphics
*graphics
, GpBrush
*brush
,
1216 GDIPCONST GpPointF
*points
, INT count
, GpFillMode fillMode
)
1219 GpPointF
*ptf
= NULL
;
1221 GpStatus retval
= Ok
;
1223 if(!graphics
|| !brush
|| !points
|| !count
)
1224 return InvalidParameter
;
1226 ptf
= GdipAlloc(count
* sizeof(GpPointF
));
1227 pti
= GdipAlloc(count
* sizeof(POINT
));
1229 retval
= OutOfMemory
;
1233 memcpy(ptf
, points
, count
* sizeof(GpPointF
));
1235 save_state
= SaveDC(graphics
->hdc
);
1236 EndPath(graphics
->hdc
);
1237 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1238 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1239 SetPolyFillMode(graphics
->hdc
, (fillMode
== FillModeAlternate
? ALTERNATE
1242 transform_and_round_points(graphics
, pti
, ptf
, count
);
1243 Polygon(graphics
->hdc
, pti
, count
);
1245 RestoreDC(graphics
->hdc
, save_state
);
1254 GpStatus WINGDIPAPI
GdipFillPolygonI(GpGraphics
*graphics
, GpBrush
*brush
,
1255 GDIPCONST GpPoint
*points
, INT count
, GpFillMode fillMode
)
1258 GpPointF
*ptf
= NULL
;
1260 GpStatus retval
= Ok
;
1262 if(!graphics
|| !brush
|| !points
|| !count
)
1263 return InvalidParameter
;
1265 ptf
= GdipAlloc(count
* sizeof(GpPointF
));
1266 pti
= GdipAlloc(count
* sizeof(POINT
));
1268 retval
= OutOfMemory
;
1272 for(i
= 0; i
< count
; i
++){
1273 ptf
[i
].X
= (REAL
) points
[i
].X
;
1274 ptf
[i
].Y
= (REAL
) points
[i
].Y
;
1277 save_state
= SaveDC(graphics
->hdc
);
1278 EndPath(graphics
->hdc
);
1279 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1280 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1281 SetPolyFillMode(graphics
->hdc
, (fillMode
== FillModeAlternate
? ALTERNATE
1284 transform_and_round_points(graphics
, pti
, ptf
, count
);
1285 Polygon(graphics
->hdc
, pti
, count
);
1287 RestoreDC(graphics
->hdc
, save_state
);
1296 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1297 GpStatus WINGDIPAPI
GdipGetCompositingQuality(GpGraphics
*graphics
,
1298 CompositingQuality
*quality
)
1300 if(!graphics
|| !quality
)
1301 return InvalidParameter
;
1303 *quality
= graphics
->compqual
;
1308 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1309 GpStatus WINGDIPAPI
GdipGetInterpolationMode(GpGraphics
*graphics
,
1310 InterpolationMode
*mode
)
1312 if(!graphics
|| !mode
)
1313 return InvalidParameter
;
1315 *mode
= graphics
->interpolation
;
1320 GpStatus WINGDIPAPI
GdipGetPageScale(GpGraphics
*graphics
, REAL
*scale
)
1322 if(!graphics
|| !scale
)
1323 return InvalidParameter
;
1325 *scale
= graphics
->scale
;
1330 GpStatus WINGDIPAPI
GdipGetPageUnit(GpGraphics
*graphics
, GpUnit
*unit
)
1332 if(!graphics
|| !unit
)
1333 return InvalidParameter
;
1335 *unit
= graphics
->unit
;
1340 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1341 GpStatus WINGDIPAPI
GdipGetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1344 if(!graphics
|| !mode
)
1345 return InvalidParameter
;
1347 *mode
= graphics
->pixeloffset
;
1352 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1353 GpStatus WINGDIPAPI
GdipGetSmoothingMode(GpGraphics
*graphics
, SmoothingMode
*mode
)
1355 if(!graphics
|| !mode
)
1356 return InvalidParameter
;
1358 *mode
= graphics
->smoothing
;
1363 GpStatus WINGDIPAPI
GdipGetWorldTransform(GpGraphics
*graphics
, GpMatrix
*matrix
)
1365 if(!graphics
|| !matrix
)
1366 return InvalidParameter
;
1368 memcpy(matrix
, graphics
->worldtrans
, sizeof(GpMatrix
));
1372 GpStatus WINGDIPAPI
GdipRestoreGraphics(GpGraphics
*graphics
, GraphicsState state
)
1377 return InvalidParameter
;
1380 FIXME("graphics state not implemented\n");
1382 return NotImplemented
;
1385 GpStatus WINGDIPAPI
GdipSaveGraphics(GpGraphics
*graphics
, GraphicsState
*state
)
1389 if(!graphics
|| !state
)
1390 return InvalidParameter
;
1393 FIXME("graphics state not implemented\n");
1395 return NotImplemented
;
1398 GpStatus WINGDIPAPI
GdipSetCompositingQuality(GpGraphics
*graphics
,
1399 CompositingQuality quality
)
1402 return InvalidParameter
;
1404 graphics
->compqual
= quality
;
1409 GpStatus WINGDIPAPI
GdipSetInterpolationMode(GpGraphics
*graphics
,
1410 InterpolationMode mode
)
1413 return InvalidParameter
;
1415 graphics
->interpolation
= mode
;
1420 GpStatus WINGDIPAPI
GdipSetPageScale(GpGraphics
*graphics
, REAL scale
)
1422 if(!graphics
|| (scale
<= 0.0))
1423 return InvalidParameter
;
1425 graphics
->scale
= scale
;
1430 GpStatus WINGDIPAPI
GdipSetPageUnit(GpGraphics
*graphics
, GpUnit unit
)
1432 if(!graphics
|| (unit
== UnitWorld
))
1433 return InvalidParameter
;
1435 graphics
->unit
= unit
;
1440 GpStatus WINGDIPAPI
GdipSetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1444 return InvalidParameter
;
1446 graphics
->pixeloffset
= mode
;
1451 GpStatus WINGDIPAPI
GdipSetSmoothingMode(GpGraphics
*graphics
, SmoothingMode mode
)
1454 return InvalidParameter
;
1456 graphics
->smoothing
= mode
;
1461 GpStatus WINGDIPAPI
GdipSetWorldTransform(GpGraphics
*graphics
, GpMatrix
*matrix
)
1463 if(!graphics
|| !matrix
)
1464 return InvalidParameter
;
1466 GdipDeleteMatrix(graphics
->worldtrans
);
1467 return GdipCloneMatrix(matrix
, &graphics
->worldtrans
);