gdiplus: Add preliminary support for pixel offset modes.
[wine.git] / dlls / gdiplus / graphics.c
blob7e16460febc2c11ddee901e72a480a1b53ee4b89
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 /* Converts angle (in degrees) to x/y coordinates */
55 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
57 REAL radAngle, hypotenuse;
59 radAngle = deg2rad(angle);
60 hypotenuse = 50.0; /* arbitrary */
62 *x = x_0 + cos(radAngle) * hypotenuse;
63 *y = y_0 + sin(radAngle) * hypotenuse;
66 /* Converts from gdiplus path point type to gdi path point type. */
67 static BYTE convert_path_point_type(BYTE type)
69 BYTE ret;
71 switch(type & PathPointTypePathTypeMask){
72 case PathPointTypeBezier:
73 ret = PT_BEZIERTO;
74 break;
75 case PathPointTypeLine:
76 ret = PT_LINETO;
77 break;
78 case PathPointTypeStart:
79 ret = PT_MOVETO;
80 break;
81 default:
82 ERR("Bad point type\n");
83 return 0;
86 if(type & PathPointTypeCloseSubpath)
87 ret |= PT_CLOSEFIGURE;
89 return ret;
92 static COLORREF get_gdi_brush_color(const GpBrush *brush)
94 ARGB argb;
96 switch (brush->bt)
98 case BrushTypeSolidColor:
100 const GpSolidFill *sf = (const GpSolidFill *)brush;
101 argb = sf->color;
102 break;
104 case BrushTypeHatchFill:
106 const GpHatch *hatch = (const GpHatch *)brush;
107 argb = hatch->forecol;
108 break;
110 case BrushTypeLinearGradient:
112 const GpLineGradient *line = (const GpLineGradient *)brush;
113 argb = line->startcolor;
114 break;
116 case BrushTypePathGradient:
118 const GpPathGradient *grad = (const GpPathGradient *)brush;
119 argb = grad->centercolor;
120 break;
122 default:
123 FIXME("unhandled brush type %d\n", brush->bt);
124 argb = 0;
125 break;
127 return ARGB2COLORREF(argb);
130 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
132 HBITMAP hbmp;
133 BITMAPINFOHEADER bmih;
134 DWORD *bits;
135 int x, y;
137 bmih.biSize = sizeof(bmih);
138 bmih.biWidth = 8;
139 bmih.biHeight = 8;
140 bmih.biPlanes = 1;
141 bmih.biBitCount = 32;
142 bmih.biCompression = BI_RGB;
143 bmih.biSizeImage = 0;
145 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
146 if (hbmp)
148 const char *hatch_data;
150 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
152 for (y = 0; y < 8; y++)
154 for (x = 0; x < 8; x++)
156 if (hatch_data[y] & (0x80 >> x))
157 bits[y * 8 + x] = hatch->forecol;
158 else
159 bits[y * 8 + x] = hatch->backcol;
163 else
165 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
167 for (y = 0; y < 64; y++)
168 bits[y] = hatch->forecol;
172 return hbmp;
175 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
177 switch (brush->bt)
179 case BrushTypeSolidColor:
181 const GpSolidFill *sf = (const GpSolidFill *)brush;
182 lb->lbStyle = BS_SOLID;
183 lb->lbColor = ARGB2COLORREF(sf->color);
184 lb->lbHatch = 0;
185 return Ok;
188 case BrushTypeHatchFill:
190 const GpHatch *hatch = (const GpHatch *)brush;
191 HBITMAP hbmp;
193 hbmp = create_hatch_bitmap(hatch);
194 if (!hbmp) return OutOfMemory;
196 lb->lbStyle = BS_PATTERN;
197 lb->lbColor = 0;
198 lb->lbHatch = (ULONG_PTR)hbmp;
199 return Ok;
202 default:
203 FIXME("unhandled brush type %d\n", brush->bt);
204 lb->lbStyle = BS_SOLID;
205 lb->lbColor = get_gdi_brush_color(brush);
206 lb->lbHatch = 0;
207 return Ok;
211 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
213 switch (lb->lbStyle)
215 case BS_PATTERN:
216 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
217 break;
219 return Ok;
222 static HBRUSH create_gdi_brush(const GpBrush *brush)
224 LOGBRUSH lb;
225 HBRUSH gdibrush;
227 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
229 gdibrush = CreateBrushIndirect(&lb);
230 free_gdi_logbrush(&lb);
232 return gdibrush;
235 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
237 LOGBRUSH lb;
238 HPEN gdipen;
239 REAL width;
240 INT save_state, i, numdashes;
241 GpPointF pt[2];
242 DWORD dash_array[MAX_DASHLEN];
244 save_state = SaveDC(graphics->hdc);
246 EndPath(graphics->hdc);
248 if(pen->unit == UnitPixel){
249 width = pen->width;
251 else{
252 /* Get an estimate for the amount the pen width is affected by the world
253 * transform. (This is similar to what some of the wine drivers do.) */
254 pt[0].X = 0.0;
255 pt[0].Y = 0.0;
256 pt[1].X = 1.0;
257 pt[1].Y = 1.0;
258 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
259 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
260 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
262 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
265 if(pen->dash == DashStyleCustom){
266 numdashes = min(pen->numdashes, MAX_DASHLEN);
268 TRACE("dashes are: ");
269 for(i = 0; i < numdashes; i++){
270 dash_array[i] = gdip_round(width * pen->dashes[i]);
271 TRACE("%d, ", dash_array[i]);
273 TRACE("\n and the pen style is %x\n", pen->style);
275 create_gdi_logbrush(pen->brush, &lb);
276 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
277 numdashes, dash_array);
278 free_gdi_logbrush(&lb);
280 else
282 create_gdi_logbrush(pen->brush, &lb);
283 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
284 free_gdi_logbrush(&lb);
287 SelectObject(graphics->hdc, gdipen);
289 return save_state;
292 static void restore_dc(GpGraphics *graphics, INT state)
294 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
295 RestoreDC(graphics->hdc, state);
298 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
299 GpCoordinateSpace src_space, GpMatrix **matrix);
301 /* This helper applies all the changes that the points listed in ptf need in
302 * order to be drawn on the device context. In the end, this should include at
303 * least:
304 * -scaling by page unit
305 * -applying world transformation
306 * -converting from float to int
307 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
308 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
309 * gdi to draw, and these functions would irreparably mess with line widths.
311 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
312 GpPointF *ptf, INT count)
314 REAL scale_x, scale_y;
315 GpMatrix *matrix;
316 int i;
318 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
319 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
321 /* apply page scale */
322 if(graphics->unit != UnitDisplay)
324 scale_x *= graphics->scale;
325 scale_y *= graphics->scale;
328 GdipCloneMatrix(graphics->worldtrans, &matrix);
329 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
330 GdipTransformMatrixPoints(matrix, ptf, count);
331 GdipDeleteMatrix(matrix);
333 for(i = 0; i < count; i++){
334 pti[i].x = gdip_round(ptf[i].X);
335 pti[i].y = gdip_round(ptf[i].Y);
339 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
340 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
342 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
344 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
346 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
347 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
349 else
351 BLENDFUNCTION bf;
353 bf.BlendOp = AC_SRC_OVER;
354 bf.BlendFlags = 0;
355 bf.SourceConstantAlpha = 255;
356 bf.AlphaFormat = AC_SRC_ALPHA;
358 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
359 hdc, src_x, src_y, src_width, src_height, bf);
363 /* Draw non-premultiplied ARGB data to the given graphics object */
364 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
365 const BYTE *src, INT src_width, INT src_height, INT src_stride)
367 if (graphics->image && graphics->image->type == ImageTypeBitmap)
369 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
370 INT x, y;
372 for (x=0; x<src_width; x++)
374 for (y=0; y<src_height; y++)
376 ARGB dst_color, src_color;
377 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
378 src_color = ((ARGB*)(src + src_stride * y))[x];
379 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
383 return Ok;
385 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
387 ERR("This should not be used for metafiles; fix caller\n");
388 return NotImplemented;
390 else
392 HDC hdc;
393 HBITMAP hbitmap;
394 BITMAPINFOHEADER bih;
395 BYTE *temp_bits;
397 hdc = CreateCompatibleDC(0);
399 bih.biSize = sizeof(BITMAPINFOHEADER);
400 bih.biWidth = src_width;
401 bih.biHeight = -src_height;
402 bih.biPlanes = 1;
403 bih.biBitCount = 32;
404 bih.biCompression = BI_RGB;
405 bih.biSizeImage = 0;
406 bih.biXPelsPerMeter = 0;
407 bih.biYPelsPerMeter = 0;
408 bih.biClrUsed = 0;
409 bih.biClrImportant = 0;
411 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
412 (void**)&temp_bits, NULL, 0);
414 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
415 4 * src_width, src, src_stride);
417 SelectObject(hdc, hbitmap);
418 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
419 hdc, 0, 0, src_width, src_height);
420 DeleteDC(hdc);
421 DeleteObject(hbitmap);
423 return Ok;
427 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
428 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
430 GpStatus stat=Ok;
432 if (graphics->image && graphics->image->type == ImageTypeBitmap)
434 int i, size;
435 RGNDATA *rgndata;
436 RECT *rects;
438 size = GetRegionData(hregion, 0, NULL);
440 rgndata = GdipAlloc(size);
441 if (!rgndata)
442 return OutOfMemory;
444 GetRegionData(hregion, size, rgndata);
446 rects = (RECT*)&rgndata->Buffer;
448 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
450 stat = alpha_blend_pixels(graphics, rects[i].left, rects[i].top,
451 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
452 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
453 src_stride);
456 GdipFree(rgndata);
458 return stat;
460 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
462 ERR("This should not be used for metafiles; fix caller\n");
463 return NotImplemented;
465 else
467 int save;
469 save = SaveDC(graphics->hdc);
471 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
473 stat = alpha_blend_pixels(graphics, dst_x, dst_y, src, src_width,
474 src_height, src_stride);
476 RestoreDC(graphics->hdc, save);
478 return stat;
482 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
484 ARGB result=0;
485 ARGB i;
486 INT a1, a2, a3;
488 a1 = (start >> 24) & 0xff;
489 a2 = (end >> 24) & 0xff;
491 a3 = (int)(a1*(1.0f - position)+a2*(position));
493 result |= a3 << 24;
495 for (i=0xff; i<=0xff0000; i = i << 8)
496 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
497 return result;
500 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
502 REAL blendfac;
504 /* clamp to between 0.0 and 1.0, using the wrap mode */
505 if (brush->wrap == WrapModeTile)
507 position = fmodf(position, 1.0f);
508 if (position < 0.0f) position += 1.0f;
510 else /* WrapModeFlip* */
512 position = fmodf(position, 2.0f);
513 if (position < 0.0f) position += 2.0f;
514 if (position > 1.0f) position = 2.0f - position;
517 if (brush->blendcount == 1)
518 blendfac = position;
519 else
521 int i=1;
522 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
523 REAL range;
525 /* locate the blend positions surrounding this position */
526 while (position > brush->blendpos[i])
527 i++;
529 /* interpolate between the blend positions */
530 left_blendpos = brush->blendpos[i-1];
531 left_blendfac = brush->blendfac[i-1];
532 right_blendpos = brush->blendpos[i];
533 right_blendfac = brush->blendfac[i];
534 range = right_blendpos - left_blendpos;
535 blendfac = (left_blendfac * (right_blendpos - position) +
536 right_blendfac * (position - left_blendpos)) / range;
539 if (brush->pblendcount == 0)
540 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
541 else
543 int i=1;
544 ARGB left_blendcolor, right_blendcolor;
545 REAL left_blendpos, right_blendpos;
547 /* locate the blend colors surrounding this position */
548 while (blendfac > brush->pblendpos[i])
549 i++;
551 /* interpolate between the blend colors */
552 left_blendpos = brush->pblendpos[i-1];
553 left_blendcolor = brush->pblendcolor[i-1];
554 right_blendpos = brush->pblendpos[i];
555 right_blendcolor = brush->pblendcolor[i];
556 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
557 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
561 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
563 REAL val[5], res[4];
564 int i, j;
565 unsigned char a, r, g, b;
567 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
568 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
569 val[2] = (color & 0xff) / 255.0; /* blue */
570 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
571 val[4] = 1.0; /* translation */
573 for (i=0; i<4; i++)
575 res[i] = 0.0;
577 for (j=0; j<5; j++)
578 res[i] += matrix->m[j][i] * val[j];
581 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
582 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
583 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
584 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
586 return (a << 24) | (r << 16) | (g << 8) | b;
589 static int color_is_gray(ARGB color)
591 unsigned char r, g, b;
593 r = (color >> 16) & 0xff;
594 g = (color >> 8) & 0xff;
595 b = color & 0xff;
597 return (r == g) && (g == b);
600 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
601 UINT width, UINT height, INT stride, ColorAdjustType type)
603 UINT x, y, i;
605 if (attributes->colorkeys[type].enabled ||
606 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
608 const struct color_key *key;
609 BYTE min_blue, min_green, min_red;
610 BYTE max_blue, max_green, max_red;
612 if (attributes->colorkeys[type].enabled)
613 key = &attributes->colorkeys[type];
614 else
615 key = &attributes->colorkeys[ColorAdjustTypeDefault];
617 min_blue = key->low&0xff;
618 min_green = (key->low>>8)&0xff;
619 min_red = (key->low>>16)&0xff;
621 max_blue = key->high&0xff;
622 max_green = (key->high>>8)&0xff;
623 max_red = (key->high>>16)&0xff;
625 for (x=0; x<width; x++)
626 for (y=0; y<height; y++)
628 ARGB *src_color;
629 BYTE blue, green, red;
630 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
631 blue = *src_color&0xff;
632 green = (*src_color>>8)&0xff;
633 red = (*src_color>>16)&0xff;
634 if (blue >= min_blue && green >= min_green && red >= min_red &&
635 blue <= max_blue && green <= max_green && red <= max_red)
636 *src_color = 0x00000000;
640 if (attributes->colorremaptables[type].enabled ||
641 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
643 const struct color_remap_table *table;
645 if (attributes->colorremaptables[type].enabled)
646 table = &attributes->colorremaptables[type];
647 else
648 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
650 for (x=0; x<width; x++)
651 for (y=0; y<height; y++)
653 ARGB *src_color;
654 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
655 for (i=0; i<table->mapsize; i++)
657 if (*src_color == table->colormap[i].oldColor.Argb)
659 *src_color = table->colormap[i].newColor.Argb;
660 break;
666 if (attributes->colormatrices[type].enabled ||
667 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
669 const struct color_matrix *colormatrices;
671 if (attributes->colormatrices[type].enabled)
672 colormatrices = &attributes->colormatrices[type];
673 else
674 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
676 for (x=0; x<width; x++)
677 for (y=0; y<height; y++)
679 ARGB *src_color;
680 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
682 if (colormatrices->flags == ColorMatrixFlagsDefault ||
683 !color_is_gray(*src_color))
685 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
687 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
689 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
694 if (attributes->gamma_enabled[type] ||
695 attributes->gamma_enabled[ColorAdjustTypeDefault])
697 REAL gamma;
699 if (attributes->gamma_enabled[type])
700 gamma = attributes->gamma[type];
701 else
702 gamma = attributes->gamma[ColorAdjustTypeDefault];
704 for (x=0; x<width; x++)
705 for (y=0; y<height; y++)
707 ARGB *src_color;
708 BYTE blue, green, red;
709 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
711 blue = *src_color&0xff;
712 green = (*src_color>>8)&0xff;
713 red = (*src_color>>16)&0xff;
715 /* FIXME: We should probably use a table for this. */
716 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
717 green = floorf(powf(green / 255.0, gamma) * 255.0);
718 red = floorf(powf(red / 255.0, gamma) * 255.0);
720 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
725 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
726 * bitmap that contains all the pixels we may need to draw it. */
727 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
728 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
729 GpRect *rect)
731 INT left, top, right, bottom;
733 switch (interpolation)
735 case InterpolationModeHighQualityBilinear:
736 case InterpolationModeHighQualityBicubic:
737 /* FIXME: Include a greater range for the prefilter? */
738 case InterpolationModeBicubic:
739 case InterpolationModeBilinear:
740 left = (INT)(floorf(srcx));
741 top = (INT)(floorf(srcy));
742 right = (INT)(ceilf(srcx+srcwidth));
743 bottom = (INT)(ceilf(srcy+srcheight));
744 break;
745 case InterpolationModeNearestNeighbor:
746 default:
747 left = gdip_round(srcx);
748 top = gdip_round(srcy);
749 right = gdip_round(srcx+srcwidth);
750 bottom = gdip_round(srcy+srcheight);
751 break;
754 if (wrap == WrapModeClamp)
756 if (left < 0)
757 left = 0;
758 if (top < 0)
759 top = 0;
760 if (right >= bitmap->width)
761 right = bitmap->width-1;
762 if (bottom >= bitmap->height)
763 bottom = bitmap->height-1;
765 else
767 /* In some cases we can make the rectangle smaller here, but the logic
768 * is hard to get right, and tiling suggests we're likely to use the
769 * entire source image. */
770 if (left < 0 || right >= bitmap->width)
772 left = 0;
773 right = bitmap->width-1;
776 if (top < 0 || bottom >= bitmap->height)
778 top = 0;
779 bottom = bitmap->height-1;
783 rect->X = left;
784 rect->Y = top;
785 rect->Width = right - left + 1;
786 rect->Height = bottom - top + 1;
789 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
790 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
792 if (attributes->wrap == WrapModeClamp)
794 if (x < 0 || y < 0 || x >= width || y >= height)
795 return attributes->outside_color;
797 else
799 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
800 if (x < 0)
801 x = width*2 + x % (width * 2);
802 if (y < 0)
803 y = height*2 + y % (height * 2);
805 if ((attributes->wrap & 1) == 1)
807 /* Flip X */
808 if ((x / width) % 2 == 0)
809 x = x % width;
810 else
811 x = width - 1 - x % width;
813 else
814 x = x % width;
816 if ((attributes->wrap & 2) == 2)
818 /* Flip Y */
819 if ((y / height) % 2 == 0)
820 y = y % height;
821 else
822 y = height - 1 - y % height;
824 else
825 y = y % height;
828 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
830 ERR("out of range pixel requested\n");
831 return 0xffcd0084;
834 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
837 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
838 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
839 InterpolationMode interpolation, PixelOffsetMode offset_mode)
841 static int fixme;
843 switch (interpolation)
845 default:
846 if (!fixme++)
847 FIXME("Unimplemented interpolation %i\n", interpolation);
848 /* fall-through */
849 case InterpolationModeBilinear:
851 REAL leftxf, topyf;
852 INT leftx, rightx, topy, bottomy;
853 ARGB topleft, topright, bottomleft, bottomright;
854 ARGB top, bottom;
855 float x_offset;
857 leftxf = floorf(point->X);
858 leftx = (INT)leftxf;
859 rightx = (INT)ceilf(point->X);
860 topyf = floorf(point->Y);
861 topy = (INT)topyf;
862 bottomy = (INT)ceilf(point->Y);
864 if (leftx == rightx && topy == bottomy)
865 return sample_bitmap_pixel(src_rect, bits, width, height,
866 leftx, topy, attributes);
868 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
869 leftx, topy, attributes);
870 topright = sample_bitmap_pixel(src_rect, bits, width, height,
871 rightx, topy, attributes);
872 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
873 leftx, bottomy, attributes);
874 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
875 rightx, bottomy, attributes);
877 x_offset = point->X - leftxf;
878 top = blend_colors(topleft, topright, x_offset);
879 bottom = blend_colors(bottomleft, bottomright, x_offset);
881 return blend_colors(top, bottom, point->Y - topyf);
883 case InterpolationModeNearestNeighbor:
885 FLOAT pixel_offset;
886 switch (offset_mode)
888 default:
889 case PixelOffsetModeNone:
890 case PixelOffsetModeHighSpeed:
891 pixel_offset = 0.5;
892 break;
894 case PixelOffsetModeHalf:
895 case PixelOffsetModeHighQuality:
896 pixel_offset = 0.0;
897 break;
899 return sample_bitmap_pixel(src_rect, bits, width, height,
900 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
906 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
908 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
911 static INT brush_can_fill_path(GpBrush *brush)
913 switch (brush->bt)
915 case BrushTypeSolidColor:
916 return 1;
917 case BrushTypeHatchFill:
919 GpHatch *hatch = (GpHatch*)brush;
920 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
921 ((hatch->backcol & 0xff000000) == 0xff000000);
923 case BrushTypeLinearGradient:
924 case BrushTypeTextureFill:
925 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
926 default:
927 return 0;
931 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
933 switch (brush->bt)
935 case BrushTypeSolidColor:
937 GpSolidFill *fill = (GpSolidFill*)brush;
938 HBITMAP bmp = ARGB2BMP(fill->color);
940 if (bmp)
942 RECT rc;
943 /* partially transparent fill */
945 SelectClipPath(graphics->hdc, RGN_AND);
946 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
948 HDC hdc = CreateCompatibleDC(NULL);
950 if (!hdc) break;
952 SelectObject(hdc, bmp);
953 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
954 hdc, 0, 0, 1, 1);
955 DeleteDC(hdc);
958 DeleteObject(bmp);
959 break;
961 /* else fall through */
963 default:
965 HBRUSH gdibrush, old_brush;
967 gdibrush = create_gdi_brush(brush);
968 if (!gdibrush) return;
970 old_brush = SelectObject(graphics->hdc, gdibrush);
971 FillPath(graphics->hdc);
972 SelectObject(graphics->hdc, old_brush);
973 DeleteObject(gdibrush);
974 break;
979 static INT brush_can_fill_pixels(GpBrush *brush)
981 switch (brush->bt)
983 case BrushTypeSolidColor:
984 case BrushTypeHatchFill:
985 case BrushTypeLinearGradient:
986 case BrushTypeTextureFill:
987 case BrushTypePathGradient:
988 return 1;
989 default:
990 return 0;
994 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
995 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
997 switch (brush->bt)
999 case BrushTypeSolidColor:
1001 int x, y;
1002 GpSolidFill *fill = (GpSolidFill*)brush;
1003 for (x=0; x<fill_area->Width; x++)
1004 for (y=0; y<fill_area->Height; y++)
1005 argb_pixels[x + y*cdwStride] = fill->color;
1006 return Ok;
1008 case BrushTypeHatchFill:
1010 int x, y;
1011 GpHatch *fill = (GpHatch*)brush;
1012 const char *hatch_data;
1014 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1015 return NotImplemented;
1017 for (x=0; x<fill_area->Width; x++)
1018 for (y=0; y<fill_area->Height; y++)
1020 int hx, hy;
1022 /* FIXME: Account for the rendering origin */
1023 hx = (x + fill_area->X) % 8;
1024 hy = (y + fill_area->Y) % 8;
1026 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1027 argb_pixels[x + y*cdwStride] = fill->forecol;
1028 else
1029 argb_pixels[x + y*cdwStride] = fill->backcol;
1032 return Ok;
1034 case BrushTypeLinearGradient:
1036 GpLineGradient *fill = (GpLineGradient*)brush;
1037 GpPointF draw_points[3], line_points[3];
1038 GpStatus stat;
1039 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1040 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1041 int x, y;
1043 draw_points[0].X = fill_area->X;
1044 draw_points[0].Y = fill_area->Y;
1045 draw_points[1].X = fill_area->X+1;
1046 draw_points[1].Y = fill_area->Y;
1047 draw_points[2].X = fill_area->X;
1048 draw_points[2].Y = fill_area->Y+1;
1050 /* Transform the points to a co-ordinate space where X is the point's
1051 * position in the gradient, 0.0 being the start point and 1.0 the
1052 * end point. */
1053 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1054 CoordinateSpaceDevice, draw_points, 3);
1056 if (stat == Ok)
1058 line_points[0] = fill->startpoint;
1059 line_points[1] = fill->endpoint;
1060 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1061 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1063 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1066 if (stat == Ok)
1068 stat = GdipInvertMatrix(world_to_gradient);
1070 if (stat == Ok)
1071 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1073 GdipDeleteMatrix(world_to_gradient);
1076 if (stat == Ok)
1078 REAL x_delta = draw_points[1].X - draw_points[0].X;
1079 REAL y_delta = draw_points[2].X - draw_points[0].X;
1081 for (y=0; y<fill_area->Height; y++)
1083 for (x=0; x<fill_area->Width; x++)
1085 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1087 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1092 return stat;
1094 case BrushTypeTextureFill:
1096 GpTexture *fill = (GpTexture*)brush;
1097 GpPointF draw_points[3];
1098 GpStatus stat;
1099 GpMatrix *world_to_texture;
1100 int x, y;
1101 GpBitmap *bitmap;
1102 int src_stride;
1103 GpRect src_area;
1105 if (fill->image->type != ImageTypeBitmap)
1107 FIXME("metafile texture brushes not implemented\n");
1108 return NotImplemented;
1111 bitmap = (GpBitmap*)fill->image;
1112 src_stride = sizeof(ARGB) * bitmap->width;
1114 src_area.X = src_area.Y = 0;
1115 src_area.Width = bitmap->width;
1116 src_area.Height = bitmap->height;
1118 draw_points[0].X = fill_area->X;
1119 draw_points[0].Y = fill_area->Y;
1120 draw_points[1].X = fill_area->X+1;
1121 draw_points[1].Y = fill_area->Y;
1122 draw_points[2].X = fill_area->X;
1123 draw_points[2].Y = fill_area->Y+1;
1125 /* Transform the points to the co-ordinate space of the bitmap. */
1126 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1127 CoordinateSpaceDevice, draw_points, 3);
1129 if (stat == Ok)
1131 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
1134 if (stat == Ok)
1136 stat = GdipInvertMatrix(world_to_texture);
1138 if (stat == Ok)
1139 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
1141 GdipDeleteMatrix(world_to_texture);
1144 if (stat == Ok && !fill->bitmap_bits)
1146 BitmapData lockeddata;
1148 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1149 if (!fill->bitmap_bits)
1150 stat = OutOfMemory;
1152 if (stat == Ok)
1154 lockeddata.Width = bitmap->width;
1155 lockeddata.Height = bitmap->height;
1156 lockeddata.Stride = src_stride;
1157 lockeddata.PixelFormat = PixelFormat32bppARGB;
1158 lockeddata.Scan0 = fill->bitmap_bits;
1160 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1161 PixelFormat32bppARGB, &lockeddata);
1164 if (stat == Ok)
1165 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1167 if (stat == Ok)
1168 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1169 bitmap->width, bitmap->height,
1170 src_stride, ColorAdjustTypeBitmap);
1172 if (stat != Ok)
1174 GdipFree(fill->bitmap_bits);
1175 fill->bitmap_bits = NULL;
1179 if (stat == Ok)
1181 REAL x_dx = draw_points[1].X - draw_points[0].X;
1182 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1183 REAL y_dx = draw_points[2].X - draw_points[0].X;
1184 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1186 for (y=0; y<fill_area->Height; y++)
1188 for (x=0; x<fill_area->Width; x++)
1190 GpPointF point;
1191 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1192 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1194 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1195 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1196 &point, fill->imageattributes, graphics->interpolation,
1197 graphics->pixeloffset);
1202 return stat;
1204 case BrushTypePathGradient:
1206 GpPathGradient *fill = (GpPathGradient*)brush;
1207 GpPath *flat_path;
1208 GpMatrix *world_to_device;
1209 GpStatus stat;
1210 int i, figure_start=0;
1211 GpPointF start_point, end_point, center_point;
1212 BYTE type;
1213 REAL min_yf, max_yf, line1_xf, line2_xf;
1214 INT min_y, max_y, min_x, max_x;
1215 INT x, y;
1216 ARGB outer_color;
1217 static int transform_fixme_once;
1219 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1221 static int once;
1222 if (!once++)
1223 FIXME("path gradient focus not implemented\n");
1226 if (fill->gamma)
1228 static int once;
1229 if (!once++)
1230 FIXME("path gradient gamma correction not implemented\n");
1233 if (fill->blendcount)
1235 static int once;
1236 if (!once++)
1237 FIXME("path gradient blend not implemented\n");
1240 if (fill->pblendcount)
1242 static int once;
1243 if (!once++)
1244 FIXME("path gradient preset blend not implemented\n");
1247 if (!transform_fixme_once)
1249 BOOL is_identity=TRUE;
1250 GdipIsMatrixIdentity(fill->transform, &is_identity);
1251 if (!is_identity)
1253 FIXME("path gradient transform not implemented\n");
1254 transform_fixme_once = 1;
1258 stat = GdipClonePath(fill->path, &flat_path);
1260 if (stat != Ok)
1261 return stat;
1263 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1264 CoordinateSpaceWorld, &world_to_device);
1265 if (stat == Ok)
1267 stat = GdipTransformPath(flat_path, world_to_device);
1269 if (stat == Ok)
1271 center_point = fill->center;
1272 stat = GdipTransformMatrixPoints(world_to_device, &center_point, 1);
1275 if (stat == Ok)
1276 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1278 GdipDeleteMatrix(world_to_device);
1281 if (stat != Ok)
1283 GdipDeletePath(flat_path);
1284 return stat;
1287 for (i=0; i<flat_path->pathdata.Count; i++)
1289 int start_center_line=0, end_center_line=0;
1290 int seen_start=0, seen_end=0, seen_center=0;
1291 REAL center_distance;
1292 ARGB start_color, end_color;
1293 REAL dy, dx;
1295 type = flat_path->pathdata.Types[i];
1297 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1298 figure_start = i;
1300 start_point = flat_path->pathdata.Points[i];
1302 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1304 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1306 end_point = flat_path->pathdata.Points[figure_start];
1307 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1309 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1311 end_point = flat_path->pathdata.Points[i+1];
1312 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1314 else
1315 continue;
1317 outer_color = start_color;
1319 min_yf = center_point.Y;
1320 if (min_yf > start_point.Y) min_yf = start_point.Y;
1321 if (min_yf > end_point.Y) min_yf = end_point.Y;
1323 if (min_yf < fill_area->Y)
1324 min_y = fill_area->Y;
1325 else
1326 min_y = (INT)ceil(min_yf);
1328 max_yf = center_point.Y;
1329 if (max_yf < start_point.Y) max_yf = start_point.Y;
1330 if (max_yf < end_point.Y) max_yf = end_point.Y;
1332 if (max_yf > fill_area->Y + fill_area->Height)
1333 max_y = fill_area->Y + fill_area->Height;
1334 else
1335 max_y = (INT)ceil(max_yf);
1337 dy = end_point.Y - start_point.Y;
1338 dx = end_point.X - start_point.X;
1340 /* This is proportional to the distance from start-end line to center point. */
1341 center_distance = dy * (start_point.X - center_point.X) +
1342 dx * (center_point.Y - start_point.Y);
1344 for (y=min_y; y<max_y; y++)
1346 REAL yf = (REAL)y;
1348 if (!seen_start && yf >= start_point.Y)
1350 seen_start = 1;
1351 start_center_line ^= 1;
1353 if (!seen_end && yf >= end_point.Y)
1355 seen_end = 1;
1356 end_center_line ^= 1;
1358 if (!seen_center && yf >= center_point.Y)
1360 seen_center = 1;
1361 start_center_line ^= 1;
1362 end_center_line ^= 1;
1365 if (start_center_line)
1366 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1367 else
1368 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1370 if (end_center_line)
1371 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1372 else
1373 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1375 if (line1_xf < line2_xf)
1377 min_x = (INT)ceil(line1_xf);
1378 max_x = (INT)ceil(line2_xf);
1380 else
1382 min_x = (INT)ceil(line2_xf);
1383 max_x = (INT)ceil(line1_xf);
1386 if (min_x < fill_area->X)
1387 min_x = fill_area->X;
1388 if (max_x > fill_area->X + fill_area->Width)
1389 max_x = fill_area->X + fill_area->Width;
1391 for (x=min_x; x<max_x; x++)
1393 REAL xf = (REAL)x;
1394 REAL distance;
1396 if (start_color != end_color)
1398 REAL blend_amount, pdy, pdx;
1399 pdy = yf - center_point.Y;
1400 pdx = xf - center_point.X;
1401 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1402 outer_color = blend_colors(start_color, end_color, blend_amount);
1405 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1406 (end_point.X - start_point.X) * (yf - start_point.Y);
1408 distance = distance / center_distance;
1410 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1411 blend_colors(outer_color, fill->centercolor, distance);
1416 GdipDeletePath(flat_path);
1417 return stat;
1419 default:
1420 return NotImplemented;
1424 /* GdipDrawPie/GdipFillPie helper function */
1425 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1426 REAL height, REAL startAngle, REAL sweepAngle)
1428 GpPointF ptf[4];
1429 POINT pti[4];
1431 ptf[0].X = x;
1432 ptf[0].Y = y;
1433 ptf[1].X = x + width;
1434 ptf[1].Y = y + height;
1436 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1437 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1439 transform_and_round_points(graphics, pti, ptf, 4);
1441 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1442 pti[2].y, pti[3].x, pti[3].y);
1445 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1446 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1447 * should not be called on an hdc that has a path you care about. */
1448 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1449 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1451 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1452 GpMatrix *matrix = NULL;
1453 HBRUSH brush = NULL;
1454 HPEN pen = NULL;
1455 PointF ptf[4], *custptf = NULL;
1456 POINT pt[4], *custpt = NULL;
1457 BYTE *tp = NULL;
1458 REAL theta, dsmall, dbig, dx, dy = 0.0;
1459 INT i, count;
1460 LOGBRUSH lb;
1461 BOOL customstroke;
1463 if((x1 == x2) && (y1 == y2))
1464 return;
1466 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1468 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1469 if(!customstroke){
1470 brush = CreateSolidBrush(color);
1471 lb.lbStyle = BS_SOLID;
1472 lb.lbColor = color;
1473 lb.lbHatch = 0;
1474 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1475 PS_JOIN_MITER, 1, &lb, 0,
1476 NULL);
1477 oldbrush = SelectObject(graphics->hdc, brush);
1478 oldpen = SelectObject(graphics->hdc, pen);
1481 switch(cap){
1482 case LineCapFlat:
1483 break;
1484 case LineCapSquare:
1485 case LineCapSquareAnchor:
1486 case LineCapDiamondAnchor:
1487 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1488 if(cap == LineCapDiamondAnchor){
1489 dsmall = cos(theta + M_PI_2) * size;
1490 dbig = sin(theta + M_PI_2) * size;
1492 else{
1493 dsmall = cos(theta + M_PI_4) * size;
1494 dbig = sin(theta + M_PI_4) * size;
1497 ptf[0].X = x2 - dsmall;
1498 ptf[1].X = x2 + dbig;
1500 ptf[0].Y = y2 - dbig;
1501 ptf[3].Y = y2 + dsmall;
1503 ptf[1].Y = y2 - dsmall;
1504 ptf[2].Y = y2 + dbig;
1506 ptf[3].X = x2 - dbig;
1507 ptf[2].X = x2 + dsmall;
1509 transform_and_round_points(graphics, pt, ptf, 4);
1510 Polygon(graphics->hdc, pt, 4);
1512 break;
1513 case LineCapArrowAnchor:
1514 size = size * 4.0 / sqrt(3.0);
1516 dx = cos(M_PI / 6.0 + theta) * size;
1517 dy = sin(M_PI / 6.0 + theta) * size;
1519 ptf[0].X = x2 - dx;
1520 ptf[0].Y = y2 - dy;
1522 dx = cos(- M_PI / 6.0 + theta) * size;
1523 dy = sin(- M_PI / 6.0 + theta) * size;
1525 ptf[1].X = x2 - dx;
1526 ptf[1].Y = y2 - dy;
1528 ptf[2].X = x2;
1529 ptf[2].Y = y2;
1531 transform_and_round_points(graphics, pt, ptf, 3);
1532 Polygon(graphics->hdc, pt, 3);
1534 break;
1535 case LineCapRoundAnchor:
1536 dx = dy = ANCHOR_WIDTH * size / 2.0;
1538 ptf[0].X = x2 - dx;
1539 ptf[0].Y = y2 - dy;
1540 ptf[1].X = x2 + dx;
1541 ptf[1].Y = y2 + dy;
1543 transform_and_round_points(graphics, pt, ptf, 2);
1544 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1546 break;
1547 case LineCapTriangle:
1548 size = size / 2.0;
1549 dx = cos(M_PI_2 + theta) * size;
1550 dy = sin(M_PI_2 + theta) * size;
1552 ptf[0].X = x2 - dx;
1553 ptf[0].Y = y2 - dy;
1554 ptf[1].X = x2 + dx;
1555 ptf[1].Y = y2 + dy;
1557 dx = cos(theta) * size;
1558 dy = sin(theta) * size;
1560 ptf[2].X = x2 + dx;
1561 ptf[2].Y = y2 + dy;
1563 transform_and_round_points(graphics, pt, ptf, 3);
1564 Polygon(graphics->hdc, pt, 3);
1566 break;
1567 case LineCapRound:
1568 dx = dy = size / 2.0;
1570 ptf[0].X = x2 - dx;
1571 ptf[0].Y = y2 - dy;
1572 ptf[1].X = x2 + dx;
1573 ptf[1].Y = y2 + dy;
1575 dx = -cos(M_PI_2 + theta) * size;
1576 dy = -sin(M_PI_2 + theta) * size;
1578 ptf[2].X = x2 - dx;
1579 ptf[2].Y = y2 - dy;
1580 ptf[3].X = x2 + dx;
1581 ptf[3].Y = y2 + dy;
1583 transform_and_round_points(graphics, pt, ptf, 4);
1584 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1585 pt[2].y, pt[3].x, pt[3].y);
1587 break;
1588 case LineCapCustom:
1589 if(!custom)
1590 break;
1592 count = custom->pathdata.Count;
1593 custptf = GdipAlloc(count * sizeof(PointF));
1594 custpt = GdipAlloc(count * sizeof(POINT));
1595 tp = GdipAlloc(count);
1597 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1598 goto custend;
1600 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1602 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1603 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1604 MatrixOrderAppend);
1605 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1606 GdipTransformMatrixPoints(matrix, custptf, count);
1608 transform_and_round_points(graphics, custpt, custptf, count);
1610 for(i = 0; i < count; i++)
1611 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1613 if(custom->fill){
1614 BeginPath(graphics->hdc);
1615 PolyDraw(graphics->hdc, custpt, tp, count);
1616 EndPath(graphics->hdc);
1617 StrokeAndFillPath(graphics->hdc);
1619 else
1620 PolyDraw(graphics->hdc, custpt, tp, count);
1622 custend:
1623 GdipFree(custptf);
1624 GdipFree(custpt);
1625 GdipFree(tp);
1626 GdipDeleteMatrix(matrix);
1627 break;
1628 default:
1629 break;
1632 if(!customstroke){
1633 SelectObject(graphics->hdc, oldbrush);
1634 SelectObject(graphics->hdc, oldpen);
1635 DeleteObject(brush);
1636 DeleteObject(pen);
1640 /* Shortens the line by the given percent by changing x2, y2.
1641 * If percent is > 1.0 then the line will change direction.
1642 * If percent is negative it can lengthen the line. */
1643 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1645 REAL dist, theta, dx, dy;
1647 if((y1 == *y2) && (x1 == *x2))
1648 return;
1650 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1651 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1652 dx = cos(theta) * dist;
1653 dy = sin(theta) * dist;
1655 *x2 = *x2 + dx;
1656 *y2 = *y2 + dy;
1659 /* Shortens the line by the given amount by changing x2, y2.
1660 * If the amount is greater than the distance, the line will become length 0.
1661 * If the amount is negative, it can lengthen the line. */
1662 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1664 REAL dx, dy, percent;
1666 dx = *x2 - x1;
1667 dy = *y2 - y1;
1668 if(dx == 0 && dy == 0)
1669 return;
1671 percent = amt / sqrt(dx * dx + dy * dy);
1672 if(percent >= 1.0){
1673 *x2 = x1;
1674 *y2 = y1;
1675 return;
1678 shorten_line_percent(x1, y1, x2, y2, percent);
1681 /* Draws lines between the given points, and if caps is true then draws an endcap
1682 * at the end of the last line. */
1683 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1684 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1686 POINT *pti = NULL;
1687 GpPointF *ptcopy = NULL;
1688 GpStatus status = GenericError;
1690 if(!count)
1691 return Ok;
1693 pti = GdipAlloc(count * sizeof(POINT));
1694 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1696 if(!pti || !ptcopy){
1697 status = OutOfMemory;
1698 goto end;
1701 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1703 if(caps){
1704 if(pen->endcap == LineCapArrowAnchor)
1705 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1706 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1707 else if((pen->endcap == LineCapCustom) && pen->customend)
1708 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1709 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1710 pen->customend->inset * pen->width);
1712 if(pen->startcap == LineCapArrowAnchor)
1713 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1714 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1715 else if((pen->startcap == LineCapCustom) && pen->customstart)
1716 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1717 &ptcopy[0].X, &ptcopy[0].Y,
1718 pen->customstart->inset * pen->width);
1720 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1721 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1722 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1723 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1726 transform_and_round_points(graphics, pti, ptcopy, count);
1728 if(Polyline(graphics->hdc, pti, count))
1729 status = Ok;
1731 end:
1732 GdipFree(pti);
1733 GdipFree(ptcopy);
1735 return status;
1738 /* Conducts a linear search to find the bezier points that will back off
1739 * the endpoint of the curve by a distance of amt. Linear search works
1740 * better than binary in this case because there are multiple solutions,
1741 * and binary searches often find a bad one. I don't think this is what
1742 * Windows does but short of rendering the bezier without GDI's help it's
1743 * the best we can do. If rev then work from the start of the passed points
1744 * instead of the end. */
1745 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1747 GpPointF origpt[4];
1748 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1749 INT i, first = 0, second = 1, third = 2, fourth = 3;
1751 if(rev){
1752 first = 3;
1753 second = 2;
1754 third = 1;
1755 fourth = 0;
1758 origx = pt[fourth].X;
1759 origy = pt[fourth].Y;
1760 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1762 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1763 /* reset bezier points to original values */
1764 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1765 /* Perform magic on bezier points. Order is important here.*/
1766 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1767 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1768 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1769 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1770 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1771 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1773 dx = pt[fourth].X - origx;
1774 dy = pt[fourth].Y - origy;
1776 diff = sqrt(dx * dx + dy * dy);
1777 percent += 0.0005 * amt;
1781 /* Draws bezier curves between given points, and if caps is true then draws an
1782 * endcap at the end of the last line. */
1783 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1784 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1786 POINT *pti;
1787 GpPointF *ptcopy;
1788 GpStatus status = GenericError;
1790 if(!count)
1791 return Ok;
1793 pti = GdipAlloc(count * sizeof(POINT));
1794 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1796 if(!pti || !ptcopy){
1797 status = OutOfMemory;
1798 goto end;
1801 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1803 if(caps){
1804 if(pen->endcap == LineCapArrowAnchor)
1805 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1806 else if((pen->endcap == LineCapCustom) && pen->customend)
1807 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1808 FALSE);
1810 if(pen->startcap == LineCapArrowAnchor)
1811 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1812 else if((pen->startcap == LineCapCustom) && pen->customstart)
1813 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1815 /* the direction of the line cap is parallel to the direction at the
1816 * end of the bezier (which, if it has been shortened, is not the same
1817 * as the direction from pt[count-2] to pt[count-1]) */
1818 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1819 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1820 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1821 pt[count - 1].X, pt[count - 1].Y);
1823 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1824 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1825 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1828 transform_and_round_points(graphics, pti, ptcopy, count);
1830 PolyBezier(graphics->hdc, pti, count);
1832 status = Ok;
1834 end:
1835 GdipFree(pti);
1836 GdipFree(ptcopy);
1838 return status;
1841 /* Draws a combination of bezier curves and lines between points. */
1842 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1843 GDIPCONST BYTE * types, INT count, BOOL caps)
1845 POINT *pti = GdipAlloc(count * sizeof(POINT));
1846 BYTE *tp = GdipAlloc(count);
1847 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1848 INT i, j;
1849 GpStatus status = GenericError;
1851 if(!count){
1852 status = Ok;
1853 goto end;
1855 if(!pti || !tp || !ptcopy){
1856 status = OutOfMemory;
1857 goto end;
1860 for(i = 1; i < count; i++){
1861 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1862 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1863 || !(types[i + 1] & PathPointTypeBezier)){
1864 ERR("Bad bezier points\n");
1865 goto end;
1867 i += 2;
1871 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1873 /* If we are drawing caps, go through the points and adjust them accordingly,
1874 * and draw the caps. */
1875 if(caps){
1876 switch(types[count - 1] & PathPointTypePathTypeMask){
1877 case PathPointTypeBezier:
1878 if(pen->endcap == LineCapArrowAnchor)
1879 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1880 else if((pen->endcap == LineCapCustom) && pen->customend)
1881 shorten_bezier_amt(&ptcopy[count - 4],
1882 pen->width * pen->customend->inset, FALSE);
1884 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1885 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1886 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1887 pt[count - 1].X, pt[count - 1].Y);
1889 break;
1890 case PathPointTypeLine:
1891 if(pen->endcap == LineCapArrowAnchor)
1892 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1893 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1894 pen->width);
1895 else if((pen->endcap == LineCapCustom) && pen->customend)
1896 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1897 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1898 pen->customend->inset * pen->width);
1900 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1901 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1902 pt[count - 1].Y);
1904 break;
1905 default:
1906 ERR("Bad path last point\n");
1907 goto end;
1910 /* Find start of points */
1911 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1912 == PathPointTypeStart); j++);
1914 switch(types[j] & PathPointTypePathTypeMask){
1915 case PathPointTypeBezier:
1916 if(pen->startcap == LineCapArrowAnchor)
1917 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1918 else if((pen->startcap == LineCapCustom) && pen->customstart)
1919 shorten_bezier_amt(&ptcopy[j - 1],
1920 pen->width * pen->customstart->inset, TRUE);
1922 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1923 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1924 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1925 pt[j - 1].X, pt[j - 1].Y);
1927 break;
1928 case PathPointTypeLine:
1929 if(pen->startcap == LineCapArrowAnchor)
1930 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1931 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1932 pen->width);
1933 else if((pen->startcap == LineCapCustom) && pen->customstart)
1934 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1935 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1936 pen->customstart->inset * pen->width);
1938 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1939 pt[j].X, pt[j].Y, pt[j - 1].X,
1940 pt[j - 1].Y);
1942 break;
1943 default:
1944 ERR("Bad path points\n");
1945 goto end;
1949 transform_and_round_points(graphics, pti, ptcopy, count);
1951 for(i = 0; i < count; i++){
1952 tp[i] = convert_path_point_type(types[i]);
1955 PolyDraw(graphics->hdc, pti, tp, count);
1957 status = Ok;
1959 end:
1960 GdipFree(pti);
1961 GdipFree(ptcopy);
1962 GdipFree(tp);
1964 return status;
1967 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1969 GpStatus result;
1971 BeginPath(graphics->hdc);
1972 result = draw_poly(graphics, NULL, path->pathdata.Points,
1973 path->pathdata.Types, path->pathdata.Count, FALSE);
1974 EndPath(graphics->hdc);
1975 return result;
1978 typedef struct _GraphicsContainerItem {
1979 struct list entry;
1980 GraphicsContainer contid;
1982 SmoothingMode smoothing;
1983 CompositingQuality compqual;
1984 InterpolationMode interpolation;
1985 CompositingMode compmode;
1986 TextRenderingHint texthint;
1987 REAL scale;
1988 GpUnit unit;
1989 PixelOffsetMode pixeloffset;
1990 UINT textcontrast;
1991 GpMatrix* worldtrans;
1992 GpRegion* clip;
1993 INT origin_x, origin_y;
1994 } GraphicsContainerItem;
1996 static GpStatus init_container(GraphicsContainerItem** container,
1997 GDIPCONST GpGraphics* graphics){
1998 GpStatus sts;
2000 *container = GdipAlloc(sizeof(GraphicsContainerItem));
2001 if(!(*container))
2002 return OutOfMemory;
2004 (*container)->contid = graphics->contid + 1;
2006 (*container)->smoothing = graphics->smoothing;
2007 (*container)->compqual = graphics->compqual;
2008 (*container)->interpolation = graphics->interpolation;
2009 (*container)->compmode = graphics->compmode;
2010 (*container)->texthint = graphics->texthint;
2011 (*container)->scale = graphics->scale;
2012 (*container)->unit = graphics->unit;
2013 (*container)->textcontrast = graphics->textcontrast;
2014 (*container)->pixeloffset = graphics->pixeloffset;
2015 (*container)->origin_x = graphics->origin_x;
2016 (*container)->origin_y = graphics->origin_y;
2018 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
2019 if(sts != Ok){
2020 GdipFree(*container);
2021 *container = NULL;
2022 return sts;
2025 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2026 if(sts != Ok){
2027 GdipDeleteMatrix((*container)->worldtrans);
2028 GdipFree(*container);
2029 *container = NULL;
2030 return sts;
2033 return Ok;
2036 static void delete_container(GraphicsContainerItem* container){
2037 GdipDeleteMatrix(container->worldtrans);
2038 GdipDeleteRegion(container->clip);
2039 GdipFree(container);
2042 static GpStatus restore_container(GpGraphics* graphics,
2043 GDIPCONST GraphicsContainerItem* container){
2044 GpStatus sts;
2045 GpMatrix *newTrans;
2046 GpRegion *newClip;
2048 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
2049 if(sts != Ok)
2050 return sts;
2052 sts = GdipCloneRegion(container->clip, &newClip);
2053 if(sts != Ok){
2054 GdipDeleteMatrix(newTrans);
2055 return sts;
2058 GdipDeleteMatrix(graphics->worldtrans);
2059 graphics->worldtrans = newTrans;
2061 GdipDeleteRegion(graphics->clip);
2062 graphics->clip = newClip;
2064 graphics->contid = container->contid - 1;
2066 graphics->smoothing = container->smoothing;
2067 graphics->compqual = container->compqual;
2068 graphics->interpolation = container->interpolation;
2069 graphics->compmode = container->compmode;
2070 graphics->texthint = container->texthint;
2071 graphics->scale = container->scale;
2072 graphics->unit = container->unit;
2073 graphics->textcontrast = container->textcontrast;
2074 graphics->pixeloffset = container->pixeloffset;
2075 graphics->origin_x = container->origin_x;
2076 graphics->origin_y = container->origin_y;
2078 return Ok;
2081 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2083 RECT wnd_rect;
2084 GpStatus stat=Ok;
2085 GpUnit unit;
2087 if(graphics->hwnd) {
2088 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2089 return GenericError;
2091 rect->X = wnd_rect.left;
2092 rect->Y = wnd_rect.top;
2093 rect->Width = wnd_rect.right - wnd_rect.left;
2094 rect->Height = wnd_rect.bottom - wnd_rect.top;
2095 }else if (graphics->image){
2096 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2097 if (stat == Ok && unit != UnitPixel)
2098 FIXME("need to convert from unit %i\n", unit);
2099 }else{
2100 rect->X = 0;
2101 rect->Y = 0;
2102 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2103 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2106 return stat;
2109 /* on success, rgn will contain the region of the graphics object which
2110 * is visible after clipping has been applied */
2111 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2113 GpStatus stat;
2114 GpRectF rectf;
2115 GpRegion* tmp;
2117 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2118 return stat;
2120 if((stat = GdipCreateRegion(&tmp)) != Ok)
2121 return stat;
2123 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2124 goto end;
2126 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2127 goto end;
2129 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2131 end:
2132 GdipDeleteRegion(tmp);
2133 return stat;
2136 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2137 GDIPCONST GpStringFormat *format, HFONT *hfont)
2139 HDC hdc = CreateCompatibleDC(0);
2140 GpPointF pt[3];
2141 REAL angle, rel_width, rel_height, font_height, font_to_pixel_scale;
2142 LOGFONTW lfw;
2143 HFONT unscaled_font;
2144 TEXTMETRICW textmet;
2146 font_to_pixel_scale = (format && format->generic_typographic) ? 1.0 : units_scale(UnitPoint, UnitPixel, font->family->dpi);
2148 if (font->unit == UnitPixel)
2149 font_height = font->emSize * font_to_pixel_scale;
2150 else
2152 REAL unit_scale, res;
2154 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2155 unit_scale = units_scale(font->unit, graphics->unit, res);
2157 font_height = font->emSize * font_to_pixel_scale * unit_scale;
2158 if (graphics->unit != UnitDisplay)
2159 font_height /= graphics->scale;
2162 pt[0].X = 0.0;
2163 pt[0].Y = 0.0;
2164 pt[1].X = 1.0;
2165 pt[1].Y = 0.0;
2166 pt[2].X = 0.0;
2167 pt[2].Y = 1.0;
2168 if (graphics)
2169 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2170 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2171 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2172 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2173 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2174 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2176 get_log_fontW(font, graphics, &lfw);
2177 lfw.lfHeight = gdip_round(font_height * rel_height);
2178 unscaled_font = CreateFontIndirectW(&lfw);
2180 SelectObject(hdc, unscaled_font);
2181 GetTextMetricsW(hdc, &textmet);
2183 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2184 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2186 *hfont = CreateFontIndirectW(&lfw);
2188 DeleteDC(hdc);
2189 DeleteObject(unscaled_font);
2192 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2194 TRACE("(%p, %p)\n", hdc, graphics);
2196 return GdipCreateFromHDC2(hdc, NULL, graphics);
2199 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2201 GpStatus retval;
2203 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2205 if(hDevice != NULL) {
2206 FIXME("Don't know how to handle parameter hDevice\n");
2207 return NotImplemented;
2210 if(hdc == NULL)
2211 return OutOfMemory;
2213 if(graphics == NULL)
2214 return InvalidParameter;
2216 *graphics = GdipAlloc(sizeof(GpGraphics));
2217 if(!*graphics) return OutOfMemory;
2219 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2220 GdipFree(*graphics);
2221 return retval;
2224 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2225 GdipFree((*graphics)->worldtrans);
2226 GdipFree(*graphics);
2227 return retval;
2230 (*graphics)->hdc = hdc;
2231 (*graphics)->hwnd = WindowFromDC(hdc);
2232 (*graphics)->owndc = FALSE;
2233 (*graphics)->smoothing = SmoothingModeDefault;
2234 (*graphics)->compqual = CompositingQualityDefault;
2235 (*graphics)->interpolation = InterpolationModeBilinear;
2236 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2237 (*graphics)->compmode = CompositingModeSourceOver;
2238 (*graphics)->unit = UnitDisplay;
2239 (*graphics)->scale = 1.0;
2240 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2241 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2242 (*graphics)->busy = FALSE;
2243 (*graphics)->textcontrast = 4;
2244 list_init(&(*graphics)->containers);
2245 (*graphics)->contid = 0;
2247 TRACE("<-- %p\n", *graphics);
2249 return Ok;
2252 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2254 GpStatus retval;
2256 *graphics = GdipAlloc(sizeof(GpGraphics));
2257 if(!*graphics) return OutOfMemory;
2259 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2260 GdipFree(*graphics);
2261 return retval;
2264 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2265 GdipFree((*graphics)->worldtrans);
2266 GdipFree(*graphics);
2267 return retval;
2270 (*graphics)->hdc = NULL;
2271 (*graphics)->hwnd = NULL;
2272 (*graphics)->owndc = FALSE;
2273 (*graphics)->image = image;
2274 (*graphics)->smoothing = SmoothingModeDefault;
2275 (*graphics)->compqual = CompositingQualityDefault;
2276 (*graphics)->interpolation = InterpolationModeBilinear;
2277 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2278 (*graphics)->compmode = CompositingModeSourceOver;
2279 (*graphics)->unit = UnitDisplay;
2280 (*graphics)->scale = 1.0;
2281 (*graphics)->xres = image->xres;
2282 (*graphics)->yres = image->yres;
2283 (*graphics)->busy = FALSE;
2284 (*graphics)->textcontrast = 4;
2285 list_init(&(*graphics)->containers);
2286 (*graphics)->contid = 0;
2288 TRACE("<-- %p\n", *graphics);
2290 return Ok;
2293 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2295 GpStatus ret;
2296 HDC hdc;
2298 TRACE("(%p, %p)\n", hwnd, graphics);
2300 hdc = GetDC(hwnd);
2302 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2304 ReleaseDC(hwnd, hdc);
2305 return ret;
2308 (*graphics)->hwnd = hwnd;
2309 (*graphics)->owndc = TRUE;
2311 return Ok;
2314 /* FIXME: no icm handling */
2315 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2317 TRACE("(%p, %p)\n", hwnd, graphics);
2319 return GdipCreateFromHWND(hwnd, graphics);
2322 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2323 GpMetafile **metafile)
2325 IStream *stream = NULL;
2326 UINT read;
2327 ENHMETAHEADER *copy;
2328 GpStatus retval = Ok;
2330 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2332 if(!hemf || !metafile)
2333 return InvalidParameter;
2335 read = GetEnhMetaFileBits(hemf, 0, NULL);
2336 copy = GdipAlloc(read);
2337 GetEnhMetaFileBits(hemf, read, (BYTE *)copy);
2339 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
2340 ERR("could not make stream\n");
2341 GdipFree(copy);
2342 retval = GenericError;
2343 goto err;
2346 *metafile = GdipAlloc(sizeof(GpMetafile));
2347 if(!*metafile){
2348 retval = OutOfMemory;
2349 goto err;
2352 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2353 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
2355 retval = GenericError;
2356 goto err;
2360 (*metafile)->image.type = ImageTypeMetafile;
2361 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
2362 (*metafile)->image.palette = NULL;
2363 (*metafile)->image.xres = (REAL)copy->szlDevice.cx;
2364 (*metafile)->image.yres = (REAL)copy->szlDevice.cy;
2365 (*metafile)->bounds.X = (REAL)copy->rclBounds.left;
2366 (*metafile)->bounds.Y = (REAL)copy->rclBounds.top;
2367 (*metafile)->bounds.Width = (REAL)(copy->rclBounds.right - copy->rclBounds.left);
2368 (*metafile)->bounds.Height = (REAL)(copy->rclBounds.bottom - copy->rclBounds.top);
2369 (*metafile)->unit = UnitPixel;
2371 if(delete)
2372 DeleteEnhMetaFile(hemf);
2374 TRACE("<-- %p\n", *metafile);
2376 err:
2377 if (retval != Ok)
2378 GdipFree(*metafile);
2379 IStream_Release(stream);
2380 return retval;
2383 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2384 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2386 UINT read;
2387 BYTE *copy;
2388 HENHMETAFILE hemf;
2389 GpStatus retval = Ok;
2391 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2393 if(!hwmf || !metafile || !placeable)
2394 return InvalidParameter;
2396 *metafile = NULL;
2397 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2398 if(!read)
2399 return GenericError;
2400 copy = GdipAlloc(read);
2401 GetMetaFileBitsEx(hwmf, read, copy);
2403 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2404 GdipFree(copy);
2406 retval = GdipCreateMetafileFromEmf(hemf, FALSE, metafile);
2408 if (retval == Ok)
2410 (*metafile)->image.xres = (REAL)placeable->Inch;
2411 (*metafile)->image.yres = (REAL)placeable->Inch;
2412 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2413 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2414 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2415 placeable->BoundingBox.Left);
2416 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2417 placeable->BoundingBox.Top);
2419 if (delete) DeleteMetaFile(hwmf);
2421 return retval;
2424 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2425 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2427 HMETAFILE hmf = GetMetaFileW(file);
2429 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2431 if(!hmf) return InvalidParameter;
2433 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2436 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2437 GpMetafile **metafile)
2439 FIXME("(%p, %p): stub\n", file, metafile);
2440 return NotImplemented;
2443 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2444 GpMetafile **metafile)
2446 FIXME("(%p, %p): stub\n", stream, metafile);
2447 return NotImplemented;
2450 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2451 UINT access, IStream **stream)
2453 DWORD dwMode;
2454 HRESULT ret;
2456 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2458 if(!stream || !filename)
2459 return InvalidParameter;
2461 if(access & GENERIC_WRITE)
2462 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2463 else if(access & GENERIC_READ)
2464 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2465 else
2466 return InvalidParameter;
2468 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2470 return hresult_to_status(ret);
2473 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2475 GraphicsContainerItem *cont, *next;
2476 GpStatus stat;
2477 TRACE("(%p)\n", graphics);
2479 if(!graphics) return InvalidParameter;
2480 if(graphics->busy) return ObjectBusy;
2482 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2484 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2485 if (stat != Ok)
2486 return stat;
2489 if(graphics->owndc)
2490 ReleaseDC(graphics->hwnd, graphics->hdc);
2492 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2493 list_remove(&cont->entry);
2494 delete_container(cont);
2497 GdipDeleteRegion(graphics->clip);
2498 GdipDeleteMatrix(graphics->worldtrans);
2499 GdipFree(graphics);
2501 return Ok;
2504 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2505 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2507 INT save_state, num_pts;
2508 GpPointF points[MAX_ARC_PTS];
2509 GpStatus retval;
2511 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2512 width, height, startAngle, sweepAngle);
2514 if(!graphics || !pen || width <= 0 || height <= 0)
2515 return InvalidParameter;
2517 if(graphics->busy)
2518 return ObjectBusy;
2520 if (!graphics->hdc)
2522 FIXME("graphics object has no HDC\n");
2523 return Ok;
2526 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2528 save_state = prepare_dc(graphics, pen);
2530 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2532 restore_dc(graphics, save_state);
2534 return retval;
2537 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2538 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2540 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2541 width, height, startAngle, sweepAngle);
2543 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2546 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2547 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2549 INT save_state;
2550 GpPointF pt[4];
2551 GpStatus retval;
2553 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2554 x2, y2, x3, y3, x4, y4);
2556 if(!graphics || !pen)
2557 return InvalidParameter;
2559 if(graphics->busy)
2560 return ObjectBusy;
2562 if (!graphics->hdc)
2564 FIXME("graphics object has no HDC\n");
2565 return Ok;
2568 pt[0].X = x1;
2569 pt[0].Y = y1;
2570 pt[1].X = x2;
2571 pt[1].Y = y2;
2572 pt[2].X = x3;
2573 pt[2].Y = y3;
2574 pt[3].X = x4;
2575 pt[3].Y = y4;
2577 save_state = prepare_dc(graphics, pen);
2579 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2581 restore_dc(graphics, save_state);
2583 return retval;
2586 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2587 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2589 INT save_state;
2590 GpPointF pt[4];
2591 GpStatus retval;
2593 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2594 x2, y2, x3, y3, x4, y4);
2596 if(!graphics || !pen)
2597 return InvalidParameter;
2599 if(graphics->busy)
2600 return ObjectBusy;
2602 if (!graphics->hdc)
2604 FIXME("graphics object has no HDC\n");
2605 return Ok;
2608 pt[0].X = x1;
2609 pt[0].Y = y1;
2610 pt[1].X = x2;
2611 pt[1].Y = y2;
2612 pt[2].X = x3;
2613 pt[2].Y = y3;
2614 pt[3].X = x4;
2615 pt[3].Y = y4;
2617 save_state = prepare_dc(graphics, pen);
2619 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2621 restore_dc(graphics, save_state);
2623 return retval;
2626 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2627 GDIPCONST GpPointF *points, INT count)
2629 INT i;
2630 GpStatus ret;
2632 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2634 if(!graphics || !pen || !points || (count <= 0))
2635 return InvalidParameter;
2637 if(graphics->busy)
2638 return ObjectBusy;
2640 for(i = 0; i < floor(count / 4); i++){
2641 ret = GdipDrawBezier(graphics, pen,
2642 points[4*i].X, points[4*i].Y,
2643 points[4*i + 1].X, points[4*i + 1].Y,
2644 points[4*i + 2].X, points[4*i + 2].Y,
2645 points[4*i + 3].X, points[4*i + 3].Y);
2646 if(ret != Ok)
2647 return ret;
2650 return Ok;
2653 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2654 GDIPCONST GpPoint *points, INT count)
2656 GpPointF *pts;
2657 GpStatus ret;
2658 INT i;
2660 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2662 if(!graphics || !pen || !points || (count <= 0))
2663 return InvalidParameter;
2665 if(graphics->busy)
2666 return ObjectBusy;
2668 pts = GdipAlloc(sizeof(GpPointF) * count);
2669 if(!pts)
2670 return OutOfMemory;
2672 for(i = 0; i < count; i++){
2673 pts[i].X = (REAL)points[i].X;
2674 pts[i].Y = (REAL)points[i].Y;
2677 ret = GdipDrawBeziers(graphics,pen,pts,count);
2679 GdipFree(pts);
2681 return ret;
2684 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2685 GDIPCONST GpPointF *points, INT count)
2687 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2689 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2692 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2693 GDIPCONST GpPoint *points, INT count)
2695 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2697 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2700 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2701 GDIPCONST GpPointF *points, INT count, REAL tension)
2703 GpPath *path;
2704 GpStatus stat;
2706 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2708 if(!graphics || !pen || !points || count <= 0)
2709 return InvalidParameter;
2711 if(graphics->busy)
2712 return ObjectBusy;
2714 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2715 return stat;
2717 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2718 if(stat != Ok){
2719 GdipDeletePath(path);
2720 return stat;
2723 stat = GdipDrawPath(graphics, pen, path);
2725 GdipDeletePath(path);
2727 return stat;
2730 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2731 GDIPCONST GpPoint *points, INT count, REAL tension)
2733 GpPointF *ptf;
2734 GpStatus stat;
2735 INT i;
2737 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2739 if(!points || count <= 0)
2740 return InvalidParameter;
2742 ptf = GdipAlloc(sizeof(GpPointF)*count);
2743 if(!ptf)
2744 return OutOfMemory;
2746 for(i = 0; i < count; i++){
2747 ptf[i].X = (REAL)points[i].X;
2748 ptf[i].Y = (REAL)points[i].Y;
2751 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2753 GdipFree(ptf);
2755 return stat;
2758 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2759 GDIPCONST GpPointF *points, INT count)
2761 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2763 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2766 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2767 GDIPCONST GpPoint *points, INT count)
2769 GpPointF *pointsF;
2770 GpStatus ret;
2771 INT i;
2773 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2775 if(!points)
2776 return InvalidParameter;
2778 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2779 if(!pointsF)
2780 return OutOfMemory;
2782 for(i = 0; i < count; i++){
2783 pointsF[i].X = (REAL)points[i].X;
2784 pointsF[i].Y = (REAL)points[i].Y;
2787 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2788 GdipFree(pointsF);
2790 return ret;
2793 /* Approximates cardinal spline with Bezier curves. */
2794 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2795 GDIPCONST GpPointF *points, INT count, REAL tension)
2797 /* PolyBezier expects count*3-2 points. */
2798 INT i, len_pt = count*3-2, save_state;
2799 GpPointF *pt;
2800 REAL x1, x2, y1, y2;
2801 GpStatus retval;
2803 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2805 if(!graphics || !pen)
2806 return InvalidParameter;
2808 if(graphics->busy)
2809 return ObjectBusy;
2811 if(count < 2)
2812 return InvalidParameter;
2814 if (!graphics->hdc)
2816 FIXME("graphics object has no HDC\n");
2817 return Ok;
2820 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2821 if(!pt)
2822 return OutOfMemory;
2824 tension = tension * TENSION_CONST;
2826 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2827 tension, &x1, &y1);
2829 pt[0].X = points[0].X;
2830 pt[0].Y = points[0].Y;
2831 pt[1].X = x1;
2832 pt[1].Y = y1;
2834 for(i = 0; i < count-2; i++){
2835 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2837 pt[3*i+2].X = x1;
2838 pt[3*i+2].Y = y1;
2839 pt[3*i+3].X = points[i+1].X;
2840 pt[3*i+3].Y = points[i+1].Y;
2841 pt[3*i+4].X = x2;
2842 pt[3*i+4].Y = y2;
2845 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2846 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2848 pt[len_pt-2].X = x1;
2849 pt[len_pt-2].Y = y1;
2850 pt[len_pt-1].X = points[count-1].X;
2851 pt[len_pt-1].Y = points[count-1].Y;
2853 save_state = prepare_dc(graphics, pen);
2855 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2857 GdipFree(pt);
2858 restore_dc(graphics, save_state);
2860 return retval;
2863 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2864 GDIPCONST GpPoint *points, INT count, REAL tension)
2866 GpPointF *pointsF;
2867 GpStatus ret;
2868 INT i;
2870 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2872 if(!points)
2873 return InvalidParameter;
2875 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2876 if(!pointsF)
2877 return OutOfMemory;
2879 for(i = 0; i < count; i++){
2880 pointsF[i].X = (REAL)points[i].X;
2881 pointsF[i].Y = (REAL)points[i].Y;
2884 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2885 GdipFree(pointsF);
2887 return ret;
2890 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2891 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2892 REAL tension)
2894 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2896 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2897 return InvalidParameter;
2900 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2903 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2904 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2905 REAL tension)
2907 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2909 if(count < 0){
2910 return OutOfMemory;
2913 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2914 return InvalidParameter;
2917 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2920 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2921 REAL y, REAL width, REAL height)
2923 INT save_state;
2924 GpPointF ptf[2];
2925 POINT pti[2];
2927 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2929 if(!graphics || !pen)
2930 return InvalidParameter;
2932 if(graphics->busy)
2933 return ObjectBusy;
2935 if (!graphics->hdc)
2937 FIXME("graphics object has no HDC\n");
2938 return Ok;
2941 ptf[0].X = x;
2942 ptf[0].Y = y;
2943 ptf[1].X = x + width;
2944 ptf[1].Y = y + height;
2946 save_state = prepare_dc(graphics, pen);
2947 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2949 transform_and_round_points(graphics, pti, ptf, 2);
2951 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2953 restore_dc(graphics, save_state);
2955 return Ok;
2958 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2959 INT y, INT width, INT height)
2961 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2963 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2967 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2969 UINT width, height;
2971 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2973 if(!graphics || !image)
2974 return InvalidParameter;
2976 GdipGetImageWidth(image, &width);
2977 GdipGetImageHeight(image, &height);
2979 return GdipDrawImagePointRect(graphics, image, x, y,
2980 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2983 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2984 INT y)
2986 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2988 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2991 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2992 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2993 GpUnit srcUnit)
2995 GpPointF points[3];
2996 REAL scale_x, scale_y, width, height;
2998 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3000 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
3001 scale_x *= graphics->xres / image->xres;
3002 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
3003 scale_y *= graphics->yres / image->yres;
3004 width = srcwidth * scale_x;
3005 height = srcheight * scale_y;
3007 points[0].X = points[2].X = x;
3008 points[0].Y = points[1].Y = y;
3009 points[1].X = x + width;
3010 points[2].Y = y + height;
3012 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3013 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3016 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3017 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3018 GpUnit srcUnit)
3020 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3023 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3024 GDIPCONST GpPointF *dstpoints, INT count)
3026 UINT width, height;
3028 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3030 if(!image)
3031 return InvalidParameter;
3033 GdipGetImageWidth(image, &width);
3034 GdipGetImageHeight(image, &height);
3036 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3037 width, height, UnitPixel, NULL, NULL, NULL);
3040 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3041 GDIPCONST GpPoint *dstpoints, INT count)
3043 GpPointF ptf[3];
3045 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3047 if (count != 3 || !dstpoints)
3048 return InvalidParameter;
3050 ptf[0].X = (REAL)dstpoints[0].X;
3051 ptf[0].Y = (REAL)dstpoints[0].Y;
3052 ptf[1].X = (REAL)dstpoints[1].X;
3053 ptf[1].Y = (REAL)dstpoints[1].Y;
3054 ptf[2].X = (REAL)dstpoints[2].X;
3055 ptf[2].Y = (REAL)dstpoints[2].Y;
3057 return GdipDrawImagePoints(graphics, image, ptf, count);
3060 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3061 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3062 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3063 DrawImageAbort callback, VOID * callbackData)
3065 GpPointF ptf[4];
3066 POINT pti[4];
3067 GpStatus stat;
3069 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3070 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3071 callbackData);
3073 if (count > 3)
3074 return NotImplemented;
3076 if(!graphics || !image || !points || count != 3)
3077 return InvalidParameter;
3079 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3080 debugstr_pointf(&points[2]));
3082 memcpy(ptf, points, 3 * sizeof(GpPointF));
3083 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3084 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3085 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3086 return Ok;
3087 transform_and_round_points(graphics, pti, ptf, 4);
3089 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3090 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3092 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3093 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3094 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3095 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3096 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3098 if (image->picture)
3100 if (!graphics->hdc)
3102 FIXME("graphics object has no HDC\n");
3105 if(IPicture_Render(image->picture, graphics->hdc,
3106 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3107 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3109 if(callback)
3110 callback(callbackData);
3111 return GenericError;
3114 else if (image->type == ImageTypeBitmap)
3116 GpBitmap* bitmap = (GpBitmap*)image;
3117 int use_software=0;
3119 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3120 graphics->xres, graphics->yres,
3121 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3122 graphics->scale, image->xres, image->yres, bitmap->format,
3123 imageAttributes ? imageAttributes->outside_color : 0);
3125 if (imageAttributes ||
3126 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3127 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3128 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3129 srcx < 0 || srcy < 0 ||
3130 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3131 use_software = 1;
3133 if (use_software)
3135 RECT dst_area;
3136 GpRect src_area;
3137 int i, x, y, src_stride, dst_stride;
3138 GpMatrix *dst_to_src;
3139 REAL m11, m12, m21, m22, mdx, mdy;
3140 LPBYTE src_data, dst_data;
3141 BitmapData lockeddata;
3142 InterpolationMode interpolation = graphics->interpolation;
3143 PixelOffsetMode offset_mode = graphics->pixeloffset;
3144 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3145 REAL x_dx, x_dy, y_dx, y_dy;
3146 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3148 if (!imageAttributes)
3149 imageAttributes = &defaultImageAttributes;
3151 dst_area.left = dst_area.right = pti[0].x;
3152 dst_area.top = dst_area.bottom = pti[0].y;
3153 for (i=1; i<4; i++)
3155 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3156 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3157 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3158 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3161 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3163 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3164 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3165 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3166 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3167 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3168 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3170 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
3171 if (stat != Ok) return stat;
3173 stat = GdipInvertMatrix(dst_to_src);
3174 if (stat != Ok)
3176 GdipDeleteMatrix(dst_to_src);
3177 return stat;
3180 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3181 if (!dst_data)
3183 GdipDeleteMatrix(dst_to_src);
3184 return OutOfMemory;
3187 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3189 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3190 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3192 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3194 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3195 if (!src_data)
3197 GdipFree(dst_data);
3198 GdipDeleteMatrix(dst_to_src);
3199 return OutOfMemory;
3201 src_stride = sizeof(ARGB) * src_area.Width;
3203 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3204 lockeddata.Width = src_area.Width;
3205 lockeddata.Height = src_area.Height;
3206 lockeddata.Stride = src_stride;
3207 lockeddata.PixelFormat = PixelFormat32bppARGB;
3208 lockeddata.Scan0 = src_data;
3210 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3211 PixelFormat32bppARGB, &lockeddata);
3213 if (stat == Ok)
3214 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3216 if (stat != Ok)
3218 if (src_data != dst_data)
3219 GdipFree(src_data);
3220 GdipFree(dst_data);
3221 GdipDeleteMatrix(dst_to_src);
3222 return stat;
3225 apply_image_attributes(imageAttributes, src_data,
3226 src_area.Width, src_area.Height,
3227 src_stride, ColorAdjustTypeBitmap);
3229 /* Transform the bits as needed to the destination. */
3230 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
3232 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3233 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3234 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3235 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3237 for (x=dst_area.left; x<dst_area.right; x++)
3239 for (y=dst_area.top; y<dst_area.bottom; y++)
3241 GpPointF src_pointf;
3242 ARGB *dst_color;
3244 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3245 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3247 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3249 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3250 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3251 imageAttributes, interpolation, offset_mode);
3252 else
3253 *dst_color = 0;
3257 GdipDeleteMatrix(dst_to_src);
3259 GdipFree(src_data);
3261 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3262 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3264 GdipFree(dst_data);
3266 return stat;
3268 else
3270 HDC hdc;
3271 int temp_hdc=0, temp_bitmap=0;
3272 HBITMAP hbitmap, old_hbm=NULL;
3274 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3275 bitmap->format == PixelFormat24bppRGB ||
3276 bitmap->format == PixelFormat32bppRGB ||
3277 bitmap->format == PixelFormat32bppPARGB))
3279 BITMAPINFOHEADER bih;
3280 BYTE *temp_bits;
3281 PixelFormat dst_format;
3283 /* we can't draw a bitmap of this format directly */
3284 hdc = CreateCompatibleDC(0);
3285 temp_hdc = 1;
3286 temp_bitmap = 1;
3288 bih.biSize = sizeof(BITMAPINFOHEADER);
3289 bih.biWidth = bitmap->width;
3290 bih.biHeight = -bitmap->height;
3291 bih.biPlanes = 1;
3292 bih.biBitCount = 32;
3293 bih.biCompression = BI_RGB;
3294 bih.biSizeImage = 0;
3295 bih.biXPelsPerMeter = 0;
3296 bih.biYPelsPerMeter = 0;
3297 bih.biClrUsed = 0;
3298 bih.biClrImportant = 0;
3300 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3301 (void**)&temp_bits, NULL, 0);
3303 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3304 dst_format = PixelFormat32bppPARGB;
3305 else
3306 dst_format = PixelFormat32bppRGB;
3308 convert_pixels(bitmap->width, bitmap->height,
3309 bitmap->width*4, temp_bits, dst_format,
3310 bitmap->stride, bitmap->bits, bitmap->format,
3311 bitmap->image.palette);
3313 else
3315 if (bitmap->hbitmap)
3316 hbitmap = bitmap->hbitmap;
3317 else
3319 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3320 temp_bitmap = 1;
3323 hdc = bitmap->hdc;
3324 temp_hdc = (hdc == 0);
3327 if (temp_hdc)
3329 if (!hdc) hdc = CreateCompatibleDC(0);
3330 old_hbm = SelectObject(hdc, hbitmap);
3333 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3335 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3336 hdc, srcx, srcy, srcwidth, srcheight);
3338 else
3340 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3341 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3344 if (temp_hdc)
3346 SelectObject(hdc, old_hbm);
3347 DeleteDC(hdc);
3350 if (temp_bitmap)
3351 DeleteObject(hbitmap);
3354 else
3356 ERR("GpImage with no IPicture or HBITMAP?!\n");
3357 return NotImplemented;
3360 return Ok;
3363 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3364 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3365 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3366 DrawImageAbort callback, VOID * callbackData)
3368 GpPointF pointsF[3];
3369 INT i;
3371 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3372 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3373 callbackData);
3375 if(!points || count!=3)
3376 return InvalidParameter;
3378 for(i = 0; i < count; i++){
3379 pointsF[i].X = (REAL)points[i].X;
3380 pointsF[i].Y = (REAL)points[i].Y;
3383 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3384 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3385 callback, callbackData);
3388 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3389 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3390 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3391 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3392 VOID * callbackData)
3394 GpPointF points[3];
3396 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3397 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3398 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3400 points[0].X = dstx;
3401 points[0].Y = dsty;
3402 points[1].X = dstx + dstwidth;
3403 points[1].Y = dsty;
3404 points[2].X = dstx;
3405 points[2].Y = dsty + dstheight;
3407 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3408 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3411 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3412 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3413 INT srcwidth, INT srcheight, GpUnit srcUnit,
3414 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3415 VOID * callbackData)
3417 GpPointF points[3];
3419 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3420 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3421 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3423 points[0].X = dstx;
3424 points[0].Y = dsty;
3425 points[1].X = dstx + dstwidth;
3426 points[1].Y = dsty;
3427 points[2].X = dstx;
3428 points[2].Y = dsty + dstheight;
3430 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3431 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3434 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3435 REAL x, REAL y, REAL width, REAL height)
3437 RectF bounds;
3438 GpUnit unit;
3439 GpStatus ret;
3441 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3443 if(!graphics || !image)
3444 return InvalidParameter;
3446 ret = GdipGetImageBounds(image, &bounds, &unit);
3447 if(ret != Ok)
3448 return ret;
3450 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3451 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3452 unit, NULL, NULL, NULL);
3455 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3456 INT x, INT y, INT width, INT height)
3458 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3460 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3463 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3464 REAL y1, REAL x2, REAL y2)
3466 INT save_state;
3467 GpPointF pt[2];
3468 GpStatus retval;
3470 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3472 if(!pen || !graphics)
3473 return InvalidParameter;
3475 if(graphics->busy)
3476 return ObjectBusy;
3478 if (!graphics->hdc)
3480 FIXME("graphics object has no HDC\n");
3481 return Ok;
3484 pt[0].X = x1;
3485 pt[0].Y = y1;
3486 pt[1].X = x2;
3487 pt[1].Y = y2;
3489 save_state = prepare_dc(graphics, pen);
3491 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3493 restore_dc(graphics, save_state);
3495 return retval;
3498 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3499 INT y1, INT x2, INT y2)
3501 INT save_state;
3502 GpPointF pt[2];
3503 GpStatus retval;
3505 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3507 if(!pen || !graphics)
3508 return InvalidParameter;
3510 if(graphics->busy)
3511 return ObjectBusy;
3513 if (!graphics->hdc)
3515 FIXME("graphics object has no HDC\n");
3516 return Ok;
3519 pt[0].X = (REAL)x1;
3520 pt[0].Y = (REAL)y1;
3521 pt[1].X = (REAL)x2;
3522 pt[1].Y = (REAL)y2;
3524 save_state = prepare_dc(graphics, pen);
3526 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3528 restore_dc(graphics, save_state);
3530 return retval;
3533 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3534 GpPointF *points, INT count)
3536 INT save_state;
3537 GpStatus retval;
3539 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3541 if(!pen || !graphics || (count < 2))
3542 return InvalidParameter;
3544 if(graphics->busy)
3545 return ObjectBusy;
3547 if (!graphics->hdc)
3549 FIXME("graphics object has no HDC\n");
3550 return Ok;
3553 save_state = prepare_dc(graphics, pen);
3555 retval = draw_polyline(graphics, pen, points, count, TRUE);
3557 restore_dc(graphics, save_state);
3559 return retval;
3562 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3563 GpPoint *points, INT count)
3565 INT save_state;
3566 GpStatus retval;
3567 GpPointF *ptf = NULL;
3568 int i;
3570 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3572 if(!pen || !graphics || (count < 2))
3573 return InvalidParameter;
3575 if(graphics->busy)
3576 return ObjectBusy;
3578 if (!graphics->hdc)
3580 FIXME("graphics object has no HDC\n");
3581 return Ok;
3584 ptf = GdipAlloc(count * sizeof(GpPointF));
3585 if(!ptf) return OutOfMemory;
3587 for(i = 0; i < count; i ++){
3588 ptf[i].X = (REAL) points[i].X;
3589 ptf[i].Y = (REAL) points[i].Y;
3592 save_state = prepare_dc(graphics, pen);
3594 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3596 restore_dc(graphics, save_state);
3598 GdipFree(ptf);
3599 return retval;
3602 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3604 INT save_state;
3605 GpStatus retval;
3607 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3609 if(!pen || !graphics)
3610 return InvalidParameter;
3612 if(graphics->busy)
3613 return ObjectBusy;
3615 if (!graphics->hdc)
3617 FIXME("graphics object has no HDC\n");
3618 return Ok;
3621 save_state = prepare_dc(graphics, pen);
3623 retval = draw_poly(graphics, pen, path->pathdata.Points,
3624 path->pathdata.Types, path->pathdata.Count, TRUE);
3626 restore_dc(graphics, save_state);
3628 return retval;
3631 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3632 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3634 INT save_state;
3636 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3637 width, height, startAngle, sweepAngle);
3639 if(!graphics || !pen)
3640 return InvalidParameter;
3642 if(graphics->busy)
3643 return ObjectBusy;
3645 if (!graphics->hdc)
3647 FIXME("graphics object has no HDC\n");
3648 return Ok;
3651 save_state = prepare_dc(graphics, pen);
3652 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3654 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3656 restore_dc(graphics, save_state);
3658 return Ok;
3661 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3662 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3664 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3665 width, height, startAngle, sweepAngle);
3667 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3670 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3671 REAL y, REAL width, REAL height)
3673 INT save_state;
3674 GpPointF ptf[4];
3675 POINT pti[4];
3677 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3679 if(!pen || !graphics)
3680 return InvalidParameter;
3682 if(graphics->busy)
3683 return ObjectBusy;
3685 if (!graphics->hdc)
3687 FIXME("graphics object has no HDC\n");
3688 return Ok;
3691 ptf[0].X = x;
3692 ptf[0].Y = y;
3693 ptf[1].X = x + width;
3694 ptf[1].Y = y;
3695 ptf[2].X = x + width;
3696 ptf[2].Y = y + height;
3697 ptf[3].X = x;
3698 ptf[3].Y = y + height;
3700 save_state = prepare_dc(graphics, pen);
3701 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3703 transform_and_round_points(graphics, pti, ptf, 4);
3704 Polygon(graphics->hdc, pti, 4);
3706 restore_dc(graphics, save_state);
3708 return Ok;
3711 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3712 INT y, INT width, INT height)
3714 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3716 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3719 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3720 GDIPCONST GpRectF* rects, INT count)
3722 GpPointF *ptf;
3723 POINT *pti;
3724 INT save_state, i;
3726 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3728 if(!graphics || !pen || !rects || count < 1)
3729 return InvalidParameter;
3731 if(graphics->busy)
3732 return ObjectBusy;
3734 if (!graphics->hdc)
3736 FIXME("graphics object has no HDC\n");
3737 return Ok;
3740 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3741 pti = GdipAlloc(4 * count * sizeof(POINT));
3743 if(!ptf || !pti){
3744 GdipFree(ptf);
3745 GdipFree(pti);
3746 return OutOfMemory;
3749 for(i = 0; i < count; i++){
3750 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3751 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3752 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3753 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3756 save_state = prepare_dc(graphics, pen);
3757 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3759 transform_and_round_points(graphics, pti, ptf, 4 * count);
3761 for(i = 0; i < count; i++)
3762 Polygon(graphics->hdc, &pti[4 * i], 4);
3764 restore_dc(graphics, save_state);
3766 GdipFree(ptf);
3767 GdipFree(pti);
3769 return Ok;
3772 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3773 GDIPCONST GpRect* rects, INT count)
3775 GpRectF *rectsF;
3776 GpStatus ret;
3777 INT i;
3779 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3781 if(!rects || count<=0)
3782 return InvalidParameter;
3784 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3785 if(!rectsF)
3786 return OutOfMemory;
3788 for(i = 0;i < count;i++){
3789 rectsF[i].X = (REAL)rects[i].X;
3790 rectsF[i].Y = (REAL)rects[i].Y;
3791 rectsF[i].Width = (REAL)rects[i].Width;
3792 rectsF[i].Height = (REAL)rects[i].Height;
3795 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3796 GdipFree(rectsF);
3798 return ret;
3801 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3802 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3804 GpPath *path;
3805 GpStatus stat;
3807 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3808 count, tension, fill);
3810 if(!graphics || !brush || !points)
3811 return InvalidParameter;
3813 if(graphics->busy)
3814 return ObjectBusy;
3816 if(count == 1) /* Do nothing */
3817 return Ok;
3819 stat = GdipCreatePath(fill, &path);
3820 if(stat != Ok)
3821 return stat;
3823 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3824 if(stat != Ok){
3825 GdipDeletePath(path);
3826 return stat;
3829 stat = GdipFillPath(graphics, brush, path);
3830 if(stat != Ok){
3831 GdipDeletePath(path);
3832 return stat;
3835 GdipDeletePath(path);
3837 return Ok;
3840 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3841 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3843 GpPointF *ptf;
3844 GpStatus stat;
3845 INT i;
3847 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3848 count, tension, fill);
3850 if(!points || count == 0)
3851 return InvalidParameter;
3853 if(count == 1) /* Do nothing */
3854 return Ok;
3856 ptf = GdipAlloc(sizeof(GpPointF)*count);
3857 if(!ptf)
3858 return OutOfMemory;
3860 for(i = 0;i < count;i++){
3861 ptf[i].X = (REAL)points[i].X;
3862 ptf[i].Y = (REAL)points[i].Y;
3865 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3867 GdipFree(ptf);
3869 return stat;
3872 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3873 GDIPCONST GpPointF *points, INT count)
3875 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3876 return GdipFillClosedCurve2(graphics, brush, points, count,
3877 0.5f, FillModeAlternate);
3880 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3881 GDIPCONST GpPoint *points, INT count)
3883 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3884 return GdipFillClosedCurve2I(graphics, brush, points, count,
3885 0.5f, FillModeAlternate);
3888 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3889 REAL y, REAL width, REAL height)
3891 GpStatus stat;
3892 GpPath *path;
3894 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3896 if(!graphics || !brush)
3897 return InvalidParameter;
3899 if(graphics->busy)
3900 return ObjectBusy;
3902 stat = GdipCreatePath(FillModeAlternate, &path);
3904 if (stat == Ok)
3906 stat = GdipAddPathEllipse(path, x, y, width, height);
3908 if (stat == Ok)
3909 stat = GdipFillPath(graphics, brush, path);
3911 GdipDeletePath(path);
3914 return stat;
3917 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3918 INT y, INT width, INT height)
3920 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3922 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3925 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3927 INT save_state;
3928 GpStatus retval;
3930 if(!graphics->hdc || !brush_can_fill_path(brush))
3931 return NotImplemented;
3933 save_state = SaveDC(graphics->hdc);
3934 EndPath(graphics->hdc);
3935 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3936 : WINDING));
3938 BeginPath(graphics->hdc);
3939 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3940 path->pathdata.Types, path->pathdata.Count, FALSE);
3942 if(retval != Ok)
3943 goto end;
3945 EndPath(graphics->hdc);
3946 brush_fill_path(graphics, brush);
3948 retval = Ok;
3950 end:
3951 RestoreDC(graphics->hdc, save_state);
3953 return retval;
3956 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3958 GpStatus stat;
3959 GpRegion *rgn;
3961 if (!brush_can_fill_pixels(brush))
3962 return NotImplemented;
3964 /* FIXME: This could probably be done more efficiently without regions. */
3966 stat = GdipCreateRegionPath(path, &rgn);
3968 if (stat == Ok)
3970 stat = GdipFillRegion(graphics, brush, rgn);
3972 GdipDeleteRegion(rgn);
3975 return stat;
3978 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3980 GpStatus stat = NotImplemented;
3982 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3984 if(!brush || !graphics || !path)
3985 return InvalidParameter;
3987 if(graphics->busy)
3988 return ObjectBusy;
3990 if (!graphics->image)
3991 stat = GDI32_GdipFillPath(graphics, brush, path);
3993 if (stat == NotImplemented)
3994 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3996 if (stat == NotImplemented)
3998 FIXME("Not implemented for brushtype %i\n", brush->bt);
3999 stat = Ok;
4002 return stat;
4005 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4006 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4008 GpStatus stat;
4009 GpPath *path;
4011 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4012 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4014 if(!graphics || !brush)
4015 return InvalidParameter;
4017 if(graphics->busy)
4018 return ObjectBusy;
4020 stat = GdipCreatePath(FillModeAlternate, &path);
4022 if (stat == Ok)
4024 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4026 if (stat == Ok)
4027 stat = GdipFillPath(graphics, brush, path);
4029 GdipDeletePath(path);
4032 return stat;
4035 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4036 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4038 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4039 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4041 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4044 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4045 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4047 GpStatus stat;
4048 GpPath *path;
4050 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4052 if(!graphics || !brush || !points || !count)
4053 return InvalidParameter;
4055 if(graphics->busy)
4056 return ObjectBusy;
4058 stat = GdipCreatePath(fillMode, &path);
4060 if (stat == Ok)
4062 stat = GdipAddPathPolygon(path, points, count);
4064 if (stat == Ok)
4065 stat = GdipFillPath(graphics, brush, path);
4067 GdipDeletePath(path);
4070 return stat;
4073 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4074 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4076 GpStatus stat;
4077 GpPath *path;
4079 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4081 if(!graphics || !brush || !points || !count)
4082 return InvalidParameter;
4084 if(graphics->busy)
4085 return ObjectBusy;
4087 stat = GdipCreatePath(fillMode, &path);
4089 if (stat == Ok)
4091 stat = GdipAddPathPolygonI(path, points, count);
4093 if (stat == Ok)
4094 stat = GdipFillPath(graphics, brush, path);
4096 GdipDeletePath(path);
4099 return stat;
4102 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4103 GDIPCONST GpPointF *points, INT count)
4105 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4107 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4110 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4111 GDIPCONST GpPoint *points, INT count)
4113 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4115 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4118 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4119 REAL x, REAL y, REAL width, REAL height)
4121 GpStatus stat;
4122 GpPath *path;
4124 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4126 if(!graphics || !brush)
4127 return InvalidParameter;
4129 if(graphics->busy)
4130 return ObjectBusy;
4132 stat = GdipCreatePath(FillModeAlternate, &path);
4134 if (stat == Ok)
4136 stat = GdipAddPathRectangle(path, x, y, width, height);
4138 if (stat == Ok)
4139 stat = GdipFillPath(graphics, brush, path);
4141 GdipDeletePath(path);
4144 return stat;
4147 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4148 INT x, INT y, INT width, INT height)
4150 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4152 return GdipFillRectangle(graphics, brush, x, y, width, height);
4155 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4156 INT count)
4158 GpStatus ret;
4159 INT i;
4161 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4163 if(!rects)
4164 return InvalidParameter;
4166 for(i = 0; i < count; i++){
4167 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4168 if(ret != Ok) return ret;
4171 return Ok;
4174 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4175 INT count)
4177 GpRectF *rectsF;
4178 GpStatus ret;
4179 INT i;
4181 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4183 if(!rects || count <= 0)
4184 return InvalidParameter;
4186 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4187 if(!rectsF)
4188 return OutOfMemory;
4190 for(i = 0; i < count; i++){
4191 rectsF[i].X = (REAL)rects[i].X;
4192 rectsF[i].Y = (REAL)rects[i].Y;
4193 rectsF[i].X = (REAL)rects[i].Width;
4194 rectsF[i].Height = (REAL)rects[i].Height;
4197 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4198 GdipFree(rectsF);
4200 return ret;
4203 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4204 GpRegion* region)
4206 INT save_state;
4207 GpStatus status;
4208 HRGN hrgn;
4209 RECT rc;
4211 if(!graphics->hdc || !brush_can_fill_path(brush))
4212 return NotImplemented;
4214 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4215 if(status != Ok)
4216 return status;
4218 save_state = SaveDC(graphics->hdc);
4219 EndPath(graphics->hdc);
4221 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4223 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4225 BeginPath(graphics->hdc);
4226 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4227 EndPath(graphics->hdc);
4229 brush_fill_path(graphics, brush);
4232 RestoreDC(graphics->hdc, save_state);
4234 DeleteObject(hrgn);
4236 return Ok;
4239 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4240 GpRegion* region)
4242 GpStatus stat;
4243 GpRegion *temp_region;
4244 GpMatrix *world_to_device;
4245 GpRectF graphics_bounds;
4246 DWORD *pixel_data;
4247 HRGN hregion;
4248 RECT bound_rect;
4249 GpRect gp_bound_rect;
4251 if (!brush_can_fill_pixels(brush))
4252 return NotImplemented;
4254 stat = get_graphics_bounds(graphics, &graphics_bounds);
4256 if (stat == Ok)
4257 stat = GdipCloneRegion(region, &temp_region);
4259 if (stat == Ok)
4261 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4262 CoordinateSpaceWorld, &world_to_device);
4264 if (stat == Ok)
4266 stat = GdipTransformRegion(temp_region, world_to_device);
4268 GdipDeleteMatrix(world_to_device);
4271 if (stat == Ok)
4272 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4274 if (stat == Ok)
4275 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4277 GdipDeleteRegion(temp_region);
4280 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4282 DeleteObject(hregion);
4283 return Ok;
4286 if (stat == Ok)
4288 gp_bound_rect.X = bound_rect.left;
4289 gp_bound_rect.Y = bound_rect.top;
4290 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4291 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4293 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4294 if (!pixel_data)
4295 stat = OutOfMemory;
4297 if (stat == Ok)
4299 stat = brush_fill_pixels(graphics, brush, pixel_data,
4300 &gp_bound_rect, gp_bound_rect.Width);
4302 if (stat == Ok)
4303 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4304 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4305 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4307 GdipFree(pixel_data);
4310 DeleteObject(hregion);
4313 return stat;
4316 /*****************************************************************************
4317 * GdipFillRegion [GDIPLUS.@]
4319 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4320 GpRegion* region)
4322 GpStatus stat = NotImplemented;
4324 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4326 if (!(graphics && brush && region))
4327 return InvalidParameter;
4329 if(graphics->busy)
4330 return ObjectBusy;
4332 if (!graphics->image)
4333 stat = GDI32_GdipFillRegion(graphics, brush, region);
4335 if (stat == NotImplemented)
4336 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4338 if (stat == NotImplemented)
4340 FIXME("not implemented for brushtype %i\n", brush->bt);
4341 stat = Ok;
4344 return stat;
4347 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4349 TRACE("(%p,%u)\n", graphics, intention);
4351 if(!graphics)
4352 return InvalidParameter;
4354 if(graphics->busy)
4355 return ObjectBusy;
4357 /* We have no internal operation queue, so there's no need to clear it. */
4359 if (graphics->hdc)
4360 GdiFlush();
4362 return Ok;
4365 /*****************************************************************************
4366 * GdipGetClipBounds [GDIPLUS.@]
4368 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4370 TRACE("(%p, %p)\n", graphics, rect);
4372 if(!graphics)
4373 return InvalidParameter;
4375 if(graphics->busy)
4376 return ObjectBusy;
4378 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4381 /*****************************************************************************
4382 * GdipGetClipBoundsI [GDIPLUS.@]
4384 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4386 TRACE("(%p, %p)\n", graphics, rect);
4388 if(!graphics)
4389 return InvalidParameter;
4391 if(graphics->busy)
4392 return ObjectBusy;
4394 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4397 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4398 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4399 CompositingMode *mode)
4401 TRACE("(%p, %p)\n", graphics, mode);
4403 if(!graphics || !mode)
4404 return InvalidParameter;
4406 if(graphics->busy)
4407 return ObjectBusy;
4409 *mode = graphics->compmode;
4411 return Ok;
4414 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4415 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4416 CompositingQuality *quality)
4418 TRACE("(%p, %p)\n", graphics, quality);
4420 if(!graphics || !quality)
4421 return InvalidParameter;
4423 if(graphics->busy)
4424 return ObjectBusy;
4426 *quality = graphics->compqual;
4428 return Ok;
4431 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4432 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4433 InterpolationMode *mode)
4435 TRACE("(%p, %p)\n", graphics, mode);
4437 if(!graphics || !mode)
4438 return InvalidParameter;
4440 if(graphics->busy)
4441 return ObjectBusy;
4443 *mode = graphics->interpolation;
4445 return Ok;
4448 /* FIXME: Need to handle color depths less than 24bpp */
4449 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4451 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4453 if(!graphics || !argb)
4454 return InvalidParameter;
4456 if(graphics->busy)
4457 return ObjectBusy;
4459 return Ok;
4462 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4464 TRACE("(%p, %p)\n", graphics, scale);
4466 if(!graphics || !scale)
4467 return InvalidParameter;
4469 if(graphics->busy)
4470 return ObjectBusy;
4472 *scale = graphics->scale;
4474 return Ok;
4477 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4479 TRACE("(%p, %p)\n", graphics, unit);
4481 if(!graphics || !unit)
4482 return InvalidParameter;
4484 if(graphics->busy)
4485 return ObjectBusy;
4487 *unit = graphics->unit;
4489 return Ok;
4492 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4493 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4494 *mode)
4496 TRACE("(%p, %p)\n", graphics, mode);
4498 if(!graphics || !mode)
4499 return InvalidParameter;
4501 if(graphics->busy)
4502 return ObjectBusy;
4504 *mode = graphics->pixeloffset;
4506 return Ok;
4509 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4510 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4512 TRACE("(%p, %p)\n", graphics, mode);
4514 if(!graphics || !mode)
4515 return InvalidParameter;
4517 if(graphics->busy)
4518 return ObjectBusy;
4520 *mode = graphics->smoothing;
4522 return Ok;
4525 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4527 TRACE("(%p, %p)\n", graphics, contrast);
4529 if(!graphics || !contrast)
4530 return InvalidParameter;
4532 *contrast = graphics->textcontrast;
4534 return Ok;
4537 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4538 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4539 TextRenderingHint *hint)
4541 TRACE("(%p, %p)\n", graphics, hint);
4543 if(!graphics || !hint)
4544 return InvalidParameter;
4546 if(graphics->busy)
4547 return ObjectBusy;
4549 *hint = graphics->texthint;
4551 return Ok;
4554 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4556 GpRegion *clip_rgn;
4557 GpStatus stat;
4559 TRACE("(%p, %p)\n", graphics, rect);
4561 if(!graphics || !rect)
4562 return InvalidParameter;
4564 if(graphics->busy)
4565 return ObjectBusy;
4567 /* intersect window and graphics clipping regions */
4568 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4569 return stat;
4571 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4572 goto cleanup;
4574 /* get bounds of the region */
4575 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4577 cleanup:
4578 GdipDeleteRegion(clip_rgn);
4580 return stat;
4583 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4585 GpRectF rectf;
4586 GpStatus stat;
4588 TRACE("(%p, %p)\n", graphics, rect);
4590 if(!graphics || !rect)
4591 return InvalidParameter;
4593 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4595 rect->X = gdip_round(rectf.X);
4596 rect->Y = gdip_round(rectf.Y);
4597 rect->Width = gdip_round(rectf.Width);
4598 rect->Height = gdip_round(rectf.Height);
4601 return stat;
4604 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4606 TRACE("(%p, %p)\n", graphics, matrix);
4608 if(!graphics || !matrix)
4609 return InvalidParameter;
4611 if(graphics->busy)
4612 return ObjectBusy;
4614 *matrix = *graphics->worldtrans;
4615 return Ok;
4618 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4620 GpSolidFill *brush;
4621 GpStatus stat;
4622 GpRectF wnd_rect;
4624 TRACE("(%p, %x)\n", graphics, color);
4626 if(!graphics)
4627 return InvalidParameter;
4629 if(graphics->busy)
4630 return ObjectBusy;
4632 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4633 return stat;
4635 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4636 GdipDeleteBrush((GpBrush*)brush);
4637 return stat;
4640 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4641 wnd_rect.Width, wnd_rect.Height);
4643 GdipDeleteBrush((GpBrush*)brush);
4645 return Ok;
4648 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4650 TRACE("(%p, %p)\n", graphics, res);
4652 if(!graphics || !res)
4653 return InvalidParameter;
4655 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4658 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4660 GpStatus stat;
4661 GpRegion* rgn;
4662 GpPointF pt;
4664 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4666 if(!graphics || !result)
4667 return InvalidParameter;
4669 if(graphics->busy)
4670 return ObjectBusy;
4672 pt.X = x;
4673 pt.Y = y;
4674 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4675 CoordinateSpaceWorld, &pt, 1)) != Ok)
4676 return stat;
4678 if((stat = GdipCreateRegion(&rgn)) != Ok)
4679 return stat;
4681 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4682 goto cleanup;
4684 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4686 cleanup:
4687 GdipDeleteRegion(rgn);
4688 return stat;
4691 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4693 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4696 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4698 GpStatus stat;
4699 GpRegion* rgn;
4700 GpPointF pts[2];
4702 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4704 if(!graphics || !result)
4705 return InvalidParameter;
4707 if(graphics->busy)
4708 return ObjectBusy;
4710 pts[0].X = x;
4711 pts[0].Y = y;
4712 pts[1].X = x + width;
4713 pts[1].Y = y + height;
4715 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4716 CoordinateSpaceWorld, pts, 2)) != Ok)
4717 return stat;
4719 pts[1].X -= pts[0].X;
4720 pts[1].Y -= pts[0].Y;
4722 if((stat = GdipCreateRegion(&rgn)) != Ok)
4723 return stat;
4725 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4726 goto cleanup;
4728 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4730 cleanup:
4731 GdipDeleteRegion(rgn);
4732 return stat;
4735 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4737 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4740 GpStatus gdip_format_string(HDC hdc,
4741 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4742 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4743 gdip_format_string_callback callback, void *user_data)
4745 WCHAR* stringdup;
4746 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4747 nheight, lineend, lineno = 0;
4748 RectF bounds;
4749 StringAlignment halign;
4750 GpStatus stat = Ok;
4751 SIZE size;
4752 HotkeyPrefix hkprefix;
4753 INT *hotkeyprefix_offsets=NULL;
4754 INT hotkeyprefix_count=0;
4755 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4756 int seen_prefix=0;
4758 if(length == -1) length = lstrlenW(string);
4760 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4761 if(!stringdup) return OutOfMemory;
4763 nwidth = rect->Width;
4764 nheight = rect->Height;
4766 if (format)
4767 hkprefix = format->hkprefix;
4768 else
4769 hkprefix = HotkeyPrefixNone;
4771 if (hkprefix == HotkeyPrefixShow)
4773 for (i=0; i<length; i++)
4775 if (string[i] == '&')
4776 hotkeyprefix_count++;
4780 if (hotkeyprefix_count)
4781 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4783 hotkeyprefix_count = 0;
4785 for(i = 0, j = 0; i < length; i++){
4786 /* FIXME: This makes the indexes passed to callback inaccurate. */
4787 if(!isprintW(string[i]) && (string[i] != '\n'))
4788 continue;
4790 /* FIXME: tabs should be handled using tabstops from stringformat */
4791 if (string[i] == '\t')
4792 continue;
4794 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4795 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4796 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4798 seen_prefix = 1;
4799 continue;
4802 seen_prefix = 0;
4804 stringdup[j] = string[i];
4805 j++;
4808 length = j;
4810 if (format) halign = format->align;
4811 else halign = StringAlignmentNear;
4813 while(sum < length){
4814 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4815 nwidth, &fit, NULL, &size);
4816 fitcpy = fit;
4818 if(fit == 0)
4819 break;
4821 for(lret = 0; lret < fit; lret++)
4822 if(*(stringdup + sum + lret) == '\n')
4823 break;
4825 /* Line break code (may look strange, but it imitates windows). */
4826 if(lret < fit)
4827 lineend = fit = lret; /* this is not an off-by-one error */
4828 else if(fit < (length - sum)){
4829 if(*(stringdup + sum + fit) == ' ')
4830 while(*(stringdup + sum + fit) == ' ')
4831 fit++;
4832 else
4833 while(*(stringdup + sum + fit - 1) != ' '){
4834 fit--;
4836 if(*(stringdup + sum + fit) == '\t')
4837 break;
4839 if(fit == 0){
4840 fit = fitcpy;
4841 break;
4844 lineend = fit;
4845 while(*(stringdup + sum + lineend - 1) == ' ' ||
4846 *(stringdup + sum + lineend - 1) == '\t')
4847 lineend--;
4849 else
4850 lineend = fit;
4852 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4853 nwidth, &j, NULL, &size);
4855 bounds.Width = size.cx;
4857 if(height + size.cy > nheight)
4858 bounds.Height = nheight - (height + size.cy);
4859 else
4860 bounds.Height = size.cy;
4862 bounds.Y = rect->Y + height;
4864 switch (halign)
4866 case StringAlignmentNear:
4867 default:
4868 bounds.X = rect->X;
4869 break;
4870 case StringAlignmentCenter:
4871 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4872 break;
4873 case StringAlignmentFar:
4874 bounds.X = rect->X + rect->Width - bounds.Width;
4875 break;
4878 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4879 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4880 break;
4882 stat = callback(hdc, stringdup, sum, lineend,
4883 font, rect, format, lineno, &bounds,
4884 &hotkeyprefix_offsets[hotkeyprefix_pos],
4885 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4887 if (stat != Ok)
4888 break;
4890 sum += fit + (lret < fitcpy ? 1 : 0);
4891 height += size.cy;
4892 lineno++;
4894 hotkeyprefix_pos = hotkeyprefix_end_pos;
4896 if(height > nheight)
4897 break;
4899 /* Stop if this was a linewrap (but not if it was a linebreak). */
4900 if ((lret == fitcpy) && format &&
4901 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4902 break;
4905 GdipFree(stringdup);
4906 GdipFree(hotkeyprefix_offsets);
4908 return stat;
4911 struct measure_ranges_args {
4912 GpRegion **regions;
4913 REAL rel_width, rel_height;
4916 static GpStatus measure_ranges_callback(HDC hdc,
4917 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4918 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4919 INT lineno, const RectF *bounds, INT *underlined_indexes,
4920 INT underlined_index_count, void *user_data)
4922 int i;
4923 GpStatus stat = Ok;
4924 struct measure_ranges_args *args = user_data;
4926 for (i=0; i<format->range_count; i++)
4928 INT range_start = max(index, format->character_ranges[i].First);
4929 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4930 if (range_start < range_end)
4932 GpRectF range_rect;
4933 SIZE range_size;
4935 range_rect.Y = bounds->Y / args->rel_height;
4936 range_rect.Height = bounds->Height / args->rel_height;
4938 GetTextExtentExPointW(hdc, string + index, range_start - index,
4939 INT_MAX, NULL, NULL, &range_size);
4940 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4942 GetTextExtentExPointW(hdc, string + index, range_end - index,
4943 INT_MAX, NULL, NULL, &range_size);
4944 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4946 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4947 if (stat != Ok)
4948 break;
4952 return stat;
4955 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4956 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4957 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4958 INT regionCount, GpRegion** regions)
4960 GpStatus stat;
4961 int i;
4962 HFONT gdifont, oldfont;
4963 struct measure_ranges_args args;
4964 HDC hdc, temp_hdc=NULL;
4965 GpPointF pt[3];
4966 RectF scaled_rect;
4967 REAL margin_x;
4969 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4970 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4972 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4973 return InvalidParameter;
4975 if (regionCount < stringFormat->range_count)
4976 return InvalidParameter;
4978 if(!graphics->hdc)
4980 hdc = temp_hdc = CreateCompatibleDC(0);
4981 if (!temp_hdc) return OutOfMemory;
4983 else
4984 hdc = graphics->hdc;
4986 if (stringFormat->attr)
4987 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4989 pt[0].X = 0.0;
4990 pt[0].Y = 0.0;
4991 pt[1].X = 1.0;
4992 pt[1].Y = 0.0;
4993 pt[2].X = 0.0;
4994 pt[2].Y = 1.0;
4995 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4996 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4997 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4998 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4999 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5001 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5002 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5004 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5005 scaled_rect.Y = layoutRect->Y * args.rel_height;
5006 if (stringFormat->attr & StringFormatFlagsNoClip)
5008 scaled_rect.Width = (REAL)(1 << 23);
5009 scaled_rect.Height = (REAL)(1 << 23);
5011 else
5013 scaled_rect.Width = layoutRect->Width * args.rel_width;
5014 scaled_rect.Height = layoutRect->Height * args.rel_height;
5016 if (scaled_rect.Width >= 0.5)
5018 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5019 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5022 get_font_hfont(graphics, font, stringFormat, &gdifont);
5023 oldfont = SelectObject(hdc, gdifont);
5025 for (i=0; i<stringFormat->range_count; i++)
5027 stat = GdipSetEmpty(regions[i]);
5028 if (stat != Ok)
5029 return stat;
5032 args.regions = regions;
5034 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5035 measure_ranges_callback, &args);
5037 SelectObject(hdc, oldfont);
5038 DeleteObject(gdifont);
5040 if (temp_hdc)
5041 DeleteDC(temp_hdc);
5043 return stat;
5046 struct measure_string_args {
5047 RectF *bounds;
5048 INT *codepointsfitted;
5049 INT *linesfilled;
5050 REAL rel_width, rel_height;
5053 static GpStatus measure_string_callback(HDC hdc,
5054 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5055 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5056 INT lineno, const RectF *bounds, INT *underlined_indexes,
5057 INT underlined_index_count, void *user_data)
5059 struct measure_string_args *args = user_data;
5060 REAL new_width, new_height;
5062 new_width = bounds->Width / args->rel_width;
5063 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5065 if (new_width > args->bounds->Width)
5066 args->bounds->Width = new_width;
5068 if (new_height > args->bounds->Height)
5069 args->bounds->Height = new_height;
5071 if (args->codepointsfitted)
5072 *args->codepointsfitted = index + length;
5074 if (args->linesfilled)
5075 (*args->linesfilled)++;
5077 return Ok;
5080 /* Find the smallest rectangle that bounds the text when it is printed in rect
5081 * according to the format options listed in format. If rect has 0 width and
5082 * height, then just find the smallest rectangle that bounds the text when it's
5083 * printed at location (rect->X, rect-Y). */
5084 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5085 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5086 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5087 INT *codepointsfitted, INT *linesfilled)
5089 HFONT oldfont, gdifont;
5090 struct measure_string_args args;
5091 HDC temp_hdc=NULL, hdc;
5092 GpPointF pt[3];
5093 RectF scaled_rect;
5094 REAL margin_x;
5095 INT lines, glyphs, format_flags = format ? format->attr : 0;
5097 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5098 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5099 bounds, codepointsfitted, linesfilled);
5101 if(!graphics || !string || !font || !rect || !bounds)
5102 return InvalidParameter;
5104 if(!graphics->hdc)
5106 hdc = temp_hdc = CreateCompatibleDC(0);
5107 if (!temp_hdc) return OutOfMemory;
5109 else
5110 hdc = graphics->hdc;
5112 if(linesfilled) *linesfilled = 0;
5113 if(codepointsfitted) *codepointsfitted = 0;
5115 if(format)
5116 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5118 pt[0].X = 0.0;
5119 pt[0].Y = 0.0;
5120 pt[1].X = 1.0;
5121 pt[1].Y = 0.0;
5122 pt[2].X = 0.0;
5123 pt[2].Y = 1.0;
5124 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5125 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5126 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5127 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5128 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5130 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5131 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5133 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5134 scaled_rect.Y = rect->Y * args.rel_height;
5135 scaled_rect.Width = rect->Width * args.rel_width;
5136 scaled_rect.Height = rect->Height * args.rel_height;
5138 if ((format_flags & StringFormatFlagsNoClip) ||
5139 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5140 if ((format_flags & StringFormatFlagsNoClip) ||
5141 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5143 if (scaled_rect.Width >= 0.5)
5145 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5146 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5149 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5150 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5152 get_font_hfont(graphics, font, format, &gdifont);
5153 oldfont = SelectObject(hdc, gdifont);
5155 bounds->X = rect->X;
5156 bounds->Y = rect->Y;
5157 bounds->Width = 0.0;
5158 bounds->Height = 0.0;
5160 args.bounds = bounds;
5161 args.codepointsfitted = &glyphs;
5162 args.linesfilled = &lines;
5163 lines = glyphs = 0;
5165 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5166 measure_string_callback, &args);
5168 if (linesfilled) *linesfilled = lines;
5169 if (codepointsfitted) *codepointsfitted = glyphs;
5171 if (lines)
5172 bounds->Width += margin_x * 2.0;
5174 SelectObject(hdc, oldfont);
5175 DeleteObject(gdifont);
5177 if (temp_hdc)
5178 DeleteDC(temp_hdc);
5180 return Ok;
5183 struct draw_string_args {
5184 GpGraphics *graphics;
5185 GDIPCONST GpBrush *brush;
5186 REAL x, y, rel_width, rel_height, ascent;
5189 static GpStatus draw_string_callback(HDC hdc,
5190 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5191 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5192 INT lineno, const RectF *bounds, INT *underlined_indexes,
5193 INT underlined_index_count, void *user_data)
5195 struct draw_string_args *args = user_data;
5196 PointF position;
5197 GpStatus stat;
5199 position.X = args->x + bounds->X / args->rel_width;
5200 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5202 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5203 args->brush, &position,
5204 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5206 if (stat == Ok && underlined_index_count)
5208 OUTLINETEXTMETRICW otm;
5209 REAL underline_y, underline_height;
5210 int i;
5212 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5214 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5215 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5217 for (i=0; i<underlined_index_count; i++)
5219 REAL start_x, end_x;
5220 SIZE text_size;
5221 INT ofs = underlined_indexes[i] - index;
5223 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5224 start_x = text_size.cx / args->rel_width;
5226 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5227 end_x = text_size.cx / args->rel_width;
5229 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5233 return stat;
5236 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5237 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5238 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5240 HRGN rgn = NULL;
5241 HFONT gdifont;
5242 GpPointF pt[3], rectcpy[4];
5243 POINT corners[4];
5244 REAL rel_width, rel_height, margin_x;
5245 INT save_state, format_flags = 0;
5246 REAL offsety = 0.0;
5247 struct draw_string_args args;
5248 RectF scaled_rect;
5249 HDC hdc, temp_hdc=NULL;
5250 TEXTMETRICW textmetric;
5252 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5253 length, font, debugstr_rectf(rect), format, brush);
5255 if(!graphics || !string || !font || !brush || !rect)
5256 return InvalidParameter;
5258 if(graphics->hdc)
5260 hdc = graphics->hdc;
5262 else
5264 hdc = temp_hdc = CreateCompatibleDC(0);
5267 if(format){
5268 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5270 format_flags = format->attr;
5272 /* Should be no need to explicitly test for StringAlignmentNear as
5273 * that is default behavior if no alignment is passed. */
5274 if(format->vertalign != StringAlignmentNear){
5275 RectF bounds, in_rect = *rect;
5276 in_rect.Height = 0.0; /* avoid height clipping */
5277 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5279 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5281 if(format->vertalign == StringAlignmentCenter)
5282 offsety = (rect->Height - bounds.Height) / 2;
5283 else if(format->vertalign == StringAlignmentFar)
5284 offsety = (rect->Height - bounds.Height);
5286 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5289 save_state = SaveDC(hdc);
5291 pt[0].X = 0.0;
5292 pt[0].Y = 0.0;
5293 pt[1].X = 1.0;
5294 pt[1].Y = 0.0;
5295 pt[2].X = 0.0;
5296 pt[2].Y = 1.0;
5297 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5298 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5299 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5300 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5301 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5303 rectcpy[3].X = rectcpy[0].X = rect->X;
5304 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5305 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5306 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5307 transform_and_round_points(graphics, corners, rectcpy, 4);
5309 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5310 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5312 scaled_rect.X = margin_x * rel_width;
5313 scaled_rect.Y = 0.0;
5314 scaled_rect.Width = rel_width * rect->Width;
5315 scaled_rect.Height = rel_height * rect->Height;
5317 if ((format_flags & StringFormatFlagsNoClip) ||
5318 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5319 if ((format_flags & StringFormatFlagsNoClip) ||
5320 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5322 if (scaled_rect.Width >= 0.5)
5324 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5325 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5328 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5329 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5331 if (!(format_flags & StringFormatFlagsNoClip) &&
5332 gdip_round(scaled_rect.Width) != 0 && gdip_round(scaled_rect.Height) != 0)
5334 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5335 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5336 SelectClipRgn(hdc, rgn);
5339 get_font_hfont(graphics, font, format, &gdifont);
5340 SelectObject(hdc, gdifont);
5342 args.graphics = graphics;
5343 args.brush = brush;
5345 args.x = rect->X;
5346 args.y = rect->Y + offsety;
5348 args.rel_width = rel_width;
5349 args.rel_height = rel_height;
5351 GetTextMetricsW(hdc, &textmetric);
5352 args.ascent = textmetric.tmAscent / rel_height;
5354 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5355 draw_string_callback, &args);
5357 DeleteObject(rgn);
5358 DeleteObject(gdifont);
5360 RestoreDC(hdc, save_state);
5362 DeleteDC(temp_hdc);
5364 return Ok;
5367 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5369 TRACE("(%p)\n", graphics);
5371 if(!graphics)
5372 return InvalidParameter;
5374 if(graphics->busy)
5375 return ObjectBusy;
5377 return GdipSetInfinite(graphics->clip);
5380 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5382 TRACE("(%p)\n", graphics);
5384 if(!graphics)
5385 return InvalidParameter;
5387 if(graphics->busy)
5388 return ObjectBusy;
5390 graphics->worldtrans->matrix[0] = 1.0;
5391 graphics->worldtrans->matrix[1] = 0.0;
5392 graphics->worldtrans->matrix[2] = 0.0;
5393 graphics->worldtrans->matrix[3] = 1.0;
5394 graphics->worldtrans->matrix[4] = 0.0;
5395 graphics->worldtrans->matrix[5] = 0.0;
5397 return Ok;
5400 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5402 return GdipEndContainer(graphics, state);
5405 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5406 GpMatrixOrder order)
5408 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5410 if(!graphics)
5411 return InvalidParameter;
5413 if(graphics->busy)
5414 return ObjectBusy;
5416 return GdipRotateMatrix(graphics->worldtrans, angle, order);
5419 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5421 return GdipBeginContainer2(graphics, state);
5424 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5425 GraphicsContainer *state)
5427 GraphicsContainerItem *container;
5428 GpStatus sts;
5430 TRACE("(%p, %p)\n", graphics, state);
5432 if(!graphics || !state)
5433 return InvalidParameter;
5435 sts = init_container(&container, graphics);
5436 if(sts != Ok)
5437 return sts;
5439 list_add_head(&graphics->containers, &container->entry);
5440 *state = graphics->contid = container->contid;
5442 return Ok;
5445 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5447 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5448 return NotImplemented;
5451 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5453 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5454 return NotImplemented;
5457 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5459 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5460 return NotImplemented;
5463 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5465 GpStatus sts;
5466 GraphicsContainerItem *container, *container2;
5468 TRACE("(%p, %x)\n", graphics, state);
5470 if(!graphics)
5471 return InvalidParameter;
5473 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5474 if(container->contid == state)
5475 break;
5478 /* did not find a matching container */
5479 if(&container->entry == &graphics->containers)
5480 return Ok;
5482 sts = restore_container(graphics, container);
5483 if(sts != Ok)
5484 return sts;
5486 /* remove all of the containers on top of the found container */
5487 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5488 if(container->contid == state)
5489 break;
5490 list_remove(&container->entry);
5491 delete_container(container);
5494 list_remove(&container->entry);
5495 delete_container(container);
5497 return Ok;
5500 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5501 REAL sy, GpMatrixOrder order)
5503 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5505 if(!graphics)
5506 return InvalidParameter;
5508 if(graphics->busy)
5509 return ObjectBusy;
5511 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
5514 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5515 CombineMode mode)
5517 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5519 if(!graphics || !srcgraphics)
5520 return InvalidParameter;
5522 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5525 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5526 CompositingMode mode)
5528 TRACE("(%p, %d)\n", graphics, mode);
5530 if(!graphics)
5531 return InvalidParameter;
5533 if(graphics->busy)
5534 return ObjectBusy;
5536 graphics->compmode = mode;
5538 return Ok;
5541 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5542 CompositingQuality quality)
5544 TRACE("(%p, %d)\n", graphics, quality);
5546 if(!graphics)
5547 return InvalidParameter;
5549 if(graphics->busy)
5550 return ObjectBusy;
5552 graphics->compqual = quality;
5554 return Ok;
5557 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5558 InterpolationMode mode)
5560 TRACE("(%p, %d)\n", graphics, mode);
5562 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5563 return InvalidParameter;
5565 if(graphics->busy)
5566 return ObjectBusy;
5568 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5569 mode = InterpolationModeBilinear;
5571 if (mode == InterpolationModeHighQuality)
5572 mode = InterpolationModeHighQualityBicubic;
5574 graphics->interpolation = mode;
5576 return Ok;
5579 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5581 TRACE("(%p, %.2f)\n", graphics, scale);
5583 if(!graphics || (scale <= 0.0))
5584 return InvalidParameter;
5586 if(graphics->busy)
5587 return ObjectBusy;
5589 graphics->scale = scale;
5591 return Ok;
5594 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5596 TRACE("(%p, %d)\n", graphics, unit);
5598 if(!graphics)
5599 return InvalidParameter;
5601 if(graphics->busy)
5602 return ObjectBusy;
5604 if(unit == UnitWorld)
5605 return InvalidParameter;
5607 graphics->unit = unit;
5609 return Ok;
5612 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5613 mode)
5615 TRACE("(%p, %d)\n", graphics, mode);
5617 if(!graphics)
5618 return InvalidParameter;
5620 if(graphics->busy)
5621 return ObjectBusy;
5623 graphics->pixeloffset = mode;
5625 return Ok;
5628 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5630 static int calls;
5632 TRACE("(%p,%i,%i)\n", graphics, x, y);
5634 if (!(calls++))
5635 FIXME("value is unused in rendering\n");
5637 if (!graphics)
5638 return InvalidParameter;
5640 graphics->origin_x = x;
5641 graphics->origin_y = y;
5643 return Ok;
5646 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5648 TRACE("(%p,%p,%p)\n", graphics, x, y);
5650 if (!graphics || !x || !y)
5651 return InvalidParameter;
5653 *x = graphics->origin_x;
5654 *y = graphics->origin_y;
5656 return Ok;
5659 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5661 TRACE("(%p, %d)\n", graphics, mode);
5663 if(!graphics)
5664 return InvalidParameter;
5666 if(graphics->busy)
5667 return ObjectBusy;
5669 graphics->smoothing = mode;
5671 return Ok;
5674 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5676 TRACE("(%p, %d)\n", graphics, contrast);
5678 if(!graphics)
5679 return InvalidParameter;
5681 graphics->textcontrast = contrast;
5683 return Ok;
5686 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5687 TextRenderingHint hint)
5689 TRACE("(%p, %d)\n", graphics, hint);
5691 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5692 return InvalidParameter;
5694 if(graphics->busy)
5695 return ObjectBusy;
5697 graphics->texthint = hint;
5699 return Ok;
5702 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5704 TRACE("(%p, %p)\n", graphics, matrix);
5706 if(!graphics || !matrix)
5707 return InvalidParameter;
5709 if(graphics->busy)
5710 return ObjectBusy;
5712 TRACE("%f,%f,%f,%f,%f,%f\n",
5713 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5714 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5716 GdipDeleteMatrix(graphics->worldtrans);
5717 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5720 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5721 REAL dy, GpMatrixOrder order)
5723 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5725 if(!graphics)
5726 return InvalidParameter;
5728 if(graphics->busy)
5729 return ObjectBusy;
5731 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5734 /*****************************************************************************
5735 * GdipSetClipHrgn [GDIPLUS.@]
5737 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5739 GpRegion *region;
5740 GpStatus status;
5742 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5744 if(!graphics)
5745 return InvalidParameter;
5747 status = GdipCreateRegionHrgn(hrgn, &region);
5748 if(status != Ok)
5749 return status;
5751 status = GdipSetClipRegion(graphics, region, mode);
5753 GdipDeleteRegion(region);
5754 return status;
5757 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5759 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5761 if(!graphics)
5762 return InvalidParameter;
5764 if(graphics->busy)
5765 return ObjectBusy;
5767 return GdipCombineRegionPath(graphics->clip, path, mode);
5770 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5771 REAL width, REAL height,
5772 CombineMode mode)
5774 GpRectF rect;
5776 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5778 if(!graphics)
5779 return InvalidParameter;
5781 if(graphics->busy)
5782 return ObjectBusy;
5784 rect.X = x;
5785 rect.Y = y;
5786 rect.Width = width;
5787 rect.Height = height;
5789 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5792 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5793 INT width, INT height,
5794 CombineMode mode)
5796 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5798 if(!graphics)
5799 return InvalidParameter;
5801 if(graphics->busy)
5802 return ObjectBusy;
5804 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5807 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5808 CombineMode mode)
5810 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5812 if(!graphics || !region)
5813 return InvalidParameter;
5815 if(graphics->busy)
5816 return ObjectBusy;
5818 return GdipCombineRegionRegion(graphics->clip, region, mode);
5821 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5822 UINT limitDpi)
5824 static int calls;
5826 TRACE("(%p,%u)\n", metafile, limitDpi);
5828 if(!(calls++))
5829 FIXME("not implemented\n");
5831 return NotImplemented;
5834 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5835 INT count)
5837 INT save_state;
5838 POINT *pti;
5840 TRACE("(%p, %p, %d)\n", graphics, points, count);
5842 if(!graphics || !pen || count<=0)
5843 return InvalidParameter;
5845 if(graphics->busy)
5846 return ObjectBusy;
5848 if (!graphics->hdc)
5850 FIXME("graphics object has no HDC\n");
5851 return Ok;
5854 pti = GdipAlloc(sizeof(POINT) * count);
5856 save_state = prepare_dc(graphics, pen);
5857 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5859 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5860 Polygon(graphics->hdc, pti, count);
5862 restore_dc(graphics, save_state);
5863 GdipFree(pti);
5865 return Ok;
5868 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5869 INT count)
5871 GpStatus ret;
5872 GpPointF *ptf;
5873 INT i;
5875 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5877 if(count<=0) return InvalidParameter;
5878 ptf = GdipAlloc(sizeof(GpPointF) * count);
5880 for(i = 0;i < count; i++){
5881 ptf[i].X = (REAL)points[i].X;
5882 ptf[i].Y = (REAL)points[i].Y;
5885 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5886 GdipFree(ptf);
5888 return ret;
5891 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5893 TRACE("(%p, %p)\n", graphics, dpi);
5895 if(!graphics || !dpi)
5896 return InvalidParameter;
5898 if(graphics->busy)
5899 return ObjectBusy;
5901 *dpi = graphics->xres;
5902 return Ok;
5905 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5907 TRACE("(%p, %p)\n", graphics, dpi);
5909 if(!graphics || !dpi)
5910 return InvalidParameter;
5912 if(graphics->busy)
5913 return ObjectBusy;
5915 *dpi = graphics->yres;
5916 return Ok;
5919 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5920 GpMatrixOrder order)
5922 GpMatrix m;
5923 GpStatus ret;
5925 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5927 if(!graphics || !matrix)
5928 return InvalidParameter;
5930 if(graphics->busy)
5931 return ObjectBusy;
5933 m = *(graphics->worldtrans);
5935 ret = GdipMultiplyMatrix(&m, matrix, order);
5936 if(ret == Ok)
5937 *(graphics->worldtrans) = m;
5939 return ret;
5942 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5943 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5945 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5947 GpStatus stat=Ok;
5949 TRACE("(%p, %p)\n", graphics, hdc);
5951 if(!graphics || !hdc)
5952 return InvalidParameter;
5954 if(graphics->busy)
5955 return ObjectBusy;
5957 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5959 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5961 else if (!graphics->hdc ||
5962 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5964 /* Create a fake HDC and fill it with a constant color. */
5965 HDC temp_hdc;
5966 HBITMAP hbitmap;
5967 GpRectF bounds;
5968 BITMAPINFOHEADER bmih;
5969 int i;
5971 stat = get_graphics_bounds(graphics, &bounds);
5972 if (stat != Ok)
5973 return stat;
5975 graphics->temp_hbitmap_width = bounds.Width;
5976 graphics->temp_hbitmap_height = bounds.Height;
5978 bmih.biSize = sizeof(bmih);
5979 bmih.biWidth = graphics->temp_hbitmap_width;
5980 bmih.biHeight = -graphics->temp_hbitmap_height;
5981 bmih.biPlanes = 1;
5982 bmih.biBitCount = 32;
5983 bmih.biCompression = BI_RGB;
5984 bmih.biSizeImage = 0;
5985 bmih.biXPelsPerMeter = 0;
5986 bmih.biYPelsPerMeter = 0;
5987 bmih.biClrUsed = 0;
5988 bmih.biClrImportant = 0;
5990 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5991 (void**)&graphics->temp_bits, NULL, 0);
5992 if (!hbitmap)
5993 return GenericError;
5995 temp_hdc = CreateCompatibleDC(0);
5996 if (!temp_hdc)
5998 DeleteObject(hbitmap);
5999 return GenericError;
6002 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6003 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6005 SelectObject(temp_hdc, hbitmap);
6007 graphics->temp_hbitmap = hbitmap;
6008 *hdc = graphics->temp_hdc = temp_hdc;
6010 else
6012 *hdc = graphics->hdc;
6015 if (stat == Ok)
6016 graphics->busy = TRUE;
6018 return stat;
6021 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6023 GpStatus stat=Ok;
6025 TRACE("(%p, %p)\n", graphics, hdc);
6027 if(!graphics || !hdc || !graphics->busy)
6028 return InvalidParameter;
6030 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6032 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6034 else if (graphics->temp_hdc == hdc)
6036 DWORD* pos;
6037 int i;
6039 /* Find the pixels that have changed, and mark them as opaque. */
6040 pos = (DWORD*)graphics->temp_bits;
6041 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6043 if (*pos != DC_BACKGROUND_KEY)
6045 *pos |= 0xff000000;
6047 pos++;
6050 /* Write the changed pixels to the real target. */
6051 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6052 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6053 graphics->temp_hbitmap_width * 4);
6055 /* Clean up. */
6056 DeleteDC(graphics->temp_hdc);
6057 DeleteObject(graphics->temp_hbitmap);
6058 graphics->temp_hdc = NULL;
6059 graphics->temp_hbitmap = NULL;
6061 else if (hdc != graphics->hdc)
6063 stat = InvalidParameter;
6066 if (stat == Ok)
6067 graphics->busy = FALSE;
6069 return stat;
6072 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6074 GpRegion *clip;
6075 GpStatus status;
6077 TRACE("(%p, %p)\n", graphics, region);
6079 if(!graphics || !region)
6080 return InvalidParameter;
6082 if(graphics->busy)
6083 return ObjectBusy;
6085 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6086 return status;
6088 /* free everything except root node and header */
6089 delete_element(&region->node);
6090 memcpy(region, clip, sizeof(GpRegion));
6091 GdipFree(clip);
6093 return Ok;
6096 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6097 GpCoordinateSpace src_space, GpMatrix **matrix)
6099 GpStatus stat = GdipCreateMatrix(matrix);
6100 REAL scale_x, scale_y;
6102 if (dst_space != src_space && stat == Ok)
6104 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6105 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6107 if(graphics->unit != UnitDisplay)
6109 scale_x *= graphics->scale;
6110 scale_y *= graphics->scale;
6113 /* transform from src_space to CoordinateSpacePage */
6114 switch (src_space)
6116 case CoordinateSpaceWorld:
6117 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
6118 break;
6119 case CoordinateSpacePage:
6120 break;
6121 case CoordinateSpaceDevice:
6122 GdipScaleMatrix(*matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6123 break;
6126 /* transform from CoordinateSpacePage to dst_space */
6127 switch (dst_space)
6129 case CoordinateSpaceWorld:
6131 GpMatrix *inverted_transform;
6132 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
6133 if (stat == Ok)
6135 stat = GdipInvertMatrix(inverted_transform);
6136 if (stat == Ok)
6137 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
6138 GdipDeleteMatrix(inverted_transform);
6140 break;
6142 case CoordinateSpacePage:
6143 break;
6144 case CoordinateSpaceDevice:
6145 GdipScaleMatrix(*matrix, scale_x, scale_y, MatrixOrderAppend);
6146 break;
6149 return stat;
6152 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6153 GpCoordinateSpace src_space, GpPointF *points, INT count)
6155 GpMatrix *matrix;
6156 GpStatus stat;
6158 if(!graphics || !points || count <= 0)
6159 return InvalidParameter;
6161 if(graphics->busy)
6162 return ObjectBusy;
6164 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6166 if (src_space == dst_space) return Ok;
6168 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6170 if (stat == Ok)
6172 stat = GdipTransformMatrixPoints(matrix, points, count);
6174 GdipDeleteMatrix(matrix);
6177 return stat;
6180 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6181 GpCoordinateSpace src_space, GpPoint *points, INT count)
6183 GpPointF *pointsF;
6184 GpStatus ret;
6185 INT i;
6187 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6189 if(count <= 0)
6190 return InvalidParameter;
6192 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6193 if(!pointsF)
6194 return OutOfMemory;
6196 for(i = 0; i < count; i++){
6197 pointsF[i].X = (REAL)points[i].X;
6198 pointsF[i].Y = (REAL)points[i].Y;
6201 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6203 if(ret == Ok)
6204 for(i = 0; i < count; i++){
6205 points[i].X = gdip_round(pointsF[i].X);
6206 points[i].Y = gdip_round(pointsF[i].Y);
6208 GdipFree(pointsF);
6210 return ret;
6213 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6215 static int calls;
6217 TRACE("\n");
6219 if (!calls++)
6220 FIXME("stub\n");
6222 return NULL;
6225 /*****************************************************************************
6226 * GdipTranslateClip [GDIPLUS.@]
6228 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6230 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6232 if(!graphics)
6233 return InvalidParameter;
6235 if(graphics->busy)
6236 return ObjectBusy;
6238 return GdipTranslateRegion(graphics->clip, dx, dy);
6241 /*****************************************************************************
6242 * GdipTranslateClipI [GDIPLUS.@]
6244 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6246 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6248 if(!graphics)
6249 return InvalidParameter;
6251 if(graphics->busy)
6252 return ObjectBusy;
6254 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6258 /*****************************************************************************
6259 * GdipMeasureDriverString [GDIPLUS.@]
6261 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6262 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6263 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6265 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6266 HFONT hfont;
6267 HDC hdc;
6268 REAL min_x, min_y, max_x, max_y, x, y;
6269 int i;
6270 TEXTMETRICW textmetric;
6271 const WORD *glyph_indices;
6272 WORD *dynamic_glyph_indices=NULL;
6273 REAL rel_width, rel_height, ascent, descent;
6274 GpPointF pt[3];
6276 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6278 if (!graphics || !text || !font || !positions || !boundingBox)
6279 return InvalidParameter;
6281 if (length == -1)
6282 length = strlenW(text);
6284 if (length == 0)
6286 boundingBox->X = 0.0;
6287 boundingBox->Y = 0.0;
6288 boundingBox->Width = 0.0;
6289 boundingBox->Height = 0.0;
6292 if (flags & unsupported_flags)
6293 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6295 if (matrix)
6296 FIXME("Ignoring matrix\n");
6298 get_font_hfont(graphics, font, NULL, &hfont);
6300 hdc = CreateCompatibleDC(0);
6301 SelectObject(hdc, hfont);
6303 GetTextMetricsW(hdc, &textmetric);
6305 pt[0].X = 0.0;
6306 pt[0].Y = 0.0;
6307 pt[1].X = 1.0;
6308 pt[1].Y = 0.0;
6309 pt[2].X = 0.0;
6310 pt[2].Y = 1.0;
6311 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6312 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6313 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6314 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6315 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6317 if (flags & DriverStringOptionsCmapLookup)
6319 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6320 if (!glyph_indices)
6322 DeleteDC(hdc);
6323 DeleteObject(hfont);
6324 return OutOfMemory;
6327 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6329 else
6330 glyph_indices = text;
6332 min_x = max_x = x = positions[0].X;
6333 min_y = max_y = y = positions[0].Y;
6335 ascent = textmetric.tmAscent / rel_height;
6336 descent = textmetric.tmDescent / rel_height;
6338 for (i=0; i<length; i++)
6340 int char_width;
6341 ABC abc;
6343 if (!(flags & DriverStringOptionsRealizedAdvance))
6345 x = positions[i].X;
6346 y = positions[i].Y;
6349 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6350 char_width = abc.abcA + abc.abcB + abc.abcB;
6352 if (min_y > y - ascent) min_y = y - ascent;
6353 if (max_y < y + descent) max_y = y + descent;
6354 if (min_x > x) min_x = x;
6356 x += char_width / rel_width;
6358 if (max_x < x) max_x = x;
6361 GdipFree(dynamic_glyph_indices);
6362 DeleteDC(hdc);
6363 DeleteObject(hfont);
6365 boundingBox->X = min_x;
6366 boundingBox->Y = min_y;
6367 boundingBox->Width = max_x - min_x;
6368 boundingBox->Height = max_y - min_y;
6370 return Ok;
6373 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6374 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6375 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6376 INT flags, GDIPCONST GpMatrix *matrix)
6378 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6379 INT save_state;
6380 GpPointF pt;
6381 HFONT hfont;
6382 UINT eto_flags=0;
6384 if (flags & unsupported_flags)
6385 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6387 if (matrix)
6388 FIXME("Ignoring matrix\n");
6390 if (!(flags & DriverStringOptionsCmapLookup))
6391 eto_flags |= ETO_GLYPH_INDEX;
6393 save_state = SaveDC(graphics->hdc);
6394 SetBkMode(graphics->hdc, TRANSPARENT);
6395 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6397 pt = positions[0];
6398 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6400 get_font_hfont(graphics, font, format, &hfont);
6401 SelectObject(graphics->hdc, hfont);
6403 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6405 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6407 RestoreDC(graphics->hdc, save_state);
6409 DeleteObject(hfont);
6411 return Ok;
6414 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6415 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6416 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6417 INT flags, GDIPCONST GpMatrix *matrix)
6419 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6420 GpStatus stat;
6421 PointF *real_positions, real_position;
6422 POINT *pti;
6423 HFONT hfont;
6424 HDC hdc;
6425 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6426 DWORD max_glyphsize=0;
6427 GLYPHMETRICS glyphmetrics;
6428 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6429 BYTE *glyph_mask;
6430 BYTE *text_mask;
6431 int text_mask_stride;
6432 BYTE *pixel_data;
6433 int pixel_data_stride;
6434 GpRect pixel_area;
6435 UINT ggo_flags = GGO_GRAY8_BITMAP;
6437 if (length <= 0)
6438 return Ok;
6440 if (!(flags & DriverStringOptionsCmapLookup))
6441 ggo_flags |= GGO_GLYPH_INDEX;
6443 if (flags & unsupported_flags)
6444 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6446 if (matrix)
6447 FIXME("Ignoring matrix\n");
6449 pti = GdipAlloc(sizeof(POINT) * length);
6450 if (!pti)
6451 return OutOfMemory;
6453 if (flags & DriverStringOptionsRealizedAdvance)
6455 real_position = positions[0];
6457 transform_and_round_points(graphics, pti, &real_position, 1);
6459 else
6461 real_positions = GdipAlloc(sizeof(PointF) * length);
6462 if (!real_positions)
6464 GdipFree(pti);
6465 return OutOfMemory;
6468 memcpy(real_positions, positions, sizeof(PointF) * length);
6470 transform_and_round_points(graphics, pti, real_positions, length);
6472 GdipFree(real_positions);
6475 get_font_hfont(graphics, font, format, &hfont);
6477 hdc = CreateCompatibleDC(0);
6478 SelectObject(hdc, hfont);
6480 /* Get the boundaries of the text to be drawn */
6481 for (i=0; i<length; i++)
6483 DWORD glyphsize;
6484 int left, top, right, bottom;
6486 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6487 &glyphmetrics, 0, NULL, &identity);
6489 if (glyphsize == GDI_ERROR)
6491 ERR("GetGlyphOutlineW failed\n");
6492 GdipFree(pti);
6493 DeleteDC(hdc);
6494 DeleteObject(hfont);
6495 return GenericError;
6498 if (glyphsize > max_glyphsize)
6499 max_glyphsize = glyphsize;
6501 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6502 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6503 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6504 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6506 if (left < min_x) min_x = left;
6507 if (top < min_y) min_y = top;
6508 if (right > max_x) max_x = right;
6509 if (bottom > max_y) max_y = bottom;
6511 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6513 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6514 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6518 glyph_mask = GdipAlloc(max_glyphsize);
6519 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6520 text_mask_stride = max_x - min_x;
6522 if (!(glyph_mask && text_mask))
6524 GdipFree(glyph_mask);
6525 GdipFree(text_mask);
6526 GdipFree(pti);
6527 DeleteDC(hdc);
6528 DeleteObject(hfont);
6529 return OutOfMemory;
6532 /* Generate a mask for the text */
6533 for (i=0; i<length; i++)
6535 int left, top, stride;
6537 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6538 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6540 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6541 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6542 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6544 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6546 BYTE *glyph_val = glyph_mask + y * stride;
6547 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6548 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6550 *text_val = min(64, *text_val + *glyph_val);
6551 glyph_val++;
6552 text_val++;
6557 GdipFree(pti);
6558 DeleteDC(hdc);
6559 DeleteObject(hfont);
6560 GdipFree(glyph_mask);
6562 /* get the brush data */
6563 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6564 if (!pixel_data)
6566 GdipFree(text_mask);
6567 return OutOfMemory;
6570 pixel_area.X = min_x;
6571 pixel_area.Y = min_y;
6572 pixel_area.Width = max_x - min_x;
6573 pixel_area.Height = max_y - min_y;
6574 pixel_data_stride = pixel_area.Width * 4;
6576 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6577 if (stat != Ok)
6579 GdipFree(text_mask);
6580 GdipFree(pixel_data);
6581 return stat;
6584 /* multiply the brush data by the mask */
6585 for (y=0; y<pixel_area.Height; y++)
6587 BYTE *text_val = text_mask + text_mask_stride * y;
6588 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6589 for (x=0; x<pixel_area.Width; x++)
6591 *pixel_val = (*pixel_val) * (*text_val) / 64;
6592 text_val++;
6593 pixel_val+=4;
6597 GdipFree(text_mask);
6599 /* draw the result */
6600 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6601 pixel_area.Height, pixel_data_stride);
6603 GdipFree(pixel_data);
6605 return stat;
6608 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6609 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6610 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6611 INT flags, GDIPCONST GpMatrix *matrix)
6613 GpStatus stat = NotImplemented;
6615 if (length == -1)
6616 length = strlenW(text);
6618 if (graphics->hdc &&
6619 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6620 brush->bt == BrushTypeSolidColor &&
6621 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6622 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6623 brush, positions, flags, matrix);
6624 if (stat == NotImplemented)
6625 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6626 brush, positions, flags, matrix);
6627 return stat;
6630 /*****************************************************************************
6631 * GdipDrawDriverString [GDIPLUS.@]
6633 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6634 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6635 GDIPCONST PointF *positions, INT flags,
6636 GDIPCONST GpMatrix *matrix )
6638 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6640 if (!graphics || !text || !font || !brush || !positions)
6641 return InvalidParameter;
6643 return draw_driver_string(graphics, text, length, font, NULL,
6644 brush, positions, flags, matrix);
6647 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6648 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6650 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6651 return NotImplemented;
6654 /*****************************************************************************
6655 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6657 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6659 GpStatus stat;
6660 GpRegion* rgn;
6662 TRACE("(%p, %p)\n", graphics, res);
6664 if((stat = GdipCreateRegion(&rgn)) != Ok)
6665 return stat;
6667 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6668 goto cleanup;
6670 stat = GdipIsEmptyRegion(rgn, graphics, res);
6672 cleanup:
6673 GdipDeleteRegion(rgn);
6674 return stat;
6677 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6679 static int calls;
6681 TRACE("(%p) stub\n", graphics);
6683 if(!(calls++))
6684 FIXME("not implemented\n");
6686 return NotImplemented;