wined3d: Ignore set_priority calls on non-managed resources.
[wine.git] / dlls / gdiplus / graphics.c
blob861d16018be04274724c3855d9dc4edc142a4ac8
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
55 GpCoordinateSpace src_space, GpMatrix *matrix);
57 /* Converts from gdiplus path point type to gdi path point type. */
58 static BYTE convert_path_point_type(BYTE type)
60 BYTE ret;
62 switch(type & PathPointTypePathTypeMask){
63 case PathPointTypeBezier:
64 ret = PT_BEZIERTO;
65 break;
66 case PathPointTypeLine:
67 ret = PT_LINETO;
68 break;
69 case PathPointTypeStart:
70 ret = PT_MOVETO;
71 break;
72 default:
73 ERR("Bad point type\n");
74 return 0;
77 if(type & PathPointTypeCloseSubpath)
78 ret |= PT_CLOSEFIGURE;
80 return ret;
83 static COLORREF get_gdi_brush_color(const GpBrush *brush)
85 ARGB argb;
87 switch (brush->bt)
89 case BrushTypeSolidColor:
91 const GpSolidFill *sf = (const GpSolidFill *)brush;
92 argb = sf->color;
93 break;
95 case BrushTypeHatchFill:
97 const GpHatch *hatch = (const GpHatch *)brush;
98 argb = hatch->forecol;
99 break;
101 case BrushTypeLinearGradient:
103 const GpLineGradient *line = (const GpLineGradient *)brush;
104 argb = line->startcolor;
105 break;
107 case BrushTypePathGradient:
109 const GpPathGradient *grad = (const GpPathGradient *)brush;
110 argb = grad->centercolor;
111 break;
113 default:
114 FIXME("unhandled brush type %d\n", brush->bt);
115 argb = 0;
116 break;
118 return ARGB2COLORREF(argb);
121 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
123 HBITMAP hbmp;
124 BITMAPINFOHEADER bmih;
125 DWORD *bits;
126 int x, y;
128 bmih.biSize = sizeof(bmih);
129 bmih.biWidth = 8;
130 bmih.biHeight = 8;
131 bmih.biPlanes = 1;
132 bmih.biBitCount = 32;
133 bmih.biCompression = BI_RGB;
134 bmih.biSizeImage = 0;
136 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
137 if (hbmp)
139 const char *hatch_data;
141 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
143 for (y = 0; y < 8; y++)
145 for (x = 0; x < 8; x++)
147 if (hatch_data[y] & (0x80 >> x))
148 bits[y * 8 + x] = hatch->forecol;
149 else
150 bits[y * 8 + x] = hatch->backcol;
154 else
156 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
158 for (y = 0; y < 64; y++)
159 bits[y] = hatch->forecol;
163 return hbmp;
166 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
168 switch (brush->bt)
170 case BrushTypeSolidColor:
172 const GpSolidFill *sf = (const GpSolidFill *)brush;
173 lb->lbStyle = BS_SOLID;
174 lb->lbColor = ARGB2COLORREF(sf->color);
175 lb->lbHatch = 0;
176 return Ok;
179 case BrushTypeHatchFill:
181 const GpHatch *hatch = (const GpHatch *)brush;
182 HBITMAP hbmp;
184 hbmp = create_hatch_bitmap(hatch);
185 if (!hbmp) return OutOfMemory;
187 lb->lbStyle = BS_PATTERN;
188 lb->lbColor = 0;
189 lb->lbHatch = (ULONG_PTR)hbmp;
190 return Ok;
193 default:
194 FIXME("unhandled brush type %d\n", brush->bt);
195 lb->lbStyle = BS_SOLID;
196 lb->lbColor = get_gdi_brush_color(brush);
197 lb->lbHatch = 0;
198 return Ok;
202 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
204 switch (lb->lbStyle)
206 case BS_PATTERN:
207 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
208 break;
210 return Ok;
213 static HBRUSH create_gdi_brush(const GpBrush *brush)
215 LOGBRUSH lb;
216 HBRUSH gdibrush;
218 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
220 gdibrush = CreateBrushIndirect(&lb);
221 free_gdi_logbrush(&lb);
223 return gdibrush;
226 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
228 LOGBRUSH lb;
229 HPEN gdipen;
230 REAL width;
231 INT save_state, i, numdashes;
232 GpPointF pt[2];
233 DWORD dash_array[MAX_DASHLEN];
235 save_state = SaveDC(graphics->hdc);
237 EndPath(graphics->hdc);
239 if(pen->unit == UnitPixel){
240 width = pen->width;
242 else{
243 /* Get an estimate for the amount the pen width is affected by the world
244 * transform. (This is similar to what some of the wine drivers do.) */
245 pt[0].X = 0.0;
246 pt[0].Y = 0.0;
247 pt[1].X = 1.0;
248 pt[1].Y = 1.0;
249 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
250 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
251 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
253 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
256 if(pen->dash == DashStyleCustom){
257 numdashes = min(pen->numdashes, MAX_DASHLEN);
259 TRACE("dashes are: ");
260 for(i = 0; i < numdashes; i++){
261 dash_array[i] = gdip_round(width * pen->dashes[i]);
262 TRACE("%d, ", dash_array[i]);
264 TRACE("\n and the pen style is %x\n", pen->style);
266 create_gdi_logbrush(pen->brush, &lb);
267 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
268 numdashes, dash_array);
269 free_gdi_logbrush(&lb);
271 else
273 create_gdi_logbrush(pen->brush, &lb);
274 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
275 free_gdi_logbrush(&lb);
278 SelectObject(graphics->hdc, gdipen);
280 return save_state;
283 static void restore_dc(GpGraphics *graphics, INT state)
285 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
286 RestoreDC(graphics->hdc, state);
289 /* This helper applies all the changes that the points listed in ptf need in
290 * order to be drawn on the device context. In the end, this should include at
291 * least:
292 * -scaling by page unit
293 * -applying world transformation
294 * -converting from float to int
295 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
296 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
297 * gdi to draw, and these functions would irreparably mess with line widths.
299 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
300 GpPointF *ptf, INT count)
302 REAL scale_x, scale_y;
303 GpMatrix matrix;
304 int i;
306 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
307 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
309 /* apply page scale */
310 if(graphics->unit != UnitDisplay)
312 scale_x *= graphics->scale;
313 scale_y *= graphics->scale;
316 matrix = graphics->worldtrans;
317 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
318 GdipTransformMatrixPoints(&matrix, ptf, count);
320 for(i = 0; i < count; i++){
321 pti[i].x = gdip_round(ptf[i].X);
322 pti[i].y = gdip_round(ptf[i].Y);
326 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
327 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
329 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
331 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
333 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
334 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
336 else
338 BLENDFUNCTION bf;
340 bf.BlendOp = AC_SRC_OVER;
341 bf.BlendFlags = 0;
342 bf.SourceConstantAlpha = 255;
343 bf.AlphaFormat = AC_SRC_ALPHA;
345 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
346 hdc, src_x, src_y, src_width, src_height, bf);
350 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
352 /* clipping region is in device coords */
353 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
356 /* Draw non-premultiplied ARGB data to the given graphics object */
357 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
358 const BYTE *src, INT src_width, INT src_height, INT src_stride)
360 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
361 INT x, y;
363 for (x=0; x<src_width; x++)
365 for (y=0; y<src_height; y++)
367 ARGB dst_color, src_color;
368 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
369 src_color = ((ARGB*)(src + src_stride * y))[x];
370 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
374 return Ok;
377 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
378 const BYTE *src, INT src_width, INT src_height, INT src_stride)
380 HDC hdc;
381 HBITMAP hbitmap;
382 BITMAPINFOHEADER bih;
383 BYTE *temp_bits;
385 hdc = CreateCompatibleDC(0);
387 bih.biSize = sizeof(BITMAPINFOHEADER);
388 bih.biWidth = src_width;
389 bih.biHeight = -src_height;
390 bih.biPlanes = 1;
391 bih.biBitCount = 32;
392 bih.biCompression = BI_RGB;
393 bih.biSizeImage = 0;
394 bih.biXPelsPerMeter = 0;
395 bih.biYPelsPerMeter = 0;
396 bih.biClrUsed = 0;
397 bih.biClrImportant = 0;
399 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
400 (void**)&temp_bits, NULL, 0);
402 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
403 memcpy(temp_bits, src, src_width * src_height * 4);
404 else
405 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
406 4 * src_width, src, src_stride);
408 SelectObject(hdc, hbitmap);
409 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
410 hdc, 0, 0, src_width, src_height);
411 DeleteDC(hdc);
412 DeleteObject(hbitmap);
414 return Ok;
417 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
418 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
420 GpStatus stat=Ok;
422 if (graphics->image && graphics->image->type == ImageTypeBitmap)
424 DWORD i;
425 int size;
426 RGNDATA *rgndata;
427 RECT *rects;
428 HRGN hrgn, visible_rgn;
430 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
431 if (!hrgn)
432 return OutOfMemory;
434 stat = get_clip_hrgn(graphics, &visible_rgn);
435 if (stat != Ok)
437 DeleteObject(hrgn);
438 return stat;
441 if (visible_rgn)
443 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
444 DeleteObject(visible_rgn);
447 if (hregion)
448 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
450 size = GetRegionData(hrgn, 0, NULL);
452 rgndata = GdipAlloc(size);
453 if (!rgndata)
455 DeleteObject(hrgn);
456 return OutOfMemory;
459 GetRegionData(hrgn, size, rgndata);
461 rects = (RECT*)rgndata->Buffer;
463 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
465 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
466 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
467 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
468 src_stride);
471 GdipFree(rgndata);
473 DeleteObject(hrgn);
475 return stat;
477 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
479 ERR("This should not be used for metafiles; fix caller\n");
480 return NotImplemented;
482 else
484 HRGN hrgn;
485 int save;
487 stat = get_clip_hrgn(graphics, &hrgn);
489 if (stat != Ok)
490 return stat;
492 save = SaveDC(graphics->hdc);
494 if (hrgn)
495 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
497 if (hregion)
498 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
500 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
501 src_height, src_stride);
503 RestoreDC(graphics->hdc, save);
505 DeleteObject(hrgn);
507 return stat;
511 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
512 const BYTE *src, INT src_width, INT src_height, INT src_stride)
514 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL);
517 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
519 ARGB result=0;
520 ARGB i;
521 INT a1, a2, a3;
523 a1 = (start >> 24) & 0xff;
524 a2 = (end >> 24) & 0xff;
526 a3 = (int)(a1*(1.0f - position)+a2*(position));
528 result |= a3 << 24;
530 for (i=0xff; i<=0xff0000; i = i << 8)
531 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
532 return result;
535 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
537 REAL blendfac;
539 /* clamp to between 0.0 and 1.0, using the wrap mode */
540 if (brush->wrap == WrapModeTile)
542 position = fmodf(position, 1.0f);
543 if (position < 0.0f) position += 1.0f;
545 else /* WrapModeFlip* */
547 position = fmodf(position, 2.0f);
548 if (position < 0.0f) position += 2.0f;
549 if (position > 1.0f) position = 2.0f - position;
552 if (brush->blendcount == 1)
553 blendfac = position;
554 else
556 int i=1;
557 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
558 REAL range;
560 /* locate the blend positions surrounding this position */
561 while (position > brush->blendpos[i])
562 i++;
564 /* interpolate between the blend positions */
565 left_blendpos = brush->blendpos[i-1];
566 left_blendfac = brush->blendfac[i-1];
567 right_blendpos = brush->blendpos[i];
568 right_blendfac = brush->blendfac[i];
569 range = right_blendpos - left_blendpos;
570 blendfac = (left_blendfac * (right_blendpos - position) +
571 right_blendfac * (position - left_blendpos)) / range;
574 if (brush->pblendcount == 0)
575 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
576 else
578 int i=1;
579 ARGB left_blendcolor, right_blendcolor;
580 REAL left_blendpos, right_blendpos;
582 /* locate the blend colors surrounding this position */
583 while (blendfac > brush->pblendpos[i])
584 i++;
586 /* interpolate between the blend colors */
587 left_blendpos = brush->pblendpos[i-1];
588 left_blendcolor = brush->pblendcolor[i-1];
589 right_blendpos = brush->pblendpos[i];
590 right_blendcolor = brush->pblendcolor[i];
591 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
592 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
596 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
598 REAL val[5], res[4];
599 int i, j;
600 unsigned char a, r, g, b;
602 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
603 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
604 val[2] = (color & 0xff) / 255.0; /* blue */
605 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
606 val[4] = 1.0; /* translation */
608 for (i=0; i<4; i++)
610 res[i] = 0.0;
612 for (j=0; j<5; j++)
613 res[i] += matrix->m[j][i] * val[j];
616 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
617 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
618 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
619 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
621 return (a << 24) | (r << 16) | (g << 8) | b;
624 static BOOL color_is_gray(ARGB color)
626 unsigned char r, g, b;
628 r = (color >> 16) & 0xff;
629 g = (color >> 8) & 0xff;
630 b = color & 0xff;
632 return (r == g) && (g == b);
635 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
636 UINT width, UINT height, INT stride, ColorAdjustType type)
638 UINT x, y;
639 INT i;
641 if (attributes->colorkeys[type].enabled ||
642 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
644 const struct color_key *key;
645 BYTE min_blue, min_green, min_red;
646 BYTE max_blue, max_green, max_red;
648 if (attributes->colorkeys[type].enabled)
649 key = &attributes->colorkeys[type];
650 else
651 key = &attributes->colorkeys[ColorAdjustTypeDefault];
653 min_blue = key->low&0xff;
654 min_green = (key->low>>8)&0xff;
655 min_red = (key->low>>16)&0xff;
657 max_blue = key->high&0xff;
658 max_green = (key->high>>8)&0xff;
659 max_red = (key->high>>16)&0xff;
661 for (x=0; x<width; x++)
662 for (y=0; y<height; y++)
664 ARGB *src_color;
665 BYTE blue, green, red;
666 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
667 blue = *src_color&0xff;
668 green = (*src_color>>8)&0xff;
669 red = (*src_color>>16)&0xff;
670 if (blue >= min_blue && green >= min_green && red >= min_red &&
671 blue <= max_blue && green <= max_green && red <= max_red)
672 *src_color = 0x00000000;
676 if (attributes->colorremaptables[type].enabled ||
677 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
679 const struct color_remap_table *table;
681 if (attributes->colorremaptables[type].enabled)
682 table = &attributes->colorremaptables[type];
683 else
684 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
686 for (x=0; x<width; x++)
687 for (y=0; y<height; y++)
689 ARGB *src_color;
690 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
691 for (i=0; i<table->mapsize; i++)
693 if (*src_color == table->colormap[i].oldColor.Argb)
695 *src_color = table->colormap[i].newColor.Argb;
696 break;
702 if (attributes->colormatrices[type].enabled ||
703 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
705 const struct color_matrix *colormatrices;
707 if (attributes->colormatrices[type].enabled)
708 colormatrices = &attributes->colormatrices[type];
709 else
710 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
712 for (x=0; x<width; x++)
713 for (y=0; y<height; y++)
715 ARGB *src_color;
716 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
718 if (colormatrices->flags == ColorMatrixFlagsDefault ||
719 !color_is_gray(*src_color))
721 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
723 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
725 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
730 if (attributes->gamma_enabled[type] ||
731 attributes->gamma_enabled[ColorAdjustTypeDefault])
733 REAL gamma;
735 if (attributes->gamma_enabled[type])
736 gamma = attributes->gamma[type];
737 else
738 gamma = attributes->gamma[ColorAdjustTypeDefault];
740 for (x=0; x<width; x++)
741 for (y=0; y<height; y++)
743 ARGB *src_color;
744 BYTE blue, green, red;
745 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
747 blue = *src_color&0xff;
748 green = (*src_color>>8)&0xff;
749 red = (*src_color>>16)&0xff;
751 /* FIXME: We should probably use a table for this. */
752 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
753 green = floorf(powf(green / 255.0, gamma) * 255.0);
754 red = floorf(powf(red / 255.0, gamma) * 255.0);
756 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
761 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
762 * bitmap that contains all the pixels we may need to draw it. */
763 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
764 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
765 GpRect *rect)
767 INT left, top, right, bottom;
769 switch (interpolation)
771 case InterpolationModeHighQualityBilinear:
772 case InterpolationModeHighQualityBicubic:
773 /* FIXME: Include a greater range for the prefilter? */
774 case InterpolationModeBicubic:
775 case InterpolationModeBilinear:
776 left = (INT)(floorf(srcx));
777 top = (INT)(floorf(srcy));
778 right = (INT)(ceilf(srcx+srcwidth));
779 bottom = (INT)(ceilf(srcy+srcheight));
780 break;
781 case InterpolationModeNearestNeighbor:
782 default:
783 left = gdip_round(srcx);
784 top = gdip_round(srcy);
785 right = gdip_round(srcx+srcwidth);
786 bottom = gdip_round(srcy+srcheight);
787 break;
790 if (wrap == WrapModeClamp)
792 if (left < 0)
793 left = 0;
794 if (top < 0)
795 top = 0;
796 if (right >= bitmap->width)
797 right = bitmap->width-1;
798 if (bottom >= bitmap->height)
799 bottom = bitmap->height-1;
801 else
803 /* In some cases we can make the rectangle smaller here, but the logic
804 * is hard to get right, and tiling suggests we're likely to use the
805 * entire source image. */
806 if (left < 0 || right >= bitmap->width)
808 left = 0;
809 right = bitmap->width-1;
812 if (top < 0 || bottom >= bitmap->height)
814 top = 0;
815 bottom = bitmap->height-1;
819 rect->X = left;
820 rect->Y = top;
821 rect->Width = right - left + 1;
822 rect->Height = bottom - top + 1;
825 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
826 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
828 if (attributes->wrap == WrapModeClamp)
830 if (x < 0 || y < 0 || x >= width || y >= height)
831 return attributes->outside_color;
833 else
835 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
836 if (x < 0)
837 x = width*2 + x % (width * 2);
838 if (y < 0)
839 y = height*2 + y % (height * 2);
841 if ((attributes->wrap & 1) == 1)
843 /* Flip X */
844 if ((x / width) % 2 == 0)
845 x = x % width;
846 else
847 x = width - 1 - x % width;
849 else
850 x = x % width;
852 if ((attributes->wrap & 2) == 2)
854 /* Flip Y */
855 if ((y / height) % 2 == 0)
856 y = y % height;
857 else
858 y = height - 1 - y % height;
860 else
861 y = y % height;
864 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
866 ERR("out of range pixel requested\n");
867 return 0xffcd0084;
870 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
873 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
874 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
875 InterpolationMode interpolation, PixelOffsetMode offset_mode)
877 static int fixme;
879 switch (interpolation)
881 default:
882 if (!fixme++)
883 FIXME("Unimplemented interpolation %i\n", interpolation);
884 /* fall-through */
885 case InterpolationModeBilinear:
887 REAL leftxf, topyf;
888 INT leftx, rightx, topy, bottomy;
889 ARGB topleft, topright, bottomleft, bottomright;
890 ARGB top, bottom;
891 float x_offset;
893 leftxf = floorf(point->X);
894 leftx = (INT)leftxf;
895 rightx = (INT)ceilf(point->X);
896 topyf = floorf(point->Y);
897 topy = (INT)topyf;
898 bottomy = (INT)ceilf(point->Y);
900 if (leftx == rightx && topy == bottomy)
901 return sample_bitmap_pixel(src_rect, bits, width, height,
902 leftx, topy, attributes);
904 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
905 leftx, topy, attributes);
906 topright = sample_bitmap_pixel(src_rect, bits, width, height,
907 rightx, topy, attributes);
908 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
909 leftx, bottomy, attributes);
910 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
911 rightx, bottomy, attributes);
913 x_offset = point->X - leftxf;
914 top = blend_colors(topleft, topright, x_offset);
915 bottom = blend_colors(bottomleft, bottomright, x_offset);
917 return blend_colors(top, bottom, point->Y - topyf);
919 case InterpolationModeNearestNeighbor:
921 FLOAT pixel_offset;
922 switch (offset_mode)
924 default:
925 case PixelOffsetModeNone:
926 case PixelOffsetModeHighSpeed:
927 pixel_offset = 0.5;
928 break;
930 case PixelOffsetModeHalf:
931 case PixelOffsetModeHighQuality:
932 pixel_offset = 0.0;
933 break;
935 return sample_bitmap_pixel(src_rect, bits, width, height,
936 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
942 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
944 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
947 static BOOL brush_can_fill_path(GpBrush *brush)
949 switch (brush->bt)
951 case BrushTypeSolidColor:
952 return TRUE;
953 case BrushTypeHatchFill:
955 GpHatch *hatch = (GpHatch*)brush;
956 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
957 ((hatch->backcol & 0xff000000) == 0xff000000);
959 case BrushTypeLinearGradient:
960 case BrushTypeTextureFill:
961 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
962 default:
963 return FALSE;
967 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
969 switch (brush->bt)
971 case BrushTypeSolidColor:
973 GpSolidFill *fill = (GpSolidFill*)brush;
974 HBITMAP bmp = ARGB2BMP(fill->color);
976 if (bmp)
978 RECT rc;
979 /* partially transparent fill */
981 SelectClipPath(graphics->hdc, RGN_AND);
982 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
984 HDC hdc = CreateCompatibleDC(NULL);
986 if (!hdc) break;
988 SelectObject(hdc, bmp);
989 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
990 hdc, 0, 0, 1, 1);
991 DeleteDC(hdc);
994 DeleteObject(bmp);
995 break;
997 /* else fall through */
999 default:
1001 HBRUSH gdibrush, old_brush;
1003 gdibrush = create_gdi_brush(brush);
1004 if (!gdibrush) return;
1006 old_brush = SelectObject(graphics->hdc, gdibrush);
1007 FillPath(graphics->hdc);
1008 SelectObject(graphics->hdc, old_brush);
1009 DeleteObject(gdibrush);
1010 break;
1015 static BOOL brush_can_fill_pixels(GpBrush *brush)
1017 switch (brush->bt)
1019 case BrushTypeSolidColor:
1020 case BrushTypeHatchFill:
1021 case BrushTypeLinearGradient:
1022 case BrushTypeTextureFill:
1023 case BrushTypePathGradient:
1024 return TRUE;
1025 default:
1026 return FALSE;
1030 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1031 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1033 switch (brush->bt)
1035 case BrushTypeSolidColor:
1037 int x, y;
1038 GpSolidFill *fill = (GpSolidFill*)brush;
1039 for (x=0; x<fill_area->Width; x++)
1040 for (y=0; y<fill_area->Height; y++)
1041 argb_pixels[x + y*cdwStride] = fill->color;
1042 return Ok;
1044 case BrushTypeHatchFill:
1046 int x, y;
1047 GpHatch *fill = (GpHatch*)brush;
1048 const char *hatch_data;
1050 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1051 return NotImplemented;
1053 for (x=0; x<fill_area->Width; x++)
1054 for (y=0; y<fill_area->Height; y++)
1056 int hx, hy;
1058 /* FIXME: Account for the rendering origin */
1059 hx = (x + fill_area->X) % 8;
1060 hy = (y + fill_area->Y) % 8;
1062 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1063 argb_pixels[x + y*cdwStride] = fill->forecol;
1064 else
1065 argb_pixels[x + y*cdwStride] = fill->backcol;
1068 return Ok;
1070 case BrushTypeLinearGradient:
1072 GpLineGradient *fill = (GpLineGradient*)brush;
1073 GpPointF draw_points[3], line_points[3];
1074 GpStatus stat;
1075 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1076 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1077 int x, y;
1079 draw_points[0].X = fill_area->X;
1080 draw_points[0].Y = fill_area->Y;
1081 draw_points[1].X = fill_area->X+1;
1082 draw_points[1].Y = fill_area->Y;
1083 draw_points[2].X = fill_area->X;
1084 draw_points[2].Y = fill_area->Y+1;
1086 /* Transform the points to a co-ordinate space where X is the point's
1087 * position in the gradient, 0.0 being the start point and 1.0 the
1088 * end point. */
1089 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1090 CoordinateSpaceDevice, draw_points, 3);
1092 if (stat == Ok)
1094 line_points[0] = fill->startpoint;
1095 line_points[1] = fill->endpoint;
1096 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1097 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1099 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1102 if (stat == Ok)
1104 stat = GdipInvertMatrix(world_to_gradient);
1106 if (stat == Ok)
1107 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1109 GdipDeleteMatrix(world_to_gradient);
1112 if (stat == Ok)
1114 REAL x_delta = draw_points[1].X - draw_points[0].X;
1115 REAL y_delta = draw_points[2].X - draw_points[0].X;
1117 for (y=0; y<fill_area->Height; y++)
1119 for (x=0; x<fill_area->Width; x++)
1121 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1123 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1128 return stat;
1130 case BrushTypeTextureFill:
1132 GpTexture *fill = (GpTexture*)brush;
1133 GpPointF draw_points[3];
1134 GpStatus stat;
1135 int x, y;
1136 GpBitmap *bitmap;
1137 int src_stride;
1138 GpRect src_area;
1140 if (fill->image->type != ImageTypeBitmap)
1142 FIXME("metafile texture brushes not implemented\n");
1143 return NotImplemented;
1146 bitmap = (GpBitmap*)fill->image;
1147 src_stride = sizeof(ARGB) * bitmap->width;
1149 src_area.X = src_area.Y = 0;
1150 src_area.Width = bitmap->width;
1151 src_area.Height = bitmap->height;
1153 draw_points[0].X = fill_area->X;
1154 draw_points[0].Y = fill_area->Y;
1155 draw_points[1].X = fill_area->X+1;
1156 draw_points[1].Y = fill_area->Y;
1157 draw_points[2].X = fill_area->X;
1158 draw_points[2].Y = fill_area->Y+1;
1160 /* Transform the points to the co-ordinate space of the bitmap. */
1161 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1162 CoordinateSpaceDevice, draw_points, 3);
1164 if (stat == Ok)
1166 GpMatrix world_to_texture = fill->transform;
1168 stat = GdipInvertMatrix(&world_to_texture);
1169 if (stat == Ok)
1170 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1173 if (stat == Ok && !fill->bitmap_bits)
1175 BitmapData lockeddata;
1177 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1178 if (!fill->bitmap_bits)
1179 stat = OutOfMemory;
1181 if (stat == Ok)
1183 lockeddata.Width = bitmap->width;
1184 lockeddata.Height = bitmap->height;
1185 lockeddata.Stride = src_stride;
1186 lockeddata.PixelFormat = PixelFormat32bppARGB;
1187 lockeddata.Scan0 = fill->bitmap_bits;
1189 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1190 PixelFormat32bppARGB, &lockeddata);
1193 if (stat == Ok)
1194 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1196 if (stat == Ok)
1197 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1198 bitmap->width, bitmap->height,
1199 src_stride, ColorAdjustTypeBitmap);
1201 if (stat != Ok)
1203 GdipFree(fill->bitmap_bits);
1204 fill->bitmap_bits = NULL;
1208 if (stat == Ok)
1210 REAL x_dx = draw_points[1].X - draw_points[0].X;
1211 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1212 REAL y_dx = draw_points[2].X - draw_points[0].X;
1213 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1215 for (y=0; y<fill_area->Height; y++)
1217 for (x=0; x<fill_area->Width; x++)
1219 GpPointF point;
1220 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1221 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1223 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1224 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1225 &point, fill->imageattributes, graphics->interpolation,
1226 graphics->pixeloffset);
1231 return stat;
1233 case BrushTypePathGradient:
1235 GpPathGradient *fill = (GpPathGradient*)brush;
1236 GpPath *flat_path;
1237 GpMatrix world_to_device;
1238 GpStatus stat;
1239 int i, figure_start=0;
1240 GpPointF start_point, end_point, center_point;
1241 BYTE type;
1242 REAL min_yf, max_yf, line1_xf, line2_xf;
1243 INT min_y, max_y, min_x, max_x;
1244 INT x, y;
1245 ARGB outer_color;
1246 static BOOL transform_fixme_once;
1248 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1250 static int once;
1251 if (!once++)
1252 FIXME("path gradient focus not implemented\n");
1255 if (fill->gamma)
1257 static int once;
1258 if (!once++)
1259 FIXME("path gradient gamma correction not implemented\n");
1262 if (fill->blendcount)
1264 static int once;
1265 if (!once++)
1266 FIXME("path gradient blend not implemented\n");
1269 if (fill->pblendcount)
1271 static int once;
1272 if (!once++)
1273 FIXME("path gradient preset blend not implemented\n");
1276 if (!transform_fixme_once)
1278 BOOL is_identity=TRUE;
1279 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1280 if (!is_identity)
1282 FIXME("path gradient transform not implemented\n");
1283 transform_fixme_once = TRUE;
1287 stat = GdipClonePath(fill->path, &flat_path);
1289 if (stat != Ok)
1290 return stat;
1292 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1293 CoordinateSpaceWorld, &world_to_device);
1294 if (stat == Ok)
1296 stat = GdipTransformPath(flat_path, &world_to_device);
1298 if (stat == Ok)
1300 center_point = fill->center;
1301 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1304 if (stat == Ok)
1305 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1308 if (stat != Ok)
1310 GdipDeletePath(flat_path);
1311 return stat;
1314 for (i=0; i<flat_path->pathdata.Count; i++)
1316 int start_center_line=0, end_center_line=0;
1317 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1318 REAL center_distance;
1319 ARGB start_color, end_color;
1320 REAL dy, dx;
1322 type = flat_path->pathdata.Types[i];
1324 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1325 figure_start = i;
1327 start_point = flat_path->pathdata.Points[i];
1329 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1331 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1333 end_point = flat_path->pathdata.Points[figure_start];
1334 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1336 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1338 end_point = flat_path->pathdata.Points[i+1];
1339 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1341 else
1342 continue;
1344 outer_color = start_color;
1346 min_yf = center_point.Y;
1347 if (min_yf > start_point.Y) min_yf = start_point.Y;
1348 if (min_yf > end_point.Y) min_yf = end_point.Y;
1350 if (min_yf < fill_area->Y)
1351 min_y = fill_area->Y;
1352 else
1353 min_y = (INT)ceil(min_yf);
1355 max_yf = center_point.Y;
1356 if (max_yf < start_point.Y) max_yf = start_point.Y;
1357 if (max_yf < end_point.Y) max_yf = end_point.Y;
1359 if (max_yf > fill_area->Y + fill_area->Height)
1360 max_y = fill_area->Y + fill_area->Height;
1361 else
1362 max_y = (INT)ceil(max_yf);
1364 dy = end_point.Y - start_point.Y;
1365 dx = end_point.X - start_point.X;
1367 /* This is proportional to the distance from start-end line to center point. */
1368 center_distance = dy * (start_point.X - center_point.X) +
1369 dx * (center_point.Y - start_point.Y);
1371 for (y=min_y; y<max_y; y++)
1373 REAL yf = (REAL)y;
1375 if (!seen_start && yf >= start_point.Y)
1377 seen_start = TRUE;
1378 start_center_line ^= 1;
1380 if (!seen_end && yf >= end_point.Y)
1382 seen_end = TRUE;
1383 end_center_line ^= 1;
1385 if (!seen_center && yf >= center_point.Y)
1387 seen_center = TRUE;
1388 start_center_line ^= 1;
1389 end_center_line ^= 1;
1392 if (start_center_line)
1393 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1394 else
1395 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1397 if (end_center_line)
1398 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1399 else
1400 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1402 if (line1_xf < line2_xf)
1404 min_x = (INT)ceil(line1_xf);
1405 max_x = (INT)ceil(line2_xf);
1407 else
1409 min_x = (INT)ceil(line2_xf);
1410 max_x = (INT)ceil(line1_xf);
1413 if (min_x < fill_area->X)
1414 min_x = fill_area->X;
1415 if (max_x > fill_area->X + fill_area->Width)
1416 max_x = fill_area->X + fill_area->Width;
1418 for (x=min_x; x<max_x; x++)
1420 REAL xf = (REAL)x;
1421 REAL distance;
1423 if (start_color != end_color)
1425 REAL blend_amount, pdy, pdx;
1426 pdy = yf - center_point.Y;
1427 pdx = xf - center_point.X;
1428 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1429 outer_color = blend_colors(start_color, end_color, blend_amount);
1432 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1433 (end_point.X - start_point.X) * (yf - start_point.Y);
1435 distance = distance / center_distance;
1437 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1438 blend_colors(outer_color, fill->centercolor, distance);
1443 GdipDeletePath(flat_path);
1444 return stat;
1446 default:
1447 return NotImplemented;
1451 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1452 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1453 * should not be called on an hdc that has a path you care about. */
1454 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1455 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1457 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1458 GpMatrix matrix;
1459 HBRUSH brush = NULL;
1460 HPEN pen = NULL;
1461 PointF ptf[4], *custptf = NULL;
1462 POINT pt[4], *custpt = NULL;
1463 BYTE *tp = NULL;
1464 REAL theta, dsmall, dbig, dx, dy = 0.0;
1465 INT i, count;
1466 LOGBRUSH lb;
1467 BOOL customstroke;
1469 if((x1 == x2) && (y1 == y2))
1470 return;
1472 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1474 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1475 if(!customstroke){
1476 brush = CreateSolidBrush(color);
1477 lb.lbStyle = BS_SOLID;
1478 lb.lbColor = color;
1479 lb.lbHatch = 0;
1480 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1481 PS_JOIN_MITER, 1, &lb, 0,
1482 NULL);
1483 oldbrush = SelectObject(graphics->hdc, brush);
1484 oldpen = SelectObject(graphics->hdc, pen);
1487 switch(cap){
1488 case LineCapFlat:
1489 break;
1490 case LineCapSquare:
1491 case LineCapSquareAnchor:
1492 case LineCapDiamondAnchor:
1493 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1494 if(cap == LineCapDiamondAnchor){
1495 dsmall = cos(theta + M_PI_2) * size;
1496 dbig = sin(theta + M_PI_2) * size;
1498 else{
1499 dsmall = cos(theta + M_PI_4) * size;
1500 dbig = sin(theta + M_PI_4) * size;
1503 ptf[0].X = x2 - dsmall;
1504 ptf[1].X = x2 + dbig;
1506 ptf[0].Y = y2 - dbig;
1507 ptf[3].Y = y2 + dsmall;
1509 ptf[1].Y = y2 - dsmall;
1510 ptf[2].Y = y2 + dbig;
1512 ptf[3].X = x2 - dbig;
1513 ptf[2].X = x2 + dsmall;
1515 transform_and_round_points(graphics, pt, ptf, 4);
1516 Polygon(graphics->hdc, pt, 4);
1518 break;
1519 case LineCapArrowAnchor:
1520 size = size * 4.0 / sqrt(3.0);
1522 dx = cos(M_PI / 6.0 + theta) * size;
1523 dy = sin(M_PI / 6.0 + theta) * size;
1525 ptf[0].X = x2 - dx;
1526 ptf[0].Y = y2 - dy;
1528 dx = cos(- M_PI / 6.0 + theta) * size;
1529 dy = sin(- M_PI / 6.0 + theta) * size;
1531 ptf[1].X = x2 - dx;
1532 ptf[1].Y = y2 - dy;
1534 ptf[2].X = x2;
1535 ptf[2].Y = y2;
1537 transform_and_round_points(graphics, pt, ptf, 3);
1538 Polygon(graphics->hdc, pt, 3);
1540 break;
1541 case LineCapRoundAnchor:
1542 dx = dy = ANCHOR_WIDTH * size / 2.0;
1544 ptf[0].X = x2 - dx;
1545 ptf[0].Y = y2 - dy;
1546 ptf[1].X = x2 + dx;
1547 ptf[1].Y = y2 + dy;
1549 transform_and_round_points(graphics, pt, ptf, 2);
1550 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1552 break;
1553 case LineCapTriangle:
1554 size = size / 2.0;
1555 dx = cos(M_PI_2 + theta) * size;
1556 dy = sin(M_PI_2 + theta) * size;
1558 ptf[0].X = x2 - dx;
1559 ptf[0].Y = y2 - dy;
1560 ptf[1].X = x2 + dx;
1561 ptf[1].Y = y2 + dy;
1563 dx = cos(theta) * size;
1564 dy = sin(theta) * size;
1566 ptf[2].X = x2 + dx;
1567 ptf[2].Y = y2 + dy;
1569 transform_and_round_points(graphics, pt, ptf, 3);
1570 Polygon(graphics->hdc, pt, 3);
1572 break;
1573 case LineCapRound:
1574 dx = dy = size / 2.0;
1576 ptf[0].X = x2 - dx;
1577 ptf[0].Y = y2 - dy;
1578 ptf[1].X = x2 + dx;
1579 ptf[1].Y = y2 + dy;
1581 dx = -cos(M_PI_2 + theta) * size;
1582 dy = -sin(M_PI_2 + theta) * size;
1584 ptf[2].X = x2 - dx;
1585 ptf[2].Y = y2 - dy;
1586 ptf[3].X = x2 + dx;
1587 ptf[3].Y = y2 + dy;
1589 transform_and_round_points(graphics, pt, ptf, 4);
1590 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1591 pt[2].y, pt[3].x, pt[3].y);
1593 break;
1594 case LineCapCustom:
1595 if(!custom)
1596 break;
1598 count = custom->pathdata.Count;
1599 custptf = GdipAlloc(count * sizeof(PointF));
1600 custpt = GdipAlloc(count * sizeof(POINT));
1601 tp = GdipAlloc(count);
1603 if(!custptf || !custpt || !tp)
1604 goto custend;
1606 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1608 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1609 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1610 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1611 MatrixOrderAppend);
1612 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1613 GdipTransformMatrixPoints(&matrix, custptf, count);
1615 transform_and_round_points(graphics, custpt, custptf, count);
1617 for(i = 0; i < count; i++)
1618 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1620 if(custom->fill){
1621 BeginPath(graphics->hdc);
1622 PolyDraw(graphics->hdc, custpt, tp, count);
1623 EndPath(graphics->hdc);
1624 StrokeAndFillPath(graphics->hdc);
1626 else
1627 PolyDraw(graphics->hdc, custpt, tp, count);
1629 custend:
1630 GdipFree(custptf);
1631 GdipFree(custpt);
1632 GdipFree(tp);
1633 break;
1634 default:
1635 break;
1638 if(!customstroke){
1639 SelectObject(graphics->hdc, oldbrush);
1640 SelectObject(graphics->hdc, oldpen);
1641 DeleteObject(brush);
1642 DeleteObject(pen);
1646 /* Shortens the line by the given percent by changing x2, y2.
1647 * If percent is > 1.0 then the line will change direction.
1648 * If percent is negative it can lengthen the line. */
1649 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1651 REAL dist, theta, dx, dy;
1653 if((y1 == *y2) && (x1 == *x2))
1654 return;
1656 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1657 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1658 dx = cos(theta) * dist;
1659 dy = sin(theta) * dist;
1661 *x2 = *x2 + dx;
1662 *y2 = *y2 + dy;
1665 /* Shortens the line by the given amount by changing x2, y2.
1666 * If the amount is greater than the distance, the line will become length 0.
1667 * If the amount is negative, it can lengthen the line. */
1668 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1670 REAL dx, dy, percent;
1672 dx = *x2 - x1;
1673 dy = *y2 - y1;
1674 if(dx == 0 && dy == 0)
1675 return;
1677 percent = amt / sqrt(dx * dx + dy * dy);
1678 if(percent >= 1.0){
1679 *x2 = x1;
1680 *y2 = y1;
1681 return;
1684 shorten_line_percent(x1, y1, x2, y2, percent);
1687 /* Conducts a linear search to find the bezier points that will back off
1688 * the endpoint of the curve by a distance of amt. Linear search works
1689 * better than binary in this case because there are multiple solutions,
1690 * and binary searches often find a bad one. I don't think this is what
1691 * Windows does but short of rendering the bezier without GDI's help it's
1692 * the best we can do. If rev then work from the start of the passed points
1693 * instead of the end. */
1694 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1696 GpPointF origpt[4];
1697 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1698 INT i, first = 0, second = 1, third = 2, fourth = 3;
1700 if(rev){
1701 first = 3;
1702 second = 2;
1703 third = 1;
1704 fourth = 0;
1707 origx = pt[fourth].X;
1708 origy = pt[fourth].Y;
1709 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1711 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1712 /* reset bezier points to original values */
1713 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1714 /* Perform magic on bezier points. Order is important here.*/
1715 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1716 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1717 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1718 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1719 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1720 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1722 dx = pt[fourth].X - origx;
1723 dy = pt[fourth].Y - origy;
1725 diff = sqrt(dx * dx + dy * dy);
1726 percent += 0.0005 * amt;
1730 /* Draws a combination of bezier curves and lines between points. */
1731 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1732 GDIPCONST BYTE * types, INT count, BOOL caps)
1734 POINT *pti = GdipAlloc(count * sizeof(POINT));
1735 BYTE *tp = GdipAlloc(count);
1736 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1737 INT i, j;
1738 GpStatus status = GenericError;
1740 if(!count){
1741 status = Ok;
1742 goto end;
1744 if(!pti || !tp || !ptcopy){
1745 status = OutOfMemory;
1746 goto end;
1749 for(i = 1; i < count; i++){
1750 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1751 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1752 || !(types[i + 1] & PathPointTypeBezier)){
1753 ERR("Bad bezier points\n");
1754 goto end;
1756 i += 2;
1760 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1762 /* If we are drawing caps, go through the points and adjust them accordingly,
1763 * and draw the caps. */
1764 if(caps){
1765 switch(types[count - 1] & PathPointTypePathTypeMask){
1766 case PathPointTypeBezier:
1767 if(pen->endcap == LineCapArrowAnchor)
1768 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1769 else if((pen->endcap == LineCapCustom) && pen->customend)
1770 shorten_bezier_amt(&ptcopy[count - 4],
1771 pen->width * pen->customend->inset, FALSE);
1773 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1774 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1775 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1776 pt[count - 1].X, pt[count - 1].Y);
1778 break;
1779 case PathPointTypeLine:
1780 if(pen->endcap == LineCapArrowAnchor)
1781 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1782 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1783 pen->width);
1784 else if((pen->endcap == LineCapCustom) && pen->customend)
1785 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1786 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1787 pen->customend->inset * pen->width);
1789 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1790 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1791 pt[count - 1].Y);
1793 break;
1794 default:
1795 ERR("Bad path last point\n");
1796 goto end;
1799 /* Find start of points */
1800 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1801 == PathPointTypeStart); j++);
1803 switch(types[j] & PathPointTypePathTypeMask){
1804 case PathPointTypeBezier:
1805 if(pen->startcap == LineCapArrowAnchor)
1806 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1807 else if((pen->startcap == LineCapCustom) && pen->customstart)
1808 shorten_bezier_amt(&ptcopy[j - 1],
1809 pen->width * pen->customstart->inset, TRUE);
1811 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1812 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1813 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1814 pt[j - 1].X, pt[j - 1].Y);
1816 break;
1817 case PathPointTypeLine:
1818 if(pen->startcap == LineCapArrowAnchor)
1819 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1820 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1821 pen->width);
1822 else if((pen->startcap == LineCapCustom) && pen->customstart)
1823 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1824 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1825 pen->customstart->inset * pen->width);
1827 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1828 pt[j].X, pt[j].Y, pt[j - 1].X,
1829 pt[j - 1].Y);
1831 break;
1832 default:
1833 ERR("Bad path points\n");
1834 goto end;
1838 transform_and_round_points(graphics, pti, ptcopy, count);
1840 for(i = 0; i < count; i++){
1841 tp[i] = convert_path_point_type(types[i]);
1844 PolyDraw(graphics->hdc, pti, tp, count);
1846 status = Ok;
1848 end:
1849 GdipFree(pti);
1850 GdipFree(ptcopy);
1851 GdipFree(tp);
1853 return status;
1856 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1858 GpStatus result;
1860 BeginPath(graphics->hdc);
1861 result = draw_poly(graphics, NULL, path->pathdata.Points,
1862 path->pathdata.Types, path->pathdata.Count, FALSE);
1863 EndPath(graphics->hdc);
1864 return result;
1867 typedef struct _GraphicsContainerItem {
1868 struct list entry;
1869 GraphicsContainer contid;
1871 SmoothingMode smoothing;
1872 CompositingQuality compqual;
1873 InterpolationMode interpolation;
1874 CompositingMode compmode;
1875 TextRenderingHint texthint;
1876 REAL scale;
1877 GpUnit unit;
1878 PixelOffsetMode pixeloffset;
1879 UINT textcontrast;
1880 GpMatrix worldtrans;
1881 GpRegion* clip;
1882 INT origin_x, origin_y;
1883 } GraphicsContainerItem;
1885 static GpStatus init_container(GraphicsContainerItem** container,
1886 GDIPCONST GpGraphics* graphics){
1887 GpStatus sts;
1889 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1890 if(!(*container))
1891 return OutOfMemory;
1893 (*container)->contid = graphics->contid + 1;
1895 (*container)->smoothing = graphics->smoothing;
1896 (*container)->compqual = graphics->compqual;
1897 (*container)->interpolation = graphics->interpolation;
1898 (*container)->compmode = graphics->compmode;
1899 (*container)->texthint = graphics->texthint;
1900 (*container)->scale = graphics->scale;
1901 (*container)->unit = graphics->unit;
1902 (*container)->textcontrast = graphics->textcontrast;
1903 (*container)->pixeloffset = graphics->pixeloffset;
1904 (*container)->origin_x = graphics->origin_x;
1905 (*container)->origin_y = graphics->origin_y;
1906 (*container)->worldtrans = graphics->worldtrans;
1908 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1909 if(sts != Ok){
1910 GdipFree(*container);
1911 *container = NULL;
1912 return sts;
1915 return Ok;
1918 static void delete_container(GraphicsContainerItem* container)
1920 GdipDeleteRegion(container->clip);
1921 GdipFree(container);
1924 static GpStatus restore_container(GpGraphics* graphics,
1925 GDIPCONST GraphicsContainerItem* container){
1926 GpStatus sts;
1927 GpRegion *newClip;
1929 sts = GdipCloneRegion(container->clip, &newClip);
1930 if(sts != Ok) return sts;
1932 graphics->worldtrans = container->worldtrans;
1934 GdipDeleteRegion(graphics->clip);
1935 graphics->clip = newClip;
1937 graphics->contid = container->contid - 1;
1939 graphics->smoothing = container->smoothing;
1940 graphics->compqual = container->compqual;
1941 graphics->interpolation = container->interpolation;
1942 graphics->compmode = container->compmode;
1943 graphics->texthint = container->texthint;
1944 graphics->scale = container->scale;
1945 graphics->unit = container->unit;
1946 graphics->textcontrast = container->textcontrast;
1947 graphics->pixeloffset = container->pixeloffset;
1948 graphics->origin_x = container->origin_x;
1949 graphics->origin_y = container->origin_y;
1951 return Ok;
1954 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1956 RECT wnd_rect;
1957 GpStatus stat=Ok;
1958 GpUnit unit;
1960 if(graphics->hwnd) {
1961 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1962 return GenericError;
1964 rect->X = wnd_rect.left;
1965 rect->Y = wnd_rect.top;
1966 rect->Width = wnd_rect.right - wnd_rect.left;
1967 rect->Height = wnd_rect.bottom - wnd_rect.top;
1968 }else if (graphics->image){
1969 stat = GdipGetImageBounds(graphics->image, rect, &unit);
1970 if (stat == Ok && unit != UnitPixel)
1971 FIXME("need to convert from unit %i\n", unit);
1972 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
1973 HBITMAP hbmp;
1974 BITMAP bmp;
1976 rect->X = 0;
1977 rect->Y = 0;
1979 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
1980 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
1982 rect->Width = bmp.bmWidth;
1983 rect->Height = bmp.bmHeight;
1985 else
1987 /* FIXME: ??? */
1988 rect->Width = 1;
1989 rect->Height = 1;
1991 }else{
1992 rect->X = 0;
1993 rect->Y = 0;
1994 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1995 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1998 return stat;
2001 /* on success, rgn will contain the region of the graphics object which
2002 * is visible after clipping has been applied */
2003 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2005 GpStatus stat;
2006 GpRectF rectf;
2007 GpRegion* tmp;
2009 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2010 return stat;
2012 if((stat = GdipCreateRegion(&tmp)) != Ok)
2013 return stat;
2015 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2016 goto end;
2018 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2019 goto end;
2021 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2023 end:
2024 GdipDeleteRegion(tmp);
2025 return stat;
2028 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2030 REAL height;
2032 if (font->unit == UnitPixel)
2034 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2036 else
2038 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2039 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2040 else
2041 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2044 lf->lfHeight = -(height + 0.5);
2045 lf->lfWidth = 0;
2046 lf->lfEscapement = 0;
2047 lf->lfOrientation = 0;
2048 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2049 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2050 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2051 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2052 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2053 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2054 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2055 lf->lfQuality = DEFAULT_QUALITY;
2056 lf->lfPitchAndFamily = 0;
2057 strcpyW(lf->lfFaceName, font->family->FamilyName);
2060 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2061 GDIPCONST GpStringFormat *format, HFONT *hfont,
2062 GDIPCONST GpMatrix *matrix)
2064 HDC hdc = CreateCompatibleDC(0);
2065 GpPointF pt[3];
2066 REAL angle, rel_width, rel_height, font_height;
2067 LOGFONTW lfw;
2068 HFONT unscaled_font;
2069 TEXTMETRICW textmet;
2071 if (font->unit == UnitPixel)
2072 font_height = font->emSize;
2073 else
2075 REAL unit_scale, res;
2077 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2078 unit_scale = units_scale(font->unit, graphics->unit, res);
2080 font_height = font->emSize * unit_scale;
2083 pt[0].X = 0.0;
2084 pt[0].Y = 0.0;
2085 pt[1].X = 1.0;
2086 pt[1].Y = 0.0;
2087 pt[2].X = 0.0;
2088 pt[2].Y = 1.0;
2089 if (matrix)
2091 GpMatrix xform = *matrix;
2092 GdipTransformMatrixPoints(&xform, pt, 3);
2094 if (graphics)
2095 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2096 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2097 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2098 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2099 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2100 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2102 get_log_fontW(font, graphics, &lfw);
2103 lfw.lfHeight = -gdip_round(font_height * rel_height);
2104 unscaled_font = CreateFontIndirectW(&lfw);
2106 SelectObject(hdc, unscaled_font);
2107 GetTextMetricsW(hdc, &textmet);
2109 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2110 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2112 *hfont = CreateFontIndirectW(&lfw);
2114 DeleteDC(hdc);
2115 DeleteObject(unscaled_font);
2118 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2120 TRACE("(%p, %p)\n", hdc, graphics);
2122 return GdipCreateFromHDC2(hdc, NULL, graphics);
2125 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2127 GpStatus retval;
2128 HBITMAP hbitmap;
2129 DIBSECTION dib;
2131 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2133 if(hDevice != NULL)
2134 FIXME("Don't know how to handle parameter hDevice\n");
2136 if(hdc == NULL)
2137 return OutOfMemory;
2139 if(graphics == NULL)
2140 return InvalidParameter;
2142 *graphics = GdipAlloc(sizeof(GpGraphics));
2143 if(!*graphics) return OutOfMemory;
2145 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2147 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2148 GdipFree(*graphics);
2149 return retval;
2152 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2153 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2154 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2156 (*graphics)->alpha_hdc = 1;
2159 (*graphics)->hdc = hdc;
2160 (*graphics)->hwnd = WindowFromDC(hdc);
2161 (*graphics)->owndc = FALSE;
2162 (*graphics)->smoothing = SmoothingModeDefault;
2163 (*graphics)->compqual = CompositingQualityDefault;
2164 (*graphics)->interpolation = InterpolationModeBilinear;
2165 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2166 (*graphics)->compmode = CompositingModeSourceOver;
2167 (*graphics)->unit = UnitDisplay;
2168 (*graphics)->scale = 1.0;
2169 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2170 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2171 (*graphics)->busy = FALSE;
2172 (*graphics)->textcontrast = 4;
2173 list_init(&(*graphics)->containers);
2174 (*graphics)->contid = 0;
2176 TRACE("<-- %p\n", *graphics);
2178 return Ok;
2181 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2183 GpStatus retval;
2185 *graphics = GdipAlloc(sizeof(GpGraphics));
2186 if(!*graphics) return OutOfMemory;
2188 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2190 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2191 GdipFree(*graphics);
2192 return retval;
2195 (*graphics)->hdc = NULL;
2196 (*graphics)->hwnd = NULL;
2197 (*graphics)->owndc = FALSE;
2198 (*graphics)->image = image;
2199 /* We have to store the image type here because the image may be freed
2200 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2201 (*graphics)->image_type = image->type;
2202 (*graphics)->smoothing = SmoothingModeDefault;
2203 (*graphics)->compqual = CompositingQualityDefault;
2204 (*graphics)->interpolation = InterpolationModeBilinear;
2205 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2206 (*graphics)->compmode = CompositingModeSourceOver;
2207 (*graphics)->unit = UnitDisplay;
2208 (*graphics)->scale = 1.0;
2209 (*graphics)->xres = image->xres;
2210 (*graphics)->yres = image->yres;
2211 (*graphics)->busy = FALSE;
2212 (*graphics)->textcontrast = 4;
2213 list_init(&(*graphics)->containers);
2214 (*graphics)->contid = 0;
2216 TRACE("<-- %p\n", *graphics);
2218 return Ok;
2221 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2223 GpStatus ret;
2224 HDC hdc;
2226 TRACE("(%p, %p)\n", hwnd, graphics);
2228 hdc = GetDC(hwnd);
2230 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2232 ReleaseDC(hwnd, hdc);
2233 return ret;
2236 (*graphics)->hwnd = hwnd;
2237 (*graphics)->owndc = TRUE;
2239 return Ok;
2242 /* FIXME: no icm handling */
2243 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2245 TRACE("(%p, %p)\n", hwnd, graphics);
2247 return GdipCreateFromHWND(hwnd, graphics);
2250 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2251 UINT access, IStream **stream)
2253 DWORD dwMode;
2254 HRESULT ret;
2256 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2258 if(!stream || !filename)
2259 return InvalidParameter;
2261 if(access & GENERIC_WRITE)
2262 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2263 else if(access & GENERIC_READ)
2264 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2265 else
2266 return InvalidParameter;
2268 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2270 return hresult_to_status(ret);
2273 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2275 GraphicsContainerItem *cont, *next;
2276 GpStatus stat;
2277 TRACE("(%p)\n", graphics);
2279 if(!graphics) return InvalidParameter;
2280 if(graphics->busy) return ObjectBusy;
2282 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2284 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2285 if (stat != Ok)
2286 return stat;
2289 if(graphics->owndc)
2290 ReleaseDC(graphics->hwnd, graphics->hdc);
2292 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2293 list_remove(&cont->entry);
2294 delete_container(cont);
2297 GdipDeleteRegion(graphics->clip);
2299 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2300 * do otherwise, but we can't have that in the test suite because it means
2301 * accessing freed memory. */
2302 graphics->busy = TRUE;
2304 GdipFree(graphics);
2306 return Ok;
2309 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2310 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2312 GpStatus status;
2313 GpPath *path;
2315 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2316 width, height, startAngle, sweepAngle);
2318 if(!graphics || !pen || width <= 0 || height <= 0)
2319 return InvalidParameter;
2321 if(graphics->busy)
2322 return ObjectBusy;
2324 status = GdipCreatePath(FillModeAlternate, &path);
2325 if (status != Ok) return status;
2327 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2328 if (status == Ok)
2329 status = GdipDrawPath(graphics, pen, path);
2331 GdipDeletePath(path);
2332 return status;
2335 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2336 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2338 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2339 width, height, startAngle, sweepAngle);
2341 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2344 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2345 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2347 GpPointF pt[4];
2349 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2350 x2, y2, x3, y3, x4, y4);
2352 if(!graphics || !pen)
2353 return InvalidParameter;
2355 if(graphics->busy)
2356 return ObjectBusy;
2358 pt[0].X = x1;
2359 pt[0].Y = y1;
2360 pt[1].X = x2;
2361 pt[1].Y = y2;
2362 pt[2].X = x3;
2363 pt[2].Y = y3;
2364 pt[3].X = x4;
2365 pt[3].Y = y4;
2366 return GdipDrawBeziers(graphics, pen, pt, 4);
2369 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2370 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2372 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2373 x2, y2, x3, y3, x4, y4);
2375 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2378 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2379 GDIPCONST GpPointF *points, INT count)
2381 GpStatus status;
2382 GpPath *path;
2384 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2386 if(!graphics || !pen || !points || (count <= 0))
2387 return InvalidParameter;
2389 if(graphics->busy)
2390 return ObjectBusy;
2392 status = GdipCreatePath(FillModeAlternate, &path);
2393 if (status != Ok) return status;
2395 status = GdipAddPathBeziers(path, points, count);
2396 if (status == Ok)
2397 status = GdipDrawPath(graphics, pen, path);
2399 GdipDeletePath(path);
2400 return status;
2403 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2404 GDIPCONST GpPoint *points, INT count)
2406 GpPointF *pts;
2407 GpStatus ret;
2408 INT i;
2410 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2412 if(!graphics || !pen || !points || (count <= 0))
2413 return InvalidParameter;
2415 if(graphics->busy)
2416 return ObjectBusy;
2418 pts = GdipAlloc(sizeof(GpPointF) * count);
2419 if(!pts)
2420 return OutOfMemory;
2422 for(i = 0; i < count; i++){
2423 pts[i].X = (REAL)points[i].X;
2424 pts[i].Y = (REAL)points[i].Y;
2427 ret = GdipDrawBeziers(graphics,pen,pts,count);
2429 GdipFree(pts);
2431 return ret;
2434 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2435 GDIPCONST GpPointF *points, INT count)
2437 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2439 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2442 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2443 GDIPCONST GpPoint *points, INT count)
2445 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2447 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2450 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2451 GDIPCONST GpPointF *points, INT count, REAL tension)
2453 GpPath *path;
2454 GpStatus status;
2456 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2458 if(!graphics || !pen || !points || count <= 0)
2459 return InvalidParameter;
2461 if(graphics->busy)
2462 return ObjectBusy;
2464 status = GdipCreatePath(FillModeAlternate, &path);
2465 if (status != Ok) return status;
2467 status = GdipAddPathClosedCurve2(path, points, count, tension);
2468 if (status == Ok)
2469 status = GdipDrawPath(graphics, pen, path);
2471 GdipDeletePath(path);
2473 return status;
2476 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2477 GDIPCONST GpPoint *points, INT count, REAL tension)
2479 GpPointF *ptf;
2480 GpStatus stat;
2481 INT i;
2483 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2485 if(!points || count <= 0)
2486 return InvalidParameter;
2488 ptf = GdipAlloc(sizeof(GpPointF)*count);
2489 if(!ptf)
2490 return OutOfMemory;
2492 for(i = 0; i < count; i++){
2493 ptf[i].X = (REAL)points[i].X;
2494 ptf[i].Y = (REAL)points[i].Y;
2497 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2499 GdipFree(ptf);
2501 return stat;
2504 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2505 GDIPCONST GpPointF *points, INT count)
2507 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2509 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2512 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2513 GDIPCONST GpPoint *points, INT count)
2515 GpPointF *pointsF;
2516 GpStatus ret;
2517 INT i;
2519 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2521 if(!points)
2522 return InvalidParameter;
2524 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2525 if(!pointsF)
2526 return OutOfMemory;
2528 for(i = 0; i < count; i++){
2529 pointsF[i].X = (REAL)points[i].X;
2530 pointsF[i].Y = (REAL)points[i].Y;
2533 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2534 GdipFree(pointsF);
2536 return ret;
2539 /* Approximates cardinal spline with Bezier curves. */
2540 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2541 GDIPCONST GpPointF *points, INT count, REAL tension)
2543 GpPath *path;
2544 GpStatus status;
2546 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2548 if(!graphics || !pen)
2549 return InvalidParameter;
2551 if(graphics->busy)
2552 return ObjectBusy;
2554 if(count < 2)
2555 return InvalidParameter;
2557 status = GdipCreatePath(FillModeAlternate, &path);
2558 if (status != Ok) return status;
2560 status = GdipAddPathCurve2(path, points, count, tension);
2561 if (status == Ok)
2562 status = GdipDrawPath(graphics, pen, path);
2564 GdipDeletePath(path);
2565 return status;
2568 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2569 GDIPCONST GpPoint *points, INT count, REAL tension)
2571 GpPointF *pointsF;
2572 GpStatus ret;
2573 INT i;
2575 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2577 if(!points)
2578 return InvalidParameter;
2580 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2581 if(!pointsF)
2582 return OutOfMemory;
2584 for(i = 0; i < count; i++){
2585 pointsF[i].X = (REAL)points[i].X;
2586 pointsF[i].Y = (REAL)points[i].Y;
2589 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2590 GdipFree(pointsF);
2592 return ret;
2595 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2596 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2597 REAL tension)
2599 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2601 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2602 return InvalidParameter;
2605 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2608 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2609 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2610 REAL tension)
2612 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2614 if(count < 0){
2615 return OutOfMemory;
2618 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2619 return InvalidParameter;
2622 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2625 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2626 REAL y, REAL width, REAL height)
2628 GpPath *path;
2629 GpStatus status;
2631 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2633 if(!graphics || !pen)
2634 return InvalidParameter;
2636 if(graphics->busy)
2637 return ObjectBusy;
2639 status = GdipCreatePath(FillModeAlternate, &path);
2640 if (status != Ok) return status;
2642 status = GdipAddPathEllipse(path, x, y, width, height);
2643 if (status == Ok)
2644 status = GdipDrawPath(graphics, pen, path);
2646 GdipDeletePath(path);
2647 return status;
2650 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2651 INT y, INT width, INT height)
2653 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2655 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2659 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2661 UINT width, height;
2663 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2665 if(!graphics || !image)
2666 return InvalidParameter;
2668 GdipGetImageWidth(image, &width);
2669 GdipGetImageHeight(image, &height);
2671 return GdipDrawImagePointRect(graphics, image, x, y,
2672 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2675 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2676 INT y)
2678 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2680 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2683 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2684 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2685 GpUnit srcUnit)
2687 GpPointF points[3];
2688 REAL scale_x, scale_y, width, height;
2690 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2692 if (!graphics || !image) return InvalidParameter;
2694 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2695 scale_x *= graphics->xres / image->xres;
2696 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2697 scale_y *= graphics->yres / image->yres;
2698 width = srcwidth * scale_x;
2699 height = srcheight * scale_y;
2701 points[0].X = points[2].X = x;
2702 points[0].Y = points[1].Y = y;
2703 points[1].X = x + width;
2704 points[2].Y = y + height;
2706 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2707 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2710 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2711 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2712 GpUnit srcUnit)
2714 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2717 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2718 GDIPCONST GpPointF *dstpoints, INT count)
2720 UINT width, height;
2722 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2724 if(!image)
2725 return InvalidParameter;
2727 GdipGetImageWidth(image, &width);
2728 GdipGetImageHeight(image, &height);
2730 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2731 width, height, UnitPixel, NULL, NULL, NULL);
2734 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2735 GDIPCONST GpPoint *dstpoints, INT count)
2737 GpPointF ptf[3];
2739 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2741 if (count != 3 || !dstpoints)
2742 return InvalidParameter;
2744 ptf[0].X = (REAL)dstpoints[0].X;
2745 ptf[0].Y = (REAL)dstpoints[0].Y;
2746 ptf[1].X = (REAL)dstpoints[1].X;
2747 ptf[1].Y = (REAL)dstpoints[1].Y;
2748 ptf[2].X = (REAL)dstpoints[2].X;
2749 ptf[2].Y = (REAL)dstpoints[2].Y;
2751 return GdipDrawImagePoints(graphics, image, ptf, count);
2754 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2755 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2757 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2758 return TRUE;
2761 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2762 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2763 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2764 DrawImageAbort callback, VOID * callbackData)
2766 GpPointF ptf[4];
2767 POINT pti[4];
2768 GpStatus stat;
2770 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2771 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2772 callbackData);
2774 if (count > 3)
2775 return NotImplemented;
2777 if(!graphics || !image || !points || count != 3)
2778 return InvalidParameter;
2780 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2781 debugstr_pointf(&points[2]));
2783 memcpy(ptf, points, 3 * sizeof(GpPointF));
2785 /* Ensure source width/height is positive */
2786 if (srcwidth < 0)
2788 GpPointF tmp = ptf[1];
2789 srcx = srcx + srcwidth;
2790 srcwidth = -srcwidth;
2791 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2792 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2793 ptf[1] = ptf[0];
2794 ptf[0] = tmp;
2797 if (srcheight < 0)
2799 GpPointF tmp = ptf[2];
2800 srcy = srcy + srcheight;
2801 srcheight = -srcheight;
2802 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2803 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2804 ptf[2] = ptf[0];
2805 ptf[0] = tmp;
2808 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2809 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2810 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2811 return Ok;
2812 transform_and_round_points(graphics, pti, ptf, 4);
2814 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2815 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2817 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2818 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2819 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2820 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2821 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2823 if (image->picture)
2825 if (!graphics->hdc)
2827 FIXME("graphics object has no HDC\n");
2830 if(IPicture_Render(image->picture, graphics->hdc,
2831 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2832 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
2834 if(callback)
2835 callback(callbackData);
2836 return GenericError;
2839 else if (image->type == ImageTypeBitmap)
2841 GpBitmap* bitmap = (GpBitmap*)image;
2842 BOOL use_software = FALSE;
2844 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2845 graphics->xres, graphics->yres,
2846 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2847 graphics->scale, image->xres, image->yres, bitmap->format,
2848 imageAttributes ? imageAttributes->outside_color : 0);
2850 if (imageAttributes || graphics->alpha_hdc ||
2851 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2852 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2853 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2854 srcx < 0 || srcy < 0 ||
2855 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2856 use_software = TRUE;
2858 if (use_software)
2860 RECT dst_area;
2861 GpRectF graphics_bounds;
2862 GpRect src_area;
2863 int i, x, y, src_stride, dst_stride;
2864 GpMatrix dst_to_src;
2865 REAL m11, m12, m21, m22, mdx, mdy;
2866 LPBYTE src_data, dst_data;
2867 BitmapData lockeddata;
2868 InterpolationMode interpolation = graphics->interpolation;
2869 PixelOffsetMode offset_mode = graphics->pixeloffset;
2870 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2871 REAL x_dx, x_dy, y_dx, y_dy;
2872 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2874 if (!imageAttributes)
2875 imageAttributes = &defaultImageAttributes;
2877 dst_area.left = dst_area.right = pti[0].x;
2878 dst_area.top = dst_area.bottom = pti[0].y;
2879 for (i=1; i<4; i++)
2881 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2882 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2883 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2884 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2887 stat = get_graphics_bounds(graphics, &graphics_bounds);
2888 if (stat != Ok) return stat;
2890 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
2891 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
2892 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
2893 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
2895 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2897 if (IsRectEmpty(&dst_area)) return Ok;
2899 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2900 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2901 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2902 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2903 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2904 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2906 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2908 stat = GdipInvertMatrix(&dst_to_src);
2909 if (stat != Ok) return stat;
2911 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2912 if (!dst_data) return OutOfMemory;
2914 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2916 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2917 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2919 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
2921 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2922 if (!src_data)
2924 GdipFree(dst_data);
2925 return OutOfMemory;
2927 src_stride = sizeof(ARGB) * src_area.Width;
2929 /* Read the bits we need from the source bitmap into an ARGB buffer. */
2930 lockeddata.Width = src_area.Width;
2931 lockeddata.Height = src_area.Height;
2932 lockeddata.Stride = src_stride;
2933 lockeddata.PixelFormat = PixelFormat32bppARGB;
2934 lockeddata.Scan0 = src_data;
2936 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
2937 PixelFormat32bppARGB, &lockeddata);
2939 if (stat == Ok)
2940 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2942 if (stat != Ok)
2944 GdipFree(src_data);
2945 GdipFree(dst_data);
2946 return stat;
2949 apply_image_attributes(imageAttributes, src_data,
2950 src_area.Width, src_area.Height,
2951 src_stride, ColorAdjustTypeBitmap);
2953 /* Transform the bits as needed to the destination. */
2954 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
2956 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
2957 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
2958 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
2959 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
2961 for (x=dst_area.left; x<dst_area.right; x++)
2963 for (y=dst_area.top; y<dst_area.bottom; y++)
2965 GpPointF src_pointf;
2966 ARGB *dst_color;
2968 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
2969 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
2971 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2973 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
2974 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
2975 imageAttributes, interpolation, offset_mode);
2976 else
2977 *dst_color = 0;
2981 GdipFree(src_data);
2983 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
2984 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
2986 GdipFree(dst_data);
2988 return stat;
2990 else
2992 HDC hdc;
2993 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
2994 HBITMAP hbitmap, old_hbm=NULL;
2996 if (!(bitmap->format == PixelFormat16bppRGB555 ||
2997 bitmap->format == PixelFormat24bppRGB ||
2998 bitmap->format == PixelFormat32bppRGB ||
2999 bitmap->format == PixelFormat32bppPARGB))
3001 BITMAPINFOHEADER bih;
3002 BYTE *temp_bits;
3003 PixelFormat dst_format;
3005 /* we can't draw a bitmap of this format directly */
3006 hdc = CreateCompatibleDC(0);
3007 temp_hdc = TRUE;
3008 temp_bitmap = TRUE;
3010 bih.biSize = sizeof(BITMAPINFOHEADER);
3011 bih.biWidth = bitmap->width;
3012 bih.biHeight = -bitmap->height;
3013 bih.biPlanes = 1;
3014 bih.biBitCount = 32;
3015 bih.biCompression = BI_RGB;
3016 bih.biSizeImage = 0;
3017 bih.biXPelsPerMeter = 0;
3018 bih.biYPelsPerMeter = 0;
3019 bih.biClrUsed = 0;
3020 bih.biClrImportant = 0;
3022 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3023 (void**)&temp_bits, NULL, 0);
3025 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3026 dst_format = PixelFormat32bppPARGB;
3027 else
3028 dst_format = PixelFormat32bppRGB;
3030 convert_pixels(bitmap->width, bitmap->height,
3031 bitmap->width*4, temp_bits, dst_format,
3032 bitmap->stride, bitmap->bits, bitmap->format,
3033 bitmap->image.palette);
3035 else
3037 if (bitmap->hbitmap)
3038 hbitmap = bitmap->hbitmap;
3039 else
3041 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3042 temp_bitmap = TRUE;
3045 hdc = bitmap->hdc;
3046 temp_hdc = (hdc == 0);
3049 if (temp_hdc)
3051 if (!hdc) hdc = CreateCompatibleDC(0);
3052 old_hbm = SelectObject(hdc, hbitmap);
3055 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3057 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3058 hdc, srcx, srcy, srcwidth, srcheight);
3060 else
3062 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3063 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3066 if (temp_hdc)
3068 SelectObject(hdc, old_hbm);
3069 DeleteDC(hdc);
3072 if (temp_bitmap)
3073 DeleteObject(hbitmap);
3076 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3078 GpRectF rc;
3080 rc.X = srcx;
3081 rc.Y = srcy;
3082 rc.Width = srcwidth;
3083 rc.Height = srcheight;
3085 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3086 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3088 else
3090 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3091 return InvalidParameter;
3094 return Ok;
3097 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3098 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3099 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3100 DrawImageAbort callback, VOID * callbackData)
3102 GpPointF pointsF[3];
3103 INT i;
3105 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3106 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3107 callbackData);
3109 if(!points || count!=3)
3110 return InvalidParameter;
3112 for(i = 0; i < count; i++){
3113 pointsF[i].X = (REAL)points[i].X;
3114 pointsF[i].Y = (REAL)points[i].Y;
3117 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3118 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3119 callback, callbackData);
3122 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3123 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3124 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3125 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3126 VOID * callbackData)
3128 GpPointF points[3];
3130 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3131 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3132 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3134 points[0].X = dstx;
3135 points[0].Y = dsty;
3136 points[1].X = dstx + dstwidth;
3137 points[1].Y = dsty;
3138 points[2].X = dstx;
3139 points[2].Y = dsty + dstheight;
3141 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3142 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3145 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3146 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3147 INT srcwidth, INT srcheight, GpUnit srcUnit,
3148 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3149 VOID * callbackData)
3151 GpPointF points[3];
3153 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3154 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3155 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3157 points[0].X = dstx;
3158 points[0].Y = dsty;
3159 points[1].X = dstx + dstwidth;
3160 points[1].Y = dsty;
3161 points[2].X = dstx;
3162 points[2].Y = dsty + dstheight;
3164 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3165 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3168 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3169 REAL x, REAL y, REAL width, REAL height)
3171 RectF bounds;
3172 GpUnit unit;
3173 GpStatus ret;
3175 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3177 if(!graphics || !image)
3178 return InvalidParameter;
3180 ret = GdipGetImageBounds(image, &bounds, &unit);
3181 if(ret != Ok)
3182 return ret;
3184 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3185 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3186 unit, NULL, NULL, NULL);
3189 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3190 INT x, INT y, INT width, INT height)
3192 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3194 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3197 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3198 REAL y1, REAL x2, REAL y2)
3200 GpPointF pt[2];
3202 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3204 pt[0].X = x1;
3205 pt[0].Y = y1;
3206 pt[1].X = x2;
3207 pt[1].Y = y2;
3208 return GdipDrawLines(graphics, pen, pt, 2);
3211 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3212 INT y1, INT x2, INT y2)
3214 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3216 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3219 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3220 GpPointF *points, INT count)
3222 GpStatus status;
3223 GpPath *path;
3225 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3227 if(!pen || !graphics || (count < 2))
3228 return InvalidParameter;
3230 if(graphics->busy)
3231 return ObjectBusy;
3233 status = GdipCreatePath(FillModeAlternate, &path);
3234 if (status != Ok) return status;
3236 status = GdipAddPathLine2(path, points, count);
3237 if (status == Ok)
3238 status = GdipDrawPath(graphics, pen, path);
3240 GdipDeletePath(path);
3241 return status;
3244 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3245 GpPoint *points, INT count)
3247 GpStatus retval;
3248 GpPointF *ptf;
3249 int i;
3251 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3253 ptf = GdipAlloc(count * sizeof(GpPointF));
3254 if(!ptf) return OutOfMemory;
3256 for(i = 0; i < count; i ++){
3257 ptf[i].X = (REAL) points[i].X;
3258 ptf[i].Y = (REAL) points[i].Y;
3261 retval = GdipDrawLines(graphics, pen, ptf, count);
3263 GdipFree(ptf);
3264 return retval;
3267 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3269 INT save_state;
3270 GpStatus retval;
3271 HRGN hrgn=NULL;
3273 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3275 if(!pen || !graphics)
3276 return InvalidParameter;
3278 if(graphics->busy)
3279 return ObjectBusy;
3281 if (!graphics->hdc)
3283 FIXME("graphics object has no HDC\n");
3284 return Ok;
3287 save_state = prepare_dc(graphics, pen);
3289 retval = get_clip_hrgn(graphics, &hrgn);
3291 if (retval != Ok)
3292 goto end;
3294 if (hrgn)
3295 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3297 retval = draw_poly(graphics, pen, path->pathdata.Points,
3298 path->pathdata.Types, path->pathdata.Count, TRUE);
3300 end:
3301 restore_dc(graphics, save_state);
3302 DeleteObject(hrgn);
3304 return retval;
3307 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3308 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3310 GpStatus status;
3311 GpPath *path;
3313 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3314 width, height, startAngle, sweepAngle);
3316 if(!graphics || !pen)
3317 return InvalidParameter;
3319 if(graphics->busy)
3320 return ObjectBusy;
3322 status = GdipCreatePath(FillModeAlternate, &path);
3323 if (status != Ok) return status;
3325 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3326 if (status == Ok)
3327 status = GdipDrawPath(graphics, pen, path);
3329 GdipDeletePath(path);
3330 return status;
3333 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3334 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3336 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3337 width, height, startAngle, sweepAngle);
3339 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3342 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3343 REAL y, REAL width, REAL height)
3345 GpStatus status;
3346 GpPath *path;
3348 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3350 if(!pen || !graphics)
3351 return InvalidParameter;
3353 if(graphics->busy)
3354 return ObjectBusy;
3356 status = GdipCreatePath(FillModeAlternate, &path);
3357 if (status != Ok) return status;
3359 status = GdipAddPathRectangle(path, x, y, width, height);
3360 if (status == Ok)
3361 status = GdipDrawPath(graphics, pen, path);
3363 GdipDeletePath(path);
3364 return status;
3367 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3368 INT y, INT width, INT height)
3370 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3372 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3375 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3376 GDIPCONST GpRectF* rects, INT count)
3378 GpStatus status;
3379 GpPath *path;
3381 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3383 if(!graphics || !pen || !rects || count < 1)
3384 return InvalidParameter;
3386 if(graphics->busy)
3387 return ObjectBusy;
3389 status = GdipCreatePath(FillModeAlternate, &path);
3390 if (status != Ok) return status;
3392 status = GdipAddPathRectangles(path, rects, count);
3393 if (status == Ok)
3394 status = GdipDrawPath(graphics, pen, path);
3396 GdipDeletePath(path);
3397 return status;
3400 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3401 GDIPCONST GpRect* rects, INT count)
3403 GpRectF *rectsF;
3404 GpStatus ret;
3405 INT i;
3407 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3409 if(!rects || count<=0)
3410 return InvalidParameter;
3412 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3413 if(!rectsF)
3414 return OutOfMemory;
3416 for(i = 0;i < count;i++){
3417 rectsF[i].X = (REAL)rects[i].X;
3418 rectsF[i].Y = (REAL)rects[i].Y;
3419 rectsF[i].Width = (REAL)rects[i].Width;
3420 rectsF[i].Height = (REAL)rects[i].Height;
3423 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3424 GdipFree(rectsF);
3426 return ret;
3429 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3430 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3432 GpPath *path;
3433 GpStatus status;
3435 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3436 count, tension, fill);
3438 if(!graphics || !brush || !points)
3439 return InvalidParameter;
3441 if(graphics->busy)
3442 return ObjectBusy;
3444 if(count == 1) /* Do nothing */
3445 return Ok;
3447 status = GdipCreatePath(fill, &path);
3448 if (status != Ok) return status;
3450 status = GdipAddPathClosedCurve2(path, points, count, tension);
3451 if (status == Ok)
3452 status = GdipFillPath(graphics, brush, path);
3454 GdipDeletePath(path);
3455 return status;
3458 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3459 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3461 GpPointF *ptf;
3462 GpStatus stat;
3463 INT i;
3465 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3466 count, tension, fill);
3468 if(!points || count == 0)
3469 return InvalidParameter;
3471 if(count == 1) /* Do nothing */
3472 return Ok;
3474 ptf = GdipAlloc(sizeof(GpPointF)*count);
3475 if(!ptf)
3476 return OutOfMemory;
3478 for(i = 0;i < count;i++){
3479 ptf[i].X = (REAL)points[i].X;
3480 ptf[i].Y = (REAL)points[i].Y;
3483 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3485 GdipFree(ptf);
3487 return stat;
3490 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3491 GDIPCONST GpPointF *points, INT count)
3493 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3494 return GdipFillClosedCurve2(graphics, brush, points, count,
3495 0.5f, FillModeAlternate);
3498 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3499 GDIPCONST GpPoint *points, INT count)
3501 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3502 return GdipFillClosedCurve2I(graphics, brush, points, count,
3503 0.5f, FillModeAlternate);
3506 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3507 REAL y, REAL width, REAL height)
3509 GpStatus stat;
3510 GpPath *path;
3512 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3514 if(!graphics || !brush)
3515 return InvalidParameter;
3517 if(graphics->busy)
3518 return ObjectBusy;
3520 stat = GdipCreatePath(FillModeAlternate, &path);
3522 if (stat == Ok)
3524 stat = GdipAddPathEllipse(path, x, y, width, height);
3526 if (stat == Ok)
3527 stat = GdipFillPath(graphics, brush, path);
3529 GdipDeletePath(path);
3532 return stat;
3535 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3536 INT y, INT width, INT height)
3538 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3540 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3543 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3545 INT save_state;
3546 GpStatus retval;
3547 HRGN hrgn=NULL;
3549 if(!graphics->hdc || !brush_can_fill_path(brush))
3550 return NotImplemented;
3552 save_state = SaveDC(graphics->hdc);
3553 EndPath(graphics->hdc);
3554 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3555 : WINDING));
3557 retval = get_clip_hrgn(graphics, &hrgn);
3559 if (retval != Ok)
3560 goto end;
3562 if (hrgn)
3563 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3565 BeginPath(graphics->hdc);
3566 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3567 path->pathdata.Types, path->pathdata.Count, FALSE);
3569 if(retval != Ok)
3570 goto end;
3572 EndPath(graphics->hdc);
3573 brush_fill_path(graphics, brush);
3575 retval = Ok;
3577 end:
3578 RestoreDC(graphics->hdc, save_state);
3579 DeleteObject(hrgn);
3581 return retval;
3584 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3586 GpStatus stat;
3587 GpRegion *rgn;
3589 if (!brush_can_fill_pixels(brush))
3590 return NotImplemented;
3592 /* FIXME: This could probably be done more efficiently without regions. */
3594 stat = GdipCreateRegionPath(path, &rgn);
3596 if (stat == Ok)
3598 stat = GdipFillRegion(graphics, brush, rgn);
3600 GdipDeleteRegion(rgn);
3603 return stat;
3606 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3608 GpStatus stat = NotImplemented;
3610 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3612 if(!brush || !graphics || !path)
3613 return InvalidParameter;
3615 if(graphics->busy)
3616 return ObjectBusy;
3618 if (!graphics->image && !graphics->alpha_hdc)
3619 stat = GDI32_GdipFillPath(graphics, brush, path);
3621 if (stat == NotImplemented)
3622 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3624 if (stat == NotImplemented)
3626 FIXME("Not implemented for brushtype %i\n", brush->bt);
3627 stat = Ok;
3630 return stat;
3633 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3634 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3636 GpStatus stat;
3637 GpPath *path;
3639 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3640 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3642 if(!graphics || !brush)
3643 return InvalidParameter;
3645 if(graphics->busy)
3646 return ObjectBusy;
3648 stat = GdipCreatePath(FillModeAlternate, &path);
3650 if (stat == Ok)
3652 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3654 if (stat == Ok)
3655 stat = GdipFillPath(graphics, brush, path);
3657 GdipDeletePath(path);
3660 return stat;
3663 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3664 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3666 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3667 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3669 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3672 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3673 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3675 GpStatus stat;
3676 GpPath *path;
3678 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3680 if(!graphics || !brush || !points || !count)
3681 return InvalidParameter;
3683 if(graphics->busy)
3684 return ObjectBusy;
3686 stat = GdipCreatePath(fillMode, &path);
3688 if (stat == Ok)
3690 stat = GdipAddPathPolygon(path, points, count);
3692 if (stat == Ok)
3693 stat = GdipFillPath(graphics, brush, path);
3695 GdipDeletePath(path);
3698 return stat;
3701 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3702 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3704 GpStatus stat;
3705 GpPath *path;
3707 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3709 if(!graphics || !brush || !points || !count)
3710 return InvalidParameter;
3712 if(graphics->busy)
3713 return ObjectBusy;
3715 stat = GdipCreatePath(fillMode, &path);
3717 if (stat == Ok)
3719 stat = GdipAddPathPolygonI(path, points, count);
3721 if (stat == Ok)
3722 stat = GdipFillPath(graphics, brush, path);
3724 GdipDeletePath(path);
3727 return stat;
3730 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3731 GDIPCONST GpPointF *points, INT count)
3733 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3735 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3738 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3739 GDIPCONST GpPoint *points, INT count)
3741 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3743 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3746 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3747 REAL x, REAL y, REAL width, REAL height)
3749 GpRectF rect;
3751 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3753 rect.X = x;
3754 rect.Y = y;
3755 rect.Width = width;
3756 rect.Height = height;
3758 return GdipFillRectangles(graphics, brush, &rect, 1);
3761 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3762 INT x, INT y, INT width, INT height)
3764 GpRectF rect;
3766 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3768 rect.X = (REAL)x;
3769 rect.Y = (REAL)y;
3770 rect.Width = (REAL)width;
3771 rect.Height = (REAL)height;
3773 return GdipFillRectangles(graphics, brush, &rect, 1);
3776 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3777 INT count)
3779 GpStatus status;
3780 GpPath *path;
3782 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3784 if(!graphics || !brush || !rects || count <= 0)
3785 return InvalidParameter;
3787 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3789 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
3790 /* FIXME: Add gdi32 drawing. */
3791 return status;
3794 status = GdipCreatePath(FillModeAlternate, &path);
3795 if (status != Ok) return status;
3797 status = GdipAddPathRectangles(path, rects, count);
3798 if (status == Ok)
3799 status = GdipFillPath(graphics, brush, path);
3801 GdipDeletePath(path);
3802 return status;
3805 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3806 INT count)
3808 GpRectF *rectsF;
3809 GpStatus ret;
3810 INT i;
3812 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3814 if(!rects || count <= 0)
3815 return InvalidParameter;
3817 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3818 if(!rectsF)
3819 return OutOfMemory;
3821 for(i = 0; i < count; i++){
3822 rectsF[i].X = (REAL)rects[i].X;
3823 rectsF[i].Y = (REAL)rects[i].Y;
3824 rectsF[i].X = (REAL)rects[i].Width;
3825 rectsF[i].Height = (REAL)rects[i].Height;
3828 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3829 GdipFree(rectsF);
3831 return ret;
3834 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3835 GpRegion* region)
3837 INT save_state;
3838 GpStatus status;
3839 HRGN hrgn;
3840 RECT rc;
3842 if(!graphics->hdc || !brush_can_fill_path(brush))
3843 return NotImplemented;
3845 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3846 if(status != Ok)
3847 return status;
3849 save_state = SaveDC(graphics->hdc);
3850 EndPath(graphics->hdc);
3852 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3854 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3856 BeginPath(graphics->hdc);
3857 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3858 EndPath(graphics->hdc);
3860 brush_fill_path(graphics, brush);
3863 RestoreDC(graphics->hdc, save_state);
3865 DeleteObject(hrgn);
3867 return Ok;
3870 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3871 GpRegion* region)
3873 GpStatus stat;
3874 GpRegion *temp_region;
3875 GpMatrix world_to_device;
3876 GpRectF graphics_bounds;
3877 DWORD *pixel_data;
3878 HRGN hregion;
3879 RECT bound_rect;
3880 GpRect gp_bound_rect;
3882 if (!brush_can_fill_pixels(brush))
3883 return NotImplemented;
3885 stat = get_graphics_bounds(graphics, &graphics_bounds);
3887 if (stat == Ok)
3888 stat = GdipCloneRegion(region, &temp_region);
3890 if (stat == Ok)
3892 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3893 CoordinateSpaceWorld, &world_to_device);
3895 if (stat == Ok)
3896 stat = GdipTransformRegion(temp_region, &world_to_device);
3898 if (stat == Ok)
3899 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3901 if (stat == Ok)
3902 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
3904 GdipDeleteRegion(temp_region);
3907 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
3909 DeleteObject(hregion);
3910 return Ok;
3913 if (stat == Ok)
3915 gp_bound_rect.X = bound_rect.left;
3916 gp_bound_rect.Y = bound_rect.top;
3917 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
3918 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
3920 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
3921 if (!pixel_data)
3922 stat = OutOfMemory;
3924 if (stat == Ok)
3926 stat = brush_fill_pixels(graphics, brush, pixel_data,
3927 &gp_bound_rect, gp_bound_rect.Width);
3929 if (stat == Ok)
3930 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
3931 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
3932 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
3934 GdipFree(pixel_data);
3937 DeleteObject(hregion);
3940 return stat;
3943 /*****************************************************************************
3944 * GdipFillRegion [GDIPLUS.@]
3946 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3947 GpRegion* region)
3949 GpStatus stat = NotImplemented;
3951 TRACE("(%p, %p, %p)\n", graphics, brush, region);
3953 if (!(graphics && brush && region))
3954 return InvalidParameter;
3956 if(graphics->busy)
3957 return ObjectBusy;
3959 if (!graphics->image && !graphics->alpha_hdc)
3960 stat = GDI32_GdipFillRegion(graphics, brush, region);
3962 if (stat == NotImplemented)
3963 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
3965 if (stat == NotImplemented)
3967 FIXME("not implemented for brushtype %i\n", brush->bt);
3968 stat = Ok;
3971 return stat;
3974 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3976 TRACE("(%p,%u)\n", graphics, intention);
3978 if(!graphics)
3979 return InvalidParameter;
3981 if(graphics->busy)
3982 return ObjectBusy;
3984 /* We have no internal operation queue, so there's no need to clear it. */
3986 if (graphics->hdc)
3987 GdiFlush();
3989 return Ok;
3992 /*****************************************************************************
3993 * GdipGetClipBounds [GDIPLUS.@]
3995 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3997 GpStatus status;
3998 GpRegion *clip;
4000 TRACE("(%p, %p)\n", graphics, rect);
4002 if(!graphics)
4003 return InvalidParameter;
4005 if(graphics->busy)
4006 return ObjectBusy;
4008 status = GdipCreateRegion(&clip);
4009 if (status != Ok) return status;
4011 status = GdipGetClip(graphics, clip);
4012 if (status == Ok)
4013 status = GdipGetRegionBounds(clip, graphics, rect);
4015 GdipDeleteRegion(clip);
4016 return status;
4019 /*****************************************************************************
4020 * GdipGetClipBoundsI [GDIPLUS.@]
4022 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4024 TRACE("(%p, %p)\n", graphics, rect);
4026 if(!graphics)
4027 return InvalidParameter;
4029 if(graphics->busy)
4030 return ObjectBusy;
4032 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4035 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4036 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4037 CompositingMode *mode)
4039 TRACE("(%p, %p)\n", graphics, mode);
4041 if(!graphics || !mode)
4042 return InvalidParameter;
4044 if(graphics->busy)
4045 return ObjectBusy;
4047 *mode = graphics->compmode;
4049 return Ok;
4052 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4053 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4054 CompositingQuality *quality)
4056 TRACE("(%p, %p)\n", graphics, quality);
4058 if(!graphics || !quality)
4059 return InvalidParameter;
4061 if(graphics->busy)
4062 return ObjectBusy;
4064 *quality = graphics->compqual;
4066 return Ok;
4069 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4070 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4071 InterpolationMode *mode)
4073 TRACE("(%p, %p)\n", graphics, mode);
4075 if(!graphics || !mode)
4076 return InvalidParameter;
4078 if(graphics->busy)
4079 return ObjectBusy;
4081 *mode = graphics->interpolation;
4083 return Ok;
4086 /* FIXME: Need to handle color depths less than 24bpp */
4087 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4089 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4091 if(!graphics || !argb)
4092 return InvalidParameter;
4094 if(graphics->busy)
4095 return ObjectBusy;
4097 return Ok;
4100 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4102 TRACE("(%p, %p)\n", graphics, scale);
4104 if(!graphics || !scale)
4105 return InvalidParameter;
4107 if(graphics->busy)
4108 return ObjectBusy;
4110 *scale = graphics->scale;
4112 return Ok;
4115 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4117 TRACE("(%p, %p)\n", graphics, unit);
4119 if(!graphics || !unit)
4120 return InvalidParameter;
4122 if(graphics->busy)
4123 return ObjectBusy;
4125 *unit = graphics->unit;
4127 return Ok;
4130 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4131 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4132 *mode)
4134 TRACE("(%p, %p)\n", graphics, mode);
4136 if(!graphics || !mode)
4137 return InvalidParameter;
4139 if(graphics->busy)
4140 return ObjectBusy;
4142 *mode = graphics->pixeloffset;
4144 return Ok;
4147 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4148 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4150 TRACE("(%p, %p)\n", graphics, mode);
4152 if(!graphics || !mode)
4153 return InvalidParameter;
4155 if(graphics->busy)
4156 return ObjectBusy;
4158 *mode = graphics->smoothing;
4160 return Ok;
4163 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4165 TRACE("(%p, %p)\n", graphics, contrast);
4167 if(!graphics || !contrast)
4168 return InvalidParameter;
4170 *contrast = graphics->textcontrast;
4172 return Ok;
4175 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4176 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4177 TextRenderingHint *hint)
4179 TRACE("(%p, %p)\n", graphics, hint);
4181 if(!graphics || !hint)
4182 return InvalidParameter;
4184 if(graphics->busy)
4185 return ObjectBusy;
4187 *hint = graphics->texthint;
4189 return Ok;
4192 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4194 GpRegion *clip_rgn;
4195 GpStatus stat;
4197 TRACE("(%p, %p)\n", graphics, rect);
4199 if(!graphics || !rect)
4200 return InvalidParameter;
4202 if(graphics->busy)
4203 return ObjectBusy;
4205 /* intersect window and graphics clipping regions */
4206 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4207 return stat;
4209 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4210 goto cleanup;
4212 /* get bounds of the region */
4213 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4215 cleanup:
4216 GdipDeleteRegion(clip_rgn);
4218 return stat;
4221 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4223 GpRectF rectf;
4224 GpStatus stat;
4226 TRACE("(%p, %p)\n", graphics, rect);
4228 if(!graphics || !rect)
4229 return InvalidParameter;
4231 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4233 rect->X = gdip_round(rectf.X);
4234 rect->Y = gdip_round(rectf.Y);
4235 rect->Width = gdip_round(rectf.Width);
4236 rect->Height = gdip_round(rectf.Height);
4239 return stat;
4242 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4244 TRACE("(%p, %p)\n", graphics, matrix);
4246 if(!graphics || !matrix)
4247 return InvalidParameter;
4249 if(graphics->busy)
4250 return ObjectBusy;
4252 *matrix = graphics->worldtrans;
4253 return Ok;
4256 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4258 GpSolidFill *brush;
4259 GpStatus stat;
4260 GpRectF wnd_rect;
4262 TRACE("(%p, %x)\n", graphics, color);
4264 if(!graphics)
4265 return InvalidParameter;
4267 if(graphics->busy)
4268 return ObjectBusy;
4270 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4271 return stat;
4273 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4274 GdipDeleteBrush((GpBrush*)brush);
4275 return stat;
4278 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4279 wnd_rect.Width, wnd_rect.Height);
4281 GdipDeleteBrush((GpBrush*)brush);
4283 return Ok;
4286 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4288 TRACE("(%p, %p)\n", graphics, res);
4290 if(!graphics || !res)
4291 return InvalidParameter;
4293 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4296 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4298 GpStatus stat;
4299 GpRegion* rgn;
4300 GpPointF pt;
4302 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4304 if(!graphics || !result)
4305 return InvalidParameter;
4307 if(graphics->busy)
4308 return ObjectBusy;
4310 pt.X = x;
4311 pt.Y = y;
4312 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4313 CoordinateSpaceWorld, &pt, 1)) != Ok)
4314 return stat;
4316 if((stat = GdipCreateRegion(&rgn)) != Ok)
4317 return stat;
4319 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4320 goto cleanup;
4322 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4324 cleanup:
4325 GdipDeleteRegion(rgn);
4326 return stat;
4329 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4331 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4334 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4336 GpStatus stat;
4337 GpRegion* rgn;
4338 GpPointF pts[2];
4340 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4342 if(!graphics || !result)
4343 return InvalidParameter;
4345 if(graphics->busy)
4346 return ObjectBusy;
4348 pts[0].X = x;
4349 pts[0].Y = y;
4350 pts[1].X = x + width;
4351 pts[1].Y = y + height;
4353 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4354 CoordinateSpaceWorld, pts, 2)) != Ok)
4355 return stat;
4357 pts[1].X -= pts[0].X;
4358 pts[1].Y -= pts[0].Y;
4360 if((stat = GdipCreateRegion(&rgn)) != Ok)
4361 return stat;
4363 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4364 goto cleanup;
4366 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4368 cleanup:
4369 GdipDeleteRegion(rgn);
4370 return stat;
4373 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4375 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4378 GpStatus gdip_format_string(HDC hdc,
4379 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4380 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4381 gdip_format_string_callback callback, void *user_data)
4383 WCHAR* stringdup;
4384 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4385 nheight, lineend, lineno = 0;
4386 RectF bounds;
4387 StringAlignment halign;
4388 GpStatus stat = Ok;
4389 SIZE size;
4390 HotkeyPrefix hkprefix;
4391 INT *hotkeyprefix_offsets=NULL;
4392 INT hotkeyprefix_count=0;
4393 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4394 BOOL seen_prefix = FALSE;
4395 GpStringFormat *dyn_format=NULL;
4397 if(length == -1) length = lstrlenW(string);
4399 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4400 if(!stringdup) return OutOfMemory;
4402 if (!format)
4404 stat = GdipStringFormatGetGenericDefault(&dyn_format);
4405 if (stat != Ok)
4407 GdipFree(stringdup);
4408 return stat;
4410 format = dyn_format;
4413 nwidth = rect->Width;
4414 nheight = rect->Height;
4415 if (ignore_empty_clip)
4417 if (!nwidth) nwidth = INT_MAX;
4418 if (!nheight) nheight = INT_MAX;
4421 hkprefix = format->hkprefix;
4423 if (hkprefix == HotkeyPrefixShow)
4425 for (i=0; i<length; i++)
4427 if (string[i] == '&')
4428 hotkeyprefix_count++;
4432 if (hotkeyprefix_count)
4433 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4435 hotkeyprefix_count = 0;
4437 for(i = 0, j = 0; i < length; i++){
4438 /* FIXME: This makes the indexes passed to callback inaccurate. */
4439 if(!isprintW(string[i]) && (string[i] != '\n'))
4440 continue;
4442 /* FIXME: tabs should be handled using tabstops from stringformat */
4443 if (string[i] == '\t')
4444 continue;
4446 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4447 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4448 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4450 seen_prefix = TRUE;
4451 continue;
4454 seen_prefix = FALSE;
4456 stringdup[j] = string[i];
4457 j++;
4460 length = j;
4462 halign = format->align;
4464 while(sum < length){
4465 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4466 nwidth, &fit, NULL, &size);
4467 fitcpy = fit;
4469 if(fit == 0)
4470 break;
4472 for(lret = 0; lret < fit; lret++)
4473 if(*(stringdup + sum + lret) == '\n')
4474 break;
4476 /* Line break code (may look strange, but it imitates windows). */
4477 if(lret < fit)
4478 lineend = fit = lret; /* this is not an off-by-one error */
4479 else if(fit < (length - sum)){
4480 if(*(stringdup + sum + fit) == ' ')
4481 while(*(stringdup + sum + fit) == ' ')
4482 fit++;
4483 else
4484 while(*(stringdup + sum + fit - 1) != ' '){
4485 fit--;
4487 if(*(stringdup + sum + fit) == '\t')
4488 break;
4490 if(fit == 0){
4491 fit = fitcpy;
4492 break;
4495 lineend = fit;
4496 while(*(stringdup + sum + lineend - 1) == ' ' ||
4497 *(stringdup + sum + lineend - 1) == '\t')
4498 lineend--;
4500 else
4501 lineend = fit;
4503 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4504 nwidth, &j, NULL, &size);
4506 bounds.Width = size.cx;
4508 if(height + size.cy > nheight)
4510 if (format->attr & StringFormatFlagsLineLimit)
4511 break;
4512 bounds.Height = nheight - (height + size.cy);
4514 else
4515 bounds.Height = size.cy;
4517 bounds.Y = rect->Y + height;
4519 switch (halign)
4521 case StringAlignmentNear:
4522 default:
4523 bounds.X = rect->X;
4524 break;
4525 case StringAlignmentCenter:
4526 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4527 break;
4528 case StringAlignmentFar:
4529 bounds.X = rect->X + rect->Width - bounds.Width;
4530 break;
4533 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4534 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4535 break;
4537 stat = callback(hdc, stringdup, sum, lineend,
4538 font, rect, format, lineno, &bounds,
4539 &hotkeyprefix_offsets[hotkeyprefix_pos],
4540 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4542 if (stat != Ok)
4543 break;
4545 sum += fit + (lret < fitcpy ? 1 : 0);
4546 height += size.cy;
4547 lineno++;
4549 hotkeyprefix_pos = hotkeyprefix_end_pos;
4551 if(height > nheight)
4552 break;
4554 /* Stop if this was a linewrap (but not if it was a linebreak). */
4555 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
4556 break;
4559 GdipFree(stringdup);
4560 GdipFree(hotkeyprefix_offsets);
4561 GdipDeleteStringFormat(dyn_format);
4563 return stat;
4566 struct measure_ranges_args {
4567 GpRegion **regions;
4568 REAL rel_width, rel_height;
4571 static GpStatus measure_ranges_callback(HDC hdc,
4572 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4573 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4574 INT lineno, const RectF *bounds, INT *underlined_indexes,
4575 INT underlined_index_count, void *user_data)
4577 int i;
4578 GpStatus stat = Ok;
4579 struct measure_ranges_args *args = user_data;
4581 for (i=0; i<format->range_count; i++)
4583 INT range_start = max(index, format->character_ranges[i].First);
4584 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4585 if (range_start < range_end)
4587 GpRectF range_rect;
4588 SIZE range_size;
4590 range_rect.Y = bounds->Y / args->rel_height;
4591 range_rect.Height = bounds->Height / args->rel_height;
4593 GetTextExtentExPointW(hdc, string + index, range_start - index,
4594 INT_MAX, NULL, NULL, &range_size);
4595 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4597 GetTextExtentExPointW(hdc, string + index, range_end - index,
4598 INT_MAX, NULL, NULL, &range_size);
4599 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4601 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4602 if (stat != Ok)
4603 break;
4607 return stat;
4610 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4611 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4612 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4613 INT regionCount, GpRegion** regions)
4615 GpStatus stat;
4616 int i;
4617 HFONT gdifont, oldfont;
4618 struct measure_ranges_args args;
4619 HDC hdc, temp_hdc=NULL;
4620 GpPointF pt[3];
4621 RectF scaled_rect;
4622 REAL margin_x;
4624 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4625 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4627 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4628 return InvalidParameter;
4630 if (regionCount < stringFormat->range_count)
4631 return InvalidParameter;
4633 if(!graphics->hdc)
4635 hdc = temp_hdc = CreateCompatibleDC(0);
4636 if (!temp_hdc) return OutOfMemory;
4638 else
4639 hdc = graphics->hdc;
4641 if (stringFormat->attr)
4642 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4644 pt[0].X = 0.0;
4645 pt[0].Y = 0.0;
4646 pt[1].X = 1.0;
4647 pt[1].Y = 0.0;
4648 pt[2].X = 0.0;
4649 pt[2].Y = 1.0;
4650 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4651 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4652 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4653 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4654 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4656 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4657 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4659 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4660 scaled_rect.Y = layoutRect->Y * args.rel_height;
4661 scaled_rect.Width = layoutRect->Width * args.rel_width;
4662 scaled_rect.Height = layoutRect->Height * args.rel_height;
4664 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4665 oldfont = SelectObject(hdc, gdifont);
4667 for (i=0; i<stringFormat->range_count; i++)
4669 stat = GdipSetEmpty(regions[i]);
4670 if (stat != Ok)
4671 return stat;
4674 args.regions = regions;
4676 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4677 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4679 SelectObject(hdc, oldfont);
4680 DeleteObject(gdifont);
4682 if (temp_hdc)
4683 DeleteDC(temp_hdc);
4685 return stat;
4688 struct measure_string_args {
4689 RectF *bounds;
4690 INT *codepointsfitted;
4691 INT *linesfilled;
4692 REAL rel_width, rel_height;
4695 static GpStatus measure_string_callback(HDC hdc,
4696 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4697 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4698 INT lineno, const RectF *bounds, INT *underlined_indexes,
4699 INT underlined_index_count, void *user_data)
4701 struct measure_string_args *args = user_data;
4702 REAL new_width, new_height;
4704 new_width = bounds->Width / args->rel_width;
4705 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4707 if (new_width > args->bounds->Width)
4708 args->bounds->Width = new_width;
4710 if (new_height > args->bounds->Height)
4711 args->bounds->Height = new_height;
4713 if (args->codepointsfitted)
4714 *args->codepointsfitted = index + length;
4716 if (args->linesfilled)
4717 (*args->linesfilled)++;
4719 return Ok;
4722 /* Find the smallest rectangle that bounds the text when it is printed in rect
4723 * according to the format options listed in format. If rect has 0 width and
4724 * height, then just find the smallest rectangle that bounds the text when it's
4725 * printed at location (rect->X, rect-Y). */
4726 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4727 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4728 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4729 INT *codepointsfitted, INT *linesfilled)
4731 HFONT oldfont, gdifont;
4732 struct measure_string_args args;
4733 HDC temp_hdc=NULL, hdc;
4734 GpPointF pt[3];
4735 RectF scaled_rect;
4736 REAL margin_x;
4737 INT lines, glyphs;
4739 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4740 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4741 bounds, codepointsfitted, linesfilled);
4743 if(!graphics || !string || !font || !rect || !bounds)
4744 return InvalidParameter;
4746 if(!graphics->hdc)
4748 hdc = temp_hdc = CreateCompatibleDC(0);
4749 if (!temp_hdc) return OutOfMemory;
4751 else
4752 hdc = graphics->hdc;
4754 if(linesfilled) *linesfilled = 0;
4755 if(codepointsfitted) *codepointsfitted = 0;
4757 if(format)
4758 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4760 pt[0].X = 0.0;
4761 pt[0].Y = 0.0;
4762 pt[1].X = 1.0;
4763 pt[1].Y = 0.0;
4764 pt[2].X = 0.0;
4765 pt[2].Y = 1.0;
4766 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4767 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4768 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4769 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4770 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4772 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4773 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4775 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4776 scaled_rect.Y = rect->Y * args.rel_height;
4777 scaled_rect.Width = rect->Width * args.rel_width;
4778 scaled_rect.Height = rect->Height * args.rel_height;
4779 if (scaled_rect.Width >= 0.5)
4781 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4782 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4785 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4786 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4788 get_font_hfont(graphics, font, format, &gdifont, NULL);
4789 oldfont = SelectObject(hdc, gdifont);
4791 bounds->X = rect->X;
4792 bounds->Y = rect->Y;
4793 bounds->Width = 0.0;
4794 bounds->Height = 0.0;
4796 args.bounds = bounds;
4797 args.codepointsfitted = &glyphs;
4798 args.linesfilled = &lines;
4799 lines = glyphs = 0;
4801 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4802 measure_string_callback, &args);
4804 if (linesfilled) *linesfilled = lines;
4805 if (codepointsfitted) *codepointsfitted = glyphs;
4807 if (lines)
4808 bounds->Width += margin_x * 2.0;
4810 SelectObject(hdc, oldfont);
4811 DeleteObject(gdifont);
4813 if (temp_hdc)
4814 DeleteDC(temp_hdc);
4816 return Ok;
4819 struct draw_string_args {
4820 GpGraphics *graphics;
4821 GDIPCONST GpBrush *brush;
4822 REAL x, y, rel_width, rel_height, ascent;
4825 static GpStatus draw_string_callback(HDC hdc,
4826 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4827 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4828 INT lineno, const RectF *bounds, INT *underlined_indexes,
4829 INT underlined_index_count, void *user_data)
4831 struct draw_string_args *args = user_data;
4832 PointF position;
4833 GpStatus stat;
4835 position.X = args->x + bounds->X / args->rel_width;
4836 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4838 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4839 args->brush, &position,
4840 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4842 if (stat == Ok && underlined_index_count)
4844 OUTLINETEXTMETRICW otm;
4845 REAL underline_y, underline_height;
4846 int i;
4848 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4850 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4851 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4853 for (i=0; i<underlined_index_count; i++)
4855 REAL start_x, end_x;
4856 SIZE text_size;
4857 INT ofs = underlined_indexes[i] - index;
4859 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4860 start_x = text_size.cx / args->rel_width;
4862 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4863 end_x = text_size.cx / args->rel_width;
4865 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4869 return stat;
4872 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4873 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4874 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4876 HRGN rgn = NULL;
4877 HFONT gdifont;
4878 GpPointF pt[3], rectcpy[4];
4879 POINT corners[4];
4880 REAL rel_width, rel_height, margin_x;
4881 INT save_state, format_flags = 0;
4882 REAL offsety = 0.0;
4883 struct draw_string_args args;
4884 RectF scaled_rect;
4885 HDC hdc, temp_hdc=NULL;
4886 TEXTMETRICW textmetric;
4888 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4889 length, font, debugstr_rectf(rect), format, brush);
4891 if(!graphics || !string || !font || !brush || !rect)
4892 return InvalidParameter;
4894 if(graphics->hdc)
4896 hdc = graphics->hdc;
4898 else
4900 hdc = temp_hdc = CreateCompatibleDC(0);
4903 if(format){
4904 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4906 format_flags = format->attr;
4908 /* Should be no need to explicitly test for StringAlignmentNear as
4909 * that is default behavior if no alignment is passed. */
4910 if(format->vertalign != StringAlignmentNear){
4911 RectF bounds, in_rect = *rect;
4912 in_rect.Height = 0.0; /* avoid height clipping */
4913 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
4915 TRACE("bounds %s\n", debugstr_rectf(&bounds));
4917 if(format->vertalign == StringAlignmentCenter)
4918 offsety = (rect->Height - bounds.Height) / 2;
4919 else if(format->vertalign == StringAlignmentFar)
4920 offsety = (rect->Height - bounds.Height);
4922 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
4925 save_state = SaveDC(hdc);
4927 pt[0].X = 0.0;
4928 pt[0].Y = 0.0;
4929 pt[1].X = 1.0;
4930 pt[1].Y = 0.0;
4931 pt[2].X = 0.0;
4932 pt[2].Y = 1.0;
4933 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4934 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4935 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4936 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4937 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4939 rectcpy[3].X = rectcpy[0].X = rect->X;
4940 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
4941 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4942 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
4943 transform_and_round_points(graphics, corners, rectcpy, 4);
4945 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4946 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4948 scaled_rect.X = margin_x * rel_width;
4949 scaled_rect.Y = 0.0;
4950 scaled_rect.Width = rel_width * rect->Width;
4951 scaled_rect.Height = rel_height * rect->Height;
4952 if (scaled_rect.Width >= 0.5)
4954 scaled_rect.Width -= margin_x * 2.0 * rel_width;
4955 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4958 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4959 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4961 if (!(format_flags & StringFormatFlagsNoClip) &&
4962 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
4963 rect->Width > 0.0 && rect->Height > 0.0)
4965 /* FIXME: If only the width or only the height is 0, we should probably still clip */
4966 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4967 SelectClipRgn(hdc, rgn);
4970 get_font_hfont(graphics, font, format, &gdifont, NULL);
4971 SelectObject(hdc, gdifont);
4973 args.graphics = graphics;
4974 args.brush = brush;
4976 args.x = rect->X;
4977 args.y = rect->Y + offsety;
4979 args.rel_width = rel_width;
4980 args.rel_height = rel_height;
4982 GetTextMetricsW(hdc, &textmetric);
4983 args.ascent = textmetric.tmAscent / rel_height;
4985 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4986 draw_string_callback, &args);
4988 DeleteObject(rgn);
4989 DeleteObject(gdifont);
4991 RestoreDC(hdc, save_state);
4993 DeleteDC(temp_hdc);
4995 return Ok;
4998 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5000 TRACE("(%p)\n", graphics);
5002 if(!graphics)
5003 return InvalidParameter;
5005 if(graphics->busy)
5006 return ObjectBusy;
5008 return GdipSetInfinite(graphics->clip);
5011 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5013 TRACE("(%p)\n", graphics);
5015 if(!graphics)
5016 return InvalidParameter;
5018 if(graphics->busy)
5019 return ObjectBusy;
5021 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5024 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5026 return GdipEndContainer(graphics, state);
5029 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5030 GpMatrixOrder order)
5032 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5034 if(!graphics)
5035 return InvalidParameter;
5037 if(graphics->busy)
5038 return ObjectBusy;
5040 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5043 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5045 return GdipBeginContainer2(graphics, state);
5048 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5049 GraphicsContainer *state)
5051 GraphicsContainerItem *container;
5052 GpStatus sts;
5054 TRACE("(%p, %p)\n", graphics, state);
5056 if(!graphics || !state)
5057 return InvalidParameter;
5059 sts = init_container(&container, graphics);
5060 if(sts != Ok)
5061 return sts;
5063 list_add_head(&graphics->containers, &container->entry);
5064 *state = graphics->contid = container->contid;
5066 return Ok;
5069 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5071 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5072 return NotImplemented;
5075 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5077 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5078 return NotImplemented;
5081 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5083 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5084 return NotImplemented;
5087 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5089 GpStatus sts;
5090 GraphicsContainerItem *container, *container2;
5092 TRACE("(%p, %x)\n", graphics, state);
5094 if(!graphics)
5095 return InvalidParameter;
5097 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5098 if(container->contid == state)
5099 break;
5102 /* did not find a matching container */
5103 if(&container->entry == &graphics->containers)
5104 return Ok;
5106 sts = restore_container(graphics, container);
5107 if(sts != Ok)
5108 return sts;
5110 /* remove all of the containers on top of the found container */
5111 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5112 if(container->contid == state)
5113 break;
5114 list_remove(&container->entry);
5115 delete_container(container);
5118 list_remove(&container->entry);
5119 delete_container(container);
5121 return Ok;
5124 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5125 REAL sy, GpMatrixOrder order)
5127 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5129 if(!graphics)
5130 return InvalidParameter;
5132 if(graphics->busy)
5133 return ObjectBusy;
5135 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5138 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5139 CombineMode mode)
5141 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5143 if(!graphics || !srcgraphics)
5144 return InvalidParameter;
5146 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5149 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5150 CompositingMode mode)
5152 TRACE("(%p, %d)\n", graphics, mode);
5154 if(!graphics)
5155 return InvalidParameter;
5157 if(graphics->busy)
5158 return ObjectBusy;
5160 graphics->compmode = mode;
5162 return Ok;
5165 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5166 CompositingQuality quality)
5168 TRACE("(%p, %d)\n", graphics, quality);
5170 if(!graphics)
5171 return InvalidParameter;
5173 if(graphics->busy)
5174 return ObjectBusy;
5176 graphics->compqual = quality;
5178 return Ok;
5181 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5182 InterpolationMode mode)
5184 TRACE("(%p, %d)\n", graphics, mode);
5186 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5187 return InvalidParameter;
5189 if(graphics->busy)
5190 return ObjectBusy;
5192 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5193 mode = InterpolationModeBilinear;
5195 if (mode == InterpolationModeHighQuality)
5196 mode = InterpolationModeHighQualityBicubic;
5198 graphics->interpolation = mode;
5200 return Ok;
5203 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5205 GpStatus stat;
5207 TRACE("(%p, %.2f)\n", graphics, scale);
5209 if(!graphics || (scale <= 0.0))
5210 return InvalidParameter;
5212 if(graphics->busy)
5213 return ObjectBusy;
5215 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5217 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5218 if (stat != Ok)
5219 return stat;
5222 graphics->scale = scale;
5224 return Ok;
5227 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5229 GpStatus stat;
5231 TRACE("(%p, %d)\n", graphics, unit);
5233 if(!graphics)
5234 return InvalidParameter;
5236 if(graphics->busy)
5237 return ObjectBusy;
5239 if(unit == UnitWorld)
5240 return InvalidParameter;
5242 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5244 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5245 if (stat != Ok)
5246 return stat;
5249 graphics->unit = unit;
5251 return Ok;
5254 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5255 mode)
5257 TRACE("(%p, %d)\n", graphics, mode);
5259 if(!graphics)
5260 return InvalidParameter;
5262 if(graphics->busy)
5263 return ObjectBusy;
5265 graphics->pixeloffset = mode;
5267 return Ok;
5270 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5272 static int calls;
5274 TRACE("(%p,%i,%i)\n", graphics, x, y);
5276 if (!(calls++))
5277 FIXME("value is unused in rendering\n");
5279 if (!graphics)
5280 return InvalidParameter;
5282 graphics->origin_x = x;
5283 graphics->origin_y = y;
5285 return Ok;
5288 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5290 TRACE("(%p,%p,%p)\n", graphics, x, y);
5292 if (!graphics || !x || !y)
5293 return InvalidParameter;
5295 *x = graphics->origin_x;
5296 *y = graphics->origin_y;
5298 return Ok;
5301 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5303 TRACE("(%p, %d)\n", graphics, mode);
5305 if(!graphics)
5306 return InvalidParameter;
5308 if(graphics->busy)
5309 return ObjectBusy;
5311 graphics->smoothing = mode;
5313 return Ok;
5316 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5318 TRACE("(%p, %d)\n", graphics, contrast);
5320 if(!graphics)
5321 return InvalidParameter;
5323 graphics->textcontrast = contrast;
5325 return Ok;
5328 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5329 TextRenderingHint hint)
5331 TRACE("(%p, %d)\n", graphics, hint);
5333 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5334 return InvalidParameter;
5336 if(graphics->busy)
5337 return ObjectBusy;
5339 graphics->texthint = hint;
5341 return Ok;
5344 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5346 TRACE("(%p, %p)\n", graphics, matrix);
5348 if(!graphics || !matrix)
5349 return InvalidParameter;
5351 if(graphics->busy)
5352 return ObjectBusy;
5354 TRACE("%f,%f,%f,%f,%f,%f\n",
5355 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5356 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5358 graphics->worldtrans = *matrix;
5360 return Ok;
5363 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5364 REAL dy, GpMatrixOrder order)
5366 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5368 if(!graphics)
5369 return InvalidParameter;
5371 if(graphics->busy)
5372 return ObjectBusy;
5374 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5377 /*****************************************************************************
5378 * GdipSetClipHrgn [GDIPLUS.@]
5380 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5382 GpRegion *region;
5383 GpStatus status;
5385 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5387 if(!graphics)
5388 return InvalidParameter;
5390 if(graphics->busy)
5391 return ObjectBusy;
5393 /* hrgn is already in device units */
5394 status = GdipCreateRegionHrgn(hrgn, &region);
5395 if(status != Ok)
5396 return status;
5398 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5400 GdipDeleteRegion(region);
5401 return status;
5404 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5406 GpStatus status;
5407 GpPath *clip_path;
5409 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5411 if(!graphics)
5412 return InvalidParameter;
5414 if(graphics->busy)
5415 return ObjectBusy;
5417 status = GdipClonePath(path, &clip_path);
5418 if (status == Ok)
5420 GpMatrix world_to_device;
5422 get_graphics_transform(graphics, CoordinateSpaceDevice,
5423 CoordinateSpaceWorld, &world_to_device);
5424 status = GdipTransformPath(clip_path, &world_to_device);
5425 if (status == Ok)
5426 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5428 GdipDeletePath(clip_path);
5430 return status;
5433 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5434 REAL width, REAL height,
5435 CombineMode mode)
5437 GpStatus status;
5438 GpRectF rect;
5439 GpRegion *region;
5441 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5443 if(!graphics)
5444 return InvalidParameter;
5446 if(graphics->busy)
5447 return ObjectBusy;
5449 rect.X = x;
5450 rect.Y = y;
5451 rect.Width = width;
5452 rect.Height = height;
5453 status = GdipCreateRegionRect(&rect, &region);
5454 if (status == Ok)
5456 GpMatrix world_to_device;
5458 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5459 status = GdipTransformRegion(region, &world_to_device);
5460 if (status == Ok)
5461 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5463 GdipDeleteRegion(region);
5465 return status;
5468 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5469 INT width, INT height,
5470 CombineMode mode)
5472 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5474 if(!graphics)
5475 return InvalidParameter;
5477 if(graphics->busy)
5478 return ObjectBusy;
5480 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5483 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5484 CombineMode mode)
5486 GpStatus status;
5487 GpRegion *clip;
5489 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5491 if(!graphics || !region)
5492 return InvalidParameter;
5494 if(graphics->busy)
5495 return ObjectBusy;
5497 status = GdipCloneRegion(region, &clip);
5498 if (status == Ok)
5500 GpMatrix world_to_device;
5502 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5503 status = GdipTransformRegion(clip, &world_to_device);
5504 if (status == Ok)
5505 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5507 GdipDeleteRegion(clip);
5509 return status;
5512 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5513 INT count)
5515 INT save_state;
5516 POINT *pti;
5518 TRACE("(%p, %p, %d)\n", graphics, points, count);
5520 if(!graphics || !pen || count<=0)
5521 return InvalidParameter;
5523 if(graphics->busy)
5524 return ObjectBusy;
5526 if (!graphics->hdc)
5528 FIXME("graphics object has no HDC\n");
5529 return Ok;
5532 pti = GdipAlloc(sizeof(POINT) * count);
5534 save_state = prepare_dc(graphics, pen);
5535 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5537 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5538 Polygon(graphics->hdc, pti, count);
5540 restore_dc(graphics, save_state);
5541 GdipFree(pti);
5543 return Ok;
5546 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5547 INT count)
5549 GpStatus ret;
5550 GpPointF *ptf;
5551 INT i;
5553 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5555 if(count<=0) return InvalidParameter;
5556 ptf = GdipAlloc(sizeof(GpPointF) * count);
5558 for(i = 0;i < count; i++){
5559 ptf[i].X = (REAL)points[i].X;
5560 ptf[i].Y = (REAL)points[i].Y;
5563 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5564 GdipFree(ptf);
5566 return ret;
5569 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5571 TRACE("(%p, %p)\n", graphics, dpi);
5573 if(!graphics || !dpi)
5574 return InvalidParameter;
5576 if(graphics->busy)
5577 return ObjectBusy;
5579 *dpi = graphics->xres;
5580 return Ok;
5583 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5585 TRACE("(%p, %p)\n", graphics, dpi);
5587 if(!graphics || !dpi)
5588 return InvalidParameter;
5590 if(graphics->busy)
5591 return ObjectBusy;
5593 *dpi = graphics->yres;
5594 return Ok;
5597 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5598 GpMatrixOrder order)
5600 GpMatrix m;
5601 GpStatus ret;
5603 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5605 if(!graphics || !matrix)
5606 return InvalidParameter;
5608 if(graphics->busy)
5609 return ObjectBusy;
5611 m = graphics->worldtrans;
5613 ret = GdipMultiplyMatrix(&m, matrix, order);
5614 if(ret == Ok)
5615 graphics->worldtrans = m;
5617 return ret;
5620 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5621 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5623 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5625 GpStatus stat=Ok;
5627 TRACE("(%p, %p)\n", graphics, hdc);
5629 if(!graphics || !hdc)
5630 return InvalidParameter;
5632 if(graphics->busy)
5633 return ObjectBusy;
5635 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5637 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5639 else if (!graphics->hdc || graphics->alpha_hdc ||
5640 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5642 /* Create a fake HDC and fill it with a constant color. */
5643 HDC temp_hdc;
5644 HBITMAP hbitmap;
5645 GpRectF bounds;
5646 BITMAPINFOHEADER bmih;
5647 int i;
5649 stat = get_graphics_bounds(graphics, &bounds);
5650 if (stat != Ok)
5651 return stat;
5653 graphics->temp_hbitmap_width = bounds.Width;
5654 graphics->temp_hbitmap_height = bounds.Height;
5656 bmih.biSize = sizeof(bmih);
5657 bmih.biWidth = graphics->temp_hbitmap_width;
5658 bmih.biHeight = -graphics->temp_hbitmap_height;
5659 bmih.biPlanes = 1;
5660 bmih.biBitCount = 32;
5661 bmih.biCompression = BI_RGB;
5662 bmih.biSizeImage = 0;
5663 bmih.biXPelsPerMeter = 0;
5664 bmih.biYPelsPerMeter = 0;
5665 bmih.biClrUsed = 0;
5666 bmih.biClrImportant = 0;
5668 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5669 (void**)&graphics->temp_bits, NULL, 0);
5670 if (!hbitmap)
5671 return GenericError;
5673 temp_hdc = CreateCompatibleDC(0);
5674 if (!temp_hdc)
5676 DeleteObject(hbitmap);
5677 return GenericError;
5680 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5681 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5683 SelectObject(temp_hdc, hbitmap);
5685 graphics->temp_hbitmap = hbitmap;
5686 *hdc = graphics->temp_hdc = temp_hdc;
5688 else
5690 *hdc = graphics->hdc;
5693 if (stat == Ok)
5694 graphics->busy = TRUE;
5696 return stat;
5699 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5701 GpStatus stat=Ok;
5703 TRACE("(%p, %p)\n", graphics, hdc);
5705 if(!graphics || !hdc || !graphics->busy)
5706 return InvalidParameter;
5708 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5710 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5712 else if (graphics->temp_hdc == hdc)
5714 DWORD* pos;
5715 int i;
5717 /* Find the pixels that have changed, and mark them as opaque. */
5718 pos = (DWORD*)graphics->temp_bits;
5719 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5721 if (*pos != DC_BACKGROUND_KEY)
5723 *pos |= 0xff000000;
5725 pos++;
5728 /* Write the changed pixels to the real target. */
5729 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5730 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5731 graphics->temp_hbitmap_width * 4);
5733 /* Clean up. */
5734 DeleteDC(graphics->temp_hdc);
5735 DeleteObject(graphics->temp_hbitmap);
5736 graphics->temp_hdc = NULL;
5737 graphics->temp_hbitmap = NULL;
5739 else if (hdc != graphics->hdc)
5741 stat = InvalidParameter;
5744 if (stat == Ok)
5745 graphics->busy = FALSE;
5747 return stat;
5750 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5752 GpRegion *clip;
5753 GpStatus status;
5754 GpMatrix device_to_world;
5756 TRACE("(%p, %p)\n", graphics, region);
5758 if(!graphics || !region)
5759 return InvalidParameter;
5761 if(graphics->busy)
5762 return ObjectBusy;
5764 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5765 return status;
5767 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5768 status = GdipTransformRegion(clip, &device_to_world);
5769 if (status != Ok)
5771 GdipDeleteRegion(clip);
5772 return status;
5775 /* free everything except root node and header */
5776 delete_element(&region->node);
5777 memcpy(region, clip, sizeof(GpRegion));
5778 GdipFree(clip);
5780 return Ok;
5783 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5784 GpCoordinateSpace src_space, GpMatrix *matrix)
5786 GpStatus stat = Ok;
5787 REAL scale_x, scale_y;
5789 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5791 if (dst_space != src_space)
5793 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5794 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5796 if(graphics->unit != UnitDisplay)
5798 scale_x *= graphics->scale;
5799 scale_y *= graphics->scale;
5802 /* transform from src_space to CoordinateSpacePage */
5803 switch (src_space)
5805 case CoordinateSpaceWorld:
5806 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5807 break;
5808 case CoordinateSpacePage:
5809 break;
5810 case CoordinateSpaceDevice:
5811 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5812 break;
5815 /* transform from CoordinateSpacePage to dst_space */
5816 switch (dst_space)
5818 case CoordinateSpaceWorld:
5820 GpMatrix inverted_transform = graphics->worldtrans;
5821 stat = GdipInvertMatrix(&inverted_transform);
5822 if (stat == Ok)
5823 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5824 break;
5826 case CoordinateSpacePage:
5827 break;
5828 case CoordinateSpaceDevice:
5829 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5830 break;
5833 return stat;
5836 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5837 GpCoordinateSpace src_space, GpPointF *points, INT count)
5839 GpMatrix matrix;
5840 GpStatus stat;
5842 if(!graphics || !points || count <= 0)
5843 return InvalidParameter;
5845 if(graphics->busy)
5846 return ObjectBusy;
5848 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5850 if (src_space == dst_space) return Ok;
5852 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5853 if (stat != Ok) return stat;
5855 return GdipTransformMatrixPoints(&matrix, points, count);
5858 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5859 GpCoordinateSpace src_space, GpPoint *points, INT count)
5861 GpPointF *pointsF;
5862 GpStatus ret;
5863 INT i;
5865 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5867 if(count <= 0)
5868 return InvalidParameter;
5870 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5871 if(!pointsF)
5872 return OutOfMemory;
5874 for(i = 0; i < count; i++){
5875 pointsF[i].X = (REAL)points[i].X;
5876 pointsF[i].Y = (REAL)points[i].Y;
5879 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5881 if(ret == Ok)
5882 for(i = 0; i < count; i++){
5883 points[i].X = gdip_round(pointsF[i].X);
5884 points[i].Y = gdip_round(pointsF[i].Y);
5886 GdipFree(pointsF);
5888 return ret;
5891 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5893 static int calls;
5895 TRACE("\n");
5897 if (!calls++)
5898 FIXME("stub\n");
5900 return NULL;
5903 /*****************************************************************************
5904 * GdipTranslateClip [GDIPLUS.@]
5906 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5908 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5910 if(!graphics)
5911 return InvalidParameter;
5913 if(graphics->busy)
5914 return ObjectBusy;
5916 return GdipTranslateRegion(graphics->clip, dx, dy);
5919 /*****************************************************************************
5920 * GdipTranslateClipI [GDIPLUS.@]
5922 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5924 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5926 if(!graphics)
5927 return InvalidParameter;
5929 if(graphics->busy)
5930 return ObjectBusy;
5932 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5936 /*****************************************************************************
5937 * GdipMeasureDriverString [GDIPLUS.@]
5939 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5940 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5941 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5943 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
5944 HFONT hfont;
5945 HDC hdc;
5946 REAL min_x, min_y, max_x, max_y, x, y;
5947 int i;
5948 TEXTMETRICW textmetric;
5949 const WORD *glyph_indices;
5950 WORD *dynamic_glyph_indices=NULL;
5951 REAL rel_width, rel_height, ascent, descent;
5952 GpPointF pt[3];
5954 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5956 if (!graphics || !text || !font || !positions || !boundingBox)
5957 return InvalidParameter;
5959 if (length == -1)
5960 length = strlenW(text);
5962 if (length == 0)
5964 boundingBox->X = 0.0;
5965 boundingBox->Y = 0.0;
5966 boundingBox->Width = 0.0;
5967 boundingBox->Height = 0.0;
5970 if (flags & unsupported_flags)
5971 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
5973 get_font_hfont(graphics, font, NULL, &hfont, matrix);
5975 hdc = CreateCompatibleDC(0);
5976 SelectObject(hdc, hfont);
5978 GetTextMetricsW(hdc, &textmetric);
5980 pt[0].X = 0.0;
5981 pt[0].Y = 0.0;
5982 pt[1].X = 1.0;
5983 pt[1].Y = 0.0;
5984 pt[2].X = 0.0;
5985 pt[2].Y = 1.0;
5986 if (matrix)
5988 GpMatrix xform = *matrix;
5989 GdipTransformMatrixPoints(&xform, pt, 3);
5991 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5992 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5993 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5994 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5995 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5997 if (flags & DriverStringOptionsCmapLookup)
5999 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6000 if (!glyph_indices)
6002 DeleteDC(hdc);
6003 DeleteObject(hfont);
6004 return OutOfMemory;
6007 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6009 else
6010 glyph_indices = text;
6012 min_x = max_x = x = positions[0].X;
6013 min_y = max_y = y = positions[0].Y;
6015 ascent = textmetric.tmAscent / rel_height;
6016 descent = textmetric.tmDescent / rel_height;
6018 for (i=0; i<length; i++)
6020 int char_width;
6021 ABC abc;
6023 if (!(flags & DriverStringOptionsRealizedAdvance))
6025 x = positions[i].X;
6026 y = positions[i].Y;
6029 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6030 char_width = abc.abcA + abc.abcB + abc.abcC;
6032 if (min_y > y - ascent) min_y = y - ascent;
6033 if (max_y < y + descent) max_y = y + descent;
6034 if (min_x > x) min_x = x;
6036 x += char_width / rel_width;
6038 if (max_x < x) max_x = x;
6041 GdipFree(dynamic_glyph_indices);
6042 DeleteDC(hdc);
6043 DeleteObject(hfont);
6045 boundingBox->X = min_x;
6046 boundingBox->Y = min_y;
6047 boundingBox->Width = max_x - min_x;
6048 boundingBox->Height = max_y - min_y;
6050 return Ok;
6053 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6054 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6055 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6056 INT flags, GDIPCONST GpMatrix *matrix)
6058 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6059 INT save_state;
6060 GpPointF pt;
6061 HFONT hfont;
6062 UINT eto_flags=0;
6064 if (flags & unsupported_flags)
6065 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6067 if (!(flags & DriverStringOptionsCmapLookup))
6068 eto_flags |= ETO_GLYPH_INDEX;
6070 save_state = SaveDC(graphics->hdc);
6071 SetBkMode(graphics->hdc, TRANSPARENT);
6072 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6074 pt = positions[0];
6075 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6077 get_font_hfont(graphics, font, format, &hfont, matrix);
6078 SelectObject(graphics->hdc, hfont);
6080 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6082 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6084 RestoreDC(graphics->hdc, save_state);
6086 DeleteObject(hfont);
6088 return Ok;
6091 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6092 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6093 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6094 INT flags, GDIPCONST GpMatrix *matrix)
6096 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6097 GpStatus stat;
6098 PointF *real_positions, real_position;
6099 POINT *pti;
6100 HFONT hfont;
6101 HDC hdc;
6102 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6103 DWORD max_glyphsize=0;
6104 GLYPHMETRICS glyphmetrics;
6105 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6106 BYTE *glyph_mask;
6107 BYTE *text_mask;
6108 int text_mask_stride;
6109 BYTE *pixel_data;
6110 int pixel_data_stride;
6111 GpRect pixel_area;
6112 UINT ggo_flags = GGO_GRAY8_BITMAP;
6114 if (length <= 0)
6115 return Ok;
6117 if (!(flags & DriverStringOptionsCmapLookup))
6118 ggo_flags |= GGO_GLYPH_INDEX;
6120 if (flags & unsupported_flags)
6121 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6123 pti = GdipAlloc(sizeof(POINT) * length);
6124 if (!pti)
6125 return OutOfMemory;
6127 if (flags & DriverStringOptionsRealizedAdvance)
6129 real_position = positions[0];
6131 transform_and_round_points(graphics, pti, &real_position, 1);
6133 else
6135 real_positions = GdipAlloc(sizeof(PointF) * length);
6136 if (!real_positions)
6138 GdipFree(pti);
6139 return OutOfMemory;
6142 memcpy(real_positions, positions, sizeof(PointF) * length);
6144 transform_and_round_points(graphics, pti, real_positions, length);
6146 GdipFree(real_positions);
6149 get_font_hfont(graphics, font, format, &hfont, matrix);
6151 hdc = CreateCompatibleDC(0);
6152 SelectObject(hdc, hfont);
6154 /* Get the boundaries of the text to be drawn */
6155 for (i=0; i<length; i++)
6157 DWORD glyphsize;
6158 int left, top, right, bottom;
6160 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6161 &glyphmetrics, 0, NULL, &identity);
6163 if (glyphsize == GDI_ERROR)
6165 ERR("GetGlyphOutlineW failed\n");
6166 GdipFree(pti);
6167 DeleteDC(hdc);
6168 DeleteObject(hfont);
6169 return GenericError;
6172 if (glyphsize > max_glyphsize)
6173 max_glyphsize = glyphsize;
6175 if (glyphsize != 0)
6177 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6178 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6179 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6180 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6182 if (left < min_x) min_x = left;
6183 if (top < min_y) min_y = top;
6184 if (right > max_x) max_x = right;
6185 if (bottom > max_y) max_y = bottom;
6188 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6190 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6191 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6195 if (max_glyphsize == 0)
6196 /* Nothing to draw. */
6197 return Ok;
6199 glyph_mask = GdipAlloc(max_glyphsize);
6200 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6201 text_mask_stride = max_x - min_x;
6203 if (!(glyph_mask && text_mask))
6205 GdipFree(glyph_mask);
6206 GdipFree(text_mask);
6207 GdipFree(pti);
6208 DeleteDC(hdc);
6209 DeleteObject(hfont);
6210 return OutOfMemory;
6213 /* Generate a mask for the text */
6214 for (i=0; i<length; i++)
6216 DWORD ret;
6217 int left, top, stride;
6219 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6220 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6222 if (ret == GDI_ERROR || ret == 0)
6223 continue; /* empty glyph */
6225 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6226 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6227 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6229 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6231 BYTE *glyph_val = glyph_mask + y * stride;
6232 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6233 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6235 *text_val = min(64, *text_val + *glyph_val);
6236 glyph_val++;
6237 text_val++;
6242 GdipFree(pti);
6243 DeleteDC(hdc);
6244 DeleteObject(hfont);
6245 GdipFree(glyph_mask);
6247 /* get the brush data */
6248 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6249 if (!pixel_data)
6251 GdipFree(text_mask);
6252 return OutOfMemory;
6255 pixel_area.X = min_x;
6256 pixel_area.Y = min_y;
6257 pixel_area.Width = max_x - min_x;
6258 pixel_area.Height = max_y - min_y;
6259 pixel_data_stride = pixel_area.Width * 4;
6261 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6262 if (stat != Ok)
6264 GdipFree(text_mask);
6265 GdipFree(pixel_data);
6266 return stat;
6269 /* multiply the brush data by the mask */
6270 for (y=0; y<pixel_area.Height; y++)
6272 BYTE *text_val = text_mask + text_mask_stride * y;
6273 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6274 for (x=0; x<pixel_area.Width; x++)
6276 *pixel_val = (*pixel_val) * (*text_val) / 64;
6277 text_val++;
6278 pixel_val+=4;
6282 GdipFree(text_mask);
6284 /* draw the result */
6285 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6286 pixel_area.Height, pixel_data_stride);
6288 GdipFree(pixel_data);
6290 return stat;
6293 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6294 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6295 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6296 INT flags, GDIPCONST GpMatrix *matrix)
6298 GpStatus stat = NotImplemented;
6300 if (length == -1)
6301 length = strlenW(text);
6303 if (graphics->hdc && !graphics->alpha_hdc &&
6304 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6305 brush->bt == BrushTypeSolidColor &&
6306 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6307 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6308 brush, positions, flags, matrix);
6309 if (stat == NotImplemented)
6310 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6311 brush, positions, flags, matrix);
6312 return stat;
6315 /*****************************************************************************
6316 * GdipDrawDriverString [GDIPLUS.@]
6318 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6319 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6320 GDIPCONST PointF *positions, INT flags,
6321 GDIPCONST GpMatrix *matrix )
6323 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6325 if (!graphics || !text || !font || !brush || !positions)
6326 return InvalidParameter;
6328 return draw_driver_string(graphics, text, length, font, NULL,
6329 brush, positions, flags, matrix);
6332 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6333 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6335 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6336 return NotImplemented;
6339 /*****************************************************************************
6340 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6342 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6344 GpStatus stat;
6345 GpRegion* rgn;
6347 TRACE("(%p, %p)\n", graphics, res);
6349 if((stat = GdipCreateRegion(&rgn)) != Ok)
6350 return stat;
6352 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6353 goto cleanup;
6355 stat = GdipIsEmptyRegion(rgn, graphics, res);
6357 cleanup:
6358 GdipDeleteRegion(rgn);
6359 return stat;
6362 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6364 static int calls;
6366 TRACE("(%p) stub\n", graphics);
6368 if(!(calls++))
6369 FIXME("not implemented\n");
6371 return NotImplemented;