wined3d: Allow using more than MAX_COMBINED_SAMPLERS texture image units.
[wine.git] / dlls / gdiplus / graphics.c
blobbd37d925d329712a5c64e86feefa56e96257f5aa
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 /* Converts from gdiplus path point type to gdi path point type. */
55 static BYTE convert_path_point_type(BYTE type)
57 BYTE ret;
59 switch(type & PathPointTypePathTypeMask){
60 case PathPointTypeBezier:
61 ret = PT_BEZIERTO;
62 break;
63 case PathPointTypeLine:
64 ret = PT_LINETO;
65 break;
66 case PathPointTypeStart:
67 ret = PT_MOVETO;
68 break;
69 default:
70 ERR("Bad point type\n");
71 return 0;
74 if(type & PathPointTypeCloseSubpath)
75 ret |= PT_CLOSEFIGURE;
77 return ret;
80 static COLORREF get_gdi_brush_color(const GpBrush *brush)
82 ARGB argb;
84 switch (brush->bt)
86 case BrushTypeSolidColor:
88 const GpSolidFill *sf = (const GpSolidFill *)brush;
89 argb = sf->color;
90 break;
92 case BrushTypeHatchFill:
94 const GpHatch *hatch = (const GpHatch *)brush;
95 argb = hatch->forecol;
96 break;
98 case BrushTypeLinearGradient:
100 const GpLineGradient *line = (const GpLineGradient *)brush;
101 argb = line->startcolor;
102 break;
104 case BrushTypePathGradient:
106 const GpPathGradient *grad = (const GpPathGradient *)brush;
107 argb = grad->centercolor;
108 break;
110 default:
111 FIXME("unhandled brush type %d\n", brush->bt);
112 argb = 0;
113 break;
115 return ARGB2COLORREF(argb);
118 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
120 HBITMAP hbmp;
121 BITMAPINFOHEADER bmih;
122 DWORD *bits;
123 int x, y;
125 bmih.biSize = sizeof(bmih);
126 bmih.biWidth = 8;
127 bmih.biHeight = 8;
128 bmih.biPlanes = 1;
129 bmih.biBitCount = 32;
130 bmih.biCompression = BI_RGB;
131 bmih.biSizeImage = 0;
133 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
134 if (hbmp)
136 const char *hatch_data;
138 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
140 for (y = 0; y < 8; y++)
142 for (x = 0; x < 8; x++)
144 if (hatch_data[y] & (0x80 >> x))
145 bits[y * 8 + x] = hatch->forecol;
146 else
147 bits[y * 8 + x] = hatch->backcol;
151 else
153 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
155 for (y = 0; y < 64; y++)
156 bits[y] = hatch->forecol;
160 return hbmp;
163 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
165 switch (brush->bt)
167 case BrushTypeSolidColor:
169 const GpSolidFill *sf = (const GpSolidFill *)brush;
170 lb->lbStyle = BS_SOLID;
171 lb->lbColor = ARGB2COLORREF(sf->color);
172 lb->lbHatch = 0;
173 return Ok;
176 case BrushTypeHatchFill:
178 const GpHatch *hatch = (const GpHatch *)brush;
179 HBITMAP hbmp;
181 hbmp = create_hatch_bitmap(hatch);
182 if (!hbmp) return OutOfMemory;
184 lb->lbStyle = BS_PATTERN;
185 lb->lbColor = 0;
186 lb->lbHatch = (ULONG_PTR)hbmp;
187 return Ok;
190 default:
191 FIXME("unhandled brush type %d\n", brush->bt);
192 lb->lbStyle = BS_SOLID;
193 lb->lbColor = get_gdi_brush_color(brush);
194 lb->lbHatch = 0;
195 return Ok;
199 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
201 switch (lb->lbStyle)
203 case BS_PATTERN:
204 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
205 break;
207 return Ok;
210 static HBRUSH create_gdi_brush(const GpBrush *brush)
212 LOGBRUSH lb;
213 HBRUSH gdibrush;
215 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
217 gdibrush = CreateBrushIndirect(&lb);
218 free_gdi_logbrush(&lb);
220 return gdibrush;
223 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
225 LOGBRUSH lb;
226 HPEN gdipen;
227 REAL width;
228 INT save_state, i, numdashes;
229 GpPointF pt[2];
230 DWORD dash_array[MAX_DASHLEN];
232 save_state = SaveDC(graphics->hdc);
234 EndPath(graphics->hdc);
236 if(pen->unit == UnitPixel){
237 width = pen->width;
239 else{
240 /* Get an estimate for the amount the pen width is affected by the world
241 * transform. (This is similar to what some of the wine drivers do.) */
242 pt[0].X = 0.0;
243 pt[0].Y = 0.0;
244 pt[1].X = 1.0;
245 pt[1].Y = 1.0;
246 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
247 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
248 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
250 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
251 width *= graphics->scale;
254 if(pen->dash == DashStyleCustom){
255 numdashes = min(pen->numdashes, MAX_DASHLEN);
257 TRACE("dashes are: ");
258 for(i = 0; i < numdashes; i++){
259 dash_array[i] = gdip_round(width * pen->dashes[i]);
260 TRACE("%d, ", dash_array[i]);
262 TRACE("\n and the pen style is %x\n", pen->style);
264 create_gdi_logbrush(pen->brush, &lb);
265 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
266 numdashes, dash_array);
267 free_gdi_logbrush(&lb);
269 else
271 create_gdi_logbrush(pen->brush, &lb);
272 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
273 free_gdi_logbrush(&lb);
276 SelectObject(graphics->hdc, gdipen);
278 return save_state;
281 static void restore_dc(GpGraphics *graphics, INT state)
283 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
284 RestoreDC(graphics->hdc, state);
287 /* This helper applies all the changes that the points listed in ptf need in
288 * order to be drawn on the device context. In the end, this should include at
289 * least:
290 * -scaling by page unit
291 * -applying world transformation
292 * -converting from float to int
293 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
294 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
295 * gdi to draw, and these functions would irreparably mess with line widths.
297 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
298 GpPointF *ptf, INT count)
300 REAL scale_x, scale_y;
301 GpMatrix matrix;
302 int i;
304 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
305 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
307 /* apply page scale */
308 if(graphics->unit != UnitDisplay)
310 scale_x *= graphics->scale;
311 scale_y *= graphics->scale;
314 matrix = graphics->worldtrans;
315 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
316 GdipTransformMatrixPoints(&matrix, ptf, count);
318 for(i = 0; i < count; i++){
319 pti[i].x = gdip_round(ptf[i].X);
320 pti[i].y = gdip_round(ptf[i].Y);
324 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
325 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
327 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
329 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
331 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
332 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
334 else
336 BLENDFUNCTION bf;
338 bf.BlendOp = AC_SRC_OVER;
339 bf.BlendFlags = 0;
340 bf.SourceConstantAlpha = 255;
341 bf.AlphaFormat = AC_SRC_ALPHA;
343 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
344 hdc, src_x, src_y, src_width, src_height, bf);
348 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
350 /* clipping region is in device coords */
351 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
354 /* Draw ARGB data to the given graphics object */
355 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
356 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
358 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
359 INT x, y;
361 for (y=0; y<src_height; y++)
363 for (x=0; x<src_width; x++)
365 ARGB dst_color, src_color;
366 src_color = ((ARGB*)(src + src_stride * y))[x];
368 if (!(src_color & 0xff000000))
369 continue;
371 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
372 if (fmt & PixelFormatPAlpha)
373 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
374 else
375 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
379 return Ok;
382 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
383 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
385 HDC hdc;
386 HBITMAP hbitmap;
387 BITMAPINFOHEADER bih;
388 BYTE *temp_bits;
390 hdc = CreateCompatibleDC(0);
392 bih.biSize = sizeof(BITMAPINFOHEADER);
393 bih.biWidth = src_width;
394 bih.biHeight = -src_height;
395 bih.biPlanes = 1;
396 bih.biBitCount = 32;
397 bih.biCompression = BI_RGB;
398 bih.biSizeImage = 0;
399 bih.biXPelsPerMeter = 0;
400 bih.biYPelsPerMeter = 0;
401 bih.biClrUsed = 0;
402 bih.biClrImportant = 0;
404 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
405 (void**)&temp_bits, NULL, 0);
407 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE ||
408 fmt & PixelFormatPAlpha)
409 memcpy(temp_bits, src, src_width * src_height * 4);
410 else
411 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
412 4 * src_width, src, src_stride);
414 SelectObject(hdc, hbitmap);
415 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
416 hdc, 0, 0, src_width, src_height);
417 DeleteDC(hdc);
418 DeleteObject(hbitmap);
420 return Ok;
423 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
424 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
426 GpStatus stat=Ok;
428 if (graphics->image && graphics->image->type == ImageTypeBitmap)
430 DWORD i;
431 int size;
432 RGNDATA *rgndata;
433 RECT *rects;
434 HRGN hrgn, visible_rgn;
436 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
437 if (!hrgn)
438 return OutOfMemory;
440 stat = get_clip_hrgn(graphics, &visible_rgn);
441 if (stat != Ok)
443 DeleteObject(hrgn);
444 return stat;
447 if (visible_rgn)
449 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
450 DeleteObject(visible_rgn);
453 if (hregion)
454 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
456 size = GetRegionData(hrgn, 0, NULL);
458 rgndata = heap_alloc_zero(size);
459 if (!rgndata)
461 DeleteObject(hrgn);
462 return OutOfMemory;
465 GetRegionData(hrgn, size, rgndata);
467 rects = (RECT*)rgndata->Buffer;
469 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
471 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
472 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
473 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
474 src_stride, fmt);
477 heap_free(rgndata);
479 DeleteObject(hrgn);
481 return stat;
483 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
485 ERR("This should not be used for metafiles; fix caller\n");
486 return NotImplemented;
488 else
490 HRGN hrgn;
491 int save;
493 stat = get_clip_hrgn(graphics, &hrgn);
495 if (stat != Ok)
496 return stat;
498 save = SaveDC(graphics->hdc);
500 if (hrgn)
501 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
503 if (hregion)
504 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
506 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
507 src_height, src_stride, fmt);
509 RestoreDC(graphics->hdc, save);
511 DeleteObject(hrgn);
513 return stat;
517 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
518 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
520 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
523 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
525 INT start_a, end_a, final_a;
526 INT pos;
528 pos = gdip_round(position * 0xff);
530 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
531 end_a = ((end >> 24) & 0xff) * pos;
533 final_a = start_a + end_a;
535 if (final_a < 0xff) return 0;
537 return (final_a / 0xff) << 24 |
538 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
539 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
540 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
543 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
545 REAL blendfac;
547 /* clamp to between 0.0 and 1.0, using the wrap mode */
548 if (brush->wrap == WrapModeTile)
550 position = fmodf(position, 1.0f);
551 if (position < 0.0f) position += 1.0f;
553 else /* WrapModeFlip* */
555 position = fmodf(position, 2.0f);
556 if (position < 0.0f) position += 2.0f;
557 if (position > 1.0f) position = 2.0f - position;
560 if (brush->blendcount == 1)
561 blendfac = position;
562 else
564 int i=1;
565 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
566 REAL range;
568 /* locate the blend positions surrounding this position */
569 while (position > brush->blendpos[i])
570 i++;
572 /* interpolate between the blend positions */
573 left_blendpos = brush->blendpos[i-1];
574 left_blendfac = brush->blendfac[i-1];
575 right_blendpos = brush->blendpos[i];
576 right_blendfac = brush->blendfac[i];
577 range = right_blendpos - left_blendpos;
578 blendfac = (left_blendfac * (right_blendpos - position) +
579 right_blendfac * (position - left_blendpos)) / range;
582 if (brush->pblendcount == 0)
583 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
584 else
586 int i=1;
587 ARGB left_blendcolor, right_blendcolor;
588 REAL left_blendpos, right_blendpos;
590 /* locate the blend colors surrounding this position */
591 while (blendfac > brush->pblendpos[i])
592 i++;
594 /* interpolate between the blend colors */
595 left_blendpos = brush->pblendpos[i-1];
596 left_blendcolor = brush->pblendcolor[i-1];
597 right_blendpos = brush->pblendpos[i];
598 right_blendcolor = brush->pblendcolor[i];
599 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
600 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
604 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
606 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
607 BOOL identity = TRUE;
608 int i, j;
610 for (i=0; i<4; i++)
611 for (j=0; j<5; j++)
613 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
614 identity = FALSE;
615 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
618 return identity;
621 static ARGB transform_color(ARGB color, int matrix[5][5])
623 int val[5], res[4];
624 int i, j;
625 unsigned char a, r, g, b;
627 val[0] = ((color >> 16) & 0xff); /* red */
628 val[1] = ((color >> 8) & 0xff); /* green */
629 val[2] = (color & 0xff); /* blue */
630 val[3] = ((color >> 24) & 0xff); /* alpha */
631 val[4] = 255; /* translation */
633 for (i=0; i<4; i++)
635 res[i] = 0;
637 for (j=0; j<5; j++)
638 res[i] += matrix[j][i] * val[j];
641 a = min(max(res[3] / 256, 0), 255);
642 r = min(max(res[0] / 256, 0), 255);
643 g = min(max(res[1] / 256, 0), 255);
644 b = min(max(res[2] / 256, 0), 255);
646 return (a << 24) | (r << 16) | (g << 8) | b;
649 static BOOL color_is_gray(ARGB color)
651 unsigned char r, g, b;
653 r = (color >> 16) & 0xff;
654 g = (color >> 8) & 0xff;
655 b = color & 0xff;
657 return (r == g) && (g == b);
660 /* returns preferred pixel format for the applied attributes */
661 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
662 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
664 UINT x, y;
665 INT i;
667 if (attributes->colorkeys[type].enabled ||
668 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
670 const struct color_key *key;
671 BYTE min_blue, min_green, min_red;
672 BYTE max_blue, max_green, max_red;
674 if (!data || fmt != PixelFormat32bppARGB)
675 return PixelFormat32bppARGB;
677 if (attributes->colorkeys[type].enabled)
678 key = &attributes->colorkeys[type];
679 else
680 key = &attributes->colorkeys[ColorAdjustTypeDefault];
682 min_blue = key->low&0xff;
683 min_green = (key->low>>8)&0xff;
684 min_red = (key->low>>16)&0xff;
686 max_blue = key->high&0xff;
687 max_green = (key->high>>8)&0xff;
688 max_red = (key->high>>16)&0xff;
690 for (x=0; x<width; x++)
691 for (y=0; y<height; y++)
693 ARGB *src_color;
694 BYTE blue, green, red;
695 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
696 blue = *src_color&0xff;
697 green = (*src_color>>8)&0xff;
698 red = (*src_color>>16)&0xff;
699 if (blue >= min_blue && green >= min_green && red >= min_red &&
700 blue <= max_blue && green <= max_green && red <= max_red)
701 *src_color = 0x00000000;
705 if (attributes->colorremaptables[type].enabled ||
706 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
708 const struct color_remap_table *table;
710 if (!data || fmt != PixelFormat32bppARGB)
711 return PixelFormat32bppARGB;
713 if (attributes->colorremaptables[type].enabled)
714 table = &attributes->colorremaptables[type];
715 else
716 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
718 for (x=0; x<width; x++)
719 for (y=0; y<height; y++)
721 ARGB *src_color;
722 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
723 for (i=0; i<table->mapsize; i++)
725 if (*src_color == table->colormap[i].oldColor.Argb)
727 *src_color = table->colormap[i].newColor.Argb;
728 break;
734 if (attributes->colormatrices[type].enabled ||
735 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
737 const struct color_matrix *colormatrices;
738 int color_matrix[5][5];
739 int gray_matrix[5][5];
740 BOOL identity;
742 if (!data || fmt != PixelFormat32bppARGB)
743 return PixelFormat32bppARGB;
745 if (attributes->colormatrices[type].enabled)
746 colormatrices = &attributes->colormatrices[type];
747 else
748 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
750 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
752 if (colormatrices->flags == ColorMatrixFlagsAltGray)
753 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
755 if (!identity)
757 for (x=0; x<width; x++)
759 for (y=0; y<height; y++)
761 ARGB *src_color;
762 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
764 if (colormatrices->flags == ColorMatrixFlagsDefault ||
765 !color_is_gray(*src_color))
767 *src_color = transform_color(*src_color, color_matrix);
769 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
771 *src_color = transform_color(*src_color, gray_matrix);
778 if (attributes->gamma_enabled[type] ||
779 attributes->gamma_enabled[ColorAdjustTypeDefault])
781 REAL gamma;
783 if (!data || fmt != PixelFormat32bppARGB)
784 return PixelFormat32bppARGB;
786 if (attributes->gamma_enabled[type])
787 gamma = attributes->gamma[type];
788 else
789 gamma = attributes->gamma[ColorAdjustTypeDefault];
791 for (x=0; x<width; x++)
792 for (y=0; y<height; y++)
794 ARGB *src_color;
795 BYTE blue, green, red;
796 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
798 blue = *src_color&0xff;
799 green = (*src_color>>8)&0xff;
800 red = (*src_color>>16)&0xff;
802 /* FIXME: We should probably use a table for this. */
803 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
804 green = floorf(powf(green / 255.0, gamma) * 255.0);
805 red = floorf(powf(red / 255.0, gamma) * 255.0);
807 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
811 return fmt;
814 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
815 * bitmap that contains all the pixels we may need to draw it. */
816 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
817 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
818 GpRect *rect)
820 INT left, top, right, bottom;
822 switch (interpolation)
824 case InterpolationModeHighQualityBilinear:
825 case InterpolationModeHighQualityBicubic:
826 /* FIXME: Include a greater range for the prefilter? */
827 case InterpolationModeBicubic:
828 case InterpolationModeBilinear:
829 left = (INT)(floorf(srcx));
830 top = (INT)(floorf(srcy));
831 right = (INT)(ceilf(srcx+srcwidth));
832 bottom = (INT)(ceilf(srcy+srcheight));
833 break;
834 case InterpolationModeNearestNeighbor:
835 default:
836 left = gdip_round(srcx);
837 top = gdip_round(srcy);
838 right = gdip_round(srcx+srcwidth);
839 bottom = gdip_round(srcy+srcheight);
840 break;
843 if (wrap == WrapModeClamp)
845 if (left < 0)
846 left = 0;
847 if (top < 0)
848 top = 0;
849 if (right >= bitmap->width)
850 right = bitmap->width-1;
851 if (bottom >= bitmap->height)
852 bottom = bitmap->height-1;
853 if (bottom < top || right < left)
854 /* entirely outside image, just sample a pixel so we don't have to
855 * special-case this later */
856 left = top = right = bottom = 0;
858 else
860 /* In some cases we can make the rectangle smaller here, but the logic
861 * is hard to get right, and tiling suggests we're likely to use the
862 * entire source image. */
863 if (left < 0 || right >= bitmap->width)
865 left = 0;
866 right = bitmap->width-1;
869 if (top < 0 || bottom >= bitmap->height)
871 top = 0;
872 bottom = bitmap->height-1;
876 rect->X = left;
877 rect->Y = top;
878 rect->Width = right - left + 1;
879 rect->Height = bottom - top + 1;
882 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
883 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
885 if (attributes->wrap == WrapModeClamp)
887 if (x < 0 || y < 0 || x >= width || y >= height)
888 return attributes->outside_color;
890 else
892 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
893 if (x < 0)
894 x = width*2 + x % (width * 2);
895 if (y < 0)
896 y = height*2 + y % (height * 2);
898 if ((attributes->wrap & 1) == 1)
900 /* Flip X */
901 if ((x / width) % 2 == 0)
902 x = x % width;
903 else
904 x = width - 1 - x % width;
906 else
907 x = x % width;
909 if ((attributes->wrap & 2) == 2)
911 /* Flip Y */
912 if ((y / height) % 2 == 0)
913 y = y % height;
914 else
915 y = height - 1 - y % height;
917 else
918 y = y % height;
921 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
923 ERR("out of range pixel requested\n");
924 return 0xffcd0084;
927 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
930 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
931 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
932 InterpolationMode interpolation, PixelOffsetMode offset_mode)
934 static int fixme;
936 switch (interpolation)
938 default:
939 if (!fixme++)
940 FIXME("Unimplemented interpolation %i\n", interpolation);
941 /* fall-through */
942 case InterpolationModeBilinear:
944 REAL leftxf, topyf;
945 INT leftx, rightx, topy, bottomy;
946 ARGB topleft, topright, bottomleft, bottomright;
947 ARGB top, bottom;
948 float x_offset;
950 leftxf = floorf(point->X);
951 leftx = (INT)leftxf;
952 rightx = (INT)ceilf(point->X);
953 topyf = floorf(point->Y);
954 topy = (INT)topyf;
955 bottomy = (INT)ceilf(point->Y);
957 if (leftx == rightx && topy == bottomy)
958 return sample_bitmap_pixel(src_rect, bits, width, height,
959 leftx, topy, attributes);
961 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
962 leftx, topy, attributes);
963 topright = sample_bitmap_pixel(src_rect, bits, width, height,
964 rightx, topy, attributes);
965 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
966 leftx, bottomy, attributes);
967 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
968 rightx, bottomy, attributes);
970 x_offset = point->X - leftxf;
971 top = blend_colors(topleft, topright, x_offset);
972 bottom = blend_colors(bottomleft, bottomright, x_offset);
974 return blend_colors(top, bottom, point->Y - topyf);
976 case InterpolationModeNearestNeighbor:
978 FLOAT pixel_offset;
979 switch (offset_mode)
981 default:
982 case PixelOffsetModeNone:
983 case PixelOffsetModeHighSpeed:
984 pixel_offset = 0.5;
985 break;
987 case PixelOffsetModeHalf:
988 case PixelOffsetModeHighQuality:
989 pixel_offset = 0.0;
990 break;
992 return sample_bitmap_pixel(src_rect, bits, width, height,
993 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
999 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1001 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1004 static BOOL brush_can_fill_path(GpBrush *brush)
1006 switch (brush->bt)
1008 case BrushTypeSolidColor:
1009 return TRUE;
1010 case BrushTypeHatchFill:
1012 GpHatch *hatch = (GpHatch*)brush;
1013 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1014 ((hatch->backcol & 0xff000000) == 0xff000000);
1016 case BrushTypeLinearGradient:
1017 case BrushTypeTextureFill:
1018 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1019 default:
1020 return FALSE;
1024 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1026 switch (brush->bt)
1028 case BrushTypeSolidColor:
1030 GpSolidFill *fill = (GpSolidFill*)brush;
1031 HBITMAP bmp = ARGB2BMP(fill->color);
1033 if (bmp)
1035 RECT rc;
1036 /* partially transparent fill */
1038 SelectClipPath(graphics->hdc, RGN_AND);
1039 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1041 HDC hdc = CreateCompatibleDC(NULL);
1043 if (!hdc) break;
1045 SelectObject(hdc, bmp);
1046 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1047 hdc, 0, 0, 1, 1);
1048 DeleteDC(hdc);
1051 DeleteObject(bmp);
1052 break;
1054 /* else fall through */
1056 default:
1058 HBRUSH gdibrush, old_brush;
1060 gdibrush = create_gdi_brush(brush);
1061 if (!gdibrush) return;
1063 old_brush = SelectObject(graphics->hdc, gdibrush);
1064 FillPath(graphics->hdc);
1065 SelectObject(graphics->hdc, old_brush);
1066 DeleteObject(gdibrush);
1067 break;
1072 static BOOL brush_can_fill_pixels(GpBrush *brush)
1074 switch (brush->bt)
1076 case BrushTypeSolidColor:
1077 case BrushTypeHatchFill:
1078 case BrushTypeLinearGradient:
1079 case BrushTypeTextureFill:
1080 case BrushTypePathGradient:
1081 return TRUE;
1082 default:
1083 return FALSE;
1087 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1088 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1090 switch (brush->bt)
1092 case BrushTypeSolidColor:
1094 int x, y;
1095 GpSolidFill *fill = (GpSolidFill*)brush;
1096 for (x=0; x<fill_area->Width; x++)
1097 for (y=0; y<fill_area->Height; y++)
1098 argb_pixels[x + y*cdwStride] = fill->color;
1099 return Ok;
1101 case BrushTypeHatchFill:
1103 int x, y;
1104 GpHatch *fill = (GpHatch*)brush;
1105 const char *hatch_data;
1107 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1108 return NotImplemented;
1110 for (x=0; x<fill_area->Width; x++)
1111 for (y=0; y<fill_area->Height; y++)
1113 int hx, hy;
1115 /* FIXME: Account for the rendering origin */
1116 hx = (x + fill_area->X) % 8;
1117 hy = (y + fill_area->Y) % 8;
1119 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1120 argb_pixels[x + y*cdwStride] = fill->forecol;
1121 else
1122 argb_pixels[x + y*cdwStride] = fill->backcol;
1125 return Ok;
1127 case BrushTypeLinearGradient:
1129 GpLineGradient *fill = (GpLineGradient*)brush;
1130 GpPointF draw_points[3], line_points[3];
1131 GpStatus stat;
1132 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1133 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1134 int x, y;
1136 draw_points[0].X = fill_area->X;
1137 draw_points[0].Y = fill_area->Y;
1138 draw_points[1].X = fill_area->X+1;
1139 draw_points[1].Y = fill_area->Y;
1140 draw_points[2].X = fill_area->X;
1141 draw_points[2].Y = fill_area->Y+1;
1143 /* Transform the points to a co-ordinate space where X is the point's
1144 * position in the gradient, 0.0 being the start point and 1.0 the
1145 * end point. */
1146 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1147 CoordinateSpaceDevice, draw_points, 3);
1149 if (stat == Ok)
1151 line_points[0] = fill->startpoint;
1152 line_points[1] = fill->endpoint;
1153 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1154 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1156 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1159 if (stat == Ok)
1161 stat = GdipInvertMatrix(world_to_gradient);
1163 if (stat == Ok)
1164 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1166 GdipDeleteMatrix(world_to_gradient);
1169 if (stat == Ok)
1171 REAL x_delta = draw_points[1].X - draw_points[0].X;
1172 REAL y_delta = draw_points[2].X - draw_points[0].X;
1174 for (y=0; y<fill_area->Height; y++)
1176 for (x=0; x<fill_area->Width; x++)
1178 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1180 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1185 return stat;
1187 case BrushTypeTextureFill:
1189 GpTexture *fill = (GpTexture*)brush;
1190 GpPointF draw_points[3];
1191 GpStatus stat;
1192 int x, y;
1193 GpBitmap *bitmap;
1194 int src_stride;
1195 GpRect src_area;
1197 if (fill->image->type != ImageTypeBitmap)
1199 FIXME("metafile texture brushes not implemented\n");
1200 return NotImplemented;
1203 bitmap = (GpBitmap*)fill->image;
1204 src_stride = sizeof(ARGB) * bitmap->width;
1206 src_area.X = src_area.Y = 0;
1207 src_area.Width = bitmap->width;
1208 src_area.Height = bitmap->height;
1210 draw_points[0].X = fill_area->X;
1211 draw_points[0].Y = fill_area->Y;
1212 draw_points[1].X = fill_area->X+1;
1213 draw_points[1].Y = fill_area->Y;
1214 draw_points[2].X = fill_area->X;
1215 draw_points[2].Y = fill_area->Y+1;
1217 /* Transform the points to the co-ordinate space of the bitmap. */
1218 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1219 CoordinateSpaceDevice, draw_points, 3);
1221 if (stat == Ok)
1223 GpMatrix world_to_texture = fill->transform;
1225 stat = GdipInvertMatrix(&world_to_texture);
1226 if (stat == Ok)
1227 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1230 if (stat == Ok && !fill->bitmap_bits)
1232 BitmapData lockeddata;
1234 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1235 if (!fill->bitmap_bits)
1236 stat = OutOfMemory;
1238 if (stat == Ok)
1240 lockeddata.Width = bitmap->width;
1241 lockeddata.Height = bitmap->height;
1242 lockeddata.Stride = src_stride;
1243 lockeddata.PixelFormat = PixelFormat32bppARGB;
1244 lockeddata.Scan0 = fill->bitmap_bits;
1246 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1247 PixelFormat32bppARGB, &lockeddata);
1250 if (stat == Ok)
1251 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1253 if (stat == Ok)
1254 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1255 bitmap->width, bitmap->height,
1256 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1258 if (stat != Ok)
1260 heap_free(fill->bitmap_bits);
1261 fill->bitmap_bits = NULL;
1265 if (stat == Ok)
1267 REAL x_dx = draw_points[1].X - draw_points[0].X;
1268 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1269 REAL y_dx = draw_points[2].X - draw_points[0].X;
1270 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1272 for (y=0; y<fill_area->Height; y++)
1274 for (x=0; x<fill_area->Width; x++)
1276 GpPointF point;
1277 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1278 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1280 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1281 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1282 &point, fill->imageattributes, graphics->interpolation,
1283 graphics->pixeloffset);
1288 return stat;
1290 case BrushTypePathGradient:
1292 GpPathGradient *fill = (GpPathGradient*)brush;
1293 GpPath *flat_path;
1294 GpMatrix world_to_device;
1295 GpStatus stat;
1296 int i, figure_start=0;
1297 GpPointF start_point, end_point, center_point;
1298 BYTE type;
1299 REAL min_yf, max_yf, line1_xf, line2_xf;
1300 INT min_y, max_y, min_x, max_x;
1301 INT x, y;
1302 ARGB outer_color;
1303 static BOOL transform_fixme_once;
1305 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1307 static int once;
1308 if (!once++)
1309 FIXME("path gradient focus not implemented\n");
1312 if (fill->gamma)
1314 static int once;
1315 if (!once++)
1316 FIXME("path gradient gamma correction not implemented\n");
1319 if (fill->blendcount)
1321 static int once;
1322 if (!once++)
1323 FIXME("path gradient blend not implemented\n");
1326 if (fill->pblendcount)
1328 static int once;
1329 if (!once++)
1330 FIXME("path gradient preset blend not implemented\n");
1333 if (!transform_fixme_once)
1335 BOOL is_identity=TRUE;
1336 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1337 if (!is_identity)
1339 FIXME("path gradient transform not implemented\n");
1340 transform_fixme_once = TRUE;
1344 stat = GdipClonePath(fill->path, &flat_path);
1346 if (stat != Ok)
1347 return stat;
1349 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1350 CoordinateSpaceWorld, &world_to_device);
1351 if (stat == Ok)
1353 stat = GdipTransformPath(flat_path, &world_to_device);
1355 if (stat == Ok)
1357 center_point = fill->center;
1358 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1361 if (stat == Ok)
1362 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1365 if (stat != Ok)
1367 GdipDeletePath(flat_path);
1368 return stat;
1371 for (i=0; i<flat_path->pathdata.Count; i++)
1373 int start_center_line=0, end_center_line=0;
1374 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1375 REAL center_distance;
1376 ARGB start_color, end_color;
1377 REAL dy, dx;
1379 type = flat_path->pathdata.Types[i];
1381 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1382 figure_start = i;
1384 start_point = flat_path->pathdata.Points[i];
1386 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1388 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1390 end_point = flat_path->pathdata.Points[figure_start];
1391 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1393 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1395 end_point = flat_path->pathdata.Points[i+1];
1396 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1398 else
1399 continue;
1401 outer_color = start_color;
1403 min_yf = center_point.Y;
1404 if (min_yf > start_point.Y) min_yf = start_point.Y;
1405 if (min_yf > end_point.Y) min_yf = end_point.Y;
1407 if (min_yf < fill_area->Y)
1408 min_y = fill_area->Y;
1409 else
1410 min_y = (INT)ceil(min_yf);
1412 max_yf = center_point.Y;
1413 if (max_yf < start_point.Y) max_yf = start_point.Y;
1414 if (max_yf < end_point.Y) max_yf = end_point.Y;
1416 if (max_yf > fill_area->Y + fill_area->Height)
1417 max_y = fill_area->Y + fill_area->Height;
1418 else
1419 max_y = (INT)ceil(max_yf);
1421 dy = end_point.Y - start_point.Y;
1422 dx = end_point.X - start_point.X;
1424 /* This is proportional to the distance from start-end line to center point. */
1425 center_distance = dy * (start_point.X - center_point.X) +
1426 dx * (center_point.Y - start_point.Y);
1428 for (y=min_y; y<max_y; y++)
1430 REAL yf = (REAL)y;
1432 if (!seen_start && yf >= start_point.Y)
1434 seen_start = TRUE;
1435 start_center_line ^= 1;
1437 if (!seen_end && yf >= end_point.Y)
1439 seen_end = TRUE;
1440 end_center_line ^= 1;
1442 if (!seen_center && yf >= center_point.Y)
1444 seen_center = TRUE;
1445 start_center_line ^= 1;
1446 end_center_line ^= 1;
1449 if (start_center_line)
1450 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1451 else
1452 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1454 if (end_center_line)
1455 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1456 else
1457 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1459 if (line1_xf < line2_xf)
1461 min_x = (INT)ceil(line1_xf);
1462 max_x = (INT)ceil(line2_xf);
1464 else
1466 min_x = (INT)ceil(line2_xf);
1467 max_x = (INT)ceil(line1_xf);
1470 if (min_x < fill_area->X)
1471 min_x = fill_area->X;
1472 if (max_x > fill_area->X + fill_area->Width)
1473 max_x = fill_area->X + fill_area->Width;
1475 for (x=min_x; x<max_x; x++)
1477 REAL xf = (REAL)x;
1478 REAL distance;
1480 if (start_color != end_color)
1482 REAL blend_amount, pdy, pdx;
1483 pdy = yf - center_point.Y;
1484 pdx = xf - center_point.X;
1485 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1486 outer_color = blend_colors(start_color, end_color, blend_amount);
1489 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1490 (end_point.X - start_point.X) * (yf - start_point.Y);
1492 distance = distance / center_distance;
1494 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1495 blend_colors(outer_color, fill->centercolor, distance);
1500 GdipDeletePath(flat_path);
1501 return stat;
1503 default:
1504 return NotImplemented;
1508 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1509 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1510 * should not be called on an hdc that has a path you care about. */
1511 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1512 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1514 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1515 GpMatrix matrix;
1516 HBRUSH brush = NULL;
1517 HPEN pen = NULL;
1518 PointF ptf[4], *custptf = NULL;
1519 POINT pt[4], *custpt = NULL;
1520 BYTE *tp = NULL;
1521 REAL theta, dsmall, dbig, dx, dy = 0.0;
1522 INT i, count;
1523 LOGBRUSH lb;
1524 BOOL customstroke;
1526 if((x1 == x2) && (y1 == y2))
1527 return;
1529 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1531 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1532 if(!customstroke){
1533 brush = CreateSolidBrush(color);
1534 lb.lbStyle = BS_SOLID;
1535 lb.lbColor = color;
1536 lb.lbHatch = 0;
1537 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1538 PS_JOIN_MITER, 1, &lb, 0,
1539 NULL);
1540 oldbrush = SelectObject(graphics->hdc, brush);
1541 oldpen = SelectObject(graphics->hdc, pen);
1544 switch(cap){
1545 case LineCapFlat:
1546 break;
1547 case LineCapSquare:
1548 case LineCapSquareAnchor:
1549 case LineCapDiamondAnchor:
1550 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1551 if(cap == LineCapDiamondAnchor){
1552 dsmall = cos(theta + M_PI_2) * size;
1553 dbig = sin(theta + M_PI_2) * size;
1555 else{
1556 dsmall = cos(theta + M_PI_4) * size;
1557 dbig = sin(theta + M_PI_4) * size;
1560 ptf[0].X = x2 - dsmall;
1561 ptf[1].X = x2 + dbig;
1563 ptf[0].Y = y2 - dbig;
1564 ptf[3].Y = y2 + dsmall;
1566 ptf[1].Y = y2 - dsmall;
1567 ptf[2].Y = y2 + dbig;
1569 ptf[3].X = x2 - dbig;
1570 ptf[2].X = x2 + dsmall;
1572 transform_and_round_points(graphics, pt, ptf, 4);
1573 Polygon(graphics->hdc, pt, 4);
1575 break;
1576 case LineCapArrowAnchor:
1577 size = size * 4.0 / sqrt(3.0);
1579 dx = cos(M_PI / 6.0 + theta) * size;
1580 dy = sin(M_PI / 6.0 + theta) * size;
1582 ptf[0].X = x2 - dx;
1583 ptf[0].Y = y2 - dy;
1585 dx = cos(- M_PI / 6.0 + theta) * size;
1586 dy = sin(- M_PI / 6.0 + theta) * size;
1588 ptf[1].X = x2 - dx;
1589 ptf[1].Y = y2 - dy;
1591 ptf[2].X = x2;
1592 ptf[2].Y = y2;
1594 transform_and_round_points(graphics, pt, ptf, 3);
1595 Polygon(graphics->hdc, pt, 3);
1597 break;
1598 case LineCapRoundAnchor:
1599 dx = dy = ANCHOR_WIDTH * size / 2.0;
1601 ptf[0].X = x2 - dx;
1602 ptf[0].Y = y2 - dy;
1603 ptf[1].X = x2 + dx;
1604 ptf[1].Y = y2 + dy;
1606 transform_and_round_points(graphics, pt, ptf, 2);
1607 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1609 break;
1610 case LineCapTriangle:
1611 size = size / 2.0;
1612 dx = cos(M_PI_2 + theta) * size;
1613 dy = sin(M_PI_2 + theta) * size;
1615 ptf[0].X = x2 - dx;
1616 ptf[0].Y = y2 - dy;
1617 ptf[1].X = x2 + dx;
1618 ptf[1].Y = y2 + dy;
1620 dx = cos(theta) * size;
1621 dy = sin(theta) * size;
1623 ptf[2].X = x2 + dx;
1624 ptf[2].Y = y2 + dy;
1626 transform_and_round_points(graphics, pt, ptf, 3);
1627 Polygon(graphics->hdc, pt, 3);
1629 break;
1630 case LineCapRound:
1631 dx = dy = size / 2.0;
1633 ptf[0].X = x2 - dx;
1634 ptf[0].Y = y2 - dy;
1635 ptf[1].X = x2 + dx;
1636 ptf[1].Y = y2 + dy;
1638 dx = -cos(M_PI_2 + theta) * size;
1639 dy = -sin(M_PI_2 + theta) * size;
1641 ptf[2].X = x2 - dx;
1642 ptf[2].Y = y2 - dy;
1643 ptf[3].X = x2 + dx;
1644 ptf[3].Y = y2 + dy;
1646 transform_and_round_points(graphics, pt, ptf, 4);
1647 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1648 pt[2].y, pt[3].x, pt[3].y);
1650 break;
1651 case LineCapCustom:
1652 if(!custom)
1653 break;
1655 count = custom->pathdata.Count;
1656 custptf = heap_alloc_zero(count * sizeof(PointF));
1657 custpt = heap_alloc_zero(count * sizeof(POINT));
1658 tp = heap_alloc_zero(count);
1660 if(!custptf || !custpt || !tp)
1661 goto custend;
1663 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1665 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1666 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1667 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1668 MatrixOrderAppend);
1669 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1670 GdipTransformMatrixPoints(&matrix, custptf, count);
1672 transform_and_round_points(graphics, custpt, custptf, count);
1674 for(i = 0; i < count; i++)
1675 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1677 if(custom->fill){
1678 BeginPath(graphics->hdc);
1679 PolyDraw(graphics->hdc, custpt, tp, count);
1680 EndPath(graphics->hdc);
1681 StrokeAndFillPath(graphics->hdc);
1683 else
1684 PolyDraw(graphics->hdc, custpt, tp, count);
1686 custend:
1687 heap_free(custptf);
1688 heap_free(custpt);
1689 heap_free(tp);
1690 break;
1691 default:
1692 break;
1695 if(!customstroke){
1696 SelectObject(graphics->hdc, oldbrush);
1697 SelectObject(graphics->hdc, oldpen);
1698 DeleteObject(brush);
1699 DeleteObject(pen);
1703 /* Shortens the line by the given percent by changing x2, y2.
1704 * If percent is > 1.0 then the line will change direction.
1705 * If percent is negative it can lengthen the line. */
1706 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1708 REAL dist, theta, dx, dy;
1710 if((y1 == *y2) && (x1 == *x2))
1711 return;
1713 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1714 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1715 dx = cos(theta) * dist;
1716 dy = sin(theta) * dist;
1718 *x2 = *x2 + dx;
1719 *y2 = *y2 + dy;
1722 /* Shortens the line by the given amount by changing x2, y2.
1723 * If the amount is greater than the distance, the line will become length 0.
1724 * If the amount is negative, it can lengthen the line. */
1725 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1727 REAL dx, dy, percent;
1729 dx = *x2 - x1;
1730 dy = *y2 - y1;
1731 if(dx == 0 && dy == 0)
1732 return;
1734 percent = amt / sqrt(dx * dx + dy * dy);
1735 if(percent >= 1.0){
1736 *x2 = x1;
1737 *y2 = y1;
1738 return;
1741 shorten_line_percent(x1, y1, x2, y2, percent);
1744 /* Conducts a linear search to find the bezier points that will back off
1745 * the endpoint of the curve by a distance of amt. Linear search works
1746 * better than binary in this case because there are multiple solutions,
1747 * and binary searches often find a bad one. I don't think this is what
1748 * Windows does but short of rendering the bezier without GDI's help it's
1749 * the best we can do. If rev then work from the start of the passed points
1750 * instead of the end. */
1751 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1753 GpPointF origpt[4];
1754 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1755 INT i, first = 0, second = 1, third = 2, fourth = 3;
1757 if(rev){
1758 first = 3;
1759 second = 2;
1760 third = 1;
1761 fourth = 0;
1764 origx = pt[fourth].X;
1765 origy = pt[fourth].Y;
1766 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1768 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1769 /* reset bezier points to original values */
1770 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1771 /* Perform magic on bezier points. Order is important here.*/
1772 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1773 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1774 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1775 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1776 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1777 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1779 dx = pt[fourth].X - origx;
1780 dy = pt[fourth].Y - origy;
1782 diff = sqrt(dx * dx + dy * dy);
1783 percent += 0.0005 * amt;
1787 /* Draws a combination of bezier curves and lines between points. */
1788 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1789 GDIPCONST BYTE * types, INT count, BOOL caps)
1791 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1792 BYTE *tp = heap_alloc_zero(count);
1793 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1794 INT i, j;
1795 GpStatus status = GenericError;
1797 if(!count){
1798 status = Ok;
1799 goto end;
1801 if(!pti || !tp || !ptcopy){
1802 status = OutOfMemory;
1803 goto end;
1806 for(i = 1; i < count; i++){
1807 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1808 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1809 || !(types[i + 2] & PathPointTypeBezier)){
1810 ERR("Bad bezier points\n");
1811 goto end;
1813 i += 2;
1817 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1819 /* If we are drawing caps, go through the points and adjust them accordingly,
1820 * and draw the caps. */
1821 if(caps){
1822 switch(types[count - 1] & PathPointTypePathTypeMask){
1823 case PathPointTypeBezier:
1824 if(pen->endcap == LineCapArrowAnchor)
1825 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1826 else if((pen->endcap == LineCapCustom) && pen->customend)
1827 shorten_bezier_amt(&ptcopy[count - 4],
1828 pen->width * pen->customend->inset, FALSE);
1830 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1831 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1832 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1833 pt[count - 1].X, pt[count - 1].Y);
1835 break;
1836 case PathPointTypeLine:
1837 if(pen->endcap == LineCapArrowAnchor)
1838 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1839 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1840 pen->width);
1841 else if((pen->endcap == LineCapCustom) && pen->customend)
1842 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1843 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1844 pen->customend->inset * pen->width);
1846 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1847 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1848 pt[count - 1].Y);
1850 break;
1851 default:
1852 ERR("Bad path last point\n");
1853 goto end;
1856 /* Find start of points */
1857 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1858 == PathPointTypeStart); j++);
1860 switch(types[j] & PathPointTypePathTypeMask){
1861 case PathPointTypeBezier:
1862 if(pen->startcap == LineCapArrowAnchor)
1863 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1864 else if((pen->startcap == LineCapCustom) && pen->customstart)
1865 shorten_bezier_amt(&ptcopy[j - 1],
1866 pen->width * pen->customstart->inset, TRUE);
1868 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1869 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1870 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1871 pt[j - 1].X, pt[j - 1].Y);
1873 break;
1874 case PathPointTypeLine:
1875 if(pen->startcap == LineCapArrowAnchor)
1876 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1877 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1878 pen->width);
1879 else if((pen->startcap == LineCapCustom) && pen->customstart)
1880 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1881 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1882 pen->customstart->inset * pen->width);
1884 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1885 pt[j].X, pt[j].Y, pt[j - 1].X,
1886 pt[j - 1].Y);
1888 break;
1889 default:
1890 ERR("Bad path points\n");
1891 goto end;
1895 transform_and_round_points(graphics, pti, ptcopy, count);
1897 for(i = 0; i < count; i++){
1898 tp[i] = convert_path_point_type(types[i]);
1901 PolyDraw(graphics->hdc, pti, tp, count);
1903 status = Ok;
1905 end:
1906 heap_free(pti);
1907 heap_free(ptcopy);
1908 heap_free(tp);
1910 return status;
1913 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1915 GpStatus result;
1917 BeginPath(graphics->hdc);
1918 result = draw_poly(graphics, NULL, path->pathdata.Points,
1919 path->pathdata.Types, path->pathdata.Count, FALSE);
1920 EndPath(graphics->hdc);
1921 return result;
1924 typedef enum GraphicsContainerType {
1925 BEGIN_CONTAINER,
1926 SAVE_GRAPHICS
1927 } GraphicsContainerType;
1929 typedef struct _GraphicsContainerItem {
1930 struct list entry;
1931 GraphicsContainer contid;
1932 GraphicsContainerType type;
1934 SmoothingMode smoothing;
1935 CompositingQuality compqual;
1936 InterpolationMode interpolation;
1937 CompositingMode compmode;
1938 TextRenderingHint texthint;
1939 REAL scale;
1940 GpUnit unit;
1941 PixelOffsetMode pixeloffset;
1942 UINT textcontrast;
1943 GpMatrix worldtrans;
1944 GpRegion* clip;
1945 INT origin_x, origin_y;
1946 } GraphicsContainerItem;
1948 static GpStatus init_container(GraphicsContainerItem** container,
1949 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
1950 GpStatus sts;
1952 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
1953 if(!(*container))
1954 return OutOfMemory;
1956 (*container)->contid = graphics->contid + 1;
1957 (*container)->type = type;
1959 (*container)->smoothing = graphics->smoothing;
1960 (*container)->compqual = graphics->compqual;
1961 (*container)->interpolation = graphics->interpolation;
1962 (*container)->compmode = graphics->compmode;
1963 (*container)->texthint = graphics->texthint;
1964 (*container)->scale = graphics->scale;
1965 (*container)->unit = graphics->unit;
1966 (*container)->textcontrast = graphics->textcontrast;
1967 (*container)->pixeloffset = graphics->pixeloffset;
1968 (*container)->origin_x = graphics->origin_x;
1969 (*container)->origin_y = graphics->origin_y;
1970 (*container)->worldtrans = graphics->worldtrans;
1972 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1973 if(sts != Ok){
1974 heap_free(*container);
1975 *container = NULL;
1976 return sts;
1979 return Ok;
1982 static void delete_container(GraphicsContainerItem* container)
1984 GdipDeleteRegion(container->clip);
1985 heap_free(container);
1988 static GpStatus restore_container(GpGraphics* graphics,
1989 GDIPCONST GraphicsContainerItem* container){
1990 GpStatus sts;
1991 GpRegion *newClip;
1993 sts = GdipCloneRegion(container->clip, &newClip);
1994 if(sts != Ok) return sts;
1996 graphics->worldtrans = container->worldtrans;
1998 GdipDeleteRegion(graphics->clip);
1999 graphics->clip = newClip;
2001 graphics->contid = container->contid - 1;
2003 graphics->smoothing = container->smoothing;
2004 graphics->compqual = container->compqual;
2005 graphics->interpolation = container->interpolation;
2006 graphics->compmode = container->compmode;
2007 graphics->texthint = container->texthint;
2008 graphics->scale = container->scale;
2009 graphics->unit = container->unit;
2010 graphics->textcontrast = container->textcontrast;
2011 graphics->pixeloffset = container->pixeloffset;
2012 graphics->origin_x = container->origin_x;
2013 graphics->origin_y = container->origin_y;
2015 return Ok;
2018 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2020 RECT wnd_rect;
2021 GpStatus stat=Ok;
2022 GpUnit unit;
2024 if(graphics->hwnd) {
2025 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2026 return GenericError;
2028 rect->X = wnd_rect.left;
2029 rect->Y = wnd_rect.top;
2030 rect->Width = wnd_rect.right - wnd_rect.left;
2031 rect->Height = wnd_rect.bottom - wnd_rect.top;
2032 }else if (graphics->image){
2033 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2034 if (stat == Ok && unit != UnitPixel)
2035 FIXME("need to convert from unit %i\n", unit);
2036 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2037 HBITMAP hbmp;
2038 BITMAP bmp;
2040 rect->X = 0;
2041 rect->Y = 0;
2043 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2044 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2046 rect->Width = bmp.bmWidth;
2047 rect->Height = bmp.bmHeight;
2049 else
2051 /* FIXME: ??? */
2052 rect->Width = 1;
2053 rect->Height = 1;
2055 }else{
2056 rect->X = 0;
2057 rect->Y = 0;
2058 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2059 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2062 if (graphics->hdc)
2064 POINT points[2];
2066 points[0].x = rect->X;
2067 points[0].y = rect->Y;
2068 points[1].x = rect->X + rect->Width;
2069 points[1].y = rect->Y + rect->Height;
2071 DPtoLP(graphics->hdc, points, sizeof(points)/sizeof(points[0]));
2073 rect->X = min(points[0].x, points[1].x);
2074 rect->Y = min(points[0].y, points[1].y);
2075 rect->Width = abs(points[1].x - points[0].x);
2076 rect->Height = abs(points[1].y - points[0].y);
2079 return stat;
2082 /* on success, rgn will contain the region of the graphics object which
2083 * is visible after clipping has been applied */
2084 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2086 GpStatus stat;
2087 GpRectF rectf;
2088 GpRegion* tmp;
2090 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2091 return stat;
2093 if((stat = GdipCreateRegion(&tmp)) != Ok)
2094 return stat;
2096 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2097 goto end;
2099 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2100 goto end;
2102 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2104 end:
2105 GdipDeleteRegion(tmp);
2106 return stat;
2109 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2111 REAL height;
2113 if (font->unit == UnitPixel)
2115 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2117 else
2119 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2120 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2121 else
2122 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2125 lf->lfHeight = -(height + 0.5);
2126 lf->lfWidth = 0;
2127 lf->lfEscapement = 0;
2128 lf->lfOrientation = 0;
2129 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2130 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2131 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2132 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2133 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2134 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2135 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2136 lf->lfQuality = DEFAULT_QUALITY;
2137 lf->lfPitchAndFamily = 0;
2138 strcpyW(lf->lfFaceName, font->family->FamilyName);
2141 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2142 GDIPCONST GpStringFormat *format, HFONT *hfont,
2143 GDIPCONST GpMatrix *matrix)
2145 HDC hdc = CreateCompatibleDC(0);
2146 GpPointF pt[3];
2147 REAL angle, rel_width, rel_height, font_height;
2148 LOGFONTW lfw;
2149 HFONT unscaled_font;
2150 TEXTMETRICW textmet;
2152 if (font->unit == UnitPixel || font->unit == UnitWorld)
2153 font_height = font->emSize;
2154 else
2156 REAL unit_scale, res;
2158 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2159 unit_scale = units_scale(font->unit, graphics->unit, res);
2161 font_height = font->emSize * unit_scale;
2164 pt[0].X = 0.0;
2165 pt[0].Y = 0.0;
2166 pt[1].X = 1.0;
2167 pt[1].Y = 0.0;
2168 pt[2].X = 0.0;
2169 pt[2].Y = 1.0;
2170 if (matrix)
2172 GpMatrix xform = *matrix;
2173 GdipTransformMatrixPoints(&xform, pt, 3);
2176 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2177 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2178 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2179 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2180 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2181 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2183 get_log_fontW(font, graphics, &lfw);
2184 lfw.lfHeight = -gdip_round(font_height * rel_height);
2185 unscaled_font = CreateFontIndirectW(&lfw);
2187 SelectObject(hdc, unscaled_font);
2188 GetTextMetricsW(hdc, &textmet);
2190 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2191 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2193 *hfont = CreateFontIndirectW(&lfw);
2195 DeleteDC(hdc);
2196 DeleteObject(unscaled_font);
2199 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2201 TRACE("(%p, %p)\n", hdc, graphics);
2203 return GdipCreateFromHDC2(hdc, NULL, graphics);
2206 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2208 GpStatus retval;
2209 HBITMAP hbitmap;
2210 DIBSECTION dib;
2212 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2214 if(hDevice != NULL)
2215 FIXME("Don't know how to handle parameter hDevice\n");
2217 if(hdc == NULL)
2218 return OutOfMemory;
2220 if(graphics == NULL)
2221 return InvalidParameter;
2223 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2224 if(!*graphics) return OutOfMemory;
2226 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2228 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2229 heap_free(*graphics);
2230 return retval;
2233 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2234 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2235 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2237 (*graphics)->alpha_hdc = 1;
2240 (*graphics)->hdc = hdc;
2241 (*graphics)->hwnd = WindowFromDC(hdc);
2242 (*graphics)->owndc = FALSE;
2243 (*graphics)->smoothing = SmoothingModeDefault;
2244 (*graphics)->compqual = CompositingQualityDefault;
2245 (*graphics)->interpolation = InterpolationModeBilinear;
2246 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2247 (*graphics)->compmode = CompositingModeSourceOver;
2248 (*graphics)->unit = UnitDisplay;
2249 (*graphics)->scale = 1.0;
2250 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2251 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2252 (*graphics)->busy = FALSE;
2253 (*graphics)->textcontrast = 4;
2254 list_init(&(*graphics)->containers);
2255 (*graphics)->contid = 0;
2257 TRACE("<-- %p\n", *graphics);
2259 return Ok;
2262 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2264 GpStatus retval;
2266 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2267 if(!*graphics) return OutOfMemory;
2269 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2271 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2272 heap_free(*graphics);
2273 return retval;
2276 (*graphics)->hdc = NULL;
2277 (*graphics)->hwnd = NULL;
2278 (*graphics)->owndc = FALSE;
2279 (*graphics)->image = image;
2280 /* We have to store the image type here because the image may be freed
2281 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2282 (*graphics)->image_type = image->type;
2283 (*graphics)->smoothing = SmoothingModeDefault;
2284 (*graphics)->compqual = CompositingQualityDefault;
2285 (*graphics)->interpolation = InterpolationModeBilinear;
2286 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2287 (*graphics)->compmode = CompositingModeSourceOver;
2288 (*graphics)->unit = UnitDisplay;
2289 (*graphics)->scale = 1.0;
2290 (*graphics)->xres = image->xres;
2291 (*graphics)->yres = image->yres;
2292 (*graphics)->busy = FALSE;
2293 (*graphics)->textcontrast = 4;
2294 list_init(&(*graphics)->containers);
2295 (*graphics)->contid = 0;
2297 TRACE("<-- %p\n", *graphics);
2299 return Ok;
2302 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2304 GpStatus ret;
2305 HDC hdc;
2307 TRACE("(%p, %p)\n", hwnd, graphics);
2309 hdc = GetDC(hwnd);
2311 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2313 ReleaseDC(hwnd, hdc);
2314 return ret;
2317 (*graphics)->hwnd = hwnd;
2318 (*graphics)->owndc = TRUE;
2320 return Ok;
2323 /* FIXME: no icm handling */
2324 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2326 TRACE("(%p, %p)\n", hwnd, graphics);
2328 return GdipCreateFromHWND(hwnd, graphics);
2331 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2332 UINT access, IStream **stream)
2334 DWORD dwMode;
2335 HRESULT ret;
2337 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2339 if(!stream || !filename)
2340 return InvalidParameter;
2342 if(access & GENERIC_WRITE)
2343 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2344 else if(access & GENERIC_READ)
2345 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2346 else
2347 return InvalidParameter;
2349 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2351 return hresult_to_status(ret);
2354 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2356 GraphicsContainerItem *cont, *next;
2357 GpStatus stat;
2358 TRACE("(%p)\n", graphics);
2360 if(!graphics) return InvalidParameter;
2361 if(graphics->busy) return ObjectBusy;
2363 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2365 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2366 if (stat != Ok)
2367 return stat;
2370 if(graphics->owndc)
2371 ReleaseDC(graphics->hwnd, graphics->hdc);
2373 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2374 list_remove(&cont->entry);
2375 delete_container(cont);
2378 GdipDeleteRegion(graphics->clip);
2380 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2381 * do otherwise, but we can't have that in the test suite because it means
2382 * accessing freed memory. */
2383 graphics->busy = TRUE;
2385 heap_free(graphics);
2387 return Ok;
2390 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2391 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2393 GpStatus status;
2394 GpPath *path;
2396 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2397 width, height, startAngle, sweepAngle);
2399 if(!graphics || !pen || width <= 0 || height <= 0)
2400 return InvalidParameter;
2402 if(graphics->busy)
2403 return ObjectBusy;
2405 status = GdipCreatePath(FillModeAlternate, &path);
2406 if (status != Ok) return status;
2408 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2409 if (status == Ok)
2410 status = GdipDrawPath(graphics, pen, path);
2412 GdipDeletePath(path);
2413 return status;
2416 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2417 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2419 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2420 width, height, startAngle, sweepAngle);
2422 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2425 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2426 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2428 GpPointF pt[4];
2430 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2431 x2, y2, x3, y3, x4, y4);
2433 if(!graphics || !pen)
2434 return InvalidParameter;
2436 if(graphics->busy)
2437 return ObjectBusy;
2439 pt[0].X = x1;
2440 pt[0].Y = y1;
2441 pt[1].X = x2;
2442 pt[1].Y = y2;
2443 pt[2].X = x3;
2444 pt[2].Y = y3;
2445 pt[3].X = x4;
2446 pt[3].Y = y4;
2447 return GdipDrawBeziers(graphics, pen, pt, 4);
2450 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2451 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2453 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2454 x2, y2, x3, y3, x4, y4);
2456 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2459 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2460 GDIPCONST GpPointF *points, INT count)
2462 GpStatus status;
2463 GpPath *path;
2465 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2467 if(!graphics || !pen || !points || (count <= 0))
2468 return InvalidParameter;
2470 if(graphics->busy)
2471 return ObjectBusy;
2473 status = GdipCreatePath(FillModeAlternate, &path);
2474 if (status != Ok) return status;
2476 status = GdipAddPathBeziers(path, points, count);
2477 if (status == Ok)
2478 status = GdipDrawPath(graphics, pen, path);
2480 GdipDeletePath(path);
2481 return status;
2484 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2485 GDIPCONST GpPoint *points, INT count)
2487 GpPointF *pts;
2488 GpStatus ret;
2489 INT i;
2491 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2493 if(!graphics || !pen || !points || (count <= 0))
2494 return InvalidParameter;
2496 if(graphics->busy)
2497 return ObjectBusy;
2499 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2500 if(!pts)
2501 return OutOfMemory;
2503 for(i = 0; i < count; i++){
2504 pts[i].X = (REAL)points[i].X;
2505 pts[i].Y = (REAL)points[i].Y;
2508 ret = GdipDrawBeziers(graphics,pen,pts,count);
2510 heap_free(pts);
2512 return ret;
2515 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2516 GDIPCONST GpPointF *points, INT count)
2518 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2520 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2523 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2524 GDIPCONST GpPoint *points, INT count)
2526 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2528 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2531 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2532 GDIPCONST GpPointF *points, INT count, REAL tension)
2534 GpPath *path;
2535 GpStatus status;
2537 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2539 if(!graphics || !pen || !points || count <= 0)
2540 return InvalidParameter;
2542 if(graphics->busy)
2543 return ObjectBusy;
2545 status = GdipCreatePath(FillModeAlternate, &path);
2546 if (status != Ok) return status;
2548 status = GdipAddPathClosedCurve2(path, points, count, tension);
2549 if (status == Ok)
2550 status = GdipDrawPath(graphics, pen, path);
2552 GdipDeletePath(path);
2554 return status;
2557 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2558 GDIPCONST GpPoint *points, INT count, REAL tension)
2560 GpPointF *ptf;
2561 GpStatus stat;
2562 INT i;
2564 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2566 if(!points || count <= 0)
2567 return InvalidParameter;
2569 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2570 if(!ptf)
2571 return OutOfMemory;
2573 for(i = 0; i < count; i++){
2574 ptf[i].X = (REAL)points[i].X;
2575 ptf[i].Y = (REAL)points[i].Y;
2578 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2580 heap_free(ptf);
2582 return stat;
2585 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2586 GDIPCONST GpPointF *points, INT count)
2588 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2590 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2593 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2594 GDIPCONST GpPoint *points, INT count)
2596 GpPointF *pointsF;
2597 GpStatus ret;
2598 INT i;
2600 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2602 if(!points)
2603 return InvalidParameter;
2605 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2606 if(!pointsF)
2607 return OutOfMemory;
2609 for(i = 0; i < count; i++){
2610 pointsF[i].X = (REAL)points[i].X;
2611 pointsF[i].Y = (REAL)points[i].Y;
2614 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2615 heap_free(pointsF);
2617 return ret;
2620 /* Approximates cardinal spline with Bezier curves. */
2621 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2622 GDIPCONST GpPointF *points, INT count, REAL tension)
2624 GpPath *path;
2625 GpStatus status;
2627 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2629 if(!graphics || !pen)
2630 return InvalidParameter;
2632 if(graphics->busy)
2633 return ObjectBusy;
2635 if(count < 2)
2636 return InvalidParameter;
2638 status = GdipCreatePath(FillModeAlternate, &path);
2639 if (status != Ok) return status;
2641 status = GdipAddPathCurve2(path, points, count, tension);
2642 if (status == Ok)
2643 status = GdipDrawPath(graphics, pen, path);
2645 GdipDeletePath(path);
2646 return status;
2649 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2650 GDIPCONST GpPoint *points, INT count, REAL tension)
2652 GpPointF *pointsF;
2653 GpStatus ret;
2654 INT i;
2656 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2658 if(!points)
2659 return InvalidParameter;
2661 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2662 if(!pointsF)
2663 return OutOfMemory;
2665 for(i = 0; i < count; i++){
2666 pointsF[i].X = (REAL)points[i].X;
2667 pointsF[i].Y = (REAL)points[i].Y;
2670 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2671 heap_free(pointsF);
2673 return ret;
2676 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2677 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2678 REAL tension)
2680 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2682 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2683 return InvalidParameter;
2686 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2689 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2690 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2691 REAL tension)
2693 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2695 if(count < 0){
2696 return OutOfMemory;
2699 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2700 return InvalidParameter;
2703 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2706 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2707 REAL y, REAL width, REAL height)
2709 GpPath *path;
2710 GpStatus status;
2712 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2714 if(!graphics || !pen)
2715 return InvalidParameter;
2717 if(graphics->busy)
2718 return ObjectBusy;
2720 status = GdipCreatePath(FillModeAlternate, &path);
2721 if (status != Ok) return status;
2723 status = GdipAddPathEllipse(path, x, y, width, height);
2724 if (status == Ok)
2725 status = GdipDrawPath(graphics, pen, path);
2727 GdipDeletePath(path);
2728 return status;
2731 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2732 INT y, INT width, INT height)
2734 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2736 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2740 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2742 UINT width, height;
2744 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2746 if(!graphics || !image)
2747 return InvalidParameter;
2749 GdipGetImageWidth(image, &width);
2750 GdipGetImageHeight(image, &height);
2752 return GdipDrawImagePointRect(graphics, image, x, y,
2753 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2756 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2757 INT y)
2759 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2761 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2764 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2765 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2766 GpUnit srcUnit)
2768 GpPointF points[3];
2769 REAL scale_x, scale_y, width, height;
2771 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2773 if (!graphics || !image) return InvalidParameter;
2775 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2776 scale_x *= graphics->xres / image->xres;
2777 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2778 scale_y *= graphics->yres / image->yres;
2779 width = srcwidth * scale_x;
2780 height = srcheight * scale_y;
2782 points[0].X = points[2].X = x;
2783 points[0].Y = points[1].Y = y;
2784 points[1].X = x + width;
2785 points[2].Y = y + height;
2787 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2788 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2791 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2792 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2793 GpUnit srcUnit)
2795 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2798 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2799 GDIPCONST GpPointF *dstpoints, INT count)
2801 UINT width, height;
2803 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2805 if(!image)
2806 return InvalidParameter;
2808 GdipGetImageWidth(image, &width);
2809 GdipGetImageHeight(image, &height);
2811 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2812 width, height, UnitPixel, NULL, NULL, NULL);
2815 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2816 GDIPCONST GpPoint *dstpoints, INT count)
2818 GpPointF ptf[3];
2820 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2822 if (count != 3 || !dstpoints)
2823 return InvalidParameter;
2825 ptf[0].X = (REAL)dstpoints[0].X;
2826 ptf[0].Y = (REAL)dstpoints[0].Y;
2827 ptf[1].X = (REAL)dstpoints[1].X;
2828 ptf[1].Y = (REAL)dstpoints[1].Y;
2829 ptf[2].X = (REAL)dstpoints[2].X;
2830 ptf[2].Y = (REAL)dstpoints[2].Y;
2832 return GdipDrawImagePoints(graphics, image, ptf, count);
2835 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2836 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2838 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2839 return TRUE;
2842 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2843 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2844 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2845 DrawImageAbort callback, VOID * callbackData)
2847 GpPointF ptf[4];
2848 POINT pti[4];
2849 GpStatus stat;
2851 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2852 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2853 callbackData);
2855 if (count > 3)
2856 return NotImplemented;
2858 if(!graphics || !image || !points || count != 3)
2859 return InvalidParameter;
2861 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2862 debugstr_pointf(&points[2]));
2864 memcpy(ptf, points, 3 * sizeof(GpPointF));
2866 /* Ensure source width/height is positive */
2867 if (srcwidth < 0)
2869 GpPointF tmp = ptf[1];
2870 srcx = srcx + srcwidth;
2871 srcwidth = -srcwidth;
2872 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2873 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2874 ptf[1] = ptf[0];
2875 ptf[0] = tmp;
2878 if (srcheight < 0)
2880 GpPointF tmp = ptf[2];
2881 srcy = srcy + srcheight;
2882 srcheight = -srcheight;
2883 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2884 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2885 ptf[2] = ptf[0];
2886 ptf[0] = tmp;
2889 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2890 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2891 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2892 return Ok;
2893 transform_and_round_points(graphics, pti, ptf, 4);
2895 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2896 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2898 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2899 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2900 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2901 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2902 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2904 if (image->type == ImageTypeBitmap)
2906 GpBitmap* bitmap = (GpBitmap*)image;
2907 BOOL do_resampling = FALSE;
2908 BOOL use_software = FALSE;
2910 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2911 graphics->xres, graphics->yres,
2912 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2913 graphics->scale, image->xres, image->yres, bitmap->format,
2914 imageAttributes ? imageAttributes->outside_color : 0);
2916 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2917 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2918 srcx < 0 || srcy < 0 ||
2919 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2920 do_resampling = TRUE;
2922 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
2923 (graphics->image && graphics->image->type == ImageTypeBitmap))
2924 use_software = TRUE;
2926 if (use_software)
2928 RECT dst_area;
2929 GpRectF graphics_bounds;
2930 GpRect src_area;
2931 int i, x, y, src_stride, dst_stride;
2932 GpMatrix dst_to_src;
2933 REAL m11, m12, m21, m22, mdx, mdy;
2934 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
2935 BitmapData lockeddata;
2936 InterpolationMode interpolation = graphics->interpolation;
2937 PixelOffsetMode offset_mode = graphics->pixeloffset;
2938 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2939 REAL x_dx, x_dy, y_dx, y_dy;
2940 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2942 if (!imageAttributes)
2943 imageAttributes = &defaultImageAttributes;
2945 dst_area.left = dst_area.right = pti[0].x;
2946 dst_area.top = dst_area.bottom = pti[0].y;
2947 for (i=1; i<4; i++)
2949 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2950 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2951 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2952 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2955 stat = get_graphics_bounds(graphics, &graphics_bounds);
2956 if (stat != Ok) return stat;
2958 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
2959 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
2960 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
2961 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
2963 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2965 if (IsRectEmpty(&dst_area)) return Ok;
2967 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2968 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2969 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2970 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2971 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2972 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2974 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2976 stat = GdipInvertMatrix(&dst_to_src);
2977 if (stat != Ok) return stat;
2979 if (do_resampling)
2981 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2982 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2984 else
2986 /* Make sure src_area is equal in size to dst_area. */
2987 src_area.X = srcx + dst_area.left - pti[0].x;
2988 src_area.Y = srcy + dst_area.top - pti[0].y;
2989 src_area.Width = dst_area.right - dst_area.left;
2990 src_area.Height = dst_area.bottom - dst_area.top;
2993 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
2995 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
2996 if (!src_data)
2997 return OutOfMemory;
2998 src_stride = sizeof(ARGB) * src_area.Width;
3000 /* Read the bits we need from the source bitmap into a compatible buffer. */
3001 lockeddata.Width = src_area.Width;
3002 lockeddata.Height = src_area.Height;
3003 lockeddata.Stride = src_stride;
3004 lockeddata.Scan0 = src_data;
3005 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3006 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3007 else
3008 lockeddata.PixelFormat = PixelFormat32bppARGB;
3010 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3011 lockeddata.PixelFormat, &lockeddata);
3013 if (stat == Ok)
3014 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3016 if (stat != Ok)
3018 heap_free(src_data);
3019 return stat;
3022 apply_image_attributes(imageAttributes, src_data,
3023 src_area.Width, src_area.Height,
3024 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3026 if (do_resampling)
3028 /* Transform the bits as needed to the destination. */
3029 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3030 if (!dst_data)
3032 heap_free(src_data);
3033 return OutOfMemory;
3036 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3038 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3040 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3041 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3042 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3043 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3045 for (x=dst_area.left; x<dst_area.right; x++)
3047 for (y=dst_area.top; y<dst_area.bottom; y++)
3049 GpPointF src_pointf;
3050 ARGB *dst_color;
3052 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3053 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3055 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3057 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3058 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3059 imageAttributes, interpolation, offset_mode);
3060 else
3061 *dst_color = 0;
3065 else
3067 dst_data = src_data;
3068 dst_stride = src_stride;
3071 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3072 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3073 lockeddata.PixelFormat);
3075 heap_free(src_data);
3077 heap_free(dst_dyn_data);
3079 return stat;
3081 else
3083 HDC hdc;
3084 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3085 HBITMAP hbitmap, old_hbm=NULL;
3086 HRGN hrgn;
3087 INT save_state;
3089 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3090 bitmap->format == PixelFormat24bppRGB ||
3091 bitmap->format == PixelFormat32bppRGB ||
3092 bitmap->format == PixelFormat32bppPARGB))
3094 BITMAPINFOHEADER bih;
3095 BYTE *temp_bits;
3096 PixelFormat dst_format;
3098 /* we can't draw a bitmap of this format directly */
3099 hdc = CreateCompatibleDC(0);
3100 temp_hdc = TRUE;
3101 temp_bitmap = TRUE;
3103 bih.biSize = sizeof(BITMAPINFOHEADER);
3104 bih.biWidth = bitmap->width;
3105 bih.biHeight = -bitmap->height;
3106 bih.biPlanes = 1;
3107 bih.biBitCount = 32;
3108 bih.biCompression = BI_RGB;
3109 bih.biSizeImage = 0;
3110 bih.biXPelsPerMeter = 0;
3111 bih.biYPelsPerMeter = 0;
3112 bih.biClrUsed = 0;
3113 bih.biClrImportant = 0;
3115 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3116 (void**)&temp_bits, NULL, 0);
3118 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3119 dst_format = PixelFormat32bppPARGB;
3120 else
3121 dst_format = PixelFormat32bppRGB;
3123 convert_pixels(bitmap->width, bitmap->height,
3124 bitmap->width*4, temp_bits, dst_format,
3125 bitmap->stride, bitmap->bits, bitmap->format,
3126 bitmap->image.palette);
3128 else
3130 if (bitmap->hbitmap)
3131 hbitmap = bitmap->hbitmap;
3132 else
3134 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3135 temp_bitmap = TRUE;
3138 hdc = bitmap->hdc;
3139 temp_hdc = (hdc == 0);
3142 if (temp_hdc)
3144 if (!hdc) hdc = CreateCompatibleDC(0);
3145 old_hbm = SelectObject(hdc, hbitmap);
3148 save_state = SaveDC(graphics->hdc);
3150 stat = get_clip_hrgn(graphics, &hrgn);
3152 if (stat == Ok && hrgn)
3154 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3155 DeleteObject(hrgn);
3158 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3160 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3161 hdc, srcx, srcy, srcwidth, srcheight);
3163 else
3165 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3166 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3169 RestoreDC(graphics->hdc, save_state);
3171 if (temp_hdc)
3173 SelectObject(hdc, old_hbm);
3174 DeleteDC(hdc);
3177 if (temp_bitmap)
3178 DeleteObject(hbitmap);
3181 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3183 GpRectF rc;
3185 rc.X = srcx;
3186 rc.Y = srcy;
3187 rc.Width = srcwidth;
3188 rc.Height = srcheight;
3190 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3191 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3193 else
3195 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3196 return InvalidParameter;
3199 return Ok;
3202 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3203 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3204 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3205 DrawImageAbort callback, VOID * callbackData)
3207 GpPointF pointsF[3];
3208 INT i;
3210 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3211 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3212 callbackData);
3214 if(!points || count!=3)
3215 return InvalidParameter;
3217 for(i = 0; i < count; i++){
3218 pointsF[i].X = (REAL)points[i].X;
3219 pointsF[i].Y = (REAL)points[i].Y;
3222 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3223 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3224 callback, callbackData);
3227 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3228 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3229 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3230 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3231 VOID * callbackData)
3233 GpPointF points[3];
3235 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3236 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3237 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3239 points[0].X = dstx;
3240 points[0].Y = dsty;
3241 points[1].X = dstx + dstwidth;
3242 points[1].Y = dsty;
3243 points[2].X = dstx;
3244 points[2].Y = dsty + dstheight;
3246 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3247 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3250 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3251 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3252 INT srcwidth, INT srcheight, GpUnit srcUnit,
3253 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3254 VOID * callbackData)
3256 GpPointF points[3];
3258 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3259 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3260 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3262 points[0].X = dstx;
3263 points[0].Y = dsty;
3264 points[1].X = dstx + dstwidth;
3265 points[1].Y = dsty;
3266 points[2].X = dstx;
3267 points[2].Y = dsty + dstheight;
3269 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3270 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3273 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3274 REAL x, REAL y, REAL width, REAL height)
3276 RectF bounds;
3277 GpUnit unit;
3278 GpStatus ret;
3280 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3282 if(!graphics || !image)
3283 return InvalidParameter;
3285 ret = GdipGetImageBounds(image, &bounds, &unit);
3286 if(ret != Ok)
3287 return ret;
3289 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3290 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3291 unit, NULL, NULL, NULL);
3294 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3295 INT x, INT y, INT width, INT height)
3297 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3299 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3302 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3303 REAL y1, REAL x2, REAL y2)
3305 GpPointF pt[2];
3307 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3309 if (!pen)
3310 return InvalidParameter;
3312 if (pen->unit == UnitPixel && pen->width <= 0.0)
3313 return Ok;
3315 pt[0].X = x1;
3316 pt[0].Y = y1;
3317 pt[1].X = x2;
3318 pt[1].Y = y2;
3319 return GdipDrawLines(graphics, pen, pt, 2);
3322 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3323 INT y1, INT x2, INT y2)
3325 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3327 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3330 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3331 GpPointF *points, INT count)
3333 GpStatus status;
3334 GpPath *path;
3336 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3338 if(!pen || !graphics || (count < 2))
3339 return InvalidParameter;
3341 if(graphics->busy)
3342 return ObjectBusy;
3344 status = GdipCreatePath(FillModeAlternate, &path);
3345 if (status != Ok) return status;
3347 status = GdipAddPathLine2(path, points, count);
3348 if (status == Ok)
3349 status = GdipDrawPath(graphics, pen, path);
3351 GdipDeletePath(path);
3352 return status;
3355 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3356 GpPoint *points, INT count)
3358 GpStatus retval;
3359 GpPointF *ptf;
3360 int i;
3362 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3364 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3365 if(!ptf) return OutOfMemory;
3367 for(i = 0; i < count; i ++){
3368 ptf[i].X = (REAL) points[i].X;
3369 ptf[i].Y = (REAL) points[i].Y;
3372 retval = GdipDrawLines(graphics, pen, ptf, count);
3374 heap_free(ptf);
3375 return retval;
3378 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3380 INT save_state;
3381 GpStatus retval;
3382 HRGN hrgn=NULL;
3384 save_state = prepare_dc(graphics, pen);
3386 retval = get_clip_hrgn(graphics, &hrgn);
3388 if (retval != Ok)
3389 goto end;
3391 if (hrgn)
3392 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3394 retval = draw_poly(graphics, pen, path->pathdata.Points,
3395 path->pathdata.Types, path->pathdata.Count, TRUE);
3397 end:
3398 restore_dc(graphics, save_state);
3399 DeleteObject(hrgn);
3401 return retval;
3404 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3406 GpStatus stat;
3407 GpPath* flat_path;
3408 GpMatrix* transform;
3409 GpRectF gp_bound_rect;
3410 GpRect gp_output_area;
3411 RECT output_area;
3412 INT output_height, output_width;
3413 DWORD *output_bits, *brush_bits=NULL;
3414 int i;
3415 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3416 const BYTE *dash_pattern;
3417 INT dash_pattern_size;
3418 BYTE *dyn_dash_pattern = NULL;
3420 stat = GdipClonePath(path, &flat_path);
3422 if (stat != Ok)
3423 return stat;
3425 stat = GdipCreateMatrix(&transform);
3427 if (stat == Ok)
3429 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3430 CoordinateSpaceWorld, transform);
3432 if (stat == Ok)
3433 stat = GdipFlattenPath(flat_path, transform, 1.0);
3435 GdipDeleteMatrix(transform);
3438 /* estimate the output size in pixels, can be larger than necessary */
3439 if (stat == Ok)
3441 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3442 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3443 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3444 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3446 for (i=1; i<flat_path->pathdata.Count; i++)
3448 REAL x, y;
3449 x = flat_path->pathdata.Points[i].X;
3450 y = flat_path->pathdata.Points[i].Y;
3452 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3453 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3454 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3455 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3458 stat = get_graphics_bounds(graphics, &gp_bound_rect);
3461 if (stat == Ok)
3463 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3464 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3465 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3466 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3468 output_width = output_area.right - output_area.left + 1;
3469 output_height = output_area.bottom - output_area.top + 1;
3471 if (output_width <= 0 || output_height <= 0)
3473 GdipDeletePath(flat_path);
3474 return Ok;
3477 gp_output_area.X = output_area.left;
3478 gp_output_area.Y = output_area.top;
3479 gp_output_area.Width = output_width;
3480 gp_output_area.Height = output_height;
3482 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3483 if (!output_bits)
3484 stat = OutOfMemory;
3487 if (stat == Ok)
3489 if (pen->brush->bt != BrushTypeSolidColor)
3491 /* allocate and draw brush output */
3492 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3494 if (brush_bits)
3496 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3497 &gp_output_area, output_width);
3499 else
3500 stat = OutOfMemory;
3503 if (stat == Ok)
3505 /* convert dash pattern to bool array */
3506 switch (pen->dash)
3508 case DashStyleCustom:
3510 dash_pattern_size = 0;
3512 for (i=0; i < pen->numdashes; i++)
3513 dash_pattern_size += gdip_round(pen->dashes[i]);
3515 if (dash_pattern_size != 0)
3517 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3519 if (dyn_dash_pattern)
3521 int j=0;
3522 for (i=0; i < pen->numdashes; i++)
3524 int k;
3525 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3526 dyn_dash_pattern[j++] = (i&1)^1;
3529 else
3530 stat = OutOfMemory;
3532 break;
3534 /* else fall through */
3536 case DashStyleSolid:
3537 default:
3538 dash_pattern = static_dash_pattern;
3539 dash_pattern_size = 1;
3540 break;
3541 case DashStyleDash:
3542 dash_pattern = static_dash_pattern;
3543 dash_pattern_size = 4;
3544 break;
3545 case DashStyleDot:
3546 dash_pattern = &static_dash_pattern[4];
3547 dash_pattern_size = 2;
3548 break;
3549 case DashStyleDashDot:
3550 dash_pattern = static_dash_pattern;
3551 dash_pattern_size = 6;
3552 break;
3553 case DashStyleDashDotDot:
3554 dash_pattern = static_dash_pattern;
3555 dash_pattern_size = 8;
3556 break;
3560 if (stat == Ok)
3562 /* trace path */
3563 GpPointF subpath_start = flat_path->pathdata.Points[0];
3564 INT prev_x = INT_MAX, prev_y = INT_MAX;
3565 int dash_pos = dash_pattern_size - 1;
3567 for (i=0; i < flat_path->pathdata.Count; i++)
3569 BYTE type, type2;
3570 GpPointF start_point, end_point;
3571 GpPoint start_pointi, end_pointi;
3573 type = flat_path->pathdata.Types[i];
3574 if (i+1 < flat_path->pathdata.Count)
3575 type2 = flat_path->pathdata.Types[i+1];
3576 else
3577 type2 = PathPointTypeStart;
3579 start_point = flat_path->pathdata.Points[i];
3581 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3582 subpath_start = start_point;
3584 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3585 end_point = subpath_start;
3586 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3587 continue;
3588 else
3589 end_point = flat_path->pathdata.Points[i+1];
3591 start_pointi.X = floorf(start_point.X);
3592 start_pointi.Y = floorf(start_point.Y);
3593 end_pointi.X = floorf(end_point.X);
3594 end_pointi.Y = floorf(end_point.Y);
3596 /* draw line segment */
3597 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3599 INT x, y, start_y, end_y, step;
3601 if (start_pointi.Y < end_pointi.Y)
3603 step = 1;
3604 start_y = ceilf(start_point.Y) - output_area.top;
3605 end_y = end_pointi.Y - output_area.top;
3607 else
3609 step = -1;
3610 start_y = start_point.Y - output_area.top;
3611 end_y = ceilf(end_point.Y) - output_area.top;
3614 for (y=start_y; y != (end_y+step); y+=step)
3616 x = gdip_round( start_point.X +
3617 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3618 - output_area.left;
3620 if (x == prev_x && y == prev_y)
3621 continue;
3623 prev_x = x;
3624 prev_y = y;
3625 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3627 if (!dash_pattern[dash_pos])
3628 continue;
3630 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3631 continue;
3633 if (brush_bits)
3634 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3635 else
3636 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3639 else
3641 INT x, y, start_x, end_x, step;
3643 if (start_pointi.X < end_pointi.X)
3645 step = 1;
3646 start_x = ceilf(start_point.X) - output_area.left;
3647 end_x = end_pointi.X - output_area.left;
3649 else
3651 step = -1;
3652 start_x = start_point.X - output_area.left;
3653 end_x = ceilf(end_point.X) - output_area.left;
3656 for (x=start_x; x != (end_x+step); x+=step)
3658 y = gdip_round( start_point.Y +
3659 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3660 - output_area.top;
3662 if (x == prev_x && y == prev_y)
3663 continue;
3665 prev_x = x;
3666 prev_y = y;
3667 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3669 if (!dash_pattern[dash_pos])
3670 continue;
3672 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3673 continue;
3675 if (brush_bits)
3676 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3677 else
3678 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3684 /* draw output image */
3685 if (stat == Ok)
3687 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3688 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3689 PixelFormat32bppARGB);
3692 heap_free(brush_bits);
3693 heap_free(dyn_dash_pattern);
3694 heap_free(output_bits);
3697 GdipDeletePath(flat_path);
3699 return stat;
3702 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3704 GpStatus stat;
3705 GpPath *wide_path;
3706 GpMatrix *transform=NULL;
3708 /* Check if the final pen thickness in pixels is too thin. */
3709 if (pen->unit == UnitPixel)
3711 if (pen->width < 1.415)
3712 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3714 else
3716 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3718 points[1].X = pen->width;
3719 points[2].Y = pen->width;
3721 stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3722 CoordinateSpaceWorld, points, 3);
3724 if (stat != Ok)
3725 return stat;
3727 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3728 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3729 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3730 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3731 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3734 stat = GdipClonePath(path, &wide_path);
3736 if (stat != Ok)
3737 return stat;
3739 if (pen->unit == UnitPixel)
3741 /* We have to transform this to device coordinates to get the widths right. */
3742 stat = GdipCreateMatrix(&transform);
3744 if (stat == Ok)
3745 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3746 CoordinateSpaceWorld, transform);
3749 if (stat == Ok)
3750 stat = GdipWidenPath(wide_path, pen, transform, 1.0);
3752 if (pen->unit == UnitPixel)
3754 /* Transform the path back to world coordinates */
3755 if (stat == Ok)
3756 stat = GdipInvertMatrix(transform);
3758 if (stat == Ok)
3759 stat = GdipTransformPath(wide_path, transform);
3762 /* Actually draw the path */
3763 if (stat == Ok)
3764 stat = GdipFillPath(graphics, pen->brush, wide_path);
3766 GdipDeleteMatrix(transform);
3768 GdipDeletePath(wide_path);
3770 return stat;
3773 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3775 GpStatus retval;
3777 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3779 if(!pen || !graphics)
3780 return InvalidParameter;
3782 if(graphics->busy)
3783 return ObjectBusy;
3785 if (path->pathdata.Count == 0)
3786 return Ok;
3788 if (!graphics->hdc)
3789 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
3790 else
3791 retval = GDI32_GdipDrawPath(graphics, pen, path);
3793 return retval;
3796 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3797 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3799 GpStatus status;
3800 GpPath *path;
3802 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3803 width, height, startAngle, sweepAngle);
3805 if(!graphics || !pen)
3806 return InvalidParameter;
3808 if(graphics->busy)
3809 return ObjectBusy;
3811 status = GdipCreatePath(FillModeAlternate, &path);
3812 if (status != Ok) return status;
3814 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3815 if (status == Ok)
3816 status = GdipDrawPath(graphics, pen, path);
3818 GdipDeletePath(path);
3819 return status;
3822 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3823 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3825 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3826 width, height, startAngle, sweepAngle);
3828 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3831 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3832 REAL y, REAL width, REAL height)
3834 GpStatus status;
3835 GpPath *path;
3837 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3839 if(!pen || !graphics)
3840 return InvalidParameter;
3842 if(graphics->busy)
3843 return ObjectBusy;
3845 status = GdipCreatePath(FillModeAlternate, &path);
3846 if (status != Ok) return status;
3848 status = GdipAddPathRectangle(path, x, y, width, height);
3849 if (status == Ok)
3850 status = GdipDrawPath(graphics, pen, path);
3852 GdipDeletePath(path);
3853 return status;
3856 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3857 INT y, INT width, INT height)
3859 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3861 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3864 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3865 GDIPCONST GpRectF* rects, INT count)
3867 GpStatus status;
3868 GpPath *path;
3870 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3872 if(!graphics || !pen || !rects || count < 1)
3873 return InvalidParameter;
3875 if(graphics->busy)
3876 return ObjectBusy;
3878 status = GdipCreatePath(FillModeAlternate, &path);
3879 if (status != Ok) return status;
3881 status = GdipAddPathRectangles(path, rects, count);
3882 if (status == Ok)
3883 status = GdipDrawPath(graphics, pen, path);
3885 GdipDeletePath(path);
3886 return status;
3889 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3890 GDIPCONST GpRect* rects, INT count)
3892 GpRectF *rectsF;
3893 GpStatus ret;
3894 INT i;
3896 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3898 if(!rects || count<=0)
3899 return InvalidParameter;
3901 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
3902 if(!rectsF)
3903 return OutOfMemory;
3905 for(i = 0;i < count;i++){
3906 rectsF[i].X = (REAL)rects[i].X;
3907 rectsF[i].Y = (REAL)rects[i].Y;
3908 rectsF[i].Width = (REAL)rects[i].Width;
3909 rectsF[i].Height = (REAL)rects[i].Height;
3912 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3913 heap_free(rectsF);
3915 return ret;
3918 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3919 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3921 GpPath *path;
3922 GpStatus status;
3924 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3925 count, tension, fill);
3927 if(!graphics || !brush || !points)
3928 return InvalidParameter;
3930 if(graphics->busy)
3931 return ObjectBusy;
3933 if(count == 1) /* Do nothing */
3934 return Ok;
3936 status = GdipCreatePath(fill, &path);
3937 if (status != Ok) return status;
3939 status = GdipAddPathClosedCurve2(path, points, count, tension);
3940 if (status == Ok)
3941 status = GdipFillPath(graphics, brush, path);
3943 GdipDeletePath(path);
3944 return status;
3947 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3948 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3950 GpPointF *ptf;
3951 GpStatus stat;
3952 INT i;
3954 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3955 count, tension, fill);
3957 if(!points || count == 0)
3958 return InvalidParameter;
3960 if(count == 1) /* Do nothing */
3961 return Ok;
3963 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
3964 if(!ptf)
3965 return OutOfMemory;
3967 for(i = 0;i < count;i++){
3968 ptf[i].X = (REAL)points[i].X;
3969 ptf[i].Y = (REAL)points[i].Y;
3972 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3974 heap_free(ptf);
3976 return stat;
3979 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3980 GDIPCONST GpPointF *points, INT count)
3982 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3983 return GdipFillClosedCurve2(graphics, brush, points, count,
3984 0.5f, FillModeAlternate);
3987 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3988 GDIPCONST GpPoint *points, INT count)
3990 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3991 return GdipFillClosedCurve2I(graphics, brush, points, count,
3992 0.5f, FillModeAlternate);
3995 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3996 REAL y, REAL width, REAL height)
3998 GpStatus stat;
3999 GpPath *path;
4001 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4003 if(!graphics || !brush)
4004 return InvalidParameter;
4006 if(graphics->busy)
4007 return ObjectBusy;
4009 stat = GdipCreatePath(FillModeAlternate, &path);
4011 if (stat == Ok)
4013 stat = GdipAddPathEllipse(path, x, y, width, height);
4015 if (stat == Ok)
4016 stat = GdipFillPath(graphics, brush, path);
4018 GdipDeletePath(path);
4021 return stat;
4024 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4025 INT y, INT width, INT height)
4027 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4029 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4032 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4034 INT save_state;
4035 GpStatus retval;
4036 HRGN hrgn=NULL;
4038 if(!graphics->hdc || !brush_can_fill_path(brush))
4039 return NotImplemented;
4041 save_state = SaveDC(graphics->hdc);
4042 EndPath(graphics->hdc);
4043 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4044 : WINDING));
4046 retval = get_clip_hrgn(graphics, &hrgn);
4048 if (retval != Ok)
4049 goto end;
4051 if (hrgn)
4052 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4054 BeginPath(graphics->hdc);
4055 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4056 path->pathdata.Types, path->pathdata.Count, FALSE);
4058 if(retval != Ok)
4059 goto end;
4061 EndPath(graphics->hdc);
4062 brush_fill_path(graphics, brush);
4064 retval = Ok;
4066 end:
4067 RestoreDC(graphics->hdc, save_state);
4068 DeleteObject(hrgn);
4070 return retval;
4073 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4075 GpStatus stat;
4076 GpRegion *rgn;
4078 if (!brush_can_fill_pixels(brush))
4079 return NotImplemented;
4081 /* FIXME: This could probably be done more efficiently without regions. */
4083 stat = GdipCreateRegionPath(path, &rgn);
4085 if (stat == Ok)
4087 stat = GdipFillRegion(graphics, brush, rgn);
4089 GdipDeleteRegion(rgn);
4092 return stat;
4095 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4097 GpStatus stat = NotImplemented;
4099 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4101 if(!brush || !graphics || !path)
4102 return InvalidParameter;
4104 if(graphics->busy)
4105 return ObjectBusy;
4107 if (!graphics->image && !graphics->alpha_hdc)
4108 stat = GDI32_GdipFillPath(graphics, brush, path);
4110 if (stat == NotImplemented)
4111 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4113 if (stat == NotImplemented)
4115 FIXME("Not implemented for brushtype %i\n", brush->bt);
4116 stat = Ok;
4119 return stat;
4122 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4123 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4125 GpStatus stat;
4126 GpPath *path;
4128 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4129 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4131 if(!graphics || !brush)
4132 return InvalidParameter;
4134 if(graphics->busy)
4135 return ObjectBusy;
4137 stat = GdipCreatePath(FillModeAlternate, &path);
4139 if (stat == Ok)
4141 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4143 if (stat == Ok)
4144 stat = GdipFillPath(graphics, brush, path);
4146 GdipDeletePath(path);
4149 return stat;
4152 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4153 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4155 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4156 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4158 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4161 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4162 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4164 GpStatus stat;
4165 GpPath *path;
4167 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4169 if(!graphics || !brush || !points || !count)
4170 return InvalidParameter;
4172 if(graphics->busy)
4173 return ObjectBusy;
4175 stat = GdipCreatePath(fillMode, &path);
4177 if (stat == Ok)
4179 stat = GdipAddPathPolygon(path, points, count);
4181 if (stat == Ok)
4182 stat = GdipFillPath(graphics, brush, path);
4184 GdipDeletePath(path);
4187 return stat;
4190 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4191 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4193 GpStatus stat;
4194 GpPath *path;
4196 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4198 if(!graphics || !brush || !points || !count)
4199 return InvalidParameter;
4201 if(graphics->busy)
4202 return ObjectBusy;
4204 stat = GdipCreatePath(fillMode, &path);
4206 if (stat == Ok)
4208 stat = GdipAddPathPolygonI(path, points, count);
4210 if (stat == Ok)
4211 stat = GdipFillPath(graphics, brush, path);
4213 GdipDeletePath(path);
4216 return stat;
4219 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4220 GDIPCONST GpPointF *points, INT count)
4222 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4224 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4227 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4228 GDIPCONST GpPoint *points, INT count)
4230 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4232 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4235 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4236 REAL x, REAL y, REAL width, REAL height)
4238 GpRectF rect;
4240 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4242 rect.X = x;
4243 rect.Y = y;
4244 rect.Width = width;
4245 rect.Height = height;
4247 return GdipFillRectangles(graphics, brush, &rect, 1);
4250 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4251 INT x, INT y, INT width, INT height)
4253 GpRectF rect;
4255 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4257 rect.X = (REAL)x;
4258 rect.Y = (REAL)y;
4259 rect.Width = (REAL)width;
4260 rect.Height = (REAL)height;
4262 return GdipFillRectangles(graphics, brush, &rect, 1);
4265 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4266 INT count)
4268 GpStatus status;
4269 GpPath *path;
4271 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4273 if(!graphics || !brush || !rects || count <= 0)
4274 return InvalidParameter;
4276 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4278 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4279 /* FIXME: Add gdi32 drawing. */
4280 return status;
4283 status = GdipCreatePath(FillModeAlternate, &path);
4284 if (status != Ok) return status;
4286 status = GdipAddPathRectangles(path, rects, count);
4287 if (status == Ok)
4288 status = GdipFillPath(graphics, brush, path);
4290 GdipDeletePath(path);
4291 return status;
4294 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4295 INT count)
4297 GpRectF *rectsF;
4298 GpStatus ret;
4299 INT i;
4301 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4303 if(!rects || count <= 0)
4304 return InvalidParameter;
4306 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4307 if(!rectsF)
4308 return OutOfMemory;
4310 for(i = 0; i < count; i++){
4311 rectsF[i].X = (REAL)rects[i].X;
4312 rectsF[i].Y = (REAL)rects[i].Y;
4313 rectsF[i].X = (REAL)rects[i].Width;
4314 rectsF[i].Height = (REAL)rects[i].Height;
4317 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4318 heap_free(rectsF);
4320 return ret;
4323 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4324 GpRegion* region)
4326 INT save_state;
4327 GpStatus status;
4328 HRGN hrgn;
4329 RECT rc;
4331 if(!graphics->hdc || !brush_can_fill_path(brush))
4332 return NotImplemented;
4334 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4335 if(status != Ok)
4336 return status;
4338 save_state = SaveDC(graphics->hdc);
4339 EndPath(graphics->hdc);
4341 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4343 DeleteObject(hrgn);
4345 hrgn = NULL;
4346 status = get_clip_hrgn(graphics, &hrgn);
4348 if (status != Ok)
4350 RestoreDC(graphics->hdc, save_state);
4351 return status;
4354 if (hrgn)
4356 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4357 DeleteObject(hrgn);
4360 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4362 BeginPath(graphics->hdc);
4363 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4364 EndPath(graphics->hdc);
4366 brush_fill_path(graphics, brush);
4369 RestoreDC(graphics->hdc, save_state);
4372 return Ok;
4375 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4376 GpRegion* region)
4378 GpStatus stat;
4379 GpRegion *temp_region;
4380 GpMatrix world_to_device;
4381 GpRectF graphics_bounds;
4382 DWORD *pixel_data;
4383 HRGN hregion;
4384 RECT bound_rect;
4385 GpRect gp_bound_rect;
4387 if (!brush_can_fill_pixels(brush))
4388 return NotImplemented;
4390 stat = get_graphics_bounds(graphics, &graphics_bounds);
4392 if (stat == Ok)
4393 stat = GdipCloneRegion(region, &temp_region);
4395 if (stat == Ok)
4397 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4398 CoordinateSpaceWorld, &world_to_device);
4400 if (stat == Ok)
4401 stat = GdipTransformRegion(temp_region, &world_to_device);
4403 if (stat == Ok)
4404 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4406 if (stat == Ok)
4407 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4409 GdipDeleteRegion(temp_region);
4412 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4414 DeleteObject(hregion);
4415 return Ok;
4418 if (stat == Ok)
4420 gp_bound_rect.X = bound_rect.left;
4421 gp_bound_rect.Y = bound_rect.top;
4422 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4423 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4425 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4426 if (!pixel_data)
4427 stat = OutOfMemory;
4429 if (stat == Ok)
4431 stat = brush_fill_pixels(graphics, brush, pixel_data,
4432 &gp_bound_rect, gp_bound_rect.Width);
4434 if (stat == Ok)
4435 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4436 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4437 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4438 PixelFormat32bppARGB);
4440 heap_free(pixel_data);
4443 DeleteObject(hregion);
4446 return stat;
4449 /*****************************************************************************
4450 * GdipFillRegion [GDIPLUS.@]
4452 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4453 GpRegion* region)
4455 GpStatus stat = NotImplemented;
4457 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4459 if (!(graphics && brush && region))
4460 return InvalidParameter;
4462 if(graphics->busy)
4463 return ObjectBusy;
4465 if (!graphics->image && !graphics->alpha_hdc)
4466 stat = GDI32_GdipFillRegion(graphics, brush, region);
4468 if (stat == NotImplemented)
4469 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4471 if (stat == NotImplemented)
4473 FIXME("not implemented for brushtype %i\n", brush->bt);
4474 stat = Ok;
4477 return stat;
4480 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4482 TRACE("(%p,%u)\n", graphics, intention);
4484 if(!graphics)
4485 return InvalidParameter;
4487 if(graphics->busy)
4488 return ObjectBusy;
4490 /* We have no internal operation queue, so there's no need to clear it. */
4492 if (graphics->hdc)
4493 GdiFlush();
4495 return Ok;
4498 /*****************************************************************************
4499 * GdipGetClipBounds [GDIPLUS.@]
4501 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4503 GpStatus status;
4504 GpRegion *clip;
4506 TRACE("(%p, %p)\n", graphics, rect);
4508 if(!graphics)
4509 return InvalidParameter;
4511 if(graphics->busy)
4512 return ObjectBusy;
4514 status = GdipCreateRegion(&clip);
4515 if (status != Ok) return status;
4517 status = GdipGetClip(graphics, clip);
4518 if (status == Ok)
4519 status = GdipGetRegionBounds(clip, graphics, rect);
4521 GdipDeleteRegion(clip);
4522 return status;
4525 /*****************************************************************************
4526 * GdipGetClipBoundsI [GDIPLUS.@]
4528 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4530 TRACE("(%p, %p)\n", graphics, rect);
4532 if(!graphics)
4533 return InvalidParameter;
4535 if(graphics->busy)
4536 return ObjectBusy;
4538 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4541 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4542 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4543 CompositingMode *mode)
4545 TRACE("(%p, %p)\n", graphics, mode);
4547 if(!graphics || !mode)
4548 return InvalidParameter;
4550 if(graphics->busy)
4551 return ObjectBusy;
4553 *mode = graphics->compmode;
4555 return Ok;
4558 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4559 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4560 CompositingQuality *quality)
4562 TRACE("(%p, %p)\n", graphics, quality);
4564 if(!graphics || !quality)
4565 return InvalidParameter;
4567 if(graphics->busy)
4568 return ObjectBusy;
4570 *quality = graphics->compqual;
4572 return Ok;
4575 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4576 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4577 InterpolationMode *mode)
4579 TRACE("(%p, %p)\n", graphics, mode);
4581 if(!graphics || !mode)
4582 return InvalidParameter;
4584 if(graphics->busy)
4585 return ObjectBusy;
4587 *mode = graphics->interpolation;
4589 return Ok;
4592 /* FIXME: Need to handle color depths less than 24bpp */
4593 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4595 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4597 if(!graphics || !argb)
4598 return InvalidParameter;
4600 if(graphics->busy)
4601 return ObjectBusy;
4603 return Ok;
4606 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4608 TRACE("(%p, %p)\n", graphics, scale);
4610 if(!graphics || !scale)
4611 return InvalidParameter;
4613 if(graphics->busy)
4614 return ObjectBusy;
4616 *scale = graphics->scale;
4618 return Ok;
4621 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4623 TRACE("(%p, %p)\n", graphics, unit);
4625 if(!graphics || !unit)
4626 return InvalidParameter;
4628 if(graphics->busy)
4629 return ObjectBusy;
4631 *unit = graphics->unit;
4633 return Ok;
4636 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4637 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4638 *mode)
4640 TRACE("(%p, %p)\n", graphics, mode);
4642 if(!graphics || !mode)
4643 return InvalidParameter;
4645 if(graphics->busy)
4646 return ObjectBusy;
4648 *mode = graphics->pixeloffset;
4650 return Ok;
4653 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4654 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4656 TRACE("(%p, %p)\n", graphics, mode);
4658 if(!graphics || !mode)
4659 return InvalidParameter;
4661 if(graphics->busy)
4662 return ObjectBusy;
4664 *mode = graphics->smoothing;
4666 return Ok;
4669 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4671 TRACE("(%p, %p)\n", graphics, contrast);
4673 if(!graphics || !contrast)
4674 return InvalidParameter;
4676 *contrast = graphics->textcontrast;
4678 return Ok;
4681 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4682 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4683 TextRenderingHint *hint)
4685 TRACE("(%p, %p)\n", graphics, hint);
4687 if(!graphics || !hint)
4688 return InvalidParameter;
4690 if(graphics->busy)
4691 return ObjectBusy;
4693 *hint = graphics->texthint;
4695 return Ok;
4698 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4700 GpRegion *clip_rgn;
4701 GpStatus stat;
4702 GpMatrix device_to_world;
4704 TRACE("(%p, %p)\n", graphics, rect);
4706 if(!graphics || !rect)
4707 return InvalidParameter;
4709 if(graphics->busy)
4710 return ObjectBusy;
4712 /* intersect window and graphics clipping regions */
4713 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4714 return stat;
4716 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4717 goto cleanup;
4719 /* transform to world coordinates */
4720 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4721 goto cleanup;
4723 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4724 goto cleanup;
4726 /* get bounds of the region */
4727 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4729 cleanup:
4730 GdipDeleteRegion(clip_rgn);
4732 return stat;
4735 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4737 GpRectF rectf;
4738 GpStatus stat;
4740 TRACE("(%p, %p)\n", graphics, rect);
4742 if(!graphics || !rect)
4743 return InvalidParameter;
4745 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4747 rect->X = gdip_round(rectf.X);
4748 rect->Y = gdip_round(rectf.Y);
4749 rect->Width = gdip_round(rectf.Width);
4750 rect->Height = gdip_round(rectf.Height);
4753 return stat;
4756 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4758 TRACE("(%p, %p)\n", graphics, matrix);
4760 if(!graphics || !matrix)
4761 return InvalidParameter;
4763 if(graphics->busy)
4764 return ObjectBusy;
4766 *matrix = graphics->worldtrans;
4767 return Ok;
4770 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4772 GpSolidFill *brush;
4773 GpStatus stat;
4774 GpRectF wnd_rect;
4776 TRACE("(%p, %x)\n", graphics, color);
4778 if(!graphics)
4779 return InvalidParameter;
4781 if(graphics->busy)
4782 return ObjectBusy;
4784 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4785 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
4787 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4788 return stat;
4790 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
4791 GdipDeleteBrush((GpBrush*)brush);
4792 return stat;
4795 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4796 wnd_rect.Width, wnd_rect.Height);
4798 GdipDeleteBrush((GpBrush*)brush);
4800 return Ok;
4803 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4805 TRACE("(%p, %p)\n", graphics, res);
4807 if(!graphics || !res)
4808 return InvalidParameter;
4810 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4813 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4815 GpStatus stat;
4816 GpRegion* rgn;
4817 GpPointF pt;
4819 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4821 if(!graphics || !result)
4822 return InvalidParameter;
4824 if(graphics->busy)
4825 return ObjectBusy;
4827 pt.X = x;
4828 pt.Y = y;
4829 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4830 CoordinateSpaceWorld, &pt, 1)) != Ok)
4831 return stat;
4833 if((stat = GdipCreateRegion(&rgn)) != Ok)
4834 return stat;
4836 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4837 goto cleanup;
4839 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4841 cleanup:
4842 GdipDeleteRegion(rgn);
4843 return stat;
4846 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4848 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4851 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4853 GpStatus stat;
4854 GpRegion* rgn;
4855 GpPointF pts[2];
4857 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4859 if(!graphics || !result)
4860 return InvalidParameter;
4862 if(graphics->busy)
4863 return ObjectBusy;
4865 pts[0].X = x;
4866 pts[0].Y = y;
4867 pts[1].X = x + width;
4868 pts[1].Y = y + height;
4870 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4871 CoordinateSpaceWorld, pts, 2)) != Ok)
4872 return stat;
4874 pts[1].X -= pts[0].X;
4875 pts[1].Y -= pts[0].Y;
4877 if((stat = GdipCreateRegion(&rgn)) != Ok)
4878 return stat;
4880 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4881 goto cleanup;
4883 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4885 cleanup:
4886 GdipDeleteRegion(rgn);
4887 return stat;
4890 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4892 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4895 GpStatus gdip_format_string(HDC hdc,
4896 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4897 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4898 gdip_format_string_callback callback, void *user_data)
4900 WCHAR* stringdup;
4901 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4902 nheight, lineend, lineno = 0;
4903 RectF bounds;
4904 StringAlignment halign;
4905 GpStatus stat = Ok;
4906 SIZE size;
4907 HotkeyPrefix hkprefix;
4908 INT *hotkeyprefix_offsets=NULL;
4909 INT hotkeyprefix_count=0;
4910 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4911 BOOL seen_prefix = FALSE;
4912 GpStringFormat *dyn_format=NULL;
4914 if(length == -1) length = lstrlenW(string);
4916 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
4917 if(!stringdup) return OutOfMemory;
4919 if (!format)
4921 stat = GdipStringFormatGetGenericDefault(&dyn_format);
4922 if (stat != Ok)
4924 heap_free(stringdup);
4925 return stat;
4927 format = dyn_format;
4930 nwidth = rect->Width;
4931 nheight = rect->Height;
4932 if (ignore_empty_clip)
4934 if (!nwidth) nwidth = INT_MAX;
4935 if (!nheight) nheight = INT_MAX;
4938 hkprefix = format->hkprefix;
4940 if (hkprefix == HotkeyPrefixShow)
4942 for (i=0; i<length; i++)
4944 if (string[i] == '&')
4945 hotkeyprefix_count++;
4949 if (hotkeyprefix_count)
4950 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
4952 hotkeyprefix_count = 0;
4954 for(i = 0, j = 0; i < length; i++){
4955 /* FIXME: This makes the indexes passed to callback inaccurate. */
4956 if(!isprintW(string[i]) && (string[i] != '\n'))
4957 continue;
4959 /* FIXME: tabs should be handled using tabstops from stringformat */
4960 if (string[i] == '\t')
4961 continue;
4963 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4964 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4965 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4967 seen_prefix = TRUE;
4968 continue;
4971 seen_prefix = FALSE;
4973 stringdup[j] = string[i];
4974 j++;
4977 length = j;
4979 halign = format->align;
4981 while(sum < length){
4982 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4983 nwidth, &fit, NULL, &size);
4984 fitcpy = fit;
4986 if(fit == 0)
4987 break;
4989 for(lret = 0; lret < fit; lret++)
4990 if(*(stringdup + sum + lret) == '\n')
4991 break;
4993 /* Line break code (may look strange, but it imitates windows). */
4994 if(lret < fit)
4995 lineend = fit = lret; /* this is not an off-by-one error */
4996 else if(fit < (length - sum)){
4997 if(*(stringdup + sum + fit) == ' ')
4998 while(*(stringdup + sum + fit) == ' ')
4999 fit++;
5000 else
5001 while(*(stringdup + sum + fit - 1) != ' '){
5002 fit--;
5004 if(*(stringdup + sum + fit) == '\t')
5005 break;
5007 if(fit == 0){
5008 fit = fitcpy;
5009 break;
5012 lineend = fit;
5013 while(*(stringdup + sum + lineend - 1) == ' ' ||
5014 *(stringdup + sum + lineend - 1) == '\t')
5015 lineend--;
5017 else
5018 lineend = fit;
5020 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5021 nwidth, &j, NULL, &size);
5023 bounds.Width = size.cx;
5025 if(height + size.cy > nheight)
5027 if (format->attr & StringFormatFlagsLineLimit)
5028 break;
5029 bounds.Height = nheight - (height + size.cy);
5031 else
5032 bounds.Height = size.cy;
5034 bounds.Y = rect->Y + height;
5036 switch (halign)
5038 case StringAlignmentNear:
5039 default:
5040 bounds.X = rect->X;
5041 break;
5042 case StringAlignmentCenter:
5043 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5044 break;
5045 case StringAlignmentFar:
5046 bounds.X = rect->X + rect->Width - bounds.Width;
5047 break;
5050 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5051 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5052 break;
5054 stat = callback(hdc, stringdup, sum, lineend,
5055 font, rect, format, lineno, &bounds,
5056 &hotkeyprefix_offsets[hotkeyprefix_pos],
5057 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5059 if (stat != Ok)
5060 break;
5062 sum += fit + (lret < fitcpy ? 1 : 0);
5063 height += size.cy;
5064 lineno++;
5066 hotkeyprefix_pos = hotkeyprefix_end_pos;
5068 if(height > nheight)
5069 break;
5071 /* Stop if this was a linewrap (but not if it was a linebreak). */
5072 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5073 break;
5076 heap_free(stringdup);
5077 heap_free(hotkeyprefix_offsets);
5078 GdipDeleteStringFormat(dyn_format);
5080 return stat;
5083 struct measure_ranges_args {
5084 GpRegion **regions;
5085 REAL rel_width, rel_height;
5088 static GpStatus measure_ranges_callback(HDC hdc,
5089 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5090 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5091 INT lineno, const RectF *bounds, INT *underlined_indexes,
5092 INT underlined_index_count, void *user_data)
5094 int i;
5095 GpStatus stat = Ok;
5096 struct measure_ranges_args *args = user_data;
5098 for (i=0; i<format->range_count; i++)
5100 INT range_start = max(index, format->character_ranges[i].First);
5101 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5102 if (range_start < range_end)
5104 GpRectF range_rect;
5105 SIZE range_size;
5107 range_rect.Y = bounds->Y / args->rel_height;
5108 range_rect.Height = bounds->Height / args->rel_height;
5110 GetTextExtentExPointW(hdc, string + index, range_start - index,
5111 INT_MAX, NULL, NULL, &range_size);
5112 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5114 GetTextExtentExPointW(hdc, string + index, range_end - index,
5115 INT_MAX, NULL, NULL, &range_size);
5116 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5118 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5119 if (stat != Ok)
5120 break;
5124 return stat;
5127 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5128 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5129 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5130 INT regionCount, GpRegion** regions)
5132 GpStatus stat;
5133 int i;
5134 HFONT gdifont, oldfont;
5135 struct measure_ranges_args args;
5136 HDC hdc, temp_hdc=NULL;
5137 GpPointF pt[3];
5138 RectF scaled_rect;
5139 REAL margin_x;
5141 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
5142 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5144 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5145 return InvalidParameter;
5147 if (regionCount < stringFormat->range_count)
5148 return InvalidParameter;
5150 if(!graphics->hdc)
5152 hdc = temp_hdc = CreateCompatibleDC(0);
5153 if (!temp_hdc) return OutOfMemory;
5155 else
5156 hdc = graphics->hdc;
5158 if (stringFormat->attr)
5159 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5161 pt[0].X = 0.0;
5162 pt[0].Y = 0.0;
5163 pt[1].X = 1.0;
5164 pt[1].Y = 0.0;
5165 pt[2].X = 0.0;
5166 pt[2].Y = 1.0;
5167 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5168 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5169 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5170 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5171 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5173 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5174 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5176 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5177 scaled_rect.Y = layoutRect->Y * args.rel_height;
5178 scaled_rect.Width = layoutRect->Width * args.rel_width;
5179 scaled_rect.Height = layoutRect->Height * args.rel_height;
5181 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5182 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5184 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5185 oldfont = SelectObject(hdc, gdifont);
5187 for (i=0; i<stringFormat->range_count; i++)
5189 stat = GdipSetEmpty(regions[i]);
5190 if (stat != Ok)
5191 return stat;
5194 args.regions = regions;
5196 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5197 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5199 SelectObject(hdc, oldfont);
5200 DeleteObject(gdifont);
5202 if (temp_hdc)
5203 DeleteDC(temp_hdc);
5205 return stat;
5208 struct measure_string_args {
5209 RectF *bounds;
5210 INT *codepointsfitted;
5211 INT *linesfilled;
5212 REAL rel_width, rel_height;
5215 static GpStatus measure_string_callback(HDC hdc,
5216 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5217 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5218 INT lineno, const RectF *bounds, INT *underlined_indexes,
5219 INT underlined_index_count, void *user_data)
5221 struct measure_string_args *args = user_data;
5222 REAL new_width, new_height;
5224 new_width = bounds->Width / args->rel_width;
5225 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5227 if (new_width > args->bounds->Width)
5228 args->bounds->Width = new_width;
5230 if (new_height > args->bounds->Height)
5231 args->bounds->Height = new_height;
5233 if (args->codepointsfitted)
5234 *args->codepointsfitted = index + length;
5236 if (args->linesfilled)
5237 (*args->linesfilled)++;
5239 return Ok;
5242 /* Find the smallest rectangle that bounds the text when it is printed in rect
5243 * according to the format options listed in format. If rect has 0 width and
5244 * height, then just find the smallest rectangle that bounds the text when it's
5245 * printed at location (rect->X, rect-Y). */
5246 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5247 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5248 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5249 INT *codepointsfitted, INT *linesfilled)
5251 HFONT oldfont, gdifont;
5252 struct measure_string_args args;
5253 HDC temp_hdc=NULL, hdc;
5254 GpPointF pt[3];
5255 RectF scaled_rect;
5256 REAL margin_x;
5257 INT lines, glyphs;
5259 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5260 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5261 bounds, codepointsfitted, linesfilled);
5263 if(!graphics || !string || !font || !rect || !bounds)
5264 return InvalidParameter;
5266 if(!graphics->hdc)
5268 hdc = temp_hdc = CreateCompatibleDC(0);
5269 if (!temp_hdc) return OutOfMemory;
5271 else
5272 hdc = graphics->hdc;
5274 if(linesfilled) *linesfilled = 0;
5275 if(codepointsfitted) *codepointsfitted = 0;
5277 if(format)
5278 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5280 pt[0].X = 0.0;
5281 pt[0].Y = 0.0;
5282 pt[1].X = 1.0;
5283 pt[1].Y = 0.0;
5284 pt[2].X = 0.0;
5285 pt[2].Y = 1.0;
5286 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5287 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5288 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5289 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5290 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5292 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5293 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5295 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5296 scaled_rect.Y = rect->Y * args.rel_height;
5297 scaled_rect.Width = rect->Width * args.rel_width;
5298 scaled_rect.Height = rect->Height * args.rel_height;
5299 if (scaled_rect.Width >= 0.5)
5301 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5302 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5305 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5306 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5308 get_font_hfont(graphics, font, format, &gdifont, NULL);
5309 oldfont = SelectObject(hdc, gdifont);
5311 bounds->X = rect->X;
5312 bounds->Y = rect->Y;
5313 bounds->Width = 0.0;
5314 bounds->Height = 0.0;
5316 args.bounds = bounds;
5317 args.codepointsfitted = &glyphs;
5318 args.linesfilled = &lines;
5319 lines = glyphs = 0;
5321 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5322 measure_string_callback, &args);
5324 if (linesfilled) *linesfilled = lines;
5325 if (codepointsfitted) *codepointsfitted = glyphs;
5327 if (lines)
5328 bounds->Width += margin_x * 2.0;
5330 SelectObject(hdc, oldfont);
5331 DeleteObject(gdifont);
5333 if (temp_hdc)
5334 DeleteDC(temp_hdc);
5336 return Ok;
5339 struct draw_string_args {
5340 GpGraphics *graphics;
5341 GDIPCONST GpBrush *brush;
5342 REAL x, y, rel_width, rel_height, ascent;
5345 static GpStatus draw_string_callback(HDC hdc,
5346 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5347 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5348 INT lineno, const RectF *bounds, INT *underlined_indexes,
5349 INT underlined_index_count, void *user_data)
5351 struct draw_string_args *args = user_data;
5352 PointF position;
5353 GpStatus stat;
5355 position.X = args->x + bounds->X / args->rel_width;
5356 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5358 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5359 args->brush, &position,
5360 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5362 if (stat == Ok && underlined_index_count)
5364 OUTLINETEXTMETRICW otm;
5365 REAL underline_y, underline_height;
5366 int i;
5368 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5370 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5371 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5373 for (i=0; i<underlined_index_count; i++)
5375 REAL start_x, end_x;
5376 SIZE text_size;
5377 INT ofs = underlined_indexes[i] - index;
5379 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5380 start_x = text_size.cx / args->rel_width;
5382 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5383 end_x = text_size.cx / args->rel_width;
5385 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5389 return stat;
5392 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5393 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5394 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5396 HRGN rgn = NULL;
5397 HFONT gdifont;
5398 GpPointF pt[3], rectcpy[4];
5399 POINT corners[4];
5400 REAL rel_width, rel_height, margin_x;
5401 INT save_state, format_flags = 0;
5402 REAL offsety = 0.0;
5403 struct draw_string_args args;
5404 RectF scaled_rect;
5405 HDC hdc, temp_hdc=NULL;
5406 TEXTMETRICW textmetric;
5408 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5409 length, font, debugstr_rectf(rect), format, brush);
5411 if(!graphics || !string || !font || !brush || !rect)
5412 return InvalidParameter;
5414 if(graphics->hdc)
5416 hdc = graphics->hdc;
5418 else
5420 hdc = temp_hdc = CreateCompatibleDC(0);
5423 if(format){
5424 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5426 format_flags = format->attr;
5428 /* Should be no need to explicitly test for StringAlignmentNear as
5429 * that is default behavior if no alignment is passed. */
5430 if(format->vertalign != StringAlignmentNear){
5431 RectF bounds, in_rect = *rect;
5432 in_rect.Height = 0.0; /* avoid height clipping */
5433 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5435 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5437 if(format->vertalign == StringAlignmentCenter)
5438 offsety = (rect->Height - bounds.Height) / 2;
5439 else if(format->vertalign == StringAlignmentFar)
5440 offsety = (rect->Height - bounds.Height);
5442 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5445 save_state = SaveDC(hdc);
5447 pt[0].X = 0.0;
5448 pt[0].Y = 0.0;
5449 pt[1].X = 1.0;
5450 pt[1].Y = 0.0;
5451 pt[2].X = 0.0;
5452 pt[2].Y = 1.0;
5453 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5454 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5455 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5456 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5457 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5459 rectcpy[3].X = rectcpy[0].X = rect->X;
5460 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5461 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5462 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5463 transform_and_round_points(graphics, corners, rectcpy, 4);
5465 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5466 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5468 scaled_rect.X = margin_x * rel_width;
5469 scaled_rect.Y = 0.0;
5470 scaled_rect.Width = rel_width * rect->Width;
5471 scaled_rect.Height = rel_height * rect->Height;
5472 if (scaled_rect.Width >= 0.5)
5474 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5475 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5478 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5479 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5481 if (!(format_flags & StringFormatFlagsNoClip) &&
5482 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5483 rect->Width > 0.0 && rect->Height > 0.0)
5485 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5486 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5487 SelectClipRgn(hdc, rgn);
5490 get_font_hfont(graphics, font, format, &gdifont, NULL);
5491 SelectObject(hdc, gdifont);
5493 args.graphics = graphics;
5494 args.brush = brush;
5496 args.x = rect->X;
5497 args.y = rect->Y + offsety;
5499 args.rel_width = rel_width;
5500 args.rel_height = rel_height;
5502 GetTextMetricsW(hdc, &textmetric);
5503 args.ascent = textmetric.tmAscent / rel_height;
5505 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5506 draw_string_callback, &args);
5508 DeleteObject(rgn);
5509 DeleteObject(gdifont);
5511 RestoreDC(hdc, save_state);
5513 DeleteDC(temp_hdc);
5515 return Ok;
5518 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5520 TRACE("(%p)\n", graphics);
5522 if(!graphics)
5523 return InvalidParameter;
5525 if(graphics->busy)
5526 return ObjectBusy;
5528 return GdipSetInfinite(graphics->clip);
5531 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5533 GpStatus stat;
5535 TRACE("(%p)\n", graphics);
5537 if(!graphics)
5538 return InvalidParameter;
5540 if(graphics->busy)
5541 return ObjectBusy;
5543 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5544 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5546 if (stat != Ok)
5547 return stat;
5550 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5553 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5554 GpMatrixOrder order)
5556 GpStatus stat;
5558 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5560 if(!graphics)
5561 return InvalidParameter;
5563 if(graphics->busy)
5564 return ObjectBusy;
5566 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5567 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5569 if (stat != Ok)
5570 return stat;
5573 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5576 static GpStatus begin_container(GpGraphics *graphics,
5577 GraphicsContainerType type, GraphicsContainer *state)
5579 GraphicsContainerItem *container;
5580 GpStatus sts;
5582 if(!graphics || !state)
5583 return InvalidParameter;
5585 sts = init_container(&container, graphics, type);
5586 if(sts != Ok)
5587 return sts;
5589 list_add_head(&graphics->containers, &container->entry);
5590 *state = graphics->contid = container->contid;
5592 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5593 if (type == BEGIN_CONTAINER)
5594 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5595 else
5596 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5599 return Ok;
5602 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5604 TRACE("(%p, %p)\n", graphics, state);
5605 return begin_container(graphics, SAVE_GRAPHICS, state);
5608 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5609 GraphicsContainer *state)
5611 TRACE("(%p, %p)\n", graphics, state);
5612 return begin_container(graphics, BEGIN_CONTAINER, state);
5615 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5617 GraphicsContainerItem *container;
5618 GpMatrix transform;
5619 GpStatus stat;
5620 GpRectF scaled_srcrect;
5621 REAL scale_x, scale_y;
5623 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5625 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5626 return InvalidParameter;
5628 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5629 if(stat != Ok)
5630 return stat;
5632 list_add_head(&graphics->containers, &container->entry);
5633 *state = graphics->contid = container->contid;
5635 scale_x = units_to_pixels(1.0, unit, graphics->xres);
5636 scale_y = units_to_pixels(1.0, unit, graphics->yres);
5638 scaled_srcrect.X = scale_x * srcrect->X;
5639 scaled_srcrect.Y = scale_y * srcrect->Y;
5640 scaled_srcrect.Width = scale_x * srcrect->Width;
5641 scaled_srcrect.Height = scale_y * srcrect->Height;
5643 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5644 transform.matrix[1] = 0.0;
5645 transform.matrix[2] = 0.0;
5646 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5647 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5648 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5650 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5652 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5653 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5656 return Ok;
5659 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5661 GpRectF dstrectf, srcrectf;
5663 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5665 if (!dstrect || !srcrect)
5666 return InvalidParameter;
5668 dstrectf.X = dstrect->X;
5669 dstrectf.Y = dstrect->Y;
5670 dstrectf.Width = dstrect->Width;
5671 dstrectf.Height = dstrect->Height;
5673 srcrectf.X = srcrect->X;
5674 srcrectf.Y = srcrect->Y;
5675 srcrectf.Width = srcrect->Width;
5676 srcrectf.Height = srcrect->Height;
5678 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5681 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5683 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5684 return NotImplemented;
5687 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5688 GraphicsContainer state)
5690 GpStatus sts;
5691 GraphicsContainerItem *container, *container2;
5693 if(!graphics)
5694 return InvalidParameter;
5696 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5697 if(container->contid == state && container->type == type)
5698 break;
5701 /* did not find a matching container */
5702 if(&container->entry == &graphics->containers)
5703 return Ok;
5705 sts = restore_container(graphics, container);
5706 if(sts != Ok)
5707 return sts;
5709 /* remove all of the containers on top of the found container */
5710 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5711 if(container->contid == state)
5712 break;
5713 list_remove(&container->entry);
5714 delete_container(container);
5717 list_remove(&container->entry);
5718 delete_container(container);
5720 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5721 if (type == BEGIN_CONTAINER)
5722 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
5723 else
5724 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
5727 return Ok;
5730 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5732 TRACE("(%p, %x)\n", graphics, state);
5733 return end_container(graphics, BEGIN_CONTAINER, state);
5736 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5738 TRACE("(%p, %x)\n", graphics, state);
5739 return end_container(graphics, SAVE_GRAPHICS, state);
5742 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5743 REAL sy, GpMatrixOrder order)
5745 GpStatus stat;
5747 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5749 if(!graphics)
5750 return InvalidParameter;
5752 if(graphics->busy)
5753 return ObjectBusy;
5755 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5756 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
5758 if (stat != Ok)
5759 return stat;
5762 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5765 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5766 CombineMode mode)
5768 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5770 if(!graphics || !srcgraphics)
5771 return InvalidParameter;
5773 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5776 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5777 CompositingMode mode)
5779 TRACE("(%p, %d)\n", graphics, mode);
5781 if(!graphics)
5782 return InvalidParameter;
5784 if(graphics->busy)
5785 return ObjectBusy;
5787 graphics->compmode = mode;
5789 return Ok;
5792 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5793 CompositingQuality quality)
5795 TRACE("(%p, %d)\n", graphics, quality);
5797 if(!graphics)
5798 return InvalidParameter;
5800 if(graphics->busy)
5801 return ObjectBusy;
5803 graphics->compqual = quality;
5805 return Ok;
5808 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5809 InterpolationMode mode)
5811 TRACE("(%p, %d)\n", graphics, mode);
5813 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5814 return InvalidParameter;
5816 if(graphics->busy)
5817 return ObjectBusy;
5819 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5820 mode = InterpolationModeBilinear;
5822 if (mode == InterpolationModeHighQuality)
5823 mode = InterpolationModeHighQualityBicubic;
5825 graphics->interpolation = mode;
5827 return Ok;
5830 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5832 GpStatus stat;
5834 TRACE("(%p, %.2f)\n", graphics, scale);
5836 if(!graphics || (scale <= 0.0))
5837 return InvalidParameter;
5839 if(graphics->busy)
5840 return ObjectBusy;
5842 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5844 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5845 if (stat != Ok)
5846 return stat;
5849 graphics->scale = scale;
5851 return Ok;
5854 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5856 GpStatus stat;
5858 TRACE("(%p, %d)\n", graphics, unit);
5860 if(!graphics)
5861 return InvalidParameter;
5863 if(graphics->busy)
5864 return ObjectBusy;
5866 if(unit == UnitWorld)
5867 return InvalidParameter;
5869 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5871 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5872 if (stat != Ok)
5873 return stat;
5876 graphics->unit = unit;
5878 return Ok;
5881 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5882 mode)
5884 TRACE("(%p, %d)\n", graphics, mode);
5886 if(!graphics)
5887 return InvalidParameter;
5889 if(graphics->busy)
5890 return ObjectBusy;
5892 graphics->pixeloffset = mode;
5894 return Ok;
5897 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5899 static int calls;
5901 TRACE("(%p,%i,%i)\n", graphics, x, y);
5903 if (!(calls++))
5904 FIXME("value is unused in rendering\n");
5906 if (!graphics)
5907 return InvalidParameter;
5909 graphics->origin_x = x;
5910 graphics->origin_y = y;
5912 return Ok;
5915 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5917 TRACE("(%p,%p,%p)\n", graphics, x, y);
5919 if (!graphics || !x || !y)
5920 return InvalidParameter;
5922 *x = graphics->origin_x;
5923 *y = graphics->origin_y;
5925 return Ok;
5928 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5930 TRACE("(%p, %d)\n", graphics, mode);
5932 if(!graphics)
5933 return InvalidParameter;
5935 if(graphics->busy)
5936 return ObjectBusy;
5938 graphics->smoothing = mode;
5940 return Ok;
5943 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5945 TRACE("(%p, %d)\n", graphics, contrast);
5947 if(!graphics)
5948 return InvalidParameter;
5950 graphics->textcontrast = contrast;
5952 return Ok;
5955 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5956 TextRenderingHint hint)
5958 TRACE("(%p, %d)\n", graphics, hint);
5960 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5961 return InvalidParameter;
5963 if(graphics->busy)
5964 return ObjectBusy;
5966 graphics->texthint = hint;
5968 return Ok;
5971 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5973 GpStatus stat;
5975 TRACE("(%p, %p)\n", graphics, matrix);
5977 if(!graphics || !matrix)
5978 return InvalidParameter;
5980 if(graphics->busy)
5981 return ObjectBusy;
5983 TRACE("%f,%f,%f,%f,%f,%f\n",
5984 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5985 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5987 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5988 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
5990 if (stat != Ok)
5991 return stat;
5994 graphics->worldtrans = *matrix;
5996 return Ok;
5999 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6000 REAL dy, GpMatrixOrder order)
6002 GpStatus stat;
6004 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6006 if(!graphics)
6007 return InvalidParameter;
6009 if(graphics->busy)
6010 return ObjectBusy;
6012 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6013 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6015 if (stat != Ok)
6016 return stat;
6019 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6022 /*****************************************************************************
6023 * GdipSetClipHrgn [GDIPLUS.@]
6025 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6027 GpRegion *region;
6028 GpStatus status;
6030 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6032 if(!graphics)
6033 return InvalidParameter;
6035 if(graphics->busy)
6036 return ObjectBusy;
6038 /* hrgn is already in device units */
6039 status = GdipCreateRegionHrgn(hrgn, &region);
6040 if(status != Ok)
6041 return status;
6043 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6045 GdipDeleteRegion(region);
6046 return status;
6049 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6051 GpStatus status;
6052 GpPath *clip_path;
6054 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6056 if(!graphics)
6057 return InvalidParameter;
6059 if(graphics->busy)
6060 return ObjectBusy;
6062 status = GdipClonePath(path, &clip_path);
6063 if (status == Ok)
6065 GpMatrix world_to_device;
6067 get_graphics_transform(graphics, CoordinateSpaceDevice,
6068 CoordinateSpaceWorld, &world_to_device);
6069 status = GdipTransformPath(clip_path, &world_to_device);
6070 if (status == Ok)
6071 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6073 GdipDeletePath(clip_path);
6075 return status;
6078 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6079 REAL width, REAL height,
6080 CombineMode mode)
6082 GpStatus status;
6083 GpRectF rect;
6084 GpRegion *region;
6086 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6088 if(!graphics)
6089 return InvalidParameter;
6091 if(graphics->busy)
6092 return ObjectBusy;
6094 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6096 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6097 if (status != Ok)
6098 return status;
6101 rect.X = x;
6102 rect.Y = y;
6103 rect.Width = width;
6104 rect.Height = height;
6105 status = GdipCreateRegionRect(&rect, &region);
6106 if (status == Ok)
6108 GpMatrix world_to_device;
6110 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6111 status = GdipTransformRegion(region, &world_to_device);
6112 if (status == Ok)
6113 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6115 GdipDeleteRegion(region);
6117 return status;
6120 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6121 INT width, INT height,
6122 CombineMode mode)
6124 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6126 if(!graphics)
6127 return InvalidParameter;
6129 if(graphics->busy)
6130 return ObjectBusy;
6132 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6135 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6136 CombineMode mode)
6138 GpStatus status;
6139 GpRegion *clip;
6141 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6143 if(!graphics || !region)
6144 return InvalidParameter;
6146 if(graphics->busy)
6147 return ObjectBusy;
6149 status = GdipCloneRegion(region, &clip);
6150 if (status == Ok)
6152 GpMatrix world_to_device;
6154 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6155 status = GdipTransformRegion(clip, &world_to_device);
6156 if (status == Ok)
6157 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6159 GdipDeleteRegion(clip);
6161 return status;
6164 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6165 INT count)
6167 GpStatus status;
6168 GpPath* path;
6170 TRACE("(%p, %p, %d)\n", graphics, points, count);
6172 if(!graphics || !pen || count<=0)
6173 return InvalidParameter;
6175 if(graphics->busy)
6176 return ObjectBusy;
6178 status = GdipCreatePath(FillModeAlternate, &path);
6179 if (status != Ok) return status;
6181 status = GdipAddPathPolygon(path, points, count);
6182 if (status == Ok)
6183 status = GdipDrawPath(graphics, pen, path);
6185 GdipDeletePath(path);
6187 return status;
6190 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6191 INT count)
6193 GpStatus ret;
6194 GpPointF *ptf;
6195 INT i;
6197 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6199 if(count<=0) return InvalidParameter;
6200 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6202 for(i = 0;i < count; i++){
6203 ptf[i].X = (REAL)points[i].X;
6204 ptf[i].Y = (REAL)points[i].Y;
6207 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6208 heap_free(ptf);
6210 return ret;
6213 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6215 TRACE("(%p, %p)\n", graphics, dpi);
6217 if(!graphics || !dpi)
6218 return InvalidParameter;
6220 if(graphics->busy)
6221 return ObjectBusy;
6223 *dpi = graphics->xres;
6224 return Ok;
6227 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6229 TRACE("(%p, %p)\n", graphics, dpi);
6231 if(!graphics || !dpi)
6232 return InvalidParameter;
6234 if(graphics->busy)
6235 return ObjectBusy;
6237 *dpi = graphics->yres;
6238 return Ok;
6241 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6242 GpMatrixOrder order)
6244 GpMatrix m;
6245 GpStatus ret;
6247 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6249 if(!graphics || !matrix)
6250 return InvalidParameter;
6252 if(graphics->busy)
6253 return ObjectBusy;
6255 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6256 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6258 if (ret != Ok)
6259 return ret;
6262 m = graphics->worldtrans;
6264 ret = GdipMultiplyMatrix(&m, matrix, order);
6265 if(ret == Ok)
6266 graphics->worldtrans = m;
6268 return ret;
6271 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6272 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6274 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6276 GpStatus stat=Ok;
6278 TRACE("(%p, %p)\n", graphics, hdc);
6280 if(!graphics || !hdc)
6281 return InvalidParameter;
6283 if(graphics->busy)
6284 return ObjectBusy;
6286 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6288 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6290 else if (!graphics->hdc ||
6291 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6293 /* Create a fake HDC and fill it with a constant color. */
6294 HDC temp_hdc;
6295 HBITMAP hbitmap;
6296 GpRectF bounds;
6297 BITMAPINFOHEADER bmih;
6298 int i;
6300 stat = get_graphics_bounds(graphics, &bounds);
6301 if (stat != Ok)
6302 return stat;
6304 graphics->temp_hbitmap_width = bounds.Width;
6305 graphics->temp_hbitmap_height = bounds.Height;
6307 bmih.biSize = sizeof(bmih);
6308 bmih.biWidth = graphics->temp_hbitmap_width;
6309 bmih.biHeight = -graphics->temp_hbitmap_height;
6310 bmih.biPlanes = 1;
6311 bmih.biBitCount = 32;
6312 bmih.biCompression = BI_RGB;
6313 bmih.biSizeImage = 0;
6314 bmih.biXPelsPerMeter = 0;
6315 bmih.biYPelsPerMeter = 0;
6316 bmih.biClrUsed = 0;
6317 bmih.biClrImportant = 0;
6319 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6320 (void**)&graphics->temp_bits, NULL, 0);
6321 if (!hbitmap)
6322 return GenericError;
6324 temp_hdc = CreateCompatibleDC(0);
6325 if (!temp_hdc)
6327 DeleteObject(hbitmap);
6328 return GenericError;
6331 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6332 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6334 SelectObject(temp_hdc, hbitmap);
6336 graphics->temp_hbitmap = hbitmap;
6337 *hdc = graphics->temp_hdc = temp_hdc;
6339 else
6341 *hdc = graphics->hdc;
6344 if (stat == Ok)
6345 graphics->busy = TRUE;
6347 return stat;
6350 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6352 GpStatus stat=Ok;
6354 TRACE("(%p, %p)\n", graphics, hdc);
6356 if(!graphics || !hdc || !graphics->busy)
6357 return InvalidParameter;
6359 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6361 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6363 else if (graphics->temp_hdc == hdc)
6365 DWORD* pos;
6366 int i;
6368 /* Find the pixels that have changed, and mark them as opaque. */
6369 pos = (DWORD*)graphics->temp_bits;
6370 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6372 if (*pos != DC_BACKGROUND_KEY)
6374 *pos |= 0xff000000;
6376 pos++;
6379 /* Write the changed pixels to the real target. */
6380 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6381 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6382 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6384 /* Clean up. */
6385 DeleteDC(graphics->temp_hdc);
6386 DeleteObject(graphics->temp_hbitmap);
6387 graphics->temp_hdc = NULL;
6388 graphics->temp_hbitmap = NULL;
6390 else if (hdc != graphics->hdc)
6392 stat = InvalidParameter;
6395 if (stat == Ok)
6396 graphics->busy = FALSE;
6398 return stat;
6401 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6403 GpRegion *clip;
6404 GpStatus status;
6405 GpMatrix device_to_world;
6407 TRACE("(%p, %p)\n", graphics, region);
6409 if(!graphics || !region)
6410 return InvalidParameter;
6412 if(graphics->busy)
6413 return ObjectBusy;
6415 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6416 return status;
6418 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6419 status = GdipTransformRegion(clip, &device_to_world);
6420 if (status != Ok)
6422 GdipDeleteRegion(clip);
6423 return status;
6426 /* free everything except root node and header */
6427 delete_element(&region->node);
6428 memcpy(region, clip, sizeof(GpRegion));
6429 heap_free(clip);
6431 return Ok;
6434 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6435 GpCoordinateSpace src_space, GpMatrix *matrix)
6437 GpStatus stat = Ok;
6438 REAL scale_x, scale_y;
6440 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6442 if (dst_space != src_space)
6444 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6445 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6447 if(graphics->unit != UnitDisplay)
6449 scale_x *= graphics->scale;
6450 scale_y *= graphics->scale;
6453 /* transform from src_space to CoordinateSpacePage */
6454 switch (src_space)
6456 case CoordinateSpaceWorld:
6457 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6458 break;
6459 case CoordinateSpacePage:
6460 break;
6461 case CoordinateSpaceDevice:
6462 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6463 break;
6466 /* transform from CoordinateSpacePage to dst_space */
6467 switch (dst_space)
6469 case CoordinateSpaceWorld:
6471 GpMatrix inverted_transform = graphics->worldtrans;
6472 stat = GdipInvertMatrix(&inverted_transform);
6473 if (stat == Ok)
6474 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6475 break;
6477 case CoordinateSpacePage:
6478 break;
6479 case CoordinateSpaceDevice:
6480 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6481 break;
6484 return stat;
6487 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6488 GpCoordinateSpace src_space, GpPointF *points, INT count)
6490 GpMatrix matrix;
6491 GpStatus stat;
6493 if(!graphics || !points || count <= 0)
6494 return InvalidParameter;
6496 if(graphics->busy)
6497 return ObjectBusy;
6499 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6501 if (src_space == dst_space) return Ok;
6503 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6504 if (stat != Ok) return stat;
6506 return GdipTransformMatrixPoints(&matrix, points, count);
6509 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6510 GpCoordinateSpace src_space, GpPoint *points, INT count)
6512 GpPointF *pointsF;
6513 GpStatus ret;
6514 INT i;
6516 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6518 if(count <= 0)
6519 return InvalidParameter;
6521 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6522 if(!pointsF)
6523 return OutOfMemory;
6525 for(i = 0; i < count; i++){
6526 pointsF[i].X = (REAL)points[i].X;
6527 pointsF[i].Y = (REAL)points[i].Y;
6530 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6532 if(ret == Ok)
6533 for(i = 0; i < count; i++){
6534 points[i].X = gdip_round(pointsF[i].X);
6535 points[i].Y = gdip_round(pointsF[i].Y);
6537 heap_free(pointsF);
6539 return ret;
6542 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6544 static int calls;
6546 TRACE("\n");
6548 if (!calls++)
6549 FIXME("stub\n");
6551 return NULL;
6554 /*****************************************************************************
6555 * GdipTranslateClip [GDIPLUS.@]
6557 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6559 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6561 if(!graphics)
6562 return InvalidParameter;
6564 if(graphics->busy)
6565 return ObjectBusy;
6567 return GdipTranslateRegion(graphics->clip, dx, dy);
6570 /*****************************************************************************
6571 * GdipTranslateClipI [GDIPLUS.@]
6573 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6575 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6577 if(!graphics)
6578 return InvalidParameter;
6580 if(graphics->busy)
6581 return ObjectBusy;
6583 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6587 /*****************************************************************************
6588 * GdipMeasureDriverString [GDIPLUS.@]
6590 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6591 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6592 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6594 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6595 HFONT hfont;
6596 HDC hdc;
6597 REAL min_x, min_y, max_x, max_y, x, y;
6598 int i;
6599 TEXTMETRICW textmetric;
6600 const WORD *glyph_indices;
6601 WORD *dynamic_glyph_indices=NULL;
6602 REAL rel_width, rel_height, ascent, descent;
6603 GpPointF pt[3];
6605 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6607 if (!graphics || !text || !font || !positions || !boundingBox)
6608 return InvalidParameter;
6610 if (length == -1)
6611 length = strlenW(text);
6613 if (length == 0)
6615 boundingBox->X = 0.0;
6616 boundingBox->Y = 0.0;
6617 boundingBox->Width = 0.0;
6618 boundingBox->Height = 0.0;
6621 if (flags & unsupported_flags)
6622 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6624 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6626 hdc = CreateCompatibleDC(0);
6627 SelectObject(hdc, hfont);
6629 GetTextMetricsW(hdc, &textmetric);
6631 pt[0].X = 0.0;
6632 pt[0].Y = 0.0;
6633 pt[1].X = 1.0;
6634 pt[1].Y = 0.0;
6635 pt[2].X = 0.0;
6636 pt[2].Y = 1.0;
6637 if (matrix)
6639 GpMatrix xform = *matrix;
6640 GdipTransformMatrixPoints(&xform, pt, 3);
6642 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6643 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6644 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6645 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6646 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6648 if (flags & DriverStringOptionsCmapLookup)
6650 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
6651 if (!glyph_indices)
6653 DeleteDC(hdc);
6654 DeleteObject(hfont);
6655 return OutOfMemory;
6658 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6660 else
6661 glyph_indices = text;
6663 min_x = max_x = x = positions[0].X;
6664 min_y = max_y = y = positions[0].Y;
6666 ascent = textmetric.tmAscent / rel_height;
6667 descent = textmetric.tmDescent / rel_height;
6669 for (i=0; i<length; i++)
6671 int char_width;
6672 ABC abc;
6674 if (!(flags & DriverStringOptionsRealizedAdvance))
6676 x = positions[i].X;
6677 y = positions[i].Y;
6680 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6681 char_width = abc.abcA + abc.abcB + abc.abcC;
6683 if (min_y > y - ascent) min_y = y - ascent;
6684 if (max_y < y + descent) max_y = y + descent;
6685 if (min_x > x) min_x = x;
6687 x += char_width / rel_width;
6689 if (max_x < x) max_x = x;
6692 heap_free(dynamic_glyph_indices);
6693 DeleteDC(hdc);
6694 DeleteObject(hfont);
6696 boundingBox->X = min_x;
6697 boundingBox->Y = min_y;
6698 boundingBox->Width = max_x - min_x;
6699 boundingBox->Height = max_y - min_y;
6701 return Ok;
6704 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6705 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6706 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6707 INT flags, GDIPCONST GpMatrix *matrix)
6709 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6710 INT save_state;
6711 GpPointF pt;
6712 HFONT hfont;
6713 UINT eto_flags=0;
6714 GpStatus status;
6715 HRGN hrgn;
6717 if (flags & unsupported_flags)
6718 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6720 if (!(flags & DriverStringOptionsCmapLookup))
6721 eto_flags |= ETO_GLYPH_INDEX;
6723 save_state = SaveDC(graphics->hdc);
6724 SetBkMode(graphics->hdc, TRANSPARENT);
6725 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6727 status = get_clip_hrgn(graphics, &hrgn);
6729 if (status == Ok && hrgn)
6731 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
6732 DeleteObject(hrgn);
6735 pt = positions[0];
6736 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6738 get_font_hfont(graphics, font, format, &hfont, matrix);
6739 SelectObject(graphics->hdc, hfont);
6741 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6743 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6745 RestoreDC(graphics->hdc, save_state);
6747 DeleteObject(hfont);
6749 return Ok;
6752 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6753 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6754 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6755 INT flags, GDIPCONST GpMatrix *matrix)
6757 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6758 GpStatus stat;
6759 PointF *real_positions, real_position;
6760 POINT *pti;
6761 HFONT hfont;
6762 HDC hdc;
6763 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6764 DWORD max_glyphsize=0;
6765 GLYPHMETRICS glyphmetrics;
6766 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6767 BYTE *glyph_mask;
6768 BYTE *text_mask;
6769 int text_mask_stride;
6770 BYTE *pixel_data;
6771 int pixel_data_stride;
6772 GpRect pixel_area;
6773 UINT ggo_flags = GGO_GRAY8_BITMAP;
6775 if (length <= 0)
6776 return Ok;
6778 if (!(flags & DriverStringOptionsCmapLookup))
6779 ggo_flags |= GGO_GLYPH_INDEX;
6781 if (flags & unsupported_flags)
6782 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6784 pti = heap_alloc_zero(sizeof(POINT) * length);
6785 if (!pti)
6786 return OutOfMemory;
6788 if (flags & DriverStringOptionsRealizedAdvance)
6790 real_position = positions[0];
6792 transform_and_round_points(graphics, pti, &real_position, 1);
6794 else
6796 real_positions = heap_alloc_zero(sizeof(PointF) * length);
6797 if (!real_positions)
6799 heap_free(pti);
6800 return OutOfMemory;
6803 memcpy(real_positions, positions, sizeof(PointF) * length);
6805 transform_and_round_points(graphics, pti, real_positions, length);
6807 heap_free(real_positions);
6810 get_font_hfont(graphics, font, format, &hfont, matrix);
6812 hdc = CreateCompatibleDC(0);
6813 SelectObject(hdc, hfont);
6815 /* Get the boundaries of the text to be drawn */
6816 for (i=0; i<length; i++)
6818 DWORD glyphsize;
6819 int left, top, right, bottom;
6821 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6822 &glyphmetrics, 0, NULL, &identity);
6824 if (glyphsize == GDI_ERROR)
6826 ERR("GetGlyphOutlineW failed\n");
6827 heap_free(pti);
6828 DeleteDC(hdc);
6829 DeleteObject(hfont);
6830 return GenericError;
6833 if (glyphsize > max_glyphsize)
6834 max_glyphsize = glyphsize;
6836 if (glyphsize != 0)
6838 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6839 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6840 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6841 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6843 if (left < min_x) min_x = left;
6844 if (top < min_y) min_y = top;
6845 if (right > max_x) max_x = right;
6846 if (bottom > max_y) max_y = bottom;
6849 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6851 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6852 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6856 if (max_glyphsize == 0)
6857 /* Nothing to draw. */
6858 return Ok;
6860 glyph_mask = heap_alloc_zero(max_glyphsize);
6861 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
6862 text_mask_stride = max_x - min_x;
6864 if (!(glyph_mask && text_mask))
6866 heap_free(glyph_mask);
6867 heap_free(text_mask);
6868 heap_free(pti);
6869 DeleteDC(hdc);
6870 DeleteObject(hfont);
6871 return OutOfMemory;
6874 /* Generate a mask for the text */
6875 for (i=0; i<length; i++)
6877 DWORD ret;
6878 int left, top, stride;
6880 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6881 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6883 if (ret == GDI_ERROR || ret == 0)
6884 continue; /* empty glyph */
6886 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6887 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6888 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6890 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6892 BYTE *glyph_val = glyph_mask + y * stride;
6893 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6894 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6896 *text_val = min(64, *text_val + *glyph_val);
6897 glyph_val++;
6898 text_val++;
6903 heap_free(pti);
6904 DeleteDC(hdc);
6905 DeleteObject(hfont);
6906 heap_free(glyph_mask);
6908 /* get the brush data */
6909 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
6910 if (!pixel_data)
6912 heap_free(text_mask);
6913 return OutOfMemory;
6916 pixel_area.X = min_x;
6917 pixel_area.Y = min_y;
6918 pixel_area.Width = max_x - min_x;
6919 pixel_area.Height = max_y - min_y;
6920 pixel_data_stride = pixel_area.Width * 4;
6922 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6923 if (stat != Ok)
6925 heap_free(text_mask);
6926 heap_free(pixel_data);
6927 return stat;
6930 /* multiply the brush data by the mask */
6931 for (y=0; y<pixel_area.Height; y++)
6933 BYTE *text_val = text_mask + text_mask_stride * y;
6934 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6935 for (x=0; x<pixel_area.Width; x++)
6937 *pixel_val = (*pixel_val) * (*text_val) / 64;
6938 text_val++;
6939 pixel_val+=4;
6943 heap_free(text_mask);
6945 /* draw the result */
6946 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6947 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
6949 heap_free(pixel_data);
6951 return stat;
6954 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6955 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6956 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6957 INT flags, GDIPCONST GpMatrix *matrix)
6959 GpStatus stat = NotImplemented;
6961 if (length == -1)
6962 length = strlenW(text);
6964 if (graphics->hdc && !graphics->alpha_hdc &&
6965 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6966 brush->bt == BrushTypeSolidColor &&
6967 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6968 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6969 brush, positions, flags, matrix);
6970 if (stat == NotImplemented)
6971 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6972 brush, positions, flags, matrix);
6973 return stat;
6976 /*****************************************************************************
6977 * GdipDrawDriverString [GDIPLUS.@]
6979 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6980 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6981 GDIPCONST PointF *positions, INT flags,
6982 GDIPCONST GpMatrix *matrix )
6984 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6986 if (!graphics || !text || !font || !brush || !positions)
6987 return InvalidParameter;
6989 return draw_driver_string(graphics, text, length, font, NULL,
6990 brush, positions, flags, matrix);
6993 /*****************************************************************************
6994 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6996 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6998 GpStatus stat;
6999 GpRegion* rgn;
7001 TRACE("(%p, %p)\n", graphics, res);
7003 if((stat = GdipCreateRegion(&rgn)) != Ok)
7004 return stat;
7006 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7007 goto cleanup;
7009 stat = GdipIsEmptyRegion(rgn, graphics, res);
7011 cleanup:
7012 GdipDeleteRegion(rgn);
7013 return stat;
7016 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7018 static int calls;
7020 TRACE("(%p) stub\n", graphics);
7022 if(!(calls++))
7023 FIXME("not implemented\n");
7025 return NotImplemented;