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 void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
533 case BrushTypeLinearGradient:
535 GpLineGradient *line = (GpLineGradient*)brush;
538 SelectClipPath(graphics->hdc, RGN_AND);
539 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
541 GpPointF endpointsf[2];
545 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
547 endpointsf[0] = line->startpoint;
548 endpointsf[1] = line->endpoint;
549 transform_and_round_points(graphics, endpointsi, endpointsf, 2);
551 if (abs(endpointsi[0].x-endpointsi[1].x) > abs(endpointsi[0].y-endpointsi[1].y))
553 /* vertical-ish gradient */
554 int startx, endx; /* x co-ordinates of endpoints shifted to intersect the top of the visible rectangle */
555 int startbottomx; /* x co-ordinate of start point shifted to intersect the bottom of the visible rectangle */
558 HBRUSH hbrush, hprevbrush;
559 int leftx, rightx; /* x co-ordinates where the leftmost and rightmost gradient lines hit the top of the visible rectangle */
561 int tilt; /* horizontal distance covered by a gradient line */
563 startx = roundr((rc.top - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
564 endx = roundr((rc.top - endpointsf[1].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[1].X);
565 width = endx - startx;
566 startbottomx = roundr((rc.bottom - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
567 tilt = startx - startbottomx;
569 if (startx >= startbottomx)
572 rightx = rc.right + tilt;
576 leftx = rc.left + tilt;
580 poly[0].y = rc.bottom;
583 poly[3].y = rc.bottom;
585 for (x=leftx; x<=rightx; x++)
587 ARGB argb = blend_line_gradient(line, (x-startx)/(REAL)width);
588 col = ARGB2COLORREF(argb);
589 hbrush = CreateSolidBrush(col);
590 hprevbrush = SelectObject(graphics->hdc, hbrush);
591 poly[0].x = x - tilt - 1;
594 poly[3].x = x - tilt;
595 Polygon(graphics->hdc, poly, 4);
596 SelectObject(graphics->hdc, hprevbrush);
597 DeleteObject(hbrush);
600 else if (endpointsi[0].y != endpointsi[1].y)
602 /* horizontal-ish gradient */
603 int starty, endy; /* y co-ordinates of endpoints shifted to intersect the left of the visible rectangle */
604 int startrighty; /* y co-ordinate of start point shifted to intersect the right of the visible rectangle */
607 HBRUSH hbrush, hprevbrush;
608 int topy, bottomy; /* y co-ordinates where the topmost and bottommost gradient lines hit the left of the visible rectangle */
610 int tilt; /* vertical distance covered by a gradient line */
612 starty = roundr((rc.left - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
613 endy = roundr((rc.left - endpointsf[1].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[1].Y);
614 height = endy - starty;
615 startrighty = roundr((rc.right - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
616 tilt = starty - startrighty;
618 if (starty >= startrighty)
621 bottomy = rc.bottom + tilt;
625 topy = rc.top + tilt;
629 poly[0].x = rc.right;
632 poly[3].x = rc.right;
634 for (y=topy; y<=bottomy; y++)
636 ARGB argb = blend_line_gradient(line, (y-starty)/(REAL)height);
637 col = ARGB2COLORREF(argb);
638 hbrush = CreateSolidBrush(col);
639 hprevbrush = SelectObject(graphics->hdc, hbrush);
640 poly[0].y = y - tilt - 1;
643 poly[3].y = y - tilt;
644 Polygon(graphics->hdc, poly, 4);
645 SelectObject(graphics->hdc, hprevbrush);
646 DeleteObject(hbrush);
649 /* else startpoint == endpoint */
653 case BrushTypeSolidColor:
655 GpSolidFill *fill = (GpSolidFill*)brush;
659 /* partially transparent fill */
661 SelectClipPath(graphics->hdc, RGN_AND);
662 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
664 HDC hdc = CreateCompatibleDC(NULL);
670 oldbmp = SelectObject(hdc, fill->bmp);
672 bf.BlendOp = AC_SRC_OVER;
674 bf.SourceConstantAlpha = 255;
675 bf.AlphaFormat = AC_SRC_ALPHA;
677 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
679 SelectObject(hdc, oldbmp);
685 /* else fall through */
688 SelectObject(graphics->hdc, brush->gdibrush);
689 FillPath(graphics->hdc);
694 static INT brush_can_fill_pixels(GpBrush *brush)
698 case BrushTypeSolidColor:
699 case BrushTypeHatchFill:
700 case BrushTypeLinearGradient:
707 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
708 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
712 case BrushTypeSolidColor:
715 GpSolidFill *fill = (GpSolidFill*)brush;
716 for (x=0; x<fill_area->Width; x++)
717 for (y=0; y<fill_area->Height; y++)
718 argb_pixels[x + y*cdwStride] = fill->color;
721 case BrushTypeHatchFill:
724 GpHatch *fill = (GpHatch*)brush;
725 const char *hatch_data;
727 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
728 return NotImplemented;
730 for (x=0; x<fill_area->Width; x++)
731 for (y=0; y<fill_area->Height; y++)
735 /* FIXME: Account for the rendering origin */
736 hx = (x + fill_area->X) % 8;
737 hy = (y + fill_area->Y) % 8;
739 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
740 argb_pixels[x + y*cdwStride] = fill->forecol;
742 argb_pixels[x + y*cdwStride] = fill->backcol;
747 case BrushTypeLinearGradient:
749 GpLineGradient *fill = (GpLineGradient*)brush;
750 GpPointF draw_points[3], line_points[3];
752 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
753 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
756 draw_points[0].X = fill_area->X;
757 draw_points[0].Y = fill_area->Y;
758 draw_points[1].X = fill_area->X+1;
759 draw_points[1].Y = fill_area->Y;
760 draw_points[2].X = fill_area->X;
761 draw_points[2].Y = fill_area->Y+1;
763 /* Transform the points to a co-ordinate space where X is the point's
764 * position in the gradient, 0.0 being the start point and 1.0 the
766 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
767 CoordinateSpaceDevice, draw_points, 3);
771 line_points[0] = fill->startpoint;
772 line_points[1] = fill->endpoint;
773 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
774 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
776 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
781 stat = GdipInvertMatrix(world_to_gradient);
784 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
786 GdipDeleteMatrix(world_to_gradient);
791 REAL x_delta = draw_points[1].X - draw_points[0].X;
792 REAL y_delta = draw_points[2].X - draw_points[0].X;
794 for (y=0; y<fill_area->Height; y++)
796 for (x=0; x<fill_area->Width; x++)
798 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
800 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
808 return NotImplemented;
812 /* GdipDrawPie/GdipFillPie helper function */
813 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
814 REAL height, REAL startAngle, REAL sweepAngle)
821 ptf[1].X = x + width;
822 ptf[1].Y = y + height;
824 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
825 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
827 transform_and_round_points(graphics, pti, ptf, 4);
829 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
830 pti[2].y, pti[3].x, pti[3].y);
833 /* Draws the linecap the specified color and size on the hdc. The linecap is in
834 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
835 * should not be called on an hdc that has a path you care about. */
836 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
837 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
839 HGDIOBJ oldbrush = NULL, oldpen = NULL;
840 GpMatrix *matrix = NULL;
843 PointF ptf[4], *custptf = NULL;
844 POINT pt[4], *custpt = NULL;
846 REAL theta, dsmall, dbig, dx, dy = 0.0;
851 if((x1 == x2) && (y1 == y2))
854 theta = gdiplus_atan2(y2 - y1, x2 - x1);
856 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
858 brush = CreateSolidBrush(color);
859 lb.lbStyle = BS_SOLID;
862 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
863 PS_JOIN_MITER, 1, &lb, 0,
865 oldbrush = SelectObject(graphics->hdc, brush);
866 oldpen = SelectObject(graphics->hdc, pen);
873 case LineCapSquareAnchor:
874 case LineCapDiamondAnchor:
875 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
876 if(cap == LineCapDiamondAnchor){
877 dsmall = cos(theta + M_PI_2) * size;
878 dbig = sin(theta + M_PI_2) * size;
881 dsmall = cos(theta + M_PI_4) * size;
882 dbig = sin(theta + M_PI_4) * size;
885 ptf[0].X = x2 - dsmall;
886 ptf[1].X = x2 + dbig;
888 ptf[0].Y = y2 - dbig;
889 ptf[3].Y = y2 + dsmall;
891 ptf[1].Y = y2 - dsmall;
892 ptf[2].Y = y2 + dbig;
894 ptf[3].X = x2 - dbig;
895 ptf[2].X = x2 + dsmall;
897 transform_and_round_points(graphics, pt, ptf, 4);
898 Polygon(graphics->hdc, pt, 4);
901 case LineCapArrowAnchor:
902 size = size * 4.0 / sqrt(3.0);
904 dx = cos(M_PI / 6.0 + theta) * size;
905 dy = sin(M_PI / 6.0 + theta) * size;
910 dx = cos(- M_PI / 6.0 + theta) * size;
911 dy = sin(- M_PI / 6.0 + theta) * size;
919 transform_and_round_points(graphics, pt, ptf, 3);
920 Polygon(graphics->hdc, pt, 3);
923 case LineCapRoundAnchor:
924 dx = dy = ANCHOR_WIDTH * size / 2.0;
931 transform_and_round_points(graphics, pt, ptf, 2);
932 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
935 case LineCapTriangle:
937 dx = cos(M_PI_2 + theta) * size;
938 dy = sin(M_PI_2 + theta) * size;
945 dx = cos(theta) * size;
946 dy = sin(theta) * size;
951 transform_and_round_points(graphics, pt, ptf, 3);
952 Polygon(graphics->hdc, pt, 3);
956 dx = dy = size / 2.0;
963 dx = -cos(M_PI_2 + theta) * size;
964 dy = -sin(M_PI_2 + theta) * size;
971 transform_and_round_points(graphics, pt, ptf, 4);
972 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
973 pt[2].y, pt[3].x, pt[3].y);
980 count = custom->pathdata.Count;
981 custptf = GdipAlloc(count * sizeof(PointF));
982 custpt = GdipAlloc(count * sizeof(POINT));
983 tp = GdipAlloc(count);
985 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
988 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
990 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
991 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
993 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
994 GdipTransformMatrixPoints(matrix, custptf, count);
996 transform_and_round_points(graphics, custpt, custptf, count);
998 for(i = 0; i < count; i++)
999 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1002 BeginPath(graphics->hdc);
1003 PolyDraw(graphics->hdc, custpt, tp, count);
1004 EndPath(graphics->hdc);
1005 StrokeAndFillPath(graphics->hdc);
1008 PolyDraw(graphics->hdc, custpt, tp, count);
1014 GdipDeleteMatrix(matrix);
1021 SelectObject(graphics->hdc, oldbrush);
1022 SelectObject(graphics->hdc, oldpen);
1023 DeleteObject(brush);
1028 /* Shortens the line by the given percent by changing x2, y2.
1029 * If percent is > 1.0 then the line will change direction.
1030 * If percent is negative it can lengthen the line. */
1031 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1033 REAL dist, theta, dx, dy;
1035 if((y1 == *y2) && (x1 == *x2))
1038 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1039 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1040 dx = cos(theta) * dist;
1041 dy = sin(theta) * dist;
1047 /* Shortens the line by the given amount by changing x2, y2.
1048 * If the amount is greater than the distance, the line will become length 0.
1049 * If the amount is negative, it can lengthen the line. */
1050 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1052 REAL dx, dy, percent;
1056 if(dx == 0 && dy == 0)
1059 percent = amt / sqrt(dx * dx + dy * dy);
1066 shorten_line_percent(x1, y1, x2, y2, percent);
1069 /* Draws lines between the given points, and if caps is true then draws an endcap
1070 * at the end of the last line. */
1071 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1072 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1075 GpPointF *ptcopy = NULL;
1076 GpStatus status = GenericError;
1081 pti = GdipAlloc(count * sizeof(POINT));
1082 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1084 if(!pti || !ptcopy){
1085 status = OutOfMemory;
1089 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1092 if(pen->endcap == LineCapArrowAnchor)
1093 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1094 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1095 else if((pen->endcap == LineCapCustom) && pen->customend)
1096 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1097 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1098 pen->customend->inset * pen->width);
1100 if(pen->startcap == LineCapArrowAnchor)
1101 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1102 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1103 else if((pen->startcap == LineCapCustom) && pen->customstart)
1104 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1105 &ptcopy[0].X, &ptcopy[0].Y,
1106 pen->customstart->inset * pen->width);
1108 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1109 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1110 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1111 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1114 transform_and_round_points(graphics, pti, ptcopy, count);
1116 if(Polyline(graphics->hdc, pti, count))
1126 /* Conducts a linear search to find the bezier points that will back off
1127 * the endpoint of the curve by a distance of amt. Linear search works
1128 * better than binary in this case because there are multiple solutions,
1129 * and binary searches often find a bad one. I don't think this is what
1130 * Windows does but short of rendering the bezier without GDI's help it's
1131 * the best we can do. If rev then work from the start of the passed points
1132 * instead of the end. */
1133 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1136 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1137 INT i, first = 0, second = 1, third = 2, fourth = 3;
1146 origx = pt[fourth].X;
1147 origy = pt[fourth].Y;
1148 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1150 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1151 /* reset bezier points to original values */
1152 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1153 /* Perform magic on bezier points. Order is important here.*/
1154 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1155 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1156 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1157 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1158 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1159 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1161 dx = pt[fourth].X - origx;
1162 dy = pt[fourth].Y - origy;
1164 diff = sqrt(dx * dx + dy * dy);
1165 percent += 0.0005 * amt;
1169 /* Draws bezier curves between given points, and if caps is true then draws an
1170 * endcap at the end of the last line. */
1171 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1172 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1176 GpStatus status = GenericError;
1181 pti = GdipAlloc(count * sizeof(POINT));
1182 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1184 if(!pti || !ptcopy){
1185 status = OutOfMemory;
1189 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1192 if(pen->endcap == LineCapArrowAnchor)
1193 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1194 else if((pen->endcap == LineCapCustom) && pen->customend)
1195 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1198 if(pen->startcap == LineCapArrowAnchor)
1199 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1200 else if((pen->startcap == LineCapCustom) && pen->customstart)
1201 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1203 /* the direction of the line cap is parallel to the direction at the
1204 * end of the bezier (which, if it has been shortened, is not the same
1205 * as the direction from pt[count-2] to pt[count-1]) */
1206 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1207 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1208 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1209 pt[count - 1].X, pt[count - 1].Y);
1211 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1212 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1213 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1216 transform_and_round_points(graphics, pti, ptcopy, count);
1218 PolyBezier(graphics->hdc, pti, count);
1229 /* Draws a combination of bezier curves and lines between points. */
1230 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1231 GDIPCONST BYTE * types, INT count, BOOL caps)
1233 POINT *pti = GdipAlloc(count * sizeof(POINT));
1234 BYTE *tp = GdipAlloc(count);
1235 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1237 GpStatus status = GenericError;
1243 if(!pti || !tp || !ptcopy){
1244 status = OutOfMemory;
1248 for(i = 1; i < count; i++){
1249 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1250 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1251 || !(types[i + 1] & PathPointTypeBezier)){
1252 ERR("Bad bezier points\n");
1259 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1261 /* If we are drawing caps, go through the points and adjust them accordingly,
1262 * and draw the caps. */
1264 switch(types[count - 1] & PathPointTypePathTypeMask){
1265 case PathPointTypeBezier:
1266 if(pen->endcap == LineCapArrowAnchor)
1267 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1268 else if((pen->endcap == LineCapCustom) && pen->customend)
1269 shorten_bezier_amt(&ptcopy[count - 4],
1270 pen->width * pen->customend->inset, FALSE);
1272 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1273 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1274 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1275 pt[count - 1].X, pt[count - 1].Y);
1278 case PathPointTypeLine:
1279 if(pen->endcap == LineCapArrowAnchor)
1280 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1281 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1283 else if((pen->endcap == LineCapCustom) && pen->customend)
1284 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1285 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1286 pen->customend->inset * pen->width);
1288 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1289 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1294 ERR("Bad path last point\n");
1298 /* Find start of points */
1299 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1300 == PathPointTypeStart); j++);
1302 switch(types[j] & PathPointTypePathTypeMask){
1303 case PathPointTypeBezier:
1304 if(pen->startcap == LineCapArrowAnchor)
1305 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1306 else if((pen->startcap == LineCapCustom) && pen->customstart)
1307 shorten_bezier_amt(&ptcopy[j - 1],
1308 pen->width * pen->customstart->inset, TRUE);
1310 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1311 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1312 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1313 pt[j - 1].X, pt[j - 1].Y);
1316 case PathPointTypeLine:
1317 if(pen->startcap == LineCapArrowAnchor)
1318 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1319 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1321 else if((pen->startcap == LineCapCustom) && pen->customstart)
1322 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1323 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1324 pen->customstart->inset * pen->width);
1326 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1327 pt[j].X, pt[j].Y, pt[j - 1].X,
1332 ERR("Bad path points\n");
1337 transform_and_round_points(graphics, pti, ptcopy, count);
1339 for(i = 0; i < count; i++){
1340 tp[i] = convert_path_point_type(types[i]);
1343 PolyDraw(graphics->hdc, pti, tp, count);
1355 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1359 BeginPath(graphics->hdc);
1360 result = draw_poly(graphics, NULL, path->pathdata.Points,
1361 path->pathdata.Types, path->pathdata.Count, FALSE);
1362 EndPath(graphics->hdc);
1366 typedef struct _GraphicsContainerItem {
1368 GraphicsContainer contid;
1370 SmoothingMode smoothing;
1371 CompositingQuality compqual;
1372 InterpolationMode interpolation;
1373 CompositingMode compmode;
1374 TextRenderingHint texthint;
1377 PixelOffsetMode pixeloffset;
1379 GpMatrix* worldtrans;
1381 } GraphicsContainerItem;
1383 static GpStatus init_container(GraphicsContainerItem** container,
1384 GDIPCONST GpGraphics* graphics){
1387 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1391 (*container)->contid = graphics->contid + 1;
1393 (*container)->smoothing = graphics->smoothing;
1394 (*container)->compqual = graphics->compqual;
1395 (*container)->interpolation = graphics->interpolation;
1396 (*container)->compmode = graphics->compmode;
1397 (*container)->texthint = graphics->texthint;
1398 (*container)->scale = graphics->scale;
1399 (*container)->unit = graphics->unit;
1400 (*container)->textcontrast = graphics->textcontrast;
1401 (*container)->pixeloffset = graphics->pixeloffset;
1403 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1405 GdipFree(*container);
1410 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1412 GdipDeleteMatrix((*container)->worldtrans);
1413 GdipFree(*container);
1421 static void delete_container(GraphicsContainerItem* container){
1422 GdipDeleteMatrix(container->worldtrans);
1423 GdipDeleteRegion(container->clip);
1424 GdipFree(container);
1427 static GpStatus restore_container(GpGraphics* graphics,
1428 GDIPCONST GraphicsContainerItem* container){
1433 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1437 sts = GdipCloneRegion(container->clip, &newClip);
1439 GdipDeleteMatrix(newTrans);
1443 GdipDeleteMatrix(graphics->worldtrans);
1444 graphics->worldtrans = newTrans;
1446 GdipDeleteRegion(graphics->clip);
1447 graphics->clip = newClip;
1449 graphics->contid = container->contid - 1;
1451 graphics->smoothing = container->smoothing;
1452 graphics->compqual = container->compqual;
1453 graphics->interpolation = container->interpolation;
1454 graphics->compmode = container->compmode;
1455 graphics->texthint = container->texthint;
1456 graphics->scale = container->scale;
1457 graphics->unit = container->unit;
1458 graphics->textcontrast = container->textcontrast;
1459 graphics->pixeloffset = container->pixeloffset;
1464 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1470 if(graphics->hwnd) {
1471 if(!GetClientRect(graphics->hwnd, &wnd_rect))
1472 return GenericError;
1474 rect->X = wnd_rect.left;
1475 rect->Y = wnd_rect.top;
1476 rect->Width = wnd_rect.right - wnd_rect.left;
1477 rect->Height = wnd_rect.bottom - wnd_rect.top;
1478 }else if (graphics->image){
1479 stat = GdipGetImageBounds(graphics->image, rect, &unit);
1480 if (stat == Ok && unit != UnitPixel)
1481 FIXME("need to convert from unit %i\n", unit);
1485 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1486 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1492 /* on success, rgn will contain the region of the graphics object which
1493 * is visible after clipping has been applied */
1494 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1500 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1503 if((stat = GdipCreateRegion(&tmp)) != Ok)
1506 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1509 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1512 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1515 GdipDeleteRegion(tmp);
1519 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1521 TRACE("(%p, %p)\n", hdc, graphics);
1523 return GdipCreateFromHDC2(hdc, NULL, graphics);
1526 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1530 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1532 if(hDevice != NULL) {
1533 FIXME("Don't know how to handle parameter hDevice\n");
1534 return NotImplemented;
1540 if(graphics == NULL)
1541 return InvalidParameter;
1543 *graphics = GdipAlloc(sizeof(GpGraphics));
1544 if(!*graphics) return OutOfMemory;
1546 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1547 GdipFree(*graphics);
1551 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1552 GdipFree((*graphics)->worldtrans);
1553 GdipFree(*graphics);
1557 (*graphics)->hdc = hdc;
1558 (*graphics)->hwnd = WindowFromDC(hdc);
1559 (*graphics)->owndc = FALSE;
1560 (*graphics)->smoothing = SmoothingModeDefault;
1561 (*graphics)->compqual = CompositingQualityDefault;
1562 (*graphics)->interpolation = InterpolationModeBilinear;
1563 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1564 (*graphics)->compmode = CompositingModeSourceOver;
1565 (*graphics)->unit = UnitDisplay;
1566 (*graphics)->scale = 1.0;
1567 (*graphics)->busy = FALSE;
1568 (*graphics)->textcontrast = 4;
1569 list_init(&(*graphics)->containers);
1570 (*graphics)->contid = 0;
1572 TRACE("<-- %p\n", *graphics);
1577 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
1581 *graphics = GdipAlloc(sizeof(GpGraphics));
1582 if(!*graphics) return OutOfMemory;
1584 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1585 GdipFree(*graphics);
1589 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1590 GdipFree((*graphics)->worldtrans);
1591 GdipFree(*graphics);
1595 (*graphics)->hdc = NULL;
1596 (*graphics)->hwnd = NULL;
1597 (*graphics)->owndc = FALSE;
1598 (*graphics)->image = image;
1599 (*graphics)->smoothing = SmoothingModeDefault;
1600 (*graphics)->compqual = CompositingQualityDefault;
1601 (*graphics)->interpolation = InterpolationModeBilinear;
1602 (*graphics)->pixeloffset = PixelOffsetModeDefault;
1603 (*graphics)->compmode = CompositingModeSourceOver;
1604 (*graphics)->unit = UnitDisplay;
1605 (*graphics)->scale = 1.0;
1606 (*graphics)->busy = FALSE;
1607 (*graphics)->textcontrast = 4;
1608 list_init(&(*graphics)->containers);
1609 (*graphics)->contid = 0;
1611 TRACE("<-- %p\n", *graphics);
1616 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1621 TRACE("(%p, %p)\n", hwnd, graphics);
1625 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1627 ReleaseDC(hwnd, hdc);
1631 (*graphics)->hwnd = hwnd;
1632 (*graphics)->owndc = TRUE;
1637 /* FIXME: no icm handling */
1638 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1640 TRACE("(%p, %p)\n", hwnd, graphics);
1642 return GdipCreateFromHWND(hwnd, graphics);
1645 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1646 GpMetafile **metafile)
1650 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1652 if(!hemf || !metafile)
1653 return InvalidParameter;
1656 FIXME("not implemented\n");
1658 return NotImplemented;
1661 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1662 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1664 IStream *stream = NULL;
1668 GpStatus retval = Ok;
1670 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1672 if(!hwmf || !metafile || !placeable)
1673 return InvalidParameter;
1676 read = GetMetaFileBitsEx(hwmf, 0, NULL);
1678 return GenericError;
1679 copy = GdipAlloc(read);
1680 GetMetaFileBitsEx(hwmf, read, copy);
1682 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1685 read = GetEnhMetaFileBits(hemf, 0, NULL);
1686 copy = GdipAlloc(read);
1687 GetEnhMetaFileBits(hemf, read, copy);
1688 DeleteEnhMetaFile(hemf);
1690 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1691 ERR("could not make stream\n");
1693 retval = GenericError;
1697 *metafile = GdipAlloc(sizeof(GpMetafile));
1699 retval = OutOfMemory;
1703 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1704 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1706 retval = GenericError;
1711 (*metafile)->image.type = ImageTypeMetafile;
1712 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1713 (*metafile)->image.palette_flags = 0;
1714 (*metafile)->image.palette_count = 0;
1715 (*metafile)->image.palette_size = 0;
1716 (*metafile)->image.palette_entries = NULL;
1717 (*metafile)->image.xres = (REAL)placeable->Inch;
1718 (*metafile)->image.yres = (REAL)placeable->Inch;
1719 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1720 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1721 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1722 - placeable->BoundingBox.Left));
1723 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1724 - placeable->BoundingBox.Top));
1725 (*metafile)->unit = UnitPixel;
1728 DeleteMetaFile(hwmf);
1730 TRACE("<-- %p\n", *metafile);
1734 GdipFree(*metafile);
1735 IStream_Release(stream);
1739 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1740 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1742 HMETAFILE hmf = GetMetaFileW(file);
1744 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1746 if(!hmf) return InvalidParameter;
1748 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1751 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1752 GpMetafile **metafile)
1754 FIXME("(%p, %p): stub\n", file, metafile);
1755 return NotImplemented;
1758 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1759 GpMetafile **metafile)
1761 FIXME("(%p, %p): stub\n", stream, metafile);
1762 return NotImplemented;
1765 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1766 UINT access, IStream **stream)
1771 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1773 if(!stream || !filename)
1774 return InvalidParameter;
1776 if(access & GENERIC_WRITE)
1777 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1778 else if(access & GENERIC_READ)
1779 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1781 return InvalidParameter;
1783 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1785 return hresult_to_status(ret);
1788 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1790 GraphicsContainerItem *cont, *next;
1791 TRACE("(%p)\n", graphics);
1793 if(!graphics) return InvalidParameter;
1794 if(graphics->busy) return ObjectBusy;
1797 ReleaseDC(graphics->hwnd, graphics->hdc);
1799 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1800 list_remove(&cont->entry);
1801 delete_container(cont);
1804 GdipDeleteRegion(graphics->clip);
1805 GdipDeleteMatrix(graphics->worldtrans);
1811 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1812 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1814 INT save_state, num_pts;
1815 GpPointF points[MAX_ARC_PTS];
1818 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1819 width, height, startAngle, sweepAngle);
1821 if(!graphics || !pen || width <= 0 || height <= 0)
1822 return InvalidParameter;
1829 FIXME("graphics object has no HDC\n");
1833 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1835 save_state = prepare_dc(graphics, pen);
1837 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1839 restore_dc(graphics, save_state);
1844 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1845 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1847 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1848 width, height, startAngle, sweepAngle);
1850 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1853 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1854 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1860 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1861 x2, y2, x3, y3, x4, y4);
1863 if(!graphics || !pen)
1864 return InvalidParameter;
1871 FIXME("graphics object has no HDC\n");
1884 save_state = prepare_dc(graphics, pen);
1886 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1888 restore_dc(graphics, save_state);
1893 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1894 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1900 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1901 x2, y2, x3, y3, x4, y4);
1903 if(!graphics || !pen)
1904 return InvalidParameter;
1911 FIXME("graphics object has no HDC\n");
1924 save_state = prepare_dc(graphics, pen);
1926 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1928 restore_dc(graphics, save_state);
1933 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1934 GDIPCONST GpPointF *points, INT count)
1939 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1941 if(!graphics || !pen || !points || (count <= 0))
1942 return InvalidParameter;
1947 for(i = 0; i < floor(count / 4); i++){
1948 ret = GdipDrawBezier(graphics, pen,
1949 points[4*i].X, points[4*i].Y,
1950 points[4*i + 1].X, points[4*i + 1].Y,
1951 points[4*i + 2].X, points[4*i + 2].Y,
1952 points[4*i + 3].X, points[4*i + 3].Y);
1960 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1961 GDIPCONST GpPoint *points, INT count)
1967 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1969 if(!graphics || !pen || !points || (count <= 0))
1970 return InvalidParameter;
1975 pts = GdipAlloc(sizeof(GpPointF) * count);
1979 for(i = 0; i < count; i++){
1980 pts[i].X = (REAL)points[i].X;
1981 pts[i].Y = (REAL)points[i].Y;
1984 ret = GdipDrawBeziers(graphics,pen,pts,count);
1991 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1992 GDIPCONST GpPointF *points, INT count)
1994 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1996 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1999 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2000 GDIPCONST GpPoint *points, INT count)
2002 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2004 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2007 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2008 GDIPCONST GpPointF *points, INT count, REAL tension)
2013 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2015 if(!graphics || !pen || !points || count <= 0)
2016 return InvalidParameter;
2021 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2024 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2026 GdipDeletePath(path);
2030 stat = GdipDrawPath(graphics, pen, path);
2032 GdipDeletePath(path);
2037 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2038 GDIPCONST GpPoint *points, INT count, REAL tension)
2044 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2046 if(!points || count <= 0)
2047 return InvalidParameter;
2049 ptf = GdipAlloc(sizeof(GpPointF)*count);
2053 for(i = 0; i < count; i++){
2054 ptf[i].X = (REAL)points[i].X;
2055 ptf[i].Y = (REAL)points[i].Y;
2058 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2065 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2066 GDIPCONST GpPointF *points, INT count)
2068 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2070 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2073 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2074 GDIPCONST GpPoint *points, INT count)
2080 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2083 return InvalidParameter;
2085 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2089 for(i = 0; i < count; i++){
2090 pointsF[i].X = (REAL)points[i].X;
2091 pointsF[i].Y = (REAL)points[i].Y;
2094 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2100 /* Approximates cardinal spline with Bezier curves. */
2101 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2102 GDIPCONST GpPointF *points, INT count, REAL tension)
2104 /* PolyBezier expects count*3-2 points. */
2105 INT i, len_pt = count*3-2, save_state;
2107 REAL x1, x2, y1, y2;
2110 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2112 if(!graphics || !pen)
2113 return InvalidParameter;
2119 return InvalidParameter;
2123 FIXME("graphics object has no HDC\n");
2127 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2131 tension = tension * TENSION_CONST;
2133 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2136 pt[0].X = points[0].X;
2137 pt[0].Y = points[0].Y;
2141 for(i = 0; i < count-2; i++){
2142 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2146 pt[3*i+3].X = points[i+1].X;
2147 pt[3*i+3].Y = points[i+1].Y;
2152 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2153 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2155 pt[len_pt-2].X = x1;
2156 pt[len_pt-2].Y = y1;
2157 pt[len_pt-1].X = points[count-1].X;
2158 pt[len_pt-1].Y = points[count-1].Y;
2160 save_state = prepare_dc(graphics, pen);
2162 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2165 restore_dc(graphics, save_state);
2170 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2171 GDIPCONST GpPoint *points, INT count, REAL tension)
2177 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2180 return InvalidParameter;
2182 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2186 for(i = 0; i < count; i++){
2187 pointsF[i].X = (REAL)points[i].X;
2188 pointsF[i].Y = (REAL)points[i].Y;
2191 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2197 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2198 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2201 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2203 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2204 return InvalidParameter;
2207 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2210 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2211 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2214 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2220 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2221 return InvalidParameter;
2224 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2227 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2228 REAL y, REAL width, REAL height)
2234 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2236 if(!graphics || !pen)
2237 return InvalidParameter;
2244 FIXME("graphics object has no HDC\n");
2250 ptf[1].X = x + width;
2251 ptf[1].Y = y + height;
2253 save_state = prepare_dc(graphics, pen);
2254 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2256 transform_and_round_points(graphics, pti, ptf, 2);
2258 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2260 restore_dc(graphics, save_state);
2265 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2266 INT y, INT width, INT height)
2268 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2270 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2274 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2279 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2281 if(!graphics || !image)
2282 return InvalidParameter;
2284 GdipGetImageWidth(image, &width);
2285 GdipGetImageHeight(image, &height);
2287 /* FIXME: we should use the graphics and image dpi, somehow */
2289 points[0].X = points[2].X = x;
2290 points[0].Y = points[1].Y = y;
2291 points[1].X = x + width;
2292 points[2].Y = y + height;
2294 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
2295 UnitPixel, NULL, NULL, NULL);
2298 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2301 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2303 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2306 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2307 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2311 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2313 points[0].X = points[2].X = x;
2314 points[0].Y = points[1].Y = y;
2316 /* FIXME: convert image coordinates to Graphics coordinates? */
2317 points[1].X = x + srcwidth;
2318 points[2].Y = y + srcheight;
2320 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2321 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2324 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2325 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2328 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2331 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2332 GDIPCONST GpPointF *dstpoints, INT count)
2334 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2335 return NotImplemented;
2338 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2339 GDIPCONST GpPoint *dstpoints, INT count)
2341 FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2342 return NotImplemented;
2345 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2346 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2347 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2348 DrawImageAbort callback, VOID * callbackData)
2355 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2356 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2360 return NotImplemented;
2362 if(!graphics || !image || !points || count != 3)
2363 return InvalidParameter;
2365 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2366 debugstr_pointf(&points[2]));
2368 memcpy(ptf, points, 3 * sizeof(GpPointF));
2369 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2370 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2371 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
2373 transform_and_round_points(graphics, pti, ptf, 4);
2379 FIXME("graphics object has no HDC\n");
2382 /* FIXME: partially implemented (only works for rectangular parallelograms) */
2383 if(srcUnit == UnitInch)
2384 dx = dy = (REAL) INCH_HIMETRIC;
2385 else if(srcUnit == UnitPixel){
2386 dx = ((REAL) INCH_HIMETRIC) /
2387 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
2388 dy = ((REAL) INCH_HIMETRIC) /
2389 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
2392 return NotImplemented;
2394 if(IPicture_Render(image->picture, graphics->hdc,
2395 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2396 srcx * dx, srcy * dy,
2397 srcwidth * dx, srcheight * dy,
2400 callback(callbackData);
2401 return GenericError;
2404 else if (image->type == ImageTypeBitmap)
2406 GpBitmap* bitmap = (GpBitmap*)image;
2409 if (srcUnit == UnitInch)
2410 dx = dy = 96.0; /* FIXME: use the image resolution */
2411 else if (srcUnit == UnitPixel)
2414 return NotImplemented;
2416 if (imageAttributes ||
2417 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2418 !((GpBitmap*)image)->hbitmap ||
2419 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X)
2426 int i, x, y, src_stride, dst_stride;
2427 GpMatrix *dst_to_src;
2428 REAL m11, m12, m21, m22, mdx, mdy;
2429 LPBYTE src_data, dst_data;
2430 BitmapData lockeddata;
2431 InterpolationMode interpolation = graphics->interpolation;
2432 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2433 REAL x_dx, x_dy, y_dx, y_dy;
2434 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2436 if (!imageAttributes)
2437 imageAttributes = &defaultImageAttributes;
2441 srcwidth = srcwidth * dx;
2442 srcheight = srcheight * dy;
2444 dst_area.left = dst_area.right = pti[0].x;
2445 dst_area.top = dst_area.bottom = pti[0].y;
2448 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2449 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2450 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2451 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2454 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2455 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2456 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2457 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2458 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2459 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2461 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
2462 if (stat != Ok) return stat;
2464 stat = GdipInvertMatrix(dst_to_src);
2467 GdipDeleteMatrix(dst_to_src);
2471 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2474 GdipDeleteMatrix(dst_to_src);
2478 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2480 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2481 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2483 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2487 GdipDeleteMatrix(dst_to_src);
2490 src_stride = sizeof(ARGB) * src_area.Width;
2492 /* Read the bits we need from the source bitmap into an ARGB buffer. */
2493 lockeddata.Width = src_area.Width;
2494 lockeddata.Height = src_area.Height;
2495 lockeddata.Stride = src_stride;
2496 lockeddata.PixelFormat = PixelFormat32bppARGB;
2497 lockeddata.Scan0 = src_data;
2499 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
2500 PixelFormat32bppARGB, &lockeddata);
2503 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2507 if (src_data != dst_data)
2510 GdipDeleteMatrix(dst_to_src);
2514 apply_image_attributes(imageAttributes, src_data,
2515 src_area.Width, src_area.Height,
2516 src_stride, ColorAdjustTypeBitmap);
2518 /* Transform the bits as needed to the destination. */
2519 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
2521 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
2522 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
2523 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
2524 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
2526 for (x=dst_area.left; x<dst_area.right; x++)
2528 for (y=dst_area.top; y<dst_area.bottom; y++)
2530 GpPointF src_pointf;
2534 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
2535 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
2537 src_x = roundr(src_pointf.X);
2538 src_y = roundr(src_pointf.Y);
2540 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2542 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
2543 *dst_color = sample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, src_x, src_y, imageAttributes);
2549 GdipDeleteMatrix(dst_to_src);
2553 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
2554 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
2563 int temp_hdc=0, temp_bitmap=0;
2564 HBITMAP hbitmap, old_hbm=NULL;
2566 if (!(bitmap->format == PixelFormat16bppRGB555 ||
2567 bitmap->format == PixelFormat24bppRGB ||
2568 bitmap->format == PixelFormat32bppRGB ||
2569 bitmap->format == PixelFormat32bppPARGB))
2571 BITMAPINFOHEADER bih;
2573 PixelFormat dst_format;
2575 /* we can't draw a bitmap of this format directly */
2576 hdc = CreateCompatibleDC(0);
2580 bih.biSize = sizeof(BITMAPINFOHEADER);
2581 bih.biWidth = bitmap->width;
2582 bih.biHeight = -bitmap->height;
2584 bih.biBitCount = 32;
2585 bih.biCompression = BI_RGB;
2586 bih.biSizeImage = 0;
2587 bih.biXPelsPerMeter = 0;
2588 bih.biYPelsPerMeter = 0;
2590 bih.biClrImportant = 0;
2592 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
2593 (void**)&temp_bits, NULL, 0);
2595 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2596 dst_format = PixelFormat32bppPARGB;
2598 dst_format = PixelFormat32bppRGB;
2600 convert_pixels(bitmap->width, bitmap->height,
2601 bitmap->width*4, temp_bits, dst_format,
2602 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
2606 hbitmap = bitmap->hbitmap;
2608 temp_hdc = (hdc == 0);
2613 if (!hdc) hdc = CreateCompatibleDC(0);
2614 old_hbm = SelectObject(hdc, hbitmap);
2617 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2621 bf.BlendOp = AC_SRC_OVER;
2623 bf.SourceConstantAlpha = 255;
2624 bf.AlphaFormat = AC_SRC_ALPHA;
2626 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2627 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, bf);
2631 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2632 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, SRCCOPY);
2637 SelectObject(hdc, old_hbm);
2642 DeleteObject(hbitmap);
2647 ERR("GpImage with no IPicture or HBITMAP?!\n");
2648 return NotImplemented;
2654 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
2655 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
2656 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2657 DrawImageAbort callback, VOID * callbackData)
2659 GpPointF pointsF[3];
2662 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
2663 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2666 if(!points || count!=3)
2667 return InvalidParameter;
2669 for(i = 0; i < count; i++){
2670 pointsF[i].X = (REAL)points[i].X;
2671 pointsF[i].Y = (REAL)points[i].Y;
2674 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2675 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2676 callback, callbackData);
2679 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2680 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2681 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2682 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2683 VOID * callbackData)
2687 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2688 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2689 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2693 points[1].X = dstx + dstwidth;
2696 points[2].Y = dsty + dstheight;
2698 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2699 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2702 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2703 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2704 INT srcwidth, INT srcheight, GpUnit srcUnit,
2705 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2706 VOID * callbackData)
2710 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2711 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2712 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2716 points[1].X = dstx + dstwidth;
2719 points[2].Y = dsty + dstheight;
2721 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2722 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2725 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2726 REAL x, REAL y, REAL width, REAL height)
2732 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2734 if(!graphics || !image)
2735 return InvalidParameter;
2737 ret = GdipGetImageBounds(image, &bounds, &unit);
2741 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2742 bounds.X, bounds.Y, bounds.Width, bounds.Height,
2743 unit, NULL, NULL, NULL);
2746 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2747 INT x, INT y, INT width, INT height)
2749 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2751 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2754 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2755 REAL y1, REAL x2, REAL y2)
2761 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2763 if(!pen || !graphics)
2764 return InvalidParameter;
2771 FIXME("graphics object has no HDC\n");
2780 save_state = prepare_dc(graphics, pen);
2782 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2784 restore_dc(graphics, save_state);
2789 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2790 INT y1, INT x2, INT y2)
2796 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2798 if(!pen || !graphics)
2799 return InvalidParameter;
2806 FIXME("graphics object has no HDC\n");
2815 save_state = prepare_dc(graphics, pen);
2817 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2819 restore_dc(graphics, save_state);
2824 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2825 GpPointF *points, INT count)
2830 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2832 if(!pen || !graphics || (count < 2))
2833 return InvalidParameter;
2840 FIXME("graphics object has no HDC\n");
2844 save_state = prepare_dc(graphics, pen);
2846 retval = draw_polyline(graphics, pen, points, count, TRUE);
2848 restore_dc(graphics, save_state);
2853 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2854 GpPoint *points, INT count)
2858 GpPointF *ptf = NULL;
2861 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2863 if(!pen || !graphics || (count < 2))
2864 return InvalidParameter;
2871 FIXME("graphics object has no HDC\n");
2875 ptf = GdipAlloc(count * sizeof(GpPointF));
2876 if(!ptf) return OutOfMemory;
2878 for(i = 0; i < count; i ++){
2879 ptf[i].X = (REAL) points[i].X;
2880 ptf[i].Y = (REAL) points[i].Y;
2883 save_state = prepare_dc(graphics, pen);
2885 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2887 restore_dc(graphics, save_state);
2893 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2898 TRACE("(%p, %p, %p)\n", graphics, pen, path);
2900 if(!pen || !graphics)
2901 return InvalidParameter;
2908 FIXME("graphics object has no HDC\n");
2912 save_state = prepare_dc(graphics, pen);
2914 retval = draw_poly(graphics, pen, path->pathdata.Points,
2915 path->pathdata.Types, path->pathdata.Count, TRUE);
2917 restore_dc(graphics, save_state);
2922 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2923 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2927 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2928 width, height, startAngle, sweepAngle);
2930 if(!graphics || !pen)
2931 return InvalidParameter;
2938 FIXME("graphics object has no HDC\n");
2942 save_state = prepare_dc(graphics, pen);
2943 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2945 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2947 restore_dc(graphics, save_state);
2952 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2953 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2955 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2956 width, height, startAngle, sweepAngle);
2958 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2961 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2962 REAL y, REAL width, REAL height)
2968 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2970 if(!pen || !graphics)
2971 return InvalidParameter;
2978 FIXME("graphics object has no HDC\n");
2984 ptf[1].X = x + width;
2986 ptf[2].X = x + width;
2987 ptf[2].Y = y + height;
2989 ptf[3].Y = y + height;
2991 save_state = prepare_dc(graphics, pen);
2992 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2994 transform_and_round_points(graphics, pti, ptf, 4);
2995 Polygon(graphics->hdc, pti, 4);
2997 restore_dc(graphics, save_state);
3002 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3003 INT y, INT width, INT height)
3005 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3007 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3010 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3011 GDIPCONST GpRectF* rects, INT count)
3017 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3019 if(!graphics || !pen || !rects || count < 1)
3020 return InvalidParameter;
3027 FIXME("graphics object has no HDC\n");
3031 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3032 pti = GdipAlloc(4 * count * sizeof(POINT));
3040 for(i = 0; i < count; i++){
3041 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3042 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3043 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3044 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3047 save_state = prepare_dc(graphics, pen);
3048 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3050 transform_and_round_points(graphics, pti, ptf, 4 * count);
3052 for(i = 0; i < count; i++)
3053 Polygon(graphics->hdc, &pti[4 * i], 4);
3055 restore_dc(graphics, save_state);
3063 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3064 GDIPCONST GpRect* rects, INT count)
3070 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3072 if(!rects || count<=0)
3073 return InvalidParameter;
3075 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3079 for(i = 0;i < count;i++){
3080 rectsF[i].X = (REAL)rects[i].X;
3081 rectsF[i].Y = (REAL)rects[i].Y;
3082 rectsF[i].Width = (REAL)rects[i].Width;
3083 rectsF[i].Height = (REAL)rects[i].Height;
3086 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3092 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3093 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3098 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3099 count, tension, fill);
3101 if(!graphics || !brush || !points)
3102 return InvalidParameter;
3107 if(count == 1) /* Do nothing */
3110 stat = GdipCreatePath(fill, &path);
3114 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3116 GdipDeletePath(path);
3120 stat = GdipFillPath(graphics, brush, path);
3122 GdipDeletePath(path);
3126 GdipDeletePath(path);
3131 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3132 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3138 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3139 count, tension, fill);
3141 if(!points || count == 0)
3142 return InvalidParameter;
3144 if(count == 1) /* Do nothing */
3147 ptf = GdipAlloc(sizeof(GpPointF)*count);
3151 for(i = 0;i < count;i++){
3152 ptf[i].X = (REAL)points[i].X;
3153 ptf[i].Y = (REAL)points[i].Y;
3156 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3163 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3164 GDIPCONST GpPointF *points, INT count)
3166 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3167 return GdipFillClosedCurve2(graphics, brush, points, count,
3168 0.5f, FillModeAlternate);
3171 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3172 GDIPCONST GpPoint *points, INT count)
3174 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3175 return GdipFillClosedCurve2I(graphics, brush, points, count,
3176 0.5f, FillModeAlternate);
3179 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3180 REAL y, REAL width, REAL height)
3186 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3188 if(!graphics || !brush)
3189 return InvalidParameter;
3196 FIXME("graphics object has no HDC\n");
3202 ptf[1].X = x + width;
3203 ptf[1].Y = y + height;
3205 save_state = SaveDC(graphics->hdc);
3206 EndPath(graphics->hdc);
3208 transform_and_round_points(graphics, pti, ptf, 2);
3210 BeginPath(graphics->hdc);
3211 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
3212 EndPath(graphics->hdc);
3214 brush_fill_path(graphics, brush);
3216 RestoreDC(graphics->hdc, save_state);
3221 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3222 INT y, INT width, INT height)
3224 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3226 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3229 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3234 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3236 if(!brush || !graphics || !path)
3237 return InvalidParameter;
3244 FIXME("graphics object has no HDC\n");
3248 save_state = SaveDC(graphics->hdc);
3249 EndPath(graphics->hdc);
3250 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3253 BeginPath(graphics->hdc);
3254 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3255 path->pathdata.Types, path->pathdata.Count, FALSE);
3260 EndPath(graphics->hdc);
3261 brush_fill_path(graphics, brush);
3266 RestoreDC(graphics->hdc, save_state);
3271 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3272 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3276 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3277 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3279 if(!graphics || !brush)
3280 return InvalidParameter;
3287 FIXME("graphics object has no HDC\n");
3291 save_state = SaveDC(graphics->hdc);
3292 EndPath(graphics->hdc);
3294 BeginPath(graphics->hdc);
3295 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3296 EndPath(graphics->hdc);
3298 brush_fill_path(graphics, brush);
3300 RestoreDC(graphics->hdc, save_state);
3305 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3306 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3308 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3309 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3311 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3314 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3315 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3318 GpPointF *ptf = NULL;
3320 GpStatus retval = Ok;
3322 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3324 if(!graphics || !brush || !points || !count)
3325 return InvalidParameter;
3332 FIXME("graphics object has no HDC\n");
3336 ptf = GdipAlloc(count * sizeof(GpPointF));
3337 pti = GdipAlloc(count * sizeof(POINT));
3339 retval = OutOfMemory;
3343 memcpy(ptf, points, count * sizeof(GpPointF));
3345 save_state = SaveDC(graphics->hdc);
3346 EndPath(graphics->hdc);
3347 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
3350 transform_and_round_points(graphics, pti, ptf, count);
3352 BeginPath(graphics->hdc);
3353 Polygon(graphics->hdc, pti, count);
3354 EndPath(graphics->hdc);
3356 brush_fill_path(graphics, brush);
3358 RestoreDC(graphics->hdc, save_state);
3367 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3368 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3371 GpPointF *ptf = NULL;
3373 GpStatus retval = Ok;
3375 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3377 if(!graphics || !brush || !points || !count)
3378 return InvalidParameter;
3385 FIXME("graphics object has no HDC\n");
3389 ptf = GdipAlloc(count * sizeof(GpPointF));
3390 pti = GdipAlloc(count * sizeof(POINT));
3392 retval = OutOfMemory;
3396 for(i = 0; i < count; i ++){
3397 ptf[i].X = (REAL) points[i].X;
3398 ptf[i].Y = (REAL) points[i].Y;
3401 save_state = SaveDC(graphics->hdc);
3402 EndPath(graphics->hdc);
3403 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
3406 transform_and_round_points(graphics, pti, ptf, count);
3408 BeginPath(graphics->hdc);
3409 Polygon(graphics->hdc, pti, count);
3410 EndPath(graphics->hdc);
3412 brush_fill_path(graphics, brush);
3414 RestoreDC(graphics->hdc, save_state);
3423 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3424 GDIPCONST GpPointF *points, INT count)
3426 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3428 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3431 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3432 GDIPCONST GpPoint *points, INT count)
3434 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3436 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3439 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3440 REAL x, REAL y, REAL width, REAL height)
3446 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3448 if(!graphics || !brush)
3449 return InvalidParameter;
3456 FIXME("graphics object has no HDC\n");
3462 ptf[1].X = x + width;
3464 ptf[2].X = x + width;
3465 ptf[2].Y = y + height;
3467 ptf[3].Y = y + height;
3469 save_state = SaveDC(graphics->hdc);
3470 EndPath(graphics->hdc);
3472 transform_and_round_points(graphics, pti, ptf, 4);
3474 BeginPath(graphics->hdc);
3475 Polygon(graphics->hdc, pti, 4);
3476 EndPath(graphics->hdc);
3478 brush_fill_path(graphics, brush);
3480 RestoreDC(graphics->hdc, save_state);
3485 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3486 INT x, INT y, INT width, INT height)
3492 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3494 if(!graphics || !brush)
3495 return InvalidParameter;
3502 FIXME("graphics object has no HDC\n");
3508 ptf[1].X = x + width;
3510 ptf[2].X = x + width;
3511 ptf[2].Y = y + height;
3513 ptf[3].Y = y + height;
3515 save_state = SaveDC(graphics->hdc);
3516 EndPath(graphics->hdc);
3518 transform_and_round_points(graphics, pti, ptf, 4);
3520 BeginPath(graphics->hdc);
3521 Polygon(graphics->hdc, pti, 4);
3522 EndPath(graphics->hdc);
3524 brush_fill_path(graphics, brush);
3526 RestoreDC(graphics->hdc, save_state);
3531 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3537 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3540 return InvalidParameter;
3542 for(i = 0; i < count; i++){
3543 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
3544 if(ret != Ok) return ret;
3550 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3557 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3559 if(!rects || count <= 0)
3560 return InvalidParameter;
3562 rectsF = GdipAlloc(sizeof(GpRectF)*count);
3566 for(i = 0; i < count; i++){
3567 rectsF[i].X = (REAL)rects[i].X;
3568 rectsF[i].Y = (REAL)rects[i].Y;
3569 rectsF[i].X = (REAL)rects[i].Width;
3570 rectsF[i].Height = (REAL)rects[i].Height;
3573 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3579 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3588 return NotImplemented;
3590 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3594 save_state = SaveDC(graphics->hdc);
3595 EndPath(graphics->hdc);
3597 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3599 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3601 BeginPath(graphics->hdc);
3602 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3603 EndPath(graphics->hdc);
3605 brush_fill_path(graphics, brush);
3608 RestoreDC(graphics->hdc, save_state);
3615 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3619 GpRegion *temp_region;
3620 GpMatrix *world_to_device, *identity;
3621 GpRectF graphics_bounds;
3622 UINT scans_count, i;
3627 if (!brush_can_fill_pixels(brush))
3628 return NotImplemented;
3630 stat = get_graphics_bounds(graphics, &graphics_bounds);
3633 stat = GdipCloneRegion(region, &temp_region);
3637 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3638 CoordinateSpaceWorld, &world_to_device);
3642 stat = GdipTransformRegion(temp_region, world_to_device);
3644 GdipDeleteMatrix(world_to_device);
3648 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3651 stat = GdipCreateMatrix(&identity);
3655 stat = GdipGetRegionScansCount(temp_region, &scans_count, identity);
3657 if (stat == Ok && scans_count != 0)
3659 scans = GdipAlloc(sizeof(*scans) * scans_count);
3665 stat = GdipGetRegionScansI(temp_region, scans, &dummy, identity);
3672 GdipDeleteMatrix(identity);
3675 GdipDeleteRegion(temp_region);
3678 if (stat == Ok && scans_count == 0)
3685 for (i=0; i<scans_count; i++)
3687 UINT size = scans[i].Width * scans[i].Height;
3689 if (size > max_size)
3693 pixel_data = GdipAlloc(sizeof(*pixel_data) * max_size);
3699 for (i=0; i<scans_count; i++)
3701 stat = brush_fill_pixels(graphics, brush, pixel_data, &scans[i],
3706 stat = alpha_blend_pixels(graphics, scans[i].X, scans[i].Y,
3707 (BYTE*)pixel_data, scans[i].Width, scans[i].Height,
3708 scans[i].Width * 4);
3715 GdipFree(pixel_data);
3724 /*****************************************************************************
3725 * GdipFillRegion [GDIPLUS.@]
3727 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3730 GpStatus stat = NotImplemented;
3732 TRACE("(%p, %p, %p)\n", graphics, brush, region);
3734 if (!(graphics && brush && region))
3735 return InvalidParameter;
3740 if (!graphics->image)
3741 stat = GDI32_GdipFillRegion(graphics, brush, region);
3743 if (stat == NotImplemented)
3744 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
3746 if (stat == NotImplemented && graphics->image)
3747 stat = GDI32_GdipFillRegion(graphics, brush, region);
3749 if (stat == NotImplemented)
3751 FIXME("not implemented for brushtype %i\n", brush->bt);
3758 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3760 TRACE("(%p,%u)\n", graphics, intention);
3763 return InvalidParameter;
3768 /* We have no internal operation queue, so there's no need to clear it. */
3776 /*****************************************************************************
3777 * GdipGetClipBounds [GDIPLUS.@]
3779 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3781 TRACE("(%p, %p)\n", graphics, rect);
3784 return InvalidParameter;
3789 return GdipGetRegionBounds(graphics->clip, graphics, rect);
3792 /*****************************************************************************
3793 * GdipGetClipBoundsI [GDIPLUS.@]
3795 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3797 TRACE("(%p, %p)\n", graphics, rect);
3800 return InvalidParameter;
3805 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3808 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3809 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3810 CompositingMode *mode)
3812 TRACE("(%p, %p)\n", graphics, mode);
3814 if(!graphics || !mode)
3815 return InvalidParameter;
3820 *mode = graphics->compmode;
3825 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3826 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3827 CompositingQuality *quality)
3829 TRACE("(%p, %p)\n", graphics, quality);
3831 if(!graphics || !quality)
3832 return InvalidParameter;
3837 *quality = graphics->compqual;
3842 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3843 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3844 InterpolationMode *mode)
3846 TRACE("(%p, %p)\n", graphics, mode);
3848 if(!graphics || !mode)
3849 return InvalidParameter;
3854 *mode = graphics->interpolation;
3859 /* FIXME: Need to handle color depths less than 24bpp */
3860 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3862 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
3864 if(!graphics || !argb)
3865 return InvalidParameter;
3873 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3875 TRACE("(%p, %p)\n", graphics, scale);
3877 if(!graphics || !scale)
3878 return InvalidParameter;
3883 *scale = graphics->scale;
3888 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3890 TRACE("(%p, %p)\n", graphics, unit);
3892 if(!graphics || !unit)
3893 return InvalidParameter;
3898 *unit = graphics->unit;
3903 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3904 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3907 TRACE("(%p, %p)\n", graphics, mode);
3909 if(!graphics || !mode)
3910 return InvalidParameter;
3915 *mode = graphics->pixeloffset;
3920 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3921 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3923 TRACE("(%p, %p)\n", graphics, mode);
3925 if(!graphics || !mode)
3926 return InvalidParameter;
3931 *mode = graphics->smoothing;
3936 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3938 TRACE("(%p, %p)\n", graphics, contrast);
3940 if(!graphics || !contrast)
3941 return InvalidParameter;
3943 *contrast = graphics->textcontrast;
3948 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3949 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3950 TextRenderingHint *hint)
3952 TRACE("(%p, %p)\n", graphics, hint);
3954 if(!graphics || !hint)
3955 return InvalidParameter;
3960 *hint = graphics->texthint;
3965 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3970 TRACE("(%p, %p)\n", graphics, rect);
3972 if(!graphics || !rect)
3973 return InvalidParameter;
3978 /* intersect window and graphics clipping regions */
3979 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3982 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3985 /* get bounds of the region */
3986 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3989 GdipDeleteRegion(clip_rgn);
3994 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3999 TRACE("(%p, %p)\n", graphics, rect);
4001 if(!graphics || !rect)
4002 return InvalidParameter;
4004 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4006 rect->X = roundr(rectf.X);
4007 rect->Y = roundr(rectf.Y);
4008 rect->Width = roundr(rectf.Width);
4009 rect->Height = roundr(rectf.Height);
4015 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4017 TRACE("(%p, %p)\n", graphics, matrix);
4019 if(!graphics || !matrix)
4020 return InvalidParameter;
4025 *matrix = *graphics->worldtrans;
4029 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4035 TRACE("(%p, %x)\n", graphics, color);
4038 return InvalidParameter;
4043 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4046 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4047 GdipDeleteBrush((GpBrush*)brush);
4051 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4052 wnd_rect.Width, wnd_rect.Height);
4054 GdipDeleteBrush((GpBrush*)brush);
4059 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4061 TRACE("(%p, %p)\n", graphics, res);
4063 if(!graphics || !res)
4064 return InvalidParameter;
4066 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4069 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4075 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4077 if(!graphics || !result)
4078 return InvalidParameter;
4085 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4086 CoordinateSpaceWorld, &pt, 1)) != Ok)
4089 if((stat = GdipCreateRegion(&rgn)) != Ok)
4092 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4095 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4098 GdipDeleteRegion(rgn);
4102 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4104 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4107 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4113 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4115 if(!graphics || !result)
4116 return InvalidParameter;
4123 pts[1].X = x + width;
4124 pts[1].Y = y + height;
4126 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4127 CoordinateSpaceWorld, pts, 2)) != Ok)
4130 pts[1].X -= pts[0].X;
4131 pts[1].Y -= pts[0].Y;
4133 if((stat = GdipCreateRegion(&rgn)) != Ok)
4136 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4139 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4142 GdipDeleteRegion(rgn);
4146 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4148 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4151 GpStatus gdip_format_string(HDC hdc,
4152 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4153 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4154 gdip_format_string_callback callback, void *user_data)
4157 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4158 nheight, lineend, lineno = 0;
4160 StringAlignment halign;
4164 if(length == -1) length = lstrlenW(string);
4166 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4167 if(!stringdup) return OutOfMemory;
4169 nwidth = roundr(rect->Width);
4170 nheight = roundr(rect->Height);
4172 if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
4173 if (rect->Height >= INT_MAX || rect->Width < 0.5) nheight = INT_MAX;
4175 for(i = 0, j = 0; i < length; i++){
4176 /* FIXME: This makes the indexes passed to callback inaccurate. */
4177 if(!isprintW(string[i]) && (string[i] != '\n'))
4180 stringdup[j] = string[i];
4186 if (format) halign = format->align;
4187 else halign = StringAlignmentNear;
4189 while(sum < length){
4190 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4191 nwidth, &fit, NULL, &size);
4197 for(lret = 0; lret < fit; lret++)
4198 if(*(stringdup + sum + lret) == '\n')
4201 /* Line break code (may look strange, but it imitates windows). */
4203 lineend = fit = lret; /* this is not an off-by-one error */
4204 else if(fit < (length - sum)){
4205 if(*(stringdup + sum + fit) == ' ')
4206 while(*(stringdup + sum + fit) == ' ')
4209 while(*(stringdup + sum + fit - 1) != ' '){
4212 if(*(stringdup + sum + fit) == '\t')
4221 while(*(stringdup + sum + lineend - 1) == ' ' ||
4222 *(stringdup + sum + lineend - 1) == '\t')
4228 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4229 nwidth, &j, NULL, &size);
4231 bounds.Width = size.cx;
4233 if(height + size.cy > nheight)
4234 bounds.Height = nheight - (height + size.cy);
4236 bounds.Height = size.cy;
4238 bounds.Y = rect->Y + height;
4242 case StringAlignmentNear:
4246 case StringAlignmentCenter:
4247 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4249 case StringAlignmentFar:
4250 bounds.X = rect->X + rect->Width - bounds.Width;
4254 stat = callback(hdc, stringdup, sum, lineend,
4255 font, rect, format, lineno, &bounds, user_data);
4260 sum += fit + (lret < fitcpy ? 1 : 0);
4264 if(height > nheight)
4267 /* Stop if this was a linewrap (but not if it was a linebreak). */
4268 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4272 GdipFree(stringdup);
4277 struct measure_ranges_args {
4281 static GpStatus measure_ranges_callback(HDC hdc,
4282 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4283 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4284 INT lineno, const RectF *bounds, void *user_data)
4288 struct measure_ranges_args *args = user_data;
4290 for (i=0; i<format->range_count; i++)
4292 INT range_start = max(index, format->character_ranges[i].First);
4293 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4294 if (range_start < range_end)
4299 range_rect.Y = bounds->Y;
4300 range_rect.Height = bounds->Height;
4302 GetTextExtentExPointW(hdc, string + index, range_start - index,
4303 INT_MAX, NULL, NULL, &range_size);
4304 range_rect.X = bounds->X + range_size.cx;
4306 GetTextExtentExPointW(hdc, string + index, range_end - index,
4307 INT_MAX, NULL, NULL, &range_size);
4308 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
4310 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4319 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4320 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4321 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4322 INT regionCount, GpRegion** regions)
4327 struct measure_ranges_args args;
4328 HDC hdc, temp_hdc=NULL;
4330 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4331 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4333 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4334 return InvalidParameter;
4336 if (regionCount < stringFormat->range_count)
4337 return InvalidParameter;
4341 hdc = temp_hdc = CreateCompatibleDC(0);
4342 if (!temp_hdc) return OutOfMemory;
4345 hdc = graphics->hdc;
4347 if (stringFormat->attr)
4348 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4350 oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4352 for (i=0; i<stringFormat->range_count; i++)
4354 stat = GdipSetEmpty(regions[i]);
4359 args.regions = regions;
4361 stat = gdip_format_string(hdc, string, length, font, layoutRect, stringFormat,
4362 measure_ranges_callback, &args);
4364 DeleteObject(SelectObject(hdc, oldfont));
4372 struct measure_string_args {
4374 INT *codepointsfitted;
4378 static GpStatus measure_string_callback(HDC hdc,
4379 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4380 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4381 INT lineno, const RectF *bounds, void *user_data)
4383 struct measure_string_args *args = user_data;
4385 if (bounds->Width > args->bounds->Width)
4386 args->bounds->Width = bounds->Width;
4388 if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
4389 args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
4391 if (args->codepointsfitted)
4392 *args->codepointsfitted = index + length;
4394 if (args->linesfilled)
4395 (*args->linesfilled)++;
4400 /* Find the smallest rectangle that bounds the text when it is printed in rect
4401 * according to the format options listed in format. If rect has 0 width and
4402 * height, then just find the smallest rectangle that bounds the text when it's
4403 * printed at location (rect->X, rect-Y). */
4404 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4405 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4406 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4407 INT *codepointsfitted, INT *linesfilled)
4410 struct measure_string_args args;
4413 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4414 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4415 bounds, codepointsfitted, linesfilled);
4417 if(!graphics || !string || !font || !rect || !bounds)
4418 return InvalidParameter;
4422 temp_hdc = CreateCompatibleDC(0);
4423 if (!temp_hdc) return OutOfMemory;
4426 if(linesfilled) *linesfilled = 0;
4427 if(codepointsfitted) *codepointsfitted = 0;
4430 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4432 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4434 bounds->X = rect->X;
4435 bounds->Y = rect->Y;
4436 bounds->Width = 0.0;
4437 bounds->Height = 0.0;
4439 args.bounds = bounds;
4440 args.codepointsfitted = codepointsfitted;
4441 args.linesfilled = linesfilled;
4443 gdip_format_string(graphics->hdc ? graphics->hdc : temp_hdc, string, length, font, rect, format,
4444 measure_string_callback, &args);
4446 DeleteObject(SelectObject(graphics->hdc, oldfont));
4454 struct draw_string_args {
4457 REAL ang_cos, ang_sin;
4460 static GpStatus draw_string_callback(HDC hdc,
4461 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4462 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4463 INT lineno, const RectF *bounds, void *user_data)
4465 struct draw_string_args *args = user_data;
4468 drawcoord.left = drawcoord.right = args->drawbase.x + roundr(args->ang_sin * bounds->Y);
4469 drawcoord.top = drawcoord.bottom = args->drawbase.y + roundr(args->ang_cos * bounds->Y);
4471 DrawTextW(hdc, string + index, length, &drawcoord, args->drawflags);
4476 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4477 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4478 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4483 TEXTMETRICW textmet;
4484 GpPointF pt[3], rectcpy[4];
4486 REAL angle, rel_width, rel_height;
4487 INT offsety = 0, save_state;
4488 struct draw_string_args args;
4491 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4492 length, font, debugstr_rectf(rect), format, brush);
4494 if(!graphics || !string || !font || !brush || !rect)
4495 return InvalidParameter;
4497 if((brush->bt != BrushTypeSolidColor)){
4498 FIXME("not implemented for given parameters\n");
4499 return NotImplemented;
4504 FIXME("graphics object has no HDC\n");
4509 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4511 /* Should be no need to explicitly test for StringAlignmentNear as
4512 * that is default behavior if no alignment is passed. */
4513 if(format->vertalign != StringAlignmentNear){
4515 GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
4517 if(format->vertalign == StringAlignmentCenter)
4518 offsety = (rect->Height - bounds.Height) / 2;
4519 else if(format->vertalign == StringAlignmentFar)
4520 offsety = (rect->Height - bounds.Height);
4524 save_state = SaveDC(graphics->hdc);
4525 SetBkMode(graphics->hdc, TRANSPARENT);
4526 SetTextColor(graphics->hdc, brush->lb.lbColor);
4534 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4535 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
4536 args.ang_cos = cos(angle);
4537 args.ang_sin = sin(angle);
4538 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4539 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4540 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4541 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4543 rectcpy[3].X = rectcpy[0].X = rect->X;
4544 rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
4545 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4546 rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
4547 transform_and_round_points(graphics, corners, rectcpy, 4);
4549 scaled_rect.X = 0.0;
4550 scaled_rect.Y = 0.0;
4551 scaled_rect.Width = rel_width * rect->Width;
4552 scaled_rect.Height = rel_height * rect->Height;
4554 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
4556 /* FIXME: If only the width or only the height is 0, we should probably still clip */
4557 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4558 SelectClipRgn(graphics->hdc, rgn);
4561 /* Use gdi to find the font, then perform transformations on it (height,
4563 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4564 GetTextMetricsW(graphics->hdc, &textmet);
4567 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
4568 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
4570 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
4572 gdifont = CreateFontIndirectW(&lfw);
4573 DeleteObject(SelectObject(graphics->hdc, gdifont));
4575 if (!format || format->align == StringAlignmentNear)
4577 args.drawbase.x = corners[0].x;
4578 args.drawbase.y = corners[0].y;
4579 args.drawflags = DT_NOCLIP | DT_EXPANDTABS;
4581 else if (format->align == StringAlignmentCenter)
4583 args.drawbase.x = (corners[0].x + corners[1].x)/2;
4584 args.drawbase.y = (corners[0].y + corners[1].y)/2;
4585 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
4587 else /* (format->align == StringAlignmentFar) */
4589 args.drawbase.x = corners[1].x;
4590 args.drawbase.y = corners[1].y;
4591 args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
4594 gdip_format_string(graphics->hdc, string, length, font, &scaled_rect, format,
4595 draw_string_callback, &args);
4598 DeleteObject(gdifont);
4600 RestoreDC(graphics->hdc, save_state);
4605 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
4607 TRACE("(%p)\n", graphics);
4610 return InvalidParameter;
4615 return GdipSetInfinite(graphics->clip);
4618 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
4620 TRACE("(%p)\n", graphics);
4623 return InvalidParameter;
4628 graphics->worldtrans->matrix[0] = 1.0;
4629 graphics->worldtrans->matrix[1] = 0.0;
4630 graphics->worldtrans->matrix[2] = 0.0;
4631 graphics->worldtrans->matrix[3] = 1.0;
4632 graphics->worldtrans->matrix[4] = 0.0;
4633 graphics->worldtrans->matrix[5] = 0.0;
4638 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
4640 return GdipEndContainer(graphics, state);
4643 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
4644 GpMatrixOrder order)
4646 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
4649 return InvalidParameter;
4654 return GdipRotateMatrix(graphics->worldtrans, angle, order);
4657 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
4659 return GdipBeginContainer2(graphics, state);
4662 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
4663 GraphicsContainer *state)
4665 GraphicsContainerItem *container;
4668 TRACE("(%p, %p)\n", graphics, state);
4670 if(!graphics || !state)
4671 return InvalidParameter;
4673 sts = init_container(&container, graphics);
4677 list_add_head(&graphics->containers, &container->entry);
4678 *state = graphics->contid = container->contid;
4683 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
4685 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4686 return NotImplemented;
4689 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
4691 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4692 return NotImplemented;
4695 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
4697 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
4698 return NotImplemented;
4701 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
4704 GraphicsContainerItem *container, *container2;
4706 TRACE("(%p, %x)\n", graphics, state);
4709 return InvalidParameter;
4711 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
4712 if(container->contid == state)
4716 /* did not find a matching container */
4717 if(&container->entry == &graphics->containers)
4720 sts = restore_container(graphics, container);
4724 /* remove all of the containers on top of the found container */
4725 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
4726 if(container->contid == state)
4728 list_remove(&container->entry);
4729 delete_container(container);
4732 list_remove(&container->entry);
4733 delete_container(container);
4738 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
4739 REAL sy, GpMatrixOrder order)
4741 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
4744 return InvalidParameter;
4749 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
4752 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
4755 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
4757 if(!graphics || !srcgraphics)
4758 return InvalidParameter;
4760 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
4763 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
4764 CompositingMode mode)
4766 TRACE("(%p, %d)\n", graphics, mode);
4769 return InvalidParameter;
4774 graphics->compmode = mode;
4779 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
4780 CompositingQuality quality)
4782 TRACE("(%p, %d)\n", graphics, quality);
4785 return InvalidParameter;
4790 graphics->compqual = quality;
4795 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
4796 InterpolationMode mode)
4798 TRACE("(%p, %d)\n", graphics, mode);
4800 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
4801 return InvalidParameter;
4806 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
4807 mode = InterpolationModeBilinear;
4809 if (mode == InterpolationModeHighQuality)
4810 mode = InterpolationModeHighQualityBicubic;
4812 graphics->interpolation = mode;
4817 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
4819 TRACE("(%p, %.2f)\n", graphics, scale);
4821 if(!graphics || (scale <= 0.0))
4822 return InvalidParameter;
4827 graphics->scale = scale;
4832 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4834 TRACE("(%p, %d)\n", graphics, unit);
4837 return InvalidParameter;
4842 if(unit == UnitWorld)
4843 return InvalidParameter;
4845 graphics->unit = unit;
4850 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4853 TRACE("(%p, %d)\n", graphics, mode);
4856 return InvalidParameter;
4861 graphics->pixeloffset = mode;
4866 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
4870 TRACE("(%p,%i,%i)\n", graphics, x, y);
4873 FIXME("not implemented\n");
4875 return NotImplemented;
4878 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
4882 TRACE("(%p,%p,%p)\n", graphics, x, y);
4885 FIXME("not implemented\n");
4889 return NotImplemented;
4892 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
4894 TRACE("(%p, %d)\n", graphics, mode);
4897 return InvalidParameter;
4902 graphics->smoothing = mode;
4907 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
4909 TRACE("(%p, %d)\n", graphics, contrast);
4912 return InvalidParameter;
4914 graphics->textcontrast = contrast;
4919 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
4920 TextRenderingHint hint)
4922 TRACE("(%p, %d)\n", graphics, hint);
4925 return InvalidParameter;
4930 graphics->texthint = hint;
4935 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4937 TRACE("(%p, %p)\n", graphics, matrix);
4939 if(!graphics || !matrix)
4940 return InvalidParameter;
4945 GdipDeleteMatrix(graphics->worldtrans);
4946 return GdipCloneMatrix(matrix, &graphics->worldtrans);
4949 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
4950 REAL dy, GpMatrixOrder order)
4952 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
4955 return InvalidParameter;
4960 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
4963 /*****************************************************************************
4964 * GdipSetClipHrgn [GDIPLUS.@]
4966 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
4971 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
4974 return InvalidParameter;
4976 status = GdipCreateRegionHrgn(hrgn, ®ion);
4980 status = GdipSetClipRegion(graphics, region, mode);
4982 GdipDeleteRegion(region);
4986 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
4988 TRACE("(%p, %p, %d)\n", graphics, path, mode);
4991 return InvalidParameter;
4996 return GdipCombineRegionPath(graphics->clip, path, mode);
4999 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5000 REAL width, REAL height,
5005 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5008 return InvalidParameter;
5016 rect.Height = height;
5018 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5021 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5022 INT width, INT height,
5025 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5028 return InvalidParameter;
5033 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5036 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5039 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5041 if(!graphics || !region)
5042 return InvalidParameter;
5047 return GdipCombineRegionRegion(graphics->clip, region, mode);
5050 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5055 TRACE("(%p,%u)\n", metafile, limitDpi);
5058 FIXME("not implemented\n");
5060 return NotImplemented;
5063 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5069 TRACE("(%p, %p, %d)\n", graphics, points, count);
5071 if(!graphics || !pen || count<=0)
5072 return InvalidParameter;
5079 FIXME("graphics object has no HDC\n");
5083 pti = GdipAlloc(sizeof(POINT) * count);
5085 save_state = prepare_dc(graphics, pen);
5086 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5088 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5089 Polygon(graphics->hdc, pti, count);
5091 restore_dc(graphics, save_state);
5097 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5104 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5106 if(count<=0) return InvalidParameter;
5107 ptf = GdipAlloc(sizeof(GpPointF) * count);
5109 for(i = 0;i < count; i++){
5110 ptf[i].X = (REAL)points[i].X;
5111 ptf[i].Y = (REAL)points[i].Y;
5114 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5120 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5122 TRACE("(%p, %p)\n", graphics, dpi);
5124 if(!graphics || !dpi)
5125 return InvalidParameter;
5130 if (graphics->image)
5131 *dpi = graphics->image->xres;
5133 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
5138 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5140 TRACE("(%p, %p)\n", graphics, dpi);
5142 if(!graphics || !dpi)
5143 return InvalidParameter;
5148 if (graphics->image)
5149 *dpi = graphics->image->yres;
5151 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
5156 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5157 GpMatrixOrder order)
5162 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5164 if(!graphics || !matrix)
5165 return InvalidParameter;
5170 m = *(graphics->worldtrans);
5172 ret = GdipMultiplyMatrix(&m, matrix, order);
5174 *(graphics->worldtrans) = m;
5179 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5180 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5182 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5184 TRACE("(%p, %p)\n", graphics, hdc);
5186 if(!graphics || !hdc)
5187 return InvalidParameter;
5192 if (!graphics->hdc ||
5193 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5195 /* Create a fake HDC and fill it with a constant color. */
5200 BITMAPINFOHEADER bmih;
5203 stat = get_graphics_bounds(graphics, &bounds);
5207 graphics->temp_hbitmap_width = bounds.Width;
5208 graphics->temp_hbitmap_height = bounds.Height;
5210 bmih.biSize = sizeof(bmih);
5211 bmih.biWidth = graphics->temp_hbitmap_width;
5212 bmih.biHeight = -graphics->temp_hbitmap_height;
5214 bmih.biBitCount = 32;
5215 bmih.biCompression = BI_RGB;
5216 bmih.biSizeImage = 0;
5217 bmih.biXPelsPerMeter = 0;
5218 bmih.biYPelsPerMeter = 0;
5220 bmih.biClrImportant = 0;
5222 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5223 (void**)&graphics->temp_bits, NULL, 0);
5225 return GenericError;
5227 temp_hdc = CreateCompatibleDC(0);
5230 DeleteObject(hbitmap);
5231 return GenericError;
5234 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5235 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5237 SelectObject(temp_hdc, hbitmap);
5239 graphics->temp_hbitmap = hbitmap;
5240 *hdc = graphics->temp_hdc = temp_hdc;
5244 *hdc = graphics->hdc;
5247 graphics->busy = TRUE;
5252 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5254 TRACE("(%p, %p)\n", graphics, hdc);
5256 if(!graphics || !hdc)
5257 return InvalidParameter;
5259 if((graphics->hdc != hdc && graphics->temp_hdc != hdc) || !(graphics->busy))
5260 return InvalidParameter;
5262 if (graphics->temp_hdc == hdc)
5267 /* Find the pixels that have changed, and mark them as opaque. */
5268 pos = (DWORD*)graphics->temp_bits;
5269 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5271 if (*pos != DC_BACKGROUND_KEY)
5278 /* Write the changed pixels to the real target. */
5279 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5280 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5281 graphics->temp_hbitmap_width * 4);
5284 DeleteDC(graphics->temp_hdc);
5285 DeleteObject(graphics->temp_hbitmap);
5286 graphics->temp_hdc = NULL;
5287 graphics->temp_hbitmap = NULL;
5290 graphics->busy = FALSE;
5295 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5300 TRACE("(%p, %p)\n", graphics, region);
5302 if(!graphics || !region)
5303 return InvalidParameter;
5308 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5311 /* free everything except root node and header */
5312 delete_element(®ion->node);
5313 memcpy(region, clip, sizeof(GpRegion));
5319 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5320 GpCoordinateSpace src_space, GpMatrix **matrix)
5322 GpStatus stat = GdipCreateMatrix(matrix);
5325 if (dst_space != src_space && stat == Ok)
5327 unitscale = convert_unit(graphics_res(graphics), graphics->unit);
5329 if(graphics->unit != UnitDisplay)
5330 unitscale *= graphics->scale;
5332 /* transform from src_space to CoordinateSpacePage */
5335 case CoordinateSpaceWorld:
5336 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5338 case CoordinateSpacePage:
5340 case CoordinateSpaceDevice:
5341 GdipScaleMatrix(*matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
5345 /* transform from CoordinateSpacePage to dst_space */
5348 case CoordinateSpaceWorld:
5350 GpMatrix *inverted_transform;
5351 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
5354 stat = GdipInvertMatrix(inverted_transform);
5356 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
5357 GdipDeleteMatrix(inverted_transform);
5361 case CoordinateSpacePage:
5363 case CoordinateSpaceDevice:
5364 GdipScaleMatrix(*matrix, unitscale, unitscale, MatrixOrderAppend);
5371 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5372 GpCoordinateSpace src_space, GpPointF *points, INT count)
5377 if(!graphics || !points || count <= 0)
5378 return InvalidParameter;
5383 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5385 if (src_space == dst_space) return Ok;
5387 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5391 stat = GdipTransformMatrixPoints(matrix, points, count);
5393 GdipDeleteMatrix(matrix);
5399 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5400 GpCoordinateSpace src_space, GpPoint *points, INT count)
5406 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5409 return InvalidParameter;
5411 pointsF = GdipAlloc(sizeof(GpPointF) * count);
5415 for(i = 0; i < count; i++){
5416 pointsF[i].X = (REAL)points[i].X;
5417 pointsF[i].Y = (REAL)points[i].Y;
5420 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5423 for(i = 0; i < count; i++){
5424 points[i].X = roundr(pointsF[i].X);
5425 points[i].Y = roundr(pointsF[i].Y);
5432 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5444 /*****************************************************************************
5445 * GdipTranslateClip [GDIPLUS.@]
5447 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5449 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5452 return InvalidParameter;
5457 return GdipTranslateRegion(graphics->clip, dx, dy);
5460 /*****************************************************************************
5461 * GdipTranslateClipI [GDIPLUS.@]
5463 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5465 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5468 return InvalidParameter;
5473 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5477 /*****************************************************************************
5478 * GdipMeasureDriverString [GDIPLUS.@]
5480 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5481 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5482 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5484 FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5485 return NotImplemented;
5488 /*****************************************************************************
5489 * GdipDrawDriverString [GDIPLUS.@]
5491 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5492 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5493 GDIPCONST PointF *positions, INT flags,
5494 GDIPCONST GpMatrix *matrix )
5496 FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
5497 return NotImplemented;
5500 GpStatus WINGDIPAPI GdipRecordMetafile(HDC hdc, EmfType type, GDIPCONST GpRectF *frameRect,
5501 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5503 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5504 return NotImplemented;
5507 /*****************************************************************************
5508 * GdipRecordMetafileI [GDIPLUS.@]
5510 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5511 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5513 FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5514 return NotImplemented;
5517 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5518 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5520 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
5521 return NotImplemented;
5524 /*****************************************************************************
5525 * GdipIsVisibleClipEmpty [GDIPLUS.@]
5527 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
5532 TRACE("(%p, %p)\n", graphics, res);
5534 if((stat = GdipCreateRegion(&rgn)) != Ok)
5537 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5540 stat = GdipIsEmptyRegion(rgn, graphics, res);
5543 GdipDeleteRegion(rgn);
5547 GpStatus WINGDIPAPI GdipGetHemfFromMetafile(GpMetafile *metafile, HENHMETAFILE *hEmf)
5549 FIXME("(%p,%p): stub\n", metafile, hEmf);
5551 if (!metafile || !hEmf)
5552 return InvalidParameter;
5556 return NotImplemented;