urlmon: Recognize <body> tag in FindMimeFromData function.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blob7ee46eea6643bea00764b67ae41f1fa2f590b6d4
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
55 GpCoordinateSpace src_space, GpMatrix *matrix);
57 /* Converts from gdiplus path point type to gdi path point type. */
58 static BYTE convert_path_point_type(BYTE type)
60 BYTE ret;
62 switch(type & PathPointTypePathTypeMask){
63 case PathPointTypeBezier:
64 ret = PT_BEZIERTO;
65 break;
66 case PathPointTypeLine:
67 ret = PT_LINETO;
68 break;
69 case PathPointTypeStart:
70 ret = PT_MOVETO;
71 break;
72 default:
73 ERR("Bad point type\n");
74 return 0;
77 if(type & PathPointTypeCloseSubpath)
78 ret |= PT_CLOSEFIGURE;
80 return ret;
83 static COLORREF get_gdi_brush_color(const GpBrush *brush)
85 ARGB argb;
87 switch (brush->bt)
89 case BrushTypeSolidColor:
91 const GpSolidFill *sf = (const GpSolidFill *)brush;
92 argb = sf->color;
93 break;
95 case BrushTypeHatchFill:
97 const GpHatch *hatch = (const GpHatch *)brush;
98 argb = hatch->forecol;
99 break;
101 case BrushTypeLinearGradient:
103 const GpLineGradient *line = (const GpLineGradient *)brush;
104 argb = line->startcolor;
105 break;
107 case BrushTypePathGradient:
109 const GpPathGradient *grad = (const GpPathGradient *)brush;
110 argb = grad->centercolor;
111 break;
113 default:
114 FIXME("unhandled brush type %d\n", brush->bt);
115 argb = 0;
116 break;
118 return ARGB2COLORREF(argb);
121 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
123 HBITMAP hbmp;
124 BITMAPINFOHEADER bmih;
125 DWORD *bits;
126 int x, y;
128 bmih.biSize = sizeof(bmih);
129 bmih.biWidth = 8;
130 bmih.biHeight = 8;
131 bmih.biPlanes = 1;
132 bmih.biBitCount = 32;
133 bmih.biCompression = BI_RGB;
134 bmih.biSizeImage = 0;
136 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
137 if (hbmp)
139 const char *hatch_data;
141 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
143 for (y = 0; y < 8; y++)
145 for (x = 0; x < 8; x++)
147 if (hatch_data[y] & (0x80 >> x))
148 bits[y * 8 + x] = hatch->forecol;
149 else
150 bits[y * 8 + x] = hatch->backcol;
154 else
156 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
158 for (y = 0; y < 64; y++)
159 bits[y] = hatch->forecol;
163 return hbmp;
166 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
168 switch (brush->bt)
170 case BrushTypeSolidColor:
172 const GpSolidFill *sf = (const GpSolidFill *)brush;
173 lb->lbStyle = BS_SOLID;
174 lb->lbColor = ARGB2COLORREF(sf->color);
175 lb->lbHatch = 0;
176 return Ok;
179 case BrushTypeHatchFill:
181 const GpHatch *hatch = (const GpHatch *)brush;
182 HBITMAP hbmp;
184 hbmp = create_hatch_bitmap(hatch);
185 if (!hbmp) return OutOfMemory;
187 lb->lbStyle = BS_PATTERN;
188 lb->lbColor = 0;
189 lb->lbHatch = (ULONG_PTR)hbmp;
190 return Ok;
193 default:
194 FIXME("unhandled brush type %d\n", brush->bt);
195 lb->lbStyle = BS_SOLID;
196 lb->lbColor = get_gdi_brush_color(brush);
197 lb->lbHatch = 0;
198 return Ok;
202 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
204 switch (lb->lbStyle)
206 case BS_PATTERN:
207 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
208 break;
210 return Ok;
213 static HBRUSH create_gdi_brush(const GpBrush *brush)
215 LOGBRUSH lb;
216 HBRUSH gdibrush;
218 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
220 gdibrush = CreateBrushIndirect(&lb);
221 free_gdi_logbrush(&lb);
223 return gdibrush;
226 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
228 LOGBRUSH lb;
229 HPEN gdipen;
230 REAL width;
231 INT save_state, i, numdashes;
232 GpPointF pt[2];
233 DWORD dash_array[MAX_DASHLEN];
235 save_state = SaveDC(graphics->hdc);
237 EndPath(graphics->hdc);
239 if(pen->unit == UnitPixel){
240 width = pen->width;
242 else{
243 /* Get an estimate for the amount the pen width is affected by the world
244 * transform. (This is similar to what some of the wine drivers do.) */
245 pt[0].X = 0.0;
246 pt[0].Y = 0.0;
247 pt[1].X = 1.0;
248 pt[1].Y = 1.0;
249 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
250 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
251 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
253 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
256 if(pen->dash == DashStyleCustom){
257 numdashes = min(pen->numdashes, MAX_DASHLEN);
259 TRACE("dashes are: ");
260 for(i = 0; i < numdashes; i++){
261 dash_array[i] = gdip_round(width * pen->dashes[i]);
262 TRACE("%d, ", dash_array[i]);
264 TRACE("\n and the pen style is %x\n", pen->style);
266 create_gdi_logbrush(pen->brush, &lb);
267 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
268 numdashes, dash_array);
269 free_gdi_logbrush(&lb);
271 else
273 create_gdi_logbrush(pen->brush, &lb);
274 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
275 free_gdi_logbrush(&lb);
278 SelectObject(graphics->hdc, gdipen);
280 return save_state;
283 static void restore_dc(GpGraphics *graphics, INT state)
285 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
286 RestoreDC(graphics->hdc, state);
289 /* This helper applies all the changes that the points listed in ptf need in
290 * order to be drawn on the device context. In the end, this should include at
291 * least:
292 * -scaling by page unit
293 * -applying world transformation
294 * -converting from float to int
295 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
296 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
297 * gdi to draw, and these functions would irreparably mess with line widths.
299 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
300 GpPointF *ptf, INT count)
302 REAL scale_x, scale_y;
303 GpMatrix matrix;
304 int i;
306 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
307 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
309 /* apply page scale */
310 if(graphics->unit != UnitDisplay)
312 scale_x *= graphics->scale;
313 scale_y *= graphics->scale;
316 matrix = graphics->worldtrans;
317 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
318 GdipTransformMatrixPoints(&matrix, ptf, count);
320 for(i = 0; i < count; i++){
321 pti[i].x = gdip_round(ptf[i].X);
322 pti[i].y = gdip_round(ptf[i].Y);
326 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
327 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
329 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
331 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
333 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
334 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
336 else
338 BLENDFUNCTION bf;
340 bf.BlendOp = AC_SRC_OVER;
341 bf.BlendFlags = 0;
342 bf.SourceConstantAlpha = 255;
343 bf.AlphaFormat = AC_SRC_ALPHA;
345 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
346 hdc, src_x, src_y, src_width, src_height, bf);
350 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
352 /* clipping region is in device coords */
353 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
356 /* Draw non-premultiplied ARGB data to the given graphics object */
357 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
358 const BYTE *src, INT src_width, INT src_height, INT src_stride)
360 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
361 INT x, y;
363 for (x=0; x<src_width; x++)
365 for (y=0; y<src_height; y++)
367 ARGB dst_color, src_color;
368 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
369 src_color = ((ARGB*)(src + src_stride * y))[x];
370 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
374 return Ok;
377 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
378 const BYTE *src, INT src_width, INT src_height, INT src_stride)
380 HDC hdc;
381 HBITMAP hbitmap;
382 BITMAPINFOHEADER bih;
383 BYTE *temp_bits;
385 hdc = CreateCompatibleDC(0);
387 bih.biSize = sizeof(BITMAPINFOHEADER);
388 bih.biWidth = src_width;
389 bih.biHeight = -src_height;
390 bih.biPlanes = 1;
391 bih.biBitCount = 32;
392 bih.biCompression = BI_RGB;
393 bih.biSizeImage = 0;
394 bih.biXPelsPerMeter = 0;
395 bih.biYPelsPerMeter = 0;
396 bih.biClrUsed = 0;
397 bih.biClrImportant = 0;
399 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
400 (void**)&temp_bits, NULL, 0);
402 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
403 memcpy(temp_bits, src, src_width * src_height * 4);
404 else
405 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
406 4 * src_width, src, src_stride);
408 SelectObject(hdc, hbitmap);
409 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
410 hdc, 0, 0, src_width, src_height);
411 DeleteDC(hdc);
412 DeleteObject(hbitmap);
414 return Ok;
417 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
418 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
420 GpStatus stat=Ok;
422 if (graphics->image && graphics->image->type == ImageTypeBitmap)
424 DWORD i;
425 int size;
426 RGNDATA *rgndata;
427 RECT *rects;
428 HRGN hrgn, visible_rgn;
430 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
431 if (!hrgn)
432 return OutOfMemory;
434 stat = get_clip_hrgn(graphics, &visible_rgn);
435 if (stat != Ok)
437 DeleteObject(hrgn);
438 return stat;
441 if (visible_rgn)
443 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
444 DeleteObject(visible_rgn);
447 if (hregion)
448 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
450 size = GetRegionData(hrgn, 0, NULL);
452 rgndata = GdipAlloc(size);
453 if (!rgndata)
455 DeleteObject(hrgn);
456 return OutOfMemory;
459 GetRegionData(hrgn, size, rgndata);
461 rects = (RECT*)rgndata->Buffer;
463 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
465 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
466 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
467 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
468 src_stride);
471 GdipFree(rgndata);
473 DeleteObject(hrgn);
475 return stat;
477 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
479 ERR("This should not be used for metafiles; fix caller\n");
480 return NotImplemented;
482 else
484 HRGN hrgn;
485 int save;
487 stat = get_clip_hrgn(graphics, &hrgn);
489 if (stat != Ok)
490 return stat;
492 save = SaveDC(graphics->hdc);
494 if (hrgn)
495 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
497 if (hregion)
498 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
500 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
501 src_height, src_stride);
503 RestoreDC(graphics->hdc, save);
505 DeleteObject(hrgn);
507 return stat;
511 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
512 const BYTE *src, INT src_width, INT src_height, INT src_stride)
514 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL);
517 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
519 ARGB result=0;
520 ARGB i;
521 INT a1, a2, a3;
523 a1 = (start >> 24) & 0xff;
524 a2 = (end >> 24) & 0xff;
526 a3 = (int)(a1*(1.0f - position)+a2*(position));
528 result |= a3 << 24;
530 for (i=0xff; i<=0xff0000; i = i << 8)
531 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
532 return result;
535 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
537 REAL blendfac;
539 /* clamp to between 0.0 and 1.0, using the wrap mode */
540 if (brush->wrap == WrapModeTile)
542 position = fmodf(position, 1.0f);
543 if (position < 0.0f) position += 1.0f;
545 else /* WrapModeFlip* */
547 position = fmodf(position, 2.0f);
548 if (position < 0.0f) position += 2.0f;
549 if (position > 1.0f) position = 2.0f - position;
552 if (brush->blendcount == 1)
553 blendfac = position;
554 else
556 int i=1;
557 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
558 REAL range;
560 /* locate the blend positions surrounding this position */
561 while (position > brush->blendpos[i])
562 i++;
564 /* interpolate between the blend positions */
565 left_blendpos = brush->blendpos[i-1];
566 left_blendfac = brush->blendfac[i-1];
567 right_blendpos = brush->blendpos[i];
568 right_blendfac = brush->blendfac[i];
569 range = right_blendpos - left_blendpos;
570 blendfac = (left_blendfac * (right_blendpos - position) +
571 right_blendfac * (position - left_blendpos)) / range;
574 if (brush->pblendcount == 0)
575 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
576 else
578 int i=1;
579 ARGB left_blendcolor, right_blendcolor;
580 REAL left_blendpos, right_blendpos;
582 /* locate the blend colors surrounding this position */
583 while (blendfac > brush->pblendpos[i])
584 i++;
586 /* interpolate between the blend colors */
587 left_blendpos = brush->pblendpos[i-1];
588 left_blendcolor = brush->pblendcolor[i-1];
589 right_blendpos = brush->pblendpos[i];
590 right_blendcolor = brush->pblendcolor[i];
591 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
592 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
596 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
598 REAL val[5], res[4];
599 int i, j;
600 unsigned char a, r, g, b;
602 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
603 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
604 val[2] = (color & 0xff) / 255.0; /* blue */
605 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
606 val[4] = 1.0; /* translation */
608 for (i=0; i<4; i++)
610 res[i] = 0.0;
612 for (j=0; j<5; j++)
613 res[i] += matrix->m[j][i] * val[j];
616 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
617 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
618 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
619 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
621 return (a << 24) | (r << 16) | (g << 8) | b;
624 static BOOL color_is_gray(ARGB color)
626 unsigned char r, g, b;
628 r = (color >> 16) & 0xff;
629 g = (color >> 8) & 0xff;
630 b = color & 0xff;
632 return (r == g) && (g == b);
635 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
636 UINT width, UINT height, INT stride, ColorAdjustType type)
638 UINT x, y;
639 INT i;
641 if (attributes->colorkeys[type].enabled ||
642 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
644 const struct color_key *key;
645 BYTE min_blue, min_green, min_red;
646 BYTE max_blue, max_green, max_red;
648 if (attributes->colorkeys[type].enabled)
649 key = &attributes->colorkeys[type];
650 else
651 key = &attributes->colorkeys[ColorAdjustTypeDefault];
653 min_blue = key->low&0xff;
654 min_green = (key->low>>8)&0xff;
655 min_red = (key->low>>16)&0xff;
657 max_blue = key->high&0xff;
658 max_green = (key->high>>8)&0xff;
659 max_red = (key->high>>16)&0xff;
661 for (x=0; x<width; x++)
662 for (y=0; y<height; y++)
664 ARGB *src_color;
665 BYTE blue, green, red;
666 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
667 blue = *src_color&0xff;
668 green = (*src_color>>8)&0xff;
669 red = (*src_color>>16)&0xff;
670 if (blue >= min_blue && green >= min_green && red >= min_red &&
671 blue <= max_blue && green <= max_green && red <= max_red)
672 *src_color = 0x00000000;
676 if (attributes->colorremaptables[type].enabled ||
677 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
679 const struct color_remap_table *table;
681 if (attributes->colorremaptables[type].enabled)
682 table = &attributes->colorremaptables[type];
683 else
684 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
686 for (x=0; x<width; x++)
687 for (y=0; y<height; y++)
689 ARGB *src_color;
690 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
691 for (i=0; i<table->mapsize; i++)
693 if (*src_color == table->colormap[i].oldColor.Argb)
695 *src_color = table->colormap[i].newColor.Argb;
696 break;
702 if (attributes->colormatrices[type].enabled ||
703 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
705 const struct color_matrix *colormatrices;
707 if (attributes->colormatrices[type].enabled)
708 colormatrices = &attributes->colormatrices[type];
709 else
710 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
712 for (x=0; x<width; x++)
713 for (y=0; y<height; y++)
715 ARGB *src_color;
716 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
718 if (colormatrices->flags == ColorMatrixFlagsDefault ||
719 !color_is_gray(*src_color))
721 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
723 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
725 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
730 if (attributes->gamma_enabled[type] ||
731 attributes->gamma_enabled[ColorAdjustTypeDefault])
733 REAL gamma;
735 if (attributes->gamma_enabled[type])
736 gamma = attributes->gamma[type];
737 else
738 gamma = attributes->gamma[ColorAdjustTypeDefault];
740 for (x=0; x<width; x++)
741 for (y=0; y<height; y++)
743 ARGB *src_color;
744 BYTE blue, green, red;
745 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
747 blue = *src_color&0xff;
748 green = (*src_color>>8)&0xff;
749 red = (*src_color>>16)&0xff;
751 /* FIXME: We should probably use a table for this. */
752 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
753 green = floorf(powf(green / 255.0, gamma) * 255.0);
754 red = floorf(powf(red / 255.0, gamma) * 255.0);
756 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
761 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
762 * bitmap that contains all the pixels we may need to draw it. */
763 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
764 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
765 GpRect *rect)
767 INT left, top, right, bottom;
769 switch (interpolation)
771 case InterpolationModeHighQualityBilinear:
772 case InterpolationModeHighQualityBicubic:
773 /* FIXME: Include a greater range for the prefilter? */
774 case InterpolationModeBicubic:
775 case InterpolationModeBilinear:
776 left = (INT)(floorf(srcx));
777 top = (INT)(floorf(srcy));
778 right = (INT)(ceilf(srcx+srcwidth));
779 bottom = (INT)(ceilf(srcy+srcheight));
780 break;
781 case InterpolationModeNearestNeighbor:
782 default:
783 left = gdip_round(srcx);
784 top = gdip_round(srcy);
785 right = gdip_round(srcx+srcwidth);
786 bottom = gdip_round(srcy+srcheight);
787 break;
790 if (wrap == WrapModeClamp)
792 if (left < 0)
793 left = 0;
794 if (top < 0)
795 top = 0;
796 if (right >= bitmap->width)
797 right = bitmap->width-1;
798 if (bottom >= bitmap->height)
799 bottom = bitmap->height-1;
801 else
803 /* In some cases we can make the rectangle smaller here, but the logic
804 * is hard to get right, and tiling suggests we're likely to use the
805 * entire source image. */
806 if (left < 0 || right >= bitmap->width)
808 left = 0;
809 right = bitmap->width-1;
812 if (top < 0 || bottom >= bitmap->height)
814 top = 0;
815 bottom = bitmap->height-1;
819 rect->X = left;
820 rect->Y = top;
821 rect->Width = right - left + 1;
822 rect->Height = bottom - top + 1;
825 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
826 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
828 if (attributes->wrap == WrapModeClamp)
830 if (x < 0 || y < 0 || x >= width || y >= height)
831 return attributes->outside_color;
833 else
835 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
836 if (x < 0)
837 x = width*2 + x % (width * 2);
838 if (y < 0)
839 y = height*2 + y % (height * 2);
841 if ((attributes->wrap & 1) == 1)
843 /* Flip X */
844 if ((x / width) % 2 == 0)
845 x = x % width;
846 else
847 x = width - 1 - x % width;
849 else
850 x = x % width;
852 if ((attributes->wrap & 2) == 2)
854 /* Flip Y */
855 if ((y / height) % 2 == 0)
856 y = y % height;
857 else
858 y = height - 1 - y % height;
860 else
861 y = y % height;
864 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
866 ERR("out of range pixel requested\n");
867 return 0xffcd0084;
870 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
873 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
874 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
875 InterpolationMode interpolation, PixelOffsetMode offset_mode)
877 static int fixme;
879 switch (interpolation)
881 default:
882 if (!fixme++)
883 FIXME("Unimplemented interpolation %i\n", interpolation);
884 /* fall-through */
885 case InterpolationModeBilinear:
887 REAL leftxf, topyf;
888 INT leftx, rightx, topy, bottomy;
889 ARGB topleft, topright, bottomleft, bottomright;
890 ARGB top, bottom;
891 float x_offset;
893 leftxf = floorf(point->X);
894 leftx = (INT)leftxf;
895 rightx = (INT)ceilf(point->X);
896 topyf = floorf(point->Y);
897 topy = (INT)topyf;
898 bottomy = (INT)ceilf(point->Y);
900 if (leftx == rightx && topy == bottomy)
901 return sample_bitmap_pixel(src_rect, bits, width, height,
902 leftx, topy, attributes);
904 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
905 leftx, topy, attributes);
906 topright = sample_bitmap_pixel(src_rect, bits, width, height,
907 rightx, topy, attributes);
908 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
909 leftx, bottomy, attributes);
910 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
911 rightx, bottomy, attributes);
913 x_offset = point->X - leftxf;
914 top = blend_colors(topleft, topright, x_offset);
915 bottom = blend_colors(bottomleft, bottomright, x_offset);
917 return blend_colors(top, bottom, point->Y - topyf);
919 case InterpolationModeNearestNeighbor:
921 FLOAT pixel_offset;
922 switch (offset_mode)
924 default:
925 case PixelOffsetModeNone:
926 case PixelOffsetModeHighSpeed:
927 pixel_offset = 0.5;
928 break;
930 case PixelOffsetModeHalf:
931 case PixelOffsetModeHighQuality:
932 pixel_offset = 0.0;
933 break;
935 return sample_bitmap_pixel(src_rect, bits, width, height,
936 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
942 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
944 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
947 static BOOL brush_can_fill_path(GpBrush *brush)
949 switch (brush->bt)
951 case BrushTypeSolidColor:
952 return TRUE;
953 case BrushTypeHatchFill:
955 GpHatch *hatch = (GpHatch*)brush;
956 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
957 ((hatch->backcol & 0xff000000) == 0xff000000);
959 case BrushTypeLinearGradient:
960 case BrushTypeTextureFill:
961 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
962 default:
963 return FALSE;
967 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
969 switch (brush->bt)
971 case BrushTypeSolidColor:
973 GpSolidFill *fill = (GpSolidFill*)brush;
974 HBITMAP bmp = ARGB2BMP(fill->color);
976 if (bmp)
978 RECT rc;
979 /* partially transparent fill */
981 SelectClipPath(graphics->hdc, RGN_AND);
982 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
984 HDC hdc = CreateCompatibleDC(NULL);
986 if (!hdc) break;
988 SelectObject(hdc, bmp);
989 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
990 hdc, 0, 0, 1, 1);
991 DeleteDC(hdc);
994 DeleteObject(bmp);
995 break;
997 /* else fall through */
999 default:
1001 HBRUSH gdibrush, old_brush;
1003 gdibrush = create_gdi_brush(brush);
1004 if (!gdibrush) return;
1006 old_brush = SelectObject(graphics->hdc, gdibrush);
1007 FillPath(graphics->hdc);
1008 SelectObject(graphics->hdc, old_brush);
1009 DeleteObject(gdibrush);
1010 break;
1015 static BOOL brush_can_fill_pixels(GpBrush *brush)
1017 switch (brush->bt)
1019 case BrushTypeSolidColor:
1020 case BrushTypeHatchFill:
1021 case BrushTypeLinearGradient:
1022 case BrushTypeTextureFill:
1023 case BrushTypePathGradient:
1024 return TRUE;
1025 default:
1026 return FALSE;
1030 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1031 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1033 switch (brush->bt)
1035 case BrushTypeSolidColor:
1037 int x, y;
1038 GpSolidFill *fill = (GpSolidFill*)brush;
1039 for (x=0; x<fill_area->Width; x++)
1040 for (y=0; y<fill_area->Height; y++)
1041 argb_pixels[x + y*cdwStride] = fill->color;
1042 return Ok;
1044 case BrushTypeHatchFill:
1046 int x, y;
1047 GpHatch *fill = (GpHatch*)brush;
1048 const char *hatch_data;
1050 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1051 return NotImplemented;
1053 for (x=0; x<fill_area->Width; x++)
1054 for (y=0; y<fill_area->Height; y++)
1056 int hx, hy;
1058 /* FIXME: Account for the rendering origin */
1059 hx = (x + fill_area->X) % 8;
1060 hy = (y + fill_area->Y) % 8;
1062 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1063 argb_pixels[x + y*cdwStride] = fill->forecol;
1064 else
1065 argb_pixels[x + y*cdwStride] = fill->backcol;
1068 return Ok;
1070 case BrushTypeLinearGradient:
1072 GpLineGradient *fill = (GpLineGradient*)brush;
1073 GpPointF draw_points[3], line_points[3];
1074 GpStatus stat;
1075 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1076 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1077 int x, y;
1079 draw_points[0].X = fill_area->X;
1080 draw_points[0].Y = fill_area->Y;
1081 draw_points[1].X = fill_area->X+1;
1082 draw_points[1].Y = fill_area->Y;
1083 draw_points[2].X = fill_area->X;
1084 draw_points[2].Y = fill_area->Y+1;
1086 /* Transform the points to a co-ordinate space where X is the point's
1087 * position in the gradient, 0.0 being the start point and 1.0 the
1088 * end point. */
1089 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1090 CoordinateSpaceDevice, draw_points, 3);
1092 if (stat == Ok)
1094 line_points[0] = fill->startpoint;
1095 line_points[1] = fill->endpoint;
1096 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1097 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1099 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1102 if (stat == Ok)
1104 stat = GdipInvertMatrix(world_to_gradient);
1106 if (stat == Ok)
1107 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1109 GdipDeleteMatrix(world_to_gradient);
1112 if (stat == Ok)
1114 REAL x_delta = draw_points[1].X - draw_points[0].X;
1115 REAL y_delta = draw_points[2].X - draw_points[0].X;
1117 for (y=0; y<fill_area->Height; y++)
1119 for (x=0; x<fill_area->Width; x++)
1121 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1123 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1128 return stat;
1130 case BrushTypeTextureFill:
1132 GpTexture *fill = (GpTexture*)brush;
1133 GpPointF draw_points[3];
1134 GpStatus stat;
1135 int x, y;
1136 GpBitmap *bitmap;
1137 int src_stride;
1138 GpRect src_area;
1140 if (fill->image->type != ImageTypeBitmap)
1142 FIXME("metafile texture brushes not implemented\n");
1143 return NotImplemented;
1146 bitmap = (GpBitmap*)fill->image;
1147 src_stride = sizeof(ARGB) * bitmap->width;
1149 src_area.X = src_area.Y = 0;
1150 src_area.Width = bitmap->width;
1151 src_area.Height = bitmap->height;
1153 draw_points[0].X = fill_area->X;
1154 draw_points[0].Y = fill_area->Y;
1155 draw_points[1].X = fill_area->X+1;
1156 draw_points[1].Y = fill_area->Y;
1157 draw_points[2].X = fill_area->X;
1158 draw_points[2].Y = fill_area->Y+1;
1160 /* Transform the points to the co-ordinate space of the bitmap. */
1161 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1162 CoordinateSpaceDevice, draw_points, 3);
1164 if (stat == Ok)
1166 GpMatrix world_to_texture = fill->transform;
1168 stat = GdipInvertMatrix(&world_to_texture);
1169 if (stat == Ok)
1170 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1173 if (stat == Ok && !fill->bitmap_bits)
1175 BitmapData lockeddata;
1177 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1178 if (!fill->bitmap_bits)
1179 stat = OutOfMemory;
1181 if (stat == Ok)
1183 lockeddata.Width = bitmap->width;
1184 lockeddata.Height = bitmap->height;
1185 lockeddata.Stride = src_stride;
1186 lockeddata.PixelFormat = PixelFormat32bppARGB;
1187 lockeddata.Scan0 = fill->bitmap_bits;
1189 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1190 PixelFormat32bppARGB, &lockeddata);
1193 if (stat == Ok)
1194 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1196 if (stat == Ok)
1197 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1198 bitmap->width, bitmap->height,
1199 src_stride, ColorAdjustTypeBitmap);
1201 if (stat != Ok)
1203 GdipFree(fill->bitmap_bits);
1204 fill->bitmap_bits = NULL;
1208 if (stat == Ok)
1210 REAL x_dx = draw_points[1].X - draw_points[0].X;
1211 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1212 REAL y_dx = draw_points[2].X - draw_points[0].X;
1213 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1215 for (y=0; y<fill_area->Height; y++)
1217 for (x=0; x<fill_area->Width; x++)
1219 GpPointF point;
1220 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1221 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1223 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1224 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1225 &point, fill->imageattributes, graphics->interpolation,
1226 graphics->pixeloffset);
1231 return stat;
1233 case BrushTypePathGradient:
1235 GpPathGradient *fill = (GpPathGradient*)brush;
1236 GpPath *flat_path;
1237 GpMatrix world_to_device;
1238 GpStatus stat;
1239 int i, figure_start=0;
1240 GpPointF start_point, end_point, center_point;
1241 BYTE type;
1242 REAL min_yf, max_yf, line1_xf, line2_xf;
1243 INT min_y, max_y, min_x, max_x;
1244 INT x, y;
1245 ARGB outer_color;
1246 static BOOL transform_fixme_once;
1248 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1250 static int once;
1251 if (!once++)
1252 FIXME("path gradient focus not implemented\n");
1255 if (fill->gamma)
1257 static int once;
1258 if (!once++)
1259 FIXME("path gradient gamma correction not implemented\n");
1262 if (fill->blendcount)
1264 static int once;
1265 if (!once++)
1266 FIXME("path gradient blend not implemented\n");
1269 if (fill->pblendcount)
1271 static int once;
1272 if (!once++)
1273 FIXME("path gradient preset blend not implemented\n");
1276 if (!transform_fixme_once)
1278 BOOL is_identity=TRUE;
1279 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1280 if (!is_identity)
1282 FIXME("path gradient transform not implemented\n");
1283 transform_fixme_once = TRUE;
1287 stat = GdipClonePath(fill->path, &flat_path);
1289 if (stat != Ok)
1290 return stat;
1292 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1293 CoordinateSpaceWorld, &world_to_device);
1294 if (stat == Ok)
1296 stat = GdipTransformPath(flat_path, &world_to_device);
1298 if (stat == Ok)
1300 center_point = fill->center;
1301 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1304 if (stat == Ok)
1305 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1308 if (stat != Ok)
1310 GdipDeletePath(flat_path);
1311 return stat;
1314 for (i=0; i<flat_path->pathdata.Count; i++)
1316 int start_center_line=0, end_center_line=0;
1317 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1318 REAL center_distance;
1319 ARGB start_color, end_color;
1320 REAL dy, dx;
1322 type = flat_path->pathdata.Types[i];
1324 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1325 figure_start = i;
1327 start_point = flat_path->pathdata.Points[i];
1329 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1331 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1333 end_point = flat_path->pathdata.Points[figure_start];
1334 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1336 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1338 end_point = flat_path->pathdata.Points[i+1];
1339 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1341 else
1342 continue;
1344 outer_color = start_color;
1346 min_yf = center_point.Y;
1347 if (min_yf > start_point.Y) min_yf = start_point.Y;
1348 if (min_yf > end_point.Y) min_yf = end_point.Y;
1350 if (min_yf < fill_area->Y)
1351 min_y = fill_area->Y;
1352 else
1353 min_y = (INT)ceil(min_yf);
1355 max_yf = center_point.Y;
1356 if (max_yf < start_point.Y) max_yf = start_point.Y;
1357 if (max_yf < end_point.Y) max_yf = end_point.Y;
1359 if (max_yf > fill_area->Y + fill_area->Height)
1360 max_y = fill_area->Y + fill_area->Height;
1361 else
1362 max_y = (INT)ceil(max_yf);
1364 dy = end_point.Y - start_point.Y;
1365 dx = end_point.X - start_point.X;
1367 /* This is proportional to the distance from start-end line to center point. */
1368 center_distance = dy * (start_point.X - center_point.X) +
1369 dx * (center_point.Y - start_point.Y);
1371 for (y=min_y; y<max_y; y++)
1373 REAL yf = (REAL)y;
1375 if (!seen_start && yf >= start_point.Y)
1377 seen_start = TRUE;
1378 start_center_line ^= 1;
1380 if (!seen_end && yf >= end_point.Y)
1382 seen_end = TRUE;
1383 end_center_line ^= 1;
1385 if (!seen_center && yf >= center_point.Y)
1387 seen_center = TRUE;
1388 start_center_line ^= 1;
1389 end_center_line ^= 1;
1392 if (start_center_line)
1393 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1394 else
1395 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1397 if (end_center_line)
1398 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1399 else
1400 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1402 if (line1_xf < line2_xf)
1404 min_x = (INT)ceil(line1_xf);
1405 max_x = (INT)ceil(line2_xf);
1407 else
1409 min_x = (INT)ceil(line2_xf);
1410 max_x = (INT)ceil(line1_xf);
1413 if (min_x < fill_area->X)
1414 min_x = fill_area->X;
1415 if (max_x > fill_area->X + fill_area->Width)
1416 max_x = fill_area->X + fill_area->Width;
1418 for (x=min_x; x<max_x; x++)
1420 REAL xf = (REAL)x;
1421 REAL distance;
1423 if (start_color != end_color)
1425 REAL blend_amount, pdy, pdx;
1426 pdy = yf - center_point.Y;
1427 pdx = xf - center_point.X;
1428 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1429 outer_color = blend_colors(start_color, end_color, blend_amount);
1432 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1433 (end_point.X - start_point.X) * (yf - start_point.Y);
1435 distance = distance / center_distance;
1437 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1438 blend_colors(outer_color, fill->centercolor, distance);
1443 GdipDeletePath(flat_path);
1444 return stat;
1446 default:
1447 return NotImplemented;
1451 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1452 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1453 * should not be called on an hdc that has a path you care about. */
1454 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1455 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1457 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1458 GpMatrix matrix;
1459 HBRUSH brush = NULL;
1460 HPEN pen = NULL;
1461 PointF ptf[4], *custptf = NULL;
1462 POINT pt[4], *custpt = NULL;
1463 BYTE *tp = NULL;
1464 REAL theta, dsmall, dbig, dx, dy = 0.0;
1465 INT i, count;
1466 LOGBRUSH lb;
1467 BOOL customstroke;
1469 if((x1 == x2) && (y1 == y2))
1470 return;
1472 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1474 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1475 if(!customstroke){
1476 brush = CreateSolidBrush(color);
1477 lb.lbStyle = BS_SOLID;
1478 lb.lbColor = color;
1479 lb.lbHatch = 0;
1480 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1481 PS_JOIN_MITER, 1, &lb, 0,
1482 NULL);
1483 oldbrush = SelectObject(graphics->hdc, brush);
1484 oldpen = SelectObject(graphics->hdc, pen);
1487 switch(cap){
1488 case LineCapFlat:
1489 break;
1490 case LineCapSquare:
1491 case LineCapSquareAnchor:
1492 case LineCapDiamondAnchor:
1493 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1494 if(cap == LineCapDiamondAnchor){
1495 dsmall = cos(theta + M_PI_2) * size;
1496 dbig = sin(theta + M_PI_2) * size;
1498 else{
1499 dsmall = cos(theta + M_PI_4) * size;
1500 dbig = sin(theta + M_PI_4) * size;
1503 ptf[0].X = x2 - dsmall;
1504 ptf[1].X = x2 + dbig;
1506 ptf[0].Y = y2 - dbig;
1507 ptf[3].Y = y2 + dsmall;
1509 ptf[1].Y = y2 - dsmall;
1510 ptf[2].Y = y2 + dbig;
1512 ptf[3].X = x2 - dbig;
1513 ptf[2].X = x2 + dsmall;
1515 transform_and_round_points(graphics, pt, ptf, 4);
1516 Polygon(graphics->hdc, pt, 4);
1518 break;
1519 case LineCapArrowAnchor:
1520 size = size * 4.0 / sqrt(3.0);
1522 dx = cos(M_PI / 6.0 + theta) * size;
1523 dy = sin(M_PI / 6.0 + theta) * size;
1525 ptf[0].X = x2 - dx;
1526 ptf[0].Y = y2 - dy;
1528 dx = cos(- M_PI / 6.0 + theta) * size;
1529 dy = sin(- M_PI / 6.0 + theta) * size;
1531 ptf[1].X = x2 - dx;
1532 ptf[1].Y = y2 - dy;
1534 ptf[2].X = x2;
1535 ptf[2].Y = y2;
1537 transform_and_round_points(graphics, pt, ptf, 3);
1538 Polygon(graphics->hdc, pt, 3);
1540 break;
1541 case LineCapRoundAnchor:
1542 dx = dy = ANCHOR_WIDTH * size / 2.0;
1544 ptf[0].X = x2 - dx;
1545 ptf[0].Y = y2 - dy;
1546 ptf[1].X = x2 + dx;
1547 ptf[1].Y = y2 + dy;
1549 transform_and_round_points(graphics, pt, ptf, 2);
1550 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1552 break;
1553 case LineCapTriangle:
1554 size = size / 2.0;
1555 dx = cos(M_PI_2 + theta) * size;
1556 dy = sin(M_PI_2 + theta) * size;
1558 ptf[0].X = x2 - dx;
1559 ptf[0].Y = y2 - dy;
1560 ptf[1].X = x2 + dx;
1561 ptf[1].Y = y2 + dy;
1563 dx = cos(theta) * size;
1564 dy = sin(theta) * size;
1566 ptf[2].X = x2 + dx;
1567 ptf[2].Y = y2 + dy;
1569 transform_and_round_points(graphics, pt, ptf, 3);
1570 Polygon(graphics->hdc, pt, 3);
1572 break;
1573 case LineCapRound:
1574 dx = dy = size / 2.0;
1576 ptf[0].X = x2 - dx;
1577 ptf[0].Y = y2 - dy;
1578 ptf[1].X = x2 + dx;
1579 ptf[1].Y = y2 + dy;
1581 dx = -cos(M_PI_2 + theta) * size;
1582 dy = -sin(M_PI_2 + theta) * size;
1584 ptf[2].X = x2 - dx;
1585 ptf[2].Y = y2 - dy;
1586 ptf[3].X = x2 + dx;
1587 ptf[3].Y = y2 + dy;
1589 transform_and_round_points(graphics, pt, ptf, 4);
1590 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1591 pt[2].y, pt[3].x, pt[3].y);
1593 break;
1594 case LineCapCustom:
1595 if(!custom)
1596 break;
1598 count = custom->pathdata.Count;
1599 custptf = GdipAlloc(count * sizeof(PointF));
1600 custpt = GdipAlloc(count * sizeof(POINT));
1601 tp = GdipAlloc(count);
1603 if(!custptf || !custpt || !tp)
1604 goto custend;
1606 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1608 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1609 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1610 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1611 MatrixOrderAppend);
1612 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1613 GdipTransformMatrixPoints(&matrix, custptf, count);
1615 transform_and_round_points(graphics, custpt, custptf, count);
1617 for(i = 0; i < count; i++)
1618 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1620 if(custom->fill){
1621 BeginPath(graphics->hdc);
1622 PolyDraw(graphics->hdc, custpt, tp, count);
1623 EndPath(graphics->hdc);
1624 StrokeAndFillPath(graphics->hdc);
1626 else
1627 PolyDraw(graphics->hdc, custpt, tp, count);
1629 custend:
1630 GdipFree(custptf);
1631 GdipFree(custpt);
1632 GdipFree(tp);
1633 break;
1634 default:
1635 break;
1638 if(!customstroke){
1639 SelectObject(graphics->hdc, oldbrush);
1640 SelectObject(graphics->hdc, oldpen);
1641 DeleteObject(brush);
1642 DeleteObject(pen);
1646 /* Shortens the line by the given percent by changing x2, y2.
1647 * If percent is > 1.0 then the line will change direction.
1648 * If percent is negative it can lengthen the line. */
1649 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1651 REAL dist, theta, dx, dy;
1653 if((y1 == *y2) && (x1 == *x2))
1654 return;
1656 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1657 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1658 dx = cos(theta) * dist;
1659 dy = sin(theta) * dist;
1661 *x2 = *x2 + dx;
1662 *y2 = *y2 + dy;
1665 /* Shortens the line by the given amount by changing x2, y2.
1666 * If the amount is greater than the distance, the line will become length 0.
1667 * If the amount is negative, it can lengthen the line. */
1668 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1670 REAL dx, dy, percent;
1672 dx = *x2 - x1;
1673 dy = *y2 - y1;
1674 if(dx == 0 && dy == 0)
1675 return;
1677 percent = amt / sqrt(dx * dx + dy * dy);
1678 if(percent >= 1.0){
1679 *x2 = x1;
1680 *y2 = y1;
1681 return;
1684 shorten_line_percent(x1, y1, x2, y2, percent);
1687 /* Conducts a linear search to find the bezier points that will back off
1688 * the endpoint of the curve by a distance of amt. Linear search works
1689 * better than binary in this case because there are multiple solutions,
1690 * and binary searches often find a bad one. I don't think this is what
1691 * Windows does but short of rendering the bezier without GDI's help it's
1692 * the best we can do. If rev then work from the start of the passed points
1693 * instead of the end. */
1694 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1696 GpPointF origpt[4];
1697 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1698 INT i, first = 0, second = 1, third = 2, fourth = 3;
1700 if(rev){
1701 first = 3;
1702 second = 2;
1703 third = 1;
1704 fourth = 0;
1707 origx = pt[fourth].X;
1708 origy = pt[fourth].Y;
1709 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1711 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1712 /* reset bezier points to original values */
1713 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1714 /* Perform magic on bezier points. Order is important here.*/
1715 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1716 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1717 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1718 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1719 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1720 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1722 dx = pt[fourth].X - origx;
1723 dy = pt[fourth].Y - origy;
1725 diff = sqrt(dx * dx + dy * dy);
1726 percent += 0.0005 * amt;
1730 /* Draws a combination of bezier curves and lines between points. */
1731 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1732 GDIPCONST BYTE * types, INT count, BOOL caps)
1734 POINT *pti = GdipAlloc(count * sizeof(POINT));
1735 BYTE *tp = GdipAlloc(count);
1736 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1737 INT i, j;
1738 GpStatus status = GenericError;
1740 if(!count){
1741 status = Ok;
1742 goto end;
1744 if(!pti || !tp || !ptcopy){
1745 status = OutOfMemory;
1746 goto end;
1749 for(i = 1; i < count; i++){
1750 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1751 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1752 || !(types[i + 1] & PathPointTypeBezier)){
1753 ERR("Bad bezier points\n");
1754 goto end;
1756 i += 2;
1760 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1762 /* If we are drawing caps, go through the points and adjust them accordingly,
1763 * and draw the caps. */
1764 if(caps){
1765 switch(types[count - 1] & PathPointTypePathTypeMask){
1766 case PathPointTypeBezier:
1767 if(pen->endcap == LineCapArrowAnchor)
1768 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1769 else if((pen->endcap == LineCapCustom) && pen->customend)
1770 shorten_bezier_amt(&ptcopy[count - 4],
1771 pen->width * pen->customend->inset, FALSE);
1773 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1774 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1775 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1776 pt[count - 1].X, pt[count - 1].Y);
1778 break;
1779 case PathPointTypeLine:
1780 if(pen->endcap == LineCapArrowAnchor)
1781 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1782 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1783 pen->width);
1784 else if((pen->endcap == LineCapCustom) && pen->customend)
1785 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1786 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1787 pen->customend->inset * pen->width);
1789 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1790 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1791 pt[count - 1].Y);
1793 break;
1794 default:
1795 ERR("Bad path last point\n");
1796 goto end;
1799 /* Find start of points */
1800 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1801 == PathPointTypeStart); j++);
1803 switch(types[j] & PathPointTypePathTypeMask){
1804 case PathPointTypeBezier:
1805 if(pen->startcap == LineCapArrowAnchor)
1806 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1807 else if((pen->startcap == LineCapCustom) && pen->customstart)
1808 shorten_bezier_amt(&ptcopy[j - 1],
1809 pen->width * pen->customstart->inset, TRUE);
1811 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1812 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1813 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1814 pt[j - 1].X, pt[j - 1].Y);
1816 break;
1817 case PathPointTypeLine:
1818 if(pen->startcap == LineCapArrowAnchor)
1819 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1820 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1821 pen->width);
1822 else if((pen->startcap == LineCapCustom) && pen->customstart)
1823 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1824 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1825 pen->customstart->inset * pen->width);
1827 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1828 pt[j].X, pt[j].Y, pt[j - 1].X,
1829 pt[j - 1].Y);
1831 break;
1832 default:
1833 ERR("Bad path points\n");
1834 goto end;
1838 transform_and_round_points(graphics, pti, ptcopy, count);
1840 for(i = 0; i < count; i++){
1841 tp[i] = convert_path_point_type(types[i]);
1844 PolyDraw(graphics->hdc, pti, tp, count);
1846 status = Ok;
1848 end:
1849 GdipFree(pti);
1850 GdipFree(ptcopy);
1851 GdipFree(tp);
1853 return status;
1856 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1858 GpStatus result;
1860 BeginPath(graphics->hdc);
1861 result = draw_poly(graphics, NULL, path->pathdata.Points,
1862 path->pathdata.Types, path->pathdata.Count, FALSE);
1863 EndPath(graphics->hdc);
1864 return result;
1867 typedef struct _GraphicsContainerItem {
1868 struct list entry;
1869 GraphicsContainer contid;
1871 SmoothingMode smoothing;
1872 CompositingQuality compqual;
1873 InterpolationMode interpolation;
1874 CompositingMode compmode;
1875 TextRenderingHint texthint;
1876 REAL scale;
1877 GpUnit unit;
1878 PixelOffsetMode pixeloffset;
1879 UINT textcontrast;
1880 GpMatrix worldtrans;
1881 GpRegion* clip;
1882 INT origin_x, origin_y;
1883 } GraphicsContainerItem;
1885 static GpStatus init_container(GraphicsContainerItem** container,
1886 GDIPCONST GpGraphics* graphics){
1887 GpStatus sts;
1889 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1890 if(!(*container))
1891 return OutOfMemory;
1893 (*container)->contid = graphics->contid + 1;
1895 (*container)->smoothing = graphics->smoothing;
1896 (*container)->compqual = graphics->compqual;
1897 (*container)->interpolation = graphics->interpolation;
1898 (*container)->compmode = graphics->compmode;
1899 (*container)->texthint = graphics->texthint;
1900 (*container)->scale = graphics->scale;
1901 (*container)->unit = graphics->unit;
1902 (*container)->textcontrast = graphics->textcontrast;
1903 (*container)->pixeloffset = graphics->pixeloffset;
1904 (*container)->origin_x = graphics->origin_x;
1905 (*container)->origin_y = graphics->origin_y;
1906 (*container)->worldtrans = graphics->worldtrans;
1908 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1909 if(sts != Ok){
1910 GdipFree(*container);
1911 *container = NULL;
1912 return sts;
1915 return Ok;
1918 static void delete_container(GraphicsContainerItem* container)
1920 GdipDeleteRegion(container->clip);
1921 GdipFree(container);
1924 static GpStatus restore_container(GpGraphics* graphics,
1925 GDIPCONST GraphicsContainerItem* container){
1926 GpStatus sts;
1927 GpRegion *newClip;
1929 sts = GdipCloneRegion(container->clip, &newClip);
1930 if(sts != Ok) return sts;
1932 graphics->worldtrans = container->worldtrans;
1934 GdipDeleteRegion(graphics->clip);
1935 graphics->clip = newClip;
1937 graphics->contid = container->contid - 1;
1939 graphics->smoothing = container->smoothing;
1940 graphics->compqual = container->compqual;
1941 graphics->interpolation = container->interpolation;
1942 graphics->compmode = container->compmode;
1943 graphics->texthint = container->texthint;
1944 graphics->scale = container->scale;
1945 graphics->unit = container->unit;
1946 graphics->textcontrast = container->textcontrast;
1947 graphics->pixeloffset = container->pixeloffset;
1948 graphics->origin_x = container->origin_x;
1949 graphics->origin_y = container->origin_y;
1951 return Ok;
1954 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1956 RECT wnd_rect;
1957 GpStatus stat=Ok;
1958 GpUnit unit;
1960 if(graphics->hwnd) {
1961 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1962 return GenericError;
1964 rect->X = wnd_rect.left;
1965 rect->Y = wnd_rect.top;
1966 rect->Width = wnd_rect.right - wnd_rect.left;
1967 rect->Height = wnd_rect.bottom - wnd_rect.top;
1968 }else if (graphics->image){
1969 stat = GdipGetImageBounds(graphics->image, rect, &unit);
1970 if (stat == Ok && unit != UnitPixel)
1971 FIXME("need to convert from unit %i\n", unit);
1972 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
1973 HBITMAP hbmp;
1974 BITMAP bmp;
1976 rect->X = 0;
1977 rect->Y = 0;
1979 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
1980 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
1982 rect->Width = bmp.bmWidth;
1983 rect->Height = bmp.bmHeight;
1985 else
1987 /* FIXME: ??? */
1988 rect->Width = 1;
1989 rect->Height = 1;
1991 }else{
1992 rect->X = 0;
1993 rect->Y = 0;
1994 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1995 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1998 if (graphics->hdc &&
1999 (GetMapMode(graphics->hdc) != MM_TEXT || GetGraphicsMode(graphics->hdc) != GM_COMPATIBLE))
2001 POINT points[2];
2003 points[0].x = rect->X;
2004 points[0].y = rect->Y;
2005 points[1].x = rect->X + rect->Width;
2006 points[1].y = rect->Y + rect->Height;
2008 DPtoLP(graphics->hdc, points, sizeof(points)/sizeof(points[0]));
2010 rect->X = min(points[0].x, points[1].x);
2011 rect->Y = min(points[0].y, points[1].y);
2012 rect->Width = abs(points[1].x - points[0].x);
2013 rect->Height = abs(points[1].y - points[0].y);
2016 return stat;
2019 /* on success, rgn will contain the region of the graphics object which
2020 * is visible after clipping has been applied */
2021 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2023 GpStatus stat;
2024 GpRectF rectf;
2025 GpRegion* tmp;
2027 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2028 return stat;
2030 if((stat = GdipCreateRegion(&tmp)) != Ok)
2031 return stat;
2033 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2034 goto end;
2036 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2037 goto end;
2039 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2041 end:
2042 GdipDeleteRegion(tmp);
2043 return stat;
2046 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2048 REAL height;
2050 if (font->unit == UnitPixel)
2052 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2054 else
2056 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2057 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2058 else
2059 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2062 lf->lfHeight = -(height + 0.5);
2063 lf->lfWidth = 0;
2064 lf->lfEscapement = 0;
2065 lf->lfOrientation = 0;
2066 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2067 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2068 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2069 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2070 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2071 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2072 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2073 lf->lfQuality = DEFAULT_QUALITY;
2074 lf->lfPitchAndFamily = 0;
2075 strcpyW(lf->lfFaceName, font->family->FamilyName);
2078 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2079 GDIPCONST GpStringFormat *format, HFONT *hfont,
2080 GDIPCONST GpMatrix *matrix)
2082 HDC hdc = CreateCompatibleDC(0);
2083 GpPointF pt[3];
2084 REAL angle, rel_width, rel_height, font_height;
2085 LOGFONTW lfw;
2086 HFONT unscaled_font;
2087 TEXTMETRICW textmet;
2089 if (font->unit == UnitPixel)
2090 font_height = font->emSize;
2091 else
2093 REAL unit_scale, res;
2095 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2096 unit_scale = units_scale(font->unit, graphics->unit, res);
2098 font_height = font->emSize * unit_scale;
2101 pt[0].X = 0.0;
2102 pt[0].Y = 0.0;
2103 pt[1].X = 1.0;
2104 pt[1].Y = 0.0;
2105 pt[2].X = 0.0;
2106 pt[2].Y = 1.0;
2107 if (matrix)
2109 GpMatrix xform = *matrix;
2110 GdipTransformMatrixPoints(&xform, pt, 3);
2112 if (graphics)
2113 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2114 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2115 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2116 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2117 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2118 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2120 get_log_fontW(font, graphics, &lfw);
2121 lfw.lfHeight = -gdip_round(font_height * rel_height);
2122 unscaled_font = CreateFontIndirectW(&lfw);
2124 SelectObject(hdc, unscaled_font);
2125 GetTextMetricsW(hdc, &textmet);
2127 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2128 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2130 *hfont = CreateFontIndirectW(&lfw);
2132 DeleteDC(hdc);
2133 DeleteObject(unscaled_font);
2136 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2138 TRACE("(%p, %p)\n", hdc, graphics);
2140 return GdipCreateFromHDC2(hdc, NULL, graphics);
2143 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2145 GpStatus retval;
2146 HBITMAP hbitmap;
2147 DIBSECTION dib;
2149 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2151 if(hDevice != NULL)
2152 FIXME("Don't know how to handle parameter hDevice\n");
2154 if(hdc == NULL)
2155 return OutOfMemory;
2157 if(graphics == NULL)
2158 return InvalidParameter;
2160 *graphics = GdipAlloc(sizeof(GpGraphics));
2161 if(!*graphics) return OutOfMemory;
2163 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2165 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2166 GdipFree(*graphics);
2167 return retval;
2170 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2171 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2172 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2174 (*graphics)->alpha_hdc = 1;
2177 (*graphics)->hdc = hdc;
2178 (*graphics)->hwnd = WindowFromDC(hdc);
2179 (*graphics)->owndc = FALSE;
2180 (*graphics)->smoothing = SmoothingModeDefault;
2181 (*graphics)->compqual = CompositingQualityDefault;
2182 (*graphics)->interpolation = InterpolationModeBilinear;
2183 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2184 (*graphics)->compmode = CompositingModeSourceOver;
2185 (*graphics)->unit = UnitDisplay;
2186 (*graphics)->scale = 1.0;
2187 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2188 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2189 (*graphics)->busy = FALSE;
2190 (*graphics)->textcontrast = 4;
2191 list_init(&(*graphics)->containers);
2192 (*graphics)->contid = 0;
2194 TRACE("<-- %p\n", *graphics);
2196 return Ok;
2199 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2201 GpStatus retval;
2203 *graphics = GdipAlloc(sizeof(GpGraphics));
2204 if(!*graphics) return OutOfMemory;
2206 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2208 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2209 GdipFree(*graphics);
2210 return retval;
2213 (*graphics)->hdc = NULL;
2214 (*graphics)->hwnd = NULL;
2215 (*graphics)->owndc = FALSE;
2216 (*graphics)->image = image;
2217 /* We have to store the image type here because the image may be freed
2218 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2219 (*graphics)->image_type = image->type;
2220 (*graphics)->smoothing = SmoothingModeDefault;
2221 (*graphics)->compqual = CompositingQualityDefault;
2222 (*graphics)->interpolation = InterpolationModeBilinear;
2223 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2224 (*graphics)->compmode = CompositingModeSourceOver;
2225 (*graphics)->unit = UnitDisplay;
2226 (*graphics)->scale = 1.0;
2227 (*graphics)->xres = image->xres;
2228 (*graphics)->yres = image->yres;
2229 (*graphics)->busy = FALSE;
2230 (*graphics)->textcontrast = 4;
2231 list_init(&(*graphics)->containers);
2232 (*graphics)->contid = 0;
2234 TRACE("<-- %p\n", *graphics);
2236 return Ok;
2239 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2241 GpStatus ret;
2242 HDC hdc;
2244 TRACE("(%p, %p)\n", hwnd, graphics);
2246 hdc = GetDC(hwnd);
2248 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2250 ReleaseDC(hwnd, hdc);
2251 return ret;
2254 (*graphics)->hwnd = hwnd;
2255 (*graphics)->owndc = TRUE;
2257 return Ok;
2260 /* FIXME: no icm handling */
2261 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2263 TRACE("(%p, %p)\n", hwnd, graphics);
2265 return GdipCreateFromHWND(hwnd, graphics);
2268 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2269 UINT access, IStream **stream)
2271 DWORD dwMode;
2272 HRESULT ret;
2274 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2276 if(!stream || !filename)
2277 return InvalidParameter;
2279 if(access & GENERIC_WRITE)
2280 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2281 else if(access & GENERIC_READ)
2282 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2283 else
2284 return InvalidParameter;
2286 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2288 return hresult_to_status(ret);
2291 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2293 GraphicsContainerItem *cont, *next;
2294 GpStatus stat;
2295 TRACE("(%p)\n", graphics);
2297 if(!graphics) return InvalidParameter;
2298 if(graphics->busy) return ObjectBusy;
2300 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2302 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2303 if (stat != Ok)
2304 return stat;
2307 if(graphics->owndc)
2308 ReleaseDC(graphics->hwnd, graphics->hdc);
2310 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2311 list_remove(&cont->entry);
2312 delete_container(cont);
2315 GdipDeleteRegion(graphics->clip);
2317 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2318 * do otherwise, but we can't have that in the test suite because it means
2319 * accessing freed memory. */
2320 graphics->busy = TRUE;
2322 GdipFree(graphics);
2324 return Ok;
2327 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2328 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2330 GpStatus status;
2331 GpPath *path;
2333 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2334 width, height, startAngle, sweepAngle);
2336 if(!graphics || !pen || width <= 0 || height <= 0)
2337 return InvalidParameter;
2339 if(graphics->busy)
2340 return ObjectBusy;
2342 status = GdipCreatePath(FillModeAlternate, &path);
2343 if (status != Ok) return status;
2345 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2346 if (status == Ok)
2347 status = GdipDrawPath(graphics, pen, path);
2349 GdipDeletePath(path);
2350 return status;
2353 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2354 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2356 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2357 width, height, startAngle, sweepAngle);
2359 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2362 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2363 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2365 GpPointF pt[4];
2367 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2368 x2, y2, x3, y3, x4, y4);
2370 if(!graphics || !pen)
2371 return InvalidParameter;
2373 if(graphics->busy)
2374 return ObjectBusy;
2376 pt[0].X = x1;
2377 pt[0].Y = y1;
2378 pt[1].X = x2;
2379 pt[1].Y = y2;
2380 pt[2].X = x3;
2381 pt[2].Y = y3;
2382 pt[3].X = x4;
2383 pt[3].Y = y4;
2384 return GdipDrawBeziers(graphics, pen, pt, 4);
2387 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2388 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2390 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2391 x2, y2, x3, y3, x4, y4);
2393 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2396 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2397 GDIPCONST GpPointF *points, INT count)
2399 GpStatus status;
2400 GpPath *path;
2402 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2404 if(!graphics || !pen || !points || (count <= 0))
2405 return InvalidParameter;
2407 if(graphics->busy)
2408 return ObjectBusy;
2410 status = GdipCreatePath(FillModeAlternate, &path);
2411 if (status != Ok) return status;
2413 status = GdipAddPathBeziers(path, points, count);
2414 if (status == Ok)
2415 status = GdipDrawPath(graphics, pen, path);
2417 GdipDeletePath(path);
2418 return status;
2421 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2422 GDIPCONST GpPoint *points, INT count)
2424 GpPointF *pts;
2425 GpStatus ret;
2426 INT i;
2428 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2430 if(!graphics || !pen || !points || (count <= 0))
2431 return InvalidParameter;
2433 if(graphics->busy)
2434 return ObjectBusy;
2436 pts = GdipAlloc(sizeof(GpPointF) * count);
2437 if(!pts)
2438 return OutOfMemory;
2440 for(i = 0; i < count; i++){
2441 pts[i].X = (REAL)points[i].X;
2442 pts[i].Y = (REAL)points[i].Y;
2445 ret = GdipDrawBeziers(graphics,pen,pts,count);
2447 GdipFree(pts);
2449 return ret;
2452 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2453 GDIPCONST GpPointF *points, INT count)
2455 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2457 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2460 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2461 GDIPCONST GpPoint *points, INT count)
2463 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2465 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2468 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2469 GDIPCONST GpPointF *points, INT count, REAL tension)
2471 GpPath *path;
2472 GpStatus status;
2474 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2476 if(!graphics || !pen || !points || count <= 0)
2477 return InvalidParameter;
2479 if(graphics->busy)
2480 return ObjectBusy;
2482 status = GdipCreatePath(FillModeAlternate, &path);
2483 if (status != Ok) return status;
2485 status = GdipAddPathClosedCurve2(path, points, count, tension);
2486 if (status == Ok)
2487 status = GdipDrawPath(graphics, pen, path);
2489 GdipDeletePath(path);
2491 return status;
2494 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2495 GDIPCONST GpPoint *points, INT count, REAL tension)
2497 GpPointF *ptf;
2498 GpStatus stat;
2499 INT i;
2501 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2503 if(!points || count <= 0)
2504 return InvalidParameter;
2506 ptf = GdipAlloc(sizeof(GpPointF)*count);
2507 if(!ptf)
2508 return OutOfMemory;
2510 for(i = 0; i < count; i++){
2511 ptf[i].X = (REAL)points[i].X;
2512 ptf[i].Y = (REAL)points[i].Y;
2515 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2517 GdipFree(ptf);
2519 return stat;
2522 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2523 GDIPCONST GpPointF *points, INT count)
2525 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2527 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2530 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2531 GDIPCONST GpPoint *points, INT count)
2533 GpPointF *pointsF;
2534 GpStatus ret;
2535 INT i;
2537 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2539 if(!points)
2540 return InvalidParameter;
2542 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2543 if(!pointsF)
2544 return OutOfMemory;
2546 for(i = 0; i < count; i++){
2547 pointsF[i].X = (REAL)points[i].X;
2548 pointsF[i].Y = (REAL)points[i].Y;
2551 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2552 GdipFree(pointsF);
2554 return ret;
2557 /* Approximates cardinal spline with Bezier curves. */
2558 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2559 GDIPCONST GpPointF *points, INT count, REAL tension)
2561 GpPath *path;
2562 GpStatus status;
2564 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2566 if(!graphics || !pen)
2567 return InvalidParameter;
2569 if(graphics->busy)
2570 return ObjectBusy;
2572 if(count < 2)
2573 return InvalidParameter;
2575 status = GdipCreatePath(FillModeAlternate, &path);
2576 if (status != Ok) return status;
2578 status = GdipAddPathCurve2(path, points, count, tension);
2579 if (status == Ok)
2580 status = GdipDrawPath(graphics, pen, path);
2582 GdipDeletePath(path);
2583 return status;
2586 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2587 GDIPCONST GpPoint *points, INT count, REAL tension)
2589 GpPointF *pointsF;
2590 GpStatus ret;
2591 INT i;
2593 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2595 if(!points)
2596 return InvalidParameter;
2598 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2599 if(!pointsF)
2600 return OutOfMemory;
2602 for(i = 0; i < count; i++){
2603 pointsF[i].X = (REAL)points[i].X;
2604 pointsF[i].Y = (REAL)points[i].Y;
2607 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2608 GdipFree(pointsF);
2610 return ret;
2613 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2614 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2615 REAL tension)
2617 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2619 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2620 return InvalidParameter;
2623 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2626 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2627 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2628 REAL tension)
2630 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2632 if(count < 0){
2633 return OutOfMemory;
2636 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2637 return InvalidParameter;
2640 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2643 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2644 REAL y, REAL width, REAL height)
2646 GpPath *path;
2647 GpStatus status;
2649 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2651 if(!graphics || !pen)
2652 return InvalidParameter;
2654 if(graphics->busy)
2655 return ObjectBusy;
2657 status = GdipCreatePath(FillModeAlternate, &path);
2658 if (status != Ok) return status;
2660 status = GdipAddPathEllipse(path, x, y, width, height);
2661 if (status == Ok)
2662 status = GdipDrawPath(graphics, pen, path);
2664 GdipDeletePath(path);
2665 return status;
2668 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2669 INT y, INT width, INT height)
2671 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2673 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2677 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2679 UINT width, height;
2681 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2683 if(!graphics || !image)
2684 return InvalidParameter;
2686 GdipGetImageWidth(image, &width);
2687 GdipGetImageHeight(image, &height);
2689 return GdipDrawImagePointRect(graphics, image, x, y,
2690 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2693 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2694 INT y)
2696 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2698 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2701 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2702 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2703 GpUnit srcUnit)
2705 GpPointF points[3];
2706 REAL scale_x, scale_y, width, height;
2708 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2710 if (!graphics || !image) return InvalidParameter;
2712 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2713 scale_x *= graphics->xres / image->xres;
2714 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2715 scale_y *= graphics->yres / image->yres;
2716 width = srcwidth * scale_x;
2717 height = srcheight * scale_y;
2719 points[0].X = points[2].X = x;
2720 points[0].Y = points[1].Y = y;
2721 points[1].X = x + width;
2722 points[2].Y = y + height;
2724 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2725 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2728 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2729 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2730 GpUnit srcUnit)
2732 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2735 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2736 GDIPCONST GpPointF *dstpoints, INT count)
2738 UINT width, height;
2740 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2742 if(!image)
2743 return InvalidParameter;
2745 GdipGetImageWidth(image, &width);
2746 GdipGetImageHeight(image, &height);
2748 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2749 width, height, UnitPixel, NULL, NULL, NULL);
2752 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2753 GDIPCONST GpPoint *dstpoints, INT count)
2755 GpPointF ptf[3];
2757 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2759 if (count != 3 || !dstpoints)
2760 return InvalidParameter;
2762 ptf[0].X = (REAL)dstpoints[0].X;
2763 ptf[0].Y = (REAL)dstpoints[0].Y;
2764 ptf[1].X = (REAL)dstpoints[1].X;
2765 ptf[1].Y = (REAL)dstpoints[1].Y;
2766 ptf[2].X = (REAL)dstpoints[2].X;
2767 ptf[2].Y = (REAL)dstpoints[2].Y;
2769 return GdipDrawImagePoints(graphics, image, ptf, count);
2772 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2773 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2775 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2776 return TRUE;
2779 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2780 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2781 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2782 DrawImageAbort callback, VOID * callbackData)
2784 GpPointF ptf[4];
2785 POINT pti[4];
2786 GpStatus stat;
2788 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2789 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2790 callbackData);
2792 if (count > 3)
2793 return NotImplemented;
2795 if(!graphics || !image || !points || count != 3)
2796 return InvalidParameter;
2798 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2799 debugstr_pointf(&points[2]));
2801 memcpy(ptf, points, 3 * sizeof(GpPointF));
2803 /* Ensure source width/height is positive */
2804 if (srcwidth < 0)
2806 GpPointF tmp = ptf[1];
2807 srcx = srcx + srcwidth;
2808 srcwidth = -srcwidth;
2809 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2810 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2811 ptf[1] = ptf[0];
2812 ptf[0] = tmp;
2815 if (srcheight < 0)
2817 GpPointF tmp = ptf[2];
2818 srcy = srcy + srcheight;
2819 srcheight = -srcheight;
2820 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2821 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2822 ptf[2] = ptf[0];
2823 ptf[0] = tmp;
2826 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2827 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2828 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2829 return Ok;
2830 transform_and_round_points(graphics, pti, ptf, 4);
2832 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2833 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2835 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2836 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2837 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2838 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2839 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2841 if (image->picture)
2843 if (!graphics->hdc)
2845 FIXME("graphics object has no HDC\n");
2848 if(IPicture_Render(image->picture, graphics->hdc,
2849 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2850 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
2852 if(callback)
2853 callback(callbackData);
2854 return GenericError;
2857 else if (image->type == ImageTypeBitmap)
2859 GpBitmap* bitmap = (GpBitmap*)image;
2860 BOOL use_software = FALSE;
2862 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2863 graphics->xres, graphics->yres,
2864 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2865 graphics->scale, image->xres, image->yres, bitmap->format,
2866 imageAttributes ? imageAttributes->outside_color : 0);
2868 if (imageAttributes || graphics->alpha_hdc ||
2869 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2870 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2871 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2872 srcx < 0 || srcy < 0 ||
2873 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2874 use_software = TRUE;
2876 if (use_software)
2878 RECT dst_area;
2879 GpRectF graphics_bounds;
2880 GpRect src_area;
2881 int i, x, y, src_stride, dst_stride;
2882 GpMatrix dst_to_src;
2883 REAL m11, m12, m21, m22, mdx, mdy;
2884 LPBYTE src_data, dst_data;
2885 BitmapData lockeddata;
2886 InterpolationMode interpolation = graphics->interpolation;
2887 PixelOffsetMode offset_mode = graphics->pixeloffset;
2888 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2889 REAL x_dx, x_dy, y_dx, y_dy;
2890 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2892 if (!imageAttributes)
2893 imageAttributes = &defaultImageAttributes;
2895 dst_area.left = dst_area.right = pti[0].x;
2896 dst_area.top = dst_area.bottom = pti[0].y;
2897 for (i=1; i<4; i++)
2899 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2900 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2901 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2902 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2905 stat = get_graphics_bounds(graphics, &graphics_bounds);
2906 if (stat != Ok) return stat;
2908 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
2909 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
2910 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
2911 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
2913 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2915 if (IsRectEmpty(&dst_area)) return Ok;
2917 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2918 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2919 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2920 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2921 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2922 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2924 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2926 stat = GdipInvertMatrix(&dst_to_src);
2927 if (stat != Ok) return stat;
2929 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2930 if (!dst_data) return OutOfMemory;
2932 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2934 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2935 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2937 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
2939 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2940 if (!src_data)
2942 GdipFree(dst_data);
2943 return OutOfMemory;
2945 src_stride = sizeof(ARGB) * src_area.Width;
2947 /* Read the bits we need from the source bitmap into an ARGB buffer. */
2948 lockeddata.Width = src_area.Width;
2949 lockeddata.Height = src_area.Height;
2950 lockeddata.Stride = src_stride;
2951 lockeddata.PixelFormat = PixelFormat32bppARGB;
2952 lockeddata.Scan0 = src_data;
2954 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
2955 PixelFormat32bppARGB, &lockeddata);
2957 if (stat == Ok)
2958 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2960 if (stat != Ok)
2962 GdipFree(src_data);
2963 GdipFree(dst_data);
2964 return stat;
2967 apply_image_attributes(imageAttributes, src_data,
2968 src_area.Width, src_area.Height,
2969 src_stride, ColorAdjustTypeBitmap);
2971 /* Transform the bits as needed to the destination. */
2972 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
2974 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
2975 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
2976 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
2977 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
2979 for (x=dst_area.left; x<dst_area.right; x++)
2981 for (y=dst_area.top; y<dst_area.bottom; y++)
2983 GpPointF src_pointf;
2984 ARGB *dst_color;
2986 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
2987 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
2989 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2991 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
2992 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
2993 imageAttributes, interpolation, offset_mode);
2994 else
2995 *dst_color = 0;
2999 GdipFree(src_data);
3001 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3002 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3004 GdipFree(dst_data);
3006 return stat;
3008 else
3010 HDC hdc;
3011 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3012 HBITMAP hbitmap, old_hbm=NULL;
3014 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3015 bitmap->format == PixelFormat24bppRGB ||
3016 bitmap->format == PixelFormat32bppRGB ||
3017 bitmap->format == PixelFormat32bppPARGB))
3019 BITMAPINFOHEADER bih;
3020 BYTE *temp_bits;
3021 PixelFormat dst_format;
3023 /* we can't draw a bitmap of this format directly */
3024 hdc = CreateCompatibleDC(0);
3025 temp_hdc = TRUE;
3026 temp_bitmap = TRUE;
3028 bih.biSize = sizeof(BITMAPINFOHEADER);
3029 bih.biWidth = bitmap->width;
3030 bih.biHeight = -bitmap->height;
3031 bih.biPlanes = 1;
3032 bih.biBitCount = 32;
3033 bih.biCompression = BI_RGB;
3034 bih.biSizeImage = 0;
3035 bih.biXPelsPerMeter = 0;
3036 bih.biYPelsPerMeter = 0;
3037 bih.biClrUsed = 0;
3038 bih.biClrImportant = 0;
3040 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3041 (void**)&temp_bits, NULL, 0);
3043 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3044 dst_format = PixelFormat32bppPARGB;
3045 else
3046 dst_format = PixelFormat32bppRGB;
3048 convert_pixels(bitmap->width, bitmap->height,
3049 bitmap->width*4, temp_bits, dst_format,
3050 bitmap->stride, bitmap->bits, bitmap->format,
3051 bitmap->image.palette);
3053 else
3055 if (bitmap->hbitmap)
3056 hbitmap = bitmap->hbitmap;
3057 else
3059 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3060 temp_bitmap = TRUE;
3063 hdc = bitmap->hdc;
3064 temp_hdc = (hdc == 0);
3067 if (temp_hdc)
3069 if (!hdc) hdc = CreateCompatibleDC(0);
3070 old_hbm = SelectObject(hdc, hbitmap);
3073 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3075 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3076 hdc, srcx, srcy, srcwidth, srcheight);
3078 else
3080 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3081 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3084 if (temp_hdc)
3086 SelectObject(hdc, old_hbm);
3087 DeleteDC(hdc);
3090 if (temp_bitmap)
3091 DeleteObject(hbitmap);
3094 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3096 GpRectF rc;
3098 rc.X = srcx;
3099 rc.Y = srcy;
3100 rc.Width = srcwidth;
3101 rc.Height = srcheight;
3103 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3104 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3106 else
3108 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3109 return InvalidParameter;
3112 return Ok;
3115 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3116 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3117 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3118 DrawImageAbort callback, VOID * callbackData)
3120 GpPointF pointsF[3];
3121 INT i;
3123 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3124 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3125 callbackData);
3127 if(!points || count!=3)
3128 return InvalidParameter;
3130 for(i = 0; i < count; i++){
3131 pointsF[i].X = (REAL)points[i].X;
3132 pointsF[i].Y = (REAL)points[i].Y;
3135 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3136 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3137 callback, callbackData);
3140 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3141 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3142 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3143 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3144 VOID * callbackData)
3146 GpPointF points[3];
3148 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3149 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3150 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3152 points[0].X = dstx;
3153 points[0].Y = dsty;
3154 points[1].X = dstx + dstwidth;
3155 points[1].Y = dsty;
3156 points[2].X = dstx;
3157 points[2].Y = dsty + dstheight;
3159 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3160 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3163 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3164 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3165 INT srcwidth, INT srcheight, GpUnit srcUnit,
3166 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3167 VOID * callbackData)
3169 GpPointF points[3];
3171 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3172 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3173 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3175 points[0].X = dstx;
3176 points[0].Y = dsty;
3177 points[1].X = dstx + dstwidth;
3178 points[1].Y = dsty;
3179 points[2].X = dstx;
3180 points[2].Y = dsty + dstheight;
3182 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3183 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3186 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3187 REAL x, REAL y, REAL width, REAL height)
3189 RectF bounds;
3190 GpUnit unit;
3191 GpStatus ret;
3193 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3195 if(!graphics || !image)
3196 return InvalidParameter;
3198 ret = GdipGetImageBounds(image, &bounds, &unit);
3199 if(ret != Ok)
3200 return ret;
3202 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3203 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3204 unit, NULL, NULL, NULL);
3207 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3208 INT x, INT y, INT width, INT height)
3210 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3212 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3215 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3216 REAL y1, REAL x2, REAL y2)
3218 GpPointF pt[2];
3220 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3222 pt[0].X = x1;
3223 pt[0].Y = y1;
3224 pt[1].X = x2;
3225 pt[1].Y = y2;
3226 return GdipDrawLines(graphics, pen, pt, 2);
3229 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3230 INT y1, INT x2, INT y2)
3232 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3234 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3237 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3238 GpPointF *points, INT count)
3240 GpStatus status;
3241 GpPath *path;
3243 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3245 if(!pen || !graphics || (count < 2))
3246 return InvalidParameter;
3248 if(graphics->busy)
3249 return ObjectBusy;
3251 status = GdipCreatePath(FillModeAlternate, &path);
3252 if (status != Ok) return status;
3254 status = GdipAddPathLine2(path, points, count);
3255 if (status == Ok)
3256 status = GdipDrawPath(graphics, pen, path);
3258 GdipDeletePath(path);
3259 return status;
3262 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3263 GpPoint *points, INT count)
3265 GpStatus retval;
3266 GpPointF *ptf;
3267 int i;
3269 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3271 ptf = GdipAlloc(count * sizeof(GpPointF));
3272 if(!ptf) return OutOfMemory;
3274 for(i = 0; i < count; i ++){
3275 ptf[i].X = (REAL) points[i].X;
3276 ptf[i].Y = (REAL) points[i].Y;
3279 retval = GdipDrawLines(graphics, pen, ptf, count);
3281 GdipFree(ptf);
3282 return retval;
3285 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3287 INT save_state;
3288 GpStatus retval;
3289 HRGN hrgn=NULL;
3291 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3293 if(!pen || !graphics)
3294 return InvalidParameter;
3296 if(graphics->busy)
3297 return ObjectBusy;
3299 if (!graphics->hdc)
3301 FIXME("graphics object has no HDC\n");
3302 return Ok;
3305 save_state = prepare_dc(graphics, pen);
3307 retval = get_clip_hrgn(graphics, &hrgn);
3309 if (retval != Ok)
3310 goto end;
3312 if (hrgn)
3313 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3315 retval = draw_poly(graphics, pen, path->pathdata.Points,
3316 path->pathdata.Types, path->pathdata.Count, TRUE);
3318 end:
3319 restore_dc(graphics, save_state);
3320 DeleteObject(hrgn);
3322 return retval;
3325 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3326 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3328 GpStatus status;
3329 GpPath *path;
3331 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3332 width, height, startAngle, sweepAngle);
3334 if(!graphics || !pen)
3335 return InvalidParameter;
3337 if(graphics->busy)
3338 return ObjectBusy;
3340 status = GdipCreatePath(FillModeAlternate, &path);
3341 if (status != Ok) return status;
3343 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3344 if (status == Ok)
3345 status = GdipDrawPath(graphics, pen, path);
3347 GdipDeletePath(path);
3348 return status;
3351 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3352 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3354 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3355 width, height, startAngle, sweepAngle);
3357 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3360 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3361 REAL y, REAL width, REAL height)
3363 GpStatus status;
3364 GpPath *path;
3366 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3368 if(!pen || !graphics)
3369 return InvalidParameter;
3371 if(graphics->busy)
3372 return ObjectBusy;
3374 status = GdipCreatePath(FillModeAlternate, &path);
3375 if (status != Ok) return status;
3377 status = GdipAddPathRectangle(path, x, y, width, height);
3378 if (status == Ok)
3379 status = GdipDrawPath(graphics, pen, path);
3381 GdipDeletePath(path);
3382 return status;
3385 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3386 INT y, INT width, INT height)
3388 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3390 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3393 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3394 GDIPCONST GpRectF* rects, INT count)
3396 GpStatus status;
3397 GpPath *path;
3399 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3401 if(!graphics || !pen || !rects || count < 1)
3402 return InvalidParameter;
3404 if(graphics->busy)
3405 return ObjectBusy;
3407 status = GdipCreatePath(FillModeAlternate, &path);
3408 if (status != Ok) return status;
3410 status = GdipAddPathRectangles(path, rects, count);
3411 if (status == Ok)
3412 status = GdipDrawPath(graphics, pen, path);
3414 GdipDeletePath(path);
3415 return status;
3418 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3419 GDIPCONST GpRect* rects, INT count)
3421 GpRectF *rectsF;
3422 GpStatus ret;
3423 INT i;
3425 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3427 if(!rects || count<=0)
3428 return InvalidParameter;
3430 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3431 if(!rectsF)
3432 return OutOfMemory;
3434 for(i = 0;i < count;i++){
3435 rectsF[i].X = (REAL)rects[i].X;
3436 rectsF[i].Y = (REAL)rects[i].Y;
3437 rectsF[i].Width = (REAL)rects[i].Width;
3438 rectsF[i].Height = (REAL)rects[i].Height;
3441 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3442 GdipFree(rectsF);
3444 return ret;
3447 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3448 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3450 GpPath *path;
3451 GpStatus status;
3453 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3454 count, tension, fill);
3456 if(!graphics || !brush || !points)
3457 return InvalidParameter;
3459 if(graphics->busy)
3460 return ObjectBusy;
3462 if(count == 1) /* Do nothing */
3463 return Ok;
3465 status = GdipCreatePath(fill, &path);
3466 if (status != Ok) return status;
3468 status = GdipAddPathClosedCurve2(path, points, count, tension);
3469 if (status == Ok)
3470 status = GdipFillPath(graphics, brush, path);
3472 GdipDeletePath(path);
3473 return status;
3476 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3477 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3479 GpPointF *ptf;
3480 GpStatus stat;
3481 INT i;
3483 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3484 count, tension, fill);
3486 if(!points || count == 0)
3487 return InvalidParameter;
3489 if(count == 1) /* Do nothing */
3490 return Ok;
3492 ptf = GdipAlloc(sizeof(GpPointF)*count);
3493 if(!ptf)
3494 return OutOfMemory;
3496 for(i = 0;i < count;i++){
3497 ptf[i].X = (REAL)points[i].X;
3498 ptf[i].Y = (REAL)points[i].Y;
3501 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3503 GdipFree(ptf);
3505 return stat;
3508 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3509 GDIPCONST GpPointF *points, INT count)
3511 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3512 return GdipFillClosedCurve2(graphics, brush, points, count,
3513 0.5f, FillModeAlternate);
3516 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3517 GDIPCONST GpPoint *points, INT count)
3519 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3520 return GdipFillClosedCurve2I(graphics, brush, points, count,
3521 0.5f, FillModeAlternate);
3524 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3525 REAL y, REAL width, REAL height)
3527 GpStatus stat;
3528 GpPath *path;
3530 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3532 if(!graphics || !brush)
3533 return InvalidParameter;
3535 if(graphics->busy)
3536 return ObjectBusy;
3538 stat = GdipCreatePath(FillModeAlternate, &path);
3540 if (stat == Ok)
3542 stat = GdipAddPathEllipse(path, x, y, width, height);
3544 if (stat == Ok)
3545 stat = GdipFillPath(graphics, brush, path);
3547 GdipDeletePath(path);
3550 return stat;
3553 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3554 INT y, INT width, INT height)
3556 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3558 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3561 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3563 INT save_state;
3564 GpStatus retval;
3565 HRGN hrgn=NULL;
3567 if(!graphics->hdc || !brush_can_fill_path(brush))
3568 return NotImplemented;
3570 save_state = SaveDC(graphics->hdc);
3571 EndPath(graphics->hdc);
3572 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3573 : WINDING));
3575 retval = get_clip_hrgn(graphics, &hrgn);
3577 if (retval != Ok)
3578 goto end;
3580 if (hrgn)
3581 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3583 BeginPath(graphics->hdc);
3584 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3585 path->pathdata.Types, path->pathdata.Count, FALSE);
3587 if(retval != Ok)
3588 goto end;
3590 EndPath(graphics->hdc);
3591 brush_fill_path(graphics, brush);
3593 retval = Ok;
3595 end:
3596 RestoreDC(graphics->hdc, save_state);
3597 DeleteObject(hrgn);
3599 return retval;
3602 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3604 GpStatus stat;
3605 GpRegion *rgn;
3607 if (!brush_can_fill_pixels(brush))
3608 return NotImplemented;
3610 /* FIXME: This could probably be done more efficiently without regions. */
3612 stat = GdipCreateRegionPath(path, &rgn);
3614 if (stat == Ok)
3616 stat = GdipFillRegion(graphics, brush, rgn);
3618 GdipDeleteRegion(rgn);
3621 return stat;
3624 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3626 GpStatus stat = NotImplemented;
3628 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3630 if(!brush || !graphics || !path)
3631 return InvalidParameter;
3633 if(graphics->busy)
3634 return ObjectBusy;
3636 if (!graphics->image && !graphics->alpha_hdc)
3637 stat = GDI32_GdipFillPath(graphics, brush, path);
3639 if (stat == NotImplemented)
3640 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3642 if (stat == NotImplemented)
3644 FIXME("Not implemented for brushtype %i\n", brush->bt);
3645 stat = Ok;
3648 return stat;
3651 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3652 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3654 GpStatus stat;
3655 GpPath *path;
3657 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3658 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3660 if(!graphics || !brush)
3661 return InvalidParameter;
3663 if(graphics->busy)
3664 return ObjectBusy;
3666 stat = GdipCreatePath(FillModeAlternate, &path);
3668 if (stat == Ok)
3670 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3672 if (stat == Ok)
3673 stat = GdipFillPath(graphics, brush, path);
3675 GdipDeletePath(path);
3678 return stat;
3681 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3682 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3684 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3685 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3687 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3690 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3691 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3693 GpStatus stat;
3694 GpPath *path;
3696 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3698 if(!graphics || !brush || !points || !count)
3699 return InvalidParameter;
3701 if(graphics->busy)
3702 return ObjectBusy;
3704 stat = GdipCreatePath(fillMode, &path);
3706 if (stat == Ok)
3708 stat = GdipAddPathPolygon(path, points, count);
3710 if (stat == Ok)
3711 stat = GdipFillPath(graphics, brush, path);
3713 GdipDeletePath(path);
3716 return stat;
3719 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3720 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3722 GpStatus stat;
3723 GpPath *path;
3725 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3727 if(!graphics || !brush || !points || !count)
3728 return InvalidParameter;
3730 if(graphics->busy)
3731 return ObjectBusy;
3733 stat = GdipCreatePath(fillMode, &path);
3735 if (stat == Ok)
3737 stat = GdipAddPathPolygonI(path, points, count);
3739 if (stat == Ok)
3740 stat = GdipFillPath(graphics, brush, path);
3742 GdipDeletePath(path);
3745 return stat;
3748 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3749 GDIPCONST GpPointF *points, INT count)
3751 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3753 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3756 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3757 GDIPCONST GpPoint *points, INT count)
3759 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3761 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3764 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3765 REAL x, REAL y, REAL width, REAL height)
3767 GpRectF rect;
3769 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3771 rect.X = x;
3772 rect.Y = y;
3773 rect.Width = width;
3774 rect.Height = height;
3776 return GdipFillRectangles(graphics, brush, &rect, 1);
3779 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3780 INT x, INT y, INT width, INT height)
3782 GpRectF rect;
3784 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3786 rect.X = (REAL)x;
3787 rect.Y = (REAL)y;
3788 rect.Width = (REAL)width;
3789 rect.Height = (REAL)height;
3791 return GdipFillRectangles(graphics, brush, &rect, 1);
3794 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3795 INT count)
3797 GpStatus status;
3798 GpPath *path;
3800 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3802 if(!graphics || !brush || !rects || count <= 0)
3803 return InvalidParameter;
3805 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3807 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
3808 /* FIXME: Add gdi32 drawing. */
3809 return status;
3812 status = GdipCreatePath(FillModeAlternate, &path);
3813 if (status != Ok) return status;
3815 status = GdipAddPathRectangles(path, rects, count);
3816 if (status == Ok)
3817 status = GdipFillPath(graphics, brush, path);
3819 GdipDeletePath(path);
3820 return status;
3823 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3824 INT count)
3826 GpRectF *rectsF;
3827 GpStatus ret;
3828 INT i;
3830 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3832 if(!rects || count <= 0)
3833 return InvalidParameter;
3835 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3836 if(!rectsF)
3837 return OutOfMemory;
3839 for(i = 0; i < count; i++){
3840 rectsF[i].X = (REAL)rects[i].X;
3841 rectsF[i].Y = (REAL)rects[i].Y;
3842 rectsF[i].X = (REAL)rects[i].Width;
3843 rectsF[i].Height = (REAL)rects[i].Height;
3846 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3847 GdipFree(rectsF);
3849 return ret;
3852 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3853 GpRegion* region)
3855 INT save_state;
3856 GpStatus status;
3857 HRGN hrgn;
3858 RECT rc;
3860 if(!graphics->hdc || !brush_can_fill_path(brush))
3861 return NotImplemented;
3863 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3864 if(status != Ok)
3865 return status;
3867 save_state = SaveDC(graphics->hdc);
3868 EndPath(graphics->hdc);
3870 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3872 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3874 BeginPath(graphics->hdc);
3875 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3876 EndPath(graphics->hdc);
3878 brush_fill_path(graphics, brush);
3881 RestoreDC(graphics->hdc, save_state);
3883 DeleteObject(hrgn);
3885 return Ok;
3888 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3889 GpRegion* region)
3891 GpStatus stat;
3892 GpRegion *temp_region;
3893 GpMatrix world_to_device;
3894 GpRectF graphics_bounds;
3895 DWORD *pixel_data;
3896 HRGN hregion;
3897 RECT bound_rect;
3898 GpRect gp_bound_rect;
3900 if (!brush_can_fill_pixels(brush))
3901 return NotImplemented;
3903 stat = get_graphics_bounds(graphics, &graphics_bounds);
3905 if (stat == Ok)
3906 stat = GdipCloneRegion(region, &temp_region);
3908 if (stat == Ok)
3910 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3911 CoordinateSpaceWorld, &world_to_device);
3913 if (stat == Ok)
3914 stat = GdipTransformRegion(temp_region, &world_to_device);
3916 if (stat == Ok)
3917 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3919 if (stat == Ok)
3920 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
3922 GdipDeleteRegion(temp_region);
3925 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
3927 DeleteObject(hregion);
3928 return Ok;
3931 if (stat == Ok)
3933 gp_bound_rect.X = bound_rect.left;
3934 gp_bound_rect.Y = bound_rect.top;
3935 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
3936 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
3938 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
3939 if (!pixel_data)
3940 stat = OutOfMemory;
3942 if (stat == Ok)
3944 stat = brush_fill_pixels(graphics, brush, pixel_data,
3945 &gp_bound_rect, gp_bound_rect.Width);
3947 if (stat == Ok)
3948 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
3949 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
3950 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
3952 GdipFree(pixel_data);
3955 DeleteObject(hregion);
3958 return stat;
3961 /*****************************************************************************
3962 * GdipFillRegion [GDIPLUS.@]
3964 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3965 GpRegion* region)
3967 GpStatus stat = NotImplemented;
3969 TRACE("(%p, %p, %p)\n", graphics, brush, region);
3971 if (!(graphics && brush && region))
3972 return InvalidParameter;
3974 if(graphics->busy)
3975 return ObjectBusy;
3977 if (!graphics->image && !graphics->alpha_hdc)
3978 stat = GDI32_GdipFillRegion(graphics, brush, region);
3980 if (stat == NotImplemented)
3981 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
3983 if (stat == NotImplemented)
3985 FIXME("not implemented for brushtype %i\n", brush->bt);
3986 stat = Ok;
3989 return stat;
3992 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3994 TRACE("(%p,%u)\n", graphics, intention);
3996 if(!graphics)
3997 return InvalidParameter;
3999 if(graphics->busy)
4000 return ObjectBusy;
4002 /* We have no internal operation queue, so there's no need to clear it. */
4004 if (graphics->hdc)
4005 GdiFlush();
4007 return Ok;
4010 /*****************************************************************************
4011 * GdipGetClipBounds [GDIPLUS.@]
4013 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4015 GpStatus status;
4016 GpRegion *clip;
4018 TRACE("(%p, %p)\n", graphics, rect);
4020 if(!graphics)
4021 return InvalidParameter;
4023 if(graphics->busy)
4024 return ObjectBusy;
4026 status = GdipCreateRegion(&clip);
4027 if (status != Ok) return status;
4029 status = GdipGetClip(graphics, clip);
4030 if (status == Ok)
4031 status = GdipGetRegionBounds(clip, graphics, rect);
4033 GdipDeleteRegion(clip);
4034 return status;
4037 /*****************************************************************************
4038 * GdipGetClipBoundsI [GDIPLUS.@]
4040 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4042 TRACE("(%p, %p)\n", graphics, rect);
4044 if(!graphics)
4045 return InvalidParameter;
4047 if(graphics->busy)
4048 return ObjectBusy;
4050 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4053 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4054 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4055 CompositingMode *mode)
4057 TRACE("(%p, %p)\n", graphics, mode);
4059 if(!graphics || !mode)
4060 return InvalidParameter;
4062 if(graphics->busy)
4063 return ObjectBusy;
4065 *mode = graphics->compmode;
4067 return Ok;
4070 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4071 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4072 CompositingQuality *quality)
4074 TRACE("(%p, %p)\n", graphics, quality);
4076 if(!graphics || !quality)
4077 return InvalidParameter;
4079 if(graphics->busy)
4080 return ObjectBusy;
4082 *quality = graphics->compqual;
4084 return Ok;
4087 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4088 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4089 InterpolationMode *mode)
4091 TRACE("(%p, %p)\n", graphics, mode);
4093 if(!graphics || !mode)
4094 return InvalidParameter;
4096 if(graphics->busy)
4097 return ObjectBusy;
4099 *mode = graphics->interpolation;
4101 return Ok;
4104 /* FIXME: Need to handle color depths less than 24bpp */
4105 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4107 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4109 if(!graphics || !argb)
4110 return InvalidParameter;
4112 if(graphics->busy)
4113 return ObjectBusy;
4115 return Ok;
4118 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4120 TRACE("(%p, %p)\n", graphics, scale);
4122 if(!graphics || !scale)
4123 return InvalidParameter;
4125 if(graphics->busy)
4126 return ObjectBusy;
4128 *scale = graphics->scale;
4130 return Ok;
4133 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4135 TRACE("(%p, %p)\n", graphics, unit);
4137 if(!graphics || !unit)
4138 return InvalidParameter;
4140 if(graphics->busy)
4141 return ObjectBusy;
4143 *unit = graphics->unit;
4145 return Ok;
4148 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4149 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4150 *mode)
4152 TRACE("(%p, %p)\n", graphics, mode);
4154 if(!graphics || !mode)
4155 return InvalidParameter;
4157 if(graphics->busy)
4158 return ObjectBusy;
4160 *mode = graphics->pixeloffset;
4162 return Ok;
4165 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4166 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4168 TRACE("(%p, %p)\n", graphics, mode);
4170 if(!graphics || !mode)
4171 return InvalidParameter;
4173 if(graphics->busy)
4174 return ObjectBusy;
4176 *mode = graphics->smoothing;
4178 return Ok;
4181 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4183 TRACE("(%p, %p)\n", graphics, contrast);
4185 if(!graphics || !contrast)
4186 return InvalidParameter;
4188 *contrast = graphics->textcontrast;
4190 return Ok;
4193 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4194 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4195 TextRenderingHint *hint)
4197 TRACE("(%p, %p)\n", graphics, hint);
4199 if(!graphics || !hint)
4200 return InvalidParameter;
4202 if(graphics->busy)
4203 return ObjectBusy;
4205 *hint = graphics->texthint;
4207 return Ok;
4210 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4212 GpRegion *clip_rgn;
4213 GpStatus stat;
4215 TRACE("(%p, %p)\n", graphics, rect);
4217 if(!graphics || !rect)
4218 return InvalidParameter;
4220 if(graphics->busy)
4221 return ObjectBusy;
4223 /* intersect window and graphics clipping regions */
4224 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4225 return stat;
4227 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4228 goto cleanup;
4230 /* get bounds of the region */
4231 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4233 cleanup:
4234 GdipDeleteRegion(clip_rgn);
4236 return stat;
4239 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4241 GpRectF rectf;
4242 GpStatus stat;
4244 TRACE("(%p, %p)\n", graphics, rect);
4246 if(!graphics || !rect)
4247 return InvalidParameter;
4249 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4251 rect->X = gdip_round(rectf.X);
4252 rect->Y = gdip_round(rectf.Y);
4253 rect->Width = gdip_round(rectf.Width);
4254 rect->Height = gdip_round(rectf.Height);
4257 return stat;
4260 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4262 TRACE("(%p, %p)\n", graphics, matrix);
4264 if(!graphics || !matrix)
4265 return InvalidParameter;
4267 if(graphics->busy)
4268 return ObjectBusy;
4270 *matrix = graphics->worldtrans;
4271 return Ok;
4274 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4276 GpSolidFill *brush;
4277 GpStatus stat;
4278 GpRectF wnd_rect;
4280 TRACE("(%p, %x)\n", graphics, color);
4282 if(!graphics)
4283 return InvalidParameter;
4285 if(graphics->busy)
4286 return ObjectBusy;
4288 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4289 return stat;
4291 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4292 GdipDeleteBrush((GpBrush*)brush);
4293 return stat;
4296 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4297 wnd_rect.Width, wnd_rect.Height);
4299 GdipDeleteBrush((GpBrush*)brush);
4301 return Ok;
4304 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4306 TRACE("(%p, %p)\n", graphics, res);
4308 if(!graphics || !res)
4309 return InvalidParameter;
4311 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4314 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4316 GpStatus stat;
4317 GpRegion* rgn;
4318 GpPointF pt;
4320 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4322 if(!graphics || !result)
4323 return InvalidParameter;
4325 if(graphics->busy)
4326 return ObjectBusy;
4328 pt.X = x;
4329 pt.Y = y;
4330 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4331 CoordinateSpaceWorld, &pt, 1)) != Ok)
4332 return stat;
4334 if((stat = GdipCreateRegion(&rgn)) != Ok)
4335 return stat;
4337 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4338 goto cleanup;
4340 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4342 cleanup:
4343 GdipDeleteRegion(rgn);
4344 return stat;
4347 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4349 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4352 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4354 GpStatus stat;
4355 GpRegion* rgn;
4356 GpPointF pts[2];
4358 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4360 if(!graphics || !result)
4361 return InvalidParameter;
4363 if(graphics->busy)
4364 return ObjectBusy;
4366 pts[0].X = x;
4367 pts[0].Y = y;
4368 pts[1].X = x + width;
4369 pts[1].Y = y + height;
4371 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4372 CoordinateSpaceWorld, pts, 2)) != Ok)
4373 return stat;
4375 pts[1].X -= pts[0].X;
4376 pts[1].Y -= pts[0].Y;
4378 if((stat = GdipCreateRegion(&rgn)) != Ok)
4379 return stat;
4381 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4382 goto cleanup;
4384 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4386 cleanup:
4387 GdipDeleteRegion(rgn);
4388 return stat;
4391 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4393 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4396 GpStatus gdip_format_string(HDC hdc,
4397 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4398 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4399 gdip_format_string_callback callback, void *user_data)
4401 WCHAR* stringdup;
4402 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4403 nheight, lineend, lineno = 0;
4404 RectF bounds;
4405 StringAlignment halign;
4406 GpStatus stat = Ok;
4407 SIZE size;
4408 HotkeyPrefix hkprefix;
4409 INT *hotkeyprefix_offsets=NULL;
4410 INT hotkeyprefix_count=0;
4411 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4412 BOOL seen_prefix = FALSE;
4413 GpStringFormat *dyn_format=NULL;
4415 if(length == -1) length = lstrlenW(string);
4417 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4418 if(!stringdup) return OutOfMemory;
4420 if (!format)
4422 stat = GdipStringFormatGetGenericDefault(&dyn_format);
4423 if (stat != Ok)
4425 GdipFree(stringdup);
4426 return stat;
4428 format = dyn_format;
4431 nwidth = rect->Width;
4432 nheight = rect->Height;
4433 if (ignore_empty_clip)
4435 if (!nwidth) nwidth = INT_MAX;
4436 if (!nheight) nheight = INT_MAX;
4439 hkprefix = format->hkprefix;
4441 if (hkprefix == HotkeyPrefixShow)
4443 for (i=0; i<length; i++)
4445 if (string[i] == '&')
4446 hotkeyprefix_count++;
4450 if (hotkeyprefix_count)
4451 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4453 hotkeyprefix_count = 0;
4455 for(i = 0, j = 0; i < length; i++){
4456 /* FIXME: This makes the indexes passed to callback inaccurate. */
4457 if(!isprintW(string[i]) && (string[i] != '\n'))
4458 continue;
4460 /* FIXME: tabs should be handled using tabstops from stringformat */
4461 if (string[i] == '\t')
4462 continue;
4464 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4465 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4466 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4468 seen_prefix = TRUE;
4469 continue;
4472 seen_prefix = FALSE;
4474 stringdup[j] = string[i];
4475 j++;
4478 length = j;
4480 halign = format->align;
4482 while(sum < length){
4483 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4484 nwidth, &fit, NULL, &size);
4485 fitcpy = fit;
4487 if(fit == 0)
4488 break;
4490 for(lret = 0; lret < fit; lret++)
4491 if(*(stringdup + sum + lret) == '\n')
4492 break;
4494 /* Line break code (may look strange, but it imitates windows). */
4495 if(lret < fit)
4496 lineend = fit = lret; /* this is not an off-by-one error */
4497 else if(fit < (length - sum)){
4498 if(*(stringdup + sum + fit) == ' ')
4499 while(*(stringdup + sum + fit) == ' ')
4500 fit++;
4501 else
4502 while(*(stringdup + sum + fit - 1) != ' '){
4503 fit--;
4505 if(*(stringdup + sum + fit) == '\t')
4506 break;
4508 if(fit == 0){
4509 fit = fitcpy;
4510 break;
4513 lineend = fit;
4514 while(*(stringdup + sum + lineend - 1) == ' ' ||
4515 *(stringdup + sum + lineend - 1) == '\t')
4516 lineend--;
4518 else
4519 lineend = fit;
4521 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4522 nwidth, &j, NULL, &size);
4524 bounds.Width = size.cx;
4526 if(height + size.cy > nheight)
4528 if (format->attr & StringFormatFlagsLineLimit)
4529 break;
4530 bounds.Height = nheight - (height + size.cy);
4532 else
4533 bounds.Height = size.cy;
4535 bounds.Y = rect->Y + height;
4537 switch (halign)
4539 case StringAlignmentNear:
4540 default:
4541 bounds.X = rect->X;
4542 break;
4543 case StringAlignmentCenter:
4544 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4545 break;
4546 case StringAlignmentFar:
4547 bounds.X = rect->X + rect->Width - bounds.Width;
4548 break;
4551 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4552 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4553 break;
4555 stat = callback(hdc, stringdup, sum, lineend,
4556 font, rect, format, lineno, &bounds,
4557 &hotkeyprefix_offsets[hotkeyprefix_pos],
4558 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4560 if (stat != Ok)
4561 break;
4563 sum += fit + (lret < fitcpy ? 1 : 0);
4564 height += size.cy;
4565 lineno++;
4567 hotkeyprefix_pos = hotkeyprefix_end_pos;
4569 if(height > nheight)
4570 break;
4572 /* Stop if this was a linewrap (but not if it was a linebreak). */
4573 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
4574 break;
4577 GdipFree(stringdup);
4578 GdipFree(hotkeyprefix_offsets);
4579 GdipDeleteStringFormat(dyn_format);
4581 return stat;
4584 struct measure_ranges_args {
4585 GpRegion **regions;
4586 REAL rel_width, rel_height;
4589 static GpStatus measure_ranges_callback(HDC hdc,
4590 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4591 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4592 INT lineno, const RectF *bounds, INT *underlined_indexes,
4593 INT underlined_index_count, void *user_data)
4595 int i;
4596 GpStatus stat = Ok;
4597 struct measure_ranges_args *args = user_data;
4599 for (i=0; i<format->range_count; i++)
4601 INT range_start = max(index, format->character_ranges[i].First);
4602 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4603 if (range_start < range_end)
4605 GpRectF range_rect;
4606 SIZE range_size;
4608 range_rect.Y = bounds->Y / args->rel_height;
4609 range_rect.Height = bounds->Height / args->rel_height;
4611 GetTextExtentExPointW(hdc, string + index, range_start - index,
4612 INT_MAX, NULL, NULL, &range_size);
4613 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4615 GetTextExtentExPointW(hdc, string + index, range_end - index,
4616 INT_MAX, NULL, NULL, &range_size);
4617 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4619 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4620 if (stat != Ok)
4621 break;
4625 return stat;
4628 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4629 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4630 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4631 INT regionCount, GpRegion** regions)
4633 GpStatus stat;
4634 int i;
4635 HFONT gdifont, oldfont;
4636 struct measure_ranges_args args;
4637 HDC hdc, temp_hdc=NULL;
4638 GpPointF pt[3];
4639 RectF scaled_rect;
4640 REAL margin_x;
4642 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4643 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4645 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4646 return InvalidParameter;
4648 if (regionCount < stringFormat->range_count)
4649 return InvalidParameter;
4651 if(!graphics->hdc)
4653 hdc = temp_hdc = CreateCompatibleDC(0);
4654 if (!temp_hdc) return OutOfMemory;
4656 else
4657 hdc = graphics->hdc;
4659 if (stringFormat->attr)
4660 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4662 pt[0].X = 0.0;
4663 pt[0].Y = 0.0;
4664 pt[1].X = 1.0;
4665 pt[1].Y = 0.0;
4666 pt[2].X = 0.0;
4667 pt[2].Y = 1.0;
4668 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4669 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4670 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4671 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4672 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4674 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4675 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4677 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4678 scaled_rect.Y = layoutRect->Y * args.rel_height;
4679 scaled_rect.Width = layoutRect->Width * args.rel_width;
4680 scaled_rect.Height = layoutRect->Height * args.rel_height;
4682 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4683 oldfont = SelectObject(hdc, gdifont);
4685 for (i=0; i<stringFormat->range_count; i++)
4687 stat = GdipSetEmpty(regions[i]);
4688 if (stat != Ok)
4689 return stat;
4692 args.regions = regions;
4694 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4695 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4697 SelectObject(hdc, oldfont);
4698 DeleteObject(gdifont);
4700 if (temp_hdc)
4701 DeleteDC(temp_hdc);
4703 return stat;
4706 struct measure_string_args {
4707 RectF *bounds;
4708 INT *codepointsfitted;
4709 INT *linesfilled;
4710 REAL rel_width, rel_height;
4713 static GpStatus measure_string_callback(HDC hdc,
4714 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4715 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4716 INT lineno, const RectF *bounds, INT *underlined_indexes,
4717 INT underlined_index_count, void *user_data)
4719 struct measure_string_args *args = user_data;
4720 REAL new_width, new_height;
4722 new_width = bounds->Width / args->rel_width;
4723 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4725 if (new_width > args->bounds->Width)
4726 args->bounds->Width = new_width;
4728 if (new_height > args->bounds->Height)
4729 args->bounds->Height = new_height;
4731 if (args->codepointsfitted)
4732 *args->codepointsfitted = index + length;
4734 if (args->linesfilled)
4735 (*args->linesfilled)++;
4737 return Ok;
4740 /* Find the smallest rectangle that bounds the text when it is printed in rect
4741 * according to the format options listed in format. If rect has 0 width and
4742 * height, then just find the smallest rectangle that bounds the text when it's
4743 * printed at location (rect->X, rect-Y). */
4744 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4745 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4746 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4747 INT *codepointsfitted, INT *linesfilled)
4749 HFONT oldfont, gdifont;
4750 struct measure_string_args args;
4751 HDC temp_hdc=NULL, hdc;
4752 GpPointF pt[3];
4753 RectF scaled_rect;
4754 REAL margin_x;
4755 INT lines, glyphs;
4757 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4758 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4759 bounds, codepointsfitted, linesfilled);
4761 if(!graphics || !string || !font || !rect || !bounds)
4762 return InvalidParameter;
4764 if(!graphics->hdc)
4766 hdc = temp_hdc = CreateCompatibleDC(0);
4767 if (!temp_hdc) return OutOfMemory;
4769 else
4770 hdc = graphics->hdc;
4772 if(linesfilled) *linesfilled = 0;
4773 if(codepointsfitted) *codepointsfitted = 0;
4775 if(format)
4776 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4778 pt[0].X = 0.0;
4779 pt[0].Y = 0.0;
4780 pt[1].X = 1.0;
4781 pt[1].Y = 0.0;
4782 pt[2].X = 0.0;
4783 pt[2].Y = 1.0;
4784 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4785 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4786 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4787 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4788 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4790 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4791 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4793 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4794 scaled_rect.Y = rect->Y * args.rel_height;
4795 scaled_rect.Width = rect->Width * args.rel_width;
4796 scaled_rect.Height = rect->Height * args.rel_height;
4797 if (scaled_rect.Width >= 0.5)
4799 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4800 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4803 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4804 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4806 get_font_hfont(graphics, font, format, &gdifont, NULL);
4807 oldfont = SelectObject(hdc, gdifont);
4809 bounds->X = rect->X;
4810 bounds->Y = rect->Y;
4811 bounds->Width = 0.0;
4812 bounds->Height = 0.0;
4814 args.bounds = bounds;
4815 args.codepointsfitted = &glyphs;
4816 args.linesfilled = &lines;
4817 lines = glyphs = 0;
4819 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4820 measure_string_callback, &args);
4822 if (linesfilled) *linesfilled = lines;
4823 if (codepointsfitted) *codepointsfitted = glyphs;
4825 if (lines)
4826 bounds->Width += margin_x * 2.0;
4828 SelectObject(hdc, oldfont);
4829 DeleteObject(gdifont);
4831 if (temp_hdc)
4832 DeleteDC(temp_hdc);
4834 return Ok;
4837 struct draw_string_args {
4838 GpGraphics *graphics;
4839 GDIPCONST GpBrush *brush;
4840 REAL x, y, rel_width, rel_height, ascent;
4843 static GpStatus draw_string_callback(HDC hdc,
4844 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4845 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4846 INT lineno, const RectF *bounds, INT *underlined_indexes,
4847 INT underlined_index_count, void *user_data)
4849 struct draw_string_args *args = user_data;
4850 PointF position;
4851 GpStatus stat;
4853 position.X = args->x + bounds->X / args->rel_width;
4854 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4856 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4857 args->brush, &position,
4858 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4860 if (stat == Ok && underlined_index_count)
4862 OUTLINETEXTMETRICW otm;
4863 REAL underline_y, underline_height;
4864 int i;
4866 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4868 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4869 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4871 for (i=0; i<underlined_index_count; i++)
4873 REAL start_x, end_x;
4874 SIZE text_size;
4875 INT ofs = underlined_indexes[i] - index;
4877 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4878 start_x = text_size.cx / args->rel_width;
4880 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4881 end_x = text_size.cx / args->rel_width;
4883 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4887 return stat;
4890 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4891 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4892 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4894 HRGN rgn = NULL;
4895 HFONT gdifont;
4896 GpPointF pt[3], rectcpy[4];
4897 POINT corners[4];
4898 REAL rel_width, rel_height, margin_x;
4899 INT save_state, format_flags = 0;
4900 REAL offsety = 0.0;
4901 struct draw_string_args args;
4902 RectF scaled_rect;
4903 HDC hdc, temp_hdc=NULL;
4904 TEXTMETRICW textmetric;
4906 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4907 length, font, debugstr_rectf(rect), format, brush);
4909 if(!graphics || !string || !font || !brush || !rect)
4910 return InvalidParameter;
4912 if(graphics->hdc)
4914 hdc = graphics->hdc;
4916 else
4918 hdc = temp_hdc = CreateCompatibleDC(0);
4921 if(format){
4922 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4924 format_flags = format->attr;
4926 /* Should be no need to explicitly test for StringAlignmentNear as
4927 * that is default behavior if no alignment is passed. */
4928 if(format->vertalign != StringAlignmentNear){
4929 RectF bounds, in_rect = *rect;
4930 in_rect.Height = 0.0; /* avoid height clipping */
4931 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
4933 TRACE("bounds %s\n", debugstr_rectf(&bounds));
4935 if(format->vertalign == StringAlignmentCenter)
4936 offsety = (rect->Height - bounds.Height) / 2;
4937 else if(format->vertalign == StringAlignmentFar)
4938 offsety = (rect->Height - bounds.Height);
4940 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
4943 save_state = SaveDC(hdc);
4945 pt[0].X = 0.0;
4946 pt[0].Y = 0.0;
4947 pt[1].X = 1.0;
4948 pt[1].Y = 0.0;
4949 pt[2].X = 0.0;
4950 pt[2].Y = 1.0;
4951 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4952 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4953 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4954 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4955 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4957 rectcpy[3].X = rectcpy[0].X = rect->X;
4958 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
4959 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4960 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
4961 transform_and_round_points(graphics, corners, rectcpy, 4);
4963 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4964 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4966 scaled_rect.X = margin_x * rel_width;
4967 scaled_rect.Y = 0.0;
4968 scaled_rect.Width = rel_width * rect->Width;
4969 scaled_rect.Height = rel_height * rect->Height;
4970 if (scaled_rect.Width >= 0.5)
4972 scaled_rect.Width -= margin_x * 2.0 * rel_width;
4973 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4976 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4977 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4979 if (!(format_flags & StringFormatFlagsNoClip) &&
4980 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
4981 rect->Width > 0.0 && rect->Height > 0.0)
4983 /* FIXME: If only the width or only the height is 0, we should probably still clip */
4984 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4985 SelectClipRgn(hdc, rgn);
4988 get_font_hfont(graphics, font, format, &gdifont, NULL);
4989 SelectObject(hdc, gdifont);
4991 args.graphics = graphics;
4992 args.brush = brush;
4994 args.x = rect->X;
4995 args.y = rect->Y + offsety;
4997 args.rel_width = rel_width;
4998 args.rel_height = rel_height;
5000 GetTextMetricsW(hdc, &textmetric);
5001 args.ascent = textmetric.tmAscent / rel_height;
5003 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5004 draw_string_callback, &args);
5006 DeleteObject(rgn);
5007 DeleteObject(gdifont);
5009 RestoreDC(hdc, save_state);
5011 DeleteDC(temp_hdc);
5013 return Ok;
5016 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5018 TRACE("(%p)\n", graphics);
5020 if(!graphics)
5021 return InvalidParameter;
5023 if(graphics->busy)
5024 return ObjectBusy;
5026 return GdipSetInfinite(graphics->clip);
5029 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5031 TRACE("(%p)\n", graphics);
5033 if(!graphics)
5034 return InvalidParameter;
5036 if(graphics->busy)
5037 return ObjectBusy;
5039 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5042 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5044 return GdipEndContainer(graphics, state);
5047 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5048 GpMatrixOrder order)
5050 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5052 if(!graphics)
5053 return InvalidParameter;
5055 if(graphics->busy)
5056 return ObjectBusy;
5058 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5061 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5063 return GdipBeginContainer2(graphics, state);
5066 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5067 GraphicsContainer *state)
5069 GraphicsContainerItem *container;
5070 GpStatus sts;
5072 TRACE("(%p, %p)\n", graphics, state);
5074 if(!graphics || !state)
5075 return InvalidParameter;
5077 sts = init_container(&container, graphics);
5078 if(sts != Ok)
5079 return sts;
5081 list_add_head(&graphics->containers, &container->entry);
5082 *state = graphics->contid = container->contid;
5084 return Ok;
5087 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5089 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5090 return NotImplemented;
5093 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5095 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5096 return NotImplemented;
5099 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5101 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5102 return NotImplemented;
5105 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5107 GpStatus sts;
5108 GraphicsContainerItem *container, *container2;
5110 TRACE("(%p, %x)\n", graphics, state);
5112 if(!graphics)
5113 return InvalidParameter;
5115 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5116 if(container->contid == state)
5117 break;
5120 /* did not find a matching container */
5121 if(&container->entry == &graphics->containers)
5122 return Ok;
5124 sts = restore_container(graphics, container);
5125 if(sts != Ok)
5126 return sts;
5128 /* remove all of the containers on top of the found container */
5129 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5130 if(container->contid == state)
5131 break;
5132 list_remove(&container->entry);
5133 delete_container(container);
5136 list_remove(&container->entry);
5137 delete_container(container);
5139 return Ok;
5142 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5143 REAL sy, GpMatrixOrder order)
5145 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5147 if(!graphics)
5148 return InvalidParameter;
5150 if(graphics->busy)
5151 return ObjectBusy;
5153 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5156 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5157 CombineMode mode)
5159 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5161 if(!graphics || !srcgraphics)
5162 return InvalidParameter;
5164 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5167 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5168 CompositingMode mode)
5170 TRACE("(%p, %d)\n", graphics, mode);
5172 if(!graphics)
5173 return InvalidParameter;
5175 if(graphics->busy)
5176 return ObjectBusy;
5178 graphics->compmode = mode;
5180 return Ok;
5183 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5184 CompositingQuality quality)
5186 TRACE("(%p, %d)\n", graphics, quality);
5188 if(!graphics)
5189 return InvalidParameter;
5191 if(graphics->busy)
5192 return ObjectBusy;
5194 graphics->compqual = quality;
5196 return Ok;
5199 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5200 InterpolationMode mode)
5202 TRACE("(%p, %d)\n", graphics, mode);
5204 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5205 return InvalidParameter;
5207 if(graphics->busy)
5208 return ObjectBusy;
5210 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5211 mode = InterpolationModeBilinear;
5213 if (mode == InterpolationModeHighQuality)
5214 mode = InterpolationModeHighQualityBicubic;
5216 graphics->interpolation = mode;
5218 return Ok;
5221 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5223 GpStatus stat;
5225 TRACE("(%p, %.2f)\n", graphics, scale);
5227 if(!graphics || (scale <= 0.0))
5228 return InvalidParameter;
5230 if(graphics->busy)
5231 return ObjectBusy;
5233 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5235 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5236 if (stat != Ok)
5237 return stat;
5240 graphics->scale = scale;
5242 return Ok;
5245 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5247 GpStatus stat;
5249 TRACE("(%p, %d)\n", graphics, unit);
5251 if(!graphics)
5252 return InvalidParameter;
5254 if(graphics->busy)
5255 return ObjectBusy;
5257 if(unit == UnitWorld)
5258 return InvalidParameter;
5260 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5262 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5263 if (stat != Ok)
5264 return stat;
5267 graphics->unit = unit;
5269 return Ok;
5272 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5273 mode)
5275 TRACE("(%p, %d)\n", graphics, mode);
5277 if(!graphics)
5278 return InvalidParameter;
5280 if(graphics->busy)
5281 return ObjectBusy;
5283 graphics->pixeloffset = mode;
5285 return Ok;
5288 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5290 static int calls;
5292 TRACE("(%p,%i,%i)\n", graphics, x, y);
5294 if (!(calls++))
5295 FIXME("value is unused in rendering\n");
5297 if (!graphics)
5298 return InvalidParameter;
5300 graphics->origin_x = x;
5301 graphics->origin_y = y;
5303 return Ok;
5306 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5308 TRACE("(%p,%p,%p)\n", graphics, x, y);
5310 if (!graphics || !x || !y)
5311 return InvalidParameter;
5313 *x = graphics->origin_x;
5314 *y = graphics->origin_y;
5316 return Ok;
5319 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5321 TRACE("(%p, %d)\n", graphics, mode);
5323 if(!graphics)
5324 return InvalidParameter;
5326 if(graphics->busy)
5327 return ObjectBusy;
5329 graphics->smoothing = mode;
5331 return Ok;
5334 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5336 TRACE("(%p, %d)\n", graphics, contrast);
5338 if(!graphics)
5339 return InvalidParameter;
5341 graphics->textcontrast = contrast;
5343 return Ok;
5346 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5347 TextRenderingHint hint)
5349 TRACE("(%p, %d)\n", graphics, hint);
5351 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5352 return InvalidParameter;
5354 if(graphics->busy)
5355 return ObjectBusy;
5357 graphics->texthint = hint;
5359 return Ok;
5362 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5364 TRACE("(%p, %p)\n", graphics, matrix);
5366 if(!graphics || !matrix)
5367 return InvalidParameter;
5369 if(graphics->busy)
5370 return ObjectBusy;
5372 TRACE("%f,%f,%f,%f,%f,%f\n",
5373 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5374 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5376 graphics->worldtrans = *matrix;
5378 return Ok;
5381 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5382 REAL dy, GpMatrixOrder order)
5384 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5386 if(!graphics)
5387 return InvalidParameter;
5389 if(graphics->busy)
5390 return ObjectBusy;
5392 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5395 /*****************************************************************************
5396 * GdipSetClipHrgn [GDIPLUS.@]
5398 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5400 GpRegion *region;
5401 GpStatus status;
5403 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5405 if(!graphics)
5406 return InvalidParameter;
5408 if(graphics->busy)
5409 return ObjectBusy;
5411 /* hrgn is already in device units */
5412 status = GdipCreateRegionHrgn(hrgn, &region);
5413 if(status != Ok)
5414 return status;
5416 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5418 GdipDeleteRegion(region);
5419 return status;
5422 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5424 GpStatus status;
5425 GpPath *clip_path;
5427 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5429 if(!graphics)
5430 return InvalidParameter;
5432 if(graphics->busy)
5433 return ObjectBusy;
5435 status = GdipClonePath(path, &clip_path);
5436 if (status == Ok)
5438 GpMatrix world_to_device;
5440 get_graphics_transform(graphics, CoordinateSpaceDevice,
5441 CoordinateSpaceWorld, &world_to_device);
5442 status = GdipTransformPath(clip_path, &world_to_device);
5443 if (status == Ok)
5444 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5446 GdipDeletePath(clip_path);
5448 return status;
5451 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5452 REAL width, REAL height,
5453 CombineMode mode)
5455 GpStatus status;
5456 GpRectF rect;
5457 GpRegion *region;
5459 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5461 if(!graphics)
5462 return InvalidParameter;
5464 if(graphics->busy)
5465 return ObjectBusy;
5467 rect.X = x;
5468 rect.Y = y;
5469 rect.Width = width;
5470 rect.Height = height;
5471 status = GdipCreateRegionRect(&rect, &region);
5472 if (status == Ok)
5474 GpMatrix world_to_device;
5476 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5477 status = GdipTransformRegion(region, &world_to_device);
5478 if (status == Ok)
5479 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5481 GdipDeleteRegion(region);
5483 return status;
5486 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5487 INT width, INT height,
5488 CombineMode mode)
5490 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5492 if(!graphics)
5493 return InvalidParameter;
5495 if(graphics->busy)
5496 return ObjectBusy;
5498 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5501 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5502 CombineMode mode)
5504 GpStatus status;
5505 GpRegion *clip;
5507 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5509 if(!graphics || !region)
5510 return InvalidParameter;
5512 if(graphics->busy)
5513 return ObjectBusy;
5515 status = GdipCloneRegion(region, &clip);
5516 if (status == Ok)
5518 GpMatrix world_to_device;
5520 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5521 status = GdipTransformRegion(clip, &world_to_device);
5522 if (status == Ok)
5523 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5525 GdipDeleteRegion(clip);
5527 return status;
5530 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5531 INT count)
5533 INT save_state;
5534 POINT *pti;
5536 TRACE("(%p, %p, %d)\n", graphics, points, count);
5538 if(!graphics || !pen || count<=0)
5539 return InvalidParameter;
5541 if(graphics->busy)
5542 return ObjectBusy;
5544 if (!graphics->hdc)
5546 FIXME("graphics object has no HDC\n");
5547 return Ok;
5550 pti = GdipAlloc(sizeof(POINT) * count);
5552 save_state = prepare_dc(graphics, pen);
5553 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5555 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5556 Polygon(graphics->hdc, pti, count);
5558 restore_dc(graphics, save_state);
5559 GdipFree(pti);
5561 return Ok;
5564 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5565 INT count)
5567 GpStatus ret;
5568 GpPointF *ptf;
5569 INT i;
5571 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5573 if(count<=0) return InvalidParameter;
5574 ptf = GdipAlloc(sizeof(GpPointF) * count);
5576 for(i = 0;i < count; i++){
5577 ptf[i].X = (REAL)points[i].X;
5578 ptf[i].Y = (REAL)points[i].Y;
5581 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5582 GdipFree(ptf);
5584 return ret;
5587 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5589 TRACE("(%p, %p)\n", graphics, dpi);
5591 if(!graphics || !dpi)
5592 return InvalidParameter;
5594 if(graphics->busy)
5595 return ObjectBusy;
5597 *dpi = graphics->xres;
5598 return Ok;
5601 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5603 TRACE("(%p, %p)\n", graphics, dpi);
5605 if(!graphics || !dpi)
5606 return InvalidParameter;
5608 if(graphics->busy)
5609 return ObjectBusy;
5611 *dpi = graphics->yres;
5612 return Ok;
5615 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5616 GpMatrixOrder order)
5618 GpMatrix m;
5619 GpStatus ret;
5621 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5623 if(!graphics || !matrix)
5624 return InvalidParameter;
5626 if(graphics->busy)
5627 return ObjectBusy;
5629 m = graphics->worldtrans;
5631 ret = GdipMultiplyMatrix(&m, matrix, order);
5632 if(ret == Ok)
5633 graphics->worldtrans = m;
5635 return ret;
5638 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5639 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5641 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5643 GpStatus stat=Ok;
5645 TRACE("(%p, %p)\n", graphics, hdc);
5647 if(!graphics || !hdc)
5648 return InvalidParameter;
5650 if(graphics->busy)
5651 return ObjectBusy;
5653 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5655 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5657 else if (!graphics->hdc || graphics->alpha_hdc ||
5658 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5660 /* Create a fake HDC and fill it with a constant color. */
5661 HDC temp_hdc;
5662 HBITMAP hbitmap;
5663 GpRectF bounds;
5664 BITMAPINFOHEADER bmih;
5665 int i;
5667 stat = get_graphics_bounds(graphics, &bounds);
5668 if (stat != Ok)
5669 return stat;
5671 graphics->temp_hbitmap_width = bounds.Width;
5672 graphics->temp_hbitmap_height = bounds.Height;
5674 bmih.biSize = sizeof(bmih);
5675 bmih.biWidth = graphics->temp_hbitmap_width;
5676 bmih.biHeight = -graphics->temp_hbitmap_height;
5677 bmih.biPlanes = 1;
5678 bmih.biBitCount = 32;
5679 bmih.biCompression = BI_RGB;
5680 bmih.biSizeImage = 0;
5681 bmih.biXPelsPerMeter = 0;
5682 bmih.biYPelsPerMeter = 0;
5683 bmih.biClrUsed = 0;
5684 bmih.biClrImportant = 0;
5686 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5687 (void**)&graphics->temp_bits, NULL, 0);
5688 if (!hbitmap)
5689 return GenericError;
5691 temp_hdc = CreateCompatibleDC(0);
5692 if (!temp_hdc)
5694 DeleteObject(hbitmap);
5695 return GenericError;
5698 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5699 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5701 SelectObject(temp_hdc, hbitmap);
5703 graphics->temp_hbitmap = hbitmap;
5704 *hdc = graphics->temp_hdc = temp_hdc;
5706 else
5708 *hdc = graphics->hdc;
5711 if (stat == Ok)
5712 graphics->busy = TRUE;
5714 return stat;
5717 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5719 GpStatus stat=Ok;
5721 TRACE("(%p, %p)\n", graphics, hdc);
5723 if(!graphics || !hdc || !graphics->busy)
5724 return InvalidParameter;
5726 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5728 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5730 else if (graphics->temp_hdc == hdc)
5732 DWORD* pos;
5733 int i;
5735 /* Find the pixels that have changed, and mark them as opaque. */
5736 pos = (DWORD*)graphics->temp_bits;
5737 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5739 if (*pos != DC_BACKGROUND_KEY)
5741 *pos |= 0xff000000;
5743 pos++;
5746 /* Write the changed pixels to the real target. */
5747 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5748 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5749 graphics->temp_hbitmap_width * 4);
5751 /* Clean up. */
5752 DeleteDC(graphics->temp_hdc);
5753 DeleteObject(graphics->temp_hbitmap);
5754 graphics->temp_hdc = NULL;
5755 graphics->temp_hbitmap = NULL;
5757 else if (hdc != graphics->hdc)
5759 stat = InvalidParameter;
5762 if (stat == Ok)
5763 graphics->busy = FALSE;
5765 return stat;
5768 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5770 GpRegion *clip;
5771 GpStatus status;
5772 GpMatrix device_to_world;
5774 TRACE("(%p, %p)\n", graphics, region);
5776 if(!graphics || !region)
5777 return InvalidParameter;
5779 if(graphics->busy)
5780 return ObjectBusy;
5782 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5783 return status;
5785 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5786 status = GdipTransformRegion(clip, &device_to_world);
5787 if (status != Ok)
5789 GdipDeleteRegion(clip);
5790 return status;
5793 /* free everything except root node and header */
5794 delete_element(&region->node);
5795 memcpy(region, clip, sizeof(GpRegion));
5796 GdipFree(clip);
5798 return Ok;
5801 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5802 GpCoordinateSpace src_space, GpMatrix *matrix)
5804 GpStatus stat = Ok;
5805 REAL scale_x, scale_y;
5807 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5809 if (dst_space != src_space)
5811 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5812 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5814 if(graphics->unit != UnitDisplay)
5816 scale_x *= graphics->scale;
5817 scale_y *= graphics->scale;
5820 /* transform from src_space to CoordinateSpacePage */
5821 switch (src_space)
5823 case CoordinateSpaceWorld:
5824 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5825 break;
5826 case CoordinateSpacePage:
5827 break;
5828 case CoordinateSpaceDevice:
5829 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5830 break;
5833 /* transform from CoordinateSpacePage to dst_space */
5834 switch (dst_space)
5836 case CoordinateSpaceWorld:
5838 GpMatrix inverted_transform = graphics->worldtrans;
5839 stat = GdipInvertMatrix(&inverted_transform);
5840 if (stat == Ok)
5841 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5842 break;
5844 case CoordinateSpacePage:
5845 break;
5846 case CoordinateSpaceDevice:
5847 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5848 break;
5851 return stat;
5854 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5855 GpCoordinateSpace src_space, GpPointF *points, INT count)
5857 GpMatrix matrix;
5858 GpStatus stat;
5860 if(!graphics || !points || count <= 0)
5861 return InvalidParameter;
5863 if(graphics->busy)
5864 return ObjectBusy;
5866 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5868 if (src_space == dst_space) return Ok;
5870 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5871 if (stat != Ok) return stat;
5873 return GdipTransformMatrixPoints(&matrix, points, count);
5876 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5877 GpCoordinateSpace src_space, GpPoint *points, INT count)
5879 GpPointF *pointsF;
5880 GpStatus ret;
5881 INT i;
5883 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5885 if(count <= 0)
5886 return InvalidParameter;
5888 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5889 if(!pointsF)
5890 return OutOfMemory;
5892 for(i = 0; i < count; i++){
5893 pointsF[i].X = (REAL)points[i].X;
5894 pointsF[i].Y = (REAL)points[i].Y;
5897 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5899 if(ret == Ok)
5900 for(i = 0; i < count; i++){
5901 points[i].X = gdip_round(pointsF[i].X);
5902 points[i].Y = gdip_round(pointsF[i].Y);
5904 GdipFree(pointsF);
5906 return ret;
5909 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5911 static int calls;
5913 TRACE("\n");
5915 if (!calls++)
5916 FIXME("stub\n");
5918 return NULL;
5921 /*****************************************************************************
5922 * GdipTranslateClip [GDIPLUS.@]
5924 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5926 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5928 if(!graphics)
5929 return InvalidParameter;
5931 if(graphics->busy)
5932 return ObjectBusy;
5934 return GdipTranslateRegion(graphics->clip, dx, dy);
5937 /*****************************************************************************
5938 * GdipTranslateClipI [GDIPLUS.@]
5940 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5942 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5944 if(!graphics)
5945 return InvalidParameter;
5947 if(graphics->busy)
5948 return ObjectBusy;
5950 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5954 /*****************************************************************************
5955 * GdipMeasureDriverString [GDIPLUS.@]
5957 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5958 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5959 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5961 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
5962 HFONT hfont;
5963 HDC hdc;
5964 REAL min_x, min_y, max_x, max_y, x, y;
5965 int i;
5966 TEXTMETRICW textmetric;
5967 const WORD *glyph_indices;
5968 WORD *dynamic_glyph_indices=NULL;
5969 REAL rel_width, rel_height, ascent, descent;
5970 GpPointF pt[3];
5972 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5974 if (!graphics || !text || !font || !positions || !boundingBox)
5975 return InvalidParameter;
5977 if (length == -1)
5978 length = strlenW(text);
5980 if (length == 0)
5982 boundingBox->X = 0.0;
5983 boundingBox->Y = 0.0;
5984 boundingBox->Width = 0.0;
5985 boundingBox->Height = 0.0;
5988 if (flags & unsupported_flags)
5989 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
5991 get_font_hfont(graphics, font, NULL, &hfont, matrix);
5993 hdc = CreateCompatibleDC(0);
5994 SelectObject(hdc, hfont);
5996 GetTextMetricsW(hdc, &textmetric);
5998 pt[0].X = 0.0;
5999 pt[0].Y = 0.0;
6000 pt[1].X = 1.0;
6001 pt[1].Y = 0.0;
6002 pt[2].X = 0.0;
6003 pt[2].Y = 1.0;
6004 if (matrix)
6006 GpMatrix xform = *matrix;
6007 GdipTransformMatrixPoints(&xform, pt, 3);
6009 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6010 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6011 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6012 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6013 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6015 if (flags & DriverStringOptionsCmapLookup)
6017 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6018 if (!glyph_indices)
6020 DeleteDC(hdc);
6021 DeleteObject(hfont);
6022 return OutOfMemory;
6025 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6027 else
6028 glyph_indices = text;
6030 min_x = max_x = x = positions[0].X;
6031 min_y = max_y = y = positions[0].Y;
6033 ascent = textmetric.tmAscent / rel_height;
6034 descent = textmetric.tmDescent / rel_height;
6036 for (i=0; i<length; i++)
6038 int char_width;
6039 ABC abc;
6041 if (!(flags & DriverStringOptionsRealizedAdvance))
6043 x = positions[i].X;
6044 y = positions[i].Y;
6047 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6048 char_width = abc.abcA + abc.abcB + abc.abcC;
6050 if (min_y > y - ascent) min_y = y - ascent;
6051 if (max_y < y + descent) max_y = y + descent;
6052 if (min_x > x) min_x = x;
6054 x += char_width / rel_width;
6056 if (max_x < x) max_x = x;
6059 GdipFree(dynamic_glyph_indices);
6060 DeleteDC(hdc);
6061 DeleteObject(hfont);
6063 boundingBox->X = min_x;
6064 boundingBox->Y = min_y;
6065 boundingBox->Width = max_x - min_x;
6066 boundingBox->Height = max_y - min_y;
6068 return Ok;
6071 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6072 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6073 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6074 INT flags, GDIPCONST GpMatrix *matrix)
6076 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6077 INT save_state;
6078 GpPointF pt;
6079 HFONT hfont;
6080 UINT eto_flags=0;
6082 if (flags & unsupported_flags)
6083 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6085 if (!(flags & DriverStringOptionsCmapLookup))
6086 eto_flags |= ETO_GLYPH_INDEX;
6088 save_state = SaveDC(graphics->hdc);
6089 SetBkMode(graphics->hdc, TRANSPARENT);
6090 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6092 pt = positions[0];
6093 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6095 get_font_hfont(graphics, font, format, &hfont, matrix);
6096 SelectObject(graphics->hdc, hfont);
6098 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6100 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6102 RestoreDC(graphics->hdc, save_state);
6104 DeleteObject(hfont);
6106 return Ok;
6109 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6110 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6111 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6112 INT flags, GDIPCONST GpMatrix *matrix)
6114 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6115 GpStatus stat;
6116 PointF *real_positions, real_position;
6117 POINT *pti;
6118 HFONT hfont;
6119 HDC hdc;
6120 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6121 DWORD max_glyphsize=0;
6122 GLYPHMETRICS glyphmetrics;
6123 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6124 BYTE *glyph_mask;
6125 BYTE *text_mask;
6126 int text_mask_stride;
6127 BYTE *pixel_data;
6128 int pixel_data_stride;
6129 GpRect pixel_area;
6130 UINT ggo_flags = GGO_GRAY8_BITMAP;
6132 if (length <= 0)
6133 return Ok;
6135 if (!(flags & DriverStringOptionsCmapLookup))
6136 ggo_flags |= GGO_GLYPH_INDEX;
6138 if (flags & unsupported_flags)
6139 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6141 pti = GdipAlloc(sizeof(POINT) * length);
6142 if (!pti)
6143 return OutOfMemory;
6145 if (flags & DriverStringOptionsRealizedAdvance)
6147 real_position = positions[0];
6149 transform_and_round_points(graphics, pti, &real_position, 1);
6151 else
6153 real_positions = GdipAlloc(sizeof(PointF) * length);
6154 if (!real_positions)
6156 GdipFree(pti);
6157 return OutOfMemory;
6160 memcpy(real_positions, positions, sizeof(PointF) * length);
6162 transform_and_round_points(graphics, pti, real_positions, length);
6164 GdipFree(real_positions);
6167 get_font_hfont(graphics, font, format, &hfont, matrix);
6169 hdc = CreateCompatibleDC(0);
6170 SelectObject(hdc, hfont);
6172 /* Get the boundaries of the text to be drawn */
6173 for (i=0; i<length; i++)
6175 DWORD glyphsize;
6176 int left, top, right, bottom;
6178 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6179 &glyphmetrics, 0, NULL, &identity);
6181 if (glyphsize == GDI_ERROR)
6183 ERR("GetGlyphOutlineW failed\n");
6184 GdipFree(pti);
6185 DeleteDC(hdc);
6186 DeleteObject(hfont);
6187 return GenericError;
6190 if (glyphsize > max_glyphsize)
6191 max_glyphsize = glyphsize;
6193 if (glyphsize != 0)
6195 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6196 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6197 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6198 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6200 if (left < min_x) min_x = left;
6201 if (top < min_y) min_y = top;
6202 if (right > max_x) max_x = right;
6203 if (bottom > max_y) max_y = bottom;
6206 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6208 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6209 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6213 if (max_glyphsize == 0)
6214 /* Nothing to draw. */
6215 return Ok;
6217 glyph_mask = GdipAlloc(max_glyphsize);
6218 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6219 text_mask_stride = max_x - min_x;
6221 if (!(glyph_mask && text_mask))
6223 GdipFree(glyph_mask);
6224 GdipFree(text_mask);
6225 GdipFree(pti);
6226 DeleteDC(hdc);
6227 DeleteObject(hfont);
6228 return OutOfMemory;
6231 /* Generate a mask for the text */
6232 for (i=0; i<length; i++)
6234 DWORD ret;
6235 int left, top, stride;
6237 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6238 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6240 if (ret == GDI_ERROR || ret == 0)
6241 continue; /* empty glyph */
6243 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6244 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6245 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6247 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6249 BYTE *glyph_val = glyph_mask + y * stride;
6250 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6251 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6253 *text_val = min(64, *text_val + *glyph_val);
6254 glyph_val++;
6255 text_val++;
6260 GdipFree(pti);
6261 DeleteDC(hdc);
6262 DeleteObject(hfont);
6263 GdipFree(glyph_mask);
6265 /* get the brush data */
6266 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6267 if (!pixel_data)
6269 GdipFree(text_mask);
6270 return OutOfMemory;
6273 pixel_area.X = min_x;
6274 pixel_area.Y = min_y;
6275 pixel_area.Width = max_x - min_x;
6276 pixel_area.Height = max_y - min_y;
6277 pixel_data_stride = pixel_area.Width * 4;
6279 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6280 if (stat != Ok)
6282 GdipFree(text_mask);
6283 GdipFree(pixel_data);
6284 return stat;
6287 /* multiply the brush data by the mask */
6288 for (y=0; y<pixel_area.Height; y++)
6290 BYTE *text_val = text_mask + text_mask_stride * y;
6291 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6292 for (x=0; x<pixel_area.Width; x++)
6294 *pixel_val = (*pixel_val) * (*text_val) / 64;
6295 text_val++;
6296 pixel_val+=4;
6300 GdipFree(text_mask);
6302 /* draw the result */
6303 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6304 pixel_area.Height, pixel_data_stride);
6306 GdipFree(pixel_data);
6308 return stat;
6311 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6312 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6313 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6314 INT flags, GDIPCONST GpMatrix *matrix)
6316 GpStatus stat = NotImplemented;
6318 if (length == -1)
6319 length = strlenW(text);
6321 if (graphics->hdc && !graphics->alpha_hdc &&
6322 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6323 brush->bt == BrushTypeSolidColor &&
6324 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6325 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6326 brush, positions, flags, matrix);
6327 if (stat == NotImplemented)
6328 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6329 brush, positions, flags, matrix);
6330 return stat;
6333 /*****************************************************************************
6334 * GdipDrawDriverString [GDIPLUS.@]
6336 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6337 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6338 GDIPCONST PointF *positions, INT flags,
6339 GDIPCONST GpMatrix *matrix )
6341 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6343 if (!graphics || !text || !font || !brush || !positions)
6344 return InvalidParameter;
6346 return draw_driver_string(graphics, text, length, font, NULL,
6347 brush, positions, flags, matrix);
6350 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6351 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6353 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6354 return NotImplemented;
6357 /*****************************************************************************
6358 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6360 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6362 GpStatus stat;
6363 GpRegion* rgn;
6365 TRACE("(%p, %p)\n", graphics, res);
6367 if((stat = GdipCreateRegion(&rgn)) != Ok)
6368 return stat;
6370 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6371 goto cleanup;
6373 stat = GdipIsEmptyRegion(rgn, graphics, res);
6375 cleanup:
6376 GdipDeleteRegion(rgn);
6377 return stat;
6380 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6382 static int calls;
6384 TRACE("(%p) stub\n", graphics);
6386 if(!(calls++))
6387 FIXME("not implemented\n");
6389 return NotImplemented;