gdiplus: Add support for StringFormatFlagsNoClip.
[wine/wine-gecko.git] / dlls / gdiplus / graphics.c
blob96e25338befdc790ed762bc152f7cd402c2464a6
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 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
52 REAL radAngle, hypotenuse;
54 radAngle = deg2rad(angle);
55 hypotenuse = 50.0; /* arbitrary */
57 *x = x_0 + cos(radAngle) * hypotenuse;
58 *y = y_0 + sin(radAngle) * hypotenuse;
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
64 BYTE ret;
66 switch(type & PathPointTypePathTypeMask){
67 case PathPointTypeBezier:
68 ret = PT_BEZIERTO;
69 break;
70 case PathPointTypeLine:
71 ret = PT_LINETO;
72 break;
73 case PathPointTypeStart:
74 ret = PT_MOVETO;
75 break;
76 default:
77 ERR("Bad point type\n");
78 return 0;
81 if(type & PathPointTypeCloseSubpath)
82 ret |= PT_CLOSEFIGURE;
84 return ret;
87 static COLORREF get_gdi_brush_color(const GpBrush *brush)
89 ARGB argb;
91 switch (brush->bt)
93 case BrushTypeSolidColor:
95 const GpSolidFill *sf = (const GpSolidFill *)brush;
96 argb = sf->color;
97 break;
99 case BrushTypeHatchFill:
101 const GpHatch *hatch = (const GpHatch *)brush;
102 argb = hatch->forecol;
103 break;
105 case BrushTypeLinearGradient:
107 const GpLineGradient *line = (const GpLineGradient *)brush;
108 argb = line->startcolor;
109 break;
111 case BrushTypePathGradient:
113 const GpPathGradient *grad = (const GpPathGradient *)brush;
114 argb = grad->centercolor;
115 break;
117 default:
118 FIXME("unhandled brush type %d\n", brush->bt);
119 argb = 0;
120 break;
122 return ARGB2COLORREF(argb);
125 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
127 HBITMAP hbmp;
128 BITMAPINFOHEADER bmih;
129 DWORD *bits;
130 int x, y;
132 bmih.biSize = sizeof(bmih);
133 bmih.biWidth = 8;
134 bmih.biHeight = 8;
135 bmih.biPlanes = 1;
136 bmih.biBitCount = 32;
137 bmih.biCompression = BI_RGB;
138 bmih.biSizeImage = 0;
140 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
141 if (hbmp)
143 const char *hatch_data;
145 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
147 for (y = 0; y < 8; y++)
149 for (x = 0; x < 8; x++)
151 if (hatch_data[y] & (0x80 >> x))
152 bits[y * 8 + x] = hatch->forecol;
153 else
154 bits[y * 8 + x] = hatch->backcol;
158 else
160 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
162 for (y = 0; y < 64; y++)
163 bits[y] = hatch->forecol;
167 return hbmp;
170 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
172 switch (brush->bt)
174 case BrushTypeSolidColor:
176 const GpSolidFill *sf = (const GpSolidFill *)brush;
177 lb->lbStyle = BS_SOLID;
178 lb->lbColor = ARGB2COLORREF(sf->color);
179 lb->lbHatch = 0;
180 return Ok;
183 case BrushTypeHatchFill:
185 const GpHatch *hatch = (const GpHatch *)brush;
186 HBITMAP hbmp;
188 hbmp = create_hatch_bitmap(hatch);
189 if (!hbmp) return OutOfMemory;
191 lb->lbStyle = BS_PATTERN;
192 lb->lbColor = 0;
193 lb->lbHatch = (ULONG_PTR)hbmp;
194 return Ok;
197 default:
198 FIXME("unhandled brush type %d\n", brush->bt);
199 lb->lbStyle = BS_SOLID;
200 lb->lbColor = get_gdi_brush_color(brush);
201 lb->lbHatch = 0;
202 return Ok;
206 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
208 switch (lb->lbStyle)
210 case BS_PATTERN:
211 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
212 break;
214 return Ok;
217 static HBRUSH create_gdi_brush(const GpBrush *brush)
219 LOGBRUSH lb;
220 HBRUSH gdibrush;
222 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
224 gdibrush = CreateBrushIndirect(&lb);
225 free_gdi_logbrush(&lb);
227 return gdibrush;
230 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
232 LOGBRUSH lb;
233 HPEN gdipen;
234 REAL width;
235 INT save_state, i, numdashes;
236 GpPointF pt[2];
237 DWORD dash_array[MAX_DASHLEN];
239 save_state = SaveDC(graphics->hdc);
241 EndPath(graphics->hdc);
243 if(pen->unit == UnitPixel){
244 width = pen->width;
246 else{
247 /* Get an estimate for the amount the pen width is affected by the world
248 * transform. (This is similar to what some of the wine drivers do.) */
249 pt[0].X = 0.0;
250 pt[0].Y = 0.0;
251 pt[1].X = 1.0;
252 pt[1].Y = 1.0;
253 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
254 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
255 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
257 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
260 if(pen->dash == DashStyleCustom){
261 numdashes = min(pen->numdashes, MAX_DASHLEN);
263 TRACE("dashes are: ");
264 for(i = 0; i < numdashes; i++){
265 dash_array[i] = gdip_round(width * pen->dashes[i]);
266 TRACE("%d, ", dash_array[i]);
268 TRACE("\n and the pen style is %x\n", pen->style);
270 create_gdi_logbrush(pen->brush, &lb);
271 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
272 numdashes, dash_array);
273 free_gdi_logbrush(&lb);
275 else
277 create_gdi_logbrush(pen->brush, &lb);
278 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
279 free_gdi_logbrush(&lb);
282 SelectObject(graphics->hdc, gdipen);
284 return save_state;
287 static void restore_dc(GpGraphics *graphics, INT state)
289 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
290 RestoreDC(graphics->hdc, state);
293 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
294 GpCoordinateSpace src_space, GpMatrix **matrix);
296 /* This helper applies all the changes that the points listed in ptf need in
297 * order to be drawn on the device context. In the end, this should include at
298 * least:
299 * -scaling by page unit
300 * -applying world transformation
301 * -converting from float to int
302 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
303 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
304 * gdi to draw, and these functions would irreparably mess with line widths.
306 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
307 GpPointF *ptf, INT count)
309 REAL scale_x, scale_y;
310 GpMatrix *matrix;
311 int i;
313 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
314 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
316 /* apply page scale */
317 if(graphics->unit != UnitDisplay)
319 scale_x *= graphics->scale;
320 scale_y *= graphics->scale;
323 GdipCloneMatrix(graphics->worldtrans, &matrix);
324 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
325 GdipTransformMatrixPoints(matrix, ptf, count);
326 GdipDeleteMatrix(matrix);
328 for(i = 0; i < count; i++){
329 pti[i].x = gdip_round(ptf[i].X);
330 pti[i].y = gdip_round(ptf[i].Y);
334 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
335 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
337 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
339 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
341 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
342 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
344 else
346 BLENDFUNCTION bf;
348 bf.BlendOp = AC_SRC_OVER;
349 bf.BlendFlags = 0;
350 bf.SourceConstantAlpha = 255;
351 bf.AlphaFormat = AC_SRC_ALPHA;
353 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
354 hdc, src_x, src_y, src_width, src_height, bf);
358 /* Draw non-premultiplied ARGB data to the given graphics object */
359 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
360 const BYTE *src, INT src_width, INT src_height, INT src_stride)
362 if (graphics->image && graphics->image->type == ImageTypeBitmap)
364 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
365 INT x, y;
367 for (x=0; x<src_width; x++)
369 for (y=0; y<src_height; y++)
371 ARGB dst_color, src_color;
372 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
373 src_color = ((ARGB*)(src + src_stride * y))[x];
374 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
378 return Ok;
380 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
382 ERR("This should not be used for metafiles; fix caller\n");
383 return NotImplemented;
385 else
387 HDC hdc;
388 HBITMAP hbitmap;
389 BITMAPINFOHEADER bih;
390 BYTE *temp_bits;
392 hdc = CreateCompatibleDC(0);
394 bih.biSize = sizeof(BITMAPINFOHEADER);
395 bih.biWidth = src_width;
396 bih.biHeight = -src_height;
397 bih.biPlanes = 1;
398 bih.biBitCount = 32;
399 bih.biCompression = BI_RGB;
400 bih.biSizeImage = 0;
401 bih.biXPelsPerMeter = 0;
402 bih.biYPelsPerMeter = 0;
403 bih.biClrUsed = 0;
404 bih.biClrImportant = 0;
406 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
407 (void**)&temp_bits, NULL, 0);
409 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
410 4 * src_width, src, src_stride);
412 SelectObject(hdc, hbitmap);
413 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
414 hdc, 0, 0, src_width, src_height);
415 DeleteDC(hdc);
416 DeleteObject(hbitmap);
418 return Ok;
422 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
423 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
425 GpStatus stat=Ok;
427 if (graphics->image && graphics->image->type == ImageTypeBitmap)
429 int i, size;
430 RGNDATA *rgndata;
431 RECT *rects;
433 size = GetRegionData(hregion, 0, NULL);
435 rgndata = GdipAlloc(size);
436 if (!rgndata)
437 return OutOfMemory;
439 GetRegionData(hregion, size, rgndata);
441 rects = (RECT*)&rgndata->Buffer;
443 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
445 stat = alpha_blend_pixels(graphics, rects[i].left, rects[i].top,
446 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
447 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
448 src_stride);
451 GdipFree(rgndata);
453 return stat;
455 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
457 ERR("This should not be used for metafiles; fix caller\n");
458 return NotImplemented;
460 else
462 int save;
464 save = SaveDC(graphics->hdc);
466 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
468 stat = alpha_blend_pixels(graphics, dst_x, dst_y, src, src_width,
469 src_height, src_stride);
471 RestoreDC(graphics->hdc, save);
473 return stat;
477 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
479 ARGB result=0;
480 ARGB i;
481 INT a1, a2, a3;
483 a1 = (start >> 24) & 0xff;
484 a2 = (end >> 24) & 0xff;
486 a3 = (int)(a1*(1.0f - position)+a2*(position));
488 result |= a3 << 24;
490 for (i=0xff; i<=0xff0000; i = i << 8)
491 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
492 return result;
495 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
497 REAL blendfac;
499 /* clamp to between 0.0 and 1.0, using the wrap mode */
500 if (brush->wrap == WrapModeTile)
502 position = fmodf(position, 1.0f);
503 if (position < 0.0f) position += 1.0f;
505 else /* WrapModeFlip* */
507 position = fmodf(position, 2.0f);
508 if (position < 0.0f) position += 2.0f;
509 if (position > 1.0f) position = 2.0f - position;
512 if (brush->blendcount == 1)
513 blendfac = position;
514 else
516 int i=1;
517 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
518 REAL range;
520 /* locate the blend positions surrounding this position */
521 while (position > brush->blendpos[i])
522 i++;
524 /* interpolate between the blend positions */
525 left_blendpos = brush->blendpos[i-1];
526 left_blendfac = brush->blendfac[i-1];
527 right_blendpos = brush->blendpos[i];
528 right_blendfac = brush->blendfac[i];
529 range = right_blendpos - left_blendpos;
530 blendfac = (left_blendfac * (right_blendpos - position) +
531 right_blendfac * (position - left_blendpos)) / range;
534 if (brush->pblendcount == 0)
535 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
536 else
538 int i=1;
539 ARGB left_blendcolor, right_blendcolor;
540 REAL left_blendpos, right_blendpos;
542 /* locate the blend colors surrounding this position */
543 while (blendfac > brush->pblendpos[i])
544 i++;
546 /* interpolate between the blend colors */
547 left_blendpos = brush->pblendpos[i-1];
548 left_blendcolor = brush->pblendcolor[i-1];
549 right_blendpos = brush->pblendpos[i];
550 right_blendcolor = brush->pblendcolor[i];
551 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
552 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
556 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
558 REAL val[5], res[4];
559 int i, j;
560 unsigned char a, r, g, b;
562 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
563 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
564 val[2] = (color & 0xff) / 255.0; /* blue */
565 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
566 val[4] = 1.0; /* translation */
568 for (i=0; i<4; i++)
570 res[i] = 0.0;
572 for (j=0; j<5; j++)
573 res[i] += matrix->m[j][i] * val[j];
576 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
577 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
578 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
579 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
581 return (a << 24) | (r << 16) | (g << 8) | b;
584 static int color_is_gray(ARGB color)
586 unsigned char r, g, b;
588 r = (color >> 16) & 0xff;
589 g = (color >> 8) & 0xff;
590 b = color & 0xff;
592 return (r == g) && (g == b);
595 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
596 UINT width, UINT height, INT stride, ColorAdjustType type)
598 UINT x, y, i;
600 if (attributes->colorkeys[type].enabled ||
601 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
603 const struct color_key *key;
604 BYTE min_blue, min_green, min_red;
605 BYTE max_blue, max_green, max_red;
607 if (attributes->colorkeys[type].enabled)
608 key = &attributes->colorkeys[type];
609 else
610 key = &attributes->colorkeys[ColorAdjustTypeDefault];
612 min_blue = key->low&0xff;
613 min_green = (key->low>>8)&0xff;
614 min_red = (key->low>>16)&0xff;
616 max_blue = key->high&0xff;
617 max_green = (key->high>>8)&0xff;
618 max_red = (key->high>>16)&0xff;
620 for (x=0; x<width; x++)
621 for (y=0; y<height; y++)
623 ARGB *src_color;
624 BYTE blue, green, red;
625 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
626 blue = *src_color&0xff;
627 green = (*src_color>>8)&0xff;
628 red = (*src_color>>16)&0xff;
629 if (blue >= min_blue && green >= min_green && red >= min_red &&
630 blue <= max_blue && green <= max_green && red <= max_red)
631 *src_color = 0x00000000;
635 if (attributes->colorremaptables[type].enabled ||
636 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
638 const struct color_remap_table *table;
640 if (attributes->colorremaptables[type].enabled)
641 table = &attributes->colorremaptables[type];
642 else
643 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
645 for (x=0; x<width; x++)
646 for (y=0; y<height; y++)
648 ARGB *src_color;
649 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
650 for (i=0; i<table->mapsize; i++)
652 if (*src_color == table->colormap[i].oldColor.Argb)
654 *src_color = table->colormap[i].newColor.Argb;
655 break;
661 if (attributes->colormatrices[type].enabled ||
662 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
664 const struct color_matrix *colormatrices;
666 if (attributes->colormatrices[type].enabled)
667 colormatrices = &attributes->colormatrices[type];
668 else
669 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
671 for (x=0; x<width; x++)
672 for (y=0; y<height; y++)
674 ARGB *src_color;
675 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
677 if (colormatrices->flags == ColorMatrixFlagsDefault ||
678 !color_is_gray(*src_color))
680 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
682 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
684 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
689 if (attributes->gamma_enabled[type] ||
690 attributes->gamma_enabled[ColorAdjustTypeDefault])
692 REAL gamma;
694 if (attributes->gamma_enabled[type])
695 gamma = attributes->gamma[type];
696 else
697 gamma = attributes->gamma[ColorAdjustTypeDefault];
699 for (x=0; x<width; x++)
700 for (y=0; y<height; y++)
702 ARGB *src_color;
703 BYTE blue, green, red;
704 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
706 blue = *src_color&0xff;
707 green = (*src_color>>8)&0xff;
708 red = (*src_color>>16)&0xff;
710 /* FIXME: We should probably use a table for this. */
711 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
712 green = floorf(powf(green / 255.0, gamma) * 255.0);
713 red = floorf(powf(red / 255.0, gamma) * 255.0);
715 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
720 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
721 * bitmap that contains all the pixels we may need to draw it. */
722 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
723 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
724 GpRect *rect)
726 INT left, top, right, bottom;
728 switch (interpolation)
730 case InterpolationModeHighQualityBilinear:
731 case InterpolationModeHighQualityBicubic:
732 /* FIXME: Include a greater range for the prefilter? */
733 case InterpolationModeBicubic:
734 case InterpolationModeBilinear:
735 left = (INT)(floorf(srcx));
736 top = (INT)(floorf(srcy));
737 right = (INT)(ceilf(srcx+srcwidth));
738 bottom = (INT)(ceilf(srcy+srcheight));
739 break;
740 case InterpolationModeNearestNeighbor:
741 default:
742 left = gdip_round(srcx);
743 top = gdip_round(srcy);
744 right = gdip_round(srcx+srcwidth);
745 bottom = gdip_round(srcy+srcheight);
746 break;
749 if (wrap == WrapModeClamp)
751 if (left < 0)
752 left = 0;
753 if (top < 0)
754 top = 0;
755 if (right >= bitmap->width)
756 right = bitmap->width-1;
757 if (bottom >= bitmap->height)
758 bottom = bitmap->height-1;
760 else
762 /* In some cases we can make the rectangle smaller here, but the logic
763 * is hard to get right, and tiling suggests we're likely to use the
764 * entire source image. */
765 if (left < 0 || right >= bitmap->width)
767 left = 0;
768 right = bitmap->width-1;
771 if (top < 0 || bottom >= bitmap->height)
773 top = 0;
774 bottom = bitmap->height-1;
778 rect->X = left;
779 rect->Y = top;
780 rect->Width = right - left + 1;
781 rect->Height = bottom - top + 1;
784 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
785 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
787 if (attributes->wrap == WrapModeClamp)
789 if (x < 0 || y < 0 || x >= width || y >= height)
790 return attributes->outside_color;
792 else
794 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
795 if (x < 0)
796 x = width*2 + x % (width * 2);
797 if (y < 0)
798 y = height*2 + y % (height * 2);
800 if ((attributes->wrap & 1) == 1)
802 /* Flip X */
803 if ((x / width) % 2 == 0)
804 x = x % width;
805 else
806 x = width - 1 - x % width;
808 else
809 x = x % width;
811 if ((attributes->wrap & 2) == 2)
813 /* Flip Y */
814 if ((y / height) % 2 == 0)
815 y = y % height;
816 else
817 y = height - 1 - y % height;
819 else
820 y = y % height;
823 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
825 ERR("out of range pixel requested\n");
826 return 0xffcd0084;
829 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
832 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
833 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
834 InterpolationMode interpolation)
836 static int fixme;
838 switch (interpolation)
840 default:
841 if (!fixme++)
842 FIXME("Unimplemented interpolation %i\n", interpolation);
843 /* fall-through */
844 case InterpolationModeBilinear:
846 REAL leftxf, topyf;
847 INT leftx, rightx, topy, bottomy;
848 ARGB topleft, topright, bottomleft, bottomright;
849 ARGB top, bottom;
850 float x_offset;
852 leftxf = floorf(point->X);
853 leftx = (INT)leftxf;
854 rightx = (INT)ceilf(point->X);
855 topyf = floorf(point->Y);
856 topy = (INT)topyf;
857 bottomy = (INT)ceilf(point->Y);
859 if (leftx == rightx && topy == bottomy)
860 return sample_bitmap_pixel(src_rect, bits, width, height,
861 leftx, topy, attributes);
863 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
864 leftx, topy, attributes);
865 topright = sample_bitmap_pixel(src_rect, bits, width, height,
866 rightx, topy, attributes);
867 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
868 leftx, bottomy, attributes);
869 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
870 rightx, bottomy, attributes);
872 x_offset = point->X - leftxf;
873 top = blend_colors(topleft, topright, x_offset);
874 bottom = blend_colors(bottomleft, bottomright, x_offset);
876 return blend_colors(top, bottom, point->Y - topyf);
878 case InterpolationModeNearestNeighbor:
879 return sample_bitmap_pixel(src_rect, bits, width, height,
880 gdip_round(point->X), gdip_round(point->Y), attributes);
884 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
886 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
889 static INT brush_can_fill_path(GpBrush *brush)
891 switch (brush->bt)
893 case BrushTypeSolidColor:
894 return 1;
895 case BrushTypeHatchFill:
897 GpHatch *hatch = (GpHatch*)brush;
898 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
899 ((hatch->backcol & 0xff000000) == 0xff000000);
901 case BrushTypeLinearGradient:
902 case BrushTypeTextureFill:
903 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
904 default:
905 return 0;
909 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
911 switch (brush->bt)
913 case BrushTypeSolidColor:
915 GpSolidFill *fill = (GpSolidFill*)brush;
916 HBITMAP bmp = ARGB2BMP(fill->color);
918 if (bmp)
920 RECT rc;
921 /* partially transparent fill */
923 SelectClipPath(graphics->hdc, RGN_AND);
924 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
926 HDC hdc = CreateCompatibleDC(NULL);
928 if (!hdc) break;
930 SelectObject(hdc, bmp);
931 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
932 hdc, 0, 0, 1, 1);
933 DeleteDC(hdc);
936 DeleteObject(bmp);
937 break;
939 /* else fall through */
941 default:
943 HBRUSH gdibrush, old_brush;
945 gdibrush = create_gdi_brush(brush);
946 if (!gdibrush) return;
948 old_brush = SelectObject(graphics->hdc, gdibrush);
949 FillPath(graphics->hdc);
950 SelectObject(graphics->hdc, old_brush);
951 DeleteObject(gdibrush);
952 break;
957 static INT brush_can_fill_pixels(GpBrush *brush)
959 switch (brush->bt)
961 case BrushTypeSolidColor:
962 case BrushTypeHatchFill:
963 case BrushTypeLinearGradient:
964 case BrushTypeTextureFill:
965 case BrushTypePathGradient:
966 return 1;
967 default:
968 return 0;
972 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
973 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
975 switch (brush->bt)
977 case BrushTypeSolidColor:
979 int x, y;
980 GpSolidFill *fill = (GpSolidFill*)brush;
981 for (x=0; x<fill_area->Width; x++)
982 for (y=0; y<fill_area->Height; y++)
983 argb_pixels[x + y*cdwStride] = fill->color;
984 return Ok;
986 case BrushTypeHatchFill:
988 int x, y;
989 GpHatch *fill = (GpHatch*)brush;
990 const char *hatch_data;
992 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
993 return NotImplemented;
995 for (x=0; x<fill_area->Width; x++)
996 for (y=0; y<fill_area->Height; y++)
998 int hx, hy;
1000 /* FIXME: Account for the rendering origin */
1001 hx = (x + fill_area->X) % 8;
1002 hy = (y + fill_area->Y) % 8;
1004 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1005 argb_pixels[x + y*cdwStride] = fill->forecol;
1006 else
1007 argb_pixels[x + y*cdwStride] = fill->backcol;
1010 return Ok;
1012 case BrushTypeLinearGradient:
1014 GpLineGradient *fill = (GpLineGradient*)brush;
1015 GpPointF draw_points[3], line_points[3];
1016 GpStatus stat;
1017 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1018 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1019 int x, y;
1021 draw_points[0].X = fill_area->X;
1022 draw_points[0].Y = fill_area->Y;
1023 draw_points[1].X = fill_area->X+1;
1024 draw_points[1].Y = fill_area->Y;
1025 draw_points[2].X = fill_area->X;
1026 draw_points[2].Y = fill_area->Y+1;
1028 /* Transform the points to a co-ordinate space where X is the point's
1029 * position in the gradient, 0.0 being the start point and 1.0 the
1030 * end point. */
1031 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1032 CoordinateSpaceDevice, draw_points, 3);
1034 if (stat == Ok)
1036 line_points[0] = fill->startpoint;
1037 line_points[1] = fill->endpoint;
1038 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1039 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1041 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1044 if (stat == Ok)
1046 stat = GdipInvertMatrix(world_to_gradient);
1048 if (stat == Ok)
1049 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1051 GdipDeleteMatrix(world_to_gradient);
1054 if (stat == Ok)
1056 REAL x_delta = draw_points[1].X - draw_points[0].X;
1057 REAL y_delta = draw_points[2].X - draw_points[0].X;
1059 for (y=0; y<fill_area->Height; y++)
1061 for (x=0; x<fill_area->Width; x++)
1063 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1065 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1070 return stat;
1072 case BrushTypeTextureFill:
1074 GpTexture *fill = (GpTexture*)brush;
1075 GpPointF draw_points[3];
1076 GpStatus stat;
1077 GpMatrix *world_to_texture;
1078 int x, y;
1079 GpBitmap *bitmap;
1080 int src_stride;
1081 GpRect src_area;
1083 if (fill->image->type != ImageTypeBitmap)
1085 FIXME("metafile texture brushes not implemented\n");
1086 return NotImplemented;
1089 bitmap = (GpBitmap*)fill->image;
1090 src_stride = sizeof(ARGB) * bitmap->width;
1092 src_area.X = src_area.Y = 0;
1093 src_area.Width = bitmap->width;
1094 src_area.Height = bitmap->height;
1096 draw_points[0].X = fill_area->X;
1097 draw_points[0].Y = fill_area->Y;
1098 draw_points[1].X = fill_area->X+1;
1099 draw_points[1].Y = fill_area->Y;
1100 draw_points[2].X = fill_area->X;
1101 draw_points[2].Y = fill_area->Y+1;
1103 /* Transform the points to the co-ordinate space of the bitmap. */
1104 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1105 CoordinateSpaceDevice, draw_points, 3);
1107 if (stat == Ok)
1109 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
1112 if (stat == Ok)
1114 stat = GdipInvertMatrix(world_to_texture);
1116 if (stat == Ok)
1117 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
1119 GdipDeleteMatrix(world_to_texture);
1122 if (stat == Ok && !fill->bitmap_bits)
1124 BitmapData lockeddata;
1126 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1127 if (!fill->bitmap_bits)
1128 stat = OutOfMemory;
1130 if (stat == Ok)
1132 lockeddata.Width = bitmap->width;
1133 lockeddata.Height = bitmap->height;
1134 lockeddata.Stride = src_stride;
1135 lockeddata.PixelFormat = PixelFormat32bppARGB;
1136 lockeddata.Scan0 = fill->bitmap_bits;
1138 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1139 PixelFormat32bppARGB, &lockeddata);
1142 if (stat == Ok)
1143 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1145 if (stat == Ok)
1146 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1147 bitmap->width, bitmap->height,
1148 src_stride, ColorAdjustTypeBitmap);
1150 if (stat != Ok)
1152 GdipFree(fill->bitmap_bits);
1153 fill->bitmap_bits = NULL;
1157 if (stat == Ok)
1159 REAL x_dx = draw_points[1].X - draw_points[0].X;
1160 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1161 REAL y_dx = draw_points[2].X - draw_points[0].X;
1162 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1164 for (y=0; y<fill_area->Height; y++)
1166 for (x=0; x<fill_area->Width; x++)
1168 GpPointF point;
1169 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1170 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1172 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1173 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1174 &point, fill->imageattributes, graphics->interpolation);
1179 return stat;
1181 case BrushTypePathGradient:
1183 GpPathGradient *fill = (GpPathGradient*)brush;
1184 GpPath *flat_path;
1185 GpMatrix *world_to_device;
1186 GpStatus stat;
1187 int i, figure_start=0;
1188 GpPointF start_point, end_point, center_point;
1189 BYTE type;
1190 REAL min_yf, max_yf, line1_xf, line2_xf;
1191 INT min_y, max_y, min_x, max_x;
1192 INT x, y;
1193 ARGB outer_color;
1194 static int transform_fixme_once;
1196 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1198 static int once;
1199 if (!once++)
1200 FIXME("path gradient focus not implemented\n");
1203 if (fill->gamma)
1205 static int once;
1206 if (!once++)
1207 FIXME("path gradient gamma correction not implemented\n");
1210 if (fill->blendcount)
1212 static int once;
1213 if (!once++)
1214 FIXME("path gradient blend not implemented\n");
1217 if (fill->pblendcount)
1219 static int once;
1220 if (!once++)
1221 FIXME("path gradient preset blend not implemented\n");
1224 if (!transform_fixme_once)
1226 BOOL is_identity=TRUE;
1227 GdipIsMatrixIdentity(fill->transform, &is_identity);
1228 if (!is_identity)
1230 FIXME("path gradient transform not implemented\n");
1231 transform_fixme_once = 1;
1235 stat = GdipClonePath(fill->path, &flat_path);
1237 if (stat != Ok)
1238 return stat;
1240 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1241 CoordinateSpaceWorld, &world_to_device);
1242 if (stat == Ok)
1244 stat = GdipTransformPath(flat_path, world_to_device);
1246 if (stat == Ok)
1248 center_point = fill->center;
1249 stat = GdipTransformMatrixPoints(world_to_device, &center_point, 1);
1252 if (stat == Ok)
1253 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1255 GdipDeleteMatrix(world_to_device);
1258 if (stat != Ok)
1260 GdipDeletePath(flat_path);
1261 return stat;
1264 for (i=0; i<flat_path->pathdata.Count; i++)
1266 int start_center_line=0, end_center_line=0;
1267 int seen_start=0, seen_end=0, seen_center=0;
1268 REAL center_distance;
1269 ARGB start_color, end_color;
1270 REAL dy, dx;
1272 type = flat_path->pathdata.Types[i];
1274 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1275 figure_start = i;
1277 start_point = flat_path->pathdata.Points[i];
1279 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1281 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1283 end_point = flat_path->pathdata.Points[figure_start];
1284 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1286 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1288 end_point = flat_path->pathdata.Points[i+1];
1289 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1291 else
1292 continue;
1294 outer_color = start_color;
1296 min_yf = center_point.Y;
1297 if (min_yf > start_point.Y) min_yf = start_point.Y;
1298 if (min_yf > end_point.Y) min_yf = end_point.Y;
1300 if (min_yf < fill_area->Y)
1301 min_y = fill_area->Y;
1302 else
1303 min_y = (INT)ceil(min_yf);
1305 max_yf = center_point.Y;
1306 if (max_yf < start_point.Y) max_yf = start_point.Y;
1307 if (max_yf < end_point.Y) max_yf = end_point.Y;
1309 if (max_yf > fill_area->Y + fill_area->Height)
1310 max_y = fill_area->Y + fill_area->Height;
1311 else
1312 max_y = (INT)ceil(max_yf);
1314 dy = end_point.Y - start_point.Y;
1315 dx = end_point.X - start_point.X;
1317 /* This is proportional to the distance from start-end line to center point. */
1318 center_distance = dy * (start_point.X - center_point.X) +
1319 dx * (center_point.Y - start_point.Y);
1321 for (y=min_y; y<max_y; y++)
1323 REAL yf = (REAL)y;
1325 if (!seen_start && yf >= start_point.Y)
1327 seen_start = 1;
1328 start_center_line ^= 1;
1330 if (!seen_end && yf >= end_point.Y)
1332 seen_end = 1;
1333 end_center_line ^= 1;
1335 if (!seen_center && yf >= center_point.Y)
1337 seen_center = 1;
1338 start_center_line ^= 1;
1339 end_center_line ^= 1;
1342 if (start_center_line)
1343 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1344 else
1345 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1347 if (end_center_line)
1348 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1349 else
1350 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1352 if (line1_xf < line2_xf)
1354 min_x = (INT)ceil(line1_xf);
1355 max_x = (INT)ceil(line2_xf);
1357 else
1359 min_x = (INT)ceil(line2_xf);
1360 max_x = (INT)ceil(line1_xf);
1363 if (min_x < fill_area->X)
1364 min_x = fill_area->X;
1365 if (max_x > fill_area->X + fill_area->Width)
1366 max_x = fill_area->X + fill_area->Width;
1368 for (x=min_x; x<max_x; x++)
1370 REAL xf = (REAL)x;
1371 REAL distance;
1373 if (start_color != end_color)
1375 REAL blend_amount, pdy, pdx;
1376 pdy = yf - center_point.Y;
1377 pdx = xf - center_point.X;
1378 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1379 outer_color = blend_colors(start_color, end_color, blend_amount);
1382 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1383 (end_point.X - start_point.X) * (yf - start_point.Y);
1385 distance = distance / center_distance;
1387 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1388 blend_colors(outer_color, fill->centercolor, distance);
1393 GdipDeletePath(flat_path);
1394 return stat;
1396 default:
1397 return NotImplemented;
1401 /* GdipDrawPie/GdipFillPie helper function */
1402 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1403 REAL height, REAL startAngle, REAL sweepAngle)
1405 GpPointF ptf[4];
1406 POINT pti[4];
1408 ptf[0].X = x;
1409 ptf[0].Y = y;
1410 ptf[1].X = x + width;
1411 ptf[1].Y = y + height;
1413 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1414 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1416 transform_and_round_points(graphics, pti, ptf, 4);
1418 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1419 pti[2].y, pti[3].x, pti[3].y);
1422 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1423 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1424 * should not be called on an hdc that has a path you care about. */
1425 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1426 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1428 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1429 GpMatrix *matrix = NULL;
1430 HBRUSH brush = NULL;
1431 HPEN pen = NULL;
1432 PointF ptf[4], *custptf = NULL;
1433 POINT pt[4], *custpt = NULL;
1434 BYTE *tp = NULL;
1435 REAL theta, dsmall, dbig, dx, dy = 0.0;
1436 INT i, count;
1437 LOGBRUSH lb;
1438 BOOL customstroke;
1440 if((x1 == x2) && (y1 == y2))
1441 return;
1443 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1445 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1446 if(!customstroke){
1447 brush = CreateSolidBrush(color);
1448 lb.lbStyle = BS_SOLID;
1449 lb.lbColor = color;
1450 lb.lbHatch = 0;
1451 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1452 PS_JOIN_MITER, 1, &lb, 0,
1453 NULL);
1454 oldbrush = SelectObject(graphics->hdc, brush);
1455 oldpen = SelectObject(graphics->hdc, pen);
1458 switch(cap){
1459 case LineCapFlat:
1460 break;
1461 case LineCapSquare:
1462 case LineCapSquareAnchor:
1463 case LineCapDiamondAnchor:
1464 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1465 if(cap == LineCapDiamondAnchor){
1466 dsmall = cos(theta + M_PI_2) * size;
1467 dbig = sin(theta + M_PI_2) * size;
1469 else{
1470 dsmall = cos(theta + M_PI_4) * size;
1471 dbig = sin(theta + M_PI_4) * size;
1474 ptf[0].X = x2 - dsmall;
1475 ptf[1].X = x2 + dbig;
1477 ptf[0].Y = y2 - dbig;
1478 ptf[3].Y = y2 + dsmall;
1480 ptf[1].Y = y2 - dsmall;
1481 ptf[2].Y = y2 + dbig;
1483 ptf[3].X = x2 - dbig;
1484 ptf[2].X = x2 + dsmall;
1486 transform_and_round_points(graphics, pt, ptf, 4);
1487 Polygon(graphics->hdc, pt, 4);
1489 break;
1490 case LineCapArrowAnchor:
1491 size = size * 4.0 / sqrt(3.0);
1493 dx = cos(M_PI / 6.0 + theta) * size;
1494 dy = sin(M_PI / 6.0 + theta) * size;
1496 ptf[0].X = x2 - dx;
1497 ptf[0].Y = y2 - dy;
1499 dx = cos(- M_PI / 6.0 + theta) * size;
1500 dy = sin(- M_PI / 6.0 + theta) * size;
1502 ptf[1].X = x2 - dx;
1503 ptf[1].Y = y2 - dy;
1505 ptf[2].X = x2;
1506 ptf[2].Y = y2;
1508 transform_and_round_points(graphics, pt, ptf, 3);
1509 Polygon(graphics->hdc, pt, 3);
1511 break;
1512 case LineCapRoundAnchor:
1513 dx = dy = ANCHOR_WIDTH * size / 2.0;
1515 ptf[0].X = x2 - dx;
1516 ptf[0].Y = y2 - dy;
1517 ptf[1].X = x2 + dx;
1518 ptf[1].Y = y2 + dy;
1520 transform_and_round_points(graphics, pt, ptf, 2);
1521 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1523 break;
1524 case LineCapTriangle:
1525 size = size / 2.0;
1526 dx = cos(M_PI_2 + theta) * size;
1527 dy = sin(M_PI_2 + theta) * size;
1529 ptf[0].X = x2 - dx;
1530 ptf[0].Y = y2 - dy;
1531 ptf[1].X = x2 + dx;
1532 ptf[1].Y = y2 + dy;
1534 dx = cos(theta) * size;
1535 dy = sin(theta) * size;
1537 ptf[2].X = x2 + dx;
1538 ptf[2].Y = y2 + dy;
1540 transform_and_round_points(graphics, pt, ptf, 3);
1541 Polygon(graphics->hdc, pt, 3);
1543 break;
1544 case LineCapRound:
1545 dx = dy = size / 2.0;
1547 ptf[0].X = x2 - dx;
1548 ptf[0].Y = y2 - dy;
1549 ptf[1].X = x2 + dx;
1550 ptf[1].Y = y2 + dy;
1552 dx = -cos(M_PI_2 + theta) * size;
1553 dy = -sin(M_PI_2 + theta) * size;
1555 ptf[2].X = x2 - dx;
1556 ptf[2].Y = y2 - dy;
1557 ptf[3].X = x2 + dx;
1558 ptf[3].Y = y2 + dy;
1560 transform_and_round_points(graphics, pt, ptf, 4);
1561 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1562 pt[2].y, pt[3].x, pt[3].y);
1564 break;
1565 case LineCapCustom:
1566 if(!custom)
1567 break;
1569 count = custom->pathdata.Count;
1570 custptf = GdipAlloc(count * sizeof(PointF));
1571 custpt = GdipAlloc(count * sizeof(POINT));
1572 tp = GdipAlloc(count);
1574 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1575 goto custend;
1577 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1579 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1580 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1581 MatrixOrderAppend);
1582 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1583 GdipTransformMatrixPoints(matrix, custptf, count);
1585 transform_and_round_points(graphics, custpt, custptf, count);
1587 for(i = 0; i < count; i++)
1588 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1590 if(custom->fill){
1591 BeginPath(graphics->hdc);
1592 PolyDraw(graphics->hdc, custpt, tp, count);
1593 EndPath(graphics->hdc);
1594 StrokeAndFillPath(graphics->hdc);
1596 else
1597 PolyDraw(graphics->hdc, custpt, tp, count);
1599 custend:
1600 GdipFree(custptf);
1601 GdipFree(custpt);
1602 GdipFree(tp);
1603 GdipDeleteMatrix(matrix);
1604 break;
1605 default:
1606 break;
1609 if(!customstroke){
1610 SelectObject(graphics->hdc, oldbrush);
1611 SelectObject(graphics->hdc, oldpen);
1612 DeleteObject(brush);
1613 DeleteObject(pen);
1617 /* Shortens the line by the given percent by changing x2, y2.
1618 * If percent is > 1.0 then the line will change direction.
1619 * If percent is negative it can lengthen the line. */
1620 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1622 REAL dist, theta, dx, dy;
1624 if((y1 == *y2) && (x1 == *x2))
1625 return;
1627 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1628 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1629 dx = cos(theta) * dist;
1630 dy = sin(theta) * dist;
1632 *x2 = *x2 + dx;
1633 *y2 = *y2 + dy;
1636 /* Shortens the line by the given amount by changing x2, y2.
1637 * If the amount is greater than the distance, the line will become length 0.
1638 * If the amount is negative, it can lengthen the line. */
1639 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1641 REAL dx, dy, percent;
1643 dx = *x2 - x1;
1644 dy = *y2 - y1;
1645 if(dx == 0 && dy == 0)
1646 return;
1648 percent = amt / sqrt(dx * dx + dy * dy);
1649 if(percent >= 1.0){
1650 *x2 = x1;
1651 *y2 = y1;
1652 return;
1655 shorten_line_percent(x1, y1, x2, y2, percent);
1658 /* Draws lines between the given points, and if caps is true then draws an endcap
1659 * at the end of the last line. */
1660 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1661 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1663 POINT *pti = NULL;
1664 GpPointF *ptcopy = NULL;
1665 GpStatus status = GenericError;
1667 if(!count)
1668 return Ok;
1670 pti = GdipAlloc(count * sizeof(POINT));
1671 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1673 if(!pti || !ptcopy){
1674 status = OutOfMemory;
1675 goto end;
1678 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1680 if(caps){
1681 if(pen->endcap == LineCapArrowAnchor)
1682 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1683 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1684 else if((pen->endcap == LineCapCustom) && pen->customend)
1685 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1686 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1687 pen->customend->inset * pen->width);
1689 if(pen->startcap == LineCapArrowAnchor)
1690 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1691 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1692 else if((pen->startcap == LineCapCustom) && pen->customstart)
1693 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1694 &ptcopy[0].X, &ptcopy[0].Y,
1695 pen->customstart->inset * pen->width);
1697 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1698 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1699 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1700 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1703 transform_and_round_points(graphics, pti, ptcopy, count);
1705 if(Polyline(graphics->hdc, pti, count))
1706 status = Ok;
1708 end:
1709 GdipFree(pti);
1710 GdipFree(ptcopy);
1712 return status;
1715 /* Conducts a linear search to find the bezier points that will back off
1716 * the endpoint of the curve by a distance of amt. Linear search works
1717 * better than binary in this case because there are multiple solutions,
1718 * and binary searches often find a bad one. I don't think this is what
1719 * Windows does but short of rendering the bezier without GDI's help it's
1720 * the best we can do. If rev then work from the start of the passed points
1721 * instead of the end. */
1722 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1724 GpPointF origpt[4];
1725 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1726 INT i, first = 0, second = 1, third = 2, fourth = 3;
1728 if(rev){
1729 first = 3;
1730 second = 2;
1731 third = 1;
1732 fourth = 0;
1735 origx = pt[fourth].X;
1736 origy = pt[fourth].Y;
1737 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1739 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1740 /* reset bezier points to original values */
1741 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1742 /* Perform magic on bezier points. Order is important here.*/
1743 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1744 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1745 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1746 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1747 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1748 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1750 dx = pt[fourth].X - origx;
1751 dy = pt[fourth].Y - origy;
1753 diff = sqrt(dx * dx + dy * dy);
1754 percent += 0.0005 * amt;
1758 /* Draws bezier curves between given points, and if caps is true then draws an
1759 * endcap at the end of the last line. */
1760 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1761 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1763 POINT *pti;
1764 GpPointF *ptcopy;
1765 GpStatus status = GenericError;
1767 if(!count)
1768 return Ok;
1770 pti = GdipAlloc(count * sizeof(POINT));
1771 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1773 if(!pti || !ptcopy){
1774 status = OutOfMemory;
1775 goto end;
1778 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1780 if(caps){
1781 if(pen->endcap == LineCapArrowAnchor)
1782 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1783 else if((pen->endcap == LineCapCustom) && pen->customend)
1784 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1785 FALSE);
1787 if(pen->startcap == LineCapArrowAnchor)
1788 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1789 else if((pen->startcap == LineCapCustom) && pen->customstart)
1790 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1792 /* the direction of the line cap is parallel to the direction at the
1793 * end of the bezier (which, if it has been shortened, is not the same
1794 * as the direction from pt[count-2] to pt[count-1]) */
1795 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1796 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1797 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1798 pt[count - 1].X, pt[count - 1].Y);
1800 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1801 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1802 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1805 transform_and_round_points(graphics, pti, ptcopy, count);
1807 PolyBezier(graphics->hdc, pti, count);
1809 status = Ok;
1811 end:
1812 GdipFree(pti);
1813 GdipFree(ptcopy);
1815 return status;
1818 /* Draws a combination of bezier curves and lines between points. */
1819 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1820 GDIPCONST BYTE * types, INT count, BOOL caps)
1822 POINT *pti = GdipAlloc(count * sizeof(POINT));
1823 BYTE *tp = GdipAlloc(count);
1824 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1825 INT i, j;
1826 GpStatus status = GenericError;
1828 if(!count){
1829 status = Ok;
1830 goto end;
1832 if(!pti || !tp || !ptcopy){
1833 status = OutOfMemory;
1834 goto end;
1837 for(i = 1; i < count; i++){
1838 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1839 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1840 || !(types[i + 1] & PathPointTypeBezier)){
1841 ERR("Bad bezier points\n");
1842 goto end;
1844 i += 2;
1848 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1850 /* If we are drawing caps, go through the points and adjust them accordingly,
1851 * and draw the caps. */
1852 if(caps){
1853 switch(types[count - 1] & PathPointTypePathTypeMask){
1854 case PathPointTypeBezier:
1855 if(pen->endcap == LineCapArrowAnchor)
1856 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1857 else if((pen->endcap == LineCapCustom) && pen->customend)
1858 shorten_bezier_amt(&ptcopy[count - 4],
1859 pen->width * pen->customend->inset, FALSE);
1861 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1862 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1863 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1864 pt[count - 1].X, pt[count - 1].Y);
1866 break;
1867 case PathPointTypeLine:
1868 if(pen->endcap == LineCapArrowAnchor)
1869 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1870 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1871 pen->width);
1872 else if((pen->endcap == LineCapCustom) && pen->customend)
1873 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1874 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1875 pen->customend->inset * pen->width);
1877 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1878 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1879 pt[count - 1].Y);
1881 break;
1882 default:
1883 ERR("Bad path last point\n");
1884 goto end;
1887 /* Find start of points */
1888 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1889 == PathPointTypeStart); j++);
1891 switch(types[j] & PathPointTypePathTypeMask){
1892 case PathPointTypeBezier:
1893 if(pen->startcap == LineCapArrowAnchor)
1894 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1895 else if((pen->startcap == LineCapCustom) && pen->customstart)
1896 shorten_bezier_amt(&ptcopy[j - 1],
1897 pen->width * pen->customstart->inset, TRUE);
1899 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1900 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1901 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1902 pt[j - 1].X, pt[j - 1].Y);
1904 break;
1905 case PathPointTypeLine:
1906 if(pen->startcap == LineCapArrowAnchor)
1907 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1908 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1909 pen->width);
1910 else if((pen->startcap == LineCapCustom) && pen->customstart)
1911 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1912 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1913 pen->customstart->inset * pen->width);
1915 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1916 pt[j].X, pt[j].Y, pt[j - 1].X,
1917 pt[j - 1].Y);
1919 break;
1920 default:
1921 ERR("Bad path points\n");
1922 goto end;
1926 transform_and_round_points(graphics, pti, ptcopy, count);
1928 for(i = 0; i < count; i++){
1929 tp[i] = convert_path_point_type(types[i]);
1932 PolyDraw(graphics->hdc, pti, tp, count);
1934 status = Ok;
1936 end:
1937 GdipFree(pti);
1938 GdipFree(ptcopy);
1939 GdipFree(tp);
1941 return status;
1944 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1946 GpStatus result;
1948 BeginPath(graphics->hdc);
1949 result = draw_poly(graphics, NULL, path->pathdata.Points,
1950 path->pathdata.Types, path->pathdata.Count, FALSE);
1951 EndPath(graphics->hdc);
1952 return result;
1955 typedef struct _GraphicsContainerItem {
1956 struct list entry;
1957 GraphicsContainer contid;
1959 SmoothingMode smoothing;
1960 CompositingQuality compqual;
1961 InterpolationMode interpolation;
1962 CompositingMode compmode;
1963 TextRenderingHint texthint;
1964 REAL scale;
1965 GpUnit unit;
1966 PixelOffsetMode pixeloffset;
1967 UINT textcontrast;
1968 GpMatrix* worldtrans;
1969 GpRegion* clip;
1970 INT origin_x, origin_y;
1971 } GraphicsContainerItem;
1973 static GpStatus init_container(GraphicsContainerItem** container,
1974 GDIPCONST GpGraphics* graphics){
1975 GpStatus sts;
1977 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1978 if(!(*container))
1979 return OutOfMemory;
1981 (*container)->contid = graphics->contid + 1;
1983 (*container)->smoothing = graphics->smoothing;
1984 (*container)->compqual = graphics->compqual;
1985 (*container)->interpolation = graphics->interpolation;
1986 (*container)->compmode = graphics->compmode;
1987 (*container)->texthint = graphics->texthint;
1988 (*container)->scale = graphics->scale;
1989 (*container)->unit = graphics->unit;
1990 (*container)->textcontrast = graphics->textcontrast;
1991 (*container)->pixeloffset = graphics->pixeloffset;
1992 (*container)->origin_x = graphics->origin_x;
1993 (*container)->origin_y = graphics->origin_y;
1995 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1996 if(sts != Ok){
1997 GdipFree(*container);
1998 *container = NULL;
1999 return sts;
2002 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2003 if(sts != Ok){
2004 GdipDeleteMatrix((*container)->worldtrans);
2005 GdipFree(*container);
2006 *container = NULL;
2007 return sts;
2010 return Ok;
2013 static void delete_container(GraphicsContainerItem* container){
2014 GdipDeleteMatrix(container->worldtrans);
2015 GdipDeleteRegion(container->clip);
2016 GdipFree(container);
2019 static GpStatus restore_container(GpGraphics* graphics,
2020 GDIPCONST GraphicsContainerItem* container){
2021 GpStatus sts;
2022 GpMatrix *newTrans;
2023 GpRegion *newClip;
2025 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
2026 if(sts != Ok)
2027 return sts;
2029 sts = GdipCloneRegion(container->clip, &newClip);
2030 if(sts != Ok){
2031 GdipDeleteMatrix(newTrans);
2032 return sts;
2035 GdipDeleteMatrix(graphics->worldtrans);
2036 graphics->worldtrans = newTrans;
2038 GdipDeleteRegion(graphics->clip);
2039 graphics->clip = newClip;
2041 graphics->contid = container->contid - 1;
2043 graphics->smoothing = container->smoothing;
2044 graphics->compqual = container->compqual;
2045 graphics->interpolation = container->interpolation;
2046 graphics->compmode = container->compmode;
2047 graphics->texthint = container->texthint;
2048 graphics->scale = container->scale;
2049 graphics->unit = container->unit;
2050 graphics->textcontrast = container->textcontrast;
2051 graphics->pixeloffset = container->pixeloffset;
2052 graphics->origin_x = container->origin_x;
2053 graphics->origin_y = container->origin_y;
2055 return Ok;
2058 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2060 RECT wnd_rect;
2061 GpStatus stat=Ok;
2062 GpUnit unit;
2064 if(graphics->hwnd) {
2065 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2066 return GenericError;
2068 rect->X = wnd_rect.left;
2069 rect->Y = wnd_rect.top;
2070 rect->Width = wnd_rect.right - wnd_rect.left;
2071 rect->Height = wnd_rect.bottom - wnd_rect.top;
2072 }else if (graphics->image){
2073 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2074 if (stat == Ok && unit != UnitPixel)
2075 FIXME("need to convert from unit %i\n", unit);
2076 }else{
2077 rect->X = 0;
2078 rect->Y = 0;
2079 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2080 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2083 return stat;
2086 /* on success, rgn will contain the region of the graphics object which
2087 * is visible after clipping has been applied */
2088 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2090 GpStatus stat;
2091 GpRectF rectf;
2092 GpRegion* tmp;
2094 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2095 return stat;
2097 if((stat = GdipCreateRegion(&tmp)) != Ok)
2098 return stat;
2100 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2101 goto end;
2103 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2104 goto end;
2106 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2108 end:
2109 GdipDeleteRegion(tmp);
2110 return stat;
2113 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font, HFONT *hfont)
2115 HDC hdc = CreateCompatibleDC(0);
2116 GpPointF pt[3];
2117 REAL angle, rel_width, rel_height, font_height, font_to_pixel_scale;
2118 LOGFONTW lfw;
2119 HFONT unscaled_font;
2120 TEXTMETRICW textmet;
2122 font_to_pixel_scale = units_scale(UnitPoint, UnitPixel, font->family->dpi);
2124 if (font->unit == UnitPixel)
2125 font_height = font->emSize * font_to_pixel_scale;
2126 else
2128 REAL unit_scale, res;
2130 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2131 unit_scale = units_scale(font->unit, graphics->unit, res);
2133 font_height = font->emSize * font_to_pixel_scale * unit_scale;
2134 if (graphics->unit != UnitDisplay)
2135 font_height /= graphics->scale;
2138 pt[0].X = 0.0;
2139 pt[0].Y = 0.0;
2140 pt[1].X = 1.0;
2141 pt[1].Y = 0.0;
2142 pt[2].X = 0.0;
2143 pt[2].Y = 1.0;
2144 if (graphics)
2145 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2146 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2147 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2148 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2149 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2150 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2152 get_log_fontW(font, graphics, &lfw);
2153 lfw.lfHeight = gdip_round(font_height * rel_height);
2154 unscaled_font = CreateFontIndirectW(&lfw);
2156 SelectObject(hdc, unscaled_font);
2157 GetTextMetricsW(hdc, &textmet);
2159 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2160 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2162 *hfont = CreateFontIndirectW(&lfw);
2164 DeleteDC(hdc);
2165 DeleteObject(unscaled_font);
2168 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2170 TRACE("(%p, %p)\n", hdc, graphics);
2172 return GdipCreateFromHDC2(hdc, NULL, graphics);
2175 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2177 GpStatus retval;
2179 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2181 if(hDevice != NULL) {
2182 FIXME("Don't know how to handle parameter hDevice\n");
2183 return NotImplemented;
2186 if(hdc == NULL)
2187 return OutOfMemory;
2189 if(graphics == NULL)
2190 return InvalidParameter;
2192 *graphics = GdipAlloc(sizeof(GpGraphics));
2193 if(!*graphics) return OutOfMemory;
2195 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2196 GdipFree(*graphics);
2197 return retval;
2200 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2201 GdipFree((*graphics)->worldtrans);
2202 GdipFree(*graphics);
2203 return retval;
2206 (*graphics)->hdc = hdc;
2207 (*graphics)->hwnd = WindowFromDC(hdc);
2208 (*graphics)->owndc = FALSE;
2209 (*graphics)->smoothing = SmoothingModeDefault;
2210 (*graphics)->compqual = CompositingQualityDefault;
2211 (*graphics)->interpolation = InterpolationModeBilinear;
2212 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2213 (*graphics)->compmode = CompositingModeSourceOver;
2214 (*graphics)->unit = UnitDisplay;
2215 (*graphics)->scale = 1.0;
2216 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2217 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2218 (*graphics)->busy = FALSE;
2219 (*graphics)->textcontrast = 4;
2220 list_init(&(*graphics)->containers);
2221 (*graphics)->contid = 0;
2223 TRACE("<-- %p\n", *graphics);
2225 return Ok;
2228 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2230 GpStatus retval;
2232 *graphics = GdipAlloc(sizeof(GpGraphics));
2233 if(!*graphics) return OutOfMemory;
2235 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2236 GdipFree(*graphics);
2237 return retval;
2240 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2241 GdipFree((*graphics)->worldtrans);
2242 GdipFree(*graphics);
2243 return retval;
2246 (*graphics)->hdc = NULL;
2247 (*graphics)->hwnd = NULL;
2248 (*graphics)->owndc = FALSE;
2249 (*graphics)->image = image;
2250 (*graphics)->smoothing = SmoothingModeDefault;
2251 (*graphics)->compqual = CompositingQualityDefault;
2252 (*graphics)->interpolation = InterpolationModeBilinear;
2253 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2254 (*graphics)->compmode = CompositingModeSourceOver;
2255 (*graphics)->unit = UnitDisplay;
2256 (*graphics)->scale = 1.0;
2257 (*graphics)->xres = image->xres;
2258 (*graphics)->yres = image->yres;
2259 (*graphics)->busy = FALSE;
2260 (*graphics)->textcontrast = 4;
2261 list_init(&(*graphics)->containers);
2262 (*graphics)->contid = 0;
2264 TRACE("<-- %p\n", *graphics);
2266 return Ok;
2269 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2271 GpStatus ret;
2272 HDC hdc;
2274 TRACE("(%p, %p)\n", hwnd, graphics);
2276 hdc = GetDC(hwnd);
2278 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2280 ReleaseDC(hwnd, hdc);
2281 return ret;
2284 (*graphics)->hwnd = hwnd;
2285 (*graphics)->owndc = TRUE;
2287 return Ok;
2290 /* FIXME: no icm handling */
2291 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2293 TRACE("(%p, %p)\n", hwnd, graphics);
2295 return GdipCreateFromHWND(hwnd, graphics);
2298 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2299 GpMetafile **metafile)
2301 IStream *stream = NULL;
2302 UINT read;
2303 ENHMETAHEADER *copy;
2304 GpStatus retval = Ok;
2306 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2308 if(!hemf || !metafile)
2309 return InvalidParameter;
2311 read = GetEnhMetaFileBits(hemf, 0, NULL);
2312 copy = GdipAlloc(read);
2313 GetEnhMetaFileBits(hemf, read, (BYTE *)copy);
2315 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
2316 ERR("could not make stream\n");
2317 GdipFree(copy);
2318 retval = GenericError;
2319 goto err;
2322 *metafile = GdipAlloc(sizeof(GpMetafile));
2323 if(!*metafile){
2324 retval = OutOfMemory;
2325 goto err;
2328 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2329 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
2331 retval = GenericError;
2332 goto err;
2336 (*metafile)->image.type = ImageTypeMetafile;
2337 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
2338 (*metafile)->image.palette = NULL;
2339 (*metafile)->image.xres = (REAL)copy->szlDevice.cx;
2340 (*metafile)->image.yres = (REAL)copy->szlDevice.cy;
2341 (*metafile)->bounds.X = (REAL)copy->rclBounds.left;
2342 (*metafile)->bounds.Y = (REAL)copy->rclBounds.top;
2343 (*metafile)->bounds.Width = (REAL)(copy->rclBounds.right - copy->rclBounds.left);
2344 (*metafile)->bounds.Height = (REAL)(copy->rclBounds.bottom - copy->rclBounds.top);
2345 (*metafile)->unit = UnitPixel;
2347 if(delete)
2348 DeleteEnhMetaFile(hemf);
2350 TRACE("<-- %p\n", *metafile);
2352 err:
2353 if (retval != Ok)
2354 GdipFree(*metafile);
2355 IStream_Release(stream);
2356 return retval;
2359 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2360 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2362 UINT read;
2363 BYTE *copy;
2364 HENHMETAFILE hemf;
2365 GpStatus retval = Ok;
2367 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2369 if(!hwmf || !metafile || !placeable)
2370 return InvalidParameter;
2372 *metafile = NULL;
2373 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2374 if(!read)
2375 return GenericError;
2376 copy = GdipAlloc(read);
2377 GetMetaFileBitsEx(hwmf, read, copy);
2379 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2380 GdipFree(copy);
2382 retval = GdipCreateMetafileFromEmf(hemf, FALSE, metafile);
2384 if (retval == Ok)
2386 (*metafile)->image.xres = (REAL)placeable->Inch;
2387 (*metafile)->image.yres = (REAL)placeable->Inch;
2388 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2389 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2390 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2391 placeable->BoundingBox.Left);
2392 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2393 placeable->BoundingBox.Top);
2395 if (delete) DeleteMetaFile(hwmf);
2397 return retval;
2400 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2401 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2403 HMETAFILE hmf = GetMetaFileW(file);
2405 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2407 if(!hmf) return InvalidParameter;
2409 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2412 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2413 GpMetafile **metafile)
2415 FIXME("(%p, %p): stub\n", file, metafile);
2416 return NotImplemented;
2419 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2420 GpMetafile **metafile)
2422 FIXME("(%p, %p): stub\n", stream, metafile);
2423 return NotImplemented;
2426 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2427 UINT access, IStream **stream)
2429 DWORD dwMode;
2430 HRESULT ret;
2432 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2434 if(!stream || !filename)
2435 return InvalidParameter;
2437 if(access & GENERIC_WRITE)
2438 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2439 else if(access & GENERIC_READ)
2440 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2441 else
2442 return InvalidParameter;
2444 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2446 return hresult_to_status(ret);
2449 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2451 GraphicsContainerItem *cont, *next;
2452 GpStatus stat;
2453 TRACE("(%p)\n", graphics);
2455 if(!graphics) return InvalidParameter;
2456 if(graphics->busy) return ObjectBusy;
2458 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2460 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2461 if (stat != Ok)
2462 return stat;
2465 if(graphics->owndc)
2466 ReleaseDC(graphics->hwnd, graphics->hdc);
2468 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2469 list_remove(&cont->entry);
2470 delete_container(cont);
2473 GdipDeleteRegion(graphics->clip);
2474 GdipDeleteMatrix(graphics->worldtrans);
2475 GdipFree(graphics);
2477 return Ok;
2480 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2481 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2483 INT save_state, num_pts;
2484 GpPointF points[MAX_ARC_PTS];
2485 GpStatus retval;
2487 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2488 width, height, startAngle, sweepAngle);
2490 if(!graphics || !pen || width <= 0 || height <= 0)
2491 return InvalidParameter;
2493 if(graphics->busy)
2494 return ObjectBusy;
2496 if (!graphics->hdc)
2498 FIXME("graphics object has no HDC\n");
2499 return Ok;
2502 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2504 save_state = prepare_dc(graphics, pen);
2506 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2508 restore_dc(graphics, save_state);
2510 return retval;
2513 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2514 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2516 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2517 width, height, startAngle, sweepAngle);
2519 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2522 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2523 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2525 INT save_state;
2526 GpPointF pt[4];
2527 GpStatus retval;
2529 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2530 x2, y2, x3, y3, x4, y4);
2532 if(!graphics || !pen)
2533 return InvalidParameter;
2535 if(graphics->busy)
2536 return ObjectBusy;
2538 if (!graphics->hdc)
2540 FIXME("graphics object has no HDC\n");
2541 return Ok;
2544 pt[0].X = x1;
2545 pt[0].Y = y1;
2546 pt[1].X = x2;
2547 pt[1].Y = y2;
2548 pt[2].X = x3;
2549 pt[2].Y = y3;
2550 pt[3].X = x4;
2551 pt[3].Y = y4;
2553 save_state = prepare_dc(graphics, pen);
2555 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2557 restore_dc(graphics, save_state);
2559 return retval;
2562 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2563 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2565 INT save_state;
2566 GpPointF pt[4];
2567 GpStatus retval;
2569 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2570 x2, y2, x3, y3, x4, y4);
2572 if(!graphics || !pen)
2573 return InvalidParameter;
2575 if(graphics->busy)
2576 return ObjectBusy;
2578 if (!graphics->hdc)
2580 FIXME("graphics object has no HDC\n");
2581 return Ok;
2584 pt[0].X = x1;
2585 pt[0].Y = y1;
2586 pt[1].X = x2;
2587 pt[1].Y = y2;
2588 pt[2].X = x3;
2589 pt[2].Y = y3;
2590 pt[3].X = x4;
2591 pt[3].Y = y4;
2593 save_state = prepare_dc(graphics, pen);
2595 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2597 restore_dc(graphics, save_state);
2599 return retval;
2602 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2603 GDIPCONST GpPointF *points, INT count)
2605 INT i;
2606 GpStatus ret;
2608 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2610 if(!graphics || !pen || !points || (count <= 0))
2611 return InvalidParameter;
2613 if(graphics->busy)
2614 return ObjectBusy;
2616 for(i = 0; i < floor(count / 4); i++){
2617 ret = GdipDrawBezier(graphics, pen,
2618 points[4*i].X, points[4*i].Y,
2619 points[4*i + 1].X, points[4*i + 1].Y,
2620 points[4*i + 2].X, points[4*i + 2].Y,
2621 points[4*i + 3].X, points[4*i + 3].Y);
2622 if(ret != Ok)
2623 return ret;
2626 return Ok;
2629 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2630 GDIPCONST GpPoint *points, INT count)
2632 GpPointF *pts;
2633 GpStatus ret;
2634 INT i;
2636 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2638 if(!graphics || !pen || !points || (count <= 0))
2639 return InvalidParameter;
2641 if(graphics->busy)
2642 return ObjectBusy;
2644 pts = GdipAlloc(sizeof(GpPointF) * count);
2645 if(!pts)
2646 return OutOfMemory;
2648 for(i = 0; i < count; i++){
2649 pts[i].X = (REAL)points[i].X;
2650 pts[i].Y = (REAL)points[i].Y;
2653 ret = GdipDrawBeziers(graphics,pen,pts,count);
2655 GdipFree(pts);
2657 return ret;
2660 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2661 GDIPCONST GpPointF *points, INT count)
2663 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2665 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2668 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2669 GDIPCONST GpPoint *points, INT count)
2671 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2673 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2676 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2677 GDIPCONST GpPointF *points, INT count, REAL tension)
2679 GpPath *path;
2680 GpStatus stat;
2682 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2684 if(!graphics || !pen || !points || count <= 0)
2685 return InvalidParameter;
2687 if(graphics->busy)
2688 return ObjectBusy;
2690 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2691 return stat;
2693 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2694 if(stat != Ok){
2695 GdipDeletePath(path);
2696 return stat;
2699 stat = GdipDrawPath(graphics, pen, path);
2701 GdipDeletePath(path);
2703 return stat;
2706 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2707 GDIPCONST GpPoint *points, INT count, REAL tension)
2709 GpPointF *ptf;
2710 GpStatus stat;
2711 INT i;
2713 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2715 if(!points || count <= 0)
2716 return InvalidParameter;
2718 ptf = GdipAlloc(sizeof(GpPointF)*count);
2719 if(!ptf)
2720 return OutOfMemory;
2722 for(i = 0; i < count; i++){
2723 ptf[i].X = (REAL)points[i].X;
2724 ptf[i].Y = (REAL)points[i].Y;
2727 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2729 GdipFree(ptf);
2731 return stat;
2734 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2735 GDIPCONST GpPointF *points, INT count)
2737 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2739 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2742 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2743 GDIPCONST GpPoint *points, INT count)
2745 GpPointF *pointsF;
2746 GpStatus ret;
2747 INT i;
2749 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2751 if(!points)
2752 return InvalidParameter;
2754 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2755 if(!pointsF)
2756 return OutOfMemory;
2758 for(i = 0; i < count; i++){
2759 pointsF[i].X = (REAL)points[i].X;
2760 pointsF[i].Y = (REAL)points[i].Y;
2763 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2764 GdipFree(pointsF);
2766 return ret;
2769 /* Approximates cardinal spline with Bezier curves. */
2770 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2771 GDIPCONST GpPointF *points, INT count, REAL tension)
2773 /* PolyBezier expects count*3-2 points. */
2774 INT i, len_pt = count*3-2, save_state;
2775 GpPointF *pt;
2776 REAL x1, x2, y1, y2;
2777 GpStatus retval;
2779 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2781 if(!graphics || !pen)
2782 return InvalidParameter;
2784 if(graphics->busy)
2785 return ObjectBusy;
2787 if(count < 2)
2788 return InvalidParameter;
2790 if (!graphics->hdc)
2792 FIXME("graphics object has no HDC\n");
2793 return Ok;
2796 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2797 if(!pt)
2798 return OutOfMemory;
2800 tension = tension * TENSION_CONST;
2802 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2803 tension, &x1, &y1);
2805 pt[0].X = points[0].X;
2806 pt[0].Y = points[0].Y;
2807 pt[1].X = x1;
2808 pt[1].Y = y1;
2810 for(i = 0; i < count-2; i++){
2811 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2813 pt[3*i+2].X = x1;
2814 pt[3*i+2].Y = y1;
2815 pt[3*i+3].X = points[i+1].X;
2816 pt[3*i+3].Y = points[i+1].Y;
2817 pt[3*i+4].X = x2;
2818 pt[3*i+4].Y = y2;
2821 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2822 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2824 pt[len_pt-2].X = x1;
2825 pt[len_pt-2].Y = y1;
2826 pt[len_pt-1].X = points[count-1].X;
2827 pt[len_pt-1].Y = points[count-1].Y;
2829 save_state = prepare_dc(graphics, pen);
2831 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2833 GdipFree(pt);
2834 restore_dc(graphics, save_state);
2836 return retval;
2839 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2840 GDIPCONST GpPoint *points, INT count, REAL tension)
2842 GpPointF *pointsF;
2843 GpStatus ret;
2844 INT i;
2846 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2848 if(!points)
2849 return InvalidParameter;
2851 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2852 if(!pointsF)
2853 return OutOfMemory;
2855 for(i = 0; i < count; i++){
2856 pointsF[i].X = (REAL)points[i].X;
2857 pointsF[i].Y = (REAL)points[i].Y;
2860 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2861 GdipFree(pointsF);
2863 return ret;
2866 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2867 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2868 REAL tension)
2870 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2872 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2873 return InvalidParameter;
2876 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2879 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2880 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2881 REAL tension)
2883 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2885 if(count < 0){
2886 return OutOfMemory;
2889 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2890 return InvalidParameter;
2893 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2896 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2897 REAL y, REAL width, REAL height)
2899 INT save_state;
2900 GpPointF ptf[2];
2901 POINT pti[2];
2903 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2905 if(!graphics || !pen)
2906 return InvalidParameter;
2908 if(graphics->busy)
2909 return ObjectBusy;
2911 if (!graphics->hdc)
2913 FIXME("graphics object has no HDC\n");
2914 return Ok;
2917 ptf[0].X = x;
2918 ptf[0].Y = y;
2919 ptf[1].X = x + width;
2920 ptf[1].Y = y + height;
2922 save_state = prepare_dc(graphics, pen);
2923 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2925 transform_and_round_points(graphics, pti, ptf, 2);
2927 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2929 restore_dc(graphics, save_state);
2931 return Ok;
2934 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2935 INT y, INT width, INT height)
2937 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2939 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2943 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2945 UINT width, height;
2947 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2949 if(!graphics || !image)
2950 return InvalidParameter;
2952 GdipGetImageWidth(image, &width);
2953 GdipGetImageHeight(image, &height);
2955 return GdipDrawImagePointRect(graphics, image, x, y,
2956 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2959 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2960 INT y)
2962 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2964 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2967 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2968 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2969 GpUnit srcUnit)
2971 GpPointF points[3];
2972 REAL scale_x, scale_y, width, height;
2974 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2976 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2977 scale_x *= graphics->xres / image->xres;
2978 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2979 scale_y *= graphics->yres / image->yres;
2980 width = srcwidth * scale_x;
2981 height = srcheight * scale_y;
2983 points[0].X = points[2].X = x;
2984 points[0].Y = points[1].Y = y;
2985 points[1].X = x + width;
2986 points[2].Y = y + height;
2988 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2989 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2992 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2993 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2994 GpUnit srcUnit)
2996 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2999 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3000 GDIPCONST GpPointF *dstpoints, INT count)
3002 UINT width, height;
3004 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3006 if(!image)
3007 return InvalidParameter;
3009 GdipGetImageWidth(image, &width);
3010 GdipGetImageHeight(image, &height);
3012 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3013 width, height, UnitPixel, NULL, NULL, NULL);
3016 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3017 GDIPCONST GpPoint *dstpoints, INT count)
3019 GpPointF ptf[3];
3021 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3023 if (count != 3 || !dstpoints)
3024 return InvalidParameter;
3026 ptf[0].X = (REAL)dstpoints[0].X;
3027 ptf[0].Y = (REAL)dstpoints[0].Y;
3028 ptf[1].X = (REAL)dstpoints[1].X;
3029 ptf[1].Y = (REAL)dstpoints[1].Y;
3030 ptf[2].X = (REAL)dstpoints[2].X;
3031 ptf[2].Y = (REAL)dstpoints[2].Y;
3033 return GdipDrawImagePoints(graphics, image, ptf, count);
3036 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3037 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3038 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3039 DrawImageAbort callback, VOID * callbackData)
3041 GpPointF ptf[4];
3042 POINT pti[4];
3043 GpStatus stat;
3045 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3046 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3047 callbackData);
3049 if (count > 3)
3050 return NotImplemented;
3052 if(!graphics || !image || !points || count != 3)
3053 return InvalidParameter;
3055 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3056 debugstr_pointf(&points[2]));
3058 memcpy(ptf, points, 3 * sizeof(GpPointF));
3059 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3060 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3061 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3062 return Ok;
3063 transform_and_round_points(graphics, pti, ptf, 4);
3065 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3066 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3068 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3069 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3070 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3071 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3072 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3074 if (image->picture)
3076 if (!graphics->hdc)
3078 FIXME("graphics object has no HDC\n");
3081 if(IPicture_Render(image->picture, graphics->hdc,
3082 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3083 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3085 if(callback)
3086 callback(callbackData);
3087 return GenericError;
3090 else if (image->type == ImageTypeBitmap)
3092 GpBitmap* bitmap = (GpBitmap*)image;
3093 int use_software=0;
3095 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3096 graphics->xres, graphics->yres,
3097 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3098 graphics->scale, image->xres, image->yres, bitmap->format,
3099 imageAttributes ? imageAttributes->outside_color : 0);
3101 if (imageAttributes ||
3102 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3103 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3104 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3105 srcx < 0 || srcy < 0 ||
3106 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3107 use_software = 1;
3109 if (use_software)
3111 RECT dst_area;
3112 GpRect src_area;
3113 int i, x, y, src_stride, dst_stride;
3114 GpMatrix *dst_to_src;
3115 REAL m11, m12, m21, m22, mdx, mdy;
3116 LPBYTE src_data, dst_data;
3117 BitmapData lockeddata;
3118 InterpolationMode interpolation = graphics->interpolation;
3119 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3120 REAL x_dx, x_dy, y_dx, y_dy;
3121 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3123 if (!imageAttributes)
3124 imageAttributes = &defaultImageAttributes;
3126 dst_area.left = dst_area.right = pti[0].x;
3127 dst_area.top = dst_area.bottom = pti[0].y;
3128 for (i=1; i<4; i++)
3130 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3131 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3132 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3133 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3136 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3138 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3139 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3140 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3141 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3142 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3143 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3145 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
3146 if (stat != Ok) return stat;
3148 stat = GdipInvertMatrix(dst_to_src);
3149 if (stat != Ok)
3151 GdipDeleteMatrix(dst_to_src);
3152 return stat;
3155 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3156 if (!dst_data)
3158 GdipDeleteMatrix(dst_to_src);
3159 return OutOfMemory;
3162 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3164 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3165 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3167 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3169 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3170 if (!src_data)
3172 GdipFree(dst_data);
3173 GdipDeleteMatrix(dst_to_src);
3174 return OutOfMemory;
3176 src_stride = sizeof(ARGB) * src_area.Width;
3178 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3179 lockeddata.Width = src_area.Width;
3180 lockeddata.Height = src_area.Height;
3181 lockeddata.Stride = src_stride;
3182 lockeddata.PixelFormat = PixelFormat32bppARGB;
3183 lockeddata.Scan0 = src_data;
3185 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3186 PixelFormat32bppARGB, &lockeddata);
3188 if (stat == Ok)
3189 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3191 if (stat != Ok)
3193 if (src_data != dst_data)
3194 GdipFree(src_data);
3195 GdipFree(dst_data);
3196 GdipDeleteMatrix(dst_to_src);
3197 return stat;
3200 apply_image_attributes(imageAttributes, src_data,
3201 src_area.Width, src_area.Height,
3202 src_stride, ColorAdjustTypeBitmap);
3204 /* Transform the bits as needed to the destination. */
3205 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
3207 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3208 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3209 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3210 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3212 for (x=dst_area.left; x<dst_area.right; x++)
3214 for (y=dst_area.top; y<dst_area.bottom; y++)
3216 GpPointF src_pointf;
3217 ARGB *dst_color;
3219 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3220 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3222 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3224 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3225 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
3226 else
3227 *dst_color = 0;
3231 GdipDeleteMatrix(dst_to_src);
3233 GdipFree(src_data);
3235 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3236 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3238 GdipFree(dst_data);
3240 return stat;
3242 else
3244 HDC hdc;
3245 int temp_hdc=0, temp_bitmap=0;
3246 HBITMAP hbitmap, old_hbm=NULL;
3248 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3249 bitmap->format == PixelFormat24bppRGB ||
3250 bitmap->format == PixelFormat32bppRGB ||
3251 bitmap->format == PixelFormat32bppPARGB))
3253 BITMAPINFOHEADER bih;
3254 BYTE *temp_bits;
3255 PixelFormat dst_format;
3257 /* we can't draw a bitmap of this format directly */
3258 hdc = CreateCompatibleDC(0);
3259 temp_hdc = 1;
3260 temp_bitmap = 1;
3262 bih.biSize = sizeof(BITMAPINFOHEADER);
3263 bih.biWidth = bitmap->width;
3264 bih.biHeight = -bitmap->height;
3265 bih.biPlanes = 1;
3266 bih.biBitCount = 32;
3267 bih.biCompression = BI_RGB;
3268 bih.biSizeImage = 0;
3269 bih.biXPelsPerMeter = 0;
3270 bih.biYPelsPerMeter = 0;
3271 bih.biClrUsed = 0;
3272 bih.biClrImportant = 0;
3274 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3275 (void**)&temp_bits, NULL, 0);
3277 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3278 dst_format = PixelFormat32bppPARGB;
3279 else
3280 dst_format = PixelFormat32bppRGB;
3282 convert_pixels(bitmap->width, bitmap->height,
3283 bitmap->width*4, temp_bits, dst_format,
3284 bitmap->stride, bitmap->bits, bitmap->format,
3285 bitmap->image.palette);
3287 else
3289 if (bitmap->hbitmap)
3290 hbitmap = bitmap->hbitmap;
3291 else
3293 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3294 temp_bitmap = 1;
3297 hdc = bitmap->hdc;
3298 temp_hdc = (hdc == 0);
3301 if (temp_hdc)
3303 if (!hdc) hdc = CreateCompatibleDC(0);
3304 old_hbm = SelectObject(hdc, hbitmap);
3307 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3309 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3310 hdc, srcx, srcy, srcwidth, srcheight);
3312 else
3314 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3315 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3318 if (temp_hdc)
3320 SelectObject(hdc, old_hbm);
3321 DeleteDC(hdc);
3324 if (temp_bitmap)
3325 DeleteObject(hbitmap);
3328 else
3330 ERR("GpImage with no IPicture or HBITMAP?!\n");
3331 return NotImplemented;
3334 return Ok;
3337 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3338 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3339 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3340 DrawImageAbort callback, VOID * callbackData)
3342 GpPointF pointsF[3];
3343 INT i;
3345 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3346 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3347 callbackData);
3349 if(!points || count!=3)
3350 return InvalidParameter;
3352 for(i = 0; i < count; i++){
3353 pointsF[i].X = (REAL)points[i].X;
3354 pointsF[i].Y = (REAL)points[i].Y;
3357 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3358 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3359 callback, callbackData);
3362 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3363 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3364 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3365 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3366 VOID * callbackData)
3368 GpPointF points[3];
3370 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3371 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3372 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3374 points[0].X = dstx;
3375 points[0].Y = dsty;
3376 points[1].X = dstx + dstwidth;
3377 points[1].Y = dsty;
3378 points[2].X = dstx;
3379 points[2].Y = dsty + dstheight;
3381 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3382 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3385 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3386 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3387 INT srcwidth, INT srcheight, GpUnit srcUnit,
3388 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3389 VOID * callbackData)
3391 GpPointF points[3];
3393 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3394 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3395 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3397 points[0].X = dstx;
3398 points[0].Y = dsty;
3399 points[1].X = dstx + dstwidth;
3400 points[1].Y = dsty;
3401 points[2].X = dstx;
3402 points[2].Y = dsty + dstheight;
3404 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3405 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3408 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3409 REAL x, REAL y, REAL width, REAL height)
3411 RectF bounds;
3412 GpUnit unit;
3413 GpStatus ret;
3415 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3417 if(!graphics || !image)
3418 return InvalidParameter;
3420 ret = GdipGetImageBounds(image, &bounds, &unit);
3421 if(ret != Ok)
3422 return ret;
3424 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3425 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3426 unit, NULL, NULL, NULL);
3429 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3430 INT x, INT y, INT width, INT height)
3432 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3434 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3437 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3438 REAL y1, REAL x2, REAL y2)
3440 INT save_state;
3441 GpPointF pt[2];
3442 GpStatus retval;
3444 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3446 if(!pen || !graphics)
3447 return InvalidParameter;
3449 if(graphics->busy)
3450 return ObjectBusy;
3452 if (!graphics->hdc)
3454 FIXME("graphics object has no HDC\n");
3455 return Ok;
3458 pt[0].X = x1;
3459 pt[0].Y = y1;
3460 pt[1].X = x2;
3461 pt[1].Y = y2;
3463 save_state = prepare_dc(graphics, pen);
3465 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3467 restore_dc(graphics, save_state);
3469 return retval;
3472 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3473 INT y1, INT x2, INT y2)
3475 INT save_state;
3476 GpPointF pt[2];
3477 GpStatus retval;
3479 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3481 if(!pen || !graphics)
3482 return InvalidParameter;
3484 if(graphics->busy)
3485 return ObjectBusy;
3487 if (!graphics->hdc)
3489 FIXME("graphics object has no HDC\n");
3490 return Ok;
3493 pt[0].X = (REAL)x1;
3494 pt[0].Y = (REAL)y1;
3495 pt[1].X = (REAL)x2;
3496 pt[1].Y = (REAL)y2;
3498 save_state = prepare_dc(graphics, pen);
3500 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3502 restore_dc(graphics, save_state);
3504 return retval;
3507 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3508 GpPointF *points, INT count)
3510 INT save_state;
3511 GpStatus retval;
3513 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3515 if(!pen || !graphics || (count < 2))
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 save_state = prepare_dc(graphics, pen);
3529 retval = draw_polyline(graphics, pen, points, count, TRUE);
3531 restore_dc(graphics, save_state);
3533 return retval;
3536 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3537 GpPoint *points, INT count)
3539 INT save_state;
3540 GpStatus retval;
3541 GpPointF *ptf = NULL;
3542 int i;
3544 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3546 if(!pen || !graphics || (count < 2))
3547 return InvalidParameter;
3549 if(graphics->busy)
3550 return ObjectBusy;
3552 if (!graphics->hdc)
3554 FIXME("graphics object has no HDC\n");
3555 return Ok;
3558 ptf = GdipAlloc(count * sizeof(GpPointF));
3559 if(!ptf) return OutOfMemory;
3561 for(i = 0; i < count; i ++){
3562 ptf[i].X = (REAL) points[i].X;
3563 ptf[i].Y = (REAL) points[i].Y;
3566 save_state = prepare_dc(graphics, pen);
3568 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3570 restore_dc(graphics, save_state);
3572 GdipFree(ptf);
3573 return retval;
3576 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3578 INT save_state;
3579 GpStatus retval;
3581 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3583 if(!pen || !graphics)
3584 return InvalidParameter;
3586 if(graphics->busy)
3587 return ObjectBusy;
3589 if (!graphics->hdc)
3591 FIXME("graphics object has no HDC\n");
3592 return Ok;
3595 save_state = prepare_dc(graphics, pen);
3597 retval = draw_poly(graphics, pen, path->pathdata.Points,
3598 path->pathdata.Types, path->pathdata.Count, TRUE);
3600 restore_dc(graphics, save_state);
3602 return retval;
3605 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3606 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3608 INT save_state;
3610 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3611 width, height, startAngle, sweepAngle);
3613 if(!graphics || !pen)
3614 return InvalidParameter;
3616 if(graphics->busy)
3617 return ObjectBusy;
3619 if (!graphics->hdc)
3621 FIXME("graphics object has no HDC\n");
3622 return Ok;
3625 save_state = prepare_dc(graphics, pen);
3626 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3628 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3630 restore_dc(graphics, save_state);
3632 return Ok;
3635 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3636 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3638 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3639 width, height, startAngle, sweepAngle);
3641 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3644 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3645 REAL y, REAL width, REAL height)
3647 INT save_state;
3648 GpPointF ptf[4];
3649 POINT pti[4];
3651 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3653 if(!pen || !graphics)
3654 return InvalidParameter;
3656 if(graphics->busy)
3657 return ObjectBusy;
3659 if (!graphics->hdc)
3661 FIXME("graphics object has no HDC\n");
3662 return Ok;
3665 ptf[0].X = x;
3666 ptf[0].Y = y;
3667 ptf[1].X = x + width;
3668 ptf[1].Y = y;
3669 ptf[2].X = x + width;
3670 ptf[2].Y = y + height;
3671 ptf[3].X = x;
3672 ptf[3].Y = y + height;
3674 save_state = prepare_dc(graphics, pen);
3675 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3677 transform_and_round_points(graphics, pti, ptf, 4);
3678 Polygon(graphics->hdc, pti, 4);
3680 restore_dc(graphics, save_state);
3682 return Ok;
3685 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3686 INT y, INT width, INT height)
3688 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3690 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3693 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3694 GDIPCONST GpRectF* rects, INT count)
3696 GpPointF *ptf;
3697 POINT *pti;
3698 INT save_state, i;
3700 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3702 if(!graphics || !pen || !rects || count < 1)
3703 return InvalidParameter;
3705 if(graphics->busy)
3706 return ObjectBusy;
3708 if (!graphics->hdc)
3710 FIXME("graphics object has no HDC\n");
3711 return Ok;
3714 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3715 pti = GdipAlloc(4 * count * sizeof(POINT));
3717 if(!ptf || !pti){
3718 GdipFree(ptf);
3719 GdipFree(pti);
3720 return OutOfMemory;
3723 for(i = 0; i < count; i++){
3724 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3725 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3726 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3727 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3730 save_state = prepare_dc(graphics, pen);
3731 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3733 transform_and_round_points(graphics, pti, ptf, 4 * count);
3735 for(i = 0; i < count; i++)
3736 Polygon(graphics->hdc, &pti[4 * i], 4);
3738 restore_dc(graphics, save_state);
3740 GdipFree(ptf);
3741 GdipFree(pti);
3743 return Ok;
3746 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3747 GDIPCONST GpRect* rects, INT count)
3749 GpRectF *rectsF;
3750 GpStatus ret;
3751 INT i;
3753 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3755 if(!rects || count<=0)
3756 return InvalidParameter;
3758 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3759 if(!rectsF)
3760 return OutOfMemory;
3762 for(i = 0;i < count;i++){
3763 rectsF[i].X = (REAL)rects[i].X;
3764 rectsF[i].Y = (REAL)rects[i].Y;
3765 rectsF[i].Width = (REAL)rects[i].Width;
3766 rectsF[i].Height = (REAL)rects[i].Height;
3769 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3770 GdipFree(rectsF);
3772 return ret;
3775 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3776 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3778 GpPath *path;
3779 GpStatus stat;
3781 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3782 count, tension, fill);
3784 if(!graphics || !brush || !points)
3785 return InvalidParameter;
3787 if(graphics->busy)
3788 return ObjectBusy;
3790 if(count == 1) /* Do nothing */
3791 return Ok;
3793 stat = GdipCreatePath(fill, &path);
3794 if(stat != Ok)
3795 return stat;
3797 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3798 if(stat != Ok){
3799 GdipDeletePath(path);
3800 return stat;
3803 stat = GdipFillPath(graphics, brush, path);
3804 if(stat != Ok){
3805 GdipDeletePath(path);
3806 return stat;
3809 GdipDeletePath(path);
3811 return Ok;
3814 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3815 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3817 GpPointF *ptf;
3818 GpStatus stat;
3819 INT i;
3821 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3822 count, tension, fill);
3824 if(!points || count == 0)
3825 return InvalidParameter;
3827 if(count == 1) /* Do nothing */
3828 return Ok;
3830 ptf = GdipAlloc(sizeof(GpPointF)*count);
3831 if(!ptf)
3832 return OutOfMemory;
3834 for(i = 0;i < count;i++){
3835 ptf[i].X = (REAL)points[i].X;
3836 ptf[i].Y = (REAL)points[i].Y;
3839 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3841 GdipFree(ptf);
3843 return stat;
3846 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3847 GDIPCONST GpPointF *points, INT count)
3849 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3850 return GdipFillClosedCurve2(graphics, brush, points, count,
3851 0.5f, FillModeAlternate);
3854 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3855 GDIPCONST GpPoint *points, INT count)
3857 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3858 return GdipFillClosedCurve2I(graphics, brush, points, count,
3859 0.5f, FillModeAlternate);
3862 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3863 REAL y, REAL width, REAL height)
3865 GpStatus stat;
3866 GpPath *path;
3868 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3870 if(!graphics || !brush)
3871 return InvalidParameter;
3873 if(graphics->busy)
3874 return ObjectBusy;
3876 stat = GdipCreatePath(FillModeAlternate, &path);
3878 if (stat == Ok)
3880 stat = GdipAddPathEllipse(path, x, y, width, height);
3882 if (stat == Ok)
3883 stat = GdipFillPath(graphics, brush, path);
3885 GdipDeletePath(path);
3888 return stat;
3891 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3892 INT y, INT width, INT height)
3894 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3896 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3899 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3901 INT save_state;
3902 GpStatus retval;
3904 if(!graphics->hdc || !brush_can_fill_path(brush))
3905 return NotImplemented;
3907 save_state = SaveDC(graphics->hdc);
3908 EndPath(graphics->hdc);
3909 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3910 : WINDING));
3912 BeginPath(graphics->hdc);
3913 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3914 path->pathdata.Types, path->pathdata.Count, FALSE);
3916 if(retval != Ok)
3917 goto end;
3919 EndPath(graphics->hdc);
3920 brush_fill_path(graphics, brush);
3922 retval = Ok;
3924 end:
3925 RestoreDC(graphics->hdc, save_state);
3927 return retval;
3930 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3932 GpStatus stat;
3933 GpRegion *rgn;
3935 if (!brush_can_fill_pixels(brush))
3936 return NotImplemented;
3938 /* FIXME: This could probably be done more efficiently without regions. */
3940 stat = GdipCreateRegionPath(path, &rgn);
3942 if (stat == Ok)
3944 stat = GdipFillRegion(graphics, brush, rgn);
3946 GdipDeleteRegion(rgn);
3949 return stat;
3952 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3954 GpStatus stat = NotImplemented;
3956 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3958 if(!brush || !graphics || !path)
3959 return InvalidParameter;
3961 if(graphics->busy)
3962 return ObjectBusy;
3964 if (!graphics->image)
3965 stat = GDI32_GdipFillPath(graphics, brush, path);
3967 if (stat == NotImplemented)
3968 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3970 if (stat == NotImplemented)
3972 FIXME("Not implemented for brushtype %i\n", brush->bt);
3973 stat = Ok;
3976 return stat;
3979 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3980 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3982 GpStatus stat;
3983 GpPath *path;
3985 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3986 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3988 if(!graphics || !brush)
3989 return InvalidParameter;
3991 if(graphics->busy)
3992 return ObjectBusy;
3994 stat = GdipCreatePath(FillModeAlternate, &path);
3996 if (stat == Ok)
3998 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4000 if (stat == Ok)
4001 stat = GdipFillPath(graphics, brush, path);
4003 GdipDeletePath(path);
4006 return stat;
4009 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4010 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4012 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4013 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4015 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4018 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4019 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4021 GpStatus stat;
4022 GpPath *path;
4024 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4026 if(!graphics || !brush || !points || !count)
4027 return InvalidParameter;
4029 if(graphics->busy)
4030 return ObjectBusy;
4032 stat = GdipCreatePath(fillMode, &path);
4034 if (stat == Ok)
4036 stat = GdipAddPathPolygon(path, points, count);
4038 if (stat == Ok)
4039 stat = GdipFillPath(graphics, brush, path);
4041 GdipDeletePath(path);
4044 return stat;
4047 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4048 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4050 GpStatus stat;
4051 GpPath *path;
4053 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4055 if(!graphics || !brush || !points || !count)
4056 return InvalidParameter;
4058 if(graphics->busy)
4059 return ObjectBusy;
4061 stat = GdipCreatePath(fillMode, &path);
4063 if (stat == Ok)
4065 stat = GdipAddPathPolygonI(path, points, count);
4067 if (stat == Ok)
4068 stat = GdipFillPath(graphics, brush, path);
4070 GdipDeletePath(path);
4073 return stat;
4076 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4077 GDIPCONST GpPointF *points, INT count)
4079 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4081 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4084 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4085 GDIPCONST GpPoint *points, INT count)
4087 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4089 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4092 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4093 REAL x, REAL y, REAL width, REAL height)
4095 GpStatus stat;
4096 GpPath *path;
4098 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4100 if(!graphics || !brush)
4101 return InvalidParameter;
4103 if(graphics->busy)
4104 return ObjectBusy;
4106 stat = GdipCreatePath(FillModeAlternate, &path);
4108 if (stat == Ok)
4110 stat = GdipAddPathRectangle(path, x, y, width, height);
4112 if (stat == Ok)
4113 stat = GdipFillPath(graphics, brush, path);
4115 GdipDeletePath(path);
4118 return stat;
4121 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4122 INT x, INT y, INT width, INT height)
4124 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4126 return GdipFillRectangle(graphics, brush, x, y, width, height);
4129 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4130 INT count)
4132 GpStatus ret;
4133 INT i;
4135 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4137 if(!rects)
4138 return InvalidParameter;
4140 for(i = 0; i < count; i++){
4141 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4142 if(ret != Ok) return ret;
4145 return Ok;
4148 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4149 INT count)
4151 GpRectF *rectsF;
4152 GpStatus ret;
4153 INT i;
4155 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4157 if(!rects || count <= 0)
4158 return InvalidParameter;
4160 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4161 if(!rectsF)
4162 return OutOfMemory;
4164 for(i = 0; i < count; i++){
4165 rectsF[i].X = (REAL)rects[i].X;
4166 rectsF[i].Y = (REAL)rects[i].Y;
4167 rectsF[i].X = (REAL)rects[i].Width;
4168 rectsF[i].Height = (REAL)rects[i].Height;
4171 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4172 GdipFree(rectsF);
4174 return ret;
4177 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4178 GpRegion* region)
4180 INT save_state;
4181 GpStatus status;
4182 HRGN hrgn;
4183 RECT rc;
4185 if(!graphics->hdc || !brush_can_fill_path(brush))
4186 return NotImplemented;
4188 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4189 if(status != Ok)
4190 return status;
4192 save_state = SaveDC(graphics->hdc);
4193 EndPath(graphics->hdc);
4195 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4197 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4199 BeginPath(graphics->hdc);
4200 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4201 EndPath(graphics->hdc);
4203 brush_fill_path(graphics, brush);
4206 RestoreDC(graphics->hdc, save_state);
4208 DeleteObject(hrgn);
4210 return Ok;
4213 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4214 GpRegion* region)
4216 GpStatus stat;
4217 GpRegion *temp_region;
4218 GpMatrix *world_to_device;
4219 GpRectF graphics_bounds;
4220 DWORD *pixel_data;
4221 HRGN hregion;
4222 RECT bound_rect;
4223 GpRect gp_bound_rect;
4225 if (!brush_can_fill_pixels(brush))
4226 return NotImplemented;
4228 stat = get_graphics_bounds(graphics, &graphics_bounds);
4230 if (stat == Ok)
4231 stat = GdipCloneRegion(region, &temp_region);
4233 if (stat == Ok)
4235 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4236 CoordinateSpaceWorld, &world_to_device);
4238 if (stat == Ok)
4240 stat = GdipTransformRegion(temp_region, world_to_device);
4242 GdipDeleteMatrix(world_to_device);
4245 if (stat == Ok)
4246 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4248 if (stat == Ok)
4249 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4251 GdipDeleteRegion(temp_region);
4254 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4256 DeleteObject(hregion);
4257 return Ok;
4260 if (stat == Ok)
4262 gp_bound_rect.X = bound_rect.left;
4263 gp_bound_rect.Y = bound_rect.top;
4264 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4265 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4267 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4268 if (!pixel_data)
4269 stat = OutOfMemory;
4271 if (stat == Ok)
4273 stat = brush_fill_pixels(graphics, brush, pixel_data,
4274 &gp_bound_rect, gp_bound_rect.Width);
4276 if (stat == Ok)
4277 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4278 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4279 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4281 GdipFree(pixel_data);
4284 DeleteObject(hregion);
4287 return stat;
4290 /*****************************************************************************
4291 * GdipFillRegion [GDIPLUS.@]
4293 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4294 GpRegion* region)
4296 GpStatus stat = NotImplemented;
4298 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4300 if (!(graphics && brush && region))
4301 return InvalidParameter;
4303 if(graphics->busy)
4304 return ObjectBusy;
4306 if (!graphics->image)
4307 stat = GDI32_GdipFillRegion(graphics, brush, region);
4309 if (stat == NotImplemented)
4310 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4312 if (stat == NotImplemented)
4314 FIXME("not implemented for brushtype %i\n", brush->bt);
4315 stat = Ok;
4318 return stat;
4321 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4323 TRACE("(%p,%u)\n", graphics, intention);
4325 if(!graphics)
4326 return InvalidParameter;
4328 if(graphics->busy)
4329 return ObjectBusy;
4331 /* We have no internal operation queue, so there's no need to clear it. */
4333 if (graphics->hdc)
4334 GdiFlush();
4336 return Ok;
4339 /*****************************************************************************
4340 * GdipGetClipBounds [GDIPLUS.@]
4342 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4344 TRACE("(%p, %p)\n", graphics, rect);
4346 if(!graphics)
4347 return InvalidParameter;
4349 if(graphics->busy)
4350 return ObjectBusy;
4352 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4355 /*****************************************************************************
4356 * GdipGetClipBoundsI [GDIPLUS.@]
4358 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4360 TRACE("(%p, %p)\n", graphics, rect);
4362 if(!graphics)
4363 return InvalidParameter;
4365 if(graphics->busy)
4366 return ObjectBusy;
4368 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4371 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4372 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4373 CompositingMode *mode)
4375 TRACE("(%p, %p)\n", graphics, mode);
4377 if(!graphics || !mode)
4378 return InvalidParameter;
4380 if(graphics->busy)
4381 return ObjectBusy;
4383 *mode = graphics->compmode;
4385 return Ok;
4388 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4389 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4390 CompositingQuality *quality)
4392 TRACE("(%p, %p)\n", graphics, quality);
4394 if(!graphics || !quality)
4395 return InvalidParameter;
4397 if(graphics->busy)
4398 return ObjectBusy;
4400 *quality = graphics->compqual;
4402 return Ok;
4405 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4406 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4407 InterpolationMode *mode)
4409 TRACE("(%p, %p)\n", graphics, mode);
4411 if(!graphics || !mode)
4412 return InvalidParameter;
4414 if(graphics->busy)
4415 return ObjectBusy;
4417 *mode = graphics->interpolation;
4419 return Ok;
4422 /* FIXME: Need to handle color depths less than 24bpp */
4423 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4425 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4427 if(!graphics || !argb)
4428 return InvalidParameter;
4430 if(graphics->busy)
4431 return ObjectBusy;
4433 return Ok;
4436 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4438 TRACE("(%p, %p)\n", graphics, scale);
4440 if(!graphics || !scale)
4441 return InvalidParameter;
4443 if(graphics->busy)
4444 return ObjectBusy;
4446 *scale = graphics->scale;
4448 return Ok;
4451 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4453 TRACE("(%p, %p)\n", graphics, unit);
4455 if(!graphics || !unit)
4456 return InvalidParameter;
4458 if(graphics->busy)
4459 return ObjectBusy;
4461 *unit = graphics->unit;
4463 return Ok;
4466 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4467 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4468 *mode)
4470 TRACE("(%p, %p)\n", graphics, mode);
4472 if(!graphics || !mode)
4473 return InvalidParameter;
4475 if(graphics->busy)
4476 return ObjectBusy;
4478 *mode = graphics->pixeloffset;
4480 return Ok;
4483 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4484 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4486 TRACE("(%p, %p)\n", graphics, mode);
4488 if(!graphics || !mode)
4489 return InvalidParameter;
4491 if(graphics->busy)
4492 return ObjectBusy;
4494 *mode = graphics->smoothing;
4496 return Ok;
4499 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4501 TRACE("(%p, %p)\n", graphics, contrast);
4503 if(!graphics || !contrast)
4504 return InvalidParameter;
4506 *contrast = graphics->textcontrast;
4508 return Ok;
4511 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4512 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4513 TextRenderingHint *hint)
4515 TRACE("(%p, %p)\n", graphics, hint);
4517 if(!graphics || !hint)
4518 return InvalidParameter;
4520 if(graphics->busy)
4521 return ObjectBusy;
4523 *hint = graphics->texthint;
4525 return Ok;
4528 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4530 GpRegion *clip_rgn;
4531 GpStatus stat;
4533 TRACE("(%p, %p)\n", graphics, rect);
4535 if(!graphics || !rect)
4536 return InvalidParameter;
4538 if(graphics->busy)
4539 return ObjectBusy;
4541 /* intersect window and graphics clipping regions */
4542 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4543 return stat;
4545 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4546 goto cleanup;
4548 /* get bounds of the region */
4549 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4551 cleanup:
4552 GdipDeleteRegion(clip_rgn);
4554 return stat;
4557 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4559 GpRectF rectf;
4560 GpStatus stat;
4562 TRACE("(%p, %p)\n", graphics, rect);
4564 if(!graphics || !rect)
4565 return InvalidParameter;
4567 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4569 rect->X = gdip_round(rectf.X);
4570 rect->Y = gdip_round(rectf.Y);
4571 rect->Width = gdip_round(rectf.Width);
4572 rect->Height = gdip_round(rectf.Height);
4575 return stat;
4578 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4580 TRACE("(%p, %p)\n", graphics, matrix);
4582 if(!graphics || !matrix)
4583 return InvalidParameter;
4585 if(graphics->busy)
4586 return ObjectBusy;
4588 *matrix = *graphics->worldtrans;
4589 return Ok;
4592 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4594 GpSolidFill *brush;
4595 GpStatus stat;
4596 GpRectF wnd_rect;
4598 TRACE("(%p, %x)\n", graphics, color);
4600 if(!graphics)
4601 return InvalidParameter;
4603 if(graphics->busy)
4604 return ObjectBusy;
4606 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4607 return stat;
4609 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4610 GdipDeleteBrush((GpBrush*)brush);
4611 return stat;
4614 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4615 wnd_rect.Width, wnd_rect.Height);
4617 GdipDeleteBrush((GpBrush*)brush);
4619 return Ok;
4622 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4624 TRACE("(%p, %p)\n", graphics, res);
4626 if(!graphics || !res)
4627 return InvalidParameter;
4629 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4632 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4634 GpStatus stat;
4635 GpRegion* rgn;
4636 GpPointF pt;
4638 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4640 if(!graphics || !result)
4641 return InvalidParameter;
4643 if(graphics->busy)
4644 return ObjectBusy;
4646 pt.X = x;
4647 pt.Y = y;
4648 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4649 CoordinateSpaceWorld, &pt, 1)) != Ok)
4650 return stat;
4652 if((stat = GdipCreateRegion(&rgn)) != Ok)
4653 return stat;
4655 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4656 goto cleanup;
4658 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4660 cleanup:
4661 GdipDeleteRegion(rgn);
4662 return stat;
4665 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4667 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4670 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4672 GpStatus stat;
4673 GpRegion* rgn;
4674 GpPointF pts[2];
4676 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4678 if(!graphics || !result)
4679 return InvalidParameter;
4681 if(graphics->busy)
4682 return ObjectBusy;
4684 pts[0].X = x;
4685 pts[0].Y = y;
4686 pts[1].X = x + width;
4687 pts[1].Y = y + height;
4689 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4690 CoordinateSpaceWorld, pts, 2)) != Ok)
4691 return stat;
4693 pts[1].X -= pts[0].X;
4694 pts[1].Y -= pts[0].Y;
4696 if((stat = GdipCreateRegion(&rgn)) != Ok)
4697 return stat;
4699 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4700 goto cleanup;
4702 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4704 cleanup:
4705 GdipDeleteRegion(rgn);
4706 return stat;
4709 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4711 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4714 GpStatus gdip_format_string(HDC hdc,
4715 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4716 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4717 gdip_format_string_callback callback, void *user_data)
4719 WCHAR* stringdup;
4720 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4721 nheight, lineend, lineno = 0;
4722 RectF bounds;
4723 StringAlignment halign;
4724 GpStatus stat = Ok;
4725 SIZE size;
4726 HotkeyPrefix hkprefix;
4727 INT *hotkeyprefix_offsets=NULL;
4728 INT hotkeyprefix_count=0;
4729 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4730 int seen_prefix=0;
4732 if(length == -1) length = lstrlenW(string);
4734 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4735 if(!stringdup) return OutOfMemory;
4737 nwidth = rect->Width;
4738 nheight = rect->Height;
4740 if (format)
4741 hkprefix = format->hkprefix;
4742 else
4743 hkprefix = HotkeyPrefixNone;
4745 if (hkprefix == HotkeyPrefixShow)
4747 for (i=0; i<length; i++)
4749 if (string[i] == '&')
4750 hotkeyprefix_count++;
4754 if (hotkeyprefix_count)
4755 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4757 hotkeyprefix_count = 0;
4759 for(i = 0, j = 0; i < length; i++){
4760 /* FIXME: This makes the indexes passed to callback inaccurate. */
4761 if(!isprintW(string[i]) && (string[i] != '\n'))
4762 continue;
4764 /* FIXME: tabs should be handled using tabstops from stringformat */
4765 if (string[i] == '\t')
4766 continue;
4768 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4769 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4770 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4772 seen_prefix = 1;
4773 continue;
4776 seen_prefix = 0;
4778 stringdup[j] = string[i];
4779 j++;
4782 length = j;
4784 if (format) halign = format->align;
4785 else halign = StringAlignmentNear;
4787 while(sum < length){
4788 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4789 nwidth, &fit, NULL, &size);
4790 fitcpy = fit;
4792 if(fit == 0)
4793 break;
4795 for(lret = 0; lret < fit; lret++)
4796 if(*(stringdup + sum + lret) == '\n')
4797 break;
4799 /* Line break code (may look strange, but it imitates windows). */
4800 if(lret < fit)
4801 lineend = fit = lret; /* this is not an off-by-one error */
4802 else if(fit < (length - sum)){
4803 if(*(stringdup + sum + fit) == ' ')
4804 while(*(stringdup + sum + fit) == ' ')
4805 fit++;
4806 else
4807 while(*(stringdup + sum + fit - 1) != ' '){
4808 fit--;
4810 if(*(stringdup + sum + fit) == '\t')
4811 break;
4813 if(fit == 0){
4814 fit = fitcpy;
4815 break;
4818 lineend = fit;
4819 while(*(stringdup + sum + lineend - 1) == ' ' ||
4820 *(stringdup + sum + lineend - 1) == '\t')
4821 lineend--;
4823 else
4824 lineend = fit;
4826 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4827 nwidth, &j, NULL, &size);
4829 bounds.Width = size.cx;
4831 if(height + size.cy > nheight)
4832 bounds.Height = nheight - (height + size.cy);
4833 else
4834 bounds.Height = size.cy;
4836 bounds.Y = rect->Y + height;
4838 switch (halign)
4840 case StringAlignmentNear:
4841 default:
4842 bounds.X = rect->X;
4843 break;
4844 case StringAlignmentCenter:
4845 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4846 break;
4847 case StringAlignmentFar:
4848 bounds.X = rect->X + rect->Width - bounds.Width;
4849 break;
4852 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4853 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4854 break;
4856 stat = callback(hdc, stringdup, sum, lineend,
4857 font, rect, format, lineno, &bounds,
4858 &hotkeyprefix_offsets[hotkeyprefix_pos],
4859 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4861 if (stat != Ok)
4862 break;
4864 sum += fit + (lret < fitcpy ? 1 : 0);
4865 height += size.cy;
4866 lineno++;
4868 hotkeyprefix_pos = hotkeyprefix_end_pos;
4870 if(height > nheight)
4871 break;
4873 /* Stop if this was a linewrap (but not if it was a linebreak). */
4874 if ((lret == fitcpy) && format &&
4875 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4876 break;
4879 GdipFree(stringdup);
4880 GdipFree(hotkeyprefix_offsets);
4882 return stat;
4885 struct measure_ranges_args {
4886 GpRegion **regions;
4887 REAL rel_width, rel_height;
4890 static GpStatus measure_ranges_callback(HDC hdc,
4891 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4892 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4893 INT lineno, const RectF *bounds, INT *underlined_indexes,
4894 INT underlined_index_count, void *user_data)
4896 int i;
4897 GpStatus stat = Ok;
4898 struct measure_ranges_args *args = user_data;
4900 for (i=0; i<format->range_count; i++)
4902 INT range_start = max(index, format->character_ranges[i].First);
4903 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4904 if (range_start < range_end)
4906 GpRectF range_rect;
4907 SIZE range_size;
4909 range_rect.Y = bounds->Y / args->rel_height;
4910 range_rect.Height = bounds->Height / args->rel_height;
4912 GetTextExtentExPointW(hdc, string + index, range_start - index,
4913 INT_MAX, NULL, NULL, &range_size);
4914 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4916 GetTextExtentExPointW(hdc, string + index, range_end - index,
4917 INT_MAX, NULL, NULL, &range_size);
4918 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4920 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4921 if (stat != Ok)
4922 break;
4926 return stat;
4929 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4930 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4931 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4932 INT regionCount, GpRegion** regions)
4934 GpStatus stat;
4935 int i;
4936 HFONT gdifont, oldfont;
4937 struct measure_ranges_args args;
4938 HDC hdc, temp_hdc=NULL;
4939 GpPointF pt[3];
4940 RectF scaled_rect;
4941 REAL margin_x;
4943 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4944 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4946 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4947 return InvalidParameter;
4949 if (regionCount < stringFormat->range_count)
4950 return InvalidParameter;
4952 if(!graphics->hdc)
4954 hdc = temp_hdc = CreateCompatibleDC(0);
4955 if (!temp_hdc) return OutOfMemory;
4957 else
4958 hdc = graphics->hdc;
4960 if (stringFormat->attr)
4961 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4963 pt[0].X = 0.0;
4964 pt[0].Y = 0.0;
4965 pt[1].X = 1.0;
4966 pt[1].Y = 0.0;
4967 pt[2].X = 0.0;
4968 pt[2].Y = 1.0;
4969 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4970 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4971 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4972 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4973 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4975 /* FIXME: GenericTypographic format prevents extra margins */
4976 margin_x = units_scale(font->unit, graphics->unit, graphics->xres) * font->emSize / 6.0;
4978 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4979 scaled_rect.Y = layoutRect->Y * args.rel_height;
4980 if (stringFormat->attr & StringFormatFlagsNoClip)
4982 scaled_rect.Width = (REAL)(1 << 23);
4983 scaled_rect.Height = (REAL)(1 << 23);
4985 else
4987 scaled_rect.Width = layoutRect->Width * args.rel_width;
4988 scaled_rect.Height = layoutRect->Height * args.rel_height;
4990 if (scaled_rect.Width >= 0.5)
4992 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4993 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4996 get_font_hfont(graphics, font, &gdifont);
4997 oldfont = SelectObject(hdc, gdifont);
4999 for (i=0; i<stringFormat->range_count; i++)
5001 stat = GdipSetEmpty(regions[i]);
5002 if (stat != Ok)
5003 return stat;
5006 args.regions = regions;
5008 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5009 measure_ranges_callback, &args);
5011 SelectObject(hdc, oldfont);
5012 DeleteObject(gdifont);
5014 if (temp_hdc)
5015 DeleteDC(temp_hdc);
5017 return stat;
5020 struct measure_string_args {
5021 RectF *bounds;
5022 INT *codepointsfitted;
5023 INT *linesfilled;
5024 REAL rel_width, rel_height;
5027 static GpStatus measure_string_callback(HDC hdc,
5028 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5029 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5030 INT lineno, const RectF *bounds, INT *underlined_indexes,
5031 INT underlined_index_count, void *user_data)
5033 struct measure_string_args *args = user_data;
5034 REAL new_width, new_height;
5036 new_width = bounds->Width / args->rel_width;
5037 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5039 if (new_width > args->bounds->Width)
5040 args->bounds->Width = new_width;
5042 if (new_height > args->bounds->Height)
5043 args->bounds->Height = new_height;
5045 if (args->codepointsfitted)
5046 *args->codepointsfitted = index + length;
5048 if (args->linesfilled)
5049 (*args->linesfilled)++;
5051 return Ok;
5054 /* Find the smallest rectangle that bounds the text when it is printed in rect
5055 * according to the format options listed in format. If rect has 0 width and
5056 * height, then just find the smallest rectangle that bounds the text when it's
5057 * printed at location (rect->X, rect-Y). */
5058 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5059 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5060 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5061 INT *codepointsfitted, INT *linesfilled)
5063 HFONT oldfont, gdifont;
5064 struct measure_string_args args;
5065 HDC temp_hdc=NULL, hdc;
5066 GpPointF pt[3];
5067 RectF scaled_rect;
5068 REAL margin_x;
5069 INT lines, glyphs, format_flags = format ? format->attr : 0;
5071 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5072 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5073 bounds, codepointsfitted, linesfilled);
5075 if(!graphics || !string || !font || !rect || !bounds)
5076 return InvalidParameter;
5078 if(!graphics->hdc)
5080 hdc = temp_hdc = CreateCompatibleDC(0);
5081 if (!temp_hdc) return OutOfMemory;
5083 else
5084 hdc = graphics->hdc;
5086 if(linesfilled) *linesfilled = 0;
5087 if(codepointsfitted) *codepointsfitted = 0;
5089 if(format)
5090 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5092 pt[0].X = 0.0;
5093 pt[0].Y = 0.0;
5094 pt[1].X = 1.0;
5095 pt[1].Y = 0.0;
5096 pt[2].X = 0.0;
5097 pt[2].Y = 1.0;
5098 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5099 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5100 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5101 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5102 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5104 /* FIXME: GenericTypographic format prevents extra margins */
5105 margin_x = units_scale(font->unit, graphics->unit, graphics->xres) * font->emSize / 6.0;
5107 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5108 scaled_rect.Y = rect->Y * args.rel_height;
5109 scaled_rect.Width = rect->Width * args.rel_width;
5110 scaled_rect.Height = rect->Height * args.rel_height;
5112 if ((format_flags & StringFormatFlagsNoClip) ||
5113 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5114 if ((format_flags & StringFormatFlagsNoClip) ||
5115 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5117 if (scaled_rect.Width >= 0.5)
5119 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5120 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5123 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5124 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5126 get_font_hfont(graphics, font, &gdifont);
5127 oldfont = SelectObject(hdc, gdifont);
5129 bounds->X = rect->X;
5130 bounds->Y = rect->Y;
5131 bounds->Width = 0.0;
5132 bounds->Height = 0.0;
5134 args.bounds = bounds;
5135 args.codepointsfitted = &glyphs;
5136 args.linesfilled = &lines;
5137 lines = glyphs = 0;
5139 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5140 measure_string_callback, &args);
5142 if (linesfilled) *linesfilled = lines;
5143 if (codepointsfitted) *codepointsfitted = glyphs;
5145 if (lines)
5146 bounds->Width += margin_x * 2.0;
5148 SelectObject(hdc, oldfont);
5149 DeleteObject(gdifont);
5151 if (temp_hdc)
5152 DeleteDC(temp_hdc);
5154 return Ok;
5157 struct draw_string_args {
5158 GpGraphics *graphics;
5159 GDIPCONST GpBrush *brush;
5160 REAL x, y, rel_width, rel_height, ascent;
5163 static GpStatus draw_string_callback(HDC hdc,
5164 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5165 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5166 INT lineno, const RectF *bounds, INT *underlined_indexes,
5167 INT underlined_index_count, void *user_data)
5169 struct draw_string_args *args = user_data;
5170 PointF position;
5171 GpStatus stat;
5173 position.X = args->x + bounds->X / args->rel_width;
5174 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5176 stat = GdipDrawDriverString(args->graphics, &string[index], length, font,
5177 args->brush, &position,
5178 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5180 if (stat == Ok && underlined_index_count)
5182 OUTLINETEXTMETRICW otm;
5183 REAL underline_y, underline_height;
5184 int i;
5186 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5188 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5189 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5191 for (i=0; i<underlined_index_count; i++)
5193 REAL start_x, end_x;
5194 SIZE text_size;
5195 INT ofs = underlined_indexes[i] - index;
5197 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5198 start_x = text_size.cx / args->rel_width;
5200 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5201 end_x = text_size.cx / args->rel_width;
5203 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5207 return stat;
5210 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5211 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5212 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5214 HRGN rgn = NULL;
5215 HFONT gdifont;
5216 GpPointF pt[3], rectcpy[4];
5217 POINT corners[4];
5218 REAL rel_width, rel_height, margin_x;
5219 INT save_state, format_flags = 0;
5220 REAL offsety = 0.0;
5221 struct draw_string_args args;
5222 RectF scaled_rect;
5223 HDC hdc, temp_hdc=NULL;
5224 TEXTMETRICW textmetric;
5226 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5227 length, font, debugstr_rectf(rect), format, brush);
5229 if(!graphics || !string || !font || !brush || !rect)
5230 return InvalidParameter;
5232 if(graphics->hdc)
5234 hdc = graphics->hdc;
5236 else
5238 hdc = temp_hdc = CreateCompatibleDC(0);
5241 if(format){
5242 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5244 format_flags = format->attr;
5246 /* Should be no need to explicitly test for StringAlignmentNear as
5247 * that is default behavior if no alignment is passed. */
5248 if(format->vertalign != StringAlignmentNear){
5249 RectF bounds, in_rect = *rect;
5250 in_rect.Height = 0.0; /* avoid height clipping */
5251 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5253 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5255 if(format->vertalign == StringAlignmentCenter)
5256 offsety = (rect->Height - bounds.Height) / 2;
5257 else if(format->vertalign == StringAlignmentFar)
5258 offsety = (rect->Height - bounds.Height);
5260 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5263 save_state = SaveDC(hdc);
5265 pt[0].X = 0.0;
5266 pt[0].Y = 0.0;
5267 pt[1].X = 1.0;
5268 pt[1].Y = 0.0;
5269 pt[2].X = 0.0;
5270 pt[2].Y = 1.0;
5271 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5272 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5273 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5274 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5275 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5277 rectcpy[3].X = rectcpy[0].X = rect->X;
5278 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5279 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5280 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5281 transform_and_round_points(graphics, corners, rectcpy, 4);
5283 /* FIXME: GenericTypographic format prevents extra margins */
5284 margin_x = units_scale(font->unit, graphics->unit, graphics->xres) * font->emSize / 6.0;
5286 scaled_rect.X = margin_x * rel_width;
5287 scaled_rect.Y = 0.0;
5288 scaled_rect.Width = rel_width * rect->Width;
5289 scaled_rect.Height = rel_height * rect->Height;
5291 if ((format_flags & StringFormatFlagsNoClip) ||
5292 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5293 if ((format_flags & StringFormatFlagsNoClip) ||
5294 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5296 if (scaled_rect.Width >= 0.5)
5298 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5299 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5302 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5303 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5305 if (!(format_flags & StringFormatFlagsNoClip) &&
5306 gdip_round(scaled_rect.Width) != 0 && gdip_round(scaled_rect.Height) != 0)
5308 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5309 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5310 SelectClipRgn(hdc, rgn);
5313 get_font_hfont(graphics, font, &gdifont);
5314 SelectObject(hdc, gdifont);
5316 args.graphics = graphics;
5317 args.brush = brush;
5319 args.x = rect->X;
5320 args.y = rect->Y + offsety;
5322 args.rel_width = rel_width;
5323 args.rel_height = rel_height;
5325 GetTextMetricsW(hdc, &textmetric);
5326 args.ascent = textmetric.tmAscent / rel_height;
5328 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5329 draw_string_callback, &args);
5331 DeleteObject(rgn);
5332 DeleteObject(gdifont);
5334 RestoreDC(hdc, save_state);
5336 DeleteDC(temp_hdc);
5338 return Ok;
5341 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5343 TRACE("(%p)\n", graphics);
5345 if(!graphics)
5346 return InvalidParameter;
5348 if(graphics->busy)
5349 return ObjectBusy;
5351 return GdipSetInfinite(graphics->clip);
5354 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5356 TRACE("(%p)\n", graphics);
5358 if(!graphics)
5359 return InvalidParameter;
5361 if(graphics->busy)
5362 return ObjectBusy;
5364 graphics->worldtrans->matrix[0] = 1.0;
5365 graphics->worldtrans->matrix[1] = 0.0;
5366 graphics->worldtrans->matrix[2] = 0.0;
5367 graphics->worldtrans->matrix[3] = 1.0;
5368 graphics->worldtrans->matrix[4] = 0.0;
5369 graphics->worldtrans->matrix[5] = 0.0;
5371 return Ok;
5374 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5376 return GdipEndContainer(graphics, state);
5379 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5380 GpMatrixOrder order)
5382 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5384 if(!graphics)
5385 return InvalidParameter;
5387 if(graphics->busy)
5388 return ObjectBusy;
5390 return GdipRotateMatrix(graphics->worldtrans, angle, order);
5393 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5395 return GdipBeginContainer2(graphics, state);
5398 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5399 GraphicsContainer *state)
5401 GraphicsContainerItem *container;
5402 GpStatus sts;
5404 TRACE("(%p, %p)\n", graphics, state);
5406 if(!graphics || !state)
5407 return InvalidParameter;
5409 sts = init_container(&container, graphics);
5410 if(sts != Ok)
5411 return sts;
5413 list_add_head(&graphics->containers, &container->entry);
5414 *state = graphics->contid = container->contid;
5416 return Ok;
5419 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5421 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5422 return NotImplemented;
5425 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5427 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5428 return NotImplemented;
5431 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5433 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5434 return NotImplemented;
5437 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5439 GpStatus sts;
5440 GraphicsContainerItem *container, *container2;
5442 TRACE("(%p, %x)\n", graphics, state);
5444 if(!graphics)
5445 return InvalidParameter;
5447 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5448 if(container->contid == state)
5449 break;
5452 /* did not find a matching container */
5453 if(&container->entry == &graphics->containers)
5454 return Ok;
5456 sts = restore_container(graphics, container);
5457 if(sts != Ok)
5458 return sts;
5460 /* remove all of the containers on top of the found container */
5461 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5462 if(container->contid == state)
5463 break;
5464 list_remove(&container->entry);
5465 delete_container(container);
5468 list_remove(&container->entry);
5469 delete_container(container);
5471 return Ok;
5474 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5475 REAL sy, GpMatrixOrder order)
5477 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5479 if(!graphics)
5480 return InvalidParameter;
5482 if(graphics->busy)
5483 return ObjectBusy;
5485 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
5488 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5489 CombineMode mode)
5491 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5493 if(!graphics || !srcgraphics)
5494 return InvalidParameter;
5496 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5499 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5500 CompositingMode mode)
5502 TRACE("(%p, %d)\n", graphics, mode);
5504 if(!graphics)
5505 return InvalidParameter;
5507 if(graphics->busy)
5508 return ObjectBusy;
5510 graphics->compmode = mode;
5512 return Ok;
5515 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5516 CompositingQuality quality)
5518 TRACE("(%p, %d)\n", graphics, quality);
5520 if(!graphics)
5521 return InvalidParameter;
5523 if(graphics->busy)
5524 return ObjectBusy;
5526 graphics->compqual = quality;
5528 return Ok;
5531 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5532 InterpolationMode mode)
5534 TRACE("(%p, %d)\n", graphics, mode);
5536 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5537 return InvalidParameter;
5539 if(graphics->busy)
5540 return ObjectBusy;
5542 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5543 mode = InterpolationModeBilinear;
5545 if (mode == InterpolationModeHighQuality)
5546 mode = InterpolationModeHighQualityBicubic;
5548 graphics->interpolation = mode;
5550 return Ok;
5553 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5555 TRACE("(%p, %.2f)\n", graphics, scale);
5557 if(!graphics || (scale <= 0.0))
5558 return InvalidParameter;
5560 if(graphics->busy)
5561 return ObjectBusy;
5563 graphics->scale = scale;
5565 return Ok;
5568 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5570 TRACE("(%p, %d)\n", graphics, unit);
5572 if(!graphics)
5573 return InvalidParameter;
5575 if(graphics->busy)
5576 return ObjectBusy;
5578 if(unit == UnitWorld)
5579 return InvalidParameter;
5581 graphics->unit = unit;
5583 return Ok;
5586 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5587 mode)
5589 TRACE("(%p, %d)\n", graphics, mode);
5591 if(!graphics)
5592 return InvalidParameter;
5594 if(graphics->busy)
5595 return ObjectBusy;
5597 graphics->pixeloffset = mode;
5599 return Ok;
5602 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5604 static int calls;
5606 TRACE("(%p,%i,%i)\n", graphics, x, y);
5608 if (!(calls++))
5609 FIXME("value is unused in rendering\n");
5611 if (!graphics)
5612 return InvalidParameter;
5614 graphics->origin_x = x;
5615 graphics->origin_y = y;
5617 return Ok;
5620 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5622 TRACE("(%p,%p,%p)\n", graphics, x, y);
5624 if (!graphics || !x || !y)
5625 return InvalidParameter;
5627 *x = graphics->origin_x;
5628 *y = graphics->origin_y;
5630 return Ok;
5633 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5635 TRACE("(%p, %d)\n", graphics, mode);
5637 if(!graphics)
5638 return InvalidParameter;
5640 if(graphics->busy)
5641 return ObjectBusy;
5643 graphics->smoothing = mode;
5645 return Ok;
5648 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5650 TRACE("(%p, %d)\n", graphics, contrast);
5652 if(!graphics)
5653 return InvalidParameter;
5655 graphics->textcontrast = contrast;
5657 return Ok;
5660 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5661 TextRenderingHint hint)
5663 TRACE("(%p, %d)\n", graphics, hint);
5665 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5666 return InvalidParameter;
5668 if(graphics->busy)
5669 return ObjectBusy;
5671 graphics->texthint = hint;
5673 return Ok;
5676 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5678 TRACE("(%p, %p)\n", graphics, matrix);
5680 if(!graphics || !matrix)
5681 return InvalidParameter;
5683 if(graphics->busy)
5684 return ObjectBusy;
5686 TRACE("%f,%f,%f,%f,%f,%f\n",
5687 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5688 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5690 GdipDeleteMatrix(graphics->worldtrans);
5691 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5694 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5695 REAL dy, GpMatrixOrder order)
5697 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5699 if(!graphics)
5700 return InvalidParameter;
5702 if(graphics->busy)
5703 return ObjectBusy;
5705 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5708 /*****************************************************************************
5709 * GdipSetClipHrgn [GDIPLUS.@]
5711 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5713 GpRegion *region;
5714 GpStatus status;
5716 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5718 if(!graphics)
5719 return InvalidParameter;
5721 status = GdipCreateRegionHrgn(hrgn, &region);
5722 if(status != Ok)
5723 return status;
5725 status = GdipSetClipRegion(graphics, region, mode);
5727 GdipDeleteRegion(region);
5728 return status;
5731 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5733 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5735 if(!graphics)
5736 return InvalidParameter;
5738 if(graphics->busy)
5739 return ObjectBusy;
5741 return GdipCombineRegionPath(graphics->clip, path, mode);
5744 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5745 REAL width, REAL height,
5746 CombineMode mode)
5748 GpRectF rect;
5750 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5752 if(!graphics)
5753 return InvalidParameter;
5755 if(graphics->busy)
5756 return ObjectBusy;
5758 rect.X = x;
5759 rect.Y = y;
5760 rect.Width = width;
5761 rect.Height = height;
5763 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5766 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5767 INT width, INT height,
5768 CombineMode mode)
5770 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5772 if(!graphics)
5773 return InvalidParameter;
5775 if(graphics->busy)
5776 return ObjectBusy;
5778 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5781 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5782 CombineMode mode)
5784 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5786 if(!graphics || !region)
5787 return InvalidParameter;
5789 if(graphics->busy)
5790 return ObjectBusy;
5792 return GdipCombineRegionRegion(graphics->clip, region, mode);
5795 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5796 UINT limitDpi)
5798 static int calls;
5800 TRACE("(%p,%u)\n", metafile, limitDpi);
5802 if(!(calls++))
5803 FIXME("not implemented\n");
5805 return NotImplemented;
5808 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5809 INT count)
5811 INT save_state;
5812 POINT *pti;
5814 TRACE("(%p, %p, %d)\n", graphics, points, count);
5816 if(!graphics || !pen || count<=0)
5817 return InvalidParameter;
5819 if(graphics->busy)
5820 return ObjectBusy;
5822 if (!graphics->hdc)
5824 FIXME("graphics object has no HDC\n");
5825 return Ok;
5828 pti = GdipAlloc(sizeof(POINT) * count);
5830 save_state = prepare_dc(graphics, pen);
5831 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5833 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5834 Polygon(graphics->hdc, pti, count);
5836 restore_dc(graphics, save_state);
5837 GdipFree(pti);
5839 return Ok;
5842 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5843 INT count)
5845 GpStatus ret;
5846 GpPointF *ptf;
5847 INT i;
5849 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5851 if(count<=0) return InvalidParameter;
5852 ptf = GdipAlloc(sizeof(GpPointF) * count);
5854 for(i = 0;i < count; i++){
5855 ptf[i].X = (REAL)points[i].X;
5856 ptf[i].Y = (REAL)points[i].Y;
5859 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5860 GdipFree(ptf);
5862 return ret;
5865 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5867 TRACE("(%p, %p)\n", graphics, dpi);
5869 if(!graphics || !dpi)
5870 return InvalidParameter;
5872 if(graphics->busy)
5873 return ObjectBusy;
5875 *dpi = graphics->xres;
5876 return Ok;
5879 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5881 TRACE("(%p, %p)\n", graphics, dpi);
5883 if(!graphics || !dpi)
5884 return InvalidParameter;
5886 if(graphics->busy)
5887 return ObjectBusy;
5889 *dpi = graphics->yres;
5890 return Ok;
5893 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5894 GpMatrixOrder order)
5896 GpMatrix m;
5897 GpStatus ret;
5899 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5901 if(!graphics || !matrix)
5902 return InvalidParameter;
5904 if(graphics->busy)
5905 return ObjectBusy;
5907 m = *(graphics->worldtrans);
5909 ret = GdipMultiplyMatrix(&m, matrix, order);
5910 if(ret == Ok)
5911 *(graphics->worldtrans) = m;
5913 return ret;
5916 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5917 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5919 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5921 GpStatus stat=Ok;
5923 TRACE("(%p, %p)\n", graphics, hdc);
5925 if(!graphics || !hdc)
5926 return InvalidParameter;
5928 if(graphics->busy)
5929 return ObjectBusy;
5931 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5933 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5935 else if (!graphics->hdc ||
5936 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5938 /* Create a fake HDC and fill it with a constant color. */
5939 HDC temp_hdc;
5940 HBITMAP hbitmap;
5941 GpRectF bounds;
5942 BITMAPINFOHEADER bmih;
5943 int i;
5945 stat = get_graphics_bounds(graphics, &bounds);
5946 if (stat != Ok)
5947 return stat;
5949 graphics->temp_hbitmap_width = bounds.Width;
5950 graphics->temp_hbitmap_height = bounds.Height;
5952 bmih.biSize = sizeof(bmih);
5953 bmih.biWidth = graphics->temp_hbitmap_width;
5954 bmih.biHeight = -graphics->temp_hbitmap_height;
5955 bmih.biPlanes = 1;
5956 bmih.biBitCount = 32;
5957 bmih.biCompression = BI_RGB;
5958 bmih.biSizeImage = 0;
5959 bmih.biXPelsPerMeter = 0;
5960 bmih.biYPelsPerMeter = 0;
5961 bmih.biClrUsed = 0;
5962 bmih.biClrImportant = 0;
5964 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5965 (void**)&graphics->temp_bits, NULL, 0);
5966 if (!hbitmap)
5967 return GenericError;
5969 temp_hdc = CreateCompatibleDC(0);
5970 if (!temp_hdc)
5972 DeleteObject(hbitmap);
5973 return GenericError;
5976 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5977 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5979 SelectObject(temp_hdc, hbitmap);
5981 graphics->temp_hbitmap = hbitmap;
5982 *hdc = graphics->temp_hdc = temp_hdc;
5984 else
5986 *hdc = graphics->hdc;
5989 if (stat == Ok)
5990 graphics->busy = TRUE;
5992 return stat;
5995 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5997 GpStatus stat=Ok;
5999 TRACE("(%p, %p)\n", graphics, hdc);
6001 if(!graphics || !hdc || !graphics->busy)
6002 return InvalidParameter;
6004 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6006 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6008 else if (graphics->temp_hdc == hdc)
6010 DWORD* pos;
6011 int i;
6013 /* Find the pixels that have changed, and mark them as opaque. */
6014 pos = (DWORD*)graphics->temp_bits;
6015 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6017 if (*pos != DC_BACKGROUND_KEY)
6019 *pos |= 0xff000000;
6021 pos++;
6024 /* Write the changed pixels to the real target. */
6025 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6026 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6027 graphics->temp_hbitmap_width * 4);
6029 /* Clean up. */
6030 DeleteDC(graphics->temp_hdc);
6031 DeleteObject(graphics->temp_hbitmap);
6032 graphics->temp_hdc = NULL;
6033 graphics->temp_hbitmap = NULL;
6035 else if (hdc != graphics->hdc)
6037 stat = InvalidParameter;
6040 if (stat == Ok)
6041 graphics->busy = FALSE;
6043 return stat;
6046 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6048 GpRegion *clip;
6049 GpStatus status;
6051 TRACE("(%p, %p)\n", graphics, region);
6053 if(!graphics || !region)
6054 return InvalidParameter;
6056 if(graphics->busy)
6057 return ObjectBusy;
6059 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6060 return status;
6062 /* free everything except root node and header */
6063 delete_element(&region->node);
6064 memcpy(region, clip, sizeof(GpRegion));
6065 GdipFree(clip);
6067 return Ok;
6070 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6071 GpCoordinateSpace src_space, GpMatrix **matrix)
6073 GpStatus stat = GdipCreateMatrix(matrix);
6074 REAL scale_x, scale_y;
6076 if (dst_space != src_space && stat == Ok)
6078 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6079 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6081 if(graphics->unit != UnitDisplay)
6083 scale_x *= graphics->scale;
6084 scale_y *= graphics->scale;
6087 /* transform from src_space to CoordinateSpacePage */
6088 switch (src_space)
6090 case CoordinateSpaceWorld:
6091 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
6092 break;
6093 case CoordinateSpacePage:
6094 break;
6095 case CoordinateSpaceDevice:
6096 GdipScaleMatrix(*matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6097 break;
6100 /* transform from CoordinateSpacePage to dst_space */
6101 switch (dst_space)
6103 case CoordinateSpaceWorld:
6105 GpMatrix *inverted_transform;
6106 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
6107 if (stat == Ok)
6109 stat = GdipInvertMatrix(inverted_transform);
6110 if (stat == Ok)
6111 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
6112 GdipDeleteMatrix(inverted_transform);
6114 break;
6116 case CoordinateSpacePage:
6117 break;
6118 case CoordinateSpaceDevice:
6119 GdipScaleMatrix(*matrix, scale_x, scale_y, MatrixOrderAppend);
6120 break;
6123 return stat;
6126 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6127 GpCoordinateSpace src_space, GpPointF *points, INT count)
6129 GpMatrix *matrix;
6130 GpStatus stat;
6132 if(!graphics || !points || count <= 0)
6133 return InvalidParameter;
6135 if(graphics->busy)
6136 return ObjectBusy;
6138 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6140 if (src_space == dst_space) return Ok;
6142 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6144 if (stat == Ok)
6146 stat = GdipTransformMatrixPoints(matrix, points, count);
6148 GdipDeleteMatrix(matrix);
6151 return stat;
6154 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6155 GpCoordinateSpace src_space, GpPoint *points, INT count)
6157 GpPointF *pointsF;
6158 GpStatus ret;
6159 INT i;
6161 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6163 if(count <= 0)
6164 return InvalidParameter;
6166 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6167 if(!pointsF)
6168 return OutOfMemory;
6170 for(i = 0; i < count; i++){
6171 pointsF[i].X = (REAL)points[i].X;
6172 pointsF[i].Y = (REAL)points[i].Y;
6175 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6177 if(ret == Ok)
6178 for(i = 0; i < count; i++){
6179 points[i].X = gdip_round(pointsF[i].X);
6180 points[i].Y = gdip_round(pointsF[i].Y);
6182 GdipFree(pointsF);
6184 return ret;
6187 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6189 static int calls;
6191 TRACE("\n");
6193 if (!calls++)
6194 FIXME("stub\n");
6196 return NULL;
6199 /*****************************************************************************
6200 * GdipTranslateClip [GDIPLUS.@]
6202 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6204 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6206 if(!graphics)
6207 return InvalidParameter;
6209 if(graphics->busy)
6210 return ObjectBusy;
6212 return GdipTranslateRegion(graphics->clip, dx, dy);
6215 /*****************************************************************************
6216 * GdipTranslateClipI [GDIPLUS.@]
6218 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6220 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6222 if(!graphics)
6223 return InvalidParameter;
6225 if(graphics->busy)
6226 return ObjectBusy;
6228 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6232 /*****************************************************************************
6233 * GdipMeasureDriverString [GDIPLUS.@]
6235 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6236 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6237 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6239 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6240 HFONT hfont;
6241 HDC hdc;
6242 REAL min_x, min_y, max_x, max_y, x, y;
6243 int i;
6244 TEXTMETRICW textmetric;
6245 const WORD *glyph_indices;
6246 WORD *dynamic_glyph_indices=NULL;
6247 REAL rel_width, rel_height, ascent, descent;
6248 GpPointF pt[3];
6250 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6252 if (!graphics || !text || !font || !positions || !boundingBox)
6253 return InvalidParameter;
6255 if (length == -1)
6256 length = strlenW(text);
6258 if (length == 0)
6260 boundingBox->X = 0.0;
6261 boundingBox->Y = 0.0;
6262 boundingBox->Width = 0.0;
6263 boundingBox->Height = 0.0;
6266 if (flags & unsupported_flags)
6267 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6269 if (matrix)
6270 FIXME("Ignoring matrix\n");
6272 get_font_hfont(graphics, font, &hfont);
6274 hdc = CreateCompatibleDC(0);
6275 SelectObject(hdc, hfont);
6277 GetTextMetricsW(hdc, &textmetric);
6279 pt[0].X = 0.0;
6280 pt[0].Y = 0.0;
6281 pt[1].X = 1.0;
6282 pt[1].Y = 0.0;
6283 pt[2].X = 0.0;
6284 pt[2].Y = 1.0;
6285 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6286 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6287 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6288 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6289 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6291 if (flags & DriverStringOptionsCmapLookup)
6293 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6294 if (!glyph_indices)
6296 DeleteDC(hdc);
6297 DeleteObject(hfont);
6298 return OutOfMemory;
6301 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6303 else
6304 glyph_indices = text;
6306 min_x = max_x = x = positions[0].X;
6307 min_y = max_y = y = positions[0].Y;
6309 ascent = textmetric.tmAscent / rel_height;
6310 descent = textmetric.tmDescent / rel_height;
6312 for (i=0; i<length; i++)
6314 int char_width;
6315 ABC abc;
6317 if (!(flags & DriverStringOptionsRealizedAdvance))
6319 x = positions[i].X;
6320 y = positions[i].Y;
6323 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6324 char_width = abc.abcA + abc.abcB + abc.abcB;
6326 if (min_y > y - ascent) min_y = y - ascent;
6327 if (max_y < y + descent) max_y = y + descent;
6328 if (min_x > x) min_x = x;
6330 x += char_width / rel_width;
6332 if (max_x < x) max_x = x;
6335 GdipFree(dynamic_glyph_indices);
6336 DeleteDC(hdc);
6337 DeleteObject(hfont);
6339 boundingBox->X = min_x;
6340 boundingBox->Y = min_y;
6341 boundingBox->Width = max_x - min_x;
6342 boundingBox->Height = max_y - min_y;
6344 return Ok;
6347 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6348 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6349 GDIPCONST PointF *positions, INT flags,
6350 GDIPCONST GpMatrix *matrix )
6352 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6353 INT save_state;
6354 GpPointF pt;
6355 HFONT hfont;
6356 UINT eto_flags=0;
6358 if (flags & unsupported_flags)
6359 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6361 if (matrix)
6362 FIXME("Ignoring matrix\n");
6364 if (!(flags & DriverStringOptionsCmapLookup))
6365 eto_flags |= ETO_GLYPH_INDEX;
6367 save_state = SaveDC(graphics->hdc);
6368 SetBkMode(graphics->hdc, TRANSPARENT);
6369 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6371 pt = positions[0];
6372 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6374 get_font_hfont(graphics, font, &hfont);
6375 SelectObject(graphics->hdc, hfont);
6377 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6379 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6381 RestoreDC(graphics->hdc, save_state);
6383 DeleteObject(hfont);
6385 return Ok;
6388 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6389 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6390 GDIPCONST PointF *positions, INT flags,
6391 GDIPCONST GpMatrix *matrix )
6393 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6394 GpStatus stat;
6395 PointF *real_positions, real_position;
6396 POINT *pti;
6397 HFONT hfont;
6398 HDC hdc;
6399 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6400 DWORD max_glyphsize=0;
6401 GLYPHMETRICS glyphmetrics;
6402 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6403 BYTE *glyph_mask;
6404 BYTE *text_mask;
6405 int text_mask_stride;
6406 BYTE *pixel_data;
6407 int pixel_data_stride;
6408 GpRect pixel_area;
6409 UINT ggo_flags = GGO_GRAY8_BITMAP;
6411 if (length <= 0)
6412 return Ok;
6414 if (!(flags & DriverStringOptionsCmapLookup))
6415 ggo_flags |= GGO_GLYPH_INDEX;
6417 if (flags & unsupported_flags)
6418 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6420 if (matrix)
6421 FIXME("Ignoring matrix\n");
6423 pti = GdipAlloc(sizeof(POINT) * length);
6424 if (!pti)
6425 return OutOfMemory;
6427 if (flags & DriverStringOptionsRealizedAdvance)
6429 real_position = positions[0];
6431 transform_and_round_points(graphics, pti, &real_position, 1);
6433 else
6435 real_positions = GdipAlloc(sizeof(PointF) * length);
6436 if (!real_positions)
6438 GdipFree(pti);
6439 return OutOfMemory;
6442 memcpy(real_positions, positions, sizeof(PointF) * length);
6444 transform_and_round_points(graphics, pti, real_positions, length);
6446 GdipFree(real_positions);
6449 get_font_hfont(graphics, font, &hfont);
6451 hdc = CreateCompatibleDC(0);
6452 SelectObject(hdc, hfont);
6454 /* Get the boundaries of the text to be drawn */
6455 for (i=0; i<length; i++)
6457 DWORD glyphsize;
6458 int left, top, right, bottom;
6460 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6461 &glyphmetrics, 0, NULL, &identity);
6463 if (glyphsize == GDI_ERROR)
6465 ERR("GetGlyphOutlineW failed\n");
6466 GdipFree(pti);
6467 DeleteDC(hdc);
6468 DeleteObject(hfont);
6469 return GenericError;
6472 if (glyphsize > max_glyphsize)
6473 max_glyphsize = glyphsize;
6475 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6476 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6477 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6478 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6480 if (left < min_x) min_x = left;
6481 if (top < min_y) min_y = top;
6482 if (right > max_x) max_x = right;
6483 if (bottom > max_y) max_y = bottom;
6485 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6487 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6488 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6492 glyph_mask = GdipAlloc(max_glyphsize);
6493 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6494 text_mask_stride = max_x - min_x;
6496 if (!(glyph_mask && text_mask))
6498 GdipFree(glyph_mask);
6499 GdipFree(text_mask);
6500 GdipFree(pti);
6501 DeleteDC(hdc);
6502 DeleteObject(hfont);
6503 return OutOfMemory;
6506 /* Generate a mask for the text */
6507 for (i=0; i<length; i++)
6509 int left, top, stride;
6511 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6512 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6514 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6515 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6516 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6518 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6520 BYTE *glyph_val = glyph_mask + y * stride;
6521 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6522 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6524 *text_val = min(64, *text_val + *glyph_val);
6525 glyph_val++;
6526 text_val++;
6531 GdipFree(pti);
6532 DeleteDC(hdc);
6533 DeleteObject(hfont);
6534 GdipFree(glyph_mask);
6536 /* get the brush data */
6537 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6538 if (!pixel_data)
6540 GdipFree(text_mask);
6541 return OutOfMemory;
6544 pixel_area.X = min_x;
6545 pixel_area.Y = min_y;
6546 pixel_area.Width = max_x - min_x;
6547 pixel_area.Height = max_y - min_y;
6548 pixel_data_stride = pixel_area.Width * 4;
6550 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6551 if (stat != Ok)
6553 GdipFree(text_mask);
6554 GdipFree(pixel_data);
6555 return stat;
6558 /* multiply the brush data by the mask */
6559 for (y=0; y<pixel_area.Height; y++)
6561 BYTE *text_val = text_mask + text_mask_stride * y;
6562 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6563 for (x=0; x<pixel_area.Width; x++)
6565 *pixel_val = (*pixel_val) * (*text_val) / 64;
6566 text_val++;
6567 pixel_val+=4;
6571 GdipFree(text_mask);
6573 /* draw the result */
6574 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6575 pixel_area.Height, pixel_data_stride);
6577 GdipFree(pixel_data);
6579 return stat;
6582 /*****************************************************************************
6583 * GdipDrawDriverString [GDIPLUS.@]
6585 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6586 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6587 GDIPCONST PointF *positions, INT flags,
6588 GDIPCONST GpMatrix *matrix )
6590 GpStatus stat=NotImplemented;
6592 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6594 if (!graphics || !text || !font || !brush || !positions)
6595 return InvalidParameter;
6597 if (length == -1)
6598 length = strlenW(text);
6600 if (graphics->hdc &&
6601 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6602 brush->bt == BrushTypeSolidColor &&
6603 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6604 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, brush,
6605 positions, flags, matrix);
6607 if (stat == NotImplemented)
6608 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, brush,
6609 positions, flags, matrix);
6611 return stat;
6614 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6615 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6617 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6618 return NotImplemented;
6621 /*****************************************************************************
6622 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6624 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6626 GpStatus stat;
6627 GpRegion* rgn;
6629 TRACE("(%p, %p)\n", graphics, res);
6631 if((stat = GdipCreateRegion(&rgn)) != Ok)
6632 return stat;
6634 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6635 goto cleanup;
6637 stat = GdipIsEmptyRegion(rgn, graphics, res);
6639 cleanup:
6640 GdipDeleteRegion(rgn);
6641 return stat;
6644 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6646 static int calls;
6648 TRACE("(%p) stub\n", graphics);
6650 if(!(calls++))
6651 FIXME("not implemented\n");
6653 return NotImplemented;