gdiplus: Detect hotkey prefixes when drawing strings.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blobc5ac9d146085c34f9169686e99edcc1ec0cd087f
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;
1196 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1198 static int once;
1199 if (!once++)
1200 FIXME("path gradient focus not implemented\n");
1203 if (fill->gamma)
1205 static int once;
1206 if (!once++)
1207 FIXME("path gradient gamma correction not implemented\n");
1210 stat = GdipClonePath(fill->path, &flat_path);
1212 if (stat != Ok)
1213 return stat;
1215 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1216 CoordinateSpaceWorld, &world_to_device);
1217 if (stat == Ok)
1219 stat = GdipTransformPath(flat_path, world_to_device);
1221 if (stat == Ok)
1223 center_point = fill->center;
1224 stat = GdipTransformMatrixPoints(world_to_device, &center_point, 1);
1227 if (stat == Ok)
1228 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1230 GdipDeleteMatrix(world_to_device);
1233 if (stat != Ok)
1235 GdipDeletePath(flat_path);
1236 return stat;
1239 for (i=0; i<flat_path->pathdata.Count; i++)
1241 int start_center_line=0, end_center_line=0;
1242 int seen_start=0, seen_end=0, seen_center=0;
1243 REAL center_distance;
1244 ARGB start_color, end_color;
1245 REAL dy, dx;
1247 type = flat_path->pathdata.Types[i];
1249 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1250 figure_start = i;
1252 start_point = flat_path->pathdata.Points[i];
1254 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1256 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1258 end_point = flat_path->pathdata.Points[figure_start];
1259 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1261 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1263 end_point = flat_path->pathdata.Points[i+1];
1264 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1266 else
1267 continue;
1269 outer_color = start_color;
1271 min_yf = center_point.Y;
1272 if (min_yf > start_point.Y) min_yf = start_point.Y;
1273 if (min_yf > end_point.Y) min_yf = end_point.Y;
1275 if (min_yf < fill_area->Y)
1276 min_y = fill_area->Y;
1277 else
1278 min_y = (INT)ceil(min_yf);
1280 max_yf = center_point.Y;
1281 if (max_yf < start_point.Y) max_yf = start_point.Y;
1282 if (max_yf < end_point.Y) max_yf = end_point.Y;
1284 if (max_yf > fill_area->Y + fill_area->Height)
1285 max_y = fill_area->Y + fill_area->Height;
1286 else
1287 max_y = (INT)ceil(max_yf);
1289 dy = end_point.Y - start_point.Y;
1290 dx = end_point.X - start_point.X;
1292 /* This is proportional to the distance from start-end line to center point. */
1293 center_distance = dy * (start_point.X - center_point.X) +
1294 dx * (center_point.Y - start_point.Y);
1296 for (y=min_y; y<max_y; y++)
1298 REAL yf = (REAL)y;
1300 if (!seen_start && yf >= start_point.Y)
1302 seen_start = 1;
1303 start_center_line ^= 1;
1305 if (!seen_end && yf >= end_point.Y)
1307 seen_end = 1;
1308 end_center_line ^= 1;
1310 if (!seen_center && yf >= center_point.Y)
1312 seen_center = 1;
1313 start_center_line ^= 1;
1314 end_center_line ^= 1;
1317 if (start_center_line)
1318 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1319 else
1320 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1322 if (end_center_line)
1323 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1324 else
1325 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1327 if (line1_xf < line2_xf)
1329 min_x = (INT)ceil(line1_xf);
1330 max_x = (INT)ceil(line2_xf);
1332 else
1334 min_x = (INT)ceil(line2_xf);
1335 max_x = (INT)ceil(line1_xf);
1338 if (min_x < fill_area->X)
1339 min_x = fill_area->X;
1340 if (max_x > fill_area->X + fill_area->Width)
1341 max_x = fill_area->X + fill_area->Width;
1343 for (x=min_x; x<max_x; x++)
1345 REAL xf = (REAL)x;
1346 REAL distance;
1348 if (start_color != end_color)
1350 REAL blend_amount, pdy, pdx;
1351 pdy = yf - center_point.Y;
1352 pdx = xf - center_point.X;
1353 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1354 outer_color = blend_colors(start_color, end_color, blend_amount);
1357 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1358 (end_point.X - start_point.X) * (yf - start_point.Y);
1360 distance = distance / center_distance;
1362 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1363 blend_colors(outer_color, fill->centercolor, distance);
1368 GdipDeletePath(flat_path);
1369 return stat;
1371 default:
1372 return NotImplemented;
1376 /* GdipDrawPie/GdipFillPie helper function */
1377 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1378 REAL height, REAL startAngle, REAL sweepAngle)
1380 GpPointF ptf[4];
1381 POINT pti[4];
1383 ptf[0].X = x;
1384 ptf[0].Y = y;
1385 ptf[1].X = x + width;
1386 ptf[1].Y = y + height;
1388 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1389 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1391 transform_and_round_points(graphics, pti, ptf, 4);
1393 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1394 pti[2].y, pti[3].x, pti[3].y);
1397 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1398 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1399 * should not be called on an hdc that has a path you care about. */
1400 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1401 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1403 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1404 GpMatrix *matrix = NULL;
1405 HBRUSH brush = NULL;
1406 HPEN pen = NULL;
1407 PointF ptf[4], *custptf = NULL;
1408 POINT pt[4], *custpt = NULL;
1409 BYTE *tp = NULL;
1410 REAL theta, dsmall, dbig, dx, dy = 0.0;
1411 INT i, count;
1412 LOGBRUSH lb;
1413 BOOL customstroke;
1415 if((x1 == x2) && (y1 == y2))
1416 return;
1418 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1420 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1421 if(!customstroke){
1422 brush = CreateSolidBrush(color);
1423 lb.lbStyle = BS_SOLID;
1424 lb.lbColor = color;
1425 lb.lbHatch = 0;
1426 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1427 PS_JOIN_MITER, 1, &lb, 0,
1428 NULL);
1429 oldbrush = SelectObject(graphics->hdc, brush);
1430 oldpen = SelectObject(graphics->hdc, pen);
1433 switch(cap){
1434 case LineCapFlat:
1435 break;
1436 case LineCapSquare:
1437 case LineCapSquareAnchor:
1438 case LineCapDiamondAnchor:
1439 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1440 if(cap == LineCapDiamondAnchor){
1441 dsmall = cos(theta + M_PI_2) * size;
1442 dbig = sin(theta + M_PI_2) * size;
1444 else{
1445 dsmall = cos(theta + M_PI_4) * size;
1446 dbig = sin(theta + M_PI_4) * size;
1449 ptf[0].X = x2 - dsmall;
1450 ptf[1].X = x2 + dbig;
1452 ptf[0].Y = y2 - dbig;
1453 ptf[3].Y = y2 + dsmall;
1455 ptf[1].Y = y2 - dsmall;
1456 ptf[2].Y = y2 + dbig;
1458 ptf[3].X = x2 - dbig;
1459 ptf[2].X = x2 + dsmall;
1461 transform_and_round_points(graphics, pt, ptf, 4);
1462 Polygon(graphics->hdc, pt, 4);
1464 break;
1465 case LineCapArrowAnchor:
1466 size = size * 4.0 / sqrt(3.0);
1468 dx = cos(M_PI / 6.0 + theta) * size;
1469 dy = sin(M_PI / 6.0 + theta) * size;
1471 ptf[0].X = x2 - dx;
1472 ptf[0].Y = y2 - dy;
1474 dx = cos(- M_PI / 6.0 + theta) * size;
1475 dy = sin(- M_PI / 6.0 + theta) * size;
1477 ptf[1].X = x2 - dx;
1478 ptf[1].Y = y2 - dy;
1480 ptf[2].X = x2;
1481 ptf[2].Y = y2;
1483 transform_and_round_points(graphics, pt, ptf, 3);
1484 Polygon(graphics->hdc, pt, 3);
1486 break;
1487 case LineCapRoundAnchor:
1488 dx = dy = ANCHOR_WIDTH * size / 2.0;
1490 ptf[0].X = x2 - dx;
1491 ptf[0].Y = y2 - dy;
1492 ptf[1].X = x2 + dx;
1493 ptf[1].Y = y2 + dy;
1495 transform_and_round_points(graphics, pt, ptf, 2);
1496 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1498 break;
1499 case LineCapTriangle:
1500 size = size / 2.0;
1501 dx = cos(M_PI_2 + theta) * size;
1502 dy = sin(M_PI_2 + theta) * size;
1504 ptf[0].X = x2 - dx;
1505 ptf[0].Y = y2 - dy;
1506 ptf[1].X = x2 + dx;
1507 ptf[1].Y = y2 + dy;
1509 dx = cos(theta) * size;
1510 dy = sin(theta) * size;
1512 ptf[2].X = x2 + dx;
1513 ptf[2].Y = y2 + dy;
1515 transform_and_round_points(graphics, pt, ptf, 3);
1516 Polygon(graphics->hdc, pt, 3);
1518 break;
1519 case LineCapRound:
1520 dx = dy = size / 2.0;
1522 ptf[0].X = x2 - dx;
1523 ptf[0].Y = y2 - dy;
1524 ptf[1].X = x2 + dx;
1525 ptf[1].Y = y2 + dy;
1527 dx = -cos(M_PI_2 + theta) * size;
1528 dy = -sin(M_PI_2 + theta) * size;
1530 ptf[2].X = x2 - dx;
1531 ptf[2].Y = y2 - dy;
1532 ptf[3].X = x2 + dx;
1533 ptf[3].Y = y2 + dy;
1535 transform_and_round_points(graphics, pt, ptf, 4);
1536 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1537 pt[2].y, pt[3].x, pt[3].y);
1539 break;
1540 case LineCapCustom:
1541 if(!custom)
1542 break;
1544 count = custom->pathdata.Count;
1545 custptf = GdipAlloc(count * sizeof(PointF));
1546 custpt = GdipAlloc(count * sizeof(POINT));
1547 tp = GdipAlloc(count);
1549 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1550 goto custend;
1552 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1554 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1555 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1556 MatrixOrderAppend);
1557 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1558 GdipTransformMatrixPoints(matrix, custptf, count);
1560 transform_and_round_points(graphics, custpt, custptf, count);
1562 for(i = 0; i < count; i++)
1563 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1565 if(custom->fill){
1566 BeginPath(graphics->hdc);
1567 PolyDraw(graphics->hdc, custpt, tp, count);
1568 EndPath(graphics->hdc);
1569 StrokeAndFillPath(graphics->hdc);
1571 else
1572 PolyDraw(graphics->hdc, custpt, tp, count);
1574 custend:
1575 GdipFree(custptf);
1576 GdipFree(custpt);
1577 GdipFree(tp);
1578 GdipDeleteMatrix(matrix);
1579 break;
1580 default:
1581 break;
1584 if(!customstroke){
1585 SelectObject(graphics->hdc, oldbrush);
1586 SelectObject(graphics->hdc, oldpen);
1587 DeleteObject(brush);
1588 DeleteObject(pen);
1592 /* Shortens the line by the given percent by changing x2, y2.
1593 * If percent is > 1.0 then the line will change direction.
1594 * If percent is negative it can lengthen the line. */
1595 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1597 REAL dist, theta, dx, dy;
1599 if((y1 == *y2) && (x1 == *x2))
1600 return;
1602 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1603 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1604 dx = cos(theta) * dist;
1605 dy = sin(theta) * dist;
1607 *x2 = *x2 + dx;
1608 *y2 = *y2 + dy;
1611 /* Shortens the line by the given amount by changing x2, y2.
1612 * If the amount is greater than the distance, the line will become length 0.
1613 * If the amount is negative, it can lengthen the line. */
1614 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1616 REAL dx, dy, percent;
1618 dx = *x2 - x1;
1619 dy = *y2 - y1;
1620 if(dx == 0 && dy == 0)
1621 return;
1623 percent = amt / sqrt(dx * dx + dy * dy);
1624 if(percent >= 1.0){
1625 *x2 = x1;
1626 *y2 = y1;
1627 return;
1630 shorten_line_percent(x1, y1, x2, y2, percent);
1633 /* Draws lines between the given points, and if caps is true then draws an endcap
1634 * at the end of the last line. */
1635 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1636 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1638 POINT *pti = NULL;
1639 GpPointF *ptcopy = NULL;
1640 GpStatus status = GenericError;
1642 if(!count)
1643 return Ok;
1645 pti = GdipAlloc(count * sizeof(POINT));
1646 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1648 if(!pti || !ptcopy){
1649 status = OutOfMemory;
1650 goto end;
1653 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1655 if(caps){
1656 if(pen->endcap == LineCapArrowAnchor)
1657 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1658 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1659 else if((pen->endcap == LineCapCustom) && pen->customend)
1660 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1661 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1662 pen->customend->inset * pen->width);
1664 if(pen->startcap == LineCapArrowAnchor)
1665 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1666 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1667 else if((pen->startcap == LineCapCustom) && pen->customstart)
1668 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1669 &ptcopy[0].X, &ptcopy[0].Y,
1670 pen->customstart->inset * pen->width);
1672 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1673 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1674 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1675 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1678 transform_and_round_points(graphics, pti, ptcopy, count);
1680 if(Polyline(graphics->hdc, pti, count))
1681 status = Ok;
1683 end:
1684 GdipFree(pti);
1685 GdipFree(ptcopy);
1687 return status;
1690 /* Conducts a linear search to find the bezier points that will back off
1691 * the endpoint of the curve by a distance of amt. Linear search works
1692 * better than binary in this case because there are multiple solutions,
1693 * and binary searches often find a bad one. I don't think this is what
1694 * Windows does but short of rendering the bezier without GDI's help it's
1695 * the best we can do. If rev then work from the start of the passed points
1696 * instead of the end. */
1697 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1699 GpPointF origpt[4];
1700 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1701 INT i, first = 0, second = 1, third = 2, fourth = 3;
1703 if(rev){
1704 first = 3;
1705 second = 2;
1706 third = 1;
1707 fourth = 0;
1710 origx = pt[fourth].X;
1711 origy = pt[fourth].Y;
1712 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1714 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1715 /* reset bezier points to original values */
1716 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1717 /* Perform magic on bezier points. Order is important here.*/
1718 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1719 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1720 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1721 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1722 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1723 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1725 dx = pt[fourth].X - origx;
1726 dy = pt[fourth].Y - origy;
1728 diff = sqrt(dx * dx + dy * dy);
1729 percent += 0.0005 * amt;
1733 /* Draws bezier curves between given points, and if caps is true then draws an
1734 * endcap at the end of the last line. */
1735 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1736 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1738 POINT *pti;
1739 GpPointF *ptcopy;
1740 GpStatus status = GenericError;
1742 if(!count)
1743 return Ok;
1745 pti = GdipAlloc(count * sizeof(POINT));
1746 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1748 if(!pti || !ptcopy){
1749 status = OutOfMemory;
1750 goto end;
1753 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1755 if(caps){
1756 if(pen->endcap == LineCapArrowAnchor)
1757 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1758 else if((pen->endcap == LineCapCustom) && pen->customend)
1759 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1760 FALSE);
1762 if(pen->startcap == LineCapArrowAnchor)
1763 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1764 else if((pen->startcap == LineCapCustom) && pen->customstart)
1765 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1767 /* the direction of the line cap is parallel to the direction at the
1768 * end of the bezier (which, if it has been shortened, is not the same
1769 * as the direction from pt[count-2] to pt[count-1]) */
1770 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1771 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1772 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1773 pt[count - 1].X, pt[count - 1].Y);
1775 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1776 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1777 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1780 transform_and_round_points(graphics, pti, ptcopy, count);
1782 PolyBezier(graphics->hdc, pti, count);
1784 status = Ok;
1786 end:
1787 GdipFree(pti);
1788 GdipFree(ptcopy);
1790 return status;
1793 /* Draws a combination of bezier curves and lines between points. */
1794 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1795 GDIPCONST BYTE * types, INT count, BOOL caps)
1797 POINT *pti = GdipAlloc(count * sizeof(POINT));
1798 BYTE *tp = GdipAlloc(count);
1799 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1800 INT i, j;
1801 GpStatus status = GenericError;
1803 if(!count){
1804 status = Ok;
1805 goto end;
1807 if(!pti || !tp || !ptcopy){
1808 status = OutOfMemory;
1809 goto end;
1812 for(i = 1; i < count; i++){
1813 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1814 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1815 || !(types[i + 1] & PathPointTypeBezier)){
1816 ERR("Bad bezier points\n");
1817 goto end;
1819 i += 2;
1823 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1825 /* If we are drawing caps, go through the points and adjust them accordingly,
1826 * and draw the caps. */
1827 if(caps){
1828 switch(types[count - 1] & PathPointTypePathTypeMask){
1829 case PathPointTypeBezier:
1830 if(pen->endcap == LineCapArrowAnchor)
1831 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1832 else if((pen->endcap == LineCapCustom) && pen->customend)
1833 shorten_bezier_amt(&ptcopy[count - 4],
1834 pen->width * pen->customend->inset, FALSE);
1836 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1837 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1838 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1839 pt[count - 1].X, pt[count - 1].Y);
1841 break;
1842 case PathPointTypeLine:
1843 if(pen->endcap == LineCapArrowAnchor)
1844 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1845 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1846 pen->width);
1847 else if((pen->endcap == LineCapCustom) && pen->customend)
1848 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1849 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1850 pen->customend->inset * pen->width);
1852 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1853 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1854 pt[count - 1].Y);
1856 break;
1857 default:
1858 ERR("Bad path last point\n");
1859 goto end;
1862 /* Find start of points */
1863 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1864 == PathPointTypeStart); j++);
1866 switch(types[j] & PathPointTypePathTypeMask){
1867 case PathPointTypeBezier:
1868 if(pen->startcap == LineCapArrowAnchor)
1869 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1870 else if((pen->startcap == LineCapCustom) && pen->customstart)
1871 shorten_bezier_amt(&ptcopy[j - 1],
1872 pen->width * pen->customstart->inset, TRUE);
1874 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1875 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1876 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1877 pt[j - 1].X, pt[j - 1].Y);
1879 break;
1880 case PathPointTypeLine:
1881 if(pen->startcap == LineCapArrowAnchor)
1882 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1883 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1884 pen->width);
1885 else if((pen->startcap == LineCapCustom) && pen->customstart)
1886 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1887 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1888 pen->customstart->inset * pen->width);
1890 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1891 pt[j].X, pt[j].Y, pt[j - 1].X,
1892 pt[j - 1].Y);
1894 break;
1895 default:
1896 ERR("Bad path points\n");
1897 goto end;
1901 transform_and_round_points(graphics, pti, ptcopy, count);
1903 for(i = 0; i < count; i++){
1904 tp[i] = convert_path_point_type(types[i]);
1907 PolyDraw(graphics->hdc, pti, tp, count);
1909 status = Ok;
1911 end:
1912 GdipFree(pti);
1913 GdipFree(ptcopy);
1914 GdipFree(tp);
1916 return status;
1919 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1921 GpStatus result;
1923 BeginPath(graphics->hdc);
1924 result = draw_poly(graphics, NULL, path->pathdata.Points,
1925 path->pathdata.Types, path->pathdata.Count, FALSE);
1926 EndPath(graphics->hdc);
1927 return result;
1930 typedef struct _GraphicsContainerItem {
1931 struct list entry;
1932 GraphicsContainer contid;
1934 SmoothingMode smoothing;
1935 CompositingQuality compqual;
1936 InterpolationMode interpolation;
1937 CompositingMode compmode;
1938 TextRenderingHint texthint;
1939 REAL scale;
1940 GpUnit unit;
1941 PixelOffsetMode pixeloffset;
1942 UINT textcontrast;
1943 GpMatrix* worldtrans;
1944 GpRegion* clip;
1945 } GraphicsContainerItem;
1947 static GpStatus init_container(GraphicsContainerItem** container,
1948 GDIPCONST GpGraphics* graphics){
1949 GpStatus sts;
1951 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1952 if(!(*container))
1953 return OutOfMemory;
1955 (*container)->contid = graphics->contid + 1;
1957 (*container)->smoothing = graphics->smoothing;
1958 (*container)->compqual = graphics->compqual;
1959 (*container)->interpolation = graphics->interpolation;
1960 (*container)->compmode = graphics->compmode;
1961 (*container)->texthint = graphics->texthint;
1962 (*container)->scale = graphics->scale;
1963 (*container)->unit = graphics->unit;
1964 (*container)->textcontrast = graphics->textcontrast;
1965 (*container)->pixeloffset = graphics->pixeloffset;
1967 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1968 if(sts != Ok){
1969 GdipFree(*container);
1970 *container = NULL;
1971 return sts;
1974 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1975 if(sts != Ok){
1976 GdipDeleteMatrix((*container)->worldtrans);
1977 GdipFree(*container);
1978 *container = NULL;
1979 return sts;
1982 return Ok;
1985 static void delete_container(GraphicsContainerItem* container){
1986 GdipDeleteMatrix(container->worldtrans);
1987 GdipDeleteRegion(container->clip);
1988 GdipFree(container);
1991 static GpStatus restore_container(GpGraphics* graphics,
1992 GDIPCONST GraphicsContainerItem* container){
1993 GpStatus sts;
1994 GpMatrix *newTrans;
1995 GpRegion *newClip;
1997 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1998 if(sts != Ok)
1999 return sts;
2001 sts = GdipCloneRegion(container->clip, &newClip);
2002 if(sts != Ok){
2003 GdipDeleteMatrix(newTrans);
2004 return sts;
2007 GdipDeleteMatrix(graphics->worldtrans);
2008 graphics->worldtrans = newTrans;
2010 GdipDeleteRegion(graphics->clip);
2011 graphics->clip = newClip;
2013 graphics->contid = container->contid - 1;
2015 graphics->smoothing = container->smoothing;
2016 graphics->compqual = container->compqual;
2017 graphics->interpolation = container->interpolation;
2018 graphics->compmode = container->compmode;
2019 graphics->texthint = container->texthint;
2020 graphics->scale = container->scale;
2021 graphics->unit = container->unit;
2022 graphics->textcontrast = container->textcontrast;
2023 graphics->pixeloffset = container->pixeloffset;
2025 return Ok;
2028 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2030 RECT wnd_rect;
2031 GpStatus stat=Ok;
2032 GpUnit unit;
2034 if(graphics->hwnd) {
2035 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2036 return GenericError;
2038 rect->X = wnd_rect.left;
2039 rect->Y = wnd_rect.top;
2040 rect->Width = wnd_rect.right - wnd_rect.left;
2041 rect->Height = wnd_rect.bottom - wnd_rect.top;
2042 }else if (graphics->image){
2043 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2044 if (stat == Ok && unit != UnitPixel)
2045 FIXME("need to convert from unit %i\n", unit);
2046 }else{
2047 rect->X = 0;
2048 rect->Y = 0;
2049 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2050 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2053 return stat;
2056 /* on success, rgn will contain the region of the graphics object which
2057 * is visible after clipping has been applied */
2058 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2060 GpStatus stat;
2061 GpRectF rectf;
2062 GpRegion* tmp;
2064 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2065 return stat;
2067 if((stat = GdipCreateRegion(&tmp)) != Ok)
2068 return stat;
2070 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2071 goto end;
2073 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2074 goto end;
2076 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2078 end:
2079 GdipDeleteRegion(tmp);
2080 return stat;
2083 void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font, HFONT *hfont)
2085 HDC hdc = CreateCompatibleDC(0);
2086 GpPointF pt[3];
2087 REAL angle, rel_width, rel_height;
2088 LOGFONTW lfw;
2089 HFONT unscaled_font;
2090 TEXTMETRICW textmet;
2092 pt[0].X = 0.0;
2093 pt[0].Y = 0.0;
2094 pt[1].X = 1.0;
2095 pt[1].Y = 0.0;
2096 pt[2].X = 0.0;
2097 pt[2].Y = 1.0;
2098 if (graphics)
2099 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2100 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2101 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2102 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2103 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2104 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2106 lfw = font->lfw;
2107 lfw.lfHeight = roundr(-font->pixel_size * rel_height);
2108 unscaled_font = CreateFontIndirectW(&lfw);
2110 SelectObject(hdc, unscaled_font);
2111 GetTextMetricsW(hdc, &textmet);
2113 lfw = font->lfw;
2114 lfw.lfHeight = roundr(-font->pixel_size * rel_height);
2115 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width / rel_height);
2116 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2118 *hfont = CreateFontIndirectW(&lfw);
2120 DeleteDC(hdc);
2121 DeleteObject(unscaled_font);
2124 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2126 TRACE("(%p, %p)\n", hdc, graphics);
2128 return GdipCreateFromHDC2(hdc, NULL, graphics);
2131 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2133 GpStatus retval;
2135 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2137 if(hDevice != NULL) {
2138 FIXME("Don't know how to handle parameter hDevice\n");
2139 return NotImplemented;
2142 if(hdc == NULL)
2143 return OutOfMemory;
2145 if(graphics == NULL)
2146 return InvalidParameter;
2148 *graphics = GdipAlloc(sizeof(GpGraphics));
2149 if(!*graphics) return OutOfMemory;
2151 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2152 GdipFree(*graphics);
2153 return retval;
2156 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2157 GdipFree((*graphics)->worldtrans);
2158 GdipFree(*graphics);
2159 return retval;
2162 (*graphics)->hdc = hdc;
2163 (*graphics)->hwnd = WindowFromDC(hdc);
2164 (*graphics)->owndc = FALSE;
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 graphics_from_image(GpImage *image, GpGraphics **graphics)
2184 GpStatus retval;
2186 *graphics = GdipAlloc(sizeof(GpGraphics));
2187 if(!*graphics) return OutOfMemory;
2189 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2190 GdipFree(*graphics);
2191 return retval;
2194 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2195 GdipFree((*graphics)->worldtrans);
2196 GdipFree(*graphics);
2197 return retval;
2200 (*graphics)->hdc = NULL;
2201 (*graphics)->hwnd = NULL;
2202 (*graphics)->owndc = FALSE;
2203 (*graphics)->image = image;
2204 (*graphics)->smoothing = SmoothingModeDefault;
2205 (*graphics)->compqual = CompositingQualityDefault;
2206 (*graphics)->interpolation = InterpolationModeBilinear;
2207 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2208 (*graphics)->compmode = CompositingModeSourceOver;
2209 (*graphics)->unit = UnitDisplay;
2210 (*graphics)->scale = 1.0;
2211 (*graphics)->busy = FALSE;
2212 (*graphics)->textcontrast = 4;
2213 list_init(&(*graphics)->containers);
2214 (*graphics)->contid = 0;
2216 TRACE("<-- %p\n", *graphics);
2218 return Ok;
2221 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2223 GpStatus ret;
2224 HDC hdc;
2226 TRACE("(%p, %p)\n", hwnd, graphics);
2228 hdc = GetDC(hwnd);
2230 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2232 ReleaseDC(hwnd, hdc);
2233 return ret;
2236 (*graphics)->hwnd = hwnd;
2237 (*graphics)->owndc = TRUE;
2239 return Ok;
2242 /* FIXME: no icm handling */
2243 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2245 TRACE("(%p, %p)\n", hwnd, graphics);
2247 return GdipCreateFromHWND(hwnd, graphics);
2250 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2251 GpMetafile **metafile)
2253 IStream *stream = NULL;
2254 UINT read;
2255 ENHMETAHEADER *copy;
2256 GpStatus retval = Ok;
2258 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2260 if(!hemf || !metafile)
2261 return InvalidParameter;
2263 read = GetEnhMetaFileBits(hemf, 0, NULL);
2264 copy = GdipAlloc(read);
2265 GetEnhMetaFileBits(hemf, read, (BYTE *)copy);
2267 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
2268 ERR("could not make stream\n");
2269 GdipFree(copy);
2270 retval = GenericError;
2271 goto err;
2274 *metafile = GdipAlloc(sizeof(GpMetafile));
2275 if(!*metafile){
2276 retval = OutOfMemory;
2277 goto err;
2280 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2281 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
2283 retval = GenericError;
2284 goto err;
2288 (*metafile)->image.type = ImageTypeMetafile;
2289 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
2290 (*metafile)->image.palette_flags = 0;
2291 (*metafile)->image.palette_count = 0;
2292 (*metafile)->image.palette_size = 0;
2293 (*metafile)->image.palette_entries = NULL;
2294 (*metafile)->image.xres = (REAL)copy->szlDevice.cx;
2295 (*metafile)->image.yres = (REAL)copy->szlDevice.cy;
2296 (*metafile)->bounds.X = (REAL)copy->rclBounds.left;
2297 (*metafile)->bounds.Y = (REAL)copy->rclBounds.top;
2298 (*metafile)->bounds.Width = (REAL)(copy->rclBounds.right - copy->rclBounds.left);
2299 (*metafile)->bounds.Height = (REAL)(copy->rclBounds.bottom - copy->rclBounds.top);
2300 (*metafile)->unit = UnitPixel;
2302 if(delete)
2303 DeleteEnhMetaFile(hemf);
2305 TRACE("<-- %p\n", *metafile);
2307 err:
2308 if (retval != Ok)
2309 GdipFree(*metafile);
2310 IStream_Release(stream);
2311 return retval;
2314 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2315 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2317 UINT read;
2318 BYTE *copy;
2319 HENHMETAFILE hemf;
2320 GpStatus retval = Ok;
2322 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2324 if(!hwmf || !metafile || !placeable)
2325 return InvalidParameter;
2327 *metafile = NULL;
2328 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2329 if(!read)
2330 return GenericError;
2331 copy = GdipAlloc(read);
2332 GetMetaFileBitsEx(hwmf, read, copy);
2334 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2335 GdipFree(copy);
2337 retval = GdipCreateMetafileFromEmf(hemf, FALSE, metafile);
2339 if (retval == Ok)
2341 (*metafile)->image.xres = (REAL)placeable->Inch;
2342 (*metafile)->image.yres = (REAL)placeable->Inch;
2343 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2344 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2345 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2346 placeable->BoundingBox.Left);
2347 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2348 placeable->BoundingBox.Top);
2350 if (delete) DeleteMetaFile(hwmf);
2352 return retval;
2355 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2356 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2358 HMETAFILE hmf = GetMetaFileW(file);
2360 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2362 if(!hmf) return InvalidParameter;
2364 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2367 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2368 GpMetafile **metafile)
2370 FIXME("(%p, %p): stub\n", file, metafile);
2371 return NotImplemented;
2374 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2375 GpMetafile **metafile)
2377 FIXME("(%p, %p): stub\n", stream, metafile);
2378 return NotImplemented;
2381 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2382 UINT access, IStream **stream)
2384 DWORD dwMode;
2385 HRESULT ret;
2387 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2389 if(!stream || !filename)
2390 return InvalidParameter;
2392 if(access & GENERIC_WRITE)
2393 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2394 else if(access & GENERIC_READ)
2395 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2396 else
2397 return InvalidParameter;
2399 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2401 return hresult_to_status(ret);
2404 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2406 GraphicsContainerItem *cont, *next;
2407 GpStatus stat;
2408 TRACE("(%p)\n", graphics);
2410 if(!graphics) return InvalidParameter;
2411 if(graphics->busy) return ObjectBusy;
2413 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2415 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2416 if (stat != Ok)
2417 return stat;
2420 if(graphics->owndc)
2421 ReleaseDC(graphics->hwnd, graphics->hdc);
2423 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2424 list_remove(&cont->entry);
2425 delete_container(cont);
2428 GdipDeleteRegion(graphics->clip);
2429 GdipDeleteMatrix(graphics->worldtrans);
2430 GdipFree(graphics);
2432 return Ok;
2435 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2436 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2438 INT save_state, num_pts;
2439 GpPointF points[MAX_ARC_PTS];
2440 GpStatus retval;
2442 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2443 width, height, startAngle, sweepAngle);
2445 if(!graphics || !pen || width <= 0 || height <= 0)
2446 return InvalidParameter;
2448 if(graphics->busy)
2449 return ObjectBusy;
2451 if (!graphics->hdc)
2453 FIXME("graphics object has no HDC\n");
2454 return Ok;
2457 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2459 save_state = prepare_dc(graphics, pen);
2461 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2463 restore_dc(graphics, save_state);
2465 return retval;
2468 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2469 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2471 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2472 width, height, startAngle, sweepAngle);
2474 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2477 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2478 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2480 INT save_state;
2481 GpPointF pt[4];
2482 GpStatus retval;
2484 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2485 x2, y2, x3, y3, x4, y4);
2487 if(!graphics || !pen)
2488 return InvalidParameter;
2490 if(graphics->busy)
2491 return ObjectBusy;
2493 if (!graphics->hdc)
2495 FIXME("graphics object has no HDC\n");
2496 return Ok;
2499 pt[0].X = x1;
2500 pt[0].Y = y1;
2501 pt[1].X = x2;
2502 pt[1].Y = y2;
2503 pt[2].X = x3;
2504 pt[2].Y = y3;
2505 pt[3].X = x4;
2506 pt[3].Y = y4;
2508 save_state = prepare_dc(graphics, pen);
2510 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2512 restore_dc(graphics, save_state);
2514 return retval;
2517 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2518 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2520 INT save_state;
2521 GpPointF pt[4];
2522 GpStatus retval;
2524 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2525 x2, y2, x3, y3, x4, y4);
2527 if(!graphics || !pen)
2528 return InvalidParameter;
2530 if(graphics->busy)
2531 return ObjectBusy;
2533 if (!graphics->hdc)
2535 FIXME("graphics object has no HDC\n");
2536 return Ok;
2539 pt[0].X = x1;
2540 pt[0].Y = y1;
2541 pt[1].X = x2;
2542 pt[1].Y = y2;
2543 pt[2].X = x3;
2544 pt[2].Y = y3;
2545 pt[3].X = x4;
2546 pt[3].Y = y4;
2548 save_state = prepare_dc(graphics, pen);
2550 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2552 restore_dc(graphics, save_state);
2554 return retval;
2557 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2558 GDIPCONST GpPointF *points, INT count)
2560 INT i;
2561 GpStatus ret;
2563 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2565 if(!graphics || !pen || !points || (count <= 0))
2566 return InvalidParameter;
2568 if(graphics->busy)
2569 return ObjectBusy;
2571 for(i = 0; i < floor(count / 4); i++){
2572 ret = GdipDrawBezier(graphics, pen,
2573 points[4*i].X, points[4*i].Y,
2574 points[4*i + 1].X, points[4*i + 1].Y,
2575 points[4*i + 2].X, points[4*i + 2].Y,
2576 points[4*i + 3].X, points[4*i + 3].Y);
2577 if(ret != Ok)
2578 return ret;
2581 return Ok;
2584 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2585 GDIPCONST GpPoint *points, INT count)
2587 GpPointF *pts;
2588 GpStatus ret;
2589 INT i;
2591 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2593 if(!graphics || !pen || !points || (count <= 0))
2594 return InvalidParameter;
2596 if(graphics->busy)
2597 return ObjectBusy;
2599 pts = GdipAlloc(sizeof(GpPointF) * count);
2600 if(!pts)
2601 return OutOfMemory;
2603 for(i = 0; i < count; i++){
2604 pts[i].X = (REAL)points[i].X;
2605 pts[i].Y = (REAL)points[i].Y;
2608 ret = GdipDrawBeziers(graphics,pen,pts,count);
2610 GdipFree(pts);
2612 return ret;
2615 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2616 GDIPCONST GpPointF *points, INT count)
2618 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2620 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2623 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2624 GDIPCONST GpPoint *points, INT count)
2626 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2628 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2631 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2632 GDIPCONST GpPointF *points, INT count, REAL tension)
2634 GpPath *path;
2635 GpStatus stat;
2637 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2639 if(!graphics || !pen || !points || count <= 0)
2640 return InvalidParameter;
2642 if(graphics->busy)
2643 return ObjectBusy;
2645 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2646 return stat;
2648 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2649 if(stat != Ok){
2650 GdipDeletePath(path);
2651 return stat;
2654 stat = GdipDrawPath(graphics, pen, path);
2656 GdipDeletePath(path);
2658 return stat;
2661 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2662 GDIPCONST GpPoint *points, INT count, REAL tension)
2664 GpPointF *ptf;
2665 GpStatus stat;
2666 INT i;
2668 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2670 if(!points || count <= 0)
2671 return InvalidParameter;
2673 ptf = GdipAlloc(sizeof(GpPointF)*count);
2674 if(!ptf)
2675 return OutOfMemory;
2677 for(i = 0; i < count; i++){
2678 ptf[i].X = (REAL)points[i].X;
2679 ptf[i].Y = (REAL)points[i].Y;
2682 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2684 GdipFree(ptf);
2686 return stat;
2689 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2690 GDIPCONST GpPointF *points, INT count)
2692 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2694 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2697 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2698 GDIPCONST GpPoint *points, INT count)
2700 GpPointF *pointsF;
2701 GpStatus ret;
2702 INT i;
2704 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2706 if(!points)
2707 return InvalidParameter;
2709 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2710 if(!pointsF)
2711 return OutOfMemory;
2713 for(i = 0; i < count; i++){
2714 pointsF[i].X = (REAL)points[i].X;
2715 pointsF[i].Y = (REAL)points[i].Y;
2718 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2719 GdipFree(pointsF);
2721 return ret;
2724 /* Approximates cardinal spline with Bezier curves. */
2725 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2726 GDIPCONST GpPointF *points, INT count, REAL tension)
2728 /* PolyBezier expects count*3-2 points. */
2729 INT i, len_pt = count*3-2, save_state;
2730 GpPointF *pt;
2731 REAL x1, x2, y1, y2;
2732 GpStatus retval;
2734 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2736 if(!graphics || !pen)
2737 return InvalidParameter;
2739 if(graphics->busy)
2740 return ObjectBusy;
2742 if(count < 2)
2743 return InvalidParameter;
2745 if (!graphics->hdc)
2747 FIXME("graphics object has no HDC\n");
2748 return Ok;
2751 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2752 if(!pt)
2753 return OutOfMemory;
2755 tension = tension * TENSION_CONST;
2757 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2758 tension, &x1, &y1);
2760 pt[0].X = points[0].X;
2761 pt[0].Y = points[0].Y;
2762 pt[1].X = x1;
2763 pt[1].Y = y1;
2765 for(i = 0; i < count-2; i++){
2766 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2768 pt[3*i+2].X = x1;
2769 pt[3*i+2].Y = y1;
2770 pt[3*i+3].X = points[i+1].X;
2771 pt[3*i+3].Y = points[i+1].Y;
2772 pt[3*i+4].X = x2;
2773 pt[3*i+4].Y = y2;
2776 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2777 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2779 pt[len_pt-2].X = x1;
2780 pt[len_pt-2].Y = y1;
2781 pt[len_pt-1].X = points[count-1].X;
2782 pt[len_pt-1].Y = points[count-1].Y;
2784 save_state = prepare_dc(graphics, pen);
2786 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2788 GdipFree(pt);
2789 restore_dc(graphics, save_state);
2791 return retval;
2794 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2795 GDIPCONST GpPoint *points, INT count, REAL tension)
2797 GpPointF *pointsF;
2798 GpStatus ret;
2799 INT i;
2801 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2803 if(!points)
2804 return InvalidParameter;
2806 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2807 if(!pointsF)
2808 return OutOfMemory;
2810 for(i = 0; i < count; i++){
2811 pointsF[i].X = (REAL)points[i].X;
2812 pointsF[i].Y = (REAL)points[i].Y;
2815 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2816 GdipFree(pointsF);
2818 return ret;
2821 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2822 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2823 REAL tension)
2825 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2827 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2828 return InvalidParameter;
2831 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2834 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2835 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2836 REAL tension)
2838 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2840 if(count < 0){
2841 return OutOfMemory;
2844 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2845 return InvalidParameter;
2848 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2851 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2852 REAL y, REAL width, REAL height)
2854 INT save_state;
2855 GpPointF ptf[2];
2856 POINT pti[2];
2858 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2860 if(!graphics || !pen)
2861 return InvalidParameter;
2863 if(graphics->busy)
2864 return ObjectBusy;
2866 if (!graphics->hdc)
2868 FIXME("graphics object has no HDC\n");
2869 return Ok;
2872 ptf[0].X = x;
2873 ptf[0].Y = y;
2874 ptf[1].X = x + width;
2875 ptf[1].Y = y + height;
2877 save_state = prepare_dc(graphics, pen);
2878 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2880 transform_and_round_points(graphics, pti, ptf, 2);
2882 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2884 restore_dc(graphics, save_state);
2886 return Ok;
2889 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2890 INT y, INT width, INT height)
2892 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2894 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2898 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2900 UINT width, height;
2901 GpPointF points[3];
2903 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2905 if(!graphics || !image)
2906 return InvalidParameter;
2908 GdipGetImageWidth(image, &width);
2909 GdipGetImageHeight(image, &height);
2911 /* FIXME: we should use the graphics and image dpi, somehow */
2913 points[0].X = points[2].X = x;
2914 points[0].Y = points[1].Y = y;
2915 points[1].X = x + width;
2916 points[2].Y = y + height;
2918 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
2919 UnitPixel, NULL, NULL, NULL);
2922 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2923 INT y)
2925 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2927 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2930 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2931 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2932 GpUnit srcUnit)
2934 GpPointF points[3];
2935 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2937 points[0].X = points[2].X = x;
2938 points[0].Y = points[1].Y = y;
2940 /* FIXME: convert image coordinates to Graphics coordinates? */
2941 points[1].X = x + srcwidth;
2942 points[2].Y = y + srcheight;
2944 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2945 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2948 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2949 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2950 GpUnit srcUnit)
2952 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2955 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2956 GDIPCONST GpPointF *dstpoints, INT count)
2958 UINT width, height;
2960 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2962 if(!image)
2963 return InvalidParameter;
2965 GdipGetImageWidth(image, &width);
2966 GdipGetImageHeight(image, &height);
2968 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2969 width, height, UnitPixel, NULL, NULL, NULL);
2972 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2973 GDIPCONST GpPoint *dstpoints, INT count)
2975 GpPointF ptf[3];
2977 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2979 if (count != 3 || !dstpoints)
2980 return InvalidParameter;
2982 ptf[0].X = (REAL)dstpoints[0].X;
2983 ptf[0].Y = (REAL)dstpoints[0].Y;
2984 ptf[1].X = (REAL)dstpoints[1].X;
2985 ptf[1].Y = (REAL)dstpoints[1].Y;
2986 ptf[2].X = (REAL)dstpoints[2].X;
2987 ptf[2].Y = (REAL)dstpoints[2].Y;
2989 return GdipDrawImagePoints(graphics, image, ptf, count);
2992 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2993 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2994 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2995 DrawImageAbort callback, VOID * callbackData)
2997 GpPointF ptf[4];
2998 POINT pti[4];
2999 REAL dx, dy;
3000 GpStatus stat;
3002 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3003 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3004 callbackData);
3006 if (count > 3)
3007 return NotImplemented;
3009 if(!graphics || !image || !points || count != 3)
3010 return InvalidParameter;
3012 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3013 debugstr_pointf(&points[2]));
3015 memcpy(ptf, points, 3 * sizeof(GpPointF));
3016 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3017 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3018 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3019 return Ok;
3020 transform_and_round_points(graphics, pti, ptf, 4);
3022 if (image->picture)
3024 if (!graphics->hdc)
3026 FIXME("graphics object has no HDC\n");
3029 /* FIXME: partially implemented (only works for rectangular parallelograms) */
3030 if(srcUnit == UnitInch)
3031 dx = dy = (REAL) INCH_HIMETRIC;
3032 else if(srcUnit == UnitPixel){
3033 dx = ((REAL) INCH_HIMETRIC) /
3034 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
3035 dy = ((REAL) INCH_HIMETRIC) /
3036 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
3038 else
3039 return NotImplemented;
3041 if(IPicture_Render(image->picture, graphics->hdc,
3042 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3043 srcx * dx, srcy * dy,
3044 srcwidth * dx, srcheight * dy,
3045 NULL) != S_OK){
3046 if(callback)
3047 callback(callbackData);
3048 return GenericError;
3051 else if (image->type == ImageTypeBitmap)
3053 GpBitmap* bitmap = (GpBitmap*)image;
3054 int use_software=0;
3056 if (srcUnit == UnitInch)
3057 dx = dy = 96.0; /* FIXME: use the image resolution */
3058 else if (srcUnit == UnitPixel)
3059 dx = dy = 1.0;
3060 else
3061 return NotImplemented;
3063 srcx = srcx * dx;
3064 srcy = srcy * dy;
3065 srcwidth = srcwidth * dx;
3066 srcheight = srcheight * dy;
3068 if (imageAttributes ||
3069 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3070 !((GpBitmap*)image)->hbitmap ||
3071 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3072 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3073 srcx < 0 || srcy < 0 ||
3074 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3075 use_software = 1;
3077 if (use_software)
3079 RECT dst_area;
3080 GpRect src_area;
3081 int i, x, y, src_stride, dst_stride;
3082 GpMatrix *dst_to_src;
3083 REAL m11, m12, m21, m22, mdx, mdy;
3084 LPBYTE src_data, dst_data;
3085 BitmapData lockeddata;
3086 InterpolationMode interpolation = graphics->interpolation;
3087 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3088 REAL x_dx, x_dy, y_dx, y_dy;
3089 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3091 if (!imageAttributes)
3092 imageAttributes = &defaultImageAttributes;
3094 dst_area.left = dst_area.right = pti[0].x;
3095 dst_area.top = dst_area.bottom = pti[0].y;
3096 for (i=1; i<4; i++)
3098 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3099 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3100 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3101 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3104 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3105 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3106 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3107 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3108 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3109 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3111 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
3112 if (stat != Ok) return stat;
3114 stat = GdipInvertMatrix(dst_to_src);
3115 if (stat != Ok)
3117 GdipDeleteMatrix(dst_to_src);
3118 return stat;
3121 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3122 if (!dst_data)
3124 GdipDeleteMatrix(dst_to_src);
3125 return OutOfMemory;
3128 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3130 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3131 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3133 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3134 if (!src_data)
3136 GdipFree(dst_data);
3137 GdipDeleteMatrix(dst_to_src);
3138 return OutOfMemory;
3140 src_stride = sizeof(ARGB) * src_area.Width;
3142 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3143 lockeddata.Width = src_area.Width;
3144 lockeddata.Height = src_area.Height;
3145 lockeddata.Stride = src_stride;
3146 lockeddata.PixelFormat = PixelFormat32bppARGB;
3147 lockeddata.Scan0 = src_data;
3149 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3150 PixelFormat32bppARGB, &lockeddata);
3152 if (stat == Ok)
3153 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3155 if (stat != Ok)
3157 if (src_data != dst_data)
3158 GdipFree(src_data);
3159 GdipFree(dst_data);
3160 GdipDeleteMatrix(dst_to_src);
3161 return OutOfMemory;
3164 apply_image_attributes(imageAttributes, src_data,
3165 src_area.Width, src_area.Height,
3166 src_stride, ColorAdjustTypeBitmap);
3168 /* Transform the bits as needed to the destination. */
3169 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
3171 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3172 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3173 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3174 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3176 for (x=dst_area.left; x<dst_area.right; x++)
3178 for (y=dst_area.top; y<dst_area.bottom; y++)
3180 GpPointF src_pointf;
3181 ARGB *dst_color;
3183 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3184 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3186 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3188 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3189 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
3190 else
3191 *dst_color = 0;
3195 GdipDeleteMatrix(dst_to_src);
3197 GdipFree(src_data);
3199 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3200 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3202 GdipFree(dst_data);
3204 return stat;
3206 else
3208 HDC hdc;
3209 int temp_hdc=0, temp_bitmap=0;
3210 HBITMAP hbitmap, old_hbm=NULL;
3212 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3213 bitmap->format == PixelFormat24bppRGB ||
3214 bitmap->format == PixelFormat32bppRGB ||
3215 bitmap->format == PixelFormat32bppPARGB))
3217 BITMAPINFOHEADER bih;
3218 BYTE *temp_bits;
3219 PixelFormat dst_format;
3221 /* we can't draw a bitmap of this format directly */
3222 hdc = CreateCompatibleDC(0);
3223 temp_hdc = 1;
3224 temp_bitmap = 1;
3226 bih.biSize = sizeof(BITMAPINFOHEADER);
3227 bih.biWidth = bitmap->width;
3228 bih.biHeight = -bitmap->height;
3229 bih.biPlanes = 1;
3230 bih.biBitCount = 32;
3231 bih.biCompression = BI_RGB;
3232 bih.biSizeImage = 0;
3233 bih.biXPelsPerMeter = 0;
3234 bih.biYPelsPerMeter = 0;
3235 bih.biClrUsed = 0;
3236 bih.biClrImportant = 0;
3238 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3239 (void**)&temp_bits, NULL, 0);
3241 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3242 dst_format = PixelFormat32bppPARGB;
3243 else
3244 dst_format = PixelFormat32bppRGB;
3246 convert_pixels(bitmap->width, bitmap->height,
3247 bitmap->width*4, temp_bits, dst_format,
3248 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
3250 else
3252 hbitmap = bitmap->hbitmap;
3253 hdc = bitmap->hdc;
3254 temp_hdc = (hdc == 0);
3257 if (temp_hdc)
3259 if (!hdc) hdc = CreateCompatibleDC(0);
3260 old_hbm = SelectObject(hdc, hbitmap);
3263 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3265 BLENDFUNCTION bf;
3267 bf.BlendOp = AC_SRC_OVER;
3268 bf.BlendFlags = 0;
3269 bf.SourceConstantAlpha = 255;
3270 bf.AlphaFormat = AC_SRC_ALPHA;
3272 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3273 hdc, srcx, srcy, srcwidth, srcheight, bf);
3275 else
3277 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3278 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3281 if (temp_hdc)
3283 SelectObject(hdc, old_hbm);
3284 DeleteDC(hdc);
3287 if (temp_bitmap)
3288 DeleteObject(hbitmap);
3291 else
3293 ERR("GpImage with no IPicture or HBITMAP?!\n");
3294 return NotImplemented;
3297 return Ok;
3300 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3301 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3302 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3303 DrawImageAbort callback, VOID * callbackData)
3305 GpPointF pointsF[3];
3306 INT i;
3308 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3309 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3310 callbackData);
3312 if(!points || count!=3)
3313 return InvalidParameter;
3315 for(i = 0; i < count; i++){
3316 pointsF[i].X = (REAL)points[i].X;
3317 pointsF[i].Y = (REAL)points[i].Y;
3320 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3321 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3322 callback, callbackData);
3325 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3326 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3327 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3328 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3329 VOID * callbackData)
3331 GpPointF points[3];
3333 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3334 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3335 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3337 points[0].X = dstx;
3338 points[0].Y = dsty;
3339 points[1].X = dstx + dstwidth;
3340 points[1].Y = dsty;
3341 points[2].X = dstx;
3342 points[2].Y = dsty + dstheight;
3344 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3345 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3348 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3349 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3350 INT srcwidth, INT srcheight, GpUnit srcUnit,
3351 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3352 VOID * callbackData)
3354 GpPointF points[3];
3356 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3357 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3358 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3360 points[0].X = dstx;
3361 points[0].Y = dsty;
3362 points[1].X = dstx + dstwidth;
3363 points[1].Y = dsty;
3364 points[2].X = dstx;
3365 points[2].Y = dsty + dstheight;
3367 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3368 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3371 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3372 REAL x, REAL y, REAL width, REAL height)
3374 RectF bounds;
3375 GpUnit unit;
3376 GpStatus ret;
3378 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3380 if(!graphics || !image)
3381 return InvalidParameter;
3383 ret = GdipGetImageBounds(image, &bounds, &unit);
3384 if(ret != Ok)
3385 return ret;
3387 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3388 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3389 unit, NULL, NULL, NULL);
3392 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3393 INT x, INT y, INT width, INT height)
3395 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3397 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3400 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3401 REAL y1, REAL x2, REAL y2)
3403 INT save_state;
3404 GpPointF pt[2];
3405 GpStatus retval;
3407 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3409 if(!pen || !graphics)
3410 return InvalidParameter;
3412 if(graphics->busy)
3413 return ObjectBusy;
3415 if (!graphics->hdc)
3417 FIXME("graphics object has no HDC\n");
3418 return Ok;
3421 pt[0].X = x1;
3422 pt[0].Y = y1;
3423 pt[1].X = x2;
3424 pt[1].Y = y2;
3426 save_state = prepare_dc(graphics, pen);
3428 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3430 restore_dc(graphics, save_state);
3432 return retval;
3435 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3436 INT y1, INT x2, INT y2)
3438 INT save_state;
3439 GpPointF pt[2];
3440 GpStatus retval;
3442 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3444 if(!pen || !graphics)
3445 return InvalidParameter;
3447 if(graphics->busy)
3448 return ObjectBusy;
3450 if (!graphics->hdc)
3452 FIXME("graphics object has no HDC\n");
3453 return Ok;
3456 pt[0].X = (REAL)x1;
3457 pt[0].Y = (REAL)y1;
3458 pt[1].X = (REAL)x2;
3459 pt[1].Y = (REAL)y2;
3461 save_state = prepare_dc(graphics, pen);
3463 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3465 restore_dc(graphics, save_state);
3467 return retval;
3470 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3471 GpPointF *points, INT count)
3473 INT save_state;
3474 GpStatus retval;
3476 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3478 if(!pen || !graphics || (count < 2))
3479 return InvalidParameter;
3481 if(graphics->busy)
3482 return ObjectBusy;
3484 if (!graphics->hdc)
3486 FIXME("graphics object has no HDC\n");
3487 return Ok;
3490 save_state = prepare_dc(graphics, pen);
3492 retval = draw_polyline(graphics, pen, points, count, TRUE);
3494 restore_dc(graphics, save_state);
3496 return retval;
3499 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3500 GpPoint *points, INT count)
3502 INT save_state;
3503 GpStatus retval;
3504 GpPointF *ptf = NULL;
3505 int i;
3507 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3509 if(!pen || !graphics || (count < 2))
3510 return InvalidParameter;
3512 if(graphics->busy)
3513 return ObjectBusy;
3515 if (!graphics->hdc)
3517 FIXME("graphics object has no HDC\n");
3518 return Ok;
3521 ptf = GdipAlloc(count * sizeof(GpPointF));
3522 if(!ptf) return OutOfMemory;
3524 for(i = 0; i < count; i ++){
3525 ptf[i].X = (REAL) points[i].X;
3526 ptf[i].Y = (REAL) points[i].Y;
3529 save_state = prepare_dc(graphics, pen);
3531 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3533 restore_dc(graphics, save_state);
3535 GdipFree(ptf);
3536 return retval;
3539 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3541 INT save_state;
3542 GpStatus retval;
3544 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3546 if(!pen || !graphics)
3547 return InvalidParameter;
3549 if(graphics->busy)
3550 return ObjectBusy;
3552 if (!graphics->hdc)
3554 FIXME("graphics object has no HDC\n");
3555 return Ok;
3558 save_state = prepare_dc(graphics, pen);
3560 retval = draw_poly(graphics, pen, path->pathdata.Points,
3561 path->pathdata.Types, path->pathdata.Count, TRUE);
3563 restore_dc(graphics, save_state);
3565 return retval;
3568 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3569 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3571 INT save_state;
3573 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3574 width, height, startAngle, sweepAngle);
3576 if(!graphics || !pen)
3577 return InvalidParameter;
3579 if(graphics->busy)
3580 return ObjectBusy;
3582 if (!graphics->hdc)
3584 FIXME("graphics object has no HDC\n");
3585 return Ok;
3588 save_state = prepare_dc(graphics, pen);
3589 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3591 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3593 restore_dc(graphics, save_state);
3595 return Ok;
3598 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3599 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3601 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3602 width, height, startAngle, sweepAngle);
3604 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3607 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3608 REAL y, REAL width, REAL height)
3610 INT save_state;
3611 GpPointF ptf[4];
3612 POINT pti[4];
3614 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3616 if(!pen || !graphics)
3617 return InvalidParameter;
3619 if(graphics->busy)
3620 return ObjectBusy;
3622 if (!graphics->hdc)
3624 FIXME("graphics object has no HDC\n");
3625 return Ok;
3628 ptf[0].X = x;
3629 ptf[0].Y = y;
3630 ptf[1].X = x + width;
3631 ptf[1].Y = y;
3632 ptf[2].X = x + width;
3633 ptf[2].Y = y + height;
3634 ptf[3].X = x;
3635 ptf[3].Y = y + height;
3637 save_state = prepare_dc(graphics, pen);
3638 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3640 transform_and_round_points(graphics, pti, ptf, 4);
3641 Polygon(graphics->hdc, pti, 4);
3643 restore_dc(graphics, save_state);
3645 return Ok;
3648 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3649 INT y, INT width, INT height)
3651 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3653 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3656 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3657 GDIPCONST GpRectF* rects, INT count)
3659 GpPointF *ptf;
3660 POINT *pti;
3661 INT save_state, i;
3663 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3665 if(!graphics || !pen || !rects || count < 1)
3666 return InvalidParameter;
3668 if(graphics->busy)
3669 return ObjectBusy;
3671 if (!graphics->hdc)
3673 FIXME("graphics object has no HDC\n");
3674 return Ok;
3677 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3678 pti = GdipAlloc(4 * count * sizeof(POINT));
3680 if(!ptf || !pti){
3681 GdipFree(ptf);
3682 GdipFree(pti);
3683 return OutOfMemory;
3686 for(i = 0; i < count; i++){
3687 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3688 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3689 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3690 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3693 save_state = prepare_dc(graphics, pen);
3694 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3696 transform_and_round_points(graphics, pti, ptf, 4 * count);
3698 for(i = 0; i < count; i++)
3699 Polygon(graphics->hdc, &pti[4 * i], 4);
3701 restore_dc(graphics, save_state);
3703 GdipFree(ptf);
3704 GdipFree(pti);
3706 return Ok;
3709 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3710 GDIPCONST GpRect* rects, INT count)
3712 GpRectF *rectsF;
3713 GpStatus ret;
3714 INT i;
3716 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3718 if(!rects || count<=0)
3719 return InvalidParameter;
3721 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3722 if(!rectsF)
3723 return OutOfMemory;
3725 for(i = 0;i < count;i++){
3726 rectsF[i].X = (REAL)rects[i].X;
3727 rectsF[i].Y = (REAL)rects[i].Y;
3728 rectsF[i].Width = (REAL)rects[i].Width;
3729 rectsF[i].Height = (REAL)rects[i].Height;
3732 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3733 GdipFree(rectsF);
3735 return ret;
3738 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3739 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3741 GpPath *path;
3742 GpStatus stat;
3744 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3745 count, tension, fill);
3747 if(!graphics || !brush || !points)
3748 return InvalidParameter;
3750 if(graphics->busy)
3751 return ObjectBusy;
3753 if(count == 1) /* Do nothing */
3754 return Ok;
3756 stat = GdipCreatePath(fill, &path);
3757 if(stat != Ok)
3758 return stat;
3760 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3761 if(stat != Ok){
3762 GdipDeletePath(path);
3763 return stat;
3766 stat = GdipFillPath(graphics, brush, path);
3767 if(stat != Ok){
3768 GdipDeletePath(path);
3769 return stat;
3772 GdipDeletePath(path);
3774 return Ok;
3777 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3778 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3780 GpPointF *ptf;
3781 GpStatus stat;
3782 INT i;
3784 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3785 count, tension, fill);
3787 if(!points || count == 0)
3788 return InvalidParameter;
3790 if(count == 1) /* Do nothing */
3791 return Ok;
3793 ptf = GdipAlloc(sizeof(GpPointF)*count);
3794 if(!ptf)
3795 return OutOfMemory;
3797 for(i = 0;i < count;i++){
3798 ptf[i].X = (REAL)points[i].X;
3799 ptf[i].Y = (REAL)points[i].Y;
3802 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3804 GdipFree(ptf);
3806 return stat;
3809 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3810 GDIPCONST GpPointF *points, INT count)
3812 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3813 return GdipFillClosedCurve2(graphics, brush, points, count,
3814 0.5f, FillModeAlternate);
3817 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3818 GDIPCONST GpPoint *points, INT count)
3820 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3821 return GdipFillClosedCurve2I(graphics, brush, points, count,
3822 0.5f, FillModeAlternate);
3825 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3826 REAL y, REAL width, REAL height)
3828 GpStatus stat;
3829 GpPath *path;
3831 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3833 if(!graphics || !brush)
3834 return InvalidParameter;
3836 if(graphics->busy)
3837 return ObjectBusy;
3839 stat = GdipCreatePath(FillModeAlternate, &path);
3841 if (stat == Ok)
3843 stat = GdipAddPathEllipse(path, x, y, width, height);
3845 if (stat == Ok)
3846 stat = GdipFillPath(graphics, brush, path);
3848 GdipDeletePath(path);
3851 return stat;
3854 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3855 INT y, INT width, INT height)
3857 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3859 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3862 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3864 INT save_state;
3865 GpStatus retval;
3867 if(!graphics->hdc || !brush_can_fill_path(brush))
3868 return NotImplemented;
3870 save_state = SaveDC(graphics->hdc);
3871 EndPath(graphics->hdc);
3872 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3873 : WINDING));
3875 BeginPath(graphics->hdc);
3876 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3877 path->pathdata.Types, path->pathdata.Count, FALSE);
3879 if(retval != Ok)
3880 goto end;
3882 EndPath(graphics->hdc);
3883 brush_fill_path(graphics, brush);
3885 retval = Ok;
3887 end:
3888 RestoreDC(graphics->hdc, save_state);
3890 return retval;
3893 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3895 GpStatus stat;
3896 GpRegion *rgn;
3898 if (!brush_can_fill_pixels(brush))
3899 return NotImplemented;
3901 /* FIXME: This could probably be done more efficiently without regions. */
3903 stat = GdipCreateRegionPath(path, &rgn);
3905 if (stat == Ok)
3907 stat = GdipFillRegion(graphics, brush, rgn);
3909 GdipDeleteRegion(rgn);
3912 return stat;
3915 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3917 GpStatus stat = NotImplemented;
3919 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3921 if(!brush || !graphics || !path)
3922 return InvalidParameter;
3924 if(graphics->busy)
3925 return ObjectBusy;
3927 if (!graphics->image)
3928 stat = GDI32_GdipFillPath(graphics, brush, path);
3930 if (stat == NotImplemented)
3931 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3933 if (stat == NotImplemented)
3935 FIXME("Not implemented for brushtype %i\n", brush->bt);
3936 stat = Ok;
3939 return stat;
3942 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3943 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3945 GpStatus stat;
3946 GpPath *path;
3948 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3949 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3951 if(!graphics || !brush)
3952 return InvalidParameter;
3954 if(graphics->busy)
3955 return ObjectBusy;
3957 stat = GdipCreatePath(FillModeAlternate, &path);
3959 if (stat == Ok)
3961 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3963 if (stat == Ok)
3964 stat = GdipFillPath(graphics, brush, path);
3966 GdipDeletePath(path);
3969 return stat;
3972 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3973 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3975 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3976 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3978 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3981 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3982 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3984 GpStatus stat;
3985 GpPath *path;
3987 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3989 if(!graphics || !brush || !points || !count)
3990 return InvalidParameter;
3992 if(graphics->busy)
3993 return ObjectBusy;
3995 stat = GdipCreatePath(fillMode, &path);
3997 if (stat == Ok)
3999 stat = GdipAddPathPolygon(path, points, count);
4001 if (stat == Ok)
4002 stat = GdipFillPath(graphics, brush, path);
4004 GdipDeletePath(path);
4007 return stat;
4010 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4011 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4013 GpStatus stat;
4014 GpPath *path;
4016 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4018 if(!graphics || !brush || !points || !count)
4019 return InvalidParameter;
4021 if(graphics->busy)
4022 return ObjectBusy;
4024 stat = GdipCreatePath(fillMode, &path);
4026 if (stat == Ok)
4028 stat = GdipAddPathPolygonI(path, points, count);
4030 if (stat == Ok)
4031 stat = GdipFillPath(graphics, brush, path);
4033 GdipDeletePath(path);
4036 return stat;
4039 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4040 GDIPCONST GpPointF *points, INT count)
4042 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4044 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4047 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4048 GDIPCONST GpPoint *points, INT count)
4050 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4052 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4055 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4056 REAL x, REAL y, REAL width, REAL height)
4058 GpStatus stat;
4059 GpPath *path;
4061 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4063 if(!graphics || !brush)
4064 return InvalidParameter;
4066 if(graphics->busy)
4067 return ObjectBusy;
4069 stat = GdipCreatePath(FillModeAlternate, &path);
4071 if (stat == Ok)
4073 stat = GdipAddPathRectangle(path, x, y, width, height);
4075 if (stat == Ok)
4076 stat = GdipFillPath(graphics, brush, path);
4078 GdipDeletePath(path);
4081 return stat;
4084 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4085 INT x, INT y, INT width, INT height)
4087 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4089 return GdipFillRectangle(graphics, brush, x, y, width, height);
4092 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4093 INT count)
4095 GpStatus ret;
4096 INT i;
4098 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4100 if(!rects)
4101 return InvalidParameter;
4103 for(i = 0; i < count; i++){
4104 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4105 if(ret != Ok) return ret;
4108 return Ok;
4111 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4112 INT count)
4114 GpRectF *rectsF;
4115 GpStatus ret;
4116 INT i;
4118 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4120 if(!rects || count <= 0)
4121 return InvalidParameter;
4123 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4124 if(!rectsF)
4125 return OutOfMemory;
4127 for(i = 0; i < count; i++){
4128 rectsF[i].X = (REAL)rects[i].X;
4129 rectsF[i].Y = (REAL)rects[i].Y;
4130 rectsF[i].X = (REAL)rects[i].Width;
4131 rectsF[i].Height = (REAL)rects[i].Height;
4134 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4135 GdipFree(rectsF);
4137 return ret;
4140 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4141 GpRegion* region)
4143 INT save_state;
4144 GpStatus status;
4145 HRGN hrgn;
4146 RECT rc;
4148 if(!graphics->hdc || !brush_can_fill_path(brush))
4149 return NotImplemented;
4151 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4152 if(status != Ok)
4153 return status;
4155 save_state = SaveDC(graphics->hdc);
4156 EndPath(graphics->hdc);
4158 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4160 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4162 BeginPath(graphics->hdc);
4163 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4164 EndPath(graphics->hdc);
4166 brush_fill_path(graphics, brush);
4169 RestoreDC(graphics->hdc, save_state);
4171 DeleteObject(hrgn);
4173 return Ok;
4176 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4177 GpRegion* region)
4179 GpStatus stat;
4180 GpRegion *temp_region;
4181 GpMatrix *world_to_device;
4182 GpRectF graphics_bounds;
4183 DWORD *pixel_data;
4184 HRGN hregion;
4185 RECT bound_rect;
4186 GpRect gp_bound_rect;
4188 if (!brush_can_fill_pixels(brush))
4189 return NotImplemented;
4191 stat = get_graphics_bounds(graphics, &graphics_bounds);
4193 if (stat == Ok)
4194 stat = GdipCloneRegion(region, &temp_region);
4196 if (stat == Ok)
4198 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4199 CoordinateSpaceWorld, &world_to_device);
4201 if (stat == Ok)
4203 stat = GdipTransformRegion(temp_region, world_to_device);
4205 GdipDeleteMatrix(world_to_device);
4208 if (stat == Ok)
4209 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4211 if (stat == Ok)
4212 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4214 GdipDeleteRegion(temp_region);
4217 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4219 DeleteObject(hregion);
4220 return Ok;
4223 if (stat == Ok)
4225 gp_bound_rect.X = bound_rect.left;
4226 gp_bound_rect.Y = bound_rect.top;
4227 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4228 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4230 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4231 if (!pixel_data)
4232 stat = OutOfMemory;
4234 if (stat == Ok)
4236 stat = brush_fill_pixels(graphics, brush, pixel_data,
4237 &gp_bound_rect, gp_bound_rect.Width);
4239 if (stat == Ok)
4240 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4241 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4242 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4244 GdipFree(pixel_data);
4247 DeleteObject(hregion);
4250 return stat;
4253 /*****************************************************************************
4254 * GdipFillRegion [GDIPLUS.@]
4256 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4257 GpRegion* region)
4259 GpStatus stat = NotImplemented;
4261 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4263 if (!(graphics && brush && region))
4264 return InvalidParameter;
4266 if(graphics->busy)
4267 return ObjectBusy;
4269 if (!graphics->image)
4270 stat = GDI32_GdipFillRegion(graphics, brush, region);
4272 if (stat == NotImplemented)
4273 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4275 if (stat == NotImplemented)
4277 FIXME("not implemented for brushtype %i\n", brush->bt);
4278 stat = Ok;
4281 return stat;
4284 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4286 TRACE("(%p,%u)\n", graphics, intention);
4288 if(!graphics)
4289 return InvalidParameter;
4291 if(graphics->busy)
4292 return ObjectBusy;
4294 /* We have no internal operation queue, so there's no need to clear it. */
4296 if (graphics->hdc)
4297 GdiFlush();
4299 return Ok;
4302 /*****************************************************************************
4303 * GdipGetClipBounds [GDIPLUS.@]
4305 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4307 TRACE("(%p, %p)\n", graphics, rect);
4309 if(!graphics)
4310 return InvalidParameter;
4312 if(graphics->busy)
4313 return ObjectBusy;
4315 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4318 /*****************************************************************************
4319 * GdipGetClipBoundsI [GDIPLUS.@]
4321 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4323 TRACE("(%p, %p)\n", graphics, rect);
4325 if(!graphics)
4326 return InvalidParameter;
4328 if(graphics->busy)
4329 return ObjectBusy;
4331 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4334 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4335 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4336 CompositingMode *mode)
4338 TRACE("(%p, %p)\n", graphics, mode);
4340 if(!graphics || !mode)
4341 return InvalidParameter;
4343 if(graphics->busy)
4344 return ObjectBusy;
4346 *mode = graphics->compmode;
4348 return Ok;
4351 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4352 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4353 CompositingQuality *quality)
4355 TRACE("(%p, %p)\n", graphics, quality);
4357 if(!graphics || !quality)
4358 return InvalidParameter;
4360 if(graphics->busy)
4361 return ObjectBusy;
4363 *quality = graphics->compqual;
4365 return Ok;
4368 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4369 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4370 InterpolationMode *mode)
4372 TRACE("(%p, %p)\n", graphics, mode);
4374 if(!graphics || !mode)
4375 return InvalidParameter;
4377 if(graphics->busy)
4378 return ObjectBusy;
4380 *mode = graphics->interpolation;
4382 return Ok;
4385 /* FIXME: Need to handle color depths less than 24bpp */
4386 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4388 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4390 if(!graphics || !argb)
4391 return InvalidParameter;
4393 if(graphics->busy)
4394 return ObjectBusy;
4396 return Ok;
4399 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4401 TRACE("(%p, %p)\n", graphics, scale);
4403 if(!graphics || !scale)
4404 return InvalidParameter;
4406 if(graphics->busy)
4407 return ObjectBusy;
4409 *scale = graphics->scale;
4411 return Ok;
4414 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4416 TRACE("(%p, %p)\n", graphics, unit);
4418 if(!graphics || !unit)
4419 return InvalidParameter;
4421 if(graphics->busy)
4422 return ObjectBusy;
4424 *unit = graphics->unit;
4426 return Ok;
4429 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4430 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4431 *mode)
4433 TRACE("(%p, %p)\n", graphics, mode);
4435 if(!graphics || !mode)
4436 return InvalidParameter;
4438 if(graphics->busy)
4439 return ObjectBusy;
4441 *mode = graphics->pixeloffset;
4443 return Ok;
4446 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4447 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4449 TRACE("(%p, %p)\n", graphics, mode);
4451 if(!graphics || !mode)
4452 return InvalidParameter;
4454 if(graphics->busy)
4455 return ObjectBusy;
4457 *mode = graphics->smoothing;
4459 return Ok;
4462 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4464 TRACE("(%p, %p)\n", graphics, contrast);
4466 if(!graphics || !contrast)
4467 return InvalidParameter;
4469 *contrast = graphics->textcontrast;
4471 return Ok;
4474 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4475 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4476 TextRenderingHint *hint)
4478 TRACE("(%p, %p)\n", graphics, hint);
4480 if(!graphics || !hint)
4481 return InvalidParameter;
4483 if(graphics->busy)
4484 return ObjectBusy;
4486 *hint = graphics->texthint;
4488 return Ok;
4491 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4493 GpRegion *clip_rgn;
4494 GpStatus stat;
4496 TRACE("(%p, %p)\n", graphics, rect);
4498 if(!graphics || !rect)
4499 return InvalidParameter;
4501 if(graphics->busy)
4502 return ObjectBusy;
4504 /* intersect window and graphics clipping regions */
4505 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4506 return stat;
4508 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4509 goto cleanup;
4511 /* get bounds of the region */
4512 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4514 cleanup:
4515 GdipDeleteRegion(clip_rgn);
4517 return stat;
4520 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4522 GpRectF rectf;
4523 GpStatus stat;
4525 TRACE("(%p, %p)\n", graphics, rect);
4527 if(!graphics || !rect)
4528 return InvalidParameter;
4530 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4532 rect->X = roundr(rectf.X);
4533 rect->Y = roundr(rectf.Y);
4534 rect->Width = roundr(rectf.Width);
4535 rect->Height = roundr(rectf.Height);
4538 return stat;
4541 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4543 TRACE("(%p, %p)\n", graphics, matrix);
4545 if(!graphics || !matrix)
4546 return InvalidParameter;
4548 if(graphics->busy)
4549 return ObjectBusy;
4551 *matrix = *graphics->worldtrans;
4552 return Ok;
4555 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4557 GpSolidFill *brush;
4558 GpStatus stat;
4559 GpRectF wnd_rect;
4561 TRACE("(%p, %x)\n", graphics, color);
4563 if(!graphics)
4564 return InvalidParameter;
4566 if(graphics->busy)
4567 return ObjectBusy;
4569 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4570 return stat;
4572 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4573 GdipDeleteBrush((GpBrush*)brush);
4574 return stat;
4577 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4578 wnd_rect.Width, wnd_rect.Height);
4580 GdipDeleteBrush((GpBrush*)brush);
4582 return Ok;
4585 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4587 TRACE("(%p, %p)\n", graphics, res);
4589 if(!graphics || !res)
4590 return InvalidParameter;
4592 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4595 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4597 GpStatus stat;
4598 GpRegion* rgn;
4599 GpPointF pt;
4601 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4603 if(!graphics || !result)
4604 return InvalidParameter;
4606 if(graphics->busy)
4607 return ObjectBusy;
4609 pt.X = x;
4610 pt.Y = y;
4611 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4612 CoordinateSpaceWorld, &pt, 1)) != Ok)
4613 return stat;
4615 if((stat = GdipCreateRegion(&rgn)) != Ok)
4616 return stat;
4618 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4619 goto cleanup;
4621 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4623 cleanup:
4624 GdipDeleteRegion(rgn);
4625 return stat;
4628 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4630 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4633 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4635 GpStatus stat;
4636 GpRegion* rgn;
4637 GpPointF pts[2];
4639 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4641 if(!graphics || !result)
4642 return InvalidParameter;
4644 if(graphics->busy)
4645 return ObjectBusy;
4647 pts[0].X = x;
4648 pts[0].Y = y;
4649 pts[1].X = x + width;
4650 pts[1].Y = y + height;
4652 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4653 CoordinateSpaceWorld, pts, 2)) != Ok)
4654 return stat;
4656 pts[1].X -= pts[0].X;
4657 pts[1].Y -= pts[0].Y;
4659 if((stat = GdipCreateRegion(&rgn)) != Ok)
4660 return stat;
4662 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4663 goto cleanup;
4665 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4667 cleanup:
4668 GdipDeleteRegion(rgn);
4669 return stat;
4672 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4674 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4677 GpStatus gdip_format_string(HDC hdc,
4678 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4679 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4680 gdip_format_string_callback callback, void *user_data)
4682 WCHAR* stringdup;
4683 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4684 nheight, lineend, lineno = 0;
4685 RectF bounds;
4686 StringAlignment halign;
4687 GpStatus stat = Ok;
4688 SIZE size;
4689 HotkeyPrefix hkprefix;
4690 INT *hotkeyprefix_offsets=NULL;
4691 INT hotkeyprefix_count=0;
4692 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4693 int seen_prefix=0;
4695 if(length == -1) length = lstrlenW(string);
4697 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4698 if(!stringdup) return OutOfMemory;
4700 nwidth = roundr(rect->Width);
4701 nheight = roundr(rect->Height);
4703 if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
4704 if (rect->Height >= INT_MAX || rect->Height < 0.5) nheight = INT_MAX;
4706 if (format)
4707 hkprefix = format->hkprefix;
4708 else
4709 hkprefix = HotkeyPrefixNone;
4711 if (hkprefix == HotkeyPrefixShow)
4713 for (i=0; i<length; i++)
4715 if (string[i] == '&')
4716 hotkeyprefix_count++;
4720 if (hotkeyprefix_count)
4721 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4723 hotkeyprefix_count = 0;
4725 for(i = 0, j = 0; i < length; i++){
4726 /* FIXME: This makes the indexes passed to callback inaccurate. */
4727 if(!isprintW(string[i]) && (string[i] != '\n'))
4728 continue;
4730 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4731 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4732 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4734 seen_prefix = 1;
4735 continue;
4738 seen_prefix = 0;
4740 stringdup[j] = string[i];
4741 j++;
4744 length = j;
4746 if (format) halign = format->align;
4747 else halign = StringAlignmentNear;
4749 while(sum < length){
4750 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4751 nwidth, &fit, NULL, &size);
4752 fitcpy = fit;
4754 if(fit == 0)
4755 break;
4757 for(lret = 0; lret < fit; lret++)
4758 if(*(stringdup + sum + lret) == '\n')
4759 break;
4761 /* Line break code (may look strange, but it imitates windows). */
4762 if(lret < fit)
4763 lineend = fit = lret; /* this is not an off-by-one error */
4764 else if(fit < (length - sum)){
4765 if(*(stringdup + sum + fit) == ' ')
4766 while(*(stringdup + sum + fit) == ' ')
4767 fit++;
4768 else
4769 while(*(stringdup + sum + fit - 1) != ' '){
4770 fit--;
4772 if(*(stringdup + sum + fit) == '\t')
4773 break;
4775 if(fit == 0){
4776 fit = fitcpy;
4777 break;
4780 lineend = fit;
4781 while(*(stringdup + sum + lineend - 1) == ' ' ||
4782 *(stringdup + sum + lineend - 1) == '\t')
4783 lineend--;
4785 else
4786 lineend = fit;
4788 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4789 nwidth, &j, NULL, &size);
4791 bounds.Width = size.cx;
4793 if(height + size.cy > nheight)
4794 bounds.Height = nheight - (height + size.cy);
4795 else
4796 bounds.Height = size.cy;
4798 bounds.Y = rect->Y + height;
4800 switch (halign)
4802 case StringAlignmentNear:
4803 default:
4804 bounds.X = rect->X;
4805 break;
4806 case StringAlignmentCenter:
4807 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4808 break;
4809 case StringAlignmentFar:
4810 bounds.X = rect->X + rect->Width - bounds.Width;
4811 break;
4814 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4815 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4816 break;
4818 stat = callback(hdc, stringdup, sum, lineend,
4819 font, rect, format, lineno, &bounds,
4820 &hotkeyprefix_offsets[hotkeyprefix_pos],
4821 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4823 if (stat != Ok)
4824 break;
4826 sum += fit + (lret < fitcpy ? 1 : 0);
4827 height += size.cy;
4828 lineno++;
4830 hotkeyprefix_pos = hotkeyprefix_end_pos;
4832 if(height > nheight)
4833 break;
4835 /* Stop if this was a linewrap (but not if it was a linebreak). */
4836 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4837 break;
4840 GdipFree(stringdup);
4841 GdipFree(hotkeyprefix_offsets);
4843 return stat;
4846 struct measure_ranges_args {
4847 GpRegion **regions;
4850 static GpStatus measure_ranges_callback(HDC hdc,
4851 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4852 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4853 INT lineno, const RectF *bounds, INT *underlined_indexes,
4854 INT underlined_index_count, void *user_data)
4856 int i;
4857 GpStatus stat = Ok;
4858 struct measure_ranges_args *args = user_data;
4860 for (i=0; i<format->range_count; i++)
4862 INT range_start = max(index, format->character_ranges[i].First);
4863 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4864 if (range_start < range_end)
4866 GpRectF range_rect;
4867 SIZE range_size;
4869 range_rect.Y = bounds->Y;
4870 range_rect.Height = bounds->Height;
4872 GetTextExtentExPointW(hdc, string + index, range_start - index,
4873 INT_MAX, NULL, NULL, &range_size);
4874 range_rect.X = bounds->X + range_size.cx;
4876 GetTextExtentExPointW(hdc, string + index, range_end - index,
4877 INT_MAX, NULL, NULL, &range_size);
4878 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
4880 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4881 if (stat != Ok)
4882 break;
4886 return stat;
4889 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4890 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4891 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4892 INT regionCount, GpRegion** regions)
4894 GpStatus stat;
4895 int i;
4896 HFONT oldfont;
4897 struct measure_ranges_args args;
4898 HDC hdc, temp_hdc=NULL;
4900 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4901 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4903 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4904 return InvalidParameter;
4906 if (regionCount < stringFormat->range_count)
4907 return InvalidParameter;
4909 if(!graphics->hdc)
4911 hdc = temp_hdc = CreateCompatibleDC(0);
4912 if (!temp_hdc) return OutOfMemory;
4914 else
4915 hdc = graphics->hdc;
4917 if (stringFormat->attr)
4918 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4920 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4922 for (i=0; i<stringFormat->range_count; i++)
4924 stat = GdipSetEmpty(regions[i]);
4925 if (stat != Ok)
4926 return stat;
4929 args.regions = regions;
4931 stat = gdip_format_string(hdc, string, length, font, layoutRect, stringFormat,
4932 measure_ranges_callback, &args);
4934 DeleteObject(SelectObject(hdc, oldfont));
4936 if (temp_hdc)
4937 DeleteDC(temp_hdc);
4939 return stat;
4942 struct measure_string_args {
4943 RectF *bounds;
4944 INT *codepointsfitted;
4945 INT *linesfilled;
4948 static GpStatus measure_string_callback(HDC hdc,
4949 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4950 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4951 INT lineno, const RectF *bounds, INT *underlined_indexes,
4952 INT underlined_index_count, void *user_data)
4954 struct measure_string_args *args = user_data;
4956 if (bounds->Width > args->bounds->Width)
4957 args->bounds->Width = bounds->Width;
4959 if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
4960 args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
4962 if (args->codepointsfitted)
4963 *args->codepointsfitted = index + length;
4965 if (args->linesfilled)
4966 (*args->linesfilled)++;
4968 return Ok;
4971 /* Find the smallest rectangle that bounds the text when it is printed in rect
4972 * according to the format options listed in format. If rect has 0 width and
4973 * height, then just find the smallest rectangle that bounds the text when it's
4974 * printed at location (rect->X, rect-Y). */
4975 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4976 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4977 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4978 INT *codepointsfitted, INT *linesfilled)
4980 HFONT oldfont;
4981 struct measure_string_args args;
4982 HDC temp_hdc=NULL, hdc;
4984 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4985 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4986 bounds, codepointsfitted, linesfilled);
4988 if(!graphics || !string || !font || !rect || !bounds)
4989 return InvalidParameter;
4991 if(!graphics->hdc)
4993 hdc = temp_hdc = CreateCompatibleDC(0);
4994 if (!temp_hdc) return OutOfMemory;
4996 else
4997 hdc = graphics->hdc;
4999 if(linesfilled) *linesfilled = 0;
5000 if(codepointsfitted) *codepointsfitted = 0;
5002 if(format)
5003 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5005 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
5007 bounds->X = rect->X;
5008 bounds->Y = rect->Y;
5009 bounds->Width = 0.0;
5010 bounds->Height = 0.0;
5012 args.bounds = bounds;
5013 args.codepointsfitted = codepointsfitted;
5014 args.linesfilled = linesfilled;
5016 gdip_format_string(hdc, string, length, font, rect, format,
5017 measure_string_callback, &args);
5019 DeleteObject(SelectObject(hdc, oldfont));
5021 if (temp_hdc)
5022 DeleteDC(temp_hdc);
5024 return Ok;
5027 struct draw_string_args {
5028 GpGraphics *graphics;
5029 GDIPCONST GpBrush *brush;
5030 REAL x, y, rel_width, rel_height, ascent;
5033 static GpStatus draw_string_callback(HDC hdc,
5034 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5035 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5036 INT lineno, const RectF *bounds, INT *underlined_indexes,
5037 INT underlined_index_count, void *user_data)
5039 struct draw_string_args *args = user_data;
5040 PointF position;
5042 if (underlined_index_count)
5043 FIXME("hotkey underlines not drawn yet\n");
5045 position.X = args->x + bounds->X / args->rel_width;
5046 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5048 return GdipDrawDriverString(args->graphics, &string[index], length, font,
5049 args->brush, &position,
5050 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5053 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5054 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5055 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5057 HRGN rgn = NULL;
5058 HFONT gdifont;
5059 GpPointF pt[3], rectcpy[4];
5060 POINT corners[4];
5061 REAL rel_width, rel_height;
5062 INT save_state;
5063 REAL offsety = 0.0;
5064 struct draw_string_args args;
5065 RectF scaled_rect;
5066 HDC hdc, temp_hdc=NULL;
5067 TEXTMETRICW textmetric;
5069 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5070 length, font, debugstr_rectf(rect), format, brush);
5072 if(!graphics || !string || !font || !brush || !rect)
5073 return InvalidParameter;
5075 if(graphics->hdc)
5077 hdc = graphics->hdc;
5079 else
5081 hdc = temp_hdc = CreateCompatibleDC(0);
5084 if(format){
5085 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5087 /* Should be no need to explicitly test for StringAlignmentNear as
5088 * that is default behavior if no alignment is passed. */
5089 if(format->vertalign != StringAlignmentNear){
5090 RectF bounds;
5091 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
5093 if(format->vertalign == StringAlignmentCenter)
5094 offsety = (rect->Height - bounds.Height) / 2;
5095 else if(format->vertalign == StringAlignmentFar)
5096 offsety = (rect->Height - bounds.Height);
5100 save_state = SaveDC(hdc);
5102 pt[0].X = 0.0;
5103 pt[0].Y = 0.0;
5104 pt[1].X = 1.0;
5105 pt[1].Y = 0.0;
5106 pt[2].X = 0.0;
5107 pt[2].Y = 1.0;
5108 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5109 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5110 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5111 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5112 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5114 rectcpy[3].X = rectcpy[0].X = rect->X;
5115 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
5116 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5117 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
5118 transform_and_round_points(graphics, corners, rectcpy, 4);
5120 scaled_rect.X = 0.0;
5121 scaled_rect.Y = 0.0;
5122 scaled_rect.Width = rel_width * rect->Width;
5123 scaled_rect.Height = rel_height * rect->Height;
5125 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
5127 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5128 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5129 SelectClipRgn(hdc, rgn);
5132 get_font_hfont(graphics, font, &gdifont);
5133 SelectObject(hdc, gdifont);
5135 args.graphics = graphics;
5136 args.brush = brush;
5138 args.x = rect->X;
5139 args.y = rect->Y + offsety;
5141 args.rel_width = rel_width;
5142 args.rel_height = rel_height;
5144 GetTextMetricsW(hdc, &textmetric);
5145 args.ascent = textmetric.tmAscent / rel_height;
5147 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5148 draw_string_callback, &args);
5150 DeleteObject(rgn);
5151 DeleteObject(gdifont);
5153 RestoreDC(hdc, save_state);
5155 DeleteDC(temp_hdc);
5157 return Ok;
5160 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5162 TRACE("(%p)\n", graphics);
5164 if(!graphics)
5165 return InvalidParameter;
5167 if(graphics->busy)
5168 return ObjectBusy;
5170 return GdipSetInfinite(graphics->clip);
5173 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5175 TRACE("(%p)\n", graphics);
5177 if(!graphics)
5178 return InvalidParameter;
5180 if(graphics->busy)
5181 return ObjectBusy;
5183 graphics->worldtrans->matrix[0] = 1.0;
5184 graphics->worldtrans->matrix[1] = 0.0;
5185 graphics->worldtrans->matrix[2] = 0.0;
5186 graphics->worldtrans->matrix[3] = 1.0;
5187 graphics->worldtrans->matrix[4] = 0.0;
5188 graphics->worldtrans->matrix[5] = 0.0;
5190 return Ok;
5193 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5195 return GdipEndContainer(graphics, state);
5198 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5199 GpMatrixOrder order)
5201 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5203 if(!graphics)
5204 return InvalidParameter;
5206 if(graphics->busy)
5207 return ObjectBusy;
5209 return GdipRotateMatrix(graphics->worldtrans, angle, order);
5212 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5214 return GdipBeginContainer2(graphics, state);
5217 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5218 GraphicsContainer *state)
5220 GraphicsContainerItem *container;
5221 GpStatus sts;
5223 TRACE("(%p, %p)\n", graphics, state);
5225 if(!graphics || !state)
5226 return InvalidParameter;
5228 sts = init_container(&container, graphics);
5229 if(sts != Ok)
5230 return sts;
5232 list_add_head(&graphics->containers, &container->entry);
5233 *state = graphics->contid = container->contid;
5235 return Ok;
5238 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5240 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5241 return NotImplemented;
5244 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5246 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5247 return NotImplemented;
5250 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5252 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5253 return NotImplemented;
5256 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5258 GpStatus sts;
5259 GraphicsContainerItem *container, *container2;
5261 TRACE("(%p, %x)\n", graphics, state);
5263 if(!graphics)
5264 return InvalidParameter;
5266 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5267 if(container->contid == state)
5268 break;
5271 /* did not find a matching container */
5272 if(&container->entry == &graphics->containers)
5273 return Ok;
5275 sts = restore_container(graphics, container);
5276 if(sts != Ok)
5277 return sts;
5279 /* remove all of the containers on top of the found container */
5280 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5281 if(container->contid == state)
5282 break;
5283 list_remove(&container->entry);
5284 delete_container(container);
5287 list_remove(&container->entry);
5288 delete_container(container);
5290 return Ok;
5293 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5294 REAL sy, GpMatrixOrder order)
5296 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5298 if(!graphics)
5299 return InvalidParameter;
5301 if(graphics->busy)
5302 return ObjectBusy;
5304 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
5307 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5308 CombineMode mode)
5310 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5312 if(!graphics || !srcgraphics)
5313 return InvalidParameter;
5315 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5318 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5319 CompositingMode mode)
5321 TRACE("(%p, %d)\n", graphics, mode);
5323 if(!graphics)
5324 return InvalidParameter;
5326 if(graphics->busy)
5327 return ObjectBusy;
5329 graphics->compmode = mode;
5331 return Ok;
5334 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5335 CompositingQuality quality)
5337 TRACE("(%p, %d)\n", graphics, quality);
5339 if(!graphics)
5340 return InvalidParameter;
5342 if(graphics->busy)
5343 return ObjectBusy;
5345 graphics->compqual = quality;
5347 return Ok;
5350 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5351 InterpolationMode mode)
5353 TRACE("(%p, %d)\n", graphics, mode);
5355 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5356 return InvalidParameter;
5358 if(graphics->busy)
5359 return ObjectBusy;
5361 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5362 mode = InterpolationModeBilinear;
5364 if (mode == InterpolationModeHighQuality)
5365 mode = InterpolationModeHighQualityBicubic;
5367 graphics->interpolation = mode;
5369 return Ok;
5372 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5374 TRACE("(%p, %.2f)\n", graphics, scale);
5376 if(!graphics || (scale <= 0.0))
5377 return InvalidParameter;
5379 if(graphics->busy)
5380 return ObjectBusy;
5382 graphics->scale = scale;
5384 return Ok;
5387 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5389 TRACE("(%p, %d)\n", graphics, unit);
5391 if(!graphics)
5392 return InvalidParameter;
5394 if(graphics->busy)
5395 return ObjectBusy;
5397 if(unit == UnitWorld)
5398 return InvalidParameter;
5400 graphics->unit = unit;
5402 return Ok;
5405 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5406 mode)
5408 TRACE("(%p, %d)\n", graphics, mode);
5410 if(!graphics)
5411 return InvalidParameter;
5413 if(graphics->busy)
5414 return ObjectBusy;
5416 graphics->pixeloffset = mode;
5418 return Ok;
5421 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5423 static int calls;
5425 TRACE("(%p,%i,%i)\n", graphics, x, y);
5427 if (!(calls++))
5428 FIXME("not implemented\n");
5430 return NotImplemented;
5433 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5435 static int calls;
5437 TRACE("(%p,%p,%p)\n", graphics, x, y);
5439 if (!(calls++))
5440 FIXME("not implemented\n");
5442 *x = *y = 0;
5444 return NotImplemented;
5447 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5449 TRACE("(%p, %d)\n", graphics, mode);
5451 if(!graphics)
5452 return InvalidParameter;
5454 if(graphics->busy)
5455 return ObjectBusy;
5457 graphics->smoothing = mode;
5459 return Ok;
5462 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5464 TRACE("(%p, %d)\n", graphics, contrast);
5466 if(!graphics)
5467 return InvalidParameter;
5469 graphics->textcontrast = contrast;
5471 return Ok;
5474 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5475 TextRenderingHint hint)
5477 TRACE("(%p, %d)\n", graphics, hint);
5479 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5480 return InvalidParameter;
5482 if(graphics->busy)
5483 return ObjectBusy;
5485 graphics->texthint = hint;
5487 return Ok;
5490 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5492 TRACE("(%p, %p)\n", graphics, matrix);
5494 if(!graphics || !matrix)
5495 return InvalidParameter;
5497 if(graphics->busy)
5498 return ObjectBusy;
5500 GdipDeleteMatrix(graphics->worldtrans);
5501 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5504 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5505 REAL dy, GpMatrixOrder order)
5507 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5509 if(!graphics)
5510 return InvalidParameter;
5512 if(graphics->busy)
5513 return ObjectBusy;
5515 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5518 /*****************************************************************************
5519 * GdipSetClipHrgn [GDIPLUS.@]
5521 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5523 GpRegion *region;
5524 GpStatus status;
5526 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5528 if(!graphics)
5529 return InvalidParameter;
5531 status = GdipCreateRegionHrgn(hrgn, &region);
5532 if(status != Ok)
5533 return status;
5535 status = GdipSetClipRegion(graphics, region, mode);
5537 GdipDeleteRegion(region);
5538 return status;
5541 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5543 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5545 if(!graphics)
5546 return InvalidParameter;
5548 if(graphics->busy)
5549 return ObjectBusy;
5551 return GdipCombineRegionPath(graphics->clip, path, mode);
5554 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5555 REAL width, REAL height,
5556 CombineMode mode)
5558 GpRectF rect;
5560 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5562 if(!graphics)
5563 return InvalidParameter;
5565 if(graphics->busy)
5566 return ObjectBusy;
5568 rect.X = x;
5569 rect.Y = y;
5570 rect.Width = width;
5571 rect.Height = height;
5573 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5576 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5577 INT width, INT height,
5578 CombineMode mode)
5580 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5582 if(!graphics)
5583 return InvalidParameter;
5585 if(graphics->busy)
5586 return ObjectBusy;
5588 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5591 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5592 CombineMode mode)
5594 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5596 if(!graphics || !region)
5597 return InvalidParameter;
5599 if(graphics->busy)
5600 return ObjectBusy;
5602 return GdipCombineRegionRegion(graphics->clip, region, mode);
5605 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5606 UINT limitDpi)
5608 static int calls;
5610 TRACE("(%p,%u)\n", metafile, limitDpi);
5612 if(!(calls++))
5613 FIXME("not implemented\n");
5615 return NotImplemented;
5618 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5619 INT count)
5621 INT save_state;
5622 POINT *pti;
5624 TRACE("(%p, %p, %d)\n", graphics, points, count);
5626 if(!graphics || !pen || count<=0)
5627 return InvalidParameter;
5629 if(graphics->busy)
5630 return ObjectBusy;
5632 if (!graphics->hdc)
5634 FIXME("graphics object has no HDC\n");
5635 return Ok;
5638 pti = GdipAlloc(sizeof(POINT) * count);
5640 save_state = prepare_dc(graphics, pen);
5641 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5643 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5644 Polygon(graphics->hdc, pti, count);
5646 restore_dc(graphics, save_state);
5647 GdipFree(pti);
5649 return Ok;
5652 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5653 INT count)
5655 GpStatus ret;
5656 GpPointF *ptf;
5657 INT i;
5659 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5661 if(count<=0) return InvalidParameter;
5662 ptf = GdipAlloc(sizeof(GpPointF) * count);
5664 for(i = 0;i < count; i++){
5665 ptf[i].X = (REAL)points[i].X;
5666 ptf[i].Y = (REAL)points[i].Y;
5669 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5670 GdipFree(ptf);
5672 return ret;
5675 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5677 TRACE("(%p, %p)\n", graphics, dpi);
5679 if(!graphics || !dpi)
5680 return InvalidParameter;
5682 if(graphics->busy)
5683 return ObjectBusy;
5685 if (graphics->image)
5686 *dpi = graphics->image->xres;
5687 else
5688 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
5690 return Ok;
5693 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5695 TRACE("(%p, %p)\n", graphics, dpi);
5697 if(!graphics || !dpi)
5698 return InvalidParameter;
5700 if(graphics->busy)
5701 return ObjectBusy;
5703 if (graphics->image)
5704 *dpi = graphics->image->yres;
5705 else
5706 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
5708 return Ok;
5711 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5712 GpMatrixOrder order)
5714 GpMatrix m;
5715 GpStatus ret;
5717 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5719 if(!graphics || !matrix)
5720 return InvalidParameter;
5722 if(graphics->busy)
5723 return ObjectBusy;
5725 m = *(graphics->worldtrans);
5727 ret = GdipMultiplyMatrix(&m, matrix, order);
5728 if(ret == Ok)
5729 *(graphics->worldtrans) = m;
5731 return ret;
5734 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5735 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5737 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5739 GpStatus stat=Ok;
5741 TRACE("(%p, %p)\n", graphics, hdc);
5743 if(!graphics || !hdc)
5744 return InvalidParameter;
5746 if(graphics->busy)
5747 return ObjectBusy;
5749 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5751 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5753 else if (!graphics->hdc ||
5754 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5756 /* Create a fake HDC and fill it with a constant color. */
5757 HDC temp_hdc;
5758 HBITMAP hbitmap;
5759 GpRectF bounds;
5760 BITMAPINFOHEADER bmih;
5761 int i;
5763 stat = get_graphics_bounds(graphics, &bounds);
5764 if (stat != Ok)
5765 return stat;
5767 graphics->temp_hbitmap_width = bounds.Width;
5768 graphics->temp_hbitmap_height = bounds.Height;
5770 bmih.biSize = sizeof(bmih);
5771 bmih.biWidth = graphics->temp_hbitmap_width;
5772 bmih.biHeight = -graphics->temp_hbitmap_height;
5773 bmih.biPlanes = 1;
5774 bmih.biBitCount = 32;
5775 bmih.biCompression = BI_RGB;
5776 bmih.biSizeImage = 0;
5777 bmih.biXPelsPerMeter = 0;
5778 bmih.biYPelsPerMeter = 0;
5779 bmih.biClrUsed = 0;
5780 bmih.biClrImportant = 0;
5782 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5783 (void**)&graphics->temp_bits, NULL, 0);
5784 if (!hbitmap)
5785 return GenericError;
5787 temp_hdc = CreateCompatibleDC(0);
5788 if (!temp_hdc)
5790 DeleteObject(hbitmap);
5791 return GenericError;
5794 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5795 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5797 SelectObject(temp_hdc, hbitmap);
5799 graphics->temp_hbitmap = hbitmap;
5800 *hdc = graphics->temp_hdc = temp_hdc;
5802 else
5804 *hdc = graphics->hdc;
5807 if (stat == Ok)
5808 graphics->busy = TRUE;
5810 return stat;
5813 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5815 GpStatus stat=Ok;
5817 TRACE("(%p, %p)\n", graphics, hdc);
5819 if(!graphics || !hdc || !graphics->busy)
5820 return InvalidParameter;
5822 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5824 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5826 else if (graphics->temp_hdc == hdc)
5828 DWORD* pos;
5829 int i;
5831 /* Find the pixels that have changed, and mark them as opaque. */
5832 pos = (DWORD*)graphics->temp_bits;
5833 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5835 if (*pos != DC_BACKGROUND_KEY)
5837 *pos |= 0xff000000;
5839 pos++;
5842 /* Write the changed pixels to the real target. */
5843 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5844 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5845 graphics->temp_hbitmap_width * 4);
5847 /* Clean up. */
5848 DeleteDC(graphics->temp_hdc);
5849 DeleteObject(graphics->temp_hbitmap);
5850 graphics->temp_hdc = NULL;
5851 graphics->temp_hbitmap = NULL;
5853 else if (hdc != graphics->hdc)
5855 stat = InvalidParameter;
5858 if (stat == Ok)
5859 graphics->busy = FALSE;
5861 return stat;
5864 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5866 GpRegion *clip;
5867 GpStatus status;
5869 TRACE("(%p, %p)\n", graphics, region);
5871 if(!graphics || !region)
5872 return InvalidParameter;
5874 if(graphics->busy)
5875 return ObjectBusy;
5877 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5878 return status;
5880 /* free everything except root node and header */
5881 delete_element(&region->node);
5882 memcpy(region, clip, sizeof(GpRegion));
5883 GdipFree(clip);
5885 return Ok;
5888 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5889 GpCoordinateSpace src_space, GpMatrix **matrix)
5891 GpStatus stat = GdipCreateMatrix(matrix);
5892 REAL unitscale;
5894 if (dst_space != src_space && stat == Ok)
5896 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
5898 if(graphics->unit != UnitDisplay)
5899 unitscale *= graphics->scale;
5901 /* transform from src_space to CoordinateSpacePage */
5902 switch (src_space)
5904 case CoordinateSpaceWorld:
5905 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5906 break;
5907 case CoordinateSpacePage:
5908 break;
5909 case CoordinateSpaceDevice:
5910 GdipScaleMatrix(*matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
5911 break;
5914 /* transform from CoordinateSpacePage to dst_space */
5915 switch (dst_space)
5917 case CoordinateSpaceWorld:
5919 GpMatrix *inverted_transform;
5920 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
5921 if (stat == Ok)
5923 stat = GdipInvertMatrix(inverted_transform);
5924 if (stat == Ok)
5925 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
5926 GdipDeleteMatrix(inverted_transform);
5928 break;
5930 case CoordinateSpacePage:
5931 break;
5932 case CoordinateSpaceDevice:
5933 GdipScaleMatrix(*matrix, unitscale, unitscale, MatrixOrderAppend);
5934 break;
5937 return stat;
5940 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5941 GpCoordinateSpace src_space, GpPointF *points, INT count)
5943 GpMatrix *matrix;
5944 GpStatus stat;
5946 if(!graphics || !points || count <= 0)
5947 return InvalidParameter;
5949 if(graphics->busy)
5950 return ObjectBusy;
5952 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5954 if (src_space == dst_space) return Ok;
5956 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5958 if (stat == Ok)
5960 stat = GdipTransformMatrixPoints(matrix, points, count);
5962 GdipDeleteMatrix(matrix);
5965 return stat;
5968 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5969 GpCoordinateSpace src_space, GpPoint *points, INT count)
5971 GpPointF *pointsF;
5972 GpStatus ret;
5973 INT i;
5975 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5977 if(count <= 0)
5978 return InvalidParameter;
5980 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5981 if(!pointsF)
5982 return OutOfMemory;
5984 for(i = 0; i < count; i++){
5985 pointsF[i].X = (REAL)points[i].X;
5986 pointsF[i].Y = (REAL)points[i].Y;
5989 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5991 if(ret == Ok)
5992 for(i = 0; i < count; i++){
5993 points[i].X = roundr(pointsF[i].X);
5994 points[i].Y = roundr(pointsF[i].Y);
5996 GdipFree(pointsF);
5998 return ret;
6001 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6003 static int calls;
6005 TRACE("\n");
6007 if (!calls++)
6008 FIXME("stub\n");
6010 return NULL;
6013 /*****************************************************************************
6014 * GdipTranslateClip [GDIPLUS.@]
6016 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6018 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6020 if(!graphics)
6021 return InvalidParameter;
6023 if(graphics->busy)
6024 return ObjectBusy;
6026 return GdipTranslateRegion(graphics->clip, dx, dy);
6029 /*****************************************************************************
6030 * GdipTranslateClipI [GDIPLUS.@]
6032 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6034 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6036 if(!graphics)
6037 return InvalidParameter;
6039 if(graphics->busy)
6040 return ObjectBusy;
6042 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6046 /*****************************************************************************
6047 * GdipMeasureDriverString [GDIPLUS.@]
6049 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6050 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6051 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6053 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6054 HFONT hfont;
6055 HDC hdc;
6056 REAL min_x, min_y, max_x, max_y, x, y;
6057 int i;
6058 TEXTMETRICW textmetric;
6059 const WORD *glyph_indices;
6060 WORD *dynamic_glyph_indices=NULL;
6061 REAL rel_width, rel_height, ascent, descent;
6062 GpPointF pt[3];
6064 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6066 if (!graphics || !text || !font || !positions || !boundingBox)
6067 return InvalidParameter;
6069 if (length == -1)
6070 length = strlenW(text);
6072 if (length == 0)
6074 boundingBox->X = 0.0;
6075 boundingBox->Y = 0.0;
6076 boundingBox->Width = 0.0;
6077 boundingBox->Height = 0.0;
6080 if (flags & unsupported_flags)
6081 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6083 if (matrix)
6084 FIXME("Ignoring matrix\n");
6086 get_font_hfont(graphics, font, &hfont);
6088 hdc = CreateCompatibleDC(0);
6089 SelectObject(hdc, hfont);
6091 GetTextMetricsW(hdc, &textmetric);
6093 pt[0].X = 0.0;
6094 pt[0].Y = 0.0;
6095 pt[1].X = 1.0;
6096 pt[1].Y = 0.0;
6097 pt[2].X = 0.0;
6098 pt[2].Y = 1.0;
6099 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6100 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6101 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6102 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6103 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6105 if (flags & DriverStringOptionsCmapLookup)
6107 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6108 if (!glyph_indices)
6110 DeleteDC(hdc);
6111 DeleteObject(hfont);
6112 return OutOfMemory;
6115 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6117 else
6118 glyph_indices = text;
6120 min_x = max_x = x = positions[0].X;
6121 min_y = max_y = y = positions[0].Y;
6123 ascent = textmetric.tmAscent / rel_height;
6124 descent = textmetric.tmDescent / rel_height;
6126 for (i=0; i<length; i++)
6128 int char_width;
6129 ABC abc;
6131 if (!(flags & DriverStringOptionsRealizedAdvance))
6133 x = positions[i].X;
6134 y = positions[i].Y;
6137 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6138 char_width = abc.abcA + abc.abcB + abc.abcB;
6140 if (min_y > y - ascent) min_y = y - ascent;
6141 if (max_y < y + descent) max_y = y + descent;
6142 if (min_x > x) min_x = x;
6144 x += char_width / rel_width;
6146 if (max_x < x) max_x = x;
6149 GdipFree(dynamic_glyph_indices);
6150 DeleteDC(hdc);
6151 DeleteObject(hfont);
6153 boundingBox->X = min_x;
6154 boundingBox->Y = min_y;
6155 boundingBox->Width = max_x - min_x;
6156 boundingBox->Height = max_y - min_y;
6158 return Ok;
6161 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6162 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6163 GDIPCONST PointF *positions, INT flags,
6164 GDIPCONST GpMatrix *matrix )
6166 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6167 INT save_state;
6168 GpPointF pt;
6169 HFONT hfont;
6170 UINT eto_flags=0;
6172 if (flags & unsupported_flags)
6173 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6175 if (matrix)
6176 FIXME("Ignoring matrix\n");
6178 if (!(flags & DriverStringOptionsCmapLookup))
6179 eto_flags |= ETO_GLYPH_INDEX;
6181 save_state = SaveDC(graphics->hdc);
6182 SetBkMode(graphics->hdc, TRANSPARENT);
6183 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6185 pt = positions[0];
6186 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6188 get_font_hfont(graphics, font, &hfont);
6189 SelectObject(graphics->hdc, hfont);
6191 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6193 ExtTextOutW(graphics->hdc, roundr(pt.X), roundr(pt.Y), eto_flags, NULL, text, length, NULL);
6195 RestoreDC(graphics->hdc, save_state);
6197 DeleteObject(hfont);
6199 return Ok;
6202 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6203 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6204 GDIPCONST PointF *positions, INT flags,
6205 GDIPCONST GpMatrix *matrix )
6207 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6208 GpStatus stat;
6209 PointF *real_positions, real_position;
6210 POINT *pti;
6211 HFONT hfont;
6212 HDC hdc;
6213 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6214 DWORD max_glyphsize=0;
6215 GLYPHMETRICS glyphmetrics;
6216 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6217 BYTE *glyph_mask;
6218 BYTE *text_mask;
6219 int text_mask_stride;
6220 BYTE *pixel_data;
6221 int pixel_data_stride;
6222 GpRect pixel_area;
6223 UINT ggo_flags = GGO_GRAY8_BITMAP;
6225 if (length <= 0)
6226 return Ok;
6228 if (!(flags & DriverStringOptionsCmapLookup))
6229 ggo_flags |= GGO_GLYPH_INDEX;
6231 if (flags & unsupported_flags)
6232 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6234 if (matrix)
6235 FIXME("Ignoring matrix\n");
6237 pti = GdipAlloc(sizeof(POINT) * length);
6238 if (!pti)
6239 return OutOfMemory;
6241 if (flags & DriverStringOptionsRealizedAdvance)
6243 real_position = positions[0];
6245 transform_and_round_points(graphics, pti, &real_position, 1);
6247 else
6249 real_positions = GdipAlloc(sizeof(PointF) * length);
6250 if (!real_positions)
6252 GdipFree(pti);
6253 return OutOfMemory;
6256 memcpy(real_positions, positions, sizeof(PointF) * length);
6258 transform_and_round_points(graphics, pti, real_positions, length);
6260 GdipFree(real_positions);
6263 get_font_hfont(graphics, font, &hfont);
6265 hdc = CreateCompatibleDC(0);
6266 SelectObject(hdc, hfont);
6268 /* Get the boundaries of the text to be drawn */
6269 for (i=0; i<length; i++)
6271 DWORD glyphsize;
6272 int left, top, right, bottom;
6274 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6275 &glyphmetrics, 0, NULL, &identity);
6277 if (glyphsize == GDI_ERROR)
6279 ERR("GetGlyphOutlineW failed\n");
6280 GdipFree(pti);
6281 DeleteDC(hdc);
6282 DeleteObject(hfont);
6283 return GenericError;
6286 if (glyphsize > max_glyphsize)
6287 max_glyphsize = glyphsize;
6289 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6290 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6291 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6292 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6294 if (left < min_x) min_x = left;
6295 if (top < min_y) min_y = top;
6296 if (right > max_x) max_x = right;
6297 if (bottom > max_y) max_y = bottom;
6299 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6301 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6302 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6306 glyph_mask = GdipAlloc(max_glyphsize);
6307 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6308 text_mask_stride = max_x - min_x;
6310 if (!(glyph_mask && text_mask))
6312 GdipFree(glyph_mask);
6313 GdipFree(text_mask);
6314 GdipFree(pti);
6315 DeleteDC(hdc);
6316 DeleteObject(hfont);
6317 return OutOfMemory;
6320 /* Generate a mask for the text */
6321 for (i=0; i<length; i++)
6323 int left, top, stride;
6325 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6326 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6328 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6329 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6330 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6332 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6334 BYTE *glyph_val = glyph_mask + y * stride;
6335 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6336 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6338 *text_val = min(64, *text_val + *glyph_val);
6339 glyph_val++;
6340 text_val++;
6345 GdipFree(pti);
6346 DeleteDC(hdc);
6347 DeleteObject(hfont);
6348 GdipFree(glyph_mask);
6350 /* get the brush data */
6351 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6352 if (!pixel_data)
6354 GdipFree(text_mask);
6355 return OutOfMemory;
6358 pixel_area.X = min_x;
6359 pixel_area.Y = min_y;
6360 pixel_area.Width = max_x - min_x;
6361 pixel_area.Height = max_y - min_y;
6362 pixel_data_stride = pixel_area.Width * 4;
6364 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6365 if (stat != Ok)
6367 GdipFree(text_mask);
6368 GdipFree(pixel_data);
6369 return stat;
6372 /* multiply the brush data by the mask */
6373 for (y=0; y<pixel_area.Height; y++)
6375 BYTE *text_val = text_mask + text_mask_stride * y;
6376 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6377 for (x=0; x<pixel_area.Width; x++)
6379 *pixel_val = (*pixel_val) * (*text_val) / 64;
6380 text_val++;
6381 pixel_val+=4;
6385 GdipFree(text_mask);
6387 /* draw the result */
6388 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6389 pixel_area.Height, pixel_data_stride);
6391 GdipFree(pixel_data);
6393 return stat;
6396 /*****************************************************************************
6397 * GdipDrawDriverString [GDIPLUS.@]
6399 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6400 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6401 GDIPCONST PointF *positions, INT flags,
6402 GDIPCONST GpMatrix *matrix )
6404 GpStatus stat=NotImplemented;
6406 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6408 if (!graphics || !text || !font || !brush || !positions)
6409 return InvalidParameter;
6411 if (length == -1)
6412 length = strlenW(text);
6414 if (graphics->hdc &&
6415 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6416 brush->bt == BrushTypeSolidColor &&
6417 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6418 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, brush,
6419 positions, flags, matrix);
6421 if (stat == NotImplemented)
6422 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, brush,
6423 positions, flags, matrix);
6425 return stat;
6428 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6429 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6431 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6432 return NotImplemented;
6435 /*****************************************************************************
6436 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6438 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6440 GpStatus stat;
6441 GpRegion* rgn;
6443 TRACE("(%p, %p)\n", graphics, res);
6445 if((stat = GdipCreateRegion(&rgn)) != Ok)
6446 return stat;
6448 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6449 goto cleanup;
6451 stat = GdipIsEmptyRegion(rgn, graphics, res);
6453 cleanup:
6454 GdipDeleteRegion(rgn);
6455 return stat;