gdiplus: Use UnitPixel for metafile bounds when creating from HMETAFILE.
[wine/hacks.git] / dlls / gdiplus / graphics.c
blob7b3f64fd2daa2fd0c061f1046706018b784552ff
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;
224 if (brush->pblendcount == 0)
225 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
226 else
228 int i=1;
229 ARGB left_blendcolor, right_blendcolor;
230 REAL left_blendpos, right_blendpos;
232 /* locate the blend colors surrounding this position */
233 while (blendfac > brush->pblendpos[i])
234 i++;
236 /* interpolate between the blend colors */
237 left_blendpos = brush->pblendpos[i-1];
238 left_blendcolor = brush->pblendcolor[i-1];
239 right_blendpos = brush->pblendpos[i];
240 right_blendcolor = brush->pblendcolor[i];
241 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
242 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
246 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
248 switch (brush->bt)
250 case BrushTypeLinearGradient:
252 GpLineGradient *line = (GpLineGradient*)brush;
253 RECT rc;
255 SelectClipPath(graphics->hdc, RGN_AND);
256 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
258 GpPointF endpointsf[2];
259 POINT endpointsi[2];
260 POINT poly[4];
262 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
264 endpointsf[0] = line->startpoint;
265 endpointsf[1] = line->endpoint;
266 transform_and_round_points(graphics, endpointsi, endpointsf, 2);
268 if (abs(endpointsi[0].x-endpointsi[1].x) > abs(endpointsi[0].y-endpointsi[1].y))
270 /* vertical-ish gradient */
271 int startx, endx; /* x co-ordinates of endpoints shifted to intersect the top of the visible rectangle */
272 int startbottomx; /* x co-ordinate of start point shifted to intersect the bottom of the visible rectangle */
273 int width;
274 COLORREF col;
275 HBRUSH hbrush, hprevbrush;
276 int leftx, rightx; /* x co-ordinates where the leftmost and rightmost gradient lines hit the top of the visible rectangle */
277 int x;
278 int tilt; /* horizontal distance covered by a gradient line */
280 startx = roundr((rc.top - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
281 endx = roundr((rc.top - endpointsf[1].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[1].X);
282 width = endx - startx;
283 startbottomx = roundr((rc.bottom - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
284 tilt = startx - startbottomx;
286 if (startx >= startbottomx)
288 leftx = rc.left;
289 rightx = rc.right + tilt;
291 else
293 leftx = rc.left + tilt;
294 rightx = rc.right;
297 poly[0].y = rc.bottom;
298 poly[1].y = rc.top;
299 poly[2].y = rc.top;
300 poly[3].y = rc.bottom;
302 for (x=leftx; x<=rightx; x++)
304 ARGB argb = blend_line_gradient(line, (x-startx)/(REAL)width);
305 col = ARGB2COLORREF(argb);
306 hbrush = CreateSolidBrush(col);
307 hprevbrush = SelectObject(graphics->hdc, hbrush);
308 poly[0].x = x - tilt - 1;
309 poly[1].x = x - 1;
310 poly[2].x = x;
311 poly[3].x = x - tilt;
312 Polygon(graphics->hdc, poly, 4);
313 SelectObject(graphics->hdc, hprevbrush);
314 DeleteObject(hbrush);
317 else if (endpointsi[0].y != endpointsi[1].y)
319 /* horizontal-ish gradient */
320 int starty, endy; /* y co-ordinates of endpoints shifted to intersect the left of the visible rectangle */
321 int startrighty; /* y co-ordinate of start point shifted to intersect the right of the visible rectangle */
322 int height;
323 COLORREF col;
324 HBRUSH hbrush, hprevbrush;
325 int topy, bottomy; /* y co-ordinates where the topmost and bottommost gradient lines hit the left of the visible rectangle */
326 int y;
327 int tilt; /* vertical distance covered by a gradient line */
329 starty = roundr((rc.left - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
330 endy = roundr((rc.left - endpointsf[1].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[1].Y);
331 height = endy - starty;
332 startrighty = roundr((rc.right - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
333 tilt = starty - startrighty;
335 if (starty >= startrighty)
337 topy = rc.top;
338 bottomy = rc.bottom + tilt;
340 else
342 topy = rc.top + tilt;
343 bottomy = rc.bottom;
346 poly[0].x = rc.right;
347 poly[1].x = rc.left;
348 poly[2].x = rc.left;
349 poly[3].x = rc.right;
351 for (y=topy; y<=bottomy; y++)
353 ARGB argb = blend_line_gradient(line, (y-starty)/(REAL)height);
354 col = ARGB2COLORREF(argb);
355 hbrush = CreateSolidBrush(col);
356 hprevbrush = SelectObject(graphics->hdc, hbrush);
357 poly[0].y = y - tilt - 1;
358 poly[1].y = y - 1;
359 poly[2].y = y;
360 poly[3].y = y - tilt;
361 Polygon(graphics->hdc, poly, 4);
362 SelectObject(graphics->hdc, hprevbrush);
363 DeleteObject(hbrush);
366 /* else startpoint == endpoint */
368 break;
370 case BrushTypeSolidColor:
372 GpSolidFill *fill = (GpSolidFill*)brush;
373 if (fill->bmp)
375 RECT rc;
376 /* partially transparent fill */
378 SelectClipPath(graphics->hdc, RGN_AND);
379 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
381 HDC hdc = CreateCompatibleDC(NULL);
382 HBITMAP oldbmp;
383 BLENDFUNCTION bf;
385 if (!hdc) break;
387 oldbmp = SelectObject(hdc, fill->bmp);
389 bf.BlendOp = AC_SRC_OVER;
390 bf.BlendFlags = 0;
391 bf.SourceConstantAlpha = 255;
392 bf.AlphaFormat = AC_SRC_ALPHA;
394 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
396 SelectObject(hdc, oldbmp);
397 DeleteDC(hdc);
400 break;
402 /* else fall through */
404 default:
405 SelectObject(graphics->hdc, brush->gdibrush);
406 FillPath(graphics->hdc);
407 break;
411 /* GdipDrawPie/GdipFillPie helper function */
412 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
413 REAL height, REAL startAngle, REAL sweepAngle)
415 GpPointF ptf[4];
416 POINT pti[4];
418 ptf[0].X = x;
419 ptf[0].Y = y;
420 ptf[1].X = x + width;
421 ptf[1].Y = y + height;
423 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
424 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
426 transform_and_round_points(graphics, pti, ptf, 4);
428 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
429 pti[2].y, pti[3].x, pti[3].y);
432 /* Draws the linecap the specified color and size on the hdc. The linecap is in
433 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
434 * should not be called on an hdc that has a path you care about. */
435 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
436 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
438 HGDIOBJ oldbrush = NULL, oldpen = NULL;
439 GpMatrix *matrix = NULL;
440 HBRUSH brush = NULL;
441 HPEN pen = NULL;
442 PointF ptf[4], *custptf = NULL;
443 POINT pt[4], *custpt = NULL;
444 BYTE *tp = NULL;
445 REAL theta, dsmall, dbig, dx, dy = 0.0;
446 INT i, count;
447 LOGBRUSH lb;
448 BOOL customstroke;
450 if((x1 == x2) && (y1 == y2))
451 return;
453 theta = gdiplus_atan2(y2 - y1, x2 - x1);
455 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
456 if(!customstroke){
457 brush = CreateSolidBrush(color);
458 lb.lbStyle = BS_SOLID;
459 lb.lbColor = color;
460 lb.lbHatch = 0;
461 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
462 PS_JOIN_MITER, 1, &lb, 0,
463 NULL);
464 oldbrush = SelectObject(graphics->hdc, brush);
465 oldpen = SelectObject(graphics->hdc, pen);
468 switch(cap){
469 case LineCapFlat:
470 break;
471 case LineCapSquare:
472 case LineCapSquareAnchor:
473 case LineCapDiamondAnchor:
474 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
475 if(cap == LineCapDiamondAnchor){
476 dsmall = cos(theta + M_PI_2) * size;
477 dbig = sin(theta + M_PI_2) * size;
479 else{
480 dsmall = cos(theta + M_PI_4) * size;
481 dbig = sin(theta + M_PI_4) * size;
484 ptf[0].X = x2 - dsmall;
485 ptf[1].X = x2 + dbig;
487 ptf[0].Y = y2 - dbig;
488 ptf[3].Y = y2 + dsmall;
490 ptf[1].Y = y2 - dsmall;
491 ptf[2].Y = y2 + dbig;
493 ptf[3].X = x2 - dbig;
494 ptf[2].X = x2 + dsmall;
496 transform_and_round_points(graphics, pt, ptf, 4);
497 Polygon(graphics->hdc, pt, 4);
499 break;
500 case LineCapArrowAnchor:
501 size = size * 4.0 / sqrt(3.0);
503 dx = cos(M_PI / 6.0 + theta) * size;
504 dy = sin(M_PI / 6.0 + theta) * size;
506 ptf[0].X = x2 - dx;
507 ptf[0].Y = y2 - dy;
509 dx = cos(- M_PI / 6.0 + theta) * size;
510 dy = sin(- M_PI / 6.0 + theta) * size;
512 ptf[1].X = x2 - dx;
513 ptf[1].Y = y2 - dy;
515 ptf[2].X = x2;
516 ptf[2].Y = y2;
518 transform_and_round_points(graphics, pt, ptf, 3);
519 Polygon(graphics->hdc, pt, 3);
521 break;
522 case LineCapRoundAnchor:
523 dx = dy = ANCHOR_WIDTH * size / 2.0;
525 ptf[0].X = x2 - dx;
526 ptf[0].Y = y2 - dy;
527 ptf[1].X = x2 + dx;
528 ptf[1].Y = y2 + dy;
530 transform_and_round_points(graphics, pt, ptf, 2);
531 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
533 break;
534 case LineCapTriangle:
535 size = size / 2.0;
536 dx = cos(M_PI_2 + theta) * size;
537 dy = sin(M_PI_2 + theta) * size;
539 ptf[0].X = x2 - dx;
540 ptf[0].Y = y2 - dy;
541 ptf[1].X = x2 + dx;
542 ptf[1].Y = y2 + dy;
544 dx = cos(theta) * size;
545 dy = sin(theta) * size;
547 ptf[2].X = x2 + dx;
548 ptf[2].Y = y2 + dy;
550 transform_and_round_points(graphics, pt, ptf, 3);
551 Polygon(graphics->hdc, pt, 3);
553 break;
554 case LineCapRound:
555 dx = dy = size / 2.0;
557 ptf[0].X = x2 - dx;
558 ptf[0].Y = y2 - dy;
559 ptf[1].X = x2 + dx;
560 ptf[1].Y = y2 + dy;
562 dx = -cos(M_PI_2 + theta) * size;
563 dy = -sin(M_PI_2 + theta) * size;
565 ptf[2].X = x2 - dx;
566 ptf[2].Y = y2 - dy;
567 ptf[3].X = x2 + dx;
568 ptf[3].Y = y2 + dy;
570 transform_and_round_points(graphics, pt, ptf, 4);
571 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
572 pt[2].y, pt[3].x, pt[3].y);
574 break;
575 case LineCapCustom:
576 if(!custom)
577 break;
579 count = custom->pathdata.Count;
580 custptf = GdipAlloc(count * sizeof(PointF));
581 custpt = GdipAlloc(count * sizeof(POINT));
582 tp = GdipAlloc(count);
584 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
585 goto custend;
587 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
589 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
590 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
591 MatrixOrderAppend);
592 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
593 GdipTransformMatrixPoints(matrix, custptf, count);
595 transform_and_round_points(graphics, custpt, custptf, count);
597 for(i = 0; i < count; i++)
598 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
600 if(custom->fill){
601 BeginPath(graphics->hdc);
602 PolyDraw(graphics->hdc, custpt, tp, count);
603 EndPath(graphics->hdc);
604 StrokeAndFillPath(graphics->hdc);
606 else
607 PolyDraw(graphics->hdc, custpt, tp, count);
609 custend:
610 GdipFree(custptf);
611 GdipFree(custpt);
612 GdipFree(tp);
613 GdipDeleteMatrix(matrix);
614 break;
615 default:
616 break;
619 if(!customstroke){
620 SelectObject(graphics->hdc, oldbrush);
621 SelectObject(graphics->hdc, oldpen);
622 DeleteObject(brush);
623 DeleteObject(pen);
627 /* Shortens the line by the given percent by changing x2, y2.
628 * If percent is > 1.0 then the line will change direction.
629 * If percent is negative it can lengthen the line. */
630 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
632 REAL dist, theta, dx, dy;
634 if((y1 == *y2) && (x1 == *x2))
635 return;
637 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
638 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
639 dx = cos(theta) * dist;
640 dy = sin(theta) * dist;
642 *x2 = *x2 + dx;
643 *y2 = *y2 + dy;
646 /* Shortens the line by the given amount by changing x2, y2.
647 * If the amount is greater than the distance, the line will become length 0.
648 * If the amount is negative, it can lengthen the line. */
649 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
651 REAL dx, dy, percent;
653 dx = *x2 - x1;
654 dy = *y2 - y1;
655 if(dx == 0 && dy == 0)
656 return;
658 percent = amt / sqrt(dx * dx + dy * dy);
659 if(percent >= 1.0){
660 *x2 = x1;
661 *y2 = y1;
662 return;
665 shorten_line_percent(x1, y1, x2, y2, percent);
668 /* Draws lines between the given points, and if caps is true then draws an endcap
669 * at the end of the last line. */
670 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
671 GDIPCONST GpPointF * pt, INT count, BOOL caps)
673 POINT *pti = NULL;
674 GpPointF *ptcopy = NULL;
675 GpStatus status = GenericError;
677 if(!count)
678 return Ok;
680 pti = GdipAlloc(count * sizeof(POINT));
681 ptcopy = GdipAlloc(count * sizeof(GpPointF));
683 if(!pti || !ptcopy){
684 status = OutOfMemory;
685 goto end;
688 memcpy(ptcopy, pt, count * sizeof(GpPointF));
690 if(caps){
691 if(pen->endcap == LineCapArrowAnchor)
692 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
693 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
694 else if((pen->endcap == LineCapCustom) && pen->customend)
695 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
696 &ptcopy[count-1].X, &ptcopy[count-1].Y,
697 pen->customend->inset * pen->width);
699 if(pen->startcap == LineCapArrowAnchor)
700 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
701 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
702 else if((pen->startcap == LineCapCustom) && pen->customstart)
703 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
704 &ptcopy[0].X, &ptcopy[0].Y,
705 pen->customstart->inset * pen->width);
707 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
708 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
709 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
710 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
713 transform_and_round_points(graphics, pti, ptcopy, count);
715 if(Polyline(graphics->hdc, pti, count))
716 status = Ok;
718 end:
719 GdipFree(pti);
720 GdipFree(ptcopy);
722 return status;
725 /* Conducts a linear search to find the bezier points that will back off
726 * the endpoint of the curve by a distance of amt. Linear search works
727 * better than binary in this case because there are multiple solutions,
728 * and binary searches often find a bad one. I don't think this is what
729 * Windows does but short of rendering the bezier without GDI's help it's
730 * the best we can do. If rev then work from the start of the passed points
731 * instead of the end. */
732 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
734 GpPointF origpt[4];
735 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
736 INT i, first = 0, second = 1, third = 2, fourth = 3;
738 if(rev){
739 first = 3;
740 second = 2;
741 third = 1;
742 fourth = 0;
745 origx = pt[fourth].X;
746 origy = pt[fourth].Y;
747 memcpy(origpt, pt, sizeof(GpPointF) * 4);
749 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
750 /* reset bezier points to original values */
751 memcpy(pt, origpt, sizeof(GpPointF) * 4);
752 /* Perform magic on bezier points. Order is important here.*/
753 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
754 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
755 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
756 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
757 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
758 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
760 dx = pt[fourth].X - origx;
761 dy = pt[fourth].Y - origy;
763 diff = sqrt(dx * dx + dy * dy);
764 percent += 0.0005 * amt;
768 /* Draws bezier curves between given points, and if caps is true then draws an
769 * endcap at the end of the last line. */
770 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
771 GDIPCONST GpPointF * pt, INT count, BOOL caps)
773 POINT *pti;
774 GpPointF *ptcopy;
775 GpStatus status = GenericError;
777 if(!count)
778 return Ok;
780 pti = GdipAlloc(count * sizeof(POINT));
781 ptcopy = GdipAlloc(count * sizeof(GpPointF));
783 if(!pti || !ptcopy){
784 status = OutOfMemory;
785 goto end;
788 memcpy(ptcopy, pt, count * sizeof(GpPointF));
790 if(caps){
791 if(pen->endcap == LineCapArrowAnchor)
792 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
793 else if((pen->endcap == LineCapCustom) && pen->customend)
794 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
795 FALSE);
797 if(pen->startcap == LineCapArrowAnchor)
798 shorten_bezier_amt(ptcopy, pen->width, TRUE);
799 else if((pen->startcap == LineCapCustom) && pen->customstart)
800 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
802 /* the direction of the line cap is parallel to the direction at the
803 * end of the bezier (which, if it has been shortened, is not the same
804 * as the direction from pt[count-2] to pt[count-1]) */
805 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
806 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
807 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
808 pt[count - 1].X, pt[count - 1].Y);
810 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
811 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
812 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
815 transform_and_round_points(graphics, pti, ptcopy, count);
817 PolyBezier(graphics->hdc, pti, count);
819 status = Ok;
821 end:
822 GdipFree(pti);
823 GdipFree(ptcopy);
825 return status;
828 /* Draws a combination of bezier curves and lines between points. */
829 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
830 GDIPCONST BYTE * types, INT count, BOOL caps)
832 POINT *pti = GdipAlloc(count * sizeof(POINT));
833 BYTE *tp = GdipAlloc(count);
834 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
835 INT i, j;
836 GpStatus status = GenericError;
838 if(!count){
839 status = Ok;
840 goto end;
842 if(!pti || !tp || !ptcopy){
843 status = OutOfMemory;
844 goto end;
847 for(i = 1; i < count; i++){
848 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
849 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
850 || !(types[i + 1] & PathPointTypeBezier)){
851 ERR("Bad bezier points\n");
852 goto end;
854 i += 2;
858 memcpy(ptcopy, pt, count * sizeof(GpPointF));
860 /* If we are drawing caps, go through the points and adjust them accordingly,
861 * and draw the caps. */
862 if(caps){
863 switch(types[count - 1] & PathPointTypePathTypeMask){
864 case PathPointTypeBezier:
865 if(pen->endcap == LineCapArrowAnchor)
866 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
867 else if((pen->endcap == LineCapCustom) && pen->customend)
868 shorten_bezier_amt(&ptcopy[count - 4],
869 pen->width * pen->customend->inset, FALSE);
871 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
872 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
873 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
874 pt[count - 1].X, pt[count - 1].Y);
876 break;
877 case PathPointTypeLine:
878 if(pen->endcap == LineCapArrowAnchor)
879 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
880 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
881 pen->width);
882 else if((pen->endcap == LineCapCustom) && pen->customend)
883 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
884 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
885 pen->customend->inset * pen->width);
887 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
888 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
889 pt[count - 1].Y);
891 break;
892 default:
893 ERR("Bad path last point\n");
894 goto end;
897 /* Find start of points */
898 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
899 == PathPointTypeStart); j++);
901 switch(types[j] & PathPointTypePathTypeMask){
902 case PathPointTypeBezier:
903 if(pen->startcap == LineCapArrowAnchor)
904 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
905 else if((pen->startcap == LineCapCustom) && pen->customstart)
906 shorten_bezier_amt(&ptcopy[j - 1],
907 pen->width * pen->customstart->inset, TRUE);
909 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
910 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
911 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
912 pt[j - 1].X, pt[j - 1].Y);
914 break;
915 case PathPointTypeLine:
916 if(pen->startcap == LineCapArrowAnchor)
917 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
918 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
919 pen->width);
920 else if((pen->startcap == LineCapCustom) && pen->customstart)
921 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
922 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
923 pen->customstart->inset * pen->width);
925 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
926 pt[j].X, pt[j].Y, pt[j - 1].X,
927 pt[j - 1].Y);
929 break;
930 default:
931 ERR("Bad path points\n");
932 goto end;
936 transform_and_round_points(graphics, pti, ptcopy, count);
938 for(i = 0; i < count; i++){
939 tp[i] = convert_path_point_type(types[i]);
942 PolyDraw(graphics->hdc, pti, tp, count);
944 status = Ok;
946 end:
947 GdipFree(pti);
948 GdipFree(ptcopy);
949 GdipFree(tp);
951 return status;
954 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
956 GpStatus result;
958 BeginPath(graphics->hdc);
959 result = draw_poly(graphics, NULL, path->pathdata.Points,
960 path->pathdata.Types, path->pathdata.Count, FALSE);
961 EndPath(graphics->hdc);
962 return result;
965 typedef struct _GraphicsContainerItem {
966 struct list entry;
967 GraphicsContainer contid;
969 SmoothingMode smoothing;
970 CompositingQuality compqual;
971 InterpolationMode interpolation;
972 CompositingMode compmode;
973 TextRenderingHint texthint;
974 REAL scale;
975 GpUnit unit;
976 PixelOffsetMode pixeloffset;
977 UINT textcontrast;
978 GpMatrix* worldtrans;
979 GpRegion* clip;
980 } GraphicsContainerItem;
982 static GpStatus init_container(GraphicsContainerItem** container,
983 GDIPCONST GpGraphics* graphics){
984 GpStatus sts;
986 *container = GdipAlloc(sizeof(GraphicsContainerItem));
987 if(!(*container))
988 return OutOfMemory;
990 (*container)->contid = graphics->contid + 1;
992 (*container)->smoothing = graphics->smoothing;
993 (*container)->compqual = graphics->compqual;
994 (*container)->interpolation = graphics->interpolation;
995 (*container)->compmode = graphics->compmode;
996 (*container)->texthint = graphics->texthint;
997 (*container)->scale = graphics->scale;
998 (*container)->unit = graphics->unit;
999 (*container)->textcontrast = graphics->textcontrast;
1000 (*container)->pixeloffset = graphics->pixeloffset;
1002 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1003 if(sts != Ok){
1004 GdipFree(*container);
1005 *container = NULL;
1006 return sts;
1009 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1010 if(sts != Ok){
1011 GdipDeleteMatrix((*container)->worldtrans);
1012 GdipFree(*container);
1013 *container = NULL;
1014 return sts;
1017 return Ok;
1020 static void delete_container(GraphicsContainerItem* container){
1021 GdipDeleteMatrix(container->worldtrans);
1022 GdipDeleteRegion(container->clip);
1023 GdipFree(container);
1026 static GpStatus restore_container(GpGraphics* graphics,
1027 GDIPCONST GraphicsContainerItem* container){
1028 GpStatus sts;
1029 GpMatrix *newTrans;
1030 GpRegion *newClip;
1032 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1033 if(sts != Ok)
1034 return sts;
1036 sts = GdipCloneRegion(container->clip, &newClip);
1037 if(sts != Ok){
1038 GdipDeleteMatrix(newTrans);
1039 return sts;
1042 GdipDeleteMatrix(graphics->worldtrans);
1043 graphics->worldtrans = newTrans;
1045 GdipDeleteRegion(graphics->clip);
1046 graphics->clip = newClip;
1048 graphics->contid = container->contid - 1;
1050 graphics->smoothing = container->smoothing;
1051 graphics->compqual = container->compqual;
1052 graphics->interpolation = container->interpolation;
1053 graphics->compmode = container->compmode;
1054 graphics->texthint = container->texthint;
1055 graphics->scale = container->scale;
1056 graphics->unit = container->unit;
1057 graphics->textcontrast = container->textcontrast;
1058 graphics->pixeloffset = container->pixeloffset;
1060 return Ok;
1063 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1065 RECT wnd_rect;
1067 if(graphics->hwnd) {
1068 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1069 return GenericError;
1071 rect->X = wnd_rect.left;
1072 rect->Y = wnd_rect.top;
1073 rect->Width = wnd_rect.right - wnd_rect.left;
1074 rect->Height = wnd_rect.bottom - wnd_rect.top;
1075 }else{
1076 rect->X = 0;
1077 rect->Y = 0;
1078 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1079 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1082 return Ok;
1085 /* on success, rgn will contain the region of the graphics object which
1086 * is visible after clipping has been applied */
1087 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1089 GpStatus stat;
1090 GpRectF rectf;
1091 GpRegion* tmp;
1093 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1094 return stat;
1096 if((stat = GdipCreateRegion(&tmp)) != Ok)
1097 return stat;
1099 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1100 goto end;
1102 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1103 goto end;
1105 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1107 end:
1108 GdipDeleteRegion(tmp);
1109 return stat;
1112 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1114 TRACE("(%p, %p)\n", hdc, graphics);
1116 return GdipCreateFromHDC2(hdc, NULL, graphics);
1119 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1121 GpStatus retval;
1123 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1125 if(hDevice != NULL) {
1126 FIXME("Don't know how to handle parameter hDevice\n");
1127 return NotImplemented;
1130 if(hdc == NULL)
1131 return OutOfMemory;
1133 if(graphics == NULL)
1134 return InvalidParameter;
1136 *graphics = GdipAlloc(sizeof(GpGraphics));
1137 if(!*graphics) return OutOfMemory;
1139 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1140 GdipFree(*graphics);
1141 return retval;
1144 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1145 GdipFree((*graphics)->worldtrans);
1146 GdipFree(*graphics);
1147 return retval;
1150 (*graphics)->hdc = hdc;
1151 (*graphics)->hwnd = WindowFromDC(hdc);
1152 (*graphics)->owndc = FALSE;
1153 (*graphics)->smoothing = SmoothingModeDefault;
1154 (*graphics)->compqual = CompositingQualityDefault;
1155 (*graphics)->interpolation = InterpolationModeDefault;
1156 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1157 (*graphics)->compmode = CompositingModeSourceOver;
1158 (*graphics)->unit = UnitDisplay;
1159 (*graphics)->scale = 1.0;
1160 (*graphics)->busy = FALSE;
1161 (*graphics)->textcontrast = 4;
1162 list_init(&(*graphics)->containers);
1163 (*graphics)->contid = 0;
1165 TRACE("<-- %p\n", *graphics);
1167 return Ok;
1170 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1172 GpStatus ret;
1173 HDC hdc;
1175 TRACE("(%p, %p)\n", hwnd, graphics);
1177 hdc = GetDC(hwnd);
1179 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1181 ReleaseDC(hwnd, hdc);
1182 return ret;
1185 (*graphics)->hwnd = hwnd;
1186 (*graphics)->owndc = TRUE;
1188 return Ok;
1191 /* FIXME: no icm handling */
1192 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1194 TRACE("(%p, %p)\n", hwnd, graphics);
1196 return GdipCreateFromHWND(hwnd, graphics);
1199 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1200 GpMetafile **metafile)
1202 static int calls;
1204 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1206 if(!hemf || !metafile)
1207 return InvalidParameter;
1209 if(!(calls++))
1210 FIXME("not implemented\n");
1212 return NotImplemented;
1215 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1216 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1218 IStream *stream = NULL;
1219 UINT read;
1220 BYTE* copy;
1221 HENHMETAFILE hemf;
1222 GpStatus retval = Ok;
1224 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1226 if(!hwmf || !metafile || !placeable)
1227 return InvalidParameter;
1229 *metafile = NULL;
1230 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1231 if(!read)
1232 return GenericError;
1233 copy = GdipAlloc(read);
1234 GetMetaFileBitsEx(hwmf, read, copy);
1236 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1237 GdipFree(copy);
1239 read = GetEnhMetaFileBits(hemf, 0, NULL);
1240 copy = GdipAlloc(read);
1241 GetEnhMetaFileBits(hemf, read, copy);
1242 DeleteEnhMetaFile(hemf);
1244 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1245 ERR("could not make stream\n");
1246 GdipFree(copy);
1247 retval = GenericError;
1248 goto err;
1251 *metafile = GdipAlloc(sizeof(GpMetafile));
1252 if(!*metafile){
1253 retval = OutOfMemory;
1254 goto err;
1257 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1258 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1260 retval = GenericError;
1261 goto err;
1265 (*metafile)->image.type = ImageTypeMetafile;
1266 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1267 (*metafile)->image.palette_flags = 0;
1268 (*metafile)->image.palette_count = 0;
1269 (*metafile)->image.palette_size = 0;
1270 (*metafile)->image.palette_entries = NULL;
1271 (*metafile)->image.xres = (REAL)placeable->Inch;
1272 (*metafile)->image.yres = (REAL)placeable->Inch;
1273 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1274 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1275 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1276 - placeable->BoundingBox.Left));
1277 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1278 - placeable->BoundingBox.Top));
1279 (*metafile)->unit = UnitPixel;
1281 if(delete)
1282 DeleteMetaFile(hwmf);
1284 TRACE("<-- %p\n", *metafile);
1286 err:
1287 if (retval != Ok)
1288 GdipFree(*metafile);
1289 IStream_Release(stream);
1290 return retval;
1293 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1294 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1296 HMETAFILE hmf = GetMetaFileW(file);
1298 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1300 if(!hmf) return InvalidParameter;
1302 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1305 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1306 GpMetafile **metafile)
1308 FIXME("(%p, %p): stub\n", file, metafile);
1309 return NotImplemented;
1312 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1313 GpMetafile **metafile)
1315 FIXME("(%p, %p): stub\n", stream, metafile);
1316 return NotImplemented;
1319 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1320 UINT access, IStream **stream)
1322 DWORD dwMode;
1323 HRESULT ret;
1325 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1327 if(!stream || !filename)
1328 return InvalidParameter;
1330 if(access & GENERIC_WRITE)
1331 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1332 else if(access & GENERIC_READ)
1333 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1334 else
1335 return InvalidParameter;
1337 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1339 return hresult_to_status(ret);
1342 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1344 GraphicsContainerItem *cont, *next;
1345 TRACE("(%p)\n", graphics);
1347 if(!graphics) return InvalidParameter;
1348 if(graphics->busy) return ObjectBusy;
1350 if(graphics->owndc)
1351 ReleaseDC(graphics->hwnd, graphics->hdc);
1353 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1354 list_remove(&cont->entry);
1355 delete_container(cont);
1358 GdipDeleteRegion(graphics->clip);
1359 GdipDeleteMatrix(graphics->worldtrans);
1360 GdipFree(graphics);
1362 return Ok;
1365 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1366 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1368 INT save_state, num_pts;
1369 GpPointF points[MAX_ARC_PTS];
1370 GpStatus retval;
1372 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1373 width, height, startAngle, sweepAngle);
1375 if(!graphics || !pen || width <= 0 || height <= 0)
1376 return InvalidParameter;
1378 if(graphics->busy)
1379 return ObjectBusy;
1381 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1383 save_state = prepare_dc(graphics, pen);
1385 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1387 restore_dc(graphics, save_state);
1389 return retval;
1392 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1393 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1395 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1396 width, height, startAngle, sweepAngle);
1398 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1401 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1402 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1404 INT save_state;
1405 GpPointF pt[4];
1406 GpStatus retval;
1408 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1409 x2, y2, x3, y3, x4, y4);
1411 if(!graphics || !pen)
1412 return InvalidParameter;
1414 if(graphics->busy)
1415 return ObjectBusy;
1417 pt[0].X = x1;
1418 pt[0].Y = y1;
1419 pt[1].X = x2;
1420 pt[1].Y = y2;
1421 pt[2].X = x3;
1422 pt[2].Y = y3;
1423 pt[3].X = x4;
1424 pt[3].Y = y4;
1426 save_state = prepare_dc(graphics, pen);
1428 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1430 restore_dc(graphics, save_state);
1432 return retval;
1435 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1436 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1438 INT save_state;
1439 GpPointF pt[4];
1440 GpStatus retval;
1442 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1443 x2, y2, x3, y3, x4, y4);
1445 if(!graphics || !pen)
1446 return InvalidParameter;
1448 if(graphics->busy)
1449 return ObjectBusy;
1451 pt[0].X = x1;
1452 pt[0].Y = y1;
1453 pt[1].X = x2;
1454 pt[1].Y = y2;
1455 pt[2].X = x3;
1456 pt[2].Y = y3;
1457 pt[3].X = x4;
1458 pt[3].Y = y4;
1460 save_state = prepare_dc(graphics, pen);
1462 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1464 restore_dc(graphics, save_state);
1466 return retval;
1469 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1470 GDIPCONST GpPointF *points, INT count)
1472 INT i;
1473 GpStatus ret;
1475 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1477 if(!graphics || !pen || !points || (count <= 0))
1478 return InvalidParameter;
1480 if(graphics->busy)
1481 return ObjectBusy;
1483 for(i = 0; i < floor(count / 4); i++){
1484 ret = GdipDrawBezier(graphics, pen,
1485 points[4*i].X, points[4*i].Y,
1486 points[4*i + 1].X, points[4*i + 1].Y,
1487 points[4*i + 2].X, points[4*i + 2].Y,
1488 points[4*i + 3].X, points[4*i + 3].Y);
1489 if(ret != Ok)
1490 return ret;
1493 return Ok;
1496 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1497 GDIPCONST GpPoint *points, INT count)
1499 GpPointF *pts;
1500 GpStatus ret;
1501 INT i;
1503 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1505 if(!graphics || !pen || !points || (count <= 0))
1506 return InvalidParameter;
1508 if(graphics->busy)
1509 return ObjectBusy;
1511 pts = GdipAlloc(sizeof(GpPointF) * count);
1512 if(!pts)
1513 return OutOfMemory;
1515 for(i = 0; i < count; i++){
1516 pts[i].X = (REAL)points[i].X;
1517 pts[i].Y = (REAL)points[i].Y;
1520 ret = GdipDrawBeziers(graphics,pen,pts,count);
1522 GdipFree(pts);
1524 return ret;
1527 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1528 GDIPCONST GpPointF *points, INT count)
1530 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1532 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1535 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1536 GDIPCONST GpPoint *points, INT count)
1538 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1540 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1543 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1544 GDIPCONST GpPointF *points, INT count, REAL tension)
1546 GpPath *path;
1547 GpStatus stat;
1549 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1551 if(!graphics || !pen || !points || count <= 0)
1552 return InvalidParameter;
1554 if(graphics->busy)
1555 return ObjectBusy;
1557 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1558 return stat;
1560 stat = GdipAddPathClosedCurve2(path, points, count, tension);
1561 if(stat != Ok){
1562 GdipDeletePath(path);
1563 return stat;
1566 stat = GdipDrawPath(graphics, pen, path);
1568 GdipDeletePath(path);
1570 return stat;
1573 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1574 GDIPCONST GpPoint *points, INT count, REAL tension)
1576 GpPointF *ptf;
1577 GpStatus stat;
1578 INT i;
1580 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1582 if(!points || count <= 0)
1583 return InvalidParameter;
1585 ptf = GdipAlloc(sizeof(GpPointF)*count);
1586 if(!ptf)
1587 return OutOfMemory;
1589 for(i = 0; i < count; i++){
1590 ptf[i].X = (REAL)points[i].X;
1591 ptf[i].Y = (REAL)points[i].Y;
1594 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1596 GdipFree(ptf);
1598 return stat;
1601 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1602 GDIPCONST GpPointF *points, INT count)
1604 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1606 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1609 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1610 GDIPCONST GpPoint *points, INT count)
1612 GpPointF *pointsF;
1613 GpStatus ret;
1614 INT i;
1616 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1618 if(!points)
1619 return InvalidParameter;
1621 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1622 if(!pointsF)
1623 return OutOfMemory;
1625 for(i = 0; i < count; i++){
1626 pointsF[i].X = (REAL)points[i].X;
1627 pointsF[i].Y = (REAL)points[i].Y;
1630 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1631 GdipFree(pointsF);
1633 return ret;
1636 /* Approximates cardinal spline with Bezier curves. */
1637 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1638 GDIPCONST GpPointF *points, INT count, REAL tension)
1640 /* PolyBezier expects count*3-2 points. */
1641 INT i, len_pt = count*3-2, save_state;
1642 GpPointF *pt;
1643 REAL x1, x2, y1, y2;
1644 GpStatus retval;
1646 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1648 if(!graphics || !pen)
1649 return InvalidParameter;
1651 if(graphics->busy)
1652 return ObjectBusy;
1654 if(count < 2)
1655 return InvalidParameter;
1657 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1658 if(!pt)
1659 return OutOfMemory;
1661 tension = tension * TENSION_CONST;
1663 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1664 tension, &x1, &y1);
1666 pt[0].X = points[0].X;
1667 pt[0].Y = points[0].Y;
1668 pt[1].X = x1;
1669 pt[1].Y = y1;
1671 for(i = 0; i < count-2; i++){
1672 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1674 pt[3*i+2].X = x1;
1675 pt[3*i+2].Y = y1;
1676 pt[3*i+3].X = points[i+1].X;
1677 pt[3*i+3].Y = points[i+1].Y;
1678 pt[3*i+4].X = x2;
1679 pt[3*i+4].Y = y2;
1682 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1683 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1685 pt[len_pt-2].X = x1;
1686 pt[len_pt-2].Y = y1;
1687 pt[len_pt-1].X = points[count-1].X;
1688 pt[len_pt-1].Y = points[count-1].Y;
1690 save_state = prepare_dc(graphics, pen);
1692 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1694 GdipFree(pt);
1695 restore_dc(graphics, save_state);
1697 return retval;
1700 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1701 GDIPCONST GpPoint *points, INT count, REAL tension)
1703 GpPointF *pointsF;
1704 GpStatus ret;
1705 INT i;
1707 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1709 if(!points)
1710 return InvalidParameter;
1712 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1713 if(!pointsF)
1714 return OutOfMemory;
1716 for(i = 0; i < count; i++){
1717 pointsF[i].X = (REAL)points[i].X;
1718 pointsF[i].Y = (REAL)points[i].Y;
1721 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1722 GdipFree(pointsF);
1724 return ret;
1727 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1728 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1729 REAL tension)
1731 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1733 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1734 return InvalidParameter;
1737 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1740 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1741 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1742 REAL tension)
1744 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1746 if(count < 0){
1747 return OutOfMemory;
1750 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1751 return InvalidParameter;
1754 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1757 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1758 REAL y, REAL width, REAL height)
1760 INT save_state;
1761 GpPointF ptf[2];
1762 POINT pti[2];
1764 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1766 if(!graphics || !pen)
1767 return InvalidParameter;
1769 if(graphics->busy)
1770 return ObjectBusy;
1772 ptf[0].X = x;
1773 ptf[0].Y = y;
1774 ptf[1].X = x + width;
1775 ptf[1].Y = y + height;
1777 save_state = prepare_dc(graphics, pen);
1778 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1780 transform_and_round_points(graphics, pti, ptf, 2);
1782 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1784 restore_dc(graphics, save_state);
1786 return Ok;
1789 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1790 INT y, INT width, INT height)
1792 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1794 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1798 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1800 UINT width, height;
1801 GpPointF points[3];
1803 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1805 if(!graphics || !image)
1806 return InvalidParameter;
1808 GdipGetImageWidth(image, &width);
1809 GdipGetImageHeight(image, &height);
1811 /* FIXME: we should use the graphics and image dpi, somehow */
1813 points[0].X = points[2].X = x;
1814 points[0].Y = points[1].Y = y;
1815 points[1].X = x + width;
1816 points[2].Y = y + height;
1818 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
1819 UnitPixel, NULL, NULL, NULL);
1822 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1823 INT y)
1825 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1827 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1830 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1831 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1832 GpUnit srcUnit)
1834 GpPointF points[3];
1835 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1837 points[0].X = points[2].X = x;
1838 points[0].Y = points[1].Y = y;
1840 /* FIXME: convert image coordinates to Graphics coordinates? */
1841 points[1].X = x + srcwidth;
1842 points[2].Y = y + srcheight;
1844 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1845 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
1848 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
1849 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
1850 GpUnit srcUnit)
1852 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1855 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
1856 GDIPCONST GpPointF *dstpoints, INT count)
1858 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1859 return NotImplemented;
1862 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
1863 GDIPCONST GpPoint *dstpoints, INT count)
1865 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1866 return NotImplemented;
1869 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1870 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1871 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1872 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1873 DrawImageAbort callback, VOID * callbackData)
1875 GpPointF ptf[4];
1876 POINT pti[4];
1877 REAL dx, dy;
1878 GpStatus stat;
1880 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1881 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1882 callbackData);
1884 if(!graphics || !image || !points || count != 3)
1885 return InvalidParameter;
1887 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
1888 debugstr_pointf(&points[2]));
1890 memcpy(ptf, points, 3 * sizeof(GpPointF));
1891 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
1892 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
1893 transform_and_round_points(graphics, pti, ptf, 4);
1895 if (image->picture)
1897 if(srcUnit == UnitInch)
1898 dx = dy = (REAL) INCH_HIMETRIC;
1899 else if(srcUnit == UnitPixel){
1900 dx = ((REAL) INCH_HIMETRIC) /
1901 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1902 dy = ((REAL) INCH_HIMETRIC) /
1903 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1905 else
1906 return NotImplemented;
1908 if(IPicture_Render(image->picture, graphics->hdc,
1909 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1910 srcx * dx, srcy * dy,
1911 srcwidth * dx, srcheight * dy,
1912 NULL) != S_OK){
1913 if(callback)
1914 callback(callbackData);
1915 return GenericError;
1918 else if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
1920 GpBitmap* bitmap = (GpBitmap*)image;
1921 int use_software=0;
1923 if (srcUnit == UnitInch)
1924 dx = dy = 96.0; /* FIXME: use the image resolution */
1925 else if (srcUnit == UnitPixel)
1926 dx = dy = 1.0;
1927 else
1928 return NotImplemented;
1930 if (graphics->image && graphics->image->type == ImageTypeBitmap)
1932 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
1933 if (!(dst_bitmap->format == PixelFormat16bppRGB555 ||
1934 dst_bitmap->format == PixelFormat24bppRGB ||
1935 dst_bitmap->format == PixelFormat32bppRGB))
1936 use_software = 1;
1939 if (use_software)
1941 RECT src_area, dst_area;
1942 int i, x, y;
1943 GpMatrix *dst_to_src;
1944 REAL m11, m12, m21, m22, mdx, mdy;
1946 src_area.left = srcx*dx;
1947 src_area.top = srcy*dy;
1948 src_area.right = (srcx+srcwidth)*dx;
1949 src_area.bottom = (srcy+srcheight)*dy;
1951 dst_area.left = dst_area.right = pti[0].x;
1952 dst_area.top = dst_area.bottom = pti[0].y;
1953 for (i=1; i<4; i++)
1955 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
1956 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
1957 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
1958 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
1961 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
1962 m12 = (ptf[2].X - ptf[0].X) / srcheight;
1963 mdx = ptf[0].X - m11 * srcx - m12 * srcy;
1964 m21 = (ptf[1].Y - ptf[0].Y) / srcwidth;
1965 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
1966 mdy = ptf[0].Y - m21 * srcx - m22 * srcy;
1968 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
1969 if (stat != Ok) return stat;
1971 stat = GdipInvertMatrix(dst_to_src);
1972 if (stat != Ok)
1974 GdipDeleteMatrix(dst_to_src);
1975 return stat;
1978 for (x=dst_area.left; x<dst_area.right; x++)
1980 for (y=dst_area.top; y<dst_area.bottom; y++)
1982 GpPointF src_pointf;
1983 int src_x, src_y;
1984 ARGB src_color, dst_color;
1986 src_pointf.X = x;
1987 src_pointf.Y = y;
1989 GdipTransformMatrixPoints(dst_to_src, &src_pointf, 1);
1991 src_x = roundr(src_pointf.X);
1992 src_y = roundr(src_pointf.Y);
1994 if (src_x < src_area.left || src_x >= src_area.right ||
1995 src_y < src_area.top || src_y >= src_area.bottom)
1996 /* FIXME: Use wrapmode */
1997 continue;
1999 GdipBitmapGetPixel(bitmap, src_x, src_y, &src_color);
2000 GdipBitmapGetPixel((GpBitmap*)graphics->image, x, y, &dst_color);
2001 GdipBitmapSetPixel((GpBitmap*)graphics->image, x, y, color_over(dst_color, src_color));
2005 GdipDeleteMatrix(dst_to_src);
2007 else
2009 HDC hdc;
2010 int temp_hdc=0, temp_bitmap=0;
2011 HBITMAP hbitmap, old_hbm=NULL;
2013 if (!(bitmap->format == PixelFormat16bppRGB555 ||
2014 bitmap->format == PixelFormat24bppRGB ||
2015 bitmap->format == PixelFormat32bppRGB ||
2016 bitmap->format == PixelFormat32bppPARGB))
2018 BITMAPINFOHEADER bih;
2019 BYTE *temp_bits;
2020 PixelFormat dst_format;
2022 /* we can't draw a bitmap of this format directly */
2023 hdc = CreateCompatibleDC(0);
2024 temp_hdc = 1;
2025 temp_bitmap = 1;
2027 bih.biSize = sizeof(BITMAPINFOHEADER);
2028 bih.biWidth = bitmap->width;
2029 bih.biHeight = -bitmap->height;
2030 bih.biPlanes = 1;
2031 bih.biBitCount = 32;
2032 bih.biCompression = BI_RGB;
2033 bih.biSizeImage = 0;
2034 bih.biXPelsPerMeter = 0;
2035 bih.biYPelsPerMeter = 0;
2036 bih.biClrUsed = 0;
2037 bih.biClrImportant = 0;
2039 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
2040 (void**)&temp_bits, NULL, 0);
2042 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2043 dst_format = PixelFormat32bppPARGB;
2044 else
2045 dst_format = PixelFormat32bppRGB;
2047 convert_pixels(bitmap->width, bitmap->height,
2048 bitmap->width*4, temp_bits, dst_format,
2049 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
2051 else
2053 hbitmap = bitmap->hbitmap;
2054 hdc = bitmap->hdc;
2055 temp_hdc = (hdc == 0);
2058 if (temp_hdc)
2060 if (!hdc) hdc = CreateCompatibleDC(0);
2061 old_hbm = SelectObject(hdc, hbitmap);
2064 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2066 BLENDFUNCTION bf;
2068 bf.BlendOp = AC_SRC_OVER;
2069 bf.BlendFlags = 0;
2070 bf.SourceConstantAlpha = 255;
2071 bf.AlphaFormat = AC_SRC_ALPHA;
2073 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2074 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, bf);
2076 else
2078 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2079 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, SRCCOPY);
2082 if (temp_hdc)
2084 SelectObject(hdc, old_hbm);
2085 DeleteDC(hdc);
2088 if (temp_bitmap)
2089 DeleteObject(hbitmap);
2092 else
2094 ERR("GpImage with no IPicture or HBITMAP?!\n");
2095 return NotImplemented;
2098 return Ok;
2101 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
2102 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
2103 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2104 DrawImageAbort callback, VOID * callbackData)
2106 GpPointF pointsF[3];
2107 INT i;
2109 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
2110 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2111 callbackData);
2113 if(!points || count!=3)
2114 return InvalidParameter;
2116 for(i = 0; i < count; i++){
2117 pointsF[i].X = (REAL)points[i].X;
2118 pointsF[i].Y = (REAL)points[i].Y;
2121 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2122 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2123 callback, callbackData);
2126 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2127 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2128 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2129 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2130 VOID * callbackData)
2132 GpPointF points[3];
2134 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2135 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2136 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2138 points[0].X = dstx;
2139 points[0].Y = dsty;
2140 points[1].X = dstx + dstwidth;
2141 points[1].Y = dsty;
2142 points[2].X = dstx;
2143 points[2].Y = dsty + dstheight;
2145 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2146 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2149 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2150 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2151 INT srcwidth, INT srcheight, GpUnit srcUnit,
2152 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2153 VOID * callbackData)
2155 GpPointF points[3];
2157 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2158 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2159 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2161 points[0].X = dstx;
2162 points[0].Y = dsty;
2163 points[1].X = dstx + dstwidth;
2164 points[1].Y = dsty;
2165 points[2].X = dstx;
2166 points[2].Y = dsty + dstheight;
2168 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2169 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2172 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2173 REAL x, REAL y, REAL width, REAL height)
2175 RectF bounds;
2176 GpUnit unit;
2177 GpStatus ret;
2179 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2181 if(!graphics || !image)
2182 return InvalidParameter;
2184 ret = GdipGetImageBounds(image, &bounds, &unit);
2185 if(ret != Ok)
2186 return ret;
2188 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2189 bounds.X, bounds.Y, bounds.Width, bounds.Height,
2190 unit, NULL, NULL, NULL);
2193 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2194 INT x, INT y, INT width, INT height)
2196 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2198 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2201 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2202 REAL y1, REAL x2, REAL y2)
2204 INT save_state;
2205 GpPointF pt[2];
2206 GpStatus retval;
2208 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2210 if(!pen || !graphics)
2211 return InvalidParameter;
2213 if(graphics->busy)
2214 return ObjectBusy;
2216 pt[0].X = x1;
2217 pt[0].Y = y1;
2218 pt[1].X = x2;
2219 pt[1].Y = y2;
2221 save_state = prepare_dc(graphics, pen);
2223 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2225 restore_dc(graphics, save_state);
2227 return retval;
2230 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2231 INT y1, INT x2, INT y2)
2233 INT save_state;
2234 GpPointF pt[2];
2235 GpStatus retval;
2237 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2239 if(!pen || !graphics)
2240 return InvalidParameter;
2242 if(graphics->busy)
2243 return ObjectBusy;
2245 pt[0].X = (REAL)x1;
2246 pt[0].Y = (REAL)y1;
2247 pt[1].X = (REAL)x2;
2248 pt[1].Y = (REAL)y2;
2250 save_state = prepare_dc(graphics, pen);
2252 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2254 restore_dc(graphics, save_state);
2256 return retval;
2259 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2260 GpPointF *points, INT count)
2262 INT save_state;
2263 GpStatus retval;
2265 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2267 if(!pen || !graphics || (count < 2))
2268 return InvalidParameter;
2270 if(graphics->busy)
2271 return ObjectBusy;
2273 save_state = prepare_dc(graphics, pen);
2275 retval = draw_polyline(graphics, pen, points, count, TRUE);
2277 restore_dc(graphics, save_state);
2279 return retval;
2282 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2283 GpPoint *points, INT count)
2285 INT save_state;
2286 GpStatus retval;
2287 GpPointF *ptf = NULL;
2288 int i;
2290 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2292 if(!pen || !graphics || (count < 2))
2293 return InvalidParameter;
2295 if(graphics->busy)
2296 return ObjectBusy;
2298 ptf = GdipAlloc(count * sizeof(GpPointF));
2299 if(!ptf) return OutOfMemory;
2301 for(i = 0; i < count; i ++){
2302 ptf[i].X = (REAL) points[i].X;
2303 ptf[i].Y = (REAL) points[i].Y;
2306 save_state = prepare_dc(graphics, pen);
2308 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2310 restore_dc(graphics, save_state);
2312 GdipFree(ptf);
2313 return retval;
2316 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2318 INT save_state;
2319 GpStatus retval;
2321 TRACE("(%p, %p, %p)\n", graphics, pen, path);
2323 if(!pen || !graphics)
2324 return InvalidParameter;
2326 if(graphics->busy)
2327 return ObjectBusy;
2329 save_state = prepare_dc(graphics, pen);
2331 retval = draw_poly(graphics, pen, path->pathdata.Points,
2332 path->pathdata.Types, path->pathdata.Count, TRUE);
2334 restore_dc(graphics, save_state);
2336 return retval;
2339 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2340 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2342 INT save_state;
2344 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2345 width, height, startAngle, sweepAngle);
2347 if(!graphics || !pen)
2348 return InvalidParameter;
2350 if(graphics->busy)
2351 return ObjectBusy;
2353 save_state = prepare_dc(graphics, pen);
2354 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2356 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2358 restore_dc(graphics, save_state);
2360 return Ok;
2363 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2364 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2366 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2367 width, height, startAngle, sweepAngle);
2369 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2372 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2373 REAL y, REAL width, REAL height)
2375 INT save_state;
2376 GpPointF ptf[4];
2377 POINT pti[4];
2379 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2381 if(!pen || !graphics)
2382 return InvalidParameter;
2384 if(graphics->busy)
2385 return ObjectBusy;
2387 ptf[0].X = x;
2388 ptf[0].Y = y;
2389 ptf[1].X = x + width;
2390 ptf[1].Y = y;
2391 ptf[2].X = x + width;
2392 ptf[2].Y = y + height;
2393 ptf[3].X = x;
2394 ptf[3].Y = y + height;
2396 save_state = prepare_dc(graphics, pen);
2397 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2399 transform_and_round_points(graphics, pti, ptf, 4);
2400 Polygon(graphics->hdc, pti, 4);
2402 restore_dc(graphics, save_state);
2404 return Ok;
2407 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2408 INT y, INT width, INT height)
2410 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2412 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2415 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2416 GDIPCONST GpRectF* rects, INT count)
2418 GpPointF *ptf;
2419 POINT *pti;
2420 INT save_state, i;
2422 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2424 if(!graphics || !pen || !rects || count < 1)
2425 return InvalidParameter;
2427 if(graphics->busy)
2428 return ObjectBusy;
2430 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2431 pti = GdipAlloc(4 * count * sizeof(POINT));
2433 if(!ptf || !pti){
2434 GdipFree(ptf);
2435 GdipFree(pti);
2436 return OutOfMemory;
2439 for(i = 0; i < count; i++){
2440 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2441 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2442 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2443 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2446 save_state = prepare_dc(graphics, pen);
2447 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2449 transform_and_round_points(graphics, pti, ptf, 4 * count);
2451 for(i = 0; i < count; i++)
2452 Polygon(graphics->hdc, &pti[4 * i], 4);
2454 restore_dc(graphics, save_state);
2456 GdipFree(ptf);
2457 GdipFree(pti);
2459 return Ok;
2462 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2463 GDIPCONST GpRect* rects, INT count)
2465 GpRectF *rectsF;
2466 GpStatus ret;
2467 INT i;
2469 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2471 if(!rects || count<=0)
2472 return InvalidParameter;
2474 rectsF = GdipAlloc(sizeof(GpRectF) * count);
2475 if(!rectsF)
2476 return OutOfMemory;
2478 for(i = 0;i < count;i++){
2479 rectsF[i].X = (REAL)rects[i].X;
2480 rectsF[i].Y = (REAL)rects[i].Y;
2481 rectsF[i].Width = (REAL)rects[i].Width;
2482 rectsF[i].Height = (REAL)rects[i].Height;
2485 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2486 GdipFree(rectsF);
2488 return ret;
2491 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2492 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2494 GpPath *path;
2495 GpStatus stat;
2497 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2498 count, tension, fill);
2500 if(!graphics || !brush || !points)
2501 return InvalidParameter;
2503 if(graphics->busy)
2504 return ObjectBusy;
2506 stat = GdipCreatePath(fill, &path);
2507 if(stat != Ok)
2508 return stat;
2510 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2511 if(stat != Ok){
2512 GdipDeletePath(path);
2513 return stat;
2516 stat = GdipFillPath(graphics, brush, path);
2517 if(stat != Ok){
2518 GdipDeletePath(path);
2519 return stat;
2522 GdipDeletePath(path);
2524 return Ok;
2527 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2528 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2530 GpPointF *ptf;
2531 GpStatus stat;
2532 INT i;
2534 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2535 count, tension, fill);
2537 if(!points || count <= 0)
2538 return InvalidParameter;
2540 ptf = GdipAlloc(sizeof(GpPointF)*count);
2541 if(!ptf)
2542 return OutOfMemory;
2544 for(i = 0;i < count;i++){
2545 ptf[i].X = (REAL)points[i].X;
2546 ptf[i].Y = (REAL)points[i].Y;
2549 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2551 GdipFree(ptf);
2553 return stat;
2556 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2557 REAL y, REAL width, REAL height)
2559 INT save_state;
2560 GpPointF ptf[2];
2561 POINT pti[2];
2563 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2565 if(!graphics || !brush)
2566 return InvalidParameter;
2568 if(graphics->busy)
2569 return ObjectBusy;
2571 ptf[0].X = x;
2572 ptf[0].Y = y;
2573 ptf[1].X = x + width;
2574 ptf[1].Y = y + height;
2576 save_state = SaveDC(graphics->hdc);
2577 EndPath(graphics->hdc);
2579 transform_and_round_points(graphics, pti, ptf, 2);
2581 BeginPath(graphics->hdc);
2582 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2583 EndPath(graphics->hdc);
2585 brush_fill_path(graphics, brush);
2587 RestoreDC(graphics->hdc, save_state);
2589 return Ok;
2592 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2593 INT y, INT width, INT height)
2595 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2597 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2600 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2602 INT save_state;
2603 GpStatus retval;
2605 TRACE("(%p, %p, %p)\n", graphics, brush, path);
2607 if(!brush || !graphics || !path)
2608 return InvalidParameter;
2610 if(graphics->busy)
2611 return ObjectBusy;
2613 save_state = SaveDC(graphics->hdc);
2614 EndPath(graphics->hdc);
2615 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2616 : WINDING));
2618 BeginPath(graphics->hdc);
2619 retval = draw_poly(graphics, NULL, path->pathdata.Points,
2620 path->pathdata.Types, path->pathdata.Count, FALSE);
2622 if(retval != Ok)
2623 goto end;
2625 EndPath(graphics->hdc);
2626 brush_fill_path(graphics, brush);
2628 retval = Ok;
2630 end:
2631 RestoreDC(graphics->hdc, save_state);
2633 return retval;
2636 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2637 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2639 INT save_state;
2641 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2642 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2644 if(!graphics || !brush)
2645 return InvalidParameter;
2647 if(graphics->busy)
2648 return ObjectBusy;
2650 save_state = SaveDC(graphics->hdc);
2651 EndPath(graphics->hdc);
2653 BeginPath(graphics->hdc);
2654 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2655 EndPath(graphics->hdc);
2657 brush_fill_path(graphics, brush);
2659 RestoreDC(graphics->hdc, save_state);
2661 return Ok;
2664 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2665 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2667 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2668 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2670 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2673 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2674 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2676 INT save_state;
2677 GpPointF *ptf = NULL;
2678 POINT *pti = NULL;
2679 GpStatus retval = Ok;
2681 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2683 if(!graphics || !brush || !points || !count)
2684 return InvalidParameter;
2686 if(graphics->busy)
2687 return ObjectBusy;
2689 ptf = GdipAlloc(count * sizeof(GpPointF));
2690 pti = GdipAlloc(count * sizeof(POINT));
2691 if(!ptf || !pti){
2692 retval = OutOfMemory;
2693 goto end;
2696 memcpy(ptf, points, count * sizeof(GpPointF));
2698 save_state = SaveDC(graphics->hdc);
2699 EndPath(graphics->hdc);
2700 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2701 : WINDING));
2703 transform_and_round_points(graphics, pti, ptf, count);
2705 BeginPath(graphics->hdc);
2706 Polygon(graphics->hdc, pti, count);
2707 EndPath(graphics->hdc);
2709 brush_fill_path(graphics, brush);
2711 RestoreDC(graphics->hdc, save_state);
2713 end:
2714 GdipFree(ptf);
2715 GdipFree(pti);
2717 return retval;
2720 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2721 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2723 INT save_state, i;
2724 GpPointF *ptf = NULL;
2725 POINT *pti = NULL;
2726 GpStatus retval = Ok;
2728 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2730 if(!graphics || !brush || !points || !count)
2731 return InvalidParameter;
2733 if(graphics->busy)
2734 return ObjectBusy;
2736 ptf = GdipAlloc(count * sizeof(GpPointF));
2737 pti = GdipAlloc(count * sizeof(POINT));
2738 if(!ptf || !pti){
2739 retval = OutOfMemory;
2740 goto end;
2743 for(i = 0; i < count; i ++){
2744 ptf[i].X = (REAL) points[i].X;
2745 ptf[i].Y = (REAL) points[i].Y;
2748 save_state = SaveDC(graphics->hdc);
2749 EndPath(graphics->hdc);
2750 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2751 : WINDING));
2753 transform_and_round_points(graphics, pti, ptf, count);
2755 BeginPath(graphics->hdc);
2756 Polygon(graphics->hdc, pti, count);
2757 EndPath(graphics->hdc);
2759 brush_fill_path(graphics, brush);
2761 RestoreDC(graphics->hdc, save_state);
2763 end:
2764 GdipFree(ptf);
2765 GdipFree(pti);
2767 return retval;
2770 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2771 GDIPCONST GpPointF *points, INT count)
2773 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2775 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2778 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2779 GDIPCONST GpPoint *points, INT count)
2781 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2783 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2786 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2787 REAL x, REAL y, REAL width, REAL height)
2789 INT save_state;
2790 GpPointF ptf[4];
2791 POINT pti[4];
2793 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2795 if(!graphics || !brush)
2796 return InvalidParameter;
2798 if(graphics->busy)
2799 return ObjectBusy;
2801 ptf[0].X = x;
2802 ptf[0].Y = y;
2803 ptf[1].X = x + width;
2804 ptf[1].Y = y;
2805 ptf[2].X = x + width;
2806 ptf[2].Y = y + height;
2807 ptf[3].X = x;
2808 ptf[3].Y = y + height;
2810 save_state = SaveDC(graphics->hdc);
2811 EndPath(graphics->hdc);
2813 transform_and_round_points(graphics, pti, ptf, 4);
2815 BeginPath(graphics->hdc);
2816 Polygon(graphics->hdc, pti, 4);
2817 EndPath(graphics->hdc);
2819 brush_fill_path(graphics, brush);
2821 RestoreDC(graphics->hdc, save_state);
2823 return Ok;
2826 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2827 INT x, INT y, INT width, INT height)
2829 INT save_state;
2830 GpPointF ptf[4];
2831 POINT pti[4];
2833 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2835 if(!graphics || !brush)
2836 return InvalidParameter;
2838 if(graphics->busy)
2839 return ObjectBusy;
2841 ptf[0].X = x;
2842 ptf[0].Y = y;
2843 ptf[1].X = x + width;
2844 ptf[1].Y = y;
2845 ptf[2].X = x + width;
2846 ptf[2].Y = y + height;
2847 ptf[3].X = x;
2848 ptf[3].Y = y + height;
2850 save_state = SaveDC(graphics->hdc);
2851 EndPath(graphics->hdc);
2853 transform_and_round_points(graphics, pti, ptf, 4);
2855 BeginPath(graphics->hdc);
2856 Polygon(graphics->hdc, pti, 4);
2857 EndPath(graphics->hdc);
2859 brush_fill_path(graphics, brush);
2861 RestoreDC(graphics->hdc, save_state);
2863 return Ok;
2866 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2867 INT count)
2869 GpStatus ret;
2870 INT i;
2872 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2874 if(!rects)
2875 return InvalidParameter;
2877 for(i = 0; i < count; i++){
2878 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2879 if(ret != Ok) return ret;
2882 return Ok;
2885 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2886 INT count)
2888 GpRectF *rectsF;
2889 GpStatus ret;
2890 INT i;
2892 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2894 if(!rects || count <= 0)
2895 return InvalidParameter;
2897 rectsF = GdipAlloc(sizeof(GpRectF)*count);
2898 if(!rectsF)
2899 return OutOfMemory;
2901 for(i = 0; i < count; i++){
2902 rectsF[i].X = (REAL)rects[i].X;
2903 rectsF[i].Y = (REAL)rects[i].Y;
2904 rectsF[i].X = (REAL)rects[i].Width;
2905 rectsF[i].Height = (REAL)rects[i].Height;
2908 ret = GdipFillRectangles(graphics,brush,rectsF,count);
2909 GdipFree(rectsF);
2911 return ret;
2914 /*****************************************************************************
2915 * GdipFillRegion [GDIPLUS.@]
2917 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2918 GpRegion* region)
2920 INT save_state;
2921 GpStatus status;
2922 HRGN hrgn;
2923 RECT rc;
2925 TRACE("(%p, %p, %p)\n", graphics, brush, region);
2927 if (!(graphics && brush && region))
2928 return InvalidParameter;
2930 if(graphics->busy)
2931 return ObjectBusy;
2933 status = GdipGetRegionHRgn(region, graphics, &hrgn);
2934 if(status != Ok)
2935 return status;
2937 save_state = SaveDC(graphics->hdc);
2938 EndPath(graphics->hdc);
2940 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
2942 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
2944 BeginPath(graphics->hdc);
2945 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
2946 EndPath(graphics->hdc);
2948 brush_fill_path(graphics, brush);
2951 RestoreDC(graphics->hdc, save_state);
2953 DeleteObject(hrgn);
2955 return Ok;
2958 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2960 TRACE("(%p,%u)\n", graphics, intention);
2962 if(!graphics)
2963 return InvalidParameter;
2965 if(graphics->busy)
2966 return ObjectBusy;
2968 /* We have no internal operation queue, so there's no need to clear it. */
2970 if (graphics->hdc)
2971 GdiFlush();
2973 return Ok;
2976 /*****************************************************************************
2977 * GdipGetClipBounds [GDIPLUS.@]
2979 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
2981 TRACE("(%p, %p)\n", graphics, rect);
2983 if(!graphics)
2984 return InvalidParameter;
2986 if(graphics->busy)
2987 return ObjectBusy;
2989 return GdipGetRegionBounds(graphics->clip, graphics, rect);
2992 /*****************************************************************************
2993 * GdipGetClipBoundsI [GDIPLUS.@]
2995 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
2997 TRACE("(%p, %p)\n", graphics, rect);
2999 if(!graphics)
3000 return InvalidParameter;
3002 if(graphics->busy)
3003 return ObjectBusy;
3005 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3008 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3009 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3010 CompositingMode *mode)
3012 TRACE("(%p, %p)\n", graphics, mode);
3014 if(!graphics || !mode)
3015 return InvalidParameter;
3017 if(graphics->busy)
3018 return ObjectBusy;
3020 *mode = graphics->compmode;
3022 return Ok;
3025 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3026 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3027 CompositingQuality *quality)
3029 TRACE("(%p, %p)\n", graphics, quality);
3031 if(!graphics || !quality)
3032 return InvalidParameter;
3034 if(graphics->busy)
3035 return ObjectBusy;
3037 *quality = graphics->compqual;
3039 return Ok;
3042 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3043 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3044 InterpolationMode *mode)
3046 TRACE("(%p, %p)\n", graphics, mode);
3048 if(!graphics || !mode)
3049 return InvalidParameter;
3051 if(graphics->busy)
3052 return ObjectBusy;
3054 *mode = graphics->interpolation;
3056 return Ok;
3059 /* FIXME: Need to handle color depths less than 24bpp */
3060 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3062 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
3064 if(!graphics || !argb)
3065 return InvalidParameter;
3067 if(graphics->busy)
3068 return ObjectBusy;
3070 return Ok;
3073 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3075 TRACE("(%p, %p)\n", graphics, scale);
3077 if(!graphics || !scale)
3078 return InvalidParameter;
3080 if(graphics->busy)
3081 return ObjectBusy;
3083 *scale = graphics->scale;
3085 return Ok;
3088 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3090 TRACE("(%p, %p)\n", graphics, unit);
3092 if(!graphics || !unit)
3093 return InvalidParameter;
3095 if(graphics->busy)
3096 return ObjectBusy;
3098 *unit = graphics->unit;
3100 return Ok;
3103 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3104 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3105 *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->pixeloffset;
3117 return Ok;
3120 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3121 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3123 TRACE("(%p, %p)\n", graphics, mode);
3125 if(!graphics || !mode)
3126 return InvalidParameter;
3128 if(graphics->busy)
3129 return ObjectBusy;
3131 *mode = graphics->smoothing;
3133 return Ok;
3136 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3138 TRACE("(%p, %p)\n", graphics, contrast);
3140 if(!graphics || !contrast)
3141 return InvalidParameter;
3143 *contrast = graphics->textcontrast;
3145 return Ok;
3148 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3149 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3150 TextRenderingHint *hint)
3152 TRACE("(%p, %p)\n", graphics, hint);
3154 if(!graphics || !hint)
3155 return InvalidParameter;
3157 if(graphics->busy)
3158 return ObjectBusy;
3160 *hint = graphics->texthint;
3162 return Ok;
3165 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3167 GpRegion *clip_rgn;
3168 GpStatus stat;
3170 TRACE("(%p, %p)\n", graphics, rect);
3172 if(!graphics || !rect)
3173 return InvalidParameter;
3175 if(graphics->busy)
3176 return ObjectBusy;
3178 /* intersect window and graphics clipping regions */
3179 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3180 return stat;
3182 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3183 goto cleanup;
3185 /* get bounds of the region */
3186 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3188 cleanup:
3189 GdipDeleteRegion(clip_rgn);
3191 return stat;
3194 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3196 GpRectF rectf;
3197 GpStatus stat;
3199 TRACE("(%p, %p)\n", graphics, rect);
3201 if(!graphics || !rect)
3202 return InvalidParameter;
3204 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
3206 rect->X = roundr(rectf.X);
3207 rect->Y = roundr(rectf.Y);
3208 rect->Width = roundr(rectf.Width);
3209 rect->Height = roundr(rectf.Height);
3212 return stat;
3215 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3217 TRACE("(%p, %p)\n", graphics, matrix);
3219 if(!graphics || !matrix)
3220 return InvalidParameter;
3222 if(graphics->busy)
3223 return ObjectBusy;
3225 *matrix = *graphics->worldtrans;
3226 return Ok;
3229 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3231 GpSolidFill *brush;
3232 GpStatus stat;
3233 GpRectF wnd_rect;
3235 TRACE("(%p, %x)\n", graphics, color);
3237 if(!graphics)
3238 return InvalidParameter;
3240 if(graphics->busy)
3241 return ObjectBusy;
3243 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3244 return stat;
3246 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
3247 GdipDeleteBrush((GpBrush*)brush);
3248 return stat;
3251 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
3252 wnd_rect.Width, wnd_rect.Height);
3254 GdipDeleteBrush((GpBrush*)brush);
3256 return Ok;
3259 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3261 TRACE("(%p, %p)\n", graphics, res);
3263 if(!graphics || !res)
3264 return InvalidParameter;
3266 return GdipIsEmptyRegion(graphics->clip, graphics, res);
3269 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3271 GpStatus stat;
3272 GpRegion* rgn;
3273 GpPointF pt;
3275 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
3277 if(!graphics || !result)
3278 return InvalidParameter;
3280 if(graphics->busy)
3281 return ObjectBusy;
3283 pt.X = x;
3284 pt.Y = y;
3285 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3286 CoordinateSpaceWorld, &pt, 1)) != Ok)
3287 return stat;
3289 if((stat = GdipCreateRegion(&rgn)) != Ok)
3290 return stat;
3292 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3293 goto cleanup;
3295 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
3297 cleanup:
3298 GdipDeleteRegion(rgn);
3299 return stat;
3302 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3304 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
3307 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
3309 GpStatus stat;
3310 GpRegion* rgn;
3311 GpPointF pts[2];
3313 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
3315 if(!graphics || !result)
3316 return InvalidParameter;
3318 if(graphics->busy)
3319 return ObjectBusy;
3321 pts[0].X = x;
3322 pts[0].Y = y;
3323 pts[1].X = x + width;
3324 pts[1].Y = y + height;
3326 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3327 CoordinateSpaceWorld, pts, 2)) != Ok)
3328 return stat;
3330 pts[1].X -= pts[0].X;
3331 pts[1].Y -= pts[0].Y;
3333 if((stat = GdipCreateRegion(&rgn)) != Ok)
3334 return stat;
3336 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3337 goto cleanup;
3339 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
3341 cleanup:
3342 GdipDeleteRegion(rgn);
3343 return stat;
3346 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
3348 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
3351 typedef GpStatus (*gdip_format_string_callback)(GpGraphics *graphics,
3352 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3353 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3354 INT lineno, const RectF *bounds, void *user_data);
3356 static GpStatus gdip_format_string(GpGraphics *graphics,
3357 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3358 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3359 gdip_format_string_callback callback, void *user_data)
3361 WCHAR* stringdup;
3362 INT sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
3363 nheight, lineend, lineno = 0;
3364 RectF bounds;
3365 StringAlignment halign;
3366 GpStatus stat = Ok;
3367 SIZE size;
3369 if(length == -1) length = lstrlenW(string);
3371 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3372 if(!stringdup) return OutOfMemory;
3374 nwidth = roundr(rect->Width);
3375 nheight = roundr(rect->Height);
3377 if (nwidth == 0) nwidth = INT_MAX;
3378 if (nheight == 0) nheight = INT_MAX;
3380 for(i = 0, j = 0; i < length; i++){
3381 /* FIXME: This makes the indexes passed to callback inaccurate. */
3382 if(!isprintW(string[i]) && (string[i] != '\n'))
3383 continue;
3385 stringdup[j] = string[i];
3386 j++;
3389 length = j;
3391 if (format) halign = format->align;
3392 else halign = StringAlignmentNear;
3394 while(sum < length){
3395 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3396 nwidth, &fit, NULL, &size);
3397 fitcpy = fit;
3399 if(fit == 0)
3400 break;
3402 for(lret = 0; lret < fit; lret++)
3403 if(*(stringdup + sum + lret) == '\n')
3404 break;
3406 /* Line break code (may look strange, but it imitates windows). */
3407 if(lret < fit)
3408 lineend = fit = lret; /* this is not an off-by-one error */
3409 else if(fit < (length - sum)){
3410 if(*(stringdup + sum + fit) == ' ')
3411 while(*(stringdup + sum + fit) == ' ')
3412 fit++;
3413 else
3414 while(*(stringdup + sum + fit - 1) != ' '){
3415 fit--;
3417 if(*(stringdup + sum + fit) == '\t')
3418 break;
3420 if(fit == 0){
3421 fit = fitcpy;
3422 break;
3425 lineend = fit;
3426 while(*(stringdup + sum + lineend - 1) == ' ' ||
3427 *(stringdup + sum + lineend - 1) == '\t')
3428 lineend--;
3430 else
3431 lineend = fit;
3433 GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3434 nwidth, &j, NULL, &size);
3436 bounds.Width = size.cx;
3438 if(height + size.cy > nheight)
3439 bounds.Height = nheight - (height + size.cy);
3440 else
3441 bounds.Height = size.cy;
3443 bounds.Y = rect->Y + height;
3445 switch (halign)
3447 case StringAlignmentNear:
3448 default:
3449 bounds.X = rect->X;
3450 break;
3451 case StringAlignmentCenter:
3452 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
3453 break;
3454 case StringAlignmentFar:
3455 bounds.X = rect->X + rect->Width - bounds.Width;
3456 break;
3459 stat = callback(graphics, stringdup, sum, lineend,
3460 font, rect, format, lineno, &bounds, user_data);
3462 if (stat != Ok)
3463 break;
3465 sum += fit + (lret < fitcpy ? 1 : 0);
3466 height += size.cy;
3467 lineno++;
3469 if(height > nheight)
3470 break;
3472 /* Stop if this was a linewrap (but not if it was a linebreak). */
3473 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3474 break;
3477 GdipFree(stringdup);
3479 return stat;
3482 struct measure_ranges_args {
3483 GpRegion **regions;
3486 GpStatus measure_ranges_callback(GpGraphics *graphics,
3487 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3488 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3489 INT lineno, const RectF *bounds, void *user_data)
3491 int i;
3492 GpStatus stat = Ok;
3493 struct measure_ranges_args *args = user_data;
3495 for (i=0; i<format->range_count; i++)
3497 INT range_start = max(index, format->character_ranges[i].First);
3498 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
3499 if (range_start < range_end)
3501 GpRectF range_rect;
3502 SIZE range_size;
3504 range_rect.Y = bounds->Y;
3505 range_rect.Height = bounds->Height;
3507 GetTextExtentExPointW(graphics->hdc, string + index, range_start - index,
3508 INT_MAX, NULL, NULL, &range_size);
3509 range_rect.X = bounds->X + range_size.cx;
3511 GetTextExtentExPointW(graphics->hdc, string + index, range_end - index,
3512 INT_MAX, NULL, NULL, &range_size);
3513 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
3515 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
3516 if (stat != Ok)
3517 break;
3521 return stat;
3524 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3525 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3526 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3527 INT regionCount, GpRegion** regions)
3529 GpStatus stat;
3530 int i;
3531 HFONT oldfont;
3532 struct measure_ranges_args args;
3534 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
3535 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
3537 if (!(graphics && string && font && layoutRect && stringFormat && regions))
3538 return InvalidParameter;
3540 if (regionCount < stringFormat->range_count)
3541 return InvalidParameter;
3543 if (stringFormat->attr)
3544 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
3546 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3548 for (i=0; i<stringFormat->range_count; i++)
3550 stat = GdipSetEmpty(regions[i]);
3551 if (stat != Ok)
3552 return stat;
3555 args.regions = regions;
3557 stat = gdip_format_string(graphics, string, length, font, layoutRect, stringFormat,
3558 measure_ranges_callback, &args);
3560 DeleteObject(SelectObject(graphics->hdc, oldfont));
3562 return stat;
3565 struct measure_string_args {
3566 RectF *bounds;
3567 INT *codepointsfitted;
3568 INT *linesfilled;
3571 static GpStatus measure_string_callback(GpGraphics *graphics,
3572 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3573 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3574 INT lineno, const RectF *bounds, void *user_data)
3576 struct measure_string_args *args = user_data;
3578 if (bounds->Width > args->bounds->Width)
3579 args->bounds->Width = bounds->Width;
3581 if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
3582 args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
3584 if (args->codepointsfitted)
3585 *args->codepointsfitted = index + length;
3587 if (args->linesfilled)
3588 (*args->linesfilled)++;
3590 return Ok;
3593 /* Find the smallest rectangle that bounds the text when it is printed in rect
3594 * according to the format options listed in format. If rect has 0 width and
3595 * height, then just find the smallest rectangle that bounds the text when it's
3596 * printed at location (rect->X, rect-Y). */
3597 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3598 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3599 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3600 INT *codepointsfitted, INT *linesfilled)
3602 HFONT oldfont;
3603 struct measure_string_args args;
3605 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3606 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3607 bounds, codepointsfitted, linesfilled);
3609 if(!graphics || !string || !font || !rect || !bounds)
3610 return InvalidParameter;
3612 if(linesfilled) *linesfilled = 0;
3613 if(codepointsfitted) *codepointsfitted = 0;
3615 if(format)
3616 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3618 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3620 bounds->X = rect->X;
3621 bounds->Y = rect->Y;
3622 bounds->Width = 0.0;
3623 bounds->Height = 0.0;
3625 args.bounds = bounds;
3626 args.codepointsfitted = codepointsfitted;
3627 args.linesfilled = linesfilled;
3629 gdip_format_string(graphics, string, length, font, rect, format,
3630 measure_string_callback, &args);
3632 DeleteObject(SelectObject(graphics->hdc, oldfont));
3634 return Ok;
3637 struct draw_string_args {
3638 POINT drawbase;
3639 UINT drawflags;
3640 REAL ang_cos, ang_sin;
3643 static GpStatus draw_string_callback(GpGraphics *graphics,
3644 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3645 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3646 INT lineno, const RectF *bounds, void *user_data)
3648 struct draw_string_args *args = user_data;
3649 RECT drawcoord;
3651 drawcoord.left = drawcoord.right = args->drawbase.x + roundr(args->ang_sin * bounds->Y);
3652 drawcoord.top = drawcoord.bottom = args->drawbase.y + roundr(args->ang_cos * bounds->Y);
3654 DrawTextW(graphics->hdc, string + index, length, &drawcoord, args->drawflags);
3656 return Ok;
3659 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
3660 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
3661 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
3663 HRGN rgn = NULL;
3664 HFONT gdifont;
3665 LOGFONTW lfw;
3666 TEXTMETRICW textmet;
3667 GpPointF pt[3], rectcpy[4];
3668 POINT corners[4];
3669 REAL angle, rel_width, rel_height;
3670 INT offsety = 0, save_state;
3671 struct draw_string_args args;
3672 RectF scaled_rect;
3674 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
3675 length, font, debugstr_rectf(rect), format, brush);
3677 if(!graphics || !string || !font || !brush || !rect)
3678 return InvalidParameter;
3680 if((brush->bt != BrushTypeSolidColor)){
3681 FIXME("not implemented for given parameters\n");
3682 return NotImplemented;
3685 if(format){
3686 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3688 /* Should be no need to explicitly test for StringAlignmentNear as
3689 * that is default behavior if no alignment is passed. */
3690 if(format->vertalign != StringAlignmentNear){
3691 RectF bounds;
3692 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
3694 if(format->vertalign == StringAlignmentCenter)
3695 offsety = (rect->Height - bounds.Height) / 2;
3696 else if(format->vertalign == StringAlignmentFar)
3697 offsety = (rect->Height - bounds.Height);
3701 save_state = SaveDC(graphics->hdc);
3702 SetBkMode(graphics->hdc, TRANSPARENT);
3703 SetTextColor(graphics->hdc, brush->lb.lbColor);
3705 pt[0].X = 0.0;
3706 pt[0].Y = 0.0;
3707 pt[1].X = 1.0;
3708 pt[1].Y = 0.0;
3709 pt[2].X = 0.0;
3710 pt[2].Y = 1.0;
3711 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
3712 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
3713 args.ang_cos = cos(angle);
3714 args.ang_sin = sin(angle);
3715 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
3716 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
3717 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
3718 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
3720 rectcpy[3].X = rectcpy[0].X = rect->X;
3721 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
3722 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
3723 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
3724 transform_and_round_points(graphics, corners, rectcpy, 4);
3726 scaled_rect.X = 0.0;
3727 scaled_rect.Y = 0.0;
3728 scaled_rect.Width = rel_width * rect->Width;
3729 scaled_rect.Height = rel_height * rect->Height;
3731 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
3733 /* FIXME: If only the width or only the height is 0, we should probably still clip */
3734 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
3735 SelectClipRgn(graphics->hdc, rgn);
3738 /* Use gdi to find the font, then perform transformations on it (height,
3739 * width, angle). */
3740 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3741 GetTextMetricsW(graphics->hdc, &textmet);
3742 lfw = font->lfw;
3744 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
3745 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
3747 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
3749 gdifont = CreateFontIndirectW(&lfw);
3750 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
3752 if (!format || format->align == StringAlignmentNear)
3754 args.drawbase.x = corners[0].x;
3755 args.drawbase.y = corners[0].y;
3756 args.drawflags = DT_NOCLIP | DT_EXPANDTABS;
3758 else if (format->align == StringAlignmentCenter)
3760 args.drawbase.x = (corners[0].x + corners[1].x)/2;
3761 args.drawbase.y = (corners[0].y + corners[1].y)/2;
3762 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
3764 else /* (format->align == StringAlignmentFar) */
3766 args.drawbase.x = corners[1].x;
3767 args.drawbase.y = corners[1].y;
3768 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
3771 gdip_format_string(graphics, string, length, font, &scaled_rect, format,
3772 draw_string_callback, &args);
3774 DeleteObject(rgn);
3775 DeleteObject(gdifont);
3777 RestoreDC(graphics->hdc, save_state);
3779 return Ok;
3782 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
3784 TRACE("(%p)\n", graphics);
3786 if(!graphics)
3787 return InvalidParameter;
3789 if(graphics->busy)
3790 return ObjectBusy;
3792 return GdipSetInfinite(graphics->clip);
3795 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
3797 TRACE("(%p)\n", graphics);
3799 if(!graphics)
3800 return InvalidParameter;
3802 if(graphics->busy)
3803 return ObjectBusy;
3805 graphics->worldtrans->matrix[0] = 1.0;
3806 graphics->worldtrans->matrix[1] = 0.0;
3807 graphics->worldtrans->matrix[2] = 0.0;
3808 graphics->worldtrans->matrix[3] = 1.0;
3809 graphics->worldtrans->matrix[4] = 0.0;
3810 graphics->worldtrans->matrix[5] = 0.0;
3812 return Ok;
3815 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
3817 return GdipEndContainer(graphics, state);
3820 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
3821 GpMatrixOrder order)
3823 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
3825 if(!graphics)
3826 return InvalidParameter;
3828 if(graphics->busy)
3829 return ObjectBusy;
3831 return GdipRotateMatrix(graphics->worldtrans, angle, order);
3834 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
3836 return GdipBeginContainer2(graphics, state);
3839 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
3840 GraphicsContainer *state)
3842 GraphicsContainerItem *container;
3843 GpStatus sts;
3845 TRACE("(%p, %p)\n", graphics, state);
3847 if(!graphics || !state)
3848 return InvalidParameter;
3850 sts = init_container(&container, graphics);
3851 if(sts != Ok)
3852 return sts;
3854 list_add_head(&graphics->containers, &container->entry);
3855 *state = graphics->contid = container->contid;
3857 return Ok;
3860 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
3862 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3863 return NotImplemented;
3866 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
3868 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3869 return NotImplemented;
3872 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
3874 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
3875 return NotImplemented;
3878 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
3880 GpStatus sts;
3881 GraphicsContainerItem *container, *container2;
3883 TRACE("(%p, %x)\n", graphics, state);
3885 if(!graphics)
3886 return InvalidParameter;
3888 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
3889 if(container->contid == state)
3890 break;
3893 /* did not find a matching container */
3894 if(&container->entry == &graphics->containers)
3895 return Ok;
3897 sts = restore_container(graphics, container);
3898 if(sts != Ok)
3899 return sts;
3901 /* remove all of the containers on top of the found container */
3902 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
3903 if(container->contid == state)
3904 break;
3905 list_remove(&container->entry);
3906 delete_container(container);
3909 list_remove(&container->entry);
3910 delete_container(container);
3912 return Ok;
3915 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
3916 REAL sy, GpMatrixOrder order)
3918 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
3920 if(!graphics)
3921 return InvalidParameter;
3923 if(graphics->busy)
3924 return ObjectBusy;
3926 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
3929 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
3930 CombineMode mode)
3932 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
3934 if(!graphics || !srcgraphics)
3935 return InvalidParameter;
3937 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
3940 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
3941 CompositingMode mode)
3943 TRACE("(%p, %d)\n", graphics, mode);
3945 if(!graphics)
3946 return InvalidParameter;
3948 if(graphics->busy)
3949 return ObjectBusy;
3951 graphics->compmode = mode;
3953 return Ok;
3956 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
3957 CompositingQuality quality)
3959 TRACE("(%p, %d)\n", graphics, quality);
3961 if(!graphics)
3962 return InvalidParameter;
3964 if(graphics->busy)
3965 return ObjectBusy;
3967 graphics->compqual = quality;
3969 return Ok;
3972 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
3973 InterpolationMode mode)
3975 TRACE("(%p, %d)\n", graphics, mode);
3977 if(!graphics)
3978 return InvalidParameter;
3980 if(graphics->busy)
3981 return ObjectBusy;
3983 graphics->interpolation = mode;
3985 return Ok;
3988 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
3990 TRACE("(%p, %.2f)\n", graphics, scale);
3992 if(!graphics || (scale <= 0.0))
3993 return InvalidParameter;
3995 if(graphics->busy)
3996 return ObjectBusy;
3998 graphics->scale = scale;
4000 return Ok;
4003 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4005 TRACE("(%p, %d)\n", graphics, unit);
4007 if(!graphics)
4008 return InvalidParameter;
4010 if(graphics->busy)
4011 return ObjectBusy;
4013 if(unit == UnitWorld)
4014 return InvalidParameter;
4016 graphics->unit = unit;
4018 return Ok;
4021 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4022 mode)
4024 TRACE("(%p, %d)\n", graphics, mode);
4026 if(!graphics)
4027 return InvalidParameter;
4029 if(graphics->busy)
4030 return ObjectBusy;
4032 graphics->pixeloffset = mode;
4034 return Ok;
4037 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
4039 static int calls;
4041 TRACE("(%p,%i,%i)\n", graphics, x, y);
4043 if (!(calls++))
4044 FIXME("not implemented\n");
4046 return NotImplemented;
4049 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
4051 TRACE("(%p, %d)\n", graphics, mode);
4053 if(!graphics)
4054 return InvalidParameter;
4056 if(graphics->busy)
4057 return ObjectBusy;
4059 graphics->smoothing = mode;
4061 return Ok;
4064 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
4066 TRACE("(%p, %d)\n", graphics, contrast);
4068 if(!graphics)
4069 return InvalidParameter;
4071 graphics->textcontrast = contrast;
4073 return Ok;
4076 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
4077 TextRenderingHint hint)
4079 TRACE("(%p, %d)\n", graphics, hint);
4081 if(!graphics)
4082 return InvalidParameter;
4084 if(graphics->busy)
4085 return ObjectBusy;
4087 graphics->texthint = hint;
4089 return Ok;
4092 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4094 TRACE("(%p, %p)\n", graphics, matrix);
4096 if(!graphics || !matrix)
4097 return InvalidParameter;
4099 if(graphics->busy)
4100 return ObjectBusy;
4102 GdipDeleteMatrix(graphics->worldtrans);
4103 return GdipCloneMatrix(matrix, &graphics->worldtrans);
4106 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
4107 REAL dy, GpMatrixOrder order)
4109 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
4111 if(!graphics)
4112 return InvalidParameter;
4114 if(graphics->busy)
4115 return ObjectBusy;
4117 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
4120 /*****************************************************************************
4121 * GdipSetClipHrgn [GDIPLUS.@]
4123 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
4125 GpRegion *region;
4126 GpStatus status;
4128 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
4130 if(!graphics)
4131 return InvalidParameter;
4133 status = GdipCreateRegionHrgn(hrgn, &region);
4134 if(status != Ok)
4135 return status;
4137 status = GdipSetClipRegion(graphics, region, mode);
4139 GdipDeleteRegion(region);
4140 return status;
4143 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
4145 TRACE("(%p, %p, %d)\n", graphics, path, mode);
4147 if(!graphics)
4148 return InvalidParameter;
4150 if(graphics->busy)
4151 return ObjectBusy;
4153 return GdipCombineRegionPath(graphics->clip, path, mode);
4156 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
4157 REAL width, REAL height,
4158 CombineMode mode)
4160 GpRectF rect;
4162 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
4164 if(!graphics)
4165 return InvalidParameter;
4167 if(graphics->busy)
4168 return ObjectBusy;
4170 rect.X = x;
4171 rect.Y = y;
4172 rect.Width = width;
4173 rect.Height = height;
4175 return GdipCombineRegionRect(graphics->clip, &rect, mode);
4178 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
4179 INT width, INT height,
4180 CombineMode mode)
4182 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
4184 if(!graphics)
4185 return InvalidParameter;
4187 if(graphics->busy)
4188 return ObjectBusy;
4190 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
4193 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
4194 CombineMode mode)
4196 TRACE("(%p, %p, %d)\n", graphics, region, mode);
4198 if(!graphics || !region)
4199 return InvalidParameter;
4201 if(graphics->busy)
4202 return ObjectBusy;
4204 return GdipCombineRegionRegion(graphics->clip, region, mode);
4207 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
4208 UINT limitDpi)
4210 static int calls;
4212 TRACE("(%p,%u)\n", metafile, limitDpi);
4214 if(!(calls++))
4215 FIXME("not implemented\n");
4217 return NotImplemented;
4220 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
4221 INT count)
4223 INT save_state;
4224 POINT *pti;
4226 TRACE("(%p, %p, %d)\n", graphics, points, count);
4228 if(!graphics || !pen || count<=0)
4229 return InvalidParameter;
4231 if(graphics->busy)
4232 return ObjectBusy;
4234 pti = GdipAlloc(sizeof(POINT) * count);
4236 save_state = prepare_dc(graphics, pen);
4237 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
4239 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
4240 Polygon(graphics->hdc, pti, count);
4242 restore_dc(graphics, save_state);
4243 GdipFree(pti);
4245 return Ok;
4248 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
4249 INT count)
4251 GpStatus ret;
4252 GpPointF *ptf;
4253 INT i;
4255 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
4257 if(count<=0) return InvalidParameter;
4258 ptf = GdipAlloc(sizeof(GpPointF) * count);
4260 for(i = 0;i < count; i++){
4261 ptf[i].X = (REAL)points[i].X;
4262 ptf[i].Y = (REAL)points[i].Y;
4265 ret = GdipDrawPolygon(graphics,pen,ptf,count);
4266 GdipFree(ptf);
4268 return ret;
4271 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
4273 TRACE("(%p, %p)\n", graphics, dpi);
4275 if(!graphics || !dpi)
4276 return InvalidParameter;
4278 if(graphics->busy)
4279 return ObjectBusy;
4281 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
4283 return Ok;
4286 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
4288 TRACE("(%p, %p)\n", graphics, dpi);
4290 if(!graphics || !dpi)
4291 return InvalidParameter;
4293 if(graphics->busy)
4294 return ObjectBusy;
4296 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
4298 return Ok;
4301 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
4302 GpMatrixOrder order)
4304 GpMatrix m;
4305 GpStatus ret;
4307 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
4309 if(!graphics || !matrix)
4310 return InvalidParameter;
4312 if(graphics->busy)
4313 return ObjectBusy;
4315 m = *(graphics->worldtrans);
4317 ret = GdipMultiplyMatrix(&m, matrix, order);
4318 if(ret == Ok)
4319 *(graphics->worldtrans) = m;
4321 return ret;
4324 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
4326 TRACE("(%p, %p)\n", graphics, hdc);
4328 if(!graphics || !hdc)
4329 return InvalidParameter;
4331 if(graphics->busy)
4332 return ObjectBusy;
4334 *hdc = graphics->hdc;
4335 graphics->busy = TRUE;
4337 return Ok;
4340 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
4342 TRACE("(%p, %p)\n", graphics, hdc);
4344 if(!graphics)
4345 return InvalidParameter;
4347 if(graphics->hdc != hdc || !(graphics->busy))
4348 return InvalidParameter;
4350 graphics->busy = FALSE;
4352 return Ok;
4355 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
4357 GpRegion *clip;
4358 GpStatus status;
4360 TRACE("(%p, %p)\n", graphics, region);
4362 if(!graphics || !region)
4363 return InvalidParameter;
4365 if(graphics->busy)
4366 return ObjectBusy;
4368 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
4369 return status;
4371 /* free everything except root node and header */
4372 delete_element(&region->node);
4373 memcpy(region, clip, sizeof(GpRegion));
4374 GdipFree(clip);
4376 return Ok;
4379 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
4380 GpCoordinateSpace src_space, GpPointF *points, INT count)
4382 GpMatrix *matrix;
4383 GpStatus stat;
4384 REAL unitscale;
4386 if(!graphics || !points || count <= 0)
4387 return InvalidParameter;
4389 if(graphics->busy)
4390 return ObjectBusy;
4392 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4394 if (src_space == dst_space) return Ok;
4396 stat = GdipCreateMatrix(&matrix);
4397 if (stat == Ok)
4399 unitscale = convert_unit(graphics->hdc, graphics->unit);
4401 if(graphics->unit != UnitDisplay)
4402 unitscale *= graphics->scale;
4404 /* transform from src_space to CoordinateSpacePage */
4405 switch (src_space)
4407 case CoordinateSpaceWorld:
4408 GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
4409 break;
4410 case CoordinateSpacePage:
4411 break;
4412 case CoordinateSpaceDevice:
4413 GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
4414 break;
4417 /* transform from CoordinateSpacePage to dst_space */
4418 switch (dst_space)
4420 case CoordinateSpaceWorld:
4422 GpMatrix *inverted_transform;
4423 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
4424 if (stat == Ok)
4426 stat = GdipInvertMatrix(inverted_transform);
4427 if (stat == Ok)
4428 GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
4429 GdipDeleteMatrix(inverted_transform);
4431 break;
4433 case CoordinateSpacePage:
4434 break;
4435 case CoordinateSpaceDevice:
4436 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
4437 break;
4440 if (stat == Ok)
4441 stat = GdipTransformMatrixPoints(matrix, points, count);
4443 GdipDeleteMatrix(matrix);
4446 return stat;
4449 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
4450 GpCoordinateSpace src_space, GpPoint *points, INT count)
4452 GpPointF *pointsF;
4453 GpStatus ret;
4454 INT i;
4456 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4458 if(count <= 0)
4459 return InvalidParameter;
4461 pointsF = GdipAlloc(sizeof(GpPointF) * count);
4462 if(!pointsF)
4463 return OutOfMemory;
4465 for(i = 0; i < count; i++){
4466 pointsF[i].X = (REAL)points[i].X;
4467 pointsF[i].Y = (REAL)points[i].Y;
4470 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
4472 if(ret == Ok)
4473 for(i = 0; i < count; i++){
4474 points[i].X = roundr(pointsF[i].X);
4475 points[i].Y = roundr(pointsF[i].Y);
4477 GdipFree(pointsF);
4479 return ret;
4482 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
4484 static int calls;
4486 TRACE("\n");
4488 if (!calls++)
4489 FIXME("stub\n");
4491 return NULL;
4494 /*****************************************************************************
4495 * GdipTranslateClip [GDIPLUS.@]
4497 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
4499 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
4501 if(!graphics)
4502 return InvalidParameter;
4504 if(graphics->busy)
4505 return ObjectBusy;
4507 return GdipTranslateRegion(graphics->clip, dx, dy);
4510 /*****************************************************************************
4511 * GdipTranslateClipI [GDIPLUS.@]
4513 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
4515 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
4517 if(!graphics)
4518 return InvalidParameter;
4520 if(graphics->busy)
4521 return ObjectBusy;
4523 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
4527 /*****************************************************************************
4528 * GdipMeasureDriverString [GDIPLUS.@]
4530 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4531 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
4532 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
4534 FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
4535 return NotImplemented;
4538 /*****************************************************************************
4539 * GdipDrawDriverString [GDIPLUS.@]
4541 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4542 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
4543 GDIPCONST PointF *positions, INT flags,
4544 GDIPCONST GpMatrix *matrix )
4546 FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4547 return NotImplemented;
4550 /*****************************************************************************
4551 * GdipRecordMetafileI [GDIPLUS.@]
4553 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
4554 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4556 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
4557 return NotImplemented;
4560 /*****************************************************************************
4561 * GdipIsVisibleClipEmpty [GDIPLUS.@]
4563 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
4565 GpStatus stat;
4566 GpRegion* rgn;
4568 TRACE("(%p, %p)\n", graphics, res);
4570 if((stat = GdipCreateRegion(&rgn)) != Ok)
4571 return stat;
4573 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4574 goto cleanup;
4576 stat = GdipIsEmptyRegion(rgn, graphics, res);
4578 cleanup:
4579 GdipDeleteRegion(rgn);
4580 return stat;