winefile: Use SendMessageW instead of SNDMSG.
[wine.git] / dlls / gdiplus / graphics.c
blob38364764c626ae3a8c2cd82867966e3be0af116d
1 /*
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
19 #include <stdarg.h>
20 #include <math.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "wingdi.h"
26 #include "gdiplus.h"
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)
52 BYTE ret;
54 switch(type & PathPointTypePathTypeMask){
55 case PathPointTypeBezier:
56 ret = PT_BEZIERTO;
57 break;
58 case PathPointTypeLine:
59 ret = PT_LINETO;
60 break;
61 case PathPointTypeStart:
62 ret = PT_MOVETO;
63 break;
64 default:
65 ERR("Bad point type\n");
66 return 0;
69 if(type & PathPointTypeCloseSubpath)
70 ret |= PT_CLOSEFIGURE;
72 return ret;
75 static REAL convert_unit(HDC hdc, GpUnit unit)
77 switch(unit)
79 case UnitInch:
80 return (REAL) GetDeviceCaps(hdc, LOGPIXELSX);
81 case UnitPoint:
82 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 72.0;
83 case UnitDocument:
84 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 300.0;
85 case UnitMillimeter:
86 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 25.4;
87 case UnitWorld:
88 ERR("cannot convert UnitWorld\n");
89 return 0.0;
90 case UnitPixel:
91 case UnitDisplay:
92 default:
93 return 1.0;
97 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
99 HPEN gdipen;
100 REAL width;
101 INT save_state = SaveDC(graphics->hdc);
102 GpPointF pt[2];
104 EndPath(graphics->hdc);
106 /* Get an estimate for the amount the pen width is affected by the world
107 * transform. (This is similar to what some of the wine drivers do.) */
108 pt[0].X = 0.0;
109 pt[0].Y = 0.0;
110 pt[1].X = 1.0;
111 pt[1].Y = 1.0;
112 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
113 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
114 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
116 width *= pen->width * convert_unit(graphics->hdc,
117 pen->unit == UnitWorld ? graphics->unit : pen->unit);
119 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
120 SelectObject(graphics->hdc, gdipen);
122 return save_state;
125 static void restore_dc(GpGraphics *graphics, INT state)
127 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
128 RestoreDC(graphics->hdc, state);
131 /* This helper applies all the changes that the points listed in ptf need in
132 * order to be drawn on the device context. In the end, this should include at
133 * least:
134 * -scaling by page unit
135 * -applying world transformation
136 * -converting from float to int
137 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
138 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
139 * gdi to draw, and these functions would irreparably mess with line widths.
141 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
142 GpPointF *ptf, INT count)
144 REAL unitscale;
145 GpMatrix *matrix;
146 int i;
148 unitscale = convert_unit(graphics->hdc, graphics->unit);
150 /* apply page scale */
151 if(graphics->unit != UnitDisplay)
152 unitscale *= graphics->scale;
154 GdipCloneMatrix(graphics->worldtrans, &matrix);
155 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
156 GdipTransformMatrixPoints(matrix, ptf, count);
157 GdipDeleteMatrix(matrix);
159 for(i = 0; i < count; i++){
160 pti[i].x = roundr(ptf[i].X);
161 pti[i].y = roundr(ptf[i].Y);
165 /* GdipDrawPie/GdipFillPie helper function */
166 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
167 REAL height, REAL startAngle, REAL sweepAngle)
169 GpPointF ptf[4];
170 POINT pti[4];
172 ptf[0].X = x;
173 ptf[0].Y = y;
174 ptf[1].X = x + width;
175 ptf[1].Y = y + height;
177 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
178 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
180 transform_and_round_points(graphics, pti, ptf, 4);
182 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
183 pti[2].y, pti[3].x, pti[3].y);
186 /* GdipDrawCurve helper function.
187 * Calculates Bezier points from cardinal spline points. */
188 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
189 REAL *y1, REAL *x2, REAL *y2)
191 REAL xdiff, ydiff;
193 /* calculate tangent */
194 xdiff = pts[2].X - pts[0].X;
195 ydiff = pts[2].Y - pts[0].Y;
197 /* apply tangent to get control points */
198 *x1 = pts[1].X - tension * xdiff;
199 *y1 = pts[1].Y - tension * ydiff;
200 *x2 = pts[1].X + tension * xdiff;
201 *y2 = pts[1].Y + tension * ydiff;
204 /* GdipDrawCurve helper function.
205 * Calculates Bezier points from cardinal spline endpoints. */
206 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
207 REAL tension, REAL *x, REAL *y)
209 /* tangent at endpoints is the line from the endpoint to the adjacent point */
210 *x = roundr(tension * (xadj - xend) + xend);
211 *y = roundr(tension * (yadj - yend) + yend);
214 /* Draws the linecap the specified color and size on the hdc. The linecap is in
215 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
216 * should not be called on an hdc that has a path you care about. */
217 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
218 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
220 HGDIOBJ oldbrush = NULL, oldpen = NULL;
221 GpMatrix *matrix = NULL;
222 HBRUSH brush = NULL;
223 HPEN pen = NULL;
224 PointF ptf[4], *custptf = NULL;
225 POINT pt[4], *custpt = NULL;
226 BYTE *tp = NULL;
227 REAL theta, dsmall, dbig, dx, dy = 0.0;
228 INT i, count;
229 LOGBRUSH lb;
230 BOOL customstroke;
232 if((x1 == x2) && (y1 == y2))
233 return;
235 theta = gdiplus_atan2(y2 - y1, x2 - x1);
237 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
238 if(!customstroke){
239 brush = CreateSolidBrush(color);
240 lb.lbStyle = BS_SOLID;
241 lb.lbColor = color;
242 lb.lbHatch = 0;
243 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
244 PS_JOIN_MITER, 1, &lb, 0,
245 NULL);
246 oldbrush = SelectObject(graphics->hdc, brush);
247 oldpen = SelectObject(graphics->hdc, pen);
250 switch(cap){
251 case LineCapFlat:
252 break;
253 case LineCapSquare:
254 case LineCapSquareAnchor:
255 case LineCapDiamondAnchor:
256 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
257 if(cap == LineCapDiamondAnchor){
258 dsmall = cos(theta + M_PI_2) * size;
259 dbig = sin(theta + M_PI_2) * size;
261 else{
262 dsmall = cos(theta + M_PI_4) * size;
263 dbig = sin(theta + M_PI_4) * size;
266 ptf[0].X = x2 - dsmall;
267 ptf[1].X = x2 + dbig;
269 ptf[0].Y = y2 - dbig;
270 ptf[3].Y = y2 + dsmall;
272 ptf[1].Y = y2 - dsmall;
273 ptf[2].Y = y2 + dbig;
275 ptf[3].X = x2 - dbig;
276 ptf[2].X = x2 + dsmall;
278 transform_and_round_points(graphics, pt, ptf, 4);
279 Polygon(graphics->hdc, pt, 4);
281 break;
282 case LineCapArrowAnchor:
283 size = size * 4.0 / sqrt(3.0);
285 dx = cos(M_PI / 6.0 + theta) * size;
286 dy = sin(M_PI / 6.0 + theta) * size;
288 ptf[0].X = x2 - dx;
289 ptf[0].Y = y2 - dy;
291 dx = cos(- M_PI / 6.0 + theta) * size;
292 dy = sin(- M_PI / 6.0 + theta) * size;
294 ptf[1].X = x2 - dx;
295 ptf[1].Y = y2 - dy;
297 ptf[2].X = x2;
298 ptf[2].Y = y2;
300 transform_and_round_points(graphics, pt, ptf, 3);
301 Polygon(graphics->hdc, pt, 3);
303 break;
304 case LineCapRoundAnchor:
305 dx = dy = ANCHOR_WIDTH * size / 2.0;
307 ptf[0].X = x2 - dx;
308 ptf[0].Y = y2 - dy;
309 ptf[1].X = x2 + dx;
310 ptf[1].Y = y2 + dy;
312 transform_and_round_points(graphics, pt, ptf, 2);
313 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
315 break;
316 case LineCapTriangle:
317 size = size / 2.0;
318 dx = cos(M_PI_2 + theta) * size;
319 dy = sin(M_PI_2 + theta) * size;
321 ptf[0].X = x2 - dx;
322 ptf[0].Y = y2 - dy;
323 ptf[1].X = x2 + dx;
324 ptf[1].Y = y2 + dy;
326 dx = cos(theta) * size;
327 dy = sin(theta) * size;
329 ptf[2].X = x2 + dx;
330 ptf[2].Y = y2 + dy;
332 transform_and_round_points(graphics, pt, ptf, 3);
333 Polygon(graphics->hdc, pt, 3);
335 break;
336 case LineCapRound:
337 dx = dy = size / 2.0;
339 ptf[0].X = x2 - dx;
340 ptf[0].Y = y2 - dy;
341 ptf[1].X = x2 + dx;
342 ptf[1].Y = y2 + dy;
344 dx = -cos(M_PI_2 + theta) * size;
345 dy = -sin(M_PI_2 + theta) * size;
347 ptf[2].X = x2 - dx;
348 ptf[2].Y = y2 - dy;
349 ptf[3].X = x2 + dx;
350 ptf[3].Y = y2 + dy;
352 transform_and_round_points(graphics, pt, ptf, 4);
353 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
354 pt[2].y, pt[3].x, pt[3].y);
356 break;
357 case LineCapCustom:
358 if(!custom)
359 break;
361 count = custom->pathdata.Count;
362 custptf = GdipAlloc(count * sizeof(PointF));
363 custpt = GdipAlloc(count * sizeof(POINT));
364 tp = GdipAlloc(count);
366 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
367 goto custend;
369 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
371 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
372 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
373 MatrixOrderAppend);
374 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
375 GdipTransformMatrixPoints(matrix, custptf, count);
377 transform_and_round_points(graphics, custpt, custptf, count);
379 for(i = 0; i < count; i++)
380 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
382 if(custom->fill){
383 BeginPath(graphics->hdc);
384 PolyDraw(graphics->hdc, custpt, tp, count);
385 EndPath(graphics->hdc);
386 StrokeAndFillPath(graphics->hdc);
388 else
389 PolyDraw(graphics->hdc, custpt, tp, count);
391 custend:
392 GdipFree(custptf);
393 GdipFree(custpt);
394 GdipFree(tp);
395 GdipDeleteMatrix(matrix);
396 break;
397 default:
398 break;
401 if(!customstroke){
402 SelectObject(graphics->hdc, oldbrush);
403 SelectObject(graphics->hdc, oldpen);
404 DeleteObject(brush);
405 DeleteObject(pen);
409 /* Shortens the line by the given percent by changing x2, y2.
410 * If percent is > 1.0 then the line will change direction.
411 * If percent is negative it can lengthen the line. */
412 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
414 REAL dist, theta, dx, dy;
416 if((y1 == *y2) && (x1 == *x2))
417 return;
419 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
420 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
421 dx = cos(theta) * dist;
422 dy = sin(theta) * dist;
424 *x2 = *x2 + dx;
425 *y2 = *y2 + dy;
428 /* Shortens the line by the given amount by changing x2, y2.
429 * If the amount is greater than the distance, the line will become length 0.
430 * If the amount is negative, it can lengthen the line. */
431 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
433 REAL dx, dy, percent;
435 dx = *x2 - x1;
436 dy = *y2 - y1;
437 if(dx == 0 && dy == 0)
438 return;
440 percent = amt / sqrt(dx * dx + dy * dy);
441 if(percent >= 1.0){
442 *x2 = x1;
443 *y2 = y1;
444 return;
447 shorten_line_percent(x1, y1, x2, y2, percent);
450 /* Draws lines between the given points, and if caps is true then draws an endcap
451 * at the end of the last line. FIXME: Startcaps not implemented. */
452 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
453 GDIPCONST GpPointF * pt, INT count, BOOL caps)
455 POINT *pti = NULL;
456 GpPointF *ptcopy = NULL;
457 GpStatus status = GenericError;
459 if(!count)
460 return Ok;
462 pti = GdipAlloc(count * sizeof(POINT));
463 ptcopy = GdipAlloc(count * sizeof(GpPointF));
465 if(!pti || !ptcopy){
466 status = OutOfMemory;
467 goto end;
470 memcpy(ptcopy, pt, count * sizeof(GpPointF));
472 if(caps){
473 if(pen->endcap == LineCapArrowAnchor)
474 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
475 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
476 else if((pen->endcap == LineCapCustom) && pen->customend)
477 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
478 &ptcopy[count-1].X, &ptcopy[count-1].Y,
479 pen->customend->inset * pen->width);
481 if(pen->startcap == LineCapArrowAnchor)
482 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
483 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
484 else if((pen->startcap == LineCapCustom) && pen->customstart)
485 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
486 &ptcopy[0].X, &ptcopy[0].Y,
487 pen->customend->inset * pen->width);
489 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
490 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
491 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
492 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
495 transform_and_round_points(graphics, pti, ptcopy, count);
497 Polyline(graphics->hdc, pti, count);
499 end:
500 GdipFree(pti);
501 GdipFree(ptcopy);
503 return status;
506 /* Conducts a linear search to find the bezier points that will back off
507 * the endpoint of the curve by a distance of amt. Linear search works
508 * better than binary in this case because there are multiple solutions,
509 * and binary searches often find a bad one. I don't think this is what
510 * Windows does but short of rendering the bezier without GDI's help it's
511 * the best we can do. If rev then work from the start of the passed points
512 * instead of the end. */
513 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
515 GpPointF origpt[4];
516 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
517 INT i, first = 0, second = 1, third = 2, fourth = 3;
519 if(rev){
520 first = 3;
521 second = 2;
522 third = 1;
523 fourth = 0;
526 origx = pt[fourth].X;
527 origy = pt[fourth].Y;
528 memcpy(origpt, pt, sizeof(GpPointF) * 4);
530 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
531 /* reset bezier points to original values */
532 memcpy(pt, origpt, sizeof(GpPointF) * 4);
533 /* Perform magic on bezier points. Order is important here.*/
534 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
535 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
536 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
537 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
538 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
539 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
541 dx = pt[fourth].X - origx;
542 dy = pt[fourth].Y - origy;
544 diff = sqrt(dx * dx + dy * dy);
545 percent += 0.0005 * amt;
549 /* Draws bezier curves between given points, and if caps is true then draws an
550 * endcap at the end of the last line. FIXME: Startcaps not implemented. */
551 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
552 GDIPCONST GpPointF * pt, INT count, BOOL caps)
554 POINT *pti, curpos;
555 GpPointF *ptcopy;
556 REAL x, y;
557 GpStatus status = GenericError;
559 if(!count)
560 return Ok;
562 pti = GdipAlloc(count * sizeof(POINT));
563 ptcopy = GdipAlloc(count * sizeof(GpPointF));
565 if(!pti || !ptcopy){
566 status = OutOfMemory;
567 goto end;
570 memcpy(ptcopy, pt, count * sizeof(GpPointF));
572 if(caps){
573 if(pen->endcap == LineCapArrowAnchor)
574 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
575 /* FIXME The following is seemingly correct only for baseinset < 0 or
576 * baseinset > ~3. With smaller baseinsets, windows actually
577 * lengthens the bezier line instead of shortening it. */
578 else if((pen->endcap == LineCapCustom) && pen->customend){
579 x = pt[count - 1].X;
580 y = pt[count - 1].Y;
581 shorten_line_amt(pt[count - 2].X, pt[count - 2].Y, &x, &y,
582 pen->width * pen->customend->inset);
583 MoveToEx(graphics->hdc, roundr(pt[count - 1].X), roundr(pt[count - 1].Y), &curpos);
584 LineTo(graphics->hdc, roundr(x), roundr(y));
585 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
588 if(pen->startcap == LineCapArrowAnchor)
589 shorten_bezier_amt(ptcopy, pen->width, TRUE);
590 else if((pen->startcap == LineCapCustom) && pen->customstart){
591 x = ptcopy[0].X;
592 y = ptcopy[0].Y;
593 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y, &x, &y,
594 pen->width * pen->customend->inset);
595 MoveToEx(graphics->hdc, roundr(pt[0].X), roundr(pt[0].Y), &curpos);
596 LineTo(graphics->hdc, roundr(x), roundr(y));
597 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
600 /* the direction of the line cap is parallel to the direction at the
601 * end of the bezier (which, if it has been shortened, is not the same
602 * as the direction from pt[count-2] to pt[count-1]) */
603 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
604 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
605 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
606 pt[count - 1].X, pt[count - 1].Y);
608 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
609 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
610 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
613 transform_and_round_points(graphics, pti, ptcopy, count);
615 PolyBezier(graphics->hdc, pti, count);
617 status = Ok;
619 end:
620 GdipFree(pti);
621 GdipFree(ptcopy);
623 return status;
626 /* Draws a combination of bezier curves and lines between points. */
627 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
628 GDIPCONST BYTE * types, INT count, BOOL caps)
630 POINT *pti = GdipAlloc(count * sizeof(POINT)), curpos;
631 BYTE *tp = GdipAlloc(count);
632 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
633 REAL x = pt[count - 1].X, y = pt[count - 1].Y;
634 INT i, j;
635 GpStatus status = GenericError;
637 if(!count){
638 status = Ok;
639 goto end;
641 if(!pti || !tp || !ptcopy){
642 status = OutOfMemory;
643 goto end;
646 for(i = 1; i < count; i++){
647 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
648 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
649 || !(types[i + 1] & PathPointTypeBezier)){
650 ERR("Bad bezier points\n");
651 goto end;
653 i += 2;
657 memcpy(ptcopy, pt, count * sizeof(GpPointF));
659 /* If we are drawing caps, go through the points and adjust them accordingly,
660 * and draw the caps. */
661 if(caps){
662 switch(types[count - 1] & PathPointTypePathTypeMask){
663 case PathPointTypeBezier:
664 if(pen->endcap == LineCapArrowAnchor)
665 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
666 else if((pen->endcap == LineCapCustom) && pen->customend){
667 x = pt[count - 1].X;
668 y = pt[count - 1].Y;
669 shorten_line_amt(pt[count - 2].X, pt[count - 2].Y, &x, &y,
670 pen->width * pen->customend->inset);
671 MoveToEx(graphics->hdc, roundr(pt[count - 1].X),
672 roundr(pt[count - 1].Y), &curpos);
673 LineTo(graphics->hdc, roundr(x), roundr(y));
674 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
677 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
678 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
679 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
680 pt[count - 1].X, pt[count - 1].Y);
682 break;
683 case PathPointTypeLine:
684 if(pen->endcap == LineCapArrowAnchor)
685 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
686 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
687 pen->width);
688 else if((pen->endcap == LineCapCustom) && pen->customend)
689 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
690 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
691 pen->customend->inset * pen->width);
693 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
694 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
695 pt[count - 1].Y);
697 break;
698 default:
699 ERR("Bad path last point\n");
700 goto end;
703 /* Find start of points */
704 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
705 == PathPointTypeStart); j++);
707 switch(types[j] & PathPointTypePathTypeMask){
708 case PathPointTypeBezier:
709 if(pen->startcap == LineCapArrowAnchor)
710 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
711 else if((pen->startcap == LineCapCustom) && pen->customstart){
712 x = pt[j - 1].X;
713 y = pt[j - 1].Y;
714 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y, &x, &y,
715 pen->width * pen->customstart->inset);
716 MoveToEx(graphics->hdc, roundr(pt[j - 1].X), roundr(pt[j - 1].Y), &curpos);
717 LineTo(graphics->hdc, roundr(x), roundr(y));
718 MoveToEx(graphics->hdc, curpos.x, curpos.y, NULL);
721 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
722 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
723 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
724 pt[j - 1].X, pt[j - 1].Y);
726 break;
727 case PathPointTypeLine:
728 if(pen->startcap == LineCapArrowAnchor)
729 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
730 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
731 pen->width);
732 else if((pen->startcap == LineCapCustom) && pen->customstart)
733 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
734 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
735 pen->customstart->inset * pen->width);
737 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customstart,
738 pt[j].X, pt[j].Y, pt[j - 1].X,
739 pt[j - 1].Y);
741 break;
742 default:
743 ERR("Bad path points\n");
744 goto end;
748 transform_and_round_points(graphics, pti, ptcopy, count);
750 for(i = 0; i < count; i++){
751 tp[i] = convert_path_point_type(types[i]);
754 PolyDraw(graphics->hdc, pti, tp, count);
756 status = Ok;
758 end:
759 GdipFree(pti);
760 GdipFree(ptcopy);
761 GdipFree(tp);
763 return status;
766 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
768 GpStatus retval;
770 if(hdc == NULL)
771 return OutOfMemory;
773 if(graphics == NULL)
774 return InvalidParameter;
776 *graphics = GdipAlloc(sizeof(GpGraphics));
777 if(!*graphics) return OutOfMemory;
779 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
780 GdipFree(*graphics);
781 return retval;
784 (*graphics)->hdc = hdc;
785 (*graphics)->hwnd = NULL;
786 (*graphics)->smoothing = SmoothingModeDefault;
787 (*graphics)->compqual = CompositingQualityDefault;
788 (*graphics)->interpolation = InterpolationModeDefault;
789 (*graphics)->pixeloffset = PixelOffsetModeDefault;
790 (*graphics)->unit = UnitDisplay;
791 (*graphics)->scale = 1.0;
793 return Ok;
796 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
798 GpStatus ret;
800 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
801 return ret;
803 (*graphics)->hwnd = hwnd;
805 return Ok;
808 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
809 GpMetafile **metafile)
811 static int calls;
813 if(!hemf || !metafile)
814 return InvalidParameter;
816 if(!(calls++))
817 FIXME("not implemented\n");
819 return NotImplemented;
822 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
823 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
825 static int calls;
827 if(!hwmf || !metafile || !placeable)
828 return InvalidParameter;
830 if(!(calls++))
831 FIXME("not implemented\n");
833 return NotImplemented;
836 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
838 if(!graphics) return InvalidParameter;
839 if(graphics->hwnd)
840 ReleaseDC(graphics->hwnd, graphics->hdc);
842 GdipDeleteMatrix(graphics->worldtrans);
843 HeapFree(GetProcessHeap(), 0, graphics);
845 return Ok;
848 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
849 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
851 INT save_state, num_pts;
852 GpPointF points[MAX_ARC_PTS];
853 GpStatus retval;
855 if(!graphics || !pen)
856 return InvalidParameter;
858 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
860 save_state = prepare_dc(graphics, pen);
862 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
864 restore_dc(graphics, save_state);
866 return retval;
869 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
870 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
872 INT save_state;
873 GpPointF pt[4];
874 GpStatus retval;
876 if(!graphics || !pen)
877 return InvalidParameter;
879 pt[0].X = x1;
880 pt[0].Y = y1;
881 pt[1].X = x2;
882 pt[1].Y = y2;
883 pt[2].X = x3;
884 pt[2].Y = y3;
885 pt[3].X = x4;
886 pt[3].Y = y4;
888 save_state = prepare_dc(graphics, pen);
890 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
892 restore_dc(graphics, save_state);
894 return retval;
897 /* Approximates cardinal spline with Bezier curves. */
898 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
899 GDIPCONST GpPointF *points, INT count, REAL tension)
901 /* PolyBezier expects count*3-2 points. */
902 INT i, len_pt = count*3-2, save_state;
903 GpPointF *pt;
904 REAL x1, x2, y1, y2;
905 GpStatus retval;
907 if(!graphics || !pen)
908 return InvalidParameter;
910 pt = GdipAlloc(len_pt * sizeof(GpPointF));
911 tension = tension * TENSION_CONST;
913 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
914 tension, &x1, &y1);
916 pt[0].X = points[0].X;
917 pt[0].Y = points[0].Y;
918 pt[1].X = x1;
919 pt[1].Y = y1;
921 for(i = 0; i < count-2; i++){
922 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
924 pt[3*i+2].X = x1;
925 pt[3*i+2].Y = y1;
926 pt[3*i+3].X = points[i+1].X;
927 pt[3*i+3].Y = points[i+1].Y;
928 pt[3*i+4].X = x2;
929 pt[3*i+4].Y = y2;
932 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
933 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
935 pt[len_pt-2].X = x1;
936 pt[len_pt-2].Y = y1;
937 pt[len_pt-1].X = points[count-1].X;
938 pt[len_pt-1].Y = points[count-1].Y;
940 save_state = prepare_dc(graphics, pen);
942 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
944 GdipFree(pt);
945 restore_dc(graphics, save_state);
947 return retval;
950 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
951 INT y1, INT x2, INT y2)
953 INT save_state;
954 GpPointF pt[2];
955 GpStatus retval;
957 if(!pen || !graphics)
958 return InvalidParameter;
960 pt[0].X = (REAL)x1;
961 pt[0].Y = (REAL)y1;
962 pt[1].X = (REAL)x2;
963 pt[1].Y = (REAL)y2;
965 save_state = prepare_dc(graphics, pen);
967 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
969 restore_dc(graphics, save_state);
971 return retval;
974 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
975 GpPointF *points, INT count)
977 INT save_state;
978 GpStatus retval;
980 if(!pen || !graphics || (count < 2))
981 return InvalidParameter;
983 save_state = prepare_dc(graphics, pen);
985 retval = draw_polyline(graphics, pen, points, count, TRUE);
987 restore_dc(graphics, save_state);
989 return retval;
992 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
994 INT save_state;
995 GpStatus retval;
997 if(!pen || !graphics)
998 return InvalidParameter;
1000 save_state = prepare_dc(graphics, pen);
1002 retval = draw_poly(graphics, pen, path->pathdata.Points,
1003 path->pathdata.Types, path->pathdata.Count, TRUE);
1005 restore_dc(graphics, save_state);
1007 return retval;
1010 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1011 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1013 INT save_state;
1015 if(!graphics || !pen)
1016 return InvalidParameter;
1018 save_state = prepare_dc(graphics, pen);
1019 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1021 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1023 restore_dc(graphics, save_state);
1025 return Ok;
1028 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1029 INT y, INT width, INT height)
1031 INT save_state;
1033 if(!pen || !graphics)
1034 return InvalidParameter;
1036 save_state = prepare_dc(graphics, pen);
1037 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1039 Rectangle(graphics->hdc, x, y, x + width, y + height);
1041 restore_dc(graphics, save_state);
1043 return Ok;
1046 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1048 INT save_state;
1049 GpStatus retval;
1051 if(!brush || !graphics || !path)
1052 return InvalidParameter;
1054 save_state = SaveDC(graphics->hdc);
1055 EndPath(graphics->hdc);
1056 SelectObject(graphics->hdc, brush->gdibrush);
1057 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1058 : WINDING));
1060 BeginPath(graphics->hdc);
1061 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1062 path->pathdata.Types, path->pathdata.Count, FALSE);
1064 if(retval != Ok)
1065 goto end;
1067 EndPath(graphics->hdc);
1068 FillPath(graphics->hdc);
1070 retval = Ok;
1072 end:
1073 RestoreDC(graphics->hdc, save_state);
1075 return retval;
1078 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1079 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1081 INT save_state;
1083 if(!graphics || !brush)
1084 return InvalidParameter;
1086 save_state = SaveDC(graphics->hdc);
1087 EndPath(graphics->hdc);
1088 SelectObject(graphics->hdc, brush->gdibrush);
1089 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1091 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1093 RestoreDC(graphics->hdc, save_state);
1095 return Ok;
1098 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1099 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1101 INT save_state, i;
1102 GpPointF *ptf = NULL;
1103 POINT *pti = NULL;
1104 GpStatus retval = Ok;
1106 if(!graphics || !brush || !points || !count)
1107 return InvalidParameter;
1109 ptf = GdipAlloc(count * sizeof(GpPointF));
1110 pti = GdipAlloc(count * sizeof(POINT));
1111 if(!ptf || !pti){
1112 retval = OutOfMemory;
1113 goto end;
1116 for(i = 0; i < count; i ++){
1117 ptf[i].X = (REAL) points[i].X;
1118 ptf[i].Y = (REAL) points[i].Y;
1121 save_state = SaveDC(graphics->hdc);
1122 EndPath(graphics->hdc);
1123 SelectObject(graphics->hdc, brush->gdibrush);
1124 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1125 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1126 : WINDING));
1128 transform_and_round_points(graphics, pti, ptf, count);
1129 Polygon(graphics->hdc, pti, count);
1131 RestoreDC(graphics->hdc, save_state);
1133 end:
1134 GdipFree(ptf);
1135 GdipFree(pti);
1137 return retval;
1140 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1141 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1142 CompositingQuality *quality)
1144 if(!graphics || !quality)
1145 return InvalidParameter;
1147 *quality = graphics->compqual;
1149 return Ok;
1152 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1153 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1154 InterpolationMode *mode)
1156 if(!graphics || !mode)
1157 return InvalidParameter;
1159 *mode = graphics->interpolation;
1161 return Ok;
1164 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1166 if(!graphics || !scale)
1167 return InvalidParameter;
1169 *scale = graphics->scale;
1171 return Ok;
1174 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1176 if(!graphics || !unit)
1177 return InvalidParameter;
1179 *unit = graphics->unit;
1181 return Ok;
1184 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1185 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1186 *mode)
1188 if(!graphics || !mode)
1189 return InvalidParameter;
1191 *mode = graphics->pixeloffset;
1193 return Ok;
1196 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1197 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1199 if(!graphics || !mode)
1200 return InvalidParameter;
1202 *mode = graphics->smoothing;
1204 return Ok;
1207 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1209 if(!graphics || !matrix)
1210 return InvalidParameter;
1212 memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1213 return Ok;
1216 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1218 static int calls;
1220 if(!graphics)
1221 return InvalidParameter;
1223 if(!(calls++))
1224 FIXME("graphics state not implemented\n");
1226 return NotImplemented;
1229 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1231 static int calls;
1233 if(!graphics || !state)
1234 return InvalidParameter;
1236 if(!(calls++))
1237 FIXME("graphics state not implemented\n");
1239 return NotImplemented;
1242 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1243 CompositingQuality quality)
1245 if(!graphics)
1246 return InvalidParameter;
1248 graphics->compqual = quality;
1250 return Ok;
1253 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1254 InterpolationMode mode)
1256 if(!graphics)
1257 return InvalidParameter;
1259 graphics->interpolation = mode;
1261 return Ok;
1264 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1266 if(!graphics || (scale <= 0.0))
1267 return InvalidParameter;
1269 graphics->scale = scale;
1271 return Ok;
1274 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1276 if(!graphics || (unit == UnitWorld))
1277 return InvalidParameter;
1279 graphics->unit = unit;
1281 return Ok;
1284 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1285 mode)
1287 if(!graphics)
1288 return InvalidParameter;
1290 graphics->pixeloffset = mode;
1292 return Ok;
1295 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1297 if(!graphics)
1298 return InvalidParameter;
1300 graphics->smoothing = mode;
1302 return Ok;
1305 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1307 if(!graphics || !matrix)
1308 return InvalidParameter;
1310 GdipDeleteMatrix(graphics->worldtrans);
1311 return GdipCloneMatrix(matrix, &graphics->worldtrans);