msacm32/tests: Allow alternative product id.
[wine.git] / dlls / gdiplus / graphics.c
blob1b7d8c47b8017dc7f4dccb77921a687e56a935cf
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 = GdipAlloc(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 GdipFree(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 static 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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(count * sizeof(PointF));
1660 custpt = GdipAlloc(count * sizeof(POINT));
1661 tp = GdipAlloc(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 GdipFree(custptf);
1691 GdipFree(custpt);
1692 GdipFree(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 = GdipAlloc(count * sizeof(POINT));
1795 BYTE *tp = GdipAlloc(count);
1796 GpPointF *ptcopy = GdipAlloc(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 GdipFree(pti);
1910 GdipFree(ptcopy);
1911 GdipFree(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 = GdipAlloc(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 GdipFree(*container);
1971 *container = NULL;
1972 return sts;
1975 return Ok;
1978 static void delete_container(GraphicsContainerItem* container)
1980 GdipDeleteRegion(container->clip);
1981 GdipFree(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 = GdipAlloc(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 GdipFree(*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 = GdipAlloc(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 GdipFree(*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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3042 if (!dst_data)
3044 GdipFree(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 GdipFree(src_data);
3089 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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;
4301 TRACE("(%p, %p)\n", graphics, rect);
4303 if(!graphics || !rect)
4304 return InvalidParameter;
4306 if(graphics->busy)
4307 return ObjectBusy;
4309 /* intersect window and graphics clipping regions */
4310 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4311 return stat;
4313 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4314 goto cleanup;
4316 /* get bounds of the region */
4317 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4319 cleanup:
4320 GdipDeleteRegion(clip_rgn);
4322 return stat;
4325 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4327 GpRectF rectf;
4328 GpStatus stat;
4330 TRACE("(%p, %p)\n", graphics, rect);
4332 if(!graphics || !rect)
4333 return InvalidParameter;
4335 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4337 rect->X = gdip_round(rectf.X);
4338 rect->Y = gdip_round(rectf.Y);
4339 rect->Width = gdip_round(rectf.Width);
4340 rect->Height = gdip_round(rectf.Height);
4343 return stat;
4346 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4348 TRACE("(%p, %p)\n", graphics, matrix);
4350 if(!graphics || !matrix)
4351 return InvalidParameter;
4353 if(graphics->busy)
4354 return ObjectBusy;
4356 *matrix = graphics->worldtrans;
4357 return Ok;
4360 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4362 GpSolidFill *brush;
4363 GpStatus stat;
4364 GpRectF wnd_rect;
4366 TRACE("(%p, %x)\n", graphics, color);
4368 if(!graphics)
4369 return InvalidParameter;
4371 if(graphics->busy)
4372 return ObjectBusy;
4374 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4375 return stat;
4377 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4378 GdipDeleteBrush((GpBrush*)brush);
4379 return stat;
4382 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4383 wnd_rect.Width, wnd_rect.Height);
4385 GdipDeleteBrush((GpBrush*)brush);
4387 return Ok;
4390 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4392 TRACE("(%p, %p)\n", graphics, res);
4394 if(!graphics || !res)
4395 return InvalidParameter;
4397 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4400 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4402 GpStatus stat;
4403 GpRegion* rgn;
4404 GpPointF pt;
4406 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4408 if(!graphics || !result)
4409 return InvalidParameter;
4411 if(graphics->busy)
4412 return ObjectBusy;
4414 pt.X = x;
4415 pt.Y = y;
4416 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4417 CoordinateSpaceWorld, &pt, 1)) != Ok)
4418 return stat;
4420 if((stat = GdipCreateRegion(&rgn)) != Ok)
4421 return stat;
4423 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4424 goto cleanup;
4426 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4428 cleanup:
4429 GdipDeleteRegion(rgn);
4430 return stat;
4433 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4435 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4438 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4440 GpStatus stat;
4441 GpRegion* rgn;
4442 GpPointF pts[2];
4444 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4446 if(!graphics || !result)
4447 return InvalidParameter;
4449 if(graphics->busy)
4450 return ObjectBusy;
4452 pts[0].X = x;
4453 pts[0].Y = y;
4454 pts[1].X = x + width;
4455 pts[1].Y = y + height;
4457 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4458 CoordinateSpaceWorld, pts, 2)) != Ok)
4459 return stat;
4461 pts[1].X -= pts[0].X;
4462 pts[1].Y -= pts[0].Y;
4464 if((stat = GdipCreateRegion(&rgn)) != Ok)
4465 return stat;
4467 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4468 goto cleanup;
4470 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4472 cleanup:
4473 GdipDeleteRegion(rgn);
4474 return stat;
4477 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4479 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4482 GpStatus gdip_format_string(HDC hdc,
4483 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4484 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4485 gdip_format_string_callback callback, void *user_data)
4487 WCHAR* stringdup;
4488 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4489 nheight, lineend, lineno = 0;
4490 RectF bounds;
4491 StringAlignment halign;
4492 GpStatus stat = Ok;
4493 SIZE size;
4494 HotkeyPrefix hkprefix;
4495 INT *hotkeyprefix_offsets=NULL;
4496 INT hotkeyprefix_count=0;
4497 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4498 BOOL seen_prefix = FALSE;
4499 GpStringFormat *dyn_format=NULL;
4501 if(length == -1) length = lstrlenW(string);
4503 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4504 if(!stringdup) return OutOfMemory;
4506 if (!format)
4508 stat = GdipStringFormatGetGenericDefault(&dyn_format);
4509 if (stat != Ok)
4511 GdipFree(stringdup);
4512 return stat;
4514 format = dyn_format;
4517 nwidth = rect->Width;
4518 nheight = rect->Height;
4519 if (ignore_empty_clip)
4521 if (!nwidth) nwidth = INT_MAX;
4522 if (!nheight) nheight = INT_MAX;
4525 hkprefix = format->hkprefix;
4527 if (hkprefix == HotkeyPrefixShow)
4529 for (i=0; i<length; i++)
4531 if (string[i] == '&')
4532 hotkeyprefix_count++;
4536 if (hotkeyprefix_count)
4537 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4539 hotkeyprefix_count = 0;
4541 for(i = 0, j = 0; i < length; i++){
4542 /* FIXME: This makes the indexes passed to callback inaccurate. */
4543 if(!isprintW(string[i]) && (string[i] != '\n'))
4544 continue;
4546 /* FIXME: tabs should be handled using tabstops from stringformat */
4547 if (string[i] == '\t')
4548 continue;
4550 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4551 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4552 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4554 seen_prefix = TRUE;
4555 continue;
4558 seen_prefix = FALSE;
4560 stringdup[j] = string[i];
4561 j++;
4564 length = j;
4566 halign = format->align;
4568 while(sum < length){
4569 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4570 nwidth, &fit, NULL, &size);
4571 fitcpy = fit;
4573 if(fit == 0)
4574 break;
4576 for(lret = 0; lret < fit; lret++)
4577 if(*(stringdup + sum + lret) == '\n')
4578 break;
4580 /* Line break code (may look strange, but it imitates windows). */
4581 if(lret < fit)
4582 lineend = fit = lret; /* this is not an off-by-one error */
4583 else if(fit < (length - sum)){
4584 if(*(stringdup + sum + fit) == ' ')
4585 while(*(stringdup + sum + fit) == ' ')
4586 fit++;
4587 else
4588 while(*(stringdup + sum + fit - 1) != ' '){
4589 fit--;
4591 if(*(stringdup + sum + fit) == '\t')
4592 break;
4594 if(fit == 0){
4595 fit = fitcpy;
4596 break;
4599 lineend = fit;
4600 while(*(stringdup + sum + lineend - 1) == ' ' ||
4601 *(stringdup + sum + lineend - 1) == '\t')
4602 lineend--;
4604 else
4605 lineend = fit;
4607 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4608 nwidth, &j, NULL, &size);
4610 bounds.Width = size.cx;
4612 if(height + size.cy > nheight)
4614 if (format->attr & StringFormatFlagsLineLimit)
4615 break;
4616 bounds.Height = nheight - (height + size.cy);
4618 else
4619 bounds.Height = size.cy;
4621 bounds.Y = rect->Y + height;
4623 switch (halign)
4625 case StringAlignmentNear:
4626 default:
4627 bounds.X = rect->X;
4628 break;
4629 case StringAlignmentCenter:
4630 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4631 break;
4632 case StringAlignmentFar:
4633 bounds.X = rect->X + rect->Width - bounds.Width;
4634 break;
4637 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4638 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4639 break;
4641 stat = callback(hdc, stringdup, sum, lineend,
4642 font, rect, format, lineno, &bounds,
4643 &hotkeyprefix_offsets[hotkeyprefix_pos],
4644 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4646 if (stat != Ok)
4647 break;
4649 sum += fit + (lret < fitcpy ? 1 : 0);
4650 height += size.cy;
4651 lineno++;
4653 hotkeyprefix_pos = hotkeyprefix_end_pos;
4655 if(height > nheight)
4656 break;
4658 /* Stop if this was a linewrap (but not if it was a linebreak). */
4659 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
4660 break;
4663 GdipFree(stringdup);
4664 GdipFree(hotkeyprefix_offsets);
4665 GdipDeleteStringFormat(dyn_format);
4667 return stat;
4670 struct measure_ranges_args {
4671 GpRegion **regions;
4672 REAL rel_width, rel_height;
4675 static GpStatus measure_ranges_callback(HDC hdc,
4676 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4677 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4678 INT lineno, const RectF *bounds, INT *underlined_indexes,
4679 INT underlined_index_count, void *user_data)
4681 int i;
4682 GpStatus stat = Ok;
4683 struct measure_ranges_args *args = user_data;
4685 for (i=0; i<format->range_count; i++)
4687 INT range_start = max(index, format->character_ranges[i].First);
4688 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4689 if (range_start < range_end)
4691 GpRectF range_rect;
4692 SIZE range_size;
4694 range_rect.Y = bounds->Y / args->rel_height;
4695 range_rect.Height = bounds->Height / args->rel_height;
4697 GetTextExtentExPointW(hdc, string + index, range_start - index,
4698 INT_MAX, NULL, NULL, &range_size);
4699 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4701 GetTextExtentExPointW(hdc, string + index, range_end - index,
4702 INT_MAX, NULL, NULL, &range_size);
4703 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4705 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4706 if (stat != Ok)
4707 break;
4711 return stat;
4714 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4715 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4716 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4717 INT regionCount, GpRegion** regions)
4719 GpStatus stat;
4720 int i;
4721 HFONT gdifont, oldfont;
4722 struct measure_ranges_args args;
4723 HDC hdc, temp_hdc=NULL;
4724 GpPointF pt[3];
4725 RectF scaled_rect;
4726 REAL margin_x;
4728 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4729 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4731 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4732 return InvalidParameter;
4734 if (regionCount < stringFormat->range_count)
4735 return InvalidParameter;
4737 if(!graphics->hdc)
4739 hdc = temp_hdc = CreateCompatibleDC(0);
4740 if (!temp_hdc) return OutOfMemory;
4742 else
4743 hdc = graphics->hdc;
4745 if (stringFormat->attr)
4746 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4748 pt[0].X = 0.0;
4749 pt[0].Y = 0.0;
4750 pt[1].X = 1.0;
4751 pt[1].Y = 0.0;
4752 pt[2].X = 0.0;
4753 pt[2].Y = 1.0;
4754 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4755 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4756 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4757 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4758 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4760 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4761 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4763 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4764 scaled_rect.Y = layoutRect->Y * args.rel_height;
4765 scaled_rect.Width = layoutRect->Width * args.rel_width;
4766 scaled_rect.Height = layoutRect->Height * args.rel_height;
4768 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4769 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4771 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4772 oldfont = SelectObject(hdc, gdifont);
4774 for (i=0; i<stringFormat->range_count; i++)
4776 stat = GdipSetEmpty(regions[i]);
4777 if (stat != Ok)
4778 return stat;
4781 args.regions = regions;
4783 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4784 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4786 SelectObject(hdc, oldfont);
4787 DeleteObject(gdifont);
4789 if (temp_hdc)
4790 DeleteDC(temp_hdc);
4792 return stat;
4795 struct measure_string_args {
4796 RectF *bounds;
4797 INT *codepointsfitted;
4798 INT *linesfilled;
4799 REAL rel_width, rel_height;
4802 static GpStatus measure_string_callback(HDC hdc,
4803 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4804 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4805 INT lineno, const RectF *bounds, INT *underlined_indexes,
4806 INT underlined_index_count, void *user_data)
4808 struct measure_string_args *args = user_data;
4809 REAL new_width, new_height;
4811 new_width = bounds->Width / args->rel_width;
4812 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4814 if (new_width > args->bounds->Width)
4815 args->bounds->Width = new_width;
4817 if (new_height > args->bounds->Height)
4818 args->bounds->Height = new_height;
4820 if (args->codepointsfitted)
4821 *args->codepointsfitted = index + length;
4823 if (args->linesfilled)
4824 (*args->linesfilled)++;
4826 return Ok;
4829 /* Find the smallest rectangle that bounds the text when it is printed in rect
4830 * according to the format options listed in format. If rect has 0 width and
4831 * height, then just find the smallest rectangle that bounds the text when it's
4832 * printed at location (rect->X, rect-Y). */
4833 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4834 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4835 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4836 INT *codepointsfitted, INT *linesfilled)
4838 HFONT oldfont, gdifont;
4839 struct measure_string_args args;
4840 HDC temp_hdc=NULL, hdc;
4841 GpPointF pt[3];
4842 RectF scaled_rect;
4843 REAL margin_x;
4844 INT lines, glyphs;
4846 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4847 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4848 bounds, codepointsfitted, linesfilled);
4850 if(!graphics || !string || !font || !rect || !bounds)
4851 return InvalidParameter;
4853 if(!graphics->hdc)
4855 hdc = temp_hdc = CreateCompatibleDC(0);
4856 if (!temp_hdc) return OutOfMemory;
4858 else
4859 hdc = graphics->hdc;
4861 if(linesfilled) *linesfilled = 0;
4862 if(codepointsfitted) *codepointsfitted = 0;
4864 if(format)
4865 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4867 pt[0].X = 0.0;
4868 pt[0].Y = 0.0;
4869 pt[1].X = 1.0;
4870 pt[1].Y = 0.0;
4871 pt[2].X = 0.0;
4872 pt[2].Y = 1.0;
4873 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4874 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4875 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4876 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4877 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4879 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4880 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4882 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4883 scaled_rect.Y = rect->Y * args.rel_height;
4884 scaled_rect.Width = rect->Width * args.rel_width;
4885 scaled_rect.Height = rect->Height * args.rel_height;
4886 if (scaled_rect.Width >= 0.5)
4888 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4889 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4892 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4893 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4895 get_font_hfont(graphics, font, format, &gdifont, NULL);
4896 oldfont = SelectObject(hdc, gdifont);
4898 bounds->X = rect->X;
4899 bounds->Y = rect->Y;
4900 bounds->Width = 0.0;
4901 bounds->Height = 0.0;
4903 args.bounds = bounds;
4904 args.codepointsfitted = &glyphs;
4905 args.linesfilled = &lines;
4906 lines = glyphs = 0;
4908 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4909 measure_string_callback, &args);
4911 if (linesfilled) *linesfilled = lines;
4912 if (codepointsfitted) *codepointsfitted = glyphs;
4914 if (lines)
4915 bounds->Width += margin_x * 2.0;
4917 SelectObject(hdc, oldfont);
4918 DeleteObject(gdifont);
4920 if (temp_hdc)
4921 DeleteDC(temp_hdc);
4923 return Ok;
4926 struct draw_string_args {
4927 GpGraphics *graphics;
4928 GDIPCONST GpBrush *brush;
4929 REAL x, y, rel_width, rel_height, ascent;
4932 static GpStatus draw_string_callback(HDC hdc,
4933 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4934 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4935 INT lineno, const RectF *bounds, INT *underlined_indexes,
4936 INT underlined_index_count, void *user_data)
4938 struct draw_string_args *args = user_data;
4939 PointF position;
4940 GpStatus stat;
4942 position.X = args->x + bounds->X / args->rel_width;
4943 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4945 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4946 args->brush, &position,
4947 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4949 if (stat == Ok && underlined_index_count)
4951 OUTLINETEXTMETRICW otm;
4952 REAL underline_y, underline_height;
4953 int i;
4955 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4957 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4958 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4960 for (i=0; i<underlined_index_count; i++)
4962 REAL start_x, end_x;
4963 SIZE text_size;
4964 INT ofs = underlined_indexes[i] - index;
4966 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4967 start_x = text_size.cx / args->rel_width;
4969 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4970 end_x = text_size.cx / args->rel_width;
4972 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4976 return stat;
4979 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4980 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4981 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4983 HRGN rgn = NULL;
4984 HFONT gdifont;
4985 GpPointF pt[3], rectcpy[4];
4986 POINT corners[4];
4987 REAL rel_width, rel_height, margin_x;
4988 INT save_state, format_flags = 0;
4989 REAL offsety = 0.0;
4990 struct draw_string_args args;
4991 RectF scaled_rect;
4992 HDC hdc, temp_hdc=NULL;
4993 TEXTMETRICW textmetric;
4995 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4996 length, font, debugstr_rectf(rect), format, brush);
4998 if(!graphics || !string || !font || !brush || !rect)
4999 return InvalidParameter;
5001 if(graphics->hdc)
5003 hdc = graphics->hdc;
5005 else
5007 hdc = temp_hdc = CreateCompatibleDC(0);
5010 if(format){
5011 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5013 format_flags = format->attr;
5015 /* Should be no need to explicitly test for StringAlignmentNear as
5016 * that is default behavior if no alignment is passed. */
5017 if(format->vertalign != StringAlignmentNear){
5018 RectF bounds, in_rect = *rect;
5019 in_rect.Height = 0.0; /* avoid height clipping */
5020 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5022 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5024 if(format->vertalign == StringAlignmentCenter)
5025 offsety = (rect->Height - bounds.Height) / 2;
5026 else if(format->vertalign == StringAlignmentFar)
5027 offsety = (rect->Height - bounds.Height);
5029 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5032 save_state = SaveDC(hdc);
5034 pt[0].X = 0.0;
5035 pt[0].Y = 0.0;
5036 pt[1].X = 1.0;
5037 pt[1].Y = 0.0;
5038 pt[2].X = 0.0;
5039 pt[2].Y = 1.0;
5040 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5041 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5042 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5043 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5044 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5046 rectcpy[3].X = rectcpy[0].X = rect->X;
5047 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5048 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5049 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5050 transform_and_round_points(graphics, corners, rectcpy, 4);
5052 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5053 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5055 scaled_rect.X = margin_x * rel_width;
5056 scaled_rect.Y = 0.0;
5057 scaled_rect.Width = rel_width * rect->Width;
5058 scaled_rect.Height = rel_height * rect->Height;
5059 if (scaled_rect.Width >= 0.5)
5061 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5062 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5065 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5066 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5068 if (!(format_flags & StringFormatFlagsNoClip) &&
5069 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5070 rect->Width > 0.0 && rect->Height > 0.0)
5072 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5073 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5074 SelectClipRgn(hdc, rgn);
5077 get_font_hfont(graphics, font, format, &gdifont, NULL);
5078 SelectObject(hdc, gdifont);
5080 args.graphics = graphics;
5081 args.brush = brush;
5083 args.x = rect->X;
5084 args.y = rect->Y + offsety;
5086 args.rel_width = rel_width;
5087 args.rel_height = rel_height;
5089 GetTextMetricsW(hdc, &textmetric);
5090 args.ascent = textmetric.tmAscent / rel_height;
5092 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5093 draw_string_callback, &args);
5095 DeleteObject(rgn);
5096 DeleteObject(gdifont);
5098 RestoreDC(hdc, save_state);
5100 DeleteDC(temp_hdc);
5102 return Ok;
5105 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5107 TRACE("(%p)\n", graphics);
5109 if(!graphics)
5110 return InvalidParameter;
5112 if(graphics->busy)
5113 return ObjectBusy;
5115 return GdipSetInfinite(graphics->clip);
5118 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5120 TRACE("(%p)\n", graphics);
5122 if(!graphics)
5123 return InvalidParameter;
5125 if(graphics->busy)
5126 return ObjectBusy;
5128 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5131 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5133 return GdipEndContainer(graphics, state);
5136 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5137 GpMatrixOrder order)
5139 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5141 if(!graphics)
5142 return InvalidParameter;
5144 if(graphics->busy)
5145 return ObjectBusy;
5147 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5150 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5152 return GdipBeginContainer2(graphics, state);
5155 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5156 GraphicsContainer *state)
5158 GraphicsContainerItem *container;
5159 GpStatus sts;
5161 TRACE("(%p, %p)\n", graphics, state);
5163 if(!graphics || !state)
5164 return InvalidParameter;
5166 sts = init_container(&container, graphics);
5167 if(sts != Ok)
5168 return sts;
5170 list_add_head(&graphics->containers, &container->entry);
5171 *state = graphics->contid = container->contid;
5173 return Ok;
5176 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5178 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5179 return NotImplemented;
5182 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5184 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5185 return NotImplemented;
5188 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5190 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5191 return NotImplemented;
5194 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5196 GpStatus sts;
5197 GraphicsContainerItem *container, *container2;
5199 TRACE("(%p, %x)\n", graphics, state);
5201 if(!graphics)
5202 return InvalidParameter;
5204 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5205 if(container->contid == state)
5206 break;
5209 /* did not find a matching container */
5210 if(&container->entry == &graphics->containers)
5211 return Ok;
5213 sts = restore_container(graphics, container);
5214 if(sts != Ok)
5215 return sts;
5217 /* remove all of the containers on top of the found container */
5218 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5219 if(container->contid == state)
5220 break;
5221 list_remove(&container->entry);
5222 delete_container(container);
5225 list_remove(&container->entry);
5226 delete_container(container);
5228 return Ok;
5231 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5232 REAL sy, GpMatrixOrder order)
5234 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5236 if(!graphics)
5237 return InvalidParameter;
5239 if(graphics->busy)
5240 return ObjectBusy;
5242 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5245 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5246 CombineMode mode)
5248 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5250 if(!graphics || !srcgraphics)
5251 return InvalidParameter;
5253 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5256 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5257 CompositingMode mode)
5259 TRACE("(%p, %d)\n", graphics, mode);
5261 if(!graphics)
5262 return InvalidParameter;
5264 if(graphics->busy)
5265 return ObjectBusy;
5267 graphics->compmode = mode;
5269 return Ok;
5272 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5273 CompositingQuality quality)
5275 TRACE("(%p, %d)\n", graphics, quality);
5277 if(!graphics)
5278 return InvalidParameter;
5280 if(graphics->busy)
5281 return ObjectBusy;
5283 graphics->compqual = quality;
5285 return Ok;
5288 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5289 InterpolationMode mode)
5291 TRACE("(%p, %d)\n", graphics, mode);
5293 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5294 return InvalidParameter;
5296 if(graphics->busy)
5297 return ObjectBusy;
5299 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5300 mode = InterpolationModeBilinear;
5302 if (mode == InterpolationModeHighQuality)
5303 mode = InterpolationModeHighQualityBicubic;
5305 graphics->interpolation = mode;
5307 return Ok;
5310 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5312 GpStatus stat;
5314 TRACE("(%p, %.2f)\n", graphics, scale);
5316 if(!graphics || (scale <= 0.0))
5317 return InvalidParameter;
5319 if(graphics->busy)
5320 return ObjectBusy;
5322 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5324 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5325 if (stat != Ok)
5326 return stat;
5329 graphics->scale = scale;
5331 return Ok;
5334 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5336 GpStatus stat;
5338 TRACE("(%p, %d)\n", graphics, unit);
5340 if(!graphics)
5341 return InvalidParameter;
5343 if(graphics->busy)
5344 return ObjectBusy;
5346 if(unit == UnitWorld)
5347 return InvalidParameter;
5349 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5351 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5352 if (stat != Ok)
5353 return stat;
5356 graphics->unit = unit;
5358 return Ok;
5361 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5362 mode)
5364 TRACE("(%p, %d)\n", graphics, mode);
5366 if(!graphics)
5367 return InvalidParameter;
5369 if(graphics->busy)
5370 return ObjectBusy;
5372 graphics->pixeloffset = mode;
5374 return Ok;
5377 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5379 static int calls;
5381 TRACE("(%p,%i,%i)\n", graphics, x, y);
5383 if (!(calls++))
5384 FIXME("value is unused in rendering\n");
5386 if (!graphics)
5387 return InvalidParameter;
5389 graphics->origin_x = x;
5390 graphics->origin_y = y;
5392 return Ok;
5395 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5397 TRACE("(%p,%p,%p)\n", graphics, x, y);
5399 if (!graphics || !x || !y)
5400 return InvalidParameter;
5402 *x = graphics->origin_x;
5403 *y = graphics->origin_y;
5405 return Ok;
5408 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5410 TRACE("(%p, %d)\n", graphics, mode);
5412 if(!graphics)
5413 return InvalidParameter;
5415 if(graphics->busy)
5416 return ObjectBusy;
5418 graphics->smoothing = mode;
5420 return Ok;
5423 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5425 TRACE("(%p, %d)\n", graphics, contrast);
5427 if(!graphics)
5428 return InvalidParameter;
5430 graphics->textcontrast = contrast;
5432 return Ok;
5435 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5436 TextRenderingHint hint)
5438 TRACE("(%p, %d)\n", graphics, hint);
5440 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5441 return InvalidParameter;
5443 if(graphics->busy)
5444 return ObjectBusy;
5446 graphics->texthint = hint;
5448 return Ok;
5451 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5453 TRACE("(%p, %p)\n", graphics, matrix);
5455 if(!graphics || !matrix)
5456 return InvalidParameter;
5458 if(graphics->busy)
5459 return ObjectBusy;
5461 TRACE("%f,%f,%f,%f,%f,%f\n",
5462 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5463 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5465 graphics->worldtrans = *matrix;
5467 return Ok;
5470 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5471 REAL dy, GpMatrixOrder order)
5473 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5475 if(!graphics)
5476 return InvalidParameter;
5478 if(graphics->busy)
5479 return ObjectBusy;
5481 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5484 /*****************************************************************************
5485 * GdipSetClipHrgn [GDIPLUS.@]
5487 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5489 GpRegion *region;
5490 GpStatus status;
5492 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5494 if(!graphics)
5495 return InvalidParameter;
5497 if(graphics->busy)
5498 return ObjectBusy;
5500 /* hrgn is already in device units */
5501 status = GdipCreateRegionHrgn(hrgn, &region);
5502 if(status != Ok)
5503 return status;
5505 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5507 GdipDeleteRegion(region);
5508 return status;
5511 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5513 GpStatus status;
5514 GpPath *clip_path;
5516 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5518 if(!graphics)
5519 return InvalidParameter;
5521 if(graphics->busy)
5522 return ObjectBusy;
5524 status = GdipClonePath(path, &clip_path);
5525 if (status == Ok)
5527 GpMatrix world_to_device;
5529 get_graphics_transform(graphics, CoordinateSpaceDevice,
5530 CoordinateSpaceWorld, &world_to_device);
5531 status = GdipTransformPath(clip_path, &world_to_device);
5532 if (status == Ok)
5533 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5535 GdipDeletePath(clip_path);
5537 return status;
5540 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5541 REAL width, REAL height,
5542 CombineMode mode)
5544 GpStatus status;
5545 GpRectF rect;
5546 GpRegion *region;
5548 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5550 if(!graphics)
5551 return InvalidParameter;
5553 if(graphics->busy)
5554 return ObjectBusy;
5556 rect.X = x;
5557 rect.Y = y;
5558 rect.Width = width;
5559 rect.Height = height;
5560 status = GdipCreateRegionRect(&rect, &region);
5561 if (status == Ok)
5563 GpMatrix world_to_device;
5565 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5566 status = GdipTransformRegion(region, &world_to_device);
5567 if (status == Ok)
5568 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5570 GdipDeleteRegion(region);
5572 return status;
5575 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5576 INT width, INT height,
5577 CombineMode mode)
5579 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5581 if(!graphics)
5582 return InvalidParameter;
5584 if(graphics->busy)
5585 return ObjectBusy;
5587 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5590 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5591 CombineMode mode)
5593 GpStatus status;
5594 GpRegion *clip;
5596 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5598 if(!graphics || !region)
5599 return InvalidParameter;
5601 if(graphics->busy)
5602 return ObjectBusy;
5604 status = GdipCloneRegion(region, &clip);
5605 if (status == Ok)
5607 GpMatrix world_to_device;
5609 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5610 status = GdipTransformRegion(clip, &world_to_device);
5611 if (status == Ok)
5612 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5614 GdipDeleteRegion(clip);
5616 return status;
5619 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5620 INT count)
5622 INT save_state;
5623 POINT *pti;
5625 TRACE("(%p, %p, %d)\n", graphics, points, count);
5627 if(!graphics || !pen || count<=0)
5628 return InvalidParameter;
5630 if(graphics->busy)
5631 return ObjectBusy;
5633 if (!graphics->hdc)
5635 FIXME("graphics object has no HDC\n");
5636 return Ok;
5639 pti = GdipAlloc(sizeof(POINT) * count);
5641 save_state = prepare_dc(graphics, pen);
5642 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5644 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5645 Polygon(graphics->hdc, pti, count);
5647 restore_dc(graphics, save_state);
5648 GdipFree(pti);
5650 return Ok;
5653 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5654 INT count)
5656 GpStatus ret;
5657 GpPointF *ptf;
5658 INT i;
5660 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5662 if(count<=0) return InvalidParameter;
5663 ptf = GdipAlloc(sizeof(GpPointF) * count);
5665 for(i = 0;i < count; i++){
5666 ptf[i].X = (REAL)points[i].X;
5667 ptf[i].Y = (REAL)points[i].Y;
5670 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5671 GdipFree(ptf);
5673 return ret;
5676 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5678 TRACE("(%p, %p)\n", graphics, dpi);
5680 if(!graphics || !dpi)
5681 return InvalidParameter;
5683 if(graphics->busy)
5684 return ObjectBusy;
5686 *dpi = graphics->xres;
5687 return Ok;
5690 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5692 TRACE("(%p, %p)\n", graphics, dpi);
5694 if(!graphics || !dpi)
5695 return InvalidParameter;
5697 if(graphics->busy)
5698 return ObjectBusy;
5700 *dpi = graphics->yres;
5701 return Ok;
5704 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5705 GpMatrixOrder order)
5707 GpMatrix m;
5708 GpStatus ret;
5710 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5712 if(!graphics || !matrix)
5713 return InvalidParameter;
5715 if(graphics->busy)
5716 return ObjectBusy;
5718 m = graphics->worldtrans;
5720 ret = GdipMultiplyMatrix(&m, matrix, order);
5721 if(ret == Ok)
5722 graphics->worldtrans = m;
5724 return ret;
5727 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5728 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5730 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5732 GpStatus stat=Ok;
5734 TRACE("(%p, %p)\n", graphics, hdc);
5736 if(!graphics || !hdc)
5737 return InvalidParameter;
5739 if(graphics->busy)
5740 return ObjectBusy;
5742 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5744 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5746 else if (!graphics->hdc ||
5747 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5749 /* Create a fake HDC and fill it with a constant color. */
5750 HDC temp_hdc;
5751 HBITMAP hbitmap;
5752 GpRectF bounds;
5753 BITMAPINFOHEADER bmih;
5754 int i;
5756 stat = get_graphics_bounds(graphics, &bounds);
5757 if (stat != Ok)
5758 return stat;
5760 graphics->temp_hbitmap_width = bounds.Width;
5761 graphics->temp_hbitmap_height = bounds.Height;
5763 bmih.biSize = sizeof(bmih);
5764 bmih.biWidth = graphics->temp_hbitmap_width;
5765 bmih.biHeight = -graphics->temp_hbitmap_height;
5766 bmih.biPlanes = 1;
5767 bmih.biBitCount = 32;
5768 bmih.biCompression = BI_RGB;
5769 bmih.biSizeImage = 0;
5770 bmih.biXPelsPerMeter = 0;
5771 bmih.biYPelsPerMeter = 0;
5772 bmih.biClrUsed = 0;
5773 bmih.biClrImportant = 0;
5775 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5776 (void**)&graphics->temp_bits, NULL, 0);
5777 if (!hbitmap)
5778 return GenericError;
5780 temp_hdc = CreateCompatibleDC(0);
5781 if (!temp_hdc)
5783 DeleteObject(hbitmap);
5784 return GenericError;
5787 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5788 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5790 SelectObject(temp_hdc, hbitmap);
5792 graphics->temp_hbitmap = hbitmap;
5793 *hdc = graphics->temp_hdc = temp_hdc;
5795 else
5797 *hdc = graphics->hdc;
5800 if (stat == Ok)
5801 graphics->busy = TRUE;
5803 return stat;
5806 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5808 GpStatus stat=Ok;
5810 TRACE("(%p, %p)\n", graphics, hdc);
5812 if(!graphics || !hdc || !graphics->busy)
5813 return InvalidParameter;
5815 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5817 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5819 else if (graphics->temp_hdc == hdc)
5821 DWORD* pos;
5822 int i;
5824 /* Find the pixels that have changed, and mark them as opaque. */
5825 pos = (DWORD*)graphics->temp_bits;
5826 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5828 if (*pos != DC_BACKGROUND_KEY)
5830 *pos |= 0xff000000;
5832 pos++;
5835 /* Write the changed pixels to the real target. */
5836 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5837 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5838 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
5840 /* Clean up. */
5841 DeleteDC(graphics->temp_hdc);
5842 DeleteObject(graphics->temp_hbitmap);
5843 graphics->temp_hdc = NULL;
5844 graphics->temp_hbitmap = NULL;
5846 else if (hdc != graphics->hdc)
5848 stat = InvalidParameter;
5851 if (stat == Ok)
5852 graphics->busy = FALSE;
5854 return stat;
5857 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5859 GpRegion *clip;
5860 GpStatus status;
5861 GpMatrix device_to_world;
5863 TRACE("(%p, %p)\n", graphics, region);
5865 if(!graphics || !region)
5866 return InvalidParameter;
5868 if(graphics->busy)
5869 return ObjectBusy;
5871 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5872 return status;
5874 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5875 status = GdipTransformRegion(clip, &device_to_world);
5876 if (status != Ok)
5878 GdipDeleteRegion(clip);
5879 return status;
5882 /* free everything except root node and header */
5883 delete_element(&region->node);
5884 memcpy(region, clip, sizeof(GpRegion));
5885 GdipFree(clip);
5887 return Ok;
5890 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5891 GpCoordinateSpace src_space, GpMatrix *matrix)
5893 GpStatus stat = Ok;
5894 REAL scale_x, scale_y;
5896 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5898 if (dst_space != src_space)
5900 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5901 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5903 if(graphics->unit != UnitDisplay)
5905 scale_x *= graphics->scale;
5906 scale_y *= graphics->scale;
5909 /* transform from src_space to CoordinateSpacePage */
5910 switch (src_space)
5912 case CoordinateSpaceWorld:
5913 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5914 break;
5915 case CoordinateSpacePage:
5916 break;
5917 case CoordinateSpaceDevice:
5918 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5919 break;
5922 /* transform from CoordinateSpacePage to dst_space */
5923 switch (dst_space)
5925 case CoordinateSpaceWorld:
5927 GpMatrix inverted_transform = graphics->worldtrans;
5928 stat = GdipInvertMatrix(&inverted_transform);
5929 if (stat == Ok)
5930 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5931 break;
5933 case CoordinateSpacePage:
5934 break;
5935 case CoordinateSpaceDevice:
5936 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5937 break;
5940 return stat;
5943 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5944 GpCoordinateSpace src_space, GpPointF *points, INT count)
5946 GpMatrix matrix;
5947 GpStatus stat;
5949 if(!graphics || !points || count <= 0)
5950 return InvalidParameter;
5952 if(graphics->busy)
5953 return ObjectBusy;
5955 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5957 if (src_space == dst_space) return Ok;
5959 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5960 if (stat != Ok) return stat;
5962 return GdipTransformMatrixPoints(&matrix, points, count);
5965 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5966 GpCoordinateSpace src_space, GpPoint *points, INT count)
5968 GpPointF *pointsF;
5969 GpStatus ret;
5970 INT i;
5972 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5974 if(count <= 0)
5975 return InvalidParameter;
5977 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5978 if(!pointsF)
5979 return OutOfMemory;
5981 for(i = 0; i < count; i++){
5982 pointsF[i].X = (REAL)points[i].X;
5983 pointsF[i].Y = (REAL)points[i].Y;
5986 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5988 if(ret == Ok)
5989 for(i = 0; i < count; i++){
5990 points[i].X = gdip_round(pointsF[i].X);
5991 points[i].Y = gdip_round(pointsF[i].Y);
5993 GdipFree(pointsF);
5995 return ret;
5998 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6000 static int calls;
6002 TRACE("\n");
6004 if (!calls++)
6005 FIXME("stub\n");
6007 return NULL;
6010 /*****************************************************************************
6011 * GdipTranslateClip [GDIPLUS.@]
6013 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6015 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6017 if(!graphics)
6018 return InvalidParameter;
6020 if(graphics->busy)
6021 return ObjectBusy;
6023 return GdipTranslateRegion(graphics->clip, dx, dy);
6026 /*****************************************************************************
6027 * GdipTranslateClipI [GDIPLUS.@]
6029 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6031 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6033 if(!graphics)
6034 return InvalidParameter;
6036 if(graphics->busy)
6037 return ObjectBusy;
6039 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6043 /*****************************************************************************
6044 * GdipMeasureDriverString [GDIPLUS.@]
6046 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6047 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6048 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6050 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6051 HFONT hfont;
6052 HDC hdc;
6053 REAL min_x, min_y, max_x, max_y, x, y;
6054 int i;
6055 TEXTMETRICW textmetric;
6056 const WORD *glyph_indices;
6057 WORD *dynamic_glyph_indices=NULL;
6058 REAL rel_width, rel_height, ascent, descent;
6059 GpPointF pt[3];
6061 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6063 if (!graphics || !text || !font || !positions || !boundingBox)
6064 return InvalidParameter;
6066 if (length == -1)
6067 length = strlenW(text);
6069 if (length == 0)
6071 boundingBox->X = 0.0;
6072 boundingBox->Y = 0.0;
6073 boundingBox->Width = 0.0;
6074 boundingBox->Height = 0.0;
6077 if (flags & unsupported_flags)
6078 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6080 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6082 hdc = CreateCompatibleDC(0);
6083 SelectObject(hdc, hfont);
6085 GetTextMetricsW(hdc, &textmetric);
6087 pt[0].X = 0.0;
6088 pt[0].Y = 0.0;
6089 pt[1].X = 1.0;
6090 pt[1].Y = 0.0;
6091 pt[2].X = 0.0;
6092 pt[2].Y = 1.0;
6093 if (matrix)
6095 GpMatrix xform = *matrix;
6096 GdipTransformMatrixPoints(&xform, pt, 3);
6098 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6099 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6100 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6101 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6102 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6104 if (flags & DriverStringOptionsCmapLookup)
6106 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6107 if (!glyph_indices)
6109 DeleteDC(hdc);
6110 DeleteObject(hfont);
6111 return OutOfMemory;
6114 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6116 else
6117 glyph_indices = text;
6119 min_x = max_x = x = positions[0].X;
6120 min_y = max_y = y = positions[0].Y;
6122 ascent = textmetric.tmAscent / rel_height;
6123 descent = textmetric.tmDescent / rel_height;
6125 for (i=0; i<length; i++)
6127 int char_width;
6128 ABC abc;
6130 if (!(flags & DriverStringOptionsRealizedAdvance))
6132 x = positions[i].X;
6133 y = positions[i].Y;
6136 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6137 char_width = abc.abcA + abc.abcB + abc.abcC;
6139 if (min_y > y - ascent) min_y = y - ascent;
6140 if (max_y < y + descent) max_y = y + descent;
6141 if (min_x > x) min_x = x;
6143 x += char_width / rel_width;
6145 if (max_x < x) max_x = x;
6148 GdipFree(dynamic_glyph_indices);
6149 DeleteDC(hdc);
6150 DeleteObject(hfont);
6152 boundingBox->X = min_x;
6153 boundingBox->Y = min_y;
6154 boundingBox->Width = max_x - min_x;
6155 boundingBox->Height = max_y - min_y;
6157 return Ok;
6160 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6161 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6162 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6163 INT flags, GDIPCONST GpMatrix *matrix)
6165 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6166 INT save_state;
6167 GpPointF pt;
6168 HFONT hfont;
6169 UINT eto_flags=0;
6171 if (flags & unsupported_flags)
6172 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6174 if (!(flags & DriverStringOptionsCmapLookup))
6175 eto_flags |= ETO_GLYPH_INDEX;
6177 save_state = SaveDC(graphics->hdc);
6178 SetBkMode(graphics->hdc, TRANSPARENT);
6179 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6181 pt = positions[0];
6182 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6184 get_font_hfont(graphics, font, format, &hfont, matrix);
6185 SelectObject(graphics->hdc, hfont);
6187 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6189 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6191 RestoreDC(graphics->hdc, save_state);
6193 DeleteObject(hfont);
6195 return Ok;
6198 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6199 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6200 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6201 INT flags, GDIPCONST GpMatrix *matrix)
6203 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6204 GpStatus stat;
6205 PointF *real_positions, real_position;
6206 POINT *pti;
6207 HFONT hfont;
6208 HDC hdc;
6209 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6210 DWORD max_glyphsize=0;
6211 GLYPHMETRICS glyphmetrics;
6212 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6213 BYTE *glyph_mask;
6214 BYTE *text_mask;
6215 int text_mask_stride;
6216 BYTE *pixel_data;
6217 int pixel_data_stride;
6218 GpRect pixel_area;
6219 UINT ggo_flags = GGO_GRAY8_BITMAP;
6221 if (length <= 0)
6222 return Ok;
6224 if (!(flags & DriverStringOptionsCmapLookup))
6225 ggo_flags |= GGO_GLYPH_INDEX;
6227 if (flags & unsupported_flags)
6228 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6230 pti = GdipAlloc(sizeof(POINT) * length);
6231 if (!pti)
6232 return OutOfMemory;
6234 if (flags & DriverStringOptionsRealizedAdvance)
6236 real_position = positions[0];
6238 transform_and_round_points(graphics, pti, &real_position, 1);
6240 else
6242 real_positions = GdipAlloc(sizeof(PointF) * length);
6243 if (!real_positions)
6245 GdipFree(pti);
6246 return OutOfMemory;
6249 memcpy(real_positions, positions, sizeof(PointF) * length);
6251 transform_and_round_points(graphics, pti, real_positions, length);
6253 GdipFree(real_positions);
6256 get_font_hfont(graphics, font, format, &hfont, matrix);
6258 hdc = CreateCompatibleDC(0);
6259 SelectObject(hdc, hfont);
6261 /* Get the boundaries of the text to be drawn */
6262 for (i=0; i<length; i++)
6264 DWORD glyphsize;
6265 int left, top, right, bottom;
6267 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6268 &glyphmetrics, 0, NULL, &identity);
6270 if (glyphsize == GDI_ERROR)
6272 ERR("GetGlyphOutlineW failed\n");
6273 GdipFree(pti);
6274 DeleteDC(hdc);
6275 DeleteObject(hfont);
6276 return GenericError;
6279 if (glyphsize > max_glyphsize)
6280 max_glyphsize = glyphsize;
6282 if (glyphsize != 0)
6284 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6285 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6286 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6287 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6289 if (left < min_x) min_x = left;
6290 if (top < min_y) min_y = top;
6291 if (right > max_x) max_x = right;
6292 if (bottom > max_y) max_y = bottom;
6295 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6297 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6298 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6302 if (max_glyphsize == 0)
6303 /* Nothing to draw. */
6304 return Ok;
6306 glyph_mask = GdipAlloc(max_glyphsize);
6307 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6308 text_mask_stride = max_x - min_x;
6310 if (!(glyph_mask && text_mask))
6312 GdipFree(glyph_mask);
6313 GdipFree(text_mask);
6314 GdipFree(pti);
6315 DeleteDC(hdc);
6316 DeleteObject(hfont);
6317 return OutOfMemory;
6320 /* Generate a mask for the text */
6321 for (i=0; i<length; i++)
6323 DWORD ret;
6324 int left, top, stride;
6326 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6327 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6329 if (ret == GDI_ERROR || ret == 0)
6330 continue; /* empty glyph */
6332 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6333 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6334 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6336 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6338 BYTE *glyph_val = glyph_mask + y * stride;
6339 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6340 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6342 *text_val = min(64, *text_val + *glyph_val);
6343 glyph_val++;
6344 text_val++;
6349 GdipFree(pti);
6350 DeleteDC(hdc);
6351 DeleteObject(hfont);
6352 GdipFree(glyph_mask);
6354 /* get the brush data */
6355 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6356 if (!pixel_data)
6358 GdipFree(text_mask);
6359 return OutOfMemory;
6362 pixel_area.X = min_x;
6363 pixel_area.Y = min_y;
6364 pixel_area.Width = max_x - min_x;
6365 pixel_area.Height = max_y - min_y;
6366 pixel_data_stride = pixel_area.Width * 4;
6368 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6369 if (stat != Ok)
6371 GdipFree(text_mask);
6372 GdipFree(pixel_data);
6373 return stat;
6376 /* multiply the brush data by the mask */
6377 for (y=0; y<pixel_area.Height; y++)
6379 BYTE *text_val = text_mask + text_mask_stride * y;
6380 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6381 for (x=0; x<pixel_area.Width; x++)
6383 *pixel_val = (*pixel_val) * (*text_val) / 64;
6384 text_val++;
6385 pixel_val+=4;
6389 GdipFree(text_mask);
6391 /* draw the result */
6392 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6393 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
6395 GdipFree(pixel_data);
6397 return stat;
6400 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6401 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6402 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6403 INT flags, GDIPCONST GpMatrix *matrix)
6405 GpStatus stat = NotImplemented;
6407 if (length == -1)
6408 length = strlenW(text);
6410 if (graphics->hdc && !graphics->alpha_hdc &&
6411 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6412 brush->bt == BrushTypeSolidColor &&
6413 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6414 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6415 brush, positions, flags, matrix);
6416 if (stat == NotImplemented)
6417 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6418 brush, positions, flags, matrix);
6419 return stat;
6422 /*****************************************************************************
6423 * GdipDrawDriverString [GDIPLUS.@]
6425 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6426 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6427 GDIPCONST PointF *positions, INT flags,
6428 GDIPCONST GpMatrix *matrix )
6430 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6432 if (!graphics || !text || !font || !brush || !positions)
6433 return InvalidParameter;
6435 return draw_driver_string(graphics, text, length, font, NULL,
6436 brush, positions, flags, matrix);
6439 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6440 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6442 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6443 return NotImplemented;
6446 /*****************************************************************************
6447 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6449 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6451 GpStatus stat;
6452 GpRegion* rgn;
6454 TRACE("(%p, %p)\n", graphics, res);
6456 if((stat = GdipCreateRegion(&rgn)) != Ok)
6457 return stat;
6459 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6460 goto cleanup;
6462 stat = GdipIsEmptyRegion(rgn, graphics, res);
6464 cleanup:
6465 GdipDeleteRegion(rgn);
6466 return stat;
6469 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6471 static int calls;
6473 TRACE("(%p) stub\n", graphics);
6475 if(!(calls++))
6476 FIXME("not implemented\n");
6478 return NotImplemented;