dpnet/tests: Add a trailing '\n' to some ok() calls.
[wine.git] / dlls / gdiplus / graphics.c
blob169d92c667982ef6f94974db6d3045885e63f38e
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 BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
598 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
599 BOOL identity = TRUE;
600 int i, j;
602 for (i=0; i<4; i++)
603 for (j=0; j<5; j++)
605 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
606 identity = FALSE;
607 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
610 return identity;
613 static ARGB transform_color(ARGB color, int matrix[5][5])
615 int val[5], res[4];
616 int i, j;
617 unsigned char a, r, g, b;
619 val[0] = ((color >> 16) & 0xff); /* red */
620 val[1] = ((color >> 8) & 0xff); /* green */
621 val[2] = (color & 0xff); /* blue */
622 val[3] = ((color >> 24) & 0xff); /* alpha */
623 val[4] = 255; /* translation */
625 for (i=0; i<4; i++)
627 res[i] = 0;
629 for (j=0; j<5; j++)
630 res[i] += matrix[j][i] * val[j];
633 a = min(max(res[3] / 256, 0), 255);
634 r = min(max(res[0] / 256, 0), 255);
635 g = min(max(res[1] / 256, 0), 255);
636 b = min(max(res[2] / 256, 0), 255);
638 return (a << 24) | (r << 16) | (g << 8) | b;
641 static BOOL color_is_gray(ARGB color)
643 unsigned char r, g, b;
645 r = (color >> 16) & 0xff;
646 g = (color >> 8) & 0xff;
647 b = color & 0xff;
649 return (r == g) && (g == b);
652 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
653 UINT width, UINT height, INT stride, ColorAdjustType type)
655 UINT x, y;
656 INT i;
658 if (attributes->colorkeys[type].enabled ||
659 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
661 const struct color_key *key;
662 BYTE min_blue, min_green, min_red;
663 BYTE max_blue, max_green, max_red;
665 if (attributes->colorkeys[type].enabled)
666 key = &attributes->colorkeys[type];
667 else
668 key = &attributes->colorkeys[ColorAdjustTypeDefault];
670 min_blue = key->low&0xff;
671 min_green = (key->low>>8)&0xff;
672 min_red = (key->low>>16)&0xff;
674 max_blue = key->high&0xff;
675 max_green = (key->high>>8)&0xff;
676 max_red = (key->high>>16)&0xff;
678 for (x=0; x<width; x++)
679 for (y=0; y<height; y++)
681 ARGB *src_color;
682 BYTE blue, green, red;
683 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
684 blue = *src_color&0xff;
685 green = (*src_color>>8)&0xff;
686 red = (*src_color>>16)&0xff;
687 if (blue >= min_blue && green >= min_green && red >= min_red &&
688 blue <= max_blue && green <= max_green && red <= max_red)
689 *src_color = 0x00000000;
693 if (attributes->colorremaptables[type].enabled ||
694 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
696 const struct color_remap_table *table;
698 if (attributes->colorremaptables[type].enabled)
699 table = &attributes->colorremaptables[type];
700 else
701 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
703 for (x=0; x<width; x++)
704 for (y=0; y<height; y++)
706 ARGB *src_color;
707 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
708 for (i=0; i<table->mapsize; i++)
710 if (*src_color == table->colormap[i].oldColor.Argb)
712 *src_color = table->colormap[i].newColor.Argb;
713 break;
719 if (attributes->colormatrices[type].enabled ||
720 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
722 const struct color_matrix *colormatrices;
723 int color_matrix[5][5];
724 int gray_matrix[5][5];
725 BOOL identity;
727 if (attributes->colormatrices[type].enabled)
728 colormatrices = &attributes->colormatrices[type];
729 else
730 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
732 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
734 if (colormatrices->flags == ColorMatrixFlagsAltGray)
735 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
737 if (!identity)
739 for (x=0; x<width; x++)
741 for (y=0; y<height; y++)
743 ARGB *src_color;
744 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
746 if (colormatrices->flags == ColorMatrixFlagsDefault ||
747 !color_is_gray(*src_color))
749 *src_color = transform_color(*src_color, color_matrix);
751 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
753 *src_color = transform_color(*src_color, gray_matrix);
760 if (attributes->gamma_enabled[type] ||
761 attributes->gamma_enabled[ColorAdjustTypeDefault])
763 REAL gamma;
765 if (attributes->gamma_enabled[type])
766 gamma = attributes->gamma[type];
767 else
768 gamma = attributes->gamma[ColorAdjustTypeDefault];
770 for (x=0; x<width; x++)
771 for (y=0; y<height; y++)
773 ARGB *src_color;
774 BYTE blue, green, red;
775 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
777 blue = *src_color&0xff;
778 green = (*src_color>>8)&0xff;
779 red = (*src_color>>16)&0xff;
781 /* FIXME: We should probably use a table for this. */
782 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
783 green = floorf(powf(green / 255.0, gamma) * 255.0);
784 red = floorf(powf(red / 255.0, gamma) * 255.0);
786 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
791 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
792 * bitmap that contains all the pixels we may need to draw it. */
793 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
794 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
795 GpRect *rect)
797 INT left, top, right, bottom;
799 switch (interpolation)
801 case InterpolationModeHighQualityBilinear:
802 case InterpolationModeHighQualityBicubic:
803 /* FIXME: Include a greater range for the prefilter? */
804 case InterpolationModeBicubic:
805 case InterpolationModeBilinear:
806 left = (INT)(floorf(srcx));
807 top = (INT)(floorf(srcy));
808 right = (INT)(ceilf(srcx+srcwidth));
809 bottom = (INT)(ceilf(srcy+srcheight));
810 break;
811 case InterpolationModeNearestNeighbor:
812 default:
813 left = gdip_round(srcx);
814 top = gdip_round(srcy);
815 right = gdip_round(srcx+srcwidth);
816 bottom = gdip_round(srcy+srcheight);
817 break;
820 if (wrap == WrapModeClamp)
822 if (left < 0)
823 left = 0;
824 if (top < 0)
825 top = 0;
826 if (right >= bitmap->width)
827 right = bitmap->width-1;
828 if (bottom >= bitmap->height)
829 bottom = bitmap->height-1;
831 else
833 /* In some cases we can make the rectangle smaller here, but the logic
834 * is hard to get right, and tiling suggests we're likely to use the
835 * entire source image. */
836 if (left < 0 || right >= bitmap->width)
838 left = 0;
839 right = bitmap->width-1;
842 if (top < 0 || bottom >= bitmap->height)
844 top = 0;
845 bottom = bitmap->height-1;
849 rect->X = left;
850 rect->Y = top;
851 rect->Width = right - left + 1;
852 rect->Height = bottom - top + 1;
855 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
856 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
858 if (attributes->wrap == WrapModeClamp)
860 if (x < 0 || y < 0 || x >= width || y >= height)
861 return attributes->outside_color;
863 else
865 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
866 if (x < 0)
867 x = width*2 + x % (width * 2);
868 if (y < 0)
869 y = height*2 + y % (height * 2);
871 if ((attributes->wrap & 1) == 1)
873 /* Flip X */
874 if ((x / width) % 2 == 0)
875 x = x % width;
876 else
877 x = width - 1 - x % width;
879 else
880 x = x % width;
882 if ((attributes->wrap & 2) == 2)
884 /* Flip Y */
885 if ((y / height) % 2 == 0)
886 y = y % height;
887 else
888 y = height - 1 - y % height;
890 else
891 y = y % height;
894 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
896 ERR("out of range pixel requested\n");
897 return 0xffcd0084;
900 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
903 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
904 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
905 InterpolationMode interpolation, PixelOffsetMode offset_mode)
907 static int fixme;
909 switch (interpolation)
911 default:
912 if (!fixme++)
913 FIXME("Unimplemented interpolation %i\n", interpolation);
914 /* fall-through */
915 case InterpolationModeBilinear:
917 REAL leftxf, topyf;
918 INT leftx, rightx, topy, bottomy;
919 ARGB topleft, topright, bottomleft, bottomright;
920 ARGB top, bottom;
921 float x_offset;
923 leftxf = floorf(point->X);
924 leftx = (INT)leftxf;
925 rightx = (INT)ceilf(point->X);
926 topyf = floorf(point->Y);
927 topy = (INT)topyf;
928 bottomy = (INT)ceilf(point->Y);
930 if (leftx == rightx && topy == bottomy)
931 return sample_bitmap_pixel(src_rect, bits, width, height,
932 leftx, topy, attributes);
934 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
935 leftx, topy, attributes);
936 topright = sample_bitmap_pixel(src_rect, bits, width, height,
937 rightx, topy, attributes);
938 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
939 leftx, bottomy, attributes);
940 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
941 rightx, bottomy, attributes);
943 x_offset = point->X - leftxf;
944 top = blend_colors(topleft, topright, x_offset);
945 bottom = blend_colors(bottomleft, bottomright, x_offset);
947 return blend_colors(top, bottom, point->Y - topyf);
949 case InterpolationModeNearestNeighbor:
951 FLOAT pixel_offset;
952 switch (offset_mode)
954 default:
955 case PixelOffsetModeNone:
956 case PixelOffsetModeHighSpeed:
957 pixel_offset = 0.5;
958 break;
960 case PixelOffsetModeHalf:
961 case PixelOffsetModeHighQuality:
962 pixel_offset = 0.0;
963 break;
965 return sample_bitmap_pixel(src_rect, bits, width, height,
966 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
972 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
974 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
977 static BOOL brush_can_fill_path(GpBrush *brush)
979 switch (brush->bt)
981 case BrushTypeSolidColor:
982 return TRUE;
983 case BrushTypeHatchFill:
985 GpHatch *hatch = (GpHatch*)brush;
986 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
987 ((hatch->backcol & 0xff000000) == 0xff000000);
989 case BrushTypeLinearGradient:
990 case BrushTypeTextureFill:
991 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
992 default:
993 return FALSE;
997 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
999 switch (brush->bt)
1001 case BrushTypeSolidColor:
1003 GpSolidFill *fill = (GpSolidFill*)brush;
1004 HBITMAP bmp = ARGB2BMP(fill->color);
1006 if (bmp)
1008 RECT rc;
1009 /* partially transparent fill */
1011 SelectClipPath(graphics->hdc, RGN_AND);
1012 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1014 HDC hdc = CreateCompatibleDC(NULL);
1016 if (!hdc) break;
1018 SelectObject(hdc, bmp);
1019 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1020 hdc, 0, 0, 1, 1);
1021 DeleteDC(hdc);
1024 DeleteObject(bmp);
1025 break;
1027 /* else fall through */
1029 default:
1031 HBRUSH gdibrush, old_brush;
1033 gdibrush = create_gdi_brush(brush);
1034 if (!gdibrush) return;
1036 old_brush = SelectObject(graphics->hdc, gdibrush);
1037 FillPath(graphics->hdc);
1038 SelectObject(graphics->hdc, old_brush);
1039 DeleteObject(gdibrush);
1040 break;
1045 static BOOL brush_can_fill_pixels(GpBrush *brush)
1047 switch (brush->bt)
1049 case BrushTypeSolidColor:
1050 case BrushTypeHatchFill:
1051 case BrushTypeLinearGradient:
1052 case BrushTypeTextureFill:
1053 case BrushTypePathGradient:
1054 return TRUE;
1055 default:
1056 return FALSE;
1060 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1061 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1063 switch (brush->bt)
1065 case BrushTypeSolidColor:
1067 int x, y;
1068 GpSolidFill *fill = (GpSolidFill*)brush;
1069 for (x=0; x<fill_area->Width; x++)
1070 for (y=0; y<fill_area->Height; y++)
1071 argb_pixels[x + y*cdwStride] = fill->color;
1072 return Ok;
1074 case BrushTypeHatchFill:
1076 int x, y;
1077 GpHatch *fill = (GpHatch*)brush;
1078 const char *hatch_data;
1080 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1081 return NotImplemented;
1083 for (x=0; x<fill_area->Width; x++)
1084 for (y=0; y<fill_area->Height; y++)
1086 int hx, hy;
1088 /* FIXME: Account for the rendering origin */
1089 hx = (x + fill_area->X) % 8;
1090 hy = (y + fill_area->Y) % 8;
1092 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1093 argb_pixels[x + y*cdwStride] = fill->forecol;
1094 else
1095 argb_pixels[x + y*cdwStride] = fill->backcol;
1098 return Ok;
1100 case BrushTypeLinearGradient:
1102 GpLineGradient *fill = (GpLineGradient*)brush;
1103 GpPointF draw_points[3], line_points[3];
1104 GpStatus stat;
1105 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1106 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1107 int x, y;
1109 draw_points[0].X = fill_area->X;
1110 draw_points[0].Y = fill_area->Y;
1111 draw_points[1].X = fill_area->X+1;
1112 draw_points[1].Y = fill_area->Y;
1113 draw_points[2].X = fill_area->X;
1114 draw_points[2].Y = fill_area->Y+1;
1116 /* Transform the points to a co-ordinate space where X is the point's
1117 * position in the gradient, 0.0 being the start point and 1.0 the
1118 * end point. */
1119 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1120 CoordinateSpaceDevice, draw_points, 3);
1122 if (stat == Ok)
1124 line_points[0] = fill->startpoint;
1125 line_points[1] = fill->endpoint;
1126 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1127 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1129 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1132 if (stat == Ok)
1134 stat = GdipInvertMatrix(world_to_gradient);
1136 if (stat == Ok)
1137 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1139 GdipDeleteMatrix(world_to_gradient);
1142 if (stat == Ok)
1144 REAL x_delta = draw_points[1].X - draw_points[0].X;
1145 REAL y_delta = draw_points[2].X - draw_points[0].X;
1147 for (y=0; y<fill_area->Height; y++)
1149 for (x=0; x<fill_area->Width; x++)
1151 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1153 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1158 return stat;
1160 case BrushTypeTextureFill:
1162 GpTexture *fill = (GpTexture*)brush;
1163 GpPointF draw_points[3];
1164 GpStatus stat;
1165 int x, y;
1166 GpBitmap *bitmap;
1167 int src_stride;
1168 GpRect src_area;
1170 if (fill->image->type != ImageTypeBitmap)
1172 FIXME("metafile texture brushes not implemented\n");
1173 return NotImplemented;
1176 bitmap = (GpBitmap*)fill->image;
1177 src_stride = sizeof(ARGB) * bitmap->width;
1179 src_area.X = src_area.Y = 0;
1180 src_area.Width = bitmap->width;
1181 src_area.Height = bitmap->height;
1183 draw_points[0].X = fill_area->X;
1184 draw_points[0].Y = fill_area->Y;
1185 draw_points[1].X = fill_area->X+1;
1186 draw_points[1].Y = fill_area->Y;
1187 draw_points[2].X = fill_area->X;
1188 draw_points[2].Y = fill_area->Y+1;
1190 /* Transform the points to the co-ordinate space of the bitmap. */
1191 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1192 CoordinateSpaceDevice, draw_points, 3);
1194 if (stat == Ok)
1196 GpMatrix world_to_texture = fill->transform;
1198 stat = GdipInvertMatrix(&world_to_texture);
1199 if (stat == Ok)
1200 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1203 if (stat == Ok && !fill->bitmap_bits)
1205 BitmapData lockeddata;
1207 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1208 if (!fill->bitmap_bits)
1209 stat = OutOfMemory;
1211 if (stat == Ok)
1213 lockeddata.Width = bitmap->width;
1214 lockeddata.Height = bitmap->height;
1215 lockeddata.Stride = src_stride;
1216 lockeddata.PixelFormat = PixelFormat32bppARGB;
1217 lockeddata.Scan0 = fill->bitmap_bits;
1219 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1220 PixelFormat32bppARGB, &lockeddata);
1223 if (stat == Ok)
1224 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1226 if (stat == Ok)
1227 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1228 bitmap->width, bitmap->height,
1229 src_stride, ColorAdjustTypeBitmap);
1231 if (stat != Ok)
1233 GdipFree(fill->bitmap_bits);
1234 fill->bitmap_bits = NULL;
1238 if (stat == Ok)
1240 REAL x_dx = draw_points[1].X - draw_points[0].X;
1241 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1242 REAL y_dx = draw_points[2].X - draw_points[0].X;
1243 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1245 for (y=0; y<fill_area->Height; y++)
1247 for (x=0; x<fill_area->Width; x++)
1249 GpPointF point;
1250 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1251 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1253 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1254 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1255 &point, fill->imageattributes, graphics->interpolation,
1256 graphics->pixeloffset);
1261 return stat;
1263 case BrushTypePathGradient:
1265 GpPathGradient *fill = (GpPathGradient*)brush;
1266 GpPath *flat_path;
1267 GpMatrix world_to_device;
1268 GpStatus stat;
1269 int i, figure_start=0;
1270 GpPointF start_point, end_point, center_point;
1271 BYTE type;
1272 REAL min_yf, max_yf, line1_xf, line2_xf;
1273 INT min_y, max_y, min_x, max_x;
1274 INT x, y;
1275 ARGB outer_color;
1276 static BOOL transform_fixme_once;
1278 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1280 static int once;
1281 if (!once++)
1282 FIXME("path gradient focus not implemented\n");
1285 if (fill->gamma)
1287 static int once;
1288 if (!once++)
1289 FIXME("path gradient gamma correction not implemented\n");
1292 if (fill->blendcount)
1294 static int once;
1295 if (!once++)
1296 FIXME("path gradient blend not implemented\n");
1299 if (fill->pblendcount)
1301 static int once;
1302 if (!once++)
1303 FIXME("path gradient preset blend not implemented\n");
1306 if (!transform_fixme_once)
1308 BOOL is_identity=TRUE;
1309 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1310 if (!is_identity)
1312 FIXME("path gradient transform not implemented\n");
1313 transform_fixme_once = TRUE;
1317 stat = GdipClonePath(fill->path, &flat_path);
1319 if (stat != Ok)
1320 return stat;
1322 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1323 CoordinateSpaceWorld, &world_to_device);
1324 if (stat == Ok)
1326 stat = GdipTransformPath(flat_path, &world_to_device);
1328 if (stat == Ok)
1330 center_point = fill->center;
1331 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1334 if (stat == Ok)
1335 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1338 if (stat != Ok)
1340 GdipDeletePath(flat_path);
1341 return stat;
1344 for (i=0; i<flat_path->pathdata.Count; i++)
1346 int start_center_line=0, end_center_line=0;
1347 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1348 REAL center_distance;
1349 ARGB start_color, end_color;
1350 REAL dy, dx;
1352 type = flat_path->pathdata.Types[i];
1354 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1355 figure_start = i;
1357 start_point = flat_path->pathdata.Points[i];
1359 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1361 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1363 end_point = flat_path->pathdata.Points[figure_start];
1364 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1366 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1368 end_point = flat_path->pathdata.Points[i+1];
1369 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1371 else
1372 continue;
1374 outer_color = start_color;
1376 min_yf = center_point.Y;
1377 if (min_yf > start_point.Y) min_yf = start_point.Y;
1378 if (min_yf > end_point.Y) min_yf = end_point.Y;
1380 if (min_yf < fill_area->Y)
1381 min_y = fill_area->Y;
1382 else
1383 min_y = (INT)ceil(min_yf);
1385 max_yf = center_point.Y;
1386 if (max_yf < start_point.Y) max_yf = start_point.Y;
1387 if (max_yf < end_point.Y) max_yf = end_point.Y;
1389 if (max_yf > fill_area->Y + fill_area->Height)
1390 max_y = fill_area->Y + fill_area->Height;
1391 else
1392 max_y = (INT)ceil(max_yf);
1394 dy = end_point.Y - start_point.Y;
1395 dx = end_point.X - start_point.X;
1397 /* This is proportional to the distance from start-end line to center point. */
1398 center_distance = dy * (start_point.X - center_point.X) +
1399 dx * (center_point.Y - start_point.Y);
1401 for (y=min_y; y<max_y; y++)
1403 REAL yf = (REAL)y;
1405 if (!seen_start && yf >= start_point.Y)
1407 seen_start = TRUE;
1408 start_center_line ^= 1;
1410 if (!seen_end && yf >= end_point.Y)
1412 seen_end = TRUE;
1413 end_center_line ^= 1;
1415 if (!seen_center && yf >= center_point.Y)
1417 seen_center = TRUE;
1418 start_center_line ^= 1;
1419 end_center_line ^= 1;
1422 if (start_center_line)
1423 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1424 else
1425 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1427 if (end_center_line)
1428 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1429 else
1430 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1432 if (line1_xf < line2_xf)
1434 min_x = (INT)ceil(line1_xf);
1435 max_x = (INT)ceil(line2_xf);
1437 else
1439 min_x = (INT)ceil(line2_xf);
1440 max_x = (INT)ceil(line1_xf);
1443 if (min_x < fill_area->X)
1444 min_x = fill_area->X;
1445 if (max_x > fill_area->X + fill_area->Width)
1446 max_x = fill_area->X + fill_area->Width;
1448 for (x=min_x; x<max_x; x++)
1450 REAL xf = (REAL)x;
1451 REAL distance;
1453 if (start_color != end_color)
1455 REAL blend_amount, pdy, pdx;
1456 pdy = yf - center_point.Y;
1457 pdx = xf - center_point.X;
1458 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1459 outer_color = blend_colors(start_color, end_color, blend_amount);
1462 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1463 (end_point.X - start_point.X) * (yf - start_point.Y);
1465 distance = distance / center_distance;
1467 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1468 blend_colors(outer_color, fill->centercolor, distance);
1473 GdipDeletePath(flat_path);
1474 return stat;
1476 default:
1477 return NotImplemented;
1481 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1482 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1483 * should not be called on an hdc that has a path you care about. */
1484 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1485 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1487 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1488 GpMatrix matrix;
1489 HBRUSH brush = NULL;
1490 HPEN pen = NULL;
1491 PointF ptf[4], *custptf = NULL;
1492 POINT pt[4], *custpt = NULL;
1493 BYTE *tp = NULL;
1494 REAL theta, dsmall, dbig, dx, dy = 0.0;
1495 INT i, count;
1496 LOGBRUSH lb;
1497 BOOL customstroke;
1499 if((x1 == x2) && (y1 == y2))
1500 return;
1502 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1504 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1505 if(!customstroke){
1506 brush = CreateSolidBrush(color);
1507 lb.lbStyle = BS_SOLID;
1508 lb.lbColor = color;
1509 lb.lbHatch = 0;
1510 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1511 PS_JOIN_MITER, 1, &lb, 0,
1512 NULL);
1513 oldbrush = SelectObject(graphics->hdc, brush);
1514 oldpen = SelectObject(graphics->hdc, pen);
1517 switch(cap){
1518 case LineCapFlat:
1519 break;
1520 case LineCapSquare:
1521 case LineCapSquareAnchor:
1522 case LineCapDiamondAnchor:
1523 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1524 if(cap == LineCapDiamondAnchor){
1525 dsmall = cos(theta + M_PI_2) * size;
1526 dbig = sin(theta + M_PI_2) * size;
1528 else{
1529 dsmall = cos(theta + M_PI_4) * size;
1530 dbig = sin(theta + M_PI_4) * size;
1533 ptf[0].X = x2 - dsmall;
1534 ptf[1].X = x2 + dbig;
1536 ptf[0].Y = y2 - dbig;
1537 ptf[3].Y = y2 + dsmall;
1539 ptf[1].Y = y2 - dsmall;
1540 ptf[2].Y = y2 + dbig;
1542 ptf[3].X = x2 - dbig;
1543 ptf[2].X = x2 + dsmall;
1545 transform_and_round_points(graphics, pt, ptf, 4);
1546 Polygon(graphics->hdc, pt, 4);
1548 break;
1549 case LineCapArrowAnchor:
1550 size = size * 4.0 / sqrt(3.0);
1552 dx = cos(M_PI / 6.0 + theta) * size;
1553 dy = sin(M_PI / 6.0 + theta) * size;
1555 ptf[0].X = x2 - dx;
1556 ptf[0].Y = y2 - dy;
1558 dx = cos(- M_PI / 6.0 + theta) * size;
1559 dy = sin(- M_PI / 6.0 + theta) * size;
1561 ptf[1].X = x2 - dx;
1562 ptf[1].Y = y2 - dy;
1564 ptf[2].X = x2;
1565 ptf[2].Y = y2;
1567 transform_and_round_points(graphics, pt, ptf, 3);
1568 Polygon(graphics->hdc, pt, 3);
1570 break;
1571 case LineCapRoundAnchor:
1572 dx = dy = ANCHOR_WIDTH * size / 2.0;
1574 ptf[0].X = x2 - dx;
1575 ptf[0].Y = y2 - dy;
1576 ptf[1].X = x2 + dx;
1577 ptf[1].Y = y2 + dy;
1579 transform_and_round_points(graphics, pt, ptf, 2);
1580 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1582 break;
1583 case LineCapTriangle:
1584 size = size / 2.0;
1585 dx = cos(M_PI_2 + theta) * size;
1586 dy = sin(M_PI_2 + theta) * size;
1588 ptf[0].X = x2 - dx;
1589 ptf[0].Y = y2 - dy;
1590 ptf[1].X = x2 + dx;
1591 ptf[1].Y = y2 + dy;
1593 dx = cos(theta) * size;
1594 dy = sin(theta) * size;
1596 ptf[2].X = x2 + dx;
1597 ptf[2].Y = y2 + dy;
1599 transform_and_round_points(graphics, pt, ptf, 3);
1600 Polygon(graphics->hdc, pt, 3);
1602 break;
1603 case LineCapRound:
1604 dx = dy = size / 2.0;
1606 ptf[0].X = x2 - dx;
1607 ptf[0].Y = y2 - dy;
1608 ptf[1].X = x2 + dx;
1609 ptf[1].Y = y2 + dy;
1611 dx = -cos(M_PI_2 + theta) * size;
1612 dy = -sin(M_PI_2 + theta) * size;
1614 ptf[2].X = x2 - dx;
1615 ptf[2].Y = y2 - dy;
1616 ptf[3].X = x2 + dx;
1617 ptf[3].Y = y2 + dy;
1619 transform_and_round_points(graphics, pt, ptf, 4);
1620 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1621 pt[2].y, pt[3].x, pt[3].y);
1623 break;
1624 case LineCapCustom:
1625 if(!custom)
1626 break;
1628 count = custom->pathdata.Count;
1629 custptf = GdipAlloc(count * sizeof(PointF));
1630 custpt = GdipAlloc(count * sizeof(POINT));
1631 tp = GdipAlloc(count);
1633 if(!custptf || !custpt || !tp)
1634 goto custend;
1636 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1638 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1639 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1640 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1641 MatrixOrderAppend);
1642 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1643 GdipTransformMatrixPoints(&matrix, custptf, count);
1645 transform_and_round_points(graphics, custpt, custptf, count);
1647 for(i = 0; i < count; i++)
1648 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1650 if(custom->fill){
1651 BeginPath(graphics->hdc);
1652 PolyDraw(graphics->hdc, custpt, tp, count);
1653 EndPath(graphics->hdc);
1654 StrokeAndFillPath(graphics->hdc);
1656 else
1657 PolyDraw(graphics->hdc, custpt, tp, count);
1659 custend:
1660 GdipFree(custptf);
1661 GdipFree(custpt);
1662 GdipFree(tp);
1663 break;
1664 default:
1665 break;
1668 if(!customstroke){
1669 SelectObject(graphics->hdc, oldbrush);
1670 SelectObject(graphics->hdc, oldpen);
1671 DeleteObject(brush);
1672 DeleteObject(pen);
1676 /* Shortens the line by the given percent by changing x2, y2.
1677 * If percent is > 1.0 then the line will change direction.
1678 * If percent is negative it can lengthen the line. */
1679 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1681 REAL dist, theta, dx, dy;
1683 if((y1 == *y2) && (x1 == *x2))
1684 return;
1686 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1687 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1688 dx = cos(theta) * dist;
1689 dy = sin(theta) * dist;
1691 *x2 = *x2 + dx;
1692 *y2 = *y2 + dy;
1695 /* Shortens the line by the given amount by changing x2, y2.
1696 * If the amount is greater than the distance, the line will become length 0.
1697 * If the amount is negative, it can lengthen the line. */
1698 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1700 REAL dx, dy, percent;
1702 dx = *x2 - x1;
1703 dy = *y2 - y1;
1704 if(dx == 0 && dy == 0)
1705 return;
1707 percent = amt / sqrt(dx * dx + dy * dy);
1708 if(percent >= 1.0){
1709 *x2 = x1;
1710 *y2 = y1;
1711 return;
1714 shorten_line_percent(x1, y1, x2, y2, percent);
1717 /* Conducts a linear search to find the bezier points that will back off
1718 * the endpoint of the curve by a distance of amt. Linear search works
1719 * better than binary in this case because there are multiple solutions,
1720 * and binary searches often find a bad one. I don't think this is what
1721 * Windows does but short of rendering the bezier without GDI's help it's
1722 * the best we can do. If rev then work from the start of the passed points
1723 * instead of the end. */
1724 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1726 GpPointF origpt[4];
1727 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1728 INT i, first = 0, second = 1, third = 2, fourth = 3;
1730 if(rev){
1731 first = 3;
1732 second = 2;
1733 third = 1;
1734 fourth = 0;
1737 origx = pt[fourth].X;
1738 origy = pt[fourth].Y;
1739 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1741 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1742 /* reset bezier points to original values */
1743 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1744 /* Perform magic on bezier points. Order is important here.*/
1745 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1746 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1747 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1748 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1749 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1750 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1752 dx = pt[fourth].X - origx;
1753 dy = pt[fourth].Y - origy;
1755 diff = sqrt(dx * dx + dy * dy);
1756 percent += 0.0005 * amt;
1760 /* Draws a combination of bezier curves and lines between points. */
1761 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1762 GDIPCONST BYTE * types, INT count, BOOL caps)
1764 POINT *pti = GdipAlloc(count * sizeof(POINT));
1765 BYTE *tp = GdipAlloc(count);
1766 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1767 INT i, j;
1768 GpStatus status = GenericError;
1770 if(!count){
1771 status = Ok;
1772 goto end;
1774 if(!pti || !tp || !ptcopy){
1775 status = OutOfMemory;
1776 goto end;
1779 for(i = 1; i < count; i++){
1780 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1781 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1782 || !(types[i + 1] & PathPointTypeBezier)){
1783 ERR("Bad bezier points\n");
1784 goto end;
1786 i += 2;
1790 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1792 /* If we are drawing caps, go through the points and adjust them accordingly,
1793 * and draw the caps. */
1794 if(caps){
1795 switch(types[count - 1] & PathPointTypePathTypeMask){
1796 case PathPointTypeBezier:
1797 if(pen->endcap == LineCapArrowAnchor)
1798 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1799 else if((pen->endcap == LineCapCustom) && pen->customend)
1800 shorten_bezier_amt(&ptcopy[count - 4],
1801 pen->width * pen->customend->inset, FALSE);
1803 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1804 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1805 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1806 pt[count - 1].X, pt[count - 1].Y);
1808 break;
1809 case PathPointTypeLine:
1810 if(pen->endcap == LineCapArrowAnchor)
1811 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1812 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1813 pen->width);
1814 else if((pen->endcap == LineCapCustom) && pen->customend)
1815 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1816 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1817 pen->customend->inset * pen->width);
1819 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1820 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1821 pt[count - 1].Y);
1823 break;
1824 default:
1825 ERR("Bad path last point\n");
1826 goto end;
1829 /* Find start of points */
1830 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1831 == PathPointTypeStart); j++);
1833 switch(types[j] & PathPointTypePathTypeMask){
1834 case PathPointTypeBezier:
1835 if(pen->startcap == LineCapArrowAnchor)
1836 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1837 else if((pen->startcap == LineCapCustom) && pen->customstart)
1838 shorten_bezier_amt(&ptcopy[j - 1],
1839 pen->width * pen->customstart->inset, TRUE);
1841 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1842 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1843 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1844 pt[j - 1].X, pt[j - 1].Y);
1846 break;
1847 case PathPointTypeLine:
1848 if(pen->startcap == LineCapArrowAnchor)
1849 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1850 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1851 pen->width);
1852 else if((pen->startcap == LineCapCustom) && pen->customstart)
1853 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1854 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1855 pen->customstart->inset * pen->width);
1857 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1858 pt[j].X, pt[j].Y, pt[j - 1].X,
1859 pt[j - 1].Y);
1861 break;
1862 default:
1863 ERR("Bad path points\n");
1864 goto end;
1868 transform_and_round_points(graphics, pti, ptcopy, count);
1870 for(i = 0; i < count; i++){
1871 tp[i] = convert_path_point_type(types[i]);
1874 PolyDraw(graphics->hdc, pti, tp, count);
1876 status = Ok;
1878 end:
1879 GdipFree(pti);
1880 GdipFree(ptcopy);
1881 GdipFree(tp);
1883 return status;
1886 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1888 GpStatus result;
1890 BeginPath(graphics->hdc);
1891 result = draw_poly(graphics, NULL, path->pathdata.Points,
1892 path->pathdata.Types, path->pathdata.Count, FALSE);
1893 EndPath(graphics->hdc);
1894 return result;
1897 typedef struct _GraphicsContainerItem {
1898 struct list entry;
1899 GraphicsContainer contid;
1901 SmoothingMode smoothing;
1902 CompositingQuality compqual;
1903 InterpolationMode interpolation;
1904 CompositingMode compmode;
1905 TextRenderingHint texthint;
1906 REAL scale;
1907 GpUnit unit;
1908 PixelOffsetMode pixeloffset;
1909 UINT textcontrast;
1910 GpMatrix worldtrans;
1911 GpRegion* clip;
1912 INT origin_x, origin_y;
1913 } GraphicsContainerItem;
1915 static GpStatus init_container(GraphicsContainerItem** container,
1916 GDIPCONST GpGraphics* graphics){
1917 GpStatus sts;
1919 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1920 if(!(*container))
1921 return OutOfMemory;
1923 (*container)->contid = graphics->contid + 1;
1925 (*container)->smoothing = graphics->smoothing;
1926 (*container)->compqual = graphics->compqual;
1927 (*container)->interpolation = graphics->interpolation;
1928 (*container)->compmode = graphics->compmode;
1929 (*container)->texthint = graphics->texthint;
1930 (*container)->scale = graphics->scale;
1931 (*container)->unit = graphics->unit;
1932 (*container)->textcontrast = graphics->textcontrast;
1933 (*container)->pixeloffset = graphics->pixeloffset;
1934 (*container)->origin_x = graphics->origin_x;
1935 (*container)->origin_y = graphics->origin_y;
1936 (*container)->worldtrans = graphics->worldtrans;
1938 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1939 if(sts != Ok){
1940 GdipFree(*container);
1941 *container = NULL;
1942 return sts;
1945 return Ok;
1948 static void delete_container(GraphicsContainerItem* container)
1950 GdipDeleteRegion(container->clip);
1951 GdipFree(container);
1954 static GpStatus restore_container(GpGraphics* graphics,
1955 GDIPCONST GraphicsContainerItem* container){
1956 GpStatus sts;
1957 GpRegion *newClip;
1959 sts = GdipCloneRegion(container->clip, &newClip);
1960 if(sts != Ok) return sts;
1962 graphics->worldtrans = container->worldtrans;
1964 GdipDeleteRegion(graphics->clip);
1965 graphics->clip = newClip;
1967 graphics->contid = container->contid - 1;
1969 graphics->smoothing = container->smoothing;
1970 graphics->compqual = container->compqual;
1971 graphics->interpolation = container->interpolation;
1972 graphics->compmode = container->compmode;
1973 graphics->texthint = container->texthint;
1974 graphics->scale = container->scale;
1975 graphics->unit = container->unit;
1976 graphics->textcontrast = container->textcontrast;
1977 graphics->pixeloffset = container->pixeloffset;
1978 graphics->origin_x = container->origin_x;
1979 graphics->origin_y = container->origin_y;
1981 return Ok;
1984 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1986 RECT wnd_rect;
1987 GpStatus stat=Ok;
1988 GpUnit unit;
1990 if(graphics->hwnd) {
1991 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1992 return GenericError;
1994 rect->X = wnd_rect.left;
1995 rect->Y = wnd_rect.top;
1996 rect->Width = wnd_rect.right - wnd_rect.left;
1997 rect->Height = wnd_rect.bottom - wnd_rect.top;
1998 }else if (graphics->image){
1999 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2000 if (stat == Ok && unit != UnitPixel)
2001 FIXME("need to convert from unit %i\n", unit);
2002 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2003 HBITMAP hbmp;
2004 BITMAP bmp;
2006 rect->X = 0;
2007 rect->Y = 0;
2009 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2010 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2012 rect->Width = bmp.bmWidth;
2013 rect->Height = bmp.bmHeight;
2015 else
2017 /* FIXME: ??? */
2018 rect->Width = 1;
2019 rect->Height = 1;
2021 }else{
2022 rect->X = 0;
2023 rect->Y = 0;
2024 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2025 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2028 if (graphics->hdc &&
2029 (GetMapMode(graphics->hdc) != MM_TEXT || GetGraphicsMode(graphics->hdc) != GM_COMPATIBLE))
2031 POINT points[2];
2033 points[0].x = rect->X;
2034 points[0].y = rect->Y;
2035 points[1].x = rect->X + rect->Width;
2036 points[1].y = rect->Y + rect->Height;
2038 DPtoLP(graphics->hdc, points, sizeof(points)/sizeof(points[0]));
2040 rect->X = min(points[0].x, points[1].x);
2041 rect->Y = min(points[0].y, points[1].y);
2042 rect->Width = abs(points[1].x - points[0].x);
2043 rect->Height = abs(points[1].y - points[0].y);
2046 return stat;
2049 /* on success, rgn will contain the region of the graphics object which
2050 * is visible after clipping has been applied */
2051 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2053 GpStatus stat;
2054 GpRectF rectf;
2055 GpRegion* tmp;
2057 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2058 return stat;
2060 if((stat = GdipCreateRegion(&tmp)) != Ok)
2061 return stat;
2063 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2064 goto end;
2066 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2067 goto end;
2069 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2071 end:
2072 GdipDeleteRegion(tmp);
2073 return stat;
2076 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2078 REAL height;
2080 if (font->unit == UnitPixel)
2082 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2084 else
2086 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2087 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2088 else
2089 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2092 lf->lfHeight = -(height + 0.5);
2093 lf->lfWidth = 0;
2094 lf->lfEscapement = 0;
2095 lf->lfOrientation = 0;
2096 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2097 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2098 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2099 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2100 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2101 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2102 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2103 lf->lfQuality = DEFAULT_QUALITY;
2104 lf->lfPitchAndFamily = 0;
2105 strcpyW(lf->lfFaceName, font->family->FamilyName);
2108 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2109 GDIPCONST GpStringFormat *format, HFONT *hfont,
2110 GDIPCONST GpMatrix *matrix)
2112 HDC hdc = CreateCompatibleDC(0);
2113 GpPointF pt[3];
2114 REAL angle, rel_width, rel_height, font_height;
2115 LOGFONTW lfw;
2116 HFONT unscaled_font;
2117 TEXTMETRICW textmet;
2119 if (font->unit == UnitPixel)
2120 font_height = font->emSize;
2121 else
2123 REAL unit_scale, res;
2125 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2126 unit_scale = units_scale(font->unit, graphics->unit, res);
2128 font_height = font->emSize * unit_scale;
2131 pt[0].X = 0.0;
2132 pt[0].Y = 0.0;
2133 pt[1].X = 1.0;
2134 pt[1].Y = 0.0;
2135 pt[2].X = 0.0;
2136 pt[2].Y = 1.0;
2137 if (matrix)
2139 GpMatrix xform = *matrix;
2140 GdipTransformMatrixPoints(&xform, pt, 3);
2142 if (graphics)
2143 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2144 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2145 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2146 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2147 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2148 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2150 get_log_fontW(font, graphics, &lfw);
2151 lfw.lfHeight = -gdip_round(font_height * rel_height);
2152 unscaled_font = CreateFontIndirectW(&lfw);
2154 SelectObject(hdc, unscaled_font);
2155 GetTextMetricsW(hdc, &textmet);
2157 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2158 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2160 *hfont = CreateFontIndirectW(&lfw);
2162 DeleteDC(hdc);
2163 DeleteObject(unscaled_font);
2166 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2168 TRACE("(%p, %p)\n", hdc, graphics);
2170 return GdipCreateFromHDC2(hdc, NULL, graphics);
2173 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2175 GpStatus retval;
2176 HBITMAP hbitmap;
2177 DIBSECTION dib;
2179 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2181 if(hDevice != NULL)
2182 FIXME("Don't know how to handle parameter hDevice\n");
2184 if(hdc == NULL)
2185 return OutOfMemory;
2187 if(graphics == NULL)
2188 return InvalidParameter;
2190 *graphics = GdipAlloc(sizeof(GpGraphics));
2191 if(!*graphics) return OutOfMemory;
2193 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2195 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2196 GdipFree(*graphics);
2197 return retval;
2200 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2201 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2202 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2204 (*graphics)->alpha_hdc = 1;
2207 (*graphics)->hdc = hdc;
2208 (*graphics)->hwnd = WindowFromDC(hdc);
2209 (*graphics)->owndc = FALSE;
2210 (*graphics)->smoothing = SmoothingModeDefault;
2211 (*graphics)->compqual = CompositingQualityDefault;
2212 (*graphics)->interpolation = InterpolationModeBilinear;
2213 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2214 (*graphics)->compmode = CompositingModeSourceOver;
2215 (*graphics)->unit = UnitDisplay;
2216 (*graphics)->scale = 1.0;
2217 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2218 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2219 (*graphics)->busy = FALSE;
2220 (*graphics)->textcontrast = 4;
2221 list_init(&(*graphics)->containers);
2222 (*graphics)->contid = 0;
2224 TRACE("<-- %p\n", *graphics);
2226 return Ok;
2229 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2231 GpStatus retval;
2233 *graphics = GdipAlloc(sizeof(GpGraphics));
2234 if(!*graphics) return OutOfMemory;
2236 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2238 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2239 GdipFree(*graphics);
2240 return retval;
2243 (*graphics)->hdc = NULL;
2244 (*graphics)->hwnd = NULL;
2245 (*graphics)->owndc = FALSE;
2246 (*graphics)->image = image;
2247 /* We have to store the image type here because the image may be freed
2248 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2249 (*graphics)->image_type = image->type;
2250 (*graphics)->smoothing = SmoothingModeDefault;
2251 (*graphics)->compqual = CompositingQualityDefault;
2252 (*graphics)->interpolation = InterpolationModeBilinear;
2253 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2254 (*graphics)->compmode = CompositingModeSourceOver;
2255 (*graphics)->unit = UnitDisplay;
2256 (*graphics)->scale = 1.0;
2257 (*graphics)->xres = image->xres;
2258 (*graphics)->yres = image->yres;
2259 (*graphics)->busy = FALSE;
2260 (*graphics)->textcontrast = 4;
2261 list_init(&(*graphics)->containers);
2262 (*graphics)->contid = 0;
2264 TRACE("<-- %p\n", *graphics);
2266 return Ok;
2269 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2271 GpStatus ret;
2272 HDC hdc;
2274 TRACE("(%p, %p)\n", hwnd, graphics);
2276 hdc = GetDC(hwnd);
2278 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2280 ReleaseDC(hwnd, hdc);
2281 return ret;
2284 (*graphics)->hwnd = hwnd;
2285 (*graphics)->owndc = TRUE;
2287 return Ok;
2290 /* FIXME: no icm handling */
2291 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2293 TRACE("(%p, %p)\n", hwnd, graphics);
2295 return GdipCreateFromHWND(hwnd, graphics);
2298 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2299 UINT access, IStream **stream)
2301 DWORD dwMode;
2302 HRESULT ret;
2304 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2306 if(!stream || !filename)
2307 return InvalidParameter;
2309 if(access & GENERIC_WRITE)
2310 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2311 else if(access & GENERIC_READ)
2312 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2313 else
2314 return InvalidParameter;
2316 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2318 return hresult_to_status(ret);
2321 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2323 GraphicsContainerItem *cont, *next;
2324 GpStatus stat;
2325 TRACE("(%p)\n", graphics);
2327 if(!graphics) return InvalidParameter;
2328 if(graphics->busy) return ObjectBusy;
2330 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2332 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2333 if (stat != Ok)
2334 return stat;
2337 if(graphics->owndc)
2338 ReleaseDC(graphics->hwnd, graphics->hdc);
2340 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2341 list_remove(&cont->entry);
2342 delete_container(cont);
2345 GdipDeleteRegion(graphics->clip);
2347 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2348 * do otherwise, but we can't have that in the test suite because it means
2349 * accessing freed memory. */
2350 graphics->busy = TRUE;
2352 GdipFree(graphics);
2354 return Ok;
2357 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2358 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2360 GpStatus status;
2361 GpPath *path;
2363 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2364 width, height, startAngle, sweepAngle);
2366 if(!graphics || !pen || width <= 0 || height <= 0)
2367 return InvalidParameter;
2369 if(graphics->busy)
2370 return ObjectBusy;
2372 status = GdipCreatePath(FillModeAlternate, &path);
2373 if (status != Ok) return status;
2375 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2376 if (status == Ok)
2377 status = GdipDrawPath(graphics, pen, path);
2379 GdipDeletePath(path);
2380 return status;
2383 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2384 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2386 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2387 width, height, startAngle, sweepAngle);
2389 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2392 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2393 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2395 GpPointF pt[4];
2397 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2398 x2, y2, x3, y3, x4, y4);
2400 if(!graphics || !pen)
2401 return InvalidParameter;
2403 if(graphics->busy)
2404 return ObjectBusy;
2406 pt[0].X = x1;
2407 pt[0].Y = y1;
2408 pt[1].X = x2;
2409 pt[1].Y = y2;
2410 pt[2].X = x3;
2411 pt[2].Y = y3;
2412 pt[3].X = x4;
2413 pt[3].Y = y4;
2414 return GdipDrawBeziers(graphics, pen, pt, 4);
2417 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2418 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2420 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2421 x2, y2, x3, y3, x4, y4);
2423 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2426 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2427 GDIPCONST GpPointF *points, INT count)
2429 GpStatus status;
2430 GpPath *path;
2432 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2434 if(!graphics || !pen || !points || (count <= 0))
2435 return InvalidParameter;
2437 if(graphics->busy)
2438 return ObjectBusy;
2440 status = GdipCreatePath(FillModeAlternate, &path);
2441 if (status != Ok) return status;
2443 status = GdipAddPathBeziers(path, points, count);
2444 if (status == Ok)
2445 status = GdipDrawPath(graphics, pen, path);
2447 GdipDeletePath(path);
2448 return status;
2451 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2452 GDIPCONST GpPoint *points, INT count)
2454 GpPointF *pts;
2455 GpStatus ret;
2456 INT i;
2458 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2460 if(!graphics || !pen || !points || (count <= 0))
2461 return InvalidParameter;
2463 if(graphics->busy)
2464 return ObjectBusy;
2466 pts = GdipAlloc(sizeof(GpPointF) * count);
2467 if(!pts)
2468 return OutOfMemory;
2470 for(i = 0; i < count; i++){
2471 pts[i].X = (REAL)points[i].X;
2472 pts[i].Y = (REAL)points[i].Y;
2475 ret = GdipDrawBeziers(graphics,pen,pts,count);
2477 GdipFree(pts);
2479 return ret;
2482 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2483 GDIPCONST GpPointF *points, INT count)
2485 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2487 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2490 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2491 GDIPCONST GpPoint *points, INT count)
2493 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2495 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2498 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2499 GDIPCONST GpPointF *points, INT count, REAL tension)
2501 GpPath *path;
2502 GpStatus status;
2504 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2506 if(!graphics || !pen || !points || count <= 0)
2507 return InvalidParameter;
2509 if(graphics->busy)
2510 return ObjectBusy;
2512 status = GdipCreatePath(FillModeAlternate, &path);
2513 if (status != Ok) return status;
2515 status = GdipAddPathClosedCurve2(path, points, count, tension);
2516 if (status == Ok)
2517 status = GdipDrawPath(graphics, pen, path);
2519 GdipDeletePath(path);
2521 return status;
2524 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2525 GDIPCONST GpPoint *points, INT count, REAL tension)
2527 GpPointF *ptf;
2528 GpStatus stat;
2529 INT i;
2531 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2533 if(!points || count <= 0)
2534 return InvalidParameter;
2536 ptf = GdipAlloc(sizeof(GpPointF)*count);
2537 if(!ptf)
2538 return OutOfMemory;
2540 for(i = 0; i < count; i++){
2541 ptf[i].X = (REAL)points[i].X;
2542 ptf[i].Y = (REAL)points[i].Y;
2545 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2547 GdipFree(ptf);
2549 return stat;
2552 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2553 GDIPCONST GpPointF *points, INT count)
2555 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2557 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2560 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2561 GDIPCONST GpPoint *points, INT count)
2563 GpPointF *pointsF;
2564 GpStatus ret;
2565 INT i;
2567 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2569 if(!points)
2570 return InvalidParameter;
2572 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2573 if(!pointsF)
2574 return OutOfMemory;
2576 for(i = 0; i < count; i++){
2577 pointsF[i].X = (REAL)points[i].X;
2578 pointsF[i].Y = (REAL)points[i].Y;
2581 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2582 GdipFree(pointsF);
2584 return ret;
2587 /* Approximates cardinal spline with Bezier curves. */
2588 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2589 GDIPCONST GpPointF *points, INT count, REAL tension)
2591 GpPath *path;
2592 GpStatus status;
2594 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2596 if(!graphics || !pen)
2597 return InvalidParameter;
2599 if(graphics->busy)
2600 return ObjectBusy;
2602 if(count < 2)
2603 return InvalidParameter;
2605 status = GdipCreatePath(FillModeAlternate, &path);
2606 if (status != Ok) return status;
2608 status = GdipAddPathCurve2(path, points, count, tension);
2609 if (status == Ok)
2610 status = GdipDrawPath(graphics, pen, path);
2612 GdipDeletePath(path);
2613 return status;
2616 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2617 GDIPCONST GpPoint *points, INT count, REAL tension)
2619 GpPointF *pointsF;
2620 GpStatus ret;
2621 INT i;
2623 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2625 if(!points)
2626 return InvalidParameter;
2628 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2629 if(!pointsF)
2630 return OutOfMemory;
2632 for(i = 0; i < count; i++){
2633 pointsF[i].X = (REAL)points[i].X;
2634 pointsF[i].Y = (REAL)points[i].Y;
2637 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2638 GdipFree(pointsF);
2640 return ret;
2643 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2644 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2645 REAL tension)
2647 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2649 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2650 return InvalidParameter;
2653 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2656 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2657 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2658 REAL tension)
2660 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2662 if(count < 0){
2663 return OutOfMemory;
2666 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2667 return InvalidParameter;
2670 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2673 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2674 REAL y, REAL width, REAL height)
2676 GpPath *path;
2677 GpStatus status;
2679 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2681 if(!graphics || !pen)
2682 return InvalidParameter;
2684 if(graphics->busy)
2685 return ObjectBusy;
2687 status = GdipCreatePath(FillModeAlternate, &path);
2688 if (status != Ok) return status;
2690 status = GdipAddPathEllipse(path, x, y, width, height);
2691 if (status == Ok)
2692 status = GdipDrawPath(graphics, pen, path);
2694 GdipDeletePath(path);
2695 return status;
2698 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2699 INT y, INT width, INT height)
2701 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2703 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2707 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2709 UINT width, height;
2711 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2713 if(!graphics || !image)
2714 return InvalidParameter;
2716 GdipGetImageWidth(image, &width);
2717 GdipGetImageHeight(image, &height);
2719 return GdipDrawImagePointRect(graphics, image, x, y,
2720 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2723 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2724 INT y)
2726 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2728 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2731 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2732 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2733 GpUnit srcUnit)
2735 GpPointF points[3];
2736 REAL scale_x, scale_y, width, height;
2738 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2740 if (!graphics || !image) return InvalidParameter;
2742 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2743 scale_x *= graphics->xres / image->xres;
2744 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2745 scale_y *= graphics->yres / image->yres;
2746 width = srcwidth * scale_x;
2747 height = srcheight * scale_y;
2749 points[0].X = points[2].X = x;
2750 points[0].Y = points[1].Y = y;
2751 points[1].X = x + width;
2752 points[2].Y = y + height;
2754 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2755 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2758 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2759 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2760 GpUnit srcUnit)
2762 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2765 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2766 GDIPCONST GpPointF *dstpoints, INT count)
2768 UINT width, height;
2770 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2772 if(!image)
2773 return InvalidParameter;
2775 GdipGetImageWidth(image, &width);
2776 GdipGetImageHeight(image, &height);
2778 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2779 width, height, UnitPixel, NULL, NULL, NULL);
2782 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2783 GDIPCONST GpPoint *dstpoints, INT count)
2785 GpPointF ptf[3];
2787 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2789 if (count != 3 || !dstpoints)
2790 return InvalidParameter;
2792 ptf[0].X = (REAL)dstpoints[0].X;
2793 ptf[0].Y = (REAL)dstpoints[0].Y;
2794 ptf[1].X = (REAL)dstpoints[1].X;
2795 ptf[1].Y = (REAL)dstpoints[1].Y;
2796 ptf[2].X = (REAL)dstpoints[2].X;
2797 ptf[2].Y = (REAL)dstpoints[2].Y;
2799 return GdipDrawImagePoints(graphics, image, ptf, count);
2802 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2803 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2805 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2806 return TRUE;
2809 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2810 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2811 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2812 DrawImageAbort callback, VOID * callbackData)
2814 GpPointF ptf[4];
2815 POINT pti[4];
2816 GpStatus stat;
2818 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2819 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2820 callbackData);
2822 if (count > 3)
2823 return NotImplemented;
2825 if(!graphics || !image || !points || count != 3)
2826 return InvalidParameter;
2828 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2829 debugstr_pointf(&points[2]));
2831 memcpy(ptf, points, 3 * sizeof(GpPointF));
2833 /* Ensure source width/height is positive */
2834 if (srcwidth < 0)
2836 GpPointF tmp = ptf[1];
2837 srcx = srcx + srcwidth;
2838 srcwidth = -srcwidth;
2839 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2840 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2841 ptf[1] = ptf[0];
2842 ptf[0] = tmp;
2845 if (srcheight < 0)
2847 GpPointF tmp = ptf[2];
2848 srcy = srcy + srcheight;
2849 srcheight = -srcheight;
2850 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2851 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2852 ptf[2] = ptf[0];
2853 ptf[0] = tmp;
2856 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2857 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2858 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2859 return Ok;
2860 transform_and_round_points(graphics, pti, ptf, 4);
2862 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2863 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2865 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2866 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2867 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2868 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2869 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2871 if (image->picture)
2873 if (!graphics->hdc)
2875 FIXME("graphics object has no HDC\n");
2878 if(IPicture_Render(image->picture, graphics->hdc,
2879 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2880 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
2882 if(callback)
2883 callback(callbackData);
2884 return GenericError;
2887 else if (image->type == ImageTypeBitmap)
2889 GpBitmap* bitmap = (GpBitmap*)image;
2890 BOOL do_resampling = FALSE;
2891 BOOL use_software = FALSE;
2893 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2894 graphics->xres, graphics->yres,
2895 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2896 graphics->scale, image->xres, image->yres, bitmap->format,
2897 imageAttributes ? imageAttributes->outside_color : 0);
2899 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2900 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2901 srcx < 0 || srcy < 0 ||
2902 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2903 do_resampling = TRUE;
2905 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
2906 (graphics->image && graphics->image->type == ImageTypeBitmap))
2907 use_software = TRUE;
2909 if (use_software)
2911 RECT dst_area;
2912 GpRectF graphics_bounds;
2913 GpRect src_area;
2914 int i, x, y, src_stride, dst_stride;
2915 GpMatrix dst_to_src;
2916 REAL m11, m12, m21, m22, mdx, mdy;
2917 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
2918 BitmapData lockeddata;
2919 InterpolationMode interpolation = graphics->interpolation;
2920 PixelOffsetMode offset_mode = graphics->pixeloffset;
2921 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2922 REAL x_dx, x_dy, y_dx, y_dy;
2923 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2925 if (!imageAttributes)
2926 imageAttributes = &defaultImageAttributes;
2928 dst_area.left = dst_area.right = pti[0].x;
2929 dst_area.top = dst_area.bottom = pti[0].y;
2930 for (i=1; i<4; i++)
2932 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2933 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2934 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2935 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2938 stat = get_graphics_bounds(graphics, &graphics_bounds);
2939 if (stat != Ok) return stat;
2941 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
2942 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
2943 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
2944 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
2946 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2948 if (IsRectEmpty(&dst_area)) return Ok;
2950 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2951 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2952 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2953 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2954 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2955 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2957 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2959 stat = GdipInvertMatrix(&dst_to_src);
2960 if (stat != Ok) return stat;
2962 if (do_resampling)
2964 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2965 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2967 else
2969 /* Make sure src_area is equal in size to dst_area. */
2970 src_area.X = srcx + dst_area.left - pti[0].x;
2971 src_area.Y = srcy + dst_area.top - pti[0].y;
2972 src_area.Width = dst_area.right - dst_area.left;
2973 src_area.Height = dst_area.bottom - dst_area.top;
2976 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
2978 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2979 if (!src_data)
2980 return OutOfMemory;
2981 src_stride = sizeof(ARGB) * src_area.Width;
2983 /* Read the bits we need from the source bitmap into an ARGB buffer. */
2984 lockeddata.Width = src_area.Width;
2985 lockeddata.Height = src_area.Height;
2986 lockeddata.Stride = src_stride;
2987 lockeddata.PixelFormat = PixelFormat32bppARGB;
2988 lockeddata.Scan0 = src_data;
2990 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
2991 PixelFormat32bppARGB, &lockeddata);
2993 if (stat == Ok)
2994 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2996 if (stat != Ok)
2998 GdipFree(src_data);
2999 return stat;
3002 apply_image_attributes(imageAttributes, src_data,
3003 src_area.Width, src_area.Height,
3004 src_stride, ColorAdjustTypeBitmap);
3006 if (do_resampling)
3008 /* Transform the bits as needed to the destination. */
3009 dst_data = dst_dyn_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3010 if (!dst_data)
3012 GdipFree(src_data);
3013 return OutOfMemory;
3016 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3018 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3020 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3021 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3022 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3023 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3025 for (x=dst_area.left; x<dst_area.right; x++)
3027 for (y=dst_area.top; y<dst_area.bottom; y++)
3029 GpPointF src_pointf;
3030 ARGB *dst_color;
3032 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3033 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3035 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3037 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3038 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3039 imageAttributes, interpolation, offset_mode);
3040 else
3041 *dst_color = 0;
3045 else
3047 dst_data = src_data;
3048 dst_stride = src_stride;
3051 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3052 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3054 GdipFree(src_data);
3056 GdipFree(dst_dyn_data);
3058 return stat;
3060 else
3062 HDC hdc;
3063 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3064 HBITMAP hbitmap, old_hbm=NULL;
3066 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3067 bitmap->format == PixelFormat24bppRGB ||
3068 bitmap->format == PixelFormat32bppRGB ||
3069 bitmap->format == PixelFormat32bppPARGB))
3071 BITMAPINFOHEADER bih;
3072 BYTE *temp_bits;
3073 PixelFormat dst_format;
3075 /* we can't draw a bitmap of this format directly */
3076 hdc = CreateCompatibleDC(0);
3077 temp_hdc = TRUE;
3078 temp_bitmap = TRUE;
3080 bih.biSize = sizeof(BITMAPINFOHEADER);
3081 bih.biWidth = bitmap->width;
3082 bih.biHeight = -bitmap->height;
3083 bih.biPlanes = 1;
3084 bih.biBitCount = 32;
3085 bih.biCompression = BI_RGB;
3086 bih.biSizeImage = 0;
3087 bih.biXPelsPerMeter = 0;
3088 bih.biYPelsPerMeter = 0;
3089 bih.biClrUsed = 0;
3090 bih.biClrImportant = 0;
3092 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3093 (void**)&temp_bits, NULL, 0);
3095 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3096 dst_format = PixelFormat32bppPARGB;
3097 else
3098 dst_format = PixelFormat32bppRGB;
3100 convert_pixels(bitmap->width, bitmap->height,
3101 bitmap->width*4, temp_bits, dst_format,
3102 bitmap->stride, bitmap->bits, bitmap->format,
3103 bitmap->image.palette);
3105 else
3107 if (bitmap->hbitmap)
3108 hbitmap = bitmap->hbitmap;
3109 else
3111 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3112 temp_bitmap = TRUE;
3115 hdc = bitmap->hdc;
3116 temp_hdc = (hdc == 0);
3119 if (temp_hdc)
3121 if (!hdc) hdc = CreateCompatibleDC(0);
3122 old_hbm = SelectObject(hdc, hbitmap);
3125 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3127 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3128 hdc, srcx, srcy, srcwidth, srcheight);
3130 else
3132 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3133 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3136 if (temp_hdc)
3138 SelectObject(hdc, old_hbm);
3139 DeleteDC(hdc);
3142 if (temp_bitmap)
3143 DeleteObject(hbitmap);
3146 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3148 GpRectF rc;
3150 rc.X = srcx;
3151 rc.Y = srcy;
3152 rc.Width = srcwidth;
3153 rc.Height = srcheight;
3155 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3156 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3158 else
3160 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3161 return InvalidParameter;
3164 return Ok;
3167 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3168 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3169 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3170 DrawImageAbort callback, VOID * callbackData)
3172 GpPointF pointsF[3];
3173 INT i;
3175 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3176 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3177 callbackData);
3179 if(!points || count!=3)
3180 return InvalidParameter;
3182 for(i = 0; i < count; i++){
3183 pointsF[i].X = (REAL)points[i].X;
3184 pointsF[i].Y = (REAL)points[i].Y;
3187 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3188 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3189 callback, callbackData);
3192 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3193 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3194 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3195 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3196 VOID * callbackData)
3198 GpPointF points[3];
3200 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3201 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3202 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3204 points[0].X = dstx;
3205 points[0].Y = dsty;
3206 points[1].X = dstx + dstwidth;
3207 points[1].Y = dsty;
3208 points[2].X = dstx;
3209 points[2].Y = dsty + dstheight;
3211 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3212 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3215 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3216 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3217 INT srcwidth, INT srcheight, GpUnit srcUnit,
3218 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3219 VOID * callbackData)
3221 GpPointF points[3];
3223 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3224 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3225 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3227 points[0].X = dstx;
3228 points[0].Y = dsty;
3229 points[1].X = dstx + dstwidth;
3230 points[1].Y = dsty;
3231 points[2].X = dstx;
3232 points[2].Y = dsty + dstheight;
3234 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3235 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3238 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3239 REAL x, REAL y, REAL width, REAL height)
3241 RectF bounds;
3242 GpUnit unit;
3243 GpStatus ret;
3245 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3247 if(!graphics || !image)
3248 return InvalidParameter;
3250 ret = GdipGetImageBounds(image, &bounds, &unit);
3251 if(ret != Ok)
3252 return ret;
3254 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3255 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3256 unit, NULL, NULL, NULL);
3259 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3260 INT x, INT y, INT width, INT height)
3262 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3264 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3267 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3268 REAL y1, REAL x2, REAL y2)
3270 GpPointF pt[2];
3272 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3274 pt[0].X = x1;
3275 pt[0].Y = y1;
3276 pt[1].X = x2;
3277 pt[1].Y = y2;
3278 return GdipDrawLines(graphics, pen, pt, 2);
3281 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3282 INT y1, INT x2, INT y2)
3284 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3286 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3289 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3290 GpPointF *points, INT count)
3292 GpStatus status;
3293 GpPath *path;
3295 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3297 if(!pen || !graphics || (count < 2))
3298 return InvalidParameter;
3300 if(graphics->busy)
3301 return ObjectBusy;
3303 status = GdipCreatePath(FillModeAlternate, &path);
3304 if (status != Ok) return status;
3306 status = GdipAddPathLine2(path, points, count);
3307 if (status == Ok)
3308 status = GdipDrawPath(graphics, pen, path);
3310 GdipDeletePath(path);
3311 return status;
3314 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3315 GpPoint *points, INT count)
3317 GpStatus retval;
3318 GpPointF *ptf;
3319 int i;
3321 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3323 ptf = GdipAlloc(count * sizeof(GpPointF));
3324 if(!ptf) return OutOfMemory;
3326 for(i = 0; i < count; i ++){
3327 ptf[i].X = (REAL) points[i].X;
3328 ptf[i].Y = (REAL) points[i].Y;
3331 retval = GdipDrawLines(graphics, pen, ptf, count);
3333 GdipFree(ptf);
3334 return retval;
3337 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3339 INT save_state;
3340 GpStatus retval;
3341 HRGN hrgn=NULL;
3343 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3345 if(!pen || !graphics)
3346 return InvalidParameter;
3348 if(graphics->busy)
3349 return ObjectBusy;
3351 if (!graphics->hdc)
3353 FIXME("graphics object has no HDC\n");
3354 return Ok;
3357 save_state = prepare_dc(graphics, pen);
3359 retval = get_clip_hrgn(graphics, &hrgn);
3361 if (retval != Ok)
3362 goto end;
3364 if (hrgn)
3365 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3367 retval = draw_poly(graphics, pen, path->pathdata.Points,
3368 path->pathdata.Types, path->pathdata.Count, TRUE);
3370 end:
3371 restore_dc(graphics, save_state);
3372 DeleteObject(hrgn);
3374 return retval;
3377 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3378 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3380 GpStatus status;
3381 GpPath *path;
3383 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3384 width, height, startAngle, sweepAngle);
3386 if(!graphics || !pen)
3387 return InvalidParameter;
3389 if(graphics->busy)
3390 return ObjectBusy;
3392 status = GdipCreatePath(FillModeAlternate, &path);
3393 if (status != Ok) return status;
3395 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3396 if (status == Ok)
3397 status = GdipDrawPath(graphics, pen, path);
3399 GdipDeletePath(path);
3400 return status;
3403 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3404 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3406 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3407 width, height, startAngle, sweepAngle);
3409 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3412 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3413 REAL y, REAL width, REAL height)
3415 GpStatus status;
3416 GpPath *path;
3418 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3420 if(!pen || !graphics)
3421 return InvalidParameter;
3423 if(graphics->busy)
3424 return ObjectBusy;
3426 status = GdipCreatePath(FillModeAlternate, &path);
3427 if (status != Ok) return status;
3429 status = GdipAddPathRectangle(path, x, y, width, height);
3430 if (status == Ok)
3431 status = GdipDrawPath(graphics, pen, path);
3433 GdipDeletePath(path);
3434 return status;
3437 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3438 INT y, INT width, INT height)
3440 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3442 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3445 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3446 GDIPCONST GpRectF* rects, INT count)
3448 GpStatus status;
3449 GpPath *path;
3451 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3453 if(!graphics || !pen || !rects || count < 1)
3454 return InvalidParameter;
3456 if(graphics->busy)
3457 return ObjectBusy;
3459 status = GdipCreatePath(FillModeAlternate, &path);
3460 if (status != Ok) return status;
3462 status = GdipAddPathRectangles(path, rects, count);
3463 if (status == Ok)
3464 status = GdipDrawPath(graphics, pen, path);
3466 GdipDeletePath(path);
3467 return status;
3470 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3471 GDIPCONST GpRect* rects, INT count)
3473 GpRectF *rectsF;
3474 GpStatus ret;
3475 INT i;
3477 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3479 if(!rects || count<=0)
3480 return InvalidParameter;
3482 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3483 if(!rectsF)
3484 return OutOfMemory;
3486 for(i = 0;i < count;i++){
3487 rectsF[i].X = (REAL)rects[i].X;
3488 rectsF[i].Y = (REAL)rects[i].Y;
3489 rectsF[i].Width = (REAL)rects[i].Width;
3490 rectsF[i].Height = (REAL)rects[i].Height;
3493 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3494 GdipFree(rectsF);
3496 return ret;
3499 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3500 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3502 GpPath *path;
3503 GpStatus status;
3505 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3506 count, tension, fill);
3508 if(!graphics || !brush || !points)
3509 return InvalidParameter;
3511 if(graphics->busy)
3512 return ObjectBusy;
3514 if(count == 1) /* Do nothing */
3515 return Ok;
3517 status = GdipCreatePath(fill, &path);
3518 if (status != Ok) return status;
3520 status = GdipAddPathClosedCurve2(path, points, count, tension);
3521 if (status == Ok)
3522 status = GdipFillPath(graphics, brush, path);
3524 GdipDeletePath(path);
3525 return status;
3528 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3529 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3531 GpPointF *ptf;
3532 GpStatus stat;
3533 INT i;
3535 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3536 count, tension, fill);
3538 if(!points || count == 0)
3539 return InvalidParameter;
3541 if(count == 1) /* Do nothing */
3542 return Ok;
3544 ptf = GdipAlloc(sizeof(GpPointF)*count);
3545 if(!ptf)
3546 return OutOfMemory;
3548 for(i = 0;i < count;i++){
3549 ptf[i].X = (REAL)points[i].X;
3550 ptf[i].Y = (REAL)points[i].Y;
3553 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3555 GdipFree(ptf);
3557 return stat;
3560 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3561 GDIPCONST GpPointF *points, INT count)
3563 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3564 return GdipFillClosedCurve2(graphics, brush, points, count,
3565 0.5f, FillModeAlternate);
3568 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3569 GDIPCONST GpPoint *points, INT count)
3571 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3572 return GdipFillClosedCurve2I(graphics, brush, points, count,
3573 0.5f, FillModeAlternate);
3576 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3577 REAL y, REAL width, REAL height)
3579 GpStatus stat;
3580 GpPath *path;
3582 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3584 if(!graphics || !brush)
3585 return InvalidParameter;
3587 if(graphics->busy)
3588 return ObjectBusy;
3590 stat = GdipCreatePath(FillModeAlternate, &path);
3592 if (stat == Ok)
3594 stat = GdipAddPathEllipse(path, x, y, width, height);
3596 if (stat == Ok)
3597 stat = GdipFillPath(graphics, brush, path);
3599 GdipDeletePath(path);
3602 return stat;
3605 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3606 INT y, INT width, INT height)
3608 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3610 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3613 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3615 INT save_state;
3616 GpStatus retval;
3617 HRGN hrgn=NULL;
3619 if(!graphics->hdc || !brush_can_fill_path(brush))
3620 return NotImplemented;
3622 save_state = SaveDC(graphics->hdc);
3623 EndPath(graphics->hdc);
3624 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3625 : WINDING));
3627 retval = get_clip_hrgn(graphics, &hrgn);
3629 if (retval != Ok)
3630 goto end;
3632 if (hrgn)
3633 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3635 BeginPath(graphics->hdc);
3636 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3637 path->pathdata.Types, path->pathdata.Count, FALSE);
3639 if(retval != Ok)
3640 goto end;
3642 EndPath(graphics->hdc);
3643 brush_fill_path(graphics, brush);
3645 retval = Ok;
3647 end:
3648 RestoreDC(graphics->hdc, save_state);
3649 DeleteObject(hrgn);
3651 return retval;
3654 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3656 GpStatus stat;
3657 GpRegion *rgn;
3659 if (!brush_can_fill_pixels(brush))
3660 return NotImplemented;
3662 /* FIXME: This could probably be done more efficiently without regions. */
3664 stat = GdipCreateRegionPath(path, &rgn);
3666 if (stat == Ok)
3668 stat = GdipFillRegion(graphics, brush, rgn);
3670 GdipDeleteRegion(rgn);
3673 return stat;
3676 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3678 GpStatus stat = NotImplemented;
3680 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3682 if(!brush || !graphics || !path)
3683 return InvalidParameter;
3685 if(graphics->busy)
3686 return ObjectBusy;
3688 if (!graphics->image && !graphics->alpha_hdc)
3689 stat = GDI32_GdipFillPath(graphics, brush, path);
3691 if (stat == NotImplemented)
3692 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3694 if (stat == NotImplemented)
3696 FIXME("Not implemented for brushtype %i\n", brush->bt);
3697 stat = Ok;
3700 return stat;
3703 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3704 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3706 GpStatus stat;
3707 GpPath *path;
3709 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3710 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3712 if(!graphics || !brush)
3713 return InvalidParameter;
3715 if(graphics->busy)
3716 return ObjectBusy;
3718 stat = GdipCreatePath(FillModeAlternate, &path);
3720 if (stat == Ok)
3722 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3724 if (stat == Ok)
3725 stat = GdipFillPath(graphics, brush, path);
3727 GdipDeletePath(path);
3730 return stat;
3733 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3734 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3736 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3737 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3739 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3742 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3743 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3745 GpStatus stat;
3746 GpPath *path;
3748 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3750 if(!graphics || !brush || !points || !count)
3751 return InvalidParameter;
3753 if(graphics->busy)
3754 return ObjectBusy;
3756 stat = GdipCreatePath(fillMode, &path);
3758 if (stat == Ok)
3760 stat = GdipAddPathPolygon(path, points, count);
3762 if (stat == Ok)
3763 stat = GdipFillPath(graphics, brush, path);
3765 GdipDeletePath(path);
3768 return stat;
3771 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3772 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3774 GpStatus stat;
3775 GpPath *path;
3777 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3779 if(!graphics || !brush || !points || !count)
3780 return InvalidParameter;
3782 if(graphics->busy)
3783 return ObjectBusy;
3785 stat = GdipCreatePath(fillMode, &path);
3787 if (stat == Ok)
3789 stat = GdipAddPathPolygonI(path, points, count);
3791 if (stat == Ok)
3792 stat = GdipFillPath(graphics, brush, path);
3794 GdipDeletePath(path);
3797 return stat;
3800 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3801 GDIPCONST GpPointF *points, INT count)
3803 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3805 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3808 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3809 GDIPCONST GpPoint *points, INT count)
3811 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3813 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3816 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3817 REAL x, REAL y, REAL width, REAL height)
3819 GpRectF rect;
3821 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3823 rect.X = x;
3824 rect.Y = y;
3825 rect.Width = width;
3826 rect.Height = height;
3828 return GdipFillRectangles(graphics, brush, &rect, 1);
3831 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3832 INT x, INT y, INT width, INT height)
3834 GpRectF rect;
3836 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3838 rect.X = (REAL)x;
3839 rect.Y = (REAL)y;
3840 rect.Width = (REAL)width;
3841 rect.Height = (REAL)height;
3843 return GdipFillRectangles(graphics, brush, &rect, 1);
3846 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3847 INT count)
3849 GpStatus status;
3850 GpPath *path;
3852 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3854 if(!graphics || !brush || !rects || count <= 0)
3855 return InvalidParameter;
3857 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3859 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
3860 /* FIXME: Add gdi32 drawing. */
3861 return status;
3864 status = GdipCreatePath(FillModeAlternate, &path);
3865 if (status != Ok) return status;
3867 status = GdipAddPathRectangles(path, rects, count);
3868 if (status == Ok)
3869 status = GdipFillPath(graphics, brush, path);
3871 GdipDeletePath(path);
3872 return status;
3875 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3876 INT count)
3878 GpRectF *rectsF;
3879 GpStatus ret;
3880 INT i;
3882 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3884 if(!rects || count <= 0)
3885 return InvalidParameter;
3887 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3888 if(!rectsF)
3889 return OutOfMemory;
3891 for(i = 0; i < count; i++){
3892 rectsF[i].X = (REAL)rects[i].X;
3893 rectsF[i].Y = (REAL)rects[i].Y;
3894 rectsF[i].X = (REAL)rects[i].Width;
3895 rectsF[i].Height = (REAL)rects[i].Height;
3898 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3899 GdipFree(rectsF);
3901 return ret;
3904 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3905 GpRegion* region)
3907 INT save_state;
3908 GpStatus status;
3909 HRGN hrgn;
3910 RECT rc;
3912 if(!graphics->hdc || !brush_can_fill_path(brush))
3913 return NotImplemented;
3915 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3916 if(status != Ok)
3917 return status;
3919 save_state = SaveDC(graphics->hdc);
3920 EndPath(graphics->hdc);
3922 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3924 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3926 BeginPath(graphics->hdc);
3927 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3928 EndPath(graphics->hdc);
3930 brush_fill_path(graphics, brush);
3933 RestoreDC(graphics->hdc, save_state);
3935 DeleteObject(hrgn);
3937 return Ok;
3940 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3941 GpRegion* region)
3943 GpStatus stat;
3944 GpRegion *temp_region;
3945 GpMatrix world_to_device;
3946 GpRectF graphics_bounds;
3947 DWORD *pixel_data;
3948 HRGN hregion;
3949 RECT bound_rect;
3950 GpRect gp_bound_rect;
3952 if (!brush_can_fill_pixels(brush))
3953 return NotImplemented;
3955 stat = get_graphics_bounds(graphics, &graphics_bounds);
3957 if (stat == Ok)
3958 stat = GdipCloneRegion(region, &temp_region);
3960 if (stat == Ok)
3962 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3963 CoordinateSpaceWorld, &world_to_device);
3965 if (stat == Ok)
3966 stat = GdipTransformRegion(temp_region, &world_to_device);
3968 if (stat == Ok)
3969 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3971 if (stat == Ok)
3972 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
3974 GdipDeleteRegion(temp_region);
3977 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
3979 DeleteObject(hregion);
3980 return Ok;
3983 if (stat == Ok)
3985 gp_bound_rect.X = bound_rect.left;
3986 gp_bound_rect.Y = bound_rect.top;
3987 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
3988 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
3990 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
3991 if (!pixel_data)
3992 stat = OutOfMemory;
3994 if (stat == Ok)
3996 stat = brush_fill_pixels(graphics, brush, pixel_data,
3997 &gp_bound_rect, gp_bound_rect.Width);
3999 if (stat == Ok)
4000 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4001 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4002 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4004 GdipFree(pixel_data);
4007 DeleteObject(hregion);
4010 return stat;
4013 /*****************************************************************************
4014 * GdipFillRegion [GDIPLUS.@]
4016 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4017 GpRegion* region)
4019 GpStatus stat = NotImplemented;
4021 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4023 if (!(graphics && brush && region))
4024 return InvalidParameter;
4026 if(graphics->busy)
4027 return ObjectBusy;
4029 if (!graphics->image && !graphics->alpha_hdc)
4030 stat = GDI32_GdipFillRegion(graphics, brush, region);
4032 if (stat == NotImplemented)
4033 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4035 if (stat == NotImplemented)
4037 FIXME("not implemented for brushtype %i\n", brush->bt);
4038 stat = Ok;
4041 return stat;
4044 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4046 TRACE("(%p,%u)\n", graphics, intention);
4048 if(!graphics)
4049 return InvalidParameter;
4051 if(graphics->busy)
4052 return ObjectBusy;
4054 /* We have no internal operation queue, so there's no need to clear it. */
4056 if (graphics->hdc)
4057 GdiFlush();
4059 return Ok;
4062 /*****************************************************************************
4063 * GdipGetClipBounds [GDIPLUS.@]
4065 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4067 GpStatus status;
4068 GpRegion *clip;
4070 TRACE("(%p, %p)\n", graphics, rect);
4072 if(!graphics)
4073 return InvalidParameter;
4075 if(graphics->busy)
4076 return ObjectBusy;
4078 status = GdipCreateRegion(&clip);
4079 if (status != Ok) return status;
4081 status = GdipGetClip(graphics, clip);
4082 if (status == Ok)
4083 status = GdipGetRegionBounds(clip, graphics, rect);
4085 GdipDeleteRegion(clip);
4086 return status;
4089 /*****************************************************************************
4090 * GdipGetClipBoundsI [GDIPLUS.@]
4092 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4094 TRACE("(%p, %p)\n", graphics, rect);
4096 if(!graphics)
4097 return InvalidParameter;
4099 if(graphics->busy)
4100 return ObjectBusy;
4102 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4105 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4106 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4107 CompositingMode *mode)
4109 TRACE("(%p, %p)\n", graphics, mode);
4111 if(!graphics || !mode)
4112 return InvalidParameter;
4114 if(graphics->busy)
4115 return ObjectBusy;
4117 *mode = graphics->compmode;
4119 return Ok;
4122 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4123 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4124 CompositingQuality *quality)
4126 TRACE("(%p, %p)\n", graphics, quality);
4128 if(!graphics || !quality)
4129 return InvalidParameter;
4131 if(graphics->busy)
4132 return ObjectBusy;
4134 *quality = graphics->compqual;
4136 return Ok;
4139 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4140 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4141 InterpolationMode *mode)
4143 TRACE("(%p, %p)\n", graphics, mode);
4145 if(!graphics || !mode)
4146 return InvalidParameter;
4148 if(graphics->busy)
4149 return ObjectBusy;
4151 *mode = graphics->interpolation;
4153 return Ok;
4156 /* FIXME: Need to handle color depths less than 24bpp */
4157 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4159 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4161 if(!graphics || !argb)
4162 return InvalidParameter;
4164 if(graphics->busy)
4165 return ObjectBusy;
4167 return Ok;
4170 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4172 TRACE("(%p, %p)\n", graphics, scale);
4174 if(!graphics || !scale)
4175 return InvalidParameter;
4177 if(graphics->busy)
4178 return ObjectBusy;
4180 *scale = graphics->scale;
4182 return Ok;
4185 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4187 TRACE("(%p, %p)\n", graphics, unit);
4189 if(!graphics || !unit)
4190 return InvalidParameter;
4192 if(graphics->busy)
4193 return ObjectBusy;
4195 *unit = graphics->unit;
4197 return Ok;
4200 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4201 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4202 *mode)
4204 TRACE("(%p, %p)\n", graphics, mode);
4206 if(!graphics || !mode)
4207 return InvalidParameter;
4209 if(graphics->busy)
4210 return ObjectBusy;
4212 *mode = graphics->pixeloffset;
4214 return Ok;
4217 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4218 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4220 TRACE("(%p, %p)\n", graphics, mode);
4222 if(!graphics || !mode)
4223 return InvalidParameter;
4225 if(graphics->busy)
4226 return ObjectBusy;
4228 *mode = graphics->smoothing;
4230 return Ok;
4233 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4235 TRACE("(%p, %p)\n", graphics, contrast);
4237 if(!graphics || !contrast)
4238 return InvalidParameter;
4240 *contrast = graphics->textcontrast;
4242 return Ok;
4245 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4246 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4247 TextRenderingHint *hint)
4249 TRACE("(%p, %p)\n", graphics, hint);
4251 if(!graphics || !hint)
4252 return InvalidParameter;
4254 if(graphics->busy)
4255 return ObjectBusy;
4257 *hint = graphics->texthint;
4259 return Ok;
4262 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4264 GpRegion *clip_rgn;
4265 GpStatus stat;
4267 TRACE("(%p, %p)\n", graphics, rect);
4269 if(!graphics || !rect)
4270 return InvalidParameter;
4272 if(graphics->busy)
4273 return ObjectBusy;
4275 /* intersect window and graphics clipping regions */
4276 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4277 return stat;
4279 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4280 goto cleanup;
4282 /* get bounds of the region */
4283 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4285 cleanup:
4286 GdipDeleteRegion(clip_rgn);
4288 return stat;
4291 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4293 GpRectF rectf;
4294 GpStatus stat;
4296 TRACE("(%p, %p)\n", graphics, rect);
4298 if(!graphics || !rect)
4299 return InvalidParameter;
4301 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4303 rect->X = gdip_round(rectf.X);
4304 rect->Y = gdip_round(rectf.Y);
4305 rect->Width = gdip_round(rectf.Width);
4306 rect->Height = gdip_round(rectf.Height);
4309 return stat;
4312 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4314 TRACE("(%p, %p)\n", graphics, matrix);
4316 if(!graphics || !matrix)
4317 return InvalidParameter;
4319 if(graphics->busy)
4320 return ObjectBusy;
4322 *matrix = graphics->worldtrans;
4323 return Ok;
4326 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4328 GpSolidFill *brush;
4329 GpStatus stat;
4330 GpRectF wnd_rect;
4332 TRACE("(%p, %x)\n", graphics, color);
4334 if(!graphics)
4335 return InvalidParameter;
4337 if(graphics->busy)
4338 return ObjectBusy;
4340 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4341 return stat;
4343 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4344 GdipDeleteBrush((GpBrush*)brush);
4345 return stat;
4348 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4349 wnd_rect.Width, wnd_rect.Height);
4351 GdipDeleteBrush((GpBrush*)brush);
4353 return Ok;
4356 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4358 TRACE("(%p, %p)\n", graphics, res);
4360 if(!graphics || !res)
4361 return InvalidParameter;
4363 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4366 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4368 GpStatus stat;
4369 GpRegion* rgn;
4370 GpPointF pt;
4372 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4374 if(!graphics || !result)
4375 return InvalidParameter;
4377 if(graphics->busy)
4378 return ObjectBusy;
4380 pt.X = x;
4381 pt.Y = y;
4382 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4383 CoordinateSpaceWorld, &pt, 1)) != Ok)
4384 return stat;
4386 if((stat = GdipCreateRegion(&rgn)) != Ok)
4387 return stat;
4389 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4390 goto cleanup;
4392 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4394 cleanup:
4395 GdipDeleteRegion(rgn);
4396 return stat;
4399 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4401 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4404 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4406 GpStatus stat;
4407 GpRegion* rgn;
4408 GpPointF pts[2];
4410 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4412 if(!graphics || !result)
4413 return InvalidParameter;
4415 if(graphics->busy)
4416 return ObjectBusy;
4418 pts[0].X = x;
4419 pts[0].Y = y;
4420 pts[1].X = x + width;
4421 pts[1].Y = y + height;
4423 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4424 CoordinateSpaceWorld, pts, 2)) != Ok)
4425 return stat;
4427 pts[1].X -= pts[0].X;
4428 pts[1].Y -= pts[0].Y;
4430 if((stat = GdipCreateRegion(&rgn)) != Ok)
4431 return stat;
4433 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4434 goto cleanup;
4436 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4438 cleanup:
4439 GdipDeleteRegion(rgn);
4440 return stat;
4443 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4445 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4448 GpStatus gdip_format_string(HDC hdc,
4449 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4450 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4451 gdip_format_string_callback callback, void *user_data)
4453 WCHAR* stringdup;
4454 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4455 nheight, lineend, lineno = 0;
4456 RectF bounds;
4457 StringAlignment halign;
4458 GpStatus stat = Ok;
4459 SIZE size;
4460 HotkeyPrefix hkprefix;
4461 INT *hotkeyprefix_offsets=NULL;
4462 INT hotkeyprefix_count=0;
4463 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4464 BOOL seen_prefix = FALSE;
4465 GpStringFormat *dyn_format=NULL;
4467 if(length == -1) length = lstrlenW(string);
4469 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4470 if(!stringdup) return OutOfMemory;
4472 if (!format)
4474 stat = GdipStringFormatGetGenericDefault(&dyn_format);
4475 if (stat != Ok)
4477 GdipFree(stringdup);
4478 return stat;
4480 format = dyn_format;
4483 nwidth = rect->Width;
4484 nheight = rect->Height;
4485 if (ignore_empty_clip)
4487 if (!nwidth) nwidth = INT_MAX;
4488 if (!nheight) nheight = INT_MAX;
4491 hkprefix = format->hkprefix;
4493 if (hkprefix == HotkeyPrefixShow)
4495 for (i=0; i<length; i++)
4497 if (string[i] == '&')
4498 hotkeyprefix_count++;
4502 if (hotkeyprefix_count)
4503 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4505 hotkeyprefix_count = 0;
4507 for(i = 0, j = 0; i < length; i++){
4508 /* FIXME: This makes the indexes passed to callback inaccurate. */
4509 if(!isprintW(string[i]) && (string[i] != '\n'))
4510 continue;
4512 /* FIXME: tabs should be handled using tabstops from stringformat */
4513 if (string[i] == '\t')
4514 continue;
4516 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4517 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4518 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4520 seen_prefix = TRUE;
4521 continue;
4524 seen_prefix = FALSE;
4526 stringdup[j] = string[i];
4527 j++;
4530 length = j;
4532 halign = format->align;
4534 while(sum < length){
4535 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4536 nwidth, &fit, NULL, &size);
4537 fitcpy = fit;
4539 if(fit == 0)
4540 break;
4542 for(lret = 0; lret < fit; lret++)
4543 if(*(stringdup + sum + lret) == '\n')
4544 break;
4546 /* Line break code (may look strange, but it imitates windows). */
4547 if(lret < fit)
4548 lineend = fit = lret; /* this is not an off-by-one error */
4549 else if(fit < (length - sum)){
4550 if(*(stringdup + sum + fit) == ' ')
4551 while(*(stringdup + sum + fit) == ' ')
4552 fit++;
4553 else
4554 while(*(stringdup + sum + fit - 1) != ' '){
4555 fit--;
4557 if(*(stringdup + sum + fit) == '\t')
4558 break;
4560 if(fit == 0){
4561 fit = fitcpy;
4562 break;
4565 lineend = fit;
4566 while(*(stringdup + sum + lineend - 1) == ' ' ||
4567 *(stringdup + sum + lineend - 1) == '\t')
4568 lineend--;
4570 else
4571 lineend = fit;
4573 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4574 nwidth, &j, NULL, &size);
4576 bounds.Width = size.cx;
4578 if(height + size.cy > nheight)
4580 if (format->attr & StringFormatFlagsLineLimit)
4581 break;
4582 bounds.Height = nheight - (height + size.cy);
4584 else
4585 bounds.Height = size.cy;
4587 bounds.Y = rect->Y + height;
4589 switch (halign)
4591 case StringAlignmentNear:
4592 default:
4593 bounds.X = rect->X;
4594 break;
4595 case StringAlignmentCenter:
4596 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4597 break;
4598 case StringAlignmentFar:
4599 bounds.X = rect->X + rect->Width - bounds.Width;
4600 break;
4603 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4604 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4605 break;
4607 stat = callback(hdc, stringdup, sum, lineend,
4608 font, rect, format, lineno, &bounds,
4609 &hotkeyprefix_offsets[hotkeyprefix_pos],
4610 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4612 if (stat != Ok)
4613 break;
4615 sum += fit + (lret < fitcpy ? 1 : 0);
4616 height += size.cy;
4617 lineno++;
4619 hotkeyprefix_pos = hotkeyprefix_end_pos;
4621 if(height > nheight)
4622 break;
4624 /* Stop if this was a linewrap (but not if it was a linebreak). */
4625 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
4626 break;
4629 GdipFree(stringdup);
4630 GdipFree(hotkeyprefix_offsets);
4631 GdipDeleteStringFormat(dyn_format);
4633 return stat;
4636 struct measure_ranges_args {
4637 GpRegion **regions;
4638 REAL rel_width, rel_height;
4641 static GpStatus measure_ranges_callback(HDC hdc,
4642 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4643 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4644 INT lineno, const RectF *bounds, INT *underlined_indexes,
4645 INT underlined_index_count, void *user_data)
4647 int i;
4648 GpStatus stat = Ok;
4649 struct measure_ranges_args *args = user_data;
4651 for (i=0; i<format->range_count; i++)
4653 INT range_start = max(index, format->character_ranges[i].First);
4654 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4655 if (range_start < range_end)
4657 GpRectF range_rect;
4658 SIZE range_size;
4660 range_rect.Y = bounds->Y / args->rel_height;
4661 range_rect.Height = bounds->Height / args->rel_height;
4663 GetTextExtentExPointW(hdc, string + index, range_start - index,
4664 INT_MAX, NULL, NULL, &range_size);
4665 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4667 GetTextExtentExPointW(hdc, string + index, range_end - index,
4668 INT_MAX, NULL, NULL, &range_size);
4669 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4671 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4672 if (stat != Ok)
4673 break;
4677 return stat;
4680 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4681 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4682 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4683 INT regionCount, GpRegion** regions)
4685 GpStatus stat;
4686 int i;
4687 HFONT gdifont, oldfont;
4688 struct measure_ranges_args args;
4689 HDC hdc, temp_hdc=NULL;
4690 GpPointF pt[3];
4691 RectF scaled_rect;
4692 REAL margin_x;
4694 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4695 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4697 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4698 return InvalidParameter;
4700 if (regionCount < stringFormat->range_count)
4701 return InvalidParameter;
4703 if(!graphics->hdc)
4705 hdc = temp_hdc = CreateCompatibleDC(0);
4706 if (!temp_hdc) return OutOfMemory;
4708 else
4709 hdc = graphics->hdc;
4711 if (stringFormat->attr)
4712 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4714 pt[0].X = 0.0;
4715 pt[0].Y = 0.0;
4716 pt[1].X = 1.0;
4717 pt[1].Y = 0.0;
4718 pt[2].X = 0.0;
4719 pt[2].Y = 1.0;
4720 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4721 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4722 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4723 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4724 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4726 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4727 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4729 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4730 scaled_rect.Y = layoutRect->Y * args.rel_height;
4731 scaled_rect.Width = layoutRect->Width * args.rel_width;
4732 scaled_rect.Height = layoutRect->Height * args.rel_height;
4734 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4735 oldfont = SelectObject(hdc, gdifont);
4737 for (i=0; i<stringFormat->range_count; i++)
4739 stat = GdipSetEmpty(regions[i]);
4740 if (stat != Ok)
4741 return stat;
4744 args.regions = regions;
4746 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4747 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4749 SelectObject(hdc, oldfont);
4750 DeleteObject(gdifont);
4752 if (temp_hdc)
4753 DeleteDC(temp_hdc);
4755 return stat;
4758 struct measure_string_args {
4759 RectF *bounds;
4760 INT *codepointsfitted;
4761 INT *linesfilled;
4762 REAL rel_width, rel_height;
4765 static GpStatus measure_string_callback(HDC hdc,
4766 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4767 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4768 INT lineno, const RectF *bounds, INT *underlined_indexes,
4769 INT underlined_index_count, void *user_data)
4771 struct measure_string_args *args = user_data;
4772 REAL new_width, new_height;
4774 new_width = bounds->Width / args->rel_width;
4775 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4777 if (new_width > args->bounds->Width)
4778 args->bounds->Width = new_width;
4780 if (new_height > args->bounds->Height)
4781 args->bounds->Height = new_height;
4783 if (args->codepointsfitted)
4784 *args->codepointsfitted = index + length;
4786 if (args->linesfilled)
4787 (*args->linesfilled)++;
4789 return Ok;
4792 /* Find the smallest rectangle that bounds the text when it is printed in rect
4793 * according to the format options listed in format. If rect has 0 width and
4794 * height, then just find the smallest rectangle that bounds the text when it's
4795 * printed at location (rect->X, rect-Y). */
4796 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4797 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4798 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4799 INT *codepointsfitted, INT *linesfilled)
4801 HFONT oldfont, gdifont;
4802 struct measure_string_args args;
4803 HDC temp_hdc=NULL, hdc;
4804 GpPointF pt[3];
4805 RectF scaled_rect;
4806 REAL margin_x;
4807 INT lines, glyphs;
4809 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4810 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4811 bounds, codepointsfitted, linesfilled);
4813 if(!graphics || !string || !font || !rect || !bounds)
4814 return InvalidParameter;
4816 if(!graphics->hdc)
4818 hdc = temp_hdc = CreateCompatibleDC(0);
4819 if (!temp_hdc) return OutOfMemory;
4821 else
4822 hdc = graphics->hdc;
4824 if(linesfilled) *linesfilled = 0;
4825 if(codepointsfitted) *codepointsfitted = 0;
4827 if(format)
4828 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4830 pt[0].X = 0.0;
4831 pt[0].Y = 0.0;
4832 pt[1].X = 1.0;
4833 pt[1].Y = 0.0;
4834 pt[2].X = 0.0;
4835 pt[2].Y = 1.0;
4836 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4837 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4838 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4839 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4840 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4842 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4843 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4845 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4846 scaled_rect.Y = rect->Y * args.rel_height;
4847 scaled_rect.Width = rect->Width * args.rel_width;
4848 scaled_rect.Height = rect->Height * args.rel_height;
4849 if (scaled_rect.Width >= 0.5)
4851 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4852 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4855 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4856 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4858 get_font_hfont(graphics, font, format, &gdifont, NULL);
4859 oldfont = SelectObject(hdc, gdifont);
4861 bounds->X = rect->X;
4862 bounds->Y = rect->Y;
4863 bounds->Width = 0.0;
4864 bounds->Height = 0.0;
4866 args.bounds = bounds;
4867 args.codepointsfitted = &glyphs;
4868 args.linesfilled = &lines;
4869 lines = glyphs = 0;
4871 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4872 measure_string_callback, &args);
4874 if (linesfilled) *linesfilled = lines;
4875 if (codepointsfitted) *codepointsfitted = glyphs;
4877 if (lines)
4878 bounds->Width += margin_x * 2.0;
4880 SelectObject(hdc, oldfont);
4881 DeleteObject(gdifont);
4883 if (temp_hdc)
4884 DeleteDC(temp_hdc);
4886 return Ok;
4889 struct draw_string_args {
4890 GpGraphics *graphics;
4891 GDIPCONST GpBrush *brush;
4892 REAL x, y, rel_width, rel_height, ascent;
4895 static GpStatus draw_string_callback(HDC hdc,
4896 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4897 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4898 INT lineno, const RectF *bounds, INT *underlined_indexes,
4899 INT underlined_index_count, void *user_data)
4901 struct draw_string_args *args = user_data;
4902 PointF position;
4903 GpStatus stat;
4905 position.X = args->x + bounds->X / args->rel_width;
4906 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4908 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4909 args->brush, &position,
4910 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4912 if (stat == Ok && underlined_index_count)
4914 OUTLINETEXTMETRICW otm;
4915 REAL underline_y, underline_height;
4916 int i;
4918 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4920 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4921 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4923 for (i=0; i<underlined_index_count; i++)
4925 REAL start_x, end_x;
4926 SIZE text_size;
4927 INT ofs = underlined_indexes[i] - index;
4929 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4930 start_x = text_size.cx / args->rel_width;
4932 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4933 end_x = text_size.cx / args->rel_width;
4935 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4939 return stat;
4942 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4943 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4944 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4946 HRGN rgn = NULL;
4947 HFONT gdifont;
4948 GpPointF pt[3], rectcpy[4];
4949 POINT corners[4];
4950 REAL rel_width, rel_height, margin_x;
4951 INT save_state, format_flags = 0;
4952 REAL offsety = 0.0;
4953 struct draw_string_args args;
4954 RectF scaled_rect;
4955 HDC hdc, temp_hdc=NULL;
4956 TEXTMETRICW textmetric;
4958 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4959 length, font, debugstr_rectf(rect), format, brush);
4961 if(!graphics || !string || !font || !brush || !rect)
4962 return InvalidParameter;
4964 if(graphics->hdc)
4966 hdc = graphics->hdc;
4968 else
4970 hdc = temp_hdc = CreateCompatibleDC(0);
4973 if(format){
4974 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4976 format_flags = format->attr;
4978 /* Should be no need to explicitly test for StringAlignmentNear as
4979 * that is default behavior if no alignment is passed. */
4980 if(format->vertalign != StringAlignmentNear){
4981 RectF bounds, in_rect = *rect;
4982 in_rect.Height = 0.0; /* avoid height clipping */
4983 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
4985 TRACE("bounds %s\n", debugstr_rectf(&bounds));
4987 if(format->vertalign == StringAlignmentCenter)
4988 offsety = (rect->Height - bounds.Height) / 2;
4989 else if(format->vertalign == StringAlignmentFar)
4990 offsety = (rect->Height - bounds.Height);
4992 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
4995 save_state = SaveDC(hdc);
4997 pt[0].X = 0.0;
4998 pt[0].Y = 0.0;
4999 pt[1].X = 1.0;
5000 pt[1].Y = 0.0;
5001 pt[2].X = 0.0;
5002 pt[2].Y = 1.0;
5003 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5004 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5005 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5006 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5007 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5009 rectcpy[3].X = rectcpy[0].X = rect->X;
5010 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5011 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5012 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5013 transform_and_round_points(graphics, corners, rectcpy, 4);
5015 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5016 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5018 scaled_rect.X = margin_x * rel_width;
5019 scaled_rect.Y = 0.0;
5020 scaled_rect.Width = rel_width * rect->Width;
5021 scaled_rect.Height = rel_height * rect->Height;
5022 if (scaled_rect.Width >= 0.5)
5024 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5025 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5028 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5029 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5031 if (!(format_flags & StringFormatFlagsNoClip) &&
5032 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5033 rect->Width > 0.0 && rect->Height > 0.0)
5035 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5036 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5037 SelectClipRgn(hdc, rgn);
5040 get_font_hfont(graphics, font, format, &gdifont, NULL);
5041 SelectObject(hdc, gdifont);
5043 args.graphics = graphics;
5044 args.brush = brush;
5046 args.x = rect->X;
5047 args.y = rect->Y + offsety;
5049 args.rel_width = rel_width;
5050 args.rel_height = rel_height;
5052 GetTextMetricsW(hdc, &textmetric);
5053 args.ascent = textmetric.tmAscent / rel_height;
5055 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5056 draw_string_callback, &args);
5058 DeleteObject(rgn);
5059 DeleteObject(gdifont);
5061 RestoreDC(hdc, save_state);
5063 DeleteDC(temp_hdc);
5065 return Ok;
5068 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5070 TRACE("(%p)\n", graphics);
5072 if(!graphics)
5073 return InvalidParameter;
5075 if(graphics->busy)
5076 return ObjectBusy;
5078 return GdipSetInfinite(graphics->clip);
5081 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5083 TRACE("(%p)\n", graphics);
5085 if(!graphics)
5086 return InvalidParameter;
5088 if(graphics->busy)
5089 return ObjectBusy;
5091 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5094 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5096 return GdipEndContainer(graphics, state);
5099 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5100 GpMatrixOrder order)
5102 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5104 if(!graphics)
5105 return InvalidParameter;
5107 if(graphics->busy)
5108 return ObjectBusy;
5110 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5113 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5115 return GdipBeginContainer2(graphics, state);
5118 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5119 GraphicsContainer *state)
5121 GraphicsContainerItem *container;
5122 GpStatus sts;
5124 TRACE("(%p, %p)\n", graphics, state);
5126 if(!graphics || !state)
5127 return InvalidParameter;
5129 sts = init_container(&container, graphics);
5130 if(sts != Ok)
5131 return sts;
5133 list_add_head(&graphics->containers, &container->entry);
5134 *state = graphics->contid = container->contid;
5136 return Ok;
5139 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5141 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5142 return NotImplemented;
5145 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5147 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5148 return NotImplemented;
5151 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5153 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5154 return NotImplemented;
5157 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5159 GpStatus sts;
5160 GraphicsContainerItem *container, *container2;
5162 TRACE("(%p, %x)\n", graphics, state);
5164 if(!graphics)
5165 return InvalidParameter;
5167 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5168 if(container->contid == state)
5169 break;
5172 /* did not find a matching container */
5173 if(&container->entry == &graphics->containers)
5174 return Ok;
5176 sts = restore_container(graphics, container);
5177 if(sts != Ok)
5178 return sts;
5180 /* remove all of the containers on top of the found container */
5181 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5182 if(container->contid == state)
5183 break;
5184 list_remove(&container->entry);
5185 delete_container(container);
5188 list_remove(&container->entry);
5189 delete_container(container);
5191 return Ok;
5194 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5195 REAL sy, GpMatrixOrder order)
5197 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5199 if(!graphics)
5200 return InvalidParameter;
5202 if(graphics->busy)
5203 return ObjectBusy;
5205 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5208 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5209 CombineMode mode)
5211 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5213 if(!graphics || !srcgraphics)
5214 return InvalidParameter;
5216 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5219 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5220 CompositingMode mode)
5222 TRACE("(%p, %d)\n", graphics, mode);
5224 if(!graphics)
5225 return InvalidParameter;
5227 if(graphics->busy)
5228 return ObjectBusy;
5230 graphics->compmode = mode;
5232 return Ok;
5235 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5236 CompositingQuality quality)
5238 TRACE("(%p, %d)\n", graphics, quality);
5240 if(!graphics)
5241 return InvalidParameter;
5243 if(graphics->busy)
5244 return ObjectBusy;
5246 graphics->compqual = quality;
5248 return Ok;
5251 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5252 InterpolationMode mode)
5254 TRACE("(%p, %d)\n", graphics, mode);
5256 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5257 return InvalidParameter;
5259 if(graphics->busy)
5260 return ObjectBusy;
5262 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5263 mode = InterpolationModeBilinear;
5265 if (mode == InterpolationModeHighQuality)
5266 mode = InterpolationModeHighQualityBicubic;
5268 graphics->interpolation = mode;
5270 return Ok;
5273 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5275 GpStatus stat;
5277 TRACE("(%p, %.2f)\n", graphics, scale);
5279 if(!graphics || (scale <= 0.0))
5280 return InvalidParameter;
5282 if(graphics->busy)
5283 return ObjectBusy;
5285 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5287 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5288 if (stat != Ok)
5289 return stat;
5292 graphics->scale = scale;
5294 return Ok;
5297 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5299 GpStatus stat;
5301 TRACE("(%p, %d)\n", graphics, unit);
5303 if(!graphics)
5304 return InvalidParameter;
5306 if(graphics->busy)
5307 return ObjectBusy;
5309 if(unit == UnitWorld)
5310 return InvalidParameter;
5312 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5314 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5315 if (stat != Ok)
5316 return stat;
5319 graphics->unit = unit;
5321 return Ok;
5324 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5325 mode)
5327 TRACE("(%p, %d)\n", graphics, mode);
5329 if(!graphics)
5330 return InvalidParameter;
5332 if(graphics->busy)
5333 return ObjectBusy;
5335 graphics->pixeloffset = mode;
5337 return Ok;
5340 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5342 static int calls;
5344 TRACE("(%p,%i,%i)\n", graphics, x, y);
5346 if (!(calls++))
5347 FIXME("value is unused in rendering\n");
5349 if (!graphics)
5350 return InvalidParameter;
5352 graphics->origin_x = x;
5353 graphics->origin_y = y;
5355 return Ok;
5358 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5360 TRACE("(%p,%p,%p)\n", graphics, x, y);
5362 if (!graphics || !x || !y)
5363 return InvalidParameter;
5365 *x = graphics->origin_x;
5366 *y = graphics->origin_y;
5368 return Ok;
5371 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5373 TRACE("(%p, %d)\n", graphics, mode);
5375 if(!graphics)
5376 return InvalidParameter;
5378 if(graphics->busy)
5379 return ObjectBusy;
5381 graphics->smoothing = mode;
5383 return Ok;
5386 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5388 TRACE("(%p, %d)\n", graphics, contrast);
5390 if(!graphics)
5391 return InvalidParameter;
5393 graphics->textcontrast = contrast;
5395 return Ok;
5398 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5399 TextRenderingHint hint)
5401 TRACE("(%p, %d)\n", graphics, hint);
5403 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5404 return InvalidParameter;
5406 if(graphics->busy)
5407 return ObjectBusy;
5409 graphics->texthint = hint;
5411 return Ok;
5414 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5416 TRACE("(%p, %p)\n", graphics, matrix);
5418 if(!graphics || !matrix)
5419 return InvalidParameter;
5421 if(graphics->busy)
5422 return ObjectBusy;
5424 TRACE("%f,%f,%f,%f,%f,%f\n",
5425 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5426 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5428 graphics->worldtrans = *matrix;
5430 return Ok;
5433 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5434 REAL dy, GpMatrixOrder order)
5436 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5438 if(!graphics)
5439 return InvalidParameter;
5441 if(graphics->busy)
5442 return ObjectBusy;
5444 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5447 /*****************************************************************************
5448 * GdipSetClipHrgn [GDIPLUS.@]
5450 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5452 GpRegion *region;
5453 GpStatus status;
5455 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5457 if(!graphics)
5458 return InvalidParameter;
5460 if(graphics->busy)
5461 return ObjectBusy;
5463 /* hrgn is already in device units */
5464 status = GdipCreateRegionHrgn(hrgn, &region);
5465 if(status != Ok)
5466 return status;
5468 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5470 GdipDeleteRegion(region);
5471 return status;
5474 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5476 GpStatus status;
5477 GpPath *clip_path;
5479 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5481 if(!graphics)
5482 return InvalidParameter;
5484 if(graphics->busy)
5485 return ObjectBusy;
5487 status = GdipClonePath(path, &clip_path);
5488 if (status == Ok)
5490 GpMatrix world_to_device;
5492 get_graphics_transform(graphics, CoordinateSpaceDevice,
5493 CoordinateSpaceWorld, &world_to_device);
5494 status = GdipTransformPath(clip_path, &world_to_device);
5495 if (status == Ok)
5496 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5498 GdipDeletePath(clip_path);
5500 return status;
5503 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5504 REAL width, REAL height,
5505 CombineMode mode)
5507 GpStatus status;
5508 GpRectF rect;
5509 GpRegion *region;
5511 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5513 if(!graphics)
5514 return InvalidParameter;
5516 if(graphics->busy)
5517 return ObjectBusy;
5519 rect.X = x;
5520 rect.Y = y;
5521 rect.Width = width;
5522 rect.Height = height;
5523 status = GdipCreateRegionRect(&rect, &region);
5524 if (status == Ok)
5526 GpMatrix world_to_device;
5528 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5529 status = GdipTransformRegion(region, &world_to_device);
5530 if (status == Ok)
5531 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5533 GdipDeleteRegion(region);
5535 return status;
5538 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5539 INT width, INT height,
5540 CombineMode mode)
5542 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5544 if(!graphics)
5545 return InvalidParameter;
5547 if(graphics->busy)
5548 return ObjectBusy;
5550 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5553 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5554 CombineMode mode)
5556 GpStatus status;
5557 GpRegion *clip;
5559 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5561 if(!graphics || !region)
5562 return InvalidParameter;
5564 if(graphics->busy)
5565 return ObjectBusy;
5567 status = GdipCloneRegion(region, &clip);
5568 if (status == Ok)
5570 GpMatrix world_to_device;
5572 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5573 status = GdipTransformRegion(clip, &world_to_device);
5574 if (status == Ok)
5575 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5577 GdipDeleteRegion(clip);
5579 return status;
5582 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5583 INT count)
5585 INT save_state;
5586 POINT *pti;
5588 TRACE("(%p, %p, %d)\n", graphics, points, count);
5590 if(!graphics || !pen || count<=0)
5591 return InvalidParameter;
5593 if(graphics->busy)
5594 return ObjectBusy;
5596 if (!graphics->hdc)
5598 FIXME("graphics object has no HDC\n");
5599 return Ok;
5602 pti = GdipAlloc(sizeof(POINT) * count);
5604 save_state = prepare_dc(graphics, pen);
5605 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5607 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5608 Polygon(graphics->hdc, pti, count);
5610 restore_dc(graphics, save_state);
5611 GdipFree(pti);
5613 return Ok;
5616 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5617 INT count)
5619 GpStatus ret;
5620 GpPointF *ptf;
5621 INT i;
5623 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5625 if(count<=0) return InvalidParameter;
5626 ptf = GdipAlloc(sizeof(GpPointF) * count);
5628 for(i = 0;i < count; i++){
5629 ptf[i].X = (REAL)points[i].X;
5630 ptf[i].Y = (REAL)points[i].Y;
5633 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5634 GdipFree(ptf);
5636 return ret;
5639 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5641 TRACE("(%p, %p)\n", graphics, dpi);
5643 if(!graphics || !dpi)
5644 return InvalidParameter;
5646 if(graphics->busy)
5647 return ObjectBusy;
5649 *dpi = graphics->xres;
5650 return Ok;
5653 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5655 TRACE("(%p, %p)\n", graphics, dpi);
5657 if(!graphics || !dpi)
5658 return InvalidParameter;
5660 if(graphics->busy)
5661 return ObjectBusy;
5663 *dpi = graphics->yres;
5664 return Ok;
5667 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5668 GpMatrixOrder order)
5670 GpMatrix m;
5671 GpStatus ret;
5673 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5675 if(!graphics || !matrix)
5676 return InvalidParameter;
5678 if(graphics->busy)
5679 return ObjectBusy;
5681 m = graphics->worldtrans;
5683 ret = GdipMultiplyMatrix(&m, matrix, order);
5684 if(ret == Ok)
5685 graphics->worldtrans = m;
5687 return ret;
5690 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5691 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5693 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5695 GpStatus stat=Ok;
5697 TRACE("(%p, %p)\n", graphics, hdc);
5699 if(!graphics || !hdc)
5700 return InvalidParameter;
5702 if(graphics->busy)
5703 return ObjectBusy;
5705 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5707 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5709 else if (!graphics->hdc || graphics->alpha_hdc ||
5710 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5712 /* Create a fake HDC and fill it with a constant color. */
5713 HDC temp_hdc;
5714 HBITMAP hbitmap;
5715 GpRectF bounds;
5716 BITMAPINFOHEADER bmih;
5717 int i;
5719 stat = get_graphics_bounds(graphics, &bounds);
5720 if (stat != Ok)
5721 return stat;
5723 graphics->temp_hbitmap_width = bounds.Width;
5724 graphics->temp_hbitmap_height = bounds.Height;
5726 bmih.biSize = sizeof(bmih);
5727 bmih.biWidth = graphics->temp_hbitmap_width;
5728 bmih.biHeight = -graphics->temp_hbitmap_height;
5729 bmih.biPlanes = 1;
5730 bmih.biBitCount = 32;
5731 bmih.biCompression = BI_RGB;
5732 bmih.biSizeImage = 0;
5733 bmih.biXPelsPerMeter = 0;
5734 bmih.biYPelsPerMeter = 0;
5735 bmih.biClrUsed = 0;
5736 bmih.biClrImportant = 0;
5738 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5739 (void**)&graphics->temp_bits, NULL, 0);
5740 if (!hbitmap)
5741 return GenericError;
5743 temp_hdc = CreateCompatibleDC(0);
5744 if (!temp_hdc)
5746 DeleteObject(hbitmap);
5747 return GenericError;
5750 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5751 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5753 SelectObject(temp_hdc, hbitmap);
5755 graphics->temp_hbitmap = hbitmap;
5756 *hdc = graphics->temp_hdc = temp_hdc;
5758 else
5760 *hdc = graphics->hdc;
5763 if (stat == Ok)
5764 graphics->busy = TRUE;
5766 return stat;
5769 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5771 GpStatus stat=Ok;
5773 TRACE("(%p, %p)\n", graphics, hdc);
5775 if(!graphics || !hdc || !graphics->busy)
5776 return InvalidParameter;
5778 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5780 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5782 else if (graphics->temp_hdc == hdc)
5784 DWORD* pos;
5785 int i;
5787 /* Find the pixels that have changed, and mark them as opaque. */
5788 pos = (DWORD*)graphics->temp_bits;
5789 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5791 if (*pos != DC_BACKGROUND_KEY)
5793 *pos |= 0xff000000;
5795 pos++;
5798 /* Write the changed pixels to the real target. */
5799 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5800 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5801 graphics->temp_hbitmap_width * 4);
5803 /* Clean up. */
5804 DeleteDC(graphics->temp_hdc);
5805 DeleteObject(graphics->temp_hbitmap);
5806 graphics->temp_hdc = NULL;
5807 graphics->temp_hbitmap = NULL;
5809 else if (hdc != graphics->hdc)
5811 stat = InvalidParameter;
5814 if (stat == Ok)
5815 graphics->busy = FALSE;
5817 return stat;
5820 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5822 GpRegion *clip;
5823 GpStatus status;
5824 GpMatrix device_to_world;
5826 TRACE("(%p, %p)\n", graphics, region);
5828 if(!graphics || !region)
5829 return InvalidParameter;
5831 if(graphics->busy)
5832 return ObjectBusy;
5834 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5835 return status;
5837 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5838 status = GdipTransformRegion(clip, &device_to_world);
5839 if (status != Ok)
5841 GdipDeleteRegion(clip);
5842 return status;
5845 /* free everything except root node and header */
5846 delete_element(&region->node);
5847 memcpy(region, clip, sizeof(GpRegion));
5848 GdipFree(clip);
5850 return Ok;
5853 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5854 GpCoordinateSpace src_space, GpMatrix *matrix)
5856 GpStatus stat = Ok;
5857 REAL scale_x, scale_y;
5859 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5861 if (dst_space != src_space)
5863 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5864 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5866 if(graphics->unit != UnitDisplay)
5868 scale_x *= graphics->scale;
5869 scale_y *= graphics->scale;
5872 /* transform from src_space to CoordinateSpacePage */
5873 switch (src_space)
5875 case CoordinateSpaceWorld:
5876 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5877 break;
5878 case CoordinateSpacePage:
5879 break;
5880 case CoordinateSpaceDevice:
5881 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5882 break;
5885 /* transform from CoordinateSpacePage to dst_space */
5886 switch (dst_space)
5888 case CoordinateSpaceWorld:
5890 GpMatrix inverted_transform = graphics->worldtrans;
5891 stat = GdipInvertMatrix(&inverted_transform);
5892 if (stat == Ok)
5893 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5894 break;
5896 case CoordinateSpacePage:
5897 break;
5898 case CoordinateSpaceDevice:
5899 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5900 break;
5903 return stat;
5906 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5907 GpCoordinateSpace src_space, GpPointF *points, INT count)
5909 GpMatrix matrix;
5910 GpStatus stat;
5912 if(!graphics || !points || count <= 0)
5913 return InvalidParameter;
5915 if(graphics->busy)
5916 return ObjectBusy;
5918 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5920 if (src_space == dst_space) return Ok;
5922 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5923 if (stat != Ok) return stat;
5925 return GdipTransformMatrixPoints(&matrix, points, count);
5928 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5929 GpCoordinateSpace src_space, GpPoint *points, INT count)
5931 GpPointF *pointsF;
5932 GpStatus ret;
5933 INT i;
5935 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5937 if(count <= 0)
5938 return InvalidParameter;
5940 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5941 if(!pointsF)
5942 return OutOfMemory;
5944 for(i = 0; i < count; i++){
5945 pointsF[i].X = (REAL)points[i].X;
5946 pointsF[i].Y = (REAL)points[i].Y;
5949 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5951 if(ret == Ok)
5952 for(i = 0; i < count; i++){
5953 points[i].X = gdip_round(pointsF[i].X);
5954 points[i].Y = gdip_round(pointsF[i].Y);
5956 GdipFree(pointsF);
5958 return ret;
5961 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5963 static int calls;
5965 TRACE("\n");
5967 if (!calls++)
5968 FIXME("stub\n");
5970 return NULL;
5973 /*****************************************************************************
5974 * GdipTranslateClip [GDIPLUS.@]
5976 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5978 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5980 if(!graphics)
5981 return InvalidParameter;
5983 if(graphics->busy)
5984 return ObjectBusy;
5986 return GdipTranslateRegion(graphics->clip, dx, dy);
5989 /*****************************************************************************
5990 * GdipTranslateClipI [GDIPLUS.@]
5992 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5994 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5996 if(!graphics)
5997 return InvalidParameter;
5999 if(graphics->busy)
6000 return ObjectBusy;
6002 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6006 /*****************************************************************************
6007 * GdipMeasureDriverString [GDIPLUS.@]
6009 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6010 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6011 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6013 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6014 HFONT hfont;
6015 HDC hdc;
6016 REAL min_x, min_y, max_x, max_y, x, y;
6017 int i;
6018 TEXTMETRICW textmetric;
6019 const WORD *glyph_indices;
6020 WORD *dynamic_glyph_indices=NULL;
6021 REAL rel_width, rel_height, ascent, descent;
6022 GpPointF pt[3];
6024 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6026 if (!graphics || !text || !font || !positions || !boundingBox)
6027 return InvalidParameter;
6029 if (length == -1)
6030 length = strlenW(text);
6032 if (length == 0)
6034 boundingBox->X = 0.0;
6035 boundingBox->Y = 0.0;
6036 boundingBox->Width = 0.0;
6037 boundingBox->Height = 0.0;
6040 if (flags & unsupported_flags)
6041 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6043 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6045 hdc = CreateCompatibleDC(0);
6046 SelectObject(hdc, hfont);
6048 GetTextMetricsW(hdc, &textmetric);
6050 pt[0].X = 0.0;
6051 pt[0].Y = 0.0;
6052 pt[1].X = 1.0;
6053 pt[1].Y = 0.0;
6054 pt[2].X = 0.0;
6055 pt[2].Y = 1.0;
6056 if (matrix)
6058 GpMatrix xform = *matrix;
6059 GdipTransformMatrixPoints(&xform, pt, 3);
6061 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6062 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6063 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6064 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6065 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6067 if (flags & DriverStringOptionsCmapLookup)
6069 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6070 if (!glyph_indices)
6072 DeleteDC(hdc);
6073 DeleteObject(hfont);
6074 return OutOfMemory;
6077 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6079 else
6080 glyph_indices = text;
6082 min_x = max_x = x = positions[0].X;
6083 min_y = max_y = y = positions[0].Y;
6085 ascent = textmetric.tmAscent / rel_height;
6086 descent = textmetric.tmDescent / rel_height;
6088 for (i=0; i<length; i++)
6090 int char_width;
6091 ABC abc;
6093 if (!(flags & DriverStringOptionsRealizedAdvance))
6095 x = positions[i].X;
6096 y = positions[i].Y;
6099 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6100 char_width = abc.abcA + abc.abcB + abc.abcC;
6102 if (min_y > y - ascent) min_y = y - ascent;
6103 if (max_y < y + descent) max_y = y + descent;
6104 if (min_x > x) min_x = x;
6106 x += char_width / rel_width;
6108 if (max_x < x) max_x = x;
6111 GdipFree(dynamic_glyph_indices);
6112 DeleteDC(hdc);
6113 DeleteObject(hfont);
6115 boundingBox->X = min_x;
6116 boundingBox->Y = min_y;
6117 boundingBox->Width = max_x - min_x;
6118 boundingBox->Height = max_y - min_y;
6120 return Ok;
6123 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6124 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6125 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6126 INT flags, GDIPCONST GpMatrix *matrix)
6128 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6129 INT save_state;
6130 GpPointF pt;
6131 HFONT hfont;
6132 UINT eto_flags=0;
6134 if (flags & unsupported_flags)
6135 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6137 if (!(flags & DriverStringOptionsCmapLookup))
6138 eto_flags |= ETO_GLYPH_INDEX;
6140 save_state = SaveDC(graphics->hdc);
6141 SetBkMode(graphics->hdc, TRANSPARENT);
6142 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6144 pt = positions[0];
6145 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6147 get_font_hfont(graphics, font, format, &hfont, matrix);
6148 SelectObject(graphics->hdc, hfont);
6150 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6152 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6154 RestoreDC(graphics->hdc, save_state);
6156 DeleteObject(hfont);
6158 return Ok;
6161 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6162 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6163 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6164 INT flags, GDIPCONST GpMatrix *matrix)
6166 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6167 GpStatus stat;
6168 PointF *real_positions, real_position;
6169 POINT *pti;
6170 HFONT hfont;
6171 HDC hdc;
6172 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6173 DWORD max_glyphsize=0;
6174 GLYPHMETRICS glyphmetrics;
6175 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6176 BYTE *glyph_mask;
6177 BYTE *text_mask;
6178 int text_mask_stride;
6179 BYTE *pixel_data;
6180 int pixel_data_stride;
6181 GpRect pixel_area;
6182 UINT ggo_flags = GGO_GRAY8_BITMAP;
6184 if (length <= 0)
6185 return Ok;
6187 if (!(flags & DriverStringOptionsCmapLookup))
6188 ggo_flags |= GGO_GLYPH_INDEX;
6190 if (flags & unsupported_flags)
6191 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6193 pti = GdipAlloc(sizeof(POINT) * length);
6194 if (!pti)
6195 return OutOfMemory;
6197 if (flags & DriverStringOptionsRealizedAdvance)
6199 real_position = positions[0];
6201 transform_and_round_points(graphics, pti, &real_position, 1);
6203 else
6205 real_positions = GdipAlloc(sizeof(PointF) * length);
6206 if (!real_positions)
6208 GdipFree(pti);
6209 return OutOfMemory;
6212 memcpy(real_positions, positions, sizeof(PointF) * length);
6214 transform_and_round_points(graphics, pti, real_positions, length);
6216 GdipFree(real_positions);
6219 get_font_hfont(graphics, font, format, &hfont, matrix);
6221 hdc = CreateCompatibleDC(0);
6222 SelectObject(hdc, hfont);
6224 /* Get the boundaries of the text to be drawn */
6225 for (i=0; i<length; i++)
6227 DWORD glyphsize;
6228 int left, top, right, bottom;
6230 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6231 &glyphmetrics, 0, NULL, &identity);
6233 if (glyphsize == GDI_ERROR)
6235 ERR("GetGlyphOutlineW failed\n");
6236 GdipFree(pti);
6237 DeleteDC(hdc);
6238 DeleteObject(hfont);
6239 return GenericError;
6242 if (glyphsize > max_glyphsize)
6243 max_glyphsize = glyphsize;
6245 if (glyphsize != 0)
6247 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6248 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6249 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6250 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6252 if (left < min_x) min_x = left;
6253 if (top < min_y) min_y = top;
6254 if (right > max_x) max_x = right;
6255 if (bottom > max_y) max_y = bottom;
6258 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6260 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6261 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6265 if (max_glyphsize == 0)
6266 /* Nothing to draw. */
6267 return Ok;
6269 glyph_mask = GdipAlloc(max_glyphsize);
6270 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6271 text_mask_stride = max_x - min_x;
6273 if (!(glyph_mask && text_mask))
6275 GdipFree(glyph_mask);
6276 GdipFree(text_mask);
6277 GdipFree(pti);
6278 DeleteDC(hdc);
6279 DeleteObject(hfont);
6280 return OutOfMemory;
6283 /* Generate a mask for the text */
6284 for (i=0; i<length; i++)
6286 DWORD ret;
6287 int left, top, stride;
6289 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6290 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6292 if (ret == GDI_ERROR || ret == 0)
6293 continue; /* empty glyph */
6295 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6296 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6297 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6299 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6301 BYTE *glyph_val = glyph_mask + y * stride;
6302 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6303 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6305 *text_val = min(64, *text_val + *glyph_val);
6306 glyph_val++;
6307 text_val++;
6312 GdipFree(pti);
6313 DeleteDC(hdc);
6314 DeleteObject(hfont);
6315 GdipFree(glyph_mask);
6317 /* get the brush data */
6318 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6319 if (!pixel_data)
6321 GdipFree(text_mask);
6322 return OutOfMemory;
6325 pixel_area.X = min_x;
6326 pixel_area.Y = min_y;
6327 pixel_area.Width = max_x - min_x;
6328 pixel_area.Height = max_y - min_y;
6329 pixel_data_stride = pixel_area.Width * 4;
6331 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6332 if (stat != Ok)
6334 GdipFree(text_mask);
6335 GdipFree(pixel_data);
6336 return stat;
6339 /* multiply the brush data by the mask */
6340 for (y=0; y<pixel_area.Height; y++)
6342 BYTE *text_val = text_mask + text_mask_stride * y;
6343 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6344 for (x=0; x<pixel_area.Width; x++)
6346 *pixel_val = (*pixel_val) * (*text_val) / 64;
6347 text_val++;
6348 pixel_val+=4;
6352 GdipFree(text_mask);
6354 /* draw the result */
6355 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6356 pixel_area.Height, pixel_data_stride);
6358 GdipFree(pixel_data);
6360 return stat;
6363 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6364 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6365 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6366 INT flags, GDIPCONST GpMatrix *matrix)
6368 GpStatus stat = NotImplemented;
6370 if (length == -1)
6371 length = strlenW(text);
6373 if (graphics->hdc && !graphics->alpha_hdc &&
6374 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6375 brush->bt == BrushTypeSolidColor &&
6376 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6377 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6378 brush, positions, flags, matrix);
6379 if (stat == NotImplemented)
6380 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6381 brush, positions, flags, matrix);
6382 return stat;
6385 /*****************************************************************************
6386 * GdipDrawDriverString [GDIPLUS.@]
6388 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6389 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6390 GDIPCONST PointF *positions, INT flags,
6391 GDIPCONST GpMatrix *matrix )
6393 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6395 if (!graphics || !text || !font || !brush || !positions)
6396 return InvalidParameter;
6398 return draw_driver_string(graphics, text, length, font, NULL,
6399 brush, positions, flags, matrix);
6402 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6403 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6405 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6406 return NotImplemented;
6409 /*****************************************************************************
6410 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6412 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6414 GpStatus stat;
6415 GpRegion* rgn;
6417 TRACE("(%p, %p)\n", graphics, res);
6419 if((stat = GdipCreateRegion(&rgn)) != Ok)
6420 return stat;
6422 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6423 goto cleanup;
6425 stat = GdipIsEmptyRegion(rgn, graphics, res);
6427 cleanup:
6428 GdipDeleteRegion(rgn);
6429 return stat;
6432 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6434 static int calls;
6436 TRACE("(%p) stub\n", graphics);
6438 if(!(calls++))
6439 FIXME("not implemented\n");
6441 return NotImplemented;