2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/unicode.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
52 REAL radAngle, hypotenuse;
54 radAngle = deg2rad(angle);
55 hypotenuse = 50.0; /* arbitrary */
57 *x = x_0 + cos(radAngle) * hypotenuse;
58 *y = y_0 + sin(radAngle) * hypotenuse;
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
66 switch(type & PathPointTypePathTypeMask){
67 case PathPointTypeBezier:
70 case PathPointTypeLine:
73 case PathPointTypeStart:
77 ERR("Bad point type\n");
81 if(type & PathPointTypeCloseSubpath)
82 ret |= PT_CLOSEFIGURE;
87 static COLORREF get_gdi_brush_color(const GpBrush *brush)
93 case BrushTypeSolidColor:
95 const GpSolidFill *sf = (const GpSolidFill *)brush;
99 case BrushTypeHatchFill:
101 const GpHatch *hatch = (const GpHatch *)brush;
102 argb = hatch->forecol;
105 case BrushTypeLinearGradient:
107 const GpLineGradient *line = (const GpLineGradient *)brush;
108 argb = line->startcolor;
111 case BrushTypePathGradient:
113 const GpPathGradient *grad = (const GpPathGradient *)brush;
114 argb = grad->centercolor;
118 FIXME("unhandled brush type %d\n", brush->bt);
122 return ARGB2COLORREF(argb);
125 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
128 BITMAPINFOHEADER bmih;
132 bmih.biSize = sizeof(bmih);
136 bmih.biBitCount = 32;
137 bmih.biCompression = BI_RGB;
138 bmih.biSizeImage = 0;
140 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
143 const char *hatch_data;
145 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
147 for (y = 0; y < 8; y++)
149 for (x = 0; x < 8; x++)
151 if (hatch_data[y] & (0x80 >> x))
152 bits[y * 8 + x] = hatch->forecol;
154 bits[y * 8 + x] = hatch->backcol;
160 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
162 for (y = 0; y < 64; y++)
163 bits[y] = hatch->forecol;
170 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
174 case BrushTypeSolidColor:
176 const GpSolidFill *sf = (const GpSolidFill *)brush;
177 lb->lbStyle = BS_SOLID;
178 lb->lbColor = ARGB2COLORREF(sf->color);
183 case BrushTypeHatchFill:
185 const GpHatch *hatch = (const GpHatch *)brush;
188 hbmp = create_hatch_bitmap(hatch);
189 if (!hbmp) return OutOfMemory;
191 lb->lbStyle = BS_PATTERN;
193 lb->lbHatch = (ULONG_PTR)hbmp;
198 FIXME("unhandled brush type %d\n", brush->bt);
199 lb->lbStyle = BS_SOLID;
200 lb->lbColor = get_gdi_brush_color(brush);
206 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
211 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
217 static HBRUSH create_gdi_brush(const GpBrush *brush)
222 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
224 gdibrush = CreateBrushIndirect(&lb);
225 free_gdi_logbrush(&lb);
230 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
235 INT save_state, i, numdashes;
237 DWORD dash_array[MAX_DASHLEN];
239 save_state = SaveDC(graphics->hdc);
241 EndPath(graphics->hdc);
243 if(pen->unit == UnitPixel){
247 /* Get an estimate for the amount the pen width is affected by the world
248 * transform. (This is similar to what some of the wine drivers do.) */
253 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
254 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
255 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
257 width *= pen->width * convert_unit(graphics->xres,
258 pen->unit == UnitWorld ? graphics->unit : pen->unit);
261 if(pen->dash == DashStyleCustom){
262 numdashes = min(pen->numdashes, MAX_DASHLEN);
264 TRACE("dashes are: ");
265 for(i = 0; i < numdashes; i++){
266 dash_array[i] = roundr(width * pen->dashes[i]);
267 TRACE("%d, ", dash_array[i]);
269 TRACE("\n and the pen style is %x\n", pen->style);
271 create_gdi_logbrush(pen->brush, &lb);
272 gdipen = ExtCreatePen(pen->style, roundr(width), &lb,
273 numdashes, dash_array);
274 free_gdi_logbrush(&lb);
278 create_gdi_logbrush(pen->brush, &lb);
279 gdipen = ExtCreatePen(pen->style, roundr(width), &lb, 0, NULL);
280 free_gdi_logbrush(&lb);
283 SelectObject(graphics->hdc, gdipen);
288 static void restore_dc(GpGraphics *graphics, INT state)
290 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
291 RestoreDC(graphics->hdc, state);
294 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
295 GpCoordinateSpace src_space, GpMatrix **matrix);
297 /* This helper applies all the changes that the points listed in ptf need in
298 * order to be drawn on the device context. In the end, this should include at
300 * -scaling by page unit
301 * -applying world transformation
302 * -converting from float to int
303 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
304 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
305 * gdi to draw, and these functions would irreparably mess with line widths.
307 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
308 GpPointF *ptf, INT count)
310 REAL scale_x, scale_y;
314 scale_x = convert_unit(graphics->xres, graphics->unit);
315 scale_y = convert_unit(graphics->yres, graphics->unit);
317 /* apply page scale */
318 if(graphics->unit != UnitDisplay)
320 scale_x *= graphics->scale;
321 scale_y *= graphics->scale;
324 GdipCloneMatrix(graphics->worldtrans, &matrix);
325 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
326 GdipTransformMatrixPoints(matrix, ptf, count);
327 GdipDeleteMatrix(matrix);
329 for(i = 0; i < count; i++){
330 pti[i].x = roundr(ptf[i].X);
331 pti[i].y = roundr(ptf[i].Y);
335 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
336 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
338 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
340 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
342 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
343 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
349 bf.BlendOp = AC_SRC_OVER;
351 bf.SourceConstantAlpha = 255;
352 bf.AlphaFormat = AC_SRC_ALPHA;
354 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
355 hdc, src_x, src_y, src_width, src_height, bf);
359 /* Draw non-premultiplied ARGB data to the given graphics object */
360 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
361 const BYTE *src, INT src_width, INT src_height, INT src_stride)
363 if (graphics->image && graphics->image->type == ImageTypeBitmap)
365 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
368 for (x=0; x<src_width; x++)
370 for (y=0; y<src_height; y++)
372 ARGB dst_color, src_color;
373 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
374 src_color = ((ARGB*)(src + src_stride * y))[x];
375 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
381 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
383 ERR("This should not be used for metafiles; fix caller\n");
384 return NotImplemented;
390 BITMAPINFOHEADER bih;
393 hdc = CreateCompatibleDC(0);
395 bih.biSize = sizeof(BITMAPINFOHEADER);
396 bih.biWidth = src_width;
397 bih.biHeight = -src_height;
400 bih.biCompression = BI_RGB;
402 bih.biXPelsPerMeter = 0;
403 bih.biYPelsPerMeter = 0;
405 bih.biClrImportant = 0;
407 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
408 (void**)&temp_bits, NULL, 0);
410 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
411 4 * src_width, src, src_stride);
413 SelectObject(hdc, hbitmap);
414 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
415 hdc, 0, 0, src_width, src_height);
417 DeleteObject(hbitmap);
423 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
424 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
428 if (graphics->image && graphics->image->type == ImageTypeBitmap)
434 size = GetRegionData(hregion, 0, NULL);
436 rgndata = GdipAlloc(size);
440 GetRegionData(hregion, size, rgndata);
442 rects = (RECT*)&rgndata->Buffer;
444 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
446 stat = alpha_blend_pixels(graphics, rects[i].left, rects[i].top,
447 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
448 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
456 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
458 ERR("This should not be used for metafiles; fix caller\n");
459 return NotImplemented;
465 save = SaveDC(graphics->hdc);
467 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
469 stat = alpha_blend_pixels(graphics, dst_x, dst_y, src, src_width,
470 src_height, src_stride);
472 RestoreDC(graphics->hdc, save);
478 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
484 a1 = (start >> 24) & 0xff;
485 a2 = (end >> 24) & 0xff;
487 a3 = (int)(a1*(1.0f - position)+a2*(position));
491 for (i=0xff; i<=0xff0000; i = i << 8)
492 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
496 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
500 /* clamp to between 0.0 and 1.0, using the wrap mode */
501 if (brush->wrap == WrapModeTile)
503 position = fmodf(position, 1.0f);
504 if (position < 0.0f) position += 1.0f;
506 else /* WrapModeFlip* */
508 position = fmodf(position, 2.0f);
509 if (position < 0.0f) position += 2.0f;
510 if (position > 1.0f) position = 2.0f - position;
513 if (brush->blendcount == 1)
518 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
521 /* locate the blend positions surrounding this position */
522 while (position > brush->blendpos[i])
525 /* interpolate between the blend positions */
526 left_blendpos = brush->blendpos[i-1];
527 left_blendfac = brush->blendfac[i-1];
528 right_blendpos = brush->blendpos[i];
529 right_blendfac = brush->blendfac[i];
530 range = right_blendpos - left_blendpos;
531 blendfac = (left_blendfac * (right_blendpos - position) +
532 right_blendfac * (position - left_blendpos)) / range;
535 if (brush->pblendcount == 0)
536 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
540 ARGB left_blendcolor, right_blendcolor;
541 REAL left_blendpos, right_blendpos;
543 /* locate the blend colors surrounding this position */
544 while (blendfac > brush->pblendpos[i])
547 /* interpolate between the blend colors */
548 left_blendpos = brush->pblendpos[i-1];
549 left_blendcolor = brush->pblendcolor[i-1];
550 right_blendpos = brush->pblendpos[i];
551 right_blendcolor = brush->pblendcolor[i];
552 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
553 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
557 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
561 unsigned char a, r, g, b;
563 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
564 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
565 val[2] = (color & 0xff) / 255.0; /* blue */
566 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
567 val[4] = 1.0; /* translation */
574 res[i] += matrix->m[j][i] * val[j];
577 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
578 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
579 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
580 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
582 return (a << 24) | (r << 16) | (g << 8) | b;
585 static int color_is_gray(ARGB color)
587 unsigned char r, g, b;
589 r = (color >> 16) & 0xff;
590 g = (color >> 8) & 0xff;
593 return (r == g) && (g == b);
596 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
597 UINT width, UINT height, INT stride, ColorAdjustType type)
601 if (attributes->colorkeys[type].enabled ||
602 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
604 const struct color_key *key;
605 BYTE min_blue, min_green, min_red;
606 BYTE max_blue, max_green, max_red;
608 if (attributes->colorkeys[type].enabled)
609 key = &attributes->colorkeys[type];
611 key = &attributes->colorkeys[ColorAdjustTypeDefault];
613 min_blue = key->low&0xff;
614 min_green = (key->low>>8)&0xff;
615 min_red = (key->low>>16)&0xff;
617 max_blue = key->high&0xff;
618 max_green = (key->high>>8)&0xff;
619 max_red = (key->high>>16)&0xff;
621 for (x=0; x<width; x++)
622 for (y=0; y<height; y++)
625 BYTE blue, green, red;
626 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
627 blue = *src_color&0xff;
628 green = (*src_color>>8)&0xff;
629 red = (*src_color>>16)&0xff;
630 if (blue >= min_blue && green >= min_green && red >= min_red &&
631 blue <= max_blue && green <= max_green && red <= max_red)
632 *src_color = 0x00000000;
636 if (attributes->colorremaptables[type].enabled ||
637 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
639 const struct color_remap_table *table;
641 if (attributes->colorremaptables[type].enabled)
642 table = &attributes->colorremaptables[type];
644 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
646 for (x=0; x<width; x++)
647 for (y=0; y<height; y++)
650 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
651 for (i=0; i<table->mapsize; i++)
653 if (*src_color == table->colormap[i].oldColor.Argb)
655 *src_color = table->colormap[i].newColor.Argb;
662 if (attributes->colormatrices[type].enabled ||
663 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
665 const struct color_matrix *colormatrices;
667 if (attributes->colormatrices[type].enabled)
668 colormatrices = &attributes->colormatrices[type];
670 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
672 for (x=0; x<width; x++)
673 for (y=0; y<height; y++)
676 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
678 if (colormatrices->flags == ColorMatrixFlagsDefault ||
679 !color_is_gray(*src_color))
681 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
683 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
685 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
690 if (attributes->gamma_enabled[type] ||
691 attributes->gamma_enabled[ColorAdjustTypeDefault])
695 if (attributes->gamma_enabled[type])
696 gamma = attributes->gamma[type];
698 gamma = attributes->gamma[ColorAdjustTypeDefault];
700 for (x=0; x<width; x++)
701 for (y=0; y<height; y++)
704 BYTE blue, green, red;
705 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
707 blue = *src_color&0xff;
708 green = (*src_color>>8)&0xff;
709 red = (*src_color>>16)&0xff;
711 /* FIXME: We should probably use a table for this. */
712 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
713 green = floorf(powf(green / 255.0, gamma) * 255.0);
714 red = floorf(powf(red / 255.0, gamma) * 255.0);
716 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
721 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
722 * bitmap that contains all the pixels we may need to draw it. */
723 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
724 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
727 INT left, top, right, bottom;
729 switch (interpolation)
731 case InterpolationModeHighQualityBilinear:
732 case InterpolationModeHighQualityBicubic:
733 /* FIXME: Include a greater range for the prefilter? */
734 case InterpolationModeBicubic:
735 case InterpolationModeBilinear:
736 left = (INT)(floorf(srcx));
737 top = (INT)(floorf(srcy));
738 right = (INT)(ceilf(srcx+srcwidth));
739 bottom = (INT)(ceilf(srcy+srcheight));
741 case InterpolationModeNearestNeighbor:
745 right = roundr(srcx+srcwidth);
746 bottom = roundr(srcy+srcheight);
750 if (wrap == WrapModeClamp)
756 if (right >= bitmap->width)
757 right = bitmap->width-1;
758 if (bottom >= bitmap->height)
759 bottom = bitmap->height-1;
763 /* In some cases we can make the rectangle smaller here, but the logic
764 * is hard to get right, and tiling suggests we're likely to use the
765 * entire source image. */
766 if (left < 0 || right >= bitmap->width)
769 right = bitmap->width-1;
772 if (top < 0 || bottom >= bitmap->height)
775 bottom = bitmap->height-1;
781 rect->Width = right - left + 1;
782 rect->Height = bottom - top + 1;
785 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
786 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
788 if (attributes->wrap == WrapModeClamp)
790 if (x < 0 || y < 0 || x >= width || y >= height)
791 return attributes->outside_color;
795 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
797 x = width*2 + x % (width * 2);
799 y = height*2 + y % (height * 2);
801 if ((attributes->wrap & 1) == 1)
804 if ((x / width) % 2 == 0)
807 x = width - 1 - x % width;
812 if ((attributes->wrap & 2) == 2)
815 if ((y / height) % 2 == 0)
818 y = height - 1 - y % height;
824 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
826 ERR("out of range pixel requested\n");
830 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
833 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
834 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
835 InterpolationMode interpolation)
839 switch (interpolation)
843 FIXME("Unimplemented interpolation %i\n", interpolation);
845 case InterpolationModeBilinear:
848 INT leftx, rightx, topy, bottomy;
849 ARGB topleft, topright, bottomleft, bottomright;
853 leftxf = floorf(point->X);
855 rightx = (INT)ceilf(point->X);
856 topyf = floorf(point->Y);
858 bottomy = (INT)ceilf(point->Y);
860 if (leftx == rightx && topy == bottomy)
861 return sample_bitmap_pixel(src_rect, bits, width, height,
862 leftx, topy, attributes);
864 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
865 leftx, topy, attributes);
866 topright = sample_bitmap_pixel(src_rect, bits, width, height,
867 rightx, topy, attributes);
868 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
869 leftx, bottomy, attributes);
870 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
871 rightx, bottomy, attributes);
873 x_offset = point->X - leftxf;
874 top = blend_colors(topleft, topright, x_offset);
875 bottom = blend_colors(bottomleft, bottomright, x_offset);
877 return blend_colors(top, bottom, point->Y - topyf);
879 case InterpolationModeNearestNeighbor:
880 return sample_bitmap_pixel(src_rect, bits, width, height,
881 roundr(point->X), roundr(point->Y), attributes);
885 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
887 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
890 static INT brush_can_fill_path(GpBrush *brush)
894 case BrushTypeSolidColor:
896 case BrushTypeHatchFill:
898 GpHatch *hatch = (GpHatch*)brush;
899 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
900 ((hatch->backcol & 0xff000000) == 0xff000000);
902 case BrushTypeLinearGradient:
903 case BrushTypeTextureFill:
904 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
910 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
914 case BrushTypeSolidColor:
916 GpSolidFill *fill = (GpSolidFill*)brush;
917 HBITMAP bmp = ARGB2BMP(fill->color);
922 /* partially transparent fill */
924 SelectClipPath(graphics->hdc, RGN_AND);
925 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
927 HDC hdc = CreateCompatibleDC(NULL);
931 SelectObject(hdc, bmp);
932 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
940 /* else fall through */
944 HBRUSH gdibrush, old_brush;
946 gdibrush = create_gdi_brush(brush);
947 if (!gdibrush) return;
949 old_brush = SelectObject(graphics->hdc, gdibrush);
950 FillPath(graphics->hdc);
951 SelectObject(graphics->hdc, old_brush);
952 DeleteObject(gdibrush);
958 static INT brush_can_fill_pixels(GpBrush *brush)
962 case BrushTypeSolidColor:
963 case BrushTypeHatchFill:
964 case BrushTypeLinearGradient:
965 case BrushTypeTextureFill:
966 case BrushTypePathGradient:
973 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
974 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
978 case BrushTypeSolidColor:
981 GpSolidFill *fill = (GpSolidFill*)brush;
982 for (x=0; x<fill_area->Width; x++)
983 for (y=0; y<fill_area->Height; y++)
984 argb_pixels[x + y*cdwStride] = fill->color;
987 case BrushTypeHatchFill:
990 GpHatch *fill = (GpHatch*)brush;
991 const char *hatch_data;
993 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
994 return NotImplemented;
996 for (x=0; x<fill_area->Width; x++)
997 for (y=0; y<fill_area->Height; y++)
1001 /* FIXME: Account for the rendering origin */
1002 hx = (x + fill_area->X) % 8;
1003 hy = (y + fill_area->Y) % 8;
1005 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1006 argb_pixels[x + y*cdwStride] = fill->forecol;
1008 argb_pixels[x + y*cdwStride] = fill->backcol;
1013 case BrushTypeLinearGradient:
1015 GpLineGradient *fill = (GpLineGradient*)brush;
1016 GpPointF draw_points[3], line_points[3];
1018 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1019 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1022 draw_points[0].X = fill_area->X;
1023 draw_points[0].Y = fill_area->Y;
1024 draw_points[1].X = fill_area->X+1;
1025 draw_points[1].Y = fill_area->Y;
1026 draw_points[2].X = fill_area->X;
1027 draw_points[2].Y = fill_area->Y+1;
1029 /* Transform the points to a co-ordinate space where X is the point's
1030 * position in the gradient, 0.0 being the start point and 1.0 the
1032 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1033 CoordinateSpaceDevice, draw_points, 3);
1037 line_points[0] = fill->startpoint;
1038 line_points[1] = fill->endpoint;
1039 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1040 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1042 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1047 stat = GdipInvertMatrix(world_to_gradient);
1050 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1052 GdipDeleteMatrix(world_to_gradient);
1057 REAL x_delta = draw_points[1].X - draw_points[0].X;
1058 REAL y_delta = draw_points[2].X - draw_points[0].X;
1060 for (y=0; y<fill_area->Height; y++)
1062 for (x=0; x<fill_area->Width; x++)
1064 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1066 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1073 case BrushTypeTextureFill:
1075 GpTexture *fill = (GpTexture*)brush;
1076 GpPointF draw_points[3];
1078 GpMatrix *world_to_texture;
1084 if (fill->image->type != ImageTypeBitmap)
1086 FIXME("metafile texture brushes not implemented\n");
1087 return NotImplemented;
1090 bitmap = (GpBitmap*)fill->image;
1091 src_stride = sizeof(ARGB) * bitmap->width;
1093 src_area.X = src_area.Y = 0;
1094 src_area.Width = bitmap->width;
1095 src_area.Height = bitmap->height;
1097 draw_points[0].X = fill_area->X;
1098 draw_points[0].Y = fill_area->Y;
1099 draw_points[1].X = fill_area->X+1;
1100 draw_points[1].Y = fill_area->Y;
1101 draw_points[2].X = fill_area->X;
1102 draw_points[2].Y = fill_area->Y+1;
1104 /* Transform the points to the co-ordinate space of the bitmap. */
1105 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1106 CoordinateSpaceDevice, draw_points, 3);
1110 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
1115 stat = GdipInvertMatrix(world_to_texture);
1118 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
1120 GdipDeleteMatrix(world_to_texture);
1123 if (stat == Ok && !fill->bitmap_bits)
1125 BitmapData lockeddata;
1127 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1128 if (!fill->bitmap_bits)
1133 lockeddata.Width = bitmap->width;
1134 lockeddata.Height = bitmap->height;
1135 lockeddata.Stride = src_stride;
1136 lockeddata.PixelFormat = PixelFormat32bppARGB;
1137 lockeddata.Scan0 = fill->bitmap_bits;
1139 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1140 PixelFormat32bppARGB, &lockeddata);
1144 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1147 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1148 bitmap->width, bitmap->height,
1149 src_stride, ColorAdjustTypeBitmap);
1153 GdipFree(fill->bitmap_bits);
1154 fill->bitmap_bits = NULL;
1160 REAL x_dx = draw_points[1].X - draw_points[0].X;
1161 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1162 REAL y_dx = draw_points[2].X - draw_points[0].X;
1163 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1165 for (y=0; y<fill_area->Height; y++)
1167 for (x=0; x<fill_area->Width; x++)
1170 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1171 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1173 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1174 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1175 &point, fill->imageattributes, graphics->interpolation);
1182 case BrushTypePathGradient:
1184 GpPathGradient *fill = (GpPathGradient*)brush;
1186 GpMatrix *world_to_device;
1188 int i, figure_start=0;
1189 GpPointF start_point, end_point, center_point;
1191 REAL min_yf, max_yf, line1_xf, line2_xf;
1192 INT min_y, max_y, min_x, max_x;
1195 static int transform_fixme_once;
1197 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1201 FIXME("path gradient focus not implemented\n");
1208 FIXME("path gradient gamma correction not implemented\n");
1211 if (fill->blendcount)
1215 FIXME("path gradient blend not implemented\n");
1218 if (fill->pblendcount)
1222 FIXME("path gradient preset blend not implemented\n");
1225 if (!transform_fixme_once)
1227 BOOL is_identity=TRUE;
1228 GdipIsMatrixIdentity(fill->transform, &is_identity);
1231 FIXME("path gradient transform not implemented\n");
1232 transform_fixme_once = 1;
1236 stat = GdipClonePath(fill->path, &flat_path);
1241 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1242 CoordinateSpaceWorld, &world_to_device);
1245 stat = GdipTransformPath(flat_path, world_to_device);
1249 center_point = fill->center;
1250 stat = GdipTransformMatrixPoints(world_to_device, ¢er_point, 1);
1254 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1256 GdipDeleteMatrix(world_to_device);
1261 GdipDeletePath(flat_path);
1265 for (i=0; i<flat_path->pathdata.Count; i++)
1267 int start_center_line=0, end_center_line=0;
1268 int seen_start=0, seen_end=0, seen_center=0;
1269 REAL center_distance;
1270 ARGB start_color, end_color;
1273 type = flat_path->pathdata.Types[i];
1275 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1278 start_point = flat_path->pathdata.Points[i];
1280 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1282 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1284 end_point = flat_path->pathdata.Points[figure_start];
1285 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1287 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1289 end_point = flat_path->pathdata.Points[i+1];
1290 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1295 outer_color = start_color;
1297 min_yf = center_point.Y;
1298 if (min_yf > start_point.Y) min_yf = start_point.Y;
1299 if (min_yf > end_point.Y) min_yf = end_point.Y;
1301 if (min_yf < fill_area->Y)
1302 min_y = fill_area->Y;
1304 min_y = (INT)ceil(min_yf);
1306 max_yf = center_point.Y;
1307 if (max_yf < start_point.Y) max_yf = start_point.Y;
1308 if (max_yf < end_point.Y) max_yf = end_point.Y;
1310 if (max_yf > fill_area->Y + fill_area->Height)
1311 max_y = fill_area->Y + fill_area->Height;
1313 max_y = (INT)ceil(max_yf);
1315 dy = end_point.Y - start_point.Y;
1316 dx = end_point.X - start_point.X;
1318 /* This is proportional to the distance from start-end line to center point. */
1319 center_distance = dy * (start_point.X - center_point.X) +
1320 dx * (center_point.Y - start_point.Y);
1322 for (y=min_y; y<max_y; y++)
1326 if (!seen_start && yf >= start_point.Y)
1329 start_center_line ^= 1;
1331 if (!seen_end && yf >= end_point.Y)
1334 end_center_line ^= 1;
1336 if (!seen_center && yf >= center_point.Y)
1339 start_center_line ^= 1;
1340 end_center_line ^= 1;
1343 if (start_center_line)
1344 line1_xf = intersect_line_scanline(&start_point, ¢er_point, yf);
1346 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1348 if (end_center_line)
1349 line2_xf = intersect_line_scanline(&end_point, ¢er_point, yf);
1351 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1353 if (line1_xf < line2_xf)
1355 min_x = (INT)ceil(line1_xf);
1356 max_x = (INT)ceil(line2_xf);
1360 min_x = (INT)ceil(line2_xf);
1361 max_x = (INT)ceil(line1_xf);
1364 if (min_x < fill_area->X)
1365 min_x = fill_area->X;
1366 if (max_x > fill_area->X + fill_area->Width)
1367 max_x = fill_area->X + fill_area->Width;
1369 for (x=min_x; x<max_x; x++)
1374 if (start_color != end_color)
1376 REAL blend_amount, pdy, pdx;
1377 pdy = yf - center_point.Y;
1378 pdx = xf - center_point.X;
1379 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1380 outer_color = blend_colors(start_color, end_color, blend_amount);
1383 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1384 (end_point.X - start_point.X) * (yf - start_point.Y);
1386 distance = distance / center_distance;
1388 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1389 blend_colors(outer_color, fill->centercolor, distance);
1394 GdipDeletePath(flat_path);
1398 return NotImplemented;
1402 /* GdipDrawPie/GdipFillPie helper function */
1403 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1404 REAL height, REAL startAngle, REAL sweepAngle)
1411 ptf[1].X = x + width;
1412 ptf[1].Y = y + height;
1414 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1415 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1417 transform_and_round_points(graphics, pti, ptf, 4);
1419 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1420 pti[2].y, pti[3].x, pti[3].y);
1423 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1424 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1425 * should not be called on an hdc that has a path you care about. */
1426 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1427 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1429 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1430 GpMatrix *matrix = NULL;
1431 HBRUSH brush = NULL;
1433 PointF ptf[4], *custptf = NULL;
1434 POINT pt[4], *custpt = NULL;
1436 REAL theta, dsmall, dbig, dx, dy = 0.0;
1441 if((x1 == x2) && (y1 == y2))
1444 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1446 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1448 brush = CreateSolidBrush(color);
1449 lb.lbStyle = BS_SOLID;
1452 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1453 PS_JOIN_MITER, 1, &lb, 0,
1455 oldbrush = SelectObject(graphics->hdc, brush);
1456 oldpen = SelectObject(graphics->hdc, pen);
1463 case LineCapSquareAnchor:
1464 case LineCapDiamondAnchor:
1465 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1466 if(cap == LineCapDiamondAnchor){
1467 dsmall = cos(theta + M_PI_2) * size;
1468 dbig = sin(theta + M_PI_2) * size;
1471 dsmall = cos(theta + M_PI_4) * size;
1472 dbig = sin(theta + M_PI_4) * size;
1475 ptf[0].X = x2 - dsmall;
1476 ptf[1].X = x2 + dbig;
1478 ptf[0].Y = y2 - dbig;
1479 ptf[3].Y = y2 + dsmall;
1481 ptf[1].Y = y2 - dsmall;
1482 ptf[2].Y = y2 + dbig;
1484 ptf[3].X = x2 - dbig;
1485 ptf[2].X = x2 + dsmall;
1487 transform_and_round_points(graphics, pt, ptf, 4);
1488 Polygon(graphics->hdc, pt, 4);
1491 case LineCapArrowAnchor:
1492 size = size * 4.0 / sqrt(3.0);
1494 dx = cos(M_PI / 6.0 + theta) * size;
1495 dy = sin(M_PI / 6.0 + theta) * size;
1500 dx = cos(- M_PI / 6.0 + theta) * size;
1501 dy = sin(- M_PI / 6.0 + theta) * size;
1509 transform_and_round_points(graphics, pt, ptf, 3);
1510 Polygon(graphics->hdc, pt, 3);
1513 case LineCapRoundAnchor:
1514 dx = dy = ANCHOR_WIDTH * size / 2.0;
1521 transform_and_round_points(graphics, pt, ptf, 2);
1522 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1525 case LineCapTriangle:
1527 dx = cos(M_PI_2 + theta) * size;
1528 dy = sin(M_PI_2 + theta) * size;
1535 dx = cos(theta) * size;
1536 dy = sin(theta) * size;
1541 transform_and_round_points(graphics, pt, ptf, 3);
1542 Polygon(graphics->hdc, pt, 3);
1546 dx = dy = size / 2.0;
1553 dx = -cos(M_PI_2 + theta) * size;
1554 dy = -sin(M_PI_2 + theta) * size;
1561 transform_and_round_points(graphics, pt, ptf, 4);
1562 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1563 pt[2].y, pt[3].x, pt[3].y);
1570 count = custom->pathdata.Count;
1571 custptf = GdipAlloc(count * sizeof(PointF));
1572 custpt = GdipAlloc(count * sizeof(POINT));
1573 tp = GdipAlloc(count);
1575 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1578 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1580 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1581 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1583 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1584 GdipTransformMatrixPoints(matrix, custptf, count);
1586 transform_and_round_points(graphics, custpt, custptf, count);
1588 for(i = 0; i < count; i++)
1589 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1592 BeginPath(graphics->hdc);
1593 PolyDraw(graphics->hdc, custpt, tp, count);
1594 EndPath(graphics->hdc);
1595 StrokeAndFillPath(graphics->hdc);
1598 PolyDraw(graphics->hdc, custpt, tp, count);
1604 GdipDeleteMatrix(matrix);
1611 SelectObject(graphics->hdc, oldbrush);
1612 SelectObject(graphics->hdc, oldpen);
1613 DeleteObject(brush);
1618 /* Shortens the line by the given percent by changing x2, y2.
1619 * If percent is > 1.0 then the line will change direction.
1620 * If percent is negative it can lengthen the line. */
1621 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1623 REAL dist, theta, dx, dy;
1625 if((y1 == *y2) && (x1 == *x2))
1628 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1629 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1630 dx = cos(theta) * dist;
1631 dy = sin(theta) * dist;
1637 /* Shortens the line by the given amount by changing x2, y2.
1638 * If the amount is greater than the distance, the line will become length 0.
1639 * If the amount is negative, it can lengthen the line. */
1640 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1642 REAL dx, dy, percent;
1646 if(dx == 0 && dy == 0)
1649 percent = amt / sqrt(dx * dx + dy * dy);
1656 shorten_line_percent(x1, y1, x2, y2, percent);
1659 /* Draws lines between the given points, and if caps is true then draws an endcap
1660 * at the end of the last line. */
1661 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1662 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1665 GpPointF *ptcopy = NULL;
1666 GpStatus status = GenericError;
1671 pti = GdipAlloc(count * sizeof(POINT));
1672 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1674 if(!pti || !ptcopy){
1675 status = OutOfMemory;
1679 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1682 if(pen->endcap == LineCapArrowAnchor)
1683 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1684 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1685 else if((pen->endcap == LineCapCustom) && pen->customend)
1686 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1687 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1688 pen->customend->inset * pen->width);
1690 if(pen->startcap == LineCapArrowAnchor)
1691 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1692 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1693 else if((pen->startcap == LineCapCustom) && pen->customstart)
1694 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1695 &ptcopy[0].X, &ptcopy[0].Y,
1696 pen->customstart->inset * pen->width);
1698 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1699 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1700 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1701 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1704 transform_and_round_points(graphics, pti, ptcopy, count);
1706 if(Polyline(graphics->hdc, pti, count))
1716 /* Conducts a linear search to find the bezier points that will back off
1717 * the endpoint of the curve by a distance of amt. Linear search works
1718 * better than binary in this case because there are multiple solutions,
1719 * and binary searches often find a bad one. I don't think this is what
1720 * Windows does but short of rendering the bezier without GDI's help it's
1721 * the best we can do. If rev then work from the start of the passed points
1722 * instead of the end. */
1723 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1726 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1727 INT i, first = 0, second = 1, third = 2, fourth = 3;
1736 origx = pt[fourth].X;
1737 origy = pt[fourth].Y;
1738 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1740 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1741 /* reset bezier points to original values */
1742 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1743 /* Perform magic on bezier points. Order is important here.*/
1744 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1745 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1746 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1747 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1748 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1749 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1751 dx = pt[fourth].X - origx;
1752 dy = pt[fourth].Y - origy;
1754 diff = sqrt(dx * dx + dy * dy);
1755 percent += 0.0005 * amt;
1759 /* Draws bezier curves between given points, and if caps is true then draws an
1760 * endcap at the end of the last line. */
1761 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1762 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1766 GpStatus status = GenericError;
1771 pti = GdipAlloc(count * sizeof(POINT));
1772 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1774 if(!pti || !ptcopy){
1775 status = OutOfMemory;
1779 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1782 if(pen->endcap == LineCapArrowAnchor)
1783 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1784 else if((pen->endcap == LineCapCustom) && pen->customend)
1785 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1788 if(pen->startcap == LineCapArrowAnchor)
1789 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1790 else if((pen->startcap == LineCapCustom) && pen->customstart)
1791 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1793 /* the direction of the line cap is parallel to the direction at the
1794 * end of the bezier (which, if it has been shortened, is not the same
1795 * as the direction from pt[count-2] to pt[count-1]) */
1796 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1797 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1798 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1799 pt[count - 1].X, pt[count - 1].Y);
1801 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1802 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1803 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1806 transform_and_round_points(graphics, pti, ptcopy, count);
1808 PolyBezier(graphics->hdc, pti, count);
1819 /* Draws a combination of bezier curves and lines between points. */
1820 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1821 GDIPCONST BYTE * types, INT count, BOOL caps)
1823 POINT *pti = GdipAlloc(count * sizeof(POINT));
1824 BYTE *tp = GdipAlloc(count);
1825 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1827 GpStatus status = GenericError;
1833 if(!pti || !tp || !ptcopy){
1834 status = OutOfMemory;
1838 for(i = 1; i < count; i++){
1839 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1840 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1841 || !(types[i + 1] & PathPointTypeBezier)){
1842 ERR("Bad bezier points\n");
1849 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1851 /* If we are drawing caps, go through the points and adjust them accordingly,
1852 * and draw the caps. */
1854 switch(types[count - 1] & PathPointTypePathTypeMask){
1855 case PathPointTypeBezier:
1856 if(pen->endcap == LineCapArrowAnchor)
1857 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1858 else if((pen->endcap == LineCapCustom) && pen->customend)
1859 shorten_bezier_amt(&ptcopy[count - 4],
1860 pen->width * pen->customend->inset, FALSE);
1862 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1863 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1864 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1865 pt[count - 1].X, pt[count - 1].Y);
1868 case PathPointTypeLine:
1869 if(pen->endcap == LineCapArrowAnchor)
1870 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1871 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1873 else if((pen->endcap == LineCapCustom) && pen->customend)
1874 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1875 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1876 pen->customend->inset * pen->width);
1878 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1879 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1884 ERR("Bad path last point\n");
1888 /* Find start of points */
1889 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1890 == PathPointTypeStart); j++);
1892 switch(types[j] & PathPointTypePathTypeMask){
1893 case PathPointTypeBezier:
1894 if(pen->startcap == LineCapArrowAnchor)
1895 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1896 else if((pen->startcap == LineCapCustom) && pen->customstart)
1897 shorten_bezier_amt(&ptcopy[j - 1],
1898 pen->width * pen->customstart->inset, TRUE);
1900 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1901 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1902 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1903 pt[j - 1].X, pt[j - 1].Y);
1906 case PathPointTypeLine:
1907 if(pen->startcap == LineCapArrowAnchor)
1908 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1909 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1911 else if((pen->startcap == LineCapCustom) && pen->customstart)
1912 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1913 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1914 pen->customstart->inset * pen->width);
1916 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1917 pt[j].X, pt[j].Y, pt[j - 1].X,
1922 ERR("Bad path points\n");
1927 transform_and_round_points(graphics, pti, ptcopy, count);
1929 for(i = 0; i < count; i++){
1930 tp[i] = convert_path_point_type(types[i]);
1933 PolyDraw(graphics->hdc, pti, tp, count);
1945 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1949 BeginPath(graphics->hdc);
1950 result = draw_poly(graphics, NULL, path->pathdata.Points,
1951 path->pathdata.Types, path->pathdata.Count, FALSE);
1952 EndPath(graphics->hdc);
1956 typedef struct _GraphicsContainerItem {
1958 GraphicsContainer contid;
1960 SmoothingMode smoothing;
1961 CompositingQuality compqual;
1962 InterpolationMode interpolation;
1963 CompositingMode compmode;
1964 TextRenderingHint texthint;
1967 PixelOffsetMode pixeloffset;
1969 GpMatrix* worldtrans;
1971 INT origin_x, origin_y;
1972 } GraphicsContainerItem;
1974 static GpStatus init_container(GraphicsContainerItem** container,
1975 GDIPCONST GpGraphics* graphics){
1978 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1982 (*container)->contid = graphics->contid + 1;
1984 (*container)->smoothing = graphics->smoothing;
1985 (*container)->compqual = graphics->compqual;
1986 (*container)->interpolation = graphics->interpolation;
1987 (*container)->compmode = graphics->compmode;
1988 (*container)->texthint = graphics->texthint;
1989 (*container)->scale = graphics->scale;
1990 (*container)->unit = graphics->unit;
1991 (*container)->textcontrast = graphics->textcontrast;
1992 (*container)->pixeloffset = graphics->pixeloffset;
1993 (*container)->origin_x = graphics->origin_x;
1994 (*container)->origin_y = graphics->origin_y;
1996 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1998 GdipFree(*container);
2003 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2005 GdipDeleteMatrix((*container)->worldtrans);
2006 GdipFree(*container);
2014 static void delete_container(GraphicsContainerItem* container){
2015 GdipDeleteMatrix(container->worldtrans);
2016 GdipDeleteRegion(container->clip);
2017 GdipFree(container);
2020 static GpStatus restore_container(GpGraphics* graphics,
2021 GDIPCONST GraphicsContainerItem* container){
2026 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
2030 sts = GdipCloneRegion(container->clip, &newClip);
2032 GdipDeleteMatrix(newTrans);
2036 GdipDeleteMatrix(graphics->worldtrans);
2037 graphics->worldtrans = newTrans;
2039 GdipDeleteRegion(graphics->clip);
2040 graphics->clip = newClip;
2042 graphics->contid = container->contid - 1;
2044 graphics->smoothing = container->smoothing;
2045 graphics->compqual = container->compqual;
2046 graphics->interpolation = container->interpolation;
2047 graphics->compmode = container->compmode;
2048 graphics->texthint = container->texthint;
2049 graphics->scale = container->scale;
2050 graphics->unit = container->unit;
2051 graphics->textcontrast = container->textcontrast;
2052 graphics->pixeloffset = container->pixeloffset;
2053 graphics->origin_x = container->origin_x;
2054 graphics->origin_y = container->origin_y;
2059 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2065 if(graphics->hwnd) {
2066 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2067 return GenericError;
2069 rect->X = wnd_rect.left;
2070 rect->Y = wnd_rect.top;
2071 rect->Width = wnd_rect.right - wnd_rect.left;
2072 rect->Height = wnd_rect.bottom - wnd_rect.top;
2073 }else if (graphics->image){
2074 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2075 if (stat == Ok && unit != UnitPixel)
2076 FIXME("need to convert from unit %i\n", unit);
2080 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2081 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2087 /* on success, rgn will contain the region of the graphics object which
2088 * is visible after clipping has been applied */
2089 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2095 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2098 if((stat = GdipCreateRegion(&tmp)) != Ok)
2101 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2104 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2107 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2110 GdipDeleteRegion(tmp);
2114 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font, HFONT *hfont)
2116 HDC hdc = CreateCompatibleDC(0);
2118 REAL angle, rel_width, rel_height;
2120 HFONT unscaled_font;
2121 TEXTMETRICW textmet;
2130 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2131 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2132 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2133 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2134 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2135 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2137 get_log_fontW(font, graphics, &lfw);
2138 lfw.lfHeight = roundr(lfw.lfHeight * rel_height);
2139 unscaled_font = CreateFontIndirectW(&lfw);
2141 SelectObject(hdc, unscaled_font);
2142 GetTextMetricsW(hdc, &textmet);
2144 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width / rel_height);
2145 lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2147 *hfont = CreateFontIndirectW(&lfw);
2150 DeleteObject(unscaled_font);
2153 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2155 TRACE("(%p, %p)\n", hdc, graphics);
2157 return GdipCreateFromHDC2(hdc, NULL, graphics);
2160 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2164 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2166 if(hDevice != NULL) {
2167 FIXME("Don't know how to handle parameter hDevice\n");
2168 return NotImplemented;
2174 if(graphics == NULL)
2175 return InvalidParameter;
2177 *graphics = GdipAlloc(sizeof(GpGraphics));
2178 if(!*graphics) return OutOfMemory;
2180 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2181 GdipFree(*graphics);
2185 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2186 GdipFree((*graphics)->worldtrans);
2187 GdipFree(*graphics);
2191 (*graphics)->hdc = hdc;
2192 (*graphics)->hwnd = WindowFromDC(hdc);
2193 (*graphics)->owndc = FALSE;
2194 (*graphics)->smoothing = SmoothingModeDefault;
2195 (*graphics)->compqual = CompositingQualityDefault;
2196 (*graphics)->interpolation = InterpolationModeBilinear;
2197 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2198 (*graphics)->compmode = CompositingModeSourceOver;
2199 (*graphics)->unit = UnitDisplay;
2200 (*graphics)->scale = 1.0;
2201 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2202 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2203 (*graphics)->busy = FALSE;
2204 (*graphics)->textcontrast = 4;
2205 list_init(&(*graphics)->containers);
2206 (*graphics)->contid = 0;
2208 TRACE("<-- %p\n", *graphics);
2213 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2217 *graphics = GdipAlloc(sizeof(GpGraphics));
2218 if(!*graphics) return OutOfMemory;
2220 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2221 GdipFree(*graphics);
2225 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2226 GdipFree((*graphics)->worldtrans);
2227 GdipFree(*graphics);
2231 (*graphics)->hdc = NULL;
2232 (*graphics)->hwnd = NULL;
2233 (*graphics)->owndc = FALSE;
2234 (*graphics)->image = image;
2235 (*graphics)->smoothing = SmoothingModeDefault;
2236 (*graphics)->compqual = CompositingQualityDefault;
2237 (*graphics)->interpolation = InterpolationModeBilinear;
2238 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2239 (*graphics)->compmode = CompositingModeSourceOver;
2240 (*graphics)->unit = UnitDisplay;
2241 (*graphics)->scale = 1.0;
2242 (*graphics)->xres = image->xres;
2243 (*graphics)->yres = image->yres;
2244 (*graphics)->busy = FALSE;
2245 (*graphics)->textcontrast = 4;
2246 list_init(&(*graphics)->containers);
2247 (*graphics)->contid = 0;
2249 TRACE("<-- %p\n", *graphics);
2254 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2259 TRACE("(%p, %p)\n", hwnd, graphics);
2263 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2265 ReleaseDC(hwnd, hdc);
2269 (*graphics)->hwnd = hwnd;
2270 (*graphics)->owndc = TRUE;
2275 /* FIXME: no icm handling */
2276 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2278 TRACE("(%p, %p)\n", hwnd, graphics);
2280 return GdipCreateFromHWND(hwnd, graphics);
2283 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2284 GpMetafile **metafile)
2286 IStream *stream = NULL;
2288 ENHMETAHEADER *copy;
2289 GpStatus retval = Ok;
2291 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2293 if(!hemf || !metafile)
2294 return InvalidParameter;
2296 read = GetEnhMetaFileBits(hemf, 0, NULL);
2297 copy = GdipAlloc(read);
2298 GetEnhMetaFileBits(hemf, read, (BYTE *)copy);
2300 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
2301 ERR("could not make stream\n");
2303 retval = GenericError;
2307 *metafile = GdipAlloc(sizeof(GpMetafile));
2309 retval = OutOfMemory;
2313 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2314 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
2316 retval = GenericError;
2321 (*metafile)->image.type = ImageTypeMetafile;
2322 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
2323 (*metafile)->image.palette = NULL;
2324 (*metafile)->image.xres = (REAL)copy->szlDevice.cx;
2325 (*metafile)->image.yres = (REAL)copy->szlDevice.cy;
2326 (*metafile)->bounds.X = (REAL)copy->rclBounds.left;
2327 (*metafile)->bounds.Y = (REAL)copy->rclBounds.top;
2328 (*metafile)->bounds.Width = (REAL)(copy->rclBounds.right - copy->rclBounds.left);
2329 (*metafile)->bounds.Height = (REAL)(copy->rclBounds.bottom - copy->rclBounds.top);
2330 (*metafile)->unit = UnitPixel;
2333 DeleteEnhMetaFile(hemf);
2335 TRACE("<-- %p\n", *metafile);
2339 GdipFree(*metafile);
2340 IStream_Release(stream);
2344 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2345 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2350 GpStatus retval = Ok;
2352 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2354 if(!hwmf || !metafile || !placeable)
2355 return InvalidParameter;
2358 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2360 return GenericError;
2361 copy = GdipAlloc(read);
2362 GetMetaFileBitsEx(hwmf, read, copy);
2364 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2367 retval = GdipCreateMetafileFromEmf(hemf, FALSE, metafile);
2371 (*metafile)->image.xres = (REAL)placeable->Inch;
2372 (*metafile)->image.yres = (REAL)placeable->Inch;
2373 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2374 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2375 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2376 placeable->BoundingBox.Left);
2377 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2378 placeable->BoundingBox.Top);
2380 if (delete) DeleteMetaFile(hwmf);
2385 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2386 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2388 HMETAFILE hmf = GetMetaFileW(file);
2390 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2392 if(!hmf) return InvalidParameter;
2394 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2397 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2398 GpMetafile **metafile)
2400 FIXME("(%p, %p): stub\n", file, metafile);
2401 return NotImplemented;
2404 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2405 GpMetafile **metafile)
2407 FIXME("(%p, %p): stub\n", stream, metafile);
2408 return NotImplemented;
2411 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2412 UINT access, IStream **stream)
2417 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2419 if(!stream || !filename)
2420 return InvalidParameter;
2422 if(access & GENERIC_WRITE)
2423 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2424 else if(access & GENERIC_READ)
2425 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2427 return InvalidParameter;
2429 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2431 return hresult_to_status(ret);
2434 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2436 GraphicsContainerItem *cont, *next;
2438 TRACE("(%p)\n", graphics);
2440 if(!graphics) return InvalidParameter;
2441 if(graphics->busy) return ObjectBusy;
2443 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2445 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2451 ReleaseDC(graphics->hwnd, graphics->hdc);
2453 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2454 list_remove(&cont->entry);
2455 delete_container(cont);
2458 GdipDeleteRegion(graphics->clip);
2459 GdipDeleteMatrix(graphics->worldtrans);
2465 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2466 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2468 INT save_state, num_pts;
2469 GpPointF points[MAX_ARC_PTS];
2472 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2473 width, height, startAngle, sweepAngle);
2475 if(!graphics || !pen || width <= 0 || height <= 0)
2476 return InvalidParameter;
2483 FIXME("graphics object has no HDC\n");
2487 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2489 save_state = prepare_dc(graphics, pen);
2491 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2493 restore_dc(graphics, save_state);
2498 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2499 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2501 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2502 width, height, startAngle, sweepAngle);
2504 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2507 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2508 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2514 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2515 x2, y2, x3, y3, x4, y4);
2517 if(!graphics || !pen)
2518 return InvalidParameter;
2525 FIXME("graphics object has no HDC\n");
2538 save_state = prepare_dc(graphics, pen);
2540 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2542 restore_dc(graphics, save_state);
2547 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2548 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2554 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2555 x2, y2, x3, y3, x4, y4);
2557 if(!graphics || !pen)
2558 return InvalidParameter;
2565 FIXME("graphics object has no HDC\n");
2578 save_state = prepare_dc(graphics, pen);
2580 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2582 restore_dc(graphics, save_state);
2587 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2588 GDIPCONST GpPointF *points, INT count)
2593 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2595 if(!graphics || !pen || !points || (count <= 0))
2596 return InvalidParameter;
2601 for(i = 0; i < floor(count / 4); i++){
2602 ret = GdipDrawBezier(graphics, pen,
2603 points[4*i].X, points[4*i].Y,
2604 points[4*i + 1].X, points[4*i + 1].Y,
2605 points[4*i + 2].X, points[4*i + 2].Y,
2606 points[4*i + 3].X, points[4*i + 3].Y);
2614 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2615 GDIPCONST GpPoint *points, INT count)
2621 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2623 if(!graphics || !pen || !points || (count <= 0))
2624 return InvalidParameter;
2629 pts = GdipAlloc(sizeof(GpPointF) * count);
2633 for(i = 0; i < count; i++){
2634 pts[i].X = (REAL)points[i].X;
2635 pts[i].Y = (REAL)points[i].Y;
2638 ret = GdipDrawBeziers(graphics,pen,pts,count);
2645 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2646 GDIPCONST GpPointF *points, INT count)
2648 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2650 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2653 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2654 GDIPCONST GpPoint *points, INT count)
2656 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2658 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2661 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2662 GDIPCONST GpPointF *points, INT count, REAL tension)
2667 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2669 if(!graphics || !pen || !points || count <= 0)
2670 return InvalidParameter;
2675 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2678 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2680 GdipDeletePath(path);
2684 stat = GdipDrawPath(graphics, pen, path);
2686 GdipDeletePath(path);
2691 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2692 GDIPCONST GpPoint *points, INT count, REAL tension)
2698 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2700 if(!points || count <= 0)
2701 return InvalidParameter;
2703 ptf = GdipAlloc(sizeof(GpPointF)*count);
2707 for(i = 0; i < count; i++){
2708 ptf[i].X = (REAL)points[i].X;
2709 ptf[i].Y = (REAL)points[i].Y;
2712 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2719 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2720 GDIPCONST GpPointF *points, INT count)
2722 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2724 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2727 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2728 GDIPCONST GpPoint *points, INT count)
2734 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2737 return InvalidParameter;
2739 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2743 for(i = 0; i < count; i++){
2744 pointsF[i].X = (REAL)points[i].X;
2745 pointsF[i].Y = (REAL)points[i].Y;
2748 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2754 /* Approximates cardinal spline with Bezier curves. */
2755 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2756 GDIPCONST GpPointF *points, INT count, REAL tension)
2758 /* PolyBezier expects count*3-2 points. */
2759 INT i, len_pt = count*3-2, save_state;
2761 REAL x1, x2, y1, y2;
2764 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2766 if(!graphics || !pen)
2767 return InvalidParameter;
2773 return InvalidParameter;
2777 FIXME("graphics object has no HDC\n");
2781 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2785 tension = tension * TENSION_CONST;
2787 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2790 pt[0].X = points[0].X;
2791 pt[0].Y = points[0].Y;
2795 for(i = 0; i < count-2; i++){
2796 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2800 pt[3*i+3].X = points[i+1].X;
2801 pt[3*i+3].Y = points[i+1].Y;
2806 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2807 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2809 pt[len_pt-2].X = x1;
2810 pt[len_pt-2].Y = y1;
2811 pt[len_pt-1].X = points[count-1].X;
2812 pt[len_pt-1].Y = points[count-1].Y;
2814 save_state = prepare_dc(graphics, pen);
2816 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2819 restore_dc(graphics, save_state);
2824 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2825 GDIPCONST GpPoint *points, INT count, REAL tension)
2831 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2834 return InvalidParameter;
2836 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2840 for(i = 0; i < count; i++){
2841 pointsF[i].X = (REAL)points[i].X;
2842 pointsF[i].Y = (REAL)points[i].Y;
2845 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2851 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2852 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2855 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2857 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2858 return InvalidParameter;
2861 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2864 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2865 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2868 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2874 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2875 return InvalidParameter;
2878 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2881 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2882 REAL y, REAL width, REAL height)
2888 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2890 if(!graphics || !pen)
2891 return InvalidParameter;
2898 FIXME("graphics object has no HDC\n");
2904 ptf[1].X = x + width;
2905 ptf[1].Y = y + height;
2907 save_state = prepare_dc(graphics, pen);
2908 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2910 transform_and_round_points(graphics, pti, ptf, 2);
2912 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2914 restore_dc(graphics, save_state);
2919 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2920 INT y, INT width, INT height)
2922 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2924 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2928 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2933 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2935 if(!graphics || !image)
2936 return InvalidParameter;
2938 GdipGetImageWidth(image, &width);
2939 GdipGetImageHeight(image, &height);
2941 /* FIXME: we should use the graphics and image dpi, somehow */
2943 points[0].X = points[2].X = x;
2944 points[0].Y = points[1].Y = y;
2945 points[1].X = x + width;
2946 points[2].Y = y + height;
2948 return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
2949 UnitPixel, NULL, NULL, NULL);
2952 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2955 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2957 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2960 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2961 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2965 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2967 points[0].X = points[2].X = x;
2968 points[0].Y = points[1].Y = y;
2970 /* FIXME: convert image coordinates to Graphics coordinates? */
2971 points[1].X = x + srcwidth;
2972 points[2].Y = y + srcheight;
2974 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2975 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2978 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2979 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2982 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2985 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2986 GDIPCONST GpPointF *dstpoints, INT count)
2990 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2993 return InvalidParameter;
2995 GdipGetImageWidth(image, &width);
2996 GdipGetImageHeight(image, &height);
2998 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2999 width, height, UnitPixel, NULL, NULL, NULL);
3002 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3003 GDIPCONST GpPoint *dstpoints, INT count)
3007 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3009 if (count != 3 || !dstpoints)
3010 return InvalidParameter;
3012 ptf[0].X = (REAL)dstpoints[0].X;
3013 ptf[0].Y = (REAL)dstpoints[0].Y;
3014 ptf[1].X = (REAL)dstpoints[1].X;
3015 ptf[1].Y = (REAL)dstpoints[1].Y;
3016 ptf[2].X = (REAL)dstpoints[2].X;
3017 ptf[2].Y = (REAL)dstpoints[2].Y;
3019 return GdipDrawImagePoints(graphics, image, ptf, count);
3022 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3023 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3024 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3025 DrawImageAbort callback, VOID * callbackData)
3032 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3033 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3037 return NotImplemented;
3039 if(!graphics || !image || !points || count != 3)
3040 return InvalidParameter;
3042 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3043 debugstr_pointf(&points[2]));
3045 memcpy(ptf, points, 3 * sizeof(GpPointF));
3046 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3047 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3048 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3050 transform_and_round_points(graphics, pti, ptf, 4);
3056 FIXME("graphics object has no HDC\n");
3059 /* FIXME: partially implemented (only works for rectangular parallelograms) */
3060 if(srcUnit == UnitInch)
3061 dx = dy = (REAL) INCH_HIMETRIC;
3062 else if(srcUnit == UnitPixel){
3063 dx = ((REAL) INCH_HIMETRIC) /
3064 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
3065 dy = ((REAL) INCH_HIMETRIC) /
3066 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
3069 return NotImplemented;
3071 if(IPicture_Render(image->picture, graphics->hdc,
3072 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3073 srcx * dx, srcy * dy,
3074 srcwidth * dx, srcheight * dy,
3077 callback(callbackData);
3078 return GenericError;
3081 else if (image->type == ImageTypeBitmap)
3083 GpBitmap* bitmap = (GpBitmap*)image;
3086 if (srcUnit == UnitInch)
3087 dx = dy = 96.0; /* FIXME: use the image resolution */
3088 else if (srcUnit == UnitPixel)
3091 return NotImplemented;
3095 srcwidth = srcwidth * dx;
3096 srcheight = srcheight * dy;
3098 if (imageAttributes ||
3099 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3100 !((GpBitmap*)image)->hbitmap ||
3101 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3102 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3103 srcx < 0 || srcy < 0 ||
3104 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3111 int i, x, y, src_stride, dst_stride;
3112 GpMatrix *dst_to_src;
3113 REAL m11, m12, m21, m22, mdx, mdy;
3114 LPBYTE src_data, dst_data;
3115 BitmapData lockeddata;
3116 InterpolationMode interpolation = graphics->interpolation;
3117 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3118 REAL x_dx, x_dy, y_dx, y_dy;
3119 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3121 if (!imageAttributes)
3122 imageAttributes = &defaultImageAttributes;
3124 dst_area.left = dst_area.right = pti[0].x;
3125 dst_area.top = dst_area.bottom = pti[0].y;
3128 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3129 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3130 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3131 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3134 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3135 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3136 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3137 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3138 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3139 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3141 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
3142 if (stat != Ok) return stat;
3144 stat = GdipInvertMatrix(dst_to_src);
3147 GdipDeleteMatrix(dst_to_src);
3151 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3154 GdipDeleteMatrix(dst_to_src);
3158 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3160 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3161 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3163 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3167 GdipDeleteMatrix(dst_to_src);
3170 src_stride = sizeof(ARGB) * src_area.Width;
3172 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3173 lockeddata.Width = src_area.Width;
3174 lockeddata.Height = src_area.Height;
3175 lockeddata.Stride = src_stride;
3176 lockeddata.PixelFormat = PixelFormat32bppARGB;
3177 lockeddata.Scan0 = src_data;
3179 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3180 PixelFormat32bppARGB, &lockeddata);
3183 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3187 if (src_data != dst_data)
3190 GdipDeleteMatrix(dst_to_src);
3194 apply_image_attributes(imageAttributes, src_data,
3195 src_area.Width, src_area.Height,
3196 src_stride, ColorAdjustTypeBitmap);
3198 /* Transform the bits as needed to the destination. */
3199 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
3201 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3202 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3203 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3204 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3206 for (x=dst_area.left; x<dst_area.right; x++)
3208 for (y=dst_area.top; y<dst_area.bottom; y++)
3210 GpPointF src_pointf;
3213 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3214 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3216 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3218 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3219 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
3225 GdipDeleteMatrix(dst_to_src);
3229 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3230 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3239 int temp_hdc=0, temp_bitmap=0;
3240 HBITMAP hbitmap, old_hbm=NULL;
3242 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3243 bitmap->format == PixelFormat24bppRGB ||
3244 bitmap->format == PixelFormat32bppRGB ||
3245 bitmap->format == PixelFormat32bppPARGB))
3247 BITMAPINFOHEADER bih;
3249 PixelFormat dst_format;
3251 /* we can't draw a bitmap of this format directly */
3252 hdc = CreateCompatibleDC(0);
3256 bih.biSize = sizeof(BITMAPINFOHEADER);
3257 bih.biWidth = bitmap->width;
3258 bih.biHeight = -bitmap->height;
3260 bih.biBitCount = 32;
3261 bih.biCompression = BI_RGB;
3262 bih.biSizeImage = 0;
3263 bih.biXPelsPerMeter = 0;
3264 bih.biYPelsPerMeter = 0;
3266 bih.biClrImportant = 0;
3268 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3269 (void**)&temp_bits, NULL, 0);
3271 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3272 dst_format = PixelFormat32bppPARGB;
3274 dst_format = PixelFormat32bppRGB;
3276 convert_pixels(bitmap->width, bitmap->height,
3277 bitmap->width*4, temp_bits, dst_format,
3278 bitmap->stride, bitmap->bits, bitmap->format,
3279 bitmap->image.palette);
3283 hbitmap = bitmap->hbitmap;
3285 temp_hdc = (hdc == 0);
3290 if (!hdc) hdc = CreateCompatibleDC(0);
3291 old_hbm = SelectObject(hdc, hbitmap);
3294 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3296 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3297 hdc, srcx, srcy, srcwidth, srcheight);
3301 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3302 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3307 SelectObject(hdc, old_hbm);
3312 DeleteObject(hbitmap);
3317 ERR("GpImage with no IPicture or HBITMAP?!\n");
3318 return NotImplemented;
3324 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3325 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3326 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3327 DrawImageAbort callback, VOID * callbackData)
3329 GpPointF pointsF[3];
3332 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3333 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3336 if(!points || count!=3)
3337 return InvalidParameter;
3339 for(i = 0; i < count; i++){
3340 pointsF[i].X = (REAL)points[i].X;
3341 pointsF[i].Y = (REAL)points[i].Y;
3344 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3345 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3346 callback, callbackData);
3349 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3350 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3351 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3352 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3353 VOID * callbackData)
3357 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3358 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3359 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3363 points[1].X = dstx + dstwidth;
3366 points[2].Y = dsty + dstheight;
3368 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3369 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3372 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3373 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3374 INT srcwidth, INT srcheight, GpUnit srcUnit,
3375 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3376 VOID * callbackData)
3380 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3381 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3382 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3386 points[1].X = dstx + dstwidth;
3389 points[2].Y = dsty + dstheight;
3391 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3392 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3395 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3396 REAL x, REAL y, REAL width, REAL height)
3402 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3404 if(!graphics || !image)
3405 return InvalidParameter;
3407 ret = GdipGetImageBounds(image, &bounds, &unit);
3411 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3412 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3413 unit, NULL, NULL, NULL);
3416 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3417 INT x, INT y, INT width, INT height)
3419 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3421 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3424 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3425 REAL y1, REAL x2, REAL y2)
3431 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3433 if(!pen || !graphics)
3434 return InvalidParameter;
3441 FIXME("graphics object has no HDC\n");
3450 save_state = prepare_dc(graphics, pen);
3452 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3454 restore_dc(graphics, save_state);
3459 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3460 INT y1, INT x2, INT y2)
3466 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3468 if(!pen || !graphics)
3469 return InvalidParameter;
3476 FIXME("graphics object has no HDC\n");
3485 save_state = prepare_dc(graphics, pen);
3487 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3489 restore_dc(graphics, save_state);
3494 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3495 GpPointF *points, INT count)
3500 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3502 if(!pen || !graphics || (count < 2))
3503 return InvalidParameter;
3510 FIXME("graphics object has no HDC\n");
3514 save_state = prepare_dc(graphics, pen);
3516 retval = draw_polyline(graphics, pen, points, count, TRUE);
3518 restore_dc(graphics, save_state);
3523 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3524 GpPoint *points, INT count)
3528 GpPointF *ptf = NULL;
3531 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3533 if(!pen || !graphics || (count < 2))
3534 return InvalidParameter;
3541 FIXME("graphics object has no HDC\n");
3545 ptf = GdipAlloc(count * sizeof(GpPointF));
3546 if(!ptf) return OutOfMemory;
3548 for(i = 0; i < count; i ++){
3549 ptf[i].X = (REAL) points[i].X;
3550 ptf[i].Y = (REAL) points[i].Y;
3553 save_state = prepare_dc(graphics, pen);
3555 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3557 restore_dc(graphics, save_state);
3563 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3568 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3570 if(!pen || !graphics)
3571 return InvalidParameter;
3578 FIXME("graphics object has no HDC\n");
3582 save_state = prepare_dc(graphics, pen);
3584 retval = draw_poly(graphics, pen, path->pathdata.Points,
3585 path->pathdata.Types, path->pathdata.Count, TRUE);
3587 restore_dc(graphics, save_state);
3592 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3593 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3597 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3598 width, height, startAngle, sweepAngle);
3600 if(!graphics || !pen)
3601 return InvalidParameter;
3608 FIXME("graphics object has no HDC\n");
3612 save_state = prepare_dc(graphics, pen);
3613 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3615 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3617 restore_dc(graphics, save_state);
3622 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3623 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3625 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3626 width, height, startAngle, sweepAngle);
3628 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3631 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3632 REAL y, REAL width, REAL height)
3638 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3640 if(!pen || !graphics)
3641 return InvalidParameter;
3648 FIXME("graphics object has no HDC\n");
3654 ptf[1].X = x + width;
3656 ptf[2].X = x + width;
3657 ptf[2].Y = y + height;
3659 ptf[3].Y = y + height;
3661 save_state = prepare_dc(graphics, pen);
3662 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3664 transform_and_round_points(graphics, pti, ptf, 4);
3665 Polygon(graphics->hdc, pti, 4);
3667 restore_dc(graphics, save_state);
3672 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3673 INT y, INT width, INT height)
3675 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3677 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3680 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3681 GDIPCONST GpRectF* rects, INT count)
3687 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3689 if(!graphics || !pen || !rects || count < 1)
3690 return InvalidParameter;
3697 FIXME("graphics object has no HDC\n");
3701 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3702 pti = GdipAlloc(4 * count * sizeof(POINT));
3710 for(i = 0; i < count; i++){
3711 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3712 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3713 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3714 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3717 save_state = prepare_dc(graphics, pen);
3718 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3720 transform_and_round_points(graphics, pti, ptf, 4 * count);
3722 for(i = 0; i < count; i++)
3723 Polygon(graphics->hdc, &pti[4 * i], 4);
3725 restore_dc(graphics, save_state);
3733 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3734 GDIPCONST GpRect* rects, INT count)
3740 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3742 if(!rects || count<=0)
3743 return InvalidParameter;
3745 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3749 for(i = 0;i < count;i++){
3750 rectsF[i].X = (REAL)rects[i].X;
3751 rectsF[i].Y = (REAL)rects[i].Y;
3752 rectsF[i].Width = (REAL)rects[i].Width;
3753 rectsF[i].Height = (REAL)rects[i].Height;
3756 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3762 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3763 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3768 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3769 count, tension, fill);
3771 if(!graphics || !brush || !points)
3772 return InvalidParameter;
3777 if(count == 1) /* Do nothing */
3780 stat = GdipCreatePath(fill, &path);
3784 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3786 GdipDeletePath(path);
3790 stat = GdipFillPath(graphics, brush, path);
3792 GdipDeletePath(path);
3796 GdipDeletePath(path);
3801 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3802 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3808 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3809 count, tension, fill);
3811 if(!points || count == 0)
3812 return InvalidParameter;
3814 if(count == 1) /* Do nothing */
3817 ptf = GdipAlloc(sizeof(GpPointF)*count);
3821 for(i = 0;i < count;i++){
3822 ptf[i].X = (REAL)points[i].X;
3823 ptf[i].Y = (REAL)points[i].Y;
3826 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3833 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3834 GDIPCONST GpPointF *points, INT count)
3836 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3837 return GdipFillClosedCurve2(graphics, brush, points, count,
3838 0.5f, FillModeAlternate);
3841 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3842 GDIPCONST GpPoint *points, INT count)
3844 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3845 return GdipFillClosedCurve2I(graphics, brush, points, count,
3846 0.5f, FillModeAlternate);
3849 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3850 REAL y, REAL width, REAL height)
3855 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3857 if(!graphics || !brush)
3858 return InvalidParameter;
3863 stat = GdipCreatePath(FillModeAlternate, &path);
3867 stat = GdipAddPathEllipse(path, x, y, width, height);
3870 stat = GdipFillPath(graphics, brush, path);
3872 GdipDeletePath(path);
3878 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3879 INT y, INT width, INT height)
3881 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3883 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3886 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3891 if(!graphics->hdc || !brush_can_fill_path(brush))
3892 return NotImplemented;
3894 save_state = SaveDC(graphics->hdc);
3895 EndPath(graphics->hdc);
3896 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3899 BeginPath(graphics->hdc);
3900 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3901 path->pathdata.Types, path->pathdata.Count, FALSE);
3906 EndPath(graphics->hdc);
3907 brush_fill_path(graphics, brush);
3912 RestoreDC(graphics->hdc, save_state);
3917 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3922 if (!brush_can_fill_pixels(brush))
3923 return NotImplemented;
3925 /* FIXME: This could probably be done more efficiently without regions. */
3927 stat = GdipCreateRegionPath(path, &rgn);
3931 stat = GdipFillRegion(graphics, brush, rgn);
3933 GdipDeleteRegion(rgn);
3939 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3941 GpStatus stat = NotImplemented;
3943 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3945 if(!brush || !graphics || !path)
3946 return InvalidParameter;
3951 if (!graphics->image)
3952 stat = GDI32_GdipFillPath(graphics, brush, path);
3954 if (stat == NotImplemented)
3955 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3957 if (stat == NotImplemented)
3959 FIXME("Not implemented for brushtype %i\n", brush->bt);
3966 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3967 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3972 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3973 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3975 if(!graphics || !brush)
3976 return InvalidParameter;
3981 stat = GdipCreatePath(FillModeAlternate, &path);
3985 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3988 stat = GdipFillPath(graphics, brush, path);
3990 GdipDeletePath(path);
3996 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3997 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3999 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4000 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4002 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4005 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4006 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4011 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4013 if(!graphics || !brush || !points || !count)
4014 return InvalidParameter;
4019 stat = GdipCreatePath(fillMode, &path);
4023 stat = GdipAddPathPolygon(path, points, count);
4026 stat = GdipFillPath(graphics, brush, path);
4028 GdipDeletePath(path);
4034 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4035 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4040 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4042 if(!graphics || !brush || !points || !count)
4043 return InvalidParameter;
4048 stat = GdipCreatePath(fillMode, &path);
4052 stat = GdipAddPathPolygonI(path, points, count);
4055 stat = GdipFillPath(graphics, brush, path);
4057 GdipDeletePath(path);
4063 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4064 GDIPCONST GpPointF *points, INT count)
4066 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4068 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4071 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4072 GDIPCONST GpPoint *points, INT count)
4074 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4076 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4079 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4080 REAL x, REAL y, REAL width, REAL height)
4085 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4087 if(!graphics || !brush)
4088 return InvalidParameter;
4093 stat = GdipCreatePath(FillModeAlternate, &path);
4097 stat = GdipAddPathRectangle(path, x, y, width, height);
4100 stat = GdipFillPath(graphics, brush, path);
4102 GdipDeletePath(path);
4108 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4109 INT x, INT y, INT width, INT height)
4111 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4113 return GdipFillRectangle(graphics, brush, x, y, width, height);
4116 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4122 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4125 return InvalidParameter;
4127 for(i = 0; i < count; i++){
4128 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4129 if(ret != Ok) return ret;
4135 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4142 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4144 if(!rects || count <= 0)
4145 return InvalidParameter;
4147 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4151 for(i = 0; i < count; i++){
4152 rectsF[i].X = (REAL)rects[i].X;
4153 rectsF[i].Y = (REAL)rects[i].Y;
4154 rectsF[i].X = (REAL)rects[i].Width;
4155 rectsF[i].Height = (REAL)rects[i].Height;
4158 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4164 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4172 if(!graphics->hdc || !brush_can_fill_path(brush))
4173 return NotImplemented;
4175 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4179 save_state = SaveDC(graphics->hdc);
4180 EndPath(graphics->hdc);
4182 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4184 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4186 BeginPath(graphics->hdc);
4187 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4188 EndPath(graphics->hdc);
4190 brush_fill_path(graphics, brush);
4193 RestoreDC(graphics->hdc, save_state);
4200 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4204 GpRegion *temp_region;
4205 GpMatrix *world_to_device;
4206 GpRectF graphics_bounds;
4210 GpRect gp_bound_rect;
4212 if (!brush_can_fill_pixels(brush))
4213 return NotImplemented;
4215 stat = get_graphics_bounds(graphics, &graphics_bounds);
4218 stat = GdipCloneRegion(region, &temp_region);
4222 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4223 CoordinateSpaceWorld, &world_to_device);
4227 stat = GdipTransformRegion(temp_region, world_to_device);
4229 GdipDeleteMatrix(world_to_device);
4233 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4236 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4238 GdipDeleteRegion(temp_region);
4241 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4243 DeleteObject(hregion);
4249 gp_bound_rect.X = bound_rect.left;
4250 gp_bound_rect.Y = bound_rect.top;
4251 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4252 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4254 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4260 stat = brush_fill_pixels(graphics, brush, pixel_data,
4261 &gp_bound_rect, gp_bound_rect.Width);
4264 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4265 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4266 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4268 GdipFree(pixel_data);
4271 DeleteObject(hregion);
4277 /*****************************************************************************
4278 * GdipFillRegion [GDIPLUS.@]
4280 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4283 GpStatus stat = NotImplemented;
4285 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4287 if (!(graphics && brush && region))
4288 return InvalidParameter;
4293 if (!graphics->image)
4294 stat = GDI32_GdipFillRegion(graphics, brush, region);
4296 if (stat == NotImplemented)
4297 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4299 if (stat == NotImplemented)
4301 FIXME("not implemented for brushtype %i\n", brush->bt);
4308 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4310 TRACE("(%p,%u)\n", graphics, intention);
4313 return InvalidParameter;
4318 /* We have no internal operation queue, so there's no need to clear it. */
4326 /*****************************************************************************
4327 * GdipGetClipBounds [GDIPLUS.@]
4329 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4331 TRACE("(%p, %p)\n", graphics, rect);
4334 return InvalidParameter;
4339 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4342 /*****************************************************************************
4343 * GdipGetClipBoundsI [GDIPLUS.@]
4345 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4347 TRACE("(%p, %p)\n", graphics, rect);
4350 return InvalidParameter;
4355 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4358 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4359 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4360 CompositingMode *mode)
4362 TRACE("(%p, %p)\n", graphics, mode);
4364 if(!graphics || !mode)
4365 return InvalidParameter;
4370 *mode = graphics->compmode;
4375 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4376 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4377 CompositingQuality *quality)
4379 TRACE("(%p, %p)\n", graphics, quality);
4381 if(!graphics || !quality)
4382 return InvalidParameter;
4387 *quality = graphics->compqual;
4392 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4393 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4394 InterpolationMode *mode)
4396 TRACE("(%p, %p)\n", graphics, mode);
4398 if(!graphics || !mode)
4399 return InvalidParameter;
4404 *mode = graphics->interpolation;
4409 /* FIXME: Need to handle color depths less than 24bpp */
4410 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4412 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4414 if(!graphics || !argb)
4415 return InvalidParameter;
4423 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4425 TRACE("(%p, %p)\n", graphics, scale);
4427 if(!graphics || !scale)
4428 return InvalidParameter;
4433 *scale = graphics->scale;
4438 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4440 TRACE("(%p, %p)\n", graphics, unit);
4442 if(!graphics || !unit)
4443 return InvalidParameter;
4448 *unit = graphics->unit;
4453 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4454 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4457 TRACE("(%p, %p)\n", graphics, mode);
4459 if(!graphics || !mode)
4460 return InvalidParameter;
4465 *mode = graphics->pixeloffset;
4470 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4471 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4473 TRACE("(%p, %p)\n", graphics, mode);
4475 if(!graphics || !mode)
4476 return InvalidParameter;
4481 *mode = graphics->smoothing;
4486 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4488 TRACE("(%p, %p)\n", graphics, contrast);
4490 if(!graphics || !contrast)
4491 return InvalidParameter;
4493 *contrast = graphics->textcontrast;
4498 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4499 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4500 TextRenderingHint *hint)
4502 TRACE("(%p, %p)\n", graphics, hint);
4504 if(!graphics || !hint)
4505 return InvalidParameter;
4510 *hint = graphics->texthint;
4515 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4520 TRACE("(%p, %p)\n", graphics, rect);
4522 if(!graphics || !rect)
4523 return InvalidParameter;
4528 /* intersect window and graphics clipping regions */
4529 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4532 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4535 /* get bounds of the region */
4536 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4539 GdipDeleteRegion(clip_rgn);
4544 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4549 TRACE("(%p, %p)\n", graphics, rect);
4551 if(!graphics || !rect)
4552 return InvalidParameter;
4554 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4556 rect->X = roundr(rectf.X);
4557 rect->Y = roundr(rectf.Y);
4558 rect->Width = roundr(rectf.Width);
4559 rect->Height = roundr(rectf.Height);
4565 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4567 TRACE("(%p, %p)\n", graphics, matrix);
4569 if(!graphics || !matrix)
4570 return InvalidParameter;
4575 *matrix = *graphics->worldtrans;
4579 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4585 TRACE("(%p, %x)\n", graphics, color);
4588 return InvalidParameter;
4593 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4596 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4597 GdipDeleteBrush((GpBrush*)brush);
4601 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4602 wnd_rect.Width, wnd_rect.Height);
4604 GdipDeleteBrush((GpBrush*)brush);
4609 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4611 TRACE("(%p, %p)\n", graphics, res);
4613 if(!graphics || !res)
4614 return InvalidParameter;
4616 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4619 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4625 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4627 if(!graphics || !result)
4628 return InvalidParameter;
4635 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4636 CoordinateSpaceWorld, &pt, 1)) != Ok)
4639 if((stat = GdipCreateRegion(&rgn)) != Ok)
4642 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4645 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4648 GdipDeleteRegion(rgn);
4652 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4654 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4657 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4663 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4665 if(!graphics || !result)
4666 return InvalidParameter;
4673 pts[1].X = x + width;
4674 pts[1].Y = y + height;
4676 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4677 CoordinateSpaceWorld, pts, 2)) != Ok)
4680 pts[1].X -= pts[0].X;
4681 pts[1].Y -= pts[0].Y;
4683 if((stat = GdipCreateRegion(&rgn)) != Ok)
4686 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4689 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4692 GdipDeleteRegion(rgn);
4696 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4698 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4701 GpStatus gdip_format_string(HDC hdc,
4702 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4703 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4704 gdip_format_string_callback callback, void *user_data)
4707 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4708 nheight, lineend, lineno = 0;
4710 StringAlignment halign;
4713 HotkeyPrefix hkprefix;
4714 INT *hotkeyprefix_offsets=NULL;
4715 INT hotkeyprefix_count=0;
4716 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4719 if(length == -1) length = lstrlenW(string);
4721 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4722 if(!stringdup) return OutOfMemory;
4724 nwidth = roundr(rect->Width);
4725 nheight = roundr(rect->Height);
4727 if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
4728 if (rect->Height >= INT_MAX || rect->Height < 0.5) nheight = INT_MAX;
4731 hkprefix = format->hkprefix;
4733 hkprefix = HotkeyPrefixNone;
4735 if (hkprefix == HotkeyPrefixShow)
4737 for (i=0; i<length; i++)
4739 if (string[i] == '&')
4740 hotkeyprefix_count++;
4744 if (hotkeyprefix_count)
4745 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4747 hotkeyprefix_count = 0;
4749 for(i = 0, j = 0; i < length; i++){
4750 /* FIXME: This makes the indexes passed to callback inaccurate. */
4751 if(!isprintW(string[i]) && (string[i] != '\n'))
4754 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4755 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4756 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4764 stringdup[j] = string[i];
4770 if (format) halign = format->align;
4771 else halign = StringAlignmentNear;
4773 while(sum < length){
4774 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4775 nwidth, &fit, NULL, &size);
4781 for(lret = 0; lret < fit; lret++)
4782 if(*(stringdup + sum + lret) == '\n')
4785 /* Line break code (may look strange, but it imitates windows). */
4787 lineend = fit = lret; /* this is not an off-by-one error */
4788 else if(fit < (length - sum)){
4789 if(*(stringdup + sum + fit) == ' ')
4790 while(*(stringdup + sum + fit) == ' ')
4793 while(*(stringdup + sum + fit - 1) != ' '){
4796 if(*(stringdup + sum + fit) == '\t')
4805 while(*(stringdup + sum + lineend - 1) == ' ' ||
4806 *(stringdup + sum + lineend - 1) == '\t')
4812 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4813 nwidth, &j, NULL, &size);
4815 bounds.Width = size.cx;
4817 if(height + size.cy > nheight)
4818 bounds.Height = nheight - (height + size.cy);
4820 bounds.Height = size.cy;
4822 bounds.Y = rect->Y + height;
4826 case StringAlignmentNear:
4830 case StringAlignmentCenter:
4831 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4833 case StringAlignmentFar:
4834 bounds.X = rect->X + rect->Width - bounds.Width;
4838 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4839 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4842 stat = callback(hdc, stringdup, sum, lineend,
4843 font, rect, format, lineno, &bounds,
4844 &hotkeyprefix_offsets[hotkeyprefix_pos],
4845 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4850 sum += fit + (lret < fitcpy ? 1 : 0);
4854 hotkeyprefix_pos = hotkeyprefix_end_pos;
4856 if(height > nheight)
4859 /* Stop if this was a linewrap (but not if it was a linebreak). */
4860 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4864 GdipFree(stringdup);
4865 GdipFree(hotkeyprefix_offsets);
4870 struct measure_ranges_args {
4874 static GpStatus measure_ranges_callback(HDC hdc,
4875 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4876 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4877 INT lineno, const RectF *bounds, INT *underlined_indexes,
4878 INT underlined_index_count, void *user_data)
4882 struct measure_ranges_args *args = user_data;
4884 for (i=0; i<format->range_count; i++)
4886 INT range_start = max(index, format->character_ranges[i].First);
4887 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4888 if (range_start < range_end)
4893 range_rect.Y = bounds->Y;
4894 range_rect.Height = bounds->Height;
4896 GetTextExtentExPointW(hdc, string + index, range_start - index,
4897 INT_MAX, NULL, NULL, &range_size);
4898 range_rect.X = bounds->X + range_size.cx;
4900 GetTextExtentExPointW(hdc, string + index, range_end - index,
4901 INT_MAX, NULL, NULL, &range_size);
4902 range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
4904 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4913 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4914 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4915 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4916 INT regionCount, GpRegion** regions)
4922 struct measure_ranges_args args;
4923 HDC hdc, temp_hdc=NULL;
4925 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4926 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4928 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4929 return InvalidParameter;
4931 if (regionCount < stringFormat->range_count)
4932 return InvalidParameter;
4934 get_log_fontW(font, graphics, &lfw);
4938 hdc = temp_hdc = CreateCompatibleDC(0);
4939 if (!temp_hdc) return OutOfMemory;
4942 hdc = graphics->hdc;
4944 if (stringFormat->attr)
4945 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4947 oldfont = SelectObject(hdc, CreateFontIndirectW(&lfw));
4949 for (i=0; i<stringFormat->range_count; i++)
4951 stat = GdipSetEmpty(regions[i]);
4956 args.regions = regions;
4958 stat = gdip_format_string(hdc, string, length, font, layoutRect, stringFormat,
4959 measure_ranges_callback, &args);
4961 DeleteObject(SelectObject(hdc, oldfont));
4969 struct measure_string_args {
4971 INT *codepointsfitted;
4973 REAL rel_width, rel_height;
4976 static GpStatus measure_string_callback(HDC hdc,
4977 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4978 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4979 INT lineno, const RectF *bounds, INT *underlined_indexes,
4980 INT underlined_index_count, void *user_data)
4982 struct measure_string_args *args = user_data;
4983 REAL new_width, new_height;
4985 new_width = bounds->Width / args->rel_width;
4986 new_height = (bounds->Height + bounds->Y - args->bounds->Y) / args->rel_height;
4988 if (new_width > args->bounds->Width)
4989 args->bounds->Width = new_width;
4991 if (new_height > args->bounds->Height)
4992 args->bounds->Height = new_height;
4994 if (args->codepointsfitted)
4995 *args->codepointsfitted = index + length;
4997 if (args->linesfilled)
4998 (*args->linesfilled)++;
5003 /* Find the smallest rectangle that bounds the text when it is printed in rect
5004 * according to the format options listed in format. If rect has 0 width and
5005 * height, then just find the smallest rectangle that bounds the text when it's
5006 * printed at location (rect->X, rect-Y). */
5007 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5008 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5009 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5010 INT *codepointsfitted, INT *linesfilled)
5012 HFONT oldfont, gdifont;
5013 struct measure_string_args args;
5014 HDC temp_hdc=NULL, hdc;
5017 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5018 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5019 bounds, codepointsfitted, linesfilled);
5021 if(!graphics || !string || !font || !rect || !bounds)
5022 return InvalidParameter;
5026 hdc = temp_hdc = CreateCompatibleDC(0);
5027 if (!temp_hdc) return OutOfMemory;
5030 hdc = graphics->hdc;
5032 if(linesfilled) *linesfilled = 0;
5033 if(codepointsfitted) *codepointsfitted = 0;
5036 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5044 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5045 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5046 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5047 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5048 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5050 get_font_hfont(graphics, font, &gdifont);
5051 oldfont = SelectObject(hdc, gdifont);
5053 bounds->X = rect->X;
5054 bounds->Y = rect->Y;
5055 bounds->Width = 0.0;
5056 bounds->Height = 0.0;
5058 args.bounds = bounds;
5059 args.codepointsfitted = codepointsfitted;
5060 args.linesfilled = linesfilled;
5062 gdip_format_string(hdc, string, length, font, rect, format,
5063 measure_string_callback, &args);
5065 SelectObject(hdc, oldfont);
5066 DeleteObject(gdifont);
5074 struct draw_string_args {
5075 GpGraphics *graphics;
5076 GDIPCONST GpBrush *brush;
5077 REAL x, y, rel_width, rel_height, ascent;
5080 static GpStatus draw_string_callback(HDC hdc,
5081 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5082 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5083 INT lineno, const RectF *bounds, INT *underlined_indexes,
5084 INT underlined_index_count, void *user_data)
5086 struct draw_string_args *args = user_data;
5090 position.X = args->x + bounds->X / args->rel_width;
5091 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5093 stat = GdipDrawDriverString(args->graphics, &string[index], length, font,
5094 args->brush, &position,
5095 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5097 if (stat == Ok && underlined_index_count)
5099 OUTLINETEXTMETRICW otm;
5100 REAL underline_y, underline_height;
5103 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5105 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5106 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5108 for (i=0; i<underlined_index_count; i++)
5110 REAL start_x, end_x;
5112 INT ofs = underlined_indexes[i] - index;
5114 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5115 start_x = text_size.cx / args->rel_width;
5117 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5118 end_x = text_size.cx / args->rel_width;
5120 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5127 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5128 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5129 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5133 GpPointF pt[3], rectcpy[4];
5135 REAL rel_width, rel_height;
5138 struct draw_string_args args;
5140 HDC hdc, temp_hdc=NULL;
5141 TEXTMETRICW textmetric;
5143 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5144 length, font, debugstr_rectf(rect), format, brush);
5146 if(!graphics || !string || !font || !brush || !rect)
5147 return InvalidParameter;
5151 hdc = graphics->hdc;
5155 hdc = temp_hdc = CreateCompatibleDC(0);
5159 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5161 /* Should be no need to explicitly test for StringAlignmentNear as
5162 * that is default behavior if no alignment is passed. */
5163 if(format->vertalign != StringAlignmentNear){
5164 RectF bounds, in_rect = *rect;
5165 in_rect.Height = 0.0; /* avoid height clipping */
5166 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5168 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5170 if(format->vertalign == StringAlignmentCenter)
5171 offsety = (rect->Height - bounds.Height) / 2;
5172 else if(format->vertalign == StringAlignmentFar)
5173 offsety = (rect->Height - bounds.Height);
5175 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5178 save_state = SaveDC(hdc);
5186 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5187 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5188 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5189 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5190 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5192 rectcpy[3].X = rectcpy[0].X = rect->X;
5193 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5194 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5195 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5196 transform_and_round_points(graphics, corners, rectcpy, 4);
5198 scaled_rect.X = 0.0;
5199 scaled_rect.Y = 0.0;
5200 scaled_rect.Width = rel_width * rect->Width;
5201 scaled_rect.Height = rel_height * rect->Height;
5203 if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
5205 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5206 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5207 SelectClipRgn(hdc, rgn);
5210 get_font_hfont(graphics, font, &gdifont);
5211 SelectObject(hdc, gdifont);
5213 args.graphics = graphics;
5217 args.y = rect->Y + offsety;
5219 args.rel_width = rel_width;
5220 args.rel_height = rel_height;
5222 GetTextMetricsW(hdc, &textmetric);
5223 args.ascent = textmetric.tmAscent / rel_height;
5225 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5226 draw_string_callback, &args);
5229 DeleteObject(gdifont);
5231 RestoreDC(hdc, save_state);
5238 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5240 TRACE("(%p)\n", graphics);
5243 return InvalidParameter;
5248 return GdipSetInfinite(graphics->clip);
5251 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5253 TRACE("(%p)\n", graphics);
5256 return InvalidParameter;
5261 graphics->worldtrans->matrix[0] = 1.0;
5262 graphics->worldtrans->matrix[1] = 0.0;
5263 graphics->worldtrans->matrix[2] = 0.0;
5264 graphics->worldtrans->matrix[3] = 1.0;
5265 graphics->worldtrans->matrix[4] = 0.0;
5266 graphics->worldtrans->matrix[5] = 0.0;
5271 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5273 return GdipEndContainer(graphics, state);
5276 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5277 GpMatrixOrder order)
5279 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5282 return InvalidParameter;
5287 return GdipRotateMatrix(graphics->worldtrans, angle, order);
5290 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5292 return GdipBeginContainer2(graphics, state);
5295 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5296 GraphicsContainer *state)
5298 GraphicsContainerItem *container;
5301 TRACE("(%p, %p)\n", graphics, state);
5303 if(!graphics || !state)
5304 return InvalidParameter;
5306 sts = init_container(&container, graphics);
5310 list_add_head(&graphics->containers, &container->entry);
5311 *state = graphics->contid = container->contid;
5316 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5318 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5319 return NotImplemented;
5322 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5324 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5325 return NotImplemented;
5328 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5330 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5331 return NotImplemented;
5334 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5337 GraphicsContainerItem *container, *container2;
5339 TRACE("(%p, %x)\n", graphics, state);
5342 return InvalidParameter;
5344 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5345 if(container->contid == state)
5349 /* did not find a matching container */
5350 if(&container->entry == &graphics->containers)
5353 sts = restore_container(graphics, container);
5357 /* remove all of the containers on top of the found container */
5358 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5359 if(container->contid == state)
5361 list_remove(&container->entry);
5362 delete_container(container);
5365 list_remove(&container->entry);
5366 delete_container(container);
5371 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5372 REAL sy, GpMatrixOrder order)
5374 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5377 return InvalidParameter;
5382 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
5385 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5388 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5390 if(!graphics || !srcgraphics)
5391 return InvalidParameter;
5393 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5396 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5397 CompositingMode mode)
5399 TRACE("(%p, %d)\n", graphics, mode);
5402 return InvalidParameter;
5407 graphics->compmode = mode;
5412 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5413 CompositingQuality quality)
5415 TRACE("(%p, %d)\n", graphics, quality);
5418 return InvalidParameter;
5423 graphics->compqual = quality;
5428 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5429 InterpolationMode mode)
5431 TRACE("(%p, %d)\n", graphics, mode);
5433 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5434 return InvalidParameter;
5439 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5440 mode = InterpolationModeBilinear;
5442 if (mode == InterpolationModeHighQuality)
5443 mode = InterpolationModeHighQualityBicubic;
5445 graphics->interpolation = mode;
5450 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5452 TRACE("(%p, %.2f)\n", graphics, scale);
5454 if(!graphics || (scale <= 0.0))
5455 return InvalidParameter;
5460 graphics->scale = scale;
5465 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5467 TRACE("(%p, %d)\n", graphics, unit);
5470 return InvalidParameter;
5475 if(unit == UnitWorld)
5476 return InvalidParameter;
5478 graphics->unit = unit;
5483 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5486 TRACE("(%p, %d)\n", graphics, mode);
5489 return InvalidParameter;
5494 graphics->pixeloffset = mode;
5499 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5503 TRACE("(%p,%i,%i)\n", graphics, x, y);
5506 FIXME("value is unused in rendering\n");
5509 return InvalidParameter;
5511 graphics->origin_x = x;
5512 graphics->origin_y = y;
5517 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5519 TRACE("(%p,%p,%p)\n", graphics, x, y);
5521 if (!graphics || !x || !y)
5522 return InvalidParameter;
5524 *x = graphics->origin_x;
5525 *y = graphics->origin_y;
5530 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5532 TRACE("(%p, %d)\n", graphics, mode);
5535 return InvalidParameter;
5540 graphics->smoothing = mode;
5545 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5547 TRACE("(%p, %d)\n", graphics, contrast);
5550 return InvalidParameter;
5552 graphics->textcontrast = contrast;
5557 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5558 TextRenderingHint hint)
5560 TRACE("(%p, %d)\n", graphics, hint);
5562 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5563 return InvalidParameter;
5568 graphics->texthint = hint;
5573 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5575 TRACE("(%p, %p)\n", graphics, matrix);
5577 if(!graphics || !matrix)
5578 return InvalidParameter;
5583 GdipDeleteMatrix(graphics->worldtrans);
5584 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5587 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5588 REAL dy, GpMatrixOrder order)
5590 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5593 return InvalidParameter;
5598 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5601 /*****************************************************************************
5602 * GdipSetClipHrgn [GDIPLUS.@]
5604 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5609 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5612 return InvalidParameter;
5614 status = GdipCreateRegionHrgn(hrgn, ®ion);
5618 status = GdipSetClipRegion(graphics, region, mode);
5620 GdipDeleteRegion(region);
5624 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5626 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5629 return InvalidParameter;
5634 return GdipCombineRegionPath(graphics->clip, path, mode);
5637 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5638 REAL width, REAL height,
5643 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5646 return InvalidParameter;
5654 rect.Height = height;
5656 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5659 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5660 INT width, INT height,
5663 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5666 return InvalidParameter;
5671 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5674 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5677 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5679 if(!graphics || !region)
5680 return InvalidParameter;
5685 return GdipCombineRegionRegion(graphics->clip, region, mode);
5688 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5693 TRACE("(%p,%u)\n", metafile, limitDpi);
5696 FIXME("not implemented\n");
5698 return NotImplemented;
5701 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5707 TRACE("(%p, %p, %d)\n", graphics, points, count);
5709 if(!graphics || !pen || count<=0)
5710 return InvalidParameter;
5717 FIXME("graphics object has no HDC\n");
5721 pti = GdipAlloc(sizeof(POINT) * count);
5723 save_state = prepare_dc(graphics, pen);
5724 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5726 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5727 Polygon(graphics->hdc, pti, count);
5729 restore_dc(graphics, save_state);
5735 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5742 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5744 if(count<=0) return InvalidParameter;
5745 ptf = GdipAlloc(sizeof(GpPointF) * count);
5747 for(i = 0;i < count; i++){
5748 ptf[i].X = (REAL)points[i].X;
5749 ptf[i].Y = (REAL)points[i].Y;
5752 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5758 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5760 TRACE("(%p, %p)\n", graphics, dpi);
5762 if(!graphics || !dpi)
5763 return InvalidParameter;
5768 *dpi = graphics->xres;
5772 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5774 TRACE("(%p, %p)\n", graphics, dpi);
5776 if(!graphics || !dpi)
5777 return InvalidParameter;
5782 *dpi = graphics->yres;
5786 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5787 GpMatrixOrder order)
5792 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5794 if(!graphics || !matrix)
5795 return InvalidParameter;
5800 m = *(graphics->worldtrans);
5802 ret = GdipMultiplyMatrix(&m, matrix, order);
5804 *(graphics->worldtrans) = m;
5809 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5810 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5812 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5816 TRACE("(%p, %p)\n", graphics, hdc);
5818 if(!graphics || !hdc)
5819 return InvalidParameter;
5824 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5826 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5828 else if (!graphics->hdc ||
5829 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5831 /* Create a fake HDC and fill it with a constant color. */
5835 BITMAPINFOHEADER bmih;
5838 stat = get_graphics_bounds(graphics, &bounds);
5842 graphics->temp_hbitmap_width = bounds.Width;
5843 graphics->temp_hbitmap_height = bounds.Height;
5845 bmih.biSize = sizeof(bmih);
5846 bmih.biWidth = graphics->temp_hbitmap_width;
5847 bmih.biHeight = -graphics->temp_hbitmap_height;
5849 bmih.biBitCount = 32;
5850 bmih.biCompression = BI_RGB;
5851 bmih.biSizeImage = 0;
5852 bmih.biXPelsPerMeter = 0;
5853 bmih.biYPelsPerMeter = 0;
5855 bmih.biClrImportant = 0;
5857 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5858 (void**)&graphics->temp_bits, NULL, 0);
5860 return GenericError;
5862 temp_hdc = CreateCompatibleDC(0);
5865 DeleteObject(hbitmap);
5866 return GenericError;
5869 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5870 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5872 SelectObject(temp_hdc, hbitmap);
5874 graphics->temp_hbitmap = hbitmap;
5875 *hdc = graphics->temp_hdc = temp_hdc;
5879 *hdc = graphics->hdc;
5883 graphics->busy = TRUE;
5888 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5892 TRACE("(%p, %p)\n", graphics, hdc);
5894 if(!graphics || !hdc || !graphics->busy)
5895 return InvalidParameter;
5897 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5899 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5901 else if (graphics->temp_hdc == hdc)
5906 /* Find the pixels that have changed, and mark them as opaque. */
5907 pos = (DWORD*)graphics->temp_bits;
5908 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5910 if (*pos != DC_BACKGROUND_KEY)
5917 /* Write the changed pixels to the real target. */
5918 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5919 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5920 graphics->temp_hbitmap_width * 4);
5923 DeleteDC(graphics->temp_hdc);
5924 DeleteObject(graphics->temp_hbitmap);
5925 graphics->temp_hdc = NULL;
5926 graphics->temp_hbitmap = NULL;
5928 else if (hdc != graphics->hdc)
5930 stat = InvalidParameter;
5934 graphics->busy = FALSE;
5939 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5944 TRACE("(%p, %p)\n", graphics, region);
5946 if(!graphics || !region)
5947 return InvalidParameter;
5952 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5955 /* free everything except root node and header */
5956 delete_element(®ion->node);
5957 memcpy(region, clip, sizeof(GpRegion));
5963 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5964 GpCoordinateSpace src_space, GpMatrix **matrix)
5966 GpStatus stat = GdipCreateMatrix(matrix);
5967 REAL scale_x, scale_y;
5969 if (dst_space != src_space && stat == Ok)
5971 scale_x = convert_unit(graphics->xres, graphics->unit);
5972 scale_y = convert_unit(graphics->yres, graphics->unit);
5974 if(graphics->unit != UnitDisplay)
5976 scale_x *= graphics->scale;
5977 scale_y *= graphics->scale;
5980 /* transform from src_space to CoordinateSpacePage */
5983 case CoordinateSpaceWorld:
5984 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5986 case CoordinateSpacePage:
5988 case CoordinateSpaceDevice:
5989 GdipScaleMatrix(*matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5993 /* transform from CoordinateSpacePage to dst_space */
5996 case CoordinateSpaceWorld:
5998 GpMatrix *inverted_transform;
5999 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
6002 stat = GdipInvertMatrix(inverted_transform);
6004 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
6005 GdipDeleteMatrix(inverted_transform);
6009 case CoordinateSpacePage:
6011 case CoordinateSpaceDevice:
6012 GdipScaleMatrix(*matrix, scale_x, scale_y, MatrixOrderAppend);
6019 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6020 GpCoordinateSpace src_space, GpPointF *points, INT count)
6025 if(!graphics || !points || count <= 0)
6026 return InvalidParameter;
6031 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6033 if (src_space == dst_space) return Ok;
6035 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6039 stat = GdipTransformMatrixPoints(matrix, points, count);
6041 GdipDeleteMatrix(matrix);
6047 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6048 GpCoordinateSpace src_space, GpPoint *points, INT count)
6054 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6057 return InvalidParameter;
6059 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6063 for(i = 0; i < count; i++){
6064 pointsF[i].X = (REAL)points[i].X;
6065 pointsF[i].Y = (REAL)points[i].Y;
6068 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6071 for(i = 0; i < count; i++){
6072 points[i].X = roundr(pointsF[i].X);
6073 points[i].Y = roundr(pointsF[i].Y);
6080 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6092 /*****************************************************************************
6093 * GdipTranslateClip [GDIPLUS.@]
6095 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6097 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6100 return InvalidParameter;
6105 return GdipTranslateRegion(graphics->clip, dx, dy);
6108 /*****************************************************************************
6109 * GdipTranslateClipI [GDIPLUS.@]
6111 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6113 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6116 return InvalidParameter;
6121 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6125 /*****************************************************************************
6126 * GdipMeasureDriverString [GDIPLUS.@]
6128 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6129 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6130 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6132 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6135 REAL min_x, min_y, max_x, max_y, x, y;
6137 TEXTMETRICW textmetric;
6138 const WORD *glyph_indices;
6139 WORD *dynamic_glyph_indices=NULL;
6140 REAL rel_width, rel_height, ascent, descent;
6143 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6145 if (!graphics || !text || !font || !positions || !boundingBox)
6146 return InvalidParameter;
6149 length = strlenW(text);
6153 boundingBox->X = 0.0;
6154 boundingBox->Y = 0.0;
6155 boundingBox->Width = 0.0;
6156 boundingBox->Height = 0.0;
6159 if (flags & unsupported_flags)
6160 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6163 FIXME("Ignoring matrix\n");
6165 get_font_hfont(graphics, font, &hfont);
6167 hdc = CreateCompatibleDC(0);
6168 SelectObject(hdc, hfont);
6170 GetTextMetricsW(hdc, &textmetric);
6178 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6179 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6180 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6181 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6182 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6184 if (flags & DriverStringOptionsCmapLookup)
6186 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6190 DeleteObject(hfont);
6194 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6197 glyph_indices = text;
6199 min_x = max_x = x = positions[0].X;
6200 min_y = max_y = y = positions[0].Y;
6202 ascent = textmetric.tmAscent / rel_height;
6203 descent = textmetric.tmDescent / rel_height;
6205 for (i=0; i<length; i++)
6210 if (!(flags & DriverStringOptionsRealizedAdvance))
6216 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6217 char_width = abc.abcA + abc.abcB + abc.abcB;
6219 if (min_y > y - ascent) min_y = y - ascent;
6220 if (max_y < y + descent) max_y = y + descent;
6221 if (min_x > x) min_x = x;
6223 x += char_width / rel_width;
6225 if (max_x < x) max_x = x;
6228 GdipFree(dynamic_glyph_indices);
6230 DeleteObject(hfont);
6232 boundingBox->X = min_x;
6233 boundingBox->Y = min_y;
6234 boundingBox->Width = max_x - min_x;
6235 boundingBox->Height = max_y - min_y;
6240 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6241 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6242 GDIPCONST PointF *positions, INT flags,
6243 GDIPCONST GpMatrix *matrix )
6245 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6251 if (flags & unsupported_flags)
6252 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6255 FIXME("Ignoring matrix\n");
6257 if (!(flags & DriverStringOptionsCmapLookup))
6258 eto_flags |= ETO_GLYPH_INDEX;
6260 save_state = SaveDC(graphics->hdc);
6261 SetBkMode(graphics->hdc, TRANSPARENT);
6262 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6265 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6267 get_font_hfont(graphics, font, &hfont);
6268 SelectObject(graphics->hdc, hfont);
6270 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6272 ExtTextOutW(graphics->hdc, roundr(pt.X), roundr(pt.Y), eto_flags, NULL, text, length, NULL);
6274 RestoreDC(graphics->hdc, save_state);
6276 DeleteObject(hfont);
6281 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6282 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6283 GDIPCONST PointF *positions, INT flags,
6284 GDIPCONST GpMatrix *matrix )
6286 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6288 PointF *real_positions, real_position;
6292 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6293 DWORD max_glyphsize=0;
6294 GLYPHMETRICS glyphmetrics;
6295 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6298 int text_mask_stride;
6300 int pixel_data_stride;
6302 UINT ggo_flags = GGO_GRAY8_BITMAP;
6307 if (!(flags & DriverStringOptionsCmapLookup))
6308 ggo_flags |= GGO_GLYPH_INDEX;
6310 if (flags & unsupported_flags)
6311 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6314 FIXME("Ignoring matrix\n");
6316 pti = GdipAlloc(sizeof(POINT) * length);
6320 if (flags & DriverStringOptionsRealizedAdvance)
6322 real_position = positions[0];
6324 transform_and_round_points(graphics, pti, &real_position, 1);
6328 real_positions = GdipAlloc(sizeof(PointF) * length);
6329 if (!real_positions)
6335 memcpy(real_positions, positions, sizeof(PointF) * length);
6337 transform_and_round_points(graphics, pti, real_positions, length);
6339 GdipFree(real_positions);
6342 get_font_hfont(graphics, font, &hfont);
6344 hdc = CreateCompatibleDC(0);
6345 SelectObject(hdc, hfont);
6347 /* Get the boundaries of the text to be drawn */
6348 for (i=0; i<length; i++)
6351 int left, top, right, bottom;
6353 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6354 &glyphmetrics, 0, NULL, &identity);
6356 if (glyphsize == GDI_ERROR)
6358 ERR("GetGlyphOutlineW failed\n");
6361 DeleteObject(hfont);
6362 return GenericError;
6365 if (glyphsize > max_glyphsize)
6366 max_glyphsize = glyphsize;
6368 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6369 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6370 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6371 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6373 if (left < min_x) min_x = left;
6374 if (top < min_y) min_y = top;
6375 if (right > max_x) max_x = right;
6376 if (bottom > max_y) max_y = bottom;
6378 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6380 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6381 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6385 glyph_mask = GdipAlloc(max_glyphsize);
6386 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6387 text_mask_stride = max_x - min_x;
6389 if (!(glyph_mask && text_mask))
6391 GdipFree(glyph_mask);
6392 GdipFree(text_mask);
6395 DeleteObject(hfont);
6399 /* Generate a mask for the text */
6400 for (i=0; i<length; i++)
6402 int left, top, stride;
6404 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6405 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6407 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6408 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6409 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6411 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6413 BYTE *glyph_val = glyph_mask + y * stride;
6414 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6415 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6417 *text_val = min(64, *text_val + *glyph_val);
6426 DeleteObject(hfont);
6427 GdipFree(glyph_mask);
6429 /* get the brush data */
6430 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6433 GdipFree(text_mask);
6437 pixel_area.X = min_x;
6438 pixel_area.Y = min_y;
6439 pixel_area.Width = max_x - min_x;
6440 pixel_area.Height = max_y - min_y;
6441 pixel_data_stride = pixel_area.Width * 4;
6443 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6446 GdipFree(text_mask);
6447 GdipFree(pixel_data);
6451 /* multiply the brush data by the mask */
6452 for (y=0; y<pixel_area.Height; y++)
6454 BYTE *text_val = text_mask + text_mask_stride * y;
6455 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6456 for (x=0; x<pixel_area.Width; x++)
6458 *pixel_val = (*pixel_val) * (*text_val) / 64;
6464 GdipFree(text_mask);
6466 /* draw the result */
6467 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6468 pixel_area.Height, pixel_data_stride);
6470 GdipFree(pixel_data);
6475 /*****************************************************************************
6476 * GdipDrawDriverString [GDIPLUS.@]
6478 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6479 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6480 GDIPCONST PointF *positions, INT flags,
6481 GDIPCONST GpMatrix *matrix )
6483 GpStatus stat=NotImplemented;
6485 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6487 if (!graphics || !text || !font || !brush || !positions)
6488 return InvalidParameter;
6491 length = strlenW(text);
6493 if (graphics->hdc &&
6494 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6495 brush->bt == BrushTypeSolidColor &&
6496 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6497 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, brush,
6498 positions, flags, matrix);
6500 if (stat == NotImplemented)
6501 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, brush,
6502 positions, flags, matrix);
6507 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6508 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6510 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6511 return NotImplemented;
6514 /*****************************************************************************
6515 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6517 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6522 TRACE("(%p, %p)\n", graphics, res);
6524 if((stat = GdipCreateRegion(&rgn)) != Ok)
6527 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6530 stat = GdipIsEmptyRegion(rgn, graphics, res);
6533 GdipDeleteRegion(rgn);
6537 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6541 TRACE("(%p) stub\n", graphics);
6544 FIXME("not implemented\n");
6546 return NotImplemented;