[GDIPLUS] Sync with Wine Staging 1.9.11. CORE-11368
[reactos.git] / reactos / dll / win32 / gdiplus / graphics.c
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "gdiplus_private.h"
20
21 #include <winreg.h>
22 #include <shlwapi.h>
23
24 /* Mike "tamlin" Nordell 2012-09-14 for ReactOS:
25 * NOTE: Wine uses per-GpGraphics id's ('contid' starting from zero in
26 * every GpGraphics). Windows seems to use process-global id's, or at
27 * least more unique id's.
28 * This have the following implications. It:
29 * 1. fails the current gdiplus test case.
30 * 2. is not what Windows does.
31 *
32 * We therefore "obfuscate" the 'contid' a little to more match Windows'
33 * behaviour. The observable behviour should still remain the same,
34 * except for handing out more "unique" id's.
35 */
36 #define GDIP_CONTID_STEP 64
37 static volatile LONG g_priv_contid = GDIP_CONTID_STEP;
38 #define GDIP_GET_NEW_CONTID_FOR(pGpGraphics) \
39 (UINT)(InterlockedExchangeAdd(&g_priv_contid,GDIP_CONTID_STEP))
40
41 /* looks-right constants */
42 #define ANCHOR_WIDTH (2.0)
43 #define MAX_ITERS (50)
44
45 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
46 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
47 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
48 INT flags, GDIPCONST GpMatrix *matrix);
49
50 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
51 GpCoordinateSpace src_space, GpMatrix *matrix);
52
53 /* Converts from gdiplus path point type to gdi path point type. */
54 static BYTE convert_path_point_type(BYTE type)
55 {
56 BYTE ret;
57
58 switch(type & PathPointTypePathTypeMask){
59 case PathPointTypeBezier:
60 ret = PT_BEZIERTO;
61 break;
62 case PathPointTypeLine:
63 ret = PT_LINETO;
64 break;
65 case PathPointTypeStart:
66 ret = PT_MOVETO;
67 break;
68 default:
69 ERR("Bad point type\n");
70 return 0;
71 }
72
73 if(type & PathPointTypeCloseSubpath)
74 ret |= PT_CLOSEFIGURE;
75
76 return ret;
77 }
78
79 static COLORREF get_gdi_brush_color(const GpBrush *brush)
80 {
81 ARGB argb;
82
83 switch (brush->bt)
84 {
85 case BrushTypeSolidColor:
86 {
87 const GpSolidFill *sf = (const GpSolidFill *)brush;
88 argb = sf->color;
89 break;
90 }
91 case BrushTypeHatchFill:
92 {
93 const GpHatch *hatch = (const GpHatch *)brush;
94 argb = hatch->forecol;
95 break;
96 }
97 case BrushTypeLinearGradient:
98 {
99 const GpLineGradient *line = (const GpLineGradient *)brush;
100 argb = line->startcolor;
101 break;
102 }
103 case BrushTypePathGradient:
104 {
105 const GpPathGradient *grad = (const GpPathGradient *)brush;
106 argb = grad->centercolor;
107 break;
108 }
109 default:
110 FIXME("unhandled brush type %d\n", brush->bt);
111 argb = 0;
112 break;
113 }
114 return ARGB2COLORREF(argb);
115 }
116
117 static HBITMAP create_hatch_bitmap(const GpHatch *hatch)
118 {
119 HBITMAP hbmp;
120 BITMAPINFOHEADER bmih;
121 DWORD *bits;
122 int x, y;
123
124 bmih.biSize = sizeof(bmih);
125 bmih.biWidth = 8;
126 bmih.biHeight = 8;
127 bmih.biPlanes = 1;
128 bmih.biBitCount = 32;
129 bmih.biCompression = BI_RGB;
130 bmih.biSizeImage = 0;
131
132 hbmp = CreateDIBSection(0, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&bits, NULL, 0);
133 if (hbmp)
134 {
135 const char *hatch_data;
136
137 if (get_hatch_data(hatch->hatchstyle, &hatch_data) == Ok)
138 {
139 for (y = 0; y < 8; y++)
140 {
141 for (x = 0; x < 8; x++)
142 {
143 if (hatch_data[y] & (0x80 >> x))
144 bits[y * 8 + x] = hatch->forecol;
145 else
146 bits[y * 8 + x] = hatch->backcol;
147 }
148 }
149 }
150 else
151 {
152 FIXME("Unimplemented hatch style %d\n", hatch->hatchstyle);
153
154 for (y = 0; y < 64; y++)
155 bits[y] = hatch->forecol;
156 }
157 }
158
159 return hbmp;
160 }
161
162 static GpStatus create_gdi_logbrush(const GpBrush *brush, LOGBRUSH *lb)
163 {
164 switch (brush->bt)
165 {
166 case BrushTypeSolidColor:
167 {
168 const GpSolidFill *sf = (const GpSolidFill *)brush;
169 lb->lbStyle = BS_SOLID;
170 lb->lbColor = ARGB2COLORREF(sf->color);
171 lb->lbHatch = 0;
172 return Ok;
173 }
174
175 case BrushTypeHatchFill:
176 {
177 const GpHatch *hatch = (const GpHatch *)brush;
178 HBITMAP hbmp;
179
180 hbmp = create_hatch_bitmap(hatch);
181 if (!hbmp) return OutOfMemory;
182
183 lb->lbStyle = BS_PATTERN;
184 lb->lbColor = 0;
185 lb->lbHatch = (ULONG_PTR)hbmp;
186 return Ok;
187 }
188
189 default:
190 FIXME("unhandled brush type %d\n", brush->bt);
191 lb->lbStyle = BS_SOLID;
192 lb->lbColor = get_gdi_brush_color(brush);
193 lb->lbHatch = 0;
194 return Ok;
195 }
196 }
197
198 static GpStatus free_gdi_logbrush(LOGBRUSH *lb)
199 {
200 switch (lb->lbStyle)
201 {
202 case BS_PATTERN:
203 DeleteObject((HGDIOBJ)(ULONG_PTR)lb->lbHatch);
204 break;
205 }
206 return Ok;
207 }
208
209 static HBRUSH create_gdi_brush(const GpBrush *brush)
210 {
211 LOGBRUSH lb;
212 HBRUSH gdibrush;
213
214 if (create_gdi_logbrush(brush, &lb) != Ok) return 0;
215
216 gdibrush = CreateBrushIndirect(&lb);
217 free_gdi_logbrush(&lb);
218
219 return gdibrush;
220 }
221
222 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
223 {
224 LOGBRUSH lb;
225 HPEN gdipen;
226 REAL width;
227 INT save_state, i, numdashes;
228 GpPointF pt[2];
229 DWORD dash_array[MAX_DASHLEN];
230
231 save_state = SaveDC(graphics->hdc);
232
233 EndPath(graphics->hdc);
234
235 if(pen->unit == UnitPixel){
236 width = pen->width;
237 }
238 else{
239 /* Get an estimate for the amount the pen width is affected by the world
240 * transform. (This is similar to what some of the wine drivers do.) */
241 pt[0].X = 0.0;
242 pt[0].Y = 0.0;
243 pt[1].X = 1.0;
244 pt[1].Y = 1.0;
245 GdipTransformMatrixPoints(&graphics->worldtrans, pt, 2);
246 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
247 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
248
249 width *= units_to_pixels(pen->width, pen->unit == UnitWorld ? graphics->unit : pen->unit, graphics->xres);
250 width *= graphics->scale;
251 }
252
253 if(pen->dash == DashStyleCustom){
254 numdashes = min(pen->numdashes, MAX_DASHLEN);
255
256 TRACE("dashes are: ");
257 for(i = 0; i < numdashes; i++){
258 dash_array[i] = gdip_round(width * pen->dashes[i]);
259 TRACE("%d, ", dash_array[i]);
260 }
261 TRACE("\n and the pen style is %x\n", pen->style);
262
263 create_gdi_logbrush(pen->brush, &lb);
264 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb,
265 numdashes, dash_array);
266 free_gdi_logbrush(&lb);
267 }
268 else
269 {
270 create_gdi_logbrush(pen->brush, &lb);
271 gdipen = ExtCreatePen(pen->style, gdip_round(width), &lb, 0, NULL);
272 free_gdi_logbrush(&lb);
273 }
274
275 SelectObject(graphics->hdc, gdipen);
276
277 return save_state;
278 }
279
280 static void restore_dc(GpGraphics *graphics, INT state)
281 {
282 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
283 RestoreDC(graphics->hdc, state);
284 }
285
286 /* This helper applies all the changes that the points listed in ptf need in
287 * order to be drawn on the device context. In the end, this should include at
288 * least:
289 * -scaling by page unit
290 * -applying world transformation
291 * -converting from float to int
292 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
293 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
294 * gdi to draw, and these functions would irreparably mess with line widths.
295 */
296 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
297 GpPointF *ptf, INT count)
298 {
299 REAL scale_x, scale_y;
300 GpMatrix matrix;
301 int i;
302
303 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
304 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
305
306 /* apply page scale */
307 if(graphics->unit != UnitDisplay)
308 {
309 scale_x *= graphics->scale;
310 scale_y *= graphics->scale;
311 }
312
313 matrix = graphics->worldtrans;
314 GdipScaleMatrix(&matrix, scale_x, scale_y, MatrixOrderAppend);
315 GdipTransformMatrixPoints(&matrix, ptf, count);
316
317 for(i = 0; i < count; i++){
318 pti[i].x = gdip_round(ptf[i].X);
319 pti[i].y = gdip_round(ptf[i].Y);
320 }
321 }
322
323 static void gdi_alpha_blend(GpGraphics *graphics, INT dst_x, INT dst_y, INT dst_width, INT dst_height,
324 HDC hdc, INT src_x, INT src_y, INT src_width, INT src_height)
325 {
326 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE)
327 {
328 TRACE("alpha blending not supported by device, fallback to StretchBlt\n");
329
330 StretchBlt(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
331 hdc, src_x, src_y, src_width, src_height, SRCCOPY);
332 }
333 else
334 {
335 BLENDFUNCTION bf;
336
337 bf.BlendOp = AC_SRC_OVER;
338 bf.BlendFlags = 0;
339 bf.SourceConstantAlpha = 255;
340 bf.AlphaFormat = AC_SRC_ALPHA;
341
342 GdiAlphaBlend(graphics->hdc, dst_x, dst_y, dst_width, dst_height,
343 hdc, src_x, src_y, src_width, src_height, bf);
344 }
345 }
346
347 static GpStatus get_clip_hrgn(GpGraphics *graphics, HRGN *hrgn)
348 {
349 /* clipping region is in device coords */
350 return GdipGetRegionHRgn(graphics->clip, NULL, hrgn);
351 }
352
353 /* Draw ARGB data to the given graphics object */
354 static GpStatus alpha_blend_bmp_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
355 const BYTE *src, INT src_width, INT src_height, INT src_stride, const PixelFormat fmt)
356 {
357 GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
358 INT x, y;
359
360 for (y=0; y<src_height; y++)
361 {
362 for (x=0; x<src_width; x++)
363 {
364 ARGB dst_color, src_color;
365 src_color = ((ARGB*)(src + src_stride * y))[x];
366
367 if (!(src_color & 0xff000000))
368 continue;
369
370 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
371 if (fmt & PixelFormatPAlpha)
372 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over_fgpremult(dst_color, src_color));
373 else
374 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
375 }
376 }
377
378 return Ok;
379 }
380
381 static GpStatus alpha_blend_hdc_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
382 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
383 {
384 HDC hdc;
385 HBITMAP hbitmap;
386 BITMAPINFOHEADER bih;
387 BYTE *temp_bits;
388
389 hdc = CreateCompatibleDC(0);
390
391 bih.biSize = sizeof(BITMAPINFOHEADER);
392 bih.biWidth = src_width;
393 bih.biHeight = -src_height;
394 bih.biPlanes = 1;
395 bih.biBitCount = 32;
396 bih.biCompression = BI_RGB;
397 bih.biSizeImage = 0;
398 bih.biXPelsPerMeter = 0;
399 bih.biYPelsPerMeter = 0;
400 bih.biClrUsed = 0;
401 bih.biClrImportant = 0;
402
403 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
404 (void**)&temp_bits, NULL, 0);
405
406 if (GetDeviceCaps(graphics->hdc, SHADEBLENDCAPS) == SB_NONE ||
407 fmt & PixelFormatPAlpha)
408 memcpy(temp_bits, src, src_width * src_height * 4);
409 else
410 convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
411 4 * src_width, src, src_stride);
412
413 SelectObject(hdc, hbitmap);
414 gdi_alpha_blend(graphics, dst_x, dst_y, src_width, src_height,
415 hdc, 0, 0, src_width, src_height);
416 DeleteDC(hdc);
417 DeleteObject(hbitmap);
418
419 return Ok;
420 }
421
422 static GpStatus alpha_blend_pixels_hrgn(GpGraphics *graphics, INT dst_x, INT dst_y,
423 const BYTE *src, INT src_width, INT src_height, INT src_stride, HRGN hregion, PixelFormat fmt)
424 {
425 GpStatus stat=Ok;
426
427 if (graphics->image && graphics->image->type == ImageTypeBitmap)
428 {
429 DWORD i;
430 int size;
431 RGNDATA *rgndata;
432 RECT *rects;
433 HRGN hrgn, visible_rgn;
434
435 hrgn = CreateRectRgn(dst_x, dst_y, dst_x + src_width, dst_y + src_height);
436 if (!hrgn)
437 return OutOfMemory;
438
439 stat = get_clip_hrgn(graphics, &visible_rgn);
440 if (stat != Ok)
441 {
442 DeleteObject(hrgn);
443 return stat;
444 }
445
446 if (visible_rgn)
447 {
448 CombineRgn(hrgn, hrgn, visible_rgn, RGN_AND);
449 DeleteObject(visible_rgn);
450 }
451
452 if (hregion)
453 CombineRgn(hrgn, hrgn, hregion, RGN_AND);
454
455 size = GetRegionData(hrgn, 0, NULL);
456
457 rgndata = heap_alloc_zero(size);
458 if (!rgndata)
459 {
460 DeleteObject(hrgn);
461 return OutOfMemory;
462 }
463
464 GetRegionData(hrgn, size, rgndata);
465
466 rects = (RECT*)rgndata->Buffer;
467
468 for (i=0; stat == Ok && i<rgndata->rdh.nCount; i++)
469 {
470 stat = alpha_blend_bmp_pixels(graphics, rects[i].left, rects[i].top,
471 &src[(rects[i].left - dst_x) * 4 + (rects[i].top - dst_y) * src_stride],
472 rects[i].right - rects[i].left, rects[i].bottom - rects[i].top,
473 src_stride, fmt);
474 }
475
476 heap_free(rgndata);
477
478 DeleteObject(hrgn);
479
480 return stat;
481 }
482 else if (graphics->image && graphics->image->type == ImageTypeMetafile)
483 {
484 ERR("This should not be used for metafiles; fix caller\n");
485 return NotImplemented;
486 }
487 else
488 {
489 HRGN hrgn;
490 int save;
491
492 stat = get_clip_hrgn(graphics, &hrgn);
493
494 if (stat != Ok)
495 return stat;
496
497 save = SaveDC(graphics->hdc);
498
499 if (hrgn)
500 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
501
502 if (hregion)
503 ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
504
505 stat = alpha_blend_hdc_pixels(graphics, dst_x, dst_y, src, src_width,
506 src_height, src_stride, fmt);
507
508 RestoreDC(graphics->hdc, save);
509
510 DeleteObject(hrgn);
511
512 return stat;
513 }
514 }
515
516 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
517 const BYTE *src, INT src_width, INT src_height, INT src_stride, PixelFormat fmt)
518 {
519 return alpha_blend_pixels_hrgn(graphics, dst_x, dst_y, src, src_width, src_height, src_stride, NULL, fmt);
520 }
521
522 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
523 {
524 INT start_a, end_a, final_a;
525 INT pos;
526
527 pos = gdip_round(position * 0xff);
528
529 start_a = ((start >> 24) & 0xff) * (pos ^ 0xff);
530 end_a = ((end >> 24) & 0xff) * pos;
531
532 final_a = start_a + end_a;
533
534 if (final_a < 0xff) return 0;
535
536 return (final_a / 0xff) << 24 |
537 ((((start >> 16) & 0xff) * start_a + (((end >> 16) & 0xff) * end_a)) / final_a) << 16 |
538 ((((start >> 8) & 0xff) * start_a + (((end >> 8) & 0xff) * end_a)) / final_a) << 8 |
539 (((start & 0xff) * start_a + ((end & 0xff) * end_a)) / final_a);
540 }
541
542 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
543 {
544 REAL blendfac;
545
546 /* clamp to between 0.0 and 1.0, using the wrap mode */
547 if (brush->wrap == WrapModeTile)
548 {
549 position = fmodf(position, 1.0f);
550 if (position < 0.0f) position += 1.0f;
551 }
552 else /* WrapModeFlip* */
553 {
554 position = fmodf(position, 2.0f);
555 if (position < 0.0f) position += 2.0f;
556 if (position > 1.0f) position = 2.0f - position;
557 }
558
559 if (brush->blendcount == 1)
560 blendfac = position;
561 else
562 {
563 int i=1;
564 REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
565 REAL range;
566
567 /* locate the blend positions surrounding this position */
568 while (position > brush->blendpos[i])
569 i++;
570
571 /* interpolate between the blend positions */
572 left_blendpos = brush->blendpos[i-1];
573 left_blendfac = brush->blendfac[i-1];
574 right_blendpos = brush->blendpos[i];
575 right_blendfac = brush->blendfac[i];
576 range = right_blendpos - left_blendpos;
577 blendfac = (left_blendfac * (right_blendpos - position) +
578 right_blendfac * (position - left_blendpos)) / range;
579 }
580
581 if (brush->pblendcount == 0)
582 return blend_colors(brush->startcolor, brush->endcolor, blendfac);
583 else
584 {
585 int i=1;
586 ARGB left_blendcolor, right_blendcolor;
587 REAL left_blendpos, right_blendpos;
588
589 /* locate the blend colors surrounding this position */
590 while (blendfac > brush->pblendpos[i])
591 i++;
592
593 /* interpolate between the blend colors */
594 left_blendpos = brush->pblendpos[i-1];
595 left_blendcolor = brush->pblendcolor[i-1];
596 right_blendpos = brush->pblendpos[i];
597 right_blendcolor = brush->pblendcolor[i];
598 blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
599 return blend_colors(left_blendcolor, right_blendcolor, blendfac);
600 }
601 }
602
603 static BOOL round_color_matrix(const ColorMatrix *matrix, int values[5][5])
604 {
605 /* Convert floating point color matrix to int[5][5], return TRUE if it's an identity */
606 BOOL identity = TRUE;
607 int i, j;
608
609 for (i=0; i<4; i++)
610 for (j=0; j<5; j++)
611 {
612 if (matrix->m[j][i] != (i == j ? 1.0 : 0.0))
613 identity = FALSE;
614 values[j][i] = gdip_round(matrix->m[j][i] * 256.0);
615 }
616
617 return identity;
618 }
619
620 static ARGB transform_color(ARGB color, int matrix[5][5])
621 {
622 int val[5], res[4];
623 int i, j;
624 unsigned char a, r, g, b;
625
626 val[0] = ((color >> 16) & 0xff); /* red */
627 val[1] = ((color >> 8) & 0xff); /* green */
628 val[2] = (color & 0xff); /* blue */
629 val[3] = ((color >> 24) & 0xff); /* alpha */
630 val[4] = 255; /* translation */
631
632 for (i=0; i<4; i++)
633 {
634 res[i] = 0;
635
636 for (j=0; j<5; j++)
637 res[i] += matrix[j][i] * val[j];
638 }
639
640 a = min(max(res[3] / 256, 0), 255);
641 r = min(max(res[0] / 256, 0), 255);
642 g = min(max(res[1] / 256, 0), 255);
643 b = min(max(res[2] / 256, 0), 255);
644
645 return (a << 24) | (r << 16) | (g << 8) | b;
646 }
647
648 static BOOL color_is_gray(ARGB color)
649 {
650 unsigned char r, g, b;
651
652 r = (color >> 16) & 0xff;
653 g = (color >> 8) & 0xff;
654 b = color & 0xff;
655
656 return (r == g) && (g == b);
657 }
658
659 /* returns preferred pixel format for the applied attributes */
660 static PixelFormat apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
661 UINT width, UINT height, INT stride, ColorAdjustType type, PixelFormat fmt)
662 {
663 UINT x, y;
664 INT i;
665
666 if (attributes->colorkeys[type].enabled ||
667 attributes->colorkeys[ColorAdjustTypeDefault].enabled)
668 {
669 const struct color_key *key;
670 BYTE min_blue, min_green, min_red;
671 BYTE max_blue, max_green, max_red;
672
673 if (!data || fmt != PixelFormat32bppARGB)
674 return PixelFormat32bppARGB;
675
676 if (attributes->colorkeys[type].enabled)
677 key = &attributes->colorkeys[type];
678 else
679 key = &attributes->colorkeys[ColorAdjustTypeDefault];
680
681 min_blue = key->low&0xff;
682 min_green = (key->low>>8)&0xff;
683 min_red = (key->low>>16)&0xff;
684
685 max_blue = key->high&0xff;
686 max_green = (key->high>>8)&0xff;
687 max_red = (key->high>>16)&0xff;
688
689 for (x=0; x<width; x++)
690 for (y=0; y<height; y++)
691 {
692 ARGB *src_color;
693 BYTE blue, green, red;
694 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
695 blue = *src_color&0xff;
696 green = (*src_color>>8)&0xff;
697 red = (*src_color>>16)&0xff;
698 if (blue >= min_blue && green >= min_green && red >= min_red &&
699 blue <= max_blue && green <= max_green && red <= max_red)
700 *src_color = 0x00000000;
701 }
702 }
703
704 if (attributes->colorremaptables[type].enabled ||
705 attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
706 {
707 const struct color_remap_table *table;
708
709 if (!data || fmt != PixelFormat32bppARGB)
710 return PixelFormat32bppARGB;
711
712 if (attributes->colorremaptables[type].enabled)
713 table = &attributes->colorremaptables[type];
714 else
715 table = &attributes->colorremaptables[ColorAdjustTypeDefault];
716
717 for (x=0; x<width; x++)
718 for (y=0; y<height; y++)
719 {
720 ARGB *src_color;
721 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
722 for (i=0; i<table->mapsize; i++)
723 {
724 if (*src_color == table->colormap[i].oldColor.Argb)
725 {
726 *src_color = table->colormap[i].newColor.Argb;
727 break;
728 }
729 }
730 }
731 }
732
733 if (attributes->colormatrices[type].enabled ||
734 attributes->colormatrices[ColorAdjustTypeDefault].enabled)
735 {
736 const struct color_matrix *colormatrices;
737 int color_matrix[5][5];
738 int gray_matrix[5][5];
739 BOOL identity;
740
741 if (!data || fmt != PixelFormat32bppARGB)
742 return PixelFormat32bppARGB;
743
744 if (attributes->colormatrices[type].enabled)
745 colormatrices = &attributes->colormatrices[type];
746 else
747 colormatrices = &attributes->colormatrices[ColorAdjustTypeDefault];
748
749 identity = round_color_matrix(&colormatrices->colormatrix, color_matrix);
750
751 if (colormatrices->flags == ColorMatrixFlagsAltGray)
752 identity = (round_color_matrix(&colormatrices->graymatrix, gray_matrix) && identity);
753
754 if (!identity)
755 {
756 for (x=0; x<width; x++)
757 {
758 for (y=0; y<height; y++)
759 {
760 ARGB *src_color;
761 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
762
763 if (colormatrices->flags == ColorMatrixFlagsDefault ||
764 !color_is_gray(*src_color))
765 {
766 *src_color = transform_color(*src_color, color_matrix);
767 }
768 else if (colormatrices->flags == ColorMatrixFlagsAltGray)
769 {
770 *src_color = transform_color(*src_color, gray_matrix);
771 }
772 }
773 }
774 }
775 }
776
777 if (attributes->gamma_enabled[type] ||
778 attributes->gamma_enabled[ColorAdjustTypeDefault])
779 {
780 REAL gamma;
781
782 if (!data || fmt != PixelFormat32bppARGB)
783 return PixelFormat32bppARGB;
784
785 if (attributes->gamma_enabled[type])
786 gamma = attributes->gamma[type];
787 else
788 gamma = attributes->gamma[ColorAdjustTypeDefault];
789
790 for (x=0; x<width; x++)
791 for (y=0; y<height; y++)
792 {
793 ARGB *src_color;
794 BYTE blue, green, red;
795 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
796
797 blue = *src_color&0xff;
798 green = (*src_color>>8)&0xff;
799 red = (*src_color>>16)&0xff;
800
801 /* FIXME: We should probably use a table for this. */
802 blue = floorf(powf(blue / 255.0, gamma) * 255.0);
803 green = floorf(powf(green / 255.0, gamma) * 255.0);
804 red = floorf(powf(red / 255.0, gamma) * 255.0);
805
806 *src_color = (*src_color & 0xff000000) | (red << 16) | (green << 8) | blue;
807 }
808 }
809
810 return fmt;
811 }
812
813 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
814 * bitmap that contains all the pixels we may need to draw it. */
815 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
816 GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
817 GpRect *rect)
818 {
819 INT left, top, right, bottom;
820
821 switch (interpolation)
822 {
823 case InterpolationModeHighQualityBilinear:
824 case InterpolationModeHighQualityBicubic:
825 /* FIXME: Include a greater range for the prefilter? */
826 case InterpolationModeBicubic:
827 case InterpolationModeBilinear:
828 left = (INT)(floorf(srcx));
829 top = (INT)(floorf(srcy));
830 right = (INT)(ceilf(srcx+srcwidth));
831 bottom = (INT)(ceilf(srcy+srcheight));
832 break;
833 case InterpolationModeNearestNeighbor:
834 default:
835 left = gdip_round(srcx);
836 top = gdip_round(srcy);
837 right = gdip_round(srcx+srcwidth);
838 bottom = gdip_round(srcy+srcheight);
839 break;
840 }
841
842 if (wrap == WrapModeClamp)
843 {
844 if (left < 0)
845 left = 0;
846 if (top < 0)
847 top = 0;
848 if (right >= bitmap->width)
849 right = bitmap->width-1;
850 if (bottom >= bitmap->height)
851 bottom = bitmap->height-1;
852 if (bottom < top || right < left)
853 /* entirely outside image, just sample a pixel so we don't have to
854 * special-case this later */
855 left = top = right = bottom = 0;
856 }
857 else
858 {
859 /* In some cases we can make the rectangle smaller here, but the logic
860 * is hard to get right, and tiling suggests we're likely to use the
861 * entire source image. */
862 if (left < 0 || right >= bitmap->width)
863 {
864 left = 0;
865 right = bitmap->width-1;
866 }
867
868 if (top < 0 || bottom >= bitmap->height)
869 {
870 top = 0;
871 bottom = bitmap->height-1;
872 }
873 }
874
875 rect->X = left;
876 rect->Y = top;
877 rect->Width = right - left + 1;
878 rect->Height = bottom - top + 1;
879 }
880
881 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
882 UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
883 {
884 if (attributes->wrap == WrapModeClamp)
885 {
886 if (x < 0 || y < 0 || x >= width || y >= height)
887 return attributes->outside_color;
888 }
889 else
890 {
891 /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
892 if (x < 0)
893 x = width*2 + x % (width * 2);
894 if (y < 0)
895 y = height*2 + y % (height * 2);
896
897 if ((attributes->wrap & 1) == 1)
898 {
899 /* Flip X */
900 if ((x / width) % 2 == 0)
901 x = x % width;
902 else
903 x = width - 1 - x % width;
904 }
905 else
906 x = x % width;
907
908 if ((attributes->wrap & 2) == 2)
909 {
910 /* Flip Y */
911 if ((y / height) % 2 == 0)
912 y = y % height;
913 else
914 y = height - 1 - y % height;
915 }
916 else
917 y = y % height;
918 }
919
920 if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
921 {
922 ERR("out of range pixel requested\n");
923 return 0xffcd0084;
924 }
925
926 return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
927 }
928
929 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
930 UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
931 InterpolationMode interpolation, PixelOffsetMode offset_mode)
932 {
933 static int fixme;
934
935 switch (interpolation)
936 {
937 default:
938 if (!fixme++)
939 FIXME("Unimplemented interpolation %i\n", interpolation);
940 /* fall-through */
941 case InterpolationModeBilinear:
942 {
943 REAL leftxf, topyf;
944 INT leftx, rightx, topy, bottomy;
945 ARGB topleft, topright, bottomleft, bottomright;
946 ARGB top, bottom;
947 float x_offset;
948
949 leftxf = floorf(point->X);
950 leftx = (INT)leftxf;
951 rightx = (INT)ceilf(point->X);
952 topyf = floorf(point->Y);
953 topy = (INT)topyf;
954 bottomy = (INT)ceilf(point->Y);
955
956 if (leftx == rightx && topy == bottomy)
957 return sample_bitmap_pixel(src_rect, bits, width, height,
958 leftx, topy, attributes);
959
960 topleft = sample_bitmap_pixel(src_rect, bits, width, height,
961 leftx, topy, attributes);
962 topright = sample_bitmap_pixel(src_rect, bits, width, height,
963 rightx, topy, attributes);
964 bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
965 leftx, bottomy, attributes);
966 bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
967 rightx, bottomy, attributes);
968
969 x_offset = point->X - leftxf;
970 top = blend_colors(topleft, topright, x_offset);
971 bottom = blend_colors(bottomleft, bottomright, x_offset);
972
973 return blend_colors(top, bottom, point->Y - topyf);
974 }
975 case InterpolationModeNearestNeighbor:
976 {
977 FLOAT pixel_offset;
978 switch (offset_mode)
979 {
980 default:
981 case PixelOffsetModeNone:
982 case PixelOffsetModeHighSpeed:
983 pixel_offset = 0.5;
984 break;
985
986 case PixelOffsetModeHalf:
987 case PixelOffsetModeHighQuality:
988 pixel_offset = 0.0;
989 break;
990 }
991 return sample_bitmap_pixel(src_rect, bits, width, height,
992 floorf(point->X + pixel_offset), floorf(point->Y + pixel_offset), attributes);
993 }
994
995 }
996 }
997
998 static REAL intersect_line_scanline(const GpPointF *p1, const GpPointF *p2, REAL y)
999 {
1000 return (p1->X - p2->X) * (p2->Y - y) / (p2->Y - p1->Y) + p2->X;
1001 }
1002
1003 static BOOL brush_can_fill_path(GpBrush *brush)
1004 {
1005 switch (brush->bt)
1006 {
1007 case BrushTypeSolidColor:
1008 return TRUE;
1009 case BrushTypeHatchFill:
1010 {
1011 GpHatch *hatch = (GpHatch*)brush;
1012 return ((hatch->forecol & 0xff000000) == 0xff000000) &&
1013 ((hatch->backcol & 0xff000000) == 0xff000000);
1014 }
1015 case BrushTypeLinearGradient:
1016 case BrushTypeTextureFill:
1017 /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
1018 default:
1019 return FALSE;
1020 }
1021 }
1022
1023 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
1024 {
1025 switch (brush->bt)
1026 {
1027 case BrushTypeSolidColor:
1028 {
1029 GpSolidFill *fill = (GpSolidFill*)brush;
1030 HBITMAP bmp = ARGB2BMP(fill->color);
1031
1032 if (bmp)
1033 {
1034 RECT rc;
1035 /* partially transparent fill */
1036
1037 SelectClipPath(graphics->hdc, RGN_AND);
1038 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
1039 {
1040 HDC hdc = CreateCompatibleDC(NULL);
1041
1042 if (!hdc) break;
1043
1044 SelectObject(hdc, bmp);
1045 gdi_alpha_blend(graphics, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
1046 hdc, 0, 0, 1, 1);
1047 DeleteDC(hdc);
1048 }
1049
1050 DeleteObject(bmp);
1051 break;
1052 }
1053 /* else fall through */
1054 }
1055 default:
1056 {
1057 HBRUSH gdibrush, old_brush;
1058
1059 gdibrush = create_gdi_brush(brush);
1060 if (!gdibrush) return;
1061
1062 old_brush = SelectObject(graphics->hdc, gdibrush);
1063 FillPath(graphics->hdc);
1064 SelectObject(graphics->hdc, old_brush);
1065 DeleteObject(gdibrush);
1066 break;
1067 }
1068 }
1069 }
1070
1071 static BOOL brush_can_fill_pixels(GpBrush *brush)
1072 {
1073 switch (brush->bt)
1074 {
1075 case BrushTypeSolidColor:
1076 case BrushTypeHatchFill:
1077 case BrushTypeLinearGradient:
1078 case BrushTypeTextureFill:
1079 case BrushTypePathGradient:
1080 return TRUE;
1081 default:
1082 return FALSE;
1083 }
1084 }
1085
1086 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
1087 DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
1088 {
1089 switch (brush->bt)
1090 {
1091 case BrushTypeSolidColor:
1092 {
1093 int x, y;
1094 GpSolidFill *fill = (GpSolidFill*)brush;
1095 for (x=0; x<fill_area->Width; x++)
1096 for (y=0; y<fill_area->Height; y++)
1097 argb_pixels[x + y*cdwStride] = fill->color;
1098 return Ok;
1099 }
1100 case BrushTypeHatchFill:
1101 {
1102 int x, y;
1103 GpHatch *fill = (GpHatch*)brush;
1104 const char *hatch_data;
1105
1106 if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
1107 return NotImplemented;
1108
1109 for (x=0; x<fill_area->Width; x++)
1110 for (y=0; y<fill_area->Height; y++)
1111 {
1112 int hx, hy;
1113
1114 /* FIXME: Account for the rendering origin */
1115 hx = (x + fill_area->X) % 8;
1116 hy = (y + fill_area->Y) % 8;
1117
1118 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
1119 argb_pixels[x + y*cdwStride] = fill->forecol;
1120 else
1121 argb_pixels[x + y*cdwStride] = fill->backcol;
1122 }
1123
1124 return Ok;
1125 }
1126 case BrushTypeLinearGradient:
1127 {
1128 GpLineGradient *fill = (GpLineGradient*)brush;
1129 GpPointF draw_points[3], line_points[3];
1130 GpStatus stat;
1131 static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
1132 GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
1133 int x, y;
1134
1135 draw_points[0].X = fill_area->X;
1136 draw_points[0].Y = fill_area->Y;
1137 draw_points[1].X = fill_area->X+1;
1138 draw_points[1].Y = fill_area->Y;
1139 draw_points[2].X = fill_area->X;
1140 draw_points[2].Y = fill_area->Y+1;
1141
1142 /* Transform the points to a co-ordinate space where X is the point's
1143 * position in the gradient, 0.0 being the start point and 1.0 the
1144 * end point. */
1145 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1146 CoordinateSpaceDevice, draw_points, 3);
1147
1148 if (stat == Ok)
1149 {
1150 line_points[0] = fill->startpoint;
1151 line_points[1] = fill->endpoint;
1152 line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
1153 line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
1154
1155 stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
1156 }
1157
1158 if (stat == Ok)
1159 {
1160 stat = GdipInvertMatrix(world_to_gradient);
1161
1162 if (stat == Ok)
1163 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
1164
1165 GdipDeleteMatrix(world_to_gradient);
1166 }
1167
1168 if (stat == Ok)
1169 {
1170 REAL x_delta = draw_points[1].X - draw_points[0].X;
1171 REAL y_delta = draw_points[2].X - draw_points[0].X;
1172
1173 for (y=0; y<fill_area->Height; y++)
1174 {
1175 for (x=0; x<fill_area->Width; x++)
1176 {
1177 REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
1178
1179 argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
1180 }
1181 }
1182 }
1183
1184 return stat;
1185 }
1186 case BrushTypeTextureFill:
1187 {
1188 GpTexture *fill = (GpTexture*)brush;
1189 GpPointF draw_points[3];
1190 GpStatus stat;
1191 int x, y;
1192 GpBitmap *bitmap;
1193 int src_stride;
1194 GpRect src_area;
1195
1196 if (fill->image->type != ImageTypeBitmap)
1197 {
1198 FIXME("metafile texture brushes not implemented\n");
1199 return NotImplemented;
1200 }
1201
1202 bitmap = (GpBitmap*)fill->image;
1203 src_stride = sizeof(ARGB) * bitmap->width;
1204
1205 src_area.X = src_area.Y = 0;
1206 src_area.Width = bitmap->width;
1207 src_area.Height = bitmap->height;
1208
1209 draw_points[0].X = fill_area->X;
1210 draw_points[0].Y = fill_area->Y;
1211 draw_points[1].X = fill_area->X+1;
1212 draw_points[1].Y = fill_area->Y;
1213 draw_points[2].X = fill_area->X;
1214 draw_points[2].Y = fill_area->Y+1;
1215
1216 /* Transform the points to the co-ordinate space of the bitmap. */
1217 stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
1218 CoordinateSpaceDevice, draw_points, 3);
1219
1220 if (stat == Ok)
1221 {
1222 GpMatrix world_to_texture = fill->transform;
1223
1224 stat = GdipInvertMatrix(&world_to_texture);
1225 if (stat == Ok)
1226 stat = GdipTransformMatrixPoints(&world_to_texture, draw_points, 3);
1227 }
1228
1229 if (stat == Ok && !fill->bitmap_bits)
1230 {
1231 BitmapData lockeddata;
1232
1233 fill->bitmap_bits = heap_alloc_zero(sizeof(ARGB) * bitmap->width * bitmap->height);
1234 if (!fill->bitmap_bits)
1235 stat = OutOfMemory;
1236
1237 if (stat == Ok)
1238 {
1239 lockeddata.Width = bitmap->width;
1240 lockeddata.Height = bitmap->height;
1241 lockeddata.Stride = src_stride;
1242 lockeddata.PixelFormat = PixelFormat32bppARGB;
1243 lockeddata.Scan0 = fill->bitmap_bits;
1244
1245 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
1246 PixelFormat32bppARGB, &lockeddata);
1247 }
1248
1249 if (stat == Ok)
1250 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1251
1252 if (stat == Ok)
1253 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
1254 bitmap->width, bitmap->height,
1255 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
1256
1257 if (stat != Ok)
1258 {
1259 heap_free(fill->bitmap_bits);
1260 fill->bitmap_bits = NULL;
1261 }
1262 }
1263
1264 if (stat == Ok)
1265 {
1266 REAL x_dx = draw_points[1].X - draw_points[0].X;
1267 REAL x_dy = draw_points[1].Y - draw_points[0].Y;
1268 REAL y_dx = draw_points[2].X - draw_points[0].X;
1269 REAL y_dy = draw_points[2].Y - draw_points[0].Y;
1270
1271 for (y=0; y<fill_area->Height; y++)
1272 {
1273 for (x=0; x<fill_area->Width; x++)
1274 {
1275 GpPointF point;
1276 point.X = draw_points[0].X + x * x_dx + y * y_dx;
1277 point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
1278
1279 argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
1280 &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
1281 &point, fill->imageattributes, graphics->interpolation,
1282 graphics->pixeloffset);
1283 }
1284 }
1285 }
1286
1287 return stat;
1288 }
1289 case BrushTypePathGradient:
1290 {
1291 GpPathGradient *fill = (GpPathGradient*)brush;
1292 GpPath *flat_path;
1293 GpMatrix world_to_device;
1294 GpStatus stat;
1295 int i, figure_start=0;
1296 GpPointF start_point, end_point, center_point;
1297 BYTE type;
1298 REAL min_yf, max_yf, line1_xf, line2_xf;
1299 INT min_y, max_y, min_x, max_x;
1300 INT x, y;
1301 ARGB outer_color;
1302 static BOOL transform_fixme_once;
1303
1304 if (fill->focus.X != 0.0 || fill->focus.Y != 0.0)
1305 {
1306 static int once;
1307 if (!once++)
1308 FIXME("path gradient focus not implemented\n");
1309 }
1310
1311 if (fill->gamma)
1312 {
1313 static int once;
1314 if (!once++)
1315 FIXME("path gradient gamma correction not implemented\n");
1316 }
1317
1318 if (fill->blendcount)
1319 {
1320 static int once;
1321 if (!once++)
1322 FIXME("path gradient blend not implemented\n");
1323 }
1324
1325 if (fill->pblendcount)
1326 {
1327 static int once;
1328 if (!once++)
1329 FIXME("path gradient preset blend not implemented\n");
1330 }
1331
1332 if (!transform_fixme_once)
1333 {
1334 BOOL is_identity=TRUE;
1335 GdipIsMatrixIdentity(&fill->transform, &is_identity);
1336 if (!is_identity)
1337 {
1338 FIXME("path gradient transform not implemented\n");
1339 transform_fixme_once = TRUE;
1340 }
1341 }
1342
1343 stat = GdipClonePath(fill->path, &flat_path);
1344
1345 if (stat != Ok)
1346 return stat;
1347
1348 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1349 CoordinateSpaceWorld, &world_to_device);
1350 if (stat == Ok)
1351 {
1352 stat = GdipTransformPath(flat_path, &world_to_device);
1353
1354 if (stat == Ok)
1355 {
1356 center_point = fill->center;
1357 stat = GdipTransformMatrixPoints(&world_to_device, &center_point, 1);
1358 }
1359
1360 if (stat == Ok)
1361 stat = GdipFlattenPath(flat_path, NULL, 0.5);
1362 }
1363
1364 if (stat != Ok)
1365 {
1366 GdipDeletePath(flat_path);
1367 return stat;
1368 }
1369
1370 for (i=0; i<flat_path->pathdata.Count; i++)
1371 {
1372 int start_center_line=0, end_center_line=0;
1373 BOOL seen_start = FALSE, seen_end = FALSE, seen_center = FALSE;
1374 REAL center_distance;
1375 ARGB start_color, end_color;
1376 REAL dy, dx;
1377
1378 type = flat_path->pathdata.Types[i];
1379
1380 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1381 figure_start = i;
1382
1383 start_point = flat_path->pathdata.Points[i];
1384
1385 start_color = fill->surroundcolors[min(i, fill->surroundcolorcount-1)];
1386
1387 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath || i+1 >= flat_path->pathdata.Count)
1388 {
1389 end_point = flat_path->pathdata.Points[figure_start];
1390 end_color = fill->surroundcolors[min(figure_start, fill->surroundcolorcount-1)];
1391 }
1392 else if ((flat_path->pathdata.Types[i+1] & PathPointTypePathTypeMask) == PathPointTypeLine)
1393 {
1394 end_point = flat_path->pathdata.Points[i+1];
1395 end_color = fill->surroundcolors[min(i+1, fill->surroundcolorcount-1)];
1396 }
1397 else
1398 continue;
1399
1400 outer_color = start_color;
1401
1402 min_yf = center_point.Y;
1403 if (min_yf > start_point.Y) min_yf = start_point.Y;
1404 if (min_yf > end_point.Y) min_yf = end_point.Y;
1405
1406 if (min_yf < fill_area->Y)
1407 min_y = fill_area->Y;
1408 else
1409 min_y = (INT)ceil(min_yf);
1410
1411 max_yf = center_point.Y;
1412 if (max_yf < start_point.Y) max_yf = start_point.Y;
1413 if (max_yf < end_point.Y) max_yf = end_point.Y;
1414
1415 if (max_yf > fill_area->Y + fill_area->Height)
1416 max_y = fill_area->Y + fill_area->Height;
1417 else
1418 max_y = (INT)ceil(max_yf);
1419
1420 dy = end_point.Y - start_point.Y;
1421 dx = end_point.X - start_point.X;
1422
1423 /* This is proportional to the distance from start-end line to center point. */
1424 center_distance = dy * (start_point.X - center_point.X) +
1425 dx * (center_point.Y - start_point.Y);
1426
1427 for (y=min_y; y<max_y; y++)
1428 {
1429 REAL yf = (REAL)y;
1430
1431 if (!seen_start && yf >= start_point.Y)
1432 {
1433 seen_start = TRUE;
1434 start_center_line ^= 1;
1435 }
1436 if (!seen_end && yf >= end_point.Y)
1437 {
1438 seen_end = TRUE;
1439 end_center_line ^= 1;
1440 }
1441 if (!seen_center && yf >= center_point.Y)
1442 {
1443 seen_center = TRUE;
1444 start_center_line ^= 1;
1445 end_center_line ^= 1;
1446 }
1447
1448 if (start_center_line)
1449 line1_xf = intersect_line_scanline(&start_point, &center_point, yf);
1450 else
1451 line1_xf = intersect_line_scanline(&start_point, &end_point, yf);
1452
1453 if (end_center_line)
1454 line2_xf = intersect_line_scanline(&end_point, &center_point, yf);
1455 else
1456 line2_xf = intersect_line_scanline(&start_point, &end_point, yf);
1457
1458 if (line1_xf < line2_xf)
1459 {
1460 min_x = (INT)ceil(line1_xf);
1461 max_x = (INT)ceil(line2_xf);
1462 }
1463 else
1464 {
1465 min_x = (INT)ceil(line2_xf);
1466 max_x = (INT)ceil(line1_xf);
1467 }
1468
1469 if (min_x < fill_area->X)
1470 min_x = fill_area->X;
1471 if (max_x > fill_area->X + fill_area->Width)
1472 max_x = fill_area->X + fill_area->Width;
1473
1474 for (x=min_x; x<max_x; x++)
1475 {
1476 REAL xf = (REAL)x;
1477 REAL distance;
1478
1479 if (start_color != end_color)
1480 {
1481 REAL blend_amount, pdy, pdx;
1482 pdy = yf - center_point.Y;
1483 pdx = xf - center_point.X;
1484 blend_amount = ( (center_point.Y - start_point.Y) * pdx + (start_point.X - center_point.X) * pdy ) / ( dy * pdx - dx * pdy );
1485 outer_color = blend_colors(start_color, end_color, blend_amount);
1486 }
1487
1488 distance = (end_point.Y - start_point.Y) * (start_point.X - xf) +
1489 (end_point.X - start_point.X) * (yf - start_point.Y);
1490
1491 distance = distance / center_distance;
1492
1493 argb_pixels[(x-fill_area->X) + (y-fill_area->Y)*cdwStride] =
1494 blend_colors(outer_color, fill->centercolor, distance);
1495 }
1496 }
1497 }
1498
1499 GdipDeletePath(flat_path);
1500 return stat;
1501 }
1502 default:
1503 return NotImplemented;
1504 }
1505 }
1506
1507 /* Draws the linecap the specified color and size on the hdc. The linecap is in
1508 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
1509 * should not be called on an hdc that has a path you care about. */
1510 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
1511 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
1512 {
1513 HGDIOBJ oldbrush = NULL, oldpen = NULL;
1514 GpMatrix matrix;
1515 HBRUSH brush = NULL;
1516 HPEN pen = NULL;
1517 PointF ptf[4], *custptf = NULL;
1518 POINT pt[4], *custpt = NULL;
1519 BYTE *tp = NULL;
1520 REAL theta, dsmall, dbig, dx, dy = 0.0;
1521 INT i, count;
1522 LOGBRUSH lb;
1523 BOOL customstroke;
1524
1525 if((x1 == x2) && (y1 == y2))
1526 return;
1527
1528 theta = gdiplus_atan2(y2 - y1, x2 - x1);
1529
1530 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
1531 if(!customstroke){
1532 brush = CreateSolidBrush(color);
1533 lb.lbStyle = BS_SOLID;
1534 lb.lbColor = color;
1535 lb.lbHatch = 0;
1536 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
1537 PS_JOIN_MITER, 1, &lb, 0,
1538 NULL);
1539 oldbrush = SelectObject(graphics->hdc, brush);
1540 oldpen = SelectObject(graphics->hdc, pen);
1541 }
1542
1543 switch(cap){
1544 case LineCapFlat:
1545 break;
1546 case LineCapSquare:
1547 case LineCapSquareAnchor:
1548 case LineCapDiamondAnchor:
1549 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
1550 if(cap == LineCapDiamondAnchor){
1551 dsmall = cos(theta + M_PI_2) * size;
1552 dbig = sin(theta + M_PI_2) * size;
1553 }
1554 else{
1555 dsmall = cos(theta + M_PI_4) * size;
1556 dbig = sin(theta + M_PI_4) * size;
1557 }
1558
1559 ptf[0].X = x2 - dsmall;
1560 ptf[1].X = x2 + dbig;
1561
1562 ptf[0].Y = y2 - dbig;
1563 ptf[3].Y = y2 + dsmall;
1564
1565 ptf[1].Y = y2 - dsmall;
1566 ptf[2].Y = y2 + dbig;
1567
1568 ptf[3].X = x2 - dbig;
1569 ptf[2].X = x2 + dsmall;
1570
1571 transform_and_round_points(graphics, pt, ptf, 4);
1572 Polygon(graphics->hdc, pt, 4);
1573
1574 break;
1575 case LineCapArrowAnchor:
1576 size = size * 4.0 / sqrt(3.0);
1577
1578 dx = cos(M_PI / 6.0 + theta) * size;
1579 dy = sin(M_PI / 6.0 + theta) * size;
1580
1581 ptf[0].X = x2 - dx;
1582 ptf[0].Y = y2 - dy;
1583
1584 dx = cos(- M_PI / 6.0 + theta) * size;
1585 dy = sin(- M_PI / 6.0 + theta) * size;
1586
1587 ptf[1].X = x2 - dx;
1588 ptf[1].Y = y2 - dy;
1589
1590 ptf[2].X = x2;
1591 ptf[2].Y = y2;
1592
1593 transform_and_round_points(graphics, pt, ptf, 3);
1594 Polygon(graphics->hdc, pt, 3);
1595
1596 break;
1597 case LineCapRoundAnchor:
1598 dx = dy = ANCHOR_WIDTH * size / 2.0;
1599
1600 ptf[0].X = x2 - dx;
1601 ptf[0].Y = y2 - dy;
1602 ptf[1].X = x2 + dx;
1603 ptf[1].Y = y2 + dy;
1604
1605 transform_and_round_points(graphics, pt, ptf, 2);
1606 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
1607
1608 break;
1609 case LineCapTriangle:
1610 size = size / 2.0;
1611 dx = cos(M_PI_2 + theta) * size;
1612 dy = sin(M_PI_2 + theta) * size;
1613
1614 ptf[0].X = x2 - dx;
1615 ptf[0].Y = y2 - dy;
1616 ptf[1].X = x2 + dx;
1617 ptf[1].Y = y2 + dy;
1618
1619 dx = cos(theta) * size;
1620 dy = sin(theta) * size;
1621
1622 ptf[2].X = x2 + dx;
1623 ptf[2].Y = y2 + dy;
1624
1625 transform_and_round_points(graphics, pt, ptf, 3);
1626 Polygon(graphics->hdc, pt, 3);
1627
1628 break;
1629 case LineCapRound:
1630 dx = dy = size / 2.0;
1631
1632 ptf[0].X = x2 - dx;
1633 ptf[0].Y = y2 - dy;
1634 ptf[1].X = x2 + dx;
1635 ptf[1].Y = y2 + dy;
1636
1637 dx = -cos(M_PI_2 + theta) * size;
1638 dy = -sin(M_PI_2 + theta) * size;
1639
1640 ptf[2].X = x2 - dx;
1641 ptf[2].Y = y2 - dy;
1642 ptf[3].X = x2 + dx;
1643 ptf[3].Y = y2 + dy;
1644
1645 transform_and_round_points(graphics, pt, ptf, 4);
1646 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1647 pt[2].y, pt[3].x, pt[3].y);
1648
1649 break;
1650 case LineCapCustom:
1651 if(!custom)
1652 break;
1653
1654 count = custom->pathdata.Count;
1655 custptf = heap_alloc_zero(count * sizeof(PointF));
1656 custpt = heap_alloc_zero(count * sizeof(POINT));
1657 tp = heap_alloc_zero(count);
1658
1659 if(!custptf || !custpt || !tp)
1660 goto custend;
1661
1662 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1663
1664 GdipSetMatrixElements(&matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
1665 GdipScaleMatrix(&matrix, size, size, MatrixOrderAppend);
1666 GdipRotateMatrix(&matrix, (180.0 / M_PI) * (theta - M_PI_2),
1667 MatrixOrderAppend);
1668 GdipTranslateMatrix(&matrix, x2, y2, MatrixOrderAppend);
1669 GdipTransformMatrixPoints(&matrix, custptf, count);
1670
1671 transform_and_round_points(graphics, custpt, custptf, count);
1672
1673 for(i = 0; i < count; i++)
1674 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1675
1676 if(custom->fill){
1677 BeginPath(graphics->hdc);
1678 PolyDraw(graphics->hdc, custpt, tp, count);
1679 EndPath(graphics->hdc);
1680 StrokeAndFillPath(graphics->hdc);
1681 }
1682 else
1683 PolyDraw(graphics->hdc, custpt, tp, count);
1684
1685 custend:
1686 heap_free(custptf);
1687 heap_free(custpt);
1688 heap_free(tp);
1689 break;
1690 default:
1691 break;
1692 }
1693
1694 if(!customstroke){
1695 SelectObject(graphics->hdc, oldbrush);
1696 SelectObject(graphics->hdc, oldpen);
1697 DeleteObject(brush);
1698 DeleteObject(pen);
1699 }
1700 }
1701
1702 /* Shortens the line by the given percent by changing x2, y2.
1703 * If percent is > 1.0 then the line will change direction.
1704 * If percent is negative it can lengthen the line. */
1705 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
1706 {
1707 REAL dist, theta, dx, dy;
1708
1709 if((y1 == *y2) && (x1 == *x2))
1710 return;
1711
1712 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1713 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1714 dx = cos(theta) * dist;
1715 dy = sin(theta) * dist;
1716
1717 *x2 = *x2 + dx;
1718 *y2 = *y2 + dy;
1719 }
1720
1721 /* Shortens the line by the given amount by changing x2, y2.
1722 * If the amount is greater than the distance, the line will become length 0.
1723 * If the amount is negative, it can lengthen the line. */
1724 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1725 {
1726 REAL dx, dy, percent;
1727
1728 dx = *x2 - x1;
1729 dy = *y2 - y1;
1730 if(dx == 0 && dy == 0)
1731 return;
1732
1733 percent = amt / sqrt(dx * dx + dy * dy);
1734 if(percent >= 1.0){
1735 *x2 = x1;
1736 *y2 = y1;
1737 return;
1738 }
1739
1740 shorten_line_percent(x1, y1, x2, y2, percent);
1741 }
1742
1743 /* Conducts a linear search to find the bezier points that will back off
1744 * the endpoint of the curve by a distance of amt. Linear search works
1745 * better than binary in this case because there are multiple solutions,
1746 * and binary searches often find a bad one. I don't think this is what
1747 * Windows does but short of rendering the bezier without GDI's help it's
1748 * the best we can do. If rev then work from the start of the passed points
1749 * instead of the end. */
1750 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1751 {
1752 GpPointF origpt[4];
1753 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1754 INT i, first = 0, second = 1, third = 2, fourth = 3;
1755
1756 if(rev){
1757 first = 3;
1758 second = 2;
1759 third = 1;
1760 fourth = 0;
1761 }
1762
1763 origx = pt[fourth].X;
1764 origy = pt[fourth].Y;
1765 memcpy(origpt, pt, sizeof(GpPointF) * 4);
1766
1767 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1768 /* reset bezier points to original values */
1769 memcpy(pt, origpt, sizeof(GpPointF) * 4);
1770 /* Perform magic on bezier points. Order is important here.*/
1771 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1772 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1773 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1774 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1775 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1776 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1777
1778 dx = pt[fourth].X - origx;
1779 dy = pt[fourth].Y - origy;
1780
1781 diff = sqrt(dx * dx + dy * dy);
1782 percent += 0.0005 * amt;
1783 }
1784 }
1785
1786 /* Draws a combination of bezier curves and lines between points. */
1787 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1788 GDIPCONST BYTE * types, INT count, BOOL caps)
1789 {
1790 POINT *pti = heap_alloc_zero(count * sizeof(POINT));
1791 BYTE *tp = heap_alloc_zero(count);
1792 GpPointF *ptcopy = heap_alloc_zero(count * sizeof(GpPointF));
1793 INT i, j;
1794 GpStatus status = GenericError;
1795
1796 if(!count){
1797 status = Ok;
1798 goto end;
1799 }
1800 if(!pti || !tp || !ptcopy){
1801 status = OutOfMemory;
1802 goto end;
1803 }
1804
1805 for(i = 1; i < count; i++){
1806 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1807 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1808 || !(types[i + 2] & PathPointTypeBezier)){
1809 ERR("Bad bezier points\n");
1810 goto end;
1811 }
1812 i += 2;
1813 }
1814 }
1815
1816 memcpy(ptcopy, pt, count * sizeof(GpPointF));
1817
1818 /* If we are drawing caps, go through the points and adjust them accordingly,
1819 * and draw the caps. */
1820 if(caps){
1821 switch(types[count - 1] & PathPointTypePathTypeMask){
1822 case PathPointTypeBezier:
1823 if(pen->endcap == LineCapArrowAnchor)
1824 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1825 else if((pen->endcap == LineCapCustom) && pen->customend)
1826 shorten_bezier_amt(&ptcopy[count - 4],
1827 pen->width * pen->customend->inset, FALSE);
1828
1829 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1830 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1831 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1832 pt[count - 1].X, pt[count - 1].Y);
1833
1834 break;
1835 case PathPointTypeLine:
1836 if(pen->endcap == LineCapArrowAnchor)
1837 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1838 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1839 pen->width);
1840 else if((pen->endcap == LineCapCustom) && pen->customend)
1841 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1842 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1843 pen->customend->inset * pen->width);
1844
1845 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->endcap, pen->width, pen->customend,
1846 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1847 pt[count - 1].Y);
1848
1849 break;
1850 default:
1851 ERR("Bad path last point\n");
1852 goto end;
1853 }
1854
1855 /* Find start of points */
1856 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1857 == PathPointTypeStart); j++);
1858
1859 switch(types[j] & PathPointTypePathTypeMask){
1860 case PathPointTypeBezier:
1861 if(pen->startcap == LineCapArrowAnchor)
1862 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1863 else if((pen->startcap == LineCapCustom) && pen->customstart)
1864 shorten_bezier_amt(&ptcopy[j - 1],
1865 pen->width * pen->customstart->inset, TRUE);
1866
1867 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1868 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1869 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1870 pt[j - 1].X, pt[j - 1].Y);
1871
1872 break;
1873 case PathPointTypeLine:
1874 if(pen->startcap == LineCapArrowAnchor)
1875 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1876 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1877 pen->width);
1878 else if((pen->startcap == LineCapCustom) && pen->customstart)
1879 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1880 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1881 pen->customstart->inset * pen->width);
1882
1883 draw_cap(graphics, get_gdi_brush_color(pen->brush), pen->startcap, pen->width, pen->customstart,
1884 pt[j].X, pt[j].Y, pt[j - 1].X,
1885 pt[j - 1].Y);
1886
1887 break;
1888 default:
1889 ERR("Bad path points\n");
1890 goto end;
1891 }
1892 }
1893
1894 transform_and_round_points(graphics, pti, ptcopy, count);
1895
1896 for(i = 0; i < count; i++){
1897 tp[i] = convert_path_point_type(types[i]);
1898 }
1899
1900 PolyDraw(graphics->hdc, pti, tp, count);
1901
1902 status = Ok;
1903
1904 end:
1905 heap_free(pti);
1906 heap_free(ptcopy);
1907 heap_free(tp);
1908
1909 return status;
1910 }
1911
1912 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1913 {
1914 GpStatus result;
1915
1916 BeginPath(graphics->hdc);
1917 result = draw_poly(graphics, NULL, path->pathdata.Points,
1918 path->pathdata.Types, path->pathdata.Count, FALSE);
1919 EndPath(graphics->hdc);
1920 return result;
1921 }
1922
1923 typedef struct _GraphicsContainerItem {
1924 struct list entry;
1925 GraphicsContainer contid;
1926
1927 SmoothingMode smoothing;
1928 CompositingQuality compqual;
1929 InterpolationMode interpolation;
1930 CompositingMode compmode;
1931 TextRenderingHint texthint;
1932 REAL scale;
1933 GpUnit unit;
1934 PixelOffsetMode pixeloffset;
1935 UINT textcontrast;
1936 GpMatrix worldtrans;
1937 GpRegion* clip;
1938 INT origin_x, origin_y;
1939 } GraphicsContainerItem;
1940
1941 static GpStatus init_container(GraphicsContainerItem** container,
1942 GDIPCONST GpGraphics* graphics){
1943 GpStatus sts;
1944
1945 *container = heap_alloc_zero(sizeof(GraphicsContainerItem));
1946 if(!(*container))
1947 return OutOfMemory;
1948
1949 (*container)->contid = graphics->contid + 1;
1950
1951 (*container)->smoothing = graphics->smoothing;
1952 (*container)->compqual = graphics->compqual;
1953 (*container)->interpolation = graphics->interpolation;
1954 (*container)->compmode = graphics->compmode;
1955 (*container)->texthint = graphics->texthint;
1956 (*container)->scale = graphics->scale;
1957 (*container)->unit = graphics->unit;
1958 (*container)->textcontrast = graphics->textcontrast;
1959 (*container)->pixeloffset = graphics->pixeloffset;
1960 (*container)->origin_x = graphics->origin_x;
1961 (*container)->origin_y = graphics->origin_y;
1962 (*container)->worldtrans = graphics->worldtrans;
1963
1964 sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1965 if(sts != Ok){
1966 heap_free(*container);
1967 *container = NULL;
1968 return sts;
1969 }
1970
1971 return Ok;
1972 }
1973
1974 static void delete_container(GraphicsContainerItem* container)
1975 {
1976 GdipDeleteRegion(container->clip);
1977 heap_free(container);
1978 }
1979
1980 static GpStatus restore_container(GpGraphics* graphics,
1981 GDIPCONST GraphicsContainerItem* container){
1982 GpStatus sts;
1983 GpRegion *newClip;
1984
1985 sts = GdipCloneRegion(container->clip, &newClip);
1986 if(sts != Ok) return sts;
1987
1988 graphics->worldtrans = container->worldtrans;
1989
1990 GdipDeleteRegion(graphics->clip);
1991 graphics->clip = newClip;
1992
1993 graphics->contid = container->contid - 1;
1994
1995 graphics->smoothing = container->smoothing;
1996 graphics->compqual = container->compqual;
1997 graphics->interpolation = container->interpolation;
1998 graphics->compmode = container->compmode;
1999 graphics->texthint = container->texthint;
2000 graphics->scale = container->scale;
2001 graphics->unit = container->unit;
2002 graphics->textcontrast = container->textcontrast;
2003 graphics->pixeloffset = container->pixeloffset;
2004 graphics->origin_x = container->origin_x;
2005 graphics->origin_y = container->origin_y;
2006
2007 return Ok;
2008 }
2009
2010 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
2011 {
2012 RECT wnd_rect;
2013 GpStatus stat=Ok;
2014 GpUnit unit;
2015
2016 if(graphics->hwnd) {
2017 if(!GetClientRect(graphics->hwnd, &wnd_rect))
2018 return GenericError;
2019
2020 rect->X = wnd_rect.left;
2021 rect->Y = wnd_rect.top;
2022 rect->Width = wnd_rect.right - wnd_rect.left;
2023 rect->Height = wnd_rect.bottom - wnd_rect.top;
2024 }else if (graphics->image){
2025 stat = GdipGetImageBounds(graphics->image, rect, &unit);
2026 if (stat == Ok && unit != UnitPixel)
2027 FIXME("need to convert from unit %i\n", unit);
2028 }else if (GetObjectType(graphics->hdc) == OBJ_MEMDC){
2029 HBITMAP hbmp;
2030 BITMAP bmp;
2031
2032 rect->X = 0;
2033 rect->Y = 0;
2034
2035 hbmp = GetCurrentObject(graphics->hdc, OBJ_BITMAP);
2036 if (hbmp && GetObjectW(hbmp, sizeof(bmp), &bmp))
2037 {
2038 rect->Width = bmp.bmWidth;
2039 rect->Height = bmp.bmHeight;
2040 }
2041 else
2042 {
2043 /* FIXME: ??? */
2044 rect->Width = 1;
2045 rect->Height = 1;
2046 }
2047 }else{
2048 rect->X = 0;
2049 rect->Y = 0;
2050 rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
2051 rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
2052 }
2053
2054 if (graphics->hdc)
2055 {
2056 POINT points[2];
2057
2058 points[0].x = rect->X;
2059 points[0].y = rect->Y;
2060 points[1].x = rect->X + rect->Width;
2061 points[1].y = rect->Y + rect->Height;
2062
2063 DPtoLP(graphics->hdc, points, sizeof(points)/sizeof(points[0]));
2064
2065 rect->X = min(points[0].x, points[1].x);
2066 rect->Y = min(points[0].y, points[1].y);
2067 rect->Width = abs(points[1].x - points[0].x);
2068 rect->Height = abs(points[1].y - points[0].y);
2069 }
2070
2071 return stat;
2072 }
2073
2074 /* on success, rgn will contain the region of the graphics object which
2075 * is visible after clipping has been applied */
2076 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
2077 {
2078 GpStatus stat;
2079 GpRectF rectf;
2080 GpRegion* tmp;
2081
2082 if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
2083 return stat;
2084
2085 if((stat = GdipCreateRegion(&tmp)) != Ok)
2086 return stat;
2087
2088 if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
2089 goto end;
2090
2091 if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
2092 goto end;
2093
2094 stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
2095
2096 end:
2097 GdipDeleteRegion(tmp);
2098 return stat;
2099 }
2100
2101 void get_log_fontW(const GpFont *font, GpGraphics *graphics, LOGFONTW *lf)
2102 {
2103 REAL height;
2104
2105 if (font->unit == UnitPixel)
2106 {
2107 height = units_to_pixels(font->emSize, graphics->unit, graphics->yres);
2108 }
2109 else
2110 {
2111 if (graphics->unit == UnitDisplay || graphics->unit == UnitPixel)
2112 height = units_to_pixels(font->emSize, font->unit, graphics->xres);
2113 else
2114 height = units_to_pixels(font->emSize, font->unit, graphics->yres);
2115 }
2116
2117 lf->lfHeight = -(height + 0.5);
2118 lf->lfWidth = 0;
2119 lf->lfEscapement = 0;
2120 lf->lfOrientation = 0;
2121 lf->lfWeight = font->otm.otmTextMetrics.tmWeight;
2122 lf->lfItalic = font->otm.otmTextMetrics.tmItalic ? 1 : 0;
2123 lf->lfUnderline = font->otm.otmTextMetrics.tmUnderlined ? 1 : 0;
2124 lf->lfStrikeOut = font->otm.otmTextMetrics.tmStruckOut ? 1 : 0;
2125 lf->lfCharSet = font->otm.otmTextMetrics.tmCharSet;
2126 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
2127 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
2128 lf->lfQuality = DEFAULT_QUALITY;
2129 lf->lfPitchAndFamily = 0;
2130 strcpyW(lf->lfFaceName, font->family->FamilyName);
2131 }
2132
2133 static void get_font_hfont(GpGraphics *graphics, GDIPCONST GpFont *font,
2134 GDIPCONST GpStringFormat *format, HFONT *hfont,
2135 GDIPCONST GpMatrix *matrix)
2136 {
2137 HDC hdc = CreateCompatibleDC(0);
2138 GpPointF pt[3];
2139 REAL angle, rel_width, rel_height, font_height;
2140 LOGFONTW lfw;
2141 HFONT unscaled_font;
2142 TEXTMETRICW textmet;
2143
2144 if (font->unit == UnitPixel || font->unit == UnitWorld)
2145 font_height = font->emSize;
2146 else
2147 {
2148 REAL unit_scale, res;
2149
2150 res = (graphics->unit == UnitDisplay || graphics->unit == UnitPixel) ? graphics->xres : graphics->yres;
2151 unit_scale = units_scale(font->unit, graphics->unit, res);
2152
2153 font_height = font->emSize * unit_scale;
2154 }
2155
2156 pt[0].X = 0.0;
2157 pt[0].Y = 0.0;
2158 pt[1].X = 1.0;
2159 pt[1].Y = 0.0;
2160 pt[2].X = 0.0;
2161 pt[2].Y = 1.0;
2162 if (matrix)
2163 {
2164 GpMatrix xform = *matrix;
2165 GdipTransformMatrixPoints(&xform, pt, 3);
2166 }
2167
2168 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2169 angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2170 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2171 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2172 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2173 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2174
2175 get_log_fontW(font, graphics, &lfw);
2176 lfw.lfHeight = -gdip_round(font_height * rel_height);
2177 unscaled_font = CreateFontIndirectW(&lfw);
2178
2179 SelectObject(hdc, unscaled_font);
2180 GetTextMetricsW(hdc, &textmet);
2181
2182 lfw.lfWidth = gdip_round(textmet.tmAveCharWidth * rel_width / rel_height);
2183 lfw.lfEscapement = lfw.lfOrientation = gdip_round((angle / M_PI) * 1800.0);
2184
2185 *hfont = CreateFontIndirectW(&lfw);
2186
2187 DeleteDC(hdc);
2188 DeleteObject(unscaled_font);
2189 }
2190
2191 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
2192 {
2193 TRACE("(%p, %p)\n", hdc, graphics);
2194
2195 return GdipCreateFromHDC2(hdc, NULL, graphics);
2196 }
2197
2198 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
2199 {
2200 GpStatus retval;
2201 HBITMAP hbitmap;
2202 DIBSECTION dib;
2203
2204 TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
2205
2206 if(hDevice != NULL)
2207 FIXME("Don't know how to handle parameter hDevice\n");
2208
2209 if(hdc == NULL)
2210 return OutOfMemory;
2211
2212 if(graphics == NULL)
2213 return InvalidParameter;
2214
2215 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2216 if(!*graphics) return OutOfMemory;
2217
2218 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2219
2220 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2221 heap_free(*graphics);
2222 return retval;
2223 }
2224
2225 hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
2226 if (hbitmap && GetObjectW(hbitmap, sizeof(dib), &dib) == sizeof(dib) &&
2227 dib.dsBmih.biBitCount == 32 && dib.dsBmih.biCompression == BI_RGB)
2228 {
2229 (*graphics)->alpha_hdc = 1;
2230 }
2231
2232 (*graphics)->hdc = hdc;
2233 (*graphics)->hwnd = WindowFromDC(hdc);
2234 (*graphics)->owndc = FALSE;
2235 (*graphics)->smoothing = SmoothingModeDefault;
2236 (*graphics)->compqual = CompositingQualityDefault;
2237 (*graphics)->interpolation = InterpolationModeBilinear;
2238 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2239 (*graphics)->compmode = CompositingModeSourceOver;
2240 (*graphics)->unit = UnitDisplay;
2241 (*graphics)->scale = 1.0;
2242 (*graphics)->xres = GetDeviceCaps(hdc, LOGPIXELSX);
2243 (*graphics)->yres = GetDeviceCaps(hdc, LOGPIXELSY);
2244 (*graphics)->busy = FALSE;
2245 (*graphics)->textcontrast = 4;
2246 list_init(&(*graphics)->containers);
2247 (*graphics)->contid = GDIP_GET_NEW_CONTID_FOR(*graphics);
2248
2249 TRACE("<-- %p\n", *graphics);
2250
2251 return Ok;
2252 }
2253
2254 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
2255 {
2256 GpStatus retval;
2257
2258 *graphics = heap_alloc_zero(sizeof(GpGraphics));
2259 if(!*graphics) return OutOfMemory;
2260
2261 GdipSetMatrixElements(&(*graphics)->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
2262
2263 if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
2264 heap_free(*graphics);
2265 return retval;
2266 }
2267
2268 (*graphics)->hdc = NULL;
2269 (*graphics)->hwnd = NULL;
2270 (*graphics)->owndc = FALSE;
2271 (*graphics)->image = image;
2272 /* We have to store the image type here because the image may be freed
2273 * before GdipDeleteGraphics is called, and metafiles need special treatment. */
2274 (*graphics)->image_type = image->type;
2275 (*graphics)->smoothing = SmoothingModeDefault;
2276 (*graphics)->compqual = CompositingQualityDefault;
2277 (*graphics)->interpolation = InterpolationModeBilinear;
2278 (*graphics)->pixeloffset = PixelOffsetModeDefault;
2279 (*graphics)->compmode = CompositingModeSourceOver;
2280 (*graphics)->unit = UnitDisplay;
2281 (*graphics)->scale = 1.0;
2282 (*graphics)->xres = image->xres;
2283 (*graphics)->yres = image->yres;
2284 (*graphics)->busy = FALSE;
2285 (*graphics)->textcontrast = 4;
2286 list_init(&(*graphics)->containers);
2287 (*graphics)->contid = GDIP_GET_NEW_CONTID_FOR(*graphics);
2288
2289 TRACE("<-- %p\n", *graphics);
2290
2291 return Ok;
2292 }
2293
2294 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
2295 {
2296 GpStatus ret;
2297 HDC hdc;
2298
2299 TRACE("(%p, %p)\n", hwnd, graphics);
2300
2301 hdc = GetDC(hwnd);
2302
2303 if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
2304 {
2305 ReleaseDC(hwnd, hdc);
2306 return ret;
2307 }
2308
2309 (*graphics)->hwnd = hwnd;
2310 (*graphics)->owndc = TRUE;
2311
2312 return Ok;
2313 }
2314
2315 /* FIXME: no icm handling */
2316 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
2317 {
2318 TRACE("(%p, %p)\n", hwnd, graphics);
2319
2320 return GdipCreateFromHWND(hwnd, graphics);
2321 }
2322
2323 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
2324 UINT access, IStream **stream)
2325 {
2326 DWORD dwMode;
2327 HRESULT ret;
2328
2329 TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
2330
2331 if(!stream || !filename)
2332 return InvalidParameter;
2333
2334 if(access & GENERIC_WRITE)
2335 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
2336 else if(access & GENERIC_READ)
2337 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
2338 else
2339 return InvalidParameter;
2340
2341 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
2342
2343 return hresult_to_status(ret);
2344 }
2345
2346 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
2347 {
2348 GraphicsContainerItem *cont, *next;
2349 GpStatus stat;
2350 TRACE("(%p)\n", graphics);
2351
2352 if(!graphics) return InvalidParameter;
2353 if(graphics->busy) return ObjectBusy;
2354
2355 if (graphics->image && graphics->image_type == ImageTypeMetafile)
2356 {
2357 stat = METAFILE_GraphicsDeleted((GpMetafile*)graphics->image);
2358 if (stat != Ok)
2359 return stat;
2360 }
2361
2362 if(graphics->owndc)
2363 ReleaseDC(graphics->hwnd, graphics->hdc);
2364
2365 LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
2366 list_remove(&cont->entry);
2367 delete_container(cont);
2368 }
2369
2370 GdipDeleteRegion(graphics->clip);
2371
2372 /* Native returns ObjectBusy on the second free, instead of crashing as we'd
2373 * do otherwise, but we can't have that in the test suite because it means
2374 * accessing freed memory. */
2375 graphics->busy = TRUE;
2376
2377 heap_free(graphics);
2378
2379 return Ok;
2380 }
2381
2382 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
2383 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2384 {
2385 GpStatus status;
2386 GpPath *path;
2387
2388 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2389 width, height, startAngle, sweepAngle);
2390
2391 if(!graphics || !pen || width <= 0 || height <= 0)
2392 return InvalidParameter;
2393
2394 if(graphics->busy)
2395 return ObjectBusy;
2396
2397 status = GdipCreatePath(FillModeAlternate, &path);
2398 if (status != Ok) return status;
2399
2400 status = GdipAddPathArc(path, x, y, width, height, startAngle, sweepAngle);
2401 if (status == Ok)
2402 status = GdipDrawPath(graphics, pen, path);
2403
2404 GdipDeletePath(path);
2405 return status;
2406 }
2407
2408 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
2409 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2410 {
2411 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2412 width, height, startAngle, sweepAngle);
2413
2414 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2415 }
2416
2417 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
2418 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
2419 {
2420 GpPointF pt[4];
2421
2422 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
2423 x2, y2, x3, y3, x4, y4);
2424
2425 if(!graphics || !pen)
2426 return InvalidParameter;
2427
2428 if(graphics->busy)
2429 return ObjectBusy;
2430
2431 pt[0].X = x1;
2432 pt[0].Y = y1;
2433 pt[1].X = x2;
2434 pt[1].Y = y2;
2435 pt[2].X = x3;
2436 pt[2].Y = y3;
2437 pt[3].X = x4;
2438 pt[3].Y = y4;
2439 return GdipDrawBeziers(graphics, pen, pt, 4);
2440 }
2441
2442 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
2443 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
2444 {
2445 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
2446 x2, y2, x3, y3, x4, y4);
2447
2448 return GdipDrawBezier(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2, (REAL)x3, (REAL)y3, (REAL)x4, (REAL)y4);
2449 }
2450
2451 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
2452 GDIPCONST GpPointF *points, INT count)
2453 {
2454 GpStatus status;
2455 GpPath *path;
2456
2457 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2458
2459 if(!graphics || !pen || !points || (count <= 0))
2460 return InvalidParameter;
2461
2462 if(graphics->busy)
2463 return ObjectBusy;
2464
2465 status = GdipCreatePath(FillModeAlternate, &path);
2466 if (status != Ok) return status;
2467
2468 status = GdipAddPathBeziers(path, points, count);
2469 if (status == Ok)
2470 status = GdipDrawPath(graphics, pen, path);
2471
2472 GdipDeletePath(path);
2473 return status;
2474 }
2475
2476 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2477 GDIPCONST GpPoint *points, INT count)
2478 {
2479 GpPointF *pts;
2480 GpStatus ret;
2481 INT i;
2482
2483 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2484
2485 if(!graphics || !pen || !points || (count <= 0))
2486 return InvalidParameter;
2487
2488 if(graphics->busy)
2489 return ObjectBusy;
2490
2491 pts = heap_alloc_zero(sizeof(GpPointF) * count);
2492 if(!pts)
2493 return OutOfMemory;
2494
2495 for(i = 0; i < count; i++){
2496 pts[i].X = (REAL)points[i].X;
2497 pts[i].Y = (REAL)points[i].Y;
2498 }
2499
2500 ret = GdipDrawBeziers(graphics,pen,pts,count);
2501
2502 heap_free(pts);
2503
2504 return ret;
2505 }
2506
2507 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2508 GDIPCONST GpPointF *points, INT count)
2509 {
2510 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2511
2512 return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2513 }
2514
2515 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2516 GDIPCONST GpPoint *points, INT count)
2517 {
2518 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2519
2520 return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2521 }
2522
2523 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2524 GDIPCONST GpPointF *points, INT count, REAL tension)
2525 {
2526 GpPath *path;
2527 GpStatus status;
2528
2529 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2530
2531 if(!graphics || !pen || !points || count <= 0)
2532 return InvalidParameter;
2533
2534 if(graphics->busy)
2535 return ObjectBusy;
2536
2537 status = GdipCreatePath(FillModeAlternate, &path);
2538 if (status != Ok) return status;
2539
2540 status = GdipAddPathClosedCurve2(path, points, count, tension);
2541 if (status == Ok)
2542 status = GdipDrawPath(graphics, pen, path);
2543
2544 GdipDeletePath(path);
2545
2546 return status;
2547 }
2548
2549 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2550 GDIPCONST GpPoint *points, INT count, REAL tension)
2551 {
2552 GpPointF *ptf;
2553 GpStatus stat;
2554 INT i;
2555
2556 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2557
2558 if(!points || count <= 0)
2559 return InvalidParameter;
2560
2561 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
2562 if(!ptf)
2563 return OutOfMemory;
2564
2565 for(i = 0; i < count; i++){
2566 ptf[i].X = (REAL)points[i].X;
2567 ptf[i].Y = (REAL)points[i].Y;
2568 }
2569
2570 stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2571
2572 heap_free(ptf);
2573
2574 return stat;
2575 }
2576
2577 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2578 GDIPCONST GpPointF *points, INT count)
2579 {
2580 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2581
2582 return GdipDrawCurve2(graphics,pen,points,count,1.0);
2583 }
2584
2585 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2586 GDIPCONST GpPoint *points, INT count)
2587 {
2588 GpPointF *pointsF;
2589 GpStatus ret;
2590 INT i;
2591
2592 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2593
2594 if(!points)
2595 return InvalidParameter;
2596
2597 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2598 if(!pointsF)
2599 return OutOfMemory;
2600
2601 for(i = 0; i < count; i++){
2602 pointsF[i].X = (REAL)points[i].X;
2603 pointsF[i].Y = (REAL)points[i].Y;
2604 }
2605
2606 ret = GdipDrawCurve(graphics,pen,pointsF,count);
2607 heap_free(pointsF);
2608
2609 return ret;
2610 }
2611
2612 /* Approximates cardinal spline with Bezier curves. */
2613 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2614 GDIPCONST GpPointF *points, INT count, REAL tension)
2615 {
2616 GpPath *path;
2617 GpStatus status;
2618
2619 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2620
2621 if(!graphics || !pen)
2622 return InvalidParameter;
2623
2624 if(graphics->busy)
2625 return ObjectBusy;
2626
2627 if(count < 2)
2628 return InvalidParameter;
2629
2630 status = GdipCreatePath(FillModeAlternate, &path);
2631 if (status != Ok) return status;
2632
2633 status = GdipAddPathCurve2(path, points, count, tension);
2634 if (status == Ok)
2635 status = GdipDrawPath(graphics, pen, path);
2636
2637 GdipDeletePath(path);
2638 return status;
2639 }
2640
2641 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2642 GDIPCONST GpPoint *points, INT count, REAL tension)
2643 {
2644 GpPointF *pointsF;
2645 GpStatus ret;
2646 INT i;
2647
2648 TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2649
2650 if(!points)
2651 return InvalidParameter;
2652
2653 pointsF = heap_alloc_zero(sizeof(GpPointF)*count);
2654 if(!pointsF)
2655 return OutOfMemory;
2656
2657 for(i = 0; i < count; i++){
2658 pointsF[i].X = (REAL)points[i].X;
2659 pointsF[i].Y = (REAL)points[i].Y;
2660 }
2661
2662 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2663 heap_free(pointsF);
2664
2665 return ret;
2666 }
2667
2668 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2669 GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2670 REAL tension)
2671 {
2672 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2673
2674 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2675 return InvalidParameter;
2676 }
2677
2678 return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2679 }
2680
2681 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2682 GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2683 REAL tension)
2684 {
2685 TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2686
2687 if(count < 0){
2688 return OutOfMemory;
2689 }
2690
2691 if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2692 return InvalidParameter;
2693 }
2694
2695 return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2696 }
2697
2698 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2699 REAL y, REAL width, REAL height)
2700 {
2701 GpPath *path;
2702 GpStatus status;
2703
2704 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2705
2706 if(!graphics || !pen)
2707 return InvalidParameter;
2708
2709 if(graphics->busy)
2710 return ObjectBusy;
2711
2712 status = GdipCreatePath(FillModeAlternate, &path);
2713 if (status != Ok) return status;
2714
2715 status = GdipAddPathEllipse(path, x, y, width, height);
2716 if (status == Ok)
2717 status = GdipDrawPath(graphics, pen, path);
2718
2719 GdipDeletePath(path);
2720 return status;
2721 }
2722
2723 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2724 INT y, INT width, INT height)
2725 {
2726 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2727
2728 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2729 }
2730
2731
2732 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2733 {
2734 UINT width, height;
2735
2736 TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2737
2738 if(!graphics || !image)
2739 return InvalidParameter;
2740
2741 GdipGetImageWidth(image, &width);
2742 GdipGetImageHeight(image, &height);
2743
2744 return GdipDrawImagePointRect(graphics, image, x, y,
2745 0.0, 0.0, (REAL)width, (REAL)height, UnitPixel);
2746 }
2747
2748 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2749 INT y)
2750 {
2751 TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2752
2753 return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2754 }
2755
2756 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2757 REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2758 GpUnit srcUnit)
2759 {
2760 GpPointF points[3];
2761 REAL scale_x, scale_y, width, height;
2762
2763 TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2764
2765 if (!graphics || !image) return InvalidParameter;
2766
2767 scale_x = units_scale(srcUnit, graphics->unit, graphics->xres);
2768 scale_x *= graphics->xres / image->xres;
2769 scale_y = units_scale(srcUnit, graphics->unit, graphics->yres);
2770 scale_y *= graphics->yres / image->yres;
2771 width = srcwidth * scale_x;
2772 height = srcheight * scale_y;
2773
2774 points[0].X = points[2].X = x;
2775 points[0].Y = points[1].Y = y;
2776 points[1].X = x + width;
2777 points[2].Y = y + height;
2778
2779 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2780 srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2781 }
2782
2783 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2784 INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2785 GpUnit srcUnit)
2786 {
2787 return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2788 }
2789
2790 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2791 GDIPCONST GpPointF *dstpoints, INT count)
2792 {
2793 UINT width, height;
2794
2795 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2796
2797 if(!image)
2798 return InvalidParameter;
2799
2800 GdipGetImageWidth(image, &width);
2801 GdipGetImageHeight(image, &height);
2802
2803 return GdipDrawImagePointsRect(graphics, image, dstpoints, count, 0, 0,
2804 width, height, UnitPixel, NULL, NULL, NULL);
2805 }
2806
2807 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2808 GDIPCONST GpPoint *dstpoints, INT count)
2809 {
2810 GpPointF ptf[3];
2811
2812 TRACE("(%p, %p, %p, %d)\n", graphics, image, dstpoints, count);
2813
2814 if (count != 3 || !dstpoints)
2815 return InvalidParameter;
2816
2817 ptf[0].X = (REAL)dstpoints[0].X;
2818 ptf[0].Y = (REAL)dstpoints[0].Y;
2819 ptf[1].X = (REAL)dstpoints[1].X;
2820 ptf[1].Y = (REAL)dstpoints[1].Y;
2821 ptf[2].X = (REAL)dstpoints[2].X;
2822 ptf[2].Y = (REAL)dstpoints[2].Y;
2823
2824 return GdipDrawImagePoints(graphics, image, ptf, count);
2825 }
2826
2827 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
2828 unsigned int dataSize, const unsigned char *pStr, void *userdata)
2829 {
2830 GdipPlayMetafileRecord(userdata, record_type, flags, dataSize, pStr);
2831 return TRUE;
2832 }
2833
2834 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2835 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2836 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2837 DrawImageAbort callback, VOID * callbackData)
2838 {
2839 GpPointF ptf[4];
2840 POINT pti[4];
2841 GpStatus stat;
2842
2843 TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2844 count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2845 callbackData);
2846
2847 if (count > 3)
2848 return NotImplemented;
2849
2850 if(!graphics || !image || !points || count != 3)
2851 return InvalidParameter;
2852
2853 TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2854 debugstr_pointf(&points[2]));
2855
2856 memcpy(ptf, points, 3 * sizeof(GpPointF));
2857
2858 /* Ensure source width/height is positive */
2859 if (srcwidth < 0)
2860 {
2861 GpPointF tmp = ptf[1];
2862 srcx = srcx + srcwidth;
2863 srcwidth = -srcwidth;
2864 ptf[2].X = ptf[2].X + ptf[1].X - ptf[0].X;
2865 ptf[2].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2866 ptf[1] = ptf[0];
2867 ptf[0] = tmp;
2868 }
2869
2870 if (srcheight < 0)
2871 {
2872 GpPointF tmp = ptf[2];
2873 srcy = srcy + srcheight;
2874 srcheight = -srcheight;
2875 ptf[1].X = ptf[1].X + ptf[2].X - ptf[0].X;
2876 ptf[1].Y = ptf[1].Y + ptf[2].Y - ptf[0].Y;
2877 ptf[2] = ptf[0];
2878 ptf[0] = tmp;
2879 }
2880
2881 ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2882 ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2883 if (!srcwidth || !srcheight || (ptf[3].X == ptf[0].X && ptf[3].Y == ptf[0].Y))
2884 return Ok;
2885 transform_and_round_points(graphics, pti, ptf, 4);
2886
2887 TRACE("%s %s %s %s\n", wine_dbgstr_point(&pti[0]), wine_dbgstr_point(&pti[1]),
2888 wine_dbgstr_point(&pti[2]), wine_dbgstr_point(&pti[3]));
2889
2890 srcx = units_to_pixels(srcx, srcUnit, image->xres);
2891 srcy = units_to_pixels(srcy, srcUnit, image->yres);
2892 srcwidth = units_to_pixels(srcwidth, srcUnit, image->xres);
2893 srcheight = units_to_pixels(srcheight, srcUnit, image->yres);
2894 TRACE("src pixels: %f,%f %fx%f\n", srcx, srcy, srcwidth, srcheight);
2895
2896 if (image->type == ImageTypeBitmap)
2897 {
2898 GpBitmap* bitmap = (GpBitmap*)image;
2899 BOOL do_resampling = FALSE;
2900 BOOL use_software = FALSE;
2901
2902 TRACE("graphics: %.2fx%.2f dpi, fmt %#x, scale %f, image: %.2fx%.2f dpi, fmt %#x, color %08x\n",
2903 graphics->xres, graphics->yres,
2904 graphics->image && graphics->image->type == ImageTypeBitmap ? ((GpBitmap *)graphics->image)->format : 0,
2905 graphics->scale, image->xres, image->yres, bitmap->format,
2906 imageAttributes ? imageAttributes->outside_color : 0);
2907
2908 if (ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2909 ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2910 srcx < 0 || srcy < 0 ||
2911 srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2912 do_resampling = TRUE;
2913
2914 if (imageAttributes || graphics->alpha_hdc || do_resampling ||
2915 (graphics->image && graphics->image->type == ImageTypeBitmap))
2916 use_software = TRUE;
2917
2918 if (use_software)
2919 {
2920 RECT dst_area;
2921 GpRectF graphics_bounds;
2922 GpRect src_area;
2923 int i, x, y, src_stride, dst_stride;
2924 GpMatrix dst_to_src;
2925 REAL m11, m12, m21, m22, mdx, mdy;
2926 LPBYTE src_data, dst_data, dst_dyn_data=NULL;
2927 BitmapData lockeddata;
2928 InterpolationMode interpolation = graphics->interpolation;
2929 PixelOffsetMode offset_mode = graphics->pixeloffset;
2930 GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2931 REAL x_dx, x_dy, y_dx, y_dy;
2932 static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2933
2934 if (!imageAttributes)
2935 imageAttributes = &defaultImageAttributes;
2936
2937 dst_area.left = dst_area.right = pti[0].x;
2938 dst_area.top = dst_area.bottom = pti[0].y;
2939 for (i=1; i<4; i++)
2940 {
2941 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2942 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2943 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2944 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2945 }
2946
2947 stat = get_graphics_bounds(graphics, &graphics_bounds);
2948 if (stat != Ok) return stat;
2949
2950 if (graphics_bounds.X > dst_area.left) dst_area.left = floorf(graphics_bounds.X);
2951 if (graphics_bounds.Y > dst_area.top) dst_area.top = floorf(graphics_bounds.Y);
2952 if (graphics_bounds.X + graphics_bounds.Width < dst_area.right) dst_area.right = ceilf(graphics_bounds.X + graphics_bounds.Width);
2953 if (graphics_bounds.Y + graphics_bounds.Height < dst_area.bottom) dst_area.bottom = ceilf(graphics_bounds.Y + graphics_bounds.Height);
2954
2955 TRACE("dst_area: %s\n", wine_dbgstr_rect(&dst_area));
2956
2957 if (IsRectEmpty(&dst_area)) return Ok;
2958
2959 m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2960 m21 = (ptf[2].X - ptf[0].X) / srcheight;
2961 mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2962 m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2963 m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2964 mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2965
2966 GdipSetMatrixElements(&dst_to_src, m11, m12, m21, m22, mdx, mdy);
2967
2968 stat = GdipInvertMatrix(&dst_to_src);
2969 if (stat != Ok) return stat;
2970
2971 if (do_resampling)
2972 {
2973 get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2974 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2975 }
2976 else
2977 {
2978 /* Make sure src_area is equal in size to dst_area. */
2979 src_area.X = srcx + dst_area.left - pti[0].x;
2980 src_area.Y = srcy + dst_area.top - pti[0].y;
2981 src_area.Width = dst_area.right - dst_area.left;
2982 src_area.Height = dst_area.bottom - dst_area.top;
2983 }
2984
2985 TRACE("src_area: %d x %d\n", src_area.Width, src_area.Height);
2986
2987 src_data = heap_alloc_zero(sizeof(ARGB) * src_area.Width * src_area.Height);
2988 if (!src_data)
2989 return OutOfMemory;
2990 src_stride = sizeof(ARGB) * src_area.Width;
2991
2992 /* Read the bits we need from the source bitmap into a compatible buffer. */
2993 lockeddata.Width = src_area.Width;
2994 lockeddata.Height = src_area.Height;
2995 lockeddata.Stride = src_stride;
2996 lockeddata.Scan0 = src_data;
2997 if (!do_resampling && bitmap->format == PixelFormat32bppPARGB)
2998 lockeddata.PixelFormat = apply_image_attributes(imageAttributes, NULL, 0, 0, 0, ColorAdjustTypeBitmap, bitmap->format);
2999 else
3000 lockeddata.PixelFormat = PixelFormat32bppARGB;
3001
3002 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
3003 lockeddata.PixelFormat, &lockeddata);
3004
3005 if (stat == Ok)
3006 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
3007
3008 if (stat != Ok)
3009 {
3010 heap_free(src_data);
3011 return stat;
3012 }
3013
3014 apply_image_attributes(imageAttributes, src_data,
3015 src_area.Width, src_area.Height,
3016 src_stride, ColorAdjustTypeBitmap, lockeddata.PixelFormat);
3017
3018 if (do_resampling)
3019 {
3020 /* Transform the bits as needed to the destination. */
3021 dst_data = dst_dyn_data = heap_alloc_zero(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
3022 if (!dst_data)
3023 {
3024 heap_free(src_data);
3025 return OutOfMemory;
3026 }
3027
3028 dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
3029
3030 GdipTransformMatrixPoints(&dst_to_src, dst_to_src_points, 3);
3031
3032 x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
3033 x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
3034 y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
3035 y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
3036
3037 for (x=dst_area.left; x<dst_area.right; x++)
3038 {
3039 for (y=dst_area.top; y<dst_area.bottom; y++)
3040 {
3041 GpPointF src_pointf;
3042 ARGB *dst_color;
3043
3044 src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
3045 src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
3046
3047 dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
3048
3049 if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
3050 *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf,
3051 imageAttributes, interpolation, offset_mode);
3052 else
3053 *dst_color = 0;
3054 }
3055 }
3056 }
3057 else
3058 {
3059 dst_data = src_data;
3060 dst_stride = src_stride;
3061 }
3062
3063 stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
3064 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride,
3065 lockeddata.PixelFormat);
3066
3067 heap_free(src_data);
3068
3069 heap_free(dst_dyn_data);
3070
3071 return stat;
3072 }
3073 else
3074 {
3075 HDC hdc;
3076 BOOL temp_hdc = FALSE, temp_bitmap = FALSE;
3077 HBITMAP hbitmap, old_hbm=NULL;
3078
3079 if (!(bitmap->format == PixelFormat16bppRGB555 ||
3080 bitmap->format == PixelFormat24bppRGB ||
3081 bitmap->format == PixelFormat32bppRGB ||
3082 bitmap->format == PixelFormat32bppPARGB))
3083 {
3084 BITMAPINFOHEADER bih;
3085 BYTE *temp_bits;
3086 PixelFormat dst_format;
3087
3088 /* we can't draw a bitmap of this format directly */
3089 hdc = CreateCompatibleDC(0);
3090 temp_hdc = TRUE;
3091 temp_bitmap = TRUE;
3092
3093 bih.biSize = sizeof(BITMAPINFOHEADER);
3094 bih.biWidth = bitmap->width;
3095 bih.biHeight = -bitmap->height;
3096 bih.biPlanes = 1;
3097 bih.biBitCount = 32;
3098 bih.biCompression = BI_RGB;
3099 bih.biSizeImage = 0;
3100 bih.biXPelsPerMeter = 0;
3101 bih.biYPelsPerMeter = 0;
3102 bih.biClrUsed = 0;
3103 bih.biClrImportant = 0;
3104
3105 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
3106 (void**)&temp_bits, NULL, 0);
3107
3108 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3109 dst_format = PixelFormat32bppPARGB;
3110 else
3111 dst_format = PixelFormat32bppRGB;
3112
3113 convert_pixels(bitmap->width, bitmap->height,
3114 bitmap->width*4, temp_bits, dst_format,
3115 bitmap->stride, bitmap->bits, bitmap->format,
3116 bitmap->image.palette);
3117 }
3118 else
3119 {
3120 if (bitmap->hbitmap)
3121 hbitmap = bitmap->hbitmap;
3122 else
3123 {
3124 GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
3125 temp_bitmap = TRUE;
3126 }
3127
3128 hdc = bitmap->hdc;
3129 temp_hdc = (hdc == 0);
3130 }
3131
3132 if (temp_hdc)
3133 {
3134 if (!hdc) hdc = CreateCompatibleDC(0);
3135 old_hbm = SelectObject(hdc, hbitmap);
3136 }
3137
3138 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
3139 {
3140 gdi_alpha_blend(graphics, pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
3141 hdc, srcx, srcy, srcwidth, srcheight);
3142 }
3143 else
3144 {
3145 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
3146 hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
3147 }
3148
3149 if (temp_hdc)
3150 {
3151 SelectObject(hdc, old_hbm);
3152 DeleteDC(hdc);
3153 }
3154
3155 if (temp_bitmap)
3156 DeleteObject(hbitmap);
3157 }
3158 }
3159 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
3160 {
3161 GpRectF rc;
3162
3163 rc.X = srcx;
3164 rc.Y = srcy;
3165 rc.Width = srcwidth;
3166 rc.Height = srcheight;
3167
3168 return GdipEnumerateMetafileSrcRectDestPoints(graphics, (GpMetafile*)image,
3169 points, count, &rc, srcUnit, play_metafile_proc, image, imageAttributes);
3170 }
3171 else
3172 {
3173 WARN("GpImage with nothing we can draw (metafile in wrong state?)\n");
3174 return InvalidParameter;
3175 }
3176
3177 return Ok;
3178 }
3179
3180 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
3181 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
3182 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
3183 DrawImageAbort callback, VOID * callbackData)
3184 {
3185 GpPointF pointsF[3];
3186 INT i;
3187
3188 TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
3189 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
3190 callbackData);
3191
3192 if(!points || count!=3)
3193 return InvalidParameter;
3194
3195 for(i = 0; i < count; i++){
3196 pointsF[i].X = (REAL)points[i].X;
3197 pointsF[i].Y = (REAL)points[i].Y;
3198 }
3199
3200 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
3201 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
3202 callback, callbackData);
3203 }
3204
3205 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
3206 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
3207 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
3208 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
3209 VOID * callbackData)
3210 {
3211 GpPointF points[3];
3212
3213 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
3214 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3215 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3216
3217 points[0].X = dstx;
3218 points[0].Y = dsty;
3219 points[1].X = dstx + dstwidth;
3220 points[1].Y = dsty;
3221 points[2].X = dstx;
3222 points[2].Y = dsty + dstheight;
3223
3224 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3225 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
3226 }
3227
3228 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
3229 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
3230 INT srcwidth, INT srcheight, GpUnit srcUnit,
3231 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
3232 VOID * callbackData)
3233 {
3234 GpPointF points[3];
3235
3236 TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
3237 graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
3238 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3239
3240 points[0].X = dstx;
3241 points[0].Y = dsty;
3242 points[1].X = dstx + dstwidth;
3243 points[1].Y = dsty;
3244 points[2].X = dstx;
3245 points[2].Y = dsty + dstheight;
3246
3247 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
3248 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
3249 }
3250
3251 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
3252 REAL x, REAL y, REAL width, REAL height)
3253 {
3254 RectF bounds;
3255 GpUnit unit;
3256 GpStatus ret;
3257
3258 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
3259
3260 if(!graphics || !image)
3261 return InvalidParameter;
3262
3263 ret = GdipGetImageBounds(image, &bounds, &unit);
3264 if(ret != Ok)
3265 return ret;
3266
3267 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
3268 bounds.X, bounds.Y, bounds.Width, bounds.Height,
3269 unit, NULL, NULL, NULL);
3270 }
3271
3272 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
3273 INT x, INT y, INT width, INT height)
3274 {
3275 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
3276
3277 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
3278 }
3279
3280 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
3281 REAL y1, REAL x2, REAL y2)
3282 {
3283 GpPointF pt[2];
3284
3285 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
3286
3287 pt[0].X = x1;
3288 pt[0].Y = y1;
3289 pt[1].X = x2;
3290 pt[1].Y = y2;
3291 return GdipDrawLines(graphics, pen, pt, 2);
3292 }
3293
3294 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
3295 INT y1, INT x2, INT y2)
3296 {
3297 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
3298
3299 return GdipDrawLine(graphics, pen, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
3300 }
3301
3302 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
3303 GpPointF *points, INT count)
3304 {
3305 GpStatus status;
3306 GpPath *path;
3307
3308 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3309
3310 if(!pen || !graphics || (count < 2))
3311 return InvalidParameter;
3312
3313 if(graphics->busy)
3314 return ObjectBusy;
3315
3316 status = GdipCreatePath(FillModeAlternate, &path);
3317 if (status != Ok) return status;
3318
3319 status = GdipAddPathLine2(path, points, count);
3320 if (status == Ok)
3321 status = GdipDrawPath(graphics, pen, path);
3322
3323 GdipDeletePath(path);
3324 return status;
3325 }
3326
3327 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
3328 GpPoint *points, INT count)
3329 {
3330 GpStatus retval;
3331 GpPointF *ptf;
3332 int i;
3333
3334 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3335
3336 ptf = heap_alloc_zero(count * sizeof(GpPointF));
3337 if(!ptf) return OutOfMemory;
3338
3339 for(i = 0; i < count; i ++){
3340 ptf[i].X = (REAL) points[i].X;
3341 ptf[i].Y = (REAL) points[i].Y;
3342 }
3343
3344 retval = GdipDrawLines(graphics, pen, ptf, count);
3345
3346 heap_free(ptf);
3347 return retval;
3348 }
3349
3350 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
3351 {
3352 INT save_state;
3353 GpStatus retval;
3354 HRGN hrgn=NULL;
3355
3356 TRACE("(%p, %p, %p)\n", graphics, pen, path);
3357
3358 if(!pen || !graphics)
3359 return InvalidParameter;
3360
3361 if(graphics->busy)
3362 return ObjectBusy;
3363
3364 if (!graphics->hdc)
3365 {
3366 FIXME("graphics object has no HDC\n");
3367 return Ok;
3368 }
3369
3370 save_state = prepare_dc(graphics, pen);
3371
3372 retval = get_clip_hrgn(graphics, &hrgn);
3373
3374 if (retval != Ok)
3375 goto end;
3376
3377 if (hrgn)
3378 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3379
3380 retval = draw_poly(graphics, pen, path->pathdata.Points,
3381 path->pathdata.Types, path->pathdata.Count, TRUE);
3382
3383 end:
3384 restore_dc(graphics, save_state);
3385 DeleteObject(hrgn);
3386
3387 return retval;
3388 }
3389
3390 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
3391 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3392 {
3393 GpStatus status;
3394 GpPath *path;
3395
3396 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
3397 width, height, startAngle, sweepAngle);
3398
3399 if(!graphics || !pen)
3400 return InvalidParameter;
3401
3402 if(graphics->busy)
3403 return ObjectBusy;
3404
3405 status = GdipCreatePath(FillModeAlternate, &path);
3406 if (status != Ok) return status;
3407
3408 status = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3409 if (status == Ok)
3410 status = GdipDrawPath(graphics, pen, path);
3411
3412 GdipDeletePath(path);
3413 return status;
3414 }
3415
3416 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3417 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3418 {
3419 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3420 width, height, startAngle, sweepAngle);
3421
3422 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3423 }
3424
3425 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3426 REAL y, REAL width, REAL height)
3427 {
3428 GpStatus status;
3429 GpPath *path;
3430
3431 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3432
3433 if(!pen || !graphics)
3434 return InvalidParameter;
3435
3436 if(graphics->busy)
3437 return ObjectBusy;
3438
3439 status = GdipCreatePath(FillModeAlternate, &path);
3440 if (status != Ok) return status;
3441
3442 status = GdipAddPathRectangle(path, x, y, width, height);
3443 if (status == Ok)
3444 status = GdipDrawPath(graphics, pen, path);
3445
3446 GdipDeletePath(path);
3447 return status;
3448 }
3449
3450 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3451 INT y, INT width, INT height)
3452 {
3453 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3454
3455 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3456 }
3457
3458 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3459 GDIPCONST GpRectF* rects, INT count)
3460 {
3461 GpStatus status;
3462 GpPath *path;
3463
3464 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3465
3466 if(!graphics || !pen || !rects || count < 1)
3467 return InvalidParameter;
3468
3469 if(graphics->busy)
3470 return ObjectBusy;
3471
3472 status = GdipCreatePath(FillModeAlternate, &path);
3473 if (status != Ok) return status;
3474
3475 status = GdipAddPathRectangles(path, rects, count);
3476 if (status == Ok)
3477 status = GdipDrawPath(graphics, pen, path);
3478
3479 GdipDeletePath(path);
3480 return status;
3481 }
3482
3483 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3484 GDIPCONST GpRect* rects, INT count)
3485 {
3486 GpRectF *rectsF;
3487 GpStatus ret;
3488 INT i;
3489
3490 TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3491
3492 if(!rects || count<=0)
3493 return InvalidParameter;
3494
3495 rectsF = heap_alloc_zero(sizeof(GpRectF) * count);
3496 if(!rectsF)
3497 return OutOfMemory;
3498
3499 for(i = 0;i < count;i++){
3500 rectsF[i].X = (REAL)rects[i].X;
3501 rectsF[i].Y = (REAL)rects[i].Y;
3502 rectsF[i].Width = (REAL)rects[i].Width;
3503 rectsF[i].Height = (REAL)rects[i].Height;
3504 }
3505
3506 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3507 heap_free(rectsF);
3508
3509 return ret;
3510 }
3511
3512 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3513 GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3514 {
3515 GpPath *path;
3516 GpStatus status;
3517
3518 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3519 count, tension, fill);
3520
3521 if(!graphics || !brush || !points)
3522 return InvalidParameter;
3523
3524 if(graphics->busy)
3525 return ObjectBusy;
3526
3527 if(count == 1) /* Do nothing */
3528 return Ok;
3529
3530 status = GdipCreatePath(fill, &path);
3531 if (status != Ok) return status;
3532
3533 status = GdipAddPathClosedCurve2(path, points, count, tension);
3534 if (status == Ok)
3535 status = GdipFillPath(graphics, brush, path);
3536
3537 GdipDeletePath(path);
3538 return status;
3539 }
3540
3541 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3542 GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3543 {
3544 GpPointF *ptf;
3545 GpStatus stat;
3546 INT i;
3547
3548 TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3549 count, tension, fill);
3550
3551 if(!points || count == 0)
3552 return InvalidParameter;
3553
3554 if(count == 1) /* Do nothing */
3555 return Ok;
3556
3557 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
3558 if(!ptf)
3559 return OutOfMemory;
3560
3561 for(i = 0;i < count;i++){
3562 ptf[i].X = (REAL)points[i].X;
3563 ptf[i].Y = (REAL)points[i].Y;
3564 }
3565
3566 stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3567
3568 heap_free(ptf);
3569
3570 return stat;
3571 }
3572
3573 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3574 GDIPCONST GpPointF *points, INT count)
3575 {
3576 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3577 return GdipFillClosedCurve2(graphics, brush, points, count,
3578 0.5f, FillModeAlternate);
3579 }
3580
3581 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3582 GDIPCONST GpPoint *points, INT count)
3583 {
3584 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3585 return GdipFillClosedCurve2I(graphics, brush, points, count,
3586 0.5f, FillModeAlternate);
3587 }
3588
3589 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3590 REAL y, REAL width, REAL height)
3591 {
3592 GpStatus stat;
3593 GpPath *path;
3594
3595 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3596
3597 if(!graphics || !brush)
3598 return InvalidParameter;
3599
3600 if(graphics->busy)
3601 return ObjectBusy;
3602
3603 stat = GdipCreatePath(FillModeAlternate, &path);
3604
3605 if (stat == Ok)
3606 {
3607 stat = GdipAddPathEllipse(path, x, y, width, height);
3608
3609 if (stat == Ok)
3610 stat = GdipFillPath(graphics, brush, path);
3611
3612 GdipDeletePath(path);
3613 }
3614
3615 return stat;
3616 }
3617
3618 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3619 INT y, INT width, INT height)
3620 {
3621 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3622
3623 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3624 }
3625
3626 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3627 {
3628 INT save_state;
3629 GpStatus retval;
3630 HRGN hrgn=NULL;
3631
3632 if(!graphics->hdc || !brush_can_fill_path(brush))
3633 return NotImplemented;
3634
3635 save_state = SaveDC(graphics->hdc);
3636 EndPath(graphics->hdc);
3637 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3638 : WINDING));
3639
3640 retval = get_clip_hrgn(graphics, &hrgn);
3641
3642 if (retval != Ok)
3643 goto end;
3644
3645 if (hrgn)
3646 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3647
3648 BeginPath(graphics->hdc);
3649 retval = draw_poly(graphics, NULL, path->pathdata.Points,
3650 path->pathdata.Types, path->pathdata.Count, FALSE);
3651
3652 if(retval != Ok)
3653 goto end;
3654
3655 EndPath(graphics->hdc);
3656 brush_fill_path(graphics, brush);
3657
3658 retval = Ok;
3659
3660 end:
3661 RestoreDC(graphics->hdc, save_state);
3662 DeleteObject(hrgn);
3663
3664 return retval;
3665 }
3666
3667 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3668 {
3669 GpStatus stat;
3670 GpRegion *rgn;
3671
3672 if (!brush_can_fill_pixels(brush))
3673 return NotImplemented;
3674
3675 /* FIXME: This could probably be done more efficiently without regions. */
3676
3677 stat = GdipCreateRegionPath(path, &rgn);
3678
3679 if (stat == Ok)
3680 {
3681 stat = GdipFillRegion(graphics, brush, rgn);
3682
3683 GdipDeleteRegion(rgn);
3684 }
3685
3686 return stat;
3687 }
3688
3689 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3690 {
3691 GpStatus stat = NotImplemented;
3692
3693 TRACE("(%p, %p, %p)\n", graphics, brush, path);
3694
3695 if(!brush || !graphics || !path)
3696 return InvalidParameter;
3697
3698 if(graphics->busy)
3699 return ObjectBusy;
3700
3701 if (!graphics->image && !graphics->alpha_hdc)
3702 stat = GDI32_GdipFillPath(graphics, brush, path);
3703
3704 if (stat == NotImplemented)
3705 stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3706
3707 if (stat == NotImplemented)
3708 {
3709 FIXME("Not implemented for brushtype %i\n", brush->bt);
3710 stat = Ok;
3711 }
3712
3713 return stat;
3714 }
3715
3716 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3717 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3718 {
3719 GpStatus stat;
3720 GpPath *path;
3721
3722 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3723 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3724
3725 if(!graphics || !brush)
3726 return InvalidParameter;
3727
3728 if(graphics->busy)
3729 return ObjectBusy;
3730
3731 stat = GdipCreatePath(FillModeAlternate, &path);
3732
3733 if (stat == Ok)
3734 {
3735 stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3736
3737 if (stat == Ok)
3738 stat = GdipFillPath(graphics, brush, path);
3739
3740 GdipDeletePath(path);
3741 }
3742
3743 return stat;
3744 }
3745
3746 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3747 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3748 {
3749 TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3750 graphics, brush, x, y, width, height, startAngle, sweepAngle);
3751
3752 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3753 }
3754
3755 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3756 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3757 {
3758 GpStatus stat;
3759 GpPath *path;
3760
3761 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3762
3763 if(!graphics || !brush || !points || !count)
3764 return InvalidParameter;
3765
3766 if(graphics->busy)
3767 return ObjectBusy;
3768
3769 stat = GdipCreatePath(fillMode, &path);
3770
3771 if (stat == Ok)
3772 {
3773 stat = GdipAddPathPolygon(path, points, count);
3774
3775 if (stat == Ok)
3776 stat = GdipFillPath(graphics, brush, path);
3777
3778 GdipDeletePath(path);
3779 }
3780
3781 return stat;
3782 }
3783
3784 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3785 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3786 {
3787 GpStatus stat;
3788 GpPath *path;
3789
3790 TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3791
3792 if(!graphics || !brush || !points || !count)
3793 return InvalidParameter;
3794
3795 if(graphics->busy)
3796 return ObjectBusy;
3797
3798 stat = GdipCreatePath(fillMode, &path);
3799
3800 if (stat == Ok)
3801 {
3802 stat = GdipAddPathPolygonI(path, points, count);
3803
3804 if (stat == Ok)
3805 stat = GdipFillPath(graphics, brush, path);
3806
3807 GdipDeletePath(path);
3808 }
3809
3810 return stat;
3811 }
3812
3813 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3814 GDIPCONST GpPointF *points, INT count)
3815 {
3816 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3817
3818 return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3819 }
3820
3821 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3822 GDIPCONST GpPoint *points, INT count)
3823 {
3824 TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3825
3826 return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3827 }
3828
3829 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3830 REAL x, REAL y, REAL width, REAL height)
3831 {
3832 GpRectF rect;
3833
3834 TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3835
3836 rect.X = x;
3837 rect.Y = y;
3838 rect.Width = width;
3839 rect.Height = height;
3840
3841 return GdipFillRectangles(graphics, brush, &rect, 1);
3842 }
3843
3844 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3845 INT x, INT y, INT width, INT height)
3846 {
3847 GpRectF rect;
3848
3849 TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3850
3851 rect.X = (REAL)x;
3852 rect.Y = (REAL)y;
3853 rect.Width = (REAL)width;
3854 rect.Height = (REAL)height;
3855
3856 return GdipFillRectangles(graphics, brush, &rect, 1);
3857 }
3858
3859 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3860 INT count)
3861 {
3862 GpStatus status;
3863 GpPath *path;
3864
3865 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3866
3867 if(!graphics || !brush || !rects || count <= 0)
3868 return InvalidParameter;
3869
3870 if (graphics->image && graphics->image->type == ImageTypeMetafile)
3871 {
3872 status = METAFILE_FillRectangles((GpMetafile*)graphics->image, brush, rects, count);
3873 /* FIXME: Add gdi32 drawing. */
3874 return status;
3875 }
3876
3877 status = GdipCreatePath(FillModeAlternate, &path);
3878 if (status != Ok) return status;
3879
3880 status = GdipAddPathRectangles(path, rects, count);
3881 if (status == Ok)
3882 status = GdipFillPath(graphics, brush, path);
3883
3884 GdipDeletePath(path);
3885 return status;
3886 }
3887
3888 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3889 INT count)
3890 {
3891 GpRectF *rectsF;
3892 GpStatus ret;
3893 INT i;
3894
3895 TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3896
3897 if(!rects || count <= 0)
3898 return InvalidParameter;
3899
3900 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
3901 if(!rectsF)
3902 return OutOfMemory;
3903
3904 for(i = 0; i < count; i++){
3905 rectsF[i].X = (REAL)rects[i].X;
3906 rectsF[i].Y = (REAL)rects[i].Y;
3907 rectsF[i].X = (REAL)rects[i].Width;
3908 rectsF[i].Height = (REAL)rects[i].Height;
3909 }
3910
3911 ret = GdipFillRectangles(graphics,brush,rectsF,count);
3912 heap_free(rectsF);
3913
3914 return ret;
3915 }
3916
3917 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3918 GpRegion* region)
3919 {
3920 INT save_state;
3921 GpStatus status;
3922 HRGN hrgn;
3923 RECT rc;
3924
3925 if(!graphics->hdc || !brush_can_fill_path(brush))
3926 return NotImplemented;
3927
3928 status = GdipGetRegionHRgn(region, graphics, &hrgn);
3929 if(status != Ok)
3930 return status;
3931
3932 save_state = SaveDC(graphics->hdc);
3933 EndPath(graphics->hdc);
3934
3935 ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3936
3937 if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3938 {
3939 BeginPath(graphics->hdc);
3940 Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3941 EndPath(graphics->hdc);
3942
3943 brush_fill_path(graphics, brush);
3944 }
3945
3946 RestoreDC(graphics->hdc, save_state);
3947
3948 DeleteObject(hrgn);
3949
3950 return Ok;
3951 }
3952
3953 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3954 GpRegion* region)
3955 {
3956 GpStatus stat;
3957 GpRegion *temp_region;
3958 GpMatrix world_to_device;
3959 GpRectF graphics_bounds;
3960 DWORD *pixel_data;
3961 HRGN hregion;
3962 RECT bound_rect;
3963 GpRect gp_bound_rect;
3964
3965 if (!brush_can_fill_pixels(brush))
3966 return NotImplemented;
3967
3968 stat = get_graphics_bounds(graphics, &graphics_bounds);
3969
3970 if (stat == Ok)
3971 stat = GdipCloneRegion(region, &temp_region);
3972
3973 if (stat == Ok)
3974 {
3975 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3976 CoordinateSpaceWorld, &world_to_device);
3977
3978 if (stat == Ok)
3979 stat = GdipTransformRegion(temp_region, &world_to_device);
3980
3981 if (stat == Ok)
3982 stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3983
3984 if (stat == Ok)
3985 stat = GdipGetRegionHRgn(temp_region, NULL, &hregion);
3986
3987 GdipDeleteRegion(temp_region);
3988 }
3989
3990 if (stat == Ok && GetRgnBox(hregion, &bound_rect) == NULLREGION)
3991 {
3992 DeleteObject(hregion);
3993 return Ok;
3994 }
3995
3996 if (stat == Ok)
3997 {
3998 gp_bound_rect.X = bound_rect.left;
3999 gp_bound_rect.Y = bound_rect.top;
4000 gp_bound_rect.Width = bound_rect.right - bound_rect.left;
4001 gp_bound_rect.Height = bound_rect.bottom - bound_rect.top;
4002
4003 pixel_data = heap_alloc_zero(sizeof(*pixel_data) * gp_bound_rect.Width * gp_bound_rect.Height);
4004 if (!pixel_data)
4005 stat = OutOfMemory;
4006
4007 if (stat == Ok)
4008 {
4009 stat = brush_fill_pixels(graphics, brush, pixel_data,
4010 &gp_bound_rect, gp_bound_rect.Width);
4011
4012 if (stat == Ok)
4013 stat = alpha_blend_pixels_hrgn(graphics, gp_bound_rect.X,
4014 gp_bound_rect.Y, (BYTE*)pixel_data, gp_bound_rect.Width,
4015 gp_bound_rect.Height, gp_bound_rect.Width * 4, hregion,
4016 PixelFormat32bppARGB);
4017
4018 heap_free(pixel_data);
4019 }
4020
4021 DeleteObject(hregion);
4022 }
4023
4024 return stat;
4025 }
4026
4027 /*****************************************************************************
4028 * GdipFillRegion [GDIPLUS.@]
4029 */
4030 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
4031 GpRegion* region)
4032 {
4033 GpStatus stat = NotImplemented;
4034
4035 TRACE("(%p, %p, %p)\n", graphics, brush, region);
4036
4037 if (!(graphics && brush && region))
4038 return InvalidParameter;
4039
4040 if(graphics->busy)
4041 return ObjectBusy;
4042
4043 if (!graphics->image && !graphics->alpha_hdc)
4044 stat = GDI32_GdipFillRegion(graphics, brush, region);
4045
4046 if (stat == NotImplemented)
4047 stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
4048
4049 if (stat == NotImplemented)
4050 {
4051 FIXME("not implemented for brushtype %i\n", brush->bt);
4052 stat = Ok;
4053 }
4054
4055 return stat;
4056 }
4057
4058 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
4059 {
4060 TRACE("(%p,%u)\n", graphics, intention);
4061
4062 if(!graphics)
4063 return InvalidParameter;
4064
4065 if(graphics->busy)
4066 return ObjectBusy;
4067
4068 /* We have no internal operation queue, so there's no need to clear it. */
4069
4070 if (graphics->hdc)
4071 GdiFlush();
4072
4073 return Ok;
4074 }
4075
4076 /*****************************************************************************
4077 * GdipGetClipBounds [GDIPLUS.@]
4078 */
4079 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
4080 {
4081 GpStatus status;
4082 GpRegion *clip;
4083
4084 TRACE("(%p, %p)\n", graphics, rect);
4085
4086 if(!graphics)
4087 return InvalidParameter;
4088
4089 if(graphics->busy)
4090 return ObjectBusy;
4091
4092 status = GdipCreateRegion(&clip);
4093 if (status != Ok) return status;
4094
4095 status = GdipGetClip(graphics, clip);
4096 if (status == Ok)
4097 status = GdipGetRegionBounds(clip, graphics, rect);
4098
4099 GdipDeleteRegion(clip);
4100 return status;
4101 }
4102
4103 /*****************************************************************************
4104 * GdipGetClipBoundsI [GDIPLUS.@]
4105 */
4106 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
4107 {
4108 TRACE("(%p, %p)\n", graphics, rect);
4109
4110 if(!graphics)
4111 return InvalidParameter;
4112
4113 if(graphics->busy)
4114 return ObjectBusy;
4115
4116 return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
4117 }
4118
4119 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
4120 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
4121 CompositingMode *mode)
4122 {
4123 TRACE("(%p, %p)\n", graphics, mode);
4124
4125 if(!graphics || !mode)
4126 return InvalidParameter;
4127
4128 if(graphics->busy)
4129 return ObjectBusy;
4130
4131 *mode = graphics->compmode;
4132
4133 return Ok;
4134 }
4135
4136 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
4137 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
4138 CompositingQuality *quality)
4139 {
4140 TRACE("(%p, %p)\n", graphics, quality);
4141
4142 if(!graphics || !quality)
4143 return InvalidParameter;
4144
4145 if(graphics->busy)
4146 return ObjectBusy;
4147
4148 *quality = graphics->compqual;
4149
4150 return Ok;
4151 }
4152
4153 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
4154 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
4155 InterpolationMode *mode)
4156 {
4157 TRACE("(%p, %p)\n", graphics, mode);
4158
4159 if(!graphics || !mode)
4160 return InvalidParameter;
4161
4162 if(graphics->busy)
4163 return ObjectBusy;
4164
4165 *mode = graphics->interpolation;
4166
4167 return Ok;
4168 }
4169
4170 /* FIXME: Need to handle color depths less than 24bpp */
4171 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
4172 {
4173 FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
4174
4175 if(!graphics || !argb)
4176 return InvalidParameter;
4177
4178 if(graphics->busy)
4179 return ObjectBusy;
4180
4181 return Ok;
4182 }
4183
4184 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
4185 {
4186 TRACE("(%p, %p)\n", graphics, scale);
4187
4188 if(!graphics || !scale)
4189 return InvalidParameter;
4190
4191 if(graphics->busy)
4192 return ObjectBusy;
4193
4194 *scale = graphics->scale;
4195
4196 return Ok;
4197 }
4198
4199 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
4200 {
4201 TRACE("(%p, %p)\n", graphics, unit);
4202
4203 if(!graphics || !unit)
4204 return InvalidParameter;
4205
4206 if(graphics->busy)
4207 return ObjectBusy;
4208
4209 *unit = graphics->unit;
4210
4211 return Ok;
4212 }
4213
4214 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
4215 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4216 *mode)
4217 {
4218 TRACE("(%p, %p)\n", graphics, mode);
4219
4220 if(!graphics || !mode)
4221 return InvalidParameter;
4222
4223 if(graphics->busy)
4224 return ObjectBusy;
4225
4226 *mode = graphics->pixeloffset;
4227
4228 return Ok;
4229 }
4230
4231 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
4232 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
4233 {
4234 TRACE("(%p, %p)\n", graphics, mode);
4235
4236 if(!graphics || !mode)
4237 return InvalidParameter;
4238
4239 if(graphics->busy)
4240 return ObjectBusy;
4241
4242 *mode = graphics->smoothing;
4243
4244 return Ok;
4245 }
4246
4247 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
4248 {
4249 TRACE("(%p, %p)\n", graphics, contrast);
4250
4251 if(!graphics || !contrast)
4252 return InvalidParameter;
4253
4254 *contrast = graphics->textcontrast;
4255
4256 return Ok;
4257 }
4258
4259 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
4260 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
4261 TextRenderingHint *hint)
4262 {
4263 TRACE("(%p, %p)\n", graphics, hint);
4264
4265 if(!graphics || !hint)
4266 return InvalidParameter;
4267
4268 if(graphics->busy)
4269 return ObjectBusy;
4270
4271 *hint = graphics->texthint;
4272
4273 return Ok;
4274 }
4275
4276 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
4277 {
4278 GpRegion *clip_rgn;
4279 GpStatus stat;
4280
4281 TRACE("(%p, %p)\n", graphics, rect);
4282
4283 if(!graphics || !rect)
4284 return InvalidParameter;
4285
4286 if(graphics->busy)
4287 return ObjectBusy;
4288
4289 /* intersect window and graphics clipping regions */
4290 if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
4291 return stat;
4292
4293 if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4294 goto cleanup;
4295
4296 /* get bounds of the region */
4297 stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4298
4299 cleanup:
4300 GdipDeleteRegion(clip_rgn);
4301
4302 return stat;
4303 }
4304
4305 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4306 {
4307 GpRectF rectf;
4308 GpStatus stat;
4309
4310 TRACE("(%p, %p)\n", graphics, rect);
4311
4312 if(!graphics || !rect)
4313 return InvalidParameter;
4314
4315 if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4316 {
4317 rect->X = gdip_round(rectf.X);
4318 rect->Y = gdip_round(rectf.Y);
4319 rect->Width = gdip_round(rectf.Width);
4320 rect->Height = gdip_round(rectf.Height);
4321 }
4322
4323 return stat;
4324 }
4325
4326 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4327 {
4328 TRACE("(%p, %p)\n", graphics, matrix);
4329
4330 if(!graphics || !matrix)
4331 return InvalidParameter;
4332
4333 if(graphics->busy)
4334 return ObjectBusy;
4335
4336 *matrix = graphics->worldtrans;
4337 return Ok;
4338 }
4339
4340 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4341 {
4342 GpSolidFill *brush;
4343 GpStatus stat;
4344 GpRectF wnd_rect;
4345
4346 TRACE("(%p, %x)\n", graphics, color);
4347
4348 if(!graphics)
4349 return InvalidParameter;
4350
4351 if(graphics->busy)
4352 return ObjectBusy;
4353
4354 if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4355 return stat;
4356
4357 if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4358 GdipDeleteBrush((GpBrush*)brush);
4359 return stat;
4360 }
4361
4362 GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4363 wnd_rect.Width, wnd_rect.Height);
4364
4365 GdipDeleteBrush((GpBrush*)brush);
4366
4367 return Ok;
4368 }
4369
4370 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4371 {
4372 TRACE("(%p, %p)\n", graphics, res);
4373
4374 if(!graphics || !res)
4375 return InvalidParameter;
4376
4377 return GdipIsEmptyRegion(graphics->clip, graphics, res);
4378 }
4379
4380 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4381 {
4382 GpStatus stat;
4383 GpRegion* rgn;
4384 GpPointF pt;
4385
4386 TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4387
4388 if(!graphics || !result)
4389 return InvalidParameter;
4390
4391 if(graphics->busy)
4392 return ObjectBusy;
4393
4394 pt.X = x;
4395 pt.Y = y;
4396 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4397 CoordinateSpaceWorld, &pt, 1)) != Ok)
4398 return stat;
4399
4400 if((stat = GdipCreateRegion(&rgn)) != Ok)
4401 return stat;
4402
4403 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4404 goto cleanup;
4405
4406 stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4407
4408 cleanup:
4409 GdipDeleteRegion(rgn);
4410 return stat;
4411 }
4412
4413 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4414 {
4415 return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4416 }
4417
4418 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4419 {
4420 GpStatus stat;
4421 GpRegion* rgn;
4422 GpPointF pts[2];
4423
4424 TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4425
4426 if(!graphics || !result)
4427 return InvalidParameter;
4428
4429 if(graphics->busy)
4430 return ObjectBusy;
4431
4432 pts[0].X = x;
4433 pts[0].Y = y;
4434 pts[1].X = x + width;
4435 pts[1].Y = y + height;
4436
4437 if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4438 CoordinateSpaceWorld, pts, 2)) != Ok)
4439 return stat;
4440
4441 pts[1].X -= pts[0].X;
4442 pts[1].Y -= pts[0].Y;
4443
4444 if((stat = GdipCreateRegion(&rgn)) != Ok)
4445 return stat;
4446
4447 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4448 goto cleanup;
4449
4450 stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4451
4452 cleanup:
4453 GdipDeleteRegion(rgn);
4454 return stat;
4455 }
4456
4457 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4458 {
4459 return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4460 }
4461
4462 GpStatus gdip_format_string(HDC hdc,
4463 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4464 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, int ignore_empty_clip,
4465 gdip_format_string_callback callback, void *user_data)
4466 {
4467 WCHAR* stringdup;
4468 int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4469 nheight, lineend, lineno = 0;
4470 RectF bounds;
4471 StringAlignment halign;
4472 GpStatus stat = Ok;
4473 SIZE size;
4474 HotkeyPrefix hkprefix;
4475 INT *hotkeyprefix_offsets=NULL;
4476 INT hotkeyprefix_count=0;
4477 INT hotkeyprefix_pos=0, hotkeyprefix_end_pos=0;
4478 BOOL seen_prefix = FALSE;
4479 GpStringFormat *dyn_format=NULL;
4480
4481 if(length == -1) length = lstrlenW(string);
4482
4483 stringdup = heap_alloc_zero((length + 1) * sizeof(WCHAR));
4484 if(!stringdup) return OutOfMemory;
4485
4486 if (!format)
4487 {
4488 stat = GdipStringFormatGetGenericDefault(&dyn_format);
4489 if (stat != Ok)
4490 {
4491 heap_free(stringdup);
4492 return stat;
4493 }
4494 format = dyn_format;
4495 }
4496
4497 nwidth = rect->Width;
4498 nheight = rect->Height;
4499 if (ignore_empty_clip)
4500 {
4501 if (!nwidth) nwidth = INT_MAX;
4502 if (!nheight) nheight = INT_MAX;
4503 }
4504
4505 hkprefix = format->hkprefix;
4506
4507 if (hkprefix == HotkeyPrefixShow)
4508 {
4509 for (i=0; i<length; i++)
4510 {
4511 if (string[i] == '&')
4512 hotkeyprefix_count++;
4513 }
4514 }
4515
4516 if (hotkeyprefix_count)
4517 hotkeyprefix_offsets = heap_alloc_zero(sizeof(INT) * hotkeyprefix_count);
4518
4519 hotkeyprefix_count = 0;
4520
4521 for(i = 0, j = 0; i < length; i++){
4522 /* FIXME: This makes the indexes passed to callback inaccurate. */
4523 if(!isprintW(string[i]) && (string[i] != '\n'))
4524 continue;
4525
4526 /* FIXME: tabs should be handled using tabstops from stringformat */
4527 if (string[i] == '\t')
4528 continue;
4529
4530 if (seen_prefix && hkprefix == HotkeyPrefixShow && string[i] != '&')
4531 hotkeyprefix_offsets[hotkeyprefix_count++] = j;
4532 else if (!seen_prefix && hkprefix != HotkeyPrefixNone && string[i] == '&')
4533 {
4534 seen_prefix = TRUE;
4535 continue;
4536 }
4537
4538 seen_prefix = FALSE;
4539
4540 stringdup[j] = string[i];
4541 j++;
4542 }
4543
4544 length = j;
4545
4546 halign = format->align;
4547
4548 while(sum < length){
4549 GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4550 nwidth, &fit, NULL, &size);
4551 fitcpy = fit;
4552
4553 if(fit == 0)
4554 break;
4555
4556 for(lret = 0; lret < fit; lret++)
4557 if(*(stringdup + sum + lret) == '\n')
4558 break;
4559
4560 /* Line break code (may look strange, but it imitates windows). */
4561 if(lret < fit)
4562 lineend = fit = lret; /* this is not an off-by-one error */
4563 else if(fit < (length - sum)){
4564 if(*(stringdup + sum + fit) == ' ')
4565 while(*(stringdup + sum + fit) == ' ')
4566 fit++;
4567 else
4568 while(*(stringdup + sum + fit - 1) != ' '){
4569 fit--;
4570
4571 if(*(stringdup + sum + fit) == '\t')
4572 break;
4573
4574 if(fit == 0){
4575 fit = fitcpy;
4576 break;
4577 }
4578 }
4579 lineend = fit;
4580 while(*(stringdup + sum + lineend - 1) == ' ' ||
4581 *(stringdup + sum + lineend - 1) == '\t')
4582 lineend--;
4583 }
4584 else
4585 lineend = fit;
4586
4587 GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4588 nwidth, &j, NULL, &size);
4589
4590 bounds.Width = size.cx;
4591
4592 if(height + size.cy > nheight)
4593 {
4594 if (format->attr & StringFormatFlagsLineLimit)
4595 break;
4596 bounds.Height = nheight - (height + size.cy);
4597 }
4598 else
4599 bounds.Height = size.cy;
4600
4601 bounds.Y = rect->Y + height;
4602
4603 switch (halign)
4604 {
4605 case StringAlignmentNear:
4606 default:
4607 bounds.X = rect->X;
4608 break;
4609 case StringAlignmentCenter:
4610 bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4611 break;
4612 case StringAlignmentFar:
4613 bounds.X = rect->X + rect->Width - bounds.Width;
4614 break;
4615 }
4616
4617 for (hotkeyprefix_end_pos=hotkeyprefix_pos; hotkeyprefix_end_pos<hotkeyprefix_count; hotkeyprefix_end_pos++)
4618 if (hotkeyprefix_offsets[hotkeyprefix_end_pos] >= sum + lineend)
4619 break;
4620
4621 stat = callback(hdc, stringdup, sum, lineend,
4622 font, rect, format, lineno, &bounds,
4623 &hotkeyprefix_offsets[hotkeyprefix_pos],
4624 hotkeyprefix_end_pos-hotkeyprefix_pos, user_data);
4625
4626 if (stat != Ok)
4627 break;
4628
4629 sum += fit + (lret < fitcpy ? 1 : 0);
4630 height += size.cy;
4631 lineno++;
4632
4633 hotkeyprefix_pos = hotkeyprefix_end_pos;
4634
4635 if(height > nheight)
4636 break;
4637
4638 /* Stop if this was a linewrap (but not if it was a linebreak). */
4639 if ((lret == fitcpy) && (format->attr & StringFormatFlagsNoWrap))
4640 break;
4641 }
4642
4643 heap_free(stringdup);
4644 heap_free(hotkeyprefix_offsets);
4645 GdipDeleteStringFormat(dyn_format);
4646
4647 return stat;
4648 }
4649
4650 struct measure_ranges_args {
4651 GpRegion **regions;
4652 REAL rel_width, rel_height;
4653 };
4654
4655 static GpStatus measure_ranges_callback(HDC hdc,
4656 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4657 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4658 INT lineno, const RectF *bounds, INT *underlined_indexes,
4659 INT underlined_index_count, void *user_data)
4660 {
4661 int i;
4662 GpStatus stat = Ok;
4663 struct measure_ranges_args *args = user_data;
4664
4665 for (i=0; i<format->range_count; i++)
4666 {
4667 INT range_start = max(index, format->character_ranges[i].First);
4668 INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4669 if (range_start < range_end)
4670 {
4671 GpRectF range_rect;
4672 SIZE range_size;
4673
4674 range_rect.Y = bounds->Y / args->rel_height;
4675 range_rect.Height = bounds->Height / args->rel_height;
4676
4677 GetTextExtentExPointW(hdc, string + index, range_start - index,
4678 INT_MAX, NULL, NULL, &range_size);
4679 range_rect.X = (bounds->X + range_size.cx) / args->rel_width;
4680
4681 GetTextExtentExPointW(hdc, string + index, range_end - index,
4682 INT_MAX, NULL, NULL, &range_size);
4683 range_rect.Width = (bounds->X + range_size.cx) / args->rel_width - range_rect.X;
4684
4685 stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4686 if (stat != Ok)
4687 break;
4688 }
4689 }
4690
4691 return stat;
4692 }
4693
4694 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4695 GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4696 GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4697 INT regionCount, GpRegion** regions)
4698 {
4699 GpStatus stat;
4700 int i;
4701 HFONT gdifont, oldfont;
4702 struct measure_ranges_args args;
4703 HDC hdc, temp_hdc=NULL;
4704 GpPointF pt[3];
4705 RectF scaled_rect;
4706 REAL margin_x;
4707
4708 TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4709 length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4710
4711 if (!(graphics && string && font && layoutRect && stringFormat && regions))
4712 return InvalidParameter;
4713
4714 if (regionCount < stringFormat->range_count)
4715 return InvalidParameter;
4716
4717 if(!graphics->hdc)
4718 {
4719 hdc = temp_hdc = CreateCompatibleDC(0);
4720 if (!temp_hdc) return OutOfMemory;
4721 }
4722 else
4723 hdc = graphics->hdc;
4724
4725 if (stringFormat->attr)
4726 TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4727
4728 pt[0].X = 0.0;
4729 pt[0].Y = 0.0;
4730 pt[1].X = 1.0;
4731 pt[1].Y = 0.0;
4732 pt[2].X = 0.0;
4733 pt[2].Y = 1.0;
4734 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4735 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4736 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4737 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4738 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4739
4740 margin_x = stringFormat->generic_typographic ? 0.0 : font->emSize / 6.0;
4741 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4742
4743 scaled_rect.X = (layoutRect->X + margin_x) * args.rel_width;
4744 scaled_rect.Y = layoutRect->Y * args.rel_height;
4745 scaled_rect.Width = layoutRect->Width * args.rel_width;
4746 scaled_rect.Height = layoutRect->Height * args.rel_height;
4747
4748 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4749 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4750
4751 get_font_hfont(graphics, font, stringFormat, &gdifont, NULL);
4752 oldfont = SelectObject(hdc, gdifont);
4753
4754 for (i=0; i<stringFormat->range_count; i++)
4755 {
4756 stat = GdipSetEmpty(regions[i]);
4757 if (stat != Ok)
4758 return stat;
4759 }
4760
4761 args.regions = regions;
4762
4763 stat = gdip_format_string(hdc, string, length, font, &scaled_rect, stringFormat,
4764 (stringFormat->attr & StringFormatFlagsNoClip) != 0, measure_ranges_callback, &args);
4765
4766 SelectObject(hdc, oldfont);
4767 DeleteObject(gdifont);
4768
4769 if (temp_hdc)
4770 DeleteDC(temp_hdc);
4771
4772 return stat;
4773 }
4774
4775 struct measure_string_args {
4776 RectF *bounds;
4777 INT *codepointsfitted;
4778 INT *linesfilled;
4779 REAL rel_width, rel_height;
4780 };
4781
4782 static GpStatus measure_string_callback(HDC hdc,
4783 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4784 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4785 INT lineno, const RectF *bounds, INT *underlined_indexes,
4786 INT underlined_index_count, void *user_data)
4787 {
4788 struct measure_string_args *args = user_data;
4789 REAL new_width, new_height;
4790
4791 new_width = bounds->Width / args->rel_width;
4792 new_height = (bounds->Height + bounds->Y) / args->rel_height - args->bounds->Y;
4793
4794 if (new_width > args->bounds->Width)
4795 args->bounds->Width = new_width;
4796
4797 if (new_height > args->bounds->Height)
4798 args->bounds->Height = new_height;
4799
4800 if (args->codepointsfitted)
4801 *args->codepointsfitted = index + length;
4802
4803 if (args->linesfilled)
4804 (*args->linesfilled)++;
4805
4806 return Ok;
4807 }
4808
4809 /* Find the smallest rectangle that bounds the text when it is printed in rect
4810 * according to the format options listed in format. If rect has 0 width and
4811 * height, then just find the smallest rectangle that bounds the text when it's
4812 * printed at location (rect->X, rect-Y). */
4813 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4814 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4815 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4816 INT *codepointsfitted, INT *linesfilled)
4817 {
4818 HFONT oldfont, gdifont;
4819 struct measure_string_args args;
4820 HDC temp_hdc=NULL, hdc;
4821 GpPointF pt[3];
4822 RectF scaled_rect;
4823 REAL margin_x;
4824 INT lines, glyphs;
4825
4826 TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4827 debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4828 bounds, codepointsfitted, linesfilled);
4829
4830 if(!graphics || !string || !font || !rect || !bounds)
4831 return InvalidParameter;
4832
4833 if(!graphics->hdc)
4834 {
4835 hdc = temp_hdc = CreateCompatibleDC(0);
4836 if (!temp_hdc) return OutOfMemory;
4837 }
4838 else
4839 hdc = graphics->hdc;
4840
4841 if(linesfilled) *linesfilled = 0;
4842 if(codepointsfitted) *codepointsfitted = 0;
4843
4844 if(format)
4845 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4846
4847 pt[0].X = 0.0;
4848 pt[0].Y = 0.0;
4849 pt[1].X = 1.0;
4850 pt[1].Y = 0.0;
4851 pt[2].X = 0.0;
4852 pt[2].Y = 1.0;
4853 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4854 args.rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4855 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4856 args.rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4857 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4858
4859 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
4860 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
4861
4862 scaled_rect.X = (rect->X + margin_x) * args.rel_width;
4863 scaled_rect.Y = rect->Y * args.rel_height;
4864 scaled_rect.Width = rect->Width * args.rel_width;
4865 scaled_rect.Height = rect->Height * args.rel_height;
4866 if (scaled_rect.Width >= 0.5)
4867 {
4868 scaled_rect.Width -= margin_x * 2.0 * args.rel_width;
4869 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
4870 }
4871
4872 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
4873 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
4874
4875 get_font_hfont(graphics, font, format, &gdifont, NULL);
4876 oldfont = SelectObject(hdc, gdifont);
4877
4878 bounds->X = rect->X;
4879 bounds->Y = rect->Y;
4880 bounds->Width = 0.0;
4881 bounds->Height = 0.0;
4882
4883 args.bounds = bounds;
4884 args.codepointsfitted = &glyphs;
4885 args.linesfilled = &lines;
4886 lines = glyphs = 0;
4887
4888 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
4889 measure_string_callback, &args);
4890
4891 if (linesfilled) *linesfilled = lines;
4892 if (codepointsfitted) *codepointsfitted = glyphs;
4893
4894 if (lines)
4895 bounds->Width += margin_x * 2.0;
4896
4897 SelectObject(hdc, oldfont);
4898 DeleteObject(gdifont);
4899
4900 if (temp_hdc)
4901 DeleteDC(temp_hdc);
4902
4903 return Ok;
4904 }
4905
4906 struct draw_string_args {
4907 GpGraphics *graphics;
4908 GDIPCONST GpBrush *brush;
4909 REAL x, y, rel_width, rel_height, ascent;
4910 };
4911
4912 static GpStatus draw_string_callback(HDC hdc,
4913 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4914 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4915 INT lineno, const RectF *bounds, INT *underlined_indexes,
4916 INT underlined_index_count, void *user_data)
4917 {
4918 struct draw_string_args *args = user_data;
4919 PointF position;
4920 GpStatus stat;
4921
4922 position.X = args->x + bounds->X / args->rel_width;
4923 position.Y = args->y + bounds->Y / args->rel_height + args->ascent;
4924
4925 stat = draw_driver_string(args->graphics, &string[index], length, font, format,
4926 args->brush, &position,
4927 DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance, NULL);
4928
4929 if (stat == Ok && underlined_index_count)
4930 {
4931 OUTLINETEXTMETRICW otm;
4932 REAL underline_y, underline_height;
4933 int i;
4934
4935 GetOutlineTextMetricsW(hdc, sizeof(otm), &otm);
4936
4937 underline_height = otm.otmsUnderscoreSize / args->rel_height;
4938 underline_y = position.Y - otm.otmsUnderscorePosition / args->rel_height - underline_height / 2;
4939
4940 for (i=0; i<underlined_index_count; i++)
4941 {
4942 REAL start_x, end_x;
4943 SIZE text_size;
4944 INT ofs = underlined_indexes[i] - index;
4945
4946 GetTextExtentExPointW(hdc, string + index, ofs, INT_MAX, NULL, NULL, &text_size);
4947 start_x = text_size.cx / args->rel_width;
4948
4949 GetTextExtentExPointW(hdc, string + index, ofs+1, INT_MAX, NULL, NULL, &text_size);
4950 end_x = text_size.cx / args->rel_width;
4951
4952 GdipFillRectangle(args->graphics, (GpBrush*)args->brush, position.X+start_x, underline_y, end_x-start_x, underline_height);
4953 }
4954 }
4955
4956 return stat;
4957 }
4958
4959 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4960 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4961 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4962 {
4963 HRGN rgn = NULL;
4964 HFONT gdifont;
4965 GpPointF pt[3], rectcpy[4];
4966 POINT corners[4];
4967 REAL rel_width, rel_height, margin_x;
4968 INT save_state, format_flags = 0;
4969 REAL offsety = 0.0;
4970 struct draw_string_args args;
4971 RectF scaled_rect;
4972 HDC hdc, temp_hdc=NULL;
4973 TEXTMETRICW textmetric;
4974
4975 TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4976 length, font, debugstr_rectf(rect), format, brush);
4977
4978 if(!graphics || !string || !font || !brush || !rect)
4979 return InvalidParameter;
4980
4981 if(graphics->hdc)
4982 {
4983 hdc = graphics->hdc;
4984 }
4985 else
4986 {
4987 hdc = temp_hdc = CreateCompatibleDC(0);
4988 }
4989
4990 if(format){
4991 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4992
4993 format_flags = format->attr;
4994
4995 /* Should be no need to explicitly test for StringAlignmentNear as
4996 * that is default behavior if no alignment is passed. */
4997 if(format->vertalign != StringAlignmentNear){
4998 RectF bounds, in_rect = *rect;
4999 in_rect.Height = 0.0; /* avoid height clipping */
5000 GdipMeasureString(graphics, string, length, font, &in_rect, format, &bounds, 0, 0);
5001
5002 TRACE("bounds %s\n", debugstr_rectf(&bounds));
5003
5004 if(format->vertalign == StringAlignmentCenter)
5005 offsety = (rect->Height - bounds.Height) / 2;
5006 else if(format->vertalign == StringAlignmentFar)
5007 offsety = (rect->Height - bounds.Height);
5008 }
5009 TRACE("vertical align %d, offsety %f\n", format->vertalign, offsety);
5010 }
5011
5012 save_state = SaveDC(hdc);
5013
5014 pt[0].X = 0.0;
5015 pt[0].Y = 0.0;
5016 pt[1].X = 1.0;
5017 pt[1].Y = 0.0;
5018 pt[2].X = 0.0;
5019 pt[2].Y = 1.0;
5020 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
5021 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
5022 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
5023 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
5024 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
5025
5026 rectcpy[3].X = rectcpy[0].X = rect->X;
5027 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
5028 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
5029 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
5030 transform_and_round_points(graphics, corners, rectcpy, 4);
5031
5032 margin_x = (format && format->generic_typographic) ? 0.0 : font->emSize / 6.0;
5033 margin_x *= units_scale(font->unit, graphics->unit, graphics->xres);
5034
5035 scaled_rect.X = margin_x * rel_width;
5036 scaled_rect.Y = 0.0;
5037 scaled_rect.Width = rel_width * rect->Width;
5038 scaled_rect.Height = rel_height * rect->Height;
5039 if (scaled_rect.Width >= 0.5)
5040 {
5041 scaled_rect.Width -= margin_x * 2.0 * rel_width;
5042 if (scaled_rect.Width < 0.5) return Ok; /* doesn't fit */
5043 }
5044
5045 if (scaled_rect.Width >= 1 << 23) scaled_rect.Width = 1 << 23;
5046 if (scaled_rect.Height >= 1 << 23) scaled_rect.Height = 1 << 23;
5047
5048 if (!(format_flags & StringFormatFlagsNoClip) &&
5049 scaled_rect.Width != 1 << 23 && scaled_rect.Height != 1 << 23 &&
5050 rect->Width > 0.0 && rect->Height > 0.0)
5051 {
5052 /* FIXME: If only the width or only the height is 0, we should probably still clip */
5053 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
5054 SelectClipRgn(hdc, rgn);
5055 }
5056
5057 get_font_hfont(graphics, font, format, &gdifont, NULL);
5058 SelectObject(hdc, gdifont);
5059
5060 args.graphics = graphics;
5061 args.brush = brush;
5062
5063 args.x = rect->X;
5064 args.y = rect->Y + offsety;
5065
5066 args.rel_width = rel_width;
5067 args.rel_height = rel_height;
5068
5069 GetTextMetricsW(hdc, &textmetric);
5070 args.ascent = textmetric.tmAscent / rel_height;
5071
5072 gdip_format_string(hdc, string, length, font, &scaled_rect, format, TRUE,
5073 draw_string_callback, &args);
5074
5075 DeleteObject(rgn);
5076 DeleteObject(gdifont);
5077
5078 RestoreDC(hdc, save_state);
5079
5080 DeleteDC(temp_hdc);
5081
5082 return Ok;
5083 }
5084
5085 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
5086 {
5087 TRACE("(%p)\n", graphics);
5088
5089 if(!graphics)
5090 return InvalidParameter;
5091
5092 if(graphics->busy)
5093 return ObjectBusy;
5094
5095 return GdipSetInfinite(graphics->clip);
5096 }
5097
5098 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
5099 {
5100 TRACE("(%p)\n", graphics);
5101
5102 if(!graphics)
5103 return InvalidParameter;
5104
5105 if(graphics->busy)
5106 return ObjectBusy;
5107
5108 return GdipSetMatrixElements(&graphics->worldtrans, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5109 }
5110
5111 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
5112 {
5113 return GdipEndContainer(graphics, state);
5114 }
5115
5116 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
5117 GpMatrixOrder order)
5118 {
5119 TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
5120
5121 if(!graphics)
5122 return InvalidParameter;
5123
5124 if(graphics->busy)
5125 return ObjectBusy;
5126
5127 return GdipRotateMatrix(&graphics->worldtrans, angle, order);
5128 }
5129
5130 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
5131 {
5132 return GdipBeginContainer2(graphics, state);
5133 }
5134
5135 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
5136 GraphicsContainer *state)
5137 {
5138 GraphicsContainerItem *container;
5139 GpStatus sts;
5140
5141 TRACE("(%p, %p)\n", graphics, state);
5142
5143 if(!graphics || !state)
5144 return InvalidParameter;
5145
5146 sts = init_container(&container, graphics);
5147 if(sts != Ok)
5148 return sts;
5149
5150 list_add_head(&graphics->containers, &container->entry);
5151 *state = graphics->contid = container->contid;
5152
5153 return Ok;
5154 }
5155
5156 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
5157 {
5158 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5159 return NotImplemented;
5160 }
5161
5162 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
5163 {
5164 FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
5165 return NotImplemented;
5166 }
5167
5168 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
5169 {
5170 FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
5171 return NotImplemented;
5172 }
5173
5174 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
5175 {
5176 GpStatus sts;
5177 GraphicsContainerItem *container, *container2;
5178
5179 TRACE("(%p, %x)\n", graphics, state);
5180
5181 if(!graphics)
5182 return InvalidParameter;
5183
5184 LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
5185 if(container->contid == state)
5186 break;
5187 }
5188
5189 /* did not find a matching container */
5190 if(&container->entry == &graphics->containers)
5191 return Ok;
5192
5193 sts = restore_container(graphics, container);
5194 if(sts != Ok)
5195 return sts;
5196
5197 /* remove all of the containers on top of the found container */
5198 LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
5199 if(container->contid == state)
5200 break;
5201 list_remove(&container->entry);
5202 delete_container(container);
5203 }
5204
5205 list_remove(&container->entry);
5206 delete_container(container);
5207
5208 return Ok;
5209 }
5210
5211 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
5212 REAL sy, GpMatrixOrder order)
5213 {
5214 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
5215
5216 if(!graphics)
5217 return InvalidParameter;
5218
5219 if(graphics->busy)
5220 return ObjectBusy;
5221
5222 return GdipScaleMatrix(&graphics->worldtrans, sx, sy, order);
5223 }
5224
5225 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
5226 CombineMode mode)
5227 {
5228 TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
5229
5230 if(!graphics || !srcgraphics)
5231 return InvalidParameter;
5232
5233 return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
5234 }
5235
5236 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
5237 CompositingMode mode)
5238 {
5239 TRACE("(%p, %d)\n", graphics, mode);
5240
5241 if(!graphics)
5242 return InvalidParameter;
5243
5244 if(graphics->busy)
5245 return ObjectBusy;
5246
5247 graphics->compmode = mode;
5248
5249 return Ok;
5250 }
5251
5252 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
5253 CompositingQuality quality)
5254 {
5255 TRACE("(%p, %d)\n", graphics, quality);
5256
5257 if(!graphics)
5258 return InvalidParameter;
5259
5260 if(graphics->busy)
5261 return ObjectBusy;
5262
5263 graphics->compqual = quality;
5264
5265 return Ok;
5266 }
5267
5268 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
5269 InterpolationMode mode)
5270 {
5271 TRACE("(%p, %d)\n", graphics, mode);
5272
5273 if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
5274 return InvalidParameter;
5275
5276 if(graphics->busy)
5277 return ObjectBusy;
5278
5279 if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
5280 mode = InterpolationModeBilinear;
5281
5282 if (mode == InterpolationModeHighQuality)
5283 mode = InterpolationModeHighQualityBicubic;
5284
5285 graphics->interpolation = mode;
5286
5287 return Ok;
5288 }
5289
5290 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
5291 {
5292 GpStatus stat;
5293
5294 TRACE("(%p, %.2f)\n", graphics, scale);
5295
5296 if(!graphics || (scale <= 0.0))
5297 return InvalidParameter;
5298
5299 if(graphics->busy)
5300 return ObjectBusy;
5301
5302 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5303 {
5304 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, graphics->unit, scale);
5305 if (stat != Ok)
5306 return stat;
5307 }
5308
5309 graphics->scale = scale;
5310
5311 return Ok;
5312 }
5313
5314 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
5315 {
5316 GpStatus stat;
5317
5318 TRACE("(%p, %d)\n", graphics, unit);
5319
5320 if(!graphics)
5321 return InvalidParameter;
5322
5323 if(graphics->busy)
5324 return ObjectBusy;
5325
5326 if(unit == UnitWorld)
5327 return InvalidParameter;
5328
5329 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5330 {
5331 stat = METAFILE_SetPageTransform((GpMetafile*)graphics->image, unit, graphics->scale);
5332 if (stat != Ok)
5333 return stat;
5334 }
5335
5336 graphics->unit = unit;
5337
5338 return Ok;
5339 }
5340
5341 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
5342 mode)
5343 {
5344 TRACE("(%p, %d)\n", graphics, mode);
5345
5346 if(!graphics)
5347 return InvalidParameter;
5348
5349 if(graphics->busy)
5350 return ObjectBusy;
5351
5352 graphics->pixeloffset = mode;
5353
5354 return Ok;
5355 }
5356
5357 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
5358 {
5359 static int calls;
5360
5361 TRACE("(%p,%i,%i)\n", graphics, x, y);
5362
5363 if (!(calls++))
5364 FIXME("value is unused in rendering\n");
5365
5366 if (!graphics)
5367 return InvalidParameter;
5368
5369 graphics->origin_x = x;
5370 graphics->origin_y = y;
5371
5372 return Ok;
5373 }
5374
5375 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
5376 {
5377 TRACE("(%p,%p,%p)\n", graphics, x, y);
5378
5379 if (!graphics || !x || !y)
5380 return InvalidParameter;
5381
5382 *x = graphics->origin_x;
5383 *y = graphics->origin_y;
5384
5385 return Ok;
5386 }
5387
5388 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
5389 {
5390 TRACE("(%p, %d)\n", graphics, mode);
5391
5392 if(!graphics)
5393 return InvalidParameter;
5394
5395 if(graphics->busy)
5396 return ObjectBusy;
5397
5398 graphics->smoothing = mode;
5399
5400 return Ok;
5401 }
5402
5403 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
5404 {
5405 TRACE("(%p, %d)\n", graphics, contrast);
5406
5407 if(!graphics)
5408 return InvalidParameter;
5409
5410 graphics->textcontrast = contrast;
5411
5412 return Ok;
5413 }
5414
5415 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
5416 TextRenderingHint hint)
5417 {
5418 TRACE("(%p, %d)\n", graphics, hint);
5419
5420 if(!graphics || hint > TextRenderingHintClearTypeGridFit)
5421 return InvalidParameter;
5422
5423 if(graphics->busy)
5424 return ObjectBusy;
5425
5426 graphics->texthint = hint;
5427
5428 return Ok;
5429 }
5430
5431 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
5432 {
5433 TRACE("(%p, %p)\n", graphics, matrix);
5434
5435 if(!graphics || !matrix)
5436 return InvalidParameter;
5437
5438 if(graphics->busy)
5439 return ObjectBusy;
5440
5441 TRACE("%f,%f,%f,%f,%f,%f\n",
5442 matrix->matrix[0], matrix->matrix[1], matrix->matrix[2],
5443 matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);
5444
5445 graphics->worldtrans = *matrix;
5446
5447 return Ok;
5448 }
5449
5450 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
5451 REAL dy, GpMatrixOrder order)
5452 {
5453 TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
5454
5455 if(!graphics)
5456 return InvalidParameter;
5457
5458 if(graphics->busy)
5459 return ObjectBusy;
5460
5461 return GdipTranslateMatrix(&graphics->worldtrans, dx, dy, order);
5462 }
5463
5464 /*****************************************************************************
5465 * GdipSetClipHrgn [GDIPLUS.@]
5466 */
5467 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
5468 {
5469 GpRegion *region;
5470 GpStatus status;
5471
5472 TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
5473
5474 if(!graphics)
5475 return InvalidParameter;
5476
5477 if(graphics->busy)
5478 return ObjectBusy;
5479
5480 /* hrgn is already in device units */
5481 status = GdipCreateRegionHrgn(hrgn, &region);
5482 if(status != Ok)
5483 return status;
5484
5485 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5486
5487 GdipDeleteRegion(region);
5488 return status;
5489 }
5490
5491 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5492 {
5493 GpStatus status;
5494 GpPath *clip_path;
5495
5496 TRACE("(%p, %p, %d)\n", graphics, path, mode);
5497
5498 if(!graphics)
5499 return InvalidParameter;
5500
5501 if(graphics->busy)
5502 return ObjectBusy;
5503
5504 status = GdipClonePath(path, &clip_path);
5505 if (status == Ok)
5506 {
5507 GpMatrix world_to_device;
5508
5509 get_graphics_transform(graphics, CoordinateSpaceDevice,
5510 CoordinateSpaceWorld, &world_to_device);
5511 status = GdipTransformPath(clip_path, &world_to_device);
5512 if (status == Ok)
5513 GdipCombineRegionPath(graphics->clip, clip_path, mode);
5514
5515 GdipDeletePath(clip_path);
5516 }
5517 return status;
5518 }
5519
5520 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5521 REAL width, REAL height,
5522 CombineMode mode)
5523 {
5524 GpStatus status;
5525 GpRectF rect;
5526 GpRegion *region;
5527
5528 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5529
5530 if(!graphics)
5531 return InvalidParameter;
5532
5533 if(graphics->busy)
5534 return ObjectBusy;
5535
5536 rect.X = x;
5537 rect.Y = y;
5538 rect.Width = width;
5539 rect.Height = height;
5540 status = GdipCreateRegionRect(&rect, &region);
5541 if (status == Ok)
5542 {
5543 GpMatrix world_to_device;
5544
5545 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5546 status = GdipTransformRegion(region, &world_to_device);
5547 if (status == Ok)
5548 status = GdipCombineRegionRegion(graphics->clip, region, mode);
5549
5550 GdipDeleteRegion(region);
5551 }
5552 return status;
5553 }
5554
5555 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5556 INT width, INT height,
5557 CombineMode mode)
5558 {
5559 TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5560
5561 if(!graphics)
5562 return InvalidParameter;
5563
5564 if(graphics->busy)
5565 return ObjectBusy;
5566
5567 return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5568 }
5569
5570 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5571 CombineMode mode)
5572 {
5573 GpStatus status;
5574 GpRegion *clip;
5575
5576 TRACE("(%p, %p, %d)\n", graphics, region, mode);
5577
5578 if(!graphics || !region)
5579 return InvalidParameter;
5580
5581 if(graphics->busy)
5582 return ObjectBusy;
5583
5584 status = GdipCloneRegion(region, &clip);
5585 if (status == Ok)
5586 {
5587 GpMatrix world_to_device;
5588
5589 get_graphics_transform(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &world_to_device);
5590 status = GdipTransformRegion(clip, &world_to_device);
5591 if (status == Ok)
5592 status = GdipCombineRegionRegion(graphics->clip, clip, mode);
5593
5594 GdipDeleteRegion(clip);
5595 }
5596 return status;
5597 }
5598
5599 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5600 INT count)
5601 {
5602 INT save_state;
5603 POINT *pti;
5604
5605 TRACE("(%p, %p, %d)\n", graphics, points, count);
5606
5607 if(!graphics || !pen || count<=0)
5608 return InvalidParameter;
5609
5610 if(graphics->busy)
5611 return ObjectBusy;
5612
5613 if (!graphics->hdc)
5614 {
5615 FIXME("graphics object has no HDC\n");
5616 return Ok;
5617 }
5618
5619 pti = heap_alloc_zero(sizeof(POINT) * count);
5620
5621 save_state = prepare_dc(graphics, pen);
5622 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5623
5624 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5625 Polygon(graphics->hdc, pti, count);
5626
5627 restore_dc(graphics, save_state);
5628 heap_free(pti);
5629
5630 return Ok;
5631 }
5632
5633 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5634 INT count)
5635 {
5636 GpStatus ret;
5637 GpPointF *ptf;
5638 INT i;
5639
5640 TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5641
5642 if(count<=0) return InvalidParameter;
5643 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
5644
5645 for(i = 0;i < count; i++){
5646 ptf[i].X = (REAL)points[i].X;
5647 ptf[i].Y = (REAL)points[i].Y;
5648 }
5649
5650 ret = GdipDrawPolygon(graphics,pen,ptf,count);
5651 heap_free(ptf);
5652
5653 return ret;
5654 }
5655
5656 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5657 {
5658 TRACE("(%p, %p)\n", graphics, dpi);
5659
5660 if(!graphics || !dpi)
5661 return InvalidParameter;
5662
5663 if(graphics->busy)
5664 return ObjectBusy;
5665
5666 *dpi = graphics->xres;
5667 return Ok;
5668 }
5669
5670 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5671 {
5672 TRACE("(%p, %p)\n", graphics, dpi);
5673
5674 if(!graphics || !dpi)
5675 return InvalidParameter;
5676
5677 if(graphics->busy)
5678 return ObjectBusy;
5679
5680 *dpi = graphics->yres;
5681 return Ok;
5682 }
5683
5684 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5685 GpMatrixOrder order)
5686 {
5687 GpMatrix m;
5688 GpStatus ret;
5689
5690 TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5691
5692 if(!graphics || !matrix)
5693 return InvalidParameter;
5694
5695 if(graphics->busy)
5696 return ObjectBusy;
5697
5698 m = graphics->worldtrans;
5699
5700 ret = GdipMultiplyMatrix(&m, matrix, order);
5701 if(ret == Ok)
5702 graphics->worldtrans = m;
5703
5704 return ret;
5705 }
5706
5707 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5708 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5709
5710 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5711 {
5712 GpStatus stat=Ok;
5713
5714 TRACE("(%p, %p)\n", graphics, hdc);
5715
5716 if(!graphics || !hdc)
5717 return InvalidParameter;
5718
5719 if(graphics->busy)
5720 return ObjectBusy;
5721
5722 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5723 {
5724 stat = METAFILE_GetDC((GpMetafile*)graphics->image, hdc);
5725 }
5726 else if (!graphics->hdc ||
5727 (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5728 {
5729 /* Create a fake HDC and fill it with a constant color. */
5730 HDC temp_hdc;
5731 HBITMAP hbitmap;
5732 GpRectF bounds;
5733 BITMAPINFOHEADER bmih;
5734 int i;
5735
5736 stat = get_graphics_bounds(graphics, &bounds);
5737 if (stat != Ok)
5738 return stat;
5739
5740 graphics->temp_hbitmap_width = bounds.Width;
5741 graphics->temp_hbitmap_height = bounds.Height;
5742
5743 bmih.biSize = sizeof(bmih);
5744 bmih.biWidth = graphics->temp_hbitmap_width;
5745 bmih.biHeight = -graphics->temp_hbitmap_height;
5746 bmih.biPlanes = 1;
5747 bmih.biBitCount = 32;
5748 bmih.biCompression = BI_RGB;
5749 bmih.biSizeImage = 0;
5750 bmih.biXPelsPerMeter = 0;
5751 bmih.biYPelsPerMeter = 0;
5752 bmih.biClrUsed = 0;
5753 bmih.biClrImportant = 0;
5754
5755 hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5756 (void**)&graphics->temp_bits, NULL, 0);
5757 if (!hbitmap)
5758 return GenericError;
5759
5760 temp_hdc = CreateCompatibleDC(0);
5761 if (!temp_hdc)
5762 {
5763 DeleteObject(hbitmap);
5764 return GenericError;
5765 }
5766
5767 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5768 ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5769
5770 SelectObject(temp_hdc, hbitmap);
5771
5772 graphics->temp_hbitmap = hbitmap;
5773 *hdc = graphics->temp_hdc = temp_hdc;
5774 }
5775 else
5776 {
5777 *hdc = graphics->hdc;
5778 }
5779
5780 if (stat == Ok)
5781 graphics->busy = TRUE;
5782
5783 return stat;
5784 }
5785
5786 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5787 {
5788 GpStatus stat=Ok;
5789
5790 TRACE("(%p, %p)\n", graphics, hdc);
5791
5792 if(!graphics || !hdc || !graphics->busy)
5793 return InvalidParameter;
5794
5795 if (graphics->image && graphics->image->type == ImageTypeMetafile)
5796 {
5797 stat = METAFILE_ReleaseDC((GpMetafile*)graphics->image, hdc);
5798 }
5799 else if (graphics->temp_hdc == hdc)
5800 {
5801 DWORD* pos;
5802 int i;
5803
5804 /* Find the pixels that have changed, and mark them as opaque. */
5805 pos = (DWORD*)graphics->temp_bits;
5806 for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5807 {
5808 if (*pos != DC_BACKGROUND_KEY)
5809 {
5810 *pos |= 0xff000000;
5811 }
5812 pos++;
5813 }
5814
5815 /* Write the changed pixels to the real target. */
5816 alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5817 graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5818 graphics->temp_hbitmap_width * 4, PixelFormat32bppARGB);
5819
5820 /* Clean up. */
5821 DeleteDC(graphics->temp_hdc);
5822 DeleteObject(graphics->temp_hbitmap);
5823 graphics->temp_hdc = NULL;
5824 graphics->temp_hbitmap = NULL;
5825 }
5826 else if (hdc != graphics->hdc)
5827 {
5828 stat = InvalidParameter;
5829 }
5830
5831 if (stat == Ok)
5832 graphics->busy = FALSE;
5833
5834 return stat;
5835 }
5836
5837 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5838 {
5839 GpRegion *clip;
5840 GpStatus status;
5841 GpMatrix device_to_world;
5842
5843 TRACE("(%p, %p)\n", graphics, region);
5844
5845 if(!graphics || !region)
5846 return InvalidParameter;
5847
5848 if(graphics->busy)
5849 return ObjectBusy;
5850
5851 if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5852 return status;
5853
5854 get_graphics_transform(graphics, CoordinateSpaceWorld, CoordinateSpaceDevice, &device_to_world);
5855 status = GdipTransformRegion(clip, &device_to_world);
5856 if (status != Ok)
5857 {
5858 GdipDeleteRegion(clip);
5859 return status;
5860 }
5861
5862 /* free everything except root node and header */
5863 delete_element(&region->node);
5864 memcpy(region, clip, sizeof(GpRegion));
5865 heap_free(clip);
5866
5867 return Ok;
5868 }
5869
5870 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5871 GpCoordinateSpace src_space, GpMatrix *matrix)
5872 {
5873 GpStatus stat = Ok;
5874 REAL scale_x, scale_y;
5875
5876 GdipSetMatrixElements(matrix, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
5877
5878 if (dst_space != src_space)
5879 {
5880 scale_x = units_to_pixels(1.0, graphics->unit, graphics->xres);
5881 scale_y = units_to_pixels(1.0, graphics->unit, graphics->yres);
5882
5883 if(graphics->unit != UnitDisplay)
5884 {
5885 scale_x *= graphics->scale;
5886 scale_y *= graphics->scale;
5887 }
5888
5889 /* transform from src_space to CoordinateSpacePage */
5890 switch (src_space)
5891 {
5892 case CoordinateSpaceWorld:
5893 GdipMultiplyMatrix(matrix, &graphics->worldtrans, MatrixOrderAppend);
5894 break;
5895 case CoordinateSpacePage:
5896 break;
5897 case CoordinateSpaceDevice:
5898 GdipScaleMatrix(matrix, 1.0/scale_x, 1.0/scale_y, MatrixOrderAppend);
5899 break;
5900 }
5901
5902 /* transform from CoordinateSpacePage to dst_space */
5903 switch (dst_space)
5904 {
5905 case CoordinateSpaceWorld:
5906 {
5907 GpMatrix inverted_transform = graphics->worldtrans;
5908 stat = GdipInvertMatrix(&inverted_transform);
5909 if (stat == Ok)
5910 GdipMultiplyMatrix(matrix, &inverted_transform, MatrixOrderAppend);
5911 break;
5912 }
5913 case CoordinateSpacePage:
5914 break;
5915 case CoordinateSpaceDevice:
5916 GdipScaleMatrix(matrix, scale_x, scale_y, MatrixOrderAppend);
5917 break;
5918 }
5919 }
5920 return stat;
5921 }
5922
5923 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5924 GpCoordinateSpace src_space, GpPointF *points, INT count)
5925 {
5926 GpMatrix matrix;
5927 GpStatus stat;
5928
5929 if(!graphics || !points || count <= 0)
5930 return InvalidParameter;
5931
5932 if(graphics->busy)
5933 return ObjectBusy;
5934
5935 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5936
5937 if (src_space == dst_space) return Ok;
5938
5939 stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5940 if (stat != Ok) return stat;
5941
5942 return GdipTransformMatrixPoints(&matrix, points, count);
5943 }
5944
5945 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5946 GpCoordinateSpace src_space, GpPoint *points, INT count)
5947 {
5948 GpPointF *pointsF;
5949 GpStatus ret;
5950 INT i;
5951
5952 TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5953
5954 if(count <= 0)
5955 return InvalidParameter;
5956
5957 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
5958 if(!pointsF)
5959 return OutOfMemory;
5960
5961 for(i = 0; i < count; i++){
5962 pointsF[i].X = (REAL)points[i].X;
5963 pointsF[i].Y = (REAL)points[i].Y;
5964 }
5965
5966 ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5967
5968 if(ret == Ok)
5969 for(i = 0; i < count; i++){
5970 points[i].X = gdip_round(pointsF[i].X);
5971 points[i].Y = gdip_round(pointsF[i].Y);
5972 }
5973 heap_free(pointsF);
5974
5975 return ret;
5976 }
5977
5978 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5979 {
5980 static int calls;
5981
5982 TRACE("\n");
5983
5984 if (!calls++)
5985 FIXME("stub\n");
5986
5987 return NULL;
5988 }
5989
5990 /*****************************************************************************
5991 * GdipTranslateClip [GDIPLUS.@]
5992 */
5993 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5994 {
5995 TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5996
5997 if(!graphics)
5998 return InvalidParameter;
5999
6000 if(graphics->busy)
6001 return ObjectBusy;
6002
6003 return GdipTranslateRegion(graphics->clip, dx, dy);
6004 }
6005
6006 /*****************************************************************************
6007 * GdipTranslateClipI [GDIPLUS.@]
6008 */
6009 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
6010 {
6011 TRACE("(%p, %d, %d)\n", graphics, dx, dy);
6012
6013 if(!graphics)
6014 return InvalidParameter;
6015
6016 if(graphics->busy)
6017 return ObjectBusy;
6018
6019 return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
6020 }
6021
6022
6023 /*****************************************************************************
6024 * GdipMeasureDriverString [GDIPLUS.@]
6025 */
6026 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6027 GDIPCONST GpFont *font, GDIPCONST PointF *positions,
6028 INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
6029 {
6030 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6031 HFONT hfont;
6032 HDC hdc;
6033 REAL min_x, min_y, max_x, max_y, x, y;
6034 int i;
6035 TEXTMETRICW textmetric;
6036 const WORD *glyph_indices;
6037 WORD *dynamic_glyph_indices=NULL;
6038 REAL rel_width, rel_height, ascent, descent;
6039 GpPointF pt[3];
6040
6041 TRACE("(%p %p %d %p %p %d %p %p)\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
6042
6043 if (!graphics || !text || !font || !positions || !boundingBox)
6044 return InvalidParameter;
6045
6046 if (length == -1)
6047 length = strlenW(text);
6048
6049 if (length == 0)
6050 {
6051 boundingBox->X = 0.0;
6052 boundingBox->Y = 0.0;
6053 boundingBox->Width = 0.0;
6054 boundingBox->Height = 0.0;
6055 }
6056
6057 if (flags & unsupported_flags)
6058 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6059
6060 get_font_hfont(graphics, font, NULL, &hfont, matrix);
6061
6062 hdc = CreateCompatibleDC(0);
6063 SelectObject(hdc, hfont);
6064
6065 GetTextMetricsW(hdc, &textmetric);
6066
6067 pt[0].X = 0.0;
6068 pt[0].Y = 0.0;
6069 pt[1].X = 1.0;
6070 pt[1].Y = 0.0;
6071 pt[2].X = 0.0;
6072 pt[2].Y = 1.0;
6073 if (matrix)
6074 {
6075 GpMatrix xform = *matrix;
6076 GdipTransformMatrixPoints(&xform, pt, 3);
6077 }
6078 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
6079 rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
6080 (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
6081 rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
6082 (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
6083
6084 if (flags & DriverStringOptionsCmapLookup)
6085 {
6086 glyph_indices = dynamic_glyph_indices = heap_alloc_zero(sizeof(WORD) * length);
6087 if (!glyph_indices)
6088 {
6089 DeleteDC(hdc);
6090 DeleteObject(hfont);
6091 return OutOfMemory;
6092 }
6093
6094 GetGlyphIndicesW(hdc, text, length, dynamic_glyph_indices, 0);
6095 }
6096 else
6097 glyph_indices = text;
6098
6099 min_x = max_x = x = positions[0].X;
6100 min_y = max_y = y = positions[0].Y;
6101
6102 ascent = textmetric.tmAscent / rel_height;
6103 descent = textmetric.tmDescent / rel_height;
6104
6105 for (i=0; i<length; i++)
6106 {
6107 int char_width;
6108 ABC abc;
6109
6110 if (!(flags & DriverStringOptionsRealizedAdvance))
6111 {
6112 x = positions[i].X;
6113 y = positions[i].Y;
6114 }
6115
6116 GetCharABCWidthsW(hdc, glyph_indices[i], glyph_indices[i], &abc);
6117 char_width = abc.abcA + abc.abcB + abc.abcC;
6118
6119 if (min_y > y - ascent) min_y = y - ascent;
6120 if (max_y < y + descent) max_y = y + descent;
6121 if (min_x > x) min_x = x;
6122
6123 x += char_width / rel_width;
6124
6125 if (max_x < x) max_x = x;
6126 }
6127
6128 heap_free(dynamic_glyph_indices);
6129 DeleteDC(hdc);
6130 DeleteObject(hfont);
6131
6132 boundingBox->X = min_x;
6133 boundingBox->Y = min_y;
6134 boundingBox->Width = max_x - min_x;
6135 boundingBox->Height = max_y - min_y;
6136
6137 return Ok;
6138 }
6139
6140 static GpStatus GDI32_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6141 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6142 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6143 INT flags, GDIPCONST GpMatrix *matrix)
6144 {
6145 static const INT unsupported_flags = ~(DriverStringOptionsRealizedAdvance|DriverStringOptionsCmapLookup);
6146 INT save_state;
6147 GpPointF pt;
6148 HFONT hfont;
6149 UINT eto_flags=0;
6150
6151 if (flags & unsupported_flags)
6152 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6153
6154 if (!(flags & DriverStringOptionsCmapLookup))
6155 eto_flags |= ETO_GLYPH_INDEX;
6156
6157 save_state = SaveDC(graphics->hdc);
6158 SetBkMode(graphics->hdc, TRANSPARENT);
6159 SetTextColor(graphics->hdc, get_gdi_brush_color(brush));
6160
6161 pt = positions[0];
6162 GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, &pt, 1);
6163
6164 get_font_hfont(graphics, font, format, &hfont, matrix);
6165 SelectObject(graphics->hdc, hfont);
6166
6167 SetTextAlign(graphics->hdc, TA_BASELINE|TA_LEFT);
6168
6169 ExtTextOutW(graphics->hdc, gdip_round(pt.X), gdip_round(pt.Y), eto_flags, NULL, text, length, NULL);
6170
6171 RestoreDC(graphics->hdc, save_state);
6172
6173 DeleteObject(hfont);
6174
6175 return Ok;
6176 }
6177
6178 static GpStatus SOFTWARE_GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6179 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6180 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6181 INT flags, GDIPCONST GpMatrix *matrix)
6182 {
6183 static const INT unsupported_flags = ~(DriverStringOptionsCmapLookup|DriverStringOptionsRealizedAdvance);
6184 GpStatus stat;
6185 PointF *real_positions, real_position;
6186 POINT *pti;
6187 HFONT hfont;
6188 HDC hdc;
6189 int min_x=INT_MAX, min_y=INT_MAX, max_x=INT_MIN, max_y=INT_MIN, i, x, y;
6190 DWORD max_glyphsize=0;
6191 GLYPHMETRICS glyphmetrics;
6192 static const MAT2 identity = {{0,1}, {0,0}, {0,0}, {0,1}};
6193 BYTE *glyph_mask;
6194 BYTE *text_mask;
6195 int text_mask_stride;
6196 BYTE *pixel_data;
6197 int pixel_data_stride;
6198 GpRect pixel_area;
6199 UINT ggo_flags = GGO_GRAY8_BITMAP;
6200
6201 if (length <= 0)
6202 return Ok;
6203
6204 if (!(flags & DriverStringOptionsCmapLookup))
6205 ggo_flags |= GGO_GLYPH_INDEX;
6206
6207 if (flags & unsupported_flags)
6208 FIXME("Ignoring flags %x\n", flags & unsupported_flags);
6209
6210 pti = heap_alloc_zero(sizeof(POINT) * length);
6211 if (!pti)
6212 return OutOfMemory;
6213
6214 if (flags & DriverStringOptionsRealizedAdvance)
6215 {
6216 real_position = positions[0];
6217
6218 transform_and_round_points(graphics, pti, &real_position, 1);
6219 }
6220 else
6221 {
6222 real_positions = heap_alloc_zero(sizeof(PointF) * length);
6223 if (!real_positions)
6224 {
6225 heap_free(pti);
6226 return OutOfMemory;
6227 }
6228
6229 memcpy(real_positions, positions, sizeof(PointF) * length);
6230
6231 transform_and_round_points(graphics, pti, real_positions, length);
6232
6233 heap_free(real_positions);
6234 }
6235
6236 get_font_hfont(graphics, font, format, &hfont, matrix);
6237
6238 hdc = CreateCompatibleDC(0);
6239 SelectObject(hdc, hfont);
6240
6241 /* Get the boundaries of the text to be drawn */
6242 for (i=0; i<length; i++)
6243 {
6244 DWORD glyphsize;
6245 int left, top, right, bottom;
6246
6247 glyphsize = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6248 &glyphmetrics, 0, NULL, &identity);
6249
6250 if (glyphsize == GDI_ERROR)
6251 {
6252 ERR("GetGlyphOutlineW failed\n");
6253 heap_free(pti);
6254 DeleteDC(hdc);
6255 DeleteObject(hfont);
6256 return GenericError;
6257 }
6258
6259 if (glyphsize > max_glyphsize)
6260 max_glyphsize = glyphsize;
6261
6262 if (glyphsize != 0)
6263 {
6264 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6265 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6266 right = pti[i].x + glyphmetrics.gmptGlyphOrigin.x + glyphmetrics.gmBlackBoxX;
6267 bottom = pti[i].y - glyphmetrics.gmptGlyphOrigin.y + glyphmetrics.gmBlackBoxY;
6268
6269 if (left < min_x) min_x = left;
6270 if (top < min_y) min_y = top;
6271 if (right > max_x) max_x = right;
6272 if (bottom > max_y) max_y = bottom;
6273 }
6274
6275 if (i+1 < length && (flags & DriverStringOptionsRealizedAdvance) == DriverStringOptionsRealizedAdvance)
6276 {
6277 pti[i+1].x = pti[i].x + glyphmetrics.gmCellIncX;
6278 pti[i+1].y = pti[i].y + glyphmetrics.gmCellIncY;
6279 }
6280 }
6281
6282 if (max_glyphsize == 0)
6283 /* Nothing to draw. */
6284 return Ok;
6285
6286 glyph_mask = heap_alloc_zero(max_glyphsize);
6287 text_mask = heap_alloc_zero((max_x - min_x) * (max_y - min_y));
6288 text_mask_stride = max_x - min_x;
6289
6290 if (!(glyph_mask && text_mask))
6291 {
6292 heap_free(glyph_mask);
6293 heap_free(text_mask);
6294 heap_free(pti);
6295 DeleteDC(hdc);
6296 DeleteObject(hfont);
6297 return OutOfMemory;
6298 }
6299
6300 /* Generate a mask for the text */
6301 for (i=0; i<length; i++)
6302 {
6303 DWORD ret;
6304 int left, top, stride;
6305
6306 ret = GetGlyphOutlineW(hdc, text[i], ggo_flags,
6307 &glyphmetrics, max_glyphsize, glyph_mask, &identity);
6308
6309 if (ret == GDI_ERROR || ret == 0)
6310 continue; /* empty glyph */
6311
6312 left = pti[i].x + glyphmetrics.gmptGlyphOrigin.x;
6313 top = pti[i].y - glyphmetrics.gmptGlyphOrigin.y;
6314 stride = (glyphmetrics.gmBlackBoxX + 3) & (~3);
6315
6316 for (y=0; y<glyphmetrics.gmBlackBoxY; y++)
6317 {
6318 BYTE *glyph_val = glyph_mask + y * stride;
6319 BYTE *text_val = text_mask + (left - min_x) + (top - min_y + y) * text_mask_stride;
6320 for (x=0; x<glyphmetrics.gmBlackBoxX; x++)
6321 {
6322 *text_val = min(64, *text_val + *glyph_val);
6323 glyph_val++;
6324 text_val++;
6325 }
6326 }
6327 }
6328
6329 heap_free(pti);
6330 DeleteDC(hdc);
6331 DeleteObject(hfont);
6332 heap_free(glyph_mask);
6333
6334 /* get the brush data */
6335 pixel_data = heap_alloc_zero(4 * (max_x - min_x) * (max_y - min_y));
6336 if (!pixel_data)
6337 {
6338 heap_free(text_mask);
6339 return OutOfMemory;
6340 }
6341
6342 pixel_area.X = min_x;
6343 pixel_area.Y = min_y;
6344 pixel_area.Width = max_x - min_x;
6345 pixel_area.Height = max_y - min_y;
6346 pixel_data_stride = pixel_area.Width * 4;
6347
6348 stat = brush_fill_pixels(graphics, (GpBrush*)brush, (DWORD*)pixel_data, &pixel_area, pixel_area.Width);
6349 if (stat != Ok)
6350 {
6351 heap_free(text_mask);
6352 heap_free(pixel_data);
6353 return stat;
6354 }
6355
6356 /* multiply the brush data by the mask */
6357 for (y=0; y<pixel_area.Height; y++)
6358 {
6359 BYTE *text_val = text_mask + text_mask_stride * y;
6360 BYTE *pixel_val = pixel_data + pixel_data_stride * y + 3;
6361 for (x=0; x<pixel_area.Width; x++)
6362 {
6363 *pixel_val = (*pixel_val) * (*text_val) / 64;
6364 text_val++;
6365 pixel_val+=4;
6366 }
6367 }
6368
6369 heap_free(text_mask);
6370
6371 /* draw the result */
6372 stat = alpha_blend_pixels(graphics, min_x, min_y, pixel_data, pixel_area.Width,
6373 pixel_area.Height, pixel_data_stride, PixelFormat32bppARGB);
6374
6375 heap_free(pixel_data);
6376
6377 return stat;
6378 }
6379
6380 static GpStatus draw_driver_string(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6381 GDIPCONST GpFont *font, GDIPCONST GpStringFormat *format,
6382 GDIPCONST GpBrush *brush, GDIPCONST PointF *positions,
6383 INT flags, GDIPCONST GpMatrix *matrix)
6384 {
6385 GpStatus stat = NotImplemented;
6386
6387 if (length == -1)
6388 length = strlenW(text);
6389
6390 if (graphics->hdc && !graphics->alpha_hdc &&
6391 ((flags & DriverStringOptionsRealizedAdvance) || length <= 1) &&
6392 brush->bt == BrushTypeSolidColor &&
6393 (((GpSolidFill*)brush)->color & 0xff000000) == 0xff000000)
6394 stat = GDI32_GdipDrawDriverString(graphics, text, length, font, format,
6395 brush, positions, flags, matrix);
6396 if (stat == NotImplemented)
6397 stat = SOFTWARE_GdipDrawDriverString(graphics, text, length, font, format,
6398 brush, positions, flags, matrix);
6399 return stat;
6400 }
6401
6402 /*****************************************************************************
6403 * GdipDrawDriverString [GDIPLUS.@]
6404 */
6405 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
6406 GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
6407 GDIPCONST PointF *positions, INT flags,
6408 GDIPCONST GpMatrix *matrix )
6409 {
6410 TRACE("(%p %s %p %p %p %d %p)\n", graphics, debugstr_wn(text, length), font, brush, positions, flags, matrix);
6411
6412 if (!graphics || !text || !font || !brush || !positions)
6413 return InvalidParameter;
6414
6415 return draw_driver_string(graphics, text, length, font, NULL,
6416 brush, positions, flags, matrix);
6417 }
6418
6419 /*****************************************************************************
6420 * GdipIsVisibleClipEmpty [GDIPLUS.@]
6421 */
6422 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
6423 {
6424 GpStatus stat;
6425 GpRegion* rgn;
6426
6427 TRACE("(%p, %p)\n", graphics, res);
6428
6429 if((stat = GdipCreateRegion(&rgn)) != Ok)
6430 return stat;
6431
6432 if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
6433 goto cleanup;
6434
6435 stat = GdipIsEmptyRegion(rgn, graphics, res);
6436
6437 cleanup:
6438 GdipDeleteRegion(rgn);
6439 return stat;
6440 }
6441
6442 GpStatus WINGDIPAPI GdipResetPageTransform(GpGraphics *graphics)
6443 {
6444 static int calls;
6445
6446 TRACE("(%p) stub\n", graphics);
6447
6448 if(!(calls++))
6449 FIXME("not implemented\n");
6450
6451 return NotImplemented;
6452 }