push a4aeee26091b4888c4962ca8601c7905b237df2d
[wine/hacks.git] / dlls / gdiplus / graphics.c
blobbff1c862f52287e8e34b28c0de269535fd3a7797
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>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44 /* looks-right constants */
45 #define ANCHOR_WIDTH (2.0)
46 #define MAX_ITERS (50)
48 /* Converts angle (in degrees) to x/y coordinates */
49 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
51 REAL radAngle, hypotenuse;
53 radAngle = deg2rad(angle);
54 hypotenuse = 50.0; /* arbitrary */
56 *x = x_0 + cos(radAngle) * hypotenuse;
57 *y = y_0 + sin(radAngle) * hypotenuse;
60 /* Converts from gdiplus path point type to gdi path point type. */
61 static BYTE convert_path_point_type(BYTE type)
63 BYTE ret;
65 switch(type & PathPointTypePathTypeMask){
66 case PathPointTypeBezier:
67 ret = PT_BEZIERTO;
68 break;
69 case PathPointTypeLine:
70 ret = PT_LINETO;
71 break;
72 case PathPointTypeStart:
73 ret = PT_MOVETO;
74 break;
75 default:
76 ERR("Bad point type\n");
77 return 0;
80 if(type & PathPointTypeCloseSubpath)
81 ret |= PT_CLOSEFIGURE;
83 return ret;
86 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
88 HPEN gdipen;
89 REAL width;
90 INT save_state = SaveDC(graphics->hdc), i, numdashes;
91 GpPointF pt[2];
92 DWORD dash_array[MAX_DASHLEN];
94 EndPath(graphics->hdc);
96 if(pen->unit == UnitPixel){
97 width = pen->width;
99 else{
100 /* Get an estimate for the amount the pen width is affected by the world
101 * transform. (This is similar to what some of the wine drivers do.) */
102 pt[0].X = 0.0;
103 pt[0].Y = 0.0;
104 pt[1].X = 1.0;
105 pt[1].Y = 1.0;
106 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
107 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
108 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
110 width *= pen->width * convert_unit(graphics->hdc,
111 pen->unit == UnitWorld ? graphics->unit : pen->unit);
114 if(pen->dash == DashStyleCustom){
115 numdashes = min(pen->numdashes, MAX_DASHLEN);
117 TRACE("dashes are: ");
118 for(i = 0; i < numdashes; i++){
119 dash_array[i] = roundr(width * pen->dashes[i]);
120 TRACE("%d, ", dash_array[i]);
122 TRACE("\n and the pen style is %x\n", pen->style);
124 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
125 numdashes, dash_array);
127 else
128 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
130 SelectObject(graphics->hdc, gdipen);
132 return save_state;
135 static void restore_dc(GpGraphics *graphics, INT state)
137 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
138 RestoreDC(graphics->hdc, state);
141 /* This helper applies all the changes that the points listed in ptf need in
142 * order to be drawn on the device context. In the end, this should include at
143 * least:
144 * -scaling by page unit
145 * -applying world transformation
146 * -converting from float to int
147 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
148 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
149 * gdi to draw, and these functions would irreparably mess with line widths.
151 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
152 GpPointF *ptf, INT count)
154 REAL unitscale;
155 GpMatrix *matrix;
156 int i;
158 unitscale = convert_unit(graphics->hdc, graphics->unit);
160 /* apply page scale */
161 if(graphics->unit != UnitDisplay)
162 unitscale *= graphics->scale;
164 GdipCloneMatrix(graphics->worldtrans, &matrix);
165 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
166 GdipTransformMatrixPoints(matrix, ptf, count);
167 GdipDeleteMatrix(matrix);
169 for(i = 0; i < count; i++){
170 pti[i].x = roundr(ptf[i].X);
171 pti[i].y = roundr(ptf[i].Y);
175 /* GdipDrawPie/GdipFillPie helper function */
176 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
177 REAL height, REAL startAngle, REAL sweepAngle)
179 GpPointF ptf[4];
180 POINT pti[4];
182 ptf[0].X = x;
183 ptf[0].Y = y;
184 ptf[1].X = x + width;
185 ptf[1].Y = y + height;
187 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
188 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
190 transform_and_round_points(graphics, pti, ptf, 4);
192 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
193 pti[2].y, pti[3].x, pti[3].y);
196 /* Draws the linecap the specified color and size on the hdc. The linecap is in
197 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
198 * should not be called on an hdc that has a path you care about. */
199 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
200 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
202 HGDIOBJ oldbrush = NULL, oldpen = NULL;
203 GpMatrix *matrix = NULL;
204 HBRUSH brush = NULL;
205 HPEN pen = NULL;
206 PointF ptf[4], *custptf = NULL;
207 POINT pt[4], *custpt = NULL;
208 BYTE *tp = NULL;
209 REAL theta, dsmall, dbig, dx, dy = 0.0;
210 INT i, count;
211 LOGBRUSH lb;
212 BOOL customstroke;
214 if((x1 == x2) && (y1 == y2))
215 return;
217 theta = gdiplus_atan2(y2 - y1, x2 - x1);
219 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
220 if(!customstroke){
221 brush = CreateSolidBrush(color);
222 lb.lbStyle = BS_SOLID;
223 lb.lbColor = color;
224 lb.lbHatch = 0;
225 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
226 PS_JOIN_MITER, 1, &lb, 0,
227 NULL);
228 oldbrush = SelectObject(graphics->hdc, brush);
229 oldpen = SelectObject(graphics->hdc, pen);
232 switch(cap){
233 case LineCapFlat:
234 break;
235 case LineCapSquare:
236 case LineCapSquareAnchor:
237 case LineCapDiamondAnchor:
238 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
239 if(cap == LineCapDiamondAnchor){
240 dsmall = cos(theta + M_PI_2) * size;
241 dbig = sin(theta + M_PI_2) * size;
243 else{
244 dsmall = cos(theta + M_PI_4) * size;
245 dbig = sin(theta + M_PI_4) * size;
248 ptf[0].X = x2 - dsmall;
249 ptf[1].X = x2 + dbig;
251 ptf[0].Y = y2 - dbig;
252 ptf[3].Y = y2 + dsmall;
254 ptf[1].Y = y2 - dsmall;
255 ptf[2].Y = y2 + dbig;
257 ptf[3].X = x2 - dbig;
258 ptf[2].X = x2 + dsmall;
260 transform_and_round_points(graphics, pt, ptf, 4);
261 Polygon(graphics->hdc, pt, 4);
263 break;
264 case LineCapArrowAnchor:
265 size = size * 4.0 / sqrt(3.0);
267 dx = cos(M_PI / 6.0 + theta) * size;
268 dy = sin(M_PI / 6.0 + theta) * size;
270 ptf[0].X = x2 - dx;
271 ptf[0].Y = y2 - dy;
273 dx = cos(- M_PI / 6.0 + theta) * size;
274 dy = sin(- M_PI / 6.0 + theta) * size;
276 ptf[1].X = x2 - dx;
277 ptf[1].Y = y2 - dy;
279 ptf[2].X = x2;
280 ptf[2].Y = y2;
282 transform_and_round_points(graphics, pt, ptf, 3);
283 Polygon(graphics->hdc, pt, 3);
285 break;
286 case LineCapRoundAnchor:
287 dx = dy = ANCHOR_WIDTH * size / 2.0;
289 ptf[0].X = x2 - dx;
290 ptf[0].Y = y2 - dy;
291 ptf[1].X = x2 + dx;
292 ptf[1].Y = y2 + dy;
294 transform_and_round_points(graphics, pt, ptf, 2);
295 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
297 break;
298 case LineCapTriangle:
299 size = size / 2.0;
300 dx = cos(M_PI_2 + theta) * size;
301 dy = sin(M_PI_2 + theta) * size;
303 ptf[0].X = x2 - dx;
304 ptf[0].Y = y2 - dy;
305 ptf[1].X = x2 + dx;
306 ptf[1].Y = y2 + dy;
308 dx = cos(theta) * size;
309 dy = sin(theta) * size;
311 ptf[2].X = x2 + dx;
312 ptf[2].Y = y2 + dy;
314 transform_and_round_points(graphics, pt, ptf, 3);
315 Polygon(graphics->hdc, pt, 3);
317 break;
318 case LineCapRound:
319 dx = dy = size / 2.0;
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(M_PI_2 + theta) * size;
327 dy = -sin(M_PI_2 + theta) * size;
329 ptf[2].X = x2 - dx;
330 ptf[2].Y = y2 - dy;
331 ptf[3].X = x2 + dx;
332 ptf[3].Y = y2 + dy;
334 transform_and_round_points(graphics, pt, ptf, 4);
335 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
336 pt[2].y, pt[3].x, pt[3].y);
338 break;
339 case LineCapCustom:
340 if(!custom)
341 break;
343 count = custom->pathdata.Count;
344 custptf = GdipAlloc(count * sizeof(PointF));
345 custpt = GdipAlloc(count * sizeof(POINT));
346 tp = GdipAlloc(count);
348 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
349 goto custend;
351 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
353 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
354 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
355 MatrixOrderAppend);
356 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
357 GdipTransformMatrixPoints(matrix, custptf, count);
359 transform_and_round_points(graphics, custpt, custptf, count);
361 for(i = 0; i < count; i++)
362 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
364 if(custom->fill){
365 BeginPath(graphics->hdc);
366 PolyDraw(graphics->hdc, custpt, tp, count);
367 EndPath(graphics->hdc);
368 StrokeAndFillPath(graphics->hdc);
370 else
371 PolyDraw(graphics->hdc, custpt, tp, count);
373 custend:
374 GdipFree(custptf);
375 GdipFree(custpt);
376 GdipFree(tp);
377 GdipDeleteMatrix(matrix);
378 break;
379 default:
380 break;
383 if(!customstroke){
384 SelectObject(graphics->hdc, oldbrush);
385 SelectObject(graphics->hdc, oldpen);
386 DeleteObject(brush);
387 DeleteObject(pen);
391 /* Shortens the line by the given percent by changing x2, y2.
392 * If percent is > 1.0 then the line will change direction.
393 * If percent is negative it can lengthen the line. */
394 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
396 REAL dist, theta, dx, dy;
398 if((y1 == *y2) && (x1 == *x2))
399 return;
401 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
402 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
403 dx = cos(theta) * dist;
404 dy = sin(theta) * dist;
406 *x2 = *x2 + dx;
407 *y2 = *y2 + dy;
410 /* Shortens the line by the given amount by changing x2, y2.
411 * If the amount is greater than the distance, the line will become length 0.
412 * If the amount is negative, it can lengthen the line. */
413 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
415 REAL dx, dy, percent;
417 dx = *x2 - x1;
418 dy = *y2 - y1;
419 if(dx == 0 && dy == 0)
420 return;
422 percent = amt / sqrt(dx * dx + dy * dy);
423 if(percent >= 1.0){
424 *x2 = x1;
425 *y2 = y1;
426 return;
429 shorten_line_percent(x1, y1, x2, y2, percent);
432 /* Draws lines between the given points, and if caps is true then draws an endcap
433 * at the end of the last line. */
434 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
435 GDIPCONST GpPointF * pt, INT count, BOOL caps)
437 POINT *pti = NULL;
438 GpPointF *ptcopy = NULL;
439 GpStatus status = GenericError;
441 if(!count)
442 return Ok;
444 pti = GdipAlloc(count * sizeof(POINT));
445 ptcopy = GdipAlloc(count * sizeof(GpPointF));
447 if(!pti || !ptcopy){
448 status = OutOfMemory;
449 goto end;
452 memcpy(ptcopy, pt, count * sizeof(GpPointF));
454 if(caps){
455 if(pen->endcap == LineCapArrowAnchor)
456 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
457 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
458 else if((pen->endcap == LineCapCustom) && pen->customend)
459 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
460 &ptcopy[count-1].X, &ptcopy[count-1].Y,
461 pen->customend->inset * pen->width);
463 if(pen->startcap == LineCapArrowAnchor)
464 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
465 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
466 else if((pen->startcap == LineCapCustom) && pen->customstart)
467 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
468 &ptcopy[0].X, &ptcopy[0].Y,
469 pen->customstart->inset * pen->width);
471 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
472 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
473 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
474 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
477 transform_and_round_points(graphics, pti, ptcopy, count);
479 if(Polyline(graphics->hdc, pti, count))
480 status = Ok;
482 end:
483 GdipFree(pti);
484 GdipFree(ptcopy);
486 return status;
489 /* Conducts a linear search to find the bezier points that will back off
490 * the endpoint of the curve by a distance of amt. Linear search works
491 * better than binary in this case because there are multiple solutions,
492 * and binary searches often find a bad one. I don't think this is what
493 * Windows does but short of rendering the bezier without GDI's help it's
494 * the best we can do. If rev then work from the start of the passed points
495 * instead of the end. */
496 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
498 GpPointF origpt[4];
499 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
500 INT i, first = 0, second = 1, third = 2, fourth = 3;
502 if(rev){
503 first = 3;
504 second = 2;
505 third = 1;
506 fourth = 0;
509 origx = pt[fourth].X;
510 origy = pt[fourth].Y;
511 memcpy(origpt, pt, sizeof(GpPointF) * 4);
513 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
514 /* reset bezier points to original values */
515 memcpy(pt, origpt, sizeof(GpPointF) * 4);
516 /* Perform magic on bezier points. Order is important here.*/
517 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
518 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
519 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
520 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
521 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
522 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
524 dx = pt[fourth].X - origx;
525 dy = pt[fourth].Y - origy;
527 diff = sqrt(dx * dx + dy * dy);
528 percent += 0.0005 * amt;
532 /* Draws bezier curves between given points, and if caps is true then draws an
533 * endcap at the end of the last line. */
534 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
535 GDIPCONST GpPointF * pt, INT count, BOOL caps)
537 POINT *pti;
538 GpPointF *ptcopy;
539 GpStatus status = GenericError;
541 if(!count)
542 return Ok;
544 pti = GdipAlloc(count * sizeof(POINT));
545 ptcopy = GdipAlloc(count * sizeof(GpPointF));
547 if(!pti || !ptcopy){
548 status = OutOfMemory;
549 goto end;
552 memcpy(ptcopy, pt, count * sizeof(GpPointF));
554 if(caps){
555 if(pen->endcap == LineCapArrowAnchor)
556 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
557 else if((pen->endcap == LineCapCustom) && pen->customend)
558 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
559 FALSE);
561 if(pen->startcap == LineCapArrowAnchor)
562 shorten_bezier_amt(ptcopy, pen->width, TRUE);
563 else if((pen->startcap == LineCapCustom) && pen->customstart)
564 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
566 /* the direction of the line cap is parallel to the direction at the
567 * end of the bezier (which, if it has been shortened, is not the same
568 * as the direction from pt[count-2] to pt[count-1]) */
569 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
570 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
571 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
572 pt[count - 1].X, pt[count - 1].Y);
574 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
575 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
576 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
579 transform_and_round_points(graphics, pti, ptcopy, count);
581 PolyBezier(graphics->hdc, pti, count);
583 status = Ok;
585 end:
586 GdipFree(pti);
587 GdipFree(ptcopy);
589 return status;
592 /* Draws a combination of bezier curves and lines between points. */
593 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
594 GDIPCONST BYTE * types, INT count, BOOL caps)
596 POINT *pti = GdipAlloc(count * sizeof(POINT));
597 BYTE *tp = GdipAlloc(count);
598 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
599 INT i, j;
600 GpStatus status = GenericError;
602 if(!count){
603 status = Ok;
604 goto end;
606 if(!pti || !tp || !ptcopy){
607 status = OutOfMemory;
608 goto end;
611 for(i = 1; i < count; i++){
612 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
613 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
614 || !(types[i + 1] & PathPointTypeBezier)){
615 ERR("Bad bezier points\n");
616 goto end;
618 i += 2;
622 memcpy(ptcopy, pt, count * sizeof(GpPointF));
624 /* If we are drawing caps, go through the points and adjust them accordingly,
625 * and draw the caps. */
626 if(caps){
627 switch(types[count - 1] & PathPointTypePathTypeMask){
628 case PathPointTypeBezier:
629 if(pen->endcap == LineCapArrowAnchor)
630 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
631 else if((pen->endcap == LineCapCustom) && pen->customend)
632 shorten_bezier_amt(&ptcopy[count - 4],
633 pen->width * pen->customend->inset, FALSE);
635 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
636 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
637 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
638 pt[count - 1].X, pt[count - 1].Y);
640 break;
641 case PathPointTypeLine:
642 if(pen->endcap == LineCapArrowAnchor)
643 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
644 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
645 pen->width);
646 else if((pen->endcap == LineCapCustom) && pen->customend)
647 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
648 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
649 pen->customend->inset * pen->width);
651 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
652 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
653 pt[count - 1].Y);
655 break;
656 default:
657 ERR("Bad path last point\n");
658 goto end;
661 /* Find start of points */
662 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
663 == PathPointTypeStart); j++);
665 switch(types[j] & PathPointTypePathTypeMask){
666 case PathPointTypeBezier:
667 if(pen->startcap == LineCapArrowAnchor)
668 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
669 else if((pen->startcap == LineCapCustom) && pen->customstart)
670 shorten_bezier_amt(&ptcopy[j - 1],
671 pen->width * pen->customstart->inset, TRUE);
673 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
674 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
675 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
676 pt[j - 1].X, pt[j - 1].Y);
678 break;
679 case PathPointTypeLine:
680 if(pen->startcap == LineCapArrowAnchor)
681 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
682 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
683 pen->width);
684 else if((pen->startcap == LineCapCustom) && pen->customstart)
685 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
686 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
687 pen->customstart->inset * pen->width);
689 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
690 pt[j].X, pt[j].Y, pt[j - 1].X,
691 pt[j - 1].Y);
693 break;
694 default:
695 ERR("Bad path points\n");
696 goto end;
700 transform_and_round_points(graphics, pti, ptcopy, count);
702 for(i = 0; i < count; i++){
703 tp[i] = convert_path_point_type(types[i]);
706 PolyDraw(graphics->hdc, pti, tp, count);
708 status = Ok;
710 end:
711 GdipFree(pti);
712 GdipFree(ptcopy);
713 GdipFree(tp);
715 return status;
718 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
720 return GdipCreateFromHDC2(hdc, NULL, graphics);
723 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
725 GpStatus retval;
727 if(hDevice != NULL) {
728 FIXME("Don't know how to hadle parameter hDevice\n");
729 return NotImplemented;
732 if(hdc == NULL)
733 return OutOfMemory;
735 if(graphics == NULL)
736 return InvalidParameter;
738 *graphics = GdipAlloc(sizeof(GpGraphics));
739 if(!*graphics) return OutOfMemory;
741 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
742 GdipFree(*graphics);
743 return retval;
746 (*graphics)->hdc = hdc;
747 (*graphics)->hwnd = NULL;
748 (*graphics)->smoothing = SmoothingModeDefault;
749 (*graphics)->compqual = CompositingQualityDefault;
750 (*graphics)->interpolation = InterpolationModeDefault;
751 (*graphics)->pixeloffset = PixelOffsetModeDefault;
752 (*graphics)->compmode = CompositingModeSourceOver;
753 (*graphics)->unit = UnitDisplay;
754 (*graphics)->scale = 1.0;
755 (*graphics)->busy = FALSE;
757 return Ok;
760 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
762 GpStatus ret;
764 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
765 return ret;
767 (*graphics)->hwnd = hwnd;
769 return Ok;
772 /* FIXME: no icm handling */
773 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
775 return GdipCreateFromHWND(hwnd, graphics);
778 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
779 GpMetafile **metafile)
781 static int calls;
783 if(!hemf || !metafile)
784 return InvalidParameter;
786 if(!(calls++))
787 FIXME("not implemented\n");
789 return NotImplemented;
792 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
793 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
795 IStream *stream = NULL;
796 UINT read;
797 BYTE* copy;
798 HENHMETAFILE hemf;
799 GpStatus retval = GenericError;
801 if(!hwmf || !metafile || !placeable)
802 return InvalidParameter;
804 *metafile = NULL;
805 read = GetMetaFileBitsEx(hwmf, 0, NULL);
806 if(!read)
807 return GenericError;
808 copy = GdipAlloc(read);
809 GetMetaFileBitsEx(hwmf, read, copy);
811 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
812 GdipFree(copy);
814 read = GetEnhMetaFileBits(hemf, 0, NULL);
815 copy = GdipAlloc(read);
816 GetEnhMetaFileBits(hemf, read, copy);
817 DeleteEnhMetaFile(hemf);
819 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
820 ERR("could not make stream\n");
821 GdipFree(copy);
822 goto err;
825 *metafile = GdipAlloc(sizeof(GpMetafile));
826 if(!*metafile){
827 retval = OutOfMemory;
828 goto err;
831 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
832 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
833 goto err;
836 (*metafile)->image.type = ImageTypeMetafile;
837 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
838 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
839 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
840 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
841 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
842 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
843 (*metafile)->unit = UnitInch;
845 if(delete)
846 DeleteMetaFile(hwmf);
848 return Ok;
850 err:
851 GdipFree(*metafile);
852 IStream_Release(stream);
853 return retval;
856 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
857 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
859 HMETAFILE hmf = GetMetaFileW(file);
861 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
863 if(!hmf) return InvalidParameter;
865 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
868 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
869 UINT access, IStream **stream)
871 DWORD dwMode;
872 HRESULT ret;
874 if(!stream || !filename)
875 return InvalidParameter;
877 if(access & GENERIC_WRITE)
878 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
879 else if(access & GENERIC_READ)
880 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
881 else
882 return InvalidParameter;
884 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
886 return hresult_to_status(ret);
889 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
891 if(!graphics) return InvalidParameter;
892 if(graphics->hwnd)
893 ReleaseDC(graphics->hwnd, graphics->hdc);
895 GdipDeleteMatrix(graphics->worldtrans);
896 GdipFree(graphics);
898 return Ok;
901 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
902 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
904 INT save_state, num_pts;
905 GpPointF points[MAX_ARC_PTS];
906 GpStatus retval;
908 if(!graphics || !pen || width <= 0 || height <= 0)
909 return InvalidParameter;
911 if(graphics->busy)
912 return ObjectBusy;
914 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
916 save_state = prepare_dc(graphics, pen);
918 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
920 restore_dc(graphics, save_state);
922 return retval;
925 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
926 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
928 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
931 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
932 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
934 INT save_state;
935 GpPointF pt[4];
936 GpStatus retval;
938 if(!graphics || !pen)
939 return InvalidParameter;
941 if(graphics->busy)
942 return ObjectBusy;
944 pt[0].X = x1;
945 pt[0].Y = y1;
946 pt[1].X = x2;
947 pt[1].Y = y2;
948 pt[2].X = x3;
949 pt[2].Y = y3;
950 pt[3].X = x4;
951 pt[3].Y = y4;
953 save_state = prepare_dc(graphics, pen);
955 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
957 restore_dc(graphics, save_state);
959 return retval;
962 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
963 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
965 INT save_state;
966 GpPointF pt[4];
967 GpStatus retval;
969 if(!graphics || !pen)
970 return InvalidParameter;
972 if(graphics->busy)
973 return ObjectBusy;
975 pt[0].X = x1;
976 pt[0].Y = y1;
977 pt[1].X = x2;
978 pt[1].Y = y2;
979 pt[2].X = x3;
980 pt[2].Y = y3;
981 pt[3].X = x4;
982 pt[3].Y = y4;
984 save_state = prepare_dc(graphics, pen);
986 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
988 restore_dc(graphics, save_state);
990 return retval;
993 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
994 GDIPCONST GpPointF *points, INT count)
996 INT i;
997 GpStatus ret;
999 if(!graphics || !pen || !points || (count <= 0))
1000 return InvalidParameter;
1002 if(graphics->busy)
1003 return ObjectBusy;
1005 for(i = 0; i < floor(count / 4); i++){
1006 ret = GdipDrawBezier(graphics, pen,
1007 points[4*i].X, points[4*i].Y,
1008 points[4*i + 1].X, points[4*i + 1].Y,
1009 points[4*i + 2].X, points[4*i + 2].Y,
1010 points[4*i + 3].X, points[4*i + 3].Y);
1011 if(ret != Ok)
1012 return ret;
1015 return Ok;
1018 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1019 GDIPCONST GpPoint *points, INT count)
1021 GpPointF *pts;
1022 GpStatus ret;
1023 INT i;
1025 if(!graphics || !pen || !points || (count <= 0))
1026 return InvalidParameter;
1028 if(graphics->busy)
1029 return ObjectBusy;
1031 pts = GdipAlloc(sizeof(GpPointF) * count);
1032 if(!pts)
1033 return OutOfMemory;
1035 for(i = 0; i < count; i++){
1036 pts[i].X = (REAL)points[i].X;
1037 pts[i].Y = (REAL)points[i].Y;
1040 ret = GdipDrawBeziers(graphics,pen,pts,count);
1042 GdipFree(pts);
1044 return ret;
1047 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1048 GDIPCONST GpPointF *points, INT count)
1050 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1053 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1054 GDIPCONST GpPoint *points, INT count)
1056 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1059 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1060 GDIPCONST GpPointF *points, INT count, REAL tension)
1062 GpPointF *ptf;
1063 GpStatus stat;
1065 if(!graphics || !pen || !points || count <= 0)
1066 return InvalidParameter;
1068 if(graphics->busy)
1069 return ObjectBusy;
1071 /* make a full points copy.. */
1072 ptf = GdipAlloc(sizeof(GpPointF)*(count+1));
1073 if(!ptf)
1074 return OutOfMemory;
1075 memcpy(ptf, points, sizeof(GpPointF)*count);
1077 /* ..and add a first point as a last one */
1078 ptf[count] = ptf[0];
1080 stat = GdipDrawCurve2(graphics, pen, ptf, count + 1, tension);
1082 GdipFree(ptf);
1084 return stat;
1087 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1088 GDIPCONST GpPoint *points, INT count, REAL tension)
1090 GpPointF *ptf;
1091 GpStatus stat;
1092 INT i;
1094 if(!points || count <= 0)
1095 return InvalidParameter;
1097 ptf = GdipAlloc(sizeof(GpPointF)*count);
1098 if(!ptf)
1099 return OutOfMemory;
1101 for(i = 0; i < count; i++){
1102 ptf[i].X = (REAL)points[i].X;
1103 ptf[i].Y = (REAL)points[i].Y;
1106 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1108 GdipFree(ptf);
1110 return stat;
1113 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1114 GDIPCONST GpPointF *points, INT count)
1116 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1119 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1120 GDIPCONST GpPoint *points, INT count)
1122 GpPointF *pointsF;
1123 GpStatus ret;
1124 INT i;
1126 if(!points || count <= 0)
1127 return InvalidParameter;
1129 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1130 if(!pointsF)
1131 return OutOfMemory;
1133 for(i = 0; i < count; i++){
1134 pointsF[i].X = (REAL)points[i].X;
1135 pointsF[i].Y = (REAL)points[i].Y;
1138 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1139 GdipFree(pointsF);
1141 return ret;
1144 /* Approximates cardinal spline with Bezier curves. */
1145 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1146 GDIPCONST GpPointF *points, INT count, REAL tension)
1148 /* PolyBezier expects count*3-2 points. */
1149 INT i, len_pt = count*3-2, save_state;
1150 GpPointF *pt;
1151 REAL x1, x2, y1, y2;
1152 GpStatus retval;
1154 if(!graphics || !pen)
1155 return InvalidParameter;
1157 if(graphics->busy)
1158 return ObjectBusy;
1160 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1161 tension = tension * TENSION_CONST;
1163 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1164 tension, &x1, &y1);
1166 pt[0].X = points[0].X;
1167 pt[0].Y = points[0].Y;
1168 pt[1].X = x1;
1169 pt[1].Y = y1;
1171 for(i = 0; i < count-2; i++){
1172 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1174 pt[3*i+2].X = x1;
1175 pt[3*i+2].Y = y1;
1176 pt[3*i+3].X = points[i+1].X;
1177 pt[3*i+3].Y = points[i+1].Y;
1178 pt[3*i+4].X = x2;
1179 pt[3*i+4].Y = y2;
1182 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1183 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1185 pt[len_pt-2].X = x1;
1186 pt[len_pt-2].Y = y1;
1187 pt[len_pt-1].X = points[count-1].X;
1188 pt[len_pt-1].Y = points[count-1].Y;
1190 save_state = prepare_dc(graphics, pen);
1192 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1194 GdipFree(pt);
1195 restore_dc(graphics, save_state);
1197 return retval;
1200 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1201 GDIPCONST GpPoint *points, INT count, REAL tension)
1203 GpPointF *pointsF;
1204 GpStatus ret;
1205 INT i;
1207 if(!points || count <= 0)
1208 return InvalidParameter;
1210 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1211 if(!pointsF)
1212 return OutOfMemory;
1214 for(i = 0; i < count; i++){
1215 pointsF[i].X = (REAL)points[i].X;
1216 pointsF[i].Y = (REAL)points[i].Y;
1219 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1220 GdipFree(pointsF);
1222 return ret;
1225 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1226 REAL y, REAL width, REAL height)
1228 INT save_state;
1229 GpPointF ptf[2];
1230 POINT pti[2];
1232 if(!graphics || !pen)
1233 return InvalidParameter;
1235 if(graphics->busy)
1236 return ObjectBusy;
1238 ptf[0].X = x;
1239 ptf[0].Y = y;
1240 ptf[1].X = x + width;
1241 ptf[1].Y = y + height;
1243 save_state = prepare_dc(graphics, pen);
1244 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1246 transform_and_round_points(graphics, pti, ptf, 2);
1248 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1250 restore_dc(graphics, save_state);
1252 return Ok;
1255 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1256 INT y, INT width, INT height)
1258 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1262 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1264 /* IPicture::Render uses LONG coords */
1265 return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1268 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1269 INT y)
1271 UINT width, height, srcw, srch;
1273 if(!graphics || !image)
1274 return InvalidParameter;
1276 GdipGetImageWidth(image, &width);
1277 GdipGetImageHeight(image, &height);
1279 srcw = width * (((REAL) INCH_HIMETRIC) /
1280 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1281 srch = height * (((REAL) INCH_HIMETRIC) /
1282 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1284 if(image->type != ImageTypeMetafile){
1285 y += height;
1286 height *= -1;
1289 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1290 0, 0, srcw, srch, NULL);
1292 return Ok;
1295 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1296 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1297 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1298 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1299 DrawImageAbort callback, VOID * callbackData)
1301 GpPointF ptf[3];
1302 POINT pti[3];
1303 REAL dx, dy;
1305 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1306 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1307 callbackData);
1309 if(!graphics || !image || !points || count != 3)
1310 return InvalidParameter;
1312 if(srcUnit == UnitInch)
1313 dx = dy = (REAL) INCH_HIMETRIC;
1314 else if(srcUnit == UnitPixel){
1315 dx = ((REAL) INCH_HIMETRIC) /
1316 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1317 dy = ((REAL) INCH_HIMETRIC) /
1318 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1320 else
1321 return NotImplemented;
1323 memcpy(ptf, points, 3 * sizeof(GpPointF));
1324 transform_and_round_points(graphics, pti, ptf, 3);
1326 /* IPicture renders bitmaps with the y-axis reversed
1327 * FIXME: flipping for unknown image type might not be correct. */
1328 if(image->type != ImageTypeMetafile){
1329 INT temp;
1330 temp = pti[0].y;
1331 pti[0].y = pti[2].y;
1332 pti[2].y = temp;
1335 if(IPicture_Render(image->picture, graphics->hdc,
1336 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1337 srcx * dx, srcy * dy,
1338 srcwidth * dx, srcheight * dy,
1339 NULL) != S_OK){
1340 if(callback)
1341 callback(callbackData);
1342 return GenericError;
1345 return Ok;
1348 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1349 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1350 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1351 DrawImageAbort callback, VOID * callbackData)
1353 GpPointF pointsF[3];
1354 INT i;
1356 if(!points || count!=3)
1357 return InvalidParameter;
1359 for(i = 0; i < count; i++){
1360 pointsF[i].X = (REAL)points[i].X;
1361 pointsF[i].Y = (REAL)points[i].Y;
1364 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1365 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1366 callback, callbackData);
1369 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1370 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1371 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1372 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1373 VOID * callbackData)
1375 GpPointF points[3];
1377 points[0].X = dstx;
1378 points[0].Y = dsty;
1379 points[1].X = dstx + dstwidth;
1380 points[1].Y = dsty;
1381 points[2].X = dstx;
1382 points[2].Y = dsty + dstheight;
1384 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1385 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1388 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1389 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1390 INT srcwidth, INT srcheight, GpUnit srcUnit,
1391 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1392 VOID * callbackData)
1394 GpPointF points[3];
1396 points[0].X = dstx;
1397 points[0].Y = dsty;
1398 points[1].X = dstx + dstwidth;
1399 points[1].Y = dsty;
1400 points[2].X = dstx;
1401 points[2].Y = dsty + dstheight;
1403 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1404 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1407 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1408 REAL x, REAL y, REAL width, REAL height)
1410 RectF bounds;
1411 GpUnit unit;
1412 GpStatus ret;
1414 if(!graphics || !image)
1415 return InvalidParameter;
1417 ret = GdipGetImageBounds(image, &bounds, &unit);
1418 if(ret != Ok)
1419 return ret;
1421 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1422 bounds.X, bounds.Y, bounds.Width, bounds.Height,
1423 unit, NULL, NULL, NULL);
1426 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1427 INT x, INT y, INT width, INT height)
1429 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1432 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1433 REAL y1, REAL x2, REAL y2)
1435 INT save_state;
1436 GpPointF pt[2];
1437 GpStatus retval;
1439 if(!pen || !graphics)
1440 return InvalidParameter;
1442 if(graphics->busy)
1443 return ObjectBusy;
1445 pt[0].X = x1;
1446 pt[0].Y = y1;
1447 pt[1].X = x2;
1448 pt[1].Y = y2;
1450 save_state = prepare_dc(graphics, pen);
1452 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1454 restore_dc(graphics, save_state);
1456 return retval;
1459 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1460 INT y1, INT x2, INT y2)
1462 INT save_state;
1463 GpPointF pt[2];
1464 GpStatus retval;
1466 if(!pen || !graphics)
1467 return InvalidParameter;
1469 if(graphics->busy)
1470 return ObjectBusy;
1472 pt[0].X = (REAL)x1;
1473 pt[0].Y = (REAL)y1;
1474 pt[1].X = (REAL)x2;
1475 pt[1].Y = (REAL)y2;
1477 save_state = prepare_dc(graphics, pen);
1479 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1481 restore_dc(graphics, save_state);
1483 return retval;
1486 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1487 GpPointF *points, INT count)
1489 INT save_state;
1490 GpStatus retval;
1492 if(!pen || !graphics || (count < 2))
1493 return InvalidParameter;
1495 if(graphics->busy)
1496 return ObjectBusy;
1498 save_state = prepare_dc(graphics, pen);
1500 retval = draw_polyline(graphics, pen, points, count, TRUE);
1502 restore_dc(graphics, save_state);
1504 return retval;
1507 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
1508 GpPoint *points, INT count)
1510 INT save_state;
1511 GpStatus retval;
1512 GpPointF *ptf = NULL;
1513 int i;
1515 if(!pen || !graphics || (count < 2))
1516 return InvalidParameter;
1518 if(graphics->busy)
1519 return ObjectBusy;
1521 ptf = GdipAlloc(count * sizeof(GpPointF));
1522 if(!ptf) return OutOfMemory;
1524 for(i = 0; i < count; i ++){
1525 ptf[i].X = (REAL) points[i].X;
1526 ptf[i].Y = (REAL) points[i].Y;
1529 save_state = prepare_dc(graphics, pen);
1531 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
1533 restore_dc(graphics, save_state);
1535 GdipFree(ptf);
1536 return retval;
1539 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1541 INT save_state;
1542 GpStatus retval;
1544 if(!pen || !graphics)
1545 return InvalidParameter;
1547 if(graphics->busy)
1548 return ObjectBusy;
1550 save_state = prepare_dc(graphics, pen);
1552 retval = draw_poly(graphics, pen, path->pathdata.Points,
1553 path->pathdata.Types, path->pathdata.Count, TRUE);
1555 restore_dc(graphics, save_state);
1557 return retval;
1560 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1561 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1563 INT save_state;
1565 if(!graphics || !pen)
1566 return InvalidParameter;
1568 if(graphics->busy)
1569 return ObjectBusy;
1571 save_state = prepare_dc(graphics, pen);
1572 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1574 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1576 restore_dc(graphics, save_state);
1578 return Ok;
1581 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
1582 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1584 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1587 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
1588 REAL y, REAL width, REAL height)
1590 INT save_state;
1591 GpPointF ptf[4];
1592 POINT pti[4];
1594 if(!pen || !graphics)
1595 return InvalidParameter;
1597 if(graphics->busy)
1598 return ObjectBusy;
1600 ptf[0].X = x;
1601 ptf[0].Y = y;
1602 ptf[1].X = x + width;
1603 ptf[1].Y = y;
1604 ptf[2].X = x + width;
1605 ptf[2].Y = y + height;
1606 ptf[3].X = x;
1607 ptf[3].Y = y + height;
1609 save_state = prepare_dc(graphics, pen);
1610 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1612 transform_and_round_points(graphics, pti, ptf, 4);
1613 Polygon(graphics->hdc, pti, 4);
1615 restore_dc(graphics, save_state);
1617 return Ok;
1620 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1621 INT y, INT width, INT height)
1623 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1626 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1627 GDIPCONST GpRectF* rects, INT count)
1629 GpPointF *ptf;
1630 POINT *pti;
1631 INT save_state, i;
1633 if(!graphics || !pen || !rects || count < 1)
1634 return InvalidParameter;
1636 if(graphics->busy)
1637 return ObjectBusy;
1639 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1640 pti = GdipAlloc(4 * count * sizeof(POINT));
1642 if(!ptf || !pti){
1643 GdipFree(ptf);
1644 GdipFree(pti);
1645 return OutOfMemory;
1648 for(i = 0; i < count; i++){
1649 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1650 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1651 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1652 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1655 save_state = prepare_dc(graphics, pen);
1656 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1658 transform_and_round_points(graphics, pti, ptf, 4 * count);
1660 for(i = 0; i < count; i++)
1661 Polygon(graphics->hdc, &pti[4 * i], 4);
1663 restore_dc(graphics, save_state);
1665 GdipFree(ptf);
1666 GdipFree(pti);
1668 return Ok;
1671 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
1672 GDIPCONST GpRect* rects, INT count)
1674 GpRectF *rectsF;
1675 GpStatus ret;
1676 INT i;
1678 if(!rects || count<=0)
1679 return InvalidParameter;
1681 rectsF = GdipAlloc(sizeof(GpRectF) * count);
1682 if(!rectsF)
1683 return OutOfMemory;
1685 for(i = 0;i < count;i++){
1686 rectsF[i].X = (REAL)rects[i].X;
1687 rectsF[i].Y = (REAL)rects[i].Y;
1688 rectsF[i].Width = (REAL)rects[i].Width;
1689 rectsF[i].Height = (REAL)rects[i].Height;
1692 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
1693 GdipFree(rectsF);
1695 return ret;
1698 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1699 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1700 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1702 HRGN rgn = NULL;
1703 HFONT gdifont;
1704 LOGFONTW lfw;
1705 TEXTMETRICW textmet;
1706 GpPointF pt[2], rectcpy[4];
1707 POINT corners[4];
1708 WCHAR* stringdup;
1709 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1710 INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1711 nheight;
1712 SIZE size;
1713 RECT drawcoord;
1715 if(!graphics || !string || !font || !brush || !rect)
1716 return InvalidParameter;
1718 if((brush->bt != BrushTypeSolidColor)){
1719 FIXME("not implemented for given parameters\n");
1720 return NotImplemented;
1723 if(format)
1724 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1726 if(length == -1) length = lstrlenW(string);
1728 stringdup = GdipAlloc(length * sizeof(WCHAR));
1729 if(!stringdup) return OutOfMemory;
1731 save_state = SaveDC(graphics->hdc);
1732 SetBkMode(graphics->hdc, TRANSPARENT);
1733 SetTextColor(graphics->hdc, brush->lb.lbColor);
1735 rectcpy[3].X = rectcpy[0].X = rect->X;
1736 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1737 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1738 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1739 transform_and_round_points(graphics, corners, rectcpy, 4);
1741 if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1742 rel_width = rel_height = 1.0;
1743 nwidth = nheight = INT_MAX;
1745 else{
1746 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1747 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1748 / rect->Width;
1749 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1750 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1751 / rect->Height;
1753 nwidth = roundr(rel_width * rect->Width);
1754 nheight = roundr(rel_height * rect->Height);
1755 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1756 SelectClipRgn(graphics->hdc, rgn);
1759 /* Use gdi to find the font, then perform transformations on it (height,
1760 * width, angle). */
1761 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1762 GetTextMetricsW(graphics->hdc, &textmet);
1763 lfw = font->lfw;
1765 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1766 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1768 pt[0].X = 0.0;
1769 pt[0].Y = 0.0;
1770 pt[1].X = 1.0;
1771 pt[1].Y = 0.0;
1772 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1773 angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1774 ang_cos = cos(angle);
1775 ang_sin = sin(angle);
1776 lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1778 gdifont = CreateFontIndirectW(&lfw);
1779 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1781 for(i = 0, j = 0; i < length; i++){
1782 if(!isprintW(string[i]) && (string[i] != '\n'))
1783 continue;
1785 stringdup[j] = string[i];
1786 j++;
1789 stringdup[j] = 0;
1790 length = j;
1792 while(sum < length){
1793 drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1794 drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1796 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1797 nwidth, &fit, NULL, &size);
1798 fitcpy = fit;
1800 if(fit == 0){
1801 DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1802 DT_EXPANDTABS);
1803 break;
1806 for(lret = 0; lret < fit; lret++)
1807 if(*(stringdup + sum + lret) == '\n')
1808 break;
1810 /* Line break code (may look strange, but it imitates windows). */
1811 if(lret < fit)
1812 fit = lret; /* this is not an off-by-one error */
1813 else if(fit < (length - sum)){
1814 if(*(stringdup + sum + fit) == ' ')
1815 while(*(stringdup + sum + fit) == ' ')
1816 fit++;
1817 else
1818 while(*(stringdup + sum + fit - 1) != ' '){
1819 fit--;
1821 if(*(stringdup + sum + fit) == '\t')
1822 break;
1824 if(fit == 0){
1825 fit = fitcpy;
1826 break;
1830 DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1831 &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1833 sum += fit + (lret < fitcpy ? 1 : 0);
1834 height += size.cy;
1836 if(height > nheight)
1837 break;
1839 /* Stop if this was a linewrap (but not if it was a linebreak). */
1840 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1841 break;
1844 GdipFree(stringdup);
1845 DeleteObject(rgn);
1846 DeleteObject(gdifont);
1848 RestoreDC(graphics->hdc, save_state);
1850 return Ok;
1853 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
1854 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
1856 GpPath *path;
1857 GpStatus stat;
1859 if(!graphics || !brush || !points)
1860 return InvalidParameter;
1862 if(graphics->busy)
1863 return ObjectBusy;
1865 stat = GdipCreatePath(fill, &path);
1866 if(stat != Ok)
1867 return stat;
1869 stat = GdipAddPathClosedCurve2(path, points, count, tension);
1870 if(stat != Ok){
1871 GdipDeletePath(path);
1872 return stat;
1875 stat = GdipFillPath(graphics, brush, path);
1876 if(stat != Ok){
1877 GdipDeletePath(path);
1878 return stat;
1881 GdipDeletePath(path);
1883 return Ok;
1886 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
1887 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
1889 GpPointF *ptf;
1890 GpStatus stat;
1891 INT i;
1893 if(!points || count <= 0)
1894 return InvalidParameter;
1896 ptf = GdipAlloc(sizeof(GpPointF)*count);
1897 if(!ptf)
1898 return OutOfMemory;
1900 for(i = 0;i < count;i++){
1901 ptf[i].X = (REAL)points[i].X;
1902 ptf[i].Y = (REAL)points[i].Y;
1905 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
1907 GdipFree(ptf);
1909 return stat;
1912 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
1913 REAL y, REAL width, REAL height)
1915 INT save_state;
1916 GpPointF ptf[2];
1917 POINT pti[2];
1919 if(!graphics || !brush)
1920 return InvalidParameter;
1922 if(graphics->busy)
1923 return ObjectBusy;
1925 ptf[0].X = x;
1926 ptf[0].Y = y;
1927 ptf[1].X = x + width;
1928 ptf[1].Y = y + height;
1930 save_state = SaveDC(graphics->hdc);
1931 EndPath(graphics->hdc);
1932 SelectObject(graphics->hdc, brush->gdibrush);
1933 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1935 transform_and_round_points(graphics, pti, ptf, 2);
1937 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1939 RestoreDC(graphics->hdc, save_state);
1941 return Ok;
1944 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
1945 INT y, INT width, INT height)
1947 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1950 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1952 INT save_state;
1953 GpStatus retval;
1955 if(!brush || !graphics || !path)
1956 return InvalidParameter;
1958 if(graphics->busy)
1959 return ObjectBusy;
1961 save_state = SaveDC(graphics->hdc);
1962 EndPath(graphics->hdc);
1963 SelectObject(graphics->hdc, brush->gdibrush);
1964 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1965 : WINDING));
1967 BeginPath(graphics->hdc);
1968 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1969 path->pathdata.Types, path->pathdata.Count, FALSE);
1971 if(retval != Ok)
1972 goto end;
1974 EndPath(graphics->hdc);
1975 FillPath(graphics->hdc);
1977 retval = Ok;
1979 end:
1980 RestoreDC(graphics->hdc, save_state);
1982 return retval;
1985 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1986 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1988 INT save_state;
1990 if(!graphics || !brush)
1991 return InvalidParameter;
1993 if(graphics->busy)
1994 return ObjectBusy;
1996 save_state = SaveDC(graphics->hdc);
1997 EndPath(graphics->hdc);
1998 SelectObject(graphics->hdc, brush->gdibrush);
1999 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2001 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2003 RestoreDC(graphics->hdc, save_state);
2005 return Ok;
2008 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2009 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2011 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2014 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2015 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2017 INT save_state;
2018 GpPointF *ptf = NULL;
2019 POINT *pti = NULL;
2020 GpStatus retval = Ok;
2022 if(!graphics || !brush || !points || !count)
2023 return InvalidParameter;
2025 if(graphics->busy)
2026 return ObjectBusy;
2028 ptf = GdipAlloc(count * sizeof(GpPointF));
2029 pti = GdipAlloc(count * sizeof(POINT));
2030 if(!ptf || !pti){
2031 retval = OutOfMemory;
2032 goto end;
2035 memcpy(ptf, points, count * sizeof(GpPointF));
2037 save_state = SaveDC(graphics->hdc);
2038 EndPath(graphics->hdc);
2039 SelectObject(graphics->hdc, brush->gdibrush);
2040 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2041 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2042 : WINDING));
2044 transform_and_round_points(graphics, pti, ptf, count);
2045 Polygon(graphics->hdc, pti, count);
2047 RestoreDC(graphics->hdc, save_state);
2049 end:
2050 GdipFree(ptf);
2051 GdipFree(pti);
2053 return retval;
2056 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2057 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2059 INT save_state, i;
2060 GpPointF *ptf = NULL;
2061 POINT *pti = NULL;
2062 GpStatus retval = Ok;
2064 if(!graphics || !brush || !points || !count)
2065 return InvalidParameter;
2067 if(graphics->busy)
2068 return ObjectBusy;
2070 ptf = GdipAlloc(count * sizeof(GpPointF));
2071 pti = GdipAlloc(count * sizeof(POINT));
2072 if(!ptf || !pti){
2073 retval = OutOfMemory;
2074 goto end;
2077 for(i = 0; i < count; i ++){
2078 ptf[i].X = (REAL) points[i].X;
2079 ptf[i].Y = (REAL) points[i].Y;
2082 save_state = SaveDC(graphics->hdc);
2083 EndPath(graphics->hdc);
2084 SelectObject(graphics->hdc, brush->gdibrush);
2085 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2086 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2087 : WINDING));
2089 transform_and_round_points(graphics, pti, ptf, count);
2090 Polygon(graphics->hdc, pti, count);
2092 RestoreDC(graphics->hdc, save_state);
2094 end:
2095 GdipFree(ptf);
2096 GdipFree(pti);
2098 return retval;
2101 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2102 GDIPCONST GpPointF *points, INT count)
2104 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2107 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2108 GDIPCONST GpPoint *points, INT count)
2110 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2113 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2114 REAL x, REAL y, REAL width, REAL height)
2116 INT save_state;
2117 GpPointF ptf[4];
2118 POINT pti[4];
2120 if(!graphics || !brush)
2121 return InvalidParameter;
2123 if(graphics->busy)
2124 return ObjectBusy;
2126 ptf[0].X = x;
2127 ptf[0].Y = y;
2128 ptf[1].X = x + width;
2129 ptf[1].Y = y;
2130 ptf[2].X = x + width;
2131 ptf[2].Y = y + height;
2132 ptf[3].X = x;
2133 ptf[3].Y = y + height;
2135 save_state = SaveDC(graphics->hdc);
2136 EndPath(graphics->hdc);
2137 SelectObject(graphics->hdc, brush->gdibrush);
2138 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2140 transform_and_round_points(graphics, pti, ptf, 4);
2142 Polygon(graphics->hdc, pti, 4);
2144 RestoreDC(graphics->hdc, save_state);
2146 return Ok;
2149 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2150 INT x, INT y, INT width, INT height)
2152 INT save_state;
2153 GpPointF ptf[4];
2154 POINT pti[4];
2156 if(!graphics || !brush)
2157 return InvalidParameter;
2159 if(graphics->busy)
2160 return ObjectBusy;
2162 ptf[0].X = x;
2163 ptf[0].Y = y;
2164 ptf[1].X = x + width;
2165 ptf[1].Y = y;
2166 ptf[2].X = x + width;
2167 ptf[2].Y = y + height;
2168 ptf[3].X = x;
2169 ptf[3].Y = y + height;
2171 save_state = SaveDC(graphics->hdc);
2172 EndPath(graphics->hdc);
2173 SelectObject(graphics->hdc, brush->gdibrush);
2174 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2176 transform_and_round_points(graphics, pti, ptf, 4);
2178 Polygon(graphics->hdc, pti, 4);
2180 RestoreDC(graphics->hdc, save_state);
2182 return Ok;
2185 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2186 INT count)
2188 GpStatus ret;
2189 INT i;
2191 if(!rects)
2192 return InvalidParameter;
2194 for(i = 0; i < count; i++){
2195 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2196 if(ret != Ok) return ret;
2199 return Ok;
2202 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2203 INT count)
2205 GpRectF *rectsF;
2206 GpStatus ret;
2207 INT i;
2209 if(!rects || count <= 0)
2210 return InvalidParameter;
2212 rectsF = GdipAlloc(sizeof(GpRectF)*count);
2213 if(!rectsF)
2214 return OutOfMemory;
2216 for(i = 0; i < count; i++){
2217 rectsF[i].X = (REAL)rects[i].X;
2218 rectsF[i].Y = (REAL)rects[i].Y;
2219 rectsF[i].X = (REAL)rects[i].Width;
2220 rectsF[i].Height = (REAL)rects[i].Height;
2223 ret = GdipFillRectangles(graphics,brush,rectsF,count);
2224 GdipFree(rectsF);
2226 return ret;
2229 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2230 GpRegion* region)
2232 if (!(graphics && brush && region))
2233 return InvalidParameter;
2235 if(graphics->busy)
2236 return ObjectBusy;
2238 FIXME("(%p, %p, %p): stub\n", graphics, brush, region);
2240 return NotImplemented;
2243 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2245 static int calls;
2247 if(!graphics)
2248 return InvalidParameter;
2250 if(graphics->busy)
2251 return ObjectBusy;
2253 if(!(calls++))
2254 FIXME("not implemented\n");
2256 return NotImplemented;
2259 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2260 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2261 CompositingMode *mode)
2263 if(!graphics || !mode)
2264 return InvalidParameter;
2266 if(graphics->busy)
2267 return ObjectBusy;
2269 *mode = graphics->compmode;
2271 return Ok;
2274 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
2275 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
2276 CompositingQuality *quality)
2278 if(!graphics || !quality)
2279 return InvalidParameter;
2281 if(graphics->busy)
2282 return ObjectBusy;
2284 *quality = graphics->compqual;
2286 return Ok;
2289 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
2290 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
2291 InterpolationMode *mode)
2293 if(!graphics || !mode)
2294 return InvalidParameter;
2296 if(graphics->busy)
2297 return ObjectBusy;
2299 *mode = graphics->interpolation;
2301 return Ok;
2304 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
2306 if(!graphics || !scale)
2307 return InvalidParameter;
2309 if(graphics->busy)
2310 return ObjectBusy;
2312 *scale = graphics->scale;
2314 return Ok;
2317 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
2319 if(!graphics || !unit)
2320 return InvalidParameter;
2322 if(graphics->busy)
2323 return ObjectBusy;
2325 *unit = graphics->unit;
2327 return Ok;
2330 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
2331 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2332 *mode)
2334 if(!graphics || !mode)
2335 return InvalidParameter;
2337 if(graphics->busy)
2338 return ObjectBusy;
2340 *mode = graphics->pixeloffset;
2342 return Ok;
2345 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
2346 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
2348 if(!graphics || !mode)
2349 return InvalidParameter;
2351 if(graphics->busy)
2352 return ObjectBusy;
2354 *mode = graphics->smoothing;
2356 return Ok;
2359 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
2360 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
2361 TextRenderingHint *hint)
2363 if(!graphics || !hint)
2364 return InvalidParameter;
2366 if(graphics->busy)
2367 return ObjectBusy;
2369 *hint = graphics->texthint;
2371 return Ok;
2374 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2376 if(!graphics || !matrix)
2377 return InvalidParameter;
2379 if(graphics->busy)
2380 return ObjectBusy;
2382 *matrix = *graphics->worldtrans;
2383 return Ok;
2386 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
2387 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
2388 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
2389 INT regionCount, GpRegion** regions)
2391 if (!(graphics && string && font && layoutRect && stringFormat && regions))
2392 return InvalidParameter;
2394 FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
2395 length, font, layoutRect, stringFormat, regionCount, regions);
2397 return NotImplemented;
2400 /* Find the smallest rectangle that bounds the text when it is printed in rect
2401 * according to the format options listed in format. If rect has 0 width and
2402 * height, then just find the smallest rectangle that bounds the text when it's
2403 * printed at location (rect->X, rect-Y). */
2404 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
2405 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
2406 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
2407 INT *codepointsfitted, INT *linesfilled)
2409 HFONT oldfont;
2410 WCHAR* stringdup;
2411 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
2412 nheight;
2413 SIZE size;
2415 if(!graphics || !string || !font || !rect)
2416 return InvalidParameter;
2418 if(codepointsfitted || linesfilled){
2419 FIXME("not implemented for given parameters\n");
2420 return NotImplemented;
2423 if(format)
2424 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2426 if(length == -1) length = lstrlenW(string);
2428 stringdup = GdipAlloc(length * sizeof(WCHAR));
2429 if(!stringdup) return OutOfMemory;
2431 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2432 nwidth = roundr(rect->Width);
2433 nheight = roundr(rect->Height);
2435 if((nwidth == 0) && (nheight == 0))
2436 nwidth = nheight = INT_MAX;
2438 for(i = 0, j = 0; i < length; i++){
2439 if(!isprintW(string[i]) && (string[i] != '\n'))
2440 continue;
2442 stringdup[j] = string[i];
2443 j++;
2446 stringdup[j] = 0;
2447 length = j;
2449 while(sum < length){
2450 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2451 nwidth, &fit, NULL, &size);
2452 fitcpy = fit;
2454 if(fit == 0)
2455 break;
2457 for(lret = 0; lret < fit; lret++)
2458 if(*(stringdup + sum + lret) == '\n')
2459 break;
2461 /* Line break code (may look strange, but it imitates windows). */
2462 if(lret < fit)
2463 fit = lret; /* this is not an off-by-one error */
2464 else if(fit < (length - sum)){
2465 if(*(stringdup + sum + fit) == ' ')
2466 while(*(stringdup + sum + fit) == ' ')
2467 fit++;
2468 else
2469 while(*(stringdup + sum + fit - 1) != ' '){
2470 fit--;
2472 if(*(stringdup + sum + fit) == '\t')
2473 break;
2475 if(fit == 0){
2476 fit = fitcpy;
2477 break;
2482 GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
2483 nwidth, &j, NULL, &size);
2485 sum += fit + (lret < fitcpy ? 1 : 0);
2486 height += size.cy;
2487 max_width = max(max_width, size.cx);
2489 if(height > nheight)
2490 break;
2492 /* Stop if this was a linewrap (but not if it was a linebreak). */
2493 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2494 break;
2497 bounds->X = rect->X;
2498 bounds->Y = rect->Y;
2499 bounds->Width = (REAL)max_width;
2500 bounds->Height = (REAL) min(height, nheight);
2502 GdipFree(stringdup);
2503 DeleteObject(SelectObject(graphics->hdc, oldfont));
2505 return Ok;
2508 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
2510 if(!graphics)
2511 return InvalidParameter;
2513 if(graphics->busy)
2514 return ObjectBusy;
2516 graphics->worldtrans->matrix[0] = 1.0;
2517 graphics->worldtrans->matrix[1] = 0.0;
2518 graphics->worldtrans->matrix[2] = 0.0;
2519 graphics->worldtrans->matrix[3] = 1.0;
2520 graphics->worldtrans->matrix[4] = 0.0;
2521 graphics->worldtrans->matrix[5] = 0.0;
2523 return Ok;
2526 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
2528 static int calls;
2530 if(!graphics)
2531 return InvalidParameter;
2533 if(!(calls++))
2534 FIXME("graphics state not implemented\n");
2536 return NotImplemented;
2539 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
2540 GpMatrixOrder order)
2542 if(!graphics)
2543 return InvalidParameter;
2545 if(graphics->busy)
2546 return ObjectBusy;
2548 return GdipRotateMatrix(graphics->worldtrans, angle, order);
2551 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
2553 static int calls;
2555 if(!graphics || !state)
2556 return InvalidParameter;
2558 if(!(calls++))
2559 FIXME("graphics state not implemented\n");
2561 return NotImplemented;
2564 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
2565 REAL sy, GpMatrixOrder order)
2567 if(!graphics)
2568 return InvalidParameter;
2570 if(graphics->busy)
2571 return ObjectBusy;
2573 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
2576 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
2577 CompositingMode mode)
2579 if(!graphics)
2580 return InvalidParameter;
2582 if(graphics->busy)
2583 return ObjectBusy;
2585 graphics->compmode = mode;
2587 return Ok;
2590 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
2591 CompositingQuality quality)
2593 if(!graphics)
2594 return InvalidParameter;
2596 if(graphics->busy)
2597 return ObjectBusy;
2599 graphics->compqual = quality;
2601 return Ok;
2604 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
2605 InterpolationMode mode)
2607 if(!graphics)
2608 return InvalidParameter;
2610 if(graphics->busy)
2611 return ObjectBusy;
2613 graphics->interpolation = mode;
2615 return Ok;
2618 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
2620 if(!graphics || (scale <= 0.0))
2621 return InvalidParameter;
2623 if(graphics->busy)
2624 return ObjectBusy;
2626 graphics->scale = scale;
2628 return Ok;
2631 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
2633 if(!graphics)
2634 return InvalidParameter;
2636 if(graphics->busy)
2637 return ObjectBusy;
2639 if(unit == UnitWorld)
2640 return InvalidParameter;
2642 graphics->unit = unit;
2644 return Ok;
2647 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2648 mode)
2650 if(!graphics)
2651 return InvalidParameter;
2653 if(graphics->busy)
2654 return ObjectBusy;
2656 graphics->pixeloffset = mode;
2658 return Ok;
2661 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
2663 if(!graphics)
2664 return InvalidParameter;
2666 if(graphics->busy)
2667 return ObjectBusy;
2669 graphics->smoothing = mode;
2671 return Ok;
2674 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
2675 TextRenderingHint hint)
2677 if(!graphics)
2678 return InvalidParameter;
2680 if(graphics->busy)
2681 return ObjectBusy;
2683 graphics->texthint = hint;
2685 return Ok;
2688 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2690 if(!graphics || !matrix)
2691 return InvalidParameter;
2693 if(graphics->busy)
2694 return ObjectBusy;
2696 GdipDeleteMatrix(graphics->worldtrans);
2697 return GdipCloneMatrix(matrix, &graphics->worldtrans);
2700 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
2701 REAL dy, GpMatrixOrder order)
2703 if(!graphics)
2704 return InvalidParameter;
2706 if(graphics->busy)
2707 return ObjectBusy;
2709 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
2712 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
2713 INT width, INT height,
2714 CombineMode combineMode)
2716 static int calls;
2718 if(!graphics)
2719 return InvalidParameter;
2721 if(graphics->busy)
2722 return ObjectBusy;
2724 if(!(calls++))
2725 FIXME("not implemented\n");
2727 return NotImplemented;
2730 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
2731 CombineMode combineMode)
2733 static int calls;
2735 if(!(calls++))
2736 FIXME("not implemented\n");
2738 return NotImplemented;
2741 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
2742 UINT limitDpi)
2744 static int calls;
2746 if(!(calls++))
2747 FIXME("not implemented\n");
2749 return NotImplemented;
2752 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
2753 INT count)
2755 INT save_state;
2756 POINT *pti;
2758 if(!graphics || !pen || count<=0)
2759 return InvalidParameter;
2761 if(graphics->busy)
2762 return ObjectBusy;
2764 pti = GdipAlloc(sizeof(POINT) * count);
2766 save_state = prepare_dc(graphics, pen);
2767 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2769 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
2770 Polygon(graphics->hdc, pti, count);
2772 restore_dc(graphics, save_state);
2773 GdipFree(pti);
2775 return Ok;
2778 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
2779 INT count)
2781 GpStatus ret;
2782 GpPointF *ptf;
2783 INT i;
2785 if(count<=0) return InvalidParameter;
2786 ptf = GdipAlloc(sizeof(GpPointF) * count);
2788 for(i = 0;i < count; i++){
2789 ptf[i].X = (REAL)points[i].X;
2790 ptf[i].Y = (REAL)points[i].Y;
2793 ret = GdipDrawPolygon(graphics,pen,ptf,count);
2794 GdipFree(ptf);
2796 return ret;
2799 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
2801 if(!graphics || !dpi)
2802 return InvalidParameter;
2804 if(graphics->busy)
2805 return ObjectBusy;
2807 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
2809 return Ok;
2812 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
2814 if(!graphics || !dpi)
2815 return InvalidParameter;
2817 if(graphics->busy)
2818 return ObjectBusy;
2820 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
2822 return Ok;
2825 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
2826 GpMatrixOrder order)
2828 GpMatrix m;
2829 GpStatus ret;
2831 if(!graphics || !matrix)
2832 return InvalidParameter;
2834 if(graphics->busy)
2835 return ObjectBusy;
2837 m = *(graphics->worldtrans);
2839 ret = GdipMultiplyMatrix(&m, (GpMatrix*)matrix, order);
2840 if(ret == Ok)
2841 *(graphics->worldtrans) = m;
2843 return ret;
2846 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
2848 if(!graphics || !hdc)
2849 return InvalidParameter;
2851 if(graphics->busy)
2852 return ObjectBusy;
2854 *hdc = graphics->hdc;
2855 graphics->busy = TRUE;
2857 return Ok;
2860 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
2862 if(!graphics)
2863 return InvalidParameter;
2865 if(graphics->hdc != hdc || !(graphics->busy))
2866 return InvalidParameter;
2868 graphics->busy = FALSE;
2870 return Ok;
2873 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
2875 if(!graphics || !region)
2876 return InvalidParameter;
2878 if(graphics->busy)
2879 return ObjectBusy;
2881 FIXME("(%p, %p): stub\n", graphics, region);
2882 return NotImplemented;
2885 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
2886 GpCoordinateSpace src_space, GpPointF *points, INT count)
2888 FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
2890 return NotImplemented;
2893 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
2894 GpCoordinateSpace src_space, GpPoint *points, INT count)
2896 FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
2898 return NotImplemented;