dinput: Enumerate user format object forwards.
[wine.git] / dlls / gdiplus / graphics.c
bloba9f7a464ce37f84d6a0b5ca4db8758dbd1d4e153
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 BOOL is_metafile_graphics(const GpGraphics *graphics)
119 return graphics->image && graphics->image_type == ImageTypeMetafile;
122 static ARGB blend_colors(ARGB start, ARGB end, REAL position);
124 static void init_hatch_palette(ARGB *hatch_palette, ARGB fore_color, ARGB back_color)
126 /* Pass the center of a 45-degree diagonal line with width of one unit through the
127 * center of a unit square, and the portion of the square that will be covered will
128 * equal sqrt(2) - 1/2. The covered portion for adjacent squares will be 1/4. */
129 hatch_palette[0] = back_color;
130 hatch_palette[1] = blend_colors(back_color, fore_color, 0.25);
131 hatch_palette[2] = blend_colors(back_color, fore_color, sqrt(2.0) - 0.5);
132 hatch_palette[3] = fore_color;
135 static HBITMAP create_hatch_bitmap(const GpHatch *hatch, INT origin_x, INT origin_y)
137 HBITMAP hbmp;
138 BITMAPINFOHEADER bmih;
139 DWORD *bits;
140 int x, y;
142 bmih.biSize = sizeof(bmih);
143 bmih.biWidth = 8;
144 bmih.biHeight = 8;
145 bmih.biPlanes = 1;
146 bmih.biBitCount = 32;
147 bmih.biCompression = BI_RGB;
148 bmih.biSizeImage = 0;
150 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
151 if (hbmp)
153 const unsigned char *hatch_data;
155 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
157 ARGB hatch_palette[4];
158 init_hatch_palette(hatch_palette, hatch->forecol, hatch->backcol);
160 /* Anti-aliasing is only specified for diagonal hatch patterns.
161 * This implementation repeats the pattern, shifts as needed,
162 * then uses bitmask 1 to check the pixel value, and the 0x82
163 * bitmask to check the adjacent pixel values, to determine the
164 * degree of shading needed. */
165 for (y = 0; y < 8; y++)
167 const int hy = (y + origin_y) & 7;
168 const int hx = origin_x & 7;
169 unsigned int row = (0x10101 * hatch_data[hy]) >> hx;
171 for (x = 0; x < 8; x++, row >>= 1)
173 int index;
174 if (hatch_data[8])
175 index = (row & 1) ? 2 : (row & 0x82) ? 1 : 0;
176 else
177 index = (row & 1) ? 3 : 0;
178 bits[y * 8 + 7 - x] = hatch_palette[index];
182 else
184 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
186 for (y = 0; y < 64; y++)
187 bits[y] = hatch->forecol;
191 return hbmp;
194 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb, INT origin_x, INT origin_y)
196 switch (brush->bt)
198 case BrushTypeSolidColor:
200 const GpSolidFill *sf = (const GpSolidFill *)brush;
201 lb->lbStyle = BS_SOLID;
202 lb->lbColor = ARGB2COLORREF(sf->color);
203 lb->lbHatch = 0;
204 return Ok;
207 case BrushTypeHatchFill:
209 const GpHatch *hatch = (const GpHatch *)brush;
210 HBITMAP hbmp;
212 hbmp = create_hatch_bitmap(hatch, origin_x, origin_y);
213 if (!hbmp) return OutOfMemory;
215 lb->lbStyle = BS_PATTERN;
216 lb->lbColor = 0;
217 lb->lbHatch = (ULONG_PTR)hbmp;
218 return Ok;
221 default:
222 FIXME("unhandled brush type %d\n", brush->bt);
223 lb->lbStyle = BS_SOLID;
224 lb->lbColor = get_gdi_brush_color(brush);
225 lb->lbHatch = 0;
226 return Ok;
230 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
232 switch (lb->lbStyle)
234 case BS_PATTERN:
235 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
236 break;
238 return Ok;
241 static HBRUSH create_gdi_brush(const GpBrush *brush, INT origin_x, INT origin_y)
243 LOGBRUSH lb;
244 HBRUSH gdibrush;
246 if (create_gdi_logbrush(brush, &lb, origin_x, origin_y) != Ok) return 0;
248 gdibrush = CreateBrushIndirect(&lb);
249 free_gdi_logbrush(&lb);
251 return gdibrush;
254 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
256 LOGBRUSH lb;
257 HPEN gdipen;
258 REAL width;
259 INT save_state, i, numdashes;
260 GpPointF pt[2];
261 DWORD dash_array[MAX_DASHLEN];
263 save_state = SaveDC(graphics->hdc);
265 EndPath(graphics->hdc);
267 if(pen->unit == UnitPixel){
268 width = pen->width;
270 else{
271 /* Get an estimate for the amount the pen width is affected by the world
272 * transform. (This is similar to what some of the wine drivers do.) */
273 pt[0].X = 0.0;
274 pt[0].Y = 0.0;
275 pt[1].X = 1.0;
276 pt[1].Y = 1.0;
277 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
278 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
279 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
281 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit,
282 graphics->xres, graphics->printer_display);
283 width *= graphics->scale;
285 pt[0].X = 0.0;
286 pt[0].Y = 0.0;
287 pt[1].X = 1.0;
288 pt[1].Y = 1.0;
289 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, pt, 2);
290 width *= sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
291 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
294 if(pen->dash == DashStyleCustom){
295 numdashes = min(pen->numdashes, MAX_DASHLEN);
297 TRACE("dashes are: ");
298 for(i = 0; i < numdashes; i++){
299 dash_array[i] = gdip_round(width * pen->dashes[i]);
300 TRACE("%ld, ", dash_array[i]);
302 TRACE("\n and the pen style is %x\n", pen->style);
304 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
305 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
306 numdashes, dash_array);
307 free_gdi_logbrush(&lb);
309 else
311 create_gdi_logbrush(pen->brush, &lb, graphics->origin_x, graphics->origin_y);
312 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
313 free_gdi_logbrush(&lb);
316 SelectObject(graphics->hdc, gdipen);
318 return save_state;
321 static void restore_dc(GpGraphics *graphics, INT state)
323 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
324 RestoreDC(graphics->hdc, state);
327 static void round_points(POINT *pti, GpPointF *ptf, INT count)
329 int i;
331 for(i = 0; i < count; i++){
332 if(isnan(ptf[i].X))
333 pti[i].x = 0;
334 else
335 pti[i].x = gdip_round(ptf[i].X);
337 if(isnan(ptf[i].Y))
338 pti[i].y = 0;
339 else
340 pti[i].y = gdip_round(ptf[i].Y);
344 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
345 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
347 CompositingMode comp_mode;
348 INT technology = GetDeviceCaps(graphics->hdc, TECHNOLOGY);
349 INT shadeblendcaps = GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS);
351 GdipGetCompositingMode(graphics, &comp_mode);
353 if ((technology == DT_RASPRINTER && shadeblendcaps == SB_NONE)
354 || comp_mode == CompositingModeSourceCopy)
356 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
358 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
359 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
361 else
363 BLENDFUNCTION bf;
365 bf.BlendOp = AC_SRC_OVER;
366 bf.BlendFlags = 0;
367 bf.SourceConstantAlpha = 255;
368 bf.AlphaFormat = AC_SRC_ALPHA;
370 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
371 hdc, src_x, src_y, src_width, src_height, bf);
375 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
377 GpRegion *rgn;
378 GpMatrix transform;
379 GpStatus stat;
380 BOOL identity;
382 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceDevice, &transform);
384 if (stat == Ok)
385 stat = GdipIsMatrixIdentity(&transform, &identity);
387 if (stat == Ok)
388 stat = GdipCloneRegion(graphics->clip, &rgn);
390 if (stat == Ok)
392 if (!identity)
393 stat = GdipTransformRegion(rgn, &transform);
395 if (stat == Ok)
396 stat = GdipGetRegionHRgn(rgn, NULL, hrgn);
398 GdipDeleteRegion(rgn);
401 if (stat == Ok && graphics->gdi_clip)
403 if (*hrgn)
404 CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
405 else
407 *hrgn = CreateRectRgn(0,0,0,0);
408 CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
412 return stat;
415 /* Draw ARGB data to the given graphics object */
416 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
417 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
419 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
420 INT x, y;
421 CompositingMode comp_mode;
423 GdipGetCompositingMode(graphics, &comp_mode);
425 for (y=0; y<src_height; y++)
427 for (x=0; x<src_width; x++)
429 ARGB dst_color, src_color;
430 src_color = ((ARGB*)(src + src_stride * y))[x];
432 if (comp_mode == CompositingModeSourceCopy)
434 if (!(src_color & 0xff000000))
435 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, 0);
436 else
437 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, src_color);
439 else
441 if (!(src_color & 0xff000000))
442 continue;
444 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
445 if (fmt & PixelFormatPAlpha)
446 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
447 else
448 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
453 return Ok;
456 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
457 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
459 HDC hdc;
460 HBITMAP hbitmap;
461 BITMAPINFOHEADER bih;
462 BYTE *temp_bits;
464 hdc = CreateCompatibleDC(0);
466 bih.biSize = sizeof(BITMAPINFOHEADER);
467 bih.biWidth = src_width;
468 bih.biHeight = -src_height;
469 bih.biPlanes = 1;
470 bih.biBitCount = 32;
471 bih.biCompression = BI_RGB;
472 bih.biSizeImage = 0;
473 bih.biXPelsPerMeter = 0;
474 bih.biYPelsPerMeter = 0;
475 bih.biClrUsed = 0;
476 bih.biClrImportant = 0;
478 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
479 (void**)&temp_bits, NULL, 0);
481 if(!hbitmap || !temp_bits)
482 goto done;
484 if ((GetDeviceCaps(graphics->hdc, TECHNOLOGY) == DT_RASPRINTER &&
485 GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE) ||
486 fmt & PixelFormatPAlpha)
487 memcpy(temp_bits, src, src_width * src_height * 4);
488 else
489 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
490 4 * src_width, src, src_stride);
492 SelectObject(hdc, hbitmap);
493 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
494 hdc, 0, 0, src_width, src_height);
496 DeleteObject(hbitmap);
498 done:
499 DeleteDC(hdc);
501 return Ok;
504 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
505 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
507 GpStatus stat=Ok;
509 if (graphics->image && graphics->image->type == ImageTypeBitmap)
511 DWORD i;
512 int size;
513 RGNDATA *rgndata;
514 RECT *rects;
515 HRGN hrgn, visible_rgn;
517 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
518 if (!hrgn)
519 return OutOfMemory;
521 stat = get_clip_hrgn(graphics, &visible_rgn);
522 if (stat != Ok)
524 DeleteObject(hrgn);
525 return stat;
528 if (visible_rgn)
530 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
531 DeleteObject(visible_rgn);
534 if (hregion)
535 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
537 size = GetRegionData(hrgn, 0, NULL);
539 rgndata = heap_alloc_zero(size);
540 if (!rgndata)
542 DeleteObject(hrgn);
543 return OutOfMemory;
546 GetRegionData(hrgn, size, rgndata);
548 rects = (RECT*)rgndata->Buffer;
550 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
552 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
553 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
554 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
555 src_stride, fmt);
558 heap_free(rgndata);
560 DeleteObject(hrgn);
562 return stat;
564 else if (is_metafile_graphics(graphics))
566 ERR("This should not be used for metafiles; fix caller\n");
567 return NotImplemented;
569 else
571 HRGN hrgn;
572 int save;
574 stat = get_clip_hrgn(graphics, &hrgn);
576 if (stat != Ok)
577 return stat;
579 save = SaveDC(graphics->hdc);
581 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
583 if (hregion)
584 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
586 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
587 src_height, src_stride, fmt);
589 RestoreDC(graphics->hdc, save);
591 DeleteObject(hrgn);
593 return stat;
597 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
598 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
600 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
603 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
605 INT start_a, end_a, final_a;
606 INT pos;
608 pos = gdip_round(position * 0xff);
610 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
611 end_a = ((end >> 24) & 0xff) * pos;
613 final_a = start_a + end_a;
615 if (final_a < 0xff) return 0;
617 return (final_a / 0xff) << 24 |
618 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
619 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
620 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
623 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
625 REAL blendfac;
627 /* clamp to between 0.0 and 1.0, using the wrap mode */
628 position = (position - brush->rect.X) / brush->rect.Width;
629 if (brush->wrap == WrapModeTile)
631 position = fmodf(position, 1.0f);
632 if (position < 0.0f) position += 1.0f;
634 else /* WrapModeFlip* */
636 position = fmodf(position, 2.0f);
637 if (position < 0.0f) position += 2.0f;
638 if (position > 1.0f) position = 2.0f - position;
641 if (brush->blendcount == 1)
642 blendfac = position;
643 else
645 int i=1;
646 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
647 REAL range;
649 /* locate the blend positions surrounding this position */
650 while (position > brush->blendpos[i])
651 i++;
653 /* interpolate between the blend positions */
654 left_blendpos = brush->blendpos[i-1];
655 left_blendfac = brush->blendfac[i-1];
656 right_blendpos = brush->blendpos[i];
657 right_blendfac = brush->blendfac[i];
658 range = right_blendpos - left_blendpos;
659 blendfac = (left_blendfac * (right_blendpos - position) +
660 right_blendfac * (position - left_blendpos)) / range;
663 if (brush->pblendcount == 0)
664 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
665 else
667 int i=1;
668 ARGB left_blendcolor, right_blendcolor;
669 REAL left_blendpos, right_blendpos;
671 /* locate the blend colors surrounding this position */
672 while (blendfac > brush->pblendpos[i])
673 i++;
675 /* interpolate between the blend colors */
676 left_blendpos = brush->pblendpos[i-1];
677 left_blendcolor = brush->pblendcolor[i-1];
678 right_blendpos = brush->pblendpos[i];
679 right_blendcolor = brush->pblendcolor[i];
680 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
681 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
685 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
687 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
688 BOOL identity = TRUE;
689 int i, j;
691 for (i=0; i<4; i++)
692 for (j=0; j<5; j++)
694 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
695 identity = FALSE;
696 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
699 return identity;
702 static ARGB transform_color(ARGB color, int matrix[5][5])
704 int val[5], res[4];
705 int i, j;
706 unsigned char a, r, g, b;
708 val[0] = ((color >> 16) & 0xff); /* red */
709 val[1] = ((color >> 8) & 0xff); /* green */
710 val[2] = (color & 0xff); /* blue */
711 val[3] = ((color >> 24) & 0xff); /* alpha */
712 val[4] = 255; /* translation */
714 for (i=0; i<4; i++)
716 res[i] = 0;
718 for (j=0; j<5; j++)
719 res[i] += matrix[j][i] * val[j];
722 a = min(max(res[3] / 256, 0), 255);
723 r = min(max(res[0] / 256, 0), 255);
724 g = min(max(res[1] / 256, 0), 255);
725 b = min(max(res[2] / 256, 0), 255);
727 return (a << 24) | (r << 16) | (g << 8) | b;
730 static BOOL color_is_gray(ARGB color)
732 unsigned char r, g, b;
734 r = (color >> 16) & 0xff;
735 g = (color >> 8) & 0xff;
736 b = color & 0xff;
738 return (r == g) && (g == b);
741 /* returns preferred pixel format for the applied attributes */
742 PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
743 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
745 UINT x, y;
746 INT i;
748 if ((attributes->noop[type] == IMAGEATTR_NOOP_UNDEFINED &&
749 attributes->noop[ColorAdjustTypeDefault] == IMAGEATTR_NOOP_SET) ||
750 (attributes->noop[type] == IMAGEATTR_NOOP_SET))
751 return fmt;
753 if (attributes->colorkeys[type].enabled ||
754 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
756 const struct color_key *key;
757 BYTE min_blue, min_green, min_red;
758 BYTE max_blue, max_green, max_red;
760 if (!data || fmt != PixelFormat32bppARGB)
761 return PixelFormat32bppARGB;
763 if (attributes->colorkeys[type].enabled)
764 key = &attributes->colorkeys[type];
765 else
766 key = &attributes->colorkeys[ColorAdjustTypeDefault];
768 min_blue = key->low&0xff;
769 min_green = (key->low>>8)&0xff;
770 min_red = (key->low>>16)&0xff;
772 max_blue = key->high&0xff;
773 max_green = (key->high>>8)&0xff;
774 max_red = (key->high>>16)&0xff;
776 for (x=0; x<width; x++)
777 for (y=0; y<height; y++)
779 ARGB *src_color;
780 BYTE blue, green, red;
781 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
782 blue = *src_color&0xff;
783 green = (*src_color>>8)&0xff;
784 red = (*src_color>>16)&0xff;
785 if (blue >= min_blue && green >= min_green && red >= min_red &&
786 blue <= max_blue && green <= max_green && red <= max_red)
787 *src_color = 0x00000000;
791 if (attributes->colorremaptables[type].enabled ||
792 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
794 const struct color_remap_table *table;
796 if (!data || fmt != PixelFormat32bppARGB)
797 return PixelFormat32bppARGB;
799 if (attributes->colorremaptables[type].enabled)
800 table = &attributes->colorremaptables[type];
801 else
802 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
804 for (x=0; x<width; x++)
805 for (y=0; y<height; y++)
807 ARGB *src_color;
808 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
809 for (i=0; i<table->mapsize; i++)
811 if (*src_color == table->colormap[i].oldColor.Argb)
813 *src_color = table->colormap[i].newColor.Argb;
814 break;
820 if (attributes->colormatrices[type].enabled ||
821 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
823 const struct color_matrix *colormatrices;
824 int color_matrix[5][5];
825 int gray_matrix[5][5];
826 BOOL identity;
828 if (!data || fmt != PixelFormat32bppARGB)
829 return PixelFormat32bppARGB;
831 if (attributes->colormatrices[type].enabled)
832 colormatrices = &attributes->colormatrices[type];
833 else
834 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
836 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
838 if (colormatrices->flags == ColorMatrixFlagsAltGray)
839 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
841 if (!identity)
843 for (x=0; x<width; x++)
845 for (y=0; y<height; y++)
847 ARGB *src_color;
848 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
850 if (colormatrices->flags == ColorMatrixFlagsDefault ||
851 !color_is_gray(*src_color))
853 *src_color = transform_color(*src_color, color_matrix);
855 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
857 *src_color = transform_color(*src_color, gray_matrix);
864 if (attributes->gamma_enabled[type] ||
865 attributes->gamma_enabled[ColorAdjustTypeDefault])
867 REAL gamma;
869 if (!data || fmt != PixelFormat32bppARGB)
870 return PixelFormat32bppARGB;
872 if (attributes->gamma_enabled[type])
873 gamma = attributes->gamma[type];
874 else
875 gamma = attributes->gamma[ColorAdjustTypeDefault];
877 for (x=0; x<width; x++)
878 for (y=0; y<height; y++)
880 ARGB *src_color;
881 BYTE blue, green, red;
882 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
884 blue = *src_color&0xff;
885 green = (*src_color>>8)&0xff;
886 red = (*src_color>>16)&0xff;
888 /* FIXME: We should probably use a table for this. */
889 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
890 green = floorf(powf(green / 255.0, gamma) * 255.0);
891 red = floorf(powf(red / 255.0, gamma) * 255.0);
893 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
897 return fmt;
900 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
901 * bitmap that contains all the pixels we may need to draw it. */
902 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
903 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
904 GpRect *rect)
906 INT left, top, right, bottom;
908 switch (interpolation)
910 case InterpolationModeHighQualityBilinear:
911 case InterpolationModeHighQualityBicubic:
912 /* FIXME: Include a greater range for the prefilter? */
913 case InterpolationModeBicubic:
914 case InterpolationModeBilinear:
915 left = (INT)(floorf(srcx));
916 top = (INT)(floorf(srcy));
917 right = (INT)(ceilf(srcx+srcwidth));
918 bottom = (INT)(ceilf(srcy+srcheight));
919 break;
920 case InterpolationModeNearestNeighbor:
921 default:
922 left = gdip_round(srcx);
923 top = gdip_round(srcy);
924 right = gdip_round(srcx+srcwidth);
925 bottom = gdip_round(srcy+srcheight);
926 break;
929 if (wrap == WrapModeClamp)
931 if (left < 0)
932 left = 0;
933 if (top < 0)
934 top = 0;
935 if (right >= bitmap->width)
936 right = bitmap->width-1;
937 if (bottom >= bitmap->height)
938 bottom = bitmap->height-1;
939 if (bottom < top || right < left)
940 /* entirely outside image, just sample a pixel so we don't have to
941 * special-case this later */
942 left = top = right = bottom = 0;
944 else
946 /* In some cases we can make the rectangle smaller here, but the logic
947 * is hard to get right, and tiling suggests we're likely to use the
948 * entire source image. */
949 if (left < 0 || right >= bitmap->width)
951 left = 0;
952 right = bitmap->width-1;
955 if (top < 0 || bottom >= bitmap->height)
957 top = 0;
958 bottom = bitmap->height-1;
962 rect->X = left;
963 rect->Y = top;
964 rect->Width = right - left + 1;
965 rect->Height = bottom - top + 1;
968 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
969 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
971 if (attributes->wrap == WrapModeClamp)
973 if (x < 0 || y < 0 || x >= width || y >= height)
974 return attributes->outside_color;
976 else
978 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
979 if (x < 0)
980 x = width*2 + x % (INT)(width * 2);
981 if (y < 0)
982 y = height*2 + y % (INT)(height * 2);
984 if (attributes->wrap & WrapModeTileFlipX)
986 if ((x / width) % 2 == 0)
987 x = x % width;
988 else
989 x = width - 1 - x % width;
991 else
992 x = x % width;
994 if (attributes->wrap & WrapModeTileFlipY)
996 if ((y / height) % 2 == 0)
997 y = y % height;
998 else
999 y = height - 1 - y % height;
1001 else
1002 y = y % height;
1005 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
1007 ERR("out of range pixel requested\n");
1008 return 0xffcd0084;
1011 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
1014 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
1015 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
1016 InterpolationMode interpolation, PixelOffsetMode offset_mode)
1018 static int fixme;
1020 switch (interpolation)
1022 default:
1023 if (!fixme++)
1024 FIXME("Unimplemented interpolation %i\n", interpolation);
1025 /* fall-through */
1026 case InterpolationModeBilinear:
1028 REAL leftxf, topyf;
1029 INT leftx, rightx, topy, bottomy;
1030 ARGB topleft, topright, bottomleft, bottomright;
1031 ARGB top, bottom;
1032 float x_offset;
1034 leftxf = floorf(point->X);
1035 leftx = (INT)leftxf;
1036 rightx = (INT)ceilf(point->X);
1037 topyf = floorf(point->Y);
1038 topy = (INT)topyf;
1039 bottomy = (INT)ceilf(point->Y);
1041 if (leftx == rightx && topy == bottomy)
1042 return sample_bitmap_pixel(src_rect, bits, width, height,
1043 leftx, topy, attributes);
1045 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
1046 leftx, topy, attributes);
1047 topright = sample_bitmap_pixel(src_rect, bits, width, height,
1048 rightx, topy, attributes);
1049 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
1050 leftx, bottomy, attributes);
1051 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
1052 rightx, bottomy, attributes);
1054 x_offset = point->X - leftxf;
1055 top = blend_colors(topleft, topright, x_offset);
1056 bottom = blend_colors(bottomleft, bottomright, x_offset);
1058 return blend_colors(top, bottom, point->Y - topyf);
1060 case InterpolationModeNearestNeighbor:
1062 FLOAT pixel_offset;
1063 switch (offset_mode)
1065 default:
1066 case PixelOffsetModeNone:
1067 case PixelOffsetModeHighSpeed:
1068 pixel_offset = 0.5;
1069 break;
1071 case PixelOffsetModeHalf:
1072 case PixelOffsetModeHighQuality:
1073 pixel_offset = 0.0;
1074 break;
1076 return sample_bitmap_pixel(src_rect, bits, width, height,
1077 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
1083 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
1085 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1088 /* is_fill is TRUE if filling regions, FALSE for drawing primitives */
1089 static BOOL brush_can_fill_path(GpBrush *brush, BOOL is_fill)
1091 switch (brush->bt)
1093 case BrushTypeSolidColor:
1095 if (is_fill)
1096 return TRUE;
1097 else
1099 /* cannot draw semi-transparent colors */
1100 return (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000;
1103 case BrushTypeHatchFill:
1105 GpHatch *hatch = (GpHatch*)brush;
1106 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1107 ((hatch->backcol & 0xff000000) == 0xff000000);
1109 case BrushTypeLinearGradient:
1110 case BrushTypeTextureFill:
1111 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1112 default:
1113 return FALSE;
1117 static GpStatus brush_fill_path(GpGraphics *graphics, GpBrush *brush)
1119 GpStatus status = Ok;
1120 switch (brush->bt)
1122 case BrushTypeSolidColor:
1124 GpSolidFill *fill = (GpSolidFill*)brush;
1125 HBITMAP bmp = ARGB2BMP(fill->color);
1127 if (bmp)
1129 RECT rc;
1130 /* partially transparent fill */
1132 if (!SelectClipPath(graphics->hdc, RGN_AND))
1134 status = GenericError;
1135 DeleteObject(bmp);
1136 break;
1138 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1140 HDC hdc = CreateCompatibleDC(NULL);
1142 if (!hdc)
1144 status = OutOfMemory;
1145 DeleteObject(bmp);
1146 break;
1149 SelectObject(hdc, bmp);
1150 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1151 hdc, 0, 0, 1, 1);
1152 DeleteDC(hdc);
1155 DeleteObject(bmp);
1156 break;
1158 /* else fall through */
1160 default:
1162 HBRUSH gdibrush, old_brush;
1164 gdibrush = create_gdi_brush(brush, graphics->origin_x, graphics->origin_y);
1165 if (!gdibrush)
1167 status = OutOfMemory;
1168 break;
1171 old_brush = SelectObject(graphics->hdc, gdibrush);
1172 FillPath(graphics->hdc);
1173 SelectObject(graphics->hdc, old_brush);
1174 DeleteObject(gdibrush);
1175 break;
1179 return status;
1182 static BOOL brush_can_fill_pixels(GpBrush *brush)
1184 switch (brush->bt)
1186 case BrushTypeSolidColor:
1187 case BrushTypeHatchFill:
1188 case BrushTypeLinearGradient:
1189 case BrushTypeTextureFill:
1190 case BrushTypePathGradient:
1191 return TRUE;
1192 default:
1193 return FALSE;
1197 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1198 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1200 switch (brush->bt)
1202 case BrushTypeSolidColor:
1204 int x, y;
1205 GpSolidFill *fill = (GpSolidFill*)brush;
1206 for (x=0; x<fill_area->Width; x++)
1207 for (y=0; y<fill_area->Height; y++)
1208 argb_pixels[x + y*cdwStride] = fill->color;
1209 return Ok;
1211 case BrushTypeHatchFill:
1213 int x, y;
1214 GpHatch *fill = (GpHatch*)brush;
1215 const unsigned char *hatch_data;
1216 ARGB hatch_palette[4];
1218 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1219 return NotImplemented;
1221 init_hatch_palette(hatch_palette, fill->forecol, fill->backcol);
1223 /* See create_hatch_bitmap for an explanation of how index is derived. */
1224 for (y = 0; y < fill_area->Height; y++, argb_pixels += cdwStride)
1226 const int hy = ~(y + fill_area->Y - graphics->origin_y) & 7;
1227 const int hx = graphics->origin_x & 7;
1228 const unsigned int row = (0x10101 * hatch_data[hy]) >> hx;
1230 for (x = 0; x < fill_area->Width; x++)
1232 const unsigned int srow = row >> (~(x + fill_area->X) & 7);
1233 int index;
1234 if (hatch_data[8])
1235 index = (srow & 1) ? 2 : (srow & 0x82) ? 1 : 0;
1236 else
1237 index = (srow & 1) ? 3 : 0;
1239 argb_pixels[x] = hatch_palette[index];
1243 return Ok;
1245 case BrushTypeLinearGradient:
1247 GpLineGradient *fill = (GpLineGradient*)brush;
1248 GpPointF draw_points[3];
1249 GpStatus stat;
1250 int x, y;
1252 draw_points[0].X = fill_area->X;
1253 draw_points[0].Y = fill_area->Y;
1254 draw_points[1].X = fill_area->X+1;
1255 draw_points[1].Y = fill_area->Y;
1256 draw_points[2].X = fill_area->X;
1257 draw_points[2].Y = fill_area->Y+1;
1259 /* Transform the points to a co-ordinate space where X is the point's
1260 * position in the gradient, 0.0 being the start point and 1.0 the
1261 * end point. */
1262 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1263 WineCoordinateSpaceGdiDevice, draw_points, 3);
1265 if (stat == Ok)
1267 GpMatrix world_to_gradient = fill->transform;
1269 stat = GdipInvertMatrix(&world_to_gradient);
1270 if (stat == Ok)
1271 stat = GdipTransformMatrixPoints(&world_to_gradient, draw_points, 3);
1274 if (stat == Ok)
1276 REAL x_delta = draw_points[1].X - draw_points[0].X;
1277 REAL y_delta = draw_points[2].X - draw_points[0].X;
1279 for (y=0; y<fill_area->Height; y++)
1281 for (x=0; x<fill_area->Width; x++)
1283 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1285 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1290 return stat;
1292 case BrushTypeTextureFill:
1294 GpTexture *fill = (GpTexture*)brush;
1295 GpPointF draw_points[3];
1296 GpStatus stat;
1297 int x, y;
1298 GpBitmap *bitmap;
1299 int src_stride;
1300 GpRect src_area;
1302 if (fill->image->type != ImageTypeBitmap)
1304 FIXME("metafile texture brushes not implemented\n");
1305 return NotImplemented;
1308 bitmap = (GpBitmap*)fill->image;
1309 src_stride = sizeof(ARGB) * bitmap->width;
1311 src_area.X = src_area.Y = 0;
1312 src_area.Width = bitmap->width;
1313 src_area.Height = bitmap->height;
1315 draw_points[0].X = fill_area->X;
1316 draw_points[0].Y = fill_area->Y;
1317 draw_points[1].X = fill_area->X+1;
1318 draw_points[1].Y = fill_area->Y;
1319 draw_points[2].X = fill_area->X;
1320 draw_points[2].Y = fill_area->Y+1;
1322 /* Transform the points to the co-ordinate space of the bitmap. */
1323 stat = gdip_transform_points(graphics, CoordinateSpaceWorld,
1324 WineCoordinateSpaceGdiDevice, draw_points, 3);
1326 if (stat == Ok)
1328 GpMatrix world_to_texture = fill->transform;
1330 stat = GdipInvertMatrix(&world_to_texture);
1331 if (stat == Ok)
1332 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1335 if (stat == Ok && !fill->bitmap_bits)
1337 BitmapData lockeddata;
1339 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1340 if (!fill->bitmap_bits)
1341 stat = OutOfMemory;
1343 if (stat == Ok)
1345 lockeddata.Width = bitmap->width;
1346 lockeddata.Height = bitmap->height;
1347 lockeddata.Stride = src_stride;
1348 lockeddata.PixelFormat = PixelFormat32bppARGB;
1349 lockeddata.Scan0 = fill->bitmap_bits;
1351 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1352 PixelFormat32bppARGB, &lockeddata);
1355 if (stat == Ok)
1356 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1358 if (stat == Ok)
1359 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1360 bitmap->width, bitmap->height,
1361 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1363 if (stat != Ok)
1365 heap_free(fill->bitmap_bits);
1366 fill->bitmap_bits = NULL;
1370 if (stat == Ok)
1372 REAL x_dx = draw_points[1].X - draw_points[0].X;
1373 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1374 REAL y_dx = draw_points[2].X - draw_points[0].X;
1375 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1377 for (y=0; y<fill_area->Height; y++)
1379 for (x=0; x<fill_area->Width; x++)
1381 GpPointF point;
1382 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1383 point.Y = draw_points[0].Y + x * x_dy + y * y_dy;
1385 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1386 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1387 &point, fill->imageattributes, graphics->interpolation,
1388 graphics->pixeloffset);
1393 return stat;
1395 case BrushTypePathGradient:
1397 GpPathGradient *fill = (GpPathGradient*)brush;
1398 GpPath *flat_path;
1399 GpMatrix world_to_device;
1400 GpStatus stat;
1401 int i, figure_start=0;
1402 GpPointF start_point, end_point, center_point;
1403 BYTE type;
1404 REAL min_yf, max_yf, line1_xf, line2_xf;
1405 INT min_y, max_y, min_x, max_x;
1406 INT x, y;
1407 ARGB outer_color;
1408 static BOOL transform_fixme_once;
1410 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1412 static int once;
1413 if (!once++)
1414 FIXME("path gradient focus not implemented\n");
1417 if (fill->gamma)
1419 static int once;
1420 if (!once++)
1421 FIXME("path gradient gamma correction not implemented\n");
1424 if (fill->blendcount)
1426 static int once;
1427 if (!once++)
1428 FIXME("path gradient blend not implemented\n");
1431 if (fill->pblendcount)
1433 static int once;
1434 if (!once++)
1435 FIXME("path gradient preset blend not implemented\n");
1438 if (!transform_fixme_once)
1440 BOOL is_identity=TRUE;
1441 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1442 if (!is_identity)
1444 FIXME("path gradient transform not implemented\n");
1445 transform_fixme_once = TRUE;
1449 stat = GdipClonePath(fill->path, &flat_path);
1451 if (stat != Ok)
1452 return stat;
1454 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
1455 CoordinateSpaceWorld, &world_to_device);
1456 if (stat == Ok)
1458 stat = GdipTransformPath(flat_path, &world_to_device);
1460 if (stat == Ok)
1462 center_point = fill->center;
1463 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1466 if (stat == Ok)
1467 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1470 if (stat != Ok)
1472 GdipDeletePath(flat_path);
1473 return stat;
1476 for (i=0; i<flat_path->pathdata.Count; i++)
1478 int start_center_line=0, end_center_line=0;
1479 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1480 REAL center_distance;
1481 ARGB start_color, end_color;
1482 REAL dy, dx;
1484 type = flat_path->pathdata.Types[i];
1486 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1487 figure_start = i;
1489 start_point = flat_path->pathdata.Points[i];
1491 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1493 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1495 end_point = flat_path->pathdata.Points[figure_start];
1496 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1498 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1500 end_point = flat_path->pathdata.Points[i+1];
1501 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1503 else
1504 continue;
1506 outer_color = start_color;
1508 min_yf = center_point.Y;
1509 if (min_yf > start_point.Y) min_yf = start_point.Y;
1510 if (min_yf > end_point.Y) min_yf = end_point.Y;
1512 if (min_yf < fill_area->Y)
1513 min_y = fill_area->Y;
1514 else
1515 min_y = (INT)ceil(min_yf);
1517 max_yf = center_point.Y;
1518 if (max_yf < start_point.Y) max_yf = start_point.Y;
1519 if (max_yf < end_point.Y) max_yf = end_point.Y;
1521 if (max_yf > fill_area->Y + fill_area->Height)
1522 max_y = fill_area->Y + fill_area->Height;
1523 else
1524 max_y = (INT)ceil(max_yf);
1526 dy = end_point.Y - start_point.Y;
1527 dx = end_point.X - start_point.X;
1529 /* This is proportional to the distance from start-end line to center point. */
1530 center_distance = dy * (start_point.X - center_point.X) +
1531 dx * (center_point.Y - start_point.Y);
1533 for (y=min_y; y<max_y; y++)
1535 REAL yf = (REAL)y;
1537 if (!seen_start && yf >= start_point.Y)
1539 seen_start = TRUE;
1540 start_center_line ^= 1;
1542 if (!seen_end && yf >= end_point.Y)
1544 seen_end = TRUE;
1545 end_center_line ^= 1;
1547 if (!seen_center && yf >= center_point.Y)
1549 seen_center = TRUE;
1550 start_center_line ^= 1;
1551 end_center_line ^= 1;
1554 if (start_center_line)
1555 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1556 else
1557 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1559 if (end_center_line)
1560 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1561 else
1562 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1564 if (line1_xf < line2_xf)
1566 min_x = (INT)ceil(line1_xf);
1567 max_x = (INT)ceil(line2_xf);
1569 else
1571 min_x = (INT)ceil(line2_xf);
1572 max_x = (INT)ceil(line1_xf);
1575 if (min_x < fill_area->X)
1576 min_x = fill_area->X;
1577 if (max_x > fill_area->X + fill_area->Width)
1578 max_x = fill_area->X + fill_area->Width;
1580 for (x=min_x; x<max_x; x++)
1582 REAL xf = (REAL)x;
1583 REAL distance;
1585 if (start_color != end_color)
1587 REAL blend_amount, pdy, pdx;
1588 pdy = yf - center_point.Y;
1589 pdx = xf - center_point.X;
1591 if (fabs(pdx) <= 0.001 && fabs(pdy) <= 0.001)
1593 /* Too close to center point, don't try to calculate outer color */
1594 outer_color = start_color;
1596 else
1598 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1599 outer_color = blend_colors(start_color, end_color, blend_amount);
1603 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1604 (end_point.X - start_point.X) * (yf - start_point.Y);
1606 distance = distance / center_distance;
1608 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1609 blend_colors(outer_color, fill->centercolor, distance);
1614 GdipDeletePath(flat_path);
1615 return stat;
1617 default:
1618 return NotImplemented;
1622 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1623 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1624 * should not be called on an hdc that has a path you care about. */
1625 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1626 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1628 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1629 GpMatrix matrix;
1630 HBRUSH brush = NULL;
1631 HPEN pen = NULL;
1632 PointF ptf[4], *custptf = NULL;
1633 POINT pt[4], *custpt = NULL;
1634 BYTE *tp = NULL;
1635 REAL theta, dsmall, dbig, dx, dy = 0.0;
1636 INT i, count;
1637 LOGBRUSH lb;
1638 BOOL customstroke;
1640 if((x1 == x2) && (y1 == y2))
1641 return;
1643 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1645 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1646 if(!customstroke){
1647 brush = CreateSolidBrush(color);
1648 lb.lbStyle = BS_SOLID;
1649 lb.lbColor = color;
1650 lb.lbHatch = 0;
1651 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1652 PS_JOIN_MITER, 1, &lb, 0,
1653 NULL);
1654 oldbrush = SelectObject(graphics->hdc, brush);
1655 oldpen = SelectObject(graphics->hdc, pen);
1658 switch(cap){
1659 case LineCapFlat:
1660 break;
1661 case LineCapSquare:
1662 case LineCapSquareAnchor:
1663 case LineCapDiamondAnchor:
1664 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1665 if(cap == LineCapDiamondAnchor){
1666 dsmall = cos(theta + M_PI_2) * size;
1667 dbig = sin(theta + M_PI_2) * size;
1669 else{
1670 dsmall = cos(theta + M_PI_4) * size;
1671 dbig = sin(theta + M_PI_4) * size;
1674 ptf[0].X = x2 - dsmall;
1675 ptf[1].X = x2 + dbig;
1677 ptf[0].Y = y2 - dbig;
1678 ptf[3].Y = y2 + dsmall;
1680 ptf[1].Y = y2 - dsmall;
1681 ptf[2].Y = y2 + dbig;
1683 ptf[3].X = x2 - dbig;
1684 ptf[2].X = x2 + dsmall;
1686 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1688 round_points(pt, ptf, 4);
1690 Polygon(graphics->hdc, pt, 4);
1692 break;
1693 case LineCapArrowAnchor:
1694 size = size * 4.0 / sqrt(3.0);
1696 dx = cos(M_PI / 6.0 + theta) * size;
1697 dy = sin(M_PI / 6.0 + theta) * size;
1699 ptf[0].X = x2 - dx;
1700 ptf[0].Y = y2 - dy;
1702 dx = cos(- M_PI / 6.0 + theta) * size;
1703 dy = sin(- M_PI / 6.0 + theta) * size;
1705 ptf[1].X = x2 - dx;
1706 ptf[1].Y = y2 - dy;
1708 ptf[2].X = x2;
1709 ptf[2].Y = y2;
1711 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1713 round_points(pt, ptf, 3);
1715 Polygon(graphics->hdc, pt, 3);
1717 break;
1718 case LineCapRoundAnchor:
1719 dx = dy = ANCHOR_WIDTH * size / 2.0;
1721 ptf[0].X = x2 - dx;
1722 ptf[0].Y = y2 - dy;
1723 ptf[1].X = x2 + dx;
1724 ptf[1].Y = y2 + dy;
1726 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 2);
1728 round_points(pt, ptf, 2);
1730 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1732 break;
1733 case LineCapTriangle:
1734 size = size / 2.0;
1735 dx = cos(M_PI_2 + theta) * size;
1736 dy = sin(M_PI_2 + theta) * size;
1738 ptf[0].X = x2 - dx;
1739 ptf[0].Y = y2 - dy;
1740 ptf[1].X = x2 + dx;
1741 ptf[1].Y = y2 + dy;
1743 dx = cos(theta) * size;
1744 dy = sin(theta) * size;
1746 ptf[2].X = x2 + dx;
1747 ptf[2].Y = y2 + dy;
1749 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 3);
1751 round_points(pt, ptf, 3);
1753 Polygon(graphics->hdc, pt, 3);
1755 break;
1756 case LineCapRound:
1757 dx = dy = size / 2.0;
1759 ptf[0].X = x2 - dx;
1760 ptf[0].Y = y2 - dy;
1761 ptf[1].X = x2 + dx;
1762 ptf[1].Y = y2 + dy;
1764 dx = -cos(M_PI_2 + theta) * size;
1765 dy = -sin(M_PI_2 + theta) * size;
1767 ptf[2].X = x2 - dx;
1768 ptf[2].Y = y2 - dy;
1769 ptf[3].X = x2 + dx;
1770 ptf[3].Y = y2 + dy;
1772 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
1774 round_points(pt, ptf, 4);
1776 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1777 pt[2].y, pt[3].x, pt[3].y);
1779 break;
1780 case LineCapCustom:
1781 if(!custom)
1782 break;
1784 if (custom->type == CustomLineCapTypeAdjustableArrow)
1786 GpAdjustableArrowCap *arrow = (GpAdjustableArrowCap *)custom;
1787 if (arrow->cap.fill && arrow->height <= 0.0)
1788 break;
1791 count = custom->pathdata.Count;
1792 custptf = heap_alloc_zero(count * sizeof(PointF));
1793 custpt = heap_alloc_zero(count * sizeof(POINT));
1794 tp = heap_alloc_zero(count);
1796 if(!custptf || !custpt || !tp)
1797 goto custend;
1799 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1801 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1802 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1803 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1804 MatrixOrderAppend);
1805 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1806 GdipTransformMatrixPoints(&matrix, custptf, count);
1808 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, custptf, count);
1810 round_points(custpt, custptf, count);
1812 for(i = 0; i < count; i++)
1813 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1815 if(custom->fill){
1816 BeginPath(graphics->hdc);
1817 PolyDraw(graphics->hdc, custpt, tp, count);
1818 EndPath(graphics->hdc);
1819 StrokeAndFillPath(graphics->hdc);
1821 else
1822 PolyDraw(graphics->hdc, custpt, tp, count);
1824 custend:
1825 heap_free(custptf);
1826 heap_free(custpt);
1827 heap_free(tp);
1828 break;
1829 default:
1830 break;
1833 if(!customstroke){
1834 SelectObject(graphics->hdc, oldbrush);
1835 SelectObject(graphics->hdc, oldpen);
1836 DeleteObject(brush);
1837 DeleteObject(pen);
1841 /* Shortens the line by the given percent by changing x2, y2.
1842 * If percent is > 1.0 then the line will change direction.
1843 * If percent is negative it can lengthen the line. */
1844 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1846 REAL dist, theta, dx, dy;
1848 if((y1 == *y2) && (x1 == *x2))
1849 return;
1851 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1852 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1853 dx = cos(theta) * dist;
1854 dy = sin(theta) * dist;
1856 *x2 = *x2 + dx;
1857 *y2 = *y2 + dy;
1860 /* Shortens the line by the given amount by changing x2, y2.
1861 * If the amount is greater than the distance, the line will become length 0.
1862 * If the amount is negative, it can lengthen the line. */
1863 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1865 REAL dx, dy, percent;
1867 dx = *x2 - x1;
1868 dy = *y2 - y1;
1869 if(dx == 0 && dy == 0)
1870 return;
1872 percent = amt / sqrt(dx * dx + dy * dy);
1873 if(percent >= 1.0){
1874 *x2 = x1;
1875 *y2 = y1;
1876 return;
1879 shorten_line_percent(x1, y1, x2, y2, percent);
1882 /* Conducts a linear search to find the bezier points that will back off
1883 * the endpoint of the curve by a distance of amt. Linear search works
1884 * better than binary in this case because there are multiple solutions,
1885 * and binary searches often find a bad one. I don't think this is what
1886 * Windows does but short of rendering the bezier without GDI's help it's
1887 * the best we can do. If rev then work from the start of the passed points
1888 * instead of the end. */
1889 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1891 GpPointF origpt[4];
1892 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1893 INT i, first = 0, second = 1, third = 2, fourth = 3;
1895 if(rev){
1896 first = 3;
1897 second = 2;
1898 third = 1;
1899 fourth = 0;
1902 origx = pt[fourth].X;
1903 origy = pt[fourth].Y;
1904 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1906 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1907 /* reset bezier points to original values */
1908 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1909 /* Perform magic on bezier points. Order is important here.*/
1910 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1911 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1912 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1913 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1914 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1915 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1917 dx = pt[fourth].X - origx;
1918 dy = pt[fourth].Y - origy;
1920 diff = sqrt(dx * dx + dy * dy);
1921 percent += 0.0005 * amt;
1925 /* Draws a combination of bezier curves and lines between points. */
1926 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1927 GDIPCONST BYTE * types, INT count, BOOL caps)
1929 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1930 BYTE *tp = heap_alloc_zero(count);
1931 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1932 INT i, j;
1933 GpStatus status = GenericError;
1935 if(!count){
1936 status = Ok;
1937 goto end;
1939 if(!pti || !tp || !ptcopy){
1940 status = OutOfMemory;
1941 goto end;
1944 for(i = 1; i < count; i++){
1945 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1946 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1947 || !(types[i + 2] & PathPointTypeBezier)){
1948 ERR("Bad bezier points\n");
1949 goto end;
1951 i += 2;
1955 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1957 /* If we are drawing caps, go through the points and adjust them accordingly,
1958 * and draw the caps. */
1959 if(caps){
1960 switch(types[count - 1] & PathPointTypePathTypeMask){
1961 case PathPointTypeBezier:
1962 if(pen->endcap == LineCapArrowAnchor)
1963 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1964 else if((pen->endcap == LineCapCustom) && pen->customend)
1965 shorten_bezier_amt(&ptcopy[count - 4],
1966 pen->width * pen->customend->inset, FALSE);
1968 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1969 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1970 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1971 pt[count - 1].X, pt[count - 1].Y);
1973 break;
1974 case PathPointTypeLine:
1975 if(pen->endcap == LineCapArrowAnchor)
1976 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1977 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1978 pen->width);
1979 else if((pen->endcap == LineCapCustom) && pen->customend)
1980 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1981 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1982 pen->customend->inset * pen->width);
1984 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1985 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1986 pt[count - 1].Y);
1988 break;
1989 default:
1990 ERR("Bad path last point\n");
1991 goto end;
1994 /* Find start of points */
1995 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1996 == PathPointTypeStart); j++);
1998 switch(types[j] & PathPointTypePathTypeMask){
1999 case PathPointTypeBezier:
2000 if(pen->startcap == LineCapArrowAnchor)
2001 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
2002 else if((pen->startcap == LineCapCustom) && pen->customstart)
2003 shorten_bezier_amt(&ptcopy[j - 1],
2004 pen->width * pen->customstart->inset, TRUE);
2006 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2007 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
2008 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
2009 pt[j - 1].X, pt[j - 1].Y);
2011 break;
2012 case PathPointTypeLine:
2013 if(pen->startcap == LineCapArrowAnchor)
2014 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2015 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2016 pen->width);
2017 else if((pen->startcap == LineCapCustom) && pen->customstart)
2018 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
2019 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
2020 pen->customstart->inset * pen->width);
2022 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
2023 pt[j].X, pt[j].Y, pt[j - 1].X,
2024 pt[j - 1].Y);
2026 break;
2027 default:
2028 ERR("Bad path points\n");
2029 goto end;
2033 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptcopy, count);
2035 round_points(pti, ptcopy, count);
2037 for(i = 0; i < count; i++){
2038 tp[i] = convert_path_point_type(types[i]);
2041 PolyDraw(graphics->hdc, pti, tp, count);
2043 status = Ok;
2045 end:
2046 heap_free(pti);
2047 heap_free(ptcopy);
2048 heap_free(tp);
2050 return status;
2053 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2055 GpStatus result;
2057 BeginPath(graphics->hdc);
2058 result = draw_poly(graphics, NULL, path->pathdata.Points,
2059 path->pathdata.Types, path->pathdata.Count, FALSE);
2060 EndPath(graphics->hdc);
2061 return result;
2064 typedef enum GraphicsContainerType {
2065 BEGIN_CONTAINER,
2066 SAVE_GRAPHICS
2067 } GraphicsContainerType;
2069 typedef struct _GraphicsContainerItem {
2070 struct list entry;
2071 GraphicsContainer contid;
2072 GraphicsContainerType type;
2074 SmoothingMode smoothing;
2075 CompositingQuality compqual;
2076 InterpolationMode interpolation;
2077 CompositingMode compmode;
2078 TextRenderingHint texthint;
2079 REAL scale;
2080 GpUnit unit;
2081 PixelOffsetMode pixeloffset;
2082 UINT textcontrast;
2083 GpMatrix worldtrans;
2084 GpRegion* clip;
2085 INT origin_x, origin_y;
2086 } GraphicsContainerItem;
2088 static GpStatus init_container(GraphicsContainerItem** container,
2089 GDIPCONST GpGraphics* graphics, GraphicsContainerType type){
2090 GpStatus sts;
2092 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
2093 if(!(*container))
2094 return OutOfMemory;
2096 (*container)->contid = graphics->contid + 1;
2097 (*container)->type = type;
2099 (*container)->smoothing = graphics->smoothing;
2100 (*container)->compqual = graphics->compqual;
2101 (*container)->interpolation = graphics->interpolation;
2102 (*container)->compmode = graphics->compmode;
2103 (*container)->texthint = graphics->texthint;
2104 (*container)->scale = graphics->scale;
2105 (*container)->unit = graphics->unit;
2106 (*container)->textcontrast = graphics->textcontrast;
2107 (*container)->pixeloffset = graphics->pixeloffset;
2108 (*container)->origin_x = graphics->origin_x;
2109 (*container)->origin_y = graphics->origin_y;
2110 (*container)->worldtrans = graphics->worldtrans;
2112 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2113 if(sts != Ok){
2114 heap_free(*container);
2115 *container = NULL;
2116 return sts;
2119 return Ok;
2122 static void delete_container(GraphicsContainerItem* container)
2124 GdipDeleteRegion(container->clip);
2125 heap_free(container);
2128 static GpStatus restore_container(GpGraphics* graphics,
2129 GDIPCONST GraphicsContainerItem* container){
2130 GpStatus sts;
2131 GpRegion *newClip;
2133 sts = GdipCloneRegion(container->clip, &newClip);
2134 if(sts != Ok) return sts;
2136 graphics->worldtrans = container->worldtrans;
2138 GdipDeleteRegion(graphics->clip);
2139 graphics->clip = newClip;
2141 graphics->contid = container->contid - 1;
2143 graphics->smoothing = container->smoothing;
2144 graphics->compqual = container->compqual;
2145 graphics->interpolation = container->interpolation;
2146 graphics->compmode = container->compmode;
2147 graphics->texthint = container->texthint;
2148 graphics->scale = container->scale;
2149 graphics->unit = container->unit;
2150 graphics->textcontrast = container->textcontrast;
2151 graphics->pixeloffset = container->pixeloffset;
2152 graphics->origin_x = container->origin_x;
2153 graphics->origin_y = container->origin_y;
2155 return Ok;
2158 static GpStatus get_graphics_device_bounds(GpGraphics* graphics, GpRectF* rect)
2160 RECT wnd_rect;
2161 GpStatus stat=Ok;
2162 GpUnit unit;
2164 if(graphics->hwnd) {
2165 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2166 return GenericError;
2168 rect->X = wnd_rect.left;
2169 rect->Y = wnd_rect.top;
2170 rect->Width = wnd_rect.right - wnd_rect.left;
2171 rect->Height = wnd_rect.bottom - wnd_rect.top;
2172 }else if (graphics->image){
2173 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2174 if (stat == Ok && unit != UnitPixel)
2175 FIXME("need to convert from unit %i\n", unit);
2176 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2177 HBITMAP hbmp;
2178 BITMAP bmp;
2180 rect->X = 0;
2181 rect->Y = 0;
2183 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2184 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2186 rect->Width = bmp.bmWidth;
2187 rect->Height = bmp.bmHeight;
2189 else
2191 /* FIXME: ??? */
2192 rect->Width = 1;
2193 rect->Height = 1;
2195 }else{
2196 rect->X = 0;
2197 rect->Y = 0;
2198 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2199 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2202 return stat;
2205 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2207 GpStatus stat = get_graphics_device_bounds(graphics, rect);
2209 if (stat == Ok && graphics->hdc)
2211 GpPointF points[4], min_point, max_point;
2212 int i;
2214 points[0].X = points[2].X = rect->X;
2215 points[0].Y = points[1].Y = rect->Y;
2216 points[1].X = points[3].X = rect->X + rect->Width;
2217 points[2].Y = points[3].Y = rect->Y + rect->Height;
2219 gdip_transform_points(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, points, 4);
2221 min_point = max_point = points[0];
2223 for (i=1; i<4; i++)
2225 if (points[i].X < min_point.X) min_point.X = points[i].X;
2226 if (points[i].Y < min_point.Y) min_point.Y = points[i].Y;
2227 if (points[i].X > max_point.X) max_point.X = points[i].X;
2228 if (points[i].Y > max_point.Y) max_point.Y = points[i].Y;
2231 rect->X = min_point.X;
2232 rect->Y = min_point.Y;
2233 rect->Width = max_point.X - min_point.X;
2234 rect->Height = max_point.Y - min_point.Y;
2237 return stat;
2240 /* on success, rgn will contain the region of the graphics object which
2241 * is visible after clipping has been applied */
2242 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2244 GpStatus stat;
2245 GpRectF rectf;
2246 GpRegion* tmp;
2248 /* Ignore graphics image bounds for metafiles */
2249 if (is_metafile_graphics(graphics))
2250 return GdipCombineRegionRegion(rgn, graphics->clip, CombineModeReplace);
2252 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2253 return stat;
2255 if((stat = GdipCreateRegion(&tmp)) != Ok)
2256 return stat;
2258 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2259 goto end;
2261 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2262 goto end;
2264 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2266 end:
2267 GdipDeleteRegion(tmp);
2268 return stat;
2271 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2273 REAL height;
2275 if (font->unit == UnitPixel)
2277 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres, graphics->printer_display);
2279 else
2281 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2282 height = units_to_pixels(font->emSize, font->unit, graphics->xres, graphics->printer_display);
2283 else
2284 height = units_to_pixels(font->emSize, font->unit, graphics->yres, graphics->printer_display);
2287 lf->lfHeight = -(height + 0.5);
2288 lf->lfWidth = 0;
2289 lf->lfEscapement = 0;
2290 lf->lfOrientation = 0;
2291 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2292 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2293 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2294 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2295 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2296 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2297 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2298 lf->lfQuality = DEFAULT_QUALITY;
2299 lf->lfPitchAndFamily = 0;
2300 lstrcpyW(lf->lfFaceName, font->family->FamilyName);
2303 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2304 GDIPCONST GpStringFormat *format, HFONT *hfont,
2305 LOGFONTW *lfw_return, GDIPCONST GpMatrix *matrix)
2307 HDC hdc = CreateCompatibleDC(0);
2308 GpPointF pt[3];
2309 REAL angle, rel_width, rel_height, font_height;
2310 LOGFONTW lfw;
2311 HFONT unscaled_font;
2312 TEXTMETRICW textmet;
2314 if (font->unit == UnitPixel || font->unit == UnitWorld)
2315 font_height = font->emSize;
2316 else
2318 REAL unit_scale, res;
2320 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2321 unit_scale = units_scale(font->unit, graphics->unit, res, graphics->printer_display);
2323 font_height = font->emSize * unit_scale;
2326 pt[0].X = 0.0;
2327 pt[0].Y = 0.0;
2328 pt[1].X = 1.0;
2329 pt[1].Y = 0.0;
2330 pt[2].X = 0.0;
2331 pt[2].Y = 1.0;
2332 if (matrix)
2334 GpMatrix xform = *matrix;
2335 GdipTransformMatrixPoints(&xform, pt, 3);
2338 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
2339 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2340 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2341 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2342 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2343 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2344 /* If the font unit is not pixels scaling should not be applied */
2345 if (font->unit != UnitPixel && font->unit != UnitWorld)
2347 rel_width /= graphics->scale;
2348 rel_height /= graphics->scale;
2351 get_log_fontW(font, graphics, &lfw);
2352 lfw.lfHeight = -gdip_round(font_height * rel_height);
2353 unscaled_font = CreateFontIndirectW(&lfw);
2355 SelectObject(hdc, unscaled_font);
2356 GetTextMetricsW(hdc, &textmet);
2358 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2359 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2361 *hfont = CreateFontIndirectW(&lfw);
2363 if (lfw_return)
2364 *lfw_return = lfw;
2366 DeleteDC(hdc);
2367 DeleteObject(unscaled_font);
2370 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2372 TRACE("(%p, %p)\n", hdc, graphics);
2374 return GdipCreateFromHDC2(hdc, NULL, graphics);
2377 static void get_gdi_transform(GpGraphics *graphics, GpMatrix *matrix)
2379 XFORM xform;
2381 if (graphics->hdc == NULL)
2383 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2384 return;
2387 GetTransform(graphics->hdc, 0x204, &xform);
2388 GdipSetMatrixElements(matrix, xform.eM11, xform.eM12, xform.eM21, xform.eM22, xform.eDx, xform.eDy);
2391 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2393 GpStatus retval;
2394 HBITMAP hbitmap;
2395 DIBSECTION dib;
2397 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2399 if(hDevice != NULL)
2400 FIXME("Don't know how to handle parameter hDevice\n");
2402 if(hdc == NULL)
2403 return OutOfMemory;
2405 if(graphics == NULL)
2406 return InvalidParameter;
2408 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2409 if(!*graphics) return OutOfMemory;
2411 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2413 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2414 heap_free(*graphics);
2415 return retval;
2418 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2419 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2420 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2422 (*graphics)->alpha_hdc = 1;
2425 (*graphics)->hdc = hdc;
2426 (*graphics)->hwnd = WindowFromDC(hdc);
2427 (*graphics)->owndc = FALSE;
2428 (*graphics)->smoothing = SmoothingModeDefault;
2429 (*graphics)->compqual = CompositingQualityDefault;
2430 (*graphics)->interpolation = InterpolationModeBilinear;
2431 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2432 (*graphics)->compmode = CompositingModeSourceOver;
2433 (*graphics)->unit = UnitDisplay;
2434 (*graphics)->scale = 1.0;
2435 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2436 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2437 (*graphics)->busy = FALSE;
2438 (*graphics)->textcontrast = 4;
2439 list_init(&(*graphics)->containers);
2440 (*graphics)->contid = 0;
2441 (*graphics)->printer_display = (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER);
2442 get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
2444 (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
2445 if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
2447 DeleteObject((*graphics)->gdi_clip);
2448 (*graphics)->gdi_clip = NULL;
2451 TRACE("<-- %p\n", *graphics);
2453 return Ok;
2456 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2458 GpStatus retval;
2460 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2461 if(!*graphics) return OutOfMemory;
2463 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2464 GdipSetMatrixElements(&(*graphics)->gdi_transform, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2466 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2467 heap_free(*graphics);
2468 return retval;
2471 (*graphics)->hdc = NULL;
2472 (*graphics)->hwnd = NULL;
2473 (*graphics)->owndc = FALSE;
2474 (*graphics)->image = image;
2475 /* We have to store the image type here because the image may be freed
2476 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2477 (*graphics)->image_type = image->type;
2478 (*graphics)->smoothing = SmoothingModeDefault;
2479 (*graphics)->compqual = CompositingQualityDefault;
2480 (*graphics)->interpolation = InterpolationModeBilinear;
2481 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2482 (*graphics)->compmode = CompositingModeSourceOver;
2483 (*graphics)->unit = UnitDisplay;
2484 (*graphics)->scale = 1.0;
2485 (*graphics)->xres = image->xres;
2486 (*graphics)->yres = image->yres;
2487 (*graphics)->busy = FALSE;
2488 (*graphics)->textcontrast = 4;
2489 list_init(&(*graphics)->containers);
2490 (*graphics)->contid = 0;
2492 TRACE("<-- %p\n", *graphics);
2494 return Ok;
2497 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2499 GpStatus ret;
2500 HDC hdc;
2502 TRACE("(%p, %p)\n", hwnd, graphics);
2504 hdc = GetDC(hwnd);
2506 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2508 ReleaseDC(hwnd, hdc);
2509 return ret;
2512 (*graphics)->hwnd = hwnd;
2513 (*graphics)->owndc = TRUE;
2515 return Ok;
2518 /* FIXME: no icm handling */
2519 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2521 TRACE("(%p, %p)\n", hwnd, graphics);
2523 return GdipCreateFromHWND(hwnd, graphics);
2526 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2527 UINT access, IStream **stream)
2529 DWORD dwMode;
2530 HRESULT ret;
2532 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2534 if(!stream || !filename)
2535 return InvalidParameter;
2537 if(access & GENERIC_WRITE)
2538 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2539 else if(access & GENERIC_READ)
2540 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2541 else
2542 return InvalidParameter;
2544 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2546 return hresult_to_status(ret);
2549 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2551 GraphicsContainerItem *cont, *next;
2552 GpStatus stat;
2553 TRACE("(%p)\n", graphics);
2555 if(!graphics) return InvalidParameter;
2556 if(graphics->busy) return ObjectBusy;
2558 if (is_metafile_graphics(graphics))
2560 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2561 if (stat != Ok)
2562 return stat;
2565 if (graphics->temp_hdc)
2567 DeleteDC(graphics->temp_hdc);
2568 graphics->temp_hdc = NULL;
2571 if(graphics->owndc)
2572 ReleaseDC(graphics->hwnd, graphics->hdc);
2574 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2575 list_remove(&cont->entry);
2576 delete_container(cont);
2579 GdipDeleteRegion(graphics->clip);
2581 DeleteObject(graphics->gdi_clip);
2583 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2584 * do otherwise, but we can't have that in the test suite because it means
2585 * accessing freed memory. */
2586 graphics->busy = TRUE;
2588 heap_free(graphics);
2590 return Ok;
2593 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2594 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2596 GpStatus status;
2597 GpPath *path;
2598 GpRectF rect;
2600 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2601 width, height, startAngle, sweepAngle);
2603 if(!graphics || !pen || width <= 0 || height <= 0)
2604 return InvalidParameter;
2606 if(graphics->busy)
2607 return ObjectBusy;
2609 if (is_metafile_graphics(graphics))
2611 set_rect(&rect, x, y, width, height);
2612 return METAFILE_DrawArc((GpMetafile *)graphics->image, pen, &rect, startAngle, sweepAngle);
2615 status = GdipCreatePath(FillModeAlternate, &path);
2616 if (status != Ok) return status;
2618 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2619 if (status == Ok)
2620 status = GdipDrawPath(graphics, pen, path);
2622 GdipDeletePath(path);
2623 return status;
2626 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2627 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2629 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2630 width, height, startAngle, sweepAngle);
2632 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2635 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2636 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2638 GpPointF pt[4];
2640 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2641 x2, y2, x3, y3, x4, y4);
2643 if(!graphics || !pen)
2644 return InvalidParameter;
2646 if(graphics->busy)
2647 return ObjectBusy;
2649 pt[0].X = x1;
2650 pt[0].Y = y1;
2651 pt[1].X = x2;
2652 pt[1].Y = y2;
2653 pt[2].X = x3;
2654 pt[2].Y = y3;
2655 pt[3].X = x4;
2656 pt[3].Y = y4;
2657 return GdipDrawBeziers(graphics, pen, pt, 4);
2660 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2661 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2663 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2664 x2, y2, x3, y3, x4, y4);
2666 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2669 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2670 GDIPCONST GpPointF *points, INT count)
2672 GpStatus status;
2673 GpPath *path;
2675 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2677 if(!graphics || !pen || !points || (count <= 0))
2678 return InvalidParameter;
2680 if(graphics->busy)
2681 return ObjectBusy;
2683 status = GdipCreatePath(FillModeAlternate, &path);
2684 if (status != Ok) return status;
2686 status = GdipAddPathBeziers(path, points, count);
2687 if (status == Ok)
2688 status = GdipDrawPath(graphics, pen, path);
2690 GdipDeletePath(path);
2691 return status;
2694 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2695 GDIPCONST GpPoint *points, INT count)
2697 GpPointF *pts;
2698 GpStatus ret;
2699 INT i;
2701 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2703 if(!graphics || !pen || !points || (count <= 0))
2704 return InvalidParameter;
2706 if(graphics->busy)
2707 return ObjectBusy;
2709 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2710 if(!pts)
2711 return OutOfMemory;
2713 for(i = 0; i < count; i++){
2714 pts[i].X = (REAL)points[i].X;
2715 pts[i].Y = (REAL)points[i].Y;
2718 ret = GdipDrawBeziers(graphics,pen,pts,count);
2720 heap_free(pts);
2722 return ret;
2725 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2726 GDIPCONST GpPointF *points, INT count)
2728 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2730 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2733 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2734 GDIPCONST GpPoint *points, INT count)
2736 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2738 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2741 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2742 GDIPCONST GpPointF *points, INT count, REAL tension)
2744 GpPath *path;
2745 GpStatus status;
2747 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2749 if(!graphics || !pen || !points || count <= 0)
2750 return InvalidParameter;
2752 if(graphics->busy)
2753 return ObjectBusy;
2755 status = GdipCreatePath(FillModeAlternate, &path);
2756 if (status != Ok) return status;
2758 status = GdipAddPathClosedCurve2(path, points, count, tension);
2759 if (status == Ok)
2760 status = GdipDrawPath(graphics, pen, path);
2762 GdipDeletePath(path);
2764 return status;
2767 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2768 GDIPCONST GpPoint *points, INT count, REAL tension)
2770 GpPointF *ptf;
2771 GpStatus stat;
2772 INT i;
2774 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2776 if(!points || count <= 0)
2777 return InvalidParameter;
2779 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2780 if(!ptf)
2781 return OutOfMemory;
2783 for(i = 0; i < count; i++){
2784 ptf[i].X = (REAL)points[i].X;
2785 ptf[i].Y = (REAL)points[i].Y;
2788 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2790 heap_free(ptf);
2792 return stat;
2795 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2796 GDIPCONST GpPointF *points, INT count)
2798 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2800 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2803 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2804 GDIPCONST GpPoint *points, INT count)
2806 GpPointF *pointsF;
2807 GpStatus ret;
2808 INT i;
2810 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2812 if(!points)
2813 return InvalidParameter;
2815 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2816 if(!pointsF)
2817 return OutOfMemory;
2819 for(i = 0; i < count; i++){
2820 pointsF[i].X = (REAL)points[i].X;
2821 pointsF[i].Y = (REAL)points[i].Y;
2824 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2825 heap_free(pointsF);
2827 return ret;
2830 /* Approximates cardinal spline with Bezier curves. */
2831 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2832 GDIPCONST GpPointF *points, INT count, REAL tension)
2834 GpPath *path;
2835 GpStatus status;
2837 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2839 if(!graphics || !pen)
2840 return InvalidParameter;
2842 if(graphics->busy)
2843 return ObjectBusy;
2845 if(count < 2)
2846 return InvalidParameter;
2848 status = GdipCreatePath(FillModeAlternate, &path);
2849 if (status != Ok) return status;
2851 status = GdipAddPathCurve2(path, points, count, tension);
2852 if (status == Ok)
2853 status = GdipDrawPath(graphics, pen, path);
2855 GdipDeletePath(path);
2856 return status;
2859 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2860 GDIPCONST GpPoint *points, INT count, REAL tension)
2862 GpPointF *pointsF;
2863 GpStatus ret;
2864 INT i;
2866 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2868 if(!points)
2869 return InvalidParameter;
2871 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2872 if(!pointsF)
2873 return OutOfMemory;
2875 for(i = 0; i < count; i++){
2876 pointsF[i].X = (REAL)points[i].X;
2877 pointsF[i].Y = (REAL)points[i].Y;
2880 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2881 heap_free(pointsF);
2883 return ret;
2886 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2887 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2888 REAL tension)
2890 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2892 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2893 return InvalidParameter;
2896 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2899 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2900 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2901 REAL tension)
2903 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2905 if(count < 0){
2906 return OutOfMemory;
2909 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2910 return InvalidParameter;
2913 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2916 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2917 REAL y, REAL width, REAL height)
2919 GpPath *path;
2920 GpStatus status;
2921 GpRectF rect;
2923 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2925 if(!graphics || !pen)
2926 return InvalidParameter;
2928 if(graphics->busy)
2929 return ObjectBusy;
2931 if (is_metafile_graphics(graphics))
2933 set_rect(&rect, x, y, width, height);
2934 return METAFILE_DrawEllipse((GpMetafile *)graphics->image, pen, &rect);
2937 status = GdipCreatePath(FillModeAlternate, &path);
2938 if (status != Ok) return status;
2940 status = GdipAddPathEllipse(path, x, y, width, height);
2941 if (status == Ok)
2942 status = GdipDrawPath(graphics, pen, path);
2944 GdipDeletePath(path);
2945 return status;
2948 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2949 INT y, INT width, INT height)
2951 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2953 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2957 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2959 UINT width, height;
2961 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2963 if(!graphics || !image)
2964 return InvalidParameter;
2966 GdipGetImageWidth(image, &width);
2967 GdipGetImageHeight(image, &height);
2969 return GdipDrawImagePointRect(graphics, image, x, y,
2970 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2973 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2974 INT y)
2976 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2978 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2981 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2982 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2983 GpUnit srcUnit)
2985 GpPointF points[3];
2986 REAL scale_x, scale_y, width, height;
2988 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2990 if (!graphics || !image) return InvalidParameter;
2992 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres, graphics->printer_display);
2993 scale_x *= graphics->xres / image->xres;
2994 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres, graphics->printer_display);
2995 scale_y *= graphics->yres / image->yres;
2996 width = srcwidth * scale_x;
2997 height = srcheight * scale_y;
2999 points[0].X = points[2].X = x;
3000 points[0].Y = points[1].Y = y;
3001 points[1].X = x + width;
3002 points[2].Y = y + height;
3004 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3005 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3008 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3009 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3010 GpUnit srcUnit)
3012 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3015 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3016 GDIPCONST GpPointF *dstpoints, INT count)
3018 UINT width, height;
3020 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3022 if(!image)
3023 return InvalidParameter;
3025 GdipGetImageWidth(image, &width);
3026 GdipGetImageHeight(image, &height);
3028 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3029 width, height, UnitPixel, NULL, NULL, NULL);
3032 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3033 GDIPCONST GpPoint *dstpoints, INT count)
3035 GpPointF ptf[3];
3037 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3039 if (count != 3 || !dstpoints)
3040 return InvalidParameter;
3042 ptf[0].X = (REAL)dstpoints[0].X;
3043 ptf[0].Y = (REAL)dstpoints[0].Y;
3044 ptf[1].X = (REAL)dstpoints[1].X;
3045 ptf[1].Y = (REAL)dstpoints[1].Y;
3046 ptf[2].X = (REAL)dstpoints[2].X;
3047 ptf[2].Y = (REAL)dstpoints[2].Y;
3049 return GdipDrawImagePoints(graphics, image, ptf, count);
3052 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3053 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3055 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3056 return TRUE;
3059 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3060 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3061 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3062 DrawImageAbort callback, VOID * callbackData)
3064 GpPointF ptf[4];
3065 POINT pti[4];
3066 GpStatus stat;
3068 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3069 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3070 callbackData);
3072 if (count > 3)
3073 return NotImplemented;
3075 if(!graphics || !image || !points || count != 3)
3076 return InvalidParameter;
3078 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3079 debugstr_pointf(&points[2]));
3081 if (is_metafile_graphics(graphics))
3083 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
3084 image, points, count, srcx, srcy, srcwidth, srcheight,
3085 srcUnit, imageAttributes, callback, callbackData);
3088 memcpy(ptf, points, 3 * sizeof(GpPointF));
3090 /* Ensure source width/height is positive */
3091 if (srcwidth < 0)
3093 GpPointF tmp = ptf[1];
3094 srcx = srcx + srcwidth;
3095 srcwidth = -srcwidth;
3096 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
3097 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3098 ptf[1] = ptf[0];
3099 ptf[0] = tmp;
3102 if (srcheight < 0)
3104 GpPointF tmp = ptf[2];
3105 srcy = srcy + srcheight;
3106 srcheight = -srcheight;
3107 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
3108 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
3109 ptf[2] = ptf[0];
3110 ptf[0] = tmp;
3113 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3114 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3115 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
3116 return Ok;
3117 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
3118 round_points(pti, ptf, 4);
3120 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3121 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3123 srcx = units_to_pixels(srcx, srcUnit, image->xres, graphics->printer_display);
3124 srcy = units_to_pixels(srcy, srcUnit, image->yres, graphics->printer_display);
3125 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres, graphics->printer_display);
3126 srcheight = units_to_pixels(srcheight, srcUnit, image->yres, graphics->printer_display);
3127 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3129 if (image->type == ImageTypeBitmap)
3131 GpBitmap* bitmap = (GpBitmap*)image;
3132 BOOL do_resampling = FALSE;
3133 BOOL use_software = FALSE;
3135 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08lx\n",
3136 graphics->xres, graphics->yres,
3137 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3138 graphics->scale, image->xres, image->yres, bitmap->format,
3139 imageAttributes ? imageAttributes->outside_color : 0);
3141 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3142 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3143 srcx < 0 || srcy < 0 ||
3144 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3145 do_resampling = TRUE;
3147 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3148 (graphics->image && graphics->image->type == ImageTypeBitmap))
3149 use_software = TRUE;
3151 if (use_software)
3153 RECT dst_area;
3154 GpRectF graphics_bounds;
3155 GpRect src_area;
3156 int i, x, y, src_stride, dst_stride;
3157 GpMatrix dst_to_src;
3158 REAL m11, m12, m21, m22, mdx, mdy;
3159 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3160 BitmapData lockeddata;
3161 InterpolationMode interpolation = graphics->interpolation;
3162 PixelOffsetMode offset_mode = graphics->pixeloffset;
3163 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3164 REAL x_dx, x_dy, y_dx, y_dy;
3165 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3167 if (!imageAttributes)
3168 imageAttributes = &defaultImageAttributes;
3170 dst_area.left = dst_area.right = pti[0].x;
3171 dst_area.top = dst_area.bottom = pti[0].y;
3172 for (i=1; i<4; i++)
3174 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3175 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3176 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3177 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3180 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3181 if (stat != Ok) return stat;
3183 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3184 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3185 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3186 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3188 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3190 if (IsRectEmpty(&dst_area)) return Ok;
3192 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3193 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3194 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3195 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3196 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3197 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3199 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3201 stat = GdipInvertMatrix(&dst_to_src);
3202 if (stat != Ok) return stat;
3204 if (do_resampling)
3206 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3207 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3209 else
3211 /* Make sure src_area is equal in size to dst_area. */
3212 src_area.X = srcx + dst_area.left - pti[0].x;
3213 src_area.Y = srcy + dst_area.top - pti[0].y;
3214 src_area.Width = dst_area.right - dst_area.left;
3215 src_area.Height = dst_area.bottom - dst_area.top;
3218 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3220 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3221 if (!src_data)
3222 return OutOfMemory;
3223 src_stride = sizeof(ARGB) * src_area.Width;
3225 /* Read the bits we need from the source bitmap into a compatible buffer. */
3226 lockeddata.Width = src_area.Width;
3227 lockeddata.Height = src_area.Height;
3228 lockeddata.Stride = src_stride;
3229 lockeddata.Scan0 = src_data;
3230 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3231 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3232 else
3233 lockeddata.PixelFormat = PixelFormat32bppARGB;
3235 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3236 lockeddata.PixelFormat, &lockeddata);
3238 if (stat == Ok)
3239 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3241 if (stat != Ok)
3243 heap_free(src_data);
3244 return stat;
3247 apply_image_attributes(imageAttributes, src_data,
3248 src_area.Width, src_area.Height,
3249 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3251 if (do_resampling)
3253 /* Transform the bits as needed to the destination. */
3254 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3255 if (!dst_data)
3257 heap_free(src_data);
3258 return OutOfMemory;
3261 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3263 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3265 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3266 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3267 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3268 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3270 for (x=dst_area.left; x<dst_area.right; x++)
3272 for (y=dst_area.top; y<dst_area.bottom; y++)
3274 GpPointF src_pointf;
3275 ARGB *dst_color;
3277 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3278 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3280 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3282 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3283 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3284 imageAttributes, interpolation, offset_mode);
3285 else
3286 *dst_color = 0;
3290 else
3292 dst_data = src_data;
3293 dst_stride = src_stride;
3296 gdi_transform_acquire(graphics);
3298 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3299 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3300 lockeddata.PixelFormat);
3302 gdi_transform_release(graphics);
3304 heap_free(src_data);
3306 heap_free(dst_dyn_data);
3308 return stat;
3310 else
3312 HDC hdc;
3313 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3314 HBITMAP hbitmap, old_hbm=NULL;
3315 HRGN hrgn;
3316 INT save_state;
3318 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3319 bitmap->format == PixelFormat24bppRGB ||
3320 bitmap->format == PixelFormat32bppRGB ||
3321 bitmap->format == PixelFormat32bppPARGB))
3323 BITMAPINFOHEADER bih;
3324 BYTE *temp_bits;
3325 PixelFormat dst_format;
3327 /* we can't draw a bitmap of this format directly */
3328 hdc = CreateCompatibleDC(0);
3329 temp_hdc = TRUE;
3330 temp_bitmap = TRUE;
3332 bih.biSize = sizeof(BITMAPINFOHEADER);
3333 bih.biWidth = bitmap->width;
3334 bih.biHeight = -bitmap->height;
3335 bih.biPlanes = 1;
3336 bih.biBitCount = 32;
3337 bih.biCompression = BI_RGB;
3338 bih.biSizeImage = 0;
3339 bih.biXPelsPerMeter = 0;
3340 bih.biYPelsPerMeter = 0;
3341 bih.biClrUsed = 0;
3342 bih.biClrImportant = 0;
3344 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3345 (void**)&temp_bits, NULL, 0);
3347 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3348 dst_format = PixelFormat32bppPARGB;
3349 else
3350 dst_format = PixelFormat32bppRGB;
3352 convert_pixels(bitmap->width, bitmap->height,
3353 bitmap->width*4, temp_bits, dst_format, bitmap->image.palette,
3354 bitmap->stride, bitmap->bits, bitmap->format,
3355 bitmap->image.palette);
3357 else
3359 if (bitmap->hbitmap)
3360 hbitmap = bitmap->hbitmap;
3361 else
3363 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3364 temp_bitmap = TRUE;
3367 hdc = bitmap->hdc;
3368 temp_hdc = (hdc == 0);
3371 if (temp_hdc)
3373 if (!hdc) hdc = CreateCompatibleDC(0);
3374 old_hbm = SelectObject(hdc, hbitmap);
3377 save_state = SaveDC(graphics->hdc);
3379 stat = get_clip_hrgn(graphics, &hrgn);
3381 if (stat == Ok)
3383 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3384 DeleteObject(hrgn);
3387 gdi_transform_acquire(graphics);
3389 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3391 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3392 hdc, srcx, srcy, srcwidth, srcheight);
3394 else
3396 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3397 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3400 gdi_transform_release(graphics);
3402 RestoreDC(graphics->hdc, save_state);
3404 if (temp_hdc)
3406 SelectObject(hdc, old_hbm);
3407 DeleteDC(hdc);
3410 if (temp_bitmap)
3411 DeleteObject(hbitmap);
3414 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3416 GpRectF rc;
3418 set_rect(&rc, srcx, srcy, srcwidth, srcheight);
3419 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3420 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3422 else
3424 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3425 return InvalidParameter;
3428 return Ok;
3431 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3432 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3433 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3434 DrawImageAbort callback, VOID * callbackData)
3436 GpPointF pointsF[3];
3437 INT i;
3439 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3440 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3441 callbackData);
3443 if(!points || count!=3)
3444 return InvalidParameter;
3446 for(i = 0; i < count; i++){
3447 pointsF[i].X = (REAL)points[i].X;
3448 pointsF[i].Y = (REAL)points[i].Y;
3451 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3452 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3453 callback, callbackData);
3456 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3457 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3458 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3459 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3460 VOID * callbackData)
3462 GpPointF points[3];
3464 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3465 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3466 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3468 points[0].X = dstx;
3469 points[0].Y = dsty;
3470 points[1].X = dstx + dstwidth;
3471 points[1].Y = dsty;
3472 points[2].X = dstx;
3473 points[2].Y = dsty + dstheight;
3475 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3476 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3479 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3480 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3481 INT srcwidth, INT srcheight, GpUnit srcUnit,
3482 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3483 VOID * callbackData)
3485 GpPointF points[3];
3487 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3488 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3489 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3491 points[0].X = dstx;
3492 points[0].Y = dsty;
3493 points[1].X = dstx + dstwidth;
3494 points[1].Y = dsty;
3495 points[2].X = dstx;
3496 points[2].Y = dsty + dstheight;
3498 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3499 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3502 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3503 REAL x, REAL y, REAL width, REAL height)
3505 RectF bounds;
3506 GpUnit unit;
3507 GpStatus ret;
3509 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3511 if(!graphics || !image)
3512 return InvalidParameter;
3514 ret = GdipGetImageBounds(image, &bounds, &unit);
3515 if(ret != Ok)
3516 return ret;
3518 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3519 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3520 unit, NULL, NULL, NULL);
3523 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3524 INT x, INT y, INT width, INT height)
3526 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3528 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3531 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3532 REAL y1, REAL x2, REAL y2)
3534 GpPointF pt[2];
3536 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3538 if (!pen)
3539 return InvalidParameter;
3541 if (pen->unit == UnitPixel && pen->width <= 0.0)
3542 return Ok;
3544 pt[0].X = x1;
3545 pt[0].Y = y1;
3546 pt[1].X = x2;
3547 pt[1].Y = y2;
3548 return GdipDrawLines(graphics, pen, pt, 2);
3551 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3552 INT y1, INT x2, INT y2)
3554 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3556 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3559 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3560 GpPointF *points, INT count)
3562 GpStatus status;
3563 GpPath *path;
3565 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3567 if(!pen || !graphics || (count < 2))
3568 return InvalidParameter;
3570 if(graphics->busy)
3571 return ObjectBusy;
3573 status = GdipCreatePath(FillModeAlternate, &path);
3574 if (status != Ok) return status;
3576 status = GdipAddPathLine2(path, points, count);
3577 if (status == Ok)
3578 status = GdipDrawPath(graphics, pen, path);
3580 GdipDeletePath(path);
3581 return status;
3584 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3585 GpPoint *points, INT count)
3587 GpStatus retval;
3588 GpPointF *ptf;
3589 int i;
3591 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3593 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3594 if(!ptf) return OutOfMemory;
3596 for(i = 0; i < count; i ++){
3597 ptf[i].X = (REAL) points[i].X;
3598 ptf[i].Y = (REAL) points[i].Y;
3601 retval = GdipDrawLines(graphics, pen, ptf, count);
3603 heap_free(ptf);
3604 return retval;
3607 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3609 INT save_state;
3610 GpStatus retval;
3611 HRGN hrgn=NULL;
3613 save_state = prepare_dc(graphics, pen);
3615 retval = get_clip_hrgn(graphics, &hrgn);
3617 if (retval != Ok)
3618 goto end;
3620 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3622 gdi_transform_acquire(graphics);
3624 retval = draw_poly(graphics, pen, path->pathdata.Points,
3625 path->pathdata.Types, path->pathdata.Count, TRUE);
3627 gdi_transform_release(graphics);
3629 end:
3630 restore_dc(graphics, save_state);
3631 DeleteObject(hrgn);
3633 return retval;
3636 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3638 GpStatus stat;
3639 GpPath* flat_path;
3640 GpMatrix* transform;
3641 GpRectF gp_bound_rect;
3642 GpRect gp_output_area;
3643 RECT output_area;
3644 INT output_height, output_width;
3645 DWORD *output_bits, *brush_bits=NULL;
3646 int i;
3647 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3648 const BYTE *dash_pattern;
3649 INT dash_pattern_size;
3650 BYTE *dyn_dash_pattern = NULL;
3652 stat = GdipClonePath(path, &flat_path);
3654 if (stat != Ok)
3655 return stat;
3657 stat = GdipCreateMatrix(&transform);
3659 if (stat == Ok)
3661 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3662 CoordinateSpaceWorld, transform);
3664 if (stat == Ok)
3665 stat = GdipFlattenPath(flat_path, transform, 1.0);
3667 GdipDeleteMatrix(transform);
3670 /* estimate the output size in pixels, can be larger than necessary */
3671 if (stat == Ok)
3673 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3674 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3675 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3676 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3678 for (i=1; i<flat_path->pathdata.Count; i++)
3680 REAL x, y;
3681 x = flat_path->pathdata.Points[i].X;
3682 y = flat_path->pathdata.Points[i].Y;
3684 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3685 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3686 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3687 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3690 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3693 if (stat == Ok)
3695 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3696 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3697 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3698 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3700 output_width = output_area.right - output_area.left + 1;
3701 output_height = output_area.bottom - output_area.top + 1;
3703 if (output_width <= 0 || output_height <= 0)
3705 GdipDeletePath(flat_path);
3706 return Ok;
3709 gp_output_area.X = output_area.left;
3710 gp_output_area.Y = output_area.top;
3711 gp_output_area.Width = output_width;
3712 gp_output_area.Height = output_height;
3714 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3715 if (!output_bits)
3716 stat = OutOfMemory;
3719 if (stat == Ok)
3721 if (pen->brush->bt != BrushTypeSolidColor)
3723 /* allocate and draw brush output */
3724 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3726 if (brush_bits)
3728 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3729 &gp_output_area, output_width);
3731 else
3732 stat = OutOfMemory;
3735 if (stat == Ok)
3737 /* convert dash pattern to bool array */
3738 switch (pen->dash)
3740 case DashStyleCustom:
3742 dash_pattern_size = 0;
3744 for (i=0; i < pen->numdashes; i++)
3745 dash_pattern_size += gdip_round(pen->dashes[i]);
3747 if (dash_pattern_size != 0)
3749 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3751 if (dyn_dash_pattern)
3753 int j=0;
3754 for (i=0; i < pen->numdashes; i++)
3756 int k;
3757 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3758 dyn_dash_pattern[j++] = (i&1)^1;
3761 else
3762 stat = OutOfMemory;
3764 break;
3766 /* else fall through */
3768 case DashStyleSolid:
3769 default:
3770 dash_pattern = static_dash_pattern;
3771 dash_pattern_size = 1;
3772 break;
3773 case DashStyleDash:
3774 dash_pattern = static_dash_pattern;
3775 dash_pattern_size = 4;
3776 break;
3777 case DashStyleDot:
3778 dash_pattern = &static_dash_pattern[4];
3779 dash_pattern_size = 2;
3780 break;
3781 case DashStyleDashDot:
3782 dash_pattern = static_dash_pattern;
3783 dash_pattern_size = 6;
3784 break;
3785 case DashStyleDashDotDot:
3786 dash_pattern = static_dash_pattern;
3787 dash_pattern_size = 8;
3788 break;
3792 if (stat == Ok)
3794 /* trace path */
3795 GpPointF subpath_start = flat_path->pathdata.Points[0];
3796 INT prev_x = INT_MAX, prev_y = INT_MAX;
3797 int dash_pos = dash_pattern_size - 1;
3799 for (i=0; i < flat_path->pathdata.Count; i++)
3801 BYTE type, type2;
3802 GpPointF start_point, end_point;
3803 GpPoint start_pointi, end_pointi;
3805 type = flat_path->pathdata.Types[i];
3806 if (i+1 < flat_path->pathdata.Count)
3807 type2 = flat_path->pathdata.Types[i+1];
3808 else
3809 type2 = PathPointTypeStart;
3811 start_point = flat_path->pathdata.Points[i];
3813 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3814 subpath_start = start_point;
3816 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3817 end_point = subpath_start;
3818 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3819 continue;
3820 else
3821 end_point = flat_path->pathdata.Points[i+1];
3823 start_pointi.X = floorf(start_point.X);
3824 start_pointi.Y = floorf(start_point.Y);
3825 end_pointi.X = floorf(end_point.X);
3826 end_pointi.Y = floorf(end_point.Y);
3828 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3829 continue;
3831 /* draw line segment */
3832 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3834 INT x, y, start_y, end_y, step;
3836 if (start_pointi.Y < end_pointi.Y)
3838 step = 1;
3839 start_y = ceilf(start_point.Y) - output_area.top;
3840 end_y = end_pointi.Y - output_area.top;
3842 else
3844 step = -1;
3845 start_y = start_point.Y - output_area.top;
3846 end_y = ceilf(end_point.Y) - output_area.top;
3849 for (y=start_y; y != (end_y+step); y+=step)
3851 x = gdip_round( start_point.X +
3852 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3853 - output_area.left;
3855 if (x == prev_x && y == prev_y)
3856 continue;
3858 prev_x = x;
3859 prev_y = y;
3860 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3862 if (!dash_pattern[dash_pos])
3863 continue;
3865 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3866 continue;
3868 if (brush_bits)
3869 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3870 else
3871 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3874 else
3876 INT x, y, start_x, end_x, step;
3878 if (start_pointi.X < end_pointi.X)
3880 step = 1;
3881 start_x = ceilf(start_point.X) - output_area.left;
3882 end_x = end_pointi.X - output_area.left;
3884 else
3886 step = -1;
3887 start_x = start_point.X - output_area.left;
3888 end_x = ceilf(end_point.X) - output_area.left;
3891 for (x=start_x; x != (end_x+step); x+=step)
3893 y = gdip_round( start_point.Y +
3894 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3895 - output_area.top;
3897 if (x == prev_x && y == prev_y)
3898 continue;
3900 prev_x = x;
3901 prev_y = y;
3902 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3904 if (!dash_pattern[dash_pos])
3905 continue;
3907 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3908 continue;
3910 if (brush_bits)
3911 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3912 else
3913 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3919 /* draw output image */
3920 if (stat == Ok)
3922 gdi_transform_acquire(graphics);
3924 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3925 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3926 PixelFormat32bppARGB);
3928 gdi_transform_release(graphics);
3931 heap_free(brush_bits);
3932 heap_free(dyn_dash_pattern);
3933 heap_free(output_bits);
3936 GdipDeletePath(flat_path);
3938 return stat;
3941 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3943 GpStatus stat;
3944 GpPath *wide_path;
3945 GpMatrix *transform=NULL;
3946 REAL flatness=1.0;
3948 /* Check if the final pen thickness in pixels is too thin. */
3949 if (pen->unit == UnitPixel)
3951 if (pen->width < 1.415)
3952 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3954 else
3956 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3958 points[1].X = pen->width;
3959 points[2].Y = pen->width;
3961 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3962 CoordinateSpaceWorld, points, 3);
3964 if (stat != Ok)
3965 return stat;
3967 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3968 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3969 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3970 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3971 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3974 stat = GdipClonePath(path, &wide_path);
3976 if (stat != Ok)
3977 return stat;
3979 if (pen->unit == UnitPixel)
3981 /* We have to transform this to device coordinates to get the widths right. */
3982 stat = GdipCreateMatrix(&transform);
3984 if (stat == Ok)
3985 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3986 CoordinateSpaceWorld, transform);
3988 else
3990 /* Set flatness based on the final coordinate space */
3991 GpMatrix t;
3993 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3994 CoordinateSpaceWorld, &t);
3996 if (stat != Ok)
3997 return stat;
3999 flatness = 1.0/sqrt(fmax(
4000 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
4001 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
4004 if (stat == Ok)
4005 stat = GdipWidenPath(wide_path, pen, transform, flatness);
4007 if (pen->unit == UnitPixel)
4009 /* Transform the path back to world coordinates */
4010 if (stat == Ok)
4011 stat = GdipInvertMatrix(transform);
4013 if (stat == Ok)
4014 stat = GdipTransformPath(wide_path, transform);
4017 /* Actually draw the path */
4018 if (stat == Ok)
4019 stat = GdipFillPath(graphics, pen->brush, wide_path);
4021 GdipDeleteMatrix(transform);
4023 GdipDeletePath(wide_path);
4025 return stat;
4028 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
4030 GpStatus retval;
4032 TRACE("(%p, %p, %p)\n", graphics, pen, path);
4034 if(!pen || !graphics)
4035 return InvalidParameter;
4037 if(graphics->busy)
4038 return ObjectBusy;
4040 if (path->pathdata.Count == 0)
4041 return Ok;
4043 if (is_metafile_graphics(graphics))
4044 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
4045 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
4046 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
4047 else
4048 retval = GDI32_GdipDrawPath(graphics, pen, path);
4050 return retval;
4053 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
4054 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4056 GpStatus status;
4057 GpPath *path;
4059 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
4060 width, height, startAngle, sweepAngle);
4062 if(!graphics || !pen)
4063 return InvalidParameter;
4065 if(graphics->busy)
4066 return ObjectBusy;
4068 status = GdipCreatePath(FillModeAlternate, &path);
4069 if (status != Ok) return status;
4071 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4072 if (status == Ok)
4073 status = GdipDrawPath(graphics, pen, path);
4075 GdipDeletePath(path);
4076 return status;
4079 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
4080 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4082 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
4083 width, height, startAngle, sweepAngle);
4085 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4088 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
4089 REAL y, REAL width, REAL height)
4091 GpRectF rect;
4093 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
4095 set_rect(&rect, x, y, width, height);
4096 return GdipDrawRectangles(graphics, pen, &rect, 1);
4099 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
4100 INT y, INT width, INT height)
4102 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
4104 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4107 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
4108 GDIPCONST GpRectF* rects, INT count)
4110 GpStatus status;
4111 GpPath *path;
4113 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4115 if(!graphics || !pen || !rects || count < 1)
4116 return InvalidParameter;
4118 if(graphics->busy)
4119 return ObjectBusy;
4121 if (is_metafile_graphics(graphics))
4122 return METAFILE_DrawRectangles((GpMetafile *)graphics->image, pen, rects, count);
4124 status = GdipCreatePath(FillModeAlternate, &path);
4125 if (status != Ok) return status;
4127 status = GdipAddPathRectangles(path, rects, count);
4128 if (status == Ok)
4129 status = GdipDrawPath(graphics, pen, path);
4131 GdipDeletePath(path);
4132 return status;
4135 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4136 GDIPCONST GpRect* rects, INT count)
4138 GpRectF *rectsF;
4139 GpStatus ret;
4140 INT i;
4142 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4144 if(!rects || count<=0)
4145 return InvalidParameter;
4147 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4148 if(!rectsF)
4149 return OutOfMemory;
4151 for(i = 0;i < count;i++)
4152 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, 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;
4242 GpRectF rect;
4244 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4246 if(!graphics || !brush)
4247 return InvalidParameter;
4249 if(graphics->busy)
4250 return ObjectBusy;
4252 if (is_metafile_graphics(graphics))
4254 set_rect(&rect, x, y, width, height);
4255 return METAFILE_FillEllipse((GpMetafile *)graphics->image, brush, &rect);
4258 stat = GdipCreatePath(FillModeAlternate, &path);
4260 if (stat == Ok)
4262 stat = GdipAddPathEllipse(path, x, y, width, height);
4264 if (stat == Ok)
4265 stat = GdipFillPath(graphics, brush, path);
4267 GdipDeletePath(path);
4270 return stat;
4273 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4274 INT y, INT width, INT height)
4276 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4278 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4281 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4283 INT save_state;
4284 GpStatus retval;
4285 HRGN hrgn=NULL;
4287 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4288 return NotImplemented;
4290 save_state = SaveDC(graphics->hdc);
4291 EndPath(graphics->hdc);
4292 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4293 : WINDING));
4295 retval = get_clip_hrgn(graphics, &hrgn);
4297 if (retval != Ok)
4298 goto end;
4300 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4302 gdi_transform_acquire(graphics);
4304 BeginPath(graphics->hdc);
4305 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4306 path->pathdata.Types, path->pathdata.Count, FALSE);
4308 if(retval == Ok)
4310 EndPath(graphics->hdc);
4311 retval = brush_fill_path(graphics, brush);
4314 gdi_transform_release(graphics);
4316 end:
4317 RestoreDC(graphics->hdc, save_state);
4318 DeleteObject(hrgn);
4320 return retval;
4323 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4325 GpStatus stat;
4326 GpRegion *rgn;
4328 if (!brush_can_fill_pixels(brush))
4329 return NotImplemented;
4331 /* FIXME: This could probably be done more efficiently without regions. */
4333 stat = GdipCreateRegionPath(path, &rgn);
4335 if (stat == Ok)
4337 stat = GdipFillRegion(graphics, brush, rgn);
4339 GdipDeleteRegion(rgn);
4342 return stat;
4345 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4347 GpStatus stat = NotImplemented;
4349 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4351 if(!brush || !graphics || !path)
4352 return InvalidParameter;
4354 if(graphics->busy)
4355 return ObjectBusy;
4357 if (!path->pathdata.Count)
4358 return Ok;
4360 if (is_metafile_graphics(graphics))
4361 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4363 if (!graphics->image && !graphics->alpha_hdc)
4364 stat = GDI32_GdipFillPath(graphics, brush, path);
4366 if (stat == NotImplemented)
4367 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4369 if (stat == NotImplemented)
4371 FIXME("Not implemented for brushtype %i\n", brush->bt);
4372 stat = Ok;
4375 return stat;
4378 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4379 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4381 GpStatus stat;
4382 GpPath *path;
4383 GpRectF rect;
4385 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4386 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4388 if(!graphics || !brush)
4389 return InvalidParameter;
4391 if(graphics->busy)
4392 return ObjectBusy;
4394 if (is_metafile_graphics(graphics))
4396 set_rect(&rect, x, y, width, height);
4397 return METAFILE_FillPie((GpMetafile *)graphics->image, brush, &rect, startAngle, sweepAngle);
4400 stat = GdipCreatePath(FillModeAlternate, &path);
4402 if (stat == Ok)
4404 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4406 if (stat == Ok)
4407 stat = GdipFillPath(graphics, brush, path);
4409 GdipDeletePath(path);
4412 return stat;
4415 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4416 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4418 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4419 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4421 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4424 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4425 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4427 GpStatus stat;
4428 GpPath *path;
4430 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4432 if(!graphics || !brush || !points || !count)
4433 return InvalidParameter;
4435 if(graphics->busy)
4436 return ObjectBusy;
4438 stat = GdipCreatePath(fillMode, &path);
4440 if (stat == Ok)
4442 stat = GdipAddPathPolygon(path, points, count);
4444 if (stat == Ok)
4445 stat = GdipFillPath(graphics, brush, path);
4447 GdipDeletePath(path);
4450 return stat;
4453 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4454 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4456 GpStatus stat;
4457 GpPath *path;
4459 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4461 if(!graphics || !brush || !points || !count)
4462 return InvalidParameter;
4464 if(graphics->busy)
4465 return ObjectBusy;
4467 stat = GdipCreatePath(fillMode, &path);
4469 if (stat == Ok)
4471 stat = GdipAddPathPolygonI(path, points, count);
4473 if (stat == Ok)
4474 stat = GdipFillPath(graphics, brush, path);
4476 GdipDeletePath(path);
4479 return stat;
4482 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4483 GDIPCONST GpPointF *points, INT count)
4485 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4487 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4490 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4491 GDIPCONST GpPoint *points, INT count)
4493 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4495 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4498 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4499 REAL x, REAL y, REAL width, REAL height)
4501 GpRectF rect;
4503 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4505 set_rect(&rect, x, y, width, height);
4506 return GdipFillRectangles(graphics, brush, &rect, 1);
4509 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4510 INT x, INT y, INT width, INT height)
4512 GpRectF rect;
4514 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4516 set_rect(&rect, x, y, width, height);
4517 return GdipFillRectangles(graphics, brush, &rect, 1);
4520 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4521 INT count)
4523 GpStatus status;
4524 GpPath *path;
4526 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4528 if(!graphics || !brush || !rects || count <= 0)
4529 return InvalidParameter;
4531 if (is_metafile_graphics(graphics))
4533 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4534 /* FIXME: Add gdi32 drawing. */
4535 return status;
4538 status = GdipCreatePath(FillModeAlternate, &path);
4539 if (status != Ok) return status;
4541 status = GdipAddPathRectangles(path, rects, count);
4542 if (status == Ok)
4543 status = GdipFillPath(graphics, brush, path);
4545 GdipDeletePath(path);
4546 return status;
4549 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4550 INT count)
4552 GpRectF *rectsF;
4553 GpStatus ret;
4554 INT i;
4556 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4558 if(!rects || count <= 0)
4559 return InvalidParameter;
4561 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4562 if(!rectsF)
4563 return OutOfMemory;
4565 for(i = 0; i < count; i++)
4566 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4568 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4569 heap_free(rectsF);
4571 return ret;
4574 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4575 GpRegion* region)
4577 INT save_state;
4578 GpStatus status;
4579 HRGN hrgn;
4580 RECT rc;
4582 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4583 return NotImplemented;
4585 save_state = SaveDC(graphics->hdc);
4586 EndPath(graphics->hdc);
4588 hrgn = NULL;
4589 status = get_clip_hrgn(graphics, &hrgn);
4590 if (status != Ok)
4592 RestoreDC(graphics->hdc, save_state);
4593 return status;
4596 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4597 DeleteObject(hrgn);
4599 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4600 if (status != Ok)
4602 RestoreDC(graphics->hdc, save_state);
4603 return status;
4606 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4607 DeleteObject(hrgn);
4609 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4611 BeginPath(graphics->hdc);
4612 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4613 EndPath(graphics->hdc);
4615 status = brush_fill_path(graphics, brush);
4618 RestoreDC(graphics->hdc, save_state);
4621 return status;
4624 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4625 GpRegion* region)
4627 GpStatus stat;
4628 GpRegion *temp_region;
4629 GpMatrix world_to_device;
4630 GpRectF graphics_bounds;
4631 DWORD *pixel_data;
4632 HRGN hregion;
4633 RECT bound_rect;
4634 GpRect gp_bound_rect;
4636 if (!brush_can_fill_pixels(brush))
4637 return NotImplemented;
4639 stat = gdi_transform_acquire(graphics);
4641 if (stat == Ok)
4642 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4644 if (stat == Ok)
4645 stat = GdipCloneRegion(region, &temp_region);
4647 if (stat == Ok)
4649 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4650 CoordinateSpaceWorld, &world_to_device);
4652 if (stat == Ok)
4653 stat = GdipTransformRegion(temp_region, &world_to_device);
4655 if (stat == Ok)
4656 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4658 if (stat == Ok)
4659 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4661 GdipDeleteRegion(temp_region);
4664 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4666 DeleteObject(hregion);
4667 gdi_transform_release(graphics);
4668 return Ok;
4671 if (stat == Ok)
4673 gp_bound_rect.X = bound_rect.left;
4674 gp_bound_rect.Y = bound_rect.top;
4675 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4676 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4678 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4679 if (!pixel_data)
4680 stat = OutOfMemory;
4682 if (stat == Ok)
4684 stat = brush_fill_pixels(graphics, brush, pixel_data,
4685 &gp_bound_rect, gp_bound_rect.Width);
4687 if (stat == Ok)
4688 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4689 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4690 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4691 PixelFormat32bppARGB);
4693 heap_free(pixel_data);
4696 DeleteObject(hregion);
4699 gdi_transform_release(graphics);
4701 return stat;
4704 /*****************************************************************************
4705 * GdipFillRegion [GDIPLUS.@]
4707 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4708 GpRegion* region)
4710 GpStatus stat = NotImplemented;
4712 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4714 if (!(graphics && brush && region))
4715 return InvalidParameter;
4717 if(graphics->busy)
4718 return ObjectBusy;
4720 if (is_metafile_graphics(graphics))
4721 stat = METAFILE_FillRegion((GpMetafile*)graphics->image, brush, region);
4722 else
4724 if (!graphics->image && !graphics->alpha_hdc)
4725 stat = GDI32_GdipFillRegion(graphics, brush, region);
4727 if (stat == NotImplemented)
4728 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4731 if (stat == NotImplemented)
4733 FIXME("not implemented for brushtype %i\n", brush->bt);
4734 stat = Ok;
4737 return stat;
4740 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4742 TRACE("(%p,%u)\n", graphics, intention);
4744 if(!graphics)
4745 return InvalidParameter;
4747 if(graphics->busy)
4748 return ObjectBusy;
4750 /* We have no internal operation queue, so there's no need to clear it. */
4752 if (graphics->hdc)
4753 GdiFlush();
4755 return Ok;
4758 /*****************************************************************************
4759 * GdipGetClipBounds [GDIPLUS.@]
4761 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4763 GpStatus status;
4764 GpRegion *clip;
4766 TRACE("(%p, %p)\n", graphics, rect);
4768 if(!graphics)
4769 return InvalidParameter;
4771 if(graphics->busy)
4772 return ObjectBusy;
4774 status = GdipCreateRegion(&clip);
4775 if (status != Ok) return status;
4777 status = GdipGetClip(graphics, clip);
4778 if (status == Ok)
4779 status = GdipGetRegionBounds(clip, graphics, rect);
4781 GdipDeleteRegion(clip);
4782 return status;
4785 /*****************************************************************************
4786 * GdipGetClipBoundsI [GDIPLUS.@]
4788 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4790 GpRectF rectf;
4791 GpStatus stat;
4793 TRACE("(%p, %p)\n", graphics, rect);
4795 if (!rect)
4796 return InvalidParameter;
4798 if ((stat = GdipGetClipBounds(graphics, &rectf)) == Ok)
4800 rect->X = gdip_round(rectf.X);
4801 rect->Y = gdip_round(rectf.Y);
4802 rect->Width = gdip_round(rectf.Width);
4803 rect->Height = gdip_round(rectf.Height);
4806 return stat;
4809 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4810 CompositingMode *mode)
4812 TRACE("(%p, %p)\n", graphics, mode);
4814 if(!graphics || !mode)
4815 return InvalidParameter;
4817 if(graphics->busy)
4818 return ObjectBusy;
4820 *mode = graphics->compmode;
4822 return Ok;
4825 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4826 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4827 CompositingQuality *quality)
4829 TRACE("(%p, %p)\n", graphics, quality);
4831 if(!graphics || !quality)
4832 return InvalidParameter;
4834 if(graphics->busy)
4835 return ObjectBusy;
4837 *quality = graphics->compqual;
4839 return Ok;
4842 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4843 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4844 InterpolationMode *mode)
4846 TRACE("(%p, %p)\n", graphics, mode);
4848 if(!graphics || !mode)
4849 return InvalidParameter;
4851 if(graphics->busy)
4852 return ObjectBusy;
4854 *mode = graphics->interpolation;
4856 return Ok;
4859 /* FIXME: Need to handle color depths less than 24bpp */
4860 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4862 TRACE("(%p, %p)\n", graphics, argb);
4864 if(!graphics || !argb)
4865 return InvalidParameter;
4867 if(graphics->busy)
4868 return ObjectBusy;
4870 if (graphics->image && graphics->image->type == ImageTypeBitmap)
4872 static int once;
4873 GpBitmap *bitmap = (GpBitmap *)graphics->image;
4874 if (IsIndexedPixelFormat(bitmap->format) && !once++)
4875 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4878 return Ok;
4881 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4883 TRACE("(%p, %p)\n", graphics, scale);
4885 if(!graphics || !scale)
4886 return InvalidParameter;
4888 if(graphics->busy)
4889 return ObjectBusy;
4891 *scale = graphics->scale;
4893 return Ok;
4896 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4898 TRACE("(%p, %p)\n", graphics, unit);
4900 if(!graphics || !unit)
4901 return InvalidParameter;
4903 if(graphics->busy)
4904 return ObjectBusy;
4906 *unit = graphics->unit;
4908 return Ok;
4911 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4912 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4913 *mode)
4915 TRACE("(%p, %p)\n", graphics, mode);
4917 if(!graphics || !mode)
4918 return InvalidParameter;
4920 if(graphics->busy)
4921 return ObjectBusy;
4923 *mode = graphics->pixeloffset;
4925 return Ok;
4928 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4929 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4931 TRACE("(%p, %p)\n", graphics, mode);
4933 if(!graphics || !mode)
4934 return InvalidParameter;
4936 if(graphics->busy)
4937 return ObjectBusy;
4939 *mode = graphics->smoothing;
4941 return Ok;
4944 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4946 TRACE("(%p, %p)\n", graphics, contrast);
4948 if(!graphics || !contrast)
4949 return InvalidParameter;
4951 *contrast = graphics->textcontrast;
4953 return Ok;
4956 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4957 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4958 TextRenderingHint *hint)
4960 TRACE("(%p, %p)\n", graphics, hint);
4962 if(!graphics || !hint)
4963 return InvalidParameter;
4965 if(graphics->busy)
4966 return ObjectBusy;
4968 *hint = graphics->texthint;
4970 return Ok;
4973 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4975 GpRegion *clip_rgn;
4976 GpStatus stat;
4977 GpMatrix device_to_world;
4979 TRACE("(%p, %p)\n", graphics, rect);
4981 if(!graphics || !rect)
4982 return InvalidParameter;
4984 if(graphics->busy)
4985 return ObjectBusy;
4987 /* intersect window and graphics clipping regions */
4988 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4989 return stat;
4991 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4992 goto cleanup;
4994 /* transform to world coordinates */
4995 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4996 goto cleanup;
4998 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4999 goto cleanup;
5001 /* get bounds of the region */
5002 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
5004 cleanup:
5005 GdipDeleteRegion(clip_rgn);
5007 return stat;
5010 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
5012 GpRectF rectf;
5013 GpStatus stat;
5015 TRACE("(%p, %p)\n", graphics, rect);
5017 if(!graphics || !rect)
5018 return InvalidParameter;
5020 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
5022 rect->X = gdip_round(rectf.X);
5023 rect->Y = gdip_round(rectf.Y);
5024 rect->Width = gdip_round(rectf.Width);
5025 rect->Height = gdip_round(rectf.Height);
5028 return stat;
5031 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5033 TRACE("(%p, %p)\n", graphics, matrix);
5035 if(!graphics || !matrix)
5036 return InvalidParameter;
5038 if(graphics->busy)
5039 return ObjectBusy;
5041 *matrix = graphics->worldtrans;
5042 return Ok;
5045 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
5047 GpSolidFill *brush;
5048 GpStatus stat;
5049 GpRectF wnd_rect;
5050 CompositingMode prev_comp_mode;
5052 TRACE("(%p, %lx)\n", graphics, color);
5054 if(!graphics)
5055 return InvalidParameter;
5057 if(graphics->busy)
5058 return ObjectBusy;
5060 if (is_metafile_graphics(graphics))
5061 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
5063 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
5064 return stat;
5066 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
5067 GdipDeleteBrush((GpBrush*)brush);
5068 return stat;
5071 GdipGetCompositingMode(graphics, &prev_comp_mode);
5072 GdipSetCompositingMode(graphics, CompositingModeSourceCopy);
5073 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
5074 wnd_rect.Width, wnd_rect.Height);
5075 GdipSetCompositingMode(graphics, prev_comp_mode);
5077 GdipDeleteBrush((GpBrush*)brush);
5079 return Ok;
5082 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
5084 TRACE("(%p, %p)\n", graphics, res);
5086 if(!graphics || !res)
5087 return InvalidParameter;
5089 return GdipIsEmptyRegion(graphics->clip, graphics, res);
5092 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
5094 GpStatus stat;
5095 GpRegion* rgn;
5096 GpPointF pt;
5098 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
5100 if(!graphics || !result)
5101 return InvalidParameter;
5103 if(graphics->busy)
5104 return ObjectBusy;
5106 pt.X = x;
5107 pt.Y = y;
5108 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5109 CoordinateSpaceWorld, &pt, 1)) != Ok)
5110 return stat;
5112 if((stat = GdipCreateRegion(&rgn)) != Ok)
5113 return stat;
5115 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5116 goto cleanup;
5118 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
5120 cleanup:
5121 GdipDeleteRegion(rgn);
5122 return stat;
5125 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
5127 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
5130 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
5132 GpStatus stat;
5133 GpRegion* rgn;
5134 GpPointF pts[2];
5136 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
5138 if(!graphics || !result)
5139 return InvalidParameter;
5141 if(graphics->busy)
5142 return ObjectBusy;
5144 pts[0].X = x;
5145 pts[0].Y = y;
5146 pts[1].X = x + width;
5147 pts[1].Y = y + height;
5149 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5150 CoordinateSpaceWorld, pts, 2)) != Ok)
5151 return stat;
5153 pts[1].X -= pts[0].X;
5154 pts[1].Y -= pts[0].Y;
5156 if((stat = GdipCreateRegion(&rgn)) != Ok)
5157 return stat;
5159 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5160 goto cleanup;
5162 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5164 cleanup:
5165 GdipDeleteRegion(rgn);
5166 return stat;
5169 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5171 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5174 GpStatus gdip_format_string(HDC hdc,
5175 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5176 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5177 gdip_format_string_callback callback, void *user_data)
5179 WCHAR* stringdup;
5180 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5181 nheight, lineend, lineno = 0;
5182 RectF bounds;
5183 StringAlignment halign;
5184 GpStatus stat = Ok;
5185 SIZE size;
5186 HotkeyPrefix hkprefix;
5187 INT *hotkeyprefix_offsets=NULL;
5188 INT hotkeyprefix_count=0;
5189 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5190 BOOL seen_prefix = FALSE;
5192 if(length == -1) length = lstrlenW(string);
5194 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5195 if(!stringdup) return OutOfMemory;
5197 if (!format)
5198 format = &default_drawstring_format;
5200 nwidth = rect->Width;
5201 nheight = rect->Height;
5202 if (ignore_empty_clip)
5204 if (!nwidth) nwidth = INT_MAX;
5205 if (!nheight) nheight = INT_MAX;
5208 hkprefix = format->hkprefix;
5210 if (hkprefix == HotkeyPrefixShow)
5212 for (i=0; i<length; i++)
5214 if (string[i] == '&')
5215 hotkeyprefix_count++;
5219 if (hotkeyprefix_count)
5221 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5222 if (!hotkeyprefix_offsets)
5224 heap_free(stringdup);
5225 return OutOfMemory;
5229 hotkeyprefix_count = 0;
5231 for(i = 0, j = 0; i < length; i++){
5233 /* FIXME: tabs should be handled using tabstops from stringformat */
5234 if (string[i] == '\t')
5235 continue;
5237 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5238 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5239 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5241 seen_prefix = TRUE;
5242 continue;
5245 seen_prefix = FALSE;
5247 stringdup[j] = string[i];
5248 j++;
5251 length = j;
5253 halign = format->align;
5255 while(sum < length){
5256 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5257 nwidth, &fit, NULL, &size);
5258 fitcpy = fit;
5260 if(fit == 0)
5261 break;
5263 for(lret = 0; lret < fit; lret++)
5264 if(*(stringdup + sum + lret) == '\n')
5265 break;
5267 /* Line break code (may look strange, but it imitates windows). */
5268 if(lret < fit)
5269 lineend = fit = lret; /* this is not an off-by-one error */
5270 else if(fit < (length - sum)){
5271 if(*(stringdup + sum + fit) == ' ')
5272 while(*(stringdup + sum + fit) == ' ')
5273 fit++;
5274 else if (!(format->attr & StringFormatFlagsNoWrap))
5275 while(*(stringdup + sum + fit - 1) != ' '){
5276 fit--;
5278 if(*(stringdup + sum + fit) == '\t')
5279 break;
5281 if(fit == 0){
5282 fit = fitcpy;
5283 break;
5286 lineend = fit;
5287 while(*(stringdup + sum + lineend - 1) == ' ' ||
5288 *(stringdup + sum + lineend - 1) == '\t')
5289 lineend--;
5291 else
5292 lineend = fit;
5294 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5295 nwidth, &j, NULL, &size);
5297 bounds.Width = size.cx;
5299 if(height + size.cy > nheight)
5301 if (format->attr & StringFormatFlagsLineLimit)
5302 break;
5303 bounds.Height = nheight - (height + size.cy);
5305 else
5306 bounds.Height = size.cy;
5308 bounds.Y = rect->Y + height;
5310 switch (halign)
5312 case StringAlignmentNear:
5313 default:
5314 bounds.X = rect->X;
5315 break;
5316 case StringAlignmentCenter:
5317 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5318 break;
5319 case StringAlignmentFar:
5320 bounds.X = rect->X + rect->Width - bounds.Width;
5321 break;
5324 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5325 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5326 break;
5328 stat = callback(hdc, stringdup, sum, lineend,
5329 font, rect, format, lineno, &bounds,
5330 &hotkeyprefix_offsets[hotkeyprefix_pos],
5331 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5333 if (stat != Ok)
5334 break;
5336 sum += fit + (lret < fitcpy ? 1 : 0);
5337 height += size.cy;
5338 lineno++;
5340 hotkeyprefix_pos = hotkeyprefix_end_pos;
5342 if(height > nheight)
5343 break;
5345 /* Stop if this was a linewrap (but not if it was a linebreak). */
5346 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5347 break;
5350 heap_free(stringdup);
5351 heap_free(hotkeyprefix_offsets);
5353 return stat;
5356 struct measure_ranges_args {
5357 GpRegion **regions;
5358 REAL rel_width, rel_height;
5361 static GpStatus measure_ranges_callback(HDC hdc,
5362 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5363 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5364 INT lineno, const RectF *bounds, INT *underlined_indexes,
5365 INT underlined_index_count, void *user_data)
5367 int i;
5368 GpStatus stat = Ok;
5369 struct measure_ranges_args *args = user_data;
5371 for (i=0; i<format->range_count; i++)
5373 INT range_start = max(index, format->character_ranges[i].First);
5374 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5375 if (range_start < range_end)
5377 GpRectF range_rect;
5378 SIZE range_size;
5380 range_rect.Y = bounds->Y / args->rel_height;
5381 range_rect.Height = bounds->Height / args->rel_height;
5383 GetTextExtentExPointW(hdc, string + index, range_start - index,
5384 INT_MAX, NULL, NULL, &range_size);
5385 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5387 GetTextExtentExPointW(hdc, string + index, range_end - index,
5388 INT_MAX, NULL, NULL, &range_size);
5389 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5391 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5392 if (stat != Ok)
5393 break;
5397 return stat;
5400 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5401 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5402 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5403 INT regionCount, GpRegion** regions)
5405 GpStatus stat;
5406 int i;
5407 HFONT gdifont, oldfont;
5408 struct measure_ranges_args args;
5409 HDC hdc, temp_hdc=NULL;
5410 GpPointF pt[3];
5411 RectF scaled_rect;
5412 REAL margin_x;
5414 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_wn(string, length),
5415 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5417 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5418 return InvalidParameter;
5420 if (regionCount < stringFormat->range_count)
5421 return InvalidParameter;
5423 if(!graphics->hdc)
5425 hdc = temp_hdc = CreateCompatibleDC(0);
5426 if (!temp_hdc) return OutOfMemory;
5428 else
5429 hdc = graphics->hdc;
5431 if (stringFormat->attr)
5432 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5434 pt[0].X = 0.0;
5435 pt[0].Y = 0.0;
5436 pt[1].X = 1.0;
5437 pt[1].Y = 0.0;
5438 pt[2].X = 0.0;
5439 pt[2].Y = 1.0;
5440 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5441 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5442 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5443 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5444 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5446 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5447 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5449 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5450 scaled_rect.Y = layoutRect->Y * args.rel_height;
5451 scaled_rect.Width = layoutRect->Width * args.rel_width;
5452 scaled_rect.Height = layoutRect->Height * args.rel_height;
5454 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5455 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5457 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL, NULL);
5458 oldfont = SelectObject(hdc, gdifont);
5460 for (i=0; i<stringFormat->range_count; i++)
5462 stat = GdipSetEmpty(regions[i]);
5463 if (stat != Ok)
5465 SelectObject(hdc, oldfont);
5466 DeleteObject(gdifont);
5467 if (temp_hdc)
5468 DeleteDC(temp_hdc);
5469 return stat;
5473 args.regions = regions;
5475 gdi_transform_acquire(graphics);
5477 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5478 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5480 gdi_transform_release(graphics);
5482 SelectObject(hdc, oldfont);
5483 DeleteObject(gdifont);
5485 if (temp_hdc)
5486 DeleteDC(temp_hdc);
5488 return stat;
5491 struct measure_string_args {
5492 RectF *bounds;
5493 INT *codepointsfitted;
5494 INT *linesfilled;
5495 REAL rel_width, rel_height;
5498 static GpStatus measure_string_callback(HDC hdc,
5499 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5500 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5501 INT lineno, const RectF *bounds, INT *underlined_indexes,
5502 INT underlined_index_count, void *user_data)
5504 struct measure_string_args *args = user_data;
5505 REAL new_width, new_height;
5507 new_width = bounds->Width / args->rel_width;
5508 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5510 if (new_width > args->bounds->Width)
5511 args->bounds->Width = new_width;
5513 if (new_height > args->bounds->Height)
5514 args->bounds->Height = new_height;
5516 if (args->codepointsfitted)
5517 *args->codepointsfitted = index + length;
5519 if (args->linesfilled)
5520 (*args->linesfilled)++;
5522 return Ok;
5525 /* Find the smallest rectangle that bounds the text when it is printed in rect
5526 * according to the format options listed in format. If rect has 0 width and
5527 * height, then just find the smallest rectangle that bounds the text when it's
5528 * printed at location (rect->X, rect-Y). */
5529 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5530 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5531 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5532 INT *codepointsfitted, INT *linesfilled)
5534 HFONT oldfont, gdifont;
5535 struct measure_string_args args;
5536 HDC temp_hdc=NULL, hdc;
5537 GpPointF pt[3];
5538 RectF scaled_rect;
5539 REAL margin_x;
5540 INT lines, glyphs;
5542 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5543 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5544 bounds, codepointsfitted, linesfilled);
5546 if(!graphics || !string || !font || !rect || !bounds)
5547 return InvalidParameter;
5549 if(!graphics->hdc)
5551 hdc = temp_hdc = CreateCompatibleDC(0);
5552 if (!temp_hdc) return OutOfMemory;
5554 else
5555 hdc = graphics->hdc;
5557 if(linesfilled) *linesfilled = 0;
5558 if(codepointsfitted) *codepointsfitted = 0;
5560 if(format)
5561 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5563 pt[0].X = 0.0;
5564 pt[0].Y = 0.0;
5565 pt[1].X = 1.0;
5566 pt[1].Y = 0.0;
5567 pt[2].X = 0.0;
5568 pt[2].Y = 1.0;
5569 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5570 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5571 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5572 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5573 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5575 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5576 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5578 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5579 scaled_rect.Y = rect->Y * args.rel_height;
5580 scaled_rect.Width = rect->Width * args.rel_width;
5581 scaled_rect.Height = rect->Height * args.rel_height;
5582 if (scaled_rect.Width >= 0.5)
5584 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5585 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5588 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5589 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5591 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5592 oldfont = SelectObject(hdc, gdifont);
5594 set_rect(bounds, rect->X, rect->Y, 0.0f, 0.0f);
5596 args.bounds = bounds;
5597 args.codepointsfitted = &glyphs;
5598 args.linesfilled = &lines;
5599 lines = glyphs = 0;
5601 gdi_transform_acquire(graphics);
5603 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5604 measure_string_callback, &args);
5606 gdi_transform_release(graphics);
5608 if (linesfilled) *linesfilled = lines;
5609 if (codepointsfitted) *codepointsfitted = glyphs;
5611 if (lines)
5612 bounds->Width += margin_x * 2.0;
5614 SelectObject(hdc, oldfont);
5615 DeleteObject(gdifont);
5617 if (temp_hdc)
5618 DeleteDC(temp_hdc);
5620 return Ok;
5623 struct draw_string_args {
5624 GpGraphics *graphics;
5625 GDIPCONST GpBrush *brush;
5626 REAL x, y, rel_width, rel_height, ascent;
5629 static GpStatus draw_string_callback(HDC hdc,
5630 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5631 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5632 INT lineno, const RectF *bounds, INT *underlined_indexes,
5633 INT underlined_index_count, void *user_data)
5635 struct draw_string_args *args = user_data;
5636 PointF position;
5637 GpStatus stat;
5639 position.X = args->x + bounds->X / args->rel_width;
5640 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5642 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5643 args->brush, &position,
5644 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5646 if (stat == Ok && underlined_index_count)
5648 OUTLINETEXTMETRICW otm;
5649 REAL underline_y, underline_height;
5650 int i;
5652 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5654 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5655 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5657 for (i=0; i<underlined_index_count; i++)
5659 REAL start_x, end_x;
5660 SIZE text_size;
5661 INT ofs = underlined_indexes[i] - index;
5663 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5664 start_x = text_size.cx / args->rel_width;
5666 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5667 end_x = text_size.cx / args->rel_width;
5669 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5673 return stat;
5676 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5677 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5678 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5680 HRGN rgn = NULL;
5681 HFONT gdifont;
5682 GpPointF pt[3], rectcpy[4];
5683 POINT corners[4];
5684 REAL rel_width, rel_height, margin_x;
5685 INT save_state, format_flags = 0;
5686 REAL offsety = 0.0;
5687 struct draw_string_args args;
5688 RectF scaled_rect;
5689 HDC hdc, temp_hdc=NULL;
5690 TEXTMETRICW textmetric;
5692 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5693 length, font, debugstr_rectf(rect), format, brush);
5695 if(!graphics || !string || !font || !brush || !rect)
5696 return InvalidParameter;
5698 if(graphics->hdc)
5700 hdc = graphics->hdc;
5702 else
5704 hdc = temp_hdc = CreateCompatibleDC(0);
5707 if(format){
5708 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5710 format_flags = format->attr;
5712 /* Should be no need to explicitly test for StringAlignmentNear as
5713 * that is default behavior if no alignment is passed. */
5714 if(format->line_align != StringAlignmentNear){
5715 RectF bounds, in_rect = *rect;
5716 in_rect.Height = 0.0; /* avoid height clipping */
5717 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5719 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5721 if(format->line_align == StringAlignmentCenter)
5722 offsety = (rect->Height - bounds.Height) / 2;
5723 else if(format->line_align == StringAlignmentFar)
5724 offsety = (rect->Height - bounds.Height);
5726 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5729 save_state = SaveDC(hdc);
5731 pt[0].X = 0.0;
5732 pt[0].Y = 0.0;
5733 pt[1].X = 1.0;
5734 pt[1].Y = 0.0;
5735 pt[2].X = 0.0;
5736 pt[2].Y = 1.0;
5737 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5738 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5739 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5740 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5741 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5743 rectcpy[3].X = rectcpy[0].X = rect->X;
5744 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5745 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5746 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5747 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5748 round_points(corners, rectcpy, 4);
5750 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5751 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5753 scaled_rect.X = margin_x * rel_width;
5754 scaled_rect.Y = 0.0;
5755 scaled_rect.Width = rel_width * rect->Width;
5756 scaled_rect.Height = rel_height * rect->Height;
5757 if (scaled_rect.Width >= 0.5)
5759 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5760 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5763 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5764 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5766 if (!(format_flags & StringFormatFlagsNoClip) &&
5767 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5768 rect->Width > 0.0 && rect->Height > 0.0)
5770 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5771 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5772 SelectClipRgn(hdc, rgn);
5775 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5776 SelectObject(hdc, gdifont);
5778 args.graphics = graphics;
5779 args.brush = brush;
5781 args.x = rect->X;
5782 args.y = rect->Y + offsety;
5784 args.rel_width = rel_width;
5785 args.rel_height = rel_height;
5787 gdi_transform_acquire(graphics);
5789 GetTextMetricsW(hdc, &textmetric);
5790 args.ascent = textmetric.tmAscent / rel_height;
5792 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5793 draw_string_callback, &args);
5795 gdi_transform_release(graphics);
5797 DeleteObject(rgn);
5798 DeleteObject(gdifont);
5800 RestoreDC(hdc, save_state);
5802 DeleteDC(temp_hdc);
5804 return Ok;
5807 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5809 GpStatus stat;
5811 TRACE("(%p)\n", graphics);
5813 if(!graphics)
5814 return InvalidParameter;
5816 if(graphics->busy)
5817 return ObjectBusy;
5819 if (is_metafile_graphics(graphics))
5821 stat = METAFILE_ResetClip((GpMetafile *)graphics->image);
5822 if (stat != Ok)
5823 return stat;
5826 return GdipSetInfinite(graphics->clip);
5829 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5831 GpStatus stat;
5833 TRACE("(%p)\n", graphics);
5835 if(!graphics)
5836 return InvalidParameter;
5838 if(graphics->busy)
5839 return ObjectBusy;
5841 if (is_metafile_graphics(graphics))
5843 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5845 if (stat != Ok)
5846 return stat;
5849 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5852 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5853 GpMatrixOrder order)
5855 GpStatus stat;
5857 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5859 if(!graphics)
5860 return InvalidParameter;
5862 if(graphics->busy)
5863 return ObjectBusy;
5865 if (is_metafile_graphics(graphics))
5867 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5869 if (stat != Ok)
5870 return stat;
5873 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5876 static GpStatus begin_container(GpGraphics *graphics,
5877 GraphicsContainerType type, GraphicsContainer *state)
5879 GraphicsContainerItem *container;
5880 GpStatus sts;
5882 if(!graphics || !state)
5883 return InvalidParameter;
5885 sts = init_container(&container, graphics, type);
5886 if(sts != Ok)
5887 return sts;
5889 list_add_head(&graphics->containers, &container->entry);
5890 *state = graphics->contid = container->contid;
5892 if (is_metafile_graphics(graphics)) {
5893 if (type == BEGIN_CONTAINER)
5894 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5895 else
5896 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5899 return Ok;
5902 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5904 TRACE("(%p, %p)\n", graphics, state);
5905 return begin_container(graphics, SAVE_GRAPHICS, state);
5908 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5909 GraphicsContainer *state)
5911 TRACE("(%p, %p)\n", graphics, state);
5912 return begin_container(graphics, BEGIN_CONTAINER, state);
5915 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5917 GraphicsContainerItem *container;
5918 GpMatrix transform;
5919 GpStatus stat;
5920 GpRectF scaled_srcrect;
5921 REAL scale_x, scale_y;
5923 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5925 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5926 return InvalidParameter;
5928 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5929 if(stat != Ok)
5930 return stat;
5932 list_add_head(&graphics->containers, &container->entry);
5933 *state = graphics->contid = container->contid;
5935 scale_x = units_to_pixels(1.0, unit, graphics->xres, graphics->printer_display);
5936 scale_y = units_to_pixels(1.0, unit, graphics->yres, graphics->printer_display);
5938 scaled_srcrect.X = scale_x * srcrect->X;
5939 scaled_srcrect.Y = scale_y * srcrect->Y;
5940 scaled_srcrect.Width = scale_x * srcrect->Width;
5941 scaled_srcrect.Height = scale_y * srcrect->Height;
5943 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5944 transform.matrix[1] = 0.0;
5945 transform.matrix[2] = 0.0;
5946 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5947 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5948 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5950 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5952 if (is_metafile_graphics(graphics))
5953 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5955 return Ok;
5958 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5960 GpRectF dstrectf, srcrectf;
5962 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5964 if (!dstrect || !srcrect)
5965 return InvalidParameter;
5967 dstrectf.X = dstrect->X;
5968 dstrectf.Y = dstrect->Y;
5969 dstrectf.Width = dstrect->Width;
5970 dstrectf.Height = dstrect->Height;
5972 srcrectf.X = srcrect->X;
5973 srcrectf.Y = srcrect->Y;
5974 srcrectf.Width = srcrect->Width;
5975 srcrectf.Height = srcrect->Height;
5977 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5980 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5982 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5983 return NotImplemented;
5986 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5987 GraphicsContainer state)
5989 GpStatus sts;
5990 GraphicsContainerItem *container, *container2;
5992 if(!graphics)
5993 return InvalidParameter;
5995 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5996 if(container->contid == state && container->type == type)
5997 break;
6000 /* did not find a matching container */
6001 if(&container->entry == &graphics->containers)
6002 return Ok;
6004 sts = restore_container(graphics, container);
6005 if(sts != Ok)
6006 return sts;
6008 /* remove all of the containers on top of the found container */
6009 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
6010 if(container->contid == state)
6011 break;
6012 list_remove(&container->entry);
6013 delete_container(container);
6016 list_remove(&container->entry);
6017 delete_container(container);
6019 if (is_metafile_graphics(graphics)) {
6020 if (type == BEGIN_CONTAINER)
6021 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
6022 else
6023 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
6026 return Ok;
6029 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
6031 TRACE("(%p, %x)\n", graphics, state);
6032 return end_container(graphics, BEGIN_CONTAINER, state);
6035 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
6037 TRACE("(%p, %x)\n", graphics, state);
6038 return end_container(graphics, SAVE_GRAPHICS, state);
6041 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
6042 REAL sy, GpMatrixOrder order)
6044 GpStatus stat;
6046 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
6048 if(!graphics)
6049 return InvalidParameter;
6051 if(graphics->busy)
6052 return ObjectBusy;
6054 if (is_metafile_graphics(graphics)) {
6055 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
6057 if (stat != Ok)
6058 return stat;
6061 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
6064 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
6065 CombineMode mode)
6067 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
6069 if(!graphics || !srcgraphics)
6070 return InvalidParameter;
6072 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
6075 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
6076 CompositingMode mode)
6078 TRACE("(%p, %d)\n", graphics, mode);
6080 if(!graphics)
6081 return InvalidParameter;
6083 if(graphics->busy)
6084 return ObjectBusy;
6086 if(graphics->compmode == mode)
6087 return Ok;
6089 if (is_metafile_graphics(graphics))
6091 GpStatus stat;
6093 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6094 EmfPlusRecordTypeSetCompositingMode, mode);
6095 if(stat != Ok)
6096 return stat;
6099 graphics->compmode = mode;
6101 return Ok;
6104 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
6105 CompositingQuality quality)
6107 TRACE("(%p, %d)\n", graphics, quality);
6109 if(!graphics)
6110 return InvalidParameter;
6112 if(graphics->busy)
6113 return ObjectBusy;
6115 if(graphics->compqual == quality)
6116 return Ok;
6118 if (is_metafile_graphics(graphics))
6120 GpStatus stat;
6122 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6123 EmfPlusRecordTypeSetCompositingQuality, quality);
6124 if(stat != Ok)
6125 return stat;
6128 graphics->compqual = quality;
6130 return Ok;
6133 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
6134 InterpolationMode mode)
6136 TRACE("(%p, %d)\n", graphics, mode);
6138 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
6139 return InvalidParameter;
6141 if(graphics->busy)
6142 return ObjectBusy;
6144 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
6145 mode = InterpolationModeBilinear;
6147 if (mode == InterpolationModeHighQuality)
6148 mode = InterpolationModeHighQualityBicubic;
6150 if (mode == graphics->interpolation)
6151 return Ok;
6153 if (is_metafile_graphics(graphics))
6155 GpStatus stat;
6157 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6158 EmfPlusRecordTypeSetInterpolationMode, mode);
6159 if (stat != Ok)
6160 return stat;
6163 graphics->interpolation = mode;
6165 return Ok;
6168 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6170 GpStatus stat;
6172 TRACE("(%p, %.2f)\n", graphics, scale);
6174 if(!graphics || (scale <= 0.0))
6175 return InvalidParameter;
6177 if(graphics->busy)
6178 return ObjectBusy;
6180 if (is_metafile_graphics(graphics))
6182 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6183 if (stat != Ok)
6184 return stat;
6187 graphics->scale = scale;
6189 return Ok;
6192 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6194 GpStatus stat;
6196 TRACE("(%p, %d)\n", graphics, unit);
6198 if(!graphics)
6199 return InvalidParameter;
6201 if(graphics->busy)
6202 return ObjectBusy;
6204 if(unit == UnitWorld)
6205 return InvalidParameter;
6207 if (is_metafile_graphics(graphics))
6209 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6210 if (stat != Ok)
6211 return stat;
6214 graphics->unit = unit;
6216 return Ok;
6219 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6220 mode)
6222 TRACE("(%p, %d)\n", graphics, mode);
6224 if(!graphics)
6225 return InvalidParameter;
6227 if(graphics->busy)
6228 return ObjectBusy;
6230 if(graphics->pixeloffset == mode)
6231 return Ok;
6233 if (is_metafile_graphics(graphics))
6235 GpStatus stat;
6237 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6238 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6239 if(stat != Ok)
6240 return stat;
6243 graphics->pixeloffset = mode;
6245 return Ok;
6248 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6250 GpStatus stat;
6252 TRACE("(%p,%i,%i)\n", graphics, x, y);
6254 if (!graphics)
6255 return InvalidParameter;
6257 if (graphics->origin_x == x && graphics->origin_y == y)
6258 return Ok;
6260 if (is_metafile_graphics(graphics))
6262 stat = METAFILE_SetRenderingOrigin((GpMetafile *)graphics->image, x, y);
6263 if (stat != Ok)
6264 return stat;
6267 graphics->origin_x = x;
6268 graphics->origin_y = y;
6270 return Ok;
6273 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6275 TRACE("(%p,%p,%p)\n", graphics, x, y);
6277 if (!graphics || !x || !y)
6278 return InvalidParameter;
6280 *x = graphics->origin_x;
6281 *y = graphics->origin_y;
6283 return Ok;
6286 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6288 TRACE("(%p, %d)\n", graphics, mode);
6290 if(!graphics)
6291 return InvalidParameter;
6293 if(graphics->busy)
6294 return ObjectBusy;
6296 if(graphics->smoothing == mode)
6297 return Ok;
6299 if (is_metafile_graphics(graphics))
6301 GpStatus stat;
6302 BOOL antialias = (mode != SmoothingModeDefault &&
6303 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6305 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6306 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6307 if(stat != Ok)
6308 return stat;
6311 graphics->smoothing = mode;
6313 return Ok;
6316 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6318 TRACE("(%p, %d)\n", graphics, contrast);
6320 if(!graphics)
6321 return InvalidParameter;
6323 graphics->textcontrast = contrast;
6325 return Ok;
6328 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6329 TextRenderingHint hint)
6331 TRACE("(%p, %d)\n", graphics, hint);
6333 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6334 return InvalidParameter;
6336 if(graphics->busy)
6337 return ObjectBusy;
6339 if(graphics->texthint == hint)
6340 return Ok;
6342 if (is_metafile_graphics(graphics)) {
6343 GpStatus stat;
6345 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6346 EmfPlusRecordTypeSetTextRenderingHint, hint);
6347 if(stat != Ok)
6348 return stat;
6351 graphics->texthint = hint;
6353 return Ok;
6356 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6358 GpStatus stat;
6360 TRACE("(%p, %p)\n", graphics, matrix);
6362 if(!graphics || !matrix)
6363 return InvalidParameter;
6365 if(graphics->busy)
6366 return ObjectBusy;
6368 TRACE("%f,%f,%f,%f,%f,%f\n",
6369 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6370 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6372 if (is_metafile_graphics(graphics)) {
6373 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6375 if (stat != Ok)
6376 return stat;
6379 graphics->worldtrans = *matrix;
6381 return Ok;
6384 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6385 REAL dy, GpMatrixOrder order)
6387 GpStatus stat;
6389 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6391 if(!graphics)
6392 return InvalidParameter;
6394 if(graphics->busy)
6395 return ObjectBusy;
6397 if (is_metafile_graphics(graphics)) {
6398 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6400 if (stat != Ok)
6401 return stat;
6404 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6407 /*****************************************************************************
6408 * GdipSetClipHrgn [GDIPLUS.@]
6410 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6412 GpRegion *region;
6413 GpStatus status;
6414 GpMatrix transform;
6416 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6418 if(!graphics)
6419 return InvalidParameter;
6421 if(graphics->busy)
6422 return ObjectBusy;
6424 /* hrgn is in gdi32 device units */
6425 status = GdipCreateRegionHrgn(hrgn, &region);
6427 if (status == Ok)
6429 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6431 if (status == Ok)
6432 status = GdipTransformRegion(region, &transform);
6434 if (status == Ok)
6435 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6437 GdipDeleteRegion(region);
6439 return status;
6442 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6444 GpStatus status;
6445 GpPath *clip_path;
6447 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6449 if(!graphics)
6450 return InvalidParameter;
6452 if(graphics->busy)
6453 return ObjectBusy;
6455 if (is_metafile_graphics(graphics))
6457 status = METAFILE_SetClipPath((GpMetafile*)graphics->image, path, mode);
6458 if (status != Ok)
6459 return status;
6462 status = GdipClonePath(path, &clip_path);
6463 if (status == Ok)
6465 GpMatrix world_to_device;
6467 get_graphics_transform(graphics, CoordinateSpaceDevice,
6468 CoordinateSpaceWorld, &world_to_device);
6469 status = GdipTransformPath(clip_path, &world_to_device);
6470 if (status == Ok)
6471 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6473 GdipDeletePath(clip_path);
6475 return status;
6478 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6479 REAL width, REAL height,
6480 CombineMode mode)
6482 GpStatus status;
6483 GpRectF rect;
6484 GpRegion *region;
6486 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6488 if(!graphics)
6489 return InvalidParameter;
6491 if(graphics->busy)
6492 return ObjectBusy;
6494 if (is_metafile_graphics(graphics))
6496 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6497 if (status != Ok)
6498 return status;
6501 set_rect(&rect, x, y, width, height);
6502 status = GdipCreateRegionRect(&rect, &region);
6503 if (status == Ok)
6505 GpMatrix world_to_device;
6506 BOOL identity;
6508 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6509 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6510 if (status == Ok && !identity)
6511 status = GdipTransformRegion(region, &world_to_device);
6512 if (status == Ok)
6513 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6515 GdipDeleteRegion(region);
6517 return status;
6520 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6521 INT width, INT height,
6522 CombineMode mode)
6524 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6526 if(!graphics)
6527 return InvalidParameter;
6529 if(graphics->busy)
6530 return ObjectBusy;
6532 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6535 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6536 CombineMode mode)
6538 GpStatus status;
6539 GpRegion *clip;
6541 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6543 if(!graphics || !region)
6544 return InvalidParameter;
6546 if(graphics->busy)
6547 return ObjectBusy;
6549 if (is_metafile_graphics(graphics))
6551 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6552 if (status != Ok)
6553 return status;
6556 status = GdipCloneRegion(region, &clip);
6557 if (status == Ok)
6559 GpMatrix world_to_device;
6560 BOOL identity;
6562 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6563 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6564 if (status == Ok && !identity)
6565 status = GdipTransformRegion(clip, &world_to_device);
6566 if (status == Ok)
6567 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6569 GdipDeleteRegion(clip);
6571 return status;
6574 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6575 INT count)
6577 GpStatus status;
6578 GpPath* path;
6580 TRACE("(%p, %p, %d)\n", graphics, points, count);
6582 if(!graphics || !pen || count<=0)
6583 return InvalidParameter;
6585 if(graphics->busy)
6586 return ObjectBusy;
6588 status = GdipCreatePath(FillModeAlternate, &path);
6589 if (status != Ok) return status;
6591 status = GdipAddPathPolygon(path, points, count);
6592 if (status == Ok)
6593 status = GdipDrawPath(graphics, pen, path);
6595 GdipDeletePath(path);
6597 return status;
6600 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6601 INT count)
6603 GpStatus ret;
6604 GpPointF *ptf;
6605 INT i;
6607 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6609 if(count<=0) return InvalidParameter;
6610 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6611 if (!ptf) return OutOfMemory;
6613 for(i = 0;i < count; i++){
6614 ptf[i].X = (REAL)points[i].X;
6615 ptf[i].Y = (REAL)points[i].Y;
6618 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6619 heap_free(ptf);
6621 return ret;
6624 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6626 TRACE("(%p, %p)\n", graphics, dpi);
6628 if(!graphics || !dpi)
6629 return InvalidParameter;
6631 if(graphics->busy)
6632 return ObjectBusy;
6634 *dpi = graphics->xres;
6635 return Ok;
6638 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6640 TRACE("(%p, %p)\n", graphics, dpi);
6642 if(!graphics || !dpi)
6643 return InvalidParameter;
6645 if(graphics->busy)
6646 return ObjectBusy;
6648 *dpi = graphics->yres;
6649 return Ok;
6652 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6653 GpMatrixOrder order)
6655 GpMatrix m;
6656 GpStatus ret;
6658 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6660 if(!graphics || !matrix)
6661 return InvalidParameter;
6663 if(graphics->busy)
6664 return ObjectBusy;
6666 if (is_metafile_graphics(graphics))
6668 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6670 if (ret != Ok)
6671 return ret;
6674 m = graphics->worldtrans;
6676 ret = GdipMultiplyMatrix(&m, matrix, order);
6677 if(ret == Ok)
6678 graphics->worldtrans = m;
6680 return ret;
6683 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6684 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6686 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6688 GpStatus stat=Ok;
6690 TRACE("(%p, %p)\n", graphics, hdc);
6692 if(!graphics || !hdc)
6693 return InvalidParameter;
6695 if(graphics->busy)
6696 return ObjectBusy;
6698 if (is_metafile_graphics(graphics))
6700 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6702 else if (!graphics->hdc ||
6703 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6705 /* Create a fake HDC and fill it with a constant color. */
6706 HDC temp_hdc;
6707 HBITMAP hbitmap;
6708 GpRectF bounds;
6709 BITMAPINFOHEADER bmih;
6710 int i;
6712 stat = get_graphics_bounds(graphics, &bounds);
6713 if (stat != Ok)
6714 return stat;
6716 graphics->temp_hbitmap_width = bounds.Width;
6717 graphics->temp_hbitmap_height = bounds.Height;
6719 bmih.biSize = sizeof(bmih);
6720 bmih.biWidth = graphics->temp_hbitmap_width;
6721 bmih.biHeight = -graphics->temp_hbitmap_height;
6722 bmih.biPlanes = 1;
6723 bmih.biBitCount = 32;
6724 bmih.biCompression = BI_RGB;
6725 bmih.biSizeImage = 0;
6726 bmih.biXPelsPerMeter = 0;
6727 bmih.biYPelsPerMeter = 0;
6728 bmih.biClrUsed = 0;
6729 bmih.biClrImportant = 0;
6731 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6732 (void**)&graphics->temp_bits, NULL, 0);
6733 if (!hbitmap)
6734 return GenericError;
6736 if (!graphics->temp_hdc)
6738 temp_hdc = CreateCompatibleDC(0);
6740 else
6742 temp_hdc = graphics->temp_hdc;
6745 if (!temp_hdc)
6747 DeleteObject(hbitmap);
6748 return GenericError;
6751 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6752 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6754 SelectObject(temp_hdc, hbitmap);
6756 graphics->temp_hbitmap = hbitmap;
6757 *hdc = graphics->temp_hdc = temp_hdc;
6759 else
6761 *hdc = graphics->hdc;
6764 if (stat == Ok)
6765 graphics->busy = TRUE;
6767 return stat;
6770 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6772 GpStatus stat=Ok;
6774 TRACE("(%p, %p)\n", graphics, hdc);
6776 if(!graphics || !hdc || !graphics->busy)
6777 return InvalidParameter;
6779 if (is_metafile_graphics(graphics))
6781 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6783 else if (graphics->temp_hdc == hdc)
6785 DWORD* pos;
6786 int i;
6788 /* Find the pixels that have changed, and mark them as opaque. */
6789 pos = (DWORD*)graphics->temp_bits;
6790 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6792 if (*pos != DC_BACKGROUND_KEY)
6794 *pos |= 0xff000000;
6796 pos++;
6799 /* Write the changed pixels to the real target. */
6800 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6801 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6802 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6804 /* Clean up. */
6805 DeleteObject(graphics->temp_hbitmap);
6806 graphics->temp_hbitmap = NULL;
6808 else if (hdc != graphics->hdc)
6810 stat = InvalidParameter;
6813 if (stat == Ok)
6814 graphics->busy = FALSE;
6816 return stat;
6819 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6821 GpRegion *clip;
6822 GpStatus status;
6823 GpMatrix device_to_world;
6825 TRACE("(%p, %p)\n", graphics, region);
6827 if(!graphics || !region)
6828 return InvalidParameter;
6830 if(graphics->busy)
6831 return ObjectBusy;
6833 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6834 return status;
6836 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6837 status = GdipTransformRegion(clip, &device_to_world);
6838 if (status != Ok)
6840 GdipDeleteRegion(clip);
6841 return status;
6844 /* free everything except root node and header */
6845 delete_element(&region->node);
6846 memcpy(region, clip, sizeof(GpRegion));
6847 heap_free(clip);
6849 return Ok;
6852 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6854 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6856 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6857 ModifyWorldTransform(graphics->hdc, NULL, MWT_IDENTITY);
6858 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6859 SetMapMode(graphics->hdc, MM_TEXT);
6860 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6861 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6863 graphics->gdi_transform_acquire_count++;
6864 return Ok;
6867 GpStatus gdi_transform_release(GpGraphics *graphics)
6869 if (graphics->gdi_transform_acquire_count <= 0)
6871 ERR("called without matching gdi_transform_acquire\n");
6872 return GenericError;
6874 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6876 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6878 graphics->gdi_transform_acquire_count--;
6879 return Ok;
6882 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6883 GpCoordinateSpace src_space, GpMatrix *matrix)
6885 GpStatus stat = Ok;
6886 REAL scale_x, scale_y;
6888 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6890 if (dst_space != src_space)
6892 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres, graphics->printer_display);
6893 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres, graphics->printer_display);
6895 if(graphics->unit != UnitDisplay)
6897 scale_x *= graphics->scale;
6898 scale_y *= graphics->scale;
6901 if (dst_space < src_space)
6903 /* transform towards world space */
6904 switch ((int)src_space)
6906 case WineCoordinateSpaceGdiDevice:
6908 GpMatrix gdixform;
6909 gdixform = graphics->gdi_transform;
6910 stat = GdipInvertMatrix(&gdixform);
6911 if (stat != Ok)
6912 break;
6913 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6914 if (dst_space == CoordinateSpaceDevice)
6915 break;
6916 /* else fall-through */
6918 case CoordinateSpaceDevice:
6919 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6920 if (dst_space == CoordinateSpacePage)
6921 break;
6922 /* else fall-through */
6923 case CoordinateSpacePage:
6925 GpMatrix inverted_transform = graphics->worldtrans;
6926 stat = GdipInvertMatrix(&inverted_transform);
6927 if (stat == Ok)
6928 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6929 break;
6933 else
6935 /* transform towards device space */
6936 switch ((int)src_space)
6938 case CoordinateSpaceWorld:
6939 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6940 if (dst_space == CoordinateSpacePage)
6941 break;
6942 /* else fall-through */
6943 case CoordinateSpacePage:
6944 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6945 if (dst_space == CoordinateSpaceDevice)
6946 break;
6947 /* else fall-through */
6948 case CoordinateSpaceDevice:
6950 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6951 break;
6956 return stat;
6959 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6960 GpCoordinateSpace src_space, GpPointF *points, INT count)
6962 GpMatrix matrix;
6963 GpStatus stat;
6965 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6966 if (stat != Ok) return stat;
6968 return GdipTransformMatrixPoints(&matrix, points, count);
6971 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6972 GpCoordinateSpace src_space, GpPointF *points, INT count)
6974 if(!graphics || !points || count <= 0 ||
6975 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6976 src_space < 0 || src_space > CoordinateSpaceDevice)
6977 return InvalidParameter;
6979 if(graphics->busy)
6980 return ObjectBusy;
6982 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6984 if (src_space == dst_space) return Ok;
6986 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6989 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6990 GpCoordinateSpace src_space, GpPoint *points, INT count)
6992 GpPointF *pointsF;
6993 GpStatus ret;
6994 INT i;
6996 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6998 if(count <= 0)
6999 return InvalidParameter;
7001 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
7002 if(!pointsF)
7003 return OutOfMemory;
7005 for(i = 0; i < count; i++){
7006 pointsF[i].X = (REAL)points[i].X;
7007 pointsF[i].Y = (REAL)points[i].Y;
7010 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
7012 if(ret == Ok)
7013 for(i = 0; i < count; i++){
7014 points[i].X = gdip_round(pointsF[i].X);
7015 points[i].Y = gdip_round(pointsF[i].Y);
7017 heap_free(pointsF);
7019 return ret;
7022 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
7024 static int calls;
7026 TRACE("\n");
7028 if (!calls++)
7029 FIXME("stub\n");
7031 return NULL;
7034 /*****************************************************************************
7035 * GdipTranslateClip [GDIPLUS.@]
7037 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
7039 GpStatus stat;
7041 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
7043 if(!graphics)
7044 return InvalidParameter;
7046 if(graphics->busy)
7047 return ObjectBusy;
7049 if (is_metafile_graphics(graphics))
7051 stat = METAFILE_OffsetClip((GpMetafile *)graphics->image, dx, dy);
7052 if (stat != Ok)
7053 return stat;
7056 return GdipTranslateRegion(graphics->clip, dx, dy);
7059 /*****************************************************************************
7060 * GdipTranslateClipI [GDIPLUS.@]
7062 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
7064 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
7066 return GdipTranslateClip(graphics, dx, dy);
7069 /*****************************************************************************
7070 * GdipMeasureDriverString [GDIPLUS.@]
7072 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7073 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
7074 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
7076 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7077 HFONT hfont;
7078 HDC hdc;
7079 REAL min_x, min_y, max_x, max_y, x, y;
7080 int i;
7081 TEXTMETRICW textmetric;
7082 const WORD *glyph_indices;
7083 WORD *dynamic_glyph_indices=NULL;
7084 REAL rel_width, rel_height, ascent, descent;
7085 GpPointF pt[3];
7087 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
7089 if (!graphics || !text || !font || !positions || !boundingBox)
7090 return InvalidParameter;
7092 if (length == -1)
7093 length = lstrlenW(text);
7095 if (length == 0)
7096 set_rect(boundingBox, 0.0f, 0.0f, 0.0f, 0.0f);
7098 if (flags & unsupported_flags)
7099 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7101 get_font_hfont(graphics, font, NULL, &hfont, NULL, matrix);
7103 hdc = CreateCompatibleDC(0);
7104 SelectObject(hdc, hfont);
7106 GetTextMetricsW(hdc, &textmetric);
7108 pt[0].X = 0.0;
7109 pt[0].Y = 0.0;
7110 pt[1].X = 1.0;
7111 pt[1].Y = 0.0;
7112 pt[2].X = 0.0;
7113 pt[2].Y = 1.0;
7114 if (matrix)
7116 GpMatrix xform = *matrix;
7117 GdipTransformMatrixPoints(&xform, pt, 3);
7119 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
7120 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
7121 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
7122 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
7123 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
7125 if (flags & DriverStringOptionsCmapLookup)
7127 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
7128 if (!glyph_indices)
7130 DeleteDC(hdc);
7131 DeleteObject(hfont);
7132 return OutOfMemory;
7135 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
7137 else
7138 glyph_indices = text;
7140 min_x = max_x = x = positions[0].X;
7141 min_y = max_y = y = positions[0].Y;
7143 ascent = textmetric.tmAscent / rel_height;
7144 descent = textmetric.tmDescent / rel_height;
7146 for (i=0; i<length; i++)
7148 int char_width;
7149 ABC abc;
7151 if (!(flags & DriverStringOptionsRealizedAdvance))
7153 x = positions[i].X;
7154 y = positions[i].Y;
7157 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
7158 char_width = abc.abcA + abc.abcB + abc.abcC;
7160 if (min_y > y - ascent) min_y = y - ascent;
7161 if (max_y < y + descent) max_y = y + descent;
7162 if (min_x > x) min_x = x;
7164 x += char_width / rel_width;
7166 if (max_x < x) max_x = x;
7169 heap_free(dynamic_glyph_indices);
7170 DeleteDC(hdc);
7171 DeleteObject(hfont);
7173 boundingBox->X = min_x;
7174 boundingBox->Y = min_y;
7175 boundingBox->Width = max_x - min_x;
7176 boundingBox->Height = max_y - min_y;
7178 return Ok;
7181 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7182 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7183 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7184 INT flags, GDIPCONST GpMatrix *matrix)
7186 INT save_state;
7187 GpPointF pt, *real_positions=NULL;
7188 INT *eto_positions=NULL;
7189 HFONT hfont;
7190 LOGFONTW lfw;
7191 UINT eto_flags=0;
7192 GpStatus status;
7193 HRGN hrgn;
7195 if (!(flags & DriverStringOptionsCmapLookup))
7196 eto_flags |= ETO_GLYPH_INDEX;
7198 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7200 real_positions = heap_alloc(sizeof(*real_positions) * length);
7201 eto_positions = heap_alloc(sizeof(*eto_positions) * 2 * (length - 1));
7202 if (!real_positions || !eto_positions)
7204 heap_free(real_positions);
7205 heap_free(eto_positions);
7206 return OutOfMemory;
7210 save_state = SaveDC(graphics->hdc);
7211 SetBkMode(graphics->hdc, TRANSPARENT);
7212 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7214 status = get_clip_hrgn(graphics, &hrgn);
7216 if (status == Ok)
7218 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7219 DeleteObject(hrgn);
7222 pt = positions[0];
7223 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7225 get_font_hfont(graphics, font, format, &hfont, &lfw, matrix);
7227 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7229 GpMatrix rotation;
7230 INT i;
7232 eto_flags |= ETO_PDY;
7234 memcpy(real_positions, positions, sizeof(PointF) * length);
7236 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7238 GdipSetMatrixElements(&rotation, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
7239 GdipRotateMatrix(&rotation, lfw.lfEscapement / 10.0, MatrixOrderAppend);
7240 GdipTransformMatrixPoints(&rotation, real_positions, length);
7242 for (i = 0; i < (length - 1); i++)
7244 eto_positions[i*2] = gdip_round(real_positions[i+1].X) - gdip_round(real_positions[i].X);
7245 eto_positions[i*2+1] = gdip_round(real_positions[i].Y) - gdip_round(real_positions[i+1].Y);
7249 SelectObject(graphics->hdc, hfont);
7251 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7253 gdi_transform_acquire(graphics);
7255 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, eto_positions);
7257 gdi_transform_release(graphics);
7259 RestoreDC(graphics->hdc, save_state);
7261 DeleteObject(hfont);
7263 heap_free(real_positions);
7264 heap_free(eto_positions);
7266 return Ok;
7269 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7270 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7271 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7272 INT flags, GDIPCONST GpMatrix *matrix)
7274 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7275 GpStatus stat;
7276 PointF *real_positions, real_position;
7277 POINT *pti;
7278 HFONT hfont;
7279 HDC hdc;
7280 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7281 DWORD max_glyphsize=0;
7282 GLYPHMETRICS glyphmetrics;
7283 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7284 BYTE *glyph_mask;
7285 BYTE *text_mask;
7286 int text_mask_stride;
7287 BYTE *pixel_data;
7288 int pixel_data_stride;
7289 GpRect pixel_area;
7290 UINT ggo_flags = GGO_GRAY8_BITMAP;
7292 if (length <= 0)
7293 return Ok;
7295 if (!(flags & DriverStringOptionsCmapLookup))
7296 ggo_flags |= GGO_GLYPH_INDEX;
7298 if (flags & unsupported_flags)
7299 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7301 pti = heap_alloc_zero(sizeof(POINT) * length);
7302 if (!pti)
7303 return OutOfMemory;
7305 if (flags & DriverStringOptionsRealizedAdvance)
7307 real_position = positions[0];
7309 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7310 round_points(pti, &real_position, 1);
7312 else
7314 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7315 if (!real_positions)
7317 heap_free(pti);
7318 return OutOfMemory;
7321 memcpy(real_positions, positions, sizeof(PointF) * length);
7323 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7324 round_points(pti, real_positions, length);
7326 heap_free(real_positions);
7329 get_font_hfont(graphics, font, format, &hfont, NULL, matrix);
7331 hdc = CreateCompatibleDC(0);
7332 SelectObject(hdc, hfont);
7334 /* Get the boundaries of the text to be drawn */
7335 for (i=0; i<length; i++)
7337 DWORD glyphsize;
7338 int left, top, right, bottom;
7340 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7341 &glyphmetrics, 0, NULL, &identity);
7343 if (glyphsize == GDI_ERROR)
7345 ERR("GetGlyphOutlineW failed\n");
7346 heap_free(pti);
7347 DeleteDC(hdc);
7348 DeleteObject(hfont);
7349 return GenericError;
7352 if (glyphsize > max_glyphsize)
7353 max_glyphsize = glyphsize;
7355 if (glyphsize != 0)
7357 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7358 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7359 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7360 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7362 if (left < min_x) min_x = left;
7363 if (top < min_y) min_y = top;
7364 if (right > max_x) max_x = right;
7365 if (bottom > max_y) max_y = bottom;
7368 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7370 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7371 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7375 if (max_glyphsize == 0)
7377 /* Nothing to draw. */
7378 heap_free(pti);
7379 DeleteDC(hdc);
7380 DeleteObject(hfont);
7381 return Ok;
7384 glyph_mask = heap_alloc_zero(max_glyphsize);
7385 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7386 text_mask_stride = max_x - min_x;
7388 if (!(glyph_mask && text_mask))
7390 heap_free(glyph_mask);
7391 heap_free(text_mask);
7392 heap_free(pti);
7393 DeleteDC(hdc);
7394 DeleteObject(hfont);
7395 return OutOfMemory;
7398 /* Generate a mask for the text */
7399 for (i=0; i<length; i++)
7401 DWORD ret;
7402 int left, top, stride;
7404 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7405 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7407 if (ret == GDI_ERROR || ret == 0)
7408 continue; /* empty glyph */
7410 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7411 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7412 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7414 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7416 BYTE *glyph_val = glyph_mask + y * stride;
7417 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7418 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7420 *text_val = min(64, *text_val + *glyph_val);
7421 glyph_val++;
7422 text_val++;
7427 heap_free(pti);
7428 DeleteDC(hdc);
7429 DeleteObject(hfont);
7430 heap_free(glyph_mask);
7432 /* get the brush data */
7433 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7434 if (!pixel_data)
7436 heap_free(text_mask);
7437 return OutOfMemory;
7440 pixel_area.X = min_x;
7441 pixel_area.Y = min_y;
7442 pixel_area.Width = max_x - min_x;
7443 pixel_area.Height = max_y - min_y;
7444 pixel_data_stride = pixel_area.Width * 4;
7446 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7447 if (stat != Ok)
7449 heap_free(text_mask);
7450 heap_free(pixel_data);
7451 return stat;
7454 /* multiply the brush data by the mask */
7455 for (y=0; y<pixel_area.Height; y++)
7457 BYTE *text_val = text_mask + text_mask_stride * y;
7458 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7459 for (x=0; x<pixel_area.Width; x++)
7461 *pixel_val = (*pixel_val) * (*text_val) / 64;
7462 text_val++;
7463 pixel_val+=4;
7467 heap_free(text_mask);
7469 gdi_transform_acquire(graphics);
7471 /* draw the result */
7472 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7473 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7475 gdi_transform_release(graphics);
7477 heap_free(pixel_data);
7479 return stat;
7482 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7483 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7484 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7485 INT flags, GDIPCONST GpMatrix *matrix)
7487 GpStatus stat = NotImplemented;
7489 if (length == -1)
7490 length = lstrlenW(text);
7492 if (is_metafile_graphics(graphics))
7493 return METAFILE_DrawDriverString((GpMetafile*)graphics->image, text, length, font,
7494 format, brush, positions, flags, matrix);
7496 if (graphics->hdc && !graphics->alpha_hdc &&
7497 brush->bt == BrushTypeSolidColor &&
7498 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7499 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7500 brush, positions, flags, matrix);
7501 if (stat == NotImplemented)
7502 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7503 brush, positions, flags, matrix);
7504 return stat;
7507 /*****************************************************************************
7508 * GdipDrawDriverString [GDIPLUS.@]
7510 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7511 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7512 GDIPCONST PointF *positions, INT flags,
7513 GDIPCONST GpMatrix *matrix )
7515 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7517 if (!graphics || !text || !font || !brush || !positions)
7518 return InvalidParameter;
7520 return draw_driver_string(graphics, text, length, font, NULL,
7521 brush, positions, flags, matrix);
7524 /*****************************************************************************
7525 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7527 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7529 GpStatus stat;
7530 GpRegion* rgn;
7532 TRACE("(%p, %p)\n", graphics, res);
7534 if((stat = GdipCreateRegion(&rgn)) != Ok)
7535 return stat;
7537 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7538 goto cleanup;
7540 stat = GdipIsEmptyRegion(rgn, graphics, res);
7542 cleanup:
7543 GdipDeleteRegion(rgn);
7544 return stat;
7547 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7549 static int calls;
7551 TRACE("(%p) stub\n", graphics);
7553 if(!(calls++))
7554 FIXME("not implemented\n");
7556 return NotImplemented;
7559 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7561 TRACE("(%p, %p)\n", graphics, pabort);
7563 if (!graphics)
7564 return InvalidParameter;
7566 if (pabort)
7567 FIXME("Abort callback is not supported.\n");
7569 return Ok;