widl: Always check the runtimeclass interfaces presence.
[wine/zf.git] / dlls / gdiplus / graphics.c
blob7b4794e362038fbf631e9964cfe11e13fe7de87a
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"
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "ocidl.h"
31 #include "olectl.h"
32 #include "ole2.h"
34 #include "winreg.h"
35 #include "shlwapi.h"
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
40 #include "wine/list.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44 /* looks-right constants */
45 #define ANCHOR_WIDTH (2.0)
46 #define MAX_ITERS (50)
48 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
49 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
50 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
51 INT flags, GDIPCONST GpMatrix *matrix);
53 /* Converts from gdiplus path point type to gdi path point type. */
54 static BYTE convert_path_point_type(BYTE type)
56 BYTE ret;
58 switch(type & PathPointTypePathTypeMask){
59 case PathPointTypeBezier:
60 ret = PT_BEZIERTO;
61 break;
62 case PathPointTypeLine:
63 ret = PT_LINETO;
64 break;
65 case PathPointTypeStart:
66 ret = PT_MOVETO;
67 break;
68 default:
69 ERR("Bad point type\n");
70 return 0;
73 if(type & PathPointTypeCloseSubpath)
74 ret |= PT_CLOSEFIGURE;
76 return ret;
79 static COLORREF get_gdi_brush_color(const GpBrush *brush)
81 ARGB argb;
83 switch (brush->bt)
85 case BrushTypeSolidColor:
87 const GpSolidFill *sf = (const GpSolidFill *)brush;
88 argb = sf->color;
89 break;
91 case BrushTypeHatchFill:
93 const GpHatch *hatch = (const GpHatch *)brush;
94 argb = hatch->forecol;
95 break;
97 case BrushTypeLinearGradient:
99 const GpLineGradient *line = (const GpLineGradient *)brush;
100 argb = line->startcolor;
101 break;
103 case BrushTypePathGradient:
105 const GpPathGradient *grad = (const GpPathGradient *)brush;
106 argb = grad->centercolor;
107 break;
109 default:
110 FIXME("unhandled brush type %d\n", brush->bt);
111 argb = 0;
112 break;
114 return ARGB2COLORREF(argb);
117 static ARGB blend_colors(ARGB start, ARGB end, REAL position);
119 static void init_hatch_palette(ARGB *hatch_palette, ARGB fore_color, ARGB back_color)
121 /* Pass the center of a 45-degree diagonal line with width of one unit through the
122 * center of a unit square, and the portion of the square that will be covered will
123 * equal sqrt(2) - 1/2. The covered portion for adjacent squares will be 1/4. */
124 hatch_palette[0] = back_color;
125 hatch_palette[1] = blend_colors(back_color, fore_color, 0.25);
126 hatch_palette[2] = blend_colors(back_color, fore_color, sqrt(2.0) - 0.5);
127 hatch_palette[3] = fore_color;
130 static HBITMAP create_hatch_bitmap(const GpHatch *hatch, INT origin_x, INT origin_y)
132 HBITMAP hbmp;
133 BITMAPINFOHEADER bmih;
134 DWORD *bits;
135 int x, y;
137 bmih.biSize = sizeof(bmih);
138 bmih.biWidth = 8;
139 bmih.biHeight = 8;
140 bmih.biPlanes = 1;
141 bmih.biBitCount = 32;
142 bmih.biCompression = BI_RGB;
143 bmih.biSizeImage = 0;
145 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
146 if (hbmp)
148 const unsigned char *hatch_data;
150 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
152 ARGB hatch_palette[4];
153 init_hatch_palette(hatch_palette, hatch->forecol, hatch->backcol);
155 /* Anti-aliasing is only specified for diagonal hatch patterns.
156 * This implementation repeats the pattern, shifts as needed,
157 * then uses bitmask 1 to check the pixel value, and the 0x82
158 * bitmask to check the adjacent pixel values, to determine the
159 * degree of shading needed. */
160 for (y = 0; y < 8; y++)
162 const int hy = (y + origin_y) & 7;
163 const int hx = origin_x & 7;
164 unsigned int row = (0x10101 * hatch_data[hy]) >> hx;
166 for (x = 0; x < 8; x++, row >>= 1)
168 int index;
169 if (hatch_data[8])
170 index = (row & 1) ? 2 : (row & 0x82) ? 1 : 0;
171 else
172 index = (row & 1) ? 3 : 0;
173 bits[y * 8 + 7 - x] = hatch_palette[index];
177 else
179 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
181 for (y = 0; y < 64; y++)
182 bits[y] = hatch->forecol;
186 return hbmp;
189 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb, INT origin_x, INT origin_y)
191 switch (brush->bt)
193 case BrushTypeSolidColor:
195 const GpSolidFill *sf = (const GpSolidFill *)brush;
196 lb->lbStyle = BS_SOLID;
197 lb->lbColor = ARGB2COLORREF(sf->color);
198 lb->lbHatch = 0;
199 return Ok;
202 case BrushTypeHatchFill:
204 const GpHatch *hatch = (const GpHatch *)brush;
205 HBITMAP hbmp;
207 hbmp = create_hatch_bitmap(hatch, origin_x, origin_y);
208 if (!hbmp) return OutOfMemory;
210 lb->lbStyle = BS_PATTERN;
211 lb->lbColor = 0;
212 lb->lbHatch = (ULONG_PTR)hbmp;
213 return Ok;
216 default:
217 FIXME("unhandled brush type %d\n", brush->bt);
218 lb->lbStyle = BS_SOLID;
219 lb->lbColor = get_gdi_brush_color(brush);
220 lb->lbHatch = 0;
221 return Ok;
225 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
227 switch (lb->lbStyle)
229 case BS_PATTERN:
230 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
231 break;
233 return Ok;
236 static HBRUSH create_gdi_brush(const GpBrush *brush, INT origin_x, INT origin_y)
238 LOGBRUSH lb;
239 HBRUSH gdibrush;
241 if (create_gdi_logbrush(brush, &lb, origin_x, origin_y) != Ok) return 0;
243 gdibrush = CreateBrushIndirect(&lb);
244 free_gdi_logbrush(&lb);
246 return gdibrush;
249 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
251 LOGBRUSH lb;
252 HPEN gdipen;
253 REAL width;
254 INT save_state, i, numdashes;
255 GpPointF pt[2];
256 DWORD dash_array[MAX_DASHLEN];
258 save_state = SaveDC(graphics->hdc);
260 EndPath(graphics->hdc);
262 if(pen->unit == UnitPixel){
263 width = pen->width;
265 else{
266 /* Get an estimate for the amount the pen width is affected by the world
267 * transform. (This is similar to what some of the wine drivers do.) */
268 pt[0].X = 0.0;
269 pt[0].Y = 0.0;
270 pt[1].X = 1.0;
271 pt[1].Y = 1.0;
272 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
273 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
274 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
276 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit,
277 graphics->xres, graphics->printer_display);
278 width *= graphics->scale;
280 pt[0].X = 0.0;
281 pt[0].Y = 0.0;
282 pt[1].X = 1.0;
283 pt[1].Y = 1.0;
284 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, pt, 2);
285 width *= sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
286 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
289 if(pen->dash == DashStyleCustom){
290 numdashes = min(pen->numdashes, MAX_DASHLEN);
292 TRACE("dashes are: ");
293 for(i = 0; i < numdashes; i++){
294 dash_array[i] = gdip_round(width * pen->dashes[i]);
295 TRACE("%d, ", dash_array[i]);
297 TRACE("\n and the pen style is %x\n", pen->style);
299 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
300 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
301 numdashes, dash_array);
302 free_gdi_logbrush(&lb);
304 else
306 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
307 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
308 free_gdi_logbrush(&lb);
311 SelectObject(graphics->hdc, gdipen);
313 return save_state;
316 static void restore_dc(GpGraphics *graphics, INT state)
318 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
319 RestoreDC(graphics->hdc, state);
322 static void round_points(POINT *pti, GpPointF *ptf, INT count)
324 int i;
326 for(i = 0; i < count; i++){
327 if(isnan(ptf[i].X))
328 pti[i].x = 0;
329 else
330 pti[i].x = gdip_round(ptf[i].X);
332 if(isnan(ptf[i].Y))
333 pti[i].y = 0;
334 else
335 pti[i].y = gdip_round(ptf[i].Y);
339 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
340 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
342 CompositingMode comp_mode;
343 INT technology = GetDeviceCaps(graphics->hdc, TECHNOLOGY);
344 INT shadeblendcaps = GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS);
346 GdipGetCompositingMode(graphics, &comp_mode);
348 if ((technology == DT_RASPRINTER && shadeblendcaps == SB_NONE)
349 || comp_mode == CompositingModeSourceCopy)
351 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
353 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
354 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
356 else
358 BLENDFUNCTION bf;
360 bf.BlendOp = AC_SRC_OVER;
361 bf.BlendFlags = 0;
362 bf.SourceConstantAlpha = 255;
363 bf.AlphaFormat = AC_SRC_ALPHA;
365 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
366 hdc, src_x, src_y, src_width, src_height, bf);
370 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
372 GpRegion *rgn;
373 GpMatrix transform;
374 GpStatus stat;
375 BOOL identity;
377 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, &transform);
379 if (stat == Ok)
380 stat = GdipIsMatrixIdentity(&transform, &identity);
382 if (stat == Ok)
383 stat = GdipCloneRegion(graphics->clip, &rgn);
385 if (stat == Ok)
387 if (!identity)
388 stat = GdipTransformRegion(rgn, &transform);
390 if (stat == Ok)
391 stat = GdipGetRegionHRgn(rgn, NULL, hrgn);
393 GdipDeleteRegion(rgn);
396 if (stat == Ok && graphics->gdi_clip)
398 if (*hrgn)
399 CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
400 else
402 *hrgn = CreateRectRgn(0,0,0,0);
403 CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
407 return stat;
410 /* Draw ARGB data to the given graphics object */
411 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
412 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
414 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
415 INT x, y;
416 CompositingMode comp_mode;
418 GdipGetCompositingMode(graphics, &comp_mode);
420 for (y=0; y<src_height; y++)
422 for (x=0; x<src_width; x++)
424 ARGB dst_color, src_color;
425 src_color = ((ARGB*)(src + src_stride * y))[x];
427 if (comp_mode == CompositingModeSourceCopy)
429 if (!(src_color & 0xff000000))
430 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, 0);
431 else
432 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, src_color);
434 else
436 if (!(src_color & 0xff000000))
437 continue;
439 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
440 if (fmt & PixelFormatPAlpha)
441 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
442 else
443 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
448 return Ok;
451 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
452 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
454 HDC hdc;
455 HBITMAP hbitmap;
456 BITMAPINFOHEADER bih;
457 BYTE *temp_bits;
459 hdc = CreateCompatibleDC(0);
461 bih.biSize = sizeof(BITMAPINFOHEADER);
462 bih.biWidth = src_width;
463 bih.biHeight = -src_height;
464 bih.biPlanes = 1;
465 bih.biBitCount = 32;
466 bih.biCompression = BI_RGB;
467 bih.biSizeImage = 0;
468 bih.biXPelsPerMeter = 0;
469 bih.biYPelsPerMeter = 0;
470 bih.biClrUsed = 0;
471 bih.biClrImportant = 0;
473 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
474 (void**)&temp_bits, NULL, 0);
476 if(!hbitmap || !temp_bits)
477 goto done;
479 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
480 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
481 fmt & PixelFormatPAlpha)
482 memcpy(temp_bits, src, src_width * src_height * 4);
483 else
484 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
485 4 * src_width, src, src_stride);
487 SelectObject(hdc, hbitmap);
488 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
489 hdc, 0, 0, src_width, src_height);
491 DeleteObject(hbitmap);
493 done:
494 DeleteDC(hdc);
496 return Ok;
499 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
500 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
502 GpStatus stat=Ok;
504 if (graphics->image && graphics->image->type == ImageTypeBitmap)
506 DWORD i;
507 int size;
508 RGNDATA *rgndata;
509 RECT *rects;
510 HRGN hrgn, visible_rgn;
512 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
513 if (!hrgn)
514 return OutOfMemory;
516 stat = get_clip_hrgn(graphics, &visible_rgn);
517 if (stat != Ok)
519 DeleteObject(hrgn);
520 return stat;
523 if (visible_rgn)
525 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
526 DeleteObject(visible_rgn);
529 if (hregion)
530 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
532 size = GetRegionData(hrgn, 0, NULL);
534 rgndata = heap_alloc_zero(size);
535 if (!rgndata)
537 DeleteObject(hrgn);
538 return OutOfMemory;
541 GetRegionData(hrgn, size, rgndata);
543 rects = (RECT*)rgndata->Buffer;
545 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
547 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
548 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
549 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
550 src_stride, fmt);
553 heap_free(rgndata);
555 DeleteObject(hrgn);
557 return stat;
559 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
561 ERR("This should not be used for metafiles; fix caller\n");
562 return NotImplemented;
564 else
566 HRGN hrgn;
567 int save;
569 stat = get_clip_hrgn(graphics, &hrgn);
571 if (stat != Ok)
572 return stat;
574 save = SaveDC(graphics->hdc);
576 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
578 if (hregion)
579 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
581 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
582 src_height, src_stride, fmt);
584 RestoreDC(graphics->hdc, save);
586 DeleteObject(hrgn);
588 return stat;
592 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
593 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
595 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
598 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
600 INT start_a, end_a, final_a;
601 INT pos;
603 pos = gdip_round(position * 0xff);
605 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
606 end_a = ((end >> 24) & 0xff) * pos;
608 final_a = start_a + end_a;
610 if (final_a < 0xff) return 0;
612 return (final_a / 0xff) << 24 |
613 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
614 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
615 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
618 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
620 REAL blendfac;
622 /* clamp to between 0.0 and 1.0, using the wrap mode */
623 position = (position - brush->rect.X) / brush->rect.Width;
624 if (brush->wrap == WrapModeTile)
626 position = fmodf(position, 1.0f);
627 if (position < 0.0f) position += 1.0f;
629 else /* WrapModeFlip* */
631 position = fmodf(position, 2.0f);
632 if (position < 0.0f) position += 2.0f;
633 if (position > 1.0f) position = 2.0f - position;
636 if (brush->blendcount == 1)
637 blendfac = position;
638 else
640 int i=1;
641 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
642 REAL range;
644 /* locate the blend positions surrounding this position */
645 while (position > brush->blendpos[i])
646 i++;
648 /* interpolate between the blend positions */
649 left_blendpos = brush->blendpos[i-1];
650 left_blendfac = brush->blendfac[i-1];
651 right_blendpos = brush->blendpos[i];
652 right_blendfac = brush->blendfac[i];
653 range = right_blendpos - left_blendpos;
654 blendfac = (left_blendfac * (right_blendpos - position) +
655 right_blendfac * (position - left_blendpos)) / range;
658 if (brush->pblendcount == 0)
659 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
660 else
662 int i=1;
663 ARGB left_blendcolor, right_blendcolor;
664 REAL left_blendpos, right_blendpos;
666 /* locate the blend colors surrounding this position */
667 while (blendfac > brush->pblendpos[i])
668 i++;
670 /* interpolate between the blend colors */
671 left_blendpos = brush->pblendpos[i-1];
672 left_blendcolor = brush->pblendcolor[i-1];
673 right_blendpos = brush->pblendpos[i];
674 right_blendcolor = brush->pblendcolor[i];
675 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
676 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
680 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
682 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
683 BOOL identity = TRUE;
684 int i, j;
686 for (i=0; i<4; i++)
687 for (j=0; j<5; j++)
689 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
690 identity = FALSE;
691 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
694 return identity;
697 static ARGB transform_color(ARGB color, int matrix[5][5])
699 int val[5], res[4];
700 int i, j;
701 unsigned char a, r, g, b;
703 val[0] = ((color >> 16) & 0xff); /* red */
704 val[1] = ((color >> 8) & 0xff); /* green */
705 val[2] = (color & 0xff); /* blue */
706 val[3] = ((color >> 24) & 0xff); /* alpha */
707 val[4] = 255; /* translation */
709 for (i=0; i<4; i++)
711 res[i] = 0;
713 for (j=0; j<5; j++)
714 res[i] += matrix[j][i] * val[j];
717 a = min(max(res[3] / 256, 0), 255);
718 r = min(max(res[0] / 256, 0), 255);
719 g = min(max(res[1] / 256, 0), 255);
720 b = min(max(res[2] / 256, 0), 255);
722 return (a << 24) | (r << 16) | (g << 8) | b;
725 static BOOL color_is_gray(ARGB color)
727 unsigned char r, g, b;
729 r = (color >> 16) & 0xff;
730 g = (color >> 8) & 0xff;
731 b = color & 0xff;
733 return (r == g) && (g == b);
736 /* returns preferred pixel format for the applied attributes */
737 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
738 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
740 UINT x, y;
741 INT i;
743 if ((attributes->noop[type] == IMAGEATTR_NOOP_UNDEFINED &&
744 attributes->noop[ColorAdjustTypeDefault] == IMAGEATTR_NOOP_SET) ||
745 (attributes->noop[type] == IMAGEATTR_NOOP_SET))
746 return fmt;
748 if (attributes->colorkeys[type].enabled ||
749 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
751 const struct color_key *key;
752 BYTE min_blue, min_green, min_red;
753 BYTE max_blue, max_green, max_red;
755 if (!data || fmt != PixelFormat32bppARGB)
756 return PixelFormat32bppARGB;
758 if (attributes->colorkeys[type].enabled)
759 key = &attributes->colorkeys[type];
760 else
761 key = &attributes->colorkeys[ColorAdjustTypeDefault];
763 min_blue = key->low&0xff;
764 min_green = (key->low>>8)&0xff;
765 min_red = (key->low>>16)&0xff;
767 max_blue = key->high&0xff;
768 max_green = (key->high>>8)&0xff;
769 max_red = (key->high>>16)&0xff;
771 for (x=0; x<width; x++)
772 for (y=0; y<height; y++)
774 ARGB *src_color;
775 BYTE blue, green, red;
776 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
777 blue = *src_color&0xff;
778 green = (*src_color>>8)&0xff;
779 red = (*src_color>>16)&0xff;
780 if (blue >= min_blue && green >= min_green && red >= min_red &&
781 blue <= max_blue && green <= max_green && red <= max_red)
782 *src_color = 0x00000000;
786 if (attributes->colorremaptables[type].enabled ||
787 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
789 const struct color_remap_table *table;
791 if (!data || fmt != PixelFormat32bppARGB)
792 return PixelFormat32bppARGB;
794 if (attributes->colorremaptables[type].enabled)
795 table = &attributes->colorremaptables[type];
796 else
797 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
799 for (x=0; x<width; x++)
800 for (y=0; y<height; y++)
802 ARGB *src_color;
803 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
804 for (i=0; i<table->mapsize; i++)
806 if (*src_color == table->colormap[i].oldColor.Argb)
808 *src_color = table->colormap[i].newColor.Argb;
809 break;
815 if (attributes->colormatrices[type].enabled ||
816 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
818 const struct color_matrix *colormatrices;
819 int color_matrix[5][5];
820 int gray_matrix[5][5];
821 BOOL identity;
823 if (!data || fmt != PixelFormat32bppARGB)
824 return PixelFormat32bppARGB;
826 if (attributes->colormatrices[type].enabled)
827 colormatrices = &attributes->colormatrices[type];
828 else
829 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
831 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
833 if (colormatrices->flags == ColorMatrixFlagsAltGray)
834 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
836 if (!identity)
838 for (x=0; x<width; x++)
840 for (y=0; y<height; y++)
842 ARGB *src_color;
843 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
845 if (colormatrices->flags == ColorMatrixFlagsDefault ||
846 !color_is_gray(*src_color))
848 *src_color = transform_color(*src_color, color_matrix);
850 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
852 *src_color = transform_color(*src_color, gray_matrix);
859 if (attributes->gamma_enabled[type] ||
860 attributes->gamma_enabled[ColorAdjustTypeDefault])
862 REAL gamma;
864 if (!data || fmt != PixelFormat32bppARGB)
865 return PixelFormat32bppARGB;
867 if (attributes->gamma_enabled[type])
868 gamma = attributes->gamma[type];
869 else
870 gamma = attributes->gamma[ColorAdjustTypeDefault];
872 for (x=0; x<width; x++)
873 for (y=0; y<height; y++)
875 ARGB *src_color;
876 BYTE blue, green, red;
877 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
879 blue = *src_color&0xff;
880 green = (*src_color>>8)&0xff;
881 red = (*src_color>>16)&0xff;
883 /* FIXME: We should probably use a table for this. */
884 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
885 green = floorf(powf(green / 255.0, gamma) * 255.0);
886 red = floorf(powf(red / 255.0, gamma) * 255.0);
888 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
892 return fmt;
895 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
896 * bitmap that contains all the pixels we may need to draw it. */
897 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
898 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
899 GpRect *rect)
901 INT left, top, right, bottom;
903 switch (interpolation)
905 case InterpolationModeHighQualityBilinear:
906 case InterpolationModeHighQualityBicubic:
907 /* FIXME: Include a greater range for the prefilter? */
908 case InterpolationModeBicubic:
909 case InterpolationModeBilinear:
910 left = (INT)(floorf(srcx));
911 top = (INT)(floorf(srcy));
912 right = (INT)(ceilf(srcx+srcwidth));
913 bottom = (INT)(ceilf(srcy+srcheight));
914 break;
915 case InterpolationModeNearestNeighbor:
916 default:
917 left = gdip_round(srcx);
918 top = gdip_round(srcy);
919 right = gdip_round(srcx+srcwidth);
920 bottom = gdip_round(srcy+srcheight);
921 break;
924 if (wrap == WrapModeClamp)
926 if (left < 0)
927 left = 0;
928 if (top < 0)
929 top = 0;
930 if (right >= bitmap->width)
931 right = bitmap->width-1;
932 if (bottom >= bitmap->height)
933 bottom = bitmap->height-1;
934 if (bottom < top || right < left)
935 /* entirely outside image, just sample a pixel so we don't have to
936 * special-case this later */
937 left = top = right = bottom = 0;
939 else
941 /* In some cases we can make the rectangle smaller here, but the logic
942 * is hard to get right, and tiling suggests we're likely to use the
943 * entire source image. */
944 if (left < 0 || right >= bitmap->width)
946 left = 0;
947 right = bitmap->width-1;
950 if (top < 0 || bottom >= bitmap->height)
952 top = 0;
953 bottom = bitmap->height-1;
957 rect->X = left;
958 rect->Y = top;
959 rect->Width = right - left + 1;
960 rect->Height = bottom - top + 1;
963 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
964 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
966 if (attributes->wrap == WrapModeClamp)
968 if (x < 0 || y < 0 || x >= width || y >= height)
969 return attributes->outside_color;
971 else
973 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
974 if (x < 0)
975 x = width*2 + x % (INT)(width * 2);
976 if (y < 0)
977 y = height*2 + y % (INT)(height * 2);
979 if (attributes->wrap & WrapModeTileFlipX)
981 if ((x / width) % 2 == 0)
982 x = x % width;
983 else
984 x = width - 1 - x % width;
986 else
987 x = x % width;
989 if (attributes->wrap & WrapModeTileFlipY)
991 if ((y / height) % 2 == 0)
992 y = y % height;
993 else
994 y = height - 1 - y % height;
996 else
997 y = y % height;
1000 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
1002 ERR("out of range pixel requested\n");
1003 return 0xffcd0084;
1006 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
1009 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
1010 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
1011 InterpolationMode interpolation, PixelOffsetMode offset_mode)
1013 static int fixme;
1015 switch (interpolation)
1017 default:
1018 if (!fixme++)
1019 FIXME("Unimplemented interpolation %i\n", interpolation);
1020 /* fall-through */
1021 case InterpolationModeBilinear:
1023 REAL leftxf, topyf;
1024 INT leftx, rightx, topy, bottomy;
1025 ARGB topleft, topright, bottomleft, bottomright;
1026 ARGB top, bottom;
1027 float x_offset;
1029 leftxf = floorf(point->X);
1030 leftx = (INT)leftxf;
1031 rightx = (INT)ceilf(point->X);
1032 topyf = floorf(point->Y);
1033 topy = (INT)topyf;
1034 bottomy = (INT)ceilf(point->Y);
1036 if (leftx == rightx && topy == bottomy)
1037 return sample_bitmap_pixel(src_rect, bits, width, height,
1038 leftx, topy, attributes);
1040 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
1041 leftx, topy, attributes);
1042 topright = sample_bitmap_pixel(src_rect, bits, width, height,
1043 rightx, topy, attributes);
1044 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
1045 leftx, bottomy, attributes);
1046 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
1047 rightx, bottomy, attributes);
1049 x_offset = point->X - leftxf;
1050 top = blend_colors(topleft, topright, x_offset);
1051 bottom = blend_colors(bottomleft, bottomright, x_offset);
1053 return blend_colors(top, bottom, point->Y - topyf);
1055 case InterpolationModeNearestNeighbor:
1057 FLOAT pixel_offset;
1058 switch (offset_mode)
1060 default:
1061 case PixelOffsetModeNone:
1062 case PixelOffsetModeHighSpeed:
1063 pixel_offset = 0.5;
1064 break;
1066 case PixelOffsetModeHalf:
1067 case PixelOffsetModeHighQuality:
1068 pixel_offset = 0.0;
1069 break;
1071 return sample_bitmap_pixel(src_rect, bits, width, height,
1072 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1078 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1080 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1083 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1084 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1086 switch (brush->bt)
1088 case BrushTypeSolidColor:
1090 if (is_fill)
1091 return TRUE;
1092 else
1094 /* cannot draw semi-transparent colors */
1095 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1098 case BrushTypeHatchFill:
1100 GpHatch *hatch = (GpHatch*)brush;
1101 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1102 ((hatch->backcol & 0xff000000) == 0xff000000);
1104 case BrushTypeLinearGradient:
1105 case BrushTypeTextureFill:
1106 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1107 default:
1108 return FALSE;
1112 static GpStatus brush_fill_path(GpGraphics *graphics, GpBrush *brush)
1114 GpStatus status = Ok;
1115 switch (brush->bt)
1117 case BrushTypeSolidColor:
1119 GpSolidFill *fill = (GpSolidFill*)brush;
1120 HBITMAP bmp = ARGB2BMP(fill->color);
1122 if (bmp)
1124 RECT rc;
1125 /* partially transparent fill */
1127 if (!SelectClipPath(graphics->hdc, RGN_AND))
1129 status = GenericError;
1130 DeleteObject(bmp);
1131 break;
1133 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1135 HDC hdc = CreateCompatibleDC(NULL);
1137 if (!hdc)
1139 status = OutOfMemory;
1140 DeleteObject(bmp);
1141 break;
1144 SelectObject(hdc, bmp);
1145 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1146 hdc, 0, 0, 1, 1);
1147 DeleteDC(hdc);
1150 DeleteObject(bmp);
1151 break;
1153 /* else fall through */
1155 default:
1157 HBRUSH gdibrush, old_brush;
1159 gdibrush = create_gdi_brush(brush, graphics->origin_x, graphics->origin_y);
1160 if (!gdibrush)
1162 status = OutOfMemory;
1163 break;
1166 old_brush = SelectObject(graphics->hdc, gdibrush);
1167 FillPath(graphics->hdc);
1168 SelectObject(graphics->hdc, old_brush);
1169 DeleteObject(gdibrush);
1170 break;
1174 return status;
1177 static BOOL brush_can_fill_pixels(GpBrush *brush)
1179 switch (brush->bt)
1181 case BrushTypeSolidColor:
1182 case BrushTypeHatchFill:
1183 case BrushTypeLinearGradient:
1184 case BrushTypeTextureFill:
1185 case BrushTypePathGradient:
1186 return TRUE;
1187 default:
1188 return FALSE;
1192 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1193 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1195 switch (brush->bt)
1197 case BrushTypeSolidColor:
1199 int x, y;
1200 GpSolidFill *fill = (GpSolidFill*)brush;
1201 for (x=0; x<fill_area->Width; x++)
1202 for (y=0; y<fill_area->Height; y++)
1203 argb_pixels[x + y*cdwStride] = fill->color;
1204 return Ok;
1206 case BrushTypeHatchFill:
1208 int x, y;
1209 GpHatch *fill = (GpHatch*)brush;
1210 const unsigned char *hatch_data;
1211 ARGB hatch_palette[4];
1213 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1214 return NotImplemented;
1216 init_hatch_palette(hatch_palette, fill->forecol, fill->backcol);
1218 /* See create_hatch_bitmap for an explanation of how index is derived. */
1219 for (y = 0; y < fill_area->Height; y++, argb_pixels += cdwStride)
1221 const int hy = ~(y + fill_area->Y - graphics->origin_y) & 7;
1222 const int hx = graphics->origin_x & 7;
1223 const unsigned int row = (0x10101 * hatch_data[hy]) >> hx;
1225 for (x = 0; x < fill_area->Width; x++)
1227 const unsigned int srow = row >> (~(x + fill_area->X) & 7);
1228 int index;
1229 if (hatch_data[8])
1230 index = (srow & 1) ? 2 : (srow & 0x82) ? 1 : 0;
1231 else
1232 index = (srow & 1) ? 3 : 0;
1234 argb_pixels[x] = hatch_palette[index];
1238 return Ok;
1240 case BrushTypeLinearGradient:
1242 GpLineGradient *fill = (GpLineGradient*)brush;
1243 GpPointF draw_points[3];
1244 GpStatus stat;
1245 int x, y;
1247 draw_points[0].X = fill_area->X;
1248 draw_points[0].Y = fill_area->Y;
1249 draw_points[1].X = fill_area->X+1;
1250 draw_points[1].Y = fill_area->Y;
1251 draw_points[2].X = fill_area->X;
1252 draw_points[2].Y = fill_area->Y+1;
1254 /* Transform the points to a co-ordinate space where X is the point's
1255 * position in the gradient, 0.0 being the start point and 1.0 the
1256 * end point. */
1257 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1258 WineCoordinateSpaceGdiDevice, draw_points, 3);
1260 if (stat == Ok)
1262 GpMatrix world_to_gradient = fill->transform;
1264 stat = GdipInvertMatrix(&world_to_gradient);
1265 if (stat == Ok)
1266 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1269 if (stat == Ok)
1271 REAL x_delta = draw_points[1].X - draw_points[0].X;
1272 REAL y_delta = draw_points[2].X - draw_points[0].X;
1274 for (y=0; y<fill_area->Height; y++)
1276 for (x=0; x<fill_area->Width; x++)
1278 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1280 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1285 return stat;
1287 case BrushTypeTextureFill:
1289 GpTexture *fill = (GpTexture*)brush;
1290 GpPointF draw_points[3];
1291 GpStatus stat;
1292 int x, y;
1293 GpBitmap *bitmap;
1294 int src_stride;
1295 GpRect src_area;
1297 if (fill->image->type != ImageTypeBitmap)
1299 FIXME("metafile texture brushes not implemented\n");
1300 return NotImplemented;
1303 bitmap = (GpBitmap*)fill->image;
1304 src_stride = sizeof(ARGB) * bitmap->width;
1306 src_area.X = src_area.Y = 0;
1307 src_area.Width = bitmap->width;
1308 src_area.Height = bitmap->height;
1310 draw_points[0].X = fill_area->X;
1311 draw_points[0].Y = fill_area->Y;
1312 draw_points[1].X = fill_area->X+1;
1313 draw_points[1].Y = fill_area->Y;
1314 draw_points[2].X = fill_area->X;
1315 draw_points[2].Y = fill_area->Y+1;
1317 /* Transform the points to the co-ordinate space of the bitmap. */
1318 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1319 WineCoordinateSpaceGdiDevice, draw_points, 3);
1321 if (stat == Ok)
1323 GpMatrix world_to_texture = fill->transform;
1325 stat = GdipInvertMatrix(&world_to_texture);
1326 if (stat == Ok)
1327 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1330 if (stat == Ok && !fill->bitmap_bits)
1332 BitmapData lockeddata;
1334 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1335 if (!fill->bitmap_bits)
1336 stat = OutOfMemory;
1338 if (stat == Ok)
1340 lockeddata.Width = bitmap->width;
1341 lockeddata.Height = bitmap->height;
1342 lockeddata.Stride = src_stride;
1343 lockeddata.PixelFormat = PixelFormat32bppARGB;
1344 lockeddata.Scan0 = fill->bitmap_bits;
1346 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1347 PixelFormat32bppARGB, &lockeddata);
1350 if (stat == Ok)
1351 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1353 if (stat == Ok)
1354 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1355 bitmap->width, bitmap->height,
1356 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1358 if (stat != Ok)
1360 heap_free(fill->bitmap_bits);
1361 fill->bitmap_bits = NULL;
1365 if (stat == Ok)
1367 REAL x_dx = draw_points[1].X - draw_points[0].X;
1368 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1369 REAL y_dx = draw_points[2].X - draw_points[0].X;
1370 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1372 for (y=0; y<fill_area->Height; y++)
1374 for (x=0; x<fill_area->Width; x++)
1376 GpPointF point;
1377 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1378 point.Y = draw_points[0].Y + x * x_dy + y * y_dy;
1380 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1381 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1382 &point, fill->imageattributes, graphics->interpolation,
1383 graphics->pixeloffset);
1388 return stat;
1390 case BrushTypePathGradient:
1392 GpPathGradient *fill = (GpPathGradient*)brush;
1393 GpPath *flat_path;
1394 GpMatrix world_to_device;
1395 GpStatus stat;
1396 int i, figure_start=0;
1397 GpPointF start_point, end_point, center_point;
1398 BYTE type;
1399 REAL min_yf, max_yf, line1_xf, line2_xf;
1400 INT min_y, max_y, min_x, max_x;
1401 INT x, y;
1402 ARGB outer_color;
1403 static BOOL transform_fixme_once;
1405 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1407 static int once;
1408 if (!once++)
1409 FIXME("path gradient focus not implemented\n");
1412 if (fill->gamma)
1414 static int once;
1415 if (!once++)
1416 FIXME("path gradient gamma correction not implemented\n");
1419 if (fill->blendcount)
1421 static int once;
1422 if (!once++)
1423 FIXME("path gradient blend not implemented\n");
1426 if (fill->pblendcount)
1428 static int once;
1429 if (!once++)
1430 FIXME("path gradient preset blend not implemented\n");
1433 if (!transform_fixme_once)
1435 BOOL is_identity=TRUE;
1436 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1437 if (!is_identity)
1439 FIXME("path gradient transform not implemented\n");
1440 transform_fixme_once = TRUE;
1444 stat = GdipClonePath(fill->path, &flat_path);
1446 if (stat != Ok)
1447 return stat;
1449 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1450 CoordinateSpaceWorld, &world_to_device);
1451 if (stat == Ok)
1453 stat = GdipTransformPath(flat_path, &world_to_device);
1455 if (stat == Ok)
1457 center_point = fill->center;
1458 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1461 if (stat == Ok)
1462 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1465 if (stat != Ok)
1467 GdipDeletePath(flat_path);
1468 return stat;
1471 for (i=0; i<flat_path->pathdata.Count; i++)
1473 int start_center_line=0, end_center_line=0;
1474 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1475 REAL center_distance;
1476 ARGB start_color, end_color;
1477 REAL dy, dx;
1479 type = flat_path->pathdata.Types[i];
1481 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1482 figure_start = i;
1484 start_point = flat_path->pathdata.Points[i];
1486 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1488 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1490 end_point = flat_path->pathdata.Points[figure_start];
1491 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1493 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1495 end_point = flat_path->pathdata.Points[i+1];
1496 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1498 else
1499 continue;
1501 outer_color = start_color;
1503 min_yf = center_point.Y;
1504 if (min_yf > start_point.Y) min_yf = start_point.Y;
1505 if (min_yf > end_point.Y) min_yf = end_point.Y;
1507 if (min_yf < fill_area->Y)
1508 min_y = fill_area->Y;
1509 else
1510 min_y = (INT)ceil(min_yf);
1512 max_yf = center_point.Y;
1513 if (max_yf < start_point.Y) max_yf = start_point.Y;
1514 if (max_yf < end_point.Y) max_yf = end_point.Y;
1516 if (max_yf > fill_area->Y + fill_area->Height)
1517 max_y = fill_area->Y + fill_area->Height;
1518 else
1519 max_y = (INT)ceil(max_yf);
1521 dy = end_point.Y - start_point.Y;
1522 dx = end_point.X - start_point.X;
1524 /* This is proportional to the distance from start-end line to center point. */
1525 center_distance = dy * (start_point.X - center_point.X) +
1526 dx * (center_point.Y - start_point.Y);
1528 for (y=min_y; y<max_y; y++)
1530 REAL yf = (REAL)y;
1532 if (!seen_start && yf >= start_point.Y)
1534 seen_start = TRUE;
1535 start_center_line ^= 1;
1537 if (!seen_end && yf >= end_point.Y)
1539 seen_end = TRUE;
1540 end_center_line ^= 1;
1542 if (!seen_center && yf >= center_point.Y)
1544 seen_center = TRUE;
1545 start_center_line ^= 1;
1546 end_center_line ^= 1;
1549 if (start_center_line)
1550 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1551 else
1552 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1554 if (end_center_line)
1555 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1556 else
1557 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1559 if (line1_xf < line2_xf)
1561 min_x = (INT)ceil(line1_xf);
1562 max_x = (INT)ceil(line2_xf);
1564 else
1566 min_x = (INT)ceil(line2_xf);
1567 max_x = (INT)ceil(line1_xf);
1570 if (min_x < fill_area->X)
1571 min_x = fill_area->X;
1572 if (max_x > fill_area->X + fill_area->Width)
1573 max_x = fill_area->X + fill_area->Width;
1575 for (x=min_x; x<max_x; x++)
1577 REAL xf = (REAL)x;
1578 REAL distance;
1580 if (start_color != end_color)
1582 REAL blend_amount, pdy, pdx;
1583 pdy = yf - center_point.Y;
1584 pdx = xf - center_point.X;
1586 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1588 /* Too close to center point, don't try to calculate outer color */
1589 outer_color = start_color;
1591 else
1593 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1594 outer_color = blend_colors(start_color, end_color, blend_amount);
1598 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1599 (end_point.X - start_point.X) * (yf - start_point.Y);
1601 distance = distance / center_distance;
1603 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1604 blend_colors(outer_color, fill->centercolor, distance);
1609 GdipDeletePath(flat_path);
1610 return stat;
1612 default:
1613 return NotImplemented;
1617 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1618 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1619 * should not be called on an hdc that has a path you care about. */
1620 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1621 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1623 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1624 GpMatrix matrix;
1625 HBRUSH brush = NULL;
1626 HPEN pen = NULL;
1627 PointF ptf[4], *custptf = NULL;
1628 POINT pt[4], *custpt = NULL;
1629 BYTE *tp = NULL;
1630 REAL theta, dsmall, dbig, dx, dy = 0.0;
1631 INT i, count;
1632 LOGBRUSH lb;
1633 BOOL customstroke;
1635 if((x1 == x2) && (y1 == y2))
1636 return;
1638 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1640 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1641 if(!customstroke){
1642 brush = CreateSolidBrush(color);
1643 lb.lbStyle = BS_SOLID;
1644 lb.lbColor = color;
1645 lb.lbHatch = 0;
1646 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1647 PS_JOIN_MITER, 1, &lb, 0,
1648 NULL);
1649 oldbrush = SelectObject(graphics->hdc, brush);
1650 oldpen = SelectObject(graphics->hdc, pen);
1653 switch(cap){
1654 case LineCapFlat:
1655 break;
1656 case LineCapSquare:
1657 case LineCapSquareAnchor:
1658 case LineCapDiamondAnchor:
1659 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1660 if(cap == LineCapDiamondAnchor){
1661 dsmall = cos(theta + M_PI_2) * size;
1662 dbig = sin(theta + M_PI_2) * size;
1664 else{
1665 dsmall = cos(theta + M_PI_4) * size;
1666 dbig = sin(theta + M_PI_4) * size;
1669 ptf[0].X = x2 - dsmall;
1670 ptf[1].X = x2 + dbig;
1672 ptf[0].Y = y2 - dbig;
1673 ptf[3].Y = y2 + dsmall;
1675 ptf[1].Y = y2 - dsmall;
1676 ptf[2].Y = y2 + dbig;
1678 ptf[3].X = x2 - dbig;
1679 ptf[2].X = x2 + dsmall;
1681 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1683 round_points(pt, ptf, 4);
1685 Polygon(graphics->hdc, pt, 4);
1687 break;
1688 case LineCapArrowAnchor:
1689 size = size * 4.0 / sqrt(3.0);
1691 dx = cos(M_PI / 6.0 + theta) * size;
1692 dy = sin(M_PI / 6.0 + theta) * size;
1694 ptf[0].X = x2 - dx;
1695 ptf[0].Y = y2 - dy;
1697 dx = cos(- M_PI / 6.0 + theta) * size;
1698 dy = sin(- M_PI / 6.0 + theta) * size;
1700 ptf[1].X = x2 - dx;
1701 ptf[1].Y = y2 - dy;
1703 ptf[2].X = x2;
1704 ptf[2].Y = y2;
1706 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1708 round_points(pt, ptf, 3);
1710 Polygon(graphics->hdc, pt, 3);
1712 break;
1713 case LineCapRoundAnchor:
1714 dx = dy = ANCHOR_WIDTH * size / 2.0;
1716 ptf[0].X = x2 - dx;
1717 ptf[0].Y = y2 - dy;
1718 ptf[1].X = x2 + dx;
1719 ptf[1].Y = y2 + dy;
1721 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 2);
1723 round_points(pt, ptf, 2);
1725 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1727 break;
1728 case LineCapTriangle:
1729 size = size / 2.0;
1730 dx = cos(M_PI_2 + theta) * size;
1731 dy = sin(M_PI_2 + theta) * size;
1733 ptf[0].X = x2 - dx;
1734 ptf[0].Y = y2 - dy;
1735 ptf[1].X = x2 + dx;
1736 ptf[1].Y = y2 + dy;
1738 dx = cos(theta) * size;
1739 dy = sin(theta) * size;
1741 ptf[2].X = x2 + dx;
1742 ptf[2].Y = y2 + dy;
1744 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1746 round_points(pt, ptf, 3);
1748 Polygon(graphics->hdc, pt, 3);
1750 break;
1751 case LineCapRound:
1752 dx = dy = size / 2.0;
1754 ptf[0].X = x2 - dx;
1755 ptf[0].Y = y2 - dy;
1756 ptf[1].X = x2 + dx;
1757 ptf[1].Y = y2 + dy;
1759 dx = -cos(M_PI_2 + theta) * size;
1760 dy = -sin(M_PI_2 + theta) * size;
1762 ptf[2].X = x2 - dx;
1763 ptf[2].Y = y2 - dy;
1764 ptf[3].X = x2 + dx;
1765 ptf[3].Y = y2 + dy;
1767 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1769 round_points(pt, ptf, 4);
1771 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1772 pt[2].y, pt[3].x, pt[3].y);
1774 break;
1775 case LineCapCustom:
1776 if(!custom)
1777 break;
1779 if (custom->type == CustomLineCapTypeAdjustableArrow)
1781 GpAdjustableArrowCap *arrow = (GpAdjustableArrowCap *)custom;
1782 if (arrow->cap.fill && arrow->height <= 0.0)
1783 break;
1786 count = custom->pathdata.Count;
1787 custptf = heap_alloc_zero(count * sizeof(PointF));
1788 custpt = heap_alloc_zero(count * sizeof(POINT));
1789 tp = heap_alloc_zero(count);
1791 if(!custptf || !custpt || !tp)
1792 goto custend;
1794 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1796 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1797 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1798 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1799 MatrixOrderAppend);
1800 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1801 GdipTransformMatrixPoints(&matrix, custptf, count);
1803 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, custptf, count);
1805 round_points(custpt, custptf, count);
1807 for(i = 0; i < count; i++)
1808 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1810 if(custom->fill){
1811 BeginPath(graphics->hdc);
1812 PolyDraw(graphics->hdc, custpt, tp, count);
1813 EndPath(graphics->hdc);
1814 StrokeAndFillPath(graphics->hdc);
1816 else
1817 PolyDraw(graphics->hdc, custpt, tp, count);
1819 custend:
1820 heap_free(custptf);
1821 heap_free(custpt);
1822 heap_free(tp);
1823 break;
1824 default:
1825 break;
1828 if(!customstroke){
1829 SelectObject(graphics->hdc, oldbrush);
1830 SelectObject(graphics->hdc, oldpen);
1831 DeleteObject(brush);
1832 DeleteObject(pen);
1836 /* Shortens the line by the given percent by changing x2, y2.
1837 * If percent is > 1.0 then the line will change direction.
1838 * If percent is negative it can lengthen the line. */
1839 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1841 REAL dist, theta, dx, dy;
1843 if((y1 == *y2) && (x1 == *x2))
1844 return;
1846 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1847 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1848 dx = cos(theta) * dist;
1849 dy = sin(theta) * dist;
1851 *x2 = *x2 + dx;
1852 *y2 = *y2 + dy;
1855 /* Shortens the line by the given amount by changing x2, y2.
1856 * If the amount is greater than the distance, the line will become length 0.
1857 * If the amount is negative, it can lengthen the line. */
1858 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1860 REAL dx, dy, percent;
1862 dx = *x2 - x1;
1863 dy = *y2 - y1;
1864 if(dx == 0 && dy == 0)
1865 return;
1867 percent = amt / sqrt(dx * dx + dy * dy);
1868 if(percent >= 1.0){
1869 *x2 = x1;
1870 *y2 = y1;
1871 return;
1874 shorten_line_percent(x1, y1, x2, y2, percent);
1877 /* Conducts a linear search to find the bezier points that will back off
1878 * the endpoint of the curve by a distance of amt. Linear search works
1879 * better than binary in this case because there are multiple solutions,
1880 * and binary searches often find a bad one. I don't think this is what
1881 * Windows does but short of rendering the bezier without GDI's help it's
1882 * the best we can do. If rev then work from the start of the passed points
1883 * instead of the end. */
1884 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1886 GpPointF origpt[4];
1887 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1888 INT i, first = 0, second = 1, third = 2, fourth = 3;
1890 if(rev){
1891 first = 3;
1892 second = 2;
1893 third = 1;
1894 fourth = 0;
1897 origx = pt[fourth].X;
1898 origy = pt[fourth].Y;
1899 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1901 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1902 /* reset bezier points to original values */
1903 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1904 /* Perform magic on bezier points. Order is important here.*/
1905 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1906 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1907 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1908 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1909 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1910 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1912 dx = pt[fourth].X - origx;
1913 dy = pt[fourth].Y - origy;
1915 diff = sqrt(dx * dx + dy * dy);
1916 percent += 0.0005 * amt;
1920 /* Draws a combination of bezier curves and lines between points. */
1921 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1922 GDIPCONST BYTE * types, INT count, BOOL caps)
1924 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1925 BYTE *tp = heap_alloc_zero(count);
1926 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1927 INT i, j;
1928 GpStatus status = GenericError;
1930 if(!count){
1931 status = Ok;
1932 goto end;
1934 if(!pti || !tp || !ptcopy){
1935 status = OutOfMemory;
1936 goto end;
1939 for(i = 1; i < count; i++){
1940 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1941 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1942 || !(types[i + 2] & PathPointTypeBezier)){
1943 ERR("Bad bezier points\n");
1944 goto end;
1946 i += 2;
1950 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1952 /* If we are drawing caps, go through the points and adjust them accordingly,
1953 * and draw the caps. */
1954 if(caps){
1955 switch(types[count - 1] & PathPointTypePathTypeMask){
1956 case PathPointTypeBezier:
1957 if(pen->endcap == LineCapArrowAnchor)
1958 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1959 else if((pen->endcap == LineCapCustom) && pen->customend)
1960 shorten_bezier_amt(&ptcopy[count - 4],
1961 pen->width * pen->customend->inset, FALSE);
1963 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1964 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1965 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1966 pt[count - 1].X, pt[count - 1].Y);
1968 break;
1969 case PathPointTypeLine:
1970 if(pen->endcap == LineCapArrowAnchor)
1971 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1972 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1973 pen->width);
1974 else if((pen->endcap == LineCapCustom) && pen->customend)
1975 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1976 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1977 pen->customend->inset * pen->width);
1979 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1980 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1981 pt[count - 1].Y);
1983 break;
1984 default:
1985 ERR("Bad path last point\n");
1986 goto end;
1989 /* Find start of points */
1990 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1991 == PathPointTypeStart); j++);
1993 switch(types[j] & PathPointTypePathTypeMask){
1994 case PathPointTypeBezier:
1995 if(pen->startcap == LineCapArrowAnchor)
1996 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1997 else if((pen->startcap == LineCapCustom) && pen->customstart)
1998 shorten_bezier_amt(&ptcopy[j - 1],
1999 pen->width * pen->customstart->inset, TRUE);
2001 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2002 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
2003 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
2004 pt[j - 1].X, pt[j - 1].Y);
2006 break;
2007 case PathPointTypeLine:
2008 if(pen->startcap == LineCapArrowAnchor)
2009 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2010 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2011 pen->width);
2012 else if((pen->startcap == LineCapCustom) && pen->customstart)
2013 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2014 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2015 pen->customstart->inset * pen->width);
2017 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2018 pt[j].X, pt[j].Y, pt[j - 1].X,
2019 pt[j - 1].Y);
2021 break;
2022 default:
2023 ERR("Bad path points\n");
2024 goto end;
2028 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
2030 round_points(pti, ptcopy, count);
2032 for(i = 0; i < count; i++){
2033 tp[i] = convert_path_point_type(types[i]);
2036 PolyDraw(graphics->hdc, pti, tp, count);
2038 status = Ok;
2040 end:
2041 heap_free(pti);
2042 heap_free(ptcopy);
2043 heap_free(tp);
2045 return status;
2048 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2050 GpStatus result;
2052 BeginPath(graphics->hdc);
2053 result = draw_poly(graphics, NULL, path->pathdata.Points,
2054 path->pathdata.Types, path->pathdata.Count, FALSE);
2055 EndPath(graphics->hdc);
2056 return result;
2059 typedef enum GraphicsContainerType {
2060 BEGIN_CONTAINER,
2061 SAVE_GRAPHICS
2062 } GraphicsContainerType;
2064 typedef struct _GraphicsContainerItem {
2065 struct list entry;
2066 GraphicsContainer contid;
2067 GraphicsContainerType type;
2069 SmoothingMode smoothing;
2070 CompositingQuality compqual;
2071 InterpolationMode interpolation;
2072 CompositingMode compmode;
2073 TextRenderingHint texthint;
2074 REAL scale;
2075 GpUnit unit;
2076 PixelOffsetMode pixeloffset;
2077 UINT textcontrast;
2078 GpMatrix worldtrans;
2079 GpRegion* clip;
2080 INT origin_x, origin_y;
2081 } GraphicsContainerItem;
2083 static GpStatus init_container(GraphicsContainerItem** container,
2084 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
2085 GpStatus sts;
2087 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
2088 if(!(*container))
2089 return OutOfMemory;
2091 (*container)->contid = graphics->contid + 1;
2092 (*container)->type = type;
2094 (*container)->smoothing = graphics->smoothing;
2095 (*container)->compqual = graphics->compqual;
2096 (*container)->interpolation = graphics->interpolation;
2097 (*container)->compmode = graphics->compmode;
2098 (*container)->texthint = graphics->texthint;
2099 (*container)->scale = graphics->scale;
2100 (*container)->unit = graphics->unit;
2101 (*container)->textcontrast = graphics->textcontrast;
2102 (*container)->pixeloffset = graphics->pixeloffset;
2103 (*container)->origin_x = graphics->origin_x;
2104 (*container)->origin_y = graphics->origin_y;
2105 (*container)->worldtrans = graphics->worldtrans;
2107 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2108 if(sts != Ok){
2109 heap_free(*container);
2110 *container = NULL;
2111 return sts;
2114 return Ok;
2117 static void delete_container(GraphicsContainerItem* container)
2119 GdipDeleteRegion(container->clip);
2120 heap_free(container);
2123 static GpStatus restore_container(GpGraphics* graphics,
2124 GDIPCONST GraphicsContainerItem* container){
2125 GpStatus sts;
2126 GpRegion *newClip;
2128 sts = GdipCloneRegion(container->clip, &newClip);
2129 if(sts != Ok) return sts;
2131 graphics->worldtrans = container->worldtrans;
2133 GdipDeleteRegion(graphics->clip);
2134 graphics->clip = newClip;
2136 graphics->contid = container->contid - 1;
2138 graphics->smoothing = container->smoothing;
2139 graphics->compqual = container->compqual;
2140 graphics->interpolation = container->interpolation;
2141 graphics->compmode = container->compmode;
2142 graphics->texthint = container->texthint;
2143 graphics->scale = container->scale;
2144 graphics->unit = container->unit;
2145 graphics->textcontrast = container->textcontrast;
2146 graphics->pixeloffset = container->pixeloffset;
2147 graphics->origin_x = container->origin_x;
2148 graphics->origin_y = container->origin_y;
2150 return Ok;
2153 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2155 RECT wnd_rect;
2156 GpStatus stat=Ok;
2157 GpUnit unit;
2159 if(graphics->hwnd) {
2160 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2161 return GenericError;
2163 rect->X = wnd_rect.left;
2164 rect->Y = wnd_rect.top;
2165 rect->Width = wnd_rect.right - wnd_rect.left;
2166 rect->Height = wnd_rect.bottom - wnd_rect.top;
2167 }else if (graphics->image){
2168 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2169 if (stat == Ok && unit != UnitPixel)
2170 FIXME("need to convert from unit %i\n", unit);
2171 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2172 HBITMAP hbmp;
2173 BITMAP bmp;
2175 rect->X = 0;
2176 rect->Y = 0;
2178 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2179 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2181 rect->Width = bmp.bmWidth;
2182 rect->Height = bmp.bmHeight;
2184 else
2186 /* FIXME: ??? */
2187 rect->Width = 1;
2188 rect->Height = 1;
2190 }else{
2191 rect->X = 0;
2192 rect->Y = 0;
2193 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2194 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2197 return stat;
2200 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2202 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2204 if (stat == Ok && graphics->hdc)
2206 GpPointF points[4], min_point, max_point;
2207 int i;
2209 points[0].X = points[2].X = rect->X;
2210 points[0].Y = points[1].Y = rect->Y;
2211 points[1].X = points[3].X = rect->X + rect->Width;
2212 points[2].Y = points[3].Y = rect->Y + rect->Height;
2214 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2216 min_point = max_point = points[0];
2218 for (i=1; i<4; i++)
2220 if (points[i].X < min_point.X) min_point.X = points[i].X;
2221 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2222 if (points[i].X > max_point.X) max_point.X = points[i].X;
2223 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2226 rect->X = min_point.X;
2227 rect->Y = min_point.Y;
2228 rect->Width = max_point.X - min_point.X;
2229 rect->Height = max_point.Y - min_point.Y;
2232 return stat;
2235 /* on success, rgn will contain the region of the graphics object which
2236 * is visible after clipping has been applied */
2237 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2239 GpStatus stat;
2240 GpRectF rectf;
2241 GpRegion* tmp;
2243 /* Ignore graphics image bounds for metafiles */
2244 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2245 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2247 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2248 return stat;
2250 if((stat = GdipCreateRegion(&tmp)) != Ok)
2251 return stat;
2253 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2254 goto end;
2256 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2257 goto end;
2259 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2261 end:
2262 GdipDeleteRegion(tmp);
2263 return stat;
2266 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2268 REAL height;
2270 if (font->unit == UnitPixel)
2272 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres, graphics->printer_display);
2274 else
2276 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2277 height = units_to_pixels(font->emSize, font->unit, graphics->xres, graphics->printer_display);
2278 else
2279 height = units_to_pixels(font->emSize, font->unit, graphics->yres, graphics->printer_display);
2282 lf->lfHeight = -(height + 0.5);
2283 lf->lfWidth = 0;
2284 lf->lfEscapement = 0;
2285 lf->lfOrientation = 0;
2286 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2287 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2288 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2289 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2290 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2291 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2292 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2293 lf->lfQuality = DEFAULT_QUALITY;
2294 lf->lfPitchAndFamily = 0;
2295 lstrcpyW(lf->lfFaceName, font->family->FamilyName);
2298 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2299 GDIPCONST GpStringFormat *format, HFONT *hfont,
2300 LOGFONTW *lfw_return, GDIPCONST GpMatrix *matrix)
2302 HDC hdc = CreateCompatibleDC(0);
2303 GpPointF pt[3];
2304 REAL angle, rel_width, rel_height, font_height;
2305 LOGFONTW lfw;
2306 HFONT unscaled_font;
2307 TEXTMETRICW textmet;
2309 if (font->unit == UnitPixel || font->unit == UnitWorld)
2310 font_height = font->emSize;
2311 else
2313 REAL unit_scale, res;
2315 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2316 unit_scale = units_scale(font->unit, graphics->unit, res, graphics->printer_display);
2318 font_height = font->emSize * unit_scale;
2321 pt[0].X = 0.0;
2322 pt[0].Y = 0.0;
2323 pt[1].X = 1.0;
2324 pt[1].Y = 0.0;
2325 pt[2].X = 0.0;
2326 pt[2].Y = 1.0;
2327 if (matrix)
2329 GpMatrix xform = *matrix;
2330 GdipTransformMatrixPoints(&xform, pt, 3);
2333 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2334 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2335 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2336 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2337 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2338 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2339 /* If the font unit is not pixels scaling should not be applied */
2340 if (font->unit != UnitPixel && font->unit != UnitWorld)
2342 rel_width /= graphics->scale;
2343 rel_height /= graphics->scale;
2346 get_log_fontW(font, graphics, &lfw);
2347 lfw.lfHeight = -gdip_round(font_height * rel_height);
2348 unscaled_font = CreateFontIndirectW(&lfw);
2350 SelectObject(hdc, unscaled_font);
2351 GetTextMetricsW(hdc, &textmet);
2353 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2354 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2356 *hfont = CreateFontIndirectW(&lfw);
2358 if (lfw_return)
2359 *lfw_return = lfw;
2361 DeleteDC(hdc);
2362 DeleteObject(unscaled_font);
2365 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2367 TRACE("(%p, %p)\n", hdc, graphics);
2369 return GdipCreateFromHDC2(hdc, NULL, graphics);
2372 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2374 XFORM xform;
2376 if (graphics->hdc == NULL)
2378 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2379 return;
2382 GetTransform(graphics->hdc, 0x204, &xform);
2383 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2386 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2388 GpStatus retval;
2389 HBITMAP hbitmap;
2390 DIBSECTION dib;
2392 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2394 if(hDevice != NULL)
2395 FIXME("Don't know how to handle parameter hDevice\n");
2397 if(hdc == NULL)
2398 return OutOfMemory;
2400 if(graphics == NULL)
2401 return InvalidParameter;
2403 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2404 if(!*graphics) return OutOfMemory;
2406 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2408 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2409 heap_free(*graphics);
2410 return retval;
2413 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2414 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2415 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2417 (*graphics)->alpha_hdc = 1;
2420 (*graphics)->hdc = hdc;
2421 (*graphics)->hwnd = WindowFromDC(hdc);
2422 (*graphics)->owndc = FALSE;
2423 (*graphics)->smoothing = SmoothingModeDefault;
2424 (*graphics)->compqual = CompositingQualityDefault;
2425 (*graphics)->interpolation = InterpolationModeBilinear;
2426 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2427 (*graphics)->compmode = CompositingModeSourceOver;
2428 (*graphics)->unit = UnitDisplay;
2429 (*graphics)->scale = 1.0;
2430 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2431 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2432 (*graphics)->busy = FALSE;
2433 (*graphics)->textcontrast = 4;
2434 list_init(&(*graphics)->containers);
2435 (*graphics)->contid = 0;
2436 (*graphics)->printer_display = (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER);
2437 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2439 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2440 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2442 DeleteObject((*graphics)->gdi_clip);
2443 (*graphics)->gdi_clip = NULL;
2446 TRACE("<-- %p\n", *graphics);
2448 return Ok;
2451 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2453 GpStatus retval;
2455 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2456 if(!*graphics) return OutOfMemory;
2458 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2459 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2461 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2462 heap_free(*graphics);
2463 return retval;
2466 (*graphics)->hdc = NULL;
2467 (*graphics)->hwnd = NULL;
2468 (*graphics)->owndc = FALSE;
2469 (*graphics)->image = image;
2470 /* We have to store the image type here because the image may be freed
2471 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2472 (*graphics)->image_type = image->type;
2473 (*graphics)->smoothing = SmoothingModeDefault;
2474 (*graphics)->compqual = CompositingQualityDefault;
2475 (*graphics)->interpolation = InterpolationModeBilinear;
2476 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2477 (*graphics)->compmode = CompositingModeSourceOver;
2478 (*graphics)->unit = UnitDisplay;
2479 (*graphics)->scale = 1.0;
2480 (*graphics)->xres = image->xres;
2481 (*graphics)->yres = image->yres;
2482 (*graphics)->busy = FALSE;
2483 (*graphics)->textcontrast = 4;
2484 list_init(&(*graphics)->containers);
2485 (*graphics)->contid = 0;
2487 TRACE("<-- %p\n", *graphics);
2489 return Ok;
2492 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2494 GpStatus ret;
2495 HDC hdc;
2497 TRACE("(%p, %p)\n", hwnd, graphics);
2499 hdc = GetDC(hwnd);
2501 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2503 ReleaseDC(hwnd, hdc);
2504 return ret;
2507 (*graphics)->hwnd = hwnd;
2508 (*graphics)->owndc = TRUE;
2510 return Ok;
2513 /* FIXME: no icm handling */
2514 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2516 TRACE("(%p, %p)\n", hwnd, graphics);
2518 return GdipCreateFromHWND(hwnd, graphics);
2521 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2522 UINT access, IStream **stream)
2524 DWORD dwMode;
2525 HRESULT ret;
2527 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2529 if(!stream || !filename)
2530 return InvalidParameter;
2532 if(access & GENERIC_WRITE)
2533 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2534 else if(access & GENERIC_READ)
2535 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2536 else
2537 return InvalidParameter;
2539 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2541 return hresult_to_status(ret);
2544 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2546 GraphicsContainerItem *cont, *next;
2547 GpStatus stat;
2548 TRACE("(%p)\n", graphics);
2550 if(!graphics) return InvalidParameter;
2551 if(graphics->busy) return ObjectBusy;
2553 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2555 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2556 if (stat != Ok)
2557 return stat;
2560 if (graphics->temp_hdc)
2562 DeleteDC(graphics->temp_hdc);
2563 graphics->temp_hdc = NULL;
2566 if(graphics->owndc)
2567 ReleaseDC(graphics->hwnd, graphics->hdc);
2569 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2570 list_remove(&cont->entry);
2571 delete_container(cont);
2574 GdipDeleteRegion(graphics->clip);
2576 DeleteObject(graphics->gdi_clip);
2578 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2579 * do otherwise, but we can't have that in the test suite because it means
2580 * accessing freed memory. */
2581 graphics->busy = TRUE;
2583 heap_free(graphics);
2585 return Ok;
2588 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2589 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2591 GpStatus status;
2592 GpPath *path;
2594 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2595 width, height, startAngle, sweepAngle);
2597 if(!graphics || !pen || width <= 0 || height <= 0)
2598 return InvalidParameter;
2600 if(graphics->busy)
2601 return ObjectBusy;
2603 status = GdipCreatePath(FillModeAlternate, &path);
2604 if (status != Ok) return status;
2606 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2607 if (status == Ok)
2608 status = GdipDrawPath(graphics, pen, path);
2610 GdipDeletePath(path);
2611 return status;
2614 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2615 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2617 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2618 width, height, startAngle, sweepAngle);
2620 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2623 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2624 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2626 GpPointF pt[4];
2628 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2629 x2, y2, x3, y3, x4, y4);
2631 if(!graphics || !pen)
2632 return InvalidParameter;
2634 if(graphics->busy)
2635 return ObjectBusy;
2637 pt[0].X = x1;
2638 pt[0].Y = y1;
2639 pt[1].X = x2;
2640 pt[1].Y = y2;
2641 pt[2].X = x3;
2642 pt[2].Y = y3;
2643 pt[3].X = x4;
2644 pt[3].Y = y4;
2645 return GdipDrawBeziers(graphics, pen, pt, 4);
2648 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2649 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2651 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2652 x2, y2, x3, y3, x4, y4);
2654 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2657 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2658 GDIPCONST GpPointF *points, INT count)
2660 GpStatus status;
2661 GpPath *path;
2663 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2665 if(!graphics || !pen || !points || (count <= 0))
2666 return InvalidParameter;
2668 if(graphics->busy)
2669 return ObjectBusy;
2671 status = GdipCreatePath(FillModeAlternate, &path);
2672 if (status != Ok) return status;
2674 status = GdipAddPathBeziers(path, points, count);
2675 if (status == Ok)
2676 status = GdipDrawPath(graphics, pen, path);
2678 GdipDeletePath(path);
2679 return status;
2682 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2683 GDIPCONST GpPoint *points, INT count)
2685 GpPointF *pts;
2686 GpStatus ret;
2687 INT i;
2689 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2691 if(!graphics || !pen || !points || (count <= 0))
2692 return InvalidParameter;
2694 if(graphics->busy)
2695 return ObjectBusy;
2697 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2698 if(!pts)
2699 return OutOfMemory;
2701 for(i = 0; i < count; i++){
2702 pts[i].X = (REAL)points[i].X;
2703 pts[i].Y = (REAL)points[i].Y;
2706 ret = GdipDrawBeziers(graphics,pen,pts,count);
2708 heap_free(pts);
2710 return ret;
2713 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2714 GDIPCONST GpPointF *points, INT count)
2716 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2718 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2721 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2722 GDIPCONST GpPoint *points, INT count)
2724 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2726 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2729 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2730 GDIPCONST GpPointF *points, INT count, REAL tension)
2732 GpPath *path;
2733 GpStatus status;
2735 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2737 if(!graphics || !pen || !points || count <= 0)
2738 return InvalidParameter;
2740 if(graphics->busy)
2741 return ObjectBusy;
2743 status = GdipCreatePath(FillModeAlternate, &path);
2744 if (status != Ok) return status;
2746 status = GdipAddPathClosedCurve2(path, points, count, tension);
2747 if (status == Ok)
2748 status = GdipDrawPath(graphics, pen, path);
2750 GdipDeletePath(path);
2752 return status;
2755 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2756 GDIPCONST GpPoint *points, INT count, REAL tension)
2758 GpPointF *ptf;
2759 GpStatus stat;
2760 INT i;
2762 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2764 if(!points || count <= 0)
2765 return InvalidParameter;
2767 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2768 if(!ptf)
2769 return OutOfMemory;
2771 for(i = 0; i < count; i++){
2772 ptf[i].X = (REAL)points[i].X;
2773 ptf[i].Y = (REAL)points[i].Y;
2776 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2778 heap_free(ptf);
2780 return stat;
2783 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2784 GDIPCONST GpPointF *points, INT count)
2786 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2788 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2791 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2792 GDIPCONST GpPoint *points, INT count)
2794 GpPointF *pointsF;
2795 GpStatus ret;
2796 INT i;
2798 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2800 if(!points)
2801 return InvalidParameter;
2803 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2804 if(!pointsF)
2805 return OutOfMemory;
2807 for(i = 0; i < count; i++){
2808 pointsF[i].X = (REAL)points[i].X;
2809 pointsF[i].Y = (REAL)points[i].Y;
2812 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2813 heap_free(pointsF);
2815 return ret;
2818 /* Approximates cardinal spline with Bezier curves. */
2819 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2820 GDIPCONST GpPointF *points, INT count, REAL tension)
2822 GpPath *path;
2823 GpStatus status;
2825 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2827 if(!graphics || !pen)
2828 return InvalidParameter;
2830 if(graphics->busy)
2831 return ObjectBusy;
2833 if(count < 2)
2834 return InvalidParameter;
2836 status = GdipCreatePath(FillModeAlternate, &path);
2837 if (status != Ok) return status;
2839 status = GdipAddPathCurve2(path, points, count, tension);
2840 if (status == Ok)
2841 status = GdipDrawPath(graphics, pen, path);
2843 GdipDeletePath(path);
2844 return status;
2847 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2848 GDIPCONST GpPoint *points, INT count, REAL tension)
2850 GpPointF *pointsF;
2851 GpStatus ret;
2852 INT i;
2854 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2856 if(!points)
2857 return InvalidParameter;
2859 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2860 if(!pointsF)
2861 return OutOfMemory;
2863 for(i = 0; i < count; i++){
2864 pointsF[i].X = (REAL)points[i].X;
2865 pointsF[i].Y = (REAL)points[i].Y;
2868 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2869 heap_free(pointsF);
2871 return ret;
2874 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2875 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2876 REAL tension)
2878 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2880 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2881 return InvalidParameter;
2884 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2887 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2888 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2889 REAL tension)
2891 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2893 if(count < 0){
2894 return OutOfMemory;
2897 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2898 return InvalidParameter;
2901 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2904 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2905 REAL y, REAL width, REAL height)
2907 GpPath *path;
2908 GpStatus status;
2910 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2912 if(!graphics || !pen)
2913 return InvalidParameter;
2915 if(graphics->busy)
2916 return ObjectBusy;
2918 status = GdipCreatePath(FillModeAlternate, &path);
2919 if (status != Ok) return status;
2921 status = GdipAddPathEllipse(path, x, y, width, height);
2922 if (status == Ok)
2923 status = GdipDrawPath(graphics, pen, path);
2925 GdipDeletePath(path);
2926 return status;
2929 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2930 INT y, INT width, INT height)
2932 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2934 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2938 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2940 UINT width, height;
2942 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2944 if(!graphics || !image)
2945 return InvalidParameter;
2947 GdipGetImageWidth(image, &width);
2948 GdipGetImageHeight(image, &height);
2950 return GdipDrawImagePointRect(graphics, image, x, y,
2951 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2954 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2955 INT y)
2957 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2959 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2962 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2963 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2964 GpUnit srcUnit)
2966 GpPointF points[3];
2967 REAL scale_x, scale_y, width, height;
2969 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2971 if (!graphics || !image) return InvalidParameter;
2973 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres, graphics->printer_display);
2974 scale_x *= graphics->xres / image->xres;
2975 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres, graphics->printer_display);
2976 scale_y *= graphics->yres / image->yres;
2977 width = srcwidth * scale_x;
2978 height = srcheight * scale_y;
2980 points[0].X = points[2].X = x;
2981 points[0].Y = points[1].Y = y;
2982 points[1].X = x + width;
2983 points[2].Y = y + height;
2985 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2986 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2989 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2990 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2991 GpUnit srcUnit)
2993 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2996 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2997 GDIPCONST GpPointF *dstpoints, INT count)
2999 UINT width, height;
3001 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3003 if(!image)
3004 return InvalidParameter;
3006 GdipGetImageWidth(image, &width);
3007 GdipGetImageHeight(image, &height);
3009 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3010 width, height, UnitPixel, NULL, NULL, NULL);
3013 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3014 GDIPCONST GpPoint *dstpoints, INT count)
3016 GpPointF ptf[3];
3018 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3020 if (count != 3 || !dstpoints)
3021 return InvalidParameter;
3023 ptf[0].X = (REAL)dstpoints[0].X;
3024 ptf[0].Y = (REAL)dstpoints[0].Y;
3025 ptf[1].X = (REAL)dstpoints[1].X;
3026 ptf[1].Y = (REAL)dstpoints[1].Y;
3027 ptf[2].X = (REAL)dstpoints[2].X;
3028 ptf[2].Y = (REAL)dstpoints[2].Y;
3030 return GdipDrawImagePoints(graphics, image, ptf, count);
3033 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3034 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3036 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3037 return TRUE;
3040 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3041 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3042 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3043 DrawImageAbort callback, VOID * callbackData)
3045 GpPointF ptf[4];
3046 POINT pti[4];
3047 GpStatus stat;
3049 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3050 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3051 callbackData);
3053 if (count > 3)
3054 return NotImplemented;
3056 if(!graphics || !image || !points || count != 3)
3057 return InvalidParameter;
3059 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3060 debugstr_pointf(&points[2]));
3062 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3064 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
3065 image, points, count, srcx, srcy, srcwidth, srcheight,
3066 srcUnit, imageAttributes, callback, callbackData);
3069 memcpy(ptf, points, 3 * sizeof(GpPointF));
3071 /* Ensure source width/height is positive */
3072 if (srcwidth < 0)
3074 GpPointF tmp = ptf[1];
3075 srcx = srcx + srcwidth;
3076 srcwidth = -srcwidth;
3077 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
3078 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3079 ptf[1] = ptf[0];
3080 ptf[0] = tmp;
3083 if (srcheight < 0)
3085 GpPointF tmp = ptf[2];
3086 srcy = srcy + srcheight;
3087 srcheight = -srcheight;
3088 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
3089 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
3090 ptf[2] = ptf[0];
3091 ptf[0] = tmp;
3094 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3095 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3096 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
3097 return Ok;
3098 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
3099 round_points(pti, ptf, 4);
3101 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3102 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3104 srcx = units_to_pixels(srcx, srcUnit, image->xres, graphics->printer_display);
3105 srcy = units_to_pixels(srcy, srcUnit, image->yres, graphics->printer_display);
3106 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres, graphics->printer_display);
3107 srcheight = units_to_pixels(srcheight, srcUnit, image->yres, graphics->printer_display);
3108 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3110 if (image->type == ImageTypeBitmap)
3112 GpBitmap* bitmap = (GpBitmap*)image;
3113 BOOL do_resampling = FALSE;
3114 BOOL use_software = FALSE;
3116 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3117 graphics->xres, graphics->yres,
3118 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3119 graphics->scale, image->xres, image->yres, bitmap->format,
3120 imageAttributes ? imageAttributes->outside_color : 0);
3122 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3123 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3124 srcx < 0 || srcy < 0 ||
3125 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3126 do_resampling = TRUE;
3128 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3129 (graphics->image && graphics->image->type == ImageTypeBitmap))
3130 use_software = TRUE;
3132 if (use_software)
3134 RECT dst_area;
3135 GpRectF graphics_bounds;
3136 GpRect src_area;
3137 int i, x, y, src_stride, dst_stride;
3138 GpMatrix dst_to_src;
3139 REAL m11, m12, m21, m22, mdx, mdy;
3140 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3141 BitmapData lockeddata;
3142 InterpolationMode interpolation = graphics->interpolation;
3143 PixelOffsetMode offset_mode = graphics->pixeloffset;
3144 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3145 REAL x_dx, x_dy, y_dx, y_dy;
3146 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3148 if (!imageAttributes)
3149 imageAttributes = &defaultImageAttributes;
3151 dst_area.left = dst_area.right = pti[0].x;
3152 dst_area.top = dst_area.bottom = pti[0].y;
3153 for (i=1; i<4; i++)
3155 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3156 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3157 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3158 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3161 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3162 if (stat != Ok) return stat;
3164 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3165 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3166 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3167 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3169 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3171 if (IsRectEmpty(&dst_area)) return Ok;
3173 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3174 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3175 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3176 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3177 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3178 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3180 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3182 stat = GdipInvertMatrix(&dst_to_src);
3183 if (stat != Ok) return stat;
3185 if (do_resampling)
3187 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3188 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3190 else
3192 /* Make sure src_area is equal in size to dst_area. */
3193 src_area.X = srcx + dst_area.left - pti[0].x;
3194 src_area.Y = srcy + dst_area.top - pti[0].y;
3195 src_area.Width = dst_area.right - dst_area.left;
3196 src_area.Height = dst_area.bottom - dst_area.top;
3199 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3201 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3202 if (!src_data)
3203 return OutOfMemory;
3204 src_stride = sizeof(ARGB) * src_area.Width;
3206 /* Read the bits we need from the source bitmap into a compatible buffer. */
3207 lockeddata.Width = src_area.Width;
3208 lockeddata.Height = src_area.Height;
3209 lockeddata.Stride = src_stride;
3210 lockeddata.Scan0 = src_data;
3211 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3212 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3213 else
3214 lockeddata.PixelFormat = PixelFormat32bppARGB;
3216 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3217 lockeddata.PixelFormat, &lockeddata);
3219 if (stat == Ok)
3220 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3222 if (stat != Ok)
3224 heap_free(src_data);
3225 return stat;
3228 apply_image_attributes(imageAttributes, src_data,
3229 src_area.Width, src_area.Height,
3230 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3232 if (do_resampling)
3234 /* Transform the bits as needed to the destination. */
3235 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3236 if (!dst_data)
3238 heap_free(src_data);
3239 return OutOfMemory;
3242 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3244 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3246 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3247 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3248 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3249 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3251 for (x=dst_area.left; x<dst_area.right; x++)
3253 for (y=dst_area.top; y<dst_area.bottom; y++)
3255 GpPointF src_pointf;
3256 ARGB *dst_color;
3258 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3259 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3261 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3263 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3264 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3265 imageAttributes, interpolation, offset_mode);
3266 else
3267 *dst_color = 0;
3271 else
3273 dst_data = src_data;
3274 dst_stride = src_stride;
3277 gdi_transform_acquire(graphics);
3279 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3280 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3281 lockeddata.PixelFormat);
3283 gdi_transform_release(graphics);
3285 heap_free(src_data);
3287 heap_free(dst_dyn_data);
3289 return stat;
3291 else
3293 HDC hdc;
3294 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3295 HBITMAP hbitmap, old_hbm=NULL;
3296 HRGN hrgn;
3297 INT save_state;
3299 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3300 bitmap->format == PixelFormat24bppRGB ||
3301 bitmap->format == PixelFormat32bppRGB ||
3302 bitmap->format == PixelFormat32bppPARGB))
3304 BITMAPINFOHEADER bih;
3305 BYTE *temp_bits;
3306 PixelFormat dst_format;
3308 /* we can't draw a bitmap of this format directly */
3309 hdc = CreateCompatibleDC(0);
3310 temp_hdc = TRUE;
3311 temp_bitmap = TRUE;
3313 bih.biSize = sizeof(BITMAPINFOHEADER);
3314 bih.biWidth = bitmap->width;
3315 bih.biHeight = -bitmap->height;
3316 bih.biPlanes = 1;
3317 bih.biBitCount = 32;
3318 bih.biCompression = BI_RGB;
3319 bih.biSizeImage = 0;
3320 bih.biXPelsPerMeter = 0;
3321 bih.biYPelsPerMeter = 0;
3322 bih.biClrUsed = 0;
3323 bih.biClrImportant = 0;
3325 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3326 (void**)&temp_bits, NULL, 0);
3328 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3329 dst_format = PixelFormat32bppPARGB;
3330 else
3331 dst_format = PixelFormat32bppRGB;
3333 convert_pixels(bitmap->width, bitmap->height,
3334 bitmap->width*4, temp_bits, dst_format,
3335 bitmap->stride, bitmap->bits, bitmap->format,
3336 bitmap->image.palette);
3338 else
3340 if (bitmap->hbitmap)
3341 hbitmap = bitmap->hbitmap;
3342 else
3344 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3345 temp_bitmap = TRUE;
3348 hdc = bitmap->hdc;
3349 temp_hdc = (hdc == 0);
3352 if (temp_hdc)
3354 if (!hdc) hdc = CreateCompatibleDC(0);
3355 old_hbm = SelectObject(hdc, hbitmap);
3358 save_state = SaveDC(graphics->hdc);
3360 stat = get_clip_hrgn(graphics, &hrgn);
3362 if (stat == Ok)
3364 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3365 DeleteObject(hrgn);
3368 gdi_transform_acquire(graphics);
3370 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3372 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3373 hdc, srcx, srcy, srcwidth, srcheight);
3375 else
3377 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3378 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3381 gdi_transform_release(graphics);
3383 RestoreDC(graphics->hdc, save_state);
3385 if (temp_hdc)
3387 SelectObject(hdc, old_hbm);
3388 DeleteDC(hdc);
3391 if (temp_bitmap)
3392 DeleteObject(hbitmap);
3395 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3397 GpRectF rc;
3399 rc.X = srcx;
3400 rc.Y = srcy;
3401 rc.Width = srcwidth;
3402 rc.Height = srcheight;
3404 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3405 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3407 else
3409 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3410 return InvalidParameter;
3413 return Ok;
3416 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3417 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3418 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3419 DrawImageAbort callback, VOID * callbackData)
3421 GpPointF pointsF[3];
3422 INT i;
3424 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3425 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3426 callbackData);
3428 if(!points || count!=3)
3429 return InvalidParameter;
3431 for(i = 0; i < count; i++){
3432 pointsF[i].X = (REAL)points[i].X;
3433 pointsF[i].Y = (REAL)points[i].Y;
3436 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3437 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3438 callback, callbackData);
3441 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3442 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3443 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3444 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3445 VOID * callbackData)
3447 GpPointF points[3];
3449 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3450 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3451 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3453 points[0].X = dstx;
3454 points[0].Y = dsty;
3455 points[1].X = dstx + dstwidth;
3456 points[1].Y = dsty;
3457 points[2].X = dstx;
3458 points[2].Y = dsty + dstheight;
3460 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3461 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3464 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3465 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3466 INT srcwidth, INT srcheight, GpUnit srcUnit,
3467 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3468 VOID * callbackData)
3470 GpPointF points[3];
3472 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3473 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3474 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3476 points[0].X = dstx;
3477 points[0].Y = dsty;
3478 points[1].X = dstx + dstwidth;
3479 points[1].Y = dsty;
3480 points[2].X = dstx;
3481 points[2].Y = dsty + dstheight;
3483 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3484 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3487 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3488 REAL x, REAL y, REAL width, REAL height)
3490 RectF bounds;
3491 GpUnit unit;
3492 GpStatus ret;
3494 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3496 if(!graphics || !image)
3497 return InvalidParameter;
3499 ret = GdipGetImageBounds(image, &bounds, &unit);
3500 if(ret != Ok)
3501 return ret;
3503 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3504 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3505 unit, NULL, NULL, NULL);
3508 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3509 INT x, INT y, INT width, INT height)
3511 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3513 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3516 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3517 REAL y1, REAL x2, REAL y2)
3519 GpPointF pt[2];
3521 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3523 if (!pen)
3524 return InvalidParameter;
3526 if (pen->unit == UnitPixel && pen->width <= 0.0)
3527 return Ok;
3529 pt[0].X = x1;
3530 pt[0].Y = y1;
3531 pt[1].X = x2;
3532 pt[1].Y = y2;
3533 return GdipDrawLines(graphics, pen, pt, 2);
3536 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3537 INT y1, INT x2, INT y2)
3539 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3541 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3544 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3545 GpPointF *points, INT count)
3547 GpStatus status;
3548 GpPath *path;
3550 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3552 if(!pen || !graphics || (count < 2))
3553 return InvalidParameter;
3555 if(graphics->busy)
3556 return ObjectBusy;
3558 status = GdipCreatePath(FillModeAlternate, &path);
3559 if (status != Ok) return status;
3561 status = GdipAddPathLine2(path, points, count);
3562 if (status == Ok)
3563 status = GdipDrawPath(graphics, pen, path);
3565 GdipDeletePath(path);
3566 return status;
3569 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3570 GpPoint *points, INT count)
3572 GpStatus retval;
3573 GpPointF *ptf;
3574 int i;
3576 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3578 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3579 if(!ptf) return OutOfMemory;
3581 for(i = 0; i < count; i ++){
3582 ptf[i].X = (REAL) points[i].X;
3583 ptf[i].Y = (REAL) points[i].Y;
3586 retval = GdipDrawLines(graphics, pen, ptf, count);
3588 heap_free(ptf);
3589 return retval;
3592 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3594 INT save_state;
3595 GpStatus retval;
3596 HRGN hrgn=NULL;
3598 save_state = prepare_dc(graphics, pen);
3600 retval = get_clip_hrgn(graphics, &hrgn);
3602 if (retval != Ok)
3603 goto end;
3605 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3607 gdi_transform_acquire(graphics);
3609 retval = draw_poly(graphics, pen, path->pathdata.Points,
3610 path->pathdata.Types, path->pathdata.Count, TRUE);
3612 gdi_transform_release(graphics);
3614 end:
3615 restore_dc(graphics, save_state);
3616 DeleteObject(hrgn);
3618 return retval;
3621 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3623 GpStatus stat;
3624 GpPath* flat_path;
3625 GpMatrix* transform;
3626 GpRectF gp_bound_rect;
3627 GpRect gp_output_area;
3628 RECT output_area;
3629 INT output_height, output_width;
3630 DWORD *output_bits, *brush_bits=NULL;
3631 int i;
3632 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3633 const BYTE *dash_pattern;
3634 INT dash_pattern_size;
3635 BYTE *dyn_dash_pattern = NULL;
3637 stat = GdipClonePath(path, &flat_path);
3639 if (stat != Ok)
3640 return stat;
3642 stat = GdipCreateMatrix(&transform);
3644 if (stat == Ok)
3646 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3647 CoordinateSpaceWorld, transform);
3649 if (stat == Ok)
3650 stat = GdipFlattenPath(flat_path, transform, 1.0);
3652 GdipDeleteMatrix(transform);
3655 /* estimate the output size in pixels, can be larger than necessary */
3656 if (stat == Ok)
3658 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3659 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3660 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3661 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3663 for (i=1; i<flat_path->pathdata.Count; i++)
3665 REAL x, y;
3666 x = flat_path->pathdata.Points[i].X;
3667 y = flat_path->pathdata.Points[i].Y;
3669 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3670 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3671 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3672 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3675 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3678 if (stat == Ok)
3680 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3681 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3682 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3683 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3685 output_width = output_area.right - output_area.left + 1;
3686 output_height = output_area.bottom - output_area.top + 1;
3688 if (output_width <= 0 || output_height <= 0)
3690 GdipDeletePath(flat_path);
3691 return Ok;
3694 gp_output_area.X = output_area.left;
3695 gp_output_area.Y = output_area.top;
3696 gp_output_area.Width = output_width;
3697 gp_output_area.Height = output_height;
3699 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3700 if (!output_bits)
3701 stat = OutOfMemory;
3704 if (stat == Ok)
3706 if (pen->brush->bt != BrushTypeSolidColor)
3708 /* allocate and draw brush output */
3709 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3711 if (brush_bits)
3713 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3714 &gp_output_area, output_width);
3716 else
3717 stat = OutOfMemory;
3720 if (stat == Ok)
3722 /* convert dash pattern to bool array */
3723 switch (pen->dash)
3725 case DashStyleCustom:
3727 dash_pattern_size = 0;
3729 for (i=0; i < pen->numdashes; i++)
3730 dash_pattern_size += gdip_round(pen->dashes[i]);
3732 if (dash_pattern_size != 0)
3734 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3736 if (dyn_dash_pattern)
3738 int j=0;
3739 for (i=0; i < pen->numdashes; i++)
3741 int k;
3742 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3743 dyn_dash_pattern[j++] = (i&1)^1;
3746 else
3747 stat = OutOfMemory;
3749 break;
3751 /* else fall through */
3753 case DashStyleSolid:
3754 default:
3755 dash_pattern = static_dash_pattern;
3756 dash_pattern_size = 1;
3757 break;
3758 case DashStyleDash:
3759 dash_pattern = static_dash_pattern;
3760 dash_pattern_size = 4;
3761 break;
3762 case DashStyleDot:
3763 dash_pattern = &static_dash_pattern[4];
3764 dash_pattern_size = 2;
3765 break;
3766 case DashStyleDashDot:
3767 dash_pattern = static_dash_pattern;
3768 dash_pattern_size = 6;
3769 break;
3770 case DashStyleDashDotDot:
3771 dash_pattern = static_dash_pattern;
3772 dash_pattern_size = 8;
3773 break;
3777 if (stat == Ok)
3779 /* trace path */
3780 GpPointF subpath_start = flat_path->pathdata.Points[0];
3781 INT prev_x = INT_MAX, prev_y = INT_MAX;
3782 int dash_pos = dash_pattern_size - 1;
3784 for (i=0; i < flat_path->pathdata.Count; i++)
3786 BYTE type, type2;
3787 GpPointF start_point, end_point;
3788 GpPoint start_pointi, end_pointi;
3790 type = flat_path->pathdata.Types[i];
3791 if (i+1 < flat_path->pathdata.Count)
3792 type2 = flat_path->pathdata.Types[i+1];
3793 else
3794 type2 = PathPointTypeStart;
3796 start_point = flat_path->pathdata.Points[i];
3798 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3799 subpath_start = start_point;
3801 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3802 end_point = subpath_start;
3803 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3804 continue;
3805 else
3806 end_point = flat_path->pathdata.Points[i+1];
3808 start_pointi.X = floorf(start_point.X);
3809 start_pointi.Y = floorf(start_point.Y);
3810 end_pointi.X = floorf(end_point.X);
3811 end_pointi.Y = floorf(end_point.Y);
3813 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3814 continue;
3816 /* draw line segment */
3817 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3819 INT x, y, start_y, end_y, step;
3821 if (start_pointi.Y < end_pointi.Y)
3823 step = 1;
3824 start_y = ceilf(start_point.Y) - output_area.top;
3825 end_y = end_pointi.Y - output_area.top;
3827 else
3829 step = -1;
3830 start_y = start_point.Y - output_area.top;
3831 end_y = ceilf(end_point.Y) - output_area.top;
3834 for (y=start_y; y != (end_y+step); y+=step)
3836 x = gdip_round( start_point.X +
3837 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3838 - output_area.left;
3840 if (x == prev_x && y == prev_y)
3841 continue;
3843 prev_x = x;
3844 prev_y = y;
3845 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3847 if (!dash_pattern[dash_pos])
3848 continue;
3850 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3851 continue;
3853 if (brush_bits)
3854 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3855 else
3856 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3859 else
3861 INT x, y, start_x, end_x, step;
3863 if (start_pointi.X < end_pointi.X)
3865 step = 1;
3866 start_x = ceilf(start_point.X) - output_area.left;
3867 end_x = end_pointi.X - output_area.left;
3869 else
3871 step = -1;
3872 start_x = start_point.X - output_area.left;
3873 end_x = ceilf(end_point.X) - output_area.left;
3876 for (x=start_x; x != (end_x+step); x+=step)
3878 y = gdip_round( start_point.Y +
3879 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3880 - output_area.top;
3882 if (x == prev_x && y == prev_y)
3883 continue;
3885 prev_x = x;
3886 prev_y = y;
3887 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3889 if (!dash_pattern[dash_pos])
3890 continue;
3892 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3893 continue;
3895 if (brush_bits)
3896 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3897 else
3898 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3904 /* draw output image */
3905 if (stat == Ok)
3907 gdi_transform_acquire(graphics);
3909 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3910 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3911 PixelFormat32bppARGB);
3913 gdi_transform_release(graphics);
3916 heap_free(brush_bits);
3917 heap_free(dyn_dash_pattern);
3918 heap_free(output_bits);
3921 GdipDeletePath(flat_path);
3923 return stat;
3926 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3928 GpStatus stat;
3929 GpPath *wide_path;
3930 GpMatrix *transform=NULL;
3931 REAL flatness=1.0;
3933 /* Check if the final pen thickness in pixels is too thin. */
3934 if (pen->unit == UnitPixel)
3936 if (pen->width < 1.415)
3937 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3939 else
3941 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3943 points[1].X = pen->width;
3944 points[2].Y = pen->width;
3946 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3947 CoordinateSpaceWorld, points, 3);
3949 if (stat != Ok)
3950 return stat;
3952 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3953 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3954 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3955 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3956 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3959 stat = GdipClonePath(path, &wide_path);
3961 if (stat != Ok)
3962 return stat;
3964 if (pen->unit == UnitPixel)
3966 /* We have to transform this to device coordinates to get the widths right. */
3967 stat = GdipCreateMatrix(&transform);
3969 if (stat == Ok)
3970 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3971 CoordinateSpaceWorld, transform);
3973 else
3975 /* Set flatness based on the final coordinate space */
3976 GpMatrix t;
3978 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3979 CoordinateSpaceWorld, &t);
3981 if (stat != Ok)
3982 return stat;
3984 flatness = 1.0/sqrt(fmax(
3985 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
3986 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
3989 if (stat == Ok)
3990 stat = GdipWidenPath(wide_path, pen, transform, flatness);
3992 if (pen->unit == UnitPixel)
3994 /* Transform the path back to world coordinates */
3995 if (stat == Ok)
3996 stat = GdipInvertMatrix(transform);
3998 if (stat == Ok)
3999 stat = GdipTransformPath(wide_path, transform);
4002 /* Actually draw the path */
4003 if (stat == Ok)
4004 stat = GdipFillPath(graphics, pen->brush, wide_path);
4006 GdipDeleteMatrix(transform);
4008 GdipDeletePath(wide_path);
4010 return stat;
4013 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
4015 GpStatus retval;
4017 TRACE("(%p, %p, %p)\n", graphics, pen, path);
4019 if(!pen || !graphics)
4020 return InvalidParameter;
4022 if(graphics->busy)
4023 return ObjectBusy;
4025 if (path->pathdata.Count == 0)
4026 return Ok;
4028 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4029 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
4030 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
4031 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
4032 else
4033 retval = GDI32_GdipDrawPath(graphics, pen, path);
4035 return retval;
4038 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
4039 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4041 GpStatus status;
4042 GpPath *path;
4044 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
4045 width, height, startAngle, sweepAngle);
4047 if(!graphics || !pen)
4048 return InvalidParameter;
4050 if(graphics->busy)
4051 return ObjectBusy;
4053 status = GdipCreatePath(FillModeAlternate, &path);
4054 if (status != Ok) return status;
4056 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4057 if (status == Ok)
4058 status = GdipDrawPath(graphics, pen, path);
4060 GdipDeletePath(path);
4061 return status;
4064 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
4065 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4067 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
4068 width, height, startAngle, sweepAngle);
4070 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4073 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
4074 REAL y, REAL width, REAL height)
4076 GpStatus status;
4077 GpPath *path;
4079 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
4081 if(!pen || !graphics)
4082 return InvalidParameter;
4084 if(graphics->busy)
4085 return ObjectBusy;
4087 status = GdipCreatePath(FillModeAlternate, &path);
4088 if (status != Ok) return status;
4090 status = GdipAddPathRectangle(path, x, y, width, height);
4091 if (status == Ok)
4092 status = GdipDrawPath(graphics, pen, path);
4094 GdipDeletePath(path);
4095 return status;
4098 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
4099 INT y, INT width, INT height)
4101 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
4103 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4106 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
4107 GDIPCONST GpRectF* rects, INT count)
4109 GpStatus status;
4110 GpPath *path;
4112 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4114 if(!graphics || !pen || !rects || count < 1)
4115 return InvalidParameter;
4117 if(graphics->busy)
4118 return ObjectBusy;
4120 status = GdipCreatePath(FillModeAlternate, &path);
4121 if (status != Ok) return status;
4123 status = GdipAddPathRectangles(path, rects, count);
4124 if (status == Ok)
4125 status = GdipDrawPath(graphics, pen, path);
4127 GdipDeletePath(path);
4128 return status;
4131 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4132 GDIPCONST GpRect* rects, INT count)
4134 GpRectF *rectsF;
4135 GpStatus ret;
4136 INT i;
4138 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4140 if(!rects || count<=0)
4141 return InvalidParameter;
4143 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4144 if(!rectsF)
4145 return OutOfMemory;
4147 for(i = 0;i < count;i++){
4148 rectsF[i].X = (REAL)rects[i].X;
4149 rectsF[i].Y = (REAL)rects[i].Y;
4150 rectsF[i].Width = (REAL)rects[i].Width;
4151 rectsF[i].Height = (REAL)rects[i].Height;
4154 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4155 heap_free(rectsF);
4157 return ret;
4160 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4161 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4163 GpPath *path;
4164 GpStatus status;
4166 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4167 count, tension, fill);
4169 if(!graphics || !brush || !points)
4170 return InvalidParameter;
4172 if(graphics->busy)
4173 return ObjectBusy;
4175 if(count == 1) /* Do nothing */
4176 return Ok;
4178 status = GdipCreatePath(fill, &path);
4179 if (status != Ok) return status;
4181 status = GdipAddPathClosedCurve2(path, points, count, tension);
4182 if (status == Ok)
4183 status = GdipFillPath(graphics, brush, path);
4185 GdipDeletePath(path);
4186 return status;
4189 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4190 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4192 GpPointF *ptf;
4193 GpStatus stat;
4194 INT i;
4196 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4197 count, tension, fill);
4199 if(!points || count == 0)
4200 return InvalidParameter;
4202 if(count == 1) /* Do nothing */
4203 return Ok;
4205 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4206 if(!ptf)
4207 return OutOfMemory;
4209 for(i = 0;i < count;i++){
4210 ptf[i].X = (REAL)points[i].X;
4211 ptf[i].Y = (REAL)points[i].Y;
4214 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4216 heap_free(ptf);
4218 return stat;
4221 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4222 GDIPCONST GpPointF *points, INT count)
4224 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4225 return GdipFillClosedCurve2(graphics, brush, points, count,
4226 0.5f, FillModeAlternate);
4229 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4230 GDIPCONST GpPoint *points, INT count)
4232 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4233 return GdipFillClosedCurve2I(graphics, brush, points, count,
4234 0.5f, FillModeAlternate);
4237 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4238 REAL y, REAL width, REAL height)
4240 GpStatus stat;
4241 GpPath *path;
4243 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4245 if(!graphics || !brush)
4246 return InvalidParameter;
4248 if(graphics->busy)
4249 return ObjectBusy;
4251 stat = GdipCreatePath(FillModeAlternate, &path);
4253 if (stat == Ok)
4255 stat = GdipAddPathEllipse(path, x, y, width, height);
4257 if (stat == Ok)
4258 stat = GdipFillPath(graphics, brush, path);
4260 GdipDeletePath(path);
4263 return stat;
4266 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4267 INT y, INT width, INT height)
4269 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4271 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4274 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4276 INT save_state;
4277 GpStatus retval;
4278 HRGN hrgn=NULL;
4280 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4281 return NotImplemented;
4283 save_state = SaveDC(graphics->hdc);
4284 EndPath(graphics->hdc);
4285 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4286 : WINDING));
4288 retval = get_clip_hrgn(graphics, &hrgn);
4290 if (retval != Ok)
4291 goto end;
4293 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4295 gdi_transform_acquire(graphics);
4297 BeginPath(graphics->hdc);
4298 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4299 path->pathdata.Types, path->pathdata.Count, FALSE);
4301 if(retval == Ok)
4303 EndPath(graphics->hdc);
4304 retval = brush_fill_path(graphics, brush);
4307 gdi_transform_release(graphics);
4309 end:
4310 RestoreDC(graphics->hdc, save_state);
4311 DeleteObject(hrgn);
4313 return retval;
4316 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4318 GpStatus stat;
4319 GpRegion *rgn;
4321 if (!brush_can_fill_pixels(brush))
4322 return NotImplemented;
4324 /* FIXME: This could probably be done more efficiently without regions. */
4326 stat = GdipCreateRegionPath(path, &rgn);
4328 if (stat == Ok)
4330 stat = GdipFillRegion(graphics, brush, rgn);
4332 GdipDeleteRegion(rgn);
4335 return stat;
4338 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4340 GpStatus stat = NotImplemented;
4342 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4344 if(!brush || !graphics || !path)
4345 return InvalidParameter;
4347 if(graphics->busy)
4348 return ObjectBusy;
4350 if (!path->pathdata.Count)
4351 return Ok;
4353 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4354 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4356 if (!graphics->image && !graphics->alpha_hdc)
4357 stat = GDI32_GdipFillPath(graphics, brush, path);
4359 if (stat == NotImplemented)
4360 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4362 if (stat == NotImplemented)
4364 FIXME("Not implemented for brushtype %i\n", brush->bt);
4365 stat = Ok;
4368 return stat;
4371 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4372 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4374 GpStatus stat;
4375 GpPath *path;
4377 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4378 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4380 if(!graphics || !brush)
4381 return InvalidParameter;
4383 if(graphics->busy)
4384 return ObjectBusy;
4386 stat = GdipCreatePath(FillModeAlternate, &path);
4388 if (stat == Ok)
4390 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4392 if (stat == Ok)
4393 stat = GdipFillPath(graphics, brush, path);
4395 GdipDeletePath(path);
4398 return stat;
4401 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4402 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4404 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4405 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4407 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4410 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4411 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4413 GpStatus stat;
4414 GpPath *path;
4416 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4418 if(!graphics || !brush || !points || !count)
4419 return InvalidParameter;
4421 if(graphics->busy)
4422 return ObjectBusy;
4424 stat = GdipCreatePath(fillMode, &path);
4426 if (stat == Ok)
4428 stat = GdipAddPathPolygon(path, points, count);
4430 if (stat == Ok)
4431 stat = GdipFillPath(graphics, brush, path);
4433 GdipDeletePath(path);
4436 return stat;
4439 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4440 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4442 GpStatus stat;
4443 GpPath *path;
4445 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4447 if(!graphics || !brush || !points || !count)
4448 return InvalidParameter;
4450 if(graphics->busy)
4451 return ObjectBusy;
4453 stat = GdipCreatePath(fillMode, &path);
4455 if (stat == Ok)
4457 stat = GdipAddPathPolygonI(path, points, count);
4459 if (stat == Ok)
4460 stat = GdipFillPath(graphics, brush, path);
4462 GdipDeletePath(path);
4465 return stat;
4468 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4469 GDIPCONST GpPointF *points, INT count)
4471 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4473 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4476 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4477 GDIPCONST GpPoint *points, INT count)
4479 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4481 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4484 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4485 REAL x, REAL y, REAL width, REAL height)
4487 GpRectF rect;
4489 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4491 rect.X = x;
4492 rect.Y = y;
4493 rect.Width = width;
4494 rect.Height = height;
4496 return GdipFillRectangles(graphics, brush, &rect, 1);
4499 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4500 INT x, INT y, INT width, INT height)
4502 GpRectF rect;
4504 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4506 rect.X = (REAL)x;
4507 rect.Y = (REAL)y;
4508 rect.Width = (REAL)width;
4509 rect.Height = (REAL)height;
4511 return GdipFillRectangles(graphics, brush, &rect, 1);
4514 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4515 INT count)
4517 GpStatus status;
4518 GpPath *path;
4520 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4522 if(!graphics || !brush || !rects || count <= 0)
4523 return InvalidParameter;
4525 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4527 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4528 /* FIXME: Add gdi32 drawing. */
4529 return status;
4532 status = GdipCreatePath(FillModeAlternate, &path);
4533 if (status != Ok) return status;
4535 status = GdipAddPathRectangles(path, rects, count);
4536 if (status == Ok)
4537 status = GdipFillPath(graphics, brush, path);
4539 GdipDeletePath(path);
4540 return status;
4543 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4544 INT count)
4546 GpRectF *rectsF;
4547 GpStatus ret;
4548 INT i;
4550 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4552 if(!rects || count <= 0)
4553 return InvalidParameter;
4555 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4556 if(!rectsF)
4557 return OutOfMemory;
4559 for(i = 0; i < count; i++){
4560 rectsF[i].X = (REAL)rects[i].X;
4561 rectsF[i].Y = (REAL)rects[i].Y;
4562 rectsF[i].Width = (REAL)rects[i].Width;
4563 rectsF[i].Height = (REAL)rects[i].Height;
4566 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4567 heap_free(rectsF);
4569 return ret;
4572 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4573 GpRegion* region)
4575 INT save_state;
4576 GpStatus status;
4577 HRGN hrgn;
4578 RECT rc;
4580 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4581 return NotImplemented;
4583 save_state = SaveDC(graphics->hdc);
4584 EndPath(graphics->hdc);
4586 hrgn = NULL;
4587 status = get_clip_hrgn(graphics, &hrgn);
4588 if (status != Ok)
4590 RestoreDC(graphics->hdc, save_state);
4591 return status;
4594 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4595 DeleteObject(hrgn);
4597 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4598 if (status != Ok)
4600 RestoreDC(graphics->hdc, save_state);
4601 return status;
4604 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4605 DeleteObject(hrgn);
4607 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4609 BeginPath(graphics->hdc);
4610 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4611 EndPath(graphics->hdc);
4613 status = brush_fill_path(graphics, brush);
4616 RestoreDC(graphics->hdc, save_state);
4619 return status;
4622 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4623 GpRegion* region)
4625 GpStatus stat;
4626 GpRegion *temp_region;
4627 GpMatrix world_to_device;
4628 GpRectF graphics_bounds;
4629 DWORD *pixel_data;
4630 HRGN hregion;
4631 RECT bound_rect;
4632 GpRect gp_bound_rect;
4634 if (!brush_can_fill_pixels(brush))
4635 return NotImplemented;
4637 stat = gdi_transform_acquire(graphics);
4639 if (stat == Ok)
4640 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4642 if (stat == Ok)
4643 stat = GdipCloneRegion(region, &temp_region);
4645 if (stat == Ok)
4647 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4648 CoordinateSpaceWorld, &world_to_device);
4650 if (stat == Ok)
4651 stat = GdipTransformRegion(temp_region, &world_to_device);
4653 if (stat == Ok)
4654 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4656 if (stat == Ok)
4657 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4659 GdipDeleteRegion(temp_region);
4662 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4664 DeleteObject(hregion);
4665 gdi_transform_release(graphics);
4666 return Ok;
4669 if (stat == Ok)
4671 gp_bound_rect.X = bound_rect.left;
4672 gp_bound_rect.Y = bound_rect.top;
4673 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4674 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4676 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4677 if (!pixel_data)
4678 stat = OutOfMemory;
4680 if (stat == Ok)
4682 stat = brush_fill_pixels(graphics, brush, pixel_data,
4683 &gp_bound_rect, gp_bound_rect.Width);
4685 if (stat == Ok)
4686 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4687 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4688 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4689 PixelFormat32bppARGB);
4691 heap_free(pixel_data);
4694 DeleteObject(hregion);
4697 gdi_transform_release(graphics);
4699 return stat;
4702 /*****************************************************************************
4703 * GdipFillRegion [GDIPLUS.@]
4705 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4706 GpRegion* region)
4708 GpStatus stat = NotImplemented;
4710 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4712 if (!(graphics && brush && region))
4713 return InvalidParameter;
4715 if(graphics->busy)
4716 return ObjectBusy;
4718 if (graphics->image && graphics->image->type == ImageTypeMetafile)
4719 stat = METAFILE_FillRegion((GpMetafile*)graphics->image, brush, region);
4720 else
4722 if (!graphics->image && !graphics->alpha_hdc)
4723 stat = GDI32_GdipFillRegion(graphics, brush, region);
4725 if (stat == NotImplemented)
4726 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4729 if (stat == NotImplemented)
4731 FIXME("not implemented for brushtype %i\n", brush->bt);
4732 stat = Ok;
4735 return stat;
4738 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4740 TRACE("(%p,%u)\n", graphics, intention);
4742 if(!graphics)
4743 return InvalidParameter;
4745 if(graphics->busy)
4746 return ObjectBusy;
4748 /* We have no internal operation queue, so there's no need to clear it. */
4750 if (graphics->hdc)
4751 GdiFlush();
4753 return Ok;
4756 /*****************************************************************************
4757 * GdipGetClipBounds [GDIPLUS.@]
4759 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4761 GpStatus status;
4762 GpRegion *clip;
4764 TRACE("(%p, %p)\n", graphics, rect);
4766 if(!graphics)
4767 return InvalidParameter;
4769 if(graphics->busy)
4770 return ObjectBusy;
4772 status = GdipCreateRegion(&clip);
4773 if (status != Ok) return status;
4775 status = GdipGetClip(graphics, clip);
4776 if (status == Ok)
4777 status = GdipGetRegionBounds(clip, graphics, rect);
4779 GdipDeleteRegion(clip);
4780 return status;
4783 /*****************************************************************************
4784 * GdipGetClipBoundsI [GDIPLUS.@]
4786 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4788 GpRectF rectf;
4789 GpStatus stat;
4791 TRACE("(%p, %p)\n", graphics, rect);
4793 if (!rect)
4794 return InvalidParameter;
4796 if ((stat = GdipGetClipBounds(graphics, &rectf)) == Ok)
4798 rect->X = gdip_round(rectf.X);
4799 rect->Y = gdip_round(rectf.Y);
4800 rect->Width = gdip_round(rectf.Width);
4801 rect->Height = gdip_round(rectf.Height);
4804 return stat;
4807 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4808 CompositingMode *mode)
4810 TRACE("(%p, %p)\n", graphics, mode);
4812 if(!graphics || !mode)
4813 return InvalidParameter;
4815 if(graphics->busy)
4816 return ObjectBusy;
4818 *mode = graphics->compmode;
4820 return Ok;
4823 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4824 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4825 CompositingQuality *quality)
4827 TRACE("(%p, %p)\n", graphics, quality);
4829 if(!graphics || !quality)
4830 return InvalidParameter;
4832 if(graphics->busy)
4833 return ObjectBusy;
4835 *quality = graphics->compqual;
4837 return Ok;
4840 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4841 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4842 InterpolationMode *mode)
4844 TRACE("(%p, %p)\n", graphics, mode);
4846 if(!graphics || !mode)
4847 return InvalidParameter;
4849 if(graphics->busy)
4850 return ObjectBusy;
4852 *mode = graphics->interpolation;
4854 return Ok;
4857 /* FIXME: Need to handle color depths less than 24bpp */
4858 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4860 TRACE("(%p, %p)\n", graphics, argb);
4862 if(!graphics || !argb)
4863 return InvalidParameter;
4865 if(graphics->busy)
4866 return ObjectBusy;
4868 if (graphics->image && graphics->image->type == ImageTypeBitmap)
4870 static int once;
4871 GpBitmap *bitmap = (GpBitmap *)graphics->image;
4872 if (IsIndexedPixelFormat(bitmap->format) && !once++)
4873 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4876 return Ok;
4879 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4881 TRACE("(%p, %p)\n", graphics, scale);
4883 if(!graphics || !scale)
4884 return InvalidParameter;
4886 if(graphics->busy)
4887 return ObjectBusy;
4889 *scale = graphics->scale;
4891 return Ok;
4894 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4896 TRACE("(%p, %p)\n", graphics, unit);
4898 if(!graphics || !unit)
4899 return InvalidParameter;
4901 if(graphics->busy)
4902 return ObjectBusy;
4904 *unit = graphics->unit;
4906 return Ok;
4909 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4910 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4911 *mode)
4913 TRACE("(%p, %p)\n", graphics, mode);
4915 if(!graphics || !mode)
4916 return InvalidParameter;
4918 if(graphics->busy)
4919 return ObjectBusy;
4921 *mode = graphics->pixeloffset;
4923 return Ok;
4926 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4927 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4929 TRACE("(%p, %p)\n", graphics, mode);
4931 if(!graphics || !mode)
4932 return InvalidParameter;
4934 if(graphics->busy)
4935 return ObjectBusy;
4937 *mode = graphics->smoothing;
4939 return Ok;
4942 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4944 TRACE("(%p, %p)\n", graphics, contrast);
4946 if(!graphics || !contrast)
4947 return InvalidParameter;
4949 *contrast = graphics->textcontrast;
4951 return Ok;
4954 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4955 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4956 TextRenderingHint *hint)
4958 TRACE("(%p, %p)\n", graphics, hint);
4960 if(!graphics || !hint)
4961 return InvalidParameter;
4963 if(graphics->busy)
4964 return ObjectBusy;
4966 *hint = graphics->texthint;
4968 return Ok;
4971 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4973 GpRegion *clip_rgn;
4974 GpStatus stat;
4975 GpMatrix device_to_world;
4977 TRACE("(%p, %p)\n", graphics, rect);
4979 if(!graphics || !rect)
4980 return InvalidParameter;
4982 if(graphics->busy)
4983 return ObjectBusy;
4985 /* intersect window and graphics clipping regions */
4986 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4987 return stat;
4989 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4990 goto cleanup;
4992 /* transform to world coordinates */
4993 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4994 goto cleanup;
4996 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4997 goto cleanup;
4999 /* get bounds of the region */
5000 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
5002 cleanup:
5003 GdipDeleteRegion(clip_rgn);
5005 return stat;
5008 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
5010 GpRectF rectf;
5011 GpStatus stat;
5013 TRACE("(%p, %p)\n", graphics, rect);
5015 if(!graphics || !rect)
5016 return InvalidParameter;
5018 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
5020 rect->X = gdip_round(rectf.X);
5021 rect->Y = gdip_round(rectf.Y);
5022 rect->Width = gdip_round(rectf.Width);
5023 rect->Height = gdip_round(rectf.Height);
5026 return stat;
5029 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5031 TRACE("(%p, %p)\n", graphics, matrix);
5033 if(!graphics || !matrix)
5034 return InvalidParameter;
5036 if(graphics->busy)
5037 return ObjectBusy;
5039 *matrix = graphics->worldtrans;
5040 return Ok;
5043 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
5045 GpSolidFill *brush;
5046 GpStatus stat;
5047 GpRectF wnd_rect;
5048 CompositingMode prev_comp_mode;
5050 TRACE("(%p, %x)\n", graphics, color);
5052 if(!graphics)
5053 return InvalidParameter;
5055 if(graphics->busy)
5056 return ObjectBusy;
5058 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5059 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
5061 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
5062 return stat;
5064 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
5065 GdipDeleteBrush((GpBrush*)brush);
5066 return stat;
5069 GdipGetCompositingMode(graphics, &prev_comp_mode);
5070 GdipSetCompositingMode(graphics, CompositingModeSourceCopy);
5071 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
5072 wnd_rect.Width, wnd_rect.Height);
5073 GdipSetCompositingMode(graphics, prev_comp_mode);
5075 GdipDeleteBrush((GpBrush*)brush);
5077 return Ok;
5080 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
5082 TRACE("(%p, %p)\n", graphics, res);
5084 if(!graphics || !res)
5085 return InvalidParameter;
5087 return GdipIsEmptyRegion(graphics->clip, graphics, res);
5090 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
5092 GpStatus stat;
5093 GpRegion* rgn;
5094 GpPointF pt;
5096 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
5098 if(!graphics || !result)
5099 return InvalidParameter;
5101 if(graphics->busy)
5102 return ObjectBusy;
5104 pt.X = x;
5105 pt.Y = y;
5106 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5107 CoordinateSpaceWorld, &pt, 1)) != Ok)
5108 return stat;
5110 if((stat = GdipCreateRegion(&rgn)) != Ok)
5111 return stat;
5113 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5114 goto cleanup;
5116 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
5118 cleanup:
5119 GdipDeleteRegion(rgn);
5120 return stat;
5123 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
5125 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
5128 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
5130 GpStatus stat;
5131 GpRegion* rgn;
5132 GpPointF pts[2];
5134 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
5136 if(!graphics || !result)
5137 return InvalidParameter;
5139 if(graphics->busy)
5140 return ObjectBusy;
5142 pts[0].X = x;
5143 pts[0].Y = y;
5144 pts[1].X = x + width;
5145 pts[1].Y = y + height;
5147 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5148 CoordinateSpaceWorld, pts, 2)) != Ok)
5149 return stat;
5151 pts[1].X -= pts[0].X;
5152 pts[1].Y -= pts[0].Y;
5154 if((stat = GdipCreateRegion(&rgn)) != Ok)
5155 return stat;
5157 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5158 goto cleanup;
5160 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5162 cleanup:
5163 GdipDeleteRegion(rgn);
5164 return stat;
5167 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5169 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5172 GpStatus gdip_format_string(HDC hdc,
5173 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5174 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5175 gdip_format_string_callback callback, void *user_data)
5177 WCHAR* stringdup;
5178 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5179 nheight, lineend, lineno = 0;
5180 RectF bounds;
5181 StringAlignment halign;
5182 GpStatus stat = Ok;
5183 SIZE size;
5184 HotkeyPrefix hkprefix;
5185 INT *hotkeyprefix_offsets=NULL;
5186 INT hotkeyprefix_count=0;
5187 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5188 BOOL seen_prefix = FALSE;
5190 if(length == -1) length = lstrlenW(string);
5192 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5193 if(!stringdup) return OutOfMemory;
5195 if (!format)
5196 format = &default_drawstring_format;
5198 nwidth = rect->Width;
5199 nheight = rect->Height;
5200 if (ignore_empty_clip)
5202 if (!nwidth) nwidth = INT_MAX;
5203 if (!nheight) nheight = INT_MAX;
5206 hkprefix = format->hkprefix;
5208 if (hkprefix == HotkeyPrefixShow)
5210 for (i=0; i<length; i++)
5212 if (string[i] == '&')
5213 hotkeyprefix_count++;
5217 if (hotkeyprefix_count)
5219 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5220 if (!hotkeyprefix_offsets)
5222 heap_free(stringdup);
5223 return OutOfMemory;
5227 hotkeyprefix_count = 0;
5229 for(i = 0, j = 0; i < length; i++){
5230 /* FIXME: This makes the indexes passed to callback inaccurate. */
5231 if(!iswprint(string[i]) && (string[i] != '\n'))
5232 continue;
5234 /* FIXME: tabs should be handled using tabstops from stringformat */
5235 if (string[i] == '\t')
5236 continue;
5238 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5239 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5240 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5242 seen_prefix = TRUE;
5243 continue;
5246 seen_prefix = FALSE;
5248 stringdup[j] = string[i];
5249 j++;
5252 length = j;
5254 halign = format->align;
5256 while(sum < length){
5257 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5258 nwidth, &fit, NULL, &size);
5259 fitcpy = fit;
5261 if(fit == 0)
5262 break;
5264 for(lret = 0; lret < fit; lret++)
5265 if(*(stringdup + sum + lret) == '\n')
5266 break;
5268 /* Line break code (may look strange, but it imitates windows). */
5269 if(lret < fit)
5270 lineend = fit = lret; /* this is not an off-by-one error */
5271 else if(fit < (length - sum)){
5272 if(*(stringdup + sum + fit) == ' ')
5273 while(*(stringdup + sum + fit) == ' ')
5274 fit++;
5275 else if (!(format->attr & StringFormatFlagsNoWrap))
5276 while(*(stringdup + sum + fit - 1) != ' '){
5277 fit--;
5279 if(*(stringdup + sum + fit) == '\t')
5280 break;
5282 if(fit == 0){
5283 fit = fitcpy;
5284 break;
5287 lineend = fit;
5288 while(*(stringdup + sum + lineend - 1) == ' ' ||
5289 *(stringdup + sum + lineend - 1) == '\t')
5290 lineend--;
5292 else
5293 lineend = fit;
5295 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5296 nwidth, &j, NULL, &size);
5298 bounds.Width = size.cx;
5300 if(height + size.cy > nheight)
5302 if (format->attr & StringFormatFlagsLineLimit)
5303 break;
5304 bounds.Height = nheight - (height + size.cy);
5306 else
5307 bounds.Height = size.cy;
5309 bounds.Y = rect->Y + height;
5311 switch (halign)
5313 case StringAlignmentNear:
5314 default:
5315 bounds.X = rect->X;
5316 break;
5317 case StringAlignmentCenter:
5318 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5319 break;
5320 case StringAlignmentFar:
5321 bounds.X = rect->X + rect->Width - bounds.Width;
5322 break;
5325 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5326 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5327 break;
5329 stat = callback(hdc, stringdup, sum, lineend,
5330 font, rect, format, lineno, &bounds,
5331 &hotkeyprefix_offsets[hotkeyprefix_pos],
5332 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5334 if (stat != Ok)
5335 break;
5337 sum += fit + (lret < fitcpy ? 1 : 0);
5338 height += size.cy;
5339 lineno++;
5341 hotkeyprefix_pos = hotkeyprefix_end_pos;
5343 if(height > nheight)
5344 break;
5346 /* Stop if this was a linewrap (but not if it was a linebreak). */
5347 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5348 break;
5351 heap_free(stringdup);
5352 heap_free(hotkeyprefix_offsets);
5354 return stat;
5357 struct measure_ranges_args {
5358 GpRegion **regions;
5359 REAL rel_width, rel_height;
5362 static GpStatus measure_ranges_callback(HDC hdc,
5363 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5364 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5365 INT lineno, const RectF *bounds, INT *underlined_indexes,
5366 INT underlined_index_count, void *user_data)
5368 int i;
5369 GpStatus stat = Ok;
5370 struct measure_ranges_args *args = user_data;
5372 for (i=0; i<format->range_count; i++)
5374 INT range_start = max(index, format->character_ranges[i].First);
5375 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5376 if (range_start < range_end)
5378 GpRectF range_rect;
5379 SIZE range_size;
5381 range_rect.Y = bounds->Y / args->rel_height;
5382 range_rect.Height = bounds->Height / args->rel_height;
5384 GetTextExtentExPointW(hdc, string + index, range_start - index,
5385 INT_MAX, NULL, NULL, &range_size);
5386 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5388 GetTextExtentExPointW(hdc, string + index, range_end - index,
5389 INT_MAX, NULL, NULL, &range_size);
5390 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5392 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5393 if (stat != Ok)
5394 break;
5398 return stat;
5401 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5402 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5403 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5404 INT regionCount, GpRegion** regions)
5406 GpStatus stat;
5407 int i;
5408 HFONT gdifont, oldfont;
5409 struct measure_ranges_args args;
5410 HDC hdc, temp_hdc=NULL;
5411 GpPointF pt[3];
5412 RectF scaled_rect;
5413 REAL margin_x;
5415 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_wn(string, length),
5416 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5418 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5419 return InvalidParameter;
5421 if (regionCount < stringFormat->range_count)
5422 return InvalidParameter;
5424 if(!graphics->hdc)
5426 hdc = temp_hdc = CreateCompatibleDC(0);
5427 if (!temp_hdc) return OutOfMemory;
5429 else
5430 hdc = graphics->hdc;
5432 if (stringFormat->attr)
5433 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5435 pt[0].X = 0.0;
5436 pt[0].Y = 0.0;
5437 pt[1].X = 1.0;
5438 pt[1].Y = 0.0;
5439 pt[2].X = 0.0;
5440 pt[2].Y = 1.0;
5441 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5442 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5443 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5444 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5445 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5447 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5448 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5450 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5451 scaled_rect.Y = layoutRect->Y * args.rel_height;
5452 scaled_rect.Width = layoutRect->Width * args.rel_width;
5453 scaled_rect.Height = layoutRect->Height * args.rel_height;
5455 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5456 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5458 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL, NULL);
5459 oldfont = SelectObject(hdc, gdifont);
5461 for (i=0; i<stringFormat->range_count; i++)
5463 stat = GdipSetEmpty(regions[i]);
5464 if (stat != Ok)
5466 SelectObject(hdc, oldfont);
5467 DeleteObject(gdifont);
5468 if (temp_hdc)
5469 DeleteDC(temp_hdc);
5470 return stat;
5474 args.regions = regions;
5476 gdi_transform_acquire(graphics);
5478 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5479 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5481 gdi_transform_release(graphics);
5483 SelectObject(hdc, oldfont);
5484 DeleteObject(gdifont);
5486 if (temp_hdc)
5487 DeleteDC(temp_hdc);
5489 return stat;
5492 struct measure_string_args {
5493 RectF *bounds;
5494 INT *codepointsfitted;
5495 INT *linesfilled;
5496 REAL rel_width, rel_height;
5499 static GpStatus measure_string_callback(HDC hdc,
5500 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5501 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5502 INT lineno, const RectF *bounds, INT *underlined_indexes,
5503 INT underlined_index_count, void *user_data)
5505 struct measure_string_args *args = user_data;
5506 REAL new_width, new_height;
5508 new_width = bounds->Width / args->rel_width;
5509 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5511 if (new_width > args->bounds->Width)
5512 args->bounds->Width = new_width;
5514 if (new_height > args->bounds->Height)
5515 args->bounds->Height = new_height;
5517 if (args->codepointsfitted)
5518 *args->codepointsfitted = index + length;
5520 if (args->linesfilled)
5521 (*args->linesfilled)++;
5523 return Ok;
5526 /* Find the smallest rectangle that bounds the text when it is printed in rect
5527 * according to the format options listed in format. If rect has 0 width and
5528 * height, then just find the smallest rectangle that bounds the text when it's
5529 * printed at location (rect->X, rect-Y). */
5530 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5531 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5532 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5533 INT *codepointsfitted, INT *linesfilled)
5535 HFONT oldfont, gdifont;
5536 struct measure_string_args args;
5537 HDC temp_hdc=NULL, hdc;
5538 GpPointF pt[3];
5539 RectF scaled_rect;
5540 REAL margin_x;
5541 INT lines, glyphs;
5543 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5544 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5545 bounds, codepointsfitted, linesfilled);
5547 if(!graphics || !string || !font || !rect || !bounds)
5548 return InvalidParameter;
5550 if(!graphics->hdc)
5552 hdc = temp_hdc = CreateCompatibleDC(0);
5553 if (!temp_hdc) return OutOfMemory;
5555 else
5556 hdc = graphics->hdc;
5558 if(linesfilled) *linesfilled = 0;
5559 if(codepointsfitted) *codepointsfitted = 0;
5561 if(format)
5562 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5564 pt[0].X = 0.0;
5565 pt[0].Y = 0.0;
5566 pt[1].X = 1.0;
5567 pt[1].Y = 0.0;
5568 pt[2].X = 0.0;
5569 pt[2].Y = 1.0;
5570 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5571 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5572 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5573 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5574 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5576 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5577 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5579 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5580 scaled_rect.Y = rect->Y * args.rel_height;
5581 scaled_rect.Width = rect->Width * args.rel_width;
5582 scaled_rect.Height = rect->Height * args.rel_height;
5583 if (scaled_rect.Width >= 0.5)
5585 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5586 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5589 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5590 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5592 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5593 oldfont = SelectObject(hdc, gdifont);
5595 bounds->X = rect->X;
5596 bounds->Y = rect->Y;
5597 bounds->Width = 0.0;
5598 bounds->Height = 0.0;
5600 args.bounds = bounds;
5601 args.codepointsfitted = &glyphs;
5602 args.linesfilled = &lines;
5603 lines = glyphs = 0;
5605 gdi_transform_acquire(graphics);
5607 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5608 measure_string_callback, &args);
5610 gdi_transform_release(graphics);
5612 if (linesfilled) *linesfilled = lines;
5613 if (codepointsfitted) *codepointsfitted = glyphs;
5615 if (lines)
5616 bounds->Width += margin_x * 2.0;
5618 SelectObject(hdc, oldfont);
5619 DeleteObject(gdifont);
5621 if (temp_hdc)
5622 DeleteDC(temp_hdc);
5624 return Ok;
5627 struct draw_string_args {
5628 GpGraphics *graphics;
5629 GDIPCONST GpBrush *brush;
5630 REAL x, y, rel_width, rel_height, ascent;
5633 static GpStatus draw_string_callback(HDC hdc,
5634 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5635 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5636 INT lineno, const RectF *bounds, INT *underlined_indexes,
5637 INT underlined_index_count, void *user_data)
5639 struct draw_string_args *args = user_data;
5640 PointF position;
5641 GpStatus stat;
5643 position.X = args->x + bounds->X / args->rel_width;
5644 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5646 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5647 args->brush, &position,
5648 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5650 if (stat == Ok && underlined_index_count)
5652 OUTLINETEXTMETRICW otm;
5653 REAL underline_y, underline_height;
5654 int i;
5656 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5658 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5659 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5661 for (i=0; i<underlined_index_count; i++)
5663 REAL start_x, end_x;
5664 SIZE text_size;
5665 INT ofs = underlined_indexes[i] - index;
5667 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5668 start_x = text_size.cx / args->rel_width;
5670 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5671 end_x = text_size.cx / args->rel_width;
5673 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5677 return stat;
5680 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5681 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5682 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5684 HRGN rgn = NULL;
5685 HFONT gdifont;
5686 GpPointF pt[3], rectcpy[4];
5687 POINT corners[4];
5688 REAL rel_width, rel_height, margin_x;
5689 INT save_state, format_flags = 0;
5690 REAL offsety = 0.0;
5691 struct draw_string_args args;
5692 RectF scaled_rect;
5693 HDC hdc, temp_hdc=NULL;
5694 TEXTMETRICW textmetric;
5696 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5697 length, font, debugstr_rectf(rect), format, brush);
5699 if(!graphics || !string || !font || !brush || !rect)
5700 return InvalidParameter;
5702 if(graphics->hdc)
5704 hdc = graphics->hdc;
5706 else
5708 hdc = temp_hdc = CreateCompatibleDC(0);
5711 if(format){
5712 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5714 format_flags = format->attr;
5716 /* Should be no need to explicitly test for StringAlignmentNear as
5717 * that is default behavior if no alignment is passed. */
5718 if(format->line_align != StringAlignmentNear){
5719 RectF bounds, in_rect = *rect;
5720 in_rect.Height = 0.0; /* avoid height clipping */
5721 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5723 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5725 if(format->line_align == StringAlignmentCenter)
5726 offsety = (rect->Height - bounds.Height) / 2;
5727 else if(format->line_align == StringAlignmentFar)
5728 offsety = (rect->Height - bounds.Height);
5730 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5733 save_state = SaveDC(hdc);
5735 pt[0].X = 0.0;
5736 pt[0].Y = 0.0;
5737 pt[1].X = 1.0;
5738 pt[1].Y = 0.0;
5739 pt[2].X = 0.0;
5740 pt[2].Y = 1.0;
5741 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5742 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5743 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5744 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5745 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5747 rectcpy[3].X = rectcpy[0].X = rect->X;
5748 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5749 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5750 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5751 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5752 round_points(corners, rectcpy, 4);
5754 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5755 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5757 scaled_rect.X = margin_x * rel_width;
5758 scaled_rect.Y = 0.0;
5759 scaled_rect.Width = rel_width * rect->Width;
5760 scaled_rect.Height = rel_height * rect->Height;
5761 if (scaled_rect.Width >= 0.5)
5763 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5764 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5767 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5768 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5770 if (!(format_flags & StringFormatFlagsNoClip) &&
5771 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5772 rect->Width > 0.0 && rect->Height > 0.0)
5774 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5775 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5776 SelectClipRgn(hdc, rgn);
5779 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5780 SelectObject(hdc, gdifont);
5782 args.graphics = graphics;
5783 args.brush = brush;
5785 args.x = rect->X;
5786 args.y = rect->Y + offsety;
5788 args.rel_width = rel_width;
5789 args.rel_height = rel_height;
5791 gdi_transform_acquire(graphics);
5793 GetTextMetricsW(hdc, &textmetric);
5794 args.ascent = textmetric.tmAscent / rel_height;
5796 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5797 draw_string_callback, &args);
5799 gdi_transform_release(graphics);
5801 DeleteObject(rgn);
5802 DeleteObject(gdifont);
5804 RestoreDC(hdc, save_state);
5806 DeleteDC(temp_hdc);
5808 return Ok;
5811 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5813 TRACE("(%p)\n", graphics);
5815 if(!graphics)
5816 return InvalidParameter;
5818 if(graphics->busy)
5819 return ObjectBusy;
5821 return GdipSetInfinite(graphics->clip);
5824 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5826 GpStatus stat;
5828 TRACE("(%p)\n", graphics);
5830 if(!graphics)
5831 return InvalidParameter;
5833 if(graphics->busy)
5834 return ObjectBusy;
5836 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5837 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5839 if (stat != Ok)
5840 return stat;
5843 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5846 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5847 GpMatrixOrder order)
5849 GpStatus stat;
5851 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5853 if(!graphics)
5854 return InvalidParameter;
5856 if(graphics->busy)
5857 return ObjectBusy;
5859 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5860 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5862 if (stat != Ok)
5863 return stat;
5866 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5869 static GpStatus begin_container(GpGraphics *graphics,
5870 GraphicsContainerType type, GraphicsContainer *state)
5872 GraphicsContainerItem *container;
5873 GpStatus sts;
5875 if(!graphics || !state)
5876 return InvalidParameter;
5878 sts = init_container(&container, graphics, type);
5879 if(sts != Ok)
5880 return sts;
5882 list_add_head(&graphics->containers, &container->entry);
5883 *state = graphics->contid = container->contid;
5885 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5886 if (type == BEGIN_CONTAINER)
5887 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5888 else
5889 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5892 return Ok;
5895 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5897 TRACE("(%p, %p)\n", graphics, state);
5898 return begin_container(graphics, SAVE_GRAPHICS, state);
5901 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5902 GraphicsContainer *state)
5904 TRACE("(%p, %p)\n", graphics, state);
5905 return begin_container(graphics, BEGIN_CONTAINER, state);
5908 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5910 GraphicsContainerItem *container;
5911 GpMatrix transform;
5912 GpStatus stat;
5913 GpRectF scaled_srcrect;
5914 REAL scale_x, scale_y;
5916 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5918 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5919 return InvalidParameter;
5921 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5922 if(stat != Ok)
5923 return stat;
5925 list_add_head(&graphics->containers, &container->entry);
5926 *state = graphics->contid = container->contid;
5928 scale_x = units_to_pixels(1.0, unit, graphics->xres, graphics->printer_display);
5929 scale_y = units_to_pixels(1.0, unit, graphics->yres, graphics->printer_display);
5931 scaled_srcrect.X = scale_x * srcrect->X;
5932 scaled_srcrect.Y = scale_y * srcrect->Y;
5933 scaled_srcrect.Width = scale_x * srcrect->Width;
5934 scaled_srcrect.Height = scale_y * srcrect->Height;
5936 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5937 transform.matrix[1] = 0.0;
5938 transform.matrix[2] = 0.0;
5939 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5940 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5941 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5943 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5945 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
5946 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5949 return Ok;
5952 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5954 GpRectF dstrectf, srcrectf;
5956 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5958 if (!dstrect || !srcrect)
5959 return InvalidParameter;
5961 dstrectf.X = dstrect->X;
5962 dstrectf.Y = dstrect->Y;
5963 dstrectf.Width = dstrect->Width;
5964 dstrectf.Height = dstrect->Height;
5966 srcrectf.X = srcrect->X;
5967 srcrectf.Y = srcrect->Y;
5968 srcrectf.Width = srcrect->Width;
5969 srcrectf.Height = srcrect->Height;
5971 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5974 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5976 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5977 return NotImplemented;
5980 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5981 GraphicsContainer state)
5983 GpStatus sts;
5984 GraphicsContainerItem *container, *container2;
5986 if(!graphics)
5987 return InvalidParameter;
5989 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5990 if(container->contid == state && container->type == type)
5991 break;
5994 /* did not find a matching container */
5995 if(&container->entry == &graphics->containers)
5996 return Ok;
5998 sts = restore_container(graphics, container);
5999 if(sts != Ok)
6000 return sts;
6002 /* remove all of the containers on top of the found container */
6003 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
6004 if(container->contid == state)
6005 break;
6006 list_remove(&container->entry);
6007 delete_container(container);
6010 list_remove(&container->entry);
6011 delete_container(container);
6013 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6014 if (type == BEGIN_CONTAINER)
6015 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
6016 else
6017 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
6020 return Ok;
6023 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
6025 TRACE("(%p, %x)\n", graphics, state);
6026 return end_container(graphics, BEGIN_CONTAINER, state);
6029 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
6031 TRACE("(%p, %x)\n", graphics, state);
6032 return end_container(graphics, SAVE_GRAPHICS, state);
6035 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
6036 REAL sy, GpMatrixOrder order)
6038 GpStatus stat;
6040 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
6042 if(!graphics)
6043 return InvalidParameter;
6045 if(graphics->busy)
6046 return ObjectBusy;
6048 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6049 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
6051 if (stat != Ok)
6052 return stat;
6055 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
6058 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
6059 CombineMode mode)
6061 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
6063 if(!graphics || !srcgraphics)
6064 return InvalidParameter;
6066 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
6069 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
6070 CompositingMode mode)
6072 TRACE("(%p, %d)\n", graphics, mode);
6074 if(!graphics)
6075 return InvalidParameter;
6077 if(graphics->busy)
6078 return ObjectBusy;
6080 if(graphics->compmode == mode)
6081 return Ok;
6083 if(graphics->image && graphics->image->type == ImageTypeMetafile)
6085 GpStatus stat;
6087 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6088 EmfPlusRecordTypeSetCompositingMode, mode);
6089 if(stat != Ok)
6090 return stat;
6093 graphics->compmode = mode;
6095 return Ok;
6098 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
6099 CompositingQuality quality)
6101 TRACE("(%p, %d)\n", graphics, quality);
6103 if(!graphics)
6104 return InvalidParameter;
6106 if(graphics->busy)
6107 return ObjectBusy;
6109 if(graphics->compqual == quality)
6110 return Ok;
6112 if(graphics->image && graphics->image->type == ImageTypeMetafile)
6114 GpStatus stat;
6116 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6117 EmfPlusRecordTypeSetCompositingQuality, quality);
6118 if(stat != Ok)
6119 return stat;
6122 graphics->compqual = quality;
6124 return Ok;
6127 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
6128 InterpolationMode mode)
6130 TRACE("(%p, %d)\n", graphics, mode);
6132 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
6133 return InvalidParameter;
6135 if(graphics->busy)
6136 return ObjectBusy;
6138 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
6139 mode = InterpolationModeBilinear;
6141 if (mode == InterpolationModeHighQuality)
6142 mode = InterpolationModeHighQualityBicubic;
6144 if (mode == graphics->interpolation)
6145 return Ok;
6147 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6149 GpStatus stat;
6151 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6152 EmfPlusRecordTypeSetInterpolationMode, mode);
6153 if (stat != Ok)
6154 return stat;
6157 graphics->interpolation = mode;
6159 return Ok;
6162 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6164 GpStatus stat;
6166 TRACE("(%p, %.2f)\n", graphics, scale);
6168 if(!graphics || (scale <= 0.0))
6169 return InvalidParameter;
6171 if(graphics->busy)
6172 return ObjectBusy;
6174 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6176 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6177 if (stat != Ok)
6178 return stat;
6181 graphics->scale = scale;
6183 return Ok;
6186 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6188 GpStatus stat;
6190 TRACE("(%p, %d)\n", graphics, unit);
6192 if(!graphics)
6193 return InvalidParameter;
6195 if(graphics->busy)
6196 return ObjectBusy;
6198 if(unit == UnitWorld)
6199 return InvalidParameter;
6201 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6203 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6204 if (stat != Ok)
6205 return stat;
6208 graphics->unit = unit;
6210 return Ok;
6213 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6214 mode)
6216 TRACE("(%p, %d)\n", graphics, mode);
6218 if(!graphics)
6219 return InvalidParameter;
6221 if(graphics->busy)
6222 return ObjectBusy;
6224 if(graphics->pixeloffset == mode)
6225 return Ok;
6227 if(graphics->image && graphics->image->type == ImageTypeMetafile)
6229 GpStatus stat;
6231 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6232 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6233 if(stat != Ok)
6234 return stat;
6237 graphics->pixeloffset = mode;
6239 return Ok;
6242 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6244 TRACE("(%p,%i,%i)\n", graphics, x, y);
6246 if (!graphics)
6247 return InvalidParameter;
6249 graphics->origin_x = x;
6250 graphics->origin_y = y;
6252 return Ok;
6255 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6257 TRACE("(%p,%p,%p)\n", graphics, x, y);
6259 if (!graphics || !x || !y)
6260 return InvalidParameter;
6262 *x = graphics->origin_x;
6263 *y = graphics->origin_y;
6265 return Ok;
6268 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6270 TRACE("(%p, %d)\n", graphics, mode);
6272 if(!graphics)
6273 return InvalidParameter;
6275 if(graphics->busy)
6276 return ObjectBusy;
6278 if(graphics->smoothing == mode)
6279 return Ok;
6281 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6282 GpStatus stat;
6283 BOOL antialias = (mode != SmoothingModeDefault &&
6284 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6286 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6287 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6288 if(stat != Ok)
6289 return stat;
6292 graphics->smoothing = mode;
6294 return Ok;
6297 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6299 TRACE("(%p, %d)\n", graphics, contrast);
6301 if(!graphics)
6302 return InvalidParameter;
6304 graphics->textcontrast = contrast;
6306 return Ok;
6309 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6310 TextRenderingHint hint)
6312 TRACE("(%p, %d)\n", graphics, hint);
6314 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6315 return InvalidParameter;
6317 if(graphics->busy)
6318 return ObjectBusy;
6320 if(graphics->texthint == hint)
6321 return Ok;
6323 if(graphics->image && graphics->image->type == ImageTypeMetafile) {
6324 GpStatus stat;
6326 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6327 EmfPlusRecordTypeSetTextRenderingHint, hint);
6328 if(stat != Ok)
6329 return stat;
6332 graphics->texthint = hint;
6334 return Ok;
6337 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6339 GpStatus stat;
6341 TRACE("(%p, %p)\n", graphics, matrix);
6343 if(!graphics || !matrix)
6344 return InvalidParameter;
6346 if(graphics->busy)
6347 return ObjectBusy;
6349 TRACE("%f,%f,%f,%f,%f,%f\n",
6350 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6351 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6353 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6354 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6356 if (stat != Ok)
6357 return stat;
6360 graphics->worldtrans = *matrix;
6362 return Ok;
6365 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6366 REAL dy, GpMatrixOrder order)
6368 GpStatus stat;
6370 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6372 if(!graphics)
6373 return InvalidParameter;
6375 if(graphics->busy)
6376 return ObjectBusy;
6378 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6379 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6381 if (stat != Ok)
6382 return stat;
6385 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6388 /*****************************************************************************
6389 * GdipSetClipHrgn [GDIPLUS.@]
6391 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6393 GpRegion *region;
6394 GpStatus status;
6395 GpMatrix transform;
6397 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6399 if(!graphics)
6400 return InvalidParameter;
6402 if(graphics->busy)
6403 return ObjectBusy;
6405 /* hrgn is in gdi32 device units */
6406 status = GdipCreateRegionHrgn(hrgn, &region);
6408 if (status == Ok)
6410 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6412 if (status == Ok)
6413 status = GdipTransformRegion(region, &transform);
6415 if (status == Ok)
6416 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6418 GdipDeleteRegion(region);
6420 return status;
6423 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6425 GpStatus status;
6426 GpPath *clip_path;
6428 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6430 if(!graphics)
6431 return InvalidParameter;
6433 if(graphics->busy)
6434 return ObjectBusy;
6436 status = GdipClonePath(path, &clip_path);
6437 if (status == Ok)
6439 GpMatrix world_to_device;
6441 get_graphics_transform(graphics, CoordinateSpaceDevice,
6442 CoordinateSpaceWorld, &world_to_device);
6443 status = GdipTransformPath(clip_path, &world_to_device);
6444 if (status == Ok)
6445 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6447 GdipDeletePath(clip_path);
6449 return status;
6452 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6453 REAL width, REAL height,
6454 CombineMode mode)
6456 GpStatus status;
6457 GpRectF rect;
6458 GpRegion *region;
6460 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6462 if(!graphics)
6463 return InvalidParameter;
6465 if(graphics->busy)
6466 return ObjectBusy;
6468 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6470 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6471 if (status != Ok)
6472 return status;
6475 rect.X = x;
6476 rect.Y = y;
6477 rect.Width = width;
6478 rect.Height = height;
6479 status = GdipCreateRegionRect(&rect, &region);
6480 if (status == Ok)
6482 GpMatrix world_to_device;
6483 BOOL identity;
6485 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6486 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6487 if (status == Ok && !identity)
6488 status = GdipTransformRegion(region, &world_to_device);
6489 if (status == Ok)
6490 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6492 GdipDeleteRegion(region);
6494 return status;
6497 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6498 INT width, INT height,
6499 CombineMode mode)
6501 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6503 if(!graphics)
6504 return InvalidParameter;
6506 if(graphics->busy)
6507 return ObjectBusy;
6509 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6512 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6513 CombineMode mode)
6515 GpStatus status;
6516 GpRegion *clip;
6518 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6520 if(!graphics || !region)
6521 return InvalidParameter;
6523 if(graphics->busy)
6524 return ObjectBusy;
6526 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6528 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6529 if (status != Ok)
6530 return status;
6533 status = GdipCloneRegion(region, &clip);
6534 if (status == Ok)
6536 GpMatrix world_to_device;
6537 BOOL identity;
6539 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6540 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6541 if (status == Ok && !identity)
6542 status = GdipTransformRegion(clip, &world_to_device);
6543 if (status == Ok)
6544 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6546 GdipDeleteRegion(clip);
6548 return status;
6551 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6552 INT count)
6554 GpStatus status;
6555 GpPath* path;
6557 TRACE("(%p, %p, %d)\n", graphics, points, count);
6559 if(!graphics || !pen || count<=0)
6560 return InvalidParameter;
6562 if(graphics->busy)
6563 return ObjectBusy;
6565 status = GdipCreatePath(FillModeAlternate, &path);
6566 if (status != Ok) return status;
6568 status = GdipAddPathPolygon(path, points, count);
6569 if (status == Ok)
6570 status = GdipDrawPath(graphics, pen, path);
6572 GdipDeletePath(path);
6574 return status;
6577 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6578 INT count)
6580 GpStatus ret;
6581 GpPointF *ptf;
6582 INT i;
6584 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6586 if(count<=0) return InvalidParameter;
6587 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6588 if (!ptf) return OutOfMemory;
6590 for(i = 0;i < count; i++){
6591 ptf[i].X = (REAL)points[i].X;
6592 ptf[i].Y = (REAL)points[i].Y;
6595 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6596 heap_free(ptf);
6598 return ret;
6601 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6603 TRACE("(%p, %p)\n", graphics, dpi);
6605 if(!graphics || !dpi)
6606 return InvalidParameter;
6608 if(graphics->busy)
6609 return ObjectBusy;
6611 *dpi = graphics->xres;
6612 return Ok;
6615 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6617 TRACE("(%p, %p)\n", graphics, dpi);
6619 if(!graphics || !dpi)
6620 return InvalidParameter;
6622 if(graphics->busy)
6623 return ObjectBusy;
6625 *dpi = graphics->yres;
6626 return Ok;
6629 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6630 GpMatrixOrder order)
6632 GpMatrix m;
6633 GpStatus ret;
6635 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6637 if(!graphics || !matrix)
6638 return InvalidParameter;
6640 if(graphics->busy)
6641 return ObjectBusy;
6643 if (graphics->image && graphics->image->type == ImageTypeMetafile) {
6644 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6646 if (ret != Ok)
6647 return ret;
6650 m = graphics->worldtrans;
6652 ret = GdipMultiplyMatrix(&m, matrix, order);
6653 if(ret == Ok)
6654 graphics->worldtrans = m;
6656 return ret;
6659 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6660 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6662 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6664 GpStatus stat=Ok;
6666 TRACE("(%p, %p)\n", graphics, hdc);
6668 if(!graphics || !hdc)
6669 return InvalidParameter;
6671 if(graphics->busy)
6672 return ObjectBusy;
6674 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6676 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6678 else if (!graphics->hdc ||
6679 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6681 /* Create a fake HDC and fill it with a constant color. */
6682 HDC temp_hdc;
6683 HBITMAP hbitmap;
6684 GpRectF bounds;
6685 BITMAPINFOHEADER bmih;
6686 int i;
6688 stat = get_graphics_bounds(graphics, &bounds);
6689 if (stat != Ok)
6690 return stat;
6692 graphics->temp_hbitmap_width = bounds.Width;
6693 graphics->temp_hbitmap_height = bounds.Height;
6695 bmih.biSize = sizeof(bmih);
6696 bmih.biWidth = graphics->temp_hbitmap_width;
6697 bmih.biHeight = -graphics->temp_hbitmap_height;
6698 bmih.biPlanes = 1;
6699 bmih.biBitCount = 32;
6700 bmih.biCompression = BI_RGB;
6701 bmih.biSizeImage = 0;
6702 bmih.biXPelsPerMeter = 0;
6703 bmih.biYPelsPerMeter = 0;
6704 bmih.biClrUsed = 0;
6705 bmih.biClrImportant = 0;
6707 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6708 (void**)&graphics->temp_bits, NULL, 0);
6709 if (!hbitmap)
6710 return GenericError;
6712 if (!graphics->temp_hdc)
6714 temp_hdc = CreateCompatibleDC(0);
6716 else
6718 temp_hdc = graphics->temp_hdc;
6721 if (!temp_hdc)
6723 DeleteObject(hbitmap);
6724 return GenericError;
6727 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6728 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6730 SelectObject(temp_hdc, hbitmap);
6732 graphics->temp_hbitmap = hbitmap;
6733 *hdc = graphics->temp_hdc = temp_hdc;
6735 else
6737 *hdc = graphics->hdc;
6740 if (stat == Ok)
6741 graphics->busy = TRUE;
6743 return stat;
6746 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6748 GpStatus stat=Ok;
6750 TRACE("(%p, %p)\n", graphics, hdc);
6752 if(!graphics || !hdc || !graphics->busy)
6753 return InvalidParameter;
6755 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6757 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6759 else if (graphics->temp_hdc == hdc)
6761 DWORD* pos;
6762 int i;
6764 /* Find the pixels that have changed, and mark them as opaque. */
6765 pos = (DWORD*)graphics->temp_bits;
6766 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6768 if (*pos != DC_BACKGROUND_KEY)
6770 *pos |= 0xff000000;
6772 pos++;
6775 /* Write the changed pixels to the real target. */
6776 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6777 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6778 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6780 /* Clean up. */
6781 DeleteObject(graphics->temp_hbitmap);
6782 graphics->temp_hbitmap = NULL;
6784 else if (hdc != graphics->hdc)
6786 stat = InvalidParameter;
6789 if (stat == Ok)
6790 graphics->busy = FALSE;
6792 return stat;
6795 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6797 GpRegion *clip;
6798 GpStatus status;
6799 GpMatrix device_to_world;
6801 TRACE("(%p, %p)\n", graphics, region);
6803 if(!graphics || !region)
6804 return InvalidParameter;
6806 if(graphics->busy)
6807 return ObjectBusy;
6809 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6810 return status;
6812 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6813 status = GdipTransformRegion(clip, &device_to_world);
6814 if (status != Ok)
6816 GdipDeleteRegion(clip);
6817 return status;
6820 /* free everything except root node and header */
6821 delete_element(&region->node);
6822 memcpy(region, clip, sizeof(GpRegion));
6823 heap_free(clip);
6825 return Ok;
6828 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6830 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6832 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6833 ModifyWorldTransform(graphics->hdc, NULL, MWT_IDENTITY);
6834 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6835 SetMapMode(graphics->hdc, MM_TEXT);
6836 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6837 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6839 graphics->gdi_transform_acquire_count++;
6840 return Ok;
6843 GpStatus gdi_transform_release(GpGraphics *graphics)
6845 if (graphics->gdi_transform_acquire_count <= 0)
6847 ERR("called without matching gdi_transform_acquire\n");
6848 return GenericError;
6850 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6852 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6854 graphics->gdi_transform_acquire_count--;
6855 return Ok;
6858 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6859 GpCoordinateSpace src_space, GpMatrix *matrix)
6861 GpStatus stat = Ok;
6862 REAL scale_x, scale_y;
6864 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6866 if (dst_space != src_space)
6868 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres, graphics->printer_display);
6869 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres, graphics->printer_display);
6871 if(graphics->unit != UnitDisplay)
6873 scale_x *= graphics->scale;
6874 scale_y *= graphics->scale;
6877 if (dst_space < src_space)
6879 /* transform towards world space */
6880 switch ((int)src_space)
6882 case WineCoordinateSpaceGdiDevice:
6884 GpMatrix gdixform;
6885 gdixform = graphics->gdi_transform;
6886 stat = GdipInvertMatrix(&gdixform);
6887 if (stat != Ok)
6888 break;
6889 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6890 if (dst_space == CoordinateSpaceDevice)
6891 break;
6892 /* else fall-through */
6894 case CoordinateSpaceDevice:
6895 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6896 if (dst_space == CoordinateSpacePage)
6897 break;
6898 /* else fall-through */
6899 case CoordinateSpacePage:
6901 GpMatrix inverted_transform = graphics->worldtrans;
6902 stat = GdipInvertMatrix(&inverted_transform);
6903 if (stat == Ok)
6904 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6905 break;
6909 else
6911 /* transform towards device space */
6912 switch ((int)src_space)
6914 case CoordinateSpaceWorld:
6915 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6916 if (dst_space == CoordinateSpacePage)
6917 break;
6918 /* else fall-through */
6919 case CoordinateSpacePage:
6920 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6921 if (dst_space == CoordinateSpaceDevice)
6922 break;
6923 /* else fall-through */
6924 case CoordinateSpaceDevice:
6926 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6927 break;
6932 return stat;
6935 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6936 GpCoordinateSpace src_space, GpPointF *points, INT count)
6938 GpMatrix matrix;
6939 GpStatus stat;
6941 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6942 if (stat != Ok) return stat;
6944 return GdipTransformMatrixPoints(&matrix, points, count);
6947 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6948 GpCoordinateSpace src_space, GpPointF *points, INT count)
6950 if(!graphics || !points || count <= 0 ||
6951 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6952 src_space < 0 || src_space > CoordinateSpaceDevice)
6953 return InvalidParameter;
6955 if(graphics->busy)
6956 return ObjectBusy;
6958 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6960 if (src_space == dst_space) return Ok;
6962 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6965 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6966 GpCoordinateSpace src_space, GpPoint *points, INT count)
6968 GpPointF *pointsF;
6969 GpStatus ret;
6970 INT i;
6972 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6974 if(count <= 0)
6975 return InvalidParameter;
6977 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6978 if(!pointsF)
6979 return OutOfMemory;
6981 for(i = 0; i < count; i++){
6982 pointsF[i].X = (REAL)points[i].X;
6983 pointsF[i].Y = (REAL)points[i].Y;
6986 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6988 if(ret == Ok)
6989 for(i = 0; i < count; i++){
6990 points[i].X = gdip_round(pointsF[i].X);
6991 points[i].Y = gdip_round(pointsF[i].Y);
6993 heap_free(pointsF);
6995 return ret;
6998 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
7000 static int calls;
7002 TRACE("\n");
7004 if (!calls++)
7005 FIXME("stub\n");
7007 return NULL;
7010 /*****************************************************************************
7011 * GdipTranslateClip [GDIPLUS.@]
7013 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
7015 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
7017 if(!graphics)
7018 return InvalidParameter;
7020 if(graphics->busy)
7021 return ObjectBusy;
7023 return GdipTranslateRegion(graphics->clip, dx, dy);
7026 /*****************************************************************************
7027 * GdipTranslateClipI [GDIPLUS.@]
7029 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
7031 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
7033 if(!graphics)
7034 return InvalidParameter;
7036 if(graphics->busy)
7037 return ObjectBusy;
7039 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
7043 /*****************************************************************************
7044 * GdipMeasureDriverString [GDIPLUS.@]
7046 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7047 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
7048 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
7050 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7051 HFONT hfont;
7052 HDC hdc;
7053 REAL min_x, min_y, max_x, max_y, x, y;
7054 int i;
7055 TEXTMETRICW textmetric;
7056 const WORD *glyph_indices;
7057 WORD *dynamic_glyph_indices=NULL;
7058 REAL rel_width, rel_height, ascent, descent;
7059 GpPointF pt[3];
7061 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
7063 if (!graphics || !text || !font || !positions || !boundingBox)
7064 return InvalidParameter;
7066 if (length == -1)
7067 length = lstrlenW(text);
7069 if (length == 0)
7071 boundingBox->X = 0.0;
7072 boundingBox->Y = 0.0;
7073 boundingBox->Width = 0.0;
7074 boundingBox->Height = 0.0;
7077 if (flags & unsupported_flags)
7078 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7080 get_font_hfont(graphics, font, NULL, &hfont, NULL, matrix);
7082 hdc = CreateCompatibleDC(0);
7083 SelectObject(hdc, hfont);
7085 GetTextMetricsW(hdc, &textmetric);
7087 pt[0].X = 0.0;
7088 pt[0].Y = 0.0;
7089 pt[1].X = 1.0;
7090 pt[1].Y = 0.0;
7091 pt[2].X = 0.0;
7092 pt[2].Y = 1.0;
7093 if (matrix)
7095 GpMatrix xform = *matrix;
7096 GdipTransformMatrixPoints(&xform, pt, 3);
7098 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
7099 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
7100 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
7101 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
7102 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
7104 if (flags & DriverStringOptionsCmapLookup)
7106 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
7107 if (!glyph_indices)
7109 DeleteDC(hdc);
7110 DeleteObject(hfont);
7111 return OutOfMemory;
7114 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
7116 else
7117 glyph_indices = text;
7119 min_x = max_x = x = positions[0].X;
7120 min_y = max_y = y = positions[0].Y;
7122 ascent = textmetric.tmAscent / rel_height;
7123 descent = textmetric.tmDescent / rel_height;
7125 for (i=0; i<length; i++)
7127 int char_width;
7128 ABC abc;
7130 if (!(flags & DriverStringOptionsRealizedAdvance))
7132 x = positions[i].X;
7133 y = positions[i].Y;
7136 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
7137 char_width = abc.abcA + abc.abcB + abc.abcC;
7139 if (min_y > y - ascent) min_y = y - ascent;
7140 if (max_y < y + descent) max_y = y + descent;
7141 if (min_x > x) min_x = x;
7143 x += char_width / rel_width;
7145 if (max_x < x) max_x = x;
7148 heap_free(dynamic_glyph_indices);
7149 DeleteDC(hdc);
7150 DeleteObject(hfont);
7152 boundingBox->X = min_x;
7153 boundingBox->Y = min_y;
7154 boundingBox->Width = max_x - min_x;
7155 boundingBox->Height = max_y - min_y;
7157 return Ok;
7160 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7161 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7162 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7163 INT flags, GDIPCONST GpMatrix *matrix)
7165 INT save_state;
7166 GpPointF pt, *real_positions=NULL;
7167 INT *eto_positions=NULL;
7168 HFONT hfont;
7169 LOGFONTW lfw;
7170 UINT eto_flags=0;
7171 GpStatus status;
7172 HRGN hrgn;
7174 if (!(flags & DriverStringOptionsCmapLookup))
7175 eto_flags |= ETO_GLYPH_INDEX;
7177 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7179 real_positions = heap_alloc(sizeof(*real_positions) * length);
7180 eto_positions = heap_alloc(sizeof(*eto_positions) * 2 * (length - 1));
7181 if (!real_positions || !eto_positions)
7183 heap_free(real_positions);
7184 heap_free(eto_positions);
7185 return OutOfMemory;
7189 save_state = SaveDC(graphics->hdc);
7190 SetBkMode(graphics->hdc, TRANSPARENT);
7191 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7193 status = get_clip_hrgn(graphics, &hrgn);
7195 if (status == Ok)
7197 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7198 DeleteObject(hrgn);
7201 pt = positions[0];
7202 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7204 get_font_hfont(graphics, font, format, &hfont, &lfw, matrix);
7206 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7208 GpMatrix rotation;
7209 INT i;
7211 eto_flags |= ETO_PDY;
7213 memcpy(real_positions, positions, sizeof(PointF) * length);
7215 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7217 GdipSetMatrixElements(&rotation, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
7218 GdipRotateMatrix(&rotation, lfw.lfEscapement / 10.0, MatrixOrderAppend);
7219 GdipTransformMatrixPoints(&rotation, real_positions, length);
7221 for (i = 0; i < (length - 1); i++)
7223 eto_positions[i*2] = gdip_round(real_positions[i+1].X) - gdip_round(real_positions[i].X);
7224 eto_positions[i*2+1] = gdip_round(real_positions[i].Y) - gdip_round(real_positions[i+1].Y);
7228 SelectObject(graphics->hdc, hfont);
7230 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7232 gdi_transform_acquire(graphics);
7234 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, eto_positions);
7236 gdi_transform_release(graphics);
7238 RestoreDC(graphics->hdc, save_state);
7240 DeleteObject(hfont);
7242 heap_free(real_positions);
7243 heap_free(eto_positions);
7245 return Ok;
7248 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7249 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7250 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7251 INT flags, GDIPCONST GpMatrix *matrix)
7253 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7254 GpStatus stat;
7255 PointF *real_positions, real_position;
7256 POINT *pti;
7257 HFONT hfont;
7258 HDC hdc;
7259 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7260 DWORD max_glyphsize=0;
7261 GLYPHMETRICS glyphmetrics;
7262 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7263 BYTE *glyph_mask;
7264 BYTE *text_mask;
7265 int text_mask_stride;
7266 BYTE *pixel_data;
7267 int pixel_data_stride;
7268 GpRect pixel_area;
7269 UINT ggo_flags = GGO_GRAY8_BITMAP;
7271 if (length <= 0)
7272 return Ok;
7274 if (!(flags & DriverStringOptionsCmapLookup))
7275 ggo_flags |= GGO_GLYPH_INDEX;
7277 if (flags & unsupported_flags)
7278 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7280 pti = heap_alloc_zero(sizeof(POINT) * length);
7281 if (!pti)
7282 return OutOfMemory;
7284 if (flags & DriverStringOptionsRealizedAdvance)
7286 real_position = positions[0];
7288 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7289 round_points(pti, &real_position, 1);
7291 else
7293 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7294 if (!real_positions)
7296 heap_free(pti);
7297 return OutOfMemory;
7300 memcpy(real_positions, positions, sizeof(PointF) * length);
7302 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7303 round_points(pti, real_positions, length);
7305 heap_free(real_positions);
7308 get_font_hfont(graphics, font, format, &hfont, NULL, matrix);
7310 hdc = CreateCompatibleDC(0);
7311 SelectObject(hdc, hfont);
7313 /* Get the boundaries of the text to be drawn */
7314 for (i=0; i<length; i++)
7316 DWORD glyphsize;
7317 int left, top, right, bottom;
7319 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7320 &glyphmetrics, 0, NULL, &identity);
7322 if (glyphsize == GDI_ERROR)
7324 ERR("GetGlyphOutlineW failed\n");
7325 heap_free(pti);
7326 DeleteDC(hdc);
7327 DeleteObject(hfont);
7328 return GenericError;
7331 if (glyphsize > max_glyphsize)
7332 max_glyphsize = glyphsize;
7334 if (glyphsize != 0)
7336 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7337 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7338 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7339 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7341 if (left < min_x) min_x = left;
7342 if (top < min_y) min_y = top;
7343 if (right > max_x) max_x = right;
7344 if (bottom > max_y) max_y = bottom;
7347 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7349 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7350 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7354 if (max_glyphsize == 0)
7356 /* Nothing to draw. */
7357 heap_free(pti);
7358 DeleteDC(hdc);
7359 DeleteObject(hfont);
7360 return Ok;
7363 glyph_mask = heap_alloc_zero(max_glyphsize);
7364 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7365 text_mask_stride = max_x - min_x;
7367 if (!(glyph_mask && text_mask))
7369 heap_free(glyph_mask);
7370 heap_free(text_mask);
7371 heap_free(pti);
7372 DeleteDC(hdc);
7373 DeleteObject(hfont);
7374 return OutOfMemory;
7377 /* Generate a mask for the text */
7378 for (i=0; i<length; i++)
7380 DWORD ret;
7381 int left, top, stride;
7383 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7384 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7386 if (ret == GDI_ERROR || ret == 0)
7387 continue; /* empty glyph */
7389 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7390 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7391 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7393 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7395 BYTE *glyph_val = glyph_mask + y * stride;
7396 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7397 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7399 *text_val = min(64, *text_val + *glyph_val);
7400 glyph_val++;
7401 text_val++;
7406 heap_free(pti);
7407 DeleteDC(hdc);
7408 DeleteObject(hfont);
7409 heap_free(glyph_mask);
7411 /* get the brush data */
7412 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7413 if (!pixel_data)
7415 heap_free(text_mask);
7416 return OutOfMemory;
7419 pixel_area.X = min_x;
7420 pixel_area.Y = min_y;
7421 pixel_area.Width = max_x - min_x;
7422 pixel_area.Height = max_y - min_y;
7423 pixel_data_stride = pixel_area.Width * 4;
7425 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7426 if (stat != Ok)
7428 heap_free(text_mask);
7429 heap_free(pixel_data);
7430 return stat;
7433 /* multiply the brush data by the mask */
7434 for (y=0; y<pixel_area.Height; y++)
7436 BYTE *text_val = text_mask + text_mask_stride * y;
7437 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7438 for (x=0; x<pixel_area.Width; x++)
7440 *pixel_val = (*pixel_val) * (*text_val) / 64;
7441 text_val++;
7442 pixel_val+=4;
7446 heap_free(text_mask);
7448 gdi_transform_acquire(graphics);
7450 /* draw the result */
7451 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7452 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7454 gdi_transform_release(graphics);
7456 heap_free(pixel_data);
7458 return stat;
7461 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7462 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7463 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7464 INT flags, GDIPCONST GpMatrix *matrix)
7466 GpStatus stat = NotImplemented;
7468 if (length == -1)
7469 length = lstrlenW(text);
7471 if (graphics->image && graphics->image->type == ImageTypeMetafile)
7472 return METAFILE_DrawDriverString((GpMetafile*)graphics->image, text, length, font,
7473 format, brush, positions, flags, matrix);
7475 if (graphics->hdc && !graphics->alpha_hdc &&
7476 brush->bt == BrushTypeSolidColor &&
7477 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7478 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7479 brush, positions, flags, matrix);
7480 if (stat == NotImplemented)
7481 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7482 brush, positions, flags, matrix);
7483 return stat;
7486 /*****************************************************************************
7487 * GdipDrawDriverString [GDIPLUS.@]
7489 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7490 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7491 GDIPCONST PointF *positions, INT flags,
7492 GDIPCONST GpMatrix *matrix )
7494 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7496 if (!graphics || !text || !font || !brush || !positions)
7497 return InvalidParameter;
7499 return draw_driver_string(graphics, text, length, font, NULL,
7500 brush, positions, flags, matrix);
7503 /*****************************************************************************
7504 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7506 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7508 GpStatus stat;
7509 GpRegion* rgn;
7511 TRACE("(%p, %p)\n", graphics, res);
7513 if((stat = GdipCreateRegion(&rgn)) != Ok)
7514 return stat;
7516 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7517 goto cleanup;
7519 stat = GdipIsEmptyRegion(rgn, graphics, res);
7521 cleanup:
7522 GdipDeleteRegion(rgn);
7523 return stat;
7526 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7528 static int calls;
7530 TRACE("(%p) stub\n", graphics);
7532 if(!(calls++))
7533 FIXME("not implemented\n");
7535 return NotImplemented;
7538 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7540 TRACE("(%p, %p)\n", graphics, pabort);
7542 if (!graphics)
7543 return InvalidParameter;
7545 if (pabort)
7546 FIXME("Abort callback is not supported.\n");
7548 return Ok;