gdiplus: Do some actual color blending when drawing path gradients.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blob56a61a7441b14c7c5085ccb9cd588d7d37a1b32e
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
52 REAL radAngle, hypotenuse;
54 radAngle = deg2rad(angle);
55 hypotenuse = 50.0; /* arbitrary */
57 *x = x_0 + cos(radAngle) * hypotenuse;
58 *y = y_0 + sin(radAngle) * hypotenuse;
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
64 BYTE ret;
66 switch(type & PathPointTypePathTypeMask){
67 case PathPointTypeBezier:
68 ret = PT_BEZIERTO;
69 break;
70 case PathPointTypeLine:
71 ret = PT_LINETO;
72 break;
73 case PathPointTypeStart:
74 ret = PT_MOVETO;
75 break;
76 default:
77 ERR("Bad point type\n");
78 return 0;
81 if(type & PathPointTypeCloseSubpath)
82 ret |= PT_CLOSEFIGURE;
84 return ret;
87 static REAL graphics_res(GpGraphics *graphics)
89 if (graphics->image) return graphics->image->xres;
90 else return (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
93 static COLORREF get_gdi_brush_color(const GpBrush *brush)
95 ARGB argb;
97 switch (brush->bt)
99 case BrushTypeSolidColor:
101 const GpSolidFill *sf = (const GpSolidFill *)brush;
102 argb = sf->color;
103 break;
105 case BrushTypeHatchFill:
107 const GpHatch *hatch = (const GpHatch *)brush;
108 argb = hatch->forecol;
109 break;
111 case BrushTypeLinearGradient:
113 const GpLineGradient *line = (const GpLineGradient *)brush;
114 argb = line->startcolor;
115 break;
117 case BrushTypePathGradient:
119 const GpPathGradient *grad = (const GpPathGradient *)brush;
120 argb = grad->centercolor;
121 break;
123 default:
124 FIXME("unhandled brush type %d\n", brush->bt);
125 argb = 0;
126 break;
128 return ARGB2COLORREF(argb);
131 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
133 HBITMAP hbmp;
134 HDC hdc;
135 BITMAPINFOHEADER bmih;
136 DWORD *bits;
137 int x, y;
139 hdc = CreateCompatibleDC(0);
141 if (!hdc) return 0;
143 bmih.biSize = sizeof(bmih);
144 bmih.biWidth = 8;
145 bmih.biHeight = 8;
146 bmih.biPlanes = 1;
147 bmih.biBitCount = 32;
148 bmih.biCompression = BI_RGB;
149 bmih.biSizeImage = 0;
151 hbmp = CreateDIBSection(hdc, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
152 if (hbmp)
154 const char *hatch_data;
156 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
158 for (y = 0; y < 8; y++)
160 for (x = 0; x < 8; x++)
162 if (hatch_data[y] & (0x80 >> x))
163 bits[y * 8 + x] = hatch->forecol;
164 else
165 bits[y * 8 + x] = hatch->backcol;
169 else
171 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
173 for (y = 0; y < 64; y++)
174 bits[y] = hatch->forecol;
178 DeleteDC(hdc);
179 return hbmp;
182 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
184 switch (brush->bt)
186 case BrushTypeSolidColor:
188 const GpSolidFill *sf = (const GpSolidFill *)brush;
189 lb->lbStyle = BS_SOLID;
190 lb->lbColor = ARGB2COLORREF(sf->color);
191 lb->lbHatch = 0;
192 return Ok;
195 case BrushTypeHatchFill:
197 const GpHatch *hatch = (const GpHatch *)brush;
198 HBITMAP hbmp;
200 hbmp = create_hatch_bitmap(hatch);
201 if (!hbmp) return OutOfMemory;
203 lb->lbStyle = BS_PATTERN;
204 lb->lbColor = 0;
205 lb->lbHatch = (ULONG_PTR)hbmp;
206 return Ok;
209 default:
210 FIXME("unhandled brush type %d\n", brush->bt);
211 lb->lbStyle = BS_SOLID;
212 lb->lbColor = get_gdi_brush_color(brush);
213 lb->lbHatch = 0;
214 return Ok;
218 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
220 switch (lb->lbStyle)
222 case BS_PATTERN:
223 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
224 break;
226 return Ok;
229 static HBRUSH create_gdi_brush(const GpBrush *brush)
231 LOGBRUSH lb;
232 HBRUSH gdibrush;
234 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
236 gdibrush = CreateBrushIndirect(&lb);
237 free_gdi_logbrush(&lb);
239 return gdibrush;
242 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
244 LOGBRUSH lb;
245 HPEN gdipen;
246 REAL width;
247 INT save_state, i, numdashes;
248 GpPointF pt[2];
249 DWORD dash_array[MAX_DASHLEN];
251 save_state = SaveDC(graphics->hdc);
253 EndPath(graphics->hdc);
255 if(pen->unit == UnitPixel){
256 width = pen->width;
258 else{
259 /* Get an estimate for the amount the pen width is affected by the world
260 * transform. (This is similar to what some of the wine drivers do.) */
261 pt[0].X = 0.0;
262 pt[0].Y = 0.0;
263 pt[1].X = 1.0;
264 pt[1].Y = 1.0;
265 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
266 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
267 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
269 width *= pen->width * convert_unit(graphics_res(graphics),
270 pen->unit == UnitWorld ? graphics->unit : pen->unit);
273 if(pen->dash == DashStyleCustom){
274 numdashes = min(pen->numdashes, MAX_DASHLEN);
276 TRACE("dashes are: ");
277 for(i = 0; i < numdashes; i++){
278 dash_array[i] = roundr(width * pen->dashes[i]);
279 TRACE("%d, ", dash_array[i]);
281 TRACE("\n and the pen style is %x\n", pen->style);
283 create_gdi_logbrush(pen->brush, &lb);
284 gdipen = ExtCreatePen(pen->style, roundr(width), &lb,
285 numdashes, dash_array);
286 free_gdi_logbrush(&lb);
288 else
290 create_gdi_logbrush(pen->brush, &lb);
291 gdipen = ExtCreatePen(pen->style, roundr(width), &lb, 0, NULL);
292 free_gdi_logbrush(&lb);
295 SelectObject(graphics->hdc, gdipen);
297 return save_state;
300 static void restore_dc(GpGraphics *graphics, INT state)
302 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
303 RestoreDC(graphics->hdc, state);
306 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
307 GpCoordinateSpace src_space, GpMatrix **matrix);
309 /* This helper applies all the changes that the points listed in ptf need in
310 * order to be drawn on the device context. In the end, this should include at
311 * least:
312 * -scaling by page unit
313 * -applying world transformation
314 * -converting from float to int
315 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
316 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
317 * gdi to draw, and these functions would irreparably mess with line widths.
319 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
320 GpPointF *ptf, INT count)
322 REAL unitscale;
323 GpMatrix *matrix;
324 int i;
326 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
328 /* apply page scale */
329 if(graphics->unit != UnitDisplay)
330 unitscale *= graphics->scale;
332 GdipCloneMatrix(graphics->worldtrans, &matrix);
333 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
334 GdipTransformMatrixPoints(matrix, ptf, count);
335 GdipDeleteMatrix(matrix);
337 for(i = 0; i < count; i++){
338 pti[i].x = roundr(ptf[i].X);
339 pti[i].y = roundr(ptf[i].Y);
343 /* Draw non-premultiplied ARGB data to the given graphics object */
344 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
345 const BYTE *src, INT src_width, INT src_height, INT src_stride)
347 if (graphics->image && graphics->image->type == ImageTypeBitmap)
349 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
350 INT x, y;
352 for (x=0; x<src_width; x++)
354 for (y=0; y<src_height; y++)
356 ARGB dst_color, src_color;
357 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
358 src_color = ((ARGB*)(src + src_stride * y))[x];
359 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
363 return Ok;
365 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
367 ERR("This should not be used for metafiles; fix caller\n");
368 return NotImplemented;
370 else
372 HDC hdc;
373 HBITMAP hbitmap, old_hbm=NULL;
374 BITMAPINFOHEADER bih;
375 BYTE *temp_bits;
376 BLENDFUNCTION bf;
378 hdc = CreateCompatibleDC(0);
380 bih.biSize = sizeof(BITMAPINFOHEADER);
381 bih.biWidth = src_width;
382 bih.biHeight = -src_height;
383 bih.biPlanes = 1;
384 bih.biBitCount = 32;
385 bih.biCompression = BI_RGB;
386 bih.biSizeImage = 0;
387 bih.biXPelsPerMeter = 0;
388 bih.biYPelsPerMeter = 0;
389 bih.biClrUsed = 0;
390 bih.biClrImportant = 0;
392 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
393 (void**)&temp_bits, NULL, 0);
395 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
396 4 * src_width, src, src_stride);
398 old_hbm = SelectObject(hdc, hbitmap);
400 bf.BlendOp = AC_SRC_OVER;
401 bf.BlendFlags = 0;
402 bf.SourceConstantAlpha = 255;
403 bf.AlphaFormat = AC_SRC_ALPHA;
405 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, src_width, src_height,
406 hdc, 0, 0, src_width, src_height, bf);
408 SelectObject(hdc, old_hbm);
409 DeleteDC(hdc);
410 DeleteObject(hbitmap);
412 return Ok;
416 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
417 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
419 GpStatus stat=Ok;
421 if (graphics->image && graphics->image->type == ImageTypeBitmap)
423 int i, size;
424 RGNDATA *rgndata;
425 RECT *rects;
427 size = GetRegionData(hregion, 0, NULL);
429 rgndata = GdipAlloc(size);
430 if (!rgndata)
431 return OutOfMemory;
433 GetRegionData(hregion, size, rgndata);
435 rects = (RECT*)&rgndata->Buffer;
437 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
439 stat = alpha_blend_pixels(graphics, rects[i].left, rects[i].top,
440 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
441 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
442 src_stride);
445 GdipFree(rgndata);
447 return stat;
449 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
451 ERR("This should not be used for metafiles; fix caller\n");
452 return NotImplemented;
454 else
456 int save;
458 save = SaveDC(graphics->hdc);
460 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
462 stat = alpha_blend_pixels(graphics, dst_x, dst_y, src, src_width,
463 src_height, src_stride);
465 RestoreDC(graphics->hdc, save);
467 return stat;
471 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
473 ARGB result=0;
474 ARGB i;
475 INT a1, a2, a3;
477 a1 = (start >> 24) & 0xff;
478 a2 = (end >> 24) & 0xff;
480 a3 = (int)(a1*(1.0f - position)+a2*(position));
482 result |= a3 << 24;
484 for (i=0xff; i<=0xff0000; i = i << 8)
485 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
486 return result;
489 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
491 REAL blendfac;
493 /* clamp to between 0.0 and 1.0, using the wrap mode */
494 if (brush->wrap == WrapModeTile)
496 position = fmodf(position, 1.0f);
497 if (position < 0.0f) position += 1.0f;
499 else /* WrapModeFlip* */
501 position = fmodf(position, 2.0f);
502 if (position < 0.0f) position += 2.0f;
503 if (position > 1.0f) position = 2.0f - position;
506 if (brush->blendcount == 1)
507 blendfac = position;
508 else
510 int i=1;
511 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
512 REAL range;
514 /* locate the blend positions surrounding this position */
515 while (position > brush->blendpos[i])
516 i++;
518 /* interpolate between the blend positions */
519 left_blendpos = brush->blendpos[i-1];
520 left_blendfac = brush->blendfac[i-1];
521 right_blendpos = brush->blendpos[i];
522 right_blendfac = brush->blendfac[i];
523 range = right_blendpos - left_blendpos;
524 blendfac = (left_blendfac * (right_blendpos - position) +
525 right_blendfac * (position - left_blendpos)) / range;
528 if (brush->pblendcount == 0)
529 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
530 else
532 int i=1;
533 ARGB left_blendcolor, right_blendcolor;
534 REAL left_blendpos, right_blendpos;
536 /* locate the blend colors surrounding this position */
537 while (blendfac > brush->pblendpos[i])
538 i++;
540 /* interpolate between the blend colors */
541 left_blendpos = brush->pblendpos[i-1];
542 left_blendcolor = brush->pblendcolor[i-1];
543 right_blendpos = brush->pblendpos[i];
544 right_blendcolor = brush->pblendcolor[i];
545 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
546 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
550 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
552 REAL val[5], res[4];
553 int i, j;
554 unsigned char a, r, g, b;
556 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
557 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
558 val[2] = (color & 0xff) / 255.0; /* blue */
559 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
560 val[4] = 1.0; /* translation */
562 for (i=0; i<4; i++)
564 res[i] = 0.0;
566 for (j=0; j<5; j++)
567 res[i] += matrix->m[j][i] * val[j];
570 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
571 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
572 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
573 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
575 return (a << 24) | (r << 16) | (g << 8) | b;
578 static int color_is_gray(ARGB color)
580 unsigned char r, g, b;
582 r = (color >> 16) & 0xff;
583 g = (color >> 8) & 0xff;
584 b = color & 0xff;
586 return (r == g) && (g == b);
589 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
590 UINT width, UINT height, INT stride, ColorAdjustType type)
592 UINT x, y, i;
594 if (attributes->colorkeys[type].enabled ||
595 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
597 const struct color_key *key;
598 BYTE min_blue, min_green, min_red;
599 BYTE max_blue, max_green, max_red;
601 if (attributes->colorkeys[type].enabled)
602 key = &attributes->colorkeys[type];
603 else
604 key = &attributes->colorkeys[ColorAdjustTypeDefault];
606 min_blue = key->low&0xff;
607 min_green = (key->low>>8)&0xff;
608 min_red = (key->low>>16)&0xff;
610 max_blue = key->high&0xff;
611 max_green = (key->high>>8)&0xff;
612 max_red = (key->high>>16)&0xff;
614 for (x=0; x<width; x++)
615 for (y=0; y<height; y++)
617 ARGB *src_color;
618 BYTE blue, green, red;
619 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
620 blue = *src_color&0xff;
621 green = (*src_color>>8)&0xff;
622 red = (*src_color>>16)&0xff;
623 if (blue >= min_blue && green >= min_green && red >= min_red &&
624 blue <= max_blue && green <= max_green && red <= max_red)
625 *src_color = 0x00000000;
629 if (attributes->colorremaptables[type].enabled ||
630 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
632 const struct color_remap_table *table;
634 if (attributes->colorremaptables[type].enabled)
635 table = &attributes->colorremaptables[type];
636 else
637 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
639 for (x=0; x<width; x++)
640 for (y=0; y<height; y++)
642 ARGB *src_color;
643 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
644 for (i=0; i<table->mapsize; i++)
646 if (*src_color == table->colormap[i].oldColor.Argb)
648 *src_color = table->colormap[i].newColor.Argb;
649 break;
655 if (attributes->colormatrices[type].enabled ||
656 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
658 const struct color_matrix *colormatrices;
660 if (attributes->colormatrices[type].enabled)
661 colormatrices = &attributes->colormatrices[type];
662 else
663 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
665 for (x=0; x<width; x++)
666 for (y=0; y<height; y++)
668 ARGB *src_color;
669 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
671 if (colormatrices->flags == ColorMatrixFlagsDefault ||
672 !color_is_gray(*src_color))
674 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
676 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
678 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
683 if (attributes->gamma_enabled[type] ||
684 attributes->gamma_enabled[ColorAdjustTypeDefault])
686 REAL gamma;
688 if (attributes->gamma_enabled[type])
689 gamma = attributes->gamma[type];
690 else
691 gamma = attributes->gamma[ColorAdjustTypeDefault];
693 for (x=0; x<width; x++)
694 for (y=0; y<height; y++)
696 ARGB *src_color;
697 BYTE blue, green, red;
698 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
700 blue = *src_color&0xff;
701 green = (*src_color>>8)&0xff;
702 red = (*src_color>>16)&0xff;
704 /* FIXME: We should probably use a table for this. */
705 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
706 green = floorf(powf(green / 255.0, gamma) * 255.0);
707 red = floorf(powf(red / 255.0, gamma) * 255.0);
709 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
714 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
715 * bitmap that contains all the pixels we may need to draw it. */
716 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
717 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
718 GpRect *rect)
720 INT left, top, right, bottom;
722 switch (interpolation)
724 case InterpolationModeHighQualityBilinear:
725 case InterpolationModeHighQualityBicubic:
726 /* FIXME: Include a greater range for the prefilter? */
727 case InterpolationModeBicubic:
728 case InterpolationModeBilinear:
729 left = (INT)(floorf(srcx));
730 top = (INT)(floorf(srcy));
731 right = (INT)(ceilf(srcx+srcwidth));
732 bottom = (INT)(ceilf(srcy+srcheight));
733 break;
734 case InterpolationModeNearestNeighbor:
735 default:
736 left = roundr(srcx);
737 top = roundr(srcy);
738 right = roundr(srcx+srcwidth);
739 bottom = roundr(srcy+srcheight);
740 break;
743 if (wrap == WrapModeClamp)
745 if (left < 0)
746 left = 0;
747 if (top < 0)
748 top = 0;
749 if (right >= bitmap->width)
750 right = bitmap->width-1;
751 if (bottom >= bitmap->height)
752 bottom = bitmap->height-1;
754 else
756 /* In some cases we can make the rectangle smaller here, but the logic
757 * is hard to get right, and tiling suggests we're likely to use the
758 * entire source image. */
759 if (left < 0 || right >= bitmap->width)
761 left = 0;
762 right = bitmap->width-1;
765 if (top < 0 || bottom >= bitmap->height)
767 top = 0;
768 bottom = bitmap->height-1;
772 rect->X = left;
773 rect->Y = top;
774 rect->Width = right - left + 1;
775 rect->Height = bottom - top + 1;
778 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
779 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
781 if (attributes->wrap == WrapModeClamp)
783 if (x < 0 || y < 0 || x >= width || y >= height)
784 return attributes->outside_color;
786 else
788 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
789 if (x < 0)
790 x = width*2 + x % (width * 2);
791 if (y < 0)
792 y = height*2 + y % (height * 2);
794 if ((attributes->wrap & 1) == 1)
796 /* Flip X */
797 if ((x / width) % 2 == 0)
798 x = x % width;
799 else
800 x = width - 1 - x % width;
802 else
803 x = x % width;
805 if ((attributes->wrap & 2) == 2)
807 /* Flip Y */
808 if ((y / height) % 2 == 0)
809 y = y % height;
810 else
811 y = height - 1 - y % height;
813 else
814 y = y % height;
817 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
819 ERR("out of range pixel requested\n");
820 return 0xffcd0084;
823 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
826 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
827 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
828 InterpolationMode interpolation)
830 static int fixme;
832 switch (interpolation)
834 default:
835 if (!fixme++)
836 FIXME("Unimplemented interpolation %i\n", interpolation);
837 /* fall-through */
838 case InterpolationModeBilinear:
840 REAL leftxf, topyf;
841 INT leftx, rightx, topy, bottomy;
842 ARGB topleft, topright, bottomleft, bottomright;
843 ARGB top, bottom;
844 float x_offset;
846 leftxf = floorf(point->X);
847 leftx = (INT)leftxf;
848 rightx = (INT)ceilf(point->X);
849 topyf = floorf(point->Y);
850 topy = (INT)topyf;
851 bottomy = (INT)ceilf(point->Y);
853 if (leftx == rightx && topy == bottomy)
854 return sample_bitmap_pixel(src_rect, bits, width, height,
855 leftx, topy, attributes);
857 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
858 leftx, topy, attributes);
859 topright = sample_bitmap_pixel(src_rect, bits, width, height,
860 rightx, topy, attributes);
861 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
862 leftx, bottomy, attributes);
863 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
864 rightx, bottomy, attributes);
866 x_offset = point->X - leftxf;
867 top = blend_colors(topleft, topright, x_offset);
868 bottom = blend_colors(bottomleft, bottomright, x_offset);
870 return blend_colors(top, bottom, point->Y - topyf);
872 case InterpolationModeNearestNeighbor:
873 return sample_bitmap_pixel(src_rect, bits, width, height,
874 roundr(point->X), roundr(point->Y), attributes);
878 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
880 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
883 static INT brush_can_fill_path(GpBrush *brush)
885 switch (brush->bt)
887 case BrushTypeSolidColor:
888 return 1;
889 case BrushTypeHatchFill:
891 GpHatch *hatch = (GpHatch*)brush;
892 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
893 ((hatch->backcol & 0xff000000) == 0xff000000);
895 case BrushTypeLinearGradient:
896 case BrushTypeTextureFill:
897 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
898 default:
899 return 0;
903 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
905 switch (brush->bt)
907 case BrushTypeSolidColor:
909 GpSolidFill *fill = (GpSolidFill*)brush;
910 HBITMAP bmp = ARGB2BMP(fill->color);
912 if (bmp)
914 RECT rc;
915 /* partially transparent fill */
917 SelectClipPath(graphics->hdc, RGN_AND);
918 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
920 HDC hdc = CreateCompatibleDC(NULL);
921 BLENDFUNCTION bf;
923 if (!hdc) break;
925 SelectObject(hdc, bmp);
927 bf.BlendOp = AC_SRC_OVER;
928 bf.BlendFlags = 0;
929 bf.SourceConstantAlpha = 255;
930 bf.AlphaFormat = AC_SRC_ALPHA;
932 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
934 DeleteDC(hdc);
937 DeleteObject(bmp);
938 break;
940 /* else fall through */
942 default:
944 HBRUSH gdibrush, old_brush;
946 gdibrush = create_gdi_brush(brush);
947 if (!gdibrush) return;
949 old_brush = SelectObject(graphics->hdc, gdibrush);
950 FillPath(graphics->hdc);
951 SelectObject(graphics->hdc, old_brush);
952 DeleteObject(gdibrush);
953 break;
958 static INT brush_can_fill_pixels(GpBrush *brush)
960 switch (brush->bt)
962 case BrushTypeSolidColor:
963 case BrushTypeHatchFill:
964 case BrushTypeLinearGradient:
965 case BrushTypeTextureFill:
966 case BrushTypePathGradient:
967 return 1;
968 default:
969 return 0;
973 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
974 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
976 switch (brush->bt)
978 case BrushTypeSolidColor:
980 int x, y;
981 GpSolidFill *fill = (GpSolidFill*)brush;
982 for (x=0; x<fill_area->Width; x++)
983 for (y=0; y<fill_area->Height; y++)
984 argb_pixels[x + y*cdwStride] = fill->color;
985 return Ok;
987 case BrushTypeHatchFill:
989 int x, y;
990 GpHatch *fill = (GpHatch*)brush;
991 const char *hatch_data;
993 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
994 return NotImplemented;
996 for (x=0; x<fill_area->Width; x++)
997 for (y=0; y<fill_area->Height; y++)
999 int hx, hy;
1001 /* FIXME: Account for the rendering origin */
1002 hx = (x + fill_area->X) % 8;
1003 hy = (y + fill_area->Y) % 8;
1005 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1006 argb_pixels[x + y*cdwStride] = fill->forecol;
1007 else
1008 argb_pixels[x + y*cdwStride] = fill->backcol;
1011 return Ok;
1013 case BrushTypeLinearGradient:
1015 GpLineGradient *fill = (GpLineGradient*)brush;
1016 GpPointF draw_points[3], line_points[3];
1017 GpStatus stat;
1018 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1019 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1020 int x, y;
1022 draw_points[0].X = fill_area->X;
1023 draw_points[0].Y = fill_area->Y;
1024 draw_points[1].X = fill_area->X+1;
1025 draw_points[1].Y = fill_area->Y;
1026 draw_points[2].X = fill_area->X;
1027 draw_points[2].Y = fill_area->Y+1;
1029 /* Transform the points to a co-ordinate space where X is the point's
1030 * position in the gradient, 0.0 being the start point and 1.0 the
1031 * end point. */
1032 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1033 CoordinateSpaceDevice, draw_points, 3);
1035 if (stat == Ok)
1037 line_points[0] = fill->startpoint;
1038 line_points[1] = fill->endpoint;
1039 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1040 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1042 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1045 if (stat == Ok)
1047 stat = GdipInvertMatrix(world_to_gradient);
1049 if (stat == Ok)
1050 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1052 GdipDeleteMatrix(world_to_gradient);
1055 if (stat == Ok)
1057 REAL x_delta = draw_points[1].X - draw_points[0].X;
1058 REAL y_delta = draw_points[2].X - draw_points[0].X;
1060 for (y=0; y<fill_area->Height; y++)
1062 for (x=0; x<fill_area->Width; x++)
1064 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1066 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1071 return stat;
1073 case BrushTypeTextureFill:
1075 GpTexture *fill = (GpTexture*)brush;
1076 GpPointF draw_points[3];
1077 GpStatus stat;
1078 GpMatrix *world_to_texture;
1079 int x, y;
1080 GpBitmap *bitmap;
1081 int src_stride;
1082 GpRect src_area;
1084 if (fill->image->type != ImageTypeBitmap)
1086 FIXME("metafile texture brushes not implemented\n");
1087 return NotImplemented;
1090 bitmap = (GpBitmap*)fill->image;
1091 src_stride = sizeof(ARGB) * bitmap->width;
1093 src_area.X = src_area.Y = 0;
1094 src_area.Width = bitmap->width;
1095 src_area.Height = bitmap->height;
1097 draw_points[0].X = fill_area->X;
1098 draw_points[0].Y = fill_area->Y;
1099 draw_points[1].X = fill_area->X+1;
1100 draw_points[1].Y = fill_area->Y;
1101 draw_points[2].X = fill_area->X;
1102 draw_points[2].Y = fill_area->Y+1;
1104 /* Transform the points to the co-ordinate space of the bitmap. */
1105 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1106 CoordinateSpaceDevice, draw_points, 3);
1108 if (stat == Ok)
1110 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
1113 if (stat == Ok)
1115 stat = GdipInvertMatrix(world_to_texture);
1117 if (stat == Ok)
1118 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
1120 GdipDeleteMatrix(world_to_texture);
1123 if (stat == Ok && !fill->bitmap_bits)
1125 BitmapData lockeddata;
1127 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1128 if (!fill->bitmap_bits)
1129 stat = OutOfMemory;
1131 if (stat == Ok)
1133 lockeddata.Width = bitmap->width;
1134 lockeddata.Height = bitmap->height;
1135 lockeddata.Stride = src_stride;
1136 lockeddata.PixelFormat = PixelFormat32bppARGB;
1137 lockeddata.Scan0 = fill->bitmap_bits;
1139 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1140 PixelFormat32bppARGB, &lockeddata);
1143 if (stat == Ok)
1144 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1146 if (stat == Ok)
1147 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1148 bitmap->width, bitmap->height,
1149 src_stride, ColorAdjustTypeBitmap);
1151 if (stat != Ok)
1153 GdipFree(fill->bitmap_bits);
1154 fill->bitmap_bits = NULL;
1158 if (stat == Ok)
1160 REAL x_dx = draw_points[1].X - draw_points[0].X;
1161 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1162 REAL y_dx = draw_points[2].X - draw_points[0].X;
1163 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1165 for (y=0; y<fill_area->Height; y++)
1167 for (x=0; x<fill_area->Width; x++)
1169 GpPointF point;
1170 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1171 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1173 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1174 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1175 &point, fill->imageattributes, graphics->interpolation);
1180 return stat;
1182 case BrushTypePathGradient:
1184 GpPathGradient *fill = (GpPathGradient*)brush;
1185 GpPath *flat_path;
1186 GpMatrix *world_to_device;
1187 GpStatus stat;
1188 int i, figure_start=0;
1189 GpPointF start_point, end_point, center_point;
1190 BYTE type;
1191 REAL min_yf, max_yf, line1_xf, line2_xf;
1192 INT min_y, max_y, min_x, max_x;
1193 INT x, y;
1194 ARGB outer_color=0xffffffff;
1196 stat = GdipClonePath(fill->path, &flat_path);
1198 if (stat != Ok)
1199 return stat;
1201 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1202 CoordinateSpaceWorld, &world_to_device);
1203 if (stat == Ok)
1205 stat = GdipTransformPath(flat_path, world_to_device);
1207 if (stat == Ok)
1209 center_point = fill->center;
1210 stat = GdipTransformMatrixPoints(world_to_device, &center_point, 1);
1213 if (stat == Ok)
1214 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1216 GdipDeleteMatrix(world_to_device);
1219 if (stat != Ok)
1221 GdipDeletePath(flat_path);
1222 return stat;
1225 for (i=0; i<flat_path->pathdata.Count; i++)
1227 int start_center_line=0, end_center_line=0;
1228 int seen_start=0, seen_end=0, seen_center=0;
1229 REAL center_distance;
1231 type = flat_path->pathdata.Types[i];
1233 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1234 figure_start = i;
1236 start_point = flat_path->pathdata.Points[i];
1238 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1239 end_point = flat_path->pathdata.Points[figure_start];
1240 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1241 end_point = flat_path->pathdata.Points[i+1];
1242 else
1243 continue;
1245 min_yf = center_point.Y;
1246 if (min_yf > start_point.Y) min_yf = start_point.Y;
1247 if (min_yf > end_point.Y) min_yf = end_point.Y;
1249 if (min_yf < fill_area->Y)
1250 min_y = fill_area->Y;
1251 else
1252 min_y = (INT)ceil(min_yf);
1254 max_yf = center_point.Y;
1255 if (max_yf < start_point.Y) max_yf = start_point.Y;
1256 if (max_yf < end_point.Y) max_yf = end_point.Y;
1258 if (max_yf > fill_area->Y + fill_area->Height)
1259 max_y = fill_area->Y + fill_area->Height;
1260 else
1261 max_y = (INT)ceil(max_yf);
1263 /* This is proportional to the distance from start-end line to center point. */
1264 center_distance = (end_point.Y - start_point.Y) * (start_point.X - center_point.X) +
1265 (end_point.X - start_point.X) * (center_point.Y - start_point.Y);
1267 for (y=min_y; y<max_y; y++)
1269 REAL yf = (REAL)y;
1271 if (!seen_start && yf >= start_point.Y)
1273 seen_start = 1;
1274 start_center_line ^= 1;
1276 if (!seen_end && yf >= end_point.Y)
1278 seen_end = 1;
1279 end_center_line ^= 1;
1281 if (!seen_center && yf >= center_point.Y)
1283 seen_center = 1;
1284 start_center_line ^= 1;
1285 end_center_line ^= 1;
1288 if (start_center_line)
1289 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1290 else
1291 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1293 if (end_center_line)
1294 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1295 else
1296 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1298 if (line1_xf < line2_xf)
1300 min_x = (INT)ceil(line1_xf);
1301 max_x = (INT)ceil(line2_xf);
1303 else
1305 min_x = (INT)ceil(line2_xf);
1306 max_x = (INT)ceil(line1_xf);
1309 if (min_x < fill_area->X)
1310 min_x = fill_area->X;
1311 if (max_x > fill_area->X + fill_area->Width)
1312 max_x = fill_area->X + fill_area->Width;
1314 for (x=min_x; x<max_x; x++)
1316 REAL distance;
1318 distance = (end_point.Y - start_point.Y) * (start_point.X - (REAL)x) +
1319 (end_point.X - start_point.X) * (yf - start_point.Y);
1321 distance = distance / center_distance;
1323 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1324 blend_colors(outer_color, fill->centercolor, distance);
1329 GdipDeletePath(flat_path);
1330 return stat;
1332 default:
1333 return NotImplemented;
1337 /* GdipDrawPie/GdipFillPie helper function */
1338 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1339 REAL height, REAL startAngle, REAL sweepAngle)
1341 GpPointF ptf[4];
1342 POINT pti[4];
1344 ptf[0].X = x;
1345 ptf[0].Y = y;
1346 ptf[1].X = x + width;
1347 ptf[1].Y = y + height;
1349 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1350 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1352 transform_and_round_points(graphics, pti, ptf, 4);
1354 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1355 pti[2].y, pti[3].x, pti[3].y);
1358 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1359 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1360 * should not be called on an hdc that has a path you care about. */
1361 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1362 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1364 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1365 GpMatrix *matrix = NULL;
1366 HBRUSH brush = NULL;
1367 HPEN pen = NULL;
1368 PointF ptf[4], *custptf = NULL;
1369 POINT pt[4], *custpt = NULL;
1370 BYTE *tp = NULL;
1371 REAL theta, dsmall, dbig, dx, dy = 0.0;
1372 INT i, count;
1373 LOGBRUSH lb;
1374 BOOL customstroke;
1376 if((x1 == x2) && (y1 == y2))
1377 return;
1379 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1381 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1382 if(!customstroke){
1383 brush = CreateSolidBrush(color);
1384 lb.lbStyle = BS_SOLID;
1385 lb.lbColor = color;
1386 lb.lbHatch = 0;
1387 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1388 PS_JOIN_MITER, 1, &lb, 0,
1389 NULL);
1390 oldbrush = SelectObject(graphics->hdc, brush);
1391 oldpen = SelectObject(graphics->hdc, pen);
1394 switch(cap){
1395 case LineCapFlat:
1396 break;
1397 case LineCapSquare:
1398 case LineCapSquareAnchor:
1399 case LineCapDiamondAnchor:
1400 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1401 if(cap == LineCapDiamondAnchor){
1402 dsmall = cos(theta + M_PI_2) * size;
1403 dbig = sin(theta + M_PI_2) * size;
1405 else{
1406 dsmall = cos(theta + M_PI_4) * size;
1407 dbig = sin(theta + M_PI_4) * size;
1410 ptf[0].X = x2 - dsmall;
1411 ptf[1].X = x2 + dbig;
1413 ptf[0].Y = y2 - dbig;
1414 ptf[3].Y = y2 + dsmall;
1416 ptf[1].Y = y2 - dsmall;
1417 ptf[2].Y = y2 + dbig;
1419 ptf[3].X = x2 - dbig;
1420 ptf[2].X = x2 + dsmall;
1422 transform_and_round_points(graphics, pt, ptf, 4);
1423 Polygon(graphics->hdc, pt, 4);
1425 break;
1426 case LineCapArrowAnchor:
1427 size = size * 4.0 / sqrt(3.0);
1429 dx = cos(M_PI / 6.0 + theta) * size;
1430 dy = sin(M_PI / 6.0 + theta) * size;
1432 ptf[0].X = x2 - dx;
1433 ptf[0].Y = y2 - dy;
1435 dx = cos(- M_PI / 6.0 + theta) * size;
1436 dy = sin(- M_PI / 6.0 + theta) * size;
1438 ptf[1].X = x2 - dx;
1439 ptf[1].Y = y2 - dy;
1441 ptf[2].X = x2;
1442 ptf[2].Y = y2;
1444 transform_and_round_points(graphics, pt, ptf, 3);
1445 Polygon(graphics->hdc, pt, 3);
1447 break;
1448 case LineCapRoundAnchor:
1449 dx = dy = ANCHOR_WIDTH * size / 2.0;
1451 ptf[0].X = x2 - dx;
1452 ptf[0].Y = y2 - dy;
1453 ptf[1].X = x2 + dx;
1454 ptf[1].Y = y2 + dy;
1456 transform_and_round_points(graphics, pt, ptf, 2);
1457 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1459 break;
1460 case LineCapTriangle:
1461 size = size / 2.0;
1462 dx = cos(M_PI_2 + theta) * size;
1463 dy = sin(M_PI_2 + theta) * size;
1465 ptf[0].X = x2 - dx;
1466 ptf[0].Y = y2 - dy;
1467 ptf[1].X = x2 + dx;
1468 ptf[1].Y = y2 + dy;
1470 dx = cos(theta) * size;
1471 dy = sin(theta) * size;
1473 ptf[2].X = x2 + dx;
1474 ptf[2].Y = y2 + dy;
1476 transform_and_round_points(graphics, pt, ptf, 3);
1477 Polygon(graphics->hdc, pt, 3);
1479 break;
1480 case LineCapRound:
1481 dx = dy = size / 2.0;
1483 ptf[0].X = x2 - dx;
1484 ptf[0].Y = y2 - dy;
1485 ptf[1].X = x2 + dx;
1486 ptf[1].Y = y2 + dy;
1488 dx = -cos(M_PI_2 + theta) * size;
1489 dy = -sin(M_PI_2 + theta) * size;
1491 ptf[2].X = x2 - dx;
1492 ptf[2].Y = y2 - dy;
1493 ptf[3].X = x2 + dx;
1494 ptf[3].Y = y2 + dy;
1496 transform_and_round_points(graphics, pt, ptf, 4);
1497 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1498 pt[2].y, pt[3].x, pt[3].y);
1500 break;
1501 case LineCapCustom:
1502 if(!custom)
1503 break;
1505 count = custom->pathdata.Count;
1506 custptf = GdipAlloc(count * sizeof(PointF));
1507 custpt = GdipAlloc(count * sizeof(POINT));
1508 tp = GdipAlloc(count);
1510 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1511 goto custend;
1513 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1515 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1516 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1517 MatrixOrderAppend);
1518 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1519 GdipTransformMatrixPoints(matrix, custptf, count);
1521 transform_and_round_points(graphics, custpt, custptf, count);
1523 for(i = 0; i < count; i++)
1524 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1526 if(custom->fill){
1527 BeginPath(graphics->hdc);
1528 PolyDraw(graphics->hdc, custpt, tp, count);
1529 EndPath(graphics->hdc);
1530 StrokeAndFillPath(graphics->hdc);
1532 else
1533 PolyDraw(graphics->hdc, custpt, tp, count);
1535 custend:
1536 GdipFree(custptf);
1537 GdipFree(custpt);
1538 GdipFree(tp);
1539 GdipDeleteMatrix(matrix);
1540 break;
1541 default:
1542 break;
1545 if(!customstroke){
1546 SelectObject(graphics->hdc, oldbrush);
1547 SelectObject(graphics->hdc, oldpen);
1548 DeleteObject(brush);
1549 DeleteObject(pen);
1553 /* Shortens the line by the given percent by changing x2, y2.
1554 * If percent is > 1.0 then the line will change direction.
1555 * If percent is negative it can lengthen the line. */
1556 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1558 REAL dist, theta, dx, dy;
1560 if((y1 == *y2) && (x1 == *x2))
1561 return;
1563 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1564 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1565 dx = cos(theta) * dist;
1566 dy = sin(theta) * dist;
1568 *x2 = *x2 + dx;
1569 *y2 = *y2 + dy;
1572 /* Shortens the line by the given amount by changing x2, y2.
1573 * If the amount is greater than the distance, the line will become length 0.
1574 * If the amount is negative, it can lengthen the line. */
1575 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1577 REAL dx, dy, percent;
1579 dx = *x2 - x1;
1580 dy = *y2 - y1;
1581 if(dx == 0 && dy == 0)
1582 return;
1584 percent = amt / sqrt(dx * dx + dy * dy);
1585 if(percent >= 1.0){
1586 *x2 = x1;
1587 *y2 = y1;
1588 return;
1591 shorten_line_percent(x1, y1, x2, y2, percent);
1594 /* Draws lines between the given points, and if caps is true then draws an endcap
1595 * at the end of the last line. */
1596 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1597 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1599 POINT *pti = NULL;
1600 GpPointF *ptcopy = NULL;
1601 GpStatus status = GenericError;
1603 if(!count)
1604 return Ok;
1606 pti = GdipAlloc(count * sizeof(POINT));
1607 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1609 if(!pti || !ptcopy){
1610 status = OutOfMemory;
1611 goto end;
1614 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1616 if(caps){
1617 if(pen->endcap == LineCapArrowAnchor)
1618 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1619 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1620 else if((pen->endcap == LineCapCustom) && pen->customend)
1621 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1622 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1623 pen->customend->inset * pen->width);
1625 if(pen->startcap == LineCapArrowAnchor)
1626 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1627 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1628 else if((pen->startcap == LineCapCustom) && pen->customstart)
1629 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1630 &ptcopy[0].X, &ptcopy[0].Y,
1631 pen->customstart->inset * pen->width);
1633 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1634 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1635 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1636 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1639 transform_and_round_points(graphics, pti, ptcopy, count);
1641 if(Polyline(graphics->hdc, pti, count))
1642 status = Ok;
1644 end:
1645 GdipFree(pti);
1646 GdipFree(ptcopy);
1648 return status;
1651 /* Conducts a linear search to find the bezier points that will back off
1652 * the endpoint of the curve by a distance of amt. Linear search works
1653 * better than binary in this case because there are multiple solutions,
1654 * and binary searches often find a bad one. I don't think this is what
1655 * Windows does but short of rendering the bezier without GDI's help it's
1656 * the best we can do. If rev then work from the start of the passed points
1657 * instead of the end. */
1658 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1660 GpPointF origpt[4];
1661 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1662 INT i, first = 0, second = 1, third = 2, fourth = 3;
1664 if(rev){
1665 first = 3;
1666 second = 2;
1667 third = 1;
1668 fourth = 0;
1671 origx = pt[fourth].X;
1672 origy = pt[fourth].Y;
1673 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1675 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1676 /* reset bezier points to original values */
1677 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1678 /* Perform magic on bezier points. Order is important here.*/
1679 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1680 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1681 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1682 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1683 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1684 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1686 dx = pt[fourth].X - origx;
1687 dy = pt[fourth].Y - origy;
1689 diff = sqrt(dx * dx + dy * dy);
1690 percent += 0.0005 * amt;
1694 /* Draws bezier curves between given points, and if caps is true then draws an
1695 * endcap at the end of the last line. */
1696 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1697 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1699 POINT *pti;
1700 GpPointF *ptcopy;
1701 GpStatus status = GenericError;
1703 if(!count)
1704 return Ok;
1706 pti = GdipAlloc(count * sizeof(POINT));
1707 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1709 if(!pti || !ptcopy){
1710 status = OutOfMemory;
1711 goto end;
1714 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1716 if(caps){
1717 if(pen->endcap == LineCapArrowAnchor)
1718 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1719 else if((pen->endcap == LineCapCustom) && pen->customend)
1720 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1721 FALSE);
1723 if(pen->startcap == LineCapArrowAnchor)
1724 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1725 else if((pen->startcap == LineCapCustom) && pen->customstart)
1726 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1728 /* the direction of the line cap is parallel to the direction at the
1729 * end of the bezier (which, if it has been shortened, is not the same
1730 * as the direction from pt[count-2] to pt[count-1]) */
1731 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1732 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1733 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1734 pt[count - 1].X, pt[count - 1].Y);
1736 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1737 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1738 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1741 transform_and_round_points(graphics, pti, ptcopy, count);
1743 PolyBezier(graphics->hdc, pti, count);
1745 status = Ok;
1747 end:
1748 GdipFree(pti);
1749 GdipFree(ptcopy);
1751 return status;
1754 /* Draws a combination of bezier curves and lines between points. */
1755 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1756 GDIPCONST BYTE * types, INT count, BOOL caps)
1758 POINT *pti = GdipAlloc(count * sizeof(POINT));
1759 BYTE *tp = GdipAlloc(count);
1760 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1761 INT i, j;
1762 GpStatus status = GenericError;
1764 if(!count){
1765 status = Ok;
1766 goto end;
1768 if(!pti || !tp || !ptcopy){
1769 status = OutOfMemory;
1770 goto end;
1773 for(i = 1; i < count; i++){
1774 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1775 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1776 || !(types[i + 1] & PathPointTypeBezier)){
1777 ERR("Bad bezier points\n");
1778 goto end;
1780 i += 2;
1784 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1786 /* If we are drawing caps, go through the points and adjust them accordingly,
1787 * and draw the caps. */
1788 if(caps){
1789 switch(types[count - 1] & PathPointTypePathTypeMask){
1790 case PathPointTypeBezier:
1791 if(pen->endcap == LineCapArrowAnchor)
1792 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1793 else if((pen->endcap == LineCapCustom) && pen->customend)
1794 shorten_bezier_amt(&ptcopy[count - 4],
1795 pen->width * pen->customend->inset, FALSE);
1797 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1798 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1799 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1800 pt[count - 1].X, pt[count - 1].Y);
1802 break;
1803 case PathPointTypeLine:
1804 if(pen->endcap == LineCapArrowAnchor)
1805 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1806 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1807 pen->width);
1808 else if((pen->endcap == LineCapCustom) && pen->customend)
1809 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1810 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1811 pen->customend->inset * pen->width);
1813 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1814 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1815 pt[count - 1].Y);
1817 break;
1818 default:
1819 ERR("Bad path last point\n");
1820 goto end;
1823 /* Find start of points */
1824 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1825 == PathPointTypeStart); j++);
1827 switch(types[j] & PathPointTypePathTypeMask){
1828 case PathPointTypeBezier:
1829 if(pen->startcap == LineCapArrowAnchor)
1830 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1831 else if((pen->startcap == LineCapCustom) && pen->customstart)
1832 shorten_bezier_amt(&ptcopy[j - 1],
1833 pen->width * pen->customstart->inset, TRUE);
1835 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1836 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1837 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1838 pt[j - 1].X, pt[j - 1].Y);
1840 break;
1841 case PathPointTypeLine:
1842 if(pen->startcap == LineCapArrowAnchor)
1843 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1844 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1845 pen->width);
1846 else if((pen->startcap == LineCapCustom) && pen->customstart)
1847 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1848 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1849 pen->customstart->inset * pen->width);
1851 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1852 pt[j].X, pt[j].Y, pt[j - 1].X,
1853 pt[j - 1].Y);
1855 break;
1856 default:
1857 ERR("Bad path points\n");
1858 goto end;
1862 transform_and_round_points(graphics, pti, ptcopy, count);
1864 for(i = 0; i < count; i++){
1865 tp[i] = convert_path_point_type(types[i]);
1868 PolyDraw(graphics->hdc, pti, tp, count);
1870 status = Ok;
1872 end:
1873 GdipFree(pti);
1874 GdipFree(ptcopy);
1875 GdipFree(tp);
1877 return status;
1880 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1882 GpStatus result;
1884 BeginPath(graphics->hdc);
1885 result = draw_poly(graphics, NULL, path->pathdata.Points,
1886 path->pathdata.Types, path->pathdata.Count, FALSE);
1887 EndPath(graphics->hdc);
1888 return result;
1891 typedef struct _GraphicsContainerItem {
1892 struct list entry;
1893 GraphicsContainer contid;
1895 SmoothingMode smoothing;
1896 CompositingQuality compqual;
1897 InterpolationMode interpolation;
1898 CompositingMode compmode;
1899 TextRenderingHint texthint;
1900 REAL scale;
1901 GpUnit unit;
1902 PixelOffsetMode pixeloffset;
1903 UINT textcontrast;
1904 GpMatrix* worldtrans;
1905 GpRegion* clip;
1906 } GraphicsContainerItem;
1908 static GpStatus init_container(GraphicsContainerItem** container,
1909 GDIPCONST GpGraphics* graphics){
1910 GpStatus sts;
1912 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1913 if(!(*container))
1914 return OutOfMemory;
1916 (*container)->contid = graphics->contid + 1;
1918 (*container)->smoothing = graphics->smoothing;
1919 (*container)->compqual = graphics->compqual;
1920 (*container)->interpolation = graphics->interpolation;
1921 (*container)->compmode = graphics->compmode;
1922 (*container)->texthint = graphics->texthint;
1923 (*container)->scale = graphics->scale;
1924 (*container)->unit = graphics->unit;
1925 (*container)->textcontrast = graphics->textcontrast;
1926 (*container)->pixeloffset = graphics->pixeloffset;
1928 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1929 if(sts != Ok){
1930 GdipFree(*container);
1931 *container = NULL;
1932 return sts;
1935 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1936 if(sts != Ok){
1937 GdipDeleteMatrix((*container)->worldtrans);
1938 GdipFree(*container);
1939 *container = NULL;
1940 return sts;
1943 return Ok;
1946 static void delete_container(GraphicsContainerItem* container){
1947 GdipDeleteMatrix(container->worldtrans);
1948 GdipDeleteRegion(container->clip);
1949 GdipFree(container);
1952 static GpStatus restore_container(GpGraphics* graphics,
1953 GDIPCONST GraphicsContainerItem* container){
1954 GpStatus sts;
1955 GpMatrix *newTrans;
1956 GpRegion *newClip;
1958 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1959 if(sts != Ok)
1960 return sts;
1962 sts = GdipCloneRegion(container->clip, &newClip);
1963 if(sts != Ok){
1964 GdipDeleteMatrix(newTrans);
1965 return sts;
1968 GdipDeleteMatrix(graphics->worldtrans);
1969 graphics->worldtrans = newTrans;
1971 GdipDeleteRegion(graphics->clip);
1972 graphics->clip = newClip;
1974 graphics->contid = container->contid - 1;
1976 graphics->smoothing = container->smoothing;
1977 graphics->compqual = container->compqual;
1978 graphics->interpolation = container->interpolation;
1979 graphics->compmode = container->compmode;
1980 graphics->texthint = container->texthint;
1981 graphics->scale = container->scale;
1982 graphics->unit = container->unit;
1983 graphics->textcontrast = container->textcontrast;
1984 graphics->pixeloffset = container->pixeloffset;
1986 return Ok;
1989 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1991 RECT wnd_rect;
1992 GpStatus stat=Ok;
1993 GpUnit unit;
1995 if(graphics->hwnd) {
1996 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1997 return GenericError;
1999 rect->X = wnd_rect.left;
2000 rect->Y = wnd_rect.top;
2001 rect->Width = wnd_rect.right - wnd_rect.left;
2002 rect->Height = wnd_rect.bottom - wnd_rect.top;
2003 }else if (graphics->image){
2004 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2005 if (stat == Ok && unit != UnitPixel)
2006 FIXME("need to convert from unit %i\n", unit);
2007 }else{
2008 rect->X = 0;
2009 rect->Y = 0;
2010 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2011 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2014 return stat;
2017 /* on success, rgn will contain the region of the graphics object which
2018 * is visible after clipping has been applied */
2019 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2021 GpStatus stat;
2022 GpRectF rectf;
2023 GpRegion* tmp;
2025 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2026 return stat;
2028 if((stat = GdipCreateRegion(&tmp)) != Ok)
2029 return stat;
2031 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2032 goto end;
2034 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2035 goto end;
2037 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2039 end:
2040 GdipDeleteRegion(tmp);
2041 return stat;
2044 void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font, HFONT *hfont)
2046 HDC hdc = CreateCompatibleDC(0);
2047 GpPointF pt[3];
2048 REAL angle, rel_width, rel_height;
2049 LOGFONTW lfw;
2050 HFONT unscaled_font;
2051 TEXTMETRICW textmet;
2053 pt[0].X = 0.0;
2054 pt[0].Y = 0.0;
2055 pt[1].X = 1.0;
2056 pt[1].Y = 0.0;
2057 pt[2].X = 0.0;
2058 pt[2].Y = 1.0;
2059 if (graphics)
2060 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2061 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2062 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2063 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2064 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2065 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2067 lfw = font->lfw;
2068 lfw.lfHeight = roundr(-font->pixel_size * rel_height);
2069 unscaled_font = CreateFontIndirectW(&lfw);
2071 SelectObject(hdc, unscaled_font);
2072 GetTextMetricsW(hdc, &textmet);
2074 lfw = font->lfw;
2075 lfw.lfHeight = roundr(-font->pixel_size * rel_height);
2076 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width / rel_height);
2077 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2079 *hfont = CreateFontIndirectW(&lfw);
2081 DeleteDC(hdc);
2082 DeleteObject(unscaled_font);
2085 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2087 TRACE("(%p, %p)\n", hdc, graphics);
2089 return GdipCreateFromHDC2(hdc, NULL, graphics);
2092 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2094 GpStatus retval;
2096 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2098 if(hDevice != NULL) {
2099 FIXME("Don't know how to handle parameter hDevice\n");
2100 return NotImplemented;
2103 if(hdc == NULL)
2104 return OutOfMemory;
2106 if(graphics == NULL)
2107 return InvalidParameter;
2109 *graphics = GdipAlloc(sizeof(GpGraphics));
2110 if(!*graphics) return OutOfMemory;
2112 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2113 GdipFree(*graphics);
2114 return retval;
2117 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2118 GdipFree((*graphics)->worldtrans);
2119 GdipFree(*graphics);
2120 return retval;
2123 (*graphics)->hdc = hdc;
2124 (*graphics)->hwnd = WindowFromDC(hdc);
2125 (*graphics)->owndc = FALSE;
2126 (*graphics)->smoothing = SmoothingModeDefault;
2127 (*graphics)->compqual = CompositingQualityDefault;
2128 (*graphics)->interpolation = InterpolationModeBilinear;
2129 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2130 (*graphics)->compmode = CompositingModeSourceOver;
2131 (*graphics)->unit = UnitDisplay;
2132 (*graphics)->scale = 1.0;
2133 (*graphics)->busy = FALSE;
2134 (*graphics)->textcontrast = 4;
2135 list_init(&(*graphics)->containers);
2136 (*graphics)->contid = 0;
2138 TRACE("<-- %p\n", *graphics);
2140 return Ok;
2143 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2145 GpStatus retval;
2147 *graphics = GdipAlloc(sizeof(GpGraphics));
2148 if(!*graphics) return OutOfMemory;
2150 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2151 GdipFree(*graphics);
2152 return retval;
2155 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2156 GdipFree((*graphics)->worldtrans);
2157 GdipFree(*graphics);
2158 return retval;
2161 (*graphics)->hdc = NULL;
2162 (*graphics)->hwnd = NULL;
2163 (*graphics)->owndc = FALSE;
2164 (*graphics)->image = image;
2165 (*graphics)->smoothing = SmoothingModeDefault;
2166 (*graphics)->compqual = CompositingQualityDefault;
2167 (*graphics)->interpolation = InterpolationModeBilinear;
2168 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2169 (*graphics)->compmode = CompositingModeSourceOver;
2170 (*graphics)->unit = UnitDisplay;
2171 (*graphics)->scale = 1.0;
2172 (*graphics)->busy = FALSE;
2173 (*graphics)->textcontrast = 4;
2174 list_init(&(*graphics)->containers);
2175 (*graphics)->contid = 0;
2177 TRACE("<-- %p\n", *graphics);
2179 return Ok;
2182 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2184 GpStatus ret;
2185 HDC hdc;
2187 TRACE("(%p, %p)\n", hwnd, graphics);
2189 hdc = GetDC(hwnd);
2191 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2193 ReleaseDC(hwnd, hdc);
2194 return ret;
2197 (*graphics)->hwnd = hwnd;
2198 (*graphics)->owndc = TRUE;
2200 return Ok;
2203 /* FIXME: no icm handling */
2204 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2206 TRACE("(%p, %p)\n", hwnd, graphics);
2208 return GdipCreateFromHWND(hwnd, graphics);
2211 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2212 GpMetafile **metafile)
2214 IStream *stream = NULL;
2215 UINT read;
2216 ENHMETAHEADER *copy;
2217 GpStatus retval = Ok;
2219 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2221 if(!hemf || !metafile)
2222 return InvalidParameter;
2224 read = GetEnhMetaFileBits(hemf, 0, NULL);
2225 copy = GdipAlloc(read);
2226 GetEnhMetaFileBits(hemf, read, (BYTE *)copy);
2228 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
2229 ERR("could not make stream\n");
2230 GdipFree(copy);
2231 retval = GenericError;
2232 goto err;
2235 *metafile = GdipAlloc(sizeof(GpMetafile));
2236 if(!*metafile){
2237 retval = OutOfMemory;
2238 goto err;
2241 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2242 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
2244 retval = GenericError;
2245 goto err;
2249 (*metafile)->image.type = ImageTypeMetafile;
2250 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
2251 (*metafile)->image.palette_flags = 0;
2252 (*metafile)->image.palette_count = 0;
2253 (*metafile)->image.palette_size = 0;
2254 (*metafile)->image.palette_entries = NULL;
2255 (*metafile)->image.xres = (REAL)copy->szlDevice.cx;
2256 (*metafile)->image.yres = (REAL)copy->szlDevice.cy;
2257 (*metafile)->bounds.X = (REAL)copy->rclBounds.left;
2258 (*metafile)->bounds.Y = (REAL)copy->rclBounds.top;
2259 (*metafile)->bounds.Width = (REAL)(copy->rclBounds.right - copy->rclBounds.left);
2260 (*metafile)->bounds.Height = (REAL)(copy->rclBounds.bottom - copy->rclBounds.top);
2261 (*metafile)->unit = UnitPixel;
2263 if(delete)
2264 DeleteEnhMetaFile(hemf);
2266 TRACE("<-- %p\n", *metafile);
2268 err:
2269 if (retval != Ok)
2270 GdipFree(*metafile);
2271 IStream_Release(stream);
2272 return retval;
2275 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2276 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2278 UINT read;
2279 BYTE *copy;
2280 HENHMETAFILE hemf;
2281 GpStatus retval = Ok;
2283 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2285 if(!hwmf || !metafile || !placeable)
2286 return InvalidParameter;
2288 *metafile = NULL;
2289 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2290 if(!read)
2291 return GenericError;
2292 copy = GdipAlloc(read);
2293 GetMetaFileBitsEx(hwmf, read, copy);
2295 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2296 GdipFree(copy);
2298 retval = GdipCreateMetafileFromEmf(hemf, FALSE, metafile);
2300 if (retval == Ok)
2302 (*metafile)->image.xres = (REAL)placeable->Inch;
2303 (*metafile)->image.yres = (REAL)placeable->Inch;
2304 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2305 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2306 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2307 placeable->BoundingBox.Left);
2308 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2309 placeable->BoundingBox.Top);
2311 if (delete) DeleteMetaFile(hwmf);
2313 return retval;
2316 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2317 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2319 HMETAFILE hmf = GetMetaFileW(file);
2321 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2323 if(!hmf) return InvalidParameter;
2325 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2328 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2329 GpMetafile **metafile)
2331 FIXME("(%p, %p): stub\n", file, metafile);
2332 return NotImplemented;
2335 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2336 GpMetafile **metafile)
2338 FIXME("(%p, %p): stub\n", stream, metafile);
2339 return NotImplemented;
2342 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2343 UINT access, IStream **stream)
2345 DWORD dwMode;
2346 HRESULT ret;
2348 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2350 if(!stream || !filename)
2351 return InvalidParameter;
2353 if(access & GENERIC_WRITE)
2354 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2355 else if(access & GENERIC_READ)
2356 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2357 else
2358 return InvalidParameter;
2360 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2362 return hresult_to_status(ret);
2365 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2367 GraphicsContainerItem *cont, *next;
2368 GpStatus stat;
2369 TRACE("(%p)\n", graphics);
2371 if(!graphics) return InvalidParameter;
2372 if(graphics->busy) return ObjectBusy;
2374 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2376 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2377 if (stat != Ok)
2378 return stat;
2381 if(graphics->owndc)
2382 ReleaseDC(graphics->hwnd, graphics->hdc);
2384 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2385 list_remove(&cont->entry);
2386 delete_container(cont);
2389 GdipDeleteRegion(graphics->clip);
2390 GdipDeleteMatrix(graphics->worldtrans);
2391 GdipFree(graphics);
2393 return Ok;
2396 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2397 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2399 INT save_state, num_pts;
2400 GpPointF points[MAX_ARC_PTS];
2401 GpStatus retval;
2403 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2404 width, height, startAngle, sweepAngle);
2406 if(!graphics || !pen || width <= 0 || height <= 0)
2407 return InvalidParameter;
2409 if(graphics->busy)
2410 return ObjectBusy;
2412 if (!graphics->hdc)
2414 FIXME("graphics object has no HDC\n");
2415 return Ok;
2418 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2420 save_state = prepare_dc(graphics, pen);
2422 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2424 restore_dc(graphics, save_state);
2426 return retval;
2429 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2430 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2432 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2433 width, height, startAngle, sweepAngle);
2435 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2438 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2439 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2441 INT save_state;
2442 GpPointF pt[4];
2443 GpStatus retval;
2445 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2446 x2, y2, x3, y3, x4, y4);
2448 if(!graphics || !pen)
2449 return InvalidParameter;
2451 if(graphics->busy)
2452 return ObjectBusy;
2454 if (!graphics->hdc)
2456 FIXME("graphics object has no HDC\n");
2457 return Ok;
2460 pt[0].X = x1;
2461 pt[0].Y = y1;
2462 pt[1].X = x2;
2463 pt[1].Y = y2;
2464 pt[2].X = x3;
2465 pt[2].Y = y3;
2466 pt[3].X = x4;
2467 pt[3].Y = y4;
2469 save_state = prepare_dc(graphics, pen);
2471 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2473 restore_dc(graphics, save_state);
2475 return retval;
2478 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2479 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2481 INT save_state;
2482 GpPointF pt[4];
2483 GpStatus retval;
2485 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2486 x2, y2, x3, y3, x4, y4);
2488 if(!graphics || !pen)
2489 return InvalidParameter;
2491 if(graphics->busy)
2492 return ObjectBusy;
2494 if (!graphics->hdc)
2496 FIXME("graphics object has no HDC\n");
2497 return Ok;
2500 pt[0].X = x1;
2501 pt[0].Y = y1;
2502 pt[1].X = x2;
2503 pt[1].Y = y2;
2504 pt[2].X = x3;
2505 pt[2].Y = y3;
2506 pt[3].X = x4;
2507 pt[3].Y = y4;
2509 save_state = prepare_dc(graphics, pen);
2511 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2513 restore_dc(graphics, save_state);
2515 return retval;
2518 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2519 GDIPCONST GpPointF *points, INT count)
2521 INT i;
2522 GpStatus ret;
2524 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2526 if(!graphics || !pen || !points || (count <= 0))
2527 return InvalidParameter;
2529 if(graphics->busy)
2530 return ObjectBusy;
2532 for(i = 0; i < floor(count / 4); i++){
2533 ret = GdipDrawBezier(graphics, pen,
2534 points[4*i].X, points[4*i].Y,
2535 points[4*i + 1].X, points[4*i + 1].Y,
2536 points[4*i + 2].X, points[4*i + 2].Y,
2537 points[4*i + 3].X, points[4*i + 3].Y);
2538 if(ret != Ok)
2539 return ret;
2542 return Ok;
2545 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2546 GDIPCONST GpPoint *points, INT count)
2548 GpPointF *pts;
2549 GpStatus ret;
2550 INT i;
2552 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2554 if(!graphics || !pen || !points || (count <= 0))
2555 return InvalidParameter;
2557 if(graphics->busy)
2558 return ObjectBusy;
2560 pts = GdipAlloc(sizeof(GpPointF) * count);
2561 if(!pts)
2562 return OutOfMemory;
2564 for(i = 0; i < count; i++){
2565 pts[i].X = (REAL)points[i].X;
2566 pts[i].Y = (REAL)points[i].Y;
2569 ret = GdipDrawBeziers(graphics,pen,pts,count);
2571 GdipFree(pts);
2573 return ret;
2576 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2577 GDIPCONST GpPointF *points, INT count)
2579 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2581 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2584 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2585 GDIPCONST GpPoint *points, INT count)
2587 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2589 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2592 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2593 GDIPCONST GpPointF *points, INT count, REAL tension)
2595 GpPath *path;
2596 GpStatus stat;
2598 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2600 if(!graphics || !pen || !points || count <= 0)
2601 return InvalidParameter;
2603 if(graphics->busy)
2604 return ObjectBusy;
2606 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2607 return stat;
2609 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2610 if(stat != Ok){
2611 GdipDeletePath(path);
2612 return stat;
2615 stat = GdipDrawPath(graphics, pen, path);
2617 GdipDeletePath(path);
2619 return stat;
2622 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2623 GDIPCONST GpPoint *points, INT count, REAL tension)
2625 GpPointF *ptf;
2626 GpStatus stat;
2627 INT i;
2629 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2631 if(!points || count <= 0)
2632 return InvalidParameter;
2634 ptf = GdipAlloc(sizeof(GpPointF)*count);
2635 if(!ptf)
2636 return OutOfMemory;
2638 for(i = 0; i < count; i++){
2639 ptf[i].X = (REAL)points[i].X;
2640 ptf[i].Y = (REAL)points[i].Y;
2643 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2645 GdipFree(ptf);
2647 return stat;
2650 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2651 GDIPCONST GpPointF *points, INT count)
2653 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2655 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2658 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2659 GDIPCONST GpPoint *points, INT count)
2661 GpPointF *pointsF;
2662 GpStatus ret;
2663 INT i;
2665 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2667 if(!points)
2668 return InvalidParameter;
2670 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2671 if(!pointsF)
2672 return OutOfMemory;
2674 for(i = 0; i < count; i++){
2675 pointsF[i].X = (REAL)points[i].X;
2676 pointsF[i].Y = (REAL)points[i].Y;
2679 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2680 GdipFree(pointsF);
2682 return ret;
2685 /* Approximates cardinal spline with Bezier curves. */
2686 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2687 GDIPCONST GpPointF *points, INT count, REAL tension)
2689 /* PolyBezier expects count*3-2 points. */
2690 INT i, len_pt = count*3-2, save_state;
2691 GpPointF *pt;
2692 REAL x1, x2, y1, y2;
2693 GpStatus retval;
2695 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2697 if(!graphics || !pen)
2698 return InvalidParameter;
2700 if(graphics->busy)
2701 return ObjectBusy;
2703 if(count < 2)
2704 return InvalidParameter;
2706 if (!graphics->hdc)
2708 FIXME("graphics object has no HDC\n");
2709 return Ok;
2712 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2713 if(!pt)
2714 return OutOfMemory;
2716 tension = tension * TENSION_CONST;
2718 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2719 tension, &x1, &y1);
2721 pt[0].X = points[0].X;
2722 pt[0].Y = points[0].Y;
2723 pt[1].X = x1;
2724 pt[1].Y = y1;
2726 for(i = 0; i < count-2; i++){
2727 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2729 pt[3*i+2].X = x1;
2730 pt[3*i+2].Y = y1;
2731 pt[3*i+3].X = points[i+1].X;
2732 pt[3*i+3].Y = points[i+1].Y;
2733 pt[3*i+4].X = x2;
2734 pt[3*i+4].Y = y2;
2737 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2738 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2740 pt[len_pt-2].X = x1;
2741 pt[len_pt-2].Y = y1;
2742 pt[len_pt-1].X = points[count-1].X;
2743 pt[len_pt-1].Y = points[count-1].Y;
2745 save_state = prepare_dc(graphics, pen);
2747 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2749 GdipFree(pt);
2750 restore_dc(graphics, save_state);
2752 return retval;
2755 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2756 GDIPCONST GpPoint *points, INT count, REAL tension)
2758 GpPointF *pointsF;
2759 GpStatus ret;
2760 INT i;
2762 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2764 if(!points)
2765 return InvalidParameter;
2767 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2768 if(!pointsF)
2769 return OutOfMemory;
2771 for(i = 0; i < count; i++){
2772 pointsF[i].X = (REAL)points[i].X;
2773 pointsF[i].Y = (REAL)points[i].Y;
2776 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2777 GdipFree(pointsF);
2779 return ret;
2782 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2783 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2784 REAL tension)
2786 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2788 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2789 return InvalidParameter;
2792 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2795 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2796 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2797 REAL tension)
2799 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2801 if(count < 0){
2802 return OutOfMemory;
2805 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2806 return InvalidParameter;
2809 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2812 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2813 REAL y, REAL width, REAL height)
2815 INT save_state;
2816 GpPointF ptf[2];
2817 POINT pti[2];
2819 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2821 if(!graphics || !pen)
2822 return InvalidParameter;
2824 if(graphics->busy)
2825 return ObjectBusy;
2827 if (!graphics->hdc)
2829 FIXME("graphics object has no HDC\n");
2830 return Ok;
2833 ptf[0].X = x;
2834 ptf[0].Y = y;
2835 ptf[1].X = x + width;
2836 ptf[1].Y = y + height;
2838 save_state = prepare_dc(graphics, pen);
2839 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2841 transform_and_round_points(graphics, pti, ptf, 2);
2843 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2845 restore_dc(graphics, save_state);
2847 return Ok;
2850 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2851 INT y, INT width, INT height)
2853 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2855 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2859 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2861 UINT width, height;
2862 GpPointF points[3];
2864 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2866 if(!graphics || !image)
2867 return InvalidParameter;
2869 GdipGetImageWidth(image, &width);
2870 GdipGetImageHeight(image, &height);
2872 /* FIXME: we should use the graphics and image dpi, somehow */
2874 points[0].X = points[2].X = x;
2875 points[0].Y = points[1].Y = y;
2876 points[1].X = x + width;
2877 points[2].Y = y + height;
2879 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
2880 UnitPixel, NULL, NULL, NULL);
2883 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2884 INT y)
2886 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2888 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2891 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2892 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2893 GpUnit srcUnit)
2895 GpPointF points[3];
2896 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2898 points[0].X = points[2].X = x;
2899 points[0].Y = points[1].Y = y;
2901 /* FIXME: convert image coordinates to Graphics coordinates? */
2902 points[1].X = x + srcwidth;
2903 points[2].Y = y + srcheight;
2905 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2906 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2909 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2910 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2911 GpUnit srcUnit)
2913 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2916 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2917 GDIPCONST GpPointF *dstpoints, INT count)
2919 UINT width, height;
2921 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2923 if(!image)
2924 return InvalidParameter;
2926 GdipGetImageWidth(image, &width);
2927 GdipGetImageHeight(image, &height);
2929 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2930 width, height, UnitPixel, NULL, NULL, NULL);
2933 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2934 GDIPCONST GpPoint *dstpoints, INT count)
2936 GpPointF ptf[3];
2938 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2940 if (count != 3 || !dstpoints)
2941 return InvalidParameter;
2943 ptf[0].X = (REAL)dstpoints[0].X;
2944 ptf[0].Y = (REAL)dstpoints[0].Y;
2945 ptf[1].X = (REAL)dstpoints[1].X;
2946 ptf[1].Y = (REAL)dstpoints[1].Y;
2947 ptf[2].X = (REAL)dstpoints[2].X;
2948 ptf[2].Y = (REAL)dstpoints[2].Y;
2950 return GdipDrawImagePoints(graphics, image, ptf, count);
2953 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2954 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2955 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2956 DrawImageAbort callback, VOID * callbackData)
2958 GpPointF ptf[4];
2959 POINT pti[4];
2960 REAL dx, dy;
2961 GpStatus stat;
2963 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2964 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2965 callbackData);
2967 if (count > 3)
2968 return NotImplemented;
2970 if(!graphics || !image || !points || count != 3)
2971 return InvalidParameter;
2973 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2974 debugstr_pointf(&points[2]));
2976 memcpy(ptf, points, 3 * sizeof(GpPointF));
2977 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2978 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2979 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
2980 return Ok;
2981 transform_and_round_points(graphics, pti, ptf, 4);
2983 if (image->picture)
2985 if (!graphics->hdc)
2987 FIXME("graphics object has no HDC\n");
2990 /* FIXME: partially implemented (only works for rectangular parallelograms) */
2991 if(srcUnit == UnitInch)
2992 dx = dy = (REAL) INCH_HIMETRIC;
2993 else if(srcUnit == UnitPixel){
2994 dx = ((REAL) INCH_HIMETRIC) /
2995 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
2996 dy = ((REAL) INCH_HIMETRIC) /
2997 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
2999 else
3000 return NotImplemented;
3002 if(IPicture_Render(image->picture, graphics->hdc,
3003 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3004 srcx * dx, srcy * dy,
3005 srcwidth * dx, srcheight * dy,
3006 NULL) != S_OK){
3007 if(callback)
3008 callback(callbackData);
3009 return GenericError;
3012 else if (image->type == ImageTypeBitmap)
3014 GpBitmap* bitmap = (GpBitmap*)image;
3015 int use_software=0;
3017 if (srcUnit == UnitInch)
3018 dx = dy = 96.0; /* FIXME: use the image resolution */
3019 else if (srcUnit == UnitPixel)
3020 dx = dy = 1.0;
3021 else
3022 return NotImplemented;
3024 srcx = srcx * dx;
3025 srcy = srcy * dy;
3026 srcwidth = srcwidth * dx;
3027 srcheight = srcheight * dy;
3029 if (imageAttributes ||
3030 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3031 !((GpBitmap*)image)->hbitmap ||
3032 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3033 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3034 srcx < 0 || srcy < 0 ||
3035 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3036 use_software = 1;
3038 if (use_software)
3040 RECT dst_area;
3041 GpRect src_area;
3042 int i, x, y, src_stride, dst_stride;
3043 GpMatrix *dst_to_src;
3044 REAL m11, m12, m21, m22, mdx, mdy;
3045 LPBYTE src_data, dst_data;
3046 BitmapData lockeddata;
3047 InterpolationMode interpolation = graphics->interpolation;
3048 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3049 REAL x_dx, x_dy, y_dx, y_dy;
3050 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3052 if (!imageAttributes)
3053 imageAttributes = &defaultImageAttributes;
3055 dst_area.left = dst_area.right = pti[0].x;
3056 dst_area.top = dst_area.bottom = pti[0].y;
3057 for (i=1; i<4; i++)
3059 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3060 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3061 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3062 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3065 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3066 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3067 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3068 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3069 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3070 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3072 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
3073 if (stat != Ok) return stat;
3075 stat = GdipInvertMatrix(dst_to_src);
3076 if (stat != Ok)
3078 GdipDeleteMatrix(dst_to_src);
3079 return stat;
3082 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3083 if (!dst_data)
3085 GdipDeleteMatrix(dst_to_src);
3086 return OutOfMemory;
3089 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3091 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3092 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3094 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3095 if (!src_data)
3097 GdipFree(dst_data);
3098 GdipDeleteMatrix(dst_to_src);
3099 return OutOfMemory;
3101 src_stride = sizeof(ARGB) * src_area.Width;
3103 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3104 lockeddata.Width = src_area.Width;
3105 lockeddata.Height = src_area.Height;
3106 lockeddata.Stride = src_stride;
3107 lockeddata.PixelFormat = PixelFormat32bppARGB;
3108 lockeddata.Scan0 = src_data;
3110 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3111 PixelFormat32bppARGB, &lockeddata);
3113 if (stat == Ok)
3114 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3116 if (stat != Ok)
3118 if (src_data != dst_data)
3119 GdipFree(src_data);
3120 GdipFree(dst_data);
3121 GdipDeleteMatrix(dst_to_src);
3122 return OutOfMemory;
3125 apply_image_attributes(imageAttributes, src_data,
3126 src_area.Width, src_area.Height,
3127 src_stride, ColorAdjustTypeBitmap);
3129 /* Transform the bits as needed to the destination. */
3130 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
3132 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3133 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3134 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3135 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3137 for (x=dst_area.left; x<dst_area.right; x++)
3139 for (y=dst_area.top; y<dst_area.bottom; y++)
3141 GpPointF src_pointf;
3142 ARGB *dst_color;
3144 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3145 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3147 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3149 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3150 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
3151 else
3152 *dst_color = 0;
3156 GdipDeleteMatrix(dst_to_src);
3158 GdipFree(src_data);
3160 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3161 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3163 GdipFree(dst_data);
3165 return stat;
3167 else
3169 HDC hdc;
3170 int temp_hdc=0, temp_bitmap=0;
3171 HBITMAP hbitmap, old_hbm=NULL;
3173 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3174 bitmap->format == PixelFormat24bppRGB ||
3175 bitmap->format == PixelFormat32bppRGB ||
3176 bitmap->format == PixelFormat32bppPARGB))
3178 BITMAPINFOHEADER bih;
3179 BYTE *temp_bits;
3180 PixelFormat dst_format;
3182 /* we can't draw a bitmap of this format directly */
3183 hdc = CreateCompatibleDC(0);
3184 temp_hdc = 1;
3185 temp_bitmap = 1;
3187 bih.biSize = sizeof(BITMAPINFOHEADER);
3188 bih.biWidth = bitmap->width;
3189 bih.biHeight = -bitmap->height;
3190 bih.biPlanes = 1;
3191 bih.biBitCount = 32;
3192 bih.biCompression = BI_RGB;
3193 bih.biSizeImage = 0;
3194 bih.biXPelsPerMeter = 0;
3195 bih.biYPelsPerMeter = 0;
3196 bih.biClrUsed = 0;
3197 bih.biClrImportant = 0;
3199 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3200 (void**)&temp_bits, NULL, 0);
3202 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3203 dst_format = PixelFormat32bppPARGB;
3204 else
3205 dst_format = PixelFormat32bppRGB;
3207 convert_pixels(bitmap->width, bitmap->height,
3208 bitmap->width*4, temp_bits, dst_format,
3209 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
3211 else
3213 hbitmap = bitmap->hbitmap;
3214 hdc = bitmap->hdc;
3215 temp_hdc = (hdc == 0);
3218 if (temp_hdc)
3220 if (!hdc) hdc = CreateCompatibleDC(0);
3221 old_hbm = SelectObject(hdc, hbitmap);
3224 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3226 BLENDFUNCTION bf;
3228 bf.BlendOp = AC_SRC_OVER;
3229 bf.BlendFlags = 0;
3230 bf.SourceConstantAlpha = 255;
3231 bf.AlphaFormat = AC_SRC_ALPHA;
3233 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3234 hdc, srcx, srcy, srcwidth, srcheight, bf);
3236 else
3238 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3239 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3242 if (temp_hdc)
3244 SelectObject(hdc, old_hbm);
3245 DeleteDC(hdc);
3248 if (temp_bitmap)
3249 DeleteObject(hbitmap);
3252 else
3254 ERR("GpImage with no IPicture or HBITMAP?!\n");
3255 return NotImplemented;
3258 return Ok;
3261 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3262 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3263 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3264 DrawImageAbort callback, VOID * callbackData)
3266 GpPointF pointsF[3];
3267 INT i;
3269 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3270 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3271 callbackData);
3273 if(!points || count!=3)
3274 return InvalidParameter;
3276 for(i = 0; i < count; i++){
3277 pointsF[i].X = (REAL)points[i].X;
3278 pointsF[i].Y = (REAL)points[i].Y;
3281 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3282 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3283 callback, callbackData);
3286 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3287 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3288 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3289 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3290 VOID * callbackData)
3292 GpPointF points[3];
3294 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3295 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3296 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3298 points[0].X = dstx;
3299 points[0].Y = dsty;
3300 points[1].X = dstx + dstwidth;
3301 points[1].Y = dsty;
3302 points[2].X = dstx;
3303 points[2].Y = dsty + dstheight;
3305 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3306 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3309 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3310 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3311 INT srcwidth, INT srcheight, GpUnit srcUnit,
3312 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3313 VOID * callbackData)
3315 GpPointF points[3];
3317 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3318 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3319 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3321 points[0].X = dstx;
3322 points[0].Y = dsty;
3323 points[1].X = dstx + dstwidth;
3324 points[1].Y = dsty;
3325 points[2].X = dstx;
3326 points[2].Y = dsty + dstheight;
3328 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3329 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3332 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3333 REAL x, REAL y, REAL width, REAL height)
3335 RectF bounds;
3336 GpUnit unit;
3337 GpStatus ret;
3339 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3341 if(!graphics || !image)
3342 return InvalidParameter;
3344 ret = GdipGetImageBounds(image, &bounds, &unit);
3345 if(ret != Ok)
3346 return ret;
3348 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3349 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3350 unit, NULL, NULL, NULL);
3353 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3354 INT x, INT y, INT width, INT height)
3356 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3358 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3361 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3362 REAL y1, REAL x2, REAL y2)
3364 INT save_state;
3365 GpPointF pt[2];
3366 GpStatus retval;
3368 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3370 if(!pen || !graphics)
3371 return InvalidParameter;
3373 if(graphics->busy)
3374 return ObjectBusy;
3376 if (!graphics->hdc)
3378 FIXME("graphics object has no HDC\n");
3379 return Ok;
3382 pt[0].X = x1;
3383 pt[0].Y = y1;
3384 pt[1].X = x2;
3385 pt[1].Y = y2;
3387 save_state = prepare_dc(graphics, pen);
3389 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3391 restore_dc(graphics, save_state);
3393 return retval;
3396 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3397 INT y1, INT x2, INT y2)
3399 INT save_state;
3400 GpPointF pt[2];
3401 GpStatus retval;
3403 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3405 if(!pen || !graphics)
3406 return InvalidParameter;
3408 if(graphics->busy)
3409 return ObjectBusy;
3411 if (!graphics->hdc)
3413 FIXME("graphics object has no HDC\n");
3414 return Ok;
3417 pt[0].X = (REAL)x1;
3418 pt[0].Y = (REAL)y1;
3419 pt[1].X = (REAL)x2;
3420 pt[1].Y = (REAL)y2;
3422 save_state = prepare_dc(graphics, pen);
3424 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3426 restore_dc(graphics, save_state);
3428 return retval;
3431 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3432 GpPointF *points, INT count)
3434 INT save_state;
3435 GpStatus retval;
3437 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3439 if(!pen || !graphics || (count < 2))
3440 return InvalidParameter;
3442 if(graphics->busy)
3443 return ObjectBusy;
3445 if (!graphics->hdc)
3447 FIXME("graphics object has no HDC\n");
3448 return Ok;
3451 save_state = prepare_dc(graphics, pen);
3453 retval = draw_polyline(graphics, pen, points, count, TRUE);
3455 restore_dc(graphics, save_state);
3457 return retval;
3460 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3461 GpPoint *points, INT count)
3463 INT save_state;
3464 GpStatus retval;
3465 GpPointF *ptf = NULL;
3466 int i;
3468 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3470 if(!pen || !graphics || (count < 2))
3471 return InvalidParameter;
3473 if(graphics->busy)
3474 return ObjectBusy;
3476 if (!graphics->hdc)
3478 FIXME("graphics object has no HDC\n");
3479 return Ok;
3482 ptf = GdipAlloc(count * sizeof(GpPointF));
3483 if(!ptf) return OutOfMemory;
3485 for(i = 0; i < count; i ++){
3486 ptf[i].X = (REAL) points[i].X;
3487 ptf[i].Y = (REAL) points[i].Y;
3490 save_state = prepare_dc(graphics, pen);
3492 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3494 restore_dc(graphics, save_state);
3496 GdipFree(ptf);
3497 return retval;
3500 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3502 INT save_state;
3503 GpStatus retval;
3505 TRACE("(%p, %p, %p)\n", graphics, pen, path);
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 save_state = prepare_dc(graphics, pen);
3521 retval = draw_poly(graphics, pen, path->pathdata.Points,
3522 path->pathdata.Types, path->pathdata.Count, TRUE);
3524 restore_dc(graphics, save_state);
3526 return retval;
3529 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3530 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3532 INT save_state;
3534 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3535 width, height, startAngle, sweepAngle);
3537 if(!graphics || !pen)
3538 return InvalidParameter;
3540 if(graphics->busy)
3541 return ObjectBusy;
3543 if (!graphics->hdc)
3545 FIXME("graphics object has no HDC\n");
3546 return Ok;
3549 save_state = prepare_dc(graphics, pen);
3550 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3552 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3554 restore_dc(graphics, save_state);
3556 return Ok;
3559 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3560 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3562 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3563 width, height, startAngle, sweepAngle);
3565 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3568 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3569 REAL y, REAL width, REAL height)
3571 INT save_state;
3572 GpPointF ptf[4];
3573 POINT pti[4];
3575 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3577 if(!pen || !graphics)
3578 return InvalidParameter;
3580 if(graphics->busy)
3581 return ObjectBusy;
3583 if (!graphics->hdc)
3585 FIXME("graphics object has no HDC\n");
3586 return Ok;
3589 ptf[0].X = x;
3590 ptf[0].Y = y;
3591 ptf[1].X = x + width;
3592 ptf[1].Y = y;
3593 ptf[2].X = x + width;
3594 ptf[2].Y = y + height;
3595 ptf[3].X = x;
3596 ptf[3].Y = y + height;
3598 save_state = prepare_dc(graphics, pen);
3599 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3601 transform_and_round_points(graphics, pti, ptf, 4);
3602 Polygon(graphics->hdc, pti, 4);
3604 restore_dc(graphics, save_state);
3606 return Ok;
3609 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3610 INT y, INT width, INT height)
3612 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3614 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3617 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3618 GDIPCONST GpRectF* rects, INT count)
3620 GpPointF *ptf;
3621 POINT *pti;
3622 INT save_state, i;
3624 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3626 if(!graphics || !pen || !rects || count < 1)
3627 return InvalidParameter;
3629 if(graphics->busy)
3630 return ObjectBusy;
3632 if (!graphics->hdc)
3634 FIXME("graphics object has no HDC\n");
3635 return Ok;
3638 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3639 pti = GdipAlloc(4 * count * sizeof(POINT));
3641 if(!ptf || !pti){
3642 GdipFree(ptf);
3643 GdipFree(pti);
3644 return OutOfMemory;
3647 for(i = 0; i < count; i++){
3648 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3649 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3650 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3651 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3654 save_state = prepare_dc(graphics, pen);
3655 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3657 transform_and_round_points(graphics, pti, ptf, 4 * count);
3659 for(i = 0; i < count; i++)
3660 Polygon(graphics->hdc, &pti[4 * i], 4);
3662 restore_dc(graphics, save_state);
3664 GdipFree(ptf);
3665 GdipFree(pti);
3667 return Ok;
3670 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3671 GDIPCONST GpRect* rects, INT count)
3673 GpRectF *rectsF;
3674 GpStatus ret;
3675 INT i;
3677 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3679 if(!rects || count<=0)
3680 return InvalidParameter;
3682 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3683 if(!rectsF)
3684 return OutOfMemory;
3686 for(i = 0;i < count;i++){
3687 rectsF[i].X = (REAL)rects[i].X;
3688 rectsF[i].Y = (REAL)rects[i].Y;
3689 rectsF[i].Width = (REAL)rects[i].Width;
3690 rectsF[i].Height = (REAL)rects[i].Height;
3693 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3694 GdipFree(rectsF);
3696 return ret;
3699 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3700 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3702 GpPath *path;
3703 GpStatus stat;
3705 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3706 count, tension, fill);
3708 if(!graphics || !brush || !points)
3709 return InvalidParameter;
3711 if(graphics->busy)
3712 return ObjectBusy;
3714 if(count == 1) /* Do nothing */
3715 return Ok;
3717 stat = GdipCreatePath(fill, &path);
3718 if(stat != Ok)
3719 return stat;
3721 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3722 if(stat != Ok){
3723 GdipDeletePath(path);
3724 return stat;
3727 stat = GdipFillPath(graphics, brush, path);
3728 if(stat != Ok){
3729 GdipDeletePath(path);
3730 return stat;
3733 GdipDeletePath(path);
3735 return Ok;
3738 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3739 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3741 GpPointF *ptf;
3742 GpStatus stat;
3743 INT i;
3745 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3746 count, tension, fill);
3748 if(!points || count == 0)
3749 return InvalidParameter;
3751 if(count == 1) /* Do nothing */
3752 return Ok;
3754 ptf = GdipAlloc(sizeof(GpPointF)*count);
3755 if(!ptf)
3756 return OutOfMemory;
3758 for(i = 0;i < count;i++){
3759 ptf[i].X = (REAL)points[i].X;
3760 ptf[i].Y = (REAL)points[i].Y;
3763 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3765 GdipFree(ptf);
3767 return stat;
3770 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3771 GDIPCONST GpPointF *points, INT count)
3773 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3774 return GdipFillClosedCurve2(graphics, brush, points, count,
3775 0.5f, FillModeAlternate);
3778 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3779 GDIPCONST GpPoint *points, INT count)
3781 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3782 return GdipFillClosedCurve2I(graphics, brush, points, count,
3783 0.5f, FillModeAlternate);
3786 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3787 REAL y, REAL width, REAL height)
3789 GpStatus stat;
3790 GpPath *path;
3792 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3794 if(!graphics || !brush)
3795 return InvalidParameter;
3797 if(graphics->busy)
3798 return ObjectBusy;
3800 stat = GdipCreatePath(FillModeAlternate, &path);
3802 if (stat == Ok)
3804 stat = GdipAddPathEllipse(path, x, y, width, height);
3806 if (stat == Ok)
3807 stat = GdipFillPath(graphics, brush, path);
3809 GdipDeletePath(path);
3812 return stat;
3815 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3816 INT y, INT width, INT height)
3818 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3820 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3823 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3825 INT save_state;
3826 GpStatus retval;
3828 if(!graphics->hdc || !brush_can_fill_path(brush))
3829 return NotImplemented;
3831 save_state = SaveDC(graphics->hdc);
3832 EndPath(graphics->hdc);
3833 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3834 : WINDING));
3836 BeginPath(graphics->hdc);
3837 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3838 path->pathdata.Types, path->pathdata.Count, FALSE);
3840 if(retval != Ok)
3841 goto end;
3843 EndPath(graphics->hdc);
3844 brush_fill_path(graphics, brush);
3846 retval = Ok;
3848 end:
3849 RestoreDC(graphics->hdc, save_state);
3851 return retval;
3854 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3856 GpStatus stat;
3857 GpRegion *rgn;
3859 if (!brush_can_fill_pixels(brush))
3860 return NotImplemented;
3862 /* FIXME: This could probably be done more efficiently without regions. */
3864 stat = GdipCreateRegionPath(path, &rgn);
3866 if (stat == Ok)
3868 stat = GdipFillRegion(graphics, brush, rgn);
3870 GdipDeleteRegion(rgn);
3873 return stat;
3876 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3878 GpStatus stat = NotImplemented;
3880 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3882 if(!brush || !graphics || !path)
3883 return InvalidParameter;
3885 if(graphics->busy)
3886 return ObjectBusy;
3888 if (!graphics->image)
3889 stat = GDI32_GdipFillPath(graphics, brush, path);
3891 if (stat == NotImplemented)
3892 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3894 if (stat == NotImplemented)
3896 FIXME("Not implemented for brushtype %i\n", brush->bt);
3897 stat = Ok;
3900 return stat;
3903 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3904 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3906 GpStatus stat;
3907 GpPath *path;
3909 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3910 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3912 if(!graphics || !brush)
3913 return InvalidParameter;
3915 if(graphics->busy)
3916 return ObjectBusy;
3918 stat = GdipCreatePath(FillModeAlternate, &path);
3920 if (stat == Ok)
3922 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3924 if (stat == Ok)
3925 stat = GdipFillPath(graphics, brush, path);
3927 GdipDeletePath(path);
3930 return stat;
3933 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3934 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3936 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3937 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3939 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3942 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3943 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3945 GpStatus stat;
3946 GpPath *path;
3948 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3950 if(!graphics || !brush || !points || !count)
3951 return InvalidParameter;
3953 if(graphics->busy)
3954 return ObjectBusy;
3956 stat = GdipCreatePath(fillMode, &path);
3958 if (stat == Ok)
3960 stat = GdipAddPathPolygon(path, points, count);
3962 if (stat == Ok)
3963 stat = GdipFillPath(graphics, brush, path);
3965 GdipDeletePath(path);
3968 return stat;
3971 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3972 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3974 GpStatus stat;
3975 GpPath *path;
3977 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3979 if(!graphics || !brush || !points || !count)
3980 return InvalidParameter;
3982 if(graphics->busy)
3983 return ObjectBusy;
3985 stat = GdipCreatePath(fillMode, &path);
3987 if (stat == Ok)
3989 stat = GdipAddPathPolygonI(path, points, count);
3991 if (stat == Ok)
3992 stat = GdipFillPath(graphics, brush, path);
3994 GdipDeletePath(path);
3997 return stat;
4000 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4001 GDIPCONST GpPointF *points, INT count)
4003 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4005 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4008 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4009 GDIPCONST GpPoint *points, INT count)
4011 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4013 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4016 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4017 REAL x, REAL y, REAL width, REAL height)
4019 GpStatus stat;
4020 GpPath *path;
4022 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4024 if(!graphics || !brush)
4025 return InvalidParameter;
4027 if(graphics->busy)
4028 return ObjectBusy;
4030 stat = GdipCreatePath(FillModeAlternate, &path);
4032 if (stat == Ok)
4034 stat = GdipAddPathRectangle(path, x, y, width, height);
4036 if (stat == Ok)
4037 stat = GdipFillPath(graphics, brush, path);
4039 GdipDeletePath(path);
4042 return stat;
4045 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4046 INT x, INT y, INT width, INT height)
4048 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4050 return GdipFillRectangle(graphics, brush, x, y, width, height);
4053 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4054 INT count)
4056 GpStatus ret;
4057 INT i;
4059 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4061 if(!rects)
4062 return InvalidParameter;
4064 for(i = 0; i < count; i++){
4065 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4066 if(ret != Ok) return ret;
4069 return Ok;
4072 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4073 INT count)
4075 GpRectF *rectsF;
4076 GpStatus ret;
4077 INT i;
4079 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4081 if(!rects || count <= 0)
4082 return InvalidParameter;
4084 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4085 if(!rectsF)
4086 return OutOfMemory;
4088 for(i = 0; i < count; i++){
4089 rectsF[i].X = (REAL)rects[i].X;
4090 rectsF[i].Y = (REAL)rects[i].Y;
4091 rectsF[i].X = (REAL)rects[i].Width;
4092 rectsF[i].Height = (REAL)rects[i].Height;
4095 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4096 GdipFree(rectsF);
4098 return ret;
4101 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4102 GpRegion* region)
4104 INT save_state;
4105 GpStatus status;
4106 HRGN hrgn;
4107 RECT rc;
4109 if(!graphics->hdc || !brush_can_fill_path(brush))
4110 return NotImplemented;
4112 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4113 if(status != Ok)
4114 return status;
4116 save_state = SaveDC(graphics->hdc);
4117 EndPath(graphics->hdc);
4119 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4121 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4123 BeginPath(graphics->hdc);
4124 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4125 EndPath(graphics->hdc);
4127 brush_fill_path(graphics, brush);
4130 RestoreDC(graphics->hdc, save_state);
4132 DeleteObject(hrgn);
4134 return Ok;
4137 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4138 GpRegion* region)
4140 GpStatus stat;
4141 GpRegion *temp_region;
4142 GpMatrix *world_to_device;
4143 GpRectF graphics_bounds;
4144 DWORD *pixel_data;
4145 HRGN hregion;
4146 RECT bound_rect;
4147 GpRect gp_bound_rect;
4149 if (!brush_can_fill_pixels(brush))
4150 return NotImplemented;
4152 stat = get_graphics_bounds(graphics, &graphics_bounds);
4154 if (stat == Ok)
4155 stat = GdipCloneRegion(region, &temp_region);
4157 if (stat == Ok)
4159 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4160 CoordinateSpaceWorld, &world_to_device);
4162 if (stat == Ok)
4164 stat = GdipTransformRegion(temp_region, world_to_device);
4166 GdipDeleteMatrix(world_to_device);
4169 if (stat == Ok)
4170 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4172 if (stat == Ok)
4173 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4175 GdipDeleteRegion(temp_region);
4178 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4180 DeleteObject(hregion);
4181 return Ok;
4184 if (stat == Ok)
4186 gp_bound_rect.X = bound_rect.left;
4187 gp_bound_rect.Y = bound_rect.top;
4188 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4189 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4191 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4192 if (!pixel_data)
4193 stat = OutOfMemory;
4195 if (stat == Ok)
4197 stat = brush_fill_pixels(graphics, brush, pixel_data,
4198 &gp_bound_rect, gp_bound_rect.Width);
4200 if (stat == Ok)
4201 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4202 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4203 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4205 GdipFree(pixel_data);
4208 DeleteObject(hregion);
4211 return stat;
4214 /*****************************************************************************
4215 * GdipFillRegion [GDIPLUS.@]
4217 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4218 GpRegion* region)
4220 GpStatus stat = NotImplemented;
4222 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4224 if (!(graphics && brush && region))
4225 return InvalidParameter;
4227 if(graphics->busy)
4228 return ObjectBusy;
4230 if (!graphics->image)
4231 stat = GDI32_GdipFillRegion(graphics, brush, region);
4233 if (stat == NotImplemented)
4234 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4236 if (stat == NotImplemented)
4238 FIXME("not implemented for brushtype %i\n", brush->bt);
4239 stat = Ok;
4242 return stat;
4245 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4247 TRACE("(%p,%u)\n", graphics, intention);
4249 if(!graphics)
4250 return InvalidParameter;
4252 if(graphics->busy)
4253 return ObjectBusy;
4255 /* We have no internal operation queue, so there's no need to clear it. */
4257 if (graphics->hdc)
4258 GdiFlush();
4260 return Ok;
4263 /*****************************************************************************
4264 * GdipGetClipBounds [GDIPLUS.@]
4266 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4268 TRACE("(%p, %p)\n", graphics, rect);
4270 if(!graphics)
4271 return InvalidParameter;
4273 if(graphics->busy)
4274 return ObjectBusy;
4276 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4279 /*****************************************************************************
4280 * GdipGetClipBoundsI [GDIPLUS.@]
4282 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4284 TRACE("(%p, %p)\n", graphics, rect);
4286 if(!graphics)
4287 return InvalidParameter;
4289 if(graphics->busy)
4290 return ObjectBusy;
4292 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4295 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4296 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4297 CompositingMode *mode)
4299 TRACE("(%p, %p)\n", graphics, mode);
4301 if(!graphics || !mode)
4302 return InvalidParameter;
4304 if(graphics->busy)
4305 return ObjectBusy;
4307 *mode = graphics->compmode;
4309 return Ok;
4312 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4313 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4314 CompositingQuality *quality)
4316 TRACE("(%p, %p)\n", graphics, quality);
4318 if(!graphics || !quality)
4319 return InvalidParameter;
4321 if(graphics->busy)
4322 return ObjectBusy;
4324 *quality = graphics->compqual;
4326 return Ok;
4329 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4330 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4331 InterpolationMode *mode)
4333 TRACE("(%p, %p)\n", graphics, mode);
4335 if(!graphics || !mode)
4336 return InvalidParameter;
4338 if(graphics->busy)
4339 return ObjectBusy;
4341 *mode = graphics->interpolation;
4343 return Ok;
4346 /* FIXME: Need to handle color depths less than 24bpp */
4347 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4349 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4351 if(!graphics || !argb)
4352 return InvalidParameter;
4354 if(graphics->busy)
4355 return ObjectBusy;
4357 return Ok;
4360 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4362 TRACE("(%p, %p)\n", graphics, scale);
4364 if(!graphics || !scale)
4365 return InvalidParameter;
4367 if(graphics->busy)
4368 return ObjectBusy;
4370 *scale = graphics->scale;
4372 return Ok;
4375 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4377 TRACE("(%p, %p)\n", graphics, unit);
4379 if(!graphics || !unit)
4380 return InvalidParameter;
4382 if(graphics->busy)
4383 return ObjectBusy;
4385 *unit = graphics->unit;
4387 return Ok;
4390 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4391 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4392 *mode)
4394 TRACE("(%p, %p)\n", graphics, mode);
4396 if(!graphics || !mode)
4397 return InvalidParameter;
4399 if(graphics->busy)
4400 return ObjectBusy;
4402 *mode = graphics->pixeloffset;
4404 return Ok;
4407 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4408 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4410 TRACE("(%p, %p)\n", graphics, mode);
4412 if(!graphics || !mode)
4413 return InvalidParameter;
4415 if(graphics->busy)
4416 return ObjectBusy;
4418 *mode = graphics->smoothing;
4420 return Ok;
4423 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4425 TRACE("(%p, %p)\n", graphics, contrast);
4427 if(!graphics || !contrast)
4428 return InvalidParameter;
4430 *contrast = graphics->textcontrast;
4432 return Ok;
4435 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4436 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4437 TextRenderingHint *hint)
4439 TRACE("(%p, %p)\n", graphics, hint);
4441 if(!graphics || !hint)
4442 return InvalidParameter;
4444 if(graphics->busy)
4445 return ObjectBusy;
4447 *hint = graphics->texthint;
4449 return Ok;
4452 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4454 GpRegion *clip_rgn;
4455 GpStatus stat;
4457 TRACE("(%p, %p)\n", graphics, rect);
4459 if(!graphics || !rect)
4460 return InvalidParameter;
4462 if(graphics->busy)
4463 return ObjectBusy;
4465 /* intersect window and graphics clipping regions */
4466 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4467 return stat;
4469 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4470 goto cleanup;
4472 /* get bounds of the region */
4473 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4475 cleanup:
4476 GdipDeleteRegion(clip_rgn);
4478 return stat;
4481 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4483 GpRectF rectf;
4484 GpStatus stat;
4486 TRACE("(%p, %p)\n", graphics, rect);
4488 if(!graphics || !rect)
4489 return InvalidParameter;
4491 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4493 rect->X = roundr(rectf.X);
4494 rect->Y = roundr(rectf.Y);
4495 rect->Width = roundr(rectf.Width);
4496 rect->Height = roundr(rectf.Height);
4499 return stat;
4502 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4504 TRACE("(%p, %p)\n", graphics, matrix);
4506 if(!graphics || !matrix)
4507 return InvalidParameter;
4509 if(graphics->busy)
4510 return ObjectBusy;
4512 *matrix = *graphics->worldtrans;
4513 return Ok;
4516 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4518 GpSolidFill *brush;
4519 GpStatus stat;
4520 GpRectF wnd_rect;
4522 TRACE("(%p, %x)\n", graphics, color);
4524 if(!graphics)
4525 return InvalidParameter;
4527 if(graphics->busy)
4528 return ObjectBusy;
4530 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4531 return stat;
4533 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4534 GdipDeleteBrush((GpBrush*)brush);
4535 return stat;
4538 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4539 wnd_rect.Width, wnd_rect.Height);
4541 GdipDeleteBrush((GpBrush*)brush);
4543 return Ok;
4546 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4548 TRACE("(%p, %p)\n", graphics, res);
4550 if(!graphics || !res)
4551 return InvalidParameter;
4553 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4556 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4558 GpStatus stat;
4559 GpRegion* rgn;
4560 GpPointF pt;
4562 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4564 if(!graphics || !result)
4565 return InvalidParameter;
4567 if(graphics->busy)
4568 return ObjectBusy;
4570 pt.X = x;
4571 pt.Y = y;
4572 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4573 CoordinateSpaceWorld, &pt, 1)) != Ok)
4574 return stat;
4576 if((stat = GdipCreateRegion(&rgn)) != Ok)
4577 return stat;
4579 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4580 goto cleanup;
4582 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4584 cleanup:
4585 GdipDeleteRegion(rgn);
4586 return stat;
4589 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4591 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4594 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4596 GpStatus stat;
4597 GpRegion* rgn;
4598 GpPointF pts[2];
4600 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4602 if(!graphics || !result)
4603 return InvalidParameter;
4605 if(graphics->busy)
4606 return ObjectBusy;
4608 pts[0].X = x;
4609 pts[0].Y = y;
4610 pts[1].X = x + width;
4611 pts[1].Y = y + height;
4613 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4614 CoordinateSpaceWorld, pts, 2)) != Ok)
4615 return stat;
4617 pts[1].X -= pts[0].X;
4618 pts[1].Y -= pts[0].Y;
4620 if((stat = GdipCreateRegion(&rgn)) != Ok)
4621 return stat;
4623 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4624 goto cleanup;
4626 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4628 cleanup:
4629 GdipDeleteRegion(rgn);
4630 return stat;
4633 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4635 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4638 GpStatus gdip_format_string(HDC hdc,
4639 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4640 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4641 gdip_format_string_callback callback, void *user_data)
4643 WCHAR* stringdup;
4644 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4645 nheight, lineend, lineno = 0;
4646 RectF bounds;
4647 StringAlignment halign;
4648 GpStatus stat = Ok;
4649 SIZE size;
4651 if(length == -1) length = lstrlenW(string);
4653 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4654 if(!stringdup) return OutOfMemory;
4656 nwidth = roundr(rect->Width);
4657 nheight = roundr(rect->Height);
4659 if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
4660 if (rect->Height >= INT_MAX || rect->Height < 0.5) nheight = INT_MAX;
4662 for(i = 0, j = 0; i < length; i++){
4663 /* FIXME: This makes the indexes passed to callback inaccurate. */
4664 if(!isprintW(string[i]) && (string[i] != '\n'))
4665 continue;
4667 stringdup[j] = string[i];
4668 j++;
4671 length = j;
4673 if (format) halign = format->align;
4674 else halign = StringAlignmentNear;
4676 while(sum < length){
4677 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4678 nwidth, &fit, NULL, &size);
4679 fitcpy = fit;
4681 if(fit == 0)
4682 break;
4684 for(lret = 0; lret < fit; lret++)
4685 if(*(stringdup + sum + lret) == '\n')
4686 break;
4688 /* Line break code (may look strange, but it imitates windows). */
4689 if(lret < fit)
4690 lineend = fit = lret; /* this is not an off-by-one error */
4691 else if(fit < (length - sum)){
4692 if(*(stringdup + sum + fit) == ' ')
4693 while(*(stringdup + sum + fit) == ' ')
4694 fit++;
4695 else
4696 while(*(stringdup + sum + fit - 1) != ' '){
4697 fit--;
4699 if(*(stringdup + sum + fit) == '\t')
4700 break;
4702 if(fit == 0){
4703 fit = fitcpy;
4704 break;
4707 lineend = fit;
4708 while(*(stringdup + sum + lineend - 1) == ' ' ||
4709 *(stringdup + sum + lineend - 1) == '\t')
4710 lineend--;
4712 else
4713 lineend = fit;
4715 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4716 nwidth, &j, NULL, &size);
4718 bounds.Width = size.cx;
4720 if(height + size.cy > nheight)
4721 bounds.Height = nheight - (height + size.cy);
4722 else
4723 bounds.Height = size.cy;
4725 bounds.Y = rect->Y + height;
4727 switch (halign)
4729 case StringAlignmentNear:
4730 default:
4731 bounds.X = rect->X;
4732 break;
4733 case StringAlignmentCenter:
4734 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4735 break;
4736 case StringAlignmentFar:
4737 bounds.X = rect->X + rect->Width - bounds.Width;
4738 break;
4741 stat = callback(hdc, stringdup, sum, lineend,
4742 font, rect, format, lineno, &bounds, user_data);
4744 if (stat != Ok)
4745 break;
4747 sum += fit + (lret < fitcpy ? 1 : 0);
4748 height += size.cy;
4749 lineno++;
4751 if(height > nheight)
4752 break;
4754 /* Stop if this was a linewrap (but not if it was a linebreak). */
4755 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4756 break;
4759 GdipFree(stringdup);
4761 return stat;
4764 struct measure_ranges_args {
4765 GpRegion **regions;
4768 static GpStatus measure_ranges_callback(HDC hdc,
4769 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4770 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4771 INT lineno, const RectF *bounds, void *user_data)
4773 int i;
4774 GpStatus stat = Ok;
4775 struct measure_ranges_args *args = user_data;
4777 for (i=0; i<format->range_count; i++)
4779 INT range_start = max(index, format->character_ranges[i].First);
4780 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4781 if (range_start < range_end)
4783 GpRectF range_rect;
4784 SIZE range_size;
4786 range_rect.Y = bounds->Y;
4787 range_rect.Height = bounds->Height;
4789 GetTextExtentExPointW(hdc, string + index, range_start - index,
4790 INT_MAX, NULL, NULL, &range_size);
4791 range_rect.X = bounds->X + range_size.cx;
4793 GetTextExtentExPointW(hdc, string + index, range_end - index,
4794 INT_MAX, NULL, NULL, &range_size);
4795 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
4797 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4798 if (stat != Ok)
4799 break;
4803 return stat;
4806 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4807 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4808 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4809 INT regionCount, GpRegion** regions)
4811 GpStatus stat;
4812 int i;
4813 HFONT oldfont;
4814 struct measure_ranges_args args;
4815 HDC hdc, temp_hdc=NULL;
4817 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4818 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4820 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4821 return InvalidParameter;
4823 if (regionCount < stringFormat->range_count)
4824 return InvalidParameter;
4826 if(!graphics->hdc)
4828 hdc = temp_hdc = CreateCompatibleDC(0);
4829 if (!temp_hdc) return OutOfMemory;
4831 else
4832 hdc = graphics->hdc;
4834 if (stringFormat->attr)
4835 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4837 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4839 for (i=0; i<stringFormat->range_count; i++)
4841 stat = GdipSetEmpty(regions[i]);
4842 if (stat != Ok)
4843 return stat;
4846 args.regions = regions;
4848 stat = gdip_format_string(hdc, string, length, font, layoutRect, stringFormat,
4849 measure_ranges_callback, &args);
4851 DeleteObject(SelectObject(hdc, oldfont));
4853 if (temp_hdc)
4854 DeleteDC(temp_hdc);
4856 return stat;
4859 struct measure_string_args {
4860 RectF *bounds;
4861 INT *codepointsfitted;
4862 INT *linesfilled;
4865 static GpStatus measure_string_callback(HDC hdc,
4866 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4867 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4868 INT lineno, const RectF *bounds, void *user_data)
4870 struct measure_string_args *args = user_data;
4872 if (bounds->Width > args->bounds->Width)
4873 args->bounds->Width = bounds->Width;
4875 if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
4876 args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
4878 if (args->codepointsfitted)
4879 *args->codepointsfitted = index + length;
4881 if (args->linesfilled)
4882 (*args->linesfilled)++;
4884 return Ok;
4887 /* Find the smallest rectangle that bounds the text when it is printed in rect
4888 * according to the format options listed in format. If rect has 0 width and
4889 * height, then just find the smallest rectangle that bounds the text when it's
4890 * printed at location (rect->X, rect-Y). */
4891 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4892 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4893 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4894 INT *codepointsfitted, INT *linesfilled)
4896 HFONT oldfont;
4897 struct measure_string_args args;
4898 HDC temp_hdc=NULL, hdc;
4900 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4901 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4902 bounds, codepointsfitted, linesfilled);
4904 if(!graphics || !string || !font || !rect || !bounds)
4905 return InvalidParameter;
4907 if(!graphics->hdc)
4909 hdc = temp_hdc = CreateCompatibleDC(0);
4910 if (!temp_hdc) return OutOfMemory;
4912 else
4913 hdc = graphics->hdc;
4915 if(linesfilled) *linesfilled = 0;
4916 if(codepointsfitted) *codepointsfitted = 0;
4918 if(format)
4919 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4921 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4923 bounds->X = rect->X;
4924 bounds->Y = rect->Y;
4925 bounds->Width = 0.0;
4926 bounds->Height = 0.0;
4928 args.bounds = bounds;
4929 args.codepointsfitted = codepointsfitted;
4930 args.linesfilled = linesfilled;
4932 gdip_format_string(hdc, string, length, font, rect, format,
4933 measure_string_callback, &args);
4935 DeleteObject(SelectObject(hdc, oldfont));
4937 if (temp_hdc)
4938 DeleteDC(temp_hdc);
4940 return Ok;
4943 struct draw_string_args {
4944 GpGraphics *graphics;
4945 GDIPCONST GpBrush *brush;
4946 REAL x, y, rel_width, rel_height, ascent;
4949 static GpStatus draw_string_callback(HDC hdc,
4950 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4951 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4952 INT lineno, const RectF *bounds, void *user_data)
4954 struct draw_string_args *args = user_data;
4955 PointF position;
4957 position.X = args->x + bounds->X / args->rel_width;
4958 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4960 return GdipDrawDriverString(args->graphics, &string[index], length, font,
4961 args->brush, &position,
4962 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4965 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4966 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4967 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4969 HRGN rgn = NULL;
4970 HFONT gdifont;
4971 GpPointF pt[3], rectcpy[4];
4972 POINT corners[4];
4973 REAL rel_width, rel_height;
4974 INT save_state;
4975 REAL offsety = 0.0;
4976 struct draw_string_args args;
4977 RectF scaled_rect;
4978 HDC hdc, temp_hdc=NULL;
4979 TEXTMETRICW textmetric;
4981 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4982 length, font, debugstr_rectf(rect), format, brush);
4984 if(!graphics || !string || !font || !brush || !rect)
4985 return InvalidParameter;
4987 if(graphics->hdc)
4989 hdc = graphics->hdc;
4991 else
4993 hdc = temp_hdc = CreateCompatibleDC(0);
4996 if(format){
4997 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4999 /* Should be no need to explicitly test for StringAlignmentNear as
5000 * that is default behavior if no alignment is passed. */
5001 if(format->vertalign != StringAlignmentNear){
5002 RectF bounds;
5003 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
5005 if(format->vertalign == StringAlignmentCenter)
5006 offsety = (rect->Height - bounds.Height) / 2;
5007 else if(format->vertalign == StringAlignmentFar)
5008 offsety = (rect->Height - bounds.Height);
5012 save_state = SaveDC(hdc);
5014 pt[0].X = 0.0;
5015 pt[0].Y = 0.0;
5016 pt[1].X = 1.0;
5017 pt[1].Y = 0.0;
5018 pt[2].X = 0.0;
5019 pt[2].Y = 1.0;
5020 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5021 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5022 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5023 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5024 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5026 rectcpy[3].X = rectcpy[0].X = rect->X;
5027 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
5028 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5029 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
5030 transform_and_round_points(graphics, corners, rectcpy, 4);
5032 scaled_rect.X = 0.0;
5033 scaled_rect.Y = 0.0;
5034 scaled_rect.Width = rel_width * rect->Width;
5035 scaled_rect.Height = rel_height * rect->Height;
5037 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
5039 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5040 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5041 SelectClipRgn(hdc, rgn);
5044 get_font_hfont(graphics, font, &gdifont);
5045 SelectObject(hdc, gdifont);
5047 args.graphics = graphics;
5048 args.brush = brush;
5050 args.x = rect->X;
5051 args.y = rect->Y + offsety;
5053 args.rel_width = rel_width;
5054 args.rel_height = rel_height;
5056 GetTextMetricsW(hdc, &textmetric);
5057 args.ascent = textmetric.tmAscent / rel_height;
5059 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5060 draw_string_callback, &args);
5062 DeleteObject(rgn);
5063 DeleteObject(gdifont);
5065 RestoreDC(hdc, save_state);
5067 DeleteDC(temp_hdc);
5069 return Ok;
5072 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5074 TRACE("(%p)\n", graphics);
5076 if(!graphics)
5077 return InvalidParameter;
5079 if(graphics->busy)
5080 return ObjectBusy;
5082 return GdipSetInfinite(graphics->clip);
5085 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5087 TRACE("(%p)\n", graphics);
5089 if(!graphics)
5090 return InvalidParameter;
5092 if(graphics->busy)
5093 return ObjectBusy;
5095 graphics->worldtrans->matrix[0] = 1.0;
5096 graphics->worldtrans->matrix[1] = 0.0;
5097 graphics->worldtrans->matrix[2] = 0.0;
5098 graphics->worldtrans->matrix[3] = 1.0;
5099 graphics->worldtrans->matrix[4] = 0.0;
5100 graphics->worldtrans->matrix[5] = 0.0;
5102 return Ok;
5105 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5107 return GdipEndContainer(graphics, state);
5110 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5111 GpMatrixOrder order)
5113 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5115 if(!graphics)
5116 return InvalidParameter;
5118 if(graphics->busy)
5119 return ObjectBusy;
5121 return GdipRotateMatrix(graphics->worldtrans, angle, order);
5124 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5126 return GdipBeginContainer2(graphics, state);
5129 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5130 GraphicsContainer *state)
5132 GraphicsContainerItem *container;
5133 GpStatus sts;
5135 TRACE("(%p, %p)\n", graphics, state);
5137 if(!graphics || !state)
5138 return InvalidParameter;
5140 sts = init_container(&container, graphics);
5141 if(sts != Ok)
5142 return sts;
5144 list_add_head(&graphics->containers, &container->entry);
5145 *state = graphics->contid = container->contid;
5147 return Ok;
5150 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5152 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5153 return NotImplemented;
5156 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5158 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5159 return NotImplemented;
5162 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5164 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5165 return NotImplemented;
5168 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5170 GpStatus sts;
5171 GraphicsContainerItem *container, *container2;
5173 TRACE("(%p, %x)\n", graphics, state);
5175 if(!graphics)
5176 return InvalidParameter;
5178 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5179 if(container->contid == state)
5180 break;
5183 /* did not find a matching container */
5184 if(&container->entry == &graphics->containers)
5185 return Ok;
5187 sts = restore_container(graphics, container);
5188 if(sts != Ok)
5189 return sts;
5191 /* remove all of the containers on top of the found container */
5192 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5193 if(container->contid == state)
5194 break;
5195 list_remove(&container->entry);
5196 delete_container(container);
5199 list_remove(&container->entry);
5200 delete_container(container);
5202 return Ok;
5205 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5206 REAL sy, GpMatrixOrder order)
5208 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5210 if(!graphics)
5211 return InvalidParameter;
5213 if(graphics->busy)
5214 return ObjectBusy;
5216 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
5219 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5220 CombineMode mode)
5222 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5224 if(!graphics || !srcgraphics)
5225 return InvalidParameter;
5227 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5230 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5231 CompositingMode mode)
5233 TRACE("(%p, %d)\n", graphics, mode);
5235 if(!graphics)
5236 return InvalidParameter;
5238 if(graphics->busy)
5239 return ObjectBusy;
5241 graphics->compmode = mode;
5243 return Ok;
5246 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5247 CompositingQuality quality)
5249 TRACE("(%p, %d)\n", graphics, quality);
5251 if(!graphics)
5252 return InvalidParameter;
5254 if(graphics->busy)
5255 return ObjectBusy;
5257 graphics->compqual = quality;
5259 return Ok;
5262 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5263 InterpolationMode mode)
5265 TRACE("(%p, %d)\n", graphics, mode);
5267 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5268 return InvalidParameter;
5270 if(graphics->busy)
5271 return ObjectBusy;
5273 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5274 mode = InterpolationModeBilinear;
5276 if (mode == InterpolationModeHighQuality)
5277 mode = InterpolationModeHighQualityBicubic;
5279 graphics->interpolation = mode;
5281 return Ok;
5284 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5286 TRACE("(%p, %.2f)\n", graphics, scale);
5288 if(!graphics || (scale <= 0.0))
5289 return InvalidParameter;
5291 if(graphics->busy)
5292 return ObjectBusy;
5294 graphics->scale = scale;
5296 return Ok;
5299 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5301 TRACE("(%p, %d)\n", graphics, unit);
5303 if(!graphics)
5304 return InvalidParameter;
5306 if(graphics->busy)
5307 return ObjectBusy;
5309 if(unit == UnitWorld)
5310 return InvalidParameter;
5312 graphics->unit = unit;
5314 return Ok;
5317 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5318 mode)
5320 TRACE("(%p, %d)\n", graphics, mode);
5322 if(!graphics)
5323 return InvalidParameter;
5325 if(graphics->busy)
5326 return ObjectBusy;
5328 graphics->pixeloffset = mode;
5330 return Ok;
5333 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5335 static int calls;
5337 TRACE("(%p,%i,%i)\n", graphics, x, y);
5339 if (!(calls++))
5340 FIXME("not implemented\n");
5342 return NotImplemented;
5345 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5347 static int calls;
5349 TRACE("(%p,%p,%p)\n", graphics, x, y);
5351 if (!(calls++))
5352 FIXME("not implemented\n");
5354 *x = *y = 0;
5356 return NotImplemented;
5359 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5361 TRACE("(%p, %d)\n", graphics, mode);
5363 if(!graphics)
5364 return InvalidParameter;
5366 if(graphics->busy)
5367 return ObjectBusy;
5369 graphics->smoothing = mode;
5371 return Ok;
5374 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5376 TRACE("(%p, %d)\n", graphics, contrast);
5378 if(!graphics)
5379 return InvalidParameter;
5381 graphics->textcontrast = contrast;
5383 return Ok;
5386 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5387 TextRenderingHint hint)
5389 TRACE("(%p, %d)\n", graphics, hint);
5391 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5392 return InvalidParameter;
5394 if(graphics->busy)
5395 return ObjectBusy;
5397 graphics->texthint = hint;
5399 return Ok;
5402 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5404 TRACE("(%p, %p)\n", graphics, matrix);
5406 if(!graphics || !matrix)
5407 return InvalidParameter;
5409 if(graphics->busy)
5410 return ObjectBusy;
5412 GdipDeleteMatrix(graphics->worldtrans);
5413 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5416 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5417 REAL dy, GpMatrixOrder order)
5419 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5421 if(!graphics)
5422 return InvalidParameter;
5424 if(graphics->busy)
5425 return ObjectBusy;
5427 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5430 /*****************************************************************************
5431 * GdipSetClipHrgn [GDIPLUS.@]
5433 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5435 GpRegion *region;
5436 GpStatus status;
5438 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5440 if(!graphics)
5441 return InvalidParameter;
5443 status = GdipCreateRegionHrgn(hrgn, &region);
5444 if(status != Ok)
5445 return status;
5447 status = GdipSetClipRegion(graphics, region, mode);
5449 GdipDeleteRegion(region);
5450 return status;
5453 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5455 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5457 if(!graphics)
5458 return InvalidParameter;
5460 if(graphics->busy)
5461 return ObjectBusy;
5463 return GdipCombineRegionPath(graphics->clip, path, mode);
5466 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5467 REAL width, REAL height,
5468 CombineMode mode)
5470 GpRectF rect;
5472 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5474 if(!graphics)
5475 return InvalidParameter;
5477 if(graphics->busy)
5478 return ObjectBusy;
5480 rect.X = x;
5481 rect.Y = y;
5482 rect.Width = width;
5483 rect.Height = height;
5485 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5488 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5489 INT width, INT height,
5490 CombineMode mode)
5492 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5494 if(!graphics)
5495 return InvalidParameter;
5497 if(graphics->busy)
5498 return ObjectBusy;
5500 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5503 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5504 CombineMode mode)
5506 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5508 if(!graphics || !region)
5509 return InvalidParameter;
5511 if(graphics->busy)
5512 return ObjectBusy;
5514 return GdipCombineRegionRegion(graphics->clip, region, mode);
5517 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5518 UINT limitDpi)
5520 static int calls;
5522 TRACE("(%p,%u)\n", metafile, limitDpi);
5524 if(!(calls++))
5525 FIXME("not implemented\n");
5527 return NotImplemented;
5530 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5531 INT count)
5533 INT save_state;
5534 POINT *pti;
5536 TRACE("(%p, %p, %d)\n", graphics, points, count);
5538 if(!graphics || !pen || count<=0)
5539 return InvalidParameter;
5541 if(graphics->busy)
5542 return ObjectBusy;
5544 if (!graphics->hdc)
5546 FIXME("graphics object has no HDC\n");
5547 return Ok;
5550 pti = GdipAlloc(sizeof(POINT) * count);
5552 save_state = prepare_dc(graphics, pen);
5553 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5555 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5556 Polygon(graphics->hdc, pti, count);
5558 restore_dc(graphics, save_state);
5559 GdipFree(pti);
5561 return Ok;
5564 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5565 INT count)
5567 GpStatus ret;
5568 GpPointF *ptf;
5569 INT i;
5571 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5573 if(count<=0) return InvalidParameter;
5574 ptf = GdipAlloc(sizeof(GpPointF) * count);
5576 for(i = 0;i < count; i++){
5577 ptf[i].X = (REAL)points[i].X;
5578 ptf[i].Y = (REAL)points[i].Y;
5581 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5582 GdipFree(ptf);
5584 return ret;
5587 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5589 TRACE("(%p, %p)\n", graphics, dpi);
5591 if(!graphics || !dpi)
5592 return InvalidParameter;
5594 if(graphics->busy)
5595 return ObjectBusy;
5597 if (graphics->image)
5598 *dpi = graphics->image->xres;
5599 else
5600 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
5602 return Ok;
5605 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5607 TRACE("(%p, %p)\n", graphics, dpi);
5609 if(!graphics || !dpi)
5610 return InvalidParameter;
5612 if(graphics->busy)
5613 return ObjectBusy;
5615 if (graphics->image)
5616 *dpi = graphics->image->yres;
5617 else
5618 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
5620 return Ok;
5623 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5624 GpMatrixOrder order)
5626 GpMatrix m;
5627 GpStatus ret;
5629 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5631 if(!graphics || !matrix)
5632 return InvalidParameter;
5634 if(graphics->busy)
5635 return ObjectBusy;
5637 m = *(graphics->worldtrans);
5639 ret = GdipMultiplyMatrix(&m, matrix, order);
5640 if(ret == Ok)
5641 *(graphics->worldtrans) = m;
5643 return ret;
5646 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5647 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5649 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5651 GpStatus stat=Ok;
5653 TRACE("(%p, %p)\n", graphics, hdc);
5655 if(!graphics || !hdc)
5656 return InvalidParameter;
5658 if(graphics->busy)
5659 return ObjectBusy;
5661 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5663 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5665 else if (!graphics->hdc ||
5666 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5668 /* Create a fake HDC and fill it with a constant color. */
5669 HDC temp_hdc;
5670 HBITMAP hbitmap;
5671 GpRectF bounds;
5672 BITMAPINFOHEADER bmih;
5673 int i;
5675 stat = get_graphics_bounds(graphics, &bounds);
5676 if (stat != Ok)
5677 return stat;
5679 graphics->temp_hbitmap_width = bounds.Width;
5680 graphics->temp_hbitmap_height = bounds.Height;
5682 bmih.biSize = sizeof(bmih);
5683 bmih.biWidth = graphics->temp_hbitmap_width;
5684 bmih.biHeight = -graphics->temp_hbitmap_height;
5685 bmih.biPlanes = 1;
5686 bmih.biBitCount = 32;
5687 bmih.biCompression = BI_RGB;
5688 bmih.biSizeImage = 0;
5689 bmih.biXPelsPerMeter = 0;
5690 bmih.biYPelsPerMeter = 0;
5691 bmih.biClrUsed = 0;
5692 bmih.biClrImportant = 0;
5694 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5695 (void**)&graphics->temp_bits, NULL, 0);
5696 if (!hbitmap)
5697 return GenericError;
5699 temp_hdc = CreateCompatibleDC(0);
5700 if (!temp_hdc)
5702 DeleteObject(hbitmap);
5703 return GenericError;
5706 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5707 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5709 SelectObject(temp_hdc, hbitmap);
5711 graphics->temp_hbitmap = hbitmap;
5712 *hdc = graphics->temp_hdc = temp_hdc;
5714 else
5716 *hdc = graphics->hdc;
5719 if (stat == Ok)
5720 graphics->busy = TRUE;
5722 return stat;
5725 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5727 GpStatus stat=Ok;
5729 TRACE("(%p, %p)\n", graphics, hdc);
5731 if(!graphics || !hdc || !graphics->busy)
5732 return InvalidParameter;
5734 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5736 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5738 else if (graphics->temp_hdc == hdc)
5740 DWORD* pos;
5741 int i;
5743 /* Find the pixels that have changed, and mark them as opaque. */
5744 pos = (DWORD*)graphics->temp_bits;
5745 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5747 if (*pos != DC_BACKGROUND_KEY)
5749 *pos |= 0xff000000;
5751 pos++;
5754 /* Write the changed pixels to the real target. */
5755 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5756 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5757 graphics->temp_hbitmap_width * 4);
5759 /* Clean up. */
5760 DeleteDC(graphics->temp_hdc);
5761 DeleteObject(graphics->temp_hbitmap);
5762 graphics->temp_hdc = NULL;
5763 graphics->temp_hbitmap = NULL;
5765 else if (hdc != graphics->hdc)
5767 stat = InvalidParameter;
5770 if (stat == Ok)
5771 graphics->busy = FALSE;
5773 return stat;
5776 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5778 GpRegion *clip;
5779 GpStatus status;
5781 TRACE("(%p, %p)\n", graphics, region);
5783 if(!graphics || !region)
5784 return InvalidParameter;
5786 if(graphics->busy)
5787 return ObjectBusy;
5789 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5790 return status;
5792 /* free everything except root node and header */
5793 delete_element(&region->node);
5794 memcpy(region, clip, sizeof(GpRegion));
5795 GdipFree(clip);
5797 return Ok;
5800 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5801 GpCoordinateSpace src_space, GpMatrix **matrix)
5803 GpStatus stat = GdipCreateMatrix(matrix);
5804 REAL unitscale;
5806 if (dst_space != src_space && stat == Ok)
5808 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
5810 if(graphics->unit != UnitDisplay)
5811 unitscale *= graphics->scale;
5813 /* transform from src_space to CoordinateSpacePage */
5814 switch (src_space)
5816 case CoordinateSpaceWorld:
5817 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5818 break;
5819 case CoordinateSpacePage:
5820 break;
5821 case CoordinateSpaceDevice:
5822 GdipScaleMatrix(*matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
5823 break;
5826 /* transform from CoordinateSpacePage to dst_space */
5827 switch (dst_space)
5829 case CoordinateSpaceWorld:
5831 GpMatrix *inverted_transform;
5832 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
5833 if (stat == Ok)
5835 stat = GdipInvertMatrix(inverted_transform);
5836 if (stat == Ok)
5837 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
5838 GdipDeleteMatrix(inverted_transform);
5840 break;
5842 case CoordinateSpacePage:
5843 break;
5844 case CoordinateSpaceDevice:
5845 GdipScaleMatrix(*matrix, unitscale, unitscale, MatrixOrderAppend);
5846 break;
5849 return stat;
5852 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5853 GpCoordinateSpace src_space, GpPointF *points, INT count)
5855 GpMatrix *matrix;
5856 GpStatus stat;
5858 if(!graphics || !points || count <= 0)
5859 return InvalidParameter;
5861 if(graphics->busy)
5862 return ObjectBusy;
5864 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5866 if (src_space == dst_space) return Ok;
5868 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5870 if (stat == Ok)
5872 stat = GdipTransformMatrixPoints(matrix, points, count);
5874 GdipDeleteMatrix(matrix);
5877 return stat;
5880 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5881 GpCoordinateSpace src_space, GpPoint *points, INT count)
5883 GpPointF *pointsF;
5884 GpStatus ret;
5885 INT i;
5887 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5889 if(count <= 0)
5890 return InvalidParameter;
5892 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5893 if(!pointsF)
5894 return OutOfMemory;
5896 for(i = 0; i < count; i++){
5897 pointsF[i].X = (REAL)points[i].X;
5898 pointsF[i].Y = (REAL)points[i].Y;
5901 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5903 if(ret == Ok)
5904 for(i = 0; i < count; i++){
5905 points[i].X = roundr(pointsF[i].X);
5906 points[i].Y = roundr(pointsF[i].Y);
5908 GdipFree(pointsF);
5910 return ret;
5913 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5915 static int calls;
5917 TRACE("\n");
5919 if (!calls++)
5920 FIXME("stub\n");
5922 return NULL;
5925 /*****************************************************************************
5926 * GdipTranslateClip [GDIPLUS.@]
5928 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5930 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5932 if(!graphics)
5933 return InvalidParameter;
5935 if(graphics->busy)
5936 return ObjectBusy;
5938 return GdipTranslateRegion(graphics->clip, dx, dy);
5941 /*****************************************************************************
5942 * GdipTranslateClipI [GDIPLUS.@]
5944 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5946 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5948 if(!graphics)
5949 return InvalidParameter;
5951 if(graphics->busy)
5952 return ObjectBusy;
5954 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5958 /*****************************************************************************
5959 * GdipMeasureDriverString [GDIPLUS.@]
5961 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5962 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5963 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5965 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
5966 HFONT hfont;
5967 HDC hdc;
5968 REAL min_x, min_y, max_x, max_y, x, y;
5969 int i;
5970 TEXTMETRICW textmetric;
5971 const WORD *glyph_indices;
5972 WORD *dynamic_glyph_indices=NULL;
5973 REAL rel_width, rel_height, ascent, descent;
5974 GpPointF pt[3];
5976 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5978 if (!graphics || !text || !font || !positions || !boundingBox)
5979 return InvalidParameter;
5981 if (length == -1)
5982 length = strlenW(text);
5984 if (length == 0)
5986 boundingBox->X = 0.0;
5987 boundingBox->Y = 0.0;
5988 boundingBox->Width = 0.0;
5989 boundingBox->Height = 0.0;
5992 if (flags & unsupported_flags)
5993 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
5995 if (matrix)
5996 FIXME("Ignoring matrix\n");
5998 get_font_hfont(graphics, font, &hfont);
6000 hdc = CreateCompatibleDC(0);
6001 SelectObject(hdc, hfont);
6003 GetTextMetricsW(hdc, &textmetric);
6005 pt[0].X = 0.0;
6006 pt[0].Y = 0.0;
6007 pt[1].X = 1.0;
6008 pt[1].Y = 0.0;
6009 pt[2].X = 0.0;
6010 pt[2].Y = 1.0;
6011 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6012 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6013 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6014 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6015 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6017 if (flags & DriverStringOptionsCmapLookup)
6019 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6020 if (!glyph_indices)
6022 DeleteDC(hdc);
6023 DeleteObject(hfont);
6024 return OutOfMemory;
6027 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6029 else
6030 glyph_indices = text;
6032 min_x = max_x = x = positions[0].X;
6033 min_y = max_y = y = positions[0].Y;
6035 ascent = textmetric.tmAscent / rel_height;
6036 descent = textmetric.tmDescent / rel_height;
6038 for (i=0; i<length; i++)
6040 int char_width;
6041 ABC abc;
6043 if (!(flags & DriverStringOptionsRealizedAdvance))
6045 x = positions[i].X;
6046 y = positions[i].Y;
6049 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6050 char_width = abc.abcA + abc.abcB + abc.abcB;
6052 if (min_y > y - ascent) min_y = y - ascent;
6053 if (max_y < y + descent) max_y = y + descent;
6054 if (min_x > x) min_x = x;
6056 x += char_width / rel_width;
6058 if (max_x < x) max_x = x;
6061 GdipFree(dynamic_glyph_indices);
6062 DeleteDC(hdc);
6063 DeleteObject(hfont);
6065 boundingBox->X = min_x;
6066 boundingBox->Y = min_y;
6067 boundingBox->Width = max_x - min_x;
6068 boundingBox->Height = max_y - min_y;
6070 return Ok;
6073 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6074 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6075 GDIPCONST PointF *positions, INT flags,
6076 GDIPCONST GpMatrix *matrix )
6078 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6079 INT save_state;
6080 GpPointF pt;
6081 HFONT hfont;
6082 UINT eto_flags=0;
6084 if (flags & unsupported_flags)
6085 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6087 if (matrix)
6088 FIXME("Ignoring matrix\n");
6090 if (!(flags & DriverStringOptionsCmapLookup))
6091 eto_flags |= ETO_GLYPH_INDEX;
6093 save_state = SaveDC(graphics->hdc);
6094 SetBkMode(graphics->hdc, TRANSPARENT);
6095 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6097 pt = positions[0];
6098 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6100 get_font_hfont(graphics, font, &hfont);
6101 SelectObject(graphics->hdc, hfont);
6103 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6105 ExtTextOutW(graphics->hdc, roundr(pt.X), roundr(pt.Y), eto_flags, NULL, text, length, NULL);
6107 RestoreDC(graphics->hdc, save_state);
6109 DeleteObject(hfont);
6111 return Ok;
6114 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6115 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6116 GDIPCONST PointF *positions, INT flags,
6117 GDIPCONST GpMatrix *matrix )
6119 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6120 GpStatus stat;
6121 PointF *real_positions, real_position;
6122 POINT *pti;
6123 HFONT hfont;
6124 HDC hdc;
6125 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6126 DWORD max_glyphsize=0;
6127 GLYPHMETRICS glyphmetrics;
6128 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6129 BYTE *glyph_mask;
6130 BYTE *text_mask;
6131 int text_mask_stride;
6132 BYTE *pixel_data;
6133 int pixel_data_stride;
6134 GpRect pixel_area;
6135 UINT ggo_flags = GGO_GRAY8_BITMAP;
6137 if (length <= 0)
6138 return Ok;
6140 if (!(flags & DriverStringOptionsCmapLookup))
6141 ggo_flags |= GGO_GLYPH_INDEX;
6143 if (flags & unsupported_flags)
6144 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6146 if (matrix)
6147 FIXME("Ignoring matrix\n");
6149 pti = GdipAlloc(sizeof(POINT) * length);
6150 if (!pti)
6151 return OutOfMemory;
6153 if (flags & DriverStringOptionsRealizedAdvance)
6155 real_position = positions[0];
6157 transform_and_round_points(graphics, pti, &real_position, 1);
6159 else
6161 real_positions = GdipAlloc(sizeof(PointF) * length);
6162 if (!real_positions)
6164 GdipFree(pti);
6165 return OutOfMemory;
6168 memcpy(real_positions, positions, sizeof(PointF) * length);
6170 transform_and_round_points(graphics, pti, real_positions, length);
6172 GdipFree(real_positions);
6175 get_font_hfont(graphics, font, &hfont);
6177 hdc = CreateCompatibleDC(0);
6178 SelectObject(hdc, hfont);
6180 /* Get the boundaries of the text to be drawn */
6181 for (i=0; i<length; i++)
6183 DWORD glyphsize;
6184 int left, top, right, bottom;
6186 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6187 &glyphmetrics, 0, NULL, &identity);
6189 if (glyphsize == GDI_ERROR)
6191 ERR("GetGlyphOutlineW failed\n");
6192 GdipFree(pti);
6193 DeleteDC(hdc);
6194 DeleteObject(hfont);
6195 return GenericError;
6198 if (glyphsize > max_glyphsize)
6199 max_glyphsize = glyphsize;
6201 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6202 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6203 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6204 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6206 if (left < min_x) min_x = left;
6207 if (top < min_y) min_y = top;
6208 if (right > max_x) max_x = right;
6209 if (bottom > max_y) max_y = bottom;
6211 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6213 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6214 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6218 glyph_mask = GdipAlloc(max_glyphsize);
6219 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6220 text_mask_stride = max_x - min_x;
6222 if (!(glyph_mask && text_mask))
6224 GdipFree(glyph_mask);
6225 GdipFree(text_mask);
6226 GdipFree(pti);
6227 DeleteDC(hdc);
6228 DeleteObject(hfont);
6229 return OutOfMemory;
6232 /* Generate a mask for the text */
6233 for (i=0; i<length; i++)
6235 int left, top, stride;
6237 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6238 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6240 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6241 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6242 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6244 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6246 BYTE *glyph_val = glyph_mask + y * stride;
6247 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6248 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6250 *text_val = min(64, *text_val + *glyph_val);
6251 glyph_val++;
6252 text_val++;
6257 GdipFree(pti);
6258 DeleteDC(hdc);
6259 DeleteObject(hfont);
6260 GdipFree(glyph_mask);
6262 /* get the brush data */
6263 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6264 if (!pixel_data)
6266 GdipFree(text_mask);
6267 return OutOfMemory;
6270 pixel_area.X = min_x;
6271 pixel_area.Y = min_y;
6272 pixel_area.Width = max_x - min_x;
6273 pixel_area.Height = max_y - min_y;
6274 pixel_data_stride = pixel_area.Width * 4;
6276 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6277 if (stat != Ok)
6279 GdipFree(text_mask);
6280 GdipFree(pixel_data);
6281 return stat;
6284 /* multiply the brush data by the mask */
6285 for (y=0; y<pixel_area.Height; y++)
6287 BYTE *text_val = text_mask + text_mask_stride * y;
6288 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6289 for (x=0; x<pixel_area.Width; x++)
6291 *pixel_val = (*pixel_val) * (*text_val) / 64;
6292 text_val++;
6293 pixel_val+=4;
6297 GdipFree(text_mask);
6299 /* draw the result */
6300 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6301 pixel_area.Height, pixel_data_stride);
6303 GdipFree(pixel_data);
6305 return stat;
6308 /*****************************************************************************
6309 * GdipDrawDriverString [GDIPLUS.@]
6311 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6312 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6313 GDIPCONST PointF *positions, INT flags,
6314 GDIPCONST GpMatrix *matrix )
6316 GpStatus stat=NotImplemented;
6318 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6320 if (!graphics || !text || !font || !brush || !positions)
6321 return InvalidParameter;
6323 if (length == -1)
6324 length = strlenW(text);
6326 if (graphics->hdc &&
6327 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6328 brush->bt == BrushTypeSolidColor &&
6329 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6330 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, brush,
6331 positions, flags, matrix);
6333 if (stat == NotImplemented)
6334 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, brush,
6335 positions, flags, matrix);
6337 return stat;
6340 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6341 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6343 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6344 return NotImplemented;
6347 /*****************************************************************************
6348 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6350 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6352 GpStatus stat;
6353 GpRegion* rgn;
6355 TRACE("(%p, %p)\n", graphics, res);
6357 if((stat = GdipCreateRegion(&rgn)) != Ok)
6358 return stat;
6360 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6361 goto cleanup;
6363 stat = GdipIsEmptyRegion(rgn, graphics, res);
6365 cleanup:
6366 GdipDeleteRegion(rgn);
6367 return stat;