gdiplus/metafile: Implement DrawRectangles() recording.
[wine.git] / dlls / gdiplus / graphics.c
blob2c18ced3e92d87e3b3ae02941f0b7745772648ca
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("%d, ", 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;
2599 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2600 width, height, startAngle, sweepAngle);
2602 if(!graphics || !pen || width <= 0 || height <= 0)
2603 return InvalidParameter;
2605 if(graphics->busy)
2606 return ObjectBusy;
2608 status = GdipCreatePath(FillModeAlternate, &path);
2609 if (status != Ok) return status;
2611 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2612 if (status == Ok)
2613 status = GdipDrawPath(graphics, pen, path);
2615 GdipDeletePath(path);
2616 return status;
2619 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2620 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2622 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2623 width, height, startAngle, sweepAngle);
2625 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2628 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2629 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2631 GpPointF pt[4];
2633 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2634 x2, y2, x3, y3, x4, y4);
2636 if(!graphics || !pen)
2637 return InvalidParameter;
2639 if(graphics->busy)
2640 return ObjectBusy;
2642 pt[0].X = x1;
2643 pt[0].Y = y1;
2644 pt[1].X = x2;
2645 pt[1].Y = y2;
2646 pt[2].X = x3;
2647 pt[2].Y = y3;
2648 pt[3].X = x4;
2649 pt[3].Y = y4;
2650 return GdipDrawBeziers(graphics, pen, pt, 4);
2653 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2654 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2656 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2657 x2, y2, x3, y3, x4, y4);
2659 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2662 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2663 GDIPCONST GpPointF *points, INT count)
2665 GpStatus status;
2666 GpPath *path;
2668 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2670 if(!graphics || !pen || !points || (count <= 0))
2671 return InvalidParameter;
2673 if(graphics->busy)
2674 return ObjectBusy;
2676 status = GdipCreatePath(FillModeAlternate, &path);
2677 if (status != Ok) return status;
2679 status = GdipAddPathBeziers(path, points, count);
2680 if (status == Ok)
2681 status = GdipDrawPath(graphics, pen, path);
2683 GdipDeletePath(path);
2684 return status;
2687 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2688 GDIPCONST GpPoint *points, INT count)
2690 GpPointF *pts;
2691 GpStatus ret;
2692 INT i;
2694 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2696 if(!graphics || !pen || !points || (count <= 0))
2697 return InvalidParameter;
2699 if(graphics->busy)
2700 return ObjectBusy;
2702 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2703 if(!pts)
2704 return OutOfMemory;
2706 for(i = 0; i < count; i++){
2707 pts[i].X = (REAL)points[i].X;
2708 pts[i].Y = (REAL)points[i].Y;
2711 ret = GdipDrawBeziers(graphics,pen,pts,count);
2713 heap_free(pts);
2715 return ret;
2718 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2719 GDIPCONST GpPointF *points, INT count)
2721 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2723 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2726 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2727 GDIPCONST GpPoint *points, INT count)
2729 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2731 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2734 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2735 GDIPCONST GpPointF *points, INT count, REAL tension)
2737 GpPath *path;
2738 GpStatus status;
2740 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2742 if(!graphics || !pen || !points || count <= 0)
2743 return InvalidParameter;
2745 if(graphics->busy)
2746 return ObjectBusy;
2748 status = GdipCreatePath(FillModeAlternate, &path);
2749 if (status != Ok) return status;
2751 status = GdipAddPathClosedCurve2(path, points, count, tension);
2752 if (status == Ok)
2753 status = GdipDrawPath(graphics, pen, path);
2755 GdipDeletePath(path);
2757 return status;
2760 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2761 GDIPCONST GpPoint *points, INT count, REAL tension)
2763 GpPointF *ptf;
2764 GpStatus stat;
2765 INT i;
2767 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2769 if(!points || count <= 0)
2770 return InvalidParameter;
2772 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2773 if(!ptf)
2774 return OutOfMemory;
2776 for(i = 0; i < count; i++){
2777 ptf[i].X = (REAL)points[i].X;
2778 ptf[i].Y = (REAL)points[i].Y;
2781 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2783 heap_free(ptf);
2785 return stat;
2788 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2789 GDIPCONST GpPointF *points, INT count)
2791 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2793 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2796 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2797 GDIPCONST GpPoint *points, INT count)
2799 GpPointF *pointsF;
2800 GpStatus ret;
2801 INT i;
2803 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2805 if(!points)
2806 return InvalidParameter;
2808 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2809 if(!pointsF)
2810 return OutOfMemory;
2812 for(i = 0; i < count; i++){
2813 pointsF[i].X = (REAL)points[i].X;
2814 pointsF[i].Y = (REAL)points[i].Y;
2817 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2818 heap_free(pointsF);
2820 return ret;
2823 /* Approximates cardinal spline with Bezier curves. */
2824 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2825 GDIPCONST GpPointF *points, INT count, REAL tension)
2827 GpPath *path;
2828 GpStatus status;
2830 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2832 if(!graphics || !pen)
2833 return InvalidParameter;
2835 if(graphics->busy)
2836 return ObjectBusy;
2838 if(count < 2)
2839 return InvalidParameter;
2841 status = GdipCreatePath(FillModeAlternate, &path);
2842 if (status != Ok) return status;
2844 status = GdipAddPathCurve2(path, points, count, tension);
2845 if (status == Ok)
2846 status = GdipDrawPath(graphics, pen, path);
2848 GdipDeletePath(path);
2849 return status;
2852 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2853 GDIPCONST GpPoint *points, INT count, REAL tension)
2855 GpPointF *pointsF;
2856 GpStatus ret;
2857 INT i;
2859 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2861 if(!points)
2862 return InvalidParameter;
2864 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2865 if(!pointsF)
2866 return OutOfMemory;
2868 for(i = 0; i < count; i++){
2869 pointsF[i].X = (REAL)points[i].X;
2870 pointsF[i].Y = (REAL)points[i].Y;
2873 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2874 heap_free(pointsF);
2876 return ret;
2879 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2880 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2881 REAL tension)
2883 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2885 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2886 return InvalidParameter;
2889 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2892 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2893 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2894 REAL tension)
2896 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2898 if(count < 0){
2899 return OutOfMemory;
2902 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2903 return InvalidParameter;
2906 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2909 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2910 REAL y, REAL width, REAL height)
2912 GpPath *path;
2913 GpStatus status;
2914 GpRectF rect;
2916 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2918 if(!graphics || !pen)
2919 return InvalidParameter;
2921 if(graphics->busy)
2922 return ObjectBusy;
2924 if (is_metafile_graphics(graphics))
2926 rect.X = x;
2927 rect.Y = y;
2928 rect.Width = width;
2929 rect.Height = height;
2930 return METAFILE_DrawEllipse((GpMetafile *)graphics->image, pen, &rect);
2933 status = GdipCreatePath(FillModeAlternate, &path);
2934 if (status != Ok) return status;
2936 status = GdipAddPathEllipse(path, x, y, width, height);
2937 if (status == Ok)
2938 status = GdipDrawPath(graphics, pen, path);
2940 GdipDeletePath(path);
2941 return status;
2944 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2945 INT y, INT width, INT height)
2947 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2949 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2953 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2955 UINT width, height;
2957 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2959 if(!graphics || !image)
2960 return InvalidParameter;
2962 GdipGetImageWidth(image, &width);
2963 GdipGetImageHeight(image, &height);
2965 return GdipDrawImagePointRect(graphics, image, x, y,
2966 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2969 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2970 INT y)
2972 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2974 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2977 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2978 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2979 GpUnit srcUnit)
2981 GpPointF points[3];
2982 REAL scale_x, scale_y, width, height;
2984 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2986 if (!graphics || !image) return InvalidParameter;
2988 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres, graphics->printer_display);
2989 scale_x *= graphics->xres / image->xres;
2990 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres, graphics->printer_display);
2991 scale_y *= graphics->yres / image->yres;
2992 width = srcwidth * scale_x;
2993 height = srcheight * scale_y;
2995 points[0].X = points[2].X = x;
2996 points[0].Y = points[1].Y = y;
2997 points[1].X = x + width;
2998 points[2].Y = y + height;
3000 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3001 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3004 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3005 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3006 GpUnit srcUnit)
3008 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3011 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3012 GDIPCONST GpPointF *dstpoints, INT count)
3014 UINT width, height;
3016 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3018 if(!image)
3019 return InvalidParameter;
3021 GdipGetImageWidth(image, &width);
3022 GdipGetImageHeight(image, &height);
3024 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3025 width, height, UnitPixel, NULL, NULL, NULL);
3028 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3029 GDIPCONST GpPoint *dstpoints, INT count)
3031 GpPointF ptf[3];
3033 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3035 if (count != 3 || !dstpoints)
3036 return InvalidParameter;
3038 ptf[0].X = (REAL)dstpoints[0].X;
3039 ptf[0].Y = (REAL)dstpoints[0].Y;
3040 ptf[1].X = (REAL)dstpoints[1].X;
3041 ptf[1].Y = (REAL)dstpoints[1].Y;
3042 ptf[2].X = (REAL)dstpoints[2].X;
3043 ptf[2].Y = (REAL)dstpoints[2].Y;
3045 return GdipDrawImagePoints(graphics, image, ptf, count);
3048 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3049 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3051 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3052 return TRUE;
3055 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3056 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3057 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3058 DrawImageAbort callback, VOID * callbackData)
3060 GpPointF ptf[4];
3061 POINT pti[4];
3062 GpStatus stat;
3064 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3065 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3066 callbackData);
3068 if (count > 3)
3069 return NotImplemented;
3071 if(!graphics || !image || !points || count != 3)
3072 return InvalidParameter;
3074 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3075 debugstr_pointf(&points[2]));
3077 if (is_metafile_graphics(graphics))
3079 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
3080 image, points, count, srcx, srcy, srcwidth, srcheight,
3081 srcUnit, imageAttributes, callback, callbackData);
3084 memcpy(ptf, points, 3 * sizeof(GpPointF));
3086 /* Ensure source width/height is positive */
3087 if (srcwidth < 0)
3089 GpPointF tmp = ptf[1];
3090 srcx = srcx + srcwidth;
3091 srcwidth = -srcwidth;
3092 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
3093 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3094 ptf[1] = ptf[0];
3095 ptf[0] = tmp;
3098 if (srcheight < 0)
3100 GpPointF tmp = ptf[2];
3101 srcy = srcy + srcheight;
3102 srcheight = -srcheight;
3103 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
3104 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
3105 ptf[2] = ptf[0];
3106 ptf[0] = tmp;
3109 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3110 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3111 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
3112 return Ok;
3113 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
3114 round_points(pti, ptf, 4);
3116 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3117 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3119 srcx = units_to_pixels(srcx, srcUnit, image->xres, graphics->printer_display);
3120 srcy = units_to_pixels(srcy, srcUnit, image->yres, graphics->printer_display);
3121 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres, graphics->printer_display);
3122 srcheight = units_to_pixels(srcheight, srcUnit, image->yres, graphics->printer_display);
3123 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3125 if (image->type == ImageTypeBitmap)
3127 GpBitmap* bitmap = (GpBitmap*)image;
3128 BOOL do_resampling = FALSE;
3129 BOOL use_software = FALSE;
3131 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3132 graphics->xres, graphics->yres,
3133 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3134 graphics->scale, image->xres, image->yres, bitmap->format,
3135 imageAttributes ? imageAttributes->outside_color : 0);
3137 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3138 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3139 srcx < 0 || srcy < 0 ||
3140 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3141 do_resampling = TRUE;
3143 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3144 (graphics->image && graphics->image->type == ImageTypeBitmap))
3145 use_software = TRUE;
3147 if (use_software)
3149 RECT dst_area;
3150 GpRectF graphics_bounds;
3151 GpRect src_area;
3152 int i, x, y, src_stride, dst_stride;
3153 GpMatrix dst_to_src;
3154 REAL m11, m12, m21, m22, mdx, mdy;
3155 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3156 BitmapData lockeddata;
3157 InterpolationMode interpolation = graphics->interpolation;
3158 PixelOffsetMode offset_mode = graphics->pixeloffset;
3159 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3160 REAL x_dx, x_dy, y_dx, y_dy;
3161 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3163 if (!imageAttributes)
3164 imageAttributes = &defaultImageAttributes;
3166 dst_area.left = dst_area.right = pti[0].x;
3167 dst_area.top = dst_area.bottom = pti[0].y;
3168 for (i=1; i<4; i++)
3170 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3171 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3172 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3173 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3176 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3177 if (stat != Ok) return stat;
3179 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3180 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3181 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3182 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3184 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3186 if (IsRectEmpty(&dst_area)) return Ok;
3188 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3189 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3190 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3191 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3192 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3193 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3195 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3197 stat = GdipInvertMatrix(&dst_to_src);
3198 if (stat != Ok) return stat;
3200 if (do_resampling)
3202 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3203 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3205 else
3207 /* Make sure src_area is equal in size to dst_area. */
3208 src_area.X = srcx + dst_area.left - pti[0].x;
3209 src_area.Y = srcy + dst_area.top - pti[0].y;
3210 src_area.Width = dst_area.right - dst_area.left;
3211 src_area.Height = dst_area.bottom - dst_area.top;
3214 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3216 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3217 if (!src_data)
3218 return OutOfMemory;
3219 src_stride = sizeof(ARGB) * src_area.Width;
3221 /* Read the bits we need from the source bitmap into a compatible buffer. */
3222 lockeddata.Width = src_area.Width;
3223 lockeddata.Height = src_area.Height;
3224 lockeddata.Stride = src_stride;
3225 lockeddata.Scan0 = src_data;
3226 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3227 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3228 else
3229 lockeddata.PixelFormat = PixelFormat32bppARGB;
3231 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3232 lockeddata.PixelFormat, &lockeddata);
3234 if (stat == Ok)
3235 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3237 if (stat != Ok)
3239 heap_free(src_data);
3240 return stat;
3243 apply_image_attributes(imageAttributes, src_data,
3244 src_area.Width, src_area.Height,
3245 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3247 if (do_resampling)
3249 /* Transform the bits as needed to the destination. */
3250 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3251 if (!dst_data)
3253 heap_free(src_data);
3254 return OutOfMemory;
3257 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3259 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3261 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3262 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3263 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3264 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3266 for (x=dst_area.left; x<dst_area.right; x++)
3268 for (y=dst_area.top; y<dst_area.bottom; y++)
3270 GpPointF src_pointf;
3271 ARGB *dst_color;
3273 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3274 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3276 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3278 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3279 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3280 imageAttributes, interpolation, offset_mode);
3281 else
3282 *dst_color = 0;
3286 else
3288 dst_data = src_data;
3289 dst_stride = src_stride;
3292 gdi_transform_acquire(graphics);
3294 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3295 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3296 lockeddata.PixelFormat);
3298 gdi_transform_release(graphics);
3300 heap_free(src_data);
3302 heap_free(dst_dyn_data);
3304 return stat;
3306 else
3308 HDC hdc;
3309 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3310 HBITMAP hbitmap, old_hbm=NULL;
3311 HRGN hrgn;
3312 INT save_state;
3314 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3315 bitmap->format == PixelFormat24bppRGB ||
3316 bitmap->format == PixelFormat32bppRGB ||
3317 bitmap->format == PixelFormat32bppPARGB))
3319 BITMAPINFOHEADER bih;
3320 BYTE *temp_bits;
3321 PixelFormat dst_format;
3323 /* we can't draw a bitmap of this format directly */
3324 hdc = CreateCompatibleDC(0);
3325 temp_hdc = TRUE;
3326 temp_bitmap = TRUE;
3328 bih.biSize = sizeof(BITMAPINFOHEADER);
3329 bih.biWidth = bitmap->width;
3330 bih.biHeight = -bitmap->height;
3331 bih.biPlanes = 1;
3332 bih.biBitCount = 32;
3333 bih.biCompression = BI_RGB;
3334 bih.biSizeImage = 0;
3335 bih.biXPelsPerMeter = 0;
3336 bih.biYPelsPerMeter = 0;
3337 bih.biClrUsed = 0;
3338 bih.biClrImportant = 0;
3340 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3341 (void**)&temp_bits, NULL, 0);
3343 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3344 dst_format = PixelFormat32bppPARGB;
3345 else
3346 dst_format = PixelFormat32bppRGB;
3348 convert_pixels(bitmap->width, bitmap->height,
3349 bitmap->width*4, temp_bits, dst_format,
3350 bitmap->stride, bitmap->bits, bitmap->format,
3351 bitmap->image.palette);
3353 else
3355 if (bitmap->hbitmap)
3356 hbitmap = bitmap->hbitmap;
3357 else
3359 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3360 temp_bitmap = TRUE;
3363 hdc = bitmap->hdc;
3364 temp_hdc = (hdc == 0);
3367 if (temp_hdc)
3369 if (!hdc) hdc = CreateCompatibleDC(0);
3370 old_hbm = SelectObject(hdc, hbitmap);
3373 save_state = SaveDC(graphics->hdc);
3375 stat = get_clip_hrgn(graphics, &hrgn);
3377 if (stat == Ok)
3379 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3380 DeleteObject(hrgn);
3383 gdi_transform_acquire(graphics);
3385 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3387 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3388 hdc, srcx, srcy, srcwidth, srcheight);
3390 else
3392 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3393 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3396 gdi_transform_release(graphics);
3398 RestoreDC(graphics->hdc, save_state);
3400 if (temp_hdc)
3402 SelectObject(hdc, old_hbm);
3403 DeleteDC(hdc);
3406 if (temp_bitmap)
3407 DeleteObject(hbitmap);
3410 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3412 GpRectF rc;
3414 rc.X = srcx;
3415 rc.Y = srcy;
3416 rc.Width = srcwidth;
3417 rc.Height = 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 GpStatus status;
4092 GpPath *path;
4094 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
4096 if(!pen || !graphics)
4097 return InvalidParameter;
4099 if(graphics->busy)
4100 return ObjectBusy;
4102 status = GdipCreatePath(FillModeAlternate, &path);
4103 if (status != Ok) return status;
4105 status = GdipAddPathRectangle(path, x, y, width, height);
4106 if (status == Ok)
4107 status = GdipDrawPath(graphics, pen, path);
4109 GdipDeletePath(path);
4110 return status;
4113 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
4114 INT y, INT width, INT height)
4116 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
4118 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4121 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
4122 GDIPCONST GpRectF* rects, INT count)
4124 GpStatus status;
4125 GpPath *path;
4127 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4129 if(!graphics || !pen || !rects || count < 1)
4130 return InvalidParameter;
4132 if(graphics->busy)
4133 return ObjectBusy;
4135 if (is_metafile_graphics(graphics))
4136 return METAFILE_DrawRectangles((GpMetafile *)graphics->image, pen, rects, count);
4138 status = GdipCreatePath(FillModeAlternate, &path);
4139 if (status != Ok) return status;
4141 status = GdipAddPathRectangles(path, rects, count);
4142 if (status == Ok)
4143 status = GdipDrawPath(graphics, pen, path);
4145 GdipDeletePath(path);
4146 return status;
4149 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4150 GDIPCONST GpRect* rects, INT count)
4152 GpRectF *rectsF;
4153 GpStatus ret;
4154 INT i;
4156 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4158 if(!rects || count<=0)
4159 return InvalidParameter;
4161 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4162 if(!rectsF)
4163 return OutOfMemory;
4165 for(i = 0;i < count;i++){
4166 rectsF[i].X = (REAL)rects[i].X;
4167 rectsF[i].Y = (REAL)rects[i].Y;
4168 rectsF[i].Width = (REAL)rects[i].Width;
4169 rectsF[i].Height = (REAL)rects[i].Height;
4172 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4173 heap_free(rectsF);
4175 return ret;
4178 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4179 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4181 GpPath *path;
4182 GpStatus status;
4184 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4185 count, tension, fill);
4187 if(!graphics || !brush || !points)
4188 return InvalidParameter;
4190 if(graphics->busy)
4191 return ObjectBusy;
4193 if(count == 1) /* Do nothing */
4194 return Ok;
4196 status = GdipCreatePath(fill, &path);
4197 if (status != Ok) return status;
4199 status = GdipAddPathClosedCurve2(path, points, count, tension);
4200 if (status == Ok)
4201 status = GdipFillPath(graphics, brush, path);
4203 GdipDeletePath(path);
4204 return status;
4207 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4208 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4210 GpPointF *ptf;
4211 GpStatus stat;
4212 INT i;
4214 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4215 count, tension, fill);
4217 if(!points || count == 0)
4218 return InvalidParameter;
4220 if(count == 1) /* Do nothing */
4221 return Ok;
4223 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4224 if(!ptf)
4225 return OutOfMemory;
4227 for(i = 0;i < count;i++){
4228 ptf[i].X = (REAL)points[i].X;
4229 ptf[i].Y = (REAL)points[i].Y;
4232 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4234 heap_free(ptf);
4236 return stat;
4239 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4240 GDIPCONST GpPointF *points, INT count)
4242 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4243 return GdipFillClosedCurve2(graphics, brush, points, count,
4244 0.5f, FillModeAlternate);
4247 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4248 GDIPCONST GpPoint *points, INT count)
4250 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4251 return GdipFillClosedCurve2I(graphics, brush, points, count,
4252 0.5f, FillModeAlternate);
4255 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4256 REAL y, REAL width, REAL height)
4258 GpStatus stat;
4259 GpPath *path;
4261 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4263 if(!graphics || !brush)
4264 return InvalidParameter;
4266 if(graphics->busy)
4267 return ObjectBusy;
4269 if (is_metafile_graphics(graphics))
4271 GpRectF rect;
4273 rect.X = x;
4274 rect.Y = y;
4275 rect.Width = width;
4276 rect.Height = height;
4277 return METAFILE_FillEllipse((GpMetafile *)graphics->image, brush, &rect);
4280 stat = GdipCreatePath(FillModeAlternate, &path);
4282 if (stat == Ok)
4284 stat = GdipAddPathEllipse(path, x, y, width, height);
4286 if (stat == Ok)
4287 stat = GdipFillPath(graphics, brush, path);
4289 GdipDeletePath(path);
4292 return stat;
4295 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4296 INT y, INT width, INT height)
4298 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4300 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4303 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4305 INT save_state;
4306 GpStatus retval;
4307 HRGN hrgn=NULL;
4309 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4310 return NotImplemented;
4312 save_state = SaveDC(graphics->hdc);
4313 EndPath(graphics->hdc);
4314 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4315 : WINDING));
4317 retval = get_clip_hrgn(graphics, &hrgn);
4319 if (retval != Ok)
4320 goto end;
4322 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4324 gdi_transform_acquire(graphics);
4326 BeginPath(graphics->hdc);
4327 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4328 path->pathdata.Types, path->pathdata.Count, FALSE);
4330 if(retval == Ok)
4332 EndPath(graphics->hdc);
4333 retval = brush_fill_path(graphics, brush);
4336 gdi_transform_release(graphics);
4338 end:
4339 RestoreDC(graphics->hdc, save_state);
4340 DeleteObject(hrgn);
4342 return retval;
4345 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4347 GpStatus stat;
4348 GpRegion *rgn;
4350 if (!brush_can_fill_pixels(brush))
4351 return NotImplemented;
4353 /* FIXME: This could probably be done more efficiently without regions. */
4355 stat = GdipCreateRegionPath(path, &rgn);
4357 if (stat == Ok)
4359 stat = GdipFillRegion(graphics, brush, rgn);
4361 GdipDeleteRegion(rgn);
4364 return stat;
4367 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4369 GpStatus stat = NotImplemented;
4371 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4373 if(!brush || !graphics || !path)
4374 return InvalidParameter;
4376 if(graphics->busy)
4377 return ObjectBusy;
4379 if (!path->pathdata.Count)
4380 return Ok;
4382 if (is_metafile_graphics(graphics))
4383 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4385 if (!graphics->image && !graphics->alpha_hdc)
4386 stat = GDI32_GdipFillPath(graphics, brush, path);
4388 if (stat == NotImplemented)
4389 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4391 if (stat == NotImplemented)
4393 FIXME("Not implemented for brushtype %i\n", brush->bt);
4394 stat = Ok;
4397 return stat;
4400 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4401 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4403 GpStatus stat;
4404 GpPath *path;
4406 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4407 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4409 if(!graphics || !brush)
4410 return InvalidParameter;
4412 if(graphics->busy)
4413 return ObjectBusy;
4415 stat = GdipCreatePath(FillModeAlternate, &path);
4417 if (stat == Ok)
4419 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4421 if (stat == Ok)
4422 stat = GdipFillPath(graphics, brush, path);
4424 GdipDeletePath(path);
4427 return stat;
4430 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4431 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4433 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4434 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4436 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4439 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4440 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4442 GpStatus stat;
4443 GpPath *path;
4445 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4447 if(!graphics || !brush || !points || !count)
4448 return InvalidParameter;
4450 if(graphics->busy)
4451 return ObjectBusy;
4453 stat = GdipCreatePath(fillMode, &path);
4455 if (stat == Ok)
4457 stat = GdipAddPathPolygon(path, points, count);
4459 if (stat == Ok)
4460 stat = GdipFillPath(graphics, brush, path);
4462 GdipDeletePath(path);
4465 return stat;
4468 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4469 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4471 GpStatus stat;
4472 GpPath *path;
4474 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4476 if(!graphics || !brush || !points || !count)
4477 return InvalidParameter;
4479 if(graphics->busy)
4480 return ObjectBusy;
4482 stat = GdipCreatePath(fillMode, &path);
4484 if (stat == Ok)
4486 stat = GdipAddPathPolygonI(path, points, count);
4488 if (stat == Ok)
4489 stat = GdipFillPath(graphics, brush, path);
4491 GdipDeletePath(path);
4494 return stat;
4497 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4498 GDIPCONST GpPointF *points, INT count)
4500 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4502 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4505 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4506 GDIPCONST GpPoint *points, INT count)
4508 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4510 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4513 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4514 REAL x, REAL y, REAL width, REAL height)
4516 GpRectF rect;
4518 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4520 rect.X = x;
4521 rect.Y = y;
4522 rect.Width = width;
4523 rect.Height = height;
4525 return GdipFillRectangles(graphics, brush, &rect, 1);
4528 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4529 INT x, INT y, INT width, INT height)
4531 GpRectF rect;
4533 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4535 rect.X = (REAL)x;
4536 rect.Y = (REAL)y;
4537 rect.Width = (REAL)width;
4538 rect.Height = (REAL)height;
4540 return GdipFillRectangles(graphics, brush, &rect, 1);
4543 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4544 INT count)
4546 GpStatus status;
4547 GpPath *path;
4549 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4551 if(!graphics || !brush || !rects || count <= 0)
4552 return InvalidParameter;
4554 if (is_metafile_graphics(graphics))
4556 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4557 /* FIXME: Add gdi32 drawing. */
4558 return status;
4561 status = GdipCreatePath(FillModeAlternate, &path);
4562 if (status != Ok) return status;
4564 status = GdipAddPathRectangles(path, rects, count);
4565 if (status == Ok)
4566 status = GdipFillPath(graphics, brush, path);
4568 GdipDeletePath(path);
4569 return status;
4572 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4573 INT count)
4575 GpRectF *rectsF;
4576 GpStatus ret;
4577 INT i;
4579 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4581 if(!rects || count <= 0)
4582 return InvalidParameter;
4584 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4585 if(!rectsF)
4586 return OutOfMemory;
4588 for(i = 0; i < count; i++){
4589 rectsF[i].X = (REAL)rects[i].X;
4590 rectsF[i].Y = (REAL)rects[i].Y;
4591 rectsF[i].Width = (REAL)rects[i].Width;
4592 rectsF[i].Height = (REAL)rects[i].Height;
4595 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4596 heap_free(rectsF);
4598 return ret;
4601 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4602 GpRegion* region)
4604 INT save_state;
4605 GpStatus status;
4606 HRGN hrgn;
4607 RECT rc;
4609 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4610 return NotImplemented;
4612 save_state = SaveDC(graphics->hdc);
4613 EndPath(graphics->hdc);
4615 hrgn = NULL;
4616 status = get_clip_hrgn(graphics, &hrgn);
4617 if (status != Ok)
4619 RestoreDC(graphics->hdc, save_state);
4620 return status;
4623 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4624 DeleteObject(hrgn);
4626 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4627 if (status != Ok)
4629 RestoreDC(graphics->hdc, save_state);
4630 return status;
4633 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4634 DeleteObject(hrgn);
4636 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4638 BeginPath(graphics->hdc);
4639 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4640 EndPath(graphics->hdc);
4642 status = brush_fill_path(graphics, brush);
4645 RestoreDC(graphics->hdc, save_state);
4648 return status;
4651 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4652 GpRegion* region)
4654 GpStatus stat;
4655 GpRegion *temp_region;
4656 GpMatrix world_to_device;
4657 GpRectF graphics_bounds;
4658 DWORD *pixel_data;
4659 HRGN hregion;
4660 RECT bound_rect;
4661 GpRect gp_bound_rect;
4663 if (!brush_can_fill_pixels(brush))
4664 return NotImplemented;
4666 stat = gdi_transform_acquire(graphics);
4668 if (stat == Ok)
4669 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4671 if (stat == Ok)
4672 stat = GdipCloneRegion(region, &temp_region);
4674 if (stat == Ok)
4676 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4677 CoordinateSpaceWorld, &world_to_device);
4679 if (stat == Ok)
4680 stat = GdipTransformRegion(temp_region, &world_to_device);
4682 if (stat == Ok)
4683 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4685 if (stat == Ok)
4686 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4688 GdipDeleteRegion(temp_region);
4691 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4693 DeleteObject(hregion);
4694 gdi_transform_release(graphics);
4695 return Ok;
4698 if (stat == Ok)
4700 gp_bound_rect.X = bound_rect.left;
4701 gp_bound_rect.Y = bound_rect.top;
4702 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4703 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4705 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4706 if (!pixel_data)
4707 stat = OutOfMemory;
4709 if (stat == Ok)
4711 stat = brush_fill_pixels(graphics, brush, pixel_data,
4712 &gp_bound_rect, gp_bound_rect.Width);
4714 if (stat == Ok)
4715 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4716 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4717 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4718 PixelFormat32bppARGB);
4720 heap_free(pixel_data);
4723 DeleteObject(hregion);
4726 gdi_transform_release(graphics);
4728 return stat;
4731 /*****************************************************************************
4732 * GdipFillRegion [GDIPLUS.@]
4734 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4735 GpRegion* region)
4737 GpStatus stat = NotImplemented;
4739 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4741 if (!(graphics && brush && region))
4742 return InvalidParameter;
4744 if(graphics->busy)
4745 return ObjectBusy;
4747 if (is_metafile_graphics(graphics))
4748 stat = METAFILE_FillRegion((GpMetafile*)graphics->image, brush, region);
4749 else
4751 if (!graphics->image && !graphics->alpha_hdc)
4752 stat = GDI32_GdipFillRegion(graphics, brush, region);
4754 if (stat == NotImplemented)
4755 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4758 if (stat == NotImplemented)
4760 FIXME("not implemented for brushtype %i\n", brush->bt);
4761 stat = Ok;
4764 return stat;
4767 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4769 TRACE("(%p,%u)\n", graphics, intention);
4771 if(!graphics)
4772 return InvalidParameter;
4774 if(graphics->busy)
4775 return ObjectBusy;
4777 /* We have no internal operation queue, so there's no need to clear it. */
4779 if (graphics->hdc)
4780 GdiFlush();
4782 return Ok;
4785 /*****************************************************************************
4786 * GdipGetClipBounds [GDIPLUS.@]
4788 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4790 GpStatus status;
4791 GpRegion *clip;
4793 TRACE("(%p, %p)\n", graphics, rect);
4795 if(!graphics)
4796 return InvalidParameter;
4798 if(graphics->busy)
4799 return ObjectBusy;
4801 status = GdipCreateRegion(&clip);
4802 if (status != Ok) return status;
4804 status = GdipGetClip(graphics, clip);
4805 if (status == Ok)
4806 status = GdipGetRegionBounds(clip, graphics, rect);
4808 GdipDeleteRegion(clip);
4809 return status;
4812 /*****************************************************************************
4813 * GdipGetClipBoundsI [GDIPLUS.@]
4815 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4817 GpRectF rectf;
4818 GpStatus stat;
4820 TRACE("(%p, %p)\n", graphics, rect);
4822 if (!rect)
4823 return InvalidParameter;
4825 if ((stat = GdipGetClipBounds(graphics, &rectf)) == Ok)
4827 rect->X = gdip_round(rectf.X);
4828 rect->Y = gdip_round(rectf.Y);
4829 rect->Width = gdip_round(rectf.Width);
4830 rect->Height = gdip_round(rectf.Height);
4833 return stat;
4836 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4837 CompositingMode *mode)
4839 TRACE("(%p, %p)\n", graphics, mode);
4841 if(!graphics || !mode)
4842 return InvalidParameter;
4844 if(graphics->busy)
4845 return ObjectBusy;
4847 *mode = graphics->compmode;
4849 return Ok;
4852 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4853 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4854 CompositingQuality *quality)
4856 TRACE("(%p, %p)\n", graphics, quality);
4858 if(!graphics || !quality)
4859 return InvalidParameter;
4861 if(graphics->busy)
4862 return ObjectBusy;
4864 *quality = graphics->compqual;
4866 return Ok;
4869 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4870 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4871 InterpolationMode *mode)
4873 TRACE("(%p, %p)\n", graphics, mode);
4875 if(!graphics || !mode)
4876 return InvalidParameter;
4878 if(graphics->busy)
4879 return ObjectBusy;
4881 *mode = graphics->interpolation;
4883 return Ok;
4886 /* FIXME: Need to handle color depths less than 24bpp */
4887 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4889 TRACE("(%p, %p)\n", graphics, argb);
4891 if(!graphics || !argb)
4892 return InvalidParameter;
4894 if(graphics->busy)
4895 return ObjectBusy;
4897 if (graphics->image && graphics->image->type == ImageTypeBitmap)
4899 static int once;
4900 GpBitmap *bitmap = (GpBitmap *)graphics->image;
4901 if (IsIndexedPixelFormat(bitmap->format) && !once++)
4902 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4905 return Ok;
4908 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4910 TRACE("(%p, %p)\n", graphics, scale);
4912 if(!graphics || !scale)
4913 return InvalidParameter;
4915 if(graphics->busy)
4916 return ObjectBusy;
4918 *scale = graphics->scale;
4920 return Ok;
4923 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4925 TRACE("(%p, %p)\n", graphics, unit);
4927 if(!graphics || !unit)
4928 return InvalidParameter;
4930 if(graphics->busy)
4931 return ObjectBusy;
4933 *unit = graphics->unit;
4935 return Ok;
4938 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4939 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4940 *mode)
4942 TRACE("(%p, %p)\n", graphics, mode);
4944 if(!graphics || !mode)
4945 return InvalidParameter;
4947 if(graphics->busy)
4948 return ObjectBusy;
4950 *mode = graphics->pixeloffset;
4952 return Ok;
4955 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4956 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4958 TRACE("(%p, %p)\n", graphics, mode);
4960 if(!graphics || !mode)
4961 return InvalidParameter;
4963 if(graphics->busy)
4964 return ObjectBusy;
4966 *mode = graphics->smoothing;
4968 return Ok;
4971 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4973 TRACE("(%p, %p)\n", graphics, contrast);
4975 if(!graphics || !contrast)
4976 return InvalidParameter;
4978 *contrast = graphics->textcontrast;
4980 return Ok;
4983 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4984 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4985 TextRenderingHint *hint)
4987 TRACE("(%p, %p)\n", graphics, hint);
4989 if(!graphics || !hint)
4990 return InvalidParameter;
4992 if(graphics->busy)
4993 return ObjectBusy;
4995 *hint = graphics->texthint;
4997 return Ok;
5000 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
5002 GpRegion *clip_rgn;
5003 GpStatus stat;
5004 GpMatrix device_to_world;
5006 TRACE("(%p, %p)\n", graphics, rect);
5008 if(!graphics || !rect)
5009 return InvalidParameter;
5011 if(graphics->busy)
5012 return ObjectBusy;
5014 /* intersect window and graphics clipping regions */
5015 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
5016 return stat;
5018 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
5019 goto cleanup;
5021 /* transform to world coordinates */
5022 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
5023 goto cleanup;
5025 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
5026 goto cleanup;
5028 /* get bounds of the region */
5029 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
5031 cleanup:
5032 GdipDeleteRegion(clip_rgn);
5034 return stat;
5037 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
5039 GpRectF rectf;
5040 GpStatus stat;
5042 TRACE("(%p, %p)\n", graphics, rect);
5044 if(!graphics || !rect)
5045 return InvalidParameter;
5047 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
5049 rect->X = gdip_round(rectf.X);
5050 rect->Y = gdip_round(rectf.Y);
5051 rect->Width = gdip_round(rectf.Width);
5052 rect->Height = gdip_round(rectf.Height);
5055 return stat;
5058 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5060 TRACE("(%p, %p)\n", graphics, matrix);
5062 if(!graphics || !matrix)
5063 return InvalidParameter;
5065 if(graphics->busy)
5066 return ObjectBusy;
5068 *matrix = graphics->worldtrans;
5069 return Ok;
5072 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
5074 GpSolidFill *brush;
5075 GpStatus stat;
5076 GpRectF wnd_rect;
5077 CompositingMode prev_comp_mode;
5079 TRACE("(%p, %x)\n", graphics, color);
5081 if(!graphics)
5082 return InvalidParameter;
5084 if(graphics->busy)
5085 return ObjectBusy;
5087 if (is_metafile_graphics(graphics))
5088 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
5090 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
5091 return stat;
5093 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
5094 GdipDeleteBrush((GpBrush*)brush);
5095 return stat;
5098 GdipGetCompositingMode(graphics, &prev_comp_mode);
5099 GdipSetCompositingMode(graphics, CompositingModeSourceCopy);
5100 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
5101 wnd_rect.Width, wnd_rect.Height);
5102 GdipSetCompositingMode(graphics, prev_comp_mode);
5104 GdipDeleteBrush((GpBrush*)brush);
5106 return Ok;
5109 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
5111 TRACE("(%p, %p)\n", graphics, res);
5113 if(!graphics || !res)
5114 return InvalidParameter;
5116 return GdipIsEmptyRegion(graphics->clip, graphics, res);
5119 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
5121 GpStatus stat;
5122 GpRegion* rgn;
5123 GpPointF pt;
5125 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
5127 if(!graphics || !result)
5128 return InvalidParameter;
5130 if(graphics->busy)
5131 return ObjectBusy;
5133 pt.X = x;
5134 pt.Y = y;
5135 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5136 CoordinateSpaceWorld, &pt, 1)) != Ok)
5137 return stat;
5139 if((stat = GdipCreateRegion(&rgn)) != Ok)
5140 return stat;
5142 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5143 goto cleanup;
5145 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
5147 cleanup:
5148 GdipDeleteRegion(rgn);
5149 return stat;
5152 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
5154 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
5157 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
5159 GpStatus stat;
5160 GpRegion* rgn;
5161 GpPointF pts[2];
5163 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
5165 if(!graphics || !result)
5166 return InvalidParameter;
5168 if(graphics->busy)
5169 return ObjectBusy;
5171 pts[0].X = x;
5172 pts[0].Y = y;
5173 pts[1].X = x + width;
5174 pts[1].Y = y + height;
5176 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5177 CoordinateSpaceWorld, pts, 2)) != Ok)
5178 return stat;
5180 pts[1].X -= pts[0].X;
5181 pts[1].Y -= pts[0].Y;
5183 if((stat = GdipCreateRegion(&rgn)) != Ok)
5184 return stat;
5186 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5187 goto cleanup;
5189 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5191 cleanup:
5192 GdipDeleteRegion(rgn);
5193 return stat;
5196 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5198 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5201 GpStatus gdip_format_string(HDC hdc,
5202 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5203 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5204 gdip_format_string_callback callback, void *user_data)
5206 WCHAR* stringdup;
5207 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5208 nheight, lineend, lineno = 0;
5209 RectF bounds;
5210 StringAlignment halign;
5211 GpStatus stat = Ok;
5212 SIZE size;
5213 HotkeyPrefix hkprefix;
5214 INT *hotkeyprefix_offsets=NULL;
5215 INT hotkeyprefix_count=0;
5216 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5217 BOOL seen_prefix = FALSE;
5219 if(length == -1) length = lstrlenW(string);
5221 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5222 if(!stringdup) return OutOfMemory;
5224 if (!format)
5225 format = &default_drawstring_format;
5227 nwidth = rect->Width;
5228 nheight = rect->Height;
5229 if (ignore_empty_clip)
5231 if (!nwidth) nwidth = INT_MAX;
5232 if (!nheight) nheight = INT_MAX;
5235 hkprefix = format->hkprefix;
5237 if (hkprefix == HotkeyPrefixShow)
5239 for (i=0; i<length; i++)
5241 if (string[i] == '&')
5242 hotkeyprefix_count++;
5246 if (hotkeyprefix_count)
5248 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5249 if (!hotkeyprefix_offsets)
5251 heap_free(stringdup);
5252 return OutOfMemory;
5256 hotkeyprefix_count = 0;
5258 for(i = 0, j = 0; i < length; i++){
5259 /* FIXME: This makes the indexes passed to callback inaccurate. */
5260 if(!iswprint(string[i]) && (string[i] != '\n'))
5261 continue;
5263 /* FIXME: tabs should be handled using tabstops from stringformat */
5264 if (string[i] == '\t')
5265 continue;
5267 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5268 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5269 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5271 seen_prefix = TRUE;
5272 continue;
5275 seen_prefix = FALSE;
5277 stringdup[j] = string[i];
5278 j++;
5281 length = j;
5283 halign = format->align;
5285 while(sum < length){
5286 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5287 nwidth, &fit, NULL, &size);
5288 fitcpy = fit;
5290 if(fit == 0)
5291 break;
5293 for(lret = 0; lret < fit; lret++)
5294 if(*(stringdup + sum + lret) == '\n')
5295 break;
5297 /* Line break code (may look strange, but it imitates windows). */
5298 if(lret < fit)
5299 lineend = fit = lret; /* this is not an off-by-one error */
5300 else if(fit < (length - sum)){
5301 if(*(stringdup + sum + fit) == ' ')
5302 while(*(stringdup + sum + fit) == ' ')
5303 fit++;
5304 else if (!(format->attr & StringFormatFlagsNoWrap))
5305 while(*(stringdup + sum + fit - 1) != ' '){
5306 fit--;
5308 if(*(stringdup + sum + fit) == '\t')
5309 break;
5311 if(fit == 0){
5312 fit = fitcpy;
5313 break;
5316 lineend = fit;
5317 while(*(stringdup + sum + lineend - 1) == ' ' ||
5318 *(stringdup + sum + lineend - 1) == '\t')
5319 lineend--;
5321 else
5322 lineend = fit;
5324 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5325 nwidth, &j, NULL, &size);
5327 bounds.Width = size.cx;
5329 if(height + size.cy > nheight)
5331 if (format->attr & StringFormatFlagsLineLimit)
5332 break;
5333 bounds.Height = nheight - (height + size.cy);
5335 else
5336 bounds.Height = size.cy;
5338 bounds.Y = rect->Y + height;
5340 switch (halign)
5342 case StringAlignmentNear:
5343 default:
5344 bounds.X = rect->X;
5345 break;
5346 case StringAlignmentCenter:
5347 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5348 break;
5349 case StringAlignmentFar:
5350 bounds.X = rect->X + rect->Width - bounds.Width;
5351 break;
5354 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5355 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5356 break;
5358 stat = callback(hdc, stringdup, sum, lineend,
5359 font, rect, format, lineno, &bounds,
5360 &hotkeyprefix_offsets[hotkeyprefix_pos],
5361 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5363 if (stat != Ok)
5364 break;
5366 sum += fit + (lret < fitcpy ? 1 : 0);
5367 height += size.cy;
5368 lineno++;
5370 hotkeyprefix_pos = hotkeyprefix_end_pos;
5372 if(height > nheight)
5373 break;
5375 /* Stop if this was a linewrap (but not if it was a linebreak). */
5376 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5377 break;
5380 heap_free(stringdup);
5381 heap_free(hotkeyprefix_offsets);
5383 return stat;
5386 struct measure_ranges_args {
5387 GpRegion **regions;
5388 REAL rel_width, rel_height;
5391 static GpStatus measure_ranges_callback(HDC hdc,
5392 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5393 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5394 INT lineno, const RectF *bounds, INT *underlined_indexes,
5395 INT underlined_index_count, void *user_data)
5397 int i;
5398 GpStatus stat = Ok;
5399 struct measure_ranges_args *args = user_data;
5401 for (i=0; i<format->range_count; i++)
5403 INT range_start = max(index, format->character_ranges[i].First);
5404 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5405 if (range_start < range_end)
5407 GpRectF range_rect;
5408 SIZE range_size;
5410 range_rect.Y = bounds->Y / args->rel_height;
5411 range_rect.Height = bounds->Height / args->rel_height;
5413 GetTextExtentExPointW(hdc, string + index, range_start - index,
5414 INT_MAX, NULL, NULL, &range_size);
5415 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5417 GetTextExtentExPointW(hdc, string + index, range_end - index,
5418 INT_MAX, NULL, NULL, &range_size);
5419 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5421 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5422 if (stat != Ok)
5423 break;
5427 return stat;
5430 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5431 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5432 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5433 INT regionCount, GpRegion** regions)
5435 GpStatus stat;
5436 int i;
5437 HFONT gdifont, oldfont;
5438 struct measure_ranges_args args;
5439 HDC hdc, temp_hdc=NULL;
5440 GpPointF pt[3];
5441 RectF scaled_rect;
5442 REAL margin_x;
5444 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_wn(string, length),
5445 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5447 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5448 return InvalidParameter;
5450 if (regionCount < stringFormat->range_count)
5451 return InvalidParameter;
5453 if(!graphics->hdc)
5455 hdc = temp_hdc = CreateCompatibleDC(0);
5456 if (!temp_hdc) return OutOfMemory;
5458 else
5459 hdc = graphics->hdc;
5461 if (stringFormat->attr)
5462 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5464 pt[0].X = 0.0;
5465 pt[0].Y = 0.0;
5466 pt[1].X = 1.0;
5467 pt[1].Y = 0.0;
5468 pt[2].X = 0.0;
5469 pt[2].Y = 1.0;
5470 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5471 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5472 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5473 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5474 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5476 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5477 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5479 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5480 scaled_rect.Y = layoutRect->Y * args.rel_height;
5481 scaled_rect.Width = layoutRect->Width * args.rel_width;
5482 scaled_rect.Height = layoutRect->Height * args.rel_height;
5484 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5485 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5487 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL, NULL);
5488 oldfont = SelectObject(hdc, gdifont);
5490 for (i=0; i<stringFormat->range_count; i++)
5492 stat = GdipSetEmpty(regions[i]);
5493 if (stat != Ok)
5495 SelectObject(hdc, oldfont);
5496 DeleteObject(gdifont);
5497 if (temp_hdc)
5498 DeleteDC(temp_hdc);
5499 return stat;
5503 args.regions = regions;
5505 gdi_transform_acquire(graphics);
5507 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5508 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5510 gdi_transform_release(graphics);
5512 SelectObject(hdc, oldfont);
5513 DeleteObject(gdifont);
5515 if (temp_hdc)
5516 DeleteDC(temp_hdc);
5518 return stat;
5521 struct measure_string_args {
5522 RectF *bounds;
5523 INT *codepointsfitted;
5524 INT *linesfilled;
5525 REAL rel_width, rel_height;
5528 static GpStatus measure_string_callback(HDC hdc,
5529 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5530 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5531 INT lineno, const RectF *bounds, INT *underlined_indexes,
5532 INT underlined_index_count, void *user_data)
5534 struct measure_string_args *args = user_data;
5535 REAL new_width, new_height;
5537 new_width = bounds->Width / args->rel_width;
5538 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5540 if (new_width > args->bounds->Width)
5541 args->bounds->Width = new_width;
5543 if (new_height > args->bounds->Height)
5544 args->bounds->Height = new_height;
5546 if (args->codepointsfitted)
5547 *args->codepointsfitted = index + length;
5549 if (args->linesfilled)
5550 (*args->linesfilled)++;
5552 return Ok;
5555 /* Find the smallest rectangle that bounds the text when it is printed in rect
5556 * according to the format options listed in format. If rect has 0 width and
5557 * height, then just find the smallest rectangle that bounds the text when it's
5558 * printed at location (rect->X, rect-Y). */
5559 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5560 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5561 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5562 INT *codepointsfitted, INT *linesfilled)
5564 HFONT oldfont, gdifont;
5565 struct measure_string_args args;
5566 HDC temp_hdc=NULL, hdc;
5567 GpPointF pt[3];
5568 RectF scaled_rect;
5569 REAL margin_x;
5570 INT lines, glyphs;
5572 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5573 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5574 bounds, codepointsfitted, linesfilled);
5576 if(!graphics || !string || !font || !rect || !bounds)
5577 return InvalidParameter;
5579 if(!graphics->hdc)
5581 hdc = temp_hdc = CreateCompatibleDC(0);
5582 if (!temp_hdc) return OutOfMemory;
5584 else
5585 hdc = graphics->hdc;
5587 if(linesfilled) *linesfilled = 0;
5588 if(codepointsfitted) *codepointsfitted = 0;
5590 if(format)
5591 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5593 pt[0].X = 0.0;
5594 pt[0].Y = 0.0;
5595 pt[1].X = 1.0;
5596 pt[1].Y = 0.0;
5597 pt[2].X = 0.0;
5598 pt[2].Y = 1.0;
5599 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5600 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5601 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5602 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5603 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5605 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5606 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5608 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5609 scaled_rect.Y = rect->Y * args.rel_height;
5610 scaled_rect.Width = rect->Width * args.rel_width;
5611 scaled_rect.Height = rect->Height * args.rel_height;
5612 if (scaled_rect.Width >= 0.5)
5614 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5615 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5618 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5619 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5621 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5622 oldfont = SelectObject(hdc, gdifont);
5624 bounds->X = rect->X;
5625 bounds->Y = rect->Y;
5626 bounds->Width = 0.0;
5627 bounds->Height = 0.0;
5629 args.bounds = bounds;
5630 args.codepointsfitted = &glyphs;
5631 args.linesfilled = &lines;
5632 lines = glyphs = 0;
5634 gdi_transform_acquire(graphics);
5636 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5637 measure_string_callback, &args);
5639 gdi_transform_release(graphics);
5641 if (linesfilled) *linesfilled = lines;
5642 if (codepointsfitted) *codepointsfitted = glyphs;
5644 if (lines)
5645 bounds->Width += margin_x * 2.0;
5647 SelectObject(hdc, oldfont);
5648 DeleteObject(gdifont);
5650 if (temp_hdc)
5651 DeleteDC(temp_hdc);
5653 return Ok;
5656 struct draw_string_args {
5657 GpGraphics *graphics;
5658 GDIPCONST GpBrush *brush;
5659 REAL x, y, rel_width, rel_height, ascent;
5662 static GpStatus draw_string_callback(HDC hdc,
5663 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5664 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5665 INT lineno, const RectF *bounds, INT *underlined_indexes,
5666 INT underlined_index_count, void *user_data)
5668 struct draw_string_args *args = user_data;
5669 PointF position;
5670 GpStatus stat;
5672 position.X = args->x + bounds->X / args->rel_width;
5673 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5675 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5676 args->brush, &position,
5677 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5679 if (stat == Ok && underlined_index_count)
5681 OUTLINETEXTMETRICW otm;
5682 REAL underline_y, underline_height;
5683 int i;
5685 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5687 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5688 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5690 for (i=0; i<underlined_index_count; i++)
5692 REAL start_x, end_x;
5693 SIZE text_size;
5694 INT ofs = underlined_indexes[i] - index;
5696 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5697 start_x = text_size.cx / args->rel_width;
5699 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5700 end_x = text_size.cx / args->rel_width;
5702 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5706 return stat;
5709 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5710 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5711 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5713 HRGN rgn = NULL;
5714 HFONT gdifont;
5715 GpPointF pt[3], rectcpy[4];
5716 POINT corners[4];
5717 REAL rel_width, rel_height, margin_x;
5718 INT save_state, format_flags = 0;
5719 REAL offsety = 0.0;
5720 struct draw_string_args args;
5721 RectF scaled_rect;
5722 HDC hdc, temp_hdc=NULL;
5723 TEXTMETRICW textmetric;
5725 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5726 length, font, debugstr_rectf(rect), format, brush);
5728 if(!graphics || !string || !font || !brush || !rect)
5729 return InvalidParameter;
5731 if(graphics->hdc)
5733 hdc = graphics->hdc;
5735 else
5737 hdc = temp_hdc = CreateCompatibleDC(0);
5740 if(format){
5741 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5743 format_flags = format->attr;
5745 /* Should be no need to explicitly test for StringAlignmentNear as
5746 * that is default behavior if no alignment is passed. */
5747 if(format->line_align != StringAlignmentNear){
5748 RectF bounds, in_rect = *rect;
5749 in_rect.Height = 0.0; /* avoid height clipping */
5750 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5752 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5754 if(format->line_align == StringAlignmentCenter)
5755 offsety = (rect->Height - bounds.Height) / 2;
5756 else if(format->line_align == StringAlignmentFar)
5757 offsety = (rect->Height - bounds.Height);
5759 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5762 save_state = SaveDC(hdc);
5764 pt[0].X = 0.0;
5765 pt[0].Y = 0.0;
5766 pt[1].X = 1.0;
5767 pt[1].Y = 0.0;
5768 pt[2].X = 0.0;
5769 pt[2].Y = 1.0;
5770 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5771 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5772 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5773 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5774 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5776 rectcpy[3].X = rectcpy[0].X = rect->X;
5777 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5778 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5779 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5780 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5781 round_points(corners, rectcpy, 4);
5783 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5784 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5786 scaled_rect.X = margin_x * rel_width;
5787 scaled_rect.Y = 0.0;
5788 scaled_rect.Width = rel_width * rect->Width;
5789 scaled_rect.Height = rel_height * rect->Height;
5790 if (scaled_rect.Width >= 0.5)
5792 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5793 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5796 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5797 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5799 if (!(format_flags & StringFormatFlagsNoClip) &&
5800 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5801 rect->Width > 0.0 && rect->Height > 0.0)
5803 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5804 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5805 SelectClipRgn(hdc, rgn);
5808 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5809 SelectObject(hdc, gdifont);
5811 args.graphics = graphics;
5812 args.brush = brush;
5814 args.x = rect->X;
5815 args.y = rect->Y + offsety;
5817 args.rel_width = rel_width;
5818 args.rel_height = rel_height;
5820 gdi_transform_acquire(graphics);
5822 GetTextMetricsW(hdc, &textmetric);
5823 args.ascent = textmetric.tmAscent / rel_height;
5825 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5826 draw_string_callback, &args);
5828 gdi_transform_release(graphics);
5830 DeleteObject(rgn);
5831 DeleteObject(gdifont);
5833 RestoreDC(hdc, save_state);
5835 DeleteDC(temp_hdc);
5837 return Ok;
5840 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5842 TRACE("(%p)\n", graphics);
5844 if(!graphics)
5845 return InvalidParameter;
5847 if(graphics->busy)
5848 return ObjectBusy;
5850 return GdipSetInfinite(graphics->clip);
5853 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5855 GpStatus stat;
5857 TRACE("(%p)\n", graphics);
5859 if(!graphics)
5860 return InvalidParameter;
5862 if(graphics->busy)
5863 return ObjectBusy;
5865 if (is_metafile_graphics(graphics))
5867 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5869 if (stat != Ok)
5870 return stat;
5873 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5876 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5877 GpMatrixOrder order)
5879 GpStatus stat;
5881 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5883 if(!graphics)
5884 return InvalidParameter;
5886 if(graphics->busy)
5887 return ObjectBusy;
5889 if (is_metafile_graphics(graphics))
5891 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5893 if (stat != Ok)
5894 return stat;
5897 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5900 static GpStatus begin_container(GpGraphics *graphics,
5901 GraphicsContainerType type, GraphicsContainer *state)
5903 GraphicsContainerItem *container;
5904 GpStatus sts;
5906 if(!graphics || !state)
5907 return InvalidParameter;
5909 sts = init_container(&container, graphics, type);
5910 if(sts != Ok)
5911 return sts;
5913 list_add_head(&graphics->containers, &container->entry);
5914 *state = graphics->contid = container->contid;
5916 if (is_metafile_graphics(graphics)) {
5917 if (type == BEGIN_CONTAINER)
5918 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5919 else
5920 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5923 return Ok;
5926 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5928 TRACE("(%p, %p)\n", graphics, state);
5929 return begin_container(graphics, SAVE_GRAPHICS, state);
5932 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5933 GraphicsContainer *state)
5935 TRACE("(%p, %p)\n", graphics, state);
5936 return begin_container(graphics, BEGIN_CONTAINER, state);
5939 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5941 GraphicsContainerItem *container;
5942 GpMatrix transform;
5943 GpStatus stat;
5944 GpRectF scaled_srcrect;
5945 REAL scale_x, scale_y;
5947 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5949 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5950 return InvalidParameter;
5952 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5953 if(stat != Ok)
5954 return stat;
5956 list_add_head(&graphics->containers, &container->entry);
5957 *state = graphics->contid = container->contid;
5959 scale_x = units_to_pixels(1.0, unit, graphics->xres, graphics->printer_display);
5960 scale_y = units_to_pixels(1.0, unit, graphics->yres, graphics->printer_display);
5962 scaled_srcrect.X = scale_x * srcrect->X;
5963 scaled_srcrect.Y = scale_y * srcrect->Y;
5964 scaled_srcrect.Width = scale_x * srcrect->Width;
5965 scaled_srcrect.Height = scale_y * srcrect->Height;
5967 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5968 transform.matrix[1] = 0.0;
5969 transform.matrix[2] = 0.0;
5970 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5971 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5972 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5974 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5976 if (is_metafile_graphics(graphics))
5977 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5979 return Ok;
5982 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5984 GpRectF dstrectf, srcrectf;
5986 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5988 if (!dstrect || !srcrect)
5989 return InvalidParameter;
5991 dstrectf.X = dstrect->X;
5992 dstrectf.Y = dstrect->Y;
5993 dstrectf.Width = dstrect->Width;
5994 dstrectf.Height = dstrect->Height;
5996 srcrectf.X = srcrect->X;
5997 srcrectf.Y = srcrect->Y;
5998 srcrectf.Width = srcrect->Width;
5999 srcrectf.Height = srcrect->Height;
6001 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
6004 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
6006 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
6007 return NotImplemented;
6010 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
6011 GraphicsContainer state)
6013 GpStatus sts;
6014 GraphicsContainerItem *container, *container2;
6016 if(!graphics)
6017 return InvalidParameter;
6019 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
6020 if(container->contid == state && container->type == type)
6021 break;
6024 /* did not find a matching container */
6025 if(&container->entry == &graphics->containers)
6026 return Ok;
6028 sts = restore_container(graphics, container);
6029 if(sts != Ok)
6030 return sts;
6032 /* remove all of the containers on top of the found container */
6033 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
6034 if(container->contid == state)
6035 break;
6036 list_remove(&container->entry);
6037 delete_container(container);
6040 list_remove(&container->entry);
6041 delete_container(container);
6043 if (is_metafile_graphics(graphics)) {
6044 if (type == BEGIN_CONTAINER)
6045 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
6046 else
6047 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
6050 return Ok;
6053 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
6055 TRACE("(%p, %x)\n", graphics, state);
6056 return end_container(graphics, BEGIN_CONTAINER, state);
6059 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
6061 TRACE("(%p, %x)\n", graphics, state);
6062 return end_container(graphics, SAVE_GRAPHICS, state);
6065 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
6066 REAL sy, GpMatrixOrder order)
6068 GpStatus stat;
6070 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
6072 if(!graphics)
6073 return InvalidParameter;
6075 if(graphics->busy)
6076 return ObjectBusy;
6078 if (is_metafile_graphics(graphics)) {
6079 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
6081 if (stat != Ok)
6082 return stat;
6085 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
6088 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
6089 CombineMode mode)
6091 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
6093 if(!graphics || !srcgraphics)
6094 return InvalidParameter;
6096 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
6099 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
6100 CompositingMode mode)
6102 TRACE("(%p, %d)\n", graphics, mode);
6104 if(!graphics)
6105 return InvalidParameter;
6107 if(graphics->busy)
6108 return ObjectBusy;
6110 if(graphics->compmode == mode)
6111 return Ok;
6113 if (is_metafile_graphics(graphics))
6115 GpStatus stat;
6117 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6118 EmfPlusRecordTypeSetCompositingMode, mode);
6119 if(stat != Ok)
6120 return stat;
6123 graphics->compmode = mode;
6125 return Ok;
6128 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
6129 CompositingQuality quality)
6131 TRACE("(%p, %d)\n", graphics, quality);
6133 if(!graphics)
6134 return InvalidParameter;
6136 if(graphics->busy)
6137 return ObjectBusy;
6139 if(graphics->compqual == quality)
6140 return Ok;
6142 if (is_metafile_graphics(graphics))
6144 GpStatus stat;
6146 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6147 EmfPlusRecordTypeSetCompositingQuality, quality);
6148 if(stat != Ok)
6149 return stat;
6152 graphics->compqual = quality;
6154 return Ok;
6157 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
6158 InterpolationMode mode)
6160 TRACE("(%p, %d)\n", graphics, mode);
6162 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
6163 return InvalidParameter;
6165 if(graphics->busy)
6166 return ObjectBusy;
6168 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
6169 mode = InterpolationModeBilinear;
6171 if (mode == InterpolationModeHighQuality)
6172 mode = InterpolationModeHighQualityBicubic;
6174 if (mode == graphics->interpolation)
6175 return Ok;
6177 if (is_metafile_graphics(graphics))
6179 GpStatus stat;
6181 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6182 EmfPlusRecordTypeSetInterpolationMode, mode);
6183 if (stat != Ok)
6184 return stat;
6187 graphics->interpolation = mode;
6189 return Ok;
6192 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6194 GpStatus stat;
6196 TRACE("(%p, %.2f)\n", graphics, scale);
6198 if(!graphics || (scale <= 0.0))
6199 return InvalidParameter;
6201 if(graphics->busy)
6202 return ObjectBusy;
6204 if (is_metafile_graphics(graphics))
6206 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6207 if (stat != Ok)
6208 return stat;
6211 graphics->scale = scale;
6213 return Ok;
6216 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6218 GpStatus stat;
6220 TRACE("(%p, %d)\n", graphics, unit);
6222 if(!graphics)
6223 return InvalidParameter;
6225 if(graphics->busy)
6226 return ObjectBusy;
6228 if(unit == UnitWorld)
6229 return InvalidParameter;
6231 if (is_metafile_graphics(graphics))
6233 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6234 if (stat != Ok)
6235 return stat;
6238 graphics->unit = unit;
6240 return Ok;
6243 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6244 mode)
6246 TRACE("(%p, %d)\n", graphics, mode);
6248 if(!graphics)
6249 return InvalidParameter;
6251 if(graphics->busy)
6252 return ObjectBusy;
6254 if(graphics->pixeloffset == mode)
6255 return Ok;
6257 if (is_metafile_graphics(graphics))
6259 GpStatus stat;
6261 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6262 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6263 if(stat != Ok)
6264 return stat;
6267 graphics->pixeloffset = mode;
6269 return Ok;
6272 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6274 TRACE("(%p,%i,%i)\n", graphics, x, y);
6276 if (!graphics)
6277 return InvalidParameter;
6279 graphics->origin_x = x;
6280 graphics->origin_y = y;
6282 return Ok;
6285 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6287 TRACE("(%p,%p,%p)\n", graphics, x, y);
6289 if (!graphics || !x || !y)
6290 return InvalidParameter;
6292 *x = graphics->origin_x;
6293 *y = graphics->origin_y;
6295 return Ok;
6298 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6300 TRACE("(%p, %d)\n", graphics, mode);
6302 if(!graphics)
6303 return InvalidParameter;
6305 if(graphics->busy)
6306 return ObjectBusy;
6308 if(graphics->smoothing == mode)
6309 return Ok;
6311 if (is_metafile_graphics(graphics))
6313 GpStatus stat;
6314 BOOL antialias = (mode != SmoothingModeDefault &&
6315 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6317 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6318 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6319 if(stat != Ok)
6320 return stat;
6323 graphics->smoothing = mode;
6325 return Ok;
6328 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6330 TRACE("(%p, %d)\n", graphics, contrast);
6332 if(!graphics)
6333 return InvalidParameter;
6335 graphics->textcontrast = contrast;
6337 return Ok;
6340 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6341 TextRenderingHint hint)
6343 TRACE("(%p, %d)\n", graphics, hint);
6345 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6346 return InvalidParameter;
6348 if(graphics->busy)
6349 return ObjectBusy;
6351 if(graphics->texthint == hint)
6352 return Ok;
6354 if (is_metafile_graphics(graphics)) {
6355 GpStatus stat;
6357 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6358 EmfPlusRecordTypeSetTextRenderingHint, hint);
6359 if(stat != Ok)
6360 return stat;
6363 graphics->texthint = hint;
6365 return Ok;
6368 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6370 GpStatus stat;
6372 TRACE("(%p, %p)\n", graphics, matrix);
6374 if(!graphics || !matrix)
6375 return InvalidParameter;
6377 if(graphics->busy)
6378 return ObjectBusy;
6380 TRACE("%f,%f,%f,%f,%f,%f\n",
6381 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6382 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6384 if (is_metafile_graphics(graphics)) {
6385 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6387 if (stat != Ok)
6388 return stat;
6391 graphics->worldtrans = *matrix;
6393 return Ok;
6396 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6397 REAL dy, GpMatrixOrder order)
6399 GpStatus stat;
6401 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6403 if(!graphics)
6404 return InvalidParameter;
6406 if(graphics->busy)
6407 return ObjectBusy;
6409 if (is_metafile_graphics(graphics)) {
6410 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6412 if (stat != Ok)
6413 return stat;
6416 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6419 /*****************************************************************************
6420 * GdipSetClipHrgn [GDIPLUS.@]
6422 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6424 GpRegion *region;
6425 GpStatus status;
6426 GpMatrix transform;
6428 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6430 if(!graphics)
6431 return InvalidParameter;
6433 if(graphics->busy)
6434 return ObjectBusy;
6436 /* hrgn is in gdi32 device units */
6437 status = GdipCreateRegionHrgn(hrgn, &region);
6439 if (status == Ok)
6441 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6443 if (status == Ok)
6444 status = GdipTransformRegion(region, &transform);
6446 if (status == Ok)
6447 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6449 GdipDeleteRegion(region);
6451 return status;
6454 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6456 GpStatus status;
6457 GpPath *clip_path;
6459 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6461 if(!graphics)
6462 return InvalidParameter;
6464 if(graphics->busy)
6465 return ObjectBusy;
6467 status = GdipClonePath(path, &clip_path);
6468 if (status == Ok)
6470 GpMatrix world_to_device;
6472 get_graphics_transform(graphics, CoordinateSpaceDevice,
6473 CoordinateSpaceWorld, &world_to_device);
6474 status = GdipTransformPath(clip_path, &world_to_device);
6475 if (status == Ok)
6476 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6478 GdipDeletePath(clip_path);
6480 return status;
6483 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6484 REAL width, REAL height,
6485 CombineMode mode)
6487 GpStatus status;
6488 GpRectF rect;
6489 GpRegion *region;
6491 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6493 if(!graphics)
6494 return InvalidParameter;
6496 if(graphics->busy)
6497 return ObjectBusy;
6499 if (is_metafile_graphics(graphics))
6501 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6502 if (status != Ok)
6503 return status;
6506 rect.X = x;
6507 rect.Y = y;
6508 rect.Width = width;
6509 rect.Height = height;
6510 status = GdipCreateRegionRect(&rect, &region);
6511 if (status == Ok)
6513 GpMatrix world_to_device;
6514 BOOL identity;
6516 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6517 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6518 if (status == Ok && !identity)
6519 status = GdipTransformRegion(region, &world_to_device);
6520 if (status == Ok)
6521 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6523 GdipDeleteRegion(region);
6525 return status;
6528 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6529 INT width, INT height,
6530 CombineMode mode)
6532 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6534 if(!graphics)
6535 return InvalidParameter;
6537 if(graphics->busy)
6538 return ObjectBusy;
6540 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6543 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6544 CombineMode mode)
6546 GpStatus status;
6547 GpRegion *clip;
6549 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6551 if(!graphics || !region)
6552 return InvalidParameter;
6554 if(graphics->busy)
6555 return ObjectBusy;
6557 if (is_metafile_graphics(graphics))
6559 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6560 if (status != Ok)
6561 return status;
6564 status = GdipCloneRegion(region, &clip);
6565 if (status == Ok)
6567 GpMatrix world_to_device;
6568 BOOL identity;
6570 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6571 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6572 if (status == Ok && !identity)
6573 status = GdipTransformRegion(clip, &world_to_device);
6574 if (status == Ok)
6575 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6577 GdipDeleteRegion(clip);
6579 return status;
6582 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6583 INT count)
6585 GpStatus status;
6586 GpPath* path;
6588 TRACE("(%p, %p, %d)\n", graphics, points, count);
6590 if(!graphics || !pen || count<=0)
6591 return InvalidParameter;
6593 if(graphics->busy)
6594 return ObjectBusy;
6596 status = GdipCreatePath(FillModeAlternate, &path);
6597 if (status != Ok) return status;
6599 status = GdipAddPathPolygon(path, points, count);
6600 if (status == Ok)
6601 status = GdipDrawPath(graphics, pen, path);
6603 GdipDeletePath(path);
6605 return status;
6608 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6609 INT count)
6611 GpStatus ret;
6612 GpPointF *ptf;
6613 INT i;
6615 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6617 if(count<=0) return InvalidParameter;
6618 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6619 if (!ptf) return OutOfMemory;
6621 for(i = 0;i < count; i++){
6622 ptf[i].X = (REAL)points[i].X;
6623 ptf[i].Y = (REAL)points[i].Y;
6626 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6627 heap_free(ptf);
6629 return ret;
6632 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6634 TRACE("(%p, %p)\n", graphics, dpi);
6636 if(!graphics || !dpi)
6637 return InvalidParameter;
6639 if(graphics->busy)
6640 return ObjectBusy;
6642 *dpi = graphics->xres;
6643 return Ok;
6646 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6648 TRACE("(%p, %p)\n", graphics, dpi);
6650 if(!graphics || !dpi)
6651 return InvalidParameter;
6653 if(graphics->busy)
6654 return ObjectBusy;
6656 *dpi = graphics->yres;
6657 return Ok;
6660 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6661 GpMatrixOrder order)
6663 GpMatrix m;
6664 GpStatus ret;
6666 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6668 if(!graphics || !matrix)
6669 return InvalidParameter;
6671 if(graphics->busy)
6672 return ObjectBusy;
6674 if (is_metafile_graphics(graphics))
6676 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6678 if (ret != Ok)
6679 return ret;
6682 m = graphics->worldtrans;
6684 ret = GdipMultiplyMatrix(&m, matrix, order);
6685 if(ret == Ok)
6686 graphics->worldtrans = m;
6688 return ret;
6691 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6692 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6694 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6696 GpStatus stat=Ok;
6698 TRACE("(%p, %p)\n", graphics, hdc);
6700 if(!graphics || !hdc)
6701 return InvalidParameter;
6703 if(graphics->busy)
6704 return ObjectBusy;
6706 if (is_metafile_graphics(graphics))
6708 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6710 else if (!graphics->hdc ||
6711 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6713 /* Create a fake HDC and fill it with a constant color. */
6714 HDC temp_hdc;
6715 HBITMAP hbitmap;
6716 GpRectF bounds;
6717 BITMAPINFOHEADER bmih;
6718 int i;
6720 stat = get_graphics_bounds(graphics, &bounds);
6721 if (stat != Ok)
6722 return stat;
6724 graphics->temp_hbitmap_width = bounds.Width;
6725 graphics->temp_hbitmap_height = bounds.Height;
6727 bmih.biSize = sizeof(bmih);
6728 bmih.biWidth = graphics->temp_hbitmap_width;
6729 bmih.biHeight = -graphics->temp_hbitmap_height;
6730 bmih.biPlanes = 1;
6731 bmih.biBitCount = 32;
6732 bmih.biCompression = BI_RGB;
6733 bmih.biSizeImage = 0;
6734 bmih.biXPelsPerMeter = 0;
6735 bmih.biYPelsPerMeter = 0;
6736 bmih.biClrUsed = 0;
6737 bmih.biClrImportant = 0;
6739 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6740 (void**)&graphics->temp_bits, NULL, 0);
6741 if (!hbitmap)
6742 return GenericError;
6744 if (!graphics->temp_hdc)
6746 temp_hdc = CreateCompatibleDC(0);
6748 else
6750 temp_hdc = graphics->temp_hdc;
6753 if (!temp_hdc)
6755 DeleteObject(hbitmap);
6756 return GenericError;
6759 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6760 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6762 SelectObject(temp_hdc, hbitmap);
6764 graphics->temp_hbitmap = hbitmap;
6765 *hdc = graphics->temp_hdc = temp_hdc;
6767 else
6769 *hdc = graphics->hdc;
6772 if (stat == Ok)
6773 graphics->busy = TRUE;
6775 return stat;
6778 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6780 GpStatus stat=Ok;
6782 TRACE("(%p, %p)\n", graphics, hdc);
6784 if(!graphics || !hdc || !graphics->busy)
6785 return InvalidParameter;
6787 if (is_metafile_graphics(graphics))
6789 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6791 else if (graphics->temp_hdc == hdc)
6793 DWORD* pos;
6794 int i;
6796 /* Find the pixels that have changed, and mark them as opaque. */
6797 pos = (DWORD*)graphics->temp_bits;
6798 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6800 if (*pos != DC_BACKGROUND_KEY)
6802 *pos |= 0xff000000;
6804 pos++;
6807 /* Write the changed pixels to the real target. */
6808 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6809 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6810 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6812 /* Clean up. */
6813 DeleteObject(graphics->temp_hbitmap);
6814 graphics->temp_hbitmap = NULL;
6816 else if (hdc != graphics->hdc)
6818 stat = InvalidParameter;
6821 if (stat == Ok)
6822 graphics->busy = FALSE;
6824 return stat;
6827 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6829 GpRegion *clip;
6830 GpStatus status;
6831 GpMatrix device_to_world;
6833 TRACE("(%p, %p)\n", graphics, region);
6835 if(!graphics || !region)
6836 return InvalidParameter;
6838 if(graphics->busy)
6839 return ObjectBusy;
6841 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6842 return status;
6844 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6845 status = GdipTransformRegion(clip, &device_to_world);
6846 if (status != Ok)
6848 GdipDeleteRegion(clip);
6849 return status;
6852 /* free everything except root node and header */
6853 delete_element(&region->node);
6854 memcpy(region, clip, sizeof(GpRegion));
6855 heap_free(clip);
6857 return Ok;
6860 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6862 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6864 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6865 ModifyWorldTransform(graphics->hdc, NULL, MWT_IDENTITY);
6866 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6867 SetMapMode(graphics->hdc, MM_TEXT);
6868 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6869 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6871 graphics->gdi_transform_acquire_count++;
6872 return Ok;
6875 GpStatus gdi_transform_release(GpGraphics *graphics)
6877 if (graphics->gdi_transform_acquire_count <= 0)
6879 ERR("called without matching gdi_transform_acquire\n");
6880 return GenericError;
6882 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6884 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6886 graphics->gdi_transform_acquire_count--;
6887 return Ok;
6890 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6891 GpCoordinateSpace src_space, GpMatrix *matrix)
6893 GpStatus stat = Ok;
6894 REAL scale_x, scale_y;
6896 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6898 if (dst_space != src_space)
6900 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres, graphics->printer_display);
6901 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres, graphics->printer_display);
6903 if(graphics->unit != UnitDisplay)
6905 scale_x *= graphics->scale;
6906 scale_y *= graphics->scale;
6909 if (dst_space < src_space)
6911 /* transform towards world space */
6912 switch ((int)src_space)
6914 case WineCoordinateSpaceGdiDevice:
6916 GpMatrix gdixform;
6917 gdixform = graphics->gdi_transform;
6918 stat = GdipInvertMatrix(&gdixform);
6919 if (stat != Ok)
6920 break;
6921 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6922 if (dst_space == CoordinateSpaceDevice)
6923 break;
6924 /* else fall-through */
6926 case CoordinateSpaceDevice:
6927 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6928 if (dst_space == CoordinateSpacePage)
6929 break;
6930 /* else fall-through */
6931 case CoordinateSpacePage:
6933 GpMatrix inverted_transform = graphics->worldtrans;
6934 stat = GdipInvertMatrix(&inverted_transform);
6935 if (stat == Ok)
6936 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6937 break;
6941 else
6943 /* transform towards device space */
6944 switch ((int)src_space)
6946 case CoordinateSpaceWorld:
6947 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6948 if (dst_space == CoordinateSpacePage)
6949 break;
6950 /* else fall-through */
6951 case CoordinateSpacePage:
6952 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6953 if (dst_space == CoordinateSpaceDevice)
6954 break;
6955 /* else fall-through */
6956 case CoordinateSpaceDevice:
6958 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6959 break;
6964 return stat;
6967 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6968 GpCoordinateSpace src_space, GpPointF *points, INT count)
6970 GpMatrix matrix;
6971 GpStatus stat;
6973 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6974 if (stat != Ok) return stat;
6976 return GdipTransformMatrixPoints(&matrix, points, count);
6979 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6980 GpCoordinateSpace src_space, GpPointF *points, INT count)
6982 if(!graphics || !points || count <= 0 ||
6983 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6984 src_space < 0 || src_space > CoordinateSpaceDevice)
6985 return InvalidParameter;
6987 if(graphics->busy)
6988 return ObjectBusy;
6990 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6992 if (src_space == dst_space) return Ok;
6994 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6997 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6998 GpCoordinateSpace src_space, GpPoint *points, INT count)
7000 GpPointF *pointsF;
7001 GpStatus ret;
7002 INT i;
7004 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
7006 if(count <= 0)
7007 return InvalidParameter;
7009 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
7010 if(!pointsF)
7011 return OutOfMemory;
7013 for(i = 0; i < count; i++){
7014 pointsF[i].X = (REAL)points[i].X;
7015 pointsF[i].Y = (REAL)points[i].Y;
7018 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
7020 if(ret == Ok)
7021 for(i = 0; i < count; i++){
7022 points[i].X = gdip_round(pointsF[i].X);
7023 points[i].Y = gdip_round(pointsF[i].Y);
7025 heap_free(pointsF);
7027 return ret;
7030 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
7032 static int calls;
7034 TRACE("\n");
7036 if (!calls++)
7037 FIXME("stub\n");
7039 return NULL;
7042 /*****************************************************************************
7043 * GdipTranslateClip [GDIPLUS.@]
7045 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
7047 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
7049 if(!graphics)
7050 return InvalidParameter;
7052 if(graphics->busy)
7053 return ObjectBusy;
7055 return GdipTranslateRegion(graphics->clip, dx, dy);
7058 /*****************************************************************************
7059 * GdipTranslateClipI [GDIPLUS.@]
7061 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
7063 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
7065 if(!graphics)
7066 return InvalidParameter;
7068 if(graphics->busy)
7069 return ObjectBusy;
7071 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
7075 /*****************************************************************************
7076 * GdipMeasureDriverString [GDIPLUS.@]
7078 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7079 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
7080 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
7082 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7083 HFONT hfont;
7084 HDC hdc;
7085 REAL min_x, min_y, max_x, max_y, x, y;
7086 int i;
7087 TEXTMETRICW textmetric;
7088 const WORD *glyph_indices;
7089 WORD *dynamic_glyph_indices=NULL;
7090 REAL rel_width, rel_height, ascent, descent;
7091 GpPointF pt[3];
7093 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
7095 if (!graphics || !text || !font || !positions || !boundingBox)
7096 return InvalidParameter;
7098 if (length == -1)
7099 length = lstrlenW(text);
7101 if (length == 0)
7103 boundingBox->X = 0.0;
7104 boundingBox->Y = 0.0;
7105 boundingBox->Width = 0.0;
7106 boundingBox->Height = 0.0;
7109 if (flags & unsupported_flags)
7110 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7112 get_font_hfont(graphics, font, NULL, &hfont, NULL, matrix);
7114 hdc = CreateCompatibleDC(0);
7115 SelectObject(hdc, hfont);
7117 GetTextMetricsW(hdc, &textmetric);
7119 pt[0].X = 0.0;
7120 pt[0].Y = 0.0;
7121 pt[1].X = 1.0;
7122 pt[1].Y = 0.0;
7123 pt[2].X = 0.0;
7124 pt[2].Y = 1.0;
7125 if (matrix)
7127 GpMatrix xform = *matrix;
7128 GdipTransformMatrixPoints(&xform, pt, 3);
7130 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
7131 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
7132 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
7133 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
7134 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
7136 if (flags & DriverStringOptionsCmapLookup)
7138 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
7139 if (!glyph_indices)
7141 DeleteDC(hdc);
7142 DeleteObject(hfont);
7143 return OutOfMemory;
7146 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
7148 else
7149 glyph_indices = text;
7151 min_x = max_x = x = positions[0].X;
7152 min_y = max_y = y = positions[0].Y;
7154 ascent = textmetric.tmAscent / rel_height;
7155 descent = textmetric.tmDescent / rel_height;
7157 for (i=0; i<length; i++)
7159 int char_width;
7160 ABC abc;
7162 if (!(flags & DriverStringOptionsRealizedAdvance))
7164 x = positions[i].X;
7165 y = positions[i].Y;
7168 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
7169 char_width = abc.abcA + abc.abcB + abc.abcC;
7171 if (min_y > y - ascent) min_y = y - ascent;
7172 if (max_y < y + descent) max_y = y + descent;
7173 if (min_x > x) min_x = x;
7175 x += char_width / rel_width;
7177 if (max_x < x) max_x = x;
7180 heap_free(dynamic_glyph_indices);
7181 DeleteDC(hdc);
7182 DeleteObject(hfont);
7184 boundingBox->X = min_x;
7185 boundingBox->Y = min_y;
7186 boundingBox->Width = max_x - min_x;
7187 boundingBox->Height = max_y - min_y;
7189 return Ok;
7192 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7193 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7194 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7195 INT flags, GDIPCONST GpMatrix *matrix)
7197 INT save_state;
7198 GpPointF pt, *real_positions=NULL;
7199 INT *eto_positions=NULL;
7200 HFONT hfont;
7201 LOGFONTW lfw;
7202 UINT eto_flags=0;
7203 GpStatus status;
7204 HRGN hrgn;
7206 if (!(flags & DriverStringOptionsCmapLookup))
7207 eto_flags |= ETO_GLYPH_INDEX;
7209 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7211 real_positions = heap_alloc(sizeof(*real_positions) * length);
7212 eto_positions = heap_alloc(sizeof(*eto_positions) * 2 * (length - 1));
7213 if (!real_positions || !eto_positions)
7215 heap_free(real_positions);
7216 heap_free(eto_positions);
7217 return OutOfMemory;
7221 save_state = SaveDC(graphics->hdc);
7222 SetBkMode(graphics->hdc, TRANSPARENT);
7223 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7225 status = get_clip_hrgn(graphics, &hrgn);
7227 if (status == Ok)
7229 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7230 DeleteObject(hrgn);
7233 pt = positions[0];
7234 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7236 get_font_hfont(graphics, font, format, &hfont, &lfw, matrix);
7238 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7240 GpMatrix rotation;
7241 INT i;
7243 eto_flags |= ETO_PDY;
7245 memcpy(real_positions, positions, sizeof(PointF) * length);
7247 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7249 GdipSetMatrixElements(&rotation, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
7250 GdipRotateMatrix(&rotation, lfw.lfEscapement / 10.0, MatrixOrderAppend);
7251 GdipTransformMatrixPoints(&rotation, real_positions, length);
7253 for (i = 0; i < (length - 1); i++)
7255 eto_positions[i*2] = gdip_round(real_positions[i+1].X) - gdip_round(real_positions[i].X);
7256 eto_positions[i*2+1] = gdip_round(real_positions[i].Y) - gdip_round(real_positions[i+1].Y);
7260 SelectObject(graphics->hdc, hfont);
7262 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7264 gdi_transform_acquire(graphics);
7266 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, eto_positions);
7268 gdi_transform_release(graphics);
7270 RestoreDC(graphics->hdc, save_state);
7272 DeleteObject(hfont);
7274 heap_free(real_positions);
7275 heap_free(eto_positions);
7277 return Ok;
7280 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7281 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7282 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7283 INT flags, GDIPCONST GpMatrix *matrix)
7285 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7286 GpStatus stat;
7287 PointF *real_positions, real_position;
7288 POINT *pti;
7289 HFONT hfont;
7290 HDC hdc;
7291 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7292 DWORD max_glyphsize=0;
7293 GLYPHMETRICS glyphmetrics;
7294 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7295 BYTE *glyph_mask;
7296 BYTE *text_mask;
7297 int text_mask_stride;
7298 BYTE *pixel_data;
7299 int pixel_data_stride;
7300 GpRect pixel_area;
7301 UINT ggo_flags = GGO_GRAY8_BITMAP;
7303 if (length <= 0)
7304 return Ok;
7306 if (!(flags & DriverStringOptionsCmapLookup))
7307 ggo_flags |= GGO_GLYPH_INDEX;
7309 if (flags & unsupported_flags)
7310 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7312 pti = heap_alloc_zero(sizeof(POINT) * length);
7313 if (!pti)
7314 return OutOfMemory;
7316 if (flags & DriverStringOptionsRealizedAdvance)
7318 real_position = positions[0];
7320 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7321 round_points(pti, &real_position, 1);
7323 else
7325 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7326 if (!real_positions)
7328 heap_free(pti);
7329 return OutOfMemory;
7332 memcpy(real_positions, positions, sizeof(PointF) * length);
7334 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7335 round_points(pti, real_positions, length);
7337 heap_free(real_positions);
7340 get_font_hfont(graphics, font, format, &hfont, NULL, matrix);
7342 hdc = CreateCompatibleDC(0);
7343 SelectObject(hdc, hfont);
7345 /* Get the boundaries of the text to be drawn */
7346 for (i=0; i<length; i++)
7348 DWORD glyphsize;
7349 int left, top, right, bottom;
7351 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7352 &glyphmetrics, 0, NULL, &identity);
7354 if (glyphsize == GDI_ERROR)
7356 ERR("GetGlyphOutlineW failed\n");
7357 heap_free(pti);
7358 DeleteDC(hdc);
7359 DeleteObject(hfont);
7360 return GenericError;
7363 if (glyphsize > max_glyphsize)
7364 max_glyphsize = glyphsize;
7366 if (glyphsize != 0)
7368 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7369 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7370 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7371 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7373 if (left < min_x) min_x = left;
7374 if (top < min_y) min_y = top;
7375 if (right > max_x) max_x = right;
7376 if (bottom > max_y) max_y = bottom;
7379 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7381 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7382 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7386 if (max_glyphsize == 0)
7388 /* Nothing to draw. */
7389 heap_free(pti);
7390 DeleteDC(hdc);
7391 DeleteObject(hfont);
7392 return Ok;
7395 glyph_mask = heap_alloc_zero(max_glyphsize);
7396 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7397 text_mask_stride = max_x - min_x;
7399 if (!(glyph_mask && text_mask))
7401 heap_free(glyph_mask);
7402 heap_free(text_mask);
7403 heap_free(pti);
7404 DeleteDC(hdc);
7405 DeleteObject(hfont);
7406 return OutOfMemory;
7409 /* Generate a mask for the text */
7410 for (i=0; i<length; i++)
7412 DWORD ret;
7413 int left, top, stride;
7415 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7416 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7418 if (ret == GDI_ERROR || ret == 0)
7419 continue; /* empty glyph */
7421 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7422 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7423 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7425 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7427 BYTE *glyph_val = glyph_mask + y * stride;
7428 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7429 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7431 *text_val = min(64, *text_val + *glyph_val);
7432 glyph_val++;
7433 text_val++;
7438 heap_free(pti);
7439 DeleteDC(hdc);
7440 DeleteObject(hfont);
7441 heap_free(glyph_mask);
7443 /* get the brush data */
7444 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7445 if (!pixel_data)
7447 heap_free(text_mask);
7448 return OutOfMemory;
7451 pixel_area.X = min_x;
7452 pixel_area.Y = min_y;
7453 pixel_area.Width = max_x - min_x;
7454 pixel_area.Height = max_y - min_y;
7455 pixel_data_stride = pixel_area.Width * 4;
7457 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7458 if (stat != Ok)
7460 heap_free(text_mask);
7461 heap_free(pixel_data);
7462 return stat;
7465 /* multiply the brush data by the mask */
7466 for (y=0; y<pixel_area.Height; y++)
7468 BYTE *text_val = text_mask + text_mask_stride * y;
7469 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7470 for (x=0; x<pixel_area.Width; x++)
7472 *pixel_val = (*pixel_val) * (*text_val) / 64;
7473 text_val++;
7474 pixel_val+=4;
7478 heap_free(text_mask);
7480 gdi_transform_acquire(graphics);
7482 /* draw the result */
7483 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7484 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7486 gdi_transform_release(graphics);
7488 heap_free(pixel_data);
7490 return stat;
7493 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7494 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7495 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7496 INT flags, GDIPCONST GpMatrix *matrix)
7498 GpStatus stat = NotImplemented;
7500 if (length == -1)
7501 length = lstrlenW(text);
7503 if (is_metafile_graphics(graphics))
7504 return METAFILE_DrawDriverString((GpMetafile*)graphics->image, text, length, font,
7505 format, brush, positions, flags, matrix);
7507 if (graphics->hdc && !graphics->alpha_hdc &&
7508 brush->bt == BrushTypeSolidColor &&
7509 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7510 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7511 brush, positions, flags, matrix);
7512 if (stat == NotImplemented)
7513 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7514 brush, positions, flags, matrix);
7515 return stat;
7518 /*****************************************************************************
7519 * GdipDrawDriverString [GDIPLUS.@]
7521 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7522 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7523 GDIPCONST PointF *positions, INT flags,
7524 GDIPCONST GpMatrix *matrix )
7526 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7528 if (!graphics || !text || !font || !brush || !positions)
7529 return InvalidParameter;
7531 return draw_driver_string(graphics, text, length, font, NULL,
7532 brush, positions, flags, matrix);
7535 /*****************************************************************************
7536 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7538 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7540 GpStatus stat;
7541 GpRegion* rgn;
7543 TRACE("(%p, %p)\n", graphics, res);
7545 if((stat = GdipCreateRegion(&rgn)) != Ok)
7546 return stat;
7548 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7549 goto cleanup;
7551 stat = GdipIsEmptyRegion(rgn, graphics, res);
7553 cleanup:
7554 GdipDeleteRegion(rgn);
7555 return stat;
7558 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7560 static int calls;
7562 TRACE("(%p) stub\n", graphics);
7564 if(!(calls++))
7565 FIXME("not implemented\n");
7567 return NotImplemented;
7570 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7572 TRACE("(%p, %p)\n", graphics, pabort);
7574 if (!graphics)
7575 return InvalidParameter;
7577 if (pabort)
7578 FIXME("Abort callback is not supported.\n");
7580 return Ok;