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 /* GdipDrawPie/GdipFillPie helper function */
76 static GpStatus draw_pie(GpGraphics *graphics, HBRUSH gdibrush, HPEN gdipen,
77 REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
80 REAL x_0, y_0, x_1, y_1, x_2, y_2;
83 return InvalidParameter;
85 save_state = SaveDC(graphics->hdc);
86 EndPath(graphics->hdc);
87 SelectObject(graphics->hdc, gdipen);
88 SelectObject(graphics->hdc, gdibrush);
90 x_0 = x + (width/2.0);
91 y_0 = y + (height/2.0);
93 deg2xy(startAngle+sweepAngle, x_0, y_0, &x_1, &y_1);
94 deg2xy(startAngle, x_0, y_0, &x_2, &y_2);
96 Pie(graphics->hdc, roundr(x), roundr(y), roundr(x+width), roundr(y+height),
97 roundr(x_1), roundr(y_1), roundr(x_2), roundr(y_2));
99 RestoreDC(graphics->hdc, save_state);
104 /* GdipDrawCurve helper function.
105 * Calculates Bezier points from cardinal spline points. */
106 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
107 REAL *y1, REAL *x2, REAL *y2)
111 /* calculate tangent */
112 xdiff = pts[2].X - pts[0].X;
113 ydiff = pts[2].Y - pts[0].Y;
115 /* apply tangent to get control points */
116 *x1 = pts[1].X - tension * xdiff;
117 *y1 = pts[1].Y - tension * ydiff;
118 *x2 = pts[1].X + tension * xdiff;
119 *y2 = pts[1].Y + tension * ydiff;
122 /* GdipDrawCurve helper function.
123 * Calculates Bezier points from cardinal spline endpoints. */
124 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
125 REAL tension, REAL *x, REAL *y)
127 /* tangent at endpoints is the line from the endpoint to the adjacent point */
128 *x = roundr(tension * (xadj - xend) + xend);
129 *y = roundr(tension * (yadj - yend) + yend);
132 /* Draws the linecap the specified color and size on the hdc. The linecap is in
133 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
134 * should not be called on an hdc that has a path you care about. */
135 static void draw_cap(HDC hdc, COLORREF color, GpLineCap cap, REAL size,
136 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
138 HGDIOBJ oldbrush, oldpen;
139 GpMatrix *matrix = NULL;
142 PointF *custptf = NULL;
143 POINT pt[4], *custpt = NULL;
145 REAL theta, dsmall, dbig, dx, dy;
149 if((x1 == x2) && (y1 == y2))
152 theta = gdiplus_atan2(y2 - y1, x2 - x1);
154 brush = CreateSolidBrush(color);
155 lb.lbStyle = BS_SOLID;
158 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT,
159 ((cap == LineCapCustom) && custom && (custom->fill)) ? size : 1,
161 oldbrush = SelectObject(hdc, brush);
162 oldpen = SelectObject(hdc, pen);
168 case LineCapSquareAnchor:
169 case LineCapDiamondAnchor:
170 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
171 if(cap == LineCapDiamondAnchor){
172 dsmall = cos(theta + M_PI_2) * size;
173 dbig = sin(theta + M_PI_2) * size;
176 dsmall = cos(theta + M_PI_4) * size;
177 dbig = sin(theta + M_PI_4) * size;
180 /* calculating the latter points from the earlier points makes them
181 * look a little better because of rounding issues */
182 pt[0].x = roundr(x2 - dsmall);
183 pt[1].x = roundr(((REAL)pt[0].x) + dbig + dsmall);
185 pt[0].y = roundr(y2 - dbig);
186 pt[3].y = roundr(((REAL)pt[0].y) + dsmall + dbig);
188 pt[1].y = roundr(y2 - dsmall);
189 pt[2].y = roundr(dbig + dsmall + ((REAL)pt[1].y));
191 pt[3].x = roundr(x2 - dbig);
192 pt[2].x = roundr(((REAL)pt[3].x) + dsmall + dbig);
197 case LineCapArrowAnchor:
198 size = size * 4.0 / sqrt(3.0);
200 dx = cos(M_PI / 6.0 + theta) * size;
201 dy = sin(M_PI / 6.0 + theta) * size;
203 pt[0].x = roundr(x2 - dx);
204 pt[0].y = roundr(y2 - dy);
206 dx = cos(- M_PI / 6.0 + theta) * size;
207 dy = sin(- M_PI / 6.0 + theta) * size;
209 pt[1].x = roundr(x2 - dx);
210 pt[1].y = roundr(y2 - dy);
212 pt[2].x = roundr(x2);
213 pt[2].y = roundr(y2);
218 case LineCapRoundAnchor:
219 dx = dy = ANCHOR_WIDTH * size / 2.0;
221 x2 = (REAL) roundr(x2 - dx);
222 y2 = (REAL) roundr(y2 - dy);
224 Ellipse(hdc, (INT) x2, (INT) y2, roundr(x2 + 2.0 * dx),
225 roundr(y2 + 2.0 * dy));
227 case LineCapTriangle:
229 dx = cos(M_PI_2 + theta) * size;
230 dy = sin(M_PI_2 + theta) * size;
232 /* Using roundr here can make the triangle float off the end of the
234 pt[0].x = ((x2 - x1) >= 0 ? floorf(x2 - dx) : ceilf(x2 - dx));
235 pt[0].y = ((y2 - y1) >= 0 ? floorf(y2 - dy) : ceilf(y2 - dy));
236 pt[1].x = roundr(pt[0].x + 2.0 * dx);
237 pt[1].y = roundr(pt[0].y + 2.0 * dy);
239 dx = cos(theta) * size;
240 dy = sin(theta) * size;
242 pt[2].x = roundr(x2 + dx);
243 pt[2].y = roundr(y2 + dy);
249 dx = -cos(M_PI_2 + theta) * size;
250 dy = -sin(M_PI_2 + theta) * size;
252 pt[0].x = ((x2 - x1) >= 0 ? floorf(x2 - dx) : ceilf(x2 - dx));
253 pt[0].y = ((y2 - y1) >= 0 ? floorf(y2 - dy) : ceilf(y2 - dy));
254 pt[1].x = roundr(pt[0].x + 2.0 * dx);
255 pt[1].y = roundr(pt[0].y + 2.0 * dy);
257 dx = dy = size / 2.0;
259 x2 = (REAL) roundr(x2 - dx);
260 y2 = (REAL) roundr(y2 - dy);
262 Pie(hdc, (INT) x2, (INT) y2, roundr(x2 + 2.0 * dx),
263 roundr(y2 + 2.0 * dy), pt[0].x, pt[0].y, pt[1].x, pt[1].y);
269 count = custom->pathdata.Count;
270 custptf = GdipAlloc(count * sizeof(PointF));
271 custpt = GdipAlloc(count * sizeof(POINT));
272 tp = GdipAlloc(count);
274 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
277 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
279 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
280 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
282 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
283 GdipTransformMatrixPoints(matrix, custptf, count);
285 for(i = 0; i < count; i++){
286 custpt[i].x = roundr(custptf[i].X);
287 custpt[i].y = roundr(custptf[i].Y);
288 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
293 PolyDraw(hdc, custpt, tp, count);
295 StrokeAndFillPath(hdc);
298 PolyDraw(hdc, custpt, tp, count);
304 GdipDeleteMatrix(matrix);
310 SelectObject(hdc, oldbrush);
311 SelectObject(hdc, oldpen);
316 /* Shortens the line by the given percent by changing x2, y2.
317 * If percent is > 1.0 then the line will change direction.
318 * If percent is negative it can lengthen the line. */
319 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
321 REAL dist, theta, dx, dy;
323 if((y1 == *y2) && (x1 == *x2))
326 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
327 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
328 dx = cos(theta) * dist;
329 dy = sin(theta) * dist;
335 /* Shortens the line by the given amount by changing x2, y2.
336 * If the amount is greater than the distance, the line will become length 0.
337 * If the amount is negative, it can lengthen the line. */
338 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
340 REAL dx, dy, percent;
344 if(dx == 0 && dy == 0)
347 percent = amt / sqrt(dx * dx + dy * dy);
354 shorten_line_percent(x1, y1, x2, y2, percent);
357 /* Draws lines between the given points, and if caps is true then draws an endcap
358 * at the end of the last line. FIXME: Startcaps not implemented. */
359 static GpStatus draw_polyline(HDC hdc, GpPen *pen, GDIPCONST GpPointF * pt,
360 INT count, BOOL caps)
363 GpPointF *ptcopy = NULL;
365 GpStatus status = GenericError;
370 pti = GdipAlloc(count * sizeof(POINT));
371 ptcopy = GdipAlloc(count * sizeof(GpPointF));
374 status = OutOfMemory;
378 memcpy(ptcopy, pt, count * sizeof(GpPointF));
381 if(pen->endcap == LineCapArrowAnchor)
382 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
383 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
384 else if((pen->endcap == LineCapCustom) && pen->customend)
385 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
386 &ptcopy[count-1].X, &ptcopy[count-1].Y,
387 pen->customend->inset * pen->width);
389 if(pen->startcap == LineCapArrowAnchor)
390 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
391 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
392 else if((pen->startcap == LineCapCustom) && pen->customstart)
393 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
394 &ptcopy[0].X, &ptcopy[0].Y,
395 pen->customend->inset * pen->width);
397 draw_cap(hdc, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
398 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
399 draw_cap(hdc, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
400 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
403 for(i = 0; i < count; i ++){
404 pti[i].x = roundr(ptcopy[i].X);
405 pti[i].y = roundr(ptcopy[i].Y);
408 Polyline(hdc, pti, count);
417 /* Conducts a linear search to find the bezier points that will back off
418 * the endpoint of the curve by a distance of amt. Linear search works
419 * better than binary in this case because there are multiple solutions,
420 * and binary searches often find a bad one. I don't think this is what
421 * Windows does but short of rendering the bezier without GDI's help it's
422 * the best we can do. If rev then work from the start of the passed points
423 * instead of the end. */
424 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
427 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
428 INT i, first = 0, second = 1, third = 2, fourth = 3;
437 origx = pt[fourth].X;
438 origy = pt[fourth].Y;
439 memcpy(origpt, pt, sizeof(GpPointF) * 4);
441 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
442 /* reset bezier points to original values */
443 memcpy(pt, origpt, sizeof(GpPointF) * 4);
444 /* Perform magic on bezier points. Order is important here.*/
445 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
446 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
447 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
448 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
449 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
450 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
452 dx = pt[fourth].X - origx;
453 dy = pt[fourth].Y - origy;
455 diff = sqrt(dx * dx + dy * dy);
456 percent += 0.0005 * amt;
460 /* Draws bezier curves between given points, and if caps is true then draws an
461 * endcap at the end of the last line. FIXME: Startcaps not implemented. */
462 static GpStatus draw_polybezier(HDC hdc, GpPen *pen, GDIPCONST GpPointF * pt,
463 INT count, BOOL caps)
469 GpStatus status = GenericError;
474 pti = GdipAlloc(count * sizeof(POINT));
475 ptcopy = GdipAlloc(count * sizeof(GpPointF));
478 status = OutOfMemory;
482 memcpy(ptcopy, pt, count * sizeof(GpPointF));
485 if(pen->endcap == LineCapArrowAnchor)
486 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
487 /* FIXME The following is seemingly correct only for baseinset < 0 or
488 * baseinset > ~3. With smaller baseinsets, windows actually
489 * lengthens the bezier line instead of shortening it. */
490 else if((pen->endcap == LineCapCustom) && pen->customend){
493 shorten_line_amt(pt[count - 2].X, pt[count - 2].Y, &x, &y,
494 pen->width * pen->customend->inset);
495 MoveToEx(hdc, roundr(pt[count - 1].X), roundr(pt[count - 1].Y), &curpos);
496 LineTo(hdc, roundr(x), roundr(y));
497 MoveToEx(hdc, curpos.x, curpos.y, NULL);
500 if(pen->startcap == LineCapArrowAnchor)
501 shorten_bezier_amt(ptcopy, pen->width, TRUE);
502 else if((pen->startcap == LineCapCustom) && pen->customstart){
505 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y, &x, &y,
506 pen->width * pen->customend->inset);
507 MoveToEx(hdc, roundr(pt[0].X), roundr(pt[0].Y), &curpos);
508 LineTo(hdc, roundr(x), roundr(y));
509 MoveToEx(hdc, curpos.x, curpos.y, NULL);
512 /* the direction of the line cap is parallel to the direction at the
513 * end of the bezier (which, if it has been shortened, is not the same
514 * as the direction from pt[count-2] to pt[count-1]) */
515 draw_cap(hdc, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
516 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
517 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
518 pt[count - 1].X, pt[count - 1].Y);
520 draw_cap(hdc, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
521 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
522 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
525 for(i = 0; i < count; i ++){
526 pti[i].x = roundr(ptcopy[i].X);
527 pti[i].y = roundr(ptcopy[i].Y);
530 PolyBezier(hdc, pti, count);
541 /* Draws a combination of bezier curves and lines between points. */
542 static GpStatus draw_poly(HDC hdc, GpPen *pen, GDIPCONST GpPointF * pt,
543 GDIPCONST BYTE * types, INT count, BOOL caps)
545 POINT *pti = GdipAlloc(count * sizeof(POINT)), curpos;
546 BYTE *tp = GdipAlloc(count);
547 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
548 REAL x = pt[count - 1].X, y = pt[count - 1].Y;
550 GpStatus status = GenericError;
556 if(!pti || !tp || !ptcopy){
557 status = OutOfMemory;
561 for(i = 1; i < count; i++){
562 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
563 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
564 || !(types[i + 1] & PathPointTypeBezier)){
565 ERR("Bad bezier points\n");
572 /* If we are drawing caps, go through the points and adjust them accordingly,
573 * and draw the caps. */
575 memcpy(ptcopy, pt, count * sizeof(GpPointF));
577 switch(types[count - 1] & PathPointTypePathTypeMask){
578 case PathPointTypeBezier:
579 if(pen->endcap == LineCapArrowAnchor)
580 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
581 else if((pen->endcap == LineCapCustom) && pen->customend){
584 shorten_line_amt(pt[count - 2].X, pt[count - 2].Y, &x, &y,
585 pen->width * pen->customend->inset);
586 MoveToEx(hdc, roundr(pt[count - 1].X), roundr(pt[count - 1].Y), &curpos);
587 LineTo(hdc, roundr(x), roundr(y));
588 MoveToEx(hdc, curpos.x, curpos.y, NULL);
591 draw_cap(hdc, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
592 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
593 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
594 pt[count - 1].X, pt[count - 1].Y);
597 case PathPointTypeLine:
598 if(pen->endcap == LineCapArrowAnchor)
599 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
600 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
602 else if((pen->endcap == LineCapCustom) && pen->customend)
603 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
604 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
605 pen->customend->inset * pen->width);
607 draw_cap(hdc, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
608 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
613 ERR("Bad path last point\n");
617 /* Find start of points */
618 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
619 == PathPointTypeStart); j++);
621 switch(types[j] & PathPointTypePathTypeMask){
622 case PathPointTypeBezier:
623 if(pen->startcap == LineCapArrowAnchor)
624 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
625 else if((pen->startcap == LineCapCustom) && pen->customstart){
628 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y, &x, &y,
629 pen->width * pen->customstart->inset);
630 MoveToEx(hdc, roundr(pt[j - 1].X), roundr(pt[j - 1].Y), &curpos);
631 LineTo(hdc, roundr(x), roundr(y));
632 MoveToEx(hdc, curpos.x, curpos.y, NULL);
635 draw_cap(hdc, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
636 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
637 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
638 pt[j - 1].X, pt[j - 1].Y);
641 case PathPointTypeLine:
642 if(pen->startcap == LineCapArrowAnchor)
643 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
644 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
646 else if((pen->startcap == LineCapCustom) && pen->customstart)
647 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
648 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
649 pen->customstart->inset * pen->width);
651 draw_cap(hdc, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customstart,
652 pt[j].X, pt[j].Y, pt[j - 1].X,
657 ERR("Bad path points\n");
660 for(i = 0; i < count; i++){
661 pti[i].x = roundr(ptcopy[i].X);
662 pti[i].y = roundr(ptcopy[i].Y);
666 for(i = 0; i < count; i++){
667 pti[i].x = roundr(pt[i].X);
668 pti[i].y = roundr(pt[i].Y);
671 for(i = 0; i < count; i++){
672 tp[i] = convert_path_point_type(types[i]);
675 PolyDraw(hdc, pti, tp, count);
687 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
693 return InvalidParameter;
695 *graphics = GdipAlloc(sizeof(GpGraphics));
696 if(!*graphics) return OutOfMemory;
698 (*graphics)->hdc = hdc;
699 (*graphics)->hwnd = NULL;
700 (*graphics)->smoothing = SmoothingModeDefault;
701 (*graphics)->compqual = CompositingQualityDefault;
702 (*graphics)->interpolation = InterpolationModeDefault;
703 (*graphics)->pixeloffset = PixelOffsetModeDefault;
708 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
712 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
715 (*graphics)->hwnd = hwnd;
720 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
722 if(!graphics) return InvalidParameter;
724 ReleaseDC(graphics->hwnd, graphics->hdc);
726 HeapFree(GetProcessHeap(), 0, graphics);
731 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
732 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
734 INT save_state, num_pts;
735 GpPointF points[MAX_ARC_PTS];
738 if(!graphics || !pen)
739 return InvalidParameter;
741 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
743 save_state = SaveDC(graphics->hdc);
744 EndPath(graphics->hdc);
745 SelectObject(graphics->hdc, pen->gdipen);
747 retval = draw_polybezier(graphics->hdc, pen, points, num_pts, TRUE);
749 RestoreDC(graphics->hdc, save_state);
754 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
755 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
761 if(!graphics || !pen)
762 return InvalidParameter;
773 save_state = SaveDC(graphics->hdc);
774 EndPath(graphics->hdc);
775 SelectObject(graphics->hdc, pen->gdipen);
777 retval = draw_polybezier(graphics->hdc, pen, pt, 4, TRUE);
779 RestoreDC(graphics->hdc, save_state);
784 /* Approximates cardinal spline with Bezier curves. */
785 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
786 GDIPCONST GpPointF *points, INT count, REAL tension)
788 /* PolyBezier expects count*3-2 points. */
789 INT i, len_pt = count*3-2, save_state;
794 if(!graphics || !pen)
795 return InvalidParameter;
797 pt = GdipAlloc(len_pt * sizeof(GpPointF));
798 tension = tension * TENSION_CONST;
800 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
803 pt[0].X = points[0].X;
804 pt[0].Y = points[0].Y;
808 for(i = 0; i < count-2; i++){
809 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
813 pt[3*i+3].X = points[i+1].X;
814 pt[3*i+3].Y = points[i+1].Y;
819 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
820 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
824 pt[len_pt-1].X = points[count-1].X;
825 pt[len_pt-1].Y = points[count-1].Y;
827 save_state = SaveDC(graphics->hdc);
828 EndPath(graphics->hdc);
829 SelectObject(graphics->hdc, pen->gdipen);
831 retval = draw_polybezier(graphics->hdc, pen, pt, len_pt, TRUE);
834 RestoreDC(graphics->hdc, save_state);
839 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
840 INT y1, INT x2, INT y2)
846 if(!pen || !graphics)
847 return InvalidParameter;
854 save_state = SaveDC(graphics->hdc);
855 EndPath(graphics->hdc);
856 SelectObject(graphics->hdc, pen->gdipen);
858 retval = draw_polyline(graphics->hdc, pen, pt, 2, TRUE);
860 RestoreDC(graphics->hdc, save_state);
865 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
866 GpPointF *points, INT count)
871 if(!pen || !graphics || (count < 2))
872 return InvalidParameter;
874 save_state = SaveDC(graphics->hdc);
875 EndPath(graphics->hdc);
876 SelectObject(graphics->hdc, pen->gdipen);
878 retval = draw_polyline(graphics->hdc, pen, points, count, TRUE);
880 RestoreDC(graphics->hdc, save_state);
885 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
890 if(!pen || !graphics)
891 return InvalidParameter;
893 save_state = SaveDC(graphics->hdc);
894 EndPath(graphics->hdc);
895 SelectObject(graphics->hdc, pen->gdipen);
897 retval = draw_poly(graphics->hdc, pen, path->pathdata.Points,
898 path->pathdata.Types, path->pathdata.Count, TRUE);
900 RestoreDC(graphics->hdc, save_state);
905 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
906 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
909 return InvalidParameter;
911 return draw_pie(graphics, GetStockObject(NULL_BRUSH), pen->gdipen, x, y,
912 width, height, startAngle, sweepAngle);
915 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
916 INT y, INT width, INT height)
920 if(!pen || !graphics)
921 return InvalidParameter;
923 save_state = SaveDC(graphics->hdc);
924 EndPath(graphics->hdc);
925 SelectObject(graphics->hdc, pen->gdipen);
926 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
928 Rectangle(graphics->hdc, x, y, x + width, y + height);
930 RestoreDC(graphics->hdc, save_state);
935 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
940 if(!brush || !graphics || !path)
941 return InvalidParameter;
943 save_state = SaveDC(graphics->hdc);
944 EndPath(graphics->hdc);
945 SelectObject(graphics->hdc, brush->gdibrush);
946 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
949 BeginPath(graphics->hdc);
950 retval = draw_poly(graphics->hdc, NULL, path->pathdata.Points,
951 path->pathdata.Types, path->pathdata.Count, FALSE);
956 EndPath(graphics->hdc);
957 FillPath(graphics->hdc);
962 RestoreDC(graphics->hdc, save_state);
967 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
968 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
971 return InvalidParameter;
973 return draw_pie(graphics, brush->gdibrush, GetStockObject(NULL_PEN), x, y,
974 width, height, startAngle, sweepAngle);
977 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
978 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
979 CompositingQuality *quality)
981 if(!graphics || !quality)
982 return InvalidParameter;
984 *quality = graphics->compqual;
989 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
990 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
991 InterpolationMode *mode)
993 if(!graphics || !mode)
994 return InvalidParameter;
996 *mode = graphics->interpolation;
1001 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1002 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1005 if(!graphics || !mode)
1006 return InvalidParameter;
1008 *mode = graphics->pixeloffset;
1013 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1014 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1016 if(!graphics || !mode)
1017 return InvalidParameter;
1019 *mode = graphics->smoothing;
1024 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1027 return InvalidParameter;
1029 FIXME("graphics state not implemented\n");
1031 return NotImplemented;
1034 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1036 if(!graphics || !state)
1037 return InvalidParameter;
1039 FIXME("graphics state not implemented\n");
1041 return NotImplemented;
1044 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1045 CompositingQuality quality)
1048 return InvalidParameter;
1050 graphics->compqual = quality;
1055 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1056 InterpolationMode mode)
1059 return InvalidParameter;
1061 graphics->interpolation = mode;
1066 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1070 return InvalidParameter;
1072 graphics->pixeloffset = mode;
1077 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1080 return InvalidParameter;
1082 graphics->smoothing = mode;