Sync with trunk r58033.
[reactos.git] / dll / win32 / gdiplus / region.c
1 /*
2 * Copyright (C) 2008 Google (Lei Zhang)
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 <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24
25 #include "objbase.h"
26
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32
33 /**********************************************************
34 *
35 * Data returned by GdipGetRegionData looks something like this:
36 *
37 * struct region_data_header
38 * {
39 * DWORD size; size in bytes of the data - 8.
40 * DWORD magic1; probably a checksum.
41 * DWORD magic2; always seems to be 0xdbc01001 - version?
42 * DWORD num_ops; number of combining ops * 2
43 * };
44 *
45 * Then follows a sequence of combining ops and region elements.
46 *
47 * A region element is either a RECTF or some path data.
48 *
49 * Combining ops are just stored as their CombineMode value.
50 *
51 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
52 * stored as 0x10000002 (with no following RECTF) and an infinite rect
53 * is stored as 0x10000003 (again with no following RECTF).
54 *
55 * Path data is preceded by the DWORD 0x10000001. Then follows a
56 * DWORD size and then size bytes of data.
57 *
58 * The combining ops are stored in the reverse order to the region
59 * elements and in the reverse order to which the region was
60 * constructed.
61 *
62 * When two or more complex regions (ie those with more than one
63 * element) are combined, the combining op for the two regions comes
64 * first, then the combining ops for the region elements in region 1,
65 * followed by the region elements for region 1, then follows the
66 * combining ops for region 2 and finally region 2's region elements.
67 * Presumably you're supposed to use the 0x1000000x header to find the
68 * end of the op list (the count of the elements in each region is not
69 * stored).
70 *
71 * When a simple region (1 element) is combined, it's treated as if a
72 * single rect/path is being combined.
73 *
74 */
75
76 #define FLAGS_NOFLAGS 0x0
77 #define FLAGS_INTPATH 0x4000
78
79 /* Header size as far as header->size is concerned. This doesn't include
80 * header->size or header->checksum
81 */
82 static const INT sizeheader_size = sizeof(DWORD) * 2;
83
84 typedef struct packed_point
85 {
86 short X;
87 short Y;
88 } packed_point;
89
90 /* Everything is measured in DWORDS; round up if there's a remainder */
91 static inline INT get_pathtypes_size(const GpPath* path)
92 {
93 INT needed = path->pathdata.Count / sizeof(DWORD);
94
95 if (path->pathdata.Count % sizeof(DWORD) > 0)
96 needed++;
97
98 return needed * sizeof(DWORD);
99 }
100
101 static inline INT get_element_size(const region_element* element)
102 {
103 INT needed = sizeof(DWORD); /* DWORD for the type */
104 switch(element->type)
105 {
106 case RegionDataRect:
107 return needed + sizeof(GpRect);
108 case RegionDataPath:
109 needed += element->elementdata.pathdata.pathheader.size;
110 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
111 return needed;
112 case RegionDataEmptyRect:
113 case RegionDataInfiniteRect:
114 return needed;
115 default:
116 needed += get_element_size(element->elementdata.combine.left);
117 needed += get_element_size(element->elementdata.combine.right);
118 return needed;
119 }
120
121 return 0;
122 }
123
124 /* Does not check parameters, caller must do that */
125 static inline GpStatus init_region(GpRegion* region, const RegionType type)
126 {
127 region->node.type = type;
128 region->header.checksum = 0xdeadbeef;
129 region->header.magic = VERSION_MAGIC;
130 region->header.num_children = 0;
131 region->header.size = sizeheader_size + get_element_size(&region->node);
132
133 return Ok;
134 }
135
136 static inline GpStatus clone_element(const region_element* element,
137 region_element** element2)
138 {
139 GpStatus stat;
140
141 /* root node is allocated with GpRegion */
142 if(!*element2){
143 *element2 = GdipAlloc(sizeof(region_element));
144 if (!*element2)
145 return OutOfMemory;
146 }
147
148 (*element2)->type = element->type;
149
150 switch (element->type)
151 {
152 case RegionDataRect:
153 (*element2)->elementdata.rect = element->elementdata.rect;
154 return Ok;
155 case RegionDataEmptyRect:
156 case RegionDataInfiniteRect:
157 return Ok;
158 case RegionDataPath:
159 (*element2)->elementdata.pathdata.pathheader = element->elementdata.pathdata.pathheader;
160 stat = GdipClonePath(element->elementdata.pathdata.path,
161 &(*element2)->elementdata.pathdata.path);
162 if (stat == Ok) return Ok;
163 break;
164 default:
165 (*element2)->elementdata.combine.left = NULL;
166 (*element2)->elementdata.combine.right = NULL;
167
168 stat = clone_element(element->elementdata.combine.left,
169 &(*element2)->elementdata.combine.left);
170 if (stat == Ok)
171 {
172 stat = clone_element(element->elementdata.combine.right,
173 &(*element2)->elementdata.combine.right);
174 if (stat == Ok) return Ok;
175 }
176 break;
177 }
178
179 delete_element(*element2);
180 *element2 = NULL;
181 return stat;
182 }
183
184 /* Common code for CombineRegion*
185 * All the caller has to do is get its format into an element
186 */
187 static inline void fuse_region(GpRegion* region, region_element* left,
188 region_element* right, const CombineMode mode)
189 {
190 region->node.type = mode;
191 region->node.elementdata.combine.left = left;
192 region->node.elementdata.combine.right = right;
193
194 region->header.size = sizeheader_size + get_element_size(&region->node);
195 region->header.num_children += 2;
196 }
197
198 /*****************************************************************************
199 * GdipCloneRegion [GDIPLUS.@]
200 *
201 * Creates a deep copy of the region
202 *
203 * PARAMS
204 * region [I] source region
205 * clone [O] resulting clone
206 *
207 * RETURNS
208 * SUCCESS: Ok
209 * FAILURE: InvalidParameter or OutOfMemory
210 */
211 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
212 {
213 region_element *element;
214
215 TRACE("%p %p\n", region, clone);
216
217 if (!(region && clone))
218 return InvalidParameter;
219
220 *clone = GdipAlloc(sizeof(GpRegion));
221 if (!*clone)
222 return OutOfMemory;
223 element = &(*clone)->node;
224
225 (*clone)->header = region->header;
226 return clone_element(&region->node, &element);
227 }
228
229 /*****************************************************************************
230 * GdipCombineRegionPath [GDIPLUS.@]
231 */
232 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
233 {
234 GpRegion *path_region;
235 region_element *left, *right = NULL;
236 GpStatus stat;
237
238 TRACE("%p %p %d\n", region, path, mode);
239
240 if (!(region && path))
241 return InvalidParameter;
242
243 stat = GdipCreateRegionPath(path, &path_region);
244 if (stat != Ok)
245 return stat;
246
247 /* simply replace region data */
248 if(mode == CombineModeReplace){
249 delete_element(&region->node);
250 memcpy(region, path_region, sizeof(GpRegion));
251 GdipFree(path_region);
252 return Ok;
253 }
254
255 left = GdipAlloc(sizeof(region_element));
256 if (left)
257 {
258 *left = region->node;
259 stat = clone_element(&path_region->node, &right);
260 if (stat == Ok)
261 {
262 fuse_region(region, left, right, mode);
263 GdipDeleteRegion(path_region);
264 return Ok;
265 }
266 }
267 else
268 stat = OutOfMemory;
269
270 GdipFree(left);
271 GdipDeleteRegion(path_region);
272 return stat;
273 }
274
275 /*****************************************************************************
276 * GdipCombineRegionRect [GDIPLUS.@]
277 */
278 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
279 GDIPCONST GpRectF *rect, CombineMode mode)
280 {
281 GpRegion *rect_region;
282 region_element *left, *right = NULL;
283 GpStatus stat;
284
285 TRACE("%p %p %d\n", region, rect, mode);
286
287 if (!(region && rect))
288 return InvalidParameter;
289
290 stat = GdipCreateRegionRect(rect, &rect_region);
291 if (stat != Ok)
292 return stat;
293
294 /* simply replace region data */
295 if(mode == CombineModeReplace){
296 delete_element(&region->node);
297 memcpy(region, rect_region, sizeof(GpRegion));
298 GdipFree(rect_region);
299 return Ok;
300 }
301
302 left = GdipAlloc(sizeof(region_element));
303 if (left)
304 {
305 memcpy(left, &region->node, sizeof(region_element));
306 stat = clone_element(&rect_region->node, &right);
307 if (stat == Ok)
308 {
309 fuse_region(region, left, right, mode);
310 GdipDeleteRegion(rect_region);
311 return Ok;
312 }
313 }
314 else
315 stat = OutOfMemory;
316
317 GdipFree(left);
318 GdipDeleteRegion(rect_region);
319 return stat;
320 }
321
322 /*****************************************************************************
323 * GdipCombineRegionRectI [GDIPLUS.@]
324 */
325 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
326 GDIPCONST GpRect *rect, CombineMode mode)
327 {
328 GpRectF rectf;
329
330 TRACE("%p %p %d\n", region, rect, mode);
331
332 if (!rect)
333 return InvalidParameter;
334
335 rectf.X = (REAL)rect->X;
336 rectf.Y = (REAL)rect->Y;
337 rectf.Height = (REAL)rect->Height;
338 rectf.Width = (REAL)rect->Width;
339
340 return GdipCombineRegionRect(region, &rectf, mode);
341 }
342
343 /*****************************************************************************
344 * GdipCombineRegionRegion [GDIPLUS.@]
345 */
346 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
347 GpRegion *region2, CombineMode mode)
348 {
349 region_element *left, *right = NULL;
350 GpStatus stat;
351 GpRegion *reg2copy;
352
353 TRACE("%p %p %d\n", region1, region2, mode);
354
355 if(!(region1 && region2))
356 return InvalidParameter;
357
358 /* simply replace region data */
359 if(mode == CombineModeReplace){
360 stat = GdipCloneRegion(region2, &reg2copy);
361 if(stat != Ok) return stat;
362
363 delete_element(&region1->node);
364 memcpy(region1, reg2copy, sizeof(GpRegion));
365 GdipFree(reg2copy);
366 return Ok;
367 }
368
369 left = GdipAlloc(sizeof(region_element));
370 if (!left)
371 return OutOfMemory;
372
373 *left = region1->node;
374 stat = clone_element(&region2->node, &right);
375 if (stat != Ok)
376 {
377 GdipFree(left);
378 return OutOfMemory;
379 }
380
381 fuse_region(region1, left, right, mode);
382 region1->header.num_children += region2->header.num_children;
383
384 return Ok;
385 }
386
387 /*****************************************************************************
388 * GdipCreateRegion [GDIPLUS.@]
389 */
390 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
391 {
392 TRACE("%p\n", region);
393
394 if(!region)
395 return InvalidParameter;
396
397 *region = GdipAlloc(sizeof(GpRegion));
398 if(!*region)
399 return OutOfMemory;
400
401 TRACE("=> %p\n", *region);
402
403 return init_region(*region, RegionDataInfiniteRect);
404 }
405
406 /*****************************************************************************
407 * GdipCreateRegionPath [GDIPLUS.@]
408 *
409 * Creates a GpRegion from a GpPath
410 *
411 * PARAMS
412 * path [I] path to base the region on
413 * region [O] pointer to the newly allocated region
414 *
415 * RETURNS
416 * SUCCESS: Ok
417 * FAILURE: InvalidParameter
418 *
419 * NOTES
420 * If a path has no floating point points, its points will be stored as shorts
421 * (INTPATH)
422 *
423 * If a path is empty, it is considered to be an INTPATH
424 */
425 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
426 {
427 region_element* element;
428 GpPoint *pointsi;
429 GpPointF *pointsf;
430
431 GpStatus stat;
432 DWORD flags = FLAGS_INTPATH;
433 INT count, i;
434
435 TRACE("%p, %p\n", path, region);
436
437 if (!(path && region))
438 return InvalidParameter;
439
440 *region = GdipAlloc(sizeof(GpRegion));
441 if(!*region)
442 return OutOfMemory;
443 stat = init_region(*region, RegionDataPath);
444 if (stat != Ok)
445 {
446 GdipDeleteRegion(*region);
447 return stat;
448 }
449 element = &(*region)->node;
450 count = path->pathdata.Count;
451
452 /* Test to see if the path is an Integer path */
453 if (count)
454 {
455 pointsi = GdipAlloc(sizeof(GpPoint) * count);
456 pointsf = GdipAlloc(sizeof(GpPointF) * count);
457 if (!(pointsi && pointsf))
458 {
459 GdipFree(pointsi);
460 GdipFree(pointsf);
461 GdipDeleteRegion(*region);
462 return OutOfMemory;
463 }
464
465 stat = GdipGetPathPointsI(path, pointsi, count);
466 if (stat != Ok)
467 {
468 GdipDeleteRegion(*region);
469 return stat;
470 }
471 stat = GdipGetPathPoints(path, pointsf, count);
472 if (stat != Ok)
473 {
474 GdipDeleteRegion(*region);
475 return stat;
476 }
477
478 for (i = 0; i < count; i++)
479 {
480 if (!(pointsi[i].X == pointsf[i].X &&
481 pointsi[i].Y == pointsf[i].Y ))
482 {
483 flags = FLAGS_NOFLAGS;
484 break;
485 }
486 }
487 GdipFree(pointsi);
488 GdipFree(pointsf);
489 }
490
491 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
492 if (stat != Ok)
493 {
494 GdipDeleteRegion(*region);
495 return stat;
496 }
497
498 /* 3 for headers, once again size doesn't count itself */
499 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
500 switch(flags)
501 {
502 /* Floats, sent out as floats */
503 case FLAGS_NOFLAGS:
504 element->elementdata.pathdata.pathheader.size +=
505 (sizeof(DWORD) * count * 2);
506 break;
507 /* INTs, sent out as packed shorts */
508 case FLAGS_INTPATH:
509 element->elementdata.pathdata.pathheader.size +=
510 (sizeof(DWORD) * count);
511 break;
512 default:
513 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
514 }
515 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
516 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
517 element->elementdata.pathdata.pathheader.count = count;
518 element->elementdata.pathdata.pathheader.flags = flags;
519 (*region)->header.size = sizeheader_size + get_element_size(element);
520
521 return Ok;
522 }
523
524 /*****************************************************************************
525 * GdipCreateRegionRect [GDIPLUS.@]
526 */
527 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
528 GpRegion **region)
529 {
530 GpStatus stat;
531
532 TRACE("%p, %p\n", rect, region);
533
534 if (!(rect && region))
535 return InvalidParameter;
536
537 *region = GdipAlloc(sizeof(GpRegion));
538 stat = init_region(*region, RegionDataRect);
539 if(stat != Ok)
540 {
541 GdipDeleteRegion(*region);
542 return stat;
543 }
544
545 (*region)->node.elementdata.rect.X = rect->X;
546 (*region)->node.elementdata.rect.Y = rect->Y;
547 (*region)->node.elementdata.rect.Width = rect->Width;
548 (*region)->node.elementdata.rect.Height = rect->Height;
549
550 return Ok;
551 }
552
553 /*****************************************************************************
554 * GdipCreateRegionRectI [GDIPLUS.@]
555 */
556 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
557 GpRegion **region)
558 {
559 GpRectF rectf;
560
561 TRACE("%p, %p\n", rect, region);
562
563 rectf.X = (REAL)rect->X;
564 rectf.Y = (REAL)rect->Y;
565 rectf.Width = (REAL)rect->Width;
566 rectf.Height = (REAL)rect->Height;
567
568 return GdipCreateRegionRect(&rectf, region);
569 }
570
571 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
572 {
573 FIXME("(%p, %d, %p): stub\n", data, size, region);
574
575 *region = NULL;
576 return NotImplemented;
577 }
578
579
580 /******************************************************************************
581 * GdipCreateRegionHrgn [GDIPLUS.@]
582 */
583 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
584 {
585 DWORD size;
586 LPRGNDATA buf;
587 LPRECT rect;
588 GpStatus stat;
589 GpPath* path;
590 GpRegion* local;
591 int i;
592
593 TRACE("(%p, %p)\n", hrgn, region);
594
595 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
596 return InvalidParameter;
597
598 buf = GdipAlloc(size);
599 if(!buf)
600 return OutOfMemory;
601
602 if(!GetRegionData(hrgn, size, buf)){
603 GdipFree(buf);
604 return GenericError;
605 }
606
607 if(buf->rdh.nCount == 0){
608 if((stat = GdipCreateRegion(&local)) != Ok){
609 GdipFree(buf);
610 return stat;
611 }
612 if((stat = GdipSetEmpty(local)) != Ok){
613 GdipFree(buf);
614 GdipDeleteRegion(local);
615 return stat;
616 }
617 *region = local;
618 GdipFree(buf);
619 return Ok;
620 }
621
622 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
623 GdipFree(buf);
624 return stat;
625 }
626
627 rect = (LPRECT)buf->Buffer;
628 for(i = 0; i < buf->rdh.nCount; i++){
629 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
630 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
631 GdipFree(buf);
632 GdipDeletePath(path);
633 return stat;
634 }
635 rect++;
636 }
637
638 stat = GdipCreateRegionPath(path, region);
639
640 GdipFree(buf);
641 GdipDeletePath(path);
642 return stat;
643 }
644
645 /*****************************************************************************
646 * GdipDeleteRegion [GDIPLUS.@]
647 */
648 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
649 {
650 TRACE("%p\n", region);
651
652 if (!region)
653 return InvalidParameter;
654
655 delete_element(&region->node);
656 GdipFree(region);
657
658 return Ok;
659 }
660
661 /*****************************************************************************
662 * GdipGetRegionBounds [GDIPLUS.@]
663 */
664 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
665 {
666 HRGN hrgn;
667 RECT r;
668 GpStatus status;
669
670 TRACE("(%p, %p, %p)\n", region, graphics, rect);
671
672 if(!region || !graphics || !rect)
673 return InvalidParameter;
674
675 /* Contrary to MSDN, native ignores the graphics transform. */
676 status = GdipGetRegionHRgn(region, NULL, &hrgn);
677 if(status != Ok)
678 return status;
679
680 /* infinite */
681 if(!hrgn){
682 rect->X = rect->Y = -(REAL)(1 << 22);
683 rect->Width = rect->Height = (REAL)(1 << 23);
684 TRACE("%p => infinite\n", region);
685 return Ok;
686 }
687
688 if(GetRgnBox(hrgn, &r)){
689 rect->X = r.left;
690 rect->Y = r.top;
691 rect->Width = r.right - r.left;
692 rect->Height = r.bottom - r.top;
693 TRACE("%p => %s\n", region, debugstr_rectf(rect));
694 }
695 else
696 status = GenericError;
697
698 DeleteObject(hrgn);
699
700 return status;
701 }
702
703 /*****************************************************************************
704 * GdipGetRegionBoundsI [GDIPLUS.@]
705 */
706 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
707 {
708 GpRectF rectf;
709 GpStatus status;
710
711 TRACE("(%p, %p, %p)\n", region, graphics, rect);
712
713 if(!rect)
714 return InvalidParameter;
715
716 status = GdipGetRegionBounds(region, graphics, &rectf);
717 if(status == Ok){
718 rect->X = gdip_round(rectf.X);
719 rect->Y = gdip_round(rectf.X);
720 rect->Width = gdip_round(rectf.Width);
721 rect->Height = gdip_round(rectf.Height);
722 }
723
724 return status;
725 }
726
727 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
728 {
729 location[*offset] = write;
730 (*offset)++;
731 }
732
733 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
734 {
735 ((FLOAT*)location)[*offset] = write;
736 (*offset)++;
737 }
738
739 static inline void write_packed_point(DWORD* location, INT* offset,
740 const GpPointF* write)
741 {
742 packed_point point;
743
744 point.X = write->X;
745 point.Y = write->Y;
746 memcpy(location + *offset, &point, sizeof(packed_point));
747 (*offset)++;
748 }
749
750 static inline void write_path_types(DWORD* location, INT* offset,
751 const GpPath* path)
752 {
753 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
754
755 /* The unwritten parts of the DWORD (if any) must be cleared */
756 if (path->pathdata.Count % sizeof(DWORD))
757 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
758 path->pathdata.Count,
759 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
760 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
761 }
762
763 static void write_element(const region_element* element, DWORD *buffer,
764 INT* filled)
765 {
766 write_dword(buffer, filled, element->type);
767 switch (element->type)
768 {
769 case CombineModeReplace:
770 case CombineModeIntersect:
771 case CombineModeUnion:
772 case CombineModeXor:
773 case CombineModeExclude:
774 case CombineModeComplement:
775 write_element(element->elementdata.combine.left, buffer, filled);
776 write_element(element->elementdata.combine.right, buffer, filled);
777 break;
778 case RegionDataRect:
779 write_float(buffer, filled, element->elementdata.rect.X);
780 write_float(buffer, filled, element->elementdata.rect.Y);
781 write_float(buffer, filled, element->elementdata.rect.Width);
782 write_float(buffer, filled, element->elementdata.rect.Height);
783 break;
784 case RegionDataPath:
785 {
786 INT i;
787 const GpPath* path = element->elementdata.pathdata.path;
788
789 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
790 sizeof(element->elementdata.pathdata.pathheader));
791 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
792 switch (element->elementdata.pathdata.pathheader.flags)
793 {
794 case FLAGS_NOFLAGS:
795 for (i = 0; i < path->pathdata.Count; i++)
796 {
797 write_float(buffer, filled, path->pathdata.Points[i].X);
798 write_float(buffer, filled, path->pathdata.Points[i].Y);
799 }
800 break;
801 case FLAGS_INTPATH:
802 for (i = 0; i < path->pathdata.Count; i++)
803 {
804 write_packed_point(buffer, filled,
805 &path->pathdata.Points[i]);
806 }
807 }
808 write_path_types(buffer, filled, path);
809 break;
810 }
811 case RegionDataEmptyRect:
812 case RegionDataInfiniteRect:
813 break;
814 }
815 }
816
817 /*****************************************************************************
818 * GdipGetRegionData [GDIPLUS.@]
819 *
820 * Returns the header, followed by combining ops and region elements.
821 *
822 * PARAMS
823 * region [I] region to retrieve from
824 * buffer [O] buffer to hold the resulting data
825 * size [I] size of the buffer
826 * needed [O] (optional) how much data was written
827 *
828 * RETURNS
829 * SUCCESS: Ok
830 * FAILURE: InvalidParameter
831 *
832 * NOTES
833 * The header contains the size, a checksum, a version string, and the number
834 * of children. The size does not count itself or the checksum.
835 * Version is always something like 0xdbc01001 or 0xdbc01002
836 *
837 * An element is a RECT, or PATH; Combining ops are stored as their
838 * CombineMode value. Special regions (infinite, empty) emit just their
839 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
840 * their code followed by a second header for the path followed by the actual
841 * path data. Followed by the flags for each point. The pathheader contains
842 * the size of the data to follow, a version number again, followed by a count
843 * of how many points, and any special flags which may apply. 0x4000 means its
844 * a path of shorts instead of FLOAT.
845 *
846 * Combining Ops are stored in reverse order from when they were constructed;
847 * the output is a tree where the left side combining area is always taken
848 * first.
849 */
850 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
851 UINT *needed)
852 {
853 INT filled = 0;
854
855 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
856
857 if (!(region && buffer && size))
858 return InvalidParameter;
859
860 memcpy(buffer, &region->header, sizeof(region->header));
861 filled += sizeof(region->header) / sizeof(DWORD);
862 /* With few exceptions, everything written is DWORD aligned,
863 * so use that as our base */
864 write_element(&region->node, (DWORD*)buffer, &filled);
865
866 if (needed)
867 *needed = filled * sizeof(DWORD);
868
869 return Ok;
870 }
871
872 /*****************************************************************************
873 * GdipGetRegionDataSize [GDIPLUS.@]
874 */
875 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
876 {
877 TRACE("%p, %p\n", region, needed);
878
879 if (!(region && needed))
880 return InvalidParameter;
881
882 /* header.size doesn't count header.size and header.checksum */
883 *needed = region->header.size + sizeof(DWORD) * 2;
884
885 return Ok;
886 }
887
888 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
889 {
890 HDC new_hdc=NULL;
891 GpGraphics *new_graphics=NULL;
892 GpStatus stat;
893 INT save_state;
894
895 if (!graphics)
896 {
897 new_hdc = GetDC(0);
898 if (!new_hdc)
899 return OutOfMemory;
900
901 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
902 graphics = new_graphics;
903 if (stat != Ok)
904 {
905 ReleaseDC(0, new_hdc);
906 return stat;
907 }
908 }
909 else if (!graphics->hdc)
910 {
911 graphics->hdc = new_hdc = GetDC(0);
912 if (!new_hdc)
913 return OutOfMemory;
914 }
915
916 save_state = SaveDC(graphics->hdc);
917 EndPath(graphics->hdc);
918
919 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
920 : WINDING));
921
922 stat = trace_path(graphics, path);
923 if (stat == Ok)
924 {
925 *hrgn = PathToRegion(graphics->hdc);
926 stat = *hrgn ? Ok : OutOfMemory;
927 }
928
929 RestoreDC(graphics->hdc, save_state);
930 if (new_hdc)
931 {
932 ReleaseDC(0, new_hdc);
933 if (new_graphics)
934 GdipDeleteGraphics(new_graphics);
935 else
936 graphics->hdc = NULL;
937 }
938
939 return stat;
940 }
941
942 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
943 {
944 switch (element->type)
945 {
946 case RegionDataInfiniteRect:
947 *hrgn = NULL;
948 return Ok;
949 case RegionDataEmptyRect:
950 *hrgn = CreateRectRgn(0, 0, 0, 0);
951 return *hrgn ? Ok : OutOfMemory;
952 case RegionDataPath:
953 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
954 case RegionDataRect:
955 {
956 GpPath* path;
957 GpStatus stat;
958 GpRectF* rc = &element->elementdata.rect;
959
960 stat = GdipCreatePath(FillModeAlternate, &path);
961 if (stat != Ok)
962 return stat;
963 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
964
965 if (stat == Ok)
966 stat = get_path_hrgn(path, graphics, hrgn);
967
968 GdipDeletePath(path);
969
970 return stat;
971 }
972 case CombineModeIntersect:
973 case CombineModeUnion:
974 case CombineModeXor:
975 case CombineModeExclude:
976 case CombineModeComplement:
977 {
978 HRGN left, right;
979 GpStatus stat;
980 int ret;
981
982 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
983 if (stat != Ok)
984 {
985 *hrgn = NULL;
986 return stat;
987 }
988
989 if (left == NULL)
990 {
991 /* existing region is infinite */
992 switch (element->type)
993 {
994 case CombineModeIntersect:
995 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
996 case CombineModeXor: case CombineModeExclude:
997 FIXME("cannot exclude from an infinite region\n");
998 /* fall-through */
999 case CombineModeUnion: case CombineModeComplement:
1000 *hrgn = NULL;
1001 return Ok;
1002 }
1003 }
1004
1005 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1006 if (stat != Ok)
1007 {
1008 DeleteObject(left);
1009 *hrgn = NULL;
1010 return stat;
1011 }
1012
1013 if (right == NULL)
1014 {
1015 /* new region is infinite */
1016 switch (element->type)
1017 {
1018 case CombineModeIntersect:
1019 *hrgn = left;
1020 return Ok;
1021 case CombineModeXor: case CombineModeComplement:
1022 FIXME("cannot exclude from an infinite region\n");
1023 /* fall-through */
1024 case CombineModeUnion: case CombineModeExclude:
1025 DeleteObject(left);
1026 *hrgn = NULL;
1027 return Ok;
1028 }
1029 }
1030
1031 switch (element->type)
1032 {
1033 case CombineModeIntersect:
1034 ret = CombineRgn(left, left, right, RGN_AND);
1035 break;
1036 case CombineModeUnion:
1037 ret = CombineRgn(left, left, right, RGN_OR);
1038 break;
1039 case CombineModeXor:
1040 ret = CombineRgn(left, left, right, RGN_XOR);
1041 break;
1042 case CombineModeExclude:
1043 ret = CombineRgn(left, left, right, RGN_DIFF);
1044 break;
1045 case CombineModeComplement:
1046 ret = CombineRgn(left, right, left, RGN_DIFF);
1047 break;
1048 default:
1049 ret = ERROR;
1050 }
1051
1052 DeleteObject(right);
1053
1054 if (ret == ERROR)
1055 {
1056 DeleteObject(left);
1057 *hrgn = NULL;
1058 return GenericError;
1059 }
1060
1061 *hrgn = left;
1062 return Ok;
1063 }
1064 default:
1065 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1066 *hrgn = NULL;
1067 return NotImplemented;
1068 }
1069 }
1070
1071 /*****************************************************************************
1072 * GdipGetRegionHRgn [GDIPLUS.@]
1073 */
1074 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1075 {
1076 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1077
1078 if (!region || !hrgn)
1079 return InvalidParameter;
1080
1081 return get_region_hrgn(&region->node, graphics, hrgn);
1082 }
1083
1084 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1085 {
1086 GpStatus status;
1087 GpRectF rect;
1088
1089 TRACE("(%p, %p, %p)\n", region, graphics, res);
1090
1091 if(!region || !graphics || !res)
1092 return InvalidParameter;
1093
1094 status = GdipGetRegionBounds(region, graphics, &rect);
1095 if (status != Ok) return status;
1096
1097 *res = rect.Width == 0.0 && rect.Height == 0.0;
1098 TRACE("=> %d\n", *res);
1099
1100 return Ok;
1101 }
1102
1103 /*****************************************************************************
1104 * GdipIsEqualRegion [GDIPLUS.@]
1105 */
1106 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1107 BOOL *res)
1108 {
1109 HRGN hrgn1, hrgn2;
1110 GpStatus stat;
1111
1112 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1113
1114 if(!region || !region2 || !graphics || !res)
1115 return InvalidParameter;
1116
1117 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1118 if(stat != Ok)
1119 return stat;
1120 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1121 if(stat != Ok){
1122 DeleteObject(hrgn1);
1123 return stat;
1124 }
1125
1126 *res = EqualRgn(hrgn1, hrgn2);
1127
1128 /* one of GpRegions is infinite */
1129 if(*res == ERROR)
1130 *res = (!hrgn1 && !hrgn2);
1131
1132 DeleteObject(hrgn1);
1133 DeleteObject(hrgn2);
1134
1135 return Ok;
1136 }
1137
1138 /*****************************************************************************
1139 * GdipIsInfiniteRegion [GDIPLUS.@]
1140 */
1141 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1142 {
1143 /* I think graphics is ignored here */
1144 TRACE("(%p, %p, %p)\n", region, graphics, res);
1145
1146 if(!region || !graphics || !res)
1147 return InvalidParameter;
1148
1149 *res = (region->node.type == RegionDataInfiniteRect);
1150
1151 return Ok;
1152 }
1153
1154 /*****************************************************************************
1155 * GdipIsVisibleRegionRect [GDIPLUS.@]
1156 */
1157 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1158 {
1159 HRGN hrgn;
1160 GpStatus stat;
1161 RECT rect;
1162
1163 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1164
1165 if(!region || !res)
1166 return InvalidParameter;
1167
1168 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1169 return stat;
1170
1171 /* infinite */
1172 if(!hrgn){
1173 *res = TRUE;
1174 return Ok;
1175 }
1176
1177 rect.left = ceilr(x);
1178 rect.top = ceilr(y);
1179 rect.right = ceilr(x + w);
1180 rect.bottom = ceilr(y + h);
1181
1182 *res = RectInRegion(hrgn, &rect);
1183
1184 DeleteObject(hrgn);
1185
1186 return Ok;
1187 }
1188
1189 /*****************************************************************************
1190 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1191 */
1192 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1193 {
1194 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1195 if(!region || !res)
1196 return InvalidParameter;
1197
1198 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1199 }
1200
1201 /*****************************************************************************
1202 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1203 */
1204 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1205 {
1206 HRGN hrgn;
1207 GpStatus stat;
1208
1209 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1210
1211 if(!region || !res)
1212 return InvalidParameter;
1213
1214 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1215 return stat;
1216
1217 /* infinite */
1218 if(!hrgn){
1219 *res = TRUE;
1220 return Ok;
1221 }
1222
1223 *res = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1224
1225 DeleteObject(hrgn);
1226
1227 return Ok;
1228 }
1229
1230 /*****************************************************************************
1231 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1232 */
1233 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1234 {
1235 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1236
1237 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1238 }
1239
1240 /*****************************************************************************
1241 * GdipSetEmpty [GDIPLUS.@]
1242 */
1243 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1244 {
1245 GpStatus stat;
1246
1247 TRACE("%p\n", region);
1248
1249 if (!region)
1250 return InvalidParameter;
1251
1252 delete_element(&region->node);
1253 stat = init_region(region, RegionDataEmptyRect);
1254
1255 return stat;
1256 }
1257
1258 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1259 {
1260 GpStatus stat;
1261
1262 TRACE("%p\n", region);
1263
1264 if (!region)
1265 return InvalidParameter;
1266
1267 delete_element(&region->node);
1268 stat = init_region(region, RegionDataInfiniteRect);
1269
1270 return stat;
1271 }
1272
1273 /* Transforms GpRegion elements with given matrix */
1274 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1275 {
1276 GpStatus stat;
1277
1278 switch(element->type)
1279 {
1280 case RegionDataEmptyRect:
1281 case RegionDataInfiniteRect:
1282 return Ok;
1283 case RegionDataRect:
1284 {
1285 /* We can't transform a rectangle, so convert it to a path. */
1286 GpRegion *new_region;
1287 GpPath *path;
1288
1289 stat = GdipCreatePath(FillModeAlternate, &path);
1290 if (stat == Ok)
1291 {
1292 stat = GdipAddPathRectangle(path,
1293 element->elementdata.rect.X, element->elementdata.rect.Y,
1294 element->elementdata.rect.Width, element->elementdata.rect.Height);
1295
1296 if (stat == Ok)
1297 stat = GdipCreateRegionPath(path, &new_region);
1298
1299 GdipDeletePath(path);
1300 }
1301
1302 if (stat == Ok)
1303 {
1304 /* Steal the element from the created region. */
1305 memcpy(element, &new_region->node, sizeof(region_element));
1306 HeapFree(GetProcessHeap(), 0, new_region);
1307 }
1308 else
1309 return stat;
1310 }
1311 /* Fall-through to do the actual conversion. */
1312 case RegionDataPath:
1313 stat = GdipTransformMatrixPoints(matrix,
1314 element->elementdata.pathdata.path->pathdata.Points,
1315 element->elementdata.pathdata.path->pathdata.Count);
1316 return stat;
1317 default:
1318 stat = transform_region_element(element->elementdata.combine.left, matrix);
1319 if (stat == Ok)
1320 stat = transform_region_element(element->elementdata.combine.right, matrix);
1321 return stat;
1322 }
1323 }
1324
1325 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1326 {
1327 TRACE("(%p, %p)\n", region, matrix);
1328
1329 if (!region || !matrix)
1330 return InvalidParameter;
1331
1332 return transform_region_element(&region->node, matrix);
1333 }
1334
1335 /* Translates GpRegion elements with specified offsets */
1336 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1337 {
1338 INT i;
1339
1340 switch(element->type)
1341 {
1342 case RegionDataEmptyRect:
1343 case RegionDataInfiniteRect:
1344 return;
1345 case RegionDataRect:
1346 element->elementdata.rect.X += dx;
1347 element->elementdata.rect.Y += dy;
1348 return;
1349 case RegionDataPath:
1350 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1351 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1352 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1353 }
1354 return;
1355 default:
1356 translate_region_element(element->elementdata.combine.left, dx, dy);
1357 translate_region_element(element->elementdata.combine.right, dx, dy);
1358 return;
1359 }
1360 }
1361
1362 /*****************************************************************************
1363 * GdipTranslateRegion [GDIPLUS.@]
1364 */
1365 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1366 {
1367 TRACE("(%p, %f, %f)\n", region, dx, dy);
1368
1369 if(!region)
1370 return InvalidParameter;
1371
1372 translate_region_element(&region->node, dx, dy);
1373
1374 return Ok;
1375 }
1376
1377 /*****************************************************************************
1378 * GdipTranslateRegionI [GDIPLUS.@]
1379 */
1380 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1381 {
1382 TRACE("(%p, %d, %d)\n", region, dx, dy);
1383
1384 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1385 }
1386
1387 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1388 {
1389 GpRegion *region_copy;
1390 GpStatus stat;
1391 HRGN hrgn;
1392 DWORD data_size;
1393
1394 stat = GdipCloneRegion(region, &region_copy);
1395
1396 if (stat == Ok)
1397 {
1398 stat = GdipTransformRegion(region_copy, matrix);
1399
1400 if (stat == Ok)
1401 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1402
1403 if (stat == Ok)
1404 {
1405 if (hrgn)
1406 {
1407 data_size = GetRegionData(hrgn, 0, NULL);
1408
1409 *data = GdipAlloc(data_size);
1410
1411 if (*data)
1412 GetRegionData(hrgn, data_size, *data);
1413 else
1414 stat = OutOfMemory;
1415
1416 DeleteObject(hrgn);
1417 }
1418 else
1419 {
1420 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1421
1422 *data = GdipAlloc(data_size);
1423
1424 if (*data)
1425 {
1426 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1427 (*data)->rdh.iType = RDH_RECTANGLES;
1428 (*data)->rdh.nCount = 1;
1429 (*data)->rdh.nRgnSize = sizeof(RECT);
1430 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1431 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1432
1433 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1434 }
1435 else
1436 stat = OutOfMemory;
1437 }
1438 }
1439
1440 GdipDeleteRegion(region_copy);
1441 }
1442
1443 return stat;
1444 }
1445
1446 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1447 {
1448 GpStatus stat;
1449 LPRGNDATA data;
1450
1451 TRACE("(%p, %p, %p)\n", region, count, matrix);
1452
1453 if (!region || !count || !matrix)
1454 return InvalidParameter;
1455
1456 stat = get_region_scans_data(region, matrix, &data);
1457
1458 if (stat == Ok)
1459 {
1460 *count = data->rdh.nCount;
1461 GdipFree(data);
1462 }
1463
1464 return stat;
1465 }
1466
1467 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1468 {
1469 GpStatus stat;
1470 INT i;
1471 LPRGNDATA data;
1472 RECT *rects;
1473
1474 if (!region || !count || !matrix)
1475 return InvalidParameter;
1476
1477 stat = get_region_scans_data(region, matrix, &data);
1478
1479 if (stat == Ok)
1480 {
1481 *count = data->rdh.nCount;
1482 rects = (RECT*)data->Buffer;
1483
1484 if (scans)
1485 {
1486 for (i=0; i<data->rdh.nCount; i++)
1487 {
1488 scans[i].X = rects[i].left;
1489 scans[i].Y = rects[i].top;
1490 scans[i].Width = rects[i].right - rects[i].left;
1491 scans[i].Height = rects[i].bottom - rects[i].top;
1492 }
1493 }
1494
1495 GdipFree(data);
1496 }
1497
1498 return Ok;
1499 }
1500
1501 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1502 {
1503 GpStatus stat;
1504 INT i;
1505 LPRGNDATA data;
1506 RECT *rects;
1507
1508 if (!region || !count || !matrix)
1509 return InvalidParameter;
1510
1511 stat = get_region_scans_data(region, matrix, &data);
1512
1513 if (stat == Ok)
1514 {
1515 *count = data->rdh.nCount;
1516 rects = (RECT*)data->Buffer;
1517
1518 if (scans)
1519 {
1520 for (i=0; i<data->rdh.nCount; i++)
1521 {
1522 scans[i].X = rects[i].left;
1523 scans[i].Y = rects[i].top;
1524 scans[i].Width = rects[i].right - rects[i].left;
1525 scans[i].Height = rects[i].bottom - rects[i].top;
1526 }
1527 }
1528
1529 GdipFree(data);
1530 }
1531
1532 return Ok;
1533 }