gdi32: Fix leak in GdiDeleteSpoolFileHandle.
[wine.git] / dlls / gdiplus / graphics.c
blob58b2de213a63a0aa0b6479529be010784ad46d99
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"
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "ocidl.h"
31 #include "olectl.h"
32 #include "ole2.h"
34 #include "winreg.h"
35 #include "shlwapi.h"
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
40 #include "wine/list.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44 /* looks-right constants */
45 #define ANCHOR_WIDTH (2.0)
46 #define MAX_ITERS (50)
48 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
49 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
50 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
51 INT flags, GDIPCONST GpMatrix *matrix);
53 /* Converts from gdiplus path point type to gdi path point type. */
54 static BYTE convert_path_point_type(BYTE type)
56 BYTE ret;
58 switch(type & PathPointTypePathTypeMask){
59 case PathPointTypeBezier:
60 ret = PT_BEZIERTO;
61 break;
62 case PathPointTypeLine:
63 ret = PT_LINETO;
64 break;
65 case PathPointTypeStart:
66 ret = PT_MOVETO;
67 break;
68 default:
69 ERR("Bad point type\n");
70 return 0;
73 if(type & PathPointTypeCloseSubpath)
74 ret |= PT_CLOSEFIGURE;
76 return ret;
79 static COLORREF get_gdi_brush_color(const GpBrush *brush)
81 ARGB argb;
83 switch (brush->bt)
85 case BrushTypeSolidColor:
87 const GpSolidFill *sf = (const GpSolidFill *)brush;
88 argb = sf->color;
89 break;
91 case BrushTypeHatchFill:
93 const GpHatch *hatch = (const GpHatch *)brush;
94 argb = hatch->forecol;
95 break;
97 case BrushTypeLinearGradient:
99 const GpLineGradient *line = (const GpLineGradient *)brush;
100 argb = line->startcolor;
101 break;
103 case BrushTypePathGradient:
105 const GpPathGradient *grad = (const GpPathGradient *)brush;
106 argb = grad->centercolor;
107 break;
109 default:
110 FIXME("unhandled brush type %d\n", brush->bt);
111 argb = 0;
112 break;
114 return ARGB2COLORREF(argb);
117 static BOOL is_metafile_graphics(const GpGraphics *graphics)
119 return graphics->image && graphics->image_type == ImageTypeMetafile;
122 static ARGB blend_colors(ARGB start, ARGB end, REAL position);
124 static void init_hatch_palette(ARGB *hatch_palette, ARGB fore_color, ARGB back_color)
126 /* Pass the center of a 45-degree diagonal line with width of one unit through the
127 * center of a unit square, and the portion of the square that will be covered will
128 * equal sqrt(2) - 1/2. The covered portion for adjacent squares will be 1/4. */
129 hatch_palette[0] = back_color;
130 hatch_palette[1] = blend_colors(back_color, fore_color, 0.25);
131 hatch_palette[2] = blend_colors(back_color, fore_color, sqrt(2.0) - 0.5);
132 hatch_palette[3] = fore_color;
135 static HBITMAP create_hatch_bitmap(const GpHatch *hatch, INT origin_x, INT origin_y)
137 HBITMAP hbmp;
138 BITMAPINFOHEADER bmih;
139 DWORD *bits;
140 int x, y;
142 bmih.biSize = sizeof(bmih);
143 bmih.biWidth = 8;
144 bmih.biHeight = 8;
145 bmih.biPlanes = 1;
146 bmih.biBitCount = 32;
147 bmih.biCompression = BI_RGB;
148 bmih.biSizeImage = 0;
150 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
151 if (hbmp)
153 const unsigned char *hatch_data;
155 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
157 ARGB hatch_palette[4];
158 init_hatch_palette(hatch_palette, hatch->forecol, hatch->backcol);
160 /* Anti-aliasing is only specified for diagonal hatch patterns.
161 * This implementation repeats the pattern, shifts as needed,
162 * then uses bitmask 1 to check the pixel value, and the 0x82
163 * bitmask to check the adjacent pixel values, to determine the
164 * degree of shading needed. */
165 for (y = 0; y < 8; y++)
167 const int hy = (y + origin_y) & 7;
168 const int hx = origin_x & 7;
169 unsigned int row = (0x10101 * hatch_data[hy]) >> hx;
171 for (x = 0; x < 8; x++, row >>= 1)
173 int index;
174 if (hatch_data[8])
175 index = (row & 1) ? 2 : (row & 0x82) ? 1 : 0;
176 else
177 index = (row & 1) ? 3 : 0;
178 bits[y * 8 + 7 - x] = hatch_palette[index];
182 else
184 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
186 for (y = 0; y < 64; y++)
187 bits[y] = hatch->forecol;
191 return hbmp;
194 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb, INT origin_x, INT origin_y)
196 switch (brush->bt)
198 case BrushTypeSolidColor:
200 const GpSolidFill *sf = (const GpSolidFill *)brush;
201 lb->lbStyle = BS_SOLID;
202 lb->lbColor = ARGB2COLORREF(sf->color);
203 lb->lbHatch = 0;
204 return Ok;
207 case BrushTypeHatchFill:
209 const GpHatch *hatch = (const GpHatch *)brush;
210 HBITMAP hbmp;
212 hbmp = create_hatch_bitmap(hatch, origin_x, origin_y);
213 if (!hbmp) return OutOfMemory;
215 lb->lbStyle = BS_PATTERN;
216 lb->lbColor = 0;
217 lb->lbHatch = (ULONG_PTR)hbmp;
218 return Ok;
221 default:
222 FIXME("unhandled brush type %d\n", brush->bt);
223 lb->lbStyle = BS_SOLID;
224 lb->lbColor = get_gdi_brush_color(brush);
225 lb->lbHatch = 0;
226 return Ok;
230 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
232 switch (lb->lbStyle)
234 case BS_PATTERN:
235 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
236 break;
238 return Ok;
241 static HBRUSH create_gdi_brush(const GpBrush *brush, INT origin_x, INT origin_y)
243 LOGBRUSH lb;
244 HBRUSH gdibrush;
246 if (create_gdi_logbrush(brush, &lb, origin_x, origin_y) != Ok) return 0;
248 gdibrush = CreateBrushIndirect(&lb);
249 free_gdi_logbrush(&lb);
251 return gdibrush;
254 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
256 LOGBRUSH lb;
257 HPEN gdipen;
258 REAL width;
259 INT save_state, i, numdashes;
260 GpPointF pt[2];
261 DWORD dash_array[MAX_DASHLEN];
263 save_state = SaveDC(graphics->hdc);
265 EndPath(graphics->hdc);
267 if(pen->unit == UnitPixel){
268 width = pen->width;
270 else{
271 REAL scale_x, scale_y;
272 /* Get an estimate for the amount the pen width is affected by the world
273 * transform. (This is similar to what some of the wine drivers do.) */
274 scale_x = graphics->worldtrans.matrix[0] + graphics->worldtrans.matrix[2];
275 scale_y = graphics->worldtrans.matrix[1] + graphics->worldtrans.matrix[3];
277 width = sqrt(scale_x * scale_x + scale_y * scale_y) / sqrt(2.0);
279 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit,
280 graphics->xres, graphics->printer_display);
281 width *= graphics->scale;
283 pt[0].X = 0.0;
284 pt[0].Y = 0.0;
285 pt[1].X = 1.0;
286 pt[1].Y = 1.0;
287 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, pt, 2);
288 width *= sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
289 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
292 if(pen->dash == DashStyleCustom){
293 numdashes = min(pen->numdashes, MAX_DASHLEN);
295 TRACE("dashes are: ");
296 for(i = 0; i < numdashes; i++){
297 dash_array[i] = gdip_round(width * pen->dashes[i]);
298 TRACE("%ld, ", dash_array[i]);
300 TRACE("\n and the pen style is %x\n", pen->style);
302 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
303 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
304 numdashes, dash_array);
305 free_gdi_logbrush(&lb);
307 else
309 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
310 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
311 free_gdi_logbrush(&lb);
314 SelectObject(graphics->hdc, gdipen);
316 return save_state;
319 static void restore_dc(GpGraphics *graphics, INT state)
321 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
322 RestoreDC(graphics->hdc, state);
325 static void round_points(POINT *pti, GpPointF *ptf, INT count)
327 int i;
329 for(i = 0; i < count; i++){
330 if(isnan(ptf[i].X))
331 pti[i].x = 0;
332 else
333 pti[i].x = gdip_round(ptf[i].X);
335 if(isnan(ptf[i].Y))
336 pti[i].y = 0;
337 else
338 pti[i].y = gdip_round(ptf[i].Y);
342 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
343 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
345 CompositingMode comp_mode;
346 INT technology = GetDeviceCaps(graphics->hdc, TECHNOLOGY);
347 INT shadeblendcaps = GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS);
349 GdipGetCompositingMode(graphics, &comp_mode);
351 if ((technology == DT_RASPRINTER && shadeblendcaps == SB_NONE)
352 || comp_mode == CompositingModeSourceCopy)
354 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
356 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
357 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
359 else
361 BLENDFUNCTION bf;
363 bf.BlendOp = AC_SRC_OVER;
364 bf.BlendFlags = 0;
365 bf.SourceConstantAlpha = 255;
366 bf.AlphaFormat = AC_SRC_ALPHA;
368 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
369 hdc, src_x, src_y, src_width, src_height, bf);
373 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
375 GpRegion *rgn;
376 GpMatrix transform;
377 GpStatus stat;
378 BOOL identity;
380 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, &transform);
382 if (stat == Ok)
383 stat = GdipIsMatrixIdentity(&transform, &identity);
385 if (stat == Ok)
386 stat = GdipCloneRegion(graphics->clip, &rgn);
388 if (stat == Ok)
390 if (!identity)
391 stat = GdipTransformRegion(rgn, &transform);
393 if (stat == Ok)
394 stat = GdipGetRegionHRgn(rgn, NULL, hrgn);
396 GdipDeleteRegion(rgn);
399 if (stat == Ok && graphics->gdi_clip)
401 if (*hrgn)
402 CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
403 else
405 *hrgn = CreateRectRgn(0,0,0,0);
406 CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
410 return stat;
413 /* Draw ARGB data to the given graphics object */
414 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
415 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
417 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
418 INT x, y;
419 CompositingMode comp_mode = graphics->compmode;
421 for (y=0; y<src_height; y++)
423 for (x=0; x<src_width; x++)
425 ARGB dst_color, src_color;
426 src_color = ((ARGB*)(src + src_stride * y))[x];
428 if (comp_mode == CompositingModeSourceCopy)
430 if (!(src_color & 0xff000000))
431 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, 0);
432 else
433 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, src_color);
435 else
437 if (!(src_color & 0xff000000))
438 continue;
440 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
441 if (fmt & PixelFormatPAlpha)
442 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
443 else
444 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
449 return Ok;
452 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
453 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
455 HDC hdc;
456 HBITMAP hbitmap;
457 BITMAPINFOHEADER bih;
458 BYTE *temp_bits;
460 hdc = CreateCompatibleDC(0);
462 bih.biSize = sizeof(BITMAPINFOHEADER);
463 bih.biWidth = src_width;
464 bih.biHeight = -src_height;
465 bih.biPlanes = 1;
466 bih.biBitCount = 32;
467 bih.biCompression = BI_RGB;
468 bih.biSizeImage = 0;
469 bih.biXPelsPerMeter = 0;
470 bih.biYPelsPerMeter = 0;
471 bih.biClrUsed = 0;
472 bih.biClrImportant = 0;
474 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
475 (void**)&temp_bits, NULL, 0);
477 if(!hbitmap || !temp_bits)
478 goto done;
480 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
481 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
482 fmt & PixelFormatPAlpha)
483 memcpy(temp_bits, src, src_width * src_height * 4);
484 else
485 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
486 4 * src_width, src, src_stride);
488 SelectObject(hdc, hbitmap);
489 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
490 hdc, 0, 0, src_width, src_height);
492 DeleteObject(hbitmap);
494 done:
495 DeleteDC(hdc);
497 return Ok;
500 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
501 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
503 GpStatus stat=Ok;
505 if (graphics->image && graphics->image->type == ImageTypeBitmap)
507 DWORD i;
508 int size;
509 RGNDATA *rgndata;
510 RECT *rects;
511 HRGN hrgn, visible_rgn;
513 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
514 if (!hrgn)
515 return OutOfMemory;
517 stat = get_clip_hrgn(graphics, &visible_rgn);
518 if (stat != Ok)
520 DeleteObject(hrgn);
521 return stat;
524 if (visible_rgn)
526 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
527 DeleteObject(visible_rgn);
530 if (hregion)
531 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
533 size = GetRegionData(hrgn, 0, NULL);
535 rgndata = heap_alloc_zero(size);
536 if (!rgndata)
538 DeleteObject(hrgn);
539 return OutOfMemory;
542 GetRegionData(hrgn, size, rgndata);
544 rects = (RECT*)rgndata->Buffer;
546 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
548 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
549 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
550 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
551 src_stride, fmt);
554 heap_free(rgndata);
556 DeleteObject(hrgn);
558 return stat;
560 else if (is_metafile_graphics(graphics))
562 ERR("This should not be used for metafiles; fix caller\n");
563 return NotImplemented;
565 else
567 HRGN hrgn;
568 int save;
570 stat = get_clip_hrgn(graphics, &hrgn);
572 if (stat != Ok)
573 return stat;
575 save = SaveDC(graphics->hdc);
577 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
579 if (hregion)
580 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
582 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
583 src_height, src_stride, fmt);
585 RestoreDC(graphics->hdc, save);
587 DeleteObject(hrgn);
589 return stat;
593 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
594 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
596 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
599 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
601 INT start_a, end_a, final_a;
602 INT pos;
604 pos = gdip_round(position * 0xff);
606 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
607 end_a = ((end >> 24) & 0xff) * pos;
609 final_a = start_a + end_a;
611 if (final_a < 0xff) return 0;
613 return (final_a / 0xff) << 24 |
614 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
615 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
616 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
619 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
621 REAL blendfac;
623 /* clamp to between 0.0 and 1.0, using the wrap mode */
624 position = (position - brush->rect.X) / brush->rect.Width;
625 if (brush->wrap == WrapModeTile)
627 position = fmodf(position, 1.0f);
628 if (position < 0.0f) position += 1.0f;
630 else /* WrapModeFlip* */
632 position = fmodf(position, 2.0f);
633 if (position < 0.0f) position += 2.0f;
634 if (position > 1.0f) position = 2.0f - position;
637 if (brush->blendcount == 1)
638 blendfac = position;
639 else
641 int i=1;
642 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
643 REAL range;
645 /* locate the blend positions surrounding this position */
646 while (position > brush->blendpos[i])
647 i++;
649 /* interpolate between the blend positions */
650 left_blendpos = brush->blendpos[i-1];
651 left_blendfac = brush->blendfac[i-1];
652 right_blendpos = brush->blendpos[i];
653 right_blendfac = brush->blendfac[i];
654 range = right_blendpos - left_blendpos;
655 blendfac = (left_blendfac * (right_blendpos - position) +
656 right_blendfac * (position - left_blendpos)) / range;
659 if (brush->pblendcount == 0)
660 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
661 else
663 int i=1;
664 ARGB left_blendcolor, right_blendcolor;
665 REAL left_blendpos, right_blendpos;
667 /* locate the blend colors surrounding this position */
668 while (blendfac > brush->pblendpos[i])
669 i++;
671 /* interpolate between the blend colors */
672 left_blendpos = brush->pblendpos[i-1];
673 left_blendcolor = brush->pblendcolor[i-1];
674 right_blendpos = brush->pblendpos[i];
675 right_blendcolor = brush->pblendcolor[i];
676 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
677 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
681 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
683 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
684 BOOL identity = TRUE;
685 int i, j;
687 for (i=0; i<4; i++)
688 for (j=0; j<5; j++)
690 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
691 identity = FALSE;
692 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
695 return identity;
698 static ARGB transform_color(ARGB color, int matrix[5][5])
700 int val[5], res[4];
701 int i, j;
702 unsigned char a, r, g, b;
704 val[0] = ((color >> 16) & 0xff); /* red */
705 val[1] = ((color >> 8) & 0xff); /* green */
706 val[2] = (color & 0xff); /* blue */
707 val[3] = ((color >> 24) & 0xff); /* alpha */
708 val[4] = 255; /* translation */
710 for (i=0; i<4; i++)
712 res[i] = 0;
714 for (j=0; j<5; j++)
715 res[i] += matrix[j][i] * val[j];
718 a = min(max(res[3] / 256, 0), 255);
719 r = min(max(res[0] / 256, 0), 255);
720 g = min(max(res[1] / 256, 0), 255);
721 b = min(max(res[2] / 256, 0), 255);
723 return (a << 24) | (r << 16) | (g << 8) | b;
726 static BOOL color_is_gray(ARGB color)
728 unsigned char r, g, b;
730 r = (color >> 16) & 0xff;
731 g = (color >> 8) & 0xff;
732 b = color & 0xff;
734 return (r == g) && (g == b);
737 /* returns preferred pixel format for the applied attributes */
738 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
739 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
741 UINT x, y;
742 INT i;
744 if ((attributes->noop[type] == IMAGEATTR_NOOP_UNDEFINED &&
745 attributes->noop[ColorAdjustTypeDefault] == IMAGEATTR_NOOP_SET) ||
746 (attributes->noop[type] == IMAGEATTR_NOOP_SET))
747 return fmt;
749 if (attributes->colorkeys[type].enabled ||
750 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
752 const struct color_key *key;
753 BYTE min_blue, min_green, min_red;
754 BYTE max_blue, max_green, max_red;
756 if (!data || fmt != PixelFormat32bppARGB)
757 return PixelFormat32bppARGB;
759 if (attributes->colorkeys[type].enabled)
760 key = &attributes->colorkeys[type];
761 else
762 key = &attributes->colorkeys[ColorAdjustTypeDefault];
764 min_blue = key->low&0xff;
765 min_green = (key->low>>8)&0xff;
766 min_red = (key->low>>16)&0xff;
768 max_blue = key->high&0xff;
769 max_green = (key->high>>8)&0xff;
770 max_red = (key->high>>16)&0xff;
772 for (y=0; y<height; y++)
773 for (x=0; x<width; x++)
775 ARGB *src_color;
776 BYTE blue, green, red;
777 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
778 blue = *src_color&0xff;
779 green = (*src_color>>8)&0xff;
780 red = (*src_color>>16)&0xff;
781 if (blue >= min_blue && green >= min_green && red >= min_red &&
782 blue <= max_blue && green <= max_green && red <= max_red)
783 *src_color = 0x00000000;
787 if (attributes->colorremaptables[type].enabled ||
788 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
790 const struct color_remap_table *table;
792 if (!data || fmt != PixelFormat32bppARGB)
793 return PixelFormat32bppARGB;
795 if (attributes->colorremaptables[type].enabled)
796 table = &attributes->colorremaptables[type];
797 else
798 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
800 for (y=0; y<height; y++)
801 for (x=0; x<width; x++)
803 ARGB *src_color;
804 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
805 for (i=0; i<table->mapsize; i++)
807 if (*src_color == table->colormap[i].oldColor.Argb)
809 *src_color = table->colormap[i].newColor.Argb;
810 break;
816 if (attributes->colormatrices[type].enabled ||
817 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
819 const struct color_matrix *colormatrices;
820 int color_matrix[5][5];
821 int gray_matrix[5][5];
822 BOOL identity;
824 if (!data || fmt != PixelFormat32bppARGB)
825 return PixelFormat32bppARGB;
827 if (attributes->colormatrices[type].enabled)
828 colormatrices = &attributes->colormatrices[type];
829 else
830 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
832 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
834 if (colormatrices->flags == ColorMatrixFlagsAltGray)
835 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
837 if (!identity)
839 for (y=0; y<height; y++)
841 for (x=0; x<width; x++)
843 ARGB *src_color;
844 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
846 if (colormatrices->flags == ColorMatrixFlagsDefault ||
847 !color_is_gray(*src_color))
849 *src_color = transform_color(*src_color, color_matrix);
851 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
853 *src_color = transform_color(*src_color, gray_matrix);
860 if (attributes->gamma_enabled[type] ||
861 attributes->gamma_enabled[ColorAdjustTypeDefault])
863 REAL gamma;
865 if (!data || fmt != PixelFormat32bppARGB)
866 return PixelFormat32bppARGB;
868 if (attributes->gamma_enabled[type])
869 gamma = attributes->gamma[type];
870 else
871 gamma = attributes->gamma[ColorAdjustTypeDefault];
873 for (y=0; y<height; y++)
874 for (x=0; x<width; x++)
876 ARGB *src_color;
877 BYTE blue, green, red;
878 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
880 blue = *src_color&0xff;
881 green = (*src_color>>8)&0xff;
882 red = (*src_color>>16)&0xff;
884 /* FIXME: We should probably use a table for this. */
885 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
886 green = floorf(powf(green / 255.0, gamma) * 255.0);
887 red = floorf(powf(red / 255.0, gamma) * 255.0);
889 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
893 return fmt;
896 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
897 * bitmap that contains all the pixels we may need to draw it. */
898 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
899 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
900 GpRect *rect)
902 INT left, top, right, bottom;
904 switch (interpolation)
906 case InterpolationModeHighQualityBilinear:
907 case InterpolationModeHighQualityBicubic:
908 /* FIXME: Include a greater range for the prefilter? */
909 case InterpolationModeBicubic:
910 case InterpolationModeBilinear:
911 left = (INT)(floorf(srcx));
912 top = (INT)(floorf(srcy));
913 right = (INT)(ceilf(srcx+srcwidth));
914 bottom = (INT)(ceilf(srcy+srcheight));
915 break;
916 case InterpolationModeNearestNeighbor:
917 default:
918 left = gdip_round(srcx);
919 top = gdip_round(srcy);
920 right = gdip_round(srcx+srcwidth);
921 bottom = gdip_round(srcy+srcheight);
922 break;
925 if (wrap == WrapModeClamp)
927 if (left < 0)
928 left = 0;
929 if (top < 0)
930 top = 0;
931 if (right >= bitmap->width)
932 right = bitmap->width-1;
933 if (bottom >= bitmap->height)
934 bottom = bitmap->height-1;
935 if (bottom < top || right < left)
936 /* entirely outside image, just sample a pixel so we don't have to
937 * special-case this later */
938 left = top = right = bottom = 0;
940 else
942 /* In some cases we can make the rectangle smaller here, but the logic
943 * is hard to get right, and tiling suggests we're likely to use the
944 * entire source image. */
945 if (left < 0 || right >= bitmap->width)
947 left = 0;
948 right = bitmap->width-1;
951 if (top < 0 || bottom >= bitmap->height)
953 top = 0;
954 bottom = bitmap->height-1;
958 rect->X = left;
959 rect->Y = top;
960 rect->Width = right - left + 1;
961 rect->Height = bottom - top + 1;
964 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
965 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
967 if (attributes->wrap == WrapModeClamp)
969 if (x < 0 || y < 0 || x >= width || y >= height)
970 return attributes->outside_color;
972 else
974 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
975 if (x < 0)
976 x = width*2 + x % (INT)(width * 2);
977 if (y < 0)
978 y = height*2 + y % (INT)(height * 2);
980 if (attributes->wrap & WrapModeTileFlipX)
982 if ((x / width) % 2 == 0)
983 x = x % width;
984 else
985 x = width - 1 - x % width;
987 else
988 x = x % width;
990 if (attributes->wrap & WrapModeTileFlipY)
992 if ((y / height) % 2 == 0)
993 y = y % height;
994 else
995 y = height - 1 - y % height;
997 else
998 y = y % height;
1001 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
1003 ERR("out of range pixel requested\n");
1004 return 0xffcd0084;
1007 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
1010 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
1011 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
1012 InterpolationMode interpolation, PixelOffsetMode offset_mode)
1014 static int fixme;
1016 switch (interpolation)
1018 default:
1019 if (!fixme++)
1020 FIXME("Unimplemented interpolation %i\n", interpolation);
1021 /* fall-through */
1022 case InterpolationModeBilinear:
1024 REAL leftxf, topyf;
1025 INT leftx, rightx, topy, bottomy;
1026 ARGB topleft, topright, bottomleft, bottomright;
1027 ARGB top, bottom;
1028 float x_offset;
1030 leftxf = floorf(point->X);
1031 leftx = (INT)leftxf;
1032 rightx = (INT)ceilf(point->X);
1033 topyf = floorf(point->Y);
1034 topy = (INT)topyf;
1035 bottomy = (INT)ceilf(point->Y);
1037 if (leftx == rightx && topy == bottomy)
1038 return sample_bitmap_pixel(src_rect, bits, width, height,
1039 leftx, topy, attributes);
1041 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
1042 leftx, topy, attributes);
1043 topright = sample_bitmap_pixel(src_rect, bits, width, height,
1044 rightx, topy, attributes);
1045 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
1046 leftx, bottomy, attributes);
1047 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
1048 rightx, bottomy, attributes);
1050 x_offset = point->X - leftxf;
1051 top = blend_colors(topleft, topright, x_offset);
1052 bottom = blend_colors(bottomleft, bottomright, x_offset);
1054 return blend_colors(top, bottom, point->Y - topyf);
1056 case InterpolationModeNearestNeighbor:
1058 FLOAT pixel_offset;
1059 switch (offset_mode)
1061 default:
1062 case PixelOffsetModeNone:
1063 case PixelOffsetModeHighSpeed:
1064 pixel_offset = 0.5;
1065 break;
1067 case PixelOffsetModeHalf:
1068 case PixelOffsetModeHighQuality:
1069 pixel_offset = 0.0;
1070 break;
1072 return sample_bitmap_pixel(src_rect, bits, width, height,
1073 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1079 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1081 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1084 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1085 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1087 switch (brush->bt)
1089 case BrushTypeSolidColor:
1091 if (is_fill)
1092 return TRUE;
1093 else
1095 /* cannot draw semi-transparent colors */
1096 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1099 case BrushTypeHatchFill:
1101 GpHatch *hatch = (GpHatch*)brush;
1102 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1103 ((hatch->backcol & 0xff000000) == 0xff000000);
1105 case BrushTypeLinearGradient:
1106 case BrushTypeTextureFill:
1107 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1108 default:
1109 return FALSE;
1113 static GpStatus brush_fill_path(GpGraphics *graphics, GpBrush *brush)
1115 GpStatus status = Ok;
1116 switch (brush->bt)
1118 case BrushTypeSolidColor:
1120 GpSolidFill *fill = (GpSolidFill*)brush;
1121 HBITMAP bmp = ARGB2BMP(fill->color);
1123 if (bmp)
1125 RECT rc;
1126 /* partially transparent fill */
1128 if (!SelectClipPath(graphics->hdc, RGN_AND))
1130 status = GenericError;
1131 DeleteObject(bmp);
1132 break;
1134 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1136 HDC hdc = CreateCompatibleDC(NULL);
1138 if (!hdc)
1140 status = OutOfMemory;
1141 DeleteObject(bmp);
1142 break;
1145 SelectObject(hdc, bmp);
1146 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1147 hdc, 0, 0, 1, 1);
1148 DeleteDC(hdc);
1151 DeleteObject(bmp);
1152 break;
1154 /* else fall through */
1156 default:
1158 HBRUSH gdibrush, old_brush;
1160 gdibrush = create_gdi_brush(brush, graphics->origin_x, graphics->origin_y);
1161 if (!gdibrush)
1163 status = OutOfMemory;
1164 break;
1167 old_brush = SelectObject(graphics->hdc, gdibrush);
1168 FillPath(graphics->hdc);
1169 SelectObject(graphics->hdc, old_brush);
1170 DeleteObject(gdibrush);
1171 break;
1175 return status;
1178 static BOOL brush_can_fill_pixels(GpBrush *brush)
1180 switch (brush->bt)
1182 case BrushTypeSolidColor:
1183 case BrushTypeHatchFill:
1184 case BrushTypeLinearGradient:
1185 case BrushTypeTextureFill:
1186 case BrushTypePathGradient:
1187 return TRUE;
1188 default:
1189 return FALSE;
1193 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1194 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1196 switch (brush->bt)
1198 case BrushTypeSolidColor:
1200 int x, y;
1201 GpSolidFill *fill = (GpSolidFill*)brush;
1202 for (y=0; y<fill_area->Height; y++)
1203 for (x=0; x<fill_area->Width; x++)
1204 argb_pixels[x + y*cdwStride] = fill->color;
1205 return Ok;
1207 case BrushTypeHatchFill:
1209 int x, y;
1210 GpHatch *fill = (GpHatch*)brush;
1211 const unsigned char *hatch_data;
1212 ARGB hatch_palette[4];
1214 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1215 return NotImplemented;
1217 init_hatch_palette(hatch_palette, fill->forecol, fill->backcol);
1219 /* See create_hatch_bitmap for an explanation of how index is derived. */
1220 for (y = 0; y < fill_area->Height; y++, argb_pixels += cdwStride)
1222 const int hy = ~(y + fill_area->Y - graphics->origin_y) & 7;
1223 const int hx = graphics->origin_x & 7;
1224 const unsigned int row = (0x10101 * hatch_data[hy]) >> hx;
1226 for (x = 0; x < fill_area->Width; x++)
1228 const unsigned int srow = row >> (~(x + fill_area->X) & 7);
1229 int index;
1230 if (hatch_data[8])
1231 index = (srow & 1) ? 2 : (srow & 0x82) ? 1 : 0;
1232 else
1233 index = (srow & 1) ? 3 : 0;
1235 argb_pixels[x] = hatch_palette[index];
1239 return Ok;
1241 case BrushTypeLinearGradient:
1243 GpLineGradient *fill = (GpLineGradient*)brush;
1244 GpPointF draw_points[3];
1245 GpStatus stat;
1246 int x, y;
1248 draw_points[0].X = fill_area->X;
1249 draw_points[0].Y = fill_area->Y;
1250 draw_points[1].X = fill_area->X+1;
1251 draw_points[1].Y = fill_area->Y;
1252 draw_points[2].X = fill_area->X;
1253 draw_points[2].Y = fill_area->Y+1;
1255 /* Transform the points to a co-ordinate space where X is the point's
1256 * position in the gradient, 0.0 being the start point and 1.0 the
1257 * end point. */
1258 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1259 WineCoordinateSpaceGdiDevice, draw_points, 3);
1261 if (stat == Ok)
1263 GpMatrix world_to_gradient = fill->transform;
1265 stat = GdipInvertMatrix(&world_to_gradient);
1266 if (stat == Ok)
1267 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1270 if (stat == Ok)
1272 REAL x_delta = draw_points[1].X - draw_points[0].X;
1273 REAL y_delta = draw_points[2].X - draw_points[0].X;
1275 for (y=0; y<fill_area->Height; y++)
1277 for (x=0; x<fill_area->Width; x++)
1279 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1281 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1286 return stat;
1288 case BrushTypeTextureFill:
1290 GpTexture *fill = (GpTexture*)brush;
1291 GpPointF draw_points[3];
1292 GpStatus stat;
1293 int x, y;
1294 GpBitmap *bitmap;
1295 int src_stride;
1296 GpRect src_area;
1298 if (fill->image->type != ImageTypeBitmap)
1300 FIXME("metafile texture brushes not implemented\n");
1301 return NotImplemented;
1304 bitmap = (GpBitmap*)fill->image;
1305 src_stride = sizeof(ARGB) * bitmap->width;
1307 src_area.X = src_area.Y = 0;
1308 src_area.Width = bitmap->width;
1309 src_area.Height = bitmap->height;
1311 draw_points[0].X = fill_area->X;
1312 draw_points[0].Y = fill_area->Y;
1313 draw_points[1].X = fill_area->X+1;
1314 draw_points[1].Y = fill_area->Y;
1315 draw_points[2].X = fill_area->X;
1316 draw_points[2].Y = fill_area->Y+1;
1318 /* Transform the points to the co-ordinate space of the bitmap. */
1319 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1320 WineCoordinateSpaceGdiDevice, draw_points, 3);
1322 if (stat == Ok)
1324 GpMatrix world_to_texture = fill->transform;
1326 stat = GdipInvertMatrix(&world_to_texture);
1327 if (stat == Ok)
1328 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1331 if (stat == Ok && !fill->bitmap_bits)
1333 BitmapData lockeddata;
1335 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1336 if (!fill->bitmap_bits)
1337 stat = OutOfMemory;
1339 if (stat == Ok)
1341 lockeddata.Width = bitmap->width;
1342 lockeddata.Height = bitmap->height;
1343 lockeddata.Stride = src_stride;
1344 lockeddata.PixelFormat = PixelFormat32bppARGB;
1345 lockeddata.Scan0 = fill->bitmap_bits;
1347 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1348 PixelFormat32bppARGB, &lockeddata);
1351 if (stat == Ok)
1352 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1354 if (stat == Ok)
1355 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1356 bitmap->width, bitmap->height,
1357 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1359 if (stat != Ok)
1361 heap_free(fill->bitmap_bits);
1362 fill->bitmap_bits = NULL;
1366 if (stat == Ok)
1368 REAL x_dx = draw_points[1].X - draw_points[0].X;
1369 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1370 REAL y_dx = draw_points[2].X - draw_points[0].X;
1371 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1373 for (y=0; y<fill_area->Height; y++)
1375 for (x=0; x<fill_area->Width; x++)
1377 GpPointF point;
1378 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1379 point.Y = draw_points[0].Y + x * x_dy + y * y_dy;
1381 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1382 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1383 &point, fill->imageattributes, graphics->interpolation,
1384 graphics->pixeloffset);
1389 return stat;
1391 case BrushTypePathGradient:
1393 GpPathGradient *fill = (GpPathGradient*)brush;
1394 GpPath *flat_path;
1395 GpMatrix world_to_device;
1396 GpStatus stat;
1397 int i, figure_start=0;
1398 GpPointF start_point, end_point, center_point;
1399 BYTE type;
1400 REAL min_yf, max_yf, line1_xf, line2_xf;
1401 INT min_y, max_y, min_x, max_x;
1402 INT x, y;
1403 ARGB outer_color;
1404 static BOOL transform_fixme_once;
1406 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1408 static int once;
1409 if (!once++)
1410 FIXME("path gradient focus not implemented\n");
1413 if (fill->gamma)
1415 static int once;
1416 if (!once++)
1417 FIXME("path gradient gamma correction not implemented\n");
1420 if (fill->blendcount)
1422 static int once;
1423 if (!once++)
1424 FIXME("path gradient blend not implemented\n");
1427 if (fill->pblendcount)
1429 static int once;
1430 if (!once++)
1431 FIXME("path gradient preset blend not implemented\n");
1434 if (!transform_fixme_once)
1436 BOOL is_identity=TRUE;
1437 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1438 if (!is_identity)
1440 FIXME("path gradient transform not implemented\n");
1441 transform_fixme_once = TRUE;
1445 stat = GdipClonePath(fill->path, &flat_path);
1447 if (stat != Ok)
1448 return stat;
1450 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1451 CoordinateSpaceWorld, &world_to_device);
1452 if (stat == Ok)
1454 stat = GdipTransformPath(flat_path, &world_to_device);
1456 if (stat == Ok)
1458 center_point = fill->center;
1459 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1462 if (stat == Ok)
1463 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1466 if (stat != Ok)
1468 GdipDeletePath(flat_path);
1469 return stat;
1472 for (i=0; i<flat_path->pathdata.Count; i++)
1474 int start_center_line=0, end_center_line=0;
1475 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1476 REAL center_distance;
1477 ARGB start_color, end_color;
1478 REAL dy, dx;
1480 type = flat_path->pathdata.Types[i];
1482 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1483 figure_start = i;
1485 start_point = flat_path->pathdata.Points[i];
1487 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1489 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1491 end_point = flat_path->pathdata.Points[figure_start];
1492 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1494 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1496 end_point = flat_path->pathdata.Points[i+1];
1497 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1499 else
1500 continue;
1502 outer_color = start_color;
1504 min_yf = center_point.Y;
1505 if (min_yf > start_point.Y) min_yf = start_point.Y;
1506 if (min_yf > end_point.Y) min_yf = end_point.Y;
1508 if (min_yf < fill_area->Y)
1509 min_y = fill_area->Y;
1510 else
1511 min_y = (INT)ceil(min_yf);
1513 max_yf = center_point.Y;
1514 if (max_yf < start_point.Y) max_yf = start_point.Y;
1515 if (max_yf < end_point.Y) max_yf = end_point.Y;
1517 if (max_yf > fill_area->Y + fill_area->Height)
1518 max_y = fill_area->Y + fill_area->Height;
1519 else
1520 max_y = (INT)ceil(max_yf);
1522 dy = end_point.Y - start_point.Y;
1523 dx = end_point.X - start_point.X;
1525 /* This is proportional to the distance from start-end line to center point. */
1526 center_distance = dy * (start_point.X - center_point.X) +
1527 dx * (center_point.Y - start_point.Y);
1529 for (y=min_y; y<max_y; y++)
1531 REAL yf = (REAL)y;
1533 if (!seen_start && yf >= start_point.Y)
1535 seen_start = TRUE;
1536 start_center_line ^= 1;
1538 if (!seen_end && yf >= end_point.Y)
1540 seen_end = TRUE;
1541 end_center_line ^= 1;
1543 if (!seen_center && yf >= center_point.Y)
1545 seen_center = TRUE;
1546 start_center_line ^= 1;
1547 end_center_line ^= 1;
1550 if (start_center_line)
1551 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1552 else
1553 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1555 if (end_center_line)
1556 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1557 else
1558 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1560 if (line1_xf < line2_xf)
1562 min_x = (INT)ceil(line1_xf);
1563 max_x = (INT)ceil(line2_xf);
1565 else
1567 min_x = (INT)ceil(line2_xf);
1568 max_x = (INT)ceil(line1_xf);
1571 if (min_x < fill_area->X)
1572 min_x = fill_area->X;
1573 if (max_x > fill_area->X + fill_area->Width)
1574 max_x = fill_area->X + fill_area->Width;
1576 for (x=min_x; x<max_x; x++)
1578 REAL xf = (REAL)x;
1579 REAL distance;
1581 if (start_color != end_color)
1583 REAL blend_amount, pdy, pdx;
1584 pdy = yf - center_point.Y;
1585 pdx = xf - center_point.X;
1587 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1589 /* Too close to center point, don't try to calculate outer color */
1590 outer_color = start_color;
1592 else
1594 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1595 outer_color = blend_colors(start_color, end_color, blend_amount);
1599 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1600 (end_point.X - start_point.X) * (yf - start_point.Y);
1602 distance = distance / center_distance;
1604 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1605 blend_colors(outer_color, fill->centercolor, distance);
1610 GdipDeletePath(flat_path);
1611 return stat;
1613 default:
1614 return NotImplemented;
1618 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1619 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1620 * should not be called on an hdc that has a path you care about. */
1621 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1622 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1624 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1625 GpMatrix matrix;
1626 HBRUSH brush = NULL;
1627 HPEN pen = NULL;
1628 PointF ptf[4], *custptf = NULL;
1629 POINT pt[4], *custpt = NULL;
1630 BYTE *tp = NULL;
1631 REAL theta, dsmall, dbig, dx, dy = 0.0;
1632 INT i, count;
1633 LOGBRUSH lb;
1634 BOOL customstroke;
1636 if((x1 == x2) && (y1 == y2))
1637 return;
1639 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1641 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1642 if(!customstroke){
1643 brush = CreateSolidBrush(color);
1644 lb.lbStyle = BS_SOLID;
1645 lb.lbColor = color;
1646 lb.lbHatch = 0;
1647 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1648 PS_JOIN_MITER, 1, &lb, 0,
1649 NULL);
1650 oldbrush = SelectObject(graphics->hdc, brush);
1651 oldpen = SelectObject(graphics->hdc, pen);
1654 switch(cap){
1655 case LineCapFlat:
1656 break;
1657 case LineCapSquare:
1658 case LineCapSquareAnchor:
1659 case LineCapDiamondAnchor:
1660 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1661 if(cap == LineCapDiamondAnchor){
1662 dsmall = cos(theta + M_PI_2) * size;
1663 dbig = sin(theta + M_PI_2) * size;
1665 else{
1666 dsmall = cos(theta + M_PI_4) * size;
1667 dbig = sin(theta + M_PI_4) * size;
1670 ptf[0].X = x2 - dsmall;
1671 ptf[1].X = x2 + dbig;
1673 ptf[0].Y = y2 - dbig;
1674 ptf[3].Y = y2 + dsmall;
1676 ptf[1].Y = y2 - dsmall;
1677 ptf[2].Y = y2 + dbig;
1679 ptf[3].X = x2 - dbig;
1680 ptf[2].X = x2 + dsmall;
1682 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1684 round_points(pt, ptf, 4);
1686 Polygon(graphics->hdc, pt, 4);
1688 break;
1689 case LineCapArrowAnchor:
1690 size = size * 4.0 / sqrt(3.0);
1692 dx = cos(M_PI / 6.0 + theta) * size;
1693 dy = sin(M_PI / 6.0 + theta) * size;
1695 ptf[0].X = x2 - dx;
1696 ptf[0].Y = y2 - dy;
1698 dx = cos(- M_PI / 6.0 + theta) * size;
1699 dy = sin(- M_PI / 6.0 + theta) * size;
1701 ptf[1].X = x2 - dx;
1702 ptf[1].Y = y2 - dy;
1704 ptf[2].X = x2;
1705 ptf[2].Y = y2;
1707 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1709 round_points(pt, ptf, 3);
1711 Polygon(graphics->hdc, pt, 3);
1713 break;
1714 case LineCapRoundAnchor:
1715 dx = dy = ANCHOR_WIDTH * size / 2.0;
1717 ptf[0].X = x2 - dx;
1718 ptf[0].Y = y2 - dy;
1719 ptf[1].X = x2 + dx;
1720 ptf[1].Y = y2 + dy;
1722 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 2);
1724 round_points(pt, ptf, 2);
1726 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1728 break;
1729 case LineCapTriangle:
1730 size = size / 2.0;
1731 dx = cos(M_PI_2 + theta) * size;
1732 dy = sin(M_PI_2 + theta) * size;
1734 ptf[0].X = x2 - dx;
1735 ptf[0].Y = y2 - dy;
1736 ptf[1].X = x2 + dx;
1737 ptf[1].Y = y2 + dy;
1739 dx = cos(theta) * size;
1740 dy = sin(theta) * size;
1742 ptf[2].X = x2 + dx;
1743 ptf[2].Y = y2 + dy;
1745 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1747 round_points(pt, ptf, 3);
1749 Polygon(graphics->hdc, pt, 3);
1751 break;
1752 case LineCapRound:
1753 dx = dy = size / 2.0;
1755 ptf[0].X = x2 - dx;
1756 ptf[0].Y = y2 - dy;
1757 ptf[1].X = x2 + dx;
1758 ptf[1].Y = y2 + dy;
1760 dx = -cos(M_PI_2 + theta) * size;
1761 dy = -sin(M_PI_2 + theta) * size;
1763 ptf[2].X = x2 - dx;
1764 ptf[2].Y = y2 - dy;
1765 ptf[3].X = x2 + dx;
1766 ptf[3].Y = y2 + dy;
1768 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1770 round_points(pt, ptf, 4);
1772 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1773 pt[2].y, pt[3].x, pt[3].y);
1775 break;
1776 case LineCapCustom:
1777 if(!custom)
1778 break;
1780 if (custom->type == CustomLineCapTypeAdjustableArrow)
1782 GpAdjustableArrowCap *arrow = (GpAdjustableArrowCap *)custom;
1783 if (arrow->cap.fill && arrow->height <= 0.0)
1784 break;
1787 count = custom->pathdata.Count;
1788 custptf = heap_alloc_zero(count * sizeof(PointF));
1789 custpt = heap_alloc_zero(count * sizeof(POINT));
1790 tp = heap_alloc_zero(count);
1792 if(!custptf || !custpt || !tp)
1793 goto custend;
1795 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1797 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1798 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1799 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1800 MatrixOrderAppend);
1801 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1802 GdipTransformMatrixPoints(&matrix, custptf, count);
1804 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, custptf, count);
1806 round_points(custpt, custptf, count);
1808 for(i = 0; i < count; i++)
1809 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1811 if(custom->fill){
1812 BeginPath(graphics->hdc);
1813 PolyDraw(graphics->hdc, custpt, tp, count);
1814 EndPath(graphics->hdc);
1815 StrokeAndFillPath(graphics->hdc);
1817 else
1818 PolyDraw(graphics->hdc, custpt, tp, count);
1820 custend:
1821 heap_free(custptf);
1822 heap_free(custpt);
1823 heap_free(tp);
1824 break;
1825 default:
1826 break;
1829 if(!customstroke){
1830 SelectObject(graphics->hdc, oldbrush);
1831 SelectObject(graphics->hdc, oldpen);
1832 DeleteObject(brush);
1833 DeleteObject(pen);
1837 /* Shortens the line by the given percent by changing x2, y2.
1838 * If percent is > 1.0 then the line will change direction.
1839 * If percent is negative it can lengthen the line. */
1840 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1842 REAL dist, theta, dx, dy;
1844 if((y1 == *y2) && (x1 == *x2))
1845 return;
1847 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1848 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1849 dx = cos(theta) * dist;
1850 dy = sin(theta) * dist;
1852 *x2 = *x2 + dx;
1853 *y2 = *y2 + dy;
1856 /* Shortens the line by the given amount by changing x2, y2.
1857 * If the amount is greater than the distance, the line will become length 0.
1858 * If the amount is negative, it can lengthen the line. */
1859 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1861 REAL dx, dy, percent;
1863 dx = *x2 - x1;
1864 dy = *y2 - y1;
1865 if(dx == 0 && dy == 0)
1866 return;
1868 percent = amt / sqrt(dx * dx + dy * dy);
1869 if(percent >= 1.0){
1870 *x2 = x1;
1871 *y2 = y1;
1872 return;
1875 shorten_line_percent(x1, y1, x2, y2, percent);
1878 /* Conducts a linear search to find the bezier points that will back off
1879 * the endpoint of the curve by a distance of amt. Linear search works
1880 * better than binary in this case because there are multiple solutions,
1881 * and binary searches often find a bad one. I don't think this is what
1882 * Windows does but short of rendering the bezier without GDI's help it's
1883 * the best we can do. If rev then work from the start of the passed points
1884 * instead of the end. */
1885 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1887 GpPointF origpt[4];
1888 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1889 INT i, first = 0, second = 1, third = 2, fourth = 3;
1891 if(rev){
1892 first = 3;
1893 second = 2;
1894 third = 1;
1895 fourth = 0;
1898 origx = pt[fourth].X;
1899 origy = pt[fourth].Y;
1900 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1902 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1903 /* reset bezier points to original values */
1904 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1905 /* Perform magic on bezier points. Order is important here.*/
1906 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1907 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1908 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1909 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1910 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1911 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1913 dx = pt[fourth].X - origx;
1914 dy = pt[fourth].Y - origy;
1916 diff = sqrt(dx * dx + dy * dy);
1917 percent += 0.0005 * amt;
1921 /* Draws a combination of bezier curves and lines between points. */
1922 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1923 GDIPCONST BYTE * types, INT count, BOOL caps)
1925 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1926 BYTE *tp = heap_alloc_zero(count);
1927 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1928 INT i, j;
1929 GpStatus status = GenericError;
1931 if(!count){
1932 status = Ok;
1933 goto end;
1935 if(!pti || !tp || !ptcopy){
1936 status = OutOfMemory;
1937 goto end;
1940 for(i = 1; i < count; i++){
1941 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1942 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1943 || !(types[i + 2] & PathPointTypeBezier)){
1944 ERR("Bad bezier points\n");
1945 goto end;
1947 i += 2;
1951 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1953 /* If we are drawing caps, go through the points and adjust them accordingly,
1954 * and draw the caps. */
1955 if(caps){
1956 switch(types[count - 1] & PathPointTypePathTypeMask){
1957 case PathPointTypeBezier:
1958 if(pen->endcap == LineCapArrowAnchor)
1959 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1960 else if((pen->endcap == LineCapCustom) && pen->customend)
1961 shorten_bezier_amt(&ptcopy[count - 4],
1962 pen->width * pen->customend->inset, FALSE);
1964 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1965 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1966 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1967 pt[count - 1].X, pt[count - 1].Y);
1969 break;
1970 case PathPointTypeLine:
1971 if(pen->endcap == LineCapArrowAnchor)
1972 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1973 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1974 pen->width);
1975 else if((pen->endcap == LineCapCustom) && pen->customend)
1976 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1977 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1978 pen->customend->inset * pen->width);
1980 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1981 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1982 pt[count - 1].Y);
1984 break;
1985 default:
1986 ERR("Bad path last point\n");
1987 goto end;
1990 /* Find start of points */
1991 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1992 == PathPointTypeStart); j++);
1994 switch(types[j] & PathPointTypePathTypeMask){
1995 case PathPointTypeBezier:
1996 if(pen->startcap == LineCapArrowAnchor)
1997 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1998 else if((pen->startcap == LineCapCustom) && pen->customstart)
1999 shorten_bezier_amt(&ptcopy[j - 1],
2000 pen->width * pen->customstart->inset, TRUE);
2002 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2003 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
2004 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
2005 pt[j - 1].X, pt[j - 1].Y);
2007 break;
2008 case PathPointTypeLine:
2009 if(pen->startcap == LineCapArrowAnchor)
2010 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2011 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2012 pen->width);
2013 else if((pen->startcap == LineCapCustom) && pen->customstart)
2014 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2015 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2016 pen->customstart->inset * pen->width);
2018 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2019 pt[j].X, pt[j].Y, pt[j - 1].X,
2020 pt[j - 1].Y);
2022 break;
2023 default:
2024 ERR("Bad path points\n");
2025 goto end;
2029 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
2031 round_points(pti, ptcopy, count);
2033 for(i = 0; i < count; i++){
2034 tp[i] = convert_path_point_type(types[i]);
2037 PolyDraw(graphics->hdc, pti, tp, count);
2039 status = Ok;
2041 end:
2042 heap_free(pti);
2043 heap_free(ptcopy);
2044 heap_free(tp);
2046 return status;
2049 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2051 GpStatus result;
2053 BeginPath(graphics->hdc);
2054 result = draw_poly(graphics, NULL, path->pathdata.Points,
2055 path->pathdata.Types, path->pathdata.Count, FALSE);
2056 EndPath(graphics->hdc);
2057 return result;
2060 typedef enum GraphicsContainerType {
2061 BEGIN_CONTAINER,
2062 SAVE_GRAPHICS
2063 } GraphicsContainerType;
2065 typedef struct _GraphicsContainerItem {
2066 struct list entry;
2067 GraphicsContainer contid;
2068 GraphicsContainerType type;
2070 SmoothingMode smoothing;
2071 CompositingQuality compqual;
2072 InterpolationMode interpolation;
2073 CompositingMode compmode;
2074 TextRenderingHint texthint;
2075 REAL scale;
2076 GpUnit unit;
2077 PixelOffsetMode pixeloffset;
2078 UINT textcontrast;
2079 GpMatrix worldtrans;
2080 GpRegion* clip;
2081 INT origin_x, origin_y;
2082 } GraphicsContainerItem;
2084 static GpStatus init_container(GraphicsContainerItem** container,
2085 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
2086 GpStatus sts;
2088 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
2089 if(!(*container))
2090 return OutOfMemory;
2092 (*container)->contid = graphics->contid + 1;
2093 (*container)->type = type;
2095 (*container)->smoothing = graphics->smoothing;
2096 (*container)->compqual = graphics->compqual;
2097 (*container)->interpolation = graphics->interpolation;
2098 (*container)->compmode = graphics->compmode;
2099 (*container)->texthint = graphics->texthint;
2100 (*container)->scale = graphics->scale;
2101 (*container)->unit = graphics->unit;
2102 (*container)->textcontrast = graphics->textcontrast;
2103 (*container)->pixeloffset = graphics->pixeloffset;
2104 (*container)->origin_x = graphics->origin_x;
2105 (*container)->origin_y = graphics->origin_y;
2106 (*container)->worldtrans = graphics->worldtrans;
2108 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2109 if(sts != Ok){
2110 heap_free(*container);
2111 *container = NULL;
2112 return sts;
2115 return Ok;
2118 static void delete_container(GraphicsContainerItem* container)
2120 GdipDeleteRegion(container->clip);
2121 heap_free(container);
2124 static GpStatus restore_container(GpGraphics* graphics,
2125 GDIPCONST GraphicsContainerItem* container){
2126 GpStatus sts;
2127 GpRegion *newClip;
2129 sts = GdipCloneRegion(container->clip, &newClip);
2130 if(sts != Ok) return sts;
2132 graphics->worldtrans = container->worldtrans;
2134 GdipDeleteRegion(graphics->clip);
2135 graphics->clip = newClip;
2137 graphics->contid = container->contid - 1;
2139 graphics->smoothing = container->smoothing;
2140 graphics->compqual = container->compqual;
2141 graphics->interpolation = container->interpolation;
2142 graphics->compmode = container->compmode;
2143 graphics->texthint = container->texthint;
2144 graphics->scale = container->scale;
2145 graphics->unit = container->unit;
2146 graphics->textcontrast = container->textcontrast;
2147 graphics->pixeloffset = container->pixeloffset;
2148 graphics->origin_x = container->origin_x;
2149 graphics->origin_y = container->origin_y;
2151 return Ok;
2154 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2156 RECT wnd_rect;
2157 GpStatus stat=Ok;
2158 GpUnit unit;
2160 if(graphics->hwnd) {
2161 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2162 return GenericError;
2164 rect->X = wnd_rect.left;
2165 rect->Y = wnd_rect.top;
2166 rect->Width = wnd_rect.right - wnd_rect.left;
2167 rect->Height = wnd_rect.bottom - wnd_rect.top;
2168 }else if (graphics->image){
2169 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2170 if (stat == Ok && unit != UnitPixel)
2171 FIXME("need to convert from unit %i\n", unit);
2172 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2173 HBITMAP hbmp;
2174 BITMAP bmp;
2176 rect->X = 0;
2177 rect->Y = 0;
2179 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2180 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2182 rect->Width = bmp.bmWidth;
2183 rect->Height = bmp.bmHeight;
2185 else
2187 /* FIXME: ??? */
2188 rect->Width = 1;
2189 rect->Height = 1;
2191 }else{
2192 rect->X = 0;
2193 rect->Y = 0;
2194 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2195 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2198 return stat;
2201 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2203 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2205 if (stat == Ok && graphics->hdc)
2207 GpPointF points[4], min_point, max_point;
2208 int i;
2210 points[0].X = points[2].X = rect->X;
2211 points[0].Y = points[1].Y = rect->Y;
2212 points[1].X = points[3].X = rect->X + rect->Width;
2213 points[2].Y = points[3].Y = rect->Y + rect->Height;
2215 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2217 min_point = max_point = points[0];
2219 for (i=1; i<4; i++)
2221 if (points[i].X < min_point.X) min_point.X = points[i].X;
2222 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2223 if (points[i].X > max_point.X) max_point.X = points[i].X;
2224 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2227 rect->X = min_point.X;
2228 rect->Y = min_point.Y;
2229 rect->Width = max_point.X - min_point.X;
2230 rect->Height = max_point.Y - min_point.Y;
2233 return stat;
2236 /* on success, rgn will contain the region of the graphics object which
2237 * is visible after clipping has been applied */
2238 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2240 GpStatus stat;
2241 GpRectF rectf;
2242 GpRegion* tmp;
2244 /* Ignore graphics image bounds for metafiles */
2245 if (is_metafile_graphics(graphics))
2246 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2248 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2249 return stat;
2251 if((stat = GdipCreateRegion(&tmp)) != Ok)
2252 return stat;
2254 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2255 goto end;
2257 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2258 goto end;
2260 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2262 end:
2263 GdipDeleteRegion(tmp);
2264 return stat;
2267 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2269 REAL height;
2271 if (font->unit == UnitPixel)
2273 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres, graphics->printer_display);
2275 else
2277 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2278 height = units_to_pixels(font->emSize, font->unit, graphics->xres, graphics->printer_display);
2279 else
2280 height = units_to_pixels(font->emSize, font->unit, graphics->yres, graphics->printer_display);
2283 lf->lfHeight = -(height + 0.5);
2284 lf->lfWidth = 0;
2285 lf->lfEscapement = 0;
2286 lf->lfOrientation = 0;
2287 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2288 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2289 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2290 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2291 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2292 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2293 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2294 lf->lfQuality = DEFAULT_QUALITY;
2295 lf->lfPitchAndFamily = 0;
2296 lstrcpyW(lf->lfFaceName, font->family->FamilyName);
2299 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2300 GDIPCONST GpStringFormat *format, HFONT *hfont,
2301 LOGFONTW *lfw_return, GDIPCONST GpMatrix *matrix)
2303 HDC hdc = CreateCompatibleDC(0);
2304 REAL angle, rel_width, rel_height, font_height;
2305 LOGFONTW lfw;
2306 HFONT unscaled_font;
2307 TEXTMETRICW textmet;
2309 if (font->unit == UnitPixel || font->unit == UnitWorld)
2310 font_height = font->emSize;
2311 else
2313 REAL unit_scale, res;
2315 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2316 unit_scale = units_scale(font->unit, graphics->unit, res, graphics->printer_display);
2318 font_height = font->emSize * unit_scale;
2321 transform_properties(graphics, matrix, TRUE, &rel_width, &rel_height, &angle);
2322 /* If the font unit is not pixels scaling should not be applied */
2323 if (font->unit != UnitPixel && font->unit != UnitWorld)
2325 rel_width /= graphics->scale;
2326 rel_height /= graphics->scale;
2329 get_log_fontW(font, graphics, &lfw);
2330 lfw.lfHeight = -gdip_round(font_height * rel_height);
2331 unscaled_font = CreateFontIndirectW(&lfw);
2333 SelectObject(hdc, unscaled_font);
2334 GetTextMetricsW(hdc, &textmet);
2336 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2337 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2339 *hfont = CreateFontIndirectW(&lfw);
2341 if (lfw_return)
2342 *lfw_return = lfw;
2344 DeleteDC(hdc);
2345 DeleteObject(unscaled_font);
2348 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2350 TRACE("(%p, %p)\n", hdc, graphics);
2352 return GdipCreateFromHDC2(hdc, NULL, graphics);
2355 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2357 XFORM xform;
2359 if (graphics->hdc == NULL)
2361 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2362 return;
2365 GetTransform(graphics->hdc, 0x204, &xform);
2366 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2369 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2371 GpStatus retval;
2372 HBITMAP hbitmap;
2373 DIBSECTION dib;
2375 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2377 if(hDevice != NULL)
2378 FIXME("Don't know how to handle parameter hDevice\n");
2380 if(hdc == NULL)
2381 return OutOfMemory;
2383 if(graphics == NULL)
2384 return InvalidParameter;
2386 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2387 if(!*graphics) return OutOfMemory;
2389 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2391 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2392 heap_free(*graphics);
2393 return retval;
2396 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2397 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2398 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2400 (*graphics)->alpha_hdc = 1;
2403 (*graphics)->hdc = hdc;
2404 (*graphics)->hwnd = WindowFromDC(hdc);
2405 (*graphics)->owndc = FALSE;
2406 (*graphics)->smoothing = SmoothingModeDefault;
2407 (*graphics)->compqual = CompositingQualityDefault;
2408 (*graphics)->interpolation = InterpolationModeBilinear;
2409 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2410 (*graphics)->compmode = CompositingModeSourceOver;
2411 (*graphics)->unit = UnitDisplay;
2412 (*graphics)->scale = 1.0;
2413 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2414 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2415 (*graphics)->busy = FALSE;
2416 (*graphics)->textcontrast = 4;
2417 list_init(&(*graphics)->containers);
2418 (*graphics)->contid = 0;
2419 (*graphics)->printer_display = (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER);
2420 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2422 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2423 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2425 DeleteObject((*graphics)->gdi_clip);
2426 (*graphics)->gdi_clip = NULL;
2429 TRACE("<-- %p\n", *graphics);
2431 return Ok;
2434 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2436 GpStatus retval;
2438 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2439 if(!*graphics) return OutOfMemory;
2441 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2442 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2444 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2445 heap_free(*graphics);
2446 return retval;
2449 (*graphics)->hdc = NULL;
2450 (*graphics)->hwnd = NULL;
2451 (*graphics)->owndc = FALSE;
2452 (*graphics)->image = image;
2453 /* We have to store the image type here because the image may be freed
2454 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2455 (*graphics)->image_type = image->type;
2456 (*graphics)->smoothing = SmoothingModeDefault;
2457 (*graphics)->compqual = CompositingQualityDefault;
2458 (*graphics)->interpolation = InterpolationModeBilinear;
2459 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2460 (*graphics)->compmode = CompositingModeSourceOver;
2461 (*graphics)->unit = UnitDisplay;
2462 (*graphics)->scale = 1.0;
2463 (*graphics)->xres = image->xres;
2464 (*graphics)->yres = image->yres;
2465 (*graphics)->busy = FALSE;
2466 (*graphics)->textcontrast = 4;
2467 list_init(&(*graphics)->containers);
2468 (*graphics)->contid = 0;
2470 TRACE("<-- %p\n", *graphics);
2472 return Ok;
2475 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2477 GpStatus ret;
2478 HDC hdc;
2480 TRACE("(%p, %p)\n", hwnd, graphics);
2482 hdc = GetDC(hwnd);
2484 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2486 ReleaseDC(hwnd, hdc);
2487 return ret;
2490 (*graphics)->hwnd = hwnd;
2491 (*graphics)->owndc = TRUE;
2493 return Ok;
2496 /* FIXME: no icm handling */
2497 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2499 TRACE("(%p, %p)\n", hwnd, graphics);
2501 return GdipCreateFromHWND(hwnd, graphics);
2504 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2505 UINT access, IStream **stream)
2507 DWORD dwMode;
2508 HRESULT ret;
2510 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2512 if(!stream || !filename)
2513 return InvalidParameter;
2515 if(access & GENERIC_WRITE)
2516 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2517 else if(access & GENERIC_READ)
2518 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2519 else
2520 return InvalidParameter;
2522 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2524 return hresult_to_status(ret);
2527 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2529 GraphicsContainerItem *cont, *next;
2530 GpStatus stat;
2531 TRACE("(%p)\n", graphics);
2533 if(!graphics) return InvalidParameter;
2534 if(graphics->busy) return ObjectBusy;
2536 if (is_metafile_graphics(graphics))
2538 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2539 if (stat != Ok)
2540 return stat;
2543 if (graphics->temp_hdc)
2545 DeleteDC(graphics->temp_hdc);
2546 graphics->temp_hdc = NULL;
2549 if(graphics->owndc)
2550 ReleaseDC(graphics->hwnd, graphics->hdc);
2552 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2553 list_remove(&cont->entry);
2554 delete_container(cont);
2557 GdipDeleteRegion(graphics->clip);
2559 DeleteObject(graphics->gdi_clip);
2561 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2562 * do otherwise, but we can't have that in the test suite because it means
2563 * accessing freed memory. */
2564 graphics->busy = TRUE;
2566 heap_free(graphics);
2568 return Ok;
2571 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2572 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2574 GpStatus status;
2575 GpPath *path;
2576 GpRectF rect;
2578 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2579 width, height, startAngle, sweepAngle);
2581 if(!graphics || !pen || width <= 0 || height <= 0)
2582 return InvalidParameter;
2584 if(graphics->busy)
2585 return ObjectBusy;
2587 if (is_metafile_graphics(graphics))
2589 set_rect(&rect, x, y, width, height);
2590 return METAFILE_DrawArc((GpMetafile *)graphics->image, pen, &rect, startAngle, sweepAngle);
2593 status = GdipCreatePath(FillModeAlternate, &path);
2594 if (status != Ok) return status;
2596 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2597 if (status == Ok)
2598 status = GdipDrawPath(graphics, pen, path);
2600 GdipDeletePath(path);
2601 return status;
2604 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2605 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2607 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2608 width, height, startAngle, sweepAngle);
2610 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2613 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2614 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2616 GpPointF pt[4];
2618 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2619 x2, y2, x3, y3, x4, y4);
2621 if(!graphics || !pen)
2622 return InvalidParameter;
2624 if(graphics->busy)
2625 return ObjectBusy;
2627 pt[0].X = x1;
2628 pt[0].Y = y1;
2629 pt[1].X = x2;
2630 pt[1].Y = y2;
2631 pt[2].X = x3;
2632 pt[2].Y = y3;
2633 pt[3].X = x4;
2634 pt[3].Y = y4;
2635 return GdipDrawBeziers(graphics, pen, pt, 4);
2638 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2639 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2641 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2642 x2, y2, x3, y3, x4, y4);
2644 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2647 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2648 GDIPCONST GpPointF *points, INT count)
2650 GpStatus status;
2651 GpPath *path;
2653 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2655 if(!graphics || !pen || !points || (count <= 0))
2656 return InvalidParameter;
2658 if(graphics->busy)
2659 return ObjectBusy;
2661 status = GdipCreatePath(FillModeAlternate, &path);
2662 if (status != Ok) return status;
2664 status = GdipAddPathBeziers(path, points, count);
2665 if (status == Ok)
2666 status = GdipDrawPath(graphics, pen, path);
2668 GdipDeletePath(path);
2669 return status;
2672 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2673 GDIPCONST GpPoint *points, INT count)
2675 GpPointF *pts;
2676 GpStatus ret;
2677 INT i;
2679 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2681 if(!graphics || !pen || !points || (count <= 0))
2682 return InvalidParameter;
2684 if(graphics->busy)
2685 return ObjectBusy;
2687 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2688 if(!pts)
2689 return OutOfMemory;
2691 for(i = 0; i < count; i++){
2692 pts[i].X = (REAL)points[i].X;
2693 pts[i].Y = (REAL)points[i].Y;
2696 ret = GdipDrawBeziers(graphics,pen,pts,count);
2698 heap_free(pts);
2700 return ret;
2703 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2704 GDIPCONST GpPointF *points, INT count)
2706 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2708 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2711 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2712 GDIPCONST GpPoint *points, INT count)
2714 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2716 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2719 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2720 GDIPCONST GpPointF *points, INT count, REAL tension)
2722 GpPath *path;
2723 GpStatus status;
2725 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2727 if(!graphics || !pen || !points || count <= 0)
2728 return InvalidParameter;
2730 if(graphics->busy)
2731 return ObjectBusy;
2733 status = GdipCreatePath(FillModeAlternate, &path);
2734 if (status != Ok) return status;
2736 status = GdipAddPathClosedCurve2(path, points, count, tension);
2737 if (status == Ok)
2738 status = GdipDrawPath(graphics, pen, path);
2740 GdipDeletePath(path);
2742 return status;
2745 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2746 GDIPCONST GpPoint *points, INT count, REAL tension)
2748 GpPointF *ptf;
2749 GpStatus stat;
2750 INT i;
2752 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2754 if(!points || count <= 0)
2755 return InvalidParameter;
2757 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2758 if(!ptf)
2759 return OutOfMemory;
2761 for(i = 0; i < count; i++){
2762 ptf[i].X = (REAL)points[i].X;
2763 ptf[i].Y = (REAL)points[i].Y;
2766 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2768 heap_free(ptf);
2770 return stat;
2773 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2774 GDIPCONST GpPointF *points, INT count)
2776 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2778 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2781 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2782 GDIPCONST GpPoint *points, INT count)
2784 GpPointF *pointsF;
2785 GpStatus ret;
2786 INT i;
2788 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2790 if(!points)
2791 return InvalidParameter;
2793 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2794 if(!pointsF)
2795 return OutOfMemory;
2797 for(i = 0; i < count; i++){
2798 pointsF[i].X = (REAL)points[i].X;
2799 pointsF[i].Y = (REAL)points[i].Y;
2802 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2803 heap_free(pointsF);
2805 return ret;
2808 /* Approximates cardinal spline with Bezier curves. */
2809 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2810 GDIPCONST GpPointF *points, INT count, REAL tension)
2812 GpPath *path;
2813 GpStatus status;
2815 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2817 if(!graphics || !pen)
2818 return InvalidParameter;
2820 if(graphics->busy)
2821 return ObjectBusy;
2823 if(count < 2)
2824 return InvalidParameter;
2826 status = GdipCreatePath(FillModeAlternate, &path);
2827 if (status != Ok) return status;
2829 status = GdipAddPathCurve2(path, points, count, tension);
2830 if (status == Ok)
2831 status = GdipDrawPath(graphics, pen, path);
2833 GdipDeletePath(path);
2834 return status;
2837 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2838 GDIPCONST GpPoint *points, INT count, REAL tension)
2840 GpPointF *pointsF;
2841 GpStatus ret;
2842 INT i;
2844 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2846 if(!points)
2847 return InvalidParameter;
2849 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2850 if(!pointsF)
2851 return OutOfMemory;
2853 for(i = 0; i < count; i++){
2854 pointsF[i].X = (REAL)points[i].X;
2855 pointsF[i].Y = (REAL)points[i].Y;
2858 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2859 heap_free(pointsF);
2861 return ret;
2864 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2865 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2866 REAL tension)
2868 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2870 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2871 return InvalidParameter;
2874 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2877 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2878 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2879 REAL tension)
2881 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2883 if(count < 0){
2884 return OutOfMemory;
2887 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2888 return InvalidParameter;
2891 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2894 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2895 REAL y, REAL width, REAL height)
2897 GpPath *path;
2898 GpStatus status;
2899 GpRectF rect;
2901 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2903 if(!graphics || !pen)
2904 return InvalidParameter;
2906 if(graphics->busy)
2907 return ObjectBusy;
2909 if (is_metafile_graphics(graphics))
2911 set_rect(&rect, x, y, width, height);
2912 return METAFILE_DrawEllipse((GpMetafile *)graphics->image, pen, &rect);
2915 status = GdipCreatePath(FillModeAlternate, &path);
2916 if (status != Ok) return status;
2918 status = GdipAddPathEllipse(path, x, y, width, height);
2919 if (status == Ok)
2920 status = GdipDrawPath(graphics, pen, path);
2922 GdipDeletePath(path);
2923 return status;
2926 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2927 INT y, INT width, INT height)
2929 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2931 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2935 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2937 UINT width, height;
2939 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2941 if(!graphics || !image)
2942 return InvalidParameter;
2944 GdipGetImageWidth(image, &width);
2945 GdipGetImageHeight(image, &height);
2947 return GdipDrawImagePointRect(graphics, image, x, y,
2948 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2951 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2952 INT y)
2954 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2956 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2959 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2960 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2961 GpUnit srcUnit)
2963 GpPointF points[3];
2964 REAL scale_x, scale_y, width, height;
2966 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2968 if (!graphics || !image) return InvalidParameter;
2970 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres, graphics->printer_display);
2971 scale_x *= graphics->xres / image->xres;
2972 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres, graphics->printer_display);
2973 scale_y *= graphics->yres / image->yres;
2974 width = srcwidth * scale_x;
2975 height = srcheight * scale_y;
2977 points[0].X = points[2].X = x;
2978 points[0].Y = points[1].Y = y;
2979 points[1].X = x + width;
2980 points[2].Y = y + height;
2982 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2983 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2986 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2987 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2988 GpUnit srcUnit)
2990 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2993 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2994 GDIPCONST GpPointF *dstpoints, INT count)
2996 UINT width, height;
2998 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3000 if(!image)
3001 return InvalidParameter;
3003 GdipGetImageWidth(image, &width);
3004 GdipGetImageHeight(image, &height);
3006 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3007 width, height, UnitPixel, NULL, NULL, NULL);
3010 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3011 GDIPCONST GpPoint *dstpoints, INT count)
3013 GpPointF ptf[3];
3015 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3017 if (count != 3 || !dstpoints)
3018 return InvalidParameter;
3020 ptf[0].X = (REAL)dstpoints[0].X;
3021 ptf[0].Y = (REAL)dstpoints[0].Y;
3022 ptf[1].X = (REAL)dstpoints[1].X;
3023 ptf[1].Y = (REAL)dstpoints[1].Y;
3024 ptf[2].X = (REAL)dstpoints[2].X;
3025 ptf[2].Y = (REAL)dstpoints[2].Y;
3027 return GdipDrawImagePoints(graphics, image, ptf, count);
3030 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3031 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3033 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3034 return TRUE;
3037 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3038 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3039 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3040 DrawImageAbort callback, VOID * callbackData)
3042 GpPointF ptf[4];
3043 POINT pti[4];
3044 GpStatus stat;
3046 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3047 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3048 callbackData);
3050 if (count == 4)
3051 return NotImplemented;
3053 if(!graphics || !image || !points || count != 3)
3054 return InvalidParameter;
3056 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3057 debugstr_pointf(&points[2]));
3059 if (is_metafile_graphics(graphics))
3061 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
3062 image, points, count, srcx, srcy, srcwidth, srcheight,
3063 srcUnit, imageAttributes, callback, callbackData);
3066 memcpy(ptf, points, 3 * sizeof(GpPointF));
3068 /* Ensure source width/height is positive */
3069 if (srcwidth < 0)
3071 GpPointF tmp = ptf[1];
3072 srcx = srcx + srcwidth;
3073 srcwidth = -srcwidth;
3074 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
3075 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3076 ptf[1] = ptf[0];
3077 ptf[0] = tmp;
3080 if (srcheight < 0)
3082 GpPointF tmp = ptf[2];
3083 srcy = srcy + srcheight;
3084 srcheight = -srcheight;
3085 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
3086 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
3087 ptf[2] = ptf[0];
3088 ptf[0] = tmp;
3091 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3092 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3093 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
3094 return Ok;
3095 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
3096 round_points(pti, ptf, 4);
3098 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3099 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3101 srcx = units_to_pixels(srcx, srcUnit, image->xres, graphics->printer_display);
3102 srcy = units_to_pixels(srcy, srcUnit, image->yres, graphics->printer_display);
3103 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres, graphics->printer_display);
3104 srcheight = units_to_pixels(srcheight, srcUnit, image->yres, graphics->printer_display);
3105 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3107 if (image->type == ImageTypeBitmap)
3109 GpBitmap* bitmap = (GpBitmap*)image;
3110 BOOL do_resampling = FALSE;
3111 BOOL use_software = FALSE;
3113 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08lx\n",
3114 graphics->xres, graphics->yres,
3115 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3116 graphics->scale, image->xres, image->yres, bitmap->format,
3117 imageAttributes ? imageAttributes->outside_color : 0);
3119 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3120 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3121 srcx < 0 || srcy < 0 ||
3122 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3123 do_resampling = TRUE;
3125 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3126 (graphics->image && graphics->image->type == ImageTypeBitmap))
3127 use_software = TRUE;
3129 if (use_software)
3131 RECT dst_area;
3132 GpRectF graphics_bounds;
3133 GpRect src_area;
3134 int i, x, y, src_stride, dst_stride;
3135 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3136 BitmapData lockeddata;
3137 InterpolationMode interpolation = graphics->interpolation;
3138 PixelOffsetMode offset_mode = graphics->pixeloffset;
3139 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3141 if (!imageAttributes)
3142 imageAttributes = &defaultImageAttributes;
3144 dst_area.left = dst_area.right = pti[0].x;
3145 dst_area.top = dst_area.bottom = pti[0].y;
3146 for (i=1; i<4; i++)
3148 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3149 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3150 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3151 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3154 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3155 if (stat != Ok) return stat;
3157 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3158 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3159 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3160 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3162 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3164 if (IsRectEmpty(&dst_area)) return Ok;
3166 if (do_resampling)
3168 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3169 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3171 else
3173 /* Make sure src_area is equal in size to dst_area. */
3174 src_area.X = srcx + dst_area.left - pti[0].x;
3175 src_area.Y = srcy + dst_area.top - pti[0].y;
3176 src_area.Width = dst_area.right - dst_area.left;
3177 src_area.Height = dst_area.bottom - dst_area.top;
3180 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3182 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3183 if (!src_data)
3184 return OutOfMemory;
3185 src_stride = sizeof(ARGB) * src_area.Width;
3187 /* Read the bits we need from the source bitmap into a compatible buffer. */
3188 lockeddata.Width = src_area.Width;
3189 lockeddata.Height = src_area.Height;
3190 lockeddata.Stride = src_stride;
3191 lockeddata.Scan0 = src_data;
3192 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3193 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3194 else
3195 lockeddata.PixelFormat = PixelFormat32bppARGB;
3197 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3198 lockeddata.PixelFormat, &lockeddata);
3200 if (stat == Ok)
3201 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3203 if (stat != Ok)
3205 heap_free(src_data);
3206 return stat;
3209 apply_image_attributes(imageAttributes, src_data,
3210 src_area.Width, src_area.Height,
3211 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3213 if (do_resampling)
3215 GpMatrix dst_to_src;
3216 REAL m11, m12, m21, m22, mdx, mdy;
3217 REAL x_dx, x_dy, y_dx, y_dy;
3218 ARGB *dst_color;
3219 GpPointF src_pointf_row, src_pointf;
3221 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3222 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3223 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3224 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3225 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3226 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3228 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3230 stat = GdipInvertMatrix(&dst_to_src);
3231 if (stat != Ok) return stat;
3233 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3234 x_dx = dst_to_src.matrix[0];
3235 x_dy = dst_to_src.matrix[1];
3236 y_dx = dst_to_src.matrix[2];
3237 y_dy = dst_to_src.matrix[3];
3239 /* Transform the bits as needed to the destination. */
3240 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3241 if (!dst_data)
3243 heap_free(src_data);
3244 return OutOfMemory;
3246 dst_color = (ARGB*)(dst_data);
3248 /* Calculate top left point of transformed image.
3249 It would be used as reference point for adding */
3250 src_pointf_row.X = dst_to_src.matrix[4] +
3251 dst_area.left * x_dx + dst_area.top * y_dx;
3252 src_pointf_row.Y = dst_to_src.matrix[5] +
3253 dst_area.left * x_dy + dst_area.top * y_dy;
3255 for (y = dst_area.top; y < dst_area.bottom;
3256 y++, src_pointf_row.X += y_dx, src_pointf_row.Y += y_dy)
3258 for (x = dst_area.left, src_pointf = src_pointf_row; x < dst_area.right;
3259 x++, src_pointf.X += x_dx, src_pointf.Y += x_dy)
3261 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth &&
3262 src_pointf.Y >= srcy && src_pointf.Y < srcy + srcheight)
3263 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3264 imageAttributes, interpolation, offset_mode);
3265 dst_color++;
3269 else
3271 dst_data = src_data;
3272 dst_stride = src_stride;
3275 gdi_transform_acquire(graphics);
3277 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3278 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3279 lockeddata.PixelFormat);
3281 gdi_transform_release(graphics);
3283 heap_free(src_data);
3285 heap_free(dst_dyn_data);
3287 return stat;
3289 else
3291 HDC hdc;
3292 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3293 HBITMAP hbitmap, old_hbm=NULL;
3294 HRGN hrgn;
3295 INT save_state;
3297 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3298 bitmap->format == PixelFormat24bppRGB ||
3299 bitmap->format == PixelFormat32bppRGB ||
3300 bitmap->format == PixelFormat32bppPARGB))
3302 BITMAPINFOHEADER bih;
3303 BYTE *temp_bits;
3304 PixelFormat dst_format;
3306 /* we can't draw a bitmap of this format directly */
3307 hdc = CreateCompatibleDC(0);
3308 temp_hdc = TRUE;
3309 temp_bitmap = TRUE;
3311 bih.biSize = sizeof(BITMAPINFOHEADER);
3312 bih.biWidth = bitmap->width;
3313 bih.biHeight = -bitmap->height;
3314 bih.biPlanes = 1;
3315 bih.biBitCount = 32;
3316 bih.biCompression = BI_RGB;
3317 bih.biSizeImage = 0;
3318 bih.biXPelsPerMeter = 0;
3319 bih.biYPelsPerMeter = 0;
3320 bih.biClrUsed = 0;
3321 bih.biClrImportant = 0;
3323 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3324 (void**)&temp_bits, NULL, 0);
3326 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3327 dst_format = PixelFormat32bppPARGB;
3328 else
3329 dst_format = PixelFormat32bppRGB;
3331 convert_pixels(bitmap->width, bitmap->height,
3332 bitmap->width*4, temp_bits, dst_format, bitmap->image.palette,
3333 bitmap->stride, bitmap->bits, bitmap->format,
3334 bitmap->image.palette);
3336 else
3338 if (bitmap->hbitmap)
3339 hbitmap = bitmap->hbitmap;
3340 else
3342 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3343 temp_bitmap = TRUE;
3346 hdc = bitmap->hdc;
3347 temp_hdc = (hdc == 0);
3350 if (temp_hdc)
3352 if (!hdc) hdc = CreateCompatibleDC(0);
3353 old_hbm = SelectObject(hdc, hbitmap);
3356 save_state = SaveDC(graphics->hdc);
3358 stat = get_clip_hrgn(graphics, &hrgn);
3360 if (stat == Ok)
3362 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3363 DeleteObject(hrgn);
3366 gdi_transform_acquire(graphics);
3368 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3370 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3371 hdc, srcx, srcy, srcwidth, srcheight);
3373 else
3375 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3376 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3379 gdi_transform_release(graphics);
3381 RestoreDC(graphics->hdc, save_state);
3383 if (temp_hdc)
3385 SelectObject(hdc, old_hbm);
3386 DeleteDC(hdc);
3389 if (temp_bitmap)
3390 DeleteObject(hbitmap);
3393 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3395 GpRectF rc;
3397 set_rect(&rc, srcx, srcy, srcwidth, srcheight);
3398 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3399 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3401 else
3403 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3404 return InvalidParameter;
3407 return Ok;
3410 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3411 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3412 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3413 DrawImageAbort callback, VOID * callbackData)
3415 GpPointF pointsF[3];
3416 INT i;
3418 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3419 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3420 callbackData);
3422 if (count == 4)
3423 return NotImplemented;
3425 if (!points || count != 3)
3426 return InvalidParameter;
3428 for(i = 0; i < count; i++){
3429 pointsF[i].X = (REAL)points[i].X;
3430 pointsF[i].Y = (REAL)points[i].Y;
3433 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3434 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3435 callback, callbackData);
3438 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3439 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3440 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3441 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3442 VOID * callbackData)
3444 GpPointF points[3];
3446 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3447 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3448 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3450 points[0].X = dstx;
3451 points[0].Y = dsty;
3452 points[1].X = dstx + dstwidth;
3453 points[1].Y = dsty;
3454 points[2].X = dstx;
3455 points[2].Y = dsty + dstheight;
3457 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3458 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3461 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3462 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3463 INT srcwidth, INT srcheight, GpUnit srcUnit,
3464 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3465 VOID * callbackData)
3467 GpPointF points[3];
3469 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3470 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3471 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3473 points[0].X = dstx;
3474 points[0].Y = dsty;
3475 points[1].X = dstx + dstwidth;
3476 points[1].Y = dsty;
3477 points[2].X = dstx;
3478 points[2].Y = dsty + dstheight;
3480 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3481 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3484 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3485 REAL x, REAL y, REAL width, REAL height)
3487 RectF bounds;
3488 GpUnit unit;
3489 GpStatus ret;
3491 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3493 if(!graphics || !image)
3494 return InvalidParameter;
3496 ret = GdipGetImageBounds(image, &bounds, &unit);
3497 if(ret != Ok)
3498 return ret;
3500 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3501 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3502 unit, NULL, NULL, NULL);
3505 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3506 INT x, INT y, INT width, INT height)
3508 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3510 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3513 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3514 REAL y1, REAL x2, REAL y2)
3516 GpPointF pt[2];
3518 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3520 if (!pen)
3521 return InvalidParameter;
3523 if (pen->unit == UnitPixel && pen->width <= 0.0)
3524 return Ok;
3526 pt[0].X = x1;
3527 pt[0].Y = y1;
3528 pt[1].X = x2;
3529 pt[1].Y = y2;
3530 return GdipDrawLines(graphics, pen, pt, 2);
3533 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3534 INT y1, INT x2, INT y2)
3536 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3538 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3541 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3542 GpPointF *points, INT count)
3544 GpStatus status;
3545 GpPath *path;
3547 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3549 if(!pen || !graphics || (count < 2))
3550 return InvalidParameter;
3552 if(graphics->busy)
3553 return ObjectBusy;
3555 status = GdipCreatePath(FillModeAlternate, &path);
3556 if (status != Ok) return status;
3558 status = GdipAddPathLine2(path, points, count);
3559 if (status == Ok)
3560 status = GdipDrawPath(graphics, pen, path);
3562 GdipDeletePath(path);
3563 return status;
3566 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3567 GpPoint *points, INT count)
3569 GpStatus retval;
3570 GpPointF *ptf;
3571 int i;
3573 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3575 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3576 if(!ptf) return OutOfMemory;
3578 for(i = 0; i < count; i ++){
3579 ptf[i].X = (REAL) points[i].X;
3580 ptf[i].Y = (REAL) points[i].Y;
3583 retval = GdipDrawLines(graphics, pen, ptf, count);
3585 heap_free(ptf);
3586 return retval;
3589 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3591 INT save_state;
3592 GpStatus retval;
3593 HRGN hrgn=NULL;
3595 save_state = prepare_dc(graphics, pen);
3597 retval = get_clip_hrgn(graphics, &hrgn);
3599 if (retval != Ok)
3600 goto end;
3602 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3604 gdi_transform_acquire(graphics);
3606 retval = draw_poly(graphics, pen, path->pathdata.Points,
3607 path->pathdata.Types, path->pathdata.Count, TRUE);
3609 gdi_transform_release(graphics);
3611 end:
3612 restore_dc(graphics, save_state);
3613 DeleteObject(hrgn);
3615 return retval;
3618 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3620 GpStatus stat;
3621 GpPath* flat_path;
3622 GpMatrix* transform;
3623 GpRectF gp_bound_rect;
3624 GpRect gp_output_area;
3625 RECT output_area;
3626 INT output_height, output_width;
3627 DWORD *output_bits, *brush_bits=NULL;
3628 int i;
3629 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3630 const BYTE *dash_pattern;
3631 INT dash_pattern_size;
3632 BYTE *dyn_dash_pattern = NULL;
3634 stat = GdipClonePath(path, &flat_path);
3636 if (stat != Ok)
3637 return stat;
3639 stat = GdipCreateMatrix(&transform);
3641 if (stat == Ok)
3643 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3644 CoordinateSpaceWorld, transform);
3646 if (stat == Ok)
3647 stat = GdipFlattenPath(flat_path, transform, 1.0);
3649 GdipDeleteMatrix(transform);
3652 /* estimate the output size in pixels, can be larger than necessary */
3653 if (stat == Ok)
3655 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3656 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3657 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3658 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3660 for (i=1; i<flat_path->pathdata.Count; i++)
3662 REAL x, y;
3663 x = flat_path->pathdata.Points[i].X;
3664 y = flat_path->pathdata.Points[i].Y;
3666 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3667 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3668 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3669 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3672 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3675 if (stat == Ok)
3677 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3678 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3679 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3680 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3682 output_width = output_area.right - output_area.left + 1;
3683 output_height = output_area.bottom - output_area.top + 1;
3685 if (output_width <= 0 || output_height <= 0)
3687 GdipDeletePath(flat_path);
3688 return Ok;
3691 gp_output_area.X = output_area.left;
3692 gp_output_area.Y = output_area.top;
3693 gp_output_area.Width = output_width;
3694 gp_output_area.Height = output_height;
3696 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3697 if (!output_bits)
3698 stat = OutOfMemory;
3701 if (stat == Ok)
3703 if (pen->brush->bt != BrushTypeSolidColor)
3705 /* allocate and draw brush output */
3706 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3708 if (brush_bits)
3710 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3711 &gp_output_area, output_width);
3713 else
3714 stat = OutOfMemory;
3717 if (stat == Ok)
3719 /* convert dash pattern to bool array */
3720 switch (pen->dash)
3722 case DashStyleCustom:
3724 dash_pattern_size = 0;
3726 for (i=0; i < pen->numdashes; i++)
3727 dash_pattern_size += gdip_round(pen->dashes[i]);
3729 if (dash_pattern_size != 0)
3731 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3733 if (dyn_dash_pattern)
3735 int j=0;
3736 for (i=0; i < pen->numdashes; i++)
3738 int k;
3739 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3740 dyn_dash_pattern[j++] = (i&1)^1;
3743 else
3744 stat = OutOfMemory;
3746 break;
3748 /* else fall through */
3750 case DashStyleSolid:
3751 default:
3752 dash_pattern = static_dash_pattern;
3753 dash_pattern_size = 1;
3754 break;
3755 case DashStyleDash:
3756 dash_pattern = static_dash_pattern;
3757 dash_pattern_size = 4;
3758 break;
3759 case DashStyleDot:
3760 dash_pattern = &static_dash_pattern[4];
3761 dash_pattern_size = 2;
3762 break;
3763 case DashStyleDashDot:
3764 dash_pattern = static_dash_pattern;
3765 dash_pattern_size = 6;
3766 break;
3767 case DashStyleDashDotDot:
3768 dash_pattern = static_dash_pattern;
3769 dash_pattern_size = 8;
3770 break;
3774 if (stat == Ok)
3776 /* trace path */
3777 GpPointF subpath_start = flat_path->pathdata.Points[0];
3778 INT prev_x = INT_MAX, prev_y = INT_MAX;
3779 int dash_pos = dash_pattern_size - 1;
3781 for (i=0; i < flat_path->pathdata.Count; i++)
3783 BYTE type, type2;
3784 GpPointF start_point, end_point;
3785 GpPoint start_pointi, end_pointi;
3787 type = flat_path->pathdata.Types[i];
3788 if (i+1 < flat_path->pathdata.Count)
3789 type2 = flat_path->pathdata.Types[i+1];
3790 else
3791 type2 = PathPointTypeStart;
3793 start_point = flat_path->pathdata.Points[i];
3795 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3796 subpath_start = start_point;
3798 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3799 end_point = subpath_start;
3800 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3801 continue;
3802 else
3803 end_point = flat_path->pathdata.Points[i+1];
3805 start_pointi.X = floorf(start_point.X);
3806 start_pointi.Y = floorf(start_point.Y);
3807 end_pointi.X = floorf(end_point.X);
3808 end_pointi.Y = floorf(end_point.Y);
3810 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3811 continue;
3813 /* draw line segment */
3814 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3816 INT x, y, start_y, end_y, step;
3818 if (start_pointi.Y < end_pointi.Y)
3820 step = 1;
3821 start_y = ceilf(start_point.Y) - output_area.top;
3822 end_y = end_pointi.Y - output_area.top;
3824 else
3826 step = -1;
3827 start_y = start_point.Y - output_area.top;
3828 end_y = ceilf(end_point.Y) - output_area.top;
3831 for (y=start_y; y != (end_y+step); y+=step)
3833 x = gdip_round( start_point.X +
3834 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3835 - output_area.left;
3837 if (x == prev_x && y == prev_y)
3838 continue;
3840 prev_x = x;
3841 prev_y = y;
3842 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3844 if (!dash_pattern[dash_pos])
3845 continue;
3847 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3848 continue;
3850 if (brush_bits)
3851 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3852 else
3853 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3856 else
3858 INT x, y, start_x, end_x, step;
3860 if (start_pointi.X < end_pointi.X)
3862 step = 1;
3863 start_x = ceilf(start_point.X) - output_area.left;
3864 end_x = end_pointi.X - output_area.left;
3866 else
3868 step = -1;
3869 start_x = start_point.X - output_area.left;
3870 end_x = ceilf(end_point.X) - output_area.left;
3873 for (x=start_x; x != (end_x+step); x+=step)
3875 y = gdip_round( start_point.Y +
3876 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3877 - output_area.top;
3879 if (x == prev_x && y == prev_y)
3880 continue;
3882 prev_x = x;
3883 prev_y = y;
3884 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3886 if (!dash_pattern[dash_pos])
3887 continue;
3889 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3890 continue;
3892 if (brush_bits)
3893 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3894 else
3895 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3901 /* draw output image */
3902 if (stat == Ok)
3904 gdi_transform_acquire(graphics);
3906 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3907 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3908 PixelFormat32bppARGB);
3910 gdi_transform_release(graphics);
3913 heap_free(brush_bits);
3914 heap_free(dyn_dash_pattern);
3915 heap_free(output_bits);
3918 GdipDeletePath(flat_path);
3920 return stat;
3923 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3925 GpStatus stat;
3926 GpPath *wide_path;
3927 GpMatrix *transform=NULL;
3928 REAL flatness=1.0;
3930 /* Check if the final pen thickness in pixels is too thin. */
3931 if (pen->unit == UnitPixel)
3933 if (pen->width < 1.415)
3934 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3936 else
3938 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3940 points[1].X = pen->width;
3941 points[2].Y = pen->width;
3943 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3944 CoordinateSpaceWorld, points, 3);
3946 if (stat != Ok)
3947 return stat;
3949 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3950 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3951 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3952 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3953 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3956 stat = GdipClonePath(path, &wide_path);
3958 if (stat != Ok)
3959 return stat;
3961 if (pen->unit == UnitPixel)
3963 /* We have to transform this to device coordinates to get the widths right. */
3964 stat = GdipCreateMatrix(&transform);
3966 if (stat == Ok)
3967 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3968 CoordinateSpaceWorld, transform);
3970 else
3972 /* Set flatness based on the final coordinate space */
3973 GpMatrix t;
3975 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3976 CoordinateSpaceWorld, &t);
3978 if (stat != Ok)
3979 return stat;
3981 flatness = 1.0/sqrt(fmax(
3982 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
3983 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
3986 if (stat == Ok)
3987 stat = GdipWidenPath(wide_path, pen, transform, flatness);
3989 if (pen->unit == UnitPixel)
3991 /* Transform the path back to world coordinates */
3992 if (stat == Ok)
3993 stat = GdipInvertMatrix(transform);
3995 if (stat == Ok)
3996 stat = GdipTransformPath(wide_path, transform);
3999 /* Actually draw the path */
4000 if (stat == Ok)
4001 stat = GdipFillPath(graphics, pen->brush, wide_path);
4003 GdipDeleteMatrix(transform);
4005 GdipDeletePath(wide_path);
4007 return stat;
4010 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
4012 GpStatus retval;
4014 TRACE("(%p, %p, %p)\n", graphics, pen, path);
4016 if(!pen || !graphics)
4017 return InvalidParameter;
4019 if(graphics->busy)
4020 return ObjectBusy;
4022 if (path->pathdata.Count == 0)
4023 return Ok;
4025 if (is_metafile_graphics(graphics))
4026 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
4027 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
4028 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
4029 else
4030 retval = GDI32_GdipDrawPath(graphics, pen, path);
4032 return retval;
4035 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
4036 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4038 GpStatus status;
4039 GpPath *path;
4041 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
4042 width, height, startAngle, sweepAngle);
4044 if(!graphics || !pen)
4045 return InvalidParameter;
4047 if(graphics->busy)
4048 return ObjectBusy;
4050 status = GdipCreatePath(FillModeAlternate, &path);
4051 if (status != Ok) return status;
4053 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4054 if (status == Ok)
4055 status = GdipDrawPath(graphics, pen, path);
4057 GdipDeletePath(path);
4058 return status;
4061 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
4062 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4064 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
4065 width, height, startAngle, sweepAngle);
4067 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4070 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
4071 REAL y, REAL width, REAL height)
4073 GpRectF rect;
4075 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
4077 set_rect(&rect, x, y, width, height);
4078 return GdipDrawRectangles(graphics, pen, &rect, 1);
4081 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
4082 INT y, INT width, INT height)
4084 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
4086 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4089 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
4090 GDIPCONST GpRectF* rects, INT count)
4092 GpStatus status;
4093 GpPath *path;
4095 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4097 if(!graphics || !pen || !rects || count < 1)
4098 return InvalidParameter;
4100 if(graphics->busy)
4101 return ObjectBusy;
4103 if (is_metafile_graphics(graphics))
4104 return METAFILE_DrawRectangles((GpMetafile *)graphics->image, pen, rects, count);
4106 status = GdipCreatePath(FillModeAlternate, &path);
4107 if (status != Ok) return status;
4109 status = GdipAddPathRectangles(path, rects, count);
4110 if (status == Ok)
4111 status = GdipDrawPath(graphics, pen, path);
4113 GdipDeletePath(path);
4114 return status;
4117 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4118 GDIPCONST GpRect* rects, INT count)
4120 GpRectF *rectsF;
4121 GpStatus ret;
4122 INT i;
4124 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4126 if(!rects || count<=0)
4127 return InvalidParameter;
4129 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4130 if(!rectsF)
4131 return OutOfMemory;
4133 for(i = 0;i < count;i++)
4134 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4136 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4137 heap_free(rectsF);
4139 return ret;
4142 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4143 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4145 GpPath *path;
4146 GpStatus status;
4148 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4149 count, tension, fill);
4151 if(!graphics || !brush || !points)
4152 return InvalidParameter;
4154 if(graphics->busy)
4155 return ObjectBusy;
4157 if(count == 1) /* Do nothing */
4158 return Ok;
4160 status = GdipCreatePath(fill, &path);
4161 if (status != Ok) return status;
4163 status = GdipAddPathClosedCurve2(path, points, count, tension);
4164 if (status == Ok)
4165 status = GdipFillPath(graphics, brush, path);
4167 GdipDeletePath(path);
4168 return status;
4171 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4172 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4174 GpPointF *ptf;
4175 GpStatus stat;
4176 INT i;
4178 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4179 count, tension, fill);
4181 if(!points || count == 0)
4182 return InvalidParameter;
4184 if(count == 1) /* Do nothing */
4185 return Ok;
4187 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4188 if(!ptf)
4189 return OutOfMemory;
4191 for(i = 0;i < count;i++){
4192 ptf[i].X = (REAL)points[i].X;
4193 ptf[i].Y = (REAL)points[i].Y;
4196 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4198 heap_free(ptf);
4200 return stat;
4203 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4204 GDIPCONST GpPointF *points, INT count)
4206 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4207 return GdipFillClosedCurve2(graphics, brush, points, count,
4208 0.5f, FillModeAlternate);
4211 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4212 GDIPCONST GpPoint *points, INT count)
4214 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4215 return GdipFillClosedCurve2I(graphics, brush, points, count,
4216 0.5f, FillModeAlternate);
4219 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4220 REAL y, REAL width, REAL height)
4222 GpStatus stat;
4223 GpPath *path;
4224 GpRectF rect;
4226 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4228 if(!graphics || !brush)
4229 return InvalidParameter;
4231 if(graphics->busy)
4232 return ObjectBusy;
4234 if (is_metafile_graphics(graphics))
4236 set_rect(&rect, x, y, width, height);
4237 return METAFILE_FillEllipse((GpMetafile *)graphics->image, brush, &rect);
4240 stat = GdipCreatePath(FillModeAlternate, &path);
4242 if (stat == Ok)
4244 stat = GdipAddPathEllipse(path, x, y, width, height);
4246 if (stat == Ok)
4247 stat = GdipFillPath(graphics, brush, path);
4249 GdipDeletePath(path);
4252 return stat;
4255 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4256 INT y, INT width, INT height)
4258 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4260 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4263 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4265 INT save_state;
4266 GpStatus retval;
4267 HRGN hrgn=NULL;
4269 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4270 return NotImplemented;
4272 save_state = SaveDC(graphics->hdc);
4273 EndPath(graphics->hdc);
4274 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4275 : WINDING));
4277 retval = get_clip_hrgn(graphics, &hrgn);
4279 if (retval != Ok)
4280 goto end;
4282 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4284 gdi_transform_acquire(graphics);
4286 BeginPath(graphics->hdc);
4287 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4288 path->pathdata.Types, path->pathdata.Count, FALSE);
4290 if(retval == Ok)
4292 EndPath(graphics->hdc);
4293 retval = brush_fill_path(graphics, brush);
4296 gdi_transform_release(graphics);
4298 end:
4299 RestoreDC(graphics->hdc, save_state);
4300 DeleteObject(hrgn);
4302 return retval;
4305 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4307 GpStatus stat;
4308 GpRegion *rgn;
4310 if (!brush_can_fill_pixels(brush))
4311 return NotImplemented;
4313 /* FIXME: This could probably be done more efficiently without regions. */
4315 stat = GdipCreateRegionPath(path, &rgn);
4317 if (stat == Ok)
4319 stat = GdipFillRegion(graphics, brush, rgn);
4321 GdipDeleteRegion(rgn);
4324 return stat;
4327 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4329 GpStatus stat = NotImplemented;
4331 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4333 if(!brush || !graphics || !path)
4334 return InvalidParameter;
4336 if(graphics->busy)
4337 return ObjectBusy;
4339 if (!path->pathdata.Count)
4340 return Ok;
4342 if (is_metafile_graphics(graphics))
4343 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4345 if (!graphics->image && !graphics->alpha_hdc)
4346 stat = GDI32_GdipFillPath(graphics, brush, path);
4348 if (stat == NotImplemented)
4349 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4351 if (stat == NotImplemented)
4353 FIXME("Not implemented for brushtype %i\n", brush->bt);
4354 stat = Ok;
4357 return stat;
4360 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4361 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4363 GpStatus stat;
4364 GpPath *path;
4365 GpRectF rect;
4367 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4368 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4370 if(!graphics || !brush)
4371 return InvalidParameter;
4373 if(graphics->busy)
4374 return ObjectBusy;
4376 if (is_metafile_graphics(graphics))
4378 set_rect(&rect, x, y, width, height);
4379 return METAFILE_FillPie((GpMetafile *)graphics->image, brush, &rect, startAngle, sweepAngle);
4382 stat = GdipCreatePath(FillModeAlternate, &path);
4384 if (stat == Ok)
4386 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4388 if (stat == Ok)
4389 stat = GdipFillPath(graphics, brush, path);
4391 GdipDeletePath(path);
4394 return stat;
4397 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4398 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4400 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4401 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4403 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4406 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4407 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4409 GpStatus stat;
4410 GpPath *path;
4412 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4414 if(!graphics || !brush || !points || !count)
4415 return InvalidParameter;
4417 if(graphics->busy)
4418 return ObjectBusy;
4420 stat = GdipCreatePath(fillMode, &path);
4422 if (stat == Ok)
4424 stat = GdipAddPathPolygon(path, points, count);
4426 if (stat == Ok)
4427 stat = GdipFillPath(graphics, brush, path);
4429 GdipDeletePath(path);
4432 return stat;
4435 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4436 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4438 GpStatus stat;
4439 GpPath *path;
4441 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4443 if(!graphics || !brush || !points || !count)
4444 return InvalidParameter;
4446 if(graphics->busy)
4447 return ObjectBusy;
4449 stat = GdipCreatePath(fillMode, &path);
4451 if (stat == Ok)
4453 stat = GdipAddPathPolygonI(path, points, count);
4455 if (stat == Ok)
4456 stat = GdipFillPath(graphics, brush, path);
4458 GdipDeletePath(path);
4461 return stat;
4464 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4465 GDIPCONST GpPointF *points, INT count)
4467 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4469 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4472 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4473 GDIPCONST GpPoint *points, INT count)
4475 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4477 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4480 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4481 REAL x, REAL y, REAL width, REAL height)
4483 GpRectF rect;
4485 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4487 set_rect(&rect, x, y, width, height);
4488 return GdipFillRectangles(graphics, brush, &rect, 1);
4491 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4492 INT x, INT y, INT width, INT height)
4494 GpRectF rect;
4496 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4498 set_rect(&rect, x, y, width, height);
4499 return GdipFillRectangles(graphics, brush, &rect, 1);
4502 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4503 INT count)
4505 GpStatus status;
4506 GpPath *path;
4508 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4510 if(!graphics || !brush || !rects || count <= 0)
4511 return InvalidParameter;
4513 if (is_metafile_graphics(graphics))
4515 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4516 /* FIXME: Add gdi32 drawing. */
4517 return status;
4520 status = GdipCreatePath(FillModeAlternate, &path);
4521 if (status != Ok) return status;
4523 status = GdipAddPathRectangles(path, rects, count);
4524 if (status == Ok)
4525 status = GdipFillPath(graphics, brush, path);
4527 GdipDeletePath(path);
4528 return status;
4531 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4532 INT count)
4534 GpRectF *rectsF;
4535 GpStatus ret;
4536 INT i;
4538 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4540 if(!rects || count <= 0)
4541 return InvalidParameter;
4543 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4544 if(!rectsF)
4545 return OutOfMemory;
4547 for(i = 0; i < count; i++)
4548 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4550 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4551 heap_free(rectsF);
4553 return ret;
4556 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4557 GpRegion* region)
4559 INT save_state;
4560 GpStatus status;
4561 HRGN hrgn;
4562 RECT rc;
4564 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4565 return NotImplemented;
4567 save_state = SaveDC(graphics->hdc);
4568 EndPath(graphics->hdc);
4570 hrgn = NULL;
4571 status = get_clip_hrgn(graphics, &hrgn);
4572 if (status != Ok)
4574 RestoreDC(graphics->hdc, save_state);
4575 return status;
4578 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4579 DeleteObject(hrgn);
4581 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4582 if (status != Ok)
4584 RestoreDC(graphics->hdc, save_state);
4585 return status;
4588 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4589 DeleteObject(hrgn);
4591 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4593 BeginPath(graphics->hdc);
4594 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4595 EndPath(graphics->hdc);
4597 status = brush_fill_path(graphics, brush);
4600 RestoreDC(graphics->hdc, save_state);
4603 return status;
4606 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4607 GpRegion* region)
4609 GpStatus stat;
4610 GpRegion *temp_region;
4611 GpMatrix world_to_device;
4612 GpRectF graphics_bounds;
4613 DWORD *pixel_data;
4614 HRGN hregion;
4615 RECT bound_rect;
4616 GpRect gp_bound_rect;
4618 if (!brush_can_fill_pixels(brush))
4619 return NotImplemented;
4621 stat = gdi_transform_acquire(graphics);
4623 if (stat == Ok)
4624 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4626 if (stat == Ok)
4627 stat = GdipCloneRegion(region, &temp_region);
4629 if (stat == Ok)
4631 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4632 CoordinateSpaceWorld, &world_to_device);
4634 if (stat == Ok)
4635 stat = GdipTransformRegion(temp_region, &world_to_device);
4637 if (stat == Ok)
4638 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4640 if (stat == Ok)
4641 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4643 GdipDeleteRegion(temp_region);
4646 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4648 DeleteObject(hregion);
4649 gdi_transform_release(graphics);
4650 return Ok;
4653 if (stat == Ok)
4655 gp_bound_rect.X = bound_rect.left;
4656 gp_bound_rect.Y = bound_rect.top;
4657 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4658 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4660 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4661 if (!pixel_data)
4662 stat = OutOfMemory;
4664 if (stat == Ok)
4666 stat = brush_fill_pixels(graphics, brush, pixel_data,
4667 &gp_bound_rect, gp_bound_rect.Width);
4669 if (stat == Ok)
4670 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4671 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4672 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4673 PixelFormat32bppARGB);
4675 heap_free(pixel_data);
4678 DeleteObject(hregion);
4681 gdi_transform_release(graphics);
4683 return stat;
4686 /*****************************************************************************
4687 * GdipFillRegion [GDIPLUS.@]
4689 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4690 GpRegion* region)
4692 GpStatus stat = NotImplemented;
4694 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4696 if (!(graphics && brush && region))
4697 return InvalidParameter;
4699 if(graphics->busy)
4700 return ObjectBusy;
4702 if (is_metafile_graphics(graphics))
4703 stat = METAFILE_FillRegion((GpMetafile*)graphics->image, brush, region);
4704 else
4706 if (!graphics->image && !graphics->alpha_hdc)
4707 stat = GDI32_GdipFillRegion(graphics, brush, region);
4709 if (stat == NotImplemented)
4710 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4713 if (stat == NotImplemented)
4715 FIXME("not implemented for brushtype %i\n", brush->bt);
4716 stat = Ok;
4719 return stat;
4722 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4724 TRACE("(%p,%u)\n", graphics, intention);
4726 if(!graphics)
4727 return InvalidParameter;
4729 if(graphics->busy)
4730 return ObjectBusy;
4732 /* We have no internal operation queue, so there's no need to clear it. */
4734 if (graphics->hdc)
4735 GdiFlush();
4737 return Ok;
4740 /*****************************************************************************
4741 * GdipGetClipBounds [GDIPLUS.@]
4743 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4745 GpStatus status;
4746 GpRegion *clip;
4748 TRACE("(%p, %p)\n", graphics, rect);
4750 if(!graphics)
4751 return InvalidParameter;
4753 if(graphics->busy)
4754 return ObjectBusy;
4756 status = GdipCreateRegion(&clip);
4757 if (status != Ok) return status;
4759 status = GdipGetClip(graphics, clip);
4760 if (status == Ok)
4761 status = GdipGetRegionBounds(clip, graphics, rect);
4763 GdipDeleteRegion(clip);
4764 return status;
4767 /*****************************************************************************
4768 * GdipGetClipBoundsI [GDIPLUS.@]
4770 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4772 GpRectF rectf;
4773 GpStatus stat;
4775 TRACE("(%p, %p)\n", graphics, rect);
4777 if (!rect)
4778 return InvalidParameter;
4780 if ((stat = GdipGetClipBounds(graphics, &rectf)) == Ok)
4782 rect->X = gdip_round(rectf.X);
4783 rect->Y = gdip_round(rectf.Y);
4784 rect->Width = gdip_round(rectf.Width);
4785 rect->Height = gdip_round(rectf.Height);
4788 return stat;
4791 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4792 CompositingMode *mode)
4794 TRACE("(%p, %p)\n", graphics, mode);
4796 if(!graphics || !mode)
4797 return InvalidParameter;
4799 if(graphics->busy)
4800 return ObjectBusy;
4802 *mode = graphics->compmode;
4804 return Ok;
4807 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4808 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4809 CompositingQuality *quality)
4811 TRACE("(%p, %p)\n", graphics, quality);
4813 if(!graphics || !quality)
4814 return InvalidParameter;
4816 if(graphics->busy)
4817 return ObjectBusy;
4819 *quality = graphics->compqual;
4821 return Ok;
4824 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4825 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4826 InterpolationMode *mode)
4828 TRACE("(%p, %p)\n", graphics, mode);
4830 if(!graphics || !mode)
4831 return InvalidParameter;
4833 if(graphics->busy)
4834 return ObjectBusy;
4836 *mode = graphics->interpolation;
4838 return Ok;
4841 /* FIXME: Need to handle color depths less than 24bpp */
4842 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4844 TRACE("(%p, %p)\n", graphics, argb);
4846 if(!graphics || !argb)
4847 return InvalidParameter;
4849 if(graphics->busy)
4850 return ObjectBusy;
4852 if (graphics->image && graphics->image->type == ImageTypeBitmap)
4854 static int once;
4855 GpBitmap *bitmap = (GpBitmap *)graphics->image;
4856 if (IsIndexedPixelFormat(bitmap->format) && !once++)
4857 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4860 return Ok;
4863 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4865 TRACE("(%p, %p)\n", graphics, scale);
4867 if(!graphics || !scale)
4868 return InvalidParameter;
4870 if(graphics->busy)
4871 return ObjectBusy;
4873 *scale = graphics->scale;
4875 return Ok;
4878 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4880 TRACE("(%p, %p)\n", graphics, unit);
4882 if(!graphics || !unit)
4883 return InvalidParameter;
4885 if(graphics->busy)
4886 return ObjectBusy;
4888 *unit = graphics->unit;
4890 return Ok;
4893 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4894 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4895 *mode)
4897 TRACE("(%p, %p)\n", graphics, mode);
4899 if(!graphics || !mode)
4900 return InvalidParameter;
4902 if(graphics->busy)
4903 return ObjectBusy;
4905 *mode = graphics->pixeloffset;
4907 return Ok;
4910 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4911 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4913 TRACE("(%p, %p)\n", graphics, mode);
4915 if(!graphics || !mode)
4916 return InvalidParameter;
4918 if(graphics->busy)
4919 return ObjectBusy;
4921 *mode = graphics->smoothing;
4923 return Ok;
4926 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4928 TRACE("(%p, %p)\n", graphics, contrast);
4930 if(!graphics || !contrast)
4931 return InvalidParameter;
4933 *contrast = graphics->textcontrast;
4935 return Ok;
4938 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4939 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4940 TextRenderingHint *hint)
4942 TRACE("(%p, %p)\n", graphics, hint);
4944 if(!graphics || !hint)
4945 return InvalidParameter;
4947 if(graphics->busy)
4948 return ObjectBusy;
4950 *hint = graphics->texthint;
4952 return Ok;
4955 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4957 GpRegion *clip_rgn;
4958 GpStatus stat;
4959 GpMatrix device_to_world;
4961 TRACE("(%p, %p)\n", graphics, rect);
4963 if(!graphics || !rect)
4964 return InvalidParameter;
4966 if(graphics->busy)
4967 return ObjectBusy;
4969 /* intersect window and graphics clipping regions */
4970 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4971 return stat;
4973 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4974 goto cleanup;
4976 /* transform to world coordinates */
4977 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4978 goto cleanup;
4980 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4981 goto cleanup;
4983 /* get bounds of the region */
4984 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4986 cleanup:
4987 GdipDeleteRegion(clip_rgn);
4989 return stat;
4992 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4994 GpRectF rectf;
4995 GpStatus stat;
4997 TRACE("(%p, %p)\n", graphics, rect);
4999 if(!graphics || !rect)
5000 return InvalidParameter;
5002 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
5004 rect->X = gdip_round(rectf.X);
5005 rect->Y = gdip_round(rectf.Y);
5006 rect->Width = gdip_round(rectf.Width);
5007 rect->Height = gdip_round(rectf.Height);
5010 return stat;
5013 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5015 TRACE("(%p, %s)\n", graphics, debugstr_matrix(matrix));
5017 if(!graphics || !matrix)
5018 return InvalidParameter;
5020 if(graphics->busy)
5021 return ObjectBusy;
5023 *matrix = graphics->worldtrans;
5024 return Ok;
5027 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
5029 GpSolidFill *brush;
5030 GpStatus stat;
5031 GpRectF wnd_rect;
5032 CompositingMode prev_comp_mode;
5034 TRACE("(%p, %lx)\n", graphics, color);
5036 if(!graphics)
5037 return InvalidParameter;
5039 if(graphics->busy)
5040 return ObjectBusy;
5042 if (is_metafile_graphics(graphics))
5043 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
5045 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
5046 return stat;
5048 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
5049 GdipDeleteBrush((GpBrush*)brush);
5050 return stat;
5053 GdipGetCompositingMode(graphics, &prev_comp_mode);
5054 GdipSetCompositingMode(graphics, CompositingModeSourceCopy);
5055 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
5056 wnd_rect.Width, wnd_rect.Height);
5057 GdipSetCompositingMode(graphics, prev_comp_mode);
5059 GdipDeleteBrush((GpBrush*)brush);
5061 return Ok;
5064 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
5066 TRACE("(%p, %p)\n", graphics, res);
5068 if(!graphics || !res)
5069 return InvalidParameter;
5071 return GdipIsEmptyRegion(graphics->clip, graphics, res);
5074 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
5076 GpStatus stat;
5077 GpRegion* rgn;
5078 GpPointF pt;
5080 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
5082 if(!graphics || !result)
5083 return InvalidParameter;
5085 if(graphics->busy)
5086 return ObjectBusy;
5088 pt.X = x;
5089 pt.Y = y;
5090 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5091 CoordinateSpaceWorld, &pt, 1)) != Ok)
5092 return stat;
5094 if((stat = GdipCreateRegion(&rgn)) != Ok)
5095 return stat;
5097 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5098 goto cleanup;
5100 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
5102 cleanup:
5103 GdipDeleteRegion(rgn);
5104 return stat;
5107 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
5109 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
5112 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
5114 GpStatus stat;
5115 GpRegion* rgn;
5116 GpPointF pts[2];
5118 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
5120 if(!graphics || !result)
5121 return InvalidParameter;
5123 if(graphics->busy)
5124 return ObjectBusy;
5126 pts[0].X = x;
5127 pts[0].Y = y;
5128 pts[1].X = x + width;
5129 pts[1].Y = y + height;
5131 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5132 CoordinateSpaceWorld, pts, 2)) != Ok)
5133 return stat;
5135 pts[1].X -= pts[0].X;
5136 pts[1].Y -= pts[0].Y;
5138 if((stat = GdipCreateRegion(&rgn)) != Ok)
5139 return stat;
5141 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5142 goto cleanup;
5144 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5146 cleanup:
5147 GdipDeleteRegion(rgn);
5148 return stat;
5151 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5153 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5156 GpStatus gdip_format_string(HDC hdc,
5157 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5158 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5159 gdip_format_string_callback callback, void *user_data)
5161 WCHAR* stringdup;
5162 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5163 nheight, lineend, lineno = 0;
5164 RectF bounds;
5165 StringAlignment halign;
5166 GpStatus stat = Ok;
5167 SIZE size;
5168 HotkeyPrefix hkprefix;
5169 INT *hotkeyprefix_offsets=NULL;
5170 INT hotkeyprefix_count=0;
5171 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5172 BOOL seen_prefix = FALSE, unixstyle_newline = TRUE;
5174 if(length == -1) length = lstrlenW(string);
5176 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5177 if(!stringdup) return OutOfMemory;
5179 if (!format)
5180 format = &default_drawstring_format;
5182 nwidth = rect->Width;
5183 nheight = rect->Height;
5184 if (ignore_empty_clip)
5186 if (!nwidth) nwidth = INT_MAX;
5187 if (!nheight) nheight = INT_MAX;
5190 hkprefix = format->hkprefix;
5192 if (hkprefix == HotkeyPrefixShow)
5194 for (i=0; i<length; i++)
5196 if (string[i] == '&')
5197 hotkeyprefix_count++;
5201 if (hotkeyprefix_count)
5203 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5204 if (!hotkeyprefix_offsets)
5206 heap_free(stringdup);
5207 return OutOfMemory;
5211 hotkeyprefix_count = 0;
5213 for(i = 0, j = 0; i < length; i++){
5215 /* FIXME: tabs should be handled using tabstops from stringformat */
5216 if (string[i] == '\t')
5217 continue;
5219 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5220 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5221 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5223 seen_prefix = TRUE;
5224 continue;
5227 seen_prefix = FALSE;
5229 stringdup[j] = string[i];
5230 j++;
5233 length = j;
5235 halign = format->align;
5237 while(sum < length){
5238 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5239 nwidth, &fit, NULL, &size);
5240 fitcpy = fit;
5242 if(fit == 0)
5243 break;
5245 for(lret = 0; lret < fit; lret++) {
5246 if(*(stringdup + sum + lret) == '\n')
5248 unixstyle_newline = TRUE;
5249 break;
5252 if(*(stringdup + sum + lret) == '\r' && lret + 1 < fit
5253 && *(stringdup + sum + lret + 1) == '\n')
5255 unixstyle_newline = FALSE;
5256 break;
5260 /* Line break code (may look strange, but it imitates windows). */
5261 if(lret < fit)
5262 lineend = fit = lret; /* this is not an off-by-one error */
5263 else if(fit < (length - sum)){
5264 if(*(stringdup + sum + fit) == ' ')
5265 while(*(stringdup + sum + fit) == ' ')
5266 fit++;
5267 else if (!(format->attr & StringFormatFlagsNoWrap))
5268 while(*(stringdup + sum + fit - 1) != ' '){
5269 fit--;
5271 if(*(stringdup + sum + fit) == '\t')
5272 break;
5274 if(fit == 0){
5275 fit = fitcpy;
5276 break;
5279 lineend = fit;
5280 while(*(stringdup + sum + lineend - 1) == ' ' ||
5281 *(stringdup + sum + lineend - 1) == '\t')
5282 lineend--;
5284 else
5285 lineend = fit;
5287 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5288 nwidth, &j, NULL, &size);
5290 bounds.Width = size.cx;
5292 if(height + size.cy > nheight)
5294 if (format->attr & StringFormatFlagsLineLimit)
5295 break;
5296 bounds.Height = nheight - (height + size.cy);
5298 else
5299 bounds.Height = size.cy;
5301 bounds.Y = rect->Y + height;
5303 switch (halign)
5305 case StringAlignmentNear:
5306 default:
5307 bounds.X = rect->X;
5308 break;
5309 case StringAlignmentCenter:
5310 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5311 break;
5312 case StringAlignmentFar:
5313 bounds.X = rect->X + rect->Width - bounds.Width;
5314 break;
5317 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5318 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5319 break;
5321 stat = callback(hdc, stringdup, sum, lineend,
5322 font, rect, format, lineno, &bounds,
5323 &hotkeyprefix_offsets[hotkeyprefix_pos],
5324 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5326 if (stat != Ok)
5327 break;
5330 if (unixstyle_newline)
5332 height += size.cy;
5333 lineno++;
5334 sum += fit + (lret < fitcpy ? 1 : 0);
5336 else
5338 height += size.cy;
5339 lineno++;
5340 sum += fit + (lret < fitcpy ? 2 : 0);
5343 hotkeyprefix_pos = hotkeyprefix_end_pos;
5345 if(height > nheight)
5346 break;
5348 /* Stop if this was a linewrap (but not if it was a linebreak). */
5349 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5350 break;
5353 heap_free(stringdup);
5354 heap_free(hotkeyprefix_offsets);
5356 return stat;
5359 void transform_properties(GpGraphics *graphics, GDIPCONST GpMatrix *matrix, BOOL graphics_transform,
5360 REAL *rel_width, REAL *rel_height, REAL *angle)
5362 GpPointF pt[3] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {0.0f, 1.0f}};
5363 GpMatrix xform;
5365 if (matrix)
5367 xform = *matrix;
5368 GdipTransformMatrixPoints(&xform, pt, 3);
5371 if (graphics_transform)
5372 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5374 if (rel_width)
5375 *rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5376 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5377 if (rel_height)
5378 *rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5379 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5380 if (angle)
5381 *angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
5384 struct measure_ranges_args {
5385 GpRegion **regions;
5386 REAL rel_width, rel_height;
5389 static GpStatus measure_ranges_callback(HDC hdc,
5390 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5391 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5392 INT lineno, const RectF *bounds, INT *underlined_indexes,
5393 INT underlined_index_count, void *user_data)
5395 int i;
5396 GpStatus stat = Ok;
5397 struct measure_ranges_args *args = user_data;
5399 for (i=0; i<format->range_count; i++)
5401 INT range_start = max(index, format->character_ranges[i].First);
5402 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5403 if (range_start < range_end)
5405 GpRectF range_rect;
5406 SIZE range_size;
5408 range_rect.Y = bounds->Y / args->rel_height;
5409 range_rect.Height = bounds->Height / args->rel_height;
5411 GetTextExtentExPointW(hdc, string + index, range_start - index,
5412 INT_MAX, NULL, NULL, &range_size);
5413 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5415 GetTextExtentExPointW(hdc, string + index, range_end - index,
5416 INT_MAX, NULL, NULL, &range_size);
5417 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5419 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5420 if (stat != Ok)
5421 break;
5425 return stat;
5428 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5429 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5430 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5431 INT regionCount, GpRegion** regions)
5433 GpStatus stat;
5434 int i;
5435 HFONT gdifont, oldfont;
5436 struct measure_ranges_args args;
5437 HDC hdc, temp_hdc=NULL;
5438 RectF scaled_rect;
5439 REAL margin_x;
5441 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_wn(string, length),
5442 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5444 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5445 return InvalidParameter;
5447 if (regionCount < stringFormat->range_count)
5448 return InvalidParameter;
5450 if(!graphics->hdc)
5452 hdc = temp_hdc = CreateCompatibleDC(0);
5453 if (!temp_hdc) return OutOfMemory;
5455 else
5456 hdc = graphics->hdc;
5458 if (stringFormat->attr)
5459 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5462 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5463 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5464 transform_properties(graphics, NULL, TRUE, &args.rel_width, &args.rel_height, NULL);
5465 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5466 scaled_rect.Y = layoutRect->Y * args.rel_height;
5467 scaled_rect.Width = layoutRect->Width * args.rel_width;
5468 scaled_rect.Height = layoutRect->Height * args.rel_height;
5470 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5471 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5473 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL, NULL);
5474 oldfont = SelectObject(hdc, gdifont);
5476 for (i=0; i<stringFormat->range_count; i++)
5478 stat = GdipSetEmpty(regions[i]);
5479 if (stat != Ok)
5481 SelectObject(hdc, oldfont);
5482 DeleteObject(gdifont);
5483 if (temp_hdc)
5484 DeleteDC(temp_hdc);
5485 return stat;
5489 args.regions = regions;
5491 gdi_transform_acquire(graphics);
5493 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5494 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5496 gdi_transform_release(graphics);
5498 SelectObject(hdc, oldfont);
5499 DeleteObject(gdifont);
5501 if (temp_hdc)
5502 DeleteDC(temp_hdc);
5504 return stat;
5507 struct measure_string_args {
5508 RectF *bounds;
5509 INT *codepointsfitted;
5510 INT *linesfilled;
5511 REAL rel_width, rel_height;
5514 static GpStatus measure_string_callback(HDC hdc,
5515 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5516 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5517 INT lineno, const RectF *bounds, INT *underlined_indexes,
5518 INT underlined_index_count, void *user_data)
5520 struct measure_string_args *args = user_data;
5521 REAL new_width, new_height;
5523 new_width = bounds->Width / args->rel_width;
5524 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5526 if (new_width > args->bounds->Width)
5527 args->bounds->Width = new_width;
5529 if (new_height > args->bounds->Height)
5530 args->bounds->Height = new_height;
5532 if (args->codepointsfitted)
5533 *args->codepointsfitted = index + length;
5535 if (args->linesfilled)
5536 (*args->linesfilled)++;
5538 return Ok;
5541 /* Find the smallest rectangle that bounds the text when it is printed in rect
5542 * according to the format options listed in format. If rect has 0 width and
5543 * height, then just find the smallest rectangle that bounds the text when it's
5544 * printed at location (rect->X, rect-Y). */
5545 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5546 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5547 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5548 INT *codepointsfitted, INT *linesfilled)
5550 HFONT oldfont, gdifont;
5551 struct measure_string_args args;
5552 HDC temp_hdc=NULL, hdc;
5553 RectF scaled_rect;
5554 REAL margin_x;
5555 INT lines, glyphs;
5557 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5558 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5559 bounds, codepointsfitted, linesfilled);
5561 if(!graphics || !string || !font || !rect || !bounds)
5562 return InvalidParameter;
5564 if(!graphics->hdc)
5566 hdc = temp_hdc = CreateCompatibleDC(0);
5567 if (!temp_hdc) return OutOfMemory;
5569 else
5570 hdc = graphics->hdc;
5572 if(linesfilled) *linesfilled = 0;
5573 if(codepointsfitted) *codepointsfitted = 0;
5575 if(format)
5576 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5578 transform_properties(graphics, NULL, TRUE, &args.rel_width, &args.rel_height, NULL);
5579 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5580 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5582 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5583 scaled_rect.Y = rect->Y * args.rel_height;
5584 scaled_rect.Width = rect->Width * args.rel_width;
5585 scaled_rect.Height = rect->Height * args.rel_height;
5586 if (scaled_rect.Width >= 0.5)
5588 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5589 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5592 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5593 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5595 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5596 oldfont = SelectObject(hdc, gdifont);
5598 set_rect(bounds, rect->X, rect->Y, 0.0f, 0.0f);
5600 args.bounds = bounds;
5601 args.codepointsfitted = &glyphs;
5602 args.linesfilled = &lines;
5603 lines = glyphs = 0;
5605 gdi_transform_acquire(graphics);
5607 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5608 measure_string_callback, &args);
5610 gdi_transform_release(graphics);
5612 if (linesfilled) *linesfilled = lines;
5613 if (codepointsfitted) *codepointsfitted = glyphs;
5615 if (lines)
5616 bounds->Width += margin_x * 2.0;
5618 SelectObject(hdc, oldfont);
5619 DeleteObject(gdifont);
5621 if (temp_hdc)
5622 DeleteDC(temp_hdc);
5624 return Ok;
5627 struct draw_string_args {
5628 GpGraphics *graphics;
5629 GDIPCONST GpBrush *brush;
5630 REAL x, y, rel_width, rel_height, ascent;
5633 static GpStatus draw_string_callback(HDC hdc,
5634 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5635 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5636 INT lineno, const RectF *bounds, INT *underlined_indexes,
5637 INT underlined_index_count, void *user_data)
5639 struct draw_string_args *args = user_data;
5640 PointF position;
5641 GpStatus stat;
5643 position.X = args->x + bounds->X / args->rel_width;
5644 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5646 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5647 args->brush, &position,
5648 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5650 if (stat == Ok && underlined_index_count)
5652 OUTLINETEXTMETRICW otm;
5653 REAL underline_y, underline_height;
5654 int i;
5656 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5658 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5659 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5661 for (i=0; i<underlined_index_count; i++)
5663 REAL start_x, end_x;
5664 SIZE text_size;
5665 INT ofs = underlined_indexes[i] - index;
5667 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5668 start_x = text_size.cx / args->rel_width;
5670 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5671 end_x = text_size.cx / args->rel_width;
5673 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5677 return stat;
5680 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5681 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5682 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5684 HRGN rgn = NULL;
5685 HFONT gdifont;
5686 GpPointF rectcpy[4];
5687 POINT corners[4];
5688 REAL rel_width, rel_height, margin_x;
5689 INT save_state, format_flags = 0;
5690 REAL offsety = 0.0;
5691 struct draw_string_args args;
5692 RectF scaled_rect;
5693 HDC hdc, temp_hdc=NULL;
5694 TEXTMETRICW textmetric;
5696 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5697 length, font, debugstr_rectf(rect), format, brush);
5699 if(!graphics || !string || !font || !brush || !rect)
5700 return InvalidParameter;
5702 if(graphics->hdc)
5704 hdc = graphics->hdc;
5706 else
5708 hdc = temp_hdc = CreateCompatibleDC(0);
5711 if(format){
5712 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5714 format_flags = format->attr;
5716 /* Should be no need to explicitly test for StringAlignmentNear as
5717 * that is default behavior if no alignment is passed. */
5718 if(format->line_align != StringAlignmentNear){
5719 RectF bounds, in_rect = *rect;
5720 in_rect.Height = 0.0; /* avoid height clipping */
5721 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5723 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5725 if(format->line_align == StringAlignmentCenter)
5726 offsety = (rect->Height - bounds.Height) / 2;
5727 else if(format->line_align == StringAlignmentFar)
5728 offsety = (rect->Height - bounds.Height);
5730 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5733 save_state = SaveDC(hdc);
5735 transform_properties(graphics, NULL, TRUE, &rel_width, &rel_height, NULL);
5736 rectcpy[3].X = rectcpy[0].X = rect->X;
5737 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5738 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5739 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5740 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5741 round_points(corners, rectcpy, 4);
5743 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5744 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5746 scaled_rect.X = margin_x * rel_width;
5747 scaled_rect.Y = 0.0;
5748 scaled_rect.Width = rel_width * rect->Width;
5749 scaled_rect.Height = rel_height * rect->Height;
5750 if (scaled_rect.Width >= 0.5)
5752 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5753 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5756 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5757 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5759 if (!(format_flags & StringFormatFlagsNoClip) &&
5760 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5761 rect->Width > 0.0 && rect->Height > 0.0)
5763 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5764 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5765 SelectClipRgn(hdc, rgn);
5768 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5769 SelectObject(hdc, gdifont);
5771 args.graphics = graphics;
5772 args.brush = brush;
5774 args.x = rect->X;
5775 args.y = rect->Y + offsety;
5777 args.rel_width = rel_width;
5778 args.rel_height = rel_height;
5780 gdi_transform_acquire(graphics);
5782 GetTextMetricsW(hdc, &textmetric);
5783 args.ascent = textmetric.tmAscent / rel_height;
5785 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5786 draw_string_callback, &args);
5788 gdi_transform_release(graphics);
5790 DeleteObject(rgn);
5791 DeleteObject(gdifont);
5793 RestoreDC(hdc, save_state);
5795 DeleteDC(temp_hdc);
5797 return Ok;
5800 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5802 GpStatus stat;
5804 TRACE("(%p)\n", graphics);
5806 if(!graphics)
5807 return InvalidParameter;
5809 if(graphics->busy)
5810 return ObjectBusy;
5812 if (is_metafile_graphics(graphics))
5814 stat = METAFILE_ResetClip((GpMetafile *)graphics->image);
5815 if (stat != Ok)
5816 return stat;
5819 return GdipSetInfinite(graphics->clip);
5822 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5824 GpStatus stat;
5826 TRACE("(%p)\n", graphics);
5828 if(!graphics)
5829 return InvalidParameter;
5831 if(graphics->busy)
5832 return ObjectBusy;
5834 if (is_metafile_graphics(graphics))
5836 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5838 if (stat != Ok)
5839 return stat;
5842 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5845 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5846 GpMatrixOrder order)
5848 GpStatus stat;
5850 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5852 if(!graphics)
5853 return InvalidParameter;
5855 if(graphics->busy)
5856 return ObjectBusy;
5858 if (is_metafile_graphics(graphics))
5860 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5862 if (stat != Ok)
5863 return stat;
5866 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5869 static GpStatus begin_container(GpGraphics *graphics,
5870 GraphicsContainerType type, GraphicsContainer *state)
5872 GraphicsContainerItem *container;
5873 GpStatus sts;
5875 if(!graphics || !state)
5876 return InvalidParameter;
5878 sts = init_container(&container, graphics, type);
5879 if(sts != Ok)
5880 return sts;
5882 list_add_head(&graphics->containers, &container->entry);
5883 *state = graphics->contid = container->contid;
5885 if (is_metafile_graphics(graphics)) {
5886 if (type == BEGIN_CONTAINER)
5887 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5888 else
5889 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5892 return Ok;
5895 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5897 TRACE("(%p, %p)\n", graphics, state);
5898 return begin_container(graphics, SAVE_GRAPHICS, state);
5901 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5902 GraphicsContainer *state)
5904 TRACE("(%p, %p)\n", graphics, state);
5905 return begin_container(graphics, BEGIN_CONTAINER, state);
5908 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5910 GraphicsContainerItem *container;
5911 GpMatrix transform;
5912 GpStatus stat;
5913 GpRectF scaled_srcrect;
5914 REAL scale_x, scale_y;
5916 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5918 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5919 return InvalidParameter;
5921 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5922 if(stat != Ok)
5923 return stat;
5925 list_add_head(&graphics->containers, &container->entry);
5926 *state = graphics->contid = container->contid;
5928 scale_x = units_to_pixels(1.0, unit, graphics->xres, graphics->printer_display);
5929 scale_y = units_to_pixels(1.0, unit, graphics->yres, graphics->printer_display);
5931 scaled_srcrect.X = scale_x * srcrect->X;
5932 scaled_srcrect.Y = scale_y * srcrect->Y;
5933 scaled_srcrect.Width = scale_x * srcrect->Width;
5934 scaled_srcrect.Height = scale_y * srcrect->Height;
5936 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5937 transform.matrix[1] = 0.0;
5938 transform.matrix[2] = 0.0;
5939 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5940 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5941 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5943 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5945 if (is_metafile_graphics(graphics))
5946 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5948 return Ok;
5951 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5953 GpRectF dstrectf, srcrectf;
5955 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5957 if (!dstrect || !srcrect)
5958 return InvalidParameter;
5960 dstrectf.X = dstrect->X;
5961 dstrectf.Y = dstrect->Y;
5962 dstrectf.Width = dstrect->Width;
5963 dstrectf.Height = dstrect->Height;
5965 srcrectf.X = srcrect->X;
5966 srcrectf.Y = srcrect->Y;
5967 srcrectf.Width = srcrect->Width;
5968 srcrectf.Height = srcrect->Height;
5970 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5973 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5975 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5976 return NotImplemented;
5979 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5980 GraphicsContainer state)
5982 GpStatus sts;
5983 GraphicsContainerItem *container, *container2;
5985 if(!graphics)
5986 return InvalidParameter;
5988 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5989 if(container->contid == state && container->type == type)
5990 break;
5993 /* did not find a matching container */
5994 if(&container->entry == &graphics->containers)
5995 return Ok;
5997 sts = restore_container(graphics, container);
5998 if(sts != Ok)
5999 return sts;
6001 /* remove all of the containers on top of the found container */
6002 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
6003 if(container->contid == state)
6004 break;
6005 list_remove(&container->entry);
6006 delete_container(container);
6009 list_remove(&container->entry);
6010 delete_container(container);
6012 if (is_metafile_graphics(graphics)) {
6013 if (type == BEGIN_CONTAINER)
6014 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
6015 else
6016 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
6019 return Ok;
6022 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
6024 TRACE("(%p, %x)\n", graphics, state);
6025 return end_container(graphics, BEGIN_CONTAINER, state);
6028 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
6030 TRACE("(%p, %x)\n", graphics, state);
6031 return end_container(graphics, SAVE_GRAPHICS, state);
6034 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
6035 REAL sy, GpMatrixOrder order)
6037 GpStatus stat;
6039 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
6041 if(!graphics)
6042 return InvalidParameter;
6044 if(graphics->busy)
6045 return ObjectBusy;
6047 if (is_metafile_graphics(graphics)) {
6048 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
6050 if (stat != Ok)
6051 return stat;
6054 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
6057 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
6058 CombineMode mode)
6060 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
6062 if(!graphics || !srcgraphics)
6063 return InvalidParameter;
6065 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
6068 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
6069 CompositingMode mode)
6071 TRACE("(%p, %d)\n", graphics, mode);
6073 if(!graphics)
6074 return InvalidParameter;
6076 if(graphics->busy)
6077 return ObjectBusy;
6079 if(graphics->compmode == mode)
6080 return Ok;
6082 if (is_metafile_graphics(graphics))
6084 GpStatus stat;
6086 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6087 EmfPlusRecordTypeSetCompositingMode, mode);
6088 if(stat != Ok)
6089 return stat;
6092 graphics->compmode = mode;
6094 return Ok;
6097 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
6098 CompositingQuality quality)
6100 TRACE("(%p, %d)\n", graphics, quality);
6102 if(!graphics)
6103 return InvalidParameter;
6105 if(graphics->busy)
6106 return ObjectBusy;
6108 if(graphics->compqual == quality)
6109 return Ok;
6111 if (is_metafile_graphics(graphics))
6113 GpStatus stat;
6115 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6116 EmfPlusRecordTypeSetCompositingQuality, quality);
6117 if(stat != Ok)
6118 return stat;
6121 graphics->compqual = quality;
6123 return Ok;
6126 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
6127 InterpolationMode mode)
6129 TRACE("(%p, %d)\n", graphics, mode);
6131 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
6132 return InvalidParameter;
6134 if(graphics->busy)
6135 return ObjectBusy;
6137 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
6138 mode = InterpolationModeBilinear;
6140 if (mode == InterpolationModeHighQuality)
6141 mode = InterpolationModeHighQualityBicubic;
6143 if (mode == graphics->interpolation)
6144 return Ok;
6146 if (is_metafile_graphics(graphics))
6148 GpStatus stat;
6150 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6151 EmfPlusRecordTypeSetInterpolationMode, mode);
6152 if (stat != Ok)
6153 return stat;
6156 graphics->interpolation = mode;
6158 return Ok;
6161 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6163 GpStatus stat;
6165 TRACE("(%p, %.2f)\n", graphics, scale);
6167 if(!graphics || (scale <= 0.0))
6168 return InvalidParameter;
6170 if(graphics->busy)
6171 return ObjectBusy;
6173 if (is_metafile_graphics(graphics))
6175 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6176 if (stat != Ok)
6177 return stat;
6180 graphics->scale = scale;
6182 return Ok;
6185 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6187 GpStatus stat;
6189 TRACE("(%p, %d)\n", graphics, unit);
6191 if(!graphics)
6192 return InvalidParameter;
6194 if(graphics->busy)
6195 return ObjectBusy;
6197 if(unit == UnitWorld)
6198 return InvalidParameter;
6200 if (is_metafile_graphics(graphics))
6202 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6203 if (stat != Ok)
6204 return stat;
6207 graphics->unit = unit;
6209 return Ok;
6212 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6213 mode)
6215 TRACE("(%p, %d)\n", graphics, mode);
6217 if(!graphics)
6218 return InvalidParameter;
6220 if(graphics->busy)
6221 return ObjectBusy;
6223 if(graphics->pixeloffset == mode)
6224 return Ok;
6226 if (is_metafile_graphics(graphics))
6228 GpStatus stat;
6230 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6231 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6232 if(stat != Ok)
6233 return stat;
6236 graphics->pixeloffset = mode;
6238 return Ok;
6241 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6243 GpStatus stat;
6245 TRACE("(%p,%i,%i)\n", graphics, x, y);
6247 if (!graphics)
6248 return InvalidParameter;
6250 if (graphics->origin_x == x && graphics->origin_y == y)
6251 return Ok;
6253 if (is_metafile_graphics(graphics))
6255 stat = METAFILE_SetRenderingOrigin((GpMetafile *)graphics->image, x, y);
6256 if (stat != Ok)
6257 return stat;
6260 graphics->origin_x = x;
6261 graphics->origin_y = y;
6263 return Ok;
6266 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6268 TRACE("(%p,%p,%p)\n", graphics, x, y);
6270 if (!graphics || !x || !y)
6271 return InvalidParameter;
6273 *x = graphics->origin_x;
6274 *y = graphics->origin_y;
6276 return Ok;
6279 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6281 TRACE("(%p, %d)\n", graphics, mode);
6283 if(!graphics)
6284 return InvalidParameter;
6286 if(graphics->busy)
6287 return ObjectBusy;
6289 if(graphics->smoothing == mode)
6290 return Ok;
6292 if (is_metafile_graphics(graphics))
6294 GpStatus stat;
6295 BOOL antialias = (mode != SmoothingModeDefault &&
6296 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6298 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6299 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6300 if(stat != Ok)
6301 return stat;
6304 graphics->smoothing = mode;
6306 return Ok;
6309 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6311 TRACE("(%p, %d)\n", graphics, contrast);
6313 if(!graphics)
6314 return InvalidParameter;
6316 graphics->textcontrast = contrast;
6318 return Ok;
6321 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6322 TextRenderingHint hint)
6324 TRACE("(%p, %d)\n", graphics, hint);
6326 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6327 return InvalidParameter;
6329 if(graphics->busy)
6330 return ObjectBusy;
6332 if(graphics->texthint == hint)
6333 return Ok;
6335 if (is_metafile_graphics(graphics)) {
6336 GpStatus stat;
6338 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6339 EmfPlusRecordTypeSetTextRenderingHint, hint);
6340 if(stat != Ok)
6341 return stat;
6344 graphics->texthint = hint;
6346 return Ok;
6349 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6351 GpStatus stat;
6353 TRACE("(%p, %s)\n", graphics, debugstr_matrix(matrix));
6355 if(!graphics || !matrix)
6356 return InvalidParameter;
6358 if(graphics->busy)
6359 return ObjectBusy;
6361 if (is_metafile_graphics(graphics)) {
6362 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6364 if (stat != Ok)
6365 return stat;
6368 graphics->worldtrans = *matrix;
6370 return Ok;
6373 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6374 REAL dy, GpMatrixOrder order)
6376 GpStatus stat;
6378 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6380 if(!graphics)
6381 return InvalidParameter;
6383 if(graphics->busy)
6384 return ObjectBusy;
6386 if (is_metafile_graphics(graphics)) {
6387 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6389 if (stat != Ok)
6390 return stat;
6393 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6396 /*****************************************************************************
6397 * GdipSetClipHrgn [GDIPLUS.@]
6399 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6401 GpRegion *region;
6402 GpStatus status;
6403 GpMatrix transform;
6405 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6407 if(!graphics)
6408 return InvalidParameter;
6410 if(graphics->busy)
6411 return ObjectBusy;
6413 /* hrgn is in gdi32 device units */
6414 status = GdipCreateRegionHrgn(hrgn, &region);
6416 if (status == Ok)
6418 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6420 if (status == Ok)
6421 status = GdipTransformRegion(region, &transform);
6423 if (status == Ok)
6424 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6426 GdipDeleteRegion(region);
6428 return status;
6431 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6433 GpStatus status;
6434 GpPath *clip_path;
6436 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6438 if(!graphics)
6439 return InvalidParameter;
6441 if(graphics->busy)
6442 return ObjectBusy;
6444 if (is_metafile_graphics(graphics))
6446 status = METAFILE_SetClipPath((GpMetafile*)graphics->image, path, mode);
6447 if (status != Ok)
6448 return status;
6451 status = GdipClonePath(path, &clip_path);
6452 if (status == Ok)
6454 GpMatrix world_to_device;
6456 get_graphics_transform(graphics, CoordinateSpaceDevice,
6457 CoordinateSpaceWorld, &world_to_device);
6458 status = GdipTransformPath(clip_path, &world_to_device);
6459 if (status == Ok)
6460 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6462 GdipDeletePath(clip_path);
6464 return status;
6467 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6468 REAL width, REAL height,
6469 CombineMode mode)
6471 GpStatus status;
6472 GpRectF rect;
6473 GpRegion *region;
6475 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6477 if(!graphics)
6478 return InvalidParameter;
6480 if(graphics->busy)
6481 return ObjectBusy;
6483 if (is_metafile_graphics(graphics))
6485 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6486 if (status != Ok)
6487 return status;
6490 set_rect(&rect, x, y, width, height);
6491 status = GdipCreateRegionRect(&rect, &region);
6492 if (status == Ok)
6494 GpMatrix world_to_device;
6495 BOOL identity;
6497 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6498 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6499 if (status == Ok && !identity)
6500 status = GdipTransformRegion(region, &world_to_device);
6501 if (status == Ok)
6502 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6504 GdipDeleteRegion(region);
6506 return status;
6509 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6510 INT width, INT height,
6511 CombineMode mode)
6513 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6515 if(!graphics)
6516 return InvalidParameter;
6518 if(graphics->busy)
6519 return ObjectBusy;
6521 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6524 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6525 CombineMode mode)
6527 GpStatus status;
6528 GpRegion *clip;
6530 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6532 if(!graphics || !region)
6533 return InvalidParameter;
6535 if(graphics->busy)
6536 return ObjectBusy;
6538 if (is_metafile_graphics(graphics))
6540 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6541 if (status != Ok)
6542 return status;
6545 status = GdipCloneRegion(region, &clip);
6546 if (status == Ok)
6548 GpMatrix world_to_device;
6549 BOOL identity;
6551 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6552 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6553 if (status == Ok && !identity)
6554 status = GdipTransformRegion(clip, &world_to_device);
6555 if (status == Ok)
6556 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6558 GdipDeleteRegion(clip);
6560 return status;
6563 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6564 INT count)
6566 GpStatus status;
6567 GpPath* path;
6569 TRACE("(%p, %p, %d)\n", graphics, points, count);
6571 if(!graphics || !pen || count<=0)
6572 return InvalidParameter;
6574 if(graphics->busy)
6575 return ObjectBusy;
6577 status = GdipCreatePath(FillModeAlternate, &path);
6578 if (status != Ok) return status;
6580 status = GdipAddPathPolygon(path, points, count);
6581 if (status == Ok)
6582 status = GdipDrawPath(graphics, pen, path);
6584 GdipDeletePath(path);
6586 return status;
6589 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6590 INT count)
6592 GpStatus ret;
6593 GpPointF *ptf;
6594 INT i;
6596 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6598 if(count<=0) return InvalidParameter;
6599 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6600 if (!ptf) return OutOfMemory;
6602 for(i = 0;i < count; i++){
6603 ptf[i].X = (REAL)points[i].X;
6604 ptf[i].Y = (REAL)points[i].Y;
6607 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6608 heap_free(ptf);
6610 return ret;
6613 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6615 TRACE("(%p, %p)\n", graphics, dpi);
6617 if(!graphics || !dpi)
6618 return InvalidParameter;
6620 if(graphics->busy)
6621 return ObjectBusy;
6623 *dpi = graphics->xres;
6624 return Ok;
6627 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6629 TRACE("(%p, %p)\n", graphics, dpi);
6631 if(!graphics || !dpi)
6632 return InvalidParameter;
6634 if(graphics->busy)
6635 return ObjectBusy;
6637 *dpi = graphics->yres;
6638 return Ok;
6641 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6642 GpMatrixOrder order)
6644 GpMatrix m;
6645 GpStatus ret;
6647 TRACE("(%p, %s, %d)\n", graphics, debugstr_matrix(matrix), order);
6649 if(!graphics || !matrix)
6650 return InvalidParameter;
6652 if(graphics->busy)
6653 return ObjectBusy;
6655 if (is_metafile_graphics(graphics))
6657 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6659 if (ret != Ok)
6660 return ret;
6663 m = graphics->worldtrans;
6665 ret = GdipMultiplyMatrix(&m, matrix, order);
6666 if(ret == Ok)
6667 graphics->worldtrans = m;
6669 return ret;
6672 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6673 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6675 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6677 GpStatus stat=Ok;
6679 TRACE("(%p, %p)\n", graphics, hdc);
6681 if(!graphics || !hdc)
6682 return InvalidParameter;
6684 if(graphics->busy)
6685 return ObjectBusy;
6687 if (is_metafile_graphics(graphics))
6689 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6691 else if (!graphics->hdc ||
6692 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6694 /* Create a fake HDC and fill it with a constant color. */
6695 HDC temp_hdc;
6696 HBITMAP hbitmap;
6697 GpRectF bounds;
6698 BITMAPINFOHEADER bmih;
6699 int i;
6701 stat = get_graphics_bounds(graphics, &bounds);
6702 if (stat != Ok)
6703 return stat;
6705 graphics->temp_hbitmap_width = bounds.Width;
6706 graphics->temp_hbitmap_height = bounds.Height;
6708 bmih.biSize = sizeof(bmih);
6709 bmih.biWidth = graphics->temp_hbitmap_width;
6710 bmih.biHeight = -graphics->temp_hbitmap_height;
6711 bmih.biPlanes = 1;
6712 bmih.biBitCount = 32;
6713 bmih.biCompression = BI_RGB;
6714 bmih.biSizeImage = 0;
6715 bmih.biXPelsPerMeter = 0;
6716 bmih.biYPelsPerMeter = 0;
6717 bmih.biClrUsed = 0;
6718 bmih.biClrImportant = 0;
6720 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6721 (void**)&graphics->temp_bits, NULL, 0);
6722 if (!hbitmap)
6723 return GenericError;
6725 if (!graphics->temp_hdc)
6727 temp_hdc = CreateCompatibleDC(0);
6729 else
6731 temp_hdc = graphics->temp_hdc;
6734 if (!temp_hdc)
6736 DeleteObject(hbitmap);
6737 return GenericError;
6740 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6741 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6743 SelectObject(temp_hdc, hbitmap);
6745 graphics->temp_hbitmap = hbitmap;
6746 *hdc = graphics->temp_hdc = temp_hdc;
6748 else
6750 *hdc = graphics->hdc;
6753 if (stat == Ok)
6754 graphics->busy = TRUE;
6756 return stat;
6759 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6761 GpStatus stat=Ok;
6763 TRACE("(%p, %p)\n", graphics, hdc);
6765 if(!graphics || !hdc || !graphics->busy)
6766 return InvalidParameter;
6768 if (is_metafile_graphics(graphics))
6770 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6772 else if (graphics->temp_hdc == hdc)
6774 DWORD* pos;
6775 int i;
6777 /* Find the pixels that have changed, and mark them as opaque. */
6778 pos = (DWORD*)graphics->temp_bits;
6779 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6781 if (*pos != DC_BACKGROUND_KEY)
6783 *pos |= 0xff000000;
6785 pos++;
6788 /* Write the changed pixels to the real target. */
6789 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6790 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6791 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6793 /* Clean up. */
6794 DeleteObject(graphics->temp_hbitmap);
6795 graphics->temp_hbitmap = NULL;
6797 else if (hdc != graphics->hdc)
6799 stat = InvalidParameter;
6802 if (stat == Ok)
6803 graphics->busy = FALSE;
6805 return stat;
6808 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6810 GpRegion *clip;
6811 GpStatus status;
6812 GpMatrix device_to_world;
6814 TRACE("(%p, %p)\n", graphics, region);
6816 if(!graphics || !region)
6817 return InvalidParameter;
6819 if(graphics->busy)
6820 return ObjectBusy;
6822 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6823 return status;
6825 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6826 status = GdipTransformRegion(clip, &device_to_world);
6827 if (status != Ok)
6829 GdipDeleteRegion(clip);
6830 return status;
6833 /* free everything except root node and header */
6834 delete_element(&region->node);
6835 memcpy(region, clip, sizeof(GpRegion));
6836 heap_free(clip);
6838 return Ok;
6841 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6843 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6845 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6846 ModifyWorldTransform(graphics->hdc, NULL, MWT_IDENTITY);
6847 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6848 SetMapMode(graphics->hdc, MM_TEXT);
6849 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6850 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6852 graphics->gdi_transform_acquire_count++;
6853 return Ok;
6856 GpStatus gdi_transform_release(GpGraphics *graphics)
6858 if (graphics->gdi_transform_acquire_count <= 0)
6860 ERR("called without matching gdi_transform_acquire\n");
6861 return GenericError;
6863 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6865 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6867 graphics->gdi_transform_acquire_count--;
6868 return Ok;
6871 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6872 GpCoordinateSpace src_space, GpMatrix *matrix)
6874 GpStatus stat = Ok;
6875 REAL scale_x, scale_y;
6877 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6879 if (dst_space != src_space)
6881 if(graphics->unit != UnitDisplay)
6883 scale_x = units_to_pixels(graphics->scale, graphics->unit, graphics->xres, graphics->printer_display);
6884 scale_y = units_to_pixels(graphics->scale, graphics->unit, graphics->yres, graphics->printer_display);
6886 else
6888 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres, graphics->printer_display);
6889 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres, graphics->printer_display);
6892 if (dst_space < src_space)
6894 /* transform towards world space */
6895 switch ((int)src_space)
6897 case WineCoordinateSpaceGdiDevice:
6899 GpMatrix gdixform = graphics->gdi_transform;
6900 stat = GdipInvertMatrix(&gdixform);
6901 if (stat != Ok)
6902 break;
6903 memcpy(matrix->matrix, gdixform.matrix, sizeof(matrix->matrix));
6904 if (dst_space == CoordinateSpaceDevice)
6905 break;
6906 /* else fall-through */
6908 case CoordinateSpaceDevice:
6909 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6910 if (dst_space == CoordinateSpacePage)
6911 break;
6912 /* else fall-through */
6913 case CoordinateSpacePage:
6915 GpMatrix inverted_transform = graphics->worldtrans;
6916 stat = GdipInvertMatrix(&inverted_transform);
6917 if (stat == Ok)
6918 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6919 break;
6923 else
6925 /* transform towards device space */
6926 switch ((int)src_space)
6928 case CoordinateSpaceWorld:
6929 memcpy(matrix->matrix, &graphics->worldtrans, sizeof(matrix->matrix));
6930 if (dst_space == CoordinateSpacePage)
6931 break;
6932 /* else fall-through */
6933 case CoordinateSpacePage:
6934 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6935 if (dst_space == CoordinateSpaceDevice)
6936 break;
6937 /* else fall-through */
6938 case CoordinateSpaceDevice:
6940 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6941 break;
6946 return stat;
6949 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6950 GpCoordinateSpace src_space, GpPointF *points, INT count)
6952 GpMatrix matrix;
6953 GpStatus stat;
6955 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6956 if (stat != Ok) return stat;
6958 return GdipTransformMatrixPoints(&matrix, points, count);
6961 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6962 GpCoordinateSpace src_space, GpPointF *points, INT count)
6964 if(!graphics || !points || count <= 0 ||
6965 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6966 src_space < 0 || src_space > CoordinateSpaceDevice)
6967 return InvalidParameter;
6969 if(graphics->busy)
6970 return ObjectBusy;
6972 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6974 if (src_space == dst_space) return Ok;
6976 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6979 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6980 GpCoordinateSpace src_space, GpPoint *points, INT count)
6982 GpPointF *pointsF;
6983 GpStatus ret;
6984 INT i;
6986 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6988 if(count <= 0)
6989 return InvalidParameter;
6991 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6992 if(!pointsF)
6993 return OutOfMemory;
6995 for(i = 0; i < count; i++){
6996 pointsF[i].X = (REAL)points[i].X;
6997 pointsF[i].Y = (REAL)points[i].Y;
7000 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
7002 if(ret == Ok)
7003 for(i = 0; i < count; i++){
7004 points[i].X = gdip_round(pointsF[i].X);
7005 points[i].Y = gdip_round(pointsF[i].Y);
7007 heap_free(pointsF);
7009 return ret;
7012 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
7014 static int calls;
7016 TRACE("\n");
7018 if (!calls++)
7019 FIXME("stub\n");
7021 return NULL;
7024 /*****************************************************************************
7025 * GdipTranslateClip [GDIPLUS.@]
7027 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
7029 GpStatus stat;
7031 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
7033 if(!graphics)
7034 return InvalidParameter;
7036 if(graphics->busy)
7037 return ObjectBusy;
7039 if (is_metafile_graphics(graphics))
7041 stat = METAFILE_OffsetClip((GpMetafile *)graphics->image, dx, dy);
7042 if (stat != Ok)
7043 return stat;
7046 return GdipTranslateRegion(graphics->clip, dx, dy);
7049 /*****************************************************************************
7050 * GdipTranslateClipI [GDIPLUS.@]
7052 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
7054 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
7056 return GdipTranslateClip(graphics, dx, dy);
7059 /*****************************************************************************
7060 * GdipMeasureDriverString [GDIPLUS.@]
7062 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7063 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
7064 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
7066 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7067 HFONT hfont;
7068 HDC hdc;
7069 REAL min_x, min_y, max_x, max_y, x, y;
7070 int i;
7071 TEXTMETRICW textmetric;
7072 const WORD *glyph_indices;
7073 WORD *dynamic_glyph_indices=NULL;
7074 REAL rel_width, rel_height, ascent, descent;
7075 GpPointF pt[3];
7077 TRACE("(%p %p %d %p %p %d %s %p)\n", graphics, text, length, font, positions, flags, debugstr_matrix(matrix), boundingBox);
7079 if (!graphics || !text || !font || !positions || !boundingBox)
7080 return InvalidParameter;
7082 if (length == -1)
7083 length = lstrlenW(text);
7085 if (length == 0)
7086 set_rect(boundingBox, 0.0f, 0.0f, 0.0f, 0.0f);
7088 if (flags & unsupported_flags)
7089 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7091 get_font_hfont(graphics, font, NULL, &hfont, NULL, matrix);
7093 hdc = CreateCompatibleDC(0);
7094 SelectObject(hdc, hfont);
7096 GetTextMetricsW(hdc, &textmetric);
7098 pt[0].X = 0.0;
7099 pt[0].Y = 0.0;
7100 pt[1].X = 1.0;
7101 pt[1].Y = 0.0;
7102 pt[2].X = 0.0;
7103 pt[2].Y = 1.0;
7104 if (matrix)
7106 GpMatrix xform = *matrix;
7107 GdipTransformMatrixPoints(&xform, pt, 3);
7109 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
7110 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
7111 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
7112 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
7113 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
7115 if (flags & DriverStringOptionsCmapLookup)
7117 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
7118 if (!glyph_indices)
7120 DeleteDC(hdc);
7121 DeleteObject(hfont);
7122 return OutOfMemory;
7125 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
7127 else
7128 glyph_indices = text;
7130 min_x = max_x = x = positions[0].X;
7131 min_y = max_y = y = positions[0].Y;
7133 ascent = textmetric.tmAscent / rel_height;
7134 descent = textmetric.tmDescent / rel_height;
7136 for (i=0; i<length; i++)
7138 int char_width;
7139 ABC abc;
7141 if (!(flags & DriverStringOptionsRealizedAdvance))
7143 x = positions[i].X;
7144 y = positions[i].Y;
7147 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
7148 char_width = abc.abcA + abc.abcB + abc.abcC;
7150 if (min_y > y - ascent) min_y = y - ascent;
7151 if (max_y < y + descent) max_y = y + descent;
7152 if (min_x > x) min_x = x;
7154 x += char_width / rel_width;
7156 if (max_x < x) max_x = x;
7159 heap_free(dynamic_glyph_indices);
7160 DeleteDC(hdc);
7161 DeleteObject(hfont);
7163 boundingBox->X = min_x;
7164 boundingBox->Y = min_y;
7165 boundingBox->Width = max_x - min_x;
7166 boundingBox->Height = max_y - min_y;
7168 return Ok;
7171 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7172 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7173 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7174 INT flags, GDIPCONST GpMatrix *matrix)
7176 INT save_state;
7177 GpPointF pt, *real_positions=NULL;
7178 INT *eto_positions=NULL;
7179 HFONT hfont;
7180 LOGFONTW lfw;
7181 UINT eto_flags=0;
7182 GpStatus status;
7183 HRGN hrgn;
7185 if (!(flags & DriverStringOptionsCmapLookup))
7186 eto_flags |= ETO_GLYPH_INDEX;
7188 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7190 real_positions = heap_alloc(sizeof(*real_positions) * length);
7191 eto_positions = heap_alloc(sizeof(*eto_positions) * 2 * (length - 1));
7192 if (!real_positions || !eto_positions)
7194 heap_free(real_positions);
7195 heap_free(eto_positions);
7196 return OutOfMemory;
7200 save_state = SaveDC(graphics->hdc);
7201 SetBkMode(graphics->hdc, TRANSPARENT);
7202 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7204 status = get_clip_hrgn(graphics, &hrgn);
7206 if (status == Ok)
7208 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7209 DeleteObject(hrgn);
7212 pt = positions[0];
7213 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7215 get_font_hfont(graphics, font, format, &hfont, &lfw, matrix);
7217 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7219 GpMatrix rotation;
7220 INT i;
7222 eto_flags |= ETO_PDY;
7224 memcpy(real_positions, positions, sizeof(PointF) * length);
7226 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7228 GdipSetMatrixElements(&rotation, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
7229 GdipRotateMatrix(&rotation, lfw.lfEscapement / 10.0, MatrixOrderAppend);
7230 GdipTransformMatrixPoints(&rotation, real_positions, length);
7232 for (i = 0; i < (length - 1); i++)
7234 eto_positions[i*2] = gdip_round(real_positions[i+1].X) - gdip_round(real_positions[i].X);
7235 eto_positions[i*2+1] = gdip_round(real_positions[i].Y) - gdip_round(real_positions[i+1].Y);
7239 SelectObject(graphics->hdc, hfont);
7241 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7243 gdi_transform_acquire(graphics);
7245 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, eto_positions);
7247 gdi_transform_release(graphics);
7249 RestoreDC(graphics->hdc, save_state);
7251 DeleteObject(hfont);
7253 heap_free(real_positions);
7254 heap_free(eto_positions);
7256 return Ok;
7259 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7260 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7261 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7262 INT flags, GDIPCONST GpMatrix *matrix)
7264 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7265 GpStatus stat;
7266 PointF *real_positions, real_position;
7267 POINT *pti;
7268 HFONT hfont;
7269 HDC hdc;
7270 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7271 DWORD max_glyphsize=0;
7272 GLYPHMETRICS glyphmetrics;
7273 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7274 BYTE *glyph_mask;
7275 BYTE *text_mask;
7276 int text_mask_stride;
7277 BYTE *pixel_data;
7278 int pixel_data_stride;
7279 GpRect pixel_area;
7280 UINT ggo_flags = GGO_GRAY8_BITMAP;
7282 if (length <= 0)
7283 return Ok;
7285 if (!(flags & DriverStringOptionsCmapLookup))
7286 ggo_flags |= GGO_GLYPH_INDEX;
7288 if (flags & unsupported_flags)
7289 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7291 pti = heap_alloc_zero(sizeof(POINT) * length);
7292 if (!pti)
7293 return OutOfMemory;
7295 if (flags & DriverStringOptionsRealizedAdvance)
7297 real_position = positions[0];
7299 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7300 round_points(pti, &real_position, 1);
7302 else
7304 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7305 if (!real_positions)
7307 heap_free(pti);
7308 return OutOfMemory;
7311 memcpy(real_positions, positions, sizeof(PointF) * length);
7313 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7314 round_points(pti, real_positions, length);
7316 heap_free(real_positions);
7319 get_font_hfont(graphics, font, format, &hfont, NULL, matrix);
7321 hdc = CreateCompatibleDC(0);
7322 SelectObject(hdc, hfont);
7324 /* Get the boundaries of the text to be drawn */
7325 for (i=0; i<length; i++)
7327 DWORD glyphsize;
7328 int left, top, right, bottom;
7330 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7331 &glyphmetrics, 0, NULL, &identity);
7333 if (glyphsize == GDI_ERROR)
7335 ERR("GetGlyphOutlineW failed\n");
7336 heap_free(pti);
7337 DeleteDC(hdc);
7338 DeleteObject(hfont);
7339 return GenericError;
7342 if (glyphsize > max_glyphsize)
7343 max_glyphsize = glyphsize;
7345 if (glyphsize != 0)
7347 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7348 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7349 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7350 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7352 if (left < min_x) min_x = left;
7353 if (top < min_y) min_y = top;
7354 if (right > max_x) max_x = right;
7355 if (bottom > max_y) max_y = bottom;
7358 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7360 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7361 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7365 if (max_glyphsize == 0)
7367 /* Nothing to draw. */
7368 heap_free(pti);
7369 DeleteDC(hdc);
7370 DeleteObject(hfont);
7371 return Ok;
7374 glyph_mask = heap_alloc_zero(max_glyphsize);
7375 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7376 text_mask_stride = max_x - min_x;
7378 if (!(glyph_mask && text_mask))
7380 heap_free(glyph_mask);
7381 heap_free(text_mask);
7382 heap_free(pti);
7383 DeleteDC(hdc);
7384 DeleteObject(hfont);
7385 return OutOfMemory;
7388 /* Generate a mask for the text */
7389 for (i=0; i<length; i++)
7391 DWORD ret;
7392 int left, top, stride;
7394 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7395 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7397 if (ret == GDI_ERROR || ret == 0)
7398 continue; /* empty glyph */
7400 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7401 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7402 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7404 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7406 BYTE *glyph_val = glyph_mask + y * stride;
7407 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7408 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7410 *text_val = min(64, *text_val + *glyph_val);
7411 glyph_val++;
7412 text_val++;
7417 heap_free(pti);
7418 DeleteDC(hdc);
7419 DeleteObject(hfont);
7420 heap_free(glyph_mask);
7422 /* get the brush data */
7423 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7424 if (!pixel_data)
7426 heap_free(text_mask);
7427 return OutOfMemory;
7430 pixel_area.X = min_x;
7431 pixel_area.Y = min_y;
7432 pixel_area.Width = max_x - min_x;
7433 pixel_area.Height = max_y - min_y;
7434 pixel_data_stride = pixel_area.Width * 4;
7436 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7437 if (stat != Ok)
7439 heap_free(text_mask);
7440 heap_free(pixel_data);
7441 return stat;
7444 /* multiply the brush data by the mask */
7445 for (y=0; y<pixel_area.Height; y++)
7447 BYTE *text_val = text_mask + text_mask_stride * y;
7448 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7449 for (x=0; x<pixel_area.Width; x++)
7451 *pixel_val = (*pixel_val) * (*text_val) / 64;
7452 text_val++;
7453 pixel_val+=4;
7457 heap_free(text_mask);
7459 gdi_transform_acquire(graphics);
7461 /* draw the result */
7462 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7463 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7465 gdi_transform_release(graphics);
7467 heap_free(pixel_data);
7469 return stat;
7472 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7473 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7474 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7475 INT flags, GDIPCONST GpMatrix *matrix)
7477 GpStatus stat = NotImplemented;
7479 if (length == -1)
7480 length = lstrlenW(text);
7482 if (is_metafile_graphics(graphics))
7483 return METAFILE_DrawDriverString((GpMetafile*)graphics->image, text, length, font,
7484 format, brush, positions, flags, matrix);
7486 if (graphics->hdc && !graphics->alpha_hdc &&
7487 brush->bt == BrushTypeSolidColor &&
7488 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7489 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7490 brush, positions, flags, matrix);
7491 if (stat == NotImplemented)
7492 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7493 brush, positions, flags, matrix);
7494 return stat;
7497 /*****************************************************************************
7498 * GdipDrawDriverString [GDIPLUS.@]
7500 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7501 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7502 GDIPCONST PointF *positions, INT flags,
7503 GDIPCONST GpMatrix *matrix )
7505 TRACE("(%p %s %p %p %p %d %s)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, debugstr_matrix(matrix));
7507 if (!graphics || !text || !font || !brush || !positions)
7508 return InvalidParameter;
7510 return draw_driver_string(graphics, text, length, font, NULL,
7511 brush, positions, flags, matrix);
7514 /*****************************************************************************
7515 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7517 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7519 GpStatus stat;
7520 GpRegion* rgn;
7522 TRACE("(%p, %p)\n", graphics, res);
7524 if((stat = GdipCreateRegion(&rgn)) != Ok)
7525 return stat;
7527 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7528 goto cleanup;
7530 stat = GdipIsEmptyRegion(rgn, graphics, res);
7532 cleanup:
7533 GdipDeleteRegion(rgn);
7534 return stat;
7537 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7539 static int calls;
7541 TRACE("(%p) stub\n", graphics);
7543 if(!(calls++))
7544 FIXME("not implemented\n");
7546 return NotImplemented;
7549 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7551 TRACE("(%p, %p)\n", graphics, pabort);
7553 if (!graphics)
7554 return InvalidParameter;
7556 if (pabort)
7557 FIXME("Abort callback is not supported.\n");
7559 return Ok;