gdiplus: Implement ResetWorldTransform metafile playback/recording.
[wine.git] / dlls / gdiplus / graphics.c
blob6003a198775f874c1cd71747c396304588cc4f99
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 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
55 GpCoordinateSpace src_space, GpMatrix *matrix);
57 /* Converts from gdiplus path point type to gdi path point type. */
58 static BYTE convert_path_point_type(BYTE type)
60 BYTE ret;
62 switch(type & PathPointTypePathTypeMask){
63 case PathPointTypeBezier:
64 ret = PT_BEZIERTO;
65 break;
66 case PathPointTypeLine:
67 ret = PT_LINETO;
68 break;
69 case PathPointTypeStart:
70 ret = PT_MOVETO;
71 break;
72 default:
73 ERR("Bad point type\n");
74 return 0;
77 if(type & PathPointTypeCloseSubpath)
78 ret |= PT_CLOSEFIGURE;
80 return ret;
83 static COLORREF get_gdi_brush_color(const GpBrush *brush)
85 ARGB argb;
87 switch (brush->bt)
89 case BrushTypeSolidColor:
91 const GpSolidFill *sf = (const GpSolidFill *)brush;
92 argb = sf->color;
93 break;
95 case BrushTypeHatchFill:
97 const GpHatch *hatch = (const GpHatch *)brush;
98 argb = hatch->forecol;
99 break;
101 case BrushTypeLinearGradient:
103 const GpLineGradient *line = (const GpLineGradient *)brush;
104 argb = line->startcolor;
105 break;
107 case BrushTypePathGradient:
109 const GpPathGradient *grad = (const GpPathGradient *)brush;
110 argb = grad->centercolor;
111 break;
113 default:
114 FIXME("unhandled brush type %d\n", brush->bt);
115 argb = 0;
116 break;
118 return ARGB2COLORREF(argb);
121 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
123 HBITMAP hbmp;
124 BITMAPINFOHEADER bmih;
125 DWORD *bits;
126 int x, y;
128 bmih.biSize = sizeof(bmih);
129 bmih.biWidth = 8;
130 bmih.biHeight = 8;
131 bmih.biPlanes = 1;
132 bmih.biBitCount = 32;
133 bmih.biCompression = BI_RGB;
134 bmih.biSizeImage = 0;
136 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
137 if (hbmp)
139 const char *hatch_data;
141 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
143 for (y = 0; y < 8; y++)
145 for (x = 0; x < 8; x++)
147 if (hatch_data[y] & (0x80 >> x))
148 bits[y * 8 + x] = hatch->forecol;
149 else
150 bits[y * 8 + x] = hatch->backcol;
154 else
156 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
158 for (y = 0; y < 64; y++)
159 bits[y] = hatch->forecol;
163 return hbmp;
166 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
168 switch (brush->bt)
170 case BrushTypeSolidColor:
172 const GpSolidFill *sf = (const GpSolidFill *)brush;
173 lb->lbStyle = BS_SOLID;
174 lb->lbColor = ARGB2COLORREF(sf->color);
175 lb->lbHatch = 0;
176 return Ok;
179 case BrushTypeHatchFill:
181 const GpHatch *hatch = (const GpHatch *)brush;
182 HBITMAP hbmp;
184 hbmp = create_hatch_bitmap(hatch);
185 if (!hbmp) return OutOfMemory;
187 lb->lbStyle = BS_PATTERN;
188 lb->lbColor = 0;
189 lb->lbHatch = (ULONG_PTR)hbmp;
190 return Ok;
193 default:
194 FIXME("unhandled brush type %d\n", brush->bt);
195 lb->lbStyle = BS_SOLID;
196 lb->lbColor = get_gdi_brush_color(brush);
197 lb->lbHatch = 0;
198 return Ok;
202 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
204 switch (lb->lbStyle)
206 case BS_PATTERN:
207 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
208 break;
210 return Ok;
213 static HBRUSH create_gdi_brush(const GpBrush *brush)
215 LOGBRUSH lb;
216 HBRUSH gdibrush;
218 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
220 gdibrush = CreateBrushIndirect(&lb);
221 free_gdi_logbrush(&lb);
223 return gdibrush;
226 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
228 LOGBRUSH lb;
229 HPEN gdipen;
230 REAL width;
231 INT save_state, i, numdashes;
232 GpPointF pt[2];
233 DWORD dash_array[MAX_DASHLEN];
235 save_state = SaveDC(graphics->hdc);
237 EndPath(graphics->hdc);
239 if(pen->unit == UnitPixel){
240 width = pen->width;
242 else{
243 /* Get an estimate for the amount the pen width is affected by the world
244 * transform. (This is similar to what some of the wine drivers do.) */
245 pt[0].X = 0.0;
246 pt[0].Y = 0.0;
247 pt[1].X = 1.0;
248 pt[1].Y = 1.0;
249 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
250 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
251 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
253 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
254 width *= graphics->scale;
257 if(pen->dash == DashStyleCustom){
258 numdashes = min(pen->numdashes, MAX_DASHLEN);
260 TRACE("dashes are: ");
261 for(i = 0; i < numdashes; i++){
262 dash_array[i] = gdip_round(width * pen->dashes[i]);
263 TRACE("%d, ", dash_array[i]);
265 TRACE("\n and the pen style is %x\n", pen->style);
267 create_gdi_logbrush(pen->brush, &lb);
268 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
269 numdashes, dash_array);
270 free_gdi_logbrush(&lb);
272 else
274 create_gdi_logbrush(pen->brush, &lb);
275 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
276 free_gdi_logbrush(&lb);
279 SelectObject(graphics->hdc, gdipen);
281 return save_state;
284 static void restore_dc(GpGraphics *graphics, INT state)
286 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
287 RestoreDC(graphics->hdc, state);
290 /* This helper applies all the changes that the points listed in ptf need in
291 * order to be drawn on the device context. In the end, this should include at
292 * least:
293 * -scaling by page unit
294 * -applying world transformation
295 * -converting from float to int
296 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
297 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
298 * gdi to draw, and these functions would irreparably mess with line widths.
300 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
301 GpPointF *ptf, INT count)
303 REAL scale_x, scale_y;
304 GpMatrix matrix;
305 int i;
307 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
308 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
310 /* apply page scale */
311 if(graphics->unit != UnitDisplay)
313 scale_x *= graphics->scale;
314 scale_y *= graphics->scale;
317 matrix = graphics->worldtrans;
318 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
319 GdipTransformMatrixPoints(&matrix, ptf, count);
321 for(i = 0; i < count; i++){
322 pti[i].x = gdip_round(ptf[i].X);
323 pti[i].y = gdip_round(ptf[i].Y);
327 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
328 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
330 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
332 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
334 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
335 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
337 else
339 BLENDFUNCTION bf;
341 bf.BlendOp = AC_SRC_OVER;
342 bf.BlendFlags = 0;
343 bf.SourceConstantAlpha = 255;
344 bf.AlphaFormat = AC_SRC_ALPHA;
346 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
347 hdc, src_x, src_y, src_width, src_height, bf);
351 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
353 /* clipping region is in device coords */
354 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
357 /* Draw ARGB data to the given graphics object */
358 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
359 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
361 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
362 INT x, y;
364 for (y=0; y<src_height; y++)
366 for (x=0; x<src_width; x++)
368 ARGB dst_color, src_color;
369 src_color = ((ARGB*)(src + src_stride * y))[x];
371 if (!(src_color & 0xff000000))
372 continue;
374 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
375 if (fmt & PixelFormatPAlpha)
376 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
377 else
378 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
382 return Ok;
385 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
386 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
388 HDC hdc;
389 HBITMAP hbitmap;
390 BITMAPINFOHEADER bih;
391 BYTE *temp_bits;
393 hdc = CreateCompatibleDC(0);
395 bih.biSize = sizeof(BITMAPINFOHEADER);
396 bih.biWidth = src_width;
397 bih.biHeight = -src_height;
398 bih.biPlanes = 1;
399 bih.biBitCount = 32;
400 bih.biCompression = BI_RGB;
401 bih.biSizeImage = 0;
402 bih.biXPelsPerMeter = 0;
403 bih.biYPelsPerMeter = 0;
404 bih.biClrUsed = 0;
405 bih.biClrImportant = 0;
407 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
408 (void**)&temp_bits, NULL, 0);
410 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE ||
411 fmt & PixelFormatPAlpha)
412 memcpy(temp_bits, src, src_width * src_height * 4);
413 else
414 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
415 4 * src_width, src, src_stride);
417 SelectObject(hdc, hbitmap);
418 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
419 hdc, 0, 0, src_width, src_height);
420 DeleteDC(hdc);
421 DeleteObject(hbitmap);
423 return Ok;
426 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
427 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
429 GpStatus stat=Ok;
431 if (graphics->image && graphics->image->type == ImageTypeBitmap)
433 DWORD i;
434 int size;
435 RGNDATA *rgndata;
436 RECT *rects;
437 HRGN hrgn, visible_rgn;
439 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
440 if (!hrgn)
441 return OutOfMemory;
443 stat = get_clip_hrgn(graphics, &visible_rgn);
444 if (stat != Ok)
446 DeleteObject(hrgn);
447 return stat;
450 if (visible_rgn)
452 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
453 DeleteObject(visible_rgn);
456 if (hregion)
457 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
459 size = GetRegionData(hrgn, 0, NULL);
461 rgndata = heap_alloc_zero(size);
462 if (!rgndata)
464 DeleteObject(hrgn);
465 return OutOfMemory;
468 GetRegionData(hrgn, size, rgndata);
470 rects = (RECT*)rgndata->Buffer;
472 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
474 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
475 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
476 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
477 src_stride, fmt);
480 heap_free(rgndata);
482 DeleteObject(hrgn);
484 return stat;
486 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
488 ERR("This should not be used for metafiles; fix caller\n");
489 return NotImplemented;
491 else
493 HRGN hrgn;
494 int save;
496 stat = get_clip_hrgn(graphics, &hrgn);
498 if (stat != Ok)
499 return stat;
501 save = SaveDC(graphics->hdc);
503 if (hrgn)
504 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
506 if (hregion)
507 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
509 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
510 src_height, src_stride, fmt);
512 RestoreDC(graphics->hdc, save);
514 DeleteObject(hrgn);
516 return stat;
520 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
521 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
523 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
526 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
528 INT start_a, end_a, final_a;
529 INT pos;
531 pos = gdip_round(position * 0xff);
533 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
534 end_a = ((end >> 24) & 0xff) * pos;
536 final_a = start_a + end_a;
538 if (final_a < 0xff) return 0;
540 return (final_a / 0xff) << 24 |
541 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
542 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
543 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
546 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
548 REAL blendfac;
550 /* clamp to between 0.0 and 1.0, using the wrap mode */
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 static BOOL brush_can_fill_path(GpBrush *brush)
1009 switch (brush->bt)
1011 case BrushTypeSolidColor:
1012 return TRUE;
1013 case BrushTypeHatchFill:
1015 GpHatch *hatch = (GpHatch*)brush;
1016 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1017 ((hatch->backcol & 0xff000000) == 0xff000000);
1019 case BrushTypeLinearGradient:
1020 case BrushTypeTextureFill:
1021 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1022 default:
1023 return FALSE;
1027 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1029 switch (brush->bt)
1031 case BrushTypeSolidColor:
1033 GpSolidFill *fill = (GpSolidFill*)brush;
1034 HBITMAP bmp = ARGB2BMP(fill->color);
1036 if (bmp)
1038 RECT rc;
1039 /* partially transparent fill */
1041 SelectClipPath(graphics->hdc, RGN_AND);
1042 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1044 HDC hdc = CreateCompatibleDC(NULL);
1046 if (!hdc) break;
1048 SelectObject(hdc, bmp);
1049 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1050 hdc, 0, 0, 1, 1);
1051 DeleteDC(hdc);
1054 DeleteObject(bmp);
1055 break;
1057 /* else fall through */
1059 default:
1061 HBRUSH gdibrush, old_brush;
1063 gdibrush = create_gdi_brush(brush);
1064 if (!gdibrush) return;
1066 old_brush = SelectObject(graphics->hdc, gdibrush);
1067 FillPath(graphics->hdc);
1068 SelectObject(graphics->hdc, old_brush);
1069 DeleteObject(gdibrush);
1070 break;
1075 static BOOL brush_can_fill_pixels(GpBrush *brush)
1077 switch (brush->bt)
1079 case BrushTypeSolidColor:
1080 case BrushTypeHatchFill:
1081 case BrushTypeLinearGradient:
1082 case BrushTypeTextureFill:
1083 case BrushTypePathGradient:
1084 return TRUE;
1085 default:
1086 return FALSE;
1090 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1091 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1093 switch (brush->bt)
1095 case BrushTypeSolidColor:
1097 int x, y;
1098 GpSolidFill *fill = (GpSolidFill*)brush;
1099 for (x=0; x<fill_area->Width; x++)
1100 for (y=0; y<fill_area->Height; y++)
1101 argb_pixels[x + y*cdwStride] = fill->color;
1102 return Ok;
1104 case BrushTypeHatchFill:
1106 int x, y;
1107 GpHatch *fill = (GpHatch*)brush;
1108 const char *hatch_data;
1110 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1111 return NotImplemented;
1113 for (x=0; x<fill_area->Width; x++)
1114 for (y=0; y<fill_area->Height; y++)
1116 int hx, hy;
1118 /* FIXME: Account for the rendering origin */
1119 hx = (x + fill_area->X) % 8;
1120 hy = (y + fill_area->Y) % 8;
1122 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1123 argb_pixels[x + y*cdwStride] = fill->forecol;
1124 else
1125 argb_pixels[x + y*cdwStride] = fill->backcol;
1128 return Ok;
1130 case BrushTypeLinearGradient:
1132 GpLineGradient *fill = (GpLineGradient*)brush;
1133 GpPointF draw_points[3], line_points[3];
1134 GpStatus stat;
1135 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1136 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1137 int x, y;
1139 draw_points[0].X = fill_area->X;
1140 draw_points[0].Y = fill_area->Y;
1141 draw_points[1].X = fill_area->X+1;
1142 draw_points[1].Y = fill_area->Y;
1143 draw_points[2].X = fill_area->X;
1144 draw_points[2].Y = fill_area->Y+1;
1146 /* Transform the points to a co-ordinate space where X is the point's
1147 * position in the gradient, 0.0 being the start point and 1.0 the
1148 * end point. */
1149 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1150 CoordinateSpaceDevice, draw_points, 3);
1152 if (stat == Ok)
1154 line_points[0] = fill->startpoint;
1155 line_points[1] = fill->endpoint;
1156 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1157 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1159 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1162 if (stat == Ok)
1164 stat = GdipInvertMatrix(world_to_gradient);
1166 if (stat == Ok)
1167 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1169 GdipDeleteMatrix(world_to_gradient);
1172 if (stat == Ok)
1174 REAL x_delta = draw_points[1].X - draw_points[0].X;
1175 REAL y_delta = draw_points[2].X - draw_points[0].X;
1177 for (y=0; y<fill_area->Height; y++)
1179 for (x=0; x<fill_area->Width; x++)
1181 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1183 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1188 return stat;
1190 case BrushTypeTextureFill:
1192 GpTexture *fill = (GpTexture*)brush;
1193 GpPointF draw_points[3];
1194 GpStatus stat;
1195 int x, y;
1196 GpBitmap *bitmap;
1197 int src_stride;
1198 GpRect src_area;
1200 if (fill->image->type != ImageTypeBitmap)
1202 FIXME("metafile texture brushes not implemented\n");
1203 return NotImplemented;
1206 bitmap = (GpBitmap*)fill->image;
1207 src_stride = sizeof(ARGB) * bitmap->width;
1209 src_area.X = src_area.Y = 0;
1210 src_area.Width = bitmap->width;
1211 src_area.Height = bitmap->height;
1213 draw_points[0].X = fill_area->X;
1214 draw_points[0].Y = fill_area->Y;
1215 draw_points[1].X = fill_area->X+1;
1216 draw_points[1].Y = fill_area->Y;
1217 draw_points[2].X = fill_area->X;
1218 draw_points[2].Y = fill_area->Y+1;
1220 /* Transform the points to the co-ordinate space of the bitmap. */
1221 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1222 CoordinateSpaceDevice, draw_points, 3);
1224 if (stat == Ok)
1226 GpMatrix world_to_texture = fill->transform;
1228 stat = GdipInvertMatrix(&world_to_texture);
1229 if (stat == Ok)
1230 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1233 if (stat == Ok && !fill->bitmap_bits)
1235 BitmapData lockeddata;
1237 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1238 if (!fill->bitmap_bits)
1239 stat = OutOfMemory;
1241 if (stat == Ok)
1243 lockeddata.Width = bitmap->width;
1244 lockeddata.Height = bitmap->height;
1245 lockeddata.Stride = src_stride;
1246 lockeddata.PixelFormat = PixelFormat32bppARGB;
1247 lockeddata.Scan0 = fill->bitmap_bits;
1249 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1250 PixelFormat32bppARGB, &lockeddata);
1253 if (stat == Ok)
1254 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1256 if (stat == Ok)
1257 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1258 bitmap->width, bitmap->height,
1259 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1261 if (stat != Ok)
1263 heap_free(fill->bitmap_bits);
1264 fill->bitmap_bits = NULL;
1268 if (stat == Ok)
1270 REAL x_dx = draw_points[1].X - draw_points[0].X;
1271 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1272 REAL y_dx = draw_points[2].X - draw_points[0].X;
1273 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1275 for (y=0; y<fill_area->Height; y++)
1277 for (x=0; x<fill_area->Width; x++)
1279 GpPointF point;
1280 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1281 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1283 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1284 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1285 &point, fill->imageattributes, graphics->interpolation,
1286 graphics->pixeloffset);
1291 return stat;
1293 case BrushTypePathGradient:
1295 GpPathGradient *fill = (GpPathGradient*)brush;
1296 GpPath *flat_path;
1297 GpMatrix world_to_device;
1298 GpStatus stat;
1299 int i, figure_start=0;
1300 GpPointF start_point, end_point, center_point;
1301 BYTE type;
1302 REAL min_yf, max_yf, line1_xf, line2_xf;
1303 INT min_y, max_y, min_x, max_x;
1304 INT x, y;
1305 ARGB outer_color;
1306 static BOOL transform_fixme_once;
1308 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1310 static int once;
1311 if (!once++)
1312 FIXME("path gradient focus not implemented\n");
1315 if (fill->gamma)
1317 static int once;
1318 if (!once++)
1319 FIXME("path gradient gamma correction not implemented\n");
1322 if (fill->blendcount)
1324 static int once;
1325 if (!once++)
1326 FIXME("path gradient blend not implemented\n");
1329 if (fill->pblendcount)
1331 static int once;
1332 if (!once++)
1333 FIXME("path gradient preset blend not implemented\n");
1336 if (!transform_fixme_once)
1338 BOOL is_identity=TRUE;
1339 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1340 if (!is_identity)
1342 FIXME("path gradient transform not implemented\n");
1343 transform_fixme_once = TRUE;
1347 stat = GdipClonePath(fill->path, &flat_path);
1349 if (stat != Ok)
1350 return stat;
1352 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1353 CoordinateSpaceWorld, &world_to_device);
1354 if (stat == Ok)
1356 stat = GdipTransformPath(flat_path, &world_to_device);
1358 if (stat == Ok)
1360 center_point = fill->center;
1361 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1364 if (stat == Ok)
1365 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1368 if (stat != Ok)
1370 GdipDeletePath(flat_path);
1371 return stat;
1374 for (i=0; i<flat_path->pathdata.Count; i++)
1376 int start_center_line=0, end_center_line=0;
1377 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1378 REAL center_distance;
1379 ARGB start_color, end_color;
1380 REAL dy, dx;
1382 type = flat_path->pathdata.Types[i];
1384 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1385 figure_start = i;
1387 start_point = flat_path->pathdata.Points[i];
1389 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1391 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1393 end_point = flat_path->pathdata.Points[figure_start];
1394 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1396 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1398 end_point = flat_path->pathdata.Points[i+1];
1399 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1401 else
1402 continue;
1404 outer_color = start_color;
1406 min_yf = center_point.Y;
1407 if (min_yf > start_point.Y) min_yf = start_point.Y;
1408 if (min_yf > end_point.Y) min_yf = end_point.Y;
1410 if (min_yf < fill_area->Y)
1411 min_y = fill_area->Y;
1412 else
1413 min_y = (INT)ceil(min_yf);
1415 max_yf = center_point.Y;
1416 if (max_yf < start_point.Y) max_yf = start_point.Y;
1417 if (max_yf < end_point.Y) max_yf = end_point.Y;
1419 if (max_yf > fill_area->Y + fill_area->Height)
1420 max_y = fill_area->Y + fill_area->Height;
1421 else
1422 max_y = (INT)ceil(max_yf);
1424 dy = end_point.Y - start_point.Y;
1425 dx = end_point.X - start_point.X;
1427 /* This is proportional to the distance from start-end line to center point. */
1428 center_distance = dy * (start_point.X - center_point.X) +
1429 dx * (center_point.Y - start_point.Y);
1431 for (y=min_y; y<max_y; y++)
1433 REAL yf = (REAL)y;
1435 if (!seen_start && yf >= start_point.Y)
1437 seen_start = TRUE;
1438 start_center_line ^= 1;
1440 if (!seen_end && yf >= end_point.Y)
1442 seen_end = TRUE;
1443 end_center_line ^= 1;
1445 if (!seen_center && yf >= center_point.Y)
1447 seen_center = TRUE;
1448 start_center_line ^= 1;
1449 end_center_line ^= 1;
1452 if (start_center_line)
1453 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1454 else
1455 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1457 if (end_center_line)
1458 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1459 else
1460 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1462 if (line1_xf < line2_xf)
1464 min_x = (INT)ceil(line1_xf);
1465 max_x = (INT)ceil(line2_xf);
1467 else
1469 min_x = (INT)ceil(line2_xf);
1470 max_x = (INT)ceil(line1_xf);
1473 if (min_x < fill_area->X)
1474 min_x = fill_area->X;
1475 if (max_x > fill_area->X + fill_area->Width)
1476 max_x = fill_area->X + fill_area->Width;
1478 for (x=min_x; x<max_x; x++)
1480 REAL xf = (REAL)x;
1481 REAL distance;
1483 if (start_color != end_color)
1485 REAL blend_amount, pdy, pdx;
1486 pdy = yf - center_point.Y;
1487 pdx = xf - center_point.X;
1488 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1489 outer_color = blend_colors(start_color, end_color, blend_amount);
1492 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1493 (end_point.X - start_point.X) * (yf - start_point.Y);
1495 distance = distance / center_distance;
1497 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1498 blend_colors(outer_color, fill->centercolor, distance);
1503 GdipDeletePath(flat_path);
1504 return stat;
1506 default:
1507 return NotImplemented;
1511 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1512 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1513 * should not be called on an hdc that has a path you care about. */
1514 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1515 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1517 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1518 GpMatrix matrix;
1519 HBRUSH brush = NULL;
1520 HPEN pen = NULL;
1521 PointF ptf[4], *custptf = NULL;
1522 POINT pt[4], *custpt = NULL;
1523 BYTE *tp = NULL;
1524 REAL theta, dsmall, dbig, dx, dy = 0.0;
1525 INT i, count;
1526 LOGBRUSH lb;
1527 BOOL customstroke;
1529 if((x1 == x2) && (y1 == y2))
1530 return;
1532 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1534 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1535 if(!customstroke){
1536 brush = CreateSolidBrush(color);
1537 lb.lbStyle = BS_SOLID;
1538 lb.lbColor = color;
1539 lb.lbHatch = 0;
1540 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1541 PS_JOIN_MITER, 1, &lb, 0,
1542 NULL);
1543 oldbrush = SelectObject(graphics->hdc, brush);
1544 oldpen = SelectObject(graphics->hdc, pen);
1547 switch(cap){
1548 case LineCapFlat:
1549 break;
1550 case LineCapSquare:
1551 case LineCapSquareAnchor:
1552 case LineCapDiamondAnchor:
1553 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1554 if(cap == LineCapDiamondAnchor){
1555 dsmall = cos(theta + M_PI_2) * size;
1556 dbig = sin(theta + M_PI_2) * size;
1558 else{
1559 dsmall = cos(theta + M_PI_4) * size;
1560 dbig = sin(theta + M_PI_4) * size;
1563 ptf[0].X = x2 - dsmall;
1564 ptf[1].X = x2 + dbig;
1566 ptf[0].Y = y2 - dbig;
1567 ptf[3].Y = y2 + dsmall;
1569 ptf[1].Y = y2 - dsmall;
1570 ptf[2].Y = y2 + dbig;
1572 ptf[3].X = x2 - dbig;
1573 ptf[2].X = x2 + dsmall;
1575 transform_and_round_points(graphics, pt, ptf, 4);
1576 Polygon(graphics->hdc, pt, 4);
1578 break;
1579 case LineCapArrowAnchor:
1580 size = size * 4.0 / sqrt(3.0);
1582 dx = cos(M_PI / 6.0 + theta) * size;
1583 dy = sin(M_PI / 6.0 + theta) * size;
1585 ptf[0].X = x2 - dx;
1586 ptf[0].Y = y2 - dy;
1588 dx = cos(- M_PI / 6.0 + theta) * size;
1589 dy = sin(- M_PI / 6.0 + theta) * size;
1591 ptf[1].X = x2 - dx;
1592 ptf[1].Y = y2 - dy;
1594 ptf[2].X = x2;
1595 ptf[2].Y = y2;
1597 transform_and_round_points(graphics, pt, ptf, 3);
1598 Polygon(graphics->hdc, pt, 3);
1600 break;
1601 case LineCapRoundAnchor:
1602 dx = dy = ANCHOR_WIDTH * size / 2.0;
1604 ptf[0].X = x2 - dx;
1605 ptf[0].Y = y2 - dy;
1606 ptf[1].X = x2 + dx;
1607 ptf[1].Y = y2 + dy;
1609 transform_and_round_points(graphics, pt, ptf, 2);
1610 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1612 break;
1613 case LineCapTriangle:
1614 size = size / 2.0;
1615 dx = cos(M_PI_2 + theta) * size;
1616 dy = sin(M_PI_2 + theta) * size;
1618 ptf[0].X = x2 - dx;
1619 ptf[0].Y = y2 - dy;
1620 ptf[1].X = x2 + dx;
1621 ptf[1].Y = y2 + dy;
1623 dx = cos(theta) * size;
1624 dy = sin(theta) * size;
1626 ptf[2].X = x2 + dx;
1627 ptf[2].Y = y2 + dy;
1629 transform_and_round_points(graphics, pt, ptf, 3);
1630 Polygon(graphics->hdc, pt, 3);
1632 break;
1633 case LineCapRound:
1634 dx = dy = size / 2.0;
1636 ptf[0].X = x2 - dx;
1637 ptf[0].Y = y2 - dy;
1638 ptf[1].X = x2 + dx;
1639 ptf[1].Y = y2 + dy;
1641 dx = -cos(M_PI_2 + theta) * size;
1642 dy = -sin(M_PI_2 + theta) * size;
1644 ptf[2].X = x2 - dx;
1645 ptf[2].Y = y2 - dy;
1646 ptf[3].X = x2 + dx;
1647 ptf[3].Y = y2 + dy;
1649 transform_and_round_points(graphics, pt, ptf, 4);
1650 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1651 pt[2].y, pt[3].x, pt[3].y);
1653 break;
1654 case LineCapCustom:
1655 if(!custom)
1656 break;
1658 count = custom->pathdata.Count;
1659 custptf = heap_alloc_zero(count * sizeof(PointF));
1660 custpt = heap_alloc_zero(count * sizeof(POINT));
1661 tp = heap_alloc_zero(count);
1663 if(!custptf || !custpt || !tp)
1664 goto custend;
1666 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1668 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1669 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1670 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1671 MatrixOrderAppend);
1672 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1673 GdipTransformMatrixPoints(&matrix, custptf, count);
1675 transform_and_round_points(graphics, custpt, custptf, count);
1677 for(i = 0; i < count; i++)
1678 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1680 if(custom->fill){
1681 BeginPath(graphics->hdc);
1682 PolyDraw(graphics->hdc, custpt, tp, count);
1683 EndPath(graphics->hdc);
1684 StrokeAndFillPath(graphics->hdc);
1686 else
1687 PolyDraw(graphics->hdc, custpt, tp, count);
1689 custend:
1690 heap_free(custptf);
1691 heap_free(custpt);
1692 heap_free(tp);
1693 break;
1694 default:
1695 break;
1698 if(!customstroke){
1699 SelectObject(graphics->hdc, oldbrush);
1700 SelectObject(graphics->hdc, oldpen);
1701 DeleteObject(brush);
1702 DeleteObject(pen);
1706 /* Shortens the line by the given percent by changing x2, y2.
1707 * If percent is > 1.0 then the line will change direction.
1708 * If percent is negative it can lengthen the line. */
1709 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1711 REAL dist, theta, dx, dy;
1713 if((y1 == *y2) && (x1 == *x2))
1714 return;
1716 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1717 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1718 dx = cos(theta) * dist;
1719 dy = sin(theta) * dist;
1721 *x2 = *x2 + dx;
1722 *y2 = *y2 + dy;
1725 /* Shortens the line by the given amount by changing x2, y2.
1726 * If the amount is greater than the distance, the line will become length 0.
1727 * If the amount is negative, it can lengthen the line. */
1728 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1730 REAL dx, dy, percent;
1732 dx = *x2 - x1;
1733 dy = *y2 - y1;
1734 if(dx == 0 && dy == 0)
1735 return;
1737 percent = amt / sqrt(dx * dx + dy * dy);
1738 if(percent >= 1.0){
1739 *x2 = x1;
1740 *y2 = y1;
1741 return;
1744 shorten_line_percent(x1, y1, x2, y2, percent);
1747 /* Conducts a linear search to find the bezier points that will back off
1748 * the endpoint of the curve by a distance of amt. Linear search works
1749 * better than binary in this case because there are multiple solutions,
1750 * and binary searches often find a bad one. I don't think this is what
1751 * Windows does but short of rendering the bezier without GDI's help it's
1752 * the best we can do. If rev then work from the start of the passed points
1753 * instead of the end. */
1754 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1756 GpPointF origpt[4];
1757 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1758 INT i, first = 0, second = 1, third = 2, fourth = 3;
1760 if(rev){
1761 first = 3;
1762 second = 2;
1763 third = 1;
1764 fourth = 0;
1767 origx = pt[fourth].X;
1768 origy = pt[fourth].Y;
1769 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1771 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1772 /* reset bezier points to original values */
1773 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1774 /* Perform magic on bezier points. Order is important here.*/
1775 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1776 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1777 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1778 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1779 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1780 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1782 dx = pt[fourth].X - origx;
1783 dy = pt[fourth].Y - origy;
1785 diff = sqrt(dx * dx + dy * dy);
1786 percent += 0.0005 * amt;
1790 /* Draws a combination of bezier curves and lines between points. */
1791 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1792 GDIPCONST BYTE * types, INT count, BOOL caps)
1794 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1795 BYTE *tp = heap_alloc_zero(count);
1796 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1797 INT i, j;
1798 GpStatus status = GenericError;
1800 if(!count){
1801 status = Ok;
1802 goto end;
1804 if(!pti || !tp || !ptcopy){
1805 status = OutOfMemory;
1806 goto end;
1809 for(i = 1; i < count; i++){
1810 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1811 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1812 || !(types[i + 2] & PathPointTypeBezier)){
1813 ERR("Bad bezier points\n");
1814 goto end;
1816 i += 2;
1820 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1822 /* If we are drawing caps, go through the points and adjust them accordingly,
1823 * and draw the caps. */
1824 if(caps){
1825 switch(types[count - 1] & PathPointTypePathTypeMask){
1826 case PathPointTypeBezier:
1827 if(pen->endcap == LineCapArrowAnchor)
1828 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1829 else if((pen->endcap == LineCapCustom) && pen->customend)
1830 shorten_bezier_amt(&ptcopy[count - 4],
1831 pen->width * pen->customend->inset, FALSE);
1833 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1834 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1835 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1836 pt[count - 1].X, pt[count - 1].Y);
1838 break;
1839 case PathPointTypeLine:
1840 if(pen->endcap == LineCapArrowAnchor)
1841 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1842 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1843 pen->width);
1844 else if((pen->endcap == LineCapCustom) && pen->customend)
1845 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1846 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1847 pen->customend->inset * pen->width);
1849 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1850 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1851 pt[count - 1].Y);
1853 break;
1854 default:
1855 ERR("Bad path last point\n");
1856 goto end;
1859 /* Find start of points */
1860 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1861 == PathPointTypeStart); j++);
1863 switch(types[j] & PathPointTypePathTypeMask){
1864 case PathPointTypeBezier:
1865 if(pen->startcap == LineCapArrowAnchor)
1866 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1867 else if((pen->startcap == LineCapCustom) && pen->customstart)
1868 shorten_bezier_amt(&ptcopy[j - 1],
1869 pen->width * pen->customstart->inset, TRUE);
1871 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1872 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1873 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1874 pt[j - 1].X, pt[j - 1].Y);
1876 break;
1877 case PathPointTypeLine:
1878 if(pen->startcap == LineCapArrowAnchor)
1879 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1880 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1881 pen->width);
1882 else if((pen->startcap == LineCapCustom) && pen->customstart)
1883 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1884 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1885 pen->customstart->inset * pen->width);
1887 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1888 pt[j].X, pt[j].Y, pt[j - 1].X,
1889 pt[j - 1].Y);
1891 break;
1892 default:
1893 ERR("Bad path points\n");
1894 goto end;
1898 transform_and_round_points(graphics, pti, ptcopy, count);
1900 for(i = 0; i < count; i++){
1901 tp[i] = convert_path_point_type(types[i]);
1904 PolyDraw(graphics->hdc, pti, tp, count);
1906 status = Ok;
1908 end:
1909 heap_free(pti);
1910 heap_free(ptcopy);
1911 heap_free(tp);
1913 return status;
1916 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1918 GpStatus result;
1920 BeginPath(graphics->hdc);
1921 result = draw_poly(graphics, NULL, path->pathdata.Points,
1922 path->pathdata.Types, path->pathdata.Count, FALSE);
1923 EndPath(graphics->hdc);
1924 return result;
1927 typedef struct _GraphicsContainerItem {
1928 struct list entry;
1929 GraphicsContainer contid;
1931 SmoothingMode smoothing;
1932 CompositingQuality compqual;
1933 InterpolationMode interpolation;
1934 CompositingMode compmode;
1935 TextRenderingHint texthint;
1936 REAL scale;
1937 GpUnit unit;
1938 PixelOffsetMode pixeloffset;
1939 UINT textcontrast;
1940 GpMatrix worldtrans;
1941 GpRegion* clip;
1942 INT origin_x, origin_y;
1943 } GraphicsContainerItem;
1945 static GpStatus init_container(GraphicsContainerItem** container,
1946 GDIPCONST GpGraphics* graphics){
1947 GpStatus sts;
1949 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
1950 if(!(*container))
1951 return OutOfMemory;
1953 (*container)->contid = graphics->contid + 1;
1955 (*container)->smoothing = graphics->smoothing;
1956 (*container)->compqual = graphics->compqual;
1957 (*container)->interpolation = graphics->interpolation;
1958 (*container)->compmode = graphics->compmode;
1959 (*container)->texthint = graphics->texthint;
1960 (*container)->scale = graphics->scale;
1961 (*container)->unit = graphics->unit;
1962 (*container)->textcontrast = graphics->textcontrast;
1963 (*container)->pixeloffset = graphics->pixeloffset;
1964 (*container)->origin_x = graphics->origin_x;
1965 (*container)->origin_y = graphics->origin_y;
1966 (*container)->worldtrans = graphics->worldtrans;
1968 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1969 if(sts != Ok){
1970 heap_free(*container);
1971 *container = NULL;
1972 return sts;
1975 return Ok;
1978 static void delete_container(GraphicsContainerItem* container)
1980 GdipDeleteRegion(container->clip);
1981 heap_free(container);
1984 static GpStatus restore_container(GpGraphics* graphics,
1985 GDIPCONST GraphicsContainerItem* container){
1986 GpStatus sts;
1987 GpRegion *newClip;
1989 sts = GdipCloneRegion(container->clip, &newClip);
1990 if(sts != Ok) return sts;
1992 graphics->worldtrans = container->worldtrans;
1994 GdipDeleteRegion(graphics->clip);
1995 graphics->clip = newClip;
1997 graphics->contid = container->contid - 1;
1999 graphics->smoothing = container->smoothing;
2000 graphics->compqual = container->compqual;
2001 graphics->interpolation = container->interpolation;
2002 graphics->compmode = container->compmode;
2003 graphics->texthint = container->texthint;
2004 graphics->scale = container->scale;
2005 graphics->unit = container->unit;
2006 graphics->textcontrast = container->textcontrast;
2007 graphics->pixeloffset = container->pixeloffset;
2008 graphics->origin_x = container->origin_x;
2009 graphics->origin_y = container->origin_y;
2011 return Ok;
2014 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2016 RECT wnd_rect;
2017 GpStatus stat=Ok;
2018 GpUnit unit;
2020 if(graphics->hwnd) {
2021 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2022 return GenericError;
2024 rect->X = wnd_rect.left;
2025 rect->Y = wnd_rect.top;
2026 rect->Width = wnd_rect.right - wnd_rect.left;
2027 rect->Height = wnd_rect.bottom - wnd_rect.top;
2028 }else if (graphics->image){
2029 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2030 if (stat == Ok && unit != UnitPixel)
2031 FIXME("need to convert from unit %i\n", unit);
2032 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2033 HBITMAP hbmp;
2034 BITMAP bmp;
2036 rect->X = 0;
2037 rect->Y = 0;
2039 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2040 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2042 rect->Width = bmp.bmWidth;
2043 rect->Height = bmp.bmHeight;
2045 else
2047 /* FIXME: ??? */
2048 rect->Width = 1;
2049 rect->Height = 1;
2051 }else{
2052 rect->X = 0;
2053 rect->Y = 0;
2054 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2055 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2058 if (graphics->hdc)
2060 POINT points[2];
2062 points[0].x = rect->X;
2063 points[0].y = rect->Y;
2064 points[1].x = rect->X + rect->Width;
2065 points[1].y = rect->Y + rect->Height;
2067 DPtoLP(graphics->hdc, points, sizeof(points)/sizeof(points[0]));
2069 rect->X = min(points[0].x, points[1].x);
2070 rect->Y = min(points[0].y, points[1].y);
2071 rect->Width = abs(points[1].x - points[0].x);
2072 rect->Height = abs(points[1].y - points[0].y);
2075 return stat;
2078 /* on success, rgn will contain the region of the graphics object which
2079 * is visible after clipping has been applied */
2080 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2082 GpStatus stat;
2083 GpRectF rectf;
2084 GpRegion* tmp;
2086 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2087 return stat;
2089 if((stat = GdipCreateRegion(&tmp)) != Ok)
2090 return stat;
2092 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2093 goto end;
2095 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2096 goto end;
2098 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2100 end:
2101 GdipDeleteRegion(tmp);
2102 return stat;
2105 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2107 REAL height;
2109 if (font->unit == UnitPixel)
2111 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2113 else
2115 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2116 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2117 else
2118 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2121 lf->lfHeight = -(height + 0.5);
2122 lf->lfWidth = 0;
2123 lf->lfEscapement = 0;
2124 lf->lfOrientation = 0;
2125 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2126 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2127 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2128 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2129 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2130 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2131 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2132 lf->lfQuality = DEFAULT_QUALITY;
2133 lf->lfPitchAndFamily = 0;
2134 strcpyW(lf->lfFaceName, font->family->FamilyName);
2137 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2138 GDIPCONST GpStringFormat *format, HFONT *hfont,
2139 GDIPCONST GpMatrix *matrix)
2141 HDC hdc = CreateCompatibleDC(0);
2142 GpPointF pt[3];
2143 REAL angle, rel_width, rel_height, font_height;
2144 LOGFONTW lfw;
2145 HFONT unscaled_font;
2146 TEXTMETRICW textmet;
2148 if (font->unit == UnitPixel || font->unit == UnitWorld)
2149 font_height = font->emSize;
2150 else
2152 REAL unit_scale, res;
2154 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2155 unit_scale = units_scale(font->unit, graphics->unit, res);
2157 font_height = font->emSize * unit_scale;
2160 pt[0].X = 0.0;
2161 pt[0].Y = 0.0;
2162 pt[1].X = 1.0;
2163 pt[1].Y = 0.0;
2164 pt[2].X = 0.0;
2165 pt[2].Y = 1.0;
2166 if (matrix)
2168 GpMatrix xform = *matrix;
2169 GdipTransformMatrixPoints(&xform, pt, 3);
2172 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2173 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2174 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2175 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2176 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2177 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2179 get_log_fontW(font, graphics, &lfw);
2180 lfw.lfHeight = -gdip_round(font_height * rel_height);
2181 unscaled_font = CreateFontIndirectW(&lfw);
2183 SelectObject(hdc, unscaled_font);
2184 GetTextMetricsW(hdc, &textmet);
2186 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2187 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2189 *hfont = CreateFontIndirectW(&lfw);
2191 DeleteDC(hdc);
2192 DeleteObject(unscaled_font);
2195 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2197 TRACE("(%p, %p)\n", hdc, graphics);
2199 return GdipCreateFromHDC2(hdc, NULL, graphics);
2202 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2204 GpStatus retval;
2205 HBITMAP hbitmap;
2206 DIBSECTION dib;
2208 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2210 if(hDevice != NULL)
2211 FIXME("Don't know how to handle parameter hDevice\n");
2213 if(hdc == NULL)
2214 return OutOfMemory;
2216 if(graphics == NULL)
2217 return InvalidParameter;
2219 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2220 if(!*graphics) return OutOfMemory;
2222 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2224 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2225 heap_free(*graphics);
2226 return retval;
2229 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2230 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2231 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2233 (*graphics)->alpha_hdc = 1;
2236 (*graphics)->hdc = hdc;
2237 (*graphics)->hwnd = WindowFromDC(hdc);
2238 (*graphics)->owndc = FALSE;
2239 (*graphics)->smoothing = SmoothingModeDefault;
2240 (*graphics)->compqual = CompositingQualityDefault;
2241 (*graphics)->interpolation = InterpolationModeBilinear;
2242 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2243 (*graphics)->compmode = CompositingModeSourceOver;
2244 (*graphics)->unit = UnitDisplay;
2245 (*graphics)->scale = 1.0;
2246 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2247 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2248 (*graphics)->busy = FALSE;
2249 (*graphics)->textcontrast = 4;
2250 list_init(&(*graphics)->containers);
2251 (*graphics)->contid = 0;
2253 TRACE("<-- %p\n", *graphics);
2255 return Ok;
2258 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2260 GpStatus retval;
2262 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2263 if(!*graphics) return OutOfMemory;
2265 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2267 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2268 heap_free(*graphics);
2269 return retval;
2272 (*graphics)->hdc = NULL;
2273 (*graphics)->hwnd = NULL;
2274 (*graphics)->owndc = FALSE;
2275 (*graphics)->image = image;
2276 /* We have to store the image type here because the image may be freed
2277 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2278 (*graphics)->image_type = image->type;
2279 (*graphics)->smoothing = SmoothingModeDefault;
2280 (*graphics)->compqual = CompositingQualityDefault;
2281 (*graphics)->interpolation = InterpolationModeBilinear;
2282 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2283 (*graphics)->compmode = CompositingModeSourceOver;
2284 (*graphics)->unit = UnitDisplay;
2285 (*graphics)->scale = 1.0;
2286 (*graphics)->xres = image->xres;
2287 (*graphics)->yres = image->yres;
2288 (*graphics)->busy = FALSE;
2289 (*graphics)->textcontrast = 4;
2290 list_init(&(*graphics)->containers);
2291 (*graphics)->contid = 0;
2293 TRACE("<-- %p\n", *graphics);
2295 return Ok;
2298 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2300 GpStatus ret;
2301 HDC hdc;
2303 TRACE("(%p, %p)\n", hwnd, graphics);
2305 hdc = GetDC(hwnd);
2307 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2309 ReleaseDC(hwnd, hdc);
2310 return ret;
2313 (*graphics)->hwnd = hwnd;
2314 (*graphics)->owndc = TRUE;
2316 return Ok;
2319 /* FIXME: no icm handling */
2320 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2322 TRACE("(%p, %p)\n", hwnd, graphics);
2324 return GdipCreateFromHWND(hwnd, graphics);
2327 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2328 UINT access, IStream **stream)
2330 DWORD dwMode;
2331 HRESULT ret;
2333 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2335 if(!stream || !filename)
2336 return InvalidParameter;
2338 if(access & GENERIC_WRITE)
2339 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2340 else if(access & GENERIC_READ)
2341 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2342 else
2343 return InvalidParameter;
2345 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2347 return hresult_to_status(ret);
2350 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2352 GraphicsContainerItem *cont, *next;
2353 GpStatus stat;
2354 TRACE("(%p)\n", graphics);
2356 if(!graphics) return InvalidParameter;
2357 if(graphics->busy) return ObjectBusy;
2359 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2361 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2362 if (stat != Ok)
2363 return stat;
2366 if(graphics->owndc)
2367 ReleaseDC(graphics->hwnd, graphics->hdc);
2369 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2370 list_remove(&cont->entry);
2371 delete_container(cont);
2374 GdipDeleteRegion(graphics->clip);
2376 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2377 * do otherwise, but we can't have that in the test suite because it means
2378 * accessing freed memory. */
2379 graphics->busy = TRUE;
2381 heap_free(graphics);
2383 return Ok;
2386 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2387 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2389 GpStatus status;
2390 GpPath *path;
2392 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2393 width, height, startAngle, sweepAngle);
2395 if(!graphics || !pen || width <= 0 || height <= 0)
2396 return InvalidParameter;
2398 if(graphics->busy)
2399 return ObjectBusy;
2401 status = GdipCreatePath(FillModeAlternate, &path);
2402 if (status != Ok) return status;
2404 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2405 if (status == Ok)
2406 status = GdipDrawPath(graphics, pen, path);
2408 GdipDeletePath(path);
2409 return status;
2412 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2413 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2415 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2416 width, height, startAngle, sweepAngle);
2418 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2421 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2422 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2424 GpPointF pt[4];
2426 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2427 x2, y2, x3, y3, x4, y4);
2429 if(!graphics || !pen)
2430 return InvalidParameter;
2432 if(graphics->busy)
2433 return ObjectBusy;
2435 pt[0].X = x1;
2436 pt[0].Y = y1;
2437 pt[1].X = x2;
2438 pt[1].Y = y2;
2439 pt[2].X = x3;
2440 pt[2].Y = y3;
2441 pt[3].X = x4;
2442 pt[3].Y = y4;
2443 return GdipDrawBeziers(graphics, pen, pt, 4);
2446 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2447 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2449 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2450 x2, y2, x3, y3, x4, y4);
2452 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2455 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2456 GDIPCONST GpPointF *points, INT count)
2458 GpStatus status;
2459 GpPath *path;
2461 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2463 if(!graphics || !pen || !points || (count <= 0))
2464 return InvalidParameter;
2466 if(graphics->busy)
2467 return ObjectBusy;
2469 status = GdipCreatePath(FillModeAlternate, &path);
2470 if (status != Ok) return status;
2472 status = GdipAddPathBeziers(path, points, count);
2473 if (status == Ok)
2474 status = GdipDrawPath(graphics, pen, path);
2476 GdipDeletePath(path);
2477 return status;
2480 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2481 GDIPCONST GpPoint *points, INT count)
2483 GpPointF *pts;
2484 GpStatus ret;
2485 INT i;
2487 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2489 if(!graphics || !pen || !points || (count <= 0))
2490 return InvalidParameter;
2492 if(graphics->busy)
2493 return ObjectBusy;
2495 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2496 if(!pts)
2497 return OutOfMemory;
2499 for(i = 0; i < count; i++){
2500 pts[i].X = (REAL)points[i].X;
2501 pts[i].Y = (REAL)points[i].Y;
2504 ret = GdipDrawBeziers(graphics,pen,pts,count);
2506 heap_free(pts);
2508 return ret;
2511 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2512 GDIPCONST GpPointF *points, INT count)
2514 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2516 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2519 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2520 GDIPCONST GpPoint *points, INT count)
2522 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2524 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2527 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2528 GDIPCONST GpPointF *points, INT count, REAL tension)
2530 GpPath *path;
2531 GpStatus status;
2533 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2535 if(!graphics || !pen || !points || count <= 0)
2536 return InvalidParameter;
2538 if(graphics->busy)
2539 return ObjectBusy;
2541 status = GdipCreatePath(FillModeAlternate, &path);
2542 if (status != Ok) return status;
2544 status = GdipAddPathClosedCurve2(path, points, count, tension);
2545 if (status == Ok)
2546 status = GdipDrawPath(graphics, pen, path);
2548 GdipDeletePath(path);
2550 return status;
2553 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2554 GDIPCONST GpPoint *points, INT count, REAL tension)
2556 GpPointF *ptf;
2557 GpStatus stat;
2558 INT i;
2560 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2562 if(!points || count <= 0)
2563 return InvalidParameter;
2565 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2566 if(!ptf)
2567 return OutOfMemory;
2569 for(i = 0; i < count; i++){
2570 ptf[i].X = (REAL)points[i].X;
2571 ptf[i].Y = (REAL)points[i].Y;
2574 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2576 heap_free(ptf);
2578 return stat;
2581 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2582 GDIPCONST GpPointF *points, INT count)
2584 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2586 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2589 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2590 GDIPCONST GpPoint *points, INT count)
2592 GpPointF *pointsF;
2593 GpStatus ret;
2594 INT i;
2596 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2598 if(!points)
2599 return InvalidParameter;
2601 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2602 if(!pointsF)
2603 return OutOfMemory;
2605 for(i = 0; i < count; i++){
2606 pointsF[i].X = (REAL)points[i].X;
2607 pointsF[i].Y = (REAL)points[i].Y;
2610 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2611 heap_free(pointsF);
2613 return ret;
2616 /* Approximates cardinal spline with Bezier curves. */
2617 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2618 GDIPCONST GpPointF *points, INT count, REAL tension)
2620 GpPath *path;
2621 GpStatus status;
2623 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2625 if(!graphics || !pen)
2626 return InvalidParameter;
2628 if(graphics->busy)
2629 return ObjectBusy;
2631 if(count < 2)
2632 return InvalidParameter;
2634 status = GdipCreatePath(FillModeAlternate, &path);
2635 if (status != Ok) return status;
2637 status = GdipAddPathCurve2(path, points, count, tension);
2638 if (status == Ok)
2639 status = GdipDrawPath(graphics, pen, path);
2641 GdipDeletePath(path);
2642 return status;
2645 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2646 GDIPCONST GpPoint *points, INT count, REAL tension)
2648 GpPointF *pointsF;
2649 GpStatus ret;
2650 INT i;
2652 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2654 if(!points)
2655 return InvalidParameter;
2657 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2658 if(!pointsF)
2659 return OutOfMemory;
2661 for(i = 0; i < count; i++){
2662 pointsF[i].X = (REAL)points[i].X;
2663 pointsF[i].Y = (REAL)points[i].Y;
2666 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2667 heap_free(pointsF);
2669 return ret;
2672 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2673 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2674 REAL tension)
2676 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2678 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2679 return InvalidParameter;
2682 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2685 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2686 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2687 REAL tension)
2689 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2691 if(count < 0){
2692 return OutOfMemory;
2695 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2696 return InvalidParameter;
2699 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2702 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2703 REAL y, REAL width, REAL height)
2705 GpPath *path;
2706 GpStatus status;
2708 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2710 if(!graphics || !pen)
2711 return InvalidParameter;
2713 if(graphics->busy)
2714 return ObjectBusy;
2716 status = GdipCreatePath(FillModeAlternate, &path);
2717 if (status != Ok) return status;
2719 status = GdipAddPathEllipse(path, x, y, width, height);
2720 if (status == Ok)
2721 status = GdipDrawPath(graphics, pen, path);
2723 GdipDeletePath(path);
2724 return status;
2727 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2728 INT y, INT width, INT height)
2730 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2732 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2736 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2738 UINT width, height;
2740 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2742 if(!graphics || !image)
2743 return InvalidParameter;
2745 GdipGetImageWidth(image, &width);
2746 GdipGetImageHeight(image, &height);
2748 return GdipDrawImagePointRect(graphics, image, x, y,
2749 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2752 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2753 INT y)
2755 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2757 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2760 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2761 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2762 GpUnit srcUnit)
2764 GpPointF points[3];
2765 REAL scale_x, scale_y, width, height;
2767 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2769 if (!graphics || !image) return InvalidParameter;
2771 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2772 scale_x *= graphics->xres / image->xres;
2773 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2774 scale_y *= graphics->yres / image->yres;
2775 width = srcwidth * scale_x;
2776 height = srcheight * scale_y;
2778 points[0].X = points[2].X = x;
2779 points[0].Y = points[1].Y = y;
2780 points[1].X = x + width;
2781 points[2].Y = y + height;
2783 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2784 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2787 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2788 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2789 GpUnit srcUnit)
2791 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2794 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2795 GDIPCONST GpPointF *dstpoints, INT count)
2797 UINT width, height;
2799 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2801 if(!image)
2802 return InvalidParameter;
2804 GdipGetImageWidth(image, &width);
2805 GdipGetImageHeight(image, &height);
2807 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2808 width, height, UnitPixel, NULL, NULL, NULL);
2811 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2812 GDIPCONST GpPoint *dstpoints, INT count)
2814 GpPointF ptf[3];
2816 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2818 if (count != 3 || !dstpoints)
2819 return InvalidParameter;
2821 ptf[0].X = (REAL)dstpoints[0].X;
2822 ptf[0].Y = (REAL)dstpoints[0].Y;
2823 ptf[1].X = (REAL)dstpoints[1].X;
2824 ptf[1].Y = (REAL)dstpoints[1].Y;
2825 ptf[2].X = (REAL)dstpoints[2].X;
2826 ptf[2].Y = (REAL)dstpoints[2].Y;
2828 return GdipDrawImagePoints(graphics, image, ptf, count);
2831 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2832 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2834 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2835 return TRUE;
2838 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2839 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2840 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2841 DrawImageAbort callback, VOID * callbackData)
2843 GpPointF ptf[4];
2844 POINT pti[4];
2845 GpStatus stat;
2847 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2848 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2849 callbackData);
2851 if (count > 3)
2852 return NotImplemented;
2854 if(!graphics || !image || !points || count != 3)
2855 return InvalidParameter;
2857 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2858 debugstr_pointf(&points[2]));
2860 memcpy(ptf, points, 3 * sizeof(GpPointF));
2862 /* Ensure source width/height is positive */
2863 if (srcwidth < 0)
2865 GpPointF tmp = ptf[1];
2866 srcx = srcx + srcwidth;
2867 srcwidth = -srcwidth;
2868 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2869 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2870 ptf[1] = ptf[0];
2871 ptf[0] = tmp;
2874 if (srcheight < 0)
2876 GpPointF tmp = ptf[2];
2877 srcy = srcy + srcheight;
2878 srcheight = -srcheight;
2879 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2880 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2881 ptf[2] = ptf[0];
2882 ptf[0] = tmp;
2885 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2886 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2887 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2888 return Ok;
2889 transform_and_round_points(graphics, pti, ptf, 4);
2891 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2892 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2894 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2895 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2896 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2897 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2898 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2900 if (image->picture)
2902 if (!graphics->hdc)
2904 FIXME("graphics object has no HDC\n");
2907 if(IPicture_Render(image->picture, graphics->hdc,
2908 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2909 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
2911 if(callback)
2912 callback(callbackData);
2913 return GenericError;
2916 else if (image->type == ImageTypeBitmap)
2918 GpBitmap* bitmap = (GpBitmap*)image;
2919 BOOL do_resampling = FALSE;
2920 BOOL use_software = FALSE;
2922 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2923 graphics->xres, graphics->yres,
2924 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2925 graphics->scale, image->xres, image->yres, bitmap->format,
2926 imageAttributes ? imageAttributes->outside_color : 0);
2928 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2929 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2930 srcx < 0 || srcy < 0 ||
2931 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2932 do_resampling = TRUE;
2934 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
2935 (graphics->image && graphics->image->type == ImageTypeBitmap))
2936 use_software = TRUE;
2938 if (use_software)
2940 RECT dst_area;
2941 GpRectF graphics_bounds;
2942 GpRect src_area;
2943 int i, x, y, src_stride, dst_stride;
2944 GpMatrix dst_to_src;
2945 REAL m11, m12, m21, m22, mdx, mdy;
2946 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
2947 BitmapData lockeddata;
2948 InterpolationMode interpolation = graphics->interpolation;
2949 PixelOffsetMode offset_mode = graphics->pixeloffset;
2950 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2951 REAL x_dx, x_dy, y_dx, y_dy;
2952 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2954 if (!imageAttributes)
2955 imageAttributes = &defaultImageAttributes;
2957 dst_area.left = dst_area.right = pti[0].x;
2958 dst_area.top = dst_area.bottom = pti[0].y;
2959 for (i=1; i<4; i++)
2961 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2962 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2963 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2964 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2967 stat = get_graphics_bounds(graphics, &graphics_bounds);
2968 if (stat != Ok) return stat;
2970 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
2971 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
2972 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
2973 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
2975 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2977 if (IsRectEmpty(&dst_area)) return Ok;
2979 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2980 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2981 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2982 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2983 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2984 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2986 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2988 stat = GdipInvertMatrix(&dst_to_src);
2989 if (stat != Ok) return stat;
2991 if (do_resampling)
2993 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2994 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2996 else
2998 /* Make sure src_area is equal in size to dst_area. */
2999 src_area.X = srcx + dst_area.left - pti[0].x;
3000 src_area.Y = srcy + dst_area.top - pti[0].y;
3001 src_area.Width = dst_area.right - dst_area.left;
3002 src_area.Height = dst_area.bottom - dst_area.top;
3005 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3007 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3008 if (!src_data)
3009 return OutOfMemory;
3010 src_stride = sizeof(ARGB) * src_area.Width;
3012 /* Read the bits we need from the source bitmap into a compatible buffer. */
3013 lockeddata.Width = src_area.Width;
3014 lockeddata.Height = src_area.Height;
3015 lockeddata.Stride = src_stride;
3016 lockeddata.Scan0 = src_data;
3017 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3018 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3019 else
3020 lockeddata.PixelFormat = PixelFormat32bppARGB;
3022 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3023 lockeddata.PixelFormat, &lockeddata);
3025 if (stat == Ok)
3026 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3028 if (stat != Ok)
3030 heap_free(src_data);
3031 return stat;
3034 apply_image_attributes(imageAttributes, src_data,
3035 src_area.Width, src_area.Height,
3036 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3038 if (do_resampling)
3040 /* Transform the bits as needed to the destination. */
3041 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3042 if (!dst_data)
3044 heap_free(src_data);
3045 return OutOfMemory;
3048 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3050 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3052 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3053 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3054 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3055 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3057 for (x=dst_area.left; x<dst_area.right; x++)
3059 for (y=dst_area.top; y<dst_area.bottom; y++)
3061 GpPointF src_pointf;
3062 ARGB *dst_color;
3064 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3065 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3067 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3069 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3070 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3071 imageAttributes, interpolation, offset_mode);
3072 else
3073 *dst_color = 0;
3077 else
3079 dst_data = src_data;
3080 dst_stride = src_stride;
3083 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3084 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3085 lockeddata.PixelFormat);
3087 heap_free(src_data);
3089 heap_free(dst_dyn_data);
3091 return stat;
3093 else
3095 HDC hdc;
3096 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3097 HBITMAP hbitmap, old_hbm=NULL;
3099 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3100 bitmap->format == PixelFormat24bppRGB ||
3101 bitmap->format == PixelFormat32bppRGB ||
3102 bitmap->format == PixelFormat32bppPARGB))
3104 BITMAPINFOHEADER bih;
3105 BYTE *temp_bits;
3106 PixelFormat dst_format;
3108 /* we can't draw a bitmap of this format directly */
3109 hdc = CreateCompatibleDC(0);
3110 temp_hdc = TRUE;
3111 temp_bitmap = TRUE;
3113 bih.biSize = sizeof(BITMAPINFOHEADER);
3114 bih.biWidth = bitmap->width;
3115 bih.biHeight = -bitmap->height;
3116 bih.biPlanes = 1;
3117 bih.biBitCount = 32;
3118 bih.biCompression = BI_RGB;
3119 bih.biSizeImage = 0;
3120 bih.biXPelsPerMeter = 0;
3121 bih.biYPelsPerMeter = 0;
3122 bih.biClrUsed = 0;
3123 bih.biClrImportant = 0;
3125 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3126 (void**)&temp_bits, NULL, 0);
3128 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3129 dst_format = PixelFormat32bppPARGB;
3130 else
3131 dst_format = PixelFormat32bppRGB;
3133 convert_pixels(bitmap->width, bitmap->height,
3134 bitmap->width*4, temp_bits, dst_format,
3135 bitmap->stride, bitmap->bits, bitmap->format,
3136 bitmap->image.palette);
3138 else
3140 if (bitmap->hbitmap)
3141 hbitmap = bitmap->hbitmap;
3142 else
3144 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3145 temp_bitmap = TRUE;
3148 hdc = bitmap->hdc;
3149 temp_hdc = (hdc == 0);
3152 if (temp_hdc)
3154 if (!hdc) hdc = CreateCompatibleDC(0);
3155 old_hbm = SelectObject(hdc, hbitmap);
3158 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3160 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3161 hdc, srcx, srcy, srcwidth, srcheight);
3163 else
3165 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3166 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3169 if (temp_hdc)
3171 SelectObject(hdc, old_hbm);
3172 DeleteDC(hdc);
3175 if (temp_bitmap)
3176 DeleteObject(hbitmap);
3179 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3181 GpRectF rc;
3183 rc.X = srcx;
3184 rc.Y = srcy;
3185 rc.Width = srcwidth;
3186 rc.Height = srcheight;
3188 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3189 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3191 else
3193 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3194 return InvalidParameter;
3197 return Ok;
3200 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3201 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3202 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3203 DrawImageAbort callback, VOID * callbackData)
3205 GpPointF pointsF[3];
3206 INT i;
3208 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3209 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3210 callbackData);
3212 if(!points || count!=3)
3213 return InvalidParameter;
3215 for(i = 0; i < count; i++){
3216 pointsF[i].X = (REAL)points[i].X;
3217 pointsF[i].Y = (REAL)points[i].Y;
3220 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3221 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3222 callback, callbackData);
3225 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3226 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3227 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3228 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3229 VOID * callbackData)
3231 GpPointF points[3];
3233 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3234 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3235 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3237 points[0].X = dstx;
3238 points[0].Y = dsty;
3239 points[1].X = dstx + dstwidth;
3240 points[1].Y = dsty;
3241 points[2].X = dstx;
3242 points[2].Y = dsty + dstheight;
3244 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3245 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3248 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3249 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3250 INT srcwidth, INT srcheight, GpUnit srcUnit,
3251 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3252 VOID * callbackData)
3254 GpPointF points[3];
3256 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3257 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3258 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3260 points[0].X = dstx;
3261 points[0].Y = dsty;
3262 points[1].X = dstx + dstwidth;
3263 points[1].Y = dsty;
3264 points[2].X = dstx;
3265 points[2].Y = dsty + dstheight;
3267 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3268 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3271 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3272 REAL x, REAL y, REAL width, REAL height)
3274 RectF bounds;
3275 GpUnit unit;
3276 GpStatus ret;
3278 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3280 if(!graphics || !image)
3281 return InvalidParameter;
3283 ret = GdipGetImageBounds(image, &bounds, &unit);
3284 if(ret != Ok)
3285 return ret;
3287 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3288 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3289 unit, NULL, NULL, NULL);
3292 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3293 INT x, INT y, INT width, INT height)
3295 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3297 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3300 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3301 REAL y1, REAL x2, REAL y2)
3303 GpPointF pt[2];
3305 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3307 pt[0].X = x1;
3308 pt[0].Y = y1;
3309 pt[1].X = x2;
3310 pt[1].Y = y2;
3311 return GdipDrawLines(graphics, pen, pt, 2);
3314 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3315 INT y1, INT x2, INT y2)
3317 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3319 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3322 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3323 GpPointF *points, INT count)
3325 GpStatus status;
3326 GpPath *path;
3328 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3330 if(!pen || !graphics || (count < 2))
3331 return InvalidParameter;
3333 if(graphics->busy)
3334 return ObjectBusy;
3336 status = GdipCreatePath(FillModeAlternate, &path);
3337 if (status != Ok) return status;
3339 status = GdipAddPathLine2(path, points, count);
3340 if (status == Ok)
3341 status = GdipDrawPath(graphics, pen, path);
3343 GdipDeletePath(path);
3344 return status;
3347 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3348 GpPoint *points, INT count)
3350 GpStatus retval;
3351 GpPointF *ptf;
3352 int i;
3354 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3356 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3357 if(!ptf) return OutOfMemory;
3359 for(i = 0; i < count; i ++){
3360 ptf[i].X = (REAL) points[i].X;
3361 ptf[i].Y = (REAL) points[i].Y;
3364 retval = GdipDrawLines(graphics, pen, ptf, count);
3366 heap_free(ptf);
3367 return retval;
3370 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3372 INT save_state;
3373 GpStatus retval;
3374 HRGN hrgn=NULL;
3376 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3378 if(!pen || !graphics)
3379 return InvalidParameter;
3381 if(graphics->busy)
3382 return ObjectBusy;
3384 if (!graphics->hdc)
3386 FIXME("graphics object has no HDC\n");
3387 return Ok;
3390 save_state = prepare_dc(graphics, pen);
3392 retval = get_clip_hrgn(graphics, &hrgn);
3394 if (retval != Ok)
3395 goto end;
3397 if (hrgn)
3398 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3400 retval = draw_poly(graphics, pen, path->pathdata.Points,
3401 path->pathdata.Types, path->pathdata.Count, TRUE);
3403 end:
3404 restore_dc(graphics, save_state);
3405 DeleteObject(hrgn);
3407 return retval;
3410 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3411 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3413 GpStatus status;
3414 GpPath *path;
3416 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3417 width, height, startAngle, sweepAngle);
3419 if(!graphics || !pen)
3420 return InvalidParameter;
3422 if(graphics->busy)
3423 return ObjectBusy;
3425 status = GdipCreatePath(FillModeAlternate, &path);
3426 if (status != Ok) return status;
3428 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3429 if (status == Ok)
3430 status = GdipDrawPath(graphics, pen, path);
3432 GdipDeletePath(path);
3433 return status;
3436 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3437 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3439 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3440 width, height, startAngle, sweepAngle);
3442 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3445 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3446 REAL y, REAL width, REAL height)
3448 GpStatus status;
3449 GpPath *path;
3451 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3453 if(!pen || !graphics)
3454 return InvalidParameter;
3456 if(graphics->busy)
3457 return ObjectBusy;
3459 status = GdipCreatePath(FillModeAlternate, &path);
3460 if (status != Ok) return status;
3462 status = GdipAddPathRectangle(path, x, y, width, height);
3463 if (status == Ok)
3464 status = GdipDrawPath(graphics, pen, path);
3466 GdipDeletePath(path);
3467 return status;
3470 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3471 INT y, INT width, INT height)
3473 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3475 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3478 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3479 GDIPCONST GpRectF* rects, INT count)
3481 GpStatus status;
3482 GpPath *path;
3484 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3486 if(!graphics || !pen || !rects || count < 1)
3487 return InvalidParameter;
3489 if(graphics->busy)
3490 return ObjectBusy;
3492 status = GdipCreatePath(FillModeAlternate, &path);
3493 if (status != Ok) return status;
3495 status = GdipAddPathRectangles(path, rects, count);
3496 if (status == Ok)
3497 status = GdipDrawPath(graphics, pen, path);
3499 GdipDeletePath(path);
3500 return status;
3503 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3504 GDIPCONST GpRect* rects, INT count)
3506 GpRectF *rectsF;
3507 GpStatus ret;
3508 INT i;
3510 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3512 if(!rects || count<=0)
3513 return InvalidParameter;
3515 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
3516 if(!rectsF)
3517 return OutOfMemory;
3519 for(i = 0;i < count;i++){
3520 rectsF[i].X = (REAL)rects[i].X;
3521 rectsF[i].Y = (REAL)rects[i].Y;
3522 rectsF[i].Width = (REAL)rects[i].Width;
3523 rectsF[i].Height = (REAL)rects[i].Height;
3526 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3527 heap_free(rectsF);
3529 return ret;
3532 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3533 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3535 GpPath *path;
3536 GpStatus status;
3538 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3539 count, tension, fill);
3541 if(!graphics || !brush || !points)
3542 return InvalidParameter;
3544 if(graphics->busy)
3545 return ObjectBusy;
3547 if(count == 1) /* Do nothing */
3548 return Ok;
3550 status = GdipCreatePath(fill, &path);
3551 if (status != Ok) return status;
3553 status = GdipAddPathClosedCurve2(path, points, count, tension);
3554 if (status == Ok)
3555 status = GdipFillPath(graphics, brush, path);
3557 GdipDeletePath(path);
3558 return status;
3561 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3562 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3564 GpPointF *ptf;
3565 GpStatus stat;
3566 INT i;
3568 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3569 count, tension, fill);
3571 if(!points || count == 0)
3572 return InvalidParameter;
3574 if(count == 1) /* Do nothing */
3575 return Ok;
3577 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
3578 if(!ptf)
3579 return OutOfMemory;
3581 for(i = 0;i < count;i++){
3582 ptf[i].X = (REAL)points[i].X;
3583 ptf[i].Y = (REAL)points[i].Y;
3586 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3588 heap_free(ptf);
3590 return stat;
3593 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3594 GDIPCONST GpPointF *points, INT count)
3596 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3597 return GdipFillClosedCurve2(graphics, brush, points, count,
3598 0.5f, FillModeAlternate);
3601 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3602 GDIPCONST GpPoint *points, INT count)
3604 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3605 return GdipFillClosedCurve2I(graphics, brush, points, count,
3606 0.5f, FillModeAlternate);
3609 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3610 REAL y, REAL width, REAL height)
3612 GpStatus stat;
3613 GpPath *path;
3615 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3617 if(!graphics || !brush)
3618 return InvalidParameter;
3620 if(graphics->busy)
3621 return ObjectBusy;
3623 stat = GdipCreatePath(FillModeAlternate, &path);
3625 if (stat == Ok)
3627 stat = GdipAddPathEllipse(path, x, y, width, height);
3629 if (stat == Ok)
3630 stat = GdipFillPath(graphics, brush, path);
3632 GdipDeletePath(path);
3635 return stat;
3638 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3639 INT y, INT width, INT height)
3641 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3643 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3646 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3648 INT save_state;
3649 GpStatus retval;
3650 HRGN hrgn=NULL;
3652 if(!graphics->hdc || !brush_can_fill_path(brush))
3653 return NotImplemented;
3655 save_state = SaveDC(graphics->hdc);
3656 EndPath(graphics->hdc);
3657 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3658 : WINDING));
3660 retval = get_clip_hrgn(graphics, &hrgn);
3662 if (retval != Ok)
3663 goto end;
3665 if (hrgn)
3666 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3668 BeginPath(graphics->hdc);
3669 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3670 path->pathdata.Types, path->pathdata.Count, FALSE);
3672 if(retval != Ok)
3673 goto end;
3675 EndPath(graphics->hdc);
3676 brush_fill_path(graphics, brush);
3678 retval = Ok;
3680 end:
3681 RestoreDC(graphics->hdc, save_state);
3682 DeleteObject(hrgn);
3684 return retval;
3687 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3689 GpStatus stat;
3690 GpRegion *rgn;
3692 if (!brush_can_fill_pixels(brush))
3693 return NotImplemented;
3695 /* FIXME: This could probably be done more efficiently without regions. */
3697 stat = GdipCreateRegionPath(path, &rgn);
3699 if (stat == Ok)
3701 stat = GdipFillRegion(graphics, brush, rgn);
3703 GdipDeleteRegion(rgn);
3706 return stat;
3709 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3711 GpStatus stat = NotImplemented;
3713 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3715 if(!brush || !graphics || !path)
3716 return InvalidParameter;
3718 if(graphics->busy)
3719 return ObjectBusy;
3721 if (!graphics->image && !graphics->alpha_hdc)
3722 stat = GDI32_GdipFillPath(graphics, brush, path);
3724 if (stat == NotImplemented)
3725 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3727 if (stat == NotImplemented)
3729 FIXME("Not implemented for brushtype %i\n", brush->bt);
3730 stat = Ok;
3733 return stat;
3736 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3737 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3739 GpStatus stat;
3740 GpPath *path;
3742 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3743 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3745 if(!graphics || !brush)
3746 return InvalidParameter;
3748 if(graphics->busy)
3749 return ObjectBusy;
3751 stat = GdipCreatePath(FillModeAlternate, &path);
3753 if (stat == Ok)
3755 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3757 if (stat == Ok)
3758 stat = GdipFillPath(graphics, brush, path);
3760 GdipDeletePath(path);
3763 return stat;
3766 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3767 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3769 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3770 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3772 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3775 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3776 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3778 GpStatus stat;
3779 GpPath *path;
3781 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3783 if(!graphics || !brush || !points || !count)
3784 return InvalidParameter;
3786 if(graphics->busy)
3787 return ObjectBusy;
3789 stat = GdipCreatePath(fillMode, &path);
3791 if (stat == Ok)
3793 stat = GdipAddPathPolygon(path, points, count);
3795 if (stat == Ok)
3796 stat = GdipFillPath(graphics, brush, path);
3798 GdipDeletePath(path);
3801 return stat;
3804 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3805 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3807 GpStatus stat;
3808 GpPath *path;
3810 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3812 if(!graphics || !brush || !points || !count)
3813 return InvalidParameter;
3815 if(graphics->busy)
3816 return ObjectBusy;
3818 stat = GdipCreatePath(fillMode, &path);
3820 if (stat == Ok)
3822 stat = GdipAddPathPolygonI(path, points, count);
3824 if (stat == Ok)
3825 stat = GdipFillPath(graphics, brush, path);
3827 GdipDeletePath(path);
3830 return stat;
3833 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3834 GDIPCONST GpPointF *points, INT count)
3836 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3838 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3841 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3842 GDIPCONST GpPoint *points, INT count)
3844 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3846 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3849 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3850 REAL x, REAL y, REAL width, REAL height)
3852 GpRectF rect;
3854 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3856 rect.X = x;
3857 rect.Y = y;
3858 rect.Width = width;
3859 rect.Height = height;
3861 return GdipFillRectangles(graphics, brush, &rect, 1);
3864 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3865 INT x, INT y, INT width, INT height)
3867 GpRectF rect;
3869 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3871 rect.X = (REAL)x;
3872 rect.Y = (REAL)y;
3873 rect.Width = (REAL)width;
3874 rect.Height = (REAL)height;
3876 return GdipFillRectangles(graphics, brush, &rect, 1);
3879 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3880 INT count)
3882 GpStatus status;
3883 GpPath *path;
3885 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3887 if(!graphics || !brush || !rects || count <= 0)
3888 return InvalidParameter;
3890 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3892 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
3893 /* FIXME: Add gdi32 drawing. */
3894 return status;
3897 status = GdipCreatePath(FillModeAlternate, &path);
3898 if (status != Ok) return status;
3900 status = GdipAddPathRectangles(path, rects, count);
3901 if (status == Ok)
3902 status = GdipFillPath(graphics, brush, path);
3904 GdipDeletePath(path);
3905 return status;
3908 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3909 INT count)
3911 GpRectF *rectsF;
3912 GpStatus ret;
3913 INT i;
3915 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3917 if(!rects || count <= 0)
3918 return InvalidParameter;
3920 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
3921 if(!rectsF)
3922 return OutOfMemory;
3924 for(i = 0; i < count; i++){
3925 rectsF[i].X = (REAL)rects[i].X;
3926 rectsF[i].Y = (REAL)rects[i].Y;
3927 rectsF[i].X = (REAL)rects[i].Width;
3928 rectsF[i].Height = (REAL)rects[i].Height;
3931 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3932 heap_free(rectsF);
3934 return ret;
3937 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3938 GpRegion* region)
3940 INT save_state;
3941 GpStatus status;
3942 HRGN hrgn;
3943 RECT rc;
3945 if(!graphics->hdc || !brush_can_fill_path(brush))
3946 return NotImplemented;
3948 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3949 if(status != Ok)
3950 return status;
3952 save_state = SaveDC(graphics->hdc);
3953 EndPath(graphics->hdc);
3955 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3957 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3959 BeginPath(graphics->hdc);
3960 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3961 EndPath(graphics->hdc);
3963 brush_fill_path(graphics, brush);
3966 RestoreDC(graphics->hdc, save_state);
3968 DeleteObject(hrgn);
3970 return Ok;
3973 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3974 GpRegion* region)
3976 GpStatus stat;
3977 GpRegion *temp_region;
3978 GpMatrix world_to_device;
3979 GpRectF graphics_bounds;
3980 DWORD *pixel_data;
3981 HRGN hregion;
3982 RECT bound_rect;
3983 GpRect gp_bound_rect;
3985 if (!brush_can_fill_pixels(brush))
3986 return NotImplemented;
3988 stat = get_graphics_bounds(graphics, &graphics_bounds);
3990 if (stat == Ok)
3991 stat = GdipCloneRegion(region, &temp_region);
3993 if (stat == Ok)
3995 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3996 CoordinateSpaceWorld, &world_to_device);
3998 if (stat == Ok)
3999 stat = GdipTransformRegion(temp_region, &world_to_device);
4001 if (stat == Ok)
4002 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4004 if (stat == Ok)
4005 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4007 GdipDeleteRegion(temp_region);
4010 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4012 DeleteObject(hregion);
4013 return Ok;
4016 if (stat == Ok)
4018 gp_bound_rect.X = bound_rect.left;
4019 gp_bound_rect.Y = bound_rect.top;
4020 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4021 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4023 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4024 if (!pixel_data)
4025 stat = OutOfMemory;
4027 if (stat == Ok)
4029 stat = brush_fill_pixels(graphics, brush, pixel_data,
4030 &gp_bound_rect, gp_bound_rect.Width);
4032 if (stat == Ok)
4033 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4034 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4035 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4036 PixelFormat32bppARGB);
4038 heap_free(pixel_data);
4041 DeleteObject(hregion);
4044 return stat;
4047 /*****************************************************************************
4048 * GdipFillRegion [GDIPLUS.@]
4050 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4051 GpRegion* region)
4053 GpStatus stat = NotImplemented;
4055 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4057 if (!(graphics && brush && region))
4058 return InvalidParameter;
4060 if(graphics->busy)
4061 return ObjectBusy;
4063 if (!graphics->image && !graphics->alpha_hdc)
4064 stat = GDI32_GdipFillRegion(graphics, brush, region);
4066 if (stat == NotImplemented)
4067 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4069 if (stat == NotImplemented)
4071 FIXME("not implemented for brushtype %i\n", brush->bt);
4072 stat = Ok;
4075 return stat;
4078 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4080 TRACE("(%p,%u)\n", graphics, intention);
4082 if(!graphics)
4083 return InvalidParameter;
4085 if(graphics->busy)
4086 return ObjectBusy;
4088 /* We have no internal operation queue, so there's no need to clear it. */
4090 if (graphics->hdc)
4091 GdiFlush();
4093 return Ok;
4096 /*****************************************************************************
4097 * GdipGetClipBounds [GDIPLUS.@]
4099 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4101 GpStatus status;
4102 GpRegion *clip;
4104 TRACE("(%p, %p)\n", graphics, rect);
4106 if(!graphics)
4107 return InvalidParameter;
4109 if(graphics->busy)
4110 return ObjectBusy;
4112 status = GdipCreateRegion(&clip);
4113 if (status != Ok) return status;
4115 status = GdipGetClip(graphics, clip);
4116 if (status == Ok)
4117 status = GdipGetRegionBounds(clip, graphics, rect);
4119 GdipDeleteRegion(clip);
4120 return status;
4123 /*****************************************************************************
4124 * GdipGetClipBoundsI [GDIPLUS.@]
4126 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4128 TRACE("(%p, %p)\n", graphics, rect);
4130 if(!graphics)
4131 return InvalidParameter;
4133 if(graphics->busy)
4134 return ObjectBusy;
4136 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4139 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4140 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4141 CompositingMode *mode)
4143 TRACE("(%p, %p)\n", graphics, mode);
4145 if(!graphics || !mode)
4146 return InvalidParameter;
4148 if(graphics->busy)
4149 return ObjectBusy;
4151 *mode = graphics->compmode;
4153 return Ok;
4156 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4157 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4158 CompositingQuality *quality)
4160 TRACE("(%p, %p)\n", graphics, quality);
4162 if(!graphics || !quality)
4163 return InvalidParameter;
4165 if(graphics->busy)
4166 return ObjectBusy;
4168 *quality = graphics->compqual;
4170 return Ok;
4173 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4174 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4175 InterpolationMode *mode)
4177 TRACE("(%p, %p)\n", graphics, mode);
4179 if(!graphics || !mode)
4180 return InvalidParameter;
4182 if(graphics->busy)
4183 return ObjectBusy;
4185 *mode = graphics->interpolation;
4187 return Ok;
4190 /* FIXME: Need to handle color depths less than 24bpp */
4191 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4193 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4195 if(!graphics || !argb)
4196 return InvalidParameter;
4198 if(graphics->busy)
4199 return ObjectBusy;
4201 return Ok;
4204 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4206 TRACE("(%p, %p)\n", graphics, scale);
4208 if(!graphics || !scale)
4209 return InvalidParameter;
4211 if(graphics->busy)
4212 return ObjectBusy;
4214 *scale = graphics->scale;
4216 return Ok;
4219 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4221 TRACE("(%p, %p)\n", graphics, unit);
4223 if(!graphics || !unit)
4224 return InvalidParameter;
4226 if(graphics->busy)
4227 return ObjectBusy;
4229 *unit = graphics->unit;
4231 return Ok;
4234 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4235 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4236 *mode)
4238 TRACE("(%p, %p)\n", graphics, mode);
4240 if(!graphics || !mode)
4241 return InvalidParameter;
4243 if(graphics->busy)
4244 return ObjectBusy;
4246 *mode = graphics->pixeloffset;
4248 return Ok;
4251 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4252 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4254 TRACE("(%p, %p)\n", graphics, mode);
4256 if(!graphics || !mode)
4257 return InvalidParameter;
4259 if(graphics->busy)
4260 return ObjectBusy;
4262 *mode = graphics->smoothing;
4264 return Ok;
4267 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4269 TRACE("(%p, %p)\n", graphics, contrast);
4271 if(!graphics || !contrast)
4272 return InvalidParameter;
4274 *contrast = graphics->textcontrast;
4276 return Ok;
4279 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4280 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4281 TextRenderingHint *hint)
4283 TRACE("(%p, %p)\n", graphics, hint);
4285 if(!graphics || !hint)
4286 return InvalidParameter;
4288 if(graphics->busy)
4289 return ObjectBusy;
4291 *hint = graphics->texthint;
4293 return Ok;
4296 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4298 GpRegion *clip_rgn;
4299 GpStatus stat;
4300 GpMatrix device_to_world;
4302 TRACE("(%p, %p)\n", graphics, rect);
4304 if(!graphics || !rect)
4305 return InvalidParameter;
4307 if(graphics->busy)
4308 return ObjectBusy;
4310 /* intersect window and graphics clipping regions */
4311 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4312 return stat;
4314 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4315 goto cleanup;
4317 /* transform to world coordinates */
4318 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4319 goto cleanup;
4321 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4322 goto cleanup;
4324 /* get bounds of the region */
4325 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4327 cleanup:
4328 GdipDeleteRegion(clip_rgn);
4330 return stat;
4333 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4335 GpRectF rectf;
4336 GpStatus stat;
4338 TRACE("(%p, %p)\n", graphics, rect);
4340 if(!graphics || !rect)
4341 return InvalidParameter;
4343 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4345 rect->X = gdip_round(rectf.X);
4346 rect->Y = gdip_round(rectf.Y);
4347 rect->Width = gdip_round(rectf.Width);
4348 rect->Height = gdip_round(rectf.Height);
4351 return stat;
4354 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4356 TRACE("(%p, %p)\n", graphics, matrix);
4358 if(!graphics || !matrix)
4359 return InvalidParameter;
4361 if(graphics->busy)
4362 return ObjectBusy;
4364 *matrix = graphics->worldtrans;
4365 return Ok;
4368 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4370 GpSolidFill *brush;
4371 GpStatus stat;
4372 GpRectF wnd_rect;
4374 TRACE("(%p, %x)\n", graphics, color);
4376 if(!graphics)
4377 return InvalidParameter;
4379 if(graphics->busy)
4380 return ObjectBusy;
4382 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4383 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
4385 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4386 return stat;
4388 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
4389 GdipDeleteBrush((GpBrush*)brush);
4390 return stat;
4393 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4394 wnd_rect.Width, wnd_rect.Height);
4396 GdipDeleteBrush((GpBrush*)brush);
4398 return Ok;
4401 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4403 TRACE("(%p, %p)\n", graphics, res);
4405 if(!graphics || !res)
4406 return InvalidParameter;
4408 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4411 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4413 GpStatus stat;
4414 GpRegion* rgn;
4415 GpPointF pt;
4417 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4419 if(!graphics || !result)
4420 return InvalidParameter;
4422 if(graphics->busy)
4423 return ObjectBusy;
4425 pt.X = x;
4426 pt.Y = y;
4427 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4428 CoordinateSpaceWorld, &pt, 1)) != Ok)
4429 return stat;
4431 if((stat = GdipCreateRegion(&rgn)) != Ok)
4432 return stat;
4434 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4435 goto cleanup;
4437 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4439 cleanup:
4440 GdipDeleteRegion(rgn);
4441 return stat;
4444 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4446 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4449 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4451 GpStatus stat;
4452 GpRegion* rgn;
4453 GpPointF pts[2];
4455 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4457 if(!graphics || !result)
4458 return InvalidParameter;
4460 if(graphics->busy)
4461 return ObjectBusy;
4463 pts[0].X = x;
4464 pts[0].Y = y;
4465 pts[1].X = x + width;
4466 pts[1].Y = y + height;
4468 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4469 CoordinateSpaceWorld, pts, 2)) != Ok)
4470 return stat;
4472 pts[1].X -= pts[0].X;
4473 pts[1].Y -= pts[0].Y;
4475 if((stat = GdipCreateRegion(&rgn)) != Ok)
4476 return stat;
4478 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4479 goto cleanup;
4481 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4483 cleanup:
4484 GdipDeleteRegion(rgn);
4485 return stat;
4488 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4490 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4493 GpStatus gdip_format_string(HDC hdc,
4494 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4495 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4496 gdip_format_string_callback callback, void *user_data)
4498 WCHAR* stringdup;
4499 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4500 nheight, lineend, lineno = 0;
4501 RectF bounds;
4502 StringAlignment halign;
4503 GpStatus stat = Ok;
4504 SIZE size;
4505 HotkeyPrefix hkprefix;
4506 INT *hotkeyprefix_offsets=NULL;
4507 INT hotkeyprefix_count=0;
4508 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4509 BOOL seen_prefix = FALSE;
4510 GpStringFormat *dyn_format=NULL;
4512 if(length == -1) length = lstrlenW(string);
4514 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
4515 if(!stringdup) return OutOfMemory;
4517 if (!format)
4519 stat = GdipStringFormatGetGenericDefault(&dyn_format);
4520 if (stat != Ok)
4522 heap_free(stringdup);
4523 return stat;
4525 format = dyn_format;
4528 nwidth = rect->Width;
4529 nheight = rect->Height;
4530 if (ignore_empty_clip)
4532 if (!nwidth) nwidth = INT_MAX;
4533 if (!nheight) nheight = INT_MAX;
4536 hkprefix = format->hkprefix;
4538 if (hkprefix == HotkeyPrefixShow)
4540 for (i=0; i<length; i++)
4542 if (string[i] == '&')
4543 hotkeyprefix_count++;
4547 if (hotkeyprefix_count)
4548 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
4550 hotkeyprefix_count = 0;
4552 for(i = 0, j = 0; i < length; i++){
4553 /* FIXME: This makes the indexes passed to callback inaccurate. */
4554 if(!isprintW(string[i]) && (string[i] != '\n'))
4555 continue;
4557 /* FIXME: tabs should be handled using tabstops from stringformat */
4558 if (string[i] == '\t')
4559 continue;
4561 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4562 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4563 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4565 seen_prefix = TRUE;
4566 continue;
4569 seen_prefix = FALSE;
4571 stringdup[j] = string[i];
4572 j++;
4575 length = j;
4577 halign = format->align;
4579 while(sum < length){
4580 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4581 nwidth, &fit, NULL, &size);
4582 fitcpy = fit;
4584 if(fit == 0)
4585 break;
4587 for(lret = 0; lret < fit; lret++)
4588 if(*(stringdup + sum + lret) == '\n')
4589 break;
4591 /* Line break code (may look strange, but it imitates windows). */
4592 if(lret < fit)
4593 lineend = fit = lret; /* this is not an off-by-one error */
4594 else if(fit < (length - sum)){
4595 if(*(stringdup + sum + fit) == ' ')
4596 while(*(stringdup + sum + fit) == ' ')
4597 fit++;
4598 else
4599 while(*(stringdup + sum + fit - 1) != ' '){
4600 fit--;
4602 if(*(stringdup + sum + fit) == '\t')
4603 break;
4605 if(fit == 0){
4606 fit = fitcpy;
4607 break;
4610 lineend = fit;
4611 while(*(stringdup + sum + lineend - 1) == ' ' ||
4612 *(stringdup + sum + lineend - 1) == '\t')
4613 lineend--;
4615 else
4616 lineend = fit;
4618 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4619 nwidth, &j, NULL, &size);
4621 bounds.Width = size.cx;
4623 if(height + size.cy > nheight)
4625 if (format->attr & StringFormatFlagsLineLimit)
4626 break;
4627 bounds.Height = nheight - (height + size.cy);
4629 else
4630 bounds.Height = size.cy;
4632 bounds.Y = rect->Y + height;
4634 switch (halign)
4636 case StringAlignmentNear:
4637 default:
4638 bounds.X = rect->X;
4639 break;
4640 case StringAlignmentCenter:
4641 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4642 break;
4643 case StringAlignmentFar:
4644 bounds.X = rect->X + rect->Width - bounds.Width;
4645 break;
4648 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4649 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4650 break;
4652 stat = callback(hdc, stringdup, sum, lineend,
4653 font, rect, format, lineno, &bounds,
4654 &hotkeyprefix_offsets[hotkeyprefix_pos],
4655 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4657 if (stat != Ok)
4658 break;
4660 sum += fit + (lret < fitcpy ? 1 : 0);
4661 height += size.cy;
4662 lineno++;
4664 hotkeyprefix_pos = hotkeyprefix_end_pos;
4666 if(height > nheight)
4667 break;
4669 /* Stop if this was a linewrap (but not if it was a linebreak). */
4670 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
4671 break;
4674 heap_free(stringdup);
4675 heap_free(hotkeyprefix_offsets);
4676 GdipDeleteStringFormat(dyn_format);
4678 return stat;
4681 struct measure_ranges_args {
4682 GpRegion **regions;
4683 REAL rel_width, rel_height;
4686 static GpStatus measure_ranges_callback(HDC hdc,
4687 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4688 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4689 INT lineno, const RectF *bounds, INT *underlined_indexes,
4690 INT underlined_index_count, void *user_data)
4692 int i;
4693 GpStatus stat = Ok;
4694 struct measure_ranges_args *args = user_data;
4696 for (i=0; i<format->range_count; i++)
4698 INT range_start = max(index, format->character_ranges[i].First);
4699 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4700 if (range_start < range_end)
4702 GpRectF range_rect;
4703 SIZE range_size;
4705 range_rect.Y = bounds->Y / args->rel_height;
4706 range_rect.Height = bounds->Height / args->rel_height;
4708 GetTextExtentExPointW(hdc, string + index, range_start - index,
4709 INT_MAX, NULL, NULL, &range_size);
4710 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4712 GetTextExtentExPointW(hdc, string + index, range_end - index,
4713 INT_MAX, NULL, NULL, &range_size);
4714 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4716 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4717 if (stat != Ok)
4718 break;
4722 return stat;
4725 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4726 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4727 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4728 INT regionCount, GpRegion** regions)
4730 GpStatus stat;
4731 int i;
4732 HFONT gdifont, oldfont;
4733 struct measure_ranges_args args;
4734 HDC hdc, temp_hdc=NULL;
4735 GpPointF pt[3];
4736 RectF scaled_rect;
4737 REAL margin_x;
4739 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4740 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4742 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4743 return InvalidParameter;
4745 if (regionCount < stringFormat->range_count)
4746 return InvalidParameter;
4748 if(!graphics->hdc)
4750 hdc = temp_hdc = CreateCompatibleDC(0);
4751 if (!temp_hdc) return OutOfMemory;
4753 else
4754 hdc = graphics->hdc;
4756 if (stringFormat->attr)
4757 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4759 pt[0].X = 0.0;
4760 pt[0].Y = 0.0;
4761 pt[1].X = 1.0;
4762 pt[1].Y = 0.0;
4763 pt[2].X = 0.0;
4764 pt[2].Y = 1.0;
4765 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4766 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4767 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4768 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4769 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4771 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4772 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4774 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4775 scaled_rect.Y = layoutRect->Y * args.rel_height;
4776 scaled_rect.Width = layoutRect->Width * args.rel_width;
4777 scaled_rect.Height = layoutRect->Height * args.rel_height;
4779 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4780 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4782 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4783 oldfont = SelectObject(hdc, gdifont);
4785 for (i=0; i<stringFormat->range_count; i++)
4787 stat = GdipSetEmpty(regions[i]);
4788 if (stat != Ok)
4789 return stat;
4792 args.regions = regions;
4794 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4795 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4797 SelectObject(hdc, oldfont);
4798 DeleteObject(gdifont);
4800 if (temp_hdc)
4801 DeleteDC(temp_hdc);
4803 return stat;
4806 struct measure_string_args {
4807 RectF *bounds;
4808 INT *codepointsfitted;
4809 INT *linesfilled;
4810 REAL rel_width, rel_height;
4813 static GpStatus measure_string_callback(HDC hdc,
4814 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4815 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4816 INT lineno, const RectF *bounds, INT *underlined_indexes,
4817 INT underlined_index_count, void *user_data)
4819 struct measure_string_args *args = user_data;
4820 REAL new_width, new_height;
4822 new_width = bounds->Width / args->rel_width;
4823 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4825 if (new_width > args->bounds->Width)
4826 args->bounds->Width = new_width;
4828 if (new_height > args->bounds->Height)
4829 args->bounds->Height = new_height;
4831 if (args->codepointsfitted)
4832 *args->codepointsfitted = index + length;
4834 if (args->linesfilled)
4835 (*args->linesfilled)++;
4837 return Ok;
4840 /* Find the smallest rectangle that bounds the text when it is printed in rect
4841 * according to the format options listed in format. If rect has 0 width and
4842 * height, then just find the smallest rectangle that bounds the text when it's
4843 * printed at location (rect->X, rect-Y). */
4844 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4845 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4846 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4847 INT *codepointsfitted, INT *linesfilled)
4849 HFONT oldfont, gdifont;
4850 struct measure_string_args args;
4851 HDC temp_hdc=NULL, hdc;
4852 GpPointF pt[3];
4853 RectF scaled_rect;
4854 REAL margin_x;
4855 INT lines, glyphs;
4857 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4858 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4859 bounds, codepointsfitted, linesfilled);
4861 if(!graphics || !string || !font || !rect || !bounds)
4862 return InvalidParameter;
4864 if(!graphics->hdc)
4866 hdc = temp_hdc = CreateCompatibleDC(0);
4867 if (!temp_hdc) return OutOfMemory;
4869 else
4870 hdc = graphics->hdc;
4872 if(linesfilled) *linesfilled = 0;
4873 if(codepointsfitted) *codepointsfitted = 0;
4875 if(format)
4876 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4878 pt[0].X = 0.0;
4879 pt[0].Y = 0.0;
4880 pt[1].X = 1.0;
4881 pt[1].Y = 0.0;
4882 pt[2].X = 0.0;
4883 pt[2].Y = 1.0;
4884 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4885 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4886 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4887 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4888 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4890 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4891 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4893 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4894 scaled_rect.Y = rect->Y * args.rel_height;
4895 scaled_rect.Width = rect->Width * args.rel_width;
4896 scaled_rect.Height = rect->Height * args.rel_height;
4897 if (scaled_rect.Width >= 0.5)
4899 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4900 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4903 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4904 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4906 get_font_hfont(graphics, font, format, &gdifont, NULL);
4907 oldfont = SelectObject(hdc, gdifont);
4909 bounds->X = rect->X;
4910 bounds->Y = rect->Y;
4911 bounds->Width = 0.0;
4912 bounds->Height = 0.0;
4914 args.bounds = bounds;
4915 args.codepointsfitted = &glyphs;
4916 args.linesfilled = &lines;
4917 lines = glyphs = 0;
4919 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4920 measure_string_callback, &args);
4922 if (linesfilled) *linesfilled = lines;
4923 if (codepointsfitted) *codepointsfitted = glyphs;
4925 if (lines)
4926 bounds->Width += margin_x * 2.0;
4928 SelectObject(hdc, oldfont);
4929 DeleteObject(gdifont);
4931 if (temp_hdc)
4932 DeleteDC(temp_hdc);
4934 return Ok;
4937 struct draw_string_args {
4938 GpGraphics *graphics;
4939 GDIPCONST GpBrush *brush;
4940 REAL x, y, rel_width, rel_height, ascent;
4943 static GpStatus draw_string_callback(HDC hdc,
4944 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4945 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4946 INT lineno, const RectF *bounds, INT *underlined_indexes,
4947 INT underlined_index_count, void *user_data)
4949 struct draw_string_args *args = user_data;
4950 PointF position;
4951 GpStatus stat;
4953 position.X = args->x + bounds->X / args->rel_width;
4954 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4956 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4957 args->brush, &position,
4958 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4960 if (stat == Ok && underlined_index_count)
4962 OUTLINETEXTMETRICW otm;
4963 REAL underline_y, underline_height;
4964 int i;
4966 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4968 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4969 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4971 for (i=0; i<underlined_index_count; i++)
4973 REAL start_x, end_x;
4974 SIZE text_size;
4975 INT ofs = underlined_indexes[i] - index;
4977 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4978 start_x = text_size.cx / args->rel_width;
4980 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4981 end_x = text_size.cx / args->rel_width;
4983 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4987 return stat;
4990 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4991 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4992 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4994 HRGN rgn = NULL;
4995 HFONT gdifont;
4996 GpPointF pt[3], rectcpy[4];
4997 POINT corners[4];
4998 REAL rel_width, rel_height, margin_x;
4999 INT save_state, format_flags = 0;
5000 REAL offsety = 0.0;
5001 struct draw_string_args args;
5002 RectF scaled_rect;
5003 HDC hdc, temp_hdc=NULL;
5004 TEXTMETRICW textmetric;
5006 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5007 length, font, debugstr_rectf(rect), format, brush);
5009 if(!graphics || !string || !font || !brush || !rect)
5010 return InvalidParameter;
5012 if(graphics->hdc)
5014 hdc = graphics->hdc;
5016 else
5018 hdc = temp_hdc = CreateCompatibleDC(0);
5021 if(format){
5022 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5024 format_flags = format->attr;
5026 /* Should be no need to explicitly test for StringAlignmentNear as
5027 * that is default behavior if no alignment is passed. */
5028 if(format->vertalign != StringAlignmentNear){
5029 RectF bounds, in_rect = *rect;
5030 in_rect.Height = 0.0; /* avoid height clipping */
5031 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5033 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5035 if(format->vertalign == StringAlignmentCenter)
5036 offsety = (rect->Height - bounds.Height) / 2;
5037 else if(format->vertalign == StringAlignmentFar)
5038 offsety = (rect->Height - bounds.Height);
5040 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5043 save_state = SaveDC(hdc);
5045 pt[0].X = 0.0;
5046 pt[0].Y = 0.0;
5047 pt[1].X = 1.0;
5048 pt[1].Y = 0.0;
5049 pt[2].X = 0.0;
5050 pt[2].Y = 1.0;
5051 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5052 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5053 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5054 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5055 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5057 rectcpy[3].X = rectcpy[0].X = rect->X;
5058 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5059 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5060 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5061 transform_and_round_points(graphics, corners, rectcpy, 4);
5063 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5064 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5066 scaled_rect.X = margin_x * rel_width;
5067 scaled_rect.Y = 0.0;
5068 scaled_rect.Width = rel_width * rect->Width;
5069 scaled_rect.Height = rel_height * rect->Height;
5070 if (scaled_rect.Width >= 0.5)
5072 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5073 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5076 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5077 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5079 if (!(format_flags & StringFormatFlagsNoClip) &&
5080 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5081 rect->Width > 0.0 && rect->Height > 0.0)
5083 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5084 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5085 SelectClipRgn(hdc, rgn);
5088 get_font_hfont(graphics, font, format, &gdifont, NULL);
5089 SelectObject(hdc, gdifont);
5091 args.graphics = graphics;
5092 args.brush = brush;
5094 args.x = rect->X;
5095 args.y = rect->Y + offsety;
5097 args.rel_width = rel_width;
5098 args.rel_height = rel_height;
5100 GetTextMetricsW(hdc, &textmetric);
5101 args.ascent = textmetric.tmAscent / rel_height;
5103 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5104 draw_string_callback, &args);
5106 DeleteObject(rgn);
5107 DeleteObject(gdifont);
5109 RestoreDC(hdc, save_state);
5111 DeleteDC(temp_hdc);
5113 return Ok;
5116 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5118 TRACE("(%p)\n", graphics);
5120 if(!graphics)
5121 return InvalidParameter;
5123 if(graphics->busy)
5124 return ObjectBusy;
5126 return GdipSetInfinite(graphics->clip);
5129 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5131 GpStatus stat;
5133 TRACE("(%p)\n", graphics);
5135 if(!graphics)
5136 return InvalidParameter;
5138 if(graphics->busy)
5139 return ObjectBusy;
5141 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5142 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5144 if (stat != Ok)
5145 return stat;
5148 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5151 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5153 return GdipEndContainer(graphics, state);
5156 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5157 GpMatrixOrder order)
5159 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5161 if(!graphics)
5162 return InvalidParameter;
5164 if(graphics->busy)
5165 return ObjectBusy;
5167 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5170 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5172 return GdipBeginContainer2(graphics, state);
5175 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5176 GraphicsContainer *state)
5178 GraphicsContainerItem *container;
5179 GpStatus sts;
5181 TRACE("(%p, %p)\n", graphics, state);
5183 if(!graphics || !state)
5184 return InvalidParameter;
5186 sts = init_container(&container, graphics);
5187 if(sts != Ok)
5188 return sts;
5190 list_add_head(&graphics->containers, &container->entry);
5191 *state = graphics->contid = container->contid;
5193 return Ok;
5196 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5198 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5199 return NotImplemented;
5202 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5204 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5205 return NotImplemented;
5208 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5210 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5211 return NotImplemented;
5214 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5216 GpStatus sts;
5217 GraphicsContainerItem *container, *container2;
5219 TRACE("(%p, %x)\n", graphics, state);
5221 if(!graphics)
5222 return InvalidParameter;
5224 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5225 if(container->contid == state)
5226 break;
5229 /* did not find a matching container */
5230 if(&container->entry == &graphics->containers)
5231 return Ok;
5233 sts = restore_container(graphics, container);
5234 if(sts != Ok)
5235 return sts;
5237 /* remove all of the containers on top of the found container */
5238 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5239 if(container->contid == state)
5240 break;
5241 list_remove(&container->entry);
5242 delete_container(container);
5245 list_remove(&container->entry);
5246 delete_container(container);
5248 return Ok;
5251 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5252 REAL sy, GpMatrixOrder order)
5254 GpStatus stat;
5256 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5258 if(!graphics)
5259 return InvalidParameter;
5261 if(graphics->busy)
5262 return ObjectBusy;
5264 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5265 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
5267 if (stat != Ok)
5268 return stat;
5271 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5274 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5275 CombineMode mode)
5277 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5279 if(!graphics || !srcgraphics)
5280 return InvalidParameter;
5282 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5285 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5286 CompositingMode mode)
5288 TRACE("(%p, %d)\n", graphics, mode);
5290 if(!graphics)
5291 return InvalidParameter;
5293 if(graphics->busy)
5294 return ObjectBusy;
5296 graphics->compmode = mode;
5298 return Ok;
5301 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5302 CompositingQuality quality)
5304 TRACE("(%p, %d)\n", graphics, quality);
5306 if(!graphics)
5307 return InvalidParameter;
5309 if(graphics->busy)
5310 return ObjectBusy;
5312 graphics->compqual = quality;
5314 return Ok;
5317 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5318 InterpolationMode mode)
5320 TRACE("(%p, %d)\n", graphics, mode);
5322 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5323 return InvalidParameter;
5325 if(graphics->busy)
5326 return ObjectBusy;
5328 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5329 mode = InterpolationModeBilinear;
5331 if (mode == InterpolationModeHighQuality)
5332 mode = InterpolationModeHighQualityBicubic;
5334 graphics->interpolation = mode;
5336 return Ok;
5339 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5341 GpStatus stat;
5343 TRACE("(%p, %.2f)\n", graphics, scale);
5345 if(!graphics || (scale <= 0.0))
5346 return InvalidParameter;
5348 if(graphics->busy)
5349 return ObjectBusy;
5351 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5353 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5354 if (stat != Ok)
5355 return stat;
5358 graphics->scale = scale;
5360 return Ok;
5363 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5365 GpStatus stat;
5367 TRACE("(%p, %d)\n", graphics, unit);
5369 if(!graphics)
5370 return InvalidParameter;
5372 if(graphics->busy)
5373 return ObjectBusy;
5375 if(unit == UnitWorld)
5376 return InvalidParameter;
5378 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5380 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5381 if (stat != Ok)
5382 return stat;
5385 graphics->unit = unit;
5387 return Ok;
5390 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5391 mode)
5393 TRACE("(%p, %d)\n", graphics, mode);
5395 if(!graphics)
5396 return InvalidParameter;
5398 if(graphics->busy)
5399 return ObjectBusy;
5401 graphics->pixeloffset = mode;
5403 return Ok;
5406 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5408 static int calls;
5410 TRACE("(%p,%i,%i)\n", graphics, x, y);
5412 if (!(calls++))
5413 FIXME("value is unused in rendering\n");
5415 if (!graphics)
5416 return InvalidParameter;
5418 graphics->origin_x = x;
5419 graphics->origin_y = y;
5421 return Ok;
5424 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5426 TRACE("(%p,%p,%p)\n", graphics, x, y);
5428 if (!graphics || !x || !y)
5429 return InvalidParameter;
5431 *x = graphics->origin_x;
5432 *y = graphics->origin_y;
5434 return Ok;
5437 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5439 TRACE("(%p, %d)\n", graphics, mode);
5441 if(!graphics)
5442 return InvalidParameter;
5444 if(graphics->busy)
5445 return ObjectBusy;
5447 graphics->smoothing = mode;
5449 return Ok;
5452 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5454 TRACE("(%p, %d)\n", graphics, contrast);
5456 if(!graphics)
5457 return InvalidParameter;
5459 graphics->textcontrast = contrast;
5461 return Ok;
5464 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5465 TextRenderingHint hint)
5467 TRACE("(%p, %d)\n", graphics, hint);
5469 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5470 return InvalidParameter;
5472 if(graphics->busy)
5473 return ObjectBusy;
5475 graphics->texthint = hint;
5477 return Ok;
5480 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5482 TRACE("(%p, %p)\n", graphics, matrix);
5484 if(!graphics || !matrix)
5485 return InvalidParameter;
5487 if(graphics->busy)
5488 return ObjectBusy;
5490 TRACE("%f,%f,%f,%f,%f,%f\n",
5491 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5492 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5494 graphics->worldtrans = *matrix;
5496 return Ok;
5499 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5500 REAL dy, GpMatrixOrder order)
5502 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5504 if(!graphics)
5505 return InvalidParameter;
5507 if(graphics->busy)
5508 return ObjectBusy;
5510 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5513 /*****************************************************************************
5514 * GdipSetClipHrgn [GDIPLUS.@]
5516 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5518 GpRegion *region;
5519 GpStatus status;
5521 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5523 if(!graphics)
5524 return InvalidParameter;
5526 if(graphics->busy)
5527 return ObjectBusy;
5529 /* hrgn is already in device units */
5530 status = GdipCreateRegionHrgn(hrgn, &region);
5531 if(status != Ok)
5532 return status;
5534 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5536 GdipDeleteRegion(region);
5537 return status;
5540 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5542 GpStatus status;
5543 GpPath *clip_path;
5545 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5547 if(!graphics)
5548 return InvalidParameter;
5550 if(graphics->busy)
5551 return ObjectBusy;
5553 status = GdipClonePath(path, &clip_path);
5554 if (status == Ok)
5556 GpMatrix world_to_device;
5558 get_graphics_transform(graphics, CoordinateSpaceDevice,
5559 CoordinateSpaceWorld, &world_to_device);
5560 status = GdipTransformPath(clip_path, &world_to_device);
5561 if (status == Ok)
5562 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5564 GdipDeletePath(clip_path);
5566 return status;
5569 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5570 REAL width, REAL height,
5571 CombineMode mode)
5573 GpStatus status;
5574 GpRectF rect;
5575 GpRegion *region;
5577 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5579 if(!graphics)
5580 return InvalidParameter;
5582 if(graphics->busy)
5583 return ObjectBusy;
5585 rect.X = x;
5586 rect.Y = y;
5587 rect.Width = width;
5588 rect.Height = height;
5589 status = GdipCreateRegionRect(&rect, &region);
5590 if (status == Ok)
5592 GpMatrix world_to_device;
5594 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5595 status = GdipTransformRegion(region, &world_to_device);
5596 if (status == Ok)
5597 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5599 GdipDeleteRegion(region);
5601 return status;
5604 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5605 INT width, INT height,
5606 CombineMode mode)
5608 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5610 if(!graphics)
5611 return InvalidParameter;
5613 if(graphics->busy)
5614 return ObjectBusy;
5616 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5619 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5620 CombineMode mode)
5622 GpStatus status;
5623 GpRegion *clip;
5625 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5627 if(!graphics || !region)
5628 return InvalidParameter;
5630 if(graphics->busy)
5631 return ObjectBusy;
5633 status = GdipCloneRegion(region, &clip);
5634 if (status == Ok)
5636 GpMatrix world_to_device;
5638 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5639 status = GdipTransformRegion(clip, &world_to_device);
5640 if (status == Ok)
5641 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5643 GdipDeleteRegion(clip);
5645 return status;
5648 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5649 INT count)
5651 INT save_state;
5652 POINT *pti;
5654 TRACE("(%p, %p, %d)\n", graphics, points, count);
5656 if(!graphics || !pen || count<=0)
5657 return InvalidParameter;
5659 if(graphics->busy)
5660 return ObjectBusy;
5662 if (!graphics->hdc)
5664 FIXME("graphics object has no HDC\n");
5665 return Ok;
5668 pti = heap_alloc_zero(sizeof(POINT) * count);
5670 save_state = prepare_dc(graphics, pen);
5671 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5673 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5674 Polygon(graphics->hdc, pti, count);
5676 restore_dc(graphics, save_state);
5677 heap_free(pti);
5679 return Ok;
5682 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5683 INT count)
5685 GpStatus ret;
5686 GpPointF *ptf;
5687 INT i;
5689 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5691 if(count<=0) return InvalidParameter;
5692 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
5694 for(i = 0;i < count; i++){
5695 ptf[i].X = (REAL)points[i].X;
5696 ptf[i].Y = (REAL)points[i].Y;
5699 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5700 heap_free(ptf);
5702 return ret;
5705 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5707 TRACE("(%p, %p)\n", graphics, dpi);
5709 if(!graphics || !dpi)
5710 return InvalidParameter;
5712 if(graphics->busy)
5713 return ObjectBusy;
5715 *dpi = graphics->xres;
5716 return Ok;
5719 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5721 TRACE("(%p, %p)\n", graphics, dpi);
5723 if(!graphics || !dpi)
5724 return InvalidParameter;
5726 if(graphics->busy)
5727 return ObjectBusy;
5729 *dpi = graphics->yres;
5730 return Ok;
5733 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5734 GpMatrixOrder order)
5736 GpMatrix m;
5737 GpStatus ret;
5739 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5741 if(!graphics || !matrix)
5742 return InvalidParameter;
5744 if(graphics->busy)
5745 return ObjectBusy;
5747 m = graphics->worldtrans;
5749 ret = GdipMultiplyMatrix(&m, matrix, order);
5750 if(ret == Ok)
5751 graphics->worldtrans = m;
5753 return ret;
5756 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5757 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5759 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5761 GpStatus stat=Ok;
5763 TRACE("(%p, %p)\n", graphics, hdc);
5765 if(!graphics || !hdc)
5766 return InvalidParameter;
5768 if(graphics->busy)
5769 return ObjectBusy;
5771 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5773 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5775 else if (!graphics->hdc ||
5776 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5778 /* Create a fake HDC and fill it with a constant color. */
5779 HDC temp_hdc;
5780 HBITMAP hbitmap;
5781 GpRectF bounds;
5782 BITMAPINFOHEADER bmih;
5783 int i;
5785 stat = get_graphics_bounds(graphics, &bounds);
5786 if (stat != Ok)
5787 return stat;
5789 graphics->temp_hbitmap_width = bounds.Width;
5790 graphics->temp_hbitmap_height = bounds.Height;
5792 bmih.biSize = sizeof(bmih);
5793 bmih.biWidth = graphics->temp_hbitmap_width;
5794 bmih.biHeight = -graphics->temp_hbitmap_height;
5795 bmih.biPlanes = 1;
5796 bmih.biBitCount = 32;
5797 bmih.biCompression = BI_RGB;
5798 bmih.biSizeImage = 0;
5799 bmih.biXPelsPerMeter = 0;
5800 bmih.biYPelsPerMeter = 0;
5801 bmih.biClrUsed = 0;
5802 bmih.biClrImportant = 0;
5804 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5805 (void**)&graphics->temp_bits, NULL, 0);
5806 if (!hbitmap)
5807 return GenericError;
5809 temp_hdc = CreateCompatibleDC(0);
5810 if (!temp_hdc)
5812 DeleteObject(hbitmap);
5813 return GenericError;
5816 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5817 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5819 SelectObject(temp_hdc, hbitmap);
5821 graphics->temp_hbitmap = hbitmap;
5822 *hdc = graphics->temp_hdc = temp_hdc;
5824 else
5826 *hdc = graphics->hdc;
5829 if (stat == Ok)
5830 graphics->busy = TRUE;
5832 return stat;
5835 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5837 GpStatus stat=Ok;
5839 TRACE("(%p, %p)\n", graphics, hdc);
5841 if(!graphics || !hdc || !graphics->busy)
5842 return InvalidParameter;
5844 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5846 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5848 else if (graphics->temp_hdc == hdc)
5850 DWORD* pos;
5851 int i;
5853 /* Find the pixels that have changed, and mark them as opaque. */
5854 pos = (DWORD*)graphics->temp_bits;
5855 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5857 if (*pos != DC_BACKGROUND_KEY)
5859 *pos |= 0xff000000;
5861 pos++;
5864 /* Write the changed pixels to the real target. */
5865 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5866 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5867 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
5869 /* Clean up. */
5870 DeleteDC(graphics->temp_hdc);
5871 DeleteObject(graphics->temp_hbitmap);
5872 graphics->temp_hdc = NULL;
5873 graphics->temp_hbitmap = NULL;
5875 else if (hdc != graphics->hdc)
5877 stat = InvalidParameter;
5880 if (stat == Ok)
5881 graphics->busy = FALSE;
5883 return stat;
5886 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5888 GpRegion *clip;
5889 GpStatus status;
5890 GpMatrix device_to_world;
5892 TRACE("(%p, %p)\n", graphics, region);
5894 if(!graphics || !region)
5895 return InvalidParameter;
5897 if(graphics->busy)
5898 return ObjectBusy;
5900 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5901 return status;
5903 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5904 status = GdipTransformRegion(clip, &device_to_world);
5905 if (status != Ok)
5907 GdipDeleteRegion(clip);
5908 return status;
5911 /* free everything except root node and header */
5912 delete_element(&region->node);
5913 memcpy(region, clip, sizeof(GpRegion));
5914 heap_free(clip);
5916 return Ok;
5919 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5920 GpCoordinateSpace src_space, GpMatrix *matrix)
5922 GpStatus stat = Ok;
5923 REAL scale_x, scale_y;
5925 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5927 if (dst_space != src_space)
5929 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5930 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5932 if(graphics->unit != UnitDisplay)
5934 scale_x *= graphics->scale;
5935 scale_y *= graphics->scale;
5938 /* transform from src_space to CoordinateSpacePage */
5939 switch (src_space)
5941 case CoordinateSpaceWorld:
5942 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5943 break;
5944 case CoordinateSpacePage:
5945 break;
5946 case CoordinateSpaceDevice:
5947 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5948 break;
5951 /* transform from CoordinateSpacePage to dst_space */
5952 switch (dst_space)
5954 case CoordinateSpaceWorld:
5956 GpMatrix inverted_transform = graphics->worldtrans;
5957 stat = GdipInvertMatrix(&inverted_transform);
5958 if (stat == Ok)
5959 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5960 break;
5962 case CoordinateSpacePage:
5963 break;
5964 case CoordinateSpaceDevice:
5965 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5966 break;
5969 return stat;
5972 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5973 GpCoordinateSpace src_space, GpPointF *points, INT count)
5975 GpMatrix matrix;
5976 GpStatus stat;
5978 if(!graphics || !points || count <= 0)
5979 return InvalidParameter;
5981 if(graphics->busy)
5982 return ObjectBusy;
5984 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5986 if (src_space == dst_space) return Ok;
5988 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5989 if (stat != Ok) return stat;
5991 return GdipTransformMatrixPoints(&matrix, points, count);
5994 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5995 GpCoordinateSpace src_space, GpPoint *points, INT count)
5997 GpPointF *pointsF;
5998 GpStatus ret;
5999 INT i;
6001 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6003 if(count <= 0)
6004 return InvalidParameter;
6006 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6007 if(!pointsF)
6008 return OutOfMemory;
6010 for(i = 0; i < count; i++){
6011 pointsF[i].X = (REAL)points[i].X;
6012 pointsF[i].Y = (REAL)points[i].Y;
6015 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6017 if(ret == Ok)
6018 for(i = 0; i < count; i++){
6019 points[i].X = gdip_round(pointsF[i].X);
6020 points[i].Y = gdip_round(pointsF[i].Y);
6022 heap_free(pointsF);
6024 return ret;
6027 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6029 static int calls;
6031 TRACE("\n");
6033 if (!calls++)
6034 FIXME("stub\n");
6036 return NULL;
6039 /*****************************************************************************
6040 * GdipTranslateClip [GDIPLUS.@]
6042 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6044 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6046 if(!graphics)
6047 return InvalidParameter;
6049 if(graphics->busy)
6050 return ObjectBusy;
6052 return GdipTranslateRegion(graphics->clip, dx, dy);
6055 /*****************************************************************************
6056 * GdipTranslateClipI [GDIPLUS.@]
6058 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6060 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6062 if(!graphics)
6063 return InvalidParameter;
6065 if(graphics->busy)
6066 return ObjectBusy;
6068 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6072 /*****************************************************************************
6073 * GdipMeasureDriverString [GDIPLUS.@]
6075 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6076 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6077 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6079 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6080 HFONT hfont;
6081 HDC hdc;
6082 REAL min_x, min_y, max_x, max_y, x, y;
6083 int i;
6084 TEXTMETRICW textmetric;
6085 const WORD *glyph_indices;
6086 WORD *dynamic_glyph_indices=NULL;
6087 REAL rel_width, rel_height, ascent, descent;
6088 GpPointF pt[3];
6090 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6092 if (!graphics || !text || !font || !positions || !boundingBox)
6093 return InvalidParameter;
6095 if (length == -1)
6096 length = strlenW(text);
6098 if (length == 0)
6100 boundingBox->X = 0.0;
6101 boundingBox->Y = 0.0;
6102 boundingBox->Width = 0.0;
6103 boundingBox->Height = 0.0;
6106 if (flags & unsupported_flags)
6107 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6109 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6111 hdc = CreateCompatibleDC(0);
6112 SelectObject(hdc, hfont);
6114 GetTextMetricsW(hdc, &textmetric);
6116 pt[0].X = 0.0;
6117 pt[0].Y = 0.0;
6118 pt[1].X = 1.0;
6119 pt[1].Y = 0.0;
6120 pt[2].X = 0.0;
6121 pt[2].Y = 1.0;
6122 if (matrix)
6124 GpMatrix xform = *matrix;
6125 GdipTransformMatrixPoints(&xform, pt, 3);
6127 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6128 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6129 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6130 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6131 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6133 if (flags & DriverStringOptionsCmapLookup)
6135 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
6136 if (!glyph_indices)
6138 DeleteDC(hdc);
6139 DeleteObject(hfont);
6140 return OutOfMemory;
6143 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6145 else
6146 glyph_indices = text;
6148 min_x = max_x = x = positions[0].X;
6149 min_y = max_y = y = positions[0].Y;
6151 ascent = textmetric.tmAscent / rel_height;
6152 descent = textmetric.tmDescent / rel_height;
6154 for (i=0; i<length; i++)
6156 int char_width;
6157 ABC abc;
6159 if (!(flags & DriverStringOptionsRealizedAdvance))
6161 x = positions[i].X;
6162 y = positions[i].Y;
6165 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6166 char_width = abc.abcA + abc.abcB + abc.abcC;
6168 if (min_y > y - ascent) min_y = y - ascent;
6169 if (max_y < y + descent) max_y = y + descent;
6170 if (min_x > x) min_x = x;
6172 x += char_width / rel_width;
6174 if (max_x < x) max_x = x;
6177 heap_free(dynamic_glyph_indices);
6178 DeleteDC(hdc);
6179 DeleteObject(hfont);
6181 boundingBox->X = min_x;
6182 boundingBox->Y = min_y;
6183 boundingBox->Width = max_x - min_x;
6184 boundingBox->Height = max_y - min_y;
6186 return Ok;
6189 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6190 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6191 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6192 INT flags, GDIPCONST GpMatrix *matrix)
6194 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6195 INT save_state;
6196 GpPointF pt;
6197 HFONT hfont;
6198 UINT eto_flags=0;
6200 if (flags & unsupported_flags)
6201 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6203 if (!(flags & DriverStringOptionsCmapLookup))
6204 eto_flags |= ETO_GLYPH_INDEX;
6206 save_state = SaveDC(graphics->hdc);
6207 SetBkMode(graphics->hdc, TRANSPARENT);
6208 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6210 pt = positions[0];
6211 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6213 get_font_hfont(graphics, font, format, &hfont, matrix);
6214 SelectObject(graphics->hdc, hfont);
6216 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6218 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6220 RestoreDC(graphics->hdc, save_state);
6222 DeleteObject(hfont);
6224 return Ok;
6227 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6228 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6229 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6230 INT flags, GDIPCONST GpMatrix *matrix)
6232 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6233 GpStatus stat;
6234 PointF *real_positions, real_position;
6235 POINT *pti;
6236 HFONT hfont;
6237 HDC hdc;
6238 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6239 DWORD max_glyphsize=0;
6240 GLYPHMETRICS glyphmetrics;
6241 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6242 BYTE *glyph_mask;
6243 BYTE *text_mask;
6244 int text_mask_stride;
6245 BYTE *pixel_data;
6246 int pixel_data_stride;
6247 GpRect pixel_area;
6248 UINT ggo_flags = GGO_GRAY8_BITMAP;
6250 if (length <= 0)
6251 return Ok;
6253 if (!(flags & DriverStringOptionsCmapLookup))
6254 ggo_flags |= GGO_GLYPH_INDEX;
6256 if (flags & unsupported_flags)
6257 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6259 pti = heap_alloc_zero(sizeof(POINT) * length);
6260 if (!pti)
6261 return OutOfMemory;
6263 if (flags & DriverStringOptionsRealizedAdvance)
6265 real_position = positions[0];
6267 transform_and_round_points(graphics, pti, &real_position, 1);
6269 else
6271 real_positions = heap_alloc_zero(sizeof(PointF) * length);
6272 if (!real_positions)
6274 heap_free(pti);
6275 return OutOfMemory;
6278 memcpy(real_positions, positions, sizeof(PointF) * length);
6280 transform_and_round_points(graphics, pti, real_positions, length);
6282 heap_free(real_positions);
6285 get_font_hfont(graphics, font, format, &hfont, matrix);
6287 hdc = CreateCompatibleDC(0);
6288 SelectObject(hdc, hfont);
6290 /* Get the boundaries of the text to be drawn */
6291 for (i=0; i<length; i++)
6293 DWORD glyphsize;
6294 int left, top, right, bottom;
6296 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6297 &glyphmetrics, 0, NULL, &identity);
6299 if (glyphsize == GDI_ERROR)
6301 ERR("GetGlyphOutlineW failed\n");
6302 heap_free(pti);
6303 DeleteDC(hdc);
6304 DeleteObject(hfont);
6305 return GenericError;
6308 if (glyphsize > max_glyphsize)
6309 max_glyphsize = glyphsize;
6311 if (glyphsize != 0)
6313 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6314 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6315 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6316 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6318 if (left < min_x) min_x = left;
6319 if (top < min_y) min_y = top;
6320 if (right > max_x) max_x = right;
6321 if (bottom > max_y) max_y = bottom;
6324 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6326 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6327 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6331 if (max_glyphsize == 0)
6332 /* Nothing to draw. */
6333 return Ok;
6335 glyph_mask = heap_alloc_zero(max_glyphsize);
6336 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
6337 text_mask_stride = max_x - min_x;
6339 if (!(glyph_mask && text_mask))
6341 heap_free(glyph_mask);
6342 heap_free(text_mask);
6343 heap_free(pti);
6344 DeleteDC(hdc);
6345 DeleteObject(hfont);
6346 return OutOfMemory;
6349 /* Generate a mask for the text */
6350 for (i=0; i<length; i++)
6352 DWORD ret;
6353 int left, top, stride;
6355 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6356 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6358 if (ret == GDI_ERROR || ret == 0)
6359 continue; /* empty glyph */
6361 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6362 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6363 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6365 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6367 BYTE *glyph_val = glyph_mask + y * stride;
6368 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6369 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6371 *text_val = min(64, *text_val + *glyph_val);
6372 glyph_val++;
6373 text_val++;
6378 heap_free(pti);
6379 DeleteDC(hdc);
6380 DeleteObject(hfont);
6381 heap_free(glyph_mask);
6383 /* get the brush data */
6384 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
6385 if (!pixel_data)
6387 heap_free(text_mask);
6388 return OutOfMemory;
6391 pixel_area.X = min_x;
6392 pixel_area.Y = min_y;
6393 pixel_area.Width = max_x - min_x;
6394 pixel_area.Height = max_y - min_y;
6395 pixel_data_stride = pixel_area.Width * 4;
6397 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6398 if (stat != Ok)
6400 heap_free(text_mask);
6401 heap_free(pixel_data);
6402 return stat;
6405 /* multiply the brush data by the mask */
6406 for (y=0; y<pixel_area.Height; y++)
6408 BYTE *text_val = text_mask + text_mask_stride * y;
6409 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6410 for (x=0; x<pixel_area.Width; x++)
6412 *pixel_val = (*pixel_val) * (*text_val) / 64;
6413 text_val++;
6414 pixel_val+=4;
6418 heap_free(text_mask);
6420 /* draw the result */
6421 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6422 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
6424 heap_free(pixel_data);
6426 return stat;
6429 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6430 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6431 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6432 INT flags, GDIPCONST GpMatrix *matrix)
6434 GpStatus stat = NotImplemented;
6436 if (length == -1)
6437 length = strlenW(text);
6439 if (graphics->hdc && !graphics->alpha_hdc &&
6440 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6441 brush->bt == BrushTypeSolidColor &&
6442 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6443 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6444 brush, positions, flags, matrix);
6445 if (stat == NotImplemented)
6446 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6447 brush, positions, flags, matrix);
6448 return stat;
6451 /*****************************************************************************
6452 * GdipDrawDriverString [GDIPLUS.@]
6454 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6455 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6456 GDIPCONST PointF *positions, INT flags,
6457 GDIPCONST GpMatrix *matrix )
6459 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6461 if (!graphics || !text || !font || !brush || !positions)
6462 return InvalidParameter;
6464 return draw_driver_string(graphics, text, length, font, NULL,
6465 brush, positions, flags, matrix);
6468 /*****************************************************************************
6469 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6471 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6473 GpStatus stat;
6474 GpRegion* rgn;
6476 TRACE("(%p, %p)\n", graphics, res);
6478 if((stat = GdipCreateRegion(&rgn)) != Ok)
6479 return stat;
6481 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6482 goto cleanup;
6484 stat = GdipIsEmptyRegion(rgn, graphics, res);
6486 cleanup:
6487 GdipDeleteRegion(rgn);
6488 return stat;
6491 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6493 static int calls;
6495 TRACE("(%p) stub\n", graphics);
6497 if(!(calls++))
6498 FIXME("not implemented\n");
6500 return NotImplemented;