gdiplus: Use clip region in GdipDrawPath.
[wine.git] / dlls / gdiplus / graphics.c
blobad5e2be292976afc9f5453014f47dff6856395ea
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
55 GpCoordinateSpace src_space, GpMatrix *matrix);
57 /* Converts from gdiplus path point type to gdi path point type. */
58 static BYTE convert_path_point_type(BYTE type)
60 BYTE ret;
62 switch(type & PathPointTypePathTypeMask){
63 case PathPointTypeBezier:
64 ret = PT_BEZIERTO;
65 break;
66 case PathPointTypeLine:
67 ret = PT_LINETO;
68 break;
69 case PathPointTypeStart:
70 ret = PT_MOVETO;
71 break;
72 default:
73 ERR("Bad point type\n");
74 return 0;
77 if(type & PathPointTypeCloseSubpath)
78 ret |= PT_CLOSEFIGURE;
80 return ret;
83 static COLORREF get_gdi_brush_color(const GpBrush *brush)
85 ARGB argb;
87 switch (brush->bt)
89 case BrushTypeSolidColor:
91 const GpSolidFill *sf = (const GpSolidFill *)brush;
92 argb = sf->color;
93 break;
95 case BrushTypeHatchFill:
97 const GpHatch *hatch = (const GpHatch *)brush;
98 argb = hatch->forecol;
99 break;
101 case BrushTypeLinearGradient:
103 const GpLineGradient *line = (const GpLineGradient *)brush;
104 argb = line->startcolor;
105 break;
107 case BrushTypePathGradient:
109 const GpPathGradient *grad = (const GpPathGradient *)brush;
110 argb = grad->centercolor;
111 break;
113 default:
114 FIXME("unhandled brush type %d\n", brush->bt);
115 argb = 0;
116 break;
118 return ARGB2COLORREF(argb);
121 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
123 HBITMAP hbmp;
124 BITMAPINFOHEADER bmih;
125 DWORD *bits;
126 int x, y;
128 bmih.biSize = sizeof(bmih);
129 bmih.biWidth = 8;
130 bmih.biHeight = 8;
131 bmih.biPlanes = 1;
132 bmih.biBitCount = 32;
133 bmih.biCompression = BI_RGB;
134 bmih.biSizeImage = 0;
136 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
137 if (hbmp)
139 const char *hatch_data;
141 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
143 for (y = 0; y < 8; y++)
145 for (x = 0; x < 8; x++)
147 if (hatch_data[y] & (0x80 >> x))
148 bits[y * 8 + x] = hatch->forecol;
149 else
150 bits[y * 8 + x] = hatch->backcol;
154 else
156 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
158 for (y = 0; y < 64; y++)
159 bits[y] = hatch->forecol;
163 return hbmp;
166 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
168 switch (brush->bt)
170 case BrushTypeSolidColor:
172 const GpSolidFill *sf = (const GpSolidFill *)brush;
173 lb->lbStyle = BS_SOLID;
174 lb->lbColor = ARGB2COLORREF(sf->color);
175 lb->lbHatch = 0;
176 return Ok;
179 case BrushTypeHatchFill:
181 const GpHatch *hatch = (const GpHatch *)brush;
182 HBITMAP hbmp;
184 hbmp = create_hatch_bitmap(hatch);
185 if (!hbmp) return OutOfMemory;
187 lb->lbStyle = BS_PATTERN;
188 lb->lbColor = 0;
189 lb->lbHatch = (ULONG_PTR)hbmp;
190 return Ok;
193 default:
194 FIXME("unhandled brush type %d\n", brush->bt);
195 lb->lbStyle = BS_SOLID;
196 lb->lbColor = get_gdi_brush_color(brush);
197 lb->lbHatch = 0;
198 return Ok;
202 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
204 switch (lb->lbStyle)
206 case BS_PATTERN:
207 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
208 break;
210 return Ok;
213 static HBRUSH create_gdi_brush(const GpBrush *brush)
215 LOGBRUSH lb;
216 HBRUSH gdibrush;
218 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
220 gdibrush = CreateBrushIndirect(&lb);
221 free_gdi_logbrush(&lb);
223 return gdibrush;
226 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
228 LOGBRUSH lb;
229 HPEN gdipen;
230 REAL width;
231 INT save_state, i, numdashes;
232 GpPointF pt[2];
233 DWORD dash_array[MAX_DASHLEN];
235 save_state = SaveDC(graphics->hdc);
237 EndPath(graphics->hdc);
239 if(pen->unit == UnitPixel){
240 width = pen->width;
242 else{
243 /* Get an estimate for the amount the pen width is affected by the world
244 * transform. (This is similar to what some of the wine drivers do.) */
245 pt[0].X = 0.0;
246 pt[0].Y = 0.0;
247 pt[1].X = 1.0;
248 pt[1].Y = 1.0;
249 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
250 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
251 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
253 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
256 if(pen->dash == DashStyleCustom){
257 numdashes = min(pen->numdashes, MAX_DASHLEN);
259 TRACE("dashes are: ");
260 for(i = 0; i < numdashes; i++){
261 dash_array[i] = gdip_round(width * pen->dashes[i]);
262 TRACE("%d, ", dash_array[i]);
264 TRACE("\n and the pen style is %x\n", pen->style);
266 create_gdi_logbrush(pen->brush, &lb);
267 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
268 numdashes, dash_array);
269 free_gdi_logbrush(&lb);
271 else
273 create_gdi_logbrush(pen->brush, &lb);
274 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
275 free_gdi_logbrush(&lb);
278 SelectObject(graphics->hdc, gdipen);
280 return save_state;
283 static void restore_dc(GpGraphics *graphics, INT state)
285 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
286 RestoreDC(graphics->hdc, state);
289 /* This helper applies all the changes that the points listed in ptf need in
290 * order to be drawn on the device context. In the end, this should include at
291 * least:
292 * -scaling by page unit
293 * -applying world transformation
294 * -converting from float to int
295 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
296 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
297 * gdi to draw, and these functions would irreparably mess with line widths.
299 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
300 GpPointF *ptf, INT count)
302 REAL scale_x, scale_y;
303 GpMatrix matrix;
304 int i;
306 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
307 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
309 /* apply page scale */
310 if(graphics->unit != UnitDisplay)
312 scale_x *= graphics->scale;
313 scale_y *= graphics->scale;
316 matrix = graphics->worldtrans;
317 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
318 GdipTransformMatrixPoints(&matrix, ptf, count);
320 for(i = 0; i < count; i++){
321 pti[i].x = gdip_round(ptf[i].X);
322 pti[i].y = gdip_round(ptf[i].Y);
326 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
327 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
329 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
331 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
333 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
334 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
336 else
338 BLENDFUNCTION bf;
340 bf.BlendOp = AC_SRC_OVER;
341 bf.BlendFlags = 0;
342 bf.SourceConstantAlpha = 255;
343 bf.AlphaFormat = AC_SRC_ALPHA;
345 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
346 hdc, src_x, src_y, src_width, src_height, bf);
350 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
352 /* clipping region is in device coords */
353 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
356 /* Draw non-premultiplied ARGB data to the given graphics object */
357 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
358 const BYTE *src, INT src_width, INT src_height, INT src_stride)
360 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
361 INT x, y;
363 for (x=0; x<src_width; x++)
365 for (y=0; y<src_height; y++)
367 ARGB dst_color, src_color;
368 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
369 src_color = ((ARGB*)(src + src_stride * y))[x];
370 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
374 return Ok;
377 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
378 const BYTE *src, INT src_width, INT src_height, INT src_stride)
380 HDC hdc;
381 HBITMAP hbitmap;
382 BITMAPINFOHEADER bih;
383 BYTE *temp_bits;
385 hdc = CreateCompatibleDC(0);
387 bih.biSize = sizeof(BITMAPINFOHEADER);
388 bih.biWidth = src_width;
389 bih.biHeight = -src_height;
390 bih.biPlanes = 1;
391 bih.biBitCount = 32;
392 bih.biCompression = BI_RGB;
393 bih.biSizeImage = 0;
394 bih.biXPelsPerMeter = 0;
395 bih.biYPelsPerMeter = 0;
396 bih.biClrUsed = 0;
397 bih.biClrImportant = 0;
399 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
400 (void**)&temp_bits, NULL, 0);
402 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
403 memcpy(temp_bits, src, src_width * src_height * 4);
404 else
405 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
406 4 * src_width, src, src_stride);
408 SelectObject(hdc, hbitmap);
409 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
410 hdc, 0, 0, src_width, src_height);
411 DeleteDC(hdc);
412 DeleteObject(hbitmap);
414 return Ok;
417 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
418 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
420 GpStatus stat=Ok;
422 if (graphics->image && graphics->image->type == ImageTypeBitmap)
424 DWORD i;
425 int size;
426 RGNDATA *rgndata;
427 RECT *rects;
428 HRGN hrgn, visible_rgn;
430 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
431 if (!hrgn)
432 return OutOfMemory;
434 stat = get_clip_hrgn(graphics, &visible_rgn);
435 if (stat != Ok)
437 DeleteObject(hrgn);
438 return stat;
441 if (visible_rgn)
443 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
444 DeleteObject(visible_rgn);
447 if (hregion)
448 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
450 size = GetRegionData(hrgn, 0, NULL);
452 rgndata = GdipAlloc(size);
453 if (!rgndata)
455 DeleteObject(hrgn);
456 return OutOfMemory;
459 GetRegionData(hrgn, size, rgndata);
461 rects = (RECT*)rgndata->Buffer;
463 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
465 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
466 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
467 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
468 src_stride);
471 GdipFree(rgndata);
473 DeleteObject(hrgn);
475 return stat;
477 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
479 ERR("This should not be used for metafiles; fix caller\n");
480 return NotImplemented;
482 else
484 HRGN hrgn;
485 int save;
487 stat = get_clip_hrgn(graphics, &hrgn);
489 if (stat != Ok)
490 return stat;
492 save = SaveDC(graphics->hdc);
494 if (hrgn)
495 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
497 if (hregion)
498 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
500 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
501 src_height, src_stride);
503 RestoreDC(graphics->hdc, save);
505 DeleteObject(hrgn);
507 return stat;
511 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
512 const BYTE *src, INT src_width, INT src_height, INT src_stride)
514 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL);
517 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
519 ARGB result=0;
520 ARGB i;
521 INT a1, a2, a3;
523 a1 = (start >> 24) & 0xff;
524 a2 = (end >> 24) & 0xff;
526 a3 = (int)(a1*(1.0f - position)+a2*(position));
528 result |= a3 << 24;
530 for (i=0xff; i<=0xff0000; i = i << 8)
531 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
532 return result;
535 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
537 REAL blendfac;
539 /* clamp to between 0.0 and 1.0, using the wrap mode */
540 if (brush->wrap == WrapModeTile)
542 position = fmodf(position, 1.0f);
543 if (position < 0.0f) position += 1.0f;
545 else /* WrapModeFlip* */
547 position = fmodf(position, 2.0f);
548 if (position < 0.0f) position += 2.0f;
549 if (position > 1.0f) position = 2.0f - position;
552 if (brush->blendcount == 1)
553 blendfac = position;
554 else
556 int i=1;
557 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
558 REAL range;
560 /* locate the blend positions surrounding this position */
561 while (position > brush->blendpos[i])
562 i++;
564 /* interpolate between the blend positions */
565 left_blendpos = brush->blendpos[i-1];
566 left_blendfac = brush->blendfac[i-1];
567 right_blendpos = brush->blendpos[i];
568 right_blendfac = brush->blendfac[i];
569 range = right_blendpos - left_blendpos;
570 blendfac = (left_blendfac * (right_blendpos - position) +
571 right_blendfac * (position - left_blendpos)) / range;
574 if (brush->pblendcount == 0)
575 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
576 else
578 int i=1;
579 ARGB left_blendcolor, right_blendcolor;
580 REAL left_blendpos, right_blendpos;
582 /* locate the blend colors surrounding this position */
583 while (blendfac > brush->pblendpos[i])
584 i++;
586 /* interpolate between the blend colors */
587 left_blendpos = brush->pblendpos[i-1];
588 left_blendcolor = brush->pblendcolor[i-1];
589 right_blendpos = brush->pblendpos[i];
590 right_blendcolor = brush->pblendcolor[i];
591 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
592 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
596 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
598 REAL val[5], res[4];
599 int i, j;
600 unsigned char a, r, g, b;
602 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
603 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
604 val[2] = (color & 0xff) / 255.0; /* blue */
605 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
606 val[4] = 1.0; /* translation */
608 for (i=0; i<4; i++)
610 res[i] = 0.0;
612 for (j=0; j<5; j++)
613 res[i] += matrix->m[j][i] * val[j];
616 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
617 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
618 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
619 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
621 return (a << 24) | (r << 16) | (g << 8) | b;
624 static BOOL color_is_gray(ARGB color)
626 unsigned char r, g, b;
628 r = (color >> 16) & 0xff;
629 g = (color >> 8) & 0xff;
630 b = color & 0xff;
632 return (r == g) && (g == b);
635 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
636 UINT width, UINT height, INT stride, ColorAdjustType type)
638 UINT x, y;
639 INT i;
641 if (attributes->colorkeys[type].enabled ||
642 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
644 const struct color_key *key;
645 BYTE min_blue, min_green, min_red;
646 BYTE max_blue, max_green, max_red;
648 if (attributes->colorkeys[type].enabled)
649 key = &attributes->colorkeys[type];
650 else
651 key = &attributes->colorkeys[ColorAdjustTypeDefault];
653 min_blue = key->low&0xff;
654 min_green = (key->low>>8)&0xff;
655 min_red = (key->low>>16)&0xff;
657 max_blue = key->high&0xff;
658 max_green = (key->high>>8)&0xff;
659 max_red = (key->high>>16)&0xff;
661 for (x=0; x<width; x++)
662 for (y=0; y<height; y++)
664 ARGB *src_color;
665 BYTE blue, green, red;
666 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
667 blue = *src_color&0xff;
668 green = (*src_color>>8)&0xff;
669 red = (*src_color>>16)&0xff;
670 if (blue >= min_blue && green >= min_green && red >= min_red &&
671 blue <= max_blue && green <= max_green && red <= max_red)
672 *src_color = 0x00000000;
676 if (attributes->colorremaptables[type].enabled ||
677 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
679 const struct color_remap_table *table;
681 if (attributes->colorremaptables[type].enabled)
682 table = &attributes->colorremaptables[type];
683 else
684 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
686 for (x=0; x<width; x++)
687 for (y=0; y<height; y++)
689 ARGB *src_color;
690 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
691 for (i=0; i<table->mapsize; i++)
693 if (*src_color == table->colormap[i].oldColor.Argb)
695 *src_color = table->colormap[i].newColor.Argb;
696 break;
702 if (attributes->colormatrices[type].enabled ||
703 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
705 const struct color_matrix *colormatrices;
707 if (attributes->colormatrices[type].enabled)
708 colormatrices = &attributes->colormatrices[type];
709 else
710 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
712 for (x=0; x<width; x++)
713 for (y=0; y<height; y++)
715 ARGB *src_color;
716 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
718 if (colormatrices->flags == ColorMatrixFlagsDefault ||
719 !color_is_gray(*src_color))
721 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
723 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
725 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
730 if (attributes->gamma_enabled[type] ||
731 attributes->gamma_enabled[ColorAdjustTypeDefault])
733 REAL gamma;
735 if (attributes->gamma_enabled[type])
736 gamma = attributes->gamma[type];
737 else
738 gamma = attributes->gamma[ColorAdjustTypeDefault];
740 for (x=0; x<width; x++)
741 for (y=0; y<height; y++)
743 ARGB *src_color;
744 BYTE blue, green, red;
745 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
747 blue = *src_color&0xff;
748 green = (*src_color>>8)&0xff;
749 red = (*src_color>>16)&0xff;
751 /* FIXME: We should probably use a table for this. */
752 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
753 green = floorf(powf(green / 255.0, gamma) * 255.0);
754 red = floorf(powf(red / 255.0, gamma) * 255.0);
756 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
761 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
762 * bitmap that contains all the pixels we may need to draw it. */
763 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
764 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
765 GpRect *rect)
767 INT left, top, right, bottom;
769 switch (interpolation)
771 case InterpolationModeHighQualityBilinear:
772 case InterpolationModeHighQualityBicubic:
773 /* FIXME: Include a greater range for the prefilter? */
774 case InterpolationModeBicubic:
775 case InterpolationModeBilinear:
776 left = (INT)(floorf(srcx));
777 top = (INT)(floorf(srcy));
778 right = (INT)(ceilf(srcx+srcwidth));
779 bottom = (INT)(ceilf(srcy+srcheight));
780 break;
781 case InterpolationModeNearestNeighbor:
782 default:
783 left = gdip_round(srcx);
784 top = gdip_round(srcy);
785 right = gdip_round(srcx+srcwidth);
786 bottom = gdip_round(srcy+srcheight);
787 break;
790 if (wrap == WrapModeClamp)
792 if (left < 0)
793 left = 0;
794 if (top < 0)
795 top = 0;
796 if (right >= bitmap->width)
797 right = bitmap->width-1;
798 if (bottom >= bitmap->height)
799 bottom = bitmap->height-1;
801 else
803 /* In some cases we can make the rectangle smaller here, but the logic
804 * is hard to get right, and tiling suggests we're likely to use the
805 * entire source image. */
806 if (left < 0 || right >= bitmap->width)
808 left = 0;
809 right = bitmap->width-1;
812 if (top < 0 || bottom >= bitmap->height)
814 top = 0;
815 bottom = bitmap->height-1;
819 rect->X = left;
820 rect->Y = top;
821 rect->Width = right - left + 1;
822 rect->Height = bottom - top + 1;
825 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
826 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
828 if (attributes->wrap == WrapModeClamp)
830 if (x < 0 || y < 0 || x >= width || y >= height)
831 return attributes->outside_color;
833 else
835 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
836 if (x < 0)
837 x = width*2 + x % (width * 2);
838 if (y < 0)
839 y = height*2 + y % (height * 2);
841 if ((attributes->wrap & 1) == 1)
843 /* Flip X */
844 if ((x / width) % 2 == 0)
845 x = x % width;
846 else
847 x = width - 1 - x % width;
849 else
850 x = x % width;
852 if ((attributes->wrap & 2) == 2)
854 /* Flip Y */
855 if ((y / height) % 2 == 0)
856 y = y % height;
857 else
858 y = height - 1 - y % height;
860 else
861 y = y % height;
864 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
866 ERR("out of range pixel requested\n");
867 return 0xffcd0084;
870 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
873 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
874 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
875 InterpolationMode interpolation, PixelOffsetMode offset_mode)
877 static int fixme;
879 switch (interpolation)
881 default:
882 if (!fixme++)
883 FIXME("Unimplemented interpolation %i\n", interpolation);
884 /* fall-through */
885 case InterpolationModeBilinear:
887 REAL leftxf, topyf;
888 INT leftx, rightx, topy, bottomy;
889 ARGB topleft, topright, bottomleft, bottomright;
890 ARGB top, bottom;
891 float x_offset;
893 leftxf = floorf(point->X);
894 leftx = (INT)leftxf;
895 rightx = (INT)ceilf(point->X);
896 topyf = floorf(point->Y);
897 topy = (INT)topyf;
898 bottomy = (INT)ceilf(point->Y);
900 if (leftx == rightx && topy == bottomy)
901 return sample_bitmap_pixel(src_rect, bits, width, height,
902 leftx, topy, attributes);
904 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
905 leftx, topy, attributes);
906 topright = sample_bitmap_pixel(src_rect, bits, width, height,
907 rightx, topy, attributes);
908 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
909 leftx, bottomy, attributes);
910 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
911 rightx, bottomy, attributes);
913 x_offset = point->X - leftxf;
914 top = blend_colors(topleft, topright, x_offset);
915 bottom = blend_colors(bottomleft, bottomright, x_offset);
917 return blend_colors(top, bottom, point->Y - topyf);
919 case InterpolationModeNearestNeighbor:
921 FLOAT pixel_offset;
922 switch (offset_mode)
924 default:
925 case PixelOffsetModeNone:
926 case PixelOffsetModeHighSpeed:
927 pixel_offset = 0.5;
928 break;
930 case PixelOffsetModeHalf:
931 case PixelOffsetModeHighQuality:
932 pixel_offset = 0.0;
933 break;
935 return sample_bitmap_pixel(src_rect, bits, width, height,
936 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
942 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
944 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
947 static BOOL brush_can_fill_path(GpBrush *brush)
949 switch (brush->bt)
951 case BrushTypeSolidColor:
952 return TRUE;
953 case BrushTypeHatchFill:
955 GpHatch *hatch = (GpHatch*)brush;
956 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
957 ((hatch->backcol & 0xff000000) == 0xff000000);
959 case BrushTypeLinearGradient:
960 case BrushTypeTextureFill:
961 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
962 default:
963 return FALSE;
967 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
969 switch (brush->bt)
971 case BrushTypeSolidColor:
973 GpSolidFill *fill = (GpSolidFill*)brush;
974 HBITMAP bmp = ARGB2BMP(fill->color);
976 if (bmp)
978 RECT rc;
979 /* partially transparent fill */
981 SelectClipPath(graphics->hdc, RGN_AND);
982 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
984 HDC hdc = CreateCompatibleDC(NULL);
986 if (!hdc) break;
988 SelectObject(hdc, bmp);
989 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
990 hdc, 0, 0, 1, 1);
991 DeleteDC(hdc);
994 DeleteObject(bmp);
995 break;
997 /* else fall through */
999 default:
1001 HBRUSH gdibrush, old_brush;
1003 gdibrush = create_gdi_brush(brush);
1004 if (!gdibrush) return;
1006 old_brush = SelectObject(graphics->hdc, gdibrush);
1007 FillPath(graphics->hdc);
1008 SelectObject(graphics->hdc, old_brush);
1009 DeleteObject(gdibrush);
1010 break;
1015 static BOOL brush_can_fill_pixels(GpBrush *brush)
1017 switch (brush->bt)
1019 case BrushTypeSolidColor:
1020 case BrushTypeHatchFill:
1021 case BrushTypeLinearGradient:
1022 case BrushTypeTextureFill:
1023 case BrushTypePathGradient:
1024 return TRUE;
1025 default:
1026 return FALSE;
1030 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1031 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1033 switch (brush->bt)
1035 case BrushTypeSolidColor:
1037 int x, y;
1038 GpSolidFill *fill = (GpSolidFill*)brush;
1039 for (x=0; x<fill_area->Width; x++)
1040 for (y=0; y<fill_area->Height; y++)
1041 argb_pixels[x + y*cdwStride] = fill->color;
1042 return Ok;
1044 case BrushTypeHatchFill:
1046 int x, y;
1047 GpHatch *fill = (GpHatch*)brush;
1048 const char *hatch_data;
1050 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1051 return NotImplemented;
1053 for (x=0; x<fill_area->Width; x++)
1054 for (y=0; y<fill_area->Height; y++)
1056 int hx, hy;
1058 /* FIXME: Account for the rendering origin */
1059 hx = (x + fill_area->X) % 8;
1060 hy = (y + fill_area->Y) % 8;
1062 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1063 argb_pixels[x + y*cdwStride] = fill->forecol;
1064 else
1065 argb_pixels[x + y*cdwStride] = fill->backcol;
1068 return Ok;
1070 case BrushTypeLinearGradient:
1072 GpLineGradient *fill = (GpLineGradient*)brush;
1073 GpPointF draw_points[3], line_points[3];
1074 GpStatus stat;
1075 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1076 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1077 int x, y;
1079 draw_points[0].X = fill_area->X;
1080 draw_points[0].Y = fill_area->Y;
1081 draw_points[1].X = fill_area->X+1;
1082 draw_points[1].Y = fill_area->Y;
1083 draw_points[2].X = fill_area->X;
1084 draw_points[2].Y = fill_area->Y+1;
1086 /* Transform the points to a co-ordinate space where X is the point's
1087 * position in the gradient, 0.0 being the start point and 1.0 the
1088 * end point. */
1089 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1090 CoordinateSpaceDevice, draw_points, 3);
1092 if (stat == Ok)
1094 line_points[0] = fill->startpoint;
1095 line_points[1] = fill->endpoint;
1096 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1097 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1099 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1102 if (stat == Ok)
1104 stat = GdipInvertMatrix(world_to_gradient);
1106 if (stat == Ok)
1107 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1109 GdipDeleteMatrix(world_to_gradient);
1112 if (stat == Ok)
1114 REAL x_delta = draw_points[1].X - draw_points[0].X;
1115 REAL y_delta = draw_points[2].X - draw_points[0].X;
1117 for (y=0; y<fill_area->Height; y++)
1119 for (x=0; x<fill_area->Width; x++)
1121 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1123 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1128 return stat;
1130 case BrushTypeTextureFill:
1132 GpTexture *fill = (GpTexture*)brush;
1133 GpPointF draw_points[3];
1134 GpStatus stat;
1135 int x, y;
1136 GpBitmap *bitmap;
1137 int src_stride;
1138 GpRect src_area;
1140 if (fill->image->type != ImageTypeBitmap)
1142 FIXME("metafile texture brushes not implemented\n");
1143 return NotImplemented;
1146 bitmap = (GpBitmap*)fill->image;
1147 src_stride = sizeof(ARGB) * bitmap->width;
1149 src_area.X = src_area.Y = 0;
1150 src_area.Width = bitmap->width;
1151 src_area.Height = bitmap->height;
1153 draw_points[0].X = fill_area->X;
1154 draw_points[0].Y = fill_area->Y;
1155 draw_points[1].X = fill_area->X+1;
1156 draw_points[1].Y = fill_area->Y;
1157 draw_points[2].X = fill_area->X;
1158 draw_points[2].Y = fill_area->Y+1;
1160 /* Transform the points to the co-ordinate space of the bitmap. */
1161 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1162 CoordinateSpaceDevice, draw_points, 3);
1164 if (stat == Ok)
1166 GpMatrix world_to_texture = fill->transform;
1168 stat = GdipInvertMatrix(&world_to_texture);
1169 if (stat == Ok)
1170 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1173 if (stat == Ok && !fill->bitmap_bits)
1175 BitmapData lockeddata;
1177 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1178 if (!fill->bitmap_bits)
1179 stat = OutOfMemory;
1181 if (stat == Ok)
1183 lockeddata.Width = bitmap->width;
1184 lockeddata.Height = bitmap->height;
1185 lockeddata.Stride = src_stride;
1186 lockeddata.PixelFormat = PixelFormat32bppARGB;
1187 lockeddata.Scan0 = fill->bitmap_bits;
1189 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1190 PixelFormat32bppARGB, &lockeddata);
1193 if (stat == Ok)
1194 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1196 if (stat == Ok)
1197 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1198 bitmap->width, bitmap->height,
1199 src_stride, ColorAdjustTypeBitmap);
1201 if (stat != Ok)
1203 GdipFree(fill->bitmap_bits);
1204 fill->bitmap_bits = NULL;
1208 if (stat == Ok)
1210 REAL x_dx = draw_points[1].X - draw_points[0].X;
1211 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1212 REAL y_dx = draw_points[2].X - draw_points[0].X;
1213 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1215 for (y=0; y<fill_area->Height; y++)
1217 for (x=0; x<fill_area->Width; x++)
1219 GpPointF point;
1220 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1221 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1223 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1224 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1225 &point, fill->imageattributes, graphics->interpolation,
1226 graphics->pixeloffset);
1231 return stat;
1233 case BrushTypePathGradient:
1235 GpPathGradient *fill = (GpPathGradient*)brush;
1236 GpPath *flat_path;
1237 GpMatrix world_to_device;
1238 GpStatus stat;
1239 int i, figure_start=0;
1240 GpPointF start_point, end_point, center_point;
1241 BYTE type;
1242 REAL min_yf, max_yf, line1_xf, line2_xf;
1243 INT min_y, max_y, min_x, max_x;
1244 INT x, y;
1245 ARGB outer_color;
1246 static int transform_fixme_once;
1248 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1250 static int once;
1251 if (!once++)
1252 FIXME("path gradient focus not implemented\n");
1255 if (fill->gamma)
1257 static int once;
1258 if (!once++)
1259 FIXME("path gradient gamma correction not implemented\n");
1262 if (fill->blendcount)
1264 static int once;
1265 if (!once++)
1266 FIXME("path gradient blend not implemented\n");
1269 if (fill->pblendcount)
1271 static int once;
1272 if (!once++)
1273 FIXME("path gradient preset blend not implemented\n");
1276 if (!transform_fixme_once)
1278 BOOL is_identity=TRUE;
1279 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1280 if (!is_identity)
1282 FIXME("path gradient transform not implemented\n");
1283 transform_fixme_once = 1;
1287 stat = GdipClonePath(fill->path, &flat_path);
1289 if (stat != Ok)
1290 return stat;
1292 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1293 CoordinateSpaceWorld, &world_to_device);
1294 if (stat == Ok)
1296 stat = GdipTransformPath(flat_path, &world_to_device);
1298 if (stat == Ok)
1300 center_point = fill->center;
1301 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1304 if (stat == Ok)
1305 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1308 if (stat != Ok)
1310 GdipDeletePath(flat_path);
1311 return stat;
1314 for (i=0; i<flat_path->pathdata.Count; i++)
1316 int start_center_line=0, end_center_line=0;
1317 int seen_start=0, seen_end=0, seen_center=0;
1318 REAL center_distance;
1319 ARGB start_color, end_color;
1320 REAL dy, dx;
1322 type = flat_path->pathdata.Types[i];
1324 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1325 figure_start = i;
1327 start_point = flat_path->pathdata.Points[i];
1329 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1331 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1333 end_point = flat_path->pathdata.Points[figure_start];
1334 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1336 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1338 end_point = flat_path->pathdata.Points[i+1];
1339 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1341 else
1342 continue;
1344 outer_color = start_color;
1346 min_yf = center_point.Y;
1347 if (min_yf > start_point.Y) min_yf = start_point.Y;
1348 if (min_yf > end_point.Y) min_yf = end_point.Y;
1350 if (min_yf < fill_area->Y)
1351 min_y = fill_area->Y;
1352 else
1353 min_y = (INT)ceil(min_yf);
1355 max_yf = center_point.Y;
1356 if (max_yf < start_point.Y) max_yf = start_point.Y;
1357 if (max_yf < end_point.Y) max_yf = end_point.Y;
1359 if (max_yf > fill_area->Y + fill_area->Height)
1360 max_y = fill_area->Y + fill_area->Height;
1361 else
1362 max_y = (INT)ceil(max_yf);
1364 dy = end_point.Y - start_point.Y;
1365 dx = end_point.X - start_point.X;
1367 /* This is proportional to the distance from start-end line to center point. */
1368 center_distance = dy * (start_point.X - center_point.X) +
1369 dx * (center_point.Y - start_point.Y);
1371 for (y=min_y; y<max_y; y++)
1373 REAL yf = (REAL)y;
1375 if (!seen_start && yf >= start_point.Y)
1377 seen_start = 1;
1378 start_center_line ^= 1;
1380 if (!seen_end && yf >= end_point.Y)
1382 seen_end = 1;
1383 end_center_line ^= 1;
1385 if (!seen_center && yf >= center_point.Y)
1387 seen_center = 1;
1388 start_center_line ^= 1;
1389 end_center_line ^= 1;
1392 if (start_center_line)
1393 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1394 else
1395 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1397 if (end_center_line)
1398 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1399 else
1400 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1402 if (line1_xf < line2_xf)
1404 min_x = (INT)ceil(line1_xf);
1405 max_x = (INT)ceil(line2_xf);
1407 else
1409 min_x = (INT)ceil(line2_xf);
1410 max_x = (INT)ceil(line1_xf);
1413 if (min_x < fill_area->X)
1414 min_x = fill_area->X;
1415 if (max_x > fill_area->X + fill_area->Width)
1416 max_x = fill_area->X + fill_area->Width;
1418 for (x=min_x; x<max_x; x++)
1420 REAL xf = (REAL)x;
1421 REAL distance;
1423 if (start_color != end_color)
1425 REAL blend_amount, pdy, pdx;
1426 pdy = yf - center_point.Y;
1427 pdx = xf - center_point.X;
1428 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1429 outer_color = blend_colors(start_color, end_color, blend_amount);
1432 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1433 (end_point.X - start_point.X) * (yf - start_point.Y);
1435 distance = distance / center_distance;
1437 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1438 blend_colors(outer_color, fill->centercolor, distance);
1443 GdipDeletePath(flat_path);
1444 return stat;
1446 default:
1447 return NotImplemented;
1451 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1452 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1453 * should not be called on an hdc that has a path you care about. */
1454 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1455 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1457 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1458 GpMatrix matrix;
1459 HBRUSH brush = NULL;
1460 HPEN pen = NULL;
1461 PointF ptf[4], *custptf = NULL;
1462 POINT pt[4], *custpt = NULL;
1463 BYTE *tp = NULL;
1464 REAL theta, dsmall, dbig, dx, dy = 0.0;
1465 INT i, count;
1466 LOGBRUSH lb;
1467 BOOL customstroke;
1469 if((x1 == x2) && (y1 == y2))
1470 return;
1472 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1474 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1475 if(!customstroke){
1476 brush = CreateSolidBrush(color);
1477 lb.lbStyle = BS_SOLID;
1478 lb.lbColor = color;
1479 lb.lbHatch = 0;
1480 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1481 PS_JOIN_MITER, 1, &lb, 0,
1482 NULL);
1483 oldbrush = SelectObject(graphics->hdc, brush);
1484 oldpen = SelectObject(graphics->hdc, pen);
1487 switch(cap){
1488 case LineCapFlat:
1489 break;
1490 case LineCapSquare:
1491 case LineCapSquareAnchor:
1492 case LineCapDiamondAnchor:
1493 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1494 if(cap == LineCapDiamondAnchor){
1495 dsmall = cos(theta + M_PI_2) * size;
1496 dbig = sin(theta + M_PI_2) * size;
1498 else{
1499 dsmall = cos(theta + M_PI_4) * size;
1500 dbig = sin(theta + M_PI_4) * size;
1503 ptf[0].X = x2 - dsmall;
1504 ptf[1].X = x2 + dbig;
1506 ptf[0].Y = y2 - dbig;
1507 ptf[3].Y = y2 + dsmall;
1509 ptf[1].Y = y2 - dsmall;
1510 ptf[2].Y = y2 + dbig;
1512 ptf[3].X = x2 - dbig;
1513 ptf[2].X = x2 + dsmall;
1515 transform_and_round_points(graphics, pt, ptf, 4);
1516 Polygon(graphics->hdc, pt, 4);
1518 break;
1519 case LineCapArrowAnchor:
1520 size = size * 4.0 / sqrt(3.0);
1522 dx = cos(M_PI / 6.0 + theta) * size;
1523 dy = sin(M_PI / 6.0 + theta) * size;
1525 ptf[0].X = x2 - dx;
1526 ptf[0].Y = y2 - dy;
1528 dx = cos(- M_PI / 6.0 + theta) * size;
1529 dy = sin(- M_PI / 6.0 + theta) * size;
1531 ptf[1].X = x2 - dx;
1532 ptf[1].Y = y2 - dy;
1534 ptf[2].X = x2;
1535 ptf[2].Y = y2;
1537 transform_and_round_points(graphics, pt, ptf, 3);
1538 Polygon(graphics->hdc, pt, 3);
1540 break;
1541 case LineCapRoundAnchor:
1542 dx = dy = ANCHOR_WIDTH * size / 2.0;
1544 ptf[0].X = x2 - dx;
1545 ptf[0].Y = y2 - dy;
1546 ptf[1].X = x2 + dx;
1547 ptf[1].Y = y2 + dy;
1549 transform_and_round_points(graphics, pt, ptf, 2);
1550 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1552 break;
1553 case LineCapTriangle:
1554 size = size / 2.0;
1555 dx = cos(M_PI_2 + theta) * size;
1556 dy = sin(M_PI_2 + theta) * size;
1558 ptf[0].X = x2 - dx;
1559 ptf[0].Y = y2 - dy;
1560 ptf[1].X = x2 + dx;
1561 ptf[1].Y = y2 + dy;
1563 dx = cos(theta) * size;
1564 dy = sin(theta) * size;
1566 ptf[2].X = x2 + dx;
1567 ptf[2].Y = y2 + dy;
1569 transform_and_round_points(graphics, pt, ptf, 3);
1570 Polygon(graphics->hdc, pt, 3);
1572 break;
1573 case LineCapRound:
1574 dx = dy = size / 2.0;
1576 ptf[0].X = x2 - dx;
1577 ptf[0].Y = y2 - dy;
1578 ptf[1].X = x2 + dx;
1579 ptf[1].Y = y2 + dy;
1581 dx = -cos(M_PI_2 + theta) * size;
1582 dy = -sin(M_PI_2 + theta) * size;
1584 ptf[2].X = x2 - dx;
1585 ptf[2].Y = y2 - dy;
1586 ptf[3].X = x2 + dx;
1587 ptf[3].Y = y2 + dy;
1589 transform_and_round_points(graphics, pt, ptf, 4);
1590 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1591 pt[2].y, pt[3].x, pt[3].y);
1593 break;
1594 case LineCapCustom:
1595 if(!custom)
1596 break;
1598 count = custom->pathdata.Count;
1599 custptf = GdipAlloc(count * sizeof(PointF));
1600 custpt = GdipAlloc(count * sizeof(POINT));
1601 tp = GdipAlloc(count);
1603 if(!custptf || !custpt || !tp)
1604 goto custend;
1606 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1608 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1609 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1610 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1611 MatrixOrderAppend);
1612 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1613 GdipTransformMatrixPoints(&matrix, custptf, count);
1615 transform_and_round_points(graphics, custpt, custptf, count);
1617 for(i = 0; i < count; i++)
1618 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1620 if(custom->fill){
1621 BeginPath(graphics->hdc);
1622 PolyDraw(graphics->hdc, custpt, tp, count);
1623 EndPath(graphics->hdc);
1624 StrokeAndFillPath(graphics->hdc);
1626 else
1627 PolyDraw(graphics->hdc, custpt, tp, count);
1629 custend:
1630 GdipFree(custptf);
1631 GdipFree(custpt);
1632 GdipFree(tp);
1633 break;
1634 default:
1635 break;
1638 if(!customstroke){
1639 SelectObject(graphics->hdc, oldbrush);
1640 SelectObject(graphics->hdc, oldpen);
1641 DeleteObject(brush);
1642 DeleteObject(pen);
1646 /* Shortens the line by the given percent by changing x2, y2.
1647 * If percent is > 1.0 then the line will change direction.
1648 * If percent is negative it can lengthen the line. */
1649 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1651 REAL dist, theta, dx, dy;
1653 if((y1 == *y2) && (x1 == *x2))
1654 return;
1656 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1657 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1658 dx = cos(theta) * dist;
1659 dy = sin(theta) * dist;
1661 *x2 = *x2 + dx;
1662 *y2 = *y2 + dy;
1665 /* Shortens the line by the given amount by changing x2, y2.
1666 * If the amount is greater than the distance, the line will become length 0.
1667 * If the amount is negative, it can lengthen the line. */
1668 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1670 REAL dx, dy, percent;
1672 dx = *x2 - x1;
1673 dy = *y2 - y1;
1674 if(dx == 0 && dy == 0)
1675 return;
1677 percent = amt / sqrt(dx * dx + dy * dy);
1678 if(percent >= 1.0){
1679 *x2 = x1;
1680 *y2 = y1;
1681 return;
1684 shorten_line_percent(x1, y1, x2, y2, percent);
1687 /* Conducts a linear search to find the bezier points that will back off
1688 * the endpoint of the curve by a distance of amt. Linear search works
1689 * better than binary in this case because there are multiple solutions,
1690 * and binary searches often find a bad one. I don't think this is what
1691 * Windows does but short of rendering the bezier without GDI's help it's
1692 * the best we can do. If rev then work from the start of the passed points
1693 * instead of the end. */
1694 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1696 GpPointF origpt[4];
1697 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1698 INT i, first = 0, second = 1, third = 2, fourth = 3;
1700 if(rev){
1701 first = 3;
1702 second = 2;
1703 third = 1;
1704 fourth = 0;
1707 origx = pt[fourth].X;
1708 origy = pt[fourth].Y;
1709 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1711 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1712 /* reset bezier points to original values */
1713 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1714 /* Perform magic on bezier points. Order is important here.*/
1715 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1716 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1717 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1718 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1719 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1720 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1722 dx = pt[fourth].X - origx;
1723 dy = pt[fourth].Y - origy;
1725 diff = sqrt(dx * dx + dy * dy);
1726 percent += 0.0005 * amt;
1730 /* Draws a combination of bezier curves and lines between points. */
1731 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1732 GDIPCONST BYTE * types, INT count, BOOL caps)
1734 POINT *pti = GdipAlloc(count * sizeof(POINT));
1735 BYTE *tp = GdipAlloc(count);
1736 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1737 INT i, j;
1738 GpStatus status = GenericError;
1740 if(!count){
1741 status = Ok;
1742 goto end;
1744 if(!pti || !tp || !ptcopy){
1745 status = OutOfMemory;
1746 goto end;
1749 for(i = 1; i < count; i++){
1750 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1751 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1752 || !(types[i + 1] & PathPointTypeBezier)){
1753 ERR("Bad bezier points\n");
1754 goto end;
1756 i += 2;
1760 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1762 /* If we are drawing caps, go through the points and adjust them accordingly,
1763 * and draw the caps. */
1764 if(caps){
1765 switch(types[count - 1] & PathPointTypePathTypeMask){
1766 case PathPointTypeBezier:
1767 if(pen->endcap == LineCapArrowAnchor)
1768 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1769 else if((pen->endcap == LineCapCustom) && pen->customend)
1770 shorten_bezier_amt(&ptcopy[count - 4],
1771 pen->width * pen->customend->inset, FALSE);
1773 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1774 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1775 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1776 pt[count - 1].X, pt[count - 1].Y);
1778 break;
1779 case PathPointTypeLine:
1780 if(pen->endcap == LineCapArrowAnchor)
1781 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1782 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1783 pen->width);
1784 else if((pen->endcap == LineCapCustom) && pen->customend)
1785 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1786 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1787 pen->customend->inset * pen->width);
1789 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1790 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1791 pt[count - 1].Y);
1793 break;
1794 default:
1795 ERR("Bad path last point\n");
1796 goto end;
1799 /* Find start of points */
1800 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1801 == PathPointTypeStart); j++);
1803 switch(types[j] & PathPointTypePathTypeMask){
1804 case PathPointTypeBezier:
1805 if(pen->startcap == LineCapArrowAnchor)
1806 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1807 else if((pen->startcap == LineCapCustom) && pen->customstart)
1808 shorten_bezier_amt(&ptcopy[j - 1],
1809 pen->width * pen->customstart->inset, TRUE);
1811 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1812 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1813 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1814 pt[j - 1].X, pt[j - 1].Y);
1816 break;
1817 case PathPointTypeLine:
1818 if(pen->startcap == LineCapArrowAnchor)
1819 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1820 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1821 pen->width);
1822 else if((pen->startcap == LineCapCustom) && pen->customstart)
1823 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1824 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1825 pen->customstart->inset * pen->width);
1827 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1828 pt[j].X, pt[j].Y, pt[j - 1].X,
1829 pt[j - 1].Y);
1831 break;
1832 default:
1833 ERR("Bad path points\n");
1834 goto end;
1838 transform_and_round_points(graphics, pti, ptcopy, count);
1840 for(i = 0; i < count; i++){
1841 tp[i] = convert_path_point_type(types[i]);
1844 PolyDraw(graphics->hdc, pti, tp, count);
1846 status = Ok;
1848 end:
1849 GdipFree(pti);
1850 GdipFree(ptcopy);
1851 GdipFree(tp);
1853 return status;
1856 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1858 GpStatus result;
1860 BeginPath(graphics->hdc);
1861 result = draw_poly(graphics, NULL, path->pathdata.Points,
1862 path->pathdata.Types, path->pathdata.Count, FALSE);
1863 EndPath(graphics->hdc);
1864 return result;
1867 typedef struct _GraphicsContainerItem {
1868 struct list entry;
1869 GraphicsContainer contid;
1871 SmoothingMode smoothing;
1872 CompositingQuality compqual;
1873 InterpolationMode interpolation;
1874 CompositingMode compmode;
1875 TextRenderingHint texthint;
1876 REAL scale;
1877 GpUnit unit;
1878 PixelOffsetMode pixeloffset;
1879 UINT textcontrast;
1880 GpMatrix worldtrans;
1881 GpRegion* clip;
1882 INT origin_x, origin_y;
1883 } GraphicsContainerItem;
1885 static GpStatus init_container(GraphicsContainerItem** container,
1886 GDIPCONST GpGraphics* graphics){
1887 GpStatus sts;
1889 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1890 if(!(*container))
1891 return OutOfMemory;
1893 (*container)->contid = graphics->contid + 1;
1895 (*container)->smoothing = graphics->smoothing;
1896 (*container)->compqual = graphics->compqual;
1897 (*container)->interpolation = graphics->interpolation;
1898 (*container)->compmode = graphics->compmode;
1899 (*container)->texthint = graphics->texthint;
1900 (*container)->scale = graphics->scale;
1901 (*container)->unit = graphics->unit;
1902 (*container)->textcontrast = graphics->textcontrast;
1903 (*container)->pixeloffset = graphics->pixeloffset;
1904 (*container)->origin_x = graphics->origin_x;
1905 (*container)->origin_y = graphics->origin_y;
1906 (*container)->worldtrans = graphics->worldtrans;
1908 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1909 if(sts != Ok){
1910 GdipFree(*container);
1911 *container = NULL;
1912 return sts;
1915 return Ok;
1918 static void delete_container(GraphicsContainerItem* container)
1920 GdipDeleteRegion(container->clip);
1921 GdipFree(container);
1924 static GpStatus restore_container(GpGraphics* graphics,
1925 GDIPCONST GraphicsContainerItem* container){
1926 GpStatus sts;
1927 GpRegion *newClip;
1929 sts = GdipCloneRegion(container->clip, &newClip);
1930 if(sts != Ok) return sts;
1932 graphics->worldtrans = container->worldtrans;
1934 GdipDeleteRegion(graphics->clip);
1935 graphics->clip = newClip;
1937 graphics->contid = container->contid - 1;
1939 graphics->smoothing = container->smoothing;
1940 graphics->compqual = container->compqual;
1941 graphics->interpolation = container->interpolation;
1942 graphics->compmode = container->compmode;
1943 graphics->texthint = container->texthint;
1944 graphics->scale = container->scale;
1945 graphics->unit = container->unit;
1946 graphics->textcontrast = container->textcontrast;
1947 graphics->pixeloffset = container->pixeloffset;
1948 graphics->origin_x = container->origin_x;
1949 graphics->origin_y = container->origin_y;
1951 return Ok;
1954 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1956 RECT wnd_rect;
1957 GpStatus stat=Ok;
1958 GpUnit unit;
1960 if(graphics->hwnd) {
1961 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1962 return GenericError;
1964 rect->X = wnd_rect.left;
1965 rect->Y = wnd_rect.top;
1966 rect->Width = wnd_rect.right - wnd_rect.left;
1967 rect->Height = wnd_rect.bottom - wnd_rect.top;
1968 }else if (graphics->image){
1969 stat = GdipGetImageBounds(graphics->image, rect, &unit);
1970 if (stat == Ok && unit != UnitPixel)
1971 FIXME("need to convert from unit %i\n", unit);
1972 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
1973 HBITMAP hbmp;
1974 BITMAP bmp;
1976 rect->X = 0;
1977 rect->Y = 0;
1979 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
1980 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
1982 rect->Width = bmp.bmWidth;
1983 rect->Height = bmp.bmHeight;
1985 else
1987 /* FIXME: ??? */
1988 rect->Width = 1;
1989 rect->Height = 1;
1991 }else{
1992 rect->X = 0;
1993 rect->Y = 0;
1994 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1995 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1998 return stat;
2001 /* on success, rgn will contain the region of the graphics object which
2002 * is visible after clipping has been applied */
2003 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2005 GpStatus stat;
2006 GpRectF rectf;
2007 GpRegion* tmp;
2009 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2010 return stat;
2012 if((stat = GdipCreateRegion(&tmp)) != Ok)
2013 return stat;
2015 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2016 goto end;
2018 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2019 goto end;
2021 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2023 end:
2024 GdipDeleteRegion(tmp);
2025 return stat;
2028 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2030 REAL height;
2032 if (font->unit == UnitPixel)
2034 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2036 else
2038 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2039 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2040 else
2041 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2044 lf->lfHeight = -(height + 0.5);
2045 lf->lfWidth = 0;
2046 lf->lfEscapement = 0;
2047 lf->lfOrientation = 0;
2048 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2049 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2050 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2051 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2052 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2053 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2054 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2055 lf->lfQuality = DEFAULT_QUALITY;
2056 lf->lfPitchAndFamily = 0;
2057 strcpyW(lf->lfFaceName, font->family->FamilyName);
2060 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2061 GDIPCONST GpStringFormat *format, HFONT *hfont,
2062 GDIPCONST GpMatrix *matrix)
2064 HDC hdc = CreateCompatibleDC(0);
2065 GpPointF pt[3];
2066 REAL angle, rel_width, rel_height, font_height;
2067 LOGFONTW lfw;
2068 HFONT unscaled_font;
2069 TEXTMETRICW textmet;
2071 if (font->unit == UnitPixel)
2072 font_height = font->emSize;
2073 else
2075 REAL unit_scale, res;
2077 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2078 unit_scale = units_scale(font->unit, graphics->unit, res);
2080 font_height = font->emSize * unit_scale;
2083 pt[0].X = 0.0;
2084 pt[0].Y = 0.0;
2085 pt[1].X = 1.0;
2086 pt[1].Y = 0.0;
2087 pt[2].X = 0.0;
2088 pt[2].Y = 1.0;
2089 if (matrix)
2091 GpMatrix xform = *matrix;
2092 GdipTransformMatrixPoints(&xform, pt, 3);
2094 if (graphics)
2095 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2096 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2097 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2098 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2099 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2100 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2102 get_log_fontW(font, graphics, &lfw);
2103 lfw.lfHeight = -gdip_round(font_height * rel_height);
2104 unscaled_font = CreateFontIndirectW(&lfw);
2106 SelectObject(hdc, unscaled_font);
2107 GetTextMetricsW(hdc, &textmet);
2109 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2110 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2112 *hfont = CreateFontIndirectW(&lfw);
2114 DeleteDC(hdc);
2115 DeleteObject(unscaled_font);
2118 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2120 TRACE("(%p, %p)\n", hdc, graphics);
2122 return GdipCreateFromHDC2(hdc, NULL, graphics);
2125 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2127 GpStatus retval;
2128 HBITMAP hbitmap;
2129 DIBSECTION dib;
2131 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2133 if(hDevice != NULL)
2134 FIXME("Don't know how to handle parameter hDevice\n");
2136 if(hdc == NULL)
2137 return OutOfMemory;
2139 if(graphics == NULL)
2140 return InvalidParameter;
2142 *graphics = GdipAlloc(sizeof(GpGraphics));
2143 if(!*graphics) return OutOfMemory;
2145 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2147 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2148 GdipFree(*graphics);
2149 return retval;
2152 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2153 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2154 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2156 (*graphics)->alpha_hdc = 1;
2159 (*graphics)->hdc = hdc;
2160 (*graphics)->hwnd = WindowFromDC(hdc);
2161 (*graphics)->owndc = FALSE;
2162 (*graphics)->smoothing = SmoothingModeDefault;
2163 (*graphics)->compqual = CompositingQualityDefault;
2164 (*graphics)->interpolation = InterpolationModeBilinear;
2165 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2166 (*graphics)->compmode = CompositingModeSourceOver;
2167 (*graphics)->unit = UnitDisplay;
2168 (*graphics)->scale = 1.0;
2169 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2170 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2171 (*graphics)->busy = FALSE;
2172 (*graphics)->textcontrast = 4;
2173 list_init(&(*graphics)->containers);
2174 (*graphics)->contid = 0;
2176 TRACE("<-- %p\n", *graphics);
2178 return Ok;
2181 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2183 GpStatus retval;
2185 *graphics = GdipAlloc(sizeof(GpGraphics));
2186 if(!*graphics) return OutOfMemory;
2188 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2190 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2191 GdipFree(*graphics);
2192 return retval;
2195 (*graphics)->hdc = NULL;
2196 (*graphics)->hwnd = NULL;
2197 (*graphics)->owndc = FALSE;
2198 (*graphics)->image = image;
2199 /* We have to store the image type here because the image may be freed
2200 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2201 (*graphics)->image_type = image->type;
2202 (*graphics)->smoothing = SmoothingModeDefault;
2203 (*graphics)->compqual = CompositingQualityDefault;
2204 (*graphics)->interpolation = InterpolationModeBilinear;
2205 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2206 (*graphics)->compmode = CompositingModeSourceOver;
2207 (*graphics)->unit = UnitDisplay;
2208 (*graphics)->scale = 1.0;
2209 (*graphics)->xres = image->xres;
2210 (*graphics)->yres = image->yres;
2211 (*graphics)->busy = FALSE;
2212 (*graphics)->textcontrast = 4;
2213 list_init(&(*graphics)->containers);
2214 (*graphics)->contid = 0;
2216 TRACE("<-- %p\n", *graphics);
2218 return Ok;
2221 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2223 GpStatus ret;
2224 HDC hdc;
2226 TRACE("(%p, %p)\n", hwnd, graphics);
2228 hdc = GetDC(hwnd);
2230 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2232 ReleaseDC(hwnd, hdc);
2233 return ret;
2236 (*graphics)->hwnd = hwnd;
2237 (*graphics)->owndc = TRUE;
2239 return Ok;
2242 /* FIXME: no icm handling */
2243 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2245 TRACE("(%p, %p)\n", hwnd, graphics);
2247 return GdipCreateFromHWND(hwnd, graphics);
2250 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2251 GpMetafile **metafile)
2253 ENHMETAHEADER header;
2254 MetafileType metafile_type;
2256 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2258 if(!hemf || !metafile)
2259 return InvalidParameter;
2261 if (GetEnhMetaFileHeader(hemf, sizeof(header), &header) == 0)
2262 return GenericError;
2264 metafile_type = METAFILE_GetEmfType(hemf);
2266 if (metafile_type == MetafileTypeInvalid)
2267 return GenericError;
2269 *metafile = GdipAlloc(sizeof(GpMetafile));
2270 if (!*metafile)
2271 return OutOfMemory;
2273 (*metafile)->image.type = ImageTypeMetafile;
2274 (*metafile)->image.format = ImageFormatEMF;
2275 (*metafile)->image.frame_count = 1;
2276 (*metafile)->image.xres = (REAL)header.szlDevice.cx;
2277 (*metafile)->image.yres = (REAL)header.szlDevice.cy;
2278 (*metafile)->bounds.X = (REAL)header.rclBounds.left;
2279 (*metafile)->bounds.Y = (REAL)header.rclBounds.top;
2280 (*metafile)->bounds.Width = (REAL)(header.rclBounds.right - header.rclBounds.left);
2281 (*metafile)->bounds.Height = (REAL)(header.rclBounds.bottom - header.rclBounds.top);
2282 (*metafile)->unit = UnitPixel;
2283 (*metafile)->metafile_type = metafile_type;
2284 (*metafile)->hemf = hemf;
2285 (*metafile)->preserve_hemf = !delete;
2287 TRACE("<-- %p\n", *metafile);
2289 return Ok;
2292 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2293 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2295 UINT read;
2296 BYTE *copy;
2297 HENHMETAFILE hemf;
2298 GpStatus retval = Ok;
2300 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2302 if(!hwmf || !metafile || !placeable)
2303 return InvalidParameter;
2305 *metafile = NULL;
2306 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2307 if(!read)
2308 return GenericError;
2309 copy = GdipAlloc(read);
2310 GetMetaFileBitsEx(hwmf, read, copy);
2312 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2313 GdipFree(copy);
2315 /* FIXME: We should store and use hwmf instead of converting to hemf */
2316 retval = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
2318 if (retval == Ok)
2320 (*metafile)->image.xres = (REAL)placeable->Inch;
2321 (*metafile)->image.yres = (REAL)placeable->Inch;
2322 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2323 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2324 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2325 placeable->BoundingBox.Left);
2326 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2327 placeable->BoundingBox.Top);
2328 (*metafile)->metafile_type = MetafileTypeWmfPlaceable;
2329 (*metafile)->image.format = ImageFormatWMF;
2331 if (delete) DeleteMetaFile(hwmf);
2333 else
2334 DeleteEnhMetaFile(hemf);
2335 return retval;
2338 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2339 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2341 HMETAFILE hmf = GetMetaFileW(file);
2343 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2345 if(!hmf) return InvalidParameter;
2347 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2350 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2351 GpMetafile **metafile)
2353 FIXME("(%p, %p): stub\n", file, metafile);
2354 return NotImplemented;
2357 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2358 GpMetafile **metafile)
2360 FIXME("(%p, %p): stub\n", stream, metafile);
2361 return NotImplemented;
2364 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2365 UINT access, IStream **stream)
2367 DWORD dwMode;
2368 HRESULT ret;
2370 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2372 if(!stream || !filename)
2373 return InvalidParameter;
2375 if(access & GENERIC_WRITE)
2376 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2377 else if(access & GENERIC_READ)
2378 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2379 else
2380 return InvalidParameter;
2382 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2384 return hresult_to_status(ret);
2387 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2389 GraphicsContainerItem *cont, *next;
2390 GpStatus stat;
2391 TRACE("(%p)\n", graphics);
2393 if(!graphics) return InvalidParameter;
2394 if(graphics->busy) return ObjectBusy;
2396 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2398 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2399 if (stat != Ok)
2400 return stat;
2403 if(graphics->owndc)
2404 ReleaseDC(graphics->hwnd, graphics->hdc);
2406 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2407 list_remove(&cont->entry);
2408 delete_container(cont);
2411 GdipDeleteRegion(graphics->clip);
2412 GdipFree(graphics);
2414 return Ok;
2417 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2418 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2420 GpStatus status;
2421 GpPath *path;
2423 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2424 width, height, startAngle, sweepAngle);
2426 if(!graphics || !pen || width <= 0 || height <= 0)
2427 return InvalidParameter;
2429 if(graphics->busy)
2430 return ObjectBusy;
2432 status = GdipCreatePath(FillModeAlternate, &path);
2433 if (status != Ok) return status;
2435 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2436 if (status == Ok)
2437 status = GdipDrawPath(graphics, pen, path);
2439 GdipDeletePath(path);
2440 return status;
2443 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2444 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2446 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2447 width, height, startAngle, sweepAngle);
2449 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2452 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2453 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2455 GpPointF pt[4];
2457 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2458 x2, y2, x3, y3, x4, y4);
2460 if(!graphics || !pen)
2461 return InvalidParameter;
2463 if(graphics->busy)
2464 return ObjectBusy;
2466 pt[0].X = x1;
2467 pt[0].Y = y1;
2468 pt[1].X = x2;
2469 pt[1].Y = y2;
2470 pt[2].X = x3;
2471 pt[2].Y = y3;
2472 pt[3].X = x4;
2473 pt[3].Y = y4;
2474 return GdipDrawBeziers(graphics, pen, pt, 4);
2477 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2478 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2480 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2481 x2, y2, x3, y3, x4, y4);
2483 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2486 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2487 GDIPCONST GpPointF *points, INT count)
2489 GpStatus status;
2490 GpPath *path;
2492 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2494 if(!graphics || !pen || !points || (count <= 0))
2495 return InvalidParameter;
2497 if(graphics->busy)
2498 return ObjectBusy;
2500 status = GdipCreatePath(FillModeAlternate, &path);
2501 if (status != Ok) return status;
2503 status = GdipAddPathBeziers(path, points, count);
2504 if (status == Ok)
2505 status = GdipDrawPath(graphics, pen, path);
2507 GdipDeletePath(path);
2508 return status;
2511 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2512 GDIPCONST GpPoint *points, INT count)
2514 GpPointF *pts;
2515 GpStatus ret;
2516 INT i;
2518 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2520 if(!graphics || !pen || !points || (count <= 0))
2521 return InvalidParameter;
2523 if(graphics->busy)
2524 return ObjectBusy;
2526 pts = GdipAlloc(sizeof(GpPointF) * count);
2527 if(!pts)
2528 return OutOfMemory;
2530 for(i = 0; i < count; i++){
2531 pts[i].X = (REAL)points[i].X;
2532 pts[i].Y = (REAL)points[i].Y;
2535 ret = GdipDrawBeziers(graphics,pen,pts,count);
2537 GdipFree(pts);
2539 return ret;
2542 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2543 GDIPCONST GpPointF *points, INT count)
2545 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2547 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2550 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2551 GDIPCONST GpPoint *points, INT count)
2553 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2555 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2558 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2559 GDIPCONST GpPointF *points, INT count, REAL tension)
2561 GpPath *path;
2562 GpStatus status;
2564 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2566 if(!graphics || !pen || !points || count <= 0)
2567 return InvalidParameter;
2569 if(graphics->busy)
2570 return ObjectBusy;
2572 status = GdipCreatePath(FillModeAlternate, &path);
2573 if (status != Ok) return status;
2575 status = GdipAddPathClosedCurve2(path, points, count, tension);
2576 if (status == Ok)
2577 status = GdipDrawPath(graphics, pen, path);
2579 GdipDeletePath(path);
2581 return status;
2584 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2585 GDIPCONST GpPoint *points, INT count, REAL tension)
2587 GpPointF *ptf;
2588 GpStatus stat;
2589 INT i;
2591 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2593 if(!points || count <= 0)
2594 return InvalidParameter;
2596 ptf = GdipAlloc(sizeof(GpPointF)*count);
2597 if(!ptf)
2598 return OutOfMemory;
2600 for(i = 0; i < count; i++){
2601 ptf[i].X = (REAL)points[i].X;
2602 ptf[i].Y = (REAL)points[i].Y;
2605 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2607 GdipFree(ptf);
2609 return stat;
2612 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2613 GDIPCONST GpPointF *points, INT count)
2615 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2617 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2620 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2621 GDIPCONST GpPoint *points, INT count)
2623 GpPointF *pointsF;
2624 GpStatus ret;
2625 INT i;
2627 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2629 if(!points)
2630 return InvalidParameter;
2632 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2633 if(!pointsF)
2634 return OutOfMemory;
2636 for(i = 0; i < count; i++){
2637 pointsF[i].X = (REAL)points[i].X;
2638 pointsF[i].Y = (REAL)points[i].Y;
2641 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2642 GdipFree(pointsF);
2644 return ret;
2647 /* Approximates cardinal spline with Bezier curves. */
2648 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2649 GDIPCONST GpPointF *points, INT count, REAL tension)
2651 GpPath *path;
2652 GpStatus status;
2654 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2656 if(!graphics || !pen)
2657 return InvalidParameter;
2659 if(graphics->busy)
2660 return ObjectBusy;
2662 if(count < 2)
2663 return InvalidParameter;
2665 status = GdipCreatePath(FillModeAlternate, &path);
2666 if (status != Ok) return status;
2668 status = GdipAddPathCurve2(path, points, count, tension);
2669 if (status == Ok)
2670 status = GdipDrawPath(graphics, pen, path);
2672 GdipDeletePath(path);
2673 return status;
2676 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2677 GDIPCONST GpPoint *points, INT count, REAL tension)
2679 GpPointF *pointsF;
2680 GpStatus ret;
2681 INT i;
2683 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2685 if(!points)
2686 return InvalidParameter;
2688 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2689 if(!pointsF)
2690 return OutOfMemory;
2692 for(i = 0; i < count; i++){
2693 pointsF[i].X = (REAL)points[i].X;
2694 pointsF[i].Y = (REAL)points[i].Y;
2697 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2698 GdipFree(pointsF);
2700 return ret;
2703 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2704 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2705 REAL tension)
2707 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2709 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2710 return InvalidParameter;
2713 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2716 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2717 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2718 REAL tension)
2720 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2722 if(count < 0){
2723 return OutOfMemory;
2726 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2727 return InvalidParameter;
2730 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2733 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2734 REAL y, REAL width, REAL height)
2736 GpPath *path;
2737 GpStatus status;
2739 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2741 if(!graphics || !pen)
2742 return InvalidParameter;
2744 if(graphics->busy)
2745 return ObjectBusy;
2747 status = GdipCreatePath(FillModeAlternate, &path);
2748 if (status != Ok) return status;
2750 status = GdipAddPathEllipse(path, x, y, width, height);
2751 if (status == Ok)
2752 status = GdipDrawPath(graphics, pen, path);
2754 GdipDeletePath(path);
2755 return status;
2758 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2759 INT y, INT width, INT height)
2761 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2763 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2767 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2769 UINT width, height;
2771 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2773 if(!graphics || !image)
2774 return InvalidParameter;
2776 GdipGetImageWidth(image, &width);
2777 GdipGetImageHeight(image, &height);
2779 return GdipDrawImagePointRect(graphics, image, x, y,
2780 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2783 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2784 INT y)
2786 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2788 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2791 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2792 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2793 GpUnit srcUnit)
2795 GpPointF points[3];
2796 REAL scale_x, scale_y, width, height;
2798 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2800 if (!graphics || !image) return InvalidParameter;
2802 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2803 scale_x *= graphics->xres / image->xres;
2804 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2805 scale_y *= graphics->yres / image->yres;
2806 width = srcwidth * scale_x;
2807 height = srcheight * scale_y;
2809 points[0].X = points[2].X = x;
2810 points[0].Y = points[1].Y = y;
2811 points[1].X = x + width;
2812 points[2].Y = y + height;
2814 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2815 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2818 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2819 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2820 GpUnit srcUnit)
2822 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2825 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2826 GDIPCONST GpPointF *dstpoints, INT count)
2828 UINT width, height;
2830 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2832 if(!image)
2833 return InvalidParameter;
2835 GdipGetImageWidth(image, &width);
2836 GdipGetImageHeight(image, &height);
2838 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2839 width, height, UnitPixel, NULL, NULL, NULL);
2842 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2843 GDIPCONST GpPoint *dstpoints, INT count)
2845 GpPointF ptf[3];
2847 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2849 if (count != 3 || !dstpoints)
2850 return InvalidParameter;
2852 ptf[0].X = (REAL)dstpoints[0].X;
2853 ptf[0].Y = (REAL)dstpoints[0].Y;
2854 ptf[1].X = (REAL)dstpoints[1].X;
2855 ptf[1].Y = (REAL)dstpoints[1].Y;
2856 ptf[2].X = (REAL)dstpoints[2].X;
2857 ptf[2].Y = (REAL)dstpoints[2].Y;
2859 return GdipDrawImagePoints(graphics, image, ptf, count);
2862 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2863 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2865 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2866 return TRUE;
2869 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2870 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2871 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2872 DrawImageAbort callback, VOID * callbackData)
2874 GpPointF ptf[4];
2875 POINT pti[4];
2876 GpStatus stat;
2878 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2879 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2880 callbackData);
2882 if (count > 3)
2883 return NotImplemented;
2885 if(!graphics || !image || !points || count != 3)
2886 return InvalidParameter;
2888 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2889 debugstr_pointf(&points[2]));
2891 memcpy(ptf, points, 3 * sizeof(GpPointF));
2892 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2893 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2894 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
2895 return Ok;
2896 transform_and_round_points(graphics, pti, ptf, 4);
2898 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2899 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2901 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2902 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2903 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2904 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2905 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2907 if (image->picture)
2909 if (!graphics->hdc)
2911 FIXME("graphics object has no HDC\n");
2914 if(IPicture_Render(image->picture, graphics->hdc,
2915 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2916 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
2918 if(callback)
2919 callback(callbackData);
2920 return GenericError;
2923 else if (image->type == ImageTypeBitmap)
2925 GpBitmap* bitmap = (GpBitmap*)image;
2926 int use_software=0;
2928 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2929 graphics->xres, graphics->yres,
2930 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2931 graphics->scale, image->xres, image->yres, bitmap->format,
2932 imageAttributes ? imageAttributes->outside_color : 0);
2934 if (imageAttributes || graphics->alpha_hdc ||
2935 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2936 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2937 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2938 srcx < 0 || srcy < 0 ||
2939 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2940 use_software = 1;
2942 if (use_software)
2944 RECT dst_area;
2945 GpRect src_area;
2946 int i, x, y, src_stride, dst_stride;
2947 GpMatrix dst_to_src;
2948 REAL m11, m12, m21, m22, mdx, mdy;
2949 LPBYTE src_data, dst_data;
2950 BitmapData lockeddata;
2951 InterpolationMode interpolation = graphics->interpolation;
2952 PixelOffsetMode offset_mode = graphics->pixeloffset;
2953 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2954 REAL x_dx, x_dy, y_dx, y_dy;
2955 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2957 if (!imageAttributes)
2958 imageAttributes = &defaultImageAttributes;
2960 dst_area.left = dst_area.right = pti[0].x;
2961 dst_area.top = dst_area.bottom = pti[0].y;
2962 for (i=1; i<4; i++)
2964 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2965 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2966 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2967 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2970 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2972 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2973 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2974 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2975 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2976 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2977 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2979 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2981 stat = GdipInvertMatrix(&dst_to_src);
2982 if (stat != Ok) return stat;
2984 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2985 if (!dst_data) return OutOfMemory;
2987 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2989 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2990 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2992 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
2994 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2995 if (!src_data)
2997 GdipFree(dst_data);
2998 return OutOfMemory;
3000 src_stride = sizeof(ARGB) * src_area.Width;
3002 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3003 lockeddata.Width = src_area.Width;
3004 lockeddata.Height = src_area.Height;
3005 lockeddata.Stride = src_stride;
3006 lockeddata.PixelFormat = PixelFormat32bppARGB;
3007 lockeddata.Scan0 = src_data;
3009 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3010 PixelFormat32bppARGB, &lockeddata);
3012 if (stat == Ok)
3013 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3015 if (stat != Ok)
3017 if (src_data != dst_data)
3018 GdipFree(src_data);
3019 GdipFree(dst_data);
3020 return stat;
3023 apply_image_attributes(imageAttributes, src_data,
3024 src_area.Width, src_area.Height,
3025 src_stride, ColorAdjustTypeBitmap);
3027 /* Transform the bits as needed to the destination. */
3028 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3030 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3031 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3032 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3033 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3035 for (x=dst_area.left; x<dst_area.right; x++)
3037 for (y=dst_area.top; y<dst_area.bottom; y++)
3039 GpPointF src_pointf;
3040 ARGB *dst_color;
3042 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3043 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3045 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3047 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3048 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3049 imageAttributes, interpolation, offset_mode);
3050 else
3051 *dst_color = 0;
3055 GdipFree(src_data);
3057 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3058 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3060 GdipFree(dst_data);
3062 return stat;
3064 else
3066 HDC hdc;
3067 int temp_hdc=0, temp_bitmap=0;
3068 HBITMAP hbitmap, old_hbm=NULL;
3070 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3071 bitmap->format == PixelFormat24bppRGB ||
3072 bitmap->format == PixelFormat32bppRGB ||
3073 bitmap->format == PixelFormat32bppPARGB))
3075 BITMAPINFOHEADER bih;
3076 BYTE *temp_bits;
3077 PixelFormat dst_format;
3079 /* we can't draw a bitmap of this format directly */
3080 hdc = CreateCompatibleDC(0);
3081 temp_hdc = 1;
3082 temp_bitmap = 1;
3084 bih.biSize = sizeof(BITMAPINFOHEADER);
3085 bih.biWidth = bitmap->width;
3086 bih.biHeight = -bitmap->height;
3087 bih.biPlanes = 1;
3088 bih.biBitCount = 32;
3089 bih.biCompression = BI_RGB;
3090 bih.biSizeImage = 0;
3091 bih.biXPelsPerMeter = 0;
3092 bih.biYPelsPerMeter = 0;
3093 bih.biClrUsed = 0;
3094 bih.biClrImportant = 0;
3096 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3097 (void**)&temp_bits, NULL, 0);
3099 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3100 dst_format = PixelFormat32bppPARGB;
3101 else
3102 dst_format = PixelFormat32bppRGB;
3104 convert_pixels(bitmap->width, bitmap->height,
3105 bitmap->width*4, temp_bits, dst_format,
3106 bitmap->stride, bitmap->bits, bitmap->format,
3107 bitmap->image.palette);
3109 else
3111 if (bitmap->hbitmap)
3112 hbitmap = bitmap->hbitmap;
3113 else
3115 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3116 temp_bitmap = 1;
3119 hdc = bitmap->hdc;
3120 temp_hdc = (hdc == 0);
3123 if (temp_hdc)
3125 if (!hdc) hdc = CreateCompatibleDC(0);
3126 old_hbm = SelectObject(hdc, hbitmap);
3129 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3131 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3132 hdc, srcx, srcy, srcwidth, srcheight);
3134 else
3136 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3137 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3140 if (temp_hdc)
3142 SelectObject(hdc, old_hbm);
3143 DeleteDC(hdc);
3146 if (temp_bitmap)
3147 DeleteObject(hbitmap);
3150 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3152 GpRectF rc;
3154 rc.X = srcx;
3155 rc.Y = srcy;
3156 rc.Width = srcwidth;
3157 rc.Height = srcheight;
3159 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3160 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3162 else
3164 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3165 return InvalidParameter;
3168 return Ok;
3171 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3172 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3173 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3174 DrawImageAbort callback, VOID * callbackData)
3176 GpPointF pointsF[3];
3177 INT i;
3179 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3180 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3181 callbackData);
3183 if(!points || count!=3)
3184 return InvalidParameter;
3186 for(i = 0; i < count; i++){
3187 pointsF[i].X = (REAL)points[i].X;
3188 pointsF[i].Y = (REAL)points[i].Y;
3191 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3192 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3193 callback, callbackData);
3196 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3197 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3198 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3199 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3200 VOID * callbackData)
3202 GpPointF points[3];
3204 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3205 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3206 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3208 points[0].X = dstx;
3209 points[0].Y = dsty;
3210 points[1].X = dstx + dstwidth;
3211 points[1].Y = dsty;
3212 points[2].X = dstx;
3213 points[2].Y = dsty + dstheight;
3215 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3216 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3219 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3220 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3221 INT srcwidth, INT srcheight, GpUnit srcUnit,
3222 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3223 VOID * callbackData)
3225 GpPointF points[3];
3227 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3228 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3229 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3231 points[0].X = dstx;
3232 points[0].Y = dsty;
3233 points[1].X = dstx + dstwidth;
3234 points[1].Y = dsty;
3235 points[2].X = dstx;
3236 points[2].Y = dsty + dstheight;
3238 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3239 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3242 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3243 REAL x, REAL y, REAL width, REAL height)
3245 RectF bounds;
3246 GpUnit unit;
3247 GpStatus ret;
3249 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3251 if(!graphics || !image)
3252 return InvalidParameter;
3254 ret = GdipGetImageBounds(image, &bounds, &unit);
3255 if(ret != Ok)
3256 return ret;
3258 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3259 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3260 unit, NULL, NULL, NULL);
3263 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3264 INT x, INT y, INT width, INT height)
3266 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3268 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3271 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3272 REAL y1, REAL x2, REAL y2)
3274 GpPointF pt[2];
3276 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3278 pt[0].X = x1;
3279 pt[0].Y = y1;
3280 pt[1].X = x2;
3281 pt[1].Y = y2;
3282 return GdipDrawLines(graphics, pen, pt, 2);
3285 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3286 INT y1, INT x2, INT y2)
3288 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3290 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3293 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3294 GpPointF *points, INT count)
3296 GpStatus status;
3297 GpPath *path;
3299 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3301 if(!pen || !graphics || (count < 2))
3302 return InvalidParameter;
3304 if(graphics->busy)
3305 return ObjectBusy;
3307 status = GdipCreatePath(FillModeAlternate, &path);
3308 if (status != Ok) return status;
3310 status = GdipAddPathLine2(path, points, count);
3311 if (status == Ok)
3312 status = GdipDrawPath(graphics, pen, path);
3314 GdipDeletePath(path);
3315 return status;
3318 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3319 GpPoint *points, INT count)
3321 GpStatus retval;
3322 GpPointF *ptf;
3323 int i;
3325 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3327 ptf = GdipAlloc(count * sizeof(GpPointF));
3328 if(!ptf) return OutOfMemory;
3330 for(i = 0; i < count; i ++){
3331 ptf[i].X = (REAL) points[i].X;
3332 ptf[i].Y = (REAL) points[i].Y;
3335 retval = GdipDrawLines(graphics, pen, ptf, count);
3337 GdipFree(ptf);
3338 return retval;
3341 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3343 INT save_state;
3344 GpStatus retval;
3345 HRGN hrgn=NULL;
3347 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3349 if(!pen || !graphics)
3350 return InvalidParameter;
3352 if(graphics->busy)
3353 return ObjectBusy;
3355 if (!graphics->hdc)
3357 FIXME("graphics object has no HDC\n");
3358 return Ok;
3361 save_state = prepare_dc(graphics, pen);
3363 retval = get_clip_hrgn(graphics, &hrgn);
3365 if (retval != Ok)
3366 goto end;
3368 if (hrgn)
3369 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3371 retval = draw_poly(graphics, pen, path->pathdata.Points,
3372 path->pathdata.Types, path->pathdata.Count, TRUE);
3374 end:
3375 restore_dc(graphics, save_state);
3376 DeleteObject(hrgn);
3378 return retval;
3381 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3382 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3384 GpStatus status;
3385 GpPath *path;
3387 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3388 width, height, startAngle, sweepAngle);
3390 if(!graphics || !pen)
3391 return InvalidParameter;
3393 if(graphics->busy)
3394 return ObjectBusy;
3396 status = GdipCreatePath(FillModeAlternate, &path);
3397 if (status != Ok) return status;
3399 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3400 if (status == Ok)
3401 status = GdipDrawPath(graphics, pen, path);
3403 GdipDeletePath(path);
3404 return status;
3407 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3408 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3410 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3411 width, height, startAngle, sweepAngle);
3413 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3416 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3417 REAL y, REAL width, REAL height)
3419 GpStatus status;
3420 GpPath *path;
3422 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3424 if(!pen || !graphics)
3425 return InvalidParameter;
3427 if(graphics->busy)
3428 return ObjectBusy;
3430 status = GdipCreatePath(FillModeAlternate, &path);
3431 if (status != Ok) return status;
3433 status = GdipAddPathRectangle(path, x, y, width, height);
3434 if (status == Ok)
3435 status = GdipDrawPath(graphics, pen, path);
3437 GdipDeletePath(path);
3438 return status;
3441 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3442 INT y, INT width, INT height)
3444 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3446 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3449 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3450 GDIPCONST GpRectF* rects, INT count)
3452 GpStatus status;
3453 GpPath *path;
3455 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3457 if(!graphics || !pen || !rects || count < 1)
3458 return InvalidParameter;
3460 if(graphics->busy)
3461 return ObjectBusy;
3463 status = GdipCreatePath(FillModeAlternate, &path);
3464 if (status != Ok) return status;
3466 status = GdipAddPathRectangles(path, rects, count);
3467 if (status == Ok)
3468 status = GdipDrawPath(graphics, pen, path);
3470 GdipDeletePath(path);
3471 return status;
3474 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3475 GDIPCONST GpRect* rects, INT count)
3477 GpRectF *rectsF;
3478 GpStatus ret;
3479 INT i;
3481 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3483 if(!rects || count<=0)
3484 return InvalidParameter;
3486 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3487 if(!rectsF)
3488 return OutOfMemory;
3490 for(i = 0;i < count;i++){
3491 rectsF[i].X = (REAL)rects[i].X;
3492 rectsF[i].Y = (REAL)rects[i].Y;
3493 rectsF[i].Width = (REAL)rects[i].Width;
3494 rectsF[i].Height = (REAL)rects[i].Height;
3497 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3498 GdipFree(rectsF);
3500 return ret;
3503 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3504 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3506 GpPath *path;
3507 GpStatus status;
3509 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3510 count, tension, fill);
3512 if(!graphics || !brush || !points)
3513 return InvalidParameter;
3515 if(graphics->busy)
3516 return ObjectBusy;
3518 if(count == 1) /* Do nothing */
3519 return Ok;
3521 status = GdipCreatePath(fill, &path);
3522 if (status != Ok) return status;
3524 status = GdipAddPathClosedCurve2(path, points, count, tension);
3525 if (status == Ok)
3526 status = GdipFillPath(graphics, brush, path);
3528 GdipDeletePath(path);
3529 return status;
3532 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3533 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3535 GpPointF *ptf;
3536 GpStatus stat;
3537 INT i;
3539 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3540 count, tension, fill);
3542 if(!points || count == 0)
3543 return InvalidParameter;
3545 if(count == 1) /* Do nothing */
3546 return Ok;
3548 ptf = GdipAlloc(sizeof(GpPointF)*count);
3549 if(!ptf)
3550 return OutOfMemory;
3552 for(i = 0;i < count;i++){
3553 ptf[i].X = (REAL)points[i].X;
3554 ptf[i].Y = (REAL)points[i].Y;
3557 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3559 GdipFree(ptf);
3561 return stat;
3564 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3565 GDIPCONST GpPointF *points, INT count)
3567 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3568 return GdipFillClosedCurve2(graphics, brush, points, count,
3569 0.5f, FillModeAlternate);
3572 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3573 GDIPCONST GpPoint *points, INT count)
3575 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3576 return GdipFillClosedCurve2I(graphics, brush, points, count,
3577 0.5f, FillModeAlternate);
3580 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3581 REAL y, REAL width, REAL height)
3583 GpStatus stat;
3584 GpPath *path;
3586 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3588 if(!graphics || !brush)
3589 return InvalidParameter;
3591 if(graphics->busy)
3592 return ObjectBusy;
3594 stat = GdipCreatePath(FillModeAlternate, &path);
3596 if (stat == Ok)
3598 stat = GdipAddPathEllipse(path, x, y, width, height);
3600 if (stat == Ok)
3601 stat = GdipFillPath(graphics, brush, path);
3603 GdipDeletePath(path);
3606 return stat;
3609 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3610 INT y, INT width, INT height)
3612 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3614 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3617 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3619 INT save_state;
3620 GpStatus retval;
3622 if(!graphics->hdc || !brush_can_fill_path(brush))
3623 return NotImplemented;
3625 save_state = SaveDC(graphics->hdc);
3626 EndPath(graphics->hdc);
3627 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3628 : WINDING));
3630 BeginPath(graphics->hdc);
3631 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3632 path->pathdata.Types, path->pathdata.Count, FALSE);
3634 if(retval != Ok)
3635 goto end;
3637 EndPath(graphics->hdc);
3638 brush_fill_path(graphics, brush);
3640 retval = Ok;
3642 end:
3643 RestoreDC(graphics->hdc, save_state);
3645 return retval;
3648 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3650 GpStatus stat;
3651 GpRegion *rgn;
3653 if (!brush_can_fill_pixels(brush))
3654 return NotImplemented;
3656 /* FIXME: This could probably be done more efficiently without regions. */
3658 stat = GdipCreateRegionPath(path, &rgn);
3660 if (stat == Ok)
3662 stat = GdipFillRegion(graphics, brush, rgn);
3664 GdipDeleteRegion(rgn);
3667 return stat;
3670 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3672 GpStatus stat = NotImplemented;
3674 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3676 if(!brush || !graphics || !path)
3677 return InvalidParameter;
3679 if(graphics->busy)
3680 return ObjectBusy;
3682 if (!graphics->image && !graphics->alpha_hdc)
3683 stat = GDI32_GdipFillPath(graphics, brush, path);
3685 if (stat == NotImplemented)
3686 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3688 if (stat == NotImplemented)
3690 FIXME("Not implemented for brushtype %i\n", brush->bt);
3691 stat = Ok;
3694 return stat;
3697 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3698 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3700 GpStatus stat;
3701 GpPath *path;
3703 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3704 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3706 if(!graphics || !brush)
3707 return InvalidParameter;
3709 if(graphics->busy)
3710 return ObjectBusy;
3712 stat = GdipCreatePath(FillModeAlternate, &path);
3714 if (stat == Ok)
3716 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3718 if (stat == Ok)
3719 stat = GdipFillPath(graphics, brush, path);
3721 GdipDeletePath(path);
3724 return stat;
3727 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3728 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3730 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3731 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3733 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3736 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3737 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3739 GpStatus stat;
3740 GpPath *path;
3742 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3744 if(!graphics || !brush || !points || !count)
3745 return InvalidParameter;
3747 if(graphics->busy)
3748 return ObjectBusy;
3750 stat = GdipCreatePath(fillMode, &path);
3752 if (stat == Ok)
3754 stat = GdipAddPathPolygon(path, points, count);
3756 if (stat == Ok)
3757 stat = GdipFillPath(graphics, brush, path);
3759 GdipDeletePath(path);
3762 return stat;
3765 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3766 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3768 GpStatus stat;
3769 GpPath *path;
3771 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3773 if(!graphics || !brush || !points || !count)
3774 return InvalidParameter;
3776 if(graphics->busy)
3777 return ObjectBusy;
3779 stat = GdipCreatePath(fillMode, &path);
3781 if (stat == Ok)
3783 stat = GdipAddPathPolygonI(path, points, count);
3785 if (stat == Ok)
3786 stat = GdipFillPath(graphics, brush, path);
3788 GdipDeletePath(path);
3791 return stat;
3794 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3795 GDIPCONST GpPointF *points, INT count)
3797 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3799 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3802 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3803 GDIPCONST GpPoint *points, INT count)
3805 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3807 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3810 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3811 REAL x, REAL y, REAL width, REAL height)
3813 GpStatus stat;
3814 GpPath *path;
3816 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3818 if(!graphics || !brush)
3819 return InvalidParameter;
3821 if(graphics->busy)
3822 return ObjectBusy;
3824 stat = GdipCreatePath(FillModeAlternate, &path);
3826 if (stat == Ok)
3828 stat = GdipAddPathRectangle(path, x, y, width, height);
3830 if (stat == Ok)
3831 stat = GdipFillPath(graphics, brush, path);
3833 GdipDeletePath(path);
3836 return stat;
3839 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3840 INT x, INT y, INT width, INT height)
3842 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3844 return GdipFillRectangle(graphics, brush, x, y, width, height);
3847 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3848 INT count)
3850 GpStatus status;
3851 GpPath *path;
3853 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3855 if(!rects)
3856 return InvalidParameter;
3858 status = GdipCreatePath(FillModeAlternate, &path);
3859 if (status != Ok) return status;
3861 status = GdipAddPathRectangles(path, rects, count);
3862 if (status == Ok)
3863 status = GdipFillPath(graphics, brush, path);
3865 GdipDeletePath(path);
3866 return status;
3869 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3870 INT count)
3872 GpRectF *rectsF;
3873 GpStatus ret;
3874 INT i;
3876 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3878 if(!rects || count <= 0)
3879 return InvalidParameter;
3881 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3882 if(!rectsF)
3883 return OutOfMemory;
3885 for(i = 0; i < count; i++){
3886 rectsF[i].X = (REAL)rects[i].X;
3887 rectsF[i].Y = (REAL)rects[i].Y;
3888 rectsF[i].X = (REAL)rects[i].Width;
3889 rectsF[i].Height = (REAL)rects[i].Height;
3892 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3893 GdipFree(rectsF);
3895 return ret;
3898 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3899 GpRegion* region)
3901 INT save_state;
3902 GpStatus status;
3903 HRGN hrgn;
3904 RECT rc;
3906 if(!graphics->hdc || !brush_can_fill_path(brush))
3907 return NotImplemented;
3909 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3910 if(status != Ok)
3911 return status;
3913 save_state = SaveDC(graphics->hdc);
3914 EndPath(graphics->hdc);
3916 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3918 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3920 BeginPath(graphics->hdc);
3921 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3922 EndPath(graphics->hdc);
3924 brush_fill_path(graphics, brush);
3927 RestoreDC(graphics->hdc, save_state);
3929 DeleteObject(hrgn);
3931 return Ok;
3934 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3935 GpRegion* region)
3937 GpStatus stat;
3938 GpRegion *temp_region;
3939 GpMatrix world_to_device;
3940 GpRectF graphics_bounds;
3941 DWORD *pixel_data;
3942 HRGN hregion;
3943 RECT bound_rect;
3944 GpRect gp_bound_rect;
3946 if (!brush_can_fill_pixels(brush))
3947 return NotImplemented;
3949 stat = get_graphics_bounds(graphics, &graphics_bounds);
3951 if (stat == Ok)
3952 stat = GdipCloneRegion(region, &temp_region);
3954 if (stat == Ok)
3956 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3957 CoordinateSpaceWorld, &world_to_device);
3959 if (stat == Ok)
3960 stat = GdipTransformRegion(temp_region, &world_to_device);
3962 if (stat == Ok)
3963 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3965 if (stat == Ok)
3966 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
3968 GdipDeleteRegion(temp_region);
3971 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
3973 DeleteObject(hregion);
3974 return Ok;
3977 if (stat == Ok)
3979 gp_bound_rect.X = bound_rect.left;
3980 gp_bound_rect.Y = bound_rect.top;
3981 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
3982 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
3984 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
3985 if (!pixel_data)
3986 stat = OutOfMemory;
3988 if (stat == Ok)
3990 stat = brush_fill_pixels(graphics, brush, pixel_data,
3991 &gp_bound_rect, gp_bound_rect.Width);
3993 if (stat == Ok)
3994 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
3995 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
3996 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
3998 GdipFree(pixel_data);
4001 DeleteObject(hregion);
4004 return stat;
4007 /*****************************************************************************
4008 * GdipFillRegion [GDIPLUS.@]
4010 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4011 GpRegion* region)
4013 GpStatus stat = NotImplemented;
4015 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4017 if (!(graphics && brush && region))
4018 return InvalidParameter;
4020 if(graphics->busy)
4021 return ObjectBusy;
4023 if (!graphics->image && !graphics->alpha_hdc)
4024 stat = GDI32_GdipFillRegion(graphics, brush, region);
4026 if (stat == NotImplemented)
4027 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4029 if (stat == NotImplemented)
4031 FIXME("not implemented for brushtype %i\n", brush->bt);
4032 stat = Ok;
4035 return stat;
4038 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4040 TRACE("(%p,%u)\n", graphics, intention);
4042 if(!graphics)
4043 return InvalidParameter;
4045 if(graphics->busy)
4046 return ObjectBusy;
4048 /* We have no internal operation queue, so there's no need to clear it. */
4050 if (graphics->hdc)
4051 GdiFlush();
4053 return Ok;
4056 /*****************************************************************************
4057 * GdipGetClipBounds [GDIPLUS.@]
4059 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4061 GpStatus status;
4062 GpRegion *clip;
4064 TRACE("(%p, %p)\n", graphics, rect);
4066 if(!graphics)
4067 return InvalidParameter;
4069 if(graphics->busy)
4070 return ObjectBusy;
4072 status = GdipCreateRegion(&clip);
4073 if (status != Ok) return status;
4075 status = GdipGetClip(graphics, clip);
4076 if (status == Ok)
4077 status = GdipGetRegionBounds(clip, graphics, rect);
4079 GdipDeleteRegion(clip);
4080 return status;
4083 /*****************************************************************************
4084 * GdipGetClipBoundsI [GDIPLUS.@]
4086 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4088 TRACE("(%p, %p)\n", graphics, rect);
4090 if(!graphics)
4091 return InvalidParameter;
4093 if(graphics->busy)
4094 return ObjectBusy;
4096 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4099 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4100 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4101 CompositingMode *mode)
4103 TRACE("(%p, %p)\n", graphics, mode);
4105 if(!graphics || !mode)
4106 return InvalidParameter;
4108 if(graphics->busy)
4109 return ObjectBusy;
4111 *mode = graphics->compmode;
4113 return Ok;
4116 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4117 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4118 CompositingQuality *quality)
4120 TRACE("(%p, %p)\n", graphics, quality);
4122 if(!graphics || !quality)
4123 return InvalidParameter;
4125 if(graphics->busy)
4126 return ObjectBusy;
4128 *quality = graphics->compqual;
4130 return Ok;
4133 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4134 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4135 InterpolationMode *mode)
4137 TRACE("(%p, %p)\n", graphics, mode);
4139 if(!graphics || !mode)
4140 return InvalidParameter;
4142 if(graphics->busy)
4143 return ObjectBusy;
4145 *mode = graphics->interpolation;
4147 return Ok;
4150 /* FIXME: Need to handle color depths less than 24bpp */
4151 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4153 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4155 if(!graphics || !argb)
4156 return InvalidParameter;
4158 if(graphics->busy)
4159 return ObjectBusy;
4161 return Ok;
4164 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4166 TRACE("(%p, %p)\n", graphics, scale);
4168 if(!graphics || !scale)
4169 return InvalidParameter;
4171 if(graphics->busy)
4172 return ObjectBusy;
4174 *scale = graphics->scale;
4176 return Ok;
4179 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4181 TRACE("(%p, %p)\n", graphics, unit);
4183 if(!graphics || !unit)
4184 return InvalidParameter;
4186 if(graphics->busy)
4187 return ObjectBusy;
4189 *unit = graphics->unit;
4191 return Ok;
4194 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4195 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4196 *mode)
4198 TRACE("(%p, %p)\n", graphics, mode);
4200 if(!graphics || !mode)
4201 return InvalidParameter;
4203 if(graphics->busy)
4204 return ObjectBusy;
4206 *mode = graphics->pixeloffset;
4208 return Ok;
4211 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4212 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4214 TRACE("(%p, %p)\n", graphics, mode);
4216 if(!graphics || !mode)
4217 return InvalidParameter;
4219 if(graphics->busy)
4220 return ObjectBusy;
4222 *mode = graphics->smoothing;
4224 return Ok;
4227 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4229 TRACE("(%p, %p)\n", graphics, contrast);
4231 if(!graphics || !contrast)
4232 return InvalidParameter;
4234 *contrast = graphics->textcontrast;
4236 return Ok;
4239 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4240 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4241 TextRenderingHint *hint)
4243 TRACE("(%p, %p)\n", graphics, hint);
4245 if(!graphics || !hint)
4246 return InvalidParameter;
4248 if(graphics->busy)
4249 return ObjectBusy;
4251 *hint = graphics->texthint;
4253 return Ok;
4256 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4258 GpRegion *clip_rgn;
4259 GpStatus stat;
4261 TRACE("(%p, %p)\n", graphics, rect);
4263 if(!graphics || !rect)
4264 return InvalidParameter;
4266 if(graphics->busy)
4267 return ObjectBusy;
4269 /* intersect window and graphics clipping regions */
4270 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4271 return stat;
4273 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4274 goto cleanup;
4276 /* get bounds of the region */
4277 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4279 cleanup:
4280 GdipDeleteRegion(clip_rgn);
4282 return stat;
4285 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4287 GpRectF rectf;
4288 GpStatus stat;
4290 TRACE("(%p, %p)\n", graphics, rect);
4292 if(!graphics || !rect)
4293 return InvalidParameter;
4295 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4297 rect->X = gdip_round(rectf.X);
4298 rect->Y = gdip_round(rectf.Y);
4299 rect->Width = gdip_round(rectf.Width);
4300 rect->Height = gdip_round(rectf.Height);
4303 return stat;
4306 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4308 TRACE("(%p, %p)\n", graphics, matrix);
4310 if(!graphics || !matrix)
4311 return InvalidParameter;
4313 if(graphics->busy)
4314 return ObjectBusy;
4316 *matrix = graphics->worldtrans;
4317 return Ok;
4320 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4322 GpSolidFill *brush;
4323 GpStatus stat;
4324 GpRectF wnd_rect;
4326 TRACE("(%p, %x)\n", graphics, color);
4328 if(!graphics)
4329 return InvalidParameter;
4331 if(graphics->busy)
4332 return ObjectBusy;
4334 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4335 return stat;
4337 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4338 GdipDeleteBrush((GpBrush*)brush);
4339 return stat;
4342 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4343 wnd_rect.Width, wnd_rect.Height);
4345 GdipDeleteBrush((GpBrush*)brush);
4347 return Ok;
4350 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4352 TRACE("(%p, %p)\n", graphics, res);
4354 if(!graphics || !res)
4355 return InvalidParameter;
4357 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4360 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4362 GpStatus stat;
4363 GpRegion* rgn;
4364 GpPointF pt;
4366 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4368 if(!graphics || !result)
4369 return InvalidParameter;
4371 if(graphics->busy)
4372 return ObjectBusy;
4374 pt.X = x;
4375 pt.Y = y;
4376 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4377 CoordinateSpaceWorld, &pt, 1)) != Ok)
4378 return stat;
4380 if((stat = GdipCreateRegion(&rgn)) != Ok)
4381 return stat;
4383 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4384 goto cleanup;
4386 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4388 cleanup:
4389 GdipDeleteRegion(rgn);
4390 return stat;
4393 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4395 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4398 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4400 GpStatus stat;
4401 GpRegion* rgn;
4402 GpPointF pts[2];
4404 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4406 if(!graphics || !result)
4407 return InvalidParameter;
4409 if(graphics->busy)
4410 return ObjectBusy;
4412 pts[0].X = x;
4413 pts[0].Y = y;
4414 pts[1].X = x + width;
4415 pts[1].Y = y + height;
4417 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4418 CoordinateSpaceWorld, pts, 2)) != Ok)
4419 return stat;
4421 pts[1].X -= pts[0].X;
4422 pts[1].Y -= pts[0].Y;
4424 if((stat = GdipCreateRegion(&rgn)) != Ok)
4425 return stat;
4427 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4428 goto cleanup;
4430 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4432 cleanup:
4433 GdipDeleteRegion(rgn);
4434 return stat;
4437 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4439 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4442 GpStatus gdip_format_string(HDC hdc,
4443 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4444 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4445 gdip_format_string_callback callback, void *user_data)
4447 WCHAR* stringdup;
4448 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4449 nheight, lineend, lineno = 0;
4450 RectF bounds;
4451 StringAlignment halign;
4452 GpStatus stat = Ok;
4453 SIZE size;
4454 HotkeyPrefix hkprefix;
4455 INT *hotkeyprefix_offsets=NULL;
4456 INT hotkeyprefix_count=0;
4457 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4458 int seen_prefix=0;
4460 if(length == -1) length = lstrlenW(string);
4462 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4463 if(!stringdup) return OutOfMemory;
4465 nwidth = rect->Width;
4466 nheight = rect->Height;
4467 if (ignore_empty_clip)
4469 if (!nwidth) nwidth = INT_MAX;
4470 if (!nheight) nheight = INT_MAX;
4473 if (format)
4474 hkprefix = format->hkprefix;
4475 else
4476 hkprefix = HotkeyPrefixNone;
4478 if (hkprefix == HotkeyPrefixShow)
4480 for (i=0; i<length; i++)
4482 if (string[i] == '&')
4483 hotkeyprefix_count++;
4487 if (hotkeyprefix_count)
4488 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4490 hotkeyprefix_count = 0;
4492 for(i = 0, j = 0; i < length; i++){
4493 /* FIXME: This makes the indexes passed to callback inaccurate. */
4494 if(!isprintW(string[i]) && (string[i] != '\n'))
4495 continue;
4497 /* FIXME: tabs should be handled using tabstops from stringformat */
4498 if (string[i] == '\t')
4499 continue;
4501 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4502 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4503 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4505 seen_prefix = 1;
4506 continue;
4509 seen_prefix = 0;
4511 stringdup[j] = string[i];
4512 j++;
4515 length = j;
4517 if (format) halign = format->align;
4518 else halign = StringAlignmentNear;
4520 while(sum < length){
4521 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4522 nwidth, &fit, NULL, &size);
4523 fitcpy = fit;
4525 if(fit == 0)
4526 break;
4528 for(lret = 0; lret < fit; lret++)
4529 if(*(stringdup + sum + lret) == '\n')
4530 break;
4532 /* Line break code (may look strange, but it imitates windows). */
4533 if(lret < fit)
4534 lineend = fit = lret; /* this is not an off-by-one error */
4535 else if(fit < (length - sum)){
4536 if(*(stringdup + sum + fit) == ' ')
4537 while(*(stringdup + sum + fit) == ' ')
4538 fit++;
4539 else
4540 while(*(stringdup + sum + fit - 1) != ' '){
4541 fit--;
4543 if(*(stringdup + sum + fit) == '\t')
4544 break;
4546 if(fit == 0){
4547 fit = fitcpy;
4548 break;
4551 lineend = fit;
4552 while(*(stringdup + sum + lineend - 1) == ' ' ||
4553 *(stringdup + sum + lineend - 1) == '\t')
4554 lineend--;
4556 else
4557 lineend = fit;
4559 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4560 nwidth, &j, NULL, &size);
4562 bounds.Width = size.cx;
4564 if(height + size.cy > nheight)
4565 bounds.Height = nheight - (height + size.cy);
4566 else
4567 bounds.Height = size.cy;
4569 bounds.Y = rect->Y + height;
4571 switch (halign)
4573 case StringAlignmentNear:
4574 default:
4575 bounds.X = rect->X;
4576 break;
4577 case StringAlignmentCenter:
4578 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4579 break;
4580 case StringAlignmentFar:
4581 bounds.X = rect->X + rect->Width - bounds.Width;
4582 break;
4585 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4586 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4587 break;
4589 stat = callback(hdc, stringdup, sum, lineend,
4590 font, rect, format, lineno, &bounds,
4591 &hotkeyprefix_offsets[hotkeyprefix_pos],
4592 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4594 if (stat != Ok)
4595 break;
4597 sum += fit + (lret < fitcpy ? 1 : 0);
4598 height += size.cy;
4599 lineno++;
4601 hotkeyprefix_pos = hotkeyprefix_end_pos;
4603 if(height > nheight)
4604 break;
4606 /* Stop if this was a linewrap (but not if it was a linebreak). */
4607 if ((lret == fitcpy) && format &&
4608 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4609 break;
4612 GdipFree(stringdup);
4613 GdipFree(hotkeyprefix_offsets);
4615 return stat;
4618 struct measure_ranges_args {
4619 GpRegion **regions;
4620 REAL rel_width, rel_height;
4623 static GpStatus measure_ranges_callback(HDC hdc,
4624 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4625 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4626 INT lineno, const RectF *bounds, INT *underlined_indexes,
4627 INT underlined_index_count, void *user_data)
4629 int i;
4630 GpStatus stat = Ok;
4631 struct measure_ranges_args *args = user_data;
4633 for (i=0; i<format->range_count; i++)
4635 INT range_start = max(index, format->character_ranges[i].First);
4636 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4637 if (range_start < range_end)
4639 GpRectF range_rect;
4640 SIZE range_size;
4642 range_rect.Y = bounds->Y / args->rel_height;
4643 range_rect.Height = bounds->Height / args->rel_height;
4645 GetTextExtentExPointW(hdc, string + index, range_start - index,
4646 INT_MAX, NULL, NULL, &range_size);
4647 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4649 GetTextExtentExPointW(hdc, string + index, range_end - index,
4650 INT_MAX, NULL, NULL, &range_size);
4651 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4653 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4654 if (stat != Ok)
4655 break;
4659 return stat;
4662 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4663 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4664 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4665 INT regionCount, GpRegion** regions)
4667 GpStatus stat;
4668 int i;
4669 HFONT gdifont, oldfont;
4670 struct measure_ranges_args args;
4671 HDC hdc, temp_hdc=NULL;
4672 GpPointF pt[3];
4673 RectF scaled_rect;
4674 REAL margin_x;
4676 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4677 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4679 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4680 return InvalidParameter;
4682 if (regionCount < stringFormat->range_count)
4683 return InvalidParameter;
4685 if(!graphics->hdc)
4687 hdc = temp_hdc = CreateCompatibleDC(0);
4688 if (!temp_hdc) return OutOfMemory;
4690 else
4691 hdc = graphics->hdc;
4693 if (stringFormat->attr)
4694 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4696 pt[0].X = 0.0;
4697 pt[0].Y = 0.0;
4698 pt[1].X = 1.0;
4699 pt[1].Y = 0.0;
4700 pt[2].X = 0.0;
4701 pt[2].Y = 1.0;
4702 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4703 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4704 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4705 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4706 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4708 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4709 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4711 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4712 scaled_rect.Y = layoutRect->Y * args.rel_height;
4713 scaled_rect.Width = layoutRect->Width * args.rel_width;
4714 scaled_rect.Height = layoutRect->Height * args.rel_height;
4716 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4717 oldfont = SelectObject(hdc, gdifont);
4719 for (i=0; i<stringFormat->range_count; i++)
4721 stat = GdipSetEmpty(regions[i]);
4722 if (stat != Ok)
4723 return stat;
4726 args.regions = regions;
4728 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4729 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4731 SelectObject(hdc, oldfont);
4732 DeleteObject(gdifont);
4734 if (temp_hdc)
4735 DeleteDC(temp_hdc);
4737 return stat;
4740 struct measure_string_args {
4741 RectF *bounds;
4742 INT *codepointsfitted;
4743 INT *linesfilled;
4744 REAL rel_width, rel_height;
4747 static GpStatus measure_string_callback(HDC hdc,
4748 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4749 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4750 INT lineno, const RectF *bounds, INT *underlined_indexes,
4751 INT underlined_index_count, void *user_data)
4753 struct measure_string_args *args = user_data;
4754 REAL new_width, new_height;
4756 new_width = bounds->Width / args->rel_width;
4757 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4759 if (new_width > args->bounds->Width)
4760 args->bounds->Width = new_width;
4762 if (new_height > args->bounds->Height)
4763 args->bounds->Height = new_height;
4765 if (args->codepointsfitted)
4766 *args->codepointsfitted = index + length;
4768 if (args->linesfilled)
4769 (*args->linesfilled)++;
4771 return Ok;
4774 /* Find the smallest rectangle that bounds the text when it is printed in rect
4775 * according to the format options listed in format. If rect has 0 width and
4776 * height, then just find the smallest rectangle that bounds the text when it's
4777 * printed at location (rect->X, rect-Y). */
4778 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4779 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4780 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4781 INT *codepointsfitted, INT *linesfilled)
4783 HFONT oldfont, gdifont;
4784 struct measure_string_args args;
4785 HDC temp_hdc=NULL, hdc;
4786 GpPointF pt[3];
4787 RectF scaled_rect;
4788 REAL margin_x;
4789 INT lines, glyphs;
4791 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4792 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4793 bounds, codepointsfitted, linesfilled);
4795 if(!graphics || !string || !font || !rect || !bounds)
4796 return InvalidParameter;
4798 if(!graphics->hdc)
4800 hdc = temp_hdc = CreateCompatibleDC(0);
4801 if (!temp_hdc) return OutOfMemory;
4803 else
4804 hdc = graphics->hdc;
4806 if(linesfilled) *linesfilled = 0;
4807 if(codepointsfitted) *codepointsfitted = 0;
4809 if(format)
4810 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4812 pt[0].X = 0.0;
4813 pt[0].Y = 0.0;
4814 pt[1].X = 1.0;
4815 pt[1].Y = 0.0;
4816 pt[2].X = 0.0;
4817 pt[2].Y = 1.0;
4818 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4819 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4820 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4821 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4822 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4824 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4825 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4827 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4828 scaled_rect.Y = rect->Y * args.rel_height;
4829 scaled_rect.Width = rect->Width * args.rel_width;
4830 scaled_rect.Height = rect->Height * args.rel_height;
4831 if (scaled_rect.Width >= 0.5)
4833 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4834 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4837 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4838 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4840 get_font_hfont(graphics, font, format, &gdifont, NULL);
4841 oldfont = SelectObject(hdc, gdifont);
4843 bounds->X = rect->X;
4844 bounds->Y = rect->Y;
4845 bounds->Width = 0.0;
4846 bounds->Height = 0.0;
4848 args.bounds = bounds;
4849 args.codepointsfitted = &glyphs;
4850 args.linesfilled = &lines;
4851 lines = glyphs = 0;
4853 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4854 measure_string_callback, &args);
4856 if (linesfilled) *linesfilled = lines;
4857 if (codepointsfitted) *codepointsfitted = glyphs;
4859 if (lines)
4860 bounds->Width += margin_x * 2.0;
4862 SelectObject(hdc, oldfont);
4863 DeleteObject(gdifont);
4865 if (temp_hdc)
4866 DeleteDC(temp_hdc);
4868 return Ok;
4871 struct draw_string_args {
4872 GpGraphics *graphics;
4873 GDIPCONST GpBrush *brush;
4874 REAL x, y, rel_width, rel_height, ascent;
4877 static GpStatus draw_string_callback(HDC hdc,
4878 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4879 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4880 INT lineno, const RectF *bounds, INT *underlined_indexes,
4881 INT underlined_index_count, void *user_data)
4883 struct draw_string_args *args = user_data;
4884 PointF position;
4885 GpStatus stat;
4887 position.X = args->x + bounds->X / args->rel_width;
4888 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4890 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4891 args->brush, &position,
4892 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4894 if (stat == Ok && underlined_index_count)
4896 OUTLINETEXTMETRICW otm;
4897 REAL underline_y, underline_height;
4898 int i;
4900 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4902 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4903 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4905 for (i=0; i<underlined_index_count; i++)
4907 REAL start_x, end_x;
4908 SIZE text_size;
4909 INT ofs = underlined_indexes[i] - index;
4911 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4912 start_x = text_size.cx / args->rel_width;
4914 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4915 end_x = text_size.cx / args->rel_width;
4917 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4921 return stat;
4924 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4925 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4926 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4928 HRGN rgn = NULL;
4929 HFONT gdifont;
4930 GpPointF pt[3], rectcpy[4];
4931 POINT corners[4];
4932 REAL rel_width, rel_height, margin_x;
4933 INT save_state, format_flags = 0;
4934 REAL offsety = 0.0;
4935 struct draw_string_args args;
4936 RectF scaled_rect;
4937 HDC hdc, temp_hdc=NULL;
4938 TEXTMETRICW textmetric;
4940 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4941 length, font, debugstr_rectf(rect), format, brush);
4943 if(!graphics || !string || !font || !brush || !rect)
4944 return InvalidParameter;
4946 if(graphics->hdc)
4948 hdc = graphics->hdc;
4950 else
4952 hdc = temp_hdc = CreateCompatibleDC(0);
4955 if(format){
4956 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4958 format_flags = format->attr;
4960 /* Should be no need to explicitly test for StringAlignmentNear as
4961 * that is default behavior if no alignment is passed. */
4962 if(format->vertalign != StringAlignmentNear){
4963 RectF bounds, in_rect = *rect;
4964 in_rect.Height = 0.0; /* avoid height clipping */
4965 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
4967 TRACE("bounds %s\n", debugstr_rectf(&bounds));
4969 if(format->vertalign == StringAlignmentCenter)
4970 offsety = (rect->Height - bounds.Height) / 2;
4971 else if(format->vertalign == StringAlignmentFar)
4972 offsety = (rect->Height - bounds.Height);
4974 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
4977 save_state = SaveDC(hdc);
4979 pt[0].X = 0.0;
4980 pt[0].Y = 0.0;
4981 pt[1].X = 1.0;
4982 pt[1].Y = 0.0;
4983 pt[2].X = 0.0;
4984 pt[2].Y = 1.0;
4985 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4986 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4987 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4988 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4989 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4991 rectcpy[3].X = rectcpy[0].X = rect->X;
4992 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
4993 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4994 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
4995 transform_and_round_points(graphics, corners, rectcpy, 4);
4997 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4998 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5000 scaled_rect.X = margin_x * rel_width;
5001 scaled_rect.Y = 0.0;
5002 scaled_rect.Width = rel_width * rect->Width;
5003 scaled_rect.Height = rel_height * rect->Height;
5004 if (scaled_rect.Width >= 0.5)
5006 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5007 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5010 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5011 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5013 if (!(format_flags & StringFormatFlagsNoClip) &&
5014 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5015 rect->Width > 0.0 && rect->Height > 0.0)
5017 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5018 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5019 SelectClipRgn(hdc, rgn);
5022 get_font_hfont(graphics, font, format, &gdifont, NULL);
5023 SelectObject(hdc, gdifont);
5025 args.graphics = graphics;
5026 args.brush = brush;
5028 args.x = rect->X;
5029 args.y = rect->Y + offsety;
5031 args.rel_width = rel_width;
5032 args.rel_height = rel_height;
5034 GetTextMetricsW(hdc, &textmetric);
5035 args.ascent = textmetric.tmAscent / rel_height;
5037 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5038 draw_string_callback, &args);
5040 DeleteObject(rgn);
5041 DeleteObject(gdifont);
5043 RestoreDC(hdc, save_state);
5045 DeleteDC(temp_hdc);
5047 return Ok;
5050 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5052 TRACE("(%p)\n", graphics);
5054 if(!graphics)
5055 return InvalidParameter;
5057 if(graphics->busy)
5058 return ObjectBusy;
5060 return GdipSetInfinite(graphics->clip);
5063 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5065 TRACE("(%p)\n", graphics);
5067 if(!graphics)
5068 return InvalidParameter;
5070 if(graphics->busy)
5071 return ObjectBusy;
5073 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5076 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5078 return GdipEndContainer(graphics, state);
5081 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5082 GpMatrixOrder order)
5084 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5086 if(!graphics)
5087 return InvalidParameter;
5089 if(graphics->busy)
5090 return ObjectBusy;
5092 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5095 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5097 return GdipBeginContainer2(graphics, state);
5100 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5101 GraphicsContainer *state)
5103 GraphicsContainerItem *container;
5104 GpStatus sts;
5106 TRACE("(%p, %p)\n", graphics, state);
5108 if(!graphics || !state)
5109 return InvalidParameter;
5111 sts = init_container(&container, graphics);
5112 if(sts != Ok)
5113 return sts;
5115 list_add_head(&graphics->containers, &container->entry);
5116 *state = graphics->contid = container->contid;
5118 return Ok;
5121 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5123 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5124 return NotImplemented;
5127 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5129 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5130 return NotImplemented;
5133 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5135 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5136 return NotImplemented;
5139 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5141 GpStatus sts;
5142 GraphicsContainerItem *container, *container2;
5144 TRACE("(%p, %x)\n", graphics, state);
5146 if(!graphics)
5147 return InvalidParameter;
5149 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5150 if(container->contid == state)
5151 break;
5154 /* did not find a matching container */
5155 if(&container->entry == &graphics->containers)
5156 return Ok;
5158 sts = restore_container(graphics, container);
5159 if(sts != Ok)
5160 return sts;
5162 /* remove all of the containers on top of the found container */
5163 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5164 if(container->contid == state)
5165 break;
5166 list_remove(&container->entry);
5167 delete_container(container);
5170 list_remove(&container->entry);
5171 delete_container(container);
5173 return Ok;
5176 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5177 REAL sy, GpMatrixOrder order)
5179 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5181 if(!graphics)
5182 return InvalidParameter;
5184 if(graphics->busy)
5185 return ObjectBusy;
5187 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5190 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5191 CombineMode mode)
5193 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5195 if(!graphics || !srcgraphics)
5196 return InvalidParameter;
5198 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5201 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5202 CompositingMode mode)
5204 TRACE("(%p, %d)\n", graphics, mode);
5206 if(!graphics)
5207 return InvalidParameter;
5209 if(graphics->busy)
5210 return ObjectBusy;
5212 graphics->compmode = mode;
5214 return Ok;
5217 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5218 CompositingQuality quality)
5220 TRACE("(%p, %d)\n", graphics, quality);
5222 if(!graphics)
5223 return InvalidParameter;
5225 if(graphics->busy)
5226 return ObjectBusy;
5228 graphics->compqual = quality;
5230 return Ok;
5233 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5234 InterpolationMode mode)
5236 TRACE("(%p, %d)\n", graphics, mode);
5238 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5239 return InvalidParameter;
5241 if(graphics->busy)
5242 return ObjectBusy;
5244 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5245 mode = InterpolationModeBilinear;
5247 if (mode == InterpolationModeHighQuality)
5248 mode = InterpolationModeHighQualityBicubic;
5250 graphics->interpolation = mode;
5252 return Ok;
5255 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5257 TRACE("(%p, %.2f)\n", graphics, scale);
5259 if(!graphics || (scale <= 0.0))
5260 return InvalidParameter;
5262 if(graphics->busy)
5263 return ObjectBusy;
5265 graphics->scale = scale;
5267 return Ok;
5270 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5272 TRACE("(%p, %d)\n", graphics, unit);
5274 if(!graphics)
5275 return InvalidParameter;
5277 if(graphics->busy)
5278 return ObjectBusy;
5280 if(unit == UnitWorld)
5281 return InvalidParameter;
5283 graphics->unit = unit;
5285 return Ok;
5288 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5289 mode)
5291 TRACE("(%p, %d)\n", graphics, mode);
5293 if(!graphics)
5294 return InvalidParameter;
5296 if(graphics->busy)
5297 return ObjectBusy;
5299 graphics->pixeloffset = mode;
5301 return Ok;
5304 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5306 static int calls;
5308 TRACE("(%p,%i,%i)\n", graphics, x, y);
5310 if (!(calls++))
5311 FIXME("value is unused in rendering\n");
5313 if (!graphics)
5314 return InvalidParameter;
5316 graphics->origin_x = x;
5317 graphics->origin_y = y;
5319 return Ok;
5322 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5324 TRACE("(%p,%p,%p)\n", graphics, x, y);
5326 if (!graphics || !x || !y)
5327 return InvalidParameter;
5329 *x = graphics->origin_x;
5330 *y = graphics->origin_y;
5332 return Ok;
5335 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5337 TRACE("(%p, %d)\n", graphics, mode);
5339 if(!graphics)
5340 return InvalidParameter;
5342 if(graphics->busy)
5343 return ObjectBusy;
5345 graphics->smoothing = mode;
5347 return Ok;
5350 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5352 TRACE("(%p, %d)\n", graphics, contrast);
5354 if(!graphics)
5355 return InvalidParameter;
5357 graphics->textcontrast = contrast;
5359 return Ok;
5362 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5363 TextRenderingHint hint)
5365 TRACE("(%p, %d)\n", graphics, hint);
5367 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5368 return InvalidParameter;
5370 if(graphics->busy)
5371 return ObjectBusy;
5373 graphics->texthint = hint;
5375 return Ok;
5378 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5380 TRACE("(%p, %p)\n", graphics, matrix);
5382 if(!graphics || !matrix)
5383 return InvalidParameter;
5385 if(graphics->busy)
5386 return ObjectBusy;
5388 TRACE("%f,%f,%f,%f,%f,%f\n",
5389 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5390 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5392 graphics->worldtrans = *matrix;
5394 return Ok;
5397 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5398 REAL dy, GpMatrixOrder order)
5400 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5402 if(!graphics)
5403 return InvalidParameter;
5405 if(graphics->busy)
5406 return ObjectBusy;
5408 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5411 /*****************************************************************************
5412 * GdipSetClipHrgn [GDIPLUS.@]
5414 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5416 GpRegion *region;
5417 GpStatus status;
5419 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5421 if(!graphics)
5422 return InvalidParameter;
5424 if(graphics->busy)
5425 return ObjectBusy;
5427 /* hrgn is already in device units */
5428 status = GdipCreateRegionHrgn(hrgn, &region);
5429 if(status != Ok)
5430 return status;
5432 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5434 GdipDeleteRegion(region);
5435 return status;
5438 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5440 GpStatus status;
5441 GpPath *clip_path;
5443 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5445 if(!graphics)
5446 return InvalidParameter;
5448 if(graphics->busy)
5449 return ObjectBusy;
5451 status = GdipClonePath(path, &clip_path);
5452 if (status == Ok)
5454 GpMatrix world_to_device;
5456 get_graphics_transform(graphics, CoordinateSpaceDevice,
5457 CoordinateSpaceWorld, &world_to_device);
5458 status = GdipTransformPath(clip_path, &world_to_device);
5459 if (status == Ok)
5460 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5462 GdipDeletePath(clip_path);
5464 return status;
5467 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5468 REAL width, REAL height,
5469 CombineMode mode)
5471 GpStatus status;
5472 GpRectF rect;
5473 GpRegion *region;
5475 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5477 if(!graphics)
5478 return InvalidParameter;
5480 if(graphics->busy)
5481 return ObjectBusy;
5483 rect.X = x;
5484 rect.Y = y;
5485 rect.Width = width;
5486 rect.Height = height;
5487 status = GdipCreateRegionRect(&rect, &region);
5488 if (status == Ok)
5490 GpMatrix world_to_device;
5492 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5493 status = GdipTransformRegion(region, &world_to_device);
5494 if (status == Ok)
5495 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5497 GdipDeleteRegion(region);
5499 return status;
5502 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5503 INT width, INT height,
5504 CombineMode mode)
5506 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5508 if(!graphics)
5509 return InvalidParameter;
5511 if(graphics->busy)
5512 return ObjectBusy;
5514 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5517 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5518 CombineMode mode)
5520 GpStatus status;
5521 GpRegion *clip;
5523 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5525 if(!graphics || !region)
5526 return InvalidParameter;
5528 if(graphics->busy)
5529 return ObjectBusy;
5531 status = GdipCloneRegion(region, &clip);
5532 if (status == Ok)
5534 GpMatrix world_to_device;
5536 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5537 status = GdipTransformRegion(clip, &world_to_device);
5538 if (status == Ok)
5539 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5541 GdipDeleteRegion(clip);
5543 return status;
5546 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5547 UINT limitDpi)
5549 static int calls;
5551 TRACE("(%p,%u)\n", metafile, limitDpi);
5553 if(!(calls++))
5554 FIXME("not implemented\n");
5556 return NotImplemented;
5559 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5560 INT count)
5562 INT save_state;
5563 POINT *pti;
5565 TRACE("(%p, %p, %d)\n", graphics, points, count);
5567 if(!graphics || !pen || count<=0)
5568 return InvalidParameter;
5570 if(graphics->busy)
5571 return ObjectBusy;
5573 if (!graphics->hdc)
5575 FIXME("graphics object has no HDC\n");
5576 return Ok;
5579 pti = GdipAlloc(sizeof(POINT) * count);
5581 save_state = prepare_dc(graphics, pen);
5582 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5584 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5585 Polygon(graphics->hdc, pti, count);
5587 restore_dc(graphics, save_state);
5588 GdipFree(pti);
5590 return Ok;
5593 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5594 INT count)
5596 GpStatus ret;
5597 GpPointF *ptf;
5598 INT i;
5600 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5602 if(count<=0) return InvalidParameter;
5603 ptf = GdipAlloc(sizeof(GpPointF) * count);
5605 for(i = 0;i < count; i++){
5606 ptf[i].X = (REAL)points[i].X;
5607 ptf[i].Y = (REAL)points[i].Y;
5610 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5611 GdipFree(ptf);
5613 return ret;
5616 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5618 TRACE("(%p, %p)\n", graphics, dpi);
5620 if(!graphics || !dpi)
5621 return InvalidParameter;
5623 if(graphics->busy)
5624 return ObjectBusy;
5626 *dpi = graphics->xres;
5627 return Ok;
5630 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5632 TRACE("(%p, %p)\n", graphics, dpi);
5634 if(!graphics || !dpi)
5635 return InvalidParameter;
5637 if(graphics->busy)
5638 return ObjectBusy;
5640 *dpi = graphics->yres;
5641 return Ok;
5644 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5645 GpMatrixOrder order)
5647 GpMatrix m;
5648 GpStatus ret;
5650 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5652 if(!graphics || !matrix)
5653 return InvalidParameter;
5655 if(graphics->busy)
5656 return ObjectBusy;
5658 m = graphics->worldtrans;
5660 ret = GdipMultiplyMatrix(&m, matrix, order);
5661 if(ret == Ok)
5662 graphics->worldtrans = m;
5664 return ret;
5667 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5668 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5670 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5672 GpStatus stat=Ok;
5674 TRACE("(%p, %p)\n", graphics, hdc);
5676 if(!graphics || !hdc)
5677 return InvalidParameter;
5679 if(graphics->busy)
5680 return ObjectBusy;
5682 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5684 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5686 else if (!graphics->hdc || graphics->alpha_hdc ||
5687 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5689 /* Create a fake HDC and fill it with a constant color. */
5690 HDC temp_hdc;
5691 HBITMAP hbitmap;
5692 GpRectF bounds;
5693 BITMAPINFOHEADER bmih;
5694 int i;
5696 stat = get_graphics_bounds(graphics, &bounds);
5697 if (stat != Ok)
5698 return stat;
5700 graphics->temp_hbitmap_width = bounds.Width;
5701 graphics->temp_hbitmap_height = bounds.Height;
5703 bmih.biSize = sizeof(bmih);
5704 bmih.biWidth = graphics->temp_hbitmap_width;
5705 bmih.biHeight = -graphics->temp_hbitmap_height;
5706 bmih.biPlanes = 1;
5707 bmih.biBitCount = 32;
5708 bmih.biCompression = BI_RGB;
5709 bmih.biSizeImage = 0;
5710 bmih.biXPelsPerMeter = 0;
5711 bmih.biYPelsPerMeter = 0;
5712 bmih.biClrUsed = 0;
5713 bmih.biClrImportant = 0;
5715 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5716 (void**)&graphics->temp_bits, NULL, 0);
5717 if (!hbitmap)
5718 return GenericError;
5720 temp_hdc = CreateCompatibleDC(0);
5721 if (!temp_hdc)
5723 DeleteObject(hbitmap);
5724 return GenericError;
5727 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5728 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5730 SelectObject(temp_hdc, hbitmap);
5732 graphics->temp_hbitmap = hbitmap;
5733 *hdc = graphics->temp_hdc = temp_hdc;
5735 else
5737 *hdc = graphics->hdc;
5740 if (stat == Ok)
5741 graphics->busy = TRUE;
5743 return stat;
5746 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5748 GpStatus stat=Ok;
5750 TRACE("(%p, %p)\n", graphics, hdc);
5752 if(!graphics || !hdc || !graphics->busy)
5753 return InvalidParameter;
5755 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5757 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5759 else if (graphics->temp_hdc == hdc)
5761 DWORD* pos;
5762 int i;
5764 /* Find the pixels that have changed, and mark them as opaque. */
5765 pos = (DWORD*)graphics->temp_bits;
5766 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5768 if (*pos != DC_BACKGROUND_KEY)
5770 *pos |= 0xff000000;
5772 pos++;
5775 /* Write the changed pixels to the real target. */
5776 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5777 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5778 graphics->temp_hbitmap_width * 4);
5780 /* Clean up. */
5781 DeleteDC(graphics->temp_hdc);
5782 DeleteObject(graphics->temp_hbitmap);
5783 graphics->temp_hdc = NULL;
5784 graphics->temp_hbitmap = NULL;
5786 else if (hdc != graphics->hdc)
5788 stat = InvalidParameter;
5791 if (stat == Ok)
5792 graphics->busy = FALSE;
5794 return stat;
5797 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5799 GpRegion *clip;
5800 GpStatus status;
5801 GpMatrix device_to_world;
5803 TRACE("(%p, %p)\n", graphics, region);
5805 if(!graphics || !region)
5806 return InvalidParameter;
5808 if(graphics->busy)
5809 return ObjectBusy;
5811 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5812 return status;
5814 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5815 status = GdipTransformRegion(clip, &device_to_world);
5816 if (status != Ok)
5818 GdipDeleteRegion(clip);
5819 return status;
5822 /* free everything except root node and header */
5823 delete_element(&region->node);
5824 memcpy(region, clip, sizeof(GpRegion));
5825 GdipFree(clip);
5827 return Ok;
5830 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5831 GpCoordinateSpace src_space, GpMatrix *matrix)
5833 GpStatus stat = Ok;
5834 REAL scale_x, scale_y;
5836 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5838 if (dst_space != src_space)
5840 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5841 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5843 if(graphics->unit != UnitDisplay)
5845 scale_x *= graphics->scale;
5846 scale_y *= graphics->scale;
5849 /* transform from src_space to CoordinateSpacePage */
5850 switch (src_space)
5852 case CoordinateSpaceWorld:
5853 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5854 break;
5855 case CoordinateSpacePage:
5856 break;
5857 case CoordinateSpaceDevice:
5858 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5859 break;
5862 /* transform from CoordinateSpacePage to dst_space */
5863 switch (dst_space)
5865 case CoordinateSpaceWorld:
5867 GpMatrix inverted_transform = graphics->worldtrans;
5868 stat = GdipInvertMatrix(&inverted_transform);
5869 if (stat == Ok)
5870 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5871 break;
5873 case CoordinateSpacePage:
5874 break;
5875 case CoordinateSpaceDevice:
5876 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5877 break;
5880 return stat;
5883 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5884 GpCoordinateSpace src_space, GpPointF *points, INT count)
5886 GpMatrix matrix;
5887 GpStatus stat;
5889 if(!graphics || !points || count <= 0)
5890 return InvalidParameter;
5892 if(graphics->busy)
5893 return ObjectBusy;
5895 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5897 if (src_space == dst_space) return Ok;
5899 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5900 if (stat != Ok) return stat;
5902 return GdipTransformMatrixPoints(&matrix, points, count);
5905 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5906 GpCoordinateSpace src_space, GpPoint *points, INT count)
5908 GpPointF *pointsF;
5909 GpStatus ret;
5910 INT i;
5912 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5914 if(count <= 0)
5915 return InvalidParameter;
5917 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5918 if(!pointsF)
5919 return OutOfMemory;
5921 for(i = 0; i < count; i++){
5922 pointsF[i].X = (REAL)points[i].X;
5923 pointsF[i].Y = (REAL)points[i].Y;
5926 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5928 if(ret == Ok)
5929 for(i = 0; i < count; i++){
5930 points[i].X = gdip_round(pointsF[i].X);
5931 points[i].Y = gdip_round(pointsF[i].Y);
5933 GdipFree(pointsF);
5935 return ret;
5938 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5940 static int calls;
5942 TRACE("\n");
5944 if (!calls++)
5945 FIXME("stub\n");
5947 return NULL;
5950 /*****************************************************************************
5951 * GdipTranslateClip [GDIPLUS.@]
5953 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5955 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5957 if(!graphics)
5958 return InvalidParameter;
5960 if(graphics->busy)
5961 return ObjectBusy;
5963 return GdipTranslateRegion(graphics->clip, dx, dy);
5966 /*****************************************************************************
5967 * GdipTranslateClipI [GDIPLUS.@]
5969 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5971 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5973 if(!graphics)
5974 return InvalidParameter;
5976 if(graphics->busy)
5977 return ObjectBusy;
5979 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5983 /*****************************************************************************
5984 * GdipMeasureDriverString [GDIPLUS.@]
5986 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5987 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5988 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5990 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
5991 HFONT hfont;
5992 HDC hdc;
5993 REAL min_x, min_y, max_x, max_y, x, y;
5994 int i;
5995 TEXTMETRICW textmetric;
5996 const WORD *glyph_indices;
5997 WORD *dynamic_glyph_indices=NULL;
5998 REAL rel_width, rel_height, ascent, descent;
5999 GpPointF pt[3];
6001 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6003 if (!graphics || !text || !font || !positions || !boundingBox)
6004 return InvalidParameter;
6006 if (length == -1)
6007 length = strlenW(text);
6009 if (length == 0)
6011 boundingBox->X = 0.0;
6012 boundingBox->Y = 0.0;
6013 boundingBox->Width = 0.0;
6014 boundingBox->Height = 0.0;
6017 if (flags & unsupported_flags)
6018 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6020 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6022 hdc = CreateCompatibleDC(0);
6023 SelectObject(hdc, hfont);
6025 GetTextMetricsW(hdc, &textmetric);
6027 pt[0].X = 0.0;
6028 pt[0].Y = 0.0;
6029 pt[1].X = 1.0;
6030 pt[1].Y = 0.0;
6031 pt[2].X = 0.0;
6032 pt[2].Y = 1.0;
6033 if (matrix)
6035 GpMatrix xform = *matrix;
6036 GdipTransformMatrixPoints(&xform, pt, 3);
6038 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6039 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6040 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6041 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6042 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6044 if (flags & DriverStringOptionsCmapLookup)
6046 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6047 if (!glyph_indices)
6049 DeleteDC(hdc);
6050 DeleteObject(hfont);
6051 return OutOfMemory;
6054 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6056 else
6057 glyph_indices = text;
6059 min_x = max_x = x = positions[0].X;
6060 min_y = max_y = y = positions[0].Y;
6062 ascent = textmetric.tmAscent / rel_height;
6063 descent = textmetric.tmDescent / rel_height;
6065 for (i=0; i<length; i++)
6067 int char_width;
6068 ABC abc;
6070 if (!(flags & DriverStringOptionsRealizedAdvance))
6072 x = positions[i].X;
6073 y = positions[i].Y;
6076 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6077 char_width = abc.abcA + abc.abcB + abc.abcC;
6079 if (min_y > y - ascent) min_y = y - ascent;
6080 if (max_y < y + descent) max_y = y + descent;
6081 if (min_x > x) min_x = x;
6083 x += char_width / rel_width;
6085 if (max_x < x) max_x = x;
6088 GdipFree(dynamic_glyph_indices);
6089 DeleteDC(hdc);
6090 DeleteObject(hfont);
6092 boundingBox->X = min_x;
6093 boundingBox->Y = min_y;
6094 boundingBox->Width = max_x - min_x;
6095 boundingBox->Height = max_y - min_y;
6097 return Ok;
6100 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6101 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6102 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6103 INT flags, GDIPCONST GpMatrix *matrix)
6105 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6106 INT save_state;
6107 GpPointF pt;
6108 HFONT hfont;
6109 UINT eto_flags=0;
6111 if (flags & unsupported_flags)
6112 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6114 if (!(flags & DriverStringOptionsCmapLookup))
6115 eto_flags |= ETO_GLYPH_INDEX;
6117 save_state = SaveDC(graphics->hdc);
6118 SetBkMode(graphics->hdc, TRANSPARENT);
6119 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6121 pt = positions[0];
6122 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6124 get_font_hfont(graphics, font, format, &hfont, matrix);
6125 SelectObject(graphics->hdc, hfont);
6127 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6129 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6131 RestoreDC(graphics->hdc, save_state);
6133 DeleteObject(hfont);
6135 return Ok;
6138 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6139 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6140 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6141 INT flags, GDIPCONST GpMatrix *matrix)
6143 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6144 GpStatus stat;
6145 PointF *real_positions, real_position;
6146 POINT *pti;
6147 HFONT hfont;
6148 HDC hdc;
6149 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6150 DWORD max_glyphsize=0;
6151 GLYPHMETRICS glyphmetrics;
6152 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6153 BYTE *glyph_mask;
6154 BYTE *text_mask;
6155 int text_mask_stride;
6156 BYTE *pixel_data;
6157 int pixel_data_stride;
6158 GpRect pixel_area;
6159 UINT ggo_flags = GGO_GRAY8_BITMAP;
6161 if (length <= 0)
6162 return Ok;
6164 if (!(flags & DriverStringOptionsCmapLookup))
6165 ggo_flags |= GGO_GLYPH_INDEX;
6167 if (flags & unsupported_flags)
6168 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6170 pti = GdipAlloc(sizeof(POINT) * length);
6171 if (!pti)
6172 return OutOfMemory;
6174 if (flags & DriverStringOptionsRealizedAdvance)
6176 real_position = positions[0];
6178 transform_and_round_points(graphics, pti, &real_position, 1);
6180 else
6182 real_positions = GdipAlloc(sizeof(PointF) * length);
6183 if (!real_positions)
6185 GdipFree(pti);
6186 return OutOfMemory;
6189 memcpy(real_positions, positions, sizeof(PointF) * length);
6191 transform_and_round_points(graphics, pti, real_positions, length);
6193 GdipFree(real_positions);
6196 get_font_hfont(graphics, font, format, &hfont, matrix);
6198 hdc = CreateCompatibleDC(0);
6199 SelectObject(hdc, hfont);
6201 /* Get the boundaries of the text to be drawn */
6202 for (i=0; i<length; i++)
6204 DWORD glyphsize;
6205 int left, top, right, bottom;
6207 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6208 &glyphmetrics, 0, NULL, &identity);
6210 if (glyphsize == GDI_ERROR)
6212 ERR("GetGlyphOutlineW failed\n");
6213 GdipFree(pti);
6214 DeleteDC(hdc);
6215 DeleteObject(hfont);
6216 return GenericError;
6219 if (glyphsize > max_glyphsize)
6220 max_glyphsize = glyphsize;
6222 if (glyphsize != 0)
6224 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6225 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6226 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6227 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6229 if (left < min_x) min_x = left;
6230 if (top < min_y) min_y = top;
6231 if (right > max_x) max_x = right;
6232 if (bottom > max_y) max_y = bottom;
6235 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6237 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6238 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6242 if (max_glyphsize == 0)
6243 /* Nothing to draw. */
6244 return Ok;
6246 glyph_mask = GdipAlloc(max_glyphsize);
6247 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6248 text_mask_stride = max_x - min_x;
6250 if (!(glyph_mask && text_mask))
6252 GdipFree(glyph_mask);
6253 GdipFree(text_mask);
6254 GdipFree(pti);
6255 DeleteDC(hdc);
6256 DeleteObject(hfont);
6257 return OutOfMemory;
6260 /* Generate a mask for the text */
6261 for (i=0; i<length; i++)
6263 DWORD ret;
6264 int left, top, stride;
6266 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6267 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6269 if (ret == GDI_ERROR || ret == 0)
6270 continue; /* empty glyph */
6272 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6273 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6274 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6276 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6278 BYTE *glyph_val = glyph_mask + y * stride;
6279 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6280 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6282 *text_val = min(64, *text_val + *glyph_val);
6283 glyph_val++;
6284 text_val++;
6289 GdipFree(pti);
6290 DeleteDC(hdc);
6291 DeleteObject(hfont);
6292 GdipFree(glyph_mask);
6294 /* get the brush data */
6295 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6296 if (!pixel_data)
6298 GdipFree(text_mask);
6299 return OutOfMemory;
6302 pixel_area.X = min_x;
6303 pixel_area.Y = min_y;
6304 pixel_area.Width = max_x - min_x;
6305 pixel_area.Height = max_y - min_y;
6306 pixel_data_stride = pixel_area.Width * 4;
6308 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6309 if (stat != Ok)
6311 GdipFree(text_mask);
6312 GdipFree(pixel_data);
6313 return stat;
6316 /* multiply the brush data by the mask */
6317 for (y=0; y<pixel_area.Height; y++)
6319 BYTE *text_val = text_mask + text_mask_stride * y;
6320 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6321 for (x=0; x<pixel_area.Width; x++)
6323 *pixel_val = (*pixel_val) * (*text_val) / 64;
6324 text_val++;
6325 pixel_val+=4;
6329 GdipFree(text_mask);
6331 /* draw the result */
6332 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6333 pixel_area.Height, pixel_data_stride);
6335 GdipFree(pixel_data);
6337 return stat;
6340 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6341 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6342 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6343 INT flags, GDIPCONST GpMatrix *matrix)
6345 GpStatus stat = NotImplemented;
6347 if (length == -1)
6348 length = strlenW(text);
6350 if (graphics->hdc && !graphics->alpha_hdc &&
6351 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6352 brush->bt == BrushTypeSolidColor &&
6353 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6354 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6355 brush, positions, flags, matrix);
6356 if (stat == NotImplemented)
6357 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6358 brush, positions, flags, matrix);
6359 return stat;
6362 /*****************************************************************************
6363 * GdipDrawDriverString [GDIPLUS.@]
6365 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6366 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6367 GDIPCONST PointF *positions, INT flags,
6368 GDIPCONST GpMatrix *matrix )
6370 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6372 if (!graphics || !text || !font || !brush || !positions)
6373 return InvalidParameter;
6375 return draw_driver_string(graphics, text, length, font, NULL,
6376 brush, positions, flags, matrix);
6379 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6380 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6382 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6383 return NotImplemented;
6386 /*****************************************************************************
6387 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6389 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6391 GpStatus stat;
6392 GpRegion* rgn;
6394 TRACE("(%p, %p)\n", graphics, res);
6396 if((stat = GdipCreateRegion(&rgn)) != Ok)
6397 return stat;
6399 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6400 goto cleanup;
6402 stat = GdipIsEmptyRegion(rgn, graphics, res);
6404 cleanup:
6405 GdipDeleteRegion(rgn);
6406 return stat;
6409 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6411 static int calls;
6413 TRACE("(%p) stub\n", graphics);
6415 if(!(calls++))
6416 FIXME("not implemented\n");
6418 return NotImplemented;