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 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 /* Converts angle (in degrees) to x/y coordinates */
55 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
57 REAL radAngle, hypotenuse;
59 radAngle = deg2rad(angle);
60 hypotenuse = 50.0; /* arbitrary */
62 *x = x_0 + cos(radAngle) * hypotenuse;
63 *y = y_0 + sin(radAngle) * hypotenuse;
66 /* Converts from gdiplus path point type to gdi path point type. */
67 static BYTE convert_path_point_type(BYTE type)
71 switch(type & PathPointTypePathTypeMask){
72 case PathPointTypeBezier:
75 case PathPointTypeLine:
78 case PathPointTypeStart:
82 ERR("Bad point type\n");
86 if(type & PathPointTypeCloseSubpath)
87 ret |= PT_CLOSEFIGURE;
92 static COLORREF get_gdi_brush_color(const GpBrush *brush)
98 case BrushTypeSolidColor:
100 const GpSolidFill *sf = (const GpSolidFill *)brush;
104 case BrushTypeHatchFill:
106 const GpHatch *hatch = (const GpHatch *)brush;
107 argb = hatch->forecol;
110 case BrushTypeLinearGradient:
112 const GpLineGradient *line = (const GpLineGradient *)brush;
113 argb = line->startcolor;
116 case BrushTypePathGradient:
118 const GpPathGradient *grad = (const GpPathGradient *)brush;
119 argb = grad->centercolor;
123 FIXME("unhandled brush type %d\n", brush->bt);
127 return ARGB2COLORREF(argb);
130 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
133 BITMAPINFOHEADER bmih;
137 bmih.biSize = sizeof(bmih);
141 bmih.biBitCount = 32;
142 bmih.biCompression = BI_RGB;
143 bmih.biSizeImage = 0;
145 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
148 const char *hatch_data;
150 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
152 for (y = 0; y < 8; y++)
154 for (x = 0; x < 8; x++)
156 if (hatch_data[y] & (0x80 >> x))
157 bits[y * 8 + x] = hatch->forecol;
159 bits[y * 8 + x] = hatch->backcol;
165 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
167 for (y = 0; y < 64; y++)
168 bits[y] = hatch->forecol;
175 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
179 case BrushTypeSolidColor:
181 const GpSolidFill *sf = (const GpSolidFill *)brush;
182 lb->lbStyle = BS_SOLID;
183 lb->lbColor = ARGB2COLORREF(sf->color);
188 case BrushTypeHatchFill:
190 const GpHatch *hatch = (const GpHatch *)brush;
193 hbmp = create_hatch_bitmap(hatch);
194 if (!hbmp) return OutOfMemory;
196 lb->lbStyle = BS_PATTERN;
198 lb->lbHatch = (ULONG_PTR)hbmp;
203 FIXME("unhandled brush type %d\n", brush->bt);
204 lb->lbStyle = BS_SOLID;
205 lb->lbColor = get_gdi_brush_color(brush);
211 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
216 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
222 static HBRUSH create_gdi_brush(const GpBrush *brush)
227 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
229 gdibrush = CreateBrushIndirect(&lb);
230 free_gdi_logbrush(&lb);
235 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
240 INT save_state, i, numdashes;
242 DWORD dash_array[MAX_DASHLEN];
244 save_state = SaveDC(graphics->hdc);
246 EndPath(graphics->hdc);
248 if(pen->unit == UnitPixel){
252 /* Get an estimate for the amount the pen width is affected by the world
253 * transform. (This is similar to what some of the wine drivers do.) */
258 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
259 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
260 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
262 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
265 if(pen->dash == DashStyleCustom){
266 numdashes = min(pen->numdashes, MAX_DASHLEN);
268 TRACE("dashes are: ");
269 for(i = 0; i < numdashes; i++){
270 dash_array[i] = gdip_round(width * pen->dashes[i]);
271 TRACE("%d, ", dash_array[i]);
273 TRACE("\n and the pen style is %x\n", pen->style);
275 create_gdi_logbrush(pen->brush, &lb);
276 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
277 numdashes, dash_array);
278 free_gdi_logbrush(&lb);
282 create_gdi_logbrush(pen->brush, &lb);
283 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
284 free_gdi_logbrush(&lb);
287 SelectObject(graphics->hdc, gdipen);
292 static void restore_dc(GpGraphics *graphics, INT state)
294 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
295 RestoreDC(graphics->hdc, state);
298 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
299 GpCoordinateSpace src_space, GpMatrix *matrix);
301 /* This helper applies all the changes that the points listed in ptf need in
302 * order to be drawn on the device context. In the end, this should include at
304 * -scaling by page unit
305 * -applying world transformation
306 * -converting from float to int
307 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
308 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
309 * gdi to draw, and these functions would irreparably mess with line widths.
311 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
312 GpPointF *ptf, INT count)
314 REAL scale_x, scale_y;
318 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
319 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
321 /* apply page scale */
322 if(graphics->unit != UnitDisplay)
324 scale_x *= graphics->scale;
325 scale_y *= graphics->scale;
328 matrix = graphics->worldtrans;
329 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
330 GdipTransformMatrixPoints(&matrix, ptf, count);
332 for(i = 0; i < count; i++){
333 pti[i].x = gdip_round(ptf[i].X);
334 pti[i].y = gdip_round(ptf[i].Y);
338 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
339 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
341 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
343 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
345 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
346 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
352 bf.BlendOp = AC_SRC_OVER;
354 bf.SourceConstantAlpha = 255;
355 bf.AlphaFormat = AC_SRC_ALPHA;
357 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
358 hdc, src_x, src_y, src_width, src_height, bf);
362 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
364 return GdipGetRegionHRgn(graphics->clip, graphics, hrgn);
367 /* Draw non-premultiplied ARGB data to the given graphics object */
368 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
369 const BYTE *src, INT src_width, INT src_height, INT src_stride)
371 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
374 for (x=0; x<src_width; x++)
376 for (y=0; y<src_height; y++)
378 ARGB dst_color, src_color;
379 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
380 src_color = ((ARGB*)(src + src_stride * y))[x];
381 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
388 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
389 const BYTE *src, INT src_width, INT src_height, INT src_stride)
393 BITMAPINFOHEADER bih;
396 hdc = CreateCompatibleDC(0);
398 bih.biSize = sizeof(BITMAPINFOHEADER);
399 bih.biWidth = src_width;
400 bih.biHeight = -src_height;
403 bih.biCompression = BI_RGB;
405 bih.biXPelsPerMeter = 0;
406 bih.biYPelsPerMeter = 0;
408 bih.biClrImportant = 0;
410 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
411 (void**)&temp_bits, NULL, 0);
413 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
414 4 * src_width, src, src_stride);
416 SelectObject(hdc, hbitmap);
417 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
418 hdc, 0, 0, src_width, src_height);
420 DeleteObject(hbitmap);
425 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
426 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
430 if (graphics->image && graphics->image->type == ImageTypeBitmap)
435 HRGN hrgn, visible_rgn;
437 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
441 stat = get_clip_hrgn(graphics, &visible_rgn);
450 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
451 DeleteObject(visible_rgn);
455 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
457 size = GetRegionData(hrgn, 0, NULL);
459 rgndata = GdipAlloc(size);
466 GetRegionData(hrgn, size, rgndata);
468 rects = (RECT*)rgndata->Buffer;
470 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
472 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
473 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
474 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
484 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
486 ERR("This should not be used for metafiles; fix caller\n");
487 return NotImplemented;
494 stat = get_clip_hrgn(graphics, &hrgn);
499 save = SaveDC(graphics->hdc);
502 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
505 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
507 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
508 src_height, src_stride);
510 RestoreDC(graphics->hdc, save);
518 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
519 const BYTE *src, INT src_width, INT src_height, INT src_stride)
521 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL);
524 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
530 a1 = (start >> 24) & 0xff;
531 a2 = (end >> 24) & 0xff;
533 a3 = (int)(a1*(1.0f - position)+a2*(position));
537 for (i=0xff; i<=0xff0000; i = i << 8)
538 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
542 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
546 /* clamp to between 0.0 and 1.0, using the wrap mode */
547 if (brush->wrap == WrapModeTile)
549 position = fmodf(position, 1.0f);
550 if (position < 0.0f) position += 1.0f;
552 else /* WrapModeFlip* */
554 position = fmodf(position, 2.0f);
555 if (position < 0.0f) position += 2.0f;
556 if (position > 1.0f) position = 2.0f - position;
559 if (brush->blendcount == 1)
564 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
567 /* locate the blend positions surrounding this position */
568 while (position > brush->blendpos[i])
571 /* interpolate between the blend positions */
572 left_blendpos = brush->blendpos[i-1];
573 left_blendfac = brush->blendfac[i-1];
574 right_blendpos = brush->blendpos[i];
575 right_blendfac = brush->blendfac[i];
576 range = right_blendpos - left_blendpos;
577 blendfac = (left_blendfac * (right_blendpos - position) +
578 right_blendfac * (position - left_blendpos)) / range;
581 if (brush->pblendcount == 0)
582 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
586 ARGB left_blendcolor, right_blendcolor;
587 REAL left_blendpos, right_blendpos;
589 /* locate the blend colors surrounding this position */
590 while (blendfac > brush->pblendpos[i])
593 /* interpolate between the blend colors */
594 left_blendpos = brush->pblendpos[i-1];
595 left_blendcolor = brush->pblendcolor[i-1];
596 right_blendpos = brush->pblendpos[i];
597 right_blendcolor = brush->pblendcolor[i];
598 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
599 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
603 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
607 unsigned char a, r, g, b;
609 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
610 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
611 val[2] = (color & 0xff) / 255.0; /* blue */
612 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
613 val[4] = 1.0; /* translation */
620 res[i] += matrix->m[j][i] * val[j];
623 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
624 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
625 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
626 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
628 return (a << 24) | (r << 16) | (g << 8) | b;
631 static int color_is_gray(ARGB color)
633 unsigned char r, g, b;
635 r = (color >> 16) & 0xff;
636 g = (color >> 8) & 0xff;
639 return (r == g) && (g == b);
642 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
643 UINT width, UINT height, INT stride, ColorAdjustType type)
647 if (attributes->colorkeys[type].enabled ||
648 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
650 const struct color_key *key;
651 BYTE min_blue, min_green, min_red;
652 BYTE max_blue, max_green, max_red;
654 if (attributes->colorkeys[type].enabled)
655 key = &attributes->colorkeys[type];
657 key = &attributes->colorkeys[ColorAdjustTypeDefault];
659 min_blue = key->low&0xff;
660 min_green = (key->low>>8)&0xff;
661 min_red = (key->low>>16)&0xff;
663 max_blue = key->high&0xff;
664 max_green = (key->high>>8)&0xff;
665 max_red = (key->high>>16)&0xff;
667 for (x=0; x<width; x++)
668 for (y=0; y<height; y++)
671 BYTE blue, green, red;
672 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
673 blue = *src_color&0xff;
674 green = (*src_color>>8)&0xff;
675 red = (*src_color>>16)&0xff;
676 if (blue >= min_blue && green >= min_green && red >= min_red &&
677 blue <= max_blue && green <= max_green && red <= max_red)
678 *src_color = 0x00000000;
682 if (attributes->colorremaptables[type].enabled ||
683 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
685 const struct color_remap_table *table;
687 if (attributes->colorremaptables[type].enabled)
688 table = &attributes->colorremaptables[type];
690 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
692 for (x=0; x<width; x++)
693 for (y=0; y<height; y++)
696 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
697 for (i=0; i<table->mapsize; i++)
699 if (*src_color == table->colormap[i].oldColor.Argb)
701 *src_color = table->colormap[i].newColor.Argb;
708 if (attributes->colormatrices[type].enabled ||
709 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
711 const struct color_matrix *colormatrices;
713 if (attributes->colormatrices[type].enabled)
714 colormatrices = &attributes->colormatrices[type];
716 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
718 for (x=0; x<width; x++)
719 for (y=0; y<height; y++)
722 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
724 if (colormatrices->flags == ColorMatrixFlagsDefault ||
725 !color_is_gray(*src_color))
727 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
729 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
731 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
736 if (attributes->gamma_enabled[type] ||
737 attributes->gamma_enabled[ColorAdjustTypeDefault])
741 if (attributes->gamma_enabled[type])
742 gamma = attributes->gamma[type];
744 gamma = attributes->gamma[ColorAdjustTypeDefault];
746 for (x=0; x<width; x++)
747 for (y=0; y<height; y++)
750 BYTE blue, green, red;
751 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
753 blue = *src_color&0xff;
754 green = (*src_color>>8)&0xff;
755 red = (*src_color>>16)&0xff;
757 /* FIXME: We should probably use a table for this. */
758 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
759 green = floorf(powf(green / 255.0, gamma) * 255.0);
760 red = floorf(powf(red / 255.0, gamma) * 255.0);
762 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
767 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
768 * bitmap that contains all the pixels we may need to draw it. */
769 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
770 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
773 INT left, top, right, bottom;
775 switch (interpolation)
777 case InterpolationModeHighQualityBilinear:
778 case InterpolationModeHighQualityBicubic:
779 /* FIXME: Include a greater range for the prefilter? */
780 case InterpolationModeBicubic:
781 case InterpolationModeBilinear:
782 left = (INT)(floorf(srcx));
783 top = (INT)(floorf(srcy));
784 right = (INT)(ceilf(srcx+srcwidth));
785 bottom = (INT)(ceilf(srcy+srcheight));
787 case InterpolationModeNearestNeighbor:
789 left = gdip_round(srcx);
790 top = gdip_round(srcy);
791 right = gdip_round(srcx+srcwidth);
792 bottom = gdip_round(srcy+srcheight);
796 if (wrap == WrapModeClamp)
802 if (right >= bitmap->width)
803 right = bitmap->width-1;
804 if (bottom >= bitmap->height)
805 bottom = bitmap->height-1;
809 /* In some cases we can make the rectangle smaller here, but the logic
810 * is hard to get right, and tiling suggests we're likely to use the
811 * entire source image. */
812 if (left < 0 || right >= bitmap->width)
815 right = bitmap->width-1;
818 if (top < 0 || bottom >= bitmap->height)
821 bottom = bitmap->height-1;
827 rect->Width = right - left + 1;
828 rect->Height = bottom - top + 1;
831 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
832 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
834 if (attributes->wrap == WrapModeClamp)
836 if (x < 0 || y < 0 || x >= width || y >= height)
837 return attributes->outside_color;
841 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
843 x = width*2 + x % (width * 2);
845 y = height*2 + y % (height * 2);
847 if ((attributes->wrap & 1) == 1)
850 if ((x / width) % 2 == 0)
853 x = width - 1 - x % width;
858 if ((attributes->wrap & 2) == 2)
861 if ((y / height) % 2 == 0)
864 y = height - 1 - y % height;
870 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
872 ERR("out of range pixel requested\n");
876 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
879 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
880 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
881 InterpolationMode interpolation, PixelOffsetMode offset_mode)
885 switch (interpolation)
889 FIXME("Unimplemented interpolation %i\n", interpolation);
891 case InterpolationModeBilinear:
894 INT leftx, rightx, topy, bottomy;
895 ARGB topleft, topright, bottomleft, bottomright;
899 leftxf = floorf(point->X);
901 rightx = (INT)ceilf(point->X);
902 topyf = floorf(point->Y);
904 bottomy = (INT)ceilf(point->Y);
906 if (leftx == rightx && topy == bottomy)
907 return sample_bitmap_pixel(src_rect, bits, width, height,
908 leftx, topy, attributes);
910 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
911 leftx, topy, attributes);
912 topright = sample_bitmap_pixel(src_rect, bits, width, height,
913 rightx, topy, attributes);
914 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
915 leftx, bottomy, attributes);
916 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
917 rightx, bottomy, attributes);
919 x_offset = point->X - leftxf;
920 top = blend_colors(topleft, topright, x_offset);
921 bottom = blend_colors(bottomleft, bottomright, x_offset);
923 return blend_colors(top, bottom, point->Y - topyf);
925 case InterpolationModeNearestNeighbor:
931 case PixelOffsetModeNone:
932 case PixelOffsetModeHighSpeed:
936 case PixelOffsetModeHalf:
937 case PixelOffsetModeHighQuality:
941 return sample_bitmap_pixel(src_rect, bits, width, height,
942 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
948 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
950 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
953 static INT brush_can_fill_path(GpBrush *brush)
957 case BrushTypeSolidColor:
959 case BrushTypeHatchFill:
961 GpHatch *hatch = (GpHatch*)brush;
962 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
963 ((hatch->backcol & 0xff000000) == 0xff000000);
965 case BrushTypeLinearGradient:
966 case BrushTypeTextureFill:
967 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
973 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
977 case BrushTypeSolidColor:
979 GpSolidFill *fill = (GpSolidFill*)brush;
980 HBITMAP bmp = ARGB2BMP(fill->color);
985 /* partially transparent fill */
987 SelectClipPath(graphics->hdc, RGN_AND);
988 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
990 HDC hdc = CreateCompatibleDC(NULL);
994 SelectObject(hdc, bmp);
995 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1003 /* else fall through */
1007 HBRUSH gdibrush, old_brush;
1009 gdibrush = create_gdi_brush(brush);
1010 if (!gdibrush) return;
1012 old_brush = SelectObject(graphics->hdc, gdibrush);
1013 FillPath(graphics->hdc);
1014 SelectObject(graphics->hdc, old_brush);
1015 DeleteObject(gdibrush);
1021 static INT brush_can_fill_pixels(GpBrush *brush)
1025 case BrushTypeSolidColor:
1026 case BrushTypeHatchFill:
1027 case BrushTypeLinearGradient:
1028 case BrushTypeTextureFill:
1029 case BrushTypePathGradient:
1036 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1037 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1041 case BrushTypeSolidColor:
1044 GpSolidFill *fill = (GpSolidFill*)brush;
1045 for (x=0; x<fill_area->Width; x++)
1046 for (y=0; y<fill_area->Height; y++)
1047 argb_pixels[x + y*cdwStride] = fill->color;
1050 case BrushTypeHatchFill:
1053 GpHatch *fill = (GpHatch*)brush;
1054 const char *hatch_data;
1056 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1057 return NotImplemented;
1059 for (x=0; x<fill_area->Width; x++)
1060 for (y=0; y<fill_area->Height; y++)
1064 /* FIXME: Account for the rendering origin */
1065 hx = (x + fill_area->X) % 8;
1066 hy = (y + fill_area->Y) % 8;
1068 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1069 argb_pixels[x + y*cdwStride] = fill->forecol;
1071 argb_pixels[x + y*cdwStride] = fill->backcol;
1076 case BrushTypeLinearGradient:
1078 GpLineGradient *fill = (GpLineGradient*)brush;
1079 GpPointF draw_points[3], line_points[3];
1081 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1082 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1085 draw_points[0].X = fill_area->X;
1086 draw_points[0].Y = fill_area->Y;
1087 draw_points[1].X = fill_area->X+1;
1088 draw_points[1].Y = fill_area->Y;
1089 draw_points[2].X = fill_area->X;
1090 draw_points[2].Y = fill_area->Y+1;
1092 /* Transform the points to a co-ordinate space where X is the point's
1093 * position in the gradient, 0.0 being the start point and 1.0 the
1095 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1096 CoordinateSpaceDevice, draw_points, 3);
1100 line_points[0] = fill->startpoint;
1101 line_points[1] = fill->endpoint;
1102 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1103 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1105 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1110 stat = GdipInvertMatrix(world_to_gradient);
1113 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1115 GdipDeleteMatrix(world_to_gradient);
1120 REAL x_delta = draw_points[1].X - draw_points[0].X;
1121 REAL y_delta = draw_points[2].X - draw_points[0].X;
1123 for (y=0; y<fill_area->Height; y++)
1125 for (x=0; x<fill_area->Width; x++)
1127 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1129 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1136 case BrushTypeTextureFill:
1138 GpTexture *fill = (GpTexture*)brush;
1139 GpPointF draw_points[3];
1146 if (fill->image->type != ImageTypeBitmap)
1148 FIXME("metafile texture brushes not implemented\n");
1149 return NotImplemented;
1152 bitmap = (GpBitmap*)fill->image;
1153 src_stride = sizeof(ARGB) * bitmap->width;
1155 src_area.X = src_area.Y = 0;
1156 src_area.Width = bitmap->width;
1157 src_area.Height = bitmap->height;
1159 draw_points[0].X = fill_area->X;
1160 draw_points[0].Y = fill_area->Y;
1161 draw_points[1].X = fill_area->X+1;
1162 draw_points[1].Y = fill_area->Y;
1163 draw_points[2].X = fill_area->X;
1164 draw_points[2].Y = fill_area->Y+1;
1166 /* Transform the points to the co-ordinate space of the bitmap. */
1167 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1168 CoordinateSpaceDevice, draw_points, 3);
1172 GpMatrix world_to_texture = fill->transform;
1174 stat = GdipInvertMatrix(&world_to_texture);
1176 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1179 if (stat == Ok && !fill->bitmap_bits)
1181 BitmapData lockeddata;
1183 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1184 if (!fill->bitmap_bits)
1189 lockeddata.Width = bitmap->width;
1190 lockeddata.Height = bitmap->height;
1191 lockeddata.Stride = src_stride;
1192 lockeddata.PixelFormat = PixelFormat32bppARGB;
1193 lockeddata.Scan0 = fill->bitmap_bits;
1195 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1196 PixelFormat32bppARGB, &lockeddata);
1200 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1203 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1204 bitmap->width, bitmap->height,
1205 src_stride, ColorAdjustTypeBitmap);
1209 GdipFree(fill->bitmap_bits);
1210 fill->bitmap_bits = NULL;
1216 REAL x_dx = draw_points[1].X - draw_points[0].X;
1217 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1218 REAL y_dx = draw_points[2].X - draw_points[0].X;
1219 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1221 for (y=0; y<fill_area->Height; y++)
1223 for (x=0; x<fill_area->Width; x++)
1226 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1227 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1229 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1230 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1231 &point, fill->imageattributes, graphics->interpolation,
1232 graphics->pixeloffset);
1239 case BrushTypePathGradient:
1241 GpPathGradient *fill = (GpPathGradient*)brush;
1243 GpMatrix world_to_device;
1245 int i, figure_start=0;
1246 GpPointF start_point, end_point, center_point;
1248 REAL min_yf, max_yf, line1_xf, line2_xf;
1249 INT min_y, max_y, min_x, max_x;
1252 static int transform_fixme_once;
1254 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1258 FIXME("path gradient focus not implemented\n");
1265 FIXME("path gradient gamma correction not implemented\n");
1268 if (fill->blendcount)
1272 FIXME("path gradient blend not implemented\n");
1275 if (fill->pblendcount)
1279 FIXME("path gradient preset blend not implemented\n");
1282 if (!transform_fixme_once)
1284 BOOL is_identity=TRUE;
1285 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1288 FIXME("path gradient transform not implemented\n");
1289 transform_fixme_once = 1;
1293 stat = GdipClonePath(fill->path, &flat_path);
1298 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1299 CoordinateSpaceWorld, &world_to_device);
1302 stat = GdipTransformPath(flat_path, &world_to_device);
1306 center_point = fill->center;
1307 stat = GdipTransformMatrixPoints(&world_to_device, ¢er_point, 1);
1311 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1316 GdipDeletePath(flat_path);
1320 for (i=0; i<flat_path->pathdata.Count; i++)
1322 int start_center_line=0, end_center_line=0;
1323 int seen_start=0, seen_end=0, seen_center=0;
1324 REAL center_distance;
1325 ARGB start_color, end_color;
1328 type = flat_path->pathdata.Types[i];
1330 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1333 start_point = flat_path->pathdata.Points[i];
1335 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1337 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1339 end_point = flat_path->pathdata.Points[figure_start];
1340 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1342 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1344 end_point = flat_path->pathdata.Points[i+1];
1345 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1350 outer_color = start_color;
1352 min_yf = center_point.Y;
1353 if (min_yf > start_point.Y) min_yf = start_point.Y;
1354 if (min_yf > end_point.Y) min_yf = end_point.Y;
1356 if (min_yf < fill_area->Y)
1357 min_y = fill_area->Y;
1359 min_y = (INT)ceil(min_yf);
1361 max_yf = center_point.Y;
1362 if (max_yf < start_point.Y) max_yf = start_point.Y;
1363 if (max_yf < end_point.Y) max_yf = end_point.Y;
1365 if (max_yf > fill_area->Y + fill_area->Height)
1366 max_y = fill_area->Y + fill_area->Height;
1368 max_y = (INT)ceil(max_yf);
1370 dy = end_point.Y - start_point.Y;
1371 dx = end_point.X - start_point.X;
1373 /* This is proportional to the distance from start-end line to center point. */
1374 center_distance = dy * (start_point.X - center_point.X) +
1375 dx * (center_point.Y - start_point.Y);
1377 for (y=min_y; y<max_y; y++)
1381 if (!seen_start && yf >= start_point.Y)
1384 start_center_line ^= 1;
1386 if (!seen_end && yf >= end_point.Y)
1389 end_center_line ^= 1;
1391 if (!seen_center && yf >= center_point.Y)
1394 start_center_line ^= 1;
1395 end_center_line ^= 1;
1398 if (start_center_line)
1399 line1_xf = intersect_line_scanline(&start_point, ¢er_point, yf);
1401 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1403 if (end_center_line)
1404 line2_xf = intersect_line_scanline(&end_point, ¢er_point, yf);
1406 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1408 if (line1_xf < line2_xf)
1410 min_x = (INT)ceil(line1_xf);
1411 max_x = (INT)ceil(line2_xf);
1415 min_x = (INT)ceil(line2_xf);
1416 max_x = (INT)ceil(line1_xf);
1419 if (min_x < fill_area->X)
1420 min_x = fill_area->X;
1421 if (max_x > fill_area->X + fill_area->Width)
1422 max_x = fill_area->X + fill_area->Width;
1424 for (x=min_x; x<max_x; x++)
1429 if (start_color != end_color)
1431 REAL blend_amount, pdy, pdx;
1432 pdy = yf - center_point.Y;
1433 pdx = xf - center_point.X;
1434 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1435 outer_color = blend_colors(start_color, end_color, blend_amount);
1438 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1439 (end_point.X - start_point.X) * (yf - start_point.Y);
1441 distance = distance / center_distance;
1443 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1444 blend_colors(outer_color, fill->centercolor, distance);
1449 GdipDeletePath(flat_path);
1453 return NotImplemented;
1457 /* GdipDrawPie/GdipFillPie helper function */
1458 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1459 REAL height, REAL startAngle, REAL sweepAngle)
1466 ptf[1].X = x + width;
1467 ptf[1].Y = y + height;
1469 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1470 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1472 transform_and_round_points(graphics, pti, ptf, 4);
1474 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1475 pti[2].y, pti[3].x, pti[3].y);
1478 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1479 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1480 * should not be called on an hdc that has a path you care about. */
1481 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1482 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1484 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1486 HBRUSH brush = NULL;
1488 PointF ptf[4], *custptf = NULL;
1489 POINT pt[4], *custpt = NULL;
1491 REAL theta, dsmall, dbig, dx, dy = 0.0;
1496 if((x1 == x2) && (y1 == y2))
1499 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1501 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1503 brush = CreateSolidBrush(color);
1504 lb.lbStyle = BS_SOLID;
1507 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1508 PS_JOIN_MITER, 1, &lb, 0,
1510 oldbrush = SelectObject(graphics->hdc, brush);
1511 oldpen = SelectObject(graphics->hdc, pen);
1518 case LineCapSquareAnchor:
1519 case LineCapDiamondAnchor:
1520 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1521 if(cap == LineCapDiamondAnchor){
1522 dsmall = cos(theta + M_PI_2) * size;
1523 dbig = sin(theta + M_PI_2) * size;
1526 dsmall = cos(theta + M_PI_4) * size;
1527 dbig = sin(theta + M_PI_4) * size;
1530 ptf[0].X = x2 - dsmall;
1531 ptf[1].X = x2 + dbig;
1533 ptf[0].Y = y2 - dbig;
1534 ptf[3].Y = y2 + dsmall;
1536 ptf[1].Y = y2 - dsmall;
1537 ptf[2].Y = y2 + dbig;
1539 ptf[3].X = x2 - dbig;
1540 ptf[2].X = x2 + dsmall;
1542 transform_and_round_points(graphics, pt, ptf, 4);
1543 Polygon(graphics->hdc, pt, 4);
1546 case LineCapArrowAnchor:
1547 size = size * 4.0 / sqrt(3.0);
1549 dx = cos(M_PI / 6.0 + theta) * size;
1550 dy = sin(M_PI / 6.0 + theta) * size;
1555 dx = cos(- M_PI / 6.0 + theta) * size;
1556 dy = sin(- M_PI / 6.0 + theta) * size;
1564 transform_and_round_points(graphics, pt, ptf, 3);
1565 Polygon(graphics->hdc, pt, 3);
1568 case LineCapRoundAnchor:
1569 dx = dy = ANCHOR_WIDTH * size / 2.0;
1576 transform_and_round_points(graphics, pt, ptf, 2);
1577 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1580 case LineCapTriangle:
1582 dx = cos(M_PI_2 + theta) * size;
1583 dy = sin(M_PI_2 + theta) * size;
1590 dx = cos(theta) * size;
1591 dy = sin(theta) * size;
1596 transform_and_round_points(graphics, pt, ptf, 3);
1597 Polygon(graphics->hdc, pt, 3);
1601 dx = dy = size / 2.0;
1608 dx = -cos(M_PI_2 + theta) * size;
1609 dy = -sin(M_PI_2 + theta) * size;
1616 transform_and_round_points(graphics, pt, ptf, 4);
1617 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1618 pt[2].y, pt[3].x, pt[3].y);
1625 count = custom->pathdata.Count;
1626 custptf = GdipAlloc(count * sizeof(PointF));
1627 custpt = GdipAlloc(count * sizeof(POINT));
1628 tp = GdipAlloc(count);
1630 if(!custptf || !custpt || !tp)
1633 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1635 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1636 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1637 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1639 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1640 GdipTransformMatrixPoints(&matrix, custptf, count);
1642 transform_and_round_points(graphics, custpt, custptf, count);
1644 for(i = 0; i < count; i++)
1645 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1648 BeginPath(graphics->hdc);
1649 PolyDraw(graphics->hdc, custpt, tp, count);
1650 EndPath(graphics->hdc);
1651 StrokeAndFillPath(graphics->hdc);
1654 PolyDraw(graphics->hdc, custpt, tp, count);
1666 SelectObject(graphics->hdc, oldbrush);
1667 SelectObject(graphics->hdc, oldpen);
1668 DeleteObject(brush);
1673 /* Shortens the line by the given percent by changing x2, y2.
1674 * If percent is > 1.0 then the line will change direction.
1675 * If percent is negative it can lengthen the line. */
1676 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1678 REAL dist, theta, dx, dy;
1680 if((y1 == *y2) && (x1 == *x2))
1683 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1684 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1685 dx = cos(theta) * dist;
1686 dy = sin(theta) * dist;
1692 /* Shortens the line by the given amount by changing x2, y2.
1693 * If the amount is greater than the distance, the line will become length 0.
1694 * If the amount is negative, it can lengthen the line. */
1695 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1697 REAL dx, dy, percent;
1701 if(dx == 0 && dy == 0)
1704 percent = amt / sqrt(dx * dx + dy * dy);
1711 shorten_line_percent(x1, y1, x2, y2, percent);
1714 /* Draws lines between the given points, and if caps is true then draws an endcap
1715 * at the end of the last line. */
1716 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1717 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1720 GpPointF *ptcopy = NULL;
1721 GpStatus status = GenericError;
1726 pti = GdipAlloc(count * sizeof(POINT));
1727 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1729 if(!pti || !ptcopy){
1730 status = OutOfMemory;
1734 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1737 if(pen->endcap == LineCapArrowAnchor)
1738 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1739 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1740 else if((pen->endcap == LineCapCustom) && pen->customend)
1741 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1742 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1743 pen->customend->inset * pen->width);
1745 if(pen->startcap == LineCapArrowAnchor)
1746 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1747 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1748 else if((pen->startcap == LineCapCustom) && pen->customstart)
1749 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1750 &ptcopy[0].X, &ptcopy[0].Y,
1751 pen->customstart->inset * pen->width);
1753 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1754 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1755 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1756 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1759 transform_and_round_points(graphics, pti, ptcopy, count);
1761 if(Polyline(graphics->hdc, pti, count))
1771 /* Conducts a linear search to find the bezier points that will back off
1772 * the endpoint of the curve by a distance of amt. Linear search works
1773 * better than binary in this case because there are multiple solutions,
1774 * and binary searches often find a bad one. I don't think this is what
1775 * Windows does but short of rendering the bezier without GDI's help it's
1776 * the best we can do. If rev then work from the start of the passed points
1777 * instead of the end. */
1778 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1781 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1782 INT i, first = 0, second = 1, third = 2, fourth = 3;
1791 origx = pt[fourth].X;
1792 origy = pt[fourth].Y;
1793 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1795 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1796 /* reset bezier points to original values */
1797 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1798 /* Perform magic on bezier points. Order is important here.*/
1799 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1800 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1801 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1802 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1803 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1804 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1806 dx = pt[fourth].X - origx;
1807 dy = pt[fourth].Y - origy;
1809 diff = sqrt(dx * dx + dy * dy);
1810 percent += 0.0005 * amt;
1814 /* Draws bezier curves between given points, and if caps is true then draws an
1815 * endcap at the end of the last line. */
1816 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1817 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1821 GpStatus status = GenericError;
1826 pti = GdipAlloc(count * sizeof(POINT));
1827 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1829 if(!pti || !ptcopy){
1830 status = OutOfMemory;
1834 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1837 if(pen->endcap == LineCapArrowAnchor)
1838 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1839 else if((pen->endcap == LineCapCustom) && pen->customend)
1840 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1843 if(pen->startcap == LineCapArrowAnchor)
1844 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1845 else if((pen->startcap == LineCapCustom) && pen->customstart)
1846 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1848 /* the direction of the line cap is parallel to the direction at the
1849 * end of the bezier (which, if it has been shortened, is not the same
1850 * as the direction from pt[count-2] to pt[count-1]) */
1851 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1852 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1853 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1854 pt[count - 1].X, pt[count - 1].Y);
1856 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1857 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1858 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1861 transform_and_round_points(graphics, pti, ptcopy, count);
1863 PolyBezier(graphics->hdc, pti, count);
1874 /* Draws a combination of bezier curves and lines between points. */
1875 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1876 GDIPCONST BYTE * types, INT count, BOOL caps)
1878 POINT *pti = GdipAlloc(count * sizeof(POINT));
1879 BYTE *tp = GdipAlloc(count);
1880 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1882 GpStatus status = GenericError;
1888 if(!pti || !tp || !ptcopy){
1889 status = OutOfMemory;
1893 for(i = 1; i < count; i++){
1894 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1895 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1896 || !(types[i + 1] & PathPointTypeBezier)){
1897 ERR("Bad bezier points\n");
1904 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1906 /* If we are drawing caps, go through the points and adjust them accordingly,
1907 * and draw the caps. */
1909 switch(types[count - 1] & PathPointTypePathTypeMask){
1910 case PathPointTypeBezier:
1911 if(pen->endcap == LineCapArrowAnchor)
1912 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1913 else if((pen->endcap == LineCapCustom) && pen->customend)
1914 shorten_bezier_amt(&ptcopy[count - 4],
1915 pen->width * pen->customend->inset, FALSE);
1917 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1918 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1919 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1920 pt[count - 1].X, pt[count - 1].Y);
1923 case PathPointTypeLine:
1924 if(pen->endcap == LineCapArrowAnchor)
1925 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1926 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1928 else if((pen->endcap == LineCapCustom) && pen->customend)
1929 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1930 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1931 pen->customend->inset * pen->width);
1933 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1934 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1939 ERR("Bad path last point\n");
1943 /* Find start of points */
1944 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1945 == PathPointTypeStart); j++);
1947 switch(types[j] & PathPointTypePathTypeMask){
1948 case PathPointTypeBezier:
1949 if(pen->startcap == LineCapArrowAnchor)
1950 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1951 else if((pen->startcap == LineCapCustom) && pen->customstart)
1952 shorten_bezier_amt(&ptcopy[j - 1],
1953 pen->width * pen->customstart->inset, TRUE);
1955 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1956 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1957 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1958 pt[j - 1].X, pt[j - 1].Y);
1961 case PathPointTypeLine:
1962 if(pen->startcap == LineCapArrowAnchor)
1963 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1964 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1966 else if((pen->startcap == LineCapCustom) && pen->customstart)
1967 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1968 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1969 pen->customstart->inset * pen->width);
1971 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1972 pt[j].X, pt[j].Y, pt[j - 1].X,
1977 ERR("Bad path points\n");
1982 transform_and_round_points(graphics, pti, ptcopy, count);
1984 for(i = 0; i < count; i++){
1985 tp[i] = convert_path_point_type(types[i]);
1988 PolyDraw(graphics->hdc, pti, tp, count);
2000 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
2004 BeginPath(graphics->hdc);
2005 result = draw_poly(graphics, NULL, path->pathdata.Points,
2006 path->pathdata.Types, path->pathdata.Count, FALSE);
2007 EndPath(graphics->hdc);
2011 typedef struct _GraphicsContainerItem {
2013 GraphicsContainer contid;
2015 SmoothingMode smoothing;
2016 CompositingQuality compqual;
2017 InterpolationMode interpolation;
2018 CompositingMode compmode;
2019 TextRenderingHint texthint;
2022 PixelOffsetMode pixeloffset;
2024 GpMatrix worldtrans;
2026 INT origin_x, origin_y;
2027 } GraphicsContainerItem;
2029 static GpStatus init_container(GraphicsContainerItem** container,
2030 GDIPCONST GpGraphics* graphics){
2033 *container = GdipAlloc(sizeof(GraphicsContainerItem));
2037 (*container)->contid = graphics->contid + 1;
2039 (*container)->smoothing = graphics->smoothing;
2040 (*container)->compqual = graphics->compqual;
2041 (*container)->interpolation = graphics->interpolation;
2042 (*container)->compmode = graphics->compmode;
2043 (*container)->texthint = graphics->texthint;
2044 (*container)->scale = graphics->scale;
2045 (*container)->unit = graphics->unit;
2046 (*container)->textcontrast = graphics->textcontrast;
2047 (*container)->pixeloffset = graphics->pixeloffset;
2048 (*container)->origin_x = graphics->origin_x;
2049 (*container)->origin_y = graphics->origin_y;
2050 (*container)->worldtrans = graphics->worldtrans;
2052 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2054 GdipFree(*container);
2062 static void delete_container(GraphicsContainerItem* container)
2064 GdipDeleteRegion(container->clip);
2065 GdipFree(container);
2068 static GpStatus restore_container(GpGraphics* graphics,
2069 GDIPCONST GraphicsContainerItem* container){
2073 sts = GdipCloneRegion(container->clip, &newClip);
2074 if(sts != Ok) return sts;
2076 graphics->worldtrans = container->worldtrans;
2078 GdipDeleteRegion(graphics->clip);
2079 graphics->clip = newClip;
2081 graphics->contid = container->contid - 1;
2083 graphics->smoothing = container->smoothing;
2084 graphics->compqual = container->compqual;
2085 graphics->interpolation = container->interpolation;
2086 graphics->compmode = container->compmode;
2087 graphics->texthint = container->texthint;
2088 graphics->scale = container->scale;
2089 graphics->unit = container->unit;
2090 graphics->textcontrast = container->textcontrast;
2091 graphics->pixeloffset = container->pixeloffset;
2092 graphics->origin_x = container->origin_x;
2093 graphics->origin_y = container->origin_y;
2098 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2104 if(graphics->hwnd) {
2105 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2106 return GenericError;
2108 rect->X = wnd_rect.left;
2109 rect->Y = wnd_rect.top;
2110 rect->Width = wnd_rect.right - wnd_rect.left;
2111 rect->Height = wnd_rect.bottom - wnd_rect.top;
2112 }else if (graphics->image){
2113 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2114 if (stat == Ok && unit != UnitPixel)
2115 FIXME("need to convert from unit %i\n", unit);
2116 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2123 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2124 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2126 rect->Width = bmp.bmWidth;
2127 rect->Height = bmp.bmHeight;
2138 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2139 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2145 /* on success, rgn will contain the region of the graphics object which
2146 * is visible after clipping has been applied */
2147 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2153 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2156 if((stat = GdipCreateRegion(&tmp)) != Ok)
2159 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2162 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2165 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2168 GdipDeleteRegion(tmp);
2172 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2176 if (font->unit == UnitPixel)
2178 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2182 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2183 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2185 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2188 lf->lfHeight = -(height + 0.5);
2190 lf->lfEscapement = 0;
2191 lf->lfOrientation = 0;
2192 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2193 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2194 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2195 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2196 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2197 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2198 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2199 lf->lfQuality = DEFAULT_QUALITY;
2200 lf->lfPitchAndFamily = 0;
2201 strcpyW(lf->lfFaceName, font->family->FamilyName);
2204 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2205 GDIPCONST GpStringFormat *format, HFONT *hfont,
2206 GDIPCONST GpMatrix *matrix)
2208 HDC hdc = CreateCompatibleDC(0);
2210 REAL angle, rel_width, rel_height, font_height;
2212 HFONT unscaled_font;
2213 TEXTMETRICW textmet;
2215 if (font->unit == UnitPixel)
2216 font_height = font->emSize;
2219 REAL unit_scale, res;
2221 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2222 unit_scale = units_scale(font->unit, graphics->unit, res);
2224 font_height = font->emSize * unit_scale;
2235 GpMatrix xform = *matrix;
2236 GdipTransformMatrixPoints(&xform, pt, 3);
2239 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2240 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2241 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2242 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2243 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2244 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2246 get_log_fontW(font, graphics, &lfw);
2247 lfw.lfHeight = gdip_round(font_height * rel_height);
2248 unscaled_font = CreateFontIndirectW(&lfw);
2250 SelectObject(hdc, unscaled_font);
2251 GetTextMetricsW(hdc, &textmet);
2253 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2254 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2256 *hfont = CreateFontIndirectW(&lfw);
2259 DeleteObject(unscaled_font);
2262 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2264 TRACE("(%p, %p)\n", hdc, graphics);
2266 return GdipCreateFromHDC2(hdc, NULL, graphics);
2269 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2275 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2277 if(hDevice != NULL) {
2278 FIXME("Don't know how to handle parameter hDevice\n");
2279 return NotImplemented;
2285 if(graphics == NULL)
2286 return InvalidParameter;
2288 *graphics = GdipAlloc(sizeof(GpGraphics));
2289 if(!*graphics) return OutOfMemory;
2291 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2293 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2294 GdipFree(*graphics);
2298 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2299 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2300 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2302 (*graphics)->alpha_hdc = 1;
2305 (*graphics)->hdc = hdc;
2306 (*graphics)->hwnd = WindowFromDC(hdc);
2307 (*graphics)->owndc = FALSE;
2308 (*graphics)->smoothing = SmoothingModeDefault;
2309 (*graphics)->compqual = CompositingQualityDefault;
2310 (*graphics)->interpolation = InterpolationModeBilinear;
2311 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2312 (*graphics)->compmode = CompositingModeSourceOver;
2313 (*graphics)->unit = UnitDisplay;
2314 (*graphics)->scale = 1.0;
2315 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2316 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2317 (*graphics)->busy = FALSE;
2318 (*graphics)->textcontrast = 4;
2319 list_init(&(*graphics)->containers);
2320 (*graphics)->contid = 0;
2322 TRACE("<-- %p\n", *graphics);
2327 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2331 *graphics = GdipAlloc(sizeof(GpGraphics));
2332 if(!*graphics) return OutOfMemory;
2334 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2336 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2337 GdipFree(*graphics);
2341 (*graphics)->hdc = NULL;
2342 (*graphics)->hwnd = NULL;
2343 (*graphics)->owndc = FALSE;
2344 (*graphics)->image = image;
2345 (*graphics)->smoothing = SmoothingModeDefault;
2346 (*graphics)->compqual = CompositingQualityDefault;
2347 (*graphics)->interpolation = InterpolationModeBilinear;
2348 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2349 (*graphics)->compmode = CompositingModeSourceOver;
2350 (*graphics)->unit = UnitDisplay;
2351 (*graphics)->scale = 1.0;
2352 (*graphics)->xres = image->xres;
2353 (*graphics)->yres = image->yres;
2354 (*graphics)->busy = FALSE;
2355 (*graphics)->textcontrast = 4;
2356 list_init(&(*graphics)->containers);
2357 (*graphics)->contid = 0;
2359 TRACE("<-- %p\n", *graphics);
2364 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2369 TRACE("(%p, %p)\n", hwnd, graphics);
2373 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2375 ReleaseDC(hwnd, hdc);
2379 (*graphics)->hwnd = hwnd;
2380 (*graphics)->owndc = TRUE;
2385 /* FIXME: no icm handling */
2386 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2388 TRACE("(%p, %p)\n", hwnd, graphics);
2390 return GdipCreateFromHWND(hwnd, graphics);
2393 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2394 GpMetafile **metafile)
2396 ENHMETAHEADER header;
2397 MetafileType metafile_type;
2399 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2401 if(!hemf || !metafile)
2402 return InvalidParameter;
2404 if (GetEnhMetaFileHeader(hemf, sizeof(header), &header) == 0)
2405 return GenericError;
2407 metafile_type = METAFILE_GetEmfType(hemf);
2409 if (metafile_type == MetafileTypeInvalid)
2410 return GenericError;
2412 *metafile = GdipAlloc(sizeof(GpMetafile));
2416 (*metafile)->image.type = ImageTypeMetafile;
2417 (*metafile)->image.format = ImageFormatEMF;
2418 (*metafile)->image.frame_count = 1;
2419 (*metafile)->image.xres = (REAL)header.szlDevice.cx;
2420 (*metafile)->image.yres = (REAL)header.szlDevice.cy;
2421 (*metafile)->bounds.X = (REAL)header.rclBounds.left;
2422 (*metafile)->bounds.Y = (REAL)header.rclBounds.top;
2423 (*metafile)->bounds.Width = (REAL)(header.rclBounds.right - header.rclBounds.left);
2424 (*metafile)->bounds.Height = (REAL)(header.rclBounds.bottom - header.rclBounds.top);
2425 (*metafile)->unit = UnitPixel;
2426 (*metafile)->metafile_type = metafile_type;
2427 (*metafile)->hemf = hemf;
2428 (*metafile)->preserve_hemf = !delete;
2430 TRACE("<-- %p\n", *metafile);
2435 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2436 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2441 GpStatus retval = Ok;
2443 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2445 if(!hwmf || !metafile || !placeable)
2446 return InvalidParameter;
2449 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2451 return GenericError;
2452 copy = GdipAlloc(read);
2453 GetMetaFileBitsEx(hwmf, read, copy);
2455 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2458 /* FIXME: We should store and use hwmf instead of converting to hemf */
2459 retval = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
2463 (*metafile)->image.xres = (REAL)placeable->Inch;
2464 (*metafile)->image.yres = (REAL)placeable->Inch;
2465 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2466 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2467 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2468 placeable->BoundingBox.Left);
2469 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2470 placeable->BoundingBox.Top);
2471 (*metafile)->metafile_type = MetafileTypeWmfPlaceable;
2472 (*metafile)->image.format = ImageFormatWMF;
2474 if (delete) DeleteMetaFile(hwmf);
2477 DeleteEnhMetaFile(hemf);
2481 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2482 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2484 HMETAFILE hmf = GetMetaFileW(file);
2486 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2488 if(!hmf) return InvalidParameter;
2490 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2493 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2494 GpMetafile **metafile)
2496 FIXME("(%p, %p): stub\n", file, metafile);
2497 return NotImplemented;
2500 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2501 GpMetafile **metafile)
2503 FIXME("(%p, %p): stub\n", stream, metafile);
2504 return NotImplemented;
2507 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2508 UINT access, IStream **stream)
2513 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2515 if(!stream || !filename)
2516 return InvalidParameter;
2518 if(access & GENERIC_WRITE)
2519 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2520 else if(access & GENERIC_READ)
2521 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2523 return InvalidParameter;
2525 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2527 return hresult_to_status(ret);
2530 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2532 GraphicsContainerItem *cont, *next;
2534 TRACE("(%p)\n", graphics);
2536 if(!graphics) return InvalidParameter;
2537 if(graphics->busy) return ObjectBusy;
2539 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2541 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2547 ReleaseDC(graphics->hwnd, graphics->hdc);
2549 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2550 list_remove(&cont->entry);
2551 delete_container(cont);
2554 GdipDeleteRegion(graphics->clip);
2560 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2561 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2563 INT save_state, num_pts;
2564 GpPointF points[MAX_ARC_PTS];
2567 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2568 width, height, startAngle, sweepAngle);
2570 if(!graphics || !pen || width <= 0 || height <= 0)
2571 return InvalidParameter;
2578 FIXME("graphics object has no HDC\n");
2582 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2584 save_state = prepare_dc(graphics, pen);
2586 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2588 restore_dc(graphics, save_state);
2593 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2594 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2596 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2597 width, height, startAngle, sweepAngle);
2599 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2602 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2603 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2609 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2610 x2, y2, x3, y3, x4, y4);
2612 if(!graphics || !pen)
2613 return InvalidParameter;
2620 FIXME("graphics object has no HDC\n");
2633 save_state = prepare_dc(graphics, pen);
2635 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2637 restore_dc(graphics, save_state);
2642 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2643 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2649 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2650 x2, y2, x3, y3, x4, y4);
2652 if(!graphics || !pen)
2653 return InvalidParameter;
2660 FIXME("graphics object has no HDC\n");
2673 save_state = prepare_dc(graphics, pen);
2675 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2677 restore_dc(graphics, save_state);
2682 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2683 GDIPCONST GpPointF *points, INT count)
2688 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2690 if(!graphics || !pen || !points || (count <= 0))
2691 return InvalidParameter;
2696 for(i = 0; i < floor(count / 4); i++){
2697 ret = GdipDrawBezier(graphics, pen,
2698 points[4*i].X, points[4*i].Y,
2699 points[4*i + 1].X, points[4*i + 1].Y,
2700 points[4*i + 2].X, points[4*i + 2].Y,
2701 points[4*i + 3].X, points[4*i + 3].Y);
2709 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2710 GDIPCONST GpPoint *points, INT count)
2716 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2718 if(!graphics || !pen || !points || (count <= 0))
2719 return InvalidParameter;
2724 pts = GdipAlloc(sizeof(GpPointF) * count);
2728 for(i = 0; i < count; i++){
2729 pts[i].X = (REAL)points[i].X;
2730 pts[i].Y = (REAL)points[i].Y;
2733 ret = GdipDrawBeziers(graphics,pen,pts,count);
2740 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2741 GDIPCONST GpPointF *points, INT count)
2743 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2745 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2748 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2749 GDIPCONST GpPoint *points, INT count)
2751 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2753 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2756 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2757 GDIPCONST GpPointF *points, INT count, REAL tension)
2762 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2764 if(!graphics || !pen || !points || count <= 0)
2765 return InvalidParameter;
2770 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2773 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2775 GdipDeletePath(path);
2779 stat = GdipDrawPath(graphics, pen, path);
2781 GdipDeletePath(path);
2786 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2787 GDIPCONST GpPoint *points, INT count, REAL tension)
2793 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2795 if(!points || count <= 0)
2796 return InvalidParameter;
2798 ptf = GdipAlloc(sizeof(GpPointF)*count);
2802 for(i = 0; i < count; i++){
2803 ptf[i].X = (REAL)points[i].X;
2804 ptf[i].Y = (REAL)points[i].Y;
2807 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2814 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2815 GDIPCONST GpPointF *points, INT count)
2817 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2819 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2822 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2823 GDIPCONST GpPoint *points, INT count)
2829 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2832 return InvalidParameter;
2834 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2838 for(i = 0; i < count; i++){
2839 pointsF[i].X = (REAL)points[i].X;
2840 pointsF[i].Y = (REAL)points[i].Y;
2843 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2849 /* Approximates cardinal spline with Bezier curves. */
2850 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2851 GDIPCONST GpPointF *points, INT count, REAL tension)
2853 /* PolyBezier expects count*3-2 points. */
2854 INT i, len_pt = count*3-2, save_state;
2856 REAL x1, x2, y1, y2;
2859 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2861 if(!graphics || !pen)
2862 return InvalidParameter;
2868 return InvalidParameter;
2872 FIXME("graphics object has no HDC\n");
2876 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2880 tension = tension * TENSION_CONST;
2882 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2885 pt[0].X = points[0].X;
2886 pt[0].Y = points[0].Y;
2890 for(i = 0; i < count-2; i++){
2891 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2895 pt[3*i+3].X = points[i+1].X;
2896 pt[3*i+3].Y = points[i+1].Y;
2901 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2902 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2904 pt[len_pt-2].X = x1;
2905 pt[len_pt-2].Y = y1;
2906 pt[len_pt-1].X = points[count-1].X;
2907 pt[len_pt-1].Y = points[count-1].Y;
2909 save_state = prepare_dc(graphics, pen);
2911 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2914 restore_dc(graphics, save_state);
2919 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2920 GDIPCONST GpPoint *points, INT count, REAL tension)
2926 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2929 return InvalidParameter;
2931 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2935 for(i = 0; i < count; i++){
2936 pointsF[i].X = (REAL)points[i].X;
2937 pointsF[i].Y = (REAL)points[i].Y;
2940 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2946 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2947 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2950 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2952 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2953 return InvalidParameter;
2956 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2959 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2960 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2963 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2969 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2970 return InvalidParameter;
2973 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2976 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2977 REAL y, REAL width, REAL height)
2983 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2985 if(!graphics || !pen)
2986 return InvalidParameter;
2993 FIXME("graphics object has no HDC\n");
2999 ptf[1].X = x + width;
3000 ptf[1].Y = y + height;
3002 save_state = prepare_dc(graphics, pen);
3003 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3005 transform_and_round_points(graphics, pti, ptf, 2);
3007 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
3009 restore_dc(graphics, save_state);
3014 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
3015 INT y, INT width, INT height)
3017 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3019 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3023 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
3027 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
3029 if(!graphics || !image)
3030 return InvalidParameter;
3032 GdipGetImageWidth(image, &width);
3033 GdipGetImageHeight(image, &height);
3035 return GdipDrawImagePointRect(graphics, image, x, y,
3036 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
3039 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
3042 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
3044 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
3047 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
3048 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
3052 REAL scale_x, scale_y, width, height;
3054 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3056 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
3057 scale_x *= graphics->xres / image->xres;
3058 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
3059 scale_y *= graphics->yres / image->yres;
3060 width = srcwidth * scale_x;
3061 height = srcheight * scale_y;
3063 points[0].X = points[2].X = x;
3064 points[0].Y = points[1].Y = y;
3065 points[1].X = x + width;
3066 points[2].Y = y + height;
3068 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3069 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3072 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3073 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3076 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3079 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3080 GDIPCONST GpPointF *dstpoints, INT count)
3084 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3087 return InvalidParameter;
3089 GdipGetImageWidth(image, &width);
3090 GdipGetImageHeight(image, &height);
3092 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3093 width, height, UnitPixel, NULL, NULL, NULL);
3096 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3097 GDIPCONST GpPoint *dstpoints, INT count)
3101 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3103 if (count != 3 || !dstpoints)
3104 return InvalidParameter;
3106 ptf[0].X = (REAL)dstpoints[0].X;
3107 ptf[0].Y = (REAL)dstpoints[0].Y;
3108 ptf[1].X = (REAL)dstpoints[1].X;
3109 ptf[1].Y = (REAL)dstpoints[1].Y;
3110 ptf[2].X = (REAL)dstpoints[2].X;
3111 ptf[2].Y = (REAL)dstpoints[2].Y;
3113 return GdipDrawImagePoints(graphics, image, ptf, count);
3116 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
3117 unsigned int dataSize, const unsigned char *pStr, void *userdata)
3119 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
3123 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3124 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3125 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3126 DrawImageAbort callback, VOID * callbackData)
3132 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3133 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3137 return NotImplemented;
3139 if(!graphics || !image || !points || count != 3)
3140 return InvalidParameter;
3142 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3143 debugstr_pointf(&points[2]));
3145 memcpy(ptf, points, 3 * sizeof(GpPointF));
3146 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3147 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3148 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3150 transform_and_round_points(graphics, pti, ptf, 4);
3152 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3153 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3155 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3156 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3157 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3158 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3159 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3165 FIXME("graphics object has no HDC\n");
3168 if(IPicture_Render(image->picture, graphics->hdc,
3169 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3170 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3173 callback(callbackData);
3174 return GenericError;
3177 else if (image->type == ImageTypeBitmap)
3179 GpBitmap* bitmap = (GpBitmap*)image;
3182 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3183 graphics->xres, graphics->yres,
3184 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3185 graphics->scale, image->xres, image->yres, bitmap->format,
3186 imageAttributes ? imageAttributes->outside_color : 0);
3188 if (imageAttributes || graphics->alpha_hdc ||
3189 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3190 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3191 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3192 srcx < 0 || srcy < 0 ||
3193 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3200 int i, x, y, src_stride, dst_stride;
3201 GpMatrix dst_to_src;
3202 REAL m11, m12, m21, m22, mdx, mdy;
3203 LPBYTE src_data, dst_data;
3204 BitmapData lockeddata;
3205 InterpolationMode interpolation = graphics->interpolation;
3206 PixelOffsetMode offset_mode = graphics->pixeloffset;
3207 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3208 REAL x_dx, x_dy, y_dx, y_dy;
3209 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3211 if (!imageAttributes)
3212 imageAttributes = &defaultImageAttributes;
3214 dst_area.left = dst_area.right = pti[0].x;
3215 dst_area.top = dst_area.bottom = pti[0].y;
3218 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3219 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3220 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3221 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3224 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3226 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3227 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3228 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3229 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3230 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3231 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3233 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
3235 stat = GdipInvertMatrix(&dst_to_src);
3236 if (stat != Ok) return stat;
3238 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3239 if (!dst_data) return OutOfMemory;
3241 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3243 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3244 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3246 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3248 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3254 src_stride = sizeof(ARGB) * src_area.Width;
3256 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3257 lockeddata.Width = src_area.Width;
3258 lockeddata.Height = src_area.Height;
3259 lockeddata.Stride = src_stride;
3260 lockeddata.PixelFormat = PixelFormat32bppARGB;
3261 lockeddata.Scan0 = src_data;
3263 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3264 PixelFormat32bppARGB, &lockeddata);
3267 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3271 if (src_data != dst_data)
3277 apply_image_attributes(imageAttributes, src_data,
3278 src_area.Width, src_area.Height,
3279 src_stride, ColorAdjustTypeBitmap);
3281 /* Transform the bits as needed to the destination. */
3282 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3284 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3285 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3286 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3287 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3289 for (x=dst_area.left; x<dst_area.right; x++)
3291 for (y=dst_area.top; y<dst_area.bottom; y++)
3293 GpPointF src_pointf;
3296 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3297 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3299 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3301 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3302 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3303 imageAttributes, interpolation, offset_mode);
3311 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3312 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3321 int temp_hdc=0, temp_bitmap=0;
3322 HBITMAP hbitmap, old_hbm=NULL;
3324 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3325 bitmap->format == PixelFormat24bppRGB ||
3326 bitmap->format == PixelFormat32bppRGB ||
3327 bitmap->format == PixelFormat32bppPARGB))
3329 BITMAPINFOHEADER bih;
3331 PixelFormat dst_format;
3333 /* we can't draw a bitmap of this format directly */
3334 hdc = CreateCompatibleDC(0);
3338 bih.biSize = sizeof(BITMAPINFOHEADER);
3339 bih.biWidth = bitmap->width;
3340 bih.biHeight = -bitmap->height;
3342 bih.biBitCount = 32;
3343 bih.biCompression = BI_RGB;
3344 bih.biSizeImage = 0;
3345 bih.biXPelsPerMeter = 0;
3346 bih.biYPelsPerMeter = 0;
3348 bih.biClrImportant = 0;
3350 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3351 (void**)&temp_bits, NULL, 0);
3353 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3354 dst_format = PixelFormat32bppPARGB;
3356 dst_format = PixelFormat32bppRGB;
3358 convert_pixels(bitmap->width, bitmap->height,
3359 bitmap->width*4, temp_bits, dst_format,
3360 bitmap->stride, bitmap->bits, bitmap->format,
3361 bitmap->image.palette);
3365 if (bitmap->hbitmap)
3366 hbitmap = bitmap->hbitmap;
3369 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3374 temp_hdc = (hdc == 0);
3379 if (!hdc) hdc = CreateCompatibleDC(0);
3380 old_hbm = SelectObject(hdc, hbitmap);
3383 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3385 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3386 hdc, srcx, srcy, srcwidth, srcheight);
3390 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3391 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3396 SelectObject(hdc, old_hbm);
3401 DeleteObject(hbitmap);
3404 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3410 rc.Width = srcwidth;
3411 rc.Height = srcheight;
3413 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3414 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3418 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3419 return InvalidParameter;
3425 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3426 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3427 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3428 DrawImageAbort callback, VOID * callbackData)
3430 GpPointF pointsF[3];
3433 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3434 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3437 if(!points || count!=3)
3438 return InvalidParameter;
3440 for(i = 0; i < count; i++){
3441 pointsF[i].X = (REAL)points[i].X;
3442 pointsF[i].Y = (REAL)points[i].Y;
3445 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3446 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3447 callback, callbackData);
3450 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3451 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3452 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3453 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3454 VOID * callbackData)
3458 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3459 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3460 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3464 points[1].X = dstx + dstwidth;
3467 points[2].Y = dsty + dstheight;
3469 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3470 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3473 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3474 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3475 INT srcwidth, INT srcheight, GpUnit srcUnit,
3476 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3477 VOID * callbackData)
3481 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3482 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3483 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3487 points[1].X = dstx + dstwidth;
3490 points[2].Y = dsty + dstheight;
3492 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3493 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3496 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3497 REAL x, REAL y, REAL width, REAL height)
3503 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3505 if(!graphics || !image)
3506 return InvalidParameter;
3508 ret = GdipGetImageBounds(image, &bounds, &unit);
3512 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3513 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3514 unit, NULL, NULL, NULL);
3517 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3518 INT x, INT y, INT width, INT height)
3520 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3522 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3525 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3526 REAL y1, REAL x2, REAL y2)
3532 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3534 if(!pen || !graphics)
3535 return InvalidParameter;
3542 FIXME("graphics object has no HDC\n");
3551 save_state = prepare_dc(graphics, pen);
3553 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3555 restore_dc(graphics, save_state);
3560 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3561 INT y1, INT x2, INT y2)
3567 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3569 if(!pen || !graphics)
3570 return InvalidParameter;
3577 FIXME("graphics object has no HDC\n");
3586 save_state = prepare_dc(graphics, pen);
3588 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3590 restore_dc(graphics, save_state);
3595 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3596 GpPointF *points, INT count)
3601 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3603 if(!pen || !graphics || (count < 2))
3604 return InvalidParameter;
3611 FIXME("graphics object has no HDC\n");
3615 save_state = prepare_dc(graphics, pen);
3617 retval = draw_polyline(graphics, pen, points, count, TRUE);
3619 restore_dc(graphics, save_state);
3624 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3625 GpPoint *points, INT count)
3629 GpPointF *ptf = NULL;
3632 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3634 if(!pen || !graphics || (count < 2))
3635 return InvalidParameter;
3642 FIXME("graphics object has no HDC\n");
3646 ptf = GdipAlloc(count * sizeof(GpPointF));
3647 if(!ptf) return OutOfMemory;
3649 for(i = 0; i < count; i ++){
3650 ptf[i].X = (REAL) points[i].X;
3651 ptf[i].Y = (REAL) points[i].Y;
3654 save_state = prepare_dc(graphics, pen);
3656 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3658 restore_dc(graphics, save_state);
3664 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3669 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3671 if(!pen || !graphics)
3672 return InvalidParameter;
3679 FIXME("graphics object has no HDC\n");
3683 save_state = prepare_dc(graphics, pen);
3685 retval = draw_poly(graphics, pen, path->pathdata.Points,
3686 path->pathdata.Types, path->pathdata.Count, TRUE);
3688 restore_dc(graphics, save_state);
3693 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3694 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3698 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3699 width, height, startAngle, sweepAngle);
3701 if(!graphics || !pen)
3702 return InvalidParameter;
3709 FIXME("graphics object has no HDC\n");
3713 save_state = prepare_dc(graphics, pen);
3714 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3716 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3718 restore_dc(graphics, save_state);
3723 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3724 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3726 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3727 width, height, startAngle, sweepAngle);
3729 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3732 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3733 REAL y, REAL width, REAL height)
3739 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3741 if(!pen || !graphics)
3742 return InvalidParameter;
3749 FIXME("graphics object has no HDC\n");
3755 ptf[1].X = x + width;
3757 ptf[2].X = x + width;
3758 ptf[2].Y = y + height;
3760 ptf[3].Y = y + height;
3762 save_state = prepare_dc(graphics, pen);
3763 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3765 transform_and_round_points(graphics, pti, ptf, 4);
3766 Polygon(graphics->hdc, pti, 4);
3768 restore_dc(graphics, save_state);
3773 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3774 INT y, INT width, INT height)
3776 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3778 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3781 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3782 GDIPCONST GpRectF* rects, INT count)
3788 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3790 if(!graphics || !pen || !rects || count < 1)
3791 return InvalidParameter;
3798 FIXME("graphics object has no HDC\n");
3802 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3803 pti = GdipAlloc(4 * count * sizeof(POINT));
3811 for(i = 0; i < count; i++){
3812 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3813 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3814 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3815 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3818 save_state = prepare_dc(graphics, pen);
3819 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3821 transform_and_round_points(graphics, pti, ptf, 4 * count);
3823 for(i = 0; i < count; i++)
3824 Polygon(graphics->hdc, &pti[4 * i], 4);
3826 restore_dc(graphics, save_state);
3834 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3835 GDIPCONST GpRect* rects, INT count)
3841 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3843 if(!rects || count<=0)
3844 return InvalidParameter;
3846 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3850 for(i = 0;i < count;i++){
3851 rectsF[i].X = (REAL)rects[i].X;
3852 rectsF[i].Y = (REAL)rects[i].Y;
3853 rectsF[i].Width = (REAL)rects[i].Width;
3854 rectsF[i].Height = (REAL)rects[i].Height;
3857 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3863 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3864 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3869 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3870 count, tension, fill);
3872 if(!graphics || !brush || !points)
3873 return InvalidParameter;
3878 if(count == 1) /* Do nothing */
3881 stat = GdipCreatePath(fill, &path);
3885 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3887 GdipDeletePath(path);
3891 stat = GdipFillPath(graphics, brush, path);
3893 GdipDeletePath(path);
3897 GdipDeletePath(path);
3902 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3903 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3909 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3910 count, tension, fill);
3912 if(!points || count == 0)
3913 return InvalidParameter;
3915 if(count == 1) /* Do nothing */
3918 ptf = GdipAlloc(sizeof(GpPointF)*count);
3922 for(i = 0;i < count;i++){
3923 ptf[i].X = (REAL)points[i].X;
3924 ptf[i].Y = (REAL)points[i].Y;
3927 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3934 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3935 GDIPCONST GpPointF *points, INT count)
3937 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3938 return GdipFillClosedCurve2(graphics, brush, points, count,
3939 0.5f, FillModeAlternate);
3942 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3943 GDIPCONST GpPoint *points, INT count)
3945 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3946 return GdipFillClosedCurve2I(graphics, brush, points, count,
3947 0.5f, FillModeAlternate);
3950 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3951 REAL y, REAL width, REAL height)
3956 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3958 if(!graphics || !brush)
3959 return InvalidParameter;
3964 stat = GdipCreatePath(FillModeAlternate, &path);
3968 stat = GdipAddPathEllipse(path, x, y, width, height);
3971 stat = GdipFillPath(graphics, brush, path);
3973 GdipDeletePath(path);
3979 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3980 INT y, INT width, INT height)
3982 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3984 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3987 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3992 if(!graphics->hdc || !brush_can_fill_path(brush))
3993 return NotImplemented;
3995 save_state = SaveDC(graphics->hdc);
3996 EndPath(graphics->hdc);
3997 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
4000 BeginPath(graphics->hdc);
4001 retval = draw_poly(graphics, NULL, path->pathdata.Points,
4002 path->pathdata.Types, path->pathdata.Count, FALSE);
4007 EndPath(graphics->hdc);
4008 brush_fill_path(graphics, brush);
4013 RestoreDC(graphics->hdc, save_state);
4018 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4023 if (!brush_can_fill_pixels(brush))
4024 return NotImplemented;
4026 /* FIXME: This could probably be done more efficiently without regions. */
4028 stat = GdipCreateRegionPath(path, &rgn);
4032 stat = GdipFillRegion(graphics, brush, rgn);
4034 GdipDeleteRegion(rgn);
4040 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4042 GpStatus stat = NotImplemented;
4044 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4046 if(!brush || !graphics || !path)
4047 return InvalidParameter;
4052 if (!graphics->image && !graphics->alpha_hdc)
4053 stat = GDI32_GdipFillPath(graphics, brush, path);
4055 if (stat == NotImplemented)
4056 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4058 if (stat == NotImplemented)
4060 FIXME("Not implemented for brushtype %i\n", brush->bt);
4067 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4068 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4073 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4074 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4076 if(!graphics || !brush)
4077 return InvalidParameter;
4082 stat = GdipCreatePath(FillModeAlternate, &path);
4086 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4089 stat = GdipFillPath(graphics, brush, path);
4091 GdipDeletePath(path);
4097 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4098 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4100 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4101 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4103 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4106 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4107 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4112 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4114 if(!graphics || !brush || !points || !count)
4115 return InvalidParameter;
4120 stat = GdipCreatePath(fillMode, &path);
4124 stat = GdipAddPathPolygon(path, points, count);
4127 stat = GdipFillPath(graphics, brush, path);
4129 GdipDeletePath(path);
4135 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4136 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4141 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4143 if(!graphics || !brush || !points || !count)
4144 return InvalidParameter;
4149 stat = GdipCreatePath(fillMode, &path);
4153 stat = GdipAddPathPolygonI(path, points, count);
4156 stat = GdipFillPath(graphics, brush, path);
4158 GdipDeletePath(path);
4164 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4165 GDIPCONST GpPointF *points, INT count)
4167 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4169 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4172 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4173 GDIPCONST GpPoint *points, INT count)
4175 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4177 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4180 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4181 REAL x, REAL y, REAL width, REAL height)
4186 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4188 if(!graphics || !brush)
4189 return InvalidParameter;
4194 stat = GdipCreatePath(FillModeAlternate, &path);
4198 stat = GdipAddPathRectangle(path, x, y, width, height);
4201 stat = GdipFillPath(graphics, brush, path);
4203 GdipDeletePath(path);
4209 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4210 INT x, INT y, INT width, INT height)
4212 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4214 return GdipFillRectangle(graphics, brush, x, y, width, height);
4217 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4223 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4226 return InvalidParameter;
4228 for(i = 0; i < count; i++){
4229 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4230 if(ret != Ok) return ret;
4236 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4243 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4245 if(!rects || count <= 0)
4246 return InvalidParameter;
4248 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4252 for(i = 0; i < count; i++){
4253 rectsF[i].X = (REAL)rects[i].X;
4254 rectsF[i].Y = (REAL)rects[i].Y;
4255 rectsF[i].X = (REAL)rects[i].Width;
4256 rectsF[i].Height = (REAL)rects[i].Height;
4259 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4265 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4273 if(!graphics->hdc || !brush_can_fill_path(brush))
4274 return NotImplemented;
4276 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4280 save_state = SaveDC(graphics->hdc);
4281 EndPath(graphics->hdc);
4283 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4285 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4287 BeginPath(graphics->hdc);
4288 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4289 EndPath(graphics->hdc);
4291 brush_fill_path(graphics, brush);
4294 RestoreDC(graphics->hdc, save_state);
4301 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4305 GpRegion *temp_region;
4306 GpMatrix world_to_device;
4307 GpRectF graphics_bounds;
4311 GpRect gp_bound_rect;
4313 if (!brush_can_fill_pixels(brush))
4314 return NotImplemented;
4316 stat = get_graphics_bounds(graphics, &graphics_bounds);
4319 stat = GdipCloneRegion(region, &temp_region);
4323 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4324 CoordinateSpaceWorld, &world_to_device);
4327 stat = GdipTransformRegion(temp_region, &world_to_device);
4330 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4333 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4335 GdipDeleteRegion(temp_region);
4338 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4340 DeleteObject(hregion);
4346 gp_bound_rect.X = bound_rect.left;
4347 gp_bound_rect.Y = bound_rect.top;
4348 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4349 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4351 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4357 stat = brush_fill_pixels(graphics, brush, pixel_data,
4358 &gp_bound_rect, gp_bound_rect.Width);
4361 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4362 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4363 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4365 GdipFree(pixel_data);
4368 DeleteObject(hregion);
4374 /*****************************************************************************
4375 * GdipFillRegion [GDIPLUS.@]
4377 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4380 GpStatus stat = NotImplemented;
4382 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4384 if (!(graphics && brush && region))
4385 return InvalidParameter;
4390 if (!graphics->image && !graphics->alpha_hdc)
4391 stat = GDI32_GdipFillRegion(graphics, brush, region);
4393 if (stat == NotImplemented)
4394 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4396 if (stat == NotImplemented)
4398 FIXME("not implemented for brushtype %i\n", brush->bt);
4405 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4407 TRACE("(%p,%u)\n", graphics, intention);
4410 return InvalidParameter;
4415 /* We have no internal operation queue, so there's no need to clear it. */
4423 /*****************************************************************************
4424 * GdipGetClipBounds [GDIPLUS.@]
4426 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4428 TRACE("(%p, %p)\n", graphics, rect);
4431 return InvalidParameter;
4436 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4439 /*****************************************************************************
4440 * GdipGetClipBoundsI [GDIPLUS.@]
4442 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4444 TRACE("(%p, %p)\n", graphics, rect);
4447 return InvalidParameter;
4452 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4455 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4456 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4457 CompositingMode *mode)
4459 TRACE("(%p, %p)\n", graphics, mode);
4461 if(!graphics || !mode)
4462 return InvalidParameter;
4467 *mode = graphics->compmode;
4472 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4473 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4474 CompositingQuality *quality)
4476 TRACE("(%p, %p)\n", graphics, quality);
4478 if(!graphics || !quality)
4479 return InvalidParameter;
4484 *quality = graphics->compqual;
4489 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4490 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4491 InterpolationMode *mode)
4493 TRACE("(%p, %p)\n", graphics, mode);
4495 if(!graphics || !mode)
4496 return InvalidParameter;
4501 *mode = graphics->interpolation;
4506 /* FIXME: Need to handle color depths less than 24bpp */
4507 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4509 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4511 if(!graphics || !argb)
4512 return InvalidParameter;
4520 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4522 TRACE("(%p, %p)\n", graphics, scale);
4524 if(!graphics || !scale)
4525 return InvalidParameter;
4530 *scale = graphics->scale;
4535 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4537 TRACE("(%p, %p)\n", graphics, unit);
4539 if(!graphics || !unit)
4540 return InvalidParameter;
4545 *unit = graphics->unit;
4550 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4551 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4554 TRACE("(%p, %p)\n", graphics, mode);
4556 if(!graphics || !mode)
4557 return InvalidParameter;
4562 *mode = graphics->pixeloffset;
4567 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4568 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4570 TRACE("(%p, %p)\n", graphics, mode);
4572 if(!graphics || !mode)
4573 return InvalidParameter;
4578 *mode = graphics->smoothing;
4583 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4585 TRACE("(%p, %p)\n", graphics, contrast);
4587 if(!graphics || !contrast)
4588 return InvalidParameter;
4590 *contrast = graphics->textcontrast;
4595 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4596 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4597 TextRenderingHint *hint)
4599 TRACE("(%p, %p)\n", graphics, hint);
4601 if(!graphics || !hint)
4602 return InvalidParameter;
4607 *hint = graphics->texthint;
4612 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4617 TRACE("(%p, %p)\n", graphics, rect);
4619 if(!graphics || !rect)
4620 return InvalidParameter;
4625 /* intersect window and graphics clipping regions */
4626 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4629 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4632 /* get bounds of the region */
4633 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4636 GdipDeleteRegion(clip_rgn);
4641 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4646 TRACE("(%p, %p)\n", graphics, rect);
4648 if(!graphics || !rect)
4649 return InvalidParameter;
4651 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4653 rect->X = gdip_round(rectf.X);
4654 rect->Y = gdip_round(rectf.Y);
4655 rect->Width = gdip_round(rectf.Width);
4656 rect->Height = gdip_round(rectf.Height);
4662 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4664 TRACE("(%p, %p)\n", graphics, matrix);
4666 if(!graphics || !matrix)
4667 return InvalidParameter;
4672 *matrix = graphics->worldtrans;
4676 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4682 TRACE("(%p, %x)\n", graphics, color);
4685 return InvalidParameter;
4690 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4693 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4694 GdipDeleteBrush((GpBrush*)brush);
4698 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4699 wnd_rect.Width, wnd_rect.Height);
4701 GdipDeleteBrush((GpBrush*)brush);
4706 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4708 TRACE("(%p, %p)\n", graphics, res);
4710 if(!graphics || !res)
4711 return InvalidParameter;
4713 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4716 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4722 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4724 if(!graphics || !result)
4725 return InvalidParameter;
4732 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4733 CoordinateSpaceWorld, &pt, 1)) != Ok)
4736 if((stat = GdipCreateRegion(&rgn)) != Ok)
4739 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4742 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4745 GdipDeleteRegion(rgn);
4749 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4751 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4754 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4760 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4762 if(!graphics || !result)
4763 return InvalidParameter;
4770 pts[1].X = x + width;
4771 pts[1].Y = y + height;
4773 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4774 CoordinateSpaceWorld, pts, 2)) != Ok)
4777 pts[1].X -= pts[0].X;
4778 pts[1].Y -= pts[0].Y;
4780 if((stat = GdipCreateRegion(&rgn)) != Ok)
4783 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4786 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4789 GdipDeleteRegion(rgn);
4793 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4795 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4798 GpStatus gdip_format_string(HDC hdc,
4799 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4800 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4801 gdip_format_string_callback callback, void *user_data)
4804 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4805 nheight, lineend, lineno = 0;
4807 StringAlignment halign;
4810 HotkeyPrefix hkprefix;
4811 INT *hotkeyprefix_offsets=NULL;
4812 INT hotkeyprefix_count=0;
4813 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4816 if(length == -1) length = lstrlenW(string);
4818 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4819 if(!stringdup) return OutOfMemory;
4821 nwidth = rect->Width;
4822 nheight = rect->Height;
4825 hkprefix = format->hkprefix;
4827 hkprefix = HotkeyPrefixNone;
4829 if (hkprefix == HotkeyPrefixShow)
4831 for (i=0; i<length; i++)
4833 if (string[i] == '&')
4834 hotkeyprefix_count++;
4838 if (hotkeyprefix_count)
4839 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4841 hotkeyprefix_count = 0;
4843 for(i = 0, j = 0; i < length; i++){
4844 /* FIXME: This makes the indexes passed to callback inaccurate. */
4845 if(!isprintW(string[i]) && (string[i] != '\n'))
4848 /* FIXME: tabs should be handled using tabstops from stringformat */
4849 if (string[i] == '\t')
4852 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4853 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4854 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4862 stringdup[j] = string[i];
4868 if (format) halign = format->align;
4869 else halign = StringAlignmentNear;
4871 while(sum < length){
4872 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4873 nwidth, &fit, NULL, &size);
4879 for(lret = 0; lret < fit; lret++)
4880 if(*(stringdup + sum + lret) == '\n')
4883 /* Line break code (may look strange, but it imitates windows). */
4885 lineend = fit = lret; /* this is not an off-by-one error */
4886 else if(fit < (length - sum)){
4887 if(*(stringdup + sum + fit) == ' ')
4888 while(*(stringdup + sum + fit) == ' ')
4891 while(*(stringdup + sum + fit - 1) != ' '){
4894 if(*(stringdup + sum + fit) == '\t')
4903 while(*(stringdup + sum + lineend - 1) == ' ' ||
4904 *(stringdup + sum + lineend - 1) == '\t')
4910 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4911 nwidth, &j, NULL, &size);
4913 bounds.Width = size.cx;
4915 if(height + size.cy > nheight)
4916 bounds.Height = nheight - (height + size.cy);
4918 bounds.Height = size.cy;
4920 bounds.Y = rect->Y + height;
4924 case StringAlignmentNear:
4928 case StringAlignmentCenter:
4929 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4931 case StringAlignmentFar:
4932 bounds.X = rect->X + rect->Width - bounds.Width;
4936 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4937 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4940 stat = callback(hdc, stringdup, sum, lineend,
4941 font, rect, format, lineno, &bounds,
4942 &hotkeyprefix_offsets[hotkeyprefix_pos],
4943 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4948 sum += fit + (lret < fitcpy ? 1 : 0);
4952 hotkeyprefix_pos = hotkeyprefix_end_pos;
4954 if(height > nheight)
4957 /* Stop if this was a linewrap (but not if it was a linebreak). */
4958 if ((lret == fitcpy) && format &&
4959 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4963 GdipFree(stringdup);
4964 GdipFree(hotkeyprefix_offsets);
4969 struct measure_ranges_args {
4971 REAL rel_width, rel_height;
4974 static GpStatus measure_ranges_callback(HDC hdc,
4975 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4976 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4977 INT lineno, const RectF *bounds, INT *underlined_indexes,
4978 INT underlined_index_count, void *user_data)
4982 struct measure_ranges_args *args = user_data;
4984 for (i=0; i<format->range_count; i++)
4986 INT range_start = max(index, format->character_ranges[i].First);
4987 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4988 if (range_start < range_end)
4993 range_rect.Y = bounds->Y / args->rel_height;
4994 range_rect.Height = bounds->Height / args->rel_height;
4996 GetTextExtentExPointW(hdc, string + index, range_start - index,
4997 INT_MAX, NULL, NULL, &range_size);
4998 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
5000 GetTextExtentExPointW(hdc, string + index, range_end - index,
5001 INT_MAX, NULL, NULL, &range_size);
5002 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
5004 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
5013 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
5014 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
5015 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
5016 INT regionCount, GpRegion** regions)
5020 HFONT gdifont, oldfont;
5021 struct measure_ranges_args args;
5022 HDC hdc, temp_hdc=NULL;
5027 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
5028 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5030 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5031 return InvalidParameter;
5033 if (regionCount < stringFormat->range_count)
5034 return InvalidParameter;
5038 hdc = temp_hdc = CreateCompatibleDC(0);
5039 if (!temp_hdc) return OutOfMemory;
5042 hdc = graphics->hdc;
5044 if (stringFormat->attr)
5045 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5053 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5054 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5055 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5056 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5057 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5059 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5060 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5062 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5063 scaled_rect.Y = layoutRect->Y * args.rel_height;
5064 if (stringFormat->attr & StringFormatFlagsNoClip)
5066 scaled_rect.Width = (REAL)(1 << 23);
5067 scaled_rect.Height = (REAL)(1 << 23);
5071 scaled_rect.Width = layoutRect->Width * args.rel_width;
5072 scaled_rect.Height = layoutRect->Height * args.rel_height;
5074 if (scaled_rect.Width >= 0.5)
5076 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5077 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5080 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
5081 oldfont = SelectObject(hdc, gdifont);
5083 for (i=0; i<stringFormat->range_count; i++)
5085 stat = GdipSetEmpty(regions[i]);
5090 args.regions = regions;
5092 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5093 measure_ranges_callback, &args);
5095 SelectObject(hdc, oldfont);
5096 DeleteObject(gdifont);
5104 struct measure_string_args {
5106 INT *codepointsfitted;
5108 REAL rel_width, rel_height;
5111 static GpStatus measure_string_callback(HDC hdc,
5112 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5113 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5114 INT lineno, const RectF *bounds, INT *underlined_indexes,
5115 INT underlined_index_count, void *user_data)
5117 struct measure_string_args *args = user_data;
5118 REAL new_width, new_height;
5120 new_width = bounds->Width / args->rel_width;
5121 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5123 if (new_width > args->bounds->Width)
5124 args->bounds->Width = new_width;
5126 if (new_height > args->bounds->Height)
5127 args->bounds->Height = new_height;
5129 if (args->codepointsfitted)
5130 *args->codepointsfitted = index + length;
5132 if (args->linesfilled)
5133 (*args->linesfilled)++;
5138 /* Find the smallest rectangle that bounds the text when it is printed in rect
5139 * according to the format options listed in format. If rect has 0 width and
5140 * height, then just find the smallest rectangle that bounds the text when it's
5141 * printed at location (rect->X, rect-Y). */
5142 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5143 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5144 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5145 INT *codepointsfitted, INT *linesfilled)
5147 HFONT oldfont, gdifont;
5148 struct measure_string_args args;
5149 HDC temp_hdc=NULL, hdc;
5153 INT lines, glyphs, format_flags = format ? format->attr : 0;
5155 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5156 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5157 bounds, codepointsfitted, linesfilled);
5159 if(!graphics || !string || !font || !rect || !bounds)
5160 return InvalidParameter;
5164 hdc = temp_hdc = CreateCompatibleDC(0);
5165 if (!temp_hdc) return OutOfMemory;
5168 hdc = graphics->hdc;
5170 if(linesfilled) *linesfilled = 0;
5171 if(codepointsfitted) *codepointsfitted = 0;
5174 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5182 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5183 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5184 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5185 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5186 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5188 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5189 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5191 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5192 scaled_rect.Y = rect->Y * args.rel_height;
5193 scaled_rect.Width = rect->Width * args.rel_width;
5194 scaled_rect.Height = rect->Height * args.rel_height;
5196 if ((format_flags & StringFormatFlagsNoClip) ||
5197 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5198 if ((format_flags & StringFormatFlagsNoClip) ||
5199 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5201 if (scaled_rect.Width >= 0.5)
5203 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5204 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5207 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5208 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5210 get_font_hfont(graphics, font, format, &gdifont, NULL);
5211 oldfont = SelectObject(hdc, gdifont);
5213 bounds->X = rect->X;
5214 bounds->Y = rect->Y;
5215 bounds->Width = 0.0;
5216 bounds->Height = 0.0;
5218 args.bounds = bounds;
5219 args.codepointsfitted = &glyphs;
5220 args.linesfilled = &lines;
5223 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5224 measure_string_callback, &args);
5226 if (linesfilled) *linesfilled = lines;
5227 if (codepointsfitted) *codepointsfitted = glyphs;
5230 bounds->Width += margin_x * 2.0;
5232 SelectObject(hdc, oldfont);
5233 DeleteObject(gdifont);
5241 struct draw_string_args {
5242 GpGraphics *graphics;
5243 GDIPCONST GpBrush *brush;
5244 REAL x, y, rel_width, rel_height, ascent;
5247 static GpStatus draw_string_callback(HDC hdc,
5248 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5249 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5250 INT lineno, const RectF *bounds, INT *underlined_indexes,
5251 INT underlined_index_count, void *user_data)
5253 struct draw_string_args *args = user_data;
5257 position.X = args->x + bounds->X / args->rel_width;
5258 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5260 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5261 args->brush, &position,
5262 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5264 if (stat == Ok && underlined_index_count)
5266 OUTLINETEXTMETRICW otm;
5267 REAL underline_y, underline_height;
5270 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5272 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5273 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5275 for (i=0; i<underlined_index_count; i++)
5277 REAL start_x, end_x;
5279 INT ofs = underlined_indexes[i] - index;
5281 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5282 start_x = text_size.cx / args->rel_width;
5284 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5285 end_x = text_size.cx / args->rel_width;
5287 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5294 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5295 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5296 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5300 GpPointF pt[3], rectcpy[4];
5302 REAL rel_width, rel_height, margin_x;
5303 INT save_state, format_flags = 0;
5305 struct draw_string_args args;
5307 HDC hdc, temp_hdc=NULL;
5308 TEXTMETRICW textmetric;
5310 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5311 length, font, debugstr_rectf(rect), format, brush);
5313 if(!graphics || !string || !font || !brush || !rect)
5314 return InvalidParameter;
5318 hdc = graphics->hdc;
5322 hdc = temp_hdc = CreateCompatibleDC(0);
5326 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5328 format_flags = format->attr;
5330 /* Should be no need to explicitly test for StringAlignmentNear as
5331 * that is default behavior if no alignment is passed. */
5332 if(format->vertalign != StringAlignmentNear){
5333 RectF bounds, in_rect = *rect;
5334 in_rect.Height = 0.0; /* avoid height clipping */
5335 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5337 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5339 if(format->vertalign == StringAlignmentCenter)
5340 offsety = (rect->Height - bounds.Height) / 2;
5341 else if(format->vertalign == StringAlignmentFar)
5342 offsety = (rect->Height - bounds.Height);
5344 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5347 save_state = SaveDC(hdc);
5355 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5356 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5357 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5358 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5359 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5361 rectcpy[3].X = rectcpy[0].X = rect->X;
5362 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5363 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5364 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5365 transform_and_round_points(graphics, corners, rectcpy, 4);
5367 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5368 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5370 scaled_rect.X = margin_x * rel_width;
5371 scaled_rect.Y = 0.0;
5372 scaled_rect.Width = rel_width * rect->Width;
5373 scaled_rect.Height = rel_height * rect->Height;
5375 if ((format_flags & StringFormatFlagsNoClip) ||
5376 scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5377 if ((format_flags & StringFormatFlagsNoClip) ||
5378 scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5380 if (scaled_rect.Width >= 0.5)
5382 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5383 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5386 if (scaled_rect.Width >= 1 << 23 || scaled_rect.Width < 0.5) scaled_rect.Width = 1 << 23;
5387 if (scaled_rect.Height >= 1 << 23 || scaled_rect.Height < 0.5) scaled_rect.Height = 1 << 23;
5389 if (!(format_flags & StringFormatFlagsNoClip) &&
5390 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23)
5392 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5393 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5394 SelectClipRgn(hdc, rgn);
5397 get_font_hfont(graphics, font, format, &gdifont, NULL);
5398 SelectObject(hdc, gdifont);
5400 args.graphics = graphics;
5404 args.y = rect->Y + offsety;
5406 args.rel_width = rel_width;
5407 args.rel_height = rel_height;
5409 GetTextMetricsW(hdc, &textmetric);
5410 args.ascent = textmetric.tmAscent / rel_height;
5412 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5413 draw_string_callback, &args);
5416 DeleteObject(gdifont);
5418 RestoreDC(hdc, save_state);
5425 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5427 TRACE("(%p)\n", graphics);
5430 return InvalidParameter;
5435 return GdipSetInfinite(graphics->clip);
5438 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5440 TRACE("(%p)\n", graphics);
5443 return InvalidParameter;
5448 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5451 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5453 return GdipEndContainer(graphics, state);
5456 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5457 GpMatrixOrder order)
5459 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5462 return InvalidParameter;
5467 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5470 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5472 return GdipBeginContainer2(graphics, state);
5475 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5476 GraphicsContainer *state)
5478 GraphicsContainerItem *container;
5481 TRACE("(%p, %p)\n", graphics, state);
5483 if(!graphics || !state)
5484 return InvalidParameter;
5486 sts = init_container(&container, graphics);
5490 list_add_head(&graphics->containers, &container->entry);
5491 *state = graphics->contid = container->contid;
5496 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5498 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5499 return NotImplemented;
5502 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5504 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5505 return NotImplemented;
5508 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5510 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5511 return NotImplemented;
5514 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5517 GraphicsContainerItem *container, *container2;
5519 TRACE("(%p, %x)\n", graphics, state);
5522 return InvalidParameter;
5524 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5525 if(container->contid == state)
5529 /* did not find a matching container */
5530 if(&container->entry == &graphics->containers)
5533 sts = restore_container(graphics, container);
5537 /* remove all of the containers on top of the found container */
5538 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5539 if(container->contid == state)
5541 list_remove(&container->entry);
5542 delete_container(container);
5545 list_remove(&container->entry);
5546 delete_container(container);
5551 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5552 REAL sy, GpMatrixOrder order)
5554 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5557 return InvalidParameter;
5562 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5565 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5568 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5570 if(!graphics || !srcgraphics)
5571 return InvalidParameter;
5573 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5576 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5577 CompositingMode mode)
5579 TRACE("(%p, %d)\n", graphics, mode);
5582 return InvalidParameter;
5587 graphics->compmode = mode;
5592 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5593 CompositingQuality quality)
5595 TRACE("(%p, %d)\n", graphics, quality);
5598 return InvalidParameter;
5603 graphics->compqual = quality;
5608 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5609 InterpolationMode mode)
5611 TRACE("(%p, %d)\n", graphics, mode);
5613 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5614 return InvalidParameter;
5619 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5620 mode = InterpolationModeBilinear;
5622 if (mode == InterpolationModeHighQuality)
5623 mode = InterpolationModeHighQualityBicubic;
5625 graphics->interpolation = mode;
5630 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5632 TRACE("(%p, %.2f)\n", graphics, scale);
5634 if(!graphics || (scale <= 0.0))
5635 return InvalidParameter;
5640 graphics->scale = scale;
5645 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5647 TRACE("(%p, %d)\n", graphics, unit);
5650 return InvalidParameter;
5655 if(unit == UnitWorld)
5656 return InvalidParameter;
5658 graphics->unit = unit;
5663 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5666 TRACE("(%p, %d)\n", graphics, mode);
5669 return InvalidParameter;
5674 graphics->pixeloffset = mode;
5679 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5683 TRACE("(%p,%i,%i)\n", graphics, x, y);
5686 FIXME("value is unused in rendering\n");
5689 return InvalidParameter;
5691 graphics->origin_x = x;
5692 graphics->origin_y = y;
5697 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5699 TRACE("(%p,%p,%p)\n", graphics, x, y);
5701 if (!graphics || !x || !y)
5702 return InvalidParameter;
5704 *x = graphics->origin_x;
5705 *y = graphics->origin_y;
5710 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5712 TRACE("(%p, %d)\n", graphics, mode);
5715 return InvalidParameter;
5720 graphics->smoothing = mode;
5725 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5727 TRACE("(%p, %d)\n", graphics, contrast);
5730 return InvalidParameter;
5732 graphics->textcontrast = contrast;
5737 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5738 TextRenderingHint hint)
5740 TRACE("(%p, %d)\n", graphics, hint);
5742 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5743 return InvalidParameter;
5748 graphics->texthint = hint;
5753 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5755 TRACE("(%p, %p)\n", graphics, matrix);
5757 if(!graphics || !matrix)
5758 return InvalidParameter;
5763 TRACE("%f,%f,%f,%f,%f,%f\n",
5764 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5765 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5767 graphics->worldtrans = *matrix;
5772 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5773 REAL dy, GpMatrixOrder order)
5775 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5778 return InvalidParameter;
5783 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5786 /*****************************************************************************
5787 * GdipSetClipHrgn [GDIPLUS.@]
5789 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5794 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5797 return InvalidParameter;
5799 status = GdipCreateRegionHrgn(hrgn, ®ion);
5803 status = GdipSetClipRegion(graphics, region, mode);
5805 GdipDeleteRegion(region);
5809 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5811 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5814 return InvalidParameter;
5819 return GdipCombineRegionPath(graphics->clip, path, mode);
5822 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5823 REAL width, REAL height,
5828 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5831 return InvalidParameter;
5839 rect.Height = height;
5841 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5844 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5845 INT width, INT height,
5848 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5851 return InvalidParameter;
5856 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5859 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5862 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5864 if(!graphics || !region)
5865 return InvalidParameter;
5870 return GdipCombineRegionRegion(graphics->clip, region, mode);
5873 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5878 TRACE("(%p,%u)\n", metafile, limitDpi);
5881 FIXME("not implemented\n");
5883 return NotImplemented;
5886 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5892 TRACE("(%p, %p, %d)\n", graphics, points, count);
5894 if(!graphics || !pen || count<=0)
5895 return InvalidParameter;
5902 FIXME("graphics object has no HDC\n");
5906 pti = GdipAlloc(sizeof(POINT) * count);
5908 save_state = prepare_dc(graphics, pen);
5909 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5911 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5912 Polygon(graphics->hdc, pti, count);
5914 restore_dc(graphics, save_state);
5920 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5927 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5929 if(count<=0) return InvalidParameter;
5930 ptf = GdipAlloc(sizeof(GpPointF) * count);
5932 for(i = 0;i < count; i++){
5933 ptf[i].X = (REAL)points[i].X;
5934 ptf[i].Y = (REAL)points[i].Y;
5937 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5943 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5945 TRACE("(%p, %p)\n", graphics, dpi);
5947 if(!graphics || !dpi)
5948 return InvalidParameter;
5953 *dpi = graphics->xres;
5957 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5959 TRACE("(%p, %p)\n", graphics, dpi);
5961 if(!graphics || !dpi)
5962 return InvalidParameter;
5967 *dpi = graphics->yres;
5971 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5972 GpMatrixOrder order)
5977 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5979 if(!graphics || !matrix)
5980 return InvalidParameter;
5985 m = graphics->worldtrans;
5987 ret = GdipMultiplyMatrix(&m, matrix, order);
5989 graphics->worldtrans = m;
5994 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5995 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5997 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
6001 TRACE("(%p, %p)\n", graphics, hdc);
6003 if(!graphics || !hdc)
6004 return InvalidParameter;
6009 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6011 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
6013 else if (!graphics->hdc || graphics->alpha_hdc ||
6014 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
6016 /* Create a fake HDC and fill it with a constant color. */
6020 BITMAPINFOHEADER bmih;
6023 stat = get_graphics_bounds(graphics, &bounds);
6027 graphics->temp_hbitmap_width = bounds.Width;
6028 graphics->temp_hbitmap_height = bounds.Height;
6030 bmih.biSize = sizeof(bmih);
6031 bmih.biWidth = graphics->temp_hbitmap_width;
6032 bmih.biHeight = -graphics->temp_hbitmap_height;
6034 bmih.biBitCount = 32;
6035 bmih.biCompression = BI_RGB;
6036 bmih.biSizeImage = 0;
6037 bmih.biXPelsPerMeter = 0;
6038 bmih.biYPelsPerMeter = 0;
6040 bmih.biClrImportant = 0;
6042 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6043 (void**)&graphics->temp_bits, NULL, 0);
6045 return GenericError;
6047 temp_hdc = CreateCompatibleDC(0);
6050 DeleteObject(hbitmap);
6051 return GenericError;
6054 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6055 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6057 SelectObject(temp_hdc, hbitmap);
6059 graphics->temp_hbitmap = hbitmap;
6060 *hdc = graphics->temp_hdc = temp_hdc;
6064 *hdc = graphics->hdc;
6068 graphics->busy = TRUE;
6073 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6077 TRACE("(%p, %p)\n", graphics, hdc);
6079 if(!graphics || !hdc || !graphics->busy)
6080 return InvalidParameter;
6082 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6084 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6086 else if (graphics->temp_hdc == hdc)
6091 /* Find the pixels that have changed, and mark them as opaque. */
6092 pos = (DWORD*)graphics->temp_bits;
6093 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6095 if (*pos != DC_BACKGROUND_KEY)
6102 /* Write the changed pixels to the real target. */
6103 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6104 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6105 graphics->temp_hbitmap_width * 4);
6108 DeleteDC(graphics->temp_hdc);
6109 DeleteObject(graphics->temp_hbitmap);
6110 graphics->temp_hdc = NULL;
6111 graphics->temp_hbitmap = NULL;
6113 else if (hdc != graphics->hdc)
6115 stat = InvalidParameter;
6119 graphics->busy = FALSE;
6124 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6129 TRACE("(%p, %p)\n", graphics, region);
6131 if(!graphics || !region)
6132 return InvalidParameter;
6137 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6140 /* free everything except root node and header */
6141 delete_element(®ion->node);
6142 memcpy(region, clip, sizeof(GpRegion));
6148 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6149 GpCoordinateSpace src_space, GpMatrix *matrix)
6152 REAL scale_x, scale_y;
6154 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
6156 if (dst_space != src_space)
6158 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6159 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6161 if(graphics->unit != UnitDisplay)
6163 scale_x *= graphics->scale;
6164 scale_y *= graphics->scale;
6167 /* transform from src_space to CoordinateSpacePage */
6170 case CoordinateSpaceWorld:
6171 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
6173 case CoordinateSpacePage:
6175 case CoordinateSpaceDevice:
6176 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6180 /* transform from CoordinateSpacePage to dst_space */
6183 case CoordinateSpaceWorld:
6185 GpMatrix inverted_transform = graphics->worldtrans;
6186 stat = GdipInvertMatrix(&inverted_transform);
6188 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
6191 case CoordinateSpacePage:
6193 case CoordinateSpaceDevice:
6194 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
6201 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6202 GpCoordinateSpace src_space, GpPointF *points, INT count)
6207 if(!graphics || !points || count <= 0)
6208 return InvalidParameter;
6213 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6215 if (src_space == dst_space) return Ok;
6217 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6218 if (stat != Ok) return stat;
6220 return GdipTransformMatrixPoints(&matrix, points, count);
6223 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6224 GpCoordinateSpace src_space, GpPoint *points, INT count)
6230 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6233 return InvalidParameter;
6235 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6239 for(i = 0; i < count; i++){
6240 pointsF[i].X = (REAL)points[i].X;
6241 pointsF[i].Y = (REAL)points[i].Y;
6244 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6247 for(i = 0; i < count; i++){
6248 points[i].X = gdip_round(pointsF[i].X);
6249 points[i].Y = gdip_round(pointsF[i].Y);
6256 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6268 /*****************************************************************************
6269 * GdipTranslateClip [GDIPLUS.@]
6271 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6273 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6276 return InvalidParameter;
6281 return GdipTranslateRegion(graphics->clip, dx, dy);
6284 /*****************************************************************************
6285 * GdipTranslateClipI [GDIPLUS.@]
6287 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6289 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6292 return InvalidParameter;
6297 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6301 /*****************************************************************************
6302 * GdipMeasureDriverString [GDIPLUS.@]
6304 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6305 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6306 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6308 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6311 REAL min_x, min_y, max_x, max_y, x, y;
6313 TEXTMETRICW textmetric;
6314 const WORD *glyph_indices;
6315 WORD *dynamic_glyph_indices=NULL;
6316 REAL rel_width, rel_height, ascent, descent;
6319 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6321 if (!graphics || !text || !font || !positions || !boundingBox)
6322 return InvalidParameter;
6325 length = strlenW(text);
6329 boundingBox->X = 0.0;
6330 boundingBox->Y = 0.0;
6331 boundingBox->Width = 0.0;
6332 boundingBox->Height = 0.0;
6335 if (flags & unsupported_flags)
6336 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6338 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6340 hdc = CreateCompatibleDC(0);
6341 SelectObject(hdc, hfont);
6343 GetTextMetricsW(hdc, &textmetric);
6353 GpMatrix xform = *matrix;
6354 GdipTransformMatrixPoints(&xform, pt, 3);
6356 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6357 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6358 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6359 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6360 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6362 if (flags & DriverStringOptionsCmapLookup)
6364 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6368 DeleteObject(hfont);
6372 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6375 glyph_indices = text;
6377 min_x = max_x = x = positions[0].X;
6378 min_y = max_y = y = positions[0].Y;
6380 ascent = textmetric.tmAscent / rel_height;
6381 descent = textmetric.tmDescent / rel_height;
6383 for (i=0; i<length; i++)
6388 if (!(flags & DriverStringOptionsRealizedAdvance))
6394 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6395 char_width = abc.abcA + abc.abcB + abc.abcC;
6397 if (min_y > y - ascent) min_y = y - ascent;
6398 if (max_y < y + descent) max_y = y + descent;
6399 if (min_x > x) min_x = x;
6401 x += char_width / rel_width;
6403 if (max_x < x) max_x = x;
6406 GdipFree(dynamic_glyph_indices);
6408 DeleteObject(hfont);
6410 boundingBox->X = min_x;
6411 boundingBox->Y = min_y;
6412 boundingBox->Width = max_x - min_x;
6413 boundingBox->Height = max_y - min_y;
6418 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6419 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6420 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6421 INT flags, GDIPCONST GpMatrix *matrix)
6423 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6429 if (flags & unsupported_flags)
6430 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6432 if (!(flags & DriverStringOptionsCmapLookup))
6433 eto_flags |= ETO_GLYPH_INDEX;
6435 save_state = SaveDC(graphics->hdc);
6436 SetBkMode(graphics->hdc, TRANSPARENT);
6437 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6440 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6442 get_font_hfont(graphics, font, format, &hfont, matrix);
6443 SelectObject(graphics->hdc, hfont);
6445 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6447 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6449 RestoreDC(graphics->hdc, save_state);
6451 DeleteObject(hfont);
6456 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6457 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6458 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6459 INT flags, GDIPCONST GpMatrix *matrix)
6461 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6463 PointF *real_positions, real_position;
6467 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6468 DWORD max_glyphsize=0;
6469 GLYPHMETRICS glyphmetrics;
6470 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6473 int text_mask_stride;
6475 int pixel_data_stride;
6477 UINT ggo_flags = GGO_GRAY8_BITMAP;
6482 if (!(flags & DriverStringOptionsCmapLookup))
6483 ggo_flags |= GGO_GLYPH_INDEX;
6485 if (flags & unsupported_flags)
6486 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6488 pti = GdipAlloc(sizeof(POINT) * length);
6492 if (flags & DriverStringOptionsRealizedAdvance)
6494 real_position = positions[0];
6496 transform_and_round_points(graphics, pti, &real_position, 1);
6500 real_positions = GdipAlloc(sizeof(PointF) * length);
6501 if (!real_positions)
6507 memcpy(real_positions, positions, sizeof(PointF) * length);
6509 transform_and_round_points(graphics, pti, real_positions, length);
6511 GdipFree(real_positions);
6514 get_font_hfont(graphics, font, format, &hfont, matrix);
6516 hdc = CreateCompatibleDC(0);
6517 SelectObject(hdc, hfont);
6519 /* Get the boundaries of the text to be drawn */
6520 for (i=0; i<length; i++)
6523 int left, top, right, bottom;
6525 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6526 &glyphmetrics, 0, NULL, &identity);
6528 if (glyphsize == GDI_ERROR)
6530 ERR("GetGlyphOutlineW failed\n");
6533 DeleteObject(hfont);
6534 return GenericError;
6537 if (glyphsize > max_glyphsize)
6538 max_glyphsize = glyphsize;
6540 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6541 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6542 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6543 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6545 if (left < min_x) min_x = left;
6546 if (top < min_y) min_y = top;
6547 if (right > max_x) max_x = right;
6548 if (bottom > max_y) max_y = bottom;
6550 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6552 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6553 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6557 glyph_mask = GdipAlloc(max_glyphsize);
6558 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6559 text_mask_stride = max_x - min_x;
6561 if (!(glyph_mask && text_mask))
6563 GdipFree(glyph_mask);
6564 GdipFree(text_mask);
6567 DeleteObject(hfont);
6571 /* Generate a mask for the text */
6572 for (i=0; i<length; i++)
6574 int left, top, stride;
6576 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6577 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6579 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6580 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6581 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6583 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6585 BYTE *glyph_val = glyph_mask + y * stride;
6586 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6587 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6589 *text_val = min(64, *text_val + *glyph_val);
6598 DeleteObject(hfont);
6599 GdipFree(glyph_mask);
6601 /* get the brush data */
6602 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6605 GdipFree(text_mask);
6609 pixel_area.X = min_x;
6610 pixel_area.Y = min_y;
6611 pixel_area.Width = max_x - min_x;
6612 pixel_area.Height = max_y - min_y;
6613 pixel_data_stride = pixel_area.Width * 4;
6615 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6618 GdipFree(text_mask);
6619 GdipFree(pixel_data);
6623 /* multiply the brush data by the mask */
6624 for (y=0; y<pixel_area.Height; y++)
6626 BYTE *text_val = text_mask + text_mask_stride * y;
6627 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6628 for (x=0; x<pixel_area.Width; x++)
6630 *pixel_val = (*pixel_val) * (*text_val) / 64;
6636 GdipFree(text_mask);
6638 /* draw the result */
6639 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6640 pixel_area.Height, pixel_data_stride);
6642 GdipFree(pixel_data);
6647 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6648 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6649 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6650 INT flags, GDIPCONST GpMatrix *matrix)
6652 GpStatus stat = NotImplemented;
6655 length = strlenW(text);
6657 if (graphics->hdc && !graphics->alpha_hdc &&
6658 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6659 brush->bt == BrushTypeSolidColor &&
6660 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6661 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6662 brush, positions, flags, matrix);
6663 if (stat == NotImplemented)
6664 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6665 brush, positions, flags, matrix);
6669 /*****************************************************************************
6670 * GdipDrawDriverString [GDIPLUS.@]
6672 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6673 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6674 GDIPCONST PointF *positions, INT flags,
6675 GDIPCONST GpMatrix *matrix )
6677 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6679 if (!graphics || !text || !font || !brush || !positions)
6680 return InvalidParameter;
6682 return draw_driver_string(graphics, text, length, font, NULL,
6683 brush, positions, flags, matrix);
6686 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6687 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6689 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6690 return NotImplemented;
6693 /*****************************************************************************
6694 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6696 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6701 TRACE("(%p, %p)\n", graphics, res);
6703 if((stat = GdipCreateRegion(&rgn)) != Ok)
6706 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6709 stat = GdipIsEmptyRegion(rgn, graphics, res);
6712 GdipDeleteRegion(rgn);
6716 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6720 TRACE("(%p) stub\n", graphics);
6723 FIXME("not implemented\n");
6725 return NotImplemented;