gdiplus: Fix use of uninitialized memory.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blob324b895524166fe79a2841b048deb27d6d850631
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 INT prepare_dc(GpGraphics *graphics, GpPen *pen)
95 HPEN gdipen;
96 REAL width;
97 INT save_state, i, numdashes;
98 GpPointF pt[2];
99 DWORD dash_array[MAX_DASHLEN];
101 save_state = SaveDC(graphics->hdc);
103 EndPath(graphics->hdc);
105 if(pen->unit == UnitPixel){
106 width = pen->width;
108 else{
109 /* Get an estimate for the amount the pen width is affected by the world
110 * transform. (This is similar to what some of the wine drivers do.) */
111 pt[0].X = 0.0;
112 pt[0].Y = 0.0;
113 pt[1].X = 1.0;
114 pt[1].Y = 1.0;
115 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
116 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
117 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
119 width *= pen->width * convert_unit(graphics_res(graphics),
120 pen->unit == UnitWorld ? graphics->unit : pen->unit);
123 if(pen->dash == DashStyleCustom){
124 numdashes = min(pen->numdashes, MAX_DASHLEN);
126 TRACE("dashes are: ");
127 for(i = 0; i < numdashes; i++){
128 dash_array[i] = roundr(width * pen->dashes[i]);
129 TRACE("%d, ", dash_array[i]);
131 TRACE("\n and the pen style is %x\n", pen->style);
133 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
134 numdashes, dash_array);
136 else
137 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
139 SelectObject(graphics->hdc, gdipen);
141 return save_state;
144 static void restore_dc(GpGraphics *graphics, INT state)
146 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
147 RestoreDC(graphics->hdc, state);
150 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
151 GpCoordinateSpace src_space, GpMatrix **matrix);
153 /* This helper applies all the changes that the points listed in ptf need in
154 * order to be drawn on the device context. In the end, this should include at
155 * least:
156 * -scaling by page unit
157 * -applying world transformation
158 * -converting from float to int
159 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
160 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
161 * gdi to draw, and these functions would irreparably mess with line widths.
163 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
164 GpPointF *ptf, INT count)
166 REAL unitscale;
167 GpMatrix *matrix;
168 int i;
170 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
172 /* apply page scale */
173 if(graphics->unit != UnitDisplay)
174 unitscale *= graphics->scale;
176 GdipCloneMatrix(graphics->worldtrans, &matrix);
177 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
178 GdipTransformMatrixPoints(matrix, ptf, count);
179 GdipDeleteMatrix(matrix);
181 for(i = 0; i < count; i++){
182 pti[i].x = roundr(ptf[i].X);
183 pti[i].y = roundr(ptf[i].Y);
187 /* Draw non-premultiplied ARGB data to the given graphics object */
188 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
189 const BYTE *src, INT src_width, INT src_height, INT src_stride)
191 if (graphics->image && graphics->image->type == ImageTypeBitmap)
193 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
194 INT x, y;
196 for (x=0; x<src_width; x++)
198 for (y=0; y<src_height; y++)
200 ARGB dst_color, src_color;
201 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
202 src_color = ((ARGB*)(src + src_stride * y))[x];
203 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
207 return Ok;
209 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
211 ERR("This should not be used for metafiles; fix caller\n");
212 return NotImplemented;
214 else
216 HDC hdc;
217 HBITMAP hbitmap, old_hbm=NULL;
218 BITMAPINFOHEADER bih;
219 BYTE *temp_bits;
220 BLENDFUNCTION bf;
222 hdc = CreateCompatibleDC(0);
224 bih.biSize = sizeof(BITMAPINFOHEADER);
225 bih.biWidth = src_width;
226 bih.biHeight = -src_height;
227 bih.biPlanes = 1;
228 bih.biBitCount = 32;
229 bih.biCompression = BI_RGB;
230 bih.biSizeImage = 0;
231 bih.biXPelsPerMeter = 0;
232 bih.biYPelsPerMeter = 0;
233 bih.biClrUsed = 0;
234 bih.biClrImportant = 0;
236 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
237 (void**)&temp_bits, NULL, 0);
239 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
240 4 * src_width, src, src_stride);
242 old_hbm = SelectObject(hdc, hbitmap);
244 bf.BlendOp = AC_SRC_OVER;
245 bf.BlendFlags = 0;
246 bf.SourceConstantAlpha = 255;
247 bf.AlphaFormat = AC_SRC_ALPHA;
249 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, src_width, src_height,
250 hdc, 0, 0, src_width, src_height, bf);
252 SelectObject(hdc, old_hbm);
253 DeleteDC(hdc);
254 DeleteObject(hbitmap);
256 return Ok;
260 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
262 ARGB result=0;
263 ARGB i;
264 INT a1, a2, a3;
266 a1 = (start >> 24) & 0xff;
267 a2 = (end >> 24) & 0xff;
269 a3 = (int)(a1*(1.0f - position)+a2*(position));
271 result |= a3 << 24;
273 for (i=0xff; i<=0xff0000; i = i << 8)
274 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
275 return result;
278 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
280 REAL blendfac;
282 /* clamp to between 0.0 and 1.0, using the wrap mode */
283 if (brush->wrap == WrapModeTile)
285 position = fmodf(position, 1.0f);
286 if (position < 0.0f) position += 1.0f;
288 else /* WrapModeFlip* */
290 position = fmodf(position, 2.0f);
291 if (position < 0.0f) position += 2.0f;
292 if (position > 1.0f) position = 2.0f - position;
295 if (brush->blendcount == 1)
296 blendfac = position;
297 else
299 int i=1;
300 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
301 REAL range;
303 /* locate the blend positions surrounding this position */
304 while (position > brush->blendpos[i])
305 i++;
307 /* interpolate between the blend positions */
308 left_blendpos = brush->blendpos[i-1];
309 left_blendfac = brush->blendfac[i-1];
310 right_blendpos = brush->blendpos[i];
311 right_blendfac = brush->blendfac[i];
312 range = right_blendpos - left_blendpos;
313 blendfac = (left_blendfac * (right_blendpos - position) +
314 right_blendfac * (position - left_blendpos)) / range;
317 if (brush->pblendcount == 0)
318 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
319 else
321 int i=1;
322 ARGB left_blendcolor, right_blendcolor;
323 REAL left_blendpos, right_blendpos;
325 /* locate the blend colors surrounding this position */
326 while (blendfac > brush->pblendpos[i])
327 i++;
329 /* interpolate between the blend colors */
330 left_blendpos = brush->pblendpos[i-1];
331 left_blendcolor = brush->pblendcolor[i-1];
332 right_blendpos = brush->pblendpos[i];
333 right_blendcolor = brush->pblendcolor[i];
334 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
335 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
339 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
341 REAL val[5], res[4];
342 int i, j;
343 unsigned char a, r, g, b;
345 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
346 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
347 val[2] = (color & 0xff) / 255.0; /* blue */
348 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
349 val[4] = 1.0; /* translation */
351 for (i=0; i<4; i++)
353 res[i] = 0.0;
355 for (j=0; j<5; j++)
356 res[i] += matrix->m[j][i] * val[j];
359 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
360 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
361 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
362 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
364 return (a << 24) | (r << 16) | (g << 8) | b;
367 static int color_is_gray(ARGB color)
369 unsigned char r, g, b;
371 r = (color >> 16) & 0xff;
372 g = (color >> 8) & 0xff;
373 b = color & 0xff;
375 return (r == g) && (g == b);
378 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
379 UINT width, UINT height, INT stride, ColorAdjustType type)
381 UINT x, y, i;
383 if (attributes->colorkeys[type].enabled ||
384 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
386 const struct color_key *key;
387 BYTE min_blue, min_green, min_red;
388 BYTE max_blue, max_green, max_red;
390 if (attributes->colorkeys[type].enabled)
391 key = &attributes->colorkeys[type];
392 else
393 key = &attributes->colorkeys[ColorAdjustTypeDefault];
395 min_blue = key->low&0xff;
396 min_green = (key->low>>8)&0xff;
397 min_red = (key->low>>16)&0xff;
399 max_blue = key->high&0xff;
400 max_green = (key->high>>8)&0xff;
401 max_red = (key->high>>16)&0xff;
403 for (x=0; x<width; x++)
404 for (y=0; y<height; y++)
406 ARGB *src_color;
407 BYTE blue, green, red;
408 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
409 blue = *src_color&0xff;
410 green = (*src_color>>8)&0xff;
411 red = (*src_color>>16)&0xff;
412 if (blue >= min_blue && green >= min_green && red >= min_red &&
413 blue <= max_blue && green <= max_green && red <= max_red)
414 *src_color = 0x00000000;
418 if (attributes->colorremaptables[type].enabled ||
419 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
421 const struct color_remap_table *table;
423 if (attributes->colorremaptables[type].enabled)
424 table = &attributes->colorremaptables[type];
425 else
426 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
428 for (x=0; x<width; x++)
429 for (y=0; y<height; y++)
431 ARGB *src_color;
432 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
433 for (i=0; i<table->mapsize; i++)
435 if (*src_color == table->colormap[i].oldColor.Argb)
437 *src_color = table->colormap[i].newColor.Argb;
438 break;
444 if (attributes->colormatrices[type].enabled ||
445 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
447 const struct color_matrix *colormatrices;
449 if (attributes->colormatrices[type].enabled)
450 colormatrices = &attributes->colormatrices[type];
451 else
452 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
454 for (x=0; x<width; x++)
455 for (y=0; y<height; y++)
457 ARGB *src_color;
458 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
460 if (colormatrices->flags == ColorMatrixFlagsDefault ||
461 !color_is_gray(*src_color))
463 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
465 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
467 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
472 if (attributes->gamma_enabled[type] ||
473 attributes->gamma_enabled[ColorAdjustTypeDefault])
475 REAL gamma;
477 if (attributes->gamma_enabled[type])
478 gamma = attributes->gamma[type];
479 else
480 gamma = attributes->gamma[ColorAdjustTypeDefault];
482 for (x=0; x<width; x++)
483 for (y=0; y<height; y++)
485 ARGB *src_color;
486 BYTE blue, green, red;
487 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
489 blue = *src_color&0xff;
490 green = (*src_color>>8)&0xff;
491 red = (*src_color>>16)&0xff;
493 /* FIXME: We should probably use a table for this. */
494 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
495 green = floorf(powf(green / 255.0, gamma) * 255.0);
496 red = floorf(powf(red / 255.0, gamma) * 255.0);
498 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
503 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
504 * bitmap that contains all the pixels we may need to draw it. */
505 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
506 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
507 GpRect *rect)
509 INT left, top, right, bottom;
511 switch (interpolation)
513 case InterpolationModeHighQualityBilinear:
514 case InterpolationModeHighQualityBicubic:
515 /* FIXME: Include a greater range for the prefilter? */
516 case InterpolationModeBicubic:
517 case InterpolationModeBilinear:
518 left = (INT)(floorf(srcx));
519 top = (INT)(floorf(srcy));
520 right = (INT)(ceilf(srcx+srcwidth));
521 bottom = (INT)(ceilf(srcy+srcheight));
522 break;
523 case InterpolationModeNearestNeighbor:
524 default:
525 left = roundr(srcx);
526 top = roundr(srcy);
527 right = roundr(srcx+srcwidth);
528 bottom = roundr(srcy+srcheight);
529 break;
532 if (wrap == WrapModeClamp)
534 if (left < 0)
535 left = 0;
536 if (top < 0)
537 top = 0;
538 if (right >= bitmap->width)
539 right = bitmap->width-1;
540 if (bottom >= bitmap->height)
541 bottom = bitmap->height-1;
543 else
545 /* In some cases we can make the rectangle smaller here, but the logic
546 * is hard to get right, and tiling suggests we're likely to use the
547 * entire source image. */
548 if (left < 0 || right >= bitmap->width)
550 left = 0;
551 right = bitmap->width-1;
554 if (top < 0 || bottom >= bitmap->height)
556 top = 0;
557 bottom = bitmap->height-1;
561 rect->X = left;
562 rect->Y = top;
563 rect->Width = right - left + 1;
564 rect->Height = bottom - top + 1;
567 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
568 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
570 if (attributes->wrap == WrapModeClamp)
572 if (x < 0 || y < 0 || x >= width || y >= height)
573 return attributes->outside_color;
575 else
577 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
578 if (x < 0)
579 x = width*2 + x % (width * 2);
580 if (y < 0)
581 y = height*2 + y % (height * 2);
583 if ((attributes->wrap & 1) == 1)
585 /* Flip X */
586 if ((x / width) % 2 == 0)
587 x = x % width;
588 else
589 x = width - 1 - x % width;
591 else
592 x = x % width;
594 if ((attributes->wrap & 2) == 2)
596 /* Flip Y */
597 if ((y / height) % 2 == 0)
598 y = y % height;
599 else
600 y = height - 1 - y % height;
602 else
603 y = y % height;
606 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
608 ERR("out of range pixel requested\n");
609 return 0xffcd0084;
612 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
615 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
616 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
617 InterpolationMode interpolation)
619 static int fixme;
621 switch (interpolation)
623 default:
624 if (!fixme++)
625 FIXME("Unimplemented interpolation %i\n", interpolation);
626 /* fall-through */
627 case InterpolationModeBilinear:
629 REAL leftxf, topyf;
630 INT leftx, rightx, topy, bottomy;
631 ARGB topleft, topright, bottomleft, bottomright;
632 ARGB top, bottom;
633 float x_offset;
635 leftxf = floorf(point->X);
636 leftx = (INT)leftxf;
637 rightx = (INT)ceilf(point->X);
638 topyf = floorf(point->Y);
639 topy = (INT)topyf;
640 bottomy = (INT)ceilf(point->Y);
642 if (leftx == rightx && topy == bottomy)
643 return sample_bitmap_pixel(src_rect, bits, width, height,
644 leftx, topy, attributes);
646 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
647 leftx, topy, attributes);
648 topright = sample_bitmap_pixel(src_rect, bits, width, height,
649 rightx, topy, attributes);
650 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
651 leftx, bottomy, attributes);
652 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
653 rightx, bottomy, attributes);
655 x_offset = point->X - leftxf;
656 top = blend_colors(topleft, topright, x_offset);
657 bottom = blend_colors(bottomleft, bottomright, x_offset);
659 return blend_colors(top, bottom, point->Y - topyf);
661 case InterpolationModeNearestNeighbor:
662 return sample_bitmap_pixel(src_rect, bits, width, height,
663 roundr(point->X), roundr(point->Y), attributes);
667 static INT brush_can_fill_path(GpBrush *brush)
669 switch (brush->bt)
671 case BrushTypeSolidColor:
672 case BrushTypeHatchFill:
673 return 1;
674 case BrushTypeLinearGradient:
675 case BrushTypeTextureFill:
676 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
677 default:
678 return 0;
682 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
684 switch (brush->bt)
686 case BrushTypeSolidColor:
688 GpSolidFill *fill = (GpSolidFill*)brush;
689 if (fill->bmp)
691 RECT rc;
692 /* partially transparent fill */
694 SelectClipPath(graphics->hdc, RGN_AND);
695 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
697 HDC hdc = CreateCompatibleDC(NULL);
698 HBITMAP oldbmp;
699 BLENDFUNCTION bf;
701 if (!hdc) break;
703 oldbmp = SelectObject(hdc, fill->bmp);
705 bf.BlendOp = AC_SRC_OVER;
706 bf.BlendFlags = 0;
707 bf.SourceConstantAlpha = 255;
708 bf.AlphaFormat = AC_SRC_ALPHA;
710 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
712 SelectObject(hdc, oldbmp);
713 DeleteDC(hdc);
716 break;
718 /* else fall through */
720 default:
721 SelectObject(graphics->hdc, brush->gdibrush);
722 FillPath(graphics->hdc);
723 break;
727 static INT brush_can_fill_pixels(GpBrush *brush)
729 switch (brush->bt)
731 case BrushTypeSolidColor:
732 case BrushTypeHatchFill:
733 case BrushTypeLinearGradient:
734 case BrushTypeTextureFill:
735 return 1;
736 default:
737 return 0;
741 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
742 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
744 switch (brush->bt)
746 case BrushTypeSolidColor:
748 int x, y;
749 GpSolidFill *fill = (GpSolidFill*)brush;
750 for (x=0; x<fill_area->Width; x++)
751 for (y=0; y<fill_area->Height; y++)
752 argb_pixels[x + y*cdwStride] = fill->color;
753 return Ok;
755 case BrushTypeHatchFill:
757 int x, y;
758 GpHatch *fill = (GpHatch*)brush;
759 const char *hatch_data;
761 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
762 return NotImplemented;
764 for (x=0; x<fill_area->Width; x++)
765 for (y=0; y<fill_area->Height; y++)
767 int hx, hy;
769 /* FIXME: Account for the rendering origin */
770 hx = (x + fill_area->X) % 8;
771 hy = (y + fill_area->Y) % 8;
773 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
774 argb_pixels[x + y*cdwStride] = fill->forecol;
775 else
776 argb_pixels[x + y*cdwStride] = fill->backcol;
779 return Ok;
781 case BrushTypeLinearGradient:
783 GpLineGradient *fill = (GpLineGradient*)brush;
784 GpPointF draw_points[3], line_points[3];
785 GpStatus stat;
786 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
787 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
788 int x, y;
790 draw_points[0].X = fill_area->X;
791 draw_points[0].Y = fill_area->Y;
792 draw_points[1].X = fill_area->X+1;
793 draw_points[1].Y = fill_area->Y;
794 draw_points[2].X = fill_area->X;
795 draw_points[2].Y = fill_area->Y+1;
797 /* Transform the points to a co-ordinate space where X is the point's
798 * position in the gradient, 0.0 being the start point and 1.0 the
799 * end point. */
800 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
801 CoordinateSpaceDevice, draw_points, 3);
803 if (stat == Ok)
805 line_points[0] = fill->startpoint;
806 line_points[1] = fill->endpoint;
807 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
808 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
810 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
813 if (stat == Ok)
815 stat = GdipInvertMatrix(world_to_gradient);
817 if (stat == Ok)
818 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
820 GdipDeleteMatrix(world_to_gradient);
823 if (stat == Ok)
825 REAL x_delta = draw_points[1].X - draw_points[0].X;
826 REAL y_delta = draw_points[2].X - draw_points[0].X;
828 for (y=0; y<fill_area->Height; y++)
830 for (x=0; x<fill_area->Width; x++)
832 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
834 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
839 return stat;
841 case BrushTypeTextureFill:
843 GpTexture *fill = (GpTexture*)brush;
844 GpPointF draw_points[3];
845 GpStatus stat;
846 GpMatrix *world_to_texture;
847 int x, y;
848 GpBitmap *bitmap;
849 int src_stride;
850 GpRect src_area;
852 if (fill->image->type != ImageTypeBitmap)
854 FIXME("metafile texture brushes not implemented\n");
855 return NotImplemented;
858 bitmap = (GpBitmap*)fill->image;
859 src_stride = sizeof(ARGB) * bitmap->width;
861 src_area.X = src_area.Y = 0;
862 src_area.Width = bitmap->width;
863 src_area.Height = bitmap->height;
865 draw_points[0].X = fill_area->X;
866 draw_points[0].Y = fill_area->Y;
867 draw_points[1].X = fill_area->X+1;
868 draw_points[1].Y = fill_area->Y;
869 draw_points[2].X = fill_area->X;
870 draw_points[2].Y = fill_area->Y+1;
872 /* Transform the points to the co-ordinate space of the bitmap. */
873 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
874 CoordinateSpaceDevice, draw_points, 3);
876 if (stat == Ok)
878 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
881 if (stat == Ok)
883 stat = GdipInvertMatrix(world_to_texture);
885 if (stat == Ok)
886 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
888 GdipDeleteMatrix(world_to_texture);
891 if (stat == Ok && !fill->bitmap_bits)
893 BitmapData lockeddata;
895 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
896 if (!fill->bitmap_bits)
897 stat = OutOfMemory;
899 if (stat == Ok)
901 lockeddata.Width = bitmap->width;
902 lockeddata.Height = bitmap->height;
903 lockeddata.Stride = src_stride;
904 lockeddata.PixelFormat = PixelFormat32bppARGB;
905 lockeddata.Scan0 = fill->bitmap_bits;
907 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
908 PixelFormat32bppARGB, &lockeddata);
911 if (stat == Ok)
912 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
914 if (stat == Ok)
915 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
916 bitmap->width, bitmap->height,
917 src_stride, ColorAdjustTypeBitmap);
919 if (stat != Ok)
921 GdipFree(fill->bitmap_bits);
922 fill->bitmap_bits = NULL;
926 if (stat == Ok)
928 REAL x_dx = draw_points[1].X - draw_points[0].X;
929 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
930 REAL y_dx = draw_points[2].X - draw_points[0].X;
931 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
933 for (y=0; y<fill_area->Height; y++)
935 for (x=0; x<fill_area->Width; x++)
937 GpPointF point;
938 point.X = draw_points[0].X + x * x_dx + y * y_dx;
939 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
941 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
942 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
943 &point, fill->imageattributes, graphics->interpolation);
948 return stat;
950 default:
951 return NotImplemented;
955 /* GdipDrawPie/GdipFillPie helper function */
956 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
957 REAL height, REAL startAngle, REAL sweepAngle)
959 GpPointF ptf[4];
960 POINT pti[4];
962 ptf[0].X = x;
963 ptf[0].Y = y;
964 ptf[1].X = x + width;
965 ptf[1].Y = y + height;
967 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
968 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
970 transform_and_round_points(graphics, pti, ptf, 4);
972 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
973 pti[2].y, pti[3].x, pti[3].y);
976 /* Draws the linecap the specified color and size on the hdc. The linecap is in
977 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
978 * should not be called on an hdc that has a path you care about. */
979 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
980 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
982 HGDIOBJ oldbrush = NULL, oldpen = NULL;
983 GpMatrix *matrix = NULL;
984 HBRUSH brush = NULL;
985 HPEN pen = NULL;
986 PointF ptf[4], *custptf = NULL;
987 POINT pt[4], *custpt = NULL;
988 BYTE *tp = NULL;
989 REAL theta, dsmall, dbig, dx, dy = 0.0;
990 INT i, count;
991 LOGBRUSH lb;
992 BOOL customstroke;
994 if((x1 == x2) && (y1 == y2))
995 return;
997 theta = gdiplus_atan2(y2 - y1, x2 - x1);
999 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1000 if(!customstroke){
1001 brush = CreateSolidBrush(color);
1002 lb.lbStyle = BS_SOLID;
1003 lb.lbColor = color;
1004 lb.lbHatch = 0;
1005 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1006 PS_JOIN_MITER, 1, &lb, 0,
1007 NULL);
1008 oldbrush = SelectObject(graphics->hdc, brush);
1009 oldpen = SelectObject(graphics->hdc, pen);
1012 switch(cap){
1013 case LineCapFlat:
1014 break;
1015 case LineCapSquare:
1016 case LineCapSquareAnchor:
1017 case LineCapDiamondAnchor:
1018 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1019 if(cap == LineCapDiamondAnchor){
1020 dsmall = cos(theta + M_PI_2) * size;
1021 dbig = sin(theta + M_PI_2) * size;
1023 else{
1024 dsmall = cos(theta + M_PI_4) * size;
1025 dbig = sin(theta + M_PI_4) * size;
1028 ptf[0].X = x2 - dsmall;
1029 ptf[1].X = x2 + dbig;
1031 ptf[0].Y = y2 - dbig;
1032 ptf[3].Y = y2 + dsmall;
1034 ptf[1].Y = y2 - dsmall;
1035 ptf[2].Y = y2 + dbig;
1037 ptf[3].X = x2 - dbig;
1038 ptf[2].X = x2 + dsmall;
1040 transform_and_round_points(graphics, pt, ptf, 4);
1041 Polygon(graphics->hdc, pt, 4);
1043 break;
1044 case LineCapArrowAnchor:
1045 size = size * 4.0 / sqrt(3.0);
1047 dx = cos(M_PI / 6.0 + theta) * size;
1048 dy = sin(M_PI / 6.0 + theta) * size;
1050 ptf[0].X = x2 - dx;
1051 ptf[0].Y = y2 - dy;
1053 dx = cos(- M_PI / 6.0 + theta) * size;
1054 dy = sin(- M_PI / 6.0 + theta) * size;
1056 ptf[1].X = x2 - dx;
1057 ptf[1].Y = y2 - dy;
1059 ptf[2].X = x2;
1060 ptf[2].Y = y2;
1062 transform_and_round_points(graphics, pt, ptf, 3);
1063 Polygon(graphics->hdc, pt, 3);
1065 break;
1066 case LineCapRoundAnchor:
1067 dx = dy = ANCHOR_WIDTH * size / 2.0;
1069 ptf[0].X = x2 - dx;
1070 ptf[0].Y = y2 - dy;
1071 ptf[1].X = x2 + dx;
1072 ptf[1].Y = y2 + dy;
1074 transform_and_round_points(graphics, pt, ptf, 2);
1075 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1077 break;
1078 case LineCapTriangle:
1079 size = size / 2.0;
1080 dx = cos(M_PI_2 + theta) * size;
1081 dy = sin(M_PI_2 + theta) * size;
1083 ptf[0].X = x2 - dx;
1084 ptf[0].Y = y2 - dy;
1085 ptf[1].X = x2 + dx;
1086 ptf[1].Y = y2 + dy;
1088 dx = cos(theta) * size;
1089 dy = sin(theta) * size;
1091 ptf[2].X = x2 + dx;
1092 ptf[2].Y = y2 + dy;
1094 transform_and_round_points(graphics, pt, ptf, 3);
1095 Polygon(graphics->hdc, pt, 3);
1097 break;
1098 case LineCapRound:
1099 dx = dy = size / 2.0;
1101 ptf[0].X = x2 - dx;
1102 ptf[0].Y = y2 - dy;
1103 ptf[1].X = x2 + dx;
1104 ptf[1].Y = y2 + dy;
1106 dx = -cos(M_PI_2 + theta) * size;
1107 dy = -sin(M_PI_2 + theta) * size;
1109 ptf[2].X = x2 - dx;
1110 ptf[2].Y = y2 - dy;
1111 ptf[3].X = x2 + dx;
1112 ptf[3].Y = y2 + dy;
1114 transform_and_round_points(graphics, pt, ptf, 4);
1115 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1116 pt[2].y, pt[3].x, pt[3].y);
1118 break;
1119 case LineCapCustom:
1120 if(!custom)
1121 break;
1123 count = custom->pathdata.Count;
1124 custptf = GdipAlloc(count * sizeof(PointF));
1125 custpt = GdipAlloc(count * sizeof(POINT));
1126 tp = GdipAlloc(count);
1128 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1129 goto custend;
1131 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1133 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1134 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1135 MatrixOrderAppend);
1136 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1137 GdipTransformMatrixPoints(matrix, custptf, count);
1139 transform_and_round_points(graphics, custpt, custptf, count);
1141 for(i = 0; i < count; i++)
1142 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1144 if(custom->fill){
1145 BeginPath(graphics->hdc);
1146 PolyDraw(graphics->hdc, custpt, tp, count);
1147 EndPath(graphics->hdc);
1148 StrokeAndFillPath(graphics->hdc);
1150 else
1151 PolyDraw(graphics->hdc, custpt, tp, count);
1153 custend:
1154 GdipFree(custptf);
1155 GdipFree(custpt);
1156 GdipFree(tp);
1157 GdipDeleteMatrix(matrix);
1158 break;
1159 default:
1160 break;
1163 if(!customstroke){
1164 SelectObject(graphics->hdc, oldbrush);
1165 SelectObject(graphics->hdc, oldpen);
1166 DeleteObject(brush);
1167 DeleteObject(pen);
1171 /* Shortens the line by the given percent by changing x2, y2.
1172 * If percent is > 1.0 then the line will change direction.
1173 * If percent is negative it can lengthen the line. */
1174 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1176 REAL dist, theta, dx, dy;
1178 if((y1 == *y2) && (x1 == *x2))
1179 return;
1181 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1182 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1183 dx = cos(theta) * dist;
1184 dy = sin(theta) * dist;
1186 *x2 = *x2 + dx;
1187 *y2 = *y2 + dy;
1190 /* Shortens the line by the given amount by changing x2, y2.
1191 * If the amount is greater than the distance, the line will become length 0.
1192 * If the amount is negative, it can lengthen the line. */
1193 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1195 REAL dx, dy, percent;
1197 dx = *x2 - x1;
1198 dy = *y2 - y1;
1199 if(dx == 0 && dy == 0)
1200 return;
1202 percent = amt / sqrt(dx * dx + dy * dy);
1203 if(percent >= 1.0){
1204 *x2 = x1;
1205 *y2 = y1;
1206 return;
1209 shorten_line_percent(x1, y1, x2, y2, percent);
1212 /* Draws lines between the given points, and if caps is true then draws an endcap
1213 * at the end of the last line. */
1214 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1215 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1217 POINT *pti = NULL;
1218 GpPointF *ptcopy = NULL;
1219 GpStatus status = GenericError;
1221 if(!count)
1222 return Ok;
1224 pti = GdipAlloc(count * sizeof(POINT));
1225 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1227 if(!pti || !ptcopy){
1228 status = OutOfMemory;
1229 goto end;
1232 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1234 if(caps){
1235 if(pen->endcap == LineCapArrowAnchor)
1236 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1237 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1238 else if((pen->endcap == LineCapCustom) && pen->customend)
1239 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1240 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1241 pen->customend->inset * pen->width);
1243 if(pen->startcap == LineCapArrowAnchor)
1244 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1245 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1246 else if((pen->startcap == LineCapCustom) && pen->customstart)
1247 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1248 &ptcopy[0].X, &ptcopy[0].Y,
1249 pen->customstart->inset * pen->width);
1251 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1252 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1253 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1254 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1257 transform_and_round_points(graphics, pti, ptcopy, count);
1259 if(Polyline(graphics->hdc, pti, count))
1260 status = Ok;
1262 end:
1263 GdipFree(pti);
1264 GdipFree(ptcopy);
1266 return status;
1269 /* Conducts a linear search to find the bezier points that will back off
1270 * the endpoint of the curve by a distance of amt. Linear search works
1271 * better than binary in this case because there are multiple solutions,
1272 * and binary searches often find a bad one. I don't think this is what
1273 * Windows does but short of rendering the bezier without GDI's help it's
1274 * the best we can do. If rev then work from the start of the passed points
1275 * instead of the end. */
1276 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1278 GpPointF origpt[4];
1279 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1280 INT i, first = 0, second = 1, third = 2, fourth = 3;
1282 if(rev){
1283 first = 3;
1284 second = 2;
1285 third = 1;
1286 fourth = 0;
1289 origx = pt[fourth].X;
1290 origy = pt[fourth].Y;
1291 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1293 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1294 /* reset bezier points to original values */
1295 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1296 /* Perform magic on bezier points. Order is important here.*/
1297 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1298 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1299 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1300 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1301 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1302 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1304 dx = pt[fourth].X - origx;
1305 dy = pt[fourth].Y - origy;
1307 diff = sqrt(dx * dx + dy * dy);
1308 percent += 0.0005 * amt;
1312 /* Draws bezier curves between given points, and if caps is true then draws an
1313 * endcap at the end of the last line. */
1314 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1315 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1317 POINT *pti;
1318 GpPointF *ptcopy;
1319 GpStatus status = GenericError;
1321 if(!count)
1322 return Ok;
1324 pti = GdipAlloc(count * sizeof(POINT));
1325 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1327 if(!pti || !ptcopy){
1328 status = OutOfMemory;
1329 goto end;
1332 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1334 if(caps){
1335 if(pen->endcap == LineCapArrowAnchor)
1336 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1337 else if((pen->endcap == LineCapCustom) && pen->customend)
1338 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1339 FALSE);
1341 if(pen->startcap == LineCapArrowAnchor)
1342 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1343 else if((pen->startcap == LineCapCustom) && pen->customstart)
1344 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1346 /* the direction of the line cap is parallel to the direction at the
1347 * end of the bezier (which, if it has been shortened, is not the same
1348 * as the direction from pt[count-2] to pt[count-1]) */
1349 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1350 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1351 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1352 pt[count - 1].X, pt[count - 1].Y);
1354 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1355 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1356 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1359 transform_and_round_points(graphics, pti, ptcopy, count);
1361 PolyBezier(graphics->hdc, pti, count);
1363 status = Ok;
1365 end:
1366 GdipFree(pti);
1367 GdipFree(ptcopy);
1369 return status;
1372 /* Draws a combination of bezier curves and lines between points. */
1373 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1374 GDIPCONST BYTE * types, INT count, BOOL caps)
1376 POINT *pti = GdipAlloc(count * sizeof(POINT));
1377 BYTE *tp = GdipAlloc(count);
1378 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1379 INT i, j;
1380 GpStatus status = GenericError;
1382 if(!count){
1383 status = Ok;
1384 goto end;
1386 if(!pti || !tp || !ptcopy){
1387 status = OutOfMemory;
1388 goto end;
1391 for(i = 1; i < count; i++){
1392 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1393 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1394 || !(types[i + 1] & PathPointTypeBezier)){
1395 ERR("Bad bezier points\n");
1396 goto end;
1398 i += 2;
1402 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1404 /* If we are drawing caps, go through the points and adjust them accordingly,
1405 * and draw the caps. */
1406 if(caps){
1407 switch(types[count - 1] & PathPointTypePathTypeMask){
1408 case PathPointTypeBezier:
1409 if(pen->endcap == LineCapArrowAnchor)
1410 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1411 else if((pen->endcap == LineCapCustom) && pen->customend)
1412 shorten_bezier_amt(&ptcopy[count - 4],
1413 pen->width * pen->customend->inset, FALSE);
1415 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1416 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1417 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1418 pt[count - 1].X, pt[count - 1].Y);
1420 break;
1421 case PathPointTypeLine:
1422 if(pen->endcap == LineCapArrowAnchor)
1423 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1424 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1425 pen->width);
1426 else if((pen->endcap == LineCapCustom) && pen->customend)
1427 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1428 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1429 pen->customend->inset * pen->width);
1431 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1432 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1433 pt[count - 1].Y);
1435 break;
1436 default:
1437 ERR("Bad path last point\n");
1438 goto end;
1441 /* Find start of points */
1442 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1443 == PathPointTypeStart); j++);
1445 switch(types[j] & PathPointTypePathTypeMask){
1446 case PathPointTypeBezier:
1447 if(pen->startcap == LineCapArrowAnchor)
1448 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1449 else if((pen->startcap == LineCapCustom) && pen->customstart)
1450 shorten_bezier_amt(&ptcopy[j - 1],
1451 pen->width * pen->customstart->inset, TRUE);
1453 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1454 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1455 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1456 pt[j - 1].X, pt[j - 1].Y);
1458 break;
1459 case PathPointTypeLine:
1460 if(pen->startcap == LineCapArrowAnchor)
1461 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1462 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1463 pen->width);
1464 else if((pen->startcap == LineCapCustom) && pen->customstart)
1465 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1466 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1467 pen->customstart->inset * pen->width);
1469 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1470 pt[j].X, pt[j].Y, pt[j - 1].X,
1471 pt[j - 1].Y);
1473 break;
1474 default:
1475 ERR("Bad path points\n");
1476 goto end;
1480 transform_and_round_points(graphics, pti, ptcopy, count);
1482 for(i = 0; i < count; i++){
1483 tp[i] = convert_path_point_type(types[i]);
1486 PolyDraw(graphics->hdc, pti, tp, count);
1488 status = Ok;
1490 end:
1491 GdipFree(pti);
1492 GdipFree(ptcopy);
1493 GdipFree(tp);
1495 return status;
1498 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1500 GpStatus result;
1502 BeginPath(graphics->hdc);
1503 result = draw_poly(graphics, NULL, path->pathdata.Points,
1504 path->pathdata.Types, path->pathdata.Count, FALSE);
1505 EndPath(graphics->hdc);
1506 return result;
1509 typedef struct _GraphicsContainerItem {
1510 struct list entry;
1511 GraphicsContainer contid;
1513 SmoothingMode smoothing;
1514 CompositingQuality compqual;
1515 InterpolationMode interpolation;
1516 CompositingMode compmode;
1517 TextRenderingHint texthint;
1518 REAL scale;
1519 GpUnit unit;
1520 PixelOffsetMode pixeloffset;
1521 UINT textcontrast;
1522 GpMatrix* worldtrans;
1523 GpRegion* clip;
1524 } GraphicsContainerItem;
1526 static GpStatus init_container(GraphicsContainerItem** container,
1527 GDIPCONST GpGraphics* graphics){
1528 GpStatus sts;
1530 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1531 if(!(*container))
1532 return OutOfMemory;
1534 (*container)->contid = graphics->contid + 1;
1536 (*container)->smoothing = graphics->smoothing;
1537 (*container)->compqual = graphics->compqual;
1538 (*container)->interpolation = graphics->interpolation;
1539 (*container)->compmode = graphics->compmode;
1540 (*container)->texthint = graphics->texthint;
1541 (*container)->scale = graphics->scale;
1542 (*container)->unit = graphics->unit;
1543 (*container)->textcontrast = graphics->textcontrast;
1544 (*container)->pixeloffset = graphics->pixeloffset;
1546 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1547 if(sts != Ok){
1548 GdipFree(*container);
1549 *container = NULL;
1550 return sts;
1553 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1554 if(sts != Ok){
1555 GdipDeleteMatrix((*container)->worldtrans);
1556 GdipFree(*container);
1557 *container = NULL;
1558 return sts;
1561 return Ok;
1564 static void delete_container(GraphicsContainerItem* container){
1565 GdipDeleteMatrix(container->worldtrans);
1566 GdipDeleteRegion(container->clip);
1567 GdipFree(container);
1570 static GpStatus restore_container(GpGraphics* graphics,
1571 GDIPCONST GraphicsContainerItem* container){
1572 GpStatus sts;
1573 GpMatrix *newTrans;
1574 GpRegion *newClip;
1576 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1577 if(sts != Ok)
1578 return sts;
1580 sts = GdipCloneRegion(container->clip, &newClip);
1581 if(sts != Ok){
1582 GdipDeleteMatrix(newTrans);
1583 return sts;
1586 GdipDeleteMatrix(graphics->worldtrans);
1587 graphics->worldtrans = newTrans;
1589 GdipDeleteRegion(graphics->clip);
1590 graphics->clip = newClip;
1592 graphics->contid = container->contid - 1;
1594 graphics->smoothing = container->smoothing;
1595 graphics->compqual = container->compqual;
1596 graphics->interpolation = container->interpolation;
1597 graphics->compmode = container->compmode;
1598 graphics->texthint = container->texthint;
1599 graphics->scale = container->scale;
1600 graphics->unit = container->unit;
1601 graphics->textcontrast = container->textcontrast;
1602 graphics->pixeloffset = container->pixeloffset;
1604 return Ok;
1607 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1609 RECT wnd_rect;
1610 GpStatus stat=Ok;
1611 GpUnit unit;
1613 if(graphics->hwnd) {
1614 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1615 return GenericError;
1617 rect->X = wnd_rect.left;
1618 rect->Y = wnd_rect.top;
1619 rect->Width = wnd_rect.right - wnd_rect.left;
1620 rect->Height = wnd_rect.bottom - wnd_rect.top;
1621 }else if (graphics->image){
1622 stat = GdipGetImageBounds(graphics->image, rect, &unit);
1623 if (stat == Ok && unit != UnitPixel)
1624 FIXME("need to convert from unit %i\n", unit);
1625 }else{
1626 rect->X = 0;
1627 rect->Y = 0;
1628 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1629 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1632 return stat;
1635 /* on success, rgn will contain the region of the graphics object which
1636 * is visible after clipping has been applied */
1637 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1639 GpStatus stat;
1640 GpRectF rectf;
1641 GpRegion* tmp;
1643 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1644 return stat;
1646 if((stat = GdipCreateRegion(&tmp)) != Ok)
1647 return stat;
1649 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1650 goto end;
1652 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1653 goto end;
1655 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1657 end:
1658 GdipDeleteRegion(tmp);
1659 return stat;
1662 void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font, HFONT *hfont)
1664 HDC hdc = CreateCompatibleDC(0);
1665 GpPointF pt[3];
1666 REAL angle, rel_width, rel_height;
1667 LOGFONTW lfw;
1668 HFONT unscaled_font;
1669 TEXTMETRICW textmet;
1671 pt[0].X = 0.0;
1672 pt[0].Y = 0.0;
1673 pt[1].X = 1.0;
1674 pt[1].Y = 0.0;
1675 pt[2].X = 0.0;
1676 pt[2].Y = 1.0;
1677 if (graphics)
1678 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
1679 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1680 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
1681 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
1682 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
1683 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
1685 lfw = font->lfw;
1686 lfw.lfHeight = roundr(-font->pixel_size * rel_height);
1687 unscaled_font = CreateFontIndirectW(&lfw);
1689 SelectObject(hdc, unscaled_font);
1690 GetTextMetricsW(hdc, &textmet);
1692 lfw = font->lfw;
1693 lfw.lfHeight = roundr(-font->pixel_size * rel_height);
1694 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width / rel_height);
1695 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
1697 *hfont = CreateFontIndirectW(&lfw);
1699 DeleteDC(hdc);
1700 DeleteObject(unscaled_font);
1703 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1705 TRACE("(%p, %p)\n", hdc, graphics);
1707 return GdipCreateFromHDC2(hdc, NULL, graphics);
1710 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1712 GpStatus retval;
1714 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1716 if(hDevice != NULL) {
1717 FIXME("Don't know how to handle parameter hDevice\n");
1718 return NotImplemented;
1721 if(hdc == NULL)
1722 return OutOfMemory;
1724 if(graphics == NULL)
1725 return InvalidParameter;
1727 *graphics = GdipAlloc(sizeof(GpGraphics));
1728 if(!*graphics) return OutOfMemory;
1730 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1731 GdipFree(*graphics);
1732 return retval;
1735 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1736 GdipFree((*graphics)->worldtrans);
1737 GdipFree(*graphics);
1738 return retval;
1741 (*graphics)->hdc = hdc;
1742 (*graphics)->hwnd = WindowFromDC(hdc);
1743 (*graphics)->owndc = FALSE;
1744 (*graphics)->smoothing = SmoothingModeDefault;
1745 (*graphics)->compqual = CompositingQualityDefault;
1746 (*graphics)->interpolation = InterpolationModeBilinear;
1747 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1748 (*graphics)->compmode = CompositingModeSourceOver;
1749 (*graphics)->unit = UnitDisplay;
1750 (*graphics)->scale = 1.0;
1751 (*graphics)->busy = FALSE;
1752 (*graphics)->textcontrast = 4;
1753 list_init(&(*graphics)->containers);
1754 (*graphics)->contid = 0;
1756 TRACE("<-- %p\n", *graphics);
1758 return Ok;
1761 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
1763 GpStatus retval;
1765 *graphics = GdipAlloc(sizeof(GpGraphics));
1766 if(!*graphics) return OutOfMemory;
1768 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1769 GdipFree(*graphics);
1770 return retval;
1773 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1774 GdipFree((*graphics)->worldtrans);
1775 GdipFree(*graphics);
1776 return retval;
1779 (*graphics)->hdc = NULL;
1780 (*graphics)->hwnd = NULL;
1781 (*graphics)->owndc = FALSE;
1782 (*graphics)->image = image;
1783 (*graphics)->smoothing = SmoothingModeDefault;
1784 (*graphics)->compqual = CompositingQualityDefault;
1785 (*graphics)->interpolation = InterpolationModeBilinear;
1786 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1787 (*graphics)->compmode = CompositingModeSourceOver;
1788 (*graphics)->unit = UnitDisplay;
1789 (*graphics)->scale = 1.0;
1790 (*graphics)->busy = FALSE;
1791 (*graphics)->textcontrast = 4;
1792 list_init(&(*graphics)->containers);
1793 (*graphics)->contid = 0;
1795 TRACE("<-- %p\n", *graphics);
1797 return Ok;
1800 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1802 GpStatus ret;
1803 HDC hdc;
1805 TRACE("(%p, %p)\n", hwnd, graphics);
1807 hdc = GetDC(hwnd);
1809 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1811 ReleaseDC(hwnd, hdc);
1812 return ret;
1815 (*graphics)->hwnd = hwnd;
1816 (*graphics)->owndc = TRUE;
1818 return Ok;
1821 /* FIXME: no icm handling */
1822 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1824 TRACE("(%p, %p)\n", hwnd, graphics);
1826 return GdipCreateFromHWND(hwnd, graphics);
1829 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1830 GpMetafile **metafile)
1832 static int calls;
1834 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1836 if(!hemf || !metafile)
1837 return InvalidParameter;
1839 if(!(calls++))
1840 FIXME("not implemented\n");
1842 return NotImplemented;
1845 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1846 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1848 IStream *stream = NULL;
1849 UINT read;
1850 BYTE* copy;
1851 HENHMETAFILE hemf;
1852 GpStatus retval = Ok;
1854 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1856 if(!hwmf || !metafile || !placeable)
1857 return InvalidParameter;
1859 *metafile = NULL;
1860 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1861 if(!read)
1862 return GenericError;
1863 copy = GdipAlloc(read);
1864 GetMetaFileBitsEx(hwmf, read, copy);
1866 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1867 GdipFree(copy);
1869 read = GetEnhMetaFileBits(hemf, 0, NULL);
1870 copy = GdipAlloc(read);
1871 GetEnhMetaFileBits(hemf, read, copy);
1872 DeleteEnhMetaFile(hemf);
1874 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1875 ERR("could not make stream\n");
1876 GdipFree(copy);
1877 retval = GenericError;
1878 goto err;
1881 *metafile = GdipAlloc(sizeof(GpMetafile));
1882 if(!*metafile){
1883 retval = OutOfMemory;
1884 goto err;
1887 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1888 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1890 retval = GenericError;
1891 goto err;
1895 (*metafile)->image.type = ImageTypeMetafile;
1896 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1897 (*metafile)->image.palette_flags = 0;
1898 (*metafile)->image.palette_count = 0;
1899 (*metafile)->image.palette_size = 0;
1900 (*metafile)->image.palette_entries = NULL;
1901 (*metafile)->image.xres = (REAL)placeable->Inch;
1902 (*metafile)->image.yres = (REAL)placeable->Inch;
1903 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1904 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1905 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1906 - placeable->BoundingBox.Left));
1907 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1908 - placeable->BoundingBox.Top));
1909 (*metafile)->unit = UnitPixel;
1911 if(delete)
1912 DeleteMetaFile(hwmf);
1914 TRACE("<-- %p\n", *metafile);
1916 err:
1917 if (retval != Ok)
1918 GdipFree(*metafile);
1919 IStream_Release(stream);
1920 return retval;
1923 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1924 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1926 HMETAFILE hmf = GetMetaFileW(file);
1928 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1930 if(!hmf) return InvalidParameter;
1932 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1935 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1936 GpMetafile **metafile)
1938 FIXME("(%p, %p): stub\n", file, metafile);
1939 return NotImplemented;
1942 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1943 GpMetafile **metafile)
1945 FIXME("(%p, %p): stub\n", stream, metafile);
1946 return NotImplemented;
1949 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1950 UINT access, IStream **stream)
1952 DWORD dwMode;
1953 HRESULT ret;
1955 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1957 if(!stream || !filename)
1958 return InvalidParameter;
1960 if(access & GENERIC_WRITE)
1961 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1962 else if(access & GENERIC_READ)
1963 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1964 else
1965 return InvalidParameter;
1967 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1969 return hresult_to_status(ret);
1972 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1974 GraphicsContainerItem *cont, *next;
1975 GpStatus stat;
1976 TRACE("(%p)\n", graphics);
1978 if(!graphics) return InvalidParameter;
1979 if(graphics->busy) return ObjectBusy;
1981 if (graphics->image && graphics->image->type == ImageTypeMetafile)
1983 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
1984 if (stat != Ok)
1985 return stat;
1988 if(graphics->owndc)
1989 ReleaseDC(graphics->hwnd, graphics->hdc);
1991 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1992 list_remove(&cont->entry);
1993 delete_container(cont);
1996 GdipDeleteRegion(graphics->clip);
1997 GdipDeleteMatrix(graphics->worldtrans);
1998 GdipFree(graphics);
2000 return Ok;
2003 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2004 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2006 INT save_state, num_pts;
2007 GpPointF points[MAX_ARC_PTS];
2008 GpStatus retval;
2010 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2011 width, height, startAngle, sweepAngle);
2013 if(!graphics || !pen || width <= 0 || height <= 0)
2014 return InvalidParameter;
2016 if(graphics->busy)
2017 return ObjectBusy;
2019 if (!graphics->hdc)
2021 FIXME("graphics object has no HDC\n");
2022 return Ok;
2025 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2027 save_state = prepare_dc(graphics, pen);
2029 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2031 restore_dc(graphics, save_state);
2033 return retval;
2036 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2037 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2039 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2040 width, height, startAngle, sweepAngle);
2042 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2045 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2046 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2048 INT save_state;
2049 GpPointF pt[4];
2050 GpStatus retval;
2052 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2053 x2, y2, x3, y3, x4, y4);
2055 if(!graphics || !pen)
2056 return InvalidParameter;
2058 if(graphics->busy)
2059 return ObjectBusy;
2061 if (!graphics->hdc)
2063 FIXME("graphics object has no HDC\n");
2064 return Ok;
2067 pt[0].X = x1;
2068 pt[0].Y = y1;
2069 pt[1].X = x2;
2070 pt[1].Y = y2;
2071 pt[2].X = x3;
2072 pt[2].Y = y3;
2073 pt[3].X = x4;
2074 pt[3].Y = y4;
2076 save_state = prepare_dc(graphics, pen);
2078 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2080 restore_dc(graphics, save_state);
2082 return retval;
2085 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2086 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2088 INT save_state;
2089 GpPointF pt[4];
2090 GpStatus retval;
2092 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2093 x2, y2, x3, y3, x4, y4);
2095 if(!graphics || !pen)
2096 return InvalidParameter;
2098 if(graphics->busy)
2099 return ObjectBusy;
2101 if (!graphics->hdc)
2103 FIXME("graphics object has no HDC\n");
2104 return Ok;
2107 pt[0].X = x1;
2108 pt[0].Y = y1;
2109 pt[1].X = x2;
2110 pt[1].Y = y2;
2111 pt[2].X = x3;
2112 pt[2].Y = y3;
2113 pt[3].X = x4;
2114 pt[3].Y = y4;
2116 save_state = prepare_dc(graphics, pen);
2118 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2120 restore_dc(graphics, save_state);
2122 return retval;
2125 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2126 GDIPCONST GpPointF *points, INT count)
2128 INT i;
2129 GpStatus ret;
2131 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2133 if(!graphics || !pen || !points || (count <= 0))
2134 return InvalidParameter;
2136 if(graphics->busy)
2137 return ObjectBusy;
2139 for(i = 0; i < floor(count / 4); i++){
2140 ret = GdipDrawBezier(graphics, pen,
2141 points[4*i].X, points[4*i].Y,
2142 points[4*i + 1].X, points[4*i + 1].Y,
2143 points[4*i + 2].X, points[4*i + 2].Y,
2144 points[4*i + 3].X, points[4*i + 3].Y);
2145 if(ret != Ok)
2146 return ret;
2149 return Ok;
2152 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2153 GDIPCONST GpPoint *points, INT count)
2155 GpPointF *pts;
2156 GpStatus ret;
2157 INT i;
2159 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2161 if(!graphics || !pen || !points || (count <= 0))
2162 return InvalidParameter;
2164 if(graphics->busy)
2165 return ObjectBusy;
2167 pts = GdipAlloc(sizeof(GpPointF) * count);
2168 if(!pts)
2169 return OutOfMemory;
2171 for(i = 0; i < count; i++){
2172 pts[i].X = (REAL)points[i].X;
2173 pts[i].Y = (REAL)points[i].Y;
2176 ret = GdipDrawBeziers(graphics,pen,pts,count);
2178 GdipFree(pts);
2180 return ret;
2183 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2184 GDIPCONST GpPointF *points, INT count)
2186 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2188 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2191 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2192 GDIPCONST GpPoint *points, INT count)
2194 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2196 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2199 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2200 GDIPCONST GpPointF *points, INT count, REAL tension)
2202 GpPath *path;
2203 GpStatus stat;
2205 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2207 if(!graphics || !pen || !points || count <= 0)
2208 return InvalidParameter;
2210 if(graphics->busy)
2211 return ObjectBusy;
2213 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2214 return stat;
2216 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2217 if(stat != Ok){
2218 GdipDeletePath(path);
2219 return stat;
2222 stat = GdipDrawPath(graphics, pen, path);
2224 GdipDeletePath(path);
2226 return stat;
2229 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2230 GDIPCONST GpPoint *points, INT count, REAL tension)
2232 GpPointF *ptf;
2233 GpStatus stat;
2234 INT i;
2236 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2238 if(!points || count <= 0)
2239 return InvalidParameter;
2241 ptf = GdipAlloc(sizeof(GpPointF)*count);
2242 if(!ptf)
2243 return OutOfMemory;
2245 for(i = 0; i < count; i++){
2246 ptf[i].X = (REAL)points[i].X;
2247 ptf[i].Y = (REAL)points[i].Y;
2250 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2252 GdipFree(ptf);
2254 return stat;
2257 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2258 GDIPCONST GpPointF *points, INT count)
2260 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2262 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2265 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2266 GDIPCONST GpPoint *points, INT count)
2268 GpPointF *pointsF;
2269 GpStatus ret;
2270 INT i;
2272 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2274 if(!points)
2275 return InvalidParameter;
2277 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2278 if(!pointsF)
2279 return OutOfMemory;
2281 for(i = 0; i < count; i++){
2282 pointsF[i].X = (REAL)points[i].X;
2283 pointsF[i].Y = (REAL)points[i].Y;
2286 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2287 GdipFree(pointsF);
2289 return ret;
2292 /* Approximates cardinal spline with Bezier curves. */
2293 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2294 GDIPCONST GpPointF *points, INT count, REAL tension)
2296 /* PolyBezier expects count*3-2 points. */
2297 INT i, len_pt = count*3-2, save_state;
2298 GpPointF *pt;
2299 REAL x1, x2, y1, y2;
2300 GpStatus retval;
2302 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2304 if(!graphics || !pen)
2305 return InvalidParameter;
2307 if(graphics->busy)
2308 return ObjectBusy;
2310 if(count < 2)
2311 return InvalidParameter;
2313 if (!graphics->hdc)
2315 FIXME("graphics object has no HDC\n");
2316 return Ok;
2319 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2320 if(!pt)
2321 return OutOfMemory;
2323 tension = tension * TENSION_CONST;
2325 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2326 tension, &x1, &y1);
2328 pt[0].X = points[0].X;
2329 pt[0].Y = points[0].Y;
2330 pt[1].X = x1;
2331 pt[1].Y = y1;
2333 for(i = 0; i < count-2; i++){
2334 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2336 pt[3*i+2].X = x1;
2337 pt[3*i+2].Y = y1;
2338 pt[3*i+3].X = points[i+1].X;
2339 pt[3*i+3].Y = points[i+1].Y;
2340 pt[3*i+4].X = x2;
2341 pt[3*i+4].Y = y2;
2344 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2345 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2347 pt[len_pt-2].X = x1;
2348 pt[len_pt-2].Y = y1;
2349 pt[len_pt-1].X = points[count-1].X;
2350 pt[len_pt-1].Y = points[count-1].Y;
2352 save_state = prepare_dc(graphics, pen);
2354 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2356 GdipFree(pt);
2357 restore_dc(graphics, save_state);
2359 return retval;
2362 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2363 GDIPCONST GpPoint *points, INT count, REAL tension)
2365 GpPointF *pointsF;
2366 GpStatus ret;
2367 INT i;
2369 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2371 if(!points)
2372 return InvalidParameter;
2374 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2375 if(!pointsF)
2376 return OutOfMemory;
2378 for(i = 0; i < count; i++){
2379 pointsF[i].X = (REAL)points[i].X;
2380 pointsF[i].Y = (REAL)points[i].Y;
2383 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2384 GdipFree(pointsF);
2386 return ret;
2389 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2390 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2391 REAL tension)
2393 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2395 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2396 return InvalidParameter;
2399 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2402 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2403 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2404 REAL tension)
2406 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2408 if(count < 0){
2409 return OutOfMemory;
2412 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2413 return InvalidParameter;
2416 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2419 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2420 REAL y, REAL width, REAL height)
2422 INT save_state;
2423 GpPointF ptf[2];
2424 POINT pti[2];
2426 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2428 if(!graphics || !pen)
2429 return InvalidParameter;
2431 if(graphics->busy)
2432 return ObjectBusy;
2434 if (!graphics->hdc)
2436 FIXME("graphics object has no HDC\n");
2437 return Ok;
2440 ptf[0].X = x;
2441 ptf[0].Y = y;
2442 ptf[1].X = x + width;
2443 ptf[1].Y = y + height;
2445 save_state = prepare_dc(graphics, pen);
2446 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2448 transform_and_round_points(graphics, pti, ptf, 2);
2450 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2452 restore_dc(graphics, save_state);
2454 return Ok;
2457 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2458 INT y, INT width, INT height)
2460 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2462 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2466 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2468 UINT width, height;
2469 GpPointF points[3];
2471 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2473 if(!graphics || !image)
2474 return InvalidParameter;
2476 GdipGetImageWidth(image, &width);
2477 GdipGetImageHeight(image, &height);
2479 /* FIXME: we should use the graphics and image dpi, somehow */
2481 points[0].X = points[2].X = x;
2482 points[0].Y = points[1].Y = y;
2483 points[1].X = x + width;
2484 points[2].Y = y + height;
2486 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
2487 UnitPixel, NULL, NULL, NULL);
2490 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2491 INT y)
2493 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2495 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2498 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2499 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2500 GpUnit srcUnit)
2502 GpPointF points[3];
2503 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2505 points[0].X = points[2].X = x;
2506 points[0].Y = points[1].Y = y;
2508 /* FIXME: convert image coordinates to Graphics coordinates? */
2509 points[1].X = x + srcwidth;
2510 points[2].Y = y + srcheight;
2512 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2513 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2516 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2517 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2518 GpUnit srcUnit)
2520 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2523 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2524 GDIPCONST GpPointF *dstpoints, INT count)
2526 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2527 return NotImplemented;
2530 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2531 GDIPCONST GpPoint *dstpoints, INT count)
2533 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2534 return NotImplemented;
2537 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2538 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2539 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2540 DrawImageAbort callback, VOID * callbackData)
2542 GpPointF ptf[4];
2543 POINT pti[4];
2544 REAL dx, dy;
2545 GpStatus stat;
2547 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2548 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2549 callbackData);
2551 if (count > 3)
2552 return NotImplemented;
2554 if(!graphics || !image || !points || count != 3)
2555 return InvalidParameter;
2557 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2558 debugstr_pointf(&points[2]));
2560 memcpy(ptf, points, 3 * sizeof(GpPointF));
2561 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2562 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2563 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
2564 return Ok;
2565 transform_and_round_points(graphics, pti, ptf, 4);
2567 if (image->picture)
2569 if (!graphics->hdc)
2571 FIXME("graphics object has no HDC\n");
2574 /* FIXME: partially implemented (only works for rectangular parallelograms) */
2575 if(srcUnit == UnitInch)
2576 dx = dy = (REAL) INCH_HIMETRIC;
2577 else if(srcUnit == UnitPixel){
2578 dx = ((REAL) INCH_HIMETRIC) /
2579 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
2580 dy = ((REAL) INCH_HIMETRIC) /
2581 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
2583 else
2584 return NotImplemented;
2586 if(IPicture_Render(image->picture, graphics->hdc,
2587 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2588 srcx * dx, srcy * dy,
2589 srcwidth * dx, srcheight * dy,
2590 NULL) != S_OK){
2591 if(callback)
2592 callback(callbackData);
2593 return GenericError;
2596 else if (image->type == ImageTypeBitmap)
2598 GpBitmap* bitmap = (GpBitmap*)image;
2599 int use_software=0;
2601 if (srcUnit == UnitInch)
2602 dx = dy = 96.0; /* FIXME: use the image resolution */
2603 else if (srcUnit == UnitPixel)
2604 dx = dy = 1.0;
2605 else
2606 return NotImplemented;
2608 srcx = srcx * dx;
2609 srcy = srcy * dy;
2610 srcwidth = srcwidth * dx;
2611 srcheight = srcheight * dy;
2613 if (imageAttributes ||
2614 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2615 !((GpBitmap*)image)->hbitmap ||
2616 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2617 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2618 srcx < 0 || srcy < 0 ||
2619 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2620 use_software = 1;
2622 if (use_software)
2624 RECT dst_area;
2625 GpRect src_area;
2626 int i, x, y, src_stride, dst_stride;
2627 GpMatrix *dst_to_src;
2628 REAL m11, m12, m21, m22, mdx, mdy;
2629 LPBYTE src_data, dst_data;
2630 BitmapData lockeddata;
2631 InterpolationMode interpolation = graphics->interpolation;
2632 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2633 REAL x_dx, x_dy, y_dx, y_dy;
2634 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2636 if (!imageAttributes)
2637 imageAttributes = &defaultImageAttributes;
2639 dst_area.left = dst_area.right = pti[0].x;
2640 dst_area.top = dst_area.bottom = pti[0].y;
2641 for (i=1; i<4; i++)
2643 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2644 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2645 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2646 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2649 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2650 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2651 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2652 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2653 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2654 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2656 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
2657 if (stat != Ok) return stat;
2659 stat = GdipInvertMatrix(dst_to_src);
2660 if (stat != Ok)
2662 GdipDeleteMatrix(dst_to_src);
2663 return stat;
2666 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2667 if (!dst_data)
2669 GdipDeleteMatrix(dst_to_src);
2670 return OutOfMemory;
2673 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2675 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2676 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2678 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2679 if (!src_data)
2681 GdipFree(dst_data);
2682 GdipDeleteMatrix(dst_to_src);
2683 return OutOfMemory;
2685 src_stride = sizeof(ARGB) * src_area.Width;
2687 /* Read the bits we need from the source bitmap into an ARGB buffer. */
2688 lockeddata.Width = src_area.Width;
2689 lockeddata.Height = src_area.Height;
2690 lockeddata.Stride = src_stride;
2691 lockeddata.PixelFormat = PixelFormat32bppARGB;
2692 lockeddata.Scan0 = src_data;
2694 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
2695 PixelFormat32bppARGB, &lockeddata);
2697 if (stat == Ok)
2698 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2700 if (stat != Ok)
2702 if (src_data != dst_data)
2703 GdipFree(src_data);
2704 GdipFree(dst_data);
2705 GdipDeleteMatrix(dst_to_src);
2706 return OutOfMemory;
2709 apply_image_attributes(imageAttributes, src_data,
2710 src_area.Width, src_area.Height,
2711 src_stride, ColorAdjustTypeBitmap);
2713 /* Transform the bits as needed to the destination. */
2714 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
2716 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
2717 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
2718 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
2719 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
2721 for (x=dst_area.left; x<dst_area.right; x++)
2723 for (y=dst_area.top; y<dst_area.bottom; y++)
2725 GpPointF src_pointf;
2726 ARGB *dst_color;
2728 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
2729 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
2731 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2733 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
2734 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
2735 else
2736 *dst_color = 0;
2740 GdipDeleteMatrix(dst_to_src);
2742 GdipFree(src_data);
2744 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
2745 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
2747 GdipFree(dst_data);
2749 return stat;
2751 else
2753 HDC hdc;
2754 int temp_hdc=0, temp_bitmap=0;
2755 HBITMAP hbitmap, old_hbm=NULL;
2757 if (!(bitmap->format == PixelFormat16bppRGB555 ||
2758 bitmap->format == PixelFormat24bppRGB ||
2759 bitmap->format == PixelFormat32bppRGB ||
2760 bitmap->format == PixelFormat32bppPARGB))
2762 BITMAPINFOHEADER bih;
2763 BYTE *temp_bits;
2764 PixelFormat dst_format;
2766 /* we can't draw a bitmap of this format directly */
2767 hdc = CreateCompatibleDC(0);
2768 temp_hdc = 1;
2769 temp_bitmap = 1;
2771 bih.biSize = sizeof(BITMAPINFOHEADER);
2772 bih.biWidth = bitmap->width;
2773 bih.biHeight = -bitmap->height;
2774 bih.biPlanes = 1;
2775 bih.biBitCount = 32;
2776 bih.biCompression = BI_RGB;
2777 bih.biSizeImage = 0;
2778 bih.biXPelsPerMeter = 0;
2779 bih.biYPelsPerMeter = 0;
2780 bih.biClrUsed = 0;
2781 bih.biClrImportant = 0;
2783 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
2784 (void**)&temp_bits, NULL, 0);
2786 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2787 dst_format = PixelFormat32bppPARGB;
2788 else
2789 dst_format = PixelFormat32bppRGB;
2791 convert_pixels(bitmap->width, bitmap->height,
2792 bitmap->width*4, temp_bits, dst_format,
2793 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
2795 else
2797 hbitmap = bitmap->hbitmap;
2798 hdc = bitmap->hdc;
2799 temp_hdc = (hdc == 0);
2802 if (temp_hdc)
2804 if (!hdc) hdc = CreateCompatibleDC(0);
2805 old_hbm = SelectObject(hdc, hbitmap);
2808 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2810 BLENDFUNCTION bf;
2812 bf.BlendOp = AC_SRC_OVER;
2813 bf.BlendFlags = 0;
2814 bf.SourceConstantAlpha = 255;
2815 bf.AlphaFormat = AC_SRC_ALPHA;
2817 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2818 hdc, srcx, srcy, srcwidth, srcheight, bf);
2820 else
2822 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2823 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
2826 if (temp_hdc)
2828 SelectObject(hdc, old_hbm);
2829 DeleteDC(hdc);
2832 if (temp_bitmap)
2833 DeleteObject(hbitmap);
2836 else
2838 ERR("GpImage with no IPicture or HBITMAP?!\n");
2839 return NotImplemented;
2842 return Ok;
2845 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
2846 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
2847 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2848 DrawImageAbort callback, VOID * callbackData)
2850 GpPointF pointsF[3];
2851 INT i;
2853 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
2854 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2855 callbackData);
2857 if(!points || count!=3)
2858 return InvalidParameter;
2860 for(i = 0; i < count; i++){
2861 pointsF[i].X = (REAL)points[i].X;
2862 pointsF[i].Y = (REAL)points[i].Y;
2865 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2866 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2867 callback, callbackData);
2870 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2871 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2872 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2873 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2874 VOID * callbackData)
2876 GpPointF points[3];
2878 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2879 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2880 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2882 points[0].X = dstx;
2883 points[0].Y = dsty;
2884 points[1].X = dstx + dstwidth;
2885 points[1].Y = dsty;
2886 points[2].X = dstx;
2887 points[2].Y = dsty + dstheight;
2889 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2890 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2893 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2894 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2895 INT srcwidth, INT srcheight, GpUnit srcUnit,
2896 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2897 VOID * callbackData)
2899 GpPointF points[3];
2901 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2902 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2903 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2905 points[0].X = dstx;
2906 points[0].Y = dsty;
2907 points[1].X = dstx + dstwidth;
2908 points[1].Y = dsty;
2909 points[2].X = dstx;
2910 points[2].Y = dsty + dstheight;
2912 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2913 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2916 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2917 REAL x, REAL y, REAL width, REAL height)
2919 RectF bounds;
2920 GpUnit unit;
2921 GpStatus ret;
2923 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2925 if(!graphics || !image)
2926 return InvalidParameter;
2928 ret = GdipGetImageBounds(image, &bounds, &unit);
2929 if(ret != Ok)
2930 return ret;
2932 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2933 bounds.X, bounds.Y, bounds.Width, bounds.Height,
2934 unit, NULL, NULL, NULL);
2937 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2938 INT x, INT y, INT width, INT height)
2940 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2942 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2945 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2946 REAL y1, REAL x2, REAL y2)
2948 INT save_state;
2949 GpPointF pt[2];
2950 GpStatus retval;
2952 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2954 if(!pen || !graphics)
2955 return InvalidParameter;
2957 if(graphics->busy)
2958 return ObjectBusy;
2960 if (!graphics->hdc)
2962 FIXME("graphics object has no HDC\n");
2963 return Ok;
2966 pt[0].X = x1;
2967 pt[0].Y = y1;
2968 pt[1].X = x2;
2969 pt[1].Y = y2;
2971 save_state = prepare_dc(graphics, pen);
2973 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2975 restore_dc(graphics, save_state);
2977 return retval;
2980 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2981 INT y1, INT x2, INT y2)
2983 INT save_state;
2984 GpPointF pt[2];
2985 GpStatus retval;
2987 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2989 if(!pen || !graphics)
2990 return InvalidParameter;
2992 if(graphics->busy)
2993 return ObjectBusy;
2995 if (!graphics->hdc)
2997 FIXME("graphics object has no HDC\n");
2998 return Ok;
3001 pt[0].X = (REAL)x1;
3002 pt[0].Y = (REAL)y1;
3003 pt[1].X = (REAL)x2;
3004 pt[1].Y = (REAL)y2;
3006 save_state = prepare_dc(graphics, pen);
3008 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3010 restore_dc(graphics, save_state);
3012 return retval;
3015 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3016 GpPointF *points, INT count)
3018 INT save_state;
3019 GpStatus retval;
3021 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3023 if(!pen || !graphics || (count < 2))
3024 return InvalidParameter;
3026 if(graphics->busy)
3027 return ObjectBusy;
3029 if (!graphics->hdc)
3031 FIXME("graphics object has no HDC\n");
3032 return Ok;
3035 save_state = prepare_dc(graphics, pen);
3037 retval = draw_polyline(graphics, pen, points, count, TRUE);
3039 restore_dc(graphics, save_state);
3041 return retval;
3044 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3045 GpPoint *points, INT count)
3047 INT save_state;
3048 GpStatus retval;
3049 GpPointF *ptf = NULL;
3050 int i;
3052 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3054 if(!pen || !graphics || (count < 2))
3055 return InvalidParameter;
3057 if(graphics->busy)
3058 return ObjectBusy;
3060 if (!graphics->hdc)
3062 FIXME("graphics object has no HDC\n");
3063 return Ok;
3066 ptf = GdipAlloc(count * sizeof(GpPointF));
3067 if(!ptf) return OutOfMemory;
3069 for(i = 0; i < count; i ++){
3070 ptf[i].X = (REAL) points[i].X;
3071 ptf[i].Y = (REAL) points[i].Y;
3074 save_state = prepare_dc(graphics, pen);
3076 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3078 restore_dc(graphics, save_state);
3080 GdipFree(ptf);
3081 return retval;
3084 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3086 INT save_state;
3087 GpStatus retval;
3089 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3091 if(!pen || !graphics)
3092 return InvalidParameter;
3094 if(graphics->busy)
3095 return ObjectBusy;
3097 if (!graphics->hdc)
3099 FIXME("graphics object has no HDC\n");
3100 return Ok;
3103 save_state = prepare_dc(graphics, pen);
3105 retval = draw_poly(graphics, pen, path->pathdata.Points,
3106 path->pathdata.Types, path->pathdata.Count, TRUE);
3108 restore_dc(graphics, save_state);
3110 return retval;
3113 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3114 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3116 INT save_state;
3118 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3119 width, height, startAngle, sweepAngle);
3121 if(!graphics || !pen)
3122 return InvalidParameter;
3124 if(graphics->busy)
3125 return ObjectBusy;
3127 if (!graphics->hdc)
3129 FIXME("graphics object has no HDC\n");
3130 return Ok;
3133 save_state = prepare_dc(graphics, pen);
3134 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3136 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3138 restore_dc(graphics, save_state);
3140 return Ok;
3143 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3144 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3146 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3147 width, height, startAngle, sweepAngle);
3149 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3152 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3153 REAL y, REAL width, REAL height)
3155 INT save_state;
3156 GpPointF ptf[4];
3157 POINT pti[4];
3159 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3161 if(!pen || !graphics)
3162 return InvalidParameter;
3164 if(graphics->busy)
3165 return ObjectBusy;
3167 if (!graphics->hdc)
3169 FIXME("graphics object has no HDC\n");
3170 return Ok;
3173 ptf[0].X = x;
3174 ptf[0].Y = y;
3175 ptf[1].X = x + width;
3176 ptf[1].Y = y;
3177 ptf[2].X = x + width;
3178 ptf[2].Y = y + height;
3179 ptf[3].X = x;
3180 ptf[3].Y = y + height;
3182 save_state = prepare_dc(graphics, pen);
3183 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3185 transform_and_round_points(graphics, pti, ptf, 4);
3186 Polygon(graphics->hdc, pti, 4);
3188 restore_dc(graphics, save_state);
3190 return Ok;
3193 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3194 INT y, INT width, INT height)
3196 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3198 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3201 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3202 GDIPCONST GpRectF* rects, INT count)
3204 GpPointF *ptf;
3205 POINT *pti;
3206 INT save_state, i;
3208 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3210 if(!graphics || !pen || !rects || count < 1)
3211 return InvalidParameter;
3213 if(graphics->busy)
3214 return ObjectBusy;
3216 if (!graphics->hdc)
3218 FIXME("graphics object has no HDC\n");
3219 return Ok;
3222 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3223 pti = GdipAlloc(4 * count * sizeof(POINT));
3225 if(!ptf || !pti){
3226 GdipFree(ptf);
3227 GdipFree(pti);
3228 return OutOfMemory;
3231 for(i = 0; i < count; i++){
3232 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3233 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3234 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3235 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3238 save_state = prepare_dc(graphics, pen);
3239 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3241 transform_and_round_points(graphics, pti, ptf, 4 * count);
3243 for(i = 0; i < count; i++)
3244 Polygon(graphics->hdc, &pti[4 * i], 4);
3246 restore_dc(graphics, save_state);
3248 GdipFree(ptf);
3249 GdipFree(pti);
3251 return Ok;
3254 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3255 GDIPCONST GpRect* rects, INT count)
3257 GpRectF *rectsF;
3258 GpStatus ret;
3259 INT i;
3261 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3263 if(!rects || count<=0)
3264 return InvalidParameter;
3266 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3267 if(!rectsF)
3268 return OutOfMemory;
3270 for(i = 0;i < count;i++){
3271 rectsF[i].X = (REAL)rects[i].X;
3272 rectsF[i].Y = (REAL)rects[i].Y;
3273 rectsF[i].Width = (REAL)rects[i].Width;
3274 rectsF[i].Height = (REAL)rects[i].Height;
3277 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3278 GdipFree(rectsF);
3280 return ret;
3283 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3284 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3286 GpPath *path;
3287 GpStatus stat;
3289 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3290 count, tension, fill);
3292 if(!graphics || !brush || !points)
3293 return InvalidParameter;
3295 if(graphics->busy)
3296 return ObjectBusy;
3298 if(count == 1) /* Do nothing */
3299 return Ok;
3301 stat = GdipCreatePath(fill, &path);
3302 if(stat != Ok)
3303 return stat;
3305 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3306 if(stat != Ok){
3307 GdipDeletePath(path);
3308 return stat;
3311 stat = GdipFillPath(graphics, brush, path);
3312 if(stat != Ok){
3313 GdipDeletePath(path);
3314 return stat;
3317 GdipDeletePath(path);
3319 return Ok;
3322 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3323 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3325 GpPointF *ptf;
3326 GpStatus stat;
3327 INT i;
3329 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3330 count, tension, fill);
3332 if(!points || count == 0)
3333 return InvalidParameter;
3335 if(count == 1) /* Do nothing */
3336 return Ok;
3338 ptf = GdipAlloc(sizeof(GpPointF)*count);
3339 if(!ptf)
3340 return OutOfMemory;
3342 for(i = 0;i < count;i++){
3343 ptf[i].X = (REAL)points[i].X;
3344 ptf[i].Y = (REAL)points[i].Y;
3347 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3349 GdipFree(ptf);
3351 return stat;
3354 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3355 GDIPCONST GpPointF *points, INT count)
3357 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3358 return GdipFillClosedCurve2(graphics, brush, points, count,
3359 0.5f, FillModeAlternate);
3362 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3363 GDIPCONST GpPoint *points, INT count)
3365 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3366 return GdipFillClosedCurve2I(graphics, brush, points, count,
3367 0.5f, FillModeAlternate);
3370 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3371 REAL y, REAL width, REAL height)
3373 GpStatus stat;
3374 GpPath *path;
3376 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3378 if(!graphics || !brush)
3379 return InvalidParameter;
3381 if(graphics->busy)
3382 return ObjectBusy;
3384 stat = GdipCreatePath(FillModeAlternate, &path);
3386 if (stat == Ok)
3388 stat = GdipAddPathEllipse(path, x, y, width, height);
3390 if (stat == Ok)
3391 stat = GdipFillPath(graphics, brush, path);
3393 GdipDeletePath(path);
3396 return stat;
3399 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3400 INT y, INT width, INT height)
3402 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3404 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3407 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3409 INT save_state;
3410 GpStatus retval;
3412 if(!graphics->hdc || !brush_can_fill_path(brush))
3413 return NotImplemented;
3415 save_state = SaveDC(graphics->hdc);
3416 EndPath(graphics->hdc);
3417 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3418 : WINDING));
3420 BeginPath(graphics->hdc);
3421 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3422 path->pathdata.Types, path->pathdata.Count, FALSE);
3424 if(retval != Ok)
3425 goto end;
3427 EndPath(graphics->hdc);
3428 brush_fill_path(graphics, brush);
3430 retval = Ok;
3432 end:
3433 RestoreDC(graphics->hdc, save_state);
3435 return retval;
3438 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3440 GpStatus stat;
3441 GpRegion *rgn;
3443 if (!brush_can_fill_pixels(brush))
3444 return NotImplemented;
3446 /* FIXME: This could probably be done more efficiently without regions. */
3448 stat = GdipCreateRegionPath(path, &rgn);
3450 if (stat == Ok)
3452 stat = GdipFillRegion(graphics, brush, rgn);
3454 GdipDeleteRegion(rgn);
3457 return stat;
3460 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3462 GpStatus stat = NotImplemented;
3464 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3466 if(!brush || !graphics || !path)
3467 return InvalidParameter;
3469 if(graphics->busy)
3470 return ObjectBusy;
3472 if (!graphics->image)
3473 stat = GDI32_GdipFillPath(graphics, brush, path);
3475 if (stat == NotImplemented)
3476 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3478 if (stat == NotImplemented)
3480 FIXME("Not implemented for brushtype %i\n", brush->bt);
3481 stat = Ok;
3484 return stat;
3487 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3488 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3490 GpStatus stat;
3491 GpPath *path;
3493 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3494 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3496 if(!graphics || !brush)
3497 return InvalidParameter;
3499 if(graphics->busy)
3500 return ObjectBusy;
3502 stat = GdipCreatePath(FillModeAlternate, &path);
3504 if (stat == Ok)
3506 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3508 if (stat == Ok)
3509 stat = GdipFillPath(graphics, brush, path);
3511 GdipDeletePath(path);
3514 return stat;
3517 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3518 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3520 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3521 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3523 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3526 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3527 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3529 GpStatus stat;
3530 GpPath *path;
3532 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3534 if(!graphics || !brush || !points || !count)
3535 return InvalidParameter;
3537 if(graphics->busy)
3538 return ObjectBusy;
3540 stat = GdipCreatePath(fillMode, &path);
3542 if (stat == Ok)
3544 stat = GdipAddPathPolygon(path, points, count);
3546 if (stat == Ok)
3547 stat = GdipFillPath(graphics, brush, path);
3549 GdipDeletePath(path);
3552 return stat;
3555 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3556 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3558 GpStatus stat;
3559 GpPath *path;
3561 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3563 if(!graphics || !brush || !points || !count)
3564 return InvalidParameter;
3566 if(graphics->busy)
3567 return ObjectBusy;
3569 stat = GdipCreatePath(fillMode, &path);
3571 if (stat == Ok)
3573 stat = GdipAddPathPolygonI(path, points, count);
3575 if (stat == Ok)
3576 stat = GdipFillPath(graphics, brush, path);
3578 GdipDeletePath(path);
3581 return stat;
3584 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3585 GDIPCONST GpPointF *points, INT count)
3587 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3589 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3592 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3593 GDIPCONST GpPoint *points, INT count)
3595 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3597 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3600 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3601 REAL x, REAL y, REAL width, REAL height)
3603 GpStatus stat;
3604 GpPath *path;
3606 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3608 if(!graphics || !brush)
3609 return InvalidParameter;
3611 if(graphics->busy)
3612 return ObjectBusy;
3614 stat = GdipCreatePath(FillModeAlternate, &path);
3616 if (stat == Ok)
3618 stat = GdipAddPathRectangle(path, x, y, width, height);
3620 if (stat == Ok)
3621 stat = GdipFillPath(graphics, brush, path);
3623 GdipDeletePath(path);
3626 return stat;
3629 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3630 INT x, INT y, INT width, INT height)
3632 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3634 return GdipFillRectangle(graphics, brush, x, y, width, height);
3637 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3638 INT count)
3640 GpStatus ret;
3641 INT i;
3643 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3645 if(!rects)
3646 return InvalidParameter;
3648 for(i = 0; i < count; i++){
3649 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
3650 if(ret != Ok) return ret;
3653 return Ok;
3656 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3657 INT count)
3659 GpRectF *rectsF;
3660 GpStatus ret;
3661 INT i;
3663 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3665 if(!rects || count <= 0)
3666 return InvalidParameter;
3668 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3669 if(!rectsF)
3670 return OutOfMemory;
3672 for(i = 0; i < count; i++){
3673 rectsF[i].X = (REAL)rects[i].X;
3674 rectsF[i].Y = (REAL)rects[i].Y;
3675 rectsF[i].X = (REAL)rects[i].Width;
3676 rectsF[i].Height = (REAL)rects[i].Height;
3679 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3680 GdipFree(rectsF);
3682 return ret;
3685 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3686 GpRegion* region)
3688 INT save_state;
3689 GpStatus status;
3690 HRGN hrgn;
3691 RECT rc;
3693 if(!graphics->hdc || !brush_can_fill_path(brush))
3694 return NotImplemented;
3696 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3697 if(status != Ok)
3698 return status;
3700 save_state = SaveDC(graphics->hdc);
3701 EndPath(graphics->hdc);
3703 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3705 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3707 BeginPath(graphics->hdc);
3708 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3709 EndPath(graphics->hdc);
3711 brush_fill_path(graphics, brush);
3714 RestoreDC(graphics->hdc, save_state);
3716 DeleteObject(hrgn);
3718 return Ok;
3721 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3722 GpRegion* region)
3724 GpStatus stat;
3725 GpRegion *temp_region;
3726 GpMatrix *world_to_device, *identity;
3727 GpRectF graphics_bounds;
3728 UINT scans_count, i;
3729 INT dummy;
3730 GpRect *scans = NULL;
3731 DWORD *pixel_data;
3733 if (!brush_can_fill_pixels(brush))
3734 return NotImplemented;
3736 stat = get_graphics_bounds(graphics, &graphics_bounds);
3738 if (stat == Ok)
3739 stat = GdipCloneRegion(region, &temp_region);
3741 if (stat == Ok)
3743 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3744 CoordinateSpaceWorld, &world_to_device);
3746 if (stat == Ok)
3748 stat = GdipTransformRegion(temp_region, world_to_device);
3750 GdipDeleteMatrix(world_to_device);
3753 if (stat == Ok)
3754 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3756 if (stat == Ok)
3757 stat = GdipCreateMatrix(&identity);
3759 if (stat == Ok)
3761 stat = GdipGetRegionScansCount(temp_region, &scans_count, identity);
3763 if (stat == Ok && scans_count != 0)
3765 scans = GdipAlloc(sizeof(*scans) * scans_count);
3766 if (!scans)
3767 stat = OutOfMemory;
3769 if (stat == Ok)
3771 stat = GdipGetRegionScansI(temp_region, scans, &dummy, identity);
3773 if (stat != Ok)
3774 GdipFree(scans);
3778 GdipDeleteMatrix(identity);
3781 GdipDeleteRegion(temp_region);
3784 if (stat == Ok && scans_count == 0)
3785 return Ok;
3787 if (stat == Ok)
3789 if (!graphics->image)
3791 /* If we have to go through gdi32, use as few alpha blends as possible. */
3792 INT min_x, min_y, max_x, max_y;
3793 UINT data_width, data_height;
3795 min_x = scans[0].X;
3796 min_y = scans[0].Y;
3797 max_x = scans[0].X+scans[0].Width;
3798 max_y = scans[0].Y+scans[0].Height;
3800 for (i=1; i<scans_count; i++)
3802 min_x = min(min_x, scans[i].X);
3803 min_y = min(min_y, scans[i].Y);
3804 max_x = max(max_x, scans[i].X+scans[i].Width);
3805 max_y = max(max_y, scans[i].Y+scans[i].Height);
3808 data_width = max_x - min_x;
3809 data_height = max_y - min_y;
3811 pixel_data = GdipAlloc(sizeof(*pixel_data) * data_width * data_height);
3812 if (!pixel_data)
3813 stat = OutOfMemory;
3815 if (stat == Ok)
3817 for (i=0; i<scans_count; i++)
3819 stat = brush_fill_pixels(graphics, brush,
3820 pixel_data + (scans[i].X - min_x) + (scans[i].Y - min_y) * data_width,
3821 &scans[i], data_width);
3823 if (stat != Ok)
3824 break;
3827 if (stat == Ok)
3829 stat = alpha_blend_pixels(graphics, min_x, min_y,
3830 (BYTE*)pixel_data, data_width, data_height,
3831 data_width * 4);
3834 GdipFree(pixel_data);
3837 else
3839 UINT max_size=0;
3841 for (i=0; i<scans_count; i++)
3843 UINT size = scans[i].Width * scans[i].Height;
3845 if (size > max_size)
3846 max_size = size;
3849 pixel_data = GdipAlloc(sizeof(*pixel_data) * max_size);
3850 if (!pixel_data)
3851 stat = OutOfMemory;
3853 if (stat == Ok)
3855 for (i=0; i<scans_count; i++)
3857 stat = brush_fill_pixels(graphics, brush, pixel_data, &scans[i],
3858 scans[i].Width);
3860 if (stat == Ok)
3862 stat = alpha_blend_pixels(graphics, scans[i].X, scans[i].Y,
3863 (BYTE*)pixel_data, scans[i].Width, scans[i].Height,
3864 scans[i].Width * 4);
3867 if (stat != Ok)
3868 break;
3871 GdipFree(pixel_data);
3875 GdipFree(scans);
3878 return stat;
3881 /*****************************************************************************
3882 * GdipFillRegion [GDIPLUS.@]
3884 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3885 GpRegion* region)
3887 GpStatus stat = NotImplemented;
3889 TRACE("(%p, %p, %p)\n", graphics, brush, region);
3891 if (!(graphics && brush && region))
3892 return InvalidParameter;
3894 if(graphics->busy)
3895 return ObjectBusy;
3897 if (!graphics->image)
3898 stat = GDI32_GdipFillRegion(graphics, brush, region);
3900 if (stat == NotImplemented)
3901 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
3903 if (stat == NotImplemented)
3905 FIXME("not implemented for brushtype %i\n", brush->bt);
3906 stat = Ok;
3909 return stat;
3912 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3914 TRACE("(%p,%u)\n", graphics, intention);
3916 if(!graphics)
3917 return InvalidParameter;
3919 if(graphics->busy)
3920 return ObjectBusy;
3922 /* We have no internal operation queue, so there's no need to clear it. */
3924 if (graphics->hdc)
3925 GdiFlush();
3927 return Ok;
3930 /*****************************************************************************
3931 * GdipGetClipBounds [GDIPLUS.@]
3933 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3935 TRACE("(%p, %p)\n", graphics, rect);
3937 if(!graphics)
3938 return InvalidParameter;
3940 if(graphics->busy)
3941 return ObjectBusy;
3943 return GdipGetRegionBounds(graphics->clip, graphics, rect);
3946 /*****************************************************************************
3947 * GdipGetClipBoundsI [GDIPLUS.@]
3949 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3951 TRACE("(%p, %p)\n", graphics, rect);
3953 if(!graphics)
3954 return InvalidParameter;
3956 if(graphics->busy)
3957 return ObjectBusy;
3959 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3962 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3963 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3964 CompositingMode *mode)
3966 TRACE("(%p, %p)\n", graphics, mode);
3968 if(!graphics || !mode)
3969 return InvalidParameter;
3971 if(graphics->busy)
3972 return ObjectBusy;
3974 *mode = graphics->compmode;
3976 return Ok;
3979 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3980 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3981 CompositingQuality *quality)
3983 TRACE("(%p, %p)\n", graphics, quality);
3985 if(!graphics || !quality)
3986 return InvalidParameter;
3988 if(graphics->busy)
3989 return ObjectBusy;
3991 *quality = graphics->compqual;
3993 return Ok;
3996 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3997 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3998 InterpolationMode *mode)
4000 TRACE("(%p, %p)\n", graphics, mode);
4002 if(!graphics || !mode)
4003 return InvalidParameter;
4005 if(graphics->busy)
4006 return ObjectBusy;
4008 *mode = graphics->interpolation;
4010 return Ok;
4013 /* FIXME: Need to handle color depths less than 24bpp */
4014 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4016 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4018 if(!graphics || !argb)
4019 return InvalidParameter;
4021 if(graphics->busy)
4022 return ObjectBusy;
4024 return Ok;
4027 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4029 TRACE("(%p, %p)\n", graphics, scale);
4031 if(!graphics || !scale)
4032 return InvalidParameter;
4034 if(graphics->busy)
4035 return ObjectBusy;
4037 *scale = graphics->scale;
4039 return Ok;
4042 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4044 TRACE("(%p, %p)\n", graphics, unit);
4046 if(!graphics || !unit)
4047 return InvalidParameter;
4049 if(graphics->busy)
4050 return ObjectBusy;
4052 *unit = graphics->unit;
4054 return Ok;
4057 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4058 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4059 *mode)
4061 TRACE("(%p, %p)\n", graphics, mode);
4063 if(!graphics || !mode)
4064 return InvalidParameter;
4066 if(graphics->busy)
4067 return ObjectBusy;
4069 *mode = graphics->pixeloffset;
4071 return Ok;
4074 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4075 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4077 TRACE("(%p, %p)\n", graphics, mode);
4079 if(!graphics || !mode)
4080 return InvalidParameter;
4082 if(graphics->busy)
4083 return ObjectBusy;
4085 *mode = graphics->smoothing;
4087 return Ok;
4090 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4092 TRACE("(%p, %p)\n", graphics, contrast);
4094 if(!graphics || !contrast)
4095 return InvalidParameter;
4097 *contrast = graphics->textcontrast;
4099 return Ok;
4102 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4103 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4104 TextRenderingHint *hint)
4106 TRACE("(%p, %p)\n", graphics, hint);
4108 if(!graphics || !hint)
4109 return InvalidParameter;
4111 if(graphics->busy)
4112 return ObjectBusy;
4114 *hint = graphics->texthint;
4116 return Ok;
4119 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4121 GpRegion *clip_rgn;
4122 GpStatus stat;
4124 TRACE("(%p, %p)\n", graphics, rect);
4126 if(!graphics || !rect)
4127 return InvalidParameter;
4129 if(graphics->busy)
4130 return ObjectBusy;
4132 /* intersect window and graphics clipping regions */
4133 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4134 return stat;
4136 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4137 goto cleanup;
4139 /* get bounds of the region */
4140 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4142 cleanup:
4143 GdipDeleteRegion(clip_rgn);
4145 return stat;
4148 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4150 GpRectF rectf;
4151 GpStatus stat;
4153 TRACE("(%p, %p)\n", graphics, rect);
4155 if(!graphics || !rect)
4156 return InvalidParameter;
4158 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4160 rect->X = roundr(rectf.X);
4161 rect->Y = roundr(rectf.Y);
4162 rect->Width = roundr(rectf.Width);
4163 rect->Height = roundr(rectf.Height);
4166 return stat;
4169 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4171 TRACE("(%p, %p)\n", graphics, matrix);
4173 if(!graphics || !matrix)
4174 return InvalidParameter;
4176 if(graphics->busy)
4177 return ObjectBusy;
4179 *matrix = *graphics->worldtrans;
4180 return Ok;
4183 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4185 GpSolidFill *brush;
4186 GpStatus stat;
4187 GpRectF wnd_rect;
4189 TRACE("(%p, %x)\n", graphics, color);
4191 if(!graphics)
4192 return InvalidParameter;
4194 if(graphics->busy)
4195 return ObjectBusy;
4197 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4198 return stat;
4200 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4201 GdipDeleteBrush((GpBrush*)brush);
4202 return stat;
4205 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4206 wnd_rect.Width, wnd_rect.Height);
4208 GdipDeleteBrush((GpBrush*)brush);
4210 return Ok;
4213 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4215 TRACE("(%p, %p)\n", graphics, res);
4217 if(!graphics || !res)
4218 return InvalidParameter;
4220 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4223 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4225 GpStatus stat;
4226 GpRegion* rgn;
4227 GpPointF pt;
4229 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4231 if(!graphics || !result)
4232 return InvalidParameter;
4234 if(graphics->busy)
4235 return ObjectBusy;
4237 pt.X = x;
4238 pt.Y = y;
4239 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4240 CoordinateSpaceWorld, &pt, 1)) != Ok)
4241 return stat;
4243 if((stat = GdipCreateRegion(&rgn)) != Ok)
4244 return stat;
4246 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4247 goto cleanup;
4249 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4251 cleanup:
4252 GdipDeleteRegion(rgn);
4253 return stat;
4256 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4258 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4261 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4263 GpStatus stat;
4264 GpRegion* rgn;
4265 GpPointF pts[2];
4267 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4269 if(!graphics || !result)
4270 return InvalidParameter;
4272 if(graphics->busy)
4273 return ObjectBusy;
4275 pts[0].X = x;
4276 pts[0].Y = y;
4277 pts[1].X = x + width;
4278 pts[1].Y = y + height;
4280 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4281 CoordinateSpaceWorld, pts, 2)) != Ok)
4282 return stat;
4284 pts[1].X -= pts[0].X;
4285 pts[1].Y -= pts[0].Y;
4287 if((stat = GdipCreateRegion(&rgn)) != Ok)
4288 return stat;
4290 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4291 goto cleanup;
4293 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4295 cleanup:
4296 GdipDeleteRegion(rgn);
4297 return stat;
4300 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4302 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4305 GpStatus gdip_format_string(HDC hdc,
4306 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4307 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4308 gdip_format_string_callback callback, void *user_data)
4310 WCHAR* stringdup;
4311 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4312 nheight, lineend, lineno = 0;
4313 RectF bounds;
4314 StringAlignment halign;
4315 GpStatus stat = Ok;
4316 SIZE size;
4318 if(length == -1) length = lstrlenW(string);
4320 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4321 if(!stringdup) return OutOfMemory;
4323 nwidth = roundr(rect->Width);
4324 nheight = roundr(rect->Height);
4326 if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
4327 if (rect->Height >= INT_MAX || rect->Width < 0.5) nheight = INT_MAX;
4329 for(i = 0, j = 0; i < length; i++){
4330 /* FIXME: This makes the indexes passed to callback inaccurate. */
4331 if(!isprintW(string[i]) && (string[i] != '\n'))
4332 continue;
4334 stringdup[j] = string[i];
4335 j++;
4338 length = j;
4340 if (format) halign = format->align;
4341 else halign = StringAlignmentNear;
4343 while(sum < length){
4344 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4345 nwidth, &fit, NULL, &size);
4346 fitcpy = fit;
4348 if(fit == 0)
4349 break;
4351 for(lret = 0; lret < fit; lret++)
4352 if(*(stringdup + sum + lret) == '\n')
4353 break;
4355 /* Line break code (may look strange, but it imitates windows). */
4356 if(lret < fit)
4357 lineend = fit = lret; /* this is not an off-by-one error */
4358 else if(fit < (length - sum)){
4359 if(*(stringdup + sum + fit) == ' ')
4360 while(*(stringdup + sum + fit) == ' ')
4361 fit++;
4362 else
4363 while(*(stringdup + sum + fit - 1) != ' '){
4364 fit--;
4366 if(*(stringdup + sum + fit) == '\t')
4367 break;
4369 if(fit == 0){
4370 fit = fitcpy;
4371 break;
4374 lineend = fit;
4375 while(*(stringdup + sum + lineend - 1) == ' ' ||
4376 *(stringdup + sum + lineend - 1) == '\t')
4377 lineend--;
4379 else
4380 lineend = fit;
4382 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4383 nwidth, &j, NULL, &size);
4385 bounds.Width = size.cx;
4387 if(height + size.cy > nheight)
4388 bounds.Height = nheight - (height + size.cy);
4389 else
4390 bounds.Height = size.cy;
4392 bounds.Y = rect->Y + height;
4394 switch (halign)
4396 case StringAlignmentNear:
4397 default:
4398 bounds.X = rect->X;
4399 break;
4400 case StringAlignmentCenter:
4401 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4402 break;
4403 case StringAlignmentFar:
4404 bounds.X = rect->X + rect->Width - bounds.Width;
4405 break;
4408 stat = callback(hdc, stringdup, sum, lineend,
4409 font, rect, format, lineno, &bounds, user_data);
4411 if (stat != Ok)
4412 break;
4414 sum += fit + (lret < fitcpy ? 1 : 0);
4415 height += size.cy;
4416 lineno++;
4418 if(height > nheight)
4419 break;
4421 /* Stop if this was a linewrap (but not if it was a linebreak). */
4422 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4423 break;
4426 GdipFree(stringdup);
4428 return stat;
4431 struct measure_ranges_args {
4432 GpRegion **regions;
4435 static GpStatus measure_ranges_callback(HDC hdc,
4436 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4437 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4438 INT lineno, const RectF *bounds, void *user_data)
4440 int i;
4441 GpStatus stat = Ok;
4442 struct measure_ranges_args *args = user_data;
4444 for (i=0; i<format->range_count; i++)
4446 INT range_start = max(index, format->character_ranges[i].First);
4447 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4448 if (range_start < range_end)
4450 GpRectF range_rect;
4451 SIZE range_size;
4453 range_rect.Y = bounds->Y;
4454 range_rect.Height = bounds->Height;
4456 GetTextExtentExPointW(hdc, string + index, range_start - index,
4457 INT_MAX, NULL, NULL, &range_size);
4458 range_rect.X = bounds->X + range_size.cx;
4460 GetTextExtentExPointW(hdc, string + index, range_end - index,
4461 INT_MAX, NULL, NULL, &range_size);
4462 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
4464 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4465 if (stat != Ok)
4466 break;
4470 return stat;
4473 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4474 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4475 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4476 INT regionCount, GpRegion** regions)
4478 GpStatus stat;
4479 int i;
4480 HFONT oldfont;
4481 struct measure_ranges_args args;
4482 HDC hdc, temp_hdc=NULL;
4484 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4485 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4487 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4488 return InvalidParameter;
4490 if (regionCount < stringFormat->range_count)
4491 return InvalidParameter;
4493 if(!graphics->hdc)
4495 hdc = temp_hdc = CreateCompatibleDC(0);
4496 if (!temp_hdc) return OutOfMemory;
4498 else
4499 hdc = graphics->hdc;
4501 if (stringFormat->attr)
4502 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4504 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4506 for (i=0; i<stringFormat->range_count; i++)
4508 stat = GdipSetEmpty(regions[i]);
4509 if (stat != Ok)
4510 return stat;
4513 args.regions = regions;
4515 stat = gdip_format_string(hdc, string, length, font, layoutRect, stringFormat,
4516 measure_ranges_callback, &args);
4518 DeleteObject(SelectObject(hdc, oldfont));
4520 if (temp_hdc)
4521 DeleteDC(temp_hdc);
4523 return stat;
4526 struct measure_string_args {
4527 RectF *bounds;
4528 INT *codepointsfitted;
4529 INT *linesfilled;
4532 static GpStatus measure_string_callback(HDC hdc,
4533 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4534 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4535 INT lineno, const RectF *bounds, void *user_data)
4537 struct measure_string_args *args = user_data;
4539 if (bounds->Width > args->bounds->Width)
4540 args->bounds->Width = bounds->Width;
4542 if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
4543 args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
4545 if (args->codepointsfitted)
4546 *args->codepointsfitted = index + length;
4548 if (args->linesfilled)
4549 (*args->linesfilled)++;
4551 return Ok;
4554 /* Find the smallest rectangle that bounds the text when it is printed in rect
4555 * according to the format options listed in format. If rect has 0 width and
4556 * height, then just find the smallest rectangle that bounds the text when it's
4557 * printed at location (rect->X, rect-Y). */
4558 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4559 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4560 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4561 INT *codepointsfitted, INT *linesfilled)
4563 HFONT oldfont;
4564 struct measure_string_args args;
4565 HDC temp_hdc=NULL, hdc;
4567 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4568 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4569 bounds, codepointsfitted, linesfilled);
4571 if(!graphics || !string || !font || !rect || !bounds)
4572 return InvalidParameter;
4574 if(!graphics->hdc)
4576 hdc = temp_hdc = CreateCompatibleDC(0);
4577 if (!temp_hdc) return OutOfMemory;
4579 else
4580 hdc = graphics->hdc;
4582 if(linesfilled) *linesfilled = 0;
4583 if(codepointsfitted) *codepointsfitted = 0;
4585 if(format)
4586 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4588 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4590 bounds->X = rect->X;
4591 bounds->Y = rect->Y;
4592 bounds->Width = 0.0;
4593 bounds->Height = 0.0;
4595 args.bounds = bounds;
4596 args.codepointsfitted = codepointsfitted;
4597 args.linesfilled = linesfilled;
4599 gdip_format_string(hdc, string, length, font, rect, format,
4600 measure_string_callback, &args);
4602 DeleteObject(SelectObject(hdc, oldfont));
4604 if (temp_hdc)
4605 DeleteDC(temp_hdc);
4607 return Ok;
4610 struct draw_string_args {
4611 GpGraphics *graphics;
4612 GDIPCONST GpBrush *brush;
4613 REAL x, y, rel_width, rel_height, ascent;
4616 static GpStatus draw_string_callback(HDC hdc,
4617 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4618 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4619 INT lineno, const RectF *bounds, void *user_data)
4621 struct draw_string_args *args = user_data;
4622 PointF position;
4624 position.X = args->x + bounds->X / args->rel_width;
4625 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4627 return GdipDrawDriverString(args->graphics, &string[index], length, font,
4628 args->brush, &position,
4629 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4632 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4633 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4634 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4636 HRGN rgn = NULL;
4637 HFONT gdifont;
4638 GpPointF pt[3], rectcpy[4];
4639 POINT corners[4];
4640 REAL rel_width, rel_height;
4641 INT offsety = 0, save_state;
4642 struct draw_string_args args;
4643 RectF scaled_rect;
4644 HDC hdc, temp_hdc=NULL;
4645 TEXTMETRICW textmetric;
4647 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4648 length, font, debugstr_rectf(rect), format, brush);
4650 if(!graphics || !string || !font || !brush || !rect)
4651 return InvalidParameter;
4653 if(graphics->hdc)
4655 hdc = graphics->hdc;
4657 else
4659 hdc = temp_hdc = CreateCompatibleDC(0);
4662 if(format){
4663 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4665 /* Should be no need to explicitly test for StringAlignmentNear as
4666 * that is default behavior if no alignment is passed. */
4667 if(format->vertalign != StringAlignmentNear){
4668 RectF bounds;
4669 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
4671 if(format->vertalign == StringAlignmentCenter)
4672 offsety = (rect->Height - bounds.Height) / 2;
4673 else if(format->vertalign == StringAlignmentFar)
4674 offsety = (rect->Height - bounds.Height);
4678 save_state = SaveDC(hdc);
4680 pt[0].X = 0.0;
4681 pt[0].Y = 0.0;
4682 pt[1].X = 1.0;
4683 pt[1].Y = 0.0;
4684 pt[2].X = 0.0;
4685 pt[2].Y = 1.0;
4686 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4687 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4688 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4689 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4690 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4692 rectcpy[3].X = rectcpy[0].X = rect->X;
4693 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
4694 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4695 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
4696 transform_and_round_points(graphics, corners, rectcpy, 4);
4698 scaled_rect.X = 0.0;
4699 scaled_rect.Y = 0.0;
4700 scaled_rect.Width = rel_width * rect->Width;
4701 scaled_rect.Height = rel_height * rect->Height;
4703 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
4705 /* FIXME: If only the width or only the height is 0, we should probably still clip */
4706 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4707 SelectClipRgn(hdc, rgn);
4710 get_font_hfont(graphics, font, &gdifont);
4711 SelectObject(hdc, gdifont);
4713 args.graphics = graphics;
4714 args.brush = brush;
4716 args.x = rect->X;
4717 args.y = rect->Y;
4719 args.rel_width = rel_width;
4720 args.rel_height = rel_height;
4722 GetTextMetricsW(hdc, &textmetric);
4723 args.ascent = textmetric.tmAscent / rel_height;
4725 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
4726 draw_string_callback, &args);
4728 DeleteObject(rgn);
4729 DeleteObject(gdifont);
4731 RestoreDC(hdc, save_state);
4733 DeleteDC(temp_hdc);
4735 return Ok;
4738 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
4740 TRACE("(%p)\n", graphics);
4742 if(!graphics)
4743 return InvalidParameter;
4745 if(graphics->busy)
4746 return ObjectBusy;
4748 return GdipSetInfinite(graphics->clip);
4751 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
4753 TRACE("(%p)\n", graphics);
4755 if(!graphics)
4756 return InvalidParameter;
4758 if(graphics->busy)
4759 return ObjectBusy;
4761 graphics->worldtrans->matrix[0] = 1.0;
4762 graphics->worldtrans->matrix[1] = 0.0;
4763 graphics->worldtrans->matrix[2] = 0.0;
4764 graphics->worldtrans->matrix[3] = 1.0;
4765 graphics->worldtrans->matrix[4] = 0.0;
4766 graphics->worldtrans->matrix[5] = 0.0;
4768 return Ok;
4771 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
4773 return GdipEndContainer(graphics, state);
4776 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
4777 GpMatrixOrder order)
4779 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
4781 if(!graphics)
4782 return InvalidParameter;
4784 if(graphics->busy)
4785 return ObjectBusy;
4787 return GdipRotateMatrix(graphics->worldtrans, angle, order);
4790 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
4792 return GdipBeginContainer2(graphics, state);
4795 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
4796 GraphicsContainer *state)
4798 GraphicsContainerItem *container;
4799 GpStatus sts;
4801 TRACE("(%p, %p)\n", graphics, state);
4803 if(!graphics || !state)
4804 return InvalidParameter;
4806 sts = init_container(&container, graphics);
4807 if(sts != Ok)
4808 return sts;
4810 list_add_head(&graphics->containers, &container->entry);
4811 *state = graphics->contid = container->contid;
4813 return Ok;
4816 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
4818 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4819 return NotImplemented;
4822 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
4824 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4825 return NotImplemented;
4828 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
4830 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
4831 return NotImplemented;
4834 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
4836 GpStatus sts;
4837 GraphicsContainerItem *container, *container2;
4839 TRACE("(%p, %x)\n", graphics, state);
4841 if(!graphics)
4842 return InvalidParameter;
4844 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
4845 if(container->contid == state)
4846 break;
4849 /* did not find a matching container */
4850 if(&container->entry == &graphics->containers)
4851 return Ok;
4853 sts = restore_container(graphics, container);
4854 if(sts != Ok)
4855 return sts;
4857 /* remove all of the containers on top of the found container */
4858 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
4859 if(container->contid == state)
4860 break;
4861 list_remove(&container->entry);
4862 delete_container(container);
4865 list_remove(&container->entry);
4866 delete_container(container);
4868 return Ok;
4871 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
4872 REAL sy, GpMatrixOrder order)
4874 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
4876 if(!graphics)
4877 return InvalidParameter;
4879 if(graphics->busy)
4880 return ObjectBusy;
4882 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
4885 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
4886 CombineMode mode)
4888 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
4890 if(!graphics || !srcgraphics)
4891 return InvalidParameter;
4893 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
4896 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
4897 CompositingMode mode)
4899 TRACE("(%p, %d)\n", graphics, mode);
4901 if(!graphics)
4902 return InvalidParameter;
4904 if(graphics->busy)
4905 return ObjectBusy;
4907 graphics->compmode = mode;
4909 return Ok;
4912 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
4913 CompositingQuality quality)
4915 TRACE("(%p, %d)\n", graphics, quality);
4917 if(!graphics)
4918 return InvalidParameter;
4920 if(graphics->busy)
4921 return ObjectBusy;
4923 graphics->compqual = quality;
4925 return Ok;
4928 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
4929 InterpolationMode mode)
4931 TRACE("(%p, %d)\n", graphics, mode);
4933 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
4934 return InvalidParameter;
4936 if(graphics->busy)
4937 return ObjectBusy;
4939 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
4940 mode = InterpolationModeBilinear;
4942 if (mode == InterpolationModeHighQuality)
4943 mode = InterpolationModeHighQualityBicubic;
4945 graphics->interpolation = mode;
4947 return Ok;
4950 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
4952 TRACE("(%p, %.2f)\n", graphics, scale);
4954 if(!graphics || (scale <= 0.0))
4955 return InvalidParameter;
4957 if(graphics->busy)
4958 return ObjectBusy;
4960 graphics->scale = scale;
4962 return Ok;
4965 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4967 TRACE("(%p, %d)\n", graphics, unit);
4969 if(!graphics)
4970 return InvalidParameter;
4972 if(graphics->busy)
4973 return ObjectBusy;
4975 if(unit == UnitWorld)
4976 return InvalidParameter;
4978 graphics->unit = unit;
4980 return Ok;
4983 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4984 mode)
4986 TRACE("(%p, %d)\n", graphics, mode);
4988 if(!graphics)
4989 return InvalidParameter;
4991 if(graphics->busy)
4992 return ObjectBusy;
4994 graphics->pixeloffset = mode;
4996 return Ok;
4999 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5001 static int calls;
5003 TRACE("(%p,%i,%i)\n", graphics, x, y);
5005 if (!(calls++))
5006 FIXME("not implemented\n");
5008 return NotImplemented;
5011 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5013 static int calls;
5015 TRACE("(%p,%p,%p)\n", graphics, x, y);
5017 if (!(calls++))
5018 FIXME("not implemented\n");
5020 *x = *y = 0;
5022 return NotImplemented;
5025 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5027 TRACE("(%p, %d)\n", graphics, mode);
5029 if(!graphics)
5030 return InvalidParameter;
5032 if(graphics->busy)
5033 return ObjectBusy;
5035 graphics->smoothing = mode;
5037 return Ok;
5040 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5042 TRACE("(%p, %d)\n", graphics, contrast);
5044 if(!graphics)
5045 return InvalidParameter;
5047 graphics->textcontrast = contrast;
5049 return Ok;
5052 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5053 TextRenderingHint hint)
5055 TRACE("(%p, %d)\n", graphics, hint);
5057 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5058 return InvalidParameter;
5060 if(graphics->busy)
5061 return ObjectBusy;
5063 graphics->texthint = hint;
5065 return Ok;
5068 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5070 TRACE("(%p, %p)\n", graphics, matrix);
5072 if(!graphics || !matrix)
5073 return InvalidParameter;
5075 if(graphics->busy)
5076 return ObjectBusy;
5078 GdipDeleteMatrix(graphics->worldtrans);
5079 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5082 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5083 REAL dy, GpMatrixOrder order)
5085 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5087 if(!graphics)
5088 return InvalidParameter;
5090 if(graphics->busy)
5091 return ObjectBusy;
5093 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5096 /*****************************************************************************
5097 * GdipSetClipHrgn [GDIPLUS.@]
5099 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5101 GpRegion *region;
5102 GpStatus status;
5104 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5106 if(!graphics)
5107 return InvalidParameter;
5109 status = GdipCreateRegionHrgn(hrgn, &region);
5110 if(status != Ok)
5111 return status;
5113 status = GdipSetClipRegion(graphics, region, mode);
5115 GdipDeleteRegion(region);
5116 return status;
5119 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5121 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5123 if(!graphics)
5124 return InvalidParameter;
5126 if(graphics->busy)
5127 return ObjectBusy;
5129 return GdipCombineRegionPath(graphics->clip, path, mode);
5132 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5133 REAL width, REAL height,
5134 CombineMode mode)
5136 GpRectF rect;
5138 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5140 if(!graphics)
5141 return InvalidParameter;
5143 if(graphics->busy)
5144 return ObjectBusy;
5146 rect.X = x;
5147 rect.Y = y;
5148 rect.Width = width;
5149 rect.Height = height;
5151 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5154 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5155 INT width, INT height,
5156 CombineMode mode)
5158 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5160 if(!graphics)
5161 return InvalidParameter;
5163 if(graphics->busy)
5164 return ObjectBusy;
5166 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5169 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5170 CombineMode mode)
5172 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5174 if(!graphics || !region)
5175 return InvalidParameter;
5177 if(graphics->busy)
5178 return ObjectBusy;
5180 return GdipCombineRegionRegion(graphics->clip, region, mode);
5183 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5184 UINT limitDpi)
5186 static int calls;
5188 TRACE("(%p,%u)\n", metafile, limitDpi);
5190 if(!(calls++))
5191 FIXME("not implemented\n");
5193 return NotImplemented;
5196 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5197 INT count)
5199 INT save_state;
5200 POINT *pti;
5202 TRACE("(%p, %p, %d)\n", graphics, points, count);
5204 if(!graphics || !pen || count<=0)
5205 return InvalidParameter;
5207 if(graphics->busy)
5208 return ObjectBusy;
5210 if (!graphics->hdc)
5212 FIXME("graphics object has no HDC\n");
5213 return Ok;
5216 pti = GdipAlloc(sizeof(POINT) * count);
5218 save_state = prepare_dc(graphics, pen);
5219 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5221 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5222 Polygon(graphics->hdc, pti, count);
5224 restore_dc(graphics, save_state);
5225 GdipFree(pti);
5227 return Ok;
5230 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5231 INT count)
5233 GpStatus ret;
5234 GpPointF *ptf;
5235 INT i;
5237 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5239 if(count<=0) return InvalidParameter;
5240 ptf = GdipAlloc(sizeof(GpPointF) * count);
5242 for(i = 0;i < count; i++){
5243 ptf[i].X = (REAL)points[i].X;
5244 ptf[i].Y = (REAL)points[i].Y;
5247 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5248 GdipFree(ptf);
5250 return ret;
5253 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5255 TRACE("(%p, %p)\n", graphics, dpi);
5257 if(!graphics || !dpi)
5258 return InvalidParameter;
5260 if(graphics->busy)
5261 return ObjectBusy;
5263 if (graphics->image)
5264 *dpi = graphics->image->xres;
5265 else
5266 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
5268 return Ok;
5271 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5273 TRACE("(%p, %p)\n", graphics, dpi);
5275 if(!graphics || !dpi)
5276 return InvalidParameter;
5278 if(graphics->busy)
5279 return ObjectBusy;
5281 if (graphics->image)
5282 *dpi = graphics->image->yres;
5283 else
5284 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
5286 return Ok;
5289 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5290 GpMatrixOrder order)
5292 GpMatrix m;
5293 GpStatus ret;
5295 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5297 if(!graphics || !matrix)
5298 return InvalidParameter;
5300 if(graphics->busy)
5301 return ObjectBusy;
5303 m = *(graphics->worldtrans);
5305 ret = GdipMultiplyMatrix(&m, matrix, order);
5306 if(ret == Ok)
5307 *(graphics->worldtrans) = m;
5309 return ret;
5312 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5313 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5315 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5317 GpStatus stat=Ok;
5319 TRACE("(%p, %p)\n", graphics, hdc);
5321 if(!graphics || !hdc)
5322 return InvalidParameter;
5324 if(graphics->busy)
5325 return ObjectBusy;
5327 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5329 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5331 else if (!graphics->hdc ||
5332 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5334 /* Create a fake HDC and fill it with a constant color. */
5335 HDC temp_hdc;
5336 HBITMAP hbitmap;
5337 GpRectF bounds;
5338 BITMAPINFOHEADER bmih;
5339 int i;
5341 stat = get_graphics_bounds(graphics, &bounds);
5342 if (stat != Ok)
5343 return stat;
5345 graphics->temp_hbitmap_width = bounds.Width;
5346 graphics->temp_hbitmap_height = bounds.Height;
5348 bmih.biSize = sizeof(bmih);
5349 bmih.biWidth = graphics->temp_hbitmap_width;
5350 bmih.biHeight = -graphics->temp_hbitmap_height;
5351 bmih.biPlanes = 1;
5352 bmih.biBitCount = 32;
5353 bmih.biCompression = BI_RGB;
5354 bmih.biSizeImage = 0;
5355 bmih.biXPelsPerMeter = 0;
5356 bmih.biYPelsPerMeter = 0;
5357 bmih.biClrUsed = 0;
5358 bmih.biClrImportant = 0;
5360 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5361 (void**)&graphics->temp_bits, NULL, 0);
5362 if (!hbitmap)
5363 return GenericError;
5365 temp_hdc = CreateCompatibleDC(0);
5366 if (!temp_hdc)
5368 DeleteObject(hbitmap);
5369 return GenericError;
5372 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5373 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5375 SelectObject(temp_hdc, hbitmap);
5377 graphics->temp_hbitmap = hbitmap;
5378 *hdc = graphics->temp_hdc = temp_hdc;
5380 else
5382 *hdc = graphics->hdc;
5385 if (stat == Ok)
5386 graphics->busy = TRUE;
5388 return stat;
5391 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5393 GpStatus stat=Ok;
5395 TRACE("(%p, %p)\n", graphics, hdc);
5397 if(!graphics || !hdc || !graphics->busy)
5398 return InvalidParameter;
5400 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5402 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5404 else if (graphics->temp_hdc == hdc)
5406 DWORD* pos;
5407 int i;
5409 /* Find the pixels that have changed, and mark them as opaque. */
5410 pos = (DWORD*)graphics->temp_bits;
5411 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5413 if (*pos != DC_BACKGROUND_KEY)
5415 *pos |= 0xff000000;
5417 pos++;
5420 /* Write the changed pixels to the real target. */
5421 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5422 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5423 graphics->temp_hbitmap_width * 4);
5425 /* Clean up. */
5426 DeleteDC(graphics->temp_hdc);
5427 DeleteObject(graphics->temp_hbitmap);
5428 graphics->temp_hdc = NULL;
5429 graphics->temp_hbitmap = NULL;
5431 else if (hdc != graphics->hdc)
5433 stat = InvalidParameter;
5436 if (stat == Ok)
5437 graphics->busy = FALSE;
5439 return stat;
5442 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5444 GpRegion *clip;
5445 GpStatus status;
5447 TRACE("(%p, %p)\n", graphics, region);
5449 if(!graphics || !region)
5450 return InvalidParameter;
5452 if(graphics->busy)
5453 return ObjectBusy;
5455 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5456 return status;
5458 /* free everything except root node and header */
5459 delete_element(&region->node);
5460 memcpy(region, clip, sizeof(GpRegion));
5461 GdipFree(clip);
5463 return Ok;
5466 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5467 GpCoordinateSpace src_space, GpMatrix **matrix)
5469 GpStatus stat = GdipCreateMatrix(matrix);
5470 REAL unitscale;
5472 if (dst_space != src_space && stat == Ok)
5474 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
5476 if(graphics->unit != UnitDisplay)
5477 unitscale *= graphics->scale;
5479 /* transform from src_space to CoordinateSpacePage */
5480 switch (src_space)
5482 case CoordinateSpaceWorld:
5483 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5484 break;
5485 case CoordinateSpacePage:
5486 break;
5487 case CoordinateSpaceDevice:
5488 GdipScaleMatrix(*matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
5489 break;
5492 /* transform from CoordinateSpacePage to dst_space */
5493 switch (dst_space)
5495 case CoordinateSpaceWorld:
5497 GpMatrix *inverted_transform;
5498 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
5499 if (stat == Ok)
5501 stat = GdipInvertMatrix(inverted_transform);
5502 if (stat == Ok)
5503 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
5504 GdipDeleteMatrix(inverted_transform);
5506 break;
5508 case CoordinateSpacePage:
5509 break;
5510 case CoordinateSpaceDevice:
5511 GdipScaleMatrix(*matrix, unitscale, unitscale, MatrixOrderAppend);
5512 break;
5515 return stat;
5518 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5519 GpCoordinateSpace src_space, GpPointF *points, INT count)
5521 GpMatrix *matrix;
5522 GpStatus stat;
5524 if(!graphics || !points || count <= 0)
5525 return InvalidParameter;
5527 if(graphics->busy)
5528 return ObjectBusy;
5530 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5532 if (src_space == dst_space) return Ok;
5534 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5536 if (stat == Ok)
5538 stat = GdipTransformMatrixPoints(matrix, points, count);
5540 GdipDeleteMatrix(matrix);
5543 return stat;
5546 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5547 GpCoordinateSpace src_space, GpPoint *points, INT count)
5549 GpPointF *pointsF;
5550 GpStatus ret;
5551 INT i;
5553 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5555 if(count <= 0)
5556 return InvalidParameter;
5558 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5559 if(!pointsF)
5560 return OutOfMemory;
5562 for(i = 0; i < count; i++){
5563 pointsF[i].X = (REAL)points[i].X;
5564 pointsF[i].Y = (REAL)points[i].Y;
5567 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5569 if(ret == Ok)
5570 for(i = 0; i < count; i++){
5571 points[i].X = roundr(pointsF[i].X);
5572 points[i].Y = roundr(pointsF[i].Y);
5574 GdipFree(pointsF);
5576 return ret;
5579 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5581 static int calls;
5583 TRACE("\n");
5585 if (!calls++)
5586 FIXME("stub\n");
5588 return NULL;
5591 /*****************************************************************************
5592 * GdipTranslateClip [GDIPLUS.@]
5594 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5596 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5598 if(!graphics)
5599 return InvalidParameter;
5601 if(graphics->busy)
5602 return ObjectBusy;
5604 return GdipTranslateRegion(graphics->clip, dx, dy);
5607 /*****************************************************************************
5608 * GdipTranslateClipI [GDIPLUS.@]
5610 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5612 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5614 if(!graphics)
5615 return InvalidParameter;
5617 if(graphics->busy)
5618 return ObjectBusy;
5620 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5624 /*****************************************************************************
5625 * GdipMeasureDriverString [GDIPLUS.@]
5627 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5628 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5629 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5631 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
5632 HFONT hfont;
5633 HDC hdc;
5634 REAL min_x, min_y, max_x, max_y, x, y;
5635 int i;
5636 TEXTMETRICW textmetric;
5637 const WORD *glyph_indices;
5638 WORD *dynamic_glyph_indices=NULL;
5639 REAL rel_width, rel_height, ascent, descent;
5640 GpPointF pt[3];
5642 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5644 if (!graphics || !text || !font || !positions || !boundingBox)
5645 return InvalidParameter;
5647 if (length == -1)
5648 length = strlenW(text);
5650 if (length == 0)
5652 boundingBox->X = 0.0;
5653 boundingBox->Y = 0.0;
5654 boundingBox->Width = 0.0;
5655 boundingBox->Height = 0.0;
5658 if (flags & unsupported_flags)
5659 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
5661 if (matrix)
5662 FIXME("Ignoring matrix\n");
5664 get_font_hfont(graphics, font, &hfont);
5666 hdc = CreateCompatibleDC(0);
5667 SelectObject(hdc, hfont);
5669 GetTextMetricsW(hdc, &textmetric);
5671 pt[0].X = 0.0;
5672 pt[0].Y = 0.0;
5673 pt[1].X = 1.0;
5674 pt[1].Y = 0.0;
5675 pt[2].X = 0.0;
5676 pt[2].Y = 1.0;
5677 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5678 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5679 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5680 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5681 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5683 if (flags & DriverStringOptionsCmapLookup)
5685 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
5686 if (!glyph_indices)
5688 DeleteDC(hdc);
5689 DeleteObject(hfont);
5690 return OutOfMemory;
5693 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
5695 else
5696 glyph_indices = text;
5698 min_x = max_x = x = positions[0].X;
5699 min_y = max_y = y = positions[0].Y;
5701 ascent = textmetric.tmAscent / rel_height;
5702 descent = textmetric.tmDescent / rel_height;
5704 for (i=0; i<length; i++)
5706 int char_width;
5707 ABC abc;
5709 if (!(flags & DriverStringOptionsRealizedAdvance))
5711 x = positions[i].X;
5712 y = positions[i].Y;
5715 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
5716 char_width = abc.abcA + abc.abcB + abc.abcB;
5718 if (min_y > y - ascent) min_y = y - ascent;
5719 if (max_y < y + descent) max_y = y + descent;
5720 if (min_x > x) min_x = x;
5722 x += char_width / rel_width;
5724 if (max_x < x) max_x = x;
5727 GdipFree(dynamic_glyph_indices);
5728 DeleteDC(hdc);
5729 DeleteObject(hfont);
5731 boundingBox->X = min_x;
5732 boundingBox->Y = min_y;
5733 boundingBox->Width = max_x - min_x;
5734 boundingBox->Height = max_y - min_y;
5736 return Ok;
5739 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5740 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5741 GDIPCONST PointF *positions, INT flags,
5742 GDIPCONST GpMatrix *matrix )
5744 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
5745 INT save_state;
5746 GpPointF pt;
5747 HFONT hfont;
5748 UINT eto_flags=0;
5750 if (flags & unsupported_flags)
5751 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
5753 if (matrix)
5754 FIXME("Ignoring matrix\n");
5756 if (!(flags & DriverStringOptionsCmapLookup))
5757 eto_flags |= ETO_GLYPH_INDEX;
5759 save_state = SaveDC(graphics->hdc);
5760 SetBkMode(graphics->hdc, TRANSPARENT);
5761 SetTextColor(graphics->hdc, brush->lb.lbColor);
5763 pt = positions[0];
5764 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
5766 get_font_hfont(graphics, font, &hfont);
5767 SelectObject(graphics->hdc, hfont);
5769 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
5771 ExtTextOutW(graphics->hdc, roundr(pt.X), roundr(pt.Y), eto_flags, NULL, text, length, NULL);
5773 RestoreDC(graphics->hdc, save_state);
5775 DeleteObject(hfont);
5777 return Ok;
5780 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5781 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5782 GDIPCONST PointF *positions, INT flags,
5783 GDIPCONST GpMatrix *matrix )
5785 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
5786 GpStatus stat;
5787 PointF *real_positions, real_position;
5788 POINT *pti;
5789 HFONT hfont;
5790 HDC hdc;
5791 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
5792 DWORD max_glyphsize=0;
5793 GLYPHMETRICS glyphmetrics;
5794 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
5795 BYTE *glyph_mask;
5796 BYTE *text_mask;
5797 int text_mask_stride;
5798 BYTE *pixel_data;
5799 int pixel_data_stride;
5800 GpRect pixel_area;
5801 UINT ggo_flags = GGO_GRAY8_BITMAP;
5803 if (length <= 0)
5804 return Ok;
5806 if (!(flags & DriverStringOptionsCmapLookup))
5807 ggo_flags |= GGO_GLYPH_INDEX;
5809 if (flags & unsupported_flags)
5810 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
5812 if (matrix)
5813 FIXME("Ignoring matrix\n");
5815 pti = GdipAlloc(sizeof(POINT) * length);
5816 if (!pti)
5817 return OutOfMemory;
5819 if (flags & DriverStringOptionsRealizedAdvance)
5821 real_position = positions[0];
5823 transform_and_round_points(graphics, pti, &real_position, 1);
5825 else
5827 real_positions = GdipAlloc(sizeof(PointF) * length);
5828 if (!real_positions)
5830 GdipFree(pti);
5831 return OutOfMemory;
5834 memcpy(real_positions, positions, sizeof(PointF) * length);
5836 transform_and_round_points(graphics, pti, real_positions, length);
5838 GdipFree(real_positions);
5841 get_font_hfont(graphics, font, &hfont);
5843 hdc = CreateCompatibleDC(0);
5844 SelectObject(hdc, hfont);
5846 /* Get the boundaries of the text to be drawn */
5847 for (i=0; i<length; i++)
5849 DWORD glyphsize;
5850 int left, top, right, bottom;
5852 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
5853 &glyphmetrics, 0, NULL, &identity);
5855 if (glyphsize == GDI_ERROR)
5857 ERR("GetGlyphOutlineW failed\n");
5858 GdipFree(pti);
5859 DeleteDC(hdc);
5860 DeleteObject(hfont);
5861 return GenericError;
5864 if (glyphsize > max_glyphsize)
5865 max_glyphsize = glyphsize;
5867 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
5868 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
5869 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
5870 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
5872 if (left < min_x) min_x = left;
5873 if (top < min_y) min_y = top;
5874 if (right > max_x) max_x = right;
5875 if (bottom > max_y) max_y = bottom;
5877 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
5879 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
5880 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
5884 glyph_mask = GdipAlloc(max_glyphsize);
5885 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
5886 text_mask_stride = max_x - min_x;
5888 if (!(glyph_mask && text_mask))
5890 GdipFree(glyph_mask);
5891 GdipFree(text_mask);
5892 GdipFree(pti);
5893 DeleteDC(hdc);
5894 DeleteObject(hfont);
5895 return OutOfMemory;
5898 /* Generate a mask for the text */
5899 for (i=0; i<length; i++)
5901 int left, top, stride;
5903 GetGlyphOutlineW(hdc, text[i], ggo_flags,
5904 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
5906 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
5907 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
5908 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
5910 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
5912 BYTE *glyph_val = glyph_mask + y * stride;
5913 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
5914 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
5916 *text_val = min(64, *text_val + *glyph_val);
5917 glyph_val++;
5918 text_val++;
5923 GdipFree(pti);
5924 DeleteDC(hdc);
5925 DeleteObject(hfont);
5926 GdipFree(glyph_mask);
5928 /* get the brush data */
5929 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
5930 if (!pixel_data)
5932 GdipFree(text_mask);
5933 return OutOfMemory;
5936 pixel_area.X = min_x;
5937 pixel_area.Y = min_y;
5938 pixel_area.Width = max_x - min_x;
5939 pixel_area.Height = max_y - min_y;
5940 pixel_data_stride = pixel_area.Width * 4;
5942 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
5943 if (stat != Ok)
5945 GdipFree(text_mask);
5946 GdipFree(pixel_data);
5947 return stat;
5950 /* multiply the brush data by the mask */
5951 for (y=0; y<pixel_area.Height; y++)
5953 BYTE *text_val = text_mask + text_mask_stride * y;
5954 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
5955 for (x=0; x<pixel_area.Width; x++)
5957 *pixel_val = (*pixel_val) * (*text_val) / 64;
5958 text_val++;
5959 pixel_val+=4;
5963 GdipFree(text_mask);
5965 /* draw the result */
5966 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
5967 pixel_area.Height, pixel_data_stride);
5969 GdipFree(pixel_data);
5971 return stat;
5974 /*****************************************************************************
5975 * GdipDrawDriverString [GDIPLUS.@]
5977 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5978 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5979 GDIPCONST PointF *positions, INT flags,
5980 GDIPCONST GpMatrix *matrix )
5982 GpStatus stat=NotImplemented;
5984 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
5986 if (!graphics || !text || !font || !brush || !positions)
5987 return InvalidParameter;
5989 if (length == -1)
5990 length = strlenW(text);
5992 if (graphics->hdc &&
5993 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
5994 brush->bt == BrushTypeSolidColor &&
5995 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
5996 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, brush,
5997 positions, flags, matrix);
5999 if (stat == NotImplemented)
6000 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, brush,
6001 positions, flags, matrix);
6003 return stat;
6006 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6007 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6009 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6010 return NotImplemented;
6013 /*****************************************************************************
6014 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6016 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6018 GpStatus stat;
6019 GpRegion* rgn;
6021 TRACE("(%p, %p)\n", graphics, res);
6023 if((stat = GdipCreateRegion(&rgn)) != Ok)
6024 return stat;
6026 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6027 goto cleanup;
6029 stat = GdipIsEmptyRegion(rgn, graphics, res);
6031 cleanup:
6032 GdipDeleteRegion(rgn);
6033 return stat;