gdiplus: Implement GdipIsVisibleRegionPoint.
[wine.git] / dlls / gdiplus / graphics.c
blob7b9dbd62b74cd21bec4d0d61035868aa2076ada4
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"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
52 REAL radAngle, hypotenuse;
54 radAngle = deg2rad(angle);
55 hypotenuse = 50.0; /* arbitrary */
57 *x = x_0 + cos(radAngle) * hypotenuse;
58 *y = y_0 + sin(radAngle) * hypotenuse;
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
64 BYTE ret;
66 switch(type & PathPointTypePathTypeMask){
67 case PathPointTypeBezier:
68 ret = PT_BEZIERTO;
69 break;
70 case PathPointTypeLine:
71 ret = PT_LINETO;
72 break;
73 case PathPointTypeStart:
74 ret = PT_MOVETO;
75 break;
76 default:
77 ERR("Bad point type\n");
78 return 0;
81 if(type & PathPointTypeCloseSubpath)
82 ret |= PT_CLOSEFIGURE;
84 return ret;
87 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
89 HPEN gdipen;
90 REAL width;
91 INT save_state = SaveDC(graphics->hdc), i, numdashes;
92 GpPointF pt[2];
93 DWORD dash_array[MAX_DASHLEN];
95 EndPath(graphics->hdc);
97 if(pen->unit == UnitPixel){
98 width = pen->width;
100 else{
101 /* Get an estimate for the amount the pen width is affected by the world
102 * transform. (This is similar to what some of the wine drivers do.) */
103 pt[0].X = 0.0;
104 pt[0].Y = 0.0;
105 pt[1].X = 1.0;
106 pt[1].Y = 1.0;
107 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
108 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
109 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
111 width *= pen->width * convert_unit(graphics->hdc,
112 pen->unit == UnitWorld ? graphics->unit : pen->unit);
115 if(pen->dash == DashStyleCustom){
116 numdashes = min(pen->numdashes, MAX_DASHLEN);
118 TRACE("dashes are: ");
119 for(i = 0; i < numdashes; i++){
120 dash_array[i] = roundr(width * pen->dashes[i]);
121 TRACE("%d, ", dash_array[i]);
123 TRACE("\n and the pen style is %x\n", pen->style);
125 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
126 numdashes, dash_array);
128 else
129 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
131 SelectObject(graphics->hdc, gdipen);
133 return save_state;
136 static void restore_dc(GpGraphics *graphics, INT state)
138 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
139 RestoreDC(graphics->hdc, state);
142 /* This helper applies all the changes that the points listed in ptf need in
143 * order to be drawn on the device context. In the end, this should include at
144 * least:
145 * -scaling by page unit
146 * -applying world transformation
147 * -converting from float to int
148 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
149 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
150 * gdi to draw, and these functions would irreparably mess with line widths.
152 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
153 GpPointF *ptf, INT count)
155 REAL unitscale;
156 GpMatrix *matrix;
157 int i;
159 unitscale = convert_unit(graphics->hdc, graphics->unit);
161 /* apply page scale */
162 if(graphics->unit != UnitDisplay)
163 unitscale *= graphics->scale;
165 GdipCloneMatrix(graphics->worldtrans, &matrix);
166 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
167 GdipTransformMatrixPoints(matrix, ptf, count);
168 GdipDeleteMatrix(matrix);
170 for(i = 0; i < count; i++){
171 pti[i].x = roundr(ptf[i].X);
172 pti[i].y = roundr(ptf[i].Y);
176 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
178 ARGB result=0;
179 ARGB i;
180 for (i=0xff; i<=0xff0000; i = i << 8)
181 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
182 return result;
185 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
187 REAL blendfac;
189 /* clamp to between 0.0 and 1.0, using the wrap mode */
190 if (brush->wrap == WrapModeTile)
192 position = fmodf(position, 1.0f);
193 if (position < 0.0f) position += 1.0f;
195 else /* WrapModeFlip* */
197 position = fmodf(position, 2.0f);
198 if (position < 0.0f) position += 2.0f;
199 if (position > 1.0f) position = 2.0f - position;
202 if (brush->blendcount == 1)
203 blendfac = position;
204 else
206 int i=1;
207 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
208 REAL range;
210 /* locate the blend positions surrounding this position */
211 while (position > brush->blendpos[i])
212 i++;
214 /* interpolate between the blend positions */
215 left_blendpos = brush->blendpos[i-1];
216 left_blendfac = brush->blendfac[i-1];
217 right_blendpos = brush->blendpos[i];
218 right_blendfac = brush->blendfac[i];
219 range = right_blendpos - left_blendpos;
220 blendfac = (left_blendfac * (right_blendpos - position) +
221 right_blendfac * (position - left_blendpos)) / range;
223 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
226 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
228 switch (brush->bt)
230 case BrushTypeLinearGradient:
232 GpLineGradient *line = (GpLineGradient*)brush;
233 RECT rc;
235 SelectClipPath(graphics->hdc, RGN_AND);
236 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
238 GpPointF endpointsf[2];
239 POINT endpointsi[2];
240 POINT poly[4];
242 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
244 endpointsf[0] = line->startpoint;
245 endpointsf[1] = line->endpoint;
246 transform_and_round_points(graphics, endpointsi, endpointsf, 2);
248 if (abs(endpointsi[0].x-endpointsi[1].x) > abs(endpointsi[0].y-endpointsi[1].y))
250 /* vertical-ish gradient */
251 int startx, endx; /* x co-ordinates of endpoints shifted to intersect the top of the visible rectangle */
252 int startbottomx; /* x co-ordinate of start point shifted to intersect the bottom of the visible rectangle */
253 int width;
254 COLORREF col;
255 HBRUSH hbrush, hprevbrush;
256 int leftx, rightx; /* x co-ordinates where the leftmost and rightmost gradient lines hit the top of the visible rectangle */
257 int x;
258 int tilt; /* horizontal distance covered by a gradient line */
260 startx = roundr((rc.top - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
261 endx = roundr((rc.top - endpointsf[1].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[1].X);
262 width = endx - startx;
263 startbottomx = roundr((rc.bottom - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
264 tilt = startx - startbottomx;
266 if (startx >= startbottomx)
268 leftx = rc.left;
269 rightx = rc.right + tilt;
271 else
273 leftx = rc.left + tilt;
274 rightx = rc.right;
277 poly[0].y = rc.bottom;
278 poly[1].y = rc.top;
279 poly[2].y = rc.top;
280 poly[3].y = rc.bottom;
282 for (x=leftx; x<=rightx; x++)
284 ARGB argb = blend_line_gradient(line, (x-startx)/(REAL)width);
285 col = ARGB2COLORREF(argb);
286 hbrush = CreateSolidBrush(col);
287 hprevbrush = SelectObject(graphics->hdc, hbrush);
288 poly[0].x = x - tilt - 1;
289 poly[1].x = x - 1;
290 poly[2].x = x;
291 poly[3].x = x - tilt;
292 Polygon(graphics->hdc, poly, 4);
293 SelectObject(graphics->hdc, hprevbrush);
294 DeleteObject(hbrush);
297 else if (endpointsi[0].y != endpointsi[1].y)
299 /* horizontal-ish gradient */
300 int starty, endy; /* y co-ordinates of endpoints shifted to intersect the left of the visible rectangle */
301 int startrighty; /* y co-ordinate of start point shifted to intersect the right of the visible rectangle */
302 int height;
303 COLORREF col;
304 HBRUSH hbrush, hprevbrush;
305 int topy, bottomy; /* y co-ordinates where the topmost and bottommost gradient lines hit the left of the visible rectangle */
306 int y;
307 int tilt; /* vertical distance covered by a gradient line */
309 starty = roundr((rc.left - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
310 endy = roundr((rc.left - endpointsf[1].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[1].Y);
311 height = endy - starty;
312 startrighty = roundr((rc.right - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
313 tilt = starty - startrighty;
315 if (starty >= startrighty)
317 topy = rc.top;
318 bottomy = rc.bottom + tilt;
320 else
322 topy = rc.top + tilt;
323 bottomy = rc.bottom;
326 poly[0].x = rc.right;
327 poly[1].x = rc.left;
328 poly[2].x = rc.left;
329 poly[3].x = rc.right;
331 for (y=topy; y<=bottomy; y++)
333 ARGB argb = blend_line_gradient(line, (y-starty)/(REAL)height);
334 col = ARGB2COLORREF(argb);
335 hbrush = CreateSolidBrush(col);
336 hprevbrush = SelectObject(graphics->hdc, hbrush);
337 poly[0].y = y - tilt - 1;
338 poly[1].y = y - 1;
339 poly[2].y = y;
340 poly[3].y = y - tilt;
341 Polygon(graphics->hdc, poly, 4);
342 SelectObject(graphics->hdc, hprevbrush);
343 DeleteObject(hbrush);
346 /* else startpoint == endpoint */
348 break;
350 case BrushTypeSolidColor:
352 GpSolidFill *fill = (GpSolidFill*)brush;
353 if (fill->bmp)
355 RECT rc;
356 /* partially transparent fill */
358 SelectClipPath(graphics->hdc, RGN_AND);
359 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
361 HDC hdc = CreateCompatibleDC(NULL);
362 HBITMAP oldbmp;
363 BLENDFUNCTION bf;
365 if (!hdc) break;
367 oldbmp = SelectObject(hdc, fill->bmp);
369 bf.BlendOp = AC_SRC_OVER;
370 bf.BlendFlags = 0;
371 bf.SourceConstantAlpha = 255;
372 bf.AlphaFormat = AC_SRC_ALPHA;
374 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
376 SelectObject(hdc, oldbmp);
377 DeleteDC(hdc);
380 break;
382 /* else fall through */
384 default:
385 SelectObject(graphics->hdc, brush->gdibrush);
386 FillPath(graphics->hdc);
387 break;
391 /* GdipDrawPie/GdipFillPie helper function */
392 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
393 REAL height, REAL startAngle, REAL sweepAngle)
395 GpPointF ptf[4];
396 POINT pti[4];
398 ptf[0].X = x;
399 ptf[0].Y = y;
400 ptf[1].X = x + width;
401 ptf[1].Y = y + height;
403 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
404 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
406 transform_and_round_points(graphics, pti, ptf, 4);
408 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
409 pti[2].y, pti[3].x, pti[3].y);
412 /* Draws the linecap the specified color and size on the hdc. The linecap is in
413 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
414 * should not be called on an hdc that has a path you care about. */
415 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
416 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
418 HGDIOBJ oldbrush = NULL, oldpen = NULL;
419 GpMatrix *matrix = NULL;
420 HBRUSH brush = NULL;
421 HPEN pen = NULL;
422 PointF ptf[4], *custptf = NULL;
423 POINT pt[4], *custpt = NULL;
424 BYTE *tp = NULL;
425 REAL theta, dsmall, dbig, dx, dy = 0.0;
426 INT i, count;
427 LOGBRUSH lb;
428 BOOL customstroke;
430 if((x1 == x2) && (y1 == y2))
431 return;
433 theta = gdiplus_atan2(y2 - y1, x2 - x1);
435 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
436 if(!customstroke){
437 brush = CreateSolidBrush(color);
438 lb.lbStyle = BS_SOLID;
439 lb.lbColor = color;
440 lb.lbHatch = 0;
441 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
442 PS_JOIN_MITER, 1, &lb, 0,
443 NULL);
444 oldbrush = SelectObject(graphics->hdc, brush);
445 oldpen = SelectObject(graphics->hdc, pen);
448 switch(cap){
449 case LineCapFlat:
450 break;
451 case LineCapSquare:
452 case LineCapSquareAnchor:
453 case LineCapDiamondAnchor:
454 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
455 if(cap == LineCapDiamondAnchor){
456 dsmall = cos(theta + M_PI_2) * size;
457 dbig = sin(theta + M_PI_2) * size;
459 else{
460 dsmall = cos(theta + M_PI_4) * size;
461 dbig = sin(theta + M_PI_4) * size;
464 ptf[0].X = x2 - dsmall;
465 ptf[1].X = x2 + dbig;
467 ptf[0].Y = y2 - dbig;
468 ptf[3].Y = y2 + dsmall;
470 ptf[1].Y = y2 - dsmall;
471 ptf[2].Y = y2 + dbig;
473 ptf[3].X = x2 - dbig;
474 ptf[2].X = x2 + dsmall;
476 transform_and_round_points(graphics, pt, ptf, 4);
477 Polygon(graphics->hdc, pt, 4);
479 break;
480 case LineCapArrowAnchor:
481 size = size * 4.0 / sqrt(3.0);
483 dx = cos(M_PI / 6.0 + theta) * size;
484 dy = sin(M_PI / 6.0 + theta) * size;
486 ptf[0].X = x2 - dx;
487 ptf[0].Y = y2 - dy;
489 dx = cos(- M_PI / 6.0 + theta) * size;
490 dy = sin(- M_PI / 6.0 + theta) * size;
492 ptf[1].X = x2 - dx;
493 ptf[1].Y = y2 - dy;
495 ptf[2].X = x2;
496 ptf[2].Y = y2;
498 transform_and_round_points(graphics, pt, ptf, 3);
499 Polygon(graphics->hdc, pt, 3);
501 break;
502 case LineCapRoundAnchor:
503 dx = dy = ANCHOR_WIDTH * size / 2.0;
505 ptf[0].X = x2 - dx;
506 ptf[0].Y = y2 - dy;
507 ptf[1].X = x2 + dx;
508 ptf[1].Y = y2 + dy;
510 transform_and_round_points(graphics, pt, ptf, 2);
511 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
513 break;
514 case LineCapTriangle:
515 size = size / 2.0;
516 dx = cos(M_PI_2 + theta) * size;
517 dy = sin(M_PI_2 + theta) * size;
519 ptf[0].X = x2 - dx;
520 ptf[0].Y = y2 - dy;
521 ptf[1].X = x2 + dx;
522 ptf[1].Y = y2 + dy;
524 dx = cos(theta) * size;
525 dy = sin(theta) * size;
527 ptf[2].X = x2 + dx;
528 ptf[2].Y = y2 + dy;
530 transform_and_round_points(graphics, pt, ptf, 3);
531 Polygon(graphics->hdc, pt, 3);
533 break;
534 case LineCapRound:
535 dx = dy = size / 2.0;
537 ptf[0].X = x2 - dx;
538 ptf[0].Y = y2 - dy;
539 ptf[1].X = x2 + dx;
540 ptf[1].Y = y2 + dy;
542 dx = -cos(M_PI_2 + theta) * size;
543 dy = -sin(M_PI_2 + theta) * size;
545 ptf[2].X = x2 - dx;
546 ptf[2].Y = y2 - dy;
547 ptf[3].X = x2 + dx;
548 ptf[3].Y = y2 + dy;
550 transform_and_round_points(graphics, pt, ptf, 4);
551 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
552 pt[2].y, pt[3].x, pt[3].y);
554 break;
555 case LineCapCustom:
556 if(!custom)
557 break;
559 count = custom->pathdata.Count;
560 custptf = GdipAlloc(count * sizeof(PointF));
561 custpt = GdipAlloc(count * sizeof(POINT));
562 tp = GdipAlloc(count);
564 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
565 goto custend;
567 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
569 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
570 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
571 MatrixOrderAppend);
572 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
573 GdipTransformMatrixPoints(matrix, custptf, count);
575 transform_and_round_points(graphics, custpt, custptf, count);
577 for(i = 0; i < count; i++)
578 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
580 if(custom->fill){
581 BeginPath(graphics->hdc);
582 PolyDraw(graphics->hdc, custpt, tp, count);
583 EndPath(graphics->hdc);
584 StrokeAndFillPath(graphics->hdc);
586 else
587 PolyDraw(graphics->hdc, custpt, tp, count);
589 custend:
590 GdipFree(custptf);
591 GdipFree(custpt);
592 GdipFree(tp);
593 GdipDeleteMatrix(matrix);
594 break;
595 default:
596 break;
599 if(!customstroke){
600 SelectObject(graphics->hdc, oldbrush);
601 SelectObject(graphics->hdc, oldpen);
602 DeleteObject(brush);
603 DeleteObject(pen);
607 /* Shortens the line by the given percent by changing x2, y2.
608 * If percent is > 1.0 then the line will change direction.
609 * If percent is negative it can lengthen the line. */
610 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
612 REAL dist, theta, dx, dy;
614 if((y1 == *y2) && (x1 == *x2))
615 return;
617 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
618 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
619 dx = cos(theta) * dist;
620 dy = sin(theta) * dist;
622 *x2 = *x2 + dx;
623 *y2 = *y2 + dy;
626 /* Shortens the line by the given amount by changing x2, y2.
627 * If the amount is greater than the distance, the line will become length 0.
628 * If the amount is negative, it can lengthen the line. */
629 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
631 REAL dx, dy, percent;
633 dx = *x2 - x1;
634 dy = *y2 - y1;
635 if(dx == 0 && dy == 0)
636 return;
638 percent = amt / sqrt(dx * dx + dy * dy);
639 if(percent >= 1.0){
640 *x2 = x1;
641 *y2 = y1;
642 return;
645 shorten_line_percent(x1, y1, x2, y2, percent);
648 /* Draws lines between the given points, and if caps is true then draws an endcap
649 * at the end of the last line. */
650 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
651 GDIPCONST GpPointF * pt, INT count, BOOL caps)
653 POINT *pti = NULL;
654 GpPointF *ptcopy = NULL;
655 GpStatus status = GenericError;
657 if(!count)
658 return Ok;
660 pti = GdipAlloc(count * sizeof(POINT));
661 ptcopy = GdipAlloc(count * sizeof(GpPointF));
663 if(!pti || !ptcopy){
664 status = OutOfMemory;
665 goto end;
668 memcpy(ptcopy, pt, count * sizeof(GpPointF));
670 if(caps){
671 if(pen->endcap == LineCapArrowAnchor)
672 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
673 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
674 else if((pen->endcap == LineCapCustom) && pen->customend)
675 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
676 &ptcopy[count-1].X, &ptcopy[count-1].Y,
677 pen->customend->inset * pen->width);
679 if(pen->startcap == LineCapArrowAnchor)
680 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
681 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
682 else if((pen->startcap == LineCapCustom) && pen->customstart)
683 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
684 &ptcopy[0].X, &ptcopy[0].Y,
685 pen->customstart->inset * pen->width);
687 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
688 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
689 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
690 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
693 transform_and_round_points(graphics, pti, ptcopy, count);
695 if(Polyline(graphics->hdc, pti, count))
696 status = Ok;
698 end:
699 GdipFree(pti);
700 GdipFree(ptcopy);
702 return status;
705 /* Conducts a linear search to find the bezier points that will back off
706 * the endpoint of the curve by a distance of amt. Linear search works
707 * better than binary in this case because there are multiple solutions,
708 * and binary searches often find a bad one. I don't think this is what
709 * Windows does but short of rendering the bezier without GDI's help it's
710 * the best we can do. If rev then work from the start of the passed points
711 * instead of the end. */
712 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
714 GpPointF origpt[4];
715 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
716 INT i, first = 0, second = 1, third = 2, fourth = 3;
718 if(rev){
719 first = 3;
720 second = 2;
721 third = 1;
722 fourth = 0;
725 origx = pt[fourth].X;
726 origy = pt[fourth].Y;
727 memcpy(origpt, pt, sizeof(GpPointF) * 4);
729 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
730 /* reset bezier points to original values */
731 memcpy(pt, origpt, sizeof(GpPointF) * 4);
732 /* Perform magic on bezier points. Order is important here.*/
733 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
734 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
735 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
736 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
737 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
738 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
740 dx = pt[fourth].X - origx;
741 dy = pt[fourth].Y - origy;
743 diff = sqrt(dx * dx + dy * dy);
744 percent += 0.0005 * amt;
748 /* Draws bezier curves between given points, and if caps is true then draws an
749 * endcap at the end of the last line. */
750 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
751 GDIPCONST GpPointF * pt, INT count, BOOL caps)
753 POINT *pti;
754 GpPointF *ptcopy;
755 GpStatus status = GenericError;
757 if(!count)
758 return Ok;
760 pti = GdipAlloc(count * sizeof(POINT));
761 ptcopy = GdipAlloc(count * sizeof(GpPointF));
763 if(!pti || !ptcopy){
764 status = OutOfMemory;
765 goto end;
768 memcpy(ptcopy, pt, count * sizeof(GpPointF));
770 if(caps){
771 if(pen->endcap == LineCapArrowAnchor)
772 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
773 else if((pen->endcap == LineCapCustom) && pen->customend)
774 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
775 FALSE);
777 if(pen->startcap == LineCapArrowAnchor)
778 shorten_bezier_amt(ptcopy, pen->width, TRUE);
779 else if((pen->startcap == LineCapCustom) && pen->customstart)
780 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
782 /* the direction of the line cap is parallel to the direction at the
783 * end of the bezier (which, if it has been shortened, is not the same
784 * as the direction from pt[count-2] to pt[count-1]) */
785 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
786 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
787 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
788 pt[count - 1].X, pt[count - 1].Y);
790 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
791 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
792 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
795 transform_and_round_points(graphics, pti, ptcopy, count);
797 PolyBezier(graphics->hdc, pti, count);
799 status = Ok;
801 end:
802 GdipFree(pti);
803 GdipFree(ptcopy);
805 return status;
808 /* Draws a combination of bezier curves and lines between points. */
809 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
810 GDIPCONST BYTE * types, INT count, BOOL caps)
812 POINT *pti = GdipAlloc(count * sizeof(POINT));
813 BYTE *tp = GdipAlloc(count);
814 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
815 INT i, j;
816 GpStatus status = GenericError;
818 if(!count){
819 status = Ok;
820 goto end;
822 if(!pti || !tp || !ptcopy){
823 status = OutOfMemory;
824 goto end;
827 for(i = 1; i < count; i++){
828 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
829 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
830 || !(types[i + 1] & PathPointTypeBezier)){
831 ERR("Bad bezier points\n");
832 goto end;
834 i += 2;
838 memcpy(ptcopy, pt, count * sizeof(GpPointF));
840 /* If we are drawing caps, go through the points and adjust them accordingly,
841 * and draw the caps. */
842 if(caps){
843 switch(types[count - 1] & PathPointTypePathTypeMask){
844 case PathPointTypeBezier:
845 if(pen->endcap == LineCapArrowAnchor)
846 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
847 else if((pen->endcap == LineCapCustom) && pen->customend)
848 shorten_bezier_amt(&ptcopy[count - 4],
849 pen->width * pen->customend->inset, FALSE);
851 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
852 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
853 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
854 pt[count - 1].X, pt[count - 1].Y);
856 break;
857 case PathPointTypeLine:
858 if(pen->endcap == LineCapArrowAnchor)
859 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
860 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
861 pen->width);
862 else if((pen->endcap == LineCapCustom) && pen->customend)
863 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
864 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
865 pen->customend->inset * pen->width);
867 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
868 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
869 pt[count - 1].Y);
871 break;
872 default:
873 ERR("Bad path last point\n");
874 goto end;
877 /* Find start of points */
878 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
879 == PathPointTypeStart); j++);
881 switch(types[j] & PathPointTypePathTypeMask){
882 case PathPointTypeBezier:
883 if(pen->startcap == LineCapArrowAnchor)
884 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
885 else if((pen->startcap == LineCapCustom) && pen->customstart)
886 shorten_bezier_amt(&ptcopy[j - 1],
887 pen->width * pen->customstart->inset, TRUE);
889 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
890 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
891 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
892 pt[j - 1].X, pt[j - 1].Y);
894 break;
895 case PathPointTypeLine:
896 if(pen->startcap == LineCapArrowAnchor)
897 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
898 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
899 pen->width);
900 else if((pen->startcap == LineCapCustom) && pen->customstart)
901 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
902 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
903 pen->customstart->inset * pen->width);
905 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
906 pt[j].X, pt[j].Y, pt[j - 1].X,
907 pt[j - 1].Y);
909 break;
910 default:
911 ERR("Bad path points\n");
912 goto end;
916 transform_and_round_points(graphics, pti, ptcopy, count);
918 for(i = 0; i < count; i++){
919 tp[i] = convert_path_point_type(types[i]);
922 PolyDraw(graphics->hdc, pti, tp, count);
924 status = Ok;
926 end:
927 GdipFree(pti);
928 GdipFree(ptcopy);
929 GdipFree(tp);
931 return status;
934 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
936 GpStatus result;
938 BeginPath(graphics->hdc);
939 result = draw_poly(graphics, NULL, path->pathdata.Points,
940 path->pathdata.Types, path->pathdata.Count, FALSE);
941 EndPath(graphics->hdc);
942 return result;
945 typedef struct _GraphicsContainerItem {
946 struct list entry;
947 GraphicsContainer contid;
949 SmoothingMode smoothing;
950 CompositingQuality compqual;
951 InterpolationMode interpolation;
952 CompositingMode compmode;
953 TextRenderingHint texthint;
954 REAL scale;
955 GpUnit unit;
956 PixelOffsetMode pixeloffset;
957 UINT textcontrast;
958 GpMatrix* worldtrans;
959 GpRegion* clip;
960 } GraphicsContainerItem;
962 static GpStatus init_container(GraphicsContainerItem** container,
963 GDIPCONST GpGraphics* graphics){
964 GpStatus sts;
966 *container = GdipAlloc(sizeof(GraphicsContainerItem));
967 if(!(*container))
968 return OutOfMemory;
970 (*container)->contid = graphics->contid + 1;
972 (*container)->smoothing = graphics->smoothing;
973 (*container)->compqual = graphics->compqual;
974 (*container)->interpolation = graphics->interpolation;
975 (*container)->compmode = graphics->compmode;
976 (*container)->texthint = graphics->texthint;
977 (*container)->scale = graphics->scale;
978 (*container)->unit = graphics->unit;
979 (*container)->textcontrast = graphics->textcontrast;
980 (*container)->pixeloffset = graphics->pixeloffset;
982 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
983 if(sts != Ok){
984 GdipFree(*container);
985 *container = NULL;
986 return sts;
989 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
990 if(sts != Ok){
991 GdipDeleteMatrix((*container)->worldtrans);
992 GdipFree(*container);
993 *container = NULL;
994 return sts;
997 return Ok;
1000 static void delete_container(GraphicsContainerItem* container){
1001 GdipDeleteMatrix(container->worldtrans);
1002 GdipDeleteRegion(container->clip);
1003 GdipFree(container);
1006 static GpStatus restore_container(GpGraphics* graphics,
1007 GDIPCONST GraphicsContainerItem* container){
1008 GpStatus sts;
1009 GpMatrix *newTrans;
1010 GpRegion *newClip;
1012 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1013 if(sts != Ok)
1014 return sts;
1016 sts = GdipCloneRegion(container->clip, &newClip);
1017 if(sts != Ok){
1018 GdipDeleteMatrix(newTrans);
1019 return sts;
1022 GdipDeleteMatrix(graphics->worldtrans);
1023 graphics->worldtrans = newTrans;
1025 GdipDeleteRegion(graphics->clip);
1026 graphics->clip = newClip;
1028 graphics->contid = container->contid - 1;
1030 graphics->smoothing = container->smoothing;
1031 graphics->compqual = container->compqual;
1032 graphics->interpolation = container->interpolation;
1033 graphics->compmode = container->compmode;
1034 graphics->texthint = container->texthint;
1035 graphics->scale = container->scale;
1036 graphics->unit = container->unit;
1037 graphics->textcontrast = container->textcontrast;
1038 graphics->pixeloffset = container->pixeloffset;
1040 return Ok;
1043 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1045 RECT wnd_rect;
1047 if(graphics->hwnd) {
1048 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1049 return GenericError;
1051 rect->X = wnd_rect.left;
1052 rect->Y = wnd_rect.top;
1053 rect->Width = wnd_rect.right - wnd_rect.left;
1054 rect->Height = wnd_rect.bottom - wnd_rect.top;
1055 }else{
1056 rect->X = 0;
1057 rect->Y = 0;
1058 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1059 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1062 return Ok;
1065 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1067 TRACE("(%p, %p)\n", hdc, graphics);
1069 return GdipCreateFromHDC2(hdc, NULL, graphics);
1072 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1074 GpStatus retval;
1076 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1078 if(hDevice != NULL) {
1079 FIXME("Don't know how to handle parameter hDevice\n");
1080 return NotImplemented;
1083 if(hdc == NULL)
1084 return OutOfMemory;
1086 if(graphics == NULL)
1087 return InvalidParameter;
1089 *graphics = GdipAlloc(sizeof(GpGraphics));
1090 if(!*graphics) return OutOfMemory;
1092 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1093 GdipFree(*graphics);
1094 return retval;
1097 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1098 GdipFree((*graphics)->worldtrans);
1099 GdipFree(*graphics);
1100 return retval;
1103 (*graphics)->hdc = hdc;
1104 (*graphics)->hwnd = WindowFromDC(hdc);
1105 (*graphics)->owndc = FALSE;
1106 (*graphics)->smoothing = SmoothingModeDefault;
1107 (*graphics)->compqual = CompositingQualityDefault;
1108 (*graphics)->interpolation = InterpolationModeDefault;
1109 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1110 (*graphics)->compmode = CompositingModeSourceOver;
1111 (*graphics)->unit = UnitDisplay;
1112 (*graphics)->scale = 1.0;
1113 (*graphics)->busy = FALSE;
1114 (*graphics)->textcontrast = 4;
1115 list_init(&(*graphics)->containers);
1116 (*graphics)->contid = 0;
1118 return Ok;
1121 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1123 GpStatus ret;
1124 HDC hdc;
1126 TRACE("(%p, %p)\n", hwnd, graphics);
1128 hdc = GetDC(hwnd);
1130 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1132 ReleaseDC(hwnd, hdc);
1133 return ret;
1136 (*graphics)->hwnd = hwnd;
1137 (*graphics)->owndc = TRUE;
1139 return Ok;
1142 /* FIXME: no icm handling */
1143 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1145 TRACE("(%p, %p)\n", hwnd, graphics);
1147 return GdipCreateFromHWND(hwnd, graphics);
1150 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1151 GpMetafile **metafile)
1153 static int calls;
1155 if(!hemf || !metafile)
1156 return InvalidParameter;
1158 if(!(calls++))
1159 FIXME("not implemented\n");
1161 return NotImplemented;
1164 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1165 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1167 IStream *stream = NULL;
1168 UINT read;
1169 BYTE* copy;
1170 HENHMETAFILE hemf;
1171 GpStatus retval = GenericError;
1173 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1175 if(!hwmf || !metafile || !placeable)
1176 return InvalidParameter;
1178 *metafile = NULL;
1179 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1180 if(!read)
1181 return GenericError;
1182 copy = GdipAlloc(read);
1183 GetMetaFileBitsEx(hwmf, read, copy);
1185 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1186 GdipFree(copy);
1188 read = GetEnhMetaFileBits(hemf, 0, NULL);
1189 copy = GdipAlloc(read);
1190 GetEnhMetaFileBits(hemf, read, copy);
1191 DeleteEnhMetaFile(hemf);
1193 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1194 ERR("could not make stream\n");
1195 GdipFree(copy);
1196 goto err;
1199 *metafile = GdipAlloc(sizeof(GpMetafile));
1200 if(!*metafile){
1201 retval = OutOfMemory;
1202 goto err;
1205 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1206 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1207 goto err;
1210 (*metafile)->image.type = ImageTypeMetafile;
1211 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1212 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
1213 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1214 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
1215 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1216 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
1217 (*metafile)->unit = UnitInch;
1219 if(delete)
1220 DeleteMetaFile(hwmf);
1222 return Ok;
1224 err:
1225 GdipFree(*metafile);
1226 IStream_Release(stream);
1227 return retval;
1230 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1231 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1233 HMETAFILE hmf = GetMetaFileW(file);
1235 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1237 if(!hmf) return InvalidParameter;
1239 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1242 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1243 GpMetafile **metafile)
1245 FIXME("(%p, %p): stub\n", file, metafile);
1246 return NotImplemented;
1249 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1250 GpMetafile **metafile)
1252 FIXME("(%p, %p): stub\n", stream, metafile);
1253 return NotImplemented;
1256 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1257 UINT access, IStream **stream)
1259 DWORD dwMode;
1260 HRESULT ret;
1262 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1264 if(!stream || !filename)
1265 return InvalidParameter;
1267 if(access & GENERIC_WRITE)
1268 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1269 else if(access & GENERIC_READ)
1270 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1271 else
1272 return InvalidParameter;
1274 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1276 return hresult_to_status(ret);
1279 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1281 GraphicsContainerItem *cont, *next;
1282 TRACE("(%p)\n", graphics);
1284 if(!graphics) return InvalidParameter;
1285 if(graphics->busy) return ObjectBusy;
1287 if(graphics->owndc)
1288 ReleaseDC(graphics->hwnd, graphics->hdc);
1290 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1291 list_remove(&cont->entry);
1292 delete_container(cont);
1295 GdipDeleteRegion(graphics->clip);
1296 GdipDeleteMatrix(graphics->worldtrans);
1297 GdipFree(graphics);
1299 return Ok;
1302 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1303 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1305 INT save_state, num_pts;
1306 GpPointF points[MAX_ARC_PTS];
1307 GpStatus retval;
1309 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1310 width, height, startAngle, sweepAngle);
1312 if(!graphics || !pen || width <= 0 || height <= 0)
1313 return InvalidParameter;
1315 if(graphics->busy)
1316 return ObjectBusy;
1318 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1320 save_state = prepare_dc(graphics, pen);
1322 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1324 restore_dc(graphics, save_state);
1326 return retval;
1329 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1330 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1332 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1333 width, height, startAngle, sweepAngle);
1335 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1338 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1339 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1341 INT save_state;
1342 GpPointF pt[4];
1343 GpStatus retval;
1345 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1346 x2, y2, x3, y3, x4, y4);
1348 if(!graphics || !pen)
1349 return InvalidParameter;
1351 if(graphics->busy)
1352 return ObjectBusy;
1354 pt[0].X = x1;
1355 pt[0].Y = y1;
1356 pt[1].X = x2;
1357 pt[1].Y = y2;
1358 pt[2].X = x3;
1359 pt[2].Y = y3;
1360 pt[3].X = x4;
1361 pt[3].Y = y4;
1363 save_state = prepare_dc(graphics, pen);
1365 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1367 restore_dc(graphics, save_state);
1369 return retval;
1372 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1373 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1375 INT save_state;
1376 GpPointF pt[4];
1377 GpStatus retval;
1379 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1380 x2, y2, x3, y3, x4, y4);
1382 if(!graphics || !pen)
1383 return InvalidParameter;
1385 if(graphics->busy)
1386 return ObjectBusy;
1388 pt[0].X = x1;
1389 pt[0].Y = y1;
1390 pt[1].X = x2;
1391 pt[1].Y = y2;
1392 pt[2].X = x3;
1393 pt[2].Y = y3;
1394 pt[3].X = x4;
1395 pt[3].Y = y4;
1397 save_state = prepare_dc(graphics, pen);
1399 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1401 restore_dc(graphics, save_state);
1403 return retval;
1406 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1407 GDIPCONST GpPointF *points, INT count)
1409 INT i;
1410 GpStatus ret;
1412 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1414 if(!graphics || !pen || !points || (count <= 0))
1415 return InvalidParameter;
1417 if(graphics->busy)
1418 return ObjectBusy;
1420 for(i = 0; i < floor(count / 4); i++){
1421 ret = GdipDrawBezier(graphics, pen,
1422 points[4*i].X, points[4*i].Y,
1423 points[4*i + 1].X, points[4*i + 1].Y,
1424 points[4*i + 2].X, points[4*i + 2].Y,
1425 points[4*i + 3].X, points[4*i + 3].Y);
1426 if(ret != Ok)
1427 return ret;
1430 return Ok;
1433 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1434 GDIPCONST GpPoint *points, INT count)
1436 GpPointF *pts;
1437 GpStatus ret;
1438 INT i;
1440 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1442 if(!graphics || !pen || !points || (count <= 0))
1443 return InvalidParameter;
1445 if(graphics->busy)
1446 return ObjectBusy;
1448 pts = GdipAlloc(sizeof(GpPointF) * count);
1449 if(!pts)
1450 return OutOfMemory;
1452 for(i = 0; i < count; i++){
1453 pts[i].X = (REAL)points[i].X;
1454 pts[i].Y = (REAL)points[i].Y;
1457 ret = GdipDrawBeziers(graphics,pen,pts,count);
1459 GdipFree(pts);
1461 return ret;
1464 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1465 GDIPCONST GpPointF *points, INT count)
1467 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1469 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1472 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1473 GDIPCONST GpPoint *points, INT count)
1475 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1477 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1480 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1481 GDIPCONST GpPointF *points, INT count, REAL tension)
1483 GpPath *path;
1484 GpStatus stat;
1486 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1488 if(!graphics || !pen || !points || count <= 0)
1489 return InvalidParameter;
1491 if(graphics->busy)
1492 return ObjectBusy;
1494 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1495 return stat;
1497 stat = GdipAddPathClosedCurve2(path, points, count, tension);
1498 if(stat != Ok){
1499 GdipDeletePath(path);
1500 return stat;
1503 stat = GdipDrawPath(graphics, pen, path);
1505 GdipDeletePath(path);
1507 return stat;
1510 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1511 GDIPCONST GpPoint *points, INT count, REAL tension)
1513 GpPointF *ptf;
1514 GpStatus stat;
1515 INT i;
1517 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1519 if(!points || count <= 0)
1520 return InvalidParameter;
1522 ptf = GdipAlloc(sizeof(GpPointF)*count);
1523 if(!ptf)
1524 return OutOfMemory;
1526 for(i = 0; i < count; i++){
1527 ptf[i].X = (REAL)points[i].X;
1528 ptf[i].Y = (REAL)points[i].Y;
1531 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1533 GdipFree(ptf);
1535 return stat;
1538 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1539 GDIPCONST GpPointF *points, INT count)
1541 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1543 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1546 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1547 GDIPCONST GpPoint *points, INT count)
1549 GpPointF *pointsF;
1550 GpStatus ret;
1551 INT i;
1553 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1555 if(!points)
1556 return InvalidParameter;
1558 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1559 if(!pointsF)
1560 return OutOfMemory;
1562 for(i = 0; i < count; i++){
1563 pointsF[i].X = (REAL)points[i].X;
1564 pointsF[i].Y = (REAL)points[i].Y;
1567 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1568 GdipFree(pointsF);
1570 return ret;
1573 /* Approximates cardinal spline with Bezier curves. */
1574 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1575 GDIPCONST GpPointF *points, INT count, REAL tension)
1577 /* PolyBezier expects count*3-2 points. */
1578 INT i, len_pt = count*3-2, save_state;
1579 GpPointF *pt;
1580 REAL x1, x2, y1, y2;
1581 GpStatus retval;
1583 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1585 if(!graphics || !pen)
1586 return InvalidParameter;
1588 if(graphics->busy)
1589 return ObjectBusy;
1591 if(count < 2)
1592 return InvalidParameter;
1594 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1595 if(!pt)
1596 return OutOfMemory;
1598 tension = tension * TENSION_CONST;
1600 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1601 tension, &x1, &y1);
1603 pt[0].X = points[0].X;
1604 pt[0].Y = points[0].Y;
1605 pt[1].X = x1;
1606 pt[1].Y = y1;
1608 for(i = 0; i < count-2; i++){
1609 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1611 pt[3*i+2].X = x1;
1612 pt[3*i+2].Y = y1;
1613 pt[3*i+3].X = points[i+1].X;
1614 pt[3*i+3].Y = points[i+1].Y;
1615 pt[3*i+4].X = x2;
1616 pt[3*i+4].Y = y2;
1619 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1620 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1622 pt[len_pt-2].X = x1;
1623 pt[len_pt-2].Y = y1;
1624 pt[len_pt-1].X = points[count-1].X;
1625 pt[len_pt-1].Y = points[count-1].Y;
1627 save_state = prepare_dc(graphics, pen);
1629 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1631 GdipFree(pt);
1632 restore_dc(graphics, save_state);
1634 return retval;
1637 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1638 GDIPCONST GpPoint *points, INT count, REAL tension)
1640 GpPointF *pointsF;
1641 GpStatus ret;
1642 INT i;
1644 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1646 if(!points)
1647 return InvalidParameter;
1649 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1650 if(!pointsF)
1651 return OutOfMemory;
1653 for(i = 0; i < count; i++){
1654 pointsF[i].X = (REAL)points[i].X;
1655 pointsF[i].Y = (REAL)points[i].Y;
1658 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1659 GdipFree(pointsF);
1661 return ret;
1664 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1665 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1666 REAL tension)
1668 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1670 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1671 return InvalidParameter;
1674 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1677 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1678 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1679 REAL tension)
1681 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1683 if(count < 0){
1684 return OutOfMemory;
1687 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1688 return InvalidParameter;
1691 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1694 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1695 REAL y, REAL width, REAL height)
1697 INT save_state;
1698 GpPointF ptf[2];
1699 POINT pti[2];
1701 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1703 if(!graphics || !pen)
1704 return InvalidParameter;
1706 if(graphics->busy)
1707 return ObjectBusy;
1709 ptf[0].X = x;
1710 ptf[0].Y = y;
1711 ptf[1].X = x + width;
1712 ptf[1].Y = y + height;
1714 save_state = prepare_dc(graphics, pen);
1715 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1717 transform_and_round_points(graphics, pti, ptf, 2);
1719 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1721 restore_dc(graphics, save_state);
1723 return Ok;
1726 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1727 INT y, INT width, INT height)
1729 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1731 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1735 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1737 UINT width, height;
1738 GpPointF points[3];
1740 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1742 if(!graphics || !image)
1743 return InvalidParameter;
1745 GdipGetImageWidth(image, &width);
1746 GdipGetImageHeight(image, &height);
1748 /* FIXME: we should use the graphics and image dpi, somehow */
1750 points[0].X = points[2].X = x;
1751 points[0].Y = points[1].Y = y;
1752 points[1].X = x + width;
1753 points[2].Y = y + height;
1755 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
1756 UnitPixel, NULL, NULL, NULL);
1759 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1760 INT y)
1762 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1764 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1767 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1768 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1769 GpUnit srcUnit)
1771 FIXME("(%p, %p, %f, %f, %f, %f, %f, %f, %d): stub\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1772 return NotImplemented;
1775 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
1776 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
1777 GpUnit srcUnit)
1779 FIXME("(%p, %p, %d, %d, %d, %d, %d, %d, %d): stub\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1780 return NotImplemented;
1783 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
1784 GDIPCONST GpPointF *dstpoints, INT count)
1786 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1787 return NotImplemented;
1790 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
1791 GDIPCONST GpPoint *dstpoints, INT count)
1793 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1794 return NotImplemented;
1797 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1798 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1799 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1800 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1801 DrawImageAbort callback, VOID * callbackData)
1803 GpPointF ptf[3];
1804 POINT pti[3];
1805 REAL dx, dy;
1807 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1808 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1809 callbackData);
1811 if(!graphics || !image || !points || count != 3)
1812 return InvalidParameter;
1814 if(srcUnit == UnitInch)
1815 dx = dy = (REAL) INCH_HIMETRIC;
1816 else if(srcUnit == UnitPixel){
1817 dx = ((REAL) INCH_HIMETRIC) /
1818 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1819 dy = ((REAL) INCH_HIMETRIC) /
1820 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1822 else
1823 return NotImplemented;
1825 memcpy(ptf, points, 3 * sizeof(GpPointF));
1826 transform_and_round_points(graphics, pti, ptf, 3);
1828 /* IPicture renders bitmaps with the y-axis reversed
1829 * FIXME: flipping for unknown image type might not be correct. */
1830 if(image->type != ImageTypeMetafile){
1831 INT temp;
1832 temp = pti[0].y;
1833 pti[0].y = pti[2].y;
1834 pti[2].y = temp;
1837 if(IPicture_Render(image->picture, graphics->hdc,
1838 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1839 srcx * dx, srcy * dy,
1840 srcwidth * dx, srcheight * dy,
1841 NULL) != S_OK){
1842 if(callback)
1843 callback(callbackData);
1844 return GenericError;
1847 return Ok;
1850 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1851 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1852 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1853 DrawImageAbort callback, VOID * callbackData)
1855 GpPointF pointsF[3];
1856 INT i;
1858 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
1859 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1860 callbackData);
1862 if(!points || count!=3)
1863 return InvalidParameter;
1865 for(i = 0; i < count; i++){
1866 pointsF[i].X = (REAL)points[i].X;
1867 pointsF[i].Y = (REAL)points[i].Y;
1870 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1871 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1872 callback, callbackData);
1875 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1876 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1877 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1878 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1879 VOID * callbackData)
1881 GpPointF points[3];
1883 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
1884 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1885 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1887 points[0].X = dstx;
1888 points[0].Y = dsty;
1889 points[1].X = dstx + dstwidth;
1890 points[1].Y = dsty;
1891 points[2].X = dstx;
1892 points[2].Y = dsty + dstheight;
1894 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1895 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1898 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1899 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1900 INT srcwidth, INT srcheight, GpUnit srcUnit,
1901 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1902 VOID * callbackData)
1904 GpPointF points[3];
1906 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
1907 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1908 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1910 points[0].X = dstx;
1911 points[0].Y = dsty;
1912 points[1].X = dstx + dstwidth;
1913 points[1].Y = dsty;
1914 points[2].X = dstx;
1915 points[2].Y = dsty + dstheight;
1917 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1918 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1921 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1922 REAL x, REAL y, REAL width, REAL height)
1924 RectF bounds;
1925 GpUnit unit;
1926 GpStatus ret;
1928 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
1930 if(!graphics || !image)
1931 return InvalidParameter;
1933 ret = GdipGetImageBounds(image, &bounds, &unit);
1934 if(ret != Ok)
1935 return ret;
1937 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1938 bounds.X, bounds.Y, bounds.Width, bounds.Height,
1939 unit, NULL, NULL, NULL);
1942 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1943 INT x, INT y, INT width, INT height)
1945 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
1947 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1950 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1951 REAL y1, REAL x2, REAL y2)
1953 INT save_state;
1954 GpPointF pt[2];
1955 GpStatus retval;
1957 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
1959 if(!pen || !graphics)
1960 return InvalidParameter;
1962 if(graphics->busy)
1963 return ObjectBusy;
1965 pt[0].X = x1;
1966 pt[0].Y = y1;
1967 pt[1].X = x2;
1968 pt[1].Y = y2;
1970 save_state = prepare_dc(graphics, pen);
1972 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1974 restore_dc(graphics, save_state);
1976 return retval;
1979 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1980 INT y1, INT x2, INT y2)
1982 INT save_state;
1983 GpPointF pt[2];
1984 GpStatus retval;
1986 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
1988 if(!pen || !graphics)
1989 return InvalidParameter;
1991 if(graphics->busy)
1992 return ObjectBusy;
1994 pt[0].X = (REAL)x1;
1995 pt[0].Y = (REAL)y1;
1996 pt[1].X = (REAL)x2;
1997 pt[1].Y = (REAL)y2;
1999 save_state = prepare_dc(graphics, pen);
2001 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2003 restore_dc(graphics, save_state);
2005 return retval;
2008 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2009 GpPointF *points, INT count)
2011 INT save_state;
2012 GpStatus retval;
2014 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2016 if(!pen || !graphics || (count < 2))
2017 return InvalidParameter;
2019 if(graphics->busy)
2020 return ObjectBusy;
2022 save_state = prepare_dc(graphics, pen);
2024 retval = draw_polyline(graphics, pen, points, count, TRUE);
2026 restore_dc(graphics, save_state);
2028 return retval;
2031 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2032 GpPoint *points, INT count)
2034 INT save_state;
2035 GpStatus retval;
2036 GpPointF *ptf = NULL;
2037 int i;
2039 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2041 if(!pen || !graphics || (count < 2))
2042 return InvalidParameter;
2044 if(graphics->busy)
2045 return ObjectBusy;
2047 ptf = GdipAlloc(count * sizeof(GpPointF));
2048 if(!ptf) return OutOfMemory;
2050 for(i = 0; i < count; i ++){
2051 ptf[i].X = (REAL) points[i].X;
2052 ptf[i].Y = (REAL) points[i].Y;
2055 save_state = prepare_dc(graphics, pen);
2057 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2059 restore_dc(graphics, save_state);
2061 GdipFree(ptf);
2062 return retval;
2065 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2067 INT save_state;
2068 GpStatus retval;
2070 TRACE("(%p, %p, %p)\n", graphics, pen, path);
2072 if(!pen || !graphics)
2073 return InvalidParameter;
2075 if(graphics->busy)
2076 return ObjectBusy;
2078 save_state = prepare_dc(graphics, pen);
2080 retval = draw_poly(graphics, pen, path->pathdata.Points,
2081 path->pathdata.Types, path->pathdata.Count, TRUE);
2083 restore_dc(graphics, save_state);
2085 return retval;
2088 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2089 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2091 INT save_state;
2093 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2094 width, height, startAngle, sweepAngle);
2096 if(!graphics || !pen)
2097 return InvalidParameter;
2099 if(graphics->busy)
2100 return ObjectBusy;
2102 save_state = prepare_dc(graphics, pen);
2103 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2105 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2107 restore_dc(graphics, save_state);
2109 return Ok;
2112 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2113 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2115 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2116 width, height, startAngle, sweepAngle);
2118 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2121 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2122 REAL y, REAL width, REAL height)
2124 INT save_state;
2125 GpPointF ptf[4];
2126 POINT pti[4];
2128 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2130 if(!pen || !graphics)
2131 return InvalidParameter;
2133 if(graphics->busy)
2134 return ObjectBusy;
2136 ptf[0].X = x;
2137 ptf[0].Y = y;
2138 ptf[1].X = x + width;
2139 ptf[1].Y = y;
2140 ptf[2].X = x + width;
2141 ptf[2].Y = y + height;
2142 ptf[3].X = x;
2143 ptf[3].Y = y + height;
2145 save_state = prepare_dc(graphics, pen);
2146 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2148 transform_and_round_points(graphics, pti, ptf, 4);
2149 Polygon(graphics->hdc, pti, 4);
2151 restore_dc(graphics, save_state);
2153 return Ok;
2156 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2157 INT y, INT width, INT height)
2159 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2161 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2164 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2165 GDIPCONST GpRectF* rects, INT count)
2167 GpPointF *ptf;
2168 POINT *pti;
2169 INT save_state, i;
2171 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2173 if(!graphics || !pen || !rects || count < 1)
2174 return InvalidParameter;
2176 if(graphics->busy)
2177 return ObjectBusy;
2179 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2180 pti = GdipAlloc(4 * count * sizeof(POINT));
2182 if(!ptf || !pti){
2183 GdipFree(ptf);
2184 GdipFree(pti);
2185 return OutOfMemory;
2188 for(i = 0; i < count; i++){
2189 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2190 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2191 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2192 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2195 save_state = prepare_dc(graphics, pen);
2196 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2198 transform_and_round_points(graphics, pti, ptf, 4 * count);
2200 for(i = 0; i < count; i++)
2201 Polygon(graphics->hdc, &pti[4 * i], 4);
2203 restore_dc(graphics, save_state);
2205 GdipFree(ptf);
2206 GdipFree(pti);
2208 return Ok;
2211 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2212 GDIPCONST GpRect* rects, INT count)
2214 GpRectF *rectsF;
2215 GpStatus ret;
2216 INT i;
2218 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2220 if(!rects || count<=0)
2221 return InvalidParameter;
2223 rectsF = GdipAlloc(sizeof(GpRectF) * count);
2224 if(!rectsF)
2225 return OutOfMemory;
2227 for(i = 0;i < count;i++){
2228 rectsF[i].X = (REAL)rects[i].X;
2229 rectsF[i].Y = (REAL)rects[i].Y;
2230 rectsF[i].Width = (REAL)rects[i].Width;
2231 rectsF[i].Height = (REAL)rects[i].Height;
2234 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2235 GdipFree(rectsF);
2237 return ret;
2240 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
2241 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
2242 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
2244 HRGN rgn = NULL;
2245 HFONT gdifont;
2246 LOGFONTW lfw;
2247 TEXTMETRICW textmet;
2248 GpPointF pt[2], rectcpy[4];
2249 POINT corners[4];
2250 WCHAR* stringdup;
2251 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
2252 INT sum = 0, height = 0, offsety = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
2253 nheight, lineend;
2254 SIZE size;
2255 POINT drawbase;
2256 UINT drawflags;
2257 RECT drawcoord;
2259 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
2260 length, font, debugstr_rectf(rect), format, brush);
2262 if(!graphics || !string || !font || !brush || !rect)
2263 return InvalidParameter;
2265 if((brush->bt != BrushTypeSolidColor)){
2266 FIXME("not implemented for given parameters\n");
2267 return NotImplemented;
2270 if(format){
2271 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2273 /* Should be no need to explicitly test for StringAlignmentNear as
2274 * that is default behavior if no alignment is passed. */
2275 if(format->vertalign != StringAlignmentNear){
2276 RectF bounds;
2277 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
2279 if(format->vertalign == StringAlignmentCenter)
2280 offsety = (rect->Height - bounds.Height) / 2;
2281 else if(format->vertalign == StringAlignmentFar)
2282 offsety = (rect->Height - bounds.Height);
2286 if(length == -1) length = lstrlenW(string);
2288 stringdup = GdipAlloc(length * sizeof(WCHAR));
2289 if(!stringdup) return OutOfMemory;
2291 save_state = SaveDC(graphics->hdc);
2292 SetBkMode(graphics->hdc, TRANSPARENT);
2293 SetTextColor(graphics->hdc, brush->lb.lbColor);
2295 rectcpy[3].X = rectcpy[0].X = rect->X;
2296 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
2297 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
2298 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
2299 transform_and_round_points(graphics, corners, rectcpy, 4);
2301 if (roundr(rect->Width) == 0)
2303 rel_width = 1.0;
2304 nwidth = INT_MAX;
2306 else
2308 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
2309 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
2310 / rect->Width;
2311 nwidth = roundr(rel_width * rect->Width);
2314 if (roundr(rect->Height) == 0)
2316 rel_height = 1.0;
2317 nheight = INT_MAX;
2319 else
2321 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
2322 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
2323 / rect->Height;
2324 nheight = roundr(rel_height * rect->Height);
2327 if (roundr(rect->Width) != 0 && roundr(rect->Height) != 0)
2329 /* FIXME: If only the width or only the height is 0, we should probably still clip */
2330 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
2331 SelectClipRgn(graphics->hdc, rgn);
2334 /* Use gdi to find the font, then perform transformations on it (height,
2335 * width, angle). */
2336 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2337 GetTextMetricsW(graphics->hdc, &textmet);
2338 lfw = font->lfw;
2340 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
2341 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
2343 pt[0].X = 0.0;
2344 pt[0].Y = 0.0;
2345 pt[1].X = 1.0;
2346 pt[1].Y = 0.0;
2347 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
2348 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2349 ang_cos = cos(angle);
2350 ang_sin = sin(angle);
2351 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2353 gdifont = CreateFontIndirectW(&lfw);
2354 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
2356 for(i = 0, j = 0; i < length; i++){
2357 if(!isprintW(string[i]) && (string[i] != '\n'))
2358 continue;
2360 stringdup[j] = string[i];
2361 j++;
2364 length = j;
2366 if (!format || format->align == StringAlignmentNear)
2368 drawbase.x = corners[0].x;
2369 drawbase.y = corners[0].y;
2370 drawflags = DT_NOCLIP | DT_EXPANDTABS;
2372 else if (format->align == StringAlignmentCenter)
2374 drawbase.x = (corners[0].x + corners[1].x)/2;
2375 drawbase.y = (corners[0].y + corners[1].y)/2;
2376 drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
2378 else /* (format->align == StringAlignmentFar) */
2380 drawbase.x = corners[1].x;
2381 drawbase.y = corners[1].y;
2382 drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
2385 while(sum < length){
2386 drawcoord.left = drawcoord.right = drawbase.x + roundr(ang_sin * (REAL) height);
2387 drawcoord.top = drawcoord.bottom = drawbase.y + roundr(ang_cos * (REAL) height);
2389 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2390 nwidth, &fit, NULL, &size);
2391 fitcpy = fit;
2393 if(fit == 0){
2394 DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, drawflags);
2395 break;
2398 for(lret = 0; lret < fit; lret++)
2399 if(*(stringdup + sum + lret) == '\n')
2400 break;
2402 /* Line break code (may look strange, but it imitates windows). */
2403 if(lret < fit)
2404 lineend = fit = lret; /* this is not an off-by-one error */
2405 else if(fit < (length - sum)){
2406 if(*(stringdup + sum + fit) == ' ')
2407 while(*(stringdup + sum + fit) == ' ')
2408 fit++;
2409 else
2410 while(*(stringdup + sum + fit - 1) != ' '){
2411 fit--;
2413 if(*(stringdup + sum + fit) == '\t')
2414 break;
2416 if(fit == 0){
2417 fit = fitcpy;
2418 break;
2421 lineend = fit;
2422 while(*(stringdup + sum + lineend - 1) == ' ' ||
2423 *(stringdup + sum + lineend - 1) == '\t')
2424 lineend--;
2426 else
2427 lineend = fit;
2428 DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, lineend),
2429 &drawcoord, drawflags);
2431 sum += fit + (lret < fitcpy ? 1 : 0);
2432 height += size.cy;
2434 if(height > nheight)
2435 break;
2437 /* Stop if this was a linewrap (but not if it was a linebreak). */
2438 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2439 break;
2442 GdipFree(stringdup);
2443 DeleteObject(rgn);
2444 DeleteObject(gdifont);
2446 RestoreDC(graphics->hdc, save_state);
2448 return Ok;
2451 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2452 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2454 GpPath *path;
2455 GpStatus stat;
2457 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2458 count, tension, fill);
2460 if(!graphics || !brush || !points)
2461 return InvalidParameter;
2463 if(graphics->busy)
2464 return ObjectBusy;
2466 stat = GdipCreatePath(fill, &path);
2467 if(stat != Ok)
2468 return stat;
2470 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2471 if(stat != Ok){
2472 GdipDeletePath(path);
2473 return stat;
2476 stat = GdipFillPath(graphics, brush, path);
2477 if(stat != Ok){
2478 GdipDeletePath(path);
2479 return stat;
2482 GdipDeletePath(path);
2484 return Ok;
2487 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2488 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2490 GpPointF *ptf;
2491 GpStatus stat;
2492 INT i;
2494 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2495 count, tension, fill);
2497 if(!points || count <= 0)
2498 return InvalidParameter;
2500 ptf = GdipAlloc(sizeof(GpPointF)*count);
2501 if(!ptf)
2502 return OutOfMemory;
2504 for(i = 0;i < count;i++){
2505 ptf[i].X = (REAL)points[i].X;
2506 ptf[i].Y = (REAL)points[i].Y;
2509 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2511 GdipFree(ptf);
2513 return stat;
2516 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2517 REAL y, REAL width, REAL height)
2519 INT save_state;
2520 GpPointF ptf[2];
2521 POINT pti[2];
2523 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2525 if(!graphics || !brush)
2526 return InvalidParameter;
2528 if(graphics->busy)
2529 return ObjectBusy;
2531 ptf[0].X = x;
2532 ptf[0].Y = y;
2533 ptf[1].X = x + width;
2534 ptf[1].Y = y + height;
2536 save_state = SaveDC(graphics->hdc);
2537 EndPath(graphics->hdc);
2539 transform_and_round_points(graphics, pti, ptf, 2);
2541 BeginPath(graphics->hdc);
2542 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2543 EndPath(graphics->hdc);
2545 brush_fill_path(graphics, brush);
2547 RestoreDC(graphics->hdc, save_state);
2549 return Ok;
2552 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2553 INT y, INT width, INT height)
2555 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2557 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2560 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2562 INT save_state;
2563 GpStatus retval;
2565 TRACE("(%p, %p, %p)\n", graphics, brush, path);
2567 if(!brush || !graphics || !path)
2568 return InvalidParameter;
2570 if(graphics->busy)
2571 return ObjectBusy;
2573 save_state = SaveDC(graphics->hdc);
2574 EndPath(graphics->hdc);
2575 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2576 : WINDING));
2578 BeginPath(graphics->hdc);
2579 retval = draw_poly(graphics, NULL, path->pathdata.Points,
2580 path->pathdata.Types, path->pathdata.Count, FALSE);
2582 if(retval != Ok)
2583 goto end;
2585 EndPath(graphics->hdc);
2586 brush_fill_path(graphics, brush);
2588 retval = Ok;
2590 end:
2591 RestoreDC(graphics->hdc, save_state);
2593 return retval;
2596 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2597 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2599 INT save_state;
2601 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2602 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2604 if(!graphics || !brush)
2605 return InvalidParameter;
2607 if(graphics->busy)
2608 return ObjectBusy;
2610 save_state = SaveDC(graphics->hdc);
2611 EndPath(graphics->hdc);
2613 BeginPath(graphics->hdc);
2614 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2615 EndPath(graphics->hdc);
2617 brush_fill_path(graphics, brush);
2619 RestoreDC(graphics->hdc, save_state);
2621 return Ok;
2624 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2625 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2627 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2628 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2630 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2633 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2634 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2636 INT save_state;
2637 GpPointF *ptf = NULL;
2638 POINT *pti = NULL;
2639 GpStatus retval = Ok;
2641 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2643 if(!graphics || !brush || !points || !count)
2644 return InvalidParameter;
2646 if(graphics->busy)
2647 return ObjectBusy;
2649 ptf = GdipAlloc(count * sizeof(GpPointF));
2650 pti = GdipAlloc(count * sizeof(POINT));
2651 if(!ptf || !pti){
2652 retval = OutOfMemory;
2653 goto end;
2656 memcpy(ptf, points, count * sizeof(GpPointF));
2658 save_state = SaveDC(graphics->hdc);
2659 EndPath(graphics->hdc);
2660 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2661 : WINDING));
2663 transform_and_round_points(graphics, pti, ptf, count);
2665 BeginPath(graphics->hdc);
2666 Polygon(graphics->hdc, pti, count);
2667 EndPath(graphics->hdc);
2669 brush_fill_path(graphics, brush);
2671 RestoreDC(graphics->hdc, save_state);
2673 end:
2674 GdipFree(ptf);
2675 GdipFree(pti);
2677 return retval;
2680 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2681 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2683 INT save_state, i;
2684 GpPointF *ptf = NULL;
2685 POINT *pti = NULL;
2686 GpStatus retval = Ok;
2688 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2690 if(!graphics || !brush || !points || !count)
2691 return InvalidParameter;
2693 if(graphics->busy)
2694 return ObjectBusy;
2696 ptf = GdipAlloc(count * sizeof(GpPointF));
2697 pti = GdipAlloc(count * sizeof(POINT));
2698 if(!ptf || !pti){
2699 retval = OutOfMemory;
2700 goto end;
2703 for(i = 0; i < count; i ++){
2704 ptf[i].X = (REAL) points[i].X;
2705 ptf[i].Y = (REAL) points[i].Y;
2708 save_state = SaveDC(graphics->hdc);
2709 EndPath(graphics->hdc);
2710 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2711 : WINDING));
2713 transform_and_round_points(graphics, pti, ptf, count);
2715 BeginPath(graphics->hdc);
2716 Polygon(graphics->hdc, pti, count);
2717 EndPath(graphics->hdc);
2719 brush_fill_path(graphics, brush);
2721 RestoreDC(graphics->hdc, save_state);
2723 end:
2724 GdipFree(ptf);
2725 GdipFree(pti);
2727 return retval;
2730 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2731 GDIPCONST GpPointF *points, INT count)
2733 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2735 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2738 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2739 GDIPCONST GpPoint *points, INT count)
2741 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2743 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2746 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2747 REAL x, REAL y, REAL width, REAL height)
2749 INT save_state;
2750 GpPointF ptf[4];
2751 POINT pti[4];
2753 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2755 if(!graphics || !brush)
2756 return InvalidParameter;
2758 if(graphics->busy)
2759 return ObjectBusy;
2761 ptf[0].X = x;
2762 ptf[0].Y = y;
2763 ptf[1].X = x + width;
2764 ptf[1].Y = y;
2765 ptf[2].X = x + width;
2766 ptf[2].Y = y + height;
2767 ptf[3].X = x;
2768 ptf[3].Y = y + height;
2770 save_state = SaveDC(graphics->hdc);
2771 EndPath(graphics->hdc);
2773 transform_and_round_points(graphics, pti, ptf, 4);
2775 BeginPath(graphics->hdc);
2776 Polygon(graphics->hdc, pti, 4);
2777 EndPath(graphics->hdc);
2779 brush_fill_path(graphics, brush);
2781 RestoreDC(graphics->hdc, save_state);
2783 return Ok;
2786 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2787 INT x, INT y, INT width, INT height)
2789 INT save_state;
2790 GpPointF ptf[4];
2791 POINT pti[4];
2793 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2795 if(!graphics || !brush)
2796 return InvalidParameter;
2798 if(graphics->busy)
2799 return ObjectBusy;
2801 ptf[0].X = x;
2802 ptf[0].Y = y;
2803 ptf[1].X = x + width;
2804 ptf[1].Y = y;
2805 ptf[2].X = x + width;
2806 ptf[2].Y = y + height;
2807 ptf[3].X = x;
2808 ptf[3].Y = y + height;
2810 save_state = SaveDC(graphics->hdc);
2811 EndPath(graphics->hdc);
2813 transform_and_round_points(graphics, pti, ptf, 4);
2815 BeginPath(graphics->hdc);
2816 Polygon(graphics->hdc, pti, 4);
2817 EndPath(graphics->hdc);
2819 brush_fill_path(graphics, brush);
2821 RestoreDC(graphics->hdc, save_state);
2823 return Ok;
2826 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2827 INT count)
2829 GpStatus ret;
2830 INT i;
2832 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2834 if(!rects)
2835 return InvalidParameter;
2837 for(i = 0; i < count; i++){
2838 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2839 if(ret != Ok) return ret;
2842 return Ok;
2845 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2846 INT count)
2848 GpRectF *rectsF;
2849 GpStatus ret;
2850 INT i;
2852 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2854 if(!rects || count <= 0)
2855 return InvalidParameter;
2857 rectsF = GdipAlloc(sizeof(GpRectF)*count);
2858 if(!rectsF)
2859 return OutOfMemory;
2861 for(i = 0; i < count; i++){
2862 rectsF[i].X = (REAL)rects[i].X;
2863 rectsF[i].Y = (REAL)rects[i].Y;
2864 rectsF[i].X = (REAL)rects[i].Width;
2865 rectsF[i].Height = (REAL)rects[i].Height;
2868 ret = GdipFillRectangles(graphics,brush,rectsF,count);
2869 GdipFree(rectsF);
2871 return ret;
2874 /*****************************************************************************
2875 * GdipFillRegion [GDIPLUS.@]
2877 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2878 GpRegion* region)
2880 INT save_state;
2881 GpStatus status;
2882 HRGN hrgn;
2883 RECT rc;
2885 TRACE("(%p, %p, %p)\n", graphics, brush, region);
2887 if (!(graphics && brush && region))
2888 return InvalidParameter;
2890 if(graphics->busy)
2891 return ObjectBusy;
2893 status = GdipGetRegionHRgn(region, graphics, &hrgn);
2894 if(status != Ok)
2895 return status;
2897 save_state = SaveDC(graphics->hdc);
2898 EndPath(graphics->hdc);
2900 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
2902 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
2904 BeginPath(graphics->hdc);
2905 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
2906 EndPath(graphics->hdc);
2908 brush_fill_path(graphics, brush);
2911 RestoreDC(graphics->hdc, save_state);
2913 DeleteObject(hrgn);
2915 return Ok;
2918 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2920 static int calls;
2922 if(!graphics)
2923 return InvalidParameter;
2925 if(graphics->busy)
2926 return ObjectBusy;
2928 if(!(calls++))
2929 FIXME("not implemented\n");
2931 return NotImplemented;
2934 /*****************************************************************************
2935 * GdipGetClipBounds [GDIPLUS.@]
2937 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
2939 TRACE("(%p, %p)\n", graphics, rect);
2941 if(!graphics)
2942 return InvalidParameter;
2944 if(graphics->busy)
2945 return ObjectBusy;
2947 return GdipGetRegionBounds(graphics->clip, graphics, rect);
2950 /*****************************************************************************
2951 * GdipGetClipBoundsI [GDIPLUS.@]
2953 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
2955 TRACE("(%p, %p)\n", graphics, rect);
2957 if(!graphics)
2958 return InvalidParameter;
2960 if(graphics->busy)
2961 return ObjectBusy;
2963 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
2966 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2967 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2968 CompositingMode *mode)
2970 TRACE("(%p, %p)\n", graphics, mode);
2972 if(!graphics || !mode)
2973 return InvalidParameter;
2975 if(graphics->busy)
2976 return ObjectBusy;
2978 *mode = graphics->compmode;
2980 return Ok;
2983 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
2984 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
2985 CompositingQuality *quality)
2987 TRACE("(%p, %p)\n", graphics, quality);
2989 if(!graphics || !quality)
2990 return InvalidParameter;
2992 if(graphics->busy)
2993 return ObjectBusy;
2995 *quality = graphics->compqual;
2997 return Ok;
3000 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3001 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3002 InterpolationMode *mode)
3004 TRACE("(%p, %p)\n", graphics, mode);
3006 if(!graphics || !mode)
3007 return InvalidParameter;
3009 if(graphics->busy)
3010 return ObjectBusy;
3012 *mode = graphics->interpolation;
3014 return Ok;
3017 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3019 if(!graphics || !argb)
3020 return InvalidParameter;
3022 if(graphics->busy)
3023 return ObjectBusy;
3025 FIXME("(%p, %p): stub\n", graphics, argb);
3027 return NotImplemented;
3030 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3032 TRACE("(%p, %p)\n", graphics, scale);
3034 if(!graphics || !scale)
3035 return InvalidParameter;
3037 if(graphics->busy)
3038 return ObjectBusy;
3040 *scale = graphics->scale;
3042 return Ok;
3045 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3047 TRACE("(%p, %p)\n", graphics, unit);
3049 if(!graphics || !unit)
3050 return InvalidParameter;
3052 if(graphics->busy)
3053 return ObjectBusy;
3055 *unit = graphics->unit;
3057 return Ok;
3060 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3061 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3062 *mode)
3064 TRACE("(%p, %p)\n", graphics, mode);
3066 if(!graphics || !mode)
3067 return InvalidParameter;
3069 if(graphics->busy)
3070 return ObjectBusy;
3072 *mode = graphics->pixeloffset;
3074 return Ok;
3077 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3078 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3080 TRACE("(%p, %p)\n", graphics, mode);
3082 if(!graphics || !mode)
3083 return InvalidParameter;
3085 if(graphics->busy)
3086 return ObjectBusy;
3088 *mode = graphics->smoothing;
3090 return Ok;
3093 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3095 TRACE("(%p, %p)\n", graphics, contrast);
3097 if(!graphics || !contrast)
3098 return InvalidParameter;
3100 *contrast = graphics->textcontrast;
3102 return Ok;
3105 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3106 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3107 TextRenderingHint *hint)
3109 TRACE("(%p, %p)\n", graphics, hint);
3111 if(!graphics || !hint)
3112 return InvalidParameter;
3114 if(graphics->busy)
3115 return ObjectBusy;
3117 *hint = graphics->texthint;
3119 return Ok;
3122 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3124 GpRegion *clip_rgn;
3125 GpStatus stat;
3126 GpRectF wnd_rect;
3128 TRACE("(%p, %p)\n", graphics, rect);
3130 if(!graphics || !rect)
3131 return InvalidParameter;
3133 if(graphics->busy)
3134 return ObjectBusy;
3136 /* get window bounds */
3137 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok)
3138 return stat;
3140 /* intersect window and graphics clipping regions */
3141 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3142 return stat;
3144 if((stat = GdipCombineRegionRect(clip_rgn, &wnd_rect, CombineModeIntersect)) != Ok)
3145 goto cleanup;
3147 if((stat = GdipCombineRegionRegion(clip_rgn, graphics->clip, CombineModeIntersect)) != Ok)
3148 goto cleanup;
3150 /* get bounds of the region */
3151 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3153 cleanup:
3154 GdipDeleteRegion(clip_rgn);
3156 return stat;
3159 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3161 GpRectF rectf;
3162 GpStatus stat;
3164 TRACE("(%p, %p)\n", graphics, rect);
3166 if(!graphics || !rect)
3167 return InvalidParameter;
3169 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
3171 rect->X = roundr(rectf.X);
3172 rect->Y = roundr(rectf.Y);
3173 rect->Width = roundr(rectf.Width);
3174 rect->Height = roundr(rectf.Height);
3177 return stat;
3180 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3182 TRACE("(%p, %p)\n", graphics, matrix);
3184 if(!graphics || !matrix)
3185 return InvalidParameter;
3187 if(graphics->busy)
3188 return ObjectBusy;
3190 *matrix = *graphics->worldtrans;
3191 return Ok;
3194 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3196 GpSolidFill *brush;
3197 GpStatus stat;
3198 GpRectF wnd_rect;
3200 TRACE("(%p, %x)\n", graphics, color);
3202 if(!graphics)
3203 return InvalidParameter;
3205 if(graphics->busy)
3206 return ObjectBusy;
3208 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3209 return stat;
3211 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
3212 GdipDeleteBrush((GpBrush*)brush);
3213 return stat;
3216 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
3217 wnd_rect.Width, wnd_rect.Height);
3219 GdipDeleteBrush((GpBrush*)brush);
3221 return Ok;
3224 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3226 TRACE("(%p, %p)\n", graphics, res);
3228 if(!graphics || !res)
3229 return InvalidParameter;
3231 return GdipIsEmptyRegion(graphics->clip, graphics, res);
3234 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3236 FIXME("(%p, %.2f, %.2f, %p) stub\n", graphics, x, y, result);
3238 if(!graphics || !result)
3239 return InvalidParameter;
3241 if(graphics->busy)
3242 return ObjectBusy;
3244 return NotImplemented;
3247 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3249 FIXME("(%p, %d, %d, %p) stub\n", graphics, x, y, result);
3251 if(!graphics || !result)
3252 return InvalidParameter;
3254 if(graphics->busy)
3255 return ObjectBusy;
3257 return NotImplemented;
3260 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3261 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3262 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3263 INT regionCount, GpRegion** regions)
3265 if (!(graphics && string && font && layoutRect && stringFormat && regions))
3266 return InvalidParameter;
3268 FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
3269 length, font, layoutRect, stringFormat, regionCount, regions);
3271 return NotImplemented;
3274 /* Find the smallest rectangle that bounds the text when it is printed in rect
3275 * according to the format options listed in format. If rect has 0 width and
3276 * height, then just find the smallest rectangle that bounds the text when it's
3277 * printed at location (rect->X, rect-Y). */
3278 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3279 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3280 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3281 INT *codepointsfitted, INT *linesfilled)
3283 HFONT oldfont;
3284 WCHAR* stringdup;
3285 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
3286 nheight, lineend;
3287 SIZE size;
3289 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3290 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3291 bounds, codepointsfitted, linesfilled);
3293 if(!graphics || !string || !font || !rect)
3294 return InvalidParameter;
3296 if(linesfilled) *linesfilled = 0;
3297 if(codepointsfitted) *codepointsfitted = 0;
3299 if(format)
3300 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3302 if(length == -1) length = lstrlenW(string);
3304 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3305 if(!stringdup) return OutOfMemory;
3307 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3308 nwidth = roundr(rect->Width);
3309 nheight = roundr(rect->Height);
3311 if((nwidth == 0) && (nheight == 0))
3312 nwidth = nheight = INT_MAX;
3314 for(i = 0, j = 0; i < length; i++){
3315 if(!isprintW(string[i]) && (string[i] != '\n'))
3316 continue;
3318 stringdup[j] = string[i];
3319 j++;
3322 stringdup[j] = 0;
3323 length = j;
3325 while(sum < length){
3326 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3327 nwidth, &fit, NULL, &size);
3328 fitcpy = fit;
3330 if(fit == 0)
3331 break;
3333 for(lret = 0; lret < fit; lret++)
3334 if(*(stringdup + sum + lret) == '\n')
3335 break;
3337 /* Line break code (may look strange, but it imitates windows). */
3338 if(lret < fit)
3339 lineend = fit = lret; /* this is not an off-by-one error */
3340 else if(fit < (length - sum)){
3341 if(*(stringdup + sum + fit) == ' ')
3342 while(*(stringdup + sum + fit) == ' ')
3343 fit++;
3344 else
3345 while(*(stringdup + sum + fit - 1) != ' '){
3346 fit--;
3348 if(*(stringdup + sum + fit) == '\t')
3349 break;
3351 if(fit == 0){
3352 fit = fitcpy;
3353 break;
3356 lineend = fit;
3357 while(*(stringdup + sum + lineend - 1) == ' ' ||
3358 *(stringdup + sum + lineend - 1) == '\t')
3359 lineend--;
3361 else
3362 lineend = fit;
3364 GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3365 nwidth, &j, NULL, &size);
3367 sum += fit + (lret < fitcpy ? 1 : 0);
3368 if(codepointsfitted) *codepointsfitted = sum;
3370 height += size.cy;
3371 if(linesfilled) *linesfilled += size.cy;
3372 max_width = max(max_width, size.cx);
3374 if(height > nheight)
3375 break;
3377 /* Stop if this was a linewrap (but not if it was a linebreak). */
3378 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3379 break;
3382 bounds->X = rect->X;
3383 bounds->Y = rect->Y;
3384 bounds->Width = (REAL)max_width;
3385 bounds->Height = (REAL) min(height, nheight);
3387 GdipFree(stringdup);
3388 DeleteObject(SelectObject(graphics->hdc, oldfont));
3390 return Ok;
3393 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
3395 TRACE("(%p)\n", graphics);
3397 if(!graphics)
3398 return InvalidParameter;
3400 if(graphics->busy)
3401 return ObjectBusy;
3403 return GdipSetInfinite(graphics->clip);
3406 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
3408 TRACE("(%p)\n", graphics);
3410 if(!graphics)
3411 return InvalidParameter;
3413 if(graphics->busy)
3414 return ObjectBusy;
3416 graphics->worldtrans->matrix[0] = 1.0;
3417 graphics->worldtrans->matrix[1] = 0.0;
3418 graphics->worldtrans->matrix[2] = 0.0;
3419 graphics->worldtrans->matrix[3] = 1.0;
3420 graphics->worldtrans->matrix[4] = 0.0;
3421 graphics->worldtrans->matrix[5] = 0.0;
3423 return Ok;
3426 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
3428 return GdipEndContainer(graphics, state);
3431 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
3432 GpMatrixOrder order)
3434 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
3436 if(!graphics)
3437 return InvalidParameter;
3439 if(graphics->busy)
3440 return ObjectBusy;
3442 return GdipRotateMatrix(graphics->worldtrans, angle, order);
3445 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
3447 return GdipBeginContainer2(graphics, state);
3450 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
3451 GraphicsContainer *state)
3453 GraphicsContainerItem *container;
3454 GpStatus sts;
3456 TRACE("(%p, %p)\n", graphics, state);
3458 if(!graphics || !state)
3459 return InvalidParameter;
3461 sts = init_container(&container, graphics);
3462 if(sts != Ok)
3463 return sts;
3465 list_add_head(&graphics->containers, &container->entry);
3466 *state = graphics->contid = container->contid;
3468 return Ok;
3471 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
3473 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3474 return NotImplemented;
3477 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
3479 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3480 return NotImplemented;
3483 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
3485 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
3486 return NotImplemented;
3489 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
3491 GpStatus sts;
3492 GraphicsContainerItem *container, *container2;
3494 TRACE("(%p, %x)\n", graphics, state);
3496 if(!graphics)
3497 return InvalidParameter;
3499 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
3500 if(container->contid == state)
3501 break;
3504 /* did not find a matching container */
3505 if(&container->entry == &graphics->containers)
3506 return Ok;
3508 sts = restore_container(graphics, container);
3509 if(sts != Ok)
3510 return sts;
3512 /* remove all of the containers on top of the found container */
3513 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
3514 if(container->contid == state)
3515 break;
3516 list_remove(&container->entry);
3517 delete_container(container);
3520 list_remove(&container->entry);
3521 delete_container(container);
3523 return Ok;
3526 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
3527 REAL sy, GpMatrixOrder order)
3529 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
3531 if(!graphics)
3532 return InvalidParameter;
3534 if(graphics->busy)
3535 return ObjectBusy;
3537 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
3540 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
3541 CombineMode mode)
3543 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
3545 if(!graphics || !srcgraphics)
3546 return InvalidParameter;
3548 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
3551 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
3552 CompositingMode mode)
3554 TRACE("(%p, %d)\n", graphics, mode);
3556 if(!graphics)
3557 return InvalidParameter;
3559 if(graphics->busy)
3560 return ObjectBusy;
3562 graphics->compmode = mode;
3564 return Ok;
3567 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
3568 CompositingQuality quality)
3570 TRACE("(%p, %d)\n", graphics, quality);
3572 if(!graphics)
3573 return InvalidParameter;
3575 if(graphics->busy)
3576 return ObjectBusy;
3578 graphics->compqual = quality;
3580 return Ok;
3583 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
3584 InterpolationMode mode)
3586 TRACE("(%p, %d)\n", graphics, mode);
3588 if(!graphics)
3589 return InvalidParameter;
3591 if(graphics->busy)
3592 return ObjectBusy;
3594 graphics->interpolation = mode;
3596 return Ok;
3599 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
3601 TRACE("(%p, %.2f)\n", graphics, scale);
3603 if(!graphics || (scale <= 0.0))
3604 return InvalidParameter;
3606 if(graphics->busy)
3607 return ObjectBusy;
3609 graphics->scale = scale;
3611 return Ok;
3614 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
3616 TRACE("(%p, %d)\n", graphics, unit);
3618 if(!graphics)
3619 return InvalidParameter;
3621 if(graphics->busy)
3622 return ObjectBusy;
3624 if(unit == UnitWorld)
3625 return InvalidParameter;
3627 graphics->unit = unit;
3629 return Ok;
3632 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3633 mode)
3635 TRACE("(%p, %d)\n", graphics, mode);
3637 if(!graphics)
3638 return InvalidParameter;
3640 if(graphics->busy)
3641 return ObjectBusy;
3643 graphics->pixeloffset = mode;
3645 return Ok;
3648 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
3650 static int calls;
3652 TRACE("(%p,%i,%i)\n", graphics, x, y);
3654 if (!(calls++))
3655 FIXME("not implemented\n");
3657 return NotImplemented;
3660 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
3662 TRACE("(%p, %d)\n", graphics, mode);
3664 if(!graphics)
3665 return InvalidParameter;
3667 if(graphics->busy)
3668 return ObjectBusy;
3670 graphics->smoothing = mode;
3672 return Ok;
3675 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
3677 TRACE("(%p, %d)\n", graphics, contrast);
3679 if(!graphics)
3680 return InvalidParameter;
3682 graphics->textcontrast = contrast;
3684 return Ok;
3687 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
3688 TextRenderingHint hint)
3690 TRACE("(%p, %d)\n", graphics, hint);
3692 if(!graphics)
3693 return InvalidParameter;
3695 if(graphics->busy)
3696 return ObjectBusy;
3698 graphics->texthint = hint;
3700 return Ok;
3703 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3705 TRACE("(%p, %p)\n", graphics, matrix);
3707 if(!graphics || !matrix)
3708 return InvalidParameter;
3710 if(graphics->busy)
3711 return ObjectBusy;
3713 GdipDeleteMatrix(graphics->worldtrans);
3714 return GdipCloneMatrix(matrix, &graphics->worldtrans);
3717 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
3718 REAL dy, GpMatrixOrder order)
3720 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
3722 if(!graphics)
3723 return InvalidParameter;
3725 if(graphics->busy)
3726 return ObjectBusy;
3728 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
3731 /*****************************************************************************
3732 * GdipSetClipHrgn [GDIPLUS.@]
3734 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
3736 GpRegion *region;
3737 GpStatus status;
3739 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
3741 if(!graphics)
3742 return InvalidParameter;
3744 status = GdipCreateRegionHrgn(hrgn, &region);
3745 if(status != Ok)
3746 return status;
3748 status = GdipSetClipRegion(graphics, region, mode);
3750 GdipDeleteRegion(region);
3751 return status;
3754 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
3756 TRACE("(%p, %p, %d)\n", graphics, path, mode);
3758 if(!graphics)
3759 return InvalidParameter;
3761 if(graphics->busy)
3762 return ObjectBusy;
3764 return GdipCombineRegionPath(graphics->clip, path, mode);
3767 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
3768 REAL width, REAL height,
3769 CombineMode mode)
3771 GpRectF rect;
3773 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
3775 if(!graphics)
3776 return InvalidParameter;
3778 if(graphics->busy)
3779 return ObjectBusy;
3781 rect.X = x;
3782 rect.Y = y;
3783 rect.Width = width;
3784 rect.Height = height;
3786 return GdipCombineRegionRect(graphics->clip, &rect, mode);
3789 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
3790 INT width, INT height,
3791 CombineMode mode)
3793 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3795 if(!graphics)
3796 return InvalidParameter;
3798 if(graphics->busy)
3799 return ObjectBusy;
3801 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3804 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
3805 CombineMode mode)
3807 TRACE("(%p, %p, %d)\n", graphics, region, mode);
3809 if(!graphics || !region)
3810 return InvalidParameter;
3812 if(graphics->busy)
3813 return ObjectBusy;
3815 return GdipCombineRegionRegion(graphics->clip, region, mode);
3818 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
3819 UINT limitDpi)
3821 static int calls;
3823 if(!(calls++))
3824 FIXME("not implemented\n");
3826 return NotImplemented;
3829 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
3830 INT count)
3832 INT save_state;
3833 POINT *pti;
3835 TRACE("(%p, %p, %d)\n", graphics, points, count);
3837 if(!graphics || !pen || count<=0)
3838 return InvalidParameter;
3840 if(graphics->busy)
3841 return ObjectBusy;
3843 pti = GdipAlloc(sizeof(POINT) * count);
3845 save_state = prepare_dc(graphics, pen);
3846 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3848 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
3849 Polygon(graphics->hdc, pti, count);
3851 restore_dc(graphics, save_state);
3852 GdipFree(pti);
3854 return Ok;
3857 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
3858 INT count)
3860 GpStatus ret;
3861 GpPointF *ptf;
3862 INT i;
3864 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3866 if(count<=0) return InvalidParameter;
3867 ptf = GdipAlloc(sizeof(GpPointF) * count);
3869 for(i = 0;i < count; i++){
3870 ptf[i].X = (REAL)points[i].X;
3871 ptf[i].Y = (REAL)points[i].Y;
3874 ret = GdipDrawPolygon(graphics,pen,ptf,count);
3875 GdipFree(ptf);
3877 return ret;
3880 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
3882 TRACE("(%p, %p)\n", graphics, dpi);
3884 if(!graphics || !dpi)
3885 return InvalidParameter;
3887 if(graphics->busy)
3888 return ObjectBusy;
3890 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
3892 return Ok;
3895 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
3897 TRACE("(%p, %p)\n", graphics, dpi);
3899 if(!graphics || !dpi)
3900 return InvalidParameter;
3902 if(graphics->busy)
3903 return ObjectBusy;
3905 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
3907 return Ok;
3910 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
3911 GpMatrixOrder order)
3913 GpMatrix m;
3914 GpStatus ret;
3916 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
3918 if(!graphics || !matrix)
3919 return InvalidParameter;
3921 if(graphics->busy)
3922 return ObjectBusy;
3924 m = *(graphics->worldtrans);
3926 ret = GdipMultiplyMatrix(&m, matrix, order);
3927 if(ret == Ok)
3928 *(graphics->worldtrans) = m;
3930 return ret;
3933 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
3935 TRACE("(%p, %p)\n", graphics, hdc);
3937 if(!graphics || !hdc)
3938 return InvalidParameter;
3940 if(graphics->busy)
3941 return ObjectBusy;
3943 *hdc = graphics->hdc;
3944 graphics->busy = TRUE;
3946 return Ok;
3949 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
3951 TRACE("(%p, %p)\n", graphics, hdc);
3953 if(!graphics)
3954 return InvalidParameter;
3956 if(graphics->hdc != hdc || !(graphics->busy))
3957 return InvalidParameter;
3959 graphics->busy = FALSE;
3961 return Ok;
3964 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
3966 GpRegion *clip;
3967 GpStatus status;
3969 TRACE("(%p, %p)\n", graphics, region);
3971 if(!graphics || !region)
3972 return InvalidParameter;
3974 if(graphics->busy)
3975 return ObjectBusy;
3977 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
3978 return status;
3980 /* free everything except root node and header */
3981 delete_element(&region->node);
3982 memcpy(region, clip, sizeof(GpRegion));
3984 return Ok;
3987 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
3988 GpCoordinateSpace src_space, GpPointF *points, INT count)
3990 GpMatrix *matrix;
3991 GpStatus stat;
3992 REAL unitscale;
3994 if(!graphics || !points || count <= 0)
3995 return InvalidParameter;
3997 if(graphics->busy)
3998 return ObjectBusy;
4000 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4002 if (src_space == dst_space) return Ok;
4004 stat = GdipCreateMatrix(&matrix);
4005 if (stat == Ok)
4007 unitscale = convert_unit(graphics->hdc, graphics->unit);
4009 if(graphics->unit != UnitDisplay)
4010 unitscale *= graphics->scale;
4012 /* transform from src_space to CoordinateSpacePage */
4013 switch (src_space)
4015 case CoordinateSpaceWorld:
4016 GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
4017 break;
4018 case CoordinateSpacePage:
4019 break;
4020 case CoordinateSpaceDevice:
4021 GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
4022 break;
4025 /* transform from CoordinateSpacePage to dst_space */
4026 switch (dst_space)
4028 case CoordinateSpaceWorld:
4030 GpMatrix *inverted_transform;
4031 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
4032 if (stat == Ok)
4034 stat = GdipInvertMatrix(inverted_transform);
4035 if (stat == Ok)
4036 GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
4037 GdipDeleteMatrix(inverted_transform);
4039 break;
4041 case CoordinateSpacePage:
4042 break;
4043 case CoordinateSpaceDevice:
4044 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
4045 break;
4048 if (stat == Ok)
4049 stat = GdipTransformMatrixPoints(matrix, points, count);
4051 GdipDeleteMatrix(matrix);
4054 return stat;
4057 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
4058 GpCoordinateSpace src_space, GpPoint *points, INT count)
4060 GpPointF *pointsF;
4061 GpStatus ret;
4062 INT i;
4064 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4066 if(count <= 0)
4067 return InvalidParameter;
4069 pointsF = GdipAlloc(sizeof(GpPointF) * count);
4070 if(!pointsF)
4071 return OutOfMemory;
4073 for(i = 0; i < count; i++){
4074 pointsF[i].X = (REAL)points[i].X;
4075 pointsF[i].Y = (REAL)points[i].Y;
4078 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
4080 if(ret == Ok)
4081 for(i = 0; i < count; i++){
4082 points[i].X = roundr(pointsF[i].X);
4083 points[i].Y = roundr(pointsF[i].Y);
4085 GdipFree(pointsF);
4087 return ret;
4090 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
4092 FIXME("\n");
4094 return NULL;
4097 /*****************************************************************************
4098 * GdipTranslateClip [GDIPLUS.@]
4100 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
4102 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
4104 if(!graphics)
4105 return InvalidParameter;
4107 if(graphics->busy)
4108 return ObjectBusy;
4110 return GdipTranslateRegion(graphics->clip, dx, dy);
4113 /*****************************************************************************
4114 * GdipTranslateClipI [GDIPLUS.@]
4116 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
4118 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
4120 if(!graphics)
4121 return InvalidParameter;
4123 if(graphics->busy)
4124 return ObjectBusy;
4126 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
4130 /*****************************************************************************
4131 * GdipMeasureDriverString [GDIPLUS.@]
4133 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4134 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
4135 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
4137 FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
4138 return NotImplemented;
4141 /*****************************************************************************
4142 * GdipDrawDriverString [GDIPLUS.@]
4144 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4145 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
4146 GDIPCONST PointF *positions, INT flags,
4147 GDIPCONST GpMatrix *matrix )
4149 FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4150 return NotImplemented;
4153 /*****************************************************************************
4154 * GdipRecordMetafileI [GDIPLUS.@]
4156 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
4157 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4159 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
4160 return NotImplemented;
4163 /*****************************************************************************
4164 * GdipIsVisibleRectI [GDIPLUS.@]
4166 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4168 FIXME("(%p %d %d %d %d %p): stub\n", graphics, x, y, width, height, result);
4169 return NotImplemented;