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
27 #include "wine/unicode.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)
66 switch(type & PathPointTypePathTypeMask){
67 case PathPointTypeBezier:
70 case PathPointTypeLine:
73 case PathPointTypeStart:
77 ERR("Bad point type\n");
81 if(type & PathPointTypeCloseSubpath)
82 ret |= PT_CLOSEFIGURE;
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)
97 INT save_state, i, numdashes;
99 DWORD dash_array[MAX_DASHLEN];
101 save_state = SaveDC(graphics->hdc);
103 EndPath(graphics->hdc);
105 if(pen->unit == UnitPixel){
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.) */
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);
137 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
139 SelectObject(graphics->hdc, gdipen);
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
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)
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;
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));
212 HBITMAP hbitmap, old_hbm=NULL;
213 BITMAPINFOHEADER bih;
217 hdc = CreateCompatibleDC(0);
219 bih.biSize = sizeof(BITMAPINFOHEADER);
220 bih.biWidth = src_width;
221 bih.biHeight = -src_height;
224 bih.biCompression = BI_RGB;
226 bih.biXPelsPerMeter = 0;
227 bih.biYPelsPerMeter = 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;
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);
249 DeleteObject(hbitmap);
255 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
261 a1 = (start >> 24) & 0xff;
262 a2 = (end >> 24) & 0xff;
264 a3 = (int)(a1*(1.0f - position)+a2*(position));
268 for (i=0xff; i<=0xff0000; i = i << 8)
269 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
273 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
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)
295 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
298 /* locate the blend positions surrounding this position */
299 while (position > brush->blendpos[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);
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])
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)
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];
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++)
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];
382 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
384 for (x=0; x<width; x++)
385 for (y=0; y<height; y++)
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;
400 if (attributes->colormatrices[type].enabled ||
401 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
405 FIXME("Color transforms not implemented\n");
408 if (attributes->gamma_enabled[type] ||
409 attributes->gamma_enabled[ColorAdjustTypeDefault])
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,
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));
437 case InterpolationModeNearestNeighbor:
441 right = roundr(srcx+srcwidth);
442 bottom = roundr(srcy+srcheight);
446 if (wrap == WrapModeClamp)
452 if (right >= bitmap->width)
453 right = bitmap->width-1;
454 if (bottom >= bitmap->height)
455 bottom = bitmap->height-1;
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)
465 right = bitmap->width-1;
468 if (top < 0 || bottom >= bitmap->height)
471 bottom = bitmap->height-1;
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;
491 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
493 x = width*2 + x % (width * 2);
495 y = height*2 + y % (height * 2);
497 if ((attributes->wrap & 1) == 1)
500 if ((x / width) % 2 == 0)
503 x = width - 1 - x % width;
508 if ((attributes->wrap & 2) == 2)
511 if ((y / height) % 2 == 0)
514 y = height - 1 - 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");
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)
535 switch (interpolation)
539 FIXME("Unimplemented interpolation %i\n", interpolation);
541 case InterpolationModeBilinear:
544 INT leftx, rightx, topy, bottomy;
545 ARGB topleft, topright, bottomleft, bottomright;
549 leftxf = floorf(point->X);
551 rightx = (INT)ceilf(point->X);
552 topyf = floorf(point->Y);
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)
585 case BrushTypeSolidColor:
586 case BrushTypeHatchFill:
588 case BrushTypeLinearGradient:
589 case BrushTypeTextureFill:
590 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
596 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
600 case BrushTypeSolidColor:
602 GpSolidFill *fill = (GpSolidFill*)brush;
606 /* partially transparent fill */
608 SelectClipPath(graphics->hdc, RGN_AND);
609 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
611 HDC hdc = CreateCompatibleDC(NULL);
617 oldbmp = SelectObject(hdc, fill->bmp);
619 bf.BlendOp = AC_SRC_OVER;
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);
632 /* else fall through */
635 SelectObject(graphics->hdc, brush->gdibrush);
636 FillPath(graphics->hdc);
641 static INT brush_can_fill_pixels(GpBrush *brush)
645 case BrushTypeSolidColor:
646 case BrushTypeHatchFill:
647 case BrushTypeLinearGradient:
648 case BrushTypeTextureFill:
655 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
656 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
660 case BrushTypeSolidColor:
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;
669 case BrushTypeHatchFill:
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++)
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;
690 argb_pixels[x + y*cdwStride] = fill->backcol;
695 case BrushTypeLinearGradient:
697 GpLineGradient *fill = (GpLineGradient*)brush;
698 GpPointF draw_points[3], line_points[3];
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? */
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
714 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
715 CoordinateSpaceDevice, draw_points, 3);
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);
729 stat = GdipInvertMatrix(world_to_gradient);
732 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
734 GdipDeleteMatrix(world_to_gradient);
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);
755 case BrushTypeTextureFill:
757 GpTexture *fill = (GpTexture*)brush;
758 GpPointF draw_points[3];
760 GpMatrix *world_to_texture;
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);
792 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
797 stat = GdipInvertMatrix(world_to_texture);
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)
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);
826 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
829 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
830 bitmap->width, bitmap->height,
831 src_stride, ColorAdjustTypeBitmap);
835 GdipFree(fill->bitmap_bits);
836 fill->bitmap_bits = NULL;
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++)
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);
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)
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;
900 PointF ptf[4], *custptf = NULL;
901 POINT pt[4], *custpt = NULL;
903 REAL theta, dsmall, dbig, dx, dy = 0.0;
908 if((x1 == x2) && (y1 == y2))
911 theta = gdiplus_atan2(y2 - y1, x2 - x1);
913 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
915 brush = CreateSolidBrush(color);
916 lb.lbStyle = BS_SOLID;
919 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
920 PS_JOIN_MITER, 1, &lb, 0,
922 oldbrush = SelectObject(graphics->hdc, brush);
923 oldpen = SelectObject(graphics->hdc, pen);
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;
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);
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;
967 dx = cos(- M_PI / 6.0 + theta) * size;
968 dy = sin(- M_PI / 6.0 + theta) * size;
976 transform_and_round_points(graphics, pt, ptf, 3);
977 Polygon(graphics->hdc, pt, 3);
980 case LineCapRoundAnchor:
981 dx = dy = ANCHOR_WIDTH * size / 2.0;
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);
992 case LineCapTriangle:
994 dx = cos(M_PI_2 + theta) * size;
995 dy = sin(M_PI_2 + theta) * size;
1002 dx = cos(theta) * size;
1003 dy = sin(theta) * size;
1008 transform_and_round_points(graphics, pt, ptf, 3);
1009 Polygon(graphics->hdc, pt, 3);
1013 dx = dy = size / 2.0;
1020 dx = -cos(M_PI_2 + theta) * size;
1021 dy = -sin(M_PI_2 + theta) * size;
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);
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))
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),
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]);
1059 BeginPath(graphics->hdc);
1060 PolyDraw(graphics->hdc, custpt, tp, count);
1061 EndPath(graphics->hdc);
1062 StrokeAndFillPath(graphics->hdc);
1065 PolyDraw(graphics->hdc, custpt, tp, count);
1071 GdipDeleteMatrix(matrix);
1078 SelectObject(graphics->hdc, oldbrush);
1079 SelectObject(graphics->hdc, oldpen);
1080 DeleteObject(brush);
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))
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;
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;
1113 if(dx == 0 && dy == 0)
1116 percent = amt / sqrt(dx * dx + dy * dy);
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)
1132 GpPointF *ptcopy = NULL;
1133 GpStatus status = GenericError;
1138 pti = GdipAlloc(count * sizeof(POINT));
1139 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1141 if(!pti || !ptcopy){
1142 status = OutOfMemory;
1146 memcpy(ptcopy, pt, count * sizeof(GpPointF));
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))
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)
1193 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1194 INT i, first = 0, second = 1, third = 2, fourth = 3;
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)
1233 GpStatus status = GenericError;
1238 pti = GdipAlloc(count * sizeof(POINT));
1239 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1241 if(!pti || !ptcopy){
1242 status = OutOfMemory;
1246 memcpy(ptcopy, pt, count * sizeof(GpPointF));
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,
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);
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));
1294 GpStatus status = GenericError;
1300 if(!pti || !tp || !ptcopy){
1301 status = OutOfMemory;
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");
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. */
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);
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,
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,
1351 ERR("Bad path last point\n");
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);
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,
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,
1389 ERR("Bad path points\n");
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);
1412 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
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);
1423 typedef struct _GraphicsContainerItem {
1425 GraphicsContainer contid;
1427 SmoothingMode smoothing;
1428 CompositingQuality compqual;
1429 InterpolationMode interpolation;
1430 CompositingMode compmode;
1431 TextRenderingHint texthint;
1434 PixelOffsetMode pixeloffset;
1436 GpMatrix* worldtrans;
1438 } GraphicsContainerItem;
1440 static GpStatus init_container(GraphicsContainerItem** container,
1441 GDIPCONST GpGraphics* graphics){
1444 *container = GdipAlloc(sizeof(GraphicsContainerItem));
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);
1462 GdipFree(*container);
1467 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1469 GdipDeleteMatrix((*container)->worldtrans);
1470 GdipFree(*container);
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){
1490 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1494 sts = GdipCloneRegion(container->clip, &newClip);
1496 GdipDeleteMatrix(newTrans);
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;
1521 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
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);
1542 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1543 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
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)
1557 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1560 if((stat = GdipCreateRegion(&tmp)) != Ok)
1563 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1566 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1569 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1572 GdipDeleteRegion(tmp);
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)
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;
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);
1608 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1609 GdipFree((*graphics)->worldtrans);
1610 GdipFree(*graphics);
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);
1634 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
1638 *graphics = GdipAlloc(sizeof(GpGraphics));
1639 if(!*graphics) return OutOfMemory;
1641 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1642 GdipFree(*graphics);
1646 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1647 GdipFree((*graphics)->worldtrans);
1648 GdipFree(*graphics);
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);
1673 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1678 TRACE("(%p, %p)\n", hwnd, graphics);
1682 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1684 ReleaseDC(hwnd, hdc);
1688 (*graphics)->hwnd = hwnd;
1689 (*graphics)->owndc = TRUE;
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)
1707 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1709 if(!hemf || !metafile)
1710 return InvalidParameter;
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;
1725 GpStatus retval = Ok;
1727 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1729 if(!hwmf || !metafile || !placeable)
1730 return InvalidParameter;
1733 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1735 return GenericError;
1736 copy = GdipAlloc(read);
1737 GetMetaFileBitsEx(hwmf, read, copy);
1739 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
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");
1750 retval = GenericError;
1754 *metafile = GdipAlloc(sizeof(GpMetafile));
1756 retval = OutOfMemory;
1760 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1761 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1763 retval = GenericError;
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;
1785 DeleteMetaFile(hwmf);
1787 TRACE("<-- %p\n", *metafile);
1791 GdipFree(*metafile);
1792 IStream_Release(stream);
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)
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;
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;
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);
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];
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;
1886 FIXME("graphics object has no HDC\n");
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);
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)
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;
1928 FIXME("graphics object has no HDC\n");
1941 save_state = prepare_dc(graphics, pen);
1943 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1945 restore_dc(graphics, save_state);
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)
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;
1968 FIXME("graphics object has no HDC\n");
1981 save_state = prepare_dc(graphics, pen);
1983 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1985 restore_dc(graphics, save_state);
1990 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1991 GDIPCONST GpPointF *points, INT count)
1996 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1998 if(!graphics || !pen || !points || (count <= 0))
1999 return InvalidParameter;
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);
2017 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2018 GDIPCONST GpPoint *points, INT count)
2024 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2026 if(!graphics || !pen || !points || (count <= 0))
2027 return InvalidParameter;
2032 pts = GdipAlloc(sizeof(GpPointF) * count);
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);
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)
2070 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2072 if(!graphics || !pen || !points || count <= 0)
2073 return InvalidParameter;
2078 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2081 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2083 GdipDeletePath(path);
2087 stat = GdipDrawPath(graphics, pen, path);
2089 GdipDeletePath(path);
2094 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2095 GDIPCONST GpPoint *points, INT count, REAL tension)
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);
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);
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)
2137 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2140 return InvalidParameter;
2142 pointsF = GdipAlloc(sizeof(GpPointF)*count);
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);
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;
2164 REAL x1, x2, y1, y2;
2167 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2169 if(!graphics || !pen)
2170 return InvalidParameter;
2176 return InvalidParameter;
2180 FIXME("graphics object has no HDC\n");
2184 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2188 tension = tension * TENSION_CONST;
2190 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2193 pt[0].X = points[0].X;
2194 pt[0].Y = points[0].Y;
2198 for(i = 0; i < count-2; i++){
2199 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2203 pt[3*i+3].X = points[i+1].X;
2204 pt[3*i+3].Y = points[i+1].Y;
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);
2222 restore_dc(graphics, save_state);
2227 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2228 GDIPCONST GpPoint *points, INT count, REAL tension)
2234 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2237 return InvalidParameter;
2239 pointsF = GdipAlloc(sizeof(GpPointF)*count);
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);
2254 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2255 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
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,
2271 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
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)
2291 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2293 if(!graphics || !pen)
2294 return InvalidParameter;
2301 FIXME("graphics object has no HDC\n");
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);
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)
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,
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,
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,
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)
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,
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)
2430 transform_and_round_points(graphics, pti, ptf, 4);
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));
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,
2457 callback(callbackData);
2458 return GenericError;
2461 else if (image->type == ImageTypeBitmap)
2463 GpBitmap* bitmap = (GpBitmap*)image;
2466 if (srcUnit == UnitInch)
2467 dx = dy = 96.0; /* FIXME: use the image resolution */
2468 else if (srcUnit == UnitPixel)
2471 return NotImplemented;
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)
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;
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);
2527 GdipDeleteMatrix(dst_to_src);
2531 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2534 GdipDeleteMatrix(dst_to_src);
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);
2547 GdipDeleteMatrix(dst_to_src);
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);
2563 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2567 if (src_data != dst_data)
2570 GdipDeleteMatrix(dst_to_src);
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;
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);
2605 GdipDeleteMatrix(dst_to_src);
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);
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;
2629 PixelFormat dst_format;
2631 /* we can't draw a bitmap of this format directly */
2632 hdc = CreateCompatibleDC(0);
2636 bih.biSize = sizeof(BITMAPINFOHEADER);
2637 bih.biWidth = bitmap->width;
2638 bih.biHeight = -bitmap->height;
2640 bih.biBitCount = 32;
2641 bih.biCompression = BI_RGB;
2642 bih.biSizeImage = 0;
2643 bih.biXPelsPerMeter = 0;
2644 bih.biYPelsPerMeter = 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;
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);
2662 hbitmap = bitmap->hbitmap;
2664 temp_hdc = (hdc == 0);
2669 if (!hdc) hdc = CreateCompatibleDC(0);
2670 old_hbm = SelectObject(hdc, hbitmap);
2673 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2677 bf.BlendOp = AC_SRC_OVER;
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);
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);
2693 SelectObject(hdc, old_hbm);
2698 DeleteObject(hbitmap);
2703 ERR("GpImage with no IPicture or HBITMAP?!\n");
2704 return NotImplemented;
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];
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,
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)
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);
2749 points[1].X = dstx + dstwidth;
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)
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);
2772 points[1].X = dstx + dstwidth;
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)
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);
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)
2817 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2819 if(!pen || !graphics)
2820 return InvalidParameter;
2827 FIXME("graphics object has no HDC\n");
2836 save_state = prepare_dc(graphics, pen);
2838 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2840 restore_dc(graphics, save_state);
2845 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2846 INT y1, INT x2, INT y2)
2852 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2854 if(!pen || !graphics)
2855 return InvalidParameter;
2862 FIXME("graphics object has no HDC\n");
2871 save_state = prepare_dc(graphics, pen);
2873 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2875 restore_dc(graphics, save_state);
2880 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2881 GpPointF *points, INT count)
2886 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2888 if(!pen || !graphics || (count < 2))
2889 return InvalidParameter;
2896 FIXME("graphics object has no HDC\n");
2900 save_state = prepare_dc(graphics, pen);
2902 retval = draw_polyline(graphics, pen, points, count, TRUE);
2904 restore_dc(graphics, save_state);
2909 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2910 GpPoint *points, INT count)
2914 GpPointF *ptf = NULL;
2917 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2919 if(!pen || !graphics || (count < 2))
2920 return InvalidParameter;
2927 FIXME("graphics object has no HDC\n");
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);
2949 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2954 TRACE("(%p, %p, %p)\n", graphics, pen, path);
2956 if(!pen || !graphics)
2957 return InvalidParameter;
2964 FIXME("graphics object has no HDC\n");
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);
2978 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2979 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
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;
2994 FIXME("graphics object has no HDC\n");
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);
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)
3024 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3026 if(!pen || !graphics)
3027 return InvalidParameter;
3034 FIXME("graphics object has no HDC\n");
3040 ptf[1].X = x + width;
3042 ptf[2].X = x + width;
3043 ptf[2].Y = y + height;
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);
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)
3073 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3075 if(!graphics || !pen || !rects || count < 1)
3076 return InvalidParameter;
3083 FIXME("graphics object has no HDC\n");
3087 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3088 pti = GdipAlloc(4 * count * sizeof(POINT));
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);
3119 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3120 GDIPCONST GpRect* rects, INT count)
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);
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);
3148 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3149 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3154 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3155 count, tension, fill);
3157 if(!graphics || !brush || !points)
3158 return InvalidParameter;
3163 if(count == 1) /* Do nothing */
3166 stat = GdipCreatePath(fill, &path);
3170 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3172 GdipDeletePath(path);
3176 stat = GdipFillPath(graphics, brush, path);
3178 GdipDeletePath(path);
3182 GdipDeletePath(path);
3187 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3188 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
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 */
3203 ptf = GdipAlloc(sizeof(GpPointF)*count);
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);
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)
3241 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3243 if(!graphics || !brush)
3244 return InvalidParameter;
3249 stat = GdipCreatePath(FillModeAlternate, &path);
3253 stat = GdipAddPathEllipse(path, x, y, width, height);
3256 stat = GdipFillPath(graphics, brush, path);
3258 GdipDeletePath(path);
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)
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
3285 BeginPath(graphics->hdc);
3286 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3287 path->pathdata.Types, path->pathdata.Count, FALSE);
3292 EndPath(graphics->hdc);
3293 brush_fill_path(graphics, brush);
3298 RestoreDC(graphics->hdc, save_state);
3303 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
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);
3317 stat = GdipFillRegion(graphics, brush, rgn);
3319 GdipDeleteRegion(rgn);
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;
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);
3352 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3353 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
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;
3367 stat = GdipCreatePath(FillModeAlternate, &path);
3371 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3374 stat = GdipFillPath(graphics, brush, path);
3376 GdipDeletePath(path);
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)
3397 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3399 if(!graphics || !brush || !points || !count)
3400 return InvalidParameter;
3405 stat = GdipCreatePath(fillMode, &path);
3409 stat = GdipAddPathPolygon(path, points, count);
3412 stat = GdipFillPath(graphics, brush, path);
3414 GdipDeletePath(path);
3420 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3421 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3426 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3428 if(!graphics || !brush || !points || !count)
3429 return InvalidParameter;
3434 stat = GdipCreatePath(fillMode, &path);
3438 stat = GdipAddPathPolygonI(path, points, count);
3441 stat = GdipFillPath(graphics, brush, path);
3443 GdipDeletePath(path);
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)
3471 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3473 if(!graphics || !brush)
3474 return InvalidParameter;
3479 stat = GdipCreatePath(FillModeAlternate, &path);
3483 stat = GdipAddPathRectangle(path, x, y, width, height);
3486 stat = GdipFillPath(graphics, brush, path);
3488 GdipDeletePath(path);
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,
3508 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
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;
3521 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
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);
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);
3550 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3558 if(!graphics->hdc || !brush_can_fill_path(brush))
3559 return NotImplemented;
3561 status = GdipGetRegionHRgn(region, graphics, &hrgn);
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);
3586 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3590 GpRegion *temp_region;
3591 GpMatrix *world_to_device, *identity;
3592 GpRectF graphics_bounds;
3593 UINT scans_count, i;
3598 if (!brush_can_fill_pixels(brush))
3599 return NotImplemented;
3601 stat = get_graphics_bounds(graphics, &graphics_bounds);
3604 stat = GdipCloneRegion(region, &temp_region);
3608 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3609 CoordinateSpaceWorld, &world_to_device);
3613 stat = GdipTransformRegion(temp_region, world_to_device);
3615 GdipDeleteMatrix(world_to_device);
3619 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3622 stat = GdipCreateMatrix(&identity);
3626 stat = GdipGetRegionScansCount(temp_region, &scans_count, identity);
3628 if (stat == Ok && scans_count != 0)
3630 scans = GdipAlloc(sizeof(*scans) * scans_count);
3636 stat = GdipGetRegionScansI(temp_region, scans, &dummy, identity);
3643 GdipDeleteMatrix(identity);
3646 GdipDeleteRegion(temp_region);
3649 if (stat == Ok && scans_count == 0)
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;
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);
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);
3694 stat = alpha_blend_pixels(graphics, min_x, min_y,
3695 (BYTE*)pixel_data, data_width, data_height,
3699 GdipFree(pixel_data);
3706 for (i=0; i<scans_count; i++)
3708 UINT size = scans[i].Width * scans[i].Height;
3710 if (size > max_size)
3714 pixel_data = GdipAlloc(sizeof(*pixel_data) * max_size);
3720 for (i=0; i<scans_count; i++)
3722 stat = brush_fill_pixels(graphics, brush, pixel_data, &scans[i],
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);
3736 GdipFree(pixel_data);
3746 /*****************************************************************************
3747 * GdipFillRegion [GDIPLUS.@]
3749 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3752 GpStatus stat = NotImplemented;
3754 TRACE("(%p, %p, %p)\n", graphics, brush, region);
3756 if (!(graphics && brush && region))
3757 return InvalidParameter;
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);
3777 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3779 TRACE("(%p,%u)\n", graphics, intention);
3782 return InvalidParameter;
3787 /* We have no internal operation queue, so there's no need to clear it. */
3795 /*****************************************************************************
3796 * GdipGetClipBounds [GDIPLUS.@]
3798 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3800 TRACE("(%p, %p)\n", graphics, rect);
3803 return InvalidParameter;
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);
3819 return InvalidParameter;
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;
3839 *mode = graphics->compmode;
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;
3856 *quality = graphics->compqual;
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;
3873 *mode = graphics->interpolation;
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;
3892 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3894 TRACE("(%p, %p)\n", graphics, scale);
3896 if(!graphics || !scale)
3897 return InvalidParameter;
3902 *scale = graphics->scale;
3907 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3909 TRACE("(%p, %p)\n", graphics, unit);
3911 if(!graphics || !unit)
3912 return InvalidParameter;
3917 *unit = graphics->unit;
3922 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3923 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3926 TRACE("(%p, %p)\n", graphics, mode);
3928 if(!graphics || !mode)
3929 return InvalidParameter;
3934 *mode = graphics->pixeloffset;
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;
3950 *mode = graphics->smoothing;
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;
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;
3979 *hint = graphics->texthint;
3984 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3989 TRACE("(%p, %p)\n", graphics, rect);
3991 if(!graphics || !rect)
3992 return InvalidParameter;
3997 /* intersect window and graphics clipping regions */
3998 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4001 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4004 /* get bounds of the region */
4005 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4008 GdipDeleteRegion(clip_rgn);
4013 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
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);
4034 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4036 TRACE("(%p, %p)\n", graphics, matrix);
4038 if(!graphics || !matrix)
4039 return InvalidParameter;
4044 *matrix = *graphics->worldtrans;
4048 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4054 TRACE("(%p, %x)\n", graphics, color);
4057 return InvalidParameter;
4062 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4065 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4066 GdipDeleteBrush((GpBrush*)brush);
4070 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4071 wnd_rect.Width, wnd_rect.Height);
4073 GdipDeleteBrush((GpBrush*)brush);
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)
4094 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4096 if(!graphics || !result)
4097 return InvalidParameter;
4104 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4105 CoordinateSpaceWorld, &pt, 1)) != Ok)
4108 if((stat = GdipCreateRegion(&rgn)) != Ok)
4111 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4114 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4117 GdipDeleteRegion(rgn);
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)
4132 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4134 if(!graphics || !result)
4135 return InvalidParameter;
4142 pts[1].X = x + width;
4143 pts[1].Y = y + height;
4145 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4146 CoordinateSpaceWorld, pts, 2)) != Ok)
4149 pts[1].X -= pts[0].X;
4150 pts[1].Y -= pts[0].Y;
4152 if((stat = GdipCreateRegion(&rgn)) != Ok)
4155 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4158 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4161 GdipDeleteRegion(rgn);
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)
4176 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4177 nheight, lineend, lineno = 0;
4179 StringAlignment halign;
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'))
4199 stringdup[j] = string[i];
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);
4216 for(lret = 0; lret < fit; lret++)
4217 if(*(stringdup + sum + lret) == '\n')
4220 /* Line break code (may look strange, but it imitates windows). */
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) == ' ')
4228 while(*(stringdup + sum + fit - 1) != ' '){
4231 if(*(stringdup + sum + fit) == '\t')
4240 while(*(stringdup + sum + lineend - 1) == ' ' ||
4241 *(stringdup + sum + lineend - 1) == '\t')
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);
4255 bounds.Height = size.cy;
4257 bounds.Y = rect->Y + height;
4261 case StringAlignmentNear:
4265 case StringAlignmentCenter:
4266 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4268 case StringAlignmentFar:
4269 bounds.X = rect->X + rect->Width - bounds.Width;
4273 stat = callback(hdc, stringdup, sum, lineend,
4274 font, rect, format, lineno, &bounds, user_data);
4279 sum += fit + (lret < fitcpy ? 1 : 0);
4283 if(height > nheight)
4286 /* Stop if this was a linewrap (but not if it was a linebreak). */
4287 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4291 GdipFree(stringdup);
4296 struct measure_ranges_args {
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)
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)
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);
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)
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;
4360 hdc = temp_hdc = CreateCompatibleDC(0);
4361 if (!temp_hdc) return OutOfMemory;
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]);
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));
4391 struct measure_string_args {
4393 INT *codepointsfitted;
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)++;
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)
4429 struct measure_string_args args;
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;
4441 temp_hdc = CreateCompatibleDC(0);
4442 if (!temp_hdc) return OutOfMemory;
4445 if(linesfilled) *linesfilled = 0;
4446 if(codepointsfitted) *codepointsfitted = 0;
4449 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4451 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4453 bounds->X = rect->X;
4454 bounds->Y = rect->Y;
4455 bounds->Width = 0.0;
4456 bounds->Height = 0.0;
4458 args.bounds = bounds;
4459 args.codepointsfitted = codepointsfitted;
4460 args.linesfilled = linesfilled;
4462 gdip_format_string(graphics->hdc ? graphics->hdc : temp_hdc, string, length, font, rect, format,
4463 measure_string_callback, &args);
4465 DeleteObject(SelectObject(graphics->hdc, oldfont));
4473 struct draw_string_args {
4476 REAL ang_cos, ang_sin;
4479 static GpStatus draw_string_callback(HDC hdc,
4480 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4481 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4482 INT lineno, const RectF *bounds, void *user_data)
4484 struct draw_string_args *args = user_data;
4487 drawcoord.left = drawcoord.right = args->drawbase.x + roundr(args->ang_sin * bounds->Y);
4488 drawcoord.top = drawcoord.bottom = args->drawbase.y + roundr(args->ang_cos * bounds->Y);
4490 DrawTextW(hdc, string + index, length, &drawcoord, args->drawflags);
4495 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4496 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4497 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4502 TEXTMETRICW textmet;
4503 GpPointF pt[3], rectcpy[4];
4505 REAL angle, rel_width, rel_height;
4506 INT offsety = 0, save_state;
4507 struct draw_string_args args;
4510 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4511 length, font, debugstr_rectf(rect), format, brush);
4513 if(!graphics || !string || !font || !brush || !rect)
4514 return InvalidParameter;
4516 if((brush->bt != BrushTypeSolidColor)){
4517 FIXME("not implemented for given parameters\n");
4518 return NotImplemented;
4523 FIXME("graphics object has no HDC\n");
4528 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4530 /* Should be no need to explicitly test for StringAlignmentNear as
4531 * that is default behavior if no alignment is passed. */
4532 if(format->vertalign != StringAlignmentNear){
4534 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
4536 if(format->vertalign == StringAlignmentCenter)
4537 offsety = (rect->Height - bounds.Height) / 2;
4538 else if(format->vertalign == StringAlignmentFar)
4539 offsety = (rect->Height - bounds.Height);
4543 save_state = SaveDC(graphics->hdc);
4544 SetBkMode(graphics->hdc, TRANSPARENT);
4545 SetTextColor(graphics->hdc, brush->lb.lbColor);
4553 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4554 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
4555 args.ang_cos = cos(angle);
4556 args.ang_sin = sin(angle);
4557 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4558 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4559 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4560 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4562 rectcpy[3].X = rectcpy[0].X = rect->X;
4563 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
4564 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4565 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
4566 transform_and_round_points(graphics, corners, rectcpy, 4);
4568 scaled_rect.X = 0.0;
4569 scaled_rect.Y = 0.0;
4570 scaled_rect.Width = rel_width * rect->Width;
4571 scaled_rect.Height = rel_height * rect->Height;
4573 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
4575 /* FIXME: If only the width or only the height is 0, we should probably still clip */
4576 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4577 SelectClipRgn(graphics->hdc, rgn);
4580 /* Use gdi to find the font, then perform transformations on it (height,
4582 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4583 GetTextMetricsW(graphics->hdc, &textmet);
4586 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
4587 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
4589 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
4591 gdifont = CreateFontIndirectW(&lfw);
4592 DeleteObject(SelectObject(graphics->hdc, gdifont));
4594 if (!format || format->align == StringAlignmentNear)
4596 args.drawbase.x = corners[0].x;
4597 args.drawbase.y = corners[0].y;
4598 args.drawflags = DT_NOCLIP | DT_EXPANDTABS;
4600 else if (format->align == StringAlignmentCenter)
4602 args.drawbase.x = (corners[0].x + corners[1].x)/2;
4603 args.drawbase.y = (corners[0].y + corners[1].y)/2;
4604 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
4606 else /* (format->align == StringAlignmentFar) */
4608 args.drawbase.x = corners[1].x;
4609 args.drawbase.y = corners[1].y;
4610 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
4613 gdip_format_string(graphics->hdc, string, length, font, &scaled_rect, format,
4614 draw_string_callback, &args);
4617 DeleteObject(gdifont);
4619 RestoreDC(graphics->hdc, save_state);
4624 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
4626 TRACE("(%p)\n", graphics);
4629 return InvalidParameter;
4634 return GdipSetInfinite(graphics->clip);
4637 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
4639 TRACE("(%p)\n", graphics);
4642 return InvalidParameter;
4647 graphics->worldtrans->matrix[0] = 1.0;
4648 graphics->worldtrans->matrix[1] = 0.0;
4649 graphics->worldtrans->matrix[2] = 0.0;
4650 graphics->worldtrans->matrix[3] = 1.0;
4651 graphics->worldtrans->matrix[4] = 0.0;
4652 graphics->worldtrans->matrix[5] = 0.0;
4657 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
4659 return GdipEndContainer(graphics, state);
4662 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
4663 GpMatrixOrder order)
4665 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
4668 return InvalidParameter;
4673 return GdipRotateMatrix(graphics->worldtrans, angle, order);
4676 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
4678 return GdipBeginContainer2(graphics, state);
4681 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
4682 GraphicsContainer *state)
4684 GraphicsContainerItem *container;
4687 TRACE("(%p, %p)\n", graphics, state);
4689 if(!graphics || !state)
4690 return InvalidParameter;
4692 sts = init_container(&container, graphics);
4696 list_add_head(&graphics->containers, &container->entry);
4697 *state = graphics->contid = container->contid;
4702 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
4704 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4705 return NotImplemented;
4708 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
4710 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4711 return NotImplemented;
4714 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
4716 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
4717 return NotImplemented;
4720 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
4723 GraphicsContainerItem *container, *container2;
4725 TRACE("(%p, %x)\n", graphics, state);
4728 return InvalidParameter;
4730 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
4731 if(container->contid == state)
4735 /* did not find a matching container */
4736 if(&container->entry == &graphics->containers)
4739 sts = restore_container(graphics, container);
4743 /* remove all of the containers on top of the found container */
4744 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
4745 if(container->contid == state)
4747 list_remove(&container->entry);
4748 delete_container(container);
4751 list_remove(&container->entry);
4752 delete_container(container);
4757 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
4758 REAL sy, GpMatrixOrder order)
4760 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
4763 return InvalidParameter;
4768 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
4771 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
4774 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
4776 if(!graphics || !srcgraphics)
4777 return InvalidParameter;
4779 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
4782 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
4783 CompositingMode mode)
4785 TRACE("(%p, %d)\n", graphics, mode);
4788 return InvalidParameter;
4793 graphics->compmode = mode;
4798 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
4799 CompositingQuality quality)
4801 TRACE("(%p, %d)\n", graphics, quality);
4804 return InvalidParameter;
4809 graphics->compqual = quality;
4814 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
4815 InterpolationMode mode)
4817 TRACE("(%p, %d)\n", graphics, mode);
4819 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
4820 return InvalidParameter;
4825 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
4826 mode = InterpolationModeBilinear;
4828 if (mode == InterpolationModeHighQuality)
4829 mode = InterpolationModeHighQualityBicubic;
4831 graphics->interpolation = mode;
4836 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
4838 TRACE("(%p, %.2f)\n", graphics, scale);
4840 if(!graphics || (scale <= 0.0))
4841 return InvalidParameter;
4846 graphics->scale = scale;
4851 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4853 TRACE("(%p, %d)\n", graphics, unit);
4856 return InvalidParameter;
4861 if(unit == UnitWorld)
4862 return InvalidParameter;
4864 graphics->unit = unit;
4869 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4872 TRACE("(%p, %d)\n", graphics, mode);
4875 return InvalidParameter;
4880 graphics->pixeloffset = mode;
4885 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
4889 TRACE("(%p,%i,%i)\n", graphics, x, y);
4892 FIXME("not implemented\n");
4894 return NotImplemented;
4897 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
4901 TRACE("(%p,%p,%p)\n", graphics, x, y);
4904 FIXME("not implemented\n");
4908 return NotImplemented;
4911 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
4913 TRACE("(%p, %d)\n", graphics, mode);
4916 return InvalidParameter;
4921 graphics->smoothing = mode;
4926 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
4928 TRACE("(%p, %d)\n", graphics, contrast);
4931 return InvalidParameter;
4933 graphics->textcontrast = contrast;
4938 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
4939 TextRenderingHint hint)
4941 TRACE("(%p, %d)\n", graphics, hint);
4944 return InvalidParameter;
4949 graphics->texthint = hint;
4954 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4956 TRACE("(%p, %p)\n", graphics, matrix);
4958 if(!graphics || !matrix)
4959 return InvalidParameter;
4964 GdipDeleteMatrix(graphics->worldtrans);
4965 return GdipCloneMatrix(matrix, &graphics->worldtrans);
4968 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
4969 REAL dy, GpMatrixOrder order)
4971 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
4974 return InvalidParameter;
4979 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
4982 /*****************************************************************************
4983 * GdipSetClipHrgn [GDIPLUS.@]
4985 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
4990 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
4993 return InvalidParameter;
4995 status = GdipCreateRegionHrgn(hrgn, ®ion);
4999 status = GdipSetClipRegion(graphics, region, mode);
5001 GdipDeleteRegion(region);
5005 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5007 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5010 return InvalidParameter;
5015 return GdipCombineRegionPath(graphics->clip, path, mode);
5018 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5019 REAL width, REAL height,
5024 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5027 return InvalidParameter;
5035 rect.Height = height;
5037 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5040 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5041 INT width, INT height,
5044 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5047 return InvalidParameter;
5052 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5055 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5058 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5060 if(!graphics || !region)
5061 return InvalidParameter;
5066 return GdipCombineRegionRegion(graphics->clip, region, mode);
5069 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5074 TRACE("(%p,%u)\n", metafile, limitDpi);
5077 FIXME("not implemented\n");
5079 return NotImplemented;
5082 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5088 TRACE("(%p, %p, %d)\n", graphics, points, count);
5090 if(!graphics || !pen || count<=0)
5091 return InvalidParameter;
5098 FIXME("graphics object has no HDC\n");
5102 pti = GdipAlloc(sizeof(POINT) * count);
5104 save_state = prepare_dc(graphics, pen);
5105 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5107 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5108 Polygon(graphics->hdc, pti, count);
5110 restore_dc(graphics, save_state);
5116 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5123 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5125 if(count<=0) return InvalidParameter;
5126 ptf = GdipAlloc(sizeof(GpPointF) * count);
5128 for(i = 0;i < count; i++){
5129 ptf[i].X = (REAL)points[i].X;
5130 ptf[i].Y = (REAL)points[i].Y;
5133 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5139 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5141 TRACE("(%p, %p)\n", graphics, dpi);
5143 if(!graphics || !dpi)
5144 return InvalidParameter;
5149 if (graphics->image)
5150 *dpi = graphics->image->xres;
5152 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
5157 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5159 TRACE("(%p, %p)\n", graphics, dpi);
5161 if(!graphics || !dpi)
5162 return InvalidParameter;
5167 if (graphics->image)
5168 *dpi = graphics->image->yres;
5170 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
5175 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5176 GpMatrixOrder order)
5181 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5183 if(!graphics || !matrix)
5184 return InvalidParameter;
5189 m = *(graphics->worldtrans);
5191 ret = GdipMultiplyMatrix(&m, matrix, order);
5193 *(graphics->worldtrans) = m;
5198 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5199 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5201 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5203 TRACE("(%p, %p)\n", graphics, hdc);
5205 if(!graphics || !hdc)
5206 return InvalidParameter;
5211 if (!graphics->hdc ||
5212 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5214 /* Create a fake HDC and fill it with a constant color. */
5219 BITMAPINFOHEADER bmih;
5222 stat = get_graphics_bounds(graphics, &bounds);
5226 graphics->temp_hbitmap_width = bounds.Width;
5227 graphics->temp_hbitmap_height = bounds.Height;
5229 bmih.biSize = sizeof(bmih);
5230 bmih.biWidth = graphics->temp_hbitmap_width;
5231 bmih.biHeight = -graphics->temp_hbitmap_height;
5233 bmih.biBitCount = 32;
5234 bmih.biCompression = BI_RGB;
5235 bmih.biSizeImage = 0;
5236 bmih.biXPelsPerMeter = 0;
5237 bmih.biYPelsPerMeter = 0;
5239 bmih.biClrImportant = 0;
5241 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5242 (void**)&graphics->temp_bits, NULL, 0);
5244 return GenericError;
5246 temp_hdc = CreateCompatibleDC(0);
5249 DeleteObject(hbitmap);
5250 return GenericError;
5253 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5254 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5256 SelectObject(temp_hdc, hbitmap);
5258 graphics->temp_hbitmap = hbitmap;
5259 *hdc = graphics->temp_hdc = temp_hdc;
5263 *hdc = graphics->hdc;
5266 graphics->busy = TRUE;
5271 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5273 TRACE("(%p, %p)\n", graphics, hdc);
5275 if(!graphics || !hdc)
5276 return InvalidParameter;
5278 if((graphics->hdc != hdc && graphics->temp_hdc != hdc) || !(graphics->busy))
5279 return InvalidParameter;
5281 if (graphics->temp_hdc == hdc)
5286 /* Find the pixels that have changed, and mark them as opaque. */
5287 pos = (DWORD*)graphics->temp_bits;
5288 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5290 if (*pos != DC_BACKGROUND_KEY)
5297 /* Write the changed pixels to the real target. */
5298 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5299 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5300 graphics->temp_hbitmap_width * 4);
5303 DeleteDC(graphics->temp_hdc);
5304 DeleteObject(graphics->temp_hbitmap);
5305 graphics->temp_hdc = NULL;
5306 graphics->temp_hbitmap = NULL;
5309 graphics->busy = FALSE;
5314 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5319 TRACE("(%p, %p)\n", graphics, region);
5321 if(!graphics || !region)
5322 return InvalidParameter;
5327 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5330 /* free everything except root node and header */
5331 delete_element(®ion->node);
5332 memcpy(region, clip, sizeof(GpRegion));
5338 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5339 GpCoordinateSpace src_space, GpMatrix **matrix)
5341 GpStatus stat = GdipCreateMatrix(matrix);
5344 if (dst_space != src_space && stat == Ok)
5346 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
5348 if(graphics->unit != UnitDisplay)
5349 unitscale *= graphics->scale;
5351 /* transform from src_space to CoordinateSpacePage */
5354 case CoordinateSpaceWorld:
5355 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5357 case CoordinateSpacePage:
5359 case CoordinateSpaceDevice:
5360 GdipScaleMatrix(*matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
5364 /* transform from CoordinateSpacePage to dst_space */
5367 case CoordinateSpaceWorld:
5369 GpMatrix *inverted_transform;
5370 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
5373 stat = GdipInvertMatrix(inverted_transform);
5375 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
5376 GdipDeleteMatrix(inverted_transform);
5380 case CoordinateSpacePage:
5382 case CoordinateSpaceDevice:
5383 GdipScaleMatrix(*matrix, unitscale, unitscale, MatrixOrderAppend);
5390 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5391 GpCoordinateSpace src_space, GpPointF *points, INT count)
5396 if(!graphics || !points || count <= 0)
5397 return InvalidParameter;
5402 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5404 if (src_space == dst_space) return Ok;
5406 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5410 stat = GdipTransformMatrixPoints(matrix, points, count);
5412 GdipDeleteMatrix(matrix);
5418 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5419 GpCoordinateSpace src_space, GpPoint *points, INT count)
5425 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5428 return InvalidParameter;
5430 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5434 for(i = 0; i < count; i++){
5435 pointsF[i].X = (REAL)points[i].X;
5436 pointsF[i].Y = (REAL)points[i].Y;
5439 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5442 for(i = 0; i < count; i++){
5443 points[i].X = roundr(pointsF[i].X);
5444 points[i].Y = roundr(pointsF[i].Y);
5451 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5463 /*****************************************************************************
5464 * GdipTranslateClip [GDIPLUS.@]
5466 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5468 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5471 return InvalidParameter;
5476 return GdipTranslateRegion(graphics->clip, dx, dy);
5479 /*****************************************************************************
5480 * GdipTranslateClipI [GDIPLUS.@]
5482 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5484 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5487 return InvalidParameter;
5492 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5496 /*****************************************************************************
5497 * GdipMeasureDriverString [GDIPLUS.@]
5499 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5500 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5501 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5503 FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5504 return NotImplemented;
5507 /*****************************************************************************
5508 * GdipDrawDriverString [GDIPLUS.@]
5510 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5511 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5512 GDIPCONST PointF *positions, INT flags,
5513 GDIPCONST GpMatrix *matrix )
5515 FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
5516 return NotImplemented;
5519 GpStatus WINGDIPAPI GdipRecordMetafile(HDC hdc, EmfType type, GDIPCONST GpRectF *frameRect,
5520 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5522 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5523 return NotImplemented;
5526 /*****************************************************************************
5527 * GdipRecordMetafileI [GDIPLUS.@]
5529 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5530 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5532 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5533 return NotImplemented;
5536 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5537 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5539 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
5540 return NotImplemented;
5543 /*****************************************************************************
5544 * GdipIsVisibleClipEmpty [GDIPLUS.@]
5546 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
5551 TRACE("(%p, %p)\n", graphics, res);
5553 if((stat = GdipCreateRegion(&rgn)) != Ok)
5556 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5559 stat = GdipIsEmptyRegion(rgn, graphics, res);
5562 GdipDeleteRegion(rgn);
5566 GpStatus WINGDIPAPI GdipGetHemfFromMetafile(GpMetafile *metafile, HENHMETAFILE *hEmf)
5568 FIXME("(%p,%p): stub\n", metafile, hEmf);
5570 if (!metafile || !hEmf)
5571 return InvalidParameter;
5575 return NotImplemented;