gdiplus: Fix the number of transformed points for round caps.
[wine.git] / dlls / gdiplus / graphics.c
bloba7e5c8015c5461e80659b45f9f16c46f5f3433cc
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;
335 BOOL identity;
337 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, &transform);
339 if (stat == Ok)
340 stat = GdipIsMatrixIdentity(&transform, &identity);
342 if (stat == Ok)
343 stat = GdipCloneRegion(graphics->clip, &rgn);
345 if (stat == Ok)
347 if (!identity)
348 stat = GdipTransformRegion(rgn, &transform);
350 if (stat == Ok)
351 stat = GdipGetRegionHRgn(rgn, NULL, hrgn);
353 GdipDeleteRegion(rgn);
356 if (stat == Ok && graphics->gdi_clip)
358 if (*hrgn)
359 CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
360 else
362 *hrgn = CreateRectRgn(0,0,0,0);
363 CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
367 return stat;
370 /* Draw ARGB data to the given graphics object */
371 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
372 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
374 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
375 INT x, y;
377 for (y=0; y<src_height; y++)
379 for (x=0; x<src_width; x++)
381 ARGB dst_color, src_color;
382 src_color = ((ARGB*)(src + src_stride * y))[x];
384 if (!(src_color & 0xff000000))
385 continue;
387 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
388 if (fmt & PixelFormatPAlpha)
389 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
390 else
391 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
395 return Ok;
398 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
399 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
401 HDC hdc;
402 HBITMAP hbitmap;
403 BITMAPINFOHEADER bih;
404 BYTE *temp_bits;
406 hdc = CreateCompatibleDC(0);
408 bih.biSize = sizeof(BITMAPINFOHEADER);
409 bih.biWidth = src_width;
410 bih.biHeight = -src_height;
411 bih.biPlanes = 1;
412 bih.biBitCount = 32;
413 bih.biCompression = BI_RGB;
414 bih.biSizeImage = 0;
415 bih.biXPelsPerMeter = 0;
416 bih.biYPelsPerMeter = 0;
417 bih.biClrUsed = 0;
418 bih.biClrImportant = 0;
420 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
421 (void**)&temp_bits, NULL, 0);
423 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
424 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
425 fmt & PixelFormatPAlpha)
426 memcpy(temp_bits, src, src_width * src_height * 4);
427 else
428 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
429 4 * src_width, src, src_stride);
431 SelectObject(hdc, hbitmap);
432 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
433 hdc, 0, 0, src_width, src_height);
434 DeleteDC(hdc);
435 DeleteObject(hbitmap);
437 return Ok;
440 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
441 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
443 GpStatus stat=Ok;
445 if (graphics->image && graphics->image->type == ImageTypeBitmap)
447 DWORD i;
448 int size;
449 RGNDATA *rgndata;
450 RECT *rects;
451 HRGN hrgn, visible_rgn;
453 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
454 if (!hrgn)
455 return OutOfMemory;
457 stat = get_clip_hrgn(graphics, &visible_rgn);
458 if (stat != Ok)
460 DeleteObject(hrgn);
461 return stat;
464 if (visible_rgn)
466 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
467 DeleteObject(visible_rgn);
470 if (hregion)
471 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
473 size = GetRegionData(hrgn, 0, NULL);
475 rgndata = heap_alloc_zero(size);
476 if (!rgndata)
478 DeleteObject(hrgn);
479 return OutOfMemory;
482 GetRegionData(hrgn, size, rgndata);
484 rects = (RECT*)rgndata->Buffer;
486 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
488 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
489 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
490 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
491 src_stride, fmt);
494 heap_free(rgndata);
496 DeleteObject(hrgn);
498 return stat;
500 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
502 ERR("This should not be used for metafiles; fix caller\n");
503 return NotImplemented;
505 else
507 HRGN hrgn;
508 int save;
510 stat = get_clip_hrgn(graphics, &hrgn);
512 if (stat != Ok)
513 return stat;
515 save = SaveDC(graphics->hdc);
517 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
519 if (hregion)
520 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
522 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
523 src_height, src_stride, fmt);
525 RestoreDC(graphics->hdc, save);
527 DeleteObject(hrgn);
529 return stat;
533 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
534 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
536 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
539 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
541 INT start_a, end_a, final_a;
542 INT pos;
544 pos = gdip_round(position * 0xff);
546 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
547 end_a = ((end >> 24) & 0xff) * pos;
549 final_a = start_a + end_a;
551 if (final_a < 0xff) return 0;
553 return (final_a / 0xff) << 24 |
554 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
555 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
556 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
559 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
561 REAL blendfac;
563 /* clamp to between 0.0 and 1.0, using the wrap mode */
564 position = (position - brush->rect.X) / brush->rect.Width;
565 if (brush->wrap == WrapModeTile)
567 position = fmodf(position, 1.0f);
568 if (position < 0.0f) position += 1.0f;
570 else /* WrapModeFlip* */
572 position = fmodf(position, 2.0f);
573 if (position < 0.0f) position += 2.0f;
574 if (position > 1.0f) position = 2.0f - position;
577 if (brush->blendcount == 1)
578 blendfac = position;
579 else
581 int i=1;
582 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
583 REAL range;
585 /* locate the blend positions surrounding this position */
586 while (position > brush->blendpos[i])
587 i++;
589 /* interpolate between the blend positions */
590 left_blendpos = brush->blendpos[i-1];
591 left_blendfac = brush->blendfac[i-1];
592 right_blendpos = brush->blendpos[i];
593 right_blendfac = brush->blendfac[i];
594 range = right_blendpos - left_blendpos;
595 blendfac = (left_blendfac * (right_blendpos - position) +
596 right_blendfac * (position - left_blendpos)) / range;
599 if (brush->pblendcount == 0)
600 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
601 else
603 int i=1;
604 ARGB left_blendcolor, right_blendcolor;
605 REAL left_blendpos, right_blendpos;
607 /* locate the blend colors surrounding this position */
608 while (blendfac > brush->pblendpos[i])
609 i++;
611 /* interpolate between the blend colors */
612 left_blendpos = brush->pblendpos[i-1];
613 left_blendcolor = brush->pblendcolor[i-1];
614 right_blendpos = brush->pblendpos[i];
615 right_blendcolor = brush->pblendcolor[i];
616 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
617 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
621 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
623 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
624 BOOL identity = TRUE;
625 int i, j;
627 for (i=0; i<4; i++)
628 for (j=0; j<5; j++)
630 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
631 identity = FALSE;
632 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
635 return identity;
638 static ARGB transform_color(ARGB color, int matrix[5][5])
640 int val[5], res[4];
641 int i, j;
642 unsigned char a, r, g, b;
644 val[0] = ((color >> 16) & 0xff); /* red */
645 val[1] = ((color >> 8) & 0xff); /* green */
646 val[2] = (color & 0xff); /* blue */
647 val[3] = ((color >> 24) & 0xff); /* alpha */
648 val[4] = 255; /* translation */
650 for (i=0; i<4; i++)
652 res[i] = 0;
654 for (j=0; j<5; j++)
655 res[i] += matrix[j][i] * val[j];
658 a = min(max(res[3] / 256, 0), 255);
659 r = min(max(res[0] / 256, 0), 255);
660 g = min(max(res[1] / 256, 0), 255);
661 b = min(max(res[2] / 256, 0), 255);
663 return (a << 24) | (r << 16) | (g << 8) | b;
666 static BOOL color_is_gray(ARGB color)
668 unsigned char r, g, b;
670 r = (color >> 16) & 0xff;
671 g = (color >> 8) & 0xff;
672 b = color & 0xff;
674 return (r == g) && (g == b);
677 /* returns preferred pixel format for the applied attributes */
678 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
679 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
681 UINT x, y;
682 INT i;
684 if ((attributes->noop[type] == IMAGEATTR_NOOP_UNDEFINED &&
685 attributes->noop[ColorAdjustTypeDefault] == IMAGEATTR_NOOP_SET) ||
686 (attributes->noop[type] == IMAGEATTR_NOOP_SET))
687 return fmt;
689 if (attributes->colorkeys[type].enabled ||
690 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
692 const struct color_key *key;
693 BYTE min_blue, min_green, min_red;
694 BYTE max_blue, max_green, max_red;
696 if (!data || fmt != PixelFormat32bppARGB)
697 return PixelFormat32bppARGB;
699 if (attributes->colorkeys[type].enabled)
700 key = &attributes->colorkeys[type];
701 else
702 key = &attributes->colorkeys[ColorAdjustTypeDefault];
704 min_blue = key->low&0xff;
705 min_green = (key->low>>8)&0xff;
706 min_red = (key->low>>16)&0xff;
708 max_blue = key->high&0xff;
709 max_green = (key->high>>8)&0xff;
710 max_red = (key->high>>16)&0xff;
712 for (x=0; x<width; x++)
713 for (y=0; y<height; y++)
715 ARGB *src_color;
716 BYTE blue, green, red;
717 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
718 blue = *src_color&0xff;
719 green = (*src_color>>8)&0xff;
720 red = (*src_color>>16)&0xff;
721 if (blue >= min_blue && green >= min_green && red >= min_red &&
722 blue <= max_blue && green <= max_green && red <= max_red)
723 *src_color = 0x00000000;
727 if (attributes->colorremaptables[type].enabled ||
728 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
730 const struct color_remap_table *table;
732 if (!data || fmt != PixelFormat32bppARGB)
733 return PixelFormat32bppARGB;
735 if (attributes->colorremaptables[type].enabled)
736 table = &attributes->colorremaptables[type];
737 else
738 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
740 for (x=0; x<width; x++)
741 for (y=0; y<height; y++)
743 ARGB *src_color;
744 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
745 for (i=0; i<table->mapsize; i++)
747 if (*src_color == table->colormap[i].oldColor.Argb)
749 *src_color = table->colormap[i].newColor.Argb;
750 break;
756 if (attributes->colormatrices[type].enabled ||
757 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
759 const struct color_matrix *colormatrices;
760 int color_matrix[5][5];
761 int gray_matrix[5][5];
762 BOOL identity;
764 if (!data || fmt != PixelFormat32bppARGB)
765 return PixelFormat32bppARGB;
767 if (attributes->colormatrices[type].enabled)
768 colormatrices = &attributes->colormatrices[type];
769 else
770 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
772 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
774 if (colormatrices->flags == ColorMatrixFlagsAltGray)
775 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
777 if (!identity)
779 for (x=0; x<width; x++)
781 for (y=0; y<height; y++)
783 ARGB *src_color;
784 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
786 if (colormatrices->flags == ColorMatrixFlagsDefault ||
787 !color_is_gray(*src_color))
789 *src_color = transform_color(*src_color, color_matrix);
791 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
793 *src_color = transform_color(*src_color, gray_matrix);
800 if (attributes->gamma_enabled[type] ||
801 attributes->gamma_enabled[ColorAdjustTypeDefault])
803 REAL gamma;
805 if (!data || fmt != PixelFormat32bppARGB)
806 return PixelFormat32bppARGB;
808 if (attributes->gamma_enabled[type])
809 gamma = attributes->gamma[type];
810 else
811 gamma = attributes->gamma[ColorAdjustTypeDefault];
813 for (x=0; x<width; x++)
814 for (y=0; y<height; y++)
816 ARGB *src_color;
817 BYTE blue, green, red;
818 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
820 blue = *src_color&0xff;
821 green = (*src_color>>8)&0xff;
822 red = (*src_color>>16)&0xff;
824 /* FIXME: We should probably use a table for this. */
825 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
826 green = floorf(powf(green / 255.0, gamma) * 255.0);
827 red = floorf(powf(red / 255.0, gamma) * 255.0);
829 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
833 return fmt;
836 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
837 * bitmap that contains all the pixels we may need to draw it. */
838 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
839 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
840 GpRect *rect)
842 INT left, top, right, bottom;
844 switch (interpolation)
846 case InterpolationModeHighQualityBilinear:
847 case InterpolationModeHighQualityBicubic:
848 /* FIXME: Include a greater range for the prefilter? */
849 case InterpolationModeBicubic:
850 case InterpolationModeBilinear:
851 left = (INT)(floorf(srcx));
852 top = (INT)(floorf(srcy));
853 right = (INT)(ceilf(srcx+srcwidth));
854 bottom = (INT)(ceilf(srcy+srcheight));
855 break;
856 case InterpolationModeNearestNeighbor:
857 default:
858 left = gdip_round(srcx);
859 top = gdip_round(srcy);
860 right = gdip_round(srcx+srcwidth);
861 bottom = gdip_round(srcy+srcheight);
862 break;
865 if (wrap == WrapModeClamp)
867 if (left < 0)
868 left = 0;
869 if (top < 0)
870 top = 0;
871 if (right >= bitmap->width)
872 right = bitmap->width-1;
873 if (bottom >= bitmap->height)
874 bottom = bitmap->height-1;
875 if (bottom < top || right < left)
876 /* entirely outside image, just sample a pixel so we don't have to
877 * special-case this later */
878 left = top = right = bottom = 0;
880 else
882 /* In some cases we can make the rectangle smaller here, but the logic
883 * is hard to get right, and tiling suggests we're likely to use the
884 * entire source image. */
885 if (left < 0 || right >= bitmap->width)
887 left = 0;
888 right = bitmap->width-1;
891 if (top < 0 || bottom >= bitmap->height)
893 top = 0;
894 bottom = bitmap->height-1;
898 rect->X = left;
899 rect->Y = top;
900 rect->Width = right - left + 1;
901 rect->Height = bottom - top + 1;
904 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
905 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
907 if (attributes->wrap == WrapModeClamp)
909 if (x < 0 || y < 0 || x >= width || y >= height)
910 return attributes->outside_color;
912 else
914 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
915 if (x < 0)
916 x = width*2 + x % (width * 2);
917 if (y < 0)
918 y = height*2 + y % (height * 2);
920 if (attributes->wrap & WrapModeTileFlipX)
922 if ((x / width) % 2 == 0)
923 x = x % width;
924 else
925 x = width - 1 - x % width;
927 else
928 x = x % width;
930 if (attributes->wrap & WrapModeTileFlipY)
932 if ((y / height) % 2 == 0)
933 y = y % height;
934 else
935 y = height - 1 - y % height;
937 else
938 y = y % height;
941 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
943 ERR("out of range pixel requested\n");
944 return 0xffcd0084;
947 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
950 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
951 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
952 InterpolationMode interpolation, PixelOffsetMode offset_mode)
954 static int fixme;
956 switch (interpolation)
958 default:
959 if (!fixme++)
960 FIXME("Unimplemented interpolation %i\n", interpolation);
961 /* fall-through */
962 case InterpolationModeBilinear:
964 REAL leftxf, topyf;
965 INT leftx, rightx, topy, bottomy;
966 ARGB topleft, topright, bottomleft, bottomright;
967 ARGB top, bottom;
968 float x_offset;
970 leftxf = floorf(point->X);
971 leftx = (INT)leftxf;
972 rightx = (INT)ceilf(point->X);
973 topyf = floorf(point->Y);
974 topy = (INT)topyf;
975 bottomy = (INT)ceilf(point->Y);
977 if (leftx == rightx && topy == bottomy)
978 return sample_bitmap_pixel(src_rect, bits, width, height,
979 leftx, topy, attributes);
981 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
982 leftx, topy, attributes);
983 topright = sample_bitmap_pixel(src_rect, bits, width, height,
984 rightx, topy, attributes);
985 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
986 leftx, bottomy, attributes);
987 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
988 rightx, bottomy, attributes);
990 x_offset = point->X - leftxf;
991 top = blend_colors(topleft, topright, x_offset);
992 bottom = blend_colors(bottomleft, bottomright, x_offset);
994 return blend_colors(top, bottom, point->Y - topyf);
996 case InterpolationModeNearestNeighbor:
998 FLOAT pixel_offset;
999 switch (offset_mode)
1001 default:
1002 case PixelOffsetModeNone:
1003 case PixelOffsetModeHighSpeed:
1004 pixel_offset = 0.5;
1005 break;
1007 case PixelOffsetModeHalf:
1008 case PixelOffsetModeHighQuality:
1009 pixel_offset = 0.0;
1010 break;
1012 return sample_bitmap_pixel(src_rect, bits, width, height,
1013 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1019 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1021 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1024 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1025 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1027 switch (brush->bt)
1029 case BrushTypeSolidColor:
1031 if (is_fill)
1032 return TRUE;
1033 else
1035 /* cannot draw semi-transparent colors */
1036 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1039 case BrushTypeHatchFill:
1041 GpHatch *hatch = (GpHatch*)brush;
1042 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1043 ((hatch->backcol & 0xff000000) == 0xff000000);
1045 case BrushTypeLinearGradient:
1046 case BrushTypeTextureFill:
1047 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1048 default:
1049 return FALSE;
1053 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1055 switch (brush->bt)
1057 case BrushTypeSolidColor:
1059 GpSolidFill *fill = (GpSolidFill*)brush;
1060 HBITMAP bmp = ARGB2BMP(fill->color);
1062 if (bmp)
1064 RECT rc;
1065 /* partially transparent fill */
1067 SelectClipPath(graphics->hdc, RGN_AND);
1068 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1070 HDC hdc = CreateCompatibleDC(NULL);
1072 if (!hdc) break;
1074 SelectObject(hdc, bmp);
1075 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1076 hdc, 0, 0, 1, 1);
1077 DeleteDC(hdc);
1080 DeleteObject(bmp);
1081 break;
1083 /* else fall through */
1085 default:
1087 HBRUSH gdibrush, old_brush;
1089 gdibrush = create_gdi_brush(brush);
1090 if (!gdibrush) return;
1092 old_brush = SelectObject(graphics->hdc, gdibrush);
1093 FillPath(graphics->hdc);
1094 SelectObject(graphics->hdc, old_brush);
1095 DeleteObject(gdibrush);
1096 break;
1101 static BOOL brush_can_fill_pixels(GpBrush *brush)
1103 switch (brush->bt)
1105 case BrushTypeSolidColor:
1106 case BrushTypeHatchFill:
1107 case BrushTypeLinearGradient:
1108 case BrushTypeTextureFill:
1109 case BrushTypePathGradient:
1110 return TRUE;
1111 default:
1112 return FALSE;
1116 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1117 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1119 switch (brush->bt)
1121 case BrushTypeSolidColor:
1123 int x, y;
1124 GpSolidFill *fill = (GpSolidFill*)brush;
1125 for (x=0; x<fill_area->Width; x++)
1126 for (y=0; y<fill_area->Height; y++)
1127 argb_pixels[x + y*cdwStride] = fill->color;
1128 return Ok;
1130 case BrushTypeHatchFill:
1132 int x, y;
1133 GpHatch *fill = (GpHatch*)brush;
1134 const char *hatch_data;
1136 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1137 return NotImplemented;
1139 for (x=0; x<fill_area->Width; x++)
1140 for (y=0; y<fill_area->Height; y++)
1142 int hx, hy;
1144 /* FIXME: Account for the rendering origin */
1145 hx = (x + fill_area->X) % 8;
1146 hy = (y + fill_area->Y) % 8;
1148 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1149 argb_pixels[x + y*cdwStride] = fill->forecol;
1150 else
1151 argb_pixels[x + y*cdwStride] = fill->backcol;
1154 return Ok;
1156 case BrushTypeLinearGradient:
1158 GpLineGradient *fill = (GpLineGradient*)brush;
1159 GpPointF draw_points[3];
1160 GpStatus stat;
1161 int x, y;
1163 draw_points[0].X = fill_area->X;
1164 draw_points[0].Y = fill_area->Y;
1165 draw_points[1].X = fill_area->X+1;
1166 draw_points[1].Y = fill_area->Y;
1167 draw_points[2].X = fill_area->X;
1168 draw_points[2].Y = fill_area->Y+1;
1170 /* Transform the points to a co-ordinate space where X is the point's
1171 * position in the gradient, 0.0 being the start point and 1.0 the
1172 * end point. */
1173 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1174 WineCoordinateSpaceGdiDevice, draw_points, 3);
1176 if (stat == Ok)
1178 GpMatrix world_to_gradient = fill->transform;
1180 stat = GdipInvertMatrix(&world_to_gradient);
1181 if (stat == Ok)
1182 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1185 if (stat == Ok)
1187 REAL x_delta = draw_points[1].X - draw_points[0].X;
1188 REAL y_delta = draw_points[2].X - draw_points[0].X;
1190 for (y=0; y<fill_area->Height; y++)
1192 for (x=0; x<fill_area->Width; x++)
1194 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1196 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1201 return stat;
1203 case BrushTypeTextureFill:
1205 GpTexture *fill = (GpTexture*)brush;
1206 GpPointF draw_points[3];
1207 GpStatus stat;
1208 int x, y;
1209 GpBitmap *bitmap;
1210 int src_stride;
1211 GpRect src_area;
1213 if (fill->image->type != ImageTypeBitmap)
1215 FIXME("metafile texture brushes not implemented\n");
1216 return NotImplemented;
1219 bitmap = (GpBitmap*)fill->image;
1220 src_stride = sizeof(ARGB) * bitmap->width;
1222 src_area.X = src_area.Y = 0;
1223 src_area.Width = bitmap->width;
1224 src_area.Height = bitmap->height;
1226 draw_points[0].X = fill_area->X;
1227 draw_points[0].Y = fill_area->Y;
1228 draw_points[1].X = fill_area->X+1;
1229 draw_points[1].Y = fill_area->Y;
1230 draw_points[2].X = fill_area->X;
1231 draw_points[2].Y = fill_area->Y+1;
1233 /* Transform the points to the co-ordinate space of the bitmap. */
1234 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1235 WineCoordinateSpaceGdiDevice, draw_points, 3);
1237 if (stat == Ok)
1239 GpMatrix world_to_texture = fill->transform;
1241 stat = GdipInvertMatrix(&world_to_texture);
1242 if (stat == Ok)
1243 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1246 if (stat == Ok && !fill->bitmap_bits)
1248 BitmapData lockeddata;
1250 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1251 if (!fill->bitmap_bits)
1252 stat = OutOfMemory;
1254 if (stat == Ok)
1256 lockeddata.Width = bitmap->width;
1257 lockeddata.Height = bitmap->height;
1258 lockeddata.Stride = src_stride;
1259 lockeddata.PixelFormat = PixelFormat32bppARGB;
1260 lockeddata.Scan0 = fill->bitmap_bits;
1262 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1263 PixelFormat32bppARGB, &lockeddata);
1266 if (stat == Ok)
1267 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1269 if (stat == Ok)
1270 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1271 bitmap->width, bitmap->height,
1272 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1274 if (stat != Ok)
1276 heap_free(fill->bitmap_bits);
1277 fill->bitmap_bits = NULL;
1281 if (stat == Ok)
1283 REAL x_dx = draw_points[1].X - draw_points[0].X;
1284 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1285 REAL y_dx = draw_points[2].X - draw_points[0].X;
1286 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1288 for (y=0; y<fill_area->Height; y++)
1290 for (x=0; x<fill_area->Width; x++)
1292 GpPointF point;
1293 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1294 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1296 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1297 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1298 &point, fill->imageattributes, graphics->interpolation,
1299 graphics->pixeloffset);
1304 return stat;
1306 case BrushTypePathGradient:
1308 GpPathGradient *fill = (GpPathGradient*)brush;
1309 GpPath *flat_path;
1310 GpMatrix world_to_device;
1311 GpStatus stat;
1312 int i, figure_start=0;
1313 GpPointF start_point, end_point, center_point;
1314 BYTE type;
1315 REAL min_yf, max_yf, line1_xf, line2_xf;
1316 INT min_y, max_y, min_x, max_x;
1317 INT x, y;
1318 ARGB outer_color;
1319 static BOOL transform_fixme_once;
1321 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1323 static int once;
1324 if (!once++)
1325 FIXME("path gradient focus not implemented\n");
1328 if (fill->gamma)
1330 static int once;
1331 if (!once++)
1332 FIXME("path gradient gamma correction not implemented\n");
1335 if (fill->blendcount)
1337 static int once;
1338 if (!once++)
1339 FIXME("path gradient blend not implemented\n");
1342 if (fill->pblendcount)
1344 static int once;
1345 if (!once++)
1346 FIXME("path gradient preset blend not implemented\n");
1349 if (!transform_fixme_once)
1351 BOOL is_identity=TRUE;
1352 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1353 if (!is_identity)
1355 FIXME("path gradient transform not implemented\n");
1356 transform_fixme_once = TRUE;
1360 stat = GdipClonePath(fill->path, &flat_path);
1362 if (stat != Ok)
1363 return stat;
1365 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1366 CoordinateSpaceWorld, &world_to_device);
1367 if (stat == Ok)
1369 stat = GdipTransformPath(flat_path, &world_to_device);
1371 if (stat == Ok)
1373 center_point = fill->center;
1374 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1377 if (stat == Ok)
1378 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1381 if (stat != Ok)
1383 GdipDeletePath(flat_path);
1384 return stat;
1387 for (i=0; i<flat_path->pathdata.Count; i++)
1389 int start_center_line=0, end_center_line=0;
1390 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1391 REAL center_distance;
1392 ARGB start_color, end_color;
1393 REAL dy, dx;
1395 type = flat_path->pathdata.Types[i];
1397 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1398 figure_start = i;
1400 start_point = flat_path->pathdata.Points[i];
1402 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1404 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1406 end_point = flat_path->pathdata.Points[figure_start];
1407 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1409 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1411 end_point = flat_path->pathdata.Points[i+1];
1412 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1414 else
1415 continue;
1417 outer_color = start_color;
1419 min_yf = center_point.Y;
1420 if (min_yf > start_point.Y) min_yf = start_point.Y;
1421 if (min_yf > end_point.Y) min_yf = end_point.Y;
1423 if (min_yf < fill_area->Y)
1424 min_y = fill_area->Y;
1425 else
1426 min_y = (INT)ceil(min_yf);
1428 max_yf = center_point.Y;
1429 if (max_yf < start_point.Y) max_yf = start_point.Y;
1430 if (max_yf < end_point.Y) max_yf = end_point.Y;
1432 if (max_yf > fill_area->Y + fill_area->Height)
1433 max_y = fill_area->Y + fill_area->Height;
1434 else
1435 max_y = (INT)ceil(max_yf);
1437 dy = end_point.Y - start_point.Y;
1438 dx = end_point.X - start_point.X;
1440 /* This is proportional to the distance from start-end line to center point. */
1441 center_distance = dy * (start_point.X - center_point.X) +
1442 dx * (center_point.Y - start_point.Y);
1444 for (y=min_y; y<max_y; y++)
1446 REAL yf = (REAL)y;
1448 if (!seen_start && yf >= start_point.Y)
1450 seen_start = TRUE;
1451 start_center_line ^= 1;
1453 if (!seen_end && yf >= end_point.Y)
1455 seen_end = TRUE;
1456 end_center_line ^= 1;
1458 if (!seen_center && yf >= center_point.Y)
1460 seen_center = TRUE;
1461 start_center_line ^= 1;
1462 end_center_line ^= 1;
1465 if (start_center_line)
1466 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1467 else
1468 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1470 if (end_center_line)
1471 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1472 else
1473 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1475 if (line1_xf < line2_xf)
1477 min_x = (INT)ceil(line1_xf);
1478 max_x = (INT)ceil(line2_xf);
1480 else
1482 min_x = (INT)ceil(line2_xf);
1483 max_x = (INT)ceil(line1_xf);
1486 if (min_x < fill_area->X)
1487 min_x = fill_area->X;
1488 if (max_x > fill_area->X + fill_area->Width)
1489 max_x = fill_area->X + fill_area->Width;
1491 for (x=min_x; x<max_x; x++)
1493 REAL xf = (REAL)x;
1494 REAL distance;
1496 if (start_color != end_color)
1498 REAL blend_amount, pdy, pdx;
1499 pdy = yf - center_point.Y;
1500 pdx = xf - center_point.X;
1502 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1504 /* Too close to center point, don't try to calculate outer color */
1505 outer_color = start_color;
1507 else
1509 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1510 outer_color = blend_colors(start_color, end_color, blend_amount);
1514 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1515 (end_point.X - start_point.X) * (yf - start_point.Y);
1517 distance = distance / center_distance;
1519 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1520 blend_colors(outer_color, fill->centercolor, distance);
1525 GdipDeletePath(flat_path);
1526 return stat;
1528 default:
1529 return NotImplemented;
1533 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1534 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1535 * should not be called on an hdc that has a path you care about. */
1536 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1537 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1539 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1540 GpMatrix matrix;
1541 HBRUSH brush = NULL;
1542 HPEN pen = NULL;
1543 PointF ptf[4], *custptf = NULL;
1544 POINT pt[4], *custpt = NULL;
1545 BYTE *tp = NULL;
1546 REAL theta, dsmall, dbig, dx, dy = 0.0;
1547 INT i, count;
1548 LOGBRUSH lb;
1549 BOOL customstroke;
1551 if((x1 == x2) && (y1 == y2))
1552 return;
1554 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1556 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1557 if(!customstroke){
1558 brush = CreateSolidBrush(color);
1559 lb.lbStyle = BS_SOLID;
1560 lb.lbColor = color;
1561 lb.lbHatch = 0;
1562 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1563 PS_JOIN_MITER, 1, &lb, 0,
1564 NULL);
1565 oldbrush = SelectObject(graphics->hdc, brush);
1566 oldpen = SelectObject(graphics->hdc, pen);
1569 switch(cap){
1570 case LineCapFlat:
1571 break;
1572 case LineCapSquare:
1573 case LineCapSquareAnchor:
1574 case LineCapDiamondAnchor:
1575 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1576 if(cap == LineCapDiamondAnchor){
1577 dsmall = cos(theta + M_PI_2) * size;
1578 dbig = sin(theta + M_PI_2) * size;
1580 else{
1581 dsmall = cos(theta + M_PI_4) * size;
1582 dbig = sin(theta + M_PI_4) * size;
1585 ptf[0].X = x2 - dsmall;
1586 ptf[1].X = x2 + dbig;
1588 ptf[0].Y = y2 - dbig;
1589 ptf[3].Y = y2 + dsmall;
1591 ptf[1].Y = y2 - dsmall;
1592 ptf[2].Y = y2 + dbig;
1594 ptf[3].X = x2 - dbig;
1595 ptf[2].X = x2 + dsmall;
1597 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1599 round_points(pt, ptf, 4);
1601 Polygon(graphics->hdc, pt, 4);
1603 break;
1604 case LineCapArrowAnchor:
1605 size = size * 4.0 / sqrt(3.0);
1607 dx = cos(M_PI / 6.0 + theta) * size;
1608 dy = sin(M_PI / 6.0 + theta) * size;
1610 ptf[0].X = x2 - dx;
1611 ptf[0].Y = y2 - dy;
1613 dx = cos(- M_PI / 6.0 + theta) * size;
1614 dy = sin(- M_PI / 6.0 + theta) * size;
1616 ptf[1].X = x2 - dx;
1617 ptf[1].Y = y2 - dy;
1619 ptf[2].X = x2;
1620 ptf[2].Y = y2;
1622 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1624 round_points(pt, ptf, 3);
1626 Polygon(graphics->hdc, pt, 3);
1628 break;
1629 case LineCapRoundAnchor:
1630 dx = dy = ANCHOR_WIDTH * size / 2.0;
1632 ptf[0].X = x2 - dx;
1633 ptf[0].Y = y2 - dy;
1634 ptf[1].X = x2 + dx;
1635 ptf[1].Y = y2 + dy;
1637 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 2);
1639 round_points(pt, ptf, 2);
1641 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1643 break;
1644 case LineCapTriangle:
1645 size = size / 2.0;
1646 dx = cos(M_PI_2 + theta) * size;
1647 dy = sin(M_PI_2 + theta) * size;
1649 ptf[0].X = x2 - dx;
1650 ptf[0].Y = y2 - dy;
1651 ptf[1].X = x2 + dx;
1652 ptf[1].Y = y2 + dy;
1654 dx = cos(theta) * size;
1655 dy = sin(theta) * size;
1657 ptf[2].X = x2 + dx;
1658 ptf[2].Y = y2 + dy;
1660 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1662 round_points(pt, ptf, 3);
1664 Polygon(graphics->hdc, pt, 3);
1666 break;
1667 case LineCapRound:
1668 dx = dy = size / 2.0;
1670 ptf[0].X = x2 - dx;
1671 ptf[0].Y = y2 - dy;
1672 ptf[1].X = x2 + dx;
1673 ptf[1].Y = y2 + dy;
1675 dx = -cos(M_PI_2 + theta) * size;
1676 dy = -sin(M_PI_2 + theta) * size;
1678 ptf[2].X = x2 - dx;
1679 ptf[2].Y = y2 - dy;
1680 ptf[3].X = x2 + dx;
1681 ptf[3].Y = y2 + dy;
1683 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1685 round_points(pt, ptf, 4);
1687 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1688 pt[2].y, pt[3].x, pt[3].y);
1690 break;
1691 case LineCapCustom:
1692 if(!custom)
1693 break;
1695 count = custom->pathdata.Count;
1696 custptf = heap_alloc_zero(count * sizeof(PointF));
1697 custpt = heap_alloc_zero(count * sizeof(POINT));
1698 tp = heap_alloc_zero(count);
1700 if(!custptf || !custpt || !tp)
1701 goto custend;
1703 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1705 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1706 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1707 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1708 MatrixOrderAppend);
1709 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1710 GdipTransformMatrixPoints(&matrix, custptf, count);
1712 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1714 round_points(pt, ptf, 3);
1716 for(i = 0; i < count; i++)
1717 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1719 if(custom->fill){
1720 BeginPath(graphics->hdc);
1721 PolyDraw(graphics->hdc, custpt, tp, count);
1722 EndPath(graphics->hdc);
1723 StrokeAndFillPath(graphics->hdc);
1725 else
1726 PolyDraw(graphics->hdc, custpt, tp, count);
1728 custend:
1729 heap_free(custptf);
1730 heap_free(custpt);
1731 heap_free(tp);
1732 break;
1733 default:
1734 break;
1737 if(!customstroke){
1738 SelectObject(graphics->hdc, oldbrush);
1739 SelectObject(graphics->hdc, oldpen);
1740 DeleteObject(brush);
1741 DeleteObject(pen);
1745 /* Shortens the line by the given percent by changing x2, y2.
1746 * If percent is > 1.0 then the line will change direction.
1747 * If percent is negative it can lengthen the line. */
1748 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1750 REAL dist, theta, dx, dy;
1752 if((y1 == *y2) && (x1 == *x2))
1753 return;
1755 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1756 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1757 dx = cos(theta) * dist;
1758 dy = sin(theta) * dist;
1760 *x2 = *x2 + dx;
1761 *y2 = *y2 + dy;
1764 /* Shortens the line by the given amount by changing x2, y2.
1765 * If the amount is greater than the distance, the line will become length 0.
1766 * If the amount is negative, it can lengthen the line. */
1767 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1769 REAL dx, dy, percent;
1771 dx = *x2 - x1;
1772 dy = *y2 - y1;
1773 if(dx == 0 && dy == 0)
1774 return;
1776 percent = amt / sqrt(dx * dx + dy * dy);
1777 if(percent >= 1.0){
1778 *x2 = x1;
1779 *y2 = y1;
1780 return;
1783 shorten_line_percent(x1, y1, x2, y2, percent);
1786 /* Conducts a linear search to find the bezier points that will back off
1787 * the endpoint of the curve by a distance of amt. Linear search works
1788 * better than binary in this case because there are multiple solutions,
1789 * and binary searches often find a bad one. I don't think this is what
1790 * Windows does but short of rendering the bezier without GDI's help it's
1791 * the best we can do. If rev then work from the start of the passed points
1792 * instead of the end. */
1793 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1795 GpPointF origpt[4];
1796 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1797 INT i, first = 0, second = 1, third = 2, fourth = 3;
1799 if(rev){
1800 first = 3;
1801 second = 2;
1802 third = 1;
1803 fourth = 0;
1806 origx = pt[fourth].X;
1807 origy = pt[fourth].Y;
1808 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1810 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1811 /* reset bezier points to original values */
1812 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1813 /* Perform magic on bezier points. Order is important here.*/
1814 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1815 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1816 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1817 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1818 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1819 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1821 dx = pt[fourth].X - origx;
1822 dy = pt[fourth].Y - origy;
1824 diff = sqrt(dx * dx + dy * dy);
1825 percent += 0.0005 * amt;
1829 /* Draws a combination of bezier curves and lines between points. */
1830 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1831 GDIPCONST BYTE * types, INT count, BOOL caps)
1833 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1834 BYTE *tp = heap_alloc_zero(count);
1835 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1836 INT i, j;
1837 GpStatus status = GenericError;
1839 if(!count){
1840 status = Ok;
1841 goto end;
1843 if(!pti || !tp || !ptcopy){
1844 status = OutOfMemory;
1845 goto end;
1848 for(i = 1; i < count; i++){
1849 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1850 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1851 || !(types[i + 2] & PathPointTypeBezier)){
1852 ERR("Bad bezier points\n");
1853 goto end;
1855 i += 2;
1859 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1861 /* If we are drawing caps, go through the points and adjust them accordingly,
1862 * and draw the caps. */
1863 if(caps){
1864 switch(types[count - 1] & PathPointTypePathTypeMask){
1865 case PathPointTypeBezier:
1866 if(pen->endcap == LineCapArrowAnchor)
1867 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1868 else if((pen->endcap == LineCapCustom) && pen->customend)
1869 shorten_bezier_amt(&ptcopy[count - 4],
1870 pen->width * pen->customend->inset, FALSE);
1872 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1873 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1874 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1875 pt[count - 1].X, pt[count - 1].Y);
1877 break;
1878 case PathPointTypeLine:
1879 if(pen->endcap == LineCapArrowAnchor)
1880 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1881 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1882 pen->width);
1883 else if((pen->endcap == LineCapCustom) && pen->customend)
1884 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1885 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1886 pen->customend->inset * pen->width);
1888 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1889 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1890 pt[count - 1].Y);
1892 break;
1893 default:
1894 ERR("Bad path last point\n");
1895 goto end;
1898 /* Find start of points */
1899 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1900 == PathPointTypeStart); j++);
1902 switch(types[j] & PathPointTypePathTypeMask){
1903 case PathPointTypeBezier:
1904 if(pen->startcap == LineCapArrowAnchor)
1905 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1906 else if((pen->startcap == LineCapCustom) && pen->customstart)
1907 shorten_bezier_amt(&ptcopy[j - 1],
1908 pen->width * pen->customstart->inset, TRUE);
1910 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1911 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1912 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1913 pt[j - 1].X, pt[j - 1].Y);
1915 break;
1916 case PathPointTypeLine:
1917 if(pen->startcap == LineCapArrowAnchor)
1918 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1919 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1920 pen->width);
1921 else if((pen->startcap == LineCapCustom) && pen->customstart)
1922 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1923 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1924 pen->customstart->inset * pen->width);
1926 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1927 pt[j].X, pt[j].Y, pt[j - 1].X,
1928 pt[j - 1].Y);
1930 break;
1931 default:
1932 ERR("Bad path points\n");
1933 goto end;
1937 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
1939 round_points(pti, ptcopy, count);
1941 for(i = 0; i < count; i++){
1942 tp[i] = convert_path_point_type(types[i]);
1945 PolyDraw(graphics->hdc, pti, tp, count);
1947 status = Ok;
1949 end:
1950 heap_free(pti);
1951 heap_free(ptcopy);
1952 heap_free(tp);
1954 return status;
1957 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1959 GpStatus result;
1961 BeginPath(graphics->hdc);
1962 result = draw_poly(graphics, NULL, path->pathdata.Points,
1963 path->pathdata.Types, path->pathdata.Count, FALSE);
1964 EndPath(graphics->hdc);
1965 return result;
1968 typedef enum GraphicsContainerType {
1969 BEGIN_CONTAINER,
1970 SAVE_GRAPHICS
1971 } GraphicsContainerType;
1973 typedef struct _GraphicsContainerItem {
1974 struct list entry;
1975 GraphicsContainer contid;
1976 GraphicsContainerType type;
1978 SmoothingMode smoothing;
1979 CompositingQuality compqual;
1980 InterpolationMode interpolation;
1981 CompositingMode compmode;
1982 TextRenderingHint texthint;
1983 REAL scale;
1984 GpUnit unit;
1985 PixelOffsetMode pixeloffset;
1986 UINT textcontrast;
1987 GpMatrix worldtrans;
1988 GpRegion* clip;
1989 INT origin_x, origin_y;
1990 } GraphicsContainerItem;
1992 static GpStatus init_container(GraphicsContainerItem** container,
1993 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
1994 GpStatus sts;
1996 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
1997 if(!(*container))
1998 return OutOfMemory;
2000 (*container)->contid = graphics->contid + 1;
2001 (*container)->type = type;
2003 (*container)->smoothing = graphics->smoothing;
2004 (*container)->compqual = graphics->compqual;
2005 (*container)->interpolation = graphics->interpolation;
2006 (*container)->compmode = graphics->compmode;
2007 (*container)->texthint = graphics->texthint;
2008 (*container)->scale = graphics->scale;
2009 (*container)->unit = graphics->unit;
2010 (*container)->textcontrast = graphics->textcontrast;
2011 (*container)->pixeloffset = graphics->pixeloffset;
2012 (*container)->origin_x = graphics->origin_x;
2013 (*container)->origin_y = graphics->origin_y;
2014 (*container)->worldtrans = graphics->worldtrans;
2016 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2017 if(sts != Ok){
2018 heap_free(*container);
2019 *container = NULL;
2020 return sts;
2023 return Ok;
2026 static void delete_container(GraphicsContainerItem* container)
2028 GdipDeleteRegion(container->clip);
2029 heap_free(container);
2032 static GpStatus restore_container(GpGraphics* graphics,
2033 GDIPCONST GraphicsContainerItem* container){
2034 GpStatus sts;
2035 GpRegion *newClip;
2037 sts = GdipCloneRegion(container->clip, &newClip);
2038 if(sts != Ok) return sts;
2040 graphics->worldtrans = container->worldtrans;
2042 GdipDeleteRegion(graphics->clip);
2043 graphics->clip = newClip;
2045 graphics->contid = container->contid - 1;
2047 graphics->smoothing = container->smoothing;
2048 graphics->compqual = container->compqual;
2049 graphics->interpolation = container->interpolation;
2050 graphics->compmode = container->compmode;
2051 graphics->texthint = container->texthint;
2052 graphics->scale = container->scale;
2053 graphics->unit = container->unit;
2054 graphics->textcontrast = container->textcontrast;
2055 graphics->pixeloffset = container->pixeloffset;
2056 graphics->origin_x = container->origin_x;
2057 graphics->origin_y = container->origin_y;
2059 return Ok;
2062 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2064 RECT wnd_rect;
2065 GpStatus stat=Ok;
2066 GpUnit unit;
2068 if(graphics->hwnd) {
2069 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2070 return GenericError;
2072 rect->X = wnd_rect.left;
2073 rect->Y = wnd_rect.top;
2074 rect->Width = wnd_rect.right - wnd_rect.left;
2075 rect->Height = wnd_rect.bottom - wnd_rect.top;
2076 }else if (graphics->image){
2077 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2078 if (stat == Ok && unit != UnitPixel)
2079 FIXME("need to convert from unit %i\n", unit);
2080 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2081 HBITMAP hbmp;
2082 BITMAP bmp;
2084 rect->X = 0;
2085 rect->Y = 0;
2087 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2088 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2090 rect->Width = bmp.bmWidth;
2091 rect->Height = bmp.bmHeight;
2093 else
2095 /* FIXME: ??? */
2096 rect->Width = 1;
2097 rect->Height = 1;
2099 }else{
2100 rect->X = 0;
2101 rect->Y = 0;
2102 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2103 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2106 return stat;
2109 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2111 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2113 if (stat == Ok && graphics->hdc)
2115 GpPointF points[4], min_point, max_point;
2116 int i;
2118 points[0].X = points[2].X = rect->X;
2119 points[0].Y = points[1].Y = rect->Y;
2120 points[1].X = points[3].X = rect->X + rect->Width;
2121 points[2].Y = points[3].Y = rect->Y + rect->Height;
2123 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2125 min_point = max_point = points[0];
2127 for (i=1; i<4; i++)
2129 if (points[i].X < min_point.X) min_point.X = points[i].X;
2130 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2131 if (points[i].X > max_point.X) max_point.X = points[i].X;
2132 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2135 rect->X = min_point.X;
2136 rect->Y = min_point.Y;
2137 rect->Width = max_point.X - min_point.X;
2138 rect->Height = max_point.Y - min_point.Y;
2141 return stat;
2144 /* on success, rgn will contain the region of the graphics object which
2145 * is visible after clipping has been applied */
2146 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2148 GpStatus stat;
2149 GpRectF rectf;
2150 GpRegion* tmp;
2152 /* Ignore graphics image bounds for metafiles */
2153 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2154 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2156 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2157 return stat;
2159 if((stat = GdipCreateRegion(&tmp)) != Ok)
2160 return stat;
2162 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2163 goto end;
2165 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2166 goto end;
2168 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2170 end:
2171 GdipDeleteRegion(tmp);
2172 return stat;
2175 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2177 REAL height;
2179 if (font->unit == UnitPixel)
2181 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2183 else
2185 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2186 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2187 else
2188 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2191 lf->lfHeight = -(height + 0.5);
2192 lf->lfWidth = 0;
2193 lf->lfEscapement = 0;
2194 lf->lfOrientation = 0;
2195 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2196 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2197 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2198 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2199 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2200 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2201 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2202 lf->lfQuality = DEFAULT_QUALITY;
2203 lf->lfPitchAndFamily = 0;
2204 strcpyW(lf->lfFaceName, font->family->FamilyName);
2207 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2208 GDIPCONST GpStringFormat *format, HFONT *hfont,
2209 GDIPCONST GpMatrix *matrix)
2211 HDC hdc = CreateCompatibleDC(0);
2212 GpPointF pt[3];
2213 REAL angle, rel_width, rel_height, font_height;
2214 LOGFONTW lfw;
2215 HFONT unscaled_font;
2216 TEXTMETRICW textmet;
2218 if (font->unit == UnitPixel || font->unit == UnitWorld)
2219 font_height = font->emSize;
2220 else
2222 REAL unit_scale, res;
2224 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2225 unit_scale = units_scale(font->unit, graphics->unit, res);
2227 font_height = font->emSize * unit_scale;
2230 pt[0].X = 0.0;
2231 pt[0].Y = 0.0;
2232 pt[1].X = 1.0;
2233 pt[1].Y = 0.0;
2234 pt[2].X = 0.0;
2235 pt[2].Y = 1.0;
2236 if (matrix)
2238 GpMatrix xform = *matrix;
2239 GdipTransformMatrixPoints(&xform, pt, 3);
2242 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2243 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2244 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2245 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2246 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2247 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2249 get_log_fontW(font, graphics, &lfw);
2250 lfw.lfHeight = -gdip_round(font_height * rel_height);
2251 unscaled_font = CreateFontIndirectW(&lfw);
2253 SelectObject(hdc, unscaled_font);
2254 GetTextMetricsW(hdc, &textmet);
2256 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2257 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2259 *hfont = CreateFontIndirectW(&lfw);
2261 DeleteDC(hdc);
2262 DeleteObject(unscaled_font);
2265 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2267 TRACE("(%p, %p)\n", hdc, graphics);
2269 return GdipCreateFromHDC2(hdc, NULL, graphics);
2272 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2274 XFORM xform;
2276 if (graphics->hdc == NULL)
2278 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2279 return;
2282 GetTransform(graphics->hdc, 0x204, &xform);
2283 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2286 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2288 GpStatus retval;
2289 HBITMAP hbitmap;
2290 DIBSECTION dib;
2292 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2294 if(hDevice != NULL)
2295 FIXME("Don't know how to handle parameter hDevice\n");
2297 if(hdc == NULL)
2298 return OutOfMemory;
2300 if(graphics == NULL)
2301 return InvalidParameter;
2303 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2304 if(!*graphics) return OutOfMemory;
2306 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2308 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2309 heap_free(*graphics);
2310 return retval;
2313 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2314 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2315 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2317 (*graphics)->alpha_hdc = 1;
2320 (*graphics)->hdc = hdc;
2321 (*graphics)->hwnd = WindowFromDC(hdc);
2322 (*graphics)->owndc = FALSE;
2323 (*graphics)->smoothing = SmoothingModeDefault;
2324 (*graphics)->compqual = CompositingQualityDefault;
2325 (*graphics)->interpolation = InterpolationModeBilinear;
2326 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2327 (*graphics)->compmode = CompositingModeSourceOver;
2328 (*graphics)->unit = UnitDisplay;
2329 (*graphics)->scale = 1.0;
2330 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2331 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2332 (*graphics)->busy = FALSE;
2333 (*graphics)->textcontrast = 4;
2334 list_init(&(*graphics)->containers);
2335 (*graphics)->contid = 0;
2336 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2338 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2339 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2341 DeleteObject((*graphics)->gdi_clip);
2342 (*graphics)->gdi_clip = NULL;
2345 TRACE("<-- %p\n", *graphics);
2347 return Ok;
2350 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2352 GpStatus retval;
2354 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2355 if(!*graphics) return OutOfMemory;
2357 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2358 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2360 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2361 heap_free(*graphics);
2362 return retval;
2365 (*graphics)->hdc = NULL;
2366 (*graphics)->hwnd = NULL;
2367 (*graphics)->owndc = FALSE;
2368 (*graphics)->image = image;
2369 /* We have to store the image type here because the image may be freed
2370 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2371 (*graphics)->image_type = image->type;
2372 (*graphics)->smoothing = SmoothingModeDefault;
2373 (*graphics)->compqual = CompositingQualityDefault;
2374 (*graphics)->interpolation = InterpolationModeBilinear;
2375 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2376 (*graphics)->compmode = CompositingModeSourceOver;
2377 (*graphics)->unit = UnitDisplay;
2378 (*graphics)->scale = 1.0;
2379 (*graphics)->xres = image->xres;
2380 (*graphics)->yres = image->yres;
2381 (*graphics)->busy = FALSE;
2382 (*graphics)->textcontrast = 4;
2383 list_init(&(*graphics)->containers);
2384 (*graphics)->contid = 0;
2386 TRACE("<-- %p\n", *graphics);
2388 return Ok;
2391 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2393 GpStatus ret;
2394 HDC hdc;
2396 TRACE("(%p, %p)\n", hwnd, graphics);
2398 hdc = GetDC(hwnd);
2400 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2402 ReleaseDC(hwnd, hdc);
2403 return ret;
2406 (*graphics)->hwnd = hwnd;
2407 (*graphics)->owndc = TRUE;
2409 return Ok;
2412 /* FIXME: no icm handling */
2413 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2415 TRACE("(%p, %p)\n", hwnd, graphics);
2417 return GdipCreateFromHWND(hwnd, graphics);
2420 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2421 UINT access, IStream **stream)
2423 DWORD dwMode;
2424 HRESULT ret;
2426 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2428 if(!stream || !filename)
2429 return InvalidParameter;
2431 if(access & GENERIC_WRITE)
2432 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2433 else if(access & GENERIC_READ)
2434 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2435 else
2436 return InvalidParameter;
2438 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2440 return hresult_to_status(ret);
2443 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2445 GraphicsContainerItem *cont, *next;
2446 GpStatus stat;
2447 TRACE("(%p)\n", graphics);
2449 if(!graphics) return InvalidParameter;
2450 if(graphics->busy) return ObjectBusy;
2452 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2454 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2455 if (stat != Ok)
2456 return stat;
2459 if(graphics->owndc)
2460 ReleaseDC(graphics->hwnd, graphics->hdc);
2462 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2463 list_remove(&cont->entry);
2464 delete_container(cont);
2467 GdipDeleteRegion(graphics->clip);
2469 DeleteObject(graphics->gdi_clip);
2471 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2472 * do otherwise, but we can't have that in the test suite because it means
2473 * accessing freed memory. */
2474 graphics->busy = TRUE;
2476 heap_free(graphics);
2478 return Ok;
2481 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2482 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2484 GpStatus status;
2485 GpPath *path;
2487 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2488 width, height, startAngle, sweepAngle);
2490 if(!graphics || !pen || width <= 0 || height <= 0)
2491 return InvalidParameter;
2493 if(graphics->busy)
2494 return ObjectBusy;
2496 status = GdipCreatePath(FillModeAlternate, &path);
2497 if (status != Ok) return status;
2499 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2500 if (status == Ok)
2501 status = GdipDrawPath(graphics, pen, path);
2503 GdipDeletePath(path);
2504 return status;
2507 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2508 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2510 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2511 width, height, startAngle, sweepAngle);
2513 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2516 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2517 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2519 GpPointF pt[4];
2521 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2522 x2, y2, x3, y3, x4, y4);
2524 if(!graphics || !pen)
2525 return InvalidParameter;
2527 if(graphics->busy)
2528 return ObjectBusy;
2530 pt[0].X = x1;
2531 pt[0].Y = y1;
2532 pt[1].X = x2;
2533 pt[1].Y = y2;
2534 pt[2].X = x3;
2535 pt[2].Y = y3;
2536 pt[3].X = x4;
2537 pt[3].Y = y4;
2538 return GdipDrawBeziers(graphics, pen, pt, 4);
2541 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2542 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2544 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2545 x2, y2, x3, y3, x4, y4);
2547 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2550 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2551 GDIPCONST GpPointF *points, INT count)
2553 GpStatus status;
2554 GpPath *path;
2556 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2558 if(!graphics || !pen || !points || (count <= 0))
2559 return InvalidParameter;
2561 if(graphics->busy)
2562 return ObjectBusy;
2564 status = GdipCreatePath(FillModeAlternate, &path);
2565 if (status != Ok) return status;
2567 status = GdipAddPathBeziers(path, points, count);
2568 if (status == Ok)
2569 status = GdipDrawPath(graphics, pen, path);
2571 GdipDeletePath(path);
2572 return status;
2575 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2576 GDIPCONST GpPoint *points, INT count)
2578 GpPointF *pts;
2579 GpStatus ret;
2580 INT i;
2582 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2584 if(!graphics || !pen || !points || (count <= 0))
2585 return InvalidParameter;
2587 if(graphics->busy)
2588 return ObjectBusy;
2590 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2591 if(!pts)
2592 return OutOfMemory;
2594 for(i = 0; i < count; i++){
2595 pts[i].X = (REAL)points[i].X;
2596 pts[i].Y = (REAL)points[i].Y;
2599 ret = GdipDrawBeziers(graphics,pen,pts,count);
2601 heap_free(pts);
2603 return ret;
2606 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2607 GDIPCONST GpPointF *points, INT count)
2609 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2611 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2614 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2615 GDIPCONST GpPoint *points, INT count)
2617 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2619 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2622 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2623 GDIPCONST GpPointF *points, INT count, REAL tension)
2625 GpPath *path;
2626 GpStatus status;
2628 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2630 if(!graphics || !pen || !points || count <= 0)
2631 return InvalidParameter;
2633 if(graphics->busy)
2634 return ObjectBusy;
2636 status = GdipCreatePath(FillModeAlternate, &path);
2637 if (status != Ok) return status;
2639 status = GdipAddPathClosedCurve2(path, points, count, tension);
2640 if (status == Ok)
2641 status = GdipDrawPath(graphics, pen, path);
2643 GdipDeletePath(path);
2645 return status;
2648 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2649 GDIPCONST GpPoint *points, INT count, REAL tension)
2651 GpPointF *ptf;
2652 GpStatus stat;
2653 INT i;
2655 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2657 if(!points || count <= 0)
2658 return InvalidParameter;
2660 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2661 if(!ptf)
2662 return OutOfMemory;
2664 for(i = 0; i < count; i++){
2665 ptf[i].X = (REAL)points[i].X;
2666 ptf[i].Y = (REAL)points[i].Y;
2669 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2671 heap_free(ptf);
2673 return stat;
2676 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2677 GDIPCONST GpPointF *points, INT count)
2679 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2681 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2684 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2685 GDIPCONST GpPoint *points, INT count)
2687 GpPointF *pointsF;
2688 GpStatus ret;
2689 INT i;
2691 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2693 if(!points)
2694 return InvalidParameter;
2696 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2697 if(!pointsF)
2698 return OutOfMemory;
2700 for(i = 0; i < count; i++){
2701 pointsF[i].X = (REAL)points[i].X;
2702 pointsF[i].Y = (REAL)points[i].Y;
2705 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2706 heap_free(pointsF);
2708 return ret;
2711 /* Approximates cardinal spline with Bezier curves. */
2712 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2713 GDIPCONST GpPointF *points, INT count, REAL tension)
2715 GpPath *path;
2716 GpStatus status;
2718 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2720 if(!graphics || !pen)
2721 return InvalidParameter;
2723 if(graphics->busy)
2724 return ObjectBusy;
2726 if(count < 2)
2727 return InvalidParameter;
2729 status = GdipCreatePath(FillModeAlternate, &path);
2730 if (status != Ok) return status;
2732 status = GdipAddPathCurve2(path, points, count, tension);
2733 if (status == Ok)
2734 status = GdipDrawPath(graphics, pen, path);
2736 GdipDeletePath(path);
2737 return status;
2740 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2741 GDIPCONST GpPoint *points, INT count, REAL tension)
2743 GpPointF *pointsF;
2744 GpStatus ret;
2745 INT i;
2747 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2749 if(!points)
2750 return InvalidParameter;
2752 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2753 if(!pointsF)
2754 return OutOfMemory;
2756 for(i = 0; i < count; i++){
2757 pointsF[i].X = (REAL)points[i].X;
2758 pointsF[i].Y = (REAL)points[i].Y;
2761 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2762 heap_free(pointsF);
2764 return ret;
2767 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2768 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2769 REAL tension)
2771 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2773 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2774 return InvalidParameter;
2777 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2780 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2781 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2782 REAL tension)
2784 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2786 if(count < 0){
2787 return OutOfMemory;
2790 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2791 return InvalidParameter;
2794 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2797 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2798 REAL y, REAL width, REAL height)
2800 GpPath *path;
2801 GpStatus status;
2803 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2805 if(!graphics || !pen)
2806 return InvalidParameter;
2808 if(graphics->busy)
2809 return ObjectBusy;
2811 status = GdipCreatePath(FillModeAlternate, &path);
2812 if (status != Ok) return status;
2814 status = GdipAddPathEllipse(path, x, y, width, height);
2815 if (status == Ok)
2816 status = GdipDrawPath(graphics, pen, path);
2818 GdipDeletePath(path);
2819 return status;
2822 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2823 INT y, INT width, INT height)
2825 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2827 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2831 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2833 UINT width, height;
2835 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2837 if(!graphics || !image)
2838 return InvalidParameter;
2840 GdipGetImageWidth(image, &width);
2841 GdipGetImageHeight(image, &height);
2843 return GdipDrawImagePointRect(graphics, image, x, y,
2844 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2847 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2848 INT y)
2850 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2852 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2855 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2856 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2857 GpUnit srcUnit)
2859 GpPointF points[3];
2860 REAL scale_x, scale_y, width, height;
2862 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2864 if (!graphics || !image) return InvalidParameter;
2866 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2867 scale_x *= graphics->xres / image->xres;
2868 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2869 scale_y *= graphics->yres / image->yres;
2870 width = srcwidth * scale_x;
2871 height = srcheight * scale_y;
2873 points[0].X = points[2].X = x;
2874 points[0].Y = points[1].Y = y;
2875 points[1].X = x + width;
2876 points[2].Y = y + height;
2878 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2879 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2882 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2883 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2884 GpUnit srcUnit)
2886 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2889 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2890 GDIPCONST GpPointF *dstpoints, INT count)
2892 UINT width, height;
2894 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2896 if(!image)
2897 return InvalidParameter;
2899 GdipGetImageWidth(image, &width);
2900 GdipGetImageHeight(image, &height);
2902 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2903 width, height, UnitPixel, NULL, NULL, NULL);
2906 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2907 GDIPCONST GpPoint *dstpoints, INT count)
2909 GpPointF ptf[3];
2911 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2913 if (count != 3 || !dstpoints)
2914 return InvalidParameter;
2916 ptf[0].X = (REAL)dstpoints[0].X;
2917 ptf[0].Y = (REAL)dstpoints[0].Y;
2918 ptf[1].X = (REAL)dstpoints[1].X;
2919 ptf[1].Y = (REAL)dstpoints[1].Y;
2920 ptf[2].X = (REAL)dstpoints[2].X;
2921 ptf[2].Y = (REAL)dstpoints[2].Y;
2923 return GdipDrawImagePoints(graphics, image, ptf, count);
2926 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2927 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2929 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2930 return TRUE;
2933 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2934 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2935 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2936 DrawImageAbort callback, VOID * callbackData)
2938 GpPointF ptf[4];
2939 POINT pti[4];
2940 GpStatus stat;
2942 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2943 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2944 callbackData);
2946 if (count > 3)
2947 return NotImplemented;
2949 if(!graphics || !image || !points || count != 3)
2950 return InvalidParameter;
2952 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2953 debugstr_pointf(&points[2]));
2955 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2957 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
2958 image, points, count, srcx, srcy, srcwidth, srcheight,
2959 srcUnit, imageAttributes, callback, callbackData);
2962 memcpy(ptf, points, 3 * sizeof(GpPointF));
2964 /* Ensure source width/height is positive */
2965 if (srcwidth < 0)
2967 GpPointF tmp = ptf[1];
2968 srcx = srcx + srcwidth;
2969 srcwidth = -srcwidth;
2970 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2971 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2972 ptf[1] = ptf[0];
2973 ptf[0] = tmp;
2976 if (srcheight < 0)
2978 GpPointF tmp = ptf[2];
2979 srcy = srcy + srcheight;
2980 srcheight = -srcheight;
2981 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2982 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2983 ptf[2] = ptf[0];
2984 ptf[0] = tmp;
2987 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2988 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2989 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2990 return Ok;
2991 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
2992 round_points(pti, ptf, 4);
2994 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2995 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2997 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2998 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2999 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3000 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3001 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3003 if (image->type == ImageTypeBitmap)
3005 GpBitmap* bitmap = (GpBitmap*)image;
3006 BOOL do_resampling = FALSE;
3007 BOOL use_software = FALSE;
3009 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3010 graphics->xres, graphics->yres,
3011 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3012 graphics->scale, image->xres, image->yres, bitmap->format,
3013 imageAttributes ? imageAttributes->outside_color : 0);
3015 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3016 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3017 srcx < 0 || srcy < 0 ||
3018 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3019 do_resampling = TRUE;
3021 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3022 (graphics->image && graphics->image->type == ImageTypeBitmap))
3023 use_software = TRUE;
3025 if (use_software)
3027 RECT dst_area;
3028 GpRectF graphics_bounds;
3029 GpRect src_area;
3030 int i, x, y, src_stride, dst_stride;
3031 GpMatrix dst_to_src;
3032 REAL m11, m12, m21, m22, mdx, mdy;
3033 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3034 BitmapData lockeddata;
3035 InterpolationMode interpolation = graphics->interpolation;
3036 PixelOffsetMode offset_mode = graphics->pixeloffset;
3037 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3038 REAL x_dx, x_dy, y_dx, y_dy;
3039 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3041 if (!imageAttributes)
3042 imageAttributes = &defaultImageAttributes;
3044 dst_area.left = dst_area.right = pti[0].x;
3045 dst_area.top = dst_area.bottom = pti[0].y;
3046 for (i=1; i<4; i++)
3048 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3049 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3050 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3051 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3054 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3055 if (stat != Ok) return stat;
3057 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3058 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3059 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3060 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3062 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3064 if (IsRectEmpty(&dst_area)) return Ok;
3066 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3067 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3068 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3069 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3070 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3071 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3073 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3075 stat = GdipInvertMatrix(&dst_to_src);
3076 if (stat != Ok) return stat;
3078 if (do_resampling)
3080 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3081 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3083 else
3085 /* Make sure src_area is equal in size to dst_area. */
3086 src_area.X = srcx + dst_area.left - pti[0].x;
3087 src_area.Y = srcy + dst_area.top - pti[0].y;
3088 src_area.Width = dst_area.right - dst_area.left;
3089 src_area.Height = dst_area.bottom - dst_area.top;
3092 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3094 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3095 if (!src_data)
3096 return OutOfMemory;
3097 src_stride = sizeof(ARGB) * src_area.Width;
3099 /* Read the bits we need from the source bitmap into a compatible buffer. */
3100 lockeddata.Width = src_area.Width;
3101 lockeddata.Height = src_area.Height;
3102 lockeddata.Stride = src_stride;
3103 lockeddata.Scan0 = src_data;
3104 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3105 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3106 else
3107 lockeddata.PixelFormat = PixelFormat32bppARGB;
3109 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3110 lockeddata.PixelFormat, &lockeddata);
3112 if (stat == Ok)
3113 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3115 if (stat != Ok)
3117 heap_free(src_data);
3118 return stat;
3121 apply_image_attributes(imageAttributes, src_data,
3122 src_area.Width, src_area.Height,
3123 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3125 if (do_resampling)
3127 /* Transform the bits as needed to the destination. */
3128 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3129 if (!dst_data)
3131 heap_free(src_data);
3132 return OutOfMemory;
3135 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3137 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3139 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3140 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3141 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3142 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3144 for (x=dst_area.left; x<dst_area.right; x++)
3146 for (y=dst_area.top; y<dst_area.bottom; y++)
3148 GpPointF src_pointf;
3149 ARGB *dst_color;
3151 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3152 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3154 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3156 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3157 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3158 imageAttributes, interpolation, offset_mode);
3159 else
3160 *dst_color = 0;
3164 else
3166 dst_data = src_data;
3167 dst_stride = src_stride;
3170 gdi_transform_acquire(graphics);
3172 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3173 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3174 lockeddata.PixelFormat);
3176 gdi_transform_release(graphics);
3178 heap_free(src_data);
3180 heap_free(dst_dyn_data);
3182 return stat;
3184 else
3186 HDC hdc;
3187 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3188 HBITMAP hbitmap, old_hbm=NULL;
3189 HRGN hrgn;
3190 INT save_state;
3192 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3193 bitmap->format == PixelFormat24bppRGB ||
3194 bitmap->format == PixelFormat32bppRGB ||
3195 bitmap->format == PixelFormat32bppPARGB))
3197 BITMAPINFOHEADER bih;
3198 BYTE *temp_bits;
3199 PixelFormat dst_format;
3201 /* we can't draw a bitmap of this format directly */
3202 hdc = CreateCompatibleDC(0);
3203 temp_hdc = TRUE;
3204 temp_bitmap = TRUE;
3206 bih.biSize = sizeof(BITMAPINFOHEADER);
3207 bih.biWidth = bitmap->width;
3208 bih.biHeight = -bitmap->height;
3209 bih.biPlanes = 1;
3210 bih.biBitCount = 32;
3211 bih.biCompression = BI_RGB;
3212 bih.biSizeImage = 0;
3213 bih.biXPelsPerMeter = 0;
3214 bih.biYPelsPerMeter = 0;
3215 bih.biClrUsed = 0;
3216 bih.biClrImportant = 0;
3218 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3219 (void**)&temp_bits, NULL, 0);
3221 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3222 dst_format = PixelFormat32bppPARGB;
3223 else
3224 dst_format = PixelFormat32bppRGB;
3226 convert_pixels(bitmap->width, bitmap->height,
3227 bitmap->width*4, temp_bits, dst_format,
3228 bitmap->stride, bitmap->bits, bitmap->format,
3229 bitmap->image.palette);
3231 else
3233 if (bitmap->hbitmap)
3234 hbitmap = bitmap->hbitmap;
3235 else
3237 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3238 temp_bitmap = TRUE;
3241 hdc = bitmap->hdc;
3242 temp_hdc = (hdc == 0);
3245 if (temp_hdc)
3247 if (!hdc) hdc = CreateCompatibleDC(0);
3248 old_hbm = SelectObject(hdc, hbitmap);
3251 save_state = SaveDC(graphics->hdc);
3253 stat = get_clip_hrgn(graphics, &hrgn);
3255 if (stat == Ok)
3257 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3258 DeleteObject(hrgn);
3261 gdi_transform_acquire(graphics);
3263 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3265 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3266 hdc, srcx, srcy, srcwidth, srcheight);
3268 else
3270 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3271 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3274 gdi_transform_release(graphics);
3276 RestoreDC(graphics->hdc, save_state);
3278 if (temp_hdc)
3280 SelectObject(hdc, old_hbm);
3281 DeleteDC(hdc);
3284 if (temp_bitmap)
3285 DeleteObject(hbitmap);
3288 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3290 GpRectF rc;
3292 rc.X = srcx;
3293 rc.Y = srcy;
3294 rc.Width = srcwidth;
3295 rc.Height = srcheight;
3297 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3298 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3300 else
3302 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3303 return InvalidParameter;
3306 return Ok;
3309 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3310 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3311 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3312 DrawImageAbort callback, VOID * callbackData)
3314 GpPointF pointsF[3];
3315 INT i;
3317 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3318 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3319 callbackData);
3321 if(!points || count!=3)
3322 return InvalidParameter;
3324 for(i = 0; i < count; i++){
3325 pointsF[i].X = (REAL)points[i].X;
3326 pointsF[i].Y = (REAL)points[i].Y;
3329 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3330 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3331 callback, callbackData);
3334 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3335 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3336 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3337 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3338 VOID * callbackData)
3340 GpPointF points[3];
3342 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3343 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3344 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3346 points[0].X = dstx;
3347 points[0].Y = dsty;
3348 points[1].X = dstx + dstwidth;
3349 points[1].Y = dsty;
3350 points[2].X = dstx;
3351 points[2].Y = dsty + dstheight;
3353 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3354 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3357 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3358 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3359 INT srcwidth, INT srcheight, GpUnit srcUnit,
3360 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3361 VOID * callbackData)
3363 GpPointF points[3];
3365 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3366 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3367 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3369 points[0].X = dstx;
3370 points[0].Y = dsty;
3371 points[1].X = dstx + dstwidth;
3372 points[1].Y = dsty;
3373 points[2].X = dstx;
3374 points[2].Y = dsty + dstheight;
3376 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3377 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3380 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3381 REAL x, REAL y, REAL width, REAL height)
3383 RectF bounds;
3384 GpUnit unit;
3385 GpStatus ret;
3387 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3389 if(!graphics || !image)
3390 return InvalidParameter;
3392 ret = GdipGetImageBounds(image, &bounds, &unit);
3393 if(ret != Ok)
3394 return ret;
3396 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3397 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3398 unit, NULL, NULL, NULL);
3401 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3402 INT x, INT y, INT width, INT height)
3404 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3406 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3409 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3410 REAL y1, REAL x2, REAL y2)
3412 GpPointF pt[2];
3414 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3416 if (!pen)
3417 return InvalidParameter;
3419 if (pen->unit == UnitPixel && pen->width <= 0.0)
3420 return Ok;
3422 pt[0].X = x1;
3423 pt[0].Y = y1;
3424 pt[1].X = x2;
3425 pt[1].Y = y2;
3426 return GdipDrawLines(graphics, pen, pt, 2);
3429 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3430 INT y1, INT x2, INT y2)
3432 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3434 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3437 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3438 GpPointF *points, INT count)
3440 GpStatus status;
3441 GpPath *path;
3443 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3445 if(!pen || !graphics || (count < 2))
3446 return InvalidParameter;
3448 if(graphics->busy)
3449 return ObjectBusy;
3451 status = GdipCreatePath(FillModeAlternate, &path);
3452 if (status != Ok) return status;
3454 status = GdipAddPathLine2(path, points, count);
3455 if (status == Ok)
3456 status = GdipDrawPath(graphics, pen, path);
3458 GdipDeletePath(path);
3459 return status;
3462 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3463 GpPoint *points, INT count)
3465 GpStatus retval;
3466 GpPointF *ptf;
3467 int i;
3469 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3471 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3472 if(!ptf) return OutOfMemory;
3474 for(i = 0; i < count; i ++){
3475 ptf[i].X = (REAL) points[i].X;
3476 ptf[i].Y = (REAL) points[i].Y;
3479 retval = GdipDrawLines(graphics, pen, ptf, count);
3481 heap_free(ptf);
3482 return retval;
3485 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3487 INT save_state;
3488 GpStatus retval;
3489 HRGN hrgn=NULL;
3491 save_state = prepare_dc(graphics, pen);
3493 retval = get_clip_hrgn(graphics, &hrgn);
3495 if (retval != Ok)
3496 goto end;
3498 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3500 gdi_transform_acquire(graphics);
3502 retval = draw_poly(graphics, pen, path->pathdata.Points,
3503 path->pathdata.Types, path->pathdata.Count, TRUE);
3505 gdi_transform_release(graphics);
3507 end:
3508 restore_dc(graphics, save_state);
3509 DeleteObject(hrgn);
3511 return retval;
3514 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3516 GpStatus stat;
3517 GpPath* flat_path;
3518 GpMatrix* transform;
3519 GpRectF gp_bound_rect;
3520 GpRect gp_output_area;
3521 RECT output_area;
3522 INT output_height, output_width;
3523 DWORD *output_bits, *brush_bits=NULL;
3524 int i;
3525 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3526 const BYTE *dash_pattern;
3527 INT dash_pattern_size;
3528 BYTE *dyn_dash_pattern = NULL;
3530 stat = GdipClonePath(path, &flat_path);
3532 if (stat != Ok)
3533 return stat;
3535 stat = GdipCreateMatrix(&transform);
3537 if (stat == Ok)
3539 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3540 CoordinateSpaceWorld, transform);
3542 if (stat == Ok)
3543 stat = GdipFlattenPath(flat_path, transform, 1.0);
3545 GdipDeleteMatrix(transform);
3548 /* estimate the output size in pixels, can be larger than necessary */
3549 if (stat == Ok)
3551 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3552 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3553 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3554 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3556 for (i=1; i<flat_path->pathdata.Count; i++)
3558 REAL x, y;
3559 x = flat_path->pathdata.Points[i].X;
3560 y = flat_path->pathdata.Points[i].Y;
3562 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3563 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3564 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3565 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3568 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3571 if (stat == Ok)
3573 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3574 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3575 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3576 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3578 output_width = output_area.right - output_area.left + 1;
3579 output_height = output_area.bottom - output_area.top + 1;
3581 if (output_width <= 0 || output_height <= 0)
3583 GdipDeletePath(flat_path);
3584 return Ok;
3587 gp_output_area.X = output_area.left;
3588 gp_output_area.Y = output_area.top;
3589 gp_output_area.Width = output_width;
3590 gp_output_area.Height = output_height;
3592 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3593 if (!output_bits)
3594 stat = OutOfMemory;
3597 if (stat == Ok)
3599 if (pen->brush->bt != BrushTypeSolidColor)
3601 /* allocate and draw brush output */
3602 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3604 if (brush_bits)
3606 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3607 &gp_output_area, output_width);
3609 else
3610 stat = OutOfMemory;
3613 if (stat == Ok)
3615 /* convert dash pattern to bool array */
3616 switch (pen->dash)
3618 case DashStyleCustom:
3620 dash_pattern_size = 0;
3622 for (i=0; i < pen->numdashes; i++)
3623 dash_pattern_size += gdip_round(pen->dashes[i]);
3625 if (dash_pattern_size != 0)
3627 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3629 if (dyn_dash_pattern)
3631 int j=0;
3632 for (i=0; i < pen->numdashes; i++)
3634 int k;
3635 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3636 dyn_dash_pattern[j++] = (i&1)^1;
3639 else
3640 stat = OutOfMemory;
3642 break;
3644 /* else fall through */
3646 case DashStyleSolid:
3647 default:
3648 dash_pattern = static_dash_pattern;
3649 dash_pattern_size = 1;
3650 break;
3651 case DashStyleDash:
3652 dash_pattern = static_dash_pattern;
3653 dash_pattern_size = 4;
3654 break;
3655 case DashStyleDot:
3656 dash_pattern = &static_dash_pattern[4];
3657 dash_pattern_size = 2;
3658 break;
3659 case DashStyleDashDot:
3660 dash_pattern = static_dash_pattern;
3661 dash_pattern_size = 6;
3662 break;
3663 case DashStyleDashDotDot:
3664 dash_pattern = static_dash_pattern;
3665 dash_pattern_size = 8;
3666 break;
3670 if (stat == Ok)
3672 /* trace path */
3673 GpPointF subpath_start = flat_path->pathdata.Points[0];
3674 INT prev_x = INT_MAX, prev_y = INT_MAX;
3675 int dash_pos = dash_pattern_size - 1;
3677 for (i=0; i < flat_path->pathdata.Count; i++)
3679 BYTE type, type2;
3680 GpPointF start_point, end_point;
3681 GpPoint start_pointi, end_pointi;
3683 type = flat_path->pathdata.Types[i];
3684 if (i+1 < flat_path->pathdata.Count)
3685 type2 = flat_path->pathdata.Types[i+1];
3686 else
3687 type2 = PathPointTypeStart;
3689 start_point = flat_path->pathdata.Points[i];
3691 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3692 subpath_start = start_point;
3694 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3695 end_point = subpath_start;
3696 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3697 continue;
3698 else
3699 end_point = flat_path->pathdata.Points[i+1];
3701 start_pointi.X = floorf(start_point.X);
3702 start_pointi.Y = floorf(start_point.Y);
3703 end_pointi.X = floorf(end_point.X);
3704 end_pointi.Y = floorf(end_point.Y);
3706 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3707 continue;
3709 /* draw line segment */
3710 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3712 INT x, y, start_y, end_y, step;
3714 if (start_pointi.Y < end_pointi.Y)
3716 step = 1;
3717 start_y = ceilf(start_point.Y) - output_area.top;
3718 end_y = end_pointi.Y - output_area.top;
3720 else
3722 step = -1;
3723 start_y = start_point.Y - output_area.top;
3724 end_y = ceilf(end_point.Y) - output_area.top;
3727 for (y=start_y; y != (end_y+step); y+=step)
3729 x = gdip_round( start_point.X +
3730 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3731 - output_area.left;
3733 if (x == prev_x && y == prev_y)
3734 continue;
3736 prev_x = x;
3737 prev_y = y;
3738 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3740 if (!dash_pattern[dash_pos])
3741 continue;
3743 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3744 continue;
3746 if (brush_bits)
3747 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3748 else
3749 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3752 else
3754 INT x, y, start_x, end_x, step;
3756 if (start_pointi.X < end_pointi.X)
3758 step = 1;
3759 start_x = ceilf(start_point.X) - output_area.left;
3760 end_x = end_pointi.X - output_area.left;
3762 else
3764 step = -1;
3765 start_x = start_point.X - output_area.left;
3766 end_x = ceilf(end_point.X) - output_area.left;
3769 for (x=start_x; x != (end_x+step); x+=step)
3771 y = gdip_round( start_point.Y +
3772 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3773 - output_area.top;
3775 if (x == prev_x && y == prev_y)
3776 continue;
3778 prev_x = x;
3779 prev_y = y;
3780 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3782 if (!dash_pattern[dash_pos])
3783 continue;
3785 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3786 continue;
3788 if (brush_bits)
3789 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3790 else
3791 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3797 /* draw output image */
3798 if (stat == Ok)
3800 gdi_transform_acquire(graphics);
3802 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3803 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3804 PixelFormat32bppARGB);
3806 gdi_transform_release(graphics);
3809 heap_free(brush_bits);
3810 heap_free(dyn_dash_pattern);
3811 heap_free(output_bits);
3814 GdipDeletePath(flat_path);
3816 return stat;
3819 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3821 GpStatus stat;
3822 GpPath *wide_path;
3823 GpMatrix *transform=NULL;
3824 REAL flatness=1.0;
3826 /* Check if the final pen thickness in pixels is too thin. */
3827 if (pen->unit == UnitPixel)
3829 if (pen->width < 1.415)
3830 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3832 else
3834 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3836 points[1].X = pen->width;
3837 points[2].Y = pen->width;
3839 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3840 CoordinateSpaceWorld, points, 3);
3842 if (stat != Ok)
3843 return stat;
3845 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3846 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3847 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3848 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3849 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3852 stat = GdipClonePath(path, &wide_path);
3854 if (stat != Ok)
3855 return stat;
3857 if (pen->unit == UnitPixel)
3859 /* We have to transform this to device coordinates to get the widths right. */
3860 stat = GdipCreateMatrix(&transform);
3862 if (stat == Ok)
3863 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3864 CoordinateSpaceWorld, transform);
3866 else
3868 /* Set flatness based on the final coordinate space */
3869 GpMatrix t;
3871 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3872 CoordinateSpaceWorld, &t);
3874 if (stat != Ok)
3875 return stat;
3877 flatness = 1.0/sqrt(fmax(
3878 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
3879 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
3882 if (stat == Ok)
3883 stat = GdipWidenPath(wide_path, pen, transform, flatness);
3885 if (pen->unit == UnitPixel)
3887 /* Transform the path back to world coordinates */
3888 if (stat == Ok)
3889 stat = GdipInvertMatrix(transform);
3891 if (stat == Ok)
3892 stat = GdipTransformPath(wide_path, transform);
3895 /* Actually draw the path */
3896 if (stat == Ok)
3897 stat = GdipFillPath(graphics, pen->brush, wide_path);
3899 GdipDeleteMatrix(transform);
3901 GdipDeletePath(wide_path);
3903 return stat;
3906 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3908 GpStatus retval;
3910 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3912 if(!pen || !graphics)
3913 return InvalidParameter;
3915 if(graphics->busy)
3916 return ObjectBusy;
3918 if (path->pathdata.Count == 0)
3919 return Ok;
3921 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3922 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
3923 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
3924 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
3925 else
3926 retval = GDI32_GdipDrawPath(graphics, pen, path);
3928 return retval;
3931 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3932 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3934 GpStatus status;
3935 GpPath *path;
3937 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3938 width, height, startAngle, sweepAngle);
3940 if(!graphics || !pen)
3941 return InvalidParameter;
3943 if(graphics->busy)
3944 return ObjectBusy;
3946 status = GdipCreatePath(FillModeAlternate, &path);
3947 if (status != Ok) return status;
3949 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3950 if (status == Ok)
3951 status = GdipDrawPath(graphics, pen, path);
3953 GdipDeletePath(path);
3954 return status;
3957 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3958 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3960 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3961 width, height, startAngle, sweepAngle);
3963 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3966 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3967 REAL y, REAL width, REAL height)
3969 GpStatus status;
3970 GpPath *path;
3972 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3974 if(!pen || !graphics)
3975 return InvalidParameter;
3977 if(graphics->busy)
3978 return ObjectBusy;
3980 status = GdipCreatePath(FillModeAlternate, &path);
3981 if (status != Ok) return status;
3983 status = GdipAddPathRectangle(path, x, y, width, height);
3984 if (status == Ok)
3985 status = GdipDrawPath(graphics, pen, path);
3987 GdipDeletePath(path);
3988 return status;
3991 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3992 INT y, INT width, INT height)
3994 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3996 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3999 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
4000 GDIPCONST GpRectF* rects, INT count)
4002 GpStatus status;
4003 GpPath *path;
4005 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4007 if(!graphics || !pen || !rects || count < 1)
4008 return InvalidParameter;
4010 if(graphics->busy)
4011 return ObjectBusy;
4013 status = GdipCreatePath(FillModeAlternate, &path);
4014 if (status != Ok) return status;
4016 status = GdipAddPathRectangles(path, rects, count);
4017 if (status == Ok)
4018 status = GdipDrawPath(graphics, pen, path);
4020 GdipDeletePath(path);
4021 return status;
4024 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4025 GDIPCONST GpRect* rects, INT count)
4027 GpRectF *rectsF;
4028 GpStatus ret;
4029 INT i;
4031 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4033 if(!rects || count<=0)
4034 return InvalidParameter;
4036 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4037 if(!rectsF)
4038 return OutOfMemory;
4040 for(i = 0;i < count;i++){
4041 rectsF[i].X = (REAL)rects[i].X;
4042 rectsF[i].Y = (REAL)rects[i].Y;
4043 rectsF[i].Width = (REAL)rects[i].Width;
4044 rectsF[i].Height = (REAL)rects[i].Height;
4047 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4048 heap_free(rectsF);
4050 return ret;
4053 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4054 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4056 GpPath *path;
4057 GpStatus status;
4059 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4060 count, tension, fill);
4062 if(!graphics || !brush || !points)
4063 return InvalidParameter;
4065 if(graphics->busy)
4066 return ObjectBusy;
4068 if(count == 1) /* Do nothing */
4069 return Ok;
4071 status = GdipCreatePath(fill, &path);
4072 if (status != Ok) return status;
4074 status = GdipAddPathClosedCurve2(path, points, count, tension);
4075 if (status == Ok)
4076 status = GdipFillPath(graphics, brush, path);
4078 GdipDeletePath(path);
4079 return status;
4082 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4083 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4085 GpPointF *ptf;
4086 GpStatus stat;
4087 INT i;
4089 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4090 count, tension, fill);
4092 if(!points || count == 0)
4093 return InvalidParameter;
4095 if(count == 1) /* Do nothing */
4096 return Ok;
4098 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4099 if(!ptf)
4100 return OutOfMemory;
4102 for(i = 0;i < count;i++){
4103 ptf[i].X = (REAL)points[i].X;
4104 ptf[i].Y = (REAL)points[i].Y;
4107 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4109 heap_free(ptf);
4111 return stat;
4114 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4115 GDIPCONST GpPointF *points, INT count)
4117 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4118 return GdipFillClosedCurve2(graphics, brush, points, count,
4119 0.5f, FillModeAlternate);
4122 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4123 GDIPCONST GpPoint *points, INT count)
4125 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4126 return GdipFillClosedCurve2I(graphics, brush, points, count,
4127 0.5f, FillModeAlternate);
4130 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4131 REAL y, REAL width, REAL height)
4133 GpStatus stat;
4134 GpPath *path;
4136 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4138 if(!graphics || !brush)
4139 return InvalidParameter;
4141 if(graphics->busy)
4142 return ObjectBusy;
4144 stat = GdipCreatePath(FillModeAlternate, &path);
4146 if (stat == Ok)
4148 stat = GdipAddPathEllipse(path, x, y, width, height);
4150 if (stat == Ok)
4151 stat = GdipFillPath(graphics, brush, path);
4153 GdipDeletePath(path);
4156 return stat;
4159 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4160 INT y, INT width, INT height)
4162 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4164 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4167 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4169 INT save_state;
4170 GpStatus retval;
4171 HRGN hrgn=NULL;
4173 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4174 return NotImplemented;
4176 save_state = SaveDC(graphics->hdc);
4177 EndPath(graphics->hdc);
4178 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4179 : WINDING));
4181 retval = get_clip_hrgn(graphics, &hrgn);
4183 if (retval != Ok)
4184 goto end;
4186 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4188 gdi_transform_acquire(graphics);
4190 BeginPath(graphics->hdc);
4191 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4192 path->pathdata.Types, path->pathdata.Count, FALSE);
4194 if(retval == Ok)
4196 EndPath(graphics->hdc);
4197 brush_fill_path(graphics, brush);
4200 gdi_transform_release(graphics);
4202 end:
4203 RestoreDC(graphics->hdc, save_state);
4204 DeleteObject(hrgn);
4206 return retval;
4209 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4211 GpStatus stat;
4212 GpRegion *rgn;
4214 if (!brush_can_fill_pixels(brush))
4215 return NotImplemented;
4217 /* FIXME: This could probably be done more efficiently without regions. */
4219 stat = GdipCreateRegionPath(path, &rgn);
4221 if (stat == Ok)
4223 stat = GdipFillRegion(graphics, brush, rgn);
4225 GdipDeleteRegion(rgn);
4228 return stat;
4231 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4233 GpStatus stat = NotImplemented;
4235 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4237 if(!brush || !graphics || !path)
4238 return InvalidParameter;
4240 if(graphics->busy)
4241 return ObjectBusy;
4243 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4244 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4246 if (!graphics->image && !graphics->alpha_hdc)
4247 stat = GDI32_GdipFillPath(graphics, brush, path);
4249 if (stat == NotImplemented)
4250 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4252 if (stat == NotImplemented)
4254 FIXME("Not implemented for brushtype %i\n", brush->bt);
4255 stat = Ok;
4258 return stat;
4261 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4262 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4264 GpStatus stat;
4265 GpPath *path;
4267 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4268 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4270 if(!graphics || !brush)
4271 return InvalidParameter;
4273 if(graphics->busy)
4274 return ObjectBusy;
4276 stat = GdipCreatePath(FillModeAlternate, &path);
4278 if (stat == Ok)
4280 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4282 if (stat == Ok)
4283 stat = GdipFillPath(graphics, brush, path);
4285 GdipDeletePath(path);
4288 return stat;
4291 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4292 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4294 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4295 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4297 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4300 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4301 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4303 GpStatus stat;
4304 GpPath *path;
4306 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4308 if(!graphics || !brush || !points || !count)
4309 return InvalidParameter;
4311 if(graphics->busy)
4312 return ObjectBusy;
4314 stat = GdipCreatePath(fillMode, &path);
4316 if (stat == Ok)
4318 stat = GdipAddPathPolygon(path, points, count);
4320 if (stat == Ok)
4321 stat = GdipFillPath(graphics, brush, path);
4323 GdipDeletePath(path);
4326 return stat;
4329 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4330 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4332 GpStatus stat;
4333 GpPath *path;
4335 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4337 if(!graphics || !brush || !points || !count)
4338 return InvalidParameter;
4340 if(graphics->busy)
4341 return ObjectBusy;
4343 stat = GdipCreatePath(fillMode, &path);
4345 if (stat == Ok)
4347 stat = GdipAddPathPolygonI(path, points, count);
4349 if (stat == Ok)
4350 stat = GdipFillPath(graphics, brush, path);
4352 GdipDeletePath(path);
4355 return stat;
4358 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4359 GDIPCONST GpPointF *points, INT count)
4361 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4363 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4366 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4367 GDIPCONST GpPoint *points, INT count)
4369 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4371 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4374 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4375 REAL x, REAL y, REAL width, REAL height)
4377 GpRectF rect;
4379 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4381 rect.X = x;
4382 rect.Y = y;
4383 rect.Width = width;
4384 rect.Height = height;
4386 return GdipFillRectangles(graphics, brush, &rect, 1);
4389 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4390 INT x, INT y, INT width, INT height)
4392 GpRectF rect;
4394 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4396 rect.X = (REAL)x;
4397 rect.Y = (REAL)y;
4398 rect.Width = (REAL)width;
4399 rect.Height = (REAL)height;
4401 return GdipFillRectangles(graphics, brush, &rect, 1);
4404 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4405 INT count)
4407 GpStatus status;
4408 GpPath *path;
4410 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4412 if(!graphics || !brush || !rects || count <= 0)
4413 return InvalidParameter;
4415 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4417 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4418 /* FIXME: Add gdi32 drawing. */
4419 return status;
4422 status = GdipCreatePath(FillModeAlternate, &path);
4423 if (status != Ok) return status;
4425 status = GdipAddPathRectangles(path, rects, count);
4426 if (status == Ok)
4427 status = GdipFillPath(graphics, brush, path);
4429 GdipDeletePath(path);
4430 return status;
4433 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4434 INT count)
4436 GpRectF *rectsF;
4437 GpStatus ret;
4438 INT i;
4440 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4442 if(!rects || count <= 0)
4443 return InvalidParameter;
4445 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4446 if(!rectsF)
4447 return OutOfMemory;
4449 for(i = 0; i < count; i++){
4450 rectsF[i].X = (REAL)rects[i].X;
4451 rectsF[i].Y = (REAL)rects[i].Y;
4452 rectsF[i].Width = (REAL)rects[i].Width;
4453 rectsF[i].Height = (REAL)rects[i].Height;
4456 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4457 heap_free(rectsF);
4459 return ret;
4462 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4463 GpRegion* region)
4465 INT save_state;
4466 GpStatus status;
4467 HRGN hrgn;
4468 RECT rc;
4470 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4471 return NotImplemented;
4473 save_state = SaveDC(graphics->hdc);
4474 EndPath(graphics->hdc);
4476 hrgn = NULL;
4477 status = get_clip_hrgn(graphics, &hrgn);
4478 if (status != Ok)
4480 RestoreDC(graphics->hdc, save_state);
4481 return status;
4484 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4485 DeleteObject(hrgn);
4487 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4488 if (status != Ok)
4490 RestoreDC(graphics->hdc, save_state);
4491 return status;
4494 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4495 DeleteObject(hrgn);
4497 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4499 BeginPath(graphics->hdc);
4500 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4501 EndPath(graphics->hdc);
4503 brush_fill_path(graphics, brush);
4506 RestoreDC(graphics->hdc, save_state);
4509 return Ok;
4512 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4513 GpRegion* region)
4515 GpStatus stat;
4516 GpRegion *temp_region;
4517 GpMatrix world_to_device;
4518 GpRectF graphics_bounds;
4519 DWORD *pixel_data;
4520 HRGN hregion;
4521 RECT bound_rect;
4522 GpRect gp_bound_rect;
4524 if (!brush_can_fill_pixels(brush))
4525 return NotImplemented;
4527 stat = gdi_transform_acquire(graphics);
4529 if (stat == Ok)
4530 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4532 if (stat == Ok)
4533 stat = GdipCloneRegion(region, &temp_region);
4535 if (stat == Ok)
4537 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4538 CoordinateSpaceWorld, &world_to_device);
4540 if (stat == Ok)
4541 stat = GdipTransformRegion(temp_region, &world_to_device);
4543 if (stat == Ok)
4544 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4546 if (stat == Ok)
4547 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4549 GdipDeleteRegion(temp_region);
4552 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4554 DeleteObject(hregion);
4555 gdi_transform_release(graphics);
4556 return Ok;
4559 if (stat == Ok)
4561 gp_bound_rect.X = bound_rect.left;
4562 gp_bound_rect.Y = bound_rect.top;
4563 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4564 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4566 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4567 if (!pixel_data)
4568 stat = OutOfMemory;
4570 if (stat == Ok)
4572 stat = brush_fill_pixels(graphics, brush, pixel_data,
4573 &gp_bound_rect, gp_bound_rect.Width);
4575 if (stat == Ok)
4576 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4577 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4578 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4579 PixelFormat32bppARGB);
4581 heap_free(pixel_data);
4584 DeleteObject(hregion);
4587 gdi_transform_release(graphics);
4589 return stat;
4592 /*****************************************************************************
4593 * GdipFillRegion [GDIPLUS.@]
4595 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4596 GpRegion* region)
4598 GpStatus stat = NotImplemented;
4600 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4602 if (!(graphics && brush && region))
4603 return InvalidParameter;
4605 if(graphics->busy)
4606 return ObjectBusy;
4608 if (!graphics->image && !graphics->alpha_hdc)
4609 stat = GDI32_GdipFillRegion(graphics, brush, region);
4611 if (stat == NotImplemented)
4612 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4614 if (stat == NotImplemented)
4616 FIXME("not implemented for brushtype %i\n", brush->bt);
4617 stat = Ok;
4620 return stat;
4623 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4625 TRACE("(%p,%u)\n", graphics, intention);
4627 if(!graphics)
4628 return InvalidParameter;
4630 if(graphics->busy)
4631 return ObjectBusy;
4633 /* We have no internal operation queue, so there's no need to clear it. */
4635 if (graphics->hdc)
4636 GdiFlush();
4638 return Ok;
4641 /*****************************************************************************
4642 * GdipGetClipBounds [GDIPLUS.@]
4644 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4646 GpStatus status;
4647 GpRegion *clip;
4649 TRACE("(%p, %p)\n", graphics, rect);
4651 if(!graphics)
4652 return InvalidParameter;
4654 if(graphics->busy)
4655 return ObjectBusy;
4657 status = GdipCreateRegion(&clip);
4658 if (status != Ok) return status;
4660 status = GdipGetClip(graphics, clip);
4661 if (status == Ok)
4662 status = GdipGetRegionBounds(clip, graphics, rect);
4664 GdipDeleteRegion(clip);
4665 return status;
4668 /*****************************************************************************
4669 * GdipGetClipBoundsI [GDIPLUS.@]
4671 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4673 TRACE("(%p, %p)\n", graphics, rect);
4675 if(!graphics)
4676 return InvalidParameter;
4678 if(graphics->busy)
4679 return ObjectBusy;
4681 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4684 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4685 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4686 CompositingMode *mode)
4688 TRACE("(%p, %p)\n", graphics, mode);
4690 if(!graphics || !mode)
4691 return InvalidParameter;
4693 if(graphics->busy)
4694 return ObjectBusy;
4696 *mode = graphics->compmode;
4698 return Ok;
4701 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4702 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4703 CompositingQuality *quality)
4705 TRACE("(%p, %p)\n", graphics, quality);
4707 if(!graphics || !quality)
4708 return InvalidParameter;
4710 if(graphics->busy)
4711 return ObjectBusy;
4713 *quality = graphics->compqual;
4715 return Ok;
4718 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4719 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4720 InterpolationMode *mode)
4722 TRACE("(%p, %p)\n", graphics, mode);
4724 if(!graphics || !mode)
4725 return InvalidParameter;
4727 if(graphics->busy)
4728 return ObjectBusy;
4730 *mode = graphics->interpolation;
4732 return Ok;
4735 /* FIXME: Need to handle color depths less than 24bpp */
4736 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4738 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4740 if(!graphics || !argb)
4741 return InvalidParameter;
4743 if(graphics->busy)
4744 return ObjectBusy;
4746 return Ok;
4749 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4751 TRACE("(%p, %p)\n", graphics, scale);
4753 if(!graphics || !scale)
4754 return InvalidParameter;
4756 if(graphics->busy)
4757 return ObjectBusy;
4759 *scale = graphics->scale;
4761 return Ok;
4764 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4766 TRACE("(%p, %p)\n", graphics, unit);
4768 if(!graphics || !unit)
4769 return InvalidParameter;
4771 if(graphics->busy)
4772 return ObjectBusy;
4774 *unit = graphics->unit;
4776 return Ok;
4779 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4780 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4781 *mode)
4783 TRACE("(%p, %p)\n", graphics, mode);
4785 if(!graphics || !mode)
4786 return InvalidParameter;
4788 if(graphics->busy)
4789 return ObjectBusy;
4791 *mode = graphics->pixeloffset;
4793 return Ok;
4796 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4797 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4799 TRACE("(%p, %p)\n", graphics, mode);
4801 if(!graphics || !mode)
4802 return InvalidParameter;
4804 if(graphics->busy)
4805 return ObjectBusy;
4807 *mode = graphics->smoothing;
4809 return Ok;
4812 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4814 TRACE("(%p, %p)\n", graphics, contrast);
4816 if(!graphics || !contrast)
4817 return InvalidParameter;
4819 *contrast = graphics->textcontrast;
4821 return Ok;
4824 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4825 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4826 TextRenderingHint *hint)
4828 TRACE("(%p, %p)\n", graphics, hint);
4830 if(!graphics || !hint)
4831 return InvalidParameter;
4833 if(graphics->busy)
4834 return ObjectBusy;
4836 *hint = graphics->texthint;
4838 return Ok;
4841 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4843 GpRegion *clip_rgn;
4844 GpStatus stat;
4845 GpMatrix device_to_world;
4847 TRACE("(%p, %p)\n", graphics, rect);
4849 if(!graphics || !rect)
4850 return InvalidParameter;
4852 if(graphics->busy)
4853 return ObjectBusy;
4855 /* intersect window and graphics clipping regions */
4856 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4857 return stat;
4859 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4860 goto cleanup;
4862 /* transform to world coordinates */
4863 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4864 goto cleanup;
4866 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4867 goto cleanup;
4869 /* get bounds of the region */
4870 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4872 cleanup:
4873 GdipDeleteRegion(clip_rgn);
4875 return stat;
4878 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4880 GpRectF rectf;
4881 GpStatus stat;
4883 TRACE("(%p, %p)\n", graphics, rect);
4885 if(!graphics || !rect)
4886 return InvalidParameter;
4888 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4890 rect->X = gdip_round(rectf.X);
4891 rect->Y = gdip_round(rectf.Y);
4892 rect->Width = gdip_round(rectf.Width);
4893 rect->Height = gdip_round(rectf.Height);
4896 return stat;
4899 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4901 TRACE("(%p, %p)\n", graphics, matrix);
4903 if(!graphics || !matrix)
4904 return InvalidParameter;
4906 if(graphics->busy)
4907 return ObjectBusy;
4909 *matrix = graphics->worldtrans;
4910 return Ok;
4913 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4915 GpSolidFill *brush;
4916 GpStatus stat;
4917 GpRectF wnd_rect;
4919 TRACE("(%p, %x)\n", graphics, color);
4921 if(!graphics)
4922 return InvalidParameter;
4924 if(graphics->busy)
4925 return ObjectBusy;
4927 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4928 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
4930 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4931 return stat;
4933 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
4934 GdipDeleteBrush((GpBrush*)brush);
4935 return stat;
4938 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4939 wnd_rect.Width, wnd_rect.Height);
4941 GdipDeleteBrush((GpBrush*)brush);
4943 return Ok;
4946 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4948 TRACE("(%p, %p)\n", graphics, res);
4950 if(!graphics || !res)
4951 return InvalidParameter;
4953 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4956 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4958 GpStatus stat;
4959 GpRegion* rgn;
4960 GpPointF pt;
4962 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4964 if(!graphics || !result)
4965 return InvalidParameter;
4967 if(graphics->busy)
4968 return ObjectBusy;
4970 pt.X = x;
4971 pt.Y = y;
4972 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4973 CoordinateSpaceWorld, &pt, 1)) != Ok)
4974 return stat;
4976 if((stat = GdipCreateRegion(&rgn)) != Ok)
4977 return stat;
4979 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4980 goto cleanup;
4982 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4984 cleanup:
4985 GdipDeleteRegion(rgn);
4986 return stat;
4989 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4991 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4994 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4996 GpStatus stat;
4997 GpRegion* rgn;
4998 GpPointF pts[2];
5000 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
5002 if(!graphics || !result)
5003 return InvalidParameter;
5005 if(graphics->busy)
5006 return ObjectBusy;
5008 pts[0].X = x;
5009 pts[0].Y = y;
5010 pts[1].X = x + width;
5011 pts[1].Y = y + height;
5013 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5014 CoordinateSpaceWorld, pts, 2)) != Ok)
5015 return stat;
5017 pts[1].X -= pts[0].X;
5018 pts[1].Y -= pts[0].Y;
5020 if((stat = GdipCreateRegion(&rgn)) != Ok)
5021 return stat;
5023 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5024 goto cleanup;
5026 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5028 cleanup:
5029 GdipDeleteRegion(rgn);
5030 return stat;
5033 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5035 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5038 GpStatus gdip_format_string(HDC hdc,
5039 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5040 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5041 gdip_format_string_callback callback, void *user_data)
5043 WCHAR* stringdup;
5044 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5045 nheight, lineend, lineno = 0;
5046 RectF bounds;
5047 StringAlignment halign;
5048 GpStatus stat = Ok;
5049 SIZE size;
5050 HotkeyPrefix hkprefix;
5051 INT *hotkeyprefix_offsets=NULL;
5052 INT hotkeyprefix_count=0;
5053 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5054 BOOL seen_prefix = FALSE;
5056 if(length == -1) length = lstrlenW(string);
5058 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5059 if(!stringdup) return OutOfMemory;
5061 if (!format)
5062 format = &default_drawstring_format;
5064 nwidth = rect->Width;
5065 nheight = rect->Height;
5066 if (ignore_empty_clip)
5068 if (!nwidth) nwidth = INT_MAX;
5069 if (!nheight) nheight = INT_MAX;
5072 hkprefix = format->hkprefix;
5074 if (hkprefix == HotkeyPrefixShow)
5076 for (i=0; i<length; i++)
5078 if (string[i] == '&')
5079 hotkeyprefix_count++;
5083 if (hotkeyprefix_count)
5084 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5086 hotkeyprefix_count = 0;
5088 for(i = 0, j = 0; i < length; i++){
5089 /* FIXME: This makes the indexes passed to callback inaccurate. */
5090 if(!isprintW(string[i]) && (string[i] != '\n'))
5091 continue;
5093 /* FIXME: tabs should be handled using tabstops from stringformat */
5094 if (string[i] == '\t')
5095 continue;
5097 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5098 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5099 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5101 seen_prefix = TRUE;
5102 continue;
5105 seen_prefix = FALSE;
5107 stringdup[j] = string[i];
5108 j++;
5111 length = j;
5113 halign = format->align;
5115 while(sum < length){
5116 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5117 nwidth, &fit, NULL, &size);
5118 fitcpy = fit;
5120 if(fit == 0)
5121 break;
5123 for(lret = 0; lret < fit; lret++)
5124 if(*(stringdup + sum + lret) == '\n')
5125 break;
5127 /* Line break code (may look strange, but it imitates windows). */
5128 if(lret < fit)
5129 lineend = fit = lret; /* this is not an off-by-one error */
5130 else if(fit < (length - sum)){
5131 if(*(stringdup + sum + fit) == ' ')
5132 while(*(stringdup + sum + fit) == ' ')
5133 fit++;
5134 else
5135 while(*(stringdup + sum + fit - 1) != ' '){
5136 fit--;
5138 if(*(stringdup + sum + fit) == '\t')
5139 break;
5141 if(fit == 0){
5142 fit = fitcpy;
5143 break;
5146 lineend = fit;
5147 while(*(stringdup + sum + lineend - 1) == ' ' ||
5148 *(stringdup + sum + lineend - 1) == '\t')
5149 lineend--;
5151 else
5152 lineend = fit;
5154 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5155 nwidth, &j, NULL, &size);
5157 bounds.Width = size.cx;
5159 if(height + size.cy > nheight)
5161 if (format->attr & StringFormatFlagsLineLimit)
5162 break;
5163 bounds.Height = nheight - (height + size.cy);
5165 else
5166 bounds.Height = size.cy;
5168 bounds.Y = rect->Y + height;
5170 switch (halign)
5172 case StringAlignmentNear:
5173 default:
5174 bounds.X = rect->X;
5175 break;
5176 case StringAlignmentCenter:
5177 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5178 break;
5179 case StringAlignmentFar:
5180 bounds.X = rect->X + rect->Width - bounds.Width;
5181 break;
5184 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5185 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5186 break;
5188 stat = callback(hdc, stringdup, sum, lineend,
5189 font, rect, format, lineno, &bounds,
5190 &hotkeyprefix_offsets[hotkeyprefix_pos],
5191 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5193 if (stat != Ok)
5194 break;
5196 sum += fit + (lret < fitcpy ? 1 : 0);
5197 height += size.cy;
5198 lineno++;
5200 hotkeyprefix_pos = hotkeyprefix_end_pos;
5202 if(height > nheight)
5203 break;
5205 /* Stop if this was a linewrap (but not if it was a linebreak). */
5206 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5207 break;
5210 heap_free(stringdup);
5211 heap_free(hotkeyprefix_offsets);
5213 return stat;
5216 struct measure_ranges_args {
5217 GpRegion **regions;
5218 REAL rel_width, rel_height;
5221 static GpStatus measure_ranges_callback(HDC hdc,
5222 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5223 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5224 INT lineno, const RectF *bounds, INT *underlined_indexes,
5225 INT underlined_index_count, void *user_data)
5227 int i;
5228 GpStatus stat = Ok;
5229 struct measure_ranges_args *args = user_data;
5231 for (i=0; i<format->range_count; i++)
5233 INT range_start = max(index, format->character_ranges[i].First);
5234 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5235 if (range_start < range_end)
5237 GpRectF range_rect;
5238 SIZE range_size;
5240 range_rect.Y = bounds->Y / args->rel_height;
5241 range_rect.Height = bounds->Height / args->rel_height;
5243 GetTextExtentExPointW(hdc, string + index, range_start - index,
5244 INT_MAX, NULL, NULL, &range_size);
5245 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5247 GetTextExtentExPointW(hdc, string + index, range_end - index,
5248 INT_MAX, NULL, NULL, &range_size);
5249 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5251 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5252 if (stat != Ok)
5253 break;
5257 return stat;
5260 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5261 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5262 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5263 INT regionCount, GpRegion** regions)
5265 GpStatus stat;
5266 int i;
5267 HFONT gdifont, oldfont;
5268 struct measure_ranges_args args;
5269 HDC hdc, temp_hdc=NULL;
5270 GpPointF pt[3];
5271 RectF scaled_rect;
5272 REAL margin_x;
5274 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
5275 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5277 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5278 return InvalidParameter;
5280 if (regionCount < stringFormat->range_count)
5281 return InvalidParameter;
5283 if(!graphics->hdc)
5285 hdc = temp_hdc = CreateCompatibleDC(0);
5286 if (!temp_hdc) return OutOfMemory;
5288 else
5289 hdc = graphics->hdc;
5291 if (stringFormat->attr)
5292 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5294 pt[0].X = 0.0;
5295 pt[0].Y = 0.0;
5296 pt[1].X = 1.0;
5297 pt[1].Y = 0.0;
5298 pt[2].X = 0.0;
5299 pt[2].Y = 1.0;
5300 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5301 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5302 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5303 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5304 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5306 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5307 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5309 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5310 scaled_rect.Y = layoutRect->Y * args.rel_height;
5311 scaled_rect.Width = layoutRect->Width * args.rel_width;
5312 scaled_rect.Height = layoutRect->Height * args.rel_height;
5314 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5315 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5317 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5318 oldfont = SelectObject(hdc, gdifont);
5320 for (i=0; i<stringFormat->range_count; i++)
5322 stat = GdipSetEmpty(regions[i]);
5323 if (stat != Ok)
5324 return stat;
5327 args.regions = regions;
5329 gdi_transform_acquire(graphics);
5331 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5332 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5334 gdi_transform_release(graphics);
5336 SelectObject(hdc, oldfont);
5337 DeleteObject(gdifont);
5339 if (temp_hdc)
5340 DeleteDC(temp_hdc);
5342 return stat;
5345 struct measure_string_args {
5346 RectF *bounds;
5347 INT *codepointsfitted;
5348 INT *linesfilled;
5349 REAL rel_width, rel_height;
5352 static GpStatus measure_string_callback(HDC hdc,
5353 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5354 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5355 INT lineno, const RectF *bounds, INT *underlined_indexes,
5356 INT underlined_index_count, void *user_data)
5358 struct measure_string_args *args = user_data;
5359 REAL new_width, new_height;
5361 new_width = bounds->Width / args->rel_width;
5362 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5364 if (new_width > args->bounds->Width)
5365 args->bounds->Width = new_width;
5367 if (new_height > args->bounds->Height)
5368 args->bounds->Height = new_height;
5370 if (args->codepointsfitted)
5371 *args->codepointsfitted = index + length;
5373 if (args->linesfilled)
5374 (*args->linesfilled)++;
5376 return Ok;
5379 /* Find the smallest rectangle that bounds the text when it is printed in rect
5380 * according to the format options listed in format. If rect has 0 width and
5381 * height, then just find the smallest rectangle that bounds the text when it's
5382 * printed at location (rect->X, rect-Y). */
5383 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5384 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5385 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5386 INT *codepointsfitted, INT *linesfilled)
5388 HFONT oldfont, gdifont;
5389 struct measure_string_args args;
5390 HDC temp_hdc=NULL, hdc;
5391 GpPointF pt[3];
5392 RectF scaled_rect;
5393 REAL margin_x;
5394 INT lines, glyphs;
5396 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5397 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5398 bounds, codepointsfitted, linesfilled);
5400 if(!graphics || !string || !font || !rect || !bounds)
5401 return InvalidParameter;
5403 if(!graphics->hdc)
5405 hdc = temp_hdc = CreateCompatibleDC(0);
5406 if (!temp_hdc) return OutOfMemory;
5408 else
5409 hdc = graphics->hdc;
5411 if(linesfilled) *linesfilled = 0;
5412 if(codepointsfitted) *codepointsfitted = 0;
5414 if(format)
5415 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5417 pt[0].X = 0.0;
5418 pt[0].Y = 0.0;
5419 pt[1].X = 1.0;
5420 pt[1].Y = 0.0;
5421 pt[2].X = 0.0;
5422 pt[2].Y = 1.0;
5423 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5424 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5425 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5426 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5427 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5429 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5430 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5432 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5433 scaled_rect.Y = rect->Y * args.rel_height;
5434 scaled_rect.Width = rect->Width * args.rel_width;
5435 scaled_rect.Height = rect->Height * args.rel_height;
5436 if (scaled_rect.Width >= 0.5)
5438 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5439 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5442 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5443 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5445 get_font_hfont(graphics, font, format, &gdifont, NULL);
5446 oldfont = SelectObject(hdc, gdifont);
5448 bounds->X = rect->X;
5449 bounds->Y = rect->Y;
5450 bounds->Width = 0.0;
5451 bounds->Height = 0.0;
5453 args.bounds = bounds;
5454 args.codepointsfitted = &glyphs;
5455 args.linesfilled = &lines;
5456 lines = glyphs = 0;
5458 gdi_transform_acquire(graphics);
5460 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5461 measure_string_callback, &args);
5463 gdi_transform_release(graphics);
5465 if (linesfilled) *linesfilled = lines;
5466 if (codepointsfitted) *codepointsfitted = glyphs;
5468 if (lines)
5469 bounds->Width += margin_x * 2.0;
5471 SelectObject(hdc, oldfont);
5472 DeleteObject(gdifont);
5474 if (temp_hdc)
5475 DeleteDC(temp_hdc);
5477 return Ok;
5480 struct draw_string_args {
5481 GpGraphics *graphics;
5482 GDIPCONST GpBrush *brush;
5483 REAL x, y, rel_width, rel_height, ascent;
5486 static GpStatus draw_string_callback(HDC hdc,
5487 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5488 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5489 INT lineno, const RectF *bounds, INT *underlined_indexes,
5490 INT underlined_index_count, void *user_data)
5492 struct draw_string_args *args = user_data;
5493 PointF position;
5494 GpStatus stat;
5496 position.X = args->x + bounds->X / args->rel_width;
5497 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5499 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5500 args->brush, &position,
5501 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5503 if (stat == Ok && underlined_index_count)
5505 OUTLINETEXTMETRICW otm;
5506 REAL underline_y, underline_height;
5507 int i;
5509 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5511 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5512 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5514 for (i=0; i<underlined_index_count; i++)
5516 REAL start_x, end_x;
5517 SIZE text_size;
5518 INT ofs = underlined_indexes[i] - index;
5520 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5521 start_x = text_size.cx / args->rel_width;
5523 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5524 end_x = text_size.cx / args->rel_width;
5526 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5530 return stat;
5533 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5534 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5535 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5537 HRGN rgn = NULL;
5538 HFONT gdifont;
5539 GpPointF pt[3], rectcpy[4];
5540 POINT corners[4];
5541 REAL rel_width, rel_height, margin_x;
5542 INT save_state, format_flags = 0;
5543 REAL offsety = 0.0;
5544 struct draw_string_args args;
5545 RectF scaled_rect;
5546 HDC hdc, temp_hdc=NULL;
5547 TEXTMETRICW textmetric;
5549 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5550 length, font, debugstr_rectf(rect), format, brush);
5552 if(!graphics || !string || !font || !brush || !rect)
5553 return InvalidParameter;
5555 if(graphics->hdc)
5557 hdc = graphics->hdc;
5559 else
5561 hdc = temp_hdc = CreateCompatibleDC(0);
5564 if(format){
5565 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5567 format_flags = format->attr;
5569 /* Should be no need to explicitly test for StringAlignmentNear as
5570 * that is default behavior if no alignment is passed. */
5571 if(format->line_align != StringAlignmentNear){
5572 RectF bounds, in_rect = *rect;
5573 in_rect.Height = 0.0; /* avoid height clipping */
5574 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5576 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5578 if(format->line_align == StringAlignmentCenter)
5579 offsety = (rect->Height - bounds.Height) / 2;
5580 else if(format->line_align == StringAlignmentFar)
5581 offsety = (rect->Height - bounds.Height);
5583 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5586 save_state = SaveDC(hdc);
5588 pt[0].X = 0.0;
5589 pt[0].Y = 0.0;
5590 pt[1].X = 1.0;
5591 pt[1].Y = 0.0;
5592 pt[2].X = 0.0;
5593 pt[2].Y = 1.0;
5594 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5595 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5596 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5597 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5598 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5600 rectcpy[3].X = rectcpy[0].X = rect->X;
5601 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5602 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5603 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5604 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5605 round_points(corners, rectcpy, 4);
5607 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5608 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5610 scaled_rect.X = margin_x * rel_width;
5611 scaled_rect.Y = 0.0;
5612 scaled_rect.Width = rel_width * rect->Width;
5613 scaled_rect.Height = rel_height * rect->Height;
5614 if (scaled_rect.Width >= 0.5)
5616 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5617 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5620 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5621 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5623 if (!(format_flags & StringFormatFlagsNoClip) &&
5624 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5625 rect->Width > 0.0 && rect->Height > 0.0)
5627 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5628 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5629 SelectClipRgn(hdc, rgn);
5632 get_font_hfont(graphics, font, format, &gdifont, NULL);
5633 SelectObject(hdc, gdifont);
5635 args.graphics = graphics;
5636 args.brush = brush;
5638 args.x = rect->X;
5639 args.y = rect->Y + offsety;
5641 args.rel_width = rel_width;
5642 args.rel_height = rel_height;
5644 gdi_transform_acquire(graphics);
5646 GetTextMetricsW(hdc, &textmetric);
5647 args.ascent = textmetric.tmAscent / rel_height;
5649 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5650 draw_string_callback, &args);
5652 gdi_transform_release(graphics);
5654 DeleteObject(rgn);
5655 DeleteObject(gdifont);
5657 RestoreDC(hdc, save_state);
5659 DeleteDC(temp_hdc);
5661 return Ok;
5664 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5666 TRACE("(%p)\n", graphics);
5668 if(!graphics)
5669 return InvalidParameter;
5671 if(graphics->busy)
5672 return ObjectBusy;
5674 return GdipSetInfinite(graphics->clip);
5677 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5679 GpStatus stat;
5681 TRACE("(%p)\n", graphics);
5683 if(!graphics)
5684 return InvalidParameter;
5686 if(graphics->busy)
5687 return ObjectBusy;
5689 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5690 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5692 if (stat != Ok)
5693 return stat;
5696 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5699 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5700 GpMatrixOrder order)
5702 GpStatus stat;
5704 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5706 if(!graphics)
5707 return InvalidParameter;
5709 if(graphics->busy)
5710 return ObjectBusy;
5712 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5713 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5715 if (stat != Ok)
5716 return stat;
5719 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5722 static GpStatus begin_container(GpGraphics *graphics,
5723 GraphicsContainerType type, GraphicsContainer *state)
5725 GraphicsContainerItem *container;
5726 GpStatus sts;
5728 if(!graphics || !state)
5729 return InvalidParameter;
5731 sts = init_container(&container, graphics, type);
5732 if(sts != Ok)
5733 return sts;
5735 list_add_head(&graphics->containers, &container->entry);
5736 *state = graphics->contid = container->contid;
5738 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5739 if (type == BEGIN_CONTAINER)
5740 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5741 else
5742 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5745 return Ok;
5748 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5750 TRACE("(%p, %p)\n", graphics, state);
5751 return begin_container(graphics, SAVE_GRAPHICS, state);
5754 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5755 GraphicsContainer *state)
5757 TRACE("(%p, %p)\n", graphics, state);
5758 return begin_container(graphics, BEGIN_CONTAINER, state);
5761 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5763 GraphicsContainerItem *container;
5764 GpMatrix transform;
5765 GpStatus stat;
5766 GpRectF scaled_srcrect;
5767 REAL scale_x, scale_y;
5769 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5771 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5772 return InvalidParameter;
5774 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5775 if(stat != Ok)
5776 return stat;
5778 list_add_head(&graphics->containers, &container->entry);
5779 *state = graphics->contid = container->contid;
5781 scale_x = units_to_pixels(1.0, unit, graphics->xres);
5782 scale_y = units_to_pixels(1.0, unit, graphics->yres);
5784 scaled_srcrect.X = scale_x * srcrect->X;
5785 scaled_srcrect.Y = scale_y * srcrect->Y;
5786 scaled_srcrect.Width = scale_x * srcrect->Width;
5787 scaled_srcrect.Height = scale_y * srcrect->Height;
5789 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5790 transform.matrix[1] = 0.0;
5791 transform.matrix[2] = 0.0;
5792 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5793 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5794 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5796 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5798 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5799 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5802 return Ok;
5805 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5807 GpRectF dstrectf, srcrectf;
5809 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5811 if (!dstrect || !srcrect)
5812 return InvalidParameter;
5814 dstrectf.X = dstrect->X;
5815 dstrectf.Y = dstrect->Y;
5816 dstrectf.Width = dstrect->Width;
5817 dstrectf.Height = dstrect->Height;
5819 srcrectf.X = srcrect->X;
5820 srcrectf.Y = srcrect->Y;
5821 srcrectf.Width = srcrect->Width;
5822 srcrectf.Height = srcrect->Height;
5824 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5827 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5829 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5830 return NotImplemented;
5833 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5834 GraphicsContainer state)
5836 GpStatus sts;
5837 GraphicsContainerItem *container, *container2;
5839 if(!graphics)
5840 return InvalidParameter;
5842 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5843 if(container->contid == state && container->type == type)
5844 break;
5847 /* did not find a matching container */
5848 if(&container->entry == &graphics->containers)
5849 return Ok;
5851 sts = restore_container(graphics, container);
5852 if(sts != Ok)
5853 return sts;
5855 /* remove all of the containers on top of the found container */
5856 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5857 if(container->contid == state)
5858 break;
5859 list_remove(&container->entry);
5860 delete_container(container);
5863 list_remove(&container->entry);
5864 delete_container(container);
5866 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5867 if (type == BEGIN_CONTAINER)
5868 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
5869 else
5870 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
5873 return Ok;
5876 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5878 TRACE("(%p, %x)\n", graphics, state);
5879 return end_container(graphics, BEGIN_CONTAINER, state);
5882 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5884 TRACE("(%p, %x)\n", graphics, state);
5885 return end_container(graphics, SAVE_GRAPHICS, state);
5888 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5889 REAL sy, GpMatrixOrder order)
5891 GpStatus stat;
5893 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5895 if(!graphics)
5896 return InvalidParameter;
5898 if(graphics->busy)
5899 return ObjectBusy;
5901 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5902 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
5904 if (stat != Ok)
5905 return stat;
5908 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5911 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5912 CombineMode mode)
5914 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5916 if(!graphics || !srcgraphics)
5917 return InvalidParameter;
5919 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5922 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5923 CompositingMode mode)
5925 TRACE("(%p, %d)\n", graphics, mode);
5927 if(!graphics)
5928 return InvalidParameter;
5930 if(graphics->busy)
5931 return ObjectBusy;
5933 if(graphics->compmode == mode)
5934 return Ok;
5936 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5938 GpStatus stat;
5940 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5941 EmfPlusRecordTypeSetCompositingMode, mode);
5942 if(stat != Ok)
5943 return stat;
5946 graphics->compmode = mode;
5948 return Ok;
5951 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5952 CompositingQuality quality)
5954 TRACE("(%p, %d)\n", graphics, quality);
5956 if(!graphics)
5957 return InvalidParameter;
5959 if(graphics->busy)
5960 return ObjectBusy;
5962 if(graphics->compqual == quality)
5963 return Ok;
5965 if(graphics->image && graphics->image->type == ImageTypeMetafile)
5967 GpStatus stat;
5969 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
5970 EmfPlusRecordTypeSetCompositingQuality, quality);
5971 if(stat != Ok)
5972 return stat;
5975 graphics->compqual = quality;
5977 return Ok;
5980 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5981 InterpolationMode mode)
5983 TRACE("(%p, %d)\n", graphics, mode);
5985 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5986 return InvalidParameter;
5988 if(graphics->busy)
5989 return ObjectBusy;
5991 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5992 mode = InterpolationModeBilinear;
5994 if (mode == InterpolationModeHighQuality)
5995 mode = InterpolationModeHighQualityBicubic;
5997 if (mode == graphics->interpolation)
5998 return Ok;
6000 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6002 GpStatus stat;
6004 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6005 EmfPlusRecordTypeSetInterpolationMode, mode);
6006 if (stat != Ok)
6007 return stat;
6010 graphics->interpolation = mode;
6012 return Ok;
6015 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6017 GpStatus stat;
6019 TRACE("(%p, %.2f)\n", graphics, scale);
6021 if(!graphics || (scale <= 0.0))
6022 return InvalidParameter;
6024 if(graphics->busy)
6025 return ObjectBusy;
6027 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6029 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6030 if (stat != Ok)
6031 return stat;
6034 graphics->scale = scale;
6036 return Ok;
6039 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6041 GpStatus stat;
6043 TRACE("(%p, %d)\n", graphics, unit);
6045 if(!graphics)
6046 return InvalidParameter;
6048 if(graphics->busy)
6049 return ObjectBusy;
6051 if(unit == UnitWorld)
6052 return InvalidParameter;
6054 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6056 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6057 if (stat != Ok)
6058 return stat;
6061 graphics->unit = unit;
6063 return Ok;
6066 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6067 mode)
6069 TRACE("(%p, %d)\n", graphics, mode);
6071 if(!graphics)
6072 return InvalidParameter;
6074 if(graphics->busy)
6075 return ObjectBusy;
6077 if(graphics->pixeloffset == mode)
6078 return Ok;
6080 if(graphics->image && graphics->image->type == ImageTypeMetafile)
6082 GpStatus stat;
6084 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6085 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6086 if(stat != Ok)
6087 return stat;
6090 graphics->pixeloffset = mode;
6092 return Ok;
6095 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6097 static int calls;
6099 TRACE("(%p,%i,%i)\n", graphics, x, y);
6101 if (!(calls++))
6102 FIXME("value is unused in rendering\n");
6104 if (!graphics)
6105 return InvalidParameter;
6107 graphics->origin_x = x;
6108 graphics->origin_y = y;
6110 return Ok;
6113 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6115 TRACE("(%p,%p,%p)\n", graphics, x, y);
6117 if (!graphics || !x || !y)
6118 return InvalidParameter;
6120 *x = graphics->origin_x;
6121 *y = graphics->origin_y;
6123 return Ok;
6126 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6128 TRACE("(%p, %d)\n", graphics, mode);
6130 if(!graphics)
6131 return InvalidParameter;
6133 if(graphics->busy)
6134 return ObjectBusy;
6136 if(graphics->smoothing == mode)
6137 return Ok;
6139 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6140 GpStatus stat;
6141 BOOL antialias = (mode != SmoothingModeDefault &&
6142 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6144 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6145 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6146 if(stat != Ok)
6147 return stat;
6150 graphics->smoothing = mode;
6152 return Ok;
6155 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6157 TRACE("(%p, %d)\n", graphics, contrast);
6159 if(!graphics)
6160 return InvalidParameter;
6162 graphics->textcontrast = contrast;
6164 return Ok;
6167 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6168 TextRenderingHint hint)
6170 TRACE("(%p, %d)\n", graphics, hint);
6172 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6173 return InvalidParameter;
6175 if(graphics->busy)
6176 return ObjectBusy;
6178 if(graphics->texthint == hint)
6179 return Ok;
6181 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6182 GpStatus stat;
6184 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6185 EmfPlusRecordTypeSetTextRenderingHint, hint);
6186 if(stat != Ok)
6187 return stat;
6190 graphics->texthint = hint;
6192 return Ok;
6195 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6197 GpStatus stat;
6199 TRACE("(%p, %p)\n", graphics, matrix);
6201 if(!graphics || !matrix)
6202 return InvalidParameter;
6204 if(graphics->busy)
6205 return ObjectBusy;
6207 TRACE("%f,%f,%f,%f,%f,%f\n",
6208 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6209 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6211 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6212 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6214 if (stat != Ok)
6215 return stat;
6218 graphics->worldtrans = *matrix;
6220 return Ok;
6223 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6224 REAL dy, GpMatrixOrder order)
6226 GpStatus stat;
6228 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6230 if(!graphics)
6231 return InvalidParameter;
6233 if(graphics->busy)
6234 return ObjectBusy;
6236 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6237 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6239 if (stat != Ok)
6240 return stat;
6243 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6246 /*****************************************************************************
6247 * GdipSetClipHrgn [GDIPLUS.@]
6249 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6251 GpRegion *region;
6252 GpStatus status;
6253 GpMatrix transform;
6255 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6257 if(!graphics)
6258 return InvalidParameter;
6260 if(graphics->busy)
6261 return ObjectBusy;
6263 /* hrgn is in gdi32 device units */
6264 status = GdipCreateRegionHrgn(hrgn, &region);
6266 if (status == Ok)
6268 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6270 if (status == Ok)
6271 status = GdipTransformRegion(region, &transform);
6273 if (status == Ok)
6274 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6276 GdipDeleteRegion(region);
6278 return status;
6281 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6283 GpStatus status;
6284 GpPath *clip_path;
6286 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6288 if(!graphics)
6289 return InvalidParameter;
6291 if(graphics->busy)
6292 return ObjectBusy;
6294 status = GdipClonePath(path, &clip_path);
6295 if (status == Ok)
6297 GpMatrix world_to_device;
6299 get_graphics_transform(graphics, CoordinateSpaceDevice,
6300 CoordinateSpaceWorld, &world_to_device);
6301 status = GdipTransformPath(clip_path, &world_to_device);
6302 if (status == Ok)
6303 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6305 GdipDeletePath(clip_path);
6307 return status;
6310 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6311 REAL width, REAL height,
6312 CombineMode mode)
6314 GpStatus status;
6315 GpRectF rect;
6316 GpRegion *region;
6318 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6320 if(!graphics)
6321 return InvalidParameter;
6323 if(graphics->busy)
6324 return ObjectBusy;
6326 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6328 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6329 if (status != Ok)
6330 return status;
6333 rect.X = x;
6334 rect.Y = y;
6335 rect.Width = width;
6336 rect.Height = height;
6337 status = GdipCreateRegionRect(&rect, &region);
6338 if (status == Ok)
6340 GpMatrix world_to_device;
6341 BOOL identity;
6343 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6344 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6345 if (status == Ok && !identity)
6346 status = GdipTransformRegion(region, &world_to_device);
6347 if (status == Ok)
6348 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6350 GdipDeleteRegion(region);
6352 return status;
6355 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6356 INT width, INT height,
6357 CombineMode mode)
6359 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6361 if(!graphics)
6362 return InvalidParameter;
6364 if(graphics->busy)
6365 return ObjectBusy;
6367 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6370 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6371 CombineMode mode)
6373 GpStatus status;
6374 GpRegion *clip;
6376 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6378 if(!graphics || !region)
6379 return InvalidParameter;
6381 if(graphics->busy)
6382 return ObjectBusy;
6384 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6386 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6387 if (status != Ok)
6388 return status;
6391 status = GdipCloneRegion(region, &clip);
6392 if (status == Ok)
6394 GpMatrix world_to_device;
6395 BOOL identity;
6397 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6398 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6399 if (status == Ok && !identity)
6400 status = GdipTransformRegion(clip, &world_to_device);
6401 if (status == Ok)
6402 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6404 GdipDeleteRegion(clip);
6406 return status;
6409 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6410 INT count)
6412 GpStatus status;
6413 GpPath* path;
6415 TRACE("(%p, %p, %d)\n", graphics, points, count);
6417 if(!graphics || !pen || count<=0)
6418 return InvalidParameter;
6420 if(graphics->busy)
6421 return ObjectBusy;
6423 status = GdipCreatePath(FillModeAlternate, &path);
6424 if (status != Ok) return status;
6426 status = GdipAddPathPolygon(path, points, count);
6427 if (status == Ok)
6428 status = GdipDrawPath(graphics, pen, path);
6430 GdipDeletePath(path);
6432 return status;
6435 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6436 INT count)
6438 GpStatus ret;
6439 GpPointF *ptf;
6440 INT i;
6442 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6444 if(count<=0) return InvalidParameter;
6445 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6447 for(i = 0;i < count; i++){
6448 ptf[i].X = (REAL)points[i].X;
6449 ptf[i].Y = (REAL)points[i].Y;
6452 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6453 heap_free(ptf);
6455 return ret;
6458 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6460 TRACE("(%p, %p)\n", graphics, dpi);
6462 if(!graphics || !dpi)
6463 return InvalidParameter;
6465 if(graphics->busy)
6466 return ObjectBusy;
6468 *dpi = graphics->xres;
6469 return Ok;
6472 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6474 TRACE("(%p, %p)\n", graphics, dpi);
6476 if(!graphics || !dpi)
6477 return InvalidParameter;
6479 if(graphics->busy)
6480 return ObjectBusy;
6482 *dpi = graphics->yres;
6483 return Ok;
6486 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6487 GpMatrixOrder order)
6489 GpMatrix m;
6490 GpStatus ret;
6492 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6494 if(!graphics || !matrix)
6495 return InvalidParameter;
6497 if(graphics->busy)
6498 return ObjectBusy;
6500 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6501 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6503 if (ret != Ok)
6504 return ret;
6507 m = graphics->worldtrans;
6509 ret = GdipMultiplyMatrix(&m, matrix, order);
6510 if(ret == Ok)
6511 graphics->worldtrans = m;
6513 return ret;
6516 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6517 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6519 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6521 GpStatus stat=Ok;
6523 TRACE("(%p, %p)\n", graphics, hdc);
6525 if(!graphics || !hdc)
6526 return InvalidParameter;
6528 if(graphics->busy)
6529 return ObjectBusy;
6531 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6533 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6535 else if (!graphics->hdc ||
6536 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6538 /* Create a fake HDC and fill it with a constant color. */
6539 HDC temp_hdc;
6540 HBITMAP hbitmap;
6541 GpRectF bounds;
6542 BITMAPINFOHEADER bmih;
6543 int i;
6545 stat = get_graphics_bounds(graphics, &bounds);
6546 if (stat != Ok)
6547 return stat;
6549 graphics->temp_hbitmap_width = bounds.Width;
6550 graphics->temp_hbitmap_height = bounds.Height;
6552 bmih.biSize = sizeof(bmih);
6553 bmih.biWidth = graphics->temp_hbitmap_width;
6554 bmih.biHeight = -graphics->temp_hbitmap_height;
6555 bmih.biPlanes = 1;
6556 bmih.biBitCount = 32;
6557 bmih.biCompression = BI_RGB;
6558 bmih.biSizeImage = 0;
6559 bmih.biXPelsPerMeter = 0;
6560 bmih.biYPelsPerMeter = 0;
6561 bmih.biClrUsed = 0;
6562 bmih.biClrImportant = 0;
6564 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6565 (void**)&graphics->temp_bits, NULL, 0);
6566 if (!hbitmap)
6567 return GenericError;
6569 temp_hdc = CreateCompatibleDC(0);
6570 if (!temp_hdc)
6572 DeleteObject(hbitmap);
6573 return GenericError;
6576 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6577 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6579 SelectObject(temp_hdc, hbitmap);
6581 graphics->temp_hbitmap = hbitmap;
6582 *hdc = graphics->temp_hdc = temp_hdc;
6584 else
6586 *hdc = graphics->hdc;
6589 if (stat == Ok)
6590 graphics->busy = TRUE;
6592 return stat;
6595 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6597 GpStatus stat=Ok;
6599 TRACE("(%p, %p)\n", graphics, hdc);
6601 if(!graphics || !hdc || !graphics->busy)
6602 return InvalidParameter;
6604 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6606 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6608 else if (graphics->temp_hdc == hdc)
6610 DWORD* pos;
6611 int i;
6613 /* Find the pixels that have changed, and mark them as opaque. */
6614 pos = (DWORD*)graphics->temp_bits;
6615 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6617 if (*pos != DC_BACKGROUND_KEY)
6619 *pos |= 0xff000000;
6621 pos++;
6624 /* Write the changed pixels to the real target. */
6625 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6626 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6627 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6629 /* Clean up. */
6630 DeleteDC(graphics->temp_hdc);
6631 DeleteObject(graphics->temp_hbitmap);
6632 graphics->temp_hdc = NULL;
6633 graphics->temp_hbitmap = NULL;
6635 else if (hdc != graphics->hdc)
6637 stat = InvalidParameter;
6640 if (stat == Ok)
6641 graphics->busy = FALSE;
6643 return stat;
6646 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6648 GpRegion *clip;
6649 GpStatus status;
6650 GpMatrix device_to_world;
6652 TRACE("(%p, %p)\n", graphics, region);
6654 if(!graphics || !region)
6655 return InvalidParameter;
6657 if(graphics->busy)
6658 return ObjectBusy;
6660 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6661 return status;
6663 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6664 status = GdipTransformRegion(clip, &device_to_world);
6665 if (status != Ok)
6667 GdipDeleteRegion(clip);
6668 return status;
6671 /* free everything except root node and header */
6672 delete_element(&region->node);
6673 memcpy(region, clip, sizeof(GpRegion));
6674 heap_free(clip);
6676 return Ok;
6679 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6681 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6683 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6684 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6685 SetMapMode(graphics->hdc, MM_TEXT);
6686 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6687 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6689 graphics->gdi_transform_acquire_count++;
6690 return Ok;
6693 GpStatus gdi_transform_release(GpGraphics *graphics)
6695 if (graphics->gdi_transform_acquire_count <= 0)
6697 ERR("called without matching gdi_transform_acquire\n");
6698 return GenericError;
6700 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6702 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6704 graphics->gdi_transform_acquire_count--;
6705 return Ok;
6708 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6709 GpCoordinateSpace src_space, GpMatrix *matrix)
6711 GpStatus stat = Ok;
6712 REAL scale_x, scale_y;
6714 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6716 if (dst_space != src_space)
6718 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6719 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6721 if(graphics->unit != UnitDisplay)
6723 scale_x *= graphics->scale;
6724 scale_y *= graphics->scale;
6727 if (dst_space < src_space)
6729 /* transform towards world space */
6730 switch ((int)src_space)
6732 case WineCoordinateSpaceGdiDevice:
6734 GpMatrix gdixform;
6735 gdixform = graphics->gdi_transform;
6736 stat = GdipInvertMatrix(&gdixform);
6737 if (stat != Ok)
6738 break;
6739 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6740 if (dst_space == CoordinateSpaceDevice)
6741 break;
6742 /* else fall-through */
6744 case CoordinateSpaceDevice:
6745 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6746 if (dst_space == CoordinateSpacePage)
6747 break;
6748 /* else fall-through */
6749 case CoordinateSpacePage:
6751 GpMatrix inverted_transform = graphics->worldtrans;
6752 stat = GdipInvertMatrix(&inverted_transform);
6753 if (stat == Ok)
6754 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6755 break;
6759 else
6761 /* transform towards device space */
6762 switch ((int)src_space)
6764 case CoordinateSpaceWorld:
6765 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6766 if (dst_space == CoordinateSpacePage)
6767 break;
6768 /* else fall-through */
6769 case CoordinateSpacePage:
6770 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6771 if (dst_space == CoordinateSpaceDevice)
6772 break;
6773 /* else fall-through */
6774 case CoordinateSpaceDevice:
6776 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6777 break;
6782 return stat;
6785 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6786 GpCoordinateSpace src_space, GpPointF *points, INT count)
6788 GpMatrix matrix;
6789 GpStatus stat;
6791 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6792 if (stat != Ok) return stat;
6794 return GdipTransformMatrixPoints(&matrix, points, count);
6797 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6798 GpCoordinateSpace src_space, GpPointF *points, INT count)
6800 if(!graphics || !points || count <= 0 ||
6801 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6802 src_space < 0 || src_space > CoordinateSpaceDevice)
6803 return InvalidParameter;
6805 if(graphics->busy)
6806 return ObjectBusy;
6808 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6810 if (src_space == dst_space) return Ok;
6812 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6815 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6816 GpCoordinateSpace src_space, GpPoint *points, INT count)
6818 GpPointF *pointsF;
6819 GpStatus ret;
6820 INT i;
6822 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6824 if(count <= 0)
6825 return InvalidParameter;
6827 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6828 if(!pointsF)
6829 return OutOfMemory;
6831 for(i = 0; i < count; i++){
6832 pointsF[i].X = (REAL)points[i].X;
6833 pointsF[i].Y = (REAL)points[i].Y;
6836 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6838 if(ret == Ok)
6839 for(i = 0; i < count; i++){
6840 points[i].X = gdip_round(pointsF[i].X);
6841 points[i].Y = gdip_round(pointsF[i].Y);
6843 heap_free(pointsF);
6845 return ret;
6848 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6850 static int calls;
6852 TRACE("\n");
6854 if (!calls++)
6855 FIXME("stub\n");
6857 return NULL;
6860 /*****************************************************************************
6861 * GdipTranslateClip [GDIPLUS.@]
6863 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6865 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6867 if(!graphics)
6868 return InvalidParameter;
6870 if(graphics->busy)
6871 return ObjectBusy;
6873 return GdipTranslateRegion(graphics->clip, dx, dy);
6876 /*****************************************************************************
6877 * GdipTranslateClipI [GDIPLUS.@]
6879 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6881 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6883 if(!graphics)
6884 return InvalidParameter;
6886 if(graphics->busy)
6887 return ObjectBusy;
6889 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6893 /*****************************************************************************
6894 * GdipMeasureDriverString [GDIPLUS.@]
6896 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6897 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6898 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6900 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6901 HFONT hfont;
6902 HDC hdc;
6903 REAL min_x, min_y, max_x, max_y, x, y;
6904 int i;
6905 TEXTMETRICW textmetric;
6906 const WORD *glyph_indices;
6907 WORD *dynamic_glyph_indices=NULL;
6908 REAL rel_width, rel_height, ascent, descent;
6909 GpPointF pt[3];
6911 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6913 if (!graphics || !text || !font || !positions || !boundingBox)
6914 return InvalidParameter;
6916 if (length == -1)
6917 length = strlenW(text);
6919 if (length == 0)
6921 boundingBox->X = 0.0;
6922 boundingBox->Y = 0.0;
6923 boundingBox->Width = 0.0;
6924 boundingBox->Height = 0.0;
6927 if (flags & unsupported_flags)
6928 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6930 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6932 hdc = CreateCompatibleDC(0);
6933 SelectObject(hdc, hfont);
6935 GetTextMetricsW(hdc, &textmetric);
6937 pt[0].X = 0.0;
6938 pt[0].Y = 0.0;
6939 pt[1].X = 1.0;
6940 pt[1].Y = 0.0;
6941 pt[2].X = 0.0;
6942 pt[2].Y = 1.0;
6943 if (matrix)
6945 GpMatrix xform = *matrix;
6946 GdipTransformMatrixPoints(&xform, pt, 3);
6948 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
6949 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6950 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6951 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6952 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6954 if (flags & DriverStringOptionsCmapLookup)
6956 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
6957 if (!glyph_indices)
6959 DeleteDC(hdc);
6960 DeleteObject(hfont);
6961 return OutOfMemory;
6964 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6966 else
6967 glyph_indices = text;
6969 min_x = max_x = x = positions[0].X;
6970 min_y = max_y = y = positions[0].Y;
6972 ascent = textmetric.tmAscent / rel_height;
6973 descent = textmetric.tmDescent / rel_height;
6975 for (i=0; i<length; i++)
6977 int char_width;
6978 ABC abc;
6980 if (!(flags & DriverStringOptionsRealizedAdvance))
6982 x = positions[i].X;
6983 y = positions[i].Y;
6986 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6987 char_width = abc.abcA + abc.abcB + abc.abcC;
6989 if (min_y > y - ascent) min_y = y - ascent;
6990 if (max_y < y + descent) max_y = y + descent;
6991 if (min_x > x) min_x = x;
6993 x += char_width / rel_width;
6995 if (max_x < x) max_x = x;
6998 heap_free(dynamic_glyph_indices);
6999 DeleteDC(hdc);
7000 DeleteObject(hfont);
7002 boundingBox->X = min_x;
7003 boundingBox->Y = min_y;
7004 boundingBox->Width = max_x - min_x;
7005 boundingBox->Height = max_y - min_y;
7007 return Ok;
7010 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7011 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7012 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7013 INT flags, GDIPCONST GpMatrix *matrix)
7015 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
7016 INT save_state;
7017 GpPointF pt;
7018 HFONT hfont;
7019 UINT eto_flags=0;
7020 GpStatus status;
7021 HRGN hrgn;
7023 if (flags & unsupported_flags)
7024 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7026 if (!(flags & DriverStringOptionsCmapLookup))
7027 eto_flags |= ETO_GLYPH_INDEX;
7029 save_state = SaveDC(graphics->hdc);
7030 SetBkMode(graphics->hdc, TRANSPARENT);
7031 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7033 status = get_clip_hrgn(graphics, &hrgn);
7035 if (status == Ok)
7037 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7038 DeleteObject(hrgn);
7041 pt = positions[0];
7042 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7044 get_font_hfont(graphics, font, format, &hfont, matrix);
7045 SelectObject(graphics->hdc, hfont);
7047 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7049 gdi_transform_acquire(graphics);
7051 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
7053 gdi_transform_release(graphics);
7055 RestoreDC(graphics->hdc, save_state);
7057 DeleteObject(hfont);
7059 return Ok;
7062 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7063 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7064 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7065 INT flags, GDIPCONST GpMatrix *matrix)
7067 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7068 GpStatus stat;
7069 PointF *real_positions, real_position;
7070 POINT *pti;
7071 HFONT hfont;
7072 HDC hdc;
7073 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7074 DWORD max_glyphsize=0;
7075 GLYPHMETRICS glyphmetrics;
7076 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7077 BYTE *glyph_mask;
7078 BYTE *text_mask;
7079 int text_mask_stride;
7080 BYTE *pixel_data;
7081 int pixel_data_stride;
7082 GpRect pixel_area;
7083 UINT ggo_flags = GGO_GRAY8_BITMAP;
7085 if (length <= 0)
7086 return Ok;
7088 if (!(flags & DriverStringOptionsCmapLookup))
7089 ggo_flags |= GGO_GLYPH_INDEX;
7091 if (flags & unsupported_flags)
7092 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7094 pti = heap_alloc_zero(sizeof(POINT) * length);
7095 if (!pti)
7096 return OutOfMemory;
7098 if (flags & DriverStringOptionsRealizedAdvance)
7100 real_position = positions[0];
7102 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7103 round_points(pti, &real_position, 1);
7105 else
7107 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7108 if (!real_positions)
7110 heap_free(pti);
7111 return OutOfMemory;
7114 memcpy(real_positions, positions, sizeof(PointF) * length);
7116 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7117 round_points(pti, real_positions, length);
7119 heap_free(real_positions);
7122 get_font_hfont(graphics, font, format, &hfont, matrix);
7124 hdc = CreateCompatibleDC(0);
7125 SelectObject(hdc, hfont);
7127 /* Get the boundaries of the text to be drawn */
7128 for (i=0; i<length; i++)
7130 DWORD glyphsize;
7131 int left, top, right, bottom;
7133 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7134 &glyphmetrics, 0, NULL, &identity);
7136 if (glyphsize == GDI_ERROR)
7138 ERR("GetGlyphOutlineW failed\n");
7139 heap_free(pti);
7140 DeleteDC(hdc);
7141 DeleteObject(hfont);
7142 return GenericError;
7145 if (glyphsize > max_glyphsize)
7146 max_glyphsize = glyphsize;
7148 if (glyphsize != 0)
7150 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7151 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7152 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7153 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7155 if (left < min_x) min_x = left;
7156 if (top < min_y) min_y = top;
7157 if (right > max_x) max_x = right;
7158 if (bottom > max_y) max_y = bottom;
7161 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7163 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7164 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7168 if (max_glyphsize == 0)
7169 /* Nothing to draw. */
7170 return Ok;
7172 glyph_mask = heap_alloc_zero(max_glyphsize);
7173 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7174 text_mask_stride = max_x - min_x;
7176 if (!(glyph_mask && text_mask))
7178 heap_free(glyph_mask);
7179 heap_free(text_mask);
7180 heap_free(pti);
7181 DeleteDC(hdc);
7182 DeleteObject(hfont);
7183 return OutOfMemory;
7186 /* Generate a mask for the text */
7187 for (i=0; i<length; i++)
7189 DWORD ret;
7190 int left, top, stride;
7192 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7193 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7195 if (ret == GDI_ERROR || ret == 0)
7196 continue; /* empty glyph */
7198 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7199 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7200 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7202 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7204 BYTE *glyph_val = glyph_mask + y * stride;
7205 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7206 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7208 *text_val = min(64, *text_val + *glyph_val);
7209 glyph_val++;
7210 text_val++;
7215 heap_free(pti);
7216 DeleteDC(hdc);
7217 DeleteObject(hfont);
7218 heap_free(glyph_mask);
7220 /* get the brush data */
7221 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7222 if (!pixel_data)
7224 heap_free(text_mask);
7225 return OutOfMemory;
7228 pixel_area.X = min_x;
7229 pixel_area.Y = min_y;
7230 pixel_area.Width = max_x - min_x;
7231 pixel_area.Height = max_y - min_y;
7232 pixel_data_stride = pixel_area.Width * 4;
7234 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7235 if (stat != Ok)
7237 heap_free(text_mask);
7238 heap_free(pixel_data);
7239 return stat;
7242 /* multiply the brush data by the mask */
7243 for (y=0; y<pixel_area.Height; y++)
7245 BYTE *text_val = text_mask + text_mask_stride * y;
7246 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7247 for (x=0; x<pixel_area.Width; x++)
7249 *pixel_val = (*pixel_val) * (*text_val) / 64;
7250 text_val++;
7251 pixel_val+=4;
7255 heap_free(text_mask);
7257 gdi_transform_acquire(graphics);
7259 /* draw the result */
7260 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7261 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7263 gdi_transform_release(graphics);
7265 heap_free(pixel_data);
7267 return stat;
7270 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7271 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7272 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7273 INT flags, GDIPCONST GpMatrix *matrix)
7275 GpStatus stat = NotImplemented;
7277 if (length == -1)
7278 length = strlenW(text);
7280 if (graphics->hdc && !graphics->alpha_hdc &&
7281 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
7282 brush->bt == BrushTypeSolidColor &&
7283 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7284 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7285 brush, positions, flags, matrix);
7286 if (stat == NotImplemented)
7287 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7288 brush, positions, flags, matrix);
7289 return stat;
7292 /*****************************************************************************
7293 * GdipDrawDriverString [GDIPLUS.@]
7295 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7296 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7297 GDIPCONST PointF *positions, INT flags,
7298 GDIPCONST GpMatrix *matrix )
7300 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7302 if (!graphics || !text || !font || !brush || !positions)
7303 return InvalidParameter;
7305 return draw_driver_string(graphics, text, length, font, NULL,
7306 brush, positions, flags, matrix);
7309 /*****************************************************************************
7310 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7312 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7314 GpStatus stat;
7315 GpRegion* rgn;
7317 TRACE("(%p, %p)\n", graphics, res);
7319 if((stat = GdipCreateRegion(&rgn)) != Ok)
7320 return stat;
7322 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7323 goto cleanup;
7325 stat = GdipIsEmptyRegion(rgn, graphics, res);
7327 cleanup:
7328 GdipDeleteRegion(rgn);
7329 return stat;
7332 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7334 static int calls;
7336 TRACE("(%p) stub\n", graphics);
7338 if(!(calls++))
7339 FIXME("not implemented\n");
7341 return NotImplemented;
7344 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7346 TRACE("(%p, %p)\n", graphics, pabort);
7348 if (!graphics)
7349 return InvalidParameter;
7351 if (pabort)
7352 FIXME("Abort callback is not supported.\n");
7354 return Ok;