push ada2d4f150af430b0cf01adcbe8b6d3e34deeeb1
[wine/hacks.git] / dlls / gdiplus / graphics.c
blobfa90d73294acfdbdcd43547723eb2b4dbb042000
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 TRACE("(%p, %p)\n", hdc, graphics);
722 return GdipCreateFromHDC2(hdc, NULL, graphics);
725 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
727 GpStatus retval;
729 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
731 if(hDevice != NULL) {
732 FIXME("Don't know how to hadle parameter hDevice\n");
733 return NotImplemented;
736 if(hdc == NULL)
737 return OutOfMemory;
739 if(graphics == NULL)
740 return InvalidParameter;
742 *graphics = GdipAlloc(sizeof(GpGraphics));
743 if(!*graphics) return OutOfMemory;
745 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
746 GdipFree(*graphics);
747 return retval;
750 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
751 GdipFree((*graphics)->worldtrans);
752 GdipFree(*graphics);
753 return retval;
756 (*graphics)->hdc = hdc;
757 (*graphics)->hwnd = WindowFromDC(hdc);
758 (*graphics)->smoothing = SmoothingModeDefault;
759 (*graphics)->compqual = CompositingQualityDefault;
760 (*graphics)->interpolation = InterpolationModeDefault;
761 (*graphics)->pixeloffset = PixelOffsetModeDefault;
762 (*graphics)->compmode = CompositingModeSourceOver;
763 (*graphics)->unit = UnitDisplay;
764 (*graphics)->scale = 1.0;
765 (*graphics)->busy = FALSE;
766 (*graphics)->textcontrast = 4;
768 return Ok;
771 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
773 GpStatus ret;
775 TRACE("(%p, %p)\n", hwnd, graphics);
777 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
778 return ret;
780 (*graphics)->hwnd = hwnd;
782 return Ok;
785 /* FIXME: no icm handling */
786 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
788 TRACE("(%p, %p)\n", hwnd, graphics);
790 return GdipCreateFromHWND(hwnd, graphics);
793 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
794 GpMetafile **metafile)
796 static int calls;
798 if(!hemf || !metafile)
799 return InvalidParameter;
801 if(!(calls++))
802 FIXME("not implemented\n");
804 return NotImplemented;
807 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
808 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
810 IStream *stream = NULL;
811 UINT read;
812 BYTE* copy;
813 HENHMETAFILE hemf;
814 GpStatus retval = GenericError;
816 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
818 if(!hwmf || !metafile || !placeable)
819 return InvalidParameter;
821 *metafile = NULL;
822 read = GetMetaFileBitsEx(hwmf, 0, NULL);
823 if(!read)
824 return GenericError;
825 copy = GdipAlloc(read);
826 GetMetaFileBitsEx(hwmf, read, copy);
828 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
829 GdipFree(copy);
831 read = GetEnhMetaFileBits(hemf, 0, NULL);
832 copy = GdipAlloc(read);
833 GetEnhMetaFileBits(hemf, read, copy);
834 DeleteEnhMetaFile(hemf);
836 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
837 ERR("could not make stream\n");
838 GdipFree(copy);
839 goto err;
842 *metafile = GdipAlloc(sizeof(GpMetafile));
843 if(!*metafile){
844 retval = OutOfMemory;
845 goto err;
848 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
849 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
850 goto err;
853 (*metafile)->image.type = ImageTypeMetafile;
854 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
855 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
856 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
857 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
858 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
859 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
860 (*metafile)->unit = UnitInch;
862 if(delete)
863 DeleteMetaFile(hwmf);
865 return Ok;
867 err:
868 GdipFree(*metafile);
869 IStream_Release(stream);
870 return retval;
873 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
874 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
876 HMETAFILE hmf = GetMetaFileW(file);
878 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
880 if(!hmf) return InvalidParameter;
882 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
885 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
886 UINT access, IStream **stream)
888 DWORD dwMode;
889 HRESULT ret;
891 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
893 if(!stream || !filename)
894 return InvalidParameter;
896 if(access & GENERIC_WRITE)
897 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
898 else if(access & GENERIC_READ)
899 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
900 else
901 return InvalidParameter;
903 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
905 return hresult_to_status(ret);
908 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
910 TRACE("(%p)\n", graphics);
912 if(!graphics) return InvalidParameter;
913 if(graphics->busy) return ObjectBusy;
915 if(graphics->hwnd)
916 ReleaseDC(graphics->hwnd, graphics->hdc);
918 GdipDeleteRegion(graphics->clip);
919 GdipDeleteMatrix(graphics->worldtrans);
920 GdipFree(graphics);
922 return Ok;
925 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
926 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
928 INT save_state, num_pts;
929 GpPointF points[MAX_ARC_PTS];
930 GpStatus retval;
932 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
933 width, height, startAngle, sweepAngle);
935 if(!graphics || !pen || width <= 0 || height <= 0)
936 return InvalidParameter;
938 if(graphics->busy)
939 return ObjectBusy;
941 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
943 save_state = prepare_dc(graphics, pen);
945 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
947 restore_dc(graphics, save_state);
949 return retval;
952 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
953 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
955 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
956 width, height, startAngle, sweepAngle);
958 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
961 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
962 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
964 INT save_state;
965 GpPointF pt[4];
966 GpStatus retval;
968 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
969 x2, y2, x3, y3, x4, y4);
971 if(!graphics || !pen)
972 return InvalidParameter;
974 if(graphics->busy)
975 return ObjectBusy;
977 pt[0].X = x1;
978 pt[0].Y = y1;
979 pt[1].X = x2;
980 pt[1].Y = y2;
981 pt[2].X = x3;
982 pt[2].Y = y3;
983 pt[3].X = x4;
984 pt[3].Y = y4;
986 save_state = prepare_dc(graphics, pen);
988 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
990 restore_dc(graphics, save_state);
992 return retval;
995 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
996 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
998 INT save_state;
999 GpPointF pt[4];
1000 GpStatus retval;
1002 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1003 x2, y2, x3, y3, x4, y4);
1005 if(!graphics || !pen)
1006 return InvalidParameter;
1008 if(graphics->busy)
1009 return ObjectBusy;
1011 pt[0].X = x1;
1012 pt[0].Y = y1;
1013 pt[1].X = x2;
1014 pt[1].Y = y2;
1015 pt[2].X = x3;
1016 pt[2].Y = y3;
1017 pt[3].X = x4;
1018 pt[3].Y = y4;
1020 save_state = prepare_dc(graphics, pen);
1022 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1024 restore_dc(graphics, save_state);
1026 return retval;
1029 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1030 GDIPCONST GpPointF *points, INT count)
1032 INT i;
1033 GpStatus ret;
1035 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1037 if(!graphics || !pen || !points || (count <= 0))
1038 return InvalidParameter;
1040 if(graphics->busy)
1041 return ObjectBusy;
1043 for(i = 0; i < floor(count / 4); i++){
1044 ret = GdipDrawBezier(graphics, pen,
1045 points[4*i].X, points[4*i].Y,
1046 points[4*i + 1].X, points[4*i + 1].Y,
1047 points[4*i + 2].X, points[4*i + 2].Y,
1048 points[4*i + 3].X, points[4*i + 3].Y);
1049 if(ret != Ok)
1050 return ret;
1053 return Ok;
1056 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1057 GDIPCONST GpPoint *points, INT count)
1059 GpPointF *pts;
1060 GpStatus ret;
1061 INT i;
1063 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1065 if(!graphics || !pen || !points || (count <= 0))
1066 return InvalidParameter;
1068 if(graphics->busy)
1069 return ObjectBusy;
1071 pts = GdipAlloc(sizeof(GpPointF) * count);
1072 if(!pts)
1073 return OutOfMemory;
1075 for(i = 0; i < count; i++){
1076 pts[i].X = (REAL)points[i].X;
1077 pts[i].Y = (REAL)points[i].Y;
1080 ret = GdipDrawBeziers(graphics,pen,pts,count);
1082 GdipFree(pts);
1084 return ret;
1087 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1088 GDIPCONST GpPointF *points, INT count)
1090 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1092 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1095 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1096 GDIPCONST GpPoint *points, INT count)
1098 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1100 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1103 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1104 GDIPCONST GpPointF *points, INT count, REAL tension)
1106 GpPath *path;
1107 GpStatus stat;
1109 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1111 if(!graphics || !pen || !points || count <= 0)
1112 return InvalidParameter;
1114 if(graphics->busy)
1115 return ObjectBusy;
1117 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1118 return stat;
1120 stat = GdipAddPathClosedCurve2(path, points, count, tension);
1121 if(stat != Ok){
1122 GdipDeletePath(path);
1123 return stat;
1126 stat = GdipDrawPath(graphics, pen, path);
1128 GdipDeletePath(path);
1130 return stat;
1133 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1134 GDIPCONST GpPoint *points, INT count, REAL tension)
1136 GpPointF *ptf;
1137 GpStatus stat;
1138 INT i;
1140 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1142 if(!points || count <= 0)
1143 return InvalidParameter;
1145 ptf = GdipAlloc(sizeof(GpPointF)*count);
1146 if(!ptf)
1147 return OutOfMemory;
1149 for(i = 0; i < count; i++){
1150 ptf[i].X = (REAL)points[i].X;
1151 ptf[i].Y = (REAL)points[i].Y;
1154 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1156 GdipFree(ptf);
1158 return stat;
1161 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1162 GDIPCONST GpPointF *points, INT count)
1164 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1166 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1169 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1170 GDIPCONST GpPoint *points, INT count)
1172 GpPointF *pointsF;
1173 GpStatus ret;
1174 INT i;
1176 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1178 if(!points || count <= 0)
1179 return InvalidParameter;
1181 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1182 if(!pointsF)
1183 return OutOfMemory;
1185 for(i = 0; i < count; i++){
1186 pointsF[i].X = (REAL)points[i].X;
1187 pointsF[i].Y = (REAL)points[i].Y;
1190 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1191 GdipFree(pointsF);
1193 return ret;
1196 /* Approximates cardinal spline with Bezier curves. */
1197 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1198 GDIPCONST GpPointF *points, INT count, REAL tension)
1200 /* PolyBezier expects count*3-2 points. */
1201 INT i, len_pt = count*3-2, save_state;
1202 GpPointF *pt;
1203 REAL x1, x2, y1, y2;
1204 GpStatus retval;
1206 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1208 if(!graphics || !pen)
1209 return InvalidParameter;
1211 if(graphics->busy)
1212 return ObjectBusy;
1214 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1215 tension = tension * TENSION_CONST;
1217 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1218 tension, &x1, &y1);
1220 pt[0].X = points[0].X;
1221 pt[0].Y = points[0].Y;
1222 pt[1].X = x1;
1223 pt[1].Y = y1;
1225 for(i = 0; i < count-2; i++){
1226 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1228 pt[3*i+2].X = x1;
1229 pt[3*i+2].Y = y1;
1230 pt[3*i+3].X = points[i+1].X;
1231 pt[3*i+3].Y = points[i+1].Y;
1232 pt[3*i+4].X = x2;
1233 pt[3*i+4].Y = y2;
1236 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1237 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1239 pt[len_pt-2].X = x1;
1240 pt[len_pt-2].Y = y1;
1241 pt[len_pt-1].X = points[count-1].X;
1242 pt[len_pt-1].Y = points[count-1].Y;
1244 save_state = prepare_dc(graphics, pen);
1246 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1248 GdipFree(pt);
1249 restore_dc(graphics, save_state);
1251 return retval;
1254 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1255 GDIPCONST GpPoint *points, INT count, REAL tension)
1257 GpPointF *pointsF;
1258 GpStatus ret;
1259 INT i;
1261 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1263 if(!points || count <= 0)
1264 return InvalidParameter;
1266 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1267 if(!pointsF)
1268 return OutOfMemory;
1270 for(i = 0; i < count; i++){
1271 pointsF[i].X = (REAL)points[i].X;
1272 pointsF[i].Y = (REAL)points[i].Y;
1275 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1276 GdipFree(pointsF);
1278 return ret;
1281 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1282 REAL y, REAL width, REAL height)
1284 INT save_state;
1285 GpPointF ptf[2];
1286 POINT pti[2];
1288 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1290 if(!graphics || !pen)
1291 return InvalidParameter;
1293 if(graphics->busy)
1294 return ObjectBusy;
1296 ptf[0].X = x;
1297 ptf[0].Y = y;
1298 ptf[1].X = x + width;
1299 ptf[1].Y = y + height;
1301 save_state = prepare_dc(graphics, pen);
1302 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1304 transform_and_round_points(graphics, pti, ptf, 2);
1306 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1308 restore_dc(graphics, save_state);
1310 return Ok;
1313 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1314 INT y, INT width, INT height)
1316 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1318 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1322 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1324 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1326 /* IPicture::Render uses LONG coords */
1327 return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1330 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1331 INT y)
1333 UINT width, height, srcw, srch;
1335 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1337 if(!graphics || !image)
1338 return InvalidParameter;
1340 GdipGetImageWidth(image, &width);
1341 GdipGetImageHeight(image, &height);
1343 srcw = width * (((REAL) INCH_HIMETRIC) /
1344 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1345 srch = height * (((REAL) INCH_HIMETRIC) /
1346 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1348 if(image->type != ImageTypeMetafile){
1349 y += height;
1350 height *= -1;
1353 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1354 0, 0, srcw, srch, NULL);
1356 return Ok;
1359 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1360 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1361 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1362 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1363 DrawImageAbort callback, VOID * callbackData)
1365 GpPointF ptf[3];
1366 POINT pti[3];
1367 REAL dx, dy;
1369 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1370 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1371 callbackData);
1373 if(!graphics || !image || !points || count != 3)
1374 return InvalidParameter;
1376 if(srcUnit == UnitInch)
1377 dx = dy = (REAL) INCH_HIMETRIC;
1378 else if(srcUnit == UnitPixel){
1379 dx = ((REAL) INCH_HIMETRIC) /
1380 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1381 dy = ((REAL) INCH_HIMETRIC) /
1382 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1384 else
1385 return NotImplemented;
1387 memcpy(ptf, points, 3 * sizeof(GpPointF));
1388 transform_and_round_points(graphics, pti, ptf, 3);
1390 /* IPicture renders bitmaps with the y-axis reversed
1391 * FIXME: flipping for unknown image type might not be correct. */
1392 if(image->type != ImageTypeMetafile){
1393 INT temp;
1394 temp = pti[0].y;
1395 pti[0].y = pti[2].y;
1396 pti[2].y = temp;
1399 if(IPicture_Render(image->picture, graphics->hdc,
1400 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1401 srcx * dx, srcy * dy,
1402 srcwidth * dx, srcheight * dy,
1403 NULL) != S_OK){
1404 if(callback)
1405 callback(callbackData);
1406 return GenericError;
1409 return Ok;
1412 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1413 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1414 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1415 DrawImageAbort callback, VOID * callbackData)
1417 GpPointF pointsF[3];
1418 INT i;
1420 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
1421 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1422 callbackData);
1424 if(!points || count!=3)
1425 return InvalidParameter;
1427 for(i = 0; i < count; i++){
1428 pointsF[i].X = (REAL)points[i].X;
1429 pointsF[i].Y = (REAL)points[i].Y;
1432 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1433 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1434 callback, callbackData);
1437 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1438 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1439 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1440 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1441 VOID * callbackData)
1443 GpPointF points[3];
1445 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
1446 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1447 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1449 points[0].X = dstx;
1450 points[0].Y = dsty;
1451 points[1].X = dstx + dstwidth;
1452 points[1].Y = dsty;
1453 points[2].X = dstx;
1454 points[2].Y = dsty + dstheight;
1456 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1457 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1460 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1461 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1462 INT srcwidth, INT srcheight, GpUnit srcUnit,
1463 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1464 VOID * callbackData)
1466 GpPointF points[3];
1468 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
1469 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1470 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1472 points[0].X = dstx;
1473 points[0].Y = dsty;
1474 points[1].X = dstx + dstwidth;
1475 points[1].Y = dsty;
1476 points[2].X = dstx;
1477 points[2].Y = dsty + dstheight;
1479 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1480 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1483 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1484 REAL x, REAL y, REAL width, REAL height)
1486 RectF bounds;
1487 GpUnit unit;
1488 GpStatus ret;
1490 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
1492 if(!graphics || !image)
1493 return InvalidParameter;
1495 ret = GdipGetImageBounds(image, &bounds, &unit);
1496 if(ret != Ok)
1497 return ret;
1499 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1500 bounds.X, bounds.Y, bounds.Width, bounds.Height,
1501 unit, NULL, NULL, NULL);
1504 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1505 INT x, INT y, INT width, INT height)
1507 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
1509 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1512 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1513 REAL y1, REAL x2, REAL y2)
1515 INT save_state;
1516 GpPointF pt[2];
1517 GpStatus retval;
1519 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
1521 if(!pen || !graphics)
1522 return InvalidParameter;
1524 if(graphics->busy)
1525 return ObjectBusy;
1527 pt[0].X = x1;
1528 pt[0].Y = y1;
1529 pt[1].X = x2;
1530 pt[1].Y = y2;
1532 save_state = prepare_dc(graphics, pen);
1534 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1536 restore_dc(graphics, save_state);
1538 return retval;
1541 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1542 INT y1, INT x2, INT y2)
1544 INT save_state;
1545 GpPointF pt[2];
1546 GpStatus retval;
1548 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
1550 if(!pen || !graphics)
1551 return InvalidParameter;
1553 if(graphics->busy)
1554 return ObjectBusy;
1556 pt[0].X = (REAL)x1;
1557 pt[0].Y = (REAL)y1;
1558 pt[1].X = (REAL)x2;
1559 pt[1].Y = (REAL)y2;
1561 save_state = prepare_dc(graphics, pen);
1563 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1565 restore_dc(graphics, save_state);
1567 return retval;
1570 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1571 GpPointF *points, INT count)
1573 INT save_state;
1574 GpStatus retval;
1576 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1578 if(!pen || !graphics || (count < 2))
1579 return InvalidParameter;
1581 if(graphics->busy)
1582 return ObjectBusy;
1584 save_state = prepare_dc(graphics, pen);
1586 retval = draw_polyline(graphics, pen, points, count, TRUE);
1588 restore_dc(graphics, save_state);
1590 return retval;
1593 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
1594 GpPoint *points, INT count)
1596 INT save_state;
1597 GpStatus retval;
1598 GpPointF *ptf = NULL;
1599 int i;
1601 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1603 if(!pen || !graphics || (count < 2))
1604 return InvalidParameter;
1606 if(graphics->busy)
1607 return ObjectBusy;
1609 ptf = GdipAlloc(count * sizeof(GpPointF));
1610 if(!ptf) return OutOfMemory;
1612 for(i = 0; i < count; i ++){
1613 ptf[i].X = (REAL) points[i].X;
1614 ptf[i].Y = (REAL) points[i].Y;
1617 save_state = prepare_dc(graphics, pen);
1619 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
1621 restore_dc(graphics, save_state);
1623 GdipFree(ptf);
1624 return retval;
1627 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1629 INT save_state;
1630 GpStatus retval;
1632 TRACE("(%p, %p, %p)\n", graphics, pen, path);
1634 if(!pen || !graphics)
1635 return InvalidParameter;
1637 if(graphics->busy)
1638 return ObjectBusy;
1640 save_state = prepare_dc(graphics, pen);
1642 retval = draw_poly(graphics, pen, path->pathdata.Points,
1643 path->pathdata.Types, path->pathdata.Count, TRUE);
1645 restore_dc(graphics, save_state);
1647 return retval;
1650 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1651 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1653 INT save_state;
1655 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1656 width, height, startAngle, sweepAngle);
1658 if(!graphics || !pen)
1659 return InvalidParameter;
1661 if(graphics->busy)
1662 return ObjectBusy;
1664 save_state = prepare_dc(graphics, pen);
1665 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1667 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1669 restore_dc(graphics, save_state);
1671 return Ok;
1674 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
1675 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1677 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1678 width, height, startAngle, sweepAngle);
1680 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1683 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
1684 REAL y, REAL width, REAL height)
1686 INT save_state;
1687 GpPointF ptf[4];
1688 POINT pti[4];
1690 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1692 if(!pen || !graphics)
1693 return InvalidParameter;
1695 if(graphics->busy)
1696 return ObjectBusy;
1698 ptf[0].X = x;
1699 ptf[0].Y = y;
1700 ptf[1].X = x + width;
1701 ptf[1].Y = y;
1702 ptf[2].X = x + width;
1703 ptf[2].Y = y + height;
1704 ptf[3].X = x;
1705 ptf[3].Y = y + height;
1707 save_state = prepare_dc(graphics, pen);
1708 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1710 transform_and_round_points(graphics, pti, ptf, 4);
1711 Polygon(graphics->hdc, pti, 4);
1713 restore_dc(graphics, save_state);
1715 return Ok;
1718 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1719 INT y, INT width, INT height)
1721 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1723 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1726 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1727 GDIPCONST GpRectF* rects, INT count)
1729 GpPointF *ptf;
1730 POINT *pti;
1731 INT save_state, i;
1733 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
1735 if(!graphics || !pen || !rects || count < 1)
1736 return InvalidParameter;
1738 if(graphics->busy)
1739 return ObjectBusy;
1741 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1742 pti = GdipAlloc(4 * count * sizeof(POINT));
1744 if(!ptf || !pti){
1745 GdipFree(ptf);
1746 GdipFree(pti);
1747 return OutOfMemory;
1750 for(i = 0; i < count; i++){
1751 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1752 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1753 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1754 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1757 save_state = prepare_dc(graphics, pen);
1758 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1760 transform_and_round_points(graphics, pti, ptf, 4 * count);
1762 for(i = 0; i < count; i++)
1763 Polygon(graphics->hdc, &pti[4 * i], 4);
1765 restore_dc(graphics, save_state);
1767 GdipFree(ptf);
1768 GdipFree(pti);
1770 return Ok;
1773 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
1774 GDIPCONST GpRect* rects, INT count)
1776 GpRectF *rectsF;
1777 GpStatus ret;
1778 INT i;
1780 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
1782 if(!rects || count<=0)
1783 return InvalidParameter;
1785 rectsF = GdipAlloc(sizeof(GpRectF) * count);
1786 if(!rectsF)
1787 return OutOfMemory;
1789 for(i = 0;i < count;i++){
1790 rectsF[i].X = (REAL)rects[i].X;
1791 rectsF[i].Y = (REAL)rects[i].Y;
1792 rectsF[i].Width = (REAL)rects[i].Width;
1793 rectsF[i].Height = (REAL)rects[i].Height;
1796 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
1797 GdipFree(rectsF);
1799 return ret;
1802 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1803 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1804 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1806 HRGN rgn = NULL;
1807 HFONT gdifont;
1808 LOGFONTW lfw;
1809 TEXTMETRICW textmet;
1810 GpPointF pt[2], rectcpy[4];
1811 POINT corners[4];
1812 WCHAR* stringdup;
1813 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1814 INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1815 nheight;
1816 SIZE size;
1817 RECT drawcoord;
1819 if(!graphics || !string || !font || !brush || !rect)
1820 return InvalidParameter;
1822 if((brush->bt != BrushTypeSolidColor)){
1823 FIXME("not implemented for given parameters\n");
1824 return NotImplemented;
1827 if(format)
1828 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1830 if(length == -1) length = lstrlenW(string);
1832 stringdup = GdipAlloc(length * sizeof(WCHAR));
1833 if(!stringdup) return OutOfMemory;
1835 save_state = SaveDC(graphics->hdc);
1836 SetBkMode(graphics->hdc, TRANSPARENT);
1837 SetTextColor(graphics->hdc, brush->lb.lbColor);
1839 rectcpy[3].X = rectcpy[0].X = rect->X;
1840 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1841 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1842 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1843 transform_and_round_points(graphics, corners, rectcpy, 4);
1845 if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1846 rel_width = rel_height = 1.0;
1847 nwidth = nheight = INT_MAX;
1849 else{
1850 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1851 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1852 / rect->Width;
1853 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1854 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1855 / rect->Height;
1857 nwidth = roundr(rel_width * rect->Width);
1858 nheight = roundr(rel_height * rect->Height);
1859 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1860 SelectClipRgn(graphics->hdc, rgn);
1863 /* Use gdi to find the font, then perform transformations on it (height,
1864 * width, angle). */
1865 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1866 GetTextMetricsW(graphics->hdc, &textmet);
1867 lfw = font->lfw;
1869 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1870 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1872 pt[0].X = 0.0;
1873 pt[0].Y = 0.0;
1874 pt[1].X = 1.0;
1875 pt[1].Y = 0.0;
1876 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1877 angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1878 ang_cos = cos(angle);
1879 ang_sin = sin(angle);
1880 lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1882 gdifont = CreateFontIndirectW(&lfw);
1883 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1885 for(i = 0, j = 0; i < length; i++){
1886 if(!isprintW(string[i]) && (string[i] != '\n'))
1887 continue;
1889 stringdup[j] = string[i];
1890 j++;
1893 stringdup[j] = 0;
1894 length = j;
1896 while(sum < length){
1897 drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1898 drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1900 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1901 nwidth, &fit, NULL, &size);
1902 fitcpy = fit;
1904 if(fit == 0){
1905 DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1906 DT_EXPANDTABS);
1907 break;
1910 for(lret = 0; lret < fit; lret++)
1911 if(*(stringdup + sum + lret) == '\n')
1912 break;
1914 /* Line break code (may look strange, but it imitates windows). */
1915 if(lret < fit)
1916 fit = lret; /* this is not an off-by-one error */
1917 else if(fit < (length - sum)){
1918 if(*(stringdup + sum + fit) == ' ')
1919 while(*(stringdup + sum + fit) == ' ')
1920 fit++;
1921 else
1922 while(*(stringdup + sum + fit - 1) != ' '){
1923 fit--;
1925 if(*(stringdup + sum + fit) == '\t')
1926 break;
1928 if(fit == 0){
1929 fit = fitcpy;
1930 break;
1934 DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1935 &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1937 sum += fit + (lret < fitcpy ? 1 : 0);
1938 height += size.cy;
1940 if(height > nheight)
1941 break;
1943 /* Stop if this was a linewrap (but not if it was a linebreak). */
1944 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1945 break;
1948 GdipFree(stringdup);
1949 DeleteObject(rgn);
1950 DeleteObject(gdifont);
1952 RestoreDC(graphics->hdc, save_state);
1954 return Ok;
1957 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
1958 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
1960 GpPath *path;
1961 GpStatus stat;
1963 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
1964 count, tension, fill);
1966 if(!graphics || !brush || !points)
1967 return InvalidParameter;
1969 if(graphics->busy)
1970 return ObjectBusy;
1972 stat = GdipCreatePath(fill, &path);
1973 if(stat != Ok)
1974 return stat;
1976 stat = GdipAddPathClosedCurve2(path, points, count, tension);
1977 if(stat != Ok){
1978 GdipDeletePath(path);
1979 return stat;
1982 stat = GdipFillPath(graphics, brush, path);
1983 if(stat != Ok){
1984 GdipDeletePath(path);
1985 return stat;
1988 GdipDeletePath(path);
1990 return Ok;
1993 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
1994 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
1996 GpPointF *ptf;
1997 GpStatus stat;
1998 INT i;
2000 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2001 count, tension, fill);
2003 if(!points || count <= 0)
2004 return InvalidParameter;
2006 ptf = GdipAlloc(sizeof(GpPointF)*count);
2007 if(!ptf)
2008 return OutOfMemory;
2010 for(i = 0;i < count;i++){
2011 ptf[i].X = (REAL)points[i].X;
2012 ptf[i].Y = (REAL)points[i].Y;
2015 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2017 GdipFree(ptf);
2019 return stat;
2022 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2023 REAL y, REAL width, REAL height)
2025 INT save_state;
2026 GpPointF ptf[2];
2027 POINT pti[2];
2029 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2031 if(!graphics || !brush)
2032 return InvalidParameter;
2034 if(graphics->busy)
2035 return ObjectBusy;
2037 ptf[0].X = x;
2038 ptf[0].Y = y;
2039 ptf[1].X = x + width;
2040 ptf[1].Y = y + height;
2042 save_state = SaveDC(graphics->hdc);
2043 EndPath(graphics->hdc);
2044 SelectObject(graphics->hdc, brush->gdibrush);
2045 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2047 transform_and_round_points(graphics, pti, ptf, 2);
2049 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2051 RestoreDC(graphics->hdc, save_state);
2053 return Ok;
2056 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2057 INT y, INT width, INT height)
2059 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2061 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2064 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2066 INT save_state;
2067 GpStatus retval;
2069 TRACE("(%p, %p, %p)\n", graphics, brush, path);
2071 if(!brush || !graphics || !path)
2072 return InvalidParameter;
2074 if(graphics->busy)
2075 return ObjectBusy;
2077 save_state = SaveDC(graphics->hdc);
2078 EndPath(graphics->hdc);
2079 SelectObject(graphics->hdc, brush->gdibrush);
2080 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2081 : WINDING));
2083 BeginPath(graphics->hdc);
2084 retval = draw_poly(graphics, NULL, path->pathdata.Points,
2085 path->pathdata.Types, path->pathdata.Count, FALSE);
2087 if(retval != Ok)
2088 goto end;
2090 EndPath(graphics->hdc);
2091 FillPath(graphics->hdc);
2093 retval = Ok;
2095 end:
2096 RestoreDC(graphics->hdc, save_state);
2098 return retval;
2101 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2102 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2104 INT save_state;
2106 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2107 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2109 if(!graphics || !brush)
2110 return InvalidParameter;
2112 if(graphics->busy)
2113 return ObjectBusy;
2115 save_state = SaveDC(graphics->hdc);
2116 EndPath(graphics->hdc);
2117 SelectObject(graphics->hdc, brush->gdibrush);
2118 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2120 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2122 RestoreDC(graphics->hdc, save_state);
2124 return Ok;
2127 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2128 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2130 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2131 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2133 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2136 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2137 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2139 INT save_state;
2140 GpPointF *ptf = NULL;
2141 POINT *pti = NULL;
2142 GpStatus retval = Ok;
2144 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2146 if(!graphics || !brush || !points || !count)
2147 return InvalidParameter;
2149 if(graphics->busy)
2150 return ObjectBusy;
2152 ptf = GdipAlloc(count * sizeof(GpPointF));
2153 pti = GdipAlloc(count * sizeof(POINT));
2154 if(!ptf || !pti){
2155 retval = OutOfMemory;
2156 goto end;
2159 memcpy(ptf, points, count * sizeof(GpPointF));
2161 save_state = SaveDC(graphics->hdc);
2162 EndPath(graphics->hdc);
2163 SelectObject(graphics->hdc, brush->gdibrush);
2164 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2165 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2166 : WINDING));
2168 transform_and_round_points(graphics, pti, ptf, count);
2169 Polygon(graphics->hdc, pti, count);
2171 RestoreDC(graphics->hdc, save_state);
2173 end:
2174 GdipFree(ptf);
2175 GdipFree(pti);
2177 return retval;
2180 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2181 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2183 INT save_state, i;
2184 GpPointF *ptf = NULL;
2185 POINT *pti = NULL;
2186 GpStatus retval = Ok;
2188 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2190 if(!graphics || !brush || !points || !count)
2191 return InvalidParameter;
2193 if(graphics->busy)
2194 return ObjectBusy;
2196 ptf = GdipAlloc(count * sizeof(GpPointF));
2197 pti = GdipAlloc(count * sizeof(POINT));
2198 if(!ptf || !pti){
2199 retval = OutOfMemory;
2200 goto end;
2203 for(i = 0; i < count; i ++){
2204 ptf[i].X = (REAL) points[i].X;
2205 ptf[i].Y = (REAL) points[i].Y;
2208 save_state = SaveDC(graphics->hdc);
2209 EndPath(graphics->hdc);
2210 SelectObject(graphics->hdc, brush->gdibrush);
2211 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2212 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2213 : WINDING));
2215 transform_and_round_points(graphics, pti, ptf, count);
2216 Polygon(graphics->hdc, pti, count);
2218 RestoreDC(graphics->hdc, save_state);
2220 end:
2221 GdipFree(ptf);
2222 GdipFree(pti);
2224 return retval;
2227 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2228 GDIPCONST GpPointF *points, INT count)
2230 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2232 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2235 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2236 GDIPCONST GpPoint *points, INT count)
2238 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2240 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2243 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2244 REAL x, REAL y, REAL width, REAL height)
2246 INT save_state;
2247 GpPointF ptf[4];
2248 POINT pti[4];
2250 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2252 if(!graphics || !brush)
2253 return InvalidParameter;
2255 if(graphics->busy)
2256 return ObjectBusy;
2258 ptf[0].X = x;
2259 ptf[0].Y = y;
2260 ptf[1].X = x + width;
2261 ptf[1].Y = y;
2262 ptf[2].X = x + width;
2263 ptf[2].Y = y + height;
2264 ptf[3].X = x;
2265 ptf[3].Y = y + height;
2267 save_state = SaveDC(graphics->hdc);
2268 EndPath(graphics->hdc);
2269 SelectObject(graphics->hdc, brush->gdibrush);
2270 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2272 transform_and_round_points(graphics, pti, ptf, 4);
2274 Polygon(graphics->hdc, pti, 4);
2276 RestoreDC(graphics->hdc, save_state);
2278 return Ok;
2281 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2282 INT x, INT y, INT width, INT height)
2284 INT save_state;
2285 GpPointF ptf[4];
2286 POINT pti[4];
2288 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2290 if(!graphics || !brush)
2291 return InvalidParameter;
2293 if(graphics->busy)
2294 return ObjectBusy;
2296 ptf[0].X = x;
2297 ptf[0].Y = y;
2298 ptf[1].X = x + width;
2299 ptf[1].Y = y;
2300 ptf[2].X = x + width;
2301 ptf[2].Y = y + height;
2302 ptf[3].X = x;
2303 ptf[3].Y = y + height;
2305 save_state = SaveDC(graphics->hdc);
2306 EndPath(graphics->hdc);
2307 SelectObject(graphics->hdc, brush->gdibrush);
2308 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2310 transform_and_round_points(graphics, pti, ptf, 4);
2312 Polygon(graphics->hdc, pti, 4);
2314 RestoreDC(graphics->hdc, save_state);
2316 return Ok;
2319 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2320 INT count)
2322 GpStatus ret;
2323 INT i;
2325 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2327 if(!rects)
2328 return InvalidParameter;
2330 for(i = 0; i < count; i++){
2331 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2332 if(ret != Ok) return ret;
2335 return Ok;
2338 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2339 INT count)
2341 GpRectF *rectsF;
2342 GpStatus ret;
2343 INT i;
2345 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2347 if(!rects || count <= 0)
2348 return InvalidParameter;
2350 rectsF = GdipAlloc(sizeof(GpRectF)*count);
2351 if(!rectsF)
2352 return OutOfMemory;
2354 for(i = 0; i < count; i++){
2355 rectsF[i].X = (REAL)rects[i].X;
2356 rectsF[i].Y = (REAL)rects[i].Y;
2357 rectsF[i].X = (REAL)rects[i].Width;
2358 rectsF[i].Height = (REAL)rects[i].Height;
2361 ret = GdipFillRectangles(graphics,brush,rectsF,count);
2362 GdipFree(rectsF);
2364 return ret;
2367 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2368 GpRegion* region)
2370 if (!(graphics && brush && region))
2371 return InvalidParameter;
2373 if(graphics->busy)
2374 return ObjectBusy;
2376 FIXME("(%p, %p, %p): stub\n", graphics, brush, region);
2378 return NotImplemented;
2381 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2383 static int calls;
2385 if(!graphics)
2386 return InvalidParameter;
2388 if(graphics->busy)
2389 return ObjectBusy;
2391 if(!(calls++))
2392 FIXME("not implemented\n");
2394 return NotImplemented;
2397 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2398 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2399 CompositingMode *mode)
2401 TRACE("(%p, %p)\n", graphics, mode);
2403 if(!graphics || !mode)
2404 return InvalidParameter;
2406 if(graphics->busy)
2407 return ObjectBusy;
2409 *mode = graphics->compmode;
2411 return Ok;
2414 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
2415 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
2416 CompositingQuality *quality)
2418 TRACE("(%p, %p)\n", graphics, quality);
2420 if(!graphics || !quality)
2421 return InvalidParameter;
2423 if(graphics->busy)
2424 return ObjectBusy;
2426 *quality = graphics->compqual;
2428 return Ok;
2431 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
2432 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
2433 InterpolationMode *mode)
2435 TRACE("(%p, %p)\n", graphics, mode);
2437 if(!graphics || !mode)
2438 return InvalidParameter;
2440 if(graphics->busy)
2441 return ObjectBusy;
2443 *mode = graphics->interpolation;
2445 return Ok;
2448 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
2450 TRACE("(%p, %p)\n", graphics, scale);
2452 if(!graphics || !scale)
2453 return InvalidParameter;
2455 if(graphics->busy)
2456 return ObjectBusy;
2458 *scale = graphics->scale;
2460 return Ok;
2463 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
2465 TRACE("(%p, %p)\n", graphics, unit);
2467 if(!graphics || !unit)
2468 return InvalidParameter;
2470 if(graphics->busy)
2471 return ObjectBusy;
2473 *unit = graphics->unit;
2475 return Ok;
2478 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
2479 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2480 *mode)
2482 TRACE("(%p, %p)\n", graphics, mode);
2484 if(!graphics || !mode)
2485 return InvalidParameter;
2487 if(graphics->busy)
2488 return ObjectBusy;
2490 *mode = graphics->pixeloffset;
2492 return Ok;
2495 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
2496 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
2498 TRACE("(%p, %p)\n", graphics, mode);
2500 if(!graphics || !mode)
2501 return InvalidParameter;
2503 if(graphics->busy)
2504 return ObjectBusy;
2506 *mode = graphics->smoothing;
2508 return Ok;
2511 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
2513 TRACE("(%p, %p)\n", graphics, contrast);
2515 if(!graphics || !contrast)
2516 return InvalidParameter;
2518 *contrast = graphics->textcontrast;
2520 return Ok;
2523 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
2524 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
2525 TextRenderingHint *hint)
2527 TRACE("(%p, %p)\n", graphics, hint);
2529 if(!graphics || !hint)
2530 return InvalidParameter;
2532 if(graphics->busy)
2533 return ObjectBusy;
2535 *hint = graphics->texthint;
2537 return Ok;
2540 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2542 TRACE("(%p, %p)\n", graphics, matrix);
2544 if(!graphics || !matrix)
2545 return InvalidParameter;
2547 if(graphics->busy)
2548 return ObjectBusy;
2550 *matrix = *graphics->worldtrans;
2551 return Ok;
2554 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
2556 GpSolidFill *brush;
2557 GpStatus stat;
2558 RECT rect;
2560 TRACE("(%p, %x)\n", graphics, color);
2562 if(!graphics)
2563 return InvalidParameter;
2565 if(graphics->busy)
2566 return ObjectBusy;
2568 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
2569 return stat;
2571 if(graphics->hwnd){
2572 if(!GetWindowRect(graphics->hwnd, &rect)){
2573 GdipDeleteBrush((GpBrush*)brush);
2574 return GenericError;
2577 GdipFillRectangle(graphics, (GpBrush*)brush, 0.0, 0.0, (REAL)(rect.right - rect.left),
2578 (REAL)(rect.bottom - rect.top));
2580 else
2581 GdipFillRectangle(graphics, (GpBrush*)brush, 0.0, 0.0, (REAL)GetDeviceCaps(graphics->hdc, HORZRES),
2582 (REAL)GetDeviceCaps(graphics->hdc, VERTRES));
2584 GdipDeleteBrush((GpBrush*)brush);
2586 return Ok;
2589 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
2591 TRACE("(%p, %p)\n", graphics, res);
2593 if(!graphics || !res)
2594 return InvalidParameter;
2596 return GdipIsEmptyRegion(graphics->clip, graphics, res);
2599 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
2601 FIXME("(%p, %.2f, %.2f, %p) stub\n", graphics, x, y, result);
2603 if(!graphics || !result)
2604 return InvalidParameter;
2606 if(graphics->busy)
2607 return ObjectBusy;
2609 return NotImplemented;
2612 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
2614 FIXME("(%p, %d, %d, %p) stub\n", graphics, x, y, result);
2616 if(!graphics || !result)
2617 return InvalidParameter;
2619 if(graphics->busy)
2620 return ObjectBusy;
2622 return NotImplemented;
2625 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
2626 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
2627 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
2628 INT regionCount, GpRegion** regions)
2630 if (!(graphics && string && font && layoutRect && stringFormat && regions))
2631 return InvalidParameter;
2633 FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
2634 length, font, layoutRect, stringFormat, regionCount, regions);
2636 return NotImplemented;
2639 /* Find the smallest rectangle that bounds the text when it is printed in rect
2640 * according to the format options listed in format. If rect has 0 width and
2641 * height, then just find the smallest rectangle that bounds the text when it's
2642 * printed at location (rect->X, rect-Y). */
2643 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
2644 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
2645 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
2646 INT *codepointsfitted, INT *linesfilled)
2648 HFONT oldfont;
2649 WCHAR* stringdup;
2650 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
2651 nheight;
2652 SIZE size;
2654 if(!graphics || !string || !font || !rect)
2655 return InvalidParameter;
2657 if(codepointsfitted || linesfilled){
2658 FIXME("not implemented for given parameters\n");
2659 return NotImplemented;
2662 if(format)
2663 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2665 if(length == -1) length = lstrlenW(string);
2667 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
2668 if(!stringdup) return OutOfMemory;
2670 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2671 nwidth = roundr(rect->Width);
2672 nheight = roundr(rect->Height);
2674 if((nwidth == 0) && (nheight == 0))
2675 nwidth = nheight = INT_MAX;
2677 for(i = 0, j = 0; i < length; i++){
2678 if(!isprintW(string[i]) && (string[i] != '\n'))
2679 continue;
2681 stringdup[j] = string[i];
2682 j++;
2685 stringdup[j] = 0;
2686 length = j;
2688 while(sum < length){
2689 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2690 nwidth, &fit, NULL, &size);
2691 fitcpy = fit;
2693 if(fit == 0)
2694 break;
2696 for(lret = 0; lret < fit; lret++)
2697 if(*(stringdup + sum + lret) == '\n')
2698 break;
2700 /* Line break code (may look strange, but it imitates windows). */
2701 if(lret < fit)
2702 fit = lret; /* this is not an off-by-one error */
2703 else if(fit < (length - sum)){
2704 if(*(stringdup + sum + fit) == ' ')
2705 while(*(stringdup + sum + fit) == ' ')
2706 fit++;
2707 else
2708 while(*(stringdup + sum + fit - 1) != ' '){
2709 fit--;
2711 if(*(stringdup + sum + fit) == '\t')
2712 break;
2714 if(fit == 0){
2715 fit = fitcpy;
2716 break;
2721 GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
2722 nwidth, &j, NULL, &size);
2724 sum += fit + (lret < fitcpy ? 1 : 0);
2725 height += size.cy;
2726 max_width = max(max_width, size.cx);
2728 if(height > nheight)
2729 break;
2731 /* Stop if this was a linewrap (but not if it was a linebreak). */
2732 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2733 break;
2736 bounds->X = rect->X;
2737 bounds->Y = rect->Y;
2738 bounds->Width = (REAL)max_width;
2739 bounds->Height = (REAL) min(height, nheight);
2741 GdipFree(stringdup);
2742 DeleteObject(SelectObject(graphics->hdc, oldfont));
2744 return Ok;
2747 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
2749 TRACE("(%p)\n", graphics);
2751 if(!graphics)
2752 return InvalidParameter;
2754 if(graphics->busy)
2755 return ObjectBusy;
2757 return GdipSetInfinite(graphics->clip);
2760 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
2762 TRACE("(%p)\n", graphics);
2764 if(!graphics)
2765 return InvalidParameter;
2767 if(graphics->busy)
2768 return ObjectBusy;
2770 graphics->worldtrans->matrix[0] = 1.0;
2771 graphics->worldtrans->matrix[1] = 0.0;
2772 graphics->worldtrans->matrix[2] = 0.0;
2773 graphics->worldtrans->matrix[3] = 1.0;
2774 graphics->worldtrans->matrix[4] = 0.0;
2775 graphics->worldtrans->matrix[5] = 0.0;
2777 return Ok;
2780 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
2782 static int calls;
2784 if(!graphics)
2785 return InvalidParameter;
2787 if(!(calls++))
2788 FIXME("graphics state not implemented\n");
2790 return NotImplemented;
2793 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
2794 GpMatrixOrder order)
2796 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
2798 if(!graphics)
2799 return InvalidParameter;
2801 if(graphics->busy)
2802 return ObjectBusy;
2804 return GdipRotateMatrix(graphics->worldtrans, angle, order);
2807 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
2809 static int calls;
2811 if(!graphics || !state)
2812 return InvalidParameter;
2814 if(!(calls++))
2815 FIXME("graphics state not implemented\n");
2817 return NotImplemented;
2820 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
2821 REAL sy, GpMatrixOrder order)
2823 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
2825 if(!graphics)
2826 return InvalidParameter;
2828 if(graphics->busy)
2829 return ObjectBusy;
2831 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
2834 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
2835 CombineMode mode)
2837 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
2839 if(!graphics || !srcgraphics)
2840 return InvalidParameter;
2842 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
2845 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
2846 CompositingMode mode)
2848 TRACE("(%p, %d)\n", graphics, mode);
2850 if(!graphics)
2851 return InvalidParameter;
2853 if(graphics->busy)
2854 return ObjectBusy;
2856 graphics->compmode = mode;
2858 return Ok;
2861 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
2862 CompositingQuality quality)
2864 TRACE("(%p, %d)\n", graphics, quality);
2866 if(!graphics)
2867 return InvalidParameter;
2869 if(graphics->busy)
2870 return ObjectBusy;
2872 graphics->compqual = quality;
2874 return Ok;
2877 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
2878 InterpolationMode mode)
2880 TRACE("(%p, %d)\n", graphics, mode);
2882 if(!graphics)
2883 return InvalidParameter;
2885 if(graphics->busy)
2886 return ObjectBusy;
2888 graphics->interpolation = mode;
2890 return Ok;
2893 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
2895 TRACE("(%p, %.2f)\n", graphics, scale);
2897 if(!graphics || (scale <= 0.0))
2898 return InvalidParameter;
2900 if(graphics->busy)
2901 return ObjectBusy;
2903 graphics->scale = scale;
2905 return Ok;
2908 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
2910 TRACE("(%p, %d)\n", graphics, unit);
2912 if(!graphics)
2913 return InvalidParameter;
2915 if(graphics->busy)
2916 return ObjectBusy;
2918 if(unit == UnitWorld)
2919 return InvalidParameter;
2921 graphics->unit = unit;
2923 return Ok;
2926 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2927 mode)
2929 TRACE("(%p, %d)\n", graphics, mode);
2931 if(!graphics)
2932 return InvalidParameter;
2934 if(graphics->busy)
2935 return ObjectBusy;
2937 graphics->pixeloffset = mode;
2939 return Ok;
2942 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
2944 TRACE("(%p, %d)\n", graphics, mode);
2946 if(!graphics)
2947 return InvalidParameter;
2949 if(graphics->busy)
2950 return ObjectBusy;
2952 graphics->smoothing = mode;
2954 return Ok;
2957 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
2959 TRACE("(%p, %d)\n", graphics, contrast);
2961 if(!graphics)
2962 return InvalidParameter;
2964 graphics->textcontrast = contrast;
2966 return Ok;
2969 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
2970 TextRenderingHint hint)
2972 TRACE("(%p, %d)\n", graphics, hint);
2974 if(!graphics)
2975 return InvalidParameter;
2977 if(graphics->busy)
2978 return ObjectBusy;
2980 graphics->texthint = hint;
2982 return Ok;
2985 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2987 TRACE("(%p, %p)\n", graphics, matrix);
2989 if(!graphics || !matrix)
2990 return InvalidParameter;
2992 if(graphics->busy)
2993 return ObjectBusy;
2995 GdipDeleteMatrix(graphics->worldtrans);
2996 return GdipCloneMatrix(matrix, &graphics->worldtrans);
2999 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
3000 REAL dy, GpMatrixOrder order)
3002 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
3004 if(!graphics)
3005 return InvalidParameter;
3007 if(graphics->busy)
3008 return ObjectBusy;
3010 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
3013 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
3015 TRACE("(%p, %p, %d)\n", graphics, path, mode);
3017 if(!graphics)
3018 return InvalidParameter;
3020 if(graphics->busy)
3021 return ObjectBusy;
3023 return GdipCombineRegionPath(graphics->clip, path, mode);
3026 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
3027 REAL width, REAL height,
3028 CombineMode mode)
3030 GpRectF rect;
3032 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
3034 if(!graphics)
3035 return InvalidParameter;
3037 if(graphics->busy)
3038 return ObjectBusy;
3040 rect.X = x;
3041 rect.Y = y;
3042 rect.Width = width;
3043 rect.Height = height;
3045 return GdipCombineRegionRect(graphics->clip, &rect, mode);
3048 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
3049 INT width, INT height,
3050 CombineMode mode)
3052 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3054 if(!graphics)
3055 return InvalidParameter;
3057 if(graphics->busy)
3058 return ObjectBusy;
3060 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3063 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
3064 CombineMode mode)
3066 TRACE("(%p, %p, %d)\n", graphics, region, mode);
3068 if(!graphics || !region)
3069 return InvalidParameter;
3071 if(graphics->busy)
3072 return ObjectBusy;
3074 return GdipCombineRegionRegion(graphics->clip, region, mode);
3077 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
3078 UINT limitDpi)
3080 static int calls;
3082 if(!(calls++))
3083 FIXME("not implemented\n");
3085 return NotImplemented;
3088 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
3089 INT count)
3091 INT save_state;
3092 POINT *pti;
3094 TRACE("(%p, %p, %d)\n", graphics, points, count);
3096 if(!graphics || !pen || count<=0)
3097 return InvalidParameter;
3099 if(graphics->busy)
3100 return ObjectBusy;
3102 pti = GdipAlloc(sizeof(POINT) * count);
3104 save_state = prepare_dc(graphics, pen);
3105 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3107 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
3108 Polygon(graphics->hdc, pti, count);
3110 restore_dc(graphics, save_state);
3111 GdipFree(pti);
3113 return Ok;
3116 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
3117 INT count)
3119 GpStatus ret;
3120 GpPointF *ptf;
3121 INT i;
3123 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3125 if(count<=0) return InvalidParameter;
3126 ptf = GdipAlloc(sizeof(GpPointF) * count);
3128 for(i = 0;i < count; i++){
3129 ptf[i].X = (REAL)points[i].X;
3130 ptf[i].Y = (REAL)points[i].Y;
3133 ret = GdipDrawPolygon(graphics,pen,ptf,count);
3134 GdipFree(ptf);
3136 return ret;
3139 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
3141 TRACE("(%p, %p)\n", graphics, dpi);
3143 if(!graphics || !dpi)
3144 return InvalidParameter;
3146 if(graphics->busy)
3147 return ObjectBusy;
3149 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
3151 return Ok;
3154 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
3156 TRACE("(%p, %p)\n", graphics, dpi);
3158 if(!graphics || !dpi)
3159 return InvalidParameter;
3161 if(graphics->busy)
3162 return ObjectBusy;
3164 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
3166 return Ok;
3169 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
3170 GpMatrixOrder order)
3172 GpMatrix m;
3173 GpStatus ret;
3175 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
3177 if(!graphics || !matrix)
3178 return InvalidParameter;
3180 if(graphics->busy)
3181 return ObjectBusy;
3183 m = *(graphics->worldtrans);
3185 ret = GdipMultiplyMatrix(&m, (GpMatrix*)matrix, order);
3186 if(ret == Ok)
3187 *(graphics->worldtrans) = m;
3189 return ret;
3192 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
3194 TRACE("(%p, %p)\n", graphics, hdc);
3196 if(!graphics || !hdc)
3197 return InvalidParameter;
3199 if(graphics->busy)
3200 return ObjectBusy;
3202 *hdc = graphics->hdc;
3203 graphics->busy = TRUE;
3205 return Ok;
3208 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
3210 TRACE("(%p, %p)\n", graphics, hdc);
3212 if(!graphics)
3213 return InvalidParameter;
3215 if(graphics->hdc != hdc || !(graphics->busy))
3216 return InvalidParameter;
3218 graphics->busy = FALSE;
3220 return Ok;
3223 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
3225 GpRegion *clip;
3226 GpStatus status;
3228 TRACE("(%p, %p)\n", graphics, region);
3230 if(!graphics || !region)
3231 return InvalidParameter;
3233 if(graphics->busy)
3234 return ObjectBusy;
3236 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
3237 return status;
3239 /* free everything except root node and header */
3240 delete_element(&region->node);
3241 memcpy(region, clip, sizeof(GpRegion));
3243 return Ok;
3246 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
3247 GpCoordinateSpace src_space, GpPointF *points, INT count)
3249 if(!graphics || !points || count <= 0)
3250 return InvalidParameter;
3252 if(graphics->busy)
3253 return ObjectBusy;
3255 FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
3257 return NotImplemented;
3260 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
3261 GpCoordinateSpace src_space, GpPoint *points, INT count)
3263 FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
3265 return NotImplemented;