urlmon: Moved FindMimeFromData to mimefilter.c.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blob43464c90695cd42620f12afc27021fc845159a29
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 static void transform_rectf(GpGraphics *graphics, GpCoordinateSpace dst_space,
58 GpCoordinateSpace src_space, GpRectF *rect)
60 GpPointF pt[3];
62 pt[0].X = rect->X;
63 pt[0].Y = rect->Y;
64 pt[1].X = rect->X + rect->Width;
65 pt[1].Y = rect->Y;
66 pt[2].X = rect->X;
67 pt[2].Y = rect->Y + rect->Height;
68 GdipTransformPoints(graphics, dst_space, src_space, pt, 3);
69 rect->X = pt[0].X;
70 rect->Y = pt[0].Y;
71 rect->Width = sqrt((pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y) +
72 (pt[1].X - pt[0].X) * (pt[1].X - pt[0].X));
73 rect->Height = sqrt((pt[2].Y - pt[0].Y) * (pt[2].Y - pt[0].Y) +
74 (pt[2].X - pt[0].X) * (pt[2].X - pt[0].X));
77 /* Converts from gdiplus path point type to gdi path point type. */
78 static BYTE convert_path_point_type(BYTE type)
80 BYTE ret;
82 switch(type & PathPointTypePathTypeMask){
83 case PathPointTypeBezier:
84 ret = PT_BEZIERTO;
85 break;
86 case PathPointTypeLine:
87 ret = PT_LINETO;
88 break;
89 case PathPointTypeStart:
90 ret = PT_MOVETO;
91 break;
92 default:
93 ERR("Bad point type\n");
94 return 0;
97 if(type & PathPointTypeCloseSubpath)
98 ret |= PT_CLOSEFIGURE;
100 return ret;
103 static COLORREF get_gdi_brush_color(const GpBrush *brush)
105 ARGB argb;
107 switch (brush->bt)
109 case BrushTypeSolidColor:
111 const GpSolidFill *sf = (const GpSolidFill *)brush;
112 argb = sf->color;
113 break;
115 case BrushTypeHatchFill:
117 const GpHatch *hatch = (const GpHatch *)brush;
118 argb = hatch->forecol;
119 break;
121 case BrushTypeLinearGradient:
123 const GpLineGradient *line = (const GpLineGradient *)brush;
124 argb = line->startcolor;
125 break;
127 case BrushTypePathGradient:
129 const GpPathGradient *grad = (const GpPathGradient *)brush;
130 argb = grad->centercolor;
131 break;
133 default:
134 FIXME("unhandled brush type %d\n", brush->bt);
135 argb = 0;
136 break;
138 return ARGB2COLORREF(argb);
141 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
143 HBITMAP hbmp;
144 BITMAPINFOHEADER bmih;
145 DWORD *bits;
146 int x, y;
148 bmih.biSize = sizeof(bmih);
149 bmih.biWidth = 8;
150 bmih.biHeight = 8;
151 bmih.biPlanes = 1;
152 bmih.biBitCount = 32;
153 bmih.biCompression = BI_RGB;
154 bmih.biSizeImage = 0;
156 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
157 if (hbmp)
159 const char *hatch_data;
161 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
163 for (y = 0; y < 8; y++)
165 for (x = 0; x < 8; x++)
167 if (hatch_data[y] & (0x80 >> x))
168 bits[y * 8 + x] = hatch->forecol;
169 else
170 bits[y * 8 + x] = hatch->backcol;
174 else
176 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
178 for (y = 0; y < 64; y++)
179 bits[y] = hatch->forecol;
183 return hbmp;
186 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
188 switch (brush->bt)
190 case BrushTypeSolidColor:
192 const GpSolidFill *sf = (const GpSolidFill *)brush;
193 lb->lbStyle = BS_SOLID;
194 lb->lbColor = ARGB2COLORREF(sf->color);
195 lb->lbHatch = 0;
196 return Ok;
199 case BrushTypeHatchFill:
201 const GpHatch *hatch = (const GpHatch *)brush;
202 HBITMAP hbmp;
204 hbmp = create_hatch_bitmap(hatch);
205 if (!hbmp) return OutOfMemory;
207 lb->lbStyle = BS_PATTERN;
208 lb->lbColor = 0;
209 lb->lbHatch = (ULONG_PTR)hbmp;
210 return Ok;
213 default:
214 FIXME("unhandled brush type %d\n", brush->bt);
215 lb->lbStyle = BS_SOLID;
216 lb->lbColor = get_gdi_brush_color(brush);
217 lb->lbHatch = 0;
218 return Ok;
222 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
224 switch (lb->lbStyle)
226 case BS_PATTERN:
227 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
228 break;
230 return Ok;
233 static HBRUSH create_gdi_brush(const GpBrush *brush)
235 LOGBRUSH lb;
236 HBRUSH gdibrush;
238 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
240 gdibrush = CreateBrushIndirect(&lb);
241 free_gdi_logbrush(&lb);
243 return gdibrush;
246 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
248 LOGBRUSH lb;
249 HPEN gdipen;
250 REAL width;
251 INT save_state, i, numdashes;
252 GpPointF pt[2];
253 DWORD dash_array[MAX_DASHLEN];
255 save_state = SaveDC(graphics->hdc);
257 EndPath(graphics->hdc);
259 if(pen->unit == UnitPixel){
260 width = pen->width;
262 else{
263 /* Get an estimate for the amount the pen width is affected by the world
264 * transform. (This is similar to what some of the wine drivers do.) */
265 pt[0].X = 0.0;
266 pt[0].Y = 0.0;
267 pt[1].X = 1.0;
268 pt[1].Y = 1.0;
269 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
270 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
271 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
273 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
276 if(pen->dash == DashStyleCustom){
277 numdashes = min(pen->numdashes, MAX_DASHLEN);
279 TRACE("dashes are: ");
280 for(i = 0; i < numdashes; i++){
281 dash_array[i] = gdip_round(width * pen->dashes[i]);
282 TRACE("%d, ", dash_array[i]);
284 TRACE("\n and the pen style is %x\n", pen->style);
286 create_gdi_logbrush(pen->brush, &lb);
287 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
288 numdashes, dash_array);
289 free_gdi_logbrush(&lb);
291 else
293 create_gdi_logbrush(pen->brush, &lb);
294 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
295 free_gdi_logbrush(&lb);
298 SelectObject(graphics->hdc, gdipen);
300 return save_state;
303 static void restore_dc(GpGraphics *graphics, INT state)
305 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
306 RestoreDC(graphics->hdc, state);
309 /* This helper applies all the changes that the points listed in ptf need in
310 * order to be drawn on the device context. In the end, this should include at
311 * least:
312 * -scaling by page unit
313 * -applying world transformation
314 * -converting from float to int
315 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
316 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
317 * gdi to draw, and these functions would irreparably mess with line widths.
319 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
320 GpPointF *ptf, INT count)
322 REAL scale_x, scale_y;
323 GpMatrix matrix;
324 int i;
326 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
327 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
329 /* apply page scale */
330 if(graphics->unit != UnitDisplay)
332 scale_x *= graphics->scale;
333 scale_y *= graphics->scale;
336 matrix = graphics->worldtrans;
337 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
338 GdipTransformMatrixPoints(&matrix, ptf, count);
340 for(i = 0; i < count; i++){
341 pti[i].x = gdip_round(ptf[i].X);
342 pti[i].y = gdip_round(ptf[i].Y);
346 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
347 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
349 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
351 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
353 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
354 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
356 else
358 BLENDFUNCTION bf;
360 bf.BlendOp = AC_SRC_OVER;
361 bf.BlendFlags = 0;
362 bf.SourceConstantAlpha = 255;
363 bf.AlphaFormat = AC_SRC_ALPHA;
365 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
366 hdc, src_x, src_y, src_width, src_height, bf);
370 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
372 /* clipping region is in device coords */
373 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
376 /* Draw non-premultiplied ARGB data to the given graphics object */
377 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
378 const BYTE *src, INT src_width, INT src_height, INT src_stride)
380 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
381 INT x, y;
383 for (x=0; x<src_width; x++)
385 for (y=0; y<src_height; y++)
387 ARGB dst_color, src_color;
388 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
389 src_color = ((ARGB*)(src + src_stride * y))[x];
390 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
394 return Ok;
397 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
398 const BYTE *src, INT src_width, INT src_height, INT src_stride)
400 HDC hdc;
401 HBITMAP hbitmap;
402 BITMAPINFOHEADER bih;
403 BYTE *temp_bits;
405 hdc = CreateCompatibleDC(0);
407 bih.biSize = sizeof(BITMAPINFOHEADER);
408 bih.biWidth = src_width;
409 bih.biHeight = -src_height;
410 bih.biPlanes = 1;
411 bih.biBitCount = 32;
412 bih.biCompression = BI_RGB;
413 bih.biSizeImage = 0;
414 bih.biXPelsPerMeter = 0;
415 bih.biYPelsPerMeter = 0;
416 bih.biClrUsed = 0;
417 bih.biClrImportant = 0;
419 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
420 (void**)&temp_bits, NULL, 0);
422 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
423 memcpy(temp_bits, src, src_width * src_height * 4);
424 else
425 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
426 4 * src_width, src, src_stride);
428 SelectObject(hdc, hbitmap);
429 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
430 hdc, 0, 0, src_width, src_height);
431 DeleteDC(hdc);
432 DeleteObject(hbitmap);
434 return Ok;
437 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
438 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
440 GpStatus stat=Ok;
442 if (graphics->image && graphics->image->type == ImageTypeBitmap)
444 DWORD i;
445 int size;
446 RGNDATA *rgndata;
447 RECT *rects;
448 HRGN hrgn, visible_rgn;
450 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
451 if (!hrgn)
452 return OutOfMemory;
454 stat = get_clip_hrgn(graphics, &visible_rgn);
455 if (stat != Ok)
457 DeleteObject(hrgn);
458 return stat;
461 if (visible_rgn)
463 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
464 DeleteObject(visible_rgn);
467 if (hregion)
468 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
470 size = GetRegionData(hrgn, 0, NULL);
472 rgndata = GdipAlloc(size);
473 if (!rgndata)
475 DeleteObject(hrgn);
476 return OutOfMemory;
479 GetRegionData(hrgn, size, rgndata);
481 rects = (RECT*)rgndata->Buffer;
483 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
485 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
486 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
487 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
488 src_stride);
491 GdipFree(rgndata);
493 DeleteObject(hrgn);
495 return stat;
497 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
499 ERR("This should not be used for metafiles; fix caller\n");
500 return NotImplemented;
502 else
504 HRGN hrgn;
505 int save;
507 stat = get_clip_hrgn(graphics, &hrgn);
509 if (stat != Ok)
510 return stat;
512 save = SaveDC(graphics->hdc);
514 if (hrgn)
515 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
517 if (hregion)
518 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
520 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
521 src_height, src_stride);
523 RestoreDC(graphics->hdc, save);
525 DeleteObject(hrgn);
527 return stat;
531 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
532 const BYTE *src, INT src_width, INT src_height, INT src_stride)
534 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL);
537 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
539 ARGB result=0;
540 ARGB i;
541 INT a1, a2, a3;
543 a1 = (start >> 24) & 0xff;
544 a2 = (end >> 24) & 0xff;
546 a3 = (int)(a1*(1.0f - position)+a2*(position));
548 result |= a3 << 24;
550 for (i=0xff; i<=0xff0000; i = i << 8)
551 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
552 return result;
555 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
557 REAL blendfac;
559 /* clamp to between 0.0 and 1.0, using the wrap mode */
560 if (brush->wrap == WrapModeTile)
562 position = fmodf(position, 1.0f);
563 if (position < 0.0f) position += 1.0f;
565 else /* WrapModeFlip* */
567 position = fmodf(position, 2.0f);
568 if (position < 0.0f) position += 2.0f;
569 if (position > 1.0f) position = 2.0f - position;
572 if (brush->blendcount == 1)
573 blendfac = position;
574 else
576 int i=1;
577 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
578 REAL range;
580 /* locate the blend positions surrounding this position */
581 while (position > brush->blendpos[i])
582 i++;
584 /* interpolate between the blend positions */
585 left_blendpos = brush->blendpos[i-1];
586 left_blendfac = brush->blendfac[i-1];
587 right_blendpos = brush->blendpos[i];
588 right_blendfac = brush->blendfac[i];
589 range = right_blendpos - left_blendpos;
590 blendfac = (left_blendfac * (right_blendpos - position) +
591 right_blendfac * (position - left_blendpos)) / range;
594 if (brush->pblendcount == 0)
595 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
596 else
598 int i=1;
599 ARGB left_blendcolor, right_blendcolor;
600 REAL left_blendpos, right_blendpos;
602 /* locate the blend colors surrounding this position */
603 while (blendfac > brush->pblendpos[i])
604 i++;
606 /* interpolate between the blend colors */
607 left_blendpos = brush->pblendpos[i-1];
608 left_blendcolor = brush->pblendcolor[i-1];
609 right_blendpos = brush->pblendpos[i];
610 right_blendcolor = brush->pblendcolor[i];
611 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
612 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
616 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
618 REAL val[5], res[4];
619 int i, j;
620 unsigned char a, r, g, b;
622 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
623 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
624 val[2] = (color & 0xff) / 255.0; /* blue */
625 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
626 val[4] = 1.0; /* translation */
628 for (i=0; i<4; i++)
630 res[i] = 0.0;
632 for (j=0; j<5; j++)
633 res[i] += matrix->m[j][i] * val[j];
636 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
637 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
638 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
639 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
641 return (a << 24) | (r << 16) | (g << 8) | b;
644 static int color_is_gray(ARGB color)
646 unsigned char r, g, b;
648 r = (color >> 16) & 0xff;
649 g = (color >> 8) & 0xff;
650 b = color & 0xff;
652 return (r == g) && (g == b);
655 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
656 UINT width, UINT height, INT stride, ColorAdjustType type)
658 UINT x, y;
659 INT i;
661 if (attributes->colorkeys[type].enabled ||
662 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
664 const struct color_key *key;
665 BYTE min_blue, min_green, min_red;
666 BYTE max_blue, max_green, max_red;
668 if (attributes->colorkeys[type].enabled)
669 key = &attributes->colorkeys[type];
670 else
671 key = &attributes->colorkeys[ColorAdjustTypeDefault];
673 min_blue = key->low&0xff;
674 min_green = (key->low>>8)&0xff;
675 min_red = (key->low>>16)&0xff;
677 max_blue = key->high&0xff;
678 max_green = (key->high>>8)&0xff;
679 max_red = (key->high>>16)&0xff;
681 for (x=0; x<width; x++)
682 for (y=0; y<height; y++)
684 ARGB *src_color;
685 BYTE blue, green, red;
686 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
687 blue = *src_color&0xff;
688 green = (*src_color>>8)&0xff;
689 red = (*src_color>>16)&0xff;
690 if (blue >= min_blue && green >= min_green && red >= min_red &&
691 blue <= max_blue && green <= max_green && red <= max_red)
692 *src_color = 0x00000000;
696 if (attributes->colorremaptables[type].enabled ||
697 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
699 const struct color_remap_table *table;
701 if (attributes->colorremaptables[type].enabled)
702 table = &attributes->colorremaptables[type];
703 else
704 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
706 for (x=0; x<width; x++)
707 for (y=0; y<height; y++)
709 ARGB *src_color;
710 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
711 for (i=0; i<table->mapsize; i++)
713 if (*src_color == table->colormap[i].oldColor.Argb)
715 *src_color = table->colormap[i].newColor.Argb;
716 break;
722 if (attributes->colormatrices[type].enabled ||
723 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
725 const struct color_matrix *colormatrices;
727 if (attributes->colormatrices[type].enabled)
728 colormatrices = &attributes->colormatrices[type];
729 else
730 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
732 for (x=0; x<width; x++)
733 for (y=0; y<height; y++)
735 ARGB *src_color;
736 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
738 if (colormatrices->flags == ColorMatrixFlagsDefault ||
739 !color_is_gray(*src_color))
741 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
743 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
745 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
750 if (attributes->gamma_enabled[type] ||
751 attributes->gamma_enabled[ColorAdjustTypeDefault])
753 REAL gamma;
755 if (attributes->gamma_enabled[type])
756 gamma = attributes->gamma[type];
757 else
758 gamma = attributes->gamma[ColorAdjustTypeDefault];
760 for (x=0; x<width; x++)
761 for (y=0; y<height; y++)
763 ARGB *src_color;
764 BYTE blue, green, red;
765 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
767 blue = *src_color&0xff;
768 green = (*src_color>>8)&0xff;
769 red = (*src_color>>16)&0xff;
771 /* FIXME: We should probably use a table for this. */
772 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
773 green = floorf(powf(green / 255.0, gamma) * 255.0);
774 red = floorf(powf(red / 255.0, gamma) * 255.0);
776 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
781 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
782 * bitmap that contains all the pixels we may need to draw it. */
783 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
784 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
785 GpRect *rect)
787 INT left, top, right, bottom;
789 switch (interpolation)
791 case InterpolationModeHighQualityBilinear:
792 case InterpolationModeHighQualityBicubic:
793 /* FIXME: Include a greater range for the prefilter? */
794 case InterpolationModeBicubic:
795 case InterpolationModeBilinear:
796 left = (INT)(floorf(srcx));
797 top = (INT)(floorf(srcy));
798 right = (INT)(ceilf(srcx+srcwidth));
799 bottom = (INT)(ceilf(srcy+srcheight));
800 break;
801 case InterpolationModeNearestNeighbor:
802 default:
803 left = gdip_round(srcx);
804 top = gdip_round(srcy);
805 right = gdip_round(srcx+srcwidth);
806 bottom = gdip_round(srcy+srcheight);
807 break;
810 if (wrap == WrapModeClamp)
812 if (left < 0)
813 left = 0;
814 if (top < 0)
815 top = 0;
816 if (right >= bitmap->width)
817 right = bitmap->width-1;
818 if (bottom >= bitmap->height)
819 bottom = bitmap->height-1;
821 else
823 /* In some cases we can make the rectangle smaller here, but the logic
824 * is hard to get right, and tiling suggests we're likely to use the
825 * entire source image. */
826 if (left < 0 || right >= bitmap->width)
828 left = 0;
829 right = bitmap->width-1;
832 if (top < 0 || bottom >= bitmap->height)
834 top = 0;
835 bottom = bitmap->height-1;
839 rect->X = left;
840 rect->Y = top;
841 rect->Width = right - left + 1;
842 rect->Height = bottom - top + 1;
845 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
846 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
848 if (attributes->wrap == WrapModeClamp)
850 if (x < 0 || y < 0 || x >= width || y >= height)
851 return attributes->outside_color;
853 else
855 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
856 if (x < 0)
857 x = width*2 + x % (width * 2);
858 if (y < 0)
859 y = height*2 + y % (height * 2);
861 if ((attributes->wrap & 1) == 1)
863 /* Flip X */
864 if ((x / width) % 2 == 0)
865 x = x % width;
866 else
867 x = width - 1 - x % width;
869 else
870 x = x % width;
872 if ((attributes->wrap & 2) == 2)
874 /* Flip Y */
875 if ((y / height) % 2 == 0)
876 y = y % height;
877 else
878 y = height - 1 - y % height;
880 else
881 y = y % height;
884 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
886 ERR("out of range pixel requested\n");
887 return 0xffcd0084;
890 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
893 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
894 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
895 InterpolationMode interpolation, PixelOffsetMode offset_mode)
897 static int fixme;
899 switch (interpolation)
901 default:
902 if (!fixme++)
903 FIXME("Unimplemented interpolation %i\n", interpolation);
904 /* fall-through */
905 case InterpolationModeBilinear:
907 REAL leftxf, topyf;
908 INT leftx, rightx, topy, bottomy;
909 ARGB topleft, topright, bottomleft, bottomright;
910 ARGB top, bottom;
911 float x_offset;
913 leftxf = floorf(point->X);
914 leftx = (INT)leftxf;
915 rightx = (INT)ceilf(point->X);
916 topyf = floorf(point->Y);
917 topy = (INT)topyf;
918 bottomy = (INT)ceilf(point->Y);
920 if (leftx == rightx && topy == bottomy)
921 return sample_bitmap_pixel(src_rect, bits, width, height,
922 leftx, topy, attributes);
924 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
925 leftx, topy, attributes);
926 topright = sample_bitmap_pixel(src_rect, bits, width, height,
927 rightx, topy, attributes);
928 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
929 leftx, bottomy, attributes);
930 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
931 rightx, bottomy, attributes);
933 x_offset = point->X - leftxf;
934 top = blend_colors(topleft, topright, x_offset);
935 bottom = blend_colors(bottomleft, bottomright, x_offset);
937 return blend_colors(top, bottom, point->Y - topyf);
939 case InterpolationModeNearestNeighbor:
941 FLOAT pixel_offset;
942 switch (offset_mode)
944 default:
945 case PixelOffsetModeNone:
946 case PixelOffsetModeHighSpeed:
947 pixel_offset = 0.5;
948 break;
950 case PixelOffsetModeHalf:
951 case PixelOffsetModeHighQuality:
952 pixel_offset = 0.0;
953 break;
955 return sample_bitmap_pixel(src_rect, bits, width, height,
956 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
962 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
964 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
967 static INT brush_can_fill_path(GpBrush *brush)
969 switch (brush->bt)
971 case BrushTypeSolidColor:
972 return 1;
973 case BrushTypeHatchFill:
975 GpHatch *hatch = (GpHatch*)brush;
976 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
977 ((hatch->backcol & 0xff000000) == 0xff000000);
979 case BrushTypeLinearGradient:
980 case BrushTypeTextureFill:
981 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
982 default:
983 return 0;
987 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
989 switch (brush->bt)
991 case BrushTypeSolidColor:
993 GpSolidFill *fill = (GpSolidFill*)brush;
994 HBITMAP bmp = ARGB2BMP(fill->color);
996 if (bmp)
998 RECT rc;
999 /* partially transparent fill */
1001 SelectClipPath(graphics->hdc, RGN_AND);
1002 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1004 HDC hdc = CreateCompatibleDC(NULL);
1006 if (!hdc) break;
1008 SelectObject(hdc, bmp);
1009 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1010 hdc, 0, 0, 1, 1);
1011 DeleteDC(hdc);
1014 DeleteObject(bmp);
1015 break;
1017 /* else fall through */
1019 default:
1021 HBRUSH gdibrush, old_brush;
1023 gdibrush = create_gdi_brush(brush);
1024 if (!gdibrush) return;
1026 old_brush = SelectObject(graphics->hdc, gdibrush);
1027 FillPath(graphics->hdc);
1028 SelectObject(graphics->hdc, old_brush);
1029 DeleteObject(gdibrush);
1030 break;
1035 static INT brush_can_fill_pixels(GpBrush *brush)
1037 switch (brush->bt)
1039 case BrushTypeSolidColor:
1040 case BrushTypeHatchFill:
1041 case BrushTypeLinearGradient:
1042 case BrushTypeTextureFill:
1043 case BrushTypePathGradient:
1044 return 1;
1045 default:
1046 return 0;
1050 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1051 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1053 switch (brush->bt)
1055 case BrushTypeSolidColor:
1057 int x, y;
1058 GpSolidFill *fill = (GpSolidFill*)brush;
1059 for (x=0; x<fill_area->Width; x++)
1060 for (y=0; y<fill_area->Height; y++)
1061 argb_pixels[x + y*cdwStride] = fill->color;
1062 return Ok;
1064 case BrushTypeHatchFill:
1066 int x, y;
1067 GpHatch *fill = (GpHatch*)brush;
1068 const char *hatch_data;
1070 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1071 return NotImplemented;
1073 for (x=0; x<fill_area->Width; x++)
1074 for (y=0; y<fill_area->Height; y++)
1076 int hx, hy;
1078 /* FIXME: Account for the rendering origin */
1079 hx = (x + fill_area->X) % 8;
1080 hy = (y + fill_area->Y) % 8;
1082 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1083 argb_pixels[x + y*cdwStride] = fill->forecol;
1084 else
1085 argb_pixels[x + y*cdwStride] = fill->backcol;
1088 return Ok;
1090 case BrushTypeLinearGradient:
1092 GpLineGradient *fill = (GpLineGradient*)brush;
1093 GpPointF draw_points[3], line_points[3];
1094 GpStatus stat;
1095 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1096 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1097 int x, y;
1099 draw_points[0].X = fill_area->X;
1100 draw_points[0].Y = fill_area->Y;
1101 draw_points[1].X = fill_area->X+1;
1102 draw_points[1].Y = fill_area->Y;
1103 draw_points[2].X = fill_area->X;
1104 draw_points[2].Y = fill_area->Y+1;
1106 /* Transform the points to a co-ordinate space where X is the point's
1107 * position in the gradient, 0.0 being the start point and 1.0 the
1108 * end point. */
1109 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1110 CoordinateSpaceDevice, draw_points, 3);
1112 if (stat == Ok)
1114 line_points[0] = fill->startpoint;
1115 line_points[1] = fill->endpoint;
1116 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1117 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1119 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1122 if (stat == Ok)
1124 stat = GdipInvertMatrix(world_to_gradient);
1126 if (stat == Ok)
1127 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1129 GdipDeleteMatrix(world_to_gradient);
1132 if (stat == Ok)
1134 REAL x_delta = draw_points[1].X - draw_points[0].X;
1135 REAL y_delta = draw_points[2].X - draw_points[0].X;
1137 for (y=0; y<fill_area->Height; y++)
1139 for (x=0; x<fill_area->Width; x++)
1141 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1143 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1148 return stat;
1150 case BrushTypeTextureFill:
1152 GpTexture *fill = (GpTexture*)brush;
1153 GpPointF draw_points[3];
1154 GpStatus stat;
1155 int x, y;
1156 GpBitmap *bitmap;
1157 int src_stride;
1158 GpRect src_area;
1160 if (fill->image->type != ImageTypeBitmap)
1162 FIXME("metafile texture brushes not implemented\n");
1163 return NotImplemented;
1166 bitmap = (GpBitmap*)fill->image;
1167 src_stride = sizeof(ARGB) * bitmap->width;
1169 src_area.X = src_area.Y = 0;
1170 src_area.Width = bitmap->width;
1171 src_area.Height = bitmap->height;
1173 draw_points[0].X = fill_area->X;
1174 draw_points[0].Y = fill_area->Y;
1175 draw_points[1].X = fill_area->X+1;
1176 draw_points[1].Y = fill_area->Y;
1177 draw_points[2].X = fill_area->X;
1178 draw_points[2].Y = fill_area->Y+1;
1180 /* Transform the points to the co-ordinate space of the bitmap. */
1181 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1182 CoordinateSpaceDevice, draw_points, 3);
1184 if (stat == Ok)
1186 GpMatrix world_to_texture = fill->transform;
1188 stat = GdipInvertMatrix(&world_to_texture);
1189 if (stat == Ok)
1190 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1193 if (stat == Ok && !fill->bitmap_bits)
1195 BitmapData lockeddata;
1197 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1198 if (!fill->bitmap_bits)
1199 stat = OutOfMemory;
1201 if (stat == Ok)
1203 lockeddata.Width = bitmap->width;
1204 lockeddata.Height = bitmap->height;
1205 lockeddata.Stride = src_stride;
1206 lockeddata.PixelFormat = PixelFormat32bppARGB;
1207 lockeddata.Scan0 = fill->bitmap_bits;
1209 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1210 PixelFormat32bppARGB, &lockeddata);
1213 if (stat == Ok)
1214 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1216 if (stat == Ok)
1217 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1218 bitmap->width, bitmap->height,
1219 src_stride, ColorAdjustTypeBitmap);
1221 if (stat != Ok)
1223 GdipFree(fill->bitmap_bits);
1224 fill->bitmap_bits = NULL;
1228 if (stat == Ok)
1230 REAL x_dx = draw_points[1].X - draw_points[0].X;
1231 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1232 REAL y_dx = draw_points[2].X - draw_points[0].X;
1233 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1235 for (y=0; y<fill_area->Height; y++)
1237 for (x=0; x<fill_area->Width; x++)
1239 GpPointF point;
1240 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1241 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1243 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1244 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1245 &point, fill->imageattributes, graphics->interpolation,
1246 graphics->pixeloffset);
1251 return stat;
1253 case BrushTypePathGradient:
1255 GpPathGradient *fill = (GpPathGradient*)brush;
1256 GpPath *flat_path;
1257 GpMatrix world_to_device;
1258 GpStatus stat;
1259 int i, figure_start=0;
1260 GpPointF start_point, end_point, center_point;
1261 BYTE type;
1262 REAL min_yf, max_yf, line1_xf, line2_xf;
1263 INT min_y, max_y, min_x, max_x;
1264 INT x, y;
1265 ARGB outer_color;
1266 static int transform_fixme_once;
1268 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1270 static int once;
1271 if (!once++)
1272 FIXME("path gradient focus not implemented\n");
1275 if (fill->gamma)
1277 static int once;
1278 if (!once++)
1279 FIXME("path gradient gamma correction not implemented\n");
1282 if (fill->blendcount)
1284 static int once;
1285 if (!once++)
1286 FIXME("path gradient blend not implemented\n");
1289 if (fill->pblendcount)
1291 static int once;
1292 if (!once++)
1293 FIXME("path gradient preset blend not implemented\n");
1296 if (!transform_fixme_once)
1298 BOOL is_identity=TRUE;
1299 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1300 if (!is_identity)
1302 FIXME("path gradient transform not implemented\n");
1303 transform_fixme_once = 1;
1307 stat = GdipClonePath(fill->path, &flat_path);
1309 if (stat != Ok)
1310 return stat;
1312 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1313 CoordinateSpaceWorld, &world_to_device);
1314 if (stat == Ok)
1316 stat = GdipTransformPath(flat_path, &world_to_device);
1318 if (stat == Ok)
1320 center_point = fill->center;
1321 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1324 if (stat == Ok)
1325 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1328 if (stat != Ok)
1330 GdipDeletePath(flat_path);
1331 return stat;
1334 for (i=0; i<flat_path->pathdata.Count; i++)
1336 int start_center_line=0, end_center_line=0;
1337 int seen_start=0, seen_end=0, seen_center=0;
1338 REAL center_distance;
1339 ARGB start_color, end_color;
1340 REAL dy, dx;
1342 type = flat_path->pathdata.Types[i];
1344 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1345 figure_start = i;
1347 start_point = flat_path->pathdata.Points[i];
1349 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1351 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1353 end_point = flat_path->pathdata.Points[figure_start];
1354 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1356 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1358 end_point = flat_path->pathdata.Points[i+1];
1359 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1361 else
1362 continue;
1364 outer_color = start_color;
1366 min_yf = center_point.Y;
1367 if (min_yf > start_point.Y) min_yf = start_point.Y;
1368 if (min_yf > end_point.Y) min_yf = end_point.Y;
1370 if (min_yf < fill_area->Y)
1371 min_y = fill_area->Y;
1372 else
1373 min_y = (INT)ceil(min_yf);
1375 max_yf = center_point.Y;
1376 if (max_yf < start_point.Y) max_yf = start_point.Y;
1377 if (max_yf < end_point.Y) max_yf = end_point.Y;
1379 if (max_yf > fill_area->Y + fill_area->Height)
1380 max_y = fill_area->Y + fill_area->Height;
1381 else
1382 max_y = (INT)ceil(max_yf);
1384 dy = end_point.Y - start_point.Y;
1385 dx = end_point.X - start_point.X;
1387 /* This is proportional to the distance from start-end line to center point. */
1388 center_distance = dy * (start_point.X - center_point.X) +
1389 dx * (center_point.Y - start_point.Y);
1391 for (y=min_y; y<max_y; y++)
1393 REAL yf = (REAL)y;
1395 if (!seen_start && yf >= start_point.Y)
1397 seen_start = 1;
1398 start_center_line ^= 1;
1400 if (!seen_end && yf >= end_point.Y)
1402 seen_end = 1;
1403 end_center_line ^= 1;
1405 if (!seen_center && yf >= center_point.Y)
1407 seen_center = 1;
1408 start_center_line ^= 1;
1409 end_center_line ^= 1;
1412 if (start_center_line)
1413 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1414 else
1415 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1417 if (end_center_line)
1418 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1419 else
1420 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1422 if (line1_xf < line2_xf)
1424 min_x = (INT)ceil(line1_xf);
1425 max_x = (INT)ceil(line2_xf);
1427 else
1429 min_x = (INT)ceil(line2_xf);
1430 max_x = (INT)ceil(line1_xf);
1433 if (min_x < fill_area->X)
1434 min_x = fill_area->X;
1435 if (max_x > fill_area->X + fill_area->Width)
1436 max_x = fill_area->X + fill_area->Width;
1438 for (x=min_x; x<max_x; x++)
1440 REAL xf = (REAL)x;
1441 REAL distance;
1443 if (start_color != end_color)
1445 REAL blend_amount, pdy, pdx;
1446 pdy = yf - center_point.Y;
1447 pdx = xf - center_point.X;
1448 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1449 outer_color = blend_colors(start_color, end_color, blend_amount);
1452 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1453 (end_point.X - start_point.X) * (yf - start_point.Y);
1455 distance = distance / center_distance;
1457 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1458 blend_colors(outer_color, fill->centercolor, distance);
1463 GdipDeletePath(flat_path);
1464 return stat;
1466 default:
1467 return NotImplemented;
1471 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1472 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1473 * should not be called on an hdc that has a path you care about. */
1474 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1475 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1477 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1478 GpMatrix matrix;
1479 HBRUSH brush = NULL;
1480 HPEN pen = NULL;
1481 PointF ptf[4], *custptf = NULL;
1482 POINT pt[4], *custpt = NULL;
1483 BYTE *tp = NULL;
1484 REAL theta, dsmall, dbig, dx, dy = 0.0;
1485 INT i, count;
1486 LOGBRUSH lb;
1487 BOOL customstroke;
1489 if((x1 == x2) && (y1 == y2))
1490 return;
1492 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1494 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1495 if(!customstroke){
1496 brush = CreateSolidBrush(color);
1497 lb.lbStyle = BS_SOLID;
1498 lb.lbColor = color;
1499 lb.lbHatch = 0;
1500 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1501 PS_JOIN_MITER, 1, &lb, 0,
1502 NULL);
1503 oldbrush = SelectObject(graphics->hdc, brush);
1504 oldpen = SelectObject(graphics->hdc, pen);
1507 switch(cap){
1508 case LineCapFlat:
1509 break;
1510 case LineCapSquare:
1511 case LineCapSquareAnchor:
1512 case LineCapDiamondAnchor:
1513 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1514 if(cap == LineCapDiamondAnchor){
1515 dsmall = cos(theta + M_PI_2) * size;
1516 dbig = sin(theta + M_PI_2) * size;
1518 else{
1519 dsmall = cos(theta + M_PI_4) * size;
1520 dbig = sin(theta + M_PI_4) * size;
1523 ptf[0].X = x2 - dsmall;
1524 ptf[1].X = x2 + dbig;
1526 ptf[0].Y = y2 - dbig;
1527 ptf[3].Y = y2 + dsmall;
1529 ptf[1].Y = y2 - dsmall;
1530 ptf[2].Y = y2 + dbig;
1532 ptf[3].X = x2 - dbig;
1533 ptf[2].X = x2 + dsmall;
1535 transform_and_round_points(graphics, pt, ptf, 4);
1536 Polygon(graphics->hdc, pt, 4);
1538 break;
1539 case LineCapArrowAnchor:
1540 size = size * 4.0 / sqrt(3.0);
1542 dx = cos(M_PI / 6.0 + theta) * size;
1543 dy = sin(M_PI / 6.0 + theta) * size;
1545 ptf[0].X = x2 - dx;
1546 ptf[0].Y = y2 - dy;
1548 dx = cos(- M_PI / 6.0 + theta) * size;
1549 dy = sin(- M_PI / 6.0 + theta) * size;
1551 ptf[1].X = x2 - dx;
1552 ptf[1].Y = y2 - dy;
1554 ptf[2].X = x2;
1555 ptf[2].Y = y2;
1557 transform_and_round_points(graphics, pt, ptf, 3);
1558 Polygon(graphics->hdc, pt, 3);
1560 break;
1561 case LineCapRoundAnchor:
1562 dx = dy = ANCHOR_WIDTH * size / 2.0;
1564 ptf[0].X = x2 - dx;
1565 ptf[0].Y = y2 - dy;
1566 ptf[1].X = x2 + dx;
1567 ptf[1].Y = y2 + dy;
1569 transform_and_round_points(graphics, pt, ptf, 2);
1570 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1572 break;
1573 case LineCapTriangle:
1574 size = size / 2.0;
1575 dx = cos(M_PI_2 + theta) * size;
1576 dy = sin(M_PI_2 + theta) * size;
1578 ptf[0].X = x2 - dx;
1579 ptf[0].Y = y2 - dy;
1580 ptf[1].X = x2 + dx;
1581 ptf[1].Y = y2 + dy;
1583 dx = cos(theta) * size;
1584 dy = sin(theta) * size;
1586 ptf[2].X = x2 + dx;
1587 ptf[2].Y = y2 + dy;
1589 transform_and_round_points(graphics, pt, ptf, 3);
1590 Polygon(graphics->hdc, pt, 3);
1592 break;
1593 case LineCapRound:
1594 dx = dy = size / 2.0;
1596 ptf[0].X = x2 - dx;
1597 ptf[0].Y = y2 - dy;
1598 ptf[1].X = x2 + dx;
1599 ptf[1].Y = y2 + dy;
1601 dx = -cos(M_PI_2 + theta) * size;
1602 dy = -sin(M_PI_2 + theta) * size;
1604 ptf[2].X = x2 - dx;
1605 ptf[2].Y = y2 - dy;
1606 ptf[3].X = x2 + dx;
1607 ptf[3].Y = y2 + dy;
1609 transform_and_round_points(graphics, pt, ptf, 4);
1610 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1611 pt[2].y, pt[3].x, pt[3].y);
1613 break;
1614 case LineCapCustom:
1615 if(!custom)
1616 break;
1618 count = custom->pathdata.Count;
1619 custptf = GdipAlloc(count * sizeof(PointF));
1620 custpt = GdipAlloc(count * sizeof(POINT));
1621 tp = GdipAlloc(count);
1623 if(!custptf || !custpt || !tp)
1624 goto custend;
1626 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1628 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1629 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1630 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1631 MatrixOrderAppend);
1632 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1633 GdipTransformMatrixPoints(&matrix, custptf, count);
1635 transform_and_round_points(graphics, custpt, custptf, count);
1637 for(i = 0; i < count; i++)
1638 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1640 if(custom->fill){
1641 BeginPath(graphics->hdc);
1642 PolyDraw(graphics->hdc, custpt, tp, count);
1643 EndPath(graphics->hdc);
1644 StrokeAndFillPath(graphics->hdc);
1646 else
1647 PolyDraw(graphics->hdc, custpt, tp, count);
1649 custend:
1650 GdipFree(custptf);
1651 GdipFree(custpt);
1652 GdipFree(tp);
1653 break;
1654 default:
1655 break;
1658 if(!customstroke){
1659 SelectObject(graphics->hdc, oldbrush);
1660 SelectObject(graphics->hdc, oldpen);
1661 DeleteObject(brush);
1662 DeleteObject(pen);
1666 /* Shortens the line by the given percent by changing x2, y2.
1667 * If percent is > 1.0 then the line will change direction.
1668 * If percent is negative it can lengthen the line. */
1669 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1671 REAL dist, theta, dx, dy;
1673 if((y1 == *y2) && (x1 == *x2))
1674 return;
1676 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1677 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1678 dx = cos(theta) * dist;
1679 dy = sin(theta) * dist;
1681 *x2 = *x2 + dx;
1682 *y2 = *y2 + dy;
1685 /* Shortens the line by the given amount by changing x2, y2.
1686 * If the amount is greater than the distance, the line will become length 0.
1687 * If the amount is negative, it can lengthen the line. */
1688 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1690 REAL dx, dy, percent;
1692 dx = *x2 - x1;
1693 dy = *y2 - y1;
1694 if(dx == 0 && dy == 0)
1695 return;
1697 percent = amt / sqrt(dx * dx + dy * dy);
1698 if(percent >= 1.0){
1699 *x2 = x1;
1700 *y2 = y1;
1701 return;
1704 shorten_line_percent(x1, y1, x2, y2, percent);
1707 /* Conducts a linear search to find the bezier points that will back off
1708 * the endpoint of the curve by a distance of amt. Linear search works
1709 * better than binary in this case because there are multiple solutions,
1710 * and binary searches often find a bad one. I don't think this is what
1711 * Windows does but short of rendering the bezier without GDI's help it's
1712 * the best we can do. If rev then work from the start of the passed points
1713 * instead of the end. */
1714 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1716 GpPointF origpt[4];
1717 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1718 INT i, first = 0, second = 1, third = 2, fourth = 3;
1720 if(rev){
1721 first = 3;
1722 second = 2;
1723 third = 1;
1724 fourth = 0;
1727 origx = pt[fourth].X;
1728 origy = pt[fourth].Y;
1729 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1731 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1732 /* reset bezier points to original values */
1733 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1734 /* Perform magic on bezier points. Order is important here.*/
1735 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1736 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1737 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1738 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1739 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1740 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1742 dx = pt[fourth].X - origx;
1743 dy = pt[fourth].Y - origy;
1745 diff = sqrt(dx * dx + dy * dy);
1746 percent += 0.0005 * amt;
1750 /* Draws a combination of bezier curves and lines between points. */
1751 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1752 GDIPCONST BYTE * types, INT count, BOOL caps)
1754 POINT *pti = GdipAlloc(count * sizeof(POINT));
1755 BYTE *tp = GdipAlloc(count);
1756 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1757 INT i, j;
1758 GpStatus status = GenericError;
1760 if(!count){
1761 status = Ok;
1762 goto end;
1764 if(!pti || !tp || !ptcopy){
1765 status = OutOfMemory;
1766 goto end;
1769 for(i = 1; i < count; i++){
1770 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1771 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1772 || !(types[i + 1] & PathPointTypeBezier)){
1773 ERR("Bad bezier points\n");
1774 goto end;
1776 i += 2;
1780 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1782 /* If we are drawing caps, go through the points and adjust them accordingly,
1783 * and draw the caps. */
1784 if(caps){
1785 switch(types[count - 1] & PathPointTypePathTypeMask){
1786 case PathPointTypeBezier:
1787 if(pen->endcap == LineCapArrowAnchor)
1788 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1789 else if((pen->endcap == LineCapCustom) && pen->customend)
1790 shorten_bezier_amt(&ptcopy[count - 4],
1791 pen->width * pen->customend->inset, FALSE);
1793 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1794 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1795 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1796 pt[count - 1].X, pt[count - 1].Y);
1798 break;
1799 case PathPointTypeLine:
1800 if(pen->endcap == LineCapArrowAnchor)
1801 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1802 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1803 pen->width);
1804 else if((pen->endcap == LineCapCustom) && pen->customend)
1805 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1806 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1807 pen->customend->inset * pen->width);
1809 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1810 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1811 pt[count - 1].Y);
1813 break;
1814 default:
1815 ERR("Bad path last point\n");
1816 goto end;
1819 /* Find start of points */
1820 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1821 == PathPointTypeStart); j++);
1823 switch(types[j] & PathPointTypePathTypeMask){
1824 case PathPointTypeBezier:
1825 if(pen->startcap == LineCapArrowAnchor)
1826 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1827 else if((pen->startcap == LineCapCustom) && pen->customstart)
1828 shorten_bezier_amt(&ptcopy[j - 1],
1829 pen->width * pen->customstart->inset, TRUE);
1831 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1832 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1833 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1834 pt[j - 1].X, pt[j - 1].Y);
1836 break;
1837 case PathPointTypeLine:
1838 if(pen->startcap == LineCapArrowAnchor)
1839 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1840 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1841 pen->width);
1842 else if((pen->startcap == LineCapCustom) && pen->customstart)
1843 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1844 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1845 pen->customstart->inset * pen->width);
1847 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1848 pt[j].X, pt[j].Y, pt[j - 1].X,
1849 pt[j - 1].Y);
1851 break;
1852 default:
1853 ERR("Bad path points\n");
1854 goto end;
1858 transform_and_round_points(graphics, pti, ptcopy, count);
1860 for(i = 0; i < count; i++){
1861 tp[i] = convert_path_point_type(types[i]);
1864 PolyDraw(graphics->hdc, pti, tp, count);
1866 status = Ok;
1868 end:
1869 GdipFree(pti);
1870 GdipFree(ptcopy);
1871 GdipFree(tp);
1873 return status;
1876 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1878 GpStatus result;
1880 BeginPath(graphics->hdc);
1881 result = draw_poly(graphics, NULL, path->pathdata.Points,
1882 path->pathdata.Types, path->pathdata.Count, FALSE);
1883 EndPath(graphics->hdc);
1884 return result;
1887 typedef struct _GraphicsContainerItem {
1888 struct list entry;
1889 GraphicsContainer contid;
1891 SmoothingMode smoothing;
1892 CompositingQuality compqual;
1893 InterpolationMode interpolation;
1894 CompositingMode compmode;
1895 TextRenderingHint texthint;
1896 REAL scale;
1897 GpUnit unit;
1898 PixelOffsetMode pixeloffset;
1899 UINT textcontrast;
1900 GpMatrix worldtrans;
1901 GpRegion* clip;
1902 INT origin_x, origin_y;
1903 } GraphicsContainerItem;
1905 static GpStatus init_container(GraphicsContainerItem** container,
1906 GDIPCONST GpGraphics* graphics){
1907 GpStatus sts;
1909 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1910 if(!(*container))
1911 return OutOfMemory;
1913 (*container)->contid = graphics->contid + 1;
1915 (*container)->smoothing = graphics->smoothing;
1916 (*container)->compqual = graphics->compqual;
1917 (*container)->interpolation = graphics->interpolation;
1918 (*container)->compmode = graphics->compmode;
1919 (*container)->texthint = graphics->texthint;
1920 (*container)->scale = graphics->scale;
1921 (*container)->unit = graphics->unit;
1922 (*container)->textcontrast = graphics->textcontrast;
1923 (*container)->pixeloffset = graphics->pixeloffset;
1924 (*container)->origin_x = graphics->origin_x;
1925 (*container)->origin_y = graphics->origin_y;
1926 (*container)->worldtrans = graphics->worldtrans;
1928 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1929 if(sts != Ok){
1930 GdipFree(*container);
1931 *container = NULL;
1932 return sts;
1935 return Ok;
1938 static void delete_container(GraphicsContainerItem* container)
1940 GdipDeleteRegion(container->clip);
1941 GdipFree(container);
1944 static GpStatus restore_container(GpGraphics* graphics,
1945 GDIPCONST GraphicsContainerItem* container){
1946 GpStatus sts;
1947 GpRegion *newClip;
1949 sts = GdipCloneRegion(container->clip, &newClip);
1950 if(sts != Ok) return sts;
1952 graphics->worldtrans = container->worldtrans;
1954 GdipDeleteRegion(graphics->clip);
1955 graphics->clip = newClip;
1957 graphics->contid = container->contid - 1;
1959 graphics->smoothing = container->smoothing;
1960 graphics->compqual = container->compqual;
1961 graphics->interpolation = container->interpolation;
1962 graphics->compmode = container->compmode;
1963 graphics->texthint = container->texthint;
1964 graphics->scale = container->scale;
1965 graphics->unit = container->unit;
1966 graphics->textcontrast = container->textcontrast;
1967 graphics->pixeloffset = container->pixeloffset;
1968 graphics->origin_x = container->origin_x;
1969 graphics->origin_y = container->origin_y;
1971 return Ok;
1974 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1976 RECT wnd_rect;
1977 GpStatus stat=Ok;
1978 GpUnit unit;
1980 if(graphics->hwnd) {
1981 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1982 return GenericError;
1984 rect->X = wnd_rect.left;
1985 rect->Y = wnd_rect.top;
1986 rect->Width = wnd_rect.right - wnd_rect.left;
1987 rect->Height = wnd_rect.bottom - wnd_rect.top;
1988 }else if (graphics->image){
1989 stat = GdipGetImageBounds(graphics->image, rect, &unit);
1990 if (stat == Ok && unit != UnitPixel)
1991 FIXME("need to convert from unit %i\n", unit);
1992 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
1993 HBITMAP hbmp;
1994 BITMAP bmp;
1996 rect->X = 0;
1997 rect->Y = 0;
1999 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2000 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2002 rect->Width = bmp.bmWidth;
2003 rect->Height = bmp.bmHeight;
2005 else
2007 /* FIXME: ??? */
2008 rect->Width = 1;
2009 rect->Height = 1;
2011 }else{
2012 rect->X = 0;
2013 rect->Y = 0;
2014 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2015 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2018 return stat;
2021 /* on success, rgn will contain the region of the graphics object which
2022 * is visible after clipping has been applied */
2023 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2025 GpStatus stat;
2026 GpRectF rectf;
2027 GpRegion* tmp;
2029 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2030 return stat;
2032 if((stat = GdipCreateRegion(&tmp)) != Ok)
2033 return stat;
2035 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2036 goto end;
2038 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2039 goto end;
2041 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2043 end:
2044 GdipDeleteRegion(tmp);
2045 return stat;
2048 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2050 REAL height;
2052 if (font->unit == UnitPixel)
2054 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2056 else
2058 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2059 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2060 else
2061 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2064 lf->lfHeight = -(height + 0.5);
2065 lf->lfWidth = 0;
2066 lf->lfEscapement = 0;
2067 lf->lfOrientation = 0;
2068 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2069 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2070 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2071 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2072 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2073 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2074 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2075 lf->lfQuality = DEFAULT_QUALITY;
2076 lf->lfPitchAndFamily = 0;
2077 strcpyW(lf->lfFaceName, font->family->FamilyName);
2080 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2081 GDIPCONST GpStringFormat *format, HFONT *hfont,
2082 GDIPCONST GpMatrix *matrix)
2084 HDC hdc = CreateCompatibleDC(0);
2085 GpPointF pt[3];
2086 REAL angle, rel_width, rel_height, font_height;
2087 LOGFONTW lfw;
2088 HFONT unscaled_font;
2089 TEXTMETRICW textmet;
2091 if (font->unit == UnitPixel)
2092 font_height = font->emSize;
2093 else
2095 REAL unit_scale, res;
2097 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2098 unit_scale = units_scale(font->unit, graphics->unit, res);
2100 font_height = font->emSize * unit_scale;
2103 pt[0].X = 0.0;
2104 pt[0].Y = 0.0;
2105 pt[1].X = 1.0;
2106 pt[1].Y = 0.0;
2107 pt[2].X = 0.0;
2108 pt[2].Y = 1.0;
2109 if (matrix)
2111 GpMatrix xform = *matrix;
2112 GdipTransformMatrixPoints(&xform, pt, 3);
2114 if (graphics)
2115 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2116 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2117 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2118 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2119 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2120 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2122 get_log_fontW(font, graphics, &lfw);
2123 lfw.lfHeight = -gdip_round(font_height * rel_height);
2124 unscaled_font = CreateFontIndirectW(&lfw);
2126 SelectObject(hdc, unscaled_font);
2127 GetTextMetricsW(hdc, &textmet);
2129 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2130 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2132 *hfont = CreateFontIndirectW(&lfw);
2134 DeleteDC(hdc);
2135 DeleteObject(unscaled_font);
2138 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2140 TRACE("(%p, %p)\n", hdc, graphics);
2142 return GdipCreateFromHDC2(hdc, NULL, graphics);
2145 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2147 GpStatus retval;
2148 HBITMAP hbitmap;
2149 DIBSECTION dib;
2151 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2153 if(hDevice != NULL)
2154 FIXME("Don't know how to handle parameter hDevice\n");
2156 if(hdc == NULL)
2157 return OutOfMemory;
2159 if(graphics == NULL)
2160 return InvalidParameter;
2162 *graphics = GdipAlloc(sizeof(GpGraphics));
2163 if(!*graphics) return OutOfMemory;
2165 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2167 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2168 GdipFree(*graphics);
2169 return retval;
2172 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2173 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2174 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2176 (*graphics)->alpha_hdc = 1;
2179 (*graphics)->hdc = hdc;
2180 (*graphics)->hwnd = WindowFromDC(hdc);
2181 (*graphics)->owndc = FALSE;
2182 (*graphics)->smoothing = SmoothingModeDefault;
2183 (*graphics)->compqual = CompositingQualityDefault;
2184 (*graphics)->interpolation = InterpolationModeBilinear;
2185 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2186 (*graphics)->compmode = CompositingModeSourceOver;
2187 (*graphics)->unit = UnitDisplay;
2188 (*graphics)->scale = 1.0;
2189 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2190 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2191 (*graphics)->busy = FALSE;
2192 (*graphics)->textcontrast = 4;
2193 list_init(&(*graphics)->containers);
2194 (*graphics)->contid = 0;
2196 TRACE("<-- %p\n", *graphics);
2198 return Ok;
2201 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2203 GpStatus retval;
2205 *graphics = GdipAlloc(sizeof(GpGraphics));
2206 if(!*graphics) return OutOfMemory;
2208 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2210 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2211 GdipFree(*graphics);
2212 return retval;
2215 (*graphics)->hdc = NULL;
2216 (*graphics)->hwnd = NULL;
2217 (*graphics)->owndc = FALSE;
2218 (*graphics)->image = image;
2219 /* We have to store the image type here because the image may be freed
2220 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2221 (*graphics)->image_type = image->type;
2222 (*graphics)->smoothing = SmoothingModeDefault;
2223 (*graphics)->compqual = CompositingQualityDefault;
2224 (*graphics)->interpolation = InterpolationModeBilinear;
2225 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2226 (*graphics)->compmode = CompositingModeSourceOver;
2227 (*graphics)->unit = UnitDisplay;
2228 (*graphics)->scale = 1.0;
2229 (*graphics)->xres = image->xres;
2230 (*graphics)->yres = image->yres;
2231 (*graphics)->busy = FALSE;
2232 (*graphics)->textcontrast = 4;
2233 list_init(&(*graphics)->containers);
2234 (*graphics)->contid = 0;
2236 TRACE("<-- %p\n", *graphics);
2238 return Ok;
2241 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2243 GpStatus ret;
2244 HDC hdc;
2246 TRACE("(%p, %p)\n", hwnd, graphics);
2248 hdc = GetDC(hwnd);
2250 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2252 ReleaseDC(hwnd, hdc);
2253 return ret;
2256 (*graphics)->hwnd = hwnd;
2257 (*graphics)->owndc = TRUE;
2259 return Ok;
2262 /* FIXME: no icm handling */
2263 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2265 TRACE("(%p, %p)\n", hwnd, graphics);
2267 return GdipCreateFromHWND(hwnd, graphics);
2270 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2271 GpMetafile **metafile)
2273 ENHMETAHEADER header;
2274 MetafileType metafile_type;
2276 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2278 if(!hemf || !metafile)
2279 return InvalidParameter;
2281 if (GetEnhMetaFileHeader(hemf, sizeof(header), &header) == 0)
2282 return GenericError;
2284 metafile_type = METAFILE_GetEmfType(hemf);
2286 if (metafile_type == MetafileTypeInvalid)
2287 return GenericError;
2289 *metafile = GdipAlloc(sizeof(GpMetafile));
2290 if (!*metafile)
2291 return OutOfMemory;
2293 (*metafile)->image.type = ImageTypeMetafile;
2294 (*metafile)->image.format = ImageFormatEMF;
2295 (*metafile)->image.frame_count = 1;
2296 (*metafile)->image.xres = (REAL)header.szlDevice.cx;
2297 (*metafile)->image.yres = (REAL)header.szlDevice.cy;
2298 (*metafile)->bounds.X = (REAL)header.rclBounds.left;
2299 (*metafile)->bounds.Y = (REAL)header.rclBounds.top;
2300 (*metafile)->bounds.Width = (REAL)(header.rclBounds.right - header.rclBounds.left);
2301 (*metafile)->bounds.Height = (REAL)(header.rclBounds.bottom - header.rclBounds.top);
2302 (*metafile)->unit = UnitPixel;
2303 (*metafile)->metafile_type = metafile_type;
2304 (*metafile)->hemf = hemf;
2305 (*metafile)->preserve_hemf = !delete;
2307 TRACE("<-- %p\n", *metafile);
2309 return Ok;
2312 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2313 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2315 UINT read;
2316 BYTE *copy;
2317 HENHMETAFILE hemf;
2318 GpStatus retval = Ok;
2320 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2322 if(!hwmf || !metafile || !placeable)
2323 return InvalidParameter;
2325 *metafile = NULL;
2326 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2327 if(!read)
2328 return GenericError;
2329 copy = GdipAlloc(read);
2330 GetMetaFileBitsEx(hwmf, read, copy);
2332 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2333 GdipFree(copy);
2335 /* FIXME: We should store and use hwmf instead of converting to hemf */
2336 retval = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
2338 if (retval == Ok)
2340 (*metafile)->image.xres = (REAL)placeable->Inch;
2341 (*metafile)->image.yres = (REAL)placeable->Inch;
2342 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2343 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2344 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2345 placeable->BoundingBox.Left);
2346 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2347 placeable->BoundingBox.Top);
2348 (*metafile)->metafile_type = MetafileTypeWmfPlaceable;
2349 (*metafile)->image.format = ImageFormatWMF;
2351 if (delete) DeleteMetaFile(hwmf);
2353 else
2354 DeleteEnhMetaFile(hemf);
2355 return retval;
2358 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2359 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2361 HMETAFILE hmf = GetMetaFileW(file);
2363 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2365 if(!hmf) return InvalidParameter;
2367 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2370 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2371 GpMetafile **metafile)
2373 FIXME("(%p, %p): stub\n", file, metafile);
2374 return NotImplemented;
2377 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2378 GpMetafile **metafile)
2380 FIXME("(%p, %p): stub\n", stream, metafile);
2381 return NotImplemented;
2384 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2385 UINT access, IStream **stream)
2387 DWORD dwMode;
2388 HRESULT ret;
2390 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2392 if(!stream || !filename)
2393 return InvalidParameter;
2395 if(access & GENERIC_WRITE)
2396 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2397 else if(access & GENERIC_READ)
2398 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2399 else
2400 return InvalidParameter;
2402 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2404 return hresult_to_status(ret);
2407 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2409 GraphicsContainerItem *cont, *next;
2410 GpStatus stat;
2411 TRACE("(%p)\n", graphics);
2413 if(!graphics) return InvalidParameter;
2414 if(graphics->busy) return ObjectBusy;
2416 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2418 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2419 if (stat != Ok)
2420 return stat;
2423 if(graphics->owndc)
2424 ReleaseDC(graphics->hwnd, graphics->hdc);
2426 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2427 list_remove(&cont->entry);
2428 delete_container(cont);
2431 GdipDeleteRegion(graphics->clip);
2432 GdipFree(graphics);
2434 return Ok;
2437 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2438 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2440 GpStatus status;
2441 GpPath *path;
2443 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2444 width, height, startAngle, sweepAngle);
2446 if(!graphics || !pen || width <= 0 || height <= 0)
2447 return InvalidParameter;
2449 if(graphics->busy)
2450 return ObjectBusy;
2452 status = GdipCreatePath(FillModeAlternate, &path);
2453 if (status != Ok) return status;
2455 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2456 if (status == Ok)
2457 status = GdipDrawPath(graphics, pen, path);
2459 GdipDeletePath(path);
2460 return status;
2463 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2464 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2466 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2467 width, height, startAngle, sweepAngle);
2469 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2472 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2473 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2475 GpPointF pt[4];
2477 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2478 x2, y2, x3, y3, x4, y4);
2480 if(!graphics || !pen)
2481 return InvalidParameter;
2483 if(graphics->busy)
2484 return ObjectBusy;
2486 pt[0].X = x1;
2487 pt[0].Y = y1;
2488 pt[1].X = x2;
2489 pt[1].Y = y2;
2490 pt[2].X = x3;
2491 pt[2].Y = y3;
2492 pt[3].X = x4;
2493 pt[3].Y = y4;
2494 return GdipDrawBeziers(graphics, pen, pt, 4);
2497 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2498 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2500 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2501 x2, y2, x3, y3, x4, y4);
2503 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2506 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2507 GDIPCONST GpPointF *points, INT count)
2509 GpStatus status;
2510 GpPath *path;
2512 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2514 if(!graphics || !pen || !points || (count <= 0))
2515 return InvalidParameter;
2517 if(graphics->busy)
2518 return ObjectBusy;
2520 status = GdipCreatePath(FillModeAlternate, &path);
2521 if (status != Ok) return status;
2523 status = GdipAddPathBeziers(path, points, count);
2524 if (status == Ok)
2525 status = GdipDrawPath(graphics, pen, path);
2527 GdipDeletePath(path);
2528 return status;
2531 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2532 GDIPCONST GpPoint *points, INT count)
2534 GpPointF *pts;
2535 GpStatus ret;
2536 INT i;
2538 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2540 if(!graphics || !pen || !points || (count <= 0))
2541 return InvalidParameter;
2543 if(graphics->busy)
2544 return ObjectBusy;
2546 pts = GdipAlloc(sizeof(GpPointF) * count);
2547 if(!pts)
2548 return OutOfMemory;
2550 for(i = 0; i < count; i++){
2551 pts[i].X = (REAL)points[i].X;
2552 pts[i].Y = (REAL)points[i].Y;
2555 ret = GdipDrawBeziers(graphics,pen,pts,count);
2557 GdipFree(pts);
2559 return ret;
2562 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2563 GDIPCONST GpPointF *points, INT count)
2565 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2567 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2570 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2571 GDIPCONST GpPoint *points, INT count)
2573 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2575 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2578 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2579 GDIPCONST GpPointF *points, INT count, REAL tension)
2581 GpPath *path;
2582 GpStatus status;
2584 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2586 if(!graphics || !pen || !points || count <= 0)
2587 return InvalidParameter;
2589 if(graphics->busy)
2590 return ObjectBusy;
2592 status = GdipCreatePath(FillModeAlternate, &path);
2593 if (status != Ok) return status;
2595 status = GdipAddPathClosedCurve2(path, points, count, tension);
2596 if (status == Ok)
2597 status = GdipDrawPath(graphics, pen, path);
2599 GdipDeletePath(path);
2601 return status;
2604 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2605 GDIPCONST GpPoint *points, INT count, REAL tension)
2607 GpPointF *ptf;
2608 GpStatus stat;
2609 INT i;
2611 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2613 if(!points || count <= 0)
2614 return InvalidParameter;
2616 ptf = GdipAlloc(sizeof(GpPointF)*count);
2617 if(!ptf)
2618 return OutOfMemory;
2620 for(i = 0; i < count; i++){
2621 ptf[i].X = (REAL)points[i].X;
2622 ptf[i].Y = (REAL)points[i].Y;
2625 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2627 GdipFree(ptf);
2629 return stat;
2632 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2633 GDIPCONST GpPointF *points, INT count)
2635 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2637 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2640 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2641 GDIPCONST GpPoint *points, INT count)
2643 GpPointF *pointsF;
2644 GpStatus ret;
2645 INT i;
2647 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2649 if(!points)
2650 return InvalidParameter;
2652 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2653 if(!pointsF)
2654 return OutOfMemory;
2656 for(i = 0; i < count; i++){
2657 pointsF[i].X = (REAL)points[i].X;
2658 pointsF[i].Y = (REAL)points[i].Y;
2661 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2662 GdipFree(pointsF);
2664 return ret;
2667 /* Approximates cardinal spline with Bezier curves. */
2668 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2669 GDIPCONST GpPointF *points, INT count, REAL tension)
2671 GpPath *path;
2672 GpStatus status;
2674 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2676 if(!graphics || !pen)
2677 return InvalidParameter;
2679 if(graphics->busy)
2680 return ObjectBusy;
2682 if(count < 2)
2683 return InvalidParameter;
2685 status = GdipCreatePath(FillModeAlternate, &path);
2686 if (status != Ok) return status;
2688 status = GdipAddPathCurve2(path, points, count, tension);
2689 if (status == Ok)
2690 status = GdipDrawPath(graphics, pen, path);
2692 GdipDeletePath(path);
2693 return status;
2696 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2697 GDIPCONST GpPoint *points, INT count, REAL tension)
2699 GpPointF *pointsF;
2700 GpStatus ret;
2701 INT i;
2703 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2705 if(!points)
2706 return InvalidParameter;
2708 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2709 if(!pointsF)
2710 return OutOfMemory;
2712 for(i = 0; i < count; i++){
2713 pointsF[i].X = (REAL)points[i].X;
2714 pointsF[i].Y = (REAL)points[i].Y;
2717 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2718 GdipFree(pointsF);
2720 return ret;
2723 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2724 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2725 REAL tension)
2727 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2729 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2730 return InvalidParameter;
2733 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2736 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2737 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2738 REAL tension)
2740 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2742 if(count < 0){
2743 return OutOfMemory;
2746 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2747 return InvalidParameter;
2750 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2753 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2754 REAL y, REAL width, REAL height)
2756 GpPath *path;
2757 GpStatus status;
2759 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2761 if(!graphics || !pen)
2762 return InvalidParameter;
2764 if(graphics->busy)
2765 return ObjectBusy;
2767 status = GdipCreatePath(FillModeAlternate, &path);
2768 if (status != Ok) return status;
2770 status = GdipAddPathEllipse(path, x, y, width, height);
2771 if (status == Ok)
2772 status = GdipDrawPath(graphics, pen, path);
2774 GdipDeletePath(path);
2775 return status;
2778 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2779 INT y, INT width, INT height)
2781 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2783 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2787 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2789 UINT width, height;
2791 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2793 if(!graphics || !image)
2794 return InvalidParameter;
2796 GdipGetImageWidth(image, &width);
2797 GdipGetImageHeight(image, &height);
2799 return GdipDrawImagePointRect(graphics, image, x, y,
2800 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2803 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2804 INT y)
2806 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2808 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2811 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2812 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2813 GpUnit srcUnit)
2815 GpPointF points[3];
2816 REAL scale_x, scale_y, width, height;
2818 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2820 if (!graphics || !image) return InvalidParameter;
2822 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2823 scale_x *= graphics->xres / image->xres;
2824 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2825 scale_y *= graphics->yres / image->yres;
2826 width = srcwidth * scale_x;
2827 height = srcheight * scale_y;
2829 points[0].X = points[2].X = x;
2830 points[0].Y = points[1].Y = y;
2831 points[1].X = x + width;
2832 points[2].Y = y + height;
2834 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2835 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2838 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2839 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2840 GpUnit srcUnit)
2842 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2845 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2846 GDIPCONST GpPointF *dstpoints, INT count)
2848 UINT width, height;
2850 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2852 if(!image)
2853 return InvalidParameter;
2855 GdipGetImageWidth(image, &width);
2856 GdipGetImageHeight(image, &height);
2858 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2859 width, height, UnitPixel, NULL, NULL, NULL);
2862 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2863 GDIPCONST GpPoint *dstpoints, INT count)
2865 GpPointF ptf[3];
2867 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2869 if (count != 3 || !dstpoints)
2870 return InvalidParameter;
2872 ptf[0].X = (REAL)dstpoints[0].X;
2873 ptf[0].Y = (REAL)dstpoints[0].Y;
2874 ptf[1].X = (REAL)dstpoints[1].X;
2875 ptf[1].Y = (REAL)dstpoints[1].Y;
2876 ptf[2].X = (REAL)dstpoints[2].X;
2877 ptf[2].Y = (REAL)dstpoints[2].Y;
2879 return GdipDrawImagePoints(graphics, image, ptf, count);
2882 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2883 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2885 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2886 return TRUE;
2889 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2890 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2891 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2892 DrawImageAbort callback, VOID * callbackData)
2894 GpPointF ptf[4];
2895 POINT pti[4];
2896 GpStatus stat;
2898 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2899 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2900 callbackData);
2902 if (count > 3)
2903 return NotImplemented;
2905 if(!graphics || !image || !points || count != 3)
2906 return InvalidParameter;
2908 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2909 debugstr_pointf(&points[2]));
2911 memcpy(ptf, points, 3 * sizeof(GpPointF));
2912 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2913 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2914 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
2915 return Ok;
2916 transform_and_round_points(graphics, pti, ptf, 4);
2918 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2919 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2921 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2922 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2923 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2924 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2925 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2927 if (image->picture)
2929 if (!graphics->hdc)
2931 FIXME("graphics object has no HDC\n");
2934 if(IPicture_Render(image->picture, graphics->hdc,
2935 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2936 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
2938 if(callback)
2939 callback(callbackData);
2940 return GenericError;
2943 else if (image->type == ImageTypeBitmap)
2945 GpBitmap* bitmap = (GpBitmap*)image;
2946 int use_software=0;
2948 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2949 graphics->xres, graphics->yres,
2950 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2951 graphics->scale, image->xres, image->yres, bitmap->format,
2952 imageAttributes ? imageAttributes->outside_color : 0);
2954 if (imageAttributes || graphics->alpha_hdc ||
2955 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2956 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2957 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2958 srcx < 0 || srcy < 0 ||
2959 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2960 use_software = 1;
2962 if (use_software)
2964 RECT dst_area;
2965 GpRect src_area;
2966 int i, x, y, src_stride, dst_stride;
2967 GpMatrix dst_to_src;
2968 REAL m11, m12, m21, m22, mdx, mdy;
2969 LPBYTE src_data, dst_data;
2970 BitmapData lockeddata;
2971 InterpolationMode interpolation = graphics->interpolation;
2972 PixelOffsetMode offset_mode = graphics->pixeloffset;
2973 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2974 REAL x_dx, x_dy, y_dx, y_dy;
2975 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2977 if (!imageAttributes)
2978 imageAttributes = &defaultImageAttributes;
2980 dst_area.left = dst_area.right = pti[0].x;
2981 dst_area.top = dst_area.bottom = pti[0].y;
2982 for (i=1; i<4; i++)
2984 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2985 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2986 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2987 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2990 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2992 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2993 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2994 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2995 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2996 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2997 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2999 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3001 stat = GdipInvertMatrix(&dst_to_src);
3002 if (stat != Ok) return stat;
3004 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3005 if (!dst_data) return OutOfMemory;
3007 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3009 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3010 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3012 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3014 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3015 if (!src_data)
3017 GdipFree(dst_data);
3018 return OutOfMemory;
3020 src_stride = sizeof(ARGB) * src_area.Width;
3022 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3023 lockeddata.Width = src_area.Width;
3024 lockeddata.Height = src_area.Height;
3025 lockeddata.Stride = src_stride;
3026 lockeddata.PixelFormat = PixelFormat32bppARGB;
3027 lockeddata.Scan0 = src_data;
3029 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3030 PixelFormat32bppARGB, &lockeddata);
3032 if (stat == Ok)
3033 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3035 if (stat != Ok)
3037 if (src_data != dst_data)
3038 GdipFree(src_data);
3039 GdipFree(dst_data);
3040 return stat;
3043 apply_image_attributes(imageAttributes, src_data,
3044 src_area.Width, src_area.Height,
3045 src_stride, ColorAdjustTypeBitmap);
3047 /* Transform the bits as needed to the destination. */
3048 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3050 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3051 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3052 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3053 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3055 for (x=dst_area.left; x<dst_area.right; x++)
3057 for (y=dst_area.top; y<dst_area.bottom; y++)
3059 GpPointF src_pointf;
3060 ARGB *dst_color;
3062 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3063 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3065 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3067 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3068 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3069 imageAttributes, interpolation, offset_mode);
3070 else
3071 *dst_color = 0;
3075 GdipFree(src_data);
3077 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3078 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3080 GdipFree(dst_data);
3082 return stat;
3084 else
3086 HDC hdc;
3087 int temp_hdc=0, temp_bitmap=0;
3088 HBITMAP hbitmap, old_hbm=NULL;
3090 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3091 bitmap->format == PixelFormat24bppRGB ||
3092 bitmap->format == PixelFormat32bppRGB ||
3093 bitmap->format == PixelFormat32bppPARGB))
3095 BITMAPINFOHEADER bih;
3096 BYTE *temp_bits;
3097 PixelFormat dst_format;
3099 /* we can't draw a bitmap of this format directly */
3100 hdc = CreateCompatibleDC(0);
3101 temp_hdc = 1;
3102 temp_bitmap = 1;
3104 bih.biSize = sizeof(BITMAPINFOHEADER);
3105 bih.biWidth = bitmap->width;
3106 bih.biHeight = -bitmap->height;
3107 bih.biPlanes = 1;
3108 bih.biBitCount = 32;
3109 bih.biCompression = BI_RGB;
3110 bih.biSizeImage = 0;
3111 bih.biXPelsPerMeter = 0;
3112 bih.biYPelsPerMeter = 0;
3113 bih.biClrUsed = 0;
3114 bih.biClrImportant = 0;
3116 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3117 (void**)&temp_bits, NULL, 0);
3119 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3120 dst_format = PixelFormat32bppPARGB;
3121 else
3122 dst_format = PixelFormat32bppRGB;
3124 convert_pixels(bitmap->width, bitmap->height,
3125 bitmap->width*4, temp_bits, dst_format,
3126 bitmap->stride, bitmap->bits, bitmap->format,
3127 bitmap->image.palette);
3129 else
3131 if (bitmap->hbitmap)
3132 hbitmap = bitmap->hbitmap;
3133 else
3135 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3136 temp_bitmap = 1;
3139 hdc = bitmap->hdc;
3140 temp_hdc = (hdc == 0);
3143 if (temp_hdc)
3145 if (!hdc) hdc = CreateCompatibleDC(0);
3146 old_hbm = SelectObject(hdc, hbitmap);
3149 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3151 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3152 hdc, srcx, srcy, srcwidth, srcheight);
3154 else
3156 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3157 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3160 if (temp_hdc)
3162 SelectObject(hdc, old_hbm);
3163 DeleteDC(hdc);
3166 if (temp_bitmap)
3167 DeleteObject(hbitmap);
3170 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3172 GpRectF rc;
3174 rc.X = srcx;
3175 rc.Y = srcy;
3176 rc.Width = srcwidth;
3177 rc.Height = srcheight;
3179 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3180 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3182 else
3184 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3185 return InvalidParameter;
3188 return Ok;
3191 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3192 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3193 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3194 DrawImageAbort callback, VOID * callbackData)
3196 GpPointF pointsF[3];
3197 INT i;
3199 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3200 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3201 callbackData);
3203 if(!points || count!=3)
3204 return InvalidParameter;
3206 for(i = 0; i < count; i++){
3207 pointsF[i].X = (REAL)points[i].X;
3208 pointsF[i].Y = (REAL)points[i].Y;
3211 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3212 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3213 callback, callbackData);
3216 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3217 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3218 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3219 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3220 VOID * callbackData)
3222 GpPointF points[3];
3224 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3225 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3226 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3228 points[0].X = dstx;
3229 points[0].Y = dsty;
3230 points[1].X = dstx + dstwidth;
3231 points[1].Y = dsty;
3232 points[2].X = dstx;
3233 points[2].Y = dsty + dstheight;
3235 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3236 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3239 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3240 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3241 INT srcwidth, INT srcheight, GpUnit srcUnit,
3242 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3243 VOID * callbackData)
3245 GpPointF points[3];
3247 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3248 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3249 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3251 points[0].X = dstx;
3252 points[0].Y = dsty;
3253 points[1].X = dstx + dstwidth;
3254 points[1].Y = dsty;
3255 points[2].X = dstx;
3256 points[2].Y = dsty + dstheight;
3258 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3259 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3262 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3263 REAL x, REAL y, REAL width, REAL height)
3265 RectF bounds;
3266 GpUnit unit;
3267 GpStatus ret;
3269 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3271 if(!graphics || !image)
3272 return InvalidParameter;
3274 ret = GdipGetImageBounds(image, &bounds, &unit);
3275 if(ret != Ok)
3276 return ret;
3278 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3279 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3280 unit, NULL, NULL, NULL);
3283 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3284 INT x, INT y, INT width, INT height)
3286 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3288 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3291 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3292 REAL y1, REAL x2, REAL y2)
3294 GpPointF pt[2];
3296 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3298 pt[0].X = x1;
3299 pt[0].Y = y1;
3300 pt[1].X = x2;
3301 pt[1].Y = y2;
3302 return GdipDrawLines(graphics, pen, pt, 2);
3305 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3306 INT y1, INT x2, INT y2)
3308 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3310 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3313 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3314 GpPointF *points, INT count)
3316 GpStatus status;
3317 GpPath *path;
3319 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3321 if(!pen || !graphics || (count < 2))
3322 return InvalidParameter;
3324 if(graphics->busy)
3325 return ObjectBusy;
3327 status = GdipCreatePath(FillModeAlternate, &path);
3328 if (status != Ok) return status;
3330 status = GdipAddPathLine2(path, points, count);
3331 if (status == Ok)
3332 status = GdipDrawPath(graphics, pen, path);
3334 GdipDeletePath(path);
3335 return status;
3338 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3339 GpPoint *points, INT count)
3341 GpStatus retval;
3342 GpPointF *ptf;
3343 int i;
3345 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3347 ptf = GdipAlloc(count * sizeof(GpPointF));
3348 if(!ptf) return OutOfMemory;
3350 for(i = 0; i < count; i ++){
3351 ptf[i].X = (REAL) points[i].X;
3352 ptf[i].Y = (REAL) points[i].Y;
3355 retval = GdipDrawLines(graphics, pen, ptf, count);
3357 GdipFree(ptf);
3358 return retval;
3361 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3363 INT save_state;
3364 GpStatus retval;
3366 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3368 if(!pen || !graphics)
3369 return InvalidParameter;
3371 if(graphics->busy)
3372 return ObjectBusy;
3374 if (!graphics->hdc)
3376 FIXME("graphics object has no HDC\n");
3377 return Ok;
3380 save_state = prepare_dc(graphics, pen);
3382 retval = draw_poly(graphics, pen, path->pathdata.Points,
3383 path->pathdata.Types, path->pathdata.Count, TRUE);
3385 restore_dc(graphics, save_state);
3387 return retval;
3390 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3391 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3393 GpStatus status;
3394 GpPath *path;
3396 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3397 width, height, startAngle, sweepAngle);
3399 if(!graphics || !pen)
3400 return InvalidParameter;
3402 if(graphics->busy)
3403 return ObjectBusy;
3405 status = GdipCreatePath(FillModeAlternate, &path);
3406 if (status != Ok) return status;
3408 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3409 if (status == Ok)
3410 status = GdipDrawPath(graphics, pen, path);
3412 GdipDeletePath(path);
3413 return status;
3416 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3417 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3419 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3420 width, height, startAngle, sweepAngle);
3422 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3425 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3426 REAL y, REAL width, REAL height)
3428 GpStatus status;
3429 GpPath *path;
3431 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3433 if(!pen || !graphics)
3434 return InvalidParameter;
3436 if(graphics->busy)
3437 return ObjectBusy;
3439 status = GdipCreatePath(FillModeAlternate, &path);
3440 if (status != Ok) return status;
3442 status = GdipAddPathRectangle(path, x, y, width, height);
3443 if (status == Ok)
3444 status = GdipDrawPath(graphics, pen, path);
3446 GdipDeletePath(path);
3447 return status;
3450 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3451 INT y, INT width, INT height)
3453 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3455 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3458 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3459 GDIPCONST GpRectF* rects, INT count)
3461 GpStatus status;
3462 GpPath *path;
3464 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3466 if(!graphics || !pen || !rects || count < 1)
3467 return InvalidParameter;
3469 if(graphics->busy)
3470 return ObjectBusy;
3472 status = GdipCreatePath(FillModeAlternate, &path);
3473 if (status != Ok) return status;
3475 status = GdipAddPathRectangles(path, rects, count);
3476 if (status == Ok)
3477 status = GdipDrawPath(graphics, pen, path);
3479 GdipDeletePath(path);
3480 return status;
3483 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3484 GDIPCONST GpRect* rects, INT count)
3486 GpRectF *rectsF;
3487 GpStatus ret;
3488 INT i;
3490 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3492 if(!rects || count<=0)
3493 return InvalidParameter;
3495 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3496 if(!rectsF)
3497 return OutOfMemory;
3499 for(i = 0;i < count;i++){
3500 rectsF[i].X = (REAL)rects[i].X;
3501 rectsF[i].Y = (REAL)rects[i].Y;
3502 rectsF[i].Width = (REAL)rects[i].Width;
3503 rectsF[i].Height = (REAL)rects[i].Height;
3506 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3507 GdipFree(rectsF);
3509 return ret;
3512 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3513 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3515 GpPath *path;
3516 GpStatus status;
3518 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3519 count, tension, fill);
3521 if(!graphics || !brush || !points)
3522 return InvalidParameter;
3524 if(graphics->busy)
3525 return ObjectBusy;
3527 if(count == 1) /* Do nothing */
3528 return Ok;
3530 status = GdipCreatePath(fill, &path);
3531 if (status != Ok) return status;
3533 status = GdipAddPathClosedCurve2(path, points, count, tension);
3534 if (status == Ok)
3535 status = GdipFillPath(graphics, brush, path);
3537 GdipDeletePath(path);
3538 return status;
3541 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3542 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3544 GpPointF *ptf;
3545 GpStatus stat;
3546 INT i;
3548 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3549 count, tension, fill);
3551 if(!points || count == 0)
3552 return InvalidParameter;
3554 if(count == 1) /* Do nothing */
3555 return Ok;
3557 ptf = GdipAlloc(sizeof(GpPointF)*count);
3558 if(!ptf)
3559 return OutOfMemory;
3561 for(i = 0;i < count;i++){
3562 ptf[i].X = (REAL)points[i].X;
3563 ptf[i].Y = (REAL)points[i].Y;
3566 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3568 GdipFree(ptf);
3570 return stat;
3573 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3574 GDIPCONST GpPointF *points, INT count)
3576 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3577 return GdipFillClosedCurve2(graphics, brush, points, count,
3578 0.5f, FillModeAlternate);
3581 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3582 GDIPCONST GpPoint *points, INT count)
3584 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3585 return GdipFillClosedCurve2I(graphics, brush, points, count,
3586 0.5f, FillModeAlternate);
3589 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3590 REAL y, REAL width, REAL height)
3592 GpStatus stat;
3593 GpPath *path;
3595 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3597 if(!graphics || !brush)
3598 return InvalidParameter;
3600 if(graphics->busy)
3601 return ObjectBusy;
3603 stat = GdipCreatePath(FillModeAlternate, &path);
3605 if (stat == Ok)
3607 stat = GdipAddPathEllipse(path, x, y, width, height);
3609 if (stat == Ok)
3610 stat = GdipFillPath(graphics, brush, path);
3612 GdipDeletePath(path);
3615 return stat;
3618 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3619 INT y, INT width, INT height)
3621 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3623 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3626 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3628 INT save_state;
3629 GpStatus retval;
3631 if(!graphics->hdc || !brush_can_fill_path(brush))
3632 return NotImplemented;
3634 save_state = SaveDC(graphics->hdc);
3635 EndPath(graphics->hdc);
3636 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3637 : WINDING));
3639 BeginPath(graphics->hdc);
3640 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3641 path->pathdata.Types, path->pathdata.Count, FALSE);
3643 if(retval != Ok)
3644 goto end;
3646 EndPath(graphics->hdc);
3647 brush_fill_path(graphics, brush);
3649 retval = Ok;
3651 end:
3652 RestoreDC(graphics->hdc, save_state);
3654 return retval;
3657 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3659 GpStatus stat;
3660 GpRegion *rgn;
3662 if (!brush_can_fill_pixels(brush))
3663 return NotImplemented;
3665 /* FIXME: This could probably be done more efficiently without regions. */
3667 stat = GdipCreateRegionPath(path, &rgn);
3669 if (stat == Ok)
3671 stat = GdipFillRegion(graphics, brush, rgn);
3673 GdipDeleteRegion(rgn);
3676 return stat;
3679 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3681 GpStatus stat = NotImplemented;
3683 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3685 if(!brush || !graphics || !path)
3686 return InvalidParameter;
3688 if(graphics->busy)
3689 return ObjectBusy;
3691 if (!graphics->image && !graphics->alpha_hdc)
3692 stat = GDI32_GdipFillPath(graphics, brush, path);
3694 if (stat == NotImplemented)
3695 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3697 if (stat == NotImplemented)
3699 FIXME("Not implemented for brushtype %i\n", brush->bt);
3700 stat = Ok;
3703 return stat;
3706 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3707 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3709 GpStatus stat;
3710 GpPath *path;
3712 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3713 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3715 if(!graphics || !brush)
3716 return InvalidParameter;
3718 if(graphics->busy)
3719 return ObjectBusy;
3721 stat = GdipCreatePath(FillModeAlternate, &path);
3723 if (stat == Ok)
3725 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3727 if (stat == Ok)
3728 stat = GdipFillPath(graphics, brush, path);
3730 GdipDeletePath(path);
3733 return stat;
3736 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3737 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3739 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3740 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3742 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3745 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3746 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3748 GpStatus stat;
3749 GpPath *path;
3751 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3753 if(!graphics || !brush || !points || !count)
3754 return InvalidParameter;
3756 if(graphics->busy)
3757 return ObjectBusy;
3759 stat = GdipCreatePath(fillMode, &path);
3761 if (stat == Ok)
3763 stat = GdipAddPathPolygon(path, points, count);
3765 if (stat == Ok)
3766 stat = GdipFillPath(graphics, brush, path);
3768 GdipDeletePath(path);
3771 return stat;
3774 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3775 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3777 GpStatus stat;
3778 GpPath *path;
3780 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3782 if(!graphics || !brush || !points || !count)
3783 return InvalidParameter;
3785 if(graphics->busy)
3786 return ObjectBusy;
3788 stat = GdipCreatePath(fillMode, &path);
3790 if (stat == Ok)
3792 stat = GdipAddPathPolygonI(path, points, count);
3794 if (stat == Ok)
3795 stat = GdipFillPath(graphics, brush, path);
3797 GdipDeletePath(path);
3800 return stat;
3803 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3804 GDIPCONST GpPointF *points, INT count)
3806 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3808 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3811 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3812 GDIPCONST GpPoint *points, INT count)
3814 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3816 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3819 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3820 REAL x, REAL y, REAL width, REAL height)
3822 GpStatus stat;
3823 GpPath *path;
3825 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3827 if(!graphics || !brush)
3828 return InvalidParameter;
3830 if(graphics->busy)
3831 return ObjectBusy;
3833 stat = GdipCreatePath(FillModeAlternate, &path);
3835 if (stat == Ok)
3837 stat = GdipAddPathRectangle(path, x, y, width, height);
3839 if (stat == Ok)
3840 stat = GdipFillPath(graphics, brush, path);
3842 GdipDeletePath(path);
3845 return stat;
3848 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3849 INT x, INT y, INT width, INT height)
3851 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3853 return GdipFillRectangle(graphics, brush, x, y, width, height);
3856 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3857 INT count)
3859 GpStatus status;
3860 GpPath *path;
3862 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3864 if(!rects)
3865 return InvalidParameter;
3867 status = GdipCreatePath(FillModeAlternate, &path);
3868 if (status != Ok) return status;
3870 status = GdipAddPathRectangles(path, rects, count);
3871 if (status == Ok)
3872 status = GdipFillPath(graphics, brush, path);
3874 GdipDeletePath(path);
3875 return status;
3878 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3879 INT count)
3881 GpRectF *rectsF;
3882 GpStatus ret;
3883 INT i;
3885 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3887 if(!rects || count <= 0)
3888 return InvalidParameter;
3890 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3891 if(!rectsF)
3892 return OutOfMemory;
3894 for(i = 0; i < count; i++){
3895 rectsF[i].X = (REAL)rects[i].X;
3896 rectsF[i].Y = (REAL)rects[i].Y;
3897 rectsF[i].X = (REAL)rects[i].Width;
3898 rectsF[i].Height = (REAL)rects[i].Height;
3901 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3902 GdipFree(rectsF);
3904 return ret;
3907 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3908 GpRegion* region)
3910 INT save_state;
3911 GpStatus status;
3912 HRGN hrgn;
3913 RECT rc;
3915 if(!graphics->hdc || !brush_can_fill_path(brush))
3916 return NotImplemented;
3918 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3919 if(status != Ok)
3920 return status;
3922 save_state = SaveDC(graphics->hdc);
3923 EndPath(graphics->hdc);
3925 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3927 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3929 BeginPath(graphics->hdc);
3930 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3931 EndPath(graphics->hdc);
3933 brush_fill_path(graphics, brush);
3936 RestoreDC(graphics->hdc, save_state);
3938 DeleteObject(hrgn);
3940 return Ok;
3943 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3944 GpRegion* region)
3946 GpStatus stat;
3947 GpRegion *temp_region;
3948 GpMatrix world_to_device;
3949 GpRectF graphics_bounds;
3950 DWORD *pixel_data;
3951 HRGN hregion;
3952 RECT bound_rect;
3953 GpRect gp_bound_rect;
3955 if (!brush_can_fill_pixels(brush))
3956 return NotImplemented;
3958 stat = get_graphics_bounds(graphics, &graphics_bounds);
3960 if (stat == Ok)
3961 stat = GdipCloneRegion(region, &temp_region);
3963 if (stat == Ok)
3965 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3966 CoordinateSpaceWorld, &world_to_device);
3968 if (stat == Ok)
3969 stat = GdipTransformRegion(temp_region, &world_to_device);
3971 if (stat == Ok)
3972 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3974 if (stat == Ok)
3975 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
3977 GdipDeleteRegion(temp_region);
3980 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
3982 DeleteObject(hregion);
3983 return Ok;
3986 if (stat == Ok)
3988 gp_bound_rect.X = bound_rect.left;
3989 gp_bound_rect.Y = bound_rect.top;
3990 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
3991 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
3993 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
3994 if (!pixel_data)
3995 stat = OutOfMemory;
3997 if (stat == Ok)
3999 stat = brush_fill_pixels(graphics, brush, pixel_data,
4000 &gp_bound_rect, gp_bound_rect.Width);
4002 if (stat == Ok)
4003 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4004 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4005 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4007 GdipFree(pixel_data);
4010 DeleteObject(hregion);
4013 return stat;
4016 /*****************************************************************************
4017 * GdipFillRegion [GDIPLUS.@]
4019 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4020 GpRegion* region)
4022 GpStatus stat = NotImplemented;
4024 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4026 if (!(graphics && brush && region))
4027 return InvalidParameter;
4029 if(graphics->busy)
4030 return ObjectBusy;
4032 if (!graphics->image && !graphics->alpha_hdc)
4033 stat = GDI32_GdipFillRegion(graphics, brush, region);
4035 if (stat == NotImplemented)
4036 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4038 if (stat == NotImplemented)
4040 FIXME("not implemented for brushtype %i\n", brush->bt);
4041 stat = Ok;
4044 return stat;
4047 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4049 TRACE("(%p,%u)\n", graphics, intention);
4051 if(!graphics)
4052 return InvalidParameter;
4054 if(graphics->busy)
4055 return ObjectBusy;
4057 /* We have no internal operation queue, so there's no need to clear it. */
4059 if (graphics->hdc)
4060 GdiFlush();
4062 return Ok;
4065 /*****************************************************************************
4066 * GdipGetClipBounds [GDIPLUS.@]
4068 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4070 GpStatus status;
4072 TRACE("(%p, %p)\n", graphics, rect);
4074 if(!graphics)
4075 return InvalidParameter;
4077 if(graphics->busy)
4078 return ObjectBusy;
4080 status = GdipGetRegionBounds(graphics->clip, graphics, rect);
4081 if (status == Ok)
4082 transform_rectf(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, rect);
4084 return status;
4087 /*****************************************************************************
4088 * GdipGetClipBoundsI [GDIPLUS.@]
4090 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4092 TRACE("(%p, %p)\n", graphics, rect);
4094 if(!graphics)
4095 return InvalidParameter;
4097 if(graphics->busy)
4098 return ObjectBusy;
4100 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4103 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4104 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4105 CompositingMode *mode)
4107 TRACE("(%p, %p)\n", graphics, mode);
4109 if(!graphics || !mode)
4110 return InvalidParameter;
4112 if(graphics->busy)
4113 return ObjectBusy;
4115 *mode = graphics->compmode;
4117 return Ok;
4120 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4121 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4122 CompositingQuality *quality)
4124 TRACE("(%p, %p)\n", graphics, quality);
4126 if(!graphics || !quality)
4127 return InvalidParameter;
4129 if(graphics->busy)
4130 return ObjectBusy;
4132 *quality = graphics->compqual;
4134 return Ok;
4137 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4138 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4139 InterpolationMode *mode)
4141 TRACE("(%p, %p)\n", graphics, mode);
4143 if(!graphics || !mode)
4144 return InvalidParameter;
4146 if(graphics->busy)
4147 return ObjectBusy;
4149 *mode = graphics->interpolation;
4151 return Ok;
4154 /* FIXME: Need to handle color depths less than 24bpp */
4155 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4157 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4159 if(!graphics || !argb)
4160 return InvalidParameter;
4162 if(graphics->busy)
4163 return ObjectBusy;
4165 return Ok;
4168 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4170 TRACE("(%p, %p)\n", graphics, scale);
4172 if(!graphics || !scale)
4173 return InvalidParameter;
4175 if(graphics->busy)
4176 return ObjectBusy;
4178 *scale = graphics->scale;
4180 return Ok;
4183 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4185 TRACE("(%p, %p)\n", graphics, unit);
4187 if(!graphics || !unit)
4188 return InvalidParameter;
4190 if(graphics->busy)
4191 return ObjectBusy;
4193 *unit = graphics->unit;
4195 return Ok;
4198 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4199 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4200 *mode)
4202 TRACE("(%p, %p)\n", graphics, mode);
4204 if(!graphics || !mode)
4205 return InvalidParameter;
4207 if(graphics->busy)
4208 return ObjectBusy;
4210 *mode = graphics->pixeloffset;
4212 return Ok;
4215 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4216 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4218 TRACE("(%p, %p)\n", graphics, mode);
4220 if(!graphics || !mode)
4221 return InvalidParameter;
4223 if(graphics->busy)
4224 return ObjectBusy;
4226 *mode = graphics->smoothing;
4228 return Ok;
4231 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4233 TRACE("(%p, %p)\n", graphics, contrast);
4235 if(!graphics || !contrast)
4236 return InvalidParameter;
4238 *contrast = graphics->textcontrast;
4240 return Ok;
4243 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4244 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4245 TextRenderingHint *hint)
4247 TRACE("(%p, %p)\n", graphics, hint);
4249 if(!graphics || !hint)
4250 return InvalidParameter;
4252 if(graphics->busy)
4253 return ObjectBusy;
4255 *hint = graphics->texthint;
4257 return Ok;
4260 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4262 GpRegion *clip_rgn;
4263 GpStatus stat;
4265 TRACE("(%p, %p)\n", graphics, rect);
4267 if(!graphics || !rect)
4268 return InvalidParameter;
4270 if(graphics->busy)
4271 return ObjectBusy;
4273 /* intersect window and graphics clipping regions */
4274 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4275 return stat;
4277 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4278 goto cleanup;
4280 /* get bounds of the region */
4281 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4283 cleanup:
4284 GdipDeleteRegion(clip_rgn);
4286 return stat;
4289 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4291 GpRectF rectf;
4292 GpStatus stat;
4294 TRACE("(%p, %p)\n", graphics, rect);
4296 if(!graphics || !rect)
4297 return InvalidParameter;
4299 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4301 rect->X = gdip_round(rectf.X);
4302 rect->Y = gdip_round(rectf.Y);
4303 rect->Width = gdip_round(rectf.Width);
4304 rect->Height = gdip_round(rectf.Height);
4307 return stat;
4310 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4312 TRACE("(%p, %p)\n", graphics, matrix);
4314 if(!graphics || !matrix)
4315 return InvalidParameter;
4317 if(graphics->busy)
4318 return ObjectBusy;
4320 *matrix = graphics->worldtrans;
4321 return Ok;
4324 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4326 GpSolidFill *brush;
4327 GpStatus stat;
4328 GpRectF wnd_rect;
4330 TRACE("(%p, %x)\n", graphics, color);
4332 if(!graphics)
4333 return InvalidParameter;
4335 if(graphics->busy)
4336 return ObjectBusy;
4338 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4339 return stat;
4341 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4342 GdipDeleteBrush((GpBrush*)brush);
4343 return stat;
4346 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4347 wnd_rect.Width, wnd_rect.Height);
4349 GdipDeleteBrush((GpBrush*)brush);
4351 return Ok;
4354 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4356 TRACE("(%p, %p)\n", graphics, res);
4358 if(!graphics || !res)
4359 return InvalidParameter;
4361 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4364 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4366 GpStatus stat;
4367 GpRegion* rgn;
4368 GpPointF pt;
4370 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4372 if(!graphics || !result)
4373 return InvalidParameter;
4375 if(graphics->busy)
4376 return ObjectBusy;
4378 pt.X = x;
4379 pt.Y = y;
4380 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4381 CoordinateSpaceWorld, &pt, 1)) != Ok)
4382 return stat;
4384 if((stat = GdipCreateRegion(&rgn)) != Ok)
4385 return stat;
4387 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4388 goto cleanup;
4390 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4392 cleanup:
4393 GdipDeleteRegion(rgn);
4394 return stat;
4397 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4399 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4402 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4404 GpStatus stat;
4405 GpRegion* rgn;
4406 GpPointF pts[2];
4408 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4410 if(!graphics || !result)
4411 return InvalidParameter;
4413 if(graphics->busy)
4414 return ObjectBusy;
4416 pts[0].X = x;
4417 pts[0].Y = y;
4418 pts[1].X = x + width;
4419 pts[1].Y = y + height;
4421 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4422 CoordinateSpaceWorld, pts, 2)) != Ok)
4423 return stat;
4425 pts[1].X -= pts[0].X;
4426 pts[1].Y -= pts[0].Y;
4428 if((stat = GdipCreateRegion(&rgn)) != Ok)
4429 return stat;
4431 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4432 goto cleanup;
4434 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4436 cleanup:
4437 GdipDeleteRegion(rgn);
4438 return stat;
4441 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4443 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4446 GpStatus gdip_format_string(HDC hdc,
4447 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4448 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4449 gdip_format_string_callback callback, void *user_data)
4451 WCHAR* stringdup;
4452 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4453 nheight, lineend, lineno = 0;
4454 RectF bounds;
4455 StringAlignment halign;
4456 GpStatus stat = Ok;
4457 SIZE size;
4458 HotkeyPrefix hkprefix;
4459 INT *hotkeyprefix_offsets=NULL;
4460 INT hotkeyprefix_count=0;
4461 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4462 int seen_prefix=0;
4464 if(length == -1) length = lstrlenW(string);
4466 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4467 if(!stringdup) return OutOfMemory;
4469 nwidth = rect->Width;
4470 nheight = rect->Height;
4471 if (ignore_empty_clip)
4473 if (!nwidth) nwidth = INT_MAX;
4474 if (!nheight) nheight = INT_MAX;
4477 if (format)
4478 hkprefix = format->hkprefix;
4479 else
4480 hkprefix = HotkeyPrefixNone;
4482 if (hkprefix == HotkeyPrefixShow)
4484 for (i=0; i<length; i++)
4486 if (string[i] == '&')
4487 hotkeyprefix_count++;
4491 if (hotkeyprefix_count)
4492 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4494 hotkeyprefix_count = 0;
4496 for(i = 0, j = 0; i < length; i++){
4497 /* FIXME: This makes the indexes passed to callback inaccurate. */
4498 if(!isprintW(string[i]) && (string[i] != '\n'))
4499 continue;
4501 /* FIXME: tabs should be handled using tabstops from stringformat */
4502 if (string[i] == '\t')
4503 continue;
4505 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4506 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4507 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4509 seen_prefix = 1;
4510 continue;
4513 seen_prefix = 0;
4515 stringdup[j] = string[i];
4516 j++;
4519 length = j;
4521 if (format) halign = format->align;
4522 else halign = StringAlignmentNear;
4524 while(sum < length){
4525 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4526 nwidth, &fit, NULL, &size);
4527 fitcpy = fit;
4529 if(fit == 0)
4530 break;
4532 for(lret = 0; lret < fit; lret++)
4533 if(*(stringdup + sum + lret) == '\n')
4534 break;
4536 /* Line break code (may look strange, but it imitates windows). */
4537 if(lret < fit)
4538 lineend = fit = lret; /* this is not an off-by-one error */
4539 else if(fit < (length - sum)){
4540 if(*(stringdup + sum + fit) == ' ')
4541 while(*(stringdup + sum + fit) == ' ')
4542 fit++;
4543 else
4544 while(*(stringdup + sum + fit - 1) != ' '){
4545 fit--;
4547 if(*(stringdup + sum + fit) == '\t')
4548 break;
4550 if(fit == 0){
4551 fit = fitcpy;
4552 break;
4555 lineend = fit;
4556 while(*(stringdup + sum + lineend - 1) == ' ' ||
4557 *(stringdup + sum + lineend - 1) == '\t')
4558 lineend--;
4560 else
4561 lineend = fit;
4563 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4564 nwidth, &j, NULL, &size);
4566 bounds.Width = size.cx;
4568 if(height + size.cy > nheight)
4569 bounds.Height = nheight - (height + size.cy);
4570 else
4571 bounds.Height = size.cy;
4573 bounds.Y = rect->Y + height;
4575 switch (halign)
4577 case StringAlignmentNear:
4578 default:
4579 bounds.X = rect->X;
4580 break;
4581 case StringAlignmentCenter:
4582 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4583 break;
4584 case StringAlignmentFar:
4585 bounds.X = rect->X + rect->Width - bounds.Width;
4586 break;
4589 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4590 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4591 break;
4593 stat = callback(hdc, stringdup, sum, lineend,
4594 font, rect, format, lineno, &bounds,
4595 &hotkeyprefix_offsets[hotkeyprefix_pos],
4596 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4598 if (stat != Ok)
4599 break;
4601 sum += fit + (lret < fitcpy ? 1 : 0);
4602 height += size.cy;
4603 lineno++;
4605 hotkeyprefix_pos = hotkeyprefix_end_pos;
4607 if(height > nheight)
4608 break;
4610 /* Stop if this was a linewrap (but not if it was a linebreak). */
4611 if ((lret == fitcpy) && format &&
4612 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4613 break;
4616 GdipFree(stringdup);
4617 GdipFree(hotkeyprefix_offsets);
4619 return stat;
4622 struct measure_ranges_args {
4623 GpRegion **regions;
4624 REAL rel_width, rel_height;
4627 static GpStatus measure_ranges_callback(HDC hdc,
4628 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4629 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4630 INT lineno, const RectF *bounds, INT *underlined_indexes,
4631 INT underlined_index_count, void *user_data)
4633 int i;
4634 GpStatus stat = Ok;
4635 struct measure_ranges_args *args = user_data;
4637 for (i=0; i<format->range_count; i++)
4639 INT range_start = max(index, format->character_ranges[i].First);
4640 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4641 if (range_start < range_end)
4643 GpRectF range_rect;
4644 SIZE range_size;
4646 range_rect.Y = bounds->Y / args->rel_height;
4647 range_rect.Height = bounds->Height / args->rel_height;
4649 GetTextExtentExPointW(hdc, string + index, range_start - index,
4650 INT_MAX, NULL, NULL, &range_size);
4651 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4653 GetTextExtentExPointW(hdc, string + index, range_end - index,
4654 INT_MAX, NULL, NULL, &range_size);
4655 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4657 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4658 if (stat != Ok)
4659 break;
4663 return stat;
4666 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4667 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4668 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4669 INT regionCount, GpRegion** regions)
4671 GpStatus stat;
4672 int i;
4673 HFONT gdifont, oldfont;
4674 struct measure_ranges_args args;
4675 HDC hdc, temp_hdc=NULL;
4676 GpPointF pt[3];
4677 RectF scaled_rect;
4678 REAL margin_x;
4680 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4681 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4683 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4684 return InvalidParameter;
4686 if (regionCount < stringFormat->range_count)
4687 return InvalidParameter;
4689 if(!graphics->hdc)
4691 hdc = temp_hdc = CreateCompatibleDC(0);
4692 if (!temp_hdc) return OutOfMemory;
4694 else
4695 hdc = graphics->hdc;
4697 if (stringFormat->attr)
4698 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4700 pt[0].X = 0.0;
4701 pt[0].Y = 0.0;
4702 pt[1].X = 1.0;
4703 pt[1].Y = 0.0;
4704 pt[2].X = 0.0;
4705 pt[2].Y = 1.0;
4706 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4707 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4708 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4709 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4710 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4712 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4713 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4715 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4716 scaled_rect.Y = layoutRect->Y * args.rel_height;
4717 scaled_rect.Width = layoutRect->Width * args.rel_width;
4718 scaled_rect.Height = layoutRect->Height * args.rel_height;
4720 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4721 oldfont = SelectObject(hdc, gdifont);
4723 for (i=0; i<stringFormat->range_count; i++)
4725 stat = GdipSetEmpty(regions[i]);
4726 if (stat != Ok)
4727 return stat;
4730 args.regions = regions;
4732 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4733 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4735 SelectObject(hdc, oldfont);
4736 DeleteObject(gdifont);
4738 if (temp_hdc)
4739 DeleteDC(temp_hdc);
4741 return stat;
4744 struct measure_string_args {
4745 RectF *bounds;
4746 INT *codepointsfitted;
4747 INT *linesfilled;
4748 REAL rel_width, rel_height;
4751 static GpStatus measure_string_callback(HDC hdc,
4752 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4753 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4754 INT lineno, const RectF *bounds, INT *underlined_indexes,
4755 INT underlined_index_count, void *user_data)
4757 struct measure_string_args *args = user_data;
4758 REAL new_width, new_height;
4760 new_width = bounds->Width / args->rel_width;
4761 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4763 if (new_width > args->bounds->Width)
4764 args->bounds->Width = new_width;
4766 if (new_height > args->bounds->Height)
4767 args->bounds->Height = new_height;
4769 if (args->codepointsfitted)
4770 *args->codepointsfitted = index + length;
4772 if (args->linesfilled)
4773 (*args->linesfilled)++;
4775 return Ok;
4778 /* Find the smallest rectangle that bounds the text when it is printed in rect
4779 * according to the format options listed in format. If rect has 0 width and
4780 * height, then just find the smallest rectangle that bounds the text when it's
4781 * printed at location (rect->X, rect-Y). */
4782 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4783 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4784 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4785 INT *codepointsfitted, INT *linesfilled)
4787 HFONT oldfont, gdifont;
4788 struct measure_string_args args;
4789 HDC temp_hdc=NULL, hdc;
4790 GpPointF pt[3];
4791 RectF scaled_rect;
4792 REAL margin_x;
4793 INT lines, glyphs;
4795 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4796 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4797 bounds, codepointsfitted, linesfilled);
4799 if(!graphics || !string || !font || !rect || !bounds)
4800 return InvalidParameter;
4802 if(!graphics->hdc)
4804 hdc = temp_hdc = CreateCompatibleDC(0);
4805 if (!temp_hdc) return OutOfMemory;
4807 else
4808 hdc = graphics->hdc;
4810 if(linesfilled) *linesfilled = 0;
4811 if(codepointsfitted) *codepointsfitted = 0;
4813 if(format)
4814 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4816 pt[0].X = 0.0;
4817 pt[0].Y = 0.0;
4818 pt[1].X = 1.0;
4819 pt[1].Y = 0.0;
4820 pt[2].X = 0.0;
4821 pt[2].Y = 1.0;
4822 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4823 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4824 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4825 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4826 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4828 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4829 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4831 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4832 scaled_rect.Y = rect->Y * args.rel_height;
4833 scaled_rect.Width = rect->Width * args.rel_width;
4834 scaled_rect.Height = rect->Height * args.rel_height;
4835 if (scaled_rect.Width >= 0.5)
4837 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4838 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4841 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4842 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4844 get_font_hfont(graphics, font, format, &gdifont, NULL);
4845 oldfont = SelectObject(hdc, gdifont);
4847 bounds->X = rect->X;
4848 bounds->Y = rect->Y;
4849 bounds->Width = 0.0;
4850 bounds->Height = 0.0;
4852 args.bounds = bounds;
4853 args.codepointsfitted = &glyphs;
4854 args.linesfilled = &lines;
4855 lines = glyphs = 0;
4857 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4858 measure_string_callback, &args);
4860 if (linesfilled) *linesfilled = lines;
4861 if (codepointsfitted) *codepointsfitted = glyphs;
4863 if (lines)
4864 bounds->Width += margin_x * 2.0;
4866 SelectObject(hdc, oldfont);
4867 DeleteObject(gdifont);
4869 if (temp_hdc)
4870 DeleteDC(temp_hdc);
4872 return Ok;
4875 struct draw_string_args {
4876 GpGraphics *graphics;
4877 GDIPCONST GpBrush *brush;
4878 REAL x, y, rel_width, rel_height, ascent;
4881 static GpStatus draw_string_callback(HDC hdc,
4882 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4883 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4884 INT lineno, const RectF *bounds, INT *underlined_indexes,
4885 INT underlined_index_count, void *user_data)
4887 struct draw_string_args *args = user_data;
4888 PointF position;
4889 GpStatus stat;
4891 position.X = args->x + bounds->X / args->rel_width;
4892 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4894 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4895 args->brush, &position,
4896 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4898 if (stat == Ok && underlined_index_count)
4900 OUTLINETEXTMETRICW otm;
4901 REAL underline_y, underline_height;
4902 int i;
4904 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4906 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4907 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4909 for (i=0; i<underlined_index_count; i++)
4911 REAL start_x, end_x;
4912 SIZE text_size;
4913 INT ofs = underlined_indexes[i] - index;
4915 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4916 start_x = text_size.cx / args->rel_width;
4918 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4919 end_x = text_size.cx / args->rel_width;
4921 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4925 return stat;
4928 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4929 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4930 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4932 HRGN rgn = NULL;
4933 HFONT gdifont;
4934 GpPointF pt[3], rectcpy[4];
4935 POINT corners[4];
4936 REAL rel_width, rel_height, margin_x;
4937 INT save_state, format_flags = 0;
4938 REAL offsety = 0.0;
4939 struct draw_string_args args;
4940 RectF scaled_rect;
4941 HDC hdc, temp_hdc=NULL;
4942 TEXTMETRICW textmetric;
4944 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4945 length, font, debugstr_rectf(rect), format, brush);
4947 if(!graphics || !string || !font || !brush || !rect)
4948 return InvalidParameter;
4950 if(graphics->hdc)
4952 hdc = graphics->hdc;
4954 else
4956 hdc = temp_hdc = CreateCompatibleDC(0);
4959 if(format){
4960 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4962 format_flags = format->attr;
4964 /* Should be no need to explicitly test for StringAlignmentNear as
4965 * that is default behavior if no alignment is passed. */
4966 if(format->vertalign != StringAlignmentNear){
4967 RectF bounds, in_rect = *rect;
4968 in_rect.Height = 0.0; /* avoid height clipping */
4969 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
4971 TRACE("bounds %s\n", debugstr_rectf(&bounds));
4973 if(format->vertalign == StringAlignmentCenter)
4974 offsety = (rect->Height - bounds.Height) / 2;
4975 else if(format->vertalign == StringAlignmentFar)
4976 offsety = (rect->Height - bounds.Height);
4978 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
4981 save_state = SaveDC(hdc);
4983 pt[0].X = 0.0;
4984 pt[0].Y = 0.0;
4985 pt[1].X = 1.0;
4986 pt[1].Y = 0.0;
4987 pt[2].X = 0.0;
4988 pt[2].Y = 1.0;
4989 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4990 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4991 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4992 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4993 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4995 rectcpy[3].X = rectcpy[0].X = rect->X;
4996 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
4997 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4998 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
4999 transform_and_round_points(graphics, corners, rectcpy, 4);
5001 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5002 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5004 scaled_rect.X = margin_x * rel_width;
5005 scaled_rect.Y = 0.0;
5006 scaled_rect.Width = rel_width * rect->Width;
5007 scaled_rect.Height = rel_height * rect->Height;
5008 if (scaled_rect.Width >= 0.5)
5010 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5011 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5014 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5015 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5017 if (!(format_flags & StringFormatFlagsNoClip) &&
5018 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23)
5020 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5021 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5022 SelectClipRgn(hdc, rgn);
5025 get_font_hfont(graphics, font, format, &gdifont, NULL);
5026 SelectObject(hdc, gdifont);
5028 args.graphics = graphics;
5029 args.brush = brush;
5031 args.x = rect->X;
5032 args.y = rect->Y + offsety;
5034 args.rel_width = rel_width;
5035 args.rel_height = rel_height;
5037 GetTextMetricsW(hdc, &textmetric);
5038 args.ascent = textmetric.tmAscent / rel_height;
5040 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5041 draw_string_callback, &args);
5043 DeleteObject(rgn);
5044 DeleteObject(gdifont);
5046 RestoreDC(hdc, save_state);
5048 DeleteDC(temp_hdc);
5050 return Ok;
5053 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5055 TRACE("(%p)\n", graphics);
5057 if(!graphics)
5058 return InvalidParameter;
5060 if(graphics->busy)
5061 return ObjectBusy;
5063 return GdipSetInfinite(graphics->clip);
5066 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5068 TRACE("(%p)\n", graphics);
5070 if(!graphics)
5071 return InvalidParameter;
5073 if(graphics->busy)
5074 return ObjectBusy;
5076 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5079 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5081 return GdipEndContainer(graphics, state);
5084 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5085 GpMatrixOrder order)
5087 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5089 if(!graphics)
5090 return InvalidParameter;
5092 if(graphics->busy)
5093 return ObjectBusy;
5095 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5098 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5100 return GdipBeginContainer2(graphics, state);
5103 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5104 GraphicsContainer *state)
5106 GraphicsContainerItem *container;
5107 GpStatus sts;
5109 TRACE("(%p, %p)\n", graphics, state);
5111 if(!graphics || !state)
5112 return InvalidParameter;
5114 sts = init_container(&container, graphics);
5115 if(sts != Ok)
5116 return sts;
5118 list_add_head(&graphics->containers, &container->entry);
5119 *state = graphics->contid = container->contid;
5121 return Ok;
5124 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5126 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5127 return NotImplemented;
5130 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5132 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5133 return NotImplemented;
5136 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5138 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5139 return NotImplemented;
5142 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5144 GpStatus sts;
5145 GraphicsContainerItem *container, *container2;
5147 TRACE("(%p, %x)\n", graphics, state);
5149 if(!graphics)
5150 return InvalidParameter;
5152 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5153 if(container->contid == state)
5154 break;
5157 /* did not find a matching container */
5158 if(&container->entry == &graphics->containers)
5159 return Ok;
5161 sts = restore_container(graphics, container);
5162 if(sts != Ok)
5163 return sts;
5165 /* remove all of the containers on top of the found container */
5166 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5167 if(container->contid == state)
5168 break;
5169 list_remove(&container->entry);
5170 delete_container(container);
5173 list_remove(&container->entry);
5174 delete_container(container);
5176 return Ok;
5179 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5180 REAL sy, GpMatrixOrder order)
5182 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5184 if(!graphics)
5185 return InvalidParameter;
5187 if(graphics->busy)
5188 return ObjectBusy;
5190 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5193 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5194 CombineMode mode)
5196 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5198 if(!graphics || !srcgraphics)
5199 return InvalidParameter;
5201 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5204 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5205 CompositingMode mode)
5207 TRACE("(%p, %d)\n", graphics, mode);
5209 if(!graphics)
5210 return InvalidParameter;
5212 if(graphics->busy)
5213 return ObjectBusy;
5215 graphics->compmode = mode;
5217 return Ok;
5220 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5221 CompositingQuality quality)
5223 TRACE("(%p, %d)\n", graphics, quality);
5225 if(!graphics)
5226 return InvalidParameter;
5228 if(graphics->busy)
5229 return ObjectBusy;
5231 graphics->compqual = quality;
5233 return Ok;
5236 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5237 InterpolationMode mode)
5239 TRACE("(%p, %d)\n", graphics, mode);
5241 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5242 return InvalidParameter;
5244 if(graphics->busy)
5245 return ObjectBusy;
5247 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5248 mode = InterpolationModeBilinear;
5250 if (mode == InterpolationModeHighQuality)
5251 mode = InterpolationModeHighQualityBicubic;
5253 graphics->interpolation = mode;
5255 return Ok;
5258 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5260 TRACE("(%p, %.2f)\n", graphics, scale);
5262 if(!graphics || (scale <= 0.0))
5263 return InvalidParameter;
5265 if(graphics->busy)
5266 return ObjectBusy;
5268 graphics->scale = scale;
5270 return Ok;
5273 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5275 TRACE("(%p, %d)\n", graphics, unit);
5277 if(!graphics)
5278 return InvalidParameter;
5280 if(graphics->busy)
5281 return ObjectBusy;
5283 if(unit == UnitWorld)
5284 return InvalidParameter;
5286 graphics->unit = unit;
5288 return Ok;
5291 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5292 mode)
5294 TRACE("(%p, %d)\n", graphics, mode);
5296 if(!graphics)
5297 return InvalidParameter;
5299 if(graphics->busy)
5300 return ObjectBusy;
5302 graphics->pixeloffset = mode;
5304 return Ok;
5307 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5309 static int calls;
5311 TRACE("(%p,%i,%i)\n", graphics, x, y);
5313 if (!(calls++))
5314 FIXME("value is unused in rendering\n");
5316 if (!graphics)
5317 return InvalidParameter;
5319 graphics->origin_x = x;
5320 graphics->origin_y = y;
5322 return Ok;
5325 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5327 TRACE("(%p,%p,%p)\n", graphics, x, y);
5329 if (!graphics || !x || !y)
5330 return InvalidParameter;
5332 *x = graphics->origin_x;
5333 *y = graphics->origin_y;
5335 return Ok;
5338 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5340 TRACE("(%p, %d)\n", graphics, mode);
5342 if(!graphics)
5343 return InvalidParameter;
5345 if(graphics->busy)
5346 return ObjectBusy;
5348 graphics->smoothing = mode;
5350 return Ok;
5353 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5355 TRACE("(%p, %d)\n", graphics, contrast);
5357 if(!graphics)
5358 return InvalidParameter;
5360 graphics->textcontrast = contrast;
5362 return Ok;
5365 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5366 TextRenderingHint hint)
5368 TRACE("(%p, %d)\n", graphics, hint);
5370 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5371 return InvalidParameter;
5373 if(graphics->busy)
5374 return ObjectBusy;
5376 graphics->texthint = hint;
5378 return Ok;
5381 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5383 TRACE("(%p, %p)\n", graphics, matrix);
5385 if(!graphics || !matrix)
5386 return InvalidParameter;
5388 if(graphics->busy)
5389 return ObjectBusy;
5391 TRACE("%f,%f,%f,%f,%f,%f\n",
5392 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5393 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5395 graphics->worldtrans = *matrix;
5397 return Ok;
5400 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5401 REAL dy, GpMatrixOrder order)
5403 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5405 if(!graphics)
5406 return InvalidParameter;
5408 if(graphics->busy)
5409 return ObjectBusy;
5411 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5414 /*****************************************************************************
5415 * GdipSetClipHrgn [GDIPLUS.@]
5417 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5419 GpRegion *region;
5420 GpStatus status;
5422 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5424 if(!graphics)
5425 return InvalidParameter;
5427 if(graphics->busy)
5428 return ObjectBusy;
5430 /* hrgn is already in device units */
5431 status = GdipCreateRegionHrgn(hrgn, &region);
5432 if(status != Ok)
5433 return status;
5435 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5437 GdipDeleteRegion(region);
5438 return status;
5441 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5443 GpStatus status;
5444 GpPath *clip_path;
5446 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5448 if(!graphics)
5449 return InvalidParameter;
5451 if(graphics->busy)
5452 return ObjectBusy;
5454 status = GdipClonePath(path, &clip_path);
5455 if (status == Ok)
5457 GpMatrix world_to_device;
5459 get_graphics_transform(graphics, CoordinateSpaceDevice,
5460 CoordinateSpaceWorld, &world_to_device);
5461 status = GdipTransformPath(clip_path, &world_to_device);
5462 if (status == Ok)
5463 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5465 GdipDeletePath(clip_path);
5467 return status;
5470 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5471 REAL width, REAL height,
5472 CombineMode mode)
5474 GpRectF rect;
5476 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5478 if(!graphics)
5479 return InvalidParameter;
5481 if(graphics->busy)
5482 return ObjectBusy;
5484 rect.X = x;
5485 rect.Y = y;
5486 rect.Width = width;
5487 rect.Height = height;
5488 transform_rectf(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &rect);
5490 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5493 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5494 INT width, INT height,
5495 CombineMode mode)
5497 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5499 if(!graphics)
5500 return InvalidParameter;
5502 if(graphics->busy)
5503 return ObjectBusy;
5505 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5508 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5509 CombineMode mode)
5511 GpStatus status;
5512 GpRegion *clip;
5514 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5516 if(!graphics || !region)
5517 return InvalidParameter;
5519 if(graphics->busy)
5520 return ObjectBusy;
5522 status = GdipCloneRegion(region, &clip);
5523 if (status == Ok)
5525 GpMatrix world_to_device;
5527 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5528 status = GdipTransformRegion(clip, &world_to_device);
5529 if (status == Ok)
5530 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5532 GdipDeleteRegion(clip);
5534 return status;
5537 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5538 UINT limitDpi)
5540 static int calls;
5542 TRACE("(%p,%u)\n", metafile, limitDpi);
5544 if(!(calls++))
5545 FIXME("not implemented\n");
5547 return NotImplemented;
5550 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5551 INT count)
5553 INT save_state;
5554 POINT *pti;
5556 TRACE("(%p, %p, %d)\n", graphics, points, count);
5558 if(!graphics || !pen || count<=0)
5559 return InvalidParameter;
5561 if(graphics->busy)
5562 return ObjectBusy;
5564 if (!graphics->hdc)
5566 FIXME("graphics object has no HDC\n");
5567 return Ok;
5570 pti = GdipAlloc(sizeof(POINT) * count);
5572 save_state = prepare_dc(graphics, pen);
5573 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5575 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5576 Polygon(graphics->hdc, pti, count);
5578 restore_dc(graphics, save_state);
5579 GdipFree(pti);
5581 return Ok;
5584 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5585 INT count)
5587 GpStatus ret;
5588 GpPointF *ptf;
5589 INT i;
5591 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5593 if(count<=0) return InvalidParameter;
5594 ptf = GdipAlloc(sizeof(GpPointF) * count);
5596 for(i = 0;i < count; i++){
5597 ptf[i].X = (REAL)points[i].X;
5598 ptf[i].Y = (REAL)points[i].Y;
5601 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5602 GdipFree(ptf);
5604 return ret;
5607 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5609 TRACE("(%p, %p)\n", graphics, dpi);
5611 if(!graphics || !dpi)
5612 return InvalidParameter;
5614 if(graphics->busy)
5615 return ObjectBusy;
5617 *dpi = graphics->xres;
5618 return Ok;
5621 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5623 TRACE("(%p, %p)\n", graphics, dpi);
5625 if(!graphics || !dpi)
5626 return InvalidParameter;
5628 if(graphics->busy)
5629 return ObjectBusy;
5631 *dpi = graphics->yres;
5632 return Ok;
5635 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5636 GpMatrixOrder order)
5638 GpMatrix m;
5639 GpStatus ret;
5641 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5643 if(!graphics || !matrix)
5644 return InvalidParameter;
5646 if(graphics->busy)
5647 return ObjectBusy;
5649 m = graphics->worldtrans;
5651 ret = GdipMultiplyMatrix(&m, matrix, order);
5652 if(ret == Ok)
5653 graphics->worldtrans = m;
5655 return ret;
5658 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5659 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5661 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5663 GpStatus stat=Ok;
5665 TRACE("(%p, %p)\n", graphics, hdc);
5667 if(!graphics || !hdc)
5668 return InvalidParameter;
5670 if(graphics->busy)
5671 return ObjectBusy;
5673 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5675 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5677 else if (!graphics->hdc || graphics->alpha_hdc ||
5678 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5680 /* Create a fake HDC and fill it with a constant color. */
5681 HDC temp_hdc;
5682 HBITMAP hbitmap;
5683 GpRectF bounds;
5684 BITMAPINFOHEADER bmih;
5685 int i;
5687 stat = get_graphics_bounds(graphics, &bounds);
5688 if (stat != Ok)
5689 return stat;
5691 graphics->temp_hbitmap_width = bounds.Width;
5692 graphics->temp_hbitmap_height = bounds.Height;
5694 bmih.biSize = sizeof(bmih);
5695 bmih.biWidth = graphics->temp_hbitmap_width;
5696 bmih.biHeight = -graphics->temp_hbitmap_height;
5697 bmih.biPlanes = 1;
5698 bmih.biBitCount = 32;
5699 bmih.biCompression = BI_RGB;
5700 bmih.biSizeImage = 0;
5701 bmih.biXPelsPerMeter = 0;
5702 bmih.biYPelsPerMeter = 0;
5703 bmih.biClrUsed = 0;
5704 bmih.biClrImportant = 0;
5706 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5707 (void**)&graphics->temp_bits, NULL, 0);
5708 if (!hbitmap)
5709 return GenericError;
5711 temp_hdc = CreateCompatibleDC(0);
5712 if (!temp_hdc)
5714 DeleteObject(hbitmap);
5715 return GenericError;
5718 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5719 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5721 SelectObject(temp_hdc, hbitmap);
5723 graphics->temp_hbitmap = hbitmap;
5724 *hdc = graphics->temp_hdc = temp_hdc;
5726 else
5728 *hdc = graphics->hdc;
5731 if (stat == Ok)
5732 graphics->busy = TRUE;
5734 return stat;
5737 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5739 GpStatus stat=Ok;
5741 TRACE("(%p, %p)\n", graphics, hdc);
5743 if(!graphics || !hdc || !graphics->busy)
5744 return InvalidParameter;
5746 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5748 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5750 else if (graphics->temp_hdc == hdc)
5752 DWORD* pos;
5753 int i;
5755 /* Find the pixels that have changed, and mark them as opaque. */
5756 pos = (DWORD*)graphics->temp_bits;
5757 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5759 if (*pos != DC_BACKGROUND_KEY)
5761 *pos |= 0xff000000;
5763 pos++;
5766 /* Write the changed pixels to the real target. */
5767 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5768 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5769 graphics->temp_hbitmap_width * 4);
5771 /* Clean up. */
5772 DeleteDC(graphics->temp_hdc);
5773 DeleteObject(graphics->temp_hbitmap);
5774 graphics->temp_hdc = NULL;
5775 graphics->temp_hbitmap = NULL;
5777 else if (hdc != graphics->hdc)
5779 stat = InvalidParameter;
5782 if (stat == Ok)
5783 graphics->busy = FALSE;
5785 return stat;
5788 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5790 GpRegion *clip;
5791 GpStatus status;
5792 GpMatrix device_to_world;
5794 TRACE("(%p, %p)\n", graphics, region);
5796 if(!graphics || !region)
5797 return InvalidParameter;
5799 if(graphics->busy)
5800 return ObjectBusy;
5802 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5803 return status;
5805 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5806 status = GdipTransformRegion(clip, &device_to_world);
5807 if (status != Ok)
5809 GdipDeleteRegion(clip);
5810 return status;
5813 /* free everything except root node and header */
5814 delete_element(&region->node);
5815 memcpy(region, clip, sizeof(GpRegion));
5816 GdipFree(clip);
5818 return Ok;
5821 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5822 GpCoordinateSpace src_space, GpMatrix *matrix)
5824 GpStatus stat = Ok;
5825 REAL scale_x, scale_y;
5827 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5829 if (dst_space != src_space)
5831 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5832 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5834 if(graphics->unit != UnitDisplay)
5836 scale_x *= graphics->scale;
5837 scale_y *= graphics->scale;
5840 /* transform from src_space to CoordinateSpacePage */
5841 switch (src_space)
5843 case CoordinateSpaceWorld:
5844 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5845 break;
5846 case CoordinateSpacePage:
5847 break;
5848 case CoordinateSpaceDevice:
5849 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5850 break;
5853 /* transform from CoordinateSpacePage to dst_space */
5854 switch (dst_space)
5856 case CoordinateSpaceWorld:
5858 GpMatrix inverted_transform = graphics->worldtrans;
5859 stat = GdipInvertMatrix(&inverted_transform);
5860 if (stat == Ok)
5861 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5862 break;
5864 case CoordinateSpacePage:
5865 break;
5866 case CoordinateSpaceDevice:
5867 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5868 break;
5871 return stat;
5874 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5875 GpCoordinateSpace src_space, GpPointF *points, INT count)
5877 GpMatrix matrix;
5878 GpStatus stat;
5880 if(!graphics || !points || count <= 0)
5881 return InvalidParameter;
5883 if(graphics->busy)
5884 return ObjectBusy;
5886 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5888 if (src_space == dst_space) return Ok;
5890 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5891 if (stat != Ok) return stat;
5893 return GdipTransformMatrixPoints(&matrix, points, count);
5896 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5897 GpCoordinateSpace src_space, GpPoint *points, INT count)
5899 GpPointF *pointsF;
5900 GpStatus ret;
5901 INT i;
5903 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5905 if(count <= 0)
5906 return InvalidParameter;
5908 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5909 if(!pointsF)
5910 return OutOfMemory;
5912 for(i = 0; i < count; i++){
5913 pointsF[i].X = (REAL)points[i].X;
5914 pointsF[i].Y = (REAL)points[i].Y;
5917 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5919 if(ret == Ok)
5920 for(i = 0; i < count; i++){
5921 points[i].X = gdip_round(pointsF[i].X);
5922 points[i].Y = gdip_round(pointsF[i].Y);
5924 GdipFree(pointsF);
5926 return ret;
5929 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5931 static int calls;
5933 TRACE("\n");
5935 if (!calls++)
5936 FIXME("stub\n");
5938 return NULL;
5941 /*****************************************************************************
5942 * GdipTranslateClip [GDIPLUS.@]
5944 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5946 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5948 if(!graphics)
5949 return InvalidParameter;
5951 if(graphics->busy)
5952 return ObjectBusy;
5954 return GdipTranslateRegion(graphics->clip, dx, dy);
5957 /*****************************************************************************
5958 * GdipTranslateClipI [GDIPLUS.@]
5960 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5962 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5964 if(!graphics)
5965 return InvalidParameter;
5967 if(graphics->busy)
5968 return ObjectBusy;
5970 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5974 /*****************************************************************************
5975 * GdipMeasureDriverString [GDIPLUS.@]
5977 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5978 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5979 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5981 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
5982 HFONT hfont;
5983 HDC hdc;
5984 REAL min_x, min_y, max_x, max_y, x, y;
5985 int i;
5986 TEXTMETRICW textmetric;
5987 const WORD *glyph_indices;
5988 WORD *dynamic_glyph_indices=NULL;
5989 REAL rel_width, rel_height, ascent, descent;
5990 GpPointF pt[3];
5992 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5994 if (!graphics || !text || !font || !positions || !boundingBox)
5995 return InvalidParameter;
5997 if (length == -1)
5998 length = strlenW(text);
6000 if (length == 0)
6002 boundingBox->X = 0.0;
6003 boundingBox->Y = 0.0;
6004 boundingBox->Width = 0.0;
6005 boundingBox->Height = 0.0;
6008 if (flags & unsupported_flags)
6009 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6011 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6013 hdc = CreateCompatibleDC(0);
6014 SelectObject(hdc, hfont);
6016 GetTextMetricsW(hdc, &textmetric);
6018 pt[0].X = 0.0;
6019 pt[0].Y = 0.0;
6020 pt[1].X = 1.0;
6021 pt[1].Y = 0.0;
6022 pt[2].X = 0.0;
6023 pt[2].Y = 1.0;
6024 if (matrix)
6026 GpMatrix xform = *matrix;
6027 GdipTransformMatrixPoints(&xform, pt, 3);
6029 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6030 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6031 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6032 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6033 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6035 if (flags & DriverStringOptionsCmapLookup)
6037 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6038 if (!glyph_indices)
6040 DeleteDC(hdc);
6041 DeleteObject(hfont);
6042 return OutOfMemory;
6045 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6047 else
6048 glyph_indices = text;
6050 min_x = max_x = x = positions[0].X;
6051 min_y = max_y = y = positions[0].Y;
6053 ascent = textmetric.tmAscent / rel_height;
6054 descent = textmetric.tmDescent / rel_height;
6056 for (i=0; i<length; i++)
6058 int char_width;
6059 ABC abc;
6061 if (!(flags & DriverStringOptionsRealizedAdvance))
6063 x = positions[i].X;
6064 y = positions[i].Y;
6067 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6068 char_width = abc.abcA + abc.abcB + abc.abcC;
6070 if (min_y > y - ascent) min_y = y - ascent;
6071 if (max_y < y + descent) max_y = y + descent;
6072 if (min_x > x) min_x = x;
6074 x += char_width / rel_width;
6076 if (max_x < x) max_x = x;
6079 GdipFree(dynamic_glyph_indices);
6080 DeleteDC(hdc);
6081 DeleteObject(hfont);
6083 boundingBox->X = min_x;
6084 boundingBox->Y = min_y;
6085 boundingBox->Width = max_x - min_x;
6086 boundingBox->Height = max_y - min_y;
6088 return Ok;
6091 static GpStatus GDI32_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 = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6097 INT save_state;
6098 GpPointF pt;
6099 HFONT hfont;
6100 UINT eto_flags=0;
6102 if (flags & unsupported_flags)
6103 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6105 if (!(flags & DriverStringOptionsCmapLookup))
6106 eto_flags |= ETO_GLYPH_INDEX;
6108 save_state = SaveDC(graphics->hdc);
6109 SetBkMode(graphics->hdc, TRANSPARENT);
6110 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6112 pt = positions[0];
6113 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6115 get_font_hfont(graphics, font, format, &hfont, matrix);
6116 SelectObject(graphics->hdc, hfont);
6118 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6120 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6122 RestoreDC(graphics->hdc, save_state);
6124 DeleteObject(hfont);
6126 return Ok;
6129 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6130 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6131 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6132 INT flags, GDIPCONST GpMatrix *matrix)
6134 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6135 GpStatus stat;
6136 PointF *real_positions, real_position;
6137 POINT *pti;
6138 HFONT hfont;
6139 HDC hdc;
6140 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6141 DWORD max_glyphsize=0;
6142 GLYPHMETRICS glyphmetrics;
6143 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6144 BYTE *glyph_mask;
6145 BYTE *text_mask;
6146 int text_mask_stride;
6147 BYTE *pixel_data;
6148 int pixel_data_stride;
6149 GpRect pixel_area;
6150 UINT ggo_flags = GGO_GRAY8_BITMAP;
6152 if (length <= 0)
6153 return Ok;
6155 if (!(flags & DriverStringOptionsCmapLookup))
6156 ggo_flags |= GGO_GLYPH_INDEX;
6158 if (flags & unsupported_flags)
6159 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6161 pti = GdipAlloc(sizeof(POINT) * length);
6162 if (!pti)
6163 return OutOfMemory;
6165 if (flags & DriverStringOptionsRealizedAdvance)
6167 real_position = positions[0];
6169 transform_and_round_points(graphics, pti, &real_position, 1);
6171 else
6173 real_positions = GdipAlloc(sizeof(PointF) * length);
6174 if (!real_positions)
6176 GdipFree(pti);
6177 return OutOfMemory;
6180 memcpy(real_positions, positions, sizeof(PointF) * length);
6182 transform_and_round_points(graphics, pti, real_positions, length);
6184 GdipFree(real_positions);
6187 get_font_hfont(graphics, font, format, &hfont, matrix);
6189 hdc = CreateCompatibleDC(0);
6190 SelectObject(hdc, hfont);
6192 /* Get the boundaries of the text to be drawn */
6193 for (i=0; i<length; i++)
6195 DWORD glyphsize;
6196 int left, top, right, bottom;
6198 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6199 &glyphmetrics, 0, NULL, &identity);
6201 if (glyphsize == GDI_ERROR)
6203 ERR("GetGlyphOutlineW failed\n");
6204 GdipFree(pti);
6205 DeleteDC(hdc);
6206 DeleteObject(hfont);
6207 return GenericError;
6210 if (glyphsize > max_glyphsize)
6211 max_glyphsize = glyphsize;
6213 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6214 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6215 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6216 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6218 if (left < min_x) min_x = left;
6219 if (top < min_y) min_y = top;
6220 if (right > max_x) max_x = right;
6221 if (bottom > max_y) max_y = bottom;
6223 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6225 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6226 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6230 glyph_mask = GdipAlloc(max_glyphsize);
6231 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6232 text_mask_stride = max_x - min_x;
6234 if (!(glyph_mask && text_mask))
6236 GdipFree(glyph_mask);
6237 GdipFree(text_mask);
6238 GdipFree(pti);
6239 DeleteDC(hdc);
6240 DeleteObject(hfont);
6241 return OutOfMemory;
6244 /* Generate a mask for the text */
6245 for (i=0; i<length; i++)
6247 int left, top, stride;
6249 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6250 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6252 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6253 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6254 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6256 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6258 BYTE *glyph_val = glyph_mask + y * stride;
6259 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6260 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6262 *text_val = min(64, *text_val + *glyph_val);
6263 glyph_val++;
6264 text_val++;
6269 GdipFree(pti);
6270 DeleteDC(hdc);
6271 DeleteObject(hfont);
6272 GdipFree(glyph_mask);
6274 /* get the brush data */
6275 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6276 if (!pixel_data)
6278 GdipFree(text_mask);
6279 return OutOfMemory;
6282 pixel_area.X = min_x;
6283 pixel_area.Y = min_y;
6284 pixel_area.Width = max_x - min_x;
6285 pixel_area.Height = max_y - min_y;
6286 pixel_data_stride = pixel_area.Width * 4;
6288 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6289 if (stat != Ok)
6291 GdipFree(text_mask);
6292 GdipFree(pixel_data);
6293 return stat;
6296 /* multiply the brush data by the mask */
6297 for (y=0; y<pixel_area.Height; y++)
6299 BYTE *text_val = text_mask + text_mask_stride * y;
6300 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6301 for (x=0; x<pixel_area.Width; x++)
6303 *pixel_val = (*pixel_val) * (*text_val) / 64;
6304 text_val++;
6305 pixel_val+=4;
6309 GdipFree(text_mask);
6311 /* draw the result */
6312 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6313 pixel_area.Height, pixel_data_stride);
6315 GdipFree(pixel_data);
6317 return stat;
6320 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6321 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6322 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6323 INT flags, GDIPCONST GpMatrix *matrix)
6325 GpStatus stat = NotImplemented;
6327 if (length == -1)
6328 length = strlenW(text);
6330 if (graphics->hdc && !graphics->alpha_hdc &&
6331 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6332 brush->bt == BrushTypeSolidColor &&
6333 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6334 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6335 brush, positions, flags, matrix);
6336 if (stat == NotImplemented)
6337 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6338 brush, positions, flags, matrix);
6339 return stat;
6342 /*****************************************************************************
6343 * GdipDrawDriverString [GDIPLUS.@]
6345 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6346 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6347 GDIPCONST PointF *positions, INT flags,
6348 GDIPCONST GpMatrix *matrix )
6350 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6352 if (!graphics || !text || !font || !brush || !positions)
6353 return InvalidParameter;
6355 return draw_driver_string(graphics, text, length, font, NULL,
6356 brush, positions, flags, matrix);
6359 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6360 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6362 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6363 return NotImplemented;
6366 /*****************************************************************************
6367 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6369 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6371 GpStatus stat;
6372 GpRegion* rgn;
6374 TRACE("(%p, %p)\n", graphics, res);
6376 if((stat = GdipCreateRegion(&rgn)) != Ok)
6377 return stat;
6379 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6380 goto cleanup;
6382 stat = GdipIsEmptyRegion(rgn, graphics, res);
6384 cleanup:
6385 GdipDeleteRegion(rgn);
6386 return stat;
6389 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6391 static int calls;
6393 TRACE("(%p) stub\n", graphics);
6395 if(!(calls++))
6396 FIXME("not implemented\n");
6398 return NotImplemented;