2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "gdiplus_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32 /* looks-right constants */
33 #define TENSION_CONST (0.3)
34 #define ANCHOR_WIDTH (2.0)
35 #define MAX_ITERS (50)
37 /* Converts angle (in degrees) to x/y coordinates */
38 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
40 REAL radAngle, hypotenuse;
42 radAngle = deg2rad(angle);
43 hypotenuse = 50.0; /* arbitrary */
45 *x = x_0 + cos(radAngle) * hypotenuse;
46 *y = y_0 + sin(radAngle) * hypotenuse;
49 /* Converts from gdiplus path point type to gdi path point type. */
50 static BYTE convert_path_point_type(BYTE type)
54 switch(type & PathPointTypePathTypeMask){
55 case PathPointTypeBezier:
58 case PathPointTypeLine:
61 case PathPointTypeStart:
65 ERR("Bad point type\n");
69 if(type & PathPointTypeCloseSubpath)
70 ret |= PT_CLOSEFIGURE;
75 static REAL convert_unit(HDC hdc, GpUnit unit)
80 return (REAL) GetDeviceCaps(hdc, LOGPIXELSX);
82 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 72.0;
84 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 300.0;
86 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 25.4;
88 ERR("cannot convert UnitWorld\n");
97 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
101 INT save_state = SaveDC(graphics->hdc), i, numdashes;
103 DWORD dash_array[MAX_DASHLEN];
105 EndPath(graphics->hdc);
107 /* Get an estimate for the amount the pen width is affected by the world
108 * transform. (This is similar to what some of the wine drivers do.) */
113 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
114 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
115 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
117 width *= pen->width * convert_unit(graphics->hdc,
118 pen->unit == UnitWorld ? graphics->unit : pen->unit);
120 if(pen->dash == DashStyleCustom){
121 numdashes = min(pen->numdashes, MAX_DASHLEN);
123 TRACE("dashes are: ");
124 for(i = 0; i < numdashes; i++){
125 dash_array[i] = roundr(width * pen->dashes[i]);
126 TRACE("%d, ", dash_array[i]);
128 TRACE("\n and the pen style is %x\n", pen->style);
130 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
131 numdashes, dash_array);
134 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
136 SelectObject(graphics->hdc, gdipen);
141 static void restore_dc(GpGraphics *graphics, INT state)
143 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
144 RestoreDC(graphics->hdc, state);
147 /* This helper applies all the changes that the points listed in ptf need in
148 * order to be drawn on the device context. In the end, this should include at
150 * -scaling by page unit
151 * -applying world transformation
152 * -converting from float to int
153 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
154 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
155 * gdi to draw, and these functions would irreparably mess with line widths.
157 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
158 GpPointF *ptf, INT count)
164 unitscale = convert_unit(graphics->hdc, graphics->unit);
166 /* apply page scale */
167 if(graphics->unit != UnitDisplay)
168 unitscale *= graphics->scale;
170 GdipCloneMatrix(graphics->worldtrans, &matrix);
171 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
172 GdipTransformMatrixPoints(matrix, ptf, count);
173 GdipDeleteMatrix(matrix);
175 for(i = 0; i < count; i++){
176 pti[i].x = roundr(ptf[i].X);
177 pti[i].y = roundr(ptf[i].Y);
181 /* GdipDrawPie/GdipFillPie helper function */
182 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
183 REAL height, REAL startAngle, REAL sweepAngle)
190 ptf[1].X = x + width;
191 ptf[1].Y = y + height;
193 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
194 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
196 transform_and_round_points(graphics, pti, ptf, 4);
198 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
199 pti[2].y, pti[3].x, pti[3].y);
202 /* GdipDrawCurve helper function.
203 * Calculates Bezier points from cardinal spline points. */
204 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
205 REAL *y1, REAL *x2, REAL *y2)
209 /* calculate tangent */
210 xdiff = pts[2].X - pts[0].X;
211 ydiff = pts[2].Y - pts[0].Y;
213 /* apply tangent to get control points */
214 *x1 = pts[1].X - tension * xdiff;
215 *y1 = pts[1].Y - tension * ydiff;
216 *x2 = pts[1].X + tension * xdiff;
217 *y2 = pts[1].Y + tension * ydiff;
220 /* GdipDrawCurve helper function.
221 * Calculates Bezier points from cardinal spline endpoints. */
222 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
223 REAL tension, REAL *x, REAL *y)
225 /* tangent at endpoints is the line from the endpoint to the adjacent point */
226 *x = roundr(tension * (xadj - xend) + xend);
227 *y = roundr(tension * (yadj - yend) + yend);
230 /* Draws the linecap the specified color and size on the hdc. The linecap is in
231 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
232 * should not be called on an hdc that has a path you care about. */
233 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
234 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
236 HGDIOBJ oldbrush = NULL, oldpen = NULL;
237 GpMatrix *matrix = NULL;
240 PointF ptf[4], *custptf = NULL;
241 POINT pt[4], *custpt = NULL;
243 REAL theta, dsmall, dbig, dx, dy = 0.0;
248 if((x1 == x2) && (y1 == y2))
251 theta = gdiplus_atan2(y2 - y1, x2 - x1);
253 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
255 brush = CreateSolidBrush(color);
256 lb.lbStyle = BS_SOLID;
259 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
260 PS_JOIN_MITER, 1, &lb, 0,
262 oldbrush = SelectObject(graphics->hdc, brush);
263 oldpen = SelectObject(graphics->hdc, pen);
270 case LineCapSquareAnchor:
271 case LineCapDiamondAnchor:
272 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
273 if(cap == LineCapDiamondAnchor){
274 dsmall = cos(theta + M_PI_2) * size;
275 dbig = sin(theta + M_PI_2) * size;
278 dsmall = cos(theta + M_PI_4) * size;
279 dbig = sin(theta + M_PI_4) * size;
282 ptf[0].X = x2 - dsmall;
283 ptf[1].X = x2 + dbig;
285 ptf[0].Y = y2 - dbig;
286 ptf[3].Y = y2 + dsmall;
288 ptf[1].Y = y2 - dsmall;
289 ptf[2].Y = y2 + dbig;
291 ptf[3].X = x2 - dbig;
292 ptf[2].X = x2 + dsmall;
294 transform_and_round_points(graphics, pt, ptf, 4);
295 Polygon(graphics->hdc, pt, 4);
298 case LineCapArrowAnchor:
299 size = size * 4.0 / sqrt(3.0);
301 dx = cos(M_PI / 6.0 + theta) * size;
302 dy = sin(M_PI / 6.0 + theta) * size;
307 dx = cos(- M_PI / 6.0 + theta) * size;
308 dy = sin(- M_PI / 6.0 + theta) * size;
316 transform_and_round_points(graphics, pt, ptf, 3);
317 Polygon(graphics->hdc, pt, 3);
320 case LineCapRoundAnchor:
321 dx = dy = ANCHOR_WIDTH * size / 2.0;
328 transform_and_round_points(graphics, pt, ptf, 2);
329 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
332 case LineCapTriangle:
334 dx = cos(M_PI_2 + theta) * size;
335 dy = sin(M_PI_2 + theta) * size;
342 dx = cos(theta) * size;
343 dy = sin(theta) * size;
348 transform_and_round_points(graphics, pt, ptf, 3);
349 Polygon(graphics->hdc, pt, 3);
353 dx = dy = size / 2.0;
360 dx = -cos(M_PI_2 + theta) * size;
361 dy = -sin(M_PI_2 + theta) * size;
368 transform_and_round_points(graphics, pt, ptf, 4);
369 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
370 pt[2].y, pt[3].x, pt[3].y);
377 count = custom->pathdata.Count;
378 custptf = GdipAlloc(count * sizeof(PointF));
379 custpt = GdipAlloc(count * sizeof(POINT));
380 tp = GdipAlloc(count);
382 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
385 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
387 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
388 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
390 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
391 GdipTransformMatrixPoints(matrix, custptf, count);
393 transform_and_round_points(graphics, custpt, custptf, count);
395 for(i = 0; i < count; i++)
396 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
399 BeginPath(graphics->hdc);
400 PolyDraw(graphics->hdc, custpt, tp, count);
401 EndPath(graphics->hdc);
402 StrokeAndFillPath(graphics->hdc);
405 PolyDraw(graphics->hdc, custpt, tp, count);
411 GdipDeleteMatrix(matrix);
418 SelectObject(graphics->hdc, oldbrush);
419 SelectObject(graphics->hdc, oldpen);
425 /* Shortens the line by the given percent by changing x2, y2.
426 * If percent is > 1.0 then the line will change direction.
427 * If percent is negative it can lengthen the line. */
428 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
430 REAL dist, theta, dx, dy;
432 if((y1 == *y2) && (x1 == *x2))
435 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
436 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
437 dx = cos(theta) * dist;
438 dy = sin(theta) * dist;
444 /* Shortens the line by the given amount by changing x2, y2.
445 * If the amount is greater than the distance, the line will become length 0.
446 * If the amount is negative, it can lengthen the line. */
447 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
449 REAL dx, dy, percent;
453 if(dx == 0 && dy == 0)
456 percent = amt / sqrt(dx * dx + dy * dy);
463 shorten_line_percent(x1, y1, x2, y2, percent);
466 /* Draws lines between the given points, and if caps is true then draws an endcap
467 * at the end of the last line. FIXME: Startcaps not implemented. */
468 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
469 GDIPCONST GpPointF * pt, INT count, BOOL caps)
472 GpPointF *ptcopy = NULL;
473 GpStatus status = GenericError;
478 pti = GdipAlloc(count * sizeof(POINT));
479 ptcopy = GdipAlloc(count * sizeof(GpPointF));
482 status = OutOfMemory;
486 memcpy(ptcopy, pt, count * sizeof(GpPointF));
489 if(pen->endcap == LineCapArrowAnchor)
490 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
491 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
492 else if((pen->endcap == LineCapCustom) && pen->customend)
493 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
494 &ptcopy[count-1].X, &ptcopy[count-1].Y,
495 pen->customend->inset * pen->width);
497 if(pen->startcap == LineCapArrowAnchor)
498 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
499 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
500 else if((pen->startcap == LineCapCustom) && pen->customstart)
501 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
502 &ptcopy[0].X, &ptcopy[0].Y,
503 pen->customend->inset * pen->width);
505 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
506 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
507 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
508 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
511 transform_and_round_points(graphics, pti, ptcopy, count);
513 Polyline(graphics->hdc, pti, count);
522 /* Conducts a linear search to find the bezier points that will back off
523 * the endpoint of the curve by a distance of amt. Linear search works
524 * better than binary in this case because there are multiple solutions,
525 * and binary searches often find a bad one. I don't think this is what
526 * Windows does but short of rendering the bezier without GDI's help it's
527 * the best we can do. If rev then work from the start of the passed points
528 * instead of the end. */
529 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
532 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
533 INT i, first = 0, second = 1, third = 2, fourth = 3;
542 origx = pt[fourth].X;
543 origy = pt[fourth].Y;
544 memcpy(origpt, pt, sizeof(GpPointF) * 4);
546 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
547 /* reset bezier points to original values */
548 memcpy(pt, origpt, sizeof(GpPointF) * 4);
549 /* Perform magic on bezier points. Order is important here.*/
550 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
551 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
552 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
553 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
554 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
555 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
557 dx = pt[fourth].X - origx;
558 dy = pt[fourth].Y - origy;
560 diff = sqrt(dx * dx + dy * dy);
561 percent += 0.0005 * amt;
565 /* Draws bezier curves between given points, and if caps is true then draws an
566 * endcap at the end of the last line. FIXME: Startcaps not implemented. */
567 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
568 GDIPCONST GpPointF * pt, INT count, BOOL caps)
573 GpStatus status = GenericError;
578 pti = GdipAlloc(count * sizeof(POINT));
579 ptcopy = GdipAlloc(count * sizeof(GpPointF));
582 status = OutOfMemory;
586 memcpy(ptcopy, pt, count * sizeof(GpPointF));
589 if(pen->endcap == LineCapArrowAnchor)
590 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
591 /* FIXME The following is seemingly correct only for baseinset < 0 or
592 * baseinset > ~3. With smaller baseinsets, windows actually
593 * lengthens the bezier line instead of shortening it. */
594 else if((pen->endcap == LineCapCustom) && pen->customend){
597 shorten_line_amt(pt[count - 2].X, pt[count - 2].Y, &x, &y,
598 pen->width * pen->customend->inset);
599 MoveToEx(graphics->hdc, roundr(pt[count - 1].X), roundr(pt[count - 1].Y), &curpos);
600 LineTo(graphics->hdc, roundr(x), roundr(y));
601 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
604 if(pen->startcap == LineCapArrowAnchor)
605 shorten_bezier_amt(ptcopy, pen->width, TRUE);
606 else if((pen->startcap == LineCapCustom) && pen->customstart){
609 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y, &x, &y,
610 pen->width * pen->customend->inset);
611 MoveToEx(graphics->hdc, roundr(pt[0].X), roundr(pt[0].Y), &curpos);
612 LineTo(graphics->hdc, roundr(x), roundr(y));
613 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
616 /* the direction of the line cap is parallel to the direction at the
617 * end of the bezier (which, if it has been shortened, is not the same
618 * as the direction from pt[count-2] to pt[count-1]) */
619 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
620 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
621 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
622 pt[count - 1].X, pt[count - 1].Y);
624 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
625 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
626 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
629 transform_and_round_points(graphics, pti, ptcopy, count);
631 PolyBezier(graphics->hdc, pti, count);
642 /* Draws a combination of bezier curves and lines between points. */
643 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
644 GDIPCONST BYTE * types, INT count, BOOL caps)
646 POINT *pti = GdipAlloc(count * sizeof(POINT)), curpos;
647 BYTE *tp = GdipAlloc(count);
648 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
649 REAL x = pt[count - 1].X, y = pt[count - 1].Y;
651 GpStatus status = GenericError;
657 if(!pti || !tp || !ptcopy){
658 status = OutOfMemory;
662 for(i = 1; i < count; i++){
663 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
664 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
665 || !(types[i + 1] & PathPointTypeBezier)){
666 ERR("Bad bezier points\n");
673 memcpy(ptcopy, pt, count * sizeof(GpPointF));
675 /* If we are drawing caps, go through the points and adjust them accordingly,
676 * and draw the caps. */
678 switch(types[count - 1] & PathPointTypePathTypeMask){
679 case PathPointTypeBezier:
680 if(pen->endcap == LineCapArrowAnchor)
681 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
682 else if((pen->endcap == LineCapCustom) && pen->customend){
685 shorten_line_amt(pt[count - 2].X, pt[count - 2].Y, &x, &y,
686 pen->width * pen->customend->inset);
687 MoveToEx(graphics->hdc, roundr(pt[count - 1].X),
688 roundr(pt[count - 1].Y), &curpos);
689 LineTo(graphics->hdc, roundr(x), roundr(y));
690 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
693 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
694 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
695 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
696 pt[count - 1].X, pt[count - 1].Y);
699 case PathPointTypeLine:
700 if(pen->endcap == LineCapArrowAnchor)
701 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
702 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
704 else if((pen->endcap == LineCapCustom) && pen->customend)
705 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
706 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
707 pen->customend->inset * pen->width);
709 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
710 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
715 ERR("Bad path last point\n");
719 /* Find start of points */
720 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
721 == PathPointTypeStart); j++);
723 switch(types[j] & PathPointTypePathTypeMask){
724 case PathPointTypeBezier:
725 if(pen->startcap == LineCapArrowAnchor)
726 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
727 else if((pen->startcap == LineCapCustom) && pen->customstart){
730 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y, &x, &y,
731 pen->width * pen->customstart->inset);
732 MoveToEx(graphics->hdc, roundr(pt[j - 1].X), roundr(pt[j - 1].Y), &curpos);
733 LineTo(graphics->hdc, roundr(x), roundr(y));
734 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
737 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
738 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
739 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
740 pt[j - 1].X, pt[j - 1].Y);
743 case PathPointTypeLine:
744 if(pen->startcap == LineCapArrowAnchor)
745 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
746 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
748 else if((pen->startcap == LineCapCustom) && pen->customstart)
749 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
750 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
751 pen->customstart->inset * pen->width);
753 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customstart,
754 pt[j].X, pt[j].Y, pt[j - 1].X,
759 ERR("Bad path points\n");
764 transform_and_round_points(graphics, pti, ptcopy, count);
766 for(i = 0; i < count; i++){
767 tp[i] = convert_path_point_type(types[i]);
770 PolyDraw(graphics->hdc, pti, tp, count);
782 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
790 return InvalidParameter;
792 *graphics = GdipAlloc(sizeof(GpGraphics));
793 if(!*graphics) return OutOfMemory;
795 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
800 (*graphics)->hdc = hdc;
801 (*graphics)->hwnd = NULL;
802 (*graphics)->smoothing = SmoothingModeDefault;
803 (*graphics)->compqual = CompositingQualityDefault;
804 (*graphics)->interpolation = InterpolationModeDefault;
805 (*graphics)->pixeloffset = PixelOffsetModeDefault;
806 (*graphics)->unit = UnitDisplay;
807 (*graphics)->scale = 1.0;
812 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
816 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
819 (*graphics)->hwnd = hwnd;
824 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
825 GpMetafile **metafile)
829 if(!hemf || !metafile)
830 return InvalidParameter;
833 FIXME("not implemented\n");
835 return NotImplemented;
838 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
839 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
843 if(!hwmf || !metafile || !placeable)
844 return InvalidParameter;
847 FIXME("not implemented\n");
849 return NotImplemented;
852 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
854 if(!graphics) return InvalidParameter;
856 ReleaseDC(graphics->hwnd, graphics->hdc);
858 GdipDeleteMatrix(graphics->worldtrans);
859 HeapFree(GetProcessHeap(), 0, graphics);
864 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
865 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
867 INT save_state, num_pts;
868 GpPointF points[MAX_ARC_PTS];
871 if(!graphics || !pen)
872 return InvalidParameter;
874 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
876 save_state = prepare_dc(graphics, pen);
878 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
880 restore_dc(graphics, save_state);
885 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
886 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
892 if(!graphics || !pen)
893 return InvalidParameter;
904 save_state = prepare_dc(graphics, pen);
906 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
908 restore_dc(graphics, save_state);
913 /* Approximates cardinal spline with Bezier curves. */
914 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
915 GDIPCONST GpPointF *points, INT count, REAL tension)
917 /* PolyBezier expects count*3-2 points. */
918 INT i, len_pt = count*3-2, save_state;
923 if(!graphics || !pen)
924 return InvalidParameter;
926 pt = GdipAlloc(len_pt * sizeof(GpPointF));
927 tension = tension * TENSION_CONST;
929 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
932 pt[0].X = points[0].X;
933 pt[0].Y = points[0].Y;
937 for(i = 0; i < count-2; i++){
938 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
942 pt[3*i+3].X = points[i+1].X;
943 pt[3*i+3].Y = points[i+1].Y;
948 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
949 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
953 pt[len_pt-1].X = points[count-1].X;
954 pt[len_pt-1].Y = points[count-1].Y;
956 save_state = prepare_dc(graphics, pen);
958 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
961 restore_dc(graphics, save_state);
966 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
967 INT y1, INT x2, INT y2)
973 if(!pen || !graphics)
974 return InvalidParameter;
981 save_state = prepare_dc(graphics, pen);
983 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
985 restore_dc(graphics, save_state);
990 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
991 GpPointF *points, INT count)
996 if(!pen || !graphics || (count < 2))
997 return InvalidParameter;
999 save_state = prepare_dc(graphics, pen);
1001 retval = draw_polyline(graphics, pen, points, count, TRUE);
1003 restore_dc(graphics, save_state);
1008 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1013 if(!pen || !graphics)
1014 return InvalidParameter;
1016 save_state = prepare_dc(graphics, pen);
1018 retval = draw_poly(graphics, pen, path->pathdata.Points,
1019 path->pathdata.Types, path->pathdata.Count, TRUE);
1021 restore_dc(graphics, save_state);
1026 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1027 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1031 if(!graphics || !pen)
1032 return InvalidParameter;
1034 save_state = prepare_dc(graphics, pen);
1035 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1037 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1039 restore_dc(graphics, save_state);
1044 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1045 INT y, INT width, INT height)
1049 if(!pen || !graphics)
1050 return InvalidParameter;
1052 save_state = prepare_dc(graphics, pen);
1053 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1055 Rectangle(graphics->hdc, x, y, x + width, y + height);
1057 restore_dc(graphics, save_state);
1062 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1067 if(!brush || !graphics || !path)
1068 return InvalidParameter;
1070 save_state = SaveDC(graphics->hdc);
1071 EndPath(graphics->hdc);
1072 SelectObject(graphics->hdc, brush->gdibrush);
1073 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1076 BeginPath(graphics->hdc);
1077 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1078 path->pathdata.Types, path->pathdata.Count, FALSE);
1083 EndPath(graphics->hdc);
1084 FillPath(graphics->hdc);
1089 RestoreDC(graphics->hdc, save_state);
1094 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1095 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1099 if(!graphics || !brush)
1100 return InvalidParameter;
1102 save_state = SaveDC(graphics->hdc);
1103 EndPath(graphics->hdc);
1104 SelectObject(graphics->hdc, brush->gdibrush);
1105 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1107 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1109 RestoreDC(graphics->hdc, save_state);
1114 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1115 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1118 GpPointF *ptf = NULL;
1120 GpStatus retval = Ok;
1122 if(!graphics || !brush || !points || !count)
1123 return InvalidParameter;
1125 ptf = GdipAlloc(count * sizeof(GpPointF));
1126 pti = GdipAlloc(count * sizeof(POINT));
1128 retval = OutOfMemory;
1132 for(i = 0; i < count; i ++){
1133 ptf[i].X = (REAL) points[i].X;
1134 ptf[i].Y = (REAL) points[i].Y;
1137 save_state = SaveDC(graphics->hdc);
1138 EndPath(graphics->hdc);
1139 SelectObject(graphics->hdc, brush->gdibrush);
1140 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1141 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1144 transform_and_round_points(graphics, pti, ptf, count);
1145 Polygon(graphics->hdc, pti, count);
1147 RestoreDC(graphics->hdc, save_state);
1156 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1157 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1158 CompositingQuality *quality)
1160 if(!graphics || !quality)
1161 return InvalidParameter;
1163 *quality = graphics->compqual;
1168 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1169 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1170 InterpolationMode *mode)
1172 if(!graphics || !mode)
1173 return InvalidParameter;
1175 *mode = graphics->interpolation;
1180 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1182 if(!graphics || !scale)
1183 return InvalidParameter;
1185 *scale = graphics->scale;
1190 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1192 if(!graphics || !unit)
1193 return InvalidParameter;
1195 *unit = graphics->unit;
1200 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1201 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1204 if(!graphics || !mode)
1205 return InvalidParameter;
1207 *mode = graphics->pixeloffset;
1212 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1213 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1215 if(!graphics || !mode)
1216 return InvalidParameter;
1218 *mode = graphics->smoothing;
1223 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1225 if(!graphics || !matrix)
1226 return InvalidParameter;
1228 memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1232 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1237 return InvalidParameter;
1240 FIXME("graphics state not implemented\n");
1242 return NotImplemented;
1245 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1249 if(!graphics || !state)
1250 return InvalidParameter;
1253 FIXME("graphics state not implemented\n");
1255 return NotImplemented;
1258 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1259 CompositingQuality quality)
1262 return InvalidParameter;
1264 graphics->compqual = quality;
1269 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1270 InterpolationMode mode)
1273 return InvalidParameter;
1275 graphics->interpolation = mode;
1280 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1282 if(!graphics || (scale <= 0.0))
1283 return InvalidParameter;
1285 graphics->scale = scale;
1290 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1292 if(!graphics || (unit == UnitWorld))
1293 return InvalidParameter;
1295 graphics->unit = unit;
1300 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1304 return InvalidParameter;
1306 graphics->pixeloffset = mode;
1311 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1314 return InvalidParameter;
1316 graphics->smoothing = mode;
1321 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1323 if(!graphics || !matrix)
1324 return InvalidParameter;
1326 GdipDeleteMatrix(graphics->worldtrans);
1327 return GdipCloneMatrix(matrix, &graphics->worldtrans);