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. FIXME: Startcaps not implemented. */
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. FIXME: Startcaps not implemented. */
574 static GpStatus
draw_polybezier(GpGraphics
*graphics
, GpPen
*pen
,
575 GDIPCONST GpPointF
* pt
, INT count
, BOOL caps
)
580 GpStatus status
= GenericError
;
585 pti
= GdipAlloc(count
* sizeof(POINT
));
586 ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
589 status
= OutOfMemory
;
593 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
596 if(pen
->endcap
== LineCapArrowAnchor
)
597 shorten_bezier_amt(&ptcopy
[count
-4], pen
->width
, FALSE
);
598 /* FIXME The following is seemingly correct only for baseinset < 0 or
599 * baseinset > ~3. With smaller baseinsets, windows actually
600 * lengthens the bezier line instead of shortening it. */
601 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
){
604 shorten_line_amt(pt
[count
- 2].X
, pt
[count
- 2].Y
, &x
, &y
,
605 pen
->width
* pen
->customend
->inset
);
606 MoveToEx(graphics
->hdc
, roundr(pt
[count
- 1].X
), roundr(pt
[count
- 1].Y
), &curpos
);
607 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
608 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
611 if(pen
->startcap
== LineCapArrowAnchor
)
612 shorten_bezier_amt(ptcopy
, pen
->width
, TRUE
);
613 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
){
616 shorten_line_amt(ptcopy
[1].X
, ptcopy
[1].Y
, &x
, &y
,
617 pen
->width
* pen
->customend
->inset
);
618 MoveToEx(graphics
->hdc
, roundr(pt
[0].X
), roundr(pt
[0].Y
), &curpos
);
619 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
620 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
623 /* the direction of the line cap is parallel to the direction at the
624 * end of the bezier (which, if it has been shortened, is not the same
625 * as the direction from pt[count-2] to pt[count-1]) */
626 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
627 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
628 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
629 pt
[count
- 1].X
, pt
[count
- 1].Y
);
631 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
632 pt
[0].X
- (ptcopy
[0].X
- ptcopy
[1].X
),
633 pt
[0].Y
- (ptcopy
[0].Y
- ptcopy
[1].Y
), pt
[0].X
, pt
[0].Y
);
636 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
638 PolyBezier(graphics
->hdc
, pti
, count
);
649 /* Draws a combination of bezier curves and lines between points. */
650 static GpStatus
draw_poly(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST GpPointF
* pt
,
651 GDIPCONST BYTE
* types
, INT count
, BOOL caps
)
653 POINT
*pti
= GdipAlloc(count
* sizeof(POINT
)), curpos
;
654 BYTE
*tp
= GdipAlloc(count
);
655 GpPointF
*ptcopy
= GdipAlloc(count
* sizeof(GpPointF
));
656 REAL x
= pt
[count
- 1].X
, y
= pt
[count
- 1].Y
;
658 GpStatus status
= GenericError
;
664 if(!pti
|| !tp
|| !ptcopy
){
665 status
= OutOfMemory
;
669 for(i
= 1; i
< count
; i
++){
670 if((types
[i
] & PathPointTypePathTypeMask
) == PathPointTypeBezier
){
671 if((i
+ 2 >= count
) || !(types
[i
+ 1] & PathPointTypeBezier
)
672 || !(types
[i
+ 1] & PathPointTypeBezier
)){
673 ERR("Bad bezier points\n");
680 memcpy(ptcopy
, pt
, count
* sizeof(GpPointF
));
682 /* If we are drawing caps, go through the points and adjust them accordingly,
683 * and draw the caps. */
685 switch(types
[count
- 1] & PathPointTypePathTypeMask
){
686 case PathPointTypeBezier
:
687 if(pen
->endcap
== LineCapArrowAnchor
)
688 shorten_bezier_amt(&ptcopy
[count
- 4], pen
->width
, FALSE
);
689 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
){
692 shorten_line_amt(pt
[count
- 2].X
, pt
[count
- 2].Y
, &x
, &y
,
693 pen
->width
* pen
->customend
->inset
);
694 MoveToEx(graphics
->hdc
, roundr(pt
[count
- 1].X
),
695 roundr(pt
[count
- 1].Y
), &curpos
);
696 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
697 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
700 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
701 pt
[count
- 1].X
- (ptcopy
[count
- 1].X
- ptcopy
[count
- 2].X
),
702 pt
[count
- 1].Y
- (ptcopy
[count
- 1].Y
- ptcopy
[count
- 2].Y
),
703 pt
[count
- 1].X
, pt
[count
- 1].Y
);
706 case PathPointTypeLine
:
707 if(pen
->endcap
== LineCapArrowAnchor
)
708 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
709 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
711 else if((pen
->endcap
== LineCapCustom
) && pen
->customend
)
712 shorten_line_amt(ptcopy
[count
- 2].X
, ptcopy
[count
- 2].Y
,
713 &ptcopy
[count
- 1].X
, &ptcopy
[count
- 1].Y
,
714 pen
->customend
->inset
* pen
->width
);
716 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->endcap
, pen
->width
, pen
->customend
,
717 pt
[count
- 2].X
, pt
[count
- 2].Y
, pt
[count
- 1].X
,
722 ERR("Bad path last point\n");
726 /* Find start of points */
727 for(j
= 1; j
< count
&& ((types
[j
] & PathPointTypePathTypeMask
)
728 == PathPointTypeStart
); j
++);
730 switch(types
[j
] & PathPointTypePathTypeMask
){
731 case PathPointTypeBezier
:
732 if(pen
->startcap
== LineCapArrowAnchor
)
733 shorten_bezier_amt(&ptcopy
[j
- 1], pen
->width
, TRUE
);
734 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
){
737 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
, &x
, &y
,
738 pen
->width
* pen
->customstart
->inset
);
739 MoveToEx(graphics
->hdc
, roundr(pt
[j
- 1].X
), roundr(pt
[j
- 1].Y
), &curpos
);
740 LineTo(graphics
->hdc
, roundr(x
), roundr(y
));
741 MoveToEx(graphics
->hdc
, curpos
.x
, curpos
.y
, NULL
);
744 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
745 pt
[j
- 1].X
- (ptcopy
[j
- 1].X
- ptcopy
[j
].X
),
746 pt
[j
- 1].Y
- (ptcopy
[j
- 1].Y
- ptcopy
[j
].Y
),
747 pt
[j
- 1].X
, pt
[j
- 1].Y
);
750 case PathPointTypeLine
:
751 if(pen
->startcap
== LineCapArrowAnchor
)
752 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
753 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
755 else if((pen
->startcap
== LineCapCustom
) && pen
->customstart
)
756 shorten_line_amt(ptcopy
[j
].X
, ptcopy
[j
].Y
,
757 &ptcopy
[j
- 1].X
, &ptcopy
[j
- 1].Y
,
758 pen
->customstart
->inset
* pen
->width
);
760 draw_cap(graphics
, pen
->brush
->lb
.lbColor
, pen
->startcap
, pen
->width
, pen
->customstart
,
761 pt
[j
].X
, pt
[j
].Y
, pt
[j
- 1].X
,
766 ERR("Bad path points\n");
771 transform_and_round_points(graphics
, pti
, ptcopy
, count
);
773 for(i
= 0; i
< count
; i
++){
774 tp
[i
] = convert_path_point_type(types
[i
]);
777 PolyDraw(graphics
->hdc
, pti
, tp
, count
);
789 GpStatus WINGDIPAPI
GdipCreateFromHDC(HDC hdc
, GpGraphics
**graphics
)
797 return InvalidParameter
;
799 *graphics
= GdipAlloc(sizeof(GpGraphics
));
800 if(!*graphics
) return OutOfMemory
;
802 if((retval
= GdipCreateMatrix(&(*graphics
)->worldtrans
)) != Ok
){
807 (*graphics
)->hdc
= hdc
;
808 (*graphics
)->hwnd
= NULL
;
809 (*graphics
)->smoothing
= SmoothingModeDefault
;
810 (*graphics
)->compqual
= CompositingQualityDefault
;
811 (*graphics
)->interpolation
= InterpolationModeDefault
;
812 (*graphics
)->pixeloffset
= PixelOffsetModeDefault
;
813 (*graphics
)->unit
= UnitDisplay
;
814 (*graphics
)->scale
= 1.0;
819 GpStatus WINGDIPAPI
GdipCreateFromHWND(HWND hwnd
, GpGraphics
**graphics
)
823 if((ret
= GdipCreateFromHDC(GetDC(hwnd
), graphics
)) != Ok
)
826 (*graphics
)->hwnd
= hwnd
;
831 GpStatus WINGDIPAPI
GdipCreateMetafileFromEmf(HENHMETAFILE hemf
, BOOL
delete,
832 GpMetafile
**metafile
)
836 if(!hemf
|| !metafile
)
837 return InvalidParameter
;
840 FIXME("not implemented\n");
842 return NotImplemented
;
845 GpStatus WINGDIPAPI
GdipCreateMetafileFromWmf(HMETAFILE hwmf
, BOOL
delete,
846 GDIPCONST WmfPlaceableFileHeader
* placeable
, GpMetafile
**metafile
)
849 IStream
*stream
= NULL
;
854 GpStatus retval
= GenericError
;
856 if(!hwmf
|| !metafile
|| !placeable
)
857 return InvalidParameter
;
860 FIXME("partially implemented\n");
862 if(placeable
->Inch
!= INCH_HIMETRIC
)
863 return NotImplemented
;
865 mfp
.mm
= MM_HIMETRIC
;
866 mfp
.xExt
= placeable
->BoundingBox
.Right
- placeable
->BoundingBox
.Left
;
867 mfp
.yExt
= placeable
->BoundingBox
.Bottom
- placeable
->BoundingBox
.Top
;
870 read
= GetMetaFileBitsEx(hwmf
, 0, NULL
);
873 copy
= GdipAlloc(read
);
874 GetMetaFileBitsEx(hwmf
, read
, copy
);
876 hemf
= SetWinMetaFileBits(read
, copy
, NULL
, &mfp
);
879 read
= GetEnhMetaFileBits(hemf
, 0, NULL
);
880 copy
= GdipAlloc(read
);
881 GetEnhMetaFileBits(hemf
, read
, copy
);
882 DeleteEnhMetaFile(hemf
);
884 if(CreateStreamOnHGlobal(copy
, TRUE
, &stream
) != S_OK
){
885 ERR("could not make stream\n");
889 *metafile
= GdipAlloc(sizeof(GpMetafile
));
891 retval
= OutOfMemory
;
895 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
896 (LPVOID
*) &((*metafile
)->image
.picture
)) != S_OK
){
901 (*metafile
)->image
.type
= ImageTypeMetafile
;
902 (*metafile
)->bounds
.X
= ((REAL
) placeable
->BoundingBox
.Left
) / ((REAL
) placeable
->Inch
);
903 (*metafile
)->bounds
.Y
= ((REAL
) placeable
->BoundingBox
.Right
) / ((REAL
) placeable
->Inch
);
904 (*metafile
)->bounds
.Width
= ((REAL
) (placeable
->BoundingBox
.Right
905 - placeable
->BoundingBox
.Left
)) / ((REAL
) placeable
->Inch
);
906 (*metafile
)->bounds
.Height
= ((REAL
) (placeable
->BoundingBox
.Bottom
907 - placeable
->BoundingBox
.Top
)) / ((REAL
) placeable
->Inch
);
908 (*metafile
)->unit
= UnitInch
;
911 DeleteMetaFile(hwmf
);
916 IStream_Release(stream
);
921 GpStatus WINGDIPAPI
GdipDeleteGraphics(GpGraphics
*graphics
)
923 if(!graphics
) return InvalidParameter
;
925 ReleaseDC(graphics
->hwnd
, graphics
->hdc
);
927 GdipDeleteMatrix(graphics
->worldtrans
);
928 HeapFree(GetProcessHeap(), 0, graphics
);
933 GpStatus WINGDIPAPI
GdipDrawArc(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
934 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
936 INT save_state
, num_pts
;
937 GpPointF points
[MAX_ARC_PTS
];
940 if(!graphics
|| !pen
)
941 return InvalidParameter
;
943 num_pts
= arc2polybezier(points
, x
, y
, width
, height
, startAngle
, sweepAngle
);
945 save_state
= prepare_dc(graphics
, pen
);
947 retval
= draw_polybezier(graphics
, pen
, points
, num_pts
, TRUE
);
949 restore_dc(graphics
, save_state
);
954 GpStatus WINGDIPAPI
GdipDrawBezier(GpGraphics
*graphics
, GpPen
*pen
, REAL x1
,
955 REAL y1
, REAL x2
, REAL y2
, REAL x3
, REAL y3
, REAL x4
, REAL y4
)
961 if(!graphics
|| !pen
)
962 return InvalidParameter
;
973 save_state
= prepare_dc(graphics
, pen
);
975 retval
= draw_polybezier(graphics
, pen
, pt
, 4, TRUE
);
977 restore_dc(graphics
, save_state
);
982 /* Approximates cardinal spline with Bezier curves. */
983 GpStatus WINGDIPAPI
GdipDrawCurve2(GpGraphics
*graphics
, GpPen
*pen
,
984 GDIPCONST GpPointF
*points
, INT count
, REAL tension
)
986 /* PolyBezier expects count*3-2 points. */
987 INT i
, len_pt
= count
*3-2, save_state
;
992 if(!graphics
|| !pen
)
993 return InvalidParameter
;
995 pt
= GdipAlloc(len_pt
* sizeof(GpPointF
));
996 tension
= tension
* TENSION_CONST
;
998 calc_curve_bezier_endp(points
[0].X
, points
[0].Y
, points
[1].X
, points
[1].Y
,
1001 pt
[0].X
= points
[0].X
;
1002 pt
[0].Y
= points
[0].Y
;
1006 for(i
= 0; i
< count
-2; i
++){
1007 calc_curve_bezier(&(points
[i
]), tension
, &x1
, &y1
, &x2
, &y2
);
1011 pt
[3*i
+3].X
= points
[i
+1].X
;
1012 pt
[3*i
+3].Y
= points
[i
+1].Y
;
1017 calc_curve_bezier_endp(points
[count
-1].X
, points
[count
-1].Y
,
1018 points
[count
-2].X
, points
[count
-2].Y
, tension
, &x1
, &y1
);
1020 pt
[len_pt
-2].X
= x1
;
1021 pt
[len_pt
-2].Y
= y1
;
1022 pt
[len_pt
-1].X
= points
[count
-1].X
;
1023 pt
[len_pt
-1].Y
= points
[count
-1].Y
;
1025 save_state
= prepare_dc(graphics
, pen
);
1027 retval
= draw_polybezier(graphics
, pen
, pt
, len_pt
, TRUE
);
1030 restore_dc(graphics
, save_state
);
1035 /* FIXME: partially implemented */
1036 GpStatus WINGDIPAPI
GdipDrawImagePointsRect(GpGraphics
*graphics
, GpImage
*image
,
1037 GDIPCONST GpPointF
*points
, INT count
, REAL srcx
, REAL srcy
, REAL srcwidth
,
1038 REAL srcheight
, GpUnit srcUnit
, GDIPCONST GpImageAttributes
* imageAttributes
,
1039 DrawImageAbort callback
, VOID
* callbackData
)
1045 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics
, image
, points
, count
,
1046 srcx
, srcy
, srcwidth
, srcheight
, srcUnit
, imageAttributes
, callback
,
1049 if(!graphics
|| !image
|| !points
|| !imageAttributes
|| count
!= 3)
1050 return InvalidParameter
;
1052 if(srcUnit
== UnitInch
)
1053 dx
= dy
= (REAL
) INCH_HIMETRIC
;
1054 else if(srcUnit
== UnitPixel
){
1055 dx
= ((REAL
) INCH_HIMETRIC
) /
1056 ((REAL
) GetDeviceCaps(graphics
->hdc
, LOGPIXELSX
));
1057 dy
= ((REAL
) INCH_HIMETRIC
) /
1058 ((REAL
) GetDeviceCaps(graphics
->hdc
, LOGPIXELSY
));
1061 return NotImplemented
;
1063 memcpy(ptf
, points
, 3 * sizeof(GpPointF
));
1064 transform_and_round_points(graphics
, pti
, ptf
, 3);
1066 /* IPicture renders bitmaps with the y-axis reversed
1067 * FIXME: flipping for unknown image type might not be correct. */
1068 if(image
->type
!= ImageTypeMetafile
){
1071 pti
[0].y
= pti
[2].y
;
1075 if(IPicture_Render(image
->picture
, graphics
->hdc
,
1076 pti
[0].x
, pti
[0].y
, pti
[1].x
- pti
[0].x
, pti
[2].y
- pti
[0].y
,
1077 srcx
* dx
, srcy
* dy
,
1078 srcwidth
* dx
, srcheight
* dy
,
1081 callback(callbackData
);
1082 return GenericError
;
1088 GpStatus WINGDIPAPI
GdipDrawLineI(GpGraphics
*graphics
, GpPen
*pen
, INT x1
,
1089 INT y1
, INT x2
, INT y2
)
1095 if(!pen
|| !graphics
)
1096 return InvalidParameter
;
1103 save_state
= prepare_dc(graphics
, pen
);
1105 retval
= draw_polyline(graphics
, pen
, pt
, 2, TRUE
);
1107 restore_dc(graphics
, save_state
);
1112 GpStatus WINGDIPAPI
GdipDrawLines(GpGraphics
*graphics
, GpPen
*pen
, GDIPCONST
1113 GpPointF
*points
, INT count
)
1118 if(!pen
|| !graphics
|| (count
< 2))
1119 return InvalidParameter
;
1121 save_state
= prepare_dc(graphics
, pen
);
1123 retval
= draw_polyline(graphics
, pen
, points
, count
, TRUE
);
1125 restore_dc(graphics
, save_state
);
1130 GpStatus WINGDIPAPI
GdipDrawPath(GpGraphics
*graphics
, GpPen
*pen
, GpPath
*path
)
1135 if(!pen
|| !graphics
)
1136 return InvalidParameter
;
1138 save_state
= prepare_dc(graphics
, pen
);
1140 retval
= draw_poly(graphics
, pen
, path
->pathdata
.Points
,
1141 path
->pathdata
.Types
, path
->pathdata
.Count
, TRUE
);
1143 restore_dc(graphics
, save_state
);
1148 GpStatus WINGDIPAPI
GdipDrawPie(GpGraphics
*graphics
, GpPen
*pen
, REAL x
,
1149 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
1153 if(!graphics
|| !pen
)
1154 return InvalidParameter
;
1156 save_state
= prepare_dc(graphics
, pen
);
1157 SelectObject(graphics
->hdc
, GetStockObject(NULL_BRUSH
));
1159 draw_pie(graphics
, x
, y
, width
, height
, startAngle
, sweepAngle
);
1161 restore_dc(graphics
, save_state
);
1166 GpStatus WINGDIPAPI
GdipDrawRectangleI(GpGraphics
*graphics
, GpPen
*pen
, INT x
,
1167 INT y
, INT width
, INT height
)
1171 if(!pen
|| !graphics
)
1172 return InvalidParameter
;
1174 save_state
= prepare_dc(graphics
, pen
);
1175 SelectObject(graphics
->hdc
, GetStockObject(NULL_BRUSH
));
1177 Rectangle(graphics
->hdc
, x
, y
, x
+ width
, y
+ height
);
1179 restore_dc(graphics
, save_state
);
1184 GpStatus WINGDIPAPI
GdipFillPath(GpGraphics
*graphics
, GpBrush
*brush
, GpPath
*path
)
1189 if(!brush
|| !graphics
|| !path
)
1190 return InvalidParameter
;
1192 save_state
= SaveDC(graphics
->hdc
);
1193 EndPath(graphics
->hdc
);
1194 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1195 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1198 BeginPath(graphics
->hdc
);
1199 retval
= draw_poly(graphics
, NULL
, path
->pathdata
.Points
,
1200 path
->pathdata
.Types
, path
->pathdata
.Count
, FALSE
);
1205 EndPath(graphics
->hdc
);
1206 FillPath(graphics
->hdc
);
1211 RestoreDC(graphics
->hdc
, save_state
);
1216 GpStatus WINGDIPAPI
GdipFillPie(GpGraphics
*graphics
, GpBrush
*brush
, REAL x
,
1217 REAL y
, REAL width
, REAL height
, REAL startAngle
, REAL sweepAngle
)
1221 if(!graphics
|| !brush
)
1222 return InvalidParameter
;
1224 save_state
= SaveDC(graphics
->hdc
);
1225 EndPath(graphics
->hdc
);
1226 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1227 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1229 draw_pie(graphics
, x
, y
, width
, height
, startAngle
, sweepAngle
);
1231 RestoreDC(graphics
->hdc
, save_state
);
1236 GpStatus WINGDIPAPI
GdipFillPolygonI(GpGraphics
*graphics
, GpBrush
*brush
,
1237 GDIPCONST GpPoint
*points
, INT count
, GpFillMode fillMode
)
1240 GpPointF
*ptf
= NULL
;
1242 GpStatus retval
= Ok
;
1244 if(!graphics
|| !brush
|| !points
|| !count
)
1245 return InvalidParameter
;
1247 ptf
= GdipAlloc(count
* sizeof(GpPointF
));
1248 pti
= GdipAlloc(count
* sizeof(POINT
));
1250 retval
= OutOfMemory
;
1254 for(i
= 0; i
< count
; i
++){
1255 ptf
[i
].X
= (REAL
) points
[i
].X
;
1256 ptf
[i
].Y
= (REAL
) points
[i
].Y
;
1259 save_state
= SaveDC(graphics
->hdc
);
1260 EndPath(graphics
->hdc
);
1261 SelectObject(graphics
->hdc
, brush
->gdibrush
);
1262 SelectObject(graphics
->hdc
, GetStockObject(NULL_PEN
));
1263 SetPolyFillMode(graphics
->hdc
, (fillMode
== FillModeAlternate
? ALTERNATE
1266 transform_and_round_points(graphics
, pti
, ptf
, count
);
1267 Polygon(graphics
->hdc
, pti
, count
);
1269 RestoreDC(graphics
->hdc
, save_state
);
1278 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1279 GpStatus WINGDIPAPI
GdipGetCompositingQuality(GpGraphics
*graphics
,
1280 CompositingQuality
*quality
)
1282 if(!graphics
|| !quality
)
1283 return InvalidParameter
;
1285 *quality
= graphics
->compqual
;
1290 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1291 GpStatus WINGDIPAPI
GdipGetInterpolationMode(GpGraphics
*graphics
,
1292 InterpolationMode
*mode
)
1294 if(!graphics
|| !mode
)
1295 return InvalidParameter
;
1297 *mode
= graphics
->interpolation
;
1302 GpStatus WINGDIPAPI
GdipGetPageScale(GpGraphics
*graphics
, REAL
*scale
)
1304 if(!graphics
|| !scale
)
1305 return InvalidParameter
;
1307 *scale
= graphics
->scale
;
1312 GpStatus WINGDIPAPI
GdipGetPageUnit(GpGraphics
*graphics
, GpUnit
*unit
)
1314 if(!graphics
|| !unit
)
1315 return InvalidParameter
;
1317 *unit
= graphics
->unit
;
1322 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1323 GpStatus WINGDIPAPI
GdipGetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1326 if(!graphics
|| !mode
)
1327 return InvalidParameter
;
1329 *mode
= graphics
->pixeloffset
;
1334 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1335 GpStatus WINGDIPAPI
GdipGetSmoothingMode(GpGraphics
*graphics
, SmoothingMode
*mode
)
1337 if(!graphics
|| !mode
)
1338 return InvalidParameter
;
1340 *mode
= graphics
->smoothing
;
1345 GpStatus WINGDIPAPI
GdipGetWorldTransform(GpGraphics
*graphics
, GpMatrix
*matrix
)
1347 if(!graphics
|| !matrix
)
1348 return InvalidParameter
;
1350 memcpy(matrix
, graphics
->worldtrans
, sizeof(GpMatrix
));
1354 GpStatus WINGDIPAPI
GdipRestoreGraphics(GpGraphics
*graphics
, GraphicsState state
)
1359 return InvalidParameter
;
1362 FIXME("graphics state not implemented\n");
1364 return NotImplemented
;
1367 GpStatus WINGDIPAPI
GdipSaveGraphics(GpGraphics
*graphics
, GraphicsState
*state
)
1371 if(!graphics
|| !state
)
1372 return InvalidParameter
;
1375 FIXME("graphics state not implemented\n");
1377 return NotImplemented
;
1380 GpStatus WINGDIPAPI
GdipSetCompositingQuality(GpGraphics
*graphics
,
1381 CompositingQuality quality
)
1384 return InvalidParameter
;
1386 graphics
->compqual
= quality
;
1391 GpStatus WINGDIPAPI
GdipSetInterpolationMode(GpGraphics
*graphics
,
1392 InterpolationMode mode
)
1395 return InvalidParameter
;
1397 graphics
->interpolation
= mode
;
1402 GpStatus WINGDIPAPI
GdipSetPageScale(GpGraphics
*graphics
, REAL scale
)
1404 if(!graphics
|| (scale
<= 0.0))
1405 return InvalidParameter
;
1407 graphics
->scale
= scale
;
1412 GpStatus WINGDIPAPI
GdipSetPageUnit(GpGraphics
*graphics
, GpUnit unit
)
1414 if(!graphics
|| (unit
== UnitWorld
))
1415 return InvalidParameter
;
1417 graphics
->unit
= unit
;
1422 GpStatus WINGDIPAPI
GdipSetPixelOffsetMode(GpGraphics
*graphics
, PixelOffsetMode
1426 return InvalidParameter
;
1428 graphics
->pixeloffset
= mode
;
1433 GpStatus WINGDIPAPI
GdipSetSmoothingMode(GpGraphics
*graphics
, SmoothingMode mode
)
1436 return InvalidParameter
;
1438 graphics
->smoothing
= mode
;
1443 GpStatus WINGDIPAPI
GdipSetWorldTransform(GpGraphics
*graphics
, GpMatrix
*matrix
)
1445 if(!graphics
|| !matrix
)
1446 return InvalidParameter
;
1448 GdipDeleteMatrix(graphics
->worldtrans
);
1449 return GdipCloneMatrix(matrix
, &graphics
->worldtrans
);