gdiplus: Forward GdipDrawLinesI to GdipDrawLines.
[wine/testsucceed.git] / dlls / gdiplus / graphics.c
blobb9aa912664ffb7160795f08177d97b3253e0dc19
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 /* Draws lines between the given points, and if caps is true then draws an endcap
1717 * at the end of the last line. */
1718 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1719 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1721 POINT *pti = NULL;
1722 GpPointF *ptcopy = NULL;
1723 GpStatus status = GenericError;
1725 if(!count)
1726 return Ok;
1728 pti = GdipAlloc(count * sizeof(POINT));
1729 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1731 if(!pti || !ptcopy){
1732 status = OutOfMemory;
1733 goto end;
1736 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1738 if(caps){
1739 if(pen->endcap == LineCapArrowAnchor)
1740 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1741 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1742 else if((pen->endcap == LineCapCustom) && pen->customend)
1743 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1744 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1745 pen->customend->inset * pen->width);
1747 if(pen->startcap == LineCapArrowAnchor)
1748 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1749 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1750 else if((pen->startcap == LineCapCustom) && pen->customstart)
1751 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1752 &ptcopy[0].X, &ptcopy[0].Y,
1753 pen->customstart->inset * pen->width);
1755 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1756 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1757 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1758 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1761 transform_and_round_points(graphics, pti, ptcopy, count);
1763 if(Polyline(graphics->hdc, pti, count))
1764 status = Ok;
1766 end:
1767 GdipFree(pti);
1768 GdipFree(ptcopy);
1770 return status;
1773 /* Conducts a linear search to find the bezier points that will back off
1774 * the endpoint of the curve by a distance of amt. Linear search works
1775 * better than binary in this case because there are multiple solutions,
1776 * and binary searches often find a bad one. I don't think this is what
1777 * Windows does but short of rendering the bezier without GDI's help it's
1778 * the best we can do. If rev then work from the start of the passed points
1779 * instead of the end. */
1780 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1782 GpPointF origpt[4];
1783 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1784 INT i, first = 0, second = 1, third = 2, fourth = 3;
1786 if(rev){
1787 first = 3;
1788 second = 2;
1789 third = 1;
1790 fourth = 0;
1793 origx = pt[fourth].X;
1794 origy = pt[fourth].Y;
1795 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1797 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1798 /* reset bezier points to original values */
1799 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1800 /* Perform magic on bezier points. Order is important here.*/
1801 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1802 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1803 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1804 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1805 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1806 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1808 dx = pt[fourth].X - origx;
1809 dy = pt[fourth].Y - origy;
1811 diff = sqrt(dx * dx + dy * dy);
1812 percent += 0.0005 * amt;
1816 /* Draws bezier curves between given points, and if caps is true then draws an
1817 * endcap at the end of the last line. */
1818 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1819 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1821 POINT *pti;
1822 GpPointF *ptcopy;
1823 GpStatus status = GenericError;
1825 if(!count)
1826 return Ok;
1828 pti = GdipAlloc(count * sizeof(POINT));
1829 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1831 if(!pti || !ptcopy){
1832 status = OutOfMemory;
1833 goto end;
1836 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1838 if(caps){
1839 if(pen->endcap == LineCapArrowAnchor)
1840 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1841 else if((pen->endcap == LineCapCustom) && pen->customend)
1842 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1843 FALSE);
1845 if(pen->startcap == LineCapArrowAnchor)
1846 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1847 else if((pen->startcap == LineCapCustom) && pen->customstart)
1848 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1850 /* the direction of the line cap is parallel to the direction at the
1851 * end of the bezier (which, if it has been shortened, is not the same
1852 * as the direction from pt[count-2] to pt[count-1]) */
1853 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1854 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1855 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1856 pt[count - 1].X, pt[count - 1].Y);
1858 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1859 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1860 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1863 transform_and_round_points(graphics, pti, ptcopy, count);
1865 PolyBezier(graphics->hdc, pti, count);
1867 status = Ok;
1869 end:
1870 GdipFree(pti);
1871 GdipFree(ptcopy);
1873 return status;
1876 /* Draws a combination of bezier curves and lines between points. */
1877 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1878 GDIPCONST BYTE * types, INT count, BOOL caps)
1880 POINT *pti = GdipAlloc(count * sizeof(POINT));
1881 BYTE *tp = GdipAlloc(count);
1882 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1883 INT i, j;
1884 GpStatus status = GenericError;
1886 if(!count){
1887 status = Ok;
1888 goto end;
1890 if(!pti || !tp || !ptcopy){
1891 status = OutOfMemory;
1892 goto end;
1895 for(i = 1; i < count; i++){
1896 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1897 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1898 || !(types[i + 1] & PathPointTypeBezier)){
1899 ERR("Bad bezier points\n");
1900 goto end;
1902 i += 2;
1906 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1908 /* If we are drawing caps, go through the points and adjust them accordingly,
1909 * and draw the caps. */
1910 if(caps){
1911 switch(types[count - 1] & PathPointTypePathTypeMask){
1912 case PathPointTypeBezier:
1913 if(pen->endcap == LineCapArrowAnchor)
1914 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1915 else if((pen->endcap == LineCapCustom) && pen->customend)
1916 shorten_bezier_amt(&ptcopy[count - 4],
1917 pen->width * pen->customend->inset, FALSE);
1919 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1920 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1921 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1922 pt[count - 1].X, pt[count - 1].Y);
1924 break;
1925 case PathPointTypeLine:
1926 if(pen->endcap == LineCapArrowAnchor)
1927 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1928 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1929 pen->width);
1930 else if((pen->endcap == LineCapCustom) && pen->customend)
1931 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1932 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1933 pen->customend->inset * pen->width);
1935 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1936 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1937 pt[count - 1].Y);
1939 break;
1940 default:
1941 ERR("Bad path last point\n");
1942 goto end;
1945 /* Find start of points */
1946 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1947 == PathPointTypeStart); j++);
1949 switch(types[j] & PathPointTypePathTypeMask){
1950 case PathPointTypeBezier:
1951 if(pen->startcap == LineCapArrowAnchor)
1952 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1953 else if((pen->startcap == LineCapCustom) && pen->customstart)
1954 shorten_bezier_amt(&ptcopy[j - 1],
1955 pen->width * pen->customstart->inset, TRUE);
1957 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1958 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1959 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1960 pt[j - 1].X, pt[j - 1].Y);
1962 break;
1963 case PathPointTypeLine:
1964 if(pen->startcap == LineCapArrowAnchor)
1965 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1966 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1967 pen->width);
1968 else if((pen->startcap == LineCapCustom) && pen->customstart)
1969 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1970 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1971 pen->customstart->inset * pen->width);
1973 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1974 pt[j].X, pt[j].Y, pt[j - 1].X,
1975 pt[j - 1].Y);
1977 break;
1978 default:
1979 ERR("Bad path points\n");
1980 goto end;
1984 transform_and_round_points(graphics, pti, ptcopy, count);
1986 for(i = 0; i < count; i++){
1987 tp[i] = convert_path_point_type(types[i]);
1990 PolyDraw(graphics->hdc, pti, tp, count);
1992 status = Ok;
1994 end:
1995 GdipFree(pti);
1996 GdipFree(ptcopy);
1997 GdipFree(tp);
1999 return status;
2002 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2004 GpStatus result;
2006 BeginPath(graphics->hdc);
2007 result = draw_poly(graphics, NULL, path->pathdata.Points,
2008 path->pathdata.Types, path->pathdata.Count, FALSE);
2009 EndPath(graphics->hdc);
2010 return result;
2013 typedef struct _GraphicsContainerItem {
2014 struct list entry;
2015 GraphicsContainer contid;
2017 SmoothingMode smoothing;
2018 CompositingQuality compqual;
2019 InterpolationMode interpolation;
2020 CompositingMode compmode;
2021 TextRenderingHint texthint;
2022 REAL scale;
2023 GpUnit unit;
2024 PixelOffsetMode pixeloffset;
2025 UINT textcontrast;
2026 GpMatrix worldtrans;
2027 GpRegion* clip;
2028 INT origin_x, origin_y;
2029 } GraphicsContainerItem;
2031 static GpStatus init_container(GraphicsContainerItem** container,
2032 GDIPCONST GpGraphics* graphics){
2033 GpStatus sts;
2035 *container = GdipAlloc(sizeof(GraphicsContainerItem));
2036 if(!(*container))
2037 return OutOfMemory;
2039 (*container)->contid = graphics->contid + 1;
2041 (*container)->smoothing = graphics->smoothing;
2042 (*container)->compqual = graphics->compqual;
2043 (*container)->interpolation = graphics->interpolation;
2044 (*container)->compmode = graphics->compmode;
2045 (*container)->texthint = graphics->texthint;
2046 (*container)->scale = graphics->scale;
2047 (*container)->unit = graphics->unit;
2048 (*container)->textcontrast = graphics->textcontrast;
2049 (*container)->pixeloffset = graphics->pixeloffset;
2050 (*container)->origin_x = graphics->origin_x;
2051 (*container)->origin_y = graphics->origin_y;
2052 (*container)->worldtrans = graphics->worldtrans;
2054 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2055 if(sts != Ok){
2056 GdipFree(*container);
2057 *container = NULL;
2058 return sts;
2061 return Ok;
2064 static void delete_container(GraphicsContainerItem* container)
2066 GdipDeleteRegion(container->clip);
2067 GdipFree(container);
2070 static GpStatus restore_container(GpGraphics* graphics,
2071 GDIPCONST GraphicsContainerItem* container){
2072 GpStatus sts;
2073 GpRegion *newClip;
2075 sts = GdipCloneRegion(container->clip, &newClip);
2076 if(sts != Ok) return sts;
2078 graphics->worldtrans = container->worldtrans;
2080 GdipDeleteRegion(graphics->clip);
2081 graphics->clip = newClip;
2083 graphics->contid = container->contid - 1;
2085 graphics->smoothing = container->smoothing;
2086 graphics->compqual = container->compqual;
2087 graphics->interpolation = container->interpolation;
2088 graphics->compmode = container->compmode;
2089 graphics->texthint = container->texthint;
2090 graphics->scale = container->scale;
2091 graphics->unit = container->unit;
2092 graphics->textcontrast = container->textcontrast;
2093 graphics->pixeloffset = container->pixeloffset;
2094 graphics->origin_x = container->origin_x;
2095 graphics->origin_y = container->origin_y;
2097 return Ok;
2100 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2102 RECT wnd_rect;
2103 GpStatus stat=Ok;
2104 GpUnit unit;
2106 if(graphics->hwnd) {
2107 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2108 return GenericError;
2110 rect->X = wnd_rect.left;
2111 rect->Y = wnd_rect.top;
2112 rect->Width = wnd_rect.right - wnd_rect.left;
2113 rect->Height = wnd_rect.bottom - wnd_rect.top;
2114 }else if (graphics->image){
2115 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2116 if (stat == Ok && unit != UnitPixel)
2117 FIXME("need to convert from unit %i\n", unit);
2118 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2119 HBITMAP hbmp;
2120 BITMAP bmp;
2122 rect->X = 0;
2123 rect->Y = 0;
2125 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2126 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2128 rect->Width = bmp.bmWidth;
2129 rect->Height = bmp.bmHeight;
2131 else
2133 /* FIXME: ??? */
2134 rect->Width = 1;
2135 rect->Height = 1;
2137 }else{
2138 rect->X = 0;
2139 rect->Y = 0;
2140 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2141 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2144 return stat;
2147 /* on success, rgn will contain the region of the graphics object which
2148 * is visible after clipping has been applied */
2149 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2151 GpStatus stat;
2152 GpRectF rectf;
2153 GpRegion* tmp;
2155 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2156 return stat;
2158 if((stat = GdipCreateRegion(&tmp)) != Ok)
2159 return stat;
2161 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2162 goto end;
2164 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2165 goto end;
2167 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2169 end:
2170 GdipDeleteRegion(tmp);
2171 return stat;
2174 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2176 REAL height;
2178 if (font->unit == UnitPixel)
2180 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2182 else
2184 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2185 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2186 else
2187 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2190 lf->lfHeight = -(height + 0.5);
2191 lf->lfWidth = 0;
2192 lf->lfEscapement = 0;
2193 lf->lfOrientation = 0;
2194 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2195 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2196 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2197 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2198 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2199 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2200 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2201 lf->lfQuality = DEFAULT_QUALITY;
2202 lf->lfPitchAndFamily = 0;
2203 strcpyW(lf->lfFaceName, font->family->FamilyName);
2206 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2207 GDIPCONST GpStringFormat *format, HFONT *hfont,
2208 GDIPCONST GpMatrix *matrix)
2210 HDC hdc = CreateCompatibleDC(0);
2211 GpPointF pt[3];
2212 REAL angle, rel_width, rel_height, font_height;
2213 LOGFONTW lfw;
2214 HFONT unscaled_font;
2215 TEXTMETRICW textmet;
2217 if (font->unit == UnitPixel)
2218 font_height = font->emSize;
2219 else
2221 REAL unit_scale, res;
2223 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2224 unit_scale = units_scale(font->unit, graphics->unit, res);
2226 font_height = font->emSize * unit_scale;
2229 pt[0].X = 0.0;
2230 pt[0].Y = 0.0;
2231 pt[1].X = 1.0;
2232 pt[1].Y = 0.0;
2233 pt[2].X = 0.0;
2234 pt[2].Y = 1.0;
2235 if (matrix)
2237 GpMatrix xform = *matrix;
2238 GdipTransformMatrixPoints(&xform, pt, 3);
2240 if (graphics)
2241 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2242 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2243 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2244 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2245 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2246 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2248 get_log_fontW(font, graphics, &lfw);
2249 lfw.lfHeight = gdip_round(font_height * rel_height);
2250 unscaled_font = CreateFontIndirectW(&lfw);
2252 SelectObject(hdc, unscaled_font);
2253 GetTextMetricsW(hdc, &textmet);
2255 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2256 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2258 *hfont = CreateFontIndirectW(&lfw);
2260 DeleteDC(hdc);
2261 DeleteObject(unscaled_font);
2264 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2266 TRACE("(%p, %p)\n", hdc, graphics);
2268 return GdipCreateFromHDC2(hdc, NULL, graphics);
2271 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2273 GpStatus retval;
2274 HBITMAP hbitmap;
2275 DIBSECTION dib;
2277 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2279 if(hDevice != NULL)
2280 FIXME("Don't know how to handle parameter hDevice\n");
2282 if(hdc == NULL)
2283 return OutOfMemory;
2285 if(graphics == NULL)
2286 return InvalidParameter;
2288 *graphics = GdipAlloc(sizeof(GpGraphics));
2289 if(!*graphics) return OutOfMemory;
2291 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2293 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2294 GdipFree(*graphics);
2295 return retval;
2298 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2299 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2300 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2302 (*graphics)->alpha_hdc = 1;
2305 (*graphics)->hdc = hdc;
2306 (*graphics)->hwnd = WindowFromDC(hdc);
2307 (*graphics)->owndc = FALSE;
2308 (*graphics)->smoothing = SmoothingModeDefault;
2309 (*graphics)->compqual = CompositingQualityDefault;
2310 (*graphics)->interpolation = InterpolationModeBilinear;
2311 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2312 (*graphics)->compmode = CompositingModeSourceOver;
2313 (*graphics)->unit = UnitDisplay;
2314 (*graphics)->scale = 1.0;
2315 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2316 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2317 (*graphics)->busy = FALSE;
2318 (*graphics)->textcontrast = 4;
2319 list_init(&(*graphics)->containers);
2320 (*graphics)->contid = 0;
2322 TRACE("<-- %p\n", *graphics);
2324 return Ok;
2327 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2329 GpStatus retval;
2331 *graphics = GdipAlloc(sizeof(GpGraphics));
2332 if(!*graphics) return OutOfMemory;
2334 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2336 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2337 GdipFree(*graphics);
2338 return retval;
2341 (*graphics)->hdc = NULL;
2342 (*graphics)->hwnd = NULL;
2343 (*graphics)->owndc = FALSE;
2344 (*graphics)->image = image;
2345 (*graphics)->smoothing = SmoothingModeDefault;
2346 (*graphics)->compqual = CompositingQualityDefault;
2347 (*graphics)->interpolation = InterpolationModeBilinear;
2348 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2349 (*graphics)->compmode = CompositingModeSourceOver;
2350 (*graphics)->unit = UnitDisplay;
2351 (*graphics)->scale = 1.0;
2352 (*graphics)->xres = image->xres;
2353 (*graphics)->yres = image->yres;
2354 (*graphics)->busy = FALSE;
2355 (*graphics)->textcontrast = 4;
2356 list_init(&(*graphics)->containers);
2357 (*graphics)->contid = 0;
2359 TRACE("<-- %p\n", *graphics);
2361 return Ok;
2364 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2366 GpStatus ret;
2367 HDC hdc;
2369 TRACE("(%p, %p)\n", hwnd, graphics);
2371 hdc = GetDC(hwnd);
2373 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2375 ReleaseDC(hwnd, hdc);
2376 return ret;
2379 (*graphics)->hwnd = hwnd;
2380 (*graphics)->owndc = TRUE;
2382 return Ok;
2385 /* FIXME: no icm handling */
2386 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2388 TRACE("(%p, %p)\n", hwnd, graphics);
2390 return GdipCreateFromHWND(hwnd, graphics);
2393 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2394 GpMetafile **metafile)
2396 ENHMETAHEADER header;
2397 MetafileType metafile_type;
2399 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2401 if(!hemf || !metafile)
2402 return InvalidParameter;
2404 if (GetEnhMetaFileHeader(hemf, sizeof(header), &header) == 0)
2405 return GenericError;
2407 metafile_type = METAFILE_GetEmfType(hemf);
2409 if (metafile_type == MetafileTypeInvalid)
2410 return GenericError;
2412 *metafile = GdipAlloc(sizeof(GpMetafile));
2413 if (!*metafile)
2414 return OutOfMemory;
2416 (*metafile)->image.type = ImageTypeMetafile;
2417 (*metafile)->image.format = ImageFormatEMF;
2418 (*metafile)->image.frame_count = 1;
2419 (*metafile)->image.xres = (REAL)header.szlDevice.cx;
2420 (*metafile)->image.yres = (REAL)header.szlDevice.cy;
2421 (*metafile)->bounds.X = (REAL)header.rclBounds.left;
2422 (*metafile)->bounds.Y = (REAL)header.rclBounds.top;
2423 (*metafile)->bounds.Width = (REAL)(header.rclBounds.right - header.rclBounds.left);
2424 (*metafile)->bounds.Height = (REAL)(header.rclBounds.bottom - header.rclBounds.top);
2425 (*metafile)->unit = UnitPixel;
2426 (*metafile)->metafile_type = metafile_type;
2427 (*metafile)->hemf = hemf;
2428 (*metafile)->preserve_hemf = !delete;
2430 TRACE("<-- %p\n", *metafile);
2432 return Ok;
2435 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2436 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2438 UINT read;
2439 BYTE *copy;
2440 HENHMETAFILE hemf;
2441 GpStatus retval = Ok;
2443 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2445 if(!hwmf || !metafile || !placeable)
2446 return InvalidParameter;
2448 *metafile = NULL;
2449 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2450 if(!read)
2451 return GenericError;
2452 copy = GdipAlloc(read);
2453 GetMetaFileBitsEx(hwmf, read, copy);
2455 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2456 GdipFree(copy);
2458 /* FIXME: We should store and use hwmf instead of converting to hemf */
2459 retval = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
2461 if (retval == Ok)
2463 (*metafile)->image.xres = (REAL)placeable->Inch;
2464 (*metafile)->image.yres = (REAL)placeable->Inch;
2465 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2466 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2467 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2468 placeable->BoundingBox.Left);
2469 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2470 placeable->BoundingBox.Top);
2471 (*metafile)->metafile_type = MetafileTypeWmfPlaceable;
2472 (*metafile)->image.format = ImageFormatWMF;
2474 if (delete) DeleteMetaFile(hwmf);
2476 else
2477 DeleteEnhMetaFile(hemf);
2478 return retval;
2481 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2482 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2484 HMETAFILE hmf = GetMetaFileW(file);
2486 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2488 if(!hmf) return InvalidParameter;
2490 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2493 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2494 GpMetafile **metafile)
2496 FIXME("(%p, %p): stub\n", file, metafile);
2497 return NotImplemented;
2500 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2501 GpMetafile **metafile)
2503 FIXME("(%p, %p): stub\n", stream, metafile);
2504 return NotImplemented;
2507 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2508 UINT access, IStream **stream)
2510 DWORD dwMode;
2511 HRESULT ret;
2513 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2515 if(!stream || !filename)
2516 return InvalidParameter;
2518 if(access & GENERIC_WRITE)
2519 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2520 else if(access & GENERIC_READ)
2521 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2522 else
2523 return InvalidParameter;
2525 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2527 return hresult_to_status(ret);
2530 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2532 GraphicsContainerItem *cont, *next;
2533 GpStatus stat;
2534 TRACE("(%p)\n", graphics);
2536 if(!graphics) return InvalidParameter;
2537 if(graphics->busy) return ObjectBusy;
2539 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2541 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2542 if (stat != Ok)
2543 return stat;
2546 if(graphics->owndc)
2547 ReleaseDC(graphics->hwnd, graphics->hdc);
2549 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2550 list_remove(&cont->entry);
2551 delete_container(cont);
2554 GdipDeleteRegion(graphics->clip);
2555 GdipFree(graphics);
2557 return Ok;
2560 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2561 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2563 INT save_state, num_pts;
2564 GpPointF points[MAX_ARC_PTS];
2565 GpStatus retval;
2567 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2568 width, height, startAngle, sweepAngle);
2570 if(!graphics || !pen || width <= 0 || height <= 0)
2571 return InvalidParameter;
2573 if(graphics->busy)
2574 return ObjectBusy;
2576 if (!graphics->hdc)
2578 FIXME("graphics object has no HDC\n");
2579 return Ok;
2582 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2584 save_state = prepare_dc(graphics, pen);
2586 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2588 restore_dc(graphics, save_state);
2590 return retval;
2593 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2594 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2596 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2597 width, height, startAngle, sweepAngle);
2599 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2602 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2603 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2605 INT save_state;
2606 GpPointF pt[4];
2607 GpStatus retval;
2609 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2610 x2, y2, x3, y3, x4, y4);
2612 if(!graphics || !pen)
2613 return InvalidParameter;
2615 if(graphics->busy)
2616 return ObjectBusy;
2618 if (!graphics->hdc)
2620 FIXME("graphics object has no HDC\n");
2621 return Ok;
2624 pt[0].X = x1;
2625 pt[0].Y = y1;
2626 pt[1].X = x2;
2627 pt[1].Y = y2;
2628 pt[2].X = x3;
2629 pt[2].Y = y3;
2630 pt[3].X = x4;
2631 pt[3].Y = y4;
2633 save_state = prepare_dc(graphics, pen);
2635 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2637 restore_dc(graphics, save_state);
2639 return retval;
2642 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2643 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2645 INT save_state;
2646 GpPointF pt[4];
2647 GpStatus retval;
2649 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2650 x2, y2, x3, y3, x4, y4);
2652 if(!graphics || !pen)
2653 return InvalidParameter;
2655 if(graphics->busy)
2656 return ObjectBusy;
2658 if (!graphics->hdc)
2660 FIXME("graphics object has no HDC\n");
2661 return Ok;
2664 pt[0].X = x1;
2665 pt[0].Y = y1;
2666 pt[1].X = x2;
2667 pt[1].Y = y2;
2668 pt[2].X = x3;
2669 pt[2].Y = y3;
2670 pt[3].X = x4;
2671 pt[3].Y = y4;
2673 save_state = prepare_dc(graphics, pen);
2675 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2677 restore_dc(graphics, save_state);
2679 return retval;
2682 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2683 GDIPCONST GpPointF *points, INT count)
2685 INT i;
2686 GpStatus ret;
2688 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2690 if(!graphics || !pen || !points || (count <= 0))
2691 return InvalidParameter;
2693 if(graphics->busy)
2694 return ObjectBusy;
2696 for(i = 0; i < floor(count / 4); i++){
2697 ret = GdipDrawBezier(graphics, pen,
2698 points[4*i].X, points[4*i].Y,
2699 points[4*i + 1].X, points[4*i + 1].Y,
2700 points[4*i + 2].X, points[4*i + 2].Y,
2701 points[4*i + 3].X, points[4*i + 3].Y);
2702 if(ret != Ok)
2703 return ret;
2706 return Ok;
2709 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2710 GDIPCONST GpPoint *points, INT count)
2712 GpPointF *pts;
2713 GpStatus ret;
2714 INT i;
2716 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2718 if(!graphics || !pen || !points || (count <= 0))
2719 return InvalidParameter;
2721 if(graphics->busy)
2722 return ObjectBusy;
2724 pts = GdipAlloc(sizeof(GpPointF) * count);
2725 if(!pts)
2726 return OutOfMemory;
2728 for(i = 0; i < count; i++){
2729 pts[i].X = (REAL)points[i].X;
2730 pts[i].Y = (REAL)points[i].Y;
2733 ret = GdipDrawBeziers(graphics,pen,pts,count);
2735 GdipFree(pts);
2737 return ret;
2740 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2741 GDIPCONST GpPointF *points, INT count)
2743 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2745 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2748 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2749 GDIPCONST GpPoint *points, INT count)
2751 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2753 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2756 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2757 GDIPCONST GpPointF *points, INT count, REAL tension)
2759 GpPath *path;
2760 GpStatus stat;
2762 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2764 if(!graphics || !pen || !points || count <= 0)
2765 return InvalidParameter;
2767 if(graphics->busy)
2768 return ObjectBusy;
2770 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2771 return stat;
2773 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2774 if(stat != Ok){
2775 GdipDeletePath(path);
2776 return stat;
2779 stat = GdipDrawPath(graphics, pen, path);
2781 GdipDeletePath(path);
2783 return stat;
2786 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2787 GDIPCONST GpPoint *points, INT count, REAL tension)
2789 GpPointF *ptf;
2790 GpStatus stat;
2791 INT i;
2793 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2795 if(!points || count <= 0)
2796 return InvalidParameter;
2798 ptf = GdipAlloc(sizeof(GpPointF)*count);
2799 if(!ptf)
2800 return OutOfMemory;
2802 for(i = 0; i < count; i++){
2803 ptf[i].X = (REAL)points[i].X;
2804 ptf[i].Y = (REAL)points[i].Y;
2807 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2809 GdipFree(ptf);
2811 return stat;
2814 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2815 GDIPCONST GpPointF *points, INT count)
2817 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2819 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2822 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2823 GDIPCONST GpPoint *points, INT count)
2825 GpPointF *pointsF;
2826 GpStatus ret;
2827 INT i;
2829 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2831 if(!points)
2832 return InvalidParameter;
2834 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2835 if(!pointsF)
2836 return OutOfMemory;
2838 for(i = 0; i < count; i++){
2839 pointsF[i].X = (REAL)points[i].X;
2840 pointsF[i].Y = (REAL)points[i].Y;
2843 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2844 GdipFree(pointsF);
2846 return ret;
2849 /* Approximates cardinal spline with Bezier curves. */
2850 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2851 GDIPCONST GpPointF *points, INT count, REAL tension)
2853 /* PolyBezier expects count*3-2 points. */
2854 INT i, len_pt = count*3-2, save_state;
2855 GpPointF *pt;
2856 REAL x1, x2, y1, y2;
2857 GpStatus retval;
2859 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2861 if(!graphics || !pen)
2862 return InvalidParameter;
2864 if(graphics->busy)
2865 return ObjectBusy;
2867 if(count < 2)
2868 return InvalidParameter;
2870 if (!graphics->hdc)
2872 FIXME("graphics object has no HDC\n");
2873 return Ok;
2876 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2877 if(!pt)
2878 return OutOfMemory;
2880 tension = tension * TENSION_CONST;
2882 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2883 tension, &x1, &y1);
2885 pt[0].X = points[0].X;
2886 pt[0].Y = points[0].Y;
2887 pt[1].X = x1;
2888 pt[1].Y = y1;
2890 for(i = 0; i < count-2; i++){
2891 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2893 pt[3*i+2].X = x1;
2894 pt[3*i+2].Y = y1;
2895 pt[3*i+3].X = points[i+1].X;
2896 pt[3*i+3].Y = points[i+1].Y;
2897 pt[3*i+4].X = x2;
2898 pt[3*i+4].Y = y2;
2901 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2902 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2904 pt[len_pt-2].X = x1;
2905 pt[len_pt-2].Y = y1;
2906 pt[len_pt-1].X = points[count-1].X;
2907 pt[len_pt-1].Y = points[count-1].Y;
2909 save_state = prepare_dc(graphics, pen);
2911 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2913 GdipFree(pt);
2914 restore_dc(graphics, save_state);
2916 return retval;
2919 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2920 GDIPCONST GpPoint *points, INT count, REAL tension)
2922 GpPointF *pointsF;
2923 GpStatus ret;
2924 INT i;
2926 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2928 if(!points)
2929 return InvalidParameter;
2931 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2932 if(!pointsF)
2933 return OutOfMemory;
2935 for(i = 0; i < count; i++){
2936 pointsF[i].X = (REAL)points[i].X;
2937 pointsF[i].Y = (REAL)points[i].Y;
2940 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2941 GdipFree(pointsF);
2943 return ret;
2946 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2947 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2948 REAL tension)
2950 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2952 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2953 return InvalidParameter;
2956 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2959 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2960 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2961 REAL tension)
2963 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2965 if(count < 0){
2966 return OutOfMemory;
2969 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2970 return InvalidParameter;
2973 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2976 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2977 REAL y, REAL width, REAL height)
2979 INT save_state;
2980 GpPointF ptf[2];
2981 POINT pti[2];
2983 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2985 if(!graphics || !pen)
2986 return InvalidParameter;
2988 if(graphics->busy)
2989 return ObjectBusy;
2991 if (!graphics->hdc)
2993 FIXME("graphics object has no HDC\n");
2994 return Ok;
2997 ptf[0].X = x;
2998 ptf[0].Y = y;
2999 ptf[1].X = x + width;
3000 ptf[1].Y = y + height;
3002 save_state = prepare_dc(graphics, pen);
3003 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3005 transform_and_round_points(graphics, pti, ptf, 2);
3007 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
3009 restore_dc(graphics, save_state);
3011 return Ok;
3014 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
3015 INT y, INT width, INT height)
3017 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3019 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3023 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
3025 UINT width, height;
3027 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
3029 if(!graphics || !image)
3030 return InvalidParameter;
3032 GdipGetImageWidth(image, &width);
3033 GdipGetImageHeight(image, &height);
3035 return GdipDrawImagePointRect(graphics, image, x, y,
3036 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
3039 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
3040 INT y)
3042 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
3044 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
3047 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
3048 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
3049 GpUnit srcUnit)
3051 GpPointF points[3];
3052 REAL scale_x, scale_y, width, height;
3054 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3056 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
3057 scale_x *= graphics->xres / image->xres;
3058 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
3059 scale_y *= graphics->yres / image->yres;
3060 width = srcwidth * scale_x;
3061 height = srcheight * scale_y;
3063 points[0].X = points[2].X = x;
3064 points[0].Y = points[1].Y = y;
3065 points[1].X = x + width;
3066 points[2].Y = y + height;
3068 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3069 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3072 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3073 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3074 GpUnit srcUnit)
3076 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3079 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3080 GDIPCONST GpPointF *dstpoints, INT count)
3082 UINT width, height;
3084 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3086 if(!image)
3087 return InvalidParameter;
3089 GdipGetImageWidth(image, &width);
3090 GdipGetImageHeight(image, &height);
3092 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3093 width, height, UnitPixel, NULL, NULL, NULL);
3096 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3097 GDIPCONST GpPoint *dstpoints, INT count)
3099 GpPointF ptf[3];
3101 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3103 if (count != 3 || !dstpoints)
3104 return InvalidParameter;
3106 ptf[0].X = (REAL)dstpoints[0].X;
3107 ptf[0].Y = (REAL)dstpoints[0].Y;
3108 ptf[1].X = (REAL)dstpoints[1].X;
3109 ptf[1].Y = (REAL)dstpoints[1].Y;
3110 ptf[2].X = (REAL)dstpoints[2].X;
3111 ptf[2].Y = (REAL)dstpoints[2].Y;
3113 return GdipDrawImagePoints(graphics, image, ptf, count);
3116 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3117 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3119 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3120 return TRUE;
3123 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3124 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3125 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3126 DrawImageAbort callback, VOID * callbackData)
3128 GpPointF ptf[4];
3129 POINT pti[4];
3130 GpStatus stat;
3132 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3133 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3134 callbackData);
3136 if (count > 3)
3137 return NotImplemented;
3139 if(!graphics || !image || !points || count != 3)
3140 return InvalidParameter;
3142 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3143 debugstr_pointf(&points[2]));
3145 memcpy(ptf, points, 3 * sizeof(GpPointF));
3146 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3147 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3148 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3149 return Ok;
3150 transform_and_round_points(graphics, pti, ptf, 4);
3152 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3153 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3155 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3156 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3157 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3158 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3159 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3161 if (image->picture)
3163 if (!graphics->hdc)
3165 FIXME("graphics object has no HDC\n");
3168 if(IPicture_Render(image->picture, graphics->hdc,
3169 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3170 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3172 if(callback)
3173 callback(callbackData);
3174 return GenericError;
3177 else if (image->type == ImageTypeBitmap)
3179 GpBitmap* bitmap = (GpBitmap*)image;
3180 int use_software=0;
3182 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3183 graphics->xres, graphics->yres,
3184 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3185 graphics->scale, image->xres, image->yres, bitmap->format,
3186 imageAttributes ? imageAttributes->outside_color : 0);
3188 if (imageAttributes || graphics->alpha_hdc ||
3189 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3190 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3191 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3192 srcx < 0 || srcy < 0 ||
3193 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3194 use_software = 1;
3196 if (use_software)
3198 RECT dst_area;
3199 GpRect src_area;
3200 int i, x, y, src_stride, dst_stride;
3201 GpMatrix dst_to_src;
3202 REAL m11, m12, m21, m22, mdx, mdy;
3203 LPBYTE src_data, dst_data;
3204 BitmapData lockeddata;
3205 InterpolationMode interpolation = graphics->interpolation;
3206 PixelOffsetMode offset_mode = graphics->pixeloffset;
3207 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3208 REAL x_dx, x_dy, y_dx, y_dy;
3209 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3211 if (!imageAttributes)
3212 imageAttributes = &defaultImageAttributes;
3214 dst_area.left = dst_area.right = pti[0].x;
3215 dst_area.top = dst_area.bottom = pti[0].y;
3216 for (i=1; i<4; i++)
3218 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3219 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3220 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3221 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3224 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3226 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3227 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3228 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3229 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3230 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3231 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3233 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3235 stat = GdipInvertMatrix(&dst_to_src);
3236 if (stat != Ok) return stat;
3238 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3239 if (!dst_data) return OutOfMemory;
3241 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3243 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3244 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3246 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3248 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3249 if (!src_data)
3251 GdipFree(dst_data);
3252 return OutOfMemory;
3254 src_stride = sizeof(ARGB) * src_area.Width;
3256 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3257 lockeddata.Width = src_area.Width;
3258 lockeddata.Height = src_area.Height;
3259 lockeddata.Stride = src_stride;
3260 lockeddata.PixelFormat = PixelFormat32bppARGB;
3261 lockeddata.Scan0 = src_data;
3263 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3264 PixelFormat32bppARGB, &lockeddata);
3266 if (stat == Ok)
3267 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3269 if (stat != Ok)
3271 if (src_data != dst_data)
3272 GdipFree(src_data);
3273 GdipFree(dst_data);
3274 return stat;
3277 apply_image_attributes(imageAttributes, src_data,
3278 src_area.Width, src_area.Height,
3279 src_stride, ColorAdjustTypeBitmap);
3281 /* Transform the bits as needed to the destination. */
3282 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3284 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3285 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3286 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3287 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3289 for (x=dst_area.left; x<dst_area.right; x++)
3291 for (y=dst_area.top; y<dst_area.bottom; y++)
3293 GpPointF src_pointf;
3294 ARGB *dst_color;
3296 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3297 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3299 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3301 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3302 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3303 imageAttributes, interpolation, offset_mode);
3304 else
3305 *dst_color = 0;
3309 GdipFree(src_data);
3311 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3312 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3314 GdipFree(dst_data);
3316 return stat;
3318 else
3320 HDC hdc;
3321 int temp_hdc=0, temp_bitmap=0;
3322 HBITMAP hbitmap, old_hbm=NULL;
3324 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3325 bitmap->format == PixelFormat24bppRGB ||
3326 bitmap->format == PixelFormat32bppRGB ||
3327 bitmap->format == PixelFormat32bppPARGB))
3329 BITMAPINFOHEADER bih;
3330 BYTE *temp_bits;
3331 PixelFormat dst_format;
3333 /* we can't draw a bitmap of this format directly */
3334 hdc = CreateCompatibleDC(0);
3335 temp_hdc = 1;
3336 temp_bitmap = 1;
3338 bih.biSize = sizeof(BITMAPINFOHEADER);
3339 bih.biWidth = bitmap->width;
3340 bih.biHeight = -bitmap->height;
3341 bih.biPlanes = 1;
3342 bih.biBitCount = 32;
3343 bih.biCompression = BI_RGB;
3344 bih.biSizeImage = 0;
3345 bih.biXPelsPerMeter = 0;
3346 bih.biYPelsPerMeter = 0;
3347 bih.biClrUsed = 0;
3348 bih.biClrImportant = 0;
3350 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3351 (void**)&temp_bits, NULL, 0);
3353 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3354 dst_format = PixelFormat32bppPARGB;
3355 else
3356 dst_format = PixelFormat32bppRGB;
3358 convert_pixels(bitmap->width, bitmap->height,
3359 bitmap->width*4, temp_bits, dst_format,
3360 bitmap->stride, bitmap->bits, bitmap->format,
3361 bitmap->image.palette);
3363 else
3365 if (bitmap->hbitmap)
3366 hbitmap = bitmap->hbitmap;
3367 else
3369 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3370 temp_bitmap = 1;
3373 hdc = bitmap->hdc;
3374 temp_hdc = (hdc == 0);
3377 if (temp_hdc)
3379 if (!hdc) hdc = CreateCompatibleDC(0);
3380 old_hbm = SelectObject(hdc, hbitmap);
3383 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3385 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3386 hdc, srcx, srcy, srcwidth, srcheight);
3388 else
3390 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3391 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3394 if (temp_hdc)
3396 SelectObject(hdc, old_hbm);
3397 DeleteDC(hdc);
3400 if (temp_bitmap)
3401 DeleteObject(hbitmap);
3404 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3406 GpRectF rc;
3408 rc.X = srcx;
3409 rc.Y = srcy;
3410 rc.Width = srcwidth;
3411 rc.Height = srcheight;
3413 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3414 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3416 else
3418 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3419 return InvalidParameter;
3422 return Ok;
3425 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3426 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3427 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3428 DrawImageAbort callback, VOID * callbackData)
3430 GpPointF pointsF[3];
3431 INT i;
3433 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3434 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3435 callbackData);
3437 if(!points || count!=3)
3438 return InvalidParameter;
3440 for(i = 0; i < count; i++){
3441 pointsF[i].X = (REAL)points[i].X;
3442 pointsF[i].Y = (REAL)points[i].Y;
3445 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3446 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3447 callback, callbackData);
3450 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3451 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3452 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3453 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3454 VOID * callbackData)
3456 GpPointF points[3];
3458 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3459 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3460 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3462 points[0].X = dstx;
3463 points[0].Y = dsty;
3464 points[1].X = dstx + dstwidth;
3465 points[1].Y = dsty;
3466 points[2].X = dstx;
3467 points[2].Y = dsty + dstheight;
3469 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3470 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3473 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3474 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3475 INT srcwidth, INT srcheight, GpUnit srcUnit,
3476 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3477 VOID * callbackData)
3479 GpPointF points[3];
3481 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3482 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3483 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3485 points[0].X = dstx;
3486 points[0].Y = dsty;
3487 points[1].X = dstx + dstwidth;
3488 points[1].Y = dsty;
3489 points[2].X = dstx;
3490 points[2].Y = dsty + dstheight;
3492 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3493 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3496 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3497 REAL x, REAL y, REAL width, REAL height)
3499 RectF bounds;
3500 GpUnit unit;
3501 GpStatus ret;
3503 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3505 if(!graphics || !image)
3506 return InvalidParameter;
3508 ret = GdipGetImageBounds(image, &bounds, &unit);
3509 if(ret != Ok)
3510 return ret;
3512 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3513 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3514 unit, NULL, NULL, NULL);
3517 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3518 INT x, INT y, INT width, INT height)
3520 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3522 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3525 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3526 REAL y1, REAL x2, REAL y2)
3528 GpPointF pt[2];
3530 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3532 pt[0].X = x1;
3533 pt[0].Y = y1;
3534 pt[1].X = x2;
3535 pt[1].Y = y2;
3536 return GdipDrawLines(graphics, pen, pt, 2);
3539 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3540 INT y1, INT x2, INT y2)
3542 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3544 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3547 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3548 GpPointF *points, INT count)
3550 INT save_state;
3551 GpStatus retval;
3553 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3555 if(!pen || !graphics || (count < 2))
3556 return InvalidParameter;
3558 if(graphics->busy)
3559 return ObjectBusy;
3561 if (!graphics->hdc)
3563 FIXME("graphics object has no HDC\n");
3564 return Ok;
3567 save_state = prepare_dc(graphics, pen);
3569 retval = draw_polyline(graphics, pen, points, count, TRUE);
3571 restore_dc(graphics, save_state);
3573 return retval;
3576 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3577 GpPoint *points, INT count)
3579 GpStatus retval;
3580 GpPointF *ptf;
3581 int i;
3583 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3585 ptf = GdipAlloc(count * sizeof(GpPointF));
3586 if(!ptf) return OutOfMemory;
3588 for(i = 0; i < count; i ++){
3589 ptf[i].X = (REAL) points[i].X;
3590 ptf[i].Y = (REAL) points[i].Y;
3593 retval = GdipDrawLines(graphics, pen, ptf, count);
3595 GdipFree(ptf);
3596 return retval;
3599 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3601 INT save_state;
3602 GpStatus retval;
3604 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3606 if(!pen || !graphics)
3607 return InvalidParameter;
3609 if(graphics->busy)
3610 return ObjectBusy;
3612 if (!graphics->hdc)
3614 FIXME("graphics object has no HDC\n");
3615 return Ok;
3618 save_state = prepare_dc(graphics, pen);
3620 retval = draw_poly(graphics, pen, path->pathdata.Points,
3621 path->pathdata.Types, path->pathdata.Count, TRUE);
3623 restore_dc(graphics, save_state);
3625 return retval;
3628 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3629 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3631 INT save_state;
3633 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3634 width, height, startAngle, sweepAngle);
3636 if(!graphics || !pen)
3637 return InvalidParameter;
3639 if(graphics->busy)
3640 return ObjectBusy;
3642 if (!graphics->hdc)
3644 FIXME("graphics object has no HDC\n");
3645 return Ok;
3648 save_state = prepare_dc(graphics, pen);
3649 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3651 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3653 restore_dc(graphics, save_state);
3655 return Ok;
3658 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3659 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3661 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3662 width, height, startAngle, sweepAngle);
3664 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3667 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3668 REAL y, REAL width, REAL height)
3670 INT save_state;
3671 GpPointF ptf[4];
3672 POINT pti[4];
3674 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3676 if(!pen || !graphics)
3677 return InvalidParameter;
3679 if(graphics->busy)
3680 return ObjectBusy;
3682 if (!graphics->hdc)
3684 FIXME("graphics object has no HDC\n");
3685 return Ok;
3688 ptf[0].X = x;
3689 ptf[0].Y = y;
3690 ptf[1].X = x + width;
3691 ptf[1].Y = y;
3692 ptf[2].X = x + width;
3693 ptf[2].Y = y + height;
3694 ptf[3].X = x;
3695 ptf[3].Y = y + height;
3697 save_state = prepare_dc(graphics, pen);
3698 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3700 transform_and_round_points(graphics, pti, ptf, 4);
3701 Polygon(graphics->hdc, pti, 4);
3703 restore_dc(graphics, save_state);
3705 return Ok;
3708 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3709 INT y, INT width, INT height)
3711 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3713 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3716 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3717 GDIPCONST GpRectF* rects, INT count)
3719 GpPointF *ptf;
3720 POINT *pti;
3721 INT save_state, i;
3723 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3725 if(!graphics || !pen || !rects || count < 1)
3726 return InvalidParameter;
3728 if(graphics->busy)
3729 return ObjectBusy;
3731 if (!graphics->hdc)
3733 FIXME("graphics object has no HDC\n");
3734 return Ok;
3737 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3738 pti = GdipAlloc(4 * count * sizeof(POINT));
3740 if(!ptf || !pti){
3741 GdipFree(ptf);
3742 GdipFree(pti);
3743 return OutOfMemory;
3746 for(i = 0; i < count; i++){
3747 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3748 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3749 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3750 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3753 save_state = prepare_dc(graphics, pen);
3754 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3756 transform_and_round_points(graphics, pti, ptf, 4 * count);
3758 for(i = 0; i < count; i++)
3759 Polygon(graphics->hdc, &pti[4 * i], 4);
3761 restore_dc(graphics, save_state);
3763 GdipFree(ptf);
3764 GdipFree(pti);
3766 return Ok;
3769 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3770 GDIPCONST GpRect* rects, INT count)
3772 GpRectF *rectsF;
3773 GpStatus ret;
3774 INT i;
3776 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3778 if(!rects || count<=0)
3779 return InvalidParameter;
3781 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3782 if(!rectsF)
3783 return OutOfMemory;
3785 for(i = 0;i < count;i++){
3786 rectsF[i].X = (REAL)rects[i].X;
3787 rectsF[i].Y = (REAL)rects[i].Y;
3788 rectsF[i].Width = (REAL)rects[i].Width;
3789 rectsF[i].Height = (REAL)rects[i].Height;
3792 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3793 GdipFree(rectsF);
3795 return ret;
3798 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3799 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3801 GpPath *path;
3802 GpStatus stat;
3804 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3805 count, tension, fill);
3807 if(!graphics || !brush || !points)
3808 return InvalidParameter;
3810 if(graphics->busy)
3811 return ObjectBusy;
3813 if(count == 1) /* Do nothing */
3814 return Ok;
3816 stat = GdipCreatePath(fill, &path);
3817 if(stat != Ok)
3818 return stat;
3820 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3821 if(stat != Ok){
3822 GdipDeletePath(path);
3823 return stat;
3826 stat = GdipFillPath(graphics, brush, path);
3827 if(stat != Ok){
3828 GdipDeletePath(path);
3829 return stat;
3832 GdipDeletePath(path);
3834 return Ok;
3837 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3838 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3840 GpPointF *ptf;
3841 GpStatus stat;
3842 INT i;
3844 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3845 count, tension, fill);
3847 if(!points || count == 0)
3848 return InvalidParameter;
3850 if(count == 1) /* Do nothing */
3851 return Ok;
3853 ptf = GdipAlloc(sizeof(GpPointF)*count);
3854 if(!ptf)
3855 return OutOfMemory;
3857 for(i = 0;i < count;i++){
3858 ptf[i].X = (REAL)points[i].X;
3859 ptf[i].Y = (REAL)points[i].Y;
3862 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3864 GdipFree(ptf);
3866 return stat;
3869 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3870 GDIPCONST GpPointF *points, INT count)
3872 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3873 return GdipFillClosedCurve2(graphics, brush, points, count,
3874 0.5f, FillModeAlternate);
3877 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3878 GDIPCONST GpPoint *points, INT count)
3880 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3881 return GdipFillClosedCurve2I(graphics, brush, points, count,
3882 0.5f, FillModeAlternate);
3885 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3886 REAL y, REAL width, REAL height)
3888 GpStatus stat;
3889 GpPath *path;
3891 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3893 if(!graphics || !brush)
3894 return InvalidParameter;
3896 if(graphics->busy)
3897 return ObjectBusy;
3899 stat = GdipCreatePath(FillModeAlternate, &path);
3901 if (stat == Ok)
3903 stat = GdipAddPathEllipse(path, x, y, width, height);
3905 if (stat == Ok)
3906 stat = GdipFillPath(graphics, brush, path);
3908 GdipDeletePath(path);
3911 return stat;
3914 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3915 INT y, INT width, INT height)
3917 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3919 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3922 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3924 INT save_state;
3925 GpStatus retval;
3927 if(!graphics->hdc || !brush_can_fill_path(brush))
3928 return NotImplemented;
3930 save_state = SaveDC(graphics->hdc);
3931 EndPath(graphics->hdc);
3932 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3933 : WINDING));
3935 BeginPath(graphics->hdc);
3936 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3937 path->pathdata.Types, path->pathdata.Count, FALSE);
3939 if(retval != Ok)
3940 goto end;
3942 EndPath(graphics->hdc);
3943 brush_fill_path(graphics, brush);
3945 retval = Ok;
3947 end:
3948 RestoreDC(graphics->hdc, save_state);
3950 return retval;
3953 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3955 GpStatus stat;
3956 GpRegion *rgn;
3958 if (!brush_can_fill_pixels(brush))
3959 return NotImplemented;
3961 /* FIXME: This could probably be done more efficiently without regions. */
3963 stat = GdipCreateRegionPath(path, &rgn);
3965 if (stat == Ok)
3967 stat = GdipFillRegion(graphics, brush, rgn);
3969 GdipDeleteRegion(rgn);
3972 return stat;
3975 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3977 GpStatus stat = NotImplemented;
3979 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3981 if(!brush || !graphics || !path)
3982 return InvalidParameter;
3984 if(graphics->busy)
3985 return ObjectBusy;
3987 if (!graphics->image && !graphics->alpha_hdc)
3988 stat = GDI32_GdipFillPath(graphics, brush, path);
3990 if (stat == NotImplemented)
3991 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3993 if (stat == NotImplemented)
3995 FIXME("Not implemented for brushtype %i\n", brush->bt);
3996 stat = Ok;
3999 return stat;
4002 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4003 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4005 GpStatus stat;
4006 GpPath *path;
4008 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4009 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4011 if(!graphics || !brush)
4012 return InvalidParameter;
4014 if(graphics->busy)
4015 return ObjectBusy;
4017 stat = GdipCreatePath(FillModeAlternate, &path);
4019 if (stat == Ok)
4021 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4023 if (stat == Ok)
4024 stat = GdipFillPath(graphics, brush, path);
4026 GdipDeletePath(path);
4029 return stat;
4032 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4033 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4035 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4036 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4038 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4041 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4042 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4044 GpStatus stat;
4045 GpPath *path;
4047 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4049 if(!graphics || !brush || !points || !count)
4050 return InvalidParameter;
4052 if(graphics->busy)
4053 return ObjectBusy;
4055 stat = GdipCreatePath(fillMode, &path);
4057 if (stat == Ok)
4059 stat = GdipAddPathPolygon(path, points, count);
4061 if (stat == Ok)
4062 stat = GdipFillPath(graphics, brush, path);
4064 GdipDeletePath(path);
4067 return stat;
4070 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4071 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4073 GpStatus stat;
4074 GpPath *path;
4076 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4078 if(!graphics || !brush || !points || !count)
4079 return InvalidParameter;
4081 if(graphics->busy)
4082 return ObjectBusy;
4084 stat = GdipCreatePath(fillMode, &path);
4086 if (stat == Ok)
4088 stat = GdipAddPathPolygonI(path, points, count);
4090 if (stat == Ok)
4091 stat = GdipFillPath(graphics, brush, path);
4093 GdipDeletePath(path);
4096 return stat;
4099 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4100 GDIPCONST GpPointF *points, INT count)
4102 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4104 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4107 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4108 GDIPCONST GpPoint *points, INT count)
4110 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4112 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4115 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4116 REAL x, REAL y, REAL width, REAL height)
4118 GpStatus stat;
4119 GpPath *path;
4121 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4123 if(!graphics || !brush)
4124 return InvalidParameter;
4126 if(graphics->busy)
4127 return ObjectBusy;
4129 stat = GdipCreatePath(FillModeAlternate, &path);
4131 if (stat == Ok)
4133 stat = GdipAddPathRectangle(path, x, y, width, height);
4135 if (stat == Ok)
4136 stat = GdipFillPath(graphics, brush, path);
4138 GdipDeletePath(path);
4141 return stat;
4144 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4145 INT x, INT y, INT width, INT height)
4147 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4149 return GdipFillRectangle(graphics, brush, x, y, width, height);
4152 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4153 INT count)
4155 GpStatus ret;
4156 INT i;
4158 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4160 if(!rects)
4161 return InvalidParameter;
4163 for(i = 0; i < count; i++){
4164 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4165 if(ret != Ok) return ret;
4168 return Ok;
4171 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4172 INT count)
4174 GpRectF *rectsF;
4175 GpStatus ret;
4176 INT i;
4178 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4180 if(!rects || count <= 0)
4181 return InvalidParameter;
4183 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4184 if(!rectsF)
4185 return OutOfMemory;
4187 for(i = 0; i < count; i++){
4188 rectsF[i].X = (REAL)rects[i].X;
4189 rectsF[i].Y = (REAL)rects[i].Y;
4190 rectsF[i].X = (REAL)rects[i].Width;
4191 rectsF[i].Height = (REAL)rects[i].Height;
4194 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4195 GdipFree(rectsF);
4197 return ret;
4200 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4201 GpRegion* region)
4203 INT save_state;
4204 GpStatus status;
4205 HRGN hrgn;
4206 RECT rc;
4208 if(!graphics->hdc || !brush_can_fill_path(brush))
4209 return NotImplemented;
4211 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4212 if(status != Ok)
4213 return status;
4215 save_state = SaveDC(graphics->hdc);
4216 EndPath(graphics->hdc);
4218 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4220 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4222 BeginPath(graphics->hdc);
4223 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4224 EndPath(graphics->hdc);
4226 brush_fill_path(graphics, brush);
4229 RestoreDC(graphics->hdc, save_state);
4231 DeleteObject(hrgn);
4233 return Ok;
4236 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4237 GpRegion* region)
4239 GpStatus stat;
4240 GpRegion *temp_region;
4241 GpMatrix world_to_device;
4242 GpRectF graphics_bounds;
4243 DWORD *pixel_data;
4244 HRGN hregion;
4245 RECT bound_rect;
4246 GpRect gp_bound_rect;
4248 if (!brush_can_fill_pixels(brush))
4249 return NotImplemented;
4251 stat = get_graphics_bounds(graphics, &graphics_bounds);
4253 if (stat == Ok)
4254 stat = GdipCloneRegion(region, &temp_region);
4256 if (stat == Ok)
4258 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4259 CoordinateSpaceWorld, &world_to_device);
4261 if (stat == Ok)
4262 stat = GdipTransformRegion(temp_region, &world_to_device);
4264 if (stat == Ok)
4265 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4267 if (stat == Ok)
4268 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4270 GdipDeleteRegion(temp_region);
4273 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4275 DeleteObject(hregion);
4276 return Ok;
4279 if (stat == Ok)
4281 gp_bound_rect.X = bound_rect.left;
4282 gp_bound_rect.Y = bound_rect.top;
4283 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4284 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4286 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4287 if (!pixel_data)
4288 stat = OutOfMemory;
4290 if (stat == Ok)
4292 stat = brush_fill_pixels(graphics, brush, pixel_data,
4293 &gp_bound_rect, gp_bound_rect.Width);
4295 if (stat == Ok)
4296 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4297 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4298 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4300 GdipFree(pixel_data);
4303 DeleteObject(hregion);
4306 return stat;
4309 /*****************************************************************************
4310 * GdipFillRegion [GDIPLUS.@]
4312 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4313 GpRegion* region)
4315 GpStatus stat = NotImplemented;
4317 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4319 if (!(graphics && brush && region))
4320 return InvalidParameter;
4322 if(graphics->busy)
4323 return ObjectBusy;
4325 if (!graphics->image && !graphics->alpha_hdc)
4326 stat = GDI32_GdipFillRegion(graphics, brush, region);
4328 if (stat == NotImplemented)
4329 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4331 if (stat == NotImplemented)
4333 FIXME("not implemented for brushtype %i\n", brush->bt);
4334 stat = Ok;
4337 return stat;
4340 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4342 TRACE("(%p,%u)\n", graphics, intention);
4344 if(!graphics)
4345 return InvalidParameter;
4347 if(graphics->busy)
4348 return ObjectBusy;
4350 /* We have no internal operation queue, so there's no need to clear it. */
4352 if (graphics->hdc)
4353 GdiFlush();
4355 return Ok;
4358 /*****************************************************************************
4359 * GdipGetClipBounds [GDIPLUS.@]
4361 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4363 TRACE("(%p, %p)\n", graphics, rect);
4365 if(!graphics)
4366 return InvalidParameter;
4368 if(graphics->busy)
4369 return ObjectBusy;
4371 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4374 /*****************************************************************************
4375 * GdipGetClipBoundsI [GDIPLUS.@]
4377 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4379 TRACE("(%p, %p)\n", graphics, rect);
4381 if(!graphics)
4382 return InvalidParameter;
4384 if(graphics->busy)
4385 return ObjectBusy;
4387 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4390 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4391 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4392 CompositingMode *mode)
4394 TRACE("(%p, %p)\n", graphics, mode);
4396 if(!graphics || !mode)
4397 return InvalidParameter;
4399 if(graphics->busy)
4400 return ObjectBusy;
4402 *mode = graphics->compmode;
4404 return Ok;
4407 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4408 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4409 CompositingQuality *quality)
4411 TRACE("(%p, %p)\n", graphics, quality);
4413 if(!graphics || !quality)
4414 return InvalidParameter;
4416 if(graphics->busy)
4417 return ObjectBusy;
4419 *quality = graphics->compqual;
4421 return Ok;
4424 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4425 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4426 InterpolationMode *mode)
4428 TRACE("(%p, %p)\n", graphics, mode);
4430 if(!graphics || !mode)
4431 return InvalidParameter;
4433 if(graphics->busy)
4434 return ObjectBusy;
4436 *mode = graphics->interpolation;
4438 return Ok;
4441 /* FIXME: Need to handle color depths less than 24bpp */
4442 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4444 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4446 if(!graphics || !argb)
4447 return InvalidParameter;
4449 if(graphics->busy)
4450 return ObjectBusy;
4452 return Ok;
4455 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4457 TRACE("(%p, %p)\n", graphics, scale);
4459 if(!graphics || !scale)
4460 return InvalidParameter;
4462 if(graphics->busy)
4463 return ObjectBusy;
4465 *scale = graphics->scale;
4467 return Ok;
4470 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4472 TRACE("(%p, %p)\n", graphics, unit);
4474 if(!graphics || !unit)
4475 return InvalidParameter;
4477 if(graphics->busy)
4478 return ObjectBusy;
4480 *unit = graphics->unit;
4482 return Ok;
4485 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4486 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4487 *mode)
4489 TRACE("(%p, %p)\n", graphics, mode);
4491 if(!graphics || !mode)
4492 return InvalidParameter;
4494 if(graphics->busy)
4495 return ObjectBusy;
4497 *mode = graphics->pixeloffset;
4499 return Ok;
4502 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4503 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4505 TRACE("(%p, %p)\n", graphics, mode);
4507 if(!graphics || !mode)
4508 return InvalidParameter;
4510 if(graphics->busy)
4511 return ObjectBusy;
4513 *mode = graphics->smoothing;
4515 return Ok;
4518 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4520 TRACE("(%p, %p)\n", graphics, contrast);
4522 if(!graphics || !contrast)
4523 return InvalidParameter;
4525 *contrast = graphics->textcontrast;
4527 return Ok;
4530 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4531 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4532 TextRenderingHint *hint)
4534 TRACE("(%p, %p)\n", graphics, hint);
4536 if(!graphics || !hint)
4537 return InvalidParameter;
4539 if(graphics->busy)
4540 return ObjectBusy;
4542 *hint = graphics->texthint;
4544 return Ok;
4547 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4549 GpRegion *clip_rgn;
4550 GpStatus stat;
4552 TRACE("(%p, %p)\n", graphics, rect);
4554 if(!graphics || !rect)
4555 return InvalidParameter;
4557 if(graphics->busy)
4558 return ObjectBusy;
4560 /* intersect window and graphics clipping regions */
4561 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4562 return stat;
4564 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4565 goto cleanup;
4567 /* get bounds of the region */
4568 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4570 cleanup:
4571 GdipDeleteRegion(clip_rgn);
4573 return stat;
4576 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4578 GpRectF rectf;
4579 GpStatus stat;
4581 TRACE("(%p, %p)\n", graphics, rect);
4583 if(!graphics || !rect)
4584 return InvalidParameter;
4586 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4588 rect->X = gdip_round(rectf.X);
4589 rect->Y = gdip_round(rectf.Y);
4590 rect->Width = gdip_round(rectf.Width);
4591 rect->Height = gdip_round(rectf.Height);
4594 return stat;
4597 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4599 TRACE("(%p, %p)\n", graphics, matrix);
4601 if(!graphics || !matrix)
4602 return InvalidParameter;
4604 if(graphics->busy)
4605 return ObjectBusy;
4607 *matrix = graphics->worldtrans;
4608 return Ok;
4611 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4613 GpSolidFill *brush;
4614 GpStatus stat;
4615 GpRectF wnd_rect;
4617 TRACE("(%p, %x)\n", graphics, color);
4619 if(!graphics)
4620 return InvalidParameter;
4622 if(graphics->busy)
4623 return ObjectBusy;
4625 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4626 return stat;
4628 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4629 GdipDeleteBrush((GpBrush*)brush);
4630 return stat;
4633 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4634 wnd_rect.Width, wnd_rect.Height);
4636 GdipDeleteBrush((GpBrush*)brush);
4638 return Ok;
4641 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4643 TRACE("(%p, %p)\n", graphics, res);
4645 if(!graphics || !res)
4646 return InvalidParameter;
4648 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4651 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4653 GpStatus stat;
4654 GpRegion* rgn;
4655 GpPointF pt;
4657 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4659 if(!graphics || !result)
4660 return InvalidParameter;
4662 if(graphics->busy)
4663 return ObjectBusy;
4665 pt.X = x;
4666 pt.Y = y;
4667 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4668 CoordinateSpaceWorld, &pt, 1)) != Ok)
4669 return stat;
4671 if((stat = GdipCreateRegion(&rgn)) != Ok)
4672 return stat;
4674 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4675 goto cleanup;
4677 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4679 cleanup:
4680 GdipDeleteRegion(rgn);
4681 return stat;
4684 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4686 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4689 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4691 GpStatus stat;
4692 GpRegion* rgn;
4693 GpPointF pts[2];
4695 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4697 if(!graphics || !result)
4698 return InvalidParameter;
4700 if(graphics->busy)
4701 return ObjectBusy;
4703 pts[0].X = x;
4704 pts[0].Y = y;
4705 pts[1].X = x + width;
4706 pts[1].Y = y + height;
4708 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4709 CoordinateSpaceWorld, pts, 2)) != Ok)
4710 return stat;
4712 pts[1].X -= pts[0].X;
4713 pts[1].Y -= pts[0].Y;
4715 if((stat = GdipCreateRegion(&rgn)) != Ok)
4716 return stat;
4718 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4719 goto cleanup;
4721 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4723 cleanup:
4724 GdipDeleteRegion(rgn);
4725 return stat;
4728 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4730 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4733 GpStatus gdip_format_string(HDC hdc,
4734 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4735 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4736 gdip_format_string_callback callback, void *user_data)
4738 WCHAR* stringdup;
4739 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4740 nheight, lineend, lineno = 0;
4741 RectF bounds;
4742 StringAlignment halign;
4743 GpStatus stat = Ok;
4744 SIZE size;
4745 HotkeyPrefix hkprefix;
4746 INT *hotkeyprefix_offsets=NULL;
4747 INT hotkeyprefix_count=0;
4748 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4749 int seen_prefix=0;
4751 if(length == -1) length = lstrlenW(string);
4753 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4754 if(!stringdup) return OutOfMemory;
4756 nwidth = rect->Width;
4757 nheight = rect->Height;
4759 if (format)
4760 hkprefix = format->hkprefix;
4761 else
4762 hkprefix = HotkeyPrefixNone;
4764 if (hkprefix == HotkeyPrefixShow)
4766 for (i=0; i<length; i++)
4768 if (string[i] == '&')
4769 hotkeyprefix_count++;
4773 if (hotkeyprefix_count)
4774 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4776 hotkeyprefix_count = 0;
4778 for(i = 0, j = 0; i < length; i++){
4779 /* FIXME: This makes the indexes passed to callback inaccurate. */
4780 if(!isprintW(string[i]) && (string[i] != '\n'))
4781 continue;
4783 /* FIXME: tabs should be handled using tabstops from stringformat */
4784 if (string[i] == '\t')
4785 continue;
4787 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4788 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4789 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4791 seen_prefix = 1;
4792 continue;
4795 seen_prefix = 0;
4797 stringdup[j] = string[i];
4798 j++;
4801 length = j;
4803 if (format) halign = format->align;
4804 else halign = StringAlignmentNear;
4806 while(sum < length){
4807 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4808 nwidth, &fit, NULL, &size);
4809 fitcpy = fit;
4811 if(fit == 0)
4812 break;
4814 for(lret = 0; lret < fit; lret++)
4815 if(*(stringdup + sum + lret) == '\n')
4816 break;
4818 /* Line break code (may look strange, but it imitates windows). */
4819 if(lret < fit)
4820 lineend = fit = lret; /* this is not an off-by-one error */
4821 else if(fit < (length - sum)){
4822 if(*(stringdup + sum + fit) == ' ')
4823 while(*(stringdup + sum + fit) == ' ')
4824 fit++;
4825 else
4826 while(*(stringdup + sum + fit - 1) != ' '){
4827 fit--;
4829 if(*(stringdup + sum + fit) == '\t')
4830 break;
4832 if(fit == 0){
4833 fit = fitcpy;
4834 break;
4837 lineend = fit;
4838 while(*(stringdup + sum + lineend - 1) == ' ' ||
4839 *(stringdup + sum + lineend - 1) == '\t')
4840 lineend--;
4842 else
4843 lineend = fit;
4845 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4846 nwidth, &j, NULL, &size);
4848 bounds.Width = size.cx;
4850 if(height + size.cy > nheight)
4851 bounds.Height = nheight - (height + size.cy);
4852 else
4853 bounds.Height = size.cy;
4855 bounds.Y = rect->Y + height;
4857 switch (halign)
4859 case StringAlignmentNear:
4860 default:
4861 bounds.X = rect->X;
4862 break;
4863 case StringAlignmentCenter:
4864 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4865 break;
4866 case StringAlignmentFar:
4867 bounds.X = rect->X + rect->Width - bounds.Width;
4868 break;
4871 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4872 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4873 break;
4875 stat = callback(hdc, stringdup, sum, lineend,
4876 font, rect, format, lineno, &bounds,
4877 &hotkeyprefix_offsets[hotkeyprefix_pos],
4878 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4880 if (stat != Ok)
4881 break;
4883 sum += fit + (lret < fitcpy ? 1 : 0);
4884 height += size.cy;
4885 lineno++;
4887 hotkeyprefix_pos = hotkeyprefix_end_pos;
4889 if(height > nheight)
4890 break;
4892 /* Stop if this was a linewrap (but not if it was a linebreak). */
4893 if ((lret == fitcpy) && format &&
4894 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4895 break;
4898 GdipFree(stringdup);
4899 GdipFree(hotkeyprefix_offsets);
4901 return stat;
4904 struct measure_ranges_args {
4905 GpRegion **regions;
4906 REAL rel_width, rel_height;
4909 static GpStatus measure_ranges_callback(HDC hdc,
4910 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4911 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4912 INT lineno, const RectF *bounds, INT *underlined_indexes,
4913 INT underlined_index_count, void *user_data)
4915 int i;
4916 GpStatus stat = Ok;
4917 struct measure_ranges_args *args = user_data;
4919 for (i=0; i<format->range_count; i++)
4921 INT range_start = max(index, format->character_ranges[i].First);
4922 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4923 if (range_start < range_end)
4925 GpRectF range_rect;
4926 SIZE range_size;
4928 range_rect.Y = bounds->Y / args->rel_height;
4929 range_rect.Height = bounds->Height / args->rel_height;
4931 GetTextExtentExPointW(hdc, string + index, range_start - index,
4932 INT_MAX, NULL, NULL, &range_size);
4933 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4935 GetTextExtentExPointW(hdc, string + index, range_end - index,
4936 INT_MAX, NULL, NULL, &range_size);
4937 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4939 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4940 if (stat != Ok)
4941 break;
4945 return stat;
4948 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4949 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4950 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4951 INT regionCount, GpRegion** regions)
4953 GpStatus stat;
4954 int i;
4955 HFONT gdifont, oldfont;
4956 struct measure_ranges_args args;
4957 HDC hdc, temp_hdc=NULL;
4958 GpPointF pt[3];
4959 RectF scaled_rect;
4960 REAL margin_x;
4962 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4963 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4965 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4966 return InvalidParameter;
4968 if (regionCount < stringFormat->range_count)
4969 return InvalidParameter;
4971 if(!graphics->hdc)
4973 hdc = temp_hdc = CreateCompatibleDC(0);
4974 if (!temp_hdc) return OutOfMemory;
4976 else
4977 hdc = graphics->hdc;
4979 if (stringFormat->attr)
4980 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4982 pt[0].X = 0.0;
4983 pt[0].Y = 0.0;
4984 pt[1].X = 1.0;
4985 pt[1].Y = 0.0;
4986 pt[2].X = 0.0;
4987 pt[2].Y = 1.0;
4988 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4989 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4990 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4991 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4992 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4994 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4995 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4997 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4998 scaled_rect.Y = layoutRect->Y * args.rel_height;
4999 if (stringFormat->attr & StringFormatFlagsNoClip)
5001 scaled_rect.Width = (REAL)(1 << 23);
5002 scaled_rect.Height = (REAL)(1 << 23);
5004 else
5006 scaled_rect.Width = layoutRect->Width * args.rel_width;
5007 scaled_rect.Height = layoutRect->Height * args.rel_height;
5009 if (scaled_rect.Width >= 0.5)
5011 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5012 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5015 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5016 oldfont = SelectObject(hdc, gdifont);
5018 for (i=0; i<stringFormat->range_count; i++)
5020 stat = GdipSetEmpty(regions[i]);
5021 if (stat != Ok)
5022 return stat;
5025 args.regions = regions;
5027 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5028 measure_ranges_callback, &args);
5030 SelectObject(hdc, oldfont);
5031 DeleteObject(gdifont);
5033 if (temp_hdc)
5034 DeleteDC(temp_hdc);
5036 return stat;
5039 struct measure_string_args {
5040 RectF *bounds;
5041 INT *codepointsfitted;
5042 INT *linesfilled;
5043 REAL rel_width, rel_height;
5046 static GpStatus measure_string_callback(HDC hdc,
5047 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5048 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5049 INT lineno, const RectF *bounds, INT *underlined_indexes,
5050 INT underlined_index_count, void *user_data)
5052 struct measure_string_args *args = user_data;
5053 REAL new_width, new_height;
5055 new_width = bounds->Width / args->rel_width;
5056 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5058 if (new_width > args->bounds->Width)
5059 args->bounds->Width = new_width;
5061 if (new_height > args->bounds->Height)
5062 args->bounds->Height = new_height;
5064 if (args->codepointsfitted)
5065 *args->codepointsfitted = index + length;
5067 if (args->linesfilled)
5068 (*args->linesfilled)++;
5070 return Ok;
5073 /* Find the smallest rectangle that bounds the text when it is printed in rect
5074 * according to the format options listed in format. If rect has 0 width and
5075 * height, then just find the smallest rectangle that bounds the text when it's
5076 * printed at location (rect->X, rect-Y). */
5077 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5078 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5079 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5080 INT *codepointsfitted, INT *linesfilled)
5082 HFONT oldfont, gdifont;
5083 struct measure_string_args args;
5084 HDC temp_hdc=NULL, hdc;
5085 GpPointF pt[3];
5086 RectF scaled_rect;
5087 REAL margin_x;
5088 INT lines, glyphs, format_flags = format ? format->attr : 0;
5090 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5091 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5092 bounds, codepointsfitted, linesfilled);
5094 if(!graphics || !string || !font || !rect || !bounds)
5095 return InvalidParameter;
5097 if(!graphics->hdc)
5099 hdc = temp_hdc = CreateCompatibleDC(0);
5100 if (!temp_hdc) return OutOfMemory;
5102 else
5103 hdc = graphics->hdc;
5105 if(linesfilled) *linesfilled = 0;
5106 if(codepointsfitted) *codepointsfitted = 0;
5108 if(format)
5109 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5111 pt[0].X = 0.0;
5112 pt[0].Y = 0.0;
5113 pt[1].X = 1.0;
5114 pt[1].Y = 0.0;
5115 pt[2].X = 0.0;
5116 pt[2].Y = 1.0;
5117 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5118 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5119 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5120 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5121 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5123 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5124 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5126 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5127 scaled_rect.Y = rect->Y * args.rel_height;
5128 scaled_rect.Width = rect->Width * args.rel_width;
5129 scaled_rect.Height = rect->Height * args.rel_height;
5131 if ((format_flags & StringFormatFlagsNoClip) ||
5132 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5133 if ((format_flags & StringFormatFlagsNoClip) ||
5134 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5136 if (scaled_rect.Width >= 0.5)
5138 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5139 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5142 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5143 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5145 get_font_hfont(graphics, font, format, &gdifont, NULL);
5146 oldfont = SelectObject(hdc, gdifont);
5148 bounds->X = rect->X;
5149 bounds->Y = rect->Y;
5150 bounds->Width = 0.0;
5151 bounds->Height = 0.0;
5153 args.bounds = bounds;
5154 args.codepointsfitted = &glyphs;
5155 args.linesfilled = &lines;
5156 lines = glyphs = 0;
5158 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5159 measure_string_callback, &args);
5161 if (linesfilled) *linesfilled = lines;
5162 if (codepointsfitted) *codepointsfitted = glyphs;
5164 if (lines)
5165 bounds->Width += margin_x * 2.0;
5167 SelectObject(hdc, oldfont);
5168 DeleteObject(gdifont);
5170 if (temp_hdc)
5171 DeleteDC(temp_hdc);
5173 return Ok;
5176 struct draw_string_args {
5177 GpGraphics *graphics;
5178 GDIPCONST GpBrush *brush;
5179 REAL x, y, rel_width, rel_height, ascent;
5182 static GpStatus draw_string_callback(HDC hdc,
5183 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5184 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5185 INT lineno, const RectF *bounds, INT *underlined_indexes,
5186 INT underlined_index_count, void *user_data)
5188 struct draw_string_args *args = user_data;
5189 PointF position;
5190 GpStatus stat;
5192 position.X = args->x + bounds->X / args->rel_width;
5193 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5195 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5196 args->brush, &position,
5197 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5199 if (stat == Ok && underlined_index_count)
5201 OUTLINETEXTMETRICW otm;
5202 REAL underline_y, underline_height;
5203 int i;
5205 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5207 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5208 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5210 for (i=0; i<underlined_index_count; i++)
5212 REAL start_x, end_x;
5213 SIZE text_size;
5214 INT ofs = underlined_indexes[i] - index;
5216 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5217 start_x = text_size.cx / args->rel_width;
5219 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5220 end_x = text_size.cx / args->rel_width;
5222 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5226 return stat;
5229 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5230 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5231 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5233 HRGN rgn = NULL;
5234 HFONT gdifont;
5235 GpPointF pt[3], rectcpy[4];
5236 POINT corners[4];
5237 REAL rel_width, rel_height, margin_x;
5238 INT save_state, format_flags = 0;
5239 REAL offsety = 0.0;
5240 struct draw_string_args args;
5241 RectF scaled_rect;
5242 HDC hdc, temp_hdc=NULL;
5243 TEXTMETRICW textmetric;
5245 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5246 length, font, debugstr_rectf(rect), format, brush);
5248 if(!graphics || !string || !font || !brush || !rect)
5249 return InvalidParameter;
5251 if(graphics->hdc)
5253 hdc = graphics->hdc;
5255 else
5257 hdc = temp_hdc = CreateCompatibleDC(0);
5260 if(format){
5261 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5263 format_flags = format->attr;
5265 /* Should be no need to explicitly test for StringAlignmentNear as
5266 * that is default behavior if no alignment is passed. */
5267 if(format->vertalign != StringAlignmentNear){
5268 RectF bounds, in_rect = *rect;
5269 in_rect.Height = 0.0; /* avoid height clipping */
5270 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5272 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5274 if(format->vertalign == StringAlignmentCenter)
5275 offsety = (rect->Height - bounds.Height) / 2;
5276 else if(format->vertalign == StringAlignmentFar)
5277 offsety = (rect->Height - bounds.Height);
5279 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5282 save_state = SaveDC(hdc);
5284 pt[0].X = 0.0;
5285 pt[0].Y = 0.0;
5286 pt[1].X = 1.0;
5287 pt[1].Y = 0.0;
5288 pt[2].X = 0.0;
5289 pt[2].Y = 1.0;
5290 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5291 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5292 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5293 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5294 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5296 rectcpy[3].X = rectcpy[0].X = rect->X;
5297 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5298 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5299 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5300 transform_and_round_points(graphics, corners, rectcpy, 4);
5302 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5303 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5305 scaled_rect.X = margin_x * rel_width;
5306 scaled_rect.Y = 0.0;
5307 scaled_rect.Width = rel_width * rect->Width;
5308 scaled_rect.Height = rel_height * rect->Height;
5310 if ((format_flags & StringFormatFlagsNoClip) ||
5311 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5312 if ((format_flags & StringFormatFlagsNoClip) ||
5313 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5315 if (scaled_rect.Width >= 0.5)
5317 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5318 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5321 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5322 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5324 if (!(format_flags & StringFormatFlagsNoClip) &&
5325 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23)
5327 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5328 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5329 SelectClipRgn(hdc, rgn);
5332 get_font_hfont(graphics, font, format, &gdifont, NULL);
5333 SelectObject(hdc, gdifont);
5335 args.graphics = graphics;
5336 args.brush = brush;
5338 args.x = rect->X;
5339 args.y = rect->Y + offsety;
5341 args.rel_width = rel_width;
5342 args.rel_height = rel_height;
5344 GetTextMetricsW(hdc, &textmetric);
5345 args.ascent = textmetric.tmAscent / rel_height;
5347 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5348 draw_string_callback, &args);
5350 DeleteObject(rgn);
5351 DeleteObject(gdifont);
5353 RestoreDC(hdc, save_state);
5355 DeleteDC(temp_hdc);
5357 return Ok;
5360 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5362 TRACE("(%p)\n", graphics);
5364 if(!graphics)
5365 return InvalidParameter;
5367 if(graphics->busy)
5368 return ObjectBusy;
5370 return GdipSetInfinite(graphics->clip);
5373 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5375 TRACE("(%p)\n", graphics);
5377 if(!graphics)
5378 return InvalidParameter;
5380 if(graphics->busy)
5381 return ObjectBusy;
5383 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5386 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5388 return GdipEndContainer(graphics, state);
5391 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5392 GpMatrixOrder order)
5394 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5396 if(!graphics)
5397 return InvalidParameter;
5399 if(graphics->busy)
5400 return ObjectBusy;
5402 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5405 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5407 return GdipBeginContainer2(graphics, state);
5410 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5411 GraphicsContainer *state)
5413 GraphicsContainerItem *container;
5414 GpStatus sts;
5416 TRACE("(%p, %p)\n", graphics, state);
5418 if(!graphics || !state)
5419 return InvalidParameter;
5421 sts = init_container(&container, graphics);
5422 if(sts != Ok)
5423 return sts;
5425 list_add_head(&graphics->containers, &container->entry);
5426 *state = graphics->contid = container->contid;
5428 return Ok;
5431 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5433 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5434 return NotImplemented;
5437 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5439 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5440 return NotImplemented;
5443 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5445 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5446 return NotImplemented;
5449 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5451 GpStatus sts;
5452 GraphicsContainerItem *container, *container2;
5454 TRACE("(%p, %x)\n", graphics, state);
5456 if(!graphics)
5457 return InvalidParameter;
5459 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5460 if(container->contid == state)
5461 break;
5464 /* did not find a matching container */
5465 if(&container->entry == &graphics->containers)
5466 return Ok;
5468 sts = restore_container(graphics, container);
5469 if(sts != Ok)
5470 return sts;
5472 /* remove all of the containers on top of the found container */
5473 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5474 if(container->contid == state)
5475 break;
5476 list_remove(&container->entry);
5477 delete_container(container);
5480 list_remove(&container->entry);
5481 delete_container(container);
5483 return Ok;
5486 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5487 REAL sy, GpMatrixOrder order)
5489 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5491 if(!graphics)
5492 return InvalidParameter;
5494 if(graphics->busy)
5495 return ObjectBusy;
5497 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5500 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5501 CombineMode mode)
5503 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5505 if(!graphics || !srcgraphics)
5506 return InvalidParameter;
5508 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5511 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5512 CompositingMode mode)
5514 TRACE("(%p, %d)\n", graphics, mode);
5516 if(!graphics)
5517 return InvalidParameter;
5519 if(graphics->busy)
5520 return ObjectBusy;
5522 graphics->compmode = mode;
5524 return Ok;
5527 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5528 CompositingQuality quality)
5530 TRACE("(%p, %d)\n", graphics, quality);
5532 if(!graphics)
5533 return InvalidParameter;
5535 if(graphics->busy)
5536 return ObjectBusy;
5538 graphics->compqual = quality;
5540 return Ok;
5543 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5544 InterpolationMode mode)
5546 TRACE("(%p, %d)\n", graphics, mode);
5548 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5549 return InvalidParameter;
5551 if(graphics->busy)
5552 return ObjectBusy;
5554 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5555 mode = InterpolationModeBilinear;
5557 if (mode == InterpolationModeHighQuality)
5558 mode = InterpolationModeHighQualityBicubic;
5560 graphics->interpolation = mode;
5562 return Ok;
5565 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5567 TRACE("(%p, %.2f)\n", graphics, scale);
5569 if(!graphics || (scale <= 0.0))
5570 return InvalidParameter;
5572 if(graphics->busy)
5573 return ObjectBusy;
5575 graphics->scale = scale;
5577 return Ok;
5580 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5582 TRACE("(%p, %d)\n", graphics, unit);
5584 if(!graphics)
5585 return InvalidParameter;
5587 if(graphics->busy)
5588 return ObjectBusy;
5590 if(unit == UnitWorld)
5591 return InvalidParameter;
5593 graphics->unit = unit;
5595 return Ok;
5598 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5599 mode)
5601 TRACE("(%p, %d)\n", graphics, mode);
5603 if(!graphics)
5604 return InvalidParameter;
5606 if(graphics->busy)
5607 return ObjectBusy;
5609 graphics->pixeloffset = mode;
5611 return Ok;
5614 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5616 static int calls;
5618 TRACE("(%p,%i,%i)\n", graphics, x, y);
5620 if (!(calls++))
5621 FIXME("value is unused in rendering\n");
5623 if (!graphics)
5624 return InvalidParameter;
5626 graphics->origin_x = x;
5627 graphics->origin_y = y;
5629 return Ok;
5632 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5634 TRACE("(%p,%p,%p)\n", graphics, x, y);
5636 if (!graphics || !x || !y)
5637 return InvalidParameter;
5639 *x = graphics->origin_x;
5640 *y = graphics->origin_y;
5642 return Ok;
5645 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5647 TRACE("(%p, %d)\n", graphics, mode);
5649 if(!graphics)
5650 return InvalidParameter;
5652 if(graphics->busy)
5653 return ObjectBusy;
5655 graphics->smoothing = mode;
5657 return Ok;
5660 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5662 TRACE("(%p, %d)\n", graphics, contrast);
5664 if(!graphics)
5665 return InvalidParameter;
5667 graphics->textcontrast = contrast;
5669 return Ok;
5672 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5673 TextRenderingHint hint)
5675 TRACE("(%p, %d)\n", graphics, hint);
5677 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5678 return InvalidParameter;
5680 if(graphics->busy)
5681 return ObjectBusy;
5683 graphics->texthint = hint;
5685 return Ok;
5688 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5690 TRACE("(%p, %p)\n", graphics, matrix);
5692 if(!graphics || !matrix)
5693 return InvalidParameter;
5695 if(graphics->busy)
5696 return ObjectBusy;
5698 TRACE("%f,%f,%f,%f,%f,%f\n",
5699 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5700 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5702 graphics->worldtrans = *matrix;
5704 return Ok;
5707 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5708 REAL dy, GpMatrixOrder order)
5710 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5712 if(!graphics)
5713 return InvalidParameter;
5715 if(graphics->busy)
5716 return ObjectBusy;
5718 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5721 /*****************************************************************************
5722 * GdipSetClipHrgn [GDIPLUS.@]
5724 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5726 GpRegion *region;
5727 GpStatus status;
5729 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5731 if(!graphics)
5732 return InvalidParameter;
5734 status = GdipCreateRegionHrgn(hrgn, &region);
5735 if(status != Ok)
5736 return status;
5738 status = GdipSetClipRegion(graphics, region, mode);
5740 GdipDeleteRegion(region);
5741 return status;
5744 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5746 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5748 if(!graphics)
5749 return InvalidParameter;
5751 if(graphics->busy)
5752 return ObjectBusy;
5754 return GdipCombineRegionPath(graphics->clip, path, mode);
5757 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5758 REAL width, REAL height,
5759 CombineMode mode)
5761 GpRectF rect;
5763 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5765 if(!graphics)
5766 return InvalidParameter;
5768 if(graphics->busy)
5769 return ObjectBusy;
5771 rect.X = x;
5772 rect.Y = y;
5773 rect.Width = width;
5774 rect.Height = height;
5776 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5779 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5780 INT width, INT height,
5781 CombineMode mode)
5783 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5785 if(!graphics)
5786 return InvalidParameter;
5788 if(graphics->busy)
5789 return ObjectBusy;
5791 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5794 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5795 CombineMode mode)
5797 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5799 if(!graphics || !region)
5800 return InvalidParameter;
5802 if(graphics->busy)
5803 return ObjectBusy;
5805 return GdipCombineRegionRegion(graphics->clip, region, mode);
5808 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5809 UINT limitDpi)
5811 static int calls;
5813 TRACE("(%p,%u)\n", metafile, limitDpi);
5815 if(!(calls++))
5816 FIXME("not implemented\n");
5818 return NotImplemented;
5821 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5822 INT count)
5824 INT save_state;
5825 POINT *pti;
5827 TRACE("(%p, %p, %d)\n", graphics, points, count);
5829 if(!graphics || !pen || count<=0)
5830 return InvalidParameter;
5832 if(graphics->busy)
5833 return ObjectBusy;
5835 if (!graphics->hdc)
5837 FIXME("graphics object has no HDC\n");
5838 return Ok;
5841 pti = GdipAlloc(sizeof(POINT) * count);
5843 save_state = prepare_dc(graphics, pen);
5844 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5846 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5847 Polygon(graphics->hdc, pti, count);
5849 restore_dc(graphics, save_state);
5850 GdipFree(pti);
5852 return Ok;
5855 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5856 INT count)
5858 GpStatus ret;
5859 GpPointF *ptf;
5860 INT i;
5862 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5864 if(count<=0) return InvalidParameter;
5865 ptf = GdipAlloc(sizeof(GpPointF) * count);
5867 for(i = 0;i < count; i++){
5868 ptf[i].X = (REAL)points[i].X;
5869 ptf[i].Y = (REAL)points[i].Y;
5872 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5873 GdipFree(ptf);
5875 return ret;
5878 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5880 TRACE("(%p, %p)\n", graphics, dpi);
5882 if(!graphics || !dpi)
5883 return InvalidParameter;
5885 if(graphics->busy)
5886 return ObjectBusy;
5888 *dpi = graphics->xres;
5889 return Ok;
5892 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5894 TRACE("(%p, %p)\n", graphics, dpi);
5896 if(!graphics || !dpi)
5897 return InvalidParameter;
5899 if(graphics->busy)
5900 return ObjectBusy;
5902 *dpi = graphics->yres;
5903 return Ok;
5906 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5907 GpMatrixOrder order)
5909 GpMatrix m;
5910 GpStatus ret;
5912 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5914 if(!graphics || !matrix)
5915 return InvalidParameter;
5917 if(graphics->busy)
5918 return ObjectBusy;
5920 m = graphics->worldtrans;
5922 ret = GdipMultiplyMatrix(&m, matrix, order);
5923 if(ret == Ok)
5924 graphics->worldtrans = m;
5926 return ret;
5929 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5930 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5932 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5934 GpStatus stat=Ok;
5936 TRACE("(%p, %p)\n", graphics, hdc);
5938 if(!graphics || !hdc)
5939 return InvalidParameter;
5941 if(graphics->busy)
5942 return ObjectBusy;
5944 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5946 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5948 else if (!graphics->hdc || graphics->alpha_hdc ||
5949 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5951 /* Create a fake HDC and fill it with a constant color. */
5952 HDC temp_hdc;
5953 HBITMAP hbitmap;
5954 GpRectF bounds;
5955 BITMAPINFOHEADER bmih;
5956 int i;
5958 stat = get_graphics_bounds(graphics, &bounds);
5959 if (stat != Ok)
5960 return stat;
5962 graphics->temp_hbitmap_width = bounds.Width;
5963 graphics->temp_hbitmap_height = bounds.Height;
5965 bmih.biSize = sizeof(bmih);
5966 bmih.biWidth = graphics->temp_hbitmap_width;
5967 bmih.biHeight = -graphics->temp_hbitmap_height;
5968 bmih.biPlanes = 1;
5969 bmih.biBitCount = 32;
5970 bmih.biCompression = BI_RGB;
5971 bmih.biSizeImage = 0;
5972 bmih.biXPelsPerMeter = 0;
5973 bmih.biYPelsPerMeter = 0;
5974 bmih.biClrUsed = 0;
5975 bmih.biClrImportant = 0;
5977 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5978 (void**)&graphics->temp_bits, NULL, 0);
5979 if (!hbitmap)
5980 return GenericError;
5982 temp_hdc = CreateCompatibleDC(0);
5983 if (!temp_hdc)
5985 DeleteObject(hbitmap);
5986 return GenericError;
5989 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5990 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5992 SelectObject(temp_hdc, hbitmap);
5994 graphics->temp_hbitmap = hbitmap;
5995 *hdc = graphics->temp_hdc = temp_hdc;
5997 else
5999 *hdc = graphics->hdc;
6002 if (stat == Ok)
6003 graphics->busy = TRUE;
6005 return stat;
6008 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6010 GpStatus stat=Ok;
6012 TRACE("(%p, %p)\n", graphics, hdc);
6014 if(!graphics || !hdc || !graphics->busy)
6015 return InvalidParameter;
6017 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6019 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6021 else if (graphics->temp_hdc == hdc)
6023 DWORD* pos;
6024 int i;
6026 /* Find the pixels that have changed, and mark them as opaque. */
6027 pos = (DWORD*)graphics->temp_bits;
6028 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6030 if (*pos != DC_BACKGROUND_KEY)
6032 *pos |= 0xff000000;
6034 pos++;
6037 /* Write the changed pixels to the real target. */
6038 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6039 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6040 graphics->temp_hbitmap_width * 4);
6042 /* Clean up. */
6043 DeleteDC(graphics->temp_hdc);
6044 DeleteObject(graphics->temp_hbitmap);
6045 graphics->temp_hdc = NULL;
6046 graphics->temp_hbitmap = NULL;
6048 else if (hdc != graphics->hdc)
6050 stat = InvalidParameter;
6053 if (stat == Ok)
6054 graphics->busy = FALSE;
6056 return stat;
6059 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6061 GpRegion *clip;
6062 GpStatus status;
6064 TRACE("(%p, %p)\n", graphics, region);
6066 if(!graphics || !region)
6067 return InvalidParameter;
6069 if(graphics->busy)
6070 return ObjectBusy;
6072 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6073 return status;
6075 /* free everything except root node and header */
6076 delete_element(&region->node);
6077 memcpy(region, clip, sizeof(GpRegion));
6078 GdipFree(clip);
6080 return Ok;
6083 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6084 GpCoordinateSpace src_space, GpMatrix *matrix)
6086 GpStatus stat = Ok;
6087 REAL scale_x, scale_y;
6089 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6091 if (dst_space != src_space)
6093 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6094 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6096 if(graphics->unit != UnitDisplay)
6098 scale_x *= graphics->scale;
6099 scale_y *= graphics->scale;
6102 /* transform from src_space to CoordinateSpacePage */
6103 switch (src_space)
6105 case CoordinateSpaceWorld:
6106 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6107 break;
6108 case CoordinateSpacePage:
6109 break;
6110 case CoordinateSpaceDevice:
6111 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6112 break;
6115 /* transform from CoordinateSpacePage to dst_space */
6116 switch (dst_space)
6118 case CoordinateSpaceWorld:
6120 GpMatrix inverted_transform = graphics->worldtrans;
6121 stat = GdipInvertMatrix(&inverted_transform);
6122 if (stat == Ok)
6123 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6124 break;
6126 case CoordinateSpacePage:
6127 break;
6128 case CoordinateSpaceDevice:
6129 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6130 break;
6133 return stat;
6136 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6137 GpCoordinateSpace src_space, GpPointF *points, INT count)
6139 GpMatrix matrix;
6140 GpStatus stat;
6142 if(!graphics || !points || count <= 0)
6143 return InvalidParameter;
6145 if(graphics->busy)
6146 return ObjectBusy;
6148 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6150 if (src_space == dst_space) return Ok;
6152 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6153 if (stat != Ok) return stat;
6155 return GdipTransformMatrixPoints(&matrix, points, count);
6158 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6159 GpCoordinateSpace src_space, GpPoint *points, INT count)
6161 GpPointF *pointsF;
6162 GpStatus ret;
6163 INT i;
6165 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6167 if(count <= 0)
6168 return InvalidParameter;
6170 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6171 if(!pointsF)
6172 return OutOfMemory;
6174 for(i = 0; i < count; i++){
6175 pointsF[i].X = (REAL)points[i].X;
6176 pointsF[i].Y = (REAL)points[i].Y;
6179 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6181 if(ret == Ok)
6182 for(i = 0; i < count; i++){
6183 points[i].X = gdip_round(pointsF[i].X);
6184 points[i].Y = gdip_round(pointsF[i].Y);
6186 GdipFree(pointsF);
6188 return ret;
6191 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6193 static int calls;
6195 TRACE("\n");
6197 if (!calls++)
6198 FIXME("stub\n");
6200 return NULL;
6203 /*****************************************************************************
6204 * GdipTranslateClip [GDIPLUS.@]
6206 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6208 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6210 if(!graphics)
6211 return InvalidParameter;
6213 if(graphics->busy)
6214 return ObjectBusy;
6216 return GdipTranslateRegion(graphics->clip, dx, dy);
6219 /*****************************************************************************
6220 * GdipTranslateClipI [GDIPLUS.@]
6222 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6224 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6226 if(!graphics)
6227 return InvalidParameter;
6229 if(graphics->busy)
6230 return ObjectBusy;
6232 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6236 /*****************************************************************************
6237 * GdipMeasureDriverString [GDIPLUS.@]
6239 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6240 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6241 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6243 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6244 HFONT hfont;
6245 HDC hdc;
6246 REAL min_x, min_y, max_x, max_y, x, y;
6247 int i;
6248 TEXTMETRICW textmetric;
6249 const WORD *glyph_indices;
6250 WORD *dynamic_glyph_indices=NULL;
6251 REAL rel_width, rel_height, ascent, descent;
6252 GpPointF pt[3];
6254 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6256 if (!graphics || !text || !font || !positions || !boundingBox)
6257 return InvalidParameter;
6259 if (length == -1)
6260 length = strlenW(text);
6262 if (length == 0)
6264 boundingBox->X = 0.0;
6265 boundingBox->Y = 0.0;
6266 boundingBox->Width = 0.0;
6267 boundingBox->Height = 0.0;
6270 if (flags & unsupported_flags)
6271 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6273 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6275 hdc = CreateCompatibleDC(0);
6276 SelectObject(hdc, hfont);
6278 GetTextMetricsW(hdc, &textmetric);
6280 pt[0].X = 0.0;
6281 pt[0].Y = 0.0;
6282 pt[1].X = 1.0;
6283 pt[1].Y = 0.0;
6284 pt[2].X = 0.0;
6285 pt[2].Y = 1.0;
6286 if (matrix)
6288 GpMatrix xform = *matrix;
6289 GdipTransformMatrixPoints(&xform, pt, 3);
6291 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6292 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6293 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6294 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6295 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6297 if (flags & DriverStringOptionsCmapLookup)
6299 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6300 if (!glyph_indices)
6302 DeleteDC(hdc);
6303 DeleteObject(hfont);
6304 return OutOfMemory;
6307 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6309 else
6310 glyph_indices = text;
6312 min_x = max_x = x = positions[0].X;
6313 min_y = max_y = y = positions[0].Y;
6315 ascent = textmetric.tmAscent / rel_height;
6316 descent = textmetric.tmDescent / rel_height;
6318 for (i=0; i<length; i++)
6320 int char_width;
6321 ABC abc;
6323 if (!(flags & DriverStringOptionsRealizedAdvance))
6325 x = positions[i].X;
6326 y = positions[i].Y;
6329 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6330 char_width = abc.abcA + abc.abcB + abc.abcC;
6332 if (min_y > y - ascent) min_y = y - ascent;
6333 if (max_y < y + descent) max_y = y + descent;
6334 if (min_x > x) min_x = x;
6336 x += char_width / rel_width;
6338 if (max_x < x) max_x = x;
6341 GdipFree(dynamic_glyph_indices);
6342 DeleteDC(hdc);
6343 DeleteObject(hfont);
6345 boundingBox->X = min_x;
6346 boundingBox->Y = min_y;
6347 boundingBox->Width = max_x - min_x;
6348 boundingBox->Height = max_y - min_y;
6350 return Ok;
6353 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6354 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6355 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6356 INT flags, GDIPCONST GpMatrix *matrix)
6358 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6359 INT save_state;
6360 GpPointF pt;
6361 HFONT hfont;
6362 UINT eto_flags=0;
6364 if (flags & unsupported_flags)
6365 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6367 if (!(flags & DriverStringOptionsCmapLookup))
6368 eto_flags |= ETO_GLYPH_INDEX;
6370 save_state = SaveDC(graphics->hdc);
6371 SetBkMode(graphics->hdc, TRANSPARENT);
6372 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6374 pt = positions[0];
6375 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6377 get_font_hfont(graphics, font, format, &hfont, matrix);
6378 SelectObject(graphics->hdc, hfont);
6380 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6382 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6384 RestoreDC(graphics->hdc, save_state);
6386 DeleteObject(hfont);
6388 return Ok;
6391 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6392 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6393 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6394 INT flags, GDIPCONST GpMatrix *matrix)
6396 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6397 GpStatus stat;
6398 PointF *real_positions, real_position;
6399 POINT *pti;
6400 HFONT hfont;
6401 HDC hdc;
6402 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6403 DWORD max_glyphsize=0;
6404 GLYPHMETRICS glyphmetrics;
6405 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6406 BYTE *glyph_mask;
6407 BYTE *text_mask;
6408 int text_mask_stride;
6409 BYTE *pixel_data;
6410 int pixel_data_stride;
6411 GpRect pixel_area;
6412 UINT ggo_flags = GGO_GRAY8_BITMAP;
6414 if (length <= 0)
6415 return Ok;
6417 if (!(flags & DriverStringOptionsCmapLookup))
6418 ggo_flags |= GGO_GLYPH_INDEX;
6420 if (flags & unsupported_flags)
6421 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6423 pti = GdipAlloc(sizeof(POINT) * length);
6424 if (!pti)
6425 return OutOfMemory;
6427 if (flags & DriverStringOptionsRealizedAdvance)
6429 real_position = positions[0];
6431 transform_and_round_points(graphics, pti, &real_position, 1);
6433 else
6435 real_positions = GdipAlloc(sizeof(PointF) * length);
6436 if (!real_positions)
6438 GdipFree(pti);
6439 return OutOfMemory;
6442 memcpy(real_positions, positions, sizeof(PointF) * length);
6444 transform_and_round_points(graphics, pti, real_positions, length);
6446 GdipFree(real_positions);
6449 get_font_hfont(graphics, font, format, &hfont, matrix);
6451 hdc = CreateCompatibleDC(0);
6452 SelectObject(hdc, hfont);
6454 /* Get the boundaries of the text to be drawn */
6455 for (i=0; i<length; i++)
6457 DWORD glyphsize;
6458 int left, top, right, bottom;
6460 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6461 &glyphmetrics, 0, NULL, &identity);
6463 if (glyphsize == GDI_ERROR)
6465 ERR("GetGlyphOutlineW failed\n");
6466 GdipFree(pti);
6467 DeleteDC(hdc);
6468 DeleteObject(hfont);
6469 return GenericError;
6472 if (glyphsize > max_glyphsize)
6473 max_glyphsize = glyphsize;
6475 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6476 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6477 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6478 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6480 if (left < min_x) min_x = left;
6481 if (top < min_y) min_y = top;
6482 if (right > max_x) max_x = right;
6483 if (bottom > max_y) max_y = bottom;
6485 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6487 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6488 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6492 glyph_mask = GdipAlloc(max_glyphsize);
6493 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6494 text_mask_stride = max_x - min_x;
6496 if (!(glyph_mask && text_mask))
6498 GdipFree(glyph_mask);
6499 GdipFree(text_mask);
6500 GdipFree(pti);
6501 DeleteDC(hdc);
6502 DeleteObject(hfont);
6503 return OutOfMemory;
6506 /* Generate a mask for the text */
6507 for (i=0; i<length; i++)
6509 int left, top, stride;
6511 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6512 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6514 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6515 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6516 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6518 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6520 BYTE *glyph_val = glyph_mask + y * stride;
6521 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6522 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6524 *text_val = min(64, *text_val + *glyph_val);
6525 glyph_val++;
6526 text_val++;
6531 GdipFree(pti);
6532 DeleteDC(hdc);
6533 DeleteObject(hfont);
6534 GdipFree(glyph_mask);
6536 /* get the brush data */
6537 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6538 if (!pixel_data)
6540 GdipFree(text_mask);
6541 return OutOfMemory;
6544 pixel_area.X = min_x;
6545 pixel_area.Y = min_y;
6546 pixel_area.Width = max_x - min_x;
6547 pixel_area.Height = max_y - min_y;
6548 pixel_data_stride = pixel_area.Width * 4;
6550 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6551 if (stat != Ok)
6553 GdipFree(text_mask);
6554 GdipFree(pixel_data);
6555 return stat;
6558 /* multiply the brush data by the mask */
6559 for (y=0; y<pixel_area.Height; y++)
6561 BYTE *text_val = text_mask + text_mask_stride * y;
6562 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6563 for (x=0; x<pixel_area.Width; x++)
6565 *pixel_val = (*pixel_val) * (*text_val) / 64;
6566 text_val++;
6567 pixel_val+=4;
6571 GdipFree(text_mask);
6573 /* draw the result */
6574 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6575 pixel_area.Height, pixel_data_stride);
6577 GdipFree(pixel_data);
6579 return stat;
6582 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6583 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6584 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6585 INT flags, GDIPCONST GpMatrix *matrix)
6587 GpStatus stat = NotImplemented;
6589 if (length == -1)
6590 length = strlenW(text);
6592 if (graphics->hdc && !graphics->alpha_hdc &&
6593 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6594 brush->bt == BrushTypeSolidColor &&
6595 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6596 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6597 brush, positions, flags, matrix);
6598 if (stat == NotImplemented)
6599 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6600 brush, positions, flags, matrix);
6601 return stat;
6604 /*****************************************************************************
6605 * GdipDrawDriverString [GDIPLUS.@]
6607 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6608 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6609 GDIPCONST PointF *positions, INT flags,
6610 GDIPCONST GpMatrix *matrix )
6612 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6614 if (!graphics || !text || !font || !brush || !positions)
6615 return InvalidParameter;
6617 return draw_driver_string(graphics, text, length, font, NULL,
6618 brush, positions, flags, matrix);
6621 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6622 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6624 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6625 return NotImplemented;
6628 /*****************************************************************************
6629 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6631 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6633 GpStatus stat;
6634 GpRegion* rgn;
6636 TRACE("(%p, %p)\n", graphics, res);
6638 if((stat = GdipCreateRegion(&rgn)) != Ok)
6639 return stat;
6641 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6642 goto cleanup;
6644 stat = GdipIsEmptyRegion(rgn, graphics, res);
6646 cleanup:
6647 GdipDeleteRegion(rgn);
6648 return stat;
6651 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6653 static int calls;
6655 TRACE("(%p) stub\n", graphics);
6657 if(!(calls++))
6658 FIXME("not implemented\n");
6660 return NotImplemented;