2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/unicode.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
50 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
51 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
52 INT flags, GDIPCONST GpMatrix *matrix);
54 /* Converts angle (in degrees) to x/y coordinates */
55 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
57 REAL radAngle, hypotenuse;
59 radAngle = deg2rad(angle);
60 hypotenuse = 50.0; /* arbitrary */
62 *x = x_0 + cos(radAngle) * hypotenuse;
63 *y = y_0 + sin(radAngle) * hypotenuse;
66 /* Converts from gdiplus path point type to gdi path point type. */
67 static BYTE convert_path_point_type(BYTE type)
71 switch(type & PathPointTypePathTypeMask){
72 case PathPointTypeBezier:
75 case PathPointTypeLine:
78 case PathPointTypeStart:
82 ERR("Bad point type\n");
86 if(type & PathPointTypeCloseSubpath)
87 ret |= PT_CLOSEFIGURE;
92 static COLORREF get_gdi_brush_color(const GpBrush *brush)
98 case BrushTypeSolidColor:
100 const GpSolidFill *sf = (const GpSolidFill *)brush;
104 case BrushTypeHatchFill:
106 const GpHatch *hatch = (const GpHatch *)brush;
107 argb = hatch->forecol;
110 case BrushTypeLinearGradient:
112 const GpLineGradient *line = (const GpLineGradient *)brush;
113 argb = line->startcolor;
116 case BrushTypePathGradient:
118 const GpPathGradient *grad = (const GpPathGradient *)brush;
119 argb = grad->centercolor;
123 FIXME("unhandled brush type %d\n", brush->bt);
127 return ARGB2COLORREF(argb);
130 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
133 BITMAPINFOHEADER bmih;
137 bmih.biSize = sizeof(bmih);
141 bmih.biBitCount = 32;
142 bmih.biCompression = BI_RGB;
143 bmih.biSizeImage = 0;
145 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
148 const char *hatch_data;
150 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
152 for (y = 0; y < 8; y++)
154 for (x = 0; x < 8; x++)
156 if (hatch_data[y] & (0x80 >> x))
157 bits[y * 8 + x] = hatch->forecol;
159 bits[y * 8 + x] = hatch->backcol;
165 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
167 for (y = 0; y < 64; y++)
168 bits[y] = hatch->forecol;
175 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
179 case BrushTypeSolidColor:
181 const GpSolidFill *sf = (const GpSolidFill *)brush;
182 lb->lbStyle = BS_SOLID;
183 lb->lbColor = ARGB2COLORREF(sf->color);
188 case BrushTypeHatchFill:
190 const GpHatch *hatch = (const GpHatch *)brush;
193 hbmp = create_hatch_bitmap(hatch);
194 if (!hbmp) return OutOfMemory;
196 lb->lbStyle = BS_PATTERN;
198 lb->lbHatch = (ULONG_PTR)hbmp;
203 FIXME("unhandled brush type %d\n", brush->bt);
204 lb->lbStyle = BS_SOLID;
205 lb->lbColor = get_gdi_brush_color(brush);
211 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
216 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
222 static HBRUSH create_gdi_brush(const GpBrush *brush)
227 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
229 gdibrush = CreateBrushIndirect(&lb);
230 free_gdi_logbrush(&lb);
235 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
240 INT save_state, i, numdashes;
242 DWORD dash_array[MAX_DASHLEN];
244 save_state = SaveDC(graphics->hdc);
246 EndPath(graphics->hdc);
248 if(pen->unit == UnitPixel){
252 /* Get an estimate for the amount the pen width is affected by the world
253 * transform. (This is similar to what some of the wine drivers do.) */
258 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
259 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
260 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
262 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
265 if(pen->dash == DashStyleCustom){
266 numdashes = min(pen->numdashes, MAX_DASHLEN);
268 TRACE("dashes are: ");
269 for(i = 0; i < numdashes; i++){
270 dash_array[i] = gdip_round(width * pen->dashes[i]);
271 TRACE("%d, ", dash_array[i]);
273 TRACE("\n and the pen style is %x\n", pen->style);
275 create_gdi_logbrush(pen->brush, &lb);
276 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
277 numdashes, dash_array);
278 free_gdi_logbrush(&lb);
282 create_gdi_logbrush(pen->brush, &lb);
283 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
284 free_gdi_logbrush(&lb);
287 SelectObject(graphics->hdc, gdipen);
292 static void restore_dc(GpGraphics *graphics, INT state)
294 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
295 RestoreDC(graphics->hdc, state);
298 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
299 GpCoordinateSpace src_space, GpMatrix **matrix);
301 /* This helper applies all the changes that the points listed in ptf need in
302 * order to be drawn on the device context. In the end, this should include at
304 * -scaling by page unit
305 * -applying world transformation
306 * -converting from float to int
307 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
308 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
309 * gdi to draw, and these functions would irreparably mess with line widths.
311 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
312 GpPointF *ptf, INT count)
314 REAL scale_x, scale_y;
318 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
319 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
321 /* apply page scale */
322 if(graphics->unit != UnitDisplay)
324 scale_x *= graphics->scale;
325 scale_y *= graphics->scale;
328 GdipCloneMatrix(graphics->worldtrans, &matrix);
329 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
330 GdipTransformMatrixPoints(matrix, ptf, count);
331 GdipDeleteMatrix(matrix);
333 for(i = 0; i < count; i++){
334 pti[i].x = gdip_round(ptf[i].X);
335 pti[i].y = gdip_round(ptf[i].Y);
339 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
340 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
342 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
344 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
346 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
347 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
353 bf.BlendOp = AC_SRC_OVER;
355 bf.SourceConstantAlpha = 255;
356 bf.AlphaFormat = AC_SRC_ALPHA;
358 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
359 hdc, src_x, src_y, src_width, src_height, bf);
363 /* Draw non-premultiplied ARGB data to the given graphics object */
364 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
365 const BYTE *src, INT src_width, INT src_height, INT src_stride)
367 if (graphics->image && graphics->image->type == ImageTypeBitmap)
369 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
372 for (x=0; x<src_width; x++)
374 for (y=0; y<src_height; y++)
376 ARGB dst_color, src_color;
377 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
378 src_color = ((ARGB*)(src + src_stride * y))[x];
379 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
385 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
387 ERR("This should not be used for metafiles; fix caller\n");
388 return NotImplemented;
394 BITMAPINFOHEADER bih;
397 hdc = CreateCompatibleDC(0);
399 bih.biSize = sizeof(BITMAPINFOHEADER);
400 bih.biWidth = src_width;
401 bih.biHeight = -src_height;
404 bih.biCompression = BI_RGB;
406 bih.biXPelsPerMeter = 0;
407 bih.biYPelsPerMeter = 0;
409 bih.biClrImportant = 0;
411 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
412 (void**)&temp_bits, NULL, 0);
414 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
415 4 * src_width, src, src_stride);
417 SelectObject(hdc, hbitmap);
418 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
419 hdc, 0, 0, src_width, src_height);
421 DeleteObject(hbitmap);
427 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
428 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion)
432 if (graphics->image && graphics->image->type == ImageTypeBitmap)
438 size = GetRegionData(hregion, 0, NULL);
440 rgndata = GdipAlloc(size);
444 GetRegionData(hregion, size, rgndata);
446 rects = (RECT*)&rgndata->Buffer;
448 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
450 stat = alpha_blend_pixels(graphics, rects[i].left, rects[i].top,
451 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
452 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
460 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
462 ERR("This should not be used for metafiles; fix caller\n");
463 return NotImplemented;
469 save = SaveDC(graphics->hdc);
471 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
473 stat = alpha_blend_pixels(graphics, dst_x, dst_y, src, src_width,
474 src_height, src_stride);
476 RestoreDC(graphics->hdc, save);
482 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
488 a1 = (start >> 24) & 0xff;
489 a2 = (end >> 24) & 0xff;
491 a3 = (int)(a1*(1.0f - position)+a2*(position));
495 for (i=0xff; i<=0xff0000; i = i << 8)
496 result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
500 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
504 /* clamp to between 0.0 and 1.0, using the wrap mode */
505 if (brush->wrap == WrapModeTile)
507 position = fmodf(position, 1.0f);
508 if (position < 0.0f) position += 1.0f;
510 else /* WrapModeFlip* */
512 position = fmodf(position, 2.0f);
513 if (position < 0.0f) position += 2.0f;
514 if (position > 1.0f) position = 2.0f - position;
517 if (brush->blendcount == 1)
522 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
525 /* locate the blend positions surrounding this position */
526 while (position > brush->blendpos[i])
529 /* interpolate between the blend positions */
530 left_blendpos = brush->blendpos[i-1];
531 left_blendfac = brush->blendfac[i-1];
532 right_blendpos = brush->blendpos[i];
533 right_blendfac = brush->blendfac[i];
534 range = right_blendpos - left_blendpos;
535 blendfac = (left_blendfac * (right_blendpos - position) +
536 right_blendfac * (position - left_blendpos)) / range;
539 if (brush->pblendcount == 0)
540 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
544 ARGB left_blendcolor, right_blendcolor;
545 REAL left_blendpos, right_blendpos;
547 /* locate the blend colors surrounding this position */
548 while (blendfac > brush->pblendpos[i])
551 /* interpolate between the blend colors */
552 left_blendpos = brush->pblendpos[i-1];
553 left_blendcolor = brush->pblendcolor[i-1];
554 right_blendpos = brush->pblendpos[i];
555 right_blendcolor = brush->pblendcolor[i];
556 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
557 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
561 static ARGB transform_color(ARGB color, const ColorMatrix *matrix)
565 unsigned char a, r, g, b;
567 val[0] = ((color >> 16) & 0xff) / 255.0; /* red */
568 val[1] = ((color >> 8) & 0xff) / 255.0; /* green */
569 val[2] = (color & 0xff) / 255.0; /* blue */
570 val[3] = ((color >> 24) & 0xff) / 255.0; /* alpha */
571 val[4] = 1.0; /* translation */
578 res[i] += matrix->m[j][i] * val[j];
581 a = min(max(floorf(res[3]*255.0), 0.0), 255.0);
582 r = min(max(floorf(res[0]*255.0), 0.0), 255.0);
583 g = min(max(floorf(res[1]*255.0), 0.0), 255.0);
584 b = min(max(floorf(res[2]*255.0), 0.0), 255.0);
586 return (a << 24) | (r << 16) | (g << 8) | b;
589 static int color_is_gray(ARGB color)
591 unsigned char r, g, b;
593 r = (color >> 16) & 0xff;
594 g = (color >> 8) & 0xff;
597 return (r == g) && (g == b);
600 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
601 UINT width, UINT height, INT stride, ColorAdjustType type)
605 if (attributes->colorkeys[type].enabled ||
606 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
608 const struct color_key *key;
609 BYTE min_blue, min_green, min_red;
610 BYTE max_blue, max_green, max_red;
612 if (attributes->colorkeys[type].enabled)
613 key = &attributes->colorkeys[type];
615 key = &attributes->colorkeys[ColorAdjustTypeDefault];
617 min_blue = key->low&0xff;
618 min_green = (key->low>>8)&0xff;
619 min_red = (key->low>>16)&0xff;
621 max_blue = key->high&0xff;
622 max_green = (key->high>>8)&0xff;
623 max_red = (key->high>>16)&0xff;
625 for (x=0; x<width; x++)
626 for (y=0; y<height; y++)
629 BYTE blue, green, red;
630 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
631 blue = *src_color&0xff;
632 green = (*src_color>>8)&0xff;
633 red = (*src_color>>16)&0xff;
634 if (blue >= min_blue && green >= min_green && red >= min_red &&
635 blue <= max_blue && green <= max_green && red <= max_red)
636 *src_color = 0x00000000;
640 if (attributes->colorremaptables[type].enabled ||
641 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
643 const struct color_remap_table *table;
645 if (attributes->colorremaptables[type].enabled)
646 table = &attributes->colorremaptables[type];
648 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
650 for (x=0; x<width; x++)
651 for (y=0; y<height; y++)
654 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
655 for (i=0; i<table->mapsize; i++)
657 if (*src_color == table->colormap[i].oldColor.Argb)
659 *src_color = table->colormap[i].newColor.Argb;
666 if (attributes->colormatrices[type].enabled ||
667 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
669 const struct color_matrix *colormatrices;
671 if (attributes->colormatrices[type].enabled)
672 colormatrices = &attributes->colormatrices[type];
674 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
676 for (x=0; x<width; x++)
677 for (y=0; y<height; y++)
680 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
682 if (colormatrices->flags == ColorMatrixFlagsDefault ||
683 !color_is_gray(*src_color))
685 *src_color = transform_color(*src_color, &colormatrices->colormatrix);
687 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
689 *src_color = transform_color(*src_color, &colormatrices->graymatrix);
694 if (attributes->gamma_enabled[type] ||
695 attributes->gamma_enabled[ColorAdjustTypeDefault])
699 if (attributes->gamma_enabled[type])
700 gamma = attributes->gamma[type];
702 gamma = attributes->gamma[ColorAdjustTypeDefault];
704 for (x=0; x<width; x++)
705 for (y=0; y<height; y++)
708 BYTE blue, green, red;
709 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
711 blue = *src_color&0xff;
712 green = (*src_color>>8)&0xff;
713 red = (*src_color>>16)&0xff;
715 /* FIXME: We should probably use a table for this. */
716 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
717 green = floorf(powf(green / 255.0, gamma) * 255.0);
718 red = floorf(powf(red / 255.0, gamma) * 255.0);
720 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
725 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
726 * bitmap that contains all the pixels we may need to draw it. */
727 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
728 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
731 INT left, top, right, bottom;
733 switch (interpolation)
735 case InterpolationModeHighQualityBilinear:
736 case InterpolationModeHighQualityBicubic:
737 /* FIXME: Include a greater range for the prefilter? */
738 case InterpolationModeBicubic:
739 case InterpolationModeBilinear:
740 left = (INT)(floorf(srcx));
741 top = (INT)(floorf(srcy));
742 right = (INT)(ceilf(srcx+srcwidth));
743 bottom = (INT)(ceilf(srcy+srcheight));
745 case InterpolationModeNearestNeighbor:
747 left = gdip_round(srcx);
748 top = gdip_round(srcy);
749 right = gdip_round(srcx+srcwidth);
750 bottom = gdip_round(srcy+srcheight);
754 if (wrap == WrapModeClamp)
760 if (right >= bitmap->width)
761 right = bitmap->width-1;
762 if (bottom >= bitmap->height)
763 bottom = bitmap->height-1;
767 /* In some cases we can make the rectangle smaller here, but the logic
768 * is hard to get right, and tiling suggests we're likely to use the
769 * entire source image. */
770 if (left < 0 || right >= bitmap->width)
773 right = bitmap->width-1;
776 if (top < 0 || bottom >= bitmap->height)
779 bottom = bitmap->height-1;
785 rect->Width = right - left + 1;
786 rect->Height = bottom - top + 1;
789 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
790 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
792 if (attributes->wrap == WrapModeClamp)
794 if (x < 0 || y < 0 || x >= width || y >= height)
795 return attributes->outside_color;
799 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
801 x = width*2 + x % (width * 2);
803 y = height*2 + y % (height * 2);
805 if ((attributes->wrap & 1) == 1)
808 if ((x / width) % 2 == 0)
811 x = width - 1 - x % width;
816 if ((attributes->wrap & 2) == 2)
819 if ((y / height) % 2 == 0)
822 y = height - 1 - y % height;
828 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
830 ERR("out of range pixel requested\n");
834 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
837 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
838 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
839 InterpolationMode interpolation)
843 switch (interpolation)
847 FIXME("Unimplemented interpolation %i\n", interpolation);
849 case InterpolationModeBilinear:
852 INT leftx, rightx, topy, bottomy;
853 ARGB topleft, topright, bottomleft, bottomright;
857 leftxf = floorf(point->X);
859 rightx = (INT)ceilf(point->X);
860 topyf = floorf(point->Y);
862 bottomy = (INT)ceilf(point->Y);
864 if (leftx == rightx && topy == bottomy)
865 return sample_bitmap_pixel(src_rect, bits, width, height,
866 leftx, topy, attributes);
868 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
869 leftx, topy, attributes);
870 topright = sample_bitmap_pixel(src_rect, bits, width, height,
871 rightx, topy, attributes);
872 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
873 leftx, bottomy, attributes);
874 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
875 rightx, bottomy, attributes);
877 x_offset = point->X - leftxf;
878 top = blend_colors(topleft, topright, x_offset);
879 bottom = blend_colors(bottomleft, bottomright, x_offset);
881 return blend_colors(top, bottom, point->Y - topyf);
883 case InterpolationModeNearestNeighbor:
884 return sample_bitmap_pixel(src_rect, bits, width, height,
885 gdip_round(point->X), gdip_round(point->Y), attributes);
889 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
891 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
894 static INT brush_can_fill_path(GpBrush *brush)
898 case BrushTypeSolidColor:
900 case BrushTypeHatchFill:
902 GpHatch *hatch = (GpHatch*)brush;
903 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
904 ((hatch->backcol & 0xff000000) == 0xff000000);
906 case BrushTypeLinearGradient:
907 case BrushTypeTextureFill:
908 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
914 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
918 case BrushTypeSolidColor:
920 GpSolidFill *fill = (GpSolidFill*)brush;
921 HBITMAP bmp = ARGB2BMP(fill->color);
926 /* partially transparent fill */
928 SelectClipPath(graphics->hdc, RGN_AND);
929 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
931 HDC hdc = CreateCompatibleDC(NULL);
935 SelectObject(hdc, bmp);
936 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
944 /* else fall through */
948 HBRUSH gdibrush, old_brush;
950 gdibrush = create_gdi_brush(brush);
951 if (!gdibrush) return;
953 old_brush = SelectObject(graphics->hdc, gdibrush);
954 FillPath(graphics->hdc);
955 SelectObject(graphics->hdc, old_brush);
956 DeleteObject(gdibrush);
962 static INT brush_can_fill_pixels(GpBrush *brush)
966 case BrushTypeSolidColor:
967 case BrushTypeHatchFill:
968 case BrushTypeLinearGradient:
969 case BrushTypeTextureFill:
970 case BrushTypePathGradient:
977 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
978 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
982 case BrushTypeSolidColor:
985 GpSolidFill *fill = (GpSolidFill*)brush;
986 for (x=0; x<fill_area->Width; x++)
987 for (y=0; y<fill_area->Height; y++)
988 argb_pixels[x + y*cdwStride] = fill->color;
991 case BrushTypeHatchFill:
994 GpHatch *fill = (GpHatch*)brush;
995 const char *hatch_data;
997 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
998 return NotImplemented;
1000 for (x=0; x<fill_area->Width; x++)
1001 for (y=0; y<fill_area->Height; y++)
1005 /* FIXME: Account for the rendering origin */
1006 hx = (x + fill_area->X) % 8;
1007 hy = (y + fill_area->Y) % 8;
1009 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1010 argb_pixels[x + y*cdwStride] = fill->forecol;
1012 argb_pixels[x + y*cdwStride] = fill->backcol;
1017 case BrushTypeLinearGradient:
1019 GpLineGradient *fill = (GpLineGradient*)brush;
1020 GpPointF draw_points[3], line_points[3];
1022 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1023 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1026 draw_points[0].X = fill_area->X;
1027 draw_points[0].Y = fill_area->Y;
1028 draw_points[1].X = fill_area->X+1;
1029 draw_points[1].Y = fill_area->Y;
1030 draw_points[2].X = fill_area->X;
1031 draw_points[2].Y = fill_area->Y+1;
1033 /* Transform the points to a co-ordinate space where X is the point's
1034 * position in the gradient, 0.0 being the start point and 1.0 the
1036 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1037 CoordinateSpaceDevice, draw_points, 3);
1041 line_points[0] = fill->startpoint;
1042 line_points[1] = fill->endpoint;
1043 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1044 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1046 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1051 stat = GdipInvertMatrix(world_to_gradient);
1054 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1056 GdipDeleteMatrix(world_to_gradient);
1061 REAL x_delta = draw_points[1].X - draw_points[0].X;
1062 REAL y_delta = draw_points[2].X - draw_points[0].X;
1064 for (y=0; y<fill_area->Height; y++)
1066 for (x=0; x<fill_area->Width; x++)
1068 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1070 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1077 case BrushTypeTextureFill:
1079 GpTexture *fill = (GpTexture*)brush;
1080 GpPointF draw_points[3];
1082 GpMatrix *world_to_texture;
1088 if (fill->image->type != ImageTypeBitmap)
1090 FIXME("metafile texture brushes not implemented\n");
1091 return NotImplemented;
1094 bitmap = (GpBitmap*)fill->image;
1095 src_stride = sizeof(ARGB) * bitmap->width;
1097 src_area.X = src_area.Y = 0;
1098 src_area.Width = bitmap->width;
1099 src_area.Height = bitmap->height;
1101 draw_points[0].X = fill_area->X;
1102 draw_points[0].Y = fill_area->Y;
1103 draw_points[1].X = fill_area->X+1;
1104 draw_points[1].Y = fill_area->Y;
1105 draw_points[2].X = fill_area->X;
1106 draw_points[2].Y = fill_area->Y+1;
1108 /* Transform the points to the co-ordinate space of the bitmap. */
1109 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1110 CoordinateSpaceDevice, draw_points, 3);
1114 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
1119 stat = GdipInvertMatrix(world_to_texture);
1122 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
1124 GdipDeleteMatrix(world_to_texture);
1127 if (stat == Ok && !fill->bitmap_bits)
1129 BitmapData lockeddata;
1131 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1132 if (!fill->bitmap_bits)
1137 lockeddata.Width = bitmap->width;
1138 lockeddata.Height = bitmap->height;
1139 lockeddata.Stride = src_stride;
1140 lockeddata.PixelFormat = PixelFormat32bppARGB;
1141 lockeddata.Scan0 = fill->bitmap_bits;
1143 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1144 PixelFormat32bppARGB, &lockeddata);
1148 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1151 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1152 bitmap->width, bitmap->height,
1153 src_stride, ColorAdjustTypeBitmap);
1157 GdipFree(fill->bitmap_bits);
1158 fill->bitmap_bits = NULL;
1164 REAL x_dx = draw_points[1].X - draw_points[0].X;
1165 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1166 REAL y_dx = draw_points[2].X - draw_points[0].X;
1167 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1169 for (y=0; y<fill_area->Height; y++)
1171 for (x=0; x<fill_area->Width; x++)
1174 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1175 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1177 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1178 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1179 &point, fill->imageattributes, graphics->interpolation);
1186 case BrushTypePathGradient:
1188 GpPathGradient *fill = (GpPathGradient*)brush;
1190 GpMatrix *world_to_device;
1192 int i, figure_start=0;
1193 GpPointF start_point, end_point, center_point;
1195 REAL min_yf, max_yf, line1_xf, line2_xf;
1196 INT min_y, max_y, min_x, max_x;
1199 static int transform_fixme_once;
1201 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1205 FIXME("path gradient focus not implemented\n");
1212 FIXME("path gradient gamma correction not implemented\n");
1215 if (fill->blendcount)
1219 FIXME("path gradient blend not implemented\n");
1222 if (fill->pblendcount)
1226 FIXME("path gradient preset blend not implemented\n");
1229 if (!transform_fixme_once)
1231 BOOL is_identity=TRUE;
1232 GdipIsMatrixIdentity(fill->transform, &is_identity);
1235 FIXME("path gradient transform not implemented\n");
1236 transform_fixme_once = 1;
1240 stat = GdipClonePath(fill->path, &flat_path);
1245 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1246 CoordinateSpaceWorld, &world_to_device);
1249 stat = GdipTransformPath(flat_path, world_to_device);
1253 center_point = fill->center;
1254 stat = GdipTransformMatrixPoints(world_to_device, ¢er_point, 1);
1258 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1260 GdipDeleteMatrix(world_to_device);
1265 GdipDeletePath(flat_path);
1269 for (i=0; i<flat_path->pathdata.Count; i++)
1271 int start_center_line=0, end_center_line=0;
1272 int seen_start=0, seen_end=0, seen_center=0;
1273 REAL center_distance;
1274 ARGB start_color, end_color;
1277 type = flat_path->pathdata.Types[i];
1279 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1282 start_point = flat_path->pathdata.Points[i];
1284 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1286 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1288 end_point = flat_path->pathdata.Points[figure_start];
1289 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1291 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1293 end_point = flat_path->pathdata.Points[i+1];
1294 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1299 outer_color = start_color;
1301 min_yf = center_point.Y;
1302 if (min_yf > start_point.Y) min_yf = start_point.Y;
1303 if (min_yf > end_point.Y) min_yf = end_point.Y;
1305 if (min_yf < fill_area->Y)
1306 min_y = fill_area->Y;
1308 min_y = (INT)ceil(min_yf);
1310 max_yf = center_point.Y;
1311 if (max_yf < start_point.Y) max_yf = start_point.Y;
1312 if (max_yf < end_point.Y) max_yf = end_point.Y;
1314 if (max_yf > fill_area->Y + fill_area->Height)
1315 max_y = fill_area->Y + fill_area->Height;
1317 max_y = (INT)ceil(max_yf);
1319 dy = end_point.Y - start_point.Y;
1320 dx = end_point.X - start_point.X;
1322 /* This is proportional to the distance from start-end line to center point. */
1323 center_distance = dy * (start_point.X - center_point.X) +
1324 dx * (center_point.Y - start_point.Y);
1326 for (y=min_y; y<max_y; y++)
1330 if (!seen_start && yf >= start_point.Y)
1333 start_center_line ^= 1;
1335 if (!seen_end && yf >= end_point.Y)
1338 end_center_line ^= 1;
1340 if (!seen_center && yf >= center_point.Y)
1343 start_center_line ^= 1;
1344 end_center_line ^= 1;
1347 if (start_center_line)
1348 line1_xf = intersect_line_scanline(&start_point, ¢er_point, yf);
1350 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1352 if (end_center_line)
1353 line2_xf = intersect_line_scanline(&end_point, ¢er_point, yf);
1355 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1357 if (line1_xf < line2_xf)
1359 min_x = (INT)ceil(line1_xf);
1360 max_x = (INT)ceil(line2_xf);
1364 min_x = (INT)ceil(line2_xf);
1365 max_x = (INT)ceil(line1_xf);
1368 if (min_x < fill_area->X)
1369 min_x = fill_area->X;
1370 if (max_x > fill_area->X + fill_area->Width)
1371 max_x = fill_area->X + fill_area->Width;
1373 for (x=min_x; x<max_x; x++)
1378 if (start_color != end_color)
1380 REAL blend_amount, pdy, pdx;
1381 pdy = yf - center_point.Y;
1382 pdx = xf - center_point.X;
1383 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1384 outer_color = blend_colors(start_color, end_color, blend_amount);
1387 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1388 (end_point.X - start_point.X) * (yf - start_point.Y);
1390 distance = distance / center_distance;
1392 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1393 blend_colors(outer_color, fill->centercolor, distance);
1398 GdipDeletePath(flat_path);
1402 return NotImplemented;
1406 /* GdipDrawPie/GdipFillPie helper function */
1407 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1408 REAL height, REAL startAngle, REAL sweepAngle)
1415 ptf[1].X = x + width;
1416 ptf[1].Y = y + height;
1418 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1419 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1421 transform_and_round_points(graphics, pti, ptf, 4);
1423 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1424 pti[2].y, pti[3].x, pti[3].y);
1427 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1428 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1429 * should not be called on an hdc that has a path you care about. */
1430 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1431 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1433 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1434 GpMatrix *matrix = NULL;
1435 HBRUSH brush = NULL;
1437 PointF ptf[4], *custptf = NULL;
1438 POINT pt[4], *custpt = NULL;
1440 REAL theta, dsmall, dbig, dx, dy = 0.0;
1445 if((x1 == x2) && (y1 == y2))
1448 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1450 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1452 brush = CreateSolidBrush(color);
1453 lb.lbStyle = BS_SOLID;
1456 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1457 PS_JOIN_MITER, 1, &lb, 0,
1459 oldbrush = SelectObject(graphics->hdc, brush);
1460 oldpen = SelectObject(graphics->hdc, pen);
1467 case LineCapSquareAnchor:
1468 case LineCapDiamondAnchor:
1469 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1470 if(cap == LineCapDiamondAnchor){
1471 dsmall = cos(theta + M_PI_2) * size;
1472 dbig = sin(theta + M_PI_2) * size;
1475 dsmall = cos(theta + M_PI_4) * size;
1476 dbig = sin(theta + M_PI_4) * size;
1479 ptf[0].X = x2 - dsmall;
1480 ptf[1].X = x2 + dbig;
1482 ptf[0].Y = y2 - dbig;
1483 ptf[3].Y = y2 + dsmall;
1485 ptf[1].Y = y2 - dsmall;
1486 ptf[2].Y = y2 + dbig;
1488 ptf[3].X = x2 - dbig;
1489 ptf[2].X = x2 + dsmall;
1491 transform_and_round_points(graphics, pt, ptf, 4);
1492 Polygon(graphics->hdc, pt, 4);
1495 case LineCapArrowAnchor:
1496 size = size * 4.0 / sqrt(3.0);
1498 dx = cos(M_PI / 6.0 + theta) * size;
1499 dy = sin(M_PI / 6.0 + theta) * size;
1504 dx = cos(- M_PI / 6.0 + theta) * size;
1505 dy = sin(- M_PI / 6.0 + theta) * size;
1513 transform_and_round_points(graphics, pt, ptf, 3);
1514 Polygon(graphics->hdc, pt, 3);
1517 case LineCapRoundAnchor:
1518 dx = dy = ANCHOR_WIDTH * size / 2.0;
1525 transform_and_round_points(graphics, pt, ptf, 2);
1526 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1529 case LineCapTriangle:
1531 dx = cos(M_PI_2 + theta) * size;
1532 dy = sin(M_PI_2 + theta) * size;
1539 dx = cos(theta) * size;
1540 dy = sin(theta) * size;
1545 transform_and_round_points(graphics, pt, ptf, 3);
1546 Polygon(graphics->hdc, pt, 3);
1550 dx = dy = size / 2.0;
1557 dx = -cos(M_PI_2 + theta) * size;
1558 dy = -sin(M_PI_2 + theta) * size;
1565 transform_and_round_points(graphics, pt, ptf, 4);
1566 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1567 pt[2].y, pt[3].x, pt[3].y);
1574 count = custom->pathdata.Count;
1575 custptf = GdipAlloc(count * sizeof(PointF));
1576 custpt = GdipAlloc(count * sizeof(POINT));
1577 tp = GdipAlloc(count);
1579 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1582 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1584 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1585 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1587 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1588 GdipTransformMatrixPoints(matrix, custptf, count);
1590 transform_and_round_points(graphics, custpt, custptf, count);
1592 for(i = 0; i < count; i++)
1593 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1596 BeginPath(graphics->hdc);
1597 PolyDraw(graphics->hdc, custpt, tp, count);
1598 EndPath(graphics->hdc);
1599 StrokeAndFillPath(graphics->hdc);
1602 PolyDraw(graphics->hdc, custpt, tp, count);
1608 GdipDeleteMatrix(matrix);
1615 SelectObject(graphics->hdc, oldbrush);
1616 SelectObject(graphics->hdc, oldpen);
1617 DeleteObject(brush);
1622 /* Shortens the line by the given percent by changing x2, y2.
1623 * If percent is > 1.0 then the line will change direction.
1624 * If percent is negative it can lengthen the line. */
1625 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1627 REAL dist, theta, dx, dy;
1629 if((y1 == *y2) && (x1 == *x2))
1632 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1633 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1634 dx = cos(theta) * dist;
1635 dy = sin(theta) * dist;
1641 /* Shortens the line by the given amount by changing x2, y2.
1642 * If the amount is greater than the distance, the line will become length 0.
1643 * If the amount is negative, it can lengthen the line. */
1644 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1646 REAL dx, dy, percent;
1650 if(dx == 0 && dy == 0)
1653 percent = amt / sqrt(dx * dx + dy * dy);
1660 shorten_line_percent(x1, y1, x2, y2, percent);
1663 /* Draws lines between the given points, and if caps is true then draws an endcap
1664 * at the end of the last line. */
1665 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1666 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1669 GpPointF *ptcopy = NULL;
1670 GpStatus status = GenericError;
1675 pti = GdipAlloc(count * sizeof(POINT));
1676 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1678 if(!pti || !ptcopy){
1679 status = OutOfMemory;
1683 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1686 if(pen->endcap == LineCapArrowAnchor)
1687 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1688 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1689 else if((pen->endcap == LineCapCustom) && pen->customend)
1690 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1691 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1692 pen->customend->inset * pen->width);
1694 if(pen->startcap == LineCapArrowAnchor)
1695 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1696 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1697 else if((pen->startcap == LineCapCustom) && pen->customstart)
1698 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1699 &ptcopy[0].X, &ptcopy[0].Y,
1700 pen->customstart->inset * pen->width);
1702 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1703 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1704 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1705 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1708 transform_and_round_points(graphics, pti, ptcopy, count);
1710 if(Polyline(graphics->hdc, pti, count))
1720 /* Conducts a linear search to find the bezier points that will back off
1721 * the endpoint of the curve by a distance of amt. Linear search works
1722 * better than binary in this case because there are multiple solutions,
1723 * and binary searches often find a bad one. I don't think this is what
1724 * Windows does but short of rendering the bezier without GDI's help it's
1725 * the best we can do. If rev then work from the start of the passed points
1726 * instead of the end. */
1727 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1730 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1731 INT i, first = 0, second = 1, third = 2, fourth = 3;
1740 origx = pt[fourth].X;
1741 origy = pt[fourth].Y;
1742 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1744 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1745 /* reset bezier points to original values */
1746 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1747 /* Perform magic on bezier points. Order is important here.*/
1748 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1749 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1750 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1751 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1752 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1753 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1755 dx = pt[fourth].X - origx;
1756 dy = pt[fourth].Y - origy;
1758 diff = sqrt(dx * dx + dy * dy);
1759 percent += 0.0005 * amt;
1763 /* Draws bezier curves between given points, and if caps is true then draws an
1764 * endcap at the end of the last line. */
1765 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1766 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1770 GpStatus status = GenericError;
1775 pti = GdipAlloc(count * sizeof(POINT));
1776 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1778 if(!pti || !ptcopy){
1779 status = OutOfMemory;
1783 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1786 if(pen->endcap == LineCapArrowAnchor)
1787 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1788 else if((pen->endcap == LineCapCustom) && pen->customend)
1789 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1792 if(pen->startcap == LineCapArrowAnchor)
1793 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1794 else if((pen->startcap == LineCapCustom) && pen->customstart)
1795 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1797 /* the direction of the line cap is parallel to the direction at the
1798 * end of the bezier (which, if it has been shortened, is not the same
1799 * as the direction from pt[count-2] to pt[count-1]) */
1800 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1801 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1802 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1803 pt[count - 1].X, pt[count - 1].Y);
1805 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1806 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1807 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1810 transform_and_round_points(graphics, pti, ptcopy, count);
1812 PolyBezier(graphics->hdc, pti, count);
1823 /* Draws a combination of bezier curves and lines between points. */
1824 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1825 GDIPCONST BYTE * types, INT count, BOOL caps)
1827 POINT *pti = GdipAlloc(count * sizeof(POINT));
1828 BYTE *tp = GdipAlloc(count);
1829 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1831 GpStatus status = GenericError;
1837 if(!pti || !tp || !ptcopy){
1838 status = OutOfMemory;
1842 for(i = 1; i < count; i++){
1843 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1844 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1845 || !(types[i + 1] & PathPointTypeBezier)){
1846 ERR("Bad bezier points\n");
1853 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1855 /* If we are drawing caps, go through the points and adjust them accordingly,
1856 * and draw the caps. */
1858 switch(types[count - 1] & PathPointTypePathTypeMask){
1859 case PathPointTypeBezier:
1860 if(pen->endcap == LineCapArrowAnchor)
1861 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1862 else if((pen->endcap == LineCapCustom) && pen->customend)
1863 shorten_bezier_amt(&ptcopy[count - 4],
1864 pen->width * pen->customend->inset, FALSE);
1866 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1867 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1868 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1869 pt[count - 1].X, pt[count - 1].Y);
1872 case PathPointTypeLine:
1873 if(pen->endcap == LineCapArrowAnchor)
1874 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1875 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1877 else if((pen->endcap == LineCapCustom) && pen->customend)
1878 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1879 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1880 pen->customend->inset * pen->width);
1882 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1883 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1888 ERR("Bad path last point\n");
1892 /* Find start of points */
1893 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1894 == PathPointTypeStart); j++);
1896 switch(types[j] & PathPointTypePathTypeMask){
1897 case PathPointTypeBezier:
1898 if(pen->startcap == LineCapArrowAnchor)
1899 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1900 else if((pen->startcap == LineCapCustom) && pen->customstart)
1901 shorten_bezier_amt(&ptcopy[j - 1],
1902 pen->width * pen->customstart->inset, TRUE);
1904 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1905 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1906 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1907 pt[j - 1].X, pt[j - 1].Y);
1910 case PathPointTypeLine:
1911 if(pen->startcap == LineCapArrowAnchor)
1912 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1913 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1915 else if((pen->startcap == LineCapCustom) && pen->customstart)
1916 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1917 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1918 pen->customstart->inset * pen->width);
1920 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1921 pt[j].X, pt[j].Y, pt[j - 1].X,
1926 ERR("Bad path points\n");
1931 transform_and_round_points(graphics, pti, ptcopy, count);
1933 for(i = 0; i < count; i++){
1934 tp[i] = convert_path_point_type(types[i]);
1937 PolyDraw(graphics->hdc, pti, tp, count);
1949 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1953 BeginPath(graphics->hdc);
1954 result = draw_poly(graphics, NULL, path->pathdata.Points,
1955 path->pathdata.Types, path->pathdata.Count, FALSE);
1956 EndPath(graphics->hdc);
1960 typedef struct _GraphicsContainerItem {
1962 GraphicsContainer contid;
1964 SmoothingMode smoothing;
1965 CompositingQuality compqual;
1966 InterpolationMode interpolation;
1967 CompositingMode compmode;
1968 TextRenderingHint texthint;
1971 PixelOffsetMode pixeloffset;
1973 GpMatrix* worldtrans;
1975 INT origin_x, origin_y;
1976 } GraphicsContainerItem;
1978 static GpStatus init_container(GraphicsContainerItem** container,
1979 GDIPCONST GpGraphics* graphics){
1982 *container = GdipAlloc(sizeof(GraphicsContainerItem));
1986 (*container)->contid = graphics->contid + 1;
1988 (*container)->smoothing = graphics->smoothing;
1989 (*container)->compqual = graphics->compqual;
1990 (*container)->interpolation = graphics->interpolation;
1991 (*container)->compmode = graphics->compmode;
1992 (*container)->texthint = graphics->texthint;
1993 (*container)->scale = graphics->scale;
1994 (*container)->unit = graphics->unit;
1995 (*container)->textcontrast = graphics->textcontrast;
1996 (*container)->pixeloffset = graphics->pixeloffset;
1997 (*container)->origin_x = graphics->origin_x;
1998 (*container)->origin_y = graphics->origin_y;
2000 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
2002 GdipFree(*container);
2007 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2009 GdipDeleteMatrix((*container)->worldtrans);
2010 GdipFree(*container);
2018 static void delete_container(GraphicsContainerItem* container){
2019 GdipDeleteMatrix(container->worldtrans);
2020 GdipDeleteRegion(container->clip);
2021 GdipFree(container);
2024 static GpStatus restore_container(GpGraphics* graphics,
2025 GDIPCONST GraphicsContainerItem* container){
2030 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
2034 sts = GdipCloneRegion(container->clip, &newClip);
2036 GdipDeleteMatrix(newTrans);
2040 GdipDeleteMatrix(graphics->worldtrans);
2041 graphics->worldtrans = newTrans;
2043 GdipDeleteRegion(graphics->clip);
2044 graphics->clip = newClip;
2046 graphics->contid = container->contid - 1;
2048 graphics->smoothing = container->smoothing;
2049 graphics->compqual = container->compqual;
2050 graphics->interpolation = container->interpolation;
2051 graphics->compmode = container->compmode;
2052 graphics->texthint = container->texthint;
2053 graphics->scale = container->scale;
2054 graphics->unit = container->unit;
2055 graphics->textcontrast = container->textcontrast;
2056 graphics->pixeloffset = container->pixeloffset;
2057 graphics->origin_x = container->origin_x;
2058 graphics->origin_y = container->origin_y;
2063 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2069 if(graphics->hwnd) {
2070 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2071 return GenericError;
2073 rect->X = wnd_rect.left;
2074 rect->Y = wnd_rect.top;
2075 rect->Width = wnd_rect.right - wnd_rect.left;
2076 rect->Height = wnd_rect.bottom - wnd_rect.top;
2077 }else if (graphics->image){
2078 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2079 if (stat == Ok && unit != UnitPixel)
2080 FIXME("need to convert from unit %i\n", unit);
2084 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2085 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2091 /* on success, rgn will contain the region of the graphics object which
2092 * is visible after clipping has been applied */
2093 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2099 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2102 if((stat = GdipCreateRegion(&tmp)) != Ok)
2105 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2108 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2111 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2114 GdipDeleteRegion(tmp);
2118 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2119 GDIPCONST GpStringFormat *format, HFONT *hfont)
2121 HDC hdc = CreateCompatibleDC(0);
2123 REAL angle, rel_width, rel_height, font_height, font_to_pixel_scale;
2125 HFONT unscaled_font;
2126 TEXTMETRICW textmet;
2128 font_to_pixel_scale = (format && format->generic_typographic) ? 1.0 : units_scale(UnitPoint, UnitPixel, font->family->dpi);
2130 if (font->unit == UnitPixel)
2131 font_height = font->emSize * font_to_pixel_scale;
2134 REAL unit_scale, res;
2136 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2137 unit_scale = units_scale(font->unit, graphics->unit, res);
2139 font_height = font->emSize * font_to_pixel_scale * unit_scale;
2140 if (graphics->unit != UnitDisplay)
2141 font_height /= graphics->scale;
2151 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2152 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2153 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2154 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2155 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2156 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2158 get_log_fontW(font, graphics, &lfw);
2159 lfw.lfHeight = gdip_round(font_height * rel_height);
2160 unscaled_font = CreateFontIndirectW(&lfw);
2162 SelectObject(hdc, unscaled_font);
2163 GetTextMetricsW(hdc, &textmet);
2165 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2166 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2168 *hfont = CreateFontIndirectW(&lfw);
2171 DeleteObject(unscaled_font);
2174 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2176 TRACE("(%p, %p)\n", hdc, graphics);
2178 return GdipCreateFromHDC2(hdc, NULL, graphics);
2181 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2185 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2187 if(hDevice != NULL) {
2188 FIXME("Don't know how to handle parameter hDevice\n");
2189 return NotImplemented;
2195 if(graphics == NULL)
2196 return InvalidParameter;
2198 *graphics = GdipAlloc(sizeof(GpGraphics));
2199 if(!*graphics) return OutOfMemory;
2201 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2202 GdipFree(*graphics);
2206 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2207 GdipFree((*graphics)->worldtrans);
2208 GdipFree(*graphics);
2212 (*graphics)->hdc = hdc;
2213 (*graphics)->hwnd = WindowFromDC(hdc);
2214 (*graphics)->owndc = FALSE;
2215 (*graphics)->smoothing = SmoothingModeDefault;
2216 (*graphics)->compqual = CompositingQualityDefault;
2217 (*graphics)->interpolation = InterpolationModeBilinear;
2218 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2219 (*graphics)->compmode = CompositingModeSourceOver;
2220 (*graphics)->unit = UnitDisplay;
2221 (*graphics)->scale = 1.0;
2222 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2223 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2224 (*graphics)->busy = FALSE;
2225 (*graphics)->textcontrast = 4;
2226 list_init(&(*graphics)->containers);
2227 (*graphics)->contid = 0;
2229 TRACE("<-- %p\n", *graphics);
2234 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2238 *graphics = GdipAlloc(sizeof(GpGraphics));
2239 if(!*graphics) return OutOfMemory;
2241 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2242 GdipFree(*graphics);
2246 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2247 GdipFree((*graphics)->worldtrans);
2248 GdipFree(*graphics);
2252 (*graphics)->hdc = NULL;
2253 (*graphics)->hwnd = NULL;
2254 (*graphics)->owndc = FALSE;
2255 (*graphics)->image = image;
2256 (*graphics)->smoothing = SmoothingModeDefault;
2257 (*graphics)->compqual = CompositingQualityDefault;
2258 (*graphics)->interpolation = InterpolationModeBilinear;
2259 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2260 (*graphics)->compmode = CompositingModeSourceOver;
2261 (*graphics)->unit = UnitDisplay;
2262 (*graphics)->scale = 1.0;
2263 (*graphics)->xres = image->xres;
2264 (*graphics)->yres = image->yres;
2265 (*graphics)->busy = FALSE;
2266 (*graphics)->textcontrast = 4;
2267 list_init(&(*graphics)->containers);
2268 (*graphics)->contid = 0;
2270 TRACE("<-- %p\n", *graphics);
2275 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2280 TRACE("(%p, %p)\n", hwnd, graphics);
2284 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2286 ReleaseDC(hwnd, hdc);
2290 (*graphics)->hwnd = hwnd;
2291 (*graphics)->owndc = TRUE;
2296 /* FIXME: no icm handling */
2297 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2299 TRACE("(%p, %p)\n", hwnd, graphics);
2301 return GdipCreateFromHWND(hwnd, graphics);
2304 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2305 GpMetafile **metafile)
2307 IStream *stream = NULL;
2309 ENHMETAHEADER *copy;
2310 GpStatus retval = Ok;
2312 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2314 if(!hemf || !metafile)
2315 return InvalidParameter;
2317 read = GetEnhMetaFileBits(hemf, 0, NULL);
2318 copy = GdipAlloc(read);
2319 GetEnhMetaFileBits(hemf, read, (BYTE *)copy);
2321 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
2322 ERR("could not make stream\n");
2324 retval = GenericError;
2328 *metafile = GdipAlloc(sizeof(GpMetafile));
2330 retval = OutOfMemory;
2334 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2335 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
2337 retval = GenericError;
2342 (*metafile)->image.type = ImageTypeMetafile;
2343 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
2344 (*metafile)->image.palette = NULL;
2345 (*metafile)->image.xres = (REAL)copy->szlDevice.cx;
2346 (*metafile)->image.yres = (REAL)copy->szlDevice.cy;
2347 (*metafile)->bounds.X = (REAL)copy->rclBounds.left;
2348 (*metafile)->bounds.Y = (REAL)copy->rclBounds.top;
2349 (*metafile)->bounds.Width = (REAL)(copy->rclBounds.right - copy->rclBounds.left);
2350 (*metafile)->bounds.Height = (REAL)(copy->rclBounds.bottom - copy->rclBounds.top);
2351 (*metafile)->unit = UnitPixel;
2354 DeleteEnhMetaFile(hemf);
2356 TRACE("<-- %p\n", *metafile);
2360 GdipFree(*metafile);
2361 IStream_Release(stream);
2365 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2366 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2371 GpStatus retval = Ok;
2373 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2375 if(!hwmf || !metafile || !placeable)
2376 return InvalidParameter;
2379 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2381 return GenericError;
2382 copy = GdipAlloc(read);
2383 GetMetaFileBitsEx(hwmf, read, copy);
2385 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2388 retval = GdipCreateMetafileFromEmf(hemf, FALSE, metafile);
2392 (*metafile)->image.xres = (REAL)placeable->Inch;
2393 (*metafile)->image.yres = (REAL)placeable->Inch;
2394 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2395 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2396 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2397 placeable->BoundingBox.Left);
2398 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2399 placeable->BoundingBox.Top);
2401 if (delete) DeleteMetaFile(hwmf);
2406 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2407 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2409 HMETAFILE hmf = GetMetaFileW(file);
2411 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2413 if(!hmf) return InvalidParameter;
2415 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2418 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2419 GpMetafile **metafile)
2421 FIXME("(%p, %p): stub\n", file, metafile);
2422 return NotImplemented;
2425 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2426 GpMetafile **metafile)
2428 FIXME("(%p, %p): stub\n", stream, metafile);
2429 return NotImplemented;
2432 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2433 UINT access, IStream **stream)
2438 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2440 if(!stream || !filename)
2441 return InvalidParameter;
2443 if(access & GENERIC_WRITE)
2444 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2445 else if(access & GENERIC_READ)
2446 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2448 return InvalidParameter;
2450 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2452 return hresult_to_status(ret);
2455 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2457 GraphicsContainerItem *cont, *next;
2459 TRACE("(%p)\n", graphics);
2461 if(!graphics) return InvalidParameter;
2462 if(graphics->busy) return ObjectBusy;
2464 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2466 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2472 ReleaseDC(graphics->hwnd, graphics->hdc);
2474 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2475 list_remove(&cont->entry);
2476 delete_container(cont);
2479 GdipDeleteRegion(graphics->clip);
2480 GdipDeleteMatrix(graphics->worldtrans);
2486 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2487 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2489 INT save_state, num_pts;
2490 GpPointF points[MAX_ARC_PTS];
2493 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2494 width, height, startAngle, sweepAngle);
2496 if(!graphics || !pen || width <= 0 || height <= 0)
2497 return InvalidParameter;
2504 FIXME("graphics object has no HDC\n");
2508 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2510 save_state = prepare_dc(graphics, pen);
2512 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2514 restore_dc(graphics, save_state);
2519 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2520 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2522 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2523 width, height, startAngle, sweepAngle);
2525 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2528 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2529 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2535 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2536 x2, y2, x3, y3, x4, y4);
2538 if(!graphics || !pen)
2539 return InvalidParameter;
2546 FIXME("graphics object has no HDC\n");
2559 save_state = prepare_dc(graphics, pen);
2561 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2563 restore_dc(graphics, save_state);
2568 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2569 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2575 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2576 x2, y2, x3, y3, x4, y4);
2578 if(!graphics || !pen)
2579 return InvalidParameter;
2586 FIXME("graphics object has no HDC\n");
2599 save_state = prepare_dc(graphics, pen);
2601 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2603 restore_dc(graphics, save_state);
2608 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2609 GDIPCONST GpPointF *points, INT count)
2614 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2616 if(!graphics || !pen || !points || (count <= 0))
2617 return InvalidParameter;
2622 for(i = 0; i < floor(count / 4); i++){
2623 ret = GdipDrawBezier(graphics, pen,
2624 points[4*i].X, points[4*i].Y,
2625 points[4*i + 1].X, points[4*i + 1].Y,
2626 points[4*i + 2].X, points[4*i + 2].Y,
2627 points[4*i + 3].X, points[4*i + 3].Y);
2635 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2636 GDIPCONST GpPoint *points, INT count)
2642 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2644 if(!graphics || !pen || !points || (count <= 0))
2645 return InvalidParameter;
2650 pts = GdipAlloc(sizeof(GpPointF) * count);
2654 for(i = 0; i < count; i++){
2655 pts[i].X = (REAL)points[i].X;
2656 pts[i].Y = (REAL)points[i].Y;
2659 ret = GdipDrawBeziers(graphics,pen,pts,count);
2666 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2667 GDIPCONST GpPointF *points, INT count)
2669 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2671 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2674 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2675 GDIPCONST GpPoint *points, INT count)
2677 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2679 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2682 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2683 GDIPCONST GpPointF *points, INT count, REAL tension)
2688 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2690 if(!graphics || !pen || !points || count <= 0)
2691 return InvalidParameter;
2696 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2699 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2701 GdipDeletePath(path);
2705 stat = GdipDrawPath(graphics, pen, path);
2707 GdipDeletePath(path);
2712 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2713 GDIPCONST GpPoint *points, INT count, REAL tension)
2719 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2721 if(!points || count <= 0)
2722 return InvalidParameter;
2724 ptf = GdipAlloc(sizeof(GpPointF)*count);
2728 for(i = 0; i < count; i++){
2729 ptf[i].X = (REAL)points[i].X;
2730 ptf[i].Y = (REAL)points[i].Y;
2733 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2740 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2741 GDIPCONST GpPointF *points, INT count)
2743 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2745 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2748 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2749 GDIPCONST GpPoint *points, INT count)
2755 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2758 return InvalidParameter;
2760 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2764 for(i = 0; i < count; i++){
2765 pointsF[i].X = (REAL)points[i].X;
2766 pointsF[i].Y = (REAL)points[i].Y;
2769 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2775 /* Approximates cardinal spline with Bezier curves. */
2776 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2777 GDIPCONST GpPointF *points, INT count, REAL tension)
2779 /* PolyBezier expects count*3-2 points. */
2780 INT i, len_pt = count*3-2, save_state;
2782 REAL x1, x2, y1, y2;
2785 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2787 if(!graphics || !pen)
2788 return InvalidParameter;
2794 return InvalidParameter;
2798 FIXME("graphics object has no HDC\n");
2802 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2806 tension = tension * TENSION_CONST;
2808 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2811 pt[0].X = points[0].X;
2812 pt[0].Y = points[0].Y;
2816 for(i = 0; i < count-2; i++){
2817 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2821 pt[3*i+3].X = points[i+1].X;
2822 pt[3*i+3].Y = points[i+1].Y;
2827 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2828 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2830 pt[len_pt-2].X = x1;
2831 pt[len_pt-2].Y = y1;
2832 pt[len_pt-1].X = points[count-1].X;
2833 pt[len_pt-1].Y = points[count-1].Y;
2835 save_state = prepare_dc(graphics, pen);
2837 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2840 restore_dc(graphics, save_state);
2845 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2846 GDIPCONST GpPoint *points, INT count, REAL tension)
2852 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2855 return InvalidParameter;
2857 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2861 for(i = 0; i < count; i++){
2862 pointsF[i].X = (REAL)points[i].X;
2863 pointsF[i].Y = (REAL)points[i].Y;
2866 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2872 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2873 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2876 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2878 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2879 return InvalidParameter;
2882 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2885 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2886 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2889 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2895 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2896 return InvalidParameter;
2899 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2902 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2903 REAL y, REAL width, REAL height)
2909 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2911 if(!graphics || !pen)
2912 return InvalidParameter;
2919 FIXME("graphics object has no HDC\n");
2925 ptf[1].X = x + width;
2926 ptf[1].Y = y + height;
2928 save_state = prepare_dc(graphics, pen);
2929 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2931 transform_and_round_points(graphics, pti, ptf, 2);
2933 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2935 restore_dc(graphics, save_state);
2940 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2941 INT y, INT width, INT height)
2943 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2945 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2949 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2953 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2955 if(!graphics || !image)
2956 return InvalidParameter;
2958 GdipGetImageWidth(image, &width);
2959 GdipGetImageHeight(image, &height);
2961 return GdipDrawImagePointRect(graphics, image, x, y,
2962 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2965 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2968 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2970 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2973 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2974 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2978 REAL scale_x, scale_y, width, height;
2980 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2982 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2983 scale_x *= graphics->xres / image->xres;
2984 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2985 scale_y *= graphics->yres / image->yres;
2986 width = srcwidth * scale_x;
2987 height = srcheight * scale_y;
2989 points[0].X = points[2].X = x;
2990 points[0].Y = points[1].Y = y;
2991 points[1].X = x + width;
2992 points[2].Y = y + height;
2994 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2995 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2998 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2999 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3002 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3005 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3006 GDIPCONST GpPointF *dstpoints, INT count)
3010 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3013 return InvalidParameter;
3015 GdipGetImageWidth(image, &width);
3016 GdipGetImageHeight(image, &height);
3018 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3019 width, height, UnitPixel, NULL, NULL, NULL);
3022 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3023 GDIPCONST GpPoint *dstpoints, INT count)
3027 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3029 if (count != 3 || !dstpoints)
3030 return InvalidParameter;
3032 ptf[0].X = (REAL)dstpoints[0].X;
3033 ptf[0].Y = (REAL)dstpoints[0].Y;
3034 ptf[1].X = (REAL)dstpoints[1].X;
3035 ptf[1].Y = (REAL)dstpoints[1].Y;
3036 ptf[2].X = (REAL)dstpoints[2].X;
3037 ptf[2].Y = (REAL)dstpoints[2].Y;
3039 return GdipDrawImagePoints(graphics, image, ptf, count);
3042 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3043 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3044 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3045 DrawImageAbort callback, VOID * callbackData)
3051 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3052 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3056 return NotImplemented;
3058 if(!graphics || !image || !points || count != 3)
3059 return InvalidParameter;
3061 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3062 debugstr_pointf(&points[2]));
3064 memcpy(ptf, points, 3 * sizeof(GpPointF));
3065 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3066 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3067 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3069 transform_and_round_points(graphics, pti, ptf, 4);
3071 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3072 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3074 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3075 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3076 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3077 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3078 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3084 FIXME("graphics object has no HDC\n");
3087 if(IPicture_Render(image->picture, graphics->hdc,
3088 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3089 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3092 callback(callbackData);
3093 return GenericError;
3096 else if (image->type == ImageTypeBitmap)
3098 GpBitmap* bitmap = (GpBitmap*)image;
3101 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3102 graphics->xres, graphics->yres,
3103 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3104 graphics->scale, image->xres, image->yres, bitmap->format,
3105 imageAttributes ? imageAttributes->outside_color : 0);
3107 if (imageAttributes ||
3108 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3109 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3110 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3111 srcx < 0 || srcy < 0 ||
3112 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3119 int i, x, y, src_stride, dst_stride;
3120 GpMatrix *dst_to_src;
3121 REAL m11, m12, m21, m22, mdx, mdy;
3122 LPBYTE src_data, dst_data;
3123 BitmapData lockeddata;
3124 InterpolationMode interpolation = graphics->interpolation;
3125 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3126 REAL x_dx, x_dy, y_dx, y_dy;
3127 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3129 if (!imageAttributes)
3130 imageAttributes = &defaultImageAttributes;
3132 dst_area.left = dst_area.right = pti[0].x;
3133 dst_area.top = dst_area.bottom = pti[0].y;
3136 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3137 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3138 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3139 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3142 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3144 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3145 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3146 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3147 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3148 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3149 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3151 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
3152 if (stat != Ok) return stat;
3154 stat = GdipInvertMatrix(dst_to_src);
3157 GdipDeleteMatrix(dst_to_src);
3161 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3164 GdipDeleteMatrix(dst_to_src);
3168 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3170 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3171 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3173 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3175 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3179 GdipDeleteMatrix(dst_to_src);
3182 src_stride = sizeof(ARGB) * src_area.Width;
3184 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3185 lockeddata.Width = src_area.Width;
3186 lockeddata.Height = src_area.Height;
3187 lockeddata.Stride = src_stride;
3188 lockeddata.PixelFormat = PixelFormat32bppARGB;
3189 lockeddata.Scan0 = src_data;
3191 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3192 PixelFormat32bppARGB, &lockeddata);
3195 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3199 if (src_data != dst_data)
3202 GdipDeleteMatrix(dst_to_src);
3206 apply_image_attributes(imageAttributes, src_data,
3207 src_area.Width, src_area.Height,
3208 src_stride, ColorAdjustTypeBitmap);
3210 /* Transform the bits as needed to the destination. */
3211 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
3213 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3214 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3215 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3216 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3218 for (x=dst_area.left; x<dst_area.right; x++)
3220 for (y=dst_area.top; y<dst_area.bottom; y++)
3222 GpPointF src_pointf;
3225 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3226 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3228 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3230 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3231 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
3237 GdipDeleteMatrix(dst_to_src);
3241 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3242 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3251 int temp_hdc=0, temp_bitmap=0;
3252 HBITMAP hbitmap, old_hbm=NULL;
3254 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3255 bitmap->format == PixelFormat24bppRGB ||
3256 bitmap->format == PixelFormat32bppRGB ||
3257 bitmap->format == PixelFormat32bppPARGB))
3259 BITMAPINFOHEADER bih;
3261 PixelFormat dst_format;
3263 /* we can't draw a bitmap of this format directly */
3264 hdc = CreateCompatibleDC(0);
3268 bih.biSize = sizeof(BITMAPINFOHEADER);
3269 bih.biWidth = bitmap->width;
3270 bih.biHeight = -bitmap->height;
3272 bih.biBitCount = 32;
3273 bih.biCompression = BI_RGB;
3274 bih.biSizeImage = 0;
3275 bih.biXPelsPerMeter = 0;
3276 bih.biYPelsPerMeter = 0;
3278 bih.biClrImportant = 0;
3280 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3281 (void**)&temp_bits, NULL, 0);
3283 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3284 dst_format = PixelFormat32bppPARGB;
3286 dst_format = PixelFormat32bppRGB;
3288 convert_pixels(bitmap->width, bitmap->height,
3289 bitmap->width*4, temp_bits, dst_format,
3290 bitmap->stride, bitmap->bits, bitmap->format,
3291 bitmap->image.palette);
3295 if (bitmap->hbitmap)
3296 hbitmap = bitmap->hbitmap;
3299 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3304 temp_hdc = (hdc == 0);
3309 if (!hdc) hdc = CreateCompatibleDC(0);
3310 old_hbm = SelectObject(hdc, hbitmap);
3313 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3315 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3316 hdc, srcx, srcy, srcwidth, srcheight);
3320 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3321 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3326 SelectObject(hdc, old_hbm);
3331 DeleteObject(hbitmap);
3336 ERR("GpImage with no IPicture or HBITMAP?!\n");
3337 return NotImplemented;
3343 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3344 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3345 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3346 DrawImageAbort callback, VOID * callbackData)
3348 GpPointF pointsF[3];
3351 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3352 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3355 if(!points || count!=3)
3356 return InvalidParameter;
3358 for(i = 0; i < count; i++){
3359 pointsF[i].X = (REAL)points[i].X;
3360 pointsF[i].Y = (REAL)points[i].Y;
3363 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3364 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3365 callback, callbackData);
3368 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3369 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3370 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3371 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3372 VOID * callbackData)
3376 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3377 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3378 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3382 points[1].X = dstx + dstwidth;
3385 points[2].Y = dsty + dstheight;
3387 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3388 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3391 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3392 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3393 INT srcwidth, INT srcheight, GpUnit srcUnit,
3394 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3395 VOID * callbackData)
3399 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3400 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3401 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3405 points[1].X = dstx + dstwidth;
3408 points[2].Y = dsty + dstheight;
3410 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3411 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3414 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3415 REAL x, REAL y, REAL width, REAL height)
3421 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3423 if(!graphics || !image)
3424 return InvalidParameter;
3426 ret = GdipGetImageBounds(image, &bounds, &unit);
3430 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3431 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3432 unit, NULL, NULL, NULL);
3435 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3436 INT x, INT y, INT width, INT height)
3438 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3440 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3443 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3444 REAL y1, REAL x2, REAL y2)
3450 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3452 if(!pen || !graphics)
3453 return InvalidParameter;
3460 FIXME("graphics object has no HDC\n");
3469 save_state = prepare_dc(graphics, pen);
3471 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3473 restore_dc(graphics, save_state);
3478 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3479 INT y1, INT x2, INT y2)
3485 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3487 if(!pen || !graphics)
3488 return InvalidParameter;
3495 FIXME("graphics object has no HDC\n");
3504 save_state = prepare_dc(graphics, pen);
3506 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3508 restore_dc(graphics, save_state);
3513 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3514 GpPointF *points, INT count)
3519 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3521 if(!pen || !graphics || (count < 2))
3522 return InvalidParameter;
3529 FIXME("graphics object has no HDC\n");
3533 save_state = prepare_dc(graphics, pen);
3535 retval = draw_polyline(graphics, pen, points, count, TRUE);
3537 restore_dc(graphics, save_state);
3542 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3543 GpPoint *points, INT count)
3547 GpPointF *ptf = NULL;
3550 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3552 if(!pen || !graphics || (count < 2))
3553 return InvalidParameter;
3560 FIXME("graphics object has no HDC\n");
3564 ptf = GdipAlloc(count * sizeof(GpPointF));
3565 if(!ptf) return OutOfMemory;
3567 for(i = 0; i < count; i ++){
3568 ptf[i].X = (REAL) points[i].X;
3569 ptf[i].Y = (REAL) points[i].Y;
3572 save_state = prepare_dc(graphics, pen);
3574 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3576 restore_dc(graphics, save_state);
3582 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3587 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3589 if(!pen || !graphics)
3590 return InvalidParameter;
3597 FIXME("graphics object has no HDC\n");
3601 save_state = prepare_dc(graphics, pen);
3603 retval = draw_poly(graphics, pen, path->pathdata.Points,
3604 path->pathdata.Types, path->pathdata.Count, TRUE);
3606 restore_dc(graphics, save_state);
3611 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3612 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3616 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3617 width, height, startAngle, sweepAngle);
3619 if(!graphics || !pen)
3620 return InvalidParameter;
3627 FIXME("graphics object has no HDC\n");
3631 save_state = prepare_dc(graphics, pen);
3632 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3634 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3636 restore_dc(graphics, save_state);
3641 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3642 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3644 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3645 width, height, startAngle, sweepAngle);
3647 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3650 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3651 REAL y, REAL width, REAL height)
3657 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3659 if(!pen || !graphics)
3660 return InvalidParameter;
3667 FIXME("graphics object has no HDC\n");
3673 ptf[1].X = x + width;
3675 ptf[2].X = x + width;
3676 ptf[2].Y = y + height;
3678 ptf[3].Y = y + height;
3680 save_state = prepare_dc(graphics, pen);
3681 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3683 transform_and_round_points(graphics, pti, ptf, 4);
3684 Polygon(graphics->hdc, pti, 4);
3686 restore_dc(graphics, save_state);
3691 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3692 INT y, INT width, INT height)
3694 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3696 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3699 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3700 GDIPCONST GpRectF* rects, INT count)
3706 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3708 if(!graphics || !pen || !rects || count < 1)
3709 return InvalidParameter;
3716 FIXME("graphics object has no HDC\n");
3720 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3721 pti = GdipAlloc(4 * count * sizeof(POINT));
3729 for(i = 0; i < count; i++){
3730 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3731 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3732 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3733 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3736 save_state = prepare_dc(graphics, pen);
3737 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3739 transform_and_round_points(graphics, pti, ptf, 4 * count);
3741 for(i = 0; i < count; i++)
3742 Polygon(graphics->hdc, &pti[4 * i], 4);
3744 restore_dc(graphics, save_state);
3752 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3753 GDIPCONST GpRect* rects, INT count)
3759 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3761 if(!rects || count<=0)
3762 return InvalidParameter;
3764 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3768 for(i = 0;i < count;i++){
3769 rectsF[i].X = (REAL)rects[i].X;
3770 rectsF[i].Y = (REAL)rects[i].Y;
3771 rectsF[i].Width = (REAL)rects[i].Width;
3772 rectsF[i].Height = (REAL)rects[i].Height;
3775 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3781 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3782 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3787 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3788 count, tension, fill);
3790 if(!graphics || !brush || !points)
3791 return InvalidParameter;
3796 if(count == 1) /* Do nothing */
3799 stat = GdipCreatePath(fill, &path);
3803 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3805 GdipDeletePath(path);
3809 stat = GdipFillPath(graphics, brush, path);
3811 GdipDeletePath(path);
3815 GdipDeletePath(path);
3820 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3821 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3827 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3828 count, tension, fill);
3830 if(!points || count == 0)
3831 return InvalidParameter;
3833 if(count == 1) /* Do nothing */
3836 ptf = GdipAlloc(sizeof(GpPointF)*count);
3840 for(i = 0;i < count;i++){
3841 ptf[i].X = (REAL)points[i].X;
3842 ptf[i].Y = (REAL)points[i].Y;
3845 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3852 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3853 GDIPCONST GpPointF *points, INT count)
3855 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3856 return GdipFillClosedCurve2(graphics, brush, points, count,
3857 0.5f, FillModeAlternate);
3860 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3861 GDIPCONST GpPoint *points, INT count)
3863 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3864 return GdipFillClosedCurve2I(graphics, brush, points, count,
3865 0.5f, FillModeAlternate);
3868 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3869 REAL y, REAL width, REAL height)
3874 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3876 if(!graphics || !brush)
3877 return InvalidParameter;
3882 stat = GdipCreatePath(FillModeAlternate, &path);
3886 stat = GdipAddPathEllipse(path, x, y, width, height);
3889 stat = GdipFillPath(graphics, brush, path);
3891 GdipDeletePath(path);
3897 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3898 INT y, INT width, INT height)
3900 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3902 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3905 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3910 if(!graphics->hdc || !brush_can_fill_path(brush))
3911 return NotImplemented;
3913 save_state = SaveDC(graphics->hdc);
3914 EndPath(graphics->hdc);
3915 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3918 BeginPath(graphics->hdc);
3919 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3920 path->pathdata.Types, path->pathdata.Count, FALSE);
3925 EndPath(graphics->hdc);
3926 brush_fill_path(graphics, brush);
3931 RestoreDC(graphics->hdc, save_state);
3936 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3941 if (!brush_can_fill_pixels(brush))
3942 return NotImplemented;
3944 /* FIXME: This could probably be done more efficiently without regions. */
3946 stat = GdipCreateRegionPath(path, &rgn);
3950 stat = GdipFillRegion(graphics, brush, rgn);
3952 GdipDeleteRegion(rgn);
3958 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3960 GpStatus stat = NotImplemented;
3962 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3964 if(!brush || !graphics || !path)
3965 return InvalidParameter;
3970 if (!graphics->image)
3971 stat = GDI32_GdipFillPath(graphics, brush, path);
3973 if (stat == NotImplemented)
3974 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3976 if (stat == NotImplemented)
3978 FIXME("Not implemented for brushtype %i\n", brush->bt);
3985 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3986 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3991 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3992 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3994 if(!graphics || !brush)
3995 return InvalidParameter;
4000 stat = GdipCreatePath(FillModeAlternate, &path);
4004 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4007 stat = GdipFillPath(graphics, brush, path);
4009 GdipDeletePath(path);
4015 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4016 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4018 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4019 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4021 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4024 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4025 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4030 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4032 if(!graphics || !brush || !points || !count)
4033 return InvalidParameter;
4038 stat = GdipCreatePath(fillMode, &path);
4042 stat = GdipAddPathPolygon(path, points, count);
4045 stat = GdipFillPath(graphics, brush, path);
4047 GdipDeletePath(path);
4053 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4054 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4059 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4061 if(!graphics || !brush || !points || !count)
4062 return InvalidParameter;
4067 stat = GdipCreatePath(fillMode, &path);
4071 stat = GdipAddPathPolygonI(path, points, count);
4074 stat = GdipFillPath(graphics, brush, path);
4076 GdipDeletePath(path);
4082 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4083 GDIPCONST GpPointF *points, INT count)
4085 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4087 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4090 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4091 GDIPCONST GpPoint *points, INT count)
4093 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4095 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4098 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4099 REAL x, REAL y, REAL width, REAL height)
4104 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4106 if(!graphics || !brush)
4107 return InvalidParameter;
4112 stat = GdipCreatePath(FillModeAlternate, &path);
4116 stat = GdipAddPathRectangle(path, x, y, width, height);
4119 stat = GdipFillPath(graphics, brush, path);
4121 GdipDeletePath(path);
4127 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4128 INT x, INT y, INT width, INT height)
4130 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4132 return GdipFillRectangle(graphics, brush, x, y, width, height);
4135 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4141 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4144 return InvalidParameter;
4146 for(i = 0; i < count; i++){
4147 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4148 if(ret != Ok) return ret;
4154 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4161 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4163 if(!rects || count <= 0)
4164 return InvalidParameter;
4166 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4170 for(i = 0; i < count; i++){
4171 rectsF[i].X = (REAL)rects[i].X;
4172 rectsF[i].Y = (REAL)rects[i].Y;
4173 rectsF[i].X = (REAL)rects[i].Width;
4174 rectsF[i].Height = (REAL)rects[i].Height;
4177 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4183 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4191 if(!graphics->hdc || !brush_can_fill_path(brush))
4192 return NotImplemented;
4194 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4198 save_state = SaveDC(graphics->hdc);
4199 EndPath(graphics->hdc);
4201 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4203 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4205 BeginPath(graphics->hdc);
4206 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4207 EndPath(graphics->hdc);
4209 brush_fill_path(graphics, brush);
4212 RestoreDC(graphics->hdc, save_state);
4219 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4223 GpRegion *temp_region;
4224 GpMatrix *world_to_device;
4225 GpRectF graphics_bounds;
4229 GpRect gp_bound_rect;
4231 if (!brush_can_fill_pixels(brush))
4232 return NotImplemented;
4234 stat = get_graphics_bounds(graphics, &graphics_bounds);
4237 stat = GdipCloneRegion(region, &temp_region);
4241 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4242 CoordinateSpaceWorld, &world_to_device);
4246 stat = GdipTransformRegion(temp_region, world_to_device);
4248 GdipDeleteMatrix(world_to_device);
4252 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4255 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4257 GdipDeleteRegion(temp_region);
4260 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4262 DeleteObject(hregion);
4268 gp_bound_rect.X = bound_rect.left;
4269 gp_bound_rect.Y = bound_rect.top;
4270 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4271 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4273 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4279 stat = brush_fill_pixels(graphics, brush, pixel_data,
4280 &gp_bound_rect, gp_bound_rect.Width);
4283 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4284 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4285 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4287 GdipFree(pixel_data);
4290 DeleteObject(hregion);
4296 /*****************************************************************************
4297 * GdipFillRegion [GDIPLUS.@]
4299 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4302 GpStatus stat = NotImplemented;
4304 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4306 if (!(graphics && brush && region))
4307 return InvalidParameter;
4312 if (!graphics->image)
4313 stat = GDI32_GdipFillRegion(graphics, brush, region);
4315 if (stat == NotImplemented)
4316 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4318 if (stat == NotImplemented)
4320 FIXME("not implemented for brushtype %i\n", brush->bt);
4327 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4329 TRACE("(%p,%u)\n", graphics, intention);
4332 return InvalidParameter;
4337 /* We have no internal operation queue, so there's no need to clear it. */
4345 /*****************************************************************************
4346 * GdipGetClipBounds [GDIPLUS.@]
4348 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4350 TRACE("(%p, %p)\n", graphics, rect);
4353 return InvalidParameter;
4358 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4361 /*****************************************************************************
4362 * GdipGetClipBoundsI [GDIPLUS.@]
4364 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4366 TRACE("(%p, %p)\n", graphics, rect);
4369 return InvalidParameter;
4374 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4377 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4378 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4379 CompositingMode *mode)
4381 TRACE("(%p, %p)\n", graphics, mode);
4383 if(!graphics || !mode)
4384 return InvalidParameter;
4389 *mode = graphics->compmode;
4394 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4395 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4396 CompositingQuality *quality)
4398 TRACE("(%p, %p)\n", graphics, quality);
4400 if(!graphics || !quality)
4401 return InvalidParameter;
4406 *quality = graphics->compqual;
4411 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4412 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4413 InterpolationMode *mode)
4415 TRACE("(%p, %p)\n", graphics, mode);
4417 if(!graphics || !mode)
4418 return InvalidParameter;
4423 *mode = graphics->interpolation;
4428 /* FIXME: Need to handle color depths less than 24bpp */
4429 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4431 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4433 if(!graphics || !argb)
4434 return InvalidParameter;
4442 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4444 TRACE("(%p, %p)\n", graphics, scale);
4446 if(!graphics || !scale)
4447 return InvalidParameter;
4452 *scale = graphics->scale;
4457 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4459 TRACE("(%p, %p)\n", graphics, unit);
4461 if(!graphics || !unit)
4462 return InvalidParameter;
4467 *unit = graphics->unit;
4472 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4473 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4476 TRACE("(%p, %p)\n", graphics, mode);
4478 if(!graphics || !mode)
4479 return InvalidParameter;
4484 *mode = graphics->pixeloffset;
4489 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4490 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4492 TRACE("(%p, %p)\n", graphics, mode);
4494 if(!graphics || !mode)
4495 return InvalidParameter;
4500 *mode = graphics->smoothing;
4505 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4507 TRACE("(%p, %p)\n", graphics, contrast);
4509 if(!graphics || !contrast)
4510 return InvalidParameter;
4512 *contrast = graphics->textcontrast;
4517 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4518 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4519 TextRenderingHint *hint)
4521 TRACE("(%p, %p)\n", graphics, hint);
4523 if(!graphics || !hint)
4524 return InvalidParameter;
4529 *hint = graphics->texthint;
4534 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4539 TRACE("(%p, %p)\n", graphics, rect);
4541 if(!graphics || !rect)
4542 return InvalidParameter;
4547 /* intersect window and graphics clipping regions */
4548 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4551 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4554 /* get bounds of the region */
4555 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4558 GdipDeleteRegion(clip_rgn);
4563 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4568 TRACE("(%p, %p)\n", graphics, rect);
4570 if(!graphics || !rect)
4571 return InvalidParameter;
4573 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4575 rect->X = gdip_round(rectf.X);
4576 rect->Y = gdip_round(rectf.Y);
4577 rect->Width = gdip_round(rectf.Width);
4578 rect->Height = gdip_round(rectf.Height);
4584 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4586 TRACE("(%p, %p)\n", graphics, matrix);
4588 if(!graphics || !matrix)
4589 return InvalidParameter;
4594 *matrix = *graphics->worldtrans;
4598 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4604 TRACE("(%p, %x)\n", graphics, color);
4607 return InvalidParameter;
4612 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4615 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4616 GdipDeleteBrush((GpBrush*)brush);
4620 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4621 wnd_rect.Width, wnd_rect.Height);
4623 GdipDeleteBrush((GpBrush*)brush);
4628 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4630 TRACE("(%p, %p)\n", graphics, res);
4632 if(!graphics || !res)
4633 return InvalidParameter;
4635 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4638 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4644 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4646 if(!graphics || !result)
4647 return InvalidParameter;
4654 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4655 CoordinateSpaceWorld, &pt, 1)) != Ok)
4658 if((stat = GdipCreateRegion(&rgn)) != Ok)
4661 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4664 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4667 GdipDeleteRegion(rgn);
4671 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4673 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4676 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4682 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4684 if(!graphics || !result)
4685 return InvalidParameter;
4692 pts[1].X = x + width;
4693 pts[1].Y = y + height;
4695 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4696 CoordinateSpaceWorld, pts, 2)) != Ok)
4699 pts[1].X -= pts[0].X;
4700 pts[1].Y -= pts[0].Y;
4702 if((stat = GdipCreateRegion(&rgn)) != Ok)
4705 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4708 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4711 GdipDeleteRegion(rgn);
4715 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4717 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4720 GpStatus gdip_format_string(HDC hdc,
4721 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4722 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4723 gdip_format_string_callback callback, void *user_data)
4726 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4727 nheight, lineend, lineno = 0;
4729 StringAlignment halign;
4732 HotkeyPrefix hkprefix;
4733 INT *hotkeyprefix_offsets=NULL;
4734 INT hotkeyprefix_count=0;
4735 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4738 if(length == -1) length = lstrlenW(string);
4740 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4741 if(!stringdup) return OutOfMemory;
4743 nwidth = rect->Width;
4744 nheight = rect->Height;
4747 hkprefix = format->hkprefix;
4749 hkprefix = HotkeyPrefixNone;
4751 if (hkprefix == HotkeyPrefixShow)
4753 for (i=0; i<length; i++)
4755 if (string[i] == '&')
4756 hotkeyprefix_count++;
4760 if (hotkeyprefix_count)
4761 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4763 hotkeyprefix_count = 0;
4765 for(i = 0, j = 0; i < length; i++){
4766 /* FIXME: This makes the indexes passed to callback inaccurate. */
4767 if(!isprintW(string[i]) && (string[i] != '\n'))
4770 /* FIXME: tabs should be handled using tabstops from stringformat */
4771 if (string[i] == '\t')
4774 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4775 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4776 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4784 stringdup[j] = string[i];
4790 if (format) halign = format->align;
4791 else halign = StringAlignmentNear;
4793 while(sum < length){
4794 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4795 nwidth, &fit, NULL, &size);
4801 for(lret = 0; lret < fit; lret++)
4802 if(*(stringdup + sum + lret) == '\n')
4805 /* Line break code (may look strange, but it imitates windows). */
4807 lineend = fit = lret; /* this is not an off-by-one error */
4808 else if(fit < (length - sum)){
4809 if(*(stringdup + sum + fit) == ' ')
4810 while(*(stringdup + sum + fit) == ' ')
4813 while(*(stringdup + sum + fit - 1) != ' '){
4816 if(*(stringdup + sum + fit) == '\t')
4825 while(*(stringdup + sum + lineend - 1) == ' ' ||
4826 *(stringdup + sum + lineend - 1) == '\t')
4832 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4833 nwidth, &j, NULL, &size);
4835 bounds.Width = size.cx;
4837 if(height + size.cy > nheight)
4838 bounds.Height = nheight - (height + size.cy);
4840 bounds.Height = size.cy;
4842 bounds.Y = rect->Y + height;
4846 case StringAlignmentNear:
4850 case StringAlignmentCenter:
4851 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4853 case StringAlignmentFar:
4854 bounds.X = rect->X + rect->Width - bounds.Width;
4858 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4859 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4862 stat = callback(hdc, stringdup, sum, lineend,
4863 font, rect, format, lineno, &bounds,
4864 &hotkeyprefix_offsets[hotkeyprefix_pos],
4865 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4870 sum += fit + (lret < fitcpy ? 1 : 0);
4874 hotkeyprefix_pos = hotkeyprefix_end_pos;
4876 if(height > nheight)
4879 /* Stop if this was a linewrap (but not if it was a linebreak). */
4880 if ((lret == fitcpy) && format &&
4881 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4885 GdipFree(stringdup);
4886 GdipFree(hotkeyprefix_offsets);
4891 struct measure_ranges_args {
4893 REAL rel_width, rel_height;
4896 static GpStatus measure_ranges_callback(HDC hdc,
4897 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4898 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4899 INT lineno, const RectF *bounds, INT *underlined_indexes,
4900 INT underlined_index_count, void *user_data)
4904 struct measure_ranges_args *args = user_data;
4906 for (i=0; i<format->range_count; i++)
4908 INT range_start = max(index, format->character_ranges[i].First);
4909 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4910 if (range_start < range_end)
4915 range_rect.Y = bounds->Y / args->rel_height;
4916 range_rect.Height = bounds->Height / args->rel_height;
4918 GetTextExtentExPointW(hdc, string + index, range_start - index,
4919 INT_MAX, NULL, NULL, &range_size);
4920 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4922 GetTextExtentExPointW(hdc, string + index, range_end - index,
4923 INT_MAX, NULL, NULL, &range_size);
4924 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4926 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4935 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4936 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4937 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4938 INT regionCount, GpRegion** regions)
4942 HFONT gdifont, oldfont;
4943 struct measure_ranges_args args;
4944 HDC hdc, temp_hdc=NULL;
4949 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4950 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4952 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4953 return InvalidParameter;
4955 if (regionCount < stringFormat->range_count)
4956 return InvalidParameter;
4960 hdc = temp_hdc = CreateCompatibleDC(0);
4961 if (!temp_hdc) return OutOfMemory;
4964 hdc = graphics->hdc;
4966 if (stringFormat->attr)
4967 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4975 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4976 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4977 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4978 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4979 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4981 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4982 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4984 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4985 scaled_rect.Y = layoutRect->Y * args.rel_height;
4986 if (stringFormat->attr & StringFormatFlagsNoClip)
4988 scaled_rect.Width = (REAL)(1 << 23);
4989 scaled_rect.Height = (REAL)(1 << 23);
4993 scaled_rect.Width = layoutRect->Width * args.rel_width;
4994 scaled_rect.Height = layoutRect->Height * args.rel_height;
4996 if (scaled_rect.Width >= 0.5)
4998 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4999 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5002 get_font_hfont(graphics, font, stringFormat, &gdifont);
5003 oldfont = SelectObject(hdc, gdifont);
5005 for (i=0; i<stringFormat->range_count; i++)
5007 stat = GdipSetEmpty(regions[i]);
5012 args.regions = regions;
5014 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5015 measure_ranges_callback, &args);
5017 SelectObject(hdc, oldfont);
5018 DeleteObject(gdifont);
5026 struct measure_string_args {
5028 INT *codepointsfitted;
5030 REAL rel_width, rel_height;
5033 static GpStatus measure_string_callback(HDC hdc,
5034 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5035 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5036 INT lineno, const RectF *bounds, INT *underlined_indexes,
5037 INT underlined_index_count, void *user_data)
5039 struct measure_string_args *args = user_data;
5040 REAL new_width, new_height;
5042 new_width = bounds->Width / args->rel_width;
5043 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5045 if (new_width > args->bounds->Width)
5046 args->bounds->Width = new_width;
5048 if (new_height > args->bounds->Height)
5049 args->bounds->Height = new_height;
5051 if (args->codepointsfitted)
5052 *args->codepointsfitted = index + length;
5054 if (args->linesfilled)
5055 (*args->linesfilled)++;
5060 /* Find the smallest rectangle that bounds the text when it is printed in rect
5061 * according to the format options listed in format. If rect has 0 width and
5062 * height, then just find the smallest rectangle that bounds the text when it's
5063 * printed at location (rect->X, rect-Y). */
5064 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5065 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5066 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5067 INT *codepointsfitted, INT *linesfilled)
5069 HFONT oldfont, gdifont;
5070 struct measure_string_args args;
5071 HDC temp_hdc=NULL, hdc;
5075 INT lines, glyphs, format_flags = format ? format->attr : 0;
5077 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5078 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5079 bounds, codepointsfitted, linesfilled);
5081 if(!graphics || !string || !font || !rect || !bounds)
5082 return InvalidParameter;
5086 hdc = temp_hdc = CreateCompatibleDC(0);
5087 if (!temp_hdc) return OutOfMemory;
5090 hdc = graphics->hdc;
5092 if(linesfilled) *linesfilled = 0;
5093 if(codepointsfitted) *codepointsfitted = 0;
5096 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5104 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5105 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5106 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5107 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5108 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5110 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5111 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5113 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5114 scaled_rect.Y = rect->Y * args.rel_height;
5115 scaled_rect.Width = rect->Width * args.rel_width;
5116 scaled_rect.Height = rect->Height * args.rel_height;
5118 if ((format_flags & StringFormatFlagsNoClip) ||
5119 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5120 if ((format_flags & StringFormatFlagsNoClip) ||
5121 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5123 if (scaled_rect.Width >= 0.5)
5125 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5126 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5129 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5130 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5132 get_font_hfont(graphics, font, format, &gdifont);
5133 oldfont = SelectObject(hdc, gdifont);
5135 bounds->X = rect->X;
5136 bounds->Y = rect->Y;
5137 bounds->Width = 0.0;
5138 bounds->Height = 0.0;
5140 args.bounds = bounds;
5141 args.codepointsfitted = &glyphs;
5142 args.linesfilled = &lines;
5145 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5146 measure_string_callback, &args);
5148 if (linesfilled) *linesfilled = lines;
5149 if (codepointsfitted) *codepointsfitted = glyphs;
5152 bounds->Width += margin_x * 2.0;
5154 SelectObject(hdc, oldfont);
5155 DeleteObject(gdifont);
5163 struct draw_string_args {
5164 GpGraphics *graphics;
5165 GDIPCONST GpBrush *brush;
5166 REAL x, y, rel_width, rel_height, ascent;
5169 static GpStatus draw_string_callback(HDC hdc,
5170 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5171 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5172 INT lineno, const RectF *bounds, INT *underlined_indexes,
5173 INT underlined_index_count, void *user_data)
5175 struct draw_string_args *args = user_data;
5179 position.X = args->x + bounds->X / args->rel_width;
5180 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5182 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5183 args->brush, &position,
5184 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5186 if (stat == Ok && underlined_index_count)
5188 OUTLINETEXTMETRICW otm;
5189 REAL underline_y, underline_height;
5192 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5194 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5195 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5197 for (i=0; i<underlined_index_count; i++)
5199 REAL start_x, end_x;
5201 INT ofs = underlined_indexes[i] - index;
5203 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5204 start_x = text_size.cx / args->rel_width;
5206 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5207 end_x = text_size.cx / args->rel_width;
5209 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5216 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5217 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5218 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5222 GpPointF pt[3], rectcpy[4];
5224 REAL rel_width, rel_height, margin_x;
5225 INT save_state, format_flags = 0;
5227 struct draw_string_args args;
5229 HDC hdc, temp_hdc=NULL;
5230 TEXTMETRICW textmetric;
5232 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5233 length, font, debugstr_rectf(rect), format, brush);
5235 if(!graphics || !string || !font || !brush || !rect)
5236 return InvalidParameter;
5240 hdc = graphics->hdc;
5244 hdc = temp_hdc = CreateCompatibleDC(0);
5248 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5250 format_flags = format->attr;
5252 /* Should be no need to explicitly test for StringAlignmentNear as
5253 * that is default behavior if no alignment is passed. */
5254 if(format->vertalign != StringAlignmentNear){
5255 RectF bounds, in_rect = *rect;
5256 in_rect.Height = 0.0; /* avoid height clipping */
5257 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5259 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5261 if(format->vertalign == StringAlignmentCenter)
5262 offsety = (rect->Height - bounds.Height) / 2;
5263 else if(format->vertalign == StringAlignmentFar)
5264 offsety = (rect->Height - bounds.Height);
5266 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5269 save_state = SaveDC(hdc);
5277 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5278 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5279 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5280 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5281 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5283 rectcpy[3].X = rectcpy[0].X = rect->X;
5284 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5285 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5286 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5287 transform_and_round_points(graphics, corners, rectcpy, 4);
5289 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5290 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5292 scaled_rect.X = margin_x * rel_width;
5293 scaled_rect.Y = 0.0;
5294 scaled_rect.Width = rel_width * rect->Width;
5295 scaled_rect.Height = rel_height * rect->Height;
5297 if ((format_flags & StringFormatFlagsNoClip) ||
5298 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5299 if ((format_flags & StringFormatFlagsNoClip) ||
5300 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5302 if (scaled_rect.Width >= 0.5)
5304 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5305 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5308 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5309 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5311 if (!(format_flags & StringFormatFlagsNoClip) &&
5312 gdip_round(scaled_rect.Width) != 0 && gdip_round(scaled_rect.Height) != 0)
5314 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5315 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5316 SelectClipRgn(hdc, rgn);
5319 get_font_hfont(graphics, font, format, &gdifont);
5320 SelectObject(hdc, gdifont);
5322 args.graphics = graphics;
5326 args.y = rect->Y + offsety;
5328 args.rel_width = rel_width;
5329 args.rel_height = rel_height;
5331 GetTextMetricsW(hdc, &textmetric);
5332 args.ascent = textmetric.tmAscent / rel_height;
5334 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5335 draw_string_callback, &args);
5338 DeleteObject(gdifont);
5340 RestoreDC(hdc, save_state);
5347 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5349 TRACE("(%p)\n", graphics);
5352 return InvalidParameter;
5357 return GdipSetInfinite(graphics->clip);
5360 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5362 TRACE("(%p)\n", graphics);
5365 return InvalidParameter;
5370 graphics->worldtrans->matrix[0] = 1.0;
5371 graphics->worldtrans->matrix[1] = 0.0;
5372 graphics->worldtrans->matrix[2] = 0.0;
5373 graphics->worldtrans->matrix[3] = 1.0;
5374 graphics->worldtrans->matrix[4] = 0.0;
5375 graphics->worldtrans->matrix[5] = 0.0;
5380 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5382 return GdipEndContainer(graphics, state);
5385 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5386 GpMatrixOrder order)
5388 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5391 return InvalidParameter;
5396 return GdipRotateMatrix(graphics->worldtrans, angle, order);
5399 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5401 return GdipBeginContainer2(graphics, state);
5404 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5405 GraphicsContainer *state)
5407 GraphicsContainerItem *container;
5410 TRACE("(%p, %p)\n", graphics, state);
5412 if(!graphics || !state)
5413 return InvalidParameter;
5415 sts = init_container(&container, graphics);
5419 list_add_head(&graphics->containers, &container->entry);
5420 *state = graphics->contid = container->contid;
5425 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5427 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5428 return NotImplemented;
5431 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5433 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5434 return NotImplemented;
5437 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5439 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5440 return NotImplemented;
5443 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5446 GraphicsContainerItem *container, *container2;
5448 TRACE("(%p, %x)\n", graphics, state);
5451 return InvalidParameter;
5453 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5454 if(container->contid == state)
5458 /* did not find a matching container */
5459 if(&container->entry == &graphics->containers)
5462 sts = restore_container(graphics, container);
5466 /* remove all of the containers on top of the found container */
5467 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5468 if(container->contid == state)
5470 list_remove(&container->entry);
5471 delete_container(container);
5474 list_remove(&container->entry);
5475 delete_container(container);
5480 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5481 REAL sy, GpMatrixOrder order)
5483 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5486 return InvalidParameter;
5491 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
5494 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5497 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5499 if(!graphics || !srcgraphics)
5500 return InvalidParameter;
5502 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5505 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5506 CompositingMode mode)
5508 TRACE("(%p, %d)\n", graphics, mode);
5511 return InvalidParameter;
5516 graphics->compmode = mode;
5521 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5522 CompositingQuality quality)
5524 TRACE("(%p, %d)\n", graphics, quality);
5527 return InvalidParameter;
5532 graphics->compqual = quality;
5537 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5538 InterpolationMode mode)
5540 TRACE("(%p, %d)\n", graphics, mode);
5542 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5543 return InvalidParameter;
5548 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5549 mode = InterpolationModeBilinear;
5551 if (mode == InterpolationModeHighQuality)
5552 mode = InterpolationModeHighQualityBicubic;
5554 graphics->interpolation = mode;
5559 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5561 TRACE("(%p, %.2f)\n", graphics, scale);
5563 if(!graphics || (scale <= 0.0))
5564 return InvalidParameter;
5569 graphics->scale = scale;
5574 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5576 TRACE("(%p, %d)\n", graphics, unit);
5579 return InvalidParameter;
5584 if(unit == UnitWorld)
5585 return InvalidParameter;
5587 graphics->unit = unit;
5592 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5595 TRACE("(%p, %d)\n", graphics, mode);
5598 return InvalidParameter;
5603 graphics->pixeloffset = mode;
5608 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5612 TRACE("(%p,%i,%i)\n", graphics, x, y);
5615 FIXME("value is unused in rendering\n");
5618 return InvalidParameter;
5620 graphics->origin_x = x;
5621 graphics->origin_y = y;
5626 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5628 TRACE("(%p,%p,%p)\n", graphics, x, y);
5630 if (!graphics || !x || !y)
5631 return InvalidParameter;
5633 *x = graphics->origin_x;
5634 *y = graphics->origin_y;
5639 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5641 TRACE("(%p, %d)\n", graphics, mode);
5644 return InvalidParameter;
5649 graphics->smoothing = mode;
5654 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5656 TRACE("(%p, %d)\n", graphics, contrast);
5659 return InvalidParameter;
5661 graphics->textcontrast = contrast;
5666 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5667 TextRenderingHint hint)
5669 TRACE("(%p, %d)\n", graphics, hint);
5671 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5672 return InvalidParameter;
5677 graphics->texthint = hint;
5682 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5684 TRACE("(%p, %p)\n", graphics, matrix);
5686 if(!graphics || !matrix)
5687 return InvalidParameter;
5692 TRACE("%f,%f,%f,%f,%f,%f\n",
5693 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5694 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5696 GdipDeleteMatrix(graphics->worldtrans);
5697 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5700 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5701 REAL dy, GpMatrixOrder order)
5703 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5706 return InvalidParameter;
5711 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5714 /*****************************************************************************
5715 * GdipSetClipHrgn [GDIPLUS.@]
5717 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5722 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5725 return InvalidParameter;
5727 status = GdipCreateRegionHrgn(hrgn, ®ion);
5731 status = GdipSetClipRegion(graphics, region, mode);
5733 GdipDeleteRegion(region);
5737 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5739 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5742 return InvalidParameter;
5747 return GdipCombineRegionPath(graphics->clip, path, mode);
5750 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5751 REAL width, REAL height,
5756 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5759 return InvalidParameter;
5767 rect.Height = height;
5769 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5772 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5773 INT width, INT height,
5776 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5779 return InvalidParameter;
5784 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5787 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5790 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5792 if(!graphics || !region)
5793 return InvalidParameter;
5798 return GdipCombineRegionRegion(graphics->clip, region, mode);
5801 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5806 TRACE("(%p,%u)\n", metafile, limitDpi);
5809 FIXME("not implemented\n");
5811 return NotImplemented;
5814 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5820 TRACE("(%p, %p, %d)\n", graphics, points, count);
5822 if(!graphics || !pen || count<=0)
5823 return InvalidParameter;
5830 FIXME("graphics object has no HDC\n");
5834 pti = GdipAlloc(sizeof(POINT) * count);
5836 save_state = prepare_dc(graphics, pen);
5837 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5839 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5840 Polygon(graphics->hdc, pti, count);
5842 restore_dc(graphics, save_state);
5848 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5855 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5857 if(count<=0) return InvalidParameter;
5858 ptf = GdipAlloc(sizeof(GpPointF) * count);
5860 for(i = 0;i < count; i++){
5861 ptf[i].X = (REAL)points[i].X;
5862 ptf[i].Y = (REAL)points[i].Y;
5865 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5871 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5873 TRACE("(%p, %p)\n", graphics, dpi);
5875 if(!graphics || !dpi)
5876 return InvalidParameter;
5881 *dpi = graphics->xres;
5885 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5887 TRACE("(%p, %p)\n", graphics, dpi);
5889 if(!graphics || !dpi)
5890 return InvalidParameter;
5895 *dpi = graphics->yres;
5899 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5900 GpMatrixOrder order)
5905 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5907 if(!graphics || !matrix)
5908 return InvalidParameter;
5913 m = *(graphics->worldtrans);
5915 ret = GdipMultiplyMatrix(&m, matrix, order);
5917 *(graphics->worldtrans) = m;
5922 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5923 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5925 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5929 TRACE("(%p, %p)\n", graphics, hdc);
5931 if(!graphics || !hdc)
5932 return InvalidParameter;
5937 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5939 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5941 else if (!graphics->hdc ||
5942 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5944 /* Create a fake HDC and fill it with a constant color. */
5948 BITMAPINFOHEADER bmih;
5951 stat = get_graphics_bounds(graphics, &bounds);
5955 graphics->temp_hbitmap_width = bounds.Width;
5956 graphics->temp_hbitmap_height = bounds.Height;
5958 bmih.biSize = sizeof(bmih);
5959 bmih.biWidth = graphics->temp_hbitmap_width;
5960 bmih.biHeight = -graphics->temp_hbitmap_height;
5962 bmih.biBitCount = 32;
5963 bmih.biCompression = BI_RGB;
5964 bmih.biSizeImage = 0;
5965 bmih.biXPelsPerMeter = 0;
5966 bmih.biYPelsPerMeter = 0;
5968 bmih.biClrImportant = 0;
5970 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5971 (void**)&graphics->temp_bits, NULL, 0);
5973 return GenericError;
5975 temp_hdc = CreateCompatibleDC(0);
5978 DeleteObject(hbitmap);
5979 return GenericError;
5982 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5983 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5985 SelectObject(temp_hdc, hbitmap);
5987 graphics->temp_hbitmap = hbitmap;
5988 *hdc = graphics->temp_hdc = temp_hdc;
5992 *hdc = graphics->hdc;
5996 graphics->busy = TRUE;
6001 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6005 TRACE("(%p, %p)\n", graphics, hdc);
6007 if(!graphics || !hdc || !graphics->busy)
6008 return InvalidParameter;
6010 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6012 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6014 else if (graphics->temp_hdc == hdc)
6019 /* Find the pixels that have changed, and mark them as opaque. */
6020 pos = (DWORD*)graphics->temp_bits;
6021 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6023 if (*pos != DC_BACKGROUND_KEY)
6030 /* Write the changed pixels to the real target. */
6031 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6032 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6033 graphics->temp_hbitmap_width * 4);
6036 DeleteDC(graphics->temp_hdc);
6037 DeleteObject(graphics->temp_hbitmap);
6038 graphics->temp_hdc = NULL;
6039 graphics->temp_hbitmap = NULL;
6041 else if (hdc != graphics->hdc)
6043 stat = InvalidParameter;
6047 graphics->busy = FALSE;
6052 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6057 TRACE("(%p, %p)\n", graphics, region);
6059 if(!graphics || !region)
6060 return InvalidParameter;
6065 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6068 /* free everything except root node and header */
6069 delete_element(®ion->node);
6070 memcpy(region, clip, sizeof(GpRegion));
6076 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6077 GpCoordinateSpace src_space, GpMatrix **matrix)
6079 GpStatus stat = GdipCreateMatrix(matrix);
6080 REAL scale_x, scale_y;
6082 if (dst_space != src_space && stat == Ok)
6084 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6085 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6087 if(graphics->unit != UnitDisplay)
6089 scale_x *= graphics->scale;
6090 scale_y *= graphics->scale;
6093 /* transform from src_space to CoordinateSpacePage */
6096 case CoordinateSpaceWorld:
6097 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
6099 case CoordinateSpacePage:
6101 case CoordinateSpaceDevice:
6102 GdipScaleMatrix(*matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6106 /* transform from CoordinateSpacePage to dst_space */
6109 case CoordinateSpaceWorld:
6111 GpMatrix *inverted_transform;
6112 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
6115 stat = GdipInvertMatrix(inverted_transform);
6117 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
6118 GdipDeleteMatrix(inverted_transform);
6122 case CoordinateSpacePage:
6124 case CoordinateSpaceDevice:
6125 GdipScaleMatrix(*matrix, scale_x, scale_y, MatrixOrderAppend);
6132 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6133 GpCoordinateSpace src_space, GpPointF *points, INT count)
6138 if(!graphics || !points || count <= 0)
6139 return InvalidParameter;
6144 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6146 if (src_space == dst_space) return Ok;
6148 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6152 stat = GdipTransformMatrixPoints(matrix, points, count);
6154 GdipDeleteMatrix(matrix);
6160 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6161 GpCoordinateSpace src_space, GpPoint *points, INT count)
6167 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6170 return InvalidParameter;
6172 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6176 for(i = 0; i < count; i++){
6177 pointsF[i].X = (REAL)points[i].X;
6178 pointsF[i].Y = (REAL)points[i].Y;
6181 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6184 for(i = 0; i < count; i++){
6185 points[i].X = gdip_round(pointsF[i].X);
6186 points[i].Y = gdip_round(pointsF[i].Y);
6193 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6205 /*****************************************************************************
6206 * GdipTranslateClip [GDIPLUS.@]
6208 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6210 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6213 return InvalidParameter;
6218 return GdipTranslateRegion(graphics->clip, dx, dy);
6221 /*****************************************************************************
6222 * GdipTranslateClipI [GDIPLUS.@]
6224 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6226 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6229 return InvalidParameter;
6234 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6238 /*****************************************************************************
6239 * GdipMeasureDriverString [GDIPLUS.@]
6241 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6242 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6243 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6245 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6248 REAL min_x, min_y, max_x, max_y, x, y;
6250 TEXTMETRICW textmetric;
6251 const WORD *glyph_indices;
6252 WORD *dynamic_glyph_indices=NULL;
6253 REAL rel_width, rel_height, ascent, descent;
6256 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6258 if (!graphics || !text || !font || !positions || !boundingBox)
6259 return InvalidParameter;
6262 length = strlenW(text);
6266 boundingBox->X = 0.0;
6267 boundingBox->Y = 0.0;
6268 boundingBox->Width = 0.0;
6269 boundingBox->Height = 0.0;
6272 if (flags & unsupported_flags)
6273 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6276 FIXME("Ignoring matrix\n");
6278 get_font_hfont(graphics, font, NULL, &hfont);
6280 hdc = CreateCompatibleDC(0);
6281 SelectObject(hdc, hfont);
6283 GetTextMetricsW(hdc, &textmetric);
6291 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6292 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6293 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6294 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6295 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6297 if (flags & DriverStringOptionsCmapLookup)
6299 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6303 DeleteObject(hfont);
6307 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6310 glyph_indices = text;
6312 min_x = max_x = x = positions[0].X;
6313 min_y = max_y = y = positions[0].Y;
6315 ascent = textmetric.tmAscent / rel_height;
6316 descent = textmetric.tmDescent / rel_height;
6318 for (i=0; i<length; i++)
6323 if (!(flags & DriverStringOptionsRealizedAdvance))
6329 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6330 char_width = abc.abcA + abc.abcB + abc.abcB;
6332 if (min_y > y - ascent) min_y = y - ascent;
6333 if (max_y < y + descent) max_y = y + descent;
6334 if (min_x > x) min_x = x;
6336 x += char_width / rel_width;
6338 if (max_x < x) max_x = x;
6341 GdipFree(dynamic_glyph_indices);
6343 DeleteObject(hfont);
6345 boundingBox->X = min_x;
6346 boundingBox->Y = min_y;
6347 boundingBox->Width = max_x - min_x;
6348 boundingBox->Height = max_y - min_y;
6353 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6354 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6355 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6356 INT flags, GDIPCONST GpMatrix *matrix)
6358 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6364 if (flags & unsupported_flags)
6365 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6368 FIXME("Ignoring matrix\n");
6370 if (!(flags & DriverStringOptionsCmapLookup))
6371 eto_flags |= ETO_GLYPH_INDEX;
6373 save_state = SaveDC(graphics->hdc);
6374 SetBkMode(graphics->hdc, TRANSPARENT);
6375 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6378 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6380 get_font_hfont(graphics, font, format, &hfont);
6381 SelectObject(graphics->hdc, hfont);
6383 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6385 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6387 RestoreDC(graphics->hdc, save_state);
6389 DeleteObject(hfont);
6394 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6395 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6396 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6397 INT flags, GDIPCONST GpMatrix *matrix)
6399 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6401 PointF *real_positions, real_position;
6405 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6406 DWORD max_glyphsize=0;
6407 GLYPHMETRICS glyphmetrics;
6408 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6411 int text_mask_stride;
6413 int pixel_data_stride;
6415 UINT ggo_flags = GGO_GRAY8_BITMAP;
6420 if (!(flags & DriverStringOptionsCmapLookup))
6421 ggo_flags |= GGO_GLYPH_INDEX;
6423 if (flags & unsupported_flags)
6424 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6427 FIXME("Ignoring matrix\n");
6429 pti = GdipAlloc(sizeof(POINT) * length);
6433 if (flags & DriverStringOptionsRealizedAdvance)
6435 real_position = positions[0];
6437 transform_and_round_points(graphics, pti, &real_position, 1);
6441 real_positions = GdipAlloc(sizeof(PointF) * length);
6442 if (!real_positions)
6448 memcpy(real_positions, positions, sizeof(PointF) * length);
6450 transform_and_round_points(graphics, pti, real_positions, length);
6452 GdipFree(real_positions);
6455 get_font_hfont(graphics, font, format, &hfont);
6457 hdc = CreateCompatibleDC(0);
6458 SelectObject(hdc, hfont);
6460 /* Get the boundaries of the text to be drawn */
6461 for (i=0; i<length; i++)
6464 int left, top, right, bottom;
6466 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6467 &glyphmetrics, 0, NULL, &identity);
6469 if (glyphsize == GDI_ERROR)
6471 ERR("GetGlyphOutlineW failed\n");
6474 DeleteObject(hfont);
6475 return GenericError;
6478 if (glyphsize > max_glyphsize)
6479 max_glyphsize = glyphsize;
6481 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6482 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6483 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6484 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6486 if (left < min_x) min_x = left;
6487 if (top < min_y) min_y = top;
6488 if (right > max_x) max_x = right;
6489 if (bottom > max_y) max_y = bottom;
6491 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6493 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6494 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6498 glyph_mask = GdipAlloc(max_glyphsize);
6499 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6500 text_mask_stride = max_x - min_x;
6502 if (!(glyph_mask && text_mask))
6504 GdipFree(glyph_mask);
6505 GdipFree(text_mask);
6508 DeleteObject(hfont);
6512 /* Generate a mask for the text */
6513 for (i=0; i<length; i++)
6515 int left, top, stride;
6517 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6518 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6520 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6521 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6522 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6524 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6526 BYTE *glyph_val = glyph_mask + y * stride;
6527 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6528 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6530 *text_val = min(64, *text_val + *glyph_val);
6539 DeleteObject(hfont);
6540 GdipFree(glyph_mask);
6542 /* get the brush data */
6543 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6546 GdipFree(text_mask);
6550 pixel_area.X = min_x;
6551 pixel_area.Y = min_y;
6552 pixel_area.Width = max_x - min_x;
6553 pixel_area.Height = max_y - min_y;
6554 pixel_data_stride = pixel_area.Width * 4;
6556 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6559 GdipFree(text_mask);
6560 GdipFree(pixel_data);
6564 /* multiply the brush data by the mask */
6565 for (y=0; y<pixel_area.Height; y++)
6567 BYTE *text_val = text_mask + text_mask_stride * y;
6568 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6569 for (x=0; x<pixel_area.Width; x++)
6571 *pixel_val = (*pixel_val) * (*text_val) / 64;
6577 GdipFree(text_mask);
6579 /* draw the result */
6580 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6581 pixel_area.Height, pixel_data_stride);
6583 GdipFree(pixel_data);
6588 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6589 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6590 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6591 INT flags, GDIPCONST GpMatrix *matrix)
6593 GpStatus stat = NotImplemented;
6596 length = strlenW(text);
6598 if (graphics->hdc &&
6599 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6600 brush->bt == BrushTypeSolidColor &&
6601 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6602 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6603 brush, positions, flags, matrix);
6604 if (stat == NotImplemented)
6605 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6606 brush, positions, flags, matrix);
6610 /*****************************************************************************
6611 * GdipDrawDriverString [GDIPLUS.@]
6613 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6614 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6615 GDIPCONST PointF *positions, INT flags,
6616 GDIPCONST GpMatrix *matrix )
6618 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6620 if (!graphics || !text || !font || !brush || !positions)
6621 return InvalidParameter;
6623 return draw_driver_string(graphics, text, length, font, NULL,
6624 brush, positions, flags, matrix);
6627 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6628 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6630 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6631 return NotImplemented;
6634 /*****************************************************************************
6635 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6637 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6642 TRACE("(%p, %p)\n", graphics, res);
6644 if((stat = GdipCreateRegion(&rgn)) != Ok)
6647 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6650 stat = GdipIsEmptyRegion(rgn, graphics, res);
6653 GdipDeleteRegion(rgn);
6657 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6661 TRACE("(%p) stub\n", graphics);
6664 FIXME("not implemented\n");
6666 return NotImplemented;