mmdevapi: Query MemoryWineUnixFuncs virtual memory and store the resulting handle.
[wine.git] / dlls / gdiplus / graphics.c
blobfca66c8654b92c6c93fc19b29748005ea5fb13b5
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 /* Get an estimate for the amount the pen width is affected by the world
272 * transform. (This is similar to what some of the wine drivers do.) */
273 pt[0].X = 0.0;
274 pt[0].Y = 0.0;
275 pt[1].X = 1.0;
276 pt[1].Y = 1.0;
277 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
278 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
279 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
281 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit,
282 graphics->xres, graphics->printer_display);
283 width *= graphics->scale;
285 pt[0].X = 0.0;
286 pt[0].Y = 0.0;
287 pt[1].X = 1.0;
288 pt[1].Y = 1.0;
289 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, pt, 2);
290 width *= sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
291 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
294 if(pen->dash == DashStyleCustom){
295 numdashes = min(pen->numdashes, MAX_DASHLEN);
297 TRACE("dashes are: ");
298 for(i = 0; i < numdashes; i++){
299 dash_array[i] = gdip_round(width * pen->dashes[i]);
300 TRACE("%ld, ", dash_array[i]);
302 TRACE("\n and the pen style is %x\n", pen->style);
304 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
305 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
306 numdashes, dash_array);
307 free_gdi_logbrush(&lb);
309 else
311 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
312 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
313 free_gdi_logbrush(&lb);
316 SelectObject(graphics->hdc, gdipen);
318 return save_state;
321 static void restore_dc(GpGraphics *graphics, INT state)
323 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
324 RestoreDC(graphics->hdc, state);
327 static void round_points(POINT *pti, GpPointF *ptf, INT count)
329 int i;
331 for(i = 0; i < count; i++){
332 if(isnan(ptf[i].X))
333 pti[i].x = 0;
334 else
335 pti[i].x = gdip_round(ptf[i].X);
337 if(isnan(ptf[i].Y))
338 pti[i].y = 0;
339 else
340 pti[i].y = gdip_round(ptf[i].Y);
344 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
345 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
347 CompositingMode comp_mode;
348 INT technology = GetDeviceCaps(graphics->hdc, TECHNOLOGY);
349 INT shadeblendcaps = GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS);
351 GdipGetCompositingMode(graphics, &comp_mode);
353 if ((technology == DT_RASPRINTER && shadeblendcaps == SB_NONE)
354 || comp_mode == CompositingModeSourceCopy)
356 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
358 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
359 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
361 else
363 BLENDFUNCTION bf;
365 bf.BlendOp = AC_SRC_OVER;
366 bf.BlendFlags = 0;
367 bf.SourceConstantAlpha = 255;
368 bf.AlphaFormat = AC_SRC_ALPHA;
370 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
371 hdc, src_x, src_y, src_width, src_height, bf);
375 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
377 GpRegion *rgn;
378 GpMatrix transform;
379 GpStatus stat;
380 BOOL identity;
382 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, &transform);
384 if (stat == Ok)
385 stat = GdipIsMatrixIdentity(&transform, &identity);
387 if (stat == Ok)
388 stat = GdipCloneRegion(graphics->clip, &rgn);
390 if (stat == Ok)
392 if (!identity)
393 stat = GdipTransformRegion(rgn, &transform);
395 if (stat == Ok)
396 stat = GdipGetRegionHRgn(rgn, NULL, hrgn);
398 GdipDeleteRegion(rgn);
401 if (stat == Ok && graphics->gdi_clip)
403 if (*hrgn)
404 CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
405 else
407 *hrgn = CreateRectRgn(0,0,0,0);
408 CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
412 return stat;
415 /* Draw ARGB data to the given graphics object */
416 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
417 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
419 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
420 INT x, y;
421 CompositingMode comp_mode = graphics->compmode;
423 for (y=0; y<src_height; y++)
425 for (x=0; x<src_width; x++)
427 ARGB dst_color, src_color;
428 src_color = ((ARGB*)(src + src_stride * y))[x];
430 if (comp_mode == CompositingModeSourceCopy)
432 if (!(src_color & 0xff000000))
433 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, 0);
434 else
435 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, src_color);
437 else
439 if (!(src_color & 0xff000000))
440 continue;
442 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
443 if (fmt & PixelFormatPAlpha)
444 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
445 else
446 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
451 return Ok;
454 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
455 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
457 HDC hdc;
458 HBITMAP hbitmap;
459 BITMAPINFOHEADER bih;
460 BYTE *temp_bits;
462 hdc = CreateCompatibleDC(0);
464 bih.biSize = sizeof(BITMAPINFOHEADER);
465 bih.biWidth = src_width;
466 bih.biHeight = -src_height;
467 bih.biPlanes = 1;
468 bih.biBitCount = 32;
469 bih.biCompression = BI_RGB;
470 bih.biSizeImage = 0;
471 bih.biXPelsPerMeter = 0;
472 bih.biYPelsPerMeter = 0;
473 bih.biClrUsed = 0;
474 bih.biClrImportant = 0;
476 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
477 (void**)&temp_bits, NULL, 0);
479 if(!hbitmap || !temp_bits)
480 goto done;
482 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
483 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
484 fmt & PixelFormatPAlpha)
485 memcpy(temp_bits, src, src_width * src_height * 4);
486 else
487 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
488 4 * src_width, src, src_stride);
490 SelectObject(hdc, hbitmap);
491 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
492 hdc, 0, 0, src_width, src_height);
494 DeleteObject(hbitmap);
496 done:
497 DeleteDC(hdc);
499 return Ok;
502 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
503 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
505 GpStatus stat=Ok;
507 if (graphics->image && graphics->image->type == ImageTypeBitmap)
509 DWORD i;
510 int size;
511 RGNDATA *rgndata;
512 RECT *rects;
513 HRGN hrgn, visible_rgn;
515 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
516 if (!hrgn)
517 return OutOfMemory;
519 stat = get_clip_hrgn(graphics, &visible_rgn);
520 if (stat != Ok)
522 DeleteObject(hrgn);
523 return stat;
526 if (visible_rgn)
528 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
529 DeleteObject(visible_rgn);
532 if (hregion)
533 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
535 size = GetRegionData(hrgn, 0, NULL);
537 rgndata = heap_alloc_zero(size);
538 if (!rgndata)
540 DeleteObject(hrgn);
541 return OutOfMemory;
544 GetRegionData(hrgn, size, rgndata);
546 rects = (RECT*)rgndata->Buffer;
548 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
550 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
551 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
552 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
553 src_stride, fmt);
556 heap_free(rgndata);
558 DeleteObject(hrgn);
560 return stat;
562 else if (is_metafile_graphics(graphics))
564 ERR("This should not be used for metafiles; fix caller\n");
565 return NotImplemented;
567 else
569 HRGN hrgn;
570 int save;
572 stat = get_clip_hrgn(graphics, &hrgn);
574 if (stat != Ok)
575 return stat;
577 save = SaveDC(graphics->hdc);
579 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
581 if (hregion)
582 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
584 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
585 src_height, src_stride, fmt);
587 RestoreDC(graphics->hdc, save);
589 DeleteObject(hrgn);
591 return stat;
595 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
596 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
598 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
601 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
603 INT start_a, end_a, final_a;
604 INT pos;
606 pos = gdip_round(position * 0xff);
608 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
609 end_a = ((end >> 24) & 0xff) * pos;
611 final_a = start_a + end_a;
613 if (final_a < 0xff) return 0;
615 return (final_a / 0xff) << 24 |
616 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
617 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
618 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
621 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
623 REAL blendfac;
625 /* clamp to between 0.0 and 1.0, using the wrap mode */
626 position = (position - brush->rect.X) / brush->rect.Width;
627 if (brush->wrap == WrapModeTile)
629 position = fmodf(position, 1.0f);
630 if (position < 0.0f) position += 1.0f;
632 else /* WrapModeFlip* */
634 position = fmodf(position, 2.0f);
635 if (position < 0.0f) position += 2.0f;
636 if (position > 1.0f) position = 2.0f - position;
639 if (brush->blendcount == 1)
640 blendfac = position;
641 else
643 int i=1;
644 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
645 REAL range;
647 /* locate the blend positions surrounding this position */
648 while (position > brush->blendpos[i])
649 i++;
651 /* interpolate between the blend positions */
652 left_blendpos = brush->blendpos[i-1];
653 left_blendfac = brush->blendfac[i-1];
654 right_blendpos = brush->blendpos[i];
655 right_blendfac = brush->blendfac[i];
656 range = right_blendpos - left_blendpos;
657 blendfac = (left_blendfac * (right_blendpos - position) +
658 right_blendfac * (position - left_blendpos)) / range;
661 if (brush->pblendcount == 0)
662 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
663 else
665 int i=1;
666 ARGB left_blendcolor, right_blendcolor;
667 REAL left_blendpos, right_blendpos;
669 /* locate the blend colors surrounding this position */
670 while (blendfac > brush->pblendpos[i])
671 i++;
673 /* interpolate between the blend colors */
674 left_blendpos = brush->pblendpos[i-1];
675 left_blendcolor = brush->pblendcolor[i-1];
676 right_blendpos = brush->pblendpos[i];
677 right_blendcolor = brush->pblendcolor[i];
678 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
679 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
683 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
685 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
686 BOOL identity = TRUE;
687 int i, j;
689 for (i=0; i<4; i++)
690 for (j=0; j<5; j++)
692 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
693 identity = FALSE;
694 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
697 return identity;
700 static ARGB transform_color(ARGB color, int matrix[5][5])
702 int val[5], res[4];
703 int i, j;
704 unsigned char a, r, g, b;
706 val[0] = ((color >> 16) & 0xff); /* red */
707 val[1] = ((color >> 8) & 0xff); /* green */
708 val[2] = (color & 0xff); /* blue */
709 val[3] = ((color >> 24) & 0xff); /* alpha */
710 val[4] = 255; /* translation */
712 for (i=0; i<4; i++)
714 res[i] = 0;
716 for (j=0; j<5; j++)
717 res[i] += matrix[j][i] * val[j];
720 a = min(max(res[3] / 256, 0), 255);
721 r = min(max(res[0] / 256, 0), 255);
722 g = min(max(res[1] / 256, 0), 255);
723 b = min(max(res[2] / 256, 0), 255);
725 return (a << 24) | (r << 16) | (g << 8) | b;
728 static BOOL color_is_gray(ARGB color)
730 unsigned char r, g, b;
732 r = (color >> 16) & 0xff;
733 g = (color >> 8) & 0xff;
734 b = color & 0xff;
736 return (r == g) && (g == b);
739 /* returns preferred pixel format for the applied attributes */
740 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
741 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
743 UINT x, y;
744 INT i;
746 if ((attributes->noop[type] == IMAGEATTR_NOOP_UNDEFINED &&
747 attributes->noop[ColorAdjustTypeDefault] == IMAGEATTR_NOOP_SET) ||
748 (attributes->noop[type] == IMAGEATTR_NOOP_SET))
749 return fmt;
751 if (attributes->colorkeys[type].enabled ||
752 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
754 const struct color_key *key;
755 BYTE min_blue, min_green, min_red;
756 BYTE max_blue, max_green, max_red;
758 if (!data || fmt != PixelFormat32bppARGB)
759 return PixelFormat32bppARGB;
761 if (attributes->colorkeys[type].enabled)
762 key = &attributes->colorkeys[type];
763 else
764 key = &attributes->colorkeys[ColorAdjustTypeDefault];
766 min_blue = key->low&0xff;
767 min_green = (key->low>>8)&0xff;
768 min_red = (key->low>>16)&0xff;
770 max_blue = key->high&0xff;
771 max_green = (key->high>>8)&0xff;
772 max_red = (key->high>>16)&0xff;
774 for (x=0; x<width; x++)
775 for (y=0; y<height; y++)
777 ARGB *src_color;
778 BYTE blue, green, red;
779 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
780 blue = *src_color&0xff;
781 green = (*src_color>>8)&0xff;
782 red = (*src_color>>16)&0xff;
783 if (blue >= min_blue && green >= min_green && red >= min_red &&
784 blue <= max_blue && green <= max_green && red <= max_red)
785 *src_color = 0x00000000;
789 if (attributes->colorremaptables[type].enabled ||
790 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
792 const struct color_remap_table *table;
794 if (!data || fmt != PixelFormat32bppARGB)
795 return PixelFormat32bppARGB;
797 if (attributes->colorremaptables[type].enabled)
798 table = &attributes->colorremaptables[type];
799 else
800 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
802 for (x=0; x<width; x++)
803 for (y=0; y<height; y++)
805 ARGB *src_color;
806 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
807 for (i=0; i<table->mapsize; i++)
809 if (*src_color == table->colormap[i].oldColor.Argb)
811 *src_color = table->colormap[i].newColor.Argb;
812 break;
818 if (attributes->colormatrices[type].enabled ||
819 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
821 const struct color_matrix *colormatrices;
822 int color_matrix[5][5];
823 int gray_matrix[5][5];
824 BOOL identity;
826 if (!data || fmt != PixelFormat32bppARGB)
827 return PixelFormat32bppARGB;
829 if (attributes->colormatrices[type].enabled)
830 colormatrices = &attributes->colormatrices[type];
831 else
832 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
834 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
836 if (colormatrices->flags == ColorMatrixFlagsAltGray)
837 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
839 if (!identity)
841 for (x=0; x<width; x++)
843 for (y=0; y<height; y++)
845 ARGB *src_color;
846 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
848 if (colormatrices->flags == ColorMatrixFlagsDefault ||
849 !color_is_gray(*src_color))
851 *src_color = transform_color(*src_color, color_matrix);
853 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
855 *src_color = transform_color(*src_color, gray_matrix);
862 if (attributes->gamma_enabled[type] ||
863 attributes->gamma_enabled[ColorAdjustTypeDefault])
865 REAL gamma;
867 if (!data || fmt != PixelFormat32bppARGB)
868 return PixelFormat32bppARGB;
870 if (attributes->gamma_enabled[type])
871 gamma = attributes->gamma[type];
872 else
873 gamma = attributes->gamma[ColorAdjustTypeDefault];
875 for (x=0; x<width; x++)
876 for (y=0; y<height; y++)
878 ARGB *src_color;
879 BYTE blue, green, red;
880 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
882 blue = *src_color&0xff;
883 green = (*src_color>>8)&0xff;
884 red = (*src_color>>16)&0xff;
886 /* FIXME: We should probably use a table for this. */
887 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
888 green = floorf(powf(green / 255.0, gamma) * 255.0);
889 red = floorf(powf(red / 255.0, gamma) * 255.0);
891 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
895 return fmt;
898 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
899 * bitmap that contains all the pixels we may need to draw it. */
900 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
901 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
902 GpRect *rect)
904 INT left, top, right, bottom;
906 switch (interpolation)
908 case InterpolationModeHighQualityBilinear:
909 case InterpolationModeHighQualityBicubic:
910 /* FIXME: Include a greater range for the prefilter? */
911 case InterpolationModeBicubic:
912 case InterpolationModeBilinear:
913 left = (INT)(floorf(srcx));
914 top = (INT)(floorf(srcy));
915 right = (INT)(ceilf(srcx+srcwidth));
916 bottom = (INT)(ceilf(srcy+srcheight));
917 break;
918 case InterpolationModeNearestNeighbor:
919 default:
920 left = gdip_round(srcx);
921 top = gdip_round(srcy);
922 right = gdip_round(srcx+srcwidth);
923 bottom = gdip_round(srcy+srcheight);
924 break;
927 if (wrap == WrapModeClamp)
929 if (left < 0)
930 left = 0;
931 if (top < 0)
932 top = 0;
933 if (right >= bitmap->width)
934 right = bitmap->width-1;
935 if (bottom >= bitmap->height)
936 bottom = bitmap->height-1;
937 if (bottom < top || right < left)
938 /* entirely outside image, just sample a pixel so we don't have to
939 * special-case this later */
940 left = top = right = bottom = 0;
942 else
944 /* In some cases we can make the rectangle smaller here, but the logic
945 * is hard to get right, and tiling suggests we're likely to use the
946 * entire source image. */
947 if (left < 0 || right >= bitmap->width)
949 left = 0;
950 right = bitmap->width-1;
953 if (top < 0 || bottom >= bitmap->height)
955 top = 0;
956 bottom = bitmap->height-1;
960 rect->X = left;
961 rect->Y = top;
962 rect->Width = right - left + 1;
963 rect->Height = bottom - top + 1;
966 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
967 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
969 if (attributes->wrap == WrapModeClamp)
971 if (x < 0 || y < 0 || x >= width || y >= height)
972 return attributes->outside_color;
974 else
976 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
977 if (x < 0)
978 x = width*2 + x % (INT)(width * 2);
979 if (y < 0)
980 y = height*2 + y % (INT)(height * 2);
982 if (attributes->wrap & WrapModeTileFlipX)
984 if ((x / width) % 2 == 0)
985 x = x % width;
986 else
987 x = width - 1 - x % width;
989 else
990 x = x % width;
992 if (attributes->wrap & WrapModeTileFlipY)
994 if ((y / height) % 2 == 0)
995 y = y % height;
996 else
997 y = height - 1 - y % height;
999 else
1000 y = y % height;
1003 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
1005 ERR("out of range pixel requested\n");
1006 return 0xffcd0084;
1009 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
1012 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
1013 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
1014 InterpolationMode interpolation, PixelOffsetMode offset_mode)
1016 static int fixme;
1018 switch (interpolation)
1020 default:
1021 if (!fixme++)
1022 FIXME("Unimplemented interpolation %i\n", interpolation);
1023 /* fall-through */
1024 case InterpolationModeBilinear:
1026 REAL leftxf, topyf;
1027 INT leftx, rightx, topy, bottomy;
1028 ARGB topleft, topright, bottomleft, bottomright;
1029 ARGB top, bottom;
1030 float x_offset;
1032 leftxf = floorf(point->X);
1033 leftx = (INT)leftxf;
1034 rightx = (INT)ceilf(point->X);
1035 topyf = floorf(point->Y);
1036 topy = (INT)topyf;
1037 bottomy = (INT)ceilf(point->Y);
1039 if (leftx == rightx && topy == bottomy)
1040 return sample_bitmap_pixel(src_rect, bits, width, height,
1041 leftx, topy, attributes);
1043 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
1044 leftx, topy, attributes);
1045 topright = sample_bitmap_pixel(src_rect, bits, width, height,
1046 rightx, topy, attributes);
1047 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
1048 leftx, bottomy, attributes);
1049 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
1050 rightx, bottomy, attributes);
1052 x_offset = point->X - leftxf;
1053 top = blend_colors(topleft, topright, x_offset);
1054 bottom = blend_colors(bottomleft, bottomright, x_offset);
1056 return blend_colors(top, bottom, point->Y - topyf);
1058 case InterpolationModeNearestNeighbor:
1060 FLOAT pixel_offset;
1061 switch (offset_mode)
1063 default:
1064 case PixelOffsetModeNone:
1065 case PixelOffsetModeHighSpeed:
1066 pixel_offset = 0.5;
1067 break;
1069 case PixelOffsetModeHalf:
1070 case PixelOffsetModeHighQuality:
1071 pixel_offset = 0.0;
1072 break;
1074 return sample_bitmap_pixel(src_rect, bits, width, height,
1075 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1081 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1083 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1086 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1087 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1089 switch (brush->bt)
1091 case BrushTypeSolidColor:
1093 if (is_fill)
1094 return TRUE;
1095 else
1097 /* cannot draw semi-transparent colors */
1098 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1101 case BrushTypeHatchFill:
1103 GpHatch *hatch = (GpHatch*)brush;
1104 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1105 ((hatch->backcol & 0xff000000) == 0xff000000);
1107 case BrushTypeLinearGradient:
1108 case BrushTypeTextureFill:
1109 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1110 default:
1111 return FALSE;
1115 static GpStatus brush_fill_path(GpGraphics *graphics, GpBrush *brush)
1117 GpStatus status = Ok;
1118 switch (brush->bt)
1120 case BrushTypeSolidColor:
1122 GpSolidFill *fill = (GpSolidFill*)brush;
1123 HBITMAP bmp = ARGB2BMP(fill->color);
1125 if (bmp)
1127 RECT rc;
1128 /* partially transparent fill */
1130 if (!SelectClipPath(graphics->hdc, RGN_AND))
1132 status = GenericError;
1133 DeleteObject(bmp);
1134 break;
1136 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1138 HDC hdc = CreateCompatibleDC(NULL);
1140 if (!hdc)
1142 status = OutOfMemory;
1143 DeleteObject(bmp);
1144 break;
1147 SelectObject(hdc, bmp);
1148 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1149 hdc, 0, 0, 1, 1);
1150 DeleteDC(hdc);
1153 DeleteObject(bmp);
1154 break;
1156 /* else fall through */
1158 default:
1160 HBRUSH gdibrush, old_brush;
1162 gdibrush = create_gdi_brush(brush, graphics->origin_x, graphics->origin_y);
1163 if (!gdibrush)
1165 status = OutOfMemory;
1166 break;
1169 old_brush = SelectObject(graphics->hdc, gdibrush);
1170 FillPath(graphics->hdc);
1171 SelectObject(graphics->hdc, old_brush);
1172 DeleteObject(gdibrush);
1173 break;
1177 return status;
1180 static BOOL brush_can_fill_pixels(GpBrush *brush)
1182 switch (brush->bt)
1184 case BrushTypeSolidColor:
1185 case BrushTypeHatchFill:
1186 case BrushTypeLinearGradient:
1187 case BrushTypeTextureFill:
1188 case BrushTypePathGradient:
1189 return TRUE;
1190 default:
1191 return FALSE;
1195 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1196 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1198 switch (brush->bt)
1200 case BrushTypeSolidColor:
1202 int x, y;
1203 GpSolidFill *fill = (GpSolidFill*)brush;
1204 for (x=0; x<fill_area->Width; x++)
1205 for (y=0; y<fill_area->Height; y++)
1206 argb_pixels[x + y*cdwStride] = fill->color;
1207 return Ok;
1209 case BrushTypeHatchFill:
1211 int x, y;
1212 GpHatch *fill = (GpHatch*)brush;
1213 const unsigned char *hatch_data;
1214 ARGB hatch_palette[4];
1216 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1217 return NotImplemented;
1219 init_hatch_palette(hatch_palette, fill->forecol, fill->backcol);
1221 /* See create_hatch_bitmap for an explanation of how index is derived. */
1222 for (y = 0; y < fill_area->Height; y++, argb_pixels += cdwStride)
1224 const int hy = ~(y + fill_area->Y - graphics->origin_y) & 7;
1225 const int hx = graphics->origin_x & 7;
1226 const unsigned int row = (0x10101 * hatch_data[hy]) >> hx;
1228 for (x = 0; x < fill_area->Width; x++)
1230 const unsigned int srow = row >> (~(x + fill_area->X) & 7);
1231 int index;
1232 if (hatch_data[8])
1233 index = (srow & 1) ? 2 : (srow & 0x82) ? 1 : 0;
1234 else
1235 index = (srow & 1) ? 3 : 0;
1237 argb_pixels[x] = hatch_palette[index];
1241 return Ok;
1243 case BrushTypeLinearGradient:
1245 GpLineGradient *fill = (GpLineGradient*)brush;
1246 GpPointF draw_points[3];
1247 GpStatus stat;
1248 int x, y;
1250 draw_points[0].X = fill_area->X;
1251 draw_points[0].Y = fill_area->Y;
1252 draw_points[1].X = fill_area->X+1;
1253 draw_points[1].Y = fill_area->Y;
1254 draw_points[2].X = fill_area->X;
1255 draw_points[2].Y = fill_area->Y+1;
1257 /* Transform the points to a co-ordinate space where X is the point's
1258 * position in the gradient, 0.0 being the start point and 1.0 the
1259 * end point. */
1260 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1261 WineCoordinateSpaceGdiDevice, draw_points, 3);
1263 if (stat == Ok)
1265 GpMatrix world_to_gradient = fill->transform;
1267 stat = GdipInvertMatrix(&world_to_gradient);
1268 if (stat == Ok)
1269 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1272 if (stat == Ok)
1274 REAL x_delta = draw_points[1].X - draw_points[0].X;
1275 REAL y_delta = draw_points[2].X - draw_points[0].X;
1277 for (y=0; y<fill_area->Height; y++)
1279 for (x=0; x<fill_area->Width; x++)
1281 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1283 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1288 return stat;
1290 case BrushTypeTextureFill:
1292 GpTexture *fill = (GpTexture*)brush;
1293 GpPointF draw_points[3];
1294 GpStatus stat;
1295 int x, y;
1296 GpBitmap *bitmap;
1297 int src_stride;
1298 GpRect src_area;
1300 if (fill->image->type != ImageTypeBitmap)
1302 FIXME("metafile texture brushes not implemented\n");
1303 return NotImplemented;
1306 bitmap = (GpBitmap*)fill->image;
1307 src_stride = sizeof(ARGB) * bitmap->width;
1309 src_area.X = src_area.Y = 0;
1310 src_area.Width = bitmap->width;
1311 src_area.Height = bitmap->height;
1313 draw_points[0].X = fill_area->X;
1314 draw_points[0].Y = fill_area->Y;
1315 draw_points[1].X = fill_area->X+1;
1316 draw_points[1].Y = fill_area->Y;
1317 draw_points[2].X = fill_area->X;
1318 draw_points[2].Y = fill_area->Y+1;
1320 /* Transform the points to the co-ordinate space of the bitmap. */
1321 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1322 WineCoordinateSpaceGdiDevice, draw_points, 3);
1324 if (stat == Ok)
1326 GpMatrix world_to_texture = fill->transform;
1328 stat = GdipInvertMatrix(&world_to_texture);
1329 if (stat == Ok)
1330 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1333 if (stat == Ok && !fill->bitmap_bits)
1335 BitmapData lockeddata;
1337 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1338 if (!fill->bitmap_bits)
1339 stat = OutOfMemory;
1341 if (stat == Ok)
1343 lockeddata.Width = bitmap->width;
1344 lockeddata.Height = bitmap->height;
1345 lockeddata.Stride = src_stride;
1346 lockeddata.PixelFormat = PixelFormat32bppARGB;
1347 lockeddata.Scan0 = fill->bitmap_bits;
1349 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1350 PixelFormat32bppARGB, &lockeddata);
1353 if (stat == Ok)
1354 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1356 if (stat == Ok)
1357 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1358 bitmap->width, bitmap->height,
1359 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1361 if (stat != Ok)
1363 heap_free(fill->bitmap_bits);
1364 fill->bitmap_bits = NULL;
1368 if (stat == Ok)
1370 REAL x_dx = draw_points[1].X - draw_points[0].X;
1371 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1372 REAL y_dx = draw_points[2].X - draw_points[0].X;
1373 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1375 for (y=0; y<fill_area->Height; y++)
1377 for (x=0; x<fill_area->Width; x++)
1379 GpPointF point;
1380 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1381 point.Y = draw_points[0].Y + x * x_dy + y * y_dy;
1383 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1384 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1385 &point, fill->imageattributes, graphics->interpolation,
1386 graphics->pixeloffset);
1391 return stat;
1393 case BrushTypePathGradient:
1395 GpPathGradient *fill = (GpPathGradient*)brush;
1396 GpPath *flat_path;
1397 GpMatrix world_to_device;
1398 GpStatus stat;
1399 int i, figure_start=0;
1400 GpPointF start_point, end_point, center_point;
1401 BYTE type;
1402 REAL min_yf, max_yf, line1_xf, line2_xf;
1403 INT min_y, max_y, min_x, max_x;
1404 INT x, y;
1405 ARGB outer_color;
1406 static BOOL transform_fixme_once;
1408 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1410 static int once;
1411 if (!once++)
1412 FIXME("path gradient focus not implemented\n");
1415 if (fill->gamma)
1417 static int once;
1418 if (!once++)
1419 FIXME("path gradient gamma correction not implemented\n");
1422 if (fill->blendcount)
1424 static int once;
1425 if (!once++)
1426 FIXME("path gradient blend not implemented\n");
1429 if (fill->pblendcount)
1431 static int once;
1432 if (!once++)
1433 FIXME("path gradient preset blend not implemented\n");
1436 if (!transform_fixme_once)
1438 BOOL is_identity=TRUE;
1439 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1440 if (!is_identity)
1442 FIXME("path gradient transform not implemented\n");
1443 transform_fixme_once = TRUE;
1447 stat = GdipClonePath(fill->path, &flat_path);
1449 if (stat != Ok)
1450 return stat;
1452 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1453 CoordinateSpaceWorld, &world_to_device);
1454 if (stat == Ok)
1456 stat = GdipTransformPath(flat_path, &world_to_device);
1458 if (stat == Ok)
1460 center_point = fill->center;
1461 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1464 if (stat == Ok)
1465 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1468 if (stat != Ok)
1470 GdipDeletePath(flat_path);
1471 return stat;
1474 for (i=0; i<flat_path->pathdata.Count; i++)
1476 int start_center_line=0, end_center_line=0;
1477 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1478 REAL center_distance;
1479 ARGB start_color, end_color;
1480 REAL dy, dx;
1482 type = flat_path->pathdata.Types[i];
1484 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1485 figure_start = i;
1487 start_point = flat_path->pathdata.Points[i];
1489 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1491 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1493 end_point = flat_path->pathdata.Points[figure_start];
1494 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1496 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1498 end_point = flat_path->pathdata.Points[i+1];
1499 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1501 else
1502 continue;
1504 outer_color = start_color;
1506 min_yf = center_point.Y;
1507 if (min_yf > start_point.Y) min_yf = start_point.Y;
1508 if (min_yf > end_point.Y) min_yf = end_point.Y;
1510 if (min_yf < fill_area->Y)
1511 min_y = fill_area->Y;
1512 else
1513 min_y = (INT)ceil(min_yf);
1515 max_yf = center_point.Y;
1516 if (max_yf < start_point.Y) max_yf = start_point.Y;
1517 if (max_yf < end_point.Y) max_yf = end_point.Y;
1519 if (max_yf > fill_area->Y + fill_area->Height)
1520 max_y = fill_area->Y + fill_area->Height;
1521 else
1522 max_y = (INT)ceil(max_yf);
1524 dy = end_point.Y - start_point.Y;
1525 dx = end_point.X - start_point.X;
1527 /* This is proportional to the distance from start-end line to center point. */
1528 center_distance = dy * (start_point.X - center_point.X) +
1529 dx * (center_point.Y - start_point.Y);
1531 for (y=min_y; y<max_y; y++)
1533 REAL yf = (REAL)y;
1535 if (!seen_start && yf >= start_point.Y)
1537 seen_start = TRUE;
1538 start_center_line ^= 1;
1540 if (!seen_end && yf >= end_point.Y)
1542 seen_end = TRUE;
1543 end_center_line ^= 1;
1545 if (!seen_center && yf >= center_point.Y)
1547 seen_center = TRUE;
1548 start_center_line ^= 1;
1549 end_center_line ^= 1;
1552 if (start_center_line)
1553 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1554 else
1555 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1557 if (end_center_line)
1558 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1559 else
1560 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1562 if (line1_xf < line2_xf)
1564 min_x = (INT)ceil(line1_xf);
1565 max_x = (INT)ceil(line2_xf);
1567 else
1569 min_x = (INT)ceil(line2_xf);
1570 max_x = (INT)ceil(line1_xf);
1573 if (min_x < fill_area->X)
1574 min_x = fill_area->X;
1575 if (max_x > fill_area->X + fill_area->Width)
1576 max_x = fill_area->X + fill_area->Width;
1578 for (x=min_x; x<max_x; x++)
1580 REAL xf = (REAL)x;
1581 REAL distance;
1583 if (start_color != end_color)
1585 REAL blend_amount, pdy, pdx;
1586 pdy = yf - center_point.Y;
1587 pdx = xf - center_point.X;
1589 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1591 /* Too close to center point, don't try to calculate outer color */
1592 outer_color = start_color;
1594 else
1596 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1597 outer_color = blend_colors(start_color, end_color, blend_amount);
1601 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1602 (end_point.X - start_point.X) * (yf - start_point.Y);
1604 distance = distance / center_distance;
1606 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1607 blend_colors(outer_color, fill->centercolor, distance);
1612 GdipDeletePath(flat_path);
1613 return stat;
1615 default:
1616 return NotImplemented;
1620 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1621 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1622 * should not be called on an hdc that has a path you care about. */
1623 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1624 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1626 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1627 GpMatrix matrix;
1628 HBRUSH brush = NULL;
1629 HPEN pen = NULL;
1630 PointF ptf[4], *custptf = NULL;
1631 POINT pt[4], *custpt = NULL;
1632 BYTE *tp = NULL;
1633 REAL theta, dsmall, dbig, dx, dy = 0.0;
1634 INT i, count;
1635 LOGBRUSH lb;
1636 BOOL customstroke;
1638 if((x1 == x2) && (y1 == y2))
1639 return;
1641 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1643 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1644 if(!customstroke){
1645 brush = CreateSolidBrush(color);
1646 lb.lbStyle = BS_SOLID;
1647 lb.lbColor = color;
1648 lb.lbHatch = 0;
1649 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1650 PS_JOIN_MITER, 1, &lb, 0,
1651 NULL);
1652 oldbrush = SelectObject(graphics->hdc, brush);
1653 oldpen = SelectObject(graphics->hdc, pen);
1656 switch(cap){
1657 case LineCapFlat:
1658 break;
1659 case LineCapSquare:
1660 case LineCapSquareAnchor:
1661 case LineCapDiamondAnchor:
1662 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1663 if(cap == LineCapDiamondAnchor){
1664 dsmall = cos(theta + M_PI_2) * size;
1665 dbig = sin(theta + M_PI_2) * size;
1667 else{
1668 dsmall = cos(theta + M_PI_4) * size;
1669 dbig = sin(theta + M_PI_4) * size;
1672 ptf[0].X = x2 - dsmall;
1673 ptf[1].X = x2 + dbig;
1675 ptf[0].Y = y2 - dbig;
1676 ptf[3].Y = y2 + dsmall;
1678 ptf[1].Y = y2 - dsmall;
1679 ptf[2].Y = y2 + dbig;
1681 ptf[3].X = x2 - dbig;
1682 ptf[2].X = x2 + dsmall;
1684 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1686 round_points(pt, ptf, 4);
1688 Polygon(graphics->hdc, pt, 4);
1690 break;
1691 case LineCapArrowAnchor:
1692 size = size * 4.0 / sqrt(3.0);
1694 dx = cos(M_PI / 6.0 + theta) * size;
1695 dy = sin(M_PI / 6.0 + theta) * size;
1697 ptf[0].X = x2 - dx;
1698 ptf[0].Y = y2 - dy;
1700 dx = cos(- M_PI / 6.0 + theta) * size;
1701 dy = sin(- M_PI / 6.0 + theta) * size;
1703 ptf[1].X = x2 - dx;
1704 ptf[1].Y = y2 - dy;
1706 ptf[2].X = x2;
1707 ptf[2].Y = y2;
1709 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1711 round_points(pt, ptf, 3);
1713 Polygon(graphics->hdc, pt, 3);
1715 break;
1716 case LineCapRoundAnchor:
1717 dx = dy = ANCHOR_WIDTH * size / 2.0;
1719 ptf[0].X = x2 - dx;
1720 ptf[0].Y = y2 - dy;
1721 ptf[1].X = x2 + dx;
1722 ptf[1].Y = y2 + dy;
1724 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 2);
1726 round_points(pt, ptf, 2);
1728 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1730 break;
1731 case LineCapTriangle:
1732 size = size / 2.0;
1733 dx = cos(M_PI_2 + theta) * size;
1734 dy = sin(M_PI_2 + theta) * size;
1736 ptf[0].X = x2 - dx;
1737 ptf[0].Y = y2 - dy;
1738 ptf[1].X = x2 + dx;
1739 ptf[1].Y = y2 + dy;
1741 dx = cos(theta) * size;
1742 dy = sin(theta) * size;
1744 ptf[2].X = x2 + dx;
1745 ptf[2].Y = y2 + dy;
1747 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1749 round_points(pt, ptf, 3);
1751 Polygon(graphics->hdc, pt, 3);
1753 break;
1754 case LineCapRound:
1755 dx = dy = size / 2.0;
1757 ptf[0].X = x2 - dx;
1758 ptf[0].Y = y2 - dy;
1759 ptf[1].X = x2 + dx;
1760 ptf[1].Y = y2 + dy;
1762 dx = -cos(M_PI_2 + theta) * size;
1763 dy = -sin(M_PI_2 + theta) * size;
1765 ptf[2].X = x2 - dx;
1766 ptf[2].Y = y2 - dy;
1767 ptf[3].X = x2 + dx;
1768 ptf[3].Y = y2 + dy;
1770 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1772 round_points(pt, ptf, 4);
1774 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1775 pt[2].y, pt[3].x, pt[3].y);
1777 break;
1778 case LineCapCustom:
1779 if(!custom)
1780 break;
1782 if (custom->type == CustomLineCapTypeAdjustableArrow)
1784 GpAdjustableArrowCap *arrow = (GpAdjustableArrowCap *)custom;
1785 if (arrow->cap.fill && arrow->height <= 0.0)
1786 break;
1789 count = custom->pathdata.Count;
1790 custptf = heap_alloc_zero(count * sizeof(PointF));
1791 custpt = heap_alloc_zero(count * sizeof(POINT));
1792 tp = heap_alloc_zero(count);
1794 if(!custptf || !custpt || !tp)
1795 goto custend;
1797 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1799 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1800 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1801 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1802 MatrixOrderAppend);
1803 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1804 GdipTransformMatrixPoints(&matrix, custptf, count);
1806 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, custptf, count);
1808 round_points(custpt, custptf, count);
1810 for(i = 0; i < count; i++)
1811 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1813 if(custom->fill){
1814 BeginPath(graphics->hdc);
1815 PolyDraw(graphics->hdc, custpt, tp, count);
1816 EndPath(graphics->hdc);
1817 StrokeAndFillPath(graphics->hdc);
1819 else
1820 PolyDraw(graphics->hdc, custpt, tp, count);
1822 custend:
1823 heap_free(custptf);
1824 heap_free(custpt);
1825 heap_free(tp);
1826 break;
1827 default:
1828 break;
1831 if(!customstroke){
1832 SelectObject(graphics->hdc, oldbrush);
1833 SelectObject(graphics->hdc, oldpen);
1834 DeleteObject(brush);
1835 DeleteObject(pen);
1839 /* Shortens the line by the given percent by changing x2, y2.
1840 * If percent is > 1.0 then the line will change direction.
1841 * If percent is negative it can lengthen the line. */
1842 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1844 REAL dist, theta, dx, dy;
1846 if((y1 == *y2) && (x1 == *x2))
1847 return;
1849 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1850 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1851 dx = cos(theta) * dist;
1852 dy = sin(theta) * dist;
1854 *x2 = *x2 + dx;
1855 *y2 = *y2 + dy;
1858 /* Shortens the line by the given amount by changing x2, y2.
1859 * If the amount is greater than the distance, the line will become length 0.
1860 * If the amount is negative, it can lengthen the line. */
1861 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1863 REAL dx, dy, percent;
1865 dx = *x2 - x1;
1866 dy = *y2 - y1;
1867 if(dx == 0 && dy == 0)
1868 return;
1870 percent = amt / sqrt(dx * dx + dy * dy);
1871 if(percent >= 1.0){
1872 *x2 = x1;
1873 *y2 = y1;
1874 return;
1877 shorten_line_percent(x1, y1, x2, y2, percent);
1880 /* Conducts a linear search to find the bezier points that will back off
1881 * the endpoint of the curve by a distance of amt. Linear search works
1882 * better than binary in this case because there are multiple solutions,
1883 * and binary searches often find a bad one. I don't think this is what
1884 * Windows does but short of rendering the bezier without GDI's help it's
1885 * the best we can do. If rev then work from the start of the passed points
1886 * instead of the end. */
1887 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1889 GpPointF origpt[4];
1890 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1891 INT i, first = 0, second = 1, third = 2, fourth = 3;
1893 if(rev){
1894 first = 3;
1895 second = 2;
1896 third = 1;
1897 fourth = 0;
1900 origx = pt[fourth].X;
1901 origy = pt[fourth].Y;
1902 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1904 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1905 /* reset bezier points to original values */
1906 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1907 /* Perform magic on bezier points. Order is important here.*/
1908 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1909 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1910 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1911 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1912 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1913 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1915 dx = pt[fourth].X - origx;
1916 dy = pt[fourth].Y - origy;
1918 diff = sqrt(dx * dx + dy * dy);
1919 percent += 0.0005 * amt;
1923 /* Draws a combination of bezier curves and lines between points. */
1924 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1925 GDIPCONST BYTE * types, INT count, BOOL caps)
1927 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1928 BYTE *tp = heap_alloc_zero(count);
1929 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1930 INT i, j;
1931 GpStatus status = GenericError;
1933 if(!count){
1934 status = Ok;
1935 goto end;
1937 if(!pti || !tp || !ptcopy){
1938 status = OutOfMemory;
1939 goto end;
1942 for(i = 1; i < count; i++){
1943 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1944 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1945 || !(types[i + 2] & PathPointTypeBezier)){
1946 ERR("Bad bezier points\n");
1947 goto end;
1949 i += 2;
1953 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1955 /* If we are drawing caps, go through the points and adjust them accordingly,
1956 * and draw the caps. */
1957 if(caps){
1958 switch(types[count - 1] & PathPointTypePathTypeMask){
1959 case PathPointTypeBezier:
1960 if(pen->endcap == LineCapArrowAnchor)
1961 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1962 else if((pen->endcap == LineCapCustom) && pen->customend)
1963 shorten_bezier_amt(&ptcopy[count - 4],
1964 pen->width * pen->customend->inset, FALSE);
1966 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1967 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1968 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1969 pt[count - 1].X, pt[count - 1].Y);
1971 break;
1972 case PathPointTypeLine:
1973 if(pen->endcap == LineCapArrowAnchor)
1974 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1975 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1976 pen->width);
1977 else if((pen->endcap == LineCapCustom) && pen->customend)
1978 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1979 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1980 pen->customend->inset * pen->width);
1982 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1983 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1984 pt[count - 1].Y);
1986 break;
1987 default:
1988 ERR("Bad path last point\n");
1989 goto end;
1992 /* Find start of points */
1993 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1994 == PathPointTypeStart); j++);
1996 switch(types[j] & PathPointTypePathTypeMask){
1997 case PathPointTypeBezier:
1998 if(pen->startcap == LineCapArrowAnchor)
1999 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
2000 else if((pen->startcap == LineCapCustom) && pen->customstart)
2001 shorten_bezier_amt(&ptcopy[j - 1],
2002 pen->width * pen->customstart->inset, TRUE);
2004 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2005 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
2006 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
2007 pt[j - 1].X, pt[j - 1].Y);
2009 break;
2010 case PathPointTypeLine:
2011 if(pen->startcap == LineCapArrowAnchor)
2012 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2013 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2014 pen->width);
2015 else if((pen->startcap == LineCapCustom) && pen->customstart)
2016 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2017 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2018 pen->customstart->inset * pen->width);
2020 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2021 pt[j].X, pt[j].Y, pt[j - 1].X,
2022 pt[j - 1].Y);
2024 break;
2025 default:
2026 ERR("Bad path points\n");
2027 goto end;
2031 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
2033 round_points(pti, ptcopy, count);
2035 for(i = 0; i < count; i++){
2036 tp[i] = convert_path_point_type(types[i]);
2039 PolyDraw(graphics->hdc, pti, tp, count);
2041 status = Ok;
2043 end:
2044 heap_free(pti);
2045 heap_free(ptcopy);
2046 heap_free(tp);
2048 return status;
2051 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2053 GpStatus result;
2055 BeginPath(graphics->hdc);
2056 result = draw_poly(graphics, NULL, path->pathdata.Points,
2057 path->pathdata.Types, path->pathdata.Count, FALSE);
2058 EndPath(graphics->hdc);
2059 return result;
2062 typedef enum GraphicsContainerType {
2063 BEGIN_CONTAINER,
2064 SAVE_GRAPHICS
2065 } GraphicsContainerType;
2067 typedef struct _GraphicsContainerItem {
2068 struct list entry;
2069 GraphicsContainer contid;
2070 GraphicsContainerType type;
2072 SmoothingMode smoothing;
2073 CompositingQuality compqual;
2074 InterpolationMode interpolation;
2075 CompositingMode compmode;
2076 TextRenderingHint texthint;
2077 REAL scale;
2078 GpUnit unit;
2079 PixelOffsetMode pixeloffset;
2080 UINT textcontrast;
2081 GpMatrix worldtrans;
2082 GpRegion* clip;
2083 INT origin_x, origin_y;
2084 } GraphicsContainerItem;
2086 static GpStatus init_container(GraphicsContainerItem** container,
2087 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
2088 GpStatus sts;
2090 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
2091 if(!(*container))
2092 return OutOfMemory;
2094 (*container)->contid = graphics->contid + 1;
2095 (*container)->type = type;
2097 (*container)->smoothing = graphics->smoothing;
2098 (*container)->compqual = graphics->compqual;
2099 (*container)->interpolation = graphics->interpolation;
2100 (*container)->compmode = graphics->compmode;
2101 (*container)->texthint = graphics->texthint;
2102 (*container)->scale = graphics->scale;
2103 (*container)->unit = graphics->unit;
2104 (*container)->textcontrast = graphics->textcontrast;
2105 (*container)->pixeloffset = graphics->pixeloffset;
2106 (*container)->origin_x = graphics->origin_x;
2107 (*container)->origin_y = graphics->origin_y;
2108 (*container)->worldtrans = graphics->worldtrans;
2110 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2111 if(sts != Ok){
2112 heap_free(*container);
2113 *container = NULL;
2114 return sts;
2117 return Ok;
2120 static void delete_container(GraphicsContainerItem* container)
2122 GdipDeleteRegion(container->clip);
2123 heap_free(container);
2126 static GpStatus restore_container(GpGraphics* graphics,
2127 GDIPCONST GraphicsContainerItem* container){
2128 GpStatus sts;
2129 GpRegion *newClip;
2131 sts = GdipCloneRegion(container->clip, &newClip);
2132 if(sts != Ok) return sts;
2134 graphics->worldtrans = container->worldtrans;
2136 GdipDeleteRegion(graphics->clip);
2137 graphics->clip = newClip;
2139 graphics->contid = container->contid - 1;
2141 graphics->smoothing = container->smoothing;
2142 graphics->compqual = container->compqual;
2143 graphics->interpolation = container->interpolation;
2144 graphics->compmode = container->compmode;
2145 graphics->texthint = container->texthint;
2146 graphics->scale = container->scale;
2147 graphics->unit = container->unit;
2148 graphics->textcontrast = container->textcontrast;
2149 graphics->pixeloffset = container->pixeloffset;
2150 graphics->origin_x = container->origin_x;
2151 graphics->origin_y = container->origin_y;
2153 return Ok;
2156 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2158 RECT wnd_rect;
2159 GpStatus stat=Ok;
2160 GpUnit unit;
2162 if(graphics->hwnd) {
2163 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2164 return GenericError;
2166 rect->X = wnd_rect.left;
2167 rect->Y = wnd_rect.top;
2168 rect->Width = wnd_rect.right - wnd_rect.left;
2169 rect->Height = wnd_rect.bottom - wnd_rect.top;
2170 }else if (graphics->image){
2171 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2172 if (stat == Ok && unit != UnitPixel)
2173 FIXME("need to convert from unit %i\n", unit);
2174 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2175 HBITMAP hbmp;
2176 BITMAP bmp;
2178 rect->X = 0;
2179 rect->Y = 0;
2181 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2182 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2184 rect->Width = bmp.bmWidth;
2185 rect->Height = bmp.bmHeight;
2187 else
2189 /* FIXME: ??? */
2190 rect->Width = 1;
2191 rect->Height = 1;
2193 }else{
2194 rect->X = 0;
2195 rect->Y = 0;
2196 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2197 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2200 return stat;
2203 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2205 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2207 if (stat == Ok && graphics->hdc)
2209 GpPointF points[4], min_point, max_point;
2210 int i;
2212 points[0].X = points[2].X = rect->X;
2213 points[0].Y = points[1].Y = rect->Y;
2214 points[1].X = points[3].X = rect->X + rect->Width;
2215 points[2].Y = points[3].Y = rect->Y + rect->Height;
2217 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2219 min_point = max_point = points[0];
2221 for (i=1; i<4; i++)
2223 if (points[i].X < min_point.X) min_point.X = points[i].X;
2224 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2225 if (points[i].X > max_point.X) max_point.X = points[i].X;
2226 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2229 rect->X = min_point.X;
2230 rect->Y = min_point.Y;
2231 rect->Width = max_point.X - min_point.X;
2232 rect->Height = max_point.Y - min_point.Y;
2235 return stat;
2238 /* on success, rgn will contain the region of the graphics object which
2239 * is visible after clipping has been applied */
2240 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2242 GpStatus stat;
2243 GpRectF rectf;
2244 GpRegion* tmp;
2246 /* Ignore graphics image bounds for metafiles */
2247 if (is_metafile_graphics(graphics))
2248 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2250 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2251 return stat;
2253 if((stat = GdipCreateRegion(&tmp)) != Ok)
2254 return stat;
2256 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2257 goto end;
2259 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2260 goto end;
2262 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2264 end:
2265 GdipDeleteRegion(tmp);
2266 return stat;
2269 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2271 REAL height;
2273 if (font->unit == UnitPixel)
2275 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres, graphics->printer_display);
2277 else
2279 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2280 height = units_to_pixels(font->emSize, font->unit, graphics->xres, graphics->printer_display);
2281 else
2282 height = units_to_pixels(font->emSize, font->unit, graphics->yres, graphics->printer_display);
2285 lf->lfHeight = -(height + 0.5);
2286 lf->lfWidth = 0;
2287 lf->lfEscapement = 0;
2288 lf->lfOrientation = 0;
2289 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2290 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2291 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2292 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2293 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2294 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2295 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2296 lf->lfQuality = DEFAULT_QUALITY;
2297 lf->lfPitchAndFamily = 0;
2298 lstrcpyW(lf->lfFaceName, font->family->FamilyName);
2301 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2302 GDIPCONST GpStringFormat *format, HFONT *hfont,
2303 LOGFONTW *lfw_return, GDIPCONST GpMatrix *matrix)
2305 HDC hdc = CreateCompatibleDC(0);
2306 GpPointF pt[3];
2307 REAL angle, rel_width, rel_height, font_height;
2308 LOGFONTW lfw;
2309 HFONT unscaled_font;
2310 TEXTMETRICW textmet;
2312 if (font->unit == UnitPixel || font->unit == UnitWorld)
2313 font_height = font->emSize;
2314 else
2316 REAL unit_scale, res;
2318 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2319 unit_scale = units_scale(font->unit, graphics->unit, res, graphics->printer_display);
2321 font_height = font->emSize * unit_scale;
2324 pt[0].X = 0.0;
2325 pt[0].Y = 0.0;
2326 pt[1].X = 1.0;
2327 pt[1].Y = 0.0;
2328 pt[2].X = 0.0;
2329 pt[2].Y = 1.0;
2330 if (matrix)
2332 GpMatrix xform = *matrix;
2333 GdipTransformMatrixPoints(&xform, pt, 3);
2336 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2337 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2338 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2339 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2340 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2341 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2342 /* If the font unit is not pixels scaling should not be applied */
2343 if (font->unit != UnitPixel && font->unit != UnitWorld)
2345 rel_width /= graphics->scale;
2346 rel_height /= graphics->scale;
2349 get_log_fontW(font, graphics, &lfw);
2350 lfw.lfHeight = -gdip_round(font_height * rel_height);
2351 unscaled_font = CreateFontIndirectW(&lfw);
2353 SelectObject(hdc, unscaled_font);
2354 GetTextMetricsW(hdc, &textmet);
2356 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2357 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2359 *hfont = CreateFontIndirectW(&lfw);
2361 if (lfw_return)
2362 *lfw_return = lfw;
2364 DeleteDC(hdc);
2365 DeleteObject(unscaled_font);
2368 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2370 TRACE("(%p, %p)\n", hdc, graphics);
2372 return GdipCreateFromHDC2(hdc, NULL, graphics);
2375 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2377 XFORM xform;
2379 if (graphics->hdc == NULL)
2381 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2382 return;
2385 GetTransform(graphics->hdc, 0x204, &xform);
2386 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2389 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2391 GpStatus retval;
2392 HBITMAP hbitmap;
2393 DIBSECTION dib;
2395 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2397 if(hDevice != NULL)
2398 FIXME("Don't know how to handle parameter hDevice\n");
2400 if(hdc == NULL)
2401 return OutOfMemory;
2403 if(graphics == NULL)
2404 return InvalidParameter;
2406 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2407 if(!*graphics) return OutOfMemory;
2409 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2411 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2412 heap_free(*graphics);
2413 return retval;
2416 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2417 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2418 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2420 (*graphics)->alpha_hdc = 1;
2423 (*graphics)->hdc = hdc;
2424 (*graphics)->hwnd = WindowFromDC(hdc);
2425 (*graphics)->owndc = FALSE;
2426 (*graphics)->smoothing = SmoothingModeDefault;
2427 (*graphics)->compqual = CompositingQualityDefault;
2428 (*graphics)->interpolation = InterpolationModeBilinear;
2429 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2430 (*graphics)->compmode = CompositingModeSourceOver;
2431 (*graphics)->unit = UnitDisplay;
2432 (*graphics)->scale = 1.0;
2433 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2434 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2435 (*graphics)->busy = FALSE;
2436 (*graphics)->textcontrast = 4;
2437 list_init(&(*graphics)->containers);
2438 (*graphics)->contid = 0;
2439 (*graphics)->printer_display = (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER);
2440 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2442 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2443 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2445 DeleteObject((*graphics)->gdi_clip);
2446 (*graphics)->gdi_clip = NULL;
2449 TRACE("<-- %p\n", *graphics);
2451 return Ok;
2454 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2456 GpStatus retval;
2458 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2459 if(!*graphics) return OutOfMemory;
2461 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2462 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2464 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2465 heap_free(*graphics);
2466 return retval;
2469 (*graphics)->hdc = NULL;
2470 (*graphics)->hwnd = NULL;
2471 (*graphics)->owndc = FALSE;
2472 (*graphics)->image = image;
2473 /* We have to store the image type here because the image may be freed
2474 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2475 (*graphics)->image_type = image->type;
2476 (*graphics)->smoothing = SmoothingModeDefault;
2477 (*graphics)->compqual = CompositingQualityDefault;
2478 (*graphics)->interpolation = InterpolationModeBilinear;
2479 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2480 (*graphics)->compmode = CompositingModeSourceOver;
2481 (*graphics)->unit = UnitDisplay;
2482 (*graphics)->scale = 1.0;
2483 (*graphics)->xres = image->xres;
2484 (*graphics)->yres = image->yres;
2485 (*graphics)->busy = FALSE;
2486 (*graphics)->textcontrast = 4;
2487 list_init(&(*graphics)->containers);
2488 (*graphics)->contid = 0;
2490 TRACE("<-- %p\n", *graphics);
2492 return Ok;
2495 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2497 GpStatus ret;
2498 HDC hdc;
2500 TRACE("(%p, %p)\n", hwnd, graphics);
2502 hdc = GetDC(hwnd);
2504 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2506 ReleaseDC(hwnd, hdc);
2507 return ret;
2510 (*graphics)->hwnd = hwnd;
2511 (*graphics)->owndc = TRUE;
2513 return Ok;
2516 /* FIXME: no icm handling */
2517 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2519 TRACE("(%p, %p)\n", hwnd, graphics);
2521 return GdipCreateFromHWND(hwnd, graphics);
2524 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2525 UINT access, IStream **stream)
2527 DWORD dwMode;
2528 HRESULT ret;
2530 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2532 if(!stream || !filename)
2533 return InvalidParameter;
2535 if(access & GENERIC_WRITE)
2536 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2537 else if(access & GENERIC_READ)
2538 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2539 else
2540 return InvalidParameter;
2542 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2544 return hresult_to_status(ret);
2547 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2549 GraphicsContainerItem *cont, *next;
2550 GpStatus stat;
2551 TRACE("(%p)\n", graphics);
2553 if(!graphics) return InvalidParameter;
2554 if(graphics->busy) return ObjectBusy;
2556 if (is_metafile_graphics(graphics))
2558 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2559 if (stat != Ok)
2560 return stat;
2563 if (graphics->temp_hdc)
2565 DeleteDC(graphics->temp_hdc);
2566 graphics->temp_hdc = NULL;
2569 if(graphics->owndc)
2570 ReleaseDC(graphics->hwnd, graphics->hdc);
2572 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2573 list_remove(&cont->entry);
2574 delete_container(cont);
2577 GdipDeleteRegion(graphics->clip);
2579 DeleteObject(graphics->gdi_clip);
2581 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2582 * do otherwise, but we can't have that in the test suite because it means
2583 * accessing freed memory. */
2584 graphics->busy = TRUE;
2586 heap_free(graphics);
2588 return Ok;
2591 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2592 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2594 GpStatus status;
2595 GpPath *path;
2596 GpRectF rect;
2598 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2599 width, height, startAngle, sweepAngle);
2601 if(!graphics || !pen || width <= 0 || height <= 0)
2602 return InvalidParameter;
2604 if(graphics->busy)
2605 return ObjectBusy;
2607 if (is_metafile_graphics(graphics))
2609 set_rect(&rect, x, y, width, height);
2610 return METAFILE_DrawArc((GpMetafile *)graphics->image, pen, &rect, startAngle, sweepAngle);
2613 status = GdipCreatePath(FillModeAlternate, &path);
2614 if (status != Ok) return status;
2616 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2617 if (status == Ok)
2618 status = GdipDrawPath(graphics, pen, path);
2620 GdipDeletePath(path);
2621 return status;
2624 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2625 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2627 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2628 width, height, startAngle, sweepAngle);
2630 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2633 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2634 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2636 GpPointF pt[4];
2638 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2639 x2, y2, x3, y3, x4, y4);
2641 if(!graphics || !pen)
2642 return InvalidParameter;
2644 if(graphics->busy)
2645 return ObjectBusy;
2647 pt[0].X = x1;
2648 pt[0].Y = y1;
2649 pt[1].X = x2;
2650 pt[1].Y = y2;
2651 pt[2].X = x3;
2652 pt[2].Y = y3;
2653 pt[3].X = x4;
2654 pt[3].Y = y4;
2655 return GdipDrawBeziers(graphics, pen, pt, 4);
2658 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2659 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2661 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2662 x2, y2, x3, y3, x4, y4);
2664 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2667 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2668 GDIPCONST GpPointF *points, INT count)
2670 GpStatus status;
2671 GpPath *path;
2673 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2675 if(!graphics || !pen || !points || (count <= 0))
2676 return InvalidParameter;
2678 if(graphics->busy)
2679 return ObjectBusy;
2681 status = GdipCreatePath(FillModeAlternate, &path);
2682 if (status != Ok) return status;
2684 status = GdipAddPathBeziers(path, points, count);
2685 if (status == Ok)
2686 status = GdipDrawPath(graphics, pen, path);
2688 GdipDeletePath(path);
2689 return status;
2692 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2693 GDIPCONST GpPoint *points, INT count)
2695 GpPointF *pts;
2696 GpStatus ret;
2697 INT i;
2699 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2701 if(!graphics || !pen || !points || (count <= 0))
2702 return InvalidParameter;
2704 if(graphics->busy)
2705 return ObjectBusy;
2707 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2708 if(!pts)
2709 return OutOfMemory;
2711 for(i = 0; i < count; i++){
2712 pts[i].X = (REAL)points[i].X;
2713 pts[i].Y = (REAL)points[i].Y;
2716 ret = GdipDrawBeziers(graphics,pen,pts,count);
2718 heap_free(pts);
2720 return ret;
2723 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2724 GDIPCONST GpPointF *points, INT count)
2726 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2728 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2731 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2732 GDIPCONST GpPoint *points, INT count)
2734 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2736 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2739 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2740 GDIPCONST GpPointF *points, INT count, REAL tension)
2742 GpPath *path;
2743 GpStatus status;
2745 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2747 if(!graphics || !pen || !points || count <= 0)
2748 return InvalidParameter;
2750 if(graphics->busy)
2751 return ObjectBusy;
2753 status = GdipCreatePath(FillModeAlternate, &path);
2754 if (status != Ok) return status;
2756 status = GdipAddPathClosedCurve2(path, points, count, tension);
2757 if (status == Ok)
2758 status = GdipDrawPath(graphics, pen, path);
2760 GdipDeletePath(path);
2762 return status;
2765 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2766 GDIPCONST GpPoint *points, INT count, REAL tension)
2768 GpPointF *ptf;
2769 GpStatus stat;
2770 INT i;
2772 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2774 if(!points || count <= 0)
2775 return InvalidParameter;
2777 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2778 if(!ptf)
2779 return OutOfMemory;
2781 for(i = 0; i < count; i++){
2782 ptf[i].X = (REAL)points[i].X;
2783 ptf[i].Y = (REAL)points[i].Y;
2786 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2788 heap_free(ptf);
2790 return stat;
2793 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2794 GDIPCONST GpPointF *points, INT count)
2796 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2798 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2801 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2802 GDIPCONST GpPoint *points, INT count)
2804 GpPointF *pointsF;
2805 GpStatus ret;
2806 INT i;
2808 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2810 if(!points)
2811 return InvalidParameter;
2813 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2814 if(!pointsF)
2815 return OutOfMemory;
2817 for(i = 0; i < count; i++){
2818 pointsF[i].X = (REAL)points[i].X;
2819 pointsF[i].Y = (REAL)points[i].Y;
2822 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2823 heap_free(pointsF);
2825 return ret;
2828 /* Approximates cardinal spline with Bezier curves. */
2829 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2830 GDIPCONST GpPointF *points, INT count, REAL tension)
2832 GpPath *path;
2833 GpStatus status;
2835 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2837 if(!graphics || !pen)
2838 return InvalidParameter;
2840 if(graphics->busy)
2841 return ObjectBusy;
2843 if(count < 2)
2844 return InvalidParameter;
2846 status = GdipCreatePath(FillModeAlternate, &path);
2847 if (status != Ok) return status;
2849 status = GdipAddPathCurve2(path, points, count, tension);
2850 if (status == Ok)
2851 status = GdipDrawPath(graphics, pen, path);
2853 GdipDeletePath(path);
2854 return status;
2857 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2858 GDIPCONST GpPoint *points, INT count, REAL tension)
2860 GpPointF *pointsF;
2861 GpStatus ret;
2862 INT i;
2864 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2866 if(!points)
2867 return InvalidParameter;
2869 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2870 if(!pointsF)
2871 return OutOfMemory;
2873 for(i = 0; i < count; i++){
2874 pointsF[i].X = (REAL)points[i].X;
2875 pointsF[i].Y = (REAL)points[i].Y;
2878 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2879 heap_free(pointsF);
2881 return ret;
2884 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2885 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2886 REAL tension)
2888 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2890 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2891 return InvalidParameter;
2894 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2897 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2898 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2899 REAL tension)
2901 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2903 if(count < 0){
2904 return OutOfMemory;
2907 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2908 return InvalidParameter;
2911 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2914 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2915 REAL y, REAL width, REAL height)
2917 GpPath *path;
2918 GpStatus status;
2919 GpRectF rect;
2921 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2923 if(!graphics || !pen)
2924 return InvalidParameter;
2926 if(graphics->busy)
2927 return ObjectBusy;
2929 if (is_metafile_graphics(graphics))
2931 set_rect(&rect, x, y, width, height);
2932 return METAFILE_DrawEllipse((GpMetafile *)graphics->image, pen, &rect);
2935 status = GdipCreatePath(FillModeAlternate, &path);
2936 if (status != Ok) return status;
2938 status = GdipAddPathEllipse(path, x, y, width, height);
2939 if (status == Ok)
2940 status = GdipDrawPath(graphics, pen, path);
2942 GdipDeletePath(path);
2943 return status;
2946 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2947 INT y, INT width, INT height)
2949 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2951 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2955 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2957 UINT width, height;
2959 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2961 if(!graphics || !image)
2962 return InvalidParameter;
2964 GdipGetImageWidth(image, &width);
2965 GdipGetImageHeight(image, &height);
2967 return GdipDrawImagePointRect(graphics, image, x, y,
2968 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2971 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2972 INT y)
2974 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2976 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2979 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2980 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2981 GpUnit srcUnit)
2983 GpPointF points[3];
2984 REAL scale_x, scale_y, width, height;
2986 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2988 if (!graphics || !image) return InvalidParameter;
2990 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres, graphics->printer_display);
2991 scale_x *= graphics->xres / image->xres;
2992 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres, graphics->printer_display);
2993 scale_y *= graphics->yres / image->yres;
2994 width = srcwidth * scale_x;
2995 height = srcheight * scale_y;
2997 points[0].X = points[2].X = x;
2998 points[0].Y = points[1].Y = y;
2999 points[1].X = x + width;
3000 points[2].Y = y + height;
3002 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3003 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3006 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3007 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3008 GpUnit srcUnit)
3010 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3013 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3014 GDIPCONST GpPointF *dstpoints, INT count)
3016 UINT width, height;
3018 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3020 if(!image)
3021 return InvalidParameter;
3023 GdipGetImageWidth(image, &width);
3024 GdipGetImageHeight(image, &height);
3026 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3027 width, height, UnitPixel, NULL, NULL, NULL);
3030 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3031 GDIPCONST GpPoint *dstpoints, INT count)
3033 GpPointF ptf[3];
3035 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3037 if (count != 3 || !dstpoints)
3038 return InvalidParameter;
3040 ptf[0].X = (REAL)dstpoints[0].X;
3041 ptf[0].Y = (REAL)dstpoints[0].Y;
3042 ptf[1].X = (REAL)dstpoints[1].X;
3043 ptf[1].Y = (REAL)dstpoints[1].Y;
3044 ptf[2].X = (REAL)dstpoints[2].X;
3045 ptf[2].Y = (REAL)dstpoints[2].Y;
3047 return GdipDrawImagePoints(graphics, image, ptf, count);
3050 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3051 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3053 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3054 return TRUE;
3057 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3058 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3059 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3060 DrawImageAbort callback, VOID * callbackData)
3062 GpPointF ptf[4];
3063 POINT pti[4];
3064 GpStatus stat;
3066 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3067 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3068 callbackData);
3070 if (count > 3)
3071 return NotImplemented;
3073 if(!graphics || !image || !points || count != 3)
3074 return InvalidParameter;
3076 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3077 debugstr_pointf(&points[2]));
3079 if (is_metafile_graphics(graphics))
3081 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
3082 image, points, count, srcx, srcy, srcwidth, srcheight,
3083 srcUnit, imageAttributes, callback, callbackData);
3086 memcpy(ptf, points, 3 * sizeof(GpPointF));
3088 /* Ensure source width/height is positive */
3089 if (srcwidth < 0)
3091 GpPointF tmp = ptf[1];
3092 srcx = srcx + srcwidth;
3093 srcwidth = -srcwidth;
3094 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
3095 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3096 ptf[1] = ptf[0];
3097 ptf[0] = tmp;
3100 if (srcheight < 0)
3102 GpPointF tmp = ptf[2];
3103 srcy = srcy + srcheight;
3104 srcheight = -srcheight;
3105 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
3106 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
3107 ptf[2] = ptf[0];
3108 ptf[0] = tmp;
3111 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3112 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3113 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
3114 return Ok;
3115 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
3116 round_points(pti, ptf, 4);
3118 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3119 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3121 srcx = units_to_pixels(srcx, srcUnit, image->xres, graphics->printer_display);
3122 srcy = units_to_pixels(srcy, srcUnit, image->yres, graphics->printer_display);
3123 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres, graphics->printer_display);
3124 srcheight = units_to_pixels(srcheight, srcUnit, image->yres, graphics->printer_display);
3125 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3127 if (image->type == ImageTypeBitmap)
3129 GpBitmap* bitmap = (GpBitmap*)image;
3130 BOOL do_resampling = FALSE;
3131 BOOL use_software = FALSE;
3133 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08lx\n",
3134 graphics->xres, graphics->yres,
3135 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3136 graphics->scale, image->xres, image->yres, bitmap->format,
3137 imageAttributes ? imageAttributes->outside_color : 0);
3139 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3140 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3141 srcx < 0 || srcy < 0 ||
3142 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3143 do_resampling = TRUE;
3145 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3146 (graphics->image && graphics->image->type == ImageTypeBitmap))
3147 use_software = TRUE;
3149 if (use_software)
3151 RECT dst_area;
3152 GpRectF graphics_bounds;
3153 GpRect src_area;
3154 int i, x, y, src_stride, dst_stride;
3155 GpMatrix dst_to_src;
3156 REAL m11, m12, m21, m22, mdx, mdy;
3157 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3158 BitmapData lockeddata;
3159 InterpolationMode interpolation = graphics->interpolation;
3160 PixelOffsetMode offset_mode = graphics->pixeloffset;
3161 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3162 REAL x_dx, x_dy, y_dx, y_dy;
3163 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3165 if (!imageAttributes)
3166 imageAttributes = &defaultImageAttributes;
3168 dst_area.left = dst_area.right = pti[0].x;
3169 dst_area.top = dst_area.bottom = pti[0].y;
3170 for (i=1; i<4; i++)
3172 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3173 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3174 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3175 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3178 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3179 if (stat != Ok) return stat;
3181 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3182 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3183 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3184 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3186 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3188 if (IsRectEmpty(&dst_area)) return Ok;
3190 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3191 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3192 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3193 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3194 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3195 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3197 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3199 stat = GdipInvertMatrix(&dst_to_src);
3200 if (stat != Ok) return stat;
3202 if (do_resampling)
3204 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3205 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3207 else
3209 /* Make sure src_area is equal in size to dst_area. */
3210 src_area.X = srcx + dst_area.left - pti[0].x;
3211 src_area.Y = srcy + dst_area.top - pti[0].y;
3212 src_area.Width = dst_area.right - dst_area.left;
3213 src_area.Height = dst_area.bottom - dst_area.top;
3216 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3218 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3219 if (!src_data)
3220 return OutOfMemory;
3221 src_stride = sizeof(ARGB) * src_area.Width;
3223 /* Read the bits we need from the source bitmap into a compatible buffer. */
3224 lockeddata.Width = src_area.Width;
3225 lockeddata.Height = src_area.Height;
3226 lockeddata.Stride = src_stride;
3227 lockeddata.Scan0 = src_data;
3228 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3229 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3230 else
3231 lockeddata.PixelFormat = PixelFormat32bppARGB;
3233 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3234 lockeddata.PixelFormat, &lockeddata);
3236 if (stat == Ok)
3237 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3239 if (stat != Ok)
3241 heap_free(src_data);
3242 return stat;
3245 apply_image_attributes(imageAttributes, src_data,
3246 src_area.Width, src_area.Height,
3247 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3249 if (do_resampling)
3251 /* Transform the bits as needed to the destination. */
3252 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3253 if (!dst_data)
3255 heap_free(src_data);
3256 return OutOfMemory;
3259 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3261 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3263 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3264 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3265 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3266 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3268 for (x=dst_area.left; x<dst_area.right; x++)
3270 for (y=dst_area.top; y<dst_area.bottom; y++)
3272 GpPointF src_pointf;
3273 ARGB *dst_color;
3275 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3276 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3278 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3280 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3281 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3282 imageAttributes, interpolation, offset_mode);
3283 else
3284 *dst_color = 0;
3288 else
3290 dst_data = src_data;
3291 dst_stride = src_stride;
3294 gdi_transform_acquire(graphics);
3296 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3297 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3298 lockeddata.PixelFormat);
3300 gdi_transform_release(graphics);
3302 heap_free(src_data);
3304 heap_free(dst_dyn_data);
3306 return stat;
3308 else
3310 HDC hdc;
3311 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3312 HBITMAP hbitmap, old_hbm=NULL;
3313 HRGN hrgn;
3314 INT save_state;
3316 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3317 bitmap->format == PixelFormat24bppRGB ||
3318 bitmap->format == PixelFormat32bppRGB ||
3319 bitmap->format == PixelFormat32bppPARGB))
3321 BITMAPINFOHEADER bih;
3322 BYTE *temp_bits;
3323 PixelFormat dst_format;
3325 /* we can't draw a bitmap of this format directly */
3326 hdc = CreateCompatibleDC(0);
3327 temp_hdc = TRUE;
3328 temp_bitmap = TRUE;
3330 bih.biSize = sizeof(BITMAPINFOHEADER);
3331 bih.biWidth = bitmap->width;
3332 bih.biHeight = -bitmap->height;
3333 bih.biPlanes = 1;
3334 bih.biBitCount = 32;
3335 bih.biCompression = BI_RGB;
3336 bih.biSizeImage = 0;
3337 bih.biXPelsPerMeter = 0;
3338 bih.biYPelsPerMeter = 0;
3339 bih.biClrUsed = 0;
3340 bih.biClrImportant = 0;
3342 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3343 (void**)&temp_bits, NULL, 0);
3345 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3346 dst_format = PixelFormat32bppPARGB;
3347 else
3348 dst_format = PixelFormat32bppRGB;
3350 convert_pixels(bitmap->width, bitmap->height,
3351 bitmap->width*4, temp_bits, dst_format, bitmap->image.palette,
3352 bitmap->stride, bitmap->bits, bitmap->format,
3353 bitmap->image.palette);
3355 else
3357 if (bitmap->hbitmap)
3358 hbitmap = bitmap->hbitmap;
3359 else
3361 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3362 temp_bitmap = TRUE;
3365 hdc = bitmap->hdc;
3366 temp_hdc = (hdc == 0);
3369 if (temp_hdc)
3371 if (!hdc) hdc = CreateCompatibleDC(0);
3372 old_hbm = SelectObject(hdc, hbitmap);
3375 save_state = SaveDC(graphics->hdc);
3377 stat = get_clip_hrgn(graphics, &hrgn);
3379 if (stat == Ok)
3381 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3382 DeleteObject(hrgn);
3385 gdi_transform_acquire(graphics);
3387 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3389 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3390 hdc, srcx, srcy, srcwidth, srcheight);
3392 else
3394 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3395 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3398 gdi_transform_release(graphics);
3400 RestoreDC(graphics->hdc, save_state);
3402 if (temp_hdc)
3404 SelectObject(hdc, old_hbm);
3405 DeleteDC(hdc);
3408 if (temp_bitmap)
3409 DeleteObject(hbitmap);
3412 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3414 GpRectF rc;
3416 set_rect(&rc, srcx, srcy, srcwidth, srcheight);
3417 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3418 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3420 else
3422 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3423 return InvalidParameter;
3426 return Ok;
3429 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3430 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3431 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3432 DrawImageAbort callback, VOID * callbackData)
3434 GpPointF pointsF[3];
3435 INT i;
3437 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3438 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3439 callbackData);
3441 if(!points || count!=3)
3442 return InvalidParameter;
3444 for(i = 0; i < count; i++){
3445 pointsF[i].X = (REAL)points[i].X;
3446 pointsF[i].Y = (REAL)points[i].Y;
3449 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3450 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3451 callback, callbackData);
3454 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3455 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3456 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3457 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3458 VOID * callbackData)
3460 GpPointF points[3];
3462 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3463 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3464 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3466 points[0].X = dstx;
3467 points[0].Y = dsty;
3468 points[1].X = dstx + dstwidth;
3469 points[1].Y = dsty;
3470 points[2].X = dstx;
3471 points[2].Y = dsty + dstheight;
3473 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3474 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3477 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3478 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3479 INT srcwidth, INT srcheight, GpUnit srcUnit,
3480 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3481 VOID * callbackData)
3483 GpPointF points[3];
3485 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3486 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3487 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3489 points[0].X = dstx;
3490 points[0].Y = dsty;
3491 points[1].X = dstx + dstwidth;
3492 points[1].Y = dsty;
3493 points[2].X = dstx;
3494 points[2].Y = dsty + dstheight;
3496 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3497 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3500 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3501 REAL x, REAL y, REAL width, REAL height)
3503 RectF bounds;
3504 GpUnit unit;
3505 GpStatus ret;
3507 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3509 if(!graphics || !image)
3510 return InvalidParameter;
3512 ret = GdipGetImageBounds(image, &bounds, &unit);
3513 if(ret != Ok)
3514 return ret;
3516 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3517 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3518 unit, NULL, NULL, NULL);
3521 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3522 INT x, INT y, INT width, INT height)
3524 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3526 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3529 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3530 REAL y1, REAL x2, REAL y2)
3532 GpPointF pt[2];
3534 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3536 if (!pen)
3537 return InvalidParameter;
3539 if (pen->unit == UnitPixel && pen->width <= 0.0)
3540 return Ok;
3542 pt[0].X = x1;
3543 pt[0].Y = y1;
3544 pt[1].X = x2;
3545 pt[1].Y = y2;
3546 return GdipDrawLines(graphics, pen, pt, 2);
3549 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3550 INT y1, INT x2, INT y2)
3552 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3554 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3557 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3558 GpPointF *points, INT count)
3560 GpStatus status;
3561 GpPath *path;
3563 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3565 if(!pen || !graphics || (count < 2))
3566 return InvalidParameter;
3568 if(graphics->busy)
3569 return ObjectBusy;
3571 status = GdipCreatePath(FillModeAlternate, &path);
3572 if (status != Ok) return status;
3574 status = GdipAddPathLine2(path, points, count);
3575 if (status == Ok)
3576 status = GdipDrawPath(graphics, pen, path);
3578 GdipDeletePath(path);
3579 return status;
3582 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3583 GpPoint *points, INT count)
3585 GpStatus retval;
3586 GpPointF *ptf;
3587 int i;
3589 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3591 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3592 if(!ptf) return OutOfMemory;
3594 for(i = 0; i < count; i ++){
3595 ptf[i].X = (REAL) points[i].X;
3596 ptf[i].Y = (REAL) points[i].Y;
3599 retval = GdipDrawLines(graphics, pen, ptf, count);
3601 heap_free(ptf);
3602 return retval;
3605 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3607 INT save_state;
3608 GpStatus retval;
3609 HRGN hrgn=NULL;
3611 save_state = prepare_dc(graphics, pen);
3613 retval = get_clip_hrgn(graphics, &hrgn);
3615 if (retval != Ok)
3616 goto end;
3618 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3620 gdi_transform_acquire(graphics);
3622 retval = draw_poly(graphics, pen, path->pathdata.Points,
3623 path->pathdata.Types, path->pathdata.Count, TRUE);
3625 gdi_transform_release(graphics);
3627 end:
3628 restore_dc(graphics, save_state);
3629 DeleteObject(hrgn);
3631 return retval;
3634 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3636 GpStatus stat;
3637 GpPath* flat_path;
3638 GpMatrix* transform;
3639 GpRectF gp_bound_rect;
3640 GpRect gp_output_area;
3641 RECT output_area;
3642 INT output_height, output_width;
3643 DWORD *output_bits, *brush_bits=NULL;
3644 int i;
3645 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3646 const BYTE *dash_pattern;
3647 INT dash_pattern_size;
3648 BYTE *dyn_dash_pattern = NULL;
3650 stat = GdipClonePath(path, &flat_path);
3652 if (stat != Ok)
3653 return stat;
3655 stat = GdipCreateMatrix(&transform);
3657 if (stat == Ok)
3659 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3660 CoordinateSpaceWorld, transform);
3662 if (stat == Ok)
3663 stat = GdipFlattenPath(flat_path, transform, 1.0);
3665 GdipDeleteMatrix(transform);
3668 /* estimate the output size in pixels, can be larger than necessary */
3669 if (stat == Ok)
3671 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3672 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3673 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3674 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3676 for (i=1; i<flat_path->pathdata.Count; i++)
3678 REAL x, y;
3679 x = flat_path->pathdata.Points[i].X;
3680 y = flat_path->pathdata.Points[i].Y;
3682 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3683 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3684 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3685 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3688 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3691 if (stat == Ok)
3693 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3694 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3695 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3696 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3698 output_width = output_area.right - output_area.left + 1;
3699 output_height = output_area.bottom - output_area.top + 1;
3701 if (output_width <= 0 || output_height <= 0)
3703 GdipDeletePath(flat_path);
3704 return Ok;
3707 gp_output_area.X = output_area.left;
3708 gp_output_area.Y = output_area.top;
3709 gp_output_area.Width = output_width;
3710 gp_output_area.Height = output_height;
3712 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3713 if (!output_bits)
3714 stat = OutOfMemory;
3717 if (stat == Ok)
3719 if (pen->brush->bt != BrushTypeSolidColor)
3721 /* allocate and draw brush output */
3722 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3724 if (brush_bits)
3726 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3727 &gp_output_area, output_width);
3729 else
3730 stat = OutOfMemory;
3733 if (stat == Ok)
3735 /* convert dash pattern to bool array */
3736 switch (pen->dash)
3738 case DashStyleCustom:
3740 dash_pattern_size = 0;
3742 for (i=0; i < pen->numdashes; i++)
3743 dash_pattern_size += gdip_round(pen->dashes[i]);
3745 if (dash_pattern_size != 0)
3747 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3749 if (dyn_dash_pattern)
3751 int j=0;
3752 for (i=0; i < pen->numdashes; i++)
3754 int k;
3755 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3756 dyn_dash_pattern[j++] = (i&1)^1;
3759 else
3760 stat = OutOfMemory;
3762 break;
3764 /* else fall through */
3766 case DashStyleSolid:
3767 default:
3768 dash_pattern = static_dash_pattern;
3769 dash_pattern_size = 1;
3770 break;
3771 case DashStyleDash:
3772 dash_pattern = static_dash_pattern;
3773 dash_pattern_size = 4;
3774 break;
3775 case DashStyleDot:
3776 dash_pattern = &static_dash_pattern[4];
3777 dash_pattern_size = 2;
3778 break;
3779 case DashStyleDashDot:
3780 dash_pattern = static_dash_pattern;
3781 dash_pattern_size = 6;
3782 break;
3783 case DashStyleDashDotDot:
3784 dash_pattern = static_dash_pattern;
3785 dash_pattern_size = 8;
3786 break;
3790 if (stat == Ok)
3792 /* trace path */
3793 GpPointF subpath_start = flat_path->pathdata.Points[0];
3794 INT prev_x = INT_MAX, prev_y = INT_MAX;
3795 int dash_pos = dash_pattern_size - 1;
3797 for (i=0; i < flat_path->pathdata.Count; i++)
3799 BYTE type, type2;
3800 GpPointF start_point, end_point;
3801 GpPoint start_pointi, end_pointi;
3803 type = flat_path->pathdata.Types[i];
3804 if (i+1 < flat_path->pathdata.Count)
3805 type2 = flat_path->pathdata.Types[i+1];
3806 else
3807 type2 = PathPointTypeStart;
3809 start_point = flat_path->pathdata.Points[i];
3811 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3812 subpath_start = start_point;
3814 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3815 end_point = subpath_start;
3816 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3817 continue;
3818 else
3819 end_point = flat_path->pathdata.Points[i+1];
3821 start_pointi.X = floorf(start_point.X);
3822 start_pointi.Y = floorf(start_point.Y);
3823 end_pointi.X = floorf(end_point.X);
3824 end_pointi.Y = floorf(end_point.Y);
3826 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3827 continue;
3829 /* draw line segment */
3830 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3832 INT x, y, start_y, end_y, step;
3834 if (start_pointi.Y < end_pointi.Y)
3836 step = 1;
3837 start_y = ceilf(start_point.Y) - output_area.top;
3838 end_y = end_pointi.Y - output_area.top;
3840 else
3842 step = -1;
3843 start_y = start_point.Y - output_area.top;
3844 end_y = ceilf(end_point.Y) - output_area.top;
3847 for (y=start_y; y != (end_y+step); y+=step)
3849 x = gdip_round( start_point.X +
3850 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3851 - output_area.left;
3853 if (x == prev_x && y == prev_y)
3854 continue;
3856 prev_x = x;
3857 prev_y = y;
3858 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3860 if (!dash_pattern[dash_pos])
3861 continue;
3863 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3864 continue;
3866 if (brush_bits)
3867 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3868 else
3869 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3872 else
3874 INT x, y, start_x, end_x, step;
3876 if (start_pointi.X < end_pointi.X)
3878 step = 1;
3879 start_x = ceilf(start_point.X) - output_area.left;
3880 end_x = end_pointi.X - output_area.left;
3882 else
3884 step = -1;
3885 start_x = start_point.X - output_area.left;
3886 end_x = ceilf(end_point.X) - output_area.left;
3889 for (x=start_x; x != (end_x+step); x+=step)
3891 y = gdip_round( start_point.Y +
3892 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3893 - output_area.top;
3895 if (x == prev_x && y == prev_y)
3896 continue;
3898 prev_x = x;
3899 prev_y = y;
3900 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3902 if (!dash_pattern[dash_pos])
3903 continue;
3905 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3906 continue;
3908 if (brush_bits)
3909 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3910 else
3911 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3917 /* draw output image */
3918 if (stat == Ok)
3920 gdi_transform_acquire(graphics);
3922 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3923 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3924 PixelFormat32bppARGB);
3926 gdi_transform_release(graphics);
3929 heap_free(brush_bits);
3930 heap_free(dyn_dash_pattern);
3931 heap_free(output_bits);
3934 GdipDeletePath(flat_path);
3936 return stat;
3939 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3941 GpStatus stat;
3942 GpPath *wide_path;
3943 GpMatrix *transform=NULL;
3944 REAL flatness=1.0;
3946 /* Check if the final pen thickness in pixels is too thin. */
3947 if (pen->unit == UnitPixel)
3949 if (pen->width < 1.415)
3950 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3952 else
3954 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3956 points[1].X = pen->width;
3957 points[2].Y = pen->width;
3959 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3960 CoordinateSpaceWorld, points, 3);
3962 if (stat != Ok)
3963 return stat;
3965 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3966 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3967 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3968 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3969 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3972 stat = GdipClonePath(path, &wide_path);
3974 if (stat != Ok)
3975 return stat;
3977 if (pen->unit == UnitPixel)
3979 /* We have to transform this to device coordinates to get the widths right. */
3980 stat = GdipCreateMatrix(&transform);
3982 if (stat == Ok)
3983 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3984 CoordinateSpaceWorld, transform);
3986 else
3988 /* Set flatness based on the final coordinate space */
3989 GpMatrix t;
3991 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3992 CoordinateSpaceWorld, &t);
3994 if (stat != Ok)
3995 return stat;
3997 flatness = 1.0/sqrt(fmax(
3998 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
3999 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
4002 if (stat == Ok)
4003 stat = GdipWidenPath(wide_path, pen, transform, flatness);
4005 if (pen->unit == UnitPixel)
4007 /* Transform the path back to world coordinates */
4008 if (stat == Ok)
4009 stat = GdipInvertMatrix(transform);
4011 if (stat == Ok)
4012 stat = GdipTransformPath(wide_path, transform);
4015 /* Actually draw the path */
4016 if (stat == Ok)
4017 stat = GdipFillPath(graphics, pen->brush, wide_path);
4019 GdipDeleteMatrix(transform);
4021 GdipDeletePath(wide_path);
4023 return stat;
4026 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
4028 GpStatus retval;
4030 TRACE("(%p, %p, %p)\n", graphics, pen, path);
4032 if(!pen || !graphics)
4033 return InvalidParameter;
4035 if(graphics->busy)
4036 return ObjectBusy;
4038 if (path->pathdata.Count == 0)
4039 return Ok;
4041 if (is_metafile_graphics(graphics))
4042 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
4043 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
4044 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
4045 else
4046 retval = GDI32_GdipDrawPath(graphics, pen, path);
4048 return retval;
4051 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
4052 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4054 GpStatus status;
4055 GpPath *path;
4057 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
4058 width, height, startAngle, sweepAngle);
4060 if(!graphics || !pen)
4061 return InvalidParameter;
4063 if(graphics->busy)
4064 return ObjectBusy;
4066 status = GdipCreatePath(FillModeAlternate, &path);
4067 if (status != Ok) return status;
4069 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4070 if (status == Ok)
4071 status = GdipDrawPath(graphics, pen, path);
4073 GdipDeletePath(path);
4074 return status;
4077 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
4078 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4080 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
4081 width, height, startAngle, sweepAngle);
4083 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4086 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
4087 REAL y, REAL width, REAL height)
4089 GpRectF rect;
4091 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
4093 set_rect(&rect, x, y, width, height);
4094 return GdipDrawRectangles(graphics, pen, &rect, 1);
4097 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
4098 INT y, INT width, INT height)
4100 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
4102 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4105 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
4106 GDIPCONST GpRectF* rects, INT count)
4108 GpStatus status;
4109 GpPath *path;
4111 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4113 if(!graphics || !pen || !rects || count < 1)
4114 return InvalidParameter;
4116 if(graphics->busy)
4117 return ObjectBusy;
4119 if (is_metafile_graphics(graphics))
4120 return METAFILE_DrawRectangles((GpMetafile *)graphics->image, pen, rects, count);
4122 status = GdipCreatePath(FillModeAlternate, &path);
4123 if (status != Ok) return status;
4125 status = GdipAddPathRectangles(path, rects, count);
4126 if (status == Ok)
4127 status = GdipDrawPath(graphics, pen, path);
4129 GdipDeletePath(path);
4130 return status;
4133 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4134 GDIPCONST GpRect* rects, INT count)
4136 GpRectF *rectsF;
4137 GpStatus ret;
4138 INT i;
4140 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4142 if(!rects || count<=0)
4143 return InvalidParameter;
4145 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4146 if(!rectsF)
4147 return OutOfMemory;
4149 for(i = 0;i < count;i++)
4150 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4152 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4153 heap_free(rectsF);
4155 return ret;
4158 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4159 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4161 GpPath *path;
4162 GpStatus status;
4164 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4165 count, tension, fill);
4167 if(!graphics || !brush || !points)
4168 return InvalidParameter;
4170 if(graphics->busy)
4171 return ObjectBusy;
4173 if(count == 1) /* Do nothing */
4174 return Ok;
4176 status = GdipCreatePath(fill, &path);
4177 if (status != Ok) return status;
4179 status = GdipAddPathClosedCurve2(path, points, count, tension);
4180 if (status == Ok)
4181 status = GdipFillPath(graphics, brush, path);
4183 GdipDeletePath(path);
4184 return status;
4187 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4188 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4190 GpPointF *ptf;
4191 GpStatus stat;
4192 INT i;
4194 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4195 count, tension, fill);
4197 if(!points || count == 0)
4198 return InvalidParameter;
4200 if(count == 1) /* Do nothing */
4201 return Ok;
4203 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4204 if(!ptf)
4205 return OutOfMemory;
4207 for(i = 0;i < count;i++){
4208 ptf[i].X = (REAL)points[i].X;
4209 ptf[i].Y = (REAL)points[i].Y;
4212 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4214 heap_free(ptf);
4216 return stat;
4219 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4220 GDIPCONST GpPointF *points, INT count)
4222 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4223 return GdipFillClosedCurve2(graphics, brush, points, count,
4224 0.5f, FillModeAlternate);
4227 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4228 GDIPCONST GpPoint *points, INT count)
4230 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4231 return GdipFillClosedCurve2I(graphics, brush, points, count,
4232 0.5f, FillModeAlternate);
4235 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4236 REAL y, REAL width, REAL height)
4238 GpStatus stat;
4239 GpPath *path;
4240 GpRectF rect;
4242 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4244 if(!graphics || !brush)
4245 return InvalidParameter;
4247 if(graphics->busy)
4248 return ObjectBusy;
4250 if (is_metafile_graphics(graphics))
4252 set_rect(&rect, x, y, width, height);
4253 return METAFILE_FillEllipse((GpMetafile *)graphics->image, brush, &rect);
4256 stat = GdipCreatePath(FillModeAlternate, &path);
4258 if (stat == Ok)
4260 stat = GdipAddPathEllipse(path, x, y, width, height);
4262 if (stat == Ok)
4263 stat = GdipFillPath(graphics, brush, path);
4265 GdipDeletePath(path);
4268 return stat;
4271 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4272 INT y, INT width, INT height)
4274 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4276 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4279 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4281 INT save_state;
4282 GpStatus retval;
4283 HRGN hrgn=NULL;
4285 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4286 return NotImplemented;
4288 save_state = SaveDC(graphics->hdc);
4289 EndPath(graphics->hdc);
4290 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4291 : WINDING));
4293 retval = get_clip_hrgn(graphics, &hrgn);
4295 if (retval != Ok)
4296 goto end;
4298 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4300 gdi_transform_acquire(graphics);
4302 BeginPath(graphics->hdc);
4303 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4304 path->pathdata.Types, path->pathdata.Count, FALSE);
4306 if(retval == Ok)
4308 EndPath(graphics->hdc);
4309 retval = brush_fill_path(graphics, brush);
4312 gdi_transform_release(graphics);
4314 end:
4315 RestoreDC(graphics->hdc, save_state);
4316 DeleteObject(hrgn);
4318 return retval;
4321 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4323 GpStatus stat;
4324 GpRegion *rgn;
4326 if (!brush_can_fill_pixels(brush))
4327 return NotImplemented;
4329 /* FIXME: This could probably be done more efficiently without regions. */
4331 stat = GdipCreateRegionPath(path, &rgn);
4333 if (stat == Ok)
4335 stat = GdipFillRegion(graphics, brush, rgn);
4337 GdipDeleteRegion(rgn);
4340 return stat;
4343 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4345 GpStatus stat = NotImplemented;
4347 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4349 if(!brush || !graphics || !path)
4350 return InvalidParameter;
4352 if(graphics->busy)
4353 return ObjectBusy;
4355 if (!path->pathdata.Count)
4356 return Ok;
4358 if (is_metafile_graphics(graphics))
4359 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4361 if (!graphics->image && !graphics->alpha_hdc)
4362 stat = GDI32_GdipFillPath(graphics, brush, path);
4364 if (stat == NotImplemented)
4365 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4367 if (stat == NotImplemented)
4369 FIXME("Not implemented for brushtype %i\n", brush->bt);
4370 stat = Ok;
4373 return stat;
4376 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4377 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4379 GpStatus stat;
4380 GpPath *path;
4381 GpRectF rect;
4383 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4384 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4386 if(!graphics || !brush)
4387 return InvalidParameter;
4389 if(graphics->busy)
4390 return ObjectBusy;
4392 if (is_metafile_graphics(graphics))
4394 set_rect(&rect, x, y, width, height);
4395 return METAFILE_FillPie((GpMetafile *)graphics->image, brush, &rect, startAngle, sweepAngle);
4398 stat = GdipCreatePath(FillModeAlternate, &path);
4400 if (stat == Ok)
4402 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4404 if (stat == Ok)
4405 stat = GdipFillPath(graphics, brush, path);
4407 GdipDeletePath(path);
4410 return stat;
4413 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4414 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4416 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4417 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4419 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4422 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4423 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4425 GpStatus stat;
4426 GpPath *path;
4428 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4430 if(!graphics || !brush || !points || !count)
4431 return InvalidParameter;
4433 if(graphics->busy)
4434 return ObjectBusy;
4436 stat = GdipCreatePath(fillMode, &path);
4438 if (stat == Ok)
4440 stat = GdipAddPathPolygon(path, points, count);
4442 if (stat == Ok)
4443 stat = GdipFillPath(graphics, brush, path);
4445 GdipDeletePath(path);
4448 return stat;
4451 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4452 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4454 GpStatus stat;
4455 GpPath *path;
4457 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4459 if(!graphics || !brush || !points || !count)
4460 return InvalidParameter;
4462 if(graphics->busy)
4463 return ObjectBusy;
4465 stat = GdipCreatePath(fillMode, &path);
4467 if (stat == Ok)
4469 stat = GdipAddPathPolygonI(path, points, count);
4471 if (stat == Ok)
4472 stat = GdipFillPath(graphics, brush, path);
4474 GdipDeletePath(path);
4477 return stat;
4480 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4481 GDIPCONST GpPointF *points, INT count)
4483 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4485 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4488 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4489 GDIPCONST GpPoint *points, INT count)
4491 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4493 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4496 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4497 REAL x, REAL y, REAL width, REAL height)
4499 GpRectF rect;
4501 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4503 set_rect(&rect, x, y, width, height);
4504 return GdipFillRectangles(graphics, brush, &rect, 1);
4507 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4508 INT x, INT y, INT width, INT height)
4510 GpRectF rect;
4512 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4514 set_rect(&rect, x, y, width, height);
4515 return GdipFillRectangles(graphics, brush, &rect, 1);
4518 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4519 INT count)
4521 GpStatus status;
4522 GpPath *path;
4524 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4526 if(!graphics || !brush || !rects || count <= 0)
4527 return InvalidParameter;
4529 if (is_metafile_graphics(graphics))
4531 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4532 /* FIXME: Add gdi32 drawing. */
4533 return status;
4536 status = GdipCreatePath(FillModeAlternate, &path);
4537 if (status != Ok) return status;
4539 status = GdipAddPathRectangles(path, rects, count);
4540 if (status == Ok)
4541 status = GdipFillPath(graphics, brush, path);
4543 GdipDeletePath(path);
4544 return status;
4547 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4548 INT count)
4550 GpRectF *rectsF;
4551 GpStatus ret;
4552 INT i;
4554 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4556 if(!rects || count <= 0)
4557 return InvalidParameter;
4559 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4560 if(!rectsF)
4561 return OutOfMemory;
4563 for(i = 0; i < count; i++)
4564 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4566 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4567 heap_free(rectsF);
4569 return ret;
4572 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4573 GpRegion* region)
4575 INT save_state;
4576 GpStatus status;
4577 HRGN hrgn;
4578 RECT rc;
4580 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4581 return NotImplemented;
4583 save_state = SaveDC(graphics->hdc);
4584 EndPath(graphics->hdc);
4586 hrgn = NULL;
4587 status = get_clip_hrgn(graphics, &hrgn);
4588 if (status != Ok)
4590 RestoreDC(graphics->hdc, save_state);
4591 return status;
4594 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4595 DeleteObject(hrgn);
4597 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4598 if (status != Ok)
4600 RestoreDC(graphics->hdc, save_state);
4601 return status;
4604 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4605 DeleteObject(hrgn);
4607 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4609 BeginPath(graphics->hdc);
4610 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4611 EndPath(graphics->hdc);
4613 status = brush_fill_path(graphics, brush);
4616 RestoreDC(graphics->hdc, save_state);
4619 return status;
4622 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4623 GpRegion* region)
4625 GpStatus stat;
4626 GpRegion *temp_region;
4627 GpMatrix world_to_device;
4628 GpRectF graphics_bounds;
4629 DWORD *pixel_data;
4630 HRGN hregion;
4631 RECT bound_rect;
4632 GpRect gp_bound_rect;
4634 if (!brush_can_fill_pixels(brush))
4635 return NotImplemented;
4637 stat = gdi_transform_acquire(graphics);
4639 if (stat == Ok)
4640 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4642 if (stat == Ok)
4643 stat = GdipCloneRegion(region, &temp_region);
4645 if (stat == Ok)
4647 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4648 CoordinateSpaceWorld, &world_to_device);
4650 if (stat == Ok)
4651 stat = GdipTransformRegion(temp_region, &world_to_device);
4653 if (stat == Ok)
4654 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4656 if (stat == Ok)
4657 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4659 GdipDeleteRegion(temp_region);
4662 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4664 DeleteObject(hregion);
4665 gdi_transform_release(graphics);
4666 return Ok;
4669 if (stat == Ok)
4671 gp_bound_rect.X = bound_rect.left;
4672 gp_bound_rect.Y = bound_rect.top;
4673 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4674 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4676 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4677 if (!pixel_data)
4678 stat = OutOfMemory;
4680 if (stat == Ok)
4682 stat = brush_fill_pixels(graphics, brush, pixel_data,
4683 &gp_bound_rect, gp_bound_rect.Width);
4685 if (stat == Ok)
4686 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4687 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4688 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4689 PixelFormat32bppARGB);
4691 heap_free(pixel_data);
4694 DeleteObject(hregion);
4697 gdi_transform_release(graphics);
4699 return stat;
4702 /*****************************************************************************
4703 * GdipFillRegion [GDIPLUS.@]
4705 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4706 GpRegion* region)
4708 GpStatus stat = NotImplemented;
4710 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4712 if (!(graphics && brush && region))
4713 return InvalidParameter;
4715 if(graphics->busy)
4716 return ObjectBusy;
4718 if (is_metafile_graphics(graphics))
4719 stat = METAFILE_FillRegion((GpMetafile*)graphics->image, brush, region);
4720 else
4722 if (!graphics->image && !graphics->alpha_hdc)
4723 stat = GDI32_GdipFillRegion(graphics, brush, region);
4725 if (stat == NotImplemented)
4726 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4729 if (stat == NotImplemented)
4731 FIXME("not implemented for brushtype %i\n", brush->bt);
4732 stat = Ok;
4735 return stat;
4738 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4740 TRACE("(%p,%u)\n", graphics, intention);
4742 if(!graphics)
4743 return InvalidParameter;
4745 if(graphics->busy)
4746 return ObjectBusy;
4748 /* We have no internal operation queue, so there's no need to clear it. */
4750 if (graphics->hdc)
4751 GdiFlush();
4753 return Ok;
4756 /*****************************************************************************
4757 * GdipGetClipBounds [GDIPLUS.@]
4759 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4761 GpStatus status;
4762 GpRegion *clip;
4764 TRACE("(%p, %p)\n", graphics, rect);
4766 if(!graphics)
4767 return InvalidParameter;
4769 if(graphics->busy)
4770 return ObjectBusy;
4772 status = GdipCreateRegion(&clip);
4773 if (status != Ok) return status;
4775 status = GdipGetClip(graphics, clip);
4776 if (status == Ok)
4777 status = GdipGetRegionBounds(clip, graphics, rect);
4779 GdipDeleteRegion(clip);
4780 return status;
4783 /*****************************************************************************
4784 * GdipGetClipBoundsI [GDIPLUS.@]
4786 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4788 GpRectF rectf;
4789 GpStatus stat;
4791 TRACE("(%p, %p)\n", graphics, rect);
4793 if (!rect)
4794 return InvalidParameter;
4796 if ((stat = GdipGetClipBounds(graphics, &rectf)) == Ok)
4798 rect->X = gdip_round(rectf.X);
4799 rect->Y = gdip_round(rectf.Y);
4800 rect->Width = gdip_round(rectf.Width);
4801 rect->Height = gdip_round(rectf.Height);
4804 return stat;
4807 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4808 CompositingMode *mode)
4810 TRACE("(%p, %p)\n", graphics, mode);
4812 if(!graphics || !mode)
4813 return InvalidParameter;
4815 if(graphics->busy)
4816 return ObjectBusy;
4818 *mode = graphics->compmode;
4820 return Ok;
4823 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4824 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4825 CompositingQuality *quality)
4827 TRACE("(%p, %p)\n", graphics, quality);
4829 if(!graphics || !quality)
4830 return InvalidParameter;
4832 if(graphics->busy)
4833 return ObjectBusy;
4835 *quality = graphics->compqual;
4837 return Ok;
4840 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4841 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4842 InterpolationMode *mode)
4844 TRACE("(%p, %p)\n", graphics, mode);
4846 if(!graphics || !mode)
4847 return InvalidParameter;
4849 if(graphics->busy)
4850 return ObjectBusy;
4852 *mode = graphics->interpolation;
4854 return Ok;
4857 /* FIXME: Need to handle color depths less than 24bpp */
4858 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4860 TRACE("(%p, %p)\n", graphics, argb);
4862 if(!graphics || !argb)
4863 return InvalidParameter;
4865 if(graphics->busy)
4866 return ObjectBusy;
4868 if (graphics->image && graphics->image->type == ImageTypeBitmap)
4870 static int once;
4871 GpBitmap *bitmap = (GpBitmap *)graphics->image;
4872 if (IsIndexedPixelFormat(bitmap->format) && !once++)
4873 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4876 return Ok;
4879 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4881 TRACE("(%p, %p)\n", graphics, scale);
4883 if(!graphics || !scale)
4884 return InvalidParameter;
4886 if(graphics->busy)
4887 return ObjectBusy;
4889 *scale = graphics->scale;
4891 return Ok;
4894 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4896 TRACE("(%p, %p)\n", graphics, unit);
4898 if(!graphics || !unit)
4899 return InvalidParameter;
4901 if(graphics->busy)
4902 return ObjectBusy;
4904 *unit = graphics->unit;
4906 return Ok;
4909 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4910 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4911 *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->pixeloffset;
4923 return Ok;
4926 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4927 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4929 TRACE("(%p, %p)\n", graphics, mode);
4931 if(!graphics || !mode)
4932 return InvalidParameter;
4934 if(graphics->busy)
4935 return ObjectBusy;
4937 *mode = graphics->smoothing;
4939 return Ok;
4942 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4944 TRACE("(%p, %p)\n", graphics, contrast);
4946 if(!graphics || !contrast)
4947 return InvalidParameter;
4949 *contrast = graphics->textcontrast;
4951 return Ok;
4954 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4955 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4956 TextRenderingHint *hint)
4958 TRACE("(%p, %p)\n", graphics, hint);
4960 if(!graphics || !hint)
4961 return InvalidParameter;
4963 if(graphics->busy)
4964 return ObjectBusy;
4966 *hint = graphics->texthint;
4968 return Ok;
4971 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4973 GpRegion *clip_rgn;
4974 GpStatus stat;
4975 GpMatrix device_to_world;
4977 TRACE("(%p, %p)\n", graphics, rect);
4979 if(!graphics || !rect)
4980 return InvalidParameter;
4982 if(graphics->busy)
4983 return ObjectBusy;
4985 /* intersect window and graphics clipping regions */
4986 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4987 return stat;
4989 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4990 goto cleanup;
4992 /* transform to world coordinates */
4993 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4994 goto cleanup;
4996 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4997 goto cleanup;
4999 /* get bounds of the region */
5000 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
5002 cleanup:
5003 GdipDeleteRegion(clip_rgn);
5005 return stat;
5008 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
5010 GpRectF rectf;
5011 GpStatus stat;
5013 TRACE("(%p, %p)\n", graphics, rect);
5015 if(!graphics || !rect)
5016 return InvalidParameter;
5018 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
5020 rect->X = gdip_round(rectf.X);
5021 rect->Y = gdip_round(rectf.Y);
5022 rect->Width = gdip_round(rectf.Width);
5023 rect->Height = gdip_round(rectf.Height);
5026 return stat;
5029 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5031 TRACE("(%p, %p)\n", graphics, matrix);
5033 if(!graphics || !matrix)
5034 return InvalidParameter;
5036 if(graphics->busy)
5037 return ObjectBusy;
5039 *matrix = graphics->worldtrans;
5040 return Ok;
5043 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
5045 GpSolidFill *brush;
5046 GpStatus stat;
5047 GpRectF wnd_rect;
5048 CompositingMode prev_comp_mode;
5050 TRACE("(%p, %lx)\n", graphics, color);
5052 if(!graphics)
5053 return InvalidParameter;
5055 if(graphics->busy)
5056 return ObjectBusy;
5058 if (is_metafile_graphics(graphics))
5059 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
5061 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
5062 return stat;
5064 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
5065 GdipDeleteBrush((GpBrush*)brush);
5066 return stat;
5069 GdipGetCompositingMode(graphics, &prev_comp_mode);
5070 GdipSetCompositingMode(graphics, CompositingModeSourceCopy);
5071 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
5072 wnd_rect.Width, wnd_rect.Height);
5073 GdipSetCompositingMode(graphics, prev_comp_mode);
5075 GdipDeleteBrush((GpBrush*)brush);
5077 return Ok;
5080 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
5082 TRACE("(%p, %p)\n", graphics, res);
5084 if(!graphics || !res)
5085 return InvalidParameter;
5087 return GdipIsEmptyRegion(graphics->clip, graphics, res);
5090 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
5092 GpStatus stat;
5093 GpRegion* rgn;
5094 GpPointF pt;
5096 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
5098 if(!graphics || !result)
5099 return InvalidParameter;
5101 if(graphics->busy)
5102 return ObjectBusy;
5104 pt.X = x;
5105 pt.Y = y;
5106 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5107 CoordinateSpaceWorld, &pt, 1)) != Ok)
5108 return stat;
5110 if((stat = GdipCreateRegion(&rgn)) != Ok)
5111 return stat;
5113 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5114 goto cleanup;
5116 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
5118 cleanup:
5119 GdipDeleteRegion(rgn);
5120 return stat;
5123 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
5125 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
5128 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
5130 GpStatus stat;
5131 GpRegion* rgn;
5132 GpPointF pts[2];
5134 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
5136 if(!graphics || !result)
5137 return InvalidParameter;
5139 if(graphics->busy)
5140 return ObjectBusy;
5142 pts[0].X = x;
5143 pts[0].Y = y;
5144 pts[1].X = x + width;
5145 pts[1].Y = y + height;
5147 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5148 CoordinateSpaceWorld, pts, 2)) != Ok)
5149 return stat;
5151 pts[1].X -= pts[0].X;
5152 pts[1].Y -= pts[0].Y;
5154 if((stat = GdipCreateRegion(&rgn)) != Ok)
5155 return stat;
5157 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5158 goto cleanup;
5160 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5162 cleanup:
5163 GdipDeleteRegion(rgn);
5164 return stat;
5167 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5169 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5172 GpStatus gdip_format_string(HDC hdc,
5173 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5174 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5175 gdip_format_string_callback callback, void *user_data)
5177 WCHAR* stringdup;
5178 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5179 nheight, lineend, lineno = 0;
5180 RectF bounds;
5181 StringAlignment halign;
5182 GpStatus stat = Ok;
5183 SIZE size;
5184 HotkeyPrefix hkprefix;
5185 INT *hotkeyprefix_offsets=NULL;
5186 INT hotkeyprefix_count=0;
5187 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5188 BOOL seen_prefix = FALSE;
5190 if(length == -1) length = lstrlenW(string);
5192 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5193 if(!stringdup) return OutOfMemory;
5195 if (!format)
5196 format = &default_drawstring_format;
5198 nwidth = rect->Width;
5199 nheight = rect->Height;
5200 if (ignore_empty_clip)
5202 if (!nwidth) nwidth = INT_MAX;
5203 if (!nheight) nheight = INT_MAX;
5206 hkprefix = format->hkprefix;
5208 if (hkprefix == HotkeyPrefixShow)
5210 for (i=0; i<length; i++)
5212 if (string[i] == '&')
5213 hotkeyprefix_count++;
5217 if (hotkeyprefix_count)
5219 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5220 if (!hotkeyprefix_offsets)
5222 heap_free(stringdup);
5223 return OutOfMemory;
5227 hotkeyprefix_count = 0;
5229 for(i = 0, j = 0; i < length; i++){
5231 /* FIXME: tabs should be handled using tabstops from stringformat */
5232 if (string[i] == '\t')
5233 continue;
5235 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5236 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5237 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5239 seen_prefix = TRUE;
5240 continue;
5243 seen_prefix = FALSE;
5245 stringdup[j] = string[i];
5246 j++;
5249 length = j;
5251 halign = format->align;
5253 while(sum < length){
5254 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5255 nwidth, &fit, NULL, &size);
5256 fitcpy = fit;
5258 if(fit == 0)
5259 break;
5261 for(lret = 0; lret < fit; lret++)
5262 if(*(stringdup + sum + lret) == '\n')
5263 break;
5265 /* Line break code (may look strange, but it imitates windows). */
5266 if(lret < fit)
5267 lineend = fit = lret; /* this is not an off-by-one error */
5268 else if(fit < (length - sum)){
5269 if(*(stringdup + sum + fit) == ' ')
5270 while(*(stringdup + sum + fit) == ' ')
5271 fit++;
5272 else if (!(format->attr & StringFormatFlagsNoWrap))
5273 while(*(stringdup + sum + fit - 1) != ' '){
5274 fit--;
5276 if(*(stringdup + sum + fit) == '\t')
5277 break;
5279 if(fit == 0){
5280 fit = fitcpy;
5281 break;
5284 lineend = fit;
5285 while(*(stringdup + sum + lineend - 1) == ' ' ||
5286 *(stringdup + sum + lineend - 1) == '\t')
5287 lineend--;
5289 else
5290 lineend = fit;
5292 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5293 nwidth, &j, NULL, &size);
5295 bounds.Width = size.cx;
5297 if(height + size.cy > nheight)
5299 if (format->attr & StringFormatFlagsLineLimit)
5300 break;
5301 bounds.Height = nheight - (height + size.cy);
5303 else
5304 bounds.Height = size.cy;
5306 bounds.Y = rect->Y + height;
5308 switch (halign)
5310 case StringAlignmentNear:
5311 default:
5312 bounds.X = rect->X;
5313 break;
5314 case StringAlignmentCenter:
5315 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5316 break;
5317 case StringAlignmentFar:
5318 bounds.X = rect->X + rect->Width - bounds.Width;
5319 break;
5322 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5323 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5324 break;
5326 stat = callback(hdc, stringdup, sum, lineend,
5327 font, rect, format, lineno, &bounds,
5328 &hotkeyprefix_offsets[hotkeyprefix_pos],
5329 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5331 if (stat != Ok)
5332 break;
5334 sum += fit + (lret < fitcpy ? 1 : 0);
5335 height += size.cy;
5336 lineno++;
5338 hotkeyprefix_pos = hotkeyprefix_end_pos;
5340 if(height > nheight)
5341 break;
5343 /* Stop if this was a linewrap (but not if it was a linebreak). */
5344 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5345 break;
5348 heap_free(stringdup);
5349 heap_free(hotkeyprefix_offsets);
5351 return stat;
5354 struct measure_ranges_args {
5355 GpRegion **regions;
5356 REAL rel_width, rel_height;
5359 static GpStatus measure_ranges_callback(HDC hdc,
5360 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5361 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5362 INT lineno, const RectF *bounds, INT *underlined_indexes,
5363 INT underlined_index_count, void *user_data)
5365 int i;
5366 GpStatus stat = Ok;
5367 struct measure_ranges_args *args = user_data;
5369 for (i=0; i<format->range_count; i++)
5371 INT range_start = max(index, format->character_ranges[i].First);
5372 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5373 if (range_start < range_end)
5375 GpRectF range_rect;
5376 SIZE range_size;
5378 range_rect.Y = bounds->Y / args->rel_height;
5379 range_rect.Height = bounds->Height / args->rel_height;
5381 GetTextExtentExPointW(hdc, string + index, range_start - index,
5382 INT_MAX, NULL, NULL, &range_size);
5383 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5385 GetTextExtentExPointW(hdc, string + index, range_end - index,
5386 INT_MAX, NULL, NULL, &range_size);
5387 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5389 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5390 if (stat != Ok)
5391 break;
5395 return stat;
5398 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5399 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5400 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5401 INT regionCount, GpRegion** regions)
5403 GpStatus stat;
5404 int i;
5405 HFONT gdifont, oldfont;
5406 struct measure_ranges_args args;
5407 HDC hdc, temp_hdc=NULL;
5408 GpPointF pt[3];
5409 RectF scaled_rect;
5410 REAL margin_x;
5412 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_wn(string, length),
5413 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5415 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5416 return InvalidParameter;
5418 if (regionCount < stringFormat->range_count)
5419 return InvalidParameter;
5421 if(!graphics->hdc)
5423 hdc = temp_hdc = CreateCompatibleDC(0);
5424 if (!temp_hdc) return OutOfMemory;
5426 else
5427 hdc = graphics->hdc;
5429 if (stringFormat->attr)
5430 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5432 pt[0].X = 0.0;
5433 pt[0].Y = 0.0;
5434 pt[1].X = 1.0;
5435 pt[1].Y = 0.0;
5436 pt[2].X = 0.0;
5437 pt[2].Y = 1.0;
5438 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5439 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5440 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5441 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5442 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5444 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5445 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5447 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5448 scaled_rect.Y = layoutRect->Y * args.rel_height;
5449 scaled_rect.Width = layoutRect->Width * args.rel_width;
5450 scaled_rect.Height = layoutRect->Height * args.rel_height;
5452 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5453 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5455 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL, NULL);
5456 oldfont = SelectObject(hdc, gdifont);
5458 for (i=0; i<stringFormat->range_count; i++)
5460 stat = GdipSetEmpty(regions[i]);
5461 if (stat != Ok)
5463 SelectObject(hdc, oldfont);
5464 DeleteObject(gdifont);
5465 if (temp_hdc)
5466 DeleteDC(temp_hdc);
5467 return stat;
5471 args.regions = regions;
5473 gdi_transform_acquire(graphics);
5475 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5476 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5478 gdi_transform_release(graphics);
5480 SelectObject(hdc, oldfont);
5481 DeleteObject(gdifont);
5483 if (temp_hdc)
5484 DeleteDC(temp_hdc);
5486 return stat;
5489 struct measure_string_args {
5490 RectF *bounds;
5491 INT *codepointsfitted;
5492 INT *linesfilled;
5493 REAL rel_width, rel_height;
5496 static GpStatus measure_string_callback(HDC hdc,
5497 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5498 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5499 INT lineno, const RectF *bounds, INT *underlined_indexes,
5500 INT underlined_index_count, void *user_data)
5502 struct measure_string_args *args = user_data;
5503 REAL new_width, new_height;
5505 new_width = bounds->Width / args->rel_width;
5506 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5508 if (new_width > args->bounds->Width)
5509 args->bounds->Width = new_width;
5511 if (new_height > args->bounds->Height)
5512 args->bounds->Height = new_height;
5514 if (args->codepointsfitted)
5515 *args->codepointsfitted = index + length;
5517 if (args->linesfilled)
5518 (*args->linesfilled)++;
5520 return Ok;
5523 /* Find the smallest rectangle that bounds the text when it is printed in rect
5524 * according to the format options listed in format. If rect has 0 width and
5525 * height, then just find the smallest rectangle that bounds the text when it's
5526 * printed at location (rect->X, rect-Y). */
5527 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5528 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5529 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5530 INT *codepointsfitted, INT *linesfilled)
5532 HFONT oldfont, gdifont;
5533 struct measure_string_args args;
5534 HDC temp_hdc=NULL, hdc;
5535 GpPointF pt[3];
5536 RectF scaled_rect;
5537 REAL margin_x;
5538 INT lines, glyphs;
5540 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5541 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5542 bounds, codepointsfitted, linesfilled);
5544 if(!graphics || !string || !font || !rect || !bounds)
5545 return InvalidParameter;
5547 if(!graphics->hdc)
5549 hdc = temp_hdc = CreateCompatibleDC(0);
5550 if (!temp_hdc) return OutOfMemory;
5552 else
5553 hdc = graphics->hdc;
5555 if(linesfilled) *linesfilled = 0;
5556 if(codepointsfitted) *codepointsfitted = 0;
5558 if(format)
5559 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5561 pt[0].X = 0.0;
5562 pt[0].Y = 0.0;
5563 pt[1].X = 1.0;
5564 pt[1].Y = 0.0;
5565 pt[2].X = 0.0;
5566 pt[2].Y = 1.0;
5567 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5568 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5569 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5570 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5571 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5573 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5574 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5576 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5577 scaled_rect.Y = rect->Y * args.rel_height;
5578 scaled_rect.Width = rect->Width * args.rel_width;
5579 scaled_rect.Height = rect->Height * args.rel_height;
5580 if (scaled_rect.Width >= 0.5)
5582 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5583 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5586 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5587 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5589 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5590 oldfont = SelectObject(hdc, gdifont);
5592 set_rect(bounds, rect->X, rect->Y, 0.0f, 0.0f);
5594 args.bounds = bounds;
5595 args.codepointsfitted = &glyphs;
5596 args.linesfilled = &lines;
5597 lines = glyphs = 0;
5599 gdi_transform_acquire(graphics);
5601 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5602 measure_string_callback, &args);
5604 gdi_transform_release(graphics);
5606 if (linesfilled) *linesfilled = lines;
5607 if (codepointsfitted) *codepointsfitted = glyphs;
5609 if (lines)
5610 bounds->Width += margin_x * 2.0;
5612 SelectObject(hdc, oldfont);
5613 DeleteObject(gdifont);
5615 if (temp_hdc)
5616 DeleteDC(temp_hdc);
5618 return Ok;
5621 struct draw_string_args {
5622 GpGraphics *graphics;
5623 GDIPCONST GpBrush *brush;
5624 REAL x, y, rel_width, rel_height, ascent;
5627 static GpStatus draw_string_callback(HDC hdc,
5628 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5629 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5630 INT lineno, const RectF *bounds, INT *underlined_indexes,
5631 INT underlined_index_count, void *user_data)
5633 struct draw_string_args *args = user_data;
5634 PointF position;
5635 GpStatus stat;
5637 position.X = args->x + bounds->X / args->rel_width;
5638 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5640 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5641 args->brush, &position,
5642 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5644 if (stat == Ok && underlined_index_count)
5646 OUTLINETEXTMETRICW otm;
5647 REAL underline_y, underline_height;
5648 int i;
5650 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5652 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5653 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5655 for (i=0; i<underlined_index_count; i++)
5657 REAL start_x, end_x;
5658 SIZE text_size;
5659 INT ofs = underlined_indexes[i] - index;
5661 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5662 start_x = text_size.cx / args->rel_width;
5664 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5665 end_x = text_size.cx / args->rel_width;
5667 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5671 return stat;
5674 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5675 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5676 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5678 HRGN rgn = NULL;
5679 HFONT gdifont;
5680 GpPointF pt[3], rectcpy[4];
5681 POINT corners[4];
5682 REAL rel_width, rel_height, margin_x;
5683 INT save_state, format_flags = 0;
5684 REAL offsety = 0.0;
5685 struct draw_string_args args;
5686 RectF scaled_rect;
5687 HDC hdc, temp_hdc=NULL;
5688 TEXTMETRICW textmetric;
5690 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5691 length, font, debugstr_rectf(rect), format, brush);
5693 if(!graphics || !string || !font || !brush || !rect)
5694 return InvalidParameter;
5696 if(graphics->hdc)
5698 hdc = graphics->hdc;
5700 else
5702 hdc = temp_hdc = CreateCompatibleDC(0);
5705 if(format){
5706 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5708 format_flags = format->attr;
5710 /* Should be no need to explicitly test for StringAlignmentNear as
5711 * that is default behavior if no alignment is passed. */
5712 if(format->line_align != StringAlignmentNear){
5713 RectF bounds, in_rect = *rect;
5714 in_rect.Height = 0.0; /* avoid height clipping */
5715 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5717 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5719 if(format->line_align == StringAlignmentCenter)
5720 offsety = (rect->Height - bounds.Height) / 2;
5721 else if(format->line_align == StringAlignmentFar)
5722 offsety = (rect->Height - bounds.Height);
5724 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5727 save_state = SaveDC(hdc);
5729 pt[0].X = 0.0;
5730 pt[0].Y = 0.0;
5731 pt[1].X = 1.0;
5732 pt[1].Y = 0.0;
5733 pt[2].X = 0.0;
5734 pt[2].Y = 1.0;
5735 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5736 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5737 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5738 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5739 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5741 rectcpy[3].X = rectcpy[0].X = rect->X;
5742 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5743 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5744 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5745 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5746 round_points(corners, rectcpy, 4);
5748 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5749 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5751 scaled_rect.X = margin_x * rel_width;
5752 scaled_rect.Y = 0.0;
5753 scaled_rect.Width = rel_width * rect->Width;
5754 scaled_rect.Height = rel_height * rect->Height;
5755 if (scaled_rect.Width >= 0.5)
5757 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5758 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5761 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5762 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5764 if (!(format_flags & StringFormatFlagsNoClip) &&
5765 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5766 rect->Width > 0.0 && rect->Height > 0.0)
5768 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5769 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5770 SelectClipRgn(hdc, rgn);
5773 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5774 SelectObject(hdc, gdifont);
5776 args.graphics = graphics;
5777 args.brush = brush;
5779 args.x = rect->X;
5780 args.y = rect->Y + offsety;
5782 args.rel_width = rel_width;
5783 args.rel_height = rel_height;
5785 gdi_transform_acquire(graphics);
5787 GetTextMetricsW(hdc, &textmetric);
5788 args.ascent = textmetric.tmAscent / rel_height;
5790 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5791 draw_string_callback, &args);
5793 gdi_transform_release(graphics);
5795 DeleteObject(rgn);
5796 DeleteObject(gdifont);
5798 RestoreDC(hdc, save_state);
5800 DeleteDC(temp_hdc);
5802 return Ok;
5805 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5807 GpStatus stat;
5809 TRACE("(%p)\n", graphics);
5811 if(!graphics)
5812 return InvalidParameter;
5814 if(graphics->busy)
5815 return ObjectBusy;
5817 if (is_metafile_graphics(graphics))
5819 stat = METAFILE_ResetClip((GpMetafile *)graphics->image);
5820 if (stat != Ok)
5821 return stat;
5824 return GdipSetInfinite(graphics->clip);
5827 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5829 GpStatus stat;
5831 TRACE("(%p)\n", graphics);
5833 if(!graphics)
5834 return InvalidParameter;
5836 if(graphics->busy)
5837 return ObjectBusy;
5839 if (is_metafile_graphics(graphics))
5841 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5843 if (stat != Ok)
5844 return stat;
5847 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5850 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5851 GpMatrixOrder order)
5853 GpStatus stat;
5855 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5857 if(!graphics)
5858 return InvalidParameter;
5860 if(graphics->busy)
5861 return ObjectBusy;
5863 if (is_metafile_graphics(graphics))
5865 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5867 if (stat != Ok)
5868 return stat;
5871 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5874 static GpStatus begin_container(GpGraphics *graphics,
5875 GraphicsContainerType type, GraphicsContainer *state)
5877 GraphicsContainerItem *container;
5878 GpStatus sts;
5880 if(!graphics || !state)
5881 return InvalidParameter;
5883 sts = init_container(&container, graphics, type);
5884 if(sts != Ok)
5885 return sts;
5887 list_add_head(&graphics->containers, &container->entry);
5888 *state = graphics->contid = container->contid;
5890 if (is_metafile_graphics(graphics)) {
5891 if (type == BEGIN_CONTAINER)
5892 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5893 else
5894 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5897 return Ok;
5900 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5902 TRACE("(%p, %p)\n", graphics, state);
5903 return begin_container(graphics, SAVE_GRAPHICS, state);
5906 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5907 GraphicsContainer *state)
5909 TRACE("(%p, %p)\n", graphics, state);
5910 return begin_container(graphics, BEGIN_CONTAINER, state);
5913 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5915 GraphicsContainerItem *container;
5916 GpMatrix transform;
5917 GpStatus stat;
5918 GpRectF scaled_srcrect;
5919 REAL scale_x, scale_y;
5921 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5923 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5924 return InvalidParameter;
5926 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5927 if(stat != Ok)
5928 return stat;
5930 list_add_head(&graphics->containers, &container->entry);
5931 *state = graphics->contid = container->contid;
5933 scale_x = units_to_pixels(1.0, unit, graphics->xres, graphics->printer_display);
5934 scale_y = units_to_pixels(1.0, unit, graphics->yres, graphics->printer_display);
5936 scaled_srcrect.X = scale_x * srcrect->X;
5937 scaled_srcrect.Y = scale_y * srcrect->Y;
5938 scaled_srcrect.Width = scale_x * srcrect->Width;
5939 scaled_srcrect.Height = scale_y * srcrect->Height;
5941 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5942 transform.matrix[1] = 0.0;
5943 transform.matrix[2] = 0.0;
5944 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5945 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5946 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5948 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5950 if (is_metafile_graphics(graphics))
5951 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5953 return Ok;
5956 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5958 GpRectF dstrectf, srcrectf;
5960 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5962 if (!dstrect || !srcrect)
5963 return InvalidParameter;
5965 dstrectf.X = dstrect->X;
5966 dstrectf.Y = dstrect->Y;
5967 dstrectf.Width = dstrect->Width;
5968 dstrectf.Height = dstrect->Height;
5970 srcrectf.X = srcrect->X;
5971 srcrectf.Y = srcrect->Y;
5972 srcrectf.Width = srcrect->Width;
5973 srcrectf.Height = srcrect->Height;
5975 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5978 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5980 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5981 return NotImplemented;
5984 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5985 GraphicsContainer state)
5987 GpStatus sts;
5988 GraphicsContainerItem *container, *container2;
5990 if(!graphics)
5991 return InvalidParameter;
5993 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5994 if(container->contid == state && container->type == type)
5995 break;
5998 /* did not find a matching container */
5999 if(&container->entry == &graphics->containers)
6000 return Ok;
6002 sts = restore_container(graphics, container);
6003 if(sts != Ok)
6004 return sts;
6006 /* remove all of the containers on top of the found container */
6007 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
6008 if(container->contid == state)
6009 break;
6010 list_remove(&container->entry);
6011 delete_container(container);
6014 list_remove(&container->entry);
6015 delete_container(container);
6017 if (is_metafile_graphics(graphics)) {
6018 if (type == BEGIN_CONTAINER)
6019 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
6020 else
6021 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
6024 return Ok;
6027 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
6029 TRACE("(%p, %x)\n", graphics, state);
6030 return end_container(graphics, BEGIN_CONTAINER, state);
6033 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
6035 TRACE("(%p, %x)\n", graphics, state);
6036 return end_container(graphics, SAVE_GRAPHICS, state);
6039 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
6040 REAL sy, GpMatrixOrder order)
6042 GpStatus stat;
6044 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
6046 if(!graphics)
6047 return InvalidParameter;
6049 if(graphics->busy)
6050 return ObjectBusy;
6052 if (is_metafile_graphics(graphics)) {
6053 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
6055 if (stat != Ok)
6056 return stat;
6059 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
6062 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
6063 CombineMode mode)
6065 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
6067 if(!graphics || !srcgraphics)
6068 return InvalidParameter;
6070 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
6073 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
6074 CompositingMode mode)
6076 TRACE("(%p, %d)\n", graphics, mode);
6078 if(!graphics)
6079 return InvalidParameter;
6081 if(graphics->busy)
6082 return ObjectBusy;
6084 if(graphics->compmode == mode)
6085 return Ok;
6087 if (is_metafile_graphics(graphics))
6089 GpStatus stat;
6091 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6092 EmfPlusRecordTypeSetCompositingMode, mode);
6093 if(stat != Ok)
6094 return stat;
6097 graphics->compmode = mode;
6099 return Ok;
6102 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
6103 CompositingQuality quality)
6105 TRACE("(%p, %d)\n", graphics, quality);
6107 if(!graphics)
6108 return InvalidParameter;
6110 if(graphics->busy)
6111 return ObjectBusy;
6113 if(graphics->compqual == quality)
6114 return Ok;
6116 if (is_metafile_graphics(graphics))
6118 GpStatus stat;
6120 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6121 EmfPlusRecordTypeSetCompositingQuality, quality);
6122 if(stat != Ok)
6123 return stat;
6126 graphics->compqual = quality;
6128 return Ok;
6131 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
6132 InterpolationMode mode)
6134 TRACE("(%p, %d)\n", graphics, mode);
6136 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
6137 return InvalidParameter;
6139 if(graphics->busy)
6140 return ObjectBusy;
6142 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
6143 mode = InterpolationModeBilinear;
6145 if (mode == InterpolationModeHighQuality)
6146 mode = InterpolationModeHighQualityBicubic;
6148 if (mode == graphics->interpolation)
6149 return Ok;
6151 if (is_metafile_graphics(graphics))
6153 GpStatus stat;
6155 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6156 EmfPlusRecordTypeSetInterpolationMode, mode);
6157 if (stat != Ok)
6158 return stat;
6161 graphics->interpolation = mode;
6163 return Ok;
6166 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6168 GpStatus stat;
6170 TRACE("(%p, %.2f)\n", graphics, scale);
6172 if(!graphics || (scale <= 0.0))
6173 return InvalidParameter;
6175 if(graphics->busy)
6176 return ObjectBusy;
6178 if (is_metafile_graphics(graphics))
6180 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6181 if (stat != Ok)
6182 return stat;
6185 graphics->scale = scale;
6187 return Ok;
6190 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6192 GpStatus stat;
6194 TRACE("(%p, %d)\n", graphics, unit);
6196 if(!graphics)
6197 return InvalidParameter;
6199 if(graphics->busy)
6200 return ObjectBusy;
6202 if(unit == UnitWorld)
6203 return InvalidParameter;
6205 if (is_metafile_graphics(graphics))
6207 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6208 if (stat != Ok)
6209 return stat;
6212 graphics->unit = unit;
6214 return Ok;
6217 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6218 mode)
6220 TRACE("(%p, %d)\n", graphics, mode);
6222 if(!graphics)
6223 return InvalidParameter;
6225 if(graphics->busy)
6226 return ObjectBusy;
6228 if(graphics->pixeloffset == mode)
6229 return Ok;
6231 if (is_metafile_graphics(graphics))
6233 GpStatus stat;
6235 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6236 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6237 if(stat != Ok)
6238 return stat;
6241 graphics->pixeloffset = mode;
6243 return Ok;
6246 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6248 GpStatus stat;
6250 TRACE("(%p,%i,%i)\n", graphics, x, y);
6252 if (!graphics)
6253 return InvalidParameter;
6255 if (graphics->origin_x == x && graphics->origin_y == y)
6256 return Ok;
6258 if (is_metafile_graphics(graphics))
6260 stat = METAFILE_SetRenderingOrigin((GpMetafile *)graphics->image, x, y);
6261 if (stat != Ok)
6262 return stat;
6265 graphics->origin_x = x;
6266 graphics->origin_y = y;
6268 return Ok;
6271 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6273 TRACE("(%p,%p,%p)\n", graphics, x, y);
6275 if (!graphics || !x || !y)
6276 return InvalidParameter;
6278 *x = graphics->origin_x;
6279 *y = graphics->origin_y;
6281 return Ok;
6284 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6286 TRACE("(%p, %d)\n", graphics, mode);
6288 if(!graphics)
6289 return InvalidParameter;
6291 if(graphics->busy)
6292 return ObjectBusy;
6294 if(graphics->smoothing == mode)
6295 return Ok;
6297 if (is_metafile_graphics(graphics))
6299 GpStatus stat;
6300 BOOL antialias = (mode != SmoothingModeDefault &&
6301 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6303 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6304 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6305 if(stat != Ok)
6306 return stat;
6309 graphics->smoothing = mode;
6311 return Ok;
6314 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6316 TRACE("(%p, %d)\n", graphics, contrast);
6318 if(!graphics)
6319 return InvalidParameter;
6321 graphics->textcontrast = contrast;
6323 return Ok;
6326 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6327 TextRenderingHint hint)
6329 TRACE("(%p, %d)\n", graphics, hint);
6331 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6332 return InvalidParameter;
6334 if(graphics->busy)
6335 return ObjectBusy;
6337 if(graphics->texthint == hint)
6338 return Ok;
6340 if (is_metafile_graphics(graphics)) {
6341 GpStatus stat;
6343 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6344 EmfPlusRecordTypeSetTextRenderingHint, hint);
6345 if(stat != Ok)
6346 return stat;
6349 graphics->texthint = hint;
6351 return Ok;
6354 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6356 GpStatus stat;
6358 TRACE("(%p, %p)\n", graphics, matrix);
6360 if(!graphics || !matrix)
6361 return InvalidParameter;
6363 if(graphics->busy)
6364 return ObjectBusy;
6366 TRACE("%f,%f,%f,%f,%f,%f\n",
6367 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6368 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6370 if (is_metafile_graphics(graphics)) {
6371 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6373 if (stat != Ok)
6374 return stat;
6377 graphics->worldtrans = *matrix;
6379 return Ok;
6382 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6383 REAL dy, GpMatrixOrder order)
6385 GpStatus stat;
6387 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6389 if(!graphics)
6390 return InvalidParameter;
6392 if(graphics->busy)
6393 return ObjectBusy;
6395 if (is_metafile_graphics(graphics)) {
6396 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6398 if (stat != Ok)
6399 return stat;
6402 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6405 /*****************************************************************************
6406 * GdipSetClipHrgn [GDIPLUS.@]
6408 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6410 GpRegion *region;
6411 GpStatus status;
6412 GpMatrix transform;
6414 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6416 if(!graphics)
6417 return InvalidParameter;
6419 if(graphics->busy)
6420 return ObjectBusy;
6422 /* hrgn is in gdi32 device units */
6423 status = GdipCreateRegionHrgn(hrgn, &region);
6425 if (status == Ok)
6427 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6429 if (status == Ok)
6430 status = GdipTransformRegion(region, &transform);
6432 if (status == Ok)
6433 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6435 GdipDeleteRegion(region);
6437 return status;
6440 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6442 GpStatus status;
6443 GpPath *clip_path;
6445 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6447 if(!graphics)
6448 return InvalidParameter;
6450 if(graphics->busy)
6451 return ObjectBusy;
6453 if (is_metafile_graphics(graphics))
6455 status = METAFILE_SetClipPath((GpMetafile*)graphics->image, path, mode);
6456 if (status != Ok)
6457 return status;
6460 status = GdipClonePath(path, &clip_path);
6461 if (status == Ok)
6463 GpMatrix world_to_device;
6465 get_graphics_transform(graphics, CoordinateSpaceDevice,
6466 CoordinateSpaceWorld, &world_to_device);
6467 status = GdipTransformPath(clip_path, &world_to_device);
6468 if (status == Ok)
6469 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6471 GdipDeletePath(clip_path);
6473 return status;
6476 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6477 REAL width, REAL height,
6478 CombineMode mode)
6480 GpStatus status;
6481 GpRectF rect;
6482 GpRegion *region;
6484 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6486 if(!graphics)
6487 return InvalidParameter;
6489 if(graphics->busy)
6490 return ObjectBusy;
6492 if (is_metafile_graphics(graphics))
6494 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6495 if (status != Ok)
6496 return status;
6499 set_rect(&rect, x, y, width, height);
6500 status = GdipCreateRegionRect(&rect, &region);
6501 if (status == Ok)
6503 GpMatrix world_to_device;
6504 BOOL identity;
6506 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6507 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6508 if (status == Ok && !identity)
6509 status = GdipTransformRegion(region, &world_to_device);
6510 if (status == Ok)
6511 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6513 GdipDeleteRegion(region);
6515 return status;
6518 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6519 INT width, INT height,
6520 CombineMode mode)
6522 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6524 if(!graphics)
6525 return InvalidParameter;
6527 if(graphics->busy)
6528 return ObjectBusy;
6530 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6533 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6534 CombineMode mode)
6536 GpStatus status;
6537 GpRegion *clip;
6539 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6541 if(!graphics || !region)
6542 return InvalidParameter;
6544 if(graphics->busy)
6545 return ObjectBusy;
6547 if (is_metafile_graphics(graphics))
6549 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6550 if (status != Ok)
6551 return status;
6554 status = GdipCloneRegion(region, &clip);
6555 if (status == Ok)
6557 GpMatrix world_to_device;
6558 BOOL identity;
6560 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6561 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6562 if (status == Ok && !identity)
6563 status = GdipTransformRegion(clip, &world_to_device);
6564 if (status == Ok)
6565 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6567 GdipDeleteRegion(clip);
6569 return status;
6572 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6573 INT count)
6575 GpStatus status;
6576 GpPath* path;
6578 TRACE("(%p, %p, %d)\n", graphics, points, count);
6580 if(!graphics || !pen || count<=0)
6581 return InvalidParameter;
6583 if(graphics->busy)
6584 return ObjectBusy;
6586 status = GdipCreatePath(FillModeAlternate, &path);
6587 if (status != Ok) return status;
6589 status = GdipAddPathPolygon(path, points, count);
6590 if (status == Ok)
6591 status = GdipDrawPath(graphics, pen, path);
6593 GdipDeletePath(path);
6595 return status;
6598 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6599 INT count)
6601 GpStatus ret;
6602 GpPointF *ptf;
6603 INT i;
6605 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6607 if(count<=0) return InvalidParameter;
6608 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6609 if (!ptf) return OutOfMemory;
6611 for(i = 0;i < count; i++){
6612 ptf[i].X = (REAL)points[i].X;
6613 ptf[i].Y = (REAL)points[i].Y;
6616 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6617 heap_free(ptf);
6619 return ret;
6622 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6624 TRACE("(%p, %p)\n", graphics, dpi);
6626 if(!graphics || !dpi)
6627 return InvalidParameter;
6629 if(graphics->busy)
6630 return ObjectBusy;
6632 *dpi = graphics->xres;
6633 return Ok;
6636 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6638 TRACE("(%p, %p)\n", graphics, dpi);
6640 if(!graphics || !dpi)
6641 return InvalidParameter;
6643 if(graphics->busy)
6644 return ObjectBusy;
6646 *dpi = graphics->yres;
6647 return Ok;
6650 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6651 GpMatrixOrder order)
6653 GpMatrix m;
6654 GpStatus ret;
6656 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6658 if(!graphics || !matrix)
6659 return InvalidParameter;
6661 if(graphics->busy)
6662 return ObjectBusy;
6664 if (is_metafile_graphics(graphics))
6666 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6668 if (ret != Ok)
6669 return ret;
6672 m = graphics->worldtrans;
6674 ret = GdipMultiplyMatrix(&m, matrix, order);
6675 if(ret == Ok)
6676 graphics->worldtrans = m;
6678 return ret;
6681 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6682 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6684 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6686 GpStatus stat=Ok;
6688 TRACE("(%p, %p)\n", graphics, hdc);
6690 if(!graphics || !hdc)
6691 return InvalidParameter;
6693 if(graphics->busy)
6694 return ObjectBusy;
6696 if (is_metafile_graphics(graphics))
6698 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6700 else if (!graphics->hdc ||
6701 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6703 /* Create a fake HDC and fill it with a constant color. */
6704 HDC temp_hdc;
6705 HBITMAP hbitmap;
6706 GpRectF bounds;
6707 BITMAPINFOHEADER bmih;
6708 int i;
6710 stat = get_graphics_bounds(graphics, &bounds);
6711 if (stat != Ok)
6712 return stat;
6714 graphics->temp_hbitmap_width = bounds.Width;
6715 graphics->temp_hbitmap_height = bounds.Height;
6717 bmih.biSize = sizeof(bmih);
6718 bmih.biWidth = graphics->temp_hbitmap_width;
6719 bmih.biHeight = -graphics->temp_hbitmap_height;
6720 bmih.biPlanes = 1;
6721 bmih.biBitCount = 32;
6722 bmih.biCompression = BI_RGB;
6723 bmih.biSizeImage = 0;
6724 bmih.biXPelsPerMeter = 0;
6725 bmih.biYPelsPerMeter = 0;
6726 bmih.biClrUsed = 0;
6727 bmih.biClrImportant = 0;
6729 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6730 (void**)&graphics->temp_bits, NULL, 0);
6731 if (!hbitmap)
6732 return GenericError;
6734 if (!graphics->temp_hdc)
6736 temp_hdc = CreateCompatibleDC(0);
6738 else
6740 temp_hdc = graphics->temp_hdc;
6743 if (!temp_hdc)
6745 DeleteObject(hbitmap);
6746 return GenericError;
6749 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6750 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6752 SelectObject(temp_hdc, hbitmap);
6754 graphics->temp_hbitmap = hbitmap;
6755 *hdc = graphics->temp_hdc = temp_hdc;
6757 else
6759 *hdc = graphics->hdc;
6762 if (stat == Ok)
6763 graphics->busy = TRUE;
6765 return stat;
6768 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6770 GpStatus stat=Ok;
6772 TRACE("(%p, %p)\n", graphics, hdc);
6774 if(!graphics || !hdc || !graphics->busy)
6775 return InvalidParameter;
6777 if (is_metafile_graphics(graphics))
6779 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6781 else if (graphics->temp_hdc == hdc)
6783 DWORD* pos;
6784 int i;
6786 /* Find the pixels that have changed, and mark them as opaque. */
6787 pos = (DWORD*)graphics->temp_bits;
6788 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6790 if (*pos != DC_BACKGROUND_KEY)
6792 *pos |= 0xff000000;
6794 pos++;
6797 /* Write the changed pixels to the real target. */
6798 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6799 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6800 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6802 /* Clean up. */
6803 DeleteObject(graphics->temp_hbitmap);
6804 graphics->temp_hbitmap = NULL;
6806 else if (hdc != graphics->hdc)
6808 stat = InvalidParameter;
6811 if (stat == Ok)
6812 graphics->busy = FALSE;
6814 return stat;
6817 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6819 GpRegion *clip;
6820 GpStatus status;
6821 GpMatrix device_to_world;
6823 TRACE("(%p, %p)\n", graphics, region);
6825 if(!graphics || !region)
6826 return InvalidParameter;
6828 if(graphics->busy)
6829 return ObjectBusy;
6831 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6832 return status;
6834 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6835 status = GdipTransformRegion(clip, &device_to_world);
6836 if (status != Ok)
6838 GdipDeleteRegion(clip);
6839 return status;
6842 /* free everything except root node and header */
6843 delete_element(&region->node);
6844 memcpy(region, clip, sizeof(GpRegion));
6845 heap_free(clip);
6847 return Ok;
6850 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6852 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6854 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6855 ModifyWorldTransform(graphics->hdc, NULL, MWT_IDENTITY);
6856 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6857 SetMapMode(graphics->hdc, MM_TEXT);
6858 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6859 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6861 graphics->gdi_transform_acquire_count++;
6862 return Ok;
6865 GpStatus gdi_transform_release(GpGraphics *graphics)
6867 if (graphics->gdi_transform_acquire_count <= 0)
6869 ERR("called without matching gdi_transform_acquire\n");
6870 return GenericError;
6872 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6874 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6876 graphics->gdi_transform_acquire_count--;
6877 return Ok;
6880 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6881 GpCoordinateSpace src_space, GpMatrix *matrix)
6883 GpStatus stat = Ok;
6884 REAL scale_x, scale_y;
6886 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6888 if (dst_space != src_space)
6890 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres, graphics->printer_display);
6891 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres, graphics->printer_display);
6893 if(graphics->unit != UnitDisplay)
6895 scale_x *= graphics->scale;
6896 scale_y *= graphics->scale;
6899 if (dst_space < src_space)
6901 /* transform towards world space */
6902 switch ((int)src_space)
6904 case WineCoordinateSpaceGdiDevice:
6906 GpMatrix gdixform;
6907 gdixform = graphics->gdi_transform;
6908 stat = GdipInvertMatrix(&gdixform);
6909 if (stat != Ok)
6910 break;
6911 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6912 if (dst_space == CoordinateSpaceDevice)
6913 break;
6914 /* else fall-through */
6916 case CoordinateSpaceDevice:
6917 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6918 if (dst_space == CoordinateSpacePage)
6919 break;
6920 /* else fall-through */
6921 case CoordinateSpacePage:
6923 GpMatrix inverted_transform = graphics->worldtrans;
6924 stat = GdipInvertMatrix(&inverted_transform);
6925 if (stat == Ok)
6926 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6927 break;
6931 else
6933 /* transform towards device space */
6934 switch ((int)src_space)
6936 case CoordinateSpaceWorld:
6937 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6938 if (dst_space == CoordinateSpacePage)
6939 break;
6940 /* else fall-through */
6941 case CoordinateSpacePage:
6942 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6943 if (dst_space == CoordinateSpaceDevice)
6944 break;
6945 /* else fall-through */
6946 case CoordinateSpaceDevice:
6948 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6949 break;
6954 return stat;
6957 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6958 GpCoordinateSpace src_space, GpPointF *points, INT count)
6960 GpMatrix matrix;
6961 GpStatus stat;
6963 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6964 if (stat != Ok) return stat;
6966 return GdipTransformMatrixPoints(&matrix, points, count);
6969 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6970 GpCoordinateSpace src_space, GpPointF *points, INT count)
6972 if(!graphics || !points || count <= 0 ||
6973 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6974 src_space < 0 || src_space > CoordinateSpaceDevice)
6975 return InvalidParameter;
6977 if(graphics->busy)
6978 return ObjectBusy;
6980 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6982 if (src_space == dst_space) return Ok;
6984 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6987 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6988 GpCoordinateSpace src_space, GpPoint *points, INT count)
6990 GpPointF *pointsF;
6991 GpStatus ret;
6992 INT i;
6994 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6996 if(count <= 0)
6997 return InvalidParameter;
6999 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
7000 if(!pointsF)
7001 return OutOfMemory;
7003 for(i = 0; i < count; i++){
7004 pointsF[i].X = (REAL)points[i].X;
7005 pointsF[i].Y = (REAL)points[i].Y;
7008 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
7010 if(ret == Ok)
7011 for(i = 0; i < count; i++){
7012 points[i].X = gdip_round(pointsF[i].X);
7013 points[i].Y = gdip_round(pointsF[i].Y);
7015 heap_free(pointsF);
7017 return ret;
7020 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
7022 static int calls;
7024 TRACE("\n");
7026 if (!calls++)
7027 FIXME("stub\n");
7029 return NULL;
7032 /*****************************************************************************
7033 * GdipTranslateClip [GDIPLUS.@]
7035 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
7037 GpStatus stat;
7039 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
7041 if(!graphics)
7042 return InvalidParameter;
7044 if(graphics->busy)
7045 return ObjectBusy;
7047 if (is_metafile_graphics(graphics))
7049 stat = METAFILE_OffsetClip((GpMetafile *)graphics->image, dx, dy);
7050 if (stat != Ok)
7051 return stat;
7054 return GdipTranslateRegion(graphics->clip, dx, dy);
7057 /*****************************************************************************
7058 * GdipTranslateClipI [GDIPLUS.@]
7060 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
7062 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
7064 return GdipTranslateClip(graphics, dx, dy);
7067 /*****************************************************************************
7068 * GdipMeasureDriverString [GDIPLUS.@]
7070 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7071 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
7072 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
7074 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7075 HFONT hfont;
7076 HDC hdc;
7077 REAL min_x, min_y, max_x, max_y, x, y;
7078 int i;
7079 TEXTMETRICW textmetric;
7080 const WORD *glyph_indices;
7081 WORD *dynamic_glyph_indices=NULL;
7082 REAL rel_width, rel_height, ascent, descent;
7083 GpPointF pt[3];
7085 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
7087 if (!graphics || !text || !font || !positions || !boundingBox)
7088 return InvalidParameter;
7090 if (length == -1)
7091 length = lstrlenW(text);
7093 if (length == 0)
7094 set_rect(boundingBox, 0.0f, 0.0f, 0.0f, 0.0f);
7096 if (flags & unsupported_flags)
7097 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7099 get_font_hfont(graphics, font, NULL, &hfont, NULL, matrix);
7101 hdc = CreateCompatibleDC(0);
7102 SelectObject(hdc, hfont);
7104 GetTextMetricsW(hdc, &textmetric);
7106 pt[0].X = 0.0;
7107 pt[0].Y = 0.0;
7108 pt[1].X = 1.0;
7109 pt[1].Y = 0.0;
7110 pt[2].X = 0.0;
7111 pt[2].Y = 1.0;
7112 if (matrix)
7114 GpMatrix xform = *matrix;
7115 GdipTransformMatrixPoints(&xform, pt, 3);
7117 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
7118 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
7119 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
7120 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
7121 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
7123 if (flags & DriverStringOptionsCmapLookup)
7125 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
7126 if (!glyph_indices)
7128 DeleteDC(hdc);
7129 DeleteObject(hfont);
7130 return OutOfMemory;
7133 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
7135 else
7136 glyph_indices = text;
7138 min_x = max_x = x = positions[0].X;
7139 min_y = max_y = y = positions[0].Y;
7141 ascent = textmetric.tmAscent / rel_height;
7142 descent = textmetric.tmDescent / rel_height;
7144 for (i=0; i<length; i++)
7146 int char_width;
7147 ABC abc;
7149 if (!(flags & DriverStringOptionsRealizedAdvance))
7151 x = positions[i].X;
7152 y = positions[i].Y;
7155 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
7156 char_width = abc.abcA + abc.abcB + abc.abcC;
7158 if (min_y > y - ascent) min_y = y - ascent;
7159 if (max_y < y + descent) max_y = y + descent;
7160 if (min_x > x) min_x = x;
7162 x += char_width / rel_width;
7164 if (max_x < x) max_x = x;
7167 heap_free(dynamic_glyph_indices);
7168 DeleteDC(hdc);
7169 DeleteObject(hfont);
7171 boundingBox->X = min_x;
7172 boundingBox->Y = min_y;
7173 boundingBox->Width = max_x - min_x;
7174 boundingBox->Height = max_y - min_y;
7176 return Ok;
7179 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7180 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7181 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7182 INT flags, GDIPCONST GpMatrix *matrix)
7184 INT save_state;
7185 GpPointF pt, *real_positions=NULL;
7186 INT *eto_positions=NULL;
7187 HFONT hfont;
7188 LOGFONTW lfw;
7189 UINT eto_flags=0;
7190 GpStatus status;
7191 HRGN hrgn;
7193 if (!(flags & DriverStringOptionsCmapLookup))
7194 eto_flags |= ETO_GLYPH_INDEX;
7196 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7198 real_positions = heap_alloc(sizeof(*real_positions) * length);
7199 eto_positions = heap_alloc(sizeof(*eto_positions) * 2 * (length - 1));
7200 if (!real_positions || !eto_positions)
7202 heap_free(real_positions);
7203 heap_free(eto_positions);
7204 return OutOfMemory;
7208 save_state = SaveDC(graphics->hdc);
7209 SetBkMode(graphics->hdc, TRANSPARENT);
7210 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7212 status = get_clip_hrgn(graphics, &hrgn);
7214 if (status == Ok)
7216 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7217 DeleteObject(hrgn);
7220 pt = positions[0];
7221 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7223 get_font_hfont(graphics, font, format, &hfont, &lfw, matrix);
7225 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7227 GpMatrix rotation;
7228 INT i;
7230 eto_flags |= ETO_PDY;
7232 memcpy(real_positions, positions, sizeof(PointF) * length);
7234 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7236 GdipSetMatrixElements(&rotation, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
7237 GdipRotateMatrix(&rotation, lfw.lfEscapement / 10.0, MatrixOrderAppend);
7238 GdipTransformMatrixPoints(&rotation, real_positions, length);
7240 for (i = 0; i < (length - 1); i++)
7242 eto_positions[i*2] = gdip_round(real_positions[i+1].X) - gdip_round(real_positions[i].X);
7243 eto_positions[i*2+1] = gdip_round(real_positions[i].Y) - gdip_round(real_positions[i+1].Y);
7247 SelectObject(graphics->hdc, hfont);
7249 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7251 gdi_transform_acquire(graphics);
7253 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, eto_positions);
7255 gdi_transform_release(graphics);
7257 RestoreDC(graphics->hdc, save_state);
7259 DeleteObject(hfont);
7261 heap_free(real_positions);
7262 heap_free(eto_positions);
7264 return Ok;
7267 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7268 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7269 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7270 INT flags, GDIPCONST GpMatrix *matrix)
7272 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7273 GpStatus stat;
7274 PointF *real_positions, real_position;
7275 POINT *pti;
7276 HFONT hfont;
7277 HDC hdc;
7278 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7279 DWORD max_glyphsize=0;
7280 GLYPHMETRICS glyphmetrics;
7281 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7282 BYTE *glyph_mask;
7283 BYTE *text_mask;
7284 int text_mask_stride;
7285 BYTE *pixel_data;
7286 int pixel_data_stride;
7287 GpRect pixel_area;
7288 UINT ggo_flags = GGO_GRAY8_BITMAP;
7290 if (length <= 0)
7291 return Ok;
7293 if (!(flags & DriverStringOptionsCmapLookup))
7294 ggo_flags |= GGO_GLYPH_INDEX;
7296 if (flags & unsupported_flags)
7297 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7299 pti = heap_alloc_zero(sizeof(POINT) * length);
7300 if (!pti)
7301 return OutOfMemory;
7303 if (flags & DriverStringOptionsRealizedAdvance)
7305 real_position = positions[0];
7307 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7308 round_points(pti, &real_position, 1);
7310 else
7312 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7313 if (!real_positions)
7315 heap_free(pti);
7316 return OutOfMemory;
7319 memcpy(real_positions, positions, sizeof(PointF) * length);
7321 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7322 round_points(pti, real_positions, length);
7324 heap_free(real_positions);
7327 get_font_hfont(graphics, font, format, &hfont, NULL, matrix);
7329 hdc = CreateCompatibleDC(0);
7330 SelectObject(hdc, hfont);
7332 /* Get the boundaries of the text to be drawn */
7333 for (i=0; i<length; i++)
7335 DWORD glyphsize;
7336 int left, top, right, bottom;
7338 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7339 &glyphmetrics, 0, NULL, &identity);
7341 if (glyphsize == GDI_ERROR)
7343 ERR("GetGlyphOutlineW failed\n");
7344 heap_free(pti);
7345 DeleteDC(hdc);
7346 DeleteObject(hfont);
7347 return GenericError;
7350 if (glyphsize > max_glyphsize)
7351 max_glyphsize = glyphsize;
7353 if (glyphsize != 0)
7355 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7356 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7357 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7358 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7360 if (left < min_x) min_x = left;
7361 if (top < min_y) min_y = top;
7362 if (right > max_x) max_x = right;
7363 if (bottom > max_y) max_y = bottom;
7366 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7368 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7369 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7373 if (max_glyphsize == 0)
7375 /* Nothing to draw. */
7376 heap_free(pti);
7377 DeleteDC(hdc);
7378 DeleteObject(hfont);
7379 return Ok;
7382 glyph_mask = heap_alloc_zero(max_glyphsize);
7383 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7384 text_mask_stride = max_x - min_x;
7386 if (!(glyph_mask && text_mask))
7388 heap_free(glyph_mask);
7389 heap_free(text_mask);
7390 heap_free(pti);
7391 DeleteDC(hdc);
7392 DeleteObject(hfont);
7393 return OutOfMemory;
7396 /* Generate a mask for the text */
7397 for (i=0; i<length; i++)
7399 DWORD ret;
7400 int left, top, stride;
7402 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7403 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7405 if (ret == GDI_ERROR || ret == 0)
7406 continue; /* empty glyph */
7408 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7409 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7410 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7412 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7414 BYTE *glyph_val = glyph_mask + y * stride;
7415 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7416 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7418 *text_val = min(64, *text_val + *glyph_val);
7419 glyph_val++;
7420 text_val++;
7425 heap_free(pti);
7426 DeleteDC(hdc);
7427 DeleteObject(hfont);
7428 heap_free(glyph_mask);
7430 /* get the brush data */
7431 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7432 if (!pixel_data)
7434 heap_free(text_mask);
7435 return OutOfMemory;
7438 pixel_area.X = min_x;
7439 pixel_area.Y = min_y;
7440 pixel_area.Width = max_x - min_x;
7441 pixel_area.Height = max_y - min_y;
7442 pixel_data_stride = pixel_area.Width * 4;
7444 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7445 if (stat != Ok)
7447 heap_free(text_mask);
7448 heap_free(pixel_data);
7449 return stat;
7452 /* multiply the brush data by the mask */
7453 for (y=0; y<pixel_area.Height; y++)
7455 BYTE *text_val = text_mask + text_mask_stride * y;
7456 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7457 for (x=0; x<pixel_area.Width; x++)
7459 *pixel_val = (*pixel_val) * (*text_val) / 64;
7460 text_val++;
7461 pixel_val+=4;
7465 heap_free(text_mask);
7467 gdi_transform_acquire(graphics);
7469 /* draw the result */
7470 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7471 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7473 gdi_transform_release(graphics);
7475 heap_free(pixel_data);
7477 return stat;
7480 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7481 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7482 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7483 INT flags, GDIPCONST GpMatrix *matrix)
7485 GpStatus stat = NotImplemented;
7487 if (length == -1)
7488 length = lstrlenW(text);
7490 if (is_metafile_graphics(graphics))
7491 return METAFILE_DrawDriverString((GpMetafile*)graphics->image, text, length, font,
7492 format, brush, positions, flags, matrix);
7494 if (graphics->hdc && !graphics->alpha_hdc &&
7495 brush->bt == BrushTypeSolidColor &&
7496 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7497 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7498 brush, positions, flags, matrix);
7499 if (stat == NotImplemented)
7500 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7501 brush, positions, flags, matrix);
7502 return stat;
7505 /*****************************************************************************
7506 * GdipDrawDriverString [GDIPLUS.@]
7508 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7509 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7510 GDIPCONST PointF *positions, INT flags,
7511 GDIPCONST GpMatrix *matrix )
7513 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7515 if (!graphics || !text || !font || !brush || !positions)
7516 return InvalidParameter;
7518 return draw_driver_string(graphics, text, length, font, NULL,
7519 brush, positions, flags, matrix);
7522 /*****************************************************************************
7523 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7525 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7527 GpStatus stat;
7528 GpRegion* rgn;
7530 TRACE("(%p, %p)\n", graphics, res);
7532 if((stat = GdipCreateRegion(&rgn)) != Ok)
7533 return stat;
7535 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7536 goto cleanup;
7538 stat = GdipIsEmptyRegion(rgn, graphics, res);
7540 cleanup:
7541 GdipDeleteRegion(rgn);
7542 return stat;
7545 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7547 static int calls;
7549 TRACE("(%p) stub\n", graphics);
7551 if(!(calls++))
7552 FIXME("not implemented\n");
7554 return NotImplemented;
7557 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7559 TRACE("(%p, %p)\n", graphics, pabort);
7561 if (!graphics)
7562 return InvalidParameter;
7564 if (pabort)
7565 FIXME("Abort callback is not supported.\n");
7567 return Ok;