gdiplus: Implement transform matrix for line gradient brushes.
[wine.git] / dlls / gdiplus / graphics.c
blob80206c93938c9257bc57b52e7ce966dc39b4d7c8
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 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 /* Converts from gdiplus path point type to gdi path point type. */
55 static BYTE convert_path_point_type(BYTE type)
57 BYTE ret;
59 switch(type & PathPointTypePathTypeMask){
60 case PathPointTypeBezier:
61 ret = PT_BEZIERTO;
62 break;
63 case PathPointTypeLine:
64 ret = PT_LINETO;
65 break;
66 case PathPointTypeStart:
67 ret = PT_MOVETO;
68 break;
69 default:
70 ERR("Bad point type\n");
71 return 0;
74 if(type & PathPointTypeCloseSubpath)
75 ret |= PT_CLOSEFIGURE;
77 return ret;
80 static COLORREF get_gdi_brush_color(const GpBrush *brush)
82 ARGB argb;
84 switch (brush->bt)
86 case BrushTypeSolidColor:
88 const GpSolidFill *sf = (const GpSolidFill *)brush;
89 argb = sf->color;
90 break;
92 case BrushTypeHatchFill:
94 const GpHatch *hatch = (const GpHatch *)brush;
95 argb = hatch->forecol;
96 break;
98 case BrushTypeLinearGradient:
100 const GpLineGradient *line = (const GpLineGradient *)brush;
101 argb = line->startcolor;
102 break;
104 case BrushTypePathGradient:
106 const GpPathGradient *grad = (const GpPathGradient *)brush;
107 argb = grad->centercolor;
108 break;
110 default:
111 FIXME("unhandled brush type %d\n", brush->bt);
112 argb = 0;
113 break;
115 return ARGB2COLORREF(argb);
118 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
120 HBITMAP hbmp;
121 BITMAPINFOHEADER bmih;
122 DWORD *bits;
123 int x, y;
125 bmih.biSize = sizeof(bmih);
126 bmih.biWidth = 8;
127 bmih.biHeight = 8;
128 bmih.biPlanes = 1;
129 bmih.biBitCount = 32;
130 bmih.biCompression = BI_RGB;
131 bmih.biSizeImage = 0;
133 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
134 if (hbmp)
136 const char *hatch_data;
138 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
140 for (y = 0; y < 8; y++)
142 for (x = 0; x < 8; x++)
144 if (hatch_data[y] & (0x80 >> x))
145 bits[y * 8 + x] = hatch->forecol;
146 else
147 bits[y * 8 + x] = hatch->backcol;
151 else
153 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
155 for (y = 0; y < 64; y++)
156 bits[y] = hatch->forecol;
160 return hbmp;
163 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
165 switch (brush->bt)
167 case BrushTypeSolidColor:
169 const GpSolidFill *sf = (const GpSolidFill *)brush;
170 lb->lbStyle = BS_SOLID;
171 lb->lbColor = ARGB2COLORREF(sf->color);
172 lb->lbHatch = 0;
173 return Ok;
176 case BrushTypeHatchFill:
178 const GpHatch *hatch = (const GpHatch *)brush;
179 HBITMAP hbmp;
181 hbmp = create_hatch_bitmap(hatch);
182 if (!hbmp) return OutOfMemory;
184 lb->lbStyle = BS_PATTERN;
185 lb->lbColor = 0;
186 lb->lbHatch = (ULONG_PTR)hbmp;
187 return Ok;
190 default:
191 FIXME("unhandled brush type %d\n", brush->bt);
192 lb->lbStyle = BS_SOLID;
193 lb->lbColor = get_gdi_brush_color(brush);
194 lb->lbHatch = 0;
195 return Ok;
199 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
201 switch (lb->lbStyle)
203 case BS_PATTERN:
204 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
205 break;
207 return Ok;
210 static HBRUSH create_gdi_brush(const GpBrush *brush)
212 LOGBRUSH lb;
213 HBRUSH gdibrush;
215 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
217 gdibrush = CreateBrushIndirect(&lb);
218 free_gdi_logbrush(&lb);
220 return gdibrush;
223 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
225 LOGBRUSH lb;
226 HPEN gdipen;
227 REAL width;
228 INT save_state, i, numdashes;
229 GpPointF pt[2];
230 DWORD dash_array[MAX_DASHLEN];
232 save_state = SaveDC(graphics->hdc);
234 EndPath(graphics->hdc);
236 if(pen->unit == UnitPixel){
237 width = pen->width;
239 else{
240 /* Get an estimate for the amount the pen width is affected by the world
241 * transform. (This is similar to what some of the wine drivers do.) */
242 pt[0].X = 0.0;
243 pt[0].Y = 0.0;
244 pt[1].X = 1.0;
245 pt[1].Y = 1.0;
246 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
247 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
248 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
250 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
251 width *= graphics->scale;
254 if(pen->dash == DashStyleCustom){
255 numdashes = min(pen->numdashes, MAX_DASHLEN);
257 TRACE("dashes are: ");
258 for(i = 0; i < numdashes; i++){
259 dash_array[i] = gdip_round(width * pen->dashes[i]);
260 TRACE("%d, ", dash_array[i]);
262 TRACE("\n and the pen style is %x\n", pen->style);
264 create_gdi_logbrush(pen->brush, &lb);
265 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
266 numdashes, dash_array);
267 free_gdi_logbrush(&lb);
269 else
271 create_gdi_logbrush(pen->brush, &lb);
272 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
273 free_gdi_logbrush(&lb);
276 SelectObject(graphics->hdc, gdipen);
278 return save_state;
281 static void restore_dc(GpGraphics *graphics, INT state)
283 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
284 RestoreDC(graphics->hdc, state);
287 /* This helper applies all the changes that the points listed in ptf need in
288 * order to be drawn on the device context. In the end, this should include at
289 * least:
290 * -scaling by page unit
291 * -applying world transformation
292 * -converting from float to int
293 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
294 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
295 * gdi to draw, and these functions would irreparably mess with line widths.
297 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
298 GpPointF *ptf, INT count)
300 REAL scale_x, scale_y;
301 GpMatrix matrix;
302 int i;
304 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
305 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
307 /* apply page scale */
308 if(graphics->unit != UnitDisplay)
310 scale_x *= graphics->scale;
311 scale_y *= graphics->scale;
314 matrix = graphics->worldtrans;
315 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
316 GdipTransformMatrixPoints(&matrix, ptf, count);
318 for(i = 0; i < count; i++){
319 pti[i].x = gdip_round(ptf[i].X);
320 pti[i].y = gdip_round(ptf[i].Y);
324 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
325 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
327 if (GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
328 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
330 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
332 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
333 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
335 else
337 BLENDFUNCTION bf;
339 bf.BlendOp = AC_SRC_OVER;
340 bf.BlendFlags = 0;
341 bf.SourceConstantAlpha = 255;
342 bf.AlphaFormat = AC_SRC_ALPHA;
344 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
345 hdc, src_x, src_y, src_width, src_height, bf);
349 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
351 /* clipping region is in device coords */
352 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
355 /* Draw ARGB data to the given graphics object */
356 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
357 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
359 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
360 INT x, y;
362 for (y=0; y<src_height; y++)
364 for (x=0; x<src_width; x++)
366 ARGB dst_color, src_color;
367 src_color = ((ARGB*)(src + src_stride * y))[x];
369 if (!(src_color & 0xff000000))
370 continue;
372 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
373 if (fmt & PixelFormatPAlpha)
374 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
375 else
376 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
380 return Ok;
383 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
384 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
386 HDC hdc;
387 HBITMAP hbitmap;
388 BITMAPINFOHEADER bih;
389 BYTE *temp_bits;
391 hdc = CreateCompatibleDC(0);
393 bih.biSize = sizeof(BITMAPINFOHEADER);
394 bih.biWidth = src_width;
395 bih.biHeight = -src_height;
396 bih.biPlanes = 1;
397 bih.biBitCount = 32;
398 bih.biCompression = BI_RGB;
399 bih.biSizeImage = 0;
400 bih.biXPelsPerMeter = 0;
401 bih.biYPelsPerMeter = 0;
402 bih.biClrUsed = 0;
403 bih.biClrImportant = 0;
405 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
406 (void**)&temp_bits, NULL, 0);
408 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
409 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
410 fmt & PixelFormatPAlpha)
411 memcpy(temp_bits, src, src_width * src_height * 4);
412 else
413 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
414 4 * src_width, src, src_stride);
416 SelectObject(hdc, hbitmap);
417 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
418 hdc, 0, 0, src_width, src_height);
419 DeleteDC(hdc);
420 DeleteObject(hbitmap);
422 return Ok;
425 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
426 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
428 GpStatus stat=Ok;
430 if (graphics->image && graphics->image->type == ImageTypeBitmap)
432 DWORD i;
433 int size;
434 RGNDATA *rgndata;
435 RECT *rects;
436 HRGN hrgn, visible_rgn;
438 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
439 if (!hrgn)
440 return OutOfMemory;
442 stat = get_clip_hrgn(graphics, &visible_rgn);
443 if (stat != Ok)
445 DeleteObject(hrgn);
446 return stat;
449 if (visible_rgn)
451 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
452 DeleteObject(visible_rgn);
455 if (hregion)
456 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
458 size = GetRegionData(hrgn, 0, NULL);
460 rgndata = heap_alloc_zero(size);
461 if (!rgndata)
463 DeleteObject(hrgn);
464 return OutOfMemory;
467 GetRegionData(hrgn, size, rgndata);
469 rects = (RECT*)rgndata->Buffer;
471 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
473 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
474 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
475 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
476 src_stride, fmt);
479 heap_free(rgndata);
481 DeleteObject(hrgn);
483 return stat;
485 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
487 ERR("This should not be used for metafiles; fix caller\n");
488 return NotImplemented;
490 else
492 HRGN hrgn;
493 int save;
495 stat = get_clip_hrgn(graphics, &hrgn);
497 if (stat != Ok)
498 return stat;
500 save = SaveDC(graphics->hdc);
502 if (hrgn)
503 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
505 if (hregion)
506 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
508 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
509 src_height, src_stride, fmt);
511 RestoreDC(graphics->hdc, save);
513 DeleteObject(hrgn);
515 return stat;
519 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
520 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
522 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
525 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
527 INT start_a, end_a, final_a;
528 INT pos;
530 pos = gdip_round(position * 0xff);
532 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
533 end_a = ((end >> 24) & 0xff) * pos;
535 final_a = start_a + end_a;
537 if (final_a < 0xff) return 0;
539 return (final_a / 0xff) << 24 |
540 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
541 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
542 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
545 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
547 REAL blendfac;
549 /* clamp to between 0.0 and 1.0, using the wrap mode */
550 position = (position - brush->rect.X) / brush->rect.Width;
551 if (brush->wrap == WrapModeTile)
553 position = fmodf(position, 1.0f);
554 if (position < 0.0f) position += 1.0f;
556 else /* WrapModeFlip* */
558 position = fmodf(position, 2.0f);
559 if (position < 0.0f) position += 2.0f;
560 if (position > 1.0f) position = 2.0f - position;
563 if (brush->blendcount == 1)
564 blendfac = position;
565 else
567 int i=1;
568 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
569 REAL range;
571 /* locate the blend positions surrounding this position */
572 while (position > brush->blendpos[i])
573 i++;
575 /* interpolate between the blend positions */
576 left_blendpos = brush->blendpos[i-1];
577 left_blendfac = brush->blendfac[i-1];
578 right_blendpos = brush->blendpos[i];
579 right_blendfac = brush->blendfac[i];
580 range = right_blendpos - left_blendpos;
581 blendfac = (left_blendfac * (right_blendpos - position) +
582 right_blendfac * (position - left_blendpos)) / range;
585 if (brush->pblendcount == 0)
586 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
587 else
589 int i=1;
590 ARGB left_blendcolor, right_blendcolor;
591 REAL left_blendpos, right_blendpos;
593 /* locate the blend colors surrounding this position */
594 while (blendfac > brush->pblendpos[i])
595 i++;
597 /* interpolate between the blend colors */
598 left_blendpos = brush->pblendpos[i-1];
599 left_blendcolor = brush->pblendcolor[i-1];
600 right_blendpos = brush->pblendpos[i];
601 right_blendcolor = brush->pblendcolor[i];
602 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
603 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
607 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
609 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
610 BOOL identity = TRUE;
611 int i, j;
613 for (i=0; i<4; i++)
614 for (j=0; j<5; j++)
616 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
617 identity = FALSE;
618 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
621 return identity;
624 static ARGB transform_color(ARGB color, int matrix[5][5])
626 int val[5], res[4];
627 int i, j;
628 unsigned char a, r, g, b;
630 val[0] = ((color >> 16) & 0xff); /* red */
631 val[1] = ((color >> 8) & 0xff); /* green */
632 val[2] = (color & 0xff); /* blue */
633 val[3] = ((color >> 24) & 0xff); /* alpha */
634 val[4] = 255; /* translation */
636 for (i=0; i<4; i++)
638 res[i] = 0;
640 for (j=0; j<5; j++)
641 res[i] += matrix[j][i] * val[j];
644 a = min(max(res[3] / 256, 0), 255);
645 r = min(max(res[0] / 256, 0), 255);
646 g = min(max(res[1] / 256, 0), 255);
647 b = min(max(res[2] / 256, 0), 255);
649 return (a << 24) | (r << 16) | (g << 8) | b;
652 static BOOL color_is_gray(ARGB color)
654 unsigned char r, g, b;
656 r = (color >> 16) & 0xff;
657 g = (color >> 8) & 0xff;
658 b = color & 0xff;
660 return (r == g) && (g == b);
663 /* returns preferred pixel format for the applied attributes */
664 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
665 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
667 UINT x, y;
668 INT i;
670 if (attributes->colorkeys[type].enabled ||
671 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
673 const struct color_key *key;
674 BYTE min_blue, min_green, min_red;
675 BYTE max_blue, max_green, max_red;
677 if (!data || fmt != PixelFormat32bppARGB)
678 return PixelFormat32bppARGB;
680 if (attributes->colorkeys[type].enabled)
681 key = &attributes->colorkeys[type];
682 else
683 key = &attributes->colorkeys[ColorAdjustTypeDefault];
685 min_blue = key->low&0xff;
686 min_green = (key->low>>8)&0xff;
687 min_red = (key->low>>16)&0xff;
689 max_blue = key->high&0xff;
690 max_green = (key->high>>8)&0xff;
691 max_red = (key->high>>16)&0xff;
693 for (x=0; x<width; x++)
694 for (y=0; y<height; y++)
696 ARGB *src_color;
697 BYTE blue, green, red;
698 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
699 blue = *src_color&0xff;
700 green = (*src_color>>8)&0xff;
701 red = (*src_color>>16)&0xff;
702 if (blue >= min_blue && green >= min_green && red >= min_red &&
703 blue <= max_blue && green <= max_green && red <= max_red)
704 *src_color = 0x00000000;
708 if (attributes->colorremaptables[type].enabled ||
709 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
711 const struct color_remap_table *table;
713 if (!data || fmt != PixelFormat32bppARGB)
714 return PixelFormat32bppARGB;
716 if (attributes->colorremaptables[type].enabled)
717 table = &attributes->colorremaptables[type];
718 else
719 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
721 for (x=0; x<width; x++)
722 for (y=0; y<height; y++)
724 ARGB *src_color;
725 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
726 for (i=0; i<table->mapsize; i++)
728 if (*src_color == table->colormap[i].oldColor.Argb)
730 *src_color = table->colormap[i].newColor.Argb;
731 break;
737 if (attributes->colormatrices[type].enabled ||
738 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
740 const struct color_matrix *colormatrices;
741 int color_matrix[5][5];
742 int gray_matrix[5][5];
743 BOOL identity;
745 if (!data || fmt != PixelFormat32bppARGB)
746 return PixelFormat32bppARGB;
748 if (attributes->colormatrices[type].enabled)
749 colormatrices = &attributes->colormatrices[type];
750 else
751 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
753 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
755 if (colormatrices->flags == ColorMatrixFlagsAltGray)
756 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
758 if (!identity)
760 for (x=0; x<width; x++)
762 for (y=0; y<height; y++)
764 ARGB *src_color;
765 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
767 if (colormatrices->flags == ColorMatrixFlagsDefault ||
768 !color_is_gray(*src_color))
770 *src_color = transform_color(*src_color, color_matrix);
772 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
774 *src_color = transform_color(*src_color, gray_matrix);
781 if (attributes->gamma_enabled[type] ||
782 attributes->gamma_enabled[ColorAdjustTypeDefault])
784 REAL gamma;
786 if (!data || fmt != PixelFormat32bppARGB)
787 return PixelFormat32bppARGB;
789 if (attributes->gamma_enabled[type])
790 gamma = attributes->gamma[type];
791 else
792 gamma = attributes->gamma[ColorAdjustTypeDefault];
794 for (x=0; x<width; x++)
795 for (y=0; y<height; y++)
797 ARGB *src_color;
798 BYTE blue, green, red;
799 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
801 blue = *src_color&0xff;
802 green = (*src_color>>8)&0xff;
803 red = (*src_color>>16)&0xff;
805 /* FIXME: We should probably use a table for this. */
806 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
807 green = floorf(powf(green / 255.0, gamma) * 255.0);
808 red = floorf(powf(red / 255.0, gamma) * 255.0);
810 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
814 return fmt;
817 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
818 * bitmap that contains all the pixels we may need to draw it. */
819 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
820 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
821 GpRect *rect)
823 INT left, top, right, bottom;
825 switch (interpolation)
827 case InterpolationModeHighQualityBilinear:
828 case InterpolationModeHighQualityBicubic:
829 /* FIXME: Include a greater range for the prefilter? */
830 case InterpolationModeBicubic:
831 case InterpolationModeBilinear:
832 left = (INT)(floorf(srcx));
833 top = (INT)(floorf(srcy));
834 right = (INT)(ceilf(srcx+srcwidth));
835 bottom = (INT)(ceilf(srcy+srcheight));
836 break;
837 case InterpolationModeNearestNeighbor:
838 default:
839 left = gdip_round(srcx);
840 top = gdip_round(srcy);
841 right = gdip_round(srcx+srcwidth);
842 bottom = gdip_round(srcy+srcheight);
843 break;
846 if (wrap == WrapModeClamp)
848 if (left < 0)
849 left = 0;
850 if (top < 0)
851 top = 0;
852 if (right >= bitmap->width)
853 right = bitmap->width-1;
854 if (bottom >= bitmap->height)
855 bottom = bitmap->height-1;
856 if (bottom < top || right < left)
857 /* entirely outside image, just sample a pixel so we don't have to
858 * special-case this later */
859 left = top = right = bottom = 0;
861 else
863 /* In some cases we can make the rectangle smaller here, but the logic
864 * is hard to get right, and tiling suggests we're likely to use the
865 * entire source image. */
866 if (left < 0 || right >= bitmap->width)
868 left = 0;
869 right = bitmap->width-1;
872 if (top < 0 || bottom >= bitmap->height)
874 top = 0;
875 bottom = bitmap->height-1;
879 rect->X = left;
880 rect->Y = top;
881 rect->Width = right - left + 1;
882 rect->Height = bottom - top + 1;
885 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
886 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
888 if (attributes->wrap == WrapModeClamp)
890 if (x < 0 || y < 0 || x >= width || y >= height)
891 return attributes->outside_color;
893 else
895 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
896 if (x < 0)
897 x = width*2 + x % (width * 2);
898 if (y < 0)
899 y = height*2 + y % (height * 2);
901 if ((attributes->wrap & 1) == 1)
903 /* Flip X */
904 if ((x / width) % 2 == 0)
905 x = x % width;
906 else
907 x = width - 1 - x % width;
909 else
910 x = x % width;
912 if ((attributes->wrap & 2) == 2)
914 /* Flip Y */
915 if ((y / height) % 2 == 0)
916 y = y % height;
917 else
918 y = height - 1 - y % height;
920 else
921 y = y % height;
924 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
926 ERR("out of range pixel requested\n");
927 return 0xffcd0084;
930 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
933 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
934 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
935 InterpolationMode interpolation, PixelOffsetMode offset_mode)
937 static int fixme;
939 switch (interpolation)
941 default:
942 if (!fixme++)
943 FIXME("Unimplemented interpolation %i\n", interpolation);
944 /* fall-through */
945 case InterpolationModeBilinear:
947 REAL leftxf, topyf;
948 INT leftx, rightx, topy, bottomy;
949 ARGB topleft, topright, bottomleft, bottomright;
950 ARGB top, bottom;
951 float x_offset;
953 leftxf = floorf(point->X);
954 leftx = (INT)leftxf;
955 rightx = (INT)ceilf(point->X);
956 topyf = floorf(point->Y);
957 topy = (INT)topyf;
958 bottomy = (INT)ceilf(point->Y);
960 if (leftx == rightx && topy == bottomy)
961 return sample_bitmap_pixel(src_rect, bits, width, height,
962 leftx, topy, attributes);
964 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
965 leftx, topy, attributes);
966 topright = sample_bitmap_pixel(src_rect, bits, width, height,
967 rightx, topy, attributes);
968 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
969 leftx, bottomy, attributes);
970 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
971 rightx, bottomy, attributes);
973 x_offset = point->X - leftxf;
974 top = blend_colors(topleft, topright, x_offset);
975 bottom = blend_colors(bottomleft, bottomright, x_offset);
977 return blend_colors(top, bottom, point->Y - topyf);
979 case InterpolationModeNearestNeighbor:
981 FLOAT pixel_offset;
982 switch (offset_mode)
984 default:
985 case PixelOffsetModeNone:
986 case PixelOffsetModeHighSpeed:
987 pixel_offset = 0.5;
988 break;
990 case PixelOffsetModeHalf:
991 case PixelOffsetModeHighQuality:
992 pixel_offset = 0.0;
993 break;
995 return sample_bitmap_pixel(src_rect, bits, width, height,
996 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1002 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1004 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1007 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1008 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1010 switch (brush->bt)
1012 case BrushTypeSolidColor:
1014 if (is_fill)
1015 return TRUE;
1016 else
1018 /* cannot draw semi-transparent colors */
1019 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1022 case BrushTypeHatchFill:
1024 GpHatch *hatch = (GpHatch*)brush;
1025 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1026 ((hatch->backcol & 0xff000000) == 0xff000000);
1028 case BrushTypeLinearGradient:
1029 case BrushTypeTextureFill:
1030 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1031 default:
1032 return FALSE;
1036 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1038 switch (brush->bt)
1040 case BrushTypeSolidColor:
1042 GpSolidFill *fill = (GpSolidFill*)brush;
1043 HBITMAP bmp = ARGB2BMP(fill->color);
1045 if (bmp)
1047 RECT rc;
1048 /* partially transparent fill */
1050 SelectClipPath(graphics->hdc, RGN_AND);
1051 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1053 HDC hdc = CreateCompatibleDC(NULL);
1055 if (!hdc) break;
1057 SelectObject(hdc, bmp);
1058 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1059 hdc, 0, 0, 1, 1);
1060 DeleteDC(hdc);
1063 DeleteObject(bmp);
1064 break;
1066 /* else fall through */
1068 default:
1070 HBRUSH gdibrush, old_brush;
1072 gdibrush = create_gdi_brush(brush);
1073 if (!gdibrush) return;
1075 old_brush = SelectObject(graphics->hdc, gdibrush);
1076 FillPath(graphics->hdc);
1077 SelectObject(graphics->hdc, old_brush);
1078 DeleteObject(gdibrush);
1079 break;
1084 static BOOL brush_can_fill_pixels(GpBrush *brush)
1086 switch (brush->bt)
1088 case BrushTypeSolidColor:
1089 case BrushTypeHatchFill:
1090 case BrushTypeLinearGradient:
1091 case BrushTypeTextureFill:
1092 case BrushTypePathGradient:
1093 return TRUE;
1094 default:
1095 return FALSE;
1099 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1100 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1102 switch (brush->bt)
1104 case BrushTypeSolidColor:
1106 int x, y;
1107 GpSolidFill *fill = (GpSolidFill*)brush;
1108 for (x=0; x<fill_area->Width; x++)
1109 for (y=0; y<fill_area->Height; y++)
1110 argb_pixels[x + y*cdwStride] = fill->color;
1111 return Ok;
1113 case BrushTypeHatchFill:
1115 int x, y;
1116 GpHatch *fill = (GpHatch*)brush;
1117 const char *hatch_data;
1119 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1120 return NotImplemented;
1122 for (x=0; x<fill_area->Width; x++)
1123 for (y=0; y<fill_area->Height; y++)
1125 int hx, hy;
1127 /* FIXME: Account for the rendering origin */
1128 hx = (x + fill_area->X) % 8;
1129 hy = (y + fill_area->Y) % 8;
1131 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1132 argb_pixels[x + y*cdwStride] = fill->forecol;
1133 else
1134 argb_pixels[x + y*cdwStride] = fill->backcol;
1137 return Ok;
1139 case BrushTypeLinearGradient:
1141 GpLineGradient *fill = (GpLineGradient*)brush;
1142 GpPointF draw_points[3];
1143 GpStatus stat;
1144 int x, y;
1146 draw_points[0].X = fill_area->X;
1147 draw_points[0].Y = fill_area->Y;
1148 draw_points[1].X = fill_area->X+1;
1149 draw_points[1].Y = fill_area->Y;
1150 draw_points[2].X = fill_area->X;
1151 draw_points[2].Y = fill_area->Y+1;
1153 /* Transform the points to a co-ordinate space where X is the point's
1154 * position in the gradient, 0.0 being the start point and 1.0 the
1155 * end point. */
1156 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1157 CoordinateSpaceDevice, draw_points, 3);
1159 if (stat == Ok)
1161 GpMatrix world_to_gradient = fill->transform;
1163 stat = GdipInvertMatrix(&world_to_gradient);
1164 if (stat == Ok)
1165 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1168 if (stat == Ok)
1170 REAL x_delta = draw_points[1].X - draw_points[0].X;
1171 REAL y_delta = draw_points[2].X - draw_points[0].X;
1173 for (y=0; y<fill_area->Height; y++)
1175 for (x=0; x<fill_area->Width; x++)
1177 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1179 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1184 return stat;
1186 case BrushTypeTextureFill:
1188 GpTexture *fill = (GpTexture*)brush;
1189 GpPointF draw_points[3];
1190 GpStatus stat;
1191 int x, y;
1192 GpBitmap *bitmap;
1193 int src_stride;
1194 GpRect src_area;
1196 if (fill->image->type != ImageTypeBitmap)
1198 FIXME("metafile texture brushes not implemented\n");
1199 return NotImplemented;
1202 bitmap = (GpBitmap*)fill->image;
1203 src_stride = sizeof(ARGB) * bitmap->width;
1205 src_area.X = src_area.Y = 0;
1206 src_area.Width = bitmap->width;
1207 src_area.Height = bitmap->height;
1209 draw_points[0].X = fill_area->X;
1210 draw_points[0].Y = fill_area->Y;
1211 draw_points[1].X = fill_area->X+1;
1212 draw_points[1].Y = fill_area->Y;
1213 draw_points[2].X = fill_area->X;
1214 draw_points[2].Y = fill_area->Y+1;
1216 /* Transform the points to the co-ordinate space of the bitmap. */
1217 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1218 CoordinateSpaceDevice, draw_points, 3);
1220 if (stat == Ok)
1222 GpMatrix world_to_texture = fill->transform;
1224 stat = GdipInvertMatrix(&world_to_texture);
1225 if (stat == Ok)
1226 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1229 if (stat == Ok && !fill->bitmap_bits)
1231 BitmapData lockeddata;
1233 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1234 if (!fill->bitmap_bits)
1235 stat = OutOfMemory;
1237 if (stat == Ok)
1239 lockeddata.Width = bitmap->width;
1240 lockeddata.Height = bitmap->height;
1241 lockeddata.Stride = src_stride;
1242 lockeddata.PixelFormat = PixelFormat32bppARGB;
1243 lockeddata.Scan0 = fill->bitmap_bits;
1245 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1246 PixelFormat32bppARGB, &lockeddata);
1249 if (stat == Ok)
1250 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1252 if (stat == Ok)
1253 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1254 bitmap->width, bitmap->height,
1255 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1257 if (stat != Ok)
1259 heap_free(fill->bitmap_bits);
1260 fill->bitmap_bits = NULL;
1264 if (stat == Ok)
1266 REAL x_dx = draw_points[1].X - draw_points[0].X;
1267 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1268 REAL y_dx = draw_points[2].X - draw_points[0].X;
1269 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1271 for (y=0; y<fill_area->Height; y++)
1273 for (x=0; x<fill_area->Width; x++)
1275 GpPointF point;
1276 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1277 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1279 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1280 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1281 &point, fill->imageattributes, graphics->interpolation,
1282 graphics->pixeloffset);
1287 return stat;
1289 case BrushTypePathGradient:
1291 GpPathGradient *fill = (GpPathGradient*)brush;
1292 GpPath *flat_path;
1293 GpMatrix world_to_device;
1294 GpStatus stat;
1295 int i, figure_start=0;
1296 GpPointF start_point, end_point, center_point;
1297 BYTE type;
1298 REAL min_yf, max_yf, line1_xf, line2_xf;
1299 INT min_y, max_y, min_x, max_x;
1300 INT x, y;
1301 ARGB outer_color;
1302 static BOOL transform_fixme_once;
1304 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1306 static int once;
1307 if (!once++)
1308 FIXME("path gradient focus not implemented\n");
1311 if (fill->gamma)
1313 static int once;
1314 if (!once++)
1315 FIXME("path gradient gamma correction not implemented\n");
1318 if (fill->blendcount)
1320 static int once;
1321 if (!once++)
1322 FIXME("path gradient blend not implemented\n");
1325 if (fill->pblendcount)
1327 static int once;
1328 if (!once++)
1329 FIXME("path gradient preset blend not implemented\n");
1332 if (!transform_fixme_once)
1334 BOOL is_identity=TRUE;
1335 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1336 if (!is_identity)
1338 FIXME("path gradient transform not implemented\n");
1339 transform_fixme_once = TRUE;
1343 stat = GdipClonePath(fill->path, &flat_path);
1345 if (stat != Ok)
1346 return stat;
1348 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1349 CoordinateSpaceWorld, &world_to_device);
1350 if (stat == Ok)
1352 stat = GdipTransformPath(flat_path, &world_to_device);
1354 if (stat == Ok)
1356 center_point = fill->center;
1357 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1360 if (stat == Ok)
1361 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1364 if (stat != Ok)
1366 GdipDeletePath(flat_path);
1367 return stat;
1370 for (i=0; i<flat_path->pathdata.Count; i++)
1372 int start_center_line=0, end_center_line=0;
1373 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1374 REAL center_distance;
1375 ARGB start_color, end_color;
1376 REAL dy, dx;
1378 type = flat_path->pathdata.Types[i];
1380 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1381 figure_start = i;
1383 start_point = flat_path->pathdata.Points[i];
1385 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1387 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1389 end_point = flat_path->pathdata.Points[figure_start];
1390 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1392 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1394 end_point = flat_path->pathdata.Points[i+1];
1395 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1397 else
1398 continue;
1400 outer_color = start_color;
1402 min_yf = center_point.Y;
1403 if (min_yf > start_point.Y) min_yf = start_point.Y;
1404 if (min_yf > end_point.Y) min_yf = end_point.Y;
1406 if (min_yf < fill_area->Y)
1407 min_y = fill_area->Y;
1408 else
1409 min_y = (INT)ceil(min_yf);
1411 max_yf = center_point.Y;
1412 if (max_yf < start_point.Y) max_yf = start_point.Y;
1413 if (max_yf < end_point.Y) max_yf = end_point.Y;
1415 if (max_yf > fill_area->Y + fill_area->Height)
1416 max_y = fill_area->Y + fill_area->Height;
1417 else
1418 max_y = (INT)ceil(max_yf);
1420 dy = end_point.Y - start_point.Y;
1421 dx = end_point.X - start_point.X;
1423 /* This is proportional to the distance from start-end line to center point. */
1424 center_distance = dy * (start_point.X - center_point.X) +
1425 dx * (center_point.Y - start_point.Y);
1427 for (y=min_y; y<max_y; y++)
1429 REAL yf = (REAL)y;
1431 if (!seen_start && yf >= start_point.Y)
1433 seen_start = TRUE;
1434 start_center_line ^= 1;
1436 if (!seen_end && yf >= end_point.Y)
1438 seen_end = TRUE;
1439 end_center_line ^= 1;
1441 if (!seen_center && yf >= center_point.Y)
1443 seen_center = TRUE;
1444 start_center_line ^= 1;
1445 end_center_line ^= 1;
1448 if (start_center_line)
1449 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1450 else
1451 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1453 if (end_center_line)
1454 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1455 else
1456 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1458 if (line1_xf < line2_xf)
1460 min_x = (INT)ceil(line1_xf);
1461 max_x = (INT)ceil(line2_xf);
1463 else
1465 min_x = (INT)ceil(line2_xf);
1466 max_x = (INT)ceil(line1_xf);
1469 if (min_x < fill_area->X)
1470 min_x = fill_area->X;
1471 if (max_x > fill_area->X + fill_area->Width)
1472 max_x = fill_area->X + fill_area->Width;
1474 for (x=min_x; x<max_x; x++)
1476 REAL xf = (REAL)x;
1477 REAL distance;
1479 if (start_color != end_color)
1481 REAL blend_amount, pdy, pdx;
1482 pdy = yf - center_point.Y;
1483 pdx = xf - center_point.X;
1485 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1487 /* Too close to center point, don't try to calculate outer color */
1488 outer_color = start_color;
1490 else
1492 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1493 outer_color = blend_colors(start_color, end_color, blend_amount);
1497 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1498 (end_point.X - start_point.X) * (yf - start_point.Y);
1500 distance = distance / center_distance;
1502 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1503 blend_colors(outer_color, fill->centercolor, distance);
1508 GdipDeletePath(flat_path);
1509 return stat;
1511 default:
1512 return NotImplemented;
1516 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1517 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1518 * should not be called on an hdc that has a path you care about. */
1519 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1520 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1522 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1523 GpMatrix matrix;
1524 HBRUSH brush = NULL;
1525 HPEN pen = NULL;
1526 PointF ptf[4], *custptf = NULL;
1527 POINT pt[4], *custpt = NULL;
1528 BYTE *tp = NULL;
1529 REAL theta, dsmall, dbig, dx, dy = 0.0;
1530 INT i, count;
1531 LOGBRUSH lb;
1532 BOOL customstroke;
1534 if((x1 == x2) && (y1 == y2))
1535 return;
1537 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1539 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1540 if(!customstroke){
1541 brush = CreateSolidBrush(color);
1542 lb.lbStyle = BS_SOLID;
1543 lb.lbColor = color;
1544 lb.lbHatch = 0;
1545 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1546 PS_JOIN_MITER, 1, &lb, 0,
1547 NULL);
1548 oldbrush = SelectObject(graphics->hdc, brush);
1549 oldpen = SelectObject(graphics->hdc, pen);
1552 switch(cap){
1553 case LineCapFlat:
1554 break;
1555 case LineCapSquare:
1556 case LineCapSquareAnchor:
1557 case LineCapDiamondAnchor:
1558 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1559 if(cap == LineCapDiamondAnchor){
1560 dsmall = cos(theta + M_PI_2) * size;
1561 dbig = sin(theta + M_PI_2) * size;
1563 else{
1564 dsmall = cos(theta + M_PI_4) * size;
1565 dbig = sin(theta + M_PI_4) * size;
1568 ptf[0].X = x2 - dsmall;
1569 ptf[1].X = x2 + dbig;
1571 ptf[0].Y = y2 - dbig;
1572 ptf[3].Y = y2 + dsmall;
1574 ptf[1].Y = y2 - dsmall;
1575 ptf[2].Y = y2 + dbig;
1577 ptf[3].X = x2 - dbig;
1578 ptf[2].X = x2 + dsmall;
1580 transform_and_round_points(graphics, pt, ptf, 4);
1581 Polygon(graphics->hdc, pt, 4);
1583 break;
1584 case LineCapArrowAnchor:
1585 size = size * 4.0 / sqrt(3.0);
1587 dx = cos(M_PI / 6.0 + theta) * size;
1588 dy = sin(M_PI / 6.0 + theta) * size;
1590 ptf[0].X = x2 - dx;
1591 ptf[0].Y = y2 - dy;
1593 dx = cos(- M_PI / 6.0 + theta) * size;
1594 dy = sin(- M_PI / 6.0 + theta) * size;
1596 ptf[1].X = x2 - dx;
1597 ptf[1].Y = y2 - dy;
1599 ptf[2].X = x2;
1600 ptf[2].Y = y2;
1602 transform_and_round_points(graphics, pt, ptf, 3);
1603 Polygon(graphics->hdc, pt, 3);
1605 break;
1606 case LineCapRoundAnchor:
1607 dx = dy = ANCHOR_WIDTH * size / 2.0;
1609 ptf[0].X = x2 - dx;
1610 ptf[0].Y = y2 - dy;
1611 ptf[1].X = x2 + dx;
1612 ptf[1].Y = y2 + dy;
1614 transform_and_round_points(graphics, pt, ptf, 2);
1615 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1617 break;
1618 case LineCapTriangle:
1619 size = size / 2.0;
1620 dx = cos(M_PI_2 + theta) * size;
1621 dy = sin(M_PI_2 + theta) * size;
1623 ptf[0].X = x2 - dx;
1624 ptf[0].Y = y2 - dy;
1625 ptf[1].X = x2 + dx;
1626 ptf[1].Y = y2 + dy;
1628 dx = cos(theta) * size;
1629 dy = sin(theta) * size;
1631 ptf[2].X = x2 + dx;
1632 ptf[2].Y = y2 + dy;
1634 transform_and_round_points(graphics, pt, ptf, 3);
1635 Polygon(graphics->hdc, pt, 3);
1637 break;
1638 case LineCapRound:
1639 dx = dy = size / 2.0;
1641 ptf[0].X = x2 - dx;
1642 ptf[0].Y = y2 - dy;
1643 ptf[1].X = x2 + dx;
1644 ptf[1].Y = y2 + dy;
1646 dx = -cos(M_PI_2 + theta) * size;
1647 dy = -sin(M_PI_2 + theta) * size;
1649 ptf[2].X = x2 - dx;
1650 ptf[2].Y = y2 - dy;
1651 ptf[3].X = x2 + dx;
1652 ptf[3].Y = y2 + dy;
1654 transform_and_round_points(graphics, pt, ptf, 4);
1655 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1656 pt[2].y, pt[3].x, pt[3].y);
1658 break;
1659 case LineCapCustom:
1660 if(!custom)
1661 break;
1663 count = custom->pathdata.Count;
1664 custptf = heap_alloc_zero(count * sizeof(PointF));
1665 custpt = heap_alloc_zero(count * sizeof(POINT));
1666 tp = heap_alloc_zero(count);
1668 if(!custptf || !custpt || !tp)
1669 goto custend;
1671 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1673 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1674 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1675 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1676 MatrixOrderAppend);
1677 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1678 GdipTransformMatrixPoints(&matrix, custptf, count);
1680 transform_and_round_points(graphics, custpt, custptf, count);
1682 for(i = 0; i < count; i++)
1683 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1685 if(custom->fill){
1686 BeginPath(graphics->hdc);
1687 PolyDraw(graphics->hdc, custpt, tp, count);
1688 EndPath(graphics->hdc);
1689 StrokeAndFillPath(graphics->hdc);
1691 else
1692 PolyDraw(graphics->hdc, custpt, tp, count);
1694 custend:
1695 heap_free(custptf);
1696 heap_free(custpt);
1697 heap_free(tp);
1698 break;
1699 default:
1700 break;
1703 if(!customstroke){
1704 SelectObject(graphics->hdc, oldbrush);
1705 SelectObject(graphics->hdc, oldpen);
1706 DeleteObject(brush);
1707 DeleteObject(pen);
1711 /* Shortens the line by the given percent by changing x2, y2.
1712 * If percent is > 1.0 then the line will change direction.
1713 * If percent is negative it can lengthen the line. */
1714 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1716 REAL dist, theta, dx, dy;
1718 if((y1 == *y2) && (x1 == *x2))
1719 return;
1721 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1722 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1723 dx = cos(theta) * dist;
1724 dy = sin(theta) * dist;
1726 *x2 = *x2 + dx;
1727 *y2 = *y2 + dy;
1730 /* Shortens the line by the given amount by changing x2, y2.
1731 * If the amount is greater than the distance, the line will become length 0.
1732 * If the amount is negative, it can lengthen the line. */
1733 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1735 REAL dx, dy, percent;
1737 dx = *x2 - x1;
1738 dy = *y2 - y1;
1739 if(dx == 0 && dy == 0)
1740 return;
1742 percent = amt / sqrt(dx * dx + dy * dy);
1743 if(percent >= 1.0){
1744 *x2 = x1;
1745 *y2 = y1;
1746 return;
1749 shorten_line_percent(x1, y1, x2, y2, percent);
1752 /* Conducts a linear search to find the bezier points that will back off
1753 * the endpoint of the curve by a distance of amt. Linear search works
1754 * better than binary in this case because there are multiple solutions,
1755 * and binary searches often find a bad one. I don't think this is what
1756 * Windows does but short of rendering the bezier without GDI's help it's
1757 * the best we can do. If rev then work from the start of the passed points
1758 * instead of the end. */
1759 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1761 GpPointF origpt[4];
1762 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1763 INT i, first = 0, second = 1, third = 2, fourth = 3;
1765 if(rev){
1766 first = 3;
1767 second = 2;
1768 third = 1;
1769 fourth = 0;
1772 origx = pt[fourth].X;
1773 origy = pt[fourth].Y;
1774 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1776 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1777 /* reset bezier points to original values */
1778 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1779 /* Perform magic on bezier points. Order is important here.*/
1780 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1781 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1782 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1783 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1784 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1785 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1787 dx = pt[fourth].X - origx;
1788 dy = pt[fourth].Y - origy;
1790 diff = sqrt(dx * dx + dy * dy);
1791 percent += 0.0005 * amt;
1795 /* Draws a combination of bezier curves and lines between points. */
1796 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1797 GDIPCONST BYTE * types, INT count, BOOL caps)
1799 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1800 BYTE *tp = heap_alloc_zero(count);
1801 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1802 INT i, j;
1803 GpStatus status = GenericError;
1805 if(!count){
1806 status = Ok;
1807 goto end;
1809 if(!pti || !tp || !ptcopy){
1810 status = OutOfMemory;
1811 goto end;
1814 for(i = 1; i < count; i++){
1815 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1816 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1817 || !(types[i + 2] & PathPointTypeBezier)){
1818 ERR("Bad bezier points\n");
1819 goto end;
1821 i += 2;
1825 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1827 /* If we are drawing caps, go through the points and adjust them accordingly,
1828 * and draw the caps. */
1829 if(caps){
1830 switch(types[count - 1] & PathPointTypePathTypeMask){
1831 case PathPointTypeBezier:
1832 if(pen->endcap == LineCapArrowAnchor)
1833 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1834 else if((pen->endcap == LineCapCustom) && pen->customend)
1835 shorten_bezier_amt(&ptcopy[count - 4],
1836 pen->width * pen->customend->inset, FALSE);
1838 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1839 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1840 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1841 pt[count - 1].X, pt[count - 1].Y);
1843 break;
1844 case PathPointTypeLine:
1845 if(pen->endcap == LineCapArrowAnchor)
1846 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1847 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1848 pen->width);
1849 else if((pen->endcap == LineCapCustom) && pen->customend)
1850 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1851 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1852 pen->customend->inset * pen->width);
1854 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1855 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1856 pt[count - 1].Y);
1858 break;
1859 default:
1860 ERR("Bad path last point\n");
1861 goto end;
1864 /* Find start of points */
1865 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1866 == PathPointTypeStart); j++);
1868 switch(types[j] & PathPointTypePathTypeMask){
1869 case PathPointTypeBezier:
1870 if(pen->startcap == LineCapArrowAnchor)
1871 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1872 else if((pen->startcap == LineCapCustom) && pen->customstart)
1873 shorten_bezier_amt(&ptcopy[j - 1],
1874 pen->width * pen->customstart->inset, TRUE);
1876 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1877 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1878 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1879 pt[j - 1].X, pt[j - 1].Y);
1881 break;
1882 case PathPointTypeLine:
1883 if(pen->startcap == LineCapArrowAnchor)
1884 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1885 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1886 pen->width);
1887 else if((pen->startcap == LineCapCustom) && pen->customstart)
1888 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1889 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1890 pen->customstart->inset * pen->width);
1892 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1893 pt[j].X, pt[j].Y, pt[j - 1].X,
1894 pt[j - 1].Y);
1896 break;
1897 default:
1898 ERR("Bad path points\n");
1899 goto end;
1903 transform_and_round_points(graphics, pti, ptcopy, count);
1905 for(i = 0; i < count; i++){
1906 tp[i] = convert_path_point_type(types[i]);
1909 PolyDraw(graphics->hdc, pti, tp, count);
1911 status = Ok;
1913 end:
1914 heap_free(pti);
1915 heap_free(ptcopy);
1916 heap_free(tp);
1918 return status;
1921 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1923 GpStatus result;
1925 BeginPath(graphics->hdc);
1926 result = draw_poly(graphics, NULL, path->pathdata.Points,
1927 path->pathdata.Types, path->pathdata.Count, FALSE);
1928 EndPath(graphics->hdc);
1929 return result;
1932 typedef enum GraphicsContainerType {
1933 BEGIN_CONTAINER,
1934 SAVE_GRAPHICS
1935 } GraphicsContainerType;
1937 typedef struct _GraphicsContainerItem {
1938 struct list entry;
1939 GraphicsContainer contid;
1940 GraphicsContainerType type;
1942 SmoothingMode smoothing;
1943 CompositingQuality compqual;
1944 InterpolationMode interpolation;
1945 CompositingMode compmode;
1946 TextRenderingHint texthint;
1947 REAL scale;
1948 GpUnit unit;
1949 PixelOffsetMode pixeloffset;
1950 UINT textcontrast;
1951 GpMatrix worldtrans;
1952 GpRegion* clip;
1953 INT origin_x, origin_y;
1954 } GraphicsContainerItem;
1956 static GpStatus init_container(GraphicsContainerItem** container,
1957 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
1958 GpStatus sts;
1960 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
1961 if(!(*container))
1962 return OutOfMemory;
1964 (*container)->contid = graphics->contid + 1;
1965 (*container)->type = type;
1967 (*container)->smoothing = graphics->smoothing;
1968 (*container)->compqual = graphics->compqual;
1969 (*container)->interpolation = graphics->interpolation;
1970 (*container)->compmode = graphics->compmode;
1971 (*container)->texthint = graphics->texthint;
1972 (*container)->scale = graphics->scale;
1973 (*container)->unit = graphics->unit;
1974 (*container)->textcontrast = graphics->textcontrast;
1975 (*container)->pixeloffset = graphics->pixeloffset;
1976 (*container)->origin_x = graphics->origin_x;
1977 (*container)->origin_y = graphics->origin_y;
1978 (*container)->worldtrans = graphics->worldtrans;
1980 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1981 if(sts != Ok){
1982 heap_free(*container);
1983 *container = NULL;
1984 return sts;
1987 return Ok;
1990 static void delete_container(GraphicsContainerItem* container)
1992 GdipDeleteRegion(container->clip);
1993 heap_free(container);
1996 static GpStatus restore_container(GpGraphics* graphics,
1997 GDIPCONST GraphicsContainerItem* container){
1998 GpStatus sts;
1999 GpRegion *newClip;
2001 sts = GdipCloneRegion(container->clip, &newClip);
2002 if(sts != Ok) return sts;
2004 graphics->worldtrans = container->worldtrans;
2006 GdipDeleteRegion(graphics->clip);
2007 graphics->clip = newClip;
2009 graphics->contid = container->contid - 1;
2011 graphics->smoothing = container->smoothing;
2012 graphics->compqual = container->compqual;
2013 graphics->interpolation = container->interpolation;
2014 graphics->compmode = container->compmode;
2015 graphics->texthint = container->texthint;
2016 graphics->scale = container->scale;
2017 graphics->unit = container->unit;
2018 graphics->textcontrast = container->textcontrast;
2019 graphics->pixeloffset = container->pixeloffset;
2020 graphics->origin_x = container->origin_x;
2021 graphics->origin_y = container->origin_y;
2023 return Ok;
2026 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2028 RECT wnd_rect;
2029 GpStatus stat=Ok;
2030 GpUnit unit;
2032 if(graphics->hwnd) {
2033 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2034 return GenericError;
2036 rect->X = wnd_rect.left;
2037 rect->Y = wnd_rect.top;
2038 rect->Width = wnd_rect.right - wnd_rect.left;
2039 rect->Height = wnd_rect.bottom - wnd_rect.top;
2040 }else if (graphics->image){
2041 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2042 if (stat == Ok && unit != UnitPixel)
2043 FIXME("need to convert from unit %i\n", unit);
2044 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2045 HBITMAP hbmp;
2046 BITMAP bmp;
2048 rect->X = 0;
2049 rect->Y = 0;
2051 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2052 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2054 rect->Width = bmp.bmWidth;
2055 rect->Height = bmp.bmHeight;
2057 else
2059 /* FIXME: ??? */
2060 rect->Width = 1;
2061 rect->Height = 1;
2063 }else{
2064 rect->X = 0;
2065 rect->Y = 0;
2066 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2067 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2070 if (graphics->hdc)
2072 POINT points[2];
2074 points[0].x = rect->X;
2075 points[0].y = rect->Y;
2076 points[1].x = rect->X + rect->Width;
2077 points[1].y = rect->Y + rect->Height;
2079 DPtoLP(graphics->hdc, points, sizeof(points)/sizeof(points[0]));
2081 rect->X = min(points[0].x, points[1].x);
2082 rect->Y = min(points[0].y, points[1].y);
2083 rect->Width = abs(points[1].x - points[0].x);
2084 rect->Height = abs(points[1].y - points[0].y);
2087 return stat;
2090 /* on success, rgn will contain the region of the graphics object which
2091 * is visible after clipping has been applied */
2092 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2094 GpStatus stat;
2095 GpRectF rectf;
2096 GpRegion* tmp;
2098 /* Ignore graphics image bounds for metafiles */
2099 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2100 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2102 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2103 return stat;
2105 if((stat = GdipCreateRegion(&tmp)) != Ok)
2106 return stat;
2108 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2109 goto end;
2111 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2112 goto end;
2114 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2116 end:
2117 GdipDeleteRegion(tmp);
2118 return stat;
2121 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2123 REAL height;
2125 if (font->unit == UnitPixel)
2127 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2129 else
2131 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2132 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2133 else
2134 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2137 lf->lfHeight = -(height + 0.5);
2138 lf->lfWidth = 0;
2139 lf->lfEscapement = 0;
2140 lf->lfOrientation = 0;
2141 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2142 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2143 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2144 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2145 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2146 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2147 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2148 lf->lfQuality = DEFAULT_QUALITY;
2149 lf->lfPitchAndFamily = 0;
2150 strcpyW(lf->lfFaceName, font->family->FamilyName);
2153 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2154 GDIPCONST GpStringFormat *format, HFONT *hfont,
2155 GDIPCONST GpMatrix *matrix)
2157 HDC hdc = CreateCompatibleDC(0);
2158 GpPointF pt[3];
2159 REAL angle, rel_width, rel_height, font_height;
2160 LOGFONTW lfw;
2161 HFONT unscaled_font;
2162 TEXTMETRICW textmet;
2164 if (font->unit == UnitPixel || font->unit == UnitWorld)
2165 font_height = font->emSize;
2166 else
2168 REAL unit_scale, res;
2170 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2171 unit_scale = units_scale(font->unit, graphics->unit, res);
2173 font_height = font->emSize * unit_scale;
2176 pt[0].X = 0.0;
2177 pt[0].Y = 0.0;
2178 pt[1].X = 1.0;
2179 pt[1].Y = 0.0;
2180 pt[2].X = 0.0;
2181 pt[2].Y = 1.0;
2182 if (matrix)
2184 GpMatrix xform = *matrix;
2185 GdipTransformMatrixPoints(&xform, pt, 3);
2188 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2189 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2190 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2191 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2192 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2193 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2195 get_log_fontW(font, graphics, &lfw);
2196 lfw.lfHeight = -gdip_round(font_height * rel_height);
2197 unscaled_font = CreateFontIndirectW(&lfw);
2199 SelectObject(hdc, unscaled_font);
2200 GetTextMetricsW(hdc, &textmet);
2202 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2203 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2205 *hfont = CreateFontIndirectW(&lfw);
2207 DeleteDC(hdc);
2208 DeleteObject(unscaled_font);
2211 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2213 TRACE("(%p, %p)\n", hdc, graphics);
2215 return GdipCreateFromHDC2(hdc, NULL, graphics);
2218 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2220 GpStatus retval;
2221 HBITMAP hbitmap;
2222 DIBSECTION dib;
2224 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2226 if(hDevice != NULL)
2227 FIXME("Don't know how to handle parameter hDevice\n");
2229 if(hdc == NULL)
2230 return OutOfMemory;
2232 if(graphics == NULL)
2233 return InvalidParameter;
2235 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2236 if(!*graphics) return OutOfMemory;
2238 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2240 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2241 heap_free(*graphics);
2242 return retval;
2245 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2246 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2247 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2249 (*graphics)->alpha_hdc = 1;
2252 (*graphics)->hdc = hdc;
2253 (*graphics)->hwnd = WindowFromDC(hdc);
2254 (*graphics)->owndc = FALSE;
2255 (*graphics)->smoothing = SmoothingModeDefault;
2256 (*graphics)->compqual = CompositingQualityDefault;
2257 (*graphics)->interpolation = InterpolationModeBilinear;
2258 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2259 (*graphics)->compmode = CompositingModeSourceOver;
2260 (*graphics)->unit = UnitDisplay;
2261 (*graphics)->scale = 1.0;
2262 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2263 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2264 (*graphics)->busy = FALSE;
2265 (*graphics)->textcontrast = 4;
2266 list_init(&(*graphics)->containers);
2267 (*graphics)->contid = 0;
2269 TRACE("<-- %p\n", *graphics);
2271 return Ok;
2274 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2276 GpStatus retval;
2278 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2279 if(!*graphics) return OutOfMemory;
2281 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2283 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2284 heap_free(*graphics);
2285 return retval;
2288 (*graphics)->hdc = NULL;
2289 (*graphics)->hwnd = NULL;
2290 (*graphics)->owndc = FALSE;
2291 (*graphics)->image = image;
2292 /* We have to store the image type here because the image may be freed
2293 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2294 (*graphics)->image_type = image->type;
2295 (*graphics)->smoothing = SmoothingModeDefault;
2296 (*graphics)->compqual = CompositingQualityDefault;
2297 (*graphics)->interpolation = InterpolationModeBilinear;
2298 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2299 (*graphics)->compmode = CompositingModeSourceOver;
2300 (*graphics)->unit = UnitDisplay;
2301 (*graphics)->scale = 1.0;
2302 (*graphics)->xres = image->xres;
2303 (*graphics)->yres = image->yres;
2304 (*graphics)->busy = FALSE;
2305 (*graphics)->textcontrast = 4;
2306 list_init(&(*graphics)->containers);
2307 (*graphics)->contid = 0;
2309 TRACE("<-- %p\n", *graphics);
2311 return Ok;
2314 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2316 GpStatus ret;
2317 HDC hdc;
2319 TRACE("(%p, %p)\n", hwnd, graphics);
2321 hdc = GetDC(hwnd);
2323 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2325 ReleaseDC(hwnd, hdc);
2326 return ret;
2329 (*graphics)->hwnd = hwnd;
2330 (*graphics)->owndc = TRUE;
2332 return Ok;
2335 /* FIXME: no icm handling */
2336 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2338 TRACE("(%p, %p)\n", hwnd, graphics);
2340 return GdipCreateFromHWND(hwnd, graphics);
2343 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2344 UINT access, IStream **stream)
2346 DWORD dwMode;
2347 HRESULT ret;
2349 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2351 if(!stream || !filename)
2352 return InvalidParameter;
2354 if(access & GENERIC_WRITE)
2355 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2356 else if(access & GENERIC_READ)
2357 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2358 else
2359 return InvalidParameter;
2361 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2363 return hresult_to_status(ret);
2366 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2368 GraphicsContainerItem *cont, *next;
2369 GpStatus stat;
2370 TRACE("(%p)\n", graphics);
2372 if(!graphics) return InvalidParameter;
2373 if(graphics->busy) return ObjectBusy;
2375 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2377 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2378 if (stat != Ok)
2379 return stat;
2382 if(graphics->owndc)
2383 ReleaseDC(graphics->hwnd, graphics->hdc);
2385 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2386 list_remove(&cont->entry);
2387 delete_container(cont);
2390 GdipDeleteRegion(graphics->clip);
2392 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2393 * do otherwise, but we can't have that in the test suite because it means
2394 * accessing freed memory. */
2395 graphics->busy = TRUE;
2397 heap_free(graphics);
2399 return Ok;
2402 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2403 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2405 GpStatus status;
2406 GpPath *path;
2408 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2409 width, height, startAngle, sweepAngle);
2411 if(!graphics || !pen || width <= 0 || height <= 0)
2412 return InvalidParameter;
2414 if(graphics->busy)
2415 return ObjectBusy;
2417 status = GdipCreatePath(FillModeAlternate, &path);
2418 if (status != Ok) return status;
2420 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2421 if (status == Ok)
2422 status = GdipDrawPath(graphics, pen, path);
2424 GdipDeletePath(path);
2425 return status;
2428 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2429 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2431 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2432 width, height, startAngle, sweepAngle);
2434 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2437 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2438 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2440 GpPointF pt[4];
2442 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2443 x2, y2, x3, y3, x4, y4);
2445 if(!graphics || !pen)
2446 return InvalidParameter;
2448 if(graphics->busy)
2449 return ObjectBusy;
2451 pt[0].X = x1;
2452 pt[0].Y = y1;
2453 pt[1].X = x2;
2454 pt[1].Y = y2;
2455 pt[2].X = x3;
2456 pt[2].Y = y3;
2457 pt[3].X = x4;
2458 pt[3].Y = y4;
2459 return GdipDrawBeziers(graphics, pen, pt, 4);
2462 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2463 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2465 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2466 x2, y2, x3, y3, x4, y4);
2468 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2471 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2472 GDIPCONST GpPointF *points, INT count)
2474 GpStatus status;
2475 GpPath *path;
2477 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2479 if(!graphics || !pen || !points || (count <= 0))
2480 return InvalidParameter;
2482 if(graphics->busy)
2483 return ObjectBusy;
2485 status = GdipCreatePath(FillModeAlternate, &path);
2486 if (status != Ok) return status;
2488 status = GdipAddPathBeziers(path, points, count);
2489 if (status == Ok)
2490 status = GdipDrawPath(graphics, pen, path);
2492 GdipDeletePath(path);
2493 return status;
2496 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2497 GDIPCONST GpPoint *points, INT count)
2499 GpPointF *pts;
2500 GpStatus ret;
2501 INT i;
2503 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2505 if(!graphics || !pen || !points || (count <= 0))
2506 return InvalidParameter;
2508 if(graphics->busy)
2509 return ObjectBusy;
2511 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2512 if(!pts)
2513 return OutOfMemory;
2515 for(i = 0; i < count; i++){
2516 pts[i].X = (REAL)points[i].X;
2517 pts[i].Y = (REAL)points[i].Y;
2520 ret = GdipDrawBeziers(graphics,pen,pts,count);
2522 heap_free(pts);
2524 return ret;
2527 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2528 GDIPCONST GpPointF *points, INT count)
2530 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2532 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2535 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2536 GDIPCONST GpPoint *points, INT count)
2538 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2540 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2543 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2544 GDIPCONST GpPointF *points, INT count, REAL tension)
2546 GpPath *path;
2547 GpStatus status;
2549 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2551 if(!graphics || !pen || !points || count <= 0)
2552 return InvalidParameter;
2554 if(graphics->busy)
2555 return ObjectBusy;
2557 status = GdipCreatePath(FillModeAlternate, &path);
2558 if (status != Ok) return status;
2560 status = GdipAddPathClosedCurve2(path, points, count, tension);
2561 if (status == Ok)
2562 status = GdipDrawPath(graphics, pen, path);
2564 GdipDeletePath(path);
2566 return status;
2569 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2570 GDIPCONST GpPoint *points, INT count, REAL tension)
2572 GpPointF *ptf;
2573 GpStatus stat;
2574 INT i;
2576 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2578 if(!points || count <= 0)
2579 return InvalidParameter;
2581 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2582 if(!ptf)
2583 return OutOfMemory;
2585 for(i = 0; i < count; i++){
2586 ptf[i].X = (REAL)points[i].X;
2587 ptf[i].Y = (REAL)points[i].Y;
2590 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2592 heap_free(ptf);
2594 return stat;
2597 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2598 GDIPCONST GpPointF *points, INT count)
2600 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2602 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2605 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2606 GDIPCONST GpPoint *points, INT count)
2608 GpPointF *pointsF;
2609 GpStatus ret;
2610 INT i;
2612 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2614 if(!points)
2615 return InvalidParameter;
2617 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2618 if(!pointsF)
2619 return OutOfMemory;
2621 for(i = 0; i < count; i++){
2622 pointsF[i].X = (REAL)points[i].X;
2623 pointsF[i].Y = (REAL)points[i].Y;
2626 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2627 heap_free(pointsF);
2629 return ret;
2632 /* Approximates cardinal spline with Bezier curves. */
2633 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2634 GDIPCONST GpPointF *points, INT count, REAL tension)
2636 GpPath *path;
2637 GpStatus status;
2639 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2641 if(!graphics || !pen)
2642 return InvalidParameter;
2644 if(graphics->busy)
2645 return ObjectBusy;
2647 if(count < 2)
2648 return InvalidParameter;
2650 status = GdipCreatePath(FillModeAlternate, &path);
2651 if (status != Ok) return status;
2653 status = GdipAddPathCurve2(path, points, count, tension);
2654 if (status == Ok)
2655 status = GdipDrawPath(graphics, pen, path);
2657 GdipDeletePath(path);
2658 return status;
2661 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2662 GDIPCONST GpPoint *points, INT count, REAL tension)
2664 GpPointF *pointsF;
2665 GpStatus ret;
2666 INT i;
2668 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2670 if(!points)
2671 return InvalidParameter;
2673 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2674 if(!pointsF)
2675 return OutOfMemory;
2677 for(i = 0; i < count; i++){
2678 pointsF[i].X = (REAL)points[i].X;
2679 pointsF[i].Y = (REAL)points[i].Y;
2682 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2683 heap_free(pointsF);
2685 return ret;
2688 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2689 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2690 REAL tension)
2692 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2694 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2695 return InvalidParameter;
2698 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2701 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2702 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2703 REAL tension)
2705 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2707 if(count < 0){
2708 return OutOfMemory;
2711 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2712 return InvalidParameter;
2715 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2718 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2719 REAL y, REAL width, REAL height)
2721 GpPath *path;
2722 GpStatus status;
2724 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2726 if(!graphics || !pen)
2727 return InvalidParameter;
2729 if(graphics->busy)
2730 return ObjectBusy;
2732 status = GdipCreatePath(FillModeAlternate, &path);
2733 if (status != Ok) return status;
2735 status = GdipAddPathEllipse(path, x, y, width, height);
2736 if (status == Ok)
2737 status = GdipDrawPath(graphics, pen, path);
2739 GdipDeletePath(path);
2740 return status;
2743 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2744 INT y, INT width, INT height)
2746 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2748 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2752 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2754 UINT width, height;
2756 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2758 if(!graphics || !image)
2759 return InvalidParameter;
2761 GdipGetImageWidth(image, &width);
2762 GdipGetImageHeight(image, &height);
2764 return GdipDrawImagePointRect(graphics, image, x, y,
2765 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2768 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2769 INT y)
2771 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2773 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2776 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2777 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2778 GpUnit srcUnit)
2780 GpPointF points[3];
2781 REAL scale_x, scale_y, width, height;
2783 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2785 if (!graphics || !image) return InvalidParameter;
2787 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2788 scale_x *= graphics->xres / image->xres;
2789 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2790 scale_y *= graphics->yres / image->yres;
2791 width = srcwidth * scale_x;
2792 height = srcheight * scale_y;
2794 points[0].X = points[2].X = x;
2795 points[0].Y = points[1].Y = y;
2796 points[1].X = x + width;
2797 points[2].Y = y + height;
2799 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2800 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2803 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2804 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2805 GpUnit srcUnit)
2807 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2810 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2811 GDIPCONST GpPointF *dstpoints, INT count)
2813 UINT width, height;
2815 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2817 if(!image)
2818 return InvalidParameter;
2820 GdipGetImageWidth(image, &width);
2821 GdipGetImageHeight(image, &height);
2823 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2824 width, height, UnitPixel, NULL, NULL, NULL);
2827 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2828 GDIPCONST GpPoint *dstpoints, INT count)
2830 GpPointF ptf[3];
2832 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2834 if (count != 3 || !dstpoints)
2835 return InvalidParameter;
2837 ptf[0].X = (REAL)dstpoints[0].X;
2838 ptf[0].Y = (REAL)dstpoints[0].Y;
2839 ptf[1].X = (REAL)dstpoints[1].X;
2840 ptf[1].Y = (REAL)dstpoints[1].Y;
2841 ptf[2].X = (REAL)dstpoints[2].X;
2842 ptf[2].Y = (REAL)dstpoints[2].Y;
2844 return GdipDrawImagePoints(graphics, image, ptf, count);
2847 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2848 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2850 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2851 return TRUE;
2854 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2855 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2856 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2857 DrawImageAbort callback, VOID * callbackData)
2859 GpPointF ptf[4];
2860 POINT pti[4];
2861 GpStatus stat;
2863 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2864 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2865 callbackData);
2867 if (count > 3)
2868 return NotImplemented;
2870 if(!graphics || !image || !points || count != 3)
2871 return InvalidParameter;
2873 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2874 debugstr_pointf(&points[2]));
2876 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2878 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
2879 image, points, count, srcx, srcy, srcwidth, srcheight,
2880 srcUnit, imageAttributes, callback, callbackData);
2883 memcpy(ptf, points, 3 * sizeof(GpPointF));
2885 /* Ensure source width/height is positive */
2886 if (srcwidth < 0)
2888 GpPointF tmp = ptf[1];
2889 srcx = srcx + srcwidth;
2890 srcwidth = -srcwidth;
2891 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2892 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2893 ptf[1] = ptf[0];
2894 ptf[0] = tmp;
2897 if (srcheight < 0)
2899 GpPointF tmp = ptf[2];
2900 srcy = srcy + srcheight;
2901 srcheight = -srcheight;
2902 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2903 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2904 ptf[2] = ptf[0];
2905 ptf[0] = tmp;
2908 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2909 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2910 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2911 return Ok;
2912 transform_and_round_points(graphics, pti, ptf, 4);
2914 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2915 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2917 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2918 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2919 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2920 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2921 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2923 if (image->type == ImageTypeBitmap)
2925 GpBitmap* bitmap = (GpBitmap*)image;
2926 BOOL do_resampling = FALSE;
2927 BOOL use_software = FALSE;
2929 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2930 graphics->xres, graphics->yres,
2931 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2932 graphics->scale, image->xres, image->yres, bitmap->format,
2933 imageAttributes ? imageAttributes->outside_color : 0);
2935 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2936 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2937 srcx < 0 || srcy < 0 ||
2938 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2939 do_resampling = TRUE;
2941 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
2942 (graphics->image && graphics->image->type == ImageTypeBitmap))
2943 use_software = TRUE;
2945 if (use_software)
2947 RECT dst_area;
2948 GpRectF graphics_bounds;
2949 GpRect src_area;
2950 int i, x, y, src_stride, dst_stride;
2951 GpMatrix dst_to_src;
2952 REAL m11, m12, m21, m22, mdx, mdy;
2953 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
2954 BitmapData lockeddata;
2955 InterpolationMode interpolation = graphics->interpolation;
2956 PixelOffsetMode offset_mode = graphics->pixeloffset;
2957 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2958 REAL x_dx, x_dy, y_dx, y_dy;
2959 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2961 if (!imageAttributes)
2962 imageAttributes = &defaultImageAttributes;
2964 dst_area.left = dst_area.right = pti[0].x;
2965 dst_area.top = dst_area.bottom = pti[0].y;
2966 for (i=1; i<4; i++)
2968 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2969 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2970 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2971 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2974 stat = get_graphics_bounds(graphics, &graphics_bounds);
2975 if (stat != Ok) return stat;
2977 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
2978 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
2979 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
2980 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
2982 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2984 if (IsRectEmpty(&dst_area)) return Ok;
2986 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2987 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2988 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2989 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2990 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2991 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2993 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2995 stat = GdipInvertMatrix(&dst_to_src);
2996 if (stat != Ok) return stat;
2998 if (do_resampling)
3000 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3001 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3003 else
3005 /* Make sure src_area is equal in size to dst_area. */
3006 src_area.X = srcx + dst_area.left - pti[0].x;
3007 src_area.Y = srcy + dst_area.top - pti[0].y;
3008 src_area.Width = dst_area.right - dst_area.left;
3009 src_area.Height = dst_area.bottom - dst_area.top;
3012 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3014 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3015 if (!src_data)
3016 return OutOfMemory;
3017 src_stride = sizeof(ARGB) * src_area.Width;
3019 /* Read the bits we need from the source bitmap into a compatible buffer. */
3020 lockeddata.Width = src_area.Width;
3021 lockeddata.Height = src_area.Height;
3022 lockeddata.Stride = src_stride;
3023 lockeddata.Scan0 = src_data;
3024 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3025 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3026 else
3027 lockeddata.PixelFormat = PixelFormat32bppARGB;
3029 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3030 lockeddata.PixelFormat, &lockeddata);
3032 if (stat == Ok)
3033 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3035 if (stat != Ok)
3037 heap_free(src_data);
3038 return stat;
3041 apply_image_attributes(imageAttributes, src_data,
3042 src_area.Width, src_area.Height,
3043 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3045 if (do_resampling)
3047 /* Transform the bits as needed to the destination. */
3048 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3049 if (!dst_data)
3051 heap_free(src_data);
3052 return OutOfMemory;
3055 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3057 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3059 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3060 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3061 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3062 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3064 for (x=dst_area.left; x<dst_area.right; x++)
3066 for (y=dst_area.top; y<dst_area.bottom; y++)
3068 GpPointF src_pointf;
3069 ARGB *dst_color;
3071 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3072 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3074 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3076 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3077 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3078 imageAttributes, interpolation, offset_mode);
3079 else
3080 *dst_color = 0;
3084 else
3086 dst_data = src_data;
3087 dst_stride = src_stride;
3090 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3091 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3092 lockeddata.PixelFormat);
3094 heap_free(src_data);
3096 heap_free(dst_dyn_data);
3098 return stat;
3100 else
3102 HDC hdc;
3103 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3104 HBITMAP hbitmap, old_hbm=NULL;
3105 HRGN hrgn;
3106 INT save_state;
3108 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3109 bitmap->format == PixelFormat24bppRGB ||
3110 bitmap->format == PixelFormat32bppRGB ||
3111 bitmap->format == PixelFormat32bppPARGB))
3113 BITMAPINFOHEADER bih;
3114 BYTE *temp_bits;
3115 PixelFormat dst_format;
3117 /* we can't draw a bitmap of this format directly */
3118 hdc = CreateCompatibleDC(0);
3119 temp_hdc = TRUE;
3120 temp_bitmap = TRUE;
3122 bih.biSize = sizeof(BITMAPINFOHEADER);
3123 bih.biWidth = bitmap->width;
3124 bih.biHeight = -bitmap->height;
3125 bih.biPlanes = 1;
3126 bih.biBitCount = 32;
3127 bih.biCompression = BI_RGB;
3128 bih.biSizeImage = 0;
3129 bih.biXPelsPerMeter = 0;
3130 bih.biYPelsPerMeter = 0;
3131 bih.biClrUsed = 0;
3132 bih.biClrImportant = 0;
3134 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3135 (void**)&temp_bits, NULL, 0);
3137 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3138 dst_format = PixelFormat32bppPARGB;
3139 else
3140 dst_format = PixelFormat32bppRGB;
3142 convert_pixels(bitmap->width, bitmap->height,
3143 bitmap->width*4, temp_bits, dst_format,
3144 bitmap->stride, bitmap->bits, bitmap->format,
3145 bitmap->image.palette);
3147 else
3149 if (bitmap->hbitmap)
3150 hbitmap = bitmap->hbitmap;
3151 else
3153 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3154 temp_bitmap = TRUE;
3157 hdc = bitmap->hdc;
3158 temp_hdc = (hdc == 0);
3161 if (temp_hdc)
3163 if (!hdc) hdc = CreateCompatibleDC(0);
3164 old_hbm = SelectObject(hdc, hbitmap);
3167 save_state = SaveDC(graphics->hdc);
3169 stat = get_clip_hrgn(graphics, &hrgn);
3171 if (stat == Ok && hrgn)
3173 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3174 DeleteObject(hrgn);
3177 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3179 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3180 hdc, srcx, srcy, srcwidth, srcheight);
3182 else
3184 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3185 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3188 RestoreDC(graphics->hdc, save_state);
3190 if (temp_hdc)
3192 SelectObject(hdc, old_hbm);
3193 DeleteDC(hdc);
3196 if (temp_bitmap)
3197 DeleteObject(hbitmap);
3200 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3202 GpRectF rc;
3204 rc.X = srcx;
3205 rc.Y = srcy;
3206 rc.Width = srcwidth;
3207 rc.Height = srcheight;
3209 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3210 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3212 else
3214 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3215 return InvalidParameter;
3218 return Ok;
3221 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3222 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3223 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3224 DrawImageAbort callback, VOID * callbackData)
3226 GpPointF pointsF[3];
3227 INT i;
3229 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3230 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3231 callbackData);
3233 if(!points || count!=3)
3234 return InvalidParameter;
3236 for(i = 0; i < count; i++){
3237 pointsF[i].X = (REAL)points[i].X;
3238 pointsF[i].Y = (REAL)points[i].Y;
3241 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3242 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3243 callback, callbackData);
3246 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3247 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3248 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3249 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3250 VOID * callbackData)
3252 GpPointF points[3];
3254 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3255 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3256 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3258 points[0].X = dstx;
3259 points[0].Y = dsty;
3260 points[1].X = dstx + dstwidth;
3261 points[1].Y = dsty;
3262 points[2].X = dstx;
3263 points[2].Y = dsty + dstheight;
3265 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3266 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3269 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3270 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3271 INT srcwidth, INT srcheight, GpUnit srcUnit,
3272 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3273 VOID * callbackData)
3275 GpPointF points[3];
3277 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3278 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3279 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3281 points[0].X = dstx;
3282 points[0].Y = dsty;
3283 points[1].X = dstx + dstwidth;
3284 points[1].Y = dsty;
3285 points[2].X = dstx;
3286 points[2].Y = dsty + dstheight;
3288 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3289 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3292 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3293 REAL x, REAL y, REAL width, REAL height)
3295 RectF bounds;
3296 GpUnit unit;
3297 GpStatus ret;
3299 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3301 if(!graphics || !image)
3302 return InvalidParameter;
3304 ret = GdipGetImageBounds(image, &bounds, &unit);
3305 if(ret != Ok)
3306 return ret;
3308 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3309 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3310 unit, NULL, NULL, NULL);
3313 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3314 INT x, INT y, INT width, INT height)
3316 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3318 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3321 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3322 REAL y1, REAL x2, REAL y2)
3324 GpPointF pt[2];
3326 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3328 if (!pen)
3329 return InvalidParameter;
3331 if (pen->unit == UnitPixel && pen->width <= 0.0)
3332 return Ok;
3334 pt[0].X = x1;
3335 pt[0].Y = y1;
3336 pt[1].X = x2;
3337 pt[1].Y = y2;
3338 return GdipDrawLines(graphics, pen, pt, 2);
3341 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3342 INT y1, INT x2, INT y2)
3344 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3346 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3349 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3350 GpPointF *points, INT count)
3352 GpStatus status;
3353 GpPath *path;
3355 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3357 if(!pen || !graphics || (count < 2))
3358 return InvalidParameter;
3360 if(graphics->busy)
3361 return ObjectBusy;
3363 status = GdipCreatePath(FillModeAlternate, &path);
3364 if (status != Ok) return status;
3366 status = GdipAddPathLine2(path, points, count);
3367 if (status == Ok)
3368 status = GdipDrawPath(graphics, pen, path);
3370 GdipDeletePath(path);
3371 return status;
3374 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3375 GpPoint *points, INT count)
3377 GpStatus retval;
3378 GpPointF *ptf;
3379 int i;
3381 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3383 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3384 if(!ptf) return OutOfMemory;
3386 for(i = 0; i < count; i ++){
3387 ptf[i].X = (REAL) points[i].X;
3388 ptf[i].Y = (REAL) points[i].Y;
3391 retval = GdipDrawLines(graphics, pen, ptf, count);
3393 heap_free(ptf);
3394 return retval;
3397 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3399 INT save_state;
3400 GpStatus retval;
3401 HRGN hrgn=NULL;
3403 save_state = prepare_dc(graphics, pen);
3405 retval = get_clip_hrgn(graphics, &hrgn);
3407 if (retval != Ok)
3408 goto end;
3410 if (hrgn)
3411 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3413 retval = draw_poly(graphics, pen, path->pathdata.Points,
3414 path->pathdata.Types, path->pathdata.Count, TRUE);
3416 end:
3417 restore_dc(graphics, save_state);
3418 DeleteObject(hrgn);
3420 return retval;
3423 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3425 GpStatus stat;
3426 GpPath* flat_path;
3427 GpMatrix* transform;
3428 GpRectF gp_bound_rect;
3429 GpRect gp_output_area;
3430 RECT output_area;
3431 INT output_height, output_width;
3432 DWORD *output_bits, *brush_bits=NULL;
3433 int i;
3434 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3435 const BYTE *dash_pattern;
3436 INT dash_pattern_size;
3437 BYTE *dyn_dash_pattern = NULL;
3439 stat = GdipClonePath(path, &flat_path);
3441 if (stat != Ok)
3442 return stat;
3444 stat = GdipCreateMatrix(&transform);
3446 if (stat == Ok)
3448 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3449 CoordinateSpaceWorld, transform);
3451 if (stat == Ok)
3452 stat = GdipFlattenPath(flat_path, transform, 1.0);
3454 GdipDeleteMatrix(transform);
3457 /* estimate the output size in pixels, can be larger than necessary */
3458 if (stat == Ok)
3460 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3461 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3462 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3463 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3465 for (i=1; i<flat_path->pathdata.Count; i++)
3467 REAL x, y;
3468 x = flat_path->pathdata.Points[i].X;
3469 y = flat_path->pathdata.Points[i].Y;
3471 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3472 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3473 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3474 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3477 stat = get_graphics_bounds(graphics, &gp_bound_rect);
3480 if (stat == Ok)
3482 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3483 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3484 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3485 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3487 output_width = output_area.right - output_area.left + 1;
3488 output_height = output_area.bottom - output_area.top + 1;
3490 if (output_width <= 0 || output_height <= 0)
3492 GdipDeletePath(flat_path);
3493 return Ok;
3496 gp_output_area.X = output_area.left;
3497 gp_output_area.Y = output_area.top;
3498 gp_output_area.Width = output_width;
3499 gp_output_area.Height = output_height;
3501 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3502 if (!output_bits)
3503 stat = OutOfMemory;
3506 if (stat == Ok)
3508 if (pen->brush->bt != BrushTypeSolidColor)
3510 /* allocate and draw brush output */
3511 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3513 if (brush_bits)
3515 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3516 &gp_output_area, output_width);
3518 else
3519 stat = OutOfMemory;
3522 if (stat == Ok)
3524 /* convert dash pattern to bool array */
3525 switch (pen->dash)
3527 case DashStyleCustom:
3529 dash_pattern_size = 0;
3531 for (i=0; i < pen->numdashes; i++)
3532 dash_pattern_size += gdip_round(pen->dashes[i]);
3534 if (dash_pattern_size != 0)
3536 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3538 if (dyn_dash_pattern)
3540 int j=0;
3541 for (i=0; i < pen->numdashes; i++)
3543 int k;
3544 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3545 dyn_dash_pattern[j++] = (i&1)^1;
3548 else
3549 stat = OutOfMemory;
3551 break;
3553 /* else fall through */
3555 case DashStyleSolid:
3556 default:
3557 dash_pattern = static_dash_pattern;
3558 dash_pattern_size = 1;
3559 break;
3560 case DashStyleDash:
3561 dash_pattern = static_dash_pattern;
3562 dash_pattern_size = 4;
3563 break;
3564 case DashStyleDot:
3565 dash_pattern = &static_dash_pattern[4];
3566 dash_pattern_size = 2;
3567 break;
3568 case DashStyleDashDot:
3569 dash_pattern = static_dash_pattern;
3570 dash_pattern_size = 6;
3571 break;
3572 case DashStyleDashDotDot:
3573 dash_pattern = static_dash_pattern;
3574 dash_pattern_size = 8;
3575 break;
3579 if (stat == Ok)
3581 /* trace path */
3582 GpPointF subpath_start = flat_path->pathdata.Points[0];
3583 INT prev_x = INT_MAX, prev_y = INT_MAX;
3584 int dash_pos = dash_pattern_size - 1;
3586 for (i=0; i < flat_path->pathdata.Count; i++)
3588 BYTE type, type2;
3589 GpPointF start_point, end_point;
3590 GpPoint start_pointi, end_pointi;
3592 type = flat_path->pathdata.Types[i];
3593 if (i+1 < flat_path->pathdata.Count)
3594 type2 = flat_path->pathdata.Types[i+1];
3595 else
3596 type2 = PathPointTypeStart;
3598 start_point = flat_path->pathdata.Points[i];
3600 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3601 subpath_start = start_point;
3603 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3604 end_point = subpath_start;
3605 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3606 continue;
3607 else
3608 end_point = flat_path->pathdata.Points[i+1];
3610 start_pointi.X = floorf(start_point.X);
3611 start_pointi.Y = floorf(start_point.Y);
3612 end_pointi.X = floorf(end_point.X);
3613 end_pointi.Y = floorf(end_point.Y);
3615 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3616 continue;
3618 /* draw line segment */
3619 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3621 INT x, y, start_y, end_y, step;
3623 if (start_pointi.Y < end_pointi.Y)
3625 step = 1;
3626 start_y = ceilf(start_point.Y) - output_area.top;
3627 end_y = end_pointi.Y - output_area.top;
3629 else
3631 step = -1;
3632 start_y = start_point.Y - output_area.top;
3633 end_y = ceilf(end_point.Y) - output_area.top;
3636 for (y=start_y; y != (end_y+step); y+=step)
3638 x = gdip_round( start_point.X +
3639 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3640 - output_area.left;
3642 if (x == prev_x && y == prev_y)
3643 continue;
3645 prev_x = x;
3646 prev_y = y;
3647 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3649 if (!dash_pattern[dash_pos])
3650 continue;
3652 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3653 continue;
3655 if (brush_bits)
3656 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3657 else
3658 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3661 else
3663 INT x, y, start_x, end_x, step;
3665 if (start_pointi.X < end_pointi.X)
3667 step = 1;
3668 start_x = ceilf(start_point.X) - output_area.left;
3669 end_x = end_pointi.X - output_area.left;
3671 else
3673 step = -1;
3674 start_x = start_point.X - output_area.left;
3675 end_x = ceilf(end_point.X) - output_area.left;
3678 for (x=start_x; x != (end_x+step); x+=step)
3680 y = gdip_round( start_point.Y +
3681 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3682 - output_area.top;
3684 if (x == prev_x && y == prev_y)
3685 continue;
3687 prev_x = x;
3688 prev_y = y;
3689 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3691 if (!dash_pattern[dash_pos])
3692 continue;
3694 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3695 continue;
3697 if (brush_bits)
3698 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3699 else
3700 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3706 /* draw output image */
3707 if (stat == Ok)
3709 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3710 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3711 PixelFormat32bppARGB);
3714 heap_free(brush_bits);
3715 heap_free(dyn_dash_pattern);
3716 heap_free(output_bits);
3719 GdipDeletePath(flat_path);
3721 return stat;
3724 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3726 GpStatus stat;
3727 GpPath *wide_path;
3728 GpMatrix *transform=NULL;
3729 REAL flatness=1.0;
3731 /* Check if the final pen thickness in pixels is too thin. */
3732 if (pen->unit == UnitPixel)
3734 if (pen->width < 1.415)
3735 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3737 else
3739 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3741 points[1].X = pen->width;
3742 points[2].Y = pen->width;
3744 stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3745 CoordinateSpaceWorld, points, 3);
3747 if (stat != Ok)
3748 return stat;
3750 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3751 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3752 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3753 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3754 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3757 stat = GdipClonePath(path, &wide_path);
3759 if (stat != Ok)
3760 return stat;
3762 if (pen->unit == UnitPixel)
3764 /* We have to transform this to device coordinates to get the widths right. */
3765 stat = GdipCreateMatrix(&transform);
3767 if (stat == Ok)
3768 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3769 CoordinateSpaceWorld, transform);
3771 else
3773 /* Set flatness based on the final coordinate space */
3774 GpMatrix t;
3776 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3777 CoordinateSpaceWorld, &t);
3779 if (stat != Ok)
3780 return stat;
3782 flatness = 1.0/sqrt(fmax(
3783 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
3784 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
3787 if (stat == Ok)
3788 stat = GdipWidenPath(wide_path, pen, transform, flatness);
3790 if (pen->unit == UnitPixel)
3792 /* Transform the path back to world coordinates */
3793 if (stat == Ok)
3794 stat = GdipInvertMatrix(transform);
3796 if (stat == Ok)
3797 stat = GdipTransformPath(wide_path, transform);
3800 /* Actually draw the path */
3801 if (stat == Ok)
3802 stat = GdipFillPath(graphics, pen->brush, wide_path);
3804 GdipDeleteMatrix(transform);
3806 GdipDeletePath(wide_path);
3808 return stat;
3811 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3813 GpStatus retval;
3815 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3817 if(!pen || !graphics)
3818 return InvalidParameter;
3820 if(graphics->busy)
3821 return ObjectBusy;
3823 if (path->pathdata.Count == 0)
3824 return Ok;
3826 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3827 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
3828 else if (!graphics->hdc || !brush_can_fill_path(pen->brush, FALSE))
3829 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
3830 else
3831 retval = GDI32_GdipDrawPath(graphics, pen, path);
3833 return retval;
3836 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3837 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3839 GpStatus status;
3840 GpPath *path;
3842 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3843 width, height, startAngle, sweepAngle);
3845 if(!graphics || !pen)
3846 return InvalidParameter;
3848 if(graphics->busy)
3849 return ObjectBusy;
3851 status = GdipCreatePath(FillModeAlternate, &path);
3852 if (status != Ok) return status;
3854 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3855 if (status == Ok)
3856 status = GdipDrawPath(graphics, pen, path);
3858 GdipDeletePath(path);
3859 return status;
3862 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3863 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3865 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3866 width, height, startAngle, sweepAngle);
3868 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3871 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3872 REAL y, REAL width, REAL height)
3874 GpStatus status;
3875 GpPath *path;
3877 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3879 if(!pen || !graphics)
3880 return InvalidParameter;
3882 if(graphics->busy)
3883 return ObjectBusy;
3885 status = GdipCreatePath(FillModeAlternate, &path);
3886 if (status != Ok) return status;
3888 status = GdipAddPathRectangle(path, x, y, width, height);
3889 if (status == Ok)
3890 status = GdipDrawPath(graphics, pen, path);
3892 GdipDeletePath(path);
3893 return status;
3896 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3897 INT y, INT width, INT height)
3899 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3901 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3904 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3905 GDIPCONST GpRectF* rects, INT count)
3907 GpStatus status;
3908 GpPath *path;
3910 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3912 if(!graphics || !pen || !rects || count < 1)
3913 return InvalidParameter;
3915 if(graphics->busy)
3916 return ObjectBusy;
3918 status = GdipCreatePath(FillModeAlternate, &path);
3919 if (status != Ok) return status;
3921 status = GdipAddPathRectangles(path, rects, count);
3922 if (status == Ok)
3923 status = GdipDrawPath(graphics, pen, path);
3925 GdipDeletePath(path);
3926 return status;
3929 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3930 GDIPCONST GpRect* rects, INT count)
3932 GpRectF *rectsF;
3933 GpStatus ret;
3934 INT i;
3936 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3938 if(!rects || count<=0)
3939 return InvalidParameter;
3941 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
3942 if(!rectsF)
3943 return OutOfMemory;
3945 for(i = 0;i < count;i++){
3946 rectsF[i].X = (REAL)rects[i].X;
3947 rectsF[i].Y = (REAL)rects[i].Y;
3948 rectsF[i].Width = (REAL)rects[i].Width;
3949 rectsF[i].Height = (REAL)rects[i].Height;
3952 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3953 heap_free(rectsF);
3955 return ret;
3958 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3959 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3961 GpPath *path;
3962 GpStatus status;
3964 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3965 count, tension, fill);
3967 if(!graphics || !brush || !points)
3968 return InvalidParameter;
3970 if(graphics->busy)
3971 return ObjectBusy;
3973 if(count == 1) /* Do nothing */
3974 return Ok;
3976 status = GdipCreatePath(fill, &path);
3977 if (status != Ok) return status;
3979 status = GdipAddPathClosedCurve2(path, points, count, tension);
3980 if (status == Ok)
3981 status = GdipFillPath(graphics, brush, path);
3983 GdipDeletePath(path);
3984 return status;
3987 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3988 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3990 GpPointF *ptf;
3991 GpStatus stat;
3992 INT i;
3994 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3995 count, tension, fill);
3997 if(!points || count == 0)
3998 return InvalidParameter;
4000 if(count == 1) /* Do nothing */
4001 return Ok;
4003 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4004 if(!ptf)
4005 return OutOfMemory;
4007 for(i = 0;i < count;i++){
4008 ptf[i].X = (REAL)points[i].X;
4009 ptf[i].Y = (REAL)points[i].Y;
4012 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4014 heap_free(ptf);
4016 return stat;
4019 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4020 GDIPCONST GpPointF *points, INT count)
4022 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4023 return GdipFillClosedCurve2(graphics, brush, points, count,
4024 0.5f, FillModeAlternate);
4027 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4028 GDIPCONST GpPoint *points, INT count)
4030 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4031 return GdipFillClosedCurve2I(graphics, brush, points, count,
4032 0.5f, FillModeAlternate);
4035 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4036 REAL y, REAL width, REAL height)
4038 GpStatus stat;
4039 GpPath *path;
4041 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4043 if(!graphics || !brush)
4044 return InvalidParameter;
4046 if(graphics->busy)
4047 return ObjectBusy;
4049 stat = GdipCreatePath(FillModeAlternate, &path);
4051 if (stat == Ok)
4053 stat = GdipAddPathEllipse(path, x, y, width, height);
4055 if (stat == Ok)
4056 stat = GdipFillPath(graphics, brush, path);
4058 GdipDeletePath(path);
4061 return stat;
4064 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4065 INT y, INT width, INT height)
4067 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4069 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4072 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4074 INT save_state;
4075 GpStatus retval;
4076 HRGN hrgn=NULL;
4078 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4079 return NotImplemented;
4081 save_state = SaveDC(graphics->hdc);
4082 EndPath(graphics->hdc);
4083 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4084 : WINDING));
4086 retval = get_clip_hrgn(graphics, &hrgn);
4088 if (retval != Ok)
4089 goto end;
4091 if (hrgn)
4092 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4094 BeginPath(graphics->hdc);
4095 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4096 path->pathdata.Types, path->pathdata.Count, FALSE);
4098 if(retval != Ok)
4099 goto end;
4101 EndPath(graphics->hdc);
4102 brush_fill_path(graphics, brush);
4104 retval = Ok;
4106 end:
4107 RestoreDC(graphics->hdc, save_state);
4108 DeleteObject(hrgn);
4110 return retval;
4113 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4115 GpStatus stat;
4116 GpRegion *rgn;
4118 if (!brush_can_fill_pixels(brush))
4119 return NotImplemented;
4121 /* FIXME: This could probably be done more efficiently without regions. */
4123 stat = GdipCreateRegionPath(path, &rgn);
4125 if (stat == Ok)
4127 stat = GdipFillRegion(graphics, brush, rgn);
4129 GdipDeleteRegion(rgn);
4132 return stat;
4135 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4137 GpStatus stat = NotImplemented;
4139 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4141 if(!brush || !graphics || !path)
4142 return InvalidParameter;
4144 if(graphics->busy)
4145 return ObjectBusy;
4147 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4148 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4150 if (!graphics->image && !graphics->alpha_hdc)
4151 stat = GDI32_GdipFillPath(graphics, brush, path);
4153 if (stat == NotImplemented)
4154 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4156 if (stat == NotImplemented)
4158 FIXME("Not implemented for brushtype %i\n", brush->bt);
4159 stat = Ok;
4162 return stat;
4165 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4166 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4168 GpStatus stat;
4169 GpPath *path;
4171 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4172 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4174 if(!graphics || !brush)
4175 return InvalidParameter;
4177 if(graphics->busy)
4178 return ObjectBusy;
4180 stat = GdipCreatePath(FillModeAlternate, &path);
4182 if (stat == Ok)
4184 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4186 if (stat == Ok)
4187 stat = GdipFillPath(graphics, brush, path);
4189 GdipDeletePath(path);
4192 return stat;
4195 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4196 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4198 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4199 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4201 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4204 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4205 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4207 GpStatus stat;
4208 GpPath *path;
4210 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4212 if(!graphics || !brush || !points || !count)
4213 return InvalidParameter;
4215 if(graphics->busy)
4216 return ObjectBusy;
4218 stat = GdipCreatePath(fillMode, &path);
4220 if (stat == Ok)
4222 stat = GdipAddPathPolygon(path, points, count);
4224 if (stat == Ok)
4225 stat = GdipFillPath(graphics, brush, path);
4227 GdipDeletePath(path);
4230 return stat;
4233 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4234 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4236 GpStatus stat;
4237 GpPath *path;
4239 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4241 if(!graphics || !brush || !points || !count)
4242 return InvalidParameter;
4244 if(graphics->busy)
4245 return ObjectBusy;
4247 stat = GdipCreatePath(fillMode, &path);
4249 if (stat == Ok)
4251 stat = GdipAddPathPolygonI(path, points, count);
4253 if (stat == Ok)
4254 stat = GdipFillPath(graphics, brush, path);
4256 GdipDeletePath(path);
4259 return stat;
4262 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4263 GDIPCONST GpPointF *points, INT count)
4265 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4267 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4270 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4271 GDIPCONST GpPoint *points, INT count)
4273 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4275 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4278 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4279 REAL x, REAL y, REAL width, REAL height)
4281 GpRectF rect;
4283 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4285 rect.X = x;
4286 rect.Y = y;
4287 rect.Width = width;
4288 rect.Height = height;
4290 return GdipFillRectangles(graphics, brush, &rect, 1);
4293 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4294 INT x, INT y, INT width, INT height)
4296 GpRectF rect;
4298 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4300 rect.X = (REAL)x;
4301 rect.Y = (REAL)y;
4302 rect.Width = (REAL)width;
4303 rect.Height = (REAL)height;
4305 return GdipFillRectangles(graphics, brush, &rect, 1);
4308 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4309 INT count)
4311 GpStatus status;
4312 GpPath *path;
4314 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4316 if(!graphics || !brush || !rects || count <= 0)
4317 return InvalidParameter;
4319 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4321 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4322 /* FIXME: Add gdi32 drawing. */
4323 return status;
4326 status = GdipCreatePath(FillModeAlternate, &path);
4327 if (status != Ok) return status;
4329 status = GdipAddPathRectangles(path, rects, count);
4330 if (status == Ok)
4331 status = GdipFillPath(graphics, brush, path);
4333 GdipDeletePath(path);
4334 return status;
4337 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4338 INT count)
4340 GpRectF *rectsF;
4341 GpStatus ret;
4342 INT i;
4344 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4346 if(!rects || count <= 0)
4347 return InvalidParameter;
4349 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4350 if(!rectsF)
4351 return OutOfMemory;
4353 for(i = 0; i < count; i++){
4354 rectsF[i].X = (REAL)rects[i].X;
4355 rectsF[i].Y = (REAL)rects[i].Y;
4356 rectsF[i].X = (REAL)rects[i].Width;
4357 rectsF[i].Height = (REAL)rects[i].Height;
4360 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4361 heap_free(rectsF);
4363 return ret;
4366 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4367 GpRegion* region)
4369 INT save_state;
4370 GpStatus status;
4371 HRGN hrgn;
4372 RECT rc;
4374 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4375 return NotImplemented;
4377 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4378 if(status != Ok)
4379 return status;
4381 save_state = SaveDC(graphics->hdc);
4382 EndPath(graphics->hdc);
4384 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4386 DeleteObject(hrgn);
4388 hrgn = NULL;
4389 status = get_clip_hrgn(graphics, &hrgn);
4391 if (status != Ok)
4393 RestoreDC(graphics->hdc, save_state);
4394 return status;
4397 if (hrgn)
4399 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4400 DeleteObject(hrgn);
4403 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4405 BeginPath(graphics->hdc);
4406 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4407 EndPath(graphics->hdc);
4409 brush_fill_path(graphics, brush);
4412 RestoreDC(graphics->hdc, save_state);
4415 return Ok;
4418 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4419 GpRegion* region)
4421 GpStatus stat;
4422 GpRegion *temp_region;
4423 GpMatrix world_to_device;
4424 GpRectF graphics_bounds;
4425 DWORD *pixel_data;
4426 HRGN hregion;
4427 RECT bound_rect;
4428 GpRect gp_bound_rect;
4430 if (!brush_can_fill_pixels(brush))
4431 return NotImplemented;
4433 stat = get_graphics_bounds(graphics, &graphics_bounds);
4435 if (stat == Ok)
4436 stat = GdipCloneRegion(region, &temp_region);
4438 if (stat == Ok)
4440 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4441 CoordinateSpaceWorld, &world_to_device);
4443 if (stat == Ok)
4444 stat = GdipTransformRegion(temp_region, &world_to_device);
4446 if (stat == Ok)
4447 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4449 if (stat == Ok)
4450 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4452 GdipDeleteRegion(temp_region);
4455 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4457 DeleteObject(hregion);
4458 return Ok;
4461 if (stat == Ok)
4463 gp_bound_rect.X = bound_rect.left;
4464 gp_bound_rect.Y = bound_rect.top;
4465 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4466 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4468 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4469 if (!pixel_data)
4470 stat = OutOfMemory;
4472 if (stat == Ok)
4474 stat = brush_fill_pixels(graphics, brush, pixel_data,
4475 &gp_bound_rect, gp_bound_rect.Width);
4477 if (stat == Ok)
4478 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4479 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4480 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4481 PixelFormat32bppARGB);
4483 heap_free(pixel_data);
4486 DeleteObject(hregion);
4489 return stat;
4492 /*****************************************************************************
4493 * GdipFillRegion [GDIPLUS.@]
4495 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4496 GpRegion* region)
4498 GpStatus stat = NotImplemented;
4500 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4502 if (!(graphics && brush && region))
4503 return InvalidParameter;
4505 if(graphics->busy)
4506 return ObjectBusy;
4508 if (!graphics->image && !graphics->alpha_hdc)
4509 stat = GDI32_GdipFillRegion(graphics, brush, region);
4511 if (stat == NotImplemented)
4512 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4514 if (stat == NotImplemented)
4516 FIXME("not implemented for brushtype %i\n", brush->bt);
4517 stat = Ok;
4520 return stat;
4523 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4525 TRACE("(%p,%u)\n", graphics, intention);
4527 if(!graphics)
4528 return InvalidParameter;
4530 if(graphics->busy)
4531 return ObjectBusy;
4533 /* We have no internal operation queue, so there's no need to clear it. */
4535 if (graphics->hdc)
4536 GdiFlush();
4538 return Ok;
4541 /*****************************************************************************
4542 * GdipGetClipBounds [GDIPLUS.@]
4544 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4546 GpStatus status;
4547 GpRegion *clip;
4549 TRACE("(%p, %p)\n", graphics, rect);
4551 if(!graphics)
4552 return InvalidParameter;
4554 if(graphics->busy)
4555 return ObjectBusy;
4557 status = GdipCreateRegion(&clip);
4558 if (status != Ok) return status;
4560 status = GdipGetClip(graphics, clip);
4561 if (status == Ok)
4562 status = GdipGetRegionBounds(clip, graphics, rect);
4564 GdipDeleteRegion(clip);
4565 return status;
4568 /*****************************************************************************
4569 * GdipGetClipBoundsI [GDIPLUS.@]
4571 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4573 TRACE("(%p, %p)\n", graphics, rect);
4575 if(!graphics)
4576 return InvalidParameter;
4578 if(graphics->busy)
4579 return ObjectBusy;
4581 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4584 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4585 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4586 CompositingMode *mode)
4588 TRACE("(%p, %p)\n", graphics, mode);
4590 if(!graphics || !mode)
4591 return InvalidParameter;
4593 if(graphics->busy)
4594 return ObjectBusy;
4596 *mode = graphics->compmode;
4598 return Ok;
4601 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4602 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4603 CompositingQuality *quality)
4605 TRACE("(%p, %p)\n", graphics, quality);
4607 if(!graphics || !quality)
4608 return InvalidParameter;
4610 if(graphics->busy)
4611 return ObjectBusy;
4613 *quality = graphics->compqual;
4615 return Ok;
4618 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4619 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4620 InterpolationMode *mode)
4622 TRACE("(%p, %p)\n", graphics, mode);
4624 if(!graphics || !mode)
4625 return InvalidParameter;
4627 if(graphics->busy)
4628 return ObjectBusy;
4630 *mode = graphics->interpolation;
4632 return Ok;
4635 /* FIXME: Need to handle color depths less than 24bpp */
4636 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4638 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4640 if(!graphics || !argb)
4641 return InvalidParameter;
4643 if(graphics->busy)
4644 return ObjectBusy;
4646 return Ok;
4649 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4651 TRACE("(%p, %p)\n", graphics, scale);
4653 if(!graphics || !scale)
4654 return InvalidParameter;
4656 if(graphics->busy)
4657 return ObjectBusy;
4659 *scale = graphics->scale;
4661 return Ok;
4664 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4666 TRACE("(%p, %p)\n", graphics, unit);
4668 if(!graphics || !unit)
4669 return InvalidParameter;
4671 if(graphics->busy)
4672 return ObjectBusy;
4674 *unit = graphics->unit;
4676 return Ok;
4679 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4680 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4681 *mode)
4683 TRACE("(%p, %p)\n", graphics, mode);
4685 if(!graphics || !mode)
4686 return InvalidParameter;
4688 if(graphics->busy)
4689 return ObjectBusy;
4691 *mode = graphics->pixeloffset;
4693 return Ok;
4696 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4697 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4699 TRACE("(%p, %p)\n", graphics, mode);
4701 if(!graphics || !mode)
4702 return InvalidParameter;
4704 if(graphics->busy)
4705 return ObjectBusy;
4707 *mode = graphics->smoothing;
4709 return Ok;
4712 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4714 TRACE("(%p, %p)\n", graphics, contrast);
4716 if(!graphics || !contrast)
4717 return InvalidParameter;
4719 *contrast = graphics->textcontrast;
4721 return Ok;
4724 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4725 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4726 TextRenderingHint *hint)
4728 TRACE("(%p, %p)\n", graphics, hint);
4730 if(!graphics || !hint)
4731 return InvalidParameter;
4733 if(graphics->busy)
4734 return ObjectBusy;
4736 *hint = graphics->texthint;
4738 return Ok;
4741 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4743 GpRegion *clip_rgn;
4744 GpStatus stat;
4745 GpMatrix device_to_world;
4747 TRACE("(%p, %p)\n", graphics, rect);
4749 if(!graphics || !rect)
4750 return InvalidParameter;
4752 if(graphics->busy)
4753 return ObjectBusy;
4755 /* intersect window and graphics clipping regions */
4756 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4757 return stat;
4759 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4760 goto cleanup;
4762 /* transform to world coordinates */
4763 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4764 goto cleanup;
4766 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4767 goto cleanup;
4769 /* get bounds of the region */
4770 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4772 cleanup:
4773 GdipDeleteRegion(clip_rgn);
4775 return stat;
4778 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4780 GpRectF rectf;
4781 GpStatus stat;
4783 TRACE("(%p, %p)\n", graphics, rect);
4785 if(!graphics || !rect)
4786 return InvalidParameter;
4788 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4790 rect->X = gdip_round(rectf.X);
4791 rect->Y = gdip_round(rectf.Y);
4792 rect->Width = gdip_round(rectf.Width);
4793 rect->Height = gdip_round(rectf.Height);
4796 return stat;
4799 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4801 TRACE("(%p, %p)\n", graphics, matrix);
4803 if(!graphics || !matrix)
4804 return InvalidParameter;
4806 if(graphics->busy)
4807 return ObjectBusy;
4809 *matrix = graphics->worldtrans;
4810 return Ok;
4813 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4815 GpSolidFill *brush;
4816 GpStatus stat;
4817 GpRectF wnd_rect;
4819 TRACE("(%p, %x)\n", graphics, color);
4821 if(!graphics)
4822 return InvalidParameter;
4824 if(graphics->busy)
4825 return ObjectBusy;
4827 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4828 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
4830 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4831 return stat;
4833 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
4834 GdipDeleteBrush((GpBrush*)brush);
4835 return stat;
4838 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4839 wnd_rect.Width, wnd_rect.Height);
4841 GdipDeleteBrush((GpBrush*)brush);
4843 return Ok;
4846 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4848 TRACE("(%p, %p)\n", graphics, res);
4850 if(!graphics || !res)
4851 return InvalidParameter;
4853 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4856 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4858 GpStatus stat;
4859 GpRegion* rgn;
4860 GpPointF pt;
4862 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4864 if(!graphics || !result)
4865 return InvalidParameter;
4867 if(graphics->busy)
4868 return ObjectBusy;
4870 pt.X = x;
4871 pt.Y = y;
4872 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4873 CoordinateSpaceWorld, &pt, 1)) != Ok)
4874 return stat;
4876 if((stat = GdipCreateRegion(&rgn)) != Ok)
4877 return stat;
4879 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4880 goto cleanup;
4882 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4884 cleanup:
4885 GdipDeleteRegion(rgn);
4886 return stat;
4889 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4891 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4894 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4896 GpStatus stat;
4897 GpRegion* rgn;
4898 GpPointF pts[2];
4900 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4902 if(!graphics || !result)
4903 return InvalidParameter;
4905 if(graphics->busy)
4906 return ObjectBusy;
4908 pts[0].X = x;
4909 pts[0].Y = y;
4910 pts[1].X = x + width;
4911 pts[1].Y = y + height;
4913 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4914 CoordinateSpaceWorld, pts, 2)) != Ok)
4915 return stat;
4917 pts[1].X -= pts[0].X;
4918 pts[1].Y -= pts[0].Y;
4920 if((stat = GdipCreateRegion(&rgn)) != Ok)
4921 return stat;
4923 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4924 goto cleanup;
4926 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4928 cleanup:
4929 GdipDeleteRegion(rgn);
4930 return stat;
4933 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4935 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4938 GpStatus gdip_format_string(HDC hdc,
4939 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4940 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4941 gdip_format_string_callback callback, void *user_data)
4943 WCHAR* stringdup;
4944 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4945 nheight, lineend, lineno = 0;
4946 RectF bounds;
4947 StringAlignment halign;
4948 GpStatus stat = Ok;
4949 SIZE size;
4950 HotkeyPrefix hkprefix;
4951 INT *hotkeyprefix_offsets=NULL;
4952 INT hotkeyprefix_count=0;
4953 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4954 BOOL seen_prefix = FALSE;
4956 if(length == -1) length = lstrlenW(string);
4958 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
4959 if(!stringdup) return OutOfMemory;
4961 if (!format)
4962 format = &default_drawstring_format;
4964 nwidth = rect->Width;
4965 nheight = rect->Height;
4966 if (ignore_empty_clip)
4968 if (!nwidth) nwidth = INT_MAX;
4969 if (!nheight) nheight = INT_MAX;
4972 hkprefix = format->hkprefix;
4974 if (hkprefix == HotkeyPrefixShow)
4976 for (i=0; i<length; i++)
4978 if (string[i] == '&')
4979 hotkeyprefix_count++;
4983 if (hotkeyprefix_count)
4984 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
4986 hotkeyprefix_count = 0;
4988 for(i = 0, j = 0; i < length; i++){
4989 /* FIXME: This makes the indexes passed to callback inaccurate. */
4990 if(!isprintW(string[i]) && (string[i] != '\n'))
4991 continue;
4993 /* FIXME: tabs should be handled using tabstops from stringformat */
4994 if (string[i] == '\t')
4995 continue;
4997 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4998 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4999 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5001 seen_prefix = TRUE;
5002 continue;
5005 seen_prefix = FALSE;
5007 stringdup[j] = string[i];
5008 j++;
5011 length = j;
5013 halign = format->align;
5015 while(sum < length){
5016 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5017 nwidth, &fit, NULL, &size);
5018 fitcpy = fit;
5020 if(fit == 0)
5021 break;
5023 for(lret = 0; lret < fit; lret++)
5024 if(*(stringdup + sum + lret) == '\n')
5025 break;
5027 /* Line break code (may look strange, but it imitates windows). */
5028 if(lret < fit)
5029 lineend = fit = lret; /* this is not an off-by-one error */
5030 else if(fit < (length - sum)){
5031 if(*(stringdup + sum + fit) == ' ')
5032 while(*(stringdup + sum + fit) == ' ')
5033 fit++;
5034 else
5035 while(*(stringdup + sum + fit - 1) != ' '){
5036 fit--;
5038 if(*(stringdup + sum + fit) == '\t')
5039 break;
5041 if(fit == 0){
5042 fit = fitcpy;
5043 break;
5046 lineend = fit;
5047 while(*(stringdup + sum + lineend - 1) == ' ' ||
5048 *(stringdup + sum + lineend - 1) == '\t')
5049 lineend--;
5051 else
5052 lineend = fit;
5054 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5055 nwidth, &j, NULL, &size);
5057 bounds.Width = size.cx;
5059 if(height + size.cy > nheight)
5061 if (format->attr & StringFormatFlagsLineLimit)
5062 break;
5063 bounds.Height = nheight - (height + size.cy);
5065 else
5066 bounds.Height = size.cy;
5068 bounds.Y = rect->Y + height;
5070 switch (halign)
5072 case StringAlignmentNear:
5073 default:
5074 bounds.X = rect->X;
5075 break;
5076 case StringAlignmentCenter:
5077 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5078 break;
5079 case StringAlignmentFar:
5080 bounds.X = rect->X + rect->Width - bounds.Width;
5081 break;
5084 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5085 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5086 break;
5088 stat = callback(hdc, stringdup, sum, lineend,
5089 font, rect, format, lineno, &bounds,
5090 &hotkeyprefix_offsets[hotkeyprefix_pos],
5091 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5093 if (stat != Ok)
5094 break;
5096 sum += fit + (lret < fitcpy ? 1 : 0);
5097 height += size.cy;
5098 lineno++;
5100 hotkeyprefix_pos = hotkeyprefix_end_pos;
5102 if(height > nheight)
5103 break;
5105 /* Stop if this was a linewrap (but not if it was a linebreak). */
5106 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5107 break;
5110 heap_free(stringdup);
5111 heap_free(hotkeyprefix_offsets);
5113 return stat;
5116 struct measure_ranges_args {
5117 GpRegion **regions;
5118 REAL rel_width, rel_height;
5121 static GpStatus measure_ranges_callback(HDC hdc,
5122 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5123 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5124 INT lineno, const RectF *bounds, INT *underlined_indexes,
5125 INT underlined_index_count, void *user_data)
5127 int i;
5128 GpStatus stat = Ok;
5129 struct measure_ranges_args *args = user_data;
5131 for (i=0; i<format->range_count; i++)
5133 INT range_start = max(index, format->character_ranges[i].First);
5134 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5135 if (range_start < range_end)
5137 GpRectF range_rect;
5138 SIZE range_size;
5140 range_rect.Y = bounds->Y / args->rel_height;
5141 range_rect.Height = bounds->Height / args->rel_height;
5143 GetTextExtentExPointW(hdc, string + index, range_start - index,
5144 INT_MAX, NULL, NULL, &range_size);
5145 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5147 GetTextExtentExPointW(hdc, string + index, range_end - index,
5148 INT_MAX, NULL, NULL, &range_size);
5149 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5151 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5152 if (stat != Ok)
5153 break;
5157 return stat;
5160 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5161 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5162 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5163 INT regionCount, GpRegion** regions)
5165 GpStatus stat;
5166 int i;
5167 HFONT gdifont, oldfont;
5168 struct measure_ranges_args args;
5169 HDC hdc, temp_hdc=NULL;
5170 GpPointF pt[3];
5171 RectF scaled_rect;
5172 REAL margin_x;
5174 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
5175 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5177 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5178 return InvalidParameter;
5180 if (regionCount < stringFormat->range_count)
5181 return InvalidParameter;
5183 if(!graphics->hdc)
5185 hdc = temp_hdc = CreateCompatibleDC(0);
5186 if (!temp_hdc) return OutOfMemory;
5188 else
5189 hdc = graphics->hdc;
5191 if (stringFormat->attr)
5192 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5194 pt[0].X = 0.0;
5195 pt[0].Y = 0.0;
5196 pt[1].X = 1.0;
5197 pt[1].Y = 0.0;
5198 pt[2].X = 0.0;
5199 pt[2].Y = 1.0;
5200 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5201 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5202 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5203 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5204 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5206 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5207 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5209 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5210 scaled_rect.Y = layoutRect->Y * args.rel_height;
5211 scaled_rect.Width = layoutRect->Width * args.rel_width;
5212 scaled_rect.Height = layoutRect->Height * args.rel_height;
5214 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5215 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5217 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5218 oldfont = SelectObject(hdc, gdifont);
5220 for (i=0; i<stringFormat->range_count; i++)
5222 stat = GdipSetEmpty(regions[i]);
5223 if (stat != Ok)
5224 return stat;
5227 args.regions = regions;
5229 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5230 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5232 SelectObject(hdc, oldfont);
5233 DeleteObject(gdifont);
5235 if (temp_hdc)
5236 DeleteDC(temp_hdc);
5238 return stat;
5241 struct measure_string_args {
5242 RectF *bounds;
5243 INT *codepointsfitted;
5244 INT *linesfilled;
5245 REAL rel_width, rel_height;
5248 static GpStatus measure_string_callback(HDC hdc,
5249 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5250 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5251 INT lineno, const RectF *bounds, INT *underlined_indexes,
5252 INT underlined_index_count, void *user_data)
5254 struct measure_string_args *args = user_data;
5255 REAL new_width, new_height;
5257 new_width = bounds->Width / args->rel_width;
5258 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5260 if (new_width > args->bounds->Width)
5261 args->bounds->Width = new_width;
5263 if (new_height > args->bounds->Height)
5264 args->bounds->Height = new_height;
5266 if (args->codepointsfitted)
5267 *args->codepointsfitted = index + length;
5269 if (args->linesfilled)
5270 (*args->linesfilled)++;
5272 return Ok;
5275 /* Find the smallest rectangle that bounds the text when it is printed in rect
5276 * according to the format options listed in format. If rect has 0 width and
5277 * height, then just find the smallest rectangle that bounds the text when it's
5278 * printed at location (rect->X, rect-Y). */
5279 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5280 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5281 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5282 INT *codepointsfitted, INT *linesfilled)
5284 HFONT oldfont, gdifont;
5285 struct measure_string_args args;
5286 HDC temp_hdc=NULL, hdc;
5287 GpPointF pt[3];
5288 RectF scaled_rect;
5289 REAL margin_x;
5290 INT lines, glyphs;
5292 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5293 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5294 bounds, codepointsfitted, linesfilled);
5296 if(!graphics || !string || !font || !rect || !bounds)
5297 return InvalidParameter;
5299 if(!graphics->hdc)
5301 hdc = temp_hdc = CreateCompatibleDC(0);
5302 if (!temp_hdc) return OutOfMemory;
5304 else
5305 hdc = graphics->hdc;
5307 if(linesfilled) *linesfilled = 0;
5308 if(codepointsfitted) *codepointsfitted = 0;
5310 if(format)
5311 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5313 pt[0].X = 0.0;
5314 pt[0].Y = 0.0;
5315 pt[1].X = 1.0;
5316 pt[1].Y = 0.0;
5317 pt[2].X = 0.0;
5318 pt[2].Y = 1.0;
5319 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5320 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5321 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5322 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5323 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5325 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5326 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5328 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5329 scaled_rect.Y = rect->Y * args.rel_height;
5330 scaled_rect.Width = rect->Width * args.rel_width;
5331 scaled_rect.Height = rect->Height * args.rel_height;
5332 if (scaled_rect.Width >= 0.5)
5334 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5335 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5338 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5339 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5341 get_font_hfont(graphics, font, format, &gdifont, NULL);
5342 oldfont = SelectObject(hdc, gdifont);
5344 bounds->X = rect->X;
5345 bounds->Y = rect->Y;
5346 bounds->Width = 0.0;
5347 bounds->Height = 0.0;
5349 args.bounds = bounds;
5350 args.codepointsfitted = &glyphs;
5351 args.linesfilled = &lines;
5352 lines = glyphs = 0;
5354 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5355 measure_string_callback, &args);
5357 if (linesfilled) *linesfilled = lines;
5358 if (codepointsfitted) *codepointsfitted = glyphs;
5360 if (lines)
5361 bounds->Width += margin_x * 2.0;
5363 SelectObject(hdc, oldfont);
5364 DeleteObject(gdifont);
5366 if (temp_hdc)
5367 DeleteDC(temp_hdc);
5369 return Ok;
5372 struct draw_string_args {
5373 GpGraphics *graphics;
5374 GDIPCONST GpBrush *brush;
5375 REAL x, y, rel_width, rel_height, ascent;
5378 static GpStatus draw_string_callback(HDC hdc,
5379 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5380 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5381 INT lineno, const RectF *bounds, INT *underlined_indexes,
5382 INT underlined_index_count, void *user_data)
5384 struct draw_string_args *args = user_data;
5385 PointF position;
5386 GpStatus stat;
5388 position.X = args->x + bounds->X / args->rel_width;
5389 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5391 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5392 args->brush, &position,
5393 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5395 if (stat == Ok && underlined_index_count)
5397 OUTLINETEXTMETRICW otm;
5398 REAL underline_y, underline_height;
5399 int i;
5401 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5403 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5404 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5406 for (i=0; i<underlined_index_count; i++)
5408 REAL start_x, end_x;
5409 SIZE text_size;
5410 INT ofs = underlined_indexes[i] - index;
5412 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5413 start_x = text_size.cx / args->rel_width;
5415 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5416 end_x = text_size.cx / args->rel_width;
5418 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5422 return stat;
5425 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5426 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5427 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5429 HRGN rgn = NULL;
5430 HFONT gdifont;
5431 GpPointF pt[3], rectcpy[4];
5432 POINT corners[4];
5433 REAL rel_width, rel_height, margin_x;
5434 INT save_state, format_flags = 0;
5435 REAL offsety = 0.0;
5436 struct draw_string_args args;
5437 RectF scaled_rect;
5438 HDC hdc, temp_hdc=NULL;
5439 TEXTMETRICW textmetric;
5441 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5442 length, font, debugstr_rectf(rect), format, brush);
5444 if(!graphics || !string || !font || !brush || !rect)
5445 return InvalidParameter;
5447 if(graphics->hdc)
5449 hdc = graphics->hdc;
5451 else
5453 hdc = temp_hdc = CreateCompatibleDC(0);
5456 if(format){
5457 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5459 format_flags = format->attr;
5461 /* Should be no need to explicitly test for StringAlignmentNear as
5462 * that is default behavior if no alignment is passed. */
5463 if(format->line_align != StringAlignmentNear){
5464 RectF bounds, in_rect = *rect;
5465 in_rect.Height = 0.0; /* avoid height clipping */
5466 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5468 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5470 if(format->line_align == StringAlignmentCenter)
5471 offsety = (rect->Height - bounds.Height) / 2;
5472 else if(format->line_align == StringAlignmentFar)
5473 offsety = (rect->Height - bounds.Height);
5475 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5478 save_state = SaveDC(hdc);
5480 pt[0].X = 0.0;
5481 pt[0].Y = 0.0;
5482 pt[1].X = 1.0;
5483 pt[1].Y = 0.0;
5484 pt[2].X = 0.0;
5485 pt[2].Y = 1.0;
5486 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5487 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5488 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5489 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5490 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5492 rectcpy[3].X = rectcpy[0].X = rect->X;
5493 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5494 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5495 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5496 transform_and_round_points(graphics, corners, rectcpy, 4);
5498 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5499 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5501 scaled_rect.X = margin_x * rel_width;
5502 scaled_rect.Y = 0.0;
5503 scaled_rect.Width = rel_width * rect->Width;
5504 scaled_rect.Height = rel_height * rect->Height;
5505 if (scaled_rect.Width >= 0.5)
5507 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5508 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5511 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5512 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5514 if (!(format_flags & StringFormatFlagsNoClip) &&
5515 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5516 rect->Width > 0.0 && rect->Height > 0.0)
5518 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5519 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5520 SelectClipRgn(hdc, rgn);
5523 get_font_hfont(graphics, font, format, &gdifont, NULL);
5524 SelectObject(hdc, gdifont);
5526 args.graphics = graphics;
5527 args.brush = brush;
5529 args.x = rect->X;
5530 args.y = rect->Y + offsety;
5532 args.rel_width = rel_width;
5533 args.rel_height = rel_height;
5535 GetTextMetricsW(hdc, &textmetric);
5536 args.ascent = textmetric.tmAscent / rel_height;
5538 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5539 draw_string_callback, &args);
5541 DeleteObject(rgn);
5542 DeleteObject(gdifont);
5544 RestoreDC(hdc, save_state);
5546 DeleteDC(temp_hdc);
5548 return Ok;
5551 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5553 TRACE("(%p)\n", graphics);
5555 if(!graphics)
5556 return InvalidParameter;
5558 if(graphics->busy)
5559 return ObjectBusy;
5561 return GdipSetInfinite(graphics->clip);
5564 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5566 GpStatus stat;
5568 TRACE("(%p)\n", graphics);
5570 if(!graphics)
5571 return InvalidParameter;
5573 if(graphics->busy)
5574 return ObjectBusy;
5576 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5577 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5579 if (stat != Ok)
5580 return stat;
5583 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5586 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5587 GpMatrixOrder order)
5589 GpStatus stat;
5591 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5593 if(!graphics)
5594 return InvalidParameter;
5596 if(graphics->busy)
5597 return ObjectBusy;
5599 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5600 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5602 if (stat != Ok)
5603 return stat;
5606 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5609 static GpStatus begin_container(GpGraphics *graphics,
5610 GraphicsContainerType type, GraphicsContainer *state)
5612 GraphicsContainerItem *container;
5613 GpStatus sts;
5615 if(!graphics || !state)
5616 return InvalidParameter;
5618 sts = init_container(&container, graphics, type);
5619 if(sts != Ok)
5620 return sts;
5622 list_add_head(&graphics->containers, &container->entry);
5623 *state = graphics->contid = container->contid;
5625 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5626 if (type == BEGIN_CONTAINER)
5627 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5628 else
5629 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5632 return Ok;
5635 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5637 TRACE("(%p, %p)\n", graphics, state);
5638 return begin_container(graphics, SAVE_GRAPHICS, state);
5641 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5642 GraphicsContainer *state)
5644 TRACE("(%p, %p)\n", graphics, state);
5645 return begin_container(graphics, BEGIN_CONTAINER, state);
5648 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5650 GraphicsContainerItem *container;
5651 GpMatrix transform;
5652 GpStatus stat;
5653 GpRectF scaled_srcrect;
5654 REAL scale_x, scale_y;
5656 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5658 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5659 return InvalidParameter;
5661 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5662 if(stat != Ok)
5663 return stat;
5665 list_add_head(&graphics->containers, &container->entry);
5666 *state = graphics->contid = container->contid;
5668 scale_x = units_to_pixels(1.0, unit, graphics->xres);
5669 scale_y = units_to_pixels(1.0, unit, graphics->yres);
5671 scaled_srcrect.X = scale_x * srcrect->X;
5672 scaled_srcrect.Y = scale_y * srcrect->Y;
5673 scaled_srcrect.Width = scale_x * srcrect->Width;
5674 scaled_srcrect.Height = scale_y * srcrect->Height;
5676 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5677 transform.matrix[1] = 0.0;
5678 transform.matrix[2] = 0.0;
5679 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5680 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5681 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5683 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5685 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5686 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5689 return Ok;
5692 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5694 GpRectF dstrectf, srcrectf;
5696 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5698 if (!dstrect || !srcrect)
5699 return InvalidParameter;
5701 dstrectf.X = dstrect->X;
5702 dstrectf.Y = dstrect->Y;
5703 dstrectf.Width = dstrect->Width;
5704 dstrectf.Height = dstrect->Height;
5706 srcrectf.X = srcrect->X;
5707 srcrectf.Y = srcrect->Y;
5708 srcrectf.Width = srcrect->Width;
5709 srcrectf.Height = srcrect->Height;
5711 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5714 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5716 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5717 return NotImplemented;
5720 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5721 GraphicsContainer state)
5723 GpStatus sts;
5724 GraphicsContainerItem *container, *container2;
5726 if(!graphics)
5727 return InvalidParameter;
5729 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5730 if(container->contid == state && container->type == type)
5731 break;
5734 /* did not find a matching container */
5735 if(&container->entry == &graphics->containers)
5736 return Ok;
5738 sts = restore_container(graphics, container);
5739 if(sts != Ok)
5740 return sts;
5742 /* remove all of the containers on top of the found container */
5743 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5744 if(container->contid == state)
5745 break;
5746 list_remove(&container->entry);
5747 delete_container(container);
5750 list_remove(&container->entry);
5751 delete_container(container);
5753 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5754 if (type == BEGIN_CONTAINER)
5755 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
5756 else
5757 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
5760 return Ok;
5763 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5765 TRACE("(%p, %x)\n", graphics, state);
5766 return end_container(graphics, BEGIN_CONTAINER, state);
5769 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5771 TRACE("(%p, %x)\n", graphics, state);
5772 return end_container(graphics, SAVE_GRAPHICS, state);
5775 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5776 REAL sy, GpMatrixOrder order)
5778 GpStatus stat;
5780 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5782 if(!graphics)
5783 return InvalidParameter;
5785 if(graphics->busy)
5786 return ObjectBusy;
5788 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5789 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
5791 if (stat != Ok)
5792 return stat;
5795 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5798 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5799 CombineMode mode)
5801 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5803 if(!graphics || !srcgraphics)
5804 return InvalidParameter;
5806 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5809 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5810 CompositingMode mode)
5812 TRACE("(%p, %d)\n", graphics, mode);
5814 if(!graphics)
5815 return InvalidParameter;
5817 if(graphics->busy)
5818 return ObjectBusy;
5820 if(graphics->compmode == mode)
5821 return Ok;
5823 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5825 GpStatus stat;
5827 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5828 EmfPlusRecordTypeSetCompositingMode, mode);
5829 if(stat != Ok)
5830 return stat;
5833 graphics->compmode = mode;
5835 return Ok;
5838 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5839 CompositingQuality quality)
5841 TRACE("(%p, %d)\n", graphics, quality);
5843 if(!graphics)
5844 return InvalidParameter;
5846 if(graphics->busy)
5847 return ObjectBusy;
5849 if(graphics->compqual == quality)
5850 return Ok;
5852 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5854 GpStatus stat;
5856 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5857 EmfPlusRecordTypeSetCompositingQuality, quality);
5858 if(stat != Ok)
5859 return stat;
5862 graphics->compqual = quality;
5864 return Ok;
5867 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5868 InterpolationMode mode)
5870 TRACE("(%p, %d)\n", graphics, mode);
5872 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5873 return InvalidParameter;
5875 if(graphics->busy)
5876 return ObjectBusy;
5878 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5879 mode = InterpolationModeBilinear;
5881 if (mode == InterpolationModeHighQuality)
5882 mode = InterpolationModeHighQualityBicubic;
5884 if (mode == graphics->interpolation)
5885 return Ok;
5887 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5889 GpStatus stat;
5891 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5892 EmfPlusRecordTypeSetInterpolationMode, mode);
5893 if (stat != Ok)
5894 return stat;
5897 graphics->interpolation = mode;
5899 return Ok;
5902 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5904 GpStatus stat;
5906 TRACE("(%p, %.2f)\n", graphics, scale);
5908 if(!graphics || (scale <= 0.0))
5909 return InvalidParameter;
5911 if(graphics->busy)
5912 return ObjectBusy;
5914 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5916 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5917 if (stat != Ok)
5918 return stat;
5921 graphics->scale = scale;
5923 return Ok;
5926 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5928 GpStatus stat;
5930 TRACE("(%p, %d)\n", graphics, unit);
5932 if(!graphics)
5933 return InvalidParameter;
5935 if(graphics->busy)
5936 return ObjectBusy;
5938 if(unit == UnitWorld)
5939 return InvalidParameter;
5941 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5943 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5944 if (stat != Ok)
5945 return stat;
5948 graphics->unit = unit;
5950 return Ok;
5953 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5954 mode)
5956 TRACE("(%p, %d)\n", graphics, mode);
5958 if(!graphics)
5959 return InvalidParameter;
5961 if(graphics->busy)
5962 return ObjectBusy;
5964 if(graphics->pixeloffset == mode)
5965 return Ok;
5967 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5969 GpStatus stat;
5971 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5972 EmfPlusRecordTypeSetPixelOffsetMode, mode);
5973 if(stat != Ok)
5974 return stat;
5977 graphics->pixeloffset = mode;
5979 return Ok;
5982 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5984 static int calls;
5986 TRACE("(%p,%i,%i)\n", graphics, x, y);
5988 if (!(calls++))
5989 FIXME("value is unused in rendering\n");
5991 if (!graphics)
5992 return InvalidParameter;
5994 graphics->origin_x = x;
5995 graphics->origin_y = y;
5997 return Ok;
6000 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6002 TRACE("(%p,%p,%p)\n", graphics, x, y);
6004 if (!graphics || !x || !y)
6005 return InvalidParameter;
6007 *x = graphics->origin_x;
6008 *y = graphics->origin_y;
6010 return Ok;
6013 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6015 TRACE("(%p, %d)\n", graphics, mode);
6017 if(!graphics)
6018 return InvalidParameter;
6020 if(graphics->busy)
6021 return ObjectBusy;
6023 if(graphics->smoothing == mode)
6024 return Ok;
6026 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6027 GpStatus stat;
6028 BOOL antialias = (mode != SmoothingModeDefault &&
6029 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6031 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6032 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6033 if(stat != Ok)
6034 return stat;
6037 graphics->smoothing = mode;
6039 return Ok;
6042 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6044 TRACE("(%p, %d)\n", graphics, contrast);
6046 if(!graphics)
6047 return InvalidParameter;
6049 graphics->textcontrast = contrast;
6051 return Ok;
6054 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6055 TextRenderingHint hint)
6057 TRACE("(%p, %d)\n", graphics, hint);
6059 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6060 return InvalidParameter;
6062 if(graphics->busy)
6063 return ObjectBusy;
6065 if(graphics->texthint == hint)
6066 return Ok;
6068 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6069 GpStatus stat;
6071 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6072 EmfPlusRecordTypeSetTextRenderingHint, hint);
6073 if(stat != Ok)
6074 return stat;
6077 graphics->texthint = hint;
6079 return Ok;
6082 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6084 GpStatus stat;
6086 TRACE("(%p, %p)\n", graphics, matrix);
6088 if(!graphics || !matrix)
6089 return InvalidParameter;
6091 if(graphics->busy)
6092 return ObjectBusy;
6094 TRACE("%f,%f,%f,%f,%f,%f\n",
6095 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6096 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6098 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6099 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6101 if (stat != Ok)
6102 return stat;
6105 graphics->worldtrans = *matrix;
6107 return Ok;
6110 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6111 REAL dy, GpMatrixOrder order)
6113 GpStatus stat;
6115 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6117 if(!graphics)
6118 return InvalidParameter;
6120 if(graphics->busy)
6121 return ObjectBusy;
6123 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6124 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6126 if (stat != Ok)
6127 return stat;
6130 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6133 /*****************************************************************************
6134 * GdipSetClipHrgn [GDIPLUS.@]
6136 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6138 GpRegion *region;
6139 GpStatus status;
6141 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6143 if(!graphics)
6144 return InvalidParameter;
6146 if(graphics->busy)
6147 return ObjectBusy;
6149 /* hrgn is already in device units */
6150 status = GdipCreateRegionHrgn(hrgn, &region);
6151 if(status != Ok)
6152 return status;
6154 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6156 GdipDeleteRegion(region);
6157 return status;
6160 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6162 GpStatus status;
6163 GpPath *clip_path;
6165 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6167 if(!graphics)
6168 return InvalidParameter;
6170 if(graphics->busy)
6171 return ObjectBusy;
6173 status = GdipClonePath(path, &clip_path);
6174 if (status == Ok)
6176 GpMatrix world_to_device;
6178 get_graphics_transform(graphics, CoordinateSpaceDevice,
6179 CoordinateSpaceWorld, &world_to_device);
6180 status = GdipTransformPath(clip_path, &world_to_device);
6181 if (status == Ok)
6182 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6184 GdipDeletePath(clip_path);
6186 return status;
6189 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6190 REAL width, REAL height,
6191 CombineMode mode)
6193 GpStatus status;
6194 GpRectF rect;
6195 GpRegion *region;
6197 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6199 if(!graphics)
6200 return InvalidParameter;
6202 if(graphics->busy)
6203 return ObjectBusy;
6205 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6207 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6208 if (status != Ok)
6209 return status;
6212 rect.X = x;
6213 rect.Y = y;
6214 rect.Width = width;
6215 rect.Height = height;
6216 status = GdipCreateRegionRect(&rect, &region);
6217 if (status == Ok)
6219 GpMatrix world_to_device;
6221 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6222 status = GdipTransformRegion(region, &world_to_device);
6223 if (status == Ok)
6224 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6226 GdipDeleteRegion(region);
6228 return status;
6231 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6232 INT width, INT height,
6233 CombineMode mode)
6235 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6237 if(!graphics)
6238 return InvalidParameter;
6240 if(graphics->busy)
6241 return ObjectBusy;
6243 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6246 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6247 CombineMode mode)
6249 GpStatus status;
6250 GpRegion *clip;
6252 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6254 if(!graphics || !region)
6255 return InvalidParameter;
6257 if(graphics->busy)
6258 return ObjectBusy;
6260 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6262 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6263 if (status != Ok)
6264 return status;
6267 status = GdipCloneRegion(region, &clip);
6268 if (status == Ok)
6270 GpMatrix world_to_device;
6272 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6273 status = GdipTransformRegion(clip, &world_to_device);
6274 if (status == Ok)
6275 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6277 GdipDeleteRegion(clip);
6279 return status;
6282 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6283 INT count)
6285 GpStatus status;
6286 GpPath* path;
6288 TRACE("(%p, %p, %d)\n", graphics, points, count);
6290 if(!graphics || !pen || count<=0)
6291 return InvalidParameter;
6293 if(graphics->busy)
6294 return ObjectBusy;
6296 status = GdipCreatePath(FillModeAlternate, &path);
6297 if (status != Ok) return status;
6299 status = GdipAddPathPolygon(path, points, count);
6300 if (status == Ok)
6301 status = GdipDrawPath(graphics, pen, path);
6303 GdipDeletePath(path);
6305 return status;
6308 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6309 INT count)
6311 GpStatus ret;
6312 GpPointF *ptf;
6313 INT i;
6315 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6317 if(count<=0) return InvalidParameter;
6318 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6320 for(i = 0;i < count; i++){
6321 ptf[i].X = (REAL)points[i].X;
6322 ptf[i].Y = (REAL)points[i].Y;
6325 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6326 heap_free(ptf);
6328 return ret;
6331 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6333 TRACE("(%p, %p)\n", graphics, dpi);
6335 if(!graphics || !dpi)
6336 return InvalidParameter;
6338 if(graphics->busy)
6339 return ObjectBusy;
6341 *dpi = graphics->xres;
6342 return Ok;
6345 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6347 TRACE("(%p, %p)\n", graphics, dpi);
6349 if(!graphics || !dpi)
6350 return InvalidParameter;
6352 if(graphics->busy)
6353 return ObjectBusy;
6355 *dpi = graphics->yres;
6356 return Ok;
6359 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6360 GpMatrixOrder order)
6362 GpMatrix m;
6363 GpStatus ret;
6365 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6367 if(!graphics || !matrix)
6368 return InvalidParameter;
6370 if(graphics->busy)
6371 return ObjectBusy;
6373 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6374 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6376 if (ret != Ok)
6377 return ret;
6380 m = graphics->worldtrans;
6382 ret = GdipMultiplyMatrix(&m, matrix, order);
6383 if(ret == Ok)
6384 graphics->worldtrans = m;
6386 return ret;
6389 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6390 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6392 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6394 GpStatus stat=Ok;
6396 TRACE("(%p, %p)\n", graphics, hdc);
6398 if(!graphics || !hdc)
6399 return InvalidParameter;
6401 if(graphics->busy)
6402 return ObjectBusy;
6404 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6406 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6408 else if (!graphics->hdc ||
6409 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6411 /* Create a fake HDC and fill it with a constant color. */
6412 HDC temp_hdc;
6413 HBITMAP hbitmap;
6414 GpRectF bounds;
6415 BITMAPINFOHEADER bmih;
6416 int i;
6418 stat = get_graphics_bounds(graphics, &bounds);
6419 if (stat != Ok)
6420 return stat;
6422 graphics->temp_hbitmap_width = bounds.Width;
6423 graphics->temp_hbitmap_height = bounds.Height;
6425 bmih.biSize = sizeof(bmih);
6426 bmih.biWidth = graphics->temp_hbitmap_width;
6427 bmih.biHeight = -graphics->temp_hbitmap_height;
6428 bmih.biPlanes = 1;
6429 bmih.biBitCount = 32;
6430 bmih.biCompression = BI_RGB;
6431 bmih.biSizeImage = 0;
6432 bmih.biXPelsPerMeter = 0;
6433 bmih.biYPelsPerMeter = 0;
6434 bmih.biClrUsed = 0;
6435 bmih.biClrImportant = 0;
6437 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6438 (void**)&graphics->temp_bits, NULL, 0);
6439 if (!hbitmap)
6440 return GenericError;
6442 temp_hdc = CreateCompatibleDC(0);
6443 if (!temp_hdc)
6445 DeleteObject(hbitmap);
6446 return GenericError;
6449 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6450 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6452 SelectObject(temp_hdc, hbitmap);
6454 graphics->temp_hbitmap = hbitmap;
6455 *hdc = graphics->temp_hdc = temp_hdc;
6457 else
6459 *hdc = graphics->hdc;
6462 if (stat == Ok)
6463 graphics->busy = TRUE;
6465 return stat;
6468 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6470 GpStatus stat=Ok;
6472 TRACE("(%p, %p)\n", graphics, hdc);
6474 if(!graphics || !hdc || !graphics->busy)
6475 return InvalidParameter;
6477 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6479 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6481 else if (graphics->temp_hdc == hdc)
6483 DWORD* pos;
6484 int i;
6486 /* Find the pixels that have changed, and mark them as opaque. */
6487 pos = (DWORD*)graphics->temp_bits;
6488 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6490 if (*pos != DC_BACKGROUND_KEY)
6492 *pos |= 0xff000000;
6494 pos++;
6497 /* Write the changed pixels to the real target. */
6498 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6499 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6500 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6502 /* Clean up. */
6503 DeleteDC(graphics->temp_hdc);
6504 DeleteObject(graphics->temp_hbitmap);
6505 graphics->temp_hdc = NULL;
6506 graphics->temp_hbitmap = NULL;
6508 else if (hdc != graphics->hdc)
6510 stat = InvalidParameter;
6513 if (stat == Ok)
6514 graphics->busy = FALSE;
6516 return stat;
6519 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6521 GpRegion *clip;
6522 GpStatus status;
6523 GpMatrix device_to_world;
6525 TRACE("(%p, %p)\n", graphics, region);
6527 if(!graphics || !region)
6528 return InvalidParameter;
6530 if(graphics->busy)
6531 return ObjectBusy;
6533 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6534 return status;
6536 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6537 status = GdipTransformRegion(clip, &device_to_world);
6538 if (status != Ok)
6540 GdipDeleteRegion(clip);
6541 return status;
6544 /* free everything except root node and header */
6545 delete_element(&region->node);
6546 memcpy(region, clip, sizeof(GpRegion));
6547 heap_free(clip);
6549 return Ok;
6552 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6553 GpCoordinateSpace src_space, GpMatrix *matrix)
6555 GpStatus stat = Ok;
6556 REAL scale_x, scale_y;
6558 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6560 if (dst_space != src_space)
6562 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6563 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6565 if(graphics->unit != UnitDisplay)
6567 scale_x *= graphics->scale;
6568 scale_y *= graphics->scale;
6571 /* transform from src_space to CoordinateSpacePage */
6572 switch (src_space)
6574 case CoordinateSpaceWorld:
6575 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6576 break;
6577 case CoordinateSpacePage:
6578 break;
6579 case CoordinateSpaceDevice:
6580 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6581 break;
6584 /* transform from CoordinateSpacePage to dst_space */
6585 switch (dst_space)
6587 case CoordinateSpaceWorld:
6589 GpMatrix inverted_transform = graphics->worldtrans;
6590 stat = GdipInvertMatrix(&inverted_transform);
6591 if (stat == Ok)
6592 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6593 break;
6595 case CoordinateSpacePage:
6596 break;
6597 case CoordinateSpaceDevice:
6598 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6599 break;
6602 return stat;
6605 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6606 GpCoordinateSpace src_space, GpPointF *points, INT count)
6608 GpMatrix matrix;
6609 GpStatus stat;
6611 if(!graphics || !points || count <= 0)
6612 return InvalidParameter;
6614 if(graphics->busy)
6615 return ObjectBusy;
6617 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6619 if (src_space == dst_space) return Ok;
6621 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6622 if (stat != Ok) return stat;
6624 return GdipTransformMatrixPoints(&matrix, points, count);
6627 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6628 GpCoordinateSpace src_space, GpPoint *points, INT count)
6630 GpPointF *pointsF;
6631 GpStatus ret;
6632 INT i;
6634 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6636 if(count <= 0)
6637 return InvalidParameter;
6639 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6640 if(!pointsF)
6641 return OutOfMemory;
6643 for(i = 0; i < count; i++){
6644 pointsF[i].X = (REAL)points[i].X;
6645 pointsF[i].Y = (REAL)points[i].Y;
6648 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6650 if(ret == Ok)
6651 for(i = 0; i < count; i++){
6652 points[i].X = gdip_round(pointsF[i].X);
6653 points[i].Y = gdip_round(pointsF[i].Y);
6655 heap_free(pointsF);
6657 return ret;
6660 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6662 static int calls;
6664 TRACE("\n");
6666 if (!calls++)
6667 FIXME("stub\n");
6669 return NULL;
6672 /*****************************************************************************
6673 * GdipTranslateClip [GDIPLUS.@]
6675 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6677 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6679 if(!graphics)
6680 return InvalidParameter;
6682 if(graphics->busy)
6683 return ObjectBusy;
6685 return GdipTranslateRegion(graphics->clip, dx, dy);
6688 /*****************************************************************************
6689 * GdipTranslateClipI [GDIPLUS.@]
6691 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6693 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6695 if(!graphics)
6696 return InvalidParameter;
6698 if(graphics->busy)
6699 return ObjectBusy;
6701 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6705 /*****************************************************************************
6706 * GdipMeasureDriverString [GDIPLUS.@]
6708 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6709 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6710 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6712 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6713 HFONT hfont;
6714 HDC hdc;
6715 REAL min_x, min_y, max_x, max_y, x, y;
6716 int i;
6717 TEXTMETRICW textmetric;
6718 const WORD *glyph_indices;
6719 WORD *dynamic_glyph_indices=NULL;
6720 REAL rel_width, rel_height, ascent, descent;
6721 GpPointF pt[3];
6723 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6725 if (!graphics || !text || !font || !positions || !boundingBox)
6726 return InvalidParameter;
6728 if (length == -1)
6729 length = strlenW(text);
6731 if (length == 0)
6733 boundingBox->X = 0.0;
6734 boundingBox->Y = 0.0;
6735 boundingBox->Width = 0.0;
6736 boundingBox->Height = 0.0;
6739 if (flags & unsupported_flags)
6740 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6742 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6744 hdc = CreateCompatibleDC(0);
6745 SelectObject(hdc, hfont);
6747 GetTextMetricsW(hdc, &textmetric);
6749 pt[0].X = 0.0;
6750 pt[0].Y = 0.0;
6751 pt[1].X = 1.0;
6752 pt[1].Y = 0.0;
6753 pt[2].X = 0.0;
6754 pt[2].Y = 1.0;
6755 if (matrix)
6757 GpMatrix xform = *matrix;
6758 GdipTransformMatrixPoints(&xform, pt, 3);
6760 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6761 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6762 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6763 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6764 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6766 if (flags & DriverStringOptionsCmapLookup)
6768 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
6769 if (!glyph_indices)
6771 DeleteDC(hdc);
6772 DeleteObject(hfont);
6773 return OutOfMemory;
6776 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6778 else
6779 glyph_indices = text;
6781 min_x = max_x = x = positions[0].X;
6782 min_y = max_y = y = positions[0].Y;
6784 ascent = textmetric.tmAscent / rel_height;
6785 descent = textmetric.tmDescent / rel_height;
6787 for (i=0; i<length; i++)
6789 int char_width;
6790 ABC abc;
6792 if (!(flags & DriverStringOptionsRealizedAdvance))
6794 x = positions[i].X;
6795 y = positions[i].Y;
6798 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6799 char_width = abc.abcA + abc.abcB + abc.abcC;
6801 if (min_y > y - ascent) min_y = y - ascent;
6802 if (max_y < y + descent) max_y = y + descent;
6803 if (min_x > x) min_x = x;
6805 x += char_width / rel_width;
6807 if (max_x < x) max_x = x;
6810 heap_free(dynamic_glyph_indices);
6811 DeleteDC(hdc);
6812 DeleteObject(hfont);
6814 boundingBox->X = min_x;
6815 boundingBox->Y = min_y;
6816 boundingBox->Width = max_x - min_x;
6817 boundingBox->Height = max_y - min_y;
6819 return Ok;
6822 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6823 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6824 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6825 INT flags, GDIPCONST GpMatrix *matrix)
6827 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6828 INT save_state;
6829 GpPointF pt;
6830 HFONT hfont;
6831 UINT eto_flags=0;
6832 GpStatus status;
6833 HRGN hrgn;
6835 if (flags & unsupported_flags)
6836 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6838 if (!(flags & DriverStringOptionsCmapLookup))
6839 eto_flags |= ETO_GLYPH_INDEX;
6841 save_state = SaveDC(graphics->hdc);
6842 SetBkMode(graphics->hdc, TRANSPARENT);
6843 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6845 status = get_clip_hrgn(graphics, &hrgn);
6847 if (status == Ok && hrgn)
6849 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
6850 DeleteObject(hrgn);
6853 pt = positions[0];
6854 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6856 get_font_hfont(graphics, font, format, &hfont, matrix);
6857 SelectObject(graphics->hdc, hfont);
6859 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6861 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6863 RestoreDC(graphics->hdc, save_state);
6865 DeleteObject(hfont);
6867 return Ok;
6870 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6871 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6872 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6873 INT flags, GDIPCONST GpMatrix *matrix)
6875 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6876 GpStatus stat;
6877 PointF *real_positions, real_position;
6878 POINT *pti;
6879 HFONT hfont;
6880 HDC hdc;
6881 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6882 DWORD max_glyphsize=0;
6883 GLYPHMETRICS glyphmetrics;
6884 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6885 BYTE *glyph_mask;
6886 BYTE *text_mask;
6887 int text_mask_stride;
6888 BYTE *pixel_data;
6889 int pixel_data_stride;
6890 GpRect pixel_area;
6891 UINT ggo_flags = GGO_GRAY8_BITMAP;
6893 if (length <= 0)
6894 return Ok;
6896 if (!(flags & DriverStringOptionsCmapLookup))
6897 ggo_flags |= GGO_GLYPH_INDEX;
6899 if (flags & unsupported_flags)
6900 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6902 pti = heap_alloc_zero(sizeof(POINT) * length);
6903 if (!pti)
6904 return OutOfMemory;
6906 if (flags & DriverStringOptionsRealizedAdvance)
6908 real_position = positions[0];
6910 transform_and_round_points(graphics, pti, &real_position, 1);
6912 else
6914 real_positions = heap_alloc_zero(sizeof(PointF) * length);
6915 if (!real_positions)
6917 heap_free(pti);
6918 return OutOfMemory;
6921 memcpy(real_positions, positions, sizeof(PointF) * length);
6923 transform_and_round_points(graphics, pti, real_positions, length);
6925 heap_free(real_positions);
6928 get_font_hfont(graphics, font, format, &hfont, matrix);
6930 hdc = CreateCompatibleDC(0);
6931 SelectObject(hdc, hfont);
6933 /* Get the boundaries of the text to be drawn */
6934 for (i=0; i<length; i++)
6936 DWORD glyphsize;
6937 int left, top, right, bottom;
6939 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6940 &glyphmetrics, 0, NULL, &identity);
6942 if (glyphsize == GDI_ERROR)
6944 ERR("GetGlyphOutlineW failed\n");
6945 heap_free(pti);
6946 DeleteDC(hdc);
6947 DeleteObject(hfont);
6948 return GenericError;
6951 if (glyphsize > max_glyphsize)
6952 max_glyphsize = glyphsize;
6954 if (glyphsize != 0)
6956 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6957 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6958 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6959 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6961 if (left < min_x) min_x = left;
6962 if (top < min_y) min_y = top;
6963 if (right > max_x) max_x = right;
6964 if (bottom > max_y) max_y = bottom;
6967 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6969 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6970 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6974 if (max_glyphsize == 0)
6975 /* Nothing to draw. */
6976 return Ok;
6978 glyph_mask = heap_alloc_zero(max_glyphsize);
6979 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
6980 text_mask_stride = max_x - min_x;
6982 if (!(glyph_mask && text_mask))
6984 heap_free(glyph_mask);
6985 heap_free(text_mask);
6986 heap_free(pti);
6987 DeleteDC(hdc);
6988 DeleteObject(hfont);
6989 return OutOfMemory;
6992 /* Generate a mask for the text */
6993 for (i=0; i<length; i++)
6995 DWORD ret;
6996 int left, top, stride;
6998 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6999 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7001 if (ret == GDI_ERROR || ret == 0)
7002 continue; /* empty glyph */
7004 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7005 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7006 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7008 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7010 BYTE *glyph_val = glyph_mask + y * stride;
7011 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7012 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7014 *text_val = min(64, *text_val + *glyph_val);
7015 glyph_val++;
7016 text_val++;
7021 heap_free(pti);
7022 DeleteDC(hdc);
7023 DeleteObject(hfont);
7024 heap_free(glyph_mask);
7026 /* get the brush data */
7027 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7028 if (!pixel_data)
7030 heap_free(text_mask);
7031 return OutOfMemory;
7034 pixel_area.X = min_x;
7035 pixel_area.Y = min_y;
7036 pixel_area.Width = max_x - min_x;
7037 pixel_area.Height = max_y - min_y;
7038 pixel_data_stride = pixel_area.Width * 4;
7040 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7041 if (stat != Ok)
7043 heap_free(text_mask);
7044 heap_free(pixel_data);
7045 return stat;
7048 /* multiply the brush data by the mask */
7049 for (y=0; y<pixel_area.Height; y++)
7051 BYTE *text_val = text_mask + text_mask_stride * y;
7052 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7053 for (x=0; x<pixel_area.Width; x++)
7055 *pixel_val = (*pixel_val) * (*text_val) / 64;
7056 text_val++;
7057 pixel_val+=4;
7061 heap_free(text_mask);
7063 /* draw the result */
7064 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7065 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7067 heap_free(pixel_data);
7069 return stat;
7072 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7073 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7074 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7075 INT flags, GDIPCONST GpMatrix *matrix)
7077 GpStatus stat = NotImplemented;
7079 if (length == -1)
7080 length = strlenW(text);
7082 if (graphics->hdc && !graphics->alpha_hdc &&
7083 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
7084 brush->bt == BrushTypeSolidColor &&
7085 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7086 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7087 brush, positions, flags, matrix);
7088 if (stat == NotImplemented)
7089 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7090 brush, positions, flags, matrix);
7091 return stat;
7094 /*****************************************************************************
7095 * GdipDrawDriverString [GDIPLUS.@]
7097 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7098 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7099 GDIPCONST PointF *positions, INT flags,
7100 GDIPCONST GpMatrix *matrix )
7102 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7104 if (!graphics || !text || !font || !brush || !positions)
7105 return InvalidParameter;
7107 return draw_driver_string(graphics, text, length, font, NULL,
7108 brush, positions, flags, matrix);
7111 /*****************************************************************************
7112 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7114 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7116 GpStatus stat;
7117 GpRegion* rgn;
7119 TRACE("(%p, %p)\n", graphics, res);
7121 if((stat = GdipCreateRegion(&rgn)) != Ok)
7122 return stat;
7124 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7125 goto cleanup;
7127 stat = GdipIsEmptyRegion(rgn, graphics, res);
7129 cleanup:
7130 GdipDeleteRegion(rgn);
7131 return stat;
7134 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7136 static int calls;
7138 TRACE("(%p) stub\n", graphics);
7140 if(!(calls++))
7141 FIXME("not implemented\n");
7143 return NotImplemented;
7146 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7148 TRACE("(%p, %p)\n", graphics, pabort);
7150 if (!graphics)
7151 return InvalidParameter;
7153 if (pabort)
7154 FIXME("Abort callback is not supported.\n");
7156 return Ok;