gdiplus: Select the font into the appropriate hdc in GdipMeasureString.
[wine/multimedia.git] / dlls / gdiplus / graphics.c
blob7d2dbcdb67f12c5a6855aa90087e0c44104aa20d
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
211 HDC hdc;
212 HBITMAP hbitmap, old_hbm=NULL;
213 BITMAPINFOHEADER bih;
214 BYTE *temp_bits;
215 BLENDFUNCTION bf;
217 hdc = CreateCompatibleDC(0);
219 bih.biSize = sizeof(BITMAPINFOHEADER);
220 bih.biWidth = src_width;
221 bih.biHeight = -src_height;
222 bih.biPlanes = 1;
223 bih.biBitCount = 32;
224 bih.biCompression = BI_RGB;
225 bih.biSizeImage = 0;
226 bih.biXPelsPerMeter = 0;
227 bih.biYPelsPerMeter = 0;
228 bih.biClrUsed = 0;
229 bih.biClrImportant = 0;
231 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
232 (void**)&temp_bits, NULL, 0);
234 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
235 4 * src_width, src, src_stride);
237 old_hbm = SelectObject(hdc, hbitmap);
239 bf.BlendOp = AC_SRC_OVER;
240 bf.BlendFlags = 0;
241 bf.SourceConstantAlpha = 255;
242 bf.AlphaFormat = AC_SRC_ALPHA;
244 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, src_width, src_height,
245 hdc, 0, 0, src_width, src_height, bf);
247 SelectObject(hdc, old_hbm);
248 DeleteDC(hdc);
249 DeleteObject(hbitmap);
251 return Ok;
255 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
257 ARGB result=0;
258 ARGB i;
259 INT a1, a2, a3;
261 a1 = (start >> 24) & 0xff;
262 a2 = (end >> 24) & 0xff;
264 a3 = (int)(a1*(1.0f - position)+a2*(position));
266 result |= a3 << 24;
268 for (i=0xff; i<=0xff0000; i = i << 8)
269 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
270 return result;
273 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
275 REAL blendfac;
277 /* clamp to between 0.0 and 1.0, using the wrap mode */
278 if (brush->wrap == WrapModeTile)
280 position = fmodf(position, 1.0f);
281 if (position < 0.0f) position += 1.0f;
283 else /* WrapModeFlip* */
285 position = fmodf(position, 2.0f);
286 if (position < 0.0f) position += 2.0f;
287 if (position > 1.0f) position = 2.0f - position;
290 if (brush->blendcount == 1)
291 blendfac = position;
292 else
294 int i=1;
295 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
296 REAL range;
298 /* locate the blend positions surrounding this position */
299 while (position > brush->blendpos[i])
300 i++;
302 /* interpolate between the blend positions */
303 left_blendpos = brush->blendpos[i-1];
304 left_blendfac = brush->blendfac[i-1];
305 right_blendpos = brush->blendpos[i];
306 right_blendfac = brush->blendfac[i];
307 range = right_blendpos - left_blendpos;
308 blendfac = (left_blendfac * (right_blendpos - position) +
309 right_blendfac * (position - left_blendpos)) / range;
312 if (brush->pblendcount == 0)
313 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
314 else
316 int i=1;
317 ARGB left_blendcolor, right_blendcolor;
318 REAL left_blendpos, right_blendpos;
320 /* locate the blend colors surrounding this position */
321 while (blendfac > brush->pblendpos[i])
322 i++;
324 /* interpolate between the blend colors */
325 left_blendpos = brush->pblendpos[i-1];
326 left_blendcolor = brush->pblendcolor[i-1];
327 right_blendpos = brush->pblendpos[i];
328 right_blendcolor = brush->pblendcolor[i];
329 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
330 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
334 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
335 UINT width, UINT height, INT stride, ColorAdjustType type)
337 UINT x, y, i;
339 if (attributes->colorkeys[type].enabled ||
340 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
342 const struct color_key *key;
343 BYTE min_blue, min_green, min_red;
344 BYTE max_blue, max_green, max_red;
346 if (attributes->colorkeys[type].enabled)
347 key = &attributes->colorkeys[type];
348 else
349 key = &attributes->colorkeys[ColorAdjustTypeDefault];
351 min_blue = key->low&0xff;
352 min_green = (key->low>>8)&0xff;
353 min_red = (key->low>>16)&0xff;
355 max_blue = key->high&0xff;
356 max_green = (key->high>>8)&0xff;
357 max_red = (key->high>>16)&0xff;
359 for (x=0; x<width; x++)
360 for (y=0; y<height; y++)
362 ARGB *src_color;
363 BYTE blue, green, red;
364 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
365 blue = *src_color&0xff;
366 green = (*src_color>>8)&0xff;
367 red = (*src_color>>16)&0xff;
368 if (blue >= min_blue && green >= min_green && red >= min_red &&
369 blue <= max_blue && green <= max_green && red <= max_red)
370 *src_color = 0x00000000;
374 if (attributes->colorremaptables[type].enabled ||
375 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
377 const struct color_remap_table *table;
379 if (attributes->colorremaptables[type].enabled)
380 table = &attributes->colorremaptables[type];
381 else
382 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
384 for (x=0; x<width; x++)
385 for (y=0; y<height; y++)
387 ARGB *src_color;
388 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
389 for (i=0; i<table->mapsize; i++)
391 if (*src_color == table->colormap[i].oldColor.Argb)
393 *src_color = table->colormap[i].newColor.Argb;
394 break;
400 if (attributes->colormatrices[type].enabled ||
401 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
403 static int fixme;
404 if (!fixme++)
405 FIXME("Color transforms not implemented\n");
408 if (attributes->gamma_enabled[type] ||
409 attributes->gamma_enabled[ColorAdjustTypeDefault])
411 static int fixme;
412 if (!fixme++)
413 FIXME("Gamma adjustment not implemented\n");
417 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
418 * bitmap that contains all the pixels we may need to draw it. */
419 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
420 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
421 GpRect *rect)
423 INT left, top, right, bottom;
425 switch (interpolation)
427 case InterpolationModeHighQualityBilinear:
428 case InterpolationModeHighQualityBicubic:
429 /* FIXME: Include a greater range for the prefilter? */
430 case InterpolationModeBicubic:
431 case InterpolationModeBilinear:
432 left = (INT)(floorf(srcx));
433 top = (INT)(floorf(srcy));
434 right = (INT)(ceilf(srcx+srcwidth));
435 bottom = (INT)(ceilf(srcy+srcheight));
436 break;
437 case InterpolationModeNearestNeighbor:
438 default:
439 left = roundr(srcx);
440 top = roundr(srcy);
441 right = roundr(srcx+srcwidth);
442 bottom = roundr(srcy+srcheight);
443 break;
446 if (wrap == WrapModeClamp)
448 if (left < 0)
449 left = 0;
450 if (top < 0)
451 top = 0;
452 if (right >= bitmap->width)
453 right = bitmap->width-1;
454 if (bottom >= bitmap->height)
455 bottom = bitmap->height-1;
457 else
459 /* In some cases we can make the rectangle smaller here, but the logic
460 * is hard to get right, and tiling suggests we're likely to use the
461 * entire source image. */
462 if (left < 0 || right >= bitmap->width)
464 left = 0;
465 right = bitmap->width-1;
468 if (top < 0 || bottom >= bitmap->height)
470 top = 0;
471 bottom = bitmap->height-1;
475 rect->X = left;
476 rect->Y = top;
477 rect->Width = right - left + 1;
478 rect->Height = bottom - top + 1;
481 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
482 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
484 if (attributes->wrap == WrapModeClamp)
486 if (x < 0 || y < 0 || x >= width || y >= height)
487 return attributes->outside_color;
489 else
491 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
492 if (x < 0)
493 x = width*2 + x % (width * 2);
494 if (y < 0)
495 y = height*2 + y % (height * 2);
497 if ((attributes->wrap & 1) == 1)
499 /* Flip X */
500 if ((x / width) % 2 == 0)
501 x = x % width;
502 else
503 x = width - 1 - x % width;
505 else
506 x = x % width;
508 if ((attributes->wrap & 2) == 2)
510 /* Flip Y */
511 if ((y / height) % 2 == 0)
512 y = y % height;
513 else
514 y = height - 1 - y % height;
516 else
517 y = y % height;
520 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
522 ERR("out of range pixel requested\n");
523 return 0xffcd0084;
526 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
529 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
530 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
531 InterpolationMode interpolation)
533 static int fixme;
535 switch (interpolation)
537 default:
538 if (!fixme++)
539 FIXME("Unimplemented interpolation %i\n", interpolation);
540 /* fall-through */
541 case InterpolationModeBilinear:
543 REAL leftxf, topyf;
544 INT leftx, rightx, topy, bottomy;
545 ARGB topleft, topright, bottomleft, bottomright;
546 ARGB top, bottom;
547 float x_offset;
549 leftxf = floorf(point->X);
550 leftx = (INT)leftxf;
551 rightx = (INT)ceilf(point->X);
552 topyf = floorf(point->Y);
553 topy = (INT)topyf;
554 bottomy = (INT)ceilf(point->Y);
556 if (leftx == rightx && topy == bottomy)
557 return sample_bitmap_pixel(src_rect, bits, width, height,
558 leftx, topy, attributes);
560 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
561 leftx, topy, attributes);
562 topright = sample_bitmap_pixel(src_rect, bits, width, height,
563 rightx, topy, attributes);
564 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
565 leftx, bottomy, attributes);
566 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
567 rightx, bottomy, attributes);
569 x_offset = point->X - leftxf;
570 top = blend_colors(topleft, topright, x_offset);
571 bottom = blend_colors(bottomleft, bottomright, x_offset);
573 return blend_colors(top, bottom, point->Y - topyf);
575 case InterpolationModeNearestNeighbor:
576 return sample_bitmap_pixel(src_rect, bits, width, height,
577 roundr(point->X), roundr(point->Y), attributes);
581 static INT brush_can_fill_path(GpBrush *brush)
583 switch (brush->bt)
585 case BrushTypeSolidColor:
586 case BrushTypeHatchFill:
587 return 1;
588 case BrushTypeLinearGradient:
589 case BrushTypeTextureFill:
590 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
591 default:
592 return 0;
596 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
598 switch (brush->bt)
600 case BrushTypeSolidColor:
602 GpSolidFill *fill = (GpSolidFill*)brush;
603 if (fill->bmp)
605 RECT rc;
606 /* partially transparent fill */
608 SelectClipPath(graphics->hdc, RGN_AND);
609 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
611 HDC hdc = CreateCompatibleDC(NULL);
612 HBITMAP oldbmp;
613 BLENDFUNCTION bf;
615 if (!hdc) break;
617 oldbmp = SelectObject(hdc, fill->bmp);
619 bf.BlendOp = AC_SRC_OVER;
620 bf.BlendFlags = 0;
621 bf.SourceConstantAlpha = 255;
622 bf.AlphaFormat = AC_SRC_ALPHA;
624 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
626 SelectObject(hdc, oldbmp);
627 DeleteDC(hdc);
630 break;
632 /* else fall through */
634 default:
635 SelectObject(graphics->hdc, brush->gdibrush);
636 FillPath(graphics->hdc);
637 break;
641 static INT brush_can_fill_pixels(GpBrush *brush)
643 switch (brush->bt)
645 case BrushTypeSolidColor:
646 case BrushTypeHatchFill:
647 case BrushTypeLinearGradient:
648 case BrushTypeTextureFill:
649 return 1;
650 default:
651 return 0;
655 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
656 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
658 switch (brush->bt)
660 case BrushTypeSolidColor:
662 int x, y;
663 GpSolidFill *fill = (GpSolidFill*)brush;
664 for (x=0; x<fill_area->Width; x++)
665 for (y=0; y<fill_area->Height; y++)
666 argb_pixels[x + y*cdwStride] = fill->color;
667 return Ok;
669 case BrushTypeHatchFill:
671 int x, y;
672 GpHatch *fill = (GpHatch*)brush;
673 const char *hatch_data;
675 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
676 return NotImplemented;
678 for (x=0; x<fill_area->Width; x++)
679 for (y=0; y<fill_area->Height; y++)
681 int hx, hy;
683 /* FIXME: Account for the rendering origin */
684 hx = (x + fill_area->X) % 8;
685 hy = (y + fill_area->Y) % 8;
687 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
688 argb_pixels[x + y*cdwStride] = fill->forecol;
689 else
690 argb_pixels[x + y*cdwStride] = fill->backcol;
693 return Ok;
695 case BrushTypeLinearGradient:
697 GpLineGradient *fill = (GpLineGradient*)brush;
698 GpPointF draw_points[3], line_points[3];
699 GpStatus stat;
700 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
701 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
702 int x, y;
704 draw_points[0].X = fill_area->X;
705 draw_points[0].Y = fill_area->Y;
706 draw_points[1].X = fill_area->X+1;
707 draw_points[1].Y = fill_area->Y;
708 draw_points[2].X = fill_area->X;
709 draw_points[2].Y = fill_area->Y+1;
711 /* Transform the points to a co-ordinate space where X is the point's
712 * position in the gradient, 0.0 being the start point and 1.0 the
713 * end point. */
714 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
715 CoordinateSpaceDevice, draw_points, 3);
717 if (stat == Ok)
719 line_points[0] = fill->startpoint;
720 line_points[1] = fill->endpoint;
721 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
722 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
724 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
727 if (stat == Ok)
729 stat = GdipInvertMatrix(world_to_gradient);
731 if (stat == Ok)
732 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
734 GdipDeleteMatrix(world_to_gradient);
737 if (stat == Ok)
739 REAL x_delta = draw_points[1].X - draw_points[0].X;
740 REAL y_delta = draw_points[2].X - draw_points[0].X;
742 for (y=0; y<fill_area->Height; y++)
744 for (x=0; x<fill_area->Width; x++)
746 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
748 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
753 return stat;
755 case BrushTypeTextureFill:
757 GpTexture *fill = (GpTexture*)brush;
758 GpPointF draw_points[3];
759 GpStatus stat;
760 GpMatrix *world_to_texture;
761 int x, y;
762 GpBitmap *bitmap;
763 int src_stride;
764 GpRect src_area;
766 if (fill->image->type != ImageTypeBitmap)
768 FIXME("metafile texture brushes not implemented\n");
769 return NotImplemented;
772 bitmap = (GpBitmap*)fill->image;
773 src_stride = sizeof(ARGB) * bitmap->width;
775 src_area.X = src_area.Y = 0;
776 src_area.Width = bitmap->width;
777 src_area.Height = bitmap->height;
779 draw_points[0].X = fill_area->X;
780 draw_points[0].Y = fill_area->Y;
781 draw_points[1].X = fill_area->X+1;
782 draw_points[1].Y = fill_area->Y;
783 draw_points[2].X = fill_area->X;
784 draw_points[2].Y = fill_area->Y+1;
786 /* Transform the points to the co-ordinate space of the bitmap. */
787 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
788 CoordinateSpaceDevice, draw_points, 3);
790 if (stat == Ok)
792 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
795 if (stat == Ok)
797 stat = GdipInvertMatrix(world_to_texture);
799 if (stat == Ok)
800 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
802 GdipDeleteMatrix(world_to_texture);
805 if (stat == Ok && !fill->bitmap_bits)
807 BitmapData lockeddata;
809 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
810 if (!fill->bitmap_bits)
811 stat = OutOfMemory;
813 if (stat == Ok)
815 lockeddata.Width = bitmap->width;
816 lockeddata.Height = bitmap->height;
817 lockeddata.Stride = src_stride;
818 lockeddata.PixelFormat = PixelFormat32bppARGB;
819 lockeddata.Scan0 = fill->bitmap_bits;
821 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
822 PixelFormat32bppARGB, &lockeddata);
825 if (stat == Ok)
826 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
828 if (stat == Ok)
829 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
830 bitmap->width, bitmap->height,
831 src_stride, ColorAdjustTypeBitmap);
833 if (stat != Ok)
835 GdipFree(fill->bitmap_bits);
836 fill->bitmap_bits = NULL;
840 if (stat == Ok)
842 REAL x_dx = draw_points[1].X - draw_points[0].X;
843 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
844 REAL y_dx = draw_points[2].X - draw_points[0].X;
845 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
847 for (y=0; y<fill_area->Height; y++)
849 for (x=0; x<fill_area->Width; x++)
851 GpPointF point;
852 point.X = draw_points[0].X + x * x_dx + y * y_dx;
853 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
855 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
856 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
857 &point, fill->imageattributes, graphics->interpolation);
862 return stat;
864 default:
865 return NotImplemented;
869 /* GdipDrawPie/GdipFillPie helper function */
870 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
871 REAL height, REAL startAngle, REAL sweepAngle)
873 GpPointF ptf[4];
874 POINT pti[4];
876 ptf[0].X = x;
877 ptf[0].Y = y;
878 ptf[1].X = x + width;
879 ptf[1].Y = y + height;
881 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
882 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
884 transform_and_round_points(graphics, pti, ptf, 4);
886 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
887 pti[2].y, pti[3].x, pti[3].y);
890 /* Draws the linecap the specified color and size on the hdc. The linecap is in
891 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
892 * should not be called on an hdc that has a path you care about. */
893 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
894 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
896 HGDIOBJ oldbrush = NULL, oldpen = NULL;
897 GpMatrix *matrix = NULL;
898 HBRUSH brush = NULL;
899 HPEN pen = NULL;
900 PointF ptf[4], *custptf = NULL;
901 POINT pt[4], *custpt = NULL;
902 BYTE *tp = NULL;
903 REAL theta, dsmall, dbig, dx, dy = 0.0;
904 INT i, count;
905 LOGBRUSH lb;
906 BOOL customstroke;
908 if((x1 == x2) && (y1 == y2))
909 return;
911 theta = gdiplus_atan2(y2 - y1, x2 - x1);
913 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
914 if(!customstroke){
915 brush = CreateSolidBrush(color);
916 lb.lbStyle = BS_SOLID;
917 lb.lbColor = color;
918 lb.lbHatch = 0;
919 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
920 PS_JOIN_MITER, 1, &lb, 0,
921 NULL);
922 oldbrush = SelectObject(graphics->hdc, brush);
923 oldpen = SelectObject(graphics->hdc, pen);
926 switch(cap){
927 case LineCapFlat:
928 break;
929 case LineCapSquare:
930 case LineCapSquareAnchor:
931 case LineCapDiamondAnchor:
932 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
933 if(cap == LineCapDiamondAnchor){
934 dsmall = cos(theta + M_PI_2) * size;
935 dbig = sin(theta + M_PI_2) * size;
937 else{
938 dsmall = cos(theta + M_PI_4) * size;
939 dbig = sin(theta + M_PI_4) * size;
942 ptf[0].X = x2 - dsmall;
943 ptf[1].X = x2 + dbig;
945 ptf[0].Y = y2 - dbig;
946 ptf[3].Y = y2 + dsmall;
948 ptf[1].Y = y2 - dsmall;
949 ptf[2].Y = y2 + dbig;
951 ptf[3].X = x2 - dbig;
952 ptf[2].X = x2 + dsmall;
954 transform_and_round_points(graphics, pt, ptf, 4);
955 Polygon(graphics->hdc, pt, 4);
957 break;
958 case LineCapArrowAnchor:
959 size = size * 4.0 / sqrt(3.0);
961 dx = cos(M_PI / 6.0 + theta) * size;
962 dy = sin(M_PI / 6.0 + theta) * size;
964 ptf[0].X = x2 - dx;
965 ptf[0].Y = y2 - dy;
967 dx = cos(- M_PI / 6.0 + theta) * size;
968 dy = sin(- M_PI / 6.0 + theta) * size;
970 ptf[1].X = x2 - dx;
971 ptf[1].Y = y2 - dy;
973 ptf[2].X = x2;
974 ptf[2].Y = y2;
976 transform_and_round_points(graphics, pt, ptf, 3);
977 Polygon(graphics->hdc, pt, 3);
979 break;
980 case LineCapRoundAnchor:
981 dx = dy = ANCHOR_WIDTH * size / 2.0;
983 ptf[0].X = x2 - dx;
984 ptf[0].Y = y2 - dy;
985 ptf[1].X = x2 + dx;
986 ptf[1].Y = y2 + dy;
988 transform_and_round_points(graphics, pt, ptf, 2);
989 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
991 break;
992 case LineCapTriangle:
993 size = size / 2.0;
994 dx = cos(M_PI_2 + theta) * size;
995 dy = sin(M_PI_2 + theta) * size;
997 ptf[0].X = x2 - dx;
998 ptf[0].Y = y2 - dy;
999 ptf[1].X = x2 + dx;
1000 ptf[1].Y = y2 + dy;
1002 dx = cos(theta) * size;
1003 dy = sin(theta) * size;
1005 ptf[2].X = x2 + dx;
1006 ptf[2].Y = y2 + dy;
1008 transform_and_round_points(graphics, pt, ptf, 3);
1009 Polygon(graphics->hdc, pt, 3);
1011 break;
1012 case LineCapRound:
1013 dx = dy = size / 2.0;
1015 ptf[0].X = x2 - dx;
1016 ptf[0].Y = y2 - dy;
1017 ptf[1].X = x2 + dx;
1018 ptf[1].Y = y2 + dy;
1020 dx = -cos(M_PI_2 + theta) * size;
1021 dy = -sin(M_PI_2 + theta) * size;
1023 ptf[2].X = x2 - dx;
1024 ptf[2].Y = y2 - dy;
1025 ptf[3].X = x2 + dx;
1026 ptf[3].Y = y2 + dy;
1028 transform_and_round_points(graphics, pt, ptf, 4);
1029 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1030 pt[2].y, pt[3].x, pt[3].y);
1032 break;
1033 case LineCapCustom:
1034 if(!custom)
1035 break;
1037 count = custom->pathdata.Count;
1038 custptf = GdipAlloc(count * sizeof(PointF));
1039 custpt = GdipAlloc(count * sizeof(POINT));
1040 tp = GdipAlloc(count);
1042 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1043 goto custend;
1045 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1047 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1048 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1049 MatrixOrderAppend);
1050 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1051 GdipTransformMatrixPoints(matrix, custptf, count);
1053 transform_and_round_points(graphics, custpt, custptf, count);
1055 for(i = 0; i < count; i++)
1056 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1058 if(custom->fill){
1059 BeginPath(graphics->hdc);
1060 PolyDraw(graphics->hdc, custpt, tp, count);
1061 EndPath(graphics->hdc);
1062 StrokeAndFillPath(graphics->hdc);
1064 else
1065 PolyDraw(graphics->hdc, custpt, tp, count);
1067 custend:
1068 GdipFree(custptf);
1069 GdipFree(custpt);
1070 GdipFree(tp);
1071 GdipDeleteMatrix(matrix);
1072 break;
1073 default:
1074 break;
1077 if(!customstroke){
1078 SelectObject(graphics->hdc, oldbrush);
1079 SelectObject(graphics->hdc, oldpen);
1080 DeleteObject(brush);
1081 DeleteObject(pen);
1085 /* Shortens the line by the given percent by changing x2, y2.
1086 * If percent is > 1.0 then the line will change direction.
1087 * If percent is negative it can lengthen the line. */
1088 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1090 REAL dist, theta, dx, dy;
1092 if((y1 == *y2) && (x1 == *x2))
1093 return;
1095 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1096 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1097 dx = cos(theta) * dist;
1098 dy = sin(theta) * dist;
1100 *x2 = *x2 + dx;
1101 *y2 = *y2 + dy;
1104 /* Shortens the line by the given amount by changing x2, y2.
1105 * If the amount is greater than the distance, the line will become length 0.
1106 * If the amount is negative, it can lengthen the line. */
1107 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1109 REAL dx, dy, percent;
1111 dx = *x2 - x1;
1112 dy = *y2 - y1;
1113 if(dx == 0 && dy == 0)
1114 return;
1116 percent = amt / sqrt(dx * dx + dy * dy);
1117 if(percent >= 1.0){
1118 *x2 = x1;
1119 *y2 = y1;
1120 return;
1123 shorten_line_percent(x1, y1, x2, y2, percent);
1126 /* Draws lines between the given points, and if caps is true then draws an endcap
1127 * at the end of the last line. */
1128 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1129 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1131 POINT *pti = NULL;
1132 GpPointF *ptcopy = NULL;
1133 GpStatus status = GenericError;
1135 if(!count)
1136 return Ok;
1138 pti = GdipAlloc(count * sizeof(POINT));
1139 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1141 if(!pti || !ptcopy){
1142 status = OutOfMemory;
1143 goto end;
1146 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1148 if(caps){
1149 if(pen->endcap == LineCapArrowAnchor)
1150 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1151 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1152 else if((pen->endcap == LineCapCustom) && pen->customend)
1153 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1154 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1155 pen->customend->inset * pen->width);
1157 if(pen->startcap == LineCapArrowAnchor)
1158 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1159 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1160 else if((pen->startcap == LineCapCustom) && pen->customstart)
1161 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1162 &ptcopy[0].X, &ptcopy[0].Y,
1163 pen->customstart->inset * pen->width);
1165 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1166 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1167 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1168 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1171 transform_and_round_points(graphics, pti, ptcopy, count);
1173 if(Polyline(graphics->hdc, pti, count))
1174 status = Ok;
1176 end:
1177 GdipFree(pti);
1178 GdipFree(ptcopy);
1180 return status;
1183 /* Conducts a linear search to find the bezier points that will back off
1184 * the endpoint of the curve by a distance of amt. Linear search works
1185 * better than binary in this case because there are multiple solutions,
1186 * and binary searches often find a bad one. I don't think this is what
1187 * Windows does but short of rendering the bezier without GDI's help it's
1188 * the best we can do. If rev then work from the start of the passed points
1189 * instead of the end. */
1190 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1192 GpPointF origpt[4];
1193 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1194 INT i, first = 0, second = 1, third = 2, fourth = 3;
1196 if(rev){
1197 first = 3;
1198 second = 2;
1199 third = 1;
1200 fourth = 0;
1203 origx = pt[fourth].X;
1204 origy = pt[fourth].Y;
1205 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1207 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1208 /* reset bezier points to original values */
1209 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1210 /* Perform magic on bezier points. Order is important here.*/
1211 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1212 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1213 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1214 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1215 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1216 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1218 dx = pt[fourth].X - origx;
1219 dy = pt[fourth].Y - origy;
1221 diff = sqrt(dx * dx + dy * dy);
1222 percent += 0.0005 * amt;
1226 /* Draws bezier curves between given points, and if caps is true then draws an
1227 * endcap at the end of the last line. */
1228 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1229 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1231 POINT *pti;
1232 GpPointF *ptcopy;
1233 GpStatus status = GenericError;
1235 if(!count)
1236 return Ok;
1238 pti = GdipAlloc(count * sizeof(POINT));
1239 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1241 if(!pti || !ptcopy){
1242 status = OutOfMemory;
1243 goto end;
1246 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1248 if(caps){
1249 if(pen->endcap == LineCapArrowAnchor)
1250 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1251 else if((pen->endcap == LineCapCustom) && pen->customend)
1252 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1253 FALSE);
1255 if(pen->startcap == LineCapArrowAnchor)
1256 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1257 else if((pen->startcap == LineCapCustom) && pen->customstart)
1258 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1260 /* the direction of the line cap is parallel to the direction at the
1261 * end of the bezier (which, if it has been shortened, is not the same
1262 * as the direction from pt[count-2] to pt[count-1]) */
1263 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1264 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1265 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1266 pt[count - 1].X, pt[count - 1].Y);
1268 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1269 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1270 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1273 transform_and_round_points(graphics, pti, ptcopy, count);
1275 PolyBezier(graphics->hdc, pti, count);
1277 status = Ok;
1279 end:
1280 GdipFree(pti);
1281 GdipFree(ptcopy);
1283 return status;
1286 /* Draws a combination of bezier curves and lines between points. */
1287 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1288 GDIPCONST BYTE * types, INT count, BOOL caps)
1290 POINT *pti = GdipAlloc(count * sizeof(POINT));
1291 BYTE *tp = GdipAlloc(count);
1292 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1293 INT i, j;
1294 GpStatus status = GenericError;
1296 if(!count){
1297 status = Ok;
1298 goto end;
1300 if(!pti || !tp || !ptcopy){
1301 status = OutOfMemory;
1302 goto end;
1305 for(i = 1; i < count; i++){
1306 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1307 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1308 || !(types[i + 1] & PathPointTypeBezier)){
1309 ERR("Bad bezier points\n");
1310 goto end;
1312 i += 2;
1316 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1318 /* If we are drawing caps, go through the points and adjust them accordingly,
1319 * and draw the caps. */
1320 if(caps){
1321 switch(types[count - 1] & PathPointTypePathTypeMask){
1322 case PathPointTypeBezier:
1323 if(pen->endcap == LineCapArrowAnchor)
1324 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1325 else if((pen->endcap == LineCapCustom) && pen->customend)
1326 shorten_bezier_amt(&ptcopy[count - 4],
1327 pen->width * pen->customend->inset, FALSE);
1329 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1330 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1331 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1332 pt[count - 1].X, pt[count - 1].Y);
1334 break;
1335 case PathPointTypeLine:
1336 if(pen->endcap == LineCapArrowAnchor)
1337 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1338 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1339 pen->width);
1340 else if((pen->endcap == LineCapCustom) && pen->customend)
1341 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1342 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1343 pen->customend->inset * pen->width);
1345 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1346 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1347 pt[count - 1].Y);
1349 break;
1350 default:
1351 ERR("Bad path last point\n");
1352 goto end;
1355 /* Find start of points */
1356 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1357 == PathPointTypeStart); j++);
1359 switch(types[j] & PathPointTypePathTypeMask){
1360 case PathPointTypeBezier:
1361 if(pen->startcap == LineCapArrowAnchor)
1362 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1363 else if((pen->startcap == LineCapCustom) && pen->customstart)
1364 shorten_bezier_amt(&ptcopy[j - 1],
1365 pen->width * pen->customstart->inset, TRUE);
1367 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1368 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1369 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1370 pt[j - 1].X, pt[j - 1].Y);
1372 break;
1373 case PathPointTypeLine:
1374 if(pen->startcap == LineCapArrowAnchor)
1375 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1376 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1377 pen->width);
1378 else if((pen->startcap == LineCapCustom) && pen->customstart)
1379 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1380 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1381 pen->customstart->inset * pen->width);
1383 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1384 pt[j].X, pt[j].Y, pt[j - 1].X,
1385 pt[j - 1].Y);
1387 break;
1388 default:
1389 ERR("Bad path points\n");
1390 goto end;
1394 transform_and_round_points(graphics, pti, ptcopy, count);
1396 for(i = 0; i < count; i++){
1397 tp[i] = convert_path_point_type(types[i]);
1400 PolyDraw(graphics->hdc, pti, tp, count);
1402 status = Ok;
1404 end:
1405 GdipFree(pti);
1406 GdipFree(ptcopy);
1407 GdipFree(tp);
1409 return status;
1412 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1414 GpStatus result;
1416 BeginPath(graphics->hdc);
1417 result = draw_poly(graphics, NULL, path->pathdata.Points,
1418 path->pathdata.Types, path->pathdata.Count, FALSE);
1419 EndPath(graphics->hdc);
1420 return result;
1423 typedef struct _GraphicsContainerItem {
1424 struct list entry;
1425 GraphicsContainer contid;
1427 SmoothingMode smoothing;
1428 CompositingQuality compqual;
1429 InterpolationMode interpolation;
1430 CompositingMode compmode;
1431 TextRenderingHint texthint;
1432 REAL scale;
1433 GpUnit unit;
1434 PixelOffsetMode pixeloffset;
1435 UINT textcontrast;
1436 GpMatrix* worldtrans;
1437 GpRegion* clip;
1438 } GraphicsContainerItem;
1440 static GpStatus init_container(GraphicsContainerItem** container,
1441 GDIPCONST GpGraphics* graphics){
1442 GpStatus sts;
1444 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1445 if(!(*container))
1446 return OutOfMemory;
1448 (*container)->contid = graphics->contid + 1;
1450 (*container)->smoothing = graphics->smoothing;
1451 (*container)->compqual = graphics->compqual;
1452 (*container)->interpolation = graphics->interpolation;
1453 (*container)->compmode = graphics->compmode;
1454 (*container)->texthint = graphics->texthint;
1455 (*container)->scale = graphics->scale;
1456 (*container)->unit = graphics->unit;
1457 (*container)->textcontrast = graphics->textcontrast;
1458 (*container)->pixeloffset = graphics->pixeloffset;
1460 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1461 if(sts != Ok){
1462 GdipFree(*container);
1463 *container = NULL;
1464 return sts;
1467 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1468 if(sts != Ok){
1469 GdipDeleteMatrix((*container)->worldtrans);
1470 GdipFree(*container);
1471 *container = NULL;
1472 return sts;
1475 return Ok;
1478 static void delete_container(GraphicsContainerItem* container){
1479 GdipDeleteMatrix(container->worldtrans);
1480 GdipDeleteRegion(container->clip);
1481 GdipFree(container);
1484 static GpStatus restore_container(GpGraphics* graphics,
1485 GDIPCONST GraphicsContainerItem* container){
1486 GpStatus sts;
1487 GpMatrix *newTrans;
1488 GpRegion *newClip;
1490 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1491 if(sts != Ok)
1492 return sts;
1494 sts = GdipCloneRegion(container->clip, &newClip);
1495 if(sts != Ok){
1496 GdipDeleteMatrix(newTrans);
1497 return sts;
1500 GdipDeleteMatrix(graphics->worldtrans);
1501 graphics->worldtrans = newTrans;
1503 GdipDeleteRegion(graphics->clip);
1504 graphics->clip = newClip;
1506 graphics->contid = container->contid - 1;
1508 graphics->smoothing = container->smoothing;
1509 graphics->compqual = container->compqual;
1510 graphics->interpolation = container->interpolation;
1511 graphics->compmode = container->compmode;
1512 graphics->texthint = container->texthint;
1513 graphics->scale = container->scale;
1514 graphics->unit = container->unit;
1515 graphics->textcontrast = container->textcontrast;
1516 graphics->pixeloffset = container->pixeloffset;
1518 return Ok;
1521 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1523 RECT wnd_rect;
1524 GpStatus stat=Ok;
1525 GpUnit unit;
1527 if(graphics->hwnd) {
1528 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1529 return GenericError;
1531 rect->X = wnd_rect.left;
1532 rect->Y = wnd_rect.top;
1533 rect->Width = wnd_rect.right - wnd_rect.left;
1534 rect->Height = wnd_rect.bottom - wnd_rect.top;
1535 }else if (graphics->image){
1536 stat = GdipGetImageBounds(graphics->image, rect, &unit);
1537 if (stat == Ok && unit != UnitPixel)
1538 FIXME("need to convert from unit %i\n", unit);
1539 }else{
1540 rect->X = 0;
1541 rect->Y = 0;
1542 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1543 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1546 return stat;
1549 /* on success, rgn will contain the region of the graphics object which
1550 * is visible after clipping has been applied */
1551 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1553 GpStatus stat;
1554 GpRectF rectf;
1555 GpRegion* tmp;
1557 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1558 return stat;
1560 if((stat = GdipCreateRegion(&tmp)) != Ok)
1561 return stat;
1563 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1564 goto end;
1566 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1567 goto end;
1569 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1571 end:
1572 GdipDeleteRegion(tmp);
1573 return stat;
1576 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1578 TRACE("(%p, %p)\n", hdc, graphics);
1580 return GdipCreateFromHDC2(hdc, NULL, graphics);
1583 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1585 GpStatus retval;
1587 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1589 if(hDevice != NULL) {
1590 FIXME("Don't know how to handle parameter hDevice\n");
1591 return NotImplemented;
1594 if(hdc == NULL)
1595 return OutOfMemory;
1597 if(graphics == NULL)
1598 return InvalidParameter;
1600 *graphics = GdipAlloc(sizeof(GpGraphics));
1601 if(!*graphics) return OutOfMemory;
1603 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1604 GdipFree(*graphics);
1605 return retval;
1608 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1609 GdipFree((*graphics)->worldtrans);
1610 GdipFree(*graphics);
1611 return retval;
1614 (*graphics)->hdc = hdc;
1615 (*graphics)->hwnd = WindowFromDC(hdc);
1616 (*graphics)->owndc = FALSE;
1617 (*graphics)->smoothing = SmoothingModeDefault;
1618 (*graphics)->compqual = CompositingQualityDefault;
1619 (*graphics)->interpolation = InterpolationModeBilinear;
1620 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1621 (*graphics)->compmode = CompositingModeSourceOver;
1622 (*graphics)->unit = UnitDisplay;
1623 (*graphics)->scale = 1.0;
1624 (*graphics)->busy = FALSE;
1625 (*graphics)->textcontrast = 4;
1626 list_init(&(*graphics)->containers);
1627 (*graphics)->contid = 0;
1629 TRACE("<-- %p\n", *graphics);
1631 return Ok;
1634 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
1636 GpStatus retval;
1638 *graphics = GdipAlloc(sizeof(GpGraphics));
1639 if(!*graphics) return OutOfMemory;
1641 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1642 GdipFree(*graphics);
1643 return retval;
1646 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1647 GdipFree((*graphics)->worldtrans);
1648 GdipFree(*graphics);
1649 return retval;
1652 (*graphics)->hdc = NULL;
1653 (*graphics)->hwnd = NULL;
1654 (*graphics)->owndc = FALSE;
1655 (*graphics)->image = image;
1656 (*graphics)->smoothing = SmoothingModeDefault;
1657 (*graphics)->compqual = CompositingQualityDefault;
1658 (*graphics)->interpolation = InterpolationModeBilinear;
1659 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1660 (*graphics)->compmode = CompositingModeSourceOver;
1661 (*graphics)->unit = UnitDisplay;
1662 (*graphics)->scale = 1.0;
1663 (*graphics)->busy = FALSE;
1664 (*graphics)->textcontrast = 4;
1665 list_init(&(*graphics)->containers);
1666 (*graphics)->contid = 0;
1668 TRACE("<-- %p\n", *graphics);
1670 return Ok;
1673 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1675 GpStatus ret;
1676 HDC hdc;
1678 TRACE("(%p, %p)\n", hwnd, graphics);
1680 hdc = GetDC(hwnd);
1682 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1684 ReleaseDC(hwnd, hdc);
1685 return ret;
1688 (*graphics)->hwnd = hwnd;
1689 (*graphics)->owndc = TRUE;
1691 return Ok;
1694 /* FIXME: no icm handling */
1695 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1697 TRACE("(%p, %p)\n", hwnd, graphics);
1699 return GdipCreateFromHWND(hwnd, graphics);
1702 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1703 GpMetafile **metafile)
1705 static int calls;
1707 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1709 if(!hemf || !metafile)
1710 return InvalidParameter;
1712 if(!(calls++))
1713 FIXME("not implemented\n");
1715 return NotImplemented;
1718 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1719 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1721 IStream *stream = NULL;
1722 UINT read;
1723 BYTE* copy;
1724 HENHMETAFILE hemf;
1725 GpStatus retval = Ok;
1727 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1729 if(!hwmf || !metafile || !placeable)
1730 return InvalidParameter;
1732 *metafile = NULL;
1733 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1734 if(!read)
1735 return GenericError;
1736 copy = GdipAlloc(read);
1737 GetMetaFileBitsEx(hwmf, read, copy);
1739 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1740 GdipFree(copy);
1742 read = GetEnhMetaFileBits(hemf, 0, NULL);
1743 copy = GdipAlloc(read);
1744 GetEnhMetaFileBits(hemf, read, copy);
1745 DeleteEnhMetaFile(hemf);
1747 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1748 ERR("could not make stream\n");
1749 GdipFree(copy);
1750 retval = GenericError;
1751 goto err;
1754 *metafile = GdipAlloc(sizeof(GpMetafile));
1755 if(!*metafile){
1756 retval = OutOfMemory;
1757 goto err;
1760 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1761 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1763 retval = GenericError;
1764 goto err;
1768 (*metafile)->image.type = ImageTypeMetafile;
1769 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1770 (*metafile)->image.palette_flags = 0;
1771 (*metafile)->image.palette_count = 0;
1772 (*metafile)->image.palette_size = 0;
1773 (*metafile)->image.palette_entries = NULL;
1774 (*metafile)->image.xres = (REAL)placeable->Inch;
1775 (*metafile)->image.yres = (REAL)placeable->Inch;
1776 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1777 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1778 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1779 - placeable->BoundingBox.Left));
1780 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1781 - placeable->BoundingBox.Top));
1782 (*metafile)->unit = UnitPixel;
1784 if(delete)
1785 DeleteMetaFile(hwmf);
1787 TRACE("<-- %p\n", *metafile);
1789 err:
1790 if (retval != Ok)
1791 GdipFree(*metafile);
1792 IStream_Release(stream);
1793 return retval;
1796 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1797 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1799 HMETAFILE hmf = GetMetaFileW(file);
1801 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1803 if(!hmf) return InvalidParameter;
1805 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1808 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1809 GpMetafile **metafile)
1811 FIXME("(%p, %p): stub\n", file, metafile);
1812 return NotImplemented;
1815 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1816 GpMetafile **metafile)
1818 FIXME("(%p, %p): stub\n", stream, metafile);
1819 return NotImplemented;
1822 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1823 UINT access, IStream **stream)
1825 DWORD dwMode;
1826 HRESULT ret;
1828 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1830 if(!stream || !filename)
1831 return InvalidParameter;
1833 if(access & GENERIC_WRITE)
1834 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1835 else if(access & GENERIC_READ)
1836 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1837 else
1838 return InvalidParameter;
1840 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1842 return hresult_to_status(ret);
1845 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1847 GraphicsContainerItem *cont, *next;
1848 TRACE("(%p)\n", graphics);
1850 if(!graphics) return InvalidParameter;
1851 if(graphics->busy) return ObjectBusy;
1853 if(graphics->owndc)
1854 ReleaseDC(graphics->hwnd, graphics->hdc);
1856 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1857 list_remove(&cont->entry);
1858 delete_container(cont);
1861 GdipDeleteRegion(graphics->clip);
1862 GdipDeleteMatrix(graphics->worldtrans);
1863 GdipFree(graphics);
1865 return Ok;
1868 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1869 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1871 INT save_state, num_pts;
1872 GpPointF points[MAX_ARC_PTS];
1873 GpStatus retval;
1875 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1876 width, height, startAngle, sweepAngle);
1878 if(!graphics || !pen || width <= 0 || height <= 0)
1879 return InvalidParameter;
1881 if(graphics->busy)
1882 return ObjectBusy;
1884 if (!graphics->hdc)
1886 FIXME("graphics object has no HDC\n");
1887 return Ok;
1890 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1892 save_state = prepare_dc(graphics, pen);
1894 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1896 restore_dc(graphics, save_state);
1898 return retval;
1901 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1902 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1904 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1905 width, height, startAngle, sweepAngle);
1907 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1910 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1911 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1913 INT save_state;
1914 GpPointF pt[4];
1915 GpStatus retval;
1917 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1918 x2, y2, x3, y3, x4, y4);
1920 if(!graphics || !pen)
1921 return InvalidParameter;
1923 if(graphics->busy)
1924 return ObjectBusy;
1926 if (!graphics->hdc)
1928 FIXME("graphics object has no HDC\n");
1929 return Ok;
1932 pt[0].X = x1;
1933 pt[0].Y = y1;
1934 pt[1].X = x2;
1935 pt[1].Y = y2;
1936 pt[2].X = x3;
1937 pt[2].Y = y3;
1938 pt[3].X = x4;
1939 pt[3].Y = y4;
1941 save_state = prepare_dc(graphics, pen);
1943 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1945 restore_dc(graphics, save_state);
1947 return retval;
1950 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1951 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1953 INT save_state;
1954 GpPointF pt[4];
1955 GpStatus retval;
1957 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1958 x2, y2, x3, y3, x4, y4);
1960 if(!graphics || !pen)
1961 return InvalidParameter;
1963 if(graphics->busy)
1964 return ObjectBusy;
1966 if (!graphics->hdc)
1968 FIXME("graphics object has no HDC\n");
1969 return Ok;
1972 pt[0].X = x1;
1973 pt[0].Y = y1;
1974 pt[1].X = x2;
1975 pt[1].Y = y2;
1976 pt[2].X = x3;
1977 pt[2].Y = y3;
1978 pt[3].X = x4;
1979 pt[3].Y = y4;
1981 save_state = prepare_dc(graphics, pen);
1983 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1985 restore_dc(graphics, save_state);
1987 return retval;
1990 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1991 GDIPCONST GpPointF *points, INT count)
1993 INT i;
1994 GpStatus ret;
1996 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1998 if(!graphics || !pen || !points || (count <= 0))
1999 return InvalidParameter;
2001 if(graphics->busy)
2002 return ObjectBusy;
2004 for(i = 0; i < floor(count / 4); i++){
2005 ret = GdipDrawBezier(graphics, pen,
2006 points[4*i].X, points[4*i].Y,
2007 points[4*i + 1].X, points[4*i + 1].Y,
2008 points[4*i + 2].X, points[4*i + 2].Y,
2009 points[4*i + 3].X, points[4*i + 3].Y);
2010 if(ret != Ok)
2011 return ret;
2014 return Ok;
2017 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2018 GDIPCONST GpPoint *points, INT count)
2020 GpPointF *pts;
2021 GpStatus ret;
2022 INT i;
2024 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2026 if(!graphics || !pen || !points || (count <= 0))
2027 return InvalidParameter;
2029 if(graphics->busy)
2030 return ObjectBusy;
2032 pts = GdipAlloc(sizeof(GpPointF) * count);
2033 if(!pts)
2034 return OutOfMemory;
2036 for(i = 0; i < count; i++){
2037 pts[i].X = (REAL)points[i].X;
2038 pts[i].Y = (REAL)points[i].Y;
2041 ret = GdipDrawBeziers(graphics,pen,pts,count);
2043 GdipFree(pts);
2045 return ret;
2048 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2049 GDIPCONST GpPointF *points, INT count)
2051 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2053 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2056 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2057 GDIPCONST GpPoint *points, INT count)
2059 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2061 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2064 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2065 GDIPCONST GpPointF *points, INT count, REAL tension)
2067 GpPath *path;
2068 GpStatus stat;
2070 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2072 if(!graphics || !pen || !points || count <= 0)
2073 return InvalidParameter;
2075 if(graphics->busy)
2076 return ObjectBusy;
2078 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2079 return stat;
2081 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2082 if(stat != Ok){
2083 GdipDeletePath(path);
2084 return stat;
2087 stat = GdipDrawPath(graphics, pen, path);
2089 GdipDeletePath(path);
2091 return stat;
2094 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2095 GDIPCONST GpPoint *points, INT count, REAL tension)
2097 GpPointF *ptf;
2098 GpStatus stat;
2099 INT i;
2101 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2103 if(!points || count <= 0)
2104 return InvalidParameter;
2106 ptf = GdipAlloc(sizeof(GpPointF)*count);
2107 if(!ptf)
2108 return OutOfMemory;
2110 for(i = 0; i < count; i++){
2111 ptf[i].X = (REAL)points[i].X;
2112 ptf[i].Y = (REAL)points[i].Y;
2115 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2117 GdipFree(ptf);
2119 return stat;
2122 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2123 GDIPCONST GpPointF *points, INT count)
2125 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2127 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2130 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2131 GDIPCONST GpPoint *points, INT count)
2133 GpPointF *pointsF;
2134 GpStatus ret;
2135 INT i;
2137 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2139 if(!points)
2140 return InvalidParameter;
2142 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2143 if(!pointsF)
2144 return OutOfMemory;
2146 for(i = 0; i < count; i++){
2147 pointsF[i].X = (REAL)points[i].X;
2148 pointsF[i].Y = (REAL)points[i].Y;
2151 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2152 GdipFree(pointsF);
2154 return ret;
2157 /* Approximates cardinal spline with Bezier curves. */
2158 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2159 GDIPCONST GpPointF *points, INT count, REAL tension)
2161 /* PolyBezier expects count*3-2 points. */
2162 INT i, len_pt = count*3-2, save_state;
2163 GpPointF *pt;
2164 REAL x1, x2, y1, y2;
2165 GpStatus retval;
2167 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2169 if(!graphics || !pen)
2170 return InvalidParameter;
2172 if(graphics->busy)
2173 return ObjectBusy;
2175 if(count < 2)
2176 return InvalidParameter;
2178 if (!graphics->hdc)
2180 FIXME("graphics object has no HDC\n");
2181 return Ok;
2184 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2185 if(!pt)
2186 return OutOfMemory;
2188 tension = tension * TENSION_CONST;
2190 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2191 tension, &x1, &y1);
2193 pt[0].X = points[0].X;
2194 pt[0].Y = points[0].Y;
2195 pt[1].X = x1;
2196 pt[1].Y = y1;
2198 for(i = 0; i < count-2; i++){
2199 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2201 pt[3*i+2].X = x1;
2202 pt[3*i+2].Y = y1;
2203 pt[3*i+3].X = points[i+1].X;
2204 pt[3*i+3].Y = points[i+1].Y;
2205 pt[3*i+4].X = x2;
2206 pt[3*i+4].Y = y2;
2209 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2210 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2212 pt[len_pt-2].X = x1;
2213 pt[len_pt-2].Y = y1;
2214 pt[len_pt-1].X = points[count-1].X;
2215 pt[len_pt-1].Y = points[count-1].Y;
2217 save_state = prepare_dc(graphics, pen);
2219 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2221 GdipFree(pt);
2222 restore_dc(graphics, save_state);
2224 return retval;
2227 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2228 GDIPCONST GpPoint *points, INT count, REAL tension)
2230 GpPointF *pointsF;
2231 GpStatus ret;
2232 INT i;
2234 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2236 if(!points)
2237 return InvalidParameter;
2239 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2240 if(!pointsF)
2241 return OutOfMemory;
2243 for(i = 0; i < count; i++){
2244 pointsF[i].X = (REAL)points[i].X;
2245 pointsF[i].Y = (REAL)points[i].Y;
2248 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2249 GdipFree(pointsF);
2251 return ret;
2254 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2255 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2256 REAL tension)
2258 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2260 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2261 return InvalidParameter;
2264 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2267 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2268 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2269 REAL tension)
2271 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2273 if(count < 0){
2274 return OutOfMemory;
2277 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2278 return InvalidParameter;
2281 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2284 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2285 REAL y, REAL width, REAL height)
2287 INT save_state;
2288 GpPointF ptf[2];
2289 POINT pti[2];
2291 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2293 if(!graphics || !pen)
2294 return InvalidParameter;
2296 if(graphics->busy)
2297 return ObjectBusy;
2299 if (!graphics->hdc)
2301 FIXME("graphics object has no HDC\n");
2302 return Ok;
2305 ptf[0].X = x;
2306 ptf[0].Y = y;
2307 ptf[1].X = x + width;
2308 ptf[1].Y = y + height;
2310 save_state = prepare_dc(graphics, pen);
2311 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2313 transform_and_round_points(graphics, pti, ptf, 2);
2315 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2317 restore_dc(graphics, save_state);
2319 return Ok;
2322 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2323 INT y, INT width, INT height)
2325 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2327 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2331 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2333 UINT width, height;
2334 GpPointF points[3];
2336 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2338 if(!graphics || !image)
2339 return InvalidParameter;
2341 GdipGetImageWidth(image, &width);
2342 GdipGetImageHeight(image, &height);
2344 /* FIXME: we should use the graphics and image dpi, somehow */
2346 points[0].X = points[2].X = x;
2347 points[0].Y = points[1].Y = y;
2348 points[1].X = x + width;
2349 points[2].Y = y + height;
2351 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
2352 UnitPixel, NULL, NULL, NULL);
2355 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2356 INT y)
2358 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2360 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2363 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2364 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2365 GpUnit srcUnit)
2367 GpPointF points[3];
2368 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2370 points[0].X = points[2].X = x;
2371 points[0].Y = points[1].Y = y;
2373 /* FIXME: convert image coordinates to Graphics coordinates? */
2374 points[1].X = x + srcwidth;
2375 points[2].Y = y + srcheight;
2377 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2378 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2381 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2382 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2383 GpUnit srcUnit)
2385 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2388 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2389 GDIPCONST GpPointF *dstpoints, INT count)
2391 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2392 return NotImplemented;
2395 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2396 GDIPCONST GpPoint *dstpoints, INT count)
2398 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2399 return NotImplemented;
2402 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2403 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2404 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2405 DrawImageAbort callback, VOID * callbackData)
2407 GpPointF ptf[4];
2408 POINT pti[4];
2409 REAL dx, dy;
2410 GpStatus stat;
2412 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2413 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2414 callbackData);
2416 if (count > 3)
2417 return NotImplemented;
2419 if(!graphics || !image || !points || count != 3)
2420 return InvalidParameter;
2422 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2423 debugstr_pointf(&points[2]));
2425 memcpy(ptf, points, 3 * sizeof(GpPointF));
2426 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2427 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2428 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
2429 return Ok;
2430 transform_and_round_points(graphics, pti, ptf, 4);
2432 if (image->picture)
2434 if (!graphics->hdc)
2436 FIXME("graphics object has no HDC\n");
2439 /* FIXME: partially implemented (only works for rectangular parallelograms) */
2440 if(srcUnit == UnitInch)
2441 dx = dy = (REAL) INCH_HIMETRIC;
2442 else if(srcUnit == UnitPixel){
2443 dx = ((REAL) INCH_HIMETRIC) /
2444 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
2445 dy = ((REAL) INCH_HIMETRIC) /
2446 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
2448 else
2449 return NotImplemented;
2451 if(IPicture_Render(image->picture, graphics->hdc,
2452 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2453 srcx * dx, srcy * dy,
2454 srcwidth * dx, srcheight * dy,
2455 NULL) != S_OK){
2456 if(callback)
2457 callback(callbackData);
2458 return GenericError;
2461 else if (image->type == ImageTypeBitmap)
2463 GpBitmap* bitmap = (GpBitmap*)image;
2464 int use_software=0;
2466 if (srcUnit == UnitInch)
2467 dx = dy = 96.0; /* FIXME: use the image resolution */
2468 else if (srcUnit == UnitPixel)
2469 dx = dy = 1.0;
2470 else
2471 return NotImplemented;
2473 srcx = srcx * dx;
2474 srcy = srcy * dy;
2475 srcwidth = srcwidth * dx;
2476 srcheight = srcheight * dy;
2478 if (imageAttributes ||
2479 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2480 !((GpBitmap*)image)->hbitmap ||
2481 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2482 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2483 srcx < 0 || srcy < 0 ||
2484 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2485 use_software = 1;
2487 if (use_software)
2489 RECT dst_area;
2490 GpRect src_area;
2491 int i, x, y, src_stride, dst_stride;
2492 GpMatrix *dst_to_src;
2493 REAL m11, m12, m21, m22, mdx, mdy;
2494 LPBYTE src_data, dst_data;
2495 BitmapData lockeddata;
2496 InterpolationMode interpolation = graphics->interpolation;
2497 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2498 REAL x_dx, x_dy, y_dx, y_dy;
2499 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2501 if (!imageAttributes)
2502 imageAttributes = &defaultImageAttributes;
2504 dst_area.left = dst_area.right = pti[0].x;
2505 dst_area.top = dst_area.bottom = pti[0].y;
2506 for (i=1; i<4; i++)
2508 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2509 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2510 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2511 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2514 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2515 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2516 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2517 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2518 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2519 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2521 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
2522 if (stat != Ok) return stat;
2524 stat = GdipInvertMatrix(dst_to_src);
2525 if (stat != Ok)
2527 GdipDeleteMatrix(dst_to_src);
2528 return stat;
2531 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2532 if (!dst_data)
2534 GdipDeleteMatrix(dst_to_src);
2535 return OutOfMemory;
2538 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2540 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2541 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2543 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2544 if (!src_data)
2546 GdipFree(dst_data);
2547 GdipDeleteMatrix(dst_to_src);
2548 return OutOfMemory;
2550 src_stride = sizeof(ARGB) * src_area.Width;
2552 /* Read the bits we need from the source bitmap into an ARGB buffer. */
2553 lockeddata.Width = src_area.Width;
2554 lockeddata.Height = src_area.Height;
2555 lockeddata.Stride = src_stride;
2556 lockeddata.PixelFormat = PixelFormat32bppARGB;
2557 lockeddata.Scan0 = src_data;
2559 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
2560 PixelFormat32bppARGB, &lockeddata);
2562 if (stat == Ok)
2563 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2565 if (stat != Ok)
2567 if (src_data != dst_data)
2568 GdipFree(src_data);
2569 GdipFree(dst_data);
2570 GdipDeleteMatrix(dst_to_src);
2571 return OutOfMemory;
2574 apply_image_attributes(imageAttributes, src_data,
2575 src_area.Width, src_area.Height,
2576 src_stride, ColorAdjustTypeBitmap);
2578 /* Transform the bits as needed to the destination. */
2579 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
2581 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
2582 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
2583 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
2584 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
2586 for (x=dst_area.left; x<dst_area.right; x++)
2588 for (y=dst_area.top; y<dst_area.bottom; y++)
2590 GpPointF src_pointf;
2591 ARGB *dst_color;
2593 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
2594 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
2596 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2598 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
2599 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
2600 else
2601 *dst_color = 0;
2605 GdipDeleteMatrix(dst_to_src);
2607 GdipFree(src_data);
2609 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
2610 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
2612 GdipFree(dst_data);
2614 return stat;
2616 else
2618 HDC hdc;
2619 int temp_hdc=0, temp_bitmap=0;
2620 HBITMAP hbitmap, old_hbm=NULL;
2622 if (!(bitmap->format == PixelFormat16bppRGB555 ||
2623 bitmap->format == PixelFormat24bppRGB ||
2624 bitmap->format == PixelFormat32bppRGB ||
2625 bitmap->format == PixelFormat32bppPARGB))
2627 BITMAPINFOHEADER bih;
2628 BYTE *temp_bits;
2629 PixelFormat dst_format;
2631 /* we can't draw a bitmap of this format directly */
2632 hdc = CreateCompatibleDC(0);
2633 temp_hdc = 1;
2634 temp_bitmap = 1;
2636 bih.biSize = sizeof(BITMAPINFOHEADER);
2637 bih.biWidth = bitmap->width;
2638 bih.biHeight = -bitmap->height;
2639 bih.biPlanes = 1;
2640 bih.biBitCount = 32;
2641 bih.biCompression = BI_RGB;
2642 bih.biSizeImage = 0;
2643 bih.biXPelsPerMeter = 0;
2644 bih.biYPelsPerMeter = 0;
2645 bih.biClrUsed = 0;
2646 bih.biClrImportant = 0;
2648 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
2649 (void**)&temp_bits, NULL, 0);
2651 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2652 dst_format = PixelFormat32bppPARGB;
2653 else
2654 dst_format = PixelFormat32bppRGB;
2656 convert_pixels(bitmap->width, bitmap->height,
2657 bitmap->width*4, temp_bits, dst_format,
2658 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
2660 else
2662 hbitmap = bitmap->hbitmap;
2663 hdc = bitmap->hdc;
2664 temp_hdc = (hdc == 0);
2667 if (temp_hdc)
2669 if (!hdc) hdc = CreateCompatibleDC(0);
2670 old_hbm = SelectObject(hdc, hbitmap);
2673 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2675 BLENDFUNCTION bf;
2677 bf.BlendOp = AC_SRC_OVER;
2678 bf.BlendFlags = 0;
2679 bf.SourceConstantAlpha = 255;
2680 bf.AlphaFormat = AC_SRC_ALPHA;
2682 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2683 hdc, srcx, srcy, srcwidth, srcheight, bf);
2685 else
2687 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2688 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
2691 if (temp_hdc)
2693 SelectObject(hdc, old_hbm);
2694 DeleteDC(hdc);
2697 if (temp_bitmap)
2698 DeleteObject(hbitmap);
2701 else
2703 ERR("GpImage with no IPicture or HBITMAP?!\n");
2704 return NotImplemented;
2707 return Ok;
2710 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
2711 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
2712 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2713 DrawImageAbort callback, VOID * callbackData)
2715 GpPointF pointsF[3];
2716 INT i;
2718 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
2719 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2720 callbackData);
2722 if(!points || count!=3)
2723 return InvalidParameter;
2725 for(i = 0; i < count; i++){
2726 pointsF[i].X = (REAL)points[i].X;
2727 pointsF[i].Y = (REAL)points[i].Y;
2730 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2731 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2732 callback, callbackData);
2735 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2736 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2737 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2738 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2739 VOID * callbackData)
2741 GpPointF points[3];
2743 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2744 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2745 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2747 points[0].X = dstx;
2748 points[0].Y = dsty;
2749 points[1].X = dstx + dstwidth;
2750 points[1].Y = dsty;
2751 points[2].X = dstx;
2752 points[2].Y = dsty + dstheight;
2754 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2755 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2758 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2759 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2760 INT srcwidth, INT srcheight, GpUnit srcUnit,
2761 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2762 VOID * callbackData)
2764 GpPointF points[3];
2766 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2767 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2768 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2770 points[0].X = dstx;
2771 points[0].Y = dsty;
2772 points[1].X = dstx + dstwidth;
2773 points[1].Y = dsty;
2774 points[2].X = dstx;
2775 points[2].Y = dsty + dstheight;
2777 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2778 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2781 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2782 REAL x, REAL y, REAL width, REAL height)
2784 RectF bounds;
2785 GpUnit unit;
2786 GpStatus ret;
2788 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2790 if(!graphics || !image)
2791 return InvalidParameter;
2793 ret = GdipGetImageBounds(image, &bounds, &unit);
2794 if(ret != Ok)
2795 return ret;
2797 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2798 bounds.X, bounds.Y, bounds.Width, bounds.Height,
2799 unit, NULL, NULL, NULL);
2802 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2803 INT x, INT y, INT width, INT height)
2805 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2807 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2810 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2811 REAL y1, REAL x2, REAL y2)
2813 INT save_state;
2814 GpPointF pt[2];
2815 GpStatus retval;
2817 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2819 if(!pen || !graphics)
2820 return InvalidParameter;
2822 if(graphics->busy)
2823 return ObjectBusy;
2825 if (!graphics->hdc)
2827 FIXME("graphics object has no HDC\n");
2828 return Ok;
2831 pt[0].X = x1;
2832 pt[0].Y = y1;
2833 pt[1].X = x2;
2834 pt[1].Y = y2;
2836 save_state = prepare_dc(graphics, pen);
2838 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2840 restore_dc(graphics, save_state);
2842 return retval;
2845 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2846 INT y1, INT x2, INT y2)
2848 INT save_state;
2849 GpPointF pt[2];
2850 GpStatus retval;
2852 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2854 if(!pen || !graphics)
2855 return InvalidParameter;
2857 if(graphics->busy)
2858 return ObjectBusy;
2860 if (!graphics->hdc)
2862 FIXME("graphics object has no HDC\n");
2863 return Ok;
2866 pt[0].X = (REAL)x1;
2867 pt[0].Y = (REAL)y1;
2868 pt[1].X = (REAL)x2;
2869 pt[1].Y = (REAL)y2;
2871 save_state = prepare_dc(graphics, pen);
2873 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2875 restore_dc(graphics, save_state);
2877 return retval;
2880 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2881 GpPointF *points, INT count)
2883 INT save_state;
2884 GpStatus retval;
2886 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2888 if(!pen || !graphics || (count < 2))
2889 return InvalidParameter;
2891 if(graphics->busy)
2892 return ObjectBusy;
2894 if (!graphics->hdc)
2896 FIXME("graphics object has no HDC\n");
2897 return Ok;
2900 save_state = prepare_dc(graphics, pen);
2902 retval = draw_polyline(graphics, pen, points, count, TRUE);
2904 restore_dc(graphics, save_state);
2906 return retval;
2909 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2910 GpPoint *points, INT count)
2912 INT save_state;
2913 GpStatus retval;
2914 GpPointF *ptf = NULL;
2915 int i;
2917 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2919 if(!pen || !graphics || (count < 2))
2920 return InvalidParameter;
2922 if(graphics->busy)
2923 return ObjectBusy;
2925 if (!graphics->hdc)
2927 FIXME("graphics object has no HDC\n");
2928 return Ok;
2931 ptf = GdipAlloc(count * sizeof(GpPointF));
2932 if(!ptf) return OutOfMemory;
2934 for(i = 0; i < count; i ++){
2935 ptf[i].X = (REAL) points[i].X;
2936 ptf[i].Y = (REAL) points[i].Y;
2939 save_state = prepare_dc(graphics, pen);
2941 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2943 restore_dc(graphics, save_state);
2945 GdipFree(ptf);
2946 return retval;
2949 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2951 INT save_state;
2952 GpStatus retval;
2954 TRACE("(%p, %p, %p)\n", graphics, pen, path);
2956 if(!pen || !graphics)
2957 return InvalidParameter;
2959 if(graphics->busy)
2960 return ObjectBusy;
2962 if (!graphics->hdc)
2964 FIXME("graphics object has no HDC\n");
2965 return Ok;
2968 save_state = prepare_dc(graphics, pen);
2970 retval = draw_poly(graphics, pen, path->pathdata.Points,
2971 path->pathdata.Types, path->pathdata.Count, TRUE);
2973 restore_dc(graphics, save_state);
2975 return retval;
2978 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2979 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2981 INT save_state;
2983 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2984 width, height, startAngle, sweepAngle);
2986 if(!graphics || !pen)
2987 return InvalidParameter;
2989 if(graphics->busy)
2990 return ObjectBusy;
2992 if (!graphics->hdc)
2994 FIXME("graphics object has no HDC\n");
2995 return Ok;
2998 save_state = prepare_dc(graphics, pen);
2999 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3001 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3003 restore_dc(graphics, save_state);
3005 return Ok;
3008 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3009 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3011 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3012 width, height, startAngle, sweepAngle);
3014 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3017 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3018 REAL y, REAL width, REAL height)
3020 INT save_state;
3021 GpPointF ptf[4];
3022 POINT pti[4];
3024 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3026 if(!pen || !graphics)
3027 return InvalidParameter;
3029 if(graphics->busy)
3030 return ObjectBusy;
3032 if (!graphics->hdc)
3034 FIXME("graphics object has no HDC\n");
3035 return Ok;
3038 ptf[0].X = x;
3039 ptf[0].Y = y;
3040 ptf[1].X = x + width;
3041 ptf[1].Y = y;
3042 ptf[2].X = x + width;
3043 ptf[2].Y = y + height;
3044 ptf[3].X = x;
3045 ptf[3].Y = y + height;
3047 save_state = prepare_dc(graphics, pen);
3048 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3050 transform_and_round_points(graphics, pti, ptf, 4);
3051 Polygon(graphics->hdc, pti, 4);
3053 restore_dc(graphics, save_state);
3055 return Ok;
3058 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3059 INT y, INT width, INT height)
3061 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3063 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3066 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3067 GDIPCONST GpRectF* rects, INT count)
3069 GpPointF *ptf;
3070 POINT *pti;
3071 INT save_state, i;
3073 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3075 if(!graphics || !pen || !rects || count < 1)
3076 return InvalidParameter;
3078 if(graphics->busy)
3079 return ObjectBusy;
3081 if (!graphics->hdc)
3083 FIXME("graphics object has no HDC\n");
3084 return Ok;
3087 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3088 pti = GdipAlloc(4 * count * sizeof(POINT));
3090 if(!ptf || !pti){
3091 GdipFree(ptf);
3092 GdipFree(pti);
3093 return OutOfMemory;
3096 for(i = 0; i < count; i++){
3097 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3098 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3099 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3100 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3103 save_state = prepare_dc(graphics, pen);
3104 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3106 transform_and_round_points(graphics, pti, ptf, 4 * count);
3108 for(i = 0; i < count; i++)
3109 Polygon(graphics->hdc, &pti[4 * i], 4);
3111 restore_dc(graphics, save_state);
3113 GdipFree(ptf);
3114 GdipFree(pti);
3116 return Ok;
3119 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3120 GDIPCONST GpRect* rects, INT count)
3122 GpRectF *rectsF;
3123 GpStatus ret;
3124 INT i;
3126 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3128 if(!rects || count<=0)
3129 return InvalidParameter;
3131 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3132 if(!rectsF)
3133 return OutOfMemory;
3135 for(i = 0;i < count;i++){
3136 rectsF[i].X = (REAL)rects[i].X;
3137 rectsF[i].Y = (REAL)rects[i].Y;
3138 rectsF[i].Width = (REAL)rects[i].Width;
3139 rectsF[i].Height = (REAL)rects[i].Height;
3142 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3143 GdipFree(rectsF);
3145 return ret;
3148 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3149 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3151 GpPath *path;
3152 GpStatus stat;
3154 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3155 count, tension, fill);
3157 if(!graphics || !brush || !points)
3158 return InvalidParameter;
3160 if(graphics->busy)
3161 return ObjectBusy;
3163 if(count == 1) /* Do nothing */
3164 return Ok;
3166 stat = GdipCreatePath(fill, &path);
3167 if(stat != Ok)
3168 return stat;
3170 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3171 if(stat != Ok){
3172 GdipDeletePath(path);
3173 return stat;
3176 stat = GdipFillPath(graphics, brush, path);
3177 if(stat != Ok){
3178 GdipDeletePath(path);
3179 return stat;
3182 GdipDeletePath(path);
3184 return Ok;
3187 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3188 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3190 GpPointF *ptf;
3191 GpStatus stat;
3192 INT i;
3194 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3195 count, tension, fill);
3197 if(!points || count == 0)
3198 return InvalidParameter;
3200 if(count == 1) /* Do nothing */
3201 return Ok;
3203 ptf = GdipAlloc(sizeof(GpPointF)*count);
3204 if(!ptf)
3205 return OutOfMemory;
3207 for(i = 0;i < count;i++){
3208 ptf[i].X = (REAL)points[i].X;
3209 ptf[i].Y = (REAL)points[i].Y;
3212 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3214 GdipFree(ptf);
3216 return stat;
3219 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3220 GDIPCONST GpPointF *points, INT count)
3222 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3223 return GdipFillClosedCurve2(graphics, brush, points, count,
3224 0.5f, FillModeAlternate);
3227 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3228 GDIPCONST GpPoint *points, INT count)
3230 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3231 return GdipFillClosedCurve2I(graphics, brush, points, count,
3232 0.5f, FillModeAlternate);
3235 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3236 REAL y, REAL width, REAL height)
3238 GpStatus stat;
3239 GpPath *path;
3241 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3243 if(!graphics || !brush)
3244 return InvalidParameter;
3246 if(graphics->busy)
3247 return ObjectBusy;
3249 stat = GdipCreatePath(FillModeAlternate, &path);
3251 if (stat == Ok)
3253 stat = GdipAddPathEllipse(path, x, y, width, height);
3255 if (stat == Ok)
3256 stat = GdipFillPath(graphics, brush, path);
3258 GdipDeletePath(path);
3261 return stat;
3264 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3265 INT y, INT width, INT height)
3267 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3269 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3272 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3274 INT save_state;
3275 GpStatus retval;
3277 if(!graphics->hdc || !brush_can_fill_path(brush))
3278 return NotImplemented;
3280 save_state = SaveDC(graphics->hdc);
3281 EndPath(graphics->hdc);
3282 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3283 : WINDING));
3285 BeginPath(graphics->hdc);
3286 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3287 path->pathdata.Types, path->pathdata.Count, FALSE);
3289 if(retval != Ok)
3290 goto end;
3292 EndPath(graphics->hdc);
3293 brush_fill_path(graphics, brush);
3295 retval = Ok;
3297 end:
3298 RestoreDC(graphics->hdc, save_state);
3300 return retval;
3303 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3305 GpStatus stat;
3306 GpRegion *rgn;
3308 if (!brush_can_fill_pixels(brush))
3309 return NotImplemented;
3311 /* FIXME: This could probably be done more efficiently without regions. */
3313 stat = GdipCreateRegionPath(path, &rgn);
3315 if (stat == Ok)
3317 stat = GdipFillRegion(graphics, brush, rgn);
3319 GdipDeleteRegion(rgn);
3322 return stat;
3325 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3327 GpStatus stat = NotImplemented;
3329 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3331 if(!brush || !graphics || !path)
3332 return InvalidParameter;
3334 if(graphics->busy)
3335 return ObjectBusy;
3337 if (!graphics->image)
3338 stat = GDI32_GdipFillPath(graphics, brush, path);
3340 if (stat == NotImplemented)
3341 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3343 if (stat == NotImplemented)
3345 FIXME("Not implemented for brushtype %i\n", brush->bt);
3346 stat = Ok;
3349 return stat;
3352 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3353 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3355 GpStatus stat;
3356 GpPath *path;
3358 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3359 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3361 if(!graphics || !brush)
3362 return InvalidParameter;
3364 if(graphics->busy)
3365 return ObjectBusy;
3367 stat = GdipCreatePath(FillModeAlternate, &path);
3369 if (stat == Ok)
3371 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3373 if (stat == Ok)
3374 stat = GdipFillPath(graphics, brush, path);
3376 GdipDeletePath(path);
3379 return stat;
3382 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3383 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3385 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3386 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3388 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3391 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3392 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3394 GpStatus stat;
3395 GpPath *path;
3397 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3399 if(!graphics || !brush || !points || !count)
3400 return InvalidParameter;
3402 if(graphics->busy)
3403 return ObjectBusy;
3405 stat = GdipCreatePath(fillMode, &path);
3407 if (stat == Ok)
3409 stat = GdipAddPathPolygon(path, points, count);
3411 if (stat == Ok)
3412 stat = GdipFillPath(graphics, brush, path);
3414 GdipDeletePath(path);
3417 return stat;
3420 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3421 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3423 GpStatus stat;
3424 GpPath *path;
3426 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3428 if(!graphics || !brush || !points || !count)
3429 return InvalidParameter;
3431 if(graphics->busy)
3432 return ObjectBusy;
3434 stat = GdipCreatePath(fillMode, &path);
3436 if (stat == Ok)
3438 stat = GdipAddPathPolygonI(path, points, count);
3440 if (stat == Ok)
3441 stat = GdipFillPath(graphics, brush, path);
3443 GdipDeletePath(path);
3446 return stat;
3449 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3450 GDIPCONST GpPointF *points, INT count)
3452 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3454 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3457 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3458 GDIPCONST GpPoint *points, INT count)
3460 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3462 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3465 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3466 REAL x, REAL y, REAL width, REAL height)
3468 GpStatus stat;
3469 GpPath *path;
3471 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3473 if(!graphics || !brush)
3474 return InvalidParameter;
3476 if(graphics->busy)
3477 return ObjectBusy;
3479 stat = GdipCreatePath(FillModeAlternate, &path);
3481 if (stat == Ok)
3483 stat = GdipAddPathRectangle(path, x, y, width, height);
3485 if (stat == Ok)
3486 stat = GdipFillPath(graphics, brush, path);
3488 GdipDeletePath(path);
3491 return stat;
3494 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3495 INT x, INT y, INT width, INT height)
3497 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3499 return GdipFillRectangle(graphics, brush, x, y, width, height);
3502 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3503 INT count)
3505 GpStatus ret;
3506 INT i;
3508 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3510 if(!rects)
3511 return InvalidParameter;
3513 for(i = 0; i < count; i++){
3514 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
3515 if(ret != Ok) return ret;
3518 return Ok;
3521 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3522 INT count)
3524 GpRectF *rectsF;
3525 GpStatus ret;
3526 INT i;
3528 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3530 if(!rects || count <= 0)
3531 return InvalidParameter;
3533 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3534 if(!rectsF)
3535 return OutOfMemory;
3537 for(i = 0; i < count; i++){
3538 rectsF[i].X = (REAL)rects[i].X;
3539 rectsF[i].Y = (REAL)rects[i].Y;
3540 rectsF[i].X = (REAL)rects[i].Width;
3541 rectsF[i].Height = (REAL)rects[i].Height;
3544 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3545 GdipFree(rectsF);
3547 return ret;
3550 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3551 GpRegion* region)
3553 INT save_state;
3554 GpStatus status;
3555 HRGN hrgn;
3556 RECT rc;
3558 if(!graphics->hdc || !brush_can_fill_path(brush))
3559 return NotImplemented;
3561 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3562 if(status != Ok)
3563 return status;
3565 save_state = SaveDC(graphics->hdc);
3566 EndPath(graphics->hdc);
3568 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3570 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3572 BeginPath(graphics->hdc);
3573 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3574 EndPath(graphics->hdc);
3576 brush_fill_path(graphics, brush);
3579 RestoreDC(graphics->hdc, save_state);
3581 DeleteObject(hrgn);
3583 return Ok;
3586 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3587 GpRegion* region)
3589 GpStatus stat;
3590 GpRegion *temp_region;
3591 GpMatrix *world_to_device, *identity;
3592 GpRectF graphics_bounds;
3593 UINT scans_count, i;
3594 INT dummy;
3595 GpRect *scans;
3596 DWORD *pixel_data;
3598 if (!brush_can_fill_pixels(brush))
3599 return NotImplemented;
3601 stat = get_graphics_bounds(graphics, &graphics_bounds);
3603 if (stat == Ok)
3604 stat = GdipCloneRegion(region, &temp_region);
3606 if (stat == Ok)
3608 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3609 CoordinateSpaceWorld, &world_to_device);
3611 if (stat == Ok)
3613 stat = GdipTransformRegion(temp_region, world_to_device);
3615 GdipDeleteMatrix(world_to_device);
3618 if (stat == Ok)
3619 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3621 if (stat == Ok)
3622 stat = GdipCreateMatrix(&identity);
3624 if (stat == Ok)
3626 stat = GdipGetRegionScansCount(temp_region, &scans_count, identity);
3628 if (stat == Ok && scans_count != 0)
3630 scans = GdipAlloc(sizeof(*scans) * scans_count);
3631 if (!scans)
3632 stat = OutOfMemory;
3634 if (stat == Ok)
3636 stat = GdipGetRegionScansI(temp_region, scans, &dummy, identity);
3638 if (stat != Ok)
3639 GdipFree(scans);
3643 GdipDeleteMatrix(identity);
3646 GdipDeleteRegion(temp_region);
3649 if (stat == Ok && scans_count == 0)
3650 return Ok;
3652 if (stat == Ok)
3654 if (!graphics->image)
3656 /* If we have to go through gdi32, use as few alpha blends as possible. */
3657 INT min_x, min_y, max_x, max_y;
3658 UINT data_width, data_height;
3660 min_x = scans[0].X;
3661 min_y = scans[0].Y;
3662 max_x = scans[0].X+scans[0].Width;
3663 max_y = scans[0].Y+scans[0].Height;
3665 for (i=1; i<scans_count; i++)
3667 min_x = min(min_x, scans[i].X);
3668 min_y = min(min_y, scans[i].Y);
3669 max_x = max(max_x, scans[i].X+scans[i].Width);
3670 max_y = max(max_y, scans[i].Y+scans[i].Height);
3673 data_width = max_x - min_x;
3674 data_height = max_y - min_y;
3676 pixel_data = GdipAlloc(sizeof(*pixel_data) * data_width * data_height);
3677 if (!pixel_data)
3678 stat = OutOfMemory;
3680 if (stat == Ok)
3682 for (i=0; i<scans_count; i++)
3684 stat = brush_fill_pixels(graphics, brush,
3685 pixel_data + (scans[i].X - min_x) + (scans[i].Y - min_y) * data_width,
3686 &scans[i], data_width);
3688 if (stat != Ok)
3689 break;
3692 if (stat == Ok)
3694 stat = alpha_blend_pixels(graphics, min_x, min_y,
3695 (BYTE*)pixel_data, data_width, data_height,
3696 data_width * 4);
3699 GdipFree(pixel_data);
3702 else
3704 UINT max_size=0;
3706 for (i=0; i<scans_count; i++)
3708 UINT size = scans[i].Width * scans[i].Height;
3710 if (size > max_size)
3711 max_size = size;
3714 pixel_data = GdipAlloc(sizeof(*pixel_data) * max_size);
3715 if (!pixel_data)
3716 stat = OutOfMemory;
3718 if (stat == Ok)
3720 for (i=0; i<scans_count; i++)
3722 stat = brush_fill_pixels(graphics, brush, pixel_data, &scans[i],
3723 scans[i].Width);
3725 if (stat == Ok)
3727 stat = alpha_blend_pixels(graphics, scans[i].X, scans[i].Y,
3728 (BYTE*)pixel_data, scans[i].Width, scans[i].Height,
3729 scans[i].Width * 4);
3732 if (stat != Ok)
3733 break;
3736 GdipFree(pixel_data);
3740 GdipFree(scans);
3743 return stat;
3746 /*****************************************************************************
3747 * GdipFillRegion [GDIPLUS.@]
3749 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3750 GpRegion* region)
3752 GpStatus stat = NotImplemented;
3754 TRACE("(%p, %p, %p)\n", graphics, brush, region);
3756 if (!(graphics && brush && region))
3757 return InvalidParameter;
3759 if(graphics->busy)
3760 return ObjectBusy;
3762 if (!graphics->image)
3763 stat = GDI32_GdipFillRegion(graphics, brush, region);
3765 if (stat == NotImplemented)
3766 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
3768 if (stat == NotImplemented)
3770 FIXME("not implemented for brushtype %i\n", brush->bt);
3771 stat = Ok;
3774 return stat;
3777 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3779 TRACE("(%p,%u)\n", graphics, intention);
3781 if(!graphics)
3782 return InvalidParameter;
3784 if(graphics->busy)
3785 return ObjectBusy;
3787 /* We have no internal operation queue, so there's no need to clear it. */
3789 if (graphics->hdc)
3790 GdiFlush();
3792 return Ok;
3795 /*****************************************************************************
3796 * GdipGetClipBounds [GDIPLUS.@]
3798 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3800 TRACE("(%p, %p)\n", graphics, rect);
3802 if(!graphics)
3803 return InvalidParameter;
3805 if(graphics->busy)
3806 return ObjectBusy;
3808 return GdipGetRegionBounds(graphics->clip, graphics, rect);
3811 /*****************************************************************************
3812 * GdipGetClipBoundsI [GDIPLUS.@]
3814 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3816 TRACE("(%p, %p)\n", graphics, rect);
3818 if(!graphics)
3819 return InvalidParameter;
3821 if(graphics->busy)
3822 return ObjectBusy;
3824 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3827 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3828 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3829 CompositingMode *mode)
3831 TRACE("(%p, %p)\n", graphics, mode);
3833 if(!graphics || !mode)
3834 return InvalidParameter;
3836 if(graphics->busy)
3837 return ObjectBusy;
3839 *mode = graphics->compmode;
3841 return Ok;
3844 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3845 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3846 CompositingQuality *quality)
3848 TRACE("(%p, %p)\n", graphics, quality);
3850 if(!graphics || !quality)
3851 return InvalidParameter;
3853 if(graphics->busy)
3854 return ObjectBusy;
3856 *quality = graphics->compqual;
3858 return Ok;
3861 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3862 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3863 InterpolationMode *mode)
3865 TRACE("(%p, %p)\n", graphics, mode);
3867 if(!graphics || !mode)
3868 return InvalidParameter;
3870 if(graphics->busy)
3871 return ObjectBusy;
3873 *mode = graphics->interpolation;
3875 return Ok;
3878 /* FIXME: Need to handle color depths less than 24bpp */
3879 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3881 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
3883 if(!graphics || !argb)
3884 return InvalidParameter;
3886 if(graphics->busy)
3887 return ObjectBusy;
3889 return Ok;
3892 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3894 TRACE("(%p, %p)\n", graphics, scale);
3896 if(!graphics || !scale)
3897 return InvalidParameter;
3899 if(graphics->busy)
3900 return ObjectBusy;
3902 *scale = graphics->scale;
3904 return Ok;
3907 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3909 TRACE("(%p, %p)\n", graphics, unit);
3911 if(!graphics || !unit)
3912 return InvalidParameter;
3914 if(graphics->busy)
3915 return ObjectBusy;
3917 *unit = graphics->unit;
3919 return Ok;
3922 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3923 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3924 *mode)
3926 TRACE("(%p, %p)\n", graphics, mode);
3928 if(!graphics || !mode)
3929 return InvalidParameter;
3931 if(graphics->busy)
3932 return ObjectBusy;
3934 *mode = graphics->pixeloffset;
3936 return Ok;
3939 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3940 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3942 TRACE("(%p, %p)\n", graphics, mode);
3944 if(!graphics || !mode)
3945 return InvalidParameter;
3947 if(graphics->busy)
3948 return ObjectBusy;
3950 *mode = graphics->smoothing;
3952 return Ok;
3955 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3957 TRACE("(%p, %p)\n", graphics, contrast);
3959 if(!graphics || !contrast)
3960 return InvalidParameter;
3962 *contrast = graphics->textcontrast;
3964 return Ok;
3967 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3968 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3969 TextRenderingHint *hint)
3971 TRACE("(%p, %p)\n", graphics, hint);
3973 if(!graphics || !hint)
3974 return InvalidParameter;
3976 if(graphics->busy)
3977 return ObjectBusy;
3979 *hint = graphics->texthint;
3981 return Ok;
3984 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3986 GpRegion *clip_rgn;
3987 GpStatus stat;
3989 TRACE("(%p, %p)\n", graphics, rect);
3991 if(!graphics || !rect)
3992 return InvalidParameter;
3994 if(graphics->busy)
3995 return ObjectBusy;
3997 /* intersect window and graphics clipping regions */
3998 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3999 return stat;
4001 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4002 goto cleanup;
4004 /* get bounds of the region */
4005 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4007 cleanup:
4008 GdipDeleteRegion(clip_rgn);
4010 return stat;
4013 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4015 GpRectF rectf;
4016 GpStatus stat;
4018 TRACE("(%p, %p)\n", graphics, rect);
4020 if(!graphics || !rect)
4021 return InvalidParameter;
4023 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4025 rect->X = roundr(rectf.X);
4026 rect->Y = roundr(rectf.Y);
4027 rect->Width = roundr(rectf.Width);
4028 rect->Height = roundr(rectf.Height);
4031 return stat;
4034 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4036 TRACE("(%p, %p)\n", graphics, matrix);
4038 if(!graphics || !matrix)
4039 return InvalidParameter;
4041 if(graphics->busy)
4042 return ObjectBusy;
4044 *matrix = *graphics->worldtrans;
4045 return Ok;
4048 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4050 GpSolidFill *brush;
4051 GpStatus stat;
4052 GpRectF wnd_rect;
4054 TRACE("(%p, %x)\n", graphics, color);
4056 if(!graphics)
4057 return InvalidParameter;
4059 if(graphics->busy)
4060 return ObjectBusy;
4062 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4063 return stat;
4065 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4066 GdipDeleteBrush((GpBrush*)brush);
4067 return stat;
4070 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4071 wnd_rect.Width, wnd_rect.Height);
4073 GdipDeleteBrush((GpBrush*)brush);
4075 return Ok;
4078 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4080 TRACE("(%p, %p)\n", graphics, res);
4082 if(!graphics || !res)
4083 return InvalidParameter;
4085 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4088 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4090 GpStatus stat;
4091 GpRegion* rgn;
4092 GpPointF pt;
4094 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4096 if(!graphics || !result)
4097 return InvalidParameter;
4099 if(graphics->busy)
4100 return ObjectBusy;
4102 pt.X = x;
4103 pt.Y = y;
4104 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4105 CoordinateSpaceWorld, &pt, 1)) != Ok)
4106 return stat;
4108 if((stat = GdipCreateRegion(&rgn)) != Ok)
4109 return stat;
4111 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4112 goto cleanup;
4114 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4116 cleanup:
4117 GdipDeleteRegion(rgn);
4118 return stat;
4121 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4123 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4126 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4128 GpStatus stat;
4129 GpRegion* rgn;
4130 GpPointF pts[2];
4132 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4134 if(!graphics || !result)
4135 return InvalidParameter;
4137 if(graphics->busy)
4138 return ObjectBusy;
4140 pts[0].X = x;
4141 pts[0].Y = y;
4142 pts[1].X = x + width;
4143 pts[1].Y = y + height;
4145 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4146 CoordinateSpaceWorld, pts, 2)) != Ok)
4147 return stat;
4149 pts[1].X -= pts[0].X;
4150 pts[1].Y -= pts[0].Y;
4152 if((stat = GdipCreateRegion(&rgn)) != Ok)
4153 return stat;
4155 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4156 goto cleanup;
4158 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4160 cleanup:
4161 GdipDeleteRegion(rgn);
4162 return stat;
4165 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4167 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4170 GpStatus gdip_format_string(HDC hdc,
4171 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4172 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4173 gdip_format_string_callback callback, void *user_data)
4175 WCHAR* stringdup;
4176 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4177 nheight, lineend, lineno = 0;
4178 RectF bounds;
4179 StringAlignment halign;
4180 GpStatus stat = Ok;
4181 SIZE size;
4183 if(length == -1) length = lstrlenW(string);
4185 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4186 if(!stringdup) return OutOfMemory;
4188 nwidth = roundr(rect->Width);
4189 nheight = roundr(rect->Height);
4191 if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
4192 if (rect->Height >= INT_MAX || rect->Width < 0.5) nheight = INT_MAX;
4194 for(i = 0, j = 0; i < length; i++){
4195 /* FIXME: This makes the indexes passed to callback inaccurate. */
4196 if(!isprintW(string[i]) && (string[i] != '\n'))
4197 continue;
4199 stringdup[j] = string[i];
4200 j++;
4203 length = j;
4205 if (format) halign = format->align;
4206 else halign = StringAlignmentNear;
4208 while(sum < length){
4209 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4210 nwidth, &fit, NULL, &size);
4211 fitcpy = fit;
4213 if(fit == 0)
4214 break;
4216 for(lret = 0; lret < fit; lret++)
4217 if(*(stringdup + sum + lret) == '\n')
4218 break;
4220 /* Line break code (may look strange, but it imitates windows). */
4221 if(lret < fit)
4222 lineend = fit = lret; /* this is not an off-by-one error */
4223 else if(fit < (length - sum)){
4224 if(*(stringdup + sum + fit) == ' ')
4225 while(*(stringdup + sum + fit) == ' ')
4226 fit++;
4227 else
4228 while(*(stringdup + sum + fit - 1) != ' '){
4229 fit--;
4231 if(*(stringdup + sum + fit) == '\t')
4232 break;
4234 if(fit == 0){
4235 fit = fitcpy;
4236 break;
4239 lineend = fit;
4240 while(*(stringdup + sum + lineend - 1) == ' ' ||
4241 *(stringdup + sum + lineend - 1) == '\t')
4242 lineend--;
4244 else
4245 lineend = fit;
4247 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4248 nwidth, &j, NULL, &size);
4250 bounds.Width = size.cx;
4252 if(height + size.cy > nheight)
4253 bounds.Height = nheight - (height + size.cy);
4254 else
4255 bounds.Height = size.cy;
4257 bounds.Y = rect->Y + height;
4259 switch (halign)
4261 case StringAlignmentNear:
4262 default:
4263 bounds.X = rect->X;
4264 break;
4265 case StringAlignmentCenter:
4266 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4267 break;
4268 case StringAlignmentFar:
4269 bounds.X = rect->X + rect->Width - bounds.Width;
4270 break;
4273 stat = callback(hdc, stringdup, sum, lineend,
4274 font, rect, format, lineno, &bounds, user_data);
4276 if (stat != Ok)
4277 break;
4279 sum += fit + (lret < fitcpy ? 1 : 0);
4280 height += size.cy;
4281 lineno++;
4283 if(height > nheight)
4284 break;
4286 /* Stop if this was a linewrap (but not if it was a linebreak). */
4287 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4288 break;
4291 GdipFree(stringdup);
4293 return stat;
4296 struct measure_ranges_args {
4297 GpRegion **regions;
4300 static GpStatus measure_ranges_callback(HDC hdc,
4301 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4302 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4303 INT lineno, const RectF *bounds, void *user_data)
4305 int i;
4306 GpStatus stat = Ok;
4307 struct measure_ranges_args *args = user_data;
4309 for (i=0; i<format->range_count; i++)
4311 INT range_start = max(index, format->character_ranges[i].First);
4312 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4313 if (range_start < range_end)
4315 GpRectF range_rect;
4316 SIZE range_size;
4318 range_rect.Y = bounds->Y;
4319 range_rect.Height = bounds->Height;
4321 GetTextExtentExPointW(hdc, string + index, range_start - index,
4322 INT_MAX, NULL, NULL, &range_size);
4323 range_rect.X = bounds->X + range_size.cx;
4325 GetTextExtentExPointW(hdc, string + index, range_end - index,
4326 INT_MAX, NULL, NULL, &range_size);
4327 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
4329 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4330 if (stat != Ok)
4331 break;
4335 return stat;
4338 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4339 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4340 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4341 INT regionCount, GpRegion** regions)
4343 GpStatus stat;
4344 int i;
4345 HFONT oldfont;
4346 struct measure_ranges_args args;
4347 HDC hdc, temp_hdc=NULL;
4349 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4350 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4352 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4353 return InvalidParameter;
4355 if (regionCount < stringFormat->range_count)
4356 return InvalidParameter;
4358 if(!graphics->hdc)
4360 hdc = temp_hdc = CreateCompatibleDC(0);
4361 if (!temp_hdc) return OutOfMemory;
4363 else
4364 hdc = graphics->hdc;
4366 if (stringFormat->attr)
4367 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4369 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4371 for (i=0; i<stringFormat->range_count; i++)
4373 stat = GdipSetEmpty(regions[i]);
4374 if (stat != Ok)
4375 return stat;
4378 args.regions = regions;
4380 stat = gdip_format_string(hdc, string, length, font, layoutRect, stringFormat,
4381 measure_ranges_callback, &args);
4383 DeleteObject(SelectObject(hdc, oldfont));
4385 if (temp_hdc)
4386 DeleteDC(temp_hdc);
4388 return stat;
4391 struct measure_string_args {
4392 RectF *bounds;
4393 INT *codepointsfitted;
4394 INT *linesfilled;
4397 static GpStatus measure_string_callback(HDC hdc,
4398 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4399 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4400 INT lineno, const RectF *bounds, void *user_data)
4402 struct measure_string_args *args = user_data;
4404 if (bounds->Width > args->bounds->Width)
4405 args->bounds->Width = bounds->Width;
4407 if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
4408 args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
4410 if (args->codepointsfitted)
4411 *args->codepointsfitted = index + length;
4413 if (args->linesfilled)
4414 (*args->linesfilled)++;
4416 return Ok;
4419 /* Find the smallest rectangle that bounds the text when it is printed in rect
4420 * according to the format options listed in format. If rect has 0 width and
4421 * height, then just find the smallest rectangle that bounds the text when it's
4422 * printed at location (rect->X, rect-Y). */
4423 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4424 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4425 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4426 INT *codepointsfitted, INT *linesfilled)
4428 HFONT oldfont;
4429 struct measure_string_args args;
4430 HDC temp_hdc=NULL, hdc;
4432 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4433 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4434 bounds, codepointsfitted, linesfilled);
4436 if(!graphics || !string || !font || !rect || !bounds)
4437 return InvalidParameter;
4439 if(!graphics->hdc)
4441 hdc = temp_hdc = CreateCompatibleDC(0);
4442 if (!temp_hdc) return OutOfMemory;
4444 else
4445 hdc = graphics->hdc;
4447 if(linesfilled) *linesfilled = 0;
4448 if(codepointsfitted) *codepointsfitted = 0;
4450 if(format)
4451 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4453 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4455 bounds->X = rect->X;
4456 bounds->Y = rect->Y;
4457 bounds->Width = 0.0;
4458 bounds->Height = 0.0;
4460 args.bounds = bounds;
4461 args.codepointsfitted = codepointsfitted;
4462 args.linesfilled = linesfilled;
4464 gdip_format_string(hdc, string, length, font, rect, format,
4465 measure_string_callback, &args);
4467 DeleteObject(SelectObject(hdc, oldfont));
4469 if (temp_hdc)
4470 DeleteDC(temp_hdc);
4472 return Ok;
4475 struct draw_string_args {
4476 POINT drawbase;
4477 UINT drawflags;
4478 REAL ang_cos, ang_sin;
4481 static GpStatus draw_string_callback(HDC hdc,
4482 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4483 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4484 INT lineno, const RectF *bounds, void *user_data)
4486 struct draw_string_args *args = user_data;
4487 RECT drawcoord;
4489 drawcoord.left = drawcoord.right = args->drawbase.x + roundr(args->ang_sin * bounds->Y);
4490 drawcoord.top = drawcoord.bottom = args->drawbase.y + roundr(args->ang_cos * bounds->Y);
4492 DrawTextW(hdc, string + index, length, &drawcoord, args->drawflags);
4494 return Ok;
4497 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4498 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4499 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4501 HRGN rgn = NULL;
4502 HFONT gdifont;
4503 LOGFONTW lfw;
4504 TEXTMETRICW textmet;
4505 GpPointF pt[3], rectcpy[4];
4506 POINT corners[4];
4507 REAL angle, rel_width, rel_height;
4508 INT offsety = 0, save_state;
4509 struct draw_string_args args;
4510 RectF scaled_rect;
4512 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4513 length, font, debugstr_rectf(rect), format, brush);
4515 if(!graphics || !string || !font || !brush || !rect)
4516 return InvalidParameter;
4518 if((brush->bt != BrushTypeSolidColor)){
4519 FIXME("not implemented for given parameters\n");
4520 return NotImplemented;
4523 if(!graphics->hdc)
4525 FIXME("graphics object has no HDC\n");
4526 return Ok;
4529 if(format){
4530 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4532 /* Should be no need to explicitly test for StringAlignmentNear as
4533 * that is default behavior if no alignment is passed. */
4534 if(format->vertalign != StringAlignmentNear){
4535 RectF bounds;
4536 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
4538 if(format->vertalign == StringAlignmentCenter)
4539 offsety = (rect->Height - bounds.Height) / 2;
4540 else if(format->vertalign == StringAlignmentFar)
4541 offsety = (rect->Height - bounds.Height);
4545 save_state = SaveDC(graphics->hdc);
4546 SetBkMode(graphics->hdc, TRANSPARENT);
4547 SetTextColor(graphics->hdc, brush->lb.lbColor);
4549 pt[0].X = 0.0;
4550 pt[0].Y = 0.0;
4551 pt[1].X = 1.0;
4552 pt[1].Y = 0.0;
4553 pt[2].X = 0.0;
4554 pt[2].Y = 1.0;
4555 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4556 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
4557 args.ang_cos = cos(angle);
4558 args.ang_sin = sin(angle);
4559 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4560 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4561 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4562 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4564 rectcpy[3].X = rectcpy[0].X = rect->X;
4565 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
4566 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4567 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
4568 transform_and_round_points(graphics, corners, rectcpy, 4);
4570 scaled_rect.X = 0.0;
4571 scaled_rect.Y = 0.0;
4572 scaled_rect.Width = rel_width * rect->Width;
4573 scaled_rect.Height = rel_height * rect->Height;
4575 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
4577 /* FIXME: If only the width or only the height is 0, we should probably still clip */
4578 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4579 SelectClipRgn(graphics->hdc, rgn);
4582 /* Use gdi to find the font, then perform transformations on it (height,
4583 * width, angle). */
4584 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4585 GetTextMetricsW(graphics->hdc, &textmet);
4586 lfw = font->lfw;
4588 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
4589 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
4591 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
4593 gdifont = CreateFontIndirectW(&lfw);
4594 DeleteObject(SelectObject(graphics->hdc, gdifont));
4596 if (!format || format->align == StringAlignmentNear)
4598 args.drawbase.x = corners[0].x;
4599 args.drawbase.y = corners[0].y;
4600 args.drawflags = DT_NOCLIP | DT_EXPANDTABS;
4602 else if (format->align == StringAlignmentCenter)
4604 args.drawbase.x = (corners[0].x + corners[1].x)/2;
4605 args.drawbase.y = (corners[0].y + corners[1].y)/2;
4606 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
4608 else /* (format->align == StringAlignmentFar) */
4610 args.drawbase.x = corners[1].x;
4611 args.drawbase.y = corners[1].y;
4612 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
4615 gdip_format_string(graphics->hdc, string, length, font, &scaled_rect, format,
4616 draw_string_callback, &args);
4618 DeleteObject(rgn);
4619 DeleteObject(gdifont);
4621 RestoreDC(graphics->hdc, save_state);
4623 return Ok;
4626 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
4628 TRACE("(%p)\n", graphics);
4630 if(!graphics)
4631 return InvalidParameter;
4633 if(graphics->busy)
4634 return ObjectBusy;
4636 return GdipSetInfinite(graphics->clip);
4639 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
4641 TRACE("(%p)\n", graphics);
4643 if(!graphics)
4644 return InvalidParameter;
4646 if(graphics->busy)
4647 return ObjectBusy;
4649 graphics->worldtrans->matrix[0] = 1.0;
4650 graphics->worldtrans->matrix[1] = 0.0;
4651 graphics->worldtrans->matrix[2] = 0.0;
4652 graphics->worldtrans->matrix[3] = 1.0;
4653 graphics->worldtrans->matrix[4] = 0.0;
4654 graphics->worldtrans->matrix[5] = 0.0;
4656 return Ok;
4659 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
4661 return GdipEndContainer(graphics, state);
4664 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
4665 GpMatrixOrder order)
4667 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
4669 if(!graphics)
4670 return InvalidParameter;
4672 if(graphics->busy)
4673 return ObjectBusy;
4675 return GdipRotateMatrix(graphics->worldtrans, angle, order);
4678 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
4680 return GdipBeginContainer2(graphics, state);
4683 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
4684 GraphicsContainer *state)
4686 GraphicsContainerItem *container;
4687 GpStatus sts;
4689 TRACE("(%p, %p)\n", graphics, state);
4691 if(!graphics || !state)
4692 return InvalidParameter;
4694 sts = init_container(&container, graphics);
4695 if(sts != Ok)
4696 return sts;
4698 list_add_head(&graphics->containers, &container->entry);
4699 *state = graphics->contid = container->contid;
4701 return Ok;
4704 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
4706 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4707 return NotImplemented;
4710 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
4712 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4713 return NotImplemented;
4716 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
4718 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
4719 return NotImplemented;
4722 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
4724 GpStatus sts;
4725 GraphicsContainerItem *container, *container2;
4727 TRACE("(%p, %x)\n", graphics, state);
4729 if(!graphics)
4730 return InvalidParameter;
4732 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
4733 if(container->contid == state)
4734 break;
4737 /* did not find a matching container */
4738 if(&container->entry == &graphics->containers)
4739 return Ok;
4741 sts = restore_container(graphics, container);
4742 if(sts != Ok)
4743 return sts;
4745 /* remove all of the containers on top of the found container */
4746 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
4747 if(container->contid == state)
4748 break;
4749 list_remove(&container->entry);
4750 delete_container(container);
4753 list_remove(&container->entry);
4754 delete_container(container);
4756 return Ok;
4759 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
4760 REAL sy, GpMatrixOrder order)
4762 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
4764 if(!graphics)
4765 return InvalidParameter;
4767 if(graphics->busy)
4768 return ObjectBusy;
4770 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
4773 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
4774 CombineMode mode)
4776 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
4778 if(!graphics || !srcgraphics)
4779 return InvalidParameter;
4781 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
4784 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
4785 CompositingMode mode)
4787 TRACE("(%p, %d)\n", graphics, mode);
4789 if(!graphics)
4790 return InvalidParameter;
4792 if(graphics->busy)
4793 return ObjectBusy;
4795 graphics->compmode = mode;
4797 return Ok;
4800 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
4801 CompositingQuality quality)
4803 TRACE("(%p, %d)\n", graphics, quality);
4805 if(!graphics)
4806 return InvalidParameter;
4808 if(graphics->busy)
4809 return ObjectBusy;
4811 graphics->compqual = quality;
4813 return Ok;
4816 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
4817 InterpolationMode mode)
4819 TRACE("(%p, %d)\n", graphics, mode);
4821 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
4822 return InvalidParameter;
4824 if(graphics->busy)
4825 return ObjectBusy;
4827 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
4828 mode = InterpolationModeBilinear;
4830 if (mode == InterpolationModeHighQuality)
4831 mode = InterpolationModeHighQualityBicubic;
4833 graphics->interpolation = mode;
4835 return Ok;
4838 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
4840 TRACE("(%p, %.2f)\n", graphics, scale);
4842 if(!graphics || (scale <= 0.0))
4843 return InvalidParameter;
4845 if(graphics->busy)
4846 return ObjectBusy;
4848 graphics->scale = scale;
4850 return Ok;
4853 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4855 TRACE("(%p, %d)\n", graphics, unit);
4857 if(!graphics)
4858 return InvalidParameter;
4860 if(graphics->busy)
4861 return ObjectBusy;
4863 if(unit == UnitWorld)
4864 return InvalidParameter;
4866 graphics->unit = unit;
4868 return Ok;
4871 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4872 mode)
4874 TRACE("(%p, %d)\n", graphics, mode);
4876 if(!graphics)
4877 return InvalidParameter;
4879 if(graphics->busy)
4880 return ObjectBusy;
4882 graphics->pixeloffset = mode;
4884 return Ok;
4887 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
4889 static int calls;
4891 TRACE("(%p,%i,%i)\n", graphics, x, y);
4893 if (!(calls++))
4894 FIXME("not implemented\n");
4896 return NotImplemented;
4899 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
4901 static int calls;
4903 TRACE("(%p,%p,%p)\n", graphics, x, y);
4905 if (!(calls++))
4906 FIXME("not implemented\n");
4908 *x = *y = 0;
4910 return NotImplemented;
4913 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
4915 TRACE("(%p, %d)\n", graphics, mode);
4917 if(!graphics)
4918 return InvalidParameter;
4920 if(graphics->busy)
4921 return ObjectBusy;
4923 graphics->smoothing = mode;
4925 return Ok;
4928 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
4930 TRACE("(%p, %d)\n", graphics, contrast);
4932 if(!graphics)
4933 return InvalidParameter;
4935 graphics->textcontrast = contrast;
4937 return Ok;
4940 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
4941 TextRenderingHint hint)
4943 TRACE("(%p, %d)\n", graphics, hint);
4945 if(!graphics)
4946 return InvalidParameter;
4948 if(graphics->busy)
4949 return ObjectBusy;
4951 graphics->texthint = hint;
4953 return Ok;
4956 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4958 TRACE("(%p, %p)\n", graphics, matrix);
4960 if(!graphics || !matrix)
4961 return InvalidParameter;
4963 if(graphics->busy)
4964 return ObjectBusy;
4966 GdipDeleteMatrix(graphics->worldtrans);
4967 return GdipCloneMatrix(matrix, &graphics->worldtrans);
4970 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
4971 REAL dy, GpMatrixOrder order)
4973 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
4975 if(!graphics)
4976 return InvalidParameter;
4978 if(graphics->busy)
4979 return ObjectBusy;
4981 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
4984 /*****************************************************************************
4985 * GdipSetClipHrgn [GDIPLUS.@]
4987 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
4989 GpRegion *region;
4990 GpStatus status;
4992 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
4994 if(!graphics)
4995 return InvalidParameter;
4997 status = GdipCreateRegionHrgn(hrgn, &region);
4998 if(status != Ok)
4999 return status;
5001 status = GdipSetClipRegion(graphics, region, mode);
5003 GdipDeleteRegion(region);
5004 return status;
5007 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5009 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5011 if(!graphics)
5012 return InvalidParameter;
5014 if(graphics->busy)
5015 return ObjectBusy;
5017 return GdipCombineRegionPath(graphics->clip, path, mode);
5020 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5021 REAL width, REAL height,
5022 CombineMode mode)
5024 GpRectF rect;
5026 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5028 if(!graphics)
5029 return InvalidParameter;
5031 if(graphics->busy)
5032 return ObjectBusy;
5034 rect.X = x;
5035 rect.Y = y;
5036 rect.Width = width;
5037 rect.Height = height;
5039 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5042 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5043 INT width, INT height,
5044 CombineMode mode)
5046 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5048 if(!graphics)
5049 return InvalidParameter;
5051 if(graphics->busy)
5052 return ObjectBusy;
5054 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5057 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5058 CombineMode mode)
5060 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5062 if(!graphics || !region)
5063 return InvalidParameter;
5065 if(graphics->busy)
5066 return ObjectBusy;
5068 return GdipCombineRegionRegion(graphics->clip, region, mode);
5071 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5072 UINT limitDpi)
5074 static int calls;
5076 TRACE("(%p,%u)\n", metafile, limitDpi);
5078 if(!(calls++))
5079 FIXME("not implemented\n");
5081 return NotImplemented;
5084 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5085 INT count)
5087 INT save_state;
5088 POINT *pti;
5090 TRACE("(%p, %p, %d)\n", graphics, points, count);
5092 if(!graphics || !pen || count<=0)
5093 return InvalidParameter;
5095 if(graphics->busy)
5096 return ObjectBusy;
5098 if (!graphics->hdc)
5100 FIXME("graphics object has no HDC\n");
5101 return Ok;
5104 pti = GdipAlloc(sizeof(POINT) * count);
5106 save_state = prepare_dc(graphics, pen);
5107 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5109 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5110 Polygon(graphics->hdc, pti, count);
5112 restore_dc(graphics, save_state);
5113 GdipFree(pti);
5115 return Ok;
5118 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5119 INT count)
5121 GpStatus ret;
5122 GpPointF *ptf;
5123 INT i;
5125 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5127 if(count<=0) return InvalidParameter;
5128 ptf = GdipAlloc(sizeof(GpPointF) * count);
5130 for(i = 0;i < count; i++){
5131 ptf[i].X = (REAL)points[i].X;
5132 ptf[i].Y = (REAL)points[i].Y;
5135 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5136 GdipFree(ptf);
5138 return ret;
5141 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5143 TRACE("(%p, %p)\n", graphics, dpi);
5145 if(!graphics || !dpi)
5146 return InvalidParameter;
5148 if(graphics->busy)
5149 return ObjectBusy;
5151 if (graphics->image)
5152 *dpi = graphics->image->xres;
5153 else
5154 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
5156 return Ok;
5159 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5161 TRACE("(%p, %p)\n", graphics, dpi);
5163 if(!graphics || !dpi)
5164 return InvalidParameter;
5166 if(graphics->busy)
5167 return ObjectBusy;
5169 if (graphics->image)
5170 *dpi = graphics->image->yres;
5171 else
5172 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
5174 return Ok;
5177 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5178 GpMatrixOrder order)
5180 GpMatrix m;
5181 GpStatus ret;
5183 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5185 if(!graphics || !matrix)
5186 return InvalidParameter;
5188 if(graphics->busy)
5189 return ObjectBusy;
5191 m = *(graphics->worldtrans);
5193 ret = GdipMultiplyMatrix(&m, matrix, order);
5194 if(ret == Ok)
5195 *(graphics->worldtrans) = m;
5197 return ret;
5200 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5201 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5203 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5205 TRACE("(%p, %p)\n", graphics, hdc);
5207 if(!graphics || !hdc)
5208 return InvalidParameter;
5210 if(graphics->busy)
5211 return ObjectBusy;
5213 if (!graphics->hdc ||
5214 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5216 /* Create a fake HDC and fill it with a constant color. */
5217 HDC temp_hdc;
5218 HBITMAP hbitmap;
5219 GpStatus stat;
5220 GpRectF bounds;
5221 BITMAPINFOHEADER bmih;
5222 int i;
5224 stat = get_graphics_bounds(graphics, &bounds);
5225 if (stat != Ok)
5226 return stat;
5228 graphics->temp_hbitmap_width = bounds.Width;
5229 graphics->temp_hbitmap_height = bounds.Height;
5231 bmih.biSize = sizeof(bmih);
5232 bmih.biWidth = graphics->temp_hbitmap_width;
5233 bmih.biHeight = -graphics->temp_hbitmap_height;
5234 bmih.biPlanes = 1;
5235 bmih.biBitCount = 32;
5236 bmih.biCompression = BI_RGB;
5237 bmih.biSizeImage = 0;
5238 bmih.biXPelsPerMeter = 0;
5239 bmih.biYPelsPerMeter = 0;
5240 bmih.biClrUsed = 0;
5241 bmih.biClrImportant = 0;
5243 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5244 (void**)&graphics->temp_bits, NULL, 0);
5245 if (!hbitmap)
5246 return GenericError;
5248 temp_hdc = CreateCompatibleDC(0);
5249 if (!temp_hdc)
5251 DeleteObject(hbitmap);
5252 return GenericError;
5255 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5256 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5258 SelectObject(temp_hdc, hbitmap);
5260 graphics->temp_hbitmap = hbitmap;
5261 *hdc = graphics->temp_hdc = temp_hdc;
5263 else
5265 *hdc = graphics->hdc;
5268 graphics->busy = TRUE;
5270 return Ok;
5273 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5275 TRACE("(%p, %p)\n", graphics, hdc);
5277 if(!graphics || !hdc)
5278 return InvalidParameter;
5280 if((graphics->hdc != hdc && graphics->temp_hdc != hdc) || !(graphics->busy))
5281 return InvalidParameter;
5283 if (graphics->temp_hdc == hdc)
5285 DWORD* pos;
5286 int i;
5288 /* Find the pixels that have changed, and mark them as opaque. */
5289 pos = (DWORD*)graphics->temp_bits;
5290 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5292 if (*pos != DC_BACKGROUND_KEY)
5294 *pos |= 0xff000000;
5296 pos++;
5299 /* Write the changed pixels to the real target. */
5300 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5301 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5302 graphics->temp_hbitmap_width * 4);
5304 /* Clean up. */
5305 DeleteDC(graphics->temp_hdc);
5306 DeleteObject(graphics->temp_hbitmap);
5307 graphics->temp_hdc = NULL;
5308 graphics->temp_hbitmap = NULL;
5311 graphics->busy = FALSE;
5313 return Ok;
5316 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5318 GpRegion *clip;
5319 GpStatus status;
5321 TRACE("(%p, %p)\n", graphics, region);
5323 if(!graphics || !region)
5324 return InvalidParameter;
5326 if(graphics->busy)
5327 return ObjectBusy;
5329 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5330 return status;
5332 /* free everything except root node and header */
5333 delete_element(&region->node);
5334 memcpy(region, clip, sizeof(GpRegion));
5335 GdipFree(clip);
5337 return Ok;
5340 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5341 GpCoordinateSpace src_space, GpMatrix **matrix)
5343 GpStatus stat = GdipCreateMatrix(matrix);
5344 REAL unitscale;
5346 if (dst_space != src_space && stat == Ok)
5348 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
5350 if(graphics->unit != UnitDisplay)
5351 unitscale *= graphics->scale;
5353 /* transform from src_space to CoordinateSpacePage */
5354 switch (src_space)
5356 case CoordinateSpaceWorld:
5357 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5358 break;
5359 case CoordinateSpacePage:
5360 break;
5361 case CoordinateSpaceDevice:
5362 GdipScaleMatrix(*matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
5363 break;
5366 /* transform from CoordinateSpacePage to dst_space */
5367 switch (dst_space)
5369 case CoordinateSpaceWorld:
5371 GpMatrix *inverted_transform;
5372 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
5373 if (stat == Ok)
5375 stat = GdipInvertMatrix(inverted_transform);
5376 if (stat == Ok)
5377 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
5378 GdipDeleteMatrix(inverted_transform);
5380 break;
5382 case CoordinateSpacePage:
5383 break;
5384 case CoordinateSpaceDevice:
5385 GdipScaleMatrix(*matrix, unitscale, unitscale, MatrixOrderAppend);
5386 break;
5389 return stat;
5392 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5393 GpCoordinateSpace src_space, GpPointF *points, INT count)
5395 GpMatrix *matrix;
5396 GpStatus stat;
5398 if(!graphics || !points || count <= 0)
5399 return InvalidParameter;
5401 if(graphics->busy)
5402 return ObjectBusy;
5404 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5406 if (src_space == dst_space) return Ok;
5408 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5410 if (stat == Ok)
5412 stat = GdipTransformMatrixPoints(matrix, points, count);
5414 GdipDeleteMatrix(matrix);
5417 return stat;
5420 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5421 GpCoordinateSpace src_space, GpPoint *points, INT count)
5423 GpPointF *pointsF;
5424 GpStatus ret;
5425 INT i;
5427 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5429 if(count <= 0)
5430 return InvalidParameter;
5432 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5433 if(!pointsF)
5434 return OutOfMemory;
5436 for(i = 0; i < count; i++){
5437 pointsF[i].X = (REAL)points[i].X;
5438 pointsF[i].Y = (REAL)points[i].Y;
5441 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5443 if(ret == Ok)
5444 for(i = 0; i < count; i++){
5445 points[i].X = roundr(pointsF[i].X);
5446 points[i].Y = roundr(pointsF[i].Y);
5448 GdipFree(pointsF);
5450 return ret;
5453 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5455 static int calls;
5457 TRACE("\n");
5459 if (!calls++)
5460 FIXME("stub\n");
5462 return NULL;
5465 /*****************************************************************************
5466 * GdipTranslateClip [GDIPLUS.@]
5468 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5470 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5472 if(!graphics)
5473 return InvalidParameter;
5475 if(graphics->busy)
5476 return ObjectBusy;
5478 return GdipTranslateRegion(graphics->clip, dx, dy);
5481 /*****************************************************************************
5482 * GdipTranslateClipI [GDIPLUS.@]
5484 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5486 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5488 if(!graphics)
5489 return InvalidParameter;
5491 if(graphics->busy)
5492 return ObjectBusy;
5494 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5498 /*****************************************************************************
5499 * GdipMeasureDriverString [GDIPLUS.@]
5501 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5502 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5503 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5505 FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5506 return NotImplemented;
5509 /*****************************************************************************
5510 * GdipDrawDriverString [GDIPLUS.@]
5512 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5513 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5514 GDIPCONST PointF *positions, INT flags,
5515 GDIPCONST GpMatrix *matrix )
5517 FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
5518 return NotImplemented;
5521 GpStatus WINGDIPAPI GdipRecordMetafile(HDC hdc, EmfType type, GDIPCONST GpRectF *frameRect,
5522 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5524 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5525 return NotImplemented;
5528 /*****************************************************************************
5529 * GdipRecordMetafileI [GDIPLUS.@]
5531 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5532 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5534 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5535 return NotImplemented;
5538 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5539 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5541 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
5542 return NotImplemented;
5545 /*****************************************************************************
5546 * GdipIsVisibleClipEmpty [GDIPLUS.@]
5548 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
5550 GpStatus stat;
5551 GpRegion* rgn;
5553 TRACE("(%p, %p)\n", graphics, res);
5555 if((stat = GdipCreateRegion(&rgn)) != Ok)
5556 return stat;
5558 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5559 goto cleanup;
5561 stat = GdipIsEmptyRegion(rgn, graphics, res);
5563 cleanup:
5564 GdipDeleteRegion(rgn);
5565 return stat;
5568 GpStatus WINGDIPAPI GdipGetHemfFromMetafile(GpMetafile *metafile, HENHMETAFILE *hEmf)
5570 FIXME("(%p,%p): stub\n", metafile, hEmf);
5572 if (!metafile || !hEmf)
5573 return InvalidParameter;
5575 *hEmf = NULL;
5577 return NotImplemented;