ntdll: Store memory views in a binary tree instead of a list.
[wine.git] / dlls / gdiplus / graphics.c
blobea02c6b28d893ef7f6e93c29321b25778a473ec5
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 /* Converts from gdiplus path point type to gdi path point type. */
55 static BYTE convert_path_point_type(BYTE type)
57 BYTE ret;
59 switch(type & PathPointTypePathTypeMask){
60 case PathPointTypeBezier:
61 ret = PT_BEZIERTO;
62 break;
63 case PathPointTypeLine:
64 ret = PT_LINETO;
65 break;
66 case PathPointTypeStart:
67 ret = PT_MOVETO;
68 break;
69 default:
70 ERR("Bad point type\n");
71 return 0;
74 if(type & PathPointTypeCloseSubpath)
75 ret |= PT_CLOSEFIGURE;
77 return ret;
80 static COLORREF get_gdi_brush_color(const GpBrush *brush)
82 ARGB argb;
84 switch (brush->bt)
86 case BrushTypeSolidColor:
88 const GpSolidFill *sf = (const GpSolidFill *)brush;
89 argb = sf->color;
90 break;
92 case BrushTypeHatchFill:
94 const GpHatch *hatch = (const GpHatch *)brush;
95 argb = hatch->forecol;
96 break;
98 case BrushTypeLinearGradient:
100 const GpLineGradient *line = (const GpLineGradient *)brush;
101 argb = line->startcolor;
102 break;
104 case BrushTypePathGradient:
106 const GpPathGradient *grad = (const GpPathGradient *)brush;
107 argb = grad->centercolor;
108 break;
110 default:
111 FIXME("unhandled brush type %d\n", brush->bt);
112 argb = 0;
113 break;
115 return ARGB2COLORREF(argb);
118 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
120 HBITMAP hbmp;
121 BITMAPINFOHEADER bmih;
122 DWORD *bits;
123 int x, y;
125 bmih.biSize = sizeof(bmih);
126 bmih.biWidth = 8;
127 bmih.biHeight = 8;
128 bmih.biPlanes = 1;
129 bmih.biBitCount = 32;
130 bmih.biCompression = BI_RGB;
131 bmih.biSizeImage = 0;
133 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
134 if (hbmp)
136 const char *hatch_data;
138 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
140 for (y = 0; y < 8; y++)
142 for (x = 0; x < 8; x++)
144 if (hatch_data[y] & (0x80 >> x))
145 bits[y * 8 + x] = hatch->forecol;
146 else
147 bits[y * 8 + x] = hatch->backcol;
151 else
153 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
155 for (y = 0; y < 64; y++)
156 bits[y] = hatch->forecol;
160 return hbmp;
163 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
165 switch (brush->bt)
167 case BrushTypeSolidColor:
169 const GpSolidFill *sf = (const GpSolidFill *)brush;
170 lb->lbStyle = BS_SOLID;
171 lb->lbColor = ARGB2COLORREF(sf->color);
172 lb->lbHatch = 0;
173 return Ok;
176 case BrushTypeHatchFill:
178 const GpHatch *hatch = (const GpHatch *)brush;
179 HBITMAP hbmp;
181 hbmp = create_hatch_bitmap(hatch);
182 if (!hbmp) return OutOfMemory;
184 lb->lbStyle = BS_PATTERN;
185 lb->lbColor = 0;
186 lb->lbHatch = (ULONG_PTR)hbmp;
187 return Ok;
190 default:
191 FIXME("unhandled brush type %d\n", brush->bt);
192 lb->lbStyle = BS_SOLID;
193 lb->lbColor = get_gdi_brush_color(brush);
194 lb->lbHatch = 0;
195 return Ok;
199 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
201 switch (lb->lbStyle)
203 case BS_PATTERN:
204 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
205 break;
207 return Ok;
210 static HBRUSH create_gdi_brush(const GpBrush *brush)
212 LOGBRUSH lb;
213 HBRUSH gdibrush;
215 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
217 gdibrush = CreateBrushIndirect(&lb);
218 free_gdi_logbrush(&lb);
220 return gdibrush;
223 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
225 LOGBRUSH lb;
226 HPEN gdipen;
227 REAL width;
228 INT save_state, i, numdashes;
229 GpPointF pt[2];
230 DWORD dash_array[MAX_DASHLEN];
232 save_state = SaveDC(graphics->hdc);
234 EndPath(graphics->hdc);
236 if(pen->unit == UnitPixel){
237 width = pen->width;
239 else{
240 /* Get an estimate for the amount the pen width is affected by the world
241 * transform. (This is similar to what some of the wine drivers do.) */
242 pt[0].X = 0.0;
243 pt[0].Y = 0.0;
244 pt[1].X = 1.0;
245 pt[1].Y = 1.0;
246 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
247 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
248 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
250 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
251 width *= graphics->scale;
253 pt[0].X = 0.0;
254 pt[0].Y = 0.0;
255 pt[1].X = 1.0;
256 pt[1].Y = 1.0;
257 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, pt, 2);
258 width *= sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
259 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
262 if(pen->dash == DashStyleCustom){
263 numdashes = min(pen->numdashes, MAX_DASHLEN);
265 TRACE("dashes are: ");
266 for(i = 0; i < numdashes; i++){
267 dash_array[i] = gdip_round(width * pen->dashes[i]);
268 TRACE("%d, ", dash_array[i]);
270 TRACE("\n and the pen style is %x\n", pen->style);
272 create_gdi_logbrush(pen->brush, &lb);
273 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
274 numdashes, dash_array);
275 free_gdi_logbrush(&lb);
277 else
279 create_gdi_logbrush(pen->brush, &lb);
280 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
281 free_gdi_logbrush(&lb);
284 SelectObject(graphics->hdc, gdipen);
286 return save_state;
289 static void restore_dc(GpGraphics *graphics, INT state)
291 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
292 RestoreDC(graphics->hdc, state);
295 static void round_points(POINT *pti, GpPointF *ptf, INT count)
297 int i;
299 for(i = 0; i < count; i++){
300 pti[i].x = gdip_round(ptf[i].X);
301 pti[i].y = gdip_round(ptf[i].Y);
305 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
306 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
308 if (GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
309 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
311 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
313 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
314 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
316 else
318 BLENDFUNCTION bf;
320 bf.BlendOp = AC_SRC_OVER;
321 bf.BlendFlags = 0;
322 bf.SourceConstantAlpha = 255;
323 bf.AlphaFormat = AC_SRC_ALPHA;
325 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
326 hdc, src_x, src_y, src_width, src_height, bf);
330 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
332 GpRegion *rgn;
333 GpMatrix transform;
334 GpStatus stat;
336 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, &transform);
338 if (stat == Ok)
339 stat = GdipCloneRegion(graphics->clip, &rgn);
341 if (stat == Ok)
343 stat = GdipTransformRegion(rgn, &transform);
345 if (stat == Ok)
346 stat = GdipGetRegionHRgn(rgn, NULL, hrgn);
348 GdipDeleteRegion(rgn);
351 return stat;
354 /* Draw ARGB data to the given graphics object */
355 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
356 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
358 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
359 INT x, y;
361 for (y=0; y<src_height; y++)
363 for (x=0; x<src_width; x++)
365 ARGB dst_color, src_color;
366 src_color = ((ARGB*)(src + src_stride * y))[x];
368 if (!(src_color & 0xff000000))
369 continue;
371 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
372 if (fmt & PixelFormatPAlpha)
373 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
374 else
375 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
379 return Ok;
382 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
383 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
385 HDC hdc;
386 HBITMAP hbitmap;
387 BITMAPINFOHEADER bih;
388 BYTE *temp_bits;
390 hdc = CreateCompatibleDC(0);
392 bih.biSize = sizeof(BITMAPINFOHEADER);
393 bih.biWidth = src_width;
394 bih.biHeight = -src_height;
395 bih.biPlanes = 1;
396 bih.biBitCount = 32;
397 bih.biCompression = BI_RGB;
398 bih.biSizeImage = 0;
399 bih.biXPelsPerMeter = 0;
400 bih.biYPelsPerMeter = 0;
401 bih.biClrUsed = 0;
402 bih.biClrImportant = 0;
404 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
405 (void**)&temp_bits, NULL, 0);
407 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
408 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
409 fmt & PixelFormatPAlpha)
410 memcpy(temp_bits, src, src_width * src_height * 4);
411 else
412 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
413 4 * src_width, src, src_stride);
415 SelectObject(hdc, hbitmap);
416 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
417 hdc, 0, 0, src_width, src_height);
418 DeleteDC(hdc);
419 DeleteObject(hbitmap);
421 return Ok;
424 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
425 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
427 GpStatus stat=Ok;
429 if (graphics->image && graphics->image->type == ImageTypeBitmap)
431 DWORD i;
432 int size;
433 RGNDATA *rgndata;
434 RECT *rects;
435 HRGN hrgn, visible_rgn;
437 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
438 if (!hrgn)
439 return OutOfMemory;
441 stat = get_clip_hrgn(graphics, &visible_rgn);
442 if (stat != Ok)
444 DeleteObject(hrgn);
445 return stat;
448 if (visible_rgn)
450 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
451 DeleteObject(visible_rgn);
454 if (hregion)
455 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
457 size = GetRegionData(hrgn, 0, NULL);
459 rgndata = heap_alloc_zero(size);
460 if (!rgndata)
462 DeleteObject(hrgn);
463 return OutOfMemory;
466 GetRegionData(hrgn, size, rgndata);
468 rects = (RECT*)rgndata->Buffer;
470 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
472 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
473 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
474 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
475 src_stride, fmt);
478 heap_free(rgndata);
480 DeleteObject(hrgn);
482 return stat;
484 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
486 ERR("This should not be used for metafiles; fix caller\n");
487 return NotImplemented;
489 else
491 HRGN hrgn;
492 int save;
494 stat = get_clip_hrgn(graphics, &hrgn);
496 if (stat != Ok)
497 return stat;
499 save = SaveDC(graphics->hdc);
501 if (hrgn)
502 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
504 if (hregion)
505 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
507 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
508 src_height, src_stride, fmt);
510 RestoreDC(graphics->hdc, save);
512 DeleteObject(hrgn);
514 return stat;
518 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
519 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
521 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
524 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
526 INT start_a, end_a, final_a;
527 INT pos;
529 pos = gdip_round(position * 0xff);
531 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
532 end_a = ((end >> 24) & 0xff) * pos;
534 final_a = start_a + end_a;
536 if (final_a < 0xff) return 0;
538 return (final_a / 0xff) << 24 |
539 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
540 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
541 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
544 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
546 REAL blendfac;
548 /* clamp to between 0.0 and 1.0, using the wrap mode */
549 position = (position - brush->rect.X) / brush->rect.Width;
550 if (brush->wrap == WrapModeTile)
552 position = fmodf(position, 1.0f);
553 if (position < 0.0f) position += 1.0f;
555 else /* WrapModeFlip* */
557 position = fmodf(position, 2.0f);
558 if (position < 0.0f) position += 2.0f;
559 if (position > 1.0f) position = 2.0f - position;
562 if (brush->blendcount == 1)
563 blendfac = position;
564 else
566 int i=1;
567 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
568 REAL range;
570 /* locate the blend positions surrounding this position */
571 while (position > brush->blendpos[i])
572 i++;
574 /* interpolate between the blend positions */
575 left_blendpos = brush->blendpos[i-1];
576 left_blendfac = brush->blendfac[i-1];
577 right_blendpos = brush->blendpos[i];
578 right_blendfac = brush->blendfac[i];
579 range = right_blendpos - left_blendpos;
580 blendfac = (left_blendfac * (right_blendpos - position) +
581 right_blendfac * (position - left_blendpos)) / range;
584 if (brush->pblendcount == 0)
585 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
586 else
588 int i=1;
589 ARGB left_blendcolor, right_blendcolor;
590 REAL left_blendpos, right_blendpos;
592 /* locate the blend colors surrounding this position */
593 while (blendfac > brush->pblendpos[i])
594 i++;
596 /* interpolate between the blend colors */
597 left_blendpos = brush->pblendpos[i-1];
598 left_blendcolor = brush->pblendcolor[i-1];
599 right_blendpos = brush->pblendpos[i];
600 right_blendcolor = brush->pblendcolor[i];
601 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
602 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
606 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
608 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
609 BOOL identity = TRUE;
610 int i, j;
612 for (i=0; i<4; i++)
613 for (j=0; j<5; j++)
615 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
616 identity = FALSE;
617 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
620 return identity;
623 static ARGB transform_color(ARGB color, int matrix[5][5])
625 int val[5], res[4];
626 int i, j;
627 unsigned char a, r, g, b;
629 val[0] = ((color >> 16) & 0xff); /* red */
630 val[1] = ((color >> 8) & 0xff); /* green */
631 val[2] = (color & 0xff); /* blue */
632 val[3] = ((color >> 24) & 0xff); /* alpha */
633 val[4] = 255; /* translation */
635 for (i=0; i<4; i++)
637 res[i] = 0;
639 for (j=0; j<5; j++)
640 res[i] += matrix[j][i] * val[j];
643 a = min(max(res[3] / 256, 0), 255);
644 r = min(max(res[0] / 256, 0), 255);
645 g = min(max(res[1] / 256, 0), 255);
646 b = min(max(res[2] / 256, 0), 255);
648 return (a << 24) | (r << 16) | (g << 8) | b;
651 static BOOL color_is_gray(ARGB color)
653 unsigned char r, g, b;
655 r = (color >> 16) & 0xff;
656 g = (color >> 8) & 0xff;
657 b = color & 0xff;
659 return (r == g) && (g == b);
662 /* returns preferred pixel format for the applied attributes */
663 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
664 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
666 UINT x, y;
667 INT i;
669 if (attributes->colorkeys[type].enabled ||
670 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
672 const struct color_key *key;
673 BYTE min_blue, min_green, min_red;
674 BYTE max_blue, max_green, max_red;
676 if (!data || fmt != PixelFormat32bppARGB)
677 return PixelFormat32bppARGB;
679 if (attributes->colorkeys[type].enabled)
680 key = &attributes->colorkeys[type];
681 else
682 key = &attributes->colorkeys[ColorAdjustTypeDefault];
684 min_blue = key->low&0xff;
685 min_green = (key->low>>8)&0xff;
686 min_red = (key->low>>16)&0xff;
688 max_blue = key->high&0xff;
689 max_green = (key->high>>8)&0xff;
690 max_red = (key->high>>16)&0xff;
692 for (x=0; x<width; x++)
693 for (y=0; y<height; y++)
695 ARGB *src_color;
696 BYTE blue, green, red;
697 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
698 blue = *src_color&0xff;
699 green = (*src_color>>8)&0xff;
700 red = (*src_color>>16)&0xff;
701 if (blue >= min_blue && green >= min_green && red >= min_red &&
702 blue <= max_blue && green <= max_green && red <= max_red)
703 *src_color = 0x00000000;
707 if (attributes->colorremaptables[type].enabled ||
708 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
710 const struct color_remap_table *table;
712 if (!data || fmt != PixelFormat32bppARGB)
713 return PixelFormat32bppARGB;
715 if (attributes->colorremaptables[type].enabled)
716 table = &attributes->colorremaptables[type];
717 else
718 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
720 for (x=0; x<width; x++)
721 for (y=0; y<height; y++)
723 ARGB *src_color;
724 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
725 for (i=0; i<table->mapsize; i++)
727 if (*src_color == table->colormap[i].oldColor.Argb)
729 *src_color = table->colormap[i].newColor.Argb;
730 break;
736 if (attributes->colormatrices[type].enabled ||
737 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
739 const struct color_matrix *colormatrices;
740 int color_matrix[5][5];
741 int gray_matrix[5][5];
742 BOOL identity;
744 if (!data || fmt != PixelFormat32bppARGB)
745 return PixelFormat32bppARGB;
747 if (attributes->colormatrices[type].enabled)
748 colormatrices = &attributes->colormatrices[type];
749 else
750 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
752 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
754 if (colormatrices->flags == ColorMatrixFlagsAltGray)
755 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
757 if (!identity)
759 for (x=0; x<width; x++)
761 for (y=0; y<height; y++)
763 ARGB *src_color;
764 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
766 if (colormatrices->flags == ColorMatrixFlagsDefault ||
767 !color_is_gray(*src_color))
769 *src_color = transform_color(*src_color, color_matrix);
771 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
773 *src_color = transform_color(*src_color, gray_matrix);
780 if (attributes->gamma_enabled[type] ||
781 attributes->gamma_enabled[ColorAdjustTypeDefault])
783 REAL gamma;
785 if (!data || fmt != PixelFormat32bppARGB)
786 return PixelFormat32bppARGB;
788 if (attributes->gamma_enabled[type])
789 gamma = attributes->gamma[type];
790 else
791 gamma = attributes->gamma[ColorAdjustTypeDefault];
793 for (x=0; x<width; x++)
794 for (y=0; y<height; y++)
796 ARGB *src_color;
797 BYTE blue, green, red;
798 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
800 blue = *src_color&0xff;
801 green = (*src_color>>8)&0xff;
802 red = (*src_color>>16)&0xff;
804 /* FIXME: We should probably use a table for this. */
805 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
806 green = floorf(powf(green / 255.0, gamma) * 255.0);
807 red = floorf(powf(red / 255.0, gamma) * 255.0);
809 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
813 return fmt;
816 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
817 * bitmap that contains all the pixels we may need to draw it. */
818 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
819 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
820 GpRect *rect)
822 INT left, top, right, bottom;
824 switch (interpolation)
826 case InterpolationModeHighQualityBilinear:
827 case InterpolationModeHighQualityBicubic:
828 /* FIXME: Include a greater range for the prefilter? */
829 case InterpolationModeBicubic:
830 case InterpolationModeBilinear:
831 left = (INT)(floorf(srcx));
832 top = (INT)(floorf(srcy));
833 right = (INT)(ceilf(srcx+srcwidth));
834 bottom = (INT)(ceilf(srcy+srcheight));
835 break;
836 case InterpolationModeNearestNeighbor:
837 default:
838 left = gdip_round(srcx);
839 top = gdip_round(srcy);
840 right = gdip_round(srcx+srcwidth);
841 bottom = gdip_round(srcy+srcheight);
842 break;
845 if (wrap == WrapModeClamp)
847 if (left < 0)
848 left = 0;
849 if (top < 0)
850 top = 0;
851 if (right >= bitmap->width)
852 right = bitmap->width-1;
853 if (bottom >= bitmap->height)
854 bottom = bitmap->height-1;
855 if (bottom < top || right < left)
856 /* entirely outside image, just sample a pixel so we don't have to
857 * special-case this later */
858 left = top = right = bottom = 0;
860 else
862 /* In some cases we can make the rectangle smaller here, but the logic
863 * is hard to get right, and tiling suggests we're likely to use the
864 * entire source image. */
865 if (left < 0 || right >= bitmap->width)
867 left = 0;
868 right = bitmap->width-1;
871 if (top < 0 || bottom >= bitmap->height)
873 top = 0;
874 bottom = bitmap->height-1;
878 rect->X = left;
879 rect->Y = top;
880 rect->Width = right - left + 1;
881 rect->Height = bottom - top + 1;
884 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
885 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
887 if (attributes->wrap == WrapModeClamp)
889 if (x < 0 || y < 0 || x >= width || y >= height)
890 return attributes->outside_color;
892 else
894 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
895 if (x < 0)
896 x = width*2 + x % (width * 2);
897 if (y < 0)
898 y = height*2 + y % (height * 2);
900 if (attributes->wrap & WrapModeTileFlipX)
902 if ((x / width) % 2 == 0)
903 x = x % width;
904 else
905 x = width - 1 - x % width;
907 else
908 x = x % width;
910 if (attributes->wrap & WrapModeTileFlipY)
912 if ((y / height) % 2 == 0)
913 y = y % height;
914 else
915 y = height - 1 - y % height;
917 else
918 y = y % height;
921 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
923 ERR("out of range pixel requested\n");
924 return 0xffcd0084;
927 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
930 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
931 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
932 InterpolationMode interpolation, PixelOffsetMode offset_mode)
934 static int fixme;
936 switch (interpolation)
938 default:
939 if (!fixme++)
940 FIXME("Unimplemented interpolation %i\n", interpolation);
941 /* fall-through */
942 case InterpolationModeBilinear:
944 REAL leftxf, topyf;
945 INT leftx, rightx, topy, bottomy;
946 ARGB topleft, topright, bottomleft, bottomright;
947 ARGB top, bottom;
948 float x_offset;
950 leftxf = floorf(point->X);
951 leftx = (INT)leftxf;
952 rightx = (INT)ceilf(point->X);
953 topyf = floorf(point->Y);
954 topy = (INT)topyf;
955 bottomy = (INT)ceilf(point->Y);
957 if (leftx == rightx && topy == bottomy)
958 return sample_bitmap_pixel(src_rect, bits, width, height,
959 leftx, topy, attributes);
961 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
962 leftx, topy, attributes);
963 topright = sample_bitmap_pixel(src_rect, bits, width, height,
964 rightx, topy, attributes);
965 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
966 leftx, bottomy, attributes);
967 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
968 rightx, bottomy, attributes);
970 x_offset = point->X - leftxf;
971 top = blend_colors(topleft, topright, x_offset);
972 bottom = blend_colors(bottomleft, bottomright, x_offset);
974 return blend_colors(top, bottom, point->Y - topyf);
976 case InterpolationModeNearestNeighbor:
978 FLOAT pixel_offset;
979 switch (offset_mode)
981 default:
982 case PixelOffsetModeNone:
983 case PixelOffsetModeHighSpeed:
984 pixel_offset = 0.5;
985 break;
987 case PixelOffsetModeHalf:
988 case PixelOffsetModeHighQuality:
989 pixel_offset = 0.0;
990 break;
992 return sample_bitmap_pixel(src_rect, bits, width, height,
993 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
999 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1001 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1004 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1005 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1007 switch (brush->bt)
1009 case BrushTypeSolidColor:
1011 if (is_fill)
1012 return TRUE;
1013 else
1015 /* cannot draw semi-transparent colors */
1016 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1019 case BrushTypeHatchFill:
1021 GpHatch *hatch = (GpHatch*)brush;
1022 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1023 ((hatch->backcol & 0xff000000) == 0xff000000);
1025 case BrushTypeLinearGradient:
1026 case BrushTypeTextureFill:
1027 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1028 default:
1029 return FALSE;
1033 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1035 switch (brush->bt)
1037 case BrushTypeSolidColor:
1039 GpSolidFill *fill = (GpSolidFill*)brush;
1040 HBITMAP bmp = ARGB2BMP(fill->color);
1042 if (bmp)
1044 RECT rc;
1045 /* partially transparent fill */
1047 SelectClipPath(graphics->hdc, RGN_AND);
1048 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1050 HDC hdc = CreateCompatibleDC(NULL);
1052 if (!hdc) break;
1054 SelectObject(hdc, bmp);
1055 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1056 hdc, 0, 0, 1, 1);
1057 DeleteDC(hdc);
1060 DeleteObject(bmp);
1061 break;
1063 /* else fall through */
1065 default:
1067 HBRUSH gdibrush, old_brush;
1069 gdibrush = create_gdi_brush(brush);
1070 if (!gdibrush) return;
1072 old_brush = SelectObject(graphics->hdc, gdibrush);
1073 FillPath(graphics->hdc);
1074 SelectObject(graphics->hdc, old_brush);
1075 DeleteObject(gdibrush);
1076 break;
1081 static BOOL brush_can_fill_pixels(GpBrush *brush)
1083 switch (brush->bt)
1085 case BrushTypeSolidColor:
1086 case BrushTypeHatchFill:
1087 case BrushTypeLinearGradient:
1088 case BrushTypeTextureFill:
1089 case BrushTypePathGradient:
1090 return TRUE;
1091 default:
1092 return FALSE;
1096 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1097 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1099 switch (brush->bt)
1101 case BrushTypeSolidColor:
1103 int x, y;
1104 GpSolidFill *fill = (GpSolidFill*)brush;
1105 for (x=0; x<fill_area->Width; x++)
1106 for (y=0; y<fill_area->Height; y++)
1107 argb_pixels[x + y*cdwStride] = fill->color;
1108 return Ok;
1110 case BrushTypeHatchFill:
1112 int x, y;
1113 GpHatch *fill = (GpHatch*)brush;
1114 const char *hatch_data;
1116 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1117 return NotImplemented;
1119 for (x=0; x<fill_area->Width; x++)
1120 for (y=0; y<fill_area->Height; y++)
1122 int hx, hy;
1124 /* FIXME: Account for the rendering origin */
1125 hx = (x + fill_area->X) % 8;
1126 hy = (y + fill_area->Y) % 8;
1128 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1129 argb_pixels[x + y*cdwStride] = fill->forecol;
1130 else
1131 argb_pixels[x + y*cdwStride] = fill->backcol;
1134 return Ok;
1136 case BrushTypeLinearGradient:
1138 GpLineGradient *fill = (GpLineGradient*)brush;
1139 GpPointF draw_points[3];
1140 GpStatus stat;
1141 int x, y;
1143 draw_points[0].X = fill_area->X;
1144 draw_points[0].Y = fill_area->Y;
1145 draw_points[1].X = fill_area->X+1;
1146 draw_points[1].Y = fill_area->Y;
1147 draw_points[2].X = fill_area->X;
1148 draw_points[2].Y = fill_area->Y+1;
1150 /* Transform the points to a co-ordinate space where X is the point's
1151 * position in the gradient, 0.0 being the start point and 1.0 the
1152 * end point. */
1153 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1154 WineCoordinateSpaceGdiDevice, draw_points, 3);
1156 if (stat == Ok)
1158 GpMatrix world_to_gradient = fill->transform;
1160 stat = GdipInvertMatrix(&world_to_gradient);
1161 if (stat == Ok)
1162 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1165 if (stat == Ok)
1167 REAL x_delta = draw_points[1].X - draw_points[0].X;
1168 REAL y_delta = draw_points[2].X - draw_points[0].X;
1170 for (y=0; y<fill_area->Height; y++)
1172 for (x=0; x<fill_area->Width; x++)
1174 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1176 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1181 return stat;
1183 case BrushTypeTextureFill:
1185 GpTexture *fill = (GpTexture*)brush;
1186 GpPointF draw_points[3];
1187 GpStatus stat;
1188 int x, y;
1189 GpBitmap *bitmap;
1190 int src_stride;
1191 GpRect src_area;
1193 if (fill->image->type != ImageTypeBitmap)
1195 FIXME("metafile texture brushes not implemented\n");
1196 return NotImplemented;
1199 bitmap = (GpBitmap*)fill->image;
1200 src_stride = sizeof(ARGB) * bitmap->width;
1202 src_area.X = src_area.Y = 0;
1203 src_area.Width = bitmap->width;
1204 src_area.Height = bitmap->height;
1206 draw_points[0].X = fill_area->X;
1207 draw_points[0].Y = fill_area->Y;
1208 draw_points[1].X = fill_area->X+1;
1209 draw_points[1].Y = fill_area->Y;
1210 draw_points[2].X = fill_area->X;
1211 draw_points[2].Y = fill_area->Y+1;
1213 /* Transform the points to the co-ordinate space of the bitmap. */
1214 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1215 WineCoordinateSpaceGdiDevice, draw_points, 3);
1217 if (stat == Ok)
1219 GpMatrix world_to_texture = fill->transform;
1221 stat = GdipInvertMatrix(&world_to_texture);
1222 if (stat == Ok)
1223 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1226 if (stat == Ok && !fill->bitmap_bits)
1228 BitmapData lockeddata;
1230 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1231 if (!fill->bitmap_bits)
1232 stat = OutOfMemory;
1234 if (stat == Ok)
1236 lockeddata.Width = bitmap->width;
1237 lockeddata.Height = bitmap->height;
1238 lockeddata.Stride = src_stride;
1239 lockeddata.PixelFormat = PixelFormat32bppARGB;
1240 lockeddata.Scan0 = fill->bitmap_bits;
1242 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1243 PixelFormat32bppARGB, &lockeddata);
1246 if (stat == Ok)
1247 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1249 if (stat == Ok)
1250 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1251 bitmap->width, bitmap->height,
1252 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1254 if (stat != Ok)
1256 heap_free(fill->bitmap_bits);
1257 fill->bitmap_bits = NULL;
1261 if (stat == Ok)
1263 REAL x_dx = draw_points[1].X - draw_points[0].X;
1264 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1265 REAL y_dx = draw_points[2].X - draw_points[0].X;
1266 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1268 for (y=0; y<fill_area->Height; y++)
1270 for (x=0; x<fill_area->Width; x++)
1272 GpPointF point;
1273 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1274 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1276 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1277 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1278 &point, fill->imageattributes, graphics->interpolation,
1279 graphics->pixeloffset);
1284 return stat;
1286 case BrushTypePathGradient:
1288 GpPathGradient *fill = (GpPathGradient*)brush;
1289 GpPath *flat_path;
1290 GpMatrix world_to_device;
1291 GpStatus stat;
1292 int i, figure_start=0;
1293 GpPointF start_point, end_point, center_point;
1294 BYTE type;
1295 REAL min_yf, max_yf, line1_xf, line2_xf;
1296 INT min_y, max_y, min_x, max_x;
1297 INT x, y;
1298 ARGB outer_color;
1299 static BOOL transform_fixme_once;
1301 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1303 static int once;
1304 if (!once++)
1305 FIXME("path gradient focus not implemented\n");
1308 if (fill->gamma)
1310 static int once;
1311 if (!once++)
1312 FIXME("path gradient gamma correction not implemented\n");
1315 if (fill->blendcount)
1317 static int once;
1318 if (!once++)
1319 FIXME("path gradient blend not implemented\n");
1322 if (fill->pblendcount)
1324 static int once;
1325 if (!once++)
1326 FIXME("path gradient preset blend not implemented\n");
1329 if (!transform_fixme_once)
1331 BOOL is_identity=TRUE;
1332 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1333 if (!is_identity)
1335 FIXME("path gradient transform not implemented\n");
1336 transform_fixme_once = TRUE;
1340 stat = GdipClonePath(fill->path, &flat_path);
1342 if (stat != Ok)
1343 return stat;
1345 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1346 CoordinateSpaceWorld, &world_to_device);
1347 if (stat == Ok)
1349 stat = GdipTransformPath(flat_path, &world_to_device);
1351 if (stat == Ok)
1353 center_point = fill->center;
1354 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1357 if (stat == Ok)
1358 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1361 if (stat != Ok)
1363 GdipDeletePath(flat_path);
1364 return stat;
1367 for (i=0; i<flat_path->pathdata.Count; i++)
1369 int start_center_line=0, end_center_line=0;
1370 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1371 REAL center_distance;
1372 ARGB start_color, end_color;
1373 REAL dy, dx;
1375 type = flat_path->pathdata.Types[i];
1377 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1378 figure_start = i;
1380 start_point = flat_path->pathdata.Points[i];
1382 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1384 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1386 end_point = flat_path->pathdata.Points[figure_start];
1387 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1389 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1391 end_point = flat_path->pathdata.Points[i+1];
1392 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1394 else
1395 continue;
1397 outer_color = start_color;
1399 min_yf = center_point.Y;
1400 if (min_yf > start_point.Y) min_yf = start_point.Y;
1401 if (min_yf > end_point.Y) min_yf = end_point.Y;
1403 if (min_yf < fill_area->Y)
1404 min_y = fill_area->Y;
1405 else
1406 min_y = (INT)ceil(min_yf);
1408 max_yf = center_point.Y;
1409 if (max_yf < start_point.Y) max_yf = start_point.Y;
1410 if (max_yf < end_point.Y) max_yf = end_point.Y;
1412 if (max_yf > fill_area->Y + fill_area->Height)
1413 max_y = fill_area->Y + fill_area->Height;
1414 else
1415 max_y = (INT)ceil(max_yf);
1417 dy = end_point.Y - start_point.Y;
1418 dx = end_point.X - start_point.X;
1420 /* This is proportional to the distance from start-end line to center point. */
1421 center_distance = dy * (start_point.X - center_point.X) +
1422 dx * (center_point.Y - start_point.Y);
1424 for (y=min_y; y<max_y; y++)
1426 REAL yf = (REAL)y;
1428 if (!seen_start && yf >= start_point.Y)
1430 seen_start = TRUE;
1431 start_center_line ^= 1;
1433 if (!seen_end && yf >= end_point.Y)
1435 seen_end = TRUE;
1436 end_center_line ^= 1;
1438 if (!seen_center && yf >= center_point.Y)
1440 seen_center = TRUE;
1441 start_center_line ^= 1;
1442 end_center_line ^= 1;
1445 if (start_center_line)
1446 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1447 else
1448 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1450 if (end_center_line)
1451 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1452 else
1453 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1455 if (line1_xf < line2_xf)
1457 min_x = (INT)ceil(line1_xf);
1458 max_x = (INT)ceil(line2_xf);
1460 else
1462 min_x = (INT)ceil(line2_xf);
1463 max_x = (INT)ceil(line1_xf);
1466 if (min_x < fill_area->X)
1467 min_x = fill_area->X;
1468 if (max_x > fill_area->X + fill_area->Width)
1469 max_x = fill_area->X + fill_area->Width;
1471 for (x=min_x; x<max_x; x++)
1473 REAL xf = (REAL)x;
1474 REAL distance;
1476 if (start_color != end_color)
1478 REAL blend_amount, pdy, pdx;
1479 pdy = yf - center_point.Y;
1480 pdx = xf - center_point.X;
1482 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1484 /* Too close to center point, don't try to calculate outer color */
1485 outer_color = start_color;
1487 else
1489 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1490 outer_color = blend_colors(start_color, end_color, blend_amount);
1494 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1495 (end_point.X - start_point.X) * (yf - start_point.Y);
1497 distance = distance / center_distance;
1499 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1500 blend_colors(outer_color, fill->centercolor, distance);
1505 GdipDeletePath(flat_path);
1506 return stat;
1508 default:
1509 return NotImplemented;
1513 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1514 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1515 * should not be called on an hdc that has a path you care about. */
1516 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1517 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1519 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1520 GpMatrix matrix;
1521 HBRUSH brush = NULL;
1522 HPEN pen = NULL;
1523 PointF ptf[4], *custptf = NULL;
1524 POINT pt[4], *custpt = NULL;
1525 BYTE *tp = NULL;
1526 REAL theta, dsmall, dbig, dx, dy = 0.0;
1527 INT i, count;
1528 LOGBRUSH lb;
1529 BOOL customstroke;
1531 if((x1 == x2) && (y1 == y2))
1532 return;
1534 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1536 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1537 if(!customstroke){
1538 brush = CreateSolidBrush(color);
1539 lb.lbStyle = BS_SOLID;
1540 lb.lbColor = color;
1541 lb.lbHatch = 0;
1542 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1543 PS_JOIN_MITER, 1, &lb, 0,
1544 NULL);
1545 oldbrush = SelectObject(graphics->hdc, brush);
1546 oldpen = SelectObject(graphics->hdc, pen);
1549 switch(cap){
1550 case LineCapFlat:
1551 break;
1552 case LineCapSquare:
1553 case LineCapSquareAnchor:
1554 case LineCapDiamondAnchor:
1555 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1556 if(cap == LineCapDiamondAnchor){
1557 dsmall = cos(theta + M_PI_2) * size;
1558 dbig = sin(theta + M_PI_2) * size;
1560 else{
1561 dsmall = cos(theta + M_PI_4) * size;
1562 dbig = sin(theta + M_PI_4) * size;
1565 ptf[0].X = x2 - dsmall;
1566 ptf[1].X = x2 + dbig;
1568 ptf[0].Y = y2 - dbig;
1569 ptf[3].Y = y2 + dsmall;
1571 ptf[1].Y = y2 - dsmall;
1572 ptf[2].Y = y2 + dbig;
1574 ptf[3].X = x2 - dbig;
1575 ptf[2].X = x2 + dsmall;
1577 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1579 round_points(pt, ptf, 3);
1581 Polygon(graphics->hdc, pt, 4);
1583 break;
1584 case LineCapArrowAnchor:
1585 size = size * 4.0 / sqrt(3.0);
1587 dx = cos(M_PI / 6.0 + theta) * size;
1588 dy = sin(M_PI / 6.0 + theta) * size;
1590 ptf[0].X = x2 - dx;
1591 ptf[0].Y = y2 - dy;
1593 dx = cos(- M_PI / 6.0 + theta) * size;
1594 dy = sin(- M_PI / 6.0 + theta) * size;
1596 ptf[1].X = x2 - dx;
1597 ptf[1].Y = y2 - dy;
1599 ptf[2].X = x2;
1600 ptf[2].Y = y2;
1602 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1604 round_points(pt, ptf, 3);
1606 Polygon(graphics->hdc, pt, 3);
1608 break;
1609 case LineCapRoundAnchor:
1610 dx = dy = ANCHOR_WIDTH * size / 2.0;
1612 ptf[0].X = x2 - dx;
1613 ptf[0].Y = y2 - dy;
1614 ptf[1].X = x2 + dx;
1615 ptf[1].Y = y2 + dy;
1617 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1619 round_points(pt, ptf, 3);
1621 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1623 break;
1624 case LineCapTriangle:
1625 size = size / 2.0;
1626 dx = cos(M_PI_2 + theta) * size;
1627 dy = sin(M_PI_2 + theta) * size;
1629 ptf[0].X = x2 - dx;
1630 ptf[0].Y = y2 - dy;
1631 ptf[1].X = x2 + dx;
1632 ptf[1].Y = y2 + dy;
1634 dx = cos(theta) * size;
1635 dy = sin(theta) * size;
1637 ptf[2].X = x2 + dx;
1638 ptf[2].Y = y2 + dy;
1640 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1642 round_points(pt, ptf, 3);
1644 Polygon(graphics->hdc, pt, 3);
1646 break;
1647 case LineCapRound:
1648 dx = dy = size / 2.0;
1650 ptf[0].X = x2 - dx;
1651 ptf[0].Y = y2 - dy;
1652 ptf[1].X = x2 + dx;
1653 ptf[1].Y = y2 + dy;
1655 dx = -cos(M_PI_2 + theta) * size;
1656 dy = -sin(M_PI_2 + theta) * size;
1658 ptf[2].X = x2 - dx;
1659 ptf[2].Y = y2 - dy;
1660 ptf[3].X = x2 + dx;
1661 ptf[3].Y = y2 + dy;
1663 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1665 round_points(pt, ptf, 3);
1667 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1668 pt[2].y, pt[3].x, pt[3].y);
1670 break;
1671 case LineCapCustom:
1672 if(!custom)
1673 break;
1675 count = custom->pathdata.Count;
1676 custptf = heap_alloc_zero(count * sizeof(PointF));
1677 custpt = heap_alloc_zero(count * sizeof(POINT));
1678 tp = heap_alloc_zero(count);
1680 if(!custptf || !custpt || !tp)
1681 goto custend;
1683 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1685 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1686 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1687 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1688 MatrixOrderAppend);
1689 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1690 GdipTransformMatrixPoints(&matrix, custptf, count);
1692 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1694 round_points(pt, ptf, 3);
1696 for(i = 0; i < count; i++)
1697 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1699 if(custom->fill){
1700 BeginPath(graphics->hdc);
1701 PolyDraw(graphics->hdc, custpt, tp, count);
1702 EndPath(graphics->hdc);
1703 StrokeAndFillPath(graphics->hdc);
1705 else
1706 PolyDraw(graphics->hdc, custpt, tp, count);
1708 custend:
1709 heap_free(custptf);
1710 heap_free(custpt);
1711 heap_free(tp);
1712 break;
1713 default:
1714 break;
1717 if(!customstroke){
1718 SelectObject(graphics->hdc, oldbrush);
1719 SelectObject(graphics->hdc, oldpen);
1720 DeleteObject(brush);
1721 DeleteObject(pen);
1725 /* Shortens the line by the given percent by changing x2, y2.
1726 * If percent is > 1.0 then the line will change direction.
1727 * If percent is negative it can lengthen the line. */
1728 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1730 REAL dist, theta, dx, dy;
1732 if((y1 == *y2) && (x1 == *x2))
1733 return;
1735 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1736 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1737 dx = cos(theta) * dist;
1738 dy = sin(theta) * dist;
1740 *x2 = *x2 + dx;
1741 *y2 = *y2 + dy;
1744 /* Shortens the line by the given amount by changing x2, y2.
1745 * If the amount is greater than the distance, the line will become length 0.
1746 * If the amount is negative, it can lengthen the line. */
1747 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1749 REAL dx, dy, percent;
1751 dx = *x2 - x1;
1752 dy = *y2 - y1;
1753 if(dx == 0 && dy == 0)
1754 return;
1756 percent = amt / sqrt(dx * dx + dy * dy);
1757 if(percent >= 1.0){
1758 *x2 = x1;
1759 *y2 = y1;
1760 return;
1763 shorten_line_percent(x1, y1, x2, y2, percent);
1766 /* Conducts a linear search to find the bezier points that will back off
1767 * the endpoint of the curve by a distance of amt. Linear search works
1768 * better than binary in this case because there are multiple solutions,
1769 * and binary searches often find a bad one. I don't think this is what
1770 * Windows does but short of rendering the bezier without GDI's help it's
1771 * the best we can do. If rev then work from the start of the passed points
1772 * instead of the end. */
1773 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1775 GpPointF origpt[4];
1776 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1777 INT i, first = 0, second = 1, third = 2, fourth = 3;
1779 if(rev){
1780 first = 3;
1781 second = 2;
1782 third = 1;
1783 fourth = 0;
1786 origx = pt[fourth].X;
1787 origy = pt[fourth].Y;
1788 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1790 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1791 /* reset bezier points to original values */
1792 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1793 /* Perform magic on bezier points. Order is important here.*/
1794 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1795 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1796 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1797 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1798 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1799 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1801 dx = pt[fourth].X - origx;
1802 dy = pt[fourth].Y - origy;
1804 diff = sqrt(dx * dx + dy * dy);
1805 percent += 0.0005 * amt;
1809 /* Draws a combination of bezier curves and lines between points. */
1810 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1811 GDIPCONST BYTE * types, INT count, BOOL caps)
1813 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1814 BYTE *tp = heap_alloc_zero(count);
1815 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1816 INT i, j;
1817 GpStatus status = GenericError;
1819 if(!count){
1820 status = Ok;
1821 goto end;
1823 if(!pti || !tp || !ptcopy){
1824 status = OutOfMemory;
1825 goto end;
1828 for(i = 1; i < count; i++){
1829 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1830 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1831 || !(types[i + 2] & PathPointTypeBezier)){
1832 ERR("Bad bezier points\n");
1833 goto end;
1835 i += 2;
1839 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1841 /* If we are drawing caps, go through the points and adjust them accordingly,
1842 * and draw the caps. */
1843 if(caps){
1844 switch(types[count - 1] & PathPointTypePathTypeMask){
1845 case PathPointTypeBezier:
1846 if(pen->endcap == LineCapArrowAnchor)
1847 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1848 else if((pen->endcap == LineCapCustom) && pen->customend)
1849 shorten_bezier_amt(&ptcopy[count - 4],
1850 pen->width * pen->customend->inset, FALSE);
1852 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1853 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1854 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1855 pt[count - 1].X, pt[count - 1].Y);
1857 break;
1858 case PathPointTypeLine:
1859 if(pen->endcap == LineCapArrowAnchor)
1860 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1861 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1862 pen->width);
1863 else if((pen->endcap == LineCapCustom) && pen->customend)
1864 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1865 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1866 pen->customend->inset * pen->width);
1868 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1869 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1870 pt[count - 1].Y);
1872 break;
1873 default:
1874 ERR("Bad path last point\n");
1875 goto end;
1878 /* Find start of points */
1879 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1880 == PathPointTypeStart); j++);
1882 switch(types[j] & PathPointTypePathTypeMask){
1883 case PathPointTypeBezier:
1884 if(pen->startcap == LineCapArrowAnchor)
1885 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1886 else if((pen->startcap == LineCapCustom) && pen->customstart)
1887 shorten_bezier_amt(&ptcopy[j - 1],
1888 pen->width * pen->customstart->inset, TRUE);
1890 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1891 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1892 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1893 pt[j - 1].X, pt[j - 1].Y);
1895 break;
1896 case PathPointTypeLine:
1897 if(pen->startcap == LineCapArrowAnchor)
1898 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1899 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1900 pen->width);
1901 else if((pen->startcap == LineCapCustom) && pen->customstart)
1902 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1903 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1904 pen->customstart->inset * pen->width);
1906 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1907 pt[j].X, pt[j].Y, pt[j - 1].X,
1908 pt[j - 1].Y);
1910 break;
1911 default:
1912 ERR("Bad path points\n");
1913 goto end;
1917 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
1919 round_points(pti, ptcopy, count);
1921 for(i = 0; i < count; i++){
1922 tp[i] = convert_path_point_type(types[i]);
1925 PolyDraw(graphics->hdc, pti, tp, count);
1927 status = Ok;
1929 end:
1930 heap_free(pti);
1931 heap_free(ptcopy);
1932 heap_free(tp);
1934 return status;
1937 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1939 GpStatus result;
1941 BeginPath(graphics->hdc);
1942 result = draw_poly(graphics, NULL, path->pathdata.Points,
1943 path->pathdata.Types, path->pathdata.Count, FALSE);
1944 EndPath(graphics->hdc);
1945 return result;
1948 typedef enum GraphicsContainerType {
1949 BEGIN_CONTAINER,
1950 SAVE_GRAPHICS
1951 } GraphicsContainerType;
1953 typedef struct _GraphicsContainerItem {
1954 struct list entry;
1955 GraphicsContainer contid;
1956 GraphicsContainerType type;
1958 SmoothingMode smoothing;
1959 CompositingQuality compqual;
1960 InterpolationMode interpolation;
1961 CompositingMode compmode;
1962 TextRenderingHint texthint;
1963 REAL scale;
1964 GpUnit unit;
1965 PixelOffsetMode pixeloffset;
1966 UINT textcontrast;
1967 GpMatrix worldtrans;
1968 GpRegion* clip;
1969 INT origin_x, origin_y;
1970 } GraphicsContainerItem;
1972 static GpStatus init_container(GraphicsContainerItem** container,
1973 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
1974 GpStatus sts;
1976 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
1977 if(!(*container))
1978 return OutOfMemory;
1980 (*container)->contid = graphics->contid + 1;
1981 (*container)->type = type;
1983 (*container)->smoothing = graphics->smoothing;
1984 (*container)->compqual = graphics->compqual;
1985 (*container)->interpolation = graphics->interpolation;
1986 (*container)->compmode = graphics->compmode;
1987 (*container)->texthint = graphics->texthint;
1988 (*container)->scale = graphics->scale;
1989 (*container)->unit = graphics->unit;
1990 (*container)->textcontrast = graphics->textcontrast;
1991 (*container)->pixeloffset = graphics->pixeloffset;
1992 (*container)->origin_x = graphics->origin_x;
1993 (*container)->origin_y = graphics->origin_y;
1994 (*container)->worldtrans = graphics->worldtrans;
1996 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1997 if(sts != Ok){
1998 heap_free(*container);
1999 *container = NULL;
2000 return sts;
2003 return Ok;
2006 static void delete_container(GraphicsContainerItem* container)
2008 GdipDeleteRegion(container->clip);
2009 heap_free(container);
2012 static GpStatus restore_container(GpGraphics* graphics,
2013 GDIPCONST GraphicsContainerItem* container){
2014 GpStatus sts;
2015 GpRegion *newClip;
2017 sts = GdipCloneRegion(container->clip, &newClip);
2018 if(sts != Ok) return sts;
2020 graphics->worldtrans = container->worldtrans;
2022 GdipDeleteRegion(graphics->clip);
2023 graphics->clip = newClip;
2025 graphics->contid = container->contid - 1;
2027 graphics->smoothing = container->smoothing;
2028 graphics->compqual = container->compqual;
2029 graphics->interpolation = container->interpolation;
2030 graphics->compmode = container->compmode;
2031 graphics->texthint = container->texthint;
2032 graphics->scale = container->scale;
2033 graphics->unit = container->unit;
2034 graphics->textcontrast = container->textcontrast;
2035 graphics->pixeloffset = container->pixeloffset;
2036 graphics->origin_x = container->origin_x;
2037 graphics->origin_y = container->origin_y;
2039 return Ok;
2042 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2044 RECT wnd_rect;
2045 GpStatus stat=Ok;
2046 GpUnit unit;
2048 if(graphics->hwnd) {
2049 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2050 return GenericError;
2052 rect->X = wnd_rect.left;
2053 rect->Y = wnd_rect.top;
2054 rect->Width = wnd_rect.right - wnd_rect.left;
2055 rect->Height = wnd_rect.bottom - wnd_rect.top;
2056 }else if (graphics->image){
2057 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2058 if (stat == Ok && unit != UnitPixel)
2059 FIXME("need to convert from unit %i\n", unit);
2060 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2061 HBITMAP hbmp;
2062 BITMAP bmp;
2064 rect->X = 0;
2065 rect->Y = 0;
2067 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2068 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2070 rect->Width = bmp.bmWidth;
2071 rect->Height = bmp.bmHeight;
2073 else
2075 /* FIXME: ??? */
2076 rect->Width = 1;
2077 rect->Height = 1;
2079 }else{
2080 rect->X = 0;
2081 rect->Y = 0;
2082 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2083 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2086 return stat;
2089 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2091 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2093 if (stat == Ok && graphics->hdc)
2095 GpPointF points[4], min_point, max_point;
2096 int i;
2098 points[0].X = points[2].X = rect->X;
2099 points[0].Y = points[1].Y = rect->Y;
2100 points[1].X = points[3].X = rect->X + rect->Width;
2101 points[2].Y = points[3].Y = rect->Y + rect->Height;
2103 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2105 min_point = max_point = points[0];
2107 for (i=1; i<4; i++)
2109 if (points[i].X < min_point.X) min_point.X = points[i].X;
2110 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2111 if (points[i].X > max_point.X) max_point.X = points[i].X;
2112 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2115 rect->X = min_point.X;
2116 rect->Y = min_point.Y;
2117 rect->Width = max_point.X - min_point.X;
2118 rect->Height = max_point.Y - min_point.Y;
2121 return stat;
2124 /* on success, rgn will contain the region of the graphics object which
2125 * is visible after clipping has been applied */
2126 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2128 GpStatus stat;
2129 GpRectF rectf;
2130 GpRegion* tmp;
2132 /* Ignore graphics image bounds for metafiles */
2133 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2134 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2136 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2137 return stat;
2139 if((stat = GdipCreateRegion(&tmp)) != Ok)
2140 return stat;
2142 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2143 goto end;
2145 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2146 goto end;
2148 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2150 end:
2151 GdipDeleteRegion(tmp);
2152 return stat;
2155 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2157 REAL height;
2159 if (font->unit == UnitPixel)
2161 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2163 else
2165 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2166 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2167 else
2168 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2171 lf->lfHeight = -(height + 0.5);
2172 lf->lfWidth = 0;
2173 lf->lfEscapement = 0;
2174 lf->lfOrientation = 0;
2175 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2176 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2177 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2178 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2179 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2180 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2181 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2182 lf->lfQuality = DEFAULT_QUALITY;
2183 lf->lfPitchAndFamily = 0;
2184 strcpyW(lf->lfFaceName, font->family->FamilyName);
2187 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2188 GDIPCONST GpStringFormat *format, HFONT *hfont,
2189 GDIPCONST GpMatrix *matrix)
2191 HDC hdc = CreateCompatibleDC(0);
2192 GpPointF pt[3];
2193 REAL angle, rel_width, rel_height, font_height;
2194 LOGFONTW lfw;
2195 HFONT unscaled_font;
2196 TEXTMETRICW textmet;
2198 if (font->unit == UnitPixel || font->unit == UnitWorld)
2199 font_height = font->emSize;
2200 else
2202 REAL unit_scale, res;
2204 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2205 unit_scale = units_scale(font->unit, graphics->unit, res);
2207 font_height = font->emSize * unit_scale;
2210 pt[0].X = 0.0;
2211 pt[0].Y = 0.0;
2212 pt[1].X = 1.0;
2213 pt[1].Y = 0.0;
2214 pt[2].X = 0.0;
2215 pt[2].Y = 1.0;
2216 if (matrix)
2218 GpMatrix xform = *matrix;
2219 GdipTransformMatrixPoints(&xform, pt, 3);
2222 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2223 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2224 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2225 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2226 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2227 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2229 get_log_fontW(font, graphics, &lfw);
2230 lfw.lfHeight = -gdip_round(font_height * rel_height);
2231 unscaled_font = CreateFontIndirectW(&lfw);
2233 SelectObject(hdc, unscaled_font);
2234 GetTextMetricsW(hdc, &textmet);
2236 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2237 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2239 *hfont = CreateFontIndirectW(&lfw);
2241 DeleteDC(hdc);
2242 DeleteObject(unscaled_font);
2245 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2247 TRACE("(%p, %p)\n", hdc, graphics);
2249 return GdipCreateFromHDC2(hdc, NULL, graphics);
2252 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2254 GpStatus retval;
2255 HBITMAP hbitmap;
2256 DIBSECTION dib;
2258 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2260 if(hDevice != NULL)
2261 FIXME("Don't know how to handle parameter hDevice\n");
2263 if(hdc == NULL)
2264 return OutOfMemory;
2266 if(graphics == NULL)
2267 return InvalidParameter;
2269 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2270 if(!*graphics) return OutOfMemory;
2272 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2274 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2275 heap_free(*graphics);
2276 return retval;
2279 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2280 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2281 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2283 (*graphics)->alpha_hdc = 1;
2286 (*graphics)->hdc = hdc;
2287 (*graphics)->hwnd = WindowFromDC(hdc);
2288 (*graphics)->owndc = FALSE;
2289 (*graphics)->smoothing = SmoothingModeDefault;
2290 (*graphics)->compqual = CompositingQualityDefault;
2291 (*graphics)->interpolation = InterpolationModeBilinear;
2292 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2293 (*graphics)->compmode = CompositingModeSourceOver;
2294 (*graphics)->unit = UnitDisplay;
2295 (*graphics)->scale = 1.0;
2296 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2297 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2298 (*graphics)->busy = FALSE;
2299 (*graphics)->textcontrast = 4;
2300 list_init(&(*graphics)->containers);
2301 (*graphics)->contid = 0;
2303 TRACE("<-- %p\n", *graphics);
2305 return Ok;
2308 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2310 GpStatus retval;
2312 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2313 if(!*graphics) return OutOfMemory;
2315 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2317 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2318 heap_free(*graphics);
2319 return retval;
2322 (*graphics)->hdc = NULL;
2323 (*graphics)->hwnd = NULL;
2324 (*graphics)->owndc = FALSE;
2325 (*graphics)->image = image;
2326 /* We have to store the image type here because the image may be freed
2327 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2328 (*graphics)->image_type = image->type;
2329 (*graphics)->smoothing = SmoothingModeDefault;
2330 (*graphics)->compqual = CompositingQualityDefault;
2331 (*graphics)->interpolation = InterpolationModeBilinear;
2332 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2333 (*graphics)->compmode = CompositingModeSourceOver;
2334 (*graphics)->unit = UnitDisplay;
2335 (*graphics)->scale = 1.0;
2336 (*graphics)->xres = image->xres;
2337 (*graphics)->yres = image->yres;
2338 (*graphics)->busy = FALSE;
2339 (*graphics)->textcontrast = 4;
2340 list_init(&(*graphics)->containers);
2341 (*graphics)->contid = 0;
2343 TRACE("<-- %p\n", *graphics);
2345 return Ok;
2348 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2350 GpStatus ret;
2351 HDC hdc;
2353 TRACE("(%p, %p)\n", hwnd, graphics);
2355 hdc = GetDC(hwnd);
2357 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2359 ReleaseDC(hwnd, hdc);
2360 return ret;
2363 (*graphics)->hwnd = hwnd;
2364 (*graphics)->owndc = TRUE;
2366 return Ok;
2369 /* FIXME: no icm handling */
2370 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2372 TRACE("(%p, %p)\n", hwnd, graphics);
2374 return GdipCreateFromHWND(hwnd, graphics);
2377 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2378 UINT access, IStream **stream)
2380 DWORD dwMode;
2381 HRESULT ret;
2383 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2385 if(!stream || !filename)
2386 return InvalidParameter;
2388 if(access & GENERIC_WRITE)
2389 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2390 else if(access & GENERIC_READ)
2391 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2392 else
2393 return InvalidParameter;
2395 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2397 return hresult_to_status(ret);
2400 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2402 GraphicsContainerItem *cont, *next;
2403 GpStatus stat;
2404 TRACE("(%p)\n", graphics);
2406 if(!graphics) return InvalidParameter;
2407 if(graphics->busy) return ObjectBusy;
2409 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2411 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2412 if (stat != Ok)
2413 return stat;
2416 if(graphics->owndc)
2417 ReleaseDC(graphics->hwnd, graphics->hdc);
2419 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2420 list_remove(&cont->entry);
2421 delete_container(cont);
2424 GdipDeleteRegion(graphics->clip);
2426 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2427 * do otherwise, but we can't have that in the test suite because it means
2428 * accessing freed memory. */
2429 graphics->busy = TRUE;
2431 heap_free(graphics);
2433 return Ok;
2436 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2437 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2439 GpStatus status;
2440 GpPath *path;
2442 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2443 width, height, startAngle, sweepAngle);
2445 if(!graphics || !pen || width <= 0 || height <= 0)
2446 return InvalidParameter;
2448 if(graphics->busy)
2449 return ObjectBusy;
2451 status = GdipCreatePath(FillModeAlternate, &path);
2452 if (status != Ok) return status;
2454 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2455 if (status == Ok)
2456 status = GdipDrawPath(graphics, pen, path);
2458 GdipDeletePath(path);
2459 return status;
2462 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2463 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2465 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2466 width, height, startAngle, sweepAngle);
2468 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2471 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2472 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2474 GpPointF pt[4];
2476 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2477 x2, y2, x3, y3, x4, y4);
2479 if(!graphics || !pen)
2480 return InvalidParameter;
2482 if(graphics->busy)
2483 return ObjectBusy;
2485 pt[0].X = x1;
2486 pt[0].Y = y1;
2487 pt[1].X = x2;
2488 pt[1].Y = y2;
2489 pt[2].X = x3;
2490 pt[2].Y = y3;
2491 pt[3].X = x4;
2492 pt[3].Y = y4;
2493 return GdipDrawBeziers(graphics, pen, pt, 4);
2496 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2497 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2499 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2500 x2, y2, x3, y3, x4, y4);
2502 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2505 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2506 GDIPCONST GpPointF *points, INT count)
2508 GpStatus status;
2509 GpPath *path;
2511 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2513 if(!graphics || !pen || !points || (count <= 0))
2514 return InvalidParameter;
2516 if(graphics->busy)
2517 return ObjectBusy;
2519 status = GdipCreatePath(FillModeAlternate, &path);
2520 if (status != Ok) return status;
2522 status = GdipAddPathBeziers(path, points, count);
2523 if (status == Ok)
2524 status = GdipDrawPath(graphics, pen, path);
2526 GdipDeletePath(path);
2527 return status;
2530 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2531 GDIPCONST GpPoint *points, INT count)
2533 GpPointF *pts;
2534 GpStatus ret;
2535 INT i;
2537 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2539 if(!graphics || !pen || !points || (count <= 0))
2540 return InvalidParameter;
2542 if(graphics->busy)
2543 return ObjectBusy;
2545 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2546 if(!pts)
2547 return OutOfMemory;
2549 for(i = 0; i < count; i++){
2550 pts[i].X = (REAL)points[i].X;
2551 pts[i].Y = (REAL)points[i].Y;
2554 ret = GdipDrawBeziers(graphics,pen,pts,count);
2556 heap_free(pts);
2558 return ret;
2561 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2562 GDIPCONST GpPointF *points, INT count)
2564 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2566 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2569 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2570 GDIPCONST GpPoint *points, INT count)
2572 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2574 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2577 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2578 GDIPCONST GpPointF *points, INT count, REAL tension)
2580 GpPath *path;
2581 GpStatus status;
2583 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2585 if(!graphics || !pen || !points || count <= 0)
2586 return InvalidParameter;
2588 if(graphics->busy)
2589 return ObjectBusy;
2591 status = GdipCreatePath(FillModeAlternate, &path);
2592 if (status != Ok) return status;
2594 status = GdipAddPathClosedCurve2(path, points, count, tension);
2595 if (status == Ok)
2596 status = GdipDrawPath(graphics, pen, path);
2598 GdipDeletePath(path);
2600 return status;
2603 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2604 GDIPCONST GpPoint *points, INT count, REAL tension)
2606 GpPointF *ptf;
2607 GpStatus stat;
2608 INT i;
2610 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2612 if(!points || count <= 0)
2613 return InvalidParameter;
2615 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2616 if(!ptf)
2617 return OutOfMemory;
2619 for(i = 0; i < count; i++){
2620 ptf[i].X = (REAL)points[i].X;
2621 ptf[i].Y = (REAL)points[i].Y;
2624 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2626 heap_free(ptf);
2628 return stat;
2631 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2632 GDIPCONST GpPointF *points, INT count)
2634 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2636 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2639 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2640 GDIPCONST GpPoint *points, INT count)
2642 GpPointF *pointsF;
2643 GpStatus ret;
2644 INT i;
2646 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2648 if(!points)
2649 return InvalidParameter;
2651 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2652 if(!pointsF)
2653 return OutOfMemory;
2655 for(i = 0; i < count; i++){
2656 pointsF[i].X = (REAL)points[i].X;
2657 pointsF[i].Y = (REAL)points[i].Y;
2660 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2661 heap_free(pointsF);
2663 return ret;
2666 /* Approximates cardinal spline with Bezier curves. */
2667 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2668 GDIPCONST GpPointF *points, INT count, REAL tension)
2670 GpPath *path;
2671 GpStatus status;
2673 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2675 if(!graphics || !pen)
2676 return InvalidParameter;
2678 if(graphics->busy)
2679 return ObjectBusy;
2681 if(count < 2)
2682 return InvalidParameter;
2684 status = GdipCreatePath(FillModeAlternate, &path);
2685 if (status != Ok) return status;
2687 status = GdipAddPathCurve2(path, points, count, tension);
2688 if (status == Ok)
2689 status = GdipDrawPath(graphics, pen, path);
2691 GdipDeletePath(path);
2692 return status;
2695 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2696 GDIPCONST GpPoint *points, INT count, REAL tension)
2698 GpPointF *pointsF;
2699 GpStatus ret;
2700 INT i;
2702 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2704 if(!points)
2705 return InvalidParameter;
2707 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2708 if(!pointsF)
2709 return OutOfMemory;
2711 for(i = 0; i < count; i++){
2712 pointsF[i].X = (REAL)points[i].X;
2713 pointsF[i].Y = (REAL)points[i].Y;
2716 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2717 heap_free(pointsF);
2719 return ret;
2722 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2723 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2724 REAL tension)
2726 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2728 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2729 return InvalidParameter;
2732 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2735 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2736 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2737 REAL tension)
2739 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2741 if(count < 0){
2742 return OutOfMemory;
2745 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2746 return InvalidParameter;
2749 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2752 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2753 REAL y, REAL width, REAL height)
2755 GpPath *path;
2756 GpStatus status;
2758 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2760 if(!graphics || !pen)
2761 return InvalidParameter;
2763 if(graphics->busy)
2764 return ObjectBusy;
2766 status = GdipCreatePath(FillModeAlternate, &path);
2767 if (status != Ok) return status;
2769 status = GdipAddPathEllipse(path, x, y, width, height);
2770 if (status == Ok)
2771 status = GdipDrawPath(graphics, pen, path);
2773 GdipDeletePath(path);
2774 return status;
2777 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2778 INT y, INT width, INT height)
2780 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2782 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2786 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2788 UINT width, height;
2790 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2792 if(!graphics || !image)
2793 return InvalidParameter;
2795 GdipGetImageWidth(image, &width);
2796 GdipGetImageHeight(image, &height);
2798 return GdipDrawImagePointRect(graphics, image, x, y,
2799 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2802 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2803 INT y)
2805 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2807 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2810 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2811 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2812 GpUnit srcUnit)
2814 GpPointF points[3];
2815 REAL scale_x, scale_y, width, height;
2817 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2819 if (!graphics || !image) return InvalidParameter;
2821 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2822 scale_x *= graphics->xres / image->xres;
2823 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2824 scale_y *= graphics->yres / image->yres;
2825 width = srcwidth * scale_x;
2826 height = srcheight * scale_y;
2828 points[0].X = points[2].X = x;
2829 points[0].Y = points[1].Y = y;
2830 points[1].X = x + width;
2831 points[2].Y = y + height;
2833 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2834 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2837 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2838 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2839 GpUnit srcUnit)
2841 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2844 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2845 GDIPCONST GpPointF *dstpoints, INT count)
2847 UINT width, height;
2849 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2851 if(!image)
2852 return InvalidParameter;
2854 GdipGetImageWidth(image, &width);
2855 GdipGetImageHeight(image, &height);
2857 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2858 width, height, UnitPixel, NULL, NULL, NULL);
2861 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2862 GDIPCONST GpPoint *dstpoints, INT count)
2864 GpPointF ptf[3];
2866 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2868 if (count != 3 || !dstpoints)
2869 return InvalidParameter;
2871 ptf[0].X = (REAL)dstpoints[0].X;
2872 ptf[0].Y = (REAL)dstpoints[0].Y;
2873 ptf[1].X = (REAL)dstpoints[1].X;
2874 ptf[1].Y = (REAL)dstpoints[1].Y;
2875 ptf[2].X = (REAL)dstpoints[2].X;
2876 ptf[2].Y = (REAL)dstpoints[2].Y;
2878 return GdipDrawImagePoints(graphics, image, ptf, count);
2881 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2882 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2884 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2885 return TRUE;
2888 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2889 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2890 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2891 DrawImageAbort callback, VOID * callbackData)
2893 GpPointF ptf[4];
2894 POINT pti[4];
2895 GpStatus stat;
2897 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2898 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2899 callbackData);
2901 if (count > 3)
2902 return NotImplemented;
2904 if(!graphics || !image || !points || count != 3)
2905 return InvalidParameter;
2907 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2908 debugstr_pointf(&points[2]));
2910 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2912 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
2913 image, points, count, srcx, srcy, srcwidth, srcheight,
2914 srcUnit, imageAttributes, callback, callbackData);
2917 memcpy(ptf, points, 3 * sizeof(GpPointF));
2919 /* Ensure source width/height is positive */
2920 if (srcwidth < 0)
2922 GpPointF tmp = ptf[1];
2923 srcx = srcx + srcwidth;
2924 srcwidth = -srcwidth;
2925 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2926 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2927 ptf[1] = ptf[0];
2928 ptf[0] = tmp;
2931 if (srcheight < 0)
2933 GpPointF tmp = ptf[2];
2934 srcy = srcy + srcheight;
2935 srcheight = -srcheight;
2936 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2937 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2938 ptf[2] = ptf[0];
2939 ptf[0] = tmp;
2942 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2943 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2944 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2945 return Ok;
2946 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
2947 round_points(pti, ptf, 4);
2949 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2950 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2952 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2953 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2954 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2955 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2956 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2958 if (image->type == ImageTypeBitmap)
2960 GpBitmap* bitmap = (GpBitmap*)image;
2961 BOOL do_resampling = FALSE;
2962 BOOL use_software = FALSE;
2964 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2965 graphics->xres, graphics->yres,
2966 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2967 graphics->scale, image->xres, image->yres, bitmap->format,
2968 imageAttributes ? imageAttributes->outside_color : 0);
2970 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2971 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2972 srcx < 0 || srcy < 0 ||
2973 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2974 do_resampling = TRUE;
2976 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
2977 (graphics->image && graphics->image->type == ImageTypeBitmap))
2978 use_software = TRUE;
2980 if (use_software)
2982 RECT dst_area;
2983 GpRectF graphics_bounds;
2984 GpRect src_area;
2985 int i, x, y, src_stride, dst_stride;
2986 GpMatrix dst_to_src;
2987 REAL m11, m12, m21, m22, mdx, mdy;
2988 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
2989 BitmapData lockeddata;
2990 InterpolationMode interpolation = graphics->interpolation;
2991 PixelOffsetMode offset_mode = graphics->pixeloffset;
2992 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2993 REAL x_dx, x_dy, y_dx, y_dy;
2994 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2996 if (!imageAttributes)
2997 imageAttributes = &defaultImageAttributes;
2999 dst_area.left = dst_area.right = pti[0].x;
3000 dst_area.top = dst_area.bottom = pti[0].y;
3001 for (i=1; i<4; i++)
3003 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3004 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3005 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3006 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3009 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3010 if (stat != Ok) return stat;
3012 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3013 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3014 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3015 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3017 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3019 if (IsRectEmpty(&dst_area)) return Ok;
3021 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3022 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3023 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3024 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3025 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3026 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3028 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3030 stat = GdipInvertMatrix(&dst_to_src);
3031 if (stat != Ok) return stat;
3033 if (do_resampling)
3035 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3036 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3038 else
3040 /* Make sure src_area is equal in size to dst_area. */
3041 src_area.X = srcx + dst_area.left - pti[0].x;
3042 src_area.Y = srcy + dst_area.top - pti[0].y;
3043 src_area.Width = dst_area.right - dst_area.left;
3044 src_area.Height = dst_area.bottom - dst_area.top;
3047 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3049 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3050 if (!src_data)
3051 return OutOfMemory;
3052 src_stride = sizeof(ARGB) * src_area.Width;
3054 /* Read the bits we need from the source bitmap into a compatible buffer. */
3055 lockeddata.Width = src_area.Width;
3056 lockeddata.Height = src_area.Height;
3057 lockeddata.Stride = src_stride;
3058 lockeddata.Scan0 = src_data;
3059 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3060 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3061 else
3062 lockeddata.PixelFormat = PixelFormat32bppARGB;
3064 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3065 lockeddata.PixelFormat, &lockeddata);
3067 if (stat == Ok)
3068 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3070 if (stat != Ok)
3072 heap_free(src_data);
3073 return stat;
3076 apply_image_attributes(imageAttributes, src_data,
3077 src_area.Width, src_area.Height,
3078 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3080 if (do_resampling)
3082 /* Transform the bits as needed to the destination. */
3083 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3084 if (!dst_data)
3086 heap_free(src_data);
3087 return OutOfMemory;
3090 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3092 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3094 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3095 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3096 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3097 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3099 for (x=dst_area.left; x<dst_area.right; x++)
3101 for (y=dst_area.top; y<dst_area.bottom; y++)
3103 GpPointF src_pointf;
3104 ARGB *dst_color;
3106 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3107 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3109 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3111 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3112 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3113 imageAttributes, interpolation, offset_mode);
3114 else
3115 *dst_color = 0;
3119 else
3121 dst_data = src_data;
3122 dst_stride = src_stride;
3125 gdi_transform_acquire(graphics);
3127 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3128 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3129 lockeddata.PixelFormat);
3131 gdi_transform_release(graphics);
3133 heap_free(src_data);
3135 heap_free(dst_dyn_data);
3137 return stat;
3139 else
3141 HDC hdc;
3142 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3143 HBITMAP hbitmap, old_hbm=NULL;
3144 HRGN hrgn;
3145 INT save_state;
3147 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3148 bitmap->format == PixelFormat24bppRGB ||
3149 bitmap->format == PixelFormat32bppRGB ||
3150 bitmap->format == PixelFormat32bppPARGB))
3152 BITMAPINFOHEADER bih;
3153 BYTE *temp_bits;
3154 PixelFormat dst_format;
3156 /* we can't draw a bitmap of this format directly */
3157 hdc = CreateCompatibleDC(0);
3158 temp_hdc = TRUE;
3159 temp_bitmap = TRUE;
3161 bih.biSize = sizeof(BITMAPINFOHEADER);
3162 bih.biWidth = bitmap->width;
3163 bih.biHeight = -bitmap->height;
3164 bih.biPlanes = 1;
3165 bih.biBitCount = 32;
3166 bih.biCompression = BI_RGB;
3167 bih.biSizeImage = 0;
3168 bih.biXPelsPerMeter = 0;
3169 bih.biYPelsPerMeter = 0;
3170 bih.biClrUsed = 0;
3171 bih.biClrImportant = 0;
3173 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3174 (void**)&temp_bits, NULL, 0);
3176 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3177 dst_format = PixelFormat32bppPARGB;
3178 else
3179 dst_format = PixelFormat32bppRGB;
3181 convert_pixels(bitmap->width, bitmap->height,
3182 bitmap->width*4, temp_bits, dst_format,
3183 bitmap->stride, bitmap->bits, bitmap->format,
3184 bitmap->image.palette);
3186 else
3188 if (bitmap->hbitmap)
3189 hbitmap = bitmap->hbitmap;
3190 else
3192 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3193 temp_bitmap = TRUE;
3196 hdc = bitmap->hdc;
3197 temp_hdc = (hdc == 0);
3200 if (temp_hdc)
3202 if (!hdc) hdc = CreateCompatibleDC(0);
3203 old_hbm = SelectObject(hdc, hbitmap);
3206 save_state = SaveDC(graphics->hdc);
3208 stat = get_clip_hrgn(graphics, &hrgn);
3210 if (stat == Ok && hrgn)
3212 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3213 DeleteObject(hrgn);
3216 gdi_transform_acquire(graphics);
3218 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3220 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3221 hdc, srcx, srcy, srcwidth, srcheight);
3223 else
3225 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3226 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3229 gdi_transform_release(graphics);
3231 RestoreDC(graphics->hdc, save_state);
3233 if (temp_hdc)
3235 SelectObject(hdc, old_hbm);
3236 DeleteDC(hdc);
3239 if (temp_bitmap)
3240 DeleteObject(hbitmap);
3243 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3245 GpRectF rc;
3247 rc.X = srcx;
3248 rc.Y = srcy;
3249 rc.Width = srcwidth;
3250 rc.Height = srcheight;
3252 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3253 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3255 else
3257 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3258 return InvalidParameter;
3261 return Ok;
3264 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3265 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3266 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3267 DrawImageAbort callback, VOID * callbackData)
3269 GpPointF pointsF[3];
3270 INT i;
3272 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3273 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3274 callbackData);
3276 if(!points || count!=3)
3277 return InvalidParameter;
3279 for(i = 0; i < count; i++){
3280 pointsF[i].X = (REAL)points[i].X;
3281 pointsF[i].Y = (REAL)points[i].Y;
3284 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3285 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3286 callback, callbackData);
3289 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3290 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3291 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3292 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3293 VOID * callbackData)
3295 GpPointF points[3];
3297 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3298 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3299 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3301 points[0].X = dstx;
3302 points[0].Y = dsty;
3303 points[1].X = dstx + dstwidth;
3304 points[1].Y = dsty;
3305 points[2].X = dstx;
3306 points[2].Y = dsty + dstheight;
3308 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3309 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3312 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3313 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3314 INT srcwidth, INT srcheight, GpUnit srcUnit,
3315 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3316 VOID * callbackData)
3318 GpPointF points[3];
3320 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3321 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3322 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3324 points[0].X = dstx;
3325 points[0].Y = dsty;
3326 points[1].X = dstx + dstwidth;
3327 points[1].Y = dsty;
3328 points[2].X = dstx;
3329 points[2].Y = dsty + dstheight;
3331 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3332 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3335 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3336 REAL x, REAL y, REAL width, REAL height)
3338 RectF bounds;
3339 GpUnit unit;
3340 GpStatus ret;
3342 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3344 if(!graphics || !image)
3345 return InvalidParameter;
3347 ret = GdipGetImageBounds(image, &bounds, &unit);
3348 if(ret != Ok)
3349 return ret;
3351 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3352 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3353 unit, NULL, NULL, NULL);
3356 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3357 INT x, INT y, INT width, INT height)
3359 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3361 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3364 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3365 REAL y1, REAL x2, REAL y2)
3367 GpPointF pt[2];
3369 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3371 if (!pen)
3372 return InvalidParameter;
3374 if (pen->unit == UnitPixel && pen->width <= 0.0)
3375 return Ok;
3377 pt[0].X = x1;
3378 pt[0].Y = y1;
3379 pt[1].X = x2;
3380 pt[1].Y = y2;
3381 return GdipDrawLines(graphics, pen, pt, 2);
3384 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3385 INT y1, INT x2, INT y2)
3387 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3389 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3392 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3393 GpPointF *points, INT count)
3395 GpStatus status;
3396 GpPath *path;
3398 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3400 if(!pen || !graphics || (count < 2))
3401 return InvalidParameter;
3403 if(graphics->busy)
3404 return ObjectBusy;
3406 status = GdipCreatePath(FillModeAlternate, &path);
3407 if (status != Ok) return status;
3409 status = GdipAddPathLine2(path, points, count);
3410 if (status == Ok)
3411 status = GdipDrawPath(graphics, pen, path);
3413 GdipDeletePath(path);
3414 return status;
3417 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3418 GpPoint *points, INT count)
3420 GpStatus retval;
3421 GpPointF *ptf;
3422 int i;
3424 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3426 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3427 if(!ptf) return OutOfMemory;
3429 for(i = 0; i < count; i ++){
3430 ptf[i].X = (REAL) points[i].X;
3431 ptf[i].Y = (REAL) points[i].Y;
3434 retval = GdipDrawLines(graphics, pen, ptf, count);
3436 heap_free(ptf);
3437 return retval;
3440 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3442 INT save_state;
3443 GpStatus retval;
3444 HRGN hrgn=NULL;
3446 save_state = prepare_dc(graphics, pen);
3448 retval = get_clip_hrgn(graphics, &hrgn);
3450 if (retval != Ok)
3451 goto end;
3453 if (hrgn)
3454 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3456 gdi_transform_acquire(graphics);
3458 retval = draw_poly(graphics, pen, path->pathdata.Points,
3459 path->pathdata.Types, path->pathdata.Count, TRUE);
3461 gdi_transform_release(graphics);
3463 end:
3464 restore_dc(graphics, save_state);
3465 DeleteObject(hrgn);
3467 return retval;
3470 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3472 GpStatus stat;
3473 GpPath* flat_path;
3474 GpMatrix* transform;
3475 GpRectF gp_bound_rect;
3476 GpRect gp_output_area;
3477 RECT output_area;
3478 INT output_height, output_width;
3479 DWORD *output_bits, *brush_bits=NULL;
3480 int i;
3481 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3482 const BYTE *dash_pattern;
3483 INT dash_pattern_size;
3484 BYTE *dyn_dash_pattern = NULL;
3486 stat = GdipClonePath(path, &flat_path);
3488 if (stat != Ok)
3489 return stat;
3491 stat = GdipCreateMatrix(&transform);
3493 if (stat == Ok)
3495 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3496 CoordinateSpaceWorld, transform);
3498 if (stat == Ok)
3499 stat = GdipFlattenPath(flat_path, transform, 1.0);
3501 GdipDeleteMatrix(transform);
3504 /* estimate the output size in pixels, can be larger than necessary */
3505 if (stat == Ok)
3507 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3508 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3509 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3510 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3512 for (i=1; i<flat_path->pathdata.Count; i++)
3514 REAL x, y;
3515 x = flat_path->pathdata.Points[i].X;
3516 y = flat_path->pathdata.Points[i].Y;
3518 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3519 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3520 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3521 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3524 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3527 if (stat == Ok)
3529 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3530 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3531 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3532 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3534 output_width = output_area.right - output_area.left + 1;
3535 output_height = output_area.bottom - output_area.top + 1;
3537 if (output_width <= 0 || output_height <= 0)
3539 GdipDeletePath(flat_path);
3540 return Ok;
3543 gp_output_area.X = output_area.left;
3544 gp_output_area.Y = output_area.top;
3545 gp_output_area.Width = output_width;
3546 gp_output_area.Height = output_height;
3548 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3549 if (!output_bits)
3550 stat = OutOfMemory;
3553 if (stat == Ok)
3555 if (pen->brush->bt != BrushTypeSolidColor)
3557 /* allocate and draw brush output */
3558 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3560 if (brush_bits)
3562 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3563 &gp_output_area, output_width);
3565 else
3566 stat = OutOfMemory;
3569 if (stat == Ok)
3571 /* convert dash pattern to bool array */
3572 switch (pen->dash)
3574 case DashStyleCustom:
3576 dash_pattern_size = 0;
3578 for (i=0; i < pen->numdashes; i++)
3579 dash_pattern_size += gdip_round(pen->dashes[i]);
3581 if (dash_pattern_size != 0)
3583 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3585 if (dyn_dash_pattern)
3587 int j=0;
3588 for (i=0; i < pen->numdashes; i++)
3590 int k;
3591 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3592 dyn_dash_pattern[j++] = (i&1)^1;
3595 else
3596 stat = OutOfMemory;
3598 break;
3600 /* else fall through */
3602 case DashStyleSolid:
3603 default:
3604 dash_pattern = static_dash_pattern;
3605 dash_pattern_size = 1;
3606 break;
3607 case DashStyleDash:
3608 dash_pattern = static_dash_pattern;
3609 dash_pattern_size = 4;
3610 break;
3611 case DashStyleDot:
3612 dash_pattern = &static_dash_pattern[4];
3613 dash_pattern_size = 2;
3614 break;
3615 case DashStyleDashDot:
3616 dash_pattern = static_dash_pattern;
3617 dash_pattern_size = 6;
3618 break;
3619 case DashStyleDashDotDot:
3620 dash_pattern = static_dash_pattern;
3621 dash_pattern_size = 8;
3622 break;
3626 if (stat == Ok)
3628 /* trace path */
3629 GpPointF subpath_start = flat_path->pathdata.Points[0];
3630 INT prev_x = INT_MAX, prev_y = INT_MAX;
3631 int dash_pos = dash_pattern_size - 1;
3633 for (i=0; i < flat_path->pathdata.Count; i++)
3635 BYTE type, type2;
3636 GpPointF start_point, end_point;
3637 GpPoint start_pointi, end_pointi;
3639 type = flat_path->pathdata.Types[i];
3640 if (i+1 < flat_path->pathdata.Count)
3641 type2 = flat_path->pathdata.Types[i+1];
3642 else
3643 type2 = PathPointTypeStart;
3645 start_point = flat_path->pathdata.Points[i];
3647 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3648 subpath_start = start_point;
3650 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3651 end_point = subpath_start;
3652 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3653 continue;
3654 else
3655 end_point = flat_path->pathdata.Points[i+1];
3657 start_pointi.X = floorf(start_point.X);
3658 start_pointi.Y = floorf(start_point.Y);
3659 end_pointi.X = floorf(end_point.X);
3660 end_pointi.Y = floorf(end_point.Y);
3662 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3663 continue;
3665 /* draw line segment */
3666 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3668 INT x, y, start_y, end_y, step;
3670 if (start_pointi.Y < end_pointi.Y)
3672 step = 1;
3673 start_y = ceilf(start_point.Y) - output_area.top;
3674 end_y = end_pointi.Y - output_area.top;
3676 else
3678 step = -1;
3679 start_y = start_point.Y - output_area.top;
3680 end_y = ceilf(end_point.Y) - output_area.top;
3683 for (y=start_y; y != (end_y+step); y+=step)
3685 x = gdip_round( start_point.X +
3686 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3687 - output_area.left;
3689 if (x == prev_x && y == prev_y)
3690 continue;
3692 prev_x = x;
3693 prev_y = y;
3694 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3696 if (!dash_pattern[dash_pos])
3697 continue;
3699 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3700 continue;
3702 if (brush_bits)
3703 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3704 else
3705 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3708 else
3710 INT x, y, start_x, end_x, step;
3712 if (start_pointi.X < end_pointi.X)
3714 step = 1;
3715 start_x = ceilf(start_point.X) - output_area.left;
3716 end_x = end_pointi.X - output_area.left;
3718 else
3720 step = -1;
3721 start_x = start_point.X - output_area.left;
3722 end_x = ceilf(end_point.X) - output_area.left;
3725 for (x=start_x; x != (end_x+step); x+=step)
3727 y = gdip_round( start_point.Y +
3728 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3729 - output_area.top;
3731 if (x == prev_x && y == prev_y)
3732 continue;
3734 prev_x = x;
3735 prev_y = y;
3736 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3738 if (!dash_pattern[dash_pos])
3739 continue;
3741 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3742 continue;
3744 if (brush_bits)
3745 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3746 else
3747 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3753 /* draw output image */
3754 if (stat == Ok)
3756 gdi_transform_acquire(graphics);
3758 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3759 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3760 PixelFormat32bppARGB);
3762 gdi_transform_release(graphics);
3765 heap_free(brush_bits);
3766 heap_free(dyn_dash_pattern);
3767 heap_free(output_bits);
3770 GdipDeletePath(flat_path);
3772 return stat;
3775 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3777 GpStatus stat;
3778 GpPath *wide_path;
3779 GpMatrix *transform=NULL;
3780 REAL flatness=1.0;
3782 /* Check if the final pen thickness in pixels is too thin. */
3783 if (pen->unit == UnitPixel)
3785 if (pen->width < 1.415)
3786 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3788 else
3790 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3792 points[1].X = pen->width;
3793 points[2].Y = pen->width;
3795 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3796 CoordinateSpaceWorld, points, 3);
3798 if (stat != Ok)
3799 return stat;
3801 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3802 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3803 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3804 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3805 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3808 stat = GdipClonePath(path, &wide_path);
3810 if (stat != Ok)
3811 return stat;
3813 if (pen->unit == UnitPixel)
3815 /* We have to transform this to device coordinates to get the widths right. */
3816 stat = GdipCreateMatrix(&transform);
3818 if (stat == Ok)
3819 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3820 CoordinateSpaceWorld, transform);
3822 else
3824 /* Set flatness based on the final coordinate space */
3825 GpMatrix t;
3827 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3828 CoordinateSpaceWorld, &t);
3830 if (stat != Ok)
3831 return stat;
3833 flatness = 1.0/sqrt(fmax(
3834 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
3835 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
3838 if (stat == Ok)
3839 stat = GdipWidenPath(wide_path, pen, transform, flatness);
3841 if (pen->unit == UnitPixel)
3843 /* Transform the path back to world coordinates */
3844 if (stat == Ok)
3845 stat = GdipInvertMatrix(transform);
3847 if (stat == Ok)
3848 stat = GdipTransformPath(wide_path, transform);
3851 /* Actually draw the path */
3852 if (stat == Ok)
3853 stat = GdipFillPath(graphics, pen->brush, wide_path);
3855 GdipDeleteMatrix(transform);
3857 GdipDeletePath(wide_path);
3859 return stat;
3862 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3864 GpStatus retval;
3866 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3868 if(!pen || !graphics)
3869 return InvalidParameter;
3871 if(graphics->busy)
3872 return ObjectBusy;
3874 if (path->pathdata.Count == 0)
3875 return Ok;
3877 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3878 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
3879 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
3880 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
3881 else
3882 retval = GDI32_GdipDrawPath(graphics, pen, path);
3884 return retval;
3887 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3888 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3890 GpStatus status;
3891 GpPath *path;
3893 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3894 width, height, startAngle, sweepAngle);
3896 if(!graphics || !pen)
3897 return InvalidParameter;
3899 if(graphics->busy)
3900 return ObjectBusy;
3902 status = GdipCreatePath(FillModeAlternate, &path);
3903 if (status != Ok) return status;
3905 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3906 if (status == Ok)
3907 status = GdipDrawPath(graphics, pen, path);
3909 GdipDeletePath(path);
3910 return status;
3913 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3914 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3916 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3917 width, height, startAngle, sweepAngle);
3919 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3922 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3923 REAL y, REAL width, REAL height)
3925 GpStatus status;
3926 GpPath *path;
3928 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3930 if(!pen || !graphics)
3931 return InvalidParameter;
3933 if(graphics->busy)
3934 return ObjectBusy;
3936 status = GdipCreatePath(FillModeAlternate, &path);
3937 if (status != Ok) return status;
3939 status = GdipAddPathRectangle(path, x, y, width, height);
3940 if (status == Ok)
3941 status = GdipDrawPath(graphics, pen, path);
3943 GdipDeletePath(path);
3944 return status;
3947 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3948 INT y, INT width, INT height)
3950 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3952 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3955 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3956 GDIPCONST GpRectF* rects, INT count)
3958 GpStatus status;
3959 GpPath *path;
3961 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3963 if(!graphics || !pen || !rects || count < 1)
3964 return InvalidParameter;
3966 if(graphics->busy)
3967 return ObjectBusy;
3969 status = GdipCreatePath(FillModeAlternate, &path);
3970 if (status != Ok) return status;
3972 status = GdipAddPathRectangles(path, rects, count);
3973 if (status == Ok)
3974 status = GdipDrawPath(graphics, pen, path);
3976 GdipDeletePath(path);
3977 return status;
3980 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3981 GDIPCONST GpRect* rects, INT count)
3983 GpRectF *rectsF;
3984 GpStatus ret;
3985 INT i;
3987 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3989 if(!rects || count<=0)
3990 return InvalidParameter;
3992 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
3993 if(!rectsF)
3994 return OutOfMemory;
3996 for(i = 0;i < count;i++){
3997 rectsF[i].X = (REAL)rects[i].X;
3998 rectsF[i].Y = (REAL)rects[i].Y;
3999 rectsF[i].Width = (REAL)rects[i].Width;
4000 rectsF[i].Height = (REAL)rects[i].Height;
4003 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4004 heap_free(rectsF);
4006 return ret;
4009 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4010 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4012 GpPath *path;
4013 GpStatus status;
4015 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4016 count, tension, fill);
4018 if(!graphics || !brush || !points)
4019 return InvalidParameter;
4021 if(graphics->busy)
4022 return ObjectBusy;
4024 if(count == 1) /* Do nothing */
4025 return Ok;
4027 status = GdipCreatePath(fill, &path);
4028 if (status != Ok) return status;
4030 status = GdipAddPathClosedCurve2(path, points, count, tension);
4031 if (status == Ok)
4032 status = GdipFillPath(graphics, brush, path);
4034 GdipDeletePath(path);
4035 return status;
4038 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4039 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4041 GpPointF *ptf;
4042 GpStatus stat;
4043 INT i;
4045 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4046 count, tension, fill);
4048 if(!points || count == 0)
4049 return InvalidParameter;
4051 if(count == 1) /* Do nothing */
4052 return Ok;
4054 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4055 if(!ptf)
4056 return OutOfMemory;
4058 for(i = 0;i < count;i++){
4059 ptf[i].X = (REAL)points[i].X;
4060 ptf[i].Y = (REAL)points[i].Y;
4063 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4065 heap_free(ptf);
4067 return stat;
4070 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4071 GDIPCONST GpPointF *points, INT count)
4073 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4074 return GdipFillClosedCurve2(graphics, brush, points, count,
4075 0.5f, FillModeAlternate);
4078 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4079 GDIPCONST GpPoint *points, INT count)
4081 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4082 return GdipFillClosedCurve2I(graphics, brush, points, count,
4083 0.5f, FillModeAlternate);
4086 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4087 REAL y, REAL width, REAL height)
4089 GpStatus stat;
4090 GpPath *path;
4092 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4094 if(!graphics || !brush)
4095 return InvalidParameter;
4097 if(graphics->busy)
4098 return ObjectBusy;
4100 stat = GdipCreatePath(FillModeAlternate, &path);
4102 if (stat == Ok)
4104 stat = GdipAddPathEllipse(path, x, y, width, height);
4106 if (stat == Ok)
4107 stat = GdipFillPath(graphics, brush, path);
4109 GdipDeletePath(path);
4112 return stat;
4115 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4116 INT y, INT width, INT height)
4118 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4120 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4123 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4125 INT save_state;
4126 GpStatus retval;
4127 HRGN hrgn=NULL;
4129 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4130 return NotImplemented;
4132 save_state = SaveDC(graphics->hdc);
4133 EndPath(graphics->hdc);
4134 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4135 : WINDING));
4137 retval = get_clip_hrgn(graphics, &hrgn);
4139 if (retval != Ok)
4140 goto end;
4142 if (hrgn)
4143 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4145 gdi_transform_acquire(graphics);
4147 BeginPath(graphics->hdc);
4148 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4149 path->pathdata.Types, path->pathdata.Count, FALSE);
4151 if(retval == Ok)
4153 EndPath(graphics->hdc);
4154 brush_fill_path(graphics, brush);
4157 gdi_transform_release(graphics);
4159 end:
4160 RestoreDC(graphics->hdc, save_state);
4161 DeleteObject(hrgn);
4163 return retval;
4166 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4168 GpStatus stat;
4169 GpRegion *rgn;
4171 if (!brush_can_fill_pixels(brush))
4172 return NotImplemented;
4174 /* FIXME: This could probably be done more efficiently without regions. */
4176 stat = GdipCreateRegionPath(path, &rgn);
4178 if (stat == Ok)
4180 stat = GdipFillRegion(graphics, brush, rgn);
4182 GdipDeleteRegion(rgn);
4185 return stat;
4188 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4190 GpStatus stat = NotImplemented;
4192 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4194 if(!brush || !graphics || !path)
4195 return InvalidParameter;
4197 if(graphics->busy)
4198 return ObjectBusy;
4200 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4201 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4203 if (!graphics->image && !graphics->alpha_hdc)
4204 stat = GDI32_GdipFillPath(graphics, brush, path);
4206 if (stat == NotImplemented)
4207 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4209 if (stat == NotImplemented)
4211 FIXME("Not implemented for brushtype %i\n", brush->bt);
4212 stat = Ok;
4215 return stat;
4218 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4219 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4221 GpStatus stat;
4222 GpPath *path;
4224 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4225 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4227 if(!graphics || !brush)
4228 return InvalidParameter;
4230 if(graphics->busy)
4231 return ObjectBusy;
4233 stat = GdipCreatePath(FillModeAlternate, &path);
4235 if (stat == Ok)
4237 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4239 if (stat == Ok)
4240 stat = GdipFillPath(graphics, brush, path);
4242 GdipDeletePath(path);
4245 return stat;
4248 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4249 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4251 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4252 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4254 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4257 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4258 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4260 GpStatus stat;
4261 GpPath *path;
4263 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4265 if(!graphics || !brush || !points || !count)
4266 return InvalidParameter;
4268 if(graphics->busy)
4269 return ObjectBusy;
4271 stat = GdipCreatePath(fillMode, &path);
4273 if (stat == Ok)
4275 stat = GdipAddPathPolygon(path, points, count);
4277 if (stat == Ok)
4278 stat = GdipFillPath(graphics, brush, path);
4280 GdipDeletePath(path);
4283 return stat;
4286 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4287 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4289 GpStatus stat;
4290 GpPath *path;
4292 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4294 if(!graphics || !brush || !points || !count)
4295 return InvalidParameter;
4297 if(graphics->busy)
4298 return ObjectBusy;
4300 stat = GdipCreatePath(fillMode, &path);
4302 if (stat == Ok)
4304 stat = GdipAddPathPolygonI(path, points, count);
4306 if (stat == Ok)
4307 stat = GdipFillPath(graphics, brush, path);
4309 GdipDeletePath(path);
4312 return stat;
4315 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4316 GDIPCONST GpPointF *points, INT count)
4318 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4320 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4323 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4324 GDIPCONST GpPoint *points, INT count)
4326 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4328 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4331 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4332 REAL x, REAL y, REAL width, REAL height)
4334 GpRectF rect;
4336 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4338 rect.X = x;
4339 rect.Y = y;
4340 rect.Width = width;
4341 rect.Height = height;
4343 return GdipFillRectangles(graphics, brush, &rect, 1);
4346 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4347 INT x, INT y, INT width, INT height)
4349 GpRectF rect;
4351 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4353 rect.X = (REAL)x;
4354 rect.Y = (REAL)y;
4355 rect.Width = (REAL)width;
4356 rect.Height = (REAL)height;
4358 return GdipFillRectangles(graphics, brush, &rect, 1);
4361 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4362 INT count)
4364 GpStatus status;
4365 GpPath *path;
4367 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4369 if(!graphics || !brush || !rects || count <= 0)
4370 return InvalidParameter;
4372 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4374 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4375 /* FIXME: Add gdi32 drawing. */
4376 return status;
4379 status = GdipCreatePath(FillModeAlternate, &path);
4380 if (status != Ok) return status;
4382 status = GdipAddPathRectangles(path, rects, count);
4383 if (status == Ok)
4384 status = GdipFillPath(graphics, brush, path);
4386 GdipDeletePath(path);
4387 return status;
4390 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4391 INT count)
4393 GpRectF *rectsF;
4394 GpStatus ret;
4395 INT i;
4397 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4399 if(!rects || count <= 0)
4400 return InvalidParameter;
4402 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4403 if(!rectsF)
4404 return OutOfMemory;
4406 for(i = 0; i < count; i++){
4407 rectsF[i].X = (REAL)rects[i].X;
4408 rectsF[i].Y = (REAL)rects[i].Y;
4409 rectsF[i].Width = (REAL)rects[i].Width;
4410 rectsF[i].Height = (REAL)rects[i].Height;
4413 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4414 heap_free(rectsF);
4416 return ret;
4419 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4420 GpRegion* region)
4422 INT save_state;
4423 GpStatus status;
4424 HRGN hrgn;
4425 RECT rc;
4427 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4428 return NotImplemented;
4430 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4431 if(status != Ok)
4432 return status;
4434 save_state = SaveDC(graphics->hdc);
4435 EndPath(graphics->hdc);
4437 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4439 DeleteObject(hrgn);
4441 hrgn = NULL;
4442 status = get_clip_hrgn(graphics, &hrgn);
4444 if (status != Ok)
4446 RestoreDC(graphics->hdc, save_state);
4447 return status;
4450 if (hrgn)
4452 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4453 DeleteObject(hrgn);
4456 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4458 BeginPath(graphics->hdc);
4459 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4460 EndPath(graphics->hdc);
4462 brush_fill_path(graphics, brush);
4465 RestoreDC(graphics->hdc, save_state);
4468 return Ok;
4471 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4472 GpRegion* region)
4474 GpStatus stat;
4475 GpRegion *temp_region;
4476 GpMatrix world_to_device;
4477 GpRectF graphics_bounds;
4478 DWORD *pixel_data;
4479 HRGN hregion;
4480 RECT bound_rect;
4481 GpRect gp_bound_rect;
4483 if (!brush_can_fill_pixels(brush))
4484 return NotImplemented;
4486 stat = gdi_transform_acquire(graphics);
4488 if (stat == Ok)
4489 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4491 if (stat == Ok)
4492 stat = GdipCloneRegion(region, &temp_region);
4494 if (stat == Ok)
4496 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4497 CoordinateSpaceWorld, &world_to_device);
4499 if (stat == Ok)
4500 stat = GdipTransformRegion(temp_region, &world_to_device);
4502 if (stat == Ok)
4503 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4505 if (stat == Ok)
4506 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4508 GdipDeleteRegion(temp_region);
4511 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4513 DeleteObject(hregion);
4514 gdi_transform_release(graphics);
4515 return Ok;
4518 if (stat == Ok)
4520 gp_bound_rect.X = bound_rect.left;
4521 gp_bound_rect.Y = bound_rect.top;
4522 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4523 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4525 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4526 if (!pixel_data)
4527 stat = OutOfMemory;
4529 if (stat == Ok)
4531 stat = brush_fill_pixels(graphics, brush, pixel_data,
4532 &gp_bound_rect, gp_bound_rect.Width);
4534 if (stat == Ok)
4535 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4536 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4537 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4538 PixelFormat32bppARGB);
4540 heap_free(pixel_data);
4543 DeleteObject(hregion);
4546 gdi_transform_release(graphics);
4548 return stat;
4551 /*****************************************************************************
4552 * GdipFillRegion [GDIPLUS.@]
4554 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4555 GpRegion* region)
4557 GpStatus stat = NotImplemented;
4559 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4561 if (!(graphics && brush && region))
4562 return InvalidParameter;
4564 if(graphics->busy)
4565 return ObjectBusy;
4567 if (!graphics->image && !graphics->alpha_hdc)
4568 stat = GDI32_GdipFillRegion(graphics, brush, region);
4570 if (stat == NotImplemented)
4571 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4573 if (stat == NotImplemented)
4575 FIXME("not implemented for brushtype %i\n", brush->bt);
4576 stat = Ok;
4579 return stat;
4582 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4584 TRACE("(%p,%u)\n", graphics, intention);
4586 if(!graphics)
4587 return InvalidParameter;
4589 if(graphics->busy)
4590 return ObjectBusy;
4592 /* We have no internal operation queue, so there's no need to clear it. */
4594 if (graphics->hdc)
4595 GdiFlush();
4597 return Ok;
4600 /*****************************************************************************
4601 * GdipGetClipBounds [GDIPLUS.@]
4603 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4605 GpStatus status;
4606 GpRegion *clip;
4608 TRACE("(%p, %p)\n", graphics, rect);
4610 if(!graphics)
4611 return InvalidParameter;
4613 if(graphics->busy)
4614 return ObjectBusy;
4616 status = GdipCreateRegion(&clip);
4617 if (status != Ok) return status;
4619 status = GdipGetClip(graphics, clip);
4620 if (status == Ok)
4621 status = GdipGetRegionBounds(clip, graphics, rect);
4623 GdipDeleteRegion(clip);
4624 return status;
4627 /*****************************************************************************
4628 * GdipGetClipBoundsI [GDIPLUS.@]
4630 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4632 TRACE("(%p, %p)\n", graphics, rect);
4634 if(!graphics)
4635 return InvalidParameter;
4637 if(graphics->busy)
4638 return ObjectBusy;
4640 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4643 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4644 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4645 CompositingMode *mode)
4647 TRACE("(%p, %p)\n", graphics, mode);
4649 if(!graphics || !mode)
4650 return InvalidParameter;
4652 if(graphics->busy)
4653 return ObjectBusy;
4655 *mode = graphics->compmode;
4657 return Ok;
4660 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4661 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4662 CompositingQuality *quality)
4664 TRACE("(%p, %p)\n", graphics, quality);
4666 if(!graphics || !quality)
4667 return InvalidParameter;
4669 if(graphics->busy)
4670 return ObjectBusy;
4672 *quality = graphics->compqual;
4674 return Ok;
4677 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4678 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4679 InterpolationMode *mode)
4681 TRACE("(%p, %p)\n", graphics, mode);
4683 if(!graphics || !mode)
4684 return InvalidParameter;
4686 if(graphics->busy)
4687 return ObjectBusy;
4689 *mode = graphics->interpolation;
4691 return Ok;
4694 /* FIXME: Need to handle color depths less than 24bpp */
4695 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4697 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4699 if(!graphics || !argb)
4700 return InvalidParameter;
4702 if(graphics->busy)
4703 return ObjectBusy;
4705 return Ok;
4708 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4710 TRACE("(%p, %p)\n", graphics, scale);
4712 if(!graphics || !scale)
4713 return InvalidParameter;
4715 if(graphics->busy)
4716 return ObjectBusy;
4718 *scale = graphics->scale;
4720 return Ok;
4723 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4725 TRACE("(%p, %p)\n", graphics, unit);
4727 if(!graphics || !unit)
4728 return InvalidParameter;
4730 if(graphics->busy)
4731 return ObjectBusy;
4733 *unit = graphics->unit;
4735 return Ok;
4738 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4739 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4740 *mode)
4742 TRACE("(%p, %p)\n", graphics, mode);
4744 if(!graphics || !mode)
4745 return InvalidParameter;
4747 if(graphics->busy)
4748 return ObjectBusy;
4750 *mode = graphics->pixeloffset;
4752 return Ok;
4755 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4756 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4758 TRACE("(%p, %p)\n", graphics, mode);
4760 if(!graphics || !mode)
4761 return InvalidParameter;
4763 if(graphics->busy)
4764 return ObjectBusy;
4766 *mode = graphics->smoothing;
4768 return Ok;
4771 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4773 TRACE("(%p, %p)\n", graphics, contrast);
4775 if(!graphics || !contrast)
4776 return InvalidParameter;
4778 *contrast = graphics->textcontrast;
4780 return Ok;
4783 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4784 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4785 TextRenderingHint *hint)
4787 TRACE("(%p, %p)\n", graphics, hint);
4789 if(!graphics || !hint)
4790 return InvalidParameter;
4792 if(graphics->busy)
4793 return ObjectBusy;
4795 *hint = graphics->texthint;
4797 return Ok;
4800 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4802 GpRegion *clip_rgn;
4803 GpStatus stat;
4804 GpMatrix device_to_world;
4806 TRACE("(%p, %p)\n", graphics, rect);
4808 if(!graphics || !rect)
4809 return InvalidParameter;
4811 if(graphics->busy)
4812 return ObjectBusy;
4814 /* intersect window and graphics clipping regions */
4815 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4816 return stat;
4818 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4819 goto cleanup;
4821 /* transform to world coordinates */
4822 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4823 goto cleanup;
4825 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4826 goto cleanup;
4828 /* get bounds of the region */
4829 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4831 cleanup:
4832 GdipDeleteRegion(clip_rgn);
4834 return stat;
4837 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4839 GpRectF rectf;
4840 GpStatus stat;
4842 TRACE("(%p, %p)\n", graphics, rect);
4844 if(!graphics || !rect)
4845 return InvalidParameter;
4847 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4849 rect->X = gdip_round(rectf.X);
4850 rect->Y = gdip_round(rectf.Y);
4851 rect->Width = gdip_round(rectf.Width);
4852 rect->Height = gdip_round(rectf.Height);
4855 return stat;
4858 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4860 TRACE("(%p, %p)\n", graphics, matrix);
4862 if(!graphics || !matrix)
4863 return InvalidParameter;
4865 if(graphics->busy)
4866 return ObjectBusy;
4868 *matrix = graphics->worldtrans;
4869 return Ok;
4872 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4874 GpSolidFill *brush;
4875 GpStatus stat;
4876 GpRectF wnd_rect;
4878 TRACE("(%p, %x)\n", graphics, color);
4880 if(!graphics)
4881 return InvalidParameter;
4883 if(graphics->busy)
4884 return ObjectBusy;
4886 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4887 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
4889 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4890 return stat;
4892 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
4893 GdipDeleteBrush((GpBrush*)brush);
4894 return stat;
4897 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4898 wnd_rect.Width, wnd_rect.Height);
4900 GdipDeleteBrush((GpBrush*)brush);
4902 return Ok;
4905 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4907 TRACE("(%p, %p)\n", graphics, res);
4909 if(!graphics || !res)
4910 return InvalidParameter;
4912 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4915 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4917 GpStatus stat;
4918 GpRegion* rgn;
4919 GpPointF pt;
4921 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4923 if(!graphics || !result)
4924 return InvalidParameter;
4926 if(graphics->busy)
4927 return ObjectBusy;
4929 pt.X = x;
4930 pt.Y = y;
4931 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4932 CoordinateSpaceWorld, &pt, 1)) != Ok)
4933 return stat;
4935 if((stat = GdipCreateRegion(&rgn)) != Ok)
4936 return stat;
4938 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4939 goto cleanup;
4941 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4943 cleanup:
4944 GdipDeleteRegion(rgn);
4945 return stat;
4948 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4950 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4953 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4955 GpStatus stat;
4956 GpRegion* rgn;
4957 GpPointF pts[2];
4959 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4961 if(!graphics || !result)
4962 return InvalidParameter;
4964 if(graphics->busy)
4965 return ObjectBusy;
4967 pts[0].X = x;
4968 pts[0].Y = y;
4969 pts[1].X = x + width;
4970 pts[1].Y = y + height;
4972 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4973 CoordinateSpaceWorld, pts, 2)) != Ok)
4974 return stat;
4976 pts[1].X -= pts[0].X;
4977 pts[1].Y -= pts[0].Y;
4979 if((stat = GdipCreateRegion(&rgn)) != Ok)
4980 return stat;
4982 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4983 goto cleanup;
4985 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4987 cleanup:
4988 GdipDeleteRegion(rgn);
4989 return stat;
4992 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4994 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4997 GpStatus gdip_format_string(HDC hdc,
4998 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4999 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5000 gdip_format_string_callback callback, void *user_data)
5002 WCHAR* stringdup;
5003 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5004 nheight, lineend, lineno = 0;
5005 RectF bounds;
5006 StringAlignment halign;
5007 GpStatus stat = Ok;
5008 SIZE size;
5009 HotkeyPrefix hkprefix;
5010 INT *hotkeyprefix_offsets=NULL;
5011 INT hotkeyprefix_count=0;
5012 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5013 BOOL seen_prefix = FALSE;
5015 if(length == -1) length = lstrlenW(string);
5017 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5018 if(!stringdup) return OutOfMemory;
5020 if (!format)
5021 format = &default_drawstring_format;
5023 nwidth = rect->Width;
5024 nheight = rect->Height;
5025 if (ignore_empty_clip)
5027 if (!nwidth) nwidth = INT_MAX;
5028 if (!nheight) nheight = INT_MAX;
5031 hkprefix = format->hkprefix;
5033 if (hkprefix == HotkeyPrefixShow)
5035 for (i=0; i<length; i++)
5037 if (string[i] == '&')
5038 hotkeyprefix_count++;
5042 if (hotkeyprefix_count)
5043 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5045 hotkeyprefix_count = 0;
5047 for(i = 0, j = 0; i < length; i++){
5048 /* FIXME: This makes the indexes passed to callback inaccurate. */
5049 if(!isprintW(string[i]) && (string[i] != '\n'))
5050 continue;
5052 /* FIXME: tabs should be handled using tabstops from stringformat */
5053 if (string[i] == '\t')
5054 continue;
5056 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5057 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5058 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5060 seen_prefix = TRUE;
5061 continue;
5064 seen_prefix = FALSE;
5066 stringdup[j] = string[i];
5067 j++;
5070 length = j;
5072 halign = format->align;
5074 while(sum < length){
5075 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5076 nwidth, &fit, NULL, &size);
5077 fitcpy = fit;
5079 if(fit == 0)
5080 break;
5082 for(lret = 0; lret < fit; lret++)
5083 if(*(stringdup + sum + lret) == '\n')
5084 break;
5086 /* Line break code (may look strange, but it imitates windows). */
5087 if(lret < fit)
5088 lineend = fit = lret; /* this is not an off-by-one error */
5089 else if(fit < (length - sum)){
5090 if(*(stringdup + sum + fit) == ' ')
5091 while(*(stringdup + sum + fit) == ' ')
5092 fit++;
5093 else
5094 while(*(stringdup + sum + fit - 1) != ' '){
5095 fit--;
5097 if(*(stringdup + sum + fit) == '\t')
5098 break;
5100 if(fit == 0){
5101 fit = fitcpy;
5102 break;
5105 lineend = fit;
5106 while(*(stringdup + sum + lineend - 1) == ' ' ||
5107 *(stringdup + sum + lineend - 1) == '\t')
5108 lineend--;
5110 else
5111 lineend = fit;
5113 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5114 nwidth, &j, NULL, &size);
5116 bounds.Width = size.cx;
5118 if(height + size.cy > nheight)
5120 if (format->attr & StringFormatFlagsLineLimit)
5121 break;
5122 bounds.Height = nheight - (height + size.cy);
5124 else
5125 bounds.Height = size.cy;
5127 bounds.Y = rect->Y + height;
5129 switch (halign)
5131 case StringAlignmentNear:
5132 default:
5133 bounds.X = rect->X;
5134 break;
5135 case StringAlignmentCenter:
5136 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5137 break;
5138 case StringAlignmentFar:
5139 bounds.X = rect->X + rect->Width - bounds.Width;
5140 break;
5143 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5144 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5145 break;
5147 stat = callback(hdc, stringdup, sum, lineend,
5148 font, rect, format, lineno, &bounds,
5149 &hotkeyprefix_offsets[hotkeyprefix_pos],
5150 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5152 if (stat != Ok)
5153 break;
5155 sum += fit + (lret < fitcpy ? 1 : 0);
5156 height += size.cy;
5157 lineno++;
5159 hotkeyprefix_pos = hotkeyprefix_end_pos;
5161 if(height > nheight)
5162 break;
5164 /* Stop if this was a linewrap (but not if it was a linebreak). */
5165 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5166 break;
5169 heap_free(stringdup);
5170 heap_free(hotkeyprefix_offsets);
5172 return stat;
5175 struct measure_ranges_args {
5176 GpRegion **regions;
5177 REAL rel_width, rel_height;
5180 static GpStatus measure_ranges_callback(HDC hdc,
5181 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5182 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5183 INT lineno, const RectF *bounds, INT *underlined_indexes,
5184 INT underlined_index_count, void *user_data)
5186 int i;
5187 GpStatus stat = Ok;
5188 struct measure_ranges_args *args = user_data;
5190 for (i=0; i<format->range_count; i++)
5192 INT range_start = max(index, format->character_ranges[i].First);
5193 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5194 if (range_start < range_end)
5196 GpRectF range_rect;
5197 SIZE range_size;
5199 range_rect.Y = bounds->Y / args->rel_height;
5200 range_rect.Height = bounds->Height / args->rel_height;
5202 GetTextExtentExPointW(hdc, string + index, range_start - index,
5203 INT_MAX, NULL, NULL, &range_size);
5204 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5206 GetTextExtentExPointW(hdc, string + index, range_end - index,
5207 INT_MAX, NULL, NULL, &range_size);
5208 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5210 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5211 if (stat != Ok)
5212 break;
5216 return stat;
5219 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5220 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5221 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5222 INT regionCount, GpRegion** regions)
5224 GpStatus stat;
5225 int i;
5226 HFONT gdifont, oldfont;
5227 struct measure_ranges_args args;
5228 HDC hdc, temp_hdc=NULL;
5229 GpPointF pt[3];
5230 RectF scaled_rect;
5231 REAL margin_x;
5233 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
5234 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5236 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5237 return InvalidParameter;
5239 if (regionCount < stringFormat->range_count)
5240 return InvalidParameter;
5242 if(!graphics->hdc)
5244 hdc = temp_hdc = CreateCompatibleDC(0);
5245 if (!temp_hdc) return OutOfMemory;
5247 else
5248 hdc = graphics->hdc;
5250 if (stringFormat->attr)
5251 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5253 pt[0].X = 0.0;
5254 pt[0].Y = 0.0;
5255 pt[1].X = 1.0;
5256 pt[1].Y = 0.0;
5257 pt[2].X = 0.0;
5258 pt[2].Y = 1.0;
5259 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5260 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5261 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5262 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5263 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5265 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5266 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5268 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5269 scaled_rect.Y = layoutRect->Y * args.rel_height;
5270 scaled_rect.Width = layoutRect->Width * args.rel_width;
5271 scaled_rect.Height = layoutRect->Height * args.rel_height;
5273 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5274 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5276 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5277 oldfont = SelectObject(hdc, gdifont);
5279 for (i=0; i<stringFormat->range_count; i++)
5281 stat = GdipSetEmpty(regions[i]);
5282 if (stat != Ok)
5283 return stat;
5286 args.regions = regions;
5288 gdi_transform_acquire(graphics);
5290 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5291 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5293 gdi_transform_release(graphics);
5295 SelectObject(hdc, oldfont);
5296 DeleteObject(gdifont);
5298 if (temp_hdc)
5299 DeleteDC(temp_hdc);
5301 return stat;
5304 struct measure_string_args {
5305 RectF *bounds;
5306 INT *codepointsfitted;
5307 INT *linesfilled;
5308 REAL rel_width, rel_height;
5311 static GpStatus measure_string_callback(HDC hdc,
5312 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5313 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5314 INT lineno, const RectF *bounds, INT *underlined_indexes,
5315 INT underlined_index_count, void *user_data)
5317 struct measure_string_args *args = user_data;
5318 REAL new_width, new_height;
5320 new_width = bounds->Width / args->rel_width;
5321 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5323 if (new_width > args->bounds->Width)
5324 args->bounds->Width = new_width;
5326 if (new_height > args->bounds->Height)
5327 args->bounds->Height = new_height;
5329 if (args->codepointsfitted)
5330 *args->codepointsfitted = index + length;
5332 if (args->linesfilled)
5333 (*args->linesfilled)++;
5335 return Ok;
5338 /* Find the smallest rectangle that bounds the text when it is printed in rect
5339 * according to the format options listed in format. If rect has 0 width and
5340 * height, then just find the smallest rectangle that bounds the text when it's
5341 * printed at location (rect->X, rect-Y). */
5342 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5343 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5344 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5345 INT *codepointsfitted, INT *linesfilled)
5347 HFONT oldfont, gdifont;
5348 struct measure_string_args args;
5349 HDC temp_hdc=NULL, hdc;
5350 GpPointF pt[3];
5351 RectF scaled_rect;
5352 REAL margin_x;
5353 INT lines, glyphs;
5355 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5356 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5357 bounds, codepointsfitted, linesfilled);
5359 if(!graphics || !string || !font || !rect || !bounds)
5360 return InvalidParameter;
5362 if(!graphics->hdc)
5364 hdc = temp_hdc = CreateCompatibleDC(0);
5365 if (!temp_hdc) return OutOfMemory;
5367 else
5368 hdc = graphics->hdc;
5370 if(linesfilled) *linesfilled = 0;
5371 if(codepointsfitted) *codepointsfitted = 0;
5373 if(format)
5374 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5376 pt[0].X = 0.0;
5377 pt[0].Y = 0.0;
5378 pt[1].X = 1.0;
5379 pt[1].Y = 0.0;
5380 pt[2].X = 0.0;
5381 pt[2].Y = 1.0;
5382 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5383 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5384 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5385 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5386 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5388 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5389 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5391 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5392 scaled_rect.Y = rect->Y * args.rel_height;
5393 scaled_rect.Width = rect->Width * args.rel_width;
5394 scaled_rect.Height = rect->Height * args.rel_height;
5395 if (scaled_rect.Width >= 0.5)
5397 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5398 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5401 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5402 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5404 get_font_hfont(graphics, font, format, &gdifont, NULL);
5405 oldfont = SelectObject(hdc, gdifont);
5407 bounds->X = rect->X;
5408 bounds->Y = rect->Y;
5409 bounds->Width = 0.0;
5410 bounds->Height = 0.0;
5412 args.bounds = bounds;
5413 args.codepointsfitted = &glyphs;
5414 args.linesfilled = &lines;
5415 lines = glyphs = 0;
5417 gdi_transform_acquire(graphics);
5419 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5420 measure_string_callback, &args);
5422 gdi_transform_release(graphics);
5424 if (linesfilled) *linesfilled = lines;
5425 if (codepointsfitted) *codepointsfitted = glyphs;
5427 if (lines)
5428 bounds->Width += margin_x * 2.0;
5430 SelectObject(hdc, oldfont);
5431 DeleteObject(gdifont);
5433 if (temp_hdc)
5434 DeleteDC(temp_hdc);
5436 return Ok;
5439 struct draw_string_args {
5440 GpGraphics *graphics;
5441 GDIPCONST GpBrush *brush;
5442 REAL x, y, rel_width, rel_height, ascent;
5445 static GpStatus draw_string_callback(HDC hdc,
5446 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5447 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5448 INT lineno, const RectF *bounds, INT *underlined_indexes,
5449 INT underlined_index_count, void *user_data)
5451 struct draw_string_args *args = user_data;
5452 PointF position;
5453 GpStatus stat;
5455 position.X = args->x + bounds->X / args->rel_width;
5456 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5458 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5459 args->brush, &position,
5460 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5462 if (stat == Ok && underlined_index_count)
5464 OUTLINETEXTMETRICW otm;
5465 REAL underline_y, underline_height;
5466 int i;
5468 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5470 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5471 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5473 for (i=0; i<underlined_index_count; i++)
5475 REAL start_x, end_x;
5476 SIZE text_size;
5477 INT ofs = underlined_indexes[i] - index;
5479 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5480 start_x = text_size.cx / args->rel_width;
5482 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5483 end_x = text_size.cx / args->rel_width;
5485 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5489 return stat;
5492 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5493 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5494 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5496 HRGN rgn = NULL;
5497 HFONT gdifont;
5498 GpPointF pt[3], rectcpy[4];
5499 POINT corners[4];
5500 REAL rel_width, rel_height, margin_x;
5501 INT save_state, format_flags = 0;
5502 REAL offsety = 0.0;
5503 struct draw_string_args args;
5504 RectF scaled_rect;
5505 HDC hdc, temp_hdc=NULL;
5506 TEXTMETRICW textmetric;
5508 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5509 length, font, debugstr_rectf(rect), format, brush);
5511 if(!graphics || !string || !font || !brush || !rect)
5512 return InvalidParameter;
5514 if(graphics->hdc)
5516 hdc = graphics->hdc;
5518 else
5520 hdc = temp_hdc = CreateCompatibleDC(0);
5523 if(format){
5524 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5526 format_flags = format->attr;
5528 /* Should be no need to explicitly test for StringAlignmentNear as
5529 * that is default behavior if no alignment is passed. */
5530 if(format->line_align != StringAlignmentNear){
5531 RectF bounds, in_rect = *rect;
5532 in_rect.Height = 0.0; /* avoid height clipping */
5533 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5535 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5537 if(format->line_align == StringAlignmentCenter)
5538 offsety = (rect->Height - bounds.Height) / 2;
5539 else if(format->line_align == StringAlignmentFar)
5540 offsety = (rect->Height - bounds.Height);
5542 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5545 save_state = SaveDC(hdc);
5547 pt[0].X = 0.0;
5548 pt[0].Y = 0.0;
5549 pt[1].X = 1.0;
5550 pt[1].Y = 0.0;
5551 pt[2].X = 0.0;
5552 pt[2].Y = 1.0;
5553 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5554 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5555 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5556 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5557 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5559 rectcpy[3].X = rectcpy[0].X = rect->X;
5560 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5561 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5562 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5563 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5564 round_points(corners, rectcpy, 4);
5566 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5567 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5569 scaled_rect.X = margin_x * rel_width;
5570 scaled_rect.Y = 0.0;
5571 scaled_rect.Width = rel_width * rect->Width;
5572 scaled_rect.Height = rel_height * rect->Height;
5573 if (scaled_rect.Width >= 0.5)
5575 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5576 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5579 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5580 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5582 if (!(format_flags & StringFormatFlagsNoClip) &&
5583 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5584 rect->Width > 0.0 && rect->Height > 0.0)
5586 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5587 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5588 SelectClipRgn(hdc, rgn);
5591 get_font_hfont(graphics, font, format, &gdifont, NULL);
5592 SelectObject(hdc, gdifont);
5594 args.graphics = graphics;
5595 args.brush = brush;
5597 args.x = rect->X;
5598 args.y = rect->Y + offsety;
5600 args.rel_width = rel_width;
5601 args.rel_height = rel_height;
5603 gdi_transform_acquire(graphics);
5605 GetTextMetricsW(hdc, &textmetric);
5606 args.ascent = textmetric.tmAscent / rel_height;
5608 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5609 draw_string_callback, &args);
5611 gdi_transform_release(graphics);
5613 DeleteObject(rgn);
5614 DeleteObject(gdifont);
5616 RestoreDC(hdc, save_state);
5618 DeleteDC(temp_hdc);
5620 return Ok;
5623 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5625 TRACE("(%p)\n", graphics);
5627 if(!graphics)
5628 return InvalidParameter;
5630 if(graphics->busy)
5631 return ObjectBusy;
5633 return GdipSetInfinite(graphics->clip);
5636 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5638 GpStatus stat;
5640 TRACE("(%p)\n", graphics);
5642 if(!graphics)
5643 return InvalidParameter;
5645 if(graphics->busy)
5646 return ObjectBusy;
5648 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5649 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5651 if (stat != Ok)
5652 return stat;
5655 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5658 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5659 GpMatrixOrder order)
5661 GpStatus stat;
5663 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5665 if(!graphics)
5666 return InvalidParameter;
5668 if(graphics->busy)
5669 return ObjectBusy;
5671 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5672 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5674 if (stat != Ok)
5675 return stat;
5678 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5681 static GpStatus begin_container(GpGraphics *graphics,
5682 GraphicsContainerType type, GraphicsContainer *state)
5684 GraphicsContainerItem *container;
5685 GpStatus sts;
5687 if(!graphics || !state)
5688 return InvalidParameter;
5690 sts = init_container(&container, graphics, type);
5691 if(sts != Ok)
5692 return sts;
5694 list_add_head(&graphics->containers, &container->entry);
5695 *state = graphics->contid = container->contid;
5697 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5698 if (type == BEGIN_CONTAINER)
5699 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5700 else
5701 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5704 return Ok;
5707 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5709 TRACE("(%p, %p)\n", graphics, state);
5710 return begin_container(graphics, SAVE_GRAPHICS, state);
5713 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5714 GraphicsContainer *state)
5716 TRACE("(%p, %p)\n", graphics, state);
5717 return begin_container(graphics, BEGIN_CONTAINER, state);
5720 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5722 GraphicsContainerItem *container;
5723 GpMatrix transform;
5724 GpStatus stat;
5725 GpRectF scaled_srcrect;
5726 REAL scale_x, scale_y;
5728 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5730 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5731 return InvalidParameter;
5733 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5734 if(stat != Ok)
5735 return stat;
5737 list_add_head(&graphics->containers, &container->entry);
5738 *state = graphics->contid = container->contid;
5740 scale_x = units_to_pixels(1.0, unit, graphics->xres);
5741 scale_y = units_to_pixels(1.0, unit, graphics->yres);
5743 scaled_srcrect.X = scale_x * srcrect->X;
5744 scaled_srcrect.Y = scale_y * srcrect->Y;
5745 scaled_srcrect.Width = scale_x * srcrect->Width;
5746 scaled_srcrect.Height = scale_y * srcrect->Height;
5748 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5749 transform.matrix[1] = 0.0;
5750 transform.matrix[2] = 0.0;
5751 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5752 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5753 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5755 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5757 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5758 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5761 return Ok;
5764 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5766 GpRectF dstrectf, srcrectf;
5768 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5770 if (!dstrect || !srcrect)
5771 return InvalidParameter;
5773 dstrectf.X = dstrect->X;
5774 dstrectf.Y = dstrect->Y;
5775 dstrectf.Width = dstrect->Width;
5776 dstrectf.Height = dstrect->Height;
5778 srcrectf.X = srcrect->X;
5779 srcrectf.Y = srcrect->Y;
5780 srcrectf.Width = srcrect->Width;
5781 srcrectf.Height = srcrect->Height;
5783 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5786 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5788 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5789 return NotImplemented;
5792 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5793 GraphicsContainer state)
5795 GpStatus sts;
5796 GraphicsContainerItem *container, *container2;
5798 if(!graphics)
5799 return InvalidParameter;
5801 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5802 if(container->contid == state && container->type == type)
5803 break;
5806 /* did not find a matching container */
5807 if(&container->entry == &graphics->containers)
5808 return Ok;
5810 sts = restore_container(graphics, container);
5811 if(sts != Ok)
5812 return sts;
5814 /* remove all of the containers on top of the found container */
5815 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5816 if(container->contid == state)
5817 break;
5818 list_remove(&container->entry);
5819 delete_container(container);
5822 list_remove(&container->entry);
5823 delete_container(container);
5825 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5826 if (type == BEGIN_CONTAINER)
5827 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
5828 else
5829 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
5832 return Ok;
5835 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5837 TRACE("(%p, %x)\n", graphics, state);
5838 return end_container(graphics, BEGIN_CONTAINER, state);
5841 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5843 TRACE("(%p, %x)\n", graphics, state);
5844 return end_container(graphics, SAVE_GRAPHICS, state);
5847 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5848 REAL sy, GpMatrixOrder order)
5850 GpStatus stat;
5852 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5854 if(!graphics)
5855 return InvalidParameter;
5857 if(graphics->busy)
5858 return ObjectBusy;
5860 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5861 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
5863 if (stat != Ok)
5864 return stat;
5867 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5870 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5871 CombineMode mode)
5873 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5875 if(!graphics || !srcgraphics)
5876 return InvalidParameter;
5878 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5881 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5882 CompositingMode mode)
5884 TRACE("(%p, %d)\n", graphics, mode);
5886 if(!graphics)
5887 return InvalidParameter;
5889 if(graphics->busy)
5890 return ObjectBusy;
5892 if(graphics->compmode == mode)
5893 return Ok;
5895 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5897 GpStatus stat;
5899 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5900 EmfPlusRecordTypeSetCompositingMode, mode);
5901 if(stat != Ok)
5902 return stat;
5905 graphics->compmode = mode;
5907 return Ok;
5910 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5911 CompositingQuality quality)
5913 TRACE("(%p, %d)\n", graphics, quality);
5915 if(!graphics)
5916 return InvalidParameter;
5918 if(graphics->busy)
5919 return ObjectBusy;
5921 if(graphics->compqual == quality)
5922 return Ok;
5924 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5926 GpStatus stat;
5928 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5929 EmfPlusRecordTypeSetCompositingQuality, quality);
5930 if(stat != Ok)
5931 return stat;
5934 graphics->compqual = quality;
5936 return Ok;
5939 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5940 InterpolationMode mode)
5942 TRACE("(%p, %d)\n", graphics, mode);
5944 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5945 return InvalidParameter;
5947 if(graphics->busy)
5948 return ObjectBusy;
5950 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5951 mode = InterpolationModeBilinear;
5953 if (mode == InterpolationModeHighQuality)
5954 mode = InterpolationModeHighQualityBicubic;
5956 if (mode == graphics->interpolation)
5957 return Ok;
5959 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5961 GpStatus stat;
5963 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5964 EmfPlusRecordTypeSetInterpolationMode, mode);
5965 if (stat != Ok)
5966 return stat;
5969 graphics->interpolation = mode;
5971 return Ok;
5974 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5976 GpStatus stat;
5978 TRACE("(%p, %.2f)\n", graphics, scale);
5980 if(!graphics || (scale <= 0.0))
5981 return InvalidParameter;
5983 if(graphics->busy)
5984 return ObjectBusy;
5986 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5988 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5989 if (stat != Ok)
5990 return stat;
5993 graphics->scale = scale;
5995 return Ok;
5998 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6000 GpStatus stat;
6002 TRACE("(%p, %d)\n", graphics, unit);
6004 if(!graphics)
6005 return InvalidParameter;
6007 if(graphics->busy)
6008 return ObjectBusy;
6010 if(unit == UnitWorld)
6011 return InvalidParameter;
6013 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6015 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6016 if (stat != Ok)
6017 return stat;
6020 graphics->unit = unit;
6022 return Ok;
6025 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6026 mode)
6028 TRACE("(%p, %d)\n", graphics, mode);
6030 if(!graphics)
6031 return InvalidParameter;
6033 if(graphics->busy)
6034 return ObjectBusy;
6036 if(graphics->pixeloffset == mode)
6037 return Ok;
6039 if(graphics->image && graphics->image->type == ImageTypeMetafile)
6041 GpStatus stat;
6043 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6044 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6045 if(stat != Ok)
6046 return stat;
6049 graphics->pixeloffset = mode;
6051 return Ok;
6054 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6056 static int calls;
6058 TRACE("(%p,%i,%i)\n", graphics, x, y);
6060 if (!(calls++))
6061 FIXME("value is unused in rendering\n");
6063 if (!graphics)
6064 return InvalidParameter;
6066 graphics->origin_x = x;
6067 graphics->origin_y = y;
6069 return Ok;
6072 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6074 TRACE("(%p,%p,%p)\n", graphics, x, y);
6076 if (!graphics || !x || !y)
6077 return InvalidParameter;
6079 *x = graphics->origin_x;
6080 *y = graphics->origin_y;
6082 return Ok;
6085 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6087 TRACE("(%p, %d)\n", graphics, mode);
6089 if(!graphics)
6090 return InvalidParameter;
6092 if(graphics->busy)
6093 return ObjectBusy;
6095 if(graphics->smoothing == mode)
6096 return Ok;
6098 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6099 GpStatus stat;
6100 BOOL antialias = (mode != SmoothingModeDefault &&
6101 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6103 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6104 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6105 if(stat != Ok)
6106 return stat;
6109 graphics->smoothing = mode;
6111 return Ok;
6114 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6116 TRACE("(%p, %d)\n", graphics, contrast);
6118 if(!graphics)
6119 return InvalidParameter;
6121 graphics->textcontrast = contrast;
6123 return Ok;
6126 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6127 TextRenderingHint hint)
6129 TRACE("(%p, %d)\n", graphics, hint);
6131 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6132 return InvalidParameter;
6134 if(graphics->busy)
6135 return ObjectBusy;
6137 if(graphics->texthint == hint)
6138 return Ok;
6140 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6141 GpStatus stat;
6143 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6144 EmfPlusRecordTypeSetTextRenderingHint, hint);
6145 if(stat != Ok)
6146 return stat;
6149 graphics->texthint = hint;
6151 return Ok;
6154 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6156 GpStatus stat;
6158 TRACE("(%p, %p)\n", graphics, matrix);
6160 if(!graphics || !matrix)
6161 return InvalidParameter;
6163 if(graphics->busy)
6164 return ObjectBusy;
6166 TRACE("%f,%f,%f,%f,%f,%f\n",
6167 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6168 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6170 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6171 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6173 if (stat != Ok)
6174 return stat;
6177 graphics->worldtrans = *matrix;
6179 return Ok;
6182 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6183 REAL dy, GpMatrixOrder order)
6185 GpStatus stat;
6187 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6189 if(!graphics)
6190 return InvalidParameter;
6192 if(graphics->busy)
6193 return ObjectBusy;
6195 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6196 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6198 if (stat != Ok)
6199 return stat;
6202 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6205 /*****************************************************************************
6206 * GdipSetClipHrgn [GDIPLUS.@]
6208 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6210 GpRegion *region;
6211 GpStatus status;
6212 GpMatrix transform;
6214 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6216 if(!graphics)
6217 return InvalidParameter;
6219 if(graphics->busy)
6220 return ObjectBusy;
6222 /* hrgn is in gdi32 device units */
6223 status = GdipCreateRegionHrgn(hrgn, &region);
6225 if (status == Ok)
6227 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6229 if (status == Ok)
6230 status = GdipTransformRegion(region, &transform);
6232 if (status == Ok)
6233 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6235 GdipDeleteRegion(region);
6237 return status;
6240 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6242 GpStatus status;
6243 GpPath *clip_path;
6245 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6247 if(!graphics)
6248 return InvalidParameter;
6250 if(graphics->busy)
6251 return ObjectBusy;
6253 status = GdipClonePath(path, &clip_path);
6254 if (status == Ok)
6256 GpMatrix world_to_device;
6258 get_graphics_transform(graphics, CoordinateSpaceDevice,
6259 CoordinateSpaceWorld, &world_to_device);
6260 status = GdipTransformPath(clip_path, &world_to_device);
6261 if (status == Ok)
6262 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6264 GdipDeletePath(clip_path);
6266 return status;
6269 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6270 REAL width, REAL height,
6271 CombineMode mode)
6273 GpStatus status;
6274 GpRectF rect;
6275 GpRegion *region;
6277 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6279 if(!graphics)
6280 return InvalidParameter;
6282 if(graphics->busy)
6283 return ObjectBusy;
6285 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6287 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6288 if (status != Ok)
6289 return status;
6292 rect.X = x;
6293 rect.Y = y;
6294 rect.Width = width;
6295 rect.Height = height;
6296 status = GdipCreateRegionRect(&rect, &region);
6297 if (status == Ok)
6299 GpMatrix world_to_device;
6301 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6302 status = GdipTransformRegion(region, &world_to_device);
6303 if (status == Ok)
6304 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6306 GdipDeleteRegion(region);
6308 return status;
6311 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6312 INT width, INT height,
6313 CombineMode mode)
6315 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6317 if(!graphics)
6318 return InvalidParameter;
6320 if(graphics->busy)
6321 return ObjectBusy;
6323 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6326 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6327 CombineMode mode)
6329 GpStatus status;
6330 GpRegion *clip;
6332 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6334 if(!graphics || !region)
6335 return InvalidParameter;
6337 if(graphics->busy)
6338 return ObjectBusy;
6340 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6342 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6343 if (status != Ok)
6344 return status;
6347 status = GdipCloneRegion(region, &clip);
6348 if (status == Ok)
6350 GpMatrix world_to_device;
6352 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6353 status = GdipTransformRegion(clip, &world_to_device);
6354 if (status == Ok)
6355 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6357 GdipDeleteRegion(clip);
6359 return status;
6362 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6363 INT count)
6365 GpStatus status;
6366 GpPath* path;
6368 TRACE("(%p, %p, %d)\n", graphics, points, count);
6370 if(!graphics || !pen || count<=0)
6371 return InvalidParameter;
6373 if(graphics->busy)
6374 return ObjectBusy;
6376 status = GdipCreatePath(FillModeAlternate, &path);
6377 if (status != Ok) return status;
6379 status = GdipAddPathPolygon(path, points, count);
6380 if (status == Ok)
6381 status = GdipDrawPath(graphics, pen, path);
6383 GdipDeletePath(path);
6385 return status;
6388 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6389 INT count)
6391 GpStatus ret;
6392 GpPointF *ptf;
6393 INT i;
6395 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6397 if(count<=0) return InvalidParameter;
6398 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6400 for(i = 0;i < count; i++){
6401 ptf[i].X = (REAL)points[i].X;
6402 ptf[i].Y = (REAL)points[i].Y;
6405 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6406 heap_free(ptf);
6408 return ret;
6411 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6413 TRACE("(%p, %p)\n", graphics, dpi);
6415 if(!graphics || !dpi)
6416 return InvalidParameter;
6418 if(graphics->busy)
6419 return ObjectBusy;
6421 *dpi = graphics->xres;
6422 return Ok;
6425 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6427 TRACE("(%p, %p)\n", graphics, dpi);
6429 if(!graphics || !dpi)
6430 return InvalidParameter;
6432 if(graphics->busy)
6433 return ObjectBusy;
6435 *dpi = graphics->yres;
6436 return Ok;
6439 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6440 GpMatrixOrder order)
6442 GpMatrix m;
6443 GpStatus ret;
6445 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6447 if(!graphics || !matrix)
6448 return InvalidParameter;
6450 if(graphics->busy)
6451 return ObjectBusy;
6453 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6454 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6456 if (ret != Ok)
6457 return ret;
6460 m = graphics->worldtrans;
6462 ret = GdipMultiplyMatrix(&m, matrix, order);
6463 if(ret == Ok)
6464 graphics->worldtrans = m;
6466 return ret;
6469 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6470 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6472 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6474 GpStatus stat=Ok;
6476 TRACE("(%p, %p)\n", graphics, hdc);
6478 if(!graphics || !hdc)
6479 return InvalidParameter;
6481 if(graphics->busy)
6482 return ObjectBusy;
6484 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6486 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6488 else if (!graphics->hdc ||
6489 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6491 /* Create a fake HDC and fill it with a constant color. */
6492 HDC temp_hdc;
6493 HBITMAP hbitmap;
6494 GpRectF bounds;
6495 BITMAPINFOHEADER bmih;
6496 int i;
6498 stat = get_graphics_bounds(graphics, &bounds);
6499 if (stat != Ok)
6500 return stat;
6502 graphics->temp_hbitmap_width = bounds.Width;
6503 graphics->temp_hbitmap_height = bounds.Height;
6505 bmih.biSize = sizeof(bmih);
6506 bmih.biWidth = graphics->temp_hbitmap_width;
6507 bmih.biHeight = -graphics->temp_hbitmap_height;
6508 bmih.biPlanes = 1;
6509 bmih.biBitCount = 32;
6510 bmih.biCompression = BI_RGB;
6511 bmih.biSizeImage = 0;
6512 bmih.biXPelsPerMeter = 0;
6513 bmih.biYPelsPerMeter = 0;
6514 bmih.biClrUsed = 0;
6515 bmih.biClrImportant = 0;
6517 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6518 (void**)&graphics->temp_bits, NULL, 0);
6519 if (!hbitmap)
6520 return GenericError;
6522 temp_hdc = CreateCompatibleDC(0);
6523 if (!temp_hdc)
6525 DeleteObject(hbitmap);
6526 return GenericError;
6529 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6530 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6532 SelectObject(temp_hdc, hbitmap);
6534 graphics->temp_hbitmap = hbitmap;
6535 *hdc = graphics->temp_hdc = temp_hdc;
6537 else
6539 *hdc = graphics->hdc;
6542 if (stat == Ok)
6543 graphics->busy = TRUE;
6545 return stat;
6548 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6550 GpStatus stat=Ok;
6552 TRACE("(%p, %p)\n", graphics, hdc);
6554 if(!graphics || !hdc || !graphics->busy)
6555 return InvalidParameter;
6557 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6559 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6561 else if (graphics->temp_hdc == hdc)
6563 DWORD* pos;
6564 int i;
6566 /* Find the pixels that have changed, and mark them as opaque. */
6567 pos = (DWORD*)graphics->temp_bits;
6568 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6570 if (*pos != DC_BACKGROUND_KEY)
6572 *pos |= 0xff000000;
6574 pos++;
6577 /* Write the changed pixels to the real target. */
6578 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6579 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6580 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6582 /* Clean up. */
6583 DeleteDC(graphics->temp_hdc);
6584 DeleteObject(graphics->temp_hbitmap);
6585 graphics->temp_hdc = NULL;
6586 graphics->temp_hbitmap = NULL;
6588 else if (hdc != graphics->hdc)
6590 stat = InvalidParameter;
6593 if (stat == Ok)
6594 graphics->busy = FALSE;
6596 return stat;
6599 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6601 GpRegion *clip;
6602 GpStatus status;
6603 GpMatrix device_to_world;
6605 TRACE("(%p, %p)\n", graphics, region);
6607 if(!graphics || !region)
6608 return InvalidParameter;
6610 if(graphics->busy)
6611 return ObjectBusy;
6613 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6614 return status;
6616 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6617 status = GdipTransformRegion(clip, &device_to_world);
6618 if (status != Ok)
6620 GdipDeleteRegion(clip);
6621 return status;
6624 /* free everything except root node and header */
6625 delete_element(&region->node);
6626 memcpy(region, clip, sizeof(GpRegion));
6627 heap_free(clip);
6629 return Ok;
6632 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
6634 XFORM xform;
6636 if (graphics->hdc == NULL)
6638 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6639 return;
6642 if (graphics->gdi_transform_acquire_count)
6644 *matrix = graphics->gdi_transform;
6645 return;
6648 GetTransform(graphics->hdc, 0x204, &xform);
6649 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
6652 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6654 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6656 get_gdi_transform(graphics, &graphics->gdi_transform);
6657 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6658 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6659 SetMapMode(graphics->hdc, MM_TEXT);
6660 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6661 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6663 graphics->gdi_transform_acquire_count++;
6664 return Ok;
6667 GpStatus gdi_transform_release(GpGraphics *graphics)
6669 if (graphics->gdi_transform_acquire_count <= 0)
6671 ERR("called without matching gdi_transform_acquire");
6672 return GenericError;
6674 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6676 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6678 graphics->gdi_transform_acquire_count--;
6679 return Ok;
6682 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6683 GpCoordinateSpace src_space, GpMatrix *matrix)
6685 GpStatus stat = Ok;
6686 REAL scale_x, scale_y;
6688 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6690 if (dst_space != src_space)
6692 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6693 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6695 if(graphics->unit != UnitDisplay)
6697 scale_x *= graphics->scale;
6698 scale_y *= graphics->scale;
6701 if (dst_space < src_space)
6703 /* transform towards world space */
6704 switch ((int)src_space)
6706 case WineCoordinateSpaceGdiDevice:
6708 GpMatrix gdixform;
6709 get_gdi_transform(graphics, &gdixform);
6710 stat = GdipInvertMatrix(&gdixform);
6711 if (stat != Ok)
6712 break;
6713 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6714 if (dst_space == CoordinateSpaceDevice)
6715 break;
6716 /* else fall-through */
6718 case CoordinateSpaceDevice:
6719 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6720 if (dst_space == CoordinateSpacePage)
6721 break;
6722 /* else fall-through */
6723 case CoordinateSpacePage:
6725 GpMatrix inverted_transform = graphics->worldtrans;
6726 stat = GdipInvertMatrix(&inverted_transform);
6727 if (stat == Ok)
6728 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6729 break;
6733 else
6735 /* transform towards device space */
6736 switch ((int)src_space)
6738 case CoordinateSpaceWorld:
6739 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6740 if (dst_space == CoordinateSpacePage)
6741 break;
6742 /* else fall-through */
6743 case CoordinateSpacePage:
6744 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6745 if (dst_space == CoordinateSpaceDevice)
6746 break;
6747 /* else fall-through */
6748 case CoordinateSpaceDevice:
6750 GpMatrix gdixform;
6751 get_gdi_transform(graphics, &gdixform);
6752 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6753 break;
6758 return stat;
6761 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6762 GpCoordinateSpace src_space, GpPointF *points, INT count)
6764 GpMatrix matrix;
6765 GpStatus stat;
6767 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6768 if (stat != Ok) return stat;
6770 return GdipTransformMatrixPoints(&matrix, points, count);
6773 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6774 GpCoordinateSpace src_space, GpPointF *points, INT count)
6776 if(!graphics || !points || count <= 0 ||
6777 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6778 src_space < 0 || src_space > CoordinateSpaceDevice)
6779 return InvalidParameter;
6781 if(graphics->busy)
6782 return ObjectBusy;
6784 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6786 if (src_space == dst_space) return Ok;
6788 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6791 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6792 GpCoordinateSpace src_space, GpPoint *points, INT count)
6794 GpPointF *pointsF;
6795 GpStatus ret;
6796 INT i;
6798 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6800 if(count <= 0)
6801 return InvalidParameter;
6803 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6804 if(!pointsF)
6805 return OutOfMemory;
6807 for(i = 0; i < count; i++){
6808 pointsF[i].X = (REAL)points[i].X;
6809 pointsF[i].Y = (REAL)points[i].Y;
6812 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6814 if(ret == Ok)
6815 for(i = 0; i < count; i++){
6816 points[i].X = gdip_round(pointsF[i].X);
6817 points[i].Y = gdip_round(pointsF[i].Y);
6819 heap_free(pointsF);
6821 return ret;
6824 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6826 static int calls;
6828 TRACE("\n");
6830 if (!calls++)
6831 FIXME("stub\n");
6833 return NULL;
6836 /*****************************************************************************
6837 * GdipTranslateClip [GDIPLUS.@]
6839 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6841 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6843 if(!graphics)
6844 return InvalidParameter;
6846 if(graphics->busy)
6847 return ObjectBusy;
6849 return GdipTranslateRegion(graphics->clip, dx, dy);
6852 /*****************************************************************************
6853 * GdipTranslateClipI [GDIPLUS.@]
6855 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6857 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6859 if(!graphics)
6860 return InvalidParameter;
6862 if(graphics->busy)
6863 return ObjectBusy;
6865 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6869 /*****************************************************************************
6870 * GdipMeasureDriverString [GDIPLUS.@]
6872 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6873 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6874 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6876 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6877 HFONT hfont;
6878 HDC hdc;
6879 REAL min_x, min_y, max_x, max_y, x, y;
6880 int i;
6881 TEXTMETRICW textmetric;
6882 const WORD *glyph_indices;
6883 WORD *dynamic_glyph_indices=NULL;
6884 REAL rel_width, rel_height, ascent, descent;
6885 GpPointF pt[3];
6887 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6889 if (!graphics || !text || !font || !positions || !boundingBox)
6890 return InvalidParameter;
6892 if (length == -1)
6893 length = strlenW(text);
6895 if (length == 0)
6897 boundingBox->X = 0.0;
6898 boundingBox->Y = 0.0;
6899 boundingBox->Width = 0.0;
6900 boundingBox->Height = 0.0;
6903 if (flags & unsupported_flags)
6904 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6906 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6908 hdc = CreateCompatibleDC(0);
6909 SelectObject(hdc, hfont);
6911 GetTextMetricsW(hdc, &textmetric);
6913 pt[0].X = 0.0;
6914 pt[0].Y = 0.0;
6915 pt[1].X = 1.0;
6916 pt[1].Y = 0.0;
6917 pt[2].X = 0.0;
6918 pt[2].Y = 1.0;
6919 if (matrix)
6921 GpMatrix xform = *matrix;
6922 GdipTransformMatrixPoints(&xform, pt, 3);
6924 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
6925 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6926 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6927 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6928 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6930 if (flags & DriverStringOptionsCmapLookup)
6932 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
6933 if (!glyph_indices)
6935 DeleteDC(hdc);
6936 DeleteObject(hfont);
6937 return OutOfMemory;
6940 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6942 else
6943 glyph_indices = text;
6945 min_x = max_x = x = positions[0].X;
6946 min_y = max_y = y = positions[0].Y;
6948 ascent = textmetric.tmAscent / rel_height;
6949 descent = textmetric.tmDescent / rel_height;
6951 for (i=0; i<length; i++)
6953 int char_width;
6954 ABC abc;
6956 if (!(flags & DriverStringOptionsRealizedAdvance))
6958 x = positions[i].X;
6959 y = positions[i].Y;
6962 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6963 char_width = abc.abcA + abc.abcB + abc.abcC;
6965 if (min_y > y - ascent) min_y = y - ascent;
6966 if (max_y < y + descent) max_y = y + descent;
6967 if (min_x > x) min_x = x;
6969 x += char_width / rel_width;
6971 if (max_x < x) max_x = x;
6974 heap_free(dynamic_glyph_indices);
6975 DeleteDC(hdc);
6976 DeleteObject(hfont);
6978 boundingBox->X = min_x;
6979 boundingBox->Y = min_y;
6980 boundingBox->Width = max_x - min_x;
6981 boundingBox->Height = max_y - min_y;
6983 return Ok;
6986 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6987 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6988 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6989 INT flags, GDIPCONST GpMatrix *matrix)
6991 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6992 INT save_state;
6993 GpPointF pt;
6994 HFONT hfont;
6995 UINT eto_flags=0;
6996 GpStatus status;
6997 HRGN hrgn;
6999 if (flags & unsupported_flags)
7000 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7002 if (!(flags & DriverStringOptionsCmapLookup))
7003 eto_flags |= ETO_GLYPH_INDEX;
7005 save_state = SaveDC(graphics->hdc);
7006 SetBkMode(graphics->hdc, TRANSPARENT);
7007 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7009 status = get_clip_hrgn(graphics, &hrgn);
7011 if (status == Ok && hrgn)
7013 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
7014 DeleteObject(hrgn);
7017 pt = positions[0];
7018 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7020 get_font_hfont(graphics, font, format, &hfont, matrix);
7021 SelectObject(graphics->hdc, hfont);
7023 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7025 gdi_transform_acquire(graphics);
7027 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
7029 gdi_transform_release(graphics);
7031 RestoreDC(graphics->hdc, save_state);
7033 DeleteObject(hfont);
7035 return Ok;
7038 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7039 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7040 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7041 INT flags, GDIPCONST GpMatrix *matrix)
7043 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7044 GpStatus stat;
7045 PointF *real_positions, real_position;
7046 POINT *pti;
7047 HFONT hfont;
7048 HDC hdc;
7049 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7050 DWORD max_glyphsize=0;
7051 GLYPHMETRICS glyphmetrics;
7052 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7053 BYTE *glyph_mask;
7054 BYTE *text_mask;
7055 int text_mask_stride;
7056 BYTE *pixel_data;
7057 int pixel_data_stride;
7058 GpRect pixel_area;
7059 UINT ggo_flags = GGO_GRAY8_BITMAP;
7061 if (length <= 0)
7062 return Ok;
7064 if (!(flags & DriverStringOptionsCmapLookup))
7065 ggo_flags |= GGO_GLYPH_INDEX;
7067 if (flags & unsupported_flags)
7068 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7070 pti = heap_alloc_zero(sizeof(POINT) * length);
7071 if (!pti)
7072 return OutOfMemory;
7074 if (flags & DriverStringOptionsRealizedAdvance)
7076 real_position = positions[0];
7078 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7079 round_points(pti, &real_position, 1);
7081 else
7083 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7084 if (!real_positions)
7086 heap_free(pti);
7087 return OutOfMemory;
7090 memcpy(real_positions, positions, sizeof(PointF) * length);
7092 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7093 round_points(pti, real_positions, length);
7095 heap_free(real_positions);
7098 get_font_hfont(graphics, font, format, &hfont, matrix);
7100 hdc = CreateCompatibleDC(0);
7101 SelectObject(hdc, hfont);
7103 /* Get the boundaries of the text to be drawn */
7104 for (i=0; i<length; i++)
7106 DWORD glyphsize;
7107 int left, top, right, bottom;
7109 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7110 &glyphmetrics, 0, NULL, &identity);
7112 if (glyphsize == GDI_ERROR)
7114 ERR("GetGlyphOutlineW failed\n");
7115 heap_free(pti);
7116 DeleteDC(hdc);
7117 DeleteObject(hfont);
7118 return GenericError;
7121 if (glyphsize > max_glyphsize)
7122 max_glyphsize = glyphsize;
7124 if (glyphsize != 0)
7126 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7127 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7128 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7129 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7131 if (left < min_x) min_x = left;
7132 if (top < min_y) min_y = top;
7133 if (right > max_x) max_x = right;
7134 if (bottom > max_y) max_y = bottom;
7137 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7139 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7140 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7144 if (max_glyphsize == 0)
7145 /* Nothing to draw. */
7146 return Ok;
7148 glyph_mask = heap_alloc_zero(max_glyphsize);
7149 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7150 text_mask_stride = max_x - min_x;
7152 if (!(glyph_mask && text_mask))
7154 heap_free(glyph_mask);
7155 heap_free(text_mask);
7156 heap_free(pti);
7157 DeleteDC(hdc);
7158 DeleteObject(hfont);
7159 return OutOfMemory;
7162 /* Generate a mask for the text */
7163 for (i=0; i<length; i++)
7165 DWORD ret;
7166 int left, top, stride;
7168 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7169 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7171 if (ret == GDI_ERROR || ret == 0)
7172 continue; /* empty glyph */
7174 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7175 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7176 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7178 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7180 BYTE *glyph_val = glyph_mask + y * stride;
7181 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7182 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7184 *text_val = min(64, *text_val + *glyph_val);
7185 glyph_val++;
7186 text_val++;
7191 heap_free(pti);
7192 DeleteDC(hdc);
7193 DeleteObject(hfont);
7194 heap_free(glyph_mask);
7196 /* get the brush data */
7197 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7198 if (!pixel_data)
7200 heap_free(text_mask);
7201 return OutOfMemory;
7204 pixel_area.X = min_x;
7205 pixel_area.Y = min_y;
7206 pixel_area.Width = max_x - min_x;
7207 pixel_area.Height = max_y - min_y;
7208 pixel_data_stride = pixel_area.Width * 4;
7210 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7211 if (stat != Ok)
7213 heap_free(text_mask);
7214 heap_free(pixel_data);
7215 return stat;
7218 /* multiply the brush data by the mask */
7219 for (y=0; y<pixel_area.Height; y++)
7221 BYTE *text_val = text_mask + text_mask_stride * y;
7222 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7223 for (x=0; x<pixel_area.Width; x++)
7225 *pixel_val = (*pixel_val) * (*text_val) / 64;
7226 text_val++;
7227 pixel_val+=4;
7231 heap_free(text_mask);
7233 gdi_transform_acquire(graphics);
7235 /* draw the result */
7236 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7237 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7239 gdi_transform_release(graphics);
7241 heap_free(pixel_data);
7243 return stat;
7246 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7247 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7248 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7249 INT flags, GDIPCONST GpMatrix *matrix)
7251 GpStatus stat = NotImplemented;
7253 if (length == -1)
7254 length = strlenW(text);
7256 if (graphics->hdc && !graphics->alpha_hdc &&
7257 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
7258 brush->bt == BrushTypeSolidColor &&
7259 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7260 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7261 brush, positions, flags, matrix);
7262 if (stat == NotImplemented)
7263 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7264 brush, positions, flags, matrix);
7265 return stat;
7268 /*****************************************************************************
7269 * GdipDrawDriverString [GDIPLUS.@]
7271 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7272 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7273 GDIPCONST PointF *positions, INT flags,
7274 GDIPCONST GpMatrix *matrix )
7276 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7278 if (!graphics || !text || !font || !brush || !positions)
7279 return InvalidParameter;
7281 return draw_driver_string(graphics, text, length, font, NULL,
7282 brush, positions, flags, matrix);
7285 /*****************************************************************************
7286 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7288 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7290 GpStatus stat;
7291 GpRegion* rgn;
7293 TRACE("(%p, %p)\n", graphics, res);
7295 if((stat = GdipCreateRegion(&rgn)) != Ok)
7296 return stat;
7298 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7299 goto cleanup;
7301 stat = GdipIsEmptyRegion(rgn, graphics, res);
7303 cleanup:
7304 GdipDeleteRegion(rgn);
7305 return stat;
7308 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7310 static int calls;
7312 TRACE("(%p) stub\n", graphics);
7314 if(!(calls++))
7315 FIXME("not implemented\n");
7317 return NotImplemented;
7320 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7322 TRACE("(%p, %p)\n", graphics, pabort);
7324 if (!graphics)
7325 return InvalidParameter;
7327 if (pabort)
7328 FIXME("Abort callback is not supported.\n");
7330 return Ok;