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, PixelOffsetMode offset_mode)
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:
889 case PixelOffsetModeNone:
890 case PixelOffsetModeHighSpeed:
894 case PixelOffsetModeHalf:
895 case PixelOffsetModeHighQuality:
899 return sample_bitmap_pixel(src_rect, bits, width, height,
900 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
906 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
908 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
911 static INT brush_can_fill_path(GpBrush *brush)
915 case BrushTypeSolidColor:
917 case BrushTypeHatchFill:
919 GpHatch *hatch = (GpHatch*)brush;
920 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
921 ((hatch->backcol & 0xff000000) == 0xff000000);
923 case BrushTypeLinearGradient:
924 case BrushTypeTextureFill:
925 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
931 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
935 case BrushTypeSolidColor:
937 GpSolidFill *fill = (GpSolidFill*)brush;
938 HBITMAP bmp = ARGB2BMP(fill->color);
943 /* partially transparent fill */
945 SelectClipPath(graphics->hdc, RGN_AND);
946 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
948 HDC hdc = CreateCompatibleDC(NULL);
952 SelectObject(hdc, bmp);
953 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
961 /* else fall through */
965 HBRUSH gdibrush, old_brush;
967 gdibrush = create_gdi_brush(brush);
968 if (!gdibrush) return;
970 old_brush = SelectObject(graphics->hdc, gdibrush);
971 FillPath(graphics->hdc);
972 SelectObject(graphics->hdc, old_brush);
973 DeleteObject(gdibrush);
979 static INT brush_can_fill_pixels(GpBrush *brush)
983 case BrushTypeSolidColor:
984 case BrushTypeHatchFill:
985 case BrushTypeLinearGradient:
986 case BrushTypeTextureFill:
987 case BrushTypePathGradient:
994 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
995 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
999 case BrushTypeSolidColor:
1002 GpSolidFill *fill = (GpSolidFill*)brush;
1003 for (x=0; x<fill_area->Width; x++)
1004 for (y=0; y<fill_area->Height; y++)
1005 argb_pixels[x + y*cdwStride] = fill->color;
1008 case BrushTypeHatchFill:
1011 GpHatch *fill = (GpHatch*)brush;
1012 const char *hatch_data;
1014 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1015 return NotImplemented;
1017 for (x=0; x<fill_area->Width; x++)
1018 for (y=0; y<fill_area->Height; y++)
1022 /* FIXME: Account for the rendering origin */
1023 hx = (x + fill_area->X) % 8;
1024 hy = (y + fill_area->Y) % 8;
1026 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1027 argb_pixels[x + y*cdwStride] = fill->forecol;
1029 argb_pixels[x + y*cdwStride] = fill->backcol;
1034 case BrushTypeLinearGradient:
1036 GpLineGradient *fill = (GpLineGradient*)brush;
1037 GpPointF draw_points[3], line_points[3];
1039 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1040 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1043 draw_points[0].X = fill_area->X;
1044 draw_points[0].Y = fill_area->Y;
1045 draw_points[1].X = fill_area->X+1;
1046 draw_points[1].Y = fill_area->Y;
1047 draw_points[2].X = fill_area->X;
1048 draw_points[2].Y = fill_area->Y+1;
1050 /* Transform the points to a co-ordinate space where X is the point's
1051 * position in the gradient, 0.0 being the start point and 1.0 the
1053 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1054 CoordinateSpaceDevice, draw_points, 3);
1058 line_points[0] = fill->startpoint;
1059 line_points[1] = fill->endpoint;
1060 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1061 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1063 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1068 stat = GdipInvertMatrix(world_to_gradient);
1071 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1073 GdipDeleteMatrix(world_to_gradient);
1078 REAL x_delta = draw_points[1].X - draw_points[0].X;
1079 REAL y_delta = draw_points[2].X - draw_points[0].X;
1081 for (y=0; y<fill_area->Height; y++)
1083 for (x=0; x<fill_area->Width; x++)
1085 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1087 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1094 case BrushTypeTextureFill:
1096 GpTexture *fill = (GpTexture*)brush;
1097 GpPointF draw_points[3];
1099 GpMatrix *world_to_texture;
1105 if (fill->image->type != ImageTypeBitmap)
1107 FIXME("metafile texture brushes not implemented\n");
1108 return NotImplemented;
1111 bitmap = (GpBitmap*)fill->image;
1112 src_stride = sizeof(ARGB) * bitmap->width;
1114 src_area.X = src_area.Y = 0;
1115 src_area.Width = bitmap->width;
1116 src_area.Height = bitmap->height;
1118 draw_points[0].X = fill_area->X;
1119 draw_points[0].Y = fill_area->Y;
1120 draw_points[1].X = fill_area->X+1;
1121 draw_points[1].Y = fill_area->Y;
1122 draw_points[2].X = fill_area->X;
1123 draw_points[2].Y = fill_area->Y+1;
1125 /* Transform the points to the co-ordinate space of the bitmap. */
1126 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1127 CoordinateSpaceDevice, draw_points, 3);
1131 stat = GdipCloneMatrix(fill->transform, &world_to_texture);
1136 stat = GdipInvertMatrix(world_to_texture);
1139 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
1141 GdipDeleteMatrix(world_to_texture);
1144 if (stat == Ok && !fill->bitmap_bits)
1146 BitmapData lockeddata;
1148 fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
1149 if (!fill->bitmap_bits)
1154 lockeddata.Width = bitmap->width;
1155 lockeddata.Height = bitmap->height;
1156 lockeddata.Stride = src_stride;
1157 lockeddata.PixelFormat = PixelFormat32bppARGB;
1158 lockeddata.Scan0 = fill->bitmap_bits;
1160 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1161 PixelFormat32bppARGB, &lockeddata);
1165 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1168 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1169 bitmap->width, bitmap->height,
1170 src_stride, ColorAdjustTypeBitmap);
1174 GdipFree(fill->bitmap_bits);
1175 fill->bitmap_bits = NULL;
1181 REAL x_dx = draw_points[1].X - draw_points[0].X;
1182 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1183 REAL y_dx = draw_points[2].X - draw_points[0].X;
1184 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1186 for (y=0; y<fill_area->Height; y++)
1188 for (x=0; x<fill_area->Width; x++)
1191 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1192 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1194 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1195 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1196 &point, fill->imageattributes, graphics->interpolation,
1197 graphics->pixeloffset);
1204 case BrushTypePathGradient:
1206 GpPathGradient *fill = (GpPathGradient*)brush;
1208 GpMatrix *world_to_device;
1210 int i, figure_start=0;
1211 GpPointF start_point, end_point, center_point;
1213 REAL min_yf, max_yf, line1_xf, line2_xf;
1214 INT min_y, max_y, min_x, max_x;
1217 static int transform_fixme_once;
1219 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1223 FIXME("path gradient focus not implemented\n");
1230 FIXME("path gradient gamma correction not implemented\n");
1233 if (fill->blendcount)
1237 FIXME("path gradient blend not implemented\n");
1240 if (fill->pblendcount)
1244 FIXME("path gradient preset blend not implemented\n");
1247 if (!transform_fixme_once)
1249 BOOL is_identity=TRUE;
1250 GdipIsMatrixIdentity(fill->transform, &is_identity);
1253 FIXME("path gradient transform not implemented\n");
1254 transform_fixme_once = 1;
1258 stat = GdipClonePath(fill->path, &flat_path);
1263 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1264 CoordinateSpaceWorld, &world_to_device);
1267 stat = GdipTransformPath(flat_path, world_to_device);
1271 center_point = fill->center;
1272 stat = GdipTransformMatrixPoints(world_to_device, ¢er_point, 1);
1276 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1278 GdipDeleteMatrix(world_to_device);
1283 GdipDeletePath(flat_path);
1287 for (i=0; i<flat_path->pathdata.Count; i++)
1289 int start_center_line=0, end_center_line=0;
1290 int seen_start=0, seen_end=0, seen_center=0;
1291 REAL center_distance;
1292 ARGB start_color, end_color;
1295 type = flat_path->pathdata.Types[i];
1297 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1300 start_point = flat_path->pathdata.Points[i];
1302 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1304 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1306 end_point = flat_path->pathdata.Points[figure_start];
1307 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1309 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1311 end_point = flat_path->pathdata.Points[i+1];
1312 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1317 outer_color = start_color;
1319 min_yf = center_point.Y;
1320 if (min_yf > start_point.Y) min_yf = start_point.Y;
1321 if (min_yf > end_point.Y) min_yf = end_point.Y;
1323 if (min_yf < fill_area->Y)
1324 min_y = fill_area->Y;
1326 min_y = (INT)ceil(min_yf);
1328 max_yf = center_point.Y;
1329 if (max_yf < start_point.Y) max_yf = start_point.Y;
1330 if (max_yf < end_point.Y) max_yf = end_point.Y;
1332 if (max_yf > fill_area->Y + fill_area->Height)
1333 max_y = fill_area->Y + fill_area->Height;
1335 max_y = (INT)ceil(max_yf);
1337 dy = end_point.Y - start_point.Y;
1338 dx = end_point.X - start_point.X;
1340 /* This is proportional to the distance from start-end line to center point. */
1341 center_distance = dy * (start_point.X - center_point.X) +
1342 dx * (center_point.Y - start_point.Y);
1344 for (y=min_y; y<max_y; y++)
1348 if (!seen_start && yf >= start_point.Y)
1351 start_center_line ^= 1;
1353 if (!seen_end && yf >= end_point.Y)
1356 end_center_line ^= 1;
1358 if (!seen_center && yf >= center_point.Y)
1361 start_center_line ^= 1;
1362 end_center_line ^= 1;
1365 if (start_center_line)
1366 line1_xf = intersect_line_scanline(&start_point, ¢er_point, yf);
1368 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1370 if (end_center_line)
1371 line2_xf = intersect_line_scanline(&end_point, ¢er_point, yf);
1373 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1375 if (line1_xf < line2_xf)
1377 min_x = (INT)ceil(line1_xf);
1378 max_x = (INT)ceil(line2_xf);
1382 min_x = (INT)ceil(line2_xf);
1383 max_x = (INT)ceil(line1_xf);
1386 if (min_x < fill_area->X)
1387 min_x = fill_area->X;
1388 if (max_x > fill_area->X + fill_area->Width)
1389 max_x = fill_area->X + fill_area->Width;
1391 for (x=min_x; x<max_x; x++)
1396 if (start_color != end_color)
1398 REAL blend_amount, pdy, pdx;
1399 pdy = yf - center_point.Y;
1400 pdx = xf - center_point.X;
1401 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1402 outer_color = blend_colors(start_color, end_color, blend_amount);
1405 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1406 (end_point.X - start_point.X) * (yf - start_point.Y);
1408 distance = distance / center_distance;
1410 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1411 blend_colors(outer_color, fill->centercolor, distance);
1416 GdipDeletePath(flat_path);
1420 return NotImplemented;
1424 /* GdipDrawPie/GdipFillPie helper function */
1425 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
1426 REAL height, REAL startAngle, REAL sweepAngle)
1433 ptf[1].X = x + width;
1434 ptf[1].Y = y + height;
1436 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
1437 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
1439 transform_and_round_points(graphics, pti, ptf, 4);
1441 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
1442 pti[2].y, pti[3].x, pti[3].y);
1445 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1446 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1447 * should not be called on an hdc that has a path you care about. */
1448 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1449 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1451 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1452 GpMatrix *matrix = NULL;
1453 HBRUSH brush = NULL;
1455 PointF ptf[4], *custptf = NULL;
1456 POINT pt[4], *custpt = NULL;
1458 REAL theta, dsmall, dbig, dx, dy = 0.0;
1463 if((x1 == x2) && (y1 == y2))
1466 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1468 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1470 brush = CreateSolidBrush(color);
1471 lb.lbStyle = BS_SOLID;
1474 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1475 PS_JOIN_MITER, 1, &lb, 0,
1477 oldbrush = SelectObject(graphics->hdc, brush);
1478 oldpen = SelectObject(graphics->hdc, pen);
1485 case LineCapSquareAnchor:
1486 case LineCapDiamondAnchor:
1487 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1488 if(cap == LineCapDiamondAnchor){
1489 dsmall = cos(theta + M_PI_2) * size;
1490 dbig = sin(theta + M_PI_2) * size;
1493 dsmall = cos(theta + M_PI_4) * size;
1494 dbig = sin(theta + M_PI_4) * size;
1497 ptf[0].X = x2 - dsmall;
1498 ptf[1].X = x2 + dbig;
1500 ptf[0].Y = y2 - dbig;
1501 ptf[3].Y = y2 + dsmall;
1503 ptf[1].Y = y2 - dsmall;
1504 ptf[2].Y = y2 + dbig;
1506 ptf[3].X = x2 - dbig;
1507 ptf[2].X = x2 + dsmall;
1509 transform_and_round_points(graphics, pt, ptf, 4);
1510 Polygon(graphics->hdc, pt, 4);
1513 case LineCapArrowAnchor:
1514 size = size * 4.0 / sqrt(3.0);
1516 dx = cos(M_PI / 6.0 + theta) * size;
1517 dy = sin(M_PI / 6.0 + theta) * size;
1522 dx = cos(- M_PI / 6.0 + theta) * size;
1523 dy = sin(- M_PI / 6.0 + theta) * size;
1531 transform_and_round_points(graphics, pt, ptf, 3);
1532 Polygon(graphics->hdc, pt, 3);
1535 case LineCapRoundAnchor:
1536 dx = dy = ANCHOR_WIDTH * size / 2.0;
1543 transform_and_round_points(graphics, pt, ptf, 2);
1544 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1547 case LineCapTriangle:
1549 dx = cos(M_PI_2 + theta) * size;
1550 dy = sin(M_PI_2 + theta) * size;
1557 dx = cos(theta) * size;
1558 dy = sin(theta) * size;
1563 transform_and_round_points(graphics, pt, ptf, 3);
1564 Polygon(graphics->hdc, pt, 3);
1568 dx = dy = size / 2.0;
1575 dx = -cos(M_PI_2 + theta) * size;
1576 dy = -sin(M_PI_2 + theta) * size;
1583 transform_and_round_points(graphics, pt, ptf, 4);
1584 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1585 pt[2].y, pt[3].x, pt[3].y);
1592 count = custom->pathdata.Count;
1593 custptf = GdipAlloc(count * sizeof(PointF));
1594 custpt = GdipAlloc(count * sizeof(POINT));
1595 tp = GdipAlloc(count);
1597 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1600 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1602 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1603 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1605 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1606 GdipTransformMatrixPoints(matrix, custptf, count);
1608 transform_and_round_points(graphics, custpt, custptf, count);
1610 for(i = 0; i < count; i++)
1611 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1614 BeginPath(graphics->hdc);
1615 PolyDraw(graphics->hdc, custpt, tp, count);
1616 EndPath(graphics->hdc);
1617 StrokeAndFillPath(graphics->hdc);
1620 PolyDraw(graphics->hdc, custpt, tp, count);
1626 GdipDeleteMatrix(matrix);
1633 SelectObject(graphics->hdc, oldbrush);
1634 SelectObject(graphics->hdc, oldpen);
1635 DeleteObject(brush);
1640 /* Shortens the line by the given percent by changing x2, y2.
1641 * If percent is > 1.0 then the line will change direction.
1642 * If percent is negative it can lengthen the line. */
1643 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1645 REAL dist, theta, dx, dy;
1647 if((y1 == *y2) && (x1 == *x2))
1650 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1651 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1652 dx = cos(theta) * dist;
1653 dy = sin(theta) * dist;
1659 /* Shortens the line by the given amount by changing x2, y2.
1660 * If the amount is greater than the distance, the line will become length 0.
1661 * If the amount is negative, it can lengthen the line. */
1662 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1664 REAL dx, dy, percent;
1668 if(dx == 0 && dy == 0)
1671 percent = amt / sqrt(dx * dx + dy * dy);
1678 shorten_line_percent(x1, y1, x2, y2, percent);
1681 /* Draws lines between the given points, and if caps is true then draws an endcap
1682 * at the end of the last line. */
1683 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1684 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1687 GpPointF *ptcopy = NULL;
1688 GpStatus status = GenericError;
1693 pti = GdipAlloc(count * sizeof(POINT));
1694 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1696 if(!pti || !ptcopy){
1697 status = OutOfMemory;
1701 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1704 if(pen->endcap == LineCapArrowAnchor)
1705 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1706 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1707 else if((pen->endcap == LineCapCustom) && pen->customend)
1708 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1709 &ptcopy[count-1].X, &ptcopy[count-1].Y,
1710 pen->customend->inset * pen->width);
1712 if(pen->startcap == LineCapArrowAnchor)
1713 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1714 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1715 else if((pen->startcap == LineCapCustom) && pen->customstart)
1716 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1717 &ptcopy[0].X, &ptcopy[0].Y,
1718 pen->customstart->inset * pen->width);
1720 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1721 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1722 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1723 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1726 transform_and_round_points(graphics, pti, ptcopy, count);
1728 if(Polyline(graphics->hdc, pti, count))
1738 /* Conducts a linear search to find the bezier points that will back off
1739 * the endpoint of the curve by a distance of amt. Linear search works
1740 * better than binary in this case because there are multiple solutions,
1741 * and binary searches often find a bad one. I don't think this is what
1742 * Windows does but short of rendering the bezier without GDI's help it's
1743 * the best we can do. If rev then work from the start of the passed points
1744 * instead of the end. */
1745 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1748 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1749 INT i, first = 0, second = 1, third = 2, fourth = 3;
1758 origx = pt[fourth].X;
1759 origy = pt[fourth].Y;
1760 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1762 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1763 /* reset bezier points to original values */
1764 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1765 /* Perform magic on bezier points. Order is important here.*/
1766 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1767 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1768 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1769 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1770 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1771 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1773 dx = pt[fourth].X - origx;
1774 dy = pt[fourth].Y - origy;
1776 diff = sqrt(dx * dx + dy * dy);
1777 percent += 0.0005 * amt;
1781 /* Draws bezier curves between given points, and if caps is true then draws an
1782 * endcap at the end of the last line. */
1783 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1784 GDIPCONST GpPointF * pt, INT count, BOOL caps)
1788 GpStatus status = GenericError;
1793 pti = GdipAlloc(count * sizeof(POINT));
1794 ptcopy = GdipAlloc(count * sizeof(GpPointF));
1796 if(!pti || !ptcopy){
1797 status = OutOfMemory;
1801 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1804 if(pen->endcap == LineCapArrowAnchor)
1805 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1806 else if((pen->endcap == LineCapCustom) && pen->customend)
1807 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1810 if(pen->startcap == LineCapArrowAnchor)
1811 shorten_bezier_amt(ptcopy, pen->width, TRUE);
1812 else if((pen->startcap == LineCapCustom) && pen->customstart)
1813 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1815 /* the direction of the line cap is parallel to the direction at the
1816 * end of the bezier (which, if it has been shortened, is not the same
1817 * as the direction from pt[count-2] to pt[count-1]) */
1818 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1819 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1820 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1821 pt[count - 1].X, pt[count - 1].Y);
1823 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1824 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1825 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1828 transform_and_round_points(graphics, pti, ptcopy, count);
1830 PolyBezier(graphics->hdc, pti, count);
1841 /* Draws a combination of bezier curves and lines between points. */
1842 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1843 GDIPCONST BYTE * types, INT count, BOOL caps)
1845 POINT *pti = GdipAlloc(count * sizeof(POINT));
1846 BYTE *tp = GdipAlloc(count);
1847 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1849 GpStatus status = GenericError;
1855 if(!pti || !tp || !ptcopy){
1856 status = OutOfMemory;
1860 for(i = 1; i < count; i++){
1861 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1862 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1863 || !(types[i + 1] & PathPointTypeBezier)){
1864 ERR("Bad bezier points\n");
1871 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1873 /* If we are drawing caps, go through the points and adjust them accordingly,
1874 * and draw the caps. */
1876 switch(types[count - 1] & PathPointTypePathTypeMask){
1877 case PathPointTypeBezier:
1878 if(pen->endcap == LineCapArrowAnchor)
1879 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1880 else if((pen->endcap == LineCapCustom) && pen->customend)
1881 shorten_bezier_amt(&ptcopy[count - 4],
1882 pen->width * pen->customend->inset, FALSE);
1884 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1885 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1886 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1887 pt[count - 1].X, pt[count - 1].Y);
1890 case PathPointTypeLine:
1891 if(pen->endcap == LineCapArrowAnchor)
1892 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1893 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1895 else if((pen->endcap == LineCapCustom) && pen->customend)
1896 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1897 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1898 pen->customend->inset * pen->width);
1900 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1901 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1906 ERR("Bad path last point\n");
1910 /* Find start of points */
1911 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1912 == PathPointTypeStart); j++);
1914 switch(types[j] & PathPointTypePathTypeMask){
1915 case PathPointTypeBezier:
1916 if(pen->startcap == LineCapArrowAnchor)
1917 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1918 else if((pen->startcap == LineCapCustom) && pen->customstart)
1919 shorten_bezier_amt(&ptcopy[j - 1],
1920 pen->width * pen->customstart->inset, TRUE);
1922 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1923 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1924 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1925 pt[j - 1].X, pt[j - 1].Y);
1928 case PathPointTypeLine:
1929 if(pen->startcap == LineCapArrowAnchor)
1930 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1931 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1933 else if((pen->startcap == LineCapCustom) && pen->customstart)
1934 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1935 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1936 pen->customstart->inset * pen->width);
1938 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1939 pt[j].X, pt[j].Y, pt[j - 1].X,
1944 ERR("Bad path points\n");
1949 transform_and_round_points(graphics, pti, ptcopy, count);
1951 for(i = 0; i < count; i++){
1952 tp[i] = convert_path_point_type(types[i]);
1955 PolyDraw(graphics->hdc, pti, tp, count);
1967 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1971 BeginPath(graphics->hdc);
1972 result = draw_poly(graphics, NULL, path->pathdata.Points,
1973 path->pathdata.Types, path->pathdata.Count, FALSE);
1974 EndPath(graphics->hdc);
1978 typedef struct _GraphicsContainerItem {
1980 GraphicsContainer contid;
1982 SmoothingMode smoothing;
1983 CompositingQuality compqual;
1984 InterpolationMode interpolation;
1985 CompositingMode compmode;
1986 TextRenderingHint texthint;
1989 PixelOffsetMode pixeloffset;
1991 GpMatrix* worldtrans;
1993 INT origin_x, origin_y;
1994 } GraphicsContainerItem;
1996 static GpStatus init_container(GraphicsContainerItem** container,
1997 GDIPCONST GpGraphics* graphics){
2000 *container = GdipAlloc(sizeof(GraphicsContainerItem));
2004 (*container)->contid = graphics->contid + 1;
2006 (*container)->smoothing = graphics->smoothing;
2007 (*container)->compqual = graphics->compqual;
2008 (*container)->interpolation = graphics->interpolation;
2009 (*container)->compmode = graphics->compmode;
2010 (*container)->texthint = graphics->texthint;
2011 (*container)->scale = graphics->scale;
2012 (*container)->unit = graphics->unit;
2013 (*container)->textcontrast = graphics->textcontrast;
2014 (*container)->pixeloffset = graphics->pixeloffset;
2015 (*container)->origin_x = graphics->origin_x;
2016 (*container)->origin_y = graphics->origin_y;
2018 sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
2020 GdipFree(*container);
2025 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
2027 GdipDeleteMatrix((*container)->worldtrans);
2028 GdipFree(*container);
2036 static void delete_container(GraphicsContainerItem* container){
2037 GdipDeleteMatrix(container->worldtrans);
2038 GdipDeleteRegion(container->clip);
2039 GdipFree(container);
2042 static GpStatus restore_container(GpGraphics* graphics,
2043 GDIPCONST GraphicsContainerItem* container){
2048 sts = GdipCloneMatrix(container->worldtrans, &newTrans);
2052 sts = GdipCloneRegion(container->clip, &newClip);
2054 GdipDeleteMatrix(newTrans);
2058 GdipDeleteMatrix(graphics->worldtrans);
2059 graphics->worldtrans = newTrans;
2061 GdipDeleteRegion(graphics->clip);
2062 graphics->clip = newClip;
2064 graphics->contid = container->contid - 1;
2066 graphics->smoothing = container->smoothing;
2067 graphics->compqual = container->compqual;
2068 graphics->interpolation = container->interpolation;
2069 graphics->compmode = container->compmode;
2070 graphics->texthint = container->texthint;
2071 graphics->scale = container->scale;
2072 graphics->unit = container->unit;
2073 graphics->textcontrast = container->textcontrast;
2074 graphics->pixeloffset = container->pixeloffset;
2075 graphics->origin_x = container->origin_x;
2076 graphics->origin_y = container->origin_y;
2081 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2087 if(graphics->hwnd) {
2088 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2089 return GenericError;
2091 rect->X = wnd_rect.left;
2092 rect->Y = wnd_rect.top;
2093 rect->Width = wnd_rect.right - wnd_rect.left;
2094 rect->Height = wnd_rect.bottom - wnd_rect.top;
2095 }else if (graphics->image){
2096 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2097 if (stat == Ok && unit != UnitPixel)
2098 FIXME("need to convert from unit %i\n", unit);
2102 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2103 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2109 /* on success, rgn will contain the region of the graphics object which
2110 * is visible after clipping has been applied */
2111 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2117 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2120 if((stat = GdipCreateRegion(&tmp)) != Ok)
2123 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2126 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2129 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2132 GdipDeleteRegion(tmp);
2136 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2140 if (font->unit == UnitPixel)
2142 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2146 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2147 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2149 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2152 lf->lfHeight = -(height + 0.5);
2154 lf->lfEscapement = 0;
2155 lf->lfOrientation = 0;
2156 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2157 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2158 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2159 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2160 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2161 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2162 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2163 lf->lfQuality = DEFAULT_QUALITY;
2164 lf->lfPitchAndFamily = 0;
2165 strcpyW(lf->lfFaceName, font->family->FamilyName);
2168 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2169 GDIPCONST GpStringFormat *format, HFONT *hfont)
2171 HDC hdc = CreateCompatibleDC(0);
2173 REAL angle, rel_width, rel_height, font_height;
2175 HFONT unscaled_font;
2176 TEXTMETRICW textmet;
2178 if (font->unit == UnitPixel)
2179 font_height = font->emSize;
2182 REAL unit_scale, res;
2184 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2185 unit_scale = units_scale(font->unit, graphics->unit, res);
2187 font_height = font->emSize * unit_scale;
2197 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2198 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2199 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2200 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2201 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2202 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2204 get_log_fontW(font, graphics, &lfw);
2205 lfw.lfHeight = gdip_round(font_height * rel_height);
2206 unscaled_font = CreateFontIndirectW(&lfw);
2208 SelectObject(hdc, unscaled_font);
2209 GetTextMetricsW(hdc, &textmet);
2211 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2212 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2214 *hfont = CreateFontIndirectW(&lfw);
2217 DeleteObject(unscaled_font);
2220 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2222 TRACE("(%p, %p)\n", hdc, graphics);
2224 return GdipCreateFromHDC2(hdc, NULL, graphics);
2227 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2231 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2233 if(hDevice != NULL) {
2234 FIXME("Don't know how to handle parameter hDevice\n");
2235 return NotImplemented;
2241 if(graphics == NULL)
2242 return InvalidParameter;
2244 *graphics = GdipAlloc(sizeof(GpGraphics));
2245 if(!*graphics) return OutOfMemory;
2247 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2248 GdipFree(*graphics);
2252 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2253 GdipFree((*graphics)->worldtrans);
2254 GdipFree(*graphics);
2258 (*graphics)->hdc = hdc;
2259 (*graphics)->hwnd = WindowFromDC(hdc);
2260 (*graphics)->owndc = FALSE;
2261 (*graphics)->smoothing = SmoothingModeDefault;
2262 (*graphics)->compqual = CompositingQualityDefault;
2263 (*graphics)->interpolation = InterpolationModeBilinear;
2264 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2265 (*graphics)->compmode = CompositingModeSourceOver;
2266 (*graphics)->unit = UnitDisplay;
2267 (*graphics)->scale = 1.0;
2268 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2269 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2270 (*graphics)->busy = FALSE;
2271 (*graphics)->textcontrast = 4;
2272 list_init(&(*graphics)->containers);
2273 (*graphics)->contid = 0;
2275 TRACE("<-- %p\n", *graphics);
2280 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2284 *graphics = GdipAlloc(sizeof(GpGraphics));
2285 if(!*graphics) return OutOfMemory;
2287 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
2288 GdipFree(*graphics);
2292 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2293 GdipFree((*graphics)->worldtrans);
2294 GdipFree(*graphics);
2298 (*graphics)->hdc = NULL;
2299 (*graphics)->hwnd = NULL;
2300 (*graphics)->owndc = FALSE;
2301 (*graphics)->image = image;
2302 (*graphics)->smoothing = SmoothingModeDefault;
2303 (*graphics)->compqual = CompositingQualityDefault;
2304 (*graphics)->interpolation = InterpolationModeBilinear;
2305 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2306 (*graphics)->compmode = CompositingModeSourceOver;
2307 (*graphics)->unit = UnitDisplay;
2308 (*graphics)->scale = 1.0;
2309 (*graphics)->xres = image->xres;
2310 (*graphics)->yres = image->yres;
2311 (*graphics)->busy = FALSE;
2312 (*graphics)->textcontrast = 4;
2313 list_init(&(*graphics)->containers);
2314 (*graphics)->contid = 0;
2316 TRACE("<-- %p\n", *graphics);
2321 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2326 TRACE("(%p, %p)\n", hwnd, graphics);
2330 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2332 ReleaseDC(hwnd, hdc);
2336 (*graphics)->hwnd = hwnd;
2337 (*graphics)->owndc = TRUE;
2342 /* FIXME: no icm handling */
2343 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2345 TRACE("(%p, %p)\n", hwnd, graphics);
2347 return GdipCreateFromHWND(hwnd, graphics);
2350 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
2351 GpMetafile **metafile)
2353 IStream *stream = NULL;
2355 ENHMETAHEADER *copy;
2356 GpStatus retval = Ok;
2358 TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
2360 if(!hemf || !metafile)
2361 return InvalidParameter;
2363 read = GetEnhMetaFileBits(hemf, 0, NULL);
2364 copy = GdipAlloc(read);
2365 GetEnhMetaFileBits(hemf, read, (BYTE *)copy);
2367 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
2368 ERR("could not make stream\n");
2370 retval = GenericError;
2374 *metafile = GdipAlloc(sizeof(GpMetafile));
2376 retval = OutOfMemory;
2380 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2381 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
2383 retval = GenericError;
2388 (*metafile)->image.type = ImageTypeMetafile;
2389 memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
2390 (*metafile)->image.palette = NULL;
2391 (*metafile)->image.xres = (REAL)copy->szlDevice.cx;
2392 (*metafile)->image.yres = (REAL)copy->szlDevice.cy;
2393 (*metafile)->bounds.X = (REAL)copy->rclBounds.left;
2394 (*metafile)->bounds.Y = (REAL)copy->rclBounds.top;
2395 (*metafile)->bounds.Width = (REAL)(copy->rclBounds.right - copy->rclBounds.left);
2396 (*metafile)->bounds.Height = (REAL)(copy->rclBounds.bottom - copy->rclBounds.top);
2397 (*metafile)->unit = UnitPixel;
2400 DeleteEnhMetaFile(hemf);
2402 TRACE("<-- %p\n", *metafile);
2406 GdipFree(*metafile);
2407 IStream_Release(stream);
2411 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
2412 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2417 GpStatus retval = Ok;
2419 TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
2421 if(!hwmf || !metafile || !placeable)
2422 return InvalidParameter;
2425 read = GetMetaFileBitsEx(hwmf, 0, NULL);
2427 return GenericError;
2428 copy = GdipAlloc(read);
2429 GetMetaFileBitsEx(hwmf, read, copy);
2431 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
2434 retval = GdipCreateMetafileFromEmf(hemf, FALSE, metafile);
2438 (*metafile)->image.xres = (REAL)placeable->Inch;
2439 (*metafile)->image.yres = (REAL)placeable->Inch;
2440 (*metafile)->bounds.X = ((REAL)placeable->BoundingBox.Left) / ((REAL)placeable->Inch);
2441 (*metafile)->bounds.Y = ((REAL)placeable->BoundingBox.Top) / ((REAL)placeable->Inch);
2442 (*metafile)->bounds.Width = (REAL)(placeable->BoundingBox.Right -
2443 placeable->BoundingBox.Left);
2444 (*metafile)->bounds.Height = (REAL)(placeable->BoundingBox.Bottom -
2445 placeable->BoundingBox.Top);
2447 if (delete) DeleteMetaFile(hwmf);
2452 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
2453 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
2455 HMETAFILE hmf = GetMetaFileW(file);
2457 TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
2459 if(!hmf) return InvalidParameter;
2461 return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
2464 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
2465 GpMetafile **metafile)
2467 FIXME("(%p, %p): stub\n", file, metafile);
2468 return NotImplemented;
2471 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
2472 GpMetafile **metafile)
2474 FIXME("(%p, %p): stub\n", stream, metafile);
2475 return NotImplemented;
2478 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2479 UINT access, IStream **stream)
2484 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2486 if(!stream || !filename)
2487 return InvalidParameter;
2489 if(access & GENERIC_WRITE)
2490 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2491 else if(access & GENERIC_READ)
2492 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2494 return InvalidParameter;
2496 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2498 return hresult_to_status(ret);
2501 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2503 GraphicsContainerItem *cont, *next;
2505 TRACE("(%p)\n", graphics);
2507 if(!graphics) return InvalidParameter;
2508 if(graphics->busy) return ObjectBusy;
2510 if (graphics->image && graphics->image->type == ImageTypeMetafile)
2512 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2518 ReleaseDC(graphics->hwnd, graphics->hdc);
2520 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2521 list_remove(&cont->entry);
2522 delete_container(cont);
2525 GdipDeleteRegion(graphics->clip);
2526 GdipDeleteMatrix(graphics->worldtrans);
2532 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2533 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2535 INT save_state, num_pts;
2536 GpPointF points[MAX_ARC_PTS];
2539 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2540 width, height, startAngle, sweepAngle);
2542 if(!graphics || !pen || width <= 0 || height <= 0)
2543 return InvalidParameter;
2550 FIXME("graphics object has no HDC\n");
2554 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
2556 save_state = prepare_dc(graphics, pen);
2558 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
2560 restore_dc(graphics, save_state);
2565 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2566 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2568 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2569 width, height, startAngle, sweepAngle);
2571 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2574 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2575 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2581 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2582 x2, y2, x3, y3, x4, y4);
2584 if(!graphics || !pen)
2585 return InvalidParameter;
2592 FIXME("graphics object has no HDC\n");
2605 save_state = prepare_dc(graphics, pen);
2607 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2609 restore_dc(graphics, save_state);
2614 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2615 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2621 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2622 x2, y2, x3, y3, x4, y4);
2624 if(!graphics || !pen)
2625 return InvalidParameter;
2632 FIXME("graphics object has no HDC\n");
2645 save_state = prepare_dc(graphics, pen);
2647 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
2649 restore_dc(graphics, save_state);
2654 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2655 GDIPCONST GpPointF *points, INT count)
2660 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2662 if(!graphics || !pen || !points || (count <= 0))
2663 return InvalidParameter;
2668 for(i = 0; i < floor(count / 4); i++){
2669 ret = GdipDrawBezier(graphics, pen,
2670 points[4*i].X, points[4*i].Y,
2671 points[4*i + 1].X, points[4*i + 1].Y,
2672 points[4*i + 2].X, points[4*i + 2].Y,
2673 points[4*i + 3].X, points[4*i + 3].Y);
2681 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2682 GDIPCONST GpPoint *points, INT count)
2688 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2690 if(!graphics || !pen || !points || (count <= 0))
2691 return InvalidParameter;
2696 pts = GdipAlloc(sizeof(GpPointF) * count);
2700 for(i = 0; i < count; i++){
2701 pts[i].X = (REAL)points[i].X;
2702 pts[i].Y = (REAL)points[i].Y;
2705 ret = GdipDrawBeziers(graphics,pen,pts,count);
2712 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2713 GDIPCONST GpPointF *points, INT count)
2715 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2717 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2720 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2721 GDIPCONST GpPoint *points, INT count)
2723 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2725 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2728 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2729 GDIPCONST GpPointF *points, INT count, REAL tension)
2734 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2736 if(!graphics || !pen || !points || count <= 0)
2737 return InvalidParameter;
2742 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2745 stat = GdipAddPathClosedCurve2(path, points, count, tension);
2747 GdipDeletePath(path);
2751 stat = GdipDrawPath(graphics, pen, path);
2753 GdipDeletePath(path);
2758 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2759 GDIPCONST GpPoint *points, INT count, REAL tension)
2765 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2767 if(!points || count <= 0)
2768 return InvalidParameter;
2770 ptf = GdipAlloc(sizeof(GpPointF)*count);
2774 for(i = 0; i < count; i++){
2775 ptf[i].X = (REAL)points[i].X;
2776 ptf[i].Y = (REAL)points[i].Y;
2779 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2786 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2787 GDIPCONST GpPointF *points, INT count)
2789 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2791 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2794 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2795 GDIPCONST GpPoint *points, INT count)
2801 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2804 return InvalidParameter;
2806 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2810 for(i = 0; i < count; i++){
2811 pointsF[i].X = (REAL)points[i].X;
2812 pointsF[i].Y = (REAL)points[i].Y;
2815 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2821 /* Approximates cardinal spline with Bezier curves. */
2822 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2823 GDIPCONST GpPointF *points, INT count, REAL tension)
2825 /* PolyBezier expects count*3-2 points. */
2826 INT i, len_pt = count*3-2, save_state;
2828 REAL x1, x2, y1, y2;
2831 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2833 if(!graphics || !pen)
2834 return InvalidParameter;
2840 return InvalidParameter;
2844 FIXME("graphics object has no HDC\n");
2848 pt = GdipAlloc(len_pt * sizeof(GpPointF));
2852 tension = tension * TENSION_CONST;
2854 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2857 pt[0].X = points[0].X;
2858 pt[0].Y = points[0].Y;
2862 for(i = 0; i < count-2; i++){
2863 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2867 pt[3*i+3].X = points[i+1].X;
2868 pt[3*i+3].Y = points[i+1].Y;
2873 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2874 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2876 pt[len_pt-2].X = x1;
2877 pt[len_pt-2].Y = y1;
2878 pt[len_pt-1].X = points[count-1].X;
2879 pt[len_pt-1].Y = points[count-1].Y;
2881 save_state = prepare_dc(graphics, pen);
2883 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2886 restore_dc(graphics, save_state);
2891 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2892 GDIPCONST GpPoint *points, INT count, REAL tension)
2898 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2901 return InvalidParameter;
2903 pointsF = GdipAlloc(sizeof(GpPointF)*count);
2907 for(i = 0; i < count; i++){
2908 pointsF[i].X = (REAL)points[i].X;
2909 pointsF[i].Y = (REAL)points[i].Y;
2912 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2918 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2919 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2922 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2924 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2925 return InvalidParameter;
2928 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2931 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2932 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2935 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2941 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2942 return InvalidParameter;
2945 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2948 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2949 REAL y, REAL width, REAL height)
2955 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2957 if(!graphics || !pen)
2958 return InvalidParameter;
2965 FIXME("graphics object has no HDC\n");
2971 ptf[1].X = x + width;
2972 ptf[1].Y = y + height;
2974 save_state = prepare_dc(graphics, pen);
2975 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2977 transform_and_round_points(graphics, pti, ptf, 2);
2979 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2981 restore_dc(graphics, save_state);
2986 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2987 INT y, INT width, INT height)
2989 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2991 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2995 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2999 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
3001 if(!graphics || !image)
3002 return InvalidParameter;
3004 GdipGetImageWidth(image, &width);
3005 GdipGetImageHeight(image, &height);
3007 return GdipDrawImagePointRect(graphics, image, x, y,
3008 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
3011 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
3014 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
3016 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
3019 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
3020 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
3024 REAL scale_x, scale_y, width, height;
3026 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3028 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
3029 scale_x *= graphics->xres / image->xres;
3030 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
3031 scale_y *= graphics->yres / image->yres;
3032 width = srcwidth * scale_x;
3033 height = srcheight * scale_y;
3035 points[0].X = points[2].X = x;
3036 points[0].Y = points[1].Y = y;
3037 points[1].X = x + width;
3038 points[2].Y = y + height;
3040 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3041 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
3044 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
3045 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
3048 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
3051 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
3052 GDIPCONST GpPointF *dstpoints, INT count)
3056 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3059 return InvalidParameter;
3061 GdipGetImageWidth(image, &width);
3062 GdipGetImageHeight(image, &height);
3064 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
3065 width, height, UnitPixel, NULL, NULL, NULL);
3068 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
3069 GDIPCONST GpPoint *dstpoints, INT count)
3073 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
3075 if (count != 3 || !dstpoints)
3076 return InvalidParameter;
3078 ptf[0].X = (REAL)dstpoints[0].X;
3079 ptf[0].Y = (REAL)dstpoints[0].Y;
3080 ptf[1].X = (REAL)dstpoints[1].X;
3081 ptf[1].Y = (REAL)dstpoints[1].Y;
3082 ptf[2].X = (REAL)dstpoints[2].X;
3083 ptf[2].Y = (REAL)dstpoints[2].Y;
3085 return GdipDrawImagePoints(graphics, image, ptf, count);
3088 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
3089 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
3090 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3091 DrawImageAbort callback, VOID * callbackData)
3097 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
3098 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3102 return NotImplemented;
3104 if(!graphics || !image || !points || count != 3)
3105 return InvalidParameter;
3107 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
3108 debugstr_pointf(&points[2]));
3110 memcpy(ptf, points, 3 * sizeof(GpPointF));
3111 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
3112 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
3113 if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
3115 transform_and_round_points(graphics, pti, ptf, 4);
3117 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
3118 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
3120 srcx = units_to_pixels(srcx, srcUnit, image->xres);
3121 srcy = units_to_pixels(srcy, srcUnit, image->yres);
3122 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
3123 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
3124 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
3130 FIXME("graphics object has no HDC\n");
3133 if(IPicture_Render(image->picture, graphics->hdc,
3134 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3135 srcx, srcy, srcwidth, srcheight, NULL) != S_OK)
3138 callback(callbackData);
3139 return GenericError;
3142 else if (image->type == ImageTypeBitmap)
3144 GpBitmap* bitmap = (GpBitmap*)image;
3147 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
3148 graphics->xres, graphics->yres,
3149 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
3150 graphics->scale, image->xres, image->yres, bitmap->format,
3151 imageAttributes ? imageAttributes->outside_color : 0);
3153 if (imageAttributes ||
3154 (graphics->image && graphics->image->type == ImageTypeBitmap) ||
3155 ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
3156 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
3157 srcx < 0 || srcy < 0 ||
3158 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
3165 int i, x, y, src_stride, dst_stride;
3166 GpMatrix *dst_to_src;
3167 REAL m11, m12, m21, m22, mdx, mdy;
3168 LPBYTE src_data, dst_data;
3169 BitmapData lockeddata;
3170 InterpolationMode interpolation = graphics->interpolation;
3171 PixelOffsetMode offset_mode = graphics->pixeloffset;
3172 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
3173 REAL x_dx, x_dy, y_dx, y_dy;
3174 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
3176 if (!imageAttributes)
3177 imageAttributes = &defaultImageAttributes;
3179 dst_area.left = dst_area.right = pti[0].x;
3180 dst_area.top = dst_area.bottom = pti[0].y;
3183 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
3184 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
3185 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
3186 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
3189 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
3191 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
3192 m21 = (ptf[2].X - ptf[0].X) / srcheight;
3193 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
3194 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
3195 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
3196 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
3198 stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
3199 if (stat != Ok) return stat;
3201 stat = GdipInvertMatrix(dst_to_src);
3204 GdipDeleteMatrix(dst_to_src);
3208 dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3211 GdipDeleteMatrix(dst_to_src);
3215 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3217 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
3218 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
3220 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
3222 src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
3226 GdipDeleteMatrix(dst_to_src);
3229 src_stride = sizeof(ARGB) * src_area.Width;
3231 /* Read the bits we need from the source bitmap into an ARGB buffer. */
3232 lockeddata.Width = src_area.Width;
3233 lockeddata.Height = src_area.Height;
3234 lockeddata.Stride = src_stride;
3235 lockeddata.PixelFormat = PixelFormat32bppARGB;
3236 lockeddata.Scan0 = src_data;
3238 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3239 PixelFormat32bppARGB, &lockeddata);
3242 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3246 if (src_data != dst_data)
3249 GdipDeleteMatrix(dst_to_src);
3253 apply_image_attributes(imageAttributes, src_data,
3254 src_area.Width, src_area.Height,
3255 src_stride, ColorAdjustTypeBitmap);
3257 /* Transform the bits as needed to the destination. */
3258 GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
3260 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3261 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3262 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3263 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3265 for (x=dst_area.left; x<dst_area.right; x++)
3267 for (y=dst_area.top; y<dst_area.bottom; y++)
3269 GpPointF src_pointf;
3272 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3273 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3275 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3277 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3278 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3279 imageAttributes, interpolation, offset_mode);
3285 GdipDeleteMatrix(dst_to_src);
3289 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3290 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
3299 int temp_hdc=0, temp_bitmap=0;
3300 HBITMAP hbitmap, old_hbm=NULL;
3302 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3303 bitmap->format == PixelFormat24bppRGB ||
3304 bitmap->format == PixelFormat32bppRGB ||
3305 bitmap->format == PixelFormat32bppPARGB))
3307 BITMAPINFOHEADER bih;
3309 PixelFormat dst_format;
3311 /* we can't draw a bitmap of this format directly */
3312 hdc = CreateCompatibleDC(0);
3316 bih.biSize = sizeof(BITMAPINFOHEADER);
3317 bih.biWidth = bitmap->width;
3318 bih.biHeight = -bitmap->height;
3320 bih.biBitCount = 32;
3321 bih.biCompression = BI_RGB;
3322 bih.biSizeImage = 0;
3323 bih.biXPelsPerMeter = 0;
3324 bih.biYPelsPerMeter = 0;
3326 bih.biClrImportant = 0;
3328 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3329 (void**)&temp_bits, NULL, 0);
3331 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3332 dst_format = PixelFormat32bppPARGB;
3334 dst_format = PixelFormat32bppRGB;
3336 convert_pixels(bitmap->width, bitmap->height,
3337 bitmap->width*4, temp_bits, dst_format,
3338 bitmap->stride, bitmap->bits, bitmap->format,
3339 bitmap->image.palette);
3343 if (bitmap->hbitmap)
3344 hbitmap = bitmap->hbitmap;
3347 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3352 temp_hdc = (hdc == 0);
3357 if (!hdc) hdc = CreateCompatibleDC(0);
3358 old_hbm = SelectObject(hdc, hbitmap);
3361 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3363 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3364 hdc, srcx, srcy, srcwidth, srcheight);
3368 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3369 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3374 SelectObject(hdc, old_hbm);
3379 DeleteObject(hbitmap);
3384 ERR("GpImage with no IPicture or HBITMAP?!\n");
3385 return NotImplemented;
3391 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3392 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3393 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3394 DrawImageAbort callback, VOID * callbackData)
3396 GpPointF pointsF[3];
3399 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3400 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3403 if(!points || count!=3)
3404 return InvalidParameter;
3406 for(i = 0; i < count; i++){
3407 pointsF[i].X = (REAL)points[i].X;
3408 pointsF[i].Y = (REAL)points[i].Y;
3411 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3412 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3413 callback, callbackData);
3416 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3417 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3418 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3419 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3420 VOID * callbackData)
3424 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3425 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3426 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3430 points[1].X = dstx + dstwidth;
3433 points[2].Y = dsty + dstheight;
3435 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3436 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3439 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3440 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3441 INT srcwidth, INT srcheight, GpUnit srcUnit,
3442 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3443 VOID * callbackData)
3447 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3448 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3449 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3453 points[1].X = dstx + dstwidth;
3456 points[2].Y = dsty + dstheight;
3458 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3459 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3462 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3463 REAL x, REAL y, REAL width, REAL height)
3469 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3471 if(!graphics || !image)
3472 return InvalidParameter;
3474 ret = GdipGetImageBounds(image, &bounds, &unit);
3478 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3479 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3480 unit, NULL, NULL, NULL);
3483 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3484 INT x, INT y, INT width, INT height)
3486 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3488 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3491 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3492 REAL y1, REAL x2, REAL y2)
3498 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3500 if(!pen || !graphics)
3501 return InvalidParameter;
3508 FIXME("graphics object has no HDC\n");
3517 save_state = prepare_dc(graphics, pen);
3519 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3521 restore_dc(graphics, save_state);
3526 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3527 INT y1, INT x2, INT y2)
3533 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3535 if(!pen || !graphics)
3536 return InvalidParameter;
3543 FIXME("graphics object has no HDC\n");
3552 save_state = prepare_dc(graphics, pen);
3554 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
3556 restore_dc(graphics, save_state);
3561 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3562 GpPointF *points, INT count)
3567 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3569 if(!pen || !graphics || (count < 2))
3570 return InvalidParameter;
3577 FIXME("graphics object has no HDC\n");
3581 save_state = prepare_dc(graphics, pen);
3583 retval = draw_polyline(graphics, pen, points, count, TRUE);
3585 restore_dc(graphics, save_state);
3590 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3591 GpPoint *points, INT count)
3595 GpPointF *ptf = NULL;
3598 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3600 if(!pen || !graphics || (count < 2))
3601 return InvalidParameter;
3608 FIXME("graphics object has no HDC\n");
3612 ptf = GdipAlloc(count * sizeof(GpPointF));
3613 if(!ptf) return OutOfMemory;
3615 for(i = 0; i < count; i ++){
3616 ptf[i].X = (REAL) points[i].X;
3617 ptf[i].Y = (REAL) points[i].Y;
3620 save_state = prepare_dc(graphics, pen);
3622 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
3624 restore_dc(graphics, save_state);
3630 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3635 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3637 if(!pen || !graphics)
3638 return InvalidParameter;
3645 FIXME("graphics object has no HDC\n");
3649 save_state = prepare_dc(graphics, pen);
3651 retval = draw_poly(graphics, pen, path->pathdata.Points,
3652 path->pathdata.Types, path->pathdata.Count, TRUE);
3654 restore_dc(graphics, save_state);
3659 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3660 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3664 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3665 width, height, startAngle, sweepAngle);
3667 if(!graphics || !pen)
3668 return InvalidParameter;
3675 FIXME("graphics object has no HDC\n");
3679 save_state = prepare_dc(graphics, pen);
3680 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3682 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3684 restore_dc(graphics, save_state);
3689 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3690 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3692 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3693 width, height, startAngle, sweepAngle);
3695 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3698 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3699 REAL y, REAL width, REAL height)
3705 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3707 if(!pen || !graphics)
3708 return InvalidParameter;
3715 FIXME("graphics object has no HDC\n");
3721 ptf[1].X = x + width;
3723 ptf[2].X = x + width;
3724 ptf[2].Y = y + height;
3726 ptf[3].Y = y + height;
3728 save_state = prepare_dc(graphics, pen);
3729 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3731 transform_and_round_points(graphics, pti, ptf, 4);
3732 Polygon(graphics->hdc, pti, 4);
3734 restore_dc(graphics, save_state);
3739 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3740 INT y, INT width, INT height)
3742 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3744 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3747 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3748 GDIPCONST GpRectF* rects, INT count)
3754 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3756 if(!graphics || !pen || !rects || count < 1)
3757 return InvalidParameter;
3764 FIXME("graphics object has no HDC\n");
3768 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3769 pti = GdipAlloc(4 * count * sizeof(POINT));
3777 for(i = 0; i < count; i++){
3778 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3779 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3780 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3781 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3784 save_state = prepare_dc(graphics, pen);
3785 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3787 transform_and_round_points(graphics, pti, ptf, 4 * count);
3789 for(i = 0; i < count; i++)
3790 Polygon(graphics->hdc, &pti[4 * i], 4);
3792 restore_dc(graphics, save_state);
3800 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3801 GDIPCONST GpRect* rects, INT count)
3807 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3809 if(!rects || count<=0)
3810 return InvalidParameter;
3812 rectsF = GdipAlloc(sizeof(GpRectF) * count);
3816 for(i = 0;i < count;i++){
3817 rectsF[i].X = (REAL)rects[i].X;
3818 rectsF[i].Y = (REAL)rects[i].Y;
3819 rectsF[i].Width = (REAL)rects[i].Width;
3820 rectsF[i].Height = (REAL)rects[i].Height;
3823 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3829 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3830 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3835 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3836 count, tension, fill);
3838 if(!graphics || !brush || !points)
3839 return InvalidParameter;
3844 if(count == 1) /* Do nothing */
3847 stat = GdipCreatePath(fill, &path);
3851 stat = GdipAddPathClosedCurve2(path, points, count, tension);
3853 GdipDeletePath(path);
3857 stat = GdipFillPath(graphics, brush, path);
3859 GdipDeletePath(path);
3863 GdipDeletePath(path);
3868 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3869 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3875 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3876 count, tension, fill);
3878 if(!points || count == 0)
3879 return InvalidParameter;
3881 if(count == 1) /* Do nothing */
3884 ptf = GdipAlloc(sizeof(GpPointF)*count);
3888 for(i = 0;i < count;i++){
3889 ptf[i].X = (REAL)points[i].X;
3890 ptf[i].Y = (REAL)points[i].Y;
3893 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3900 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3901 GDIPCONST GpPointF *points, INT count)
3903 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3904 return GdipFillClosedCurve2(graphics, brush, points, count,
3905 0.5f, FillModeAlternate);
3908 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3909 GDIPCONST GpPoint *points, INT count)
3911 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3912 return GdipFillClosedCurve2I(graphics, brush, points, count,
3913 0.5f, FillModeAlternate);
3916 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3917 REAL y, REAL width, REAL height)
3922 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3924 if(!graphics || !brush)
3925 return InvalidParameter;
3930 stat = GdipCreatePath(FillModeAlternate, &path);
3934 stat = GdipAddPathEllipse(path, x, y, width, height);
3937 stat = GdipFillPath(graphics, brush, path);
3939 GdipDeletePath(path);
3945 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3946 INT y, INT width, INT height)
3948 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3950 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3953 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3958 if(!graphics->hdc || !brush_can_fill_path(brush))
3959 return NotImplemented;
3961 save_state = SaveDC(graphics->hdc);
3962 EndPath(graphics->hdc);
3963 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3966 BeginPath(graphics->hdc);
3967 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3968 path->pathdata.Types, path->pathdata.Count, FALSE);
3973 EndPath(graphics->hdc);
3974 brush_fill_path(graphics, brush);
3979 RestoreDC(graphics->hdc, save_state);
3984 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3989 if (!brush_can_fill_pixels(brush))
3990 return NotImplemented;
3992 /* FIXME: This could probably be done more efficiently without regions. */
3994 stat = GdipCreateRegionPath(path, &rgn);
3998 stat = GdipFillRegion(graphics, brush, rgn);
4000 GdipDeleteRegion(rgn);
4006 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
4008 GpStatus stat = NotImplemented;
4010 TRACE("(%p, %p, %p)\n", graphics, brush, path);
4012 if(!brush || !graphics || !path)
4013 return InvalidParameter;
4018 if (!graphics->image)
4019 stat = GDI32_GdipFillPath(graphics, brush, path);
4021 if (stat == NotImplemented)
4022 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
4024 if (stat == NotImplemented)
4026 FIXME("Not implemented for brushtype %i\n", brush->bt);
4033 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
4034 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
4039 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
4040 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4042 if(!graphics || !brush)
4043 return InvalidParameter;
4048 stat = GdipCreatePath(FillModeAlternate, &path);
4052 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
4055 stat = GdipFillPath(graphics, brush, path);
4057 GdipDeletePath(path);
4063 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
4064 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
4066 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
4067 graphics, brush, x, y, width, height, startAngle, sweepAngle);
4069 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
4072 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
4073 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
4078 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4080 if(!graphics || !brush || !points || !count)
4081 return InvalidParameter;
4086 stat = GdipCreatePath(fillMode, &path);
4090 stat = GdipAddPathPolygon(path, points, count);
4093 stat = GdipFillPath(graphics, brush, path);
4095 GdipDeletePath(path);
4101 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
4102 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
4107 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
4109 if(!graphics || !brush || !points || !count)
4110 return InvalidParameter;
4115 stat = GdipCreatePath(fillMode, &path);
4119 stat = GdipAddPathPolygonI(path, points, count);
4122 stat = GdipFillPath(graphics, brush, path);
4124 GdipDeletePath(path);
4130 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
4131 GDIPCONST GpPointF *points, INT count)
4133 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4135 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
4138 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
4139 GDIPCONST GpPoint *points, INT count)
4141 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
4143 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
4146 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
4147 REAL x, REAL y, REAL width, REAL height)
4152 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
4154 if(!graphics || !brush)
4155 return InvalidParameter;
4160 stat = GdipCreatePath(FillModeAlternate, &path);
4164 stat = GdipAddPathRectangle(path, x, y, width, height);
4167 stat = GdipFillPath(graphics, brush, path);
4169 GdipDeletePath(path);
4175 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
4176 INT x, INT y, INT width, INT height)
4178 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
4180 return GdipFillRectangle(graphics, brush, x, y, width, height);
4183 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
4189 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4192 return InvalidParameter;
4194 for(i = 0; i < count; i++){
4195 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
4196 if(ret != Ok) return ret;
4202 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
4209 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
4211 if(!rects || count <= 0)
4212 return InvalidParameter;
4214 rectsF = GdipAlloc(sizeof(GpRectF)*count);
4218 for(i = 0; i < count; i++){
4219 rectsF[i].X = (REAL)rects[i].X;
4220 rectsF[i].Y = (REAL)rects[i].Y;
4221 rectsF[i].X = (REAL)rects[i].Width;
4222 rectsF[i].Height = (REAL)rects[i].Height;
4225 ret = GdipFillRectangles(graphics,brush,rectsF,count);
4231 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4239 if(!graphics->hdc || !brush_can_fill_path(brush))
4240 return NotImplemented;
4242 status = GdipGetRegionHRgn(region, graphics, &hrgn);
4246 save_state = SaveDC(graphics->hdc);
4247 EndPath(graphics->hdc);
4249 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
4251 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
4253 BeginPath(graphics->hdc);
4254 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
4255 EndPath(graphics->hdc);
4257 brush_fill_path(graphics, brush);
4260 RestoreDC(graphics->hdc, save_state);
4267 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
4271 GpRegion *temp_region;
4272 GpMatrix *world_to_device;
4273 GpRectF graphics_bounds;
4277 GpRect gp_bound_rect;
4279 if (!brush_can_fill_pixels(brush))
4280 return NotImplemented;
4282 stat = get_graphics_bounds(graphics, &graphics_bounds);
4285 stat = GdipCloneRegion(region, &temp_region);
4289 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
4290 CoordinateSpaceWorld, &world_to_device);
4294 stat = GdipTransformRegion(temp_region, world_to_device);
4296 GdipDeleteMatrix(world_to_device);
4300 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
4303 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
4305 GdipDeleteRegion(temp_region);
4308 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
4310 DeleteObject(hregion);
4316 gp_bound_rect.X = bound_rect.left;
4317 gp_bound_rect.Y = bound_rect.top;
4318 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4319 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4321 pixel_data = GdipAlloc(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4327 stat = brush_fill_pixels(graphics, brush, pixel_data,
4328 &gp_bound_rect, gp_bound_rect.Width);
4331 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4332 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4333 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion);
4335 GdipFree(pixel_data);
4338 DeleteObject(hregion);
4344 /*****************************************************************************
4345 * GdipFillRegion [GDIPLUS.@]
4347 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4350 GpStatus stat = NotImplemented;
4352 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4354 if (!(graphics && brush && region))
4355 return InvalidParameter;
4360 if (!graphics->image)
4361 stat = GDI32_GdipFillRegion(graphics, brush, region);
4363 if (stat == NotImplemented)
4364 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4366 if (stat == NotImplemented)
4368 FIXME("not implemented for brushtype %i\n", brush->bt);
4375 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4377 TRACE("(%p,%u)\n", graphics, intention);
4380 return InvalidParameter;
4385 /* We have no internal operation queue, so there's no need to clear it. */
4393 /*****************************************************************************
4394 * GdipGetClipBounds [GDIPLUS.@]
4396 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4398 TRACE("(%p, %p)\n", graphics, rect);
4401 return InvalidParameter;
4406 return GdipGetRegionBounds(graphics->clip, graphics, rect);
4409 /*****************************************************************************
4410 * GdipGetClipBoundsI [GDIPLUS.@]
4412 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4414 TRACE("(%p, %p)\n", graphics, rect);
4417 return InvalidParameter;
4422 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4425 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4426 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4427 CompositingMode *mode)
4429 TRACE("(%p, %p)\n", graphics, mode);
4431 if(!graphics || !mode)
4432 return InvalidParameter;
4437 *mode = graphics->compmode;
4442 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4443 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4444 CompositingQuality *quality)
4446 TRACE("(%p, %p)\n", graphics, quality);
4448 if(!graphics || !quality)
4449 return InvalidParameter;
4454 *quality = graphics->compqual;
4459 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4460 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4461 InterpolationMode *mode)
4463 TRACE("(%p, %p)\n", graphics, mode);
4465 if(!graphics || !mode)
4466 return InvalidParameter;
4471 *mode = graphics->interpolation;
4476 /* FIXME: Need to handle color depths less than 24bpp */
4477 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4479 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4481 if(!graphics || !argb)
4482 return InvalidParameter;
4490 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4492 TRACE("(%p, %p)\n", graphics, scale);
4494 if(!graphics || !scale)
4495 return InvalidParameter;
4500 *scale = graphics->scale;
4505 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4507 TRACE("(%p, %p)\n", graphics, unit);
4509 if(!graphics || !unit)
4510 return InvalidParameter;
4515 *unit = graphics->unit;
4520 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4521 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4524 TRACE("(%p, %p)\n", graphics, mode);
4526 if(!graphics || !mode)
4527 return InvalidParameter;
4532 *mode = graphics->pixeloffset;
4537 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4538 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4540 TRACE("(%p, %p)\n", graphics, mode);
4542 if(!graphics || !mode)
4543 return InvalidParameter;
4548 *mode = graphics->smoothing;
4553 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4555 TRACE("(%p, %p)\n", graphics, contrast);
4557 if(!graphics || !contrast)
4558 return InvalidParameter;
4560 *contrast = graphics->textcontrast;
4565 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4566 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4567 TextRenderingHint *hint)
4569 TRACE("(%p, %p)\n", graphics, hint);
4571 if(!graphics || !hint)
4572 return InvalidParameter;
4577 *hint = graphics->texthint;
4582 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4587 TRACE("(%p, %p)\n", graphics, rect);
4589 if(!graphics || !rect)
4590 return InvalidParameter;
4595 /* intersect window and graphics clipping regions */
4596 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4599 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4602 /* get bounds of the region */
4603 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4606 GdipDeleteRegion(clip_rgn);
4611 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4616 TRACE("(%p, %p)\n", graphics, rect);
4618 if(!graphics || !rect)
4619 return InvalidParameter;
4621 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4623 rect->X = gdip_round(rectf.X);
4624 rect->Y = gdip_round(rectf.Y);
4625 rect->Width = gdip_round(rectf.Width);
4626 rect->Height = gdip_round(rectf.Height);
4632 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4634 TRACE("(%p, %p)\n", graphics, matrix);
4636 if(!graphics || !matrix)
4637 return InvalidParameter;
4642 *matrix = *graphics->worldtrans;
4646 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4652 TRACE("(%p, %x)\n", graphics, color);
4655 return InvalidParameter;
4660 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4663 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4664 GdipDeleteBrush((GpBrush*)brush);
4668 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4669 wnd_rect.Width, wnd_rect.Height);
4671 GdipDeleteBrush((GpBrush*)brush);
4676 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4678 TRACE("(%p, %p)\n", graphics, res);
4680 if(!graphics || !res)
4681 return InvalidParameter;
4683 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4686 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4692 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4694 if(!graphics || !result)
4695 return InvalidParameter;
4702 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4703 CoordinateSpaceWorld, &pt, 1)) != Ok)
4706 if((stat = GdipCreateRegion(&rgn)) != Ok)
4709 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4712 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4715 GdipDeleteRegion(rgn);
4719 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4721 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4724 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4730 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4732 if(!graphics || !result)
4733 return InvalidParameter;
4740 pts[1].X = x + width;
4741 pts[1].Y = y + height;
4743 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4744 CoordinateSpaceWorld, pts, 2)) != Ok)
4747 pts[1].X -= pts[0].X;
4748 pts[1].Y -= pts[0].Y;
4750 if((stat = GdipCreateRegion(&rgn)) != Ok)
4753 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4756 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4759 GdipDeleteRegion(rgn);
4763 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4765 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4768 GpStatus gdip_format_string(HDC hdc,
4769 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4770 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4771 gdip_format_string_callback callback, void *user_data)
4774 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4775 nheight, lineend, lineno = 0;
4777 StringAlignment halign;
4780 HotkeyPrefix hkprefix;
4781 INT *hotkeyprefix_offsets=NULL;
4782 INT hotkeyprefix_count=0;
4783 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4786 if(length == -1) length = lstrlenW(string);
4788 stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4789 if(!stringdup) return OutOfMemory;
4791 nwidth = rect->Width;
4792 nheight = rect->Height;
4795 hkprefix = format->hkprefix;
4797 hkprefix = HotkeyPrefixNone;
4799 if (hkprefix == HotkeyPrefixShow)
4801 for (i=0; i<length; i++)
4803 if (string[i] == '&')
4804 hotkeyprefix_count++;
4808 if (hotkeyprefix_count)
4809 hotkeyprefix_offsets = GdipAlloc(sizeof(INT) * hotkeyprefix_count);
4811 hotkeyprefix_count = 0;
4813 for(i = 0, j = 0; i < length; i++){
4814 /* FIXME: This makes the indexes passed to callback inaccurate. */
4815 if(!isprintW(string[i]) && (string[i] != '\n'))
4818 /* FIXME: tabs should be handled using tabstops from stringformat */
4819 if (string[i] == '\t')
4822 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4823 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4824 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4832 stringdup[j] = string[i];
4838 if (format) halign = format->align;
4839 else halign = StringAlignmentNear;
4841 while(sum < length){
4842 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4843 nwidth, &fit, NULL, &size);
4849 for(lret = 0; lret < fit; lret++)
4850 if(*(stringdup + sum + lret) == '\n')
4853 /* Line break code (may look strange, but it imitates windows). */
4855 lineend = fit = lret; /* this is not an off-by-one error */
4856 else if(fit < (length - sum)){
4857 if(*(stringdup + sum + fit) == ' ')
4858 while(*(stringdup + sum + fit) == ' ')
4861 while(*(stringdup + sum + fit - 1) != ' '){
4864 if(*(stringdup + sum + fit) == '\t')
4873 while(*(stringdup + sum + lineend - 1) == ' ' ||
4874 *(stringdup + sum + lineend - 1) == '\t')
4880 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4881 nwidth, &j, NULL, &size);
4883 bounds.Width = size.cx;
4885 if(height + size.cy > nheight)
4886 bounds.Height = nheight - (height + size.cy);
4888 bounds.Height = size.cy;
4890 bounds.Y = rect->Y + height;
4894 case StringAlignmentNear:
4898 case StringAlignmentCenter:
4899 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4901 case StringAlignmentFar:
4902 bounds.X = rect->X + rect->Width - bounds.Width;
4906 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4907 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4910 stat = callback(hdc, stringdup, sum, lineend,
4911 font, rect, format, lineno, &bounds,
4912 &hotkeyprefix_offsets[hotkeyprefix_pos],
4913 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4918 sum += fit + (lret < fitcpy ? 1 : 0);
4922 hotkeyprefix_pos = hotkeyprefix_end_pos;
4924 if(height > nheight)
4927 /* Stop if this was a linewrap (but not if it was a linebreak). */
4928 if ((lret == fitcpy) && format &&
4929 (format->attr & (StringFormatFlagsNoWrap | StringFormatFlagsLineLimit)))
4933 GdipFree(stringdup);
4934 GdipFree(hotkeyprefix_offsets);
4939 struct measure_ranges_args {
4941 REAL rel_width, rel_height;
4944 static GpStatus measure_ranges_callback(HDC hdc,
4945 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4946 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4947 INT lineno, const RectF *bounds, INT *underlined_indexes,
4948 INT underlined_index_count, void *user_data)
4952 struct measure_ranges_args *args = user_data;
4954 for (i=0; i<format->range_count; i++)
4956 INT range_start = max(index, format->character_ranges[i].First);
4957 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4958 if (range_start < range_end)
4963 range_rect.Y = bounds->Y / args->rel_height;
4964 range_rect.Height = bounds->Height / args->rel_height;
4966 GetTextExtentExPointW(hdc, string + index, range_start - index,
4967 INT_MAX, NULL, NULL, &range_size);
4968 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4970 GetTextExtentExPointW(hdc, string + index, range_end - index,
4971 INT_MAX, NULL, NULL, &range_size);
4972 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4974 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4983 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4984 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4985 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4986 INT regionCount, GpRegion** regions)
4990 HFONT gdifont, oldfont;
4991 struct measure_ranges_args args;
4992 HDC hdc, temp_hdc=NULL;
4997 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4998 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
5000 if (!(graphics && string && font && layoutRect && stringFormat && regions))
5001 return InvalidParameter;
5003 if (regionCount < stringFormat->range_count)
5004 return InvalidParameter;
5008 hdc = temp_hdc = CreateCompatibleDC(0);
5009 if (!temp_hdc) return OutOfMemory;
5012 hdc = graphics->hdc;
5014 if (stringFormat->attr)
5015 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
5023 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5024 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5025 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5026 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5027 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5029 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
5030 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5032 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
5033 scaled_rect.Y = layoutRect->Y * args.rel_height;
5034 if (stringFormat->attr & StringFormatFlagsNoClip)
5036 scaled_rect.Width = (REAL)(1 << 23);
5037 scaled_rect.Height = (REAL)(1 << 23);
5041 scaled_rect.Width = layoutRect->Width * args.rel_width;
5042 scaled_rect.Height = layoutRect->Height * args.rel_height;
5044 if (scaled_rect.Width >= 0.5)
5046 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5047 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5050 get_font_hfont(graphics, font, stringFormat, &gdifont);
5051 oldfont = SelectObject(hdc, gdifont);
5053 for (i=0; i<stringFormat->range_count; i++)
5055 stat = GdipSetEmpty(regions[i]);
5060 args.regions = regions;
5062 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
5063 measure_ranges_callback, &args);
5065 SelectObject(hdc, oldfont);
5066 DeleteObject(gdifont);
5074 struct measure_string_args {
5076 INT *codepointsfitted;
5078 REAL rel_width, rel_height;
5081 static GpStatus measure_string_callback(HDC hdc,
5082 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5083 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5084 INT lineno, const RectF *bounds, INT *underlined_indexes,
5085 INT underlined_index_count, void *user_data)
5087 struct measure_string_args *args = user_data;
5088 REAL new_width, new_height;
5090 new_width = bounds->Width / args->rel_width;
5091 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
5093 if (new_width > args->bounds->Width)
5094 args->bounds->Width = new_width;
5096 if (new_height > args->bounds->Height)
5097 args->bounds->Height = new_height;
5099 if (args->codepointsfitted)
5100 *args->codepointsfitted = index + length;
5102 if (args->linesfilled)
5103 (*args->linesfilled)++;
5108 /* Find the smallest rectangle that bounds the text when it is printed in rect
5109 * according to the format options listed in format. If rect has 0 width and
5110 * height, then just find the smallest rectangle that bounds the text when it's
5111 * printed at location (rect->X, rect-Y). */
5112 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
5113 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
5114 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
5115 INT *codepointsfitted, INT *linesfilled)
5117 HFONT oldfont, gdifont;
5118 struct measure_string_args args;
5119 HDC temp_hdc=NULL, hdc;
5123 INT lines, glyphs, format_flags = format ? format->attr : 0;
5125 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
5126 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
5127 bounds, codepointsfitted, linesfilled);
5129 if(!graphics || !string || !font || !rect || !bounds)
5130 return InvalidParameter;
5134 hdc = temp_hdc = CreateCompatibleDC(0);
5135 if (!temp_hdc) return OutOfMemory;
5138 hdc = graphics->hdc;
5140 if(linesfilled) *linesfilled = 0;
5141 if(codepointsfitted) *codepointsfitted = 0;
5144 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5152 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5153 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5154 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5155 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5156 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5158 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5159 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5161 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
5162 scaled_rect.Y = rect->Y * args.rel_height;
5163 scaled_rect.Width = rect->Width * args.rel_width;
5164 scaled_rect.Height = rect->Height * args.rel_height;
5166 if ((format_flags & StringFormatFlagsNoClip) ||
5167 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5168 if ((format_flags & StringFormatFlagsNoClip) ||
5169 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5171 if (scaled_rect.Width >= 0.5)
5173 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
5174 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5177 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5178 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5180 get_font_hfont(graphics, font, format, &gdifont);
5181 oldfont = SelectObject(hdc, gdifont);
5183 bounds->X = rect->X;
5184 bounds->Y = rect->Y;
5185 bounds->Width = 0.0;
5186 bounds->Height = 0.0;
5188 args.bounds = bounds;
5189 args.codepointsfitted = &glyphs;
5190 args.linesfilled = &lines;
5193 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5194 measure_string_callback, &args);
5196 if (linesfilled) *linesfilled = lines;
5197 if (codepointsfitted) *codepointsfitted = glyphs;
5200 bounds->Width += margin_x * 2.0;
5202 SelectObject(hdc, oldfont);
5203 DeleteObject(gdifont);
5211 struct draw_string_args {
5212 GpGraphics *graphics;
5213 GDIPCONST GpBrush *brush;
5214 REAL x, y, rel_width, rel_height, ascent;
5217 static GpStatus draw_string_callback(HDC hdc,
5218 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
5219 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
5220 INT lineno, const RectF *bounds, INT *underlined_indexes,
5221 INT underlined_index_count, void *user_data)
5223 struct draw_string_args *args = user_data;
5227 position.X = args->x + bounds->X / args->rel_width;
5228 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
5230 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
5231 args->brush, &position,
5232 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
5234 if (stat == Ok && underlined_index_count)
5236 OUTLINETEXTMETRICW otm;
5237 REAL underline_y, underline_height;
5240 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
5242 underline_height = otm.otmsUnderscoreSize / args->rel_height;
5243 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
5245 for (i=0; i<underlined_index_count; i++)
5247 REAL start_x, end_x;
5249 INT ofs = underlined_indexes[i] - index;
5251 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
5252 start_x = text_size.cx / args->rel_width;
5254 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
5255 end_x = text_size.cx / args->rel_width;
5257 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
5264 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
5265 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
5266 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
5270 GpPointF pt[3], rectcpy[4];
5272 REAL rel_width, rel_height, margin_x;
5273 INT save_state, format_flags = 0;
5275 struct draw_string_args args;
5277 HDC hdc, temp_hdc=NULL;
5278 TEXTMETRICW textmetric;
5280 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
5281 length, font, debugstr_rectf(rect), format, brush);
5283 if(!graphics || !string || !font || !brush || !rect)
5284 return InvalidParameter;
5288 hdc = graphics->hdc;
5292 hdc = temp_hdc = CreateCompatibleDC(0);
5296 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
5298 format_flags = format->attr;
5300 /* Should be no need to explicitly test for StringAlignmentNear as
5301 * that is default behavior if no alignment is passed. */
5302 if(format->vertalign != StringAlignmentNear){
5303 RectF bounds, in_rect = *rect;
5304 in_rect.Height = 0.0; /* avoid height clipping */
5305 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5307 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5309 if(format->vertalign == StringAlignmentCenter)
5310 offsety = (rect->Height - bounds.Height) / 2;
5311 else if(format->vertalign == StringAlignmentFar)
5312 offsety = (rect->Height - bounds.Height);
5314 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5317 save_state = SaveDC(hdc);
5325 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5326 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5327 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5328 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5329 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5331 rectcpy[3].X = rectcpy[0].X = rect->X;
5332 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5333 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5334 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5335 transform_and_round_points(graphics, corners, rectcpy, 4);
5337 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5338 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5340 scaled_rect.X = margin_x * rel_width;
5341 scaled_rect.Y = 0.0;
5342 scaled_rect.Width = rel_width * rect->Width;
5343 scaled_rect.Height = rel_height * rect->Height;
5345 if ((format_flags & StringFormatFlagsNoClip) ||
5346 scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5347 if ((format_flags & StringFormatFlagsNoClip) ||
5348 scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5350 if (scaled_rect.Width >= 0.5)
5352 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5353 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5356 if (scaled_rect.Width >= INT_MAX || scaled_rect.Width < 0.5) scaled_rect.Width = (REAL)(1 << 23);
5357 if (scaled_rect.Height >= INT_MAX || scaled_rect.Height < 0.5) scaled_rect.Height = (REAL)(1 << 23);
5359 if (!(format_flags & StringFormatFlagsNoClip) &&
5360 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23)
5362 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5363 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5364 SelectClipRgn(hdc, rgn);
5367 get_font_hfont(graphics, font, format, &gdifont);
5368 SelectObject(hdc, gdifont);
5370 args.graphics = graphics;
5374 args.y = rect->Y + offsety;
5376 args.rel_width = rel_width;
5377 args.rel_height = rel_height;
5379 GetTextMetricsW(hdc, &textmetric);
5380 args.ascent = textmetric.tmAscent / rel_height;
5382 gdip_format_string(hdc, string, length, font, &scaled_rect, format,
5383 draw_string_callback, &args);
5386 DeleteObject(gdifont);
5388 RestoreDC(hdc, save_state);
5395 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5397 TRACE("(%p)\n", graphics);
5400 return InvalidParameter;
5405 return GdipSetInfinite(graphics->clip);
5408 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5410 TRACE("(%p)\n", graphics);
5413 return InvalidParameter;
5418 graphics->worldtrans->matrix[0] = 1.0;
5419 graphics->worldtrans->matrix[1] = 0.0;
5420 graphics->worldtrans->matrix[2] = 0.0;
5421 graphics->worldtrans->matrix[3] = 1.0;
5422 graphics->worldtrans->matrix[4] = 0.0;
5423 graphics->worldtrans->matrix[5] = 0.0;
5428 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5430 return GdipEndContainer(graphics, state);
5433 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5434 GpMatrixOrder order)
5436 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5439 return InvalidParameter;
5444 return GdipRotateMatrix(graphics->worldtrans, angle, order);
5447 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5449 return GdipBeginContainer2(graphics, state);
5452 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5453 GraphicsContainer *state)
5455 GraphicsContainerItem *container;
5458 TRACE("(%p, %p)\n", graphics, state);
5460 if(!graphics || !state)
5461 return InvalidParameter;
5463 sts = init_container(&container, graphics);
5467 list_add_head(&graphics->containers, &container->entry);
5468 *state = graphics->contid = container->contid;
5473 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5475 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5476 return NotImplemented;
5479 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5481 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5482 return NotImplemented;
5485 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5487 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5488 return NotImplemented;
5491 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5494 GraphicsContainerItem *container, *container2;
5496 TRACE("(%p, %x)\n", graphics, state);
5499 return InvalidParameter;
5501 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5502 if(container->contid == state)
5506 /* did not find a matching container */
5507 if(&container->entry == &graphics->containers)
5510 sts = restore_container(graphics, container);
5514 /* remove all of the containers on top of the found container */
5515 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5516 if(container->contid == state)
5518 list_remove(&container->entry);
5519 delete_container(container);
5522 list_remove(&container->entry);
5523 delete_container(container);
5528 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5529 REAL sy, GpMatrixOrder order)
5531 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5534 return InvalidParameter;
5539 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
5542 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5545 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5547 if(!graphics || !srcgraphics)
5548 return InvalidParameter;
5550 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5553 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5554 CompositingMode mode)
5556 TRACE("(%p, %d)\n", graphics, mode);
5559 return InvalidParameter;
5564 graphics->compmode = mode;
5569 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5570 CompositingQuality quality)
5572 TRACE("(%p, %d)\n", graphics, quality);
5575 return InvalidParameter;
5580 graphics->compqual = quality;
5585 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5586 InterpolationMode mode)
5588 TRACE("(%p, %d)\n", graphics, mode);
5590 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5591 return InvalidParameter;
5596 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5597 mode = InterpolationModeBilinear;
5599 if (mode == InterpolationModeHighQuality)
5600 mode = InterpolationModeHighQualityBicubic;
5602 graphics->interpolation = mode;
5607 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5609 TRACE("(%p, %.2f)\n", graphics, scale);
5611 if(!graphics || (scale <= 0.0))
5612 return InvalidParameter;
5617 graphics->scale = scale;
5622 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5624 TRACE("(%p, %d)\n", graphics, unit);
5627 return InvalidParameter;
5632 if(unit == UnitWorld)
5633 return InvalidParameter;
5635 graphics->unit = unit;
5640 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5643 TRACE("(%p, %d)\n", graphics, mode);
5646 return InvalidParameter;
5651 graphics->pixeloffset = mode;
5656 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5660 TRACE("(%p,%i,%i)\n", graphics, x, y);
5663 FIXME("value is unused in rendering\n");
5666 return InvalidParameter;
5668 graphics->origin_x = x;
5669 graphics->origin_y = y;
5674 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5676 TRACE("(%p,%p,%p)\n", graphics, x, y);
5678 if (!graphics || !x || !y)
5679 return InvalidParameter;
5681 *x = graphics->origin_x;
5682 *y = graphics->origin_y;
5687 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5689 TRACE("(%p, %d)\n", graphics, mode);
5692 return InvalidParameter;
5697 graphics->smoothing = mode;
5702 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5704 TRACE("(%p, %d)\n", graphics, contrast);
5707 return InvalidParameter;
5709 graphics->textcontrast = contrast;
5714 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5715 TextRenderingHint hint)
5717 TRACE("(%p, %d)\n", graphics, hint);
5719 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5720 return InvalidParameter;
5725 graphics->texthint = hint;
5730 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5732 TRACE("(%p, %p)\n", graphics, matrix);
5734 if(!graphics || !matrix)
5735 return InvalidParameter;
5740 TRACE("%f,%f,%f,%f,%f,%f\n",
5741 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5742 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5744 GdipDeleteMatrix(graphics->worldtrans);
5745 return GdipCloneMatrix(matrix, &graphics->worldtrans);
5748 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5749 REAL dy, GpMatrixOrder order)
5751 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5754 return InvalidParameter;
5759 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
5762 /*****************************************************************************
5763 * GdipSetClipHrgn [GDIPLUS.@]
5765 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5770 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5773 return InvalidParameter;
5775 status = GdipCreateRegionHrgn(hrgn, ®ion);
5779 status = GdipSetClipRegion(graphics, region, mode);
5781 GdipDeleteRegion(region);
5785 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5787 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5790 return InvalidParameter;
5795 return GdipCombineRegionPath(graphics->clip, path, mode);
5798 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5799 REAL width, REAL height,
5804 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5807 return InvalidParameter;
5815 rect.Height = height;
5817 return GdipCombineRegionRect(graphics->clip, &rect, mode);
5820 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5821 INT width, INT height,
5824 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5827 return InvalidParameter;
5832 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5835 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5838 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5840 if(!graphics || !region)
5841 return InvalidParameter;
5846 return GdipCombineRegionRegion(graphics->clip, region, mode);
5849 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5854 TRACE("(%p,%u)\n", metafile, limitDpi);
5857 FIXME("not implemented\n");
5859 return NotImplemented;
5862 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5868 TRACE("(%p, %p, %d)\n", graphics, points, count);
5870 if(!graphics || !pen || count<=0)
5871 return InvalidParameter;
5878 FIXME("graphics object has no HDC\n");
5882 pti = GdipAlloc(sizeof(POINT) * count);
5884 save_state = prepare_dc(graphics, pen);
5885 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5887 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5888 Polygon(graphics->hdc, pti, count);
5890 restore_dc(graphics, save_state);
5896 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5903 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5905 if(count<=0) return InvalidParameter;
5906 ptf = GdipAlloc(sizeof(GpPointF) * count);
5908 for(i = 0;i < count; i++){
5909 ptf[i].X = (REAL)points[i].X;
5910 ptf[i].Y = (REAL)points[i].Y;
5913 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5919 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5921 TRACE("(%p, %p)\n", graphics, dpi);
5923 if(!graphics || !dpi)
5924 return InvalidParameter;
5929 *dpi = graphics->xres;
5933 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5935 TRACE("(%p, %p)\n", graphics, dpi);
5937 if(!graphics || !dpi)
5938 return InvalidParameter;
5943 *dpi = graphics->yres;
5947 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5948 GpMatrixOrder order)
5953 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5955 if(!graphics || !matrix)
5956 return InvalidParameter;
5961 m = *(graphics->worldtrans);
5963 ret = GdipMultiplyMatrix(&m, matrix, order);
5965 *(graphics->worldtrans) = m;
5970 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5971 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5973 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5977 TRACE("(%p, %p)\n", graphics, hdc);
5979 if(!graphics || !hdc)
5980 return InvalidParameter;
5985 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5987 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5989 else if (!graphics->hdc ||
5990 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5992 /* Create a fake HDC and fill it with a constant color. */
5996 BITMAPINFOHEADER bmih;
5999 stat = get_graphics_bounds(graphics, &bounds);
6003 graphics->temp_hbitmap_width = bounds.Width;
6004 graphics->temp_hbitmap_height = bounds.Height;
6006 bmih.biSize = sizeof(bmih);
6007 bmih.biWidth = graphics->temp_hbitmap_width;
6008 bmih.biHeight = -graphics->temp_hbitmap_height;
6010 bmih.biBitCount = 32;
6011 bmih.biCompression = BI_RGB;
6012 bmih.biSizeImage = 0;
6013 bmih.biXPelsPerMeter = 0;
6014 bmih.biYPelsPerMeter = 0;
6016 bmih.biClrImportant = 0;
6018 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
6019 (void**)&graphics->temp_bits, NULL, 0);
6021 return GenericError;
6023 temp_hdc = CreateCompatibleDC(0);
6026 DeleteObject(hbitmap);
6027 return GenericError;
6030 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6031 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
6033 SelectObject(temp_hdc, hbitmap);
6035 graphics->temp_hbitmap = hbitmap;
6036 *hdc = graphics->temp_hdc = temp_hdc;
6040 *hdc = graphics->hdc;
6044 graphics->busy = TRUE;
6049 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
6053 TRACE("(%p, %p)\n", graphics, hdc);
6055 if(!graphics || !hdc || !graphics->busy)
6056 return InvalidParameter;
6058 if (graphics->image && graphics->image->type == ImageTypeMetafile)
6060 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
6062 else if (graphics->temp_hdc == hdc)
6067 /* Find the pixels that have changed, and mark them as opaque. */
6068 pos = (DWORD*)graphics->temp_bits;
6069 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
6071 if (*pos != DC_BACKGROUND_KEY)
6078 /* Write the changed pixels to the real target. */
6079 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
6080 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
6081 graphics->temp_hbitmap_width * 4);
6084 DeleteDC(graphics->temp_hdc);
6085 DeleteObject(graphics->temp_hbitmap);
6086 graphics->temp_hdc = NULL;
6087 graphics->temp_hbitmap = NULL;
6089 else if (hdc != graphics->hdc)
6091 stat = InvalidParameter;
6095 graphics->busy = FALSE;
6100 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
6105 TRACE("(%p, %p)\n", graphics, region);
6107 if(!graphics || !region)
6108 return InvalidParameter;
6113 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
6116 /* free everything except root node and header */
6117 delete_element(®ion->node);
6118 memcpy(region, clip, sizeof(GpRegion));
6124 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
6125 GpCoordinateSpace src_space, GpMatrix **matrix)
6127 GpStatus stat = GdipCreateMatrix(matrix);
6128 REAL scale_x, scale_y;
6130 if (dst_space != src_space && stat == Ok)
6132 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
6133 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
6135 if(graphics->unit != UnitDisplay)
6137 scale_x *= graphics->scale;
6138 scale_y *= graphics->scale;
6141 /* transform from src_space to CoordinateSpacePage */
6144 case CoordinateSpaceWorld:
6145 GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
6147 case CoordinateSpacePage:
6149 case CoordinateSpaceDevice:
6150 GdipScaleMatrix(*matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
6154 /* transform from CoordinateSpacePage to dst_space */
6157 case CoordinateSpaceWorld:
6159 GpMatrix *inverted_transform;
6160 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
6163 stat = GdipInvertMatrix(inverted_transform);
6165 GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
6166 GdipDeleteMatrix(inverted_transform);
6170 case CoordinateSpacePage:
6172 case CoordinateSpaceDevice:
6173 GdipScaleMatrix(*matrix, scale_x, scale_y, MatrixOrderAppend);
6180 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
6181 GpCoordinateSpace src_space, GpPointF *points, INT count)
6186 if(!graphics || !points || count <= 0)
6187 return InvalidParameter;
6192 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6194 if (src_space == dst_space) return Ok;
6196 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
6200 stat = GdipTransformMatrixPoints(matrix, points, count);
6202 GdipDeleteMatrix(matrix);
6208 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
6209 GpCoordinateSpace src_space, GpPoint *points, INT count)
6215 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
6218 return InvalidParameter;
6220 pointsF = GdipAlloc(sizeof(GpPointF) * count);
6224 for(i = 0; i < count; i++){
6225 pointsF[i].X = (REAL)points[i].X;
6226 pointsF[i].Y = (REAL)points[i].Y;
6229 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
6232 for(i = 0; i < count; i++){
6233 points[i].X = gdip_round(pointsF[i].X);
6234 points[i].Y = gdip_round(pointsF[i].Y);
6241 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
6253 /*****************************************************************************
6254 * GdipTranslateClip [GDIPLUS.@]
6256 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
6258 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
6261 return InvalidParameter;
6266 return GdipTranslateRegion(graphics->clip, dx, dy);
6269 /*****************************************************************************
6270 * GdipTranslateClipI [GDIPLUS.@]
6272 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6274 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6277 return InvalidParameter;
6282 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6286 /*****************************************************************************
6287 * GdipMeasureDriverString [GDIPLUS.@]
6289 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6290 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6291 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6293 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6296 REAL min_x, min_y, max_x, max_y, x, y;
6298 TEXTMETRICW textmetric;
6299 const WORD *glyph_indices;
6300 WORD *dynamic_glyph_indices=NULL;
6301 REAL rel_width, rel_height, ascent, descent;
6304 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6306 if (!graphics || !text || !font || !positions || !boundingBox)
6307 return InvalidParameter;
6310 length = strlenW(text);
6314 boundingBox->X = 0.0;
6315 boundingBox->Y = 0.0;
6316 boundingBox->Width = 0.0;
6317 boundingBox->Height = 0.0;
6320 if (flags & unsupported_flags)
6321 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6324 FIXME("Ignoring matrix\n");
6326 get_font_hfont(graphics, font, NULL, &hfont);
6328 hdc = CreateCompatibleDC(0);
6329 SelectObject(hdc, hfont);
6331 GetTextMetricsW(hdc, &textmetric);
6339 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6340 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6341 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6342 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6343 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6345 if (flags & DriverStringOptionsCmapLookup)
6347 glyph_indices = dynamic_glyph_indices = GdipAlloc(sizeof(WORD) * length);
6351 DeleteObject(hfont);
6355 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6358 glyph_indices = text;
6360 min_x = max_x = x = positions[0].X;
6361 min_y = max_y = y = positions[0].Y;
6363 ascent = textmetric.tmAscent / rel_height;
6364 descent = textmetric.tmDescent / rel_height;
6366 for (i=0; i<length; i++)
6371 if (!(flags & DriverStringOptionsRealizedAdvance))
6377 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6378 char_width = abc.abcA + abc.abcB + abc.abcC;
6380 if (min_y > y - ascent) min_y = y - ascent;
6381 if (max_y < y + descent) max_y = y + descent;
6382 if (min_x > x) min_x = x;
6384 x += char_width / rel_width;
6386 if (max_x < x) max_x = x;
6389 GdipFree(dynamic_glyph_indices);
6391 DeleteObject(hfont);
6393 boundingBox->X = min_x;
6394 boundingBox->Y = min_y;
6395 boundingBox->Width = max_x - min_x;
6396 boundingBox->Height = max_y - min_y;
6401 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6402 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6403 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6404 INT flags, GDIPCONST GpMatrix *matrix)
6406 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6412 if (flags & unsupported_flags)
6413 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6416 FIXME("Ignoring matrix\n");
6418 if (!(flags & DriverStringOptionsCmapLookup))
6419 eto_flags |= ETO_GLYPH_INDEX;
6421 save_state = SaveDC(graphics->hdc);
6422 SetBkMode(graphics->hdc, TRANSPARENT);
6423 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6426 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6428 get_font_hfont(graphics, font, format, &hfont);
6429 SelectObject(graphics->hdc, hfont);
6431 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6433 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6435 RestoreDC(graphics->hdc, save_state);
6437 DeleteObject(hfont);
6442 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6443 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6444 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6445 INT flags, GDIPCONST GpMatrix *matrix)
6447 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6449 PointF *real_positions, real_position;
6453 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6454 DWORD max_glyphsize=0;
6455 GLYPHMETRICS glyphmetrics;
6456 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6459 int text_mask_stride;
6461 int pixel_data_stride;
6463 UINT ggo_flags = GGO_GRAY8_BITMAP;
6468 if (!(flags & DriverStringOptionsCmapLookup))
6469 ggo_flags |= GGO_GLYPH_INDEX;
6471 if (flags & unsupported_flags)
6472 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6475 FIXME("Ignoring matrix\n");
6477 pti = GdipAlloc(sizeof(POINT) * length);
6481 if (flags & DriverStringOptionsRealizedAdvance)
6483 real_position = positions[0];
6485 transform_and_round_points(graphics, pti, &real_position, 1);
6489 real_positions = GdipAlloc(sizeof(PointF) * length);
6490 if (!real_positions)
6496 memcpy(real_positions, positions, sizeof(PointF) * length);
6498 transform_and_round_points(graphics, pti, real_positions, length);
6500 GdipFree(real_positions);
6503 get_font_hfont(graphics, font, format, &hfont);
6505 hdc = CreateCompatibleDC(0);
6506 SelectObject(hdc, hfont);
6508 /* Get the boundaries of the text to be drawn */
6509 for (i=0; i<length; i++)
6512 int left, top, right, bottom;
6514 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6515 &glyphmetrics, 0, NULL, &identity);
6517 if (glyphsize == GDI_ERROR)
6519 ERR("GetGlyphOutlineW failed\n");
6522 DeleteObject(hfont);
6523 return GenericError;
6526 if (glyphsize > max_glyphsize)
6527 max_glyphsize = glyphsize;
6529 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6530 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6531 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6532 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6534 if (left < min_x) min_x = left;
6535 if (top < min_y) min_y = top;
6536 if (right > max_x) max_x = right;
6537 if (bottom > max_y) max_y = bottom;
6539 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6541 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6542 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6546 glyph_mask = GdipAlloc(max_glyphsize);
6547 text_mask = GdipAlloc((max_x - min_x) * (max_y - min_y));
6548 text_mask_stride = max_x - min_x;
6550 if (!(glyph_mask && text_mask))
6552 GdipFree(glyph_mask);
6553 GdipFree(text_mask);
6556 DeleteObject(hfont);
6560 /* Generate a mask for the text */
6561 for (i=0; i<length; i++)
6563 int left, top, stride;
6565 GetGlyphOutlineW(hdc, text[i], ggo_flags,
6566 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6568 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6569 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6570 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6572 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6574 BYTE *glyph_val = glyph_mask + y * stride;
6575 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6576 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6578 *text_val = min(64, *text_val + *glyph_val);
6587 DeleteObject(hfont);
6588 GdipFree(glyph_mask);
6590 /* get the brush data */
6591 pixel_data = GdipAlloc(4 * (max_x - min_x) * (max_y - min_y));
6594 GdipFree(text_mask);
6598 pixel_area.X = min_x;
6599 pixel_area.Y = min_y;
6600 pixel_area.Width = max_x - min_x;
6601 pixel_area.Height = max_y - min_y;
6602 pixel_data_stride = pixel_area.Width * 4;
6604 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6607 GdipFree(text_mask);
6608 GdipFree(pixel_data);
6612 /* multiply the brush data by the mask */
6613 for (y=0; y<pixel_area.Height; y++)
6615 BYTE *text_val = text_mask + text_mask_stride * y;
6616 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6617 for (x=0; x<pixel_area.Width; x++)
6619 *pixel_val = (*pixel_val) * (*text_val) / 64;
6625 GdipFree(text_mask);
6627 /* draw the result */
6628 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6629 pixel_area.Height, pixel_data_stride);
6631 GdipFree(pixel_data);
6636 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6637 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6638 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6639 INT flags, GDIPCONST GpMatrix *matrix)
6641 GpStatus stat = NotImplemented;
6644 length = strlenW(text);
6646 if (graphics->hdc &&
6647 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6648 brush->bt == BrushTypeSolidColor &&
6649 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6650 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6651 brush, positions, flags, matrix);
6652 if (stat == NotImplemented)
6653 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6654 brush, positions, flags, matrix);
6658 /*****************************************************************************
6659 * GdipDrawDriverString [GDIPLUS.@]
6661 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6662 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6663 GDIPCONST PointF *positions, INT flags,
6664 GDIPCONST GpMatrix *matrix )
6666 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6668 if (!graphics || !text || !font || !brush || !positions)
6669 return InvalidParameter;
6671 return draw_driver_string(graphics, text, length, font, NULL,
6672 brush, positions, flags, matrix);
6675 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
6676 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
6678 FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
6679 return NotImplemented;
6682 /*****************************************************************************
6683 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6685 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6690 TRACE("(%p, %p)\n", graphics, res);
6692 if((stat = GdipCreateRegion(&rgn)) != Ok)
6695 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6698 stat = GdipIsEmptyRegion(rgn, graphics, res);
6701 GdipDeleteRegion(rgn);
6705 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6709 TRACE("(%p) stub\n", graphics);
6712 FIXME("not implemented\n");
6714 return NotImplemented;