gdiplus: Always use AlphaBlend to draw to 32-bit DIB's.
[wine.git] / dlls / gdiplus / graphics.c
blob4474db600016ef381eead3cdf66dbae7ea903d2a
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 int i, size;
433 RGNDATA *rgndata;
434 RECT *rects;
435 HRGN hrgn, visible_rgn;
437 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
438 if (!hrgn)
439 return OutOfMemory;
441 stat = get_clip_hrgn(graphics, &visible_rgn);
442 if (stat != Ok)
444 DeleteObject(hrgn);
445 return stat;
448 if (visible_rgn)
450 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
451 DeleteObject(visible_rgn);
454 if (hregion)
455 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
457 size = GetRegionData(hrgn, 0, NULL);
459 rgndata = GdipAlloc(size);
460 if (!rgndata)
462 DeleteObject(hrgn);
463 return OutOfMemory;
466 GetRegionData(hrgn, size, rgndata);
468 rects = (RECT*)&rgndata->Buffer;
470 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
472 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
473 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
474 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
475 src_stride);
478 GdipFree(rgndata);
480 DeleteObject(hrgn);
482 return stat;
484 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
486 ERR("This should not be used for metafiles; fix caller\n");
487 return NotImplemented;
489 else
491 HRGN hrgn;
492 int save;
494 stat = get_clip_hrgn(graphics, &hrgn);
496 if (stat != Ok)
497 return stat;
499 save = SaveDC(graphics->hdc);
501 if (hrgn)
502 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
504 if (hregion)
505 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
507 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
508 src_height, src_stride);
510 RestoreDC(graphics->hdc, save);
512 DeleteObject(hrgn);
514 return stat;
518 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
519 const BYTE *src, INT src_width, INT src_height, INT src_stride)
521 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL);
524 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
526 ARGB result=0;
527 ARGB i;
528 INT a1, a2, a3;
530 a1 = (start >> 24) & 0xff;
531 a2 = (end >> 24) & 0xff;
533 a3 = (int)(a1*(1.0f - position)+a2*(position));
535 result |= a3 << 24;
537 for (i=0xff; i<=0xff0000; i = i << 8)
538 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
539 return result;
542 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
544 REAL blendfac;
546 /* clamp to between 0.0 and 1.0, using the wrap mode */
547 if (brush->wrap == WrapModeTile)
549 position = fmodf(position, 1.0f);
550 if (position < 0.0f) position += 1.0f;
552 else /* WrapModeFlip* */
554 position = fmodf(position, 2.0f);
555 if (position < 0.0f) position += 2.0f;
556 if (position > 1.0f) position = 2.0f - position;
559 if (brush->blendcount == 1)
560 blendfac = position;
561 else
563 int i=1;
564 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
565 REAL range;
567 /* locate the blend positions surrounding this position */
568 while (position > brush->blendpos[i])
569 i++;
571 /* interpolate between the blend positions */
572 left_blendpos = brush->blendpos[i-1];
573 left_blendfac = brush->blendfac[i-1];
574 right_blendpos = brush->blendpos[i];
575 right_blendfac = brush->blendfac[i];
576 range = right_blendpos - left_blendpos;
577 blendfac = (left_blendfac * (right_blendpos - position) +
578 right_blendfac * (position - left_blendpos)) / range;
581 if (brush->pblendcount == 0)
582 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
583 else
585 int i=1;
586 ARGB left_blendcolor, right_blendcolor;
587 REAL left_blendpos, right_blendpos;
589 /* locate the blend colors surrounding this position */
590 while (blendfac > brush->pblendpos[i])
591 i++;
593 /* interpolate between the blend colors */
594 left_blendpos = brush->pblendpos[i-1];
595 left_blendcolor = brush->pblendcolor[i-1];
596 right_blendpos = brush->pblendpos[i];
597 right_blendcolor = brush->pblendcolor[i];
598 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
599 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
603 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
605 REAL val[5], res[4];
606 int i, j;
607 unsigned char a, r, g, b;
609 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
610 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
611 val[2] = (color & 0xff) / 255.0; /* blue */
612 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
613 val[4] = 1.0; /* translation */
615 for (i=0; i<4; i++)
617 res[i] = 0.0;
619 for (j=0; j<5; j++)
620 res[i] += matrix->m[j][i] * val[j];
623 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
624 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
625 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
626 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
628 return (a << 24) | (r << 16) | (g << 8) | b;
631 static int color_is_gray(ARGB color)
633 unsigned char r, g, b;
635 r = (color >> 16) & 0xff;
636 g = (color >> 8) & 0xff;
637 b = color & 0xff;
639 return (r == g) && (g == b);
642 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
643 UINT width, UINT height, INT stride, ColorAdjustType type)
645 UINT x, y, i;
647 if (attributes->colorkeys[type].enabled ||
648 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
650 const struct color_key *key;
651 BYTE min_blue, min_green, min_red;
652 BYTE max_blue, max_green, max_red;
654 if (attributes->colorkeys[type].enabled)
655 key = &attributes->colorkeys[type];
656 else
657 key = &attributes->colorkeys[ColorAdjustTypeDefault];
659 min_blue = key->low&0xff;
660 min_green = (key->low>>8)&0xff;
661 min_red = (key->low>>16)&0xff;
663 max_blue = key->high&0xff;
664 max_green = (key->high>>8)&0xff;
665 max_red = (key->high>>16)&0xff;
667 for (x=0; x<width; x++)
668 for (y=0; y<height; y++)
670 ARGB *src_color;
671 BYTE blue, green, red;
672 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
673 blue = *src_color&0xff;
674 green = (*src_color>>8)&0xff;
675 red = (*src_color>>16)&0xff;
676 if (blue >= min_blue && green >= min_green && red >= min_red &&
677 blue <= max_blue && green <= max_green && red <= max_red)
678 *src_color = 0x00000000;
682 if (attributes->colorremaptables[type].enabled ||
683 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
685 const struct color_remap_table *table;
687 if (attributes->colorremaptables[type].enabled)
688 table = &attributes->colorremaptables[type];
689 else
690 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
692 for (x=0; x<width; x++)
693 for (y=0; y<height; y++)
695 ARGB *src_color;
696 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
697 for (i=0; i<table->mapsize; i++)
699 if (*src_color == table->colormap[i].oldColor.Argb)
701 *src_color = table->colormap[i].newColor.Argb;
702 break;
708 if (attributes->colormatrices[type].enabled ||
709 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
711 const struct color_matrix *colormatrices;
713 if (attributes->colormatrices[type].enabled)
714 colormatrices = &attributes->colormatrices[type];
715 else
716 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
718 for (x=0; x<width; x++)
719 for (y=0; y<height; y++)
721 ARGB *src_color;
722 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
724 if (colormatrices->flags == ColorMatrixFlagsDefault ||
725 !color_is_gray(*src_color))
727 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
729 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
731 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
736 if (attributes->gamma_enabled[type] ||
737 attributes->gamma_enabled[ColorAdjustTypeDefault])
739 REAL gamma;
741 if (attributes->gamma_enabled[type])
742 gamma = attributes->gamma[type];
743 else
744 gamma = attributes->gamma[ColorAdjustTypeDefault];
746 for (x=0; x<width; x++)
747 for (y=0; y<height; y++)
749 ARGB *src_color;
750 BYTE blue, green, red;
751 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
753 blue = *src_color&0xff;
754 green = (*src_color>>8)&0xff;
755 red = (*src_color>>16)&0xff;
757 /* FIXME: We should probably use a table for this. */
758 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
759 green = floorf(powf(green / 255.0, gamma) * 255.0);
760 red = floorf(powf(red / 255.0, gamma) * 255.0);
762 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
767 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
768 * bitmap that contains all the pixels we may need to draw it. */
769 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
770 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
771 GpRect *rect)
773 INT left, top, right, bottom;
775 switch (interpolation)
777 case InterpolationModeHighQualityBilinear:
778 case InterpolationModeHighQualityBicubic:
779 /* FIXME: Include a greater range for the prefilter? */
780 case InterpolationModeBicubic:
781 case InterpolationModeBilinear:
782 left = (INT)(floorf(srcx));
783 top = (INT)(floorf(srcy));
784 right = (INT)(ceilf(srcx+srcwidth));
785 bottom = (INT)(ceilf(srcy+srcheight));
786 break;
787 case InterpolationModeNearestNeighbor:
788 default:
789 left = gdip_round(srcx);
790 top = gdip_round(srcy);
791 right = gdip_round(srcx+srcwidth);
792 bottom = gdip_round(srcy+srcheight);
793 break;
796 if (wrap == WrapModeClamp)
798 if (left < 0)
799 left = 0;
800 if (top < 0)
801 top = 0;
802 if (right >= bitmap->width)
803 right = bitmap->width-1;
804 if (bottom >= bitmap->height)
805 bottom = bitmap->height-1;
807 else
809 /* In some cases we can make the rectangle smaller here, but the logic
810 * is hard to get right, and tiling suggests we're likely to use the
811 * entire source image. */
812 if (left < 0 || right >= bitmap->width)
814 left = 0;
815 right = bitmap->width-1;
818 if (top < 0 || bottom >= bitmap->height)
820 top = 0;
821 bottom = bitmap->height-1;
825 rect->X = left;
826 rect->Y = top;
827 rect->Width = right - left + 1;
828 rect->Height = bottom - top + 1;
831 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
832 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
834 if (attributes->wrap == WrapModeClamp)
836 if (x < 0 || y < 0 || x >= width || y >= height)
837 return attributes->outside_color;
839 else
841 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
842 if (x < 0)
843 x = width*2 + x % (width * 2);
844 if (y < 0)
845 y = height*2 + y % (height * 2);
847 if ((attributes->wrap & 1) == 1)
849 /* Flip X */
850 if ((x / width) % 2 == 0)
851 x = x % width;
852 else
853 x = width - 1 - x % width;
855 else
856 x = x % width;
858 if ((attributes->wrap & 2) == 2)
860 /* Flip Y */
861 if ((y / height) % 2 == 0)
862 y = y % height;
863 else
864 y = height - 1 - y % height;
866 else
867 y = y % height;
870 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
872 ERR("out of range pixel requested\n");
873 return 0xffcd0084;
876 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
879 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
880 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
881 InterpolationMode interpolation, PixelOffsetMode offset_mode)
883 static int fixme;
885 switch (interpolation)
887 default:
888 if (!fixme++)
889 FIXME("Unimplemented interpolation %i\n", interpolation);
890 /* fall-through */
891 case InterpolationModeBilinear:
893 REAL leftxf, topyf;
894 INT leftx, rightx, topy, bottomy;
895 ARGB topleft, topright, bottomleft, bottomright;
896 ARGB top, bottom;
897 float x_offset;
899 leftxf = floorf(point->X);
900 leftx = (INT)leftxf;
901 rightx = (INT)ceilf(point->X);
902 topyf = floorf(point->Y);
903 topy = (INT)topyf;
904 bottomy = (INT)ceilf(point->Y);
906 if (leftx == rightx && topy == bottomy)
907 return sample_bitmap_pixel(src_rect, bits, width, height,
908 leftx, topy, attributes);
910 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
911 leftx, topy, attributes);
912 topright = sample_bitmap_pixel(src_rect, bits, width, height,
913 rightx, topy, attributes);
914 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
915 leftx, bottomy, attributes);
916 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
917 rightx, bottomy, attributes);
919 x_offset = point->X - leftxf;
920 top = blend_colors(topleft, topright, x_offset);
921 bottom = blend_colors(bottomleft, bottomright, x_offset);
923 return blend_colors(top, bottom, point->Y - topyf);
925 case InterpolationModeNearestNeighbor:
927 FLOAT pixel_offset;
928 switch (offset_mode)
930 default:
931 case PixelOffsetModeNone:
932 case PixelOffsetModeHighSpeed:
933 pixel_offset = 0.5;
934 break;
936 case PixelOffsetModeHalf:
937 case PixelOffsetModeHighQuality:
938 pixel_offset = 0.0;
939 break;
941 return sample_bitmap_pixel(src_rect, bits, width, height,
942 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
948 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
950 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
953 static INT brush_can_fill_path(GpBrush *brush)
955 switch (brush->bt)
957 case BrushTypeSolidColor:
958 return 1;
959 case BrushTypeHatchFill:
961 GpHatch *hatch = (GpHatch*)brush;
962 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
963 ((hatch->backcol & 0xff000000) == 0xff000000);
965 case BrushTypeLinearGradient:
966 case BrushTypeTextureFill:
967 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
968 default:
969 return 0;
973 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
975 switch (brush->bt)
977 case BrushTypeSolidColor:
979 GpSolidFill *fill = (GpSolidFill*)brush;
980 HBITMAP bmp = ARGB2BMP(fill->color);
982 if (bmp)
984 RECT rc;
985 /* partially transparent fill */
987 SelectClipPath(graphics->hdc, RGN_AND);
988 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
990 HDC hdc = CreateCompatibleDC(NULL);
992 if (!hdc) break;
994 SelectObject(hdc, bmp);
995 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
996 hdc, 0, 0, 1, 1);
997 DeleteDC(hdc);
1000 DeleteObject(bmp);
1001 break;
1003 /* else fall through */
1005 default:
1007 HBRUSH gdibrush, old_brush;
1009 gdibrush = create_gdi_brush(brush);
1010 if (!gdibrush) return;
1012 old_brush = SelectObject(graphics->hdc, gdibrush);
1013 FillPath(graphics->hdc);
1014 SelectObject(graphics->hdc, old_brush);
1015 DeleteObject(gdibrush);
1016 break;
1021 static INT brush_can_fill_pixels(GpBrush *brush)
1023 switch (brush->bt)
1025 case BrushTypeSolidColor:
1026 case BrushTypeHatchFill:
1027 case BrushTypeLinearGradient:
1028 case BrushTypeTextureFill:
1029 case BrushTypePathGradient:
1030 return 1;
1031 default:
1032 return 0;
1036 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1037 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1039 switch (brush->bt)
1041 case BrushTypeSolidColor:
1043 int x, y;
1044 GpSolidFill *fill = (GpSolidFill*)brush;
1045 for (x=0; x<fill_area->Width; x++)
1046 for (y=0; y<fill_area->Height; y++)
1047 argb_pixels[x + y*cdwStride] = fill->color;
1048 return Ok;
1050 case BrushTypeHatchFill:
1052 int x, y;
1053 GpHatch *fill = (GpHatch*)brush;
1054 const char *hatch_data;
1056 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1057 return NotImplemented;
1059 for (x=0; x<fill_area->Width; x++)
1060 for (y=0; y<fill_area->Height; y++)
1062 int hx, hy;
1064 /* FIXME: Account for the rendering origin */
1065 hx = (x + fill_area->X) % 8;
1066 hy = (y + fill_area->Y) % 8;
1068 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1069 argb_pixels[x + y*cdwStride] = fill->forecol;
1070 else
1071 argb_pixels[x + y*cdwStride] = fill->backcol;
1074 return Ok;
1076 case BrushTypeLinearGradient:
1078 GpLineGradient *fill = (GpLineGradient*)brush;
1079 GpPointF draw_points[3], line_points[3];
1080 GpStatus stat;
1081 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1082 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1083 int x, y;
1085 draw_points[0].X = fill_area->X;
1086 draw_points[0].Y = fill_area->Y;
1087 draw_points[1].X = fill_area->X+1;
1088 draw_points[1].Y = fill_area->Y;
1089 draw_points[2].X = fill_area->X;
1090 draw_points[2].Y = fill_area->Y+1;
1092 /* Transform the points to a co-ordinate space where X is the point's
1093 * position in the gradient, 0.0 being the start point and 1.0 the
1094 * end point. */
1095 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1096 CoordinateSpaceDevice, draw_points, 3);
1098 if (stat == Ok)
1100 line_points[0] = fill->startpoint;
1101 line_points[1] = fill->endpoint;
1102 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1103 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1105 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1108 if (stat == Ok)
1110 stat = GdipInvertMatrix(world_to_gradient);
1112 if (stat == Ok)
1113 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1115 GdipDeleteMatrix(world_to_gradient);
1118 if (stat == Ok)
1120 REAL x_delta = draw_points[1].X - draw_points[0].X;
1121 REAL y_delta = draw_points[2].X - draw_points[0].X;
1123 for (y=0; y<fill_area->Height; y++)
1125 for (x=0; x<fill_area->Width; x++)
1127 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1129 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1134 return stat;
1136 case BrushTypeTextureFill:
1138 GpTexture *fill = (GpTexture*)brush;
1139 GpPointF draw_points[3];
1140 GpStatus stat;
1141 int x, y;
1142 GpBitmap *bitmap;
1143 int src_stride;
1144 GpRect src_area;
1146 if (fill->image->type != ImageTypeBitmap)
1148 FIXME("metafile texture brushes not implemented\n");
1149 return NotImplemented;
1152 bitmap = (GpBitmap*)fill->image;
1153 src_stride = sizeof(ARGB) * bitmap->width;
1155 src_area.X = src_area.Y = 0;
1156 src_area.Width = bitmap->width;
1157 src_area.Height = bitmap->height;
1159 draw_points[0].X = fill_area->X;
1160 draw_points[0].Y = fill_area->Y;
1161 draw_points[1].X = fill_area->X+1;
1162 draw_points[1].Y = fill_area->Y;
1163 draw_points[2].X = fill_area->X;
1164 draw_points[2].Y = fill_area->Y+1;
1166 /* Transform the points to the co-ordinate space of the bitmap. */
1167 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1168 CoordinateSpaceDevice, draw_points, 3);
1170 if (stat == Ok)
1172 GpMatrix world_to_texture = fill->transform;
1174 stat = GdipInvertMatrix(&world_to_texture);
1175 if (stat == Ok)
1176 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1179 if (stat == Ok && !fill->bitmap_bits)
1181 BitmapData lockeddata;
1183 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1184 if (!fill->bitmap_bits)
1185 stat = OutOfMemory;
1187 if (stat == Ok)
1189 lockeddata.Width = bitmap->width;
1190 lockeddata.Height = bitmap->height;
1191 lockeddata.Stride = src_stride;
1192 lockeddata.PixelFormat = PixelFormat32bppARGB;
1193 lockeddata.Scan0 = fill->bitmap_bits;
1195 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1196 PixelFormat32bppARGB, &lockeddata);
1199 if (stat == Ok)
1200 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1202 if (stat == Ok)
1203 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1204 bitmap->width, bitmap->height,
1205 src_stride, ColorAdjustTypeBitmap);
1207 if (stat != Ok)
1209 GdipFree(fill->bitmap_bits);
1210 fill->bitmap_bits = NULL;
1214 if (stat == Ok)
1216 REAL x_dx = draw_points[1].X - draw_points[0].X;
1217 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1218 REAL y_dx = draw_points[2].X - draw_points[0].X;
1219 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1221 for (y=0; y<fill_area->Height; y++)
1223 for (x=0; x<fill_area->Width; x++)
1225 GpPointF point;
1226 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1227 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1229 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1230 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1231 &point, fill->imageattributes, graphics->interpolation,
1232 graphics->pixeloffset);
1237 return stat;
1239 case BrushTypePathGradient:
1241 GpPathGradient *fill = (GpPathGradient*)brush;
1242 GpPath *flat_path;
1243 GpMatrix world_to_device;
1244 GpStatus stat;
1245 int i, figure_start=0;
1246 GpPointF start_point, end_point, center_point;
1247 BYTE type;
1248 REAL min_yf, max_yf, line1_xf, line2_xf;
1249 INT min_y, max_y, min_x, max_x;
1250 INT x, y;
1251 ARGB outer_color;
1252 static int transform_fixme_once;
1254 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1256 static int once;
1257 if (!once++)
1258 FIXME("path gradient focus not implemented\n");
1261 if (fill->gamma)
1263 static int once;
1264 if (!once++)
1265 FIXME("path gradient gamma correction not implemented\n");
1268 if (fill->blendcount)
1270 static int once;
1271 if (!once++)
1272 FIXME("path gradient blend not implemented\n");
1275 if (fill->pblendcount)
1277 static int once;
1278 if (!once++)
1279 FIXME("path gradient preset blend not implemented\n");
1282 if (!transform_fixme_once)
1284 BOOL is_identity=TRUE;
1285 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1286 if (!is_identity)
1288 FIXME("path gradient transform not implemented\n");
1289 transform_fixme_once = 1;
1293 stat = GdipClonePath(fill->path, &flat_path);
1295 if (stat != Ok)
1296 return stat;
1298 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1299 CoordinateSpaceWorld, &world_to_device);
1300 if (stat == Ok)
1302 stat = GdipTransformPath(flat_path, &world_to_device);
1304 if (stat == Ok)
1306 center_point = fill->center;
1307 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1310 if (stat == Ok)
1311 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1314 if (stat != Ok)
1316 GdipDeletePath(flat_path);
1317 return stat;
1320 for (i=0; i<flat_path->pathdata.Count; i++)
1322 int start_center_line=0, end_center_line=0;
1323 int seen_start=0, seen_end=0, seen_center=0;
1324 REAL center_distance;
1325 ARGB start_color, end_color;
1326 REAL dy, dx;
1328 type = flat_path->pathdata.Types[i];
1330 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1331 figure_start = i;
1333 start_point = flat_path->pathdata.Points[i];
1335 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1337 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1339 end_point = flat_path->pathdata.Points[figure_start];
1340 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1342 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1344 end_point = flat_path->pathdata.Points[i+1];
1345 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1347 else
1348 continue;
1350 outer_color = start_color;
1352 min_yf = center_point.Y;
1353 if (min_yf > start_point.Y) min_yf = start_point.Y;
1354 if (min_yf > end_point.Y) min_yf = end_point.Y;
1356 if (min_yf < fill_area->Y)
1357 min_y = fill_area->Y;
1358 else
1359 min_y = (INT)ceil(min_yf);
1361 max_yf = center_point.Y;
1362 if (max_yf < start_point.Y) max_yf = start_point.Y;
1363 if (max_yf < end_point.Y) max_yf = end_point.Y;
1365 if (max_yf > fill_area->Y + fill_area->Height)
1366 max_y = fill_area->Y + fill_area->Height;
1367 else
1368 max_y = (INT)ceil(max_yf);
1370 dy = end_point.Y - start_point.Y;
1371 dx = end_point.X - start_point.X;
1373 /* This is proportional to the distance from start-end line to center point. */
1374 center_distance = dy * (start_point.X - center_point.X) +
1375 dx * (center_point.Y - start_point.Y);
1377 for (y=min_y; y<max_y; y++)
1379 REAL yf = (REAL)y;
1381 if (!seen_start && yf >= start_point.Y)
1383 seen_start = 1;
1384 start_center_line ^= 1;
1386 if (!seen_end && yf >= end_point.Y)
1388 seen_end = 1;
1389 end_center_line ^= 1;
1391 if (!seen_center && yf >= center_point.Y)
1393 seen_center = 1;
1394 start_center_line ^= 1;
1395 end_center_line ^= 1;
1398 if (start_center_line)
1399 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1400 else
1401 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1403 if (end_center_line)
1404 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1405 else
1406 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1408 if (line1_xf < line2_xf)
1410 min_x = (INT)ceil(line1_xf);
1411 max_x = (INT)ceil(line2_xf);
1413 else
1415 min_x = (INT)ceil(line2_xf);
1416 max_x = (INT)ceil(line1_xf);
1419 if (min_x < fill_area->X)
1420 min_x = fill_area->X;
1421 if (max_x > fill_area->X + fill_area->Width)
1422 max_x = fill_area->X + fill_area->Width;
1424 for (x=min_x; x<max_x; x++)
1426 REAL xf = (REAL)x;
1427 REAL distance;
1429 if (start_color != end_color)
1431 REAL blend_amount, pdy, pdx;
1432 pdy = yf - center_point.Y;
1433 pdx = xf - center_point.X;
1434 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1435 outer_color = blend_colors(start_color, end_color, blend_amount);
1438 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1439 (end_point.X - start_point.X) * (yf - start_point.Y);
1441 distance = distance / center_distance;
1443 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1444 blend_colors(outer_color, fill->centercolor, distance);
1449 GdipDeletePath(flat_path);
1450 return stat;
1452 default:
1453 return NotImplemented;
1457 /* GdipDrawPie/GdipFillPie helper function */
1458 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1459 REAL height, REAL startAngle, REAL sweepAngle)
1461 GpPointF ptf[4];
1462 POINT pti[4];
1464 ptf[0].X = x;
1465 ptf[0].Y = y;
1466 ptf[1].X = x + width;
1467 ptf[1].Y = y + height;
1469 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1470 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1472 transform_and_round_points(graphics, pti, ptf, 4);
1474 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1475 pti[2].y, pti[3].x, pti[3].y);
1478 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1479 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1480 * should not be called on an hdc that has a path you care about. */
1481 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1482 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1484 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1485 GpMatrix matrix;
1486 HBRUSH brush = NULL;
1487 HPEN pen = NULL;
1488 PointF ptf[4], *custptf = NULL;
1489 POINT pt[4], *custpt = NULL;
1490 BYTE *tp = NULL;
1491 REAL theta, dsmall, dbig, dx, dy = 0.0;
1492 INT i, count;
1493 LOGBRUSH lb;
1494 BOOL customstroke;
1496 if((x1 == x2) && (y1 == y2))
1497 return;
1499 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1501 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1502 if(!customstroke){
1503 brush = CreateSolidBrush(color);
1504 lb.lbStyle = BS_SOLID;
1505 lb.lbColor = color;
1506 lb.lbHatch = 0;
1507 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1508 PS_JOIN_MITER, 1, &lb, 0,
1509 NULL);
1510 oldbrush = SelectObject(graphics->hdc, brush);
1511 oldpen = SelectObject(graphics->hdc, pen);
1514 switch(cap){
1515 case LineCapFlat:
1516 break;
1517 case LineCapSquare:
1518 case LineCapSquareAnchor:
1519 case LineCapDiamondAnchor:
1520 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1521 if(cap == LineCapDiamondAnchor){
1522 dsmall = cos(theta + M_PI_2) * size;
1523 dbig = sin(theta + M_PI_2) * size;
1525 else{
1526 dsmall = cos(theta + M_PI_4) * size;
1527 dbig = sin(theta + M_PI_4) * size;
1530 ptf[0].X = x2 - dsmall;
1531 ptf[1].X = x2 + dbig;
1533 ptf[0].Y = y2 - dbig;
1534 ptf[3].Y = y2 + dsmall;
1536 ptf[1].Y = y2 - dsmall;
1537 ptf[2].Y = y2 + dbig;
1539 ptf[3].X = x2 - dbig;
1540 ptf[2].X = x2 + dsmall;
1542 transform_and_round_points(graphics, pt, ptf, 4);
1543 Polygon(graphics->hdc, pt, 4);
1545 break;
1546 case LineCapArrowAnchor:
1547 size = size * 4.0 / sqrt(3.0);
1549 dx = cos(M_PI / 6.0 + theta) * size;
1550 dy = sin(M_PI / 6.0 + theta) * size;
1552 ptf[0].X = x2 - dx;
1553 ptf[0].Y = y2 - dy;
1555 dx = cos(- M_PI / 6.0 + theta) * size;
1556 dy = sin(- M_PI / 6.0 + theta) * size;
1558 ptf[1].X = x2 - dx;
1559 ptf[1].Y = y2 - dy;
1561 ptf[2].X = x2;
1562 ptf[2].Y = y2;
1564 transform_and_round_points(graphics, pt, ptf, 3);
1565 Polygon(graphics->hdc, pt, 3);
1567 break;
1568 case LineCapRoundAnchor:
1569 dx = dy = ANCHOR_WIDTH * size / 2.0;
1571 ptf[0].X = x2 - dx;
1572 ptf[0].Y = y2 - dy;
1573 ptf[1].X = x2 + dx;
1574 ptf[1].Y = y2 + dy;
1576 transform_and_round_points(graphics, pt, ptf, 2);
1577 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1579 break;
1580 case LineCapTriangle:
1581 size = size / 2.0;
1582 dx = cos(M_PI_2 + theta) * size;
1583 dy = sin(M_PI_2 + theta) * size;
1585 ptf[0].X = x2 - dx;
1586 ptf[0].Y = y2 - dy;
1587 ptf[1].X = x2 + dx;
1588 ptf[1].Y = y2 + dy;
1590 dx = cos(theta) * size;
1591 dy = sin(theta) * size;
1593 ptf[2].X = x2 + dx;
1594 ptf[2].Y = y2 + dy;
1596 transform_and_round_points(graphics, pt, ptf, 3);
1597 Polygon(graphics->hdc, pt, 3);
1599 break;
1600 case LineCapRound:
1601 dx = dy = size / 2.0;
1603 ptf[0].X = x2 - dx;
1604 ptf[0].Y = y2 - dy;
1605 ptf[1].X = x2 + dx;
1606 ptf[1].Y = y2 + dy;
1608 dx = -cos(M_PI_2 + theta) * size;
1609 dy = -sin(M_PI_2 + theta) * size;
1611 ptf[2].X = x2 - dx;
1612 ptf[2].Y = y2 - dy;
1613 ptf[3].X = x2 + dx;
1614 ptf[3].Y = y2 + dy;
1616 transform_and_round_points(graphics, pt, ptf, 4);
1617 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1618 pt[2].y, pt[3].x, pt[3].y);
1620 break;
1621 case LineCapCustom:
1622 if(!custom)
1623 break;
1625 count = custom->pathdata.Count;
1626 custptf = GdipAlloc(count * sizeof(PointF));
1627 custpt = GdipAlloc(count * sizeof(POINT));
1628 tp = GdipAlloc(count);
1630 if(!custptf || !custpt || !tp)
1631 goto custend;
1633 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1635 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1636 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1637 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1638 MatrixOrderAppend);
1639 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1640 GdipTransformMatrixPoints(&matrix, custptf, count);
1642 transform_and_round_points(graphics, custpt, custptf, count);
1644 for(i = 0; i < count; i++)
1645 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1647 if(custom->fill){
1648 BeginPath(graphics->hdc);
1649 PolyDraw(graphics->hdc, custpt, tp, count);
1650 EndPath(graphics->hdc);
1651 StrokeAndFillPath(graphics->hdc);
1653 else
1654 PolyDraw(graphics->hdc, custpt, tp, count);
1656 custend:
1657 GdipFree(custptf);
1658 GdipFree(custpt);
1659 GdipFree(tp);
1660 break;
1661 default:
1662 break;
1665 if(!customstroke){
1666 SelectObject(graphics->hdc, oldbrush);
1667 SelectObject(graphics->hdc, oldpen);
1668 DeleteObject(brush);
1669 DeleteObject(pen);
1673 /* Shortens the line by the given percent by changing x2, y2.
1674 * If percent is > 1.0 then the line will change direction.
1675 * If percent is negative it can lengthen the line. */
1676 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1678 REAL dist, theta, dx, dy;
1680 if((y1 == *y2) && (x1 == *x2))
1681 return;
1683 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1684 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1685 dx = cos(theta) * dist;
1686 dy = sin(theta) * dist;
1688 *x2 = *x2 + dx;
1689 *y2 = *y2 + dy;
1692 /* Shortens the line by the given amount by changing x2, y2.
1693 * If the amount is greater than the distance, the line will become length 0.
1694 * If the amount is negative, it can lengthen the line. */
1695 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1697 REAL dx, dy, percent;
1699 dx = *x2 - x1;
1700 dy = *y2 - y1;
1701 if(dx == 0 && dy == 0)
1702 return;
1704 percent = amt / sqrt(dx * dx + dy * dy);
1705 if(percent >= 1.0){
1706 *x2 = x1;
1707 *y2 = y1;
1708 return;
1711 shorten_line_percent(x1, y1, x2, y2, percent);
1714 /* Draws lines between the given points, and if caps is true then draws an endcap
1715 * at the end of the last line. */
1716 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1717 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1719 POINT *pti = NULL;
1720 GpPointF *ptcopy = NULL;
1721 GpStatus status = GenericError;
1723 if(!count)
1724 return Ok;
1726 pti = GdipAlloc(count * sizeof(POINT));
1727 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1729 if(!pti || !ptcopy){
1730 status = OutOfMemory;
1731 goto end;
1734 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1736 if(caps){
1737 if(pen->endcap == LineCapArrowAnchor)
1738 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1739 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1740 else if((pen->endcap == LineCapCustom) && pen->customend)
1741 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1742 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1743 pen->customend->inset * pen->width);
1745 if(pen->startcap == LineCapArrowAnchor)
1746 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1747 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1748 else if((pen->startcap == LineCapCustom) && pen->customstart)
1749 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1750 &ptcopy[0].X, &ptcopy[0].Y,
1751 pen->customstart->inset * pen->width);
1753 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1754 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1755 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1756 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1759 transform_and_round_points(graphics, pti, ptcopy, count);
1761 if(Polyline(graphics->hdc, pti, count))
1762 status = Ok;
1764 end:
1765 GdipFree(pti);
1766 GdipFree(ptcopy);
1768 return status;
1771 /* Conducts a linear search to find the bezier points that will back off
1772 * the endpoint of the curve by a distance of amt. Linear search works
1773 * better than binary in this case because there are multiple solutions,
1774 * and binary searches often find a bad one. I don't think this is what
1775 * Windows does but short of rendering the bezier without GDI's help it's
1776 * the best we can do. If rev then work from the start of the passed points
1777 * instead of the end. */
1778 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1780 GpPointF origpt[4];
1781 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1782 INT i, first = 0, second = 1, third = 2, fourth = 3;
1784 if(rev){
1785 first = 3;
1786 second = 2;
1787 third = 1;
1788 fourth = 0;
1791 origx = pt[fourth].X;
1792 origy = pt[fourth].Y;
1793 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1795 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1796 /* reset bezier points to original values */
1797 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1798 /* Perform magic on bezier points. Order is important here.*/
1799 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1800 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1801 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1802 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1803 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1804 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1806 dx = pt[fourth].X - origx;
1807 dy = pt[fourth].Y - origy;
1809 diff = sqrt(dx * dx + dy * dy);
1810 percent += 0.0005 * amt;
1814 /* Draws bezier curves between given points, and if caps is true then draws an
1815 * endcap at the end of the last line. */
1816 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1817 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1819 POINT *pti;
1820 GpPointF *ptcopy;
1821 GpStatus status = GenericError;
1823 if(!count)
1824 return Ok;
1826 pti = GdipAlloc(count * sizeof(POINT));
1827 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1829 if(!pti || !ptcopy){
1830 status = OutOfMemory;
1831 goto end;
1834 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1836 if(caps){
1837 if(pen->endcap == LineCapArrowAnchor)
1838 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1839 else if((pen->endcap == LineCapCustom) && pen->customend)
1840 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1841 FALSE);
1843 if(pen->startcap == LineCapArrowAnchor)
1844 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1845 else if((pen->startcap == LineCapCustom) && pen->customstart)
1846 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1848 /* the direction of the line cap is parallel to the direction at the
1849 * end of the bezier (which, if it has been shortened, is not the same
1850 * as the direction from pt[count-2] to pt[count-1]) */
1851 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1852 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1853 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1854 pt[count - 1].X, pt[count - 1].Y);
1856 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1857 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1858 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1861 transform_and_round_points(graphics, pti, ptcopy, count);
1863 PolyBezier(graphics->hdc, pti, count);
1865 status = Ok;
1867 end:
1868 GdipFree(pti);
1869 GdipFree(ptcopy);
1871 return status;
1874 /* Draws a combination of bezier curves and lines between points. */
1875 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1876 GDIPCONST BYTE * types, INT count, BOOL caps)
1878 POINT *pti = GdipAlloc(count * sizeof(POINT));
1879 BYTE *tp = GdipAlloc(count);
1880 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1881 INT i, j;
1882 GpStatus status = GenericError;
1884 if(!count){
1885 status = Ok;
1886 goto end;
1888 if(!pti || !tp || !ptcopy){
1889 status = OutOfMemory;
1890 goto end;
1893 for(i = 1; i < count; i++){
1894 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1895 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1896 || !(types[i + 1] & PathPointTypeBezier)){
1897 ERR("Bad bezier points\n");
1898 goto end;
1900 i += 2;
1904 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1906 /* If we are drawing caps, go through the points and adjust them accordingly,
1907 * and draw the caps. */
1908 if(caps){
1909 switch(types[count - 1] & PathPointTypePathTypeMask){
1910 case PathPointTypeBezier:
1911 if(pen->endcap == LineCapArrowAnchor)
1912 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1913 else if((pen->endcap == LineCapCustom) && pen->customend)
1914 shorten_bezier_amt(&ptcopy[count - 4],
1915 pen->width * pen->customend->inset, FALSE);
1917 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1918 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1919 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1920 pt[count - 1].X, pt[count - 1].Y);
1922 break;
1923 case PathPointTypeLine:
1924 if(pen->endcap == LineCapArrowAnchor)
1925 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1926 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1927 pen->width);
1928 else if((pen->endcap == LineCapCustom) && pen->customend)
1929 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1930 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1931 pen->customend->inset * pen->width);
1933 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1934 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1935 pt[count - 1].Y);
1937 break;
1938 default:
1939 ERR("Bad path last point\n");
1940 goto end;
1943 /* Find start of points */
1944 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1945 == PathPointTypeStart); j++);
1947 switch(types[j] & PathPointTypePathTypeMask){
1948 case PathPointTypeBezier:
1949 if(pen->startcap == LineCapArrowAnchor)
1950 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1951 else if((pen->startcap == LineCapCustom) && pen->customstart)
1952 shorten_bezier_amt(&ptcopy[j - 1],
1953 pen->width * pen->customstart->inset, TRUE);
1955 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1956 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1957 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1958 pt[j - 1].X, pt[j - 1].Y);
1960 break;
1961 case PathPointTypeLine:
1962 if(pen->startcap == LineCapArrowAnchor)
1963 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1964 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1965 pen->width);
1966 else if((pen->startcap == LineCapCustom) && pen->customstart)
1967 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1968 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1969 pen->customstart->inset * pen->width);
1971 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1972 pt[j].X, pt[j].Y, pt[j - 1].X,
1973 pt[j - 1].Y);
1975 break;
1976 default:
1977 ERR("Bad path points\n");
1978 goto end;
1982 transform_and_round_points(graphics, pti, ptcopy, count);
1984 for(i = 0; i < count; i++){
1985 tp[i] = convert_path_point_type(types[i]);
1988 PolyDraw(graphics->hdc, pti, tp, count);
1990 status = Ok;
1992 end:
1993 GdipFree(pti);
1994 GdipFree(ptcopy);
1995 GdipFree(tp);
1997 return status;
2000 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2002 GpStatus result;
2004 BeginPath(graphics->hdc);
2005 result = draw_poly(graphics, NULL, path->pathdata.Points,
2006 path->pathdata.Types, path->pathdata.Count, FALSE);
2007 EndPath(graphics->hdc);
2008 return result;
2011 typedef struct _GraphicsContainerItem {
2012 struct list entry;
2013 GraphicsContainer contid;
2015 SmoothingMode smoothing;
2016 CompositingQuality compqual;
2017 InterpolationMode interpolation;
2018 CompositingMode compmode;
2019 TextRenderingHint texthint;
2020 REAL scale;
2021 GpUnit unit;
2022 PixelOffsetMode pixeloffset;
2023 UINT textcontrast;
2024 GpMatrix worldtrans;
2025 GpRegion* clip;
2026 INT origin_x, origin_y;
2027 } GraphicsContainerItem;
2029 static GpStatus init_container(GraphicsContainerItem** container,
2030 GDIPCONST GpGraphics* graphics){
2031 GpStatus sts;
2033 *container = GdipAlloc(sizeof(GraphicsContainerItem));
2034 if(!(*container))
2035 return OutOfMemory;
2037 (*container)->contid = graphics->contid + 1;
2039 (*container)->smoothing = graphics->smoothing;
2040 (*container)->compqual = graphics->compqual;
2041 (*container)->interpolation = graphics->interpolation;
2042 (*container)->compmode = graphics->compmode;
2043 (*container)->texthint = graphics->texthint;
2044 (*container)->scale = graphics->scale;
2045 (*container)->unit = graphics->unit;
2046 (*container)->textcontrast = graphics->textcontrast;
2047 (*container)->pixeloffset = graphics->pixeloffset;
2048 (*container)->origin_x = graphics->origin_x;
2049 (*container)->origin_y = graphics->origin_y;
2050 (*container)->worldtrans = graphics->worldtrans;
2052 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2053 if(sts != Ok){
2054 GdipFree(*container);
2055 *container = NULL;
2056 return sts;
2059 return Ok;
2062 static void delete_container(GraphicsContainerItem* container)
2064 GdipDeleteRegion(container->clip);
2065 GdipFree(container);
2068 static GpStatus restore_container(GpGraphics* graphics,
2069 GDIPCONST GraphicsContainerItem* container){
2070 GpStatus sts;
2071 GpRegion *newClip;
2073 sts = GdipCloneRegion(container->clip, &newClip);
2074 if(sts != Ok) return sts;
2076 graphics->worldtrans = container->worldtrans;
2078 GdipDeleteRegion(graphics->clip);
2079 graphics->clip = newClip;
2081 graphics->contid = container->contid - 1;
2083 graphics->smoothing = container->smoothing;
2084 graphics->compqual = container->compqual;
2085 graphics->interpolation = container->interpolation;
2086 graphics->compmode = container->compmode;
2087 graphics->texthint = container->texthint;
2088 graphics->scale = container->scale;
2089 graphics->unit = container->unit;
2090 graphics->textcontrast = container->textcontrast;
2091 graphics->pixeloffset = container->pixeloffset;
2092 graphics->origin_x = container->origin_x;
2093 graphics->origin_y = container->origin_y;
2095 return Ok;
2098 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2100 RECT wnd_rect;
2101 GpStatus stat=Ok;
2102 GpUnit unit;
2104 if(graphics->hwnd) {
2105 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2106 return GenericError;
2108 rect->X = wnd_rect.left;
2109 rect->Y = wnd_rect.top;
2110 rect->Width = wnd_rect.right - wnd_rect.left;
2111 rect->Height = wnd_rect.bottom - wnd_rect.top;
2112 }else if (graphics->image){
2113 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2114 if (stat == Ok && unit != UnitPixel)
2115 FIXME("need to convert from unit %i\n", unit);
2116 }else{
2117 rect->X = 0;
2118 rect->Y = 0;
2119 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2120 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2123 return stat;
2126 /* on success, rgn will contain the region of the graphics object which
2127 * is visible after clipping has been applied */
2128 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2130 GpStatus stat;
2131 GpRectF rectf;
2132 GpRegion* tmp;
2134 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2135 return stat;
2137 if((stat = GdipCreateRegion(&tmp)) != Ok)
2138 return stat;
2140 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2141 goto end;
2143 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2144 goto end;
2146 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2148 end:
2149 GdipDeleteRegion(tmp);
2150 return stat;
2153 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2155 REAL height;
2157 if (font->unit == UnitPixel)
2159 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2161 else
2163 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2164 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2165 else
2166 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2169 lf->lfHeight = -(height + 0.5);
2170 lf->lfWidth = 0;
2171 lf->lfEscapement = 0;
2172 lf->lfOrientation = 0;
2173 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2174 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2175 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2176 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2177 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2178 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2179 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2180 lf->lfQuality = DEFAULT_QUALITY;
2181 lf->lfPitchAndFamily = 0;
2182 strcpyW(lf->lfFaceName, font->family->FamilyName);
2185 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2186 GDIPCONST GpStringFormat *format, HFONT *hfont,
2187 GDIPCONST GpMatrix *matrix)
2189 HDC hdc = CreateCompatibleDC(0);
2190 GpPointF pt[3];
2191 REAL angle, rel_width, rel_height, font_height;
2192 LOGFONTW lfw;
2193 HFONT unscaled_font;
2194 TEXTMETRICW textmet;
2196 if (font->unit == UnitPixel)
2197 font_height = font->emSize;
2198 else
2200 REAL unit_scale, res;
2202 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2203 unit_scale = units_scale(font->unit, graphics->unit, res);
2205 font_height = font->emSize * unit_scale;
2208 pt[0].X = 0.0;
2209 pt[0].Y = 0.0;
2210 pt[1].X = 1.0;
2211 pt[1].Y = 0.0;
2212 pt[2].X = 0.0;
2213 pt[2].Y = 1.0;
2214 if (matrix)
2216 GpMatrix xform = *matrix;
2217 GdipTransformMatrixPoints(&xform, pt, 3);
2219 if (graphics)
2220 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2221 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2222 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2223 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2224 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2225 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2227 get_log_fontW(font, graphics, &lfw);
2228 lfw.lfHeight = gdip_round(font_height * rel_height);
2229 unscaled_font = CreateFontIndirectW(&lfw);
2231 SelectObject(hdc, unscaled_font);
2232 GetTextMetricsW(hdc, &textmet);
2234 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2235 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2237 *hfont = CreateFontIndirectW(&lfw);
2239 DeleteDC(hdc);
2240 DeleteObject(unscaled_font);
2243 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2245 TRACE("(%p, %p)\n", hdc, graphics);
2247 return GdipCreateFromHDC2(hdc, NULL, graphics);
2250 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2252 GpStatus retval;
2253 HBITMAP hbitmap;
2254 DIBSECTION dib;
2256 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2258 if(hDevice != NULL) {
2259 FIXME("Don't know how to handle parameter hDevice\n");
2260 return NotImplemented;
2263 if(hdc == NULL)
2264 return OutOfMemory;
2266 if(graphics == NULL)
2267 return InvalidParameter;
2269 *graphics = GdipAlloc(sizeof(GpGraphics));
2270 if(!*graphics) return OutOfMemory;
2272 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2274 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2275 GdipFree(*graphics);
2276 return retval;
2279 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2280 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2281 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2283 (*graphics)->alpha_hdc = 1;
2286 (*graphics)->hdc = hdc;
2287 (*graphics)->hwnd = WindowFromDC(hdc);
2288 (*graphics)->owndc = FALSE;
2289 (*graphics)->smoothing = SmoothingModeDefault;
2290 (*graphics)->compqual = CompositingQualityDefault;
2291 (*graphics)->interpolation = InterpolationModeBilinear;
2292 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2293 (*graphics)->compmode = CompositingModeSourceOver;
2294 (*graphics)->unit = UnitDisplay;
2295 (*graphics)->scale = 1.0;
2296 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2297 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2298 (*graphics)->busy = FALSE;
2299 (*graphics)->textcontrast = 4;
2300 list_init(&(*graphics)->containers);
2301 (*graphics)->contid = 0;
2303 TRACE("<-- %p\n", *graphics);
2305 return Ok;
2308 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2310 GpStatus retval;
2312 *graphics = GdipAlloc(sizeof(GpGraphics));
2313 if(!*graphics) return OutOfMemory;
2315 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2317 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2318 GdipFree(*graphics);
2319 return retval;
2322 (*graphics)->hdc = NULL;
2323 (*graphics)->hwnd = NULL;
2324 (*graphics)->owndc = FALSE;
2325 (*graphics)->image = image;
2326 (*graphics)->smoothing = SmoothingModeDefault;
2327 (*graphics)->compqual = CompositingQualityDefault;
2328 (*graphics)->interpolation = InterpolationModeBilinear;
2329 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2330 (*graphics)->compmode = CompositingModeSourceOver;
2331 (*graphics)->unit = UnitDisplay;
2332 (*graphics)->scale = 1.0;
2333 (*graphics)->xres = image->xres;
2334 (*graphics)->yres = image->yres;
2335 (*graphics)->busy = FALSE;
2336 (*graphics)->textcontrast = 4;
2337 list_init(&(*graphics)->containers);
2338 (*graphics)->contid = 0;
2340 TRACE("<-- %p\n", *graphics);
2342 return Ok;
2345 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2347 GpStatus ret;
2348 HDC hdc;
2350 TRACE("(%p, %p)\n", hwnd, graphics);
2352 hdc = GetDC(hwnd);
2354 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2356 ReleaseDC(hwnd, hdc);
2357 return ret;
2360 (*graphics)->hwnd = hwnd;
2361 (*graphics)->owndc = TRUE;
2363 return Ok;
2366 /* FIXME: no icm handling */
2367 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2369 TRACE("(%p, %p)\n", hwnd, graphics);
2371 return GdipCreateFromHWND(hwnd, graphics);
2374 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2375 GpMetafile **metafile)
2377 ENHMETAHEADER header;
2378 MetafileType metafile_type;
2380 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2382 if(!hemf || !metafile)
2383 return InvalidParameter;
2385 if (GetEnhMetaFileHeader(hemf, sizeof(header), &header) == 0)
2386 return GenericError;
2388 metafile_type = METAFILE_GetEmfType(hemf);
2390 if (metafile_type == MetafileTypeInvalid)
2391 return GenericError;
2393 *metafile = GdipAlloc(sizeof(GpMetafile));
2394 if (!*metafile)
2395 return OutOfMemory;
2397 (*metafile)->image.type = ImageTypeMetafile;
2398 (*metafile)->image.format = ImageFormatEMF;
2399 (*metafile)->image.frame_count = 1;
2400 (*metafile)->image.xres = (REAL)header.szlDevice.cx;
2401 (*metafile)->image.yres = (REAL)header.szlDevice.cy;
2402 (*metafile)->bounds.X = (REAL)header.rclBounds.left;
2403 (*metafile)->bounds.Y = (REAL)header.rclBounds.top;
2404 (*metafile)->bounds.Width = (REAL)(header.rclBounds.right - header.rclBounds.left);
2405 (*metafile)->bounds.Height = (REAL)(header.rclBounds.bottom - header.rclBounds.top);
2406 (*metafile)->unit = UnitPixel;
2407 (*metafile)->metafile_type = metafile_type;
2408 (*metafile)->hemf = hemf;
2409 (*metafile)->preserve_hemf = !delete;
2411 TRACE("<-- %p\n", *metafile);
2413 return Ok;
2416 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2417 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2419 UINT read;
2420 BYTE *copy;
2421 HENHMETAFILE hemf;
2422 GpStatus retval = Ok;
2424 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2426 if(!hwmf || !metafile || !placeable)
2427 return InvalidParameter;
2429 *metafile = NULL;
2430 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2431 if(!read)
2432 return GenericError;
2433 copy = GdipAlloc(read);
2434 GetMetaFileBitsEx(hwmf, read, copy);
2436 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2437 GdipFree(copy);
2439 /* FIXME: We should store and use hwmf instead of converting to hemf */
2440 retval = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
2442 if (retval == Ok)
2444 (*metafile)->image.xres = (REAL)placeable->Inch;
2445 (*metafile)->image.yres = (REAL)placeable->Inch;
2446 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2447 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2448 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2449 placeable->BoundingBox.Left);
2450 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2451 placeable->BoundingBox.Top);
2452 (*metafile)->metafile_type = MetafileTypeWmfPlaceable;
2453 (*metafile)->image.format = ImageFormatWMF;
2455 if (delete) DeleteMetaFile(hwmf);
2457 else
2458 DeleteEnhMetaFile(hemf);
2459 return retval;
2462 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2463 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2465 HMETAFILE hmf = GetMetaFileW(file);
2467 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2469 if(!hmf) return InvalidParameter;
2471 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2474 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2475 GpMetafile **metafile)
2477 FIXME("(%p, %p): stub\n", file, metafile);
2478 return NotImplemented;
2481 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2482 GpMetafile **metafile)
2484 FIXME("(%p, %p): stub\n", stream, metafile);
2485 return NotImplemented;
2488 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2489 UINT access, IStream **stream)
2491 DWORD dwMode;
2492 HRESULT ret;
2494 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2496 if(!stream || !filename)
2497 return InvalidParameter;
2499 if(access & GENERIC_WRITE)
2500 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2501 else if(access & GENERIC_READ)
2502 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2503 else
2504 return InvalidParameter;
2506 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2508 return hresult_to_status(ret);
2511 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2513 GraphicsContainerItem *cont, *next;
2514 GpStatus stat;
2515 TRACE("(%p)\n", graphics);
2517 if(!graphics) return InvalidParameter;
2518 if(graphics->busy) return ObjectBusy;
2520 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2522 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2523 if (stat != Ok)
2524 return stat;
2527 if(graphics->owndc)
2528 ReleaseDC(graphics->hwnd, graphics->hdc);
2530 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2531 list_remove(&cont->entry);
2532 delete_container(cont);
2535 GdipDeleteRegion(graphics->clip);
2536 GdipFree(graphics);
2538 return Ok;
2541 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2542 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2544 INT save_state, num_pts;
2545 GpPointF points[MAX_ARC_PTS];
2546 GpStatus retval;
2548 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2549 width, height, startAngle, sweepAngle);
2551 if(!graphics || !pen || width <= 0 || height <= 0)
2552 return InvalidParameter;
2554 if(graphics->busy)
2555 return ObjectBusy;
2557 if (!graphics->hdc)
2559 FIXME("graphics object has no HDC\n");
2560 return Ok;
2563 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2565 save_state = prepare_dc(graphics, pen);
2567 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2569 restore_dc(graphics, save_state);
2571 return retval;
2574 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2575 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2577 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2578 width, height, startAngle, sweepAngle);
2580 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2583 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2584 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2586 INT save_state;
2587 GpPointF pt[4];
2588 GpStatus retval;
2590 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2591 x2, y2, x3, y3, x4, y4);
2593 if(!graphics || !pen)
2594 return InvalidParameter;
2596 if(graphics->busy)
2597 return ObjectBusy;
2599 if (!graphics->hdc)
2601 FIXME("graphics object has no HDC\n");
2602 return Ok;
2605 pt[0].X = x1;
2606 pt[0].Y = y1;
2607 pt[1].X = x2;
2608 pt[1].Y = y2;
2609 pt[2].X = x3;
2610 pt[2].Y = y3;
2611 pt[3].X = x4;
2612 pt[3].Y = y4;
2614 save_state = prepare_dc(graphics, pen);
2616 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2618 restore_dc(graphics, save_state);
2620 return retval;
2623 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2624 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2626 INT save_state;
2627 GpPointF pt[4];
2628 GpStatus retval;
2630 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2631 x2, y2, x3, y3, x4, y4);
2633 if(!graphics || !pen)
2634 return InvalidParameter;
2636 if(graphics->busy)
2637 return ObjectBusy;
2639 if (!graphics->hdc)
2641 FIXME("graphics object has no HDC\n");
2642 return Ok;
2645 pt[0].X = x1;
2646 pt[0].Y = y1;
2647 pt[1].X = x2;
2648 pt[1].Y = y2;
2649 pt[2].X = x3;
2650 pt[2].Y = y3;
2651 pt[3].X = x4;
2652 pt[3].Y = y4;
2654 save_state = prepare_dc(graphics, pen);
2656 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2658 restore_dc(graphics, save_state);
2660 return retval;
2663 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2664 GDIPCONST GpPointF *points, INT count)
2666 INT i;
2667 GpStatus ret;
2669 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2671 if(!graphics || !pen || !points || (count <= 0))
2672 return InvalidParameter;
2674 if(graphics->busy)
2675 return ObjectBusy;
2677 for(i = 0; i < floor(count / 4); i++){
2678 ret = GdipDrawBezier(graphics, pen,
2679 points[4*i].X, points[4*i].Y,
2680 points[4*i + 1].X, points[4*i + 1].Y,
2681 points[4*i + 2].X, points[4*i + 2].Y,
2682 points[4*i + 3].X, points[4*i + 3].Y);
2683 if(ret != Ok)
2684 return ret;
2687 return Ok;
2690 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2691 GDIPCONST GpPoint *points, INT count)
2693 GpPointF *pts;
2694 GpStatus ret;
2695 INT i;
2697 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2699 if(!graphics || !pen || !points || (count <= 0))
2700 return InvalidParameter;
2702 if(graphics->busy)
2703 return ObjectBusy;
2705 pts = GdipAlloc(sizeof(GpPointF) * count);
2706 if(!pts)
2707 return OutOfMemory;
2709 for(i = 0; i < count; i++){
2710 pts[i].X = (REAL)points[i].X;
2711 pts[i].Y = (REAL)points[i].Y;
2714 ret = GdipDrawBeziers(graphics,pen,pts,count);
2716 GdipFree(pts);
2718 return ret;
2721 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2722 GDIPCONST GpPointF *points, INT count)
2724 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2726 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2729 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2730 GDIPCONST GpPoint *points, INT count)
2732 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2734 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2737 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2738 GDIPCONST GpPointF *points, INT count, REAL tension)
2740 GpPath *path;
2741 GpStatus stat;
2743 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2745 if(!graphics || !pen || !points || count <= 0)
2746 return InvalidParameter;
2748 if(graphics->busy)
2749 return ObjectBusy;
2751 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2752 return stat;
2754 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2755 if(stat != Ok){
2756 GdipDeletePath(path);
2757 return stat;
2760 stat = GdipDrawPath(graphics, pen, path);
2762 GdipDeletePath(path);
2764 return stat;
2767 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2768 GDIPCONST GpPoint *points, INT count, REAL tension)
2770 GpPointF *ptf;
2771 GpStatus stat;
2772 INT i;
2774 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2776 if(!points || count <= 0)
2777 return InvalidParameter;
2779 ptf = GdipAlloc(sizeof(GpPointF)*count);
2780 if(!ptf)
2781 return OutOfMemory;
2783 for(i = 0; i < count; i++){
2784 ptf[i].X = (REAL)points[i].X;
2785 ptf[i].Y = (REAL)points[i].Y;
2788 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2790 GdipFree(ptf);
2792 return stat;
2795 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2796 GDIPCONST GpPointF *points, INT count)
2798 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2800 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2803 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2804 GDIPCONST GpPoint *points, INT count)
2806 GpPointF *pointsF;
2807 GpStatus ret;
2808 INT i;
2810 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2812 if(!points)
2813 return InvalidParameter;
2815 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2816 if(!pointsF)
2817 return OutOfMemory;
2819 for(i = 0; i < count; i++){
2820 pointsF[i].X = (REAL)points[i].X;
2821 pointsF[i].Y = (REAL)points[i].Y;
2824 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2825 GdipFree(pointsF);
2827 return ret;
2830 /* Approximates cardinal spline with Bezier curves. */
2831 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2832 GDIPCONST GpPointF *points, INT count, REAL tension)
2834 /* PolyBezier expects count*3-2 points. */
2835 INT i, len_pt = count*3-2, save_state;
2836 GpPointF *pt;
2837 REAL x1, x2, y1, y2;
2838 GpStatus retval;
2840 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2842 if(!graphics || !pen)
2843 return InvalidParameter;
2845 if(graphics->busy)
2846 return ObjectBusy;
2848 if(count < 2)
2849 return InvalidParameter;
2851 if (!graphics->hdc)
2853 FIXME("graphics object has no HDC\n");
2854 return Ok;
2857 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2858 if(!pt)
2859 return OutOfMemory;
2861 tension = tension * TENSION_CONST;
2863 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2864 tension, &x1, &y1);
2866 pt[0].X = points[0].X;
2867 pt[0].Y = points[0].Y;
2868 pt[1].X = x1;
2869 pt[1].Y = y1;
2871 for(i = 0; i < count-2; i++){
2872 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2874 pt[3*i+2].X = x1;
2875 pt[3*i+2].Y = y1;
2876 pt[3*i+3].X = points[i+1].X;
2877 pt[3*i+3].Y = points[i+1].Y;
2878 pt[3*i+4].X = x2;
2879 pt[3*i+4].Y = y2;
2882 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2883 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2885 pt[len_pt-2].X = x1;
2886 pt[len_pt-2].Y = y1;
2887 pt[len_pt-1].X = points[count-1].X;
2888 pt[len_pt-1].Y = points[count-1].Y;
2890 save_state = prepare_dc(graphics, pen);
2892 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2894 GdipFree(pt);
2895 restore_dc(graphics, save_state);
2897 return retval;
2900 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2901 GDIPCONST GpPoint *points, INT count, REAL tension)
2903 GpPointF *pointsF;
2904 GpStatus ret;
2905 INT i;
2907 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2909 if(!points)
2910 return InvalidParameter;
2912 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2913 if(!pointsF)
2914 return OutOfMemory;
2916 for(i = 0; i < count; i++){
2917 pointsF[i].X = (REAL)points[i].X;
2918 pointsF[i].Y = (REAL)points[i].Y;
2921 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2922 GdipFree(pointsF);
2924 return ret;
2927 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2928 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2929 REAL tension)
2931 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2933 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2934 return InvalidParameter;
2937 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2940 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2941 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2942 REAL tension)
2944 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2946 if(count < 0){
2947 return OutOfMemory;
2950 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2951 return InvalidParameter;
2954 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2957 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2958 REAL y, REAL width, REAL height)
2960 INT save_state;
2961 GpPointF ptf[2];
2962 POINT pti[2];
2964 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2966 if(!graphics || !pen)
2967 return InvalidParameter;
2969 if(graphics->busy)
2970 return ObjectBusy;
2972 if (!graphics->hdc)
2974 FIXME("graphics object has no HDC\n");
2975 return Ok;
2978 ptf[0].X = x;
2979 ptf[0].Y = y;
2980 ptf[1].X = x + width;
2981 ptf[1].Y = y + height;
2983 save_state = prepare_dc(graphics, pen);
2984 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2986 transform_and_round_points(graphics, pti, ptf, 2);
2988 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2990 restore_dc(graphics, save_state);
2992 return Ok;
2995 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2996 INT y, INT width, INT height)
2998 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3000 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3004 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
3006 UINT width, height;
3008 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
3010 if(!graphics || !image)
3011 return InvalidParameter;
3013 GdipGetImageWidth(image, &width);
3014 GdipGetImageHeight(image, &height);
3016 return GdipDrawImagePointRect(graphics, image, x, y,
3017 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
3020 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
3021 INT y)
3023 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
3025 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
3028 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
3029 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
3030 GpUnit srcUnit)
3032 GpPointF points[3];
3033 REAL scale_x, scale_y, width, height;
3035 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3037 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
3038 scale_x *= graphics->xres / image->xres;
3039 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
3040 scale_y *= graphics->yres / image->yres;
3041 width = srcwidth * scale_x;
3042 height = srcheight * scale_y;
3044 points[0].X = points[2].X = x;
3045 points[0].Y = points[1].Y = y;
3046 points[1].X = x + width;
3047 points[2].Y = y + height;
3049 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3050 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3053 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3054 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3055 GpUnit srcUnit)
3057 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3060 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3061 GDIPCONST GpPointF *dstpoints, INT count)
3063 UINT width, height;
3065 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3067 if(!image)
3068 return InvalidParameter;
3070 GdipGetImageWidth(image, &width);
3071 GdipGetImageHeight(image, &height);
3073 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3074 width, height, UnitPixel, NULL, NULL, NULL);
3077 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3078 GDIPCONST GpPoint *dstpoints, INT count)
3080 GpPointF ptf[3];
3082 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3084 if (count != 3 || !dstpoints)
3085 return InvalidParameter;
3087 ptf[0].X = (REAL)dstpoints[0].X;
3088 ptf[0].Y = (REAL)dstpoints[0].Y;
3089 ptf[1].X = (REAL)dstpoints[1].X;
3090 ptf[1].Y = (REAL)dstpoints[1].Y;
3091 ptf[2].X = (REAL)dstpoints[2].X;
3092 ptf[2].Y = (REAL)dstpoints[2].Y;
3094 return GdipDrawImagePoints(graphics, image, ptf, count);
3097 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3098 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3100 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3101 return TRUE;
3104 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3105 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3106 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3107 DrawImageAbort callback, VOID * callbackData)
3109 GpPointF ptf[4];
3110 POINT pti[4];
3111 GpStatus stat;
3113 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3114 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3115 callbackData);
3117 if (count > 3)
3118 return NotImplemented;
3120 if(!graphics || !image || !points || count != 3)
3121 return InvalidParameter;
3123 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3124 debugstr_pointf(&points[2]));
3126 memcpy(ptf, points, 3 * sizeof(GpPointF));
3127 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3128 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3129 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3130 return Ok;
3131 transform_and_round_points(graphics, pti, ptf, 4);
3133 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3134 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3136 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3137 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3138 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3139 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3140 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3142 if (image->picture)
3144 if (!graphics->hdc)
3146 FIXME("graphics object has no HDC\n");
3149 if(IPicture_Render(image->picture, graphics->hdc,
3150 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3151 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3153 if(callback)
3154 callback(callbackData);
3155 return GenericError;
3158 else if (image->type == ImageTypeBitmap)
3160 GpBitmap* bitmap = (GpBitmap*)image;
3161 int use_software=0;
3163 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3164 graphics->xres, graphics->yres,
3165 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3166 graphics->scale, image->xres, image->yres, bitmap->format,
3167 imageAttributes ? imageAttributes->outside_color : 0);
3169 if (imageAttributes || graphics->alpha_hdc ||
3170 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3171 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3172 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3173 srcx < 0 || srcy < 0 ||
3174 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3175 use_software = 1;
3177 if (use_software)
3179 RECT dst_area;
3180 GpRect src_area;
3181 int i, x, y, src_stride, dst_stride;
3182 GpMatrix dst_to_src;
3183 REAL m11, m12, m21, m22, mdx, mdy;
3184 LPBYTE src_data, dst_data;
3185 BitmapData lockeddata;
3186 InterpolationMode interpolation = graphics->interpolation;
3187 PixelOffsetMode offset_mode = graphics->pixeloffset;
3188 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3189 REAL x_dx, x_dy, y_dx, y_dy;
3190 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3192 if (!imageAttributes)
3193 imageAttributes = &defaultImageAttributes;
3195 dst_area.left = dst_area.right = pti[0].x;
3196 dst_area.top = dst_area.bottom = pti[0].y;
3197 for (i=1; i<4; i++)
3199 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3200 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3201 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3202 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3205 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3207 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3208 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3209 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3210 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3211 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3212 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3214 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3216 stat = GdipInvertMatrix(&dst_to_src);
3217 if (stat != Ok) return stat;
3219 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3220 if (!dst_data) return OutOfMemory;
3222 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3224 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3225 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3227 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3229 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3230 if (!src_data)
3232 GdipFree(dst_data);
3233 return OutOfMemory;
3235 src_stride = sizeof(ARGB) * src_area.Width;
3237 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3238 lockeddata.Width = src_area.Width;
3239 lockeddata.Height = src_area.Height;
3240 lockeddata.Stride = src_stride;
3241 lockeddata.PixelFormat = PixelFormat32bppARGB;
3242 lockeddata.Scan0 = src_data;
3244 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3245 PixelFormat32bppARGB, &lockeddata);
3247 if (stat == Ok)
3248 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3250 if (stat != Ok)
3252 if (src_data != dst_data)
3253 GdipFree(src_data);
3254 GdipFree(dst_data);
3255 return stat;
3258 apply_image_attributes(imageAttributes, src_data,
3259 src_area.Width, src_area.Height,
3260 src_stride, ColorAdjustTypeBitmap);
3262 /* Transform the bits as needed to the destination. */
3263 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3265 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3266 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3267 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3268 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3270 for (x=dst_area.left; x<dst_area.right; x++)
3272 for (y=dst_area.top; y<dst_area.bottom; y++)
3274 GpPointF src_pointf;
3275 ARGB *dst_color;
3277 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3278 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3280 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3282 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3283 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3284 imageAttributes, interpolation, offset_mode);
3285 else
3286 *dst_color = 0;
3290 GdipFree(src_data);
3292 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3293 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3295 GdipFree(dst_data);
3297 return stat;
3299 else
3301 HDC hdc;
3302 int temp_hdc=0, temp_bitmap=0;
3303 HBITMAP hbitmap, old_hbm=NULL;
3305 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3306 bitmap->format == PixelFormat24bppRGB ||
3307 bitmap->format == PixelFormat32bppRGB ||
3308 bitmap->format == PixelFormat32bppPARGB))
3310 BITMAPINFOHEADER bih;
3311 BYTE *temp_bits;
3312 PixelFormat dst_format;
3314 /* we can't draw a bitmap of this format directly */
3315 hdc = CreateCompatibleDC(0);
3316 temp_hdc = 1;
3317 temp_bitmap = 1;
3319 bih.biSize = sizeof(BITMAPINFOHEADER);
3320 bih.biWidth = bitmap->width;
3321 bih.biHeight = -bitmap->height;
3322 bih.biPlanes = 1;
3323 bih.biBitCount = 32;
3324 bih.biCompression = BI_RGB;
3325 bih.biSizeImage = 0;
3326 bih.biXPelsPerMeter = 0;
3327 bih.biYPelsPerMeter = 0;
3328 bih.biClrUsed = 0;
3329 bih.biClrImportant = 0;
3331 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3332 (void**)&temp_bits, NULL, 0);
3334 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3335 dst_format = PixelFormat32bppPARGB;
3336 else
3337 dst_format = PixelFormat32bppRGB;
3339 convert_pixels(bitmap->width, bitmap->height,
3340 bitmap->width*4, temp_bits, dst_format,
3341 bitmap->stride, bitmap->bits, bitmap->format,
3342 bitmap->image.palette);
3344 else
3346 if (bitmap->hbitmap)
3347 hbitmap = bitmap->hbitmap;
3348 else
3350 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3351 temp_bitmap = 1;
3354 hdc = bitmap->hdc;
3355 temp_hdc = (hdc == 0);
3358 if (temp_hdc)
3360 if (!hdc) hdc = CreateCompatibleDC(0);
3361 old_hbm = SelectObject(hdc, hbitmap);
3364 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3366 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3367 hdc, srcx, srcy, srcwidth, srcheight);
3369 else
3371 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3372 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3375 if (temp_hdc)
3377 SelectObject(hdc, old_hbm);
3378 DeleteDC(hdc);
3381 if (temp_bitmap)
3382 DeleteObject(hbitmap);
3385 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3387 GpRectF rc;
3389 rc.X = srcx;
3390 rc.Y = srcy;
3391 rc.Width = srcwidth;
3392 rc.Height = srcheight;
3394 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3395 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3397 else
3399 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3400 return InvalidParameter;
3403 return Ok;
3406 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3407 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3408 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3409 DrawImageAbort callback, VOID * callbackData)
3411 GpPointF pointsF[3];
3412 INT i;
3414 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3415 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3416 callbackData);
3418 if(!points || count!=3)
3419 return InvalidParameter;
3421 for(i = 0; i < count; i++){
3422 pointsF[i].X = (REAL)points[i].X;
3423 pointsF[i].Y = (REAL)points[i].Y;
3426 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3427 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3428 callback, callbackData);
3431 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3432 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3433 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3434 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3435 VOID * callbackData)
3437 GpPointF points[3];
3439 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3440 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3441 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3443 points[0].X = dstx;
3444 points[0].Y = dsty;
3445 points[1].X = dstx + dstwidth;
3446 points[1].Y = dsty;
3447 points[2].X = dstx;
3448 points[2].Y = dsty + dstheight;
3450 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3451 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3454 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3455 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3456 INT srcwidth, INT srcheight, GpUnit srcUnit,
3457 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3458 VOID * callbackData)
3460 GpPointF points[3];
3462 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3463 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3464 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3466 points[0].X = dstx;
3467 points[0].Y = dsty;
3468 points[1].X = dstx + dstwidth;
3469 points[1].Y = dsty;
3470 points[2].X = dstx;
3471 points[2].Y = dsty + dstheight;
3473 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3474 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3477 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3478 REAL x, REAL y, REAL width, REAL height)
3480 RectF bounds;
3481 GpUnit unit;
3482 GpStatus ret;
3484 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3486 if(!graphics || !image)
3487 return InvalidParameter;
3489 ret = GdipGetImageBounds(image, &bounds, &unit);
3490 if(ret != Ok)
3491 return ret;
3493 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3494 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3495 unit, NULL, NULL, NULL);
3498 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3499 INT x, INT y, INT width, INT height)
3501 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3503 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3506 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3507 REAL y1, REAL x2, REAL y2)
3509 INT save_state;
3510 GpPointF pt[2];
3511 GpStatus retval;
3513 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3515 if(!pen || !graphics)
3516 return InvalidParameter;
3518 if(graphics->busy)
3519 return ObjectBusy;
3521 if (!graphics->hdc)
3523 FIXME("graphics object has no HDC\n");
3524 return Ok;
3527 pt[0].X = x1;
3528 pt[0].Y = y1;
3529 pt[1].X = x2;
3530 pt[1].Y = y2;
3532 save_state = prepare_dc(graphics, pen);
3534 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3536 restore_dc(graphics, save_state);
3538 return retval;
3541 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3542 INT y1, INT x2, INT y2)
3544 INT save_state;
3545 GpPointF pt[2];
3546 GpStatus retval;
3548 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3550 if(!pen || !graphics)
3551 return InvalidParameter;
3553 if(graphics->busy)
3554 return ObjectBusy;
3556 if (!graphics->hdc)
3558 FIXME("graphics object has no HDC\n");
3559 return Ok;
3562 pt[0].X = (REAL)x1;
3563 pt[0].Y = (REAL)y1;
3564 pt[1].X = (REAL)x2;
3565 pt[1].Y = (REAL)y2;
3567 save_state = prepare_dc(graphics, pen);
3569 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3571 restore_dc(graphics, save_state);
3573 return retval;
3576 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3577 GpPointF *points, INT count)
3579 INT save_state;
3580 GpStatus retval;
3582 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3584 if(!pen || !graphics || (count < 2))
3585 return InvalidParameter;
3587 if(graphics->busy)
3588 return ObjectBusy;
3590 if (!graphics->hdc)
3592 FIXME("graphics object has no HDC\n");
3593 return Ok;
3596 save_state = prepare_dc(graphics, pen);
3598 retval = draw_polyline(graphics, pen, points, count, TRUE);
3600 restore_dc(graphics, save_state);
3602 return retval;
3605 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3606 GpPoint *points, INT count)
3608 INT save_state;
3609 GpStatus retval;
3610 GpPointF *ptf = NULL;
3611 int i;
3613 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3615 if(!pen || !graphics || (count < 2))
3616 return InvalidParameter;
3618 if(graphics->busy)
3619 return ObjectBusy;
3621 if (!graphics->hdc)
3623 FIXME("graphics object has no HDC\n");
3624 return Ok;
3627 ptf = GdipAlloc(count * sizeof(GpPointF));
3628 if(!ptf) return OutOfMemory;
3630 for(i = 0; i < count; i ++){
3631 ptf[i].X = (REAL) points[i].X;
3632 ptf[i].Y = (REAL) points[i].Y;
3635 save_state = prepare_dc(graphics, pen);
3637 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3639 restore_dc(graphics, save_state);
3641 GdipFree(ptf);
3642 return retval;
3645 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3647 INT save_state;
3648 GpStatus retval;
3650 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3652 if(!pen || !graphics)
3653 return InvalidParameter;
3655 if(graphics->busy)
3656 return ObjectBusy;
3658 if (!graphics->hdc)
3660 FIXME("graphics object has no HDC\n");
3661 return Ok;
3664 save_state = prepare_dc(graphics, pen);
3666 retval = draw_poly(graphics, pen, path->pathdata.Points,
3667 path->pathdata.Types, path->pathdata.Count, TRUE);
3669 restore_dc(graphics, save_state);
3671 return retval;
3674 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3675 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3677 INT save_state;
3679 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3680 width, height, startAngle, sweepAngle);
3682 if(!graphics || !pen)
3683 return InvalidParameter;
3685 if(graphics->busy)
3686 return ObjectBusy;
3688 if (!graphics->hdc)
3690 FIXME("graphics object has no HDC\n");
3691 return Ok;
3694 save_state = prepare_dc(graphics, pen);
3695 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3697 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3699 restore_dc(graphics, save_state);
3701 return Ok;
3704 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3705 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3707 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3708 width, height, startAngle, sweepAngle);
3710 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3713 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3714 REAL y, REAL width, REAL height)
3716 INT save_state;
3717 GpPointF ptf[4];
3718 POINT pti[4];
3720 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3722 if(!pen || !graphics)
3723 return InvalidParameter;
3725 if(graphics->busy)
3726 return ObjectBusy;
3728 if (!graphics->hdc)
3730 FIXME("graphics object has no HDC\n");
3731 return Ok;
3734 ptf[0].X = x;
3735 ptf[0].Y = y;
3736 ptf[1].X = x + width;
3737 ptf[1].Y = y;
3738 ptf[2].X = x + width;
3739 ptf[2].Y = y + height;
3740 ptf[3].X = x;
3741 ptf[3].Y = y + height;
3743 save_state = prepare_dc(graphics, pen);
3744 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3746 transform_and_round_points(graphics, pti, ptf, 4);
3747 Polygon(graphics->hdc, pti, 4);
3749 restore_dc(graphics, save_state);
3751 return Ok;
3754 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3755 INT y, INT width, INT height)
3757 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3759 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3762 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3763 GDIPCONST GpRectF* rects, INT count)
3765 GpPointF *ptf;
3766 POINT *pti;
3767 INT save_state, i;
3769 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3771 if(!graphics || !pen || !rects || count < 1)
3772 return InvalidParameter;
3774 if(graphics->busy)
3775 return ObjectBusy;
3777 if (!graphics->hdc)
3779 FIXME("graphics object has no HDC\n");
3780 return Ok;
3783 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3784 pti = GdipAlloc(4 * count * sizeof(POINT));
3786 if(!ptf || !pti){
3787 GdipFree(ptf);
3788 GdipFree(pti);
3789 return OutOfMemory;
3792 for(i = 0; i < count; i++){
3793 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3794 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3795 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3796 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3799 save_state = prepare_dc(graphics, pen);
3800 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3802 transform_and_round_points(graphics, pti, ptf, 4 * count);
3804 for(i = 0; i < count; i++)
3805 Polygon(graphics->hdc, &pti[4 * i], 4);
3807 restore_dc(graphics, save_state);
3809 GdipFree(ptf);
3810 GdipFree(pti);
3812 return Ok;
3815 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3816 GDIPCONST GpRect* rects, INT count)
3818 GpRectF *rectsF;
3819 GpStatus ret;
3820 INT i;
3822 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3824 if(!rects || count<=0)
3825 return InvalidParameter;
3827 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3828 if(!rectsF)
3829 return OutOfMemory;
3831 for(i = 0;i < count;i++){
3832 rectsF[i].X = (REAL)rects[i].X;
3833 rectsF[i].Y = (REAL)rects[i].Y;
3834 rectsF[i].Width = (REAL)rects[i].Width;
3835 rectsF[i].Height = (REAL)rects[i].Height;
3838 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3839 GdipFree(rectsF);
3841 return ret;
3844 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3845 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3847 GpPath *path;
3848 GpStatus stat;
3850 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3851 count, tension, fill);
3853 if(!graphics || !brush || !points)
3854 return InvalidParameter;
3856 if(graphics->busy)
3857 return ObjectBusy;
3859 if(count == 1) /* Do nothing */
3860 return Ok;
3862 stat = GdipCreatePath(fill, &path);
3863 if(stat != Ok)
3864 return stat;
3866 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3867 if(stat != Ok){
3868 GdipDeletePath(path);
3869 return stat;
3872 stat = GdipFillPath(graphics, brush, path);
3873 if(stat != Ok){
3874 GdipDeletePath(path);
3875 return stat;
3878 GdipDeletePath(path);
3880 return Ok;
3883 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3884 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3886 GpPointF *ptf;
3887 GpStatus stat;
3888 INT i;
3890 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3891 count, tension, fill);
3893 if(!points || count == 0)
3894 return InvalidParameter;
3896 if(count == 1) /* Do nothing */
3897 return Ok;
3899 ptf = GdipAlloc(sizeof(GpPointF)*count);
3900 if(!ptf)
3901 return OutOfMemory;
3903 for(i = 0;i < count;i++){
3904 ptf[i].X = (REAL)points[i].X;
3905 ptf[i].Y = (REAL)points[i].Y;
3908 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3910 GdipFree(ptf);
3912 return stat;
3915 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3916 GDIPCONST GpPointF *points, INT count)
3918 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3919 return GdipFillClosedCurve2(graphics, brush, points, count,
3920 0.5f, FillModeAlternate);
3923 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3924 GDIPCONST GpPoint *points, INT count)
3926 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3927 return GdipFillClosedCurve2I(graphics, brush, points, count,
3928 0.5f, FillModeAlternate);
3931 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3932 REAL y, REAL width, REAL height)
3934 GpStatus stat;
3935 GpPath *path;
3937 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3939 if(!graphics || !brush)
3940 return InvalidParameter;
3942 if(graphics->busy)
3943 return ObjectBusy;
3945 stat = GdipCreatePath(FillModeAlternate, &path);
3947 if (stat == Ok)
3949 stat = GdipAddPathEllipse(path, x, y, width, height);
3951 if (stat == Ok)
3952 stat = GdipFillPath(graphics, brush, path);
3954 GdipDeletePath(path);
3957 return stat;
3960 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3961 INT y, INT width, INT height)
3963 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3965 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3968 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3970 INT save_state;
3971 GpStatus retval;
3973 if(!graphics->hdc || !brush_can_fill_path(brush))
3974 return NotImplemented;
3976 save_state = SaveDC(graphics->hdc);
3977 EndPath(graphics->hdc);
3978 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3979 : WINDING));
3981 BeginPath(graphics->hdc);
3982 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3983 path->pathdata.Types, path->pathdata.Count, FALSE);
3985 if(retval != Ok)
3986 goto end;
3988 EndPath(graphics->hdc);
3989 brush_fill_path(graphics, brush);
3991 retval = Ok;
3993 end:
3994 RestoreDC(graphics->hdc, save_state);
3996 return retval;
3999 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4001 GpStatus stat;
4002 GpRegion *rgn;
4004 if (!brush_can_fill_pixels(brush))
4005 return NotImplemented;
4007 /* FIXME: This could probably be done more efficiently without regions. */
4009 stat = GdipCreateRegionPath(path, &rgn);
4011 if (stat == Ok)
4013 stat = GdipFillRegion(graphics, brush, rgn);
4015 GdipDeleteRegion(rgn);
4018 return stat;
4021 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4023 GpStatus stat = NotImplemented;
4025 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4027 if(!brush || !graphics || !path)
4028 return InvalidParameter;
4030 if(graphics->busy)
4031 return ObjectBusy;
4033 if (!graphics->image && !graphics->alpha_hdc)
4034 stat = GDI32_GdipFillPath(graphics, brush, path);
4036 if (stat == NotImplemented)
4037 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4039 if (stat == NotImplemented)
4041 FIXME("Not implemented for brushtype %i\n", brush->bt);
4042 stat = Ok;
4045 return stat;
4048 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4049 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4051 GpStatus stat;
4052 GpPath *path;
4054 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4055 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4057 if(!graphics || !brush)
4058 return InvalidParameter;
4060 if(graphics->busy)
4061 return ObjectBusy;
4063 stat = GdipCreatePath(FillModeAlternate, &path);
4065 if (stat == Ok)
4067 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4069 if (stat == Ok)
4070 stat = GdipFillPath(graphics, brush, path);
4072 GdipDeletePath(path);
4075 return stat;
4078 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4079 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4081 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4082 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4084 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4087 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4088 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4090 GpStatus stat;
4091 GpPath *path;
4093 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4095 if(!graphics || !brush || !points || !count)
4096 return InvalidParameter;
4098 if(graphics->busy)
4099 return ObjectBusy;
4101 stat = GdipCreatePath(fillMode, &path);
4103 if (stat == Ok)
4105 stat = GdipAddPathPolygon(path, points, count);
4107 if (stat == Ok)
4108 stat = GdipFillPath(graphics, brush, path);
4110 GdipDeletePath(path);
4113 return stat;
4116 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4117 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4119 GpStatus stat;
4120 GpPath *path;
4122 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4124 if(!graphics || !brush || !points || !count)
4125 return InvalidParameter;
4127 if(graphics->busy)
4128 return ObjectBusy;
4130 stat = GdipCreatePath(fillMode, &path);
4132 if (stat == Ok)
4134 stat = GdipAddPathPolygonI(path, points, count);
4136 if (stat == Ok)
4137 stat = GdipFillPath(graphics, brush, path);
4139 GdipDeletePath(path);
4142 return stat;
4145 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4146 GDIPCONST GpPointF *points, INT count)
4148 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4150 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4153 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4154 GDIPCONST GpPoint *points, INT count)
4156 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4158 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4161 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4162 REAL x, REAL y, REAL width, REAL height)
4164 GpStatus stat;
4165 GpPath *path;
4167 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4169 if(!graphics || !brush)
4170 return InvalidParameter;
4172 if(graphics->busy)
4173 return ObjectBusy;
4175 stat = GdipCreatePath(FillModeAlternate, &path);
4177 if (stat == Ok)
4179 stat = GdipAddPathRectangle(path, x, y, width, height);
4181 if (stat == Ok)
4182 stat = GdipFillPath(graphics, brush, path);
4184 GdipDeletePath(path);
4187 return stat;
4190 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4191 INT x, INT y, INT width, INT height)
4193 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4195 return GdipFillRectangle(graphics, brush, x, y, width, height);
4198 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4199 INT count)
4201 GpStatus ret;
4202 INT i;
4204 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4206 if(!rects)
4207 return InvalidParameter;
4209 for(i = 0; i < count; i++){
4210 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4211 if(ret != Ok) return ret;
4214 return Ok;
4217 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4218 INT count)
4220 GpRectF *rectsF;
4221 GpStatus ret;
4222 INT i;
4224 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4226 if(!rects || count <= 0)
4227 return InvalidParameter;
4229 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4230 if(!rectsF)
4231 return OutOfMemory;
4233 for(i = 0; i < count; i++){
4234 rectsF[i].X = (REAL)rects[i].X;
4235 rectsF[i].Y = (REAL)rects[i].Y;
4236 rectsF[i].X = (REAL)rects[i].Width;
4237 rectsF[i].Height = (REAL)rects[i].Height;
4240 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4241 GdipFree(rectsF);
4243 return ret;
4246 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4247 GpRegion* region)
4249 INT save_state;
4250 GpStatus status;
4251 HRGN hrgn;
4252 RECT rc;
4254 if(!graphics->hdc || !brush_can_fill_path(brush))
4255 return NotImplemented;
4257 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4258 if(status != Ok)
4259 return status;
4261 save_state = SaveDC(graphics->hdc);
4262 EndPath(graphics->hdc);
4264 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4266 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4268 BeginPath(graphics->hdc);
4269 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4270 EndPath(graphics->hdc);
4272 brush_fill_path(graphics, brush);
4275 RestoreDC(graphics->hdc, save_state);
4277 DeleteObject(hrgn);
4279 return Ok;
4282 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4283 GpRegion* region)
4285 GpStatus stat;
4286 GpRegion *temp_region;
4287 GpMatrix world_to_device;
4288 GpRectF graphics_bounds;
4289 DWORD *pixel_data;
4290 HRGN hregion;
4291 RECT bound_rect;
4292 GpRect gp_bound_rect;
4294 if (!brush_can_fill_pixels(brush))
4295 return NotImplemented;
4297 stat = get_graphics_bounds(graphics, &graphics_bounds);
4299 if (stat == Ok)
4300 stat = GdipCloneRegion(region, &temp_region);
4302 if (stat == Ok)
4304 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4305 CoordinateSpaceWorld, &world_to_device);
4307 if (stat == Ok)
4308 stat = GdipTransformRegion(temp_region, &world_to_device);
4310 if (stat == Ok)
4311 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4313 if (stat == Ok)
4314 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4316 GdipDeleteRegion(temp_region);
4319 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4321 DeleteObject(hregion);
4322 return Ok;
4325 if (stat == Ok)
4327 gp_bound_rect.X = bound_rect.left;
4328 gp_bound_rect.Y = bound_rect.top;
4329 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4330 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4332 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4333 if (!pixel_data)
4334 stat = OutOfMemory;
4336 if (stat == Ok)
4338 stat = brush_fill_pixels(graphics, brush, pixel_data,
4339 &gp_bound_rect, gp_bound_rect.Width);
4341 if (stat == Ok)
4342 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4343 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4344 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4346 GdipFree(pixel_data);
4349 DeleteObject(hregion);
4352 return stat;
4355 /*****************************************************************************
4356 * GdipFillRegion [GDIPLUS.@]
4358 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4359 GpRegion* region)
4361 GpStatus stat = NotImplemented;
4363 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4365 if (!(graphics && brush && region))
4366 return InvalidParameter;
4368 if(graphics->busy)
4369 return ObjectBusy;
4371 if (!graphics->image && !graphics->alpha_hdc)
4372 stat = GDI32_GdipFillRegion(graphics, brush, region);
4374 if (stat == NotImplemented)
4375 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4377 if (stat == NotImplemented)
4379 FIXME("not implemented for brushtype %i\n", brush->bt);
4380 stat = Ok;
4383 return stat;
4386 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4388 TRACE("(%p,%u)\n", graphics, intention);
4390 if(!graphics)
4391 return InvalidParameter;
4393 if(graphics->busy)
4394 return ObjectBusy;
4396 /* We have no internal operation queue, so there's no need to clear it. */
4398 if (graphics->hdc)
4399 GdiFlush();
4401 return Ok;
4404 /*****************************************************************************
4405 * GdipGetClipBounds [GDIPLUS.@]
4407 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4409 TRACE("(%p, %p)\n", graphics, rect);
4411 if(!graphics)
4412 return InvalidParameter;
4414 if(graphics->busy)
4415 return ObjectBusy;
4417 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4420 /*****************************************************************************
4421 * GdipGetClipBoundsI [GDIPLUS.@]
4423 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4425 TRACE("(%p, %p)\n", graphics, rect);
4427 if(!graphics)
4428 return InvalidParameter;
4430 if(graphics->busy)
4431 return ObjectBusy;
4433 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4436 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4437 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4438 CompositingMode *mode)
4440 TRACE("(%p, %p)\n", graphics, mode);
4442 if(!graphics || !mode)
4443 return InvalidParameter;
4445 if(graphics->busy)
4446 return ObjectBusy;
4448 *mode = graphics->compmode;
4450 return Ok;
4453 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4454 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4455 CompositingQuality *quality)
4457 TRACE("(%p, %p)\n", graphics, quality);
4459 if(!graphics || !quality)
4460 return InvalidParameter;
4462 if(graphics->busy)
4463 return ObjectBusy;
4465 *quality = graphics->compqual;
4467 return Ok;
4470 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4471 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4472 InterpolationMode *mode)
4474 TRACE("(%p, %p)\n", graphics, mode);
4476 if(!graphics || !mode)
4477 return InvalidParameter;
4479 if(graphics->busy)
4480 return ObjectBusy;
4482 *mode = graphics->interpolation;
4484 return Ok;
4487 /* FIXME: Need to handle color depths less than 24bpp */
4488 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4490 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4492 if(!graphics || !argb)
4493 return InvalidParameter;
4495 if(graphics->busy)
4496 return ObjectBusy;
4498 return Ok;
4501 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4503 TRACE("(%p, %p)\n", graphics, scale);
4505 if(!graphics || !scale)
4506 return InvalidParameter;
4508 if(graphics->busy)
4509 return ObjectBusy;
4511 *scale = graphics->scale;
4513 return Ok;
4516 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4518 TRACE("(%p, %p)\n", graphics, unit);
4520 if(!graphics || !unit)
4521 return InvalidParameter;
4523 if(graphics->busy)
4524 return ObjectBusy;
4526 *unit = graphics->unit;
4528 return Ok;
4531 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4532 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4533 *mode)
4535 TRACE("(%p, %p)\n", graphics, mode);
4537 if(!graphics || !mode)
4538 return InvalidParameter;
4540 if(graphics->busy)
4541 return ObjectBusy;
4543 *mode = graphics->pixeloffset;
4545 return Ok;
4548 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4549 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4551 TRACE("(%p, %p)\n", graphics, mode);
4553 if(!graphics || !mode)
4554 return InvalidParameter;
4556 if(graphics->busy)
4557 return ObjectBusy;
4559 *mode = graphics->smoothing;
4561 return Ok;
4564 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4566 TRACE("(%p, %p)\n", graphics, contrast);
4568 if(!graphics || !contrast)
4569 return InvalidParameter;
4571 *contrast = graphics->textcontrast;
4573 return Ok;
4576 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4577 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4578 TextRenderingHint *hint)
4580 TRACE("(%p, %p)\n", graphics, hint);
4582 if(!graphics || !hint)
4583 return InvalidParameter;
4585 if(graphics->busy)
4586 return ObjectBusy;
4588 *hint = graphics->texthint;
4590 return Ok;
4593 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4595 GpRegion *clip_rgn;
4596 GpStatus stat;
4598 TRACE("(%p, %p)\n", graphics, rect);
4600 if(!graphics || !rect)
4601 return InvalidParameter;
4603 if(graphics->busy)
4604 return ObjectBusy;
4606 /* intersect window and graphics clipping regions */
4607 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4608 return stat;
4610 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4611 goto cleanup;
4613 /* get bounds of the region */
4614 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4616 cleanup:
4617 GdipDeleteRegion(clip_rgn);
4619 return stat;
4622 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4624 GpRectF rectf;
4625 GpStatus stat;
4627 TRACE("(%p, %p)\n", graphics, rect);
4629 if(!graphics || !rect)
4630 return InvalidParameter;
4632 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4634 rect->X = gdip_round(rectf.X);
4635 rect->Y = gdip_round(rectf.Y);
4636 rect->Width = gdip_round(rectf.Width);
4637 rect->Height = gdip_round(rectf.Height);
4640 return stat;
4643 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4645 TRACE("(%p, %p)\n", graphics, matrix);
4647 if(!graphics || !matrix)
4648 return InvalidParameter;
4650 if(graphics->busy)
4651 return ObjectBusy;
4653 *matrix = graphics->worldtrans;
4654 return Ok;
4657 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4659 GpSolidFill *brush;
4660 GpStatus stat;
4661 GpRectF wnd_rect;
4663 TRACE("(%p, %x)\n", graphics, color);
4665 if(!graphics)
4666 return InvalidParameter;
4668 if(graphics->busy)
4669 return ObjectBusy;
4671 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4672 return stat;
4674 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4675 GdipDeleteBrush((GpBrush*)brush);
4676 return stat;
4679 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4680 wnd_rect.Width, wnd_rect.Height);
4682 GdipDeleteBrush((GpBrush*)brush);
4684 return Ok;
4687 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4689 TRACE("(%p, %p)\n", graphics, res);
4691 if(!graphics || !res)
4692 return InvalidParameter;
4694 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4697 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4699 GpStatus stat;
4700 GpRegion* rgn;
4701 GpPointF pt;
4703 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4705 if(!graphics || !result)
4706 return InvalidParameter;
4708 if(graphics->busy)
4709 return ObjectBusy;
4711 pt.X = x;
4712 pt.Y = y;
4713 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4714 CoordinateSpaceWorld, &pt, 1)) != Ok)
4715 return stat;
4717 if((stat = GdipCreateRegion(&rgn)) != Ok)
4718 return stat;
4720 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4721 goto cleanup;
4723 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4725 cleanup:
4726 GdipDeleteRegion(rgn);
4727 return stat;
4730 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4732 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4735 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4737 GpStatus stat;
4738 GpRegion* rgn;
4739 GpPointF pts[2];
4741 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4743 if(!graphics || !result)
4744 return InvalidParameter;
4746 if(graphics->busy)
4747 return ObjectBusy;
4749 pts[0].X = x;
4750 pts[0].Y = y;
4751 pts[1].X = x + width;
4752 pts[1].Y = y + height;
4754 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4755 CoordinateSpaceWorld, pts, 2)) != Ok)
4756 return stat;
4758 pts[1].X -= pts[0].X;
4759 pts[1].Y -= pts[0].Y;
4761 if((stat = GdipCreateRegion(&rgn)) != Ok)
4762 return stat;
4764 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4765 goto cleanup;
4767 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4769 cleanup:
4770 GdipDeleteRegion(rgn);
4771 return stat;
4774 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4776 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4779 GpStatus gdip_format_string(HDC hdc,
4780 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4781 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4782 gdip_format_string_callback callback, void *user_data)
4784 WCHAR* stringdup;
4785 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4786 nheight, lineend, lineno = 0;
4787 RectF bounds;
4788 StringAlignment halign;
4789 GpStatus stat = Ok;
4790 SIZE size;
4791 HotkeyPrefix hkprefix;
4792 INT *hotkeyprefix_offsets=NULL;
4793 INT hotkeyprefix_count=0;
4794 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4795 int seen_prefix=0;
4797 if(length == -1) length = lstrlenW(string);
4799 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4800 if(!stringdup) return OutOfMemory;
4802 nwidth = rect->Width;
4803 nheight = rect->Height;
4805 if (format)
4806 hkprefix = format->hkprefix;
4807 else
4808 hkprefix = HotkeyPrefixNone;
4810 if (hkprefix == HotkeyPrefixShow)
4812 for (i=0; i<length; i++)
4814 if (string[i] == '&')
4815 hotkeyprefix_count++;
4819 if (hotkeyprefix_count)
4820 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4822 hotkeyprefix_count = 0;
4824 for(i = 0, j = 0; i < length; i++){
4825 /* FIXME: This makes the indexes passed to callback inaccurate. */
4826 if(!isprintW(string[i]) && (string[i] != '\n'))
4827 continue;
4829 /* FIXME: tabs should be handled using tabstops from stringformat */
4830 if (string[i] == '\t')
4831 continue;
4833 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4834 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4835 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4837 seen_prefix = 1;
4838 continue;
4841 seen_prefix = 0;
4843 stringdup[j] = string[i];
4844 j++;
4847 length = j;
4849 if (format) halign = format->align;
4850 else halign = StringAlignmentNear;
4852 while(sum < length){
4853 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4854 nwidth, &fit, NULL, &size);
4855 fitcpy = fit;
4857 if(fit == 0)
4858 break;
4860 for(lret = 0; lret < fit; lret++)
4861 if(*(stringdup + sum + lret) == '\n')
4862 break;
4864 /* Line break code (may look strange, but it imitates windows). */
4865 if(lret < fit)
4866 lineend = fit = lret; /* this is not an off-by-one error */
4867 else if(fit < (length - sum)){
4868 if(*(stringdup + sum + fit) == ' ')
4869 while(*(stringdup + sum + fit) == ' ')
4870 fit++;
4871 else
4872 while(*(stringdup + sum + fit - 1) != ' '){
4873 fit--;
4875 if(*(stringdup + sum + fit) == '\t')
4876 break;
4878 if(fit == 0){
4879 fit = fitcpy;
4880 break;
4883 lineend = fit;
4884 while(*(stringdup + sum + lineend - 1) == ' ' ||
4885 *(stringdup + sum + lineend - 1) == '\t')
4886 lineend--;
4888 else
4889 lineend = fit;
4891 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4892 nwidth, &j, NULL, &size);
4894 bounds.Width = size.cx;
4896 if(height + size.cy > nheight)
4897 bounds.Height = nheight - (height + size.cy);
4898 else
4899 bounds.Height = size.cy;
4901 bounds.Y = rect->Y + height;
4903 switch (halign)
4905 case StringAlignmentNear:
4906 default:
4907 bounds.X = rect->X;
4908 break;
4909 case StringAlignmentCenter:
4910 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4911 break;
4912 case StringAlignmentFar:
4913 bounds.X = rect->X + rect->Width - bounds.Width;
4914 break;
4917 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4918 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4919 break;
4921 stat = callback(hdc, stringdup, sum, lineend,
4922 font, rect, format, lineno, &bounds,
4923 &hotkeyprefix_offsets[hotkeyprefix_pos],
4924 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4926 if (stat != Ok)
4927 break;
4929 sum += fit + (lret < fitcpy ? 1 : 0);
4930 height += size.cy;
4931 lineno++;
4933 hotkeyprefix_pos = hotkeyprefix_end_pos;
4935 if(height > nheight)
4936 break;
4938 /* Stop if this was a linewrap (but not if it was a linebreak). */
4939 if ((lret == fitcpy) && format &&
4940 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4941 break;
4944 GdipFree(stringdup);
4945 GdipFree(hotkeyprefix_offsets);
4947 return stat;
4950 struct measure_ranges_args {
4951 GpRegion **regions;
4952 REAL rel_width, rel_height;
4955 static GpStatus measure_ranges_callback(HDC hdc,
4956 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4957 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4958 INT lineno, const RectF *bounds, INT *underlined_indexes,
4959 INT underlined_index_count, void *user_data)
4961 int i;
4962 GpStatus stat = Ok;
4963 struct measure_ranges_args *args = user_data;
4965 for (i=0; i<format->range_count; i++)
4967 INT range_start = max(index, format->character_ranges[i].First);
4968 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4969 if (range_start < range_end)
4971 GpRectF range_rect;
4972 SIZE range_size;
4974 range_rect.Y = bounds->Y / args->rel_height;
4975 range_rect.Height = bounds->Height / args->rel_height;
4977 GetTextExtentExPointW(hdc, string + index, range_start - index,
4978 INT_MAX, NULL, NULL, &range_size);
4979 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4981 GetTextExtentExPointW(hdc, string + index, range_end - index,
4982 INT_MAX, NULL, NULL, &range_size);
4983 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4985 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4986 if (stat != Ok)
4987 break;
4991 return stat;
4994 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4995 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4996 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4997 INT regionCount, GpRegion** regions)
4999 GpStatus stat;
5000 int i;
5001 HFONT gdifont, oldfont;
5002 struct measure_ranges_args args;
5003 HDC hdc, temp_hdc=NULL;
5004 GpPointF pt[3];
5005 RectF scaled_rect;
5006 REAL margin_x;
5008 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
5009 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5011 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5012 return InvalidParameter;
5014 if (regionCount < stringFormat->range_count)
5015 return InvalidParameter;
5017 if(!graphics->hdc)
5019 hdc = temp_hdc = CreateCompatibleDC(0);
5020 if (!temp_hdc) return OutOfMemory;
5022 else
5023 hdc = graphics->hdc;
5025 if (stringFormat->attr)
5026 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5028 pt[0].X = 0.0;
5029 pt[0].Y = 0.0;
5030 pt[1].X = 1.0;
5031 pt[1].Y = 0.0;
5032 pt[2].X = 0.0;
5033 pt[2].Y = 1.0;
5034 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5035 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5036 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5037 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5038 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5040 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5041 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5043 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5044 scaled_rect.Y = layoutRect->Y * args.rel_height;
5045 if (stringFormat->attr & StringFormatFlagsNoClip)
5047 scaled_rect.Width = (REAL)(1 << 23);
5048 scaled_rect.Height = (REAL)(1 << 23);
5050 else
5052 scaled_rect.Width = layoutRect->Width * args.rel_width;
5053 scaled_rect.Height = layoutRect->Height * args.rel_height;
5055 if (scaled_rect.Width >= 0.5)
5057 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5058 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5061 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5062 oldfont = SelectObject(hdc, gdifont);
5064 for (i=0; i<stringFormat->range_count; i++)
5066 stat = GdipSetEmpty(regions[i]);
5067 if (stat != Ok)
5068 return stat;
5071 args.regions = regions;
5073 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5074 measure_ranges_callback, &args);
5076 SelectObject(hdc, oldfont);
5077 DeleteObject(gdifont);
5079 if (temp_hdc)
5080 DeleteDC(temp_hdc);
5082 return stat;
5085 struct measure_string_args {
5086 RectF *bounds;
5087 INT *codepointsfitted;
5088 INT *linesfilled;
5089 REAL rel_width, rel_height;
5092 static GpStatus measure_string_callback(HDC hdc,
5093 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5094 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5095 INT lineno, const RectF *bounds, INT *underlined_indexes,
5096 INT underlined_index_count, void *user_data)
5098 struct measure_string_args *args = user_data;
5099 REAL new_width, new_height;
5101 new_width = bounds->Width / args->rel_width;
5102 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5104 if (new_width > args->bounds->Width)
5105 args->bounds->Width = new_width;
5107 if (new_height > args->bounds->Height)
5108 args->bounds->Height = new_height;
5110 if (args->codepointsfitted)
5111 *args->codepointsfitted = index + length;
5113 if (args->linesfilled)
5114 (*args->linesfilled)++;
5116 return Ok;
5119 /* Find the smallest rectangle that bounds the text when it is printed in rect
5120 * according to the format options listed in format. If rect has 0 width and
5121 * height, then just find the smallest rectangle that bounds the text when it's
5122 * printed at location (rect->X, rect-Y). */
5123 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5124 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5125 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5126 INT *codepointsfitted, INT *linesfilled)
5128 HFONT oldfont, gdifont;
5129 struct measure_string_args args;
5130 HDC temp_hdc=NULL, hdc;
5131 GpPointF pt[3];
5132 RectF scaled_rect;
5133 REAL margin_x;
5134 INT lines, glyphs, format_flags = format ? format->attr : 0;
5136 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5137 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5138 bounds, codepointsfitted, linesfilled);
5140 if(!graphics || !string || !font || !rect || !bounds)
5141 return InvalidParameter;
5143 if(!graphics->hdc)
5145 hdc = temp_hdc = CreateCompatibleDC(0);
5146 if (!temp_hdc) return OutOfMemory;
5148 else
5149 hdc = graphics->hdc;
5151 if(linesfilled) *linesfilled = 0;
5152 if(codepointsfitted) *codepointsfitted = 0;
5154 if(format)
5155 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5157 pt[0].X = 0.0;
5158 pt[0].Y = 0.0;
5159 pt[1].X = 1.0;
5160 pt[1].Y = 0.0;
5161 pt[2].X = 0.0;
5162 pt[2].Y = 1.0;
5163 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5164 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5165 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5166 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5167 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5169 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5170 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5172 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5173 scaled_rect.Y = rect->Y * args.rel_height;
5174 scaled_rect.Width = rect->Width * args.rel_width;
5175 scaled_rect.Height = rect->Height * args.rel_height;
5177 if ((format_flags & StringFormatFlagsNoClip) ||
5178 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5179 if ((format_flags & StringFormatFlagsNoClip) ||
5180 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5182 if (scaled_rect.Width >= 0.5)
5184 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5185 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5188 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5189 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5191 get_font_hfont(graphics, font, format, &gdifont, NULL);
5192 oldfont = SelectObject(hdc, gdifont);
5194 bounds->X = rect->X;
5195 bounds->Y = rect->Y;
5196 bounds->Width = 0.0;
5197 bounds->Height = 0.0;
5199 args.bounds = bounds;
5200 args.codepointsfitted = &glyphs;
5201 args.linesfilled = &lines;
5202 lines = glyphs = 0;
5204 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5205 measure_string_callback, &args);
5207 if (linesfilled) *linesfilled = lines;
5208 if (codepointsfitted) *codepointsfitted = glyphs;
5210 if (lines)
5211 bounds->Width += margin_x * 2.0;
5213 SelectObject(hdc, oldfont);
5214 DeleteObject(gdifont);
5216 if (temp_hdc)
5217 DeleteDC(temp_hdc);
5219 return Ok;
5222 struct draw_string_args {
5223 GpGraphics *graphics;
5224 GDIPCONST GpBrush *brush;
5225 REAL x, y, rel_width, rel_height, ascent;
5228 static GpStatus draw_string_callback(HDC hdc,
5229 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5230 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5231 INT lineno, const RectF *bounds, INT *underlined_indexes,
5232 INT underlined_index_count, void *user_data)
5234 struct draw_string_args *args = user_data;
5235 PointF position;
5236 GpStatus stat;
5238 position.X = args->x + bounds->X / args->rel_width;
5239 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5241 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5242 args->brush, &position,
5243 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5245 if (stat == Ok && underlined_index_count)
5247 OUTLINETEXTMETRICW otm;
5248 REAL underline_y, underline_height;
5249 int i;
5251 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5253 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5254 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5256 for (i=0; i<underlined_index_count; i++)
5258 REAL start_x, end_x;
5259 SIZE text_size;
5260 INT ofs = underlined_indexes[i] - index;
5262 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5263 start_x = text_size.cx / args->rel_width;
5265 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5266 end_x = text_size.cx / args->rel_width;
5268 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5272 return stat;
5275 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5276 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5277 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5279 HRGN rgn = NULL;
5280 HFONT gdifont;
5281 GpPointF pt[3], rectcpy[4];
5282 POINT corners[4];
5283 REAL rel_width, rel_height, margin_x;
5284 INT save_state, format_flags = 0;
5285 REAL offsety = 0.0;
5286 struct draw_string_args args;
5287 RectF scaled_rect;
5288 HDC hdc, temp_hdc=NULL;
5289 TEXTMETRICW textmetric;
5291 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5292 length, font, debugstr_rectf(rect), format, brush);
5294 if(!graphics || !string || !font || !brush || !rect)
5295 return InvalidParameter;
5297 if(graphics->hdc)
5299 hdc = graphics->hdc;
5301 else
5303 hdc = temp_hdc = CreateCompatibleDC(0);
5306 if(format){
5307 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5309 format_flags = format->attr;
5311 /* Should be no need to explicitly test for StringAlignmentNear as
5312 * that is default behavior if no alignment is passed. */
5313 if(format->vertalign != StringAlignmentNear){
5314 RectF bounds, in_rect = *rect;
5315 in_rect.Height = 0.0; /* avoid height clipping */
5316 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5318 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5320 if(format->vertalign == StringAlignmentCenter)
5321 offsety = (rect->Height - bounds.Height) / 2;
5322 else if(format->vertalign == StringAlignmentFar)
5323 offsety = (rect->Height - bounds.Height);
5325 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5328 save_state = SaveDC(hdc);
5330 pt[0].X = 0.0;
5331 pt[0].Y = 0.0;
5332 pt[1].X = 1.0;
5333 pt[1].Y = 0.0;
5334 pt[2].X = 0.0;
5335 pt[2].Y = 1.0;
5336 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5337 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5338 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5339 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5340 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5342 rectcpy[3].X = rectcpy[0].X = rect->X;
5343 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5344 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5345 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5346 transform_and_round_points(graphics, corners, rectcpy, 4);
5348 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5349 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5351 scaled_rect.X = margin_x * rel_width;
5352 scaled_rect.Y = 0.0;
5353 scaled_rect.Width = rel_width * rect->Width;
5354 scaled_rect.Height = rel_height * rect->Height;
5356 if ((format_flags & StringFormatFlagsNoClip) ||
5357 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5358 if ((format_flags & StringFormatFlagsNoClip) ||
5359 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5361 if (scaled_rect.Width >= 0.5)
5363 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5364 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5367 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5368 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5370 if (!(format_flags & StringFormatFlagsNoClip) &&
5371 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23)
5373 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5374 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5375 SelectClipRgn(hdc, rgn);
5378 get_font_hfont(graphics, font, format, &gdifont, NULL);
5379 SelectObject(hdc, gdifont);
5381 args.graphics = graphics;
5382 args.brush = brush;
5384 args.x = rect->X;
5385 args.y = rect->Y + offsety;
5387 args.rel_width = rel_width;
5388 args.rel_height = rel_height;
5390 GetTextMetricsW(hdc, &textmetric);
5391 args.ascent = textmetric.tmAscent / rel_height;
5393 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5394 draw_string_callback, &args);
5396 DeleteObject(rgn);
5397 DeleteObject(gdifont);
5399 RestoreDC(hdc, save_state);
5401 DeleteDC(temp_hdc);
5403 return Ok;
5406 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5408 TRACE("(%p)\n", graphics);
5410 if(!graphics)
5411 return InvalidParameter;
5413 if(graphics->busy)
5414 return ObjectBusy;
5416 return GdipSetInfinite(graphics->clip);
5419 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5421 TRACE("(%p)\n", graphics);
5423 if(!graphics)
5424 return InvalidParameter;
5426 if(graphics->busy)
5427 return ObjectBusy;
5429 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5432 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5434 return GdipEndContainer(graphics, state);
5437 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5438 GpMatrixOrder order)
5440 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5442 if(!graphics)
5443 return InvalidParameter;
5445 if(graphics->busy)
5446 return ObjectBusy;
5448 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5451 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5453 return GdipBeginContainer2(graphics, state);
5456 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5457 GraphicsContainer *state)
5459 GraphicsContainerItem *container;
5460 GpStatus sts;
5462 TRACE("(%p, %p)\n", graphics, state);
5464 if(!graphics || !state)
5465 return InvalidParameter;
5467 sts = init_container(&container, graphics);
5468 if(sts != Ok)
5469 return sts;
5471 list_add_head(&graphics->containers, &container->entry);
5472 *state = graphics->contid = container->contid;
5474 return Ok;
5477 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5479 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5480 return NotImplemented;
5483 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5485 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5486 return NotImplemented;
5489 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5491 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5492 return NotImplemented;
5495 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5497 GpStatus sts;
5498 GraphicsContainerItem *container, *container2;
5500 TRACE("(%p, %x)\n", graphics, state);
5502 if(!graphics)
5503 return InvalidParameter;
5505 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5506 if(container->contid == state)
5507 break;
5510 /* did not find a matching container */
5511 if(&container->entry == &graphics->containers)
5512 return Ok;
5514 sts = restore_container(graphics, container);
5515 if(sts != Ok)
5516 return sts;
5518 /* remove all of the containers on top of the found container */
5519 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5520 if(container->contid == state)
5521 break;
5522 list_remove(&container->entry);
5523 delete_container(container);
5526 list_remove(&container->entry);
5527 delete_container(container);
5529 return Ok;
5532 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5533 REAL sy, GpMatrixOrder order)
5535 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5537 if(!graphics)
5538 return InvalidParameter;
5540 if(graphics->busy)
5541 return ObjectBusy;
5543 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5546 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5547 CombineMode mode)
5549 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5551 if(!graphics || !srcgraphics)
5552 return InvalidParameter;
5554 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5557 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5558 CompositingMode mode)
5560 TRACE("(%p, %d)\n", graphics, mode);
5562 if(!graphics)
5563 return InvalidParameter;
5565 if(graphics->busy)
5566 return ObjectBusy;
5568 graphics->compmode = mode;
5570 return Ok;
5573 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5574 CompositingQuality quality)
5576 TRACE("(%p, %d)\n", graphics, quality);
5578 if(!graphics)
5579 return InvalidParameter;
5581 if(graphics->busy)
5582 return ObjectBusy;
5584 graphics->compqual = quality;
5586 return Ok;
5589 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5590 InterpolationMode mode)
5592 TRACE("(%p, %d)\n", graphics, mode);
5594 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5595 return InvalidParameter;
5597 if(graphics->busy)
5598 return ObjectBusy;
5600 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5601 mode = InterpolationModeBilinear;
5603 if (mode == InterpolationModeHighQuality)
5604 mode = InterpolationModeHighQualityBicubic;
5606 graphics->interpolation = mode;
5608 return Ok;
5611 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5613 TRACE("(%p, %.2f)\n", graphics, scale);
5615 if(!graphics || (scale <= 0.0))
5616 return InvalidParameter;
5618 if(graphics->busy)
5619 return ObjectBusy;
5621 graphics->scale = scale;
5623 return Ok;
5626 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5628 TRACE("(%p, %d)\n", graphics, unit);
5630 if(!graphics)
5631 return InvalidParameter;
5633 if(graphics->busy)
5634 return ObjectBusy;
5636 if(unit == UnitWorld)
5637 return InvalidParameter;
5639 graphics->unit = unit;
5641 return Ok;
5644 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5645 mode)
5647 TRACE("(%p, %d)\n", graphics, mode);
5649 if(!graphics)
5650 return InvalidParameter;
5652 if(graphics->busy)
5653 return ObjectBusy;
5655 graphics->pixeloffset = mode;
5657 return Ok;
5660 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5662 static int calls;
5664 TRACE("(%p,%i,%i)\n", graphics, x, y);
5666 if (!(calls++))
5667 FIXME("value is unused in rendering\n");
5669 if (!graphics)
5670 return InvalidParameter;
5672 graphics->origin_x = x;
5673 graphics->origin_y = y;
5675 return Ok;
5678 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5680 TRACE("(%p,%p,%p)\n", graphics, x, y);
5682 if (!graphics || !x || !y)
5683 return InvalidParameter;
5685 *x = graphics->origin_x;
5686 *y = graphics->origin_y;
5688 return Ok;
5691 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5693 TRACE("(%p, %d)\n", graphics, mode);
5695 if(!graphics)
5696 return InvalidParameter;
5698 if(graphics->busy)
5699 return ObjectBusy;
5701 graphics->smoothing = mode;
5703 return Ok;
5706 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5708 TRACE("(%p, %d)\n", graphics, contrast);
5710 if(!graphics)
5711 return InvalidParameter;
5713 graphics->textcontrast = contrast;
5715 return Ok;
5718 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5719 TextRenderingHint hint)
5721 TRACE("(%p, %d)\n", graphics, hint);
5723 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5724 return InvalidParameter;
5726 if(graphics->busy)
5727 return ObjectBusy;
5729 graphics->texthint = hint;
5731 return Ok;
5734 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5736 TRACE("(%p, %p)\n", graphics, matrix);
5738 if(!graphics || !matrix)
5739 return InvalidParameter;
5741 if(graphics->busy)
5742 return ObjectBusy;
5744 TRACE("%f,%f,%f,%f,%f,%f\n",
5745 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5746 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5748 graphics->worldtrans = *matrix;
5750 return Ok;
5753 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5754 REAL dy, GpMatrixOrder order)
5756 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5758 if(!graphics)
5759 return InvalidParameter;
5761 if(graphics->busy)
5762 return ObjectBusy;
5764 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5767 /*****************************************************************************
5768 * GdipSetClipHrgn [GDIPLUS.@]
5770 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5772 GpRegion *region;
5773 GpStatus status;
5775 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5777 if(!graphics)
5778 return InvalidParameter;
5780 status = GdipCreateRegionHrgn(hrgn, &region);
5781 if(status != Ok)
5782 return status;
5784 status = GdipSetClipRegion(graphics, region, mode);
5786 GdipDeleteRegion(region);
5787 return status;
5790 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5792 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5794 if(!graphics)
5795 return InvalidParameter;
5797 if(graphics->busy)
5798 return ObjectBusy;
5800 return GdipCombineRegionPath(graphics->clip, path, mode);
5803 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5804 REAL width, REAL height,
5805 CombineMode mode)
5807 GpRectF rect;
5809 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5811 if(!graphics)
5812 return InvalidParameter;
5814 if(graphics->busy)
5815 return ObjectBusy;
5817 rect.X = x;
5818 rect.Y = y;
5819 rect.Width = width;
5820 rect.Height = height;
5822 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5825 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5826 INT width, INT height,
5827 CombineMode mode)
5829 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5831 if(!graphics)
5832 return InvalidParameter;
5834 if(graphics->busy)
5835 return ObjectBusy;
5837 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5840 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5841 CombineMode mode)
5843 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5845 if(!graphics || !region)
5846 return InvalidParameter;
5848 if(graphics->busy)
5849 return ObjectBusy;
5851 return GdipCombineRegionRegion(graphics->clip, region, mode);
5854 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5855 UINT limitDpi)
5857 static int calls;
5859 TRACE("(%p,%u)\n", metafile, limitDpi);
5861 if(!(calls++))
5862 FIXME("not implemented\n");
5864 return NotImplemented;
5867 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5868 INT count)
5870 INT save_state;
5871 POINT *pti;
5873 TRACE("(%p, %p, %d)\n", graphics, points, count);
5875 if(!graphics || !pen || count<=0)
5876 return InvalidParameter;
5878 if(graphics->busy)
5879 return ObjectBusy;
5881 if (!graphics->hdc)
5883 FIXME("graphics object has no HDC\n");
5884 return Ok;
5887 pti = GdipAlloc(sizeof(POINT) * count);
5889 save_state = prepare_dc(graphics, pen);
5890 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5892 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5893 Polygon(graphics->hdc, pti, count);
5895 restore_dc(graphics, save_state);
5896 GdipFree(pti);
5898 return Ok;
5901 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5902 INT count)
5904 GpStatus ret;
5905 GpPointF *ptf;
5906 INT i;
5908 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5910 if(count<=0) return InvalidParameter;
5911 ptf = GdipAlloc(sizeof(GpPointF) * count);
5913 for(i = 0;i < count; i++){
5914 ptf[i].X = (REAL)points[i].X;
5915 ptf[i].Y = (REAL)points[i].Y;
5918 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5919 GdipFree(ptf);
5921 return ret;
5924 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5926 TRACE("(%p, %p)\n", graphics, dpi);
5928 if(!graphics || !dpi)
5929 return InvalidParameter;
5931 if(graphics->busy)
5932 return ObjectBusy;
5934 *dpi = graphics->xres;
5935 return Ok;
5938 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5940 TRACE("(%p, %p)\n", graphics, dpi);
5942 if(!graphics || !dpi)
5943 return InvalidParameter;
5945 if(graphics->busy)
5946 return ObjectBusy;
5948 *dpi = graphics->yres;
5949 return Ok;
5952 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5953 GpMatrixOrder order)
5955 GpMatrix m;
5956 GpStatus ret;
5958 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5960 if(!graphics || !matrix)
5961 return InvalidParameter;
5963 if(graphics->busy)
5964 return ObjectBusy;
5966 m = graphics->worldtrans;
5968 ret = GdipMultiplyMatrix(&m, matrix, order);
5969 if(ret == Ok)
5970 graphics->worldtrans = m;
5972 return ret;
5975 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5976 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5978 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5980 GpStatus stat=Ok;
5982 TRACE("(%p, %p)\n", graphics, hdc);
5984 if(!graphics || !hdc)
5985 return InvalidParameter;
5987 if(graphics->busy)
5988 return ObjectBusy;
5990 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5992 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5994 else if (!graphics->hdc || graphics->alpha_hdc ||
5995 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5997 /* Create a fake HDC and fill it with a constant color. */
5998 HDC temp_hdc;
5999 HBITMAP hbitmap;
6000 GpRectF bounds;
6001 BITMAPINFOHEADER bmih;
6002 int i;
6004 stat = get_graphics_bounds(graphics, &bounds);
6005 if (stat != Ok)
6006 return stat;
6008 graphics->temp_hbitmap_width = bounds.Width;
6009 graphics->temp_hbitmap_height = bounds.Height;
6011 bmih.biSize = sizeof(bmih);
6012 bmih.biWidth = graphics->temp_hbitmap_width;
6013 bmih.biHeight = -graphics->temp_hbitmap_height;
6014 bmih.biPlanes = 1;
6015 bmih.biBitCount = 32;
6016 bmih.biCompression = BI_RGB;
6017 bmih.biSizeImage = 0;
6018 bmih.biXPelsPerMeter = 0;
6019 bmih.biYPelsPerMeter = 0;
6020 bmih.biClrUsed = 0;
6021 bmih.biClrImportant = 0;
6023 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6024 (void**)&graphics->temp_bits, NULL, 0);
6025 if (!hbitmap)
6026 return GenericError;
6028 temp_hdc = CreateCompatibleDC(0);
6029 if (!temp_hdc)
6031 DeleteObject(hbitmap);
6032 return GenericError;
6035 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6036 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6038 SelectObject(temp_hdc, hbitmap);
6040 graphics->temp_hbitmap = hbitmap;
6041 *hdc = graphics->temp_hdc = temp_hdc;
6043 else
6045 *hdc = graphics->hdc;
6048 if (stat == Ok)
6049 graphics->busy = TRUE;
6051 return stat;
6054 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6056 GpStatus stat=Ok;
6058 TRACE("(%p, %p)\n", graphics, hdc);
6060 if(!graphics || !hdc || !graphics->busy)
6061 return InvalidParameter;
6063 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6065 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6067 else if (graphics->temp_hdc == hdc)
6069 DWORD* pos;
6070 int i;
6072 /* Find the pixels that have changed, and mark them as opaque. */
6073 pos = (DWORD*)graphics->temp_bits;
6074 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6076 if (*pos != DC_BACKGROUND_KEY)
6078 *pos |= 0xff000000;
6080 pos++;
6083 /* Write the changed pixels to the real target. */
6084 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6085 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6086 graphics->temp_hbitmap_width * 4);
6088 /* Clean up. */
6089 DeleteDC(graphics->temp_hdc);
6090 DeleteObject(graphics->temp_hbitmap);
6091 graphics->temp_hdc = NULL;
6092 graphics->temp_hbitmap = NULL;
6094 else if (hdc != graphics->hdc)
6096 stat = InvalidParameter;
6099 if (stat == Ok)
6100 graphics->busy = FALSE;
6102 return stat;
6105 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6107 GpRegion *clip;
6108 GpStatus status;
6110 TRACE("(%p, %p)\n", graphics, region);
6112 if(!graphics || !region)
6113 return InvalidParameter;
6115 if(graphics->busy)
6116 return ObjectBusy;
6118 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6119 return status;
6121 /* free everything except root node and header */
6122 delete_element(&region->node);
6123 memcpy(region, clip, sizeof(GpRegion));
6124 GdipFree(clip);
6126 return Ok;
6129 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6130 GpCoordinateSpace src_space, GpMatrix *matrix)
6132 GpStatus stat = Ok;
6133 REAL scale_x, scale_y;
6135 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6137 if (dst_space != src_space)
6139 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6140 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6142 if(graphics->unit != UnitDisplay)
6144 scale_x *= graphics->scale;
6145 scale_y *= graphics->scale;
6148 /* transform from src_space to CoordinateSpacePage */
6149 switch (src_space)
6151 case CoordinateSpaceWorld:
6152 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6153 break;
6154 case CoordinateSpacePage:
6155 break;
6156 case CoordinateSpaceDevice:
6157 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6158 break;
6161 /* transform from CoordinateSpacePage to dst_space */
6162 switch (dst_space)
6164 case CoordinateSpaceWorld:
6166 GpMatrix inverted_transform = graphics->worldtrans;
6167 stat = GdipInvertMatrix(&inverted_transform);
6168 if (stat == Ok)
6169 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6170 break;
6172 case CoordinateSpacePage:
6173 break;
6174 case CoordinateSpaceDevice:
6175 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6176 break;
6179 return stat;
6182 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6183 GpCoordinateSpace src_space, GpPointF *points, INT count)
6185 GpMatrix matrix;
6186 GpStatus stat;
6188 if(!graphics || !points || count <= 0)
6189 return InvalidParameter;
6191 if(graphics->busy)
6192 return ObjectBusy;
6194 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6196 if (src_space == dst_space) return Ok;
6198 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6199 if (stat != Ok) return stat;
6201 return GdipTransformMatrixPoints(&matrix, points, count);
6204 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6205 GpCoordinateSpace src_space, GpPoint *points, INT count)
6207 GpPointF *pointsF;
6208 GpStatus ret;
6209 INT i;
6211 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6213 if(count <= 0)
6214 return InvalidParameter;
6216 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6217 if(!pointsF)
6218 return OutOfMemory;
6220 for(i = 0; i < count; i++){
6221 pointsF[i].X = (REAL)points[i].X;
6222 pointsF[i].Y = (REAL)points[i].Y;
6225 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6227 if(ret == Ok)
6228 for(i = 0; i < count; i++){
6229 points[i].X = gdip_round(pointsF[i].X);
6230 points[i].Y = gdip_round(pointsF[i].Y);
6232 GdipFree(pointsF);
6234 return ret;
6237 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6239 static int calls;
6241 TRACE("\n");
6243 if (!calls++)
6244 FIXME("stub\n");
6246 return NULL;
6249 /*****************************************************************************
6250 * GdipTranslateClip [GDIPLUS.@]
6252 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6254 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6256 if(!graphics)
6257 return InvalidParameter;
6259 if(graphics->busy)
6260 return ObjectBusy;
6262 return GdipTranslateRegion(graphics->clip, dx, dy);
6265 /*****************************************************************************
6266 * GdipTranslateClipI [GDIPLUS.@]
6268 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6270 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6272 if(!graphics)
6273 return InvalidParameter;
6275 if(graphics->busy)
6276 return ObjectBusy;
6278 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6282 /*****************************************************************************
6283 * GdipMeasureDriverString [GDIPLUS.@]
6285 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6286 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6287 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6289 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6290 HFONT hfont;
6291 HDC hdc;
6292 REAL min_x, min_y, max_x, max_y, x, y;
6293 int i;
6294 TEXTMETRICW textmetric;
6295 const WORD *glyph_indices;
6296 WORD *dynamic_glyph_indices=NULL;
6297 REAL rel_width, rel_height, ascent, descent;
6298 GpPointF pt[3];
6300 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6302 if (!graphics || !text || !font || !positions || !boundingBox)
6303 return InvalidParameter;
6305 if (length == -1)
6306 length = strlenW(text);
6308 if (length == 0)
6310 boundingBox->X = 0.0;
6311 boundingBox->Y = 0.0;
6312 boundingBox->Width = 0.0;
6313 boundingBox->Height = 0.0;
6316 if (flags & unsupported_flags)
6317 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6319 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6321 hdc = CreateCompatibleDC(0);
6322 SelectObject(hdc, hfont);
6324 GetTextMetricsW(hdc, &textmetric);
6326 pt[0].X = 0.0;
6327 pt[0].Y = 0.0;
6328 pt[1].X = 1.0;
6329 pt[1].Y = 0.0;
6330 pt[2].X = 0.0;
6331 pt[2].Y = 1.0;
6332 if (matrix)
6334 GpMatrix xform = *matrix;
6335 GdipTransformMatrixPoints(&xform, pt, 3);
6337 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6338 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6339 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6340 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6341 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6343 if (flags & DriverStringOptionsCmapLookup)
6345 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6346 if (!glyph_indices)
6348 DeleteDC(hdc);
6349 DeleteObject(hfont);
6350 return OutOfMemory;
6353 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6355 else
6356 glyph_indices = text;
6358 min_x = max_x = x = positions[0].X;
6359 min_y = max_y = y = positions[0].Y;
6361 ascent = textmetric.tmAscent / rel_height;
6362 descent = textmetric.tmDescent / rel_height;
6364 for (i=0; i<length; i++)
6366 int char_width;
6367 ABC abc;
6369 if (!(flags & DriverStringOptionsRealizedAdvance))
6371 x = positions[i].X;
6372 y = positions[i].Y;
6375 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6376 char_width = abc.abcA + abc.abcB + abc.abcC;
6378 if (min_y > y - ascent) min_y = y - ascent;
6379 if (max_y < y + descent) max_y = y + descent;
6380 if (min_x > x) min_x = x;
6382 x += char_width / rel_width;
6384 if (max_x < x) max_x = x;
6387 GdipFree(dynamic_glyph_indices);
6388 DeleteDC(hdc);
6389 DeleteObject(hfont);
6391 boundingBox->X = min_x;
6392 boundingBox->Y = min_y;
6393 boundingBox->Width = max_x - min_x;
6394 boundingBox->Height = max_y - min_y;
6396 return Ok;
6399 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6400 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6401 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6402 INT flags, GDIPCONST GpMatrix *matrix)
6404 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6405 INT save_state;
6406 GpPointF pt;
6407 HFONT hfont;
6408 UINT eto_flags=0;
6410 if (flags & unsupported_flags)
6411 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6413 if (!(flags & DriverStringOptionsCmapLookup))
6414 eto_flags |= ETO_GLYPH_INDEX;
6416 save_state = SaveDC(graphics->hdc);
6417 SetBkMode(graphics->hdc, TRANSPARENT);
6418 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6420 pt = positions[0];
6421 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6423 get_font_hfont(graphics, font, format, &hfont, matrix);
6424 SelectObject(graphics->hdc, hfont);
6426 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6428 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6430 RestoreDC(graphics->hdc, save_state);
6432 DeleteObject(hfont);
6434 return Ok;
6437 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6438 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6439 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6440 INT flags, GDIPCONST GpMatrix *matrix)
6442 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6443 GpStatus stat;
6444 PointF *real_positions, real_position;
6445 POINT *pti;
6446 HFONT hfont;
6447 HDC hdc;
6448 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6449 DWORD max_glyphsize=0;
6450 GLYPHMETRICS glyphmetrics;
6451 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6452 BYTE *glyph_mask;
6453 BYTE *text_mask;
6454 int text_mask_stride;
6455 BYTE *pixel_data;
6456 int pixel_data_stride;
6457 GpRect pixel_area;
6458 UINT ggo_flags = GGO_GRAY8_BITMAP;
6460 if (length <= 0)
6461 return Ok;
6463 if (!(flags & DriverStringOptionsCmapLookup))
6464 ggo_flags |= GGO_GLYPH_INDEX;
6466 if (flags & unsupported_flags)
6467 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6469 pti = GdipAlloc(sizeof(POINT) * length);
6470 if (!pti)
6471 return OutOfMemory;
6473 if (flags & DriverStringOptionsRealizedAdvance)
6475 real_position = positions[0];
6477 transform_and_round_points(graphics, pti, &real_position, 1);
6479 else
6481 real_positions = GdipAlloc(sizeof(PointF) * length);
6482 if (!real_positions)
6484 GdipFree(pti);
6485 return OutOfMemory;
6488 memcpy(real_positions, positions, sizeof(PointF) * length);
6490 transform_and_round_points(graphics, pti, real_positions, length);
6492 GdipFree(real_positions);
6495 get_font_hfont(graphics, font, format, &hfont, matrix);
6497 hdc = CreateCompatibleDC(0);
6498 SelectObject(hdc, hfont);
6500 /* Get the boundaries of the text to be drawn */
6501 for (i=0; i<length; i++)
6503 DWORD glyphsize;
6504 int left, top, right, bottom;
6506 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6507 &glyphmetrics, 0, NULL, &identity);
6509 if (glyphsize == GDI_ERROR)
6511 ERR("GetGlyphOutlineW failed\n");
6512 GdipFree(pti);
6513 DeleteDC(hdc);
6514 DeleteObject(hfont);
6515 return GenericError;
6518 if (glyphsize > max_glyphsize)
6519 max_glyphsize = glyphsize;
6521 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6522 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6523 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6524 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6526 if (left < min_x) min_x = left;
6527 if (top < min_y) min_y = top;
6528 if (right > max_x) max_x = right;
6529 if (bottom > max_y) max_y = bottom;
6531 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6533 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6534 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6538 glyph_mask = GdipAlloc(max_glyphsize);
6539 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6540 text_mask_stride = max_x - min_x;
6542 if (!(glyph_mask && text_mask))
6544 GdipFree(glyph_mask);
6545 GdipFree(text_mask);
6546 GdipFree(pti);
6547 DeleteDC(hdc);
6548 DeleteObject(hfont);
6549 return OutOfMemory;
6552 /* Generate a mask for the text */
6553 for (i=0; i<length; i++)
6555 int left, top, stride;
6557 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6558 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6560 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6561 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6562 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6564 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6566 BYTE *glyph_val = glyph_mask + y * stride;
6567 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6568 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6570 *text_val = min(64, *text_val + *glyph_val);
6571 glyph_val++;
6572 text_val++;
6577 GdipFree(pti);
6578 DeleteDC(hdc);
6579 DeleteObject(hfont);
6580 GdipFree(glyph_mask);
6582 /* get the brush data */
6583 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6584 if (!pixel_data)
6586 GdipFree(text_mask);
6587 return OutOfMemory;
6590 pixel_area.X = min_x;
6591 pixel_area.Y = min_y;
6592 pixel_area.Width = max_x - min_x;
6593 pixel_area.Height = max_y - min_y;
6594 pixel_data_stride = pixel_area.Width * 4;
6596 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6597 if (stat != Ok)
6599 GdipFree(text_mask);
6600 GdipFree(pixel_data);
6601 return stat;
6604 /* multiply the brush data by the mask */
6605 for (y=0; y<pixel_area.Height; y++)
6607 BYTE *text_val = text_mask + text_mask_stride * y;
6608 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6609 for (x=0; x<pixel_area.Width; x++)
6611 *pixel_val = (*pixel_val) * (*text_val) / 64;
6612 text_val++;
6613 pixel_val+=4;
6617 GdipFree(text_mask);
6619 /* draw the result */
6620 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6621 pixel_area.Height, pixel_data_stride);
6623 GdipFree(pixel_data);
6625 return stat;
6628 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6629 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6630 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6631 INT flags, GDIPCONST GpMatrix *matrix)
6633 GpStatus stat = NotImplemented;
6635 if (length == -1)
6636 length = strlenW(text);
6638 if (graphics->hdc && !graphics->alpha_hdc &&
6639 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6640 brush->bt == BrushTypeSolidColor &&
6641 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6642 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6643 brush, positions, flags, matrix);
6644 if (stat == NotImplemented)
6645 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6646 brush, positions, flags, matrix);
6647 return stat;
6650 /*****************************************************************************
6651 * GdipDrawDriverString [GDIPLUS.@]
6653 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6654 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6655 GDIPCONST PointF *positions, INT flags,
6656 GDIPCONST GpMatrix *matrix )
6658 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6660 if (!graphics || !text || !font || !brush || !positions)
6661 return InvalidParameter;
6663 return draw_driver_string(graphics, text, length, font, NULL,
6664 brush, positions, flags, matrix);
6667 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6668 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6670 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6671 return NotImplemented;
6674 /*****************************************************************************
6675 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6677 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6679 GpStatus stat;
6680 GpRegion* rgn;
6682 TRACE("(%p, %p)\n", graphics, res);
6684 if((stat = GdipCreateRegion(&rgn)) != Ok)
6685 return stat;
6687 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6688 goto cleanup;
6690 stat = GdipIsEmptyRegion(rgn, graphics, res);
6692 cleanup:
6693 GdipDeleteRegion(rgn);
6694 return stat;
6697 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6699 static int calls;
6701 TRACE("(%p) stub\n", graphics);
6703 if(!(calls++))
6704 FIXME("not implemented\n");
6706 return NotImplemented;