gdiplus: Add utility function get_visible_clip_region.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blob297393a72d8b466e29955f0b79b3a73c9937365f
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 /* on success, rgn will contain the region of the graphics object which
1066 * is visible after clipping has been applied */
1067 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1069 GpStatus stat;
1070 GpRectF rectf;
1071 GpRegion* tmp;
1073 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1074 return stat;
1076 if((stat = GdipCreateRegion(&tmp)) != Ok)
1077 return stat;
1079 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1080 goto end;
1082 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1083 goto end;
1085 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1087 end:
1088 GdipDeleteRegion(tmp);
1089 return stat;
1092 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1094 TRACE("(%p, %p)\n", hdc, graphics);
1096 return GdipCreateFromHDC2(hdc, NULL, graphics);
1099 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1101 GpStatus retval;
1103 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1105 if(hDevice != NULL) {
1106 FIXME("Don't know how to handle parameter hDevice\n");
1107 return NotImplemented;
1110 if(hdc == NULL)
1111 return OutOfMemory;
1113 if(graphics == NULL)
1114 return InvalidParameter;
1116 *graphics = GdipAlloc(sizeof(GpGraphics));
1117 if(!*graphics) return OutOfMemory;
1119 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1120 GdipFree(*graphics);
1121 return retval;
1124 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1125 GdipFree((*graphics)->worldtrans);
1126 GdipFree(*graphics);
1127 return retval;
1130 (*graphics)->hdc = hdc;
1131 (*graphics)->hwnd = WindowFromDC(hdc);
1132 (*graphics)->owndc = FALSE;
1133 (*graphics)->smoothing = SmoothingModeDefault;
1134 (*graphics)->compqual = CompositingQualityDefault;
1135 (*graphics)->interpolation = InterpolationModeDefault;
1136 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1137 (*graphics)->compmode = CompositingModeSourceOver;
1138 (*graphics)->unit = UnitDisplay;
1139 (*graphics)->scale = 1.0;
1140 (*graphics)->busy = FALSE;
1141 (*graphics)->textcontrast = 4;
1142 list_init(&(*graphics)->containers);
1143 (*graphics)->contid = 0;
1145 return Ok;
1148 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1150 GpStatus ret;
1151 HDC hdc;
1153 TRACE("(%p, %p)\n", hwnd, graphics);
1155 hdc = GetDC(hwnd);
1157 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1159 ReleaseDC(hwnd, hdc);
1160 return ret;
1163 (*graphics)->hwnd = hwnd;
1164 (*graphics)->owndc = TRUE;
1166 return Ok;
1169 /* FIXME: no icm handling */
1170 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1172 TRACE("(%p, %p)\n", hwnd, graphics);
1174 return GdipCreateFromHWND(hwnd, graphics);
1177 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1178 GpMetafile **metafile)
1180 static int calls;
1182 if(!hemf || !metafile)
1183 return InvalidParameter;
1185 if(!(calls++))
1186 FIXME("not implemented\n");
1188 return NotImplemented;
1191 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1192 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1194 IStream *stream = NULL;
1195 UINT read;
1196 BYTE* copy;
1197 HENHMETAFILE hemf;
1198 GpStatus retval = GenericError;
1200 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1202 if(!hwmf || !metafile || !placeable)
1203 return InvalidParameter;
1205 *metafile = NULL;
1206 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1207 if(!read)
1208 return GenericError;
1209 copy = GdipAlloc(read);
1210 GetMetaFileBitsEx(hwmf, read, copy);
1212 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1213 GdipFree(copy);
1215 read = GetEnhMetaFileBits(hemf, 0, NULL);
1216 copy = GdipAlloc(read);
1217 GetEnhMetaFileBits(hemf, read, copy);
1218 DeleteEnhMetaFile(hemf);
1220 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1221 ERR("could not make stream\n");
1222 GdipFree(copy);
1223 goto err;
1226 *metafile = GdipAlloc(sizeof(GpMetafile));
1227 if(!*metafile){
1228 retval = OutOfMemory;
1229 goto err;
1232 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1233 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1234 goto err;
1237 (*metafile)->image.type = ImageTypeMetafile;
1238 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1239 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
1240 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1241 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
1242 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1243 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
1244 (*metafile)->unit = UnitInch;
1246 if(delete)
1247 DeleteMetaFile(hwmf);
1249 return Ok;
1251 err:
1252 GdipFree(*metafile);
1253 IStream_Release(stream);
1254 return retval;
1257 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1258 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1260 HMETAFILE hmf = GetMetaFileW(file);
1262 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1264 if(!hmf) return InvalidParameter;
1266 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1269 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1270 GpMetafile **metafile)
1272 FIXME("(%p, %p): stub\n", file, metafile);
1273 return NotImplemented;
1276 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1277 GpMetafile **metafile)
1279 FIXME("(%p, %p): stub\n", stream, metafile);
1280 return NotImplemented;
1283 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1284 UINT access, IStream **stream)
1286 DWORD dwMode;
1287 HRESULT ret;
1289 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1291 if(!stream || !filename)
1292 return InvalidParameter;
1294 if(access & GENERIC_WRITE)
1295 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1296 else if(access & GENERIC_READ)
1297 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1298 else
1299 return InvalidParameter;
1301 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1303 return hresult_to_status(ret);
1306 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1308 GraphicsContainerItem *cont, *next;
1309 TRACE("(%p)\n", graphics);
1311 if(!graphics) return InvalidParameter;
1312 if(graphics->busy) return ObjectBusy;
1314 if(graphics->owndc)
1315 ReleaseDC(graphics->hwnd, graphics->hdc);
1317 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1318 list_remove(&cont->entry);
1319 delete_container(cont);
1322 GdipDeleteRegion(graphics->clip);
1323 GdipDeleteMatrix(graphics->worldtrans);
1324 GdipFree(graphics);
1326 return Ok;
1329 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1330 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1332 INT save_state, num_pts;
1333 GpPointF points[MAX_ARC_PTS];
1334 GpStatus retval;
1336 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1337 width, height, startAngle, sweepAngle);
1339 if(!graphics || !pen || width <= 0 || height <= 0)
1340 return InvalidParameter;
1342 if(graphics->busy)
1343 return ObjectBusy;
1345 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1347 save_state = prepare_dc(graphics, pen);
1349 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1351 restore_dc(graphics, save_state);
1353 return retval;
1356 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1357 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1359 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1360 width, height, startAngle, sweepAngle);
1362 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1365 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1366 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1368 INT save_state;
1369 GpPointF pt[4];
1370 GpStatus retval;
1372 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1373 x2, y2, x3, y3, x4, y4);
1375 if(!graphics || !pen)
1376 return InvalidParameter;
1378 if(graphics->busy)
1379 return ObjectBusy;
1381 pt[0].X = x1;
1382 pt[0].Y = y1;
1383 pt[1].X = x2;
1384 pt[1].Y = y2;
1385 pt[2].X = x3;
1386 pt[2].Y = y3;
1387 pt[3].X = x4;
1388 pt[3].Y = y4;
1390 save_state = prepare_dc(graphics, pen);
1392 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1394 restore_dc(graphics, save_state);
1396 return retval;
1399 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1400 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1402 INT save_state;
1403 GpPointF pt[4];
1404 GpStatus retval;
1406 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1407 x2, y2, x3, y3, x4, y4);
1409 if(!graphics || !pen)
1410 return InvalidParameter;
1412 if(graphics->busy)
1413 return ObjectBusy;
1415 pt[0].X = x1;
1416 pt[0].Y = y1;
1417 pt[1].X = x2;
1418 pt[1].Y = y2;
1419 pt[2].X = x3;
1420 pt[2].Y = y3;
1421 pt[3].X = x4;
1422 pt[3].Y = y4;
1424 save_state = prepare_dc(graphics, pen);
1426 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1428 restore_dc(graphics, save_state);
1430 return retval;
1433 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1434 GDIPCONST GpPointF *points, INT count)
1436 INT i;
1437 GpStatus ret;
1439 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1441 if(!graphics || !pen || !points || (count <= 0))
1442 return InvalidParameter;
1444 if(graphics->busy)
1445 return ObjectBusy;
1447 for(i = 0; i < floor(count / 4); i++){
1448 ret = GdipDrawBezier(graphics, pen,
1449 points[4*i].X, points[4*i].Y,
1450 points[4*i + 1].X, points[4*i + 1].Y,
1451 points[4*i + 2].X, points[4*i + 2].Y,
1452 points[4*i + 3].X, points[4*i + 3].Y);
1453 if(ret != Ok)
1454 return ret;
1457 return Ok;
1460 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1461 GDIPCONST GpPoint *points, INT count)
1463 GpPointF *pts;
1464 GpStatus ret;
1465 INT i;
1467 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1469 if(!graphics || !pen || !points || (count <= 0))
1470 return InvalidParameter;
1472 if(graphics->busy)
1473 return ObjectBusy;
1475 pts = GdipAlloc(sizeof(GpPointF) * count);
1476 if(!pts)
1477 return OutOfMemory;
1479 for(i = 0; i < count; i++){
1480 pts[i].X = (REAL)points[i].X;
1481 pts[i].Y = (REAL)points[i].Y;
1484 ret = GdipDrawBeziers(graphics,pen,pts,count);
1486 GdipFree(pts);
1488 return ret;
1491 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1492 GDIPCONST GpPointF *points, INT count)
1494 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1496 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1499 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1500 GDIPCONST GpPoint *points, INT count)
1502 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1504 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1507 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1508 GDIPCONST GpPointF *points, INT count, REAL tension)
1510 GpPath *path;
1511 GpStatus stat;
1513 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1515 if(!graphics || !pen || !points || count <= 0)
1516 return InvalidParameter;
1518 if(graphics->busy)
1519 return ObjectBusy;
1521 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1522 return stat;
1524 stat = GdipAddPathClosedCurve2(path, points, count, tension);
1525 if(stat != Ok){
1526 GdipDeletePath(path);
1527 return stat;
1530 stat = GdipDrawPath(graphics, pen, path);
1532 GdipDeletePath(path);
1534 return stat;
1537 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1538 GDIPCONST GpPoint *points, INT count, REAL tension)
1540 GpPointF *ptf;
1541 GpStatus stat;
1542 INT i;
1544 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1546 if(!points || count <= 0)
1547 return InvalidParameter;
1549 ptf = GdipAlloc(sizeof(GpPointF)*count);
1550 if(!ptf)
1551 return OutOfMemory;
1553 for(i = 0; i < count; i++){
1554 ptf[i].X = (REAL)points[i].X;
1555 ptf[i].Y = (REAL)points[i].Y;
1558 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1560 GdipFree(ptf);
1562 return stat;
1565 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1566 GDIPCONST GpPointF *points, INT count)
1568 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1570 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1573 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1574 GDIPCONST GpPoint *points, INT count)
1576 GpPointF *pointsF;
1577 GpStatus ret;
1578 INT i;
1580 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1582 if(!points)
1583 return InvalidParameter;
1585 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1586 if(!pointsF)
1587 return OutOfMemory;
1589 for(i = 0; i < count; i++){
1590 pointsF[i].X = (REAL)points[i].X;
1591 pointsF[i].Y = (REAL)points[i].Y;
1594 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1595 GdipFree(pointsF);
1597 return ret;
1600 /* Approximates cardinal spline with Bezier curves. */
1601 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1602 GDIPCONST GpPointF *points, INT count, REAL tension)
1604 /* PolyBezier expects count*3-2 points. */
1605 INT i, len_pt = count*3-2, save_state;
1606 GpPointF *pt;
1607 REAL x1, x2, y1, y2;
1608 GpStatus retval;
1610 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1612 if(!graphics || !pen)
1613 return InvalidParameter;
1615 if(graphics->busy)
1616 return ObjectBusy;
1618 if(count < 2)
1619 return InvalidParameter;
1621 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1622 if(!pt)
1623 return OutOfMemory;
1625 tension = tension * TENSION_CONST;
1627 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1628 tension, &x1, &y1);
1630 pt[0].X = points[0].X;
1631 pt[0].Y = points[0].Y;
1632 pt[1].X = x1;
1633 pt[1].Y = y1;
1635 for(i = 0; i < count-2; i++){
1636 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1638 pt[3*i+2].X = x1;
1639 pt[3*i+2].Y = y1;
1640 pt[3*i+3].X = points[i+1].X;
1641 pt[3*i+3].Y = points[i+1].Y;
1642 pt[3*i+4].X = x2;
1643 pt[3*i+4].Y = y2;
1646 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1647 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1649 pt[len_pt-2].X = x1;
1650 pt[len_pt-2].Y = y1;
1651 pt[len_pt-1].X = points[count-1].X;
1652 pt[len_pt-1].Y = points[count-1].Y;
1654 save_state = prepare_dc(graphics, pen);
1656 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1658 GdipFree(pt);
1659 restore_dc(graphics, save_state);
1661 return retval;
1664 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1665 GDIPCONST GpPoint *points, INT count, REAL tension)
1667 GpPointF *pointsF;
1668 GpStatus ret;
1669 INT i;
1671 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1673 if(!points)
1674 return InvalidParameter;
1676 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1677 if(!pointsF)
1678 return OutOfMemory;
1680 for(i = 0; i < count; i++){
1681 pointsF[i].X = (REAL)points[i].X;
1682 pointsF[i].Y = (REAL)points[i].Y;
1685 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1686 GdipFree(pointsF);
1688 return ret;
1691 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1692 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1693 REAL tension)
1695 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1697 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1698 return InvalidParameter;
1701 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1704 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1705 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1706 REAL tension)
1708 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1710 if(count < 0){
1711 return OutOfMemory;
1714 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1715 return InvalidParameter;
1718 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1721 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1722 REAL y, REAL width, REAL height)
1724 INT save_state;
1725 GpPointF ptf[2];
1726 POINT pti[2];
1728 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1730 if(!graphics || !pen)
1731 return InvalidParameter;
1733 if(graphics->busy)
1734 return ObjectBusy;
1736 ptf[0].X = x;
1737 ptf[0].Y = y;
1738 ptf[1].X = x + width;
1739 ptf[1].Y = y + height;
1741 save_state = prepare_dc(graphics, pen);
1742 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1744 transform_and_round_points(graphics, pti, ptf, 2);
1746 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1748 restore_dc(graphics, save_state);
1750 return Ok;
1753 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1754 INT y, INT width, INT height)
1756 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1758 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1762 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1764 UINT width, height;
1765 GpPointF points[3];
1767 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1769 if(!graphics || !image)
1770 return InvalidParameter;
1772 GdipGetImageWidth(image, &width);
1773 GdipGetImageHeight(image, &height);
1775 /* FIXME: we should use the graphics and image dpi, somehow */
1777 points[0].X = points[2].X = x;
1778 points[0].Y = points[1].Y = y;
1779 points[1].X = x + width;
1780 points[2].Y = y + height;
1782 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
1783 UnitPixel, NULL, NULL, NULL);
1786 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1787 INT y)
1789 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1791 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1794 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1795 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1796 GpUnit srcUnit)
1798 FIXME("(%p, %p, %f, %f, %f, %f, %f, %f, %d): stub\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1799 return NotImplemented;
1802 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
1803 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
1804 GpUnit srcUnit)
1806 FIXME("(%p, %p, %d, %d, %d, %d, %d, %d, %d): stub\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1807 return NotImplemented;
1810 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
1811 GDIPCONST GpPointF *dstpoints, INT count)
1813 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1814 return NotImplemented;
1817 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
1818 GDIPCONST GpPoint *dstpoints, INT count)
1820 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1821 return NotImplemented;
1824 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1825 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1826 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1827 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1828 DrawImageAbort callback, VOID * callbackData)
1830 GpPointF ptf[3];
1831 POINT pti[3];
1832 REAL dx, dy;
1834 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1835 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1836 callbackData);
1838 if(!graphics || !image || !points || count != 3)
1839 return InvalidParameter;
1841 if(srcUnit == UnitInch)
1842 dx = dy = (REAL) INCH_HIMETRIC;
1843 else if(srcUnit == UnitPixel){
1844 dx = ((REAL) INCH_HIMETRIC) /
1845 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1846 dy = ((REAL) INCH_HIMETRIC) /
1847 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1849 else
1850 return NotImplemented;
1852 memcpy(ptf, points, 3 * sizeof(GpPointF));
1853 transform_and_round_points(graphics, pti, ptf, 3);
1855 /* IPicture renders bitmaps with the y-axis reversed
1856 * FIXME: flipping for unknown image type might not be correct. */
1857 if(image->type != ImageTypeMetafile){
1858 INT temp;
1859 temp = pti[0].y;
1860 pti[0].y = pti[2].y;
1861 pti[2].y = temp;
1864 if(IPicture_Render(image->picture, graphics->hdc,
1865 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1866 srcx * dx, srcy * dy,
1867 srcwidth * dx, srcheight * dy,
1868 NULL) != S_OK){
1869 if(callback)
1870 callback(callbackData);
1871 return GenericError;
1874 return Ok;
1877 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1878 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1879 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1880 DrawImageAbort callback, VOID * callbackData)
1882 GpPointF pointsF[3];
1883 INT i;
1885 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
1886 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1887 callbackData);
1889 if(!points || count!=3)
1890 return InvalidParameter;
1892 for(i = 0; i < count; i++){
1893 pointsF[i].X = (REAL)points[i].X;
1894 pointsF[i].Y = (REAL)points[i].Y;
1897 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1898 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1899 callback, callbackData);
1902 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1903 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1904 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1905 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1906 VOID * callbackData)
1908 GpPointF points[3];
1910 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
1911 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1912 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1914 points[0].X = dstx;
1915 points[0].Y = dsty;
1916 points[1].X = dstx + dstwidth;
1917 points[1].Y = dsty;
1918 points[2].X = dstx;
1919 points[2].Y = dsty + dstheight;
1921 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1922 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1925 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1926 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1927 INT srcwidth, INT srcheight, GpUnit srcUnit,
1928 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1929 VOID * callbackData)
1931 GpPointF points[3];
1933 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
1934 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1935 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1937 points[0].X = dstx;
1938 points[0].Y = dsty;
1939 points[1].X = dstx + dstwidth;
1940 points[1].Y = dsty;
1941 points[2].X = dstx;
1942 points[2].Y = dsty + dstheight;
1944 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1945 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1948 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1949 REAL x, REAL y, REAL width, REAL height)
1951 RectF bounds;
1952 GpUnit unit;
1953 GpStatus ret;
1955 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
1957 if(!graphics || !image)
1958 return InvalidParameter;
1960 ret = GdipGetImageBounds(image, &bounds, &unit);
1961 if(ret != Ok)
1962 return ret;
1964 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1965 bounds.X, bounds.Y, bounds.Width, bounds.Height,
1966 unit, NULL, NULL, NULL);
1969 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1970 INT x, INT y, INT width, INT height)
1972 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
1974 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1977 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1978 REAL y1, REAL x2, REAL y2)
1980 INT save_state;
1981 GpPointF pt[2];
1982 GpStatus retval;
1984 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
1986 if(!pen || !graphics)
1987 return InvalidParameter;
1989 if(graphics->busy)
1990 return ObjectBusy;
1992 pt[0].X = x1;
1993 pt[0].Y = y1;
1994 pt[1].X = x2;
1995 pt[1].Y = y2;
1997 save_state = prepare_dc(graphics, pen);
1999 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2001 restore_dc(graphics, save_state);
2003 return retval;
2006 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2007 INT y1, INT x2, INT y2)
2009 INT save_state;
2010 GpPointF pt[2];
2011 GpStatus retval;
2013 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2015 if(!pen || !graphics)
2016 return InvalidParameter;
2018 if(graphics->busy)
2019 return ObjectBusy;
2021 pt[0].X = (REAL)x1;
2022 pt[0].Y = (REAL)y1;
2023 pt[1].X = (REAL)x2;
2024 pt[1].Y = (REAL)y2;
2026 save_state = prepare_dc(graphics, pen);
2028 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2030 restore_dc(graphics, save_state);
2032 return retval;
2035 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2036 GpPointF *points, INT count)
2038 INT save_state;
2039 GpStatus retval;
2041 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2043 if(!pen || !graphics || (count < 2))
2044 return InvalidParameter;
2046 if(graphics->busy)
2047 return ObjectBusy;
2049 save_state = prepare_dc(graphics, pen);
2051 retval = draw_polyline(graphics, pen, points, count, TRUE);
2053 restore_dc(graphics, save_state);
2055 return retval;
2058 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2059 GpPoint *points, INT count)
2061 INT save_state;
2062 GpStatus retval;
2063 GpPointF *ptf = NULL;
2064 int i;
2066 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2068 if(!pen || !graphics || (count < 2))
2069 return InvalidParameter;
2071 if(graphics->busy)
2072 return ObjectBusy;
2074 ptf = GdipAlloc(count * sizeof(GpPointF));
2075 if(!ptf) return OutOfMemory;
2077 for(i = 0; i < count; i ++){
2078 ptf[i].X = (REAL) points[i].X;
2079 ptf[i].Y = (REAL) points[i].Y;
2082 save_state = prepare_dc(graphics, pen);
2084 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2086 restore_dc(graphics, save_state);
2088 GdipFree(ptf);
2089 return retval;
2092 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2094 INT save_state;
2095 GpStatus retval;
2097 TRACE("(%p, %p, %p)\n", graphics, pen, path);
2099 if(!pen || !graphics)
2100 return InvalidParameter;
2102 if(graphics->busy)
2103 return ObjectBusy;
2105 save_state = prepare_dc(graphics, pen);
2107 retval = draw_poly(graphics, pen, path->pathdata.Points,
2108 path->pathdata.Types, path->pathdata.Count, TRUE);
2110 restore_dc(graphics, save_state);
2112 return retval;
2115 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2116 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2118 INT save_state;
2120 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2121 width, height, startAngle, sweepAngle);
2123 if(!graphics || !pen)
2124 return InvalidParameter;
2126 if(graphics->busy)
2127 return ObjectBusy;
2129 save_state = prepare_dc(graphics, pen);
2130 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2132 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2134 restore_dc(graphics, save_state);
2136 return Ok;
2139 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2140 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2142 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2143 width, height, startAngle, sweepAngle);
2145 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2148 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2149 REAL y, REAL width, REAL height)
2151 INT save_state;
2152 GpPointF ptf[4];
2153 POINT pti[4];
2155 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2157 if(!pen || !graphics)
2158 return InvalidParameter;
2160 if(graphics->busy)
2161 return ObjectBusy;
2163 ptf[0].X = x;
2164 ptf[0].Y = y;
2165 ptf[1].X = x + width;
2166 ptf[1].Y = y;
2167 ptf[2].X = x + width;
2168 ptf[2].Y = y + height;
2169 ptf[3].X = x;
2170 ptf[3].Y = y + height;
2172 save_state = prepare_dc(graphics, pen);
2173 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2175 transform_and_round_points(graphics, pti, ptf, 4);
2176 Polygon(graphics->hdc, pti, 4);
2178 restore_dc(graphics, save_state);
2180 return Ok;
2183 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2184 INT y, INT width, INT height)
2186 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2188 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2191 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2192 GDIPCONST GpRectF* rects, INT count)
2194 GpPointF *ptf;
2195 POINT *pti;
2196 INT save_state, i;
2198 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2200 if(!graphics || !pen || !rects || count < 1)
2201 return InvalidParameter;
2203 if(graphics->busy)
2204 return ObjectBusy;
2206 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2207 pti = GdipAlloc(4 * count * sizeof(POINT));
2209 if(!ptf || !pti){
2210 GdipFree(ptf);
2211 GdipFree(pti);
2212 return OutOfMemory;
2215 for(i = 0; i < count; i++){
2216 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2217 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2218 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2219 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2222 save_state = prepare_dc(graphics, pen);
2223 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2225 transform_and_round_points(graphics, pti, ptf, 4 * count);
2227 for(i = 0; i < count; i++)
2228 Polygon(graphics->hdc, &pti[4 * i], 4);
2230 restore_dc(graphics, save_state);
2232 GdipFree(ptf);
2233 GdipFree(pti);
2235 return Ok;
2238 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2239 GDIPCONST GpRect* rects, INT count)
2241 GpRectF *rectsF;
2242 GpStatus ret;
2243 INT i;
2245 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2247 if(!rects || count<=0)
2248 return InvalidParameter;
2250 rectsF = GdipAlloc(sizeof(GpRectF) * count);
2251 if(!rectsF)
2252 return OutOfMemory;
2254 for(i = 0;i < count;i++){
2255 rectsF[i].X = (REAL)rects[i].X;
2256 rectsF[i].Y = (REAL)rects[i].Y;
2257 rectsF[i].Width = (REAL)rects[i].Width;
2258 rectsF[i].Height = (REAL)rects[i].Height;
2261 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2262 GdipFree(rectsF);
2264 return ret;
2267 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
2268 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
2269 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
2271 HRGN rgn = NULL;
2272 HFONT gdifont;
2273 LOGFONTW lfw;
2274 TEXTMETRICW textmet;
2275 GpPointF pt[2], rectcpy[4];
2276 POINT corners[4];
2277 WCHAR* stringdup;
2278 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
2279 INT sum = 0, height = 0, offsety = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
2280 nheight, lineend;
2281 SIZE size;
2282 POINT drawbase;
2283 UINT drawflags;
2284 RECT drawcoord;
2286 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
2287 length, font, debugstr_rectf(rect), format, brush);
2289 if(!graphics || !string || !font || !brush || !rect)
2290 return InvalidParameter;
2292 if((brush->bt != BrushTypeSolidColor)){
2293 FIXME("not implemented for given parameters\n");
2294 return NotImplemented;
2297 if(format){
2298 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2300 /* Should be no need to explicitly test for StringAlignmentNear as
2301 * that is default behavior if no alignment is passed. */
2302 if(format->vertalign != StringAlignmentNear){
2303 RectF bounds;
2304 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
2306 if(format->vertalign == StringAlignmentCenter)
2307 offsety = (rect->Height - bounds.Height) / 2;
2308 else if(format->vertalign == StringAlignmentFar)
2309 offsety = (rect->Height - bounds.Height);
2313 if(length == -1) length = lstrlenW(string);
2315 stringdup = GdipAlloc(length * sizeof(WCHAR));
2316 if(!stringdup) return OutOfMemory;
2318 save_state = SaveDC(graphics->hdc);
2319 SetBkMode(graphics->hdc, TRANSPARENT);
2320 SetTextColor(graphics->hdc, brush->lb.lbColor);
2322 rectcpy[3].X = rectcpy[0].X = rect->X;
2323 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
2324 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
2325 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
2326 transform_and_round_points(graphics, corners, rectcpy, 4);
2328 if (roundr(rect->Width) == 0)
2330 rel_width = 1.0;
2331 nwidth = INT_MAX;
2333 else
2335 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
2336 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
2337 / rect->Width;
2338 nwidth = roundr(rel_width * rect->Width);
2341 if (roundr(rect->Height) == 0)
2343 rel_height = 1.0;
2344 nheight = INT_MAX;
2346 else
2348 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
2349 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
2350 / rect->Height;
2351 nheight = roundr(rel_height * rect->Height);
2354 if (roundr(rect->Width) != 0 && roundr(rect->Height) != 0)
2356 /* FIXME: If only the width or only the height is 0, we should probably still clip */
2357 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
2358 SelectClipRgn(graphics->hdc, rgn);
2361 /* Use gdi to find the font, then perform transformations on it (height,
2362 * width, angle). */
2363 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2364 GetTextMetricsW(graphics->hdc, &textmet);
2365 lfw = font->lfw;
2367 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
2368 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
2370 pt[0].X = 0.0;
2371 pt[0].Y = 0.0;
2372 pt[1].X = 1.0;
2373 pt[1].Y = 0.0;
2374 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
2375 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2376 ang_cos = cos(angle);
2377 ang_sin = sin(angle);
2378 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2380 gdifont = CreateFontIndirectW(&lfw);
2381 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
2383 for(i = 0, j = 0; i < length; i++){
2384 if(!isprintW(string[i]) && (string[i] != '\n'))
2385 continue;
2387 stringdup[j] = string[i];
2388 j++;
2391 length = j;
2393 if (!format || format->align == StringAlignmentNear)
2395 drawbase.x = corners[0].x;
2396 drawbase.y = corners[0].y;
2397 drawflags = DT_NOCLIP | DT_EXPANDTABS;
2399 else if (format->align == StringAlignmentCenter)
2401 drawbase.x = (corners[0].x + corners[1].x)/2;
2402 drawbase.y = (corners[0].y + corners[1].y)/2;
2403 drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
2405 else /* (format->align == StringAlignmentFar) */
2407 drawbase.x = corners[1].x;
2408 drawbase.y = corners[1].y;
2409 drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
2412 while(sum < length){
2413 drawcoord.left = drawcoord.right = drawbase.x + roundr(ang_sin * (REAL) height);
2414 drawcoord.top = drawcoord.bottom = drawbase.y + roundr(ang_cos * (REAL) height);
2416 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2417 nwidth, &fit, NULL, &size);
2418 fitcpy = fit;
2420 if(fit == 0){
2421 DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, drawflags);
2422 break;
2425 for(lret = 0; lret < fit; lret++)
2426 if(*(stringdup + sum + lret) == '\n')
2427 break;
2429 /* Line break code (may look strange, but it imitates windows). */
2430 if(lret < fit)
2431 lineend = fit = lret; /* this is not an off-by-one error */
2432 else if(fit < (length - sum)){
2433 if(*(stringdup + sum + fit) == ' ')
2434 while(*(stringdup + sum + fit) == ' ')
2435 fit++;
2436 else
2437 while(*(stringdup + sum + fit - 1) != ' '){
2438 fit--;
2440 if(*(stringdup + sum + fit) == '\t')
2441 break;
2443 if(fit == 0){
2444 fit = fitcpy;
2445 break;
2448 lineend = fit;
2449 while(*(stringdup + sum + lineend - 1) == ' ' ||
2450 *(stringdup + sum + lineend - 1) == '\t')
2451 lineend--;
2453 else
2454 lineend = fit;
2455 DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, lineend),
2456 &drawcoord, drawflags);
2458 sum += fit + (lret < fitcpy ? 1 : 0);
2459 height += size.cy;
2461 if(height > nheight)
2462 break;
2464 /* Stop if this was a linewrap (but not if it was a linebreak). */
2465 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2466 break;
2469 GdipFree(stringdup);
2470 DeleteObject(rgn);
2471 DeleteObject(gdifont);
2473 RestoreDC(graphics->hdc, save_state);
2475 return Ok;
2478 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2479 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2481 GpPath *path;
2482 GpStatus stat;
2484 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2485 count, tension, fill);
2487 if(!graphics || !brush || !points)
2488 return InvalidParameter;
2490 if(graphics->busy)
2491 return ObjectBusy;
2493 stat = GdipCreatePath(fill, &path);
2494 if(stat != Ok)
2495 return stat;
2497 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2498 if(stat != Ok){
2499 GdipDeletePath(path);
2500 return stat;
2503 stat = GdipFillPath(graphics, brush, path);
2504 if(stat != Ok){
2505 GdipDeletePath(path);
2506 return stat;
2509 GdipDeletePath(path);
2511 return Ok;
2514 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2515 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2517 GpPointF *ptf;
2518 GpStatus stat;
2519 INT i;
2521 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2522 count, tension, fill);
2524 if(!points || count <= 0)
2525 return InvalidParameter;
2527 ptf = GdipAlloc(sizeof(GpPointF)*count);
2528 if(!ptf)
2529 return OutOfMemory;
2531 for(i = 0;i < count;i++){
2532 ptf[i].X = (REAL)points[i].X;
2533 ptf[i].Y = (REAL)points[i].Y;
2536 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2538 GdipFree(ptf);
2540 return stat;
2543 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2544 REAL y, REAL width, REAL height)
2546 INT save_state;
2547 GpPointF ptf[2];
2548 POINT pti[2];
2550 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2552 if(!graphics || !brush)
2553 return InvalidParameter;
2555 if(graphics->busy)
2556 return ObjectBusy;
2558 ptf[0].X = x;
2559 ptf[0].Y = y;
2560 ptf[1].X = x + width;
2561 ptf[1].Y = y + height;
2563 save_state = SaveDC(graphics->hdc);
2564 EndPath(graphics->hdc);
2566 transform_and_round_points(graphics, pti, ptf, 2);
2568 BeginPath(graphics->hdc);
2569 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2570 EndPath(graphics->hdc);
2572 brush_fill_path(graphics, brush);
2574 RestoreDC(graphics->hdc, save_state);
2576 return Ok;
2579 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2580 INT y, INT width, INT height)
2582 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2584 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2587 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2589 INT save_state;
2590 GpStatus retval;
2592 TRACE("(%p, %p, %p)\n", graphics, brush, path);
2594 if(!brush || !graphics || !path)
2595 return InvalidParameter;
2597 if(graphics->busy)
2598 return ObjectBusy;
2600 save_state = SaveDC(graphics->hdc);
2601 EndPath(graphics->hdc);
2602 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2603 : WINDING));
2605 BeginPath(graphics->hdc);
2606 retval = draw_poly(graphics, NULL, path->pathdata.Points,
2607 path->pathdata.Types, path->pathdata.Count, FALSE);
2609 if(retval != Ok)
2610 goto end;
2612 EndPath(graphics->hdc);
2613 brush_fill_path(graphics, brush);
2615 retval = Ok;
2617 end:
2618 RestoreDC(graphics->hdc, save_state);
2620 return retval;
2623 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2624 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2626 INT save_state;
2628 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2629 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2631 if(!graphics || !brush)
2632 return InvalidParameter;
2634 if(graphics->busy)
2635 return ObjectBusy;
2637 save_state = SaveDC(graphics->hdc);
2638 EndPath(graphics->hdc);
2640 BeginPath(graphics->hdc);
2641 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2642 EndPath(graphics->hdc);
2644 brush_fill_path(graphics, brush);
2646 RestoreDC(graphics->hdc, save_state);
2648 return Ok;
2651 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2652 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2654 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2655 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2657 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2660 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2661 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2663 INT save_state;
2664 GpPointF *ptf = NULL;
2665 POINT *pti = NULL;
2666 GpStatus retval = Ok;
2668 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2670 if(!graphics || !brush || !points || !count)
2671 return InvalidParameter;
2673 if(graphics->busy)
2674 return ObjectBusy;
2676 ptf = GdipAlloc(count * sizeof(GpPointF));
2677 pti = GdipAlloc(count * sizeof(POINT));
2678 if(!ptf || !pti){
2679 retval = OutOfMemory;
2680 goto end;
2683 memcpy(ptf, points, count * sizeof(GpPointF));
2685 save_state = SaveDC(graphics->hdc);
2686 EndPath(graphics->hdc);
2687 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2688 : WINDING));
2690 transform_and_round_points(graphics, pti, ptf, count);
2692 BeginPath(graphics->hdc);
2693 Polygon(graphics->hdc, pti, count);
2694 EndPath(graphics->hdc);
2696 brush_fill_path(graphics, brush);
2698 RestoreDC(graphics->hdc, save_state);
2700 end:
2701 GdipFree(ptf);
2702 GdipFree(pti);
2704 return retval;
2707 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2708 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2710 INT save_state, i;
2711 GpPointF *ptf = NULL;
2712 POINT *pti = NULL;
2713 GpStatus retval = Ok;
2715 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2717 if(!graphics || !brush || !points || !count)
2718 return InvalidParameter;
2720 if(graphics->busy)
2721 return ObjectBusy;
2723 ptf = GdipAlloc(count * sizeof(GpPointF));
2724 pti = GdipAlloc(count * sizeof(POINT));
2725 if(!ptf || !pti){
2726 retval = OutOfMemory;
2727 goto end;
2730 for(i = 0; i < count; i ++){
2731 ptf[i].X = (REAL) points[i].X;
2732 ptf[i].Y = (REAL) points[i].Y;
2735 save_state = SaveDC(graphics->hdc);
2736 EndPath(graphics->hdc);
2737 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2738 : WINDING));
2740 transform_and_round_points(graphics, pti, ptf, count);
2742 BeginPath(graphics->hdc);
2743 Polygon(graphics->hdc, pti, count);
2744 EndPath(graphics->hdc);
2746 brush_fill_path(graphics, brush);
2748 RestoreDC(graphics->hdc, save_state);
2750 end:
2751 GdipFree(ptf);
2752 GdipFree(pti);
2754 return retval;
2757 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2758 GDIPCONST GpPointF *points, INT count)
2760 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2762 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2765 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2766 GDIPCONST GpPoint *points, INT count)
2768 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2770 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2773 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2774 REAL x, REAL y, REAL width, REAL height)
2776 INT save_state;
2777 GpPointF ptf[4];
2778 POINT pti[4];
2780 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2782 if(!graphics || !brush)
2783 return InvalidParameter;
2785 if(graphics->busy)
2786 return ObjectBusy;
2788 ptf[0].X = x;
2789 ptf[0].Y = y;
2790 ptf[1].X = x + width;
2791 ptf[1].Y = y;
2792 ptf[2].X = x + width;
2793 ptf[2].Y = y + height;
2794 ptf[3].X = x;
2795 ptf[3].Y = y + height;
2797 save_state = SaveDC(graphics->hdc);
2798 EndPath(graphics->hdc);
2800 transform_and_round_points(graphics, pti, ptf, 4);
2802 BeginPath(graphics->hdc);
2803 Polygon(graphics->hdc, pti, 4);
2804 EndPath(graphics->hdc);
2806 brush_fill_path(graphics, brush);
2808 RestoreDC(graphics->hdc, save_state);
2810 return Ok;
2813 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2814 INT x, INT y, INT width, INT height)
2816 INT save_state;
2817 GpPointF ptf[4];
2818 POINT pti[4];
2820 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2822 if(!graphics || !brush)
2823 return InvalidParameter;
2825 if(graphics->busy)
2826 return ObjectBusy;
2828 ptf[0].X = x;
2829 ptf[0].Y = y;
2830 ptf[1].X = x + width;
2831 ptf[1].Y = y;
2832 ptf[2].X = x + width;
2833 ptf[2].Y = y + height;
2834 ptf[3].X = x;
2835 ptf[3].Y = y + height;
2837 save_state = SaveDC(graphics->hdc);
2838 EndPath(graphics->hdc);
2840 transform_and_round_points(graphics, pti, ptf, 4);
2842 BeginPath(graphics->hdc);
2843 Polygon(graphics->hdc, pti, 4);
2844 EndPath(graphics->hdc);
2846 brush_fill_path(graphics, brush);
2848 RestoreDC(graphics->hdc, save_state);
2850 return Ok;
2853 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2854 INT count)
2856 GpStatus ret;
2857 INT i;
2859 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2861 if(!rects)
2862 return InvalidParameter;
2864 for(i = 0; i < count; i++){
2865 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2866 if(ret != Ok) return ret;
2869 return Ok;
2872 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2873 INT count)
2875 GpRectF *rectsF;
2876 GpStatus ret;
2877 INT i;
2879 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2881 if(!rects || count <= 0)
2882 return InvalidParameter;
2884 rectsF = GdipAlloc(sizeof(GpRectF)*count);
2885 if(!rectsF)
2886 return OutOfMemory;
2888 for(i = 0; i < count; i++){
2889 rectsF[i].X = (REAL)rects[i].X;
2890 rectsF[i].Y = (REAL)rects[i].Y;
2891 rectsF[i].X = (REAL)rects[i].Width;
2892 rectsF[i].Height = (REAL)rects[i].Height;
2895 ret = GdipFillRectangles(graphics,brush,rectsF,count);
2896 GdipFree(rectsF);
2898 return ret;
2901 /*****************************************************************************
2902 * GdipFillRegion [GDIPLUS.@]
2904 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2905 GpRegion* region)
2907 INT save_state;
2908 GpStatus status;
2909 HRGN hrgn;
2910 RECT rc;
2912 TRACE("(%p, %p, %p)\n", graphics, brush, region);
2914 if (!(graphics && brush && region))
2915 return InvalidParameter;
2917 if(graphics->busy)
2918 return ObjectBusy;
2920 status = GdipGetRegionHRgn(region, graphics, &hrgn);
2921 if(status != Ok)
2922 return status;
2924 save_state = SaveDC(graphics->hdc);
2925 EndPath(graphics->hdc);
2927 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
2929 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
2931 BeginPath(graphics->hdc);
2932 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
2933 EndPath(graphics->hdc);
2935 brush_fill_path(graphics, brush);
2938 RestoreDC(graphics->hdc, save_state);
2940 DeleteObject(hrgn);
2942 return Ok;
2945 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2947 static int calls;
2949 if(!graphics)
2950 return InvalidParameter;
2952 if(graphics->busy)
2953 return ObjectBusy;
2955 if(!(calls++))
2956 FIXME("not implemented\n");
2958 return NotImplemented;
2961 /*****************************************************************************
2962 * GdipGetClipBounds [GDIPLUS.@]
2964 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
2966 TRACE("(%p, %p)\n", graphics, rect);
2968 if(!graphics)
2969 return InvalidParameter;
2971 if(graphics->busy)
2972 return ObjectBusy;
2974 return GdipGetRegionBounds(graphics->clip, graphics, rect);
2977 /*****************************************************************************
2978 * GdipGetClipBoundsI [GDIPLUS.@]
2980 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
2982 TRACE("(%p, %p)\n", graphics, rect);
2984 if(!graphics)
2985 return InvalidParameter;
2987 if(graphics->busy)
2988 return ObjectBusy;
2990 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
2993 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2994 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2995 CompositingMode *mode)
2997 TRACE("(%p, %p)\n", graphics, mode);
2999 if(!graphics || !mode)
3000 return InvalidParameter;
3002 if(graphics->busy)
3003 return ObjectBusy;
3005 *mode = graphics->compmode;
3007 return Ok;
3010 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3011 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3012 CompositingQuality *quality)
3014 TRACE("(%p, %p)\n", graphics, quality);
3016 if(!graphics || !quality)
3017 return InvalidParameter;
3019 if(graphics->busy)
3020 return ObjectBusy;
3022 *quality = graphics->compqual;
3024 return Ok;
3027 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3028 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3029 InterpolationMode *mode)
3031 TRACE("(%p, %p)\n", graphics, mode);
3033 if(!graphics || !mode)
3034 return InvalidParameter;
3036 if(graphics->busy)
3037 return ObjectBusy;
3039 *mode = graphics->interpolation;
3041 return Ok;
3044 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3046 if(!graphics || !argb)
3047 return InvalidParameter;
3049 if(graphics->busy)
3050 return ObjectBusy;
3052 FIXME("(%p, %p): stub\n", graphics, argb);
3054 return NotImplemented;
3057 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3059 TRACE("(%p, %p)\n", graphics, scale);
3061 if(!graphics || !scale)
3062 return InvalidParameter;
3064 if(graphics->busy)
3065 return ObjectBusy;
3067 *scale = graphics->scale;
3069 return Ok;
3072 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3074 TRACE("(%p, %p)\n", graphics, unit);
3076 if(!graphics || !unit)
3077 return InvalidParameter;
3079 if(graphics->busy)
3080 return ObjectBusy;
3082 *unit = graphics->unit;
3084 return Ok;
3087 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3088 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3089 *mode)
3091 TRACE("(%p, %p)\n", graphics, mode);
3093 if(!graphics || !mode)
3094 return InvalidParameter;
3096 if(graphics->busy)
3097 return ObjectBusy;
3099 *mode = graphics->pixeloffset;
3101 return Ok;
3104 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3105 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3107 TRACE("(%p, %p)\n", graphics, mode);
3109 if(!graphics || !mode)
3110 return InvalidParameter;
3112 if(graphics->busy)
3113 return ObjectBusy;
3115 *mode = graphics->smoothing;
3117 return Ok;
3120 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3122 TRACE("(%p, %p)\n", graphics, contrast);
3124 if(!graphics || !contrast)
3125 return InvalidParameter;
3127 *contrast = graphics->textcontrast;
3129 return Ok;
3132 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3133 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3134 TextRenderingHint *hint)
3136 TRACE("(%p, %p)\n", graphics, hint);
3138 if(!graphics || !hint)
3139 return InvalidParameter;
3141 if(graphics->busy)
3142 return ObjectBusy;
3144 *hint = graphics->texthint;
3146 return Ok;
3149 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3151 GpRegion *clip_rgn;
3152 GpStatus stat;
3154 TRACE("(%p, %p)\n", graphics, rect);
3156 if(!graphics || !rect)
3157 return InvalidParameter;
3159 if(graphics->busy)
3160 return ObjectBusy;
3162 /* intersect window and graphics clipping regions */
3163 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3164 return stat;
3166 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3167 goto cleanup;
3169 /* get bounds of the region */
3170 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3172 cleanup:
3173 GdipDeleteRegion(clip_rgn);
3175 return stat;
3178 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3180 GpRectF rectf;
3181 GpStatus stat;
3183 TRACE("(%p, %p)\n", graphics, rect);
3185 if(!graphics || !rect)
3186 return InvalidParameter;
3188 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
3190 rect->X = roundr(rectf.X);
3191 rect->Y = roundr(rectf.Y);
3192 rect->Width = roundr(rectf.Width);
3193 rect->Height = roundr(rectf.Height);
3196 return stat;
3199 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3201 TRACE("(%p, %p)\n", graphics, matrix);
3203 if(!graphics || !matrix)
3204 return InvalidParameter;
3206 if(graphics->busy)
3207 return ObjectBusy;
3209 *matrix = *graphics->worldtrans;
3210 return Ok;
3213 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3215 GpSolidFill *brush;
3216 GpStatus stat;
3217 GpRectF wnd_rect;
3219 TRACE("(%p, %x)\n", graphics, color);
3221 if(!graphics)
3222 return InvalidParameter;
3224 if(graphics->busy)
3225 return ObjectBusy;
3227 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3228 return stat;
3230 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
3231 GdipDeleteBrush((GpBrush*)brush);
3232 return stat;
3235 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
3236 wnd_rect.Width, wnd_rect.Height);
3238 GdipDeleteBrush((GpBrush*)brush);
3240 return Ok;
3243 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3245 TRACE("(%p, %p)\n", graphics, res);
3247 if(!graphics || !res)
3248 return InvalidParameter;
3250 return GdipIsEmptyRegion(graphics->clip, graphics, res);
3253 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3255 FIXME("(%p, %.2f, %.2f, %p) stub\n", graphics, x, y, result);
3257 if(!graphics || !result)
3258 return InvalidParameter;
3260 if(graphics->busy)
3261 return ObjectBusy;
3263 return NotImplemented;
3266 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3268 FIXME("(%p, %d, %d, %p) stub\n", graphics, x, y, result);
3270 if(!graphics || !result)
3271 return InvalidParameter;
3273 if(graphics->busy)
3274 return ObjectBusy;
3276 return NotImplemented;
3279 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3280 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3281 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3282 INT regionCount, GpRegion** regions)
3284 if (!(graphics && string && font && layoutRect && stringFormat && regions))
3285 return InvalidParameter;
3287 FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
3288 length, font, layoutRect, stringFormat, regionCount, regions);
3290 return NotImplemented;
3293 /* Find the smallest rectangle that bounds the text when it is printed in rect
3294 * according to the format options listed in format. If rect has 0 width and
3295 * height, then just find the smallest rectangle that bounds the text when it's
3296 * printed at location (rect->X, rect-Y). */
3297 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3298 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3299 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3300 INT *codepointsfitted, INT *linesfilled)
3302 HFONT oldfont;
3303 WCHAR* stringdup;
3304 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
3305 nheight, lineend;
3306 SIZE size;
3308 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3309 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3310 bounds, codepointsfitted, linesfilled);
3312 if(!graphics || !string || !font || !rect)
3313 return InvalidParameter;
3315 if(linesfilled) *linesfilled = 0;
3316 if(codepointsfitted) *codepointsfitted = 0;
3318 if(format)
3319 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3321 if(length == -1) length = lstrlenW(string);
3323 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3324 if(!stringdup) return OutOfMemory;
3326 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3327 nwidth = roundr(rect->Width);
3328 nheight = roundr(rect->Height);
3330 if((nwidth == 0) && (nheight == 0))
3331 nwidth = nheight = INT_MAX;
3333 for(i = 0, j = 0; i < length; i++){
3334 if(!isprintW(string[i]) && (string[i] != '\n'))
3335 continue;
3337 stringdup[j] = string[i];
3338 j++;
3341 stringdup[j] = 0;
3342 length = j;
3344 while(sum < length){
3345 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3346 nwidth, &fit, NULL, &size);
3347 fitcpy = fit;
3349 if(fit == 0)
3350 break;
3352 for(lret = 0; lret < fit; lret++)
3353 if(*(stringdup + sum + lret) == '\n')
3354 break;
3356 /* Line break code (may look strange, but it imitates windows). */
3357 if(lret < fit)
3358 lineend = fit = lret; /* this is not an off-by-one error */
3359 else if(fit < (length - sum)){
3360 if(*(stringdup + sum + fit) == ' ')
3361 while(*(stringdup + sum + fit) == ' ')
3362 fit++;
3363 else
3364 while(*(stringdup + sum + fit - 1) != ' '){
3365 fit--;
3367 if(*(stringdup + sum + fit) == '\t')
3368 break;
3370 if(fit == 0){
3371 fit = fitcpy;
3372 break;
3375 lineend = fit;
3376 while(*(stringdup + sum + lineend - 1) == ' ' ||
3377 *(stringdup + sum + lineend - 1) == '\t')
3378 lineend--;
3380 else
3381 lineend = fit;
3383 GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3384 nwidth, &j, NULL, &size);
3386 sum += fit + (lret < fitcpy ? 1 : 0);
3387 if(codepointsfitted) *codepointsfitted = sum;
3389 height += size.cy;
3390 if(linesfilled) *linesfilled += size.cy;
3391 max_width = max(max_width, size.cx);
3393 if(height > nheight)
3394 break;
3396 /* Stop if this was a linewrap (but not if it was a linebreak). */
3397 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3398 break;
3401 bounds->X = rect->X;
3402 bounds->Y = rect->Y;
3403 bounds->Width = (REAL)max_width;
3404 bounds->Height = (REAL) min(height, nheight);
3406 GdipFree(stringdup);
3407 DeleteObject(SelectObject(graphics->hdc, oldfont));
3409 return Ok;
3412 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
3414 TRACE("(%p)\n", graphics);
3416 if(!graphics)
3417 return InvalidParameter;
3419 if(graphics->busy)
3420 return ObjectBusy;
3422 return GdipSetInfinite(graphics->clip);
3425 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
3427 TRACE("(%p)\n", graphics);
3429 if(!graphics)
3430 return InvalidParameter;
3432 if(graphics->busy)
3433 return ObjectBusy;
3435 graphics->worldtrans->matrix[0] = 1.0;
3436 graphics->worldtrans->matrix[1] = 0.0;
3437 graphics->worldtrans->matrix[2] = 0.0;
3438 graphics->worldtrans->matrix[3] = 1.0;
3439 graphics->worldtrans->matrix[4] = 0.0;
3440 graphics->worldtrans->matrix[5] = 0.0;
3442 return Ok;
3445 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
3447 return GdipEndContainer(graphics, state);
3450 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
3451 GpMatrixOrder order)
3453 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
3455 if(!graphics)
3456 return InvalidParameter;
3458 if(graphics->busy)
3459 return ObjectBusy;
3461 return GdipRotateMatrix(graphics->worldtrans, angle, order);
3464 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
3466 return GdipBeginContainer2(graphics, state);
3469 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
3470 GraphicsContainer *state)
3472 GraphicsContainerItem *container;
3473 GpStatus sts;
3475 TRACE("(%p, %p)\n", graphics, state);
3477 if(!graphics || !state)
3478 return InvalidParameter;
3480 sts = init_container(&container, graphics);
3481 if(sts != Ok)
3482 return sts;
3484 list_add_head(&graphics->containers, &container->entry);
3485 *state = graphics->contid = container->contid;
3487 return Ok;
3490 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
3492 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3493 return NotImplemented;
3496 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
3498 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3499 return NotImplemented;
3502 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
3504 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
3505 return NotImplemented;
3508 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
3510 GpStatus sts;
3511 GraphicsContainerItem *container, *container2;
3513 TRACE("(%p, %x)\n", graphics, state);
3515 if(!graphics)
3516 return InvalidParameter;
3518 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
3519 if(container->contid == state)
3520 break;
3523 /* did not find a matching container */
3524 if(&container->entry == &graphics->containers)
3525 return Ok;
3527 sts = restore_container(graphics, container);
3528 if(sts != Ok)
3529 return sts;
3531 /* remove all of the containers on top of the found container */
3532 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
3533 if(container->contid == state)
3534 break;
3535 list_remove(&container->entry);
3536 delete_container(container);
3539 list_remove(&container->entry);
3540 delete_container(container);
3542 return Ok;
3545 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
3546 REAL sy, GpMatrixOrder order)
3548 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
3550 if(!graphics)
3551 return InvalidParameter;
3553 if(graphics->busy)
3554 return ObjectBusy;
3556 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
3559 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
3560 CombineMode mode)
3562 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
3564 if(!graphics || !srcgraphics)
3565 return InvalidParameter;
3567 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
3570 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
3571 CompositingMode mode)
3573 TRACE("(%p, %d)\n", graphics, mode);
3575 if(!graphics)
3576 return InvalidParameter;
3578 if(graphics->busy)
3579 return ObjectBusy;
3581 graphics->compmode = mode;
3583 return Ok;
3586 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
3587 CompositingQuality quality)
3589 TRACE("(%p, %d)\n", graphics, quality);
3591 if(!graphics)
3592 return InvalidParameter;
3594 if(graphics->busy)
3595 return ObjectBusy;
3597 graphics->compqual = quality;
3599 return Ok;
3602 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
3603 InterpolationMode mode)
3605 TRACE("(%p, %d)\n", graphics, mode);
3607 if(!graphics)
3608 return InvalidParameter;
3610 if(graphics->busy)
3611 return ObjectBusy;
3613 graphics->interpolation = mode;
3615 return Ok;
3618 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
3620 TRACE("(%p, %.2f)\n", graphics, scale);
3622 if(!graphics || (scale <= 0.0))
3623 return InvalidParameter;
3625 if(graphics->busy)
3626 return ObjectBusy;
3628 graphics->scale = scale;
3630 return Ok;
3633 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
3635 TRACE("(%p, %d)\n", graphics, unit);
3637 if(!graphics)
3638 return InvalidParameter;
3640 if(graphics->busy)
3641 return ObjectBusy;
3643 if(unit == UnitWorld)
3644 return InvalidParameter;
3646 graphics->unit = unit;
3648 return Ok;
3651 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3652 mode)
3654 TRACE("(%p, %d)\n", graphics, mode);
3656 if(!graphics)
3657 return InvalidParameter;
3659 if(graphics->busy)
3660 return ObjectBusy;
3662 graphics->pixeloffset = mode;
3664 return Ok;
3667 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
3669 static int calls;
3671 TRACE("(%p,%i,%i)\n", graphics, x, y);
3673 if (!(calls++))
3674 FIXME("not implemented\n");
3676 return NotImplemented;
3679 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
3681 TRACE("(%p, %d)\n", graphics, mode);
3683 if(!graphics)
3684 return InvalidParameter;
3686 if(graphics->busy)
3687 return ObjectBusy;
3689 graphics->smoothing = mode;
3691 return Ok;
3694 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
3696 TRACE("(%p, %d)\n", graphics, contrast);
3698 if(!graphics)
3699 return InvalidParameter;
3701 graphics->textcontrast = contrast;
3703 return Ok;
3706 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
3707 TextRenderingHint hint)
3709 TRACE("(%p, %d)\n", graphics, hint);
3711 if(!graphics)
3712 return InvalidParameter;
3714 if(graphics->busy)
3715 return ObjectBusy;
3717 graphics->texthint = hint;
3719 return Ok;
3722 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3724 TRACE("(%p, %p)\n", graphics, matrix);
3726 if(!graphics || !matrix)
3727 return InvalidParameter;
3729 if(graphics->busy)
3730 return ObjectBusy;
3732 GdipDeleteMatrix(graphics->worldtrans);
3733 return GdipCloneMatrix(matrix, &graphics->worldtrans);
3736 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
3737 REAL dy, GpMatrixOrder order)
3739 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
3741 if(!graphics)
3742 return InvalidParameter;
3744 if(graphics->busy)
3745 return ObjectBusy;
3747 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
3750 /*****************************************************************************
3751 * GdipSetClipHrgn [GDIPLUS.@]
3753 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
3755 GpRegion *region;
3756 GpStatus status;
3758 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
3760 if(!graphics)
3761 return InvalidParameter;
3763 status = GdipCreateRegionHrgn(hrgn, &region);
3764 if(status != Ok)
3765 return status;
3767 status = GdipSetClipRegion(graphics, region, mode);
3769 GdipDeleteRegion(region);
3770 return status;
3773 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
3775 TRACE("(%p, %p, %d)\n", graphics, path, mode);
3777 if(!graphics)
3778 return InvalidParameter;
3780 if(graphics->busy)
3781 return ObjectBusy;
3783 return GdipCombineRegionPath(graphics->clip, path, mode);
3786 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
3787 REAL width, REAL height,
3788 CombineMode mode)
3790 GpRectF rect;
3792 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
3794 if(!graphics)
3795 return InvalidParameter;
3797 if(graphics->busy)
3798 return ObjectBusy;
3800 rect.X = x;
3801 rect.Y = y;
3802 rect.Width = width;
3803 rect.Height = height;
3805 return GdipCombineRegionRect(graphics->clip, &rect, mode);
3808 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
3809 INT width, INT height,
3810 CombineMode mode)
3812 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3814 if(!graphics)
3815 return InvalidParameter;
3817 if(graphics->busy)
3818 return ObjectBusy;
3820 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3823 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
3824 CombineMode mode)
3826 TRACE("(%p, %p, %d)\n", graphics, region, mode);
3828 if(!graphics || !region)
3829 return InvalidParameter;
3831 if(graphics->busy)
3832 return ObjectBusy;
3834 return GdipCombineRegionRegion(graphics->clip, region, mode);
3837 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
3838 UINT limitDpi)
3840 static int calls;
3842 if(!(calls++))
3843 FIXME("not implemented\n");
3845 return NotImplemented;
3848 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
3849 INT count)
3851 INT save_state;
3852 POINT *pti;
3854 TRACE("(%p, %p, %d)\n", graphics, points, count);
3856 if(!graphics || !pen || count<=0)
3857 return InvalidParameter;
3859 if(graphics->busy)
3860 return ObjectBusy;
3862 pti = GdipAlloc(sizeof(POINT) * count);
3864 save_state = prepare_dc(graphics, pen);
3865 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3867 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
3868 Polygon(graphics->hdc, pti, count);
3870 restore_dc(graphics, save_state);
3871 GdipFree(pti);
3873 return Ok;
3876 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
3877 INT count)
3879 GpStatus ret;
3880 GpPointF *ptf;
3881 INT i;
3883 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3885 if(count<=0) return InvalidParameter;
3886 ptf = GdipAlloc(sizeof(GpPointF) * count);
3888 for(i = 0;i < count; i++){
3889 ptf[i].X = (REAL)points[i].X;
3890 ptf[i].Y = (REAL)points[i].Y;
3893 ret = GdipDrawPolygon(graphics,pen,ptf,count);
3894 GdipFree(ptf);
3896 return ret;
3899 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
3901 TRACE("(%p, %p)\n", graphics, dpi);
3903 if(!graphics || !dpi)
3904 return InvalidParameter;
3906 if(graphics->busy)
3907 return ObjectBusy;
3909 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
3911 return Ok;
3914 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
3916 TRACE("(%p, %p)\n", graphics, dpi);
3918 if(!graphics || !dpi)
3919 return InvalidParameter;
3921 if(graphics->busy)
3922 return ObjectBusy;
3924 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
3926 return Ok;
3929 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
3930 GpMatrixOrder order)
3932 GpMatrix m;
3933 GpStatus ret;
3935 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
3937 if(!graphics || !matrix)
3938 return InvalidParameter;
3940 if(graphics->busy)
3941 return ObjectBusy;
3943 m = *(graphics->worldtrans);
3945 ret = GdipMultiplyMatrix(&m, matrix, order);
3946 if(ret == Ok)
3947 *(graphics->worldtrans) = m;
3949 return ret;
3952 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
3954 TRACE("(%p, %p)\n", graphics, hdc);
3956 if(!graphics || !hdc)
3957 return InvalidParameter;
3959 if(graphics->busy)
3960 return ObjectBusy;
3962 *hdc = graphics->hdc;
3963 graphics->busy = TRUE;
3965 return Ok;
3968 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
3970 TRACE("(%p, %p)\n", graphics, hdc);
3972 if(!graphics)
3973 return InvalidParameter;
3975 if(graphics->hdc != hdc || !(graphics->busy))
3976 return InvalidParameter;
3978 graphics->busy = FALSE;
3980 return Ok;
3983 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
3985 GpRegion *clip;
3986 GpStatus status;
3988 TRACE("(%p, %p)\n", graphics, region);
3990 if(!graphics || !region)
3991 return InvalidParameter;
3993 if(graphics->busy)
3994 return ObjectBusy;
3996 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
3997 return status;
3999 /* free everything except root node and header */
4000 delete_element(&region->node);
4001 memcpy(region, clip, sizeof(GpRegion));
4003 return Ok;
4006 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
4007 GpCoordinateSpace src_space, GpPointF *points, INT count)
4009 GpMatrix *matrix;
4010 GpStatus stat;
4011 REAL unitscale;
4013 if(!graphics || !points || count <= 0)
4014 return InvalidParameter;
4016 if(graphics->busy)
4017 return ObjectBusy;
4019 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4021 if (src_space == dst_space) return Ok;
4023 stat = GdipCreateMatrix(&matrix);
4024 if (stat == Ok)
4026 unitscale = convert_unit(graphics->hdc, graphics->unit);
4028 if(graphics->unit != UnitDisplay)
4029 unitscale *= graphics->scale;
4031 /* transform from src_space to CoordinateSpacePage */
4032 switch (src_space)
4034 case CoordinateSpaceWorld:
4035 GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
4036 break;
4037 case CoordinateSpacePage:
4038 break;
4039 case CoordinateSpaceDevice:
4040 GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
4041 break;
4044 /* transform from CoordinateSpacePage to dst_space */
4045 switch (dst_space)
4047 case CoordinateSpaceWorld:
4049 GpMatrix *inverted_transform;
4050 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
4051 if (stat == Ok)
4053 stat = GdipInvertMatrix(inverted_transform);
4054 if (stat == Ok)
4055 GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
4056 GdipDeleteMatrix(inverted_transform);
4058 break;
4060 case CoordinateSpacePage:
4061 break;
4062 case CoordinateSpaceDevice:
4063 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
4064 break;
4067 if (stat == Ok)
4068 stat = GdipTransformMatrixPoints(matrix, points, count);
4070 GdipDeleteMatrix(matrix);
4073 return stat;
4076 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
4077 GpCoordinateSpace src_space, GpPoint *points, INT count)
4079 GpPointF *pointsF;
4080 GpStatus ret;
4081 INT i;
4083 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4085 if(count <= 0)
4086 return InvalidParameter;
4088 pointsF = GdipAlloc(sizeof(GpPointF) * count);
4089 if(!pointsF)
4090 return OutOfMemory;
4092 for(i = 0; i < count; i++){
4093 pointsF[i].X = (REAL)points[i].X;
4094 pointsF[i].Y = (REAL)points[i].Y;
4097 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
4099 if(ret == Ok)
4100 for(i = 0; i < count; i++){
4101 points[i].X = roundr(pointsF[i].X);
4102 points[i].Y = roundr(pointsF[i].Y);
4104 GdipFree(pointsF);
4106 return ret;
4109 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
4111 FIXME("\n");
4113 return NULL;
4116 /*****************************************************************************
4117 * GdipTranslateClip [GDIPLUS.@]
4119 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
4121 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
4123 if(!graphics)
4124 return InvalidParameter;
4126 if(graphics->busy)
4127 return ObjectBusy;
4129 return GdipTranslateRegion(graphics->clip, dx, dy);
4132 /*****************************************************************************
4133 * GdipTranslateClipI [GDIPLUS.@]
4135 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
4137 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
4139 if(!graphics)
4140 return InvalidParameter;
4142 if(graphics->busy)
4143 return ObjectBusy;
4145 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
4149 /*****************************************************************************
4150 * GdipMeasureDriverString [GDIPLUS.@]
4152 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4153 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
4154 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
4156 FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
4157 return NotImplemented;
4160 /*****************************************************************************
4161 * GdipDrawDriverString [GDIPLUS.@]
4163 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4164 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
4165 GDIPCONST PointF *positions, INT flags,
4166 GDIPCONST GpMatrix *matrix )
4168 FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4169 return NotImplemented;
4172 /*****************************************************************************
4173 * GdipRecordMetafileI [GDIPLUS.@]
4175 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
4176 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4178 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
4179 return NotImplemented;
4182 /*****************************************************************************
4183 * GdipIsVisibleRectI [GDIPLUS.@]
4185 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4187 FIXME("(%p %d %d %d %d %p): stub\n", graphics, x, y, width, height, result);
4188 return NotImplemented;