gdiplus: Don't require an HDC for the convert_unit function.
[wine.git] / dlls / gdiplus / graphics.c
blob094ed6167715964f97c06888a6fe5d7e8b42b4f3
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 REAL graphics_res(GpGraphics *graphics)
89 if (graphics->image) return graphics->image->xres;
90 else return (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
93 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
95 HPEN gdipen;
96 REAL width;
97 INT save_state = SaveDC(graphics->hdc), i, numdashes;
98 GpPointF pt[2];
99 DWORD dash_array[MAX_DASHLEN];
101 EndPath(graphics->hdc);
103 if(pen->unit == UnitPixel){
104 width = pen->width;
106 else{
107 /* Get an estimate for the amount the pen width is affected by the world
108 * transform. (This is similar to what some of the wine drivers do.) */
109 pt[0].X = 0.0;
110 pt[0].Y = 0.0;
111 pt[1].X = 1.0;
112 pt[1].Y = 1.0;
113 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
114 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
115 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
117 width *= pen->width * convert_unit(graphics_res(graphics),
118 pen->unit == UnitWorld ? graphics->unit : pen->unit);
121 if(pen->dash == DashStyleCustom){
122 numdashes = min(pen->numdashes, MAX_DASHLEN);
124 TRACE("dashes are: ");
125 for(i = 0; i < numdashes; i++){
126 dash_array[i] = roundr(width * pen->dashes[i]);
127 TRACE("%d, ", dash_array[i]);
129 TRACE("\n and the pen style is %x\n", pen->style);
131 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
132 numdashes, dash_array);
134 else
135 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
137 SelectObject(graphics->hdc, gdipen);
139 return save_state;
142 static void restore_dc(GpGraphics *graphics, INT state)
144 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
145 RestoreDC(graphics->hdc, state);
148 /* This helper applies all the changes that the points listed in ptf need in
149 * order to be drawn on the device context. In the end, this should include at
150 * least:
151 * -scaling by page unit
152 * -applying world transformation
153 * -converting from float to int
154 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
155 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
156 * gdi to draw, and these functions would irreparably mess with line widths.
158 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
159 GpPointF *ptf, INT count)
161 REAL unitscale;
162 GpMatrix *matrix;
163 int i;
165 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
167 /* apply page scale */
168 if(graphics->unit != UnitDisplay)
169 unitscale *= graphics->scale;
171 GdipCloneMatrix(graphics->worldtrans, &matrix);
172 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
173 GdipTransformMatrixPoints(matrix, ptf, count);
174 GdipDeleteMatrix(matrix);
176 for(i = 0; i < count; i++){
177 pti[i].x = roundr(ptf[i].X);
178 pti[i].y = roundr(ptf[i].Y);
182 /* Draw non-premultiplied ARGB data to the given graphics object */
183 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
184 const BYTE *src, INT src_width, INT src_height, INT src_stride)
186 if (graphics->image && graphics->image->type == ImageTypeBitmap)
188 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
189 INT x, y;
191 for (x=0; x<src_width; x++)
193 for (y=0; y<src_height; y++)
195 ARGB dst_color, src_color;
196 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
197 src_color = ((ARGB*)(src + src_stride * y))[x];
198 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
202 return Ok;
204 else
206 HDC hdc;
207 HBITMAP hbitmap, old_hbm=NULL;
208 BITMAPINFOHEADER bih;
209 BYTE *temp_bits;
210 BLENDFUNCTION bf;
212 hdc = CreateCompatibleDC(0);
214 bih.biSize = sizeof(BITMAPINFOHEADER);
215 bih.biWidth = src_width;
216 bih.biHeight = -src_height;
217 bih.biPlanes = 1;
218 bih.biBitCount = 32;
219 bih.biCompression = BI_RGB;
220 bih.biSizeImage = 0;
221 bih.biXPelsPerMeter = 0;
222 bih.biYPelsPerMeter = 0;
223 bih.biClrUsed = 0;
224 bih.biClrImportant = 0;
226 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
227 (void**)&temp_bits, NULL, 0);
229 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
230 4 * src_width, src, src_stride);
232 old_hbm = SelectObject(hdc, hbitmap);
234 bf.BlendOp = AC_SRC_OVER;
235 bf.BlendFlags = 0;
236 bf.SourceConstantAlpha = 255;
237 bf.AlphaFormat = AC_SRC_ALPHA;
239 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, src_width, src_height,
240 hdc, 0, 0, src_width, src_height, bf);
242 SelectObject(hdc, old_hbm);
243 DeleteDC(hdc);
244 DeleteObject(hbitmap);
246 return Ok;
250 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
252 ARGB result=0;
253 ARGB i;
254 for (i=0xff; i<=0xff0000; i = i << 8)
255 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
256 return result;
259 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
261 REAL blendfac;
263 /* clamp to between 0.0 and 1.0, using the wrap mode */
264 if (brush->wrap == WrapModeTile)
266 position = fmodf(position, 1.0f);
267 if (position < 0.0f) position += 1.0f;
269 else /* WrapModeFlip* */
271 position = fmodf(position, 2.0f);
272 if (position < 0.0f) position += 2.0f;
273 if (position > 1.0f) position = 2.0f - position;
276 if (brush->blendcount == 1)
277 blendfac = position;
278 else
280 int i=1;
281 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
282 REAL range;
284 /* locate the blend positions surrounding this position */
285 while (position > brush->blendpos[i])
286 i++;
288 /* interpolate between the blend positions */
289 left_blendpos = brush->blendpos[i-1];
290 left_blendfac = brush->blendfac[i-1];
291 right_blendpos = brush->blendpos[i];
292 right_blendfac = brush->blendfac[i];
293 range = right_blendpos - left_blendpos;
294 blendfac = (left_blendfac * (right_blendpos - position) +
295 right_blendfac * (position - left_blendpos)) / range;
298 if (brush->pblendcount == 0)
299 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
300 else
302 int i=1;
303 ARGB left_blendcolor, right_blendcolor;
304 REAL left_blendpos, right_blendpos;
306 /* locate the blend colors surrounding this position */
307 while (blendfac > brush->pblendpos[i])
308 i++;
310 /* interpolate between the blend colors */
311 left_blendpos = brush->pblendpos[i-1];
312 left_blendcolor = brush->pblendcolor[i-1];
313 right_blendpos = brush->pblendpos[i];
314 right_blendcolor = brush->pblendcolor[i];
315 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
316 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
320 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
322 switch (brush->bt)
324 case BrushTypeLinearGradient:
326 GpLineGradient *line = (GpLineGradient*)brush;
327 RECT rc;
329 SelectClipPath(graphics->hdc, RGN_AND);
330 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
332 GpPointF endpointsf[2];
333 POINT endpointsi[2];
334 POINT poly[4];
336 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
338 endpointsf[0] = line->startpoint;
339 endpointsf[1] = line->endpoint;
340 transform_and_round_points(graphics, endpointsi, endpointsf, 2);
342 if (abs(endpointsi[0].x-endpointsi[1].x) > abs(endpointsi[0].y-endpointsi[1].y))
344 /* vertical-ish gradient */
345 int startx, endx; /* x co-ordinates of endpoints shifted to intersect the top of the visible rectangle */
346 int startbottomx; /* x co-ordinate of start point shifted to intersect the bottom of the visible rectangle */
347 int width;
348 COLORREF col;
349 HBRUSH hbrush, hprevbrush;
350 int leftx, rightx; /* x co-ordinates where the leftmost and rightmost gradient lines hit the top of the visible rectangle */
351 int x;
352 int tilt; /* horizontal distance covered by a gradient line */
354 startx = roundr((rc.top - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
355 endx = roundr((rc.top - endpointsf[1].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[1].X);
356 width = endx - startx;
357 startbottomx = roundr((rc.bottom - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
358 tilt = startx - startbottomx;
360 if (startx >= startbottomx)
362 leftx = rc.left;
363 rightx = rc.right + tilt;
365 else
367 leftx = rc.left + tilt;
368 rightx = rc.right;
371 poly[0].y = rc.bottom;
372 poly[1].y = rc.top;
373 poly[2].y = rc.top;
374 poly[3].y = rc.bottom;
376 for (x=leftx; x<=rightx; x++)
378 ARGB argb = blend_line_gradient(line, (x-startx)/(REAL)width);
379 col = ARGB2COLORREF(argb);
380 hbrush = CreateSolidBrush(col);
381 hprevbrush = SelectObject(graphics->hdc, hbrush);
382 poly[0].x = x - tilt - 1;
383 poly[1].x = x - 1;
384 poly[2].x = x;
385 poly[3].x = x - tilt;
386 Polygon(graphics->hdc, poly, 4);
387 SelectObject(graphics->hdc, hprevbrush);
388 DeleteObject(hbrush);
391 else if (endpointsi[0].y != endpointsi[1].y)
393 /* horizontal-ish gradient */
394 int starty, endy; /* y co-ordinates of endpoints shifted to intersect the left of the visible rectangle */
395 int startrighty; /* y co-ordinate of start point shifted to intersect the right of the visible rectangle */
396 int height;
397 COLORREF col;
398 HBRUSH hbrush, hprevbrush;
399 int topy, bottomy; /* y co-ordinates where the topmost and bottommost gradient lines hit the left of the visible rectangle */
400 int y;
401 int tilt; /* vertical distance covered by a gradient line */
403 starty = roundr((rc.left - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
404 endy = roundr((rc.left - endpointsf[1].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[1].Y);
405 height = endy - starty;
406 startrighty = roundr((rc.right - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
407 tilt = starty - startrighty;
409 if (starty >= startrighty)
411 topy = rc.top;
412 bottomy = rc.bottom + tilt;
414 else
416 topy = rc.top + tilt;
417 bottomy = rc.bottom;
420 poly[0].x = rc.right;
421 poly[1].x = rc.left;
422 poly[2].x = rc.left;
423 poly[3].x = rc.right;
425 for (y=topy; y<=bottomy; y++)
427 ARGB argb = blend_line_gradient(line, (y-starty)/(REAL)height);
428 col = ARGB2COLORREF(argb);
429 hbrush = CreateSolidBrush(col);
430 hprevbrush = SelectObject(graphics->hdc, hbrush);
431 poly[0].y = y - tilt - 1;
432 poly[1].y = y - 1;
433 poly[2].y = y;
434 poly[3].y = y - tilt;
435 Polygon(graphics->hdc, poly, 4);
436 SelectObject(graphics->hdc, hprevbrush);
437 DeleteObject(hbrush);
440 /* else startpoint == endpoint */
442 break;
444 case BrushTypeSolidColor:
446 GpSolidFill *fill = (GpSolidFill*)brush;
447 if (fill->bmp)
449 RECT rc;
450 /* partially transparent fill */
452 SelectClipPath(graphics->hdc, RGN_AND);
453 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
455 HDC hdc = CreateCompatibleDC(NULL);
456 HBITMAP oldbmp;
457 BLENDFUNCTION bf;
459 if (!hdc) break;
461 oldbmp = SelectObject(hdc, fill->bmp);
463 bf.BlendOp = AC_SRC_OVER;
464 bf.BlendFlags = 0;
465 bf.SourceConstantAlpha = 255;
466 bf.AlphaFormat = AC_SRC_ALPHA;
468 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
470 SelectObject(hdc, oldbmp);
471 DeleteDC(hdc);
474 break;
476 /* else fall through */
478 default:
479 SelectObject(graphics->hdc, brush->gdibrush);
480 FillPath(graphics->hdc);
481 break;
485 /* GdipDrawPie/GdipFillPie helper function */
486 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
487 REAL height, REAL startAngle, REAL sweepAngle)
489 GpPointF ptf[4];
490 POINT pti[4];
492 ptf[0].X = x;
493 ptf[0].Y = y;
494 ptf[1].X = x + width;
495 ptf[1].Y = y + height;
497 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
498 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
500 transform_and_round_points(graphics, pti, ptf, 4);
502 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
503 pti[2].y, pti[3].x, pti[3].y);
506 /* Draws the linecap the specified color and size on the hdc. The linecap is in
507 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
508 * should not be called on an hdc that has a path you care about. */
509 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
510 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
512 HGDIOBJ oldbrush = NULL, oldpen = NULL;
513 GpMatrix *matrix = NULL;
514 HBRUSH brush = NULL;
515 HPEN pen = NULL;
516 PointF ptf[4], *custptf = NULL;
517 POINT pt[4], *custpt = NULL;
518 BYTE *tp = NULL;
519 REAL theta, dsmall, dbig, dx, dy = 0.0;
520 INT i, count;
521 LOGBRUSH lb;
522 BOOL customstroke;
524 if((x1 == x2) && (y1 == y2))
525 return;
527 theta = gdiplus_atan2(y2 - y1, x2 - x1);
529 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
530 if(!customstroke){
531 brush = CreateSolidBrush(color);
532 lb.lbStyle = BS_SOLID;
533 lb.lbColor = color;
534 lb.lbHatch = 0;
535 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
536 PS_JOIN_MITER, 1, &lb, 0,
537 NULL);
538 oldbrush = SelectObject(graphics->hdc, brush);
539 oldpen = SelectObject(graphics->hdc, pen);
542 switch(cap){
543 case LineCapFlat:
544 break;
545 case LineCapSquare:
546 case LineCapSquareAnchor:
547 case LineCapDiamondAnchor:
548 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
549 if(cap == LineCapDiamondAnchor){
550 dsmall = cos(theta + M_PI_2) * size;
551 dbig = sin(theta + M_PI_2) * size;
553 else{
554 dsmall = cos(theta + M_PI_4) * size;
555 dbig = sin(theta + M_PI_4) * size;
558 ptf[0].X = x2 - dsmall;
559 ptf[1].X = x2 + dbig;
561 ptf[0].Y = y2 - dbig;
562 ptf[3].Y = y2 + dsmall;
564 ptf[1].Y = y2 - dsmall;
565 ptf[2].Y = y2 + dbig;
567 ptf[3].X = x2 - dbig;
568 ptf[2].X = x2 + dsmall;
570 transform_and_round_points(graphics, pt, ptf, 4);
571 Polygon(graphics->hdc, pt, 4);
573 break;
574 case LineCapArrowAnchor:
575 size = size * 4.0 / sqrt(3.0);
577 dx = cos(M_PI / 6.0 + theta) * size;
578 dy = sin(M_PI / 6.0 + theta) * size;
580 ptf[0].X = x2 - dx;
581 ptf[0].Y = y2 - dy;
583 dx = cos(- M_PI / 6.0 + theta) * size;
584 dy = sin(- M_PI / 6.0 + theta) * size;
586 ptf[1].X = x2 - dx;
587 ptf[1].Y = y2 - dy;
589 ptf[2].X = x2;
590 ptf[2].Y = y2;
592 transform_and_round_points(graphics, pt, ptf, 3);
593 Polygon(graphics->hdc, pt, 3);
595 break;
596 case LineCapRoundAnchor:
597 dx = dy = ANCHOR_WIDTH * size / 2.0;
599 ptf[0].X = x2 - dx;
600 ptf[0].Y = y2 - dy;
601 ptf[1].X = x2 + dx;
602 ptf[1].Y = y2 + dy;
604 transform_and_round_points(graphics, pt, ptf, 2);
605 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
607 break;
608 case LineCapTriangle:
609 size = size / 2.0;
610 dx = cos(M_PI_2 + theta) * size;
611 dy = sin(M_PI_2 + theta) * size;
613 ptf[0].X = x2 - dx;
614 ptf[0].Y = y2 - dy;
615 ptf[1].X = x2 + dx;
616 ptf[1].Y = y2 + dy;
618 dx = cos(theta) * size;
619 dy = sin(theta) * size;
621 ptf[2].X = x2 + dx;
622 ptf[2].Y = y2 + dy;
624 transform_and_round_points(graphics, pt, ptf, 3);
625 Polygon(graphics->hdc, pt, 3);
627 break;
628 case LineCapRound:
629 dx = dy = size / 2.0;
631 ptf[0].X = x2 - dx;
632 ptf[0].Y = y2 - dy;
633 ptf[1].X = x2 + dx;
634 ptf[1].Y = y2 + dy;
636 dx = -cos(M_PI_2 + theta) * size;
637 dy = -sin(M_PI_2 + theta) * size;
639 ptf[2].X = x2 - dx;
640 ptf[2].Y = y2 - dy;
641 ptf[3].X = x2 + dx;
642 ptf[3].Y = y2 + dy;
644 transform_and_round_points(graphics, pt, ptf, 4);
645 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
646 pt[2].y, pt[3].x, pt[3].y);
648 break;
649 case LineCapCustom:
650 if(!custom)
651 break;
653 count = custom->pathdata.Count;
654 custptf = GdipAlloc(count * sizeof(PointF));
655 custpt = GdipAlloc(count * sizeof(POINT));
656 tp = GdipAlloc(count);
658 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
659 goto custend;
661 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
663 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
664 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
665 MatrixOrderAppend);
666 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
667 GdipTransformMatrixPoints(matrix, custptf, count);
669 transform_and_round_points(graphics, custpt, custptf, count);
671 for(i = 0; i < count; i++)
672 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
674 if(custom->fill){
675 BeginPath(graphics->hdc);
676 PolyDraw(graphics->hdc, custpt, tp, count);
677 EndPath(graphics->hdc);
678 StrokeAndFillPath(graphics->hdc);
680 else
681 PolyDraw(graphics->hdc, custpt, tp, count);
683 custend:
684 GdipFree(custptf);
685 GdipFree(custpt);
686 GdipFree(tp);
687 GdipDeleteMatrix(matrix);
688 break;
689 default:
690 break;
693 if(!customstroke){
694 SelectObject(graphics->hdc, oldbrush);
695 SelectObject(graphics->hdc, oldpen);
696 DeleteObject(brush);
697 DeleteObject(pen);
701 /* Shortens the line by the given percent by changing x2, y2.
702 * If percent is > 1.0 then the line will change direction.
703 * If percent is negative it can lengthen the line. */
704 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
706 REAL dist, theta, dx, dy;
708 if((y1 == *y2) && (x1 == *x2))
709 return;
711 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
712 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
713 dx = cos(theta) * dist;
714 dy = sin(theta) * dist;
716 *x2 = *x2 + dx;
717 *y2 = *y2 + dy;
720 /* Shortens the line by the given amount by changing x2, y2.
721 * If the amount is greater than the distance, the line will become length 0.
722 * If the amount is negative, it can lengthen the line. */
723 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
725 REAL dx, dy, percent;
727 dx = *x2 - x1;
728 dy = *y2 - y1;
729 if(dx == 0 && dy == 0)
730 return;
732 percent = amt / sqrt(dx * dx + dy * dy);
733 if(percent >= 1.0){
734 *x2 = x1;
735 *y2 = y1;
736 return;
739 shorten_line_percent(x1, y1, x2, y2, percent);
742 /* Draws lines between the given points, and if caps is true then draws an endcap
743 * at the end of the last line. */
744 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
745 GDIPCONST GpPointF * pt, INT count, BOOL caps)
747 POINT *pti = NULL;
748 GpPointF *ptcopy = NULL;
749 GpStatus status = GenericError;
751 if(!count)
752 return Ok;
754 pti = GdipAlloc(count * sizeof(POINT));
755 ptcopy = GdipAlloc(count * sizeof(GpPointF));
757 if(!pti || !ptcopy){
758 status = OutOfMemory;
759 goto end;
762 memcpy(ptcopy, pt, count * sizeof(GpPointF));
764 if(caps){
765 if(pen->endcap == LineCapArrowAnchor)
766 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
767 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
768 else if((pen->endcap == LineCapCustom) && pen->customend)
769 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
770 &ptcopy[count-1].X, &ptcopy[count-1].Y,
771 pen->customend->inset * pen->width);
773 if(pen->startcap == LineCapArrowAnchor)
774 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
775 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
776 else if((pen->startcap == LineCapCustom) && pen->customstart)
777 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
778 &ptcopy[0].X, &ptcopy[0].Y,
779 pen->customstart->inset * pen->width);
781 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
782 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
783 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
784 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
787 transform_and_round_points(graphics, pti, ptcopy, count);
789 if(Polyline(graphics->hdc, pti, count))
790 status = Ok;
792 end:
793 GdipFree(pti);
794 GdipFree(ptcopy);
796 return status;
799 /* Conducts a linear search to find the bezier points that will back off
800 * the endpoint of the curve by a distance of amt. Linear search works
801 * better than binary in this case because there are multiple solutions,
802 * and binary searches often find a bad one. I don't think this is what
803 * Windows does but short of rendering the bezier without GDI's help it's
804 * the best we can do. If rev then work from the start of the passed points
805 * instead of the end. */
806 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
808 GpPointF origpt[4];
809 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
810 INT i, first = 0, second = 1, third = 2, fourth = 3;
812 if(rev){
813 first = 3;
814 second = 2;
815 third = 1;
816 fourth = 0;
819 origx = pt[fourth].X;
820 origy = pt[fourth].Y;
821 memcpy(origpt, pt, sizeof(GpPointF) * 4);
823 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
824 /* reset bezier points to original values */
825 memcpy(pt, origpt, sizeof(GpPointF) * 4);
826 /* Perform magic on bezier points. Order is important here.*/
827 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
828 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
829 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
830 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
831 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
832 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
834 dx = pt[fourth].X - origx;
835 dy = pt[fourth].Y - origy;
837 diff = sqrt(dx * dx + dy * dy);
838 percent += 0.0005 * amt;
842 /* Draws bezier curves between given points, and if caps is true then draws an
843 * endcap at the end of the last line. */
844 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
845 GDIPCONST GpPointF * pt, INT count, BOOL caps)
847 POINT *pti;
848 GpPointF *ptcopy;
849 GpStatus status = GenericError;
851 if(!count)
852 return Ok;
854 pti = GdipAlloc(count * sizeof(POINT));
855 ptcopy = GdipAlloc(count * sizeof(GpPointF));
857 if(!pti || !ptcopy){
858 status = OutOfMemory;
859 goto end;
862 memcpy(ptcopy, pt, count * sizeof(GpPointF));
864 if(caps){
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], pen->width * pen->customend->inset,
869 FALSE);
871 if(pen->startcap == LineCapArrowAnchor)
872 shorten_bezier_amt(ptcopy, pen->width, TRUE);
873 else if((pen->startcap == LineCapCustom) && pen->customstart)
874 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
876 /* the direction of the line cap is parallel to the direction at the
877 * end of the bezier (which, if it has been shortened, is not the same
878 * as the direction from pt[count-2] to pt[count-1]) */
879 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
880 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
881 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
882 pt[count - 1].X, pt[count - 1].Y);
884 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
885 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
886 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
889 transform_and_round_points(graphics, pti, ptcopy, count);
891 PolyBezier(graphics->hdc, pti, count);
893 status = Ok;
895 end:
896 GdipFree(pti);
897 GdipFree(ptcopy);
899 return status;
902 /* Draws a combination of bezier curves and lines between points. */
903 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
904 GDIPCONST BYTE * types, INT count, BOOL caps)
906 POINT *pti = GdipAlloc(count * sizeof(POINT));
907 BYTE *tp = GdipAlloc(count);
908 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
909 INT i, j;
910 GpStatus status = GenericError;
912 if(!count){
913 status = Ok;
914 goto end;
916 if(!pti || !tp || !ptcopy){
917 status = OutOfMemory;
918 goto end;
921 for(i = 1; i < count; i++){
922 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
923 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
924 || !(types[i + 1] & PathPointTypeBezier)){
925 ERR("Bad bezier points\n");
926 goto end;
928 i += 2;
932 memcpy(ptcopy, pt, count * sizeof(GpPointF));
934 /* If we are drawing caps, go through the points and adjust them accordingly,
935 * and draw the caps. */
936 if(caps){
937 switch(types[count - 1] & PathPointTypePathTypeMask){
938 case PathPointTypeBezier:
939 if(pen->endcap == LineCapArrowAnchor)
940 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
941 else if((pen->endcap == LineCapCustom) && pen->customend)
942 shorten_bezier_amt(&ptcopy[count - 4],
943 pen->width * pen->customend->inset, FALSE);
945 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
946 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
947 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
948 pt[count - 1].X, pt[count - 1].Y);
950 break;
951 case PathPointTypeLine:
952 if(pen->endcap == LineCapArrowAnchor)
953 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
954 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
955 pen->width);
956 else if((pen->endcap == LineCapCustom) && pen->customend)
957 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
958 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
959 pen->customend->inset * pen->width);
961 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
962 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
963 pt[count - 1].Y);
965 break;
966 default:
967 ERR("Bad path last point\n");
968 goto end;
971 /* Find start of points */
972 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
973 == PathPointTypeStart); j++);
975 switch(types[j] & PathPointTypePathTypeMask){
976 case PathPointTypeBezier:
977 if(pen->startcap == LineCapArrowAnchor)
978 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
979 else if((pen->startcap == LineCapCustom) && pen->customstart)
980 shorten_bezier_amt(&ptcopy[j - 1],
981 pen->width * pen->customstart->inset, TRUE);
983 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
984 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
985 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
986 pt[j - 1].X, pt[j - 1].Y);
988 break;
989 case PathPointTypeLine:
990 if(pen->startcap == LineCapArrowAnchor)
991 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
992 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
993 pen->width);
994 else if((pen->startcap == LineCapCustom) && pen->customstart)
995 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
996 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
997 pen->customstart->inset * pen->width);
999 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1000 pt[j].X, pt[j].Y, pt[j - 1].X,
1001 pt[j - 1].Y);
1003 break;
1004 default:
1005 ERR("Bad path points\n");
1006 goto end;
1010 transform_and_round_points(graphics, pti, ptcopy, count);
1012 for(i = 0; i < count; i++){
1013 tp[i] = convert_path_point_type(types[i]);
1016 PolyDraw(graphics->hdc, pti, tp, count);
1018 status = Ok;
1020 end:
1021 GdipFree(pti);
1022 GdipFree(ptcopy);
1023 GdipFree(tp);
1025 return status;
1028 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1030 GpStatus result;
1032 BeginPath(graphics->hdc);
1033 result = draw_poly(graphics, NULL, path->pathdata.Points,
1034 path->pathdata.Types, path->pathdata.Count, FALSE);
1035 EndPath(graphics->hdc);
1036 return result;
1039 typedef struct _GraphicsContainerItem {
1040 struct list entry;
1041 GraphicsContainer contid;
1043 SmoothingMode smoothing;
1044 CompositingQuality compqual;
1045 InterpolationMode interpolation;
1046 CompositingMode compmode;
1047 TextRenderingHint texthint;
1048 REAL scale;
1049 GpUnit unit;
1050 PixelOffsetMode pixeloffset;
1051 UINT textcontrast;
1052 GpMatrix* worldtrans;
1053 GpRegion* clip;
1054 } GraphicsContainerItem;
1056 static GpStatus init_container(GraphicsContainerItem** container,
1057 GDIPCONST GpGraphics* graphics){
1058 GpStatus sts;
1060 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1061 if(!(*container))
1062 return OutOfMemory;
1064 (*container)->contid = graphics->contid + 1;
1066 (*container)->smoothing = graphics->smoothing;
1067 (*container)->compqual = graphics->compqual;
1068 (*container)->interpolation = graphics->interpolation;
1069 (*container)->compmode = graphics->compmode;
1070 (*container)->texthint = graphics->texthint;
1071 (*container)->scale = graphics->scale;
1072 (*container)->unit = graphics->unit;
1073 (*container)->textcontrast = graphics->textcontrast;
1074 (*container)->pixeloffset = graphics->pixeloffset;
1076 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1077 if(sts != Ok){
1078 GdipFree(*container);
1079 *container = NULL;
1080 return sts;
1083 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1084 if(sts != Ok){
1085 GdipDeleteMatrix((*container)->worldtrans);
1086 GdipFree(*container);
1087 *container = NULL;
1088 return sts;
1091 return Ok;
1094 static void delete_container(GraphicsContainerItem* container){
1095 GdipDeleteMatrix(container->worldtrans);
1096 GdipDeleteRegion(container->clip);
1097 GdipFree(container);
1100 static GpStatus restore_container(GpGraphics* graphics,
1101 GDIPCONST GraphicsContainerItem* container){
1102 GpStatus sts;
1103 GpMatrix *newTrans;
1104 GpRegion *newClip;
1106 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1107 if(sts != Ok)
1108 return sts;
1110 sts = GdipCloneRegion(container->clip, &newClip);
1111 if(sts != Ok){
1112 GdipDeleteMatrix(newTrans);
1113 return sts;
1116 GdipDeleteMatrix(graphics->worldtrans);
1117 graphics->worldtrans = newTrans;
1119 GdipDeleteRegion(graphics->clip);
1120 graphics->clip = newClip;
1122 graphics->contid = container->contid - 1;
1124 graphics->smoothing = container->smoothing;
1125 graphics->compqual = container->compqual;
1126 graphics->interpolation = container->interpolation;
1127 graphics->compmode = container->compmode;
1128 graphics->texthint = container->texthint;
1129 graphics->scale = container->scale;
1130 graphics->unit = container->unit;
1131 graphics->textcontrast = container->textcontrast;
1132 graphics->pixeloffset = container->pixeloffset;
1134 return Ok;
1137 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1139 RECT wnd_rect;
1141 if(graphics->hwnd) {
1142 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1143 return GenericError;
1145 rect->X = wnd_rect.left;
1146 rect->Y = wnd_rect.top;
1147 rect->Width = wnd_rect.right - wnd_rect.left;
1148 rect->Height = wnd_rect.bottom - wnd_rect.top;
1149 }else{
1150 rect->X = 0;
1151 rect->Y = 0;
1152 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1153 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1156 return Ok;
1159 /* on success, rgn will contain the region of the graphics object which
1160 * is visible after clipping has been applied */
1161 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1163 GpStatus stat;
1164 GpRectF rectf;
1165 GpRegion* tmp;
1167 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1168 return stat;
1170 if((stat = GdipCreateRegion(&tmp)) != Ok)
1171 return stat;
1173 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1174 goto end;
1176 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1177 goto end;
1179 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1181 end:
1182 GdipDeleteRegion(tmp);
1183 return stat;
1186 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1188 TRACE("(%p, %p)\n", hdc, graphics);
1190 return GdipCreateFromHDC2(hdc, NULL, graphics);
1193 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1195 GpStatus retval;
1197 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1199 if(hDevice != NULL) {
1200 FIXME("Don't know how to handle parameter hDevice\n");
1201 return NotImplemented;
1204 if(hdc == NULL)
1205 return OutOfMemory;
1207 if(graphics == NULL)
1208 return InvalidParameter;
1210 *graphics = GdipAlloc(sizeof(GpGraphics));
1211 if(!*graphics) return OutOfMemory;
1213 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1214 GdipFree(*graphics);
1215 return retval;
1218 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1219 GdipFree((*graphics)->worldtrans);
1220 GdipFree(*graphics);
1221 return retval;
1224 (*graphics)->hdc = hdc;
1225 (*graphics)->hwnd = WindowFromDC(hdc);
1226 (*graphics)->owndc = FALSE;
1227 (*graphics)->smoothing = SmoothingModeDefault;
1228 (*graphics)->compqual = CompositingQualityDefault;
1229 (*graphics)->interpolation = InterpolationModeDefault;
1230 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1231 (*graphics)->compmode = CompositingModeSourceOver;
1232 (*graphics)->unit = UnitDisplay;
1233 (*graphics)->scale = 1.0;
1234 (*graphics)->busy = FALSE;
1235 (*graphics)->textcontrast = 4;
1236 list_init(&(*graphics)->containers);
1237 (*graphics)->contid = 0;
1239 TRACE("<-- %p\n", *graphics);
1241 return Ok;
1244 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1246 GpStatus ret;
1247 HDC hdc;
1249 TRACE("(%p, %p)\n", hwnd, graphics);
1251 hdc = GetDC(hwnd);
1253 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1255 ReleaseDC(hwnd, hdc);
1256 return ret;
1259 (*graphics)->hwnd = hwnd;
1260 (*graphics)->owndc = TRUE;
1262 return Ok;
1265 /* FIXME: no icm handling */
1266 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1268 TRACE("(%p, %p)\n", hwnd, graphics);
1270 return GdipCreateFromHWND(hwnd, graphics);
1273 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1274 GpMetafile **metafile)
1276 static int calls;
1278 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1280 if(!hemf || !metafile)
1281 return InvalidParameter;
1283 if(!(calls++))
1284 FIXME("not implemented\n");
1286 return NotImplemented;
1289 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1290 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1292 IStream *stream = NULL;
1293 UINT read;
1294 BYTE* copy;
1295 HENHMETAFILE hemf;
1296 GpStatus retval = Ok;
1298 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1300 if(!hwmf || !metafile || !placeable)
1301 return InvalidParameter;
1303 *metafile = NULL;
1304 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1305 if(!read)
1306 return GenericError;
1307 copy = GdipAlloc(read);
1308 GetMetaFileBitsEx(hwmf, read, copy);
1310 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1311 GdipFree(copy);
1313 read = GetEnhMetaFileBits(hemf, 0, NULL);
1314 copy = GdipAlloc(read);
1315 GetEnhMetaFileBits(hemf, read, copy);
1316 DeleteEnhMetaFile(hemf);
1318 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1319 ERR("could not make stream\n");
1320 GdipFree(copy);
1321 retval = GenericError;
1322 goto err;
1325 *metafile = GdipAlloc(sizeof(GpMetafile));
1326 if(!*metafile){
1327 retval = OutOfMemory;
1328 goto err;
1331 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1332 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1334 retval = GenericError;
1335 goto err;
1339 (*metafile)->image.type = ImageTypeMetafile;
1340 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1341 (*metafile)->image.palette_flags = 0;
1342 (*metafile)->image.palette_count = 0;
1343 (*metafile)->image.palette_size = 0;
1344 (*metafile)->image.palette_entries = NULL;
1345 (*metafile)->image.xres = (REAL)placeable->Inch;
1346 (*metafile)->image.yres = (REAL)placeable->Inch;
1347 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1348 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1349 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1350 - placeable->BoundingBox.Left));
1351 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1352 - placeable->BoundingBox.Top));
1353 (*metafile)->unit = UnitPixel;
1355 if(delete)
1356 DeleteMetaFile(hwmf);
1358 TRACE("<-- %p\n", *metafile);
1360 err:
1361 if (retval != Ok)
1362 GdipFree(*metafile);
1363 IStream_Release(stream);
1364 return retval;
1367 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1368 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1370 HMETAFILE hmf = GetMetaFileW(file);
1372 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1374 if(!hmf) return InvalidParameter;
1376 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1379 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1380 GpMetafile **metafile)
1382 FIXME("(%p, %p): stub\n", file, metafile);
1383 return NotImplemented;
1386 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1387 GpMetafile **metafile)
1389 FIXME("(%p, %p): stub\n", stream, metafile);
1390 return NotImplemented;
1393 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1394 UINT access, IStream **stream)
1396 DWORD dwMode;
1397 HRESULT ret;
1399 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1401 if(!stream || !filename)
1402 return InvalidParameter;
1404 if(access & GENERIC_WRITE)
1405 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1406 else if(access & GENERIC_READ)
1407 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1408 else
1409 return InvalidParameter;
1411 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1413 return hresult_to_status(ret);
1416 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1418 GraphicsContainerItem *cont, *next;
1419 TRACE("(%p)\n", graphics);
1421 if(!graphics) return InvalidParameter;
1422 if(graphics->busy) return ObjectBusy;
1424 if(graphics->owndc)
1425 ReleaseDC(graphics->hwnd, graphics->hdc);
1427 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1428 list_remove(&cont->entry);
1429 delete_container(cont);
1432 GdipDeleteRegion(graphics->clip);
1433 GdipDeleteMatrix(graphics->worldtrans);
1434 GdipFree(graphics);
1436 return Ok;
1439 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1440 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1442 INT save_state, num_pts;
1443 GpPointF points[MAX_ARC_PTS];
1444 GpStatus retval;
1446 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1447 width, height, startAngle, sweepAngle);
1449 if(!graphics || !pen || width <= 0 || height <= 0)
1450 return InvalidParameter;
1452 if(graphics->busy)
1453 return ObjectBusy;
1455 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1457 save_state = prepare_dc(graphics, pen);
1459 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1461 restore_dc(graphics, save_state);
1463 return retval;
1466 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1467 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1469 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1470 width, height, startAngle, sweepAngle);
1472 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1475 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1476 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1478 INT save_state;
1479 GpPointF pt[4];
1480 GpStatus retval;
1482 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1483 x2, y2, x3, y3, x4, y4);
1485 if(!graphics || !pen)
1486 return InvalidParameter;
1488 if(graphics->busy)
1489 return ObjectBusy;
1491 pt[0].X = x1;
1492 pt[0].Y = y1;
1493 pt[1].X = x2;
1494 pt[1].Y = y2;
1495 pt[2].X = x3;
1496 pt[2].Y = y3;
1497 pt[3].X = x4;
1498 pt[3].Y = y4;
1500 save_state = prepare_dc(graphics, pen);
1502 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1504 restore_dc(graphics, save_state);
1506 return retval;
1509 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1510 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1512 INT save_state;
1513 GpPointF pt[4];
1514 GpStatus retval;
1516 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1517 x2, y2, x3, y3, x4, y4);
1519 if(!graphics || !pen)
1520 return InvalidParameter;
1522 if(graphics->busy)
1523 return ObjectBusy;
1525 pt[0].X = x1;
1526 pt[0].Y = y1;
1527 pt[1].X = x2;
1528 pt[1].Y = y2;
1529 pt[2].X = x3;
1530 pt[2].Y = y3;
1531 pt[3].X = x4;
1532 pt[3].Y = y4;
1534 save_state = prepare_dc(graphics, pen);
1536 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1538 restore_dc(graphics, save_state);
1540 return retval;
1543 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1544 GDIPCONST GpPointF *points, INT count)
1546 INT i;
1547 GpStatus ret;
1549 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1551 if(!graphics || !pen || !points || (count <= 0))
1552 return InvalidParameter;
1554 if(graphics->busy)
1555 return ObjectBusy;
1557 for(i = 0; i < floor(count / 4); i++){
1558 ret = GdipDrawBezier(graphics, pen,
1559 points[4*i].X, points[4*i].Y,
1560 points[4*i + 1].X, points[4*i + 1].Y,
1561 points[4*i + 2].X, points[4*i + 2].Y,
1562 points[4*i + 3].X, points[4*i + 3].Y);
1563 if(ret != Ok)
1564 return ret;
1567 return Ok;
1570 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1571 GDIPCONST GpPoint *points, INT count)
1573 GpPointF *pts;
1574 GpStatus ret;
1575 INT i;
1577 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1579 if(!graphics || !pen || !points || (count <= 0))
1580 return InvalidParameter;
1582 if(graphics->busy)
1583 return ObjectBusy;
1585 pts = GdipAlloc(sizeof(GpPointF) * count);
1586 if(!pts)
1587 return OutOfMemory;
1589 for(i = 0; i < count; i++){
1590 pts[i].X = (REAL)points[i].X;
1591 pts[i].Y = (REAL)points[i].Y;
1594 ret = GdipDrawBeziers(graphics,pen,pts,count);
1596 GdipFree(pts);
1598 return ret;
1601 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1602 GDIPCONST GpPointF *points, INT count)
1604 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1606 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1609 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1610 GDIPCONST GpPoint *points, INT count)
1612 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1614 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1617 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1618 GDIPCONST GpPointF *points, INT count, REAL tension)
1620 GpPath *path;
1621 GpStatus stat;
1623 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1625 if(!graphics || !pen || !points || count <= 0)
1626 return InvalidParameter;
1628 if(graphics->busy)
1629 return ObjectBusy;
1631 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1632 return stat;
1634 stat = GdipAddPathClosedCurve2(path, points, count, tension);
1635 if(stat != Ok){
1636 GdipDeletePath(path);
1637 return stat;
1640 stat = GdipDrawPath(graphics, pen, path);
1642 GdipDeletePath(path);
1644 return stat;
1647 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1648 GDIPCONST GpPoint *points, INT count, REAL tension)
1650 GpPointF *ptf;
1651 GpStatus stat;
1652 INT i;
1654 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1656 if(!points || count <= 0)
1657 return InvalidParameter;
1659 ptf = GdipAlloc(sizeof(GpPointF)*count);
1660 if(!ptf)
1661 return OutOfMemory;
1663 for(i = 0; i < count; i++){
1664 ptf[i].X = (REAL)points[i].X;
1665 ptf[i].Y = (REAL)points[i].Y;
1668 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1670 GdipFree(ptf);
1672 return stat;
1675 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1676 GDIPCONST GpPointF *points, INT count)
1678 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1680 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1683 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1684 GDIPCONST GpPoint *points, INT count)
1686 GpPointF *pointsF;
1687 GpStatus ret;
1688 INT i;
1690 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1692 if(!points)
1693 return InvalidParameter;
1695 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1696 if(!pointsF)
1697 return OutOfMemory;
1699 for(i = 0; i < count; i++){
1700 pointsF[i].X = (REAL)points[i].X;
1701 pointsF[i].Y = (REAL)points[i].Y;
1704 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1705 GdipFree(pointsF);
1707 return ret;
1710 /* Approximates cardinal spline with Bezier curves. */
1711 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1712 GDIPCONST GpPointF *points, INT count, REAL tension)
1714 /* PolyBezier expects count*3-2 points. */
1715 INT i, len_pt = count*3-2, save_state;
1716 GpPointF *pt;
1717 REAL x1, x2, y1, y2;
1718 GpStatus retval;
1720 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1722 if(!graphics || !pen)
1723 return InvalidParameter;
1725 if(graphics->busy)
1726 return ObjectBusy;
1728 if(count < 2)
1729 return InvalidParameter;
1731 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1732 if(!pt)
1733 return OutOfMemory;
1735 tension = tension * TENSION_CONST;
1737 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1738 tension, &x1, &y1);
1740 pt[0].X = points[0].X;
1741 pt[0].Y = points[0].Y;
1742 pt[1].X = x1;
1743 pt[1].Y = y1;
1745 for(i = 0; i < count-2; i++){
1746 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1748 pt[3*i+2].X = x1;
1749 pt[3*i+2].Y = y1;
1750 pt[3*i+3].X = points[i+1].X;
1751 pt[3*i+3].Y = points[i+1].Y;
1752 pt[3*i+4].X = x2;
1753 pt[3*i+4].Y = y2;
1756 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1757 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1759 pt[len_pt-2].X = x1;
1760 pt[len_pt-2].Y = y1;
1761 pt[len_pt-1].X = points[count-1].X;
1762 pt[len_pt-1].Y = points[count-1].Y;
1764 save_state = prepare_dc(graphics, pen);
1766 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1768 GdipFree(pt);
1769 restore_dc(graphics, save_state);
1771 return retval;
1774 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1775 GDIPCONST GpPoint *points, INT count, REAL tension)
1777 GpPointF *pointsF;
1778 GpStatus ret;
1779 INT i;
1781 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1783 if(!points)
1784 return InvalidParameter;
1786 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1787 if(!pointsF)
1788 return OutOfMemory;
1790 for(i = 0; i < count; i++){
1791 pointsF[i].X = (REAL)points[i].X;
1792 pointsF[i].Y = (REAL)points[i].Y;
1795 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1796 GdipFree(pointsF);
1798 return ret;
1801 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1802 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1803 REAL tension)
1805 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1807 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1808 return InvalidParameter;
1811 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1814 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1815 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1816 REAL tension)
1818 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1820 if(count < 0){
1821 return OutOfMemory;
1824 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1825 return InvalidParameter;
1828 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1831 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1832 REAL y, REAL width, REAL height)
1834 INT save_state;
1835 GpPointF ptf[2];
1836 POINT pti[2];
1838 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1840 if(!graphics || !pen)
1841 return InvalidParameter;
1843 if(graphics->busy)
1844 return ObjectBusy;
1846 ptf[0].X = x;
1847 ptf[0].Y = y;
1848 ptf[1].X = x + width;
1849 ptf[1].Y = y + height;
1851 save_state = prepare_dc(graphics, pen);
1852 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1854 transform_and_round_points(graphics, pti, ptf, 2);
1856 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1858 restore_dc(graphics, save_state);
1860 return Ok;
1863 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1864 INT y, INT width, INT height)
1866 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1868 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1872 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1874 UINT width, height;
1875 GpPointF points[3];
1877 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1879 if(!graphics || !image)
1880 return InvalidParameter;
1882 GdipGetImageWidth(image, &width);
1883 GdipGetImageHeight(image, &height);
1885 /* FIXME: we should use the graphics and image dpi, somehow */
1887 points[0].X = points[2].X = x;
1888 points[0].Y = points[1].Y = y;
1889 points[1].X = x + width;
1890 points[2].Y = y + height;
1892 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
1893 UnitPixel, NULL, NULL, NULL);
1896 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1897 INT y)
1899 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1901 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1904 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1905 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1906 GpUnit srcUnit)
1908 GpPointF points[3];
1909 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1911 points[0].X = points[2].X = x;
1912 points[0].Y = points[1].Y = y;
1914 /* FIXME: convert image coordinates to Graphics coordinates? */
1915 points[1].X = x + srcwidth;
1916 points[2].Y = y + srcheight;
1918 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1919 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
1922 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
1923 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
1924 GpUnit srcUnit)
1926 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1929 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
1930 GDIPCONST GpPointF *dstpoints, INT count)
1932 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1933 return NotImplemented;
1936 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
1937 GDIPCONST GpPoint *dstpoints, INT count)
1939 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1940 return NotImplemented;
1943 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1944 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1945 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1946 DrawImageAbort callback, VOID * callbackData)
1948 GpPointF ptf[4];
1949 POINT pti[4];
1950 REAL dx, dy;
1951 GpStatus stat;
1953 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1954 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1955 callbackData);
1957 if(!graphics || !image || !points || count != 3)
1958 return InvalidParameter;
1960 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
1961 debugstr_pointf(&points[2]));
1963 memcpy(ptf, points, 3 * sizeof(GpPointF));
1964 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
1965 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
1966 transform_and_round_points(graphics, pti, ptf, 4);
1968 if (image->picture)
1970 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1971 if(srcUnit == UnitInch)
1972 dx = dy = (REAL) INCH_HIMETRIC;
1973 else if(srcUnit == UnitPixel){
1974 dx = ((REAL) INCH_HIMETRIC) /
1975 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1976 dy = ((REAL) INCH_HIMETRIC) /
1977 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1979 else
1980 return NotImplemented;
1982 if(IPicture_Render(image->picture, graphics->hdc,
1983 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1984 srcx * dx, srcy * dy,
1985 srcwidth * dx, srcheight * dy,
1986 NULL) != S_OK){
1987 if(callback)
1988 callback(callbackData);
1989 return GenericError;
1992 else if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
1994 GpBitmap* bitmap = (GpBitmap*)image;
1995 int use_software=0;
1997 if (srcUnit == UnitInch)
1998 dx = dy = 96.0; /* FIXME: use the image resolution */
1999 else if (srcUnit == UnitPixel)
2000 dx = dy = 1.0;
2001 else
2002 return NotImplemented;
2004 if (imageAttributes ||
2005 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2006 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X)
2007 use_software = 1;
2009 if (use_software)
2011 RECT src_area, dst_area;
2012 int i, x, y, stride;
2013 GpMatrix *dst_to_src;
2014 REAL m11, m12, m21, m22, mdx, mdy;
2015 LPBYTE data;
2017 src_area.left = srcx*dx;
2018 src_area.top = srcy*dy;
2019 src_area.right = (srcx+srcwidth)*dx;
2020 src_area.bottom = (srcy+srcheight)*dy;
2022 dst_area.left = dst_area.right = pti[0].x;
2023 dst_area.top = dst_area.bottom = pti[0].y;
2024 for (i=1; i<4; i++)
2026 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2027 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2028 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2029 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2032 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2033 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2034 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2035 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2036 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2037 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2039 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
2040 if (stat != Ok) return stat;
2042 stat = GdipInvertMatrix(dst_to_src);
2043 if (stat != Ok)
2045 GdipDeleteMatrix(dst_to_src);
2046 return stat;
2049 data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2050 if (!data)
2052 GdipDeleteMatrix(dst_to_src);
2053 return OutOfMemory;
2056 stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2058 if (imageAttributes &&
2059 (imageAttributes->wrap != WrapModeClamp ||
2060 imageAttributes->outside_color != 0x00000000 ||
2061 imageAttributes->clamp))
2063 static int fixme;
2064 if (!fixme++)
2065 FIXME("Image wrap mode not implemented\n");
2068 for (x=dst_area.left; x<dst_area.right; x++)
2070 for (y=dst_area.top; y<dst_area.bottom; y++)
2072 GpPointF src_pointf;
2073 int src_x, src_y;
2074 ARGB *src_color;
2076 src_pointf.X = x;
2077 src_pointf.Y = y;
2079 GdipTransformMatrixPoints(dst_to_src, &src_pointf, 1);
2081 src_x = roundr(src_pointf.X);
2082 src_y = roundr(src_pointf.Y);
2084 src_color = (ARGB*)(data + stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2086 if (src_x < src_area.left || src_x >= src_area.right ||
2087 src_y < src_area.top || src_y >= src_area.bottom)
2088 *src_color = 0;
2089 else
2090 GdipBitmapGetPixel(bitmap, src_x, src_y, src_color);
2094 GdipDeleteMatrix(dst_to_src);
2096 if (imageAttributes)
2098 if (imageAttributes->colorkeys[ColorAdjustTypeBitmap].enabled ||
2099 imageAttributes->colorkeys[ColorAdjustTypeDefault].enabled)
2101 const struct color_key *key;
2102 BYTE min_blue, min_green, min_red;
2103 BYTE max_blue, max_green, max_red;
2105 if (imageAttributes->colorkeys[ColorAdjustTypeBitmap].enabled)
2106 key = &imageAttributes->colorkeys[ColorAdjustTypeBitmap];
2107 else
2108 key = &imageAttributes->colorkeys[ColorAdjustTypeDefault];
2110 min_blue = key->low&0xff;
2111 min_green = (key->low>>8)&0xff;
2112 min_red = (key->low>>16)&0xff;
2114 max_blue = key->high&0xff;
2115 max_green = (key->high>>8)&0xff;
2116 max_red = (key->high>>16)&0xff;
2118 for (x=dst_area.left; x<dst_area.right; x++)
2119 for (y=dst_area.top; y<dst_area.bottom; y++)
2121 ARGB *src_color;
2122 BYTE blue, green, red;
2123 src_color = (ARGB*)(data + stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2124 blue = *src_color&0xff;
2125 green = (*src_color>>8)&0xff;
2126 red = (*src_color>>16)&0xff;
2127 if (blue >= min_blue && green >= min_green && red >= min_red &&
2128 blue <= max_blue && green <= max_green && red <= max_red)
2129 *src_color = 0x00000000;
2133 if (imageAttributes->colorremaptables[ColorAdjustTypeBitmap].enabled ||
2134 imageAttributes->colorremaptables[ColorAdjustTypeDefault].enabled)
2136 const struct color_remap_table *table;
2138 if (imageAttributes->colorremaptables[ColorAdjustTypeBitmap].enabled)
2139 table = &imageAttributes->colorremaptables[ColorAdjustTypeBitmap];
2140 else
2141 table = &imageAttributes->colorremaptables[ColorAdjustTypeDefault];
2143 for (x=dst_area.left; x<dst_area.right; x++)
2144 for (y=dst_area.top; y<dst_area.bottom; y++)
2146 ARGB *src_color;
2147 src_color = (ARGB*)(data + stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2148 for (i=0; i<table->mapsize; i++)
2150 if (*src_color == table->colormap[i].oldColor.Argb)
2152 *src_color = table->colormap[i].newColor.Argb;
2153 break;
2159 if (imageAttributes->colormatrices[ColorAdjustTypeBitmap].enabled ||
2160 imageAttributes->colormatrices[ColorAdjustTypeDefault].enabled)
2162 static int fixme;
2163 if (!fixme++)
2164 FIXME("Color transforms not implemented\n");
2167 if (imageAttributes->gamma_enabled[ColorAdjustTypeBitmap] ||
2168 imageAttributes->gamma_enabled[ColorAdjustTypeDefault])
2170 static int fixme;
2171 if (!fixme++)
2172 FIXME("Gamma adjustment not implemented\n");
2176 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
2177 data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, stride);
2179 GdipFree(data);
2181 return stat;
2183 else
2185 HDC hdc;
2186 int temp_hdc=0, temp_bitmap=0;
2187 HBITMAP hbitmap, old_hbm=NULL;
2189 if (!(bitmap->format == PixelFormat16bppRGB555 ||
2190 bitmap->format == PixelFormat24bppRGB ||
2191 bitmap->format == PixelFormat32bppRGB ||
2192 bitmap->format == PixelFormat32bppPARGB))
2194 BITMAPINFOHEADER bih;
2195 BYTE *temp_bits;
2196 PixelFormat dst_format;
2198 /* we can't draw a bitmap of this format directly */
2199 hdc = CreateCompatibleDC(0);
2200 temp_hdc = 1;
2201 temp_bitmap = 1;
2203 bih.biSize = sizeof(BITMAPINFOHEADER);
2204 bih.biWidth = bitmap->width;
2205 bih.biHeight = -bitmap->height;
2206 bih.biPlanes = 1;
2207 bih.biBitCount = 32;
2208 bih.biCompression = BI_RGB;
2209 bih.biSizeImage = 0;
2210 bih.biXPelsPerMeter = 0;
2211 bih.biYPelsPerMeter = 0;
2212 bih.biClrUsed = 0;
2213 bih.biClrImportant = 0;
2215 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
2216 (void**)&temp_bits, NULL, 0);
2218 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2219 dst_format = PixelFormat32bppPARGB;
2220 else
2221 dst_format = PixelFormat32bppRGB;
2223 convert_pixels(bitmap->width, bitmap->height,
2224 bitmap->width*4, temp_bits, dst_format,
2225 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
2227 else
2229 hbitmap = bitmap->hbitmap;
2230 hdc = bitmap->hdc;
2231 temp_hdc = (hdc == 0);
2234 if (temp_hdc)
2236 if (!hdc) hdc = CreateCompatibleDC(0);
2237 old_hbm = SelectObject(hdc, hbitmap);
2240 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2242 BLENDFUNCTION bf;
2244 bf.BlendOp = AC_SRC_OVER;
2245 bf.BlendFlags = 0;
2246 bf.SourceConstantAlpha = 255;
2247 bf.AlphaFormat = AC_SRC_ALPHA;
2249 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2250 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, bf);
2252 else
2254 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2255 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, SRCCOPY);
2258 if (temp_hdc)
2260 SelectObject(hdc, old_hbm);
2261 DeleteDC(hdc);
2264 if (temp_bitmap)
2265 DeleteObject(hbitmap);
2268 else
2270 ERR("GpImage with no IPicture or HBITMAP?!\n");
2271 return NotImplemented;
2274 return Ok;
2277 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
2278 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
2279 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2280 DrawImageAbort callback, VOID * callbackData)
2282 GpPointF pointsF[3];
2283 INT i;
2285 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
2286 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2287 callbackData);
2289 if(!points || count!=3)
2290 return InvalidParameter;
2292 for(i = 0; i < count; i++){
2293 pointsF[i].X = (REAL)points[i].X;
2294 pointsF[i].Y = (REAL)points[i].Y;
2297 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2298 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2299 callback, callbackData);
2302 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2303 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2304 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2305 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2306 VOID * callbackData)
2308 GpPointF points[3];
2310 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2311 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2312 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2314 points[0].X = dstx;
2315 points[0].Y = dsty;
2316 points[1].X = dstx + dstwidth;
2317 points[1].Y = dsty;
2318 points[2].X = dstx;
2319 points[2].Y = dsty + dstheight;
2321 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2322 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2325 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2326 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2327 INT srcwidth, INT srcheight, GpUnit srcUnit,
2328 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2329 VOID * callbackData)
2331 GpPointF points[3];
2333 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2334 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2335 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2337 points[0].X = dstx;
2338 points[0].Y = dsty;
2339 points[1].X = dstx + dstwidth;
2340 points[1].Y = dsty;
2341 points[2].X = dstx;
2342 points[2].Y = dsty + dstheight;
2344 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2345 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2348 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2349 REAL x, REAL y, REAL width, REAL height)
2351 RectF bounds;
2352 GpUnit unit;
2353 GpStatus ret;
2355 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2357 if(!graphics || !image)
2358 return InvalidParameter;
2360 ret = GdipGetImageBounds(image, &bounds, &unit);
2361 if(ret != Ok)
2362 return ret;
2364 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2365 bounds.X, bounds.Y, bounds.Width, bounds.Height,
2366 unit, NULL, NULL, NULL);
2369 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2370 INT x, INT y, INT width, INT height)
2372 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2374 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2377 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2378 REAL y1, REAL x2, REAL y2)
2380 INT save_state;
2381 GpPointF pt[2];
2382 GpStatus retval;
2384 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2386 if(!pen || !graphics)
2387 return InvalidParameter;
2389 if(graphics->busy)
2390 return ObjectBusy;
2392 pt[0].X = x1;
2393 pt[0].Y = y1;
2394 pt[1].X = x2;
2395 pt[1].Y = y2;
2397 save_state = prepare_dc(graphics, pen);
2399 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2401 restore_dc(graphics, save_state);
2403 return retval;
2406 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2407 INT y1, INT x2, INT y2)
2409 INT save_state;
2410 GpPointF pt[2];
2411 GpStatus retval;
2413 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2415 if(!pen || !graphics)
2416 return InvalidParameter;
2418 if(graphics->busy)
2419 return ObjectBusy;
2421 pt[0].X = (REAL)x1;
2422 pt[0].Y = (REAL)y1;
2423 pt[1].X = (REAL)x2;
2424 pt[1].Y = (REAL)y2;
2426 save_state = prepare_dc(graphics, pen);
2428 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2430 restore_dc(graphics, save_state);
2432 return retval;
2435 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2436 GpPointF *points, INT count)
2438 INT save_state;
2439 GpStatus retval;
2441 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2443 if(!pen || !graphics || (count < 2))
2444 return InvalidParameter;
2446 if(graphics->busy)
2447 return ObjectBusy;
2449 save_state = prepare_dc(graphics, pen);
2451 retval = draw_polyline(graphics, pen, points, count, TRUE);
2453 restore_dc(graphics, save_state);
2455 return retval;
2458 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2459 GpPoint *points, INT count)
2461 INT save_state;
2462 GpStatus retval;
2463 GpPointF *ptf = NULL;
2464 int i;
2466 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2468 if(!pen || !graphics || (count < 2))
2469 return InvalidParameter;
2471 if(graphics->busy)
2472 return ObjectBusy;
2474 ptf = GdipAlloc(count * sizeof(GpPointF));
2475 if(!ptf) return OutOfMemory;
2477 for(i = 0; i < count; i ++){
2478 ptf[i].X = (REAL) points[i].X;
2479 ptf[i].Y = (REAL) points[i].Y;
2482 save_state = prepare_dc(graphics, pen);
2484 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2486 restore_dc(graphics, save_state);
2488 GdipFree(ptf);
2489 return retval;
2492 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2494 INT save_state;
2495 GpStatus retval;
2497 TRACE("(%p, %p, %p)\n", graphics, pen, path);
2499 if(!pen || !graphics)
2500 return InvalidParameter;
2502 if(graphics->busy)
2503 return ObjectBusy;
2505 save_state = prepare_dc(graphics, pen);
2507 retval = draw_poly(graphics, pen, path->pathdata.Points,
2508 path->pathdata.Types, path->pathdata.Count, TRUE);
2510 restore_dc(graphics, save_state);
2512 return retval;
2515 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2516 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2518 INT save_state;
2520 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2521 width, height, startAngle, sweepAngle);
2523 if(!graphics || !pen)
2524 return InvalidParameter;
2526 if(graphics->busy)
2527 return ObjectBusy;
2529 save_state = prepare_dc(graphics, pen);
2530 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2532 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2534 restore_dc(graphics, save_state);
2536 return Ok;
2539 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2540 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2542 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2543 width, height, startAngle, sweepAngle);
2545 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2548 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2549 REAL y, REAL width, REAL height)
2551 INT save_state;
2552 GpPointF ptf[4];
2553 POINT pti[4];
2555 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2557 if(!pen || !graphics)
2558 return InvalidParameter;
2560 if(graphics->busy)
2561 return ObjectBusy;
2563 ptf[0].X = x;
2564 ptf[0].Y = y;
2565 ptf[1].X = x + width;
2566 ptf[1].Y = y;
2567 ptf[2].X = x + width;
2568 ptf[2].Y = y + height;
2569 ptf[3].X = x;
2570 ptf[3].Y = y + height;
2572 save_state = prepare_dc(graphics, pen);
2573 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2575 transform_and_round_points(graphics, pti, ptf, 4);
2576 Polygon(graphics->hdc, pti, 4);
2578 restore_dc(graphics, save_state);
2580 return Ok;
2583 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2584 INT y, INT width, INT height)
2586 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2588 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2591 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2592 GDIPCONST GpRectF* rects, INT count)
2594 GpPointF *ptf;
2595 POINT *pti;
2596 INT save_state, i;
2598 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2600 if(!graphics || !pen || !rects || count < 1)
2601 return InvalidParameter;
2603 if(graphics->busy)
2604 return ObjectBusy;
2606 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2607 pti = GdipAlloc(4 * count * sizeof(POINT));
2609 if(!ptf || !pti){
2610 GdipFree(ptf);
2611 GdipFree(pti);
2612 return OutOfMemory;
2615 for(i = 0; i < count; i++){
2616 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2617 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2618 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2619 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2622 save_state = prepare_dc(graphics, pen);
2623 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2625 transform_and_round_points(graphics, pti, ptf, 4 * count);
2627 for(i = 0; i < count; i++)
2628 Polygon(graphics->hdc, &pti[4 * i], 4);
2630 restore_dc(graphics, save_state);
2632 GdipFree(ptf);
2633 GdipFree(pti);
2635 return Ok;
2638 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2639 GDIPCONST GpRect* rects, INT count)
2641 GpRectF *rectsF;
2642 GpStatus ret;
2643 INT i;
2645 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2647 if(!rects || count<=0)
2648 return InvalidParameter;
2650 rectsF = GdipAlloc(sizeof(GpRectF) * count);
2651 if(!rectsF)
2652 return OutOfMemory;
2654 for(i = 0;i < count;i++){
2655 rectsF[i].X = (REAL)rects[i].X;
2656 rectsF[i].Y = (REAL)rects[i].Y;
2657 rectsF[i].Width = (REAL)rects[i].Width;
2658 rectsF[i].Height = (REAL)rects[i].Height;
2661 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2662 GdipFree(rectsF);
2664 return ret;
2667 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2668 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2670 GpPath *path;
2671 GpStatus stat;
2673 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2674 count, tension, fill);
2676 if(!graphics || !brush || !points)
2677 return InvalidParameter;
2679 if(graphics->busy)
2680 return ObjectBusy;
2682 if(count == 1) /* Do nothing */
2683 return Ok;
2685 stat = GdipCreatePath(fill, &path);
2686 if(stat != Ok)
2687 return stat;
2689 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2690 if(stat != Ok){
2691 GdipDeletePath(path);
2692 return stat;
2695 stat = GdipFillPath(graphics, brush, path);
2696 if(stat != Ok){
2697 GdipDeletePath(path);
2698 return stat;
2701 GdipDeletePath(path);
2703 return Ok;
2706 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2707 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2709 GpPointF *ptf;
2710 GpStatus stat;
2711 INT i;
2713 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2714 count, tension, fill);
2716 if(!points || count == 0)
2717 return InvalidParameter;
2719 if(count == 1) /* Do nothing */
2720 return Ok;
2722 ptf = GdipAlloc(sizeof(GpPointF)*count);
2723 if(!ptf)
2724 return OutOfMemory;
2726 for(i = 0;i < count;i++){
2727 ptf[i].X = (REAL)points[i].X;
2728 ptf[i].Y = (REAL)points[i].Y;
2731 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2733 GdipFree(ptf);
2735 return stat;
2738 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
2739 GDIPCONST GpPointF *points, INT count)
2741 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2742 return GdipFillClosedCurve2(graphics, brush, points, count,
2743 0.5f, FillModeAlternate);
2746 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
2747 GDIPCONST GpPoint *points, INT count)
2749 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2750 return GdipFillClosedCurve2I(graphics, brush, points, count,
2751 0.5f, FillModeAlternate);
2754 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2755 REAL y, REAL width, REAL height)
2757 INT save_state;
2758 GpPointF ptf[2];
2759 POINT pti[2];
2761 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2763 if(!graphics || !brush)
2764 return InvalidParameter;
2766 if(graphics->busy)
2767 return ObjectBusy;
2769 ptf[0].X = x;
2770 ptf[0].Y = y;
2771 ptf[1].X = x + width;
2772 ptf[1].Y = y + height;
2774 save_state = SaveDC(graphics->hdc);
2775 EndPath(graphics->hdc);
2777 transform_and_round_points(graphics, pti, ptf, 2);
2779 BeginPath(graphics->hdc);
2780 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2781 EndPath(graphics->hdc);
2783 brush_fill_path(graphics, brush);
2785 RestoreDC(graphics->hdc, save_state);
2787 return Ok;
2790 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2791 INT y, INT width, INT height)
2793 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2795 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2798 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2800 INT save_state;
2801 GpStatus retval;
2803 TRACE("(%p, %p, %p)\n", graphics, brush, path);
2805 if(!brush || !graphics || !path)
2806 return InvalidParameter;
2808 if(graphics->busy)
2809 return ObjectBusy;
2811 save_state = SaveDC(graphics->hdc);
2812 EndPath(graphics->hdc);
2813 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2814 : WINDING));
2816 BeginPath(graphics->hdc);
2817 retval = draw_poly(graphics, NULL, path->pathdata.Points,
2818 path->pathdata.Types, path->pathdata.Count, FALSE);
2820 if(retval != Ok)
2821 goto end;
2823 EndPath(graphics->hdc);
2824 brush_fill_path(graphics, brush);
2826 retval = Ok;
2828 end:
2829 RestoreDC(graphics->hdc, save_state);
2831 return retval;
2834 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2835 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2837 INT save_state;
2839 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2840 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2842 if(!graphics || !brush)
2843 return InvalidParameter;
2845 if(graphics->busy)
2846 return ObjectBusy;
2848 save_state = SaveDC(graphics->hdc);
2849 EndPath(graphics->hdc);
2851 BeginPath(graphics->hdc);
2852 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2853 EndPath(graphics->hdc);
2855 brush_fill_path(graphics, brush);
2857 RestoreDC(graphics->hdc, save_state);
2859 return Ok;
2862 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2863 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2865 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2866 graphics, brush, x, y, width, height, startAngle, sweepAngle);
2868 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2871 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2872 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2874 INT save_state;
2875 GpPointF *ptf = NULL;
2876 POINT *pti = NULL;
2877 GpStatus retval = Ok;
2879 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2881 if(!graphics || !brush || !points || !count)
2882 return InvalidParameter;
2884 if(graphics->busy)
2885 return ObjectBusy;
2887 ptf = GdipAlloc(count * sizeof(GpPointF));
2888 pti = GdipAlloc(count * sizeof(POINT));
2889 if(!ptf || !pti){
2890 retval = OutOfMemory;
2891 goto end;
2894 memcpy(ptf, points, count * sizeof(GpPointF));
2896 save_state = SaveDC(graphics->hdc);
2897 EndPath(graphics->hdc);
2898 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2899 : WINDING));
2901 transform_and_round_points(graphics, pti, ptf, count);
2903 BeginPath(graphics->hdc);
2904 Polygon(graphics->hdc, pti, count);
2905 EndPath(graphics->hdc);
2907 brush_fill_path(graphics, brush);
2909 RestoreDC(graphics->hdc, save_state);
2911 end:
2912 GdipFree(ptf);
2913 GdipFree(pti);
2915 return retval;
2918 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2919 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2921 INT save_state, i;
2922 GpPointF *ptf = NULL;
2923 POINT *pti = NULL;
2924 GpStatus retval = Ok;
2926 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2928 if(!graphics || !brush || !points || !count)
2929 return InvalidParameter;
2931 if(graphics->busy)
2932 return ObjectBusy;
2934 ptf = GdipAlloc(count * sizeof(GpPointF));
2935 pti = GdipAlloc(count * sizeof(POINT));
2936 if(!ptf || !pti){
2937 retval = OutOfMemory;
2938 goto end;
2941 for(i = 0; i < count; i ++){
2942 ptf[i].X = (REAL) points[i].X;
2943 ptf[i].Y = (REAL) points[i].Y;
2946 save_state = SaveDC(graphics->hdc);
2947 EndPath(graphics->hdc);
2948 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2949 : WINDING));
2951 transform_and_round_points(graphics, pti, ptf, count);
2953 BeginPath(graphics->hdc);
2954 Polygon(graphics->hdc, pti, count);
2955 EndPath(graphics->hdc);
2957 brush_fill_path(graphics, brush);
2959 RestoreDC(graphics->hdc, save_state);
2961 end:
2962 GdipFree(ptf);
2963 GdipFree(pti);
2965 return retval;
2968 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2969 GDIPCONST GpPointF *points, INT count)
2971 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2973 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2976 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2977 GDIPCONST GpPoint *points, INT count)
2979 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2981 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2984 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2985 REAL x, REAL y, REAL width, REAL height)
2987 INT save_state;
2988 GpPointF ptf[4];
2989 POINT pti[4];
2991 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2993 if(!graphics || !brush)
2994 return InvalidParameter;
2996 if(graphics->busy)
2997 return ObjectBusy;
2999 ptf[0].X = x;
3000 ptf[0].Y = y;
3001 ptf[1].X = x + width;
3002 ptf[1].Y = y;
3003 ptf[2].X = x + width;
3004 ptf[2].Y = y + height;
3005 ptf[3].X = x;
3006 ptf[3].Y = y + height;
3008 save_state = SaveDC(graphics->hdc);
3009 EndPath(graphics->hdc);
3011 transform_and_round_points(graphics, pti, ptf, 4);
3013 BeginPath(graphics->hdc);
3014 Polygon(graphics->hdc, pti, 4);
3015 EndPath(graphics->hdc);
3017 brush_fill_path(graphics, brush);
3019 RestoreDC(graphics->hdc, save_state);
3021 return Ok;
3024 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3025 INT x, INT y, INT width, INT height)
3027 INT save_state;
3028 GpPointF ptf[4];
3029 POINT pti[4];
3031 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3033 if(!graphics || !brush)
3034 return InvalidParameter;
3036 if(graphics->busy)
3037 return ObjectBusy;
3039 ptf[0].X = x;
3040 ptf[0].Y = y;
3041 ptf[1].X = x + width;
3042 ptf[1].Y = y;
3043 ptf[2].X = x + width;
3044 ptf[2].Y = y + height;
3045 ptf[3].X = x;
3046 ptf[3].Y = y + height;
3048 save_state = SaveDC(graphics->hdc);
3049 EndPath(graphics->hdc);
3051 transform_and_round_points(graphics, pti, ptf, 4);
3053 BeginPath(graphics->hdc);
3054 Polygon(graphics->hdc, pti, 4);
3055 EndPath(graphics->hdc);
3057 brush_fill_path(graphics, brush);
3059 RestoreDC(graphics->hdc, save_state);
3061 return Ok;
3064 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3065 INT count)
3067 GpStatus ret;
3068 INT i;
3070 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3072 if(!rects)
3073 return InvalidParameter;
3075 for(i = 0; i < count; i++){
3076 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
3077 if(ret != Ok) return ret;
3080 return Ok;
3083 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3084 INT count)
3086 GpRectF *rectsF;
3087 GpStatus ret;
3088 INT i;
3090 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3092 if(!rects || count <= 0)
3093 return InvalidParameter;
3095 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3096 if(!rectsF)
3097 return OutOfMemory;
3099 for(i = 0; i < count; i++){
3100 rectsF[i].X = (REAL)rects[i].X;
3101 rectsF[i].Y = (REAL)rects[i].Y;
3102 rectsF[i].X = (REAL)rects[i].Width;
3103 rectsF[i].Height = (REAL)rects[i].Height;
3106 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3107 GdipFree(rectsF);
3109 return ret;
3112 /*****************************************************************************
3113 * GdipFillRegion [GDIPLUS.@]
3115 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3116 GpRegion* region)
3118 INT save_state;
3119 GpStatus status;
3120 HRGN hrgn;
3121 RECT rc;
3123 TRACE("(%p, %p, %p)\n", graphics, brush, region);
3125 if (!(graphics && brush && region))
3126 return InvalidParameter;
3128 if(graphics->busy)
3129 return ObjectBusy;
3131 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3132 if(status != Ok)
3133 return status;
3135 save_state = SaveDC(graphics->hdc);
3136 EndPath(graphics->hdc);
3138 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3140 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3142 BeginPath(graphics->hdc);
3143 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3144 EndPath(graphics->hdc);
3146 brush_fill_path(graphics, brush);
3149 RestoreDC(graphics->hdc, save_state);
3151 DeleteObject(hrgn);
3153 return Ok;
3156 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3158 TRACE("(%p,%u)\n", graphics, intention);
3160 if(!graphics)
3161 return InvalidParameter;
3163 if(graphics->busy)
3164 return ObjectBusy;
3166 /* We have no internal operation queue, so there's no need to clear it. */
3168 if (graphics->hdc)
3169 GdiFlush();
3171 return Ok;
3174 /*****************************************************************************
3175 * GdipGetClipBounds [GDIPLUS.@]
3177 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3179 TRACE("(%p, %p)\n", graphics, rect);
3181 if(!graphics)
3182 return InvalidParameter;
3184 if(graphics->busy)
3185 return ObjectBusy;
3187 return GdipGetRegionBounds(graphics->clip, graphics, rect);
3190 /*****************************************************************************
3191 * GdipGetClipBoundsI [GDIPLUS.@]
3193 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3195 TRACE("(%p, %p)\n", graphics, rect);
3197 if(!graphics)
3198 return InvalidParameter;
3200 if(graphics->busy)
3201 return ObjectBusy;
3203 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3206 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3207 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3208 CompositingMode *mode)
3210 TRACE("(%p, %p)\n", graphics, mode);
3212 if(!graphics || !mode)
3213 return InvalidParameter;
3215 if(graphics->busy)
3216 return ObjectBusy;
3218 *mode = graphics->compmode;
3220 return Ok;
3223 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3224 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3225 CompositingQuality *quality)
3227 TRACE("(%p, %p)\n", graphics, quality);
3229 if(!graphics || !quality)
3230 return InvalidParameter;
3232 if(graphics->busy)
3233 return ObjectBusy;
3235 *quality = graphics->compqual;
3237 return Ok;
3240 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3241 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3242 InterpolationMode *mode)
3244 TRACE("(%p, %p)\n", graphics, mode);
3246 if(!graphics || !mode)
3247 return InvalidParameter;
3249 if(graphics->busy)
3250 return ObjectBusy;
3252 *mode = graphics->interpolation;
3254 return Ok;
3257 /* FIXME: Need to handle color depths less than 24bpp */
3258 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3260 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
3262 if(!graphics || !argb)
3263 return InvalidParameter;
3265 if(graphics->busy)
3266 return ObjectBusy;
3268 return Ok;
3271 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3273 TRACE("(%p, %p)\n", graphics, scale);
3275 if(!graphics || !scale)
3276 return InvalidParameter;
3278 if(graphics->busy)
3279 return ObjectBusy;
3281 *scale = graphics->scale;
3283 return Ok;
3286 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3288 TRACE("(%p, %p)\n", graphics, unit);
3290 if(!graphics || !unit)
3291 return InvalidParameter;
3293 if(graphics->busy)
3294 return ObjectBusy;
3296 *unit = graphics->unit;
3298 return Ok;
3301 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3302 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3303 *mode)
3305 TRACE("(%p, %p)\n", graphics, mode);
3307 if(!graphics || !mode)
3308 return InvalidParameter;
3310 if(graphics->busy)
3311 return ObjectBusy;
3313 *mode = graphics->pixeloffset;
3315 return Ok;
3318 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3319 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3321 TRACE("(%p, %p)\n", graphics, mode);
3323 if(!graphics || !mode)
3324 return InvalidParameter;
3326 if(graphics->busy)
3327 return ObjectBusy;
3329 *mode = graphics->smoothing;
3331 return Ok;
3334 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3336 TRACE("(%p, %p)\n", graphics, contrast);
3338 if(!graphics || !contrast)
3339 return InvalidParameter;
3341 *contrast = graphics->textcontrast;
3343 return Ok;
3346 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3347 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3348 TextRenderingHint *hint)
3350 TRACE("(%p, %p)\n", graphics, hint);
3352 if(!graphics || !hint)
3353 return InvalidParameter;
3355 if(graphics->busy)
3356 return ObjectBusy;
3358 *hint = graphics->texthint;
3360 return Ok;
3363 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3365 GpRegion *clip_rgn;
3366 GpStatus stat;
3368 TRACE("(%p, %p)\n", graphics, rect);
3370 if(!graphics || !rect)
3371 return InvalidParameter;
3373 if(graphics->busy)
3374 return ObjectBusy;
3376 /* intersect window and graphics clipping regions */
3377 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3378 return stat;
3380 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3381 goto cleanup;
3383 /* get bounds of the region */
3384 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3386 cleanup:
3387 GdipDeleteRegion(clip_rgn);
3389 return stat;
3392 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3394 GpRectF rectf;
3395 GpStatus stat;
3397 TRACE("(%p, %p)\n", graphics, rect);
3399 if(!graphics || !rect)
3400 return InvalidParameter;
3402 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
3404 rect->X = roundr(rectf.X);
3405 rect->Y = roundr(rectf.Y);
3406 rect->Width = roundr(rectf.Width);
3407 rect->Height = roundr(rectf.Height);
3410 return stat;
3413 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3415 TRACE("(%p, %p)\n", graphics, matrix);
3417 if(!graphics || !matrix)
3418 return InvalidParameter;
3420 if(graphics->busy)
3421 return ObjectBusy;
3423 *matrix = *graphics->worldtrans;
3424 return Ok;
3427 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3429 GpSolidFill *brush;
3430 GpStatus stat;
3431 GpRectF wnd_rect;
3433 TRACE("(%p, %x)\n", graphics, color);
3435 if(!graphics)
3436 return InvalidParameter;
3438 if(graphics->busy)
3439 return ObjectBusy;
3441 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3442 return stat;
3444 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
3445 GdipDeleteBrush((GpBrush*)brush);
3446 return stat;
3449 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
3450 wnd_rect.Width, wnd_rect.Height);
3452 GdipDeleteBrush((GpBrush*)brush);
3454 return Ok;
3457 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3459 TRACE("(%p, %p)\n", graphics, res);
3461 if(!graphics || !res)
3462 return InvalidParameter;
3464 return GdipIsEmptyRegion(graphics->clip, graphics, res);
3467 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3469 GpStatus stat;
3470 GpRegion* rgn;
3471 GpPointF pt;
3473 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
3475 if(!graphics || !result)
3476 return InvalidParameter;
3478 if(graphics->busy)
3479 return ObjectBusy;
3481 pt.X = x;
3482 pt.Y = y;
3483 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3484 CoordinateSpaceWorld, &pt, 1)) != Ok)
3485 return stat;
3487 if((stat = GdipCreateRegion(&rgn)) != Ok)
3488 return stat;
3490 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3491 goto cleanup;
3493 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
3495 cleanup:
3496 GdipDeleteRegion(rgn);
3497 return stat;
3500 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3502 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
3505 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
3507 GpStatus stat;
3508 GpRegion* rgn;
3509 GpPointF pts[2];
3511 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
3513 if(!graphics || !result)
3514 return InvalidParameter;
3516 if(graphics->busy)
3517 return ObjectBusy;
3519 pts[0].X = x;
3520 pts[0].Y = y;
3521 pts[1].X = x + width;
3522 pts[1].Y = y + height;
3524 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3525 CoordinateSpaceWorld, pts, 2)) != Ok)
3526 return stat;
3528 pts[1].X -= pts[0].X;
3529 pts[1].Y -= pts[0].Y;
3531 if((stat = GdipCreateRegion(&rgn)) != Ok)
3532 return stat;
3534 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3535 goto cleanup;
3537 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
3539 cleanup:
3540 GdipDeleteRegion(rgn);
3541 return stat;
3544 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
3546 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
3549 typedef GpStatus (*gdip_format_string_callback)(GpGraphics *graphics,
3550 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3551 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3552 INT lineno, const RectF *bounds, void *user_data);
3554 static GpStatus gdip_format_string(GpGraphics *graphics,
3555 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3556 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3557 gdip_format_string_callback callback, void *user_data)
3559 WCHAR* stringdup;
3560 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
3561 nheight, lineend, lineno = 0;
3562 RectF bounds;
3563 StringAlignment halign;
3564 GpStatus stat = Ok;
3565 SIZE size;
3567 if(length == -1) length = lstrlenW(string);
3569 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3570 if(!stringdup) return OutOfMemory;
3572 nwidth = roundr(rect->Width);
3573 nheight = roundr(rect->Height);
3575 if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
3576 if (rect->Height >= INT_MAX || rect->Width < 0.5) nheight = INT_MAX;
3578 for(i = 0, j = 0; i < length; i++){
3579 /* FIXME: This makes the indexes passed to callback inaccurate. */
3580 if(!isprintW(string[i]) && (string[i] != '\n'))
3581 continue;
3583 stringdup[j] = string[i];
3584 j++;
3587 length = j;
3589 if (format) halign = format->align;
3590 else halign = StringAlignmentNear;
3592 while(sum < length){
3593 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3594 nwidth, &fit, NULL, &size);
3595 fitcpy = fit;
3597 if(fit == 0)
3598 break;
3600 for(lret = 0; lret < fit; lret++)
3601 if(*(stringdup + sum + lret) == '\n')
3602 break;
3604 /* Line break code (may look strange, but it imitates windows). */
3605 if(lret < fit)
3606 lineend = fit = lret; /* this is not an off-by-one error */
3607 else if(fit < (length - sum)){
3608 if(*(stringdup + sum + fit) == ' ')
3609 while(*(stringdup + sum + fit) == ' ')
3610 fit++;
3611 else
3612 while(*(stringdup + sum + fit - 1) != ' '){
3613 fit--;
3615 if(*(stringdup + sum + fit) == '\t')
3616 break;
3618 if(fit == 0){
3619 fit = fitcpy;
3620 break;
3623 lineend = fit;
3624 while(*(stringdup + sum + lineend - 1) == ' ' ||
3625 *(stringdup + sum + lineend - 1) == '\t')
3626 lineend--;
3628 else
3629 lineend = fit;
3631 GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3632 nwidth, &j, NULL, &size);
3634 bounds.Width = size.cx;
3636 if(height + size.cy > nheight)
3637 bounds.Height = nheight - (height + size.cy);
3638 else
3639 bounds.Height = size.cy;
3641 bounds.Y = rect->Y + height;
3643 switch (halign)
3645 case StringAlignmentNear:
3646 default:
3647 bounds.X = rect->X;
3648 break;
3649 case StringAlignmentCenter:
3650 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
3651 break;
3652 case StringAlignmentFar:
3653 bounds.X = rect->X + rect->Width - bounds.Width;
3654 break;
3657 stat = callback(graphics, stringdup, sum, lineend,
3658 font, rect, format, lineno, &bounds, user_data);
3660 if (stat != Ok)
3661 break;
3663 sum += fit + (lret < fitcpy ? 1 : 0);
3664 height += size.cy;
3665 lineno++;
3667 if(height > nheight)
3668 break;
3670 /* Stop if this was a linewrap (but not if it was a linebreak). */
3671 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3672 break;
3675 GdipFree(stringdup);
3677 return stat;
3680 struct measure_ranges_args {
3681 GpRegion **regions;
3684 static GpStatus measure_ranges_callback(GpGraphics *graphics,
3685 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3686 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3687 INT lineno, const RectF *bounds, void *user_data)
3689 int i;
3690 GpStatus stat = Ok;
3691 struct measure_ranges_args *args = user_data;
3693 for (i=0; i<format->range_count; i++)
3695 INT range_start = max(index, format->character_ranges[i].First);
3696 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
3697 if (range_start < range_end)
3699 GpRectF range_rect;
3700 SIZE range_size;
3702 range_rect.Y = bounds->Y;
3703 range_rect.Height = bounds->Height;
3705 GetTextExtentExPointW(graphics->hdc, string + index, range_start - index,
3706 INT_MAX, NULL, NULL, &range_size);
3707 range_rect.X = bounds->X + range_size.cx;
3709 GetTextExtentExPointW(graphics->hdc, string + index, range_end - index,
3710 INT_MAX, NULL, NULL, &range_size);
3711 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
3713 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
3714 if (stat != Ok)
3715 break;
3719 return stat;
3722 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3723 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3724 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3725 INT regionCount, GpRegion** regions)
3727 GpStatus stat;
3728 int i;
3729 HFONT oldfont;
3730 struct measure_ranges_args args;
3732 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
3733 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
3735 if (!(graphics && string && font && layoutRect && stringFormat && regions))
3736 return InvalidParameter;
3738 if (regionCount < stringFormat->range_count)
3739 return InvalidParameter;
3741 if (stringFormat->attr)
3742 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
3744 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3746 for (i=0; i<stringFormat->range_count; i++)
3748 stat = GdipSetEmpty(regions[i]);
3749 if (stat != Ok)
3750 return stat;
3753 args.regions = regions;
3755 stat = gdip_format_string(graphics, string, length, font, layoutRect, stringFormat,
3756 measure_ranges_callback, &args);
3758 DeleteObject(SelectObject(graphics->hdc, oldfont));
3760 return stat;
3763 struct measure_string_args {
3764 RectF *bounds;
3765 INT *codepointsfitted;
3766 INT *linesfilled;
3769 static GpStatus measure_string_callback(GpGraphics *graphics,
3770 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3771 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3772 INT lineno, const RectF *bounds, void *user_data)
3774 struct measure_string_args *args = user_data;
3776 if (bounds->Width > args->bounds->Width)
3777 args->bounds->Width = bounds->Width;
3779 if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
3780 args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
3782 if (args->codepointsfitted)
3783 *args->codepointsfitted = index + length;
3785 if (args->linesfilled)
3786 (*args->linesfilled)++;
3788 return Ok;
3791 /* Find the smallest rectangle that bounds the text when it is printed in rect
3792 * according to the format options listed in format. If rect has 0 width and
3793 * height, then just find the smallest rectangle that bounds the text when it's
3794 * printed at location (rect->X, rect-Y). */
3795 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3796 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3797 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3798 INT *codepointsfitted, INT *linesfilled)
3800 HFONT oldfont;
3801 struct measure_string_args args;
3803 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3804 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3805 bounds, codepointsfitted, linesfilled);
3807 if(!graphics || !string || !font || !rect || !bounds)
3808 return InvalidParameter;
3810 if(linesfilled) *linesfilled = 0;
3811 if(codepointsfitted) *codepointsfitted = 0;
3813 if(format)
3814 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3816 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3818 bounds->X = rect->X;
3819 bounds->Y = rect->Y;
3820 bounds->Width = 0.0;
3821 bounds->Height = 0.0;
3823 args.bounds = bounds;
3824 args.codepointsfitted = codepointsfitted;
3825 args.linesfilled = linesfilled;
3827 gdip_format_string(graphics, string, length, font, rect, format,
3828 measure_string_callback, &args);
3830 DeleteObject(SelectObject(graphics->hdc, oldfont));
3832 return Ok;
3835 struct draw_string_args {
3836 POINT drawbase;
3837 UINT drawflags;
3838 REAL ang_cos, ang_sin;
3841 static GpStatus draw_string_callback(GpGraphics *graphics,
3842 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3843 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3844 INT lineno, const RectF *bounds, void *user_data)
3846 struct draw_string_args *args = user_data;
3847 RECT drawcoord;
3849 drawcoord.left = drawcoord.right = args->drawbase.x + roundr(args->ang_sin * bounds->Y);
3850 drawcoord.top = drawcoord.bottom = args->drawbase.y + roundr(args->ang_cos * bounds->Y);
3852 DrawTextW(graphics->hdc, string + index, length, &drawcoord, args->drawflags);
3854 return Ok;
3857 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
3858 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
3859 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
3861 HRGN rgn = NULL;
3862 HFONT gdifont;
3863 LOGFONTW lfw;
3864 TEXTMETRICW textmet;
3865 GpPointF pt[3], rectcpy[4];
3866 POINT corners[4];
3867 REAL angle, rel_width, rel_height;
3868 INT offsety = 0, save_state;
3869 struct draw_string_args args;
3870 RectF scaled_rect;
3872 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
3873 length, font, debugstr_rectf(rect), format, brush);
3875 if(!graphics || !string || !font || !brush || !rect)
3876 return InvalidParameter;
3878 if((brush->bt != BrushTypeSolidColor)){
3879 FIXME("not implemented for given parameters\n");
3880 return NotImplemented;
3883 if(format){
3884 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3886 /* Should be no need to explicitly test for StringAlignmentNear as
3887 * that is default behavior if no alignment is passed. */
3888 if(format->vertalign != StringAlignmentNear){
3889 RectF bounds;
3890 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
3892 if(format->vertalign == StringAlignmentCenter)
3893 offsety = (rect->Height - bounds.Height) / 2;
3894 else if(format->vertalign == StringAlignmentFar)
3895 offsety = (rect->Height - bounds.Height);
3899 save_state = SaveDC(graphics->hdc);
3900 SetBkMode(graphics->hdc, TRANSPARENT);
3901 SetTextColor(graphics->hdc, brush->lb.lbColor);
3903 pt[0].X = 0.0;
3904 pt[0].Y = 0.0;
3905 pt[1].X = 1.0;
3906 pt[1].Y = 0.0;
3907 pt[2].X = 0.0;
3908 pt[2].Y = 1.0;
3909 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
3910 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
3911 args.ang_cos = cos(angle);
3912 args.ang_sin = sin(angle);
3913 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
3914 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
3915 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
3916 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
3918 rectcpy[3].X = rectcpy[0].X = rect->X;
3919 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
3920 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
3921 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
3922 transform_and_round_points(graphics, corners, rectcpy, 4);
3924 scaled_rect.X = 0.0;
3925 scaled_rect.Y = 0.0;
3926 scaled_rect.Width = rel_width * rect->Width;
3927 scaled_rect.Height = rel_height * rect->Height;
3929 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
3931 /* FIXME: If only the width or only the height is 0, we should probably still clip */
3932 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
3933 SelectClipRgn(graphics->hdc, rgn);
3936 /* Use gdi to find the font, then perform transformations on it (height,
3937 * width, angle). */
3938 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3939 GetTextMetricsW(graphics->hdc, &textmet);
3940 lfw = font->lfw;
3942 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
3943 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
3945 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
3947 gdifont = CreateFontIndirectW(&lfw);
3948 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
3950 if (!format || format->align == StringAlignmentNear)
3952 args.drawbase.x = corners[0].x;
3953 args.drawbase.y = corners[0].y;
3954 args.drawflags = DT_NOCLIP | DT_EXPANDTABS;
3956 else if (format->align == StringAlignmentCenter)
3958 args.drawbase.x = (corners[0].x + corners[1].x)/2;
3959 args.drawbase.y = (corners[0].y + corners[1].y)/2;
3960 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
3962 else /* (format->align == StringAlignmentFar) */
3964 args.drawbase.x = corners[1].x;
3965 args.drawbase.y = corners[1].y;
3966 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
3969 gdip_format_string(graphics, string, length, font, &scaled_rect, format,
3970 draw_string_callback, &args);
3972 DeleteObject(rgn);
3973 DeleteObject(gdifont);
3975 RestoreDC(graphics->hdc, save_state);
3977 return Ok;
3980 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
3982 TRACE("(%p)\n", graphics);
3984 if(!graphics)
3985 return InvalidParameter;
3987 if(graphics->busy)
3988 return ObjectBusy;
3990 return GdipSetInfinite(graphics->clip);
3993 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
3995 TRACE("(%p)\n", graphics);
3997 if(!graphics)
3998 return InvalidParameter;
4000 if(graphics->busy)
4001 return ObjectBusy;
4003 graphics->worldtrans->matrix[0] = 1.0;
4004 graphics->worldtrans->matrix[1] = 0.0;
4005 graphics->worldtrans->matrix[2] = 0.0;
4006 graphics->worldtrans->matrix[3] = 1.0;
4007 graphics->worldtrans->matrix[4] = 0.0;
4008 graphics->worldtrans->matrix[5] = 0.0;
4010 return Ok;
4013 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
4015 return GdipEndContainer(graphics, state);
4018 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
4019 GpMatrixOrder order)
4021 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
4023 if(!graphics)
4024 return InvalidParameter;
4026 if(graphics->busy)
4027 return ObjectBusy;
4029 return GdipRotateMatrix(graphics->worldtrans, angle, order);
4032 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
4034 return GdipBeginContainer2(graphics, state);
4037 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
4038 GraphicsContainer *state)
4040 GraphicsContainerItem *container;
4041 GpStatus sts;
4043 TRACE("(%p, %p)\n", graphics, state);
4045 if(!graphics || !state)
4046 return InvalidParameter;
4048 sts = init_container(&container, graphics);
4049 if(sts != Ok)
4050 return sts;
4052 list_add_head(&graphics->containers, &container->entry);
4053 *state = graphics->contid = container->contid;
4055 return Ok;
4058 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
4060 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4061 return NotImplemented;
4064 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
4066 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4067 return NotImplemented;
4070 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
4072 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
4073 return NotImplemented;
4076 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
4078 GpStatus sts;
4079 GraphicsContainerItem *container, *container2;
4081 TRACE("(%p, %x)\n", graphics, state);
4083 if(!graphics)
4084 return InvalidParameter;
4086 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
4087 if(container->contid == state)
4088 break;
4091 /* did not find a matching container */
4092 if(&container->entry == &graphics->containers)
4093 return Ok;
4095 sts = restore_container(graphics, container);
4096 if(sts != Ok)
4097 return sts;
4099 /* remove all of the containers on top of the found container */
4100 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
4101 if(container->contid == state)
4102 break;
4103 list_remove(&container->entry);
4104 delete_container(container);
4107 list_remove(&container->entry);
4108 delete_container(container);
4110 return Ok;
4113 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
4114 REAL sy, GpMatrixOrder order)
4116 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
4118 if(!graphics)
4119 return InvalidParameter;
4121 if(graphics->busy)
4122 return ObjectBusy;
4124 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
4127 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
4128 CombineMode mode)
4130 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
4132 if(!graphics || !srcgraphics)
4133 return InvalidParameter;
4135 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
4138 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
4139 CompositingMode mode)
4141 TRACE("(%p, %d)\n", graphics, mode);
4143 if(!graphics)
4144 return InvalidParameter;
4146 if(graphics->busy)
4147 return ObjectBusy;
4149 graphics->compmode = mode;
4151 return Ok;
4154 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
4155 CompositingQuality quality)
4157 TRACE("(%p, %d)\n", graphics, quality);
4159 if(!graphics)
4160 return InvalidParameter;
4162 if(graphics->busy)
4163 return ObjectBusy;
4165 graphics->compqual = quality;
4167 return Ok;
4170 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
4171 InterpolationMode mode)
4173 TRACE("(%p, %d)\n", graphics, mode);
4175 if(!graphics)
4176 return InvalidParameter;
4178 if(graphics->busy)
4179 return ObjectBusy;
4181 graphics->interpolation = mode;
4183 return Ok;
4186 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
4188 TRACE("(%p, %.2f)\n", graphics, scale);
4190 if(!graphics || (scale <= 0.0))
4191 return InvalidParameter;
4193 if(graphics->busy)
4194 return ObjectBusy;
4196 graphics->scale = scale;
4198 return Ok;
4201 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4203 TRACE("(%p, %d)\n", graphics, unit);
4205 if(!graphics)
4206 return InvalidParameter;
4208 if(graphics->busy)
4209 return ObjectBusy;
4211 if(unit == UnitWorld)
4212 return InvalidParameter;
4214 graphics->unit = unit;
4216 return Ok;
4219 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4220 mode)
4222 TRACE("(%p, %d)\n", graphics, mode);
4224 if(!graphics)
4225 return InvalidParameter;
4227 if(graphics->busy)
4228 return ObjectBusy;
4230 graphics->pixeloffset = mode;
4232 return Ok;
4235 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
4237 static int calls;
4239 TRACE("(%p,%i,%i)\n", graphics, x, y);
4241 if (!(calls++))
4242 FIXME("not implemented\n");
4244 return NotImplemented;
4247 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
4249 static int calls;
4251 TRACE("(%p,%p,%p)\n", graphics, x, y);
4253 if (!(calls++))
4254 FIXME("not implemented\n");
4256 *x = *y = 0;
4258 return NotImplemented;
4261 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
4263 TRACE("(%p, %d)\n", graphics, mode);
4265 if(!graphics)
4266 return InvalidParameter;
4268 if(graphics->busy)
4269 return ObjectBusy;
4271 graphics->smoothing = mode;
4273 return Ok;
4276 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
4278 TRACE("(%p, %d)\n", graphics, contrast);
4280 if(!graphics)
4281 return InvalidParameter;
4283 graphics->textcontrast = contrast;
4285 return Ok;
4288 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
4289 TextRenderingHint hint)
4291 TRACE("(%p, %d)\n", graphics, hint);
4293 if(!graphics)
4294 return InvalidParameter;
4296 if(graphics->busy)
4297 return ObjectBusy;
4299 graphics->texthint = hint;
4301 return Ok;
4304 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4306 TRACE("(%p, %p)\n", graphics, matrix);
4308 if(!graphics || !matrix)
4309 return InvalidParameter;
4311 if(graphics->busy)
4312 return ObjectBusy;
4314 GdipDeleteMatrix(graphics->worldtrans);
4315 return GdipCloneMatrix(matrix, &graphics->worldtrans);
4318 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
4319 REAL dy, GpMatrixOrder order)
4321 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
4323 if(!graphics)
4324 return InvalidParameter;
4326 if(graphics->busy)
4327 return ObjectBusy;
4329 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
4332 /*****************************************************************************
4333 * GdipSetClipHrgn [GDIPLUS.@]
4335 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
4337 GpRegion *region;
4338 GpStatus status;
4340 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
4342 if(!graphics)
4343 return InvalidParameter;
4345 status = GdipCreateRegionHrgn(hrgn, &region);
4346 if(status != Ok)
4347 return status;
4349 status = GdipSetClipRegion(graphics, region, mode);
4351 GdipDeleteRegion(region);
4352 return status;
4355 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
4357 TRACE("(%p, %p, %d)\n", graphics, path, mode);
4359 if(!graphics)
4360 return InvalidParameter;
4362 if(graphics->busy)
4363 return ObjectBusy;
4365 return GdipCombineRegionPath(graphics->clip, path, mode);
4368 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
4369 REAL width, REAL height,
4370 CombineMode mode)
4372 GpRectF rect;
4374 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
4376 if(!graphics)
4377 return InvalidParameter;
4379 if(graphics->busy)
4380 return ObjectBusy;
4382 rect.X = x;
4383 rect.Y = y;
4384 rect.Width = width;
4385 rect.Height = height;
4387 return GdipCombineRegionRect(graphics->clip, &rect, mode);
4390 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
4391 INT width, INT height,
4392 CombineMode mode)
4394 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
4396 if(!graphics)
4397 return InvalidParameter;
4399 if(graphics->busy)
4400 return ObjectBusy;
4402 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
4405 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
4406 CombineMode mode)
4408 TRACE("(%p, %p, %d)\n", graphics, region, mode);
4410 if(!graphics || !region)
4411 return InvalidParameter;
4413 if(graphics->busy)
4414 return ObjectBusy;
4416 return GdipCombineRegionRegion(graphics->clip, region, mode);
4419 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
4420 UINT limitDpi)
4422 static int calls;
4424 TRACE("(%p,%u)\n", metafile, limitDpi);
4426 if(!(calls++))
4427 FIXME("not implemented\n");
4429 return NotImplemented;
4432 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
4433 INT count)
4435 INT save_state;
4436 POINT *pti;
4438 TRACE("(%p, %p, %d)\n", graphics, points, count);
4440 if(!graphics || !pen || count<=0)
4441 return InvalidParameter;
4443 if(graphics->busy)
4444 return ObjectBusy;
4446 pti = GdipAlloc(sizeof(POINT) * count);
4448 save_state = prepare_dc(graphics, pen);
4449 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
4451 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
4452 Polygon(graphics->hdc, pti, count);
4454 restore_dc(graphics, save_state);
4455 GdipFree(pti);
4457 return Ok;
4460 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
4461 INT count)
4463 GpStatus ret;
4464 GpPointF *ptf;
4465 INT i;
4467 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
4469 if(count<=0) return InvalidParameter;
4470 ptf = GdipAlloc(sizeof(GpPointF) * count);
4472 for(i = 0;i < count; i++){
4473 ptf[i].X = (REAL)points[i].X;
4474 ptf[i].Y = (REAL)points[i].Y;
4477 ret = GdipDrawPolygon(graphics,pen,ptf,count);
4478 GdipFree(ptf);
4480 return ret;
4483 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
4485 TRACE("(%p, %p)\n", graphics, dpi);
4487 if(!graphics || !dpi)
4488 return InvalidParameter;
4490 if(graphics->busy)
4491 return ObjectBusy;
4493 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
4495 return Ok;
4498 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
4500 TRACE("(%p, %p)\n", graphics, dpi);
4502 if(!graphics || !dpi)
4503 return InvalidParameter;
4505 if(graphics->busy)
4506 return ObjectBusy;
4508 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
4510 return Ok;
4513 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
4514 GpMatrixOrder order)
4516 GpMatrix m;
4517 GpStatus ret;
4519 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
4521 if(!graphics || !matrix)
4522 return InvalidParameter;
4524 if(graphics->busy)
4525 return ObjectBusy;
4527 m = *(graphics->worldtrans);
4529 ret = GdipMultiplyMatrix(&m, matrix, order);
4530 if(ret == Ok)
4531 *(graphics->worldtrans) = m;
4533 return ret;
4536 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
4538 TRACE("(%p, %p)\n", graphics, hdc);
4540 if(!graphics || !hdc)
4541 return InvalidParameter;
4543 if(graphics->busy)
4544 return ObjectBusy;
4546 *hdc = graphics->hdc;
4547 graphics->busy = TRUE;
4549 return Ok;
4552 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
4554 TRACE("(%p, %p)\n", graphics, hdc);
4556 if(!graphics)
4557 return InvalidParameter;
4559 if(graphics->hdc != hdc || !(graphics->busy))
4560 return InvalidParameter;
4562 graphics->busy = FALSE;
4564 return Ok;
4567 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
4569 GpRegion *clip;
4570 GpStatus status;
4572 TRACE("(%p, %p)\n", graphics, region);
4574 if(!graphics || !region)
4575 return InvalidParameter;
4577 if(graphics->busy)
4578 return ObjectBusy;
4580 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
4581 return status;
4583 /* free everything except root node and header */
4584 delete_element(&region->node);
4585 memcpy(region, clip, sizeof(GpRegion));
4586 GdipFree(clip);
4588 return Ok;
4591 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
4592 GpCoordinateSpace src_space, GpPointF *points, INT count)
4594 GpMatrix *matrix;
4595 GpStatus stat;
4596 REAL unitscale;
4598 if(!graphics || !points || count <= 0)
4599 return InvalidParameter;
4601 if(graphics->busy)
4602 return ObjectBusy;
4604 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4606 if (src_space == dst_space) return Ok;
4608 stat = GdipCreateMatrix(&matrix);
4609 if (stat == Ok)
4611 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
4613 if(graphics->unit != UnitDisplay)
4614 unitscale *= graphics->scale;
4616 /* transform from src_space to CoordinateSpacePage */
4617 switch (src_space)
4619 case CoordinateSpaceWorld:
4620 GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
4621 break;
4622 case CoordinateSpacePage:
4623 break;
4624 case CoordinateSpaceDevice:
4625 GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
4626 break;
4629 /* transform from CoordinateSpacePage to dst_space */
4630 switch (dst_space)
4632 case CoordinateSpaceWorld:
4634 GpMatrix *inverted_transform;
4635 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
4636 if (stat == Ok)
4638 stat = GdipInvertMatrix(inverted_transform);
4639 if (stat == Ok)
4640 GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
4641 GdipDeleteMatrix(inverted_transform);
4643 break;
4645 case CoordinateSpacePage:
4646 break;
4647 case CoordinateSpaceDevice:
4648 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
4649 break;
4652 if (stat == Ok)
4653 stat = GdipTransformMatrixPoints(matrix, points, count);
4655 GdipDeleteMatrix(matrix);
4658 return stat;
4661 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
4662 GpCoordinateSpace src_space, GpPoint *points, INT count)
4664 GpPointF *pointsF;
4665 GpStatus ret;
4666 INT i;
4668 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4670 if(count <= 0)
4671 return InvalidParameter;
4673 pointsF = GdipAlloc(sizeof(GpPointF) * count);
4674 if(!pointsF)
4675 return OutOfMemory;
4677 for(i = 0; i < count; i++){
4678 pointsF[i].X = (REAL)points[i].X;
4679 pointsF[i].Y = (REAL)points[i].Y;
4682 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
4684 if(ret == Ok)
4685 for(i = 0; i < count; i++){
4686 points[i].X = roundr(pointsF[i].X);
4687 points[i].Y = roundr(pointsF[i].Y);
4689 GdipFree(pointsF);
4691 return ret;
4694 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
4696 static int calls;
4698 TRACE("\n");
4700 if (!calls++)
4701 FIXME("stub\n");
4703 return NULL;
4706 /*****************************************************************************
4707 * GdipTranslateClip [GDIPLUS.@]
4709 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
4711 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
4713 if(!graphics)
4714 return InvalidParameter;
4716 if(graphics->busy)
4717 return ObjectBusy;
4719 return GdipTranslateRegion(graphics->clip, dx, dy);
4722 /*****************************************************************************
4723 * GdipTranslateClipI [GDIPLUS.@]
4725 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
4727 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
4729 if(!graphics)
4730 return InvalidParameter;
4732 if(graphics->busy)
4733 return ObjectBusy;
4735 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
4739 /*****************************************************************************
4740 * GdipMeasureDriverString [GDIPLUS.@]
4742 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4743 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
4744 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
4746 FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
4747 return NotImplemented;
4750 /*****************************************************************************
4751 * GdipDrawDriverString [GDIPLUS.@]
4753 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4754 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
4755 GDIPCONST PointF *positions, INT flags,
4756 GDIPCONST GpMatrix *matrix )
4758 FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4759 return NotImplemented;
4762 GpStatus WINGDIPAPI GdipRecordMetafile(HDC hdc, EmfType type, GDIPCONST GpRectF *frameRect,
4763 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4765 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
4766 return NotImplemented;
4769 /*****************************************************************************
4770 * GdipRecordMetafileI [GDIPLUS.@]
4772 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
4773 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4775 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
4776 return NotImplemented;
4779 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
4780 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4782 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
4783 return NotImplemented;
4786 /*****************************************************************************
4787 * GdipIsVisibleClipEmpty [GDIPLUS.@]
4789 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
4791 GpStatus stat;
4792 GpRegion* rgn;
4794 TRACE("(%p, %p)\n", graphics, res);
4796 if((stat = GdipCreateRegion(&rgn)) != Ok)
4797 return stat;
4799 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4800 goto cleanup;
4802 stat = GdipIsEmptyRegion(rgn, graphics, res);
4804 cleanup:
4805 GdipDeleteRegion(rgn);
4806 return stat;