appwiz.cpl: Add comment explaining why we use HTTP instead of HTTPS.
[wine.git] / dlls / gdiplus / graphics.c
blob06fb89e5d32e4af6017b6acaebddc7726f5976c6
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 if (stat == Ok && graphics->gdi_clip)
353 if (*hrgn)
354 CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
355 else
357 *hrgn = CreateRectRgn(0,0,0,0);
358 CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
362 return stat;
365 /* Draw ARGB data to the given graphics object */
366 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
367 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
369 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
370 INT x, y;
372 for (y=0; y<src_height; y++)
374 for (x=0; x<src_width; x++)
376 ARGB dst_color, src_color;
377 src_color = ((ARGB*)(src + src_stride * y))[x];
379 if (!(src_color & 0xff000000))
380 continue;
382 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
383 if (fmt & PixelFormatPAlpha)
384 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
385 else
386 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
390 return Ok;
393 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
394 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
396 HDC hdc;
397 HBITMAP hbitmap;
398 BITMAPINFOHEADER bih;
399 BYTE *temp_bits;
401 hdc = CreateCompatibleDC(0);
403 bih.biSize = sizeof(BITMAPINFOHEADER);
404 bih.biWidth = src_width;
405 bih.biHeight = -src_height;
406 bih.biPlanes = 1;
407 bih.biBitCount = 32;
408 bih.biCompression = BI_RGB;
409 bih.biSizeImage = 0;
410 bih.biXPelsPerMeter = 0;
411 bih.biYPelsPerMeter = 0;
412 bih.biClrUsed = 0;
413 bih.biClrImportant = 0;
415 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
416 (void**)&temp_bits, NULL, 0);
418 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
419 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
420 fmt & PixelFormatPAlpha)
421 memcpy(temp_bits, src, src_width * src_height * 4);
422 else
423 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
424 4 * src_width, src, src_stride);
426 SelectObject(hdc, hbitmap);
427 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
428 hdc, 0, 0, src_width, src_height);
429 DeleteDC(hdc);
430 DeleteObject(hbitmap);
432 return Ok;
435 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
436 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
438 GpStatus stat=Ok;
440 if (graphics->image && graphics->image->type == ImageTypeBitmap)
442 DWORD i;
443 int size;
444 RGNDATA *rgndata;
445 RECT *rects;
446 HRGN hrgn, visible_rgn;
448 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
449 if (!hrgn)
450 return OutOfMemory;
452 stat = get_clip_hrgn(graphics, &visible_rgn);
453 if (stat != Ok)
455 DeleteObject(hrgn);
456 return stat;
459 if (visible_rgn)
461 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
462 DeleteObject(visible_rgn);
465 if (hregion)
466 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
468 size = GetRegionData(hrgn, 0, NULL);
470 rgndata = heap_alloc_zero(size);
471 if (!rgndata)
473 DeleteObject(hrgn);
474 return OutOfMemory;
477 GetRegionData(hrgn, size, rgndata);
479 rects = (RECT*)rgndata->Buffer;
481 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
483 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
484 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
485 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
486 src_stride, fmt);
489 heap_free(rgndata);
491 DeleteObject(hrgn);
493 return stat;
495 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
497 ERR("This should not be used for metafiles; fix caller\n");
498 return NotImplemented;
500 else
502 HRGN hrgn;
503 int save;
505 stat = get_clip_hrgn(graphics, &hrgn);
507 if (stat != Ok)
508 return stat;
510 save = SaveDC(graphics->hdc);
512 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
514 if (hregion)
515 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
517 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
518 src_height, src_stride, fmt);
520 RestoreDC(graphics->hdc, save);
522 DeleteObject(hrgn);
524 return stat;
528 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
529 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
531 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
534 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
536 INT start_a, end_a, final_a;
537 INT pos;
539 pos = gdip_round(position * 0xff);
541 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
542 end_a = ((end >> 24) & 0xff) * pos;
544 final_a = start_a + end_a;
546 if (final_a < 0xff) return 0;
548 return (final_a / 0xff) << 24 |
549 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
550 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
551 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
554 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
556 REAL blendfac;
558 /* clamp to between 0.0 and 1.0, using the wrap mode */
559 position = (position - brush->rect.X) / brush->rect.Width;
560 if (brush->wrap == WrapModeTile)
562 position = fmodf(position, 1.0f);
563 if (position < 0.0f) position += 1.0f;
565 else /* WrapModeFlip* */
567 position = fmodf(position, 2.0f);
568 if (position < 0.0f) position += 2.0f;
569 if (position > 1.0f) position = 2.0f - position;
572 if (brush->blendcount == 1)
573 blendfac = position;
574 else
576 int i=1;
577 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
578 REAL range;
580 /* locate the blend positions surrounding this position */
581 while (position > brush->blendpos[i])
582 i++;
584 /* interpolate between the blend positions */
585 left_blendpos = brush->blendpos[i-1];
586 left_blendfac = brush->blendfac[i-1];
587 right_blendpos = brush->blendpos[i];
588 right_blendfac = brush->blendfac[i];
589 range = right_blendpos - left_blendpos;
590 blendfac = (left_blendfac * (right_blendpos - position) +
591 right_blendfac * (position - left_blendpos)) / range;
594 if (brush->pblendcount == 0)
595 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
596 else
598 int i=1;
599 ARGB left_blendcolor, right_blendcolor;
600 REAL left_blendpos, right_blendpos;
602 /* locate the blend colors surrounding this position */
603 while (blendfac > brush->pblendpos[i])
604 i++;
606 /* interpolate between the blend colors */
607 left_blendpos = brush->pblendpos[i-1];
608 left_blendcolor = brush->pblendcolor[i-1];
609 right_blendpos = brush->pblendpos[i];
610 right_blendcolor = brush->pblendcolor[i];
611 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
612 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
616 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
618 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
619 BOOL identity = TRUE;
620 int i, j;
622 for (i=0; i<4; i++)
623 for (j=0; j<5; j++)
625 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
626 identity = FALSE;
627 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
630 return identity;
633 static ARGB transform_color(ARGB color, int matrix[5][5])
635 int val[5], res[4];
636 int i, j;
637 unsigned char a, r, g, b;
639 val[0] = ((color >> 16) & 0xff); /* red */
640 val[1] = ((color >> 8) & 0xff); /* green */
641 val[2] = (color & 0xff); /* blue */
642 val[3] = ((color >> 24) & 0xff); /* alpha */
643 val[4] = 255; /* translation */
645 for (i=0; i<4; i++)
647 res[i] = 0;
649 for (j=0; j<5; j++)
650 res[i] += matrix[j][i] * val[j];
653 a = min(max(res[3] / 256, 0), 255);
654 r = min(max(res[0] / 256, 0), 255);
655 g = min(max(res[1] / 256, 0), 255);
656 b = min(max(res[2] / 256, 0), 255);
658 return (a << 24) | (r << 16) | (g << 8) | b;
661 static BOOL color_is_gray(ARGB color)
663 unsigned char r, g, b;
665 r = (color >> 16) & 0xff;
666 g = (color >> 8) & 0xff;
667 b = color & 0xff;
669 return (r == g) && (g == b);
672 /* returns preferred pixel format for the applied attributes */
673 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
674 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
676 UINT x, y;
677 INT i;
679 if ((attributes->noop[type] == IMAGEATTR_NOOP_UNDEFINED &&
680 attributes->noop[ColorAdjustTypeDefault] == IMAGEATTR_NOOP_SET) ||
681 (attributes->noop[type] == IMAGEATTR_NOOP_SET))
682 return fmt;
684 if (attributes->colorkeys[type].enabled ||
685 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
687 const struct color_key *key;
688 BYTE min_blue, min_green, min_red;
689 BYTE max_blue, max_green, max_red;
691 if (!data || fmt != PixelFormat32bppARGB)
692 return PixelFormat32bppARGB;
694 if (attributes->colorkeys[type].enabled)
695 key = &attributes->colorkeys[type];
696 else
697 key = &attributes->colorkeys[ColorAdjustTypeDefault];
699 min_blue = key->low&0xff;
700 min_green = (key->low>>8)&0xff;
701 min_red = (key->low>>16)&0xff;
703 max_blue = key->high&0xff;
704 max_green = (key->high>>8)&0xff;
705 max_red = (key->high>>16)&0xff;
707 for (x=0; x<width; x++)
708 for (y=0; y<height; y++)
710 ARGB *src_color;
711 BYTE blue, green, red;
712 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
713 blue = *src_color&0xff;
714 green = (*src_color>>8)&0xff;
715 red = (*src_color>>16)&0xff;
716 if (blue >= min_blue && green >= min_green && red >= min_red &&
717 blue <= max_blue && green <= max_green && red <= max_red)
718 *src_color = 0x00000000;
722 if (attributes->colorremaptables[type].enabled ||
723 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
725 const struct color_remap_table *table;
727 if (!data || fmt != PixelFormat32bppARGB)
728 return PixelFormat32bppARGB;
730 if (attributes->colorremaptables[type].enabled)
731 table = &attributes->colorremaptables[type];
732 else
733 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
735 for (x=0; x<width; x++)
736 for (y=0; y<height; y++)
738 ARGB *src_color;
739 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
740 for (i=0; i<table->mapsize; i++)
742 if (*src_color == table->colormap[i].oldColor.Argb)
744 *src_color = table->colormap[i].newColor.Argb;
745 break;
751 if (attributes->colormatrices[type].enabled ||
752 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
754 const struct color_matrix *colormatrices;
755 int color_matrix[5][5];
756 int gray_matrix[5][5];
757 BOOL identity;
759 if (!data || fmt != PixelFormat32bppARGB)
760 return PixelFormat32bppARGB;
762 if (attributes->colormatrices[type].enabled)
763 colormatrices = &attributes->colormatrices[type];
764 else
765 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
767 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
769 if (colormatrices->flags == ColorMatrixFlagsAltGray)
770 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
772 if (!identity)
774 for (x=0; x<width; x++)
776 for (y=0; y<height; y++)
778 ARGB *src_color;
779 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
781 if (colormatrices->flags == ColorMatrixFlagsDefault ||
782 !color_is_gray(*src_color))
784 *src_color = transform_color(*src_color, color_matrix);
786 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
788 *src_color = transform_color(*src_color, gray_matrix);
795 if (attributes->gamma_enabled[type] ||
796 attributes->gamma_enabled[ColorAdjustTypeDefault])
798 REAL gamma;
800 if (!data || fmt != PixelFormat32bppARGB)
801 return PixelFormat32bppARGB;
803 if (attributes->gamma_enabled[type])
804 gamma = attributes->gamma[type];
805 else
806 gamma = attributes->gamma[ColorAdjustTypeDefault];
808 for (x=0; x<width; x++)
809 for (y=0; y<height; y++)
811 ARGB *src_color;
812 BYTE blue, green, red;
813 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
815 blue = *src_color&0xff;
816 green = (*src_color>>8)&0xff;
817 red = (*src_color>>16)&0xff;
819 /* FIXME: We should probably use a table for this. */
820 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
821 green = floorf(powf(green / 255.0, gamma) * 255.0);
822 red = floorf(powf(red / 255.0, gamma) * 255.0);
824 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
828 return fmt;
831 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
832 * bitmap that contains all the pixels we may need to draw it. */
833 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
834 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
835 GpRect *rect)
837 INT left, top, right, bottom;
839 switch (interpolation)
841 case InterpolationModeHighQualityBilinear:
842 case InterpolationModeHighQualityBicubic:
843 /* FIXME: Include a greater range for the prefilter? */
844 case InterpolationModeBicubic:
845 case InterpolationModeBilinear:
846 left = (INT)(floorf(srcx));
847 top = (INT)(floorf(srcy));
848 right = (INT)(ceilf(srcx+srcwidth));
849 bottom = (INT)(ceilf(srcy+srcheight));
850 break;
851 case InterpolationModeNearestNeighbor:
852 default:
853 left = gdip_round(srcx);
854 top = gdip_round(srcy);
855 right = gdip_round(srcx+srcwidth);
856 bottom = gdip_round(srcy+srcheight);
857 break;
860 if (wrap == WrapModeClamp)
862 if (left < 0)
863 left = 0;
864 if (top < 0)
865 top = 0;
866 if (right >= bitmap->width)
867 right = bitmap->width-1;
868 if (bottom >= bitmap->height)
869 bottom = bitmap->height-1;
870 if (bottom < top || right < left)
871 /* entirely outside image, just sample a pixel so we don't have to
872 * special-case this later */
873 left = top = right = bottom = 0;
875 else
877 /* In some cases we can make the rectangle smaller here, but the logic
878 * is hard to get right, and tiling suggests we're likely to use the
879 * entire source image. */
880 if (left < 0 || right >= bitmap->width)
882 left = 0;
883 right = bitmap->width-1;
886 if (top < 0 || bottom >= bitmap->height)
888 top = 0;
889 bottom = bitmap->height-1;
893 rect->X = left;
894 rect->Y = top;
895 rect->Width = right - left + 1;
896 rect->Height = bottom - top + 1;
899 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
900 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
902 if (attributes->wrap == WrapModeClamp)
904 if (x < 0 || y < 0 || x >= width || y >= height)
905 return attributes->outside_color;
907 else
909 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
910 if (x < 0)
911 x = width*2 + x % (width * 2);
912 if (y < 0)
913 y = height*2 + y % (height * 2);
915 if (attributes->wrap & WrapModeTileFlipX)
917 if ((x / width) % 2 == 0)
918 x = x % width;
919 else
920 x = width - 1 - x % width;
922 else
923 x = x % width;
925 if (attributes->wrap & WrapModeTileFlipY)
927 if ((y / height) % 2 == 0)
928 y = y % height;
929 else
930 y = height - 1 - y % height;
932 else
933 y = y % height;
936 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
938 ERR("out of range pixel requested\n");
939 return 0xffcd0084;
942 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
945 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
946 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
947 InterpolationMode interpolation, PixelOffsetMode offset_mode)
949 static int fixme;
951 switch (interpolation)
953 default:
954 if (!fixme++)
955 FIXME("Unimplemented interpolation %i\n", interpolation);
956 /* fall-through */
957 case InterpolationModeBilinear:
959 REAL leftxf, topyf;
960 INT leftx, rightx, topy, bottomy;
961 ARGB topleft, topright, bottomleft, bottomright;
962 ARGB top, bottom;
963 float x_offset;
965 leftxf = floorf(point->X);
966 leftx = (INT)leftxf;
967 rightx = (INT)ceilf(point->X);
968 topyf = floorf(point->Y);
969 topy = (INT)topyf;
970 bottomy = (INT)ceilf(point->Y);
972 if (leftx == rightx && topy == bottomy)
973 return sample_bitmap_pixel(src_rect, bits, width, height,
974 leftx, topy, attributes);
976 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
977 leftx, topy, attributes);
978 topright = sample_bitmap_pixel(src_rect, bits, width, height,
979 rightx, topy, attributes);
980 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
981 leftx, bottomy, attributes);
982 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
983 rightx, bottomy, attributes);
985 x_offset = point->X - leftxf;
986 top = blend_colors(topleft, topright, x_offset);
987 bottom = blend_colors(bottomleft, bottomright, x_offset);
989 return blend_colors(top, bottom, point->Y - topyf);
991 case InterpolationModeNearestNeighbor:
993 FLOAT pixel_offset;
994 switch (offset_mode)
996 default:
997 case PixelOffsetModeNone:
998 case PixelOffsetModeHighSpeed:
999 pixel_offset = 0.5;
1000 break;
1002 case PixelOffsetModeHalf:
1003 case PixelOffsetModeHighQuality:
1004 pixel_offset = 0.0;
1005 break;
1007 return sample_bitmap_pixel(src_rect, bits, width, height,
1008 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1014 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1016 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1019 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1020 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1022 switch (brush->bt)
1024 case BrushTypeSolidColor:
1026 if (is_fill)
1027 return TRUE;
1028 else
1030 /* cannot draw semi-transparent colors */
1031 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1034 case BrushTypeHatchFill:
1036 GpHatch *hatch = (GpHatch*)brush;
1037 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1038 ((hatch->backcol & 0xff000000) == 0xff000000);
1040 case BrushTypeLinearGradient:
1041 case BrushTypeTextureFill:
1042 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1043 default:
1044 return FALSE;
1048 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1050 switch (brush->bt)
1052 case BrushTypeSolidColor:
1054 GpSolidFill *fill = (GpSolidFill*)brush;
1055 HBITMAP bmp = ARGB2BMP(fill->color);
1057 if (bmp)
1059 RECT rc;
1060 /* partially transparent fill */
1062 SelectClipPath(graphics->hdc, RGN_AND);
1063 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1065 HDC hdc = CreateCompatibleDC(NULL);
1067 if (!hdc) break;
1069 SelectObject(hdc, bmp);
1070 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1071 hdc, 0, 0, 1, 1);
1072 DeleteDC(hdc);
1075 DeleteObject(bmp);
1076 break;
1078 /* else fall through */
1080 default:
1082 HBRUSH gdibrush, old_brush;
1084 gdibrush = create_gdi_brush(brush);
1085 if (!gdibrush) return;
1087 old_brush = SelectObject(graphics->hdc, gdibrush);
1088 FillPath(graphics->hdc);
1089 SelectObject(graphics->hdc, old_brush);
1090 DeleteObject(gdibrush);
1091 break;
1096 static BOOL brush_can_fill_pixels(GpBrush *brush)
1098 switch (brush->bt)
1100 case BrushTypeSolidColor:
1101 case BrushTypeHatchFill:
1102 case BrushTypeLinearGradient:
1103 case BrushTypeTextureFill:
1104 case BrushTypePathGradient:
1105 return TRUE;
1106 default:
1107 return FALSE;
1111 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1112 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1114 switch (brush->bt)
1116 case BrushTypeSolidColor:
1118 int x, y;
1119 GpSolidFill *fill = (GpSolidFill*)brush;
1120 for (x=0; x<fill_area->Width; x++)
1121 for (y=0; y<fill_area->Height; y++)
1122 argb_pixels[x + y*cdwStride] = fill->color;
1123 return Ok;
1125 case BrushTypeHatchFill:
1127 int x, y;
1128 GpHatch *fill = (GpHatch*)brush;
1129 const char *hatch_data;
1131 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1132 return NotImplemented;
1134 for (x=0; x<fill_area->Width; x++)
1135 for (y=0; y<fill_area->Height; y++)
1137 int hx, hy;
1139 /* FIXME: Account for the rendering origin */
1140 hx = (x + fill_area->X) % 8;
1141 hy = (y + fill_area->Y) % 8;
1143 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1144 argb_pixels[x + y*cdwStride] = fill->forecol;
1145 else
1146 argb_pixels[x + y*cdwStride] = fill->backcol;
1149 return Ok;
1151 case BrushTypeLinearGradient:
1153 GpLineGradient *fill = (GpLineGradient*)brush;
1154 GpPointF draw_points[3];
1155 GpStatus stat;
1156 int x, y;
1158 draw_points[0].X = fill_area->X;
1159 draw_points[0].Y = fill_area->Y;
1160 draw_points[1].X = fill_area->X+1;
1161 draw_points[1].Y = fill_area->Y;
1162 draw_points[2].X = fill_area->X;
1163 draw_points[2].Y = fill_area->Y+1;
1165 /* Transform the points to a co-ordinate space where X is the point's
1166 * position in the gradient, 0.0 being the start point and 1.0 the
1167 * end point. */
1168 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1169 WineCoordinateSpaceGdiDevice, draw_points, 3);
1171 if (stat == Ok)
1173 GpMatrix world_to_gradient = fill->transform;
1175 stat = GdipInvertMatrix(&world_to_gradient);
1176 if (stat == Ok)
1177 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1180 if (stat == Ok)
1182 REAL x_delta = draw_points[1].X - draw_points[0].X;
1183 REAL y_delta = draw_points[2].X - draw_points[0].X;
1185 for (y=0; y<fill_area->Height; y++)
1187 for (x=0; x<fill_area->Width; x++)
1189 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1191 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1196 return stat;
1198 case BrushTypeTextureFill:
1200 GpTexture *fill = (GpTexture*)brush;
1201 GpPointF draw_points[3];
1202 GpStatus stat;
1203 int x, y;
1204 GpBitmap *bitmap;
1205 int src_stride;
1206 GpRect src_area;
1208 if (fill->image->type != ImageTypeBitmap)
1210 FIXME("metafile texture brushes not implemented\n");
1211 return NotImplemented;
1214 bitmap = (GpBitmap*)fill->image;
1215 src_stride = sizeof(ARGB) * bitmap->width;
1217 src_area.X = src_area.Y = 0;
1218 src_area.Width = bitmap->width;
1219 src_area.Height = bitmap->height;
1221 draw_points[0].X = fill_area->X;
1222 draw_points[0].Y = fill_area->Y;
1223 draw_points[1].X = fill_area->X+1;
1224 draw_points[1].Y = fill_area->Y;
1225 draw_points[2].X = fill_area->X;
1226 draw_points[2].Y = fill_area->Y+1;
1228 /* Transform the points to the co-ordinate space of the bitmap. */
1229 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1230 WineCoordinateSpaceGdiDevice, draw_points, 3);
1232 if (stat == Ok)
1234 GpMatrix world_to_texture = fill->transform;
1236 stat = GdipInvertMatrix(&world_to_texture);
1237 if (stat == Ok)
1238 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1241 if (stat == Ok && !fill->bitmap_bits)
1243 BitmapData lockeddata;
1245 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1246 if (!fill->bitmap_bits)
1247 stat = OutOfMemory;
1249 if (stat == Ok)
1251 lockeddata.Width = bitmap->width;
1252 lockeddata.Height = bitmap->height;
1253 lockeddata.Stride = src_stride;
1254 lockeddata.PixelFormat = PixelFormat32bppARGB;
1255 lockeddata.Scan0 = fill->bitmap_bits;
1257 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1258 PixelFormat32bppARGB, &lockeddata);
1261 if (stat == Ok)
1262 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1264 if (stat == Ok)
1265 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1266 bitmap->width, bitmap->height,
1267 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1269 if (stat != Ok)
1271 heap_free(fill->bitmap_bits);
1272 fill->bitmap_bits = NULL;
1276 if (stat == Ok)
1278 REAL x_dx = draw_points[1].X - draw_points[0].X;
1279 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1280 REAL y_dx = draw_points[2].X - draw_points[0].X;
1281 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1283 for (y=0; y<fill_area->Height; y++)
1285 for (x=0; x<fill_area->Width; x++)
1287 GpPointF point;
1288 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1289 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1291 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1292 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1293 &point, fill->imageattributes, graphics->interpolation,
1294 graphics->pixeloffset);
1299 return stat;
1301 case BrushTypePathGradient:
1303 GpPathGradient *fill = (GpPathGradient*)brush;
1304 GpPath *flat_path;
1305 GpMatrix world_to_device;
1306 GpStatus stat;
1307 int i, figure_start=0;
1308 GpPointF start_point, end_point, center_point;
1309 BYTE type;
1310 REAL min_yf, max_yf, line1_xf, line2_xf;
1311 INT min_y, max_y, min_x, max_x;
1312 INT x, y;
1313 ARGB outer_color;
1314 static BOOL transform_fixme_once;
1316 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1318 static int once;
1319 if (!once++)
1320 FIXME("path gradient focus not implemented\n");
1323 if (fill->gamma)
1325 static int once;
1326 if (!once++)
1327 FIXME("path gradient gamma correction not implemented\n");
1330 if (fill->blendcount)
1332 static int once;
1333 if (!once++)
1334 FIXME("path gradient blend not implemented\n");
1337 if (fill->pblendcount)
1339 static int once;
1340 if (!once++)
1341 FIXME("path gradient preset blend not implemented\n");
1344 if (!transform_fixme_once)
1346 BOOL is_identity=TRUE;
1347 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1348 if (!is_identity)
1350 FIXME("path gradient transform not implemented\n");
1351 transform_fixme_once = TRUE;
1355 stat = GdipClonePath(fill->path, &flat_path);
1357 if (stat != Ok)
1358 return stat;
1360 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1361 CoordinateSpaceWorld, &world_to_device);
1362 if (stat == Ok)
1364 stat = GdipTransformPath(flat_path, &world_to_device);
1366 if (stat == Ok)
1368 center_point = fill->center;
1369 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1372 if (stat == Ok)
1373 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1376 if (stat != Ok)
1378 GdipDeletePath(flat_path);
1379 return stat;
1382 for (i=0; i<flat_path->pathdata.Count; i++)
1384 int start_center_line=0, end_center_line=0;
1385 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1386 REAL center_distance;
1387 ARGB start_color, end_color;
1388 REAL dy, dx;
1390 type = flat_path->pathdata.Types[i];
1392 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1393 figure_start = i;
1395 start_point = flat_path->pathdata.Points[i];
1397 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1399 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1401 end_point = flat_path->pathdata.Points[figure_start];
1402 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1404 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1406 end_point = flat_path->pathdata.Points[i+1];
1407 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1409 else
1410 continue;
1412 outer_color = start_color;
1414 min_yf = center_point.Y;
1415 if (min_yf > start_point.Y) min_yf = start_point.Y;
1416 if (min_yf > end_point.Y) min_yf = end_point.Y;
1418 if (min_yf < fill_area->Y)
1419 min_y = fill_area->Y;
1420 else
1421 min_y = (INT)ceil(min_yf);
1423 max_yf = center_point.Y;
1424 if (max_yf < start_point.Y) max_yf = start_point.Y;
1425 if (max_yf < end_point.Y) max_yf = end_point.Y;
1427 if (max_yf > fill_area->Y + fill_area->Height)
1428 max_y = fill_area->Y + fill_area->Height;
1429 else
1430 max_y = (INT)ceil(max_yf);
1432 dy = end_point.Y - start_point.Y;
1433 dx = end_point.X - start_point.X;
1435 /* This is proportional to the distance from start-end line to center point. */
1436 center_distance = dy * (start_point.X - center_point.X) +
1437 dx * (center_point.Y - start_point.Y);
1439 for (y=min_y; y<max_y; y++)
1441 REAL yf = (REAL)y;
1443 if (!seen_start && yf >= start_point.Y)
1445 seen_start = TRUE;
1446 start_center_line ^= 1;
1448 if (!seen_end && yf >= end_point.Y)
1450 seen_end = TRUE;
1451 end_center_line ^= 1;
1453 if (!seen_center && yf >= center_point.Y)
1455 seen_center = TRUE;
1456 start_center_line ^= 1;
1457 end_center_line ^= 1;
1460 if (start_center_line)
1461 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1462 else
1463 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1465 if (end_center_line)
1466 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1467 else
1468 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1470 if (line1_xf < line2_xf)
1472 min_x = (INT)ceil(line1_xf);
1473 max_x = (INT)ceil(line2_xf);
1475 else
1477 min_x = (INT)ceil(line2_xf);
1478 max_x = (INT)ceil(line1_xf);
1481 if (min_x < fill_area->X)
1482 min_x = fill_area->X;
1483 if (max_x > fill_area->X + fill_area->Width)
1484 max_x = fill_area->X + fill_area->Width;
1486 for (x=min_x; x<max_x; x++)
1488 REAL xf = (REAL)x;
1489 REAL distance;
1491 if (start_color != end_color)
1493 REAL blend_amount, pdy, pdx;
1494 pdy = yf - center_point.Y;
1495 pdx = xf - center_point.X;
1497 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1499 /* Too close to center point, don't try to calculate outer color */
1500 outer_color = start_color;
1502 else
1504 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1505 outer_color = blend_colors(start_color, end_color, blend_amount);
1509 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1510 (end_point.X - start_point.X) * (yf - start_point.Y);
1512 distance = distance / center_distance;
1514 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1515 blend_colors(outer_color, fill->centercolor, distance);
1520 GdipDeletePath(flat_path);
1521 return stat;
1523 default:
1524 return NotImplemented;
1528 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1529 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1530 * should not be called on an hdc that has a path you care about. */
1531 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1532 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1534 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1535 GpMatrix matrix;
1536 HBRUSH brush = NULL;
1537 HPEN pen = NULL;
1538 PointF ptf[4], *custptf = NULL;
1539 POINT pt[4], *custpt = NULL;
1540 BYTE *tp = NULL;
1541 REAL theta, dsmall, dbig, dx, dy = 0.0;
1542 INT i, count;
1543 LOGBRUSH lb;
1544 BOOL customstroke;
1546 if((x1 == x2) && (y1 == y2))
1547 return;
1549 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1551 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1552 if(!customstroke){
1553 brush = CreateSolidBrush(color);
1554 lb.lbStyle = BS_SOLID;
1555 lb.lbColor = color;
1556 lb.lbHatch = 0;
1557 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1558 PS_JOIN_MITER, 1, &lb, 0,
1559 NULL);
1560 oldbrush = SelectObject(graphics->hdc, brush);
1561 oldpen = SelectObject(graphics->hdc, pen);
1564 switch(cap){
1565 case LineCapFlat:
1566 break;
1567 case LineCapSquare:
1568 case LineCapSquareAnchor:
1569 case LineCapDiamondAnchor:
1570 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1571 if(cap == LineCapDiamondAnchor){
1572 dsmall = cos(theta + M_PI_2) * size;
1573 dbig = sin(theta + M_PI_2) * size;
1575 else{
1576 dsmall = cos(theta + M_PI_4) * size;
1577 dbig = sin(theta + M_PI_4) * size;
1580 ptf[0].X = x2 - dsmall;
1581 ptf[1].X = x2 + dbig;
1583 ptf[0].Y = y2 - dbig;
1584 ptf[3].Y = y2 + dsmall;
1586 ptf[1].Y = y2 - dsmall;
1587 ptf[2].Y = y2 + dbig;
1589 ptf[3].X = x2 - dbig;
1590 ptf[2].X = x2 + dsmall;
1592 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1594 round_points(pt, ptf, 3);
1596 Polygon(graphics->hdc, pt, 4);
1598 break;
1599 case LineCapArrowAnchor:
1600 size = size * 4.0 / sqrt(3.0);
1602 dx = cos(M_PI / 6.0 + theta) * size;
1603 dy = sin(M_PI / 6.0 + theta) * size;
1605 ptf[0].X = x2 - dx;
1606 ptf[0].Y = y2 - dy;
1608 dx = cos(- M_PI / 6.0 + theta) * size;
1609 dy = sin(- M_PI / 6.0 + theta) * size;
1611 ptf[1].X = x2 - dx;
1612 ptf[1].Y = y2 - dy;
1614 ptf[2].X = x2;
1615 ptf[2].Y = y2;
1617 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1619 round_points(pt, ptf, 3);
1621 Polygon(graphics->hdc, pt, 3);
1623 break;
1624 case LineCapRoundAnchor:
1625 dx = dy = ANCHOR_WIDTH * size / 2.0;
1627 ptf[0].X = x2 - dx;
1628 ptf[0].Y = y2 - dy;
1629 ptf[1].X = x2 + dx;
1630 ptf[1].Y = y2 + dy;
1632 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1634 round_points(pt, ptf, 3);
1636 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1638 break;
1639 case LineCapTriangle:
1640 size = size / 2.0;
1641 dx = cos(M_PI_2 + theta) * size;
1642 dy = sin(M_PI_2 + theta) * size;
1644 ptf[0].X = x2 - dx;
1645 ptf[0].Y = y2 - dy;
1646 ptf[1].X = x2 + dx;
1647 ptf[1].Y = y2 + dy;
1649 dx = cos(theta) * size;
1650 dy = sin(theta) * size;
1652 ptf[2].X = x2 + dx;
1653 ptf[2].Y = y2 + dy;
1655 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1657 round_points(pt, ptf, 3);
1659 Polygon(graphics->hdc, pt, 3);
1661 break;
1662 case LineCapRound:
1663 dx = dy = size / 2.0;
1665 ptf[0].X = x2 - dx;
1666 ptf[0].Y = y2 - dy;
1667 ptf[1].X = x2 + dx;
1668 ptf[1].Y = y2 + dy;
1670 dx = -cos(M_PI_2 + theta) * size;
1671 dy = -sin(M_PI_2 + theta) * size;
1673 ptf[2].X = x2 - dx;
1674 ptf[2].Y = y2 - dy;
1675 ptf[3].X = x2 + dx;
1676 ptf[3].Y = y2 + dy;
1678 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1680 round_points(pt, ptf, 3);
1682 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1683 pt[2].y, pt[3].x, pt[3].y);
1685 break;
1686 case LineCapCustom:
1687 if(!custom)
1688 break;
1690 count = custom->pathdata.Count;
1691 custptf = heap_alloc_zero(count * sizeof(PointF));
1692 custpt = heap_alloc_zero(count * sizeof(POINT));
1693 tp = heap_alloc_zero(count);
1695 if(!custptf || !custpt || !tp)
1696 goto custend;
1698 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1700 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1701 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1702 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1703 MatrixOrderAppend);
1704 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1705 GdipTransformMatrixPoints(&matrix, custptf, count);
1707 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1709 round_points(pt, ptf, 3);
1711 for(i = 0; i < count; i++)
1712 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1714 if(custom->fill){
1715 BeginPath(graphics->hdc);
1716 PolyDraw(graphics->hdc, custpt, tp, count);
1717 EndPath(graphics->hdc);
1718 StrokeAndFillPath(graphics->hdc);
1720 else
1721 PolyDraw(graphics->hdc, custpt, tp, count);
1723 custend:
1724 heap_free(custptf);
1725 heap_free(custpt);
1726 heap_free(tp);
1727 break;
1728 default:
1729 break;
1732 if(!customstroke){
1733 SelectObject(graphics->hdc, oldbrush);
1734 SelectObject(graphics->hdc, oldpen);
1735 DeleteObject(brush);
1736 DeleteObject(pen);
1740 /* Shortens the line by the given percent by changing x2, y2.
1741 * If percent is > 1.0 then the line will change direction.
1742 * If percent is negative it can lengthen the line. */
1743 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1745 REAL dist, theta, dx, dy;
1747 if((y1 == *y2) && (x1 == *x2))
1748 return;
1750 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1751 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1752 dx = cos(theta) * dist;
1753 dy = sin(theta) * dist;
1755 *x2 = *x2 + dx;
1756 *y2 = *y2 + dy;
1759 /* Shortens the line by the given amount by changing x2, y2.
1760 * If the amount is greater than the distance, the line will become length 0.
1761 * If the amount is negative, it can lengthen the line. */
1762 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1764 REAL dx, dy, percent;
1766 dx = *x2 - x1;
1767 dy = *y2 - y1;
1768 if(dx == 0 && dy == 0)
1769 return;
1771 percent = amt / sqrt(dx * dx + dy * dy);
1772 if(percent >= 1.0){
1773 *x2 = x1;
1774 *y2 = y1;
1775 return;
1778 shorten_line_percent(x1, y1, x2, y2, percent);
1781 /* Conducts a linear search to find the bezier points that will back off
1782 * the endpoint of the curve by a distance of amt. Linear search works
1783 * better than binary in this case because there are multiple solutions,
1784 * and binary searches often find a bad one. I don't think this is what
1785 * Windows does but short of rendering the bezier without GDI's help it's
1786 * the best we can do. If rev then work from the start of the passed points
1787 * instead of the end. */
1788 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1790 GpPointF origpt[4];
1791 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1792 INT i, first = 0, second = 1, third = 2, fourth = 3;
1794 if(rev){
1795 first = 3;
1796 second = 2;
1797 third = 1;
1798 fourth = 0;
1801 origx = pt[fourth].X;
1802 origy = pt[fourth].Y;
1803 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1805 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1806 /* reset bezier points to original values */
1807 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1808 /* Perform magic on bezier points. Order is important here.*/
1809 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1810 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1811 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1812 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1813 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1814 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1816 dx = pt[fourth].X - origx;
1817 dy = pt[fourth].Y - origy;
1819 diff = sqrt(dx * dx + dy * dy);
1820 percent += 0.0005 * amt;
1824 /* Draws a combination of bezier curves and lines between points. */
1825 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1826 GDIPCONST BYTE * types, INT count, BOOL caps)
1828 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1829 BYTE *tp = heap_alloc_zero(count);
1830 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1831 INT i, j;
1832 GpStatus status = GenericError;
1834 if(!count){
1835 status = Ok;
1836 goto end;
1838 if(!pti || !tp || !ptcopy){
1839 status = OutOfMemory;
1840 goto end;
1843 for(i = 1; i < count; i++){
1844 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1845 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1846 || !(types[i + 2] & PathPointTypeBezier)){
1847 ERR("Bad bezier points\n");
1848 goto end;
1850 i += 2;
1854 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1856 /* If we are drawing caps, go through the points and adjust them accordingly,
1857 * and draw the caps. */
1858 if(caps){
1859 switch(types[count - 1] & PathPointTypePathTypeMask){
1860 case PathPointTypeBezier:
1861 if(pen->endcap == LineCapArrowAnchor)
1862 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1863 else if((pen->endcap == LineCapCustom) && pen->customend)
1864 shorten_bezier_amt(&ptcopy[count - 4],
1865 pen->width * pen->customend->inset, FALSE);
1867 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1868 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1869 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1870 pt[count - 1].X, pt[count - 1].Y);
1872 break;
1873 case PathPointTypeLine:
1874 if(pen->endcap == LineCapArrowAnchor)
1875 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1876 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1877 pen->width);
1878 else if((pen->endcap == LineCapCustom) && pen->customend)
1879 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1880 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1881 pen->customend->inset * pen->width);
1883 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1884 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1885 pt[count - 1].Y);
1887 break;
1888 default:
1889 ERR("Bad path last point\n");
1890 goto end;
1893 /* Find start of points */
1894 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1895 == PathPointTypeStart); j++);
1897 switch(types[j] & PathPointTypePathTypeMask){
1898 case PathPointTypeBezier:
1899 if(pen->startcap == LineCapArrowAnchor)
1900 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1901 else if((pen->startcap == LineCapCustom) && pen->customstart)
1902 shorten_bezier_amt(&ptcopy[j - 1],
1903 pen->width * pen->customstart->inset, TRUE);
1905 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1906 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1907 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1908 pt[j - 1].X, pt[j - 1].Y);
1910 break;
1911 case PathPointTypeLine:
1912 if(pen->startcap == LineCapArrowAnchor)
1913 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1914 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1915 pen->width);
1916 else if((pen->startcap == LineCapCustom) && pen->customstart)
1917 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1918 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1919 pen->customstart->inset * pen->width);
1921 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1922 pt[j].X, pt[j].Y, pt[j - 1].X,
1923 pt[j - 1].Y);
1925 break;
1926 default:
1927 ERR("Bad path points\n");
1928 goto end;
1932 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
1934 round_points(pti, ptcopy, count);
1936 for(i = 0; i < count; i++){
1937 tp[i] = convert_path_point_type(types[i]);
1940 PolyDraw(graphics->hdc, pti, tp, count);
1942 status = Ok;
1944 end:
1945 heap_free(pti);
1946 heap_free(ptcopy);
1947 heap_free(tp);
1949 return status;
1952 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1954 GpStatus result;
1956 BeginPath(graphics->hdc);
1957 result = draw_poly(graphics, NULL, path->pathdata.Points,
1958 path->pathdata.Types, path->pathdata.Count, FALSE);
1959 EndPath(graphics->hdc);
1960 return result;
1963 typedef enum GraphicsContainerType {
1964 BEGIN_CONTAINER,
1965 SAVE_GRAPHICS
1966 } GraphicsContainerType;
1968 typedef struct _GraphicsContainerItem {
1969 struct list entry;
1970 GraphicsContainer contid;
1971 GraphicsContainerType type;
1973 SmoothingMode smoothing;
1974 CompositingQuality compqual;
1975 InterpolationMode interpolation;
1976 CompositingMode compmode;
1977 TextRenderingHint texthint;
1978 REAL scale;
1979 GpUnit unit;
1980 PixelOffsetMode pixeloffset;
1981 UINT textcontrast;
1982 GpMatrix worldtrans;
1983 GpRegion* clip;
1984 INT origin_x, origin_y;
1985 } GraphicsContainerItem;
1987 static GpStatus init_container(GraphicsContainerItem** container,
1988 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
1989 GpStatus sts;
1991 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
1992 if(!(*container))
1993 return OutOfMemory;
1995 (*container)->contid = graphics->contid + 1;
1996 (*container)->type = type;
1998 (*container)->smoothing = graphics->smoothing;
1999 (*container)->compqual = graphics->compqual;
2000 (*container)->interpolation = graphics->interpolation;
2001 (*container)->compmode = graphics->compmode;
2002 (*container)->texthint = graphics->texthint;
2003 (*container)->scale = graphics->scale;
2004 (*container)->unit = graphics->unit;
2005 (*container)->textcontrast = graphics->textcontrast;
2006 (*container)->pixeloffset = graphics->pixeloffset;
2007 (*container)->origin_x = graphics->origin_x;
2008 (*container)->origin_y = graphics->origin_y;
2009 (*container)->worldtrans = graphics->worldtrans;
2011 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2012 if(sts != Ok){
2013 heap_free(*container);
2014 *container = NULL;
2015 return sts;
2018 return Ok;
2021 static void delete_container(GraphicsContainerItem* container)
2023 GdipDeleteRegion(container->clip);
2024 heap_free(container);
2027 static GpStatus restore_container(GpGraphics* graphics,
2028 GDIPCONST GraphicsContainerItem* container){
2029 GpStatus sts;
2030 GpRegion *newClip;
2032 sts = GdipCloneRegion(container->clip, &newClip);
2033 if(sts != Ok) return sts;
2035 graphics->worldtrans = container->worldtrans;
2037 GdipDeleteRegion(graphics->clip);
2038 graphics->clip = newClip;
2040 graphics->contid = container->contid - 1;
2042 graphics->smoothing = container->smoothing;
2043 graphics->compqual = container->compqual;
2044 graphics->interpolation = container->interpolation;
2045 graphics->compmode = container->compmode;
2046 graphics->texthint = container->texthint;
2047 graphics->scale = container->scale;
2048 graphics->unit = container->unit;
2049 graphics->textcontrast = container->textcontrast;
2050 graphics->pixeloffset = container->pixeloffset;
2051 graphics->origin_x = container->origin_x;
2052 graphics->origin_y = container->origin_y;
2054 return Ok;
2057 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2059 RECT wnd_rect;
2060 GpStatus stat=Ok;
2061 GpUnit unit;
2063 if(graphics->hwnd) {
2064 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2065 return GenericError;
2067 rect->X = wnd_rect.left;
2068 rect->Y = wnd_rect.top;
2069 rect->Width = wnd_rect.right - wnd_rect.left;
2070 rect->Height = wnd_rect.bottom - wnd_rect.top;
2071 }else if (graphics->image){
2072 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2073 if (stat == Ok && unit != UnitPixel)
2074 FIXME("need to convert from unit %i\n", unit);
2075 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2076 HBITMAP hbmp;
2077 BITMAP bmp;
2079 rect->X = 0;
2080 rect->Y = 0;
2082 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2083 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2085 rect->Width = bmp.bmWidth;
2086 rect->Height = bmp.bmHeight;
2088 else
2090 /* FIXME: ??? */
2091 rect->Width = 1;
2092 rect->Height = 1;
2094 }else{
2095 rect->X = 0;
2096 rect->Y = 0;
2097 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2098 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2101 return stat;
2104 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2106 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2108 if (stat == Ok && graphics->hdc)
2110 GpPointF points[4], min_point, max_point;
2111 int i;
2113 points[0].X = points[2].X = rect->X;
2114 points[0].Y = points[1].Y = rect->Y;
2115 points[1].X = points[3].X = rect->X + rect->Width;
2116 points[2].Y = points[3].Y = rect->Y + rect->Height;
2118 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2120 min_point = max_point = points[0];
2122 for (i=1; i<4; i++)
2124 if (points[i].X < min_point.X) min_point.X = points[i].X;
2125 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2126 if (points[i].X > max_point.X) max_point.X = points[i].X;
2127 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2130 rect->X = min_point.X;
2131 rect->Y = min_point.Y;
2132 rect->Width = max_point.X - min_point.X;
2133 rect->Height = max_point.Y - min_point.Y;
2136 return stat;
2139 /* on success, rgn will contain the region of the graphics object which
2140 * is visible after clipping has been applied */
2141 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2143 GpStatus stat;
2144 GpRectF rectf;
2145 GpRegion* tmp;
2147 /* Ignore graphics image bounds for metafiles */
2148 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2149 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2151 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2152 return stat;
2154 if((stat = GdipCreateRegion(&tmp)) != Ok)
2155 return stat;
2157 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2158 goto end;
2160 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2161 goto end;
2163 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2165 end:
2166 GdipDeleteRegion(tmp);
2167 return stat;
2170 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2172 REAL height;
2174 if (font->unit == UnitPixel)
2176 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2178 else
2180 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2181 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2182 else
2183 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2186 lf->lfHeight = -(height + 0.5);
2187 lf->lfWidth = 0;
2188 lf->lfEscapement = 0;
2189 lf->lfOrientation = 0;
2190 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2191 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2192 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2193 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2194 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2195 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2196 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2197 lf->lfQuality = DEFAULT_QUALITY;
2198 lf->lfPitchAndFamily = 0;
2199 strcpyW(lf->lfFaceName, font->family->FamilyName);
2202 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2203 GDIPCONST GpStringFormat *format, HFONT *hfont,
2204 GDIPCONST GpMatrix *matrix)
2206 HDC hdc = CreateCompatibleDC(0);
2207 GpPointF pt[3];
2208 REAL angle, rel_width, rel_height, font_height;
2209 LOGFONTW lfw;
2210 HFONT unscaled_font;
2211 TEXTMETRICW textmet;
2213 if (font->unit == UnitPixel || font->unit == UnitWorld)
2214 font_height = font->emSize;
2215 else
2217 REAL unit_scale, res;
2219 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2220 unit_scale = units_scale(font->unit, graphics->unit, res);
2222 font_height = font->emSize * unit_scale;
2225 pt[0].X = 0.0;
2226 pt[0].Y = 0.0;
2227 pt[1].X = 1.0;
2228 pt[1].Y = 0.0;
2229 pt[2].X = 0.0;
2230 pt[2].Y = 1.0;
2231 if (matrix)
2233 GpMatrix xform = *matrix;
2234 GdipTransformMatrixPoints(&xform, pt, 3);
2237 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2238 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2239 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2240 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2241 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2242 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2244 get_log_fontW(font, graphics, &lfw);
2245 lfw.lfHeight = -gdip_round(font_height * rel_height);
2246 unscaled_font = CreateFontIndirectW(&lfw);
2248 SelectObject(hdc, unscaled_font);
2249 GetTextMetricsW(hdc, &textmet);
2251 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2252 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2254 *hfont = CreateFontIndirectW(&lfw);
2256 DeleteDC(hdc);
2257 DeleteObject(unscaled_font);
2260 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2262 TRACE("(%p, %p)\n", hdc, graphics);
2264 return GdipCreateFromHDC2(hdc, NULL, graphics);
2267 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2269 XFORM xform;
2271 if (graphics->hdc == NULL)
2273 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2274 return;
2277 GetTransform(graphics->hdc, 0x204, &xform);
2278 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2281 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2283 GpStatus retval;
2284 HBITMAP hbitmap;
2285 DIBSECTION dib;
2287 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2289 if(hDevice != NULL)
2290 FIXME("Don't know how to handle parameter hDevice\n");
2292 if(hdc == NULL)
2293 return OutOfMemory;
2295 if(graphics == NULL)
2296 return InvalidParameter;
2298 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2299 if(!*graphics) return OutOfMemory;
2301 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2303 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2304 heap_free(*graphics);
2305 return retval;
2308 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2309 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2310 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2312 (*graphics)->alpha_hdc = 1;
2315 (*graphics)->hdc = hdc;
2316 (*graphics)->hwnd = WindowFromDC(hdc);
2317 (*graphics)->owndc = FALSE;
2318 (*graphics)->smoothing = SmoothingModeDefault;
2319 (*graphics)->compqual = CompositingQualityDefault;
2320 (*graphics)->interpolation = InterpolationModeBilinear;
2321 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2322 (*graphics)->compmode = CompositingModeSourceOver;
2323 (*graphics)->unit = UnitDisplay;
2324 (*graphics)->scale = 1.0;
2325 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2326 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2327 (*graphics)->busy = FALSE;
2328 (*graphics)->textcontrast = 4;
2329 list_init(&(*graphics)->containers);
2330 (*graphics)->contid = 0;
2331 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2333 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2334 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2336 DeleteObject((*graphics)->gdi_clip);
2337 (*graphics)->gdi_clip = NULL;
2340 TRACE("<-- %p\n", *graphics);
2342 return Ok;
2345 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2347 GpStatus retval;
2349 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2350 if(!*graphics) return OutOfMemory;
2352 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2353 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2355 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2356 heap_free(*graphics);
2357 return retval;
2360 (*graphics)->hdc = NULL;
2361 (*graphics)->hwnd = NULL;
2362 (*graphics)->owndc = FALSE;
2363 (*graphics)->image = image;
2364 /* We have to store the image type here because the image may be freed
2365 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2366 (*graphics)->image_type = image->type;
2367 (*graphics)->smoothing = SmoothingModeDefault;
2368 (*graphics)->compqual = CompositingQualityDefault;
2369 (*graphics)->interpolation = InterpolationModeBilinear;
2370 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2371 (*graphics)->compmode = CompositingModeSourceOver;
2372 (*graphics)->unit = UnitDisplay;
2373 (*graphics)->scale = 1.0;
2374 (*graphics)->xres = image->xres;
2375 (*graphics)->yres = image->yres;
2376 (*graphics)->busy = FALSE;
2377 (*graphics)->textcontrast = 4;
2378 list_init(&(*graphics)->containers);
2379 (*graphics)->contid = 0;
2381 TRACE("<-- %p\n", *graphics);
2383 return Ok;
2386 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2388 GpStatus ret;
2389 HDC hdc;
2391 TRACE("(%p, %p)\n", hwnd, graphics);
2393 hdc = GetDC(hwnd);
2395 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2397 ReleaseDC(hwnd, hdc);
2398 return ret;
2401 (*graphics)->hwnd = hwnd;
2402 (*graphics)->owndc = TRUE;
2404 return Ok;
2407 /* FIXME: no icm handling */
2408 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2410 TRACE("(%p, %p)\n", hwnd, graphics);
2412 return GdipCreateFromHWND(hwnd, graphics);
2415 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2416 UINT access, IStream **stream)
2418 DWORD dwMode;
2419 HRESULT ret;
2421 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2423 if(!stream || !filename)
2424 return InvalidParameter;
2426 if(access & GENERIC_WRITE)
2427 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2428 else if(access & GENERIC_READ)
2429 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2430 else
2431 return InvalidParameter;
2433 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2435 return hresult_to_status(ret);
2438 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2440 GraphicsContainerItem *cont, *next;
2441 GpStatus stat;
2442 TRACE("(%p)\n", graphics);
2444 if(!graphics) return InvalidParameter;
2445 if(graphics->busy) return ObjectBusy;
2447 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2449 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2450 if (stat != Ok)
2451 return stat;
2454 if(graphics->owndc)
2455 ReleaseDC(graphics->hwnd, graphics->hdc);
2457 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2458 list_remove(&cont->entry);
2459 delete_container(cont);
2462 GdipDeleteRegion(graphics->clip);
2464 DeleteObject(graphics->gdi_clip);
2466 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2467 * do otherwise, but we can't have that in the test suite because it means
2468 * accessing freed memory. */
2469 graphics->busy = TRUE;
2471 heap_free(graphics);
2473 return Ok;
2476 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2477 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2479 GpStatus status;
2480 GpPath *path;
2482 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2483 width, height, startAngle, sweepAngle);
2485 if(!graphics || !pen || width <= 0 || height <= 0)
2486 return InvalidParameter;
2488 if(graphics->busy)
2489 return ObjectBusy;
2491 status = GdipCreatePath(FillModeAlternate, &path);
2492 if (status != Ok) return status;
2494 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2495 if (status == Ok)
2496 status = GdipDrawPath(graphics, pen, path);
2498 GdipDeletePath(path);
2499 return status;
2502 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2503 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2505 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2506 width, height, startAngle, sweepAngle);
2508 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2511 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2512 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2514 GpPointF pt[4];
2516 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2517 x2, y2, x3, y3, x4, y4);
2519 if(!graphics || !pen)
2520 return InvalidParameter;
2522 if(graphics->busy)
2523 return ObjectBusy;
2525 pt[0].X = x1;
2526 pt[0].Y = y1;
2527 pt[1].X = x2;
2528 pt[1].Y = y2;
2529 pt[2].X = x3;
2530 pt[2].Y = y3;
2531 pt[3].X = x4;
2532 pt[3].Y = y4;
2533 return GdipDrawBeziers(graphics, pen, pt, 4);
2536 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2537 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2539 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2540 x2, y2, x3, y3, x4, y4);
2542 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2545 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2546 GDIPCONST GpPointF *points, INT count)
2548 GpStatus status;
2549 GpPath *path;
2551 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2553 if(!graphics || !pen || !points || (count <= 0))
2554 return InvalidParameter;
2556 if(graphics->busy)
2557 return ObjectBusy;
2559 status = GdipCreatePath(FillModeAlternate, &path);
2560 if (status != Ok) return status;
2562 status = GdipAddPathBeziers(path, points, count);
2563 if (status == Ok)
2564 status = GdipDrawPath(graphics, pen, path);
2566 GdipDeletePath(path);
2567 return status;
2570 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2571 GDIPCONST GpPoint *points, INT count)
2573 GpPointF *pts;
2574 GpStatus ret;
2575 INT i;
2577 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2579 if(!graphics || !pen || !points || (count <= 0))
2580 return InvalidParameter;
2582 if(graphics->busy)
2583 return ObjectBusy;
2585 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2586 if(!pts)
2587 return OutOfMemory;
2589 for(i = 0; i < count; i++){
2590 pts[i].X = (REAL)points[i].X;
2591 pts[i].Y = (REAL)points[i].Y;
2594 ret = GdipDrawBeziers(graphics,pen,pts,count);
2596 heap_free(pts);
2598 return ret;
2601 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2602 GDIPCONST GpPointF *points, INT count)
2604 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2606 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2609 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2610 GDIPCONST GpPoint *points, INT count)
2612 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2614 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2617 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2618 GDIPCONST GpPointF *points, INT count, REAL tension)
2620 GpPath *path;
2621 GpStatus status;
2623 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2625 if(!graphics || !pen || !points || count <= 0)
2626 return InvalidParameter;
2628 if(graphics->busy)
2629 return ObjectBusy;
2631 status = GdipCreatePath(FillModeAlternate, &path);
2632 if (status != Ok) return status;
2634 status = GdipAddPathClosedCurve2(path, points, count, tension);
2635 if (status == Ok)
2636 status = GdipDrawPath(graphics, pen, path);
2638 GdipDeletePath(path);
2640 return status;
2643 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2644 GDIPCONST GpPoint *points, INT count, REAL tension)
2646 GpPointF *ptf;
2647 GpStatus stat;
2648 INT i;
2650 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2652 if(!points || count <= 0)
2653 return InvalidParameter;
2655 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2656 if(!ptf)
2657 return OutOfMemory;
2659 for(i = 0; i < count; i++){
2660 ptf[i].X = (REAL)points[i].X;
2661 ptf[i].Y = (REAL)points[i].Y;
2664 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2666 heap_free(ptf);
2668 return stat;
2671 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2672 GDIPCONST GpPointF *points, INT count)
2674 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2676 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2679 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2680 GDIPCONST GpPoint *points, INT count)
2682 GpPointF *pointsF;
2683 GpStatus ret;
2684 INT i;
2686 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2688 if(!points)
2689 return InvalidParameter;
2691 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2692 if(!pointsF)
2693 return OutOfMemory;
2695 for(i = 0; i < count; i++){
2696 pointsF[i].X = (REAL)points[i].X;
2697 pointsF[i].Y = (REAL)points[i].Y;
2700 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2701 heap_free(pointsF);
2703 return ret;
2706 /* Approximates cardinal spline with Bezier curves. */
2707 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2708 GDIPCONST GpPointF *points, INT count, REAL tension)
2710 GpPath *path;
2711 GpStatus status;
2713 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2715 if(!graphics || !pen)
2716 return InvalidParameter;
2718 if(graphics->busy)
2719 return ObjectBusy;
2721 if(count < 2)
2722 return InvalidParameter;
2724 status = GdipCreatePath(FillModeAlternate, &path);
2725 if (status != Ok) return status;
2727 status = GdipAddPathCurve2(path, points, count, tension);
2728 if (status == Ok)
2729 status = GdipDrawPath(graphics, pen, path);
2731 GdipDeletePath(path);
2732 return status;
2735 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2736 GDIPCONST GpPoint *points, INT count, REAL tension)
2738 GpPointF *pointsF;
2739 GpStatus ret;
2740 INT i;
2742 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2744 if(!points)
2745 return InvalidParameter;
2747 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2748 if(!pointsF)
2749 return OutOfMemory;
2751 for(i = 0; i < count; i++){
2752 pointsF[i].X = (REAL)points[i].X;
2753 pointsF[i].Y = (REAL)points[i].Y;
2756 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2757 heap_free(pointsF);
2759 return ret;
2762 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2763 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2764 REAL tension)
2766 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2768 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2769 return InvalidParameter;
2772 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2775 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2776 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2777 REAL tension)
2779 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2781 if(count < 0){
2782 return OutOfMemory;
2785 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2786 return InvalidParameter;
2789 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2792 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2793 REAL y, REAL width, REAL height)
2795 GpPath *path;
2796 GpStatus status;
2798 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2800 if(!graphics || !pen)
2801 return InvalidParameter;
2803 if(graphics->busy)
2804 return ObjectBusy;
2806 status = GdipCreatePath(FillModeAlternate, &path);
2807 if (status != Ok) return status;
2809 status = GdipAddPathEllipse(path, x, y, width, height);
2810 if (status == Ok)
2811 status = GdipDrawPath(graphics, pen, path);
2813 GdipDeletePath(path);
2814 return status;
2817 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2818 INT y, INT width, INT height)
2820 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2822 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2826 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2828 UINT width, height;
2830 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2832 if(!graphics || !image)
2833 return InvalidParameter;
2835 GdipGetImageWidth(image, &width);
2836 GdipGetImageHeight(image, &height);
2838 return GdipDrawImagePointRect(graphics, image, x, y,
2839 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2842 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2843 INT y)
2845 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2847 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2850 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2851 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2852 GpUnit srcUnit)
2854 GpPointF points[3];
2855 REAL scale_x, scale_y, width, height;
2857 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2859 if (!graphics || !image) return InvalidParameter;
2861 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2862 scale_x *= graphics->xres / image->xres;
2863 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2864 scale_y *= graphics->yres / image->yres;
2865 width = srcwidth * scale_x;
2866 height = srcheight * scale_y;
2868 points[0].X = points[2].X = x;
2869 points[0].Y = points[1].Y = y;
2870 points[1].X = x + width;
2871 points[2].Y = y + height;
2873 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2874 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2877 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2878 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2879 GpUnit srcUnit)
2881 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2884 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2885 GDIPCONST GpPointF *dstpoints, INT count)
2887 UINT width, height;
2889 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2891 if(!image)
2892 return InvalidParameter;
2894 GdipGetImageWidth(image, &width);
2895 GdipGetImageHeight(image, &height);
2897 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2898 width, height, UnitPixel, NULL, NULL, NULL);
2901 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2902 GDIPCONST GpPoint *dstpoints, INT count)
2904 GpPointF ptf[3];
2906 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2908 if (count != 3 || !dstpoints)
2909 return InvalidParameter;
2911 ptf[0].X = (REAL)dstpoints[0].X;
2912 ptf[0].Y = (REAL)dstpoints[0].Y;
2913 ptf[1].X = (REAL)dstpoints[1].X;
2914 ptf[1].Y = (REAL)dstpoints[1].Y;
2915 ptf[2].X = (REAL)dstpoints[2].X;
2916 ptf[2].Y = (REAL)dstpoints[2].Y;
2918 return GdipDrawImagePoints(graphics, image, ptf, count);
2921 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2922 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2924 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2925 return TRUE;
2928 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2929 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2930 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2931 DrawImageAbort callback, VOID * callbackData)
2933 GpPointF ptf[4];
2934 POINT pti[4];
2935 GpStatus stat;
2937 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2938 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2939 callbackData);
2941 if (count > 3)
2942 return NotImplemented;
2944 if(!graphics || !image || !points || count != 3)
2945 return InvalidParameter;
2947 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2948 debugstr_pointf(&points[2]));
2950 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2952 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
2953 image, points, count, srcx, srcy, srcwidth, srcheight,
2954 srcUnit, imageAttributes, callback, callbackData);
2957 memcpy(ptf, points, 3 * sizeof(GpPointF));
2959 /* Ensure source width/height is positive */
2960 if (srcwidth < 0)
2962 GpPointF tmp = ptf[1];
2963 srcx = srcx + srcwidth;
2964 srcwidth = -srcwidth;
2965 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2966 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2967 ptf[1] = ptf[0];
2968 ptf[0] = tmp;
2971 if (srcheight < 0)
2973 GpPointF tmp = ptf[2];
2974 srcy = srcy + srcheight;
2975 srcheight = -srcheight;
2976 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2977 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2978 ptf[2] = ptf[0];
2979 ptf[0] = tmp;
2982 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2983 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2984 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2985 return Ok;
2986 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
2987 round_points(pti, ptf, 4);
2989 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2990 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2992 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2993 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2994 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2995 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2996 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2998 if (image->type == ImageTypeBitmap)
3000 GpBitmap* bitmap = (GpBitmap*)image;
3001 BOOL do_resampling = FALSE;
3002 BOOL use_software = FALSE;
3004 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3005 graphics->xres, graphics->yres,
3006 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3007 graphics->scale, image->xres, image->yres, bitmap->format,
3008 imageAttributes ? imageAttributes->outside_color : 0);
3010 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3011 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3012 srcx < 0 || srcy < 0 ||
3013 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3014 do_resampling = TRUE;
3016 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3017 (graphics->image && graphics->image->type == ImageTypeBitmap))
3018 use_software = TRUE;
3020 if (use_software)
3022 RECT dst_area;
3023 GpRectF graphics_bounds;
3024 GpRect src_area;
3025 int i, x, y, src_stride, dst_stride;
3026 GpMatrix dst_to_src;
3027 REAL m11, m12, m21, m22, mdx, mdy;
3028 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3029 BitmapData lockeddata;
3030 InterpolationMode interpolation = graphics->interpolation;
3031 PixelOffsetMode offset_mode = graphics->pixeloffset;
3032 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3033 REAL x_dx, x_dy, y_dx, y_dy;
3034 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3036 if (!imageAttributes)
3037 imageAttributes = &defaultImageAttributes;
3039 dst_area.left = dst_area.right = pti[0].x;
3040 dst_area.top = dst_area.bottom = pti[0].y;
3041 for (i=1; i<4; i++)
3043 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3044 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3045 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3046 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3049 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3050 if (stat != Ok) return stat;
3052 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3053 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3054 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3055 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3057 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3059 if (IsRectEmpty(&dst_area)) return Ok;
3061 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3062 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3063 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3064 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3065 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3066 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3068 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3070 stat = GdipInvertMatrix(&dst_to_src);
3071 if (stat != Ok) return stat;
3073 if (do_resampling)
3075 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3076 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3078 else
3080 /* Make sure src_area is equal in size to dst_area. */
3081 src_area.X = srcx + dst_area.left - pti[0].x;
3082 src_area.Y = srcy + dst_area.top - pti[0].y;
3083 src_area.Width = dst_area.right - dst_area.left;
3084 src_area.Height = dst_area.bottom - dst_area.top;
3087 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3089 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3090 if (!src_data)
3091 return OutOfMemory;
3092 src_stride = sizeof(ARGB) * src_area.Width;
3094 /* Read the bits we need from the source bitmap into a compatible buffer. */
3095 lockeddata.Width = src_area.Width;
3096 lockeddata.Height = src_area.Height;
3097 lockeddata.Stride = src_stride;
3098 lockeddata.Scan0 = src_data;
3099 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3100 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3101 else
3102 lockeddata.PixelFormat = PixelFormat32bppARGB;
3104 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3105 lockeddata.PixelFormat, &lockeddata);
3107 if (stat == Ok)
3108 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3110 if (stat != Ok)
3112 heap_free(src_data);
3113 return stat;
3116 apply_image_attributes(imageAttributes, src_data,
3117 src_area.Width, src_area.Height,
3118 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3120 if (do_resampling)
3122 /* Transform the bits as needed to the destination. */
3123 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3124 if (!dst_data)
3126 heap_free(src_data);
3127 return OutOfMemory;
3130 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3132 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3134 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3135 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3136 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3137 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3139 for (x=dst_area.left; x<dst_area.right; x++)
3141 for (y=dst_area.top; y<dst_area.bottom; y++)
3143 GpPointF src_pointf;
3144 ARGB *dst_color;
3146 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3147 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3149 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3151 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3152 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3153 imageAttributes, interpolation, offset_mode);
3154 else
3155 *dst_color = 0;
3159 else
3161 dst_data = src_data;
3162 dst_stride = src_stride;
3165 gdi_transform_acquire(graphics);
3167 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3168 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3169 lockeddata.PixelFormat);
3171 gdi_transform_release(graphics);
3173 heap_free(src_data);
3175 heap_free(dst_dyn_data);
3177 return stat;
3179 else
3181 HDC hdc;
3182 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3183 HBITMAP hbitmap, old_hbm=NULL;
3184 HRGN hrgn;
3185 INT save_state;
3187 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3188 bitmap->format == PixelFormat24bppRGB ||
3189 bitmap->format == PixelFormat32bppRGB ||
3190 bitmap->format == PixelFormat32bppPARGB))
3192 BITMAPINFOHEADER bih;
3193 BYTE *temp_bits;
3194 PixelFormat dst_format;
3196 /* we can't draw a bitmap of this format directly */
3197 hdc = CreateCompatibleDC(0);
3198 temp_hdc = TRUE;
3199 temp_bitmap = TRUE;
3201 bih.biSize = sizeof(BITMAPINFOHEADER);
3202 bih.biWidth = bitmap->width;
3203 bih.biHeight = -bitmap->height;
3204 bih.biPlanes = 1;
3205 bih.biBitCount = 32;
3206 bih.biCompression = BI_RGB;
3207 bih.biSizeImage = 0;
3208 bih.biXPelsPerMeter = 0;
3209 bih.biYPelsPerMeter = 0;
3210 bih.biClrUsed = 0;
3211 bih.biClrImportant = 0;
3213 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3214 (void**)&temp_bits, NULL, 0);
3216 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3217 dst_format = PixelFormat32bppPARGB;
3218 else
3219 dst_format = PixelFormat32bppRGB;
3221 convert_pixels(bitmap->width, bitmap->height,
3222 bitmap->width*4, temp_bits, dst_format,
3223 bitmap->stride, bitmap->bits, bitmap->format,
3224 bitmap->image.palette);
3226 else
3228 if (bitmap->hbitmap)
3229 hbitmap = bitmap->hbitmap;
3230 else
3232 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3233 temp_bitmap = TRUE;
3236 hdc = bitmap->hdc;
3237 temp_hdc = (hdc == 0);
3240 if (temp_hdc)
3242 if (!hdc) hdc = CreateCompatibleDC(0);
3243 old_hbm = SelectObject(hdc, hbitmap);
3246 save_state = SaveDC(graphics->hdc);
3248 stat = get_clip_hrgn(graphics, &hrgn);
3250 if (stat == Ok)
3252 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3253 DeleteObject(hrgn);
3256 gdi_transform_acquire(graphics);
3258 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3260 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3261 hdc, srcx, srcy, srcwidth, srcheight);
3263 else
3265 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3266 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3269 gdi_transform_release(graphics);
3271 RestoreDC(graphics->hdc, save_state);
3273 if (temp_hdc)
3275 SelectObject(hdc, old_hbm);
3276 DeleteDC(hdc);
3279 if (temp_bitmap)
3280 DeleteObject(hbitmap);
3283 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3285 GpRectF rc;
3287 rc.X = srcx;
3288 rc.Y = srcy;
3289 rc.Width = srcwidth;
3290 rc.Height = srcheight;
3292 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3293 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3295 else
3297 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3298 return InvalidParameter;
3301 return Ok;
3304 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3305 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3306 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3307 DrawImageAbort callback, VOID * callbackData)
3309 GpPointF pointsF[3];
3310 INT i;
3312 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3313 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3314 callbackData);
3316 if(!points || count!=3)
3317 return InvalidParameter;
3319 for(i = 0; i < count; i++){
3320 pointsF[i].X = (REAL)points[i].X;
3321 pointsF[i].Y = (REAL)points[i].Y;
3324 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3325 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3326 callback, callbackData);
3329 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3330 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3331 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3332 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3333 VOID * callbackData)
3335 GpPointF points[3];
3337 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3338 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3339 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3341 points[0].X = dstx;
3342 points[0].Y = dsty;
3343 points[1].X = dstx + dstwidth;
3344 points[1].Y = dsty;
3345 points[2].X = dstx;
3346 points[2].Y = dsty + dstheight;
3348 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3349 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3352 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3353 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3354 INT srcwidth, INT srcheight, GpUnit srcUnit,
3355 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3356 VOID * callbackData)
3358 GpPointF points[3];
3360 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3361 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3362 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3364 points[0].X = dstx;
3365 points[0].Y = dsty;
3366 points[1].X = dstx + dstwidth;
3367 points[1].Y = dsty;
3368 points[2].X = dstx;
3369 points[2].Y = dsty + dstheight;
3371 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3372 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3375 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3376 REAL x, REAL y, REAL width, REAL height)
3378 RectF bounds;
3379 GpUnit unit;
3380 GpStatus ret;
3382 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3384 if(!graphics || !image)
3385 return InvalidParameter;
3387 ret = GdipGetImageBounds(image, &bounds, &unit);
3388 if(ret != Ok)
3389 return ret;
3391 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3392 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3393 unit, NULL, NULL, NULL);
3396 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3397 INT x, INT y, INT width, INT height)
3399 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3401 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3404 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3405 REAL y1, REAL x2, REAL y2)
3407 GpPointF pt[2];
3409 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3411 if (!pen)
3412 return InvalidParameter;
3414 if (pen->unit == UnitPixel && pen->width <= 0.0)
3415 return Ok;
3417 pt[0].X = x1;
3418 pt[0].Y = y1;
3419 pt[1].X = x2;
3420 pt[1].Y = y2;
3421 return GdipDrawLines(graphics, pen, pt, 2);
3424 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3425 INT y1, INT x2, INT y2)
3427 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3429 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3432 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3433 GpPointF *points, INT count)
3435 GpStatus status;
3436 GpPath *path;
3438 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3440 if(!pen || !graphics || (count < 2))
3441 return InvalidParameter;
3443 if(graphics->busy)
3444 return ObjectBusy;
3446 status = GdipCreatePath(FillModeAlternate, &path);
3447 if (status != Ok) return status;
3449 status = GdipAddPathLine2(path, points, count);
3450 if (status == Ok)
3451 status = GdipDrawPath(graphics, pen, path);
3453 GdipDeletePath(path);
3454 return status;
3457 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3458 GpPoint *points, INT count)
3460 GpStatus retval;
3461 GpPointF *ptf;
3462 int i;
3464 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3466 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3467 if(!ptf) return OutOfMemory;
3469 for(i = 0; i < count; i ++){
3470 ptf[i].X = (REAL) points[i].X;
3471 ptf[i].Y = (REAL) points[i].Y;
3474 retval = GdipDrawLines(graphics, pen, ptf, count);
3476 heap_free(ptf);
3477 return retval;
3480 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3482 INT save_state;
3483 GpStatus retval;
3484 HRGN hrgn=NULL;
3486 save_state = prepare_dc(graphics, pen);
3488 retval = get_clip_hrgn(graphics, &hrgn);
3490 if (retval != Ok)
3491 goto end;
3493 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3495 gdi_transform_acquire(graphics);
3497 retval = draw_poly(graphics, pen, path->pathdata.Points,
3498 path->pathdata.Types, path->pathdata.Count, TRUE);
3500 gdi_transform_release(graphics);
3502 end:
3503 restore_dc(graphics, save_state);
3504 DeleteObject(hrgn);
3506 return retval;
3509 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3511 GpStatus stat;
3512 GpPath* flat_path;
3513 GpMatrix* transform;
3514 GpRectF gp_bound_rect;
3515 GpRect gp_output_area;
3516 RECT output_area;
3517 INT output_height, output_width;
3518 DWORD *output_bits, *brush_bits=NULL;
3519 int i;
3520 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3521 const BYTE *dash_pattern;
3522 INT dash_pattern_size;
3523 BYTE *dyn_dash_pattern = NULL;
3525 stat = GdipClonePath(path, &flat_path);
3527 if (stat != Ok)
3528 return stat;
3530 stat = GdipCreateMatrix(&transform);
3532 if (stat == Ok)
3534 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3535 CoordinateSpaceWorld, transform);
3537 if (stat == Ok)
3538 stat = GdipFlattenPath(flat_path, transform, 1.0);
3540 GdipDeleteMatrix(transform);
3543 /* estimate the output size in pixels, can be larger than necessary */
3544 if (stat == Ok)
3546 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3547 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3548 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3549 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3551 for (i=1; i<flat_path->pathdata.Count; i++)
3553 REAL x, y;
3554 x = flat_path->pathdata.Points[i].X;
3555 y = flat_path->pathdata.Points[i].Y;
3557 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3558 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3559 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3560 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3563 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3566 if (stat == Ok)
3568 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3569 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3570 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3571 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3573 output_width = output_area.right - output_area.left + 1;
3574 output_height = output_area.bottom - output_area.top + 1;
3576 if (output_width <= 0 || output_height <= 0)
3578 GdipDeletePath(flat_path);
3579 return Ok;
3582 gp_output_area.X = output_area.left;
3583 gp_output_area.Y = output_area.top;
3584 gp_output_area.Width = output_width;
3585 gp_output_area.Height = output_height;
3587 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3588 if (!output_bits)
3589 stat = OutOfMemory;
3592 if (stat == Ok)
3594 if (pen->brush->bt != BrushTypeSolidColor)
3596 /* allocate and draw brush output */
3597 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3599 if (brush_bits)
3601 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3602 &gp_output_area, output_width);
3604 else
3605 stat = OutOfMemory;
3608 if (stat == Ok)
3610 /* convert dash pattern to bool array */
3611 switch (pen->dash)
3613 case DashStyleCustom:
3615 dash_pattern_size = 0;
3617 for (i=0; i < pen->numdashes; i++)
3618 dash_pattern_size += gdip_round(pen->dashes[i]);
3620 if (dash_pattern_size != 0)
3622 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3624 if (dyn_dash_pattern)
3626 int j=0;
3627 for (i=0; i < pen->numdashes; i++)
3629 int k;
3630 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3631 dyn_dash_pattern[j++] = (i&1)^1;
3634 else
3635 stat = OutOfMemory;
3637 break;
3639 /* else fall through */
3641 case DashStyleSolid:
3642 default:
3643 dash_pattern = static_dash_pattern;
3644 dash_pattern_size = 1;
3645 break;
3646 case DashStyleDash:
3647 dash_pattern = static_dash_pattern;
3648 dash_pattern_size = 4;
3649 break;
3650 case DashStyleDot:
3651 dash_pattern = &static_dash_pattern[4];
3652 dash_pattern_size = 2;
3653 break;
3654 case DashStyleDashDot:
3655 dash_pattern = static_dash_pattern;
3656 dash_pattern_size = 6;
3657 break;
3658 case DashStyleDashDotDot:
3659 dash_pattern = static_dash_pattern;
3660 dash_pattern_size = 8;
3661 break;
3665 if (stat == Ok)
3667 /* trace path */
3668 GpPointF subpath_start = flat_path->pathdata.Points[0];
3669 INT prev_x = INT_MAX, prev_y = INT_MAX;
3670 int dash_pos = dash_pattern_size - 1;
3672 for (i=0; i < flat_path->pathdata.Count; i++)
3674 BYTE type, type2;
3675 GpPointF start_point, end_point;
3676 GpPoint start_pointi, end_pointi;
3678 type = flat_path->pathdata.Types[i];
3679 if (i+1 < flat_path->pathdata.Count)
3680 type2 = flat_path->pathdata.Types[i+1];
3681 else
3682 type2 = PathPointTypeStart;
3684 start_point = flat_path->pathdata.Points[i];
3686 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3687 subpath_start = start_point;
3689 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3690 end_point = subpath_start;
3691 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3692 continue;
3693 else
3694 end_point = flat_path->pathdata.Points[i+1];
3696 start_pointi.X = floorf(start_point.X);
3697 start_pointi.Y = floorf(start_point.Y);
3698 end_pointi.X = floorf(end_point.X);
3699 end_pointi.Y = floorf(end_point.Y);
3701 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3702 continue;
3704 /* draw line segment */
3705 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3707 INT x, y, start_y, end_y, step;
3709 if (start_pointi.Y < end_pointi.Y)
3711 step = 1;
3712 start_y = ceilf(start_point.Y) - output_area.top;
3713 end_y = end_pointi.Y - output_area.top;
3715 else
3717 step = -1;
3718 start_y = start_point.Y - output_area.top;
3719 end_y = ceilf(end_point.Y) - output_area.top;
3722 for (y=start_y; y != (end_y+step); y+=step)
3724 x = gdip_round( start_point.X +
3725 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3726 - output_area.left;
3728 if (x == prev_x && y == prev_y)
3729 continue;
3731 prev_x = x;
3732 prev_y = y;
3733 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3735 if (!dash_pattern[dash_pos])
3736 continue;
3738 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3739 continue;
3741 if (brush_bits)
3742 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3743 else
3744 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3747 else
3749 INT x, y, start_x, end_x, step;
3751 if (start_pointi.X < end_pointi.X)
3753 step = 1;
3754 start_x = ceilf(start_point.X) - output_area.left;
3755 end_x = end_pointi.X - output_area.left;
3757 else
3759 step = -1;
3760 start_x = start_point.X - output_area.left;
3761 end_x = ceilf(end_point.X) - output_area.left;
3764 for (x=start_x; x != (end_x+step); x+=step)
3766 y = gdip_round( start_point.Y +
3767 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3768 - output_area.top;
3770 if (x == prev_x && y == prev_y)
3771 continue;
3773 prev_x = x;
3774 prev_y = y;
3775 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3777 if (!dash_pattern[dash_pos])
3778 continue;
3780 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3781 continue;
3783 if (brush_bits)
3784 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3785 else
3786 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3792 /* draw output image */
3793 if (stat == Ok)
3795 gdi_transform_acquire(graphics);
3797 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3798 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3799 PixelFormat32bppARGB);
3801 gdi_transform_release(graphics);
3804 heap_free(brush_bits);
3805 heap_free(dyn_dash_pattern);
3806 heap_free(output_bits);
3809 GdipDeletePath(flat_path);
3811 return stat;
3814 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3816 GpStatus stat;
3817 GpPath *wide_path;
3818 GpMatrix *transform=NULL;
3819 REAL flatness=1.0;
3821 /* Check if the final pen thickness in pixels is too thin. */
3822 if (pen->unit == UnitPixel)
3824 if (pen->width < 1.415)
3825 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3827 else
3829 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3831 points[1].X = pen->width;
3832 points[2].Y = pen->width;
3834 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3835 CoordinateSpaceWorld, points, 3);
3837 if (stat != Ok)
3838 return stat;
3840 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3841 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3842 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3843 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3844 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3847 stat = GdipClonePath(path, &wide_path);
3849 if (stat != Ok)
3850 return stat;
3852 if (pen->unit == UnitPixel)
3854 /* We have to transform this to device coordinates to get the widths right. */
3855 stat = GdipCreateMatrix(&transform);
3857 if (stat == Ok)
3858 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3859 CoordinateSpaceWorld, transform);
3861 else
3863 /* Set flatness based on the final coordinate space */
3864 GpMatrix t;
3866 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3867 CoordinateSpaceWorld, &t);
3869 if (stat != Ok)
3870 return stat;
3872 flatness = 1.0/sqrt(fmax(
3873 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
3874 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
3877 if (stat == Ok)
3878 stat = GdipWidenPath(wide_path, pen, transform, flatness);
3880 if (pen->unit == UnitPixel)
3882 /* Transform the path back to world coordinates */
3883 if (stat == Ok)
3884 stat = GdipInvertMatrix(transform);
3886 if (stat == Ok)
3887 stat = GdipTransformPath(wide_path, transform);
3890 /* Actually draw the path */
3891 if (stat == Ok)
3892 stat = GdipFillPath(graphics, pen->brush, wide_path);
3894 GdipDeleteMatrix(transform);
3896 GdipDeletePath(wide_path);
3898 return stat;
3901 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3903 GpStatus retval;
3905 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3907 if(!pen || !graphics)
3908 return InvalidParameter;
3910 if(graphics->busy)
3911 return ObjectBusy;
3913 if (path->pathdata.Count == 0)
3914 return Ok;
3916 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3917 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
3918 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
3919 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
3920 else
3921 retval = GDI32_GdipDrawPath(graphics, pen, path);
3923 return retval;
3926 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3927 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3929 GpStatus status;
3930 GpPath *path;
3932 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3933 width, height, startAngle, sweepAngle);
3935 if(!graphics || !pen)
3936 return InvalidParameter;
3938 if(graphics->busy)
3939 return ObjectBusy;
3941 status = GdipCreatePath(FillModeAlternate, &path);
3942 if (status != Ok) return status;
3944 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3945 if (status == Ok)
3946 status = GdipDrawPath(graphics, pen, path);
3948 GdipDeletePath(path);
3949 return status;
3952 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3953 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3955 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3956 width, height, startAngle, sweepAngle);
3958 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3961 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3962 REAL y, REAL width, REAL height)
3964 GpStatus status;
3965 GpPath *path;
3967 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3969 if(!pen || !graphics)
3970 return InvalidParameter;
3972 if(graphics->busy)
3973 return ObjectBusy;
3975 status = GdipCreatePath(FillModeAlternate, &path);
3976 if (status != Ok) return status;
3978 status = GdipAddPathRectangle(path, x, y, width, height);
3979 if (status == Ok)
3980 status = GdipDrawPath(graphics, pen, path);
3982 GdipDeletePath(path);
3983 return status;
3986 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3987 INT y, INT width, INT height)
3989 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3991 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3994 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3995 GDIPCONST GpRectF* rects, INT count)
3997 GpStatus status;
3998 GpPath *path;
4000 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4002 if(!graphics || !pen || !rects || count < 1)
4003 return InvalidParameter;
4005 if(graphics->busy)
4006 return ObjectBusy;
4008 status = GdipCreatePath(FillModeAlternate, &path);
4009 if (status != Ok) return status;
4011 status = GdipAddPathRectangles(path, rects, count);
4012 if (status == Ok)
4013 status = GdipDrawPath(graphics, pen, path);
4015 GdipDeletePath(path);
4016 return status;
4019 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4020 GDIPCONST GpRect* rects, INT count)
4022 GpRectF *rectsF;
4023 GpStatus ret;
4024 INT i;
4026 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4028 if(!rects || count<=0)
4029 return InvalidParameter;
4031 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4032 if(!rectsF)
4033 return OutOfMemory;
4035 for(i = 0;i < count;i++){
4036 rectsF[i].X = (REAL)rects[i].X;
4037 rectsF[i].Y = (REAL)rects[i].Y;
4038 rectsF[i].Width = (REAL)rects[i].Width;
4039 rectsF[i].Height = (REAL)rects[i].Height;
4042 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4043 heap_free(rectsF);
4045 return ret;
4048 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4049 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4051 GpPath *path;
4052 GpStatus status;
4054 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4055 count, tension, fill);
4057 if(!graphics || !brush || !points)
4058 return InvalidParameter;
4060 if(graphics->busy)
4061 return ObjectBusy;
4063 if(count == 1) /* Do nothing */
4064 return Ok;
4066 status = GdipCreatePath(fill, &path);
4067 if (status != Ok) return status;
4069 status = GdipAddPathClosedCurve2(path, points, count, tension);
4070 if (status == Ok)
4071 status = GdipFillPath(graphics, brush, path);
4073 GdipDeletePath(path);
4074 return status;
4077 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4078 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4080 GpPointF *ptf;
4081 GpStatus stat;
4082 INT i;
4084 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4085 count, tension, fill);
4087 if(!points || count == 0)
4088 return InvalidParameter;
4090 if(count == 1) /* Do nothing */
4091 return Ok;
4093 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4094 if(!ptf)
4095 return OutOfMemory;
4097 for(i = 0;i < count;i++){
4098 ptf[i].X = (REAL)points[i].X;
4099 ptf[i].Y = (REAL)points[i].Y;
4102 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4104 heap_free(ptf);
4106 return stat;
4109 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4110 GDIPCONST GpPointF *points, INT count)
4112 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4113 return GdipFillClosedCurve2(graphics, brush, points, count,
4114 0.5f, FillModeAlternate);
4117 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4118 GDIPCONST GpPoint *points, INT count)
4120 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4121 return GdipFillClosedCurve2I(graphics, brush, points, count,
4122 0.5f, FillModeAlternate);
4125 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4126 REAL y, REAL width, REAL height)
4128 GpStatus stat;
4129 GpPath *path;
4131 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4133 if(!graphics || !brush)
4134 return InvalidParameter;
4136 if(graphics->busy)
4137 return ObjectBusy;
4139 stat = GdipCreatePath(FillModeAlternate, &path);
4141 if (stat == Ok)
4143 stat = GdipAddPathEllipse(path, x, y, width, height);
4145 if (stat == Ok)
4146 stat = GdipFillPath(graphics, brush, path);
4148 GdipDeletePath(path);
4151 return stat;
4154 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4155 INT y, INT width, INT height)
4157 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4159 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4162 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4164 INT save_state;
4165 GpStatus retval;
4166 HRGN hrgn=NULL;
4168 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4169 return NotImplemented;
4171 save_state = SaveDC(graphics->hdc);
4172 EndPath(graphics->hdc);
4173 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4174 : WINDING));
4176 retval = get_clip_hrgn(graphics, &hrgn);
4178 if (retval != Ok)
4179 goto end;
4181 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4183 gdi_transform_acquire(graphics);
4185 BeginPath(graphics->hdc);
4186 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4187 path->pathdata.Types, path->pathdata.Count, FALSE);
4189 if(retval == Ok)
4191 EndPath(graphics->hdc);
4192 brush_fill_path(graphics, brush);
4195 gdi_transform_release(graphics);
4197 end:
4198 RestoreDC(graphics->hdc, save_state);
4199 DeleteObject(hrgn);
4201 return retval;
4204 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4206 GpStatus stat;
4207 GpRegion *rgn;
4209 if (!brush_can_fill_pixels(brush))
4210 return NotImplemented;
4212 /* FIXME: This could probably be done more efficiently without regions. */
4214 stat = GdipCreateRegionPath(path, &rgn);
4216 if (stat == Ok)
4218 stat = GdipFillRegion(graphics, brush, rgn);
4220 GdipDeleteRegion(rgn);
4223 return stat;
4226 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4228 GpStatus stat = NotImplemented;
4230 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4232 if(!brush || !graphics || !path)
4233 return InvalidParameter;
4235 if(graphics->busy)
4236 return ObjectBusy;
4238 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4239 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4241 if (!graphics->image && !graphics->alpha_hdc)
4242 stat = GDI32_GdipFillPath(graphics, brush, path);
4244 if (stat == NotImplemented)
4245 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4247 if (stat == NotImplemented)
4249 FIXME("Not implemented for brushtype %i\n", brush->bt);
4250 stat = Ok;
4253 return stat;
4256 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4257 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4259 GpStatus stat;
4260 GpPath *path;
4262 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4263 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4265 if(!graphics || !brush)
4266 return InvalidParameter;
4268 if(graphics->busy)
4269 return ObjectBusy;
4271 stat = GdipCreatePath(FillModeAlternate, &path);
4273 if (stat == Ok)
4275 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4277 if (stat == Ok)
4278 stat = GdipFillPath(graphics, brush, path);
4280 GdipDeletePath(path);
4283 return stat;
4286 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4287 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4289 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4290 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4292 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4295 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4296 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4298 GpStatus stat;
4299 GpPath *path;
4301 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4303 if(!graphics || !brush || !points || !count)
4304 return InvalidParameter;
4306 if(graphics->busy)
4307 return ObjectBusy;
4309 stat = GdipCreatePath(fillMode, &path);
4311 if (stat == Ok)
4313 stat = GdipAddPathPolygon(path, points, count);
4315 if (stat == Ok)
4316 stat = GdipFillPath(graphics, brush, path);
4318 GdipDeletePath(path);
4321 return stat;
4324 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4325 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4327 GpStatus stat;
4328 GpPath *path;
4330 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4332 if(!graphics || !brush || !points || !count)
4333 return InvalidParameter;
4335 if(graphics->busy)
4336 return ObjectBusy;
4338 stat = GdipCreatePath(fillMode, &path);
4340 if (stat == Ok)
4342 stat = GdipAddPathPolygonI(path, points, count);
4344 if (stat == Ok)
4345 stat = GdipFillPath(graphics, brush, path);
4347 GdipDeletePath(path);
4350 return stat;
4353 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4354 GDIPCONST GpPointF *points, INT count)
4356 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4358 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4361 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4362 GDIPCONST GpPoint *points, INT count)
4364 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4366 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4369 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4370 REAL x, REAL y, REAL width, REAL height)
4372 GpRectF rect;
4374 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4376 rect.X = x;
4377 rect.Y = y;
4378 rect.Width = width;
4379 rect.Height = height;
4381 return GdipFillRectangles(graphics, brush, &rect, 1);
4384 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4385 INT x, INT y, INT width, INT height)
4387 GpRectF rect;
4389 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4391 rect.X = (REAL)x;
4392 rect.Y = (REAL)y;
4393 rect.Width = (REAL)width;
4394 rect.Height = (REAL)height;
4396 return GdipFillRectangles(graphics, brush, &rect, 1);
4399 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4400 INT count)
4402 GpStatus status;
4403 GpPath *path;
4405 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4407 if(!graphics || !brush || !rects || count <= 0)
4408 return InvalidParameter;
4410 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4412 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4413 /* FIXME: Add gdi32 drawing. */
4414 return status;
4417 status = GdipCreatePath(FillModeAlternate, &path);
4418 if (status != Ok) return status;
4420 status = GdipAddPathRectangles(path, rects, count);
4421 if (status == Ok)
4422 status = GdipFillPath(graphics, brush, path);
4424 GdipDeletePath(path);
4425 return status;
4428 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4429 INT count)
4431 GpRectF *rectsF;
4432 GpStatus ret;
4433 INT i;
4435 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4437 if(!rects || count <= 0)
4438 return InvalidParameter;
4440 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4441 if(!rectsF)
4442 return OutOfMemory;
4444 for(i = 0; i < count; i++){
4445 rectsF[i].X = (REAL)rects[i].X;
4446 rectsF[i].Y = (REAL)rects[i].Y;
4447 rectsF[i].Width = (REAL)rects[i].Width;
4448 rectsF[i].Height = (REAL)rects[i].Height;
4451 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4452 heap_free(rectsF);
4454 return ret;
4457 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4458 GpRegion* region)
4460 INT save_state;
4461 GpStatus status;
4462 HRGN hrgn;
4463 RECT rc;
4465 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4466 return NotImplemented;
4468 save_state = SaveDC(graphics->hdc);
4469 EndPath(graphics->hdc);
4471 hrgn = NULL;
4472 status = get_clip_hrgn(graphics, &hrgn);
4473 if (status != Ok)
4475 RestoreDC(graphics->hdc, save_state);
4476 return status;
4479 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4480 DeleteObject(hrgn);
4482 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4483 if (status != Ok)
4485 RestoreDC(graphics->hdc, save_state);
4486 return status;
4489 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4490 DeleteObject(hrgn);
4492 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4494 BeginPath(graphics->hdc);
4495 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4496 EndPath(graphics->hdc);
4498 brush_fill_path(graphics, brush);
4501 RestoreDC(graphics->hdc, save_state);
4504 return Ok;
4507 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4508 GpRegion* region)
4510 GpStatus stat;
4511 GpRegion *temp_region;
4512 GpMatrix world_to_device;
4513 GpRectF graphics_bounds;
4514 DWORD *pixel_data;
4515 HRGN hregion;
4516 RECT bound_rect;
4517 GpRect gp_bound_rect;
4519 if (!brush_can_fill_pixels(brush))
4520 return NotImplemented;
4522 stat = gdi_transform_acquire(graphics);
4524 if (stat == Ok)
4525 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4527 if (stat == Ok)
4528 stat = GdipCloneRegion(region, &temp_region);
4530 if (stat == Ok)
4532 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4533 CoordinateSpaceWorld, &world_to_device);
4535 if (stat == Ok)
4536 stat = GdipTransformRegion(temp_region, &world_to_device);
4538 if (stat == Ok)
4539 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4541 if (stat == Ok)
4542 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4544 GdipDeleteRegion(temp_region);
4547 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4549 DeleteObject(hregion);
4550 gdi_transform_release(graphics);
4551 return Ok;
4554 if (stat == Ok)
4556 gp_bound_rect.X = bound_rect.left;
4557 gp_bound_rect.Y = bound_rect.top;
4558 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4559 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4561 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4562 if (!pixel_data)
4563 stat = OutOfMemory;
4565 if (stat == Ok)
4567 stat = brush_fill_pixels(graphics, brush, pixel_data,
4568 &gp_bound_rect, gp_bound_rect.Width);
4570 if (stat == Ok)
4571 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4572 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4573 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4574 PixelFormat32bppARGB);
4576 heap_free(pixel_data);
4579 DeleteObject(hregion);
4582 gdi_transform_release(graphics);
4584 return stat;
4587 /*****************************************************************************
4588 * GdipFillRegion [GDIPLUS.@]
4590 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4591 GpRegion* region)
4593 GpStatus stat = NotImplemented;
4595 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4597 if (!(graphics && brush && region))
4598 return InvalidParameter;
4600 if(graphics->busy)
4601 return ObjectBusy;
4603 if (!graphics->image && !graphics->alpha_hdc)
4604 stat = GDI32_GdipFillRegion(graphics, brush, region);
4606 if (stat == NotImplemented)
4607 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4609 if (stat == NotImplemented)
4611 FIXME("not implemented for brushtype %i\n", brush->bt);
4612 stat = Ok;
4615 return stat;
4618 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4620 TRACE("(%p,%u)\n", graphics, intention);
4622 if(!graphics)
4623 return InvalidParameter;
4625 if(graphics->busy)
4626 return ObjectBusy;
4628 /* We have no internal operation queue, so there's no need to clear it. */
4630 if (graphics->hdc)
4631 GdiFlush();
4633 return Ok;
4636 /*****************************************************************************
4637 * GdipGetClipBounds [GDIPLUS.@]
4639 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4641 GpStatus status;
4642 GpRegion *clip;
4644 TRACE("(%p, %p)\n", graphics, rect);
4646 if(!graphics)
4647 return InvalidParameter;
4649 if(graphics->busy)
4650 return ObjectBusy;
4652 status = GdipCreateRegion(&clip);
4653 if (status != Ok) return status;
4655 status = GdipGetClip(graphics, clip);
4656 if (status == Ok)
4657 status = GdipGetRegionBounds(clip, graphics, rect);
4659 GdipDeleteRegion(clip);
4660 return status;
4663 /*****************************************************************************
4664 * GdipGetClipBoundsI [GDIPLUS.@]
4666 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4668 TRACE("(%p, %p)\n", graphics, rect);
4670 if(!graphics)
4671 return InvalidParameter;
4673 if(graphics->busy)
4674 return ObjectBusy;
4676 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4679 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4680 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4681 CompositingMode *mode)
4683 TRACE("(%p, %p)\n", graphics, mode);
4685 if(!graphics || !mode)
4686 return InvalidParameter;
4688 if(graphics->busy)
4689 return ObjectBusy;
4691 *mode = graphics->compmode;
4693 return Ok;
4696 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4697 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4698 CompositingQuality *quality)
4700 TRACE("(%p, %p)\n", graphics, quality);
4702 if(!graphics || !quality)
4703 return InvalidParameter;
4705 if(graphics->busy)
4706 return ObjectBusy;
4708 *quality = graphics->compqual;
4710 return Ok;
4713 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4714 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4715 InterpolationMode *mode)
4717 TRACE("(%p, %p)\n", graphics, mode);
4719 if(!graphics || !mode)
4720 return InvalidParameter;
4722 if(graphics->busy)
4723 return ObjectBusy;
4725 *mode = graphics->interpolation;
4727 return Ok;
4730 /* FIXME: Need to handle color depths less than 24bpp */
4731 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4733 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4735 if(!graphics || !argb)
4736 return InvalidParameter;
4738 if(graphics->busy)
4739 return ObjectBusy;
4741 return Ok;
4744 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4746 TRACE("(%p, %p)\n", graphics, scale);
4748 if(!graphics || !scale)
4749 return InvalidParameter;
4751 if(graphics->busy)
4752 return ObjectBusy;
4754 *scale = graphics->scale;
4756 return Ok;
4759 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4761 TRACE("(%p, %p)\n", graphics, unit);
4763 if(!graphics || !unit)
4764 return InvalidParameter;
4766 if(graphics->busy)
4767 return ObjectBusy;
4769 *unit = graphics->unit;
4771 return Ok;
4774 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4775 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4776 *mode)
4778 TRACE("(%p, %p)\n", graphics, mode);
4780 if(!graphics || !mode)
4781 return InvalidParameter;
4783 if(graphics->busy)
4784 return ObjectBusy;
4786 *mode = graphics->pixeloffset;
4788 return Ok;
4791 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4792 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4794 TRACE("(%p, %p)\n", graphics, mode);
4796 if(!graphics || !mode)
4797 return InvalidParameter;
4799 if(graphics->busy)
4800 return ObjectBusy;
4802 *mode = graphics->smoothing;
4804 return Ok;
4807 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4809 TRACE("(%p, %p)\n", graphics, contrast);
4811 if(!graphics || !contrast)
4812 return InvalidParameter;
4814 *contrast = graphics->textcontrast;
4816 return Ok;
4819 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4820 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4821 TextRenderingHint *hint)
4823 TRACE("(%p, %p)\n", graphics, hint);
4825 if(!graphics || !hint)
4826 return InvalidParameter;
4828 if(graphics->busy)
4829 return ObjectBusy;
4831 *hint = graphics->texthint;
4833 return Ok;
4836 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4838 GpRegion *clip_rgn;
4839 GpStatus stat;
4840 GpMatrix device_to_world;
4842 TRACE("(%p, %p)\n", graphics, rect);
4844 if(!graphics || !rect)
4845 return InvalidParameter;
4847 if(graphics->busy)
4848 return ObjectBusy;
4850 /* intersect window and graphics clipping regions */
4851 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4852 return stat;
4854 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4855 goto cleanup;
4857 /* transform to world coordinates */
4858 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4859 goto cleanup;
4861 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4862 goto cleanup;
4864 /* get bounds of the region */
4865 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4867 cleanup:
4868 GdipDeleteRegion(clip_rgn);
4870 return stat;
4873 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4875 GpRectF rectf;
4876 GpStatus stat;
4878 TRACE("(%p, %p)\n", graphics, rect);
4880 if(!graphics || !rect)
4881 return InvalidParameter;
4883 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4885 rect->X = gdip_round(rectf.X);
4886 rect->Y = gdip_round(rectf.Y);
4887 rect->Width = gdip_round(rectf.Width);
4888 rect->Height = gdip_round(rectf.Height);
4891 return stat;
4894 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4896 TRACE("(%p, %p)\n", graphics, matrix);
4898 if(!graphics || !matrix)
4899 return InvalidParameter;
4901 if(graphics->busy)
4902 return ObjectBusy;
4904 *matrix = graphics->worldtrans;
4905 return Ok;
4908 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4910 GpSolidFill *brush;
4911 GpStatus stat;
4912 GpRectF wnd_rect;
4914 TRACE("(%p, %x)\n", graphics, color);
4916 if(!graphics)
4917 return InvalidParameter;
4919 if(graphics->busy)
4920 return ObjectBusy;
4922 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4923 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
4925 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4926 return stat;
4928 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
4929 GdipDeleteBrush((GpBrush*)brush);
4930 return stat;
4933 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4934 wnd_rect.Width, wnd_rect.Height);
4936 GdipDeleteBrush((GpBrush*)brush);
4938 return Ok;
4941 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4943 TRACE("(%p, %p)\n", graphics, res);
4945 if(!graphics || !res)
4946 return InvalidParameter;
4948 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4951 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4953 GpStatus stat;
4954 GpRegion* rgn;
4955 GpPointF pt;
4957 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4959 if(!graphics || !result)
4960 return InvalidParameter;
4962 if(graphics->busy)
4963 return ObjectBusy;
4965 pt.X = x;
4966 pt.Y = y;
4967 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4968 CoordinateSpaceWorld, &pt, 1)) != Ok)
4969 return stat;
4971 if((stat = GdipCreateRegion(&rgn)) != Ok)
4972 return stat;
4974 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4975 goto cleanup;
4977 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4979 cleanup:
4980 GdipDeleteRegion(rgn);
4981 return stat;
4984 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4986 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4989 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4991 GpStatus stat;
4992 GpRegion* rgn;
4993 GpPointF pts[2];
4995 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4997 if(!graphics || !result)
4998 return InvalidParameter;
5000 if(graphics->busy)
5001 return ObjectBusy;
5003 pts[0].X = x;
5004 pts[0].Y = y;
5005 pts[1].X = x + width;
5006 pts[1].Y = y + height;
5008 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5009 CoordinateSpaceWorld, pts, 2)) != Ok)
5010 return stat;
5012 pts[1].X -= pts[0].X;
5013 pts[1].Y -= pts[0].Y;
5015 if((stat = GdipCreateRegion(&rgn)) != Ok)
5016 return stat;
5018 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5019 goto cleanup;
5021 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5023 cleanup:
5024 GdipDeleteRegion(rgn);
5025 return stat;
5028 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5030 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5033 GpStatus gdip_format_string(HDC hdc,
5034 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5035 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5036 gdip_format_string_callback callback, void *user_data)
5038 WCHAR* stringdup;
5039 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5040 nheight, lineend, lineno = 0;
5041 RectF bounds;
5042 StringAlignment halign;
5043 GpStatus stat = Ok;
5044 SIZE size;
5045 HotkeyPrefix hkprefix;
5046 INT *hotkeyprefix_offsets=NULL;
5047 INT hotkeyprefix_count=0;
5048 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5049 BOOL seen_prefix = FALSE;
5051 if(length == -1) length = lstrlenW(string);
5053 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5054 if(!stringdup) return OutOfMemory;
5056 if (!format)
5057 format = &default_drawstring_format;
5059 nwidth = rect->Width;
5060 nheight = rect->Height;
5061 if (ignore_empty_clip)
5063 if (!nwidth) nwidth = INT_MAX;
5064 if (!nheight) nheight = INT_MAX;
5067 hkprefix = format->hkprefix;
5069 if (hkprefix == HotkeyPrefixShow)
5071 for (i=0; i<length; i++)
5073 if (string[i] == '&')
5074 hotkeyprefix_count++;
5078 if (hotkeyprefix_count)
5079 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5081 hotkeyprefix_count = 0;
5083 for(i = 0, j = 0; i < length; i++){
5084 /* FIXME: This makes the indexes passed to callback inaccurate. */
5085 if(!isprintW(string[i]) && (string[i] != '\n'))
5086 continue;
5088 /* FIXME: tabs should be handled using tabstops from stringformat */
5089 if (string[i] == '\t')
5090 continue;
5092 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5093 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5094 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5096 seen_prefix = TRUE;
5097 continue;
5100 seen_prefix = FALSE;
5102 stringdup[j] = string[i];
5103 j++;
5106 length = j;
5108 halign = format->align;
5110 while(sum < length){
5111 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5112 nwidth, &fit, NULL, &size);
5113 fitcpy = fit;
5115 if(fit == 0)
5116 break;
5118 for(lret = 0; lret < fit; lret++)
5119 if(*(stringdup + sum + lret) == '\n')
5120 break;
5122 /* Line break code (may look strange, but it imitates windows). */
5123 if(lret < fit)
5124 lineend = fit = lret; /* this is not an off-by-one error */
5125 else if(fit < (length - sum)){
5126 if(*(stringdup + sum + fit) == ' ')
5127 while(*(stringdup + sum + fit) == ' ')
5128 fit++;
5129 else
5130 while(*(stringdup + sum + fit - 1) != ' '){
5131 fit--;
5133 if(*(stringdup + sum + fit) == '\t')
5134 break;
5136 if(fit == 0){
5137 fit = fitcpy;
5138 break;
5141 lineend = fit;
5142 while(*(stringdup + sum + lineend - 1) == ' ' ||
5143 *(stringdup + sum + lineend - 1) == '\t')
5144 lineend--;
5146 else
5147 lineend = fit;
5149 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5150 nwidth, &j, NULL, &size);
5152 bounds.Width = size.cx;
5154 if(height + size.cy > nheight)
5156 if (format->attr & StringFormatFlagsLineLimit)
5157 break;
5158 bounds.Height = nheight - (height + size.cy);
5160 else
5161 bounds.Height = size.cy;
5163 bounds.Y = rect->Y + height;
5165 switch (halign)
5167 case StringAlignmentNear:
5168 default:
5169 bounds.X = rect->X;
5170 break;
5171 case StringAlignmentCenter:
5172 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5173 break;
5174 case StringAlignmentFar:
5175 bounds.X = rect->X + rect->Width - bounds.Width;
5176 break;
5179 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5180 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5181 break;
5183 stat = callback(hdc, stringdup, sum, lineend,
5184 font, rect, format, lineno, &bounds,
5185 &hotkeyprefix_offsets[hotkeyprefix_pos],
5186 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5188 if (stat != Ok)
5189 break;
5191 sum += fit + (lret < fitcpy ? 1 : 0);
5192 height += size.cy;
5193 lineno++;
5195 hotkeyprefix_pos = hotkeyprefix_end_pos;
5197 if(height > nheight)
5198 break;
5200 /* Stop if this was a linewrap (but not if it was a linebreak). */
5201 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5202 break;
5205 heap_free(stringdup);
5206 heap_free(hotkeyprefix_offsets);
5208 return stat;
5211 struct measure_ranges_args {
5212 GpRegion **regions;
5213 REAL rel_width, rel_height;
5216 static GpStatus measure_ranges_callback(HDC hdc,
5217 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5218 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5219 INT lineno, const RectF *bounds, INT *underlined_indexes,
5220 INT underlined_index_count, void *user_data)
5222 int i;
5223 GpStatus stat = Ok;
5224 struct measure_ranges_args *args = user_data;
5226 for (i=0; i<format->range_count; i++)
5228 INT range_start = max(index, format->character_ranges[i].First);
5229 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5230 if (range_start < range_end)
5232 GpRectF range_rect;
5233 SIZE range_size;
5235 range_rect.Y = bounds->Y / args->rel_height;
5236 range_rect.Height = bounds->Height / args->rel_height;
5238 GetTextExtentExPointW(hdc, string + index, range_start - index,
5239 INT_MAX, NULL, NULL, &range_size);
5240 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5242 GetTextExtentExPointW(hdc, string + index, range_end - index,
5243 INT_MAX, NULL, NULL, &range_size);
5244 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5246 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5247 if (stat != Ok)
5248 break;
5252 return stat;
5255 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5256 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5257 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5258 INT regionCount, GpRegion** regions)
5260 GpStatus stat;
5261 int i;
5262 HFONT gdifont, oldfont;
5263 struct measure_ranges_args args;
5264 HDC hdc, temp_hdc=NULL;
5265 GpPointF pt[3];
5266 RectF scaled_rect;
5267 REAL margin_x;
5269 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
5270 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5272 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5273 return InvalidParameter;
5275 if (regionCount < stringFormat->range_count)
5276 return InvalidParameter;
5278 if(!graphics->hdc)
5280 hdc = temp_hdc = CreateCompatibleDC(0);
5281 if (!temp_hdc) return OutOfMemory;
5283 else
5284 hdc = graphics->hdc;
5286 if (stringFormat->attr)
5287 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5289 pt[0].X = 0.0;
5290 pt[0].Y = 0.0;
5291 pt[1].X = 1.0;
5292 pt[1].Y = 0.0;
5293 pt[2].X = 0.0;
5294 pt[2].Y = 1.0;
5295 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5296 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5297 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5298 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5299 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5301 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5302 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5304 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5305 scaled_rect.Y = layoutRect->Y * args.rel_height;
5306 scaled_rect.Width = layoutRect->Width * args.rel_width;
5307 scaled_rect.Height = layoutRect->Height * args.rel_height;
5309 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5310 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5312 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5313 oldfont = SelectObject(hdc, gdifont);
5315 for (i=0; i<stringFormat->range_count; i++)
5317 stat = GdipSetEmpty(regions[i]);
5318 if (stat != Ok)
5319 return stat;
5322 args.regions = regions;
5324 gdi_transform_acquire(graphics);
5326 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5327 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5329 gdi_transform_release(graphics);
5331 SelectObject(hdc, oldfont);
5332 DeleteObject(gdifont);
5334 if (temp_hdc)
5335 DeleteDC(temp_hdc);
5337 return stat;
5340 struct measure_string_args {
5341 RectF *bounds;
5342 INT *codepointsfitted;
5343 INT *linesfilled;
5344 REAL rel_width, rel_height;
5347 static GpStatus measure_string_callback(HDC hdc,
5348 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5349 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5350 INT lineno, const RectF *bounds, INT *underlined_indexes,
5351 INT underlined_index_count, void *user_data)
5353 struct measure_string_args *args = user_data;
5354 REAL new_width, new_height;
5356 new_width = bounds->Width / args->rel_width;
5357 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5359 if (new_width > args->bounds->Width)
5360 args->bounds->Width = new_width;
5362 if (new_height > args->bounds->Height)
5363 args->bounds->Height = new_height;
5365 if (args->codepointsfitted)
5366 *args->codepointsfitted = index + length;
5368 if (args->linesfilled)
5369 (*args->linesfilled)++;
5371 return Ok;
5374 /* Find the smallest rectangle that bounds the text when it is printed in rect
5375 * according to the format options listed in format. If rect has 0 width and
5376 * height, then just find the smallest rectangle that bounds the text when it's
5377 * printed at location (rect->X, rect-Y). */
5378 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5379 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5380 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5381 INT *codepointsfitted, INT *linesfilled)
5383 HFONT oldfont, gdifont;
5384 struct measure_string_args args;
5385 HDC temp_hdc=NULL, hdc;
5386 GpPointF pt[3];
5387 RectF scaled_rect;
5388 REAL margin_x;
5389 INT lines, glyphs;
5391 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5392 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5393 bounds, codepointsfitted, linesfilled);
5395 if(!graphics || !string || !font || !rect || !bounds)
5396 return InvalidParameter;
5398 if(!graphics->hdc)
5400 hdc = temp_hdc = CreateCompatibleDC(0);
5401 if (!temp_hdc) return OutOfMemory;
5403 else
5404 hdc = graphics->hdc;
5406 if(linesfilled) *linesfilled = 0;
5407 if(codepointsfitted) *codepointsfitted = 0;
5409 if(format)
5410 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5412 pt[0].X = 0.0;
5413 pt[0].Y = 0.0;
5414 pt[1].X = 1.0;
5415 pt[1].Y = 0.0;
5416 pt[2].X = 0.0;
5417 pt[2].Y = 1.0;
5418 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5419 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5420 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5421 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5422 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5424 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5425 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5427 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5428 scaled_rect.Y = rect->Y * args.rel_height;
5429 scaled_rect.Width = rect->Width * args.rel_width;
5430 scaled_rect.Height = rect->Height * args.rel_height;
5431 if (scaled_rect.Width >= 0.5)
5433 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5434 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5437 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5438 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5440 get_font_hfont(graphics, font, format, &gdifont, NULL);
5441 oldfont = SelectObject(hdc, gdifont);
5443 bounds->X = rect->X;
5444 bounds->Y = rect->Y;
5445 bounds->Width = 0.0;
5446 bounds->Height = 0.0;
5448 args.bounds = bounds;
5449 args.codepointsfitted = &glyphs;
5450 args.linesfilled = &lines;
5451 lines = glyphs = 0;
5453 gdi_transform_acquire(graphics);
5455 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5456 measure_string_callback, &args);
5458 gdi_transform_release(graphics);
5460 if (linesfilled) *linesfilled = lines;
5461 if (codepointsfitted) *codepointsfitted = glyphs;
5463 if (lines)
5464 bounds->Width += margin_x * 2.0;
5466 SelectObject(hdc, oldfont);
5467 DeleteObject(gdifont);
5469 if (temp_hdc)
5470 DeleteDC(temp_hdc);
5472 return Ok;
5475 struct draw_string_args {
5476 GpGraphics *graphics;
5477 GDIPCONST GpBrush *brush;
5478 REAL x, y, rel_width, rel_height, ascent;
5481 static GpStatus draw_string_callback(HDC hdc,
5482 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5483 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5484 INT lineno, const RectF *bounds, INT *underlined_indexes,
5485 INT underlined_index_count, void *user_data)
5487 struct draw_string_args *args = user_data;
5488 PointF position;
5489 GpStatus stat;
5491 position.X = args->x + bounds->X / args->rel_width;
5492 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5494 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5495 args->brush, &position,
5496 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5498 if (stat == Ok && underlined_index_count)
5500 OUTLINETEXTMETRICW otm;
5501 REAL underline_y, underline_height;
5502 int i;
5504 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5506 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5507 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5509 for (i=0; i<underlined_index_count; i++)
5511 REAL start_x, end_x;
5512 SIZE text_size;
5513 INT ofs = underlined_indexes[i] - index;
5515 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5516 start_x = text_size.cx / args->rel_width;
5518 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5519 end_x = text_size.cx / args->rel_width;
5521 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5525 return stat;
5528 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5529 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5530 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5532 HRGN rgn = NULL;
5533 HFONT gdifont;
5534 GpPointF pt[3], rectcpy[4];
5535 POINT corners[4];
5536 REAL rel_width, rel_height, margin_x;
5537 INT save_state, format_flags = 0;
5538 REAL offsety = 0.0;
5539 struct draw_string_args args;
5540 RectF scaled_rect;
5541 HDC hdc, temp_hdc=NULL;
5542 TEXTMETRICW textmetric;
5544 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5545 length, font, debugstr_rectf(rect), format, brush);
5547 if(!graphics || !string || !font || !brush || !rect)
5548 return InvalidParameter;
5550 if(graphics->hdc)
5552 hdc = graphics->hdc;
5554 else
5556 hdc = temp_hdc = CreateCompatibleDC(0);
5559 if(format){
5560 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5562 format_flags = format->attr;
5564 /* Should be no need to explicitly test for StringAlignmentNear as
5565 * that is default behavior if no alignment is passed. */
5566 if(format->line_align != StringAlignmentNear){
5567 RectF bounds, in_rect = *rect;
5568 in_rect.Height = 0.0; /* avoid height clipping */
5569 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5571 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5573 if(format->line_align == StringAlignmentCenter)
5574 offsety = (rect->Height - bounds.Height) / 2;
5575 else if(format->line_align == StringAlignmentFar)
5576 offsety = (rect->Height - bounds.Height);
5578 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5581 save_state = SaveDC(hdc);
5583 pt[0].X = 0.0;
5584 pt[0].Y = 0.0;
5585 pt[1].X = 1.0;
5586 pt[1].Y = 0.0;
5587 pt[2].X = 0.0;
5588 pt[2].Y = 1.0;
5589 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5590 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5591 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5592 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5593 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5595 rectcpy[3].X = rectcpy[0].X = rect->X;
5596 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5597 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5598 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5599 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5600 round_points(corners, rectcpy, 4);
5602 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5603 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5605 scaled_rect.X = margin_x * rel_width;
5606 scaled_rect.Y = 0.0;
5607 scaled_rect.Width = rel_width * rect->Width;
5608 scaled_rect.Height = rel_height * rect->Height;
5609 if (scaled_rect.Width >= 0.5)
5611 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5612 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5615 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5616 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5618 if (!(format_flags & StringFormatFlagsNoClip) &&
5619 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5620 rect->Width > 0.0 && rect->Height > 0.0)
5622 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5623 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5624 SelectClipRgn(hdc, rgn);
5627 get_font_hfont(graphics, font, format, &gdifont, NULL);
5628 SelectObject(hdc, gdifont);
5630 args.graphics = graphics;
5631 args.brush = brush;
5633 args.x = rect->X;
5634 args.y = rect->Y + offsety;
5636 args.rel_width = rel_width;
5637 args.rel_height = rel_height;
5639 gdi_transform_acquire(graphics);
5641 GetTextMetricsW(hdc, &textmetric);
5642 args.ascent = textmetric.tmAscent / rel_height;
5644 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5645 draw_string_callback, &args);
5647 gdi_transform_release(graphics);
5649 DeleteObject(rgn);
5650 DeleteObject(gdifont);
5652 RestoreDC(hdc, save_state);
5654 DeleteDC(temp_hdc);
5656 return Ok;
5659 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5661 TRACE("(%p)\n", graphics);
5663 if(!graphics)
5664 return InvalidParameter;
5666 if(graphics->busy)
5667 return ObjectBusy;
5669 return GdipSetInfinite(graphics->clip);
5672 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5674 GpStatus stat;
5676 TRACE("(%p)\n", graphics);
5678 if(!graphics)
5679 return InvalidParameter;
5681 if(graphics->busy)
5682 return ObjectBusy;
5684 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5685 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5687 if (stat != Ok)
5688 return stat;
5691 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5694 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5695 GpMatrixOrder order)
5697 GpStatus stat;
5699 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5701 if(!graphics)
5702 return InvalidParameter;
5704 if(graphics->busy)
5705 return ObjectBusy;
5707 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5708 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5710 if (stat != Ok)
5711 return stat;
5714 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5717 static GpStatus begin_container(GpGraphics *graphics,
5718 GraphicsContainerType type, GraphicsContainer *state)
5720 GraphicsContainerItem *container;
5721 GpStatus sts;
5723 if(!graphics || !state)
5724 return InvalidParameter;
5726 sts = init_container(&container, graphics, type);
5727 if(sts != Ok)
5728 return sts;
5730 list_add_head(&graphics->containers, &container->entry);
5731 *state = graphics->contid = container->contid;
5733 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5734 if (type == BEGIN_CONTAINER)
5735 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5736 else
5737 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5740 return Ok;
5743 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5745 TRACE("(%p, %p)\n", graphics, state);
5746 return begin_container(graphics, SAVE_GRAPHICS, state);
5749 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5750 GraphicsContainer *state)
5752 TRACE("(%p, %p)\n", graphics, state);
5753 return begin_container(graphics, BEGIN_CONTAINER, state);
5756 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5758 GraphicsContainerItem *container;
5759 GpMatrix transform;
5760 GpStatus stat;
5761 GpRectF scaled_srcrect;
5762 REAL scale_x, scale_y;
5764 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5766 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5767 return InvalidParameter;
5769 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5770 if(stat != Ok)
5771 return stat;
5773 list_add_head(&graphics->containers, &container->entry);
5774 *state = graphics->contid = container->contid;
5776 scale_x = units_to_pixels(1.0, unit, graphics->xres);
5777 scale_y = units_to_pixels(1.0, unit, graphics->yres);
5779 scaled_srcrect.X = scale_x * srcrect->X;
5780 scaled_srcrect.Y = scale_y * srcrect->Y;
5781 scaled_srcrect.Width = scale_x * srcrect->Width;
5782 scaled_srcrect.Height = scale_y * srcrect->Height;
5784 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5785 transform.matrix[1] = 0.0;
5786 transform.matrix[2] = 0.0;
5787 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5788 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5789 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5791 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5793 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5794 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5797 return Ok;
5800 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5802 GpRectF dstrectf, srcrectf;
5804 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5806 if (!dstrect || !srcrect)
5807 return InvalidParameter;
5809 dstrectf.X = dstrect->X;
5810 dstrectf.Y = dstrect->Y;
5811 dstrectf.Width = dstrect->Width;
5812 dstrectf.Height = dstrect->Height;
5814 srcrectf.X = srcrect->X;
5815 srcrectf.Y = srcrect->Y;
5816 srcrectf.Width = srcrect->Width;
5817 srcrectf.Height = srcrect->Height;
5819 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5822 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5824 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5825 return NotImplemented;
5828 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5829 GraphicsContainer state)
5831 GpStatus sts;
5832 GraphicsContainerItem *container, *container2;
5834 if(!graphics)
5835 return InvalidParameter;
5837 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5838 if(container->contid == state && container->type == type)
5839 break;
5842 /* did not find a matching container */
5843 if(&container->entry == &graphics->containers)
5844 return Ok;
5846 sts = restore_container(graphics, container);
5847 if(sts != Ok)
5848 return sts;
5850 /* remove all of the containers on top of the found container */
5851 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5852 if(container->contid == state)
5853 break;
5854 list_remove(&container->entry);
5855 delete_container(container);
5858 list_remove(&container->entry);
5859 delete_container(container);
5861 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5862 if (type == BEGIN_CONTAINER)
5863 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
5864 else
5865 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
5868 return Ok;
5871 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5873 TRACE("(%p, %x)\n", graphics, state);
5874 return end_container(graphics, BEGIN_CONTAINER, state);
5877 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5879 TRACE("(%p, %x)\n", graphics, state);
5880 return end_container(graphics, SAVE_GRAPHICS, state);
5883 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5884 REAL sy, GpMatrixOrder order)
5886 GpStatus stat;
5888 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5890 if(!graphics)
5891 return InvalidParameter;
5893 if(graphics->busy)
5894 return ObjectBusy;
5896 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5897 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
5899 if (stat != Ok)
5900 return stat;
5903 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5906 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5907 CombineMode mode)
5909 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5911 if(!graphics || !srcgraphics)
5912 return InvalidParameter;
5914 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5917 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5918 CompositingMode mode)
5920 TRACE("(%p, %d)\n", graphics, mode);
5922 if(!graphics)
5923 return InvalidParameter;
5925 if(graphics->busy)
5926 return ObjectBusy;
5928 if(graphics->compmode == mode)
5929 return Ok;
5931 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5933 GpStatus stat;
5935 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5936 EmfPlusRecordTypeSetCompositingMode, mode);
5937 if(stat != Ok)
5938 return stat;
5941 graphics->compmode = mode;
5943 return Ok;
5946 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5947 CompositingQuality quality)
5949 TRACE("(%p, %d)\n", graphics, quality);
5951 if(!graphics)
5952 return InvalidParameter;
5954 if(graphics->busy)
5955 return ObjectBusy;
5957 if(graphics->compqual == quality)
5958 return Ok;
5960 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5962 GpStatus stat;
5964 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5965 EmfPlusRecordTypeSetCompositingQuality, quality);
5966 if(stat != Ok)
5967 return stat;
5970 graphics->compqual = quality;
5972 return Ok;
5975 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5976 InterpolationMode mode)
5978 TRACE("(%p, %d)\n", graphics, mode);
5980 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5981 return InvalidParameter;
5983 if(graphics->busy)
5984 return ObjectBusy;
5986 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5987 mode = InterpolationModeBilinear;
5989 if (mode == InterpolationModeHighQuality)
5990 mode = InterpolationModeHighQualityBicubic;
5992 if (mode == graphics->interpolation)
5993 return Ok;
5995 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5997 GpStatus stat;
5999 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6000 EmfPlusRecordTypeSetInterpolationMode, mode);
6001 if (stat != Ok)
6002 return stat;
6005 graphics->interpolation = mode;
6007 return Ok;
6010 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6012 GpStatus stat;
6014 TRACE("(%p, %.2f)\n", graphics, scale);
6016 if(!graphics || (scale <= 0.0))
6017 return InvalidParameter;
6019 if(graphics->busy)
6020 return ObjectBusy;
6022 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6024 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6025 if (stat != Ok)
6026 return stat;
6029 graphics->scale = scale;
6031 return Ok;
6034 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6036 GpStatus stat;
6038 TRACE("(%p, %d)\n", graphics, unit);
6040 if(!graphics)
6041 return InvalidParameter;
6043 if(graphics->busy)
6044 return ObjectBusy;
6046 if(unit == UnitWorld)
6047 return InvalidParameter;
6049 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6051 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6052 if (stat != Ok)
6053 return stat;
6056 graphics->unit = unit;
6058 return Ok;
6061 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6062 mode)
6064 TRACE("(%p, %d)\n", graphics, mode);
6066 if(!graphics)
6067 return InvalidParameter;
6069 if(graphics->busy)
6070 return ObjectBusy;
6072 if(graphics->pixeloffset == mode)
6073 return Ok;
6075 if(graphics->image && graphics->image->type == ImageTypeMetafile)
6077 GpStatus stat;
6079 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6080 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6081 if(stat != Ok)
6082 return stat;
6085 graphics->pixeloffset = mode;
6087 return Ok;
6090 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6092 static int calls;
6094 TRACE("(%p,%i,%i)\n", graphics, x, y);
6096 if (!(calls++))
6097 FIXME("value is unused in rendering\n");
6099 if (!graphics)
6100 return InvalidParameter;
6102 graphics->origin_x = x;
6103 graphics->origin_y = y;
6105 return Ok;
6108 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6110 TRACE("(%p,%p,%p)\n", graphics, x, y);
6112 if (!graphics || !x || !y)
6113 return InvalidParameter;
6115 *x = graphics->origin_x;
6116 *y = graphics->origin_y;
6118 return Ok;
6121 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6123 TRACE("(%p, %d)\n", graphics, mode);
6125 if(!graphics)
6126 return InvalidParameter;
6128 if(graphics->busy)
6129 return ObjectBusy;
6131 if(graphics->smoothing == mode)
6132 return Ok;
6134 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6135 GpStatus stat;
6136 BOOL antialias = (mode != SmoothingModeDefault &&
6137 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6139 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6140 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6141 if(stat != Ok)
6142 return stat;
6145 graphics->smoothing = mode;
6147 return Ok;
6150 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6152 TRACE("(%p, %d)\n", graphics, contrast);
6154 if(!graphics)
6155 return InvalidParameter;
6157 graphics->textcontrast = contrast;
6159 return Ok;
6162 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6163 TextRenderingHint hint)
6165 TRACE("(%p, %d)\n", graphics, hint);
6167 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6168 return InvalidParameter;
6170 if(graphics->busy)
6171 return ObjectBusy;
6173 if(graphics->texthint == hint)
6174 return Ok;
6176 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6177 GpStatus stat;
6179 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6180 EmfPlusRecordTypeSetTextRenderingHint, hint);
6181 if(stat != Ok)
6182 return stat;
6185 graphics->texthint = hint;
6187 return Ok;
6190 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6192 GpStatus stat;
6194 TRACE("(%p, %p)\n", graphics, matrix);
6196 if(!graphics || !matrix)
6197 return InvalidParameter;
6199 if(graphics->busy)
6200 return ObjectBusy;
6202 TRACE("%f,%f,%f,%f,%f,%f\n",
6203 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6204 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6206 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6207 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6209 if (stat != Ok)
6210 return stat;
6213 graphics->worldtrans = *matrix;
6215 return Ok;
6218 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6219 REAL dy, GpMatrixOrder order)
6221 GpStatus stat;
6223 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6225 if(!graphics)
6226 return InvalidParameter;
6228 if(graphics->busy)
6229 return ObjectBusy;
6231 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6232 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6234 if (stat != Ok)
6235 return stat;
6238 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6241 /*****************************************************************************
6242 * GdipSetClipHrgn [GDIPLUS.@]
6244 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6246 GpRegion *region;
6247 GpStatus status;
6248 GpMatrix transform;
6250 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6252 if(!graphics)
6253 return InvalidParameter;
6255 if(graphics->busy)
6256 return ObjectBusy;
6258 /* hrgn is in gdi32 device units */
6259 status = GdipCreateRegionHrgn(hrgn, &region);
6261 if (status == Ok)
6263 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6265 if (status == Ok)
6266 status = GdipTransformRegion(region, &transform);
6268 if (status == Ok)
6269 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6271 GdipDeleteRegion(region);
6273 return status;
6276 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6278 GpStatus status;
6279 GpPath *clip_path;
6281 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6283 if(!graphics)
6284 return InvalidParameter;
6286 if(graphics->busy)
6287 return ObjectBusy;
6289 status = GdipClonePath(path, &clip_path);
6290 if (status == Ok)
6292 GpMatrix world_to_device;
6294 get_graphics_transform(graphics, CoordinateSpaceDevice,
6295 CoordinateSpaceWorld, &world_to_device);
6296 status = GdipTransformPath(clip_path, &world_to_device);
6297 if (status == Ok)
6298 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6300 GdipDeletePath(clip_path);
6302 return status;
6305 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6306 REAL width, REAL height,
6307 CombineMode mode)
6309 GpStatus status;
6310 GpRectF rect;
6311 GpRegion *region;
6313 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6315 if(!graphics)
6316 return InvalidParameter;
6318 if(graphics->busy)
6319 return ObjectBusy;
6321 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6323 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6324 if (status != Ok)
6325 return status;
6328 rect.X = x;
6329 rect.Y = y;
6330 rect.Width = width;
6331 rect.Height = height;
6332 status = GdipCreateRegionRect(&rect, &region);
6333 if (status == Ok)
6335 GpMatrix world_to_device;
6337 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6338 status = GdipTransformRegion(region, &world_to_device);
6339 if (status == Ok)
6340 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6342 GdipDeleteRegion(region);
6344 return status;
6347 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6348 INT width, INT height,
6349 CombineMode mode)
6351 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6353 if(!graphics)
6354 return InvalidParameter;
6356 if(graphics->busy)
6357 return ObjectBusy;
6359 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6362 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6363 CombineMode mode)
6365 GpStatus status;
6366 GpRegion *clip;
6368 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6370 if(!graphics || !region)
6371 return InvalidParameter;
6373 if(graphics->busy)
6374 return ObjectBusy;
6376 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6378 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6379 if (status != Ok)
6380 return status;
6383 status = GdipCloneRegion(region, &clip);
6384 if (status == Ok)
6386 GpMatrix world_to_device;
6388 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6389 status = GdipTransformRegion(clip, &world_to_device);
6390 if (status == Ok)
6391 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6393 GdipDeleteRegion(clip);
6395 return status;
6398 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6399 INT count)
6401 GpStatus status;
6402 GpPath* path;
6404 TRACE("(%p, %p, %d)\n", graphics, points, count);
6406 if(!graphics || !pen || count<=0)
6407 return InvalidParameter;
6409 if(graphics->busy)
6410 return ObjectBusy;
6412 status = GdipCreatePath(FillModeAlternate, &path);
6413 if (status != Ok) return status;
6415 status = GdipAddPathPolygon(path, points, count);
6416 if (status == Ok)
6417 status = GdipDrawPath(graphics, pen, path);
6419 GdipDeletePath(path);
6421 return status;
6424 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6425 INT count)
6427 GpStatus ret;
6428 GpPointF *ptf;
6429 INT i;
6431 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6433 if(count<=0) return InvalidParameter;
6434 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6436 for(i = 0;i < count; i++){
6437 ptf[i].X = (REAL)points[i].X;
6438 ptf[i].Y = (REAL)points[i].Y;
6441 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6442 heap_free(ptf);
6444 return ret;
6447 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6449 TRACE("(%p, %p)\n", graphics, dpi);
6451 if(!graphics || !dpi)
6452 return InvalidParameter;
6454 if(graphics->busy)
6455 return ObjectBusy;
6457 *dpi = graphics->xres;
6458 return Ok;
6461 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6463 TRACE("(%p, %p)\n", graphics, dpi);
6465 if(!graphics || !dpi)
6466 return InvalidParameter;
6468 if(graphics->busy)
6469 return ObjectBusy;
6471 *dpi = graphics->yres;
6472 return Ok;
6475 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6476 GpMatrixOrder order)
6478 GpMatrix m;
6479 GpStatus ret;
6481 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6483 if(!graphics || !matrix)
6484 return InvalidParameter;
6486 if(graphics->busy)
6487 return ObjectBusy;
6489 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6490 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6492 if (ret != Ok)
6493 return ret;
6496 m = graphics->worldtrans;
6498 ret = GdipMultiplyMatrix(&m, matrix, order);
6499 if(ret == Ok)
6500 graphics->worldtrans = m;
6502 return ret;
6505 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6506 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6508 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6510 GpStatus stat=Ok;
6512 TRACE("(%p, %p)\n", graphics, hdc);
6514 if(!graphics || !hdc)
6515 return InvalidParameter;
6517 if(graphics->busy)
6518 return ObjectBusy;
6520 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6522 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6524 else if (!graphics->hdc ||
6525 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6527 /* Create a fake HDC and fill it with a constant color. */
6528 HDC temp_hdc;
6529 HBITMAP hbitmap;
6530 GpRectF bounds;
6531 BITMAPINFOHEADER bmih;
6532 int i;
6534 stat = get_graphics_bounds(graphics, &bounds);
6535 if (stat != Ok)
6536 return stat;
6538 graphics->temp_hbitmap_width = bounds.Width;
6539 graphics->temp_hbitmap_height = bounds.Height;
6541 bmih.biSize = sizeof(bmih);
6542 bmih.biWidth = graphics->temp_hbitmap_width;
6543 bmih.biHeight = -graphics->temp_hbitmap_height;
6544 bmih.biPlanes = 1;
6545 bmih.biBitCount = 32;
6546 bmih.biCompression = BI_RGB;
6547 bmih.biSizeImage = 0;
6548 bmih.biXPelsPerMeter = 0;
6549 bmih.biYPelsPerMeter = 0;
6550 bmih.biClrUsed = 0;
6551 bmih.biClrImportant = 0;
6553 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6554 (void**)&graphics->temp_bits, NULL, 0);
6555 if (!hbitmap)
6556 return GenericError;
6558 temp_hdc = CreateCompatibleDC(0);
6559 if (!temp_hdc)
6561 DeleteObject(hbitmap);
6562 return GenericError;
6565 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6566 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6568 SelectObject(temp_hdc, hbitmap);
6570 graphics->temp_hbitmap = hbitmap;
6571 *hdc = graphics->temp_hdc = temp_hdc;
6573 else
6575 *hdc = graphics->hdc;
6578 if (stat == Ok)
6579 graphics->busy = TRUE;
6581 return stat;
6584 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6586 GpStatus stat=Ok;
6588 TRACE("(%p, %p)\n", graphics, hdc);
6590 if(!graphics || !hdc || !graphics->busy)
6591 return InvalidParameter;
6593 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6595 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6597 else if (graphics->temp_hdc == hdc)
6599 DWORD* pos;
6600 int i;
6602 /* Find the pixels that have changed, and mark them as opaque. */
6603 pos = (DWORD*)graphics->temp_bits;
6604 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6606 if (*pos != DC_BACKGROUND_KEY)
6608 *pos |= 0xff000000;
6610 pos++;
6613 /* Write the changed pixels to the real target. */
6614 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6615 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6616 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6618 /* Clean up. */
6619 DeleteDC(graphics->temp_hdc);
6620 DeleteObject(graphics->temp_hbitmap);
6621 graphics->temp_hdc = NULL;
6622 graphics->temp_hbitmap = NULL;
6624 else if (hdc != graphics->hdc)
6626 stat = InvalidParameter;
6629 if (stat == Ok)
6630 graphics->busy = FALSE;
6632 return stat;
6635 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6637 GpRegion *clip;
6638 GpStatus status;
6639 GpMatrix device_to_world;
6641 TRACE("(%p, %p)\n", graphics, region);
6643 if(!graphics || !region)
6644 return InvalidParameter;
6646 if(graphics->busy)
6647 return ObjectBusy;
6649 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6650 return status;
6652 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6653 status = GdipTransformRegion(clip, &device_to_world);
6654 if (status != Ok)
6656 GdipDeleteRegion(clip);
6657 return status;
6660 /* free everything except root node and header */
6661 delete_element(&region->node);
6662 memcpy(region, clip, sizeof(GpRegion));
6663 heap_free(clip);
6665 return Ok;
6668 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6670 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6672 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6673 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6674 SetMapMode(graphics->hdc, MM_TEXT);
6675 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6676 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6678 graphics->gdi_transform_acquire_count++;
6679 return Ok;
6682 GpStatus gdi_transform_release(GpGraphics *graphics)
6684 if (graphics->gdi_transform_acquire_count <= 0)
6686 ERR("called without matching gdi_transform_acquire\n");
6687 return GenericError;
6689 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6691 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6693 graphics->gdi_transform_acquire_count--;
6694 return Ok;
6697 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6698 GpCoordinateSpace src_space, GpMatrix *matrix)
6700 GpStatus stat = Ok;
6701 REAL scale_x, scale_y;
6703 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6705 if (dst_space != src_space)
6707 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6708 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6710 if(graphics->unit != UnitDisplay)
6712 scale_x *= graphics->scale;
6713 scale_y *= graphics->scale;
6716 if (dst_space < src_space)
6718 /* transform towards world space */
6719 switch ((int)src_space)
6721 case WineCoordinateSpaceGdiDevice:
6723 GpMatrix gdixform;
6724 gdixform = graphics->gdi_transform;
6725 stat = GdipInvertMatrix(&gdixform);
6726 if (stat != Ok)
6727 break;
6728 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6729 if (dst_space == CoordinateSpaceDevice)
6730 break;
6731 /* else fall-through */
6733 case CoordinateSpaceDevice:
6734 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6735 if (dst_space == CoordinateSpacePage)
6736 break;
6737 /* else fall-through */
6738 case CoordinateSpacePage:
6740 GpMatrix inverted_transform = graphics->worldtrans;
6741 stat = GdipInvertMatrix(&inverted_transform);
6742 if (stat == Ok)
6743 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6744 break;
6748 else
6750 /* transform towards device space */
6751 switch ((int)src_space)
6753 case CoordinateSpaceWorld:
6754 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6755 if (dst_space == CoordinateSpacePage)
6756 break;
6757 /* else fall-through */
6758 case CoordinateSpacePage:
6759 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6760 if (dst_space == CoordinateSpaceDevice)
6761 break;
6762 /* else fall-through */
6763 case CoordinateSpaceDevice:
6765 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6766 break;
6771 return stat;
6774 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6775 GpCoordinateSpace src_space, GpPointF *points, INT count)
6777 GpMatrix matrix;
6778 GpStatus stat;
6780 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6781 if (stat != Ok) return stat;
6783 return GdipTransformMatrixPoints(&matrix, points, count);
6786 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6787 GpCoordinateSpace src_space, GpPointF *points, INT count)
6789 if(!graphics || !points || count <= 0 ||
6790 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6791 src_space < 0 || src_space > CoordinateSpaceDevice)
6792 return InvalidParameter;
6794 if(graphics->busy)
6795 return ObjectBusy;
6797 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6799 if (src_space == dst_space) return Ok;
6801 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6804 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6805 GpCoordinateSpace src_space, GpPoint *points, INT count)
6807 GpPointF *pointsF;
6808 GpStatus ret;
6809 INT i;
6811 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6813 if(count <= 0)
6814 return InvalidParameter;
6816 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6817 if(!pointsF)
6818 return OutOfMemory;
6820 for(i = 0; i < count; i++){
6821 pointsF[i].X = (REAL)points[i].X;
6822 pointsF[i].Y = (REAL)points[i].Y;
6825 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6827 if(ret == Ok)
6828 for(i = 0; i < count; i++){
6829 points[i].X = gdip_round(pointsF[i].X);
6830 points[i].Y = gdip_round(pointsF[i].Y);
6832 heap_free(pointsF);
6834 return ret;
6837 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6839 static int calls;
6841 TRACE("\n");
6843 if (!calls++)
6844 FIXME("stub\n");
6846 return NULL;
6849 /*****************************************************************************
6850 * GdipTranslateClip [GDIPLUS.@]
6852 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6854 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6856 if(!graphics)
6857 return InvalidParameter;
6859 if(graphics->busy)
6860 return ObjectBusy;
6862 return GdipTranslateRegion(graphics->clip, dx, dy);
6865 /*****************************************************************************
6866 * GdipTranslateClipI [GDIPLUS.@]
6868 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6870 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6872 if(!graphics)
6873 return InvalidParameter;
6875 if(graphics->busy)
6876 return ObjectBusy;
6878 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6882 /*****************************************************************************
6883 * GdipMeasureDriverString [GDIPLUS.@]
6885 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6886 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6887 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6889 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6890 HFONT hfont;
6891 HDC hdc;
6892 REAL min_x, min_y, max_x, max_y, x, y;
6893 int i;
6894 TEXTMETRICW textmetric;
6895 const WORD *glyph_indices;
6896 WORD *dynamic_glyph_indices=NULL;
6897 REAL rel_width, rel_height, ascent, descent;
6898 GpPointF pt[3];
6900 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6902 if (!graphics || !text || !font || !positions || !boundingBox)
6903 return InvalidParameter;
6905 if (length == -1)
6906 length = strlenW(text);
6908 if (length == 0)
6910 boundingBox->X = 0.0;
6911 boundingBox->Y = 0.0;
6912 boundingBox->Width = 0.0;
6913 boundingBox->Height = 0.0;
6916 if (flags & unsupported_flags)
6917 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6919 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6921 hdc = CreateCompatibleDC(0);
6922 SelectObject(hdc, hfont);
6924 GetTextMetricsW(hdc, &textmetric);
6926 pt[0].X = 0.0;
6927 pt[0].Y = 0.0;
6928 pt[1].X = 1.0;
6929 pt[1].Y = 0.0;
6930 pt[2].X = 0.0;
6931 pt[2].Y = 1.0;
6932 if (matrix)
6934 GpMatrix xform = *matrix;
6935 GdipTransformMatrixPoints(&xform, pt, 3);
6937 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
6938 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6939 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6940 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6941 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6943 if (flags & DriverStringOptionsCmapLookup)
6945 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
6946 if (!glyph_indices)
6948 DeleteDC(hdc);
6949 DeleteObject(hfont);
6950 return OutOfMemory;
6953 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6955 else
6956 glyph_indices = text;
6958 min_x = max_x = x = positions[0].X;
6959 min_y = max_y = y = positions[0].Y;
6961 ascent = textmetric.tmAscent / rel_height;
6962 descent = textmetric.tmDescent / rel_height;
6964 for (i=0; i<length; i++)
6966 int char_width;
6967 ABC abc;
6969 if (!(flags & DriverStringOptionsRealizedAdvance))
6971 x = positions[i].X;
6972 y = positions[i].Y;
6975 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6976 char_width = abc.abcA + abc.abcB + abc.abcC;
6978 if (min_y > y - ascent) min_y = y - ascent;
6979 if (max_y < y + descent) max_y = y + descent;
6980 if (min_x > x) min_x = x;
6982 x += char_width / rel_width;
6984 if (max_x < x) max_x = x;
6987 heap_free(dynamic_glyph_indices);
6988 DeleteDC(hdc);
6989 DeleteObject(hfont);
6991 boundingBox->X = min_x;
6992 boundingBox->Y = min_y;
6993 boundingBox->Width = max_x - min_x;
6994 boundingBox->Height = max_y - min_y;
6996 return Ok;
6999 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7000 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7001 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7002 INT flags, GDIPCONST GpMatrix *matrix)
7004 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
7005 INT save_state;
7006 GpPointF pt;
7007 HFONT hfont;
7008 UINT eto_flags=0;
7009 GpStatus status;
7010 HRGN hrgn;
7012 if (flags & unsupported_flags)
7013 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7015 if (!(flags & DriverStringOptionsCmapLookup))
7016 eto_flags |= ETO_GLYPH_INDEX;
7018 save_state = SaveDC(graphics->hdc);
7019 SetBkMode(graphics->hdc, TRANSPARENT);
7020 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7022 status = get_clip_hrgn(graphics, &hrgn);
7024 if (status == Ok)
7026 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7027 DeleteObject(hrgn);
7030 pt = positions[0];
7031 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7033 get_font_hfont(graphics, font, format, &hfont, matrix);
7034 SelectObject(graphics->hdc, hfont);
7036 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7038 gdi_transform_acquire(graphics);
7040 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
7042 gdi_transform_release(graphics);
7044 RestoreDC(graphics->hdc, save_state);
7046 DeleteObject(hfont);
7048 return Ok;
7051 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7052 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7053 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7054 INT flags, GDIPCONST GpMatrix *matrix)
7056 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7057 GpStatus stat;
7058 PointF *real_positions, real_position;
7059 POINT *pti;
7060 HFONT hfont;
7061 HDC hdc;
7062 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7063 DWORD max_glyphsize=0;
7064 GLYPHMETRICS glyphmetrics;
7065 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7066 BYTE *glyph_mask;
7067 BYTE *text_mask;
7068 int text_mask_stride;
7069 BYTE *pixel_data;
7070 int pixel_data_stride;
7071 GpRect pixel_area;
7072 UINT ggo_flags = GGO_GRAY8_BITMAP;
7074 if (length <= 0)
7075 return Ok;
7077 if (!(flags & DriverStringOptionsCmapLookup))
7078 ggo_flags |= GGO_GLYPH_INDEX;
7080 if (flags & unsupported_flags)
7081 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7083 pti = heap_alloc_zero(sizeof(POINT) * length);
7084 if (!pti)
7085 return OutOfMemory;
7087 if (flags & DriverStringOptionsRealizedAdvance)
7089 real_position = positions[0];
7091 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7092 round_points(pti, &real_position, 1);
7094 else
7096 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7097 if (!real_positions)
7099 heap_free(pti);
7100 return OutOfMemory;
7103 memcpy(real_positions, positions, sizeof(PointF) * length);
7105 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7106 round_points(pti, real_positions, length);
7108 heap_free(real_positions);
7111 get_font_hfont(graphics, font, format, &hfont, matrix);
7113 hdc = CreateCompatibleDC(0);
7114 SelectObject(hdc, hfont);
7116 /* Get the boundaries of the text to be drawn */
7117 for (i=0; i<length; i++)
7119 DWORD glyphsize;
7120 int left, top, right, bottom;
7122 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7123 &glyphmetrics, 0, NULL, &identity);
7125 if (glyphsize == GDI_ERROR)
7127 ERR("GetGlyphOutlineW failed\n");
7128 heap_free(pti);
7129 DeleteDC(hdc);
7130 DeleteObject(hfont);
7131 return GenericError;
7134 if (glyphsize > max_glyphsize)
7135 max_glyphsize = glyphsize;
7137 if (glyphsize != 0)
7139 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7140 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7141 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7142 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7144 if (left < min_x) min_x = left;
7145 if (top < min_y) min_y = top;
7146 if (right > max_x) max_x = right;
7147 if (bottom > max_y) max_y = bottom;
7150 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7152 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7153 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7157 if (max_glyphsize == 0)
7158 /* Nothing to draw. */
7159 return Ok;
7161 glyph_mask = heap_alloc_zero(max_glyphsize);
7162 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7163 text_mask_stride = max_x - min_x;
7165 if (!(glyph_mask && text_mask))
7167 heap_free(glyph_mask);
7168 heap_free(text_mask);
7169 heap_free(pti);
7170 DeleteDC(hdc);
7171 DeleteObject(hfont);
7172 return OutOfMemory;
7175 /* Generate a mask for the text */
7176 for (i=0; i<length; i++)
7178 DWORD ret;
7179 int left, top, stride;
7181 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7182 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7184 if (ret == GDI_ERROR || ret == 0)
7185 continue; /* empty glyph */
7187 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7188 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7189 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7191 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7193 BYTE *glyph_val = glyph_mask + y * stride;
7194 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7195 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7197 *text_val = min(64, *text_val + *glyph_val);
7198 glyph_val++;
7199 text_val++;
7204 heap_free(pti);
7205 DeleteDC(hdc);
7206 DeleteObject(hfont);
7207 heap_free(glyph_mask);
7209 /* get the brush data */
7210 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7211 if (!pixel_data)
7213 heap_free(text_mask);
7214 return OutOfMemory;
7217 pixel_area.X = min_x;
7218 pixel_area.Y = min_y;
7219 pixel_area.Width = max_x - min_x;
7220 pixel_area.Height = max_y - min_y;
7221 pixel_data_stride = pixel_area.Width * 4;
7223 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7224 if (stat != Ok)
7226 heap_free(text_mask);
7227 heap_free(pixel_data);
7228 return stat;
7231 /* multiply the brush data by the mask */
7232 for (y=0; y<pixel_area.Height; y++)
7234 BYTE *text_val = text_mask + text_mask_stride * y;
7235 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7236 for (x=0; x<pixel_area.Width; x++)
7238 *pixel_val = (*pixel_val) * (*text_val) / 64;
7239 text_val++;
7240 pixel_val+=4;
7244 heap_free(text_mask);
7246 gdi_transform_acquire(graphics);
7248 /* draw the result */
7249 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7250 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7252 gdi_transform_release(graphics);
7254 heap_free(pixel_data);
7256 return stat;
7259 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7260 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7261 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7262 INT flags, GDIPCONST GpMatrix *matrix)
7264 GpStatus stat = NotImplemented;
7266 if (length == -1)
7267 length = strlenW(text);
7269 if (graphics->hdc && !graphics->alpha_hdc &&
7270 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
7271 brush->bt == BrushTypeSolidColor &&
7272 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7273 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7274 brush, positions, flags, matrix);
7275 if (stat == NotImplemented)
7276 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7277 brush, positions, flags, matrix);
7278 return stat;
7281 /*****************************************************************************
7282 * GdipDrawDriverString [GDIPLUS.@]
7284 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7285 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7286 GDIPCONST PointF *positions, INT flags,
7287 GDIPCONST GpMatrix *matrix )
7289 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7291 if (!graphics || !text || !font || !brush || !positions)
7292 return InvalidParameter;
7294 return draw_driver_string(graphics, text, length, font, NULL,
7295 brush, positions, flags, matrix);
7298 /*****************************************************************************
7299 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7301 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7303 GpStatus stat;
7304 GpRegion* rgn;
7306 TRACE("(%p, %p)\n", graphics, res);
7308 if((stat = GdipCreateRegion(&rgn)) != Ok)
7309 return stat;
7311 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7312 goto cleanup;
7314 stat = GdipIsEmptyRegion(rgn, graphics, res);
7316 cleanup:
7317 GdipDeleteRegion(rgn);
7318 return stat;
7321 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7323 static int calls;
7325 TRACE("(%p) stub\n", graphics);
7327 if(!(calls++))
7328 FIXME("not implemented\n");
7330 return NotImplemented;
7333 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7335 TRACE("(%p, %p)\n", graphics, pabort);
7337 if (!graphics)
7338 return InvalidParameter;
7340 if (pabort)
7341 FIXME("Abort callback is not supported.\n");
7343 return Ok;