gdiplus: Reimplement GdipDrawArc using GdipDrawPath.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blobb408db4678db1a265397f4aa902bb8d9914910a5
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 /* Converts angle (in degrees) to x/y coordinates */
55 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
57 REAL radAngle, hypotenuse;
59 radAngle = deg2rad(angle);
60 hypotenuse = 50.0; /* arbitrary */
62 *x = x_0 + cos(radAngle) * hypotenuse;
63 *y = y_0 + sin(radAngle) * hypotenuse;
66 /* Converts from gdiplus path point type to gdi path point type. */
67 static BYTE convert_path_point_type(BYTE type)
69 BYTE ret;
71 switch(type & PathPointTypePathTypeMask){
72 case PathPointTypeBezier:
73 ret = PT_BEZIERTO;
74 break;
75 case PathPointTypeLine:
76 ret = PT_LINETO;
77 break;
78 case PathPointTypeStart:
79 ret = PT_MOVETO;
80 break;
81 default:
82 ERR("Bad point type\n");
83 return 0;
86 if(type & PathPointTypeCloseSubpath)
87 ret |= PT_CLOSEFIGURE;
89 return ret;
92 static COLORREF get_gdi_brush_color(const GpBrush *brush)
94 ARGB argb;
96 switch (brush->bt)
98 case BrushTypeSolidColor:
100 const GpSolidFill *sf = (const GpSolidFill *)brush;
101 argb = sf->color;
102 break;
104 case BrushTypeHatchFill:
106 const GpHatch *hatch = (const GpHatch *)brush;
107 argb = hatch->forecol;
108 break;
110 case BrushTypeLinearGradient:
112 const GpLineGradient *line = (const GpLineGradient *)brush;
113 argb = line->startcolor;
114 break;
116 case BrushTypePathGradient:
118 const GpPathGradient *grad = (const GpPathGradient *)brush;
119 argb = grad->centercolor;
120 break;
122 default:
123 FIXME("unhandled brush type %d\n", brush->bt);
124 argb = 0;
125 break;
127 return ARGB2COLORREF(argb);
130 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
132 HBITMAP hbmp;
133 BITMAPINFOHEADER bmih;
134 DWORD *bits;
135 int x, y;
137 bmih.biSize = sizeof(bmih);
138 bmih.biWidth = 8;
139 bmih.biHeight = 8;
140 bmih.biPlanes = 1;
141 bmih.biBitCount = 32;
142 bmih.biCompression = BI_RGB;
143 bmih.biSizeImage = 0;
145 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
146 if (hbmp)
148 const char *hatch_data;
150 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
152 for (y = 0; y < 8; y++)
154 for (x = 0; x < 8; x++)
156 if (hatch_data[y] & (0x80 >> x))
157 bits[y * 8 + x] = hatch->forecol;
158 else
159 bits[y * 8 + x] = hatch->backcol;
163 else
165 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
167 for (y = 0; y < 64; y++)
168 bits[y] = hatch->forecol;
172 return hbmp;
175 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
177 switch (brush->bt)
179 case BrushTypeSolidColor:
181 const GpSolidFill *sf = (const GpSolidFill *)brush;
182 lb->lbStyle = BS_SOLID;
183 lb->lbColor = ARGB2COLORREF(sf->color);
184 lb->lbHatch = 0;
185 return Ok;
188 case BrushTypeHatchFill:
190 const GpHatch *hatch = (const GpHatch *)brush;
191 HBITMAP hbmp;
193 hbmp = create_hatch_bitmap(hatch);
194 if (!hbmp) return OutOfMemory;
196 lb->lbStyle = BS_PATTERN;
197 lb->lbColor = 0;
198 lb->lbHatch = (ULONG_PTR)hbmp;
199 return Ok;
202 default:
203 FIXME("unhandled brush type %d\n", brush->bt);
204 lb->lbStyle = BS_SOLID;
205 lb->lbColor = get_gdi_brush_color(brush);
206 lb->lbHatch = 0;
207 return Ok;
211 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
213 switch (lb->lbStyle)
215 case BS_PATTERN:
216 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
217 break;
219 return Ok;
222 static HBRUSH create_gdi_brush(const GpBrush *brush)
224 LOGBRUSH lb;
225 HBRUSH gdibrush;
227 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
229 gdibrush = CreateBrushIndirect(&lb);
230 free_gdi_logbrush(&lb);
232 return gdibrush;
235 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
237 LOGBRUSH lb;
238 HPEN gdipen;
239 REAL width;
240 INT save_state, i, numdashes;
241 GpPointF pt[2];
242 DWORD dash_array[MAX_DASHLEN];
244 save_state = SaveDC(graphics->hdc);
246 EndPath(graphics->hdc);
248 if(pen->unit == UnitPixel){
249 width = pen->width;
251 else{
252 /* Get an estimate for the amount the pen width is affected by the world
253 * transform. (This is similar to what some of the wine drivers do.) */
254 pt[0].X = 0.0;
255 pt[0].Y = 0.0;
256 pt[1].X = 1.0;
257 pt[1].Y = 1.0;
258 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
259 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
260 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
262 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
265 if(pen->dash == DashStyleCustom){
266 numdashes = min(pen->numdashes, MAX_DASHLEN);
268 TRACE("dashes are: ");
269 for(i = 0; i < numdashes; i++){
270 dash_array[i] = gdip_round(width * pen->dashes[i]);
271 TRACE("%d, ", dash_array[i]);
273 TRACE("\n and the pen style is %x\n", pen->style);
275 create_gdi_logbrush(pen->brush, &lb);
276 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
277 numdashes, dash_array);
278 free_gdi_logbrush(&lb);
280 else
282 create_gdi_logbrush(pen->brush, &lb);
283 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
284 free_gdi_logbrush(&lb);
287 SelectObject(graphics->hdc, gdipen);
289 return save_state;
292 static void restore_dc(GpGraphics *graphics, INT state)
294 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
295 RestoreDC(graphics->hdc, state);
298 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
299 GpCoordinateSpace src_space, GpMatrix *matrix);
301 /* This helper applies all the changes that the points listed in ptf need in
302 * order to be drawn on the device context. In the end, this should include at
303 * least:
304 * -scaling by page unit
305 * -applying world transformation
306 * -converting from float to int
307 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
308 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
309 * gdi to draw, and these functions would irreparably mess with line widths.
311 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
312 GpPointF *ptf, INT count)
314 REAL scale_x, scale_y;
315 GpMatrix matrix;
316 int i;
318 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
319 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
321 /* apply page scale */
322 if(graphics->unit != UnitDisplay)
324 scale_x *= graphics->scale;
325 scale_y *= graphics->scale;
328 matrix = graphics->worldtrans;
329 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
330 GdipTransformMatrixPoints(&matrix, ptf, count);
332 for(i = 0; i < count; i++){
333 pti[i].x = gdip_round(ptf[i].X);
334 pti[i].y = gdip_round(ptf[i].Y);
338 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
339 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
341 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
343 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
345 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
346 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
348 else
350 BLENDFUNCTION bf;
352 bf.BlendOp = AC_SRC_OVER;
353 bf.BlendFlags = 0;
354 bf.SourceConstantAlpha = 255;
355 bf.AlphaFormat = AC_SRC_ALPHA;
357 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
358 hdc, src_x, src_y, src_width, src_height, bf);
362 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
364 return GdipGetRegionHRgn(graphics->clip, graphics, hrgn);
367 /* Draw non-premultiplied ARGB data to the given graphics object */
368 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
369 const BYTE *src, INT src_width, INT src_height, INT src_stride)
371 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
372 INT x, y;
374 for (x=0; x<src_width; x++)
376 for (y=0; y<src_height; y++)
378 ARGB dst_color, src_color;
379 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
380 src_color = ((ARGB*)(src + src_stride * y))[x];
381 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
385 return Ok;
388 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
389 const BYTE *src, INT src_width, INT src_height, INT src_stride)
391 HDC hdc;
392 HBITMAP hbitmap;
393 BITMAPINFOHEADER bih;
394 BYTE *temp_bits;
396 hdc = CreateCompatibleDC(0);
398 bih.biSize = sizeof(BITMAPINFOHEADER);
399 bih.biWidth = src_width;
400 bih.biHeight = -src_height;
401 bih.biPlanes = 1;
402 bih.biBitCount = 32;
403 bih.biCompression = BI_RGB;
404 bih.biSizeImage = 0;
405 bih.biXPelsPerMeter = 0;
406 bih.biYPelsPerMeter = 0;
407 bih.biClrUsed = 0;
408 bih.biClrImportant = 0;
410 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
411 (void**)&temp_bits, NULL, 0);
413 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
414 4 * src_width, src, src_stride);
416 SelectObject(hdc, hbitmap);
417 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
418 hdc, 0, 0, src_width, src_height);
419 DeleteDC(hdc);
420 DeleteObject(hbitmap);
422 return Ok;
425 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
426 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
428 GpStatus stat=Ok;
430 if (graphics->image && graphics->image->type == ImageTypeBitmap)
432 DWORD i;
433 int size;
434 RGNDATA *rgndata;
435 RECT *rects;
436 HRGN hrgn, visible_rgn;
438 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
439 if (!hrgn)
440 return OutOfMemory;
442 stat = get_clip_hrgn(graphics, &visible_rgn);
443 if (stat != Ok)
445 DeleteObject(hrgn);
446 return stat;
449 if (visible_rgn)
451 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
452 DeleteObject(visible_rgn);
455 if (hregion)
456 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
458 size = GetRegionData(hrgn, 0, NULL);
460 rgndata = GdipAlloc(size);
461 if (!rgndata)
463 DeleteObject(hrgn);
464 return OutOfMemory;
467 GetRegionData(hrgn, size, rgndata);
469 rects = (RECT*)rgndata->Buffer;
471 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
473 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
474 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
475 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
476 src_stride);
479 GdipFree(rgndata);
481 DeleteObject(hrgn);
483 return stat;
485 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
487 ERR("This should not be used for metafiles; fix caller\n");
488 return NotImplemented;
490 else
492 HRGN hrgn;
493 int save;
495 stat = get_clip_hrgn(graphics, &hrgn);
497 if (stat != Ok)
498 return stat;
500 save = SaveDC(graphics->hdc);
502 if (hrgn)
503 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
505 if (hregion)
506 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
508 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
509 src_height, src_stride);
511 RestoreDC(graphics->hdc, save);
513 DeleteObject(hrgn);
515 return stat;
519 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
520 const BYTE *src, INT src_width, INT src_height, INT src_stride)
522 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL);
525 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
527 ARGB result=0;
528 ARGB i;
529 INT a1, a2, a3;
531 a1 = (start >> 24) & 0xff;
532 a2 = (end >> 24) & 0xff;
534 a3 = (int)(a1*(1.0f - position)+a2*(position));
536 result |= a3 << 24;
538 for (i=0xff; i<=0xff0000; i = i << 8)
539 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
540 return result;
543 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
545 REAL blendfac;
547 /* clamp to between 0.0 and 1.0, using the wrap mode */
548 if (brush->wrap == WrapModeTile)
550 position = fmodf(position, 1.0f);
551 if (position < 0.0f) position += 1.0f;
553 else /* WrapModeFlip* */
555 position = fmodf(position, 2.0f);
556 if (position < 0.0f) position += 2.0f;
557 if (position > 1.0f) position = 2.0f - position;
560 if (brush->blendcount == 1)
561 blendfac = position;
562 else
564 int i=1;
565 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
566 REAL range;
568 /* locate the blend positions surrounding this position */
569 while (position > brush->blendpos[i])
570 i++;
572 /* interpolate between the blend positions */
573 left_blendpos = brush->blendpos[i-1];
574 left_blendfac = brush->blendfac[i-1];
575 right_blendpos = brush->blendpos[i];
576 right_blendfac = brush->blendfac[i];
577 range = right_blendpos - left_blendpos;
578 blendfac = (left_blendfac * (right_blendpos - position) +
579 right_blendfac * (position - left_blendpos)) / range;
582 if (brush->pblendcount == 0)
583 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
584 else
586 int i=1;
587 ARGB left_blendcolor, right_blendcolor;
588 REAL left_blendpos, right_blendpos;
590 /* locate the blend colors surrounding this position */
591 while (blendfac > brush->pblendpos[i])
592 i++;
594 /* interpolate between the blend colors */
595 left_blendpos = brush->pblendpos[i-1];
596 left_blendcolor = brush->pblendcolor[i-1];
597 right_blendpos = brush->pblendpos[i];
598 right_blendcolor = brush->pblendcolor[i];
599 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
600 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
604 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
606 REAL val[5], res[4];
607 int i, j;
608 unsigned char a, r, g, b;
610 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
611 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
612 val[2] = (color & 0xff) / 255.0; /* blue */
613 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
614 val[4] = 1.0; /* translation */
616 for (i=0; i<4; i++)
618 res[i] = 0.0;
620 for (j=0; j<5; j++)
621 res[i] += matrix->m[j][i] * val[j];
624 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
625 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
626 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
627 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
629 return (a << 24) | (r << 16) | (g << 8) | b;
632 static int color_is_gray(ARGB color)
634 unsigned char r, g, b;
636 r = (color >> 16) & 0xff;
637 g = (color >> 8) & 0xff;
638 b = color & 0xff;
640 return (r == g) && (g == b);
643 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
644 UINT width, UINT height, INT stride, ColorAdjustType type)
646 UINT x, y;
647 INT i;
649 if (attributes->colorkeys[type].enabled ||
650 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
652 const struct color_key *key;
653 BYTE min_blue, min_green, min_red;
654 BYTE max_blue, max_green, max_red;
656 if (attributes->colorkeys[type].enabled)
657 key = &attributes->colorkeys[type];
658 else
659 key = &attributes->colorkeys[ColorAdjustTypeDefault];
661 min_blue = key->low&0xff;
662 min_green = (key->low>>8)&0xff;
663 min_red = (key->low>>16)&0xff;
665 max_blue = key->high&0xff;
666 max_green = (key->high>>8)&0xff;
667 max_red = (key->high>>16)&0xff;
669 for (x=0; x<width; x++)
670 for (y=0; y<height; y++)
672 ARGB *src_color;
673 BYTE blue, green, red;
674 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
675 blue = *src_color&0xff;
676 green = (*src_color>>8)&0xff;
677 red = (*src_color>>16)&0xff;
678 if (blue >= min_blue && green >= min_green && red >= min_red &&
679 blue <= max_blue && green <= max_green && red <= max_red)
680 *src_color = 0x00000000;
684 if (attributes->colorremaptables[type].enabled ||
685 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
687 const struct color_remap_table *table;
689 if (attributes->colorremaptables[type].enabled)
690 table = &attributes->colorremaptables[type];
691 else
692 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
694 for (x=0; x<width; x++)
695 for (y=0; y<height; y++)
697 ARGB *src_color;
698 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
699 for (i=0; i<table->mapsize; i++)
701 if (*src_color == table->colormap[i].oldColor.Argb)
703 *src_color = table->colormap[i].newColor.Argb;
704 break;
710 if (attributes->colormatrices[type].enabled ||
711 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
713 const struct color_matrix *colormatrices;
715 if (attributes->colormatrices[type].enabled)
716 colormatrices = &attributes->colormatrices[type];
717 else
718 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
720 for (x=0; x<width; x++)
721 for (y=0; y<height; y++)
723 ARGB *src_color;
724 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
726 if (colormatrices->flags == ColorMatrixFlagsDefault ||
727 !color_is_gray(*src_color))
729 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
731 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
733 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
738 if (attributes->gamma_enabled[type] ||
739 attributes->gamma_enabled[ColorAdjustTypeDefault])
741 REAL gamma;
743 if (attributes->gamma_enabled[type])
744 gamma = attributes->gamma[type];
745 else
746 gamma = attributes->gamma[ColorAdjustTypeDefault];
748 for (x=0; x<width; x++)
749 for (y=0; y<height; y++)
751 ARGB *src_color;
752 BYTE blue, green, red;
753 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
755 blue = *src_color&0xff;
756 green = (*src_color>>8)&0xff;
757 red = (*src_color>>16)&0xff;
759 /* FIXME: We should probably use a table for this. */
760 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
761 green = floorf(powf(green / 255.0, gamma) * 255.0);
762 red = floorf(powf(red / 255.0, gamma) * 255.0);
764 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
769 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
770 * bitmap that contains all the pixels we may need to draw it. */
771 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
772 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
773 GpRect *rect)
775 INT left, top, right, bottom;
777 switch (interpolation)
779 case InterpolationModeHighQualityBilinear:
780 case InterpolationModeHighQualityBicubic:
781 /* FIXME: Include a greater range for the prefilter? */
782 case InterpolationModeBicubic:
783 case InterpolationModeBilinear:
784 left = (INT)(floorf(srcx));
785 top = (INT)(floorf(srcy));
786 right = (INT)(ceilf(srcx+srcwidth));
787 bottom = (INT)(ceilf(srcy+srcheight));
788 break;
789 case InterpolationModeNearestNeighbor:
790 default:
791 left = gdip_round(srcx);
792 top = gdip_round(srcy);
793 right = gdip_round(srcx+srcwidth);
794 bottom = gdip_round(srcy+srcheight);
795 break;
798 if (wrap == WrapModeClamp)
800 if (left < 0)
801 left = 0;
802 if (top < 0)
803 top = 0;
804 if (right >= bitmap->width)
805 right = bitmap->width-1;
806 if (bottom >= bitmap->height)
807 bottom = bitmap->height-1;
809 else
811 /* In some cases we can make the rectangle smaller here, but the logic
812 * is hard to get right, and tiling suggests we're likely to use the
813 * entire source image. */
814 if (left < 0 || right >= bitmap->width)
816 left = 0;
817 right = bitmap->width-1;
820 if (top < 0 || bottom >= bitmap->height)
822 top = 0;
823 bottom = bitmap->height-1;
827 rect->X = left;
828 rect->Y = top;
829 rect->Width = right - left + 1;
830 rect->Height = bottom - top + 1;
833 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
834 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
836 if (attributes->wrap == WrapModeClamp)
838 if (x < 0 || y < 0 || x >= width || y >= height)
839 return attributes->outside_color;
841 else
843 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
844 if (x < 0)
845 x = width*2 + x % (width * 2);
846 if (y < 0)
847 y = height*2 + y % (height * 2);
849 if ((attributes->wrap & 1) == 1)
851 /* Flip X */
852 if ((x / width) % 2 == 0)
853 x = x % width;
854 else
855 x = width - 1 - x % width;
857 else
858 x = x % width;
860 if ((attributes->wrap & 2) == 2)
862 /* Flip Y */
863 if ((y / height) % 2 == 0)
864 y = y % height;
865 else
866 y = height - 1 - y % height;
868 else
869 y = y % height;
872 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
874 ERR("out of range pixel requested\n");
875 return 0xffcd0084;
878 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
881 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
882 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
883 InterpolationMode interpolation, PixelOffsetMode offset_mode)
885 static int fixme;
887 switch (interpolation)
889 default:
890 if (!fixme++)
891 FIXME("Unimplemented interpolation %i\n", interpolation);
892 /* fall-through */
893 case InterpolationModeBilinear:
895 REAL leftxf, topyf;
896 INT leftx, rightx, topy, bottomy;
897 ARGB topleft, topright, bottomleft, bottomright;
898 ARGB top, bottom;
899 float x_offset;
901 leftxf = floorf(point->X);
902 leftx = (INT)leftxf;
903 rightx = (INT)ceilf(point->X);
904 topyf = floorf(point->Y);
905 topy = (INT)topyf;
906 bottomy = (INT)ceilf(point->Y);
908 if (leftx == rightx && topy == bottomy)
909 return sample_bitmap_pixel(src_rect, bits, width, height,
910 leftx, topy, attributes);
912 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
913 leftx, topy, attributes);
914 topright = sample_bitmap_pixel(src_rect, bits, width, height,
915 rightx, topy, attributes);
916 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
917 leftx, bottomy, attributes);
918 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
919 rightx, bottomy, attributes);
921 x_offset = point->X - leftxf;
922 top = blend_colors(topleft, topright, x_offset);
923 bottom = blend_colors(bottomleft, bottomright, x_offset);
925 return blend_colors(top, bottom, point->Y - topyf);
927 case InterpolationModeNearestNeighbor:
929 FLOAT pixel_offset;
930 switch (offset_mode)
932 default:
933 case PixelOffsetModeNone:
934 case PixelOffsetModeHighSpeed:
935 pixel_offset = 0.5;
936 break;
938 case PixelOffsetModeHalf:
939 case PixelOffsetModeHighQuality:
940 pixel_offset = 0.0;
941 break;
943 return sample_bitmap_pixel(src_rect, bits, width, height,
944 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
950 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
952 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
955 static INT brush_can_fill_path(GpBrush *brush)
957 switch (brush->bt)
959 case BrushTypeSolidColor:
960 return 1;
961 case BrushTypeHatchFill:
963 GpHatch *hatch = (GpHatch*)brush;
964 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
965 ((hatch->backcol & 0xff000000) == 0xff000000);
967 case BrushTypeLinearGradient:
968 case BrushTypeTextureFill:
969 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
970 default:
971 return 0;
975 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
977 switch (brush->bt)
979 case BrushTypeSolidColor:
981 GpSolidFill *fill = (GpSolidFill*)brush;
982 HBITMAP bmp = ARGB2BMP(fill->color);
984 if (bmp)
986 RECT rc;
987 /* partially transparent fill */
989 SelectClipPath(graphics->hdc, RGN_AND);
990 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
992 HDC hdc = CreateCompatibleDC(NULL);
994 if (!hdc) break;
996 SelectObject(hdc, bmp);
997 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
998 hdc, 0, 0, 1, 1);
999 DeleteDC(hdc);
1002 DeleteObject(bmp);
1003 break;
1005 /* else fall through */
1007 default:
1009 HBRUSH gdibrush, old_brush;
1011 gdibrush = create_gdi_brush(brush);
1012 if (!gdibrush) return;
1014 old_brush = SelectObject(graphics->hdc, gdibrush);
1015 FillPath(graphics->hdc);
1016 SelectObject(graphics->hdc, old_brush);
1017 DeleteObject(gdibrush);
1018 break;
1023 static INT brush_can_fill_pixels(GpBrush *brush)
1025 switch (brush->bt)
1027 case BrushTypeSolidColor:
1028 case BrushTypeHatchFill:
1029 case BrushTypeLinearGradient:
1030 case BrushTypeTextureFill:
1031 case BrushTypePathGradient:
1032 return 1;
1033 default:
1034 return 0;
1038 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1039 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1041 switch (brush->bt)
1043 case BrushTypeSolidColor:
1045 int x, y;
1046 GpSolidFill *fill = (GpSolidFill*)brush;
1047 for (x=0; x<fill_area->Width; x++)
1048 for (y=0; y<fill_area->Height; y++)
1049 argb_pixels[x + y*cdwStride] = fill->color;
1050 return Ok;
1052 case BrushTypeHatchFill:
1054 int x, y;
1055 GpHatch *fill = (GpHatch*)brush;
1056 const char *hatch_data;
1058 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1059 return NotImplemented;
1061 for (x=0; x<fill_area->Width; x++)
1062 for (y=0; y<fill_area->Height; y++)
1064 int hx, hy;
1066 /* FIXME: Account for the rendering origin */
1067 hx = (x + fill_area->X) % 8;
1068 hy = (y + fill_area->Y) % 8;
1070 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1071 argb_pixels[x + y*cdwStride] = fill->forecol;
1072 else
1073 argb_pixels[x + y*cdwStride] = fill->backcol;
1076 return Ok;
1078 case BrushTypeLinearGradient:
1080 GpLineGradient *fill = (GpLineGradient*)brush;
1081 GpPointF draw_points[3], line_points[3];
1082 GpStatus stat;
1083 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1084 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1085 int x, y;
1087 draw_points[0].X = fill_area->X;
1088 draw_points[0].Y = fill_area->Y;
1089 draw_points[1].X = fill_area->X+1;
1090 draw_points[1].Y = fill_area->Y;
1091 draw_points[2].X = fill_area->X;
1092 draw_points[2].Y = fill_area->Y+1;
1094 /* Transform the points to a co-ordinate space where X is the point's
1095 * position in the gradient, 0.0 being the start point and 1.0 the
1096 * end point. */
1097 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1098 CoordinateSpaceDevice, draw_points, 3);
1100 if (stat == Ok)
1102 line_points[0] = fill->startpoint;
1103 line_points[1] = fill->endpoint;
1104 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1105 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1107 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1110 if (stat == Ok)
1112 stat = GdipInvertMatrix(world_to_gradient);
1114 if (stat == Ok)
1115 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1117 GdipDeleteMatrix(world_to_gradient);
1120 if (stat == Ok)
1122 REAL x_delta = draw_points[1].X - draw_points[0].X;
1123 REAL y_delta = draw_points[2].X - draw_points[0].X;
1125 for (y=0; y<fill_area->Height; y++)
1127 for (x=0; x<fill_area->Width; x++)
1129 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1131 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1136 return stat;
1138 case BrushTypeTextureFill:
1140 GpTexture *fill = (GpTexture*)brush;
1141 GpPointF draw_points[3];
1142 GpStatus stat;
1143 int x, y;
1144 GpBitmap *bitmap;
1145 int src_stride;
1146 GpRect src_area;
1148 if (fill->image->type != ImageTypeBitmap)
1150 FIXME("metafile texture brushes not implemented\n");
1151 return NotImplemented;
1154 bitmap = (GpBitmap*)fill->image;
1155 src_stride = sizeof(ARGB) * bitmap->width;
1157 src_area.X = src_area.Y = 0;
1158 src_area.Width = bitmap->width;
1159 src_area.Height = bitmap->height;
1161 draw_points[0].X = fill_area->X;
1162 draw_points[0].Y = fill_area->Y;
1163 draw_points[1].X = fill_area->X+1;
1164 draw_points[1].Y = fill_area->Y;
1165 draw_points[2].X = fill_area->X;
1166 draw_points[2].Y = fill_area->Y+1;
1168 /* Transform the points to the co-ordinate space of the bitmap. */
1169 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1170 CoordinateSpaceDevice, draw_points, 3);
1172 if (stat == Ok)
1174 GpMatrix world_to_texture = fill->transform;
1176 stat = GdipInvertMatrix(&world_to_texture);
1177 if (stat == Ok)
1178 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1181 if (stat == Ok && !fill->bitmap_bits)
1183 BitmapData lockeddata;
1185 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1186 if (!fill->bitmap_bits)
1187 stat = OutOfMemory;
1189 if (stat == Ok)
1191 lockeddata.Width = bitmap->width;
1192 lockeddata.Height = bitmap->height;
1193 lockeddata.Stride = src_stride;
1194 lockeddata.PixelFormat = PixelFormat32bppARGB;
1195 lockeddata.Scan0 = fill->bitmap_bits;
1197 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1198 PixelFormat32bppARGB, &lockeddata);
1201 if (stat == Ok)
1202 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1204 if (stat == Ok)
1205 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1206 bitmap->width, bitmap->height,
1207 src_stride, ColorAdjustTypeBitmap);
1209 if (stat != Ok)
1211 GdipFree(fill->bitmap_bits);
1212 fill->bitmap_bits = NULL;
1216 if (stat == Ok)
1218 REAL x_dx = draw_points[1].X - draw_points[0].X;
1219 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1220 REAL y_dx = draw_points[2].X - draw_points[0].X;
1221 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1223 for (y=0; y<fill_area->Height; y++)
1225 for (x=0; x<fill_area->Width; x++)
1227 GpPointF point;
1228 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1229 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1231 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1232 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1233 &point, fill->imageattributes, graphics->interpolation,
1234 graphics->pixeloffset);
1239 return stat;
1241 case BrushTypePathGradient:
1243 GpPathGradient *fill = (GpPathGradient*)brush;
1244 GpPath *flat_path;
1245 GpMatrix world_to_device;
1246 GpStatus stat;
1247 int i, figure_start=0;
1248 GpPointF start_point, end_point, center_point;
1249 BYTE type;
1250 REAL min_yf, max_yf, line1_xf, line2_xf;
1251 INT min_y, max_y, min_x, max_x;
1252 INT x, y;
1253 ARGB outer_color;
1254 static int transform_fixme_once;
1256 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1258 static int once;
1259 if (!once++)
1260 FIXME("path gradient focus not implemented\n");
1263 if (fill->gamma)
1265 static int once;
1266 if (!once++)
1267 FIXME("path gradient gamma correction not implemented\n");
1270 if (fill->blendcount)
1272 static int once;
1273 if (!once++)
1274 FIXME("path gradient blend not implemented\n");
1277 if (fill->pblendcount)
1279 static int once;
1280 if (!once++)
1281 FIXME("path gradient preset blend not implemented\n");
1284 if (!transform_fixme_once)
1286 BOOL is_identity=TRUE;
1287 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1288 if (!is_identity)
1290 FIXME("path gradient transform not implemented\n");
1291 transform_fixme_once = 1;
1295 stat = GdipClonePath(fill->path, &flat_path);
1297 if (stat != Ok)
1298 return stat;
1300 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1301 CoordinateSpaceWorld, &world_to_device);
1302 if (stat == Ok)
1304 stat = GdipTransformPath(flat_path, &world_to_device);
1306 if (stat == Ok)
1308 center_point = fill->center;
1309 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1312 if (stat == Ok)
1313 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1316 if (stat != Ok)
1318 GdipDeletePath(flat_path);
1319 return stat;
1322 for (i=0; i<flat_path->pathdata.Count; i++)
1324 int start_center_line=0, end_center_line=0;
1325 int seen_start=0, seen_end=0, seen_center=0;
1326 REAL center_distance;
1327 ARGB start_color, end_color;
1328 REAL dy, dx;
1330 type = flat_path->pathdata.Types[i];
1332 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1333 figure_start = i;
1335 start_point = flat_path->pathdata.Points[i];
1337 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1339 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1341 end_point = flat_path->pathdata.Points[figure_start];
1342 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1344 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1346 end_point = flat_path->pathdata.Points[i+1];
1347 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1349 else
1350 continue;
1352 outer_color = start_color;
1354 min_yf = center_point.Y;
1355 if (min_yf > start_point.Y) min_yf = start_point.Y;
1356 if (min_yf > end_point.Y) min_yf = end_point.Y;
1358 if (min_yf < fill_area->Y)
1359 min_y = fill_area->Y;
1360 else
1361 min_y = (INT)ceil(min_yf);
1363 max_yf = center_point.Y;
1364 if (max_yf < start_point.Y) max_yf = start_point.Y;
1365 if (max_yf < end_point.Y) max_yf = end_point.Y;
1367 if (max_yf > fill_area->Y + fill_area->Height)
1368 max_y = fill_area->Y + fill_area->Height;
1369 else
1370 max_y = (INT)ceil(max_yf);
1372 dy = end_point.Y - start_point.Y;
1373 dx = end_point.X - start_point.X;
1375 /* This is proportional to the distance from start-end line to center point. */
1376 center_distance = dy * (start_point.X - center_point.X) +
1377 dx * (center_point.Y - start_point.Y);
1379 for (y=min_y; y<max_y; y++)
1381 REAL yf = (REAL)y;
1383 if (!seen_start && yf >= start_point.Y)
1385 seen_start = 1;
1386 start_center_line ^= 1;
1388 if (!seen_end && yf >= end_point.Y)
1390 seen_end = 1;
1391 end_center_line ^= 1;
1393 if (!seen_center && yf >= center_point.Y)
1395 seen_center = 1;
1396 start_center_line ^= 1;
1397 end_center_line ^= 1;
1400 if (start_center_line)
1401 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1402 else
1403 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1405 if (end_center_line)
1406 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1407 else
1408 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1410 if (line1_xf < line2_xf)
1412 min_x = (INT)ceil(line1_xf);
1413 max_x = (INT)ceil(line2_xf);
1415 else
1417 min_x = (INT)ceil(line2_xf);
1418 max_x = (INT)ceil(line1_xf);
1421 if (min_x < fill_area->X)
1422 min_x = fill_area->X;
1423 if (max_x > fill_area->X + fill_area->Width)
1424 max_x = fill_area->X + fill_area->Width;
1426 for (x=min_x; x<max_x; x++)
1428 REAL xf = (REAL)x;
1429 REAL distance;
1431 if (start_color != end_color)
1433 REAL blend_amount, pdy, pdx;
1434 pdy = yf - center_point.Y;
1435 pdx = xf - center_point.X;
1436 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1437 outer_color = blend_colors(start_color, end_color, blend_amount);
1440 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1441 (end_point.X - start_point.X) * (yf - start_point.Y);
1443 distance = distance / center_distance;
1445 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1446 blend_colors(outer_color, fill->centercolor, distance);
1451 GdipDeletePath(flat_path);
1452 return stat;
1454 default:
1455 return NotImplemented;
1459 /* GdipDrawPie/GdipFillPie helper function */
1460 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1461 REAL height, REAL startAngle, REAL sweepAngle)
1463 GpPointF ptf[4];
1464 POINT pti[4];
1466 ptf[0].X = x;
1467 ptf[0].Y = y;
1468 ptf[1].X = x + width;
1469 ptf[1].Y = y + height;
1471 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1472 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1474 transform_and_round_points(graphics, pti, ptf, 4);
1476 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1477 pti[2].y, pti[3].x, pti[3].y);
1480 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1481 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1482 * should not be called on an hdc that has a path you care about. */
1483 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1484 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1486 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1487 GpMatrix matrix;
1488 HBRUSH brush = NULL;
1489 HPEN pen = NULL;
1490 PointF ptf[4], *custptf = NULL;
1491 POINT pt[4], *custpt = NULL;
1492 BYTE *tp = NULL;
1493 REAL theta, dsmall, dbig, dx, dy = 0.0;
1494 INT i, count;
1495 LOGBRUSH lb;
1496 BOOL customstroke;
1498 if((x1 == x2) && (y1 == y2))
1499 return;
1501 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1503 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1504 if(!customstroke){
1505 brush = CreateSolidBrush(color);
1506 lb.lbStyle = BS_SOLID;
1507 lb.lbColor = color;
1508 lb.lbHatch = 0;
1509 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1510 PS_JOIN_MITER, 1, &lb, 0,
1511 NULL);
1512 oldbrush = SelectObject(graphics->hdc, brush);
1513 oldpen = SelectObject(graphics->hdc, pen);
1516 switch(cap){
1517 case LineCapFlat:
1518 break;
1519 case LineCapSquare:
1520 case LineCapSquareAnchor:
1521 case LineCapDiamondAnchor:
1522 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1523 if(cap == LineCapDiamondAnchor){
1524 dsmall = cos(theta + M_PI_2) * size;
1525 dbig = sin(theta + M_PI_2) * size;
1527 else{
1528 dsmall = cos(theta + M_PI_4) * size;
1529 dbig = sin(theta + M_PI_4) * size;
1532 ptf[0].X = x2 - dsmall;
1533 ptf[1].X = x2 + dbig;
1535 ptf[0].Y = y2 - dbig;
1536 ptf[3].Y = y2 + dsmall;
1538 ptf[1].Y = y2 - dsmall;
1539 ptf[2].Y = y2 + dbig;
1541 ptf[3].X = x2 - dbig;
1542 ptf[2].X = x2 + dsmall;
1544 transform_and_round_points(graphics, pt, ptf, 4);
1545 Polygon(graphics->hdc, pt, 4);
1547 break;
1548 case LineCapArrowAnchor:
1549 size = size * 4.0 / sqrt(3.0);
1551 dx = cos(M_PI / 6.0 + theta) * size;
1552 dy = sin(M_PI / 6.0 + theta) * size;
1554 ptf[0].X = x2 - dx;
1555 ptf[0].Y = y2 - dy;
1557 dx = cos(- M_PI / 6.0 + theta) * size;
1558 dy = sin(- M_PI / 6.0 + theta) * size;
1560 ptf[1].X = x2 - dx;
1561 ptf[1].Y = y2 - dy;
1563 ptf[2].X = x2;
1564 ptf[2].Y = y2;
1566 transform_and_round_points(graphics, pt, ptf, 3);
1567 Polygon(graphics->hdc, pt, 3);
1569 break;
1570 case LineCapRoundAnchor:
1571 dx = dy = ANCHOR_WIDTH * size / 2.0;
1573 ptf[0].X = x2 - dx;
1574 ptf[0].Y = y2 - dy;
1575 ptf[1].X = x2 + dx;
1576 ptf[1].Y = y2 + dy;
1578 transform_and_round_points(graphics, pt, ptf, 2);
1579 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1581 break;
1582 case LineCapTriangle:
1583 size = size / 2.0;
1584 dx = cos(M_PI_2 + theta) * size;
1585 dy = sin(M_PI_2 + theta) * size;
1587 ptf[0].X = x2 - dx;
1588 ptf[0].Y = y2 - dy;
1589 ptf[1].X = x2 + dx;
1590 ptf[1].Y = y2 + dy;
1592 dx = cos(theta) * size;
1593 dy = sin(theta) * size;
1595 ptf[2].X = x2 + dx;
1596 ptf[2].Y = y2 + dy;
1598 transform_and_round_points(graphics, pt, ptf, 3);
1599 Polygon(graphics->hdc, pt, 3);
1601 break;
1602 case LineCapRound:
1603 dx = dy = size / 2.0;
1605 ptf[0].X = x2 - dx;
1606 ptf[0].Y = y2 - dy;
1607 ptf[1].X = x2 + dx;
1608 ptf[1].Y = y2 + dy;
1610 dx = -cos(M_PI_2 + theta) * size;
1611 dy = -sin(M_PI_2 + theta) * size;
1613 ptf[2].X = x2 - dx;
1614 ptf[2].Y = y2 - dy;
1615 ptf[3].X = x2 + dx;
1616 ptf[3].Y = y2 + dy;
1618 transform_and_round_points(graphics, pt, ptf, 4);
1619 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1620 pt[2].y, pt[3].x, pt[3].y);
1622 break;
1623 case LineCapCustom:
1624 if(!custom)
1625 break;
1627 count = custom->pathdata.Count;
1628 custptf = GdipAlloc(count * sizeof(PointF));
1629 custpt = GdipAlloc(count * sizeof(POINT));
1630 tp = GdipAlloc(count);
1632 if(!custptf || !custpt || !tp)
1633 goto custend;
1635 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1637 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1638 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1639 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1640 MatrixOrderAppend);
1641 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1642 GdipTransformMatrixPoints(&matrix, custptf, count);
1644 transform_and_round_points(graphics, custpt, custptf, count);
1646 for(i = 0; i < count; i++)
1647 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1649 if(custom->fill){
1650 BeginPath(graphics->hdc);
1651 PolyDraw(graphics->hdc, custpt, tp, count);
1652 EndPath(graphics->hdc);
1653 StrokeAndFillPath(graphics->hdc);
1655 else
1656 PolyDraw(graphics->hdc, custpt, tp, count);
1658 custend:
1659 GdipFree(custptf);
1660 GdipFree(custpt);
1661 GdipFree(tp);
1662 break;
1663 default:
1664 break;
1667 if(!customstroke){
1668 SelectObject(graphics->hdc, oldbrush);
1669 SelectObject(graphics->hdc, oldpen);
1670 DeleteObject(brush);
1671 DeleteObject(pen);
1675 /* Shortens the line by the given percent by changing x2, y2.
1676 * If percent is > 1.0 then the line will change direction.
1677 * If percent is negative it can lengthen the line. */
1678 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1680 REAL dist, theta, dx, dy;
1682 if((y1 == *y2) && (x1 == *x2))
1683 return;
1685 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1686 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1687 dx = cos(theta) * dist;
1688 dy = sin(theta) * dist;
1690 *x2 = *x2 + dx;
1691 *y2 = *y2 + dy;
1694 /* Shortens the line by the given amount by changing x2, y2.
1695 * If the amount is greater than the distance, the line will become length 0.
1696 * If the amount is negative, it can lengthen the line. */
1697 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1699 REAL dx, dy, percent;
1701 dx = *x2 - x1;
1702 dy = *y2 - y1;
1703 if(dx == 0 && dy == 0)
1704 return;
1706 percent = amt / sqrt(dx * dx + dy * dy);
1707 if(percent >= 1.0){
1708 *x2 = x1;
1709 *y2 = y1;
1710 return;
1713 shorten_line_percent(x1, y1, x2, y2, percent);
1716 /* Conducts a linear search to find the bezier points that will back off
1717 * the endpoint of the curve by a distance of amt. Linear search works
1718 * better than binary in this case because there are multiple solutions,
1719 * and binary searches often find a bad one. I don't think this is what
1720 * Windows does but short of rendering the bezier without GDI's help it's
1721 * the best we can do. If rev then work from the start of the passed points
1722 * instead of the end. */
1723 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1725 GpPointF origpt[4];
1726 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1727 INT i, first = 0, second = 1, third = 2, fourth = 3;
1729 if(rev){
1730 first = 3;
1731 second = 2;
1732 third = 1;
1733 fourth = 0;
1736 origx = pt[fourth].X;
1737 origy = pt[fourth].Y;
1738 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1740 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1741 /* reset bezier points to original values */
1742 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1743 /* Perform magic on bezier points. Order is important here.*/
1744 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1745 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1746 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1747 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1748 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1749 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1751 dx = pt[fourth].X - origx;
1752 dy = pt[fourth].Y - origy;
1754 diff = sqrt(dx * dx + dy * dy);
1755 percent += 0.0005 * amt;
1759 /* Draws bezier curves between given points, and if caps is true then draws an
1760 * endcap at the end of the last line. */
1761 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1762 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1764 POINT *pti;
1765 GpPointF *ptcopy;
1766 GpStatus status = GenericError;
1768 if(!count)
1769 return Ok;
1771 pti = GdipAlloc(count * sizeof(POINT));
1772 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1774 if(!pti || !ptcopy){
1775 status = OutOfMemory;
1776 goto end;
1779 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1781 if(caps){
1782 if(pen->endcap == LineCapArrowAnchor)
1783 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1784 else if((pen->endcap == LineCapCustom) && pen->customend)
1785 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1786 FALSE);
1788 if(pen->startcap == LineCapArrowAnchor)
1789 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1790 else if((pen->startcap == LineCapCustom) && pen->customstart)
1791 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1793 /* the direction of the line cap is parallel to the direction at the
1794 * end of the bezier (which, if it has been shortened, is not the same
1795 * as the direction from pt[count-2] to pt[count-1]) */
1796 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1797 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1798 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1799 pt[count - 1].X, pt[count - 1].Y);
1801 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1802 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1803 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1806 transform_and_round_points(graphics, pti, ptcopy, count);
1808 PolyBezier(graphics->hdc, pti, count);
1810 status = Ok;
1812 end:
1813 GdipFree(pti);
1814 GdipFree(ptcopy);
1816 return status;
1819 /* Draws a combination of bezier curves and lines between points. */
1820 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1821 GDIPCONST BYTE * types, INT count, BOOL caps)
1823 POINT *pti = GdipAlloc(count * sizeof(POINT));
1824 BYTE *tp = GdipAlloc(count);
1825 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1826 INT i, j;
1827 GpStatus status = GenericError;
1829 if(!count){
1830 status = Ok;
1831 goto end;
1833 if(!pti || !tp || !ptcopy){
1834 status = OutOfMemory;
1835 goto end;
1838 for(i = 1; i < count; i++){
1839 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1840 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1841 || !(types[i + 1] & PathPointTypeBezier)){
1842 ERR("Bad bezier points\n");
1843 goto end;
1845 i += 2;
1849 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1851 /* If we are drawing caps, go through the points and adjust them accordingly,
1852 * and draw the caps. */
1853 if(caps){
1854 switch(types[count - 1] & PathPointTypePathTypeMask){
1855 case PathPointTypeBezier:
1856 if(pen->endcap == LineCapArrowAnchor)
1857 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1858 else if((pen->endcap == LineCapCustom) && pen->customend)
1859 shorten_bezier_amt(&ptcopy[count - 4],
1860 pen->width * pen->customend->inset, FALSE);
1862 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1863 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1864 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1865 pt[count - 1].X, pt[count - 1].Y);
1867 break;
1868 case PathPointTypeLine:
1869 if(pen->endcap == LineCapArrowAnchor)
1870 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1871 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1872 pen->width);
1873 else if((pen->endcap == LineCapCustom) && pen->customend)
1874 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1875 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1876 pen->customend->inset * pen->width);
1878 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1879 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1880 pt[count - 1].Y);
1882 break;
1883 default:
1884 ERR("Bad path last point\n");
1885 goto end;
1888 /* Find start of points */
1889 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1890 == PathPointTypeStart); j++);
1892 switch(types[j] & PathPointTypePathTypeMask){
1893 case PathPointTypeBezier:
1894 if(pen->startcap == LineCapArrowAnchor)
1895 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1896 else if((pen->startcap == LineCapCustom) && pen->customstart)
1897 shorten_bezier_amt(&ptcopy[j - 1],
1898 pen->width * pen->customstart->inset, TRUE);
1900 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1901 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1902 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1903 pt[j - 1].X, pt[j - 1].Y);
1905 break;
1906 case PathPointTypeLine:
1907 if(pen->startcap == LineCapArrowAnchor)
1908 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1909 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1910 pen->width);
1911 else if((pen->startcap == LineCapCustom) && pen->customstart)
1912 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1913 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1914 pen->customstart->inset * pen->width);
1916 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1917 pt[j].X, pt[j].Y, pt[j - 1].X,
1918 pt[j - 1].Y);
1920 break;
1921 default:
1922 ERR("Bad path points\n");
1923 goto end;
1927 transform_and_round_points(graphics, pti, ptcopy, count);
1929 for(i = 0; i < count; i++){
1930 tp[i] = convert_path_point_type(types[i]);
1933 PolyDraw(graphics->hdc, pti, tp, count);
1935 status = Ok;
1937 end:
1938 GdipFree(pti);
1939 GdipFree(ptcopy);
1940 GdipFree(tp);
1942 return status;
1945 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1947 GpStatus result;
1949 BeginPath(graphics->hdc);
1950 result = draw_poly(graphics, NULL, path->pathdata.Points,
1951 path->pathdata.Types, path->pathdata.Count, FALSE);
1952 EndPath(graphics->hdc);
1953 return result;
1956 typedef struct _GraphicsContainerItem {
1957 struct list entry;
1958 GraphicsContainer contid;
1960 SmoothingMode smoothing;
1961 CompositingQuality compqual;
1962 InterpolationMode interpolation;
1963 CompositingMode compmode;
1964 TextRenderingHint texthint;
1965 REAL scale;
1966 GpUnit unit;
1967 PixelOffsetMode pixeloffset;
1968 UINT textcontrast;
1969 GpMatrix worldtrans;
1970 GpRegion* clip;
1971 INT origin_x, origin_y;
1972 } GraphicsContainerItem;
1974 static GpStatus init_container(GraphicsContainerItem** container,
1975 GDIPCONST GpGraphics* graphics){
1976 GpStatus sts;
1978 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1979 if(!(*container))
1980 return OutOfMemory;
1982 (*container)->contid = graphics->contid + 1;
1984 (*container)->smoothing = graphics->smoothing;
1985 (*container)->compqual = graphics->compqual;
1986 (*container)->interpolation = graphics->interpolation;
1987 (*container)->compmode = graphics->compmode;
1988 (*container)->texthint = graphics->texthint;
1989 (*container)->scale = graphics->scale;
1990 (*container)->unit = graphics->unit;
1991 (*container)->textcontrast = graphics->textcontrast;
1992 (*container)->pixeloffset = graphics->pixeloffset;
1993 (*container)->origin_x = graphics->origin_x;
1994 (*container)->origin_y = graphics->origin_y;
1995 (*container)->worldtrans = graphics->worldtrans;
1997 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1998 if(sts != Ok){
1999 GdipFree(*container);
2000 *container = NULL;
2001 return sts;
2004 return Ok;
2007 static void delete_container(GraphicsContainerItem* container)
2009 GdipDeleteRegion(container->clip);
2010 GdipFree(container);
2013 static GpStatus restore_container(GpGraphics* graphics,
2014 GDIPCONST GraphicsContainerItem* container){
2015 GpStatus sts;
2016 GpRegion *newClip;
2018 sts = GdipCloneRegion(container->clip, &newClip);
2019 if(sts != Ok) return sts;
2021 graphics->worldtrans = container->worldtrans;
2023 GdipDeleteRegion(graphics->clip);
2024 graphics->clip = newClip;
2026 graphics->contid = container->contid - 1;
2028 graphics->smoothing = container->smoothing;
2029 graphics->compqual = container->compqual;
2030 graphics->interpolation = container->interpolation;
2031 graphics->compmode = container->compmode;
2032 graphics->texthint = container->texthint;
2033 graphics->scale = container->scale;
2034 graphics->unit = container->unit;
2035 graphics->textcontrast = container->textcontrast;
2036 graphics->pixeloffset = container->pixeloffset;
2037 graphics->origin_x = container->origin_x;
2038 graphics->origin_y = container->origin_y;
2040 return Ok;
2043 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2045 RECT wnd_rect;
2046 GpStatus stat=Ok;
2047 GpUnit unit;
2049 if(graphics->hwnd) {
2050 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2051 return GenericError;
2053 rect->X = wnd_rect.left;
2054 rect->Y = wnd_rect.top;
2055 rect->Width = wnd_rect.right - wnd_rect.left;
2056 rect->Height = wnd_rect.bottom - wnd_rect.top;
2057 }else if (graphics->image){
2058 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2059 if (stat == Ok && unit != UnitPixel)
2060 FIXME("need to convert from unit %i\n", unit);
2061 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2062 HBITMAP hbmp;
2063 BITMAP bmp;
2065 rect->X = 0;
2066 rect->Y = 0;
2068 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2069 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2071 rect->Width = bmp.bmWidth;
2072 rect->Height = bmp.bmHeight;
2074 else
2076 /* FIXME: ??? */
2077 rect->Width = 1;
2078 rect->Height = 1;
2080 }else{
2081 rect->X = 0;
2082 rect->Y = 0;
2083 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2084 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2087 return stat;
2090 /* on success, rgn will contain the region of the graphics object which
2091 * is visible after clipping has been applied */
2092 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2094 GpStatus stat;
2095 GpRectF rectf;
2096 GpRegion* tmp;
2098 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2099 return stat;
2101 if((stat = GdipCreateRegion(&tmp)) != Ok)
2102 return stat;
2104 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2105 goto end;
2107 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2108 goto end;
2110 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2112 end:
2113 GdipDeleteRegion(tmp);
2114 return stat;
2117 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2119 REAL height;
2121 if (font->unit == UnitPixel)
2123 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2125 else
2127 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2128 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2129 else
2130 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2133 lf->lfHeight = -(height + 0.5);
2134 lf->lfWidth = 0;
2135 lf->lfEscapement = 0;
2136 lf->lfOrientation = 0;
2137 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2138 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2139 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2140 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2141 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2142 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2143 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2144 lf->lfQuality = DEFAULT_QUALITY;
2145 lf->lfPitchAndFamily = 0;
2146 strcpyW(lf->lfFaceName, font->family->FamilyName);
2149 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2150 GDIPCONST GpStringFormat *format, HFONT *hfont,
2151 GDIPCONST GpMatrix *matrix)
2153 HDC hdc = CreateCompatibleDC(0);
2154 GpPointF pt[3];
2155 REAL angle, rel_width, rel_height, font_height;
2156 LOGFONTW lfw;
2157 HFONT unscaled_font;
2158 TEXTMETRICW textmet;
2160 if (font->unit == UnitPixel)
2161 font_height = font->emSize;
2162 else
2164 REAL unit_scale, res;
2166 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2167 unit_scale = units_scale(font->unit, graphics->unit, res);
2169 font_height = font->emSize * unit_scale;
2172 pt[0].X = 0.0;
2173 pt[0].Y = 0.0;
2174 pt[1].X = 1.0;
2175 pt[1].Y = 0.0;
2176 pt[2].X = 0.0;
2177 pt[2].Y = 1.0;
2178 if (matrix)
2180 GpMatrix xform = *matrix;
2181 GdipTransformMatrixPoints(&xform, pt, 3);
2183 if (graphics)
2184 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2185 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2186 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2187 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2188 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2189 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2191 get_log_fontW(font, graphics, &lfw);
2192 lfw.lfHeight = gdip_round(font_height * rel_height);
2193 unscaled_font = CreateFontIndirectW(&lfw);
2195 SelectObject(hdc, unscaled_font);
2196 GetTextMetricsW(hdc, &textmet);
2198 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2199 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2201 *hfont = CreateFontIndirectW(&lfw);
2203 DeleteDC(hdc);
2204 DeleteObject(unscaled_font);
2207 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2209 TRACE("(%p, %p)\n", hdc, graphics);
2211 return GdipCreateFromHDC2(hdc, NULL, graphics);
2214 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2216 GpStatus retval;
2217 HBITMAP hbitmap;
2218 DIBSECTION dib;
2220 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2222 if(hDevice != NULL)
2223 FIXME("Don't know how to handle parameter hDevice\n");
2225 if(hdc == NULL)
2226 return OutOfMemory;
2228 if(graphics == NULL)
2229 return InvalidParameter;
2231 *graphics = GdipAlloc(sizeof(GpGraphics));
2232 if(!*graphics) return OutOfMemory;
2234 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2236 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2237 GdipFree(*graphics);
2238 return retval;
2241 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2242 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2243 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2245 (*graphics)->alpha_hdc = 1;
2248 (*graphics)->hdc = hdc;
2249 (*graphics)->hwnd = WindowFromDC(hdc);
2250 (*graphics)->owndc = FALSE;
2251 (*graphics)->smoothing = SmoothingModeDefault;
2252 (*graphics)->compqual = CompositingQualityDefault;
2253 (*graphics)->interpolation = InterpolationModeBilinear;
2254 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2255 (*graphics)->compmode = CompositingModeSourceOver;
2256 (*graphics)->unit = UnitDisplay;
2257 (*graphics)->scale = 1.0;
2258 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2259 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2260 (*graphics)->busy = FALSE;
2261 (*graphics)->textcontrast = 4;
2262 list_init(&(*graphics)->containers);
2263 (*graphics)->contid = 0;
2265 TRACE("<-- %p\n", *graphics);
2267 return Ok;
2270 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2272 GpStatus retval;
2274 *graphics = GdipAlloc(sizeof(GpGraphics));
2275 if(!*graphics) return OutOfMemory;
2277 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2279 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2280 GdipFree(*graphics);
2281 return retval;
2284 (*graphics)->hdc = NULL;
2285 (*graphics)->hwnd = NULL;
2286 (*graphics)->owndc = FALSE;
2287 (*graphics)->image = image;
2288 (*graphics)->smoothing = SmoothingModeDefault;
2289 (*graphics)->compqual = CompositingQualityDefault;
2290 (*graphics)->interpolation = InterpolationModeBilinear;
2291 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2292 (*graphics)->compmode = CompositingModeSourceOver;
2293 (*graphics)->unit = UnitDisplay;
2294 (*graphics)->scale = 1.0;
2295 (*graphics)->xres = image->xres;
2296 (*graphics)->yres = image->yres;
2297 (*graphics)->busy = FALSE;
2298 (*graphics)->textcontrast = 4;
2299 list_init(&(*graphics)->containers);
2300 (*graphics)->contid = 0;
2302 TRACE("<-- %p\n", *graphics);
2304 return Ok;
2307 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2309 GpStatus ret;
2310 HDC hdc;
2312 TRACE("(%p, %p)\n", hwnd, graphics);
2314 hdc = GetDC(hwnd);
2316 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2318 ReleaseDC(hwnd, hdc);
2319 return ret;
2322 (*graphics)->hwnd = hwnd;
2323 (*graphics)->owndc = TRUE;
2325 return Ok;
2328 /* FIXME: no icm handling */
2329 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2331 TRACE("(%p, %p)\n", hwnd, graphics);
2333 return GdipCreateFromHWND(hwnd, graphics);
2336 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2337 GpMetafile **metafile)
2339 ENHMETAHEADER header;
2340 MetafileType metafile_type;
2342 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2344 if(!hemf || !metafile)
2345 return InvalidParameter;
2347 if (GetEnhMetaFileHeader(hemf, sizeof(header), &header) == 0)
2348 return GenericError;
2350 metafile_type = METAFILE_GetEmfType(hemf);
2352 if (metafile_type == MetafileTypeInvalid)
2353 return GenericError;
2355 *metafile = GdipAlloc(sizeof(GpMetafile));
2356 if (!*metafile)
2357 return OutOfMemory;
2359 (*metafile)->image.type = ImageTypeMetafile;
2360 (*metafile)->image.format = ImageFormatEMF;
2361 (*metafile)->image.frame_count = 1;
2362 (*metafile)->image.xres = (REAL)header.szlDevice.cx;
2363 (*metafile)->image.yres = (REAL)header.szlDevice.cy;
2364 (*metafile)->bounds.X = (REAL)header.rclBounds.left;
2365 (*metafile)->bounds.Y = (REAL)header.rclBounds.top;
2366 (*metafile)->bounds.Width = (REAL)(header.rclBounds.right - header.rclBounds.left);
2367 (*metafile)->bounds.Height = (REAL)(header.rclBounds.bottom - header.rclBounds.top);
2368 (*metafile)->unit = UnitPixel;
2369 (*metafile)->metafile_type = metafile_type;
2370 (*metafile)->hemf = hemf;
2371 (*metafile)->preserve_hemf = !delete;
2373 TRACE("<-- %p\n", *metafile);
2375 return Ok;
2378 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2379 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2381 UINT read;
2382 BYTE *copy;
2383 HENHMETAFILE hemf;
2384 GpStatus retval = Ok;
2386 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2388 if(!hwmf || !metafile || !placeable)
2389 return InvalidParameter;
2391 *metafile = NULL;
2392 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2393 if(!read)
2394 return GenericError;
2395 copy = GdipAlloc(read);
2396 GetMetaFileBitsEx(hwmf, read, copy);
2398 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2399 GdipFree(copy);
2401 /* FIXME: We should store and use hwmf instead of converting to hemf */
2402 retval = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
2404 if (retval == Ok)
2406 (*metafile)->image.xres = (REAL)placeable->Inch;
2407 (*metafile)->image.yres = (REAL)placeable->Inch;
2408 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2409 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2410 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2411 placeable->BoundingBox.Left);
2412 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2413 placeable->BoundingBox.Top);
2414 (*metafile)->metafile_type = MetafileTypeWmfPlaceable;
2415 (*metafile)->image.format = ImageFormatWMF;
2417 if (delete) DeleteMetaFile(hwmf);
2419 else
2420 DeleteEnhMetaFile(hemf);
2421 return retval;
2424 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2425 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2427 HMETAFILE hmf = GetMetaFileW(file);
2429 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2431 if(!hmf) return InvalidParameter;
2433 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2436 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2437 GpMetafile **metafile)
2439 FIXME("(%p, %p): stub\n", file, metafile);
2440 return NotImplemented;
2443 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2444 GpMetafile **metafile)
2446 FIXME("(%p, %p): stub\n", stream, metafile);
2447 return NotImplemented;
2450 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2451 UINT access, IStream **stream)
2453 DWORD dwMode;
2454 HRESULT ret;
2456 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2458 if(!stream || !filename)
2459 return InvalidParameter;
2461 if(access & GENERIC_WRITE)
2462 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2463 else if(access & GENERIC_READ)
2464 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2465 else
2466 return InvalidParameter;
2468 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2470 return hresult_to_status(ret);
2473 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2475 GraphicsContainerItem *cont, *next;
2476 GpStatus stat;
2477 TRACE("(%p)\n", graphics);
2479 if(!graphics) return InvalidParameter;
2480 if(graphics->busy) return ObjectBusy;
2482 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2484 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2485 if (stat != Ok)
2486 return stat;
2489 if(graphics->owndc)
2490 ReleaseDC(graphics->hwnd, graphics->hdc);
2492 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2493 list_remove(&cont->entry);
2494 delete_container(cont);
2497 GdipDeleteRegion(graphics->clip);
2498 GdipFree(graphics);
2500 return Ok;
2503 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2504 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2506 GpStatus status;
2507 GpPath *path;
2509 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2510 width, height, startAngle, sweepAngle);
2512 if(!graphics || !pen || width <= 0 || height <= 0)
2513 return InvalidParameter;
2515 if(graphics->busy)
2516 return ObjectBusy;
2518 status = GdipCreatePath(FillModeAlternate, &path);
2519 if (status != Ok) return status;
2521 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2522 if (status == Ok)
2523 status = GdipDrawPath(graphics, pen, path);
2525 GdipDeletePath(path);
2526 return status;
2529 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2530 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2532 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2533 width, height, startAngle, sweepAngle);
2535 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2538 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2539 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2541 INT save_state;
2542 GpPointF pt[4];
2543 GpStatus retval;
2545 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2546 x2, y2, x3, y3, x4, y4);
2548 if(!graphics || !pen)
2549 return InvalidParameter;
2551 if(graphics->busy)
2552 return ObjectBusy;
2554 if (!graphics->hdc)
2556 FIXME("graphics object has no HDC\n");
2557 return Ok;
2560 pt[0].X = x1;
2561 pt[0].Y = y1;
2562 pt[1].X = x2;
2563 pt[1].Y = y2;
2564 pt[2].X = x3;
2565 pt[2].Y = y3;
2566 pt[3].X = x4;
2567 pt[3].Y = y4;
2569 save_state = prepare_dc(graphics, pen);
2571 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2573 restore_dc(graphics, save_state);
2575 return retval;
2578 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2579 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2581 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2582 x2, y2, x3, y3, x4, y4);
2584 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2587 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2588 GDIPCONST GpPointF *points, INT count)
2590 INT i;
2591 GpStatus ret;
2593 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2595 if(!graphics || !pen || !points || (count <= 0))
2596 return InvalidParameter;
2598 if(graphics->busy)
2599 return ObjectBusy;
2601 for(i = 0; i < floor(count / 4); i++){
2602 ret = GdipDrawBezier(graphics, pen,
2603 points[4*i].X, points[4*i].Y,
2604 points[4*i + 1].X, points[4*i + 1].Y,
2605 points[4*i + 2].X, points[4*i + 2].Y,
2606 points[4*i + 3].X, points[4*i + 3].Y);
2607 if(ret != Ok)
2608 return ret;
2611 return Ok;
2614 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2615 GDIPCONST GpPoint *points, INT count)
2617 GpPointF *pts;
2618 GpStatus ret;
2619 INT i;
2621 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2623 if(!graphics || !pen || !points || (count <= 0))
2624 return InvalidParameter;
2626 if(graphics->busy)
2627 return ObjectBusy;
2629 pts = GdipAlloc(sizeof(GpPointF) * count);
2630 if(!pts)
2631 return OutOfMemory;
2633 for(i = 0; i < count; i++){
2634 pts[i].X = (REAL)points[i].X;
2635 pts[i].Y = (REAL)points[i].Y;
2638 ret = GdipDrawBeziers(graphics,pen,pts,count);
2640 GdipFree(pts);
2642 return ret;
2645 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2646 GDIPCONST GpPointF *points, INT count)
2648 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2650 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2653 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2654 GDIPCONST GpPoint *points, INT count)
2656 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2658 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2661 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2662 GDIPCONST GpPointF *points, INT count, REAL tension)
2664 GpPath *path;
2665 GpStatus status;
2667 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2669 if(!graphics || !pen || !points || count <= 0)
2670 return InvalidParameter;
2672 if(graphics->busy)
2673 return ObjectBusy;
2675 status = GdipCreatePath(FillModeAlternate, &path);
2676 if (status != Ok) return status;
2678 status = GdipAddPathClosedCurve2(path, points, count, tension);
2679 if (status == Ok)
2680 status = GdipDrawPath(graphics, pen, path);
2682 GdipDeletePath(path);
2684 return status;
2687 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2688 GDIPCONST GpPoint *points, INT count, REAL tension)
2690 GpPointF *ptf;
2691 GpStatus stat;
2692 INT i;
2694 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2696 if(!points || count <= 0)
2697 return InvalidParameter;
2699 ptf = GdipAlloc(sizeof(GpPointF)*count);
2700 if(!ptf)
2701 return OutOfMemory;
2703 for(i = 0; i < count; i++){
2704 ptf[i].X = (REAL)points[i].X;
2705 ptf[i].Y = (REAL)points[i].Y;
2708 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2710 GdipFree(ptf);
2712 return stat;
2715 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2716 GDIPCONST GpPointF *points, INT count)
2718 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2720 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2723 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2724 GDIPCONST GpPoint *points, INT count)
2726 GpPointF *pointsF;
2727 GpStatus ret;
2728 INT i;
2730 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2732 if(!points)
2733 return InvalidParameter;
2735 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2736 if(!pointsF)
2737 return OutOfMemory;
2739 for(i = 0; i < count; i++){
2740 pointsF[i].X = (REAL)points[i].X;
2741 pointsF[i].Y = (REAL)points[i].Y;
2744 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2745 GdipFree(pointsF);
2747 return ret;
2750 /* Approximates cardinal spline with Bezier curves. */
2751 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2752 GDIPCONST GpPointF *points, INT count, REAL tension)
2754 GpPath *path;
2755 GpStatus status;
2757 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2759 if(!graphics || !pen)
2760 return InvalidParameter;
2762 if(graphics->busy)
2763 return ObjectBusy;
2765 if(count < 2)
2766 return InvalidParameter;
2768 status = GdipCreatePath(FillModeAlternate, &path);
2769 if (status != Ok) return status;
2771 status = GdipAddPathCurve2(path, points, count, tension);
2772 if (status == Ok)
2773 status = GdipDrawPath(graphics, pen, path);
2775 GdipDeletePath(path);
2776 return status;
2779 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2780 GDIPCONST GpPoint *points, INT count, REAL tension)
2782 GpPointF *pointsF;
2783 GpStatus ret;
2784 INT i;
2786 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2788 if(!points)
2789 return InvalidParameter;
2791 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2792 if(!pointsF)
2793 return OutOfMemory;
2795 for(i = 0; i < count; i++){
2796 pointsF[i].X = (REAL)points[i].X;
2797 pointsF[i].Y = (REAL)points[i].Y;
2800 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2801 GdipFree(pointsF);
2803 return ret;
2806 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2807 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2808 REAL tension)
2810 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2812 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2813 return InvalidParameter;
2816 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2819 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2820 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2821 REAL tension)
2823 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2825 if(count < 0){
2826 return OutOfMemory;
2829 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2830 return InvalidParameter;
2833 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2836 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2837 REAL y, REAL width, REAL height)
2839 INT save_state;
2840 GpPointF ptf[2];
2841 POINT pti[2];
2843 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2845 if(!graphics || !pen)
2846 return InvalidParameter;
2848 if(graphics->busy)
2849 return ObjectBusy;
2851 if (!graphics->hdc)
2853 FIXME("graphics object has no HDC\n");
2854 return Ok;
2857 ptf[0].X = x;
2858 ptf[0].Y = y;
2859 ptf[1].X = x + width;
2860 ptf[1].Y = y + height;
2862 save_state = prepare_dc(graphics, pen);
2863 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2865 transform_and_round_points(graphics, pti, ptf, 2);
2867 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2869 restore_dc(graphics, save_state);
2871 return Ok;
2874 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2875 INT y, INT width, INT height)
2877 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2879 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2883 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2885 UINT width, height;
2887 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2889 if(!graphics || !image)
2890 return InvalidParameter;
2892 GdipGetImageWidth(image, &width);
2893 GdipGetImageHeight(image, &height);
2895 return GdipDrawImagePointRect(graphics, image, x, y,
2896 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2899 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2900 INT y)
2902 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2904 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2907 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2908 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2909 GpUnit srcUnit)
2911 GpPointF points[3];
2912 REAL scale_x, scale_y, width, height;
2914 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2916 if (!graphics || !image) return InvalidParameter;
2918 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2919 scale_x *= graphics->xres / image->xres;
2920 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2921 scale_y *= graphics->yres / image->yres;
2922 width = srcwidth * scale_x;
2923 height = srcheight * scale_y;
2925 points[0].X = points[2].X = x;
2926 points[0].Y = points[1].Y = y;
2927 points[1].X = x + width;
2928 points[2].Y = y + height;
2930 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2931 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2934 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2935 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2936 GpUnit srcUnit)
2938 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2941 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2942 GDIPCONST GpPointF *dstpoints, INT count)
2944 UINT width, height;
2946 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2948 if(!image)
2949 return InvalidParameter;
2951 GdipGetImageWidth(image, &width);
2952 GdipGetImageHeight(image, &height);
2954 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2955 width, height, UnitPixel, NULL, NULL, NULL);
2958 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2959 GDIPCONST GpPoint *dstpoints, INT count)
2961 GpPointF ptf[3];
2963 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2965 if (count != 3 || !dstpoints)
2966 return InvalidParameter;
2968 ptf[0].X = (REAL)dstpoints[0].X;
2969 ptf[0].Y = (REAL)dstpoints[0].Y;
2970 ptf[1].X = (REAL)dstpoints[1].X;
2971 ptf[1].Y = (REAL)dstpoints[1].Y;
2972 ptf[2].X = (REAL)dstpoints[2].X;
2973 ptf[2].Y = (REAL)dstpoints[2].Y;
2975 return GdipDrawImagePoints(graphics, image, ptf, count);
2978 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2979 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2981 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2982 return TRUE;
2985 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2986 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2987 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2988 DrawImageAbort callback, VOID * callbackData)
2990 GpPointF ptf[4];
2991 POINT pti[4];
2992 GpStatus stat;
2994 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2995 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2996 callbackData);
2998 if (count > 3)
2999 return NotImplemented;
3001 if(!graphics || !image || !points || count != 3)
3002 return InvalidParameter;
3004 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3005 debugstr_pointf(&points[2]));
3007 memcpy(ptf, points, 3 * sizeof(GpPointF));
3008 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3009 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3010 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3011 return Ok;
3012 transform_and_round_points(graphics, pti, ptf, 4);
3014 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3015 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3017 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3018 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3019 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3020 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3021 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3023 if (image->picture)
3025 if (!graphics->hdc)
3027 FIXME("graphics object has no HDC\n");
3030 if(IPicture_Render(image->picture, graphics->hdc,
3031 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3032 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3034 if(callback)
3035 callback(callbackData);
3036 return GenericError;
3039 else if (image->type == ImageTypeBitmap)
3041 GpBitmap* bitmap = (GpBitmap*)image;
3042 int use_software=0;
3044 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3045 graphics->xres, graphics->yres,
3046 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3047 graphics->scale, image->xres, image->yres, bitmap->format,
3048 imageAttributes ? imageAttributes->outside_color : 0);
3050 if (imageAttributes || graphics->alpha_hdc ||
3051 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3052 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3053 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3054 srcx < 0 || srcy < 0 ||
3055 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3056 use_software = 1;
3058 if (use_software)
3060 RECT dst_area;
3061 GpRect src_area;
3062 int i, x, y, src_stride, dst_stride;
3063 GpMatrix dst_to_src;
3064 REAL m11, m12, m21, m22, mdx, mdy;
3065 LPBYTE src_data, dst_data;
3066 BitmapData lockeddata;
3067 InterpolationMode interpolation = graphics->interpolation;
3068 PixelOffsetMode offset_mode = graphics->pixeloffset;
3069 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3070 REAL x_dx, x_dy, y_dx, y_dy;
3071 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3073 if (!imageAttributes)
3074 imageAttributes = &defaultImageAttributes;
3076 dst_area.left = dst_area.right = pti[0].x;
3077 dst_area.top = dst_area.bottom = pti[0].y;
3078 for (i=1; i<4; i++)
3080 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3081 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3082 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3083 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3086 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3088 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3089 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3090 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3091 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3092 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3093 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3095 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3097 stat = GdipInvertMatrix(&dst_to_src);
3098 if (stat != Ok) return stat;
3100 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3101 if (!dst_data) return OutOfMemory;
3103 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3105 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3106 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3108 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3110 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3111 if (!src_data)
3113 GdipFree(dst_data);
3114 return OutOfMemory;
3116 src_stride = sizeof(ARGB) * src_area.Width;
3118 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3119 lockeddata.Width = src_area.Width;
3120 lockeddata.Height = src_area.Height;
3121 lockeddata.Stride = src_stride;
3122 lockeddata.PixelFormat = PixelFormat32bppARGB;
3123 lockeddata.Scan0 = src_data;
3125 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3126 PixelFormat32bppARGB, &lockeddata);
3128 if (stat == Ok)
3129 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3131 if (stat != Ok)
3133 if (src_data != dst_data)
3134 GdipFree(src_data);
3135 GdipFree(dst_data);
3136 return stat;
3139 apply_image_attributes(imageAttributes, src_data,
3140 src_area.Width, src_area.Height,
3141 src_stride, ColorAdjustTypeBitmap);
3143 /* Transform the bits as needed to the destination. */
3144 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3146 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3147 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3148 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3149 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3151 for (x=dst_area.left; x<dst_area.right; x++)
3153 for (y=dst_area.top; y<dst_area.bottom; y++)
3155 GpPointF src_pointf;
3156 ARGB *dst_color;
3158 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3159 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3161 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3163 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3164 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3165 imageAttributes, interpolation, offset_mode);
3166 else
3167 *dst_color = 0;
3171 GdipFree(src_data);
3173 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3174 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3176 GdipFree(dst_data);
3178 return stat;
3180 else
3182 HDC hdc;
3183 int temp_hdc=0, temp_bitmap=0;
3184 HBITMAP hbitmap, old_hbm=NULL;
3186 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3187 bitmap->format == PixelFormat24bppRGB ||
3188 bitmap->format == PixelFormat32bppRGB ||
3189 bitmap->format == PixelFormat32bppPARGB))
3191 BITMAPINFOHEADER bih;
3192 BYTE *temp_bits;
3193 PixelFormat dst_format;
3195 /* we can't draw a bitmap of this format directly */
3196 hdc = CreateCompatibleDC(0);
3197 temp_hdc = 1;
3198 temp_bitmap = 1;
3200 bih.biSize = sizeof(BITMAPINFOHEADER);
3201 bih.biWidth = bitmap->width;
3202 bih.biHeight = -bitmap->height;
3203 bih.biPlanes = 1;
3204 bih.biBitCount = 32;
3205 bih.biCompression = BI_RGB;
3206 bih.biSizeImage = 0;
3207 bih.biXPelsPerMeter = 0;
3208 bih.biYPelsPerMeter = 0;
3209 bih.biClrUsed = 0;
3210 bih.biClrImportant = 0;
3212 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3213 (void**)&temp_bits, NULL, 0);
3215 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3216 dst_format = PixelFormat32bppPARGB;
3217 else
3218 dst_format = PixelFormat32bppRGB;
3220 convert_pixels(bitmap->width, bitmap->height,
3221 bitmap->width*4, temp_bits, dst_format,
3222 bitmap->stride, bitmap->bits, bitmap->format,
3223 bitmap->image.palette);
3225 else
3227 if (bitmap->hbitmap)
3228 hbitmap = bitmap->hbitmap;
3229 else
3231 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3232 temp_bitmap = 1;
3235 hdc = bitmap->hdc;
3236 temp_hdc = (hdc == 0);
3239 if (temp_hdc)
3241 if (!hdc) hdc = CreateCompatibleDC(0);
3242 old_hbm = SelectObject(hdc, hbitmap);
3245 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3247 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3248 hdc, srcx, srcy, srcwidth, srcheight);
3250 else
3252 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3253 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3256 if (temp_hdc)
3258 SelectObject(hdc, old_hbm);
3259 DeleteDC(hdc);
3262 if (temp_bitmap)
3263 DeleteObject(hbitmap);
3266 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3268 GpRectF rc;
3270 rc.X = srcx;
3271 rc.Y = srcy;
3272 rc.Width = srcwidth;
3273 rc.Height = srcheight;
3275 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3276 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3278 else
3280 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3281 return InvalidParameter;
3284 return Ok;
3287 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3288 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3289 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3290 DrawImageAbort callback, VOID * callbackData)
3292 GpPointF pointsF[3];
3293 INT i;
3295 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3296 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3297 callbackData);
3299 if(!points || count!=3)
3300 return InvalidParameter;
3302 for(i = 0; i < count; i++){
3303 pointsF[i].X = (REAL)points[i].X;
3304 pointsF[i].Y = (REAL)points[i].Y;
3307 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3308 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3309 callback, callbackData);
3312 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3313 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3314 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3315 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3316 VOID * callbackData)
3318 GpPointF points[3];
3320 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3321 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3322 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3324 points[0].X = dstx;
3325 points[0].Y = dsty;
3326 points[1].X = dstx + dstwidth;
3327 points[1].Y = dsty;
3328 points[2].X = dstx;
3329 points[2].Y = dsty + dstheight;
3331 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3332 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3335 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3336 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3337 INT srcwidth, INT srcheight, GpUnit srcUnit,
3338 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3339 VOID * callbackData)
3341 GpPointF points[3];
3343 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3344 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3345 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3347 points[0].X = dstx;
3348 points[0].Y = dsty;
3349 points[1].X = dstx + dstwidth;
3350 points[1].Y = dsty;
3351 points[2].X = dstx;
3352 points[2].Y = dsty + dstheight;
3354 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3355 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3358 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3359 REAL x, REAL y, REAL width, REAL height)
3361 RectF bounds;
3362 GpUnit unit;
3363 GpStatus ret;
3365 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3367 if(!graphics || !image)
3368 return InvalidParameter;
3370 ret = GdipGetImageBounds(image, &bounds, &unit);
3371 if(ret != Ok)
3372 return ret;
3374 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3375 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3376 unit, NULL, NULL, NULL);
3379 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3380 INT x, INT y, INT width, INT height)
3382 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3384 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3387 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3388 REAL y1, REAL x2, REAL y2)
3390 GpPointF pt[2];
3392 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3394 pt[0].X = x1;
3395 pt[0].Y = y1;
3396 pt[1].X = x2;
3397 pt[1].Y = y2;
3398 return GdipDrawLines(graphics, pen, pt, 2);
3401 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3402 INT y1, INT x2, INT y2)
3404 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3406 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3409 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3410 GpPointF *points, INT count)
3412 GpStatus status;
3413 GpPath *path;
3415 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3417 if(!pen || !graphics || (count < 2))
3418 return InvalidParameter;
3420 if(graphics->busy)
3421 return ObjectBusy;
3423 status = GdipCreatePath(FillModeAlternate, &path);
3424 if (status != Ok) return status;
3426 status = GdipAddPathLine2(path, points, count);
3427 if (status == Ok)
3428 status = GdipDrawPath(graphics, pen, path);
3430 GdipDeletePath(path);
3431 return status;
3434 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3435 GpPoint *points, INT count)
3437 GpStatus retval;
3438 GpPointF *ptf;
3439 int i;
3441 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3443 ptf = GdipAlloc(count * sizeof(GpPointF));
3444 if(!ptf) return OutOfMemory;
3446 for(i = 0; i < count; i ++){
3447 ptf[i].X = (REAL) points[i].X;
3448 ptf[i].Y = (REAL) points[i].Y;
3451 retval = GdipDrawLines(graphics, pen, ptf, count);
3453 GdipFree(ptf);
3454 return retval;
3457 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3459 INT save_state;
3460 GpStatus retval;
3462 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3464 if(!pen || !graphics)
3465 return InvalidParameter;
3467 if(graphics->busy)
3468 return ObjectBusy;
3470 if (!graphics->hdc)
3472 FIXME("graphics object has no HDC\n");
3473 return Ok;
3476 save_state = prepare_dc(graphics, pen);
3478 retval = draw_poly(graphics, pen, path->pathdata.Points,
3479 path->pathdata.Types, path->pathdata.Count, TRUE);
3481 restore_dc(graphics, save_state);
3483 return retval;
3486 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3487 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3489 INT save_state;
3491 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3492 width, height, startAngle, sweepAngle);
3494 if(!graphics || !pen)
3495 return InvalidParameter;
3497 if(graphics->busy)
3498 return ObjectBusy;
3500 if (!graphics->hdc)
3502 FIXME("graphics object has no HDC\n");
3503 return Ok;
3506 save_state = prepare_dc(graphics, pen);
3507 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3509 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3511 restore_dc(graphics, save_state);
3513 return Ok;
3516 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3517 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3519 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3520 width, height, startAngle, sweepAngle);
3522 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3525 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3526 REAL y, REAL width, REAL height)
3528 INT save_state;
3529 GpPointF ptf[4];
3530 POINT pti[4];
3532 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3534 if(!pen || !graphics)
3535 return InvalidParameter;
3537 if(graphics->busy)
3538 return ObjectBusy;
3540 if (!graphics->hdc)
3542 FIXME("graphics object has no HDC\n");
3543 return Ok;
3546 ptf[0].X = x;
3547 ptf[0].Y = y;
3548 ptf[1].X = x + width;
3549 ptf[1].Y = y;
3550 ptf[2].X = x + width;
3551 ptf[2].Y = y + height;
3552 ptf[3].X = x;
3553 ptf[3].Y = y + height;
3555 save_state = prepare_dc(graphics, pen);
3556 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3558 transform_and_round_points(graphics, pti, ptf, 4);
3559 Polygon(graphics->hdc, pti, 4);
3561 restore_dc(graphics, save_state);
3563 return Ok;
3566 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3567 INT y, INT width, INT height)
3569 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3571 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3574 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3575 GDIPCONST GpRectF* rects, INT count)
3577 GpPointF *ptf;
3578 POINT *pti;
3579 INT save_state, i;
3581 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3583 if(!graphics || !pen || !rects || count < 1)
3584 return InvalidParameter;
3586 if(graphics->busy)
3587 return ObjectBusy;
3589 if (!graphics->hdc)
3591 FIXME("graphics object has no HDC\n");
3592 return Ok;
3595 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3596 pti = GdipAlloc(4 * count * sizeof(POINT));
3598 if(!ptf || !pti){
3599 GdipFree(ptf);
3600 GdipFree(pti);
3601 return OutOfMemory;
3604 for(i = 0; i < count; i++){
3605 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3606 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3607 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3608 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3611 save_state = prepare_dc(graphics, pen);
3612 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3614 transform_and_round_points(graphics, pti, ptf, 4 * count);
3616 for(i = 0; i < count; i++)
3617 Polygon(graphics->hdc, &pti[4 * i], 4);
3619 restore_dc(graphics, save_state);
3621 GdipFree(ptf);
3622 GdipFree(pti);
3624 return Ok;
3627 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3628 GDIPCONST GpRect* rects, INT count)
3630 GpRectF *rectsF;
3631 GpStatus ret;
3632 INT i;
3634 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3636 if(!rects || count<=0)
3637 return InvalidParameter;
3639 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3640 if(!rectsF)
3641 return OutOfMemory;
3643 for(i = 0;i < count;i++){
3644 rectsF[i].X = (REAL)rects[i].X;
3645 rectsF[i].Y = (REAL)rects[i].Y;
3646 rectsF[i].Width = (REAL)rects[i].Width;
3647 rectsF[i].Height = (REAL)rects[i].Height;
3650 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3651 GdipFree(rectsF);
3653 return ret;
3656 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3657 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3659 GpPath *path;
3660 GpStatus stat;
3662 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3663 count, tension, fill);
3665 if(!graphics || !brush || !points)
3666 return InvalidParameter;
3668 if(graphics->busy)
3669 return ObjectBusy;
3671 if(count == 1) /* Do nothing */
3672 return Ok;
3674 stat = GdipCreatePath(fill, &path);
3675 if(stat != Ok)
3676 return stat;
3678 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3679 if(stat != Ok){
3680 GdipDeletePath(path);
3681 return stat;
3684 stat = GdipFillPath(graphics, brush, path);
3685 if(stat != Ok){
3686 GdipDeletePath(path);
3687 return stat;
3690 GdipDeletePath(path);
3692 return Ok;
3695 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3696 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3698 GpPointF *ptf;
3699 GpStatus stat;
3700 INT i;
3702 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3703 count, tension, fill);
3705 if(!points || count == 0)
3706 return InvalidParameter;
3708 if(count == 1) /* Do nothing */
3709 return Ok;
3711 ptf = GdipAlloc(sizeof(GpPointF)*count);
3712 if(!ptf)
3713 return OutOfMemory;
3715 for(i = 0;i < count;i++){
3716 ptf[i].X = (REAL)points[i].X;
3717 ptf[i].Y = (REAL)points[i].Y;
3720 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3722 GdipFree(ptf);
3724 return stat;
3727 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3728 GDIPCONST GpPointF *points, INT count)
3730 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3731 return GdipFillClosedCurve2(graphics, brush, points, count,
3732 0.5f, FillModeAlternate);
3735 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3736 GDIPCONST GpPoint *points, INT count)
3738 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3739 return GdipFillClosedCurve2I(graphics, brush, points, count,
3740 0.5f, FillModeAlternate);
3743 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3744 REAL y, REAL width, REAL height)
3746 GpStatus stat;
3747 GpPath *path;
3749 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3751 if(!graphics || !brush)
3752 return InvalidParameter;
3754 if(graphics->busy)
3755 return ObjectBusy;
3757 stat = GdipCreatePath(FillModeAlternate, &path);
3759 if (stat == Ok)
3761 stat = GdipAddPathEllipse(path, x, y, width, height);
3763 if (stat == Ok)
3764 stat = GdipFillPath(graphics, brush, path);
3766 GdipDeletePath(path);
3769 return stat;
3772 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3773 INT y, INT width, INT height)
3775 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3777 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3780 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3782 INT save_state;
3783 GpStatus retval;
3785 if(!graphics->hdc || !brush_can_fill_path(brush))
3786 return NotImplemented;
3788 save_state = SaveDC(graphics->hdc);
3789 EndPath(graphics->hdc);
3790 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3791 : WINDING));
3793 BeginPath(graphics->hdc);
3794 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3795 path->pathdata.Types, path->pathdata.Count, FALSE);
3797 if(retval != Ok)
3798 goto end;
3800 EndPath(graphics->hdc);
3801 brush_fill_path(graphics, brush);
3803 retval = Ok;
3805 end:
3806 RestoreDC(graphics->hdc, save_state);
3808 return retval;
3811 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3813 GpStatus stat;
3814 GpRegion *rgn;
3816 if (!brush_can_fill_pixels(brush))
3817 return NotImplemented;
3819 /* FIXME: This could probably be done more efficiently without regions. */
3821 stat = GdipCreateRegionPath(path, &rgn);
3823 if (stat == Ok)
3825 stat = GdipFillRegion(graphics, brush, rgn);
3827 GdipDeleteRegion(rgn);
3830 return stat;
3833 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3835 GpStatus stat = NotImplemented;
3837 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3839 if(!brush || !graphics || !path)
3840 return InvalidParameter;
3842 if(graphics->busy)
3843 return ObjectBusy;
3845 if (!graphics->image && !graphics->alpha_hdc)
3846 stat = GDI32_GdipFillPath(graphics, brush, path);
3848 if (stat == NotImplemented)
3849 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3851 if (stat == NotImplemented)
3853 FIXME("Not implemented for brushtype %i\n", brush->bt);
3854 stat = Ok;
3857 return stat;
3860 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3861 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3863 GpStatus stat;
3864 GpPath *path;
3866 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3867 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3869 if(!graphics || !brush)
3870 return InvalidParameter;
3872 if(graphics->busy)
3873 return ObjectBusy;
3875 stat = GdipCreatePath(FillModeAlternate, &path);
3877 if (stat == Ok)
3879 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3881 if (stat == Ok)
3882 stat = GdipFillPath(graphics, brush, path);
3884 GdipDeletePath(path);
3887 return stat;
3890 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3891 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3893 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3894 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3896 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3899 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3900 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3902 GpStatus stat;
3903 GpPath *path;
3905 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3907 if(!graphics || !brush || !points || !count)
3908 return InvalidParameter;
3910 if(graphics->busy)
3911 return ObjectBusy;
3913 stat = GdipCreatePath(fillMode, &path);
3915 if (stat == Ok)
3917 stat = GdipAddPathPolygon(path, points, count);
3919 if (stat == Ok)
3920 stat = GdipFillPath(graphics, brush, path);
3922 GdipDeletePath(path);
3925 return stat;
3928 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3929 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3931 GpStatus stat;
3932 GpPath *path;
3934 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3936 if(!graphics || !brush || !points || !count)
3937 return InvalidParameter;
3939 if(graphics->busy)
3940 return ObjectBusy;
3942 stat = GdipCreatePath(fillMode, &path);
3944 if (stat == Ok)
3946 stat = GdipAddPathPolygonI(path, points, count);
3948 if (stat == Ok)
3949 stat = GdipFillPath(graphics, brush, path);
3951 GdipDeletePath(path);
3954 return stat;
3957 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3958 GDIPCONST GpPointF *points, INT count)
3960 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3962 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3965 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3966 GDIPCONST GpPoint *points, INT count)
3968 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3970 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3973 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3974 REAL x, REAL y, REAL width, REAL height)
3976 GpStatus stat;
3977 GpPath *path;
3979 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3981 if(!graphics || !brush)
3982 return InvalidParameter;
3984 if(graphics->busy)
3985 return ObjectBusy;
3987 stat = GdipCreatePath(FillModeAlternate, &path);
3989 if (stat == Ok)
3991 stat = GdipAddPathRectangle(path, x, y, width, height);
3993 if (stat == Ok)
3994 stat = GdipFillPath(graphics, brush, path);
3996 GdipDeletePath(path);
3999 return stat;
4002 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4003 INT x, INT y, INT width, INT height)
4005 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4007 return GdipFillRectangle(graphics, brush, x, y, width, height);
4010 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4011 INT count)
4013 GpStatus ret;
4014 INT i;
4016 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4018 if(!rects)
4019 return InvalidParameter;
4021 for(i = 0; i < count; i++){
4022 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4023 if(ret != Ok) return ret;
4026 return Ok;
4029 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4030 INT count)
4032 GpRectF *rectsF;
4033 GpStatus ret;
4034 INT i;
4036 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4038 if(!rects || count <= 0)
4039 return InvalidParameter;
4041 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4042 if(!rectsF)
4043 return OutOfMemory;
4045 for(i = 0; i < count; i++){
4046 rectsF[i].X = (REAL)rects[i].X;
4047 rectsF[i].Y = (REAL)rects[i].Y;
4048 rectsF[i].X = (REAL)rects[i].Width;
4049 rectsF[i].Height = (REAL)rects[i].Height;
4052 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4053 GdipFree(rectsF);
4055 return ret;
4058 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4059 GpRegion* region)
4061 INT save_state;
4062 GpStatus status;
4063 HRGN hrgn;
4064 RECT rc;
4066 if(!graphics->hdc || !brush_can_fill_path(brush))
4067 return NotImplemented;
4069 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4070 if(status != Ok)
4071 return status;
4073 save_state = SaveDC(graphics->hdc);
4074 EndPath(graphics->hdc);
4076 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4078 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4080 BeginPath(graphics->hdc);
4081 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4082 EndPath(graphics->hdc);
4084 brush_fill_path(graphics, brush);
4087 RestoreDC(graphics->hdc, save_state);
4089 DeleteObject(hrgn);
4091 return Ok;
4094 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4095 GpRegion* region)
4097 GpStatus stat;
4098 GpRegion *temp_region;
4099 GpMatrix world_to_device;
4100 GpRectF graphics_bounds;
4101 DWORD *pixel_data;
4102 HRGN hregion;
4103 RECT bound_rect;
4104 GpRect gp_bound_rect;
4106 if (!brush_can_fill_pixels(brush))
4107 return NotImplemented;
4109 stat = get_graphics_bounds(graphics, &graphics_bounds);
4111 if (stat == Ok)
4112 stat = GdipCloneRegion(region, &temp_region);
4114 if (stat == Ok)
4116 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4117 CoordinateSpaceWorld, &world_to_device);
4119 if (stat == Ok)
4120 stat = GdipTransformRegion(temp_region, &world_to_device);
4122 if (stat == Ok)
4123 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4125 if (stat == Ok)
4126 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4128 GdipDeleteRegion(temp_region);
4131 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4133 DeleteObject(hregion);
4134 return Ok;
4137 if (stat == Ok)
4139 gp_bound_rect.X = bound_rect.left;
4140 gp_bound_rect.Y = bound_rect.top;
4141 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4142 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4144 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4145 if (!pixel_data)
4146 stat = OutOfMemory;
4148 if (stat == Ok)
4150 stat = brush_fill_pixels(graphics, brush, pixel_data,
4151 &gp_bound_rect, gp_bound_rect.Width);
4153 if (stat == Ok)
4154 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4155 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4156 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4158 GdipFree(pixel_data);
4161 DeleteObject(hregion);
4164 return stat;
4167 /*****************************************************************************
4168 * GdipFillRegion [GDIPLUS.@]
4170 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4171 GpRegion* region)
4173 GpStatus stat = NotImplemented;
4175 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4177 if (!(graphics && brush && region))
4178 return InvalidParameter;
4180 if(graphics->busy)
4181 return ObjectBusy;
4183 if (!graphics->image && !graphics->alpha_hdc)
4184 stat = GDI32_GdipFillRegion(graphics, brush, region);
4186 if (stat == NotImplemented)
4187 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4189 if (stat == NotImplemented)
4191 FIXME("not implemented for brushtype %i\n", brush->bt);
4192 stat = Ok;
4195 return stat;
4198 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4200 TRACE("(%p,%u)\n", graphics, intention);
4202 if(!graphics)
4203 return InvalidParameter;
4205 if(graphics->busy)
4206 return ObjectBusy;
4208 /* We have no internal operation queue, so there's no need to clear it. */
4210 if (graphics->hdc)
4211 GdiFlush();
4213 return Ok;
4216 /*****************************************************************************
4217 * GdipGetClipBounds [GDIPLUS.@]
4219 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4221 TRACE("(%p, %p)\n", graphics, rect);
4223 if(!graphics)
4224 return InvalidParameter;
4226 if(graphics->busy)
4227 return ObjectBusy;
4229 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4232 /*****************************************************************************
4233 * GdipGetClipBoundsI [GDIPLUS.@]
4235 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4237 TRACE("(%p, %p)\n", graphics, rect);
4239 if(!graphics)
4240 return InvalidParameter;
4242 if(graphics->busy)
4243 return ObjectBusy;
4245 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4248 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4249 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4250 CompositingMode *mode)
4252 TRACE("(%p, %p)\n", graphics, mode);
4254 if(!graphics || !mode)
4255 return InvalidParameter;
4257 if(graphics->busy)
4258 return ObjectBusy;
4260 *mode = graphics->compmode;
4262 return Ok;
4265 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4266 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4267 CompositingQuality *quality)
4269 TRACE("(%p, %p)\n", graphics, quality);
4271 if(!graphics || !quality)
4272 return InvalidParameter;
4274 if(graphics->busy)
4275 return ObjectBusy;
4277 *quality = graphics->compqual;
4279 return Ok;
4282 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4283 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4284 InterpolationMode *mode)
4286 TRACE("(%p, %p)\n", graphics, mode);
4288 if(!graphics || !mode)
4289 return InvalidParameter;
4291 if(graphics->busy)
4292 return ObjectBusy;
4294 *mode = graphics->interpolation;
4296 return Ok;
4299 /* FIXME: Need to handle color depths less than 24bpp */
4300 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4302 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4304 if(!graphics || !argb)
4305 return InvalidParameter;
4307 if(graphics->busy)
4308 return ObjectBusy;
4310 return Ok;
4313 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4315 TRACE("(%p, %p)\n", graphics, scale);
4317 if(!graphics || !scale)
4318 return InvalidParameter;
4320 if(graphics->busy)
4321 return ObjectBusy;
4323 *scale = graphics->scale;
4325 return Ok;
4328 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4330 TRACE("(%p, %p)\n", graphics, unit);
4332 if(!graphics || !unit)
4333 return InvalidParameter;
4335 if(graphics->busy)
4336 return ObjectBusy;
4338 *unit = graphics->unit;
4340 return Ok;
4343 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4344 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4345 *mode)
4347 TRACE("(%p, %p)\n", graphics, mode);
4349 if(!graphics || !mode)
4350 return InvalidParameter;
4352 if(graphics->busy)
4353 return ObjectBusy;
4355 *mode = graphics->pixeloffset;
4357 return Ok;
4360 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4361 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4363 TRACE("(%p, %p)\n", graphics, mode);
4365 if(!graphics || !mode)
4366 return InvalidParameter;
4368 if(graphics->busy)
4369 return ObjectBusy;
4371 *mode = graphics->smoothing;
4373 return Ok;
4376 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4378 TRACE("(%p, %p)\n", graphics, contrast);
4380 if(!graphics || !contrast)
4381 return InvalidParameter;
4383 *contrast = graphics->textcontrast;
4385 return Ok;
4388 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4389 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4390 TextRenderingHint *hint)
4392 TRACE("(%p, %p)\n", graphics, hint);
4394 if(!graphics || !hint)
4395 return InvalidParameter;
4397 if(graphics->busy)
4398 return ObjectBusy;
4400 *hint = graphics->texthint;
4402 return Ok;
4405 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4407 GpRegion *clip_rgn;
4408 GpStatus stat;
4410 TRACE("(%p, %p)\n", graphics, rect);
4412 if(!graphics || !rect)
4413 return InvalidParameter;
4415 if(graphics->busy)
4416 return ObjectBusy;
4418 /* intersect window and graphics clipping regions */
4419 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4420 return stat;
4422 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4423 goto cleanup;
4425 /* get bounds of the region */
4426 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4428 cleanup:
4429 GdipDeleteRegion(clip_rgn);
4431 return stat;
4434 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4436 GpRectF rectf;
4437 GpStatus stat;
4439 TRACE("(%p, %p)\n", graphics, rect);
4441 if(!graphics || !rect)
4442 return InvalidParameter;
4444 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4446 rect->X = gdip_round(rectf.X);
4447 rect->Y = gdip_round(rectf.Y);
4448 rect->Width = gdip_round(rectf.Width);
4449 rect->Height = gdip_round(rectf.Height);
4452 return stat;
4455 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4457 TRACE("(%p, %p)\n", graphics, matrix);
4459 if(!graphics || !matrix)
4460 return InvalidParameter;
4462 if(graphics->busy)
4463 return ObjectBusy;
4465 *matrix = graphics->worldtrans;
4466 return Ok;
4469 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4471 GpSolidFill *brush;
4472 GpStatus stat;
4473 GpRectF wnd_rect;
4475 TRACE("(%p, %x)\n", graphics, color);
4477 if(!graphics)
4478 return InvalidParameter;
4480 if(graphics->busy)
4481 return ObjectBusy;
4483 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4484 return stat;
4486 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4487 GdipDeleteBrush((GpBrush*)brush);
4488 return stat;
4491 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4492 wnd_rect.Width, wnd_rect.Height);
4494 GdipDeleteBrush((GpBrush*)brush);
4496 return Ok;
4499 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4501 TRACE("(%p, %p)\n", graphics, res);
4503 if(!graphics || !res)
4504 return InvalidParameter;
4506 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4509 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4511 GpStatus stat;
4512 GpRegion* rgn;
4513 GpPointF pt;
4515 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4517 if(!graphics || !result)
4518 return InvalidParameter;
4520 if(graphics->busy)
4521 return ObjectBusy;
4523 pt.X = x;
4524 pt.Y = y;
4525 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4526 CoordinateSpaceWorld, &pt, 1)) != Ok)
4527 return stat;
4529 if((stat = GdipCreateRegion(&rgn)) != Ok)
4530 return stat;
4532 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4533 goto cleanup;
4535 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4537 cleanup:
4538 GdipDeleteRegion(rgn);
4539 return stat;
4542 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4544 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4547 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4549 GpStatus stat;
4550 GpRegion* rgn;
4551 GpPointF pts[2];
4553 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4555 if(!graphics || !result)
4556 return InvalidParameter;
4558 if(graphics->busy)
4559 return ObjectBusy;
4561 pts[0].X = x;
4562 pts[0].Y = y;
4563 pts[1].X = x + width;
4564 pts[1].Y = y + height;
4566 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4567 CoordinateSpaceWorld, pts, 2)) != Ok)
4568 return stat;
4570 pts[1].X -= pts[0].X;
4571 pts[1].Y -= pts[0].Y;
4573 if((stat = GdipCreateRegion(&rgn)) != Ok)
4574 return stat;
4576 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4577 goto cleanup;
4579 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4581 cleanup:
4582 GdipDeleteRegion(rgn);
4583 return stat;
4586 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4588 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4591 GpStatus gdip_format_string(HDC hdc,
4592 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4593 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4594 gdip_format_string_callback callback, void *user_data)
4596 WCHAR* stringdup;
4597 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4598 nheight, lineend, lineno = 0;
4599 RectF bounds;
4600 StringAlignment halign;
4601 GpStatus stat = Ok;
4602 SIZE size;
4603 HotkeyPrefix hkprefix;
4604 INT *hotkeyprefix_offsets=NULL;
4605 INT hotkeyprefix_count=0;
4606 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4607 int seen_prefix=0;
4609 if(length == -1) length = lstrlenW(string);
4611 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4612 if(!stringdup) return OutOfMemory;
4614 nwidth = rect->Width;
4615 nheight = rect->Height;
4617 if (format)
4618 hkprefix = format->hkprefix;
4619 else
4620 hkprefix = HotkeyPrefixNone;
4622 if (hkprefix == HotkeyPrefixShow)
4624 for (i=0; i<length; i++)
4626 if (string[i] == '&')
4627 hotkeyprefix_count++;
4631 if (hotkeyprefix_count)
4632 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4634 hotkeyprefix_count = 0;
4636 for(i = 0, j = 0; i < length; i++){
4637 /* FIXME: This makes the indexes passed to callback inaccurate. */
4638 if(!isprintW(string[i]) && (string[i] != '\n'))
4639 continue;
4641 /* FIXME: tabs should be handled using tabstops from stringformat */
4642 if (string[i] == '\t')
4643 continue;
4645 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4646 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4647 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4649 seen_prefix = 1;
4650 continue;
4653 seen_prefix = 0;
4655 stringdup[j] = string[i];
4656 j++;
4659 length = j;
4661 if (format) halign = format->align;
4662 else halign = StringAlignmentNear;
4664 while(sum < length){
4665 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4666 nwidth, &fit, NULL, &size);
4667 fitcpy = fit;
4669 if(fit == 0)
4670 break;
4672 for(lret = 0; lret < fit; lret++)
4673 if(*(stringdup + sum + lret) == '\n')
4674 break;
4676 /* Line break code (may look strange, but it imitates windows). */
4677 if(lret < fit)
4678 lineend = fit = lret; /* this is not an off-by-one error */
4679 else if(fit < (length - sum)){
4680 if(*(stringdup + sum + fit) == ' ')
4681 while(*(stringdup + sum + fit) == ' ')
4682 fit++;
4683 else
4684 while(*(stringdup + sum + fit - 1) != ' '){
4685 fit--;
4687 if(*(stringdup + sum + fit) == '\t')
4688 break;
4690 if(fit == 0){
4691 fit = fitcpy;
4692 break;
4695 lineend = fit;
4696 while(*(stringdup + sum + lineend - 1) == ' ' ||
4697 *(stringdup + sum + lineend - 1) == '\t')
4698 lineend--;
4700 else
4701 lineend = fit;
4703 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4704 nwidth, &j, NULL, &size);
4706 bounds.Width = size.cx;
4708 if(height + size.cy > nheight)
4709 bounds.Height = nheight - (height + size.cy);
4710 else
4711 bounds.Height = size.cy;
4713 bounds.Y = rect->Y + height;
4715 switch (halign)
4717 case StringAlignmentNear:
4718 default:
4719 bounds.X = rect->X;
4720 break;
4721 case StringAlignmentCenter:
4722 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4723 break;
4724 case StringAlignmentFar:
4725 bounds.X = rect->X + rect->Width - bounds.Width;
4726 break;
4729 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4730 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4731 break;
4733 stat = callback(hdc, stringdup, sum, lineend,
4734 font, rect, format, lineno, &bounds,
4735 &hotkeyprefix_offsets[hotkeyprefix_pos],
4736 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4738 if (stat != Ok)
4739 break;
4741 sum += fit + (lret < fitcpy ? 1 : 0);
4742 height += size.cy;
4743 lineno++;
4745 hotkeyprefix_pos = hotkeyprefix_end_pos;
4747 if(height > nheight)
4748 break;
4750 /* Stop if this was a linewrap (but not if it was a linebreak). */
4751 if ((lret == fitcpy) && format &&
4752 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4753 break;
4756 GdipFree(stringdup);
4757 GdipFree(hotkeyprefix_offsets);
4759 return stat;
4762 struct measure_ranges_args {
4763 GpRegion **regions;
4764 REAL rel_width, rel_height;
4767 static GpStatus measure_ranges_callback(HDC hdc,
4768 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4769 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4770 INT lineno, const RectF *bounds, INT *underlined_indexes,
4771 INT underlined_index_count, void *user_data)
4773 int i;
4774 GpStatus stat = Ok;
4775 struct measure_ranges_args *args = user_data;
4777 for (i=0; i<format->range_count; i++)
4779 INT range_start = max(index, format->character_ranges[i].First);
4780 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4781 if (range_start < range_end)
4783 GpRectF range_rect;
4784 SIZE range_size;
4786 range_rect.Y = bounds->Y / args->rel_height;
4787 range_rect.Height = bounds->Height / args->rel_height;
4789 GetTextExtentExPointW(hdc, string + index, range_start - index,
4790 INT_MAX, NULL, NULL, &range_size);
4791 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4793 GetTextExtentExPointW(hdc, string + index, range_end - index,
4794 INT_MAX, NULL, NULL, &range_size);
4795 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4797 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4798 if (stat != Ok)
4799 break;
4803 return stat;
4806 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4807 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4808 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4809 INT regionCount, GpRegion** regions)
4811 GpStatus stat;
4812 int i;
4813 HFONT gdifont, oldfont;
4814 struct measure_ranges_args args;
4815 HDC hdc, temp_hdc=NULL;
4816 GpPointF pt[3];
4817 RectF scaled_rect;
4818 REAL margin_x;
4820 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4821 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4823 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4824 return InvalidParameter;
4826 if (regionCount < stringFormat->range_count)
4827 return InvalidParameter;
4829 if(!graphics->hdc)
4831 hdc = temp_hdc = CreateCompatibleDC(0);
4832 if (!temp_hdc) return OutOfMemory;
4834 else
4835 hdc = graphics->hdc;
4837 if (stringFormat->attr)
4838 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4840 pt[0].X = 0.0;
4841 pt[0].Y = 0.0;
4842 pt[1].X = 1.0;
4843 pt[1].Y = 0.0;
4844 pt[2].X = 0.0;
4845 pt[2].Y = 1.0;
4846 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4847 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4848 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4849 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4850 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4852 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4853 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4855 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4856 scaled_rect.Y = layoutRect->Y * args.rel_height;
4857 if (stringFormat->attr & StringFormatFlagsNoClip)
4859 scaled_rect.Width = (REAL)(1 << 23);
4860 scaled_rect.Height = (REAL)(1 << 23);
4862 else
4864 scaled_rect.Width = layoutRect->Width * args.rel_width;
4865 scaled_rect.Height = layoutRect->Height * args.rel_height;
4867 if (scaled_rect.Width >= 0.5)
4869 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4870 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4873 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4874 oldfont = SelectObject(hdc, gdifont);
4876 for (i=0; i<stringFormat->range_count; i++)
4878 stat = GdipSetEmpty(regions[i]);
4879 if (stat != Ok)
4880 return stat;
4883 args.regions = regions;
4885 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4886 measure_ranges_callback, &args);
4888 SelectObject(hdc, oldfont);
4889 DeleteObject(gdifont);
4891 if (temp_hdc)
4892 DeleteDC(temp_hdc);
4894 return stat;
4897 struct measure_string_args {
4898 RectF *bounds;
4899 INT *codepointsfitted;
4900 INT *linesfilled;
4901 REAL rel_width, rel_height;
4904 static GpStatus measure_string_callback(HDC hdc,
4905 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4906 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4907 INT lineno, const RectF *bounds, INT *underlined_indexes,
4908 INT underlined_index_count, void *user_data)
4910 struct measure_string_args *args = user_data;
4911 REAL new_width, new_height;
4913 new_width = bounds->Width / args->rel_width;
4914 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4916 if (new_width > args->bounds->Width)
4917 args->bounds->Width = new_width;
4919 if (new_height > args->bounds->Height)
4920 args->bounds->Height = new_height;
4922 if (args->codepointsfitted)
4923 *args->codepointsfitted = index + length;
4925 if (args->linesfilled)
4926 (*args->linesfilled)++;
4928 return Ok;
4931 /* Find the smallest rectangle that bounds the text when it is printed in rect
4932 * according to the format options listed in format. If rect has 0 width and
4933 * height, then just find the smallest rectangle that bounds the text when it's
4934 * printed at location (rect->X, rect-Y). */
4935 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4936 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4937 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4938 INT *codepointsfitted, INT *linesfilled)
4940 HFONT oldfont, gdifont;
4941 struct measure_string_args args;
4942 HDC temp_hdc=NULL, hdc;
4943 GpPointF pt[3];
4944 RectF scaled_rect;
4945 REAL margin_x;
4946 INT lines, glyphs, format_flags = format ? format->attr : 0;
4948 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4949 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4950 bounds, codepointsfitted, linesfilled);
4952 if(!graphics || !string || !font || !rect || !bounds)
4953 return InvalidParameter;
4955 if(!graphics->hdc)
4957 hdc = temp_hdc = CreateCompatibleDC(0);
4958 if (!temp_hdc) return OutOfMemory;
4960 else
4961 hdc = graphics->hdc;
4963 if(linesfilled) *linesfilled = 0;
4964 if(codepointsfitted) *codepointsfitted = 0;
4966 if(format)
4967 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4969 pt[0].X = 0.0;
4970 pt[0].Y = 0.0;
4971 pt[1].X = 1.0;
4972 pt[1].Y = 0.0;
4973 pt[2].X = 0.0;
4974 pt[2].Y = 1.0;
4975 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4976 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4977 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4978 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4979 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4981 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4982 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4984 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4985 scaled_rect.Y = rect->Y * args.rel_height;
4986 scaled_rect.Width = rect->Width * args.rel_width;
4987 scaled_rect.Height = rect->Height * args.rel_height;
4989 if ((format_flags & StringFormatFlagsNoClip) ||
4990 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
4991 if ((format_flags & StringFormatFlagsNoClip) ||
4992 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
4994 if (scaled_rect.Width >= 0.5)
4996 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4997 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5000 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5001 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5003 get_font_hfont(graphics, font, format, &gdifont, NULL);
5004 oldfont = SelectObject(hdc, gdifont);
5006 bounds->X = rect->X;
5007 bounds->Y = rect->Y;
5008 bounds->Width = 0.0;
5009 bounds->Height = 0.0;
5011 args.bounds = bounds;
5012 args.codepointsfitted = &glyphs;
5013 args.linesfilled = &lines;
5014 lines = glyphs = 0;
5016 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5017 measure_string_callback, &args);
5019 if (linesfilled) *linesfilled = lines;
5020 if (codepointsfitted) *codepointsfitted = glyphs;
5022 if (lines)
5023 bounds->Width += margin_x * 2.0;
5025 SelectObject(hdc, oldfont);
5026 DeleteObject(gdifont);
5028 if (temp_hdc)
5029 DeleteDC(temp_hdc);
5031 return Ok;
5034 struct draw_string_args {
5035 GpGraphics *graphics;
5036 GDIPCONST GpBrush *brush;
5037 REAL x, y, rel_width, rel_height, ascent;
5040 static GpStatus draw_string_callback(HDC hdc,
5041 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5042 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5043 INT lineno, const RectF *bounds, INT *underlined_indexes,
5044 INT underlined_index_count, void *user_data)
5046 struct draw_string_args *args = user_data;
5047 PointF position;
5048 GpStatus stat;
5050 position.X = args->x + bounds->X / args->rel_width;
5051 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5053 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5054 args->brush, &position,
5055 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5057 if (stat == Ok && underlined_index_count)
5059 OUTLINETEXTMETRICW otm;
5060 REAL underline_y, underline_height;
5061 int i;
5063 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5065 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5066 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5068 for (i=0; i<underlined_index_count; i++)
5070 REAL start_x, end_x;
5071 SIZE text_size;
5072 INT ofs = underlined_indexes[i] - index;
5074 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5075 start_x = text_size.cx / args->rel_width;
5077 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5078 end_x = text_size.cx / args->rel_width;
5080 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5084 return stat;
5087 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5088 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5089 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5091 HRGN rgn = NULL;
5092 HFONT gdifont;
5093 GpPointF pt[3], rectcpy[4];
5094 POINT corners[4];
5095 REAL rel_width, rel_height, margin_x;
5096 INT save_state, format_flags = 0;
5097 REAL offsety = 0.0;
5098 struct draw_string_args args;
5099 RectF scaled_rect;
5100 HDC hdc, temp_hdc=NULL;
5101 TEXTMETRICW textmetric;
5103 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5104 length, font, debugstr_rectf(rect), format, brush);
5106 if(!graphics || !string || !font || !brush || !rect)
5107 return InvalidParameter;
5109 if(graphics->hdc)
5111 hdc = graphics->hdc;
5113 else
5115 hdc = temp_hdc = CreateCompatibleDC(0);
5118 if(format){
5119 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5121 format_flags = format->attr;
5123 /* Should be no need to explicitly test for StringAlignmentNear as
5124 * that is default behavior if no alignment is passed. */
5125 if(format->vertalign != StringAlignmentNear){
5126 RectF bounds, in_rect = *rect;
5127 in_rect.Height = 0.0; /* avoid height clipping */
5128 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5130 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5132 if(format->vertalign == StringAlignmentCenter)
5133 offsety = (rect->Height - bounds.Height) / 2;
5134 else if(format->vertalign == StringAlignmentFar)
5135 offsety = (rect->Height - bounds.Height);
5137 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5140 save_state = SaveDC(hdc);
5142 pt[0].X = 0.0;
5143 pt[0].Y = 0.0;
5144 pt[1].X = 1.0;
5145 pt[1].Y = 0.0;
5146 pt[2].X = 0.0;
5147 pt[2].Y = 1.0;
5148 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5149 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5150 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5151 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5152 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5154 rectcpy[3].X = rectcpy[0].X = rect->X;
5155 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5156 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5157 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5158 transform_and_round_points(graphics, corners, rectcpy, 4);
5160 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5161 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5163 scaled_rect.X = margin_x * rel_width;
5164 scaled_rect.Y = 0.0;
5165 scaled_rect.Width = rel_width * rect->Width;
5166 scaled_rect.Height = rel_height * rect->Height;
5168 if ((format_flags & StringFormatFlagsNoClip) ||
5169 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5170 if ((format_flags & StringFormatFlagsNoClip) ||
5171 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5173 if (scaled_rect.Width >= 0.5)
5175 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5176 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5179 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5180 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5182 if (!(format_flags & StringFormatFlagsNoClip) &&
5183 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23)
5185 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5186 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5187 SelectClipRgn(hdc, rgn);
5190 get_font_hfont(graphics, font, format, &gdifont, NULL);
5191 SelectObject(hdc, gdifont);
5193 args.graphics = graphics;
5194 args.brush = brush;
5196 args.x = rect->X;
5197 args.y = rect->Y + offsety;
5199 args.rel_width = rel_width;
5200 args.rel_height = rel_height;
5202 GetTextMetricsW(hdc, &textmetric);
5203 args.ascent = textmetric.tmAscent / rel_height;
5205 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5206 draw_string_callback, &args);
5208 DeleteObject(rgn);
5209 DeleteObject(gdifont);
5211 RestoreDC(hdc, save_state);
5213 DeleteDC(temp_hdc);
5215 return Ok;
5218 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5220 TRACE("(%p)\n", graphics);
5222 if(!graphics)
5223 return InvalidParameter;
5225 if(graphics->busy)
5226 return ObjectBusy;
5228 return GdipSetInfinite(graphics->clip);
5231 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5233 TRACE("(%p)\n", graphics);
5235 if(!graphics)
5236 return InvalidParameter;
5238 if(graphics->busy)
5239 return ObjectBusy;
5241 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5244 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5246 return GdipEndContainer(graphics, state);
5249 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5250 GpMatrixOrder order)
5252 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5254 if(!graphics)
5255 return InvalidParameter;
5257 if(graphics->busy)
5258 return ObjectBusy;
5260 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5263 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5265 return GdipBeginContainer2(graphics, state);
5268 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5269 GraphicsContainer *state)
5271 GraphicsContainerItem *container;
5272 GpStatus sts;
5274 TRACE("(%p, %p)\n", graphics, state);
5276 if(!graphics || !state)
5277 return InvalidParameter;
5279 sts = init_container(&container, graphics);
5280 if(sts != Ok)
5281 return sts;
5283 list_add_head(&graphics->containers, &container->entry);
5284 *state = graphics->contid = container->contid;
5286 return Ok;
5289 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5291 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5292 return NotImplemented;
5295 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5297 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5298 return NotImplemented;
5301 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5303 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5304 return NotImplemented;
5307 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5309 GpStatus sts;
5310 GraphicsContainerItem *container, *container2;
5312 TRACE("(%p, %x)\n", graphics, state);
5314 if(!graphics)
5315 return InvalidParameter;
5317 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5318 if(container->contid == state)
5319 break;
5322 /* did not find a matching container */
5323 if(&container->entry == &graphics->containers)
5324 return Ok;
5326 sts = restore_container(graphics, container);
5327 if(sts != Ok)
5328 return sts;
5330 /* remove all of the containers on top of the found container */
5331 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5332 if(container->contid == state)
5333 break;
5334 list_remove(&container->entry);
5335 delete_container(container);
5338 list_remove(&container->entry);
5339 delete_container(container);
5341 return Ok;
5344 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5345 REAL sy, GpMatrixOrder order)
5347 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5349 if(!graphics)
5350 return InvalidParameter;
5352 if(graphics->busy)
5353 return ObjectBusy;
5355 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5358 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5359 CombineMode mode)
5361 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5363 if(!graphics || !srcgraphics)
5364 return InvalidParameter;
5366 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5369 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5370 CompositingMode mode)
5372 TRACE("(%p, %d)\n", graphics, mode);
5374 if(!graphics)
5375 return InvalidParameter;
5377 if(graphics->busy)
5378 return ObjectBusy;
5380 graphics->compmode = mode;
5382 return Ok;
5385 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5386 CompositingQuality quality)
5388 TRACE("(%p, %d)\n", graphics, quality);
5390 if(!graphics)
5391 return InvalidParameter;
5393 if(graphics->busy)
5394 return ObjectBusy;
5396 graphics->compqual = quality;
5398 return Ok;
5401 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5402 InterpolationMode mode)
5404 TRACE("(%p, %d)\n", graphics, mode);
5406 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5407 return InvalidParameter;
5409 if(graphics->busy)
5410 return ObjectBusy;
5412 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5413 mode = InterpolationModeBilinear;
5415 if (mode == InterpolationModeHighQuality)
5416 mode = InterpolationModeHighQualityBicubic;
5418 graphics->interpolation = mode;
5420 return Ok;
5423 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5425 TRACE("(%p, %.2f)\n", graphics, scale);
5427 if(!graphics || (scale <= 0.0))
5428 return InvalidParameter;
5430 if(graphics->busy)
5431 return ObjectBusy;
5433 graphics->scale = scale;
5435 return Ok;
5438 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5440 TRACE("(%p, %d)\n", graphics, unit);
5442 if(!graphics)
5443 return InvalidParameter;
5445 if(graphics->busy)
5446 return ObjectBusy;
5448 if(unit == UnitWorld)
5449 return InvalidParameter;
5451 graphics->unit = unit;
5453 return Ok;
5456 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5457 mode)
5459 TRACE("(%p, %d)\n", graphics, mode);
5461 if(!graphics)
5462 return InvalidParameter;
5464 if(graphics->busy)
5465 return ObjectBusy;
5467 graphics->pixeloffset = mode;
5469 return Ok;
5472 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5474 static int calls;
5476 TRACE("(%p,%i,%i)\n", graphics, x, y);
5478 if (!(calls++))
5479 FIXME("value is unused in rendering\n");
5481 if (!graphics)
5482 return InvalidParameter;
5484 graphics->origin_x = x;
5485 graphics->origin_y = y;
5487 return Ok;
5490 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5492 TRACE("(%p,%p,%p)\n", graphics, x, y);
5494 if (!graphics || !x || !y)
5495 return InvalidParameter;
5497 *x = graphics->origin_x;
5498 *y = graphics->origin_y;
5500 return Ok;
5503 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5505 TRACE("(%p, %d)\n", graphics, mode);
5507 if(!graphics)
5508 return InvalidParameter;
5510 if(graphics->busy)
5511 return ObjectBusy;
5513 graphics->smoothing = mode;
5515 return Ok;
5518 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5520 TRACE("(%p, %d)\n", graphics, contrast);
5522 if(!graphics)
5523 return InvalidParameter;
5525 graphics->textcontrast = contrast;
5527 return Ok;
5530 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5531 TextRenderingHint hint)
5533 TRACE("(%p, %d)\n", graphics, hint);
5535 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5536 return InvalidParameter;
5538 if(graphics->busy)
5539 return ObjectBusy;
5541 graphics->texthint = hint;
5543 return Ok;
5546 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5548 TRACE("(%p, %p)\n", graphics, matrix);
5550 if(!graphics || !matrix)
5551 return InvalidParameter;
5553 if(graphics->busy)
5554 return ObjectBusy;
5556 TRACE("%f,%f,%f,%f,%f,%f\n",
5557 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5558 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5560 graphics->worldtrans = *matrix;
5562 return Ok;
5565 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5566 REAL dy, GpMatrixOrder order)
5568 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5570 if(!graphics)
5571 return InvalidParameter;
5573 if(graphics->busy)
5574 return ObjectBusy;
5576 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5579 /*****************************************************************************
5580 * GdipSetClipHrgn [GDIPLUS.@]
5582 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5584 GpRegion *region;
5585 GpStatus status;
5587 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5589 if(!graphics)
5590 return InvalidParameter;
5592 status = GdipCreateRegionHrgn(hrgn, &region);
5593 if(status != Ok)
5594 return status;
5596 status = GdipSetClipRegion(graphics, region, mode);
5598 GdipDeleteRegion(region);
5599 return status;
5602 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5604 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5606 if(!graphics)
5607 return InvalidParameter;
5609 if(graphics->busy)
5610 return ObjectBusy;
5612 return GdipCombineRegionPath(graphics->clip, path, mode);
5615 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5616 REAL width, REAL height,
5617 CombineMode mode)
5619 GpRectF rect;
5621 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5623 if(!graphics)
5624 return InvalidParameter;
5626 if(graphics->busy)
5627 return ObjectBusy;
5629 rect.X = x;
5630 rect.Y = y;
5631 rect.Width = width;
5632 rect.Height = height;
5634 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5637 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5638 INT width, INT height,
5639 CombineMode mode)
5641 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5643 if(!graphics)
5644 return InvalidParameter;
5646 if(graphics->busy)
5647 return ObjectBusy;
5649 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5652 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5653 CombineMode mode)
5655 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5657 if(!graphics || !region)
5658 return InvalidParameter;
5660 if(graphics->busy)
5661 return ObjectBusy;
5663 return GdipCombineRegionRegion(graphics->clip, region, mode);
5666 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5667 UINT limitDpi)
5669 static int calls;
5671 TRACE("(%p,%u)\n", metafile, limitDpi);
5673 if(!(calls++))
5674 FIXME("not implemented\n");
5676 return NotImplemented;
5679 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5680 INT count)
5682 INT save_state;
5683 POINT *pti;
5685 TRACE("(%p, %p, %d)\n", graphics, points, count);
5687 if(!graphics || !pen || count<=0)
5688 return InvalidParameter;
5690 if(graphics->busy)
5691 return ObjectBusy;
5693 if (!graphics->hdc)
5695 FIXME("graphics object has no HDC\n");
5696 return Ok;
5699 pti = GdipAlloc(sizeof(POINT) * count);
5701 save_state = prepare_dc(graphics, pen);
5702 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5704 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5705 Polygon(graphics->hdc, pti, count);
5707 restore_dc(graphics, save_state);
5708 GdipFree(pti);
5710 return Ok;
5713 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5714 INT count)
5716 GpStatus ret;
5717 GpPointF *ptf;
5718 INT i;
5720 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5722 if(count<=0) return InvalidParameter;
5723 ptf = GdipAlloc(sizeof(GpPointF) * count);
5725 for(i = 0;i < count; i++){
5726 ptf[i].X = (REAL)points[i].X;
5727 ptf[i].Y = (REAL)points[i].Y;
5730 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5731 GdipFree(ptf);
5733 return ret;
5736 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5738 TRACE("(%p, %p)\n", graphics, dpi);
5740 if(!graphics || !dpi)
5741 return InvalidParameter;
5743 if(graphics->busy)
5744 return ObjectBusy;
5746 *dpi = graphics->xres;
5747 return Ok;
5750 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5752 TRACE("(%p, %p)\n", graphics, dpi);
5754 if(!graphics || !dpi)
5755 return InvalidParameter;
5757 if(graphics->busy)
5758 return ObjectBusy;
5760 *dpi = graphics->yres;
5761 return Ok;
5764 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5765 GpMatrixOrder order)
5767 GpMatrix m;
5768 GpStatus ret;
5770 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5772 if(!graphics || !matrix)
5773 return InvalidParameter;
5775 if(graphics->busy)
5776 return ObjectBusy;
5778 m = graphics->worldtrans;
5780 ret = GdipMultiplyMatrix(&m, matrix, order);
5781 if(ret == Ok)
5782 graphics->worldtrans = m;
5784 return ret;
5787 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5788 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5790 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5792 GpStatus stat=Ok;
5794 TRACE("(%p, %p)\n", graphics, hdc);
5796 if(!graphics || !hdc)
5797 return InvalidParameter;
5799 if(graphics->busy)
5800 return ObjectBusy;
5802 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5804 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5806 else if (!graphics->hdc || graphics->alpha_hdc ||
5807 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5809 /* Create a fake HDC and fill it with a constant color. */
5810 HDC temp_hdc;
5811 HBITMAP hbitmap;
5812 GpRectF bounds;
5813 BITMAPINFOHEADER bmih;
5814 int i;
5816 stat = get_graphics_bounds(graphics, &bounds);
5817 if (stat != Ok)
5818 return stat;
5820 graphics->temp_hbitmap_width = bounds.Width;
5821 graphics->temp_hbitmap_height = bounds.Height;
5823 bmih.biSize = sizeof(bmih);
5824 bmih.biWidth = graphics->temp_hbitmap_width;
5825 bmih.biHeight = -graphics->temp_hbitmap_height;
5826 bmih.biPlanes = 1;
5827 bmih.biBitCount = 32;
5828 bmih.biCompression = BI_RGB;
5829 bmih.biSizeImage = 0;
5830 bmih.biXPelsPerMeter = 0;
5831 bmih.biYPelsPerMeter = 0;
5832 bmih.biClrUsed = 0;
5833 bmih.biClrImportant = 0;
5835 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5836 (void**)&graphics->temp_bits, NULL, 0);
5837 if (!hbitmap)
5838 return GenericError;
5840 temp_hdc = CreateCompatibleDC(0);
5841 if (!temp_hdc)
5843 DeleteObject(hbitmap);
5844 return GenericError;
5847 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5848 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5850 SelectObject(temp_hdc, hbitmap);
5852 graphics->temp_hbitmap = hbitmap;
5853 *hdc = graphics->temp_hdc = temp_hdc;
5855 else
5857 *hdc = graphics->hdc;
5860 if (stat == Ok)
5861 graphics->busy = TRUE;
5863 return stat;
5866 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5868 GpStatus stat=Ok;
5870 TRACE("(%p, %p)\n", graphics, hdc);
5872 if(!graphics || !hdc || !graphics->busy)
5873 return InvalidParameter;
5875 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5877 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5879 else if (graphics->temp_hdc == hdc)
5881 DWORD* pos;
5882 int i;
5884 /* Find the pixels that have changed, and mark them as opaque. */
5885 pos = (DWORD*)graphics->temp_bits;
5886 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5888 if (*pos != DC_BACKGROUND_KEY)
5890 *pos |= 0xff000000;
5892 pos++;
5895 /* Write the changed pixels to the real target. */
5896 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5897 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5898 graphics->temp_hbitmap_width * 4);
5900 /* Clean up. */
5901 DeleteDC(graphics->temp_hdc);
5902 DeleteObject(graphics->temp_hbitmap);
5903 graphics->temp_hdc = NULL;
5904 graphics->temp_hbitmap = NULL;
5906 else if (hdc != graphics->hdc)
5908 stat = InvalidParameter;
5911 if (stat == Ok)
5912 graphics->busy = FALSE;
5914 return stat;
5917 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5919 GpRegion *clip;
5920 GpStatus status;
5922 TRACE("(%p, %p)\n", graphics, region);
5924 if(!graphics || !region)
5925 return InvalidParameter;
5927 if(graphics->busy)
5928 return ObjectBusy;
5930 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5931 return status;
5933 /* free everything except root node and header */
5934 delete_element(&region->node);
5935 memcpy(region, clip, sizeof(GpRegion));
5936 GdipFree(clip);
5938 return Ok;
5941 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5942 GpCoordinateSpace src_space, GpMatrix *matrix)
5944 GpStatus stat = Ok;
5945 REAL scale_x, scale_y;
5947 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5949 if (dst_space != src_space)
5951 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5952 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5954 if(graphics->unit != UnitDisplay)
5956 scale_x *= graphics->scale;
5957 scale_y *= graphics->scale;
5960 /* transform from src_space to CoordinateSpacePage */
5961 switch (src_space)
5963 case CoordinateSpaceWorld:
5964 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5965 break;
5966 case CoordinateSpacePage:
5967 break;
5968 case CoordinateSpaceDevice:
5969 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5970 break;
5973 /* transform from CoordinateSpacePage to dst_space */
5974 switch (dst_space)
5976 case CoordinateSpaceWorld:
5978 GpMatrix inverted_transform = graphics->worldtrans;
5979 stat = GdipInvertMatrix(&inverted_transform);
5980 if (stat == Ok)
5981 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5982 break;
5984 case CoordinateSpacePage:
5985 break;
5986 case CoordinateSpaceDevice:
5987 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5988 break;
5991 return stat;
5994 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5995 GpCoordinateSpace src_space, GpPointF *points, INT count)
5997 GpMatrix matrix;
5998 GpStatus stat;
6000 if(!graphics || !points || count <= 0)
6001 return InvalidParameter;
6003 if(graphics->busy)
6004 return ObjectBusy;
6006 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6008 if (src_space == dst_space) return Ok;
6010 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6011 if (stat != Ok) return stat;
6013 return GdipTransformMatrixPoints(&matrix, points, count);
6016 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6017 GpCoordinateSpace src_space, GpPoint *points, INT count)
6019 GpPointF *pointsF;
6020 GpStatus ret;
6021 INT i;
6023 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6025 if(count <= 0)
6026 return InvalidParameter;
6028 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6029 if(!pointsF)
6030 return OutOfMemory;
6032 for(i = 0; i < count; i++){
6033 pointsF[i].X = (REAL)points[i].X;
6034 pointsF[i].Y = (REAL)points[i].Y;
6037 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6039 if(ret == Ok)
6040 for(i = 0; i < count; i++){
6041 points[i].X = gdip_round(pointsF[i].X);
6042 points[i].Y = gdip_round(pointsF[i].Y);
6044 GdipFree(pointsF);
6046 return ret;
6049 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6051 static int calls;
6053 TRACE("\n");
6055 if (!calls++)
6056 FIXME("stub\n");
6058 return NULL;
6061 /*****************************************************************************
6062 * GdipTranslateClip [GDIPLUS.@]
6064 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6066 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6068 if(!graphics)
6069 return InvalidParameter;
6071 if(graphics->busy)
6072 return ObjectBusy;
6074 return GdipTranslateRegion(graphics->clip, dx, dy);
6077 /*****************************************************************************
6078 * GdipTranslateClipI [GDIPLUS.@]
6080 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6082 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6084 if(!graphics)
6085 return InvalidParameter;
6087 if(graphics->busy)
6088 return ObjectBusy;
6090 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6094 /*****************************************************************************
6095 * GdipMeasureDriverString [GDIPLUS.@]
6097 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6098 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6099 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6101 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6102 HFONT hfont;
6103 HDC hdc;
6104 REAL min_x, min_y, max_x, max_y, x, y;
6105 int i;
6106 TEXTMETRICW textmetric;
6107 const WORD *glyph_indices;
6108 WORD *dynamic_glyph_indices=NULL;
6109 REAL rel_width, rel_height, ascent, descent;
6110 GpPointF pt[3];
6112 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6114 if (!graphics || !text || !font || !positions || !boundingBox)
6115 return InvalidParameter;
6117 if (length == -1)
6118 length = strlenW(text);
6120 if (length == 0)
6122 boundingBox->X = 0.0;
6123 boundingBox->Y = 0.0;
6124 boundingBox->Width = 0.0;
6125 boundingBox->Height = 0.0;
6128 if (flags & unsupported_flags)
6129 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6131 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6133 hdc = CreateCompatibleDC(0);
6134 SelectObject(hdc, hfont);
6136 GetTextMetricsW(hdc, &textmetric);
6138 pt[0].X = 0.0;
6139 pt[0].Y = 0.0;
6140 pt[1].X = 1.0;
6141 pt[1].Y = 0.0;
6142 pt[2].X = 0.0;
6143 pt[2].Y = 1.0;
6144 if (matrix)
6146 GpMatrix xform = *matrix;
6147 GdipTransformMatrixPoints(&xform, pt, 3);
6149 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6150 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6151 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6152 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6153 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6155 if (flags & DriverStringOptionsCmapLookup)
6157 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6158 if (!glyph_indices)
6160 DeleteDC(hdc);
6161 DeleteObject(hfont);
6162 return OutOfMemory;
6165 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6167 else
6168 glyph_indices = text;
6170 min_x = max_x = x = positions[0].X;
6171 min_y = max_y = y = positions[0].Y;
6173 ascent = textmetric.tmAscent / rel_height;
6174 descent = textmetric.tmDescent / rel_height;
6176 for (i=0; i<length; i++)
6178 int char_width;
6179 ABC abc;
6181 if (!(flags & DriverStringOptionsRealizedAdvance))
6183 x = positions[i].X;
6184 y = positions[i].Y;
6187 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6188 char_width = abc.abcA + abc.abcB + abc.abcC;
6190 if (min_y > y - ascent) min_y = y - ascent;
6191 if (max_y < y + descent) max_y = y + descent;
6192 if (min_x > x) min_x = x;
6194 x += char_width / rel_width;
6196 if (max_x < x) max_x = x;
6199 GdipFree(dynamic_glyph_indices);
6200 DeleteDC(hdc);
6201 DeleteObject(hfont);
6203 boundingBox->X = min_x;
6204 boundingBox->Y = min_y;
6205 boundingBox->Width = max_x - min_x;
6206 boundingBox->Height = max_y - min_y;
6208 return Ok;
6211 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6212 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6213 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6214 INT flags, GDIPCONST GpMatrix *matrix)
6216 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6217 INT save_state;
6218 GpPointF pt;
6219 HFONT hfont;
6220 UINT eto_flags=0;
6222 if (flags & unsupported_flags)
6223 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6225 if (!(flags & DriverStringOptionsCmapLookup))
6226 eto_flags |= ETO_GLYPH_INDEX;
6228 save_state = SaveDC(graphics->hdc);
6229 SetBkMode(graphics->hdc, TRANSPARENT);
6230 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6232 pt = positions[0];
6233 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6235 get_font_hfont(graphics, font, format, &hfont, matrix);
6236 SelectObject(graphics->hdc, hfont);
6238 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6240 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6242 RestoreDC(graphics->hdc, save_state);
6244 DeleteObject(hfont);
6246 return Ok;
6249 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6250 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6251 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6252 INT flags, GDIPCONST GpMatrix *matrix)
6254 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6255 GpStatus stat;
6256 PointF *real_positions, real_position;
6257 POINT *pti;
6258 HFONT hfont;
6259 HDC hdc;
6260 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6261 DWORD max_glyphsize=0;
6262 GLYPHMETRICS glyphmetrics;
6263 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6264 BYTE *glyph_mask;
6265 BYTE *text_mask;
6266 int text_mask_stride;
6267 BYTE *pixel_data;
6268 int pixel_data_stride;
6269 GpRect pixel_area;
6270 UINT ggo_flags = GGO_GRAY8_BITMAP;
6272 if (length <= 0)
6273 return Ok;
6275 if (!(flags & DriverStringOptionsCmapLookup))
6276 ggo_flags |= GGO_GLYPH_INDEX;
6278 if (flags & unsupported_flags)
6279 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6281 pti = GdipAlloc(sizeof(POINT) * length);
6282 if (!pti)
6283 return OutOfMemory;
6285 if (flags & DriverStringOptionsRealizedAdvance)
6287 real_position = positions[0];
6289 transform_and_round_points(graphics, pti, &real_position, 1);
6291 else
6293 real_positions = GdipAlloc(sizeof(PointF) * length);
6294 if (!real_positions)
6296 GdipFree(pti);
6297 return OutOfMemory;
6300 memcpy(real_positions, positions, sizeof(PointF) * length);
6302 transform_and_round_points(graphics, pti, real_positions, length);
6304 GdipFree(real_positions);
6307 get_font_hfont(graphics, font, format, &hfont, matrix);
6309 hdc = CreateCompatibleDC(0);
6310 SelectObject(hdc, hfont);
6312 /* Get the boundaries of the text to be drawn */
6313 for (i=0; i<length; i++)
6315 DWORD glyphsize;
6316 int left, top, right, bottom;
6318 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6319 &glyphmetrics, 0, NULL, &identity);
6321 if (glyphsize == GDI_ERROR)
6323 ERR("GetGlyphOutlineW failed\n");
6324 GdipFree(pti);
6325 DeleteDC(hdc);
6326 DeleteObject(hfont);
6327 return GenericError;
6330 if (glyphsize > max_glyphsize)
6331 max_glyphsize = glyphsize;
6333 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6334 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6335 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6336 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6338 if (left < min_x) min_x = left;
6339 if (top < min_y) min_y = top;
6340 if (right > max_x) max_x = right;
6341 if (bottom > max_y) max_y = bottom;
6343 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6345 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6346 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6350 glyph_mask = GdipAlloc(max_glyphsize);
6351 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6352 text_mask_stride = max_x - min_x;
6354 if (!(glyph_mask && text_mask))
6356 GdipFree(glyph_mask);
6357 GdipFree(text_mask);
6358 GdipFree(pti);
6359 DeleteDC(hdc);
6360 DeleteObject(hfont);
6361 return OutOfMemory;
6364 /* Generate a mask for the text */
6365 for (i=0; i<length; i++)
6367 int left, top, stride;
6369 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6370 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6372 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6373 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6374 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6376 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6378 BYTE *glyph_val = glyph_mask + y * stride;
6379 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6380 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6382 *text_val = min(64, *text_val + *glyph_val);
6383 glyph_val++;
6384 text_val++;
6389 GdipFree(pti);
6390 DeleteDC(hdc);
6391 DeleteObject(hfont);
6392 GdipFree(glyph_mask);
6394 /* get the brush data */
6395 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6396 if (!pixel_data)
6398 GdipFree(text_mask);
6399 return OutOfMemory;
6402 pixel_area.X = min_x;
6403 pixel_area.Y = min_y;
6404 pixel_area.Width = max_x - min_x;
6405 pixel_area.Height = max_y - min_y;
6406 pixel_data_stride = pixel_area.Width * 4;
6408 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6409 if (stat != Ok)
6411 GdipFree(text_mask);
6412 GdipFree(pixel_data);
6413 return stat;
6416 /* multiply the brush data by the mask */
6417 for (y=0; y<pixel_area.Height; y++)
6419 BYTE *text_val = text_mask + text_mask_stride * y;
6420 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6421 for (x=0; x<pixel_area.Width; x++)
6423 *pixel_val = (*pixel_val) * (*text_val) / 64;
6424 text_val++;
6425 pixel_val+=4;
6429 GdipFree(text_mask);
6431 /* draw the result */
6432 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6433 pixel_area.Height, pixel_data_stride);
6435 GdipFree(pixel_data);
6437 return stat;
6440 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6441 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6442 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6443 INT flags, GDIPCONST GpMatrix *matrix)
6445 GpStatus stat = NotImplemented;
6447 if (length == -1)
6448 length = strlenW(text);
6450 if (graphics->hdc && !graphics->alpha_hdc &&
6451 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6452 brush->bt == BrushTypeSolidColor &&
6453 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6454 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6455 brush, positions, flags, matrix);
6456 if (stat == NotImplemented)
6457 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6458 brush, positions, flags, matrix);
6459 return stat;
6462 /*****************************************************************************
6463 * GdipDrawDriverString [GDIPLUS.@]
6465 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6466 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6467 GDIPCONST PointF *positions, INT flags,
6468 GDIPCONST GpMatrix *matrix )
6470 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6472 if (!graphics || !text || !font || !brush || !positions)
6473 return InvalidParameter;
6475 return draw_driver_string(graphics, text, length, font, NULL,
6476 brush, positions, flags, matrix);
6479 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6480 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6482 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6483 return NotImplemented;
6486 /*****************************************************************************
6487 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6489 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6491 GpStatus stat;
6492 GpRegion* rgn;
6494 TRACE("(%p, %p)\n", graphics, res);
6496 if((stat = GdipCreateRegion(&rgn)) != Ok)
6497 return stat;
6499 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6500 goto cleanup;
6502 stat = GdipIsEmptyRegion(rgn, graphics, res);
6504 cleanup:
6505 GdipDeleteRegion(rgn);
6506 return stat;
6509 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6511 static int calls;
6513 TRACE("(%p) stub\n", graphics);
6515 if(!(calls++))
6516 FIXME("not implemented\n");
6518 return NotImplemented;