mfplat: Implement GetScanline0AndPitch() for d3d11 buffers.
[wine/zf.git] / dlls / gdiplus / graphics.c
blobe292afdfb2c104d6988d3b9dc5d3378bb576717a
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;
2598 GpRectF rect;
2600 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2601 width, height, startAngle, sweepAngle);
2603 if(!graphics || !pen || width <= 0 || height <= 0)
2604 return InvalidParameter;
2606 if(graphics->busy)
2607 return ObjectBusy;
2609 if (is_metafile_graphics(graphics))
2611 set_rect(&rect, x, y, width, height);
2612 return METAFILE_DrawArc((GpMetafile *)graphics->image, pen, &rect, startAngle, sweepAngle);
2615 status = GdipCreatePath(FillModeAlternate, &path);
2616 if (status != Ok) return status;
2618 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2619 if (status == Ok)
2620 status = GdipDrawPath(graphics, pen, path);
2622 GdipDeletePath(path);
2623 return status;
2626 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2627 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2629 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2630 width, height, startAngle, sweepAngle);
2632 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2635 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2636 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2638 GpPointF pt[4];
2640 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2641 x2, y2, x3, y3, x4, y4);
2643 if(!graphics || !pen)
2644 return InvalidParameter;
2646 if(graphics->busy)
2647 return ObjectBusy;
2649 pt[0].X = x1;
2650 pt[0].Y = y1;
2651 pt[1].X = x2;
2652 pt[1].Y = y2;
2653 pt[2].X = x3;
2654 pt[2].Y = y3;
2655 pt[3].X = x4;
2656 pt[3].Y = y4;
2657 return GdipDrawBeziers(graphics, pen, pt, 4);
2660 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2661 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2663 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2664 x2, y2, x3, y3, x4, y4);
2666 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2669 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2670 GDIPCONST GpPointF *points, INT count)
2672 GpStatus status;
2673 GpPath *path;
2675 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2677 if(!graphics || !pen || !points || (count <= 0))
2678 return InvalidParameter;
2680 if(graphics->busy)
2681 return ObjectBusy;
2683 status = GdipCreatePath(FillModeAlternate, &path);
2684 if (status != Ok) return status;
2686 status = GdipAddPathBeziers(path, points, count);
2687 if (status == Ok)
2688 status = GdipDrawPath(graphics, pen, path);
2690 GdipDeletePath(path);
2691 return status;
2694 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2695 GDIPCONST GpPoint *points, INT count)
2697 GpPointF *pts;
2698 GpStatus ret;
2699 INT i;
2701 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2703 if(!graphics || !pen || !points || (count <= 0))
2704 return InvalidParameter;
2706 if(graphics->busy)
2707 return ObjectBusy;
2709 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2710 if(!pts)
2711 return OutOfMemory;
2713 for(i = 0; i < count; i++){
2714 pts[i].X = (REAL)points[i].X;
2715 pts[i].Y = (REAL)points[i].Y;
2718 ret = GdipDrawBeziers(graphics,pen,pts,count);
2720 heap_free(pts);
2722 return ret;
2725 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2726 GDIPCONST GpPointF *points, INT count)
2728 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2730 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2733 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2734 GDIPCONST GpPoint *points, INT count)
2736 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2738 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2741 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2742 GDIPCONST GpPointF *points, INT count, REAL tension)
2744 GpPath *path;
2745 GpStatus status;
2747 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2749 if(!graphics || !pen || !points || count <= 0)
2750 return InvalidParameter;
2752 if(graphics->busy)
2753 return ObjectBusy;
2755 status = GdipCreatePath(FillModeAlternate, &path);
2756 if (status != Ok) return status;
2758 status = GdipAddPathClosedCurve2(path, points, count, tension);
2759 if (status == Ok)
2760 status = GdipDrawPath(graphics, pen, path);
2762 GdipDeletePath(path);
2764 return status;
2767 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2768 GDIPCONST GpPoint *points, INT count, REAL tension)
2770 GpPointF *ptf;
2771 GpStatus stat;
2772 INT i;
2774 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2776 if(!points || count <= 0)
2777 return InvalidParameter;
2779 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2780 if(!ptf)
2781 return OutOfMemory;
2783 for(i = 0; i < count; i++){
2784 ptf[i].X = (REAL)points[i].X;
2785 ptf[i].Y = (REAL)points[i].Y;
2788 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2790 heap_free(ptf);
2792 return stat;
2795 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2796 GDIPCONST GpPointF *points, INT count)
2798 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2800 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2803 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2804 GDIPCONST GpPoint *points, INT count)
2806 GpPointF *pointsF;
2807 GpStatus ret;
2808 INT i;
2810 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2812 if(!points)
2813 return InvalidParameter;
2815 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2816 if(!pointsF)
2817 return OutOfMemory;
2819 for(i = 0; i < count; i++){
2820 pointsF[i].X = (REAL)points[i].X;
2821 pointsF[i].Y = (REAL)points[i].Y;
2824 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2825 heap_free(pointsF);
2827 return ret;
2830 /* Approximates cardinal spline with Bezier curves. */
2831 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2832 GDIPCONST GpPointF *points, INT count, REAL tension)
2834 GpPath *path;
2835 GpStatus status;
2837 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2839 if(!graphics || !pen)
2840 return InvalidParameter;
2842 if(graphics->busy)
2843 return ObjectBusy;
2845 if(count < 2)
2846 return InvalidParameter;
2848 status = GdipCreatePath(FillModeAlternate, &path);
2849 if (status != Ok) return status;
2851 status = GdipAddPathCurve2(path, points, count, tension);
2852 if (status == Ok)
2853 status = GdipDrawPath(graphics, pen, path);
2855 GdipDeletePath(path);
2856 return status;
2859 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2860 GDIPCONST GpPoint *points, INT count, REAL tension)
2862 GpPointF *pointsF;
2863 GpStatus ret;
2864 INT i;
2866 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2868 if(!points)
2869 return InvalidParameter;
2871 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2872 if(!pointsF)
2873 return OutOfMemory;
2875 for(i = 0; i < count; i++){
2876 pointsF[i].X = (REAL)points[i].X;
2877 pointsF[i].Y = (REAL)points[i].Y;
2880 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2881 heap_free(pointsF);
2883 return ret;
2886 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2887 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2888 REAL tension)
2890 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2892 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2893 return InvalidParameter;
2896 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2899 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2900 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2901 REAL tension)
2903 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2905 if(count < 0){
2906 return OutOfMemory;
2909 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2910 return InvalidParameter;
2913 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2916 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2917 REAL y, REAL width, REAL height)
2919 GpPath *path;
2920 GpStatus status;
2921 GpRectF rect;
2923 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2925 if(!graphics || !pen)
2926 return InvalidParameter;
2928 if(graphics->busy)
2929 return ObjectBusy;
2931 if (is_metafile_graphics(graphics))
2933 set_rect(&rect, x, y, width, height);
2934 return METAFILE_DrawEllipse((GpMetafile *)graphics->image, pen, &rect);
2937 status = GdipCreatePath(FillModeAlternate, &path);
2938 if (status != Ok) return status;
2940 status = GdipAddPathEllipse(path, x, y, width, height);
2941 if (status == Ok)
2942 status = GdipDrawPath(graphics, pen, path);
2944 GdipDeletePath(path);
2945 return status;
2948 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2949 INT y, INT width, INT height)
2951 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2953 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2957 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2959 UINT width, height;
2961 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2963 if(!graphics || !image)
2964 return InvalidParameter;
2966 GdipGetImageWidth(image, &width);
2967 GdipGetImageHeight(image, &height);
2969 return GdipDrawImagePointRect(graphics, image, x, y,
2970 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2973 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2974 INT y)
2976 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2978 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2981 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2982 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2983 GpUnit srcUnit)
2985 GpPointF points[3];
2986 REAL scale_x, scale_y, width, height;
2988 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2990 if (!graphics || !image) return InvalidParameter;
2992 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres, graphics->printer_display);
2993 scale_x *= graphics->xres / image->xres;
2994 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres, graphics->printer_display);
2995 scale_y *= graphics->yres / image->yres;
2996 width = srcwidth * scale_x;
2997 height = srcheight * scale_y;
2999 points[0].X = points[2].X = x;
3000 points[0].Y = points[1].Y = y;
3001 points[1].X = x + width;
3002 points[2].Y = y + height;
3004 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3005 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3008 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3009 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3010 GpUnit srcUnit)
3012 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3015 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3016 GDIPCONST GpPointF *dstpoints, INT count)
3018 UINT width, height;
3020 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3022 if(!image)
3023 return InvalidParameter;
3025 GdipGetImageWidth(image, &width);
3026 GdipGetImageHeight(image, &height);
3028 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3029 width, height, UnitPixel, NULL, NULL, NULL);
3032 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3033 GDIPCONST GpPoint *dstpoints, INT count)
3035 GpPointF ptf[3];
3037 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3039 if (count != 3 || !dstpoints)
3040 return InvalidParameter;
3042 ptf[0].X = (REAL)dstpoints[0].X;
3043 ptf[0].Y = (REAL)dstpoints[0].Y;
3044 ptf[1].X = (REAL)dstpoints[1].X;
3045 ptf[1].Y = (REAL)dstpoints[1].Y;
3046 ptf[2].X = (REAL)dstpoints[2].X;
3047 ptf[2].Y = (REAL)dstpoints[2].Y;
3049 return GdipDrawImagePoints(graphics, image, ptf, count);
3052 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3053 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3055 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3056 return TRUE;
3059 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3060 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3061 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3062 DrawImageAbort callback, VOID * callbackData)
3064 GpPointF ptf[4];
3065 POINT pti[4];
3066 GpStatus stat;
3068 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3069 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3070 callbackData);
3072 if (count > 3)
3073 return NotImplemented;
3075 if(!graphics || !image || !points || count != 3)
3076 return InvalidParameter;
3078 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3079 debugstr_pointf(&points[2]));
3081 if (is_metafile_graphics(graphics))
3083 return METAFILE_DrawImagePointsRect((GpMetafile*)graphics->image,
3084 image, points, count, srcx, srcy, srcwidth, srcheight,
3085 srcUnit, imageAttributes, callback, callbackData);
3088 memcpy(ptf, points, 3 * sizeof(GpPointF));
3090 /* Ensure source width/height is positive */
3091 if (srcwidth < 0)
3093 GpPointF tmp = ptf[1];
3094 srcx = srcx + srcwidth;
3095 srcwidth = -srcwidth;
3096 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
3097 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3098 ptf[1] = ptf[0];
3099 ptf[0] = tmp;
3102 if (srcheight < 0)
3104 GpPointF tmp = ptf[2];
3105 srcy = srcy + srcheight;
3106 srcheight = -srcheight;
3107 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
3108 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
3109 ptf[2] = ptf[0];
3110 ptf[0] = tmp;
3113 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3114 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3115 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
3116 return Ok;
3117 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, ptf, 4);
3118 round_points(pti, ptf, 4);
3120 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3121 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3123 srcx = units_to_pixels(srcx, srcUnit, image->xres, graphics->printer_display);
3124 srcy = units_to_pixels(srcy, srcUnit, image->yres, graphics->printer_display);
3125 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres, graphics->printer_display);
3126 srcheight = units_to_pixels(srcheight, srcUnit, image->yres, graphics->printer_display);
3127 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3129 if (image->type == ImageTypeBitmap)
3131 GpBitmap* bitmap = (GpBitmap*)image;
3132 BOOL do_resampling = FALSE;
3133 BOOL use_software = FALSE;
3135 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3136 graphics->xres, graphics->yres,
3137 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3138 graphics->scale, image->xres, image->yres, bitmap->format,
3139 imageAttributes ? imageAttributes->outside_color : 0);
3141 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3142 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3143 srcx < 0 || srcy < 0 ||
3144 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3145 do_resampling = TRUE;
3147 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
3148 (graphics->image && graphics->image->type == ImageTypeBitmap))
3149 use_software = TRUE;
3151 if (use_software)
3153 RECT dst_area;
3154 GpRectF graphics_bounds;
3155 GpRect src_area;
3156 int i, x, y, src_stride, dst_stride;
3157 GpMatrix dst_to_src;
3158 REAL m11, m12, m21, m22, mdx, mdy;
3159 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
3160 BitmapData lockeddata;
3161 InterpolationMode interpolation = graphics->interpolation;
3162 PixelOffsetMode offset_mode = graphics->pixeloffset;
3163 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3164 REAL x_dx, x_dy, y_dx, y_dy;
3165 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3167 if (!imageAttributes)
3168 imageAttributes = &defaultImageAttributes;
3170 dst_area.left = dst_area.right = pti[0].x;
3171 dst_area.top = dst_area.bottom = pti[0].y;
3172 for (i=1; i<4; i++)
3174 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3175 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3176 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3177 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3180 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
3181 if (stat != Ok) return stat;
3183 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
3184 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
3185 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
3186 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
3188 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3190 if (IsRectEmpty(&dst_area)) return Ok;
3192 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3193 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3194 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3195 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3196 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3197 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3199 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3201 stat = GdipInvertMatrix(&dst_to_src);
3202 if (stat != Ok) return stat;
3204 if (do_resampling)
3206 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3207 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3209 else
3211 /* Make sure src_area is equal in size to dst_area. */
3212 src_area.X = srcx + dst_area.left - pti[0].x;
3213 src_area.Y = srcy + dst_area.top - pti[0].y;
3214 src_area.Width = dst_area.right - dst_area.left;
3215 src_area.Height = dst_area.bottom - dst_area.top;
3218 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3220 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
3221 if (!src_data)
3222 return OutOfMemory;
3223 src_stride = sizeof(ARGB) * src_area.Width;
3225 /* Read the bits we need from the source bitmap into a compatible buffer. */
3226 lockeddata.Width = src_area.Width;
3227 lockeddata.Height = src_area.Height;
3228 lockeddata.Stride = src_stride;
3229 lockeddata.Scan0 = src_data;
3230 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
3231 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
3232 else
3233 lockeddata.PixelFormat = PixelFormat32bppARGB;
3235 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3236 lockeddata.PixelFormat, &lockeddata);
3238 if (stat == Ok)
3239 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3241 if (stat != Ok)
3243 heap_free(src_data);
3244 return stat;
3247 apply_image_attributes(imageAttributes, src_data,
3248 src_area.Width, src_area.Height,
3249 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3251 if (do_resampling)
3253 /* Transform the bits as needed to the destination. */
3254 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3255 if (!dst_data)
3257 heap_free(src_data);
3258 return OutOfMemory;
3261 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3263 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3265 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3266 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3267 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3268 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3270 for (x=dst_area.left; x<dst_area.right; x++)
3272 for (y=dst_area.top; y<dst_area.bottom; y++)
3274 GpPointF src_pointf;
3275 ARGB *dst_color;
3277 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3278 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3280 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3282 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3283 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3284 imageAttributes, interpolation, offset_mode);
3285 else
3286 *dst_color = 0;
3290 else
3292 dst_data = src_data;
3293 dst_stride = src_stride;
3296 gdi_transform_acquire(graphics);
3298 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3299 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3300 lockeddata.PixelFormat);
3302 gdi_transform_release(graphics);
3304 heap_free(src_data);
3306 heap_free(dst_dyn_data);
3308 return stat;
3310 else
3312 HDC hdc;
3313 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3314 HBITMAP hbitmap, old_hbm=NULL;
3315 HRGN hrgn;
3316 INT save_state;
3318 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3319 bitmap->format == PixelFormat24bppRGB ||
3320 bitmap->format == PixelFormat32bppRGB ||
3321 bitmap->format == PixelFormat32bppPARGB))
3323 BITMAPINFOHEADER bih;
3324 BYTE *temp_bits;
3325 PixelFormat dst_format;
3327 /* we can't draw a bitmap of this format directly */
3328 hdc = CreateCompatibleDC(0);
3329 temp_hdc = TRUE;
3330 temp_bitmap = TRUE;
3332 bih.biSize = sizeof(BITMAPINFOHEADER);
3333 bih.biWidth = bitmap->width;
3334 bih.biHeight = -bitmap->height;
3335 bih.biPlanes = 1;
3336 bih.biBitCount = 32;
3337 bih.biCompression = BI_RGB;
3338 bih.biSizeImage = 0;
3339 bih.biXPelsPerMeter = 0;
3340 bih.biYPelsPerMeter = 0;
3341 bih.biClrUsed = 0;
3342 bih.biClrImportant = 0;
3344 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3345 (void**)&temp_bits, NULL, 0);
3347 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3348 dst_format = PixelFormat32bppPARGB;
3349 else
3350 dst_format = PixelFormat32bppRGB;
3352 convert_pixels(bitmap->width, bitmap->height,
3353 bitmap->width*4, temp_bits, dst_format,
3354 bitmap->stride, bitmap->bits, bitmap->format,
3355 bitmap->image.palette);
3357 else
3359 if (bitmap->hbitmap)
3360 hbitmap = bitmap->hbitmap;
3361 else
3363 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3364 temp_bitmap = TRUE;
3367 hdc = bitmap->hdc;
3368 temp_hdc = (hdc == 0);
3371 if (temp_hdc)
3373 if (!hdc) hdc = CreateCompatibleDC(0);
3374 old_hbm = SelectObject(hdc, hbitmap);
3377 save_state = SaveDC(graphics->hdc);
3379 stat = get_clip_hrgn(graphics, &hrgn);
3381 if (stat == Ok)
3383 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3384 DeleteObject(hrgn);
3387 gdi_transform_acquire(graphics);
3389 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3391 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3392 hdc, srcx, srcy, srcwidth, srcheight);
3394 else
3396 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3397 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3400 gdi_transform_release(graphics);
3402 RestoreDC(graphics->hdc, save_state);
3404 if (temp_hdc)
3406 SelectObject(hdc, old_hbm);
3407 DeleteDC(hdc);
3410 if (temp_bitmap)
3411 DeleteObject(hbitmap);
3414 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3416 GpRectF rc;
3418 set_rect(&rc, srcx, srcy, srcwidth, srcheight);
3419 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3420 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3422 else
3424 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3425 return InvalidParameter;
3428 return Ok;
3431 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3432 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3433 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3434 DrawImageAbort callback, VOID * callbackData)
3436 GpPointF pointsF[3];
3437 INT i;
3439 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3440 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3441 callbackData);
3443 if(!points || count!=3)
3444 return InvalidParameter;
3446 for(i = 0; i < count; i++){
3447 pointsF[i].X = (REAL)points[i].X;
3448 pointsF[i].Y = (REAL)points[i].Y;
3451 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3452 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3453 callback, callbackData);
3456 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3457 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3458 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3459 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3460 VOID * callbackData)
3462 GpPointF points[3];
3464 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3465 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3466 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3468 points[0].X = dstx;
3469 points[0].Y = dsty;
3470 points[1].X = dstx + dstwidth;
3471 points[1].Y = dsty;
3472 points[2].X = dstx;
3473 points[2].Y = dsty + dstheight;
3475 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3476 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3479 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3480 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3481 INT srcwidth, INT srcheight, GpUnit srcUnit,
3482 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3483 VOID * callbackData)
3485 GpPointF points[3];
3487 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3488 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3489 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3491 points[0].X = dstx;
3492 points[0].Y = dsty;
3493 points[1].X = dstx + dstwidth;
3494 points[1].Y = dsty;
3495 points[2].X = dstx;
3496 points[2].Y = dsty + dstheight;
3498 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3499 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3502 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3503 REAL x, REAL y, REAL width, REAL height)
3505 RectF bounds;
3506 GpUnit unit;
3507 GpStatus ret;
3509 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3511 if(!graphics || !image)
3512 return InvalidParameter;
3514 ret = GdipGetImageBounds(image, &bounds, &unit);
3515 if(ret != Ok)
3516 return ret;
3518 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3519 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3520 unit, NULL, NULL, NULL);
3523 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3524 INT x, INT y, INT width, INT height)
3526 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3528 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3531 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3532 REAL y1, REAL x2, REAL y2)
3534 GpPointF pt[2];
3536 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3538 if (!pen)
3539 return InvalidParameter;
3541 if (pen->unit == UnitPixel && pen->width <= 0.0)
3542 return Ok;
3544 pt[0].X = x1;
3545 pt[0].Y = y1;
3546 pt[1].X = x2;
3547 pt[1].Y = y2;
3548 return GdipDrawLines(graphics, pen, pt, 2);
3551 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3552 INT y1, INT x2, INT y2)
3554 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3556 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3559 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3560 GpPointF *points, INT count)
3562 GpStatus status;
3563 GpPath *path;
3565 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3567 if(!pen || !graphics || (count < 2))
3568 return InvalidParameter;
3570 if(graphics->busy)
3571 return ObjectBusy;
3573 status = GdipCreatePath(FillModeAlternate, &path);
3574 if (status != Ok) return status;
3576 status = GdipAddPathLine2(path, points, count);
3577 if (status == Ok)
3578 status = GdipDrawPath(graphics, pen, path);
3580 GdipDeletePath(path);
3581 return status;
3584 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3585 GpPoint *points, INT count)
3587 GpStatus retval;
3588 GpPointF *ptf;
3589 int i;
3591 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3593 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3594 if(!ptf) return OutOfMemory;
3596 for(i = 0; i < count; i ++){
3597 ptf[i].X = (REAL) points[i].X;
3598 ptf[i].Y = (REAL) points[i].Y;
3601 retval = GdipDrawLines(graphics, pen, ptf, count);
3603 heap_free(ptf);
3604 return retval;
3607 static GpStatus GDI32_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3609 INT save_state;
3610 GpStatus retval;
3611 HRGN hrgn=NULL;
3613 save_state = prepare_dc(graphics, pen);
3615 retval = get_clip_hrgn(graphics, &hrgn);
3617 if (retval != Ok)
3618 goto end;
3620 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
3622 gdi_transform_acquire(graphics);
3624 retval = draw_poly(graphics, pen, path->pathdata.Points,
3625 path->pathdata.Types, path->pathdata.Count, TRUE);
3627 gdi_transform_release(graphics);
3629 end:
3630 restore_dc(graphics, save_state);
3631 DeleteObject(hrgn);
3633 return retval;
3636 static GpStatus SOFTWARE_GdipDrawThinPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3638 GpStatus stat;
3639 GpPath* flat_path;
3640 GpMatrix* transform;
3641 GpRectF gp_bound_rect;
3642 GpRect gp_output_area;
3643 RECT output_area;
3644 INT output_height, output_width;
3645 DWORD *output_bits, *brush_bits=NULL;
3646 int i;
3647 static const BYTE static_dash_pattern[] = {1,1,1,0,1,0,1,0};
3648 const BYTE *dash_pattern;
3649 INT dash_pattern_size;
3650 BYTE *dyn_dash_pattern = NULL;
3652 stat = GdipClonePath(path, &flat_path);
3654 if (stat != Ok)
3655 return stat;
3657 stat = GdipCreateMatrix(&transform);
3659 if (stat == Ok)
3661 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3662 CoordinateSpaceWorld, transform);
3664 if (stat == Ok)
3665 stat = GdipFlattenPath(flat_path, transform, 1.0);
3667 GdipDeleteMatrix(transform);
3670 /* estimate the output size in pixels, can be larger than necessary */
3671 if (stat == Ok)
3673 output_area.left = floorf(flat_path->pathdata.Points[0].X);
3674 output_area.right = ceilf(flat_path->pathdata.Points[0].X);
3675 output_area.top = floorf(flat_path->pathdata.Points[0].Y);
3676 output_area.bottom = ceilf(flat_path->pathdata.Points[0].Y);
3678 for (i=1; i<flat_path->pathdata.Count; i++)
3680 REAL x, y;
3681 x = flat_path->pathdata.Points[i].X;
3682 y = flat_path->pathdata.Points[i].Y;
3684 if (floorf(x) < output_area.left) output_area.left = floorf(x);
3685 if (floorf(y) < output_area.top) output_area.top = floorf(y);
3686 if (ceilf(x) > output_area.right) output_area.right = ceilf(x);
3687 if (ceilf(y) > output_area.bottom) output_area.bottom = ceilf(y);
3690 stat = get_graphics_device_bounds(graphics, &gp_bound_rect);
3693 if (stat == Ok)
3695 output_area.left = max(output_area.left, floorf(gp_bound_rect.X));
3696 output_area.top = max(output_area.top, floorf(gp_bound_rect.Y));
3697 output_area.right = min(output_area.right, ceilf(gp_bound_rect.X + gp_bound_rect.Width));
3698 output_area.bottom = min(output_area.bottom, ceilf(gp_bound_rect.Y + gp_bound_rect.Height));
3700 output_width = output_area.right - output_area.left + 1;
3701 output_height = output_area.bottom - output_area.top + 1;
3703 if (output_width <= 0 || output_height <= 0)
3705 GdipDeletePath(flat_path);
3706 return Ok;
3709 gp_output_area.X = output_area.left;
3710 gp_output_area.Y = output_area.top;
3711 gp_output_area.Width = output_width;
3712 gp_output_area.Height = output_height;
3714 output_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3715 if (!output_bits)
3716 stat = OutOfMemory;
3719 if (stat == Ok)
3721 if (pen->brush->bt != BrushTypeSolidColor)
3723 /* allocate and draw brush output */
3724 brush_bits = heap_alloc_zero(output_width * output_height * sizeof(DWORD));
3726 if (brush_bits)
3728 stat = brush_fill_pixels(graphics, pen->brush, brush_bits,
3729 &gp_output_area, output_width);
3731 else
3732 stat = OutOfMemory;
3735 if (stat == Ok)
3737 /* convert dash pattern to bool array */
3738 switch (pen->dash)
3740 case DashStyleCustom:
3742 dash_pattern_size = 0;
3744 for (i=0; i < pen->numdashes; i++)
3745 dash_pattern_size += gdip_round(pen->dashes[i]);
3747 if (dash_pattern_size != 0)
3749 dash_pattern = dyn_dash_pattern = heap_alloc(dash_pattern_size);
3751 if (dyn_dash_pattern)
3753 int j=0;
3754 for (i=0; i < pen->numdashes; i++)
3756 int k;
3757 for (k=0; k < gdip_round(pen->dashes[i]); k++)
3758 dyn_dash_pattern[j++] = (i&1)^1;
3761 else
3762 stat = OutOfMemory;
3764 break;
3766 /* else fall through */
3768 case DashStyleSolid:
3769 default:
3770 dash_pattern = static_dash_pattern;
3771 dash_pattern_size = 1;
3772 break;
3773 case DashStyleDash:
3774 dash_pattern = static_dash_pattern;
3775 dash_pattern_size = 4;
3776 break;
3777 case DashStyleDot:
3778 dash_pattern = &static_dash_pattern[4];
3779 dash_pattern_size = 2;
3780 break;
3781 case DashStyleDashDot:
3782 dash_pattern = static_dash_pattern;
3783 dash_pattern_size = 6;
3784 break;
3785 case DashStyleDashDotDot:
3786 dash_pattern = static_dash_pattern;
3787 dash_pattern_size = 8;
3788 break;
3792 if (stat == Ok)
3794 /* trace path */
3795 GpPointF subpath_start = flat_path->pathdata.Points[0];
3796 INT prev_x = INT_MAX, prev_y = INT_MAX;
3797 int dash_pos = dash_pattern_size - 1;
3799 for (i=0; i < flat_path->pathdata.Count; i++)
3801 BYTE type, type2;
3802 GpPointF start_point, end_point;
3803 GpPoint start_pointi, end_pointi;
3805 type = flat_path->pathdata.Types[i];
3806 if (i+1 < flat_path->pathdata.Count)
3807 type2 = flat_path->pathdata.Types[i+1];
3808 else
3809 type2 = PathPointTypeStart;
3811 start_point = flat_path->pathdata.Points[i];
3813 if ((type & PathPointTypePathTypeMask) == PathPointTypeStart)
3814 subpath_start = start_point;
3816 if ((type & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
3817 end_point = subpath_start;
3818 else if ((type2 & PathPointTypePathTypeMask) == PathPointTypeStart)
3819 continue;
3820 else
3821 end_point = flat_path->pathdata.Points[i+1];
3823 start_pointi.X = floorf(start_point.X);
3824 start_pointi.Y = floorf(start_point.Y);
3825 end_pointi.X = floorf(end_point.X);
3826 end_pointi.Y = floorf(end_point.Y);
3828 if(start_pointi.X == end_pointi.X && start_pointi.Y == end_pointi.Y)
3829 continue;
3831 /* draw line segment */
3832 if (abs(start_pointi.Y - end_pointi.Y) > abs(start_pointi.X - end_pointi.X))
3834 INT x, y, start_y, end_y, step;
3836 if (start_pointi.Y < end_pointi.Y)
3838 step = 1;
3839 start_y = ceilf(start_point.Y) - output_area.top;
3840 end_y = end_pointi.Y - output_area.top;
3842 else
3844 step = -1;
3845 start_y = start_point.Y - output_area.top;
3846 end_y = ceilf(end_point.Y) - output_area.top;
3849 for (y=start_y; y != (end_y+step); y+=step)
3851 x = gdip_round( start_point.X +
3852 (end_point.X - start_point.X) * (y + output_area.top - start_point.Y) / (end_point.Y - start_point.Y) )
3853 - output_area.left;
3855 if (x == prev_x && y == prev_y)
3856 continue;
3858 prev_x = x;
3859 prev_y = y;
3860 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3862 if (!dash_pattern[dash_pos])
3863 continue;
3865 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3866 continue;
3868 if (brush_bits)
3869 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3870 else
3871 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3874 else
3876 INT x, y, start_x, end_x, step;
3878 if (start_pointi.X < end_pointi.X)
3880 step = 1;
3881 start_x = ceilf(start_point.X) - output_area.left;
3882 end_x = end_pointi.X - output_area.left;
3884 else
3886 step = -1;
3887 start_x = start_point.X - output_area.left;
3888 end_x = ceilf(end_point.X) - output_area.left;
3891 for (x=start_x; x != (end_x+step); x+=step)
3893 y = gdip_round( start_point.Y +
3894 (end_point.Y - start_point.Y) * (x + output_area.left - start_point.X) / (end_point.X - start_point.X) )
3895 - output_area.top;
3897 if (x == prev_x && y == prev_y)
3898 continue;
3900 prev_x = x;
3901 prev_y = y;
3902 dash_pos = (dash_pos + 1 == dash_pattern_size) ? 0 : dash_pos + 1;
3904 if (!dash_pattern[dash_pos])
3905 continue;
3907 if (x < 0 || x >= output_width || y < 0 || y >= output_height)
3908 continue;
3910 if (brush_bits)
3911 output_bits[x + y*output_width] = brush_bits[x + y*output_width];
3912 else
3913 output_bits[x + y*output_width] = ((GpSolidFill*)pen->brush)->color;
3919 /* draw output image */
3920 if (stat == Ok)
3922 gdi_transform_acquire(graphics);
3924 stat = alpha_blend_pixels(graphics, output_area.left, output_area.top,
3925 (BYTE*)output_bits, output_width, output_height, output_width * 4,
3926 PixelFormat32bppARGB);
3928 gdi_transform_release(graphics);
3931 heap_free(brush_bits);
3932 heap_free(dyn_dash_pattern);
3933 heap_free(output_bits);
3936 GdipDeletePath(flat_path);
3938 return stat;
3941 static GpStatus SOFTWARE_GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3943 GpStatus stat;
3944 GpPath *wide_path;
3945 GpMatrix *transform=NULL;
3946 REAL flatness=1.0;
3948 /* Check if the final pen thickness in pixels is too thin. */
3949 if (pen->unit == UnitPixel)
3951 if (pen->width < 1.415)
3952 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3954 else
3956 GpPointF points[3] = {{0,0}, {1,0}, {0,1}};
3958 points[1].X = pen->width;
3959 points[2].Y = pen->width;
3961 stat = gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice,
3962 CoordinateSpaceWorld, points, 3);
3964 if (stat != Ok)
3965 return stat;
3967 if (((points[1].X-points[0].X)*(points[1].X-points[0].X) +
3968 (points[1].Y-points[0].Y)*(points[1].Y-points[0].Y) < 2.0001) &&
3969 ((points[2].X-points[0].X)*(points[2].X-points[0].X) +
3970 (points[2].Y-points[0].Y)*(points[2].Y-points[0].Y) < 2.0001))
3971 return SOFTWARE_GdipDrawThinPath(graphics, pen, path);
3974 stat = GdipClonePath(path, &wide_path);
3976 if (stat != Ok)
3977 return stat;
3979 if (pen->unit == UnitPixel)
3981 /* We have to transform this to device coordinates to get the widths right. */
3982 stat = GdipCreateMatrix(&transform);
3984 if (stat == Ok)
3985 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3986 CoordinateSpaceWorld, transform);
3988 else
3990 /* Set flatness based on the final coordinate space */
3991 GpMatrix t;
3993 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
3994 CoordinateSpaceWorld, &t);
3996 if (stat != Ok)
3997 return stat;
3999 flatness = 1.0/sqrt(fmax(
4000 t.matrix[0] * t.matrix[0] + t.matrix[1] * t.matrix[1],
4001 t.matrix[2] * t.matrix[2] + t.matrix[3] * t.matrix[3]));
4004 if (stat == Ok)
4005 stat = GdipWidenPath(wide_path, pen, transform, flatness);
4007 if (pen->unit == UnitPixel)
4009 /* Transform the path back to world coordinates */
4010 if (stat == Ok)
4011 stat = GdipInvertMatrix(transform);
4013 if (stat == Ok)
4014 stat = GdipTransformPath(wide_path, transform);
4017 /* Actually draw the path */
4018 if (stat == Ok)
4019 stat = GdipFillPath(graphics, pen->brush, wide_path);
4021 GdipDeleteMatrix(transform);
4023 GdipDeletePath(wide_path);
4025 return stat;
4028 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
4030 GpStatus retval;
4032 TRACE("(%p, %p, %p)\n", graphics, pen, path);
4034 if(!pen || !graphics)
4035 return InvalidParameter;
4037 if(graphics->busy)
4038 return ObjectBusy;
4040 if (path->pathdata.Count == 0)
4041 return Ok;
4043 if (is_metafile_graphics(graphics))
4044 retval = METAFILE_DrawPath((GpMetafile*)graphics->image, pen, path);
4045 else if (!graphics->hdc || graphics->alpha_hdc || !brush_can_fill_path(pen->brush, FALSE))
4046 retval = SOFTWARE_GdipDrawPath(graphics, pen, path);
4047 else
4048 retval = GDI32_GdipDrawPath(graphics, pen, path);
4050 return retval;
4053 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
4054 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4056 GpStatus status;
4057 GpPath *path;
4059 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
4060 width, height, startAngle, sweepAngle);
4062 if(!graphics || !pen)
4063 return InvalidParameter;
4065 if(graphics->busy)
4066 return ObjectBusy;
4068 status = GdipCreatePath(FillModeAlternate, &path);
4069 if (status != Ok) return status;
4071 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4072 if (status == Ok)
4073 status = GdipDrawPath(graphics, pen, path);
4075 GdipDeletePath(path);
4076 return status;
4079 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
4080 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4082 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
4083 width, height, startAngle, sweepAngle);
4085 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4088 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
4089 REAL y, REAL width, REAL height)
4091 GpRectF rect;
4093 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
4095 set_rect(&rect, x, y, width, height);
4096 return GdipDrawRectangles(graphics, pen, &rect, 1);
4099 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
4100 INT y, INT width, INT height)
4102 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
4104 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4107 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
4108 GDIPCONST GpRectF* rects, INT count)
4110 GpStatus status;
4111 GpPath *path;
4113 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4115 if(!graphics || !pen || !rects || count < 1)
4116 return InvalidParameter;
4118 if(graphics->busy)
4119 return ObjectBusy;
4121 if (is_metafile_graphics(graphics))
4122 return METAFILE_DrawRectangles((GpMetafile *)graphics->image, pen, rects, count);
4124 status = GdipCreatePath(FillModeAlternate, &path);
4125 if (status != Ok) return status;
4127 status = GdipAddPathRectangles(path, rects, count);
4128 if (status == Ok)
4129 status = GdipDrawPath(graphics, pen, path);
4131 GdipDeletePath(path);
4132 return status;
4135 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
4136 GDIPCONST GpRect* rects, INT count)
4138 GpRectF *rectsF;
4139 GpStatus ret;
4140 INT i;
4142 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
4144 if(!rects || count<=0)
4145 return InvalidParameter;
4147 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
4148 if(!rectsF)
4149 return OutOfMemory;
4151 for(i = 0;i < count;i++)
4152 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4154 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
4155 heap_free(rectsF);
4157 return ret;
4160 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
4161 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
4163 GpPath *path;
4164 GpStatus status;
4166 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4167 count, tension, fill);
4169 if(!graphics || !brush || !points)
4170 return InvalidParameter;
4172 if(graphics->busy)
4173 return ObjectBusy;
4175 if(count == 1) /* Do nothing */
4176 return Ok;
4178 status = GdipCreatePath(fill, &path);
4179 if (status != Ok) return status;
4181 status = GdipAddPathClosedCurve2(path, points, count, tension);
4182 if (status == Ok)
4183 status = GdipFillPath(graphics, brush, path);
4185 GdipDeletePath(path);
4186 return status;
4189 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
4190 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
4192 GpPointF *ptf;
4193 GpStatus stat;
4194 INT i;
4196 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
4197 count, tension, fill);
4199 if(!points || count == 0)
4200 return InvalidParameter;
4202 if(count == 1) /* Do nothing */
4203 return Ok;
4205 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
4206 if(!ptf)
4207 return OutOfMemory;
4209 for(i = 0;i < count;i++){
4210 ptf[i].X = (REAL)points[i].X;
4211 ptf[i].Y = (REAL)points[i].Y;
4214 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
4216 heap_free(ptf);
4218 return stat;
4221 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
4222 GDIPCONST GpPointF *points, INT count)
4224 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4225 return GdipFillClosedCurve2(graphics, brush, points, count,
4226 0.5f, FillModeAlternate);
4229 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
4230 GDIPCONST GpPoint *points, INT count)
4232 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4233 return GdipFillClosedCurve2I(graphics, brush, points, count,
4234 0.5f, FillModeAlternate);
4237 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
4238 REAL y, REAL width, REAL height)
4240 GpStatus stat;
4241 GpPath *path;
4242 GpRectF rect;
4244 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4246 if(!graphics || !brush)
4247 return InvalidParameter;
4249 if(graphics->busy)
4250 return ObjectBusy;
4252 if (is_metafile_graphics(graphics))
4254 set_rect(&rect, x, y, width, height);
4255 return METAFILE_FillEllipse((GpMetafile *)graphics->image, brush, &rect);
4258 stat = GdipCreatePath(FillModeAlternate, &path);
4260 if (stat == Ok)
4262 stat = GdipAddPathEllipse(path, x, y, width, height);
4264 if (stat == Ok)
4265 stat = GdipFillPath(graphics, brush, path);
4267 GdipDeletePath(path);
4270 return stat;
4273 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
4274 INT y, INT width, INT height)
4276 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4278 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
4281 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4283 INT save_state;
4284 GpStatus retval;
4285 HRGN hrgn=NULL;
4287 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4288 return NotImplemented;
4290 save_state = SaveDC(graphics->hdc);
4291 EndPath(graphics->hdc);
4292 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4293 : WINDING));
4295 retval = get_clip_hrgn(graphics, &hrgn);
4297 if (retval != Ok)
4298 goto end;
4300 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4302 gdi_transform_acquire(graphics);
4304 BeginPath(graphics->hdc);
4305 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4306 path->pathdata.Types, path->pathdata.Count, FALSE);
4308 if(retval == Ok)
4310 EndPath(graphics->hdc);
4311 retval = brush_fill_path(graphics, brush);
4314 gdi_transform_release(graphics);
4316 end:
4317 RestoreDC(graphics->hdc, save_state);
4318 DeleteObject(hrgn);
4320 return retval;
4323 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4325 GpStatus stat;
4326 GpRegion *rgn;
4328 if (!brush_can_fill_pixels(brush))
4329 return NotImplemented;
4331 /* FIXME: This could probably be done more efficiently without regions. */
4333 stat = GdipCreateRegionPath(path, &rgn);
4335 if (stat == Ok)
4337 stat = GdipFillRegion(graphics, brush, rgn);
4339 GdipDeleteRegion(rgn);
4342 return stat;
4345 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4347 GpStatus stat = NotImplemented;
4349 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4351 if(!brush || !graphics || !path)
4352 return InvalidParameter;
4354 if(graphics->busy)
4355 return ObjectBusy;
4357 if (!path->pathdata.Count)
4358 return Ok;
4360 if (is_metafile_graphics(graphics))
4361 return METAFILE_FillPath((GpMetafile*)graphics->image, brush, path);
4363 if (!graphics->image && !graphics->alpha_hdc)
4364 stat = GDI32_GdipFillPath(graphics, brush, path);
4366 if (stat == NotImplemented)
4367 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4369 if (stat == NotImplemented)
4371 FIXME("Not implemented for brushtype %i\n", brush->bt);
4372 stat = Ok;
4375 return stat;
4378 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4379 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4381 GpStatus stat;
4382 GpPath *path;
4383 GpRectF rect;
4385 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4386 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4388 if(!graphics || !brush)
4389 return InvalidParameter;
4391 if(graphics->busy)
4392 return ObjectBusy;
4394 if (is_metafile_graphics(graphics))
4396 set_rect(&rect, x, y, width, height);
4397 return METAFILE_FillPie((GpMetafile *)graphics->image, brush, &rect, startAngle, sweepAngle);
4400 stat = GdipCreatePath(FillModeAlternate, &path);
4402 if (stat == Ok)
4404 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4406 if (stat == Ok)
4407 stat = GdipFillPath(graphics, brush, path);
4409 GdipDeletePath(path);
4412 return stat;
4415 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4416 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4418 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4419 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4421 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4424 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4425 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4427 GpStatus stat;
4428 GpPath *path;
4430 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4432 if(!graphics || !brush || !points || !count)
4433 return InvalidParameter;
4435 if(graphics->busy)
4436 return ObjectBusy;
4438 stat = GdipCreatePath(fillMode, &path);
4440 if (stat == Ok)
4442 stat = GdipAddPathPolygon(path, points, count);
4444 if (stat == Ok)
4445 stat = GdipFillPath(graphics, brush, path);
4447 GdipDeletePath(path);
4450 return stat;
4453 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4454 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4456 GpStatus stat;
4457 GpPath *path;
4459 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4461 if(!graphics || !brush || !points || !count)
4462 return InvalidParameter;
4464 if(graphics->busy)
4465 return ObjectBusy;
4467 stat = GdipCreatePath(fillMode, &path);
4469 if (stat == Ok)
4471 stat = GdipAddPathPolygonI(path, points, count);
4473 if (stat == Ok)
4474 stat = GdipFillPath(graphics, brush, path);
4476 GdipDeletePath(path);
4479 return stat;
4482 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4483 GDIPCONST GpPointF *points, INT count)
4485 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4487 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4490 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4491 GDIPCONST GpPoint *points, INT count)
4493 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4495 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4498 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4499 REAL x, REAL y, REAL width, REAL height)
4501 GpRectF rect;
4503 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4505 set_rect(&rect, x, y, width, height);
4506 return GdipFillRectangles(graphics, brush, &rect, 1);
4509 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4510 INT x, INT y, INT width, INT height)
4512 GpRectF rect;
4514 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4516 set_rect(&rect, x, y, width, height);
4517 return GdipFillRectangles(graphics, brush, &rect, 1);
4520 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4521 INT count)
4523 GpStatus status;
4524 GpPath *path;
4526 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4528 if(!graphics || !brush || !rects || count <= 0)
4529 return InvalidParameter;
4531 if (is_metafile_graphics(graphics))
4533 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
4534 /* FIXME: Add gdi32 drawing. */
4535 return status;
4538 status = GdipCreatePath(FillModeAlternate, &path);
4539 if (status != Ok) return status;
4541 status = GdipAddPathRectangles(path, rects, count);
4542 if (status == Ok)
4543 status = GdipFillPath(graphics, brush, path);
4545 GdipDeletePath(path);
4546 return status;
4549 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4550 INT count)
4552 GpRectF *rectsF;
4553 GpStatus ret;
4554 INT i;
4556 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4558 if(!rects || count <= 0)
4559 return InvalidParameter;
4561 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
4562 if(!rectsF)
4563 return OutOfMemory;
4565 for(i = 0; i < count; i++)
4566 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4568 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4569 heap_free(rectsF);
4571 return ret;
4574 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4575 GpRegion* region)
4577 INT save_state;
4578 GpStatus status;
4579 HRGN hrgn;
4580 RECT rc;
4582 if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
4583 return NotImplemented;
4585 save_state = SaveDC(graphics->hdc);
4586 EndPath(graphics->hdc);
4588 hrgn = NULL;
4589 status = get_clip_hrgn(graphics, &hrgn);
4590 if (status != Ok)
4592 RestoreDC(graphics->hdc, save_state);
4593 return status;
4596 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
4597 DeleteObject(hrgn);
4599 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4600 if (status != Ok)
4602 RestoreDC(graphics->hdc, save_state);
4603 return status;
4606 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4607 DeleteObject(hrgn);
4609 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4611 BeginPath(graphics->hdc);
4612 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4613 EndPath(graphics->hdc);
4615 status = brush_fill_path(graphics, brush);
4618 RestoreDC(graphics->hdc, save_state);
4621 return status;
4624 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4625 GpRegion* region)
4627 GpStatus stat;
4628 GpRegion *temp_region;
4629 GpMatrix world_to_device;
4630 GpRectF graphics_bounds;
4631 DWORD *pixel_data;
4632 HRGN hregion;
4633 RECT bound_rect;
4634 GpRect gp_bound_rect;
4636 if (!brush_can_fill_pixels(brush))
4637 return NotImplemented;
4639 stat = gdi_transform_acquire(graphics);
4641 if (stat == Ok)
4642 stat = get_graphics_device_bounds(graphics, &graphics_bounds);
4644 if (stat == Ok)
4645 stat = GdipCloneRegion(region, &temp_region);
4647 if (stat == Ok)
4649 stat = get_graphics_transform(graphics, WineCoordinateSpaceGdiDevice,
4650 CoordinateSpaceWorld, &world_to_device);
4652 if (stat == Ok)
4653 stat = GdipTransformRegion(temp_region, &world_to_device);
4655 if (stat == Ok)
4656 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4658 if (stat == Ok)
4659 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4661 GdipDeleteRegion(temp_region);
4664 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4666 DeleteObject(hregion);
4667 gdi_transform_release(graphics);
4668 return Ok;
4671 if (stat == Ok)
4673 gp_bound_rect.X = bound_rect.left;
4674 gp_bound_rect.Y = bound_rect.top;
4675 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4676 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4678 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4679 if (!pixel_data)
4680 stat = OutOfMemory;
4682 if (stat == Ok)
4684 stat = brush_fill_pixels(graphics, brush, pixel_data,
4685 &gp_bound_rect, gp_bound_rect.Width);
4687 if (stat == Ok)
4688 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4689 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4690 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4691 PixelFormat32bppARGB);
4693 heap_free(pixel_data);
4696 DeleteObject(hregion);
4699 gdi_transform_release(graphics);
4701 return stat;
4704 /*****************************************************************************
4705 * GdipFillRegion [GDIPLUS.@]
4707 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4708 GpRegion* region)
4710 GpStatus stat = NotImplemented;
4712 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4714 if (!(graphics && brush && region))
4715 return InvalidParameter;
4717 if(graphics->busy)
4718 return ObjectBusy;
4720 if (is_metafile_graphics(graphics))
4721 stat = METAFILE_FillRegion((GpMetafile*)graphics->image, brush, region);
4722 else
4724 if (!graphics->image && !graphics->alpha_hdc)
4725 stat = GDI32_GdipFillRegion(graphics, brush, region);
4727 if (stat == NotImplemented)
4728 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4731 if (stat == NotImplemented)
4733 FIXME("not implemented for brushtype %i\n", brush->bt);
4734 stat = Ok;
4737 return stat;
4740 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4742 TRACE("(%p,%u)\n", graphics, intention);
4744 if(!graphics)
4745 return InvalidParameter;
4747 if(graphics->busy)
4748 return ObjectBusy;
4750 /* We have no internal operation queue, so there's no need to clear it. */
4752 if (graphics->hdc)
4753 GdiFlush();
4755 return Ok;
4758 /*****************************************************************************
4759 * GdipGetClipBounds [GDIPLUS.@]
4761 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4763 GpStatus status;
4764 GpRegion *clip;
4766 TRACE("(%p, %p)\n", graphics, rect);
4768 if(!graphics)
4769 return InvalidParameter;
4771 if(graphics->busy)
4772 return ObjectBusy;
4774 status = GdipCreateRegion(&clip);
4775 if (status != Ok) return status;
4777 status = GdipGetClip(graphics, clip);
4778 if (status == Ok)
4779 status = GdipGetRegionBounds(clip, graphics, rect);
4781 GdipDeleteRegion(clip);
4782 return status;
4785 /*****************************************************************************
4786 * GdipGetClipBoundsI [GDIPLUS.@]
4788 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4790 GpRectF rectf;
4791 GpStatus stat;
4793 TRACE("(%p, %p)\n", graphics, rect);
4795 if (!rect)
4796 return InvalidParameter;
4798 if ((stat = GdipGetClipBounds(graphics, &rectf)) == Ok)
4800 rect->X = gdip_round(rectf.X);
4801 rect->Y = gdip_round(rectf.Y);
4802 rect->Width = gdip_round(rectf.Width);
4803 rect->Height = gdip_round(rectf.Height);
4806 return stat;
4809 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4810 CompositingMode *mode)
4812 TRACE("(%p, %p)\n", graphics, mode);
4814 if(!graphics || !mode)
4815 return InvalidParameter;
4817 if(graphics->busy)
4818 return ObjectBusy;
4820 *mode = graphics->compmode;
4822 return Ok;
4825 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4826 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4827 CompositingQuality *quality)
4829 TRACE("(%p, %p)\n", graphics, quality);
4831 if(!graphics || !quality)
4832 return InvalidParameter;
4834 if(graphics->busy)
4835 return ObjectBusy;
4837 *quality = graphics->compqual;
4839 return Ok;
4842 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4843 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4844 InterpolationMode *mode)
4846 TRACE("(%p, %p)\n", graphics, mode);
4848 if(!graphics || !mode)
4849 return InvalidParameter;
4851 if(graphics->busy)
4852 return ObjectBusy;
4854 *mode = graphics->interpolation;
4856 return Ok;
4859 /* FIXME: Need to handle color depths less than 24bpp */
4860 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4862 TRACE("(%p, %p)\n", graphics, argb);
4864 if(!graphics || !argb)
4865 return InvalidParameter;
4867 if(graphics->busy)
4868 return ObjectBusy;
4870 if (graphics->image && graphics->image->type == ImageTypeBitmap)
4872 static int once;
4873 GpBitmap *bitmap = (GpBitmap *)graphics->image;
4874 if (IsIndexedPixelFormat(bitmap->format) && !once++)
4875 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4878 return Ok;
4881 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4883 TRACE("(%p, %p)\n", graphics, scale);
4885 if(!graphics || !scale)
4886 return InvalidParameter;
4888 if(graphics->busy)
4889 return ObjectBusy;
4891 *scale = graphics->scale;
4893 return Ok;
4896 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4898 TRACE("(%p, %p)\n", graphics, unit);
4900 if(!graphics || !unit)
4901 return InvalidParameter;
4903 if(graphics->busy)
4904 return ObjectBusy;
4906 *unit = graphics->unit;
4908 return Ok;
4911 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4912 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4913 *mode)
4915 TRACE("(%p, %p)\n", graphics, mode);
4917 if(!graphics || !mode)
4918 return InvalidParameter;
4920 if(graphics->busy)
4921 return ObjectBusy;
4923 *mode = graphics->pixeloffset;
4925 return Ok;
4928 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4929 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4931 TRACE("(%p, %p)\n", graphics, mode);
4933 if(!graphics || !mode)
4934 return InvalidParameter;
4936 if(graphics->busy)
4937 return ObjectBusy;
4939 *mode = graphics->smoothing;
4941 return Ok;
4944 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4946 TRACE("(%p, %p)\n", graphics, contrast);
4948 if(!graphics || !contrast)
4949 return InvalidParameter;
4951 *contrast = graphics->textcontrast;
4953 return Ok;
4956 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4957 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4958 TextRenderingHint *hint)
4960 TRACE("(%p, %p)\n", graphics, hint);
4962 if(!graphics || !hint)
4963 return InvalidParameter;
4965 if(graphics->busy)
4966 return ObjectBusy;
4968 *hint = graphics->texthint;
4970 return Ok;
4973 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4975 GpRegion *clip_rgn;
4976 GpStatus stat;
4977 GpMatrix device_to_world;
4979 TRACE("(%p, %p)\n", graphics, rect);
4981 if(!graphics || !rect)
4982 return InvalidParameter;
4984 if(graphics->busy)
4985 return ObjectBusy;
4987 /* intersect window and graphics clipping regions */
4988 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4989 return stat;
4991 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4992 goto cleanup;
4994 /* transform to world coordinates */
4995 if((stat = get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world)) != Ok)
4996 goto cleanup;
4998 if((stat = GdipTransformRegion(clip_rgn, &device_to_world)) != Ok)
4999 goto cleanup;
5001 /* get bounds of the region */
5002 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
5004 cleanup:
5005 GdipDeleteRegion(clip_rgn);
5007 return stat;
5010 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
5012 GpRectF rectf;
5013 GpStatus stat;
5015 TRACE("(%p, %p)\n", graphics, rect);
5017 if(!graphics || !rect)
5018 return InvalidParameter;
5020 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
5022 rect->X = gdip_round(rectf.X);
5023 rect->Y = gdip_round(rectf.Y);
5024 rect->Width = gdip_round(rectf.Width);
5025 rect->Height = gdip_round(rectf.Height);
5028 return stat;
5031 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5033 TRACE("(%p, %p)\n", graphics, matrix);
5035 if(!graphics || !matrix)
5036 return InvalidParameter;
5038 if(graphics->busy)
5039 return ObjectBusy;
5041 *matrix = graphics->worldtrans;
5042 return Ok;
5045 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
5047 GpSolidFill *brush;
5048 GpStatus stat;
5049 GpRectF wnd_rect;
5050 CompositingMode prev_comp_mode;
5052 TRACE("(%p, %x)\n", graphics, color);
5054 if(!graphics)
5055 return InvalidParameter;
5057 if(graphics->busy)
5058 return ObjectBusy;
5060 if (is_metafile_graphics(graphics))
5061 return METAFILE_GraphicsClear((GpMetafile*)graphics->image, color);
5063 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
5064 return stat;
5066 if((stat = GdipGetVisibleClipBounds(graphics, &wnd_rect)) != Ok){
5067 GdipDeleteBrush((GpBrush*)brush);
5068 return stat;
5071 GdipGetCompositingMode(graphics, &prev_comp_mode);
5072 GdipSetCompositingMode(graphics, CompositingModeSourceCopy);
5073 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
5074 wnd_rect.Width, wnd_rect.Height);
5075 GdipSetCompositingMode(graphics, prev_comp_mode);
5077 GdipDeleteBrush((GpBrush*)brush);
5079 return Ok;
5082 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
5084 TRACE("(%p, %p)\n", graphics, res);
5086 if(!graphics || !res)
5087 return InvalidParameter;
5089 return GdipIsEmptyRegion(graphics->clip, graphics, res);
5092 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
5094 GpStatus stat;
5095 GpRegion* rgn;
5096 GpPointF pt;
5098 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
5100 if(!graphics || !result)
5101 return InvalidParameter;
5103 if(graphics->busy)
5104 return ObjectBusy;
5106 pt.X = x;
5107 pt.Y = y;
5108 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5109 CoordinateSpaceWorld, &pt, 1)) != Ok)
5110 return stat;
5112 if((stat = GdipCreateRegion(&rgn)) != Ok)
5113 return stat;
5115 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5116 goto cleanup;
5118 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
5120 cleanup:
5121 GdipDeleteRegion(rgn);
5122 return stat;
5125 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
5127 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
5130 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
5132 GpStatus stat;
5133 GpRegion* rgn;
5134 GpPointF pts[2];
5136 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
5138 if(!graphics || !result)
5139 return InvalidParameter;
5141 if(graphics->busy)
5142 return ObjectBusy;
5144 pts[0].X = x;
5145 pts[0].Y = y;
5146 pts[1].X = x + width;
5147 pts[1].Y = y + height;
5149 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
5150 CoordinateSpaceWorld, pts, 2)) != Ok)
5151 return stat;
5153 pts[1].X -= pts[0].X;
5154 pts[1].Y -= pts[0].Y;
5156 if((stat = GdipCreateRegion(&rgn)) != Ok)
5157 return stat;
5159 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5160 goto cleanup;
5162 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
5164 cleanup:
5165 GdipDeleteRegion(rgn);
5166 return stat;
5169 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
5171 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
5174 GpStatus gdip_format_string(HDC hdc,
5175 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5176 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
5177 gdip_format_string_callback callback, void *user_data)
5179 WCHAR* stringdup;
5180 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
5181 nheight, lineend, lineno = 0;
5182 RectF bounds;
5183 StringAlignment halign;
5184 GpStatus stat = Ok;
5185 SIZE size;
5186 HotkeyPrefix hkprefix;
5187 INT *hotkeyprefix_offsets=NULL;
5188 INT hotkeyprefix_count=0;
5189 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
5190 BOOL seen_prefix = FALSE;
5192 if(length == -1) length = lstrlenW(string);
5194 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
5195 if(!stringdup) return OutOfMemory;
5197 if (!format)
5198 format = &default_drawstring_format;
5200 nwidth = rect->Width;
5201 nheight = rect->Height;
5202 if (ignore_empty_clip)
5204 if (!nwidth) nwidth = INT_MAX;
5205 if (!nheight) nheight = INT_MAX;
5208 hkprefix = format->hkprefix;
5210 if (hkprefix == HotkeyPrefixShow)
5212 for (i=0; i<length; i++)
5214 if (string[i] == '&')
5215 hotkeyprefix_count++;
5219 if (hotkeyprefix_count)
5221 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
5222 if (!hotkeyprefix_offsets)
5224 heap_free(stringdup);
5225 return OutOfMemory;
5229 hotkeyprefix_count = 0;
5231 for(i = 0, j = 0; i < length; i++){
5232 /* FIXME: This makes the indexes passed to callback inaccurate. */
5233 if(!iswprint(string[i]) && (string[i] != '\n'))
5234 continue;
5236 /* FIXME: tabs should be handled using tabstops from stringformat */
5237 if (string[i] == '\t')
5238 continue;
5240 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
5241 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
5242 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
5244 seen_prefix = TRUE;
5245 continue;
5248 seen_prefix = FALSE;
5250 stringdup[j] = string[i];
5251 j++;
5254 length = j;
5256 halign = format->align;
5258 while(sum < length){
5259 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
5260 nwidth, &fit, NULL, &size);
5261 fitcpy = fit;
5263 if(fit == 0)
5264 break;
5266 for(lret = 0; lret < fit; lret++)
5267 if(*(stringdup + sum + lret) == '\n')
5268 break;
5270 /* Line break code (may look strange, but it imitates windows). */
5271 if(lret < fit)
5272 lineend = fit = lret; /* this is not an off-by-one error */
5273 else if(fit < (length - sum)){
5274 if(*(stringdup + sum + fit) == ' ')
5275 while(*(stringdup + sum + fit) == ' ')
5276 fit++;
5277 else if (!(format->attr & StringFormatFlagsNoWrap))
5278 while(*(stringdup + sum + fit - 1) != ' '){
5279 fit--;
5281 if(*(stringdup + sum + fit) == '\t')
5282 break;
5284 if(fit == 0){
5285 fit = fitcpy;
5286 break;
5289 lineend = fit;
5290 while(*(stringdup + sum + lineend - 1) == ' ' ||
5291 *(stringdup + sum + lineend - 1) == '\t')
5292 lineend--;
5294 else
5295 lineend = fit;
5297 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
5298 nwidth, &j, NULL, &size);
5300 bounds.Width = size.cx;
5302 if(height + size.cy > nheight)
5304 if (format->attr & StringFormatFlagsLineLimit)
5305 break;
5306 bounds.Height = nheight - (height + size.cy);
5308 else
5309 bounds.Height = size.cy;
5311 bounds.Y = rect->Y + height;
5313 switch (halign)
5315 case StringAlignmentNear:
5316 default:
5317 bounds.X = rect->X;
5318 break;
5319 case StringAlignmentCenter:
5320 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
5321 break;
5322 case StringAlignmentFar:
5323 bounds.X = rect->X + rect->Width - bounds.Width;
5324 break;
5327 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
5328 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
5329 break;
5331 stat = callback(hdc, stringdup, sum, lineend,
5332 font, rect, format, lineno, &bounds,
5333 &hotkeyprefix_offsets[hotkeyprefix_pos],
5334 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
5336 if (stat != Ok)
5337 break;
5339 sum += fit + (lret < fitcpy ? 1 : 0);
5340 height += size.cy;
5341 lineno++;
5343 hotkeyprefix_pos = hotkeyprefix_end_pos;
5345 if(height > nheight)
5346 break;
5348 /* Stop if this was a linewrap (but not if it was a linebreak). */
5349 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
5350 break;
5353 heap_free(stringdup);
5354 heap_free(hotkeyprefix_offsets);
5356 return stat;
5359 struct measure_ranges_args {
5360 GpRegion **regions;
5361 REAL rel_width, rel_height;
5364 static GpStatus measure_ranges_callback(HDC hdc,
5365 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5366 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5367 INT lineno, const RectF *bounds, INT *underlined_indexes,
5368 INT underlined_index_count, void *user_data)
5370 int i;
5371 GpStatus stat = Ok;
5372 struct measure_ranges_args *args = user_data;
5374 for (i=0; i<format->range_count; i++)
5376 INT range_start = max(index, format->character_ranges[i].First);
5377 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
5378 if (range_start < range_end)
5380 GpRectF range_rect;
5381 SIZE range_size;
5383 range_rect.Y = bounds->Y / args->rel_height;
5384 range_rect.Height = bounds->Height / args->rel_height;
5386 GetTextExtentExPointW(hdc, string + index, range_start - index,
5387 INT_MAX, NULL, NULL, &range_size);
5388 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5390 GetTextExtentExPointW(hdc, string + index, range_end - index,
5391 INT_MAX, NULL, NULL, &range_size);
5392 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5394 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5395 if (stat != Ok)
5396 break;
5400 return stat;
5403 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5404 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5405 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5406 INT regionCount, GpRegion** regions)
5408 GpStatus stat;
5409 int i;
5410 HFONT gdifont, oldfont;
5411 struct measure_ranges_args args;
5412 HDC hdc, temp_hdc=NULL;
5413 GpPointF pt[3];
5414 RectF scaled_rect;
5415 REAL margin_x;
5417 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_wn(string, length),
5418 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5420 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5421 return InvalidParameter;
5423 if (regionCount < stringFormat->range_count)
5424 return InvalidParameter;
5426 if(!graphics->hdc)
5428 hdc = temp_hdc = CreateCompatibleDC(0);
5429 if (!temp_hdc) return OutOfMemory;
5431 else
5432 hdc = graphics->hdc;
5434 if (stringFormat->attr)
5435 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5437 pt[0].X = 0.0;
5438 pt[0].Y = 0.0;
5439 pt[1].X = 1.0;
5440 pt[1].Y = 0.0;
5441 pt[2].X = 0.0;
5442 pt[2].Y = 1.0;
5443 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5444 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5445 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5446 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5447 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5449 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5450 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5452 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5453 scaled_rect.Y = layoutRect->Y * args.rel_height;
5454 scaled_rect.Width = layoutRect->Width * args.rel_width;
5455 scaled_rect.Height = layoutRect->Height * args.rel_height;
5457 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5458 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5460 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL, NULL);
5461 oldfont = SelectObject(hdc, gdifont);
5463 for (i=0; i<stringFormat->range_count; i++)
5465 stat = GdipSetEmpty(regions[i]);
5466 if (stat != Ok)
5468 SelectObject(hdc, oldfont);
5469 DeleteObject(gdifont);
5470 if (temp_hdc)
5471 DeleteDC(temp_hdc);
5472 return stat;
5476 args.regions = regions;
5478 gdi_transform_acquire(graphics);
5480 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5481 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
5483 gdi_transform_release(graphics);
5485 SelectObject(hdc, oldfont);
5486 DeleteObject(gdifont);
5488 if (temp_hdc)
5489 DeleteDC(temp_hdc);
5491 return stat;
5494 struct measure_string_args {
5495 RectF *bounds;
5496 INT *codepointsfitted;
5497 INT *linesfilled;
5498 REAL rel_width, rel_height;
5501 static GpStatus measure_string_callback(HDC hdc,
5502 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5503 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5504 INT lineno, const RectF *bounds, INT *underlined_indexes,
5505 INT underlined_index_count, void *user_data)
5507 struct measure_string_args *args = user_data;
5508 REAL new_width, new_height;
5510 new_width = bounds->Width / args->rel_width;
5511 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5513 if (new_width > args->bounds->Width)
5514 args->bounds->Width = new_width;
5516 if (new_height > args->bounds->Height)
5517 args->bounds->Height = new_height;
5519 if (args->codepointsfitted)
5520 *args->codepointsfitted = index + length;
5522 if (args->linesfilled)
5523 (*args->linesfilled)++;
5525 return Ok;
5528 /* Find the smallest rectangle that bounds the text when it is printed in rect
5529 * according to the format options listed in format. If rect has 0 width and
5530 * height, then just find the smallest rectangle that bounds the text when it's
5531 * printed at location (rect->X, rect-Y). */
5532 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5533 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5534 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5535 INT *codepointsfitted, INT *linesfilled)
5537 HFONT oldfont, gdifont;
5538 struct measure_string_args args;
5539 HDC temp_hdc=NULL, hdc;
5540 GpPointF pt[3];
5541 RectF scaled_rect;
5542 REAL margin_x;
5543 INT lines, glyphs;
5545 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5546 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5547 bounds, codepointsfitted, linesfilled);
5549 if(!graphics || !string || !font || !rect || !bounds)
5550 return InvalidParameter;
5552 if(!graphics->hdc)
5554 hdc = temp_hdc = CreateCompatibleDC(0);
5555 if (!temp_hdc) return OutOfMemory;
5557 else
5558 hdc = graphics->hdc;
5560 if(linesfilled) *linesfilled = 0;
5561 if(codepointsfitted) *codepointsfitted = 0;
5563 if(format)
5564 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5566 pt[0].X = 0.0;
5567 pt[0].Y = 0.0;
5568 pt[1].X = 1.0;
5569 pt[1].Y = 0.0;
5570 pt[2].X = 0.0;
5571 pt[2].Y = 1.0;
5572 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5573 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5574 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5575 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5576 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5578 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5579 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5581 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5582 scaled_rect.Y = rect->Y * args.rel_height;
5583 scaled_rect.Width = rect->Width * args.rel_width;
5584 scaled_rect.Height = rect->Height * args.rel_height;
5585 if (scaled_rect.Width >= 0.5)
5587 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5588 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5591 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5592 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5594 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5595 oldfont = SelectObject(hdc, gdifont);
5597 set_rect(bounds, rect->X, rect->Y, 0.0f, 0.0f);
5599 args.bounds = bounds;
5600 args.codepointsfitted = &glyphs;
5601 args.linesfilled = &lines;
5602 lines = glyphs = 0;
5604 gdi_transform_acquire(graphics);
5606 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5607 measure_string_callback, &args);
5609 gdi_transform_release(graphics);
5611 if (linesfilled) *linesfilled = lines;
5612 if (codepointsfitted) *codepointsfitted = glyphs;
5614 if (lines)
5615 bounds->Width += margin_x * 2.0;
5617 SelectObject(hdc, oldfont);
5618 DeleteObject(gdifont);
5620 if (temp_hdc)
5621 DeleteDC(temp_hdc);
5623 return Ok;
5626 struct draw_string_args {
5627 GpGraphics *graphics;
5628 GDIPCONST GpBrush *brush;
5629 REAL x, y, rel_width, rel_height, ascent;
5632 static GpStatus draw_string_callback(HDC hdc,
5633 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5634 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5635 INT lineno, const RectF *bounds, INT *underlined_indexes,
5636 INT underlined_index_count, void *user_data)
5638 struct draw_string_args *args = user_data;
5639 PointF position;
5640 GpStatus stat;
5642 position.X = args->x + bounds->X / args->rel_width;
5643 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5645 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5646 args->brush, &position,
5647 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5649 if (stat == Ok && underlined_index_count)
5651 OUTLINETEXTMETRICW otm;
5652 REAL underline_y, underline_height;
5653 int i;
5655 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5657 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5658 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5660 for (i=0; i<underlined_index_count; i++)
5662 REAL start_x, end_x;
5663 SIZE text_size;
5664 INT ofs = underlined_indexes[i] - index;
5666 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5667 start_x = text_size.cx / args->rel_width;
5669 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5670 end_x = text_size.cx / args->rel_width;
5672 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5676 return stat;
5679 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5680 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5681 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5683 HRGN rgn = NULL;
5684 HFONT gdifont;
5685 GpPointF pt[3], rectcpy[4];
5686 POINT corners[4];
5687 REAL rel_width, rel_height, margin_x;
5688 INT save_state, format_flags = 0;
5689 REAL offsety = 0.0;
5690 struct draw_string_args args;
5691 RectF scaled_rect;
5692 HDC hdc, temp_hdc=NULL;
5693 TEXTMETRICW textmetric;
5695 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5696 length, font, debugstr_rectf(rect), format, brush);
5698 if(!graphics || !string || !font || !brush || !rect)
5699 return InvalidParameter;
5701 if(graphics->hdc)
5703 hdc = graphics->hdc;
5705 else
5707 hdc = temp_hdc = CreateCompatibleDC(0);
5710 if(format){
5711 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5713 format_flags = format->attr;
5715 /* Should be no need to explicitly test for StringAlignmentNear as
5716 * that is default behavior if no alignment is passed. */
5717 if(format->line_align != StringAlignmentNear){
5718 RectF bounds, in_rect = *rect;
5719 in_rect.Height = 0.0; /* avoid height clipping */
5720 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5722 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5724 if(format->line_align == StringAlignmentCenter)
5725 offsety = (rect->Height - bounds.Height) / 2;
5726 else if(format->line_align == StringAlignmentFar)
5727 offsety = (rect->Height - bounds.Height);
5729 TRACE("line align %d, offsety %f\n", format->line_align, offsety);
5732 save_state = SaveDC(hdc);
5734 pt[0].X = 0.0;
5735 pt[0].Y = 0.0;
5736 pt[1].X = 1.0;
5737 pt[1].Y = 0.0;
5738 pt[2].X = 0.0;
5739 pt[2].Y = 1.0;
5740 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
5741 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5742 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5743 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5744 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5746 rectcpy[3].X = rectcpy[0].X = rect->X;
5747 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5748 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5749 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5750 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, rectcpy, 4);
5751 round_points(corners, rectcpy, 4);
5753 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5754 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres, graphics->printer_display);
5756 scaled_rect.X = margin_x * rel_width;
5757 scaled_rect.Y = 0.0;
5758 scaled_rect.Width = rel_width * rect->Width;
5759 scaled_rect.Height = rel_height * rect->Height;
5760 if (scaled_rect.Width >= 0.5)
5762 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5763 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5766 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5767 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5769 if (!(format_flags & StringFormatFlagsNoClip) &&
5770 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5771 rect->Width > 0.0 && rect->Height > 0.0)
5773 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5774 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5775 SelectClipRgn(hdc, rgn);
5778 get_font_hfont(graphics, font, format, &gdifont, NULL, NULL);
5779 SelectObject(hdc, gdifont);
5781 args.graphics = graphics;
5782 args.brush = brush;
5784 args.x = rect->X;
5785 args.y = rect->Y + offsety;
5787 args.rel_width = rel_width;
5788 args.rel_height = rel_height;
5790 gdi_transform_acquire(graphics);
5792 GetTextMetricsW(hdc, &textmetric);
5793 args.ascent = textmetric.tmAscent / rel_height;
5795 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5796 draw_string_callback, &args);
5798 gdi_transform_release(graphics);
5800 DeleteObject(rgn);
5801 DeleteObject(gdifont);
5803 RestoreDC(hdc, save_state);
5805 DeleteDC(temp_hdc);
5807 return Ok;
5810 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5812 TRACE("(%p)\n", graphics);
5814 if(!graphics)
5815 return InvalidParameter;
5817 if(graphics->busy)
5818 return ObjectBusy;
5820 return GdipSetInfinite(graphics->clip);
5823 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5825 GpStatus stat;
5827 TRACE("(%p)\n", graphics);
5829 if(!graphics)
5830 return InvalidParameter;
5832 if(graphics->busy)
5833 return ObjectBusy;
5835 if (is_metafile_graphics(graphics))
5837 stat = METAFILE_ResetWorldTransform((GpMetafile*)graphics->image);
5839 if (stat != Ok)
5840 return stat;
5843 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5846 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5847 GpMatrixOrder order)
5849 GpStatus stat;
5851 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5853 if(!graphics)
5854 return InvalidParameter;
5856 if(graphics->busy)
5857 return ObjectBusy;
5859 if (is_metafile_graphics(graphics))
5861 stat = METAFILE_RotateWorldTransform((GpMetafile*)graphics->image, angle, order);
5863 if (stat != Ok)
5864 return stat;
5867 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5870 static GpStatus begin_container(GpGraphics *graphics,
5871 GraphicsContainerType type, GraphicsContainer *state)
5873 GraphicsContainerItem *container;
5874 GpStatus sts;
5876 if(!graphics || !state)
5877 return InvalidParameter;
5879 sts = init_container(&container, graphics, type);
5880 if(sts != Ok)
5881 return sts;
5883 list_add_head(&graphics->containers, &container->entry);
5884 *state = graphics->contid = container->contid;
5886 if (is_metafile_graphics(graphics)) {
5887 if (type == BEGIN_CONTAINER)
5888 METAFILE_BeginContainerNoParams((GpMetafile*)graphics->image, container->contid);
5889 else
5890 METAFILE_SaveGraphics((GpMetafile*)graphics->image, container->contid);
5893 return Ok;
5896 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5898 TRACE("(%p, %p)\n", graphics, state);
5899 return begin_container(graphics, SAVE_GRAPHICS, state);
5902 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5903 GraphicsContainer *state)
5905 TRACE("(%p, %p)\n", graphics, state);
5906 return begin_container(graphics, BEGIN_CONTAINER, state);
5909 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5911 GraphicsContainerItem *container;
5912 GpMatrix transform;
5913 GpStatus stat;
5914 GpRectF scaled_srcrect;
5915 REAL scale_x, scale_y;
5917 TRACE("(%p, %s, %s, %d, %p)\n", graphics, debugstr_rectf(dstrect), debugstr_rectf(srcrect), unit, state);
5919 if(!graphics || !dstrect || !srcrect || unit < UnitPixel || unit > UnitMillimeter || !state)
5920 return InvalidParameter;
5922 stat = init_container(&container, graphics, BEGIN_CONTAINER);
5923 if(stat != Ok)
5924 return stat;
5926 list_add_head(&graphics->containers, &container->entry);
5927 *state = graphics->contid = container->contid;
5929 scale_x = units_to_pixels(1.0, unit, graphics->xres, graphics->printer_display);
5930 scale_y = units_to_pixels(1.0, unit, graphics->yres, graphics->printer_display);
5932 scaled_srcrect.X = scale_x * srcrect->X;
5933 scaled_srcrect.Y = scale_y * srcrect->Y;
5934 scaled_srcrect.Width = scale_x * srcrect->Width;
5935 scaled_srcrect.Height = scale_y * srcrect->Height;
5937 transform.matrix[0] = dstrect->Width / scaled_srcrect.Width;
5938 transform.matrix[1] = 0.0;
5939 transform.matrix[2] = 0.0;
5940 transform.matrix[3] = dstrect->Height / scaled_srcrect.Height;
5941 transform.matrix[4] = dstrect->X - scaled_srcrect.X;
5942 transform.matrix[5] = dstrect->Y - scaled_srcrect.Y;
5944 GdipMultiplyMatrix(&graphics->worldtrans, &transform, MatrixOrderPrepend);
5946 if (is_metafile_graphics(graphics))
5947 METAFILE_BeginContainer((GpMetafile*)graphics->image, dstrect, srcrect, unit, container->contid);
5949 return Ok;
5952 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5954 GpRectF dstrectf, srcrectf;
5956 TRACE("(%p, %p, %p, %d, %p)\n", graphics, dstrect, srcrect, unit, state);
5958 if (!dstrect || !srcrect)
5959 return InvalidParameter;
5961 dstrectf.X = dstrect->X;
5962 dstrectf.Y = dstrect->Y;
5963 dstrectf.Width = dstrect->Width;
5964 dstrectf.Height = dstrect->Height;
5966 srcrectf.X = srcrect->X;
5967 srcrectf.Y = srcrect->Y;
5968 srcrectf.Width = srcrect->Width;
5969 srcrectf.Height = srcrect->Height;
5971 return GdipBeginContainer(graphics, &dstrectf, &srcrectf, unit, state);
5974 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5976 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5977 return NotImplemented;
5980 static GpStatus end_container(GpGraphics *graphics, GraphicsContainerType type,
5981 GraphicsContainer state)
5983 GpStatus sts;
5984 GraphicsContainerItem *container, *container2;
5986 if(!graphics)
5987 return InvalidParameter;
5989 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5990 if(container->contid == state && container->type == type)
5991 break;
5994 /* did not find a matching container */
5995 if(&container->entry == &graphics->containers)
5996 return Ok;
5998 sts = restore_container(graphics, container);
5999 if(sts != Ok)
6000 return sts;
6002 /* remove all of the containers on top of the found container */
6003 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
6004 if(container->contid == state)
6005 break;
6006 list_remove(&container->entry);
6007 delete_container(container);
6010 list_remove(&container->entry);
6011 delete_container(container);
6013 if (is_metafile_graphics(graphics)) {
6014 if (type == BEGIN_CONTAINER)
6015 METAFILE_EndContainer((GpMetafile*)graphics->image, state);
6016 else
6017 METAFILE_RestoreGraphics((GpMetafile*)graphics->image, state);
6020 return Ok;
6023 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
6025 TRACE("(%p, %x)\n", graphics, state);
6026 return end_container(graphics, BEGIN_CONTAINER, state);
6029 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
6031 TRACE("(%p, %x)\n", graphics, state);
6032 return end_container(graphics, SAVE_GRAPHICS, state);
6035 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
6036 REAL sy, GpMatrixOrder order)
6038 GpStatus stat;
6040 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
6042 if(!graphics)
6043 return InvalidParameter;
6045 if(graphics->busy)
6046 return ObjectBusy;
6048 if (is_metafile_graphics(graphics)) {
6049 stat = METAFILE_ScaleWorldTransform((GpMetafile*)graphics->image, sx, sy, order);
6051 if (stat != Ok)
6052 return stat;
6055 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
6058 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
6059 CombineMode mode)
6061 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
6063 if(!graphics || !srcgraphics)
6064 return InvalidParameter;
6066 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
6069 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
6070 CompositingMode mode)
6072 TRACE("(%p, %d)\n", graphics, mode);
6074 if(!graphics)
6075 return InvalidParameter;
6077 if(graphics->busy)
6078 return ObjectBusy;
6080 if(graphics->compmode == mode)
6081 return Ok;
6083 if (is_metafile_graphics(graphics))
6085 GpStatus stat;
6087 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6088 EmfPlusRecordTypeSetCompositingMode, mode);
6089 if(stat != Ok)
6090 return stat;
6093 graphics->compmode = mode;
6095 return Ok;
6098 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
6099 CompositingQuality quality)
6101 TRACE("(%p, %d)\n", graphics, quality);
6103 if(!graphics)
6104 return InvalidParameter;
6106 if(graphics->busy)
6107 return ObjectBusy;
6109 if(graphics->compqual == quality)
6110 return Ok;
6112 if (is_metafile_graphics(graphics))
6114 GpStatus stat;
6116 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6117 EmfPlusRecordTypeSetCompositingQuality, quality);
6118 if(stat != Ok)
6119 return stat;
6122 graphics->compqual = quality;
6124 return Ok;
6127 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
6128 InterpolationMode mode)
6130 TRACE("(%p, %d)\n", graphics, mode);
6132 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
6133 return InvalidParameter;
6135 if(graphics->busy)
6136 return ObjectBusy;
6138 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
6139 mode = InterpolationModeBilinear;
6141 if (mode == InterpolationModeHighQuality)
6142 mode = InterpolationModeHighQualityBicubic;
6144 if (mode == graphics->interpolation)
6145 return Ok;
6147 if (is_metafile_graphics(graphics))
6149 GpStatus stat;
6151 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6152 EmfPlusRecordTypeSetInterpolationMode, mode);
6153 if (stat != Ok)
6154 return stat;
6157 graphics->interpolation = mode;
6159 return Ok;
6162 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
6164 GpStatus stat;
6166 TRACE("(%p, %.2f)\n", graphics, scale);
6168 if(!graphics || (scale <= 0.0))
6169 return InvalidParameter;
6171 if(graphics->busy)
6172 return ObjectBusy;
6174 if (is_metafile_graphics(graphics))
6176 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
6177 if (stat != Ok)
6178 return stat;
6181 graphics->scale = scale;
6183 return Ok;
6186 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
6188 GpStatus stat;
6190 TRACE("(%p, %d)\n", graphics, unit);
6192 if(!graphics)
6193 return InvalidParameter;
6195 if(graphics->busy)
6196 return ObjectBusy;
6198 if(unit == UnitWorld)
6199 return InvalidParameter;
6201 if (is_metafile_graphics(graphics))
6203 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
6204 if (stat != Ok)
6205 return stat;
6208 graphics->unit = unit;
6210 return Ok;
6213 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
6214 mode)
6216 TRACE("(%p, %d)\n", graphics, mode);
6218 if(!graphics)
6219 return InvalidParameter;
6221 if(graphics->busy)
6222 return ObjectBusy;
6224 if(graphics->pixeloffset == mode)
6225 return Ok;
6227 if (is_metafile_graphics(graphics))
6229 GpStatus stat;
6231 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6232 EmfPlusRecordTypeSetPixelOffsetMode, mode);
6233 if(stat != Ok)
6234 return stat;
6237 graphics->pixeloffset = mode;
6239 return Ok;
6242 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
6244 TRACE("(%p,%i,%i)\n", graphics, x, y);
6246 if (!graphics)
6247 return InvalidParameter;
6249 graphics->origin_x = x;
6250 graphics->origin_y = y;
6252 return Ok;
6255 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
6257 TRACE("(%p,%p,%p)\n", graphics, x, y);
6259 if (!graphics || !x || !y)
6260 return InvalidParameter;
6262 *x = graphics->origin_x;
6263 *y = graphics->origin_y;
6265 return Ok;
6268 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
6270 TRACE("(%p, %d)\n", graphics, mode);
6272 if(!graphics)
6273 return InvalidParameter;
6275 if(graphics->busy)
6276 return ObjectBusy;
6278 if(graphics->smoothing == mode)
6279 return Ok;
6281 if (is_metafile_graphics(graphics))
6283 GpStatus stat;
6284 BOOL antialias = (mode != SmoothingModeDefault &&
6285 mode != SmoothingModeNone && mode != SmoothingModeHighSpeed);
6287 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6288 EmfPlusRecordTypeSetAntiAliasMode, (mode << 1) + antialias);
6289 if(stat != Ok)
6290 return stat;
6293 graphics->smoothing = mode;
6295 return Ok;
6298 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
6300 TRACE("(%p, %d)\n", graphics, contrast);
6302 if(!graphics)
6303 return InvalidParameter;
6305 graphics->textcontrast = contrast;
6307 return Ok;
6310 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
6311 TextRenderingHint hint)
6313 TRACE("(%p, %d)\n", graphics, hint);
6315 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
6316 return InvalidParameter;
6318 if(graphics->busy)
6319 return ObjectBusy;
6321 if(graphics->texthint == hint)
6322 return Ok;
6324 if (is_metafile_graphics(graphics)) {
6325 GpStatus stat;
6327 stat = METAFILE_AddSimpleProperty((GpMetafile*)graphics->image,
6328 EmfPlusRecordTypeSetTextRenderingHint, hint);
6329 if(stat != Ok)
6330 return stat;
6333 graphics->texthint = hint;
6335 return Ok;
6338 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
6340 GpStatus stat;
6342 TRACE("(%p, %p)\n", graphics, matrix);
6344 if(!graphics || !matrix)
6345 return InvalidParameter;
6347 if(graphics->busy)
6348 return ObjectBusy;
6350 TRACE("%f,%f,%f,%f,%f,%f\n",
6351 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
6352 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
6354 if (is_metafile_graphics(graphics)) {
6355 stat = METAFILE_SetWorldTransform((GpMetafile*)graphics->image, matrix);
6357 if (stat != Ok)
6358 return stat;
6361 graphics->worldtrans = *matrix;
6363 return Ok;
6366 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
6367 REAL dy, GpMatrixOrder order)
6369 GpStatus stat;
6371 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
6373 if(!graphics)
6374 return InvalidParameter;
6376 if(graphics->busy)
6377 return ObjectBusy;
6379 if (is_metafile_graphics(graphics)) {
6380 stat = METAFILE_TranslateWorldTransform((GpMetafile*)graphics->image, dx, dy, order);
6382 if (stat != Ok)
6383 return stat;
6386 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
6389 /*****************************************************************************
6390 * GdipSetClipHrgn [GDIPLUS.@]
6392 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
6394 GpRegion *region;
6395 GpStatus status;
6396 GpMatrix transform;
6398 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
6400 if(!graphics)
6401 return InvalidParameter;
6403 if(graphics->busy)
6404 return ObjectBusy;
6406 /* hrgn is in gdi32 device units */
6407 status = GdipCreateRegionHrgn(hrgn, &region);
6409 if (status == Ok)
6411 status = get_graphics_transform(graphics, CoordinateSpaceDevice, WineCoordinateSpaceGdiDevice, &transform);
6413 if (status == Ok)
6414 status = GdipTransformRegion(region, &transform);
6416 if (status == Ok)
6417 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6419 GdipDeleteRegion(region);
6421 return status;
6424 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
6426 GpStatus status;
6427 GpPath *clip_path;
6429 TRACE("(%p, %p, %d)\n", graphics, path, mode);
6431 if(!graphics)
6432 return InvalidParameter;
6434 if(graphics->busy)
6435 return ObjectBusy;
6437 status = GdipClonePath(path, &clip_path);
6438 if (status == Ok)
6440 GpMatrix world_to_device;
6442 get_graphics_transform(graphics, CoordinateSpaceDevice,
6443 CoordinateSpaceWorld, &world_to_device);
6444 status = GdipTransformPath(clip_path, &world_to_device);
6445 if (status == Ok)
6446 GdipCombineRegionPath(graphics->clip, clip_path, mode);
6448 GdipDeletePath(clip_path);
6450 return status;
6453 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
6454 REAL width, REAL height,
6455 CombineMode mode)
6457 GpStatus status;
6458 GpRectF rect;
6459 GpRegion *region;
6461 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
6463 if(!graphics)
6464 return InvalidParameter;
6466 if(graphics->busy)
6467 return ObjectBusy;
6469 if (is_metafile_graphics(graphics))
6471 status = METAFILE_SetClipRect((GpMetafile*)graphics->image, x, y, width, height, mode);
6472 if (status != Ok)
6473 return status;
6476 set_rect(&rect, x, y, width, height);
6477 status = GdipCreateRegionRect(&rect, &region);
6478 if (status == Ok)
6480 GpMatrix world_to_device;
6481 BOOL identity;
6483 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6484 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6485 if (status == Ok && !identity)
6486 status = GdipTransformRegion(region, &world_to_device);
6487 if (status == Ok)
6488 status = GdipCombineRegionRegion(graphics->clip, region, mode);
6490 GdipDeleteRegion(region);
6492 return status;
6495 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
6496 INT width, INT height,
6497 CombineMode mode)
6499 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
6501 if(!graphics)
6502 return InvalidParameter;
6504 if(graphics->busy)
6505 return ObjectBusy;
6507 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
6510 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
6511 CombineMode mode)
6513 GpStatus status;
6514 GpRegion *clip;
6516 TRACE("(%p, %p, %d)\n", graphics, region, mode);
6518 if(!graphics || !region)
6519 return InvalidParameter;
6521 if(graphics->busy)
6522 return ObjectBusy;
6524 if (is_metafile_graphics(graphics))
6526 status = METAFILE_SetClipRegion((GpMetafile*)graphics->image, region, mode);
6527 if (status != Ok)
6528 return status;
6531 status = GdipCloneRegion(region, &clip);
6532 if (status == Ok)
6534 GpMatrix world_to_device;
6535 BOOL identity;
6537 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
6538 status = GdipIsMatrixIdentity(&world_to_device, &identity);
6539 if (status == Ok && !identity)
6540 status = GdipTransformRegion(clip, &world_to_device);
6541 if (status == Ok)
6542 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
6544 GdipDeleteRegion(clip);
6546 return status;
6549 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
6550 INT count)
6552 GpStatus status;
6553 GpPath* path;
6555 TRACE("(%p, %p, %d)\n", graphics, points, count);
6557 if(!graphics || !pen || count<=0)
6558 return InvalidParameter;
6560 if(graphics->busy)
6561 return ObjectBusy;
6563 status = GdipCreatePath(FillModeAlternate, &path);
6564 if (status != Ok) return status;
6566 status = GdipAddPathPolygon(path, points, count);
6567 if (status == Ok)
6568 status = GdipDrawPath(graphics, pen, path);
6570 GdipDeletePath(path);
6572 return status;
6575 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
6576 INT count)
6578 GpStatus ret;
6579 GpPointF *ptf;
6580 INT i;
6582 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
6584 if(count<=0) return InvalidParameter;
6585 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
6586 if (!ptf) return OutOfMemory;
6588 for(i = 0;i < count; i++){
6589 ptf[i].X = (REAL)points[i].X;
6590 ptf[i].Y = (REAL)points[i].Y;
6593 ret = GdipDrawPolygon(graphics,pen,ptf,count);
6594 heap_free(ptf);
6596 return ret;
6599 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
6601 TRACE("(%p, %p)\n", graphics, dpi);
6603 if(!graphics || !dpi)
6604 return InvalidParameter;
6606 if(graphics->busy)
6607 return ObjectBusy;
6609 *dpi = graphics->xres;
6610 return Ok;
6613 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
6615 TRACE("(%p, %p)\n", graphics, dpi);
6617 if(!graphics || !dpi)
6618 return InvalidParameter;
6620 if(graphics->busy)
6621 return ObjectBusy;
6623 *dpi = graphics->yres;
6624 return Ok;
6627 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
6628 GpMatrixOrder order)
6630 GpMatrix m;
6631 GpStatus ret;
6633 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
6635 if(!graphics || !matrix)
6636 return InvalidParameter;
6638 if(graphics->busy)
6639 return ObjectBusy;
6641 if (is_metafile_graphics(graphics))
6643 ret = METAFILE_MultiplyWorldTransform((GpMetafile*)graphics->image, matrix, order);
6645 if (ret != Ok)
6646 return ret;
6649 m = graphics->worldtrans;
6651 ret = GdipMultiplyMatrix(&m, matrix, order);
6652 if(ret == Ok)
6653 graphics->worldtrans = m;
6655 return ret;
6658 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
6659 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
6661 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6663 GpStatus stat=Ok;
6665 TRACE("(%p, %p)\n", graphics, hdc);
6667 if(!graphics || !hdc)
6668 return InvalidParameter;
6670 if(graphics->busy)
6671 return ObjectBusy;
6673 if (is_metafile_graphics(graphics))
6675 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6677 else if (!graphics->hdc ||
6678 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6680 /* Create a fake HDC and fill it with a constant color. */
6681 HDC temp_hdc;
6682 HBITMAP hbitmap;
6683 GpRectF bounds;
6684 BITMAPINFOHEADER bmih;
6685 int i;
6687 stat = get_graphics_bounds(graphics, &bounds);
6688 if (stat != Ok)
6689 return stat;
6691 graphics->temp_hbitmap_width = bounds.Width;
6692 graphics->temp_hbitmap_height = bounds.Height;
6694 bmih.biSize = sizeof(bmih);
6695 bmih.biWidth = graphics->temp_hbitmap_width;
6696 bmih.biHeight = -graphics->temp_hbitmap_height;
6697 bmih.biPlanes = 1;
6698 bmih.biBitCount = 32;
6699 bmih.biCompression = BI_RGB;
6700 bmih.biSizeImage = 0;
6701 bmih.biXPelsPerMeter = 0;
6702 bmih.biYPelsPerMeter = 0;
6703 bmih.biClrUsed = 0;
6704 bmih.biClrImportant = 0;
6706 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6707 (void**)&graphics->temp_bits, NULL, 0);
6708 if (!hbitmap)
6709 return GenericError;
6711 if (!graphics->temp_hdc)
6713 temp_hdc = CreateCompatibleDC(0);
6715 else
6717 temp_hdc = graphics->temp_hdc;
6720 if (!temp_hdc)
6722 DeleteObject(hbitmap);
6723 return GenericError;
6726 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6727 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6729 SelectObject(temp_hdc, hbitmap);
6731 graphics->temp_hbitmap = hbitmap;
6732 *hdc = graphics->temp_hdc = temp_hdc;
6734 else
6736 *hdc = graphics->hdc;
6739 if (stat == Ok)
6740 graphics->busy = TRUE;
6742 return stat;
6745 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6747 GpStatus stat=Ok;
6749 TRACE("(%p, %p)\n", graphics, hdc);
6751 if(!graphics || !hdc || !graphics->busy)
6752 return InvalidParameter;
6754 if (is_metafile_graphics(graphics))
6756 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6758 else if (graphics->temp_hdc == hdc)
6760 DWORD* pos;
6761 int i;
6763 /* Find the pixels that have changed, and mark them as opaque. */
6764 pos = (DWORD*)graphics->temp_bits;
6765 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6767 if (*pos != DC_BACKGROUND_KEY)
6769 *pos |= 0xff000000;
6771 pos++;
6774 /* Write the changed pixels to the real target. */
6775 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6776 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6777 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
6779 /* Clean up. */
6780 DeleteObject(graphics->temp_hbitmap);
6781 graphics->temp_hbitmap = NULL;
6783 else if (hdc != graphics->hdc)
6785 stat = InvalidParameter;
6788 if (stat == Ok)
6789 graphics->busy = FALSE;
6791 return stat;
6794 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6796 GpRegion *clip;
6797 GpStatus status;
6798 GpMatrix device_to_world;
6800 TRACE("(%p, %p)\n", graphics, region);
6802 if(!graphics || !region)
6803 return InvalidParameter;
6805 if(graphics->busy)
6806 return ObjectBusy;
6808 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6809 return status;
6811 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
6812 status = GdipTransformRegion(clip, &device_to_world);
6813 if (status != Ok)
6815 GdipDeleteRegion(clip);
6816 return status;
6819 /* free everything except root node and header */
6820 delete_element(&region->node);
6821 memcpy(region, clip, sizeof(GpRegion));
6822 heap_free(clip);
6824 return Ok;
6827 GpStatus gdi_transform_acquire(GpGraphics *graphics)
6829 if (graphics->gdi_transform_acquire_count == 0 && graphics->hdc)
6831 graphics->gdi_transform_save = SaveDC(graphics->hdc);
6832 ModifyWorldTransform(graphics->hdc, NULL, MWT_IDENTITY);
6833 SetGraphicsMode(graphics->hdc, GM_COMPATIBLE);
6834 SetMapMode(graphics->hdc, MM_TEXT);
6835 SetWindowOrgEx(graphics->hdc, 0, 0, NULL);
6836 SetViewportOrgEx(graphics->hdc, 0, 0, NULL);
6838 graphics->gdi_transform_acquire_count++;
6839 return Ok;
6842 GpStatus gdi_transform_release(GpGraphics *graphics)
6844 if (graphics->gdi_transform_acquire_count <= 0)
6846 ERR("called without matching gdi_transform_acquire\n");
6847 return GenericError;
6849 if (graphics->gdi_transform_acquire_count == 1 && graphics->hdc)
6851 RestoreDC(graphics->hdc, graphics->gdi_transform_save);
6853 graphics->gdi_transform_acquire_count--;
6854 return Ok;
6857 GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6858 GpCoordinateSpace src_space, GpMatrix *matrix)
6860 GpStatus stat = Ok;
6861 REAL scale_x, scale_y;
6863 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6865 if (dst_space != src_space)
6867 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres, graphics->printer_display);
6868 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres, graphics->printer_display);
6870 if(graphics->unit != UnitDisplay)
6872 scale_x *= graphics->scale;
6873 scale_y *= graphics->scale;
6876 if (dst_space < src_space)
6878 /* transform towards world space */
6879 switch ((int)src_space)
6881 case WineCoordinateSpaceGdiDevice:
6883 GpMatrix gdixform;
6884 gdixform = graphics->gdi_transform;
6885 stat = GdipInvertMatrix(&gdixform);
6886 if (stat != Ok)
6887 break;
6888 GdipMultiplyMatrix(matrix, &gdixform, MatrixOrderAppend);
6889 if (dst_space == CoordinateSpaceDevice)
6890 break;
6891 /* else fall-through */
6893 case CoordinateSpaceDevice:
6894 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6895 if (dst_space == CoordinateSpacePage)
6896 break;
6897 /* else fall-through */
6898 case CoordinateSpacePage:
6900 GpMatrix inverted_transform = graphics->worldtrans;
6901 stat = GdipInvertMatrix(&inverted_transform);
6902 if (stat == Ok)
6903 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6904 break;
6908 else
6910 /* transform towards device space */
6911 switch ((int)src_space)
6913 case CoordinateSpaceWorld:
6914 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6915 if (dst_space == CoordinateSpacePage)
6916 break;
6917 /* else fall-through */
6918 case CoordinateSpacePage:
6919 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6920 if (dst_space == CoordinateSpaceDevice)
6921 break;
6922 /* else fall-through */
6923 case CoordinateSpaceDevice:
6925 GdipMultiplyMatrix(matrix, &graphics->gdi_transform, MatrixOrderAppend);
6926 break;
6931 return stat;
6934 GpStatus gdip_transform_points(GpGraphics *graphics, GpCoordinateSpace dst_space,
6935 GpCoordinateSpace src_space, GpPointF *points, INT count)
6937 GpMatrix matrix;
6938 GpStatus stat;
6940 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6941 if (stat != Ok) return stat;
6943 return GdipTransformMatrixPoints(&matrix, points, count);
6946 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6947 GpCoordinateSpace src_space, GpPointF *points, INT count)
6949 if(!graphics || !points || count <= 0 ||
6950 dst_space < 0 || dst_space > CoordinateSpaceDevice ||
6951 src_space < 0 || src_space > CoordinateSpaceDevice)
6952 return InvalidParameter;
6954 if(graphics->busy)
6955 return ObjectBusy;
6957 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6959 if (src_space == dst_space) return Ok;
6961 return gdip_transform_points(graphics, dst_space, src_space, points, count);
6964 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6965 GpCoordinateSpace src_space, GpPoint *points, INT count)
6967 GpPointF *pointsF;
6968 GpStatus ret;
6969 INT i;
6971 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6973 if(count <= 0)
6974 return InvalidParameter;
6976 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
6977 if(!pointsF)
6978 return OutOfMemory;
6980 for(i = 0; i < count; i++){
6981 pointsF[i].X = (REAL)points[i].X;
6982 pointsF[i].Y = (REAL)points[i].Y;
6985 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6987 if(ret == Ok)
6988 for(i = 0; i < count; i++){
6989 points[i].X = gdip_round(pointsF[i].X);
6990 points[i].Y = gdip_round(pointsF[i].Y);
6992 heap_free(pointsF);
6994 return ret;
6997 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6999 static int calls;
7001 TRACE("\n");
7003 if (!calls++)
7004 FIXME("stub\n");
7006 return NULL;
7009 /*****************************************************************************
7010 * GdipTranslateClip [GDIPLUS.@]
7012 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
7014 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
7016 if(!graphics)
7017 return InvalidParameter;
7019 if(graphics->busy)
7020 return ObjectBusy;
7022 return GdipTranslateRegion(graphics->clip, dx, dy);
7025 /*****************************************************************************
7026 * GdipTranslateClipI [GDIPLUS.@]
7028 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
7030 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
7032 if(!graphics)
7033 return InvalidParameter;
7035 if(graphics->busy)
7036 return ObjectBusy;
7038 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
7042 /*****************************************************************************
7043 * GdipMeasureDriverString [GDIPLUS.@]
7045 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7046 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
7047 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
7049 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7050 HFONT hfont;
7051 HDC hdc;
7052 REAL min_x, min_y, max_x, max_y, x, y;
7053 int i;
7054 TEXTMETRICW textmetric;
7055 const WORD *glyph_indices;
7056 WORD *dynamic_glyph_indices=NULL;
7057 REAL rel_width, rel_height, ascent, descent;
7058 GpPointF pt[3];
7060 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
7062 if (!graphics || !text || !font || !positions || !boundingBox)
7063 return InvalidParameter;
7065 if (length == -1)
7066 length = lstrlenW(text);
7068 if (length == 0)
7069 set_rect(boundingBox, 0.0f, 0.0f, 0.0f, 0.0f);
7071 if (flags & unsupported_flags)
7072 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7074 get_font_hfont(graphics, font, NULL, &hfont, NULL, matrix);
7076 hdc = CreateCompatibleDC(0);
7077 SelectObject(hdc, hfont);
7079 GetTextMetricsW(hdc, &textmetric);
7081 pt[0].X = 0.0;
7082 pt[0].Y = 0.0;
7083 pt[1].X = 1.0;
7084 pt[1].Y = 0.0;
7085 pt[2].X = 0.0;
7086 pt[2].Y = 1.0;
7087 if (matrix)
7089 GpMatrix xform = *matrix;
7090 GdipTransformMatrixPoints(&xform, pt, 3);
7092 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, pt, 3);
7093 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
7094 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
7095 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
7096 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
7098 if (flags & DriverStringOptionsCmapLookup)
7100 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
7101 if (!glyph_indices)
7103 DeleteDC(hdc);
7104 DeleteObject(hfont);
7105 return OutOfMemory;
7108 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
7110 else
7111 glyph_indices = text;
7113 min_x = max_x = x = positions[0].X;
7114 min_y = max_y = y = positions[0].Y;
7116 ascent = textmetric.tmAscent / rel_height;
7117 descent = textmetric.tmDescent / rel_height;
7119 for (i=0; i<length; i++)
7121 int char_width;
7122 ABC abc;
7124 if (!(flags & DriverStringOptionsRealizedAdvance))
7126 x = positions[i].X;
7127 y = positions[i].Y;
7130 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
7131 char_width = abc.abcA + abc.abcB + abc.abcC;
7133 if (min_y > y - ascent) min_y = y - ascent;
7134 if (max_y < y + descent) max_y = y + descent;
7135 if (min_x > x) min_x = x;
7137 x += char_width / rel_width;
7139 if (max_x < x) max_x = x;
7142 heap_free(dynamic_glyph_indices);
7143 DeleteDC(hdc);
7144 DeleteObject(hfont);
7146 boundingBox->X = min_x;
7147 boundingBox->Y = min_y;
7148 boundingBox->Width = max_x - min_x;
7149 boundingBox->Height = max_y - min_y;
7151 return Ok;
7154 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7155 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7156 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7157 INT flags, GDIPCONST GpMatrix *matrix)
7159 INT save_state;
7160 GpPointF pt, *real_positions=NULL;
7161 INT *eto_positions=NULL;
7162 HFONT hfont;
7163 LOGFONTW lfw;
7164 UINT eto_flags=0;
7165 GpStatus status;
7166 HRGN hrgn;
7168 if (!(flags & DriverStringOptionsCmapLookup))
7169 eto_flags |= ETO_GLYPH_INDEX;
7171 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7173 real_positions = heap_alloc(sizeof(*real_positions) * length);
7174 eto_positions = heap_alloc(sizeof(*eto_positions) * 2 * (length - 1));
7175 if (!real_positions || !eto_positions)
7177 heap_free(real_positions);
7178 heap_free(eto_positions);
7179 return OutOfMemory;
7183 save_state = SaveDC(graphics->hdc);
7184 SetBkMode(graphics->hdc, TRANSPARENT);
7185 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
7187 status = get_clip_hrgn(graphics, &hrgn);
7189 if (status == Ok)
7191 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
7192 DeleteObject(hrgn);
7195 pt = positions[0];
7196 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &pt, 1);
7198 get_font_hfont(graphics, font, format, &hfont, &lfw, matrix);
7200 if (!(flags & DriverStringOptionsRealizedAdvance) && length > 1)
7202 GpMatrix rotation;
7203 INT i;
7205 eto_flags |= ETO_PDY;
7207 memcpy(real_positions, positions, sizeof(PointF) * length);
7209 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7211 GdipSetMatrixElements(&rotation, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
7212 GdipRotateMatrix(&rotation, lfw.lfEscapement / 10.0, MatrixOrderAppend);
7213 GdipTransformMatrixPoints(&rotation, real_positions, length);
7215 for (i = 0; i < (length - 1); i++)
7217 eto_positions[i*2] = gdip_round(real_positions[i+1].X) - gdip_round(real_positions[i].X);
7218 eto_positions[i*2+1] = gdip_round(real_positions[i].Y) - gdip_round(real_positions[i+1].Y);
7222 SelectObject(graphics->hdc, hfont);
7224 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
7226 gdi_transform_acquire(graphics);
7228 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, eto_positions);
7230 gdi_transform_release(graphics);
7232 RestoreDC(graphics->hdc, save_state);
7234 DeleteObject(hfont);
7236 heap_free(real_positions);
7237 heap_free(eto_positions);
7239 return Ok;
7242 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7243 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7244 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7245 INT flags, GDIPCONST GpMatrix *matrix)
7247 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
7248 GpStatus stat;
7249 PointF *real_positions, real_position;
7250 POINT *pti;
7251 HFONT hfont;
7252 HDC hdc;
7253 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
7254 DWORD max_glyphsize=0;
7255 GLYPHMETRICS glyphmetrics;
7256 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
7257 BYTE *glyph_mask;
7258 BYTE *text_mask;
7259 int text_mask_stride;
7260 BYTE *pixel_data;
7261 int pixel_data_stride;
7262 GpRect pixel_area;
7263 UINT ggo_flags = GGO_GRAY8_BITMAP;
7265 if (length <= 0)
7266 return Ok;
7268 if (!(flags & DriverStringOptionsCmapLookup))
7269 ggo_flags |= GGO_GLYPH_INDEX;
7271 if (flags & unsupported_flags)
7272 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
7274 pti = heap_alloc_zero(sizeof(POINT) * length);
7275 if (!pti)
7276 return OutOfMemory;
7278 if (flags & DriverStringOptionsRealizedAdvance)
7280 real_position = positions[0];
7282 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, &real_position, 1);
7283 round_points(pti, &real_position, 1);
7285 else
7287 real_positions = heap_alloc_zero(sizeof(PointF) * length);
7288 if (!real_positions)
7290 heap_free(pti);
7291 return OutOfMemory;
7294 memcpy(real_positions, positions, sizeof(PointF) * length);
7296 gdip_transform_points(graphics, WineCoordinateSpaceGdiDevice, CoordinateSpaceWorld, real_positions, length);
7297 round_points(pti, real_positions, length);
7299 heap_free(real_positions);
7302 get_font_hfont(graphics, font, format, &hfont, NULL, matrix);
7304 hdc = CreateCompatibleDC(0);
7305 SelectObject(hdc, hfont);
7307 /* Get the boundaries of the text to be drawn */
7308 for (i=0; i<length; i++)
7310 DWORD glyphsize;
7311 int left, top, right, bottom;
7313 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7314 &glyphmetrics, 0, NULL, &identity);
7316 if (glyphsize == GDI_ERROR)
7318 ERR("GetGlyphOutlineW failed\n");
7319 heap_free(pti);
7320 DeleteDC(hdc);
7321 DeleteObject(hfont);
7322 return GenericError;
7325 if (glyphsize > max_glyphsize)
7326 max_glyphsize = glyphsize;
7328 if (glyphsize != 0)
7330 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7331 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7332 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
7333 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
7335 if (left < min_x) min_x = left;
7336 if (top < min_y) min_y = top;
7337 if (right > max_x) max_x = right;
7338 if (bottom > max_y) max_y = bottom;
7341 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
7343 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
7344 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
7348 if (max_glyphsize == 0)
7350 /* Nothing to draw. */
7351 heap_free(pti);
7352 DeleteDC(hdc);
7353 DeleteObject(hfont);
7354 return Ok;
7357 glyph_mask = heap_alloc_zero(max_glyphsize);
7358 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
7359 text_mask_stride = max_x - min_x;
7361 if (!(glyph_mask && text_mask))
7363 heap_free(glyph_mask);
7364 heap_free(text_mask);
7365 heap_free(pti);
7366 DeleteDC(hdc);
7367 DeleteObject(hfont);
7368 return OutOfMemory;
7371 /* Generate a mask for the text */
7372 for (i=0; i<length; i++)
7374 DWORD ret;
7375 int left, top, stride;
7377 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
7378 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
7380 if (ret == GDI_ERROR || ret == 0)
7381 continue; /* empty glyph */
7383 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
7384 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
7385 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
7387 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
7389 BYTE *glyph_val = glyph_mask + y * stride;
7390 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
7391 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
7393 *text_val = min(64, *text_val + *glyph_val);
7394 glyph_val++;
7395 text_val++;
7400 heap_free(pti);
7401 DeleteDC(hdc);
7402 DeleteObject(hfont);
7403 heap_free(glyph_mask);
7405 /* get the brush data */
7406 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
7407 if (!pixel_data)
7409 heap_free(text_mask);
7410 return OutOfMemory;
7413 pixel_area.X = min_x;
7414 pixel_area.Y = min_y;
7415 pixel_area.Width = max_x - min_x;
7416 pixel_area.Height = max_y - min_y;
7417 pixel_data_stride = pixel_area.Width * 4;
7419 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
7420 if (stat != Ok)
7422 heap_free(text_mask);
7423 heap_free(pixel_data);
7424 return stat;
7427 /* multiply the brush data by the mask */
7428 for (y=0; y<pixel_area.Height; y++)
7430 BYTE *text_val = text_mask + text_mask_stride * y;
7431 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
7432 for (x=0; x<pixel_area.Width; x++)
7434 *pixel_val = (*pixel_val) * (*text_val) / 64;
7435 text_val++;
7436 pixel_val+=4;
7440 heap_free(text_mask);
7442 gdi_transform_acquire(graphics);
7444 /* draw the result */
7445 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
7446 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
7448 gdi_transform_release(graphics);
7450 heap_free(pixel_data);
7452 return stat;
7455 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7456 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
7457 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
7458 INT flags, GDIPCONST GpMatrix *matrix)
7460 GpStatus stat = NotImplemented;
7462 if (length == -1)
7463 length = lstrlenW(text);
7465 if (is_metafile_graphics(graphics))
7466 return METAFILE_DrawDriverString((GpMetafile*)graphics->image, text, length, font,
7467 format, brush, positions, flags, matrix);
7469 if (graphics->hdc && !graphics->alpha_hdc &&
7470 brush->bt == BrushTypeSolidColor &&
7471 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
7472 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
7473 brush, positions, flags, matrix);
7474 if (stat == NotImplemented)
7475 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
7476 brush, positions, flags, matrix);
7477 return stat;
7480 /*****************************************************************************
7481 * GdipDrawDriverString [GDIPLUS.@]
7483 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
7484 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
7485 GDIPCONST PointF *positions, INT flags,
7486 GDIPCONST GpMatrix *matrix )
7488 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
7490 if (!graphics || !text || !font || !brush || !positions)
7491 return InvalidParameter;
7493 return draw_driver_string(graphics, text, length, font, NULL,
7494 brush, positions, flags, matrix);
7497 /*****************************************************************************
7498 * GdipIsVisibleClipEmpty [GDIPLUS.@]
7500 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
7502 GpStatus stat;
7503 GpRegion* rgn;
7505 TRACE("(%p, %p)\n", graphics, res);
7507 if((stat = GdipCreateRegion(&rgn)) != Ok)
7508 return stat;
7510 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
7511 goto cleanup;
7513 stat = GdipIsEmptyRegion(rgn, graphics, res);
7515 cleanup:
7516 GdipDeleteRegion(rgn);
7517 return stat;
7520 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
7522 static int calls;
7524 TRACE("(%p) stub\n", graphics);
7526 if(!(calls++))
7527 FIXME("not implemented\n");
7529 return NotImplemented;
7532 GpStatus WINGDIPAPI GdipGraphicsSetAbort(GpGraphics *graphics, GdiplusAbort *pabort)
7534 TRACE("(%p, %p)\n", graphics, pabort);
7536 if (!graphics)
7537 return InvalidParameter;
7539 if (pabort)
7540 FIXME("Abort callback is not supported.\n");
7542 return Ok;