2 * Copyright (C) 2008 Google (Lei Zhang)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
33 /**********************************************************
35 * Data returned by GdipGetRegionData looks something like this:
37 * struct region_data_header
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
45 * Then follows a sequence of combining ops and region elements.
47 * A region element is either a RECTF or some path data.
49 * Combining ops are just stored as their CombineMode value.
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).
55 * Path data is preceded by the DWORD 0x10000001. Then follows a
56 * DWORD size and then size bytes of data.
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
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
71 * When a simple region (1 element) is combined, it's treated as if a
72 * single rect/path is being combined.
76 #define FLAGS_NOFLAGS 0x0
77 #define FLAGS_INTPATH 0x4000
79 /* Header size as far as header->size is concerned. This doesn't include
80 * header->size or header->checksum
82 static const INT sizeheader_size = sizeof(DWORD) * 2;
84 typedef struct packed_point
90 /* Everything is measured in DWORDS; round up if there's a remainder */
91 static inline INT get_pathtypes_size(const GpPath* path)
93 INT needed = path->pathdata.Count / sizeof(DWORD);
95 if (path->pathdata.Count % sizeof(DWORD) > 0)
98 return needed * sizeof(DWORD);
101 static inline INT get_element_size(const region_element* element)
103 INT needed = sizeof(DWORD); /* DWORD for the type */
104 switch(element->type)
107 return needed + sizeof(GpRect);
109 needed += element->elementdata.pathdata.pathheader.size;
110 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
112 case RegionDataEmptyRect:
113 case RegionDataInfiniteRect:
116 needed += get_element_size(element->elementdata.combine.left);
117 needed += get_element_size(element->elementdata.combine.right);
124 /* Does not check parameters, caller must do that */
125 static inline GpStatus init_region(GpRegion* region, const RegionType type)
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(®ion->node);
136 static inline GpStatus clone_element(const region_element* element,
137 region_element** element2)
141 /* root node is allocated with GpRegion */
143 *element2 = GdipAlloc(sizeof(region_element));
148 (*element2)->type = element->type;
150 switch (element->type)
153 (*element2)->elementdata.rect = element->elementdata.rect;
155 case RegionDataEmptyRect:
156 case RegionDataInfiniteRect:
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) goto clone_out;
165 (*element2)->elementdata.combine.left = NULL;
166 (*element2)->elementdata.combine.right = NULL;
168 stat = clone_element(element->elementdata.combine.left,
169 &(*element2)->elementdata.combine.left);
170 if (stat != Ok) goto clone_out;
171 stat = clone_element(element->elementdata.combine.right,
172 &(*element2)->elementdata.combine.right);
173 if (stat != Ok) goto clone_out;
180 delete_element(*element2);
185 /* Common code for CombineRegion*
186 * All the caller has to do is get its format into an element
188 static inline void fuse_region(GpRegion* region, region_element* left,
189 region_element* right, const CombineMode mode)
191 region->node.type = mode;
192 region->node.elementdata.combine.left = left;
193 region->node.elementdata.combine.right = right;
195 region->header.size = sizeheader_size + get_element_size(®ion->node);
196 region->header.num_children += 2;
199 /*****************************************************************************
200 * GdipCloneRegion [GDIPLUS.@]
202 * Creates a deep copy of the region
205 * region [I] source region
206 * clone [O] resulting clone
210 * FAILURE: InvalidParameter or OutOfMemory
212 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
214 region_element *element;
216 TRACE("%p %p\n", region, clone);
218 if (!(region && clone))
219 return InvalidParameter;
221 *clone = GdipAlloc(sizeof(GpRegion));
224 element = &(*clone)->node;
226 (*clone)->header = region->header;
227 return clone_element(®ion->node, &element);
230 /*****************************************************************************
231 * GdipCombineRegionPath [GDIPLUS.@]
233 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
235 GpRegion *path_region;
236 region_element *left, *right = NULL;
239 TRACE("%p %p %d\n", region, path, mode);
241 if (!(region && path))
242 return InvalidParameter;
244 stat = GdipCreateRegionPath(path, &path_region);
248 /* simply replace region data */
249 if(mode == CombineModeReplace){
250 delete_element(®ion->node);
251 memcpy(region, path_region, sizeof(GpRegion));
252 GdipFree(path_region);
256 left = GdipAlloc(sizeof(region_element));
259 *left = region->node;
260 stat = clone_element(&path_region->node, &right);
263 fuse_region(region, left, right, mode);
264 GdipDeleteRegion(path_region);
272 GdipDeleteRegion(path_region);
276 /*****************************************************************************
277 * GdipCombineRegionRect [GDIPLUS.@]
279 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
280 GDIPCONST GpRectF *rect, CombineMode mode)
282 GpRegion *rect_region;
283 region_element *left, *right = NULL;
286 TRACE("%p %p %d\n", region, rect, mode);
288 if (!(region && rect))
289 return InvalidParameter;
291 stat = GdipCreateRegionRect(rect, &rect_region);
295 /* simply replace region data */
296 if(mode == CombineModeReplace){
297 delete_element(®ion->node);
298 memcpy(region, rect_region, sizeof(GpRegion));
299 GdipFree(rect_region);
303 left = GdipAlloc(sizeof(region_element));
306 memcpy(left, ®ion->node, sizeof(region_element));
307 stat = clone_element(&rect_region->node, &right);
310 fuse_region(region, left, right, mode);
311 GdipDeleteRegion(rect_region);
319 GdipDeleteRegion(rect_region);
323 /*****************************************************************************
324 * GdipCombineRegionRectI [GDIPLUS.@]
326 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
327 GDIPCONST GpRect *rect, CombineMode mode)
331 TRACE("%p %p %d\n", region, rect, mode);
334 return InvalidParameter;
336 rectf.X = (REAL)rect->X;
337 rectf.Y = (REAL)rect->Y;
338 rectf.Height = (REAL)rect->Height;
339 rectf.Width = (REAL)rect->Width;
341 return GdipCombineRegionRect(region, &rectf, mode);
344 /*****************************************************************************
345 * GdipCombineRegionRegion [GDIPLUS.@]
347 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
348 GpRegion *region2, CombineMode mode)
350 region_element *left, *right = NULL;
354 TRACE("%p %p %d\n", region1, region2, mode);
356 if(!(region1 && region2))
357 return InvalidParameter;
359 /* simply replace region data */
360 if(mode == CombineModeReplace){
361 stat = GdipCloneRegion(region2, ®2copy);
362 if(stat != Ok) return stat;
364 delete_element(®ion1->node);
365 memcpy(region1, reg2copy, sizeof(GpRegion));
370 left = GdipAlloc(sizeof(region_element));
374 *left = region1->node;
375 stat = clone_element(®ion2->node, &right);
382 fuse_region(region1, left, right, mode);
383 region1->header.num_children += region2->header.num_children;
388 /*****************************************************************************
389 * GdipCreateRegion [GDIPLUS.@]
391 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
393 TRACE("%p\n", region);
396 return InvalidParameter;
398 *region = GdipAlloc(sizeof(GpRegion));
402 TRACE("=> %p\n", *region);
404 return init_region(*region, RegionDataInfiniteRect);
407 /*****************************************************************************
408 * GdipCreateRegionPath [GDIPLUS.@]
410 * Creates a GpRegion from a GpPath
413 * path [I] path to base the region on
414 * region [O] pointer to the newly allocated region
418 * FAILURE: InvalidParameter
421 * If a path has no floating point points, its points will be stored as shorts
424 * If a path is empty, it is considered to be an INTPATH
426 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
428 region_element* element;
433 DWORD flags = FLAGS_INTPATH;
436 TRACE("%p, %p\n", path, region);
438 if (!(path && region))
439 return InvalidParameter;
441 *region = GdipAlloc(sizeof(GpRegion));
444 stat = init_region(*region, RegionDataPath);
447 GdipDeleteRegion(*region);
450 element = &(*region)->node;
451 count = path->pathdata.Count;
453 /* Test to see if the path is an Integer path */
456 pointsi = GdipAlloc(sizeof(GpPoint) * count);
457 pointsf = GdipAlloc(sizeof(GpPointF) * count);
458 if (!(pointsi && pointsf))
462 GdipDeleteRegion(*region);
466 stat = GdipGetPathPointsI(path, pointsi, count);
469 GdipDeleteRegion(*region);
472 stat = GdipGetPathPoints(path, pointsf, count);
475 GdipDeleteRegion(*region);
479 for (i = 0; i < count; i++)
481 if (!(pointsi[i].X == pointsf[i].X &&
482 pointsi[i].Y == pointsf[i].Y ))
484 flags = FLAGS_NOFLAGS;
492 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
495 GdipDeleteRegion(*region);
499 /* 3 for headers, once again size doesn't count itself */
500 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
503 /* Floats, sent out as floats */
505 element->elementdata.pathdata.pathheader.size +=
506 (sizeof(DWORD) * count * 2);
508 /* INTs, sent out as packed shorts */
510 element->elementdata.pathdata.pathheader.size +=
511 (sizeof(DWORD) * count);
514 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
516 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
517 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
518 element->elementdata.pathdata.pathheader.count = count;
519 element->elementdata.pathdata.pathheader.flags = flags;
520 (*region)->header.size = sizeheader_size + get_element_size(element);
525 /*****************************************************************************
526 * GdipCreateRegionRect [GDIPLUS.@]
528 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
533 TRACE("%p, %p\n", rect, region);
535 if (!(rect && region))
536 return InvalidParameter;
538 *region = GdipAlloc(sizeof(GpRegion));
539 stat = init_region(*region, RegionDataRect);
542 GdipDeleteRegion(*region);
546 (*region)->node.elementdata.rect.X = rect->X;
547 (*region)->node.elementdata.rect.Y = rect->Y;
548 (*region)->node.elementdata.rect.Width = rect->Width;
549 (*region)->node.elementdata.rect.Height = rect->Height;
554 /*****************************************************************************
555 * GdipCreateRegionRectI [GDIPLUS.@]
557 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
562 TRACE("%p, %p\n", rect, region);
564 rectf.X = (REAL)rect->X;
565 rectf.Y = (REAL)rect->Y;
566 rectf.Width = (REAL)rect->Width;
567 rectf.Height = (REAL)rect->Height;
569 return GdipCreateRegionRect(&rectf, region);
572 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
574 FIXME("(%p, %d, %p): stub\n", data, size, region);
577 return NotImplemented;
581 /******************************************************************************
582 * GdipCreateRegionHrgn [GDIPLUS.@]
584 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
594 TRACE("(%p, %p)\n", hrgn, region);
596 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
597 return InvalidParameter;
599 buf = GdipAlloc(size);
603 if(!GetRegionData(hrgn, size, buf)){
608 if(buf->rdh.nCount == 0){
609 if((stat = GdipCreateRegion(&local)) != Ok){
613 if((stat = GdipSetEmpty(local)) != Ok){
615 GdipDeleteRegion(local);
623 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
628 rect = (LPRECT)buf->Buffer;
629 for(i = 0; i < buf->rdh.nCount; i++){
630 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
631 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
633 GdipDeletePath(path);
639 stat = GdipCreateRegionPath(path, region);
642 GdipDeletePath(path);
646 /*****************************************************************************
647 * GdipDeleteRegion [GDIPLUS.@]
649 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
651 TRACE("%p\n", region);
654 return InvalidParameter;
656 delete_element(®ion->node);
662 /*****************************************************************************
663 * GdipGetRegionBounds [GDIPLUS.@]
665 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
671 TRACE("(%p, %p, %p)\n", region, graphics, rect);
673 if(!region || !graphics || !rect)
674 return InvalidParameter;
676 /* Contrary to MSDN, native ignores the graphics transform. */
677 status = GdipGetRegionHRgn(region, NULL, &hrgn);
683 rect->X = rect->Y = -(REAL)(1 << 22);
684 rect->Width = rect->Height = (REAL)(1 << 23);
685 TRACE("%p => infinite\n", region);
689 if(GetRgnBox(hrgn, &r)){
692 rect->Width = r.right - r.left;
693 rect->Height = r.bottom - r.top;
694 TRACE("%p => %s\n", region, debugstr_rectf(rect));
697 status = GenericError;
704 /*****************************************************************************
705 * GdipGetRegionBoundsI [GDIPLUS.@]
707 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
712 TRACE("(%p, %p, %p)\n", region, graphics, rect);
715 return InvalidParameter;
717 status = GdipGetRegionBounds(region, graphics, &rectf);
719 rect->X = roundr(rectf.X);
720 rect->Y = roundr(rectf.X);
721 rect->Width = roundr(rectf.Width);
722 rect->Height = roundr(rectf.Height);
728 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
730 location[*offset] = write;
734 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
736 ((FLOAT*)location)[*offset] = write;
740 static inline void write_packed_point(DWORD* location, INT* offset,
741 const GpPointF* write)
747 memcpy(location + *offset, &point, sizeof(packed_point));
751 static inline void write_path_types(DWORD* location, INT* offset,
754 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
756 /* The unwritten parts of the DWORD (if any) must be cleared */
757 if (path->pathdata.Count % sizeof(DWORD))
758 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
759 path->pathdata.Count,
760 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
761 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
764 static void write_element(const region_element* element, DWORD *buffer,
767 write_dword(buffer, filled, element->type);
768 switch (element->type)
770 case CombineModeReplace:
771 case CombineModeIntersect:
772 case CombineModeUnion:
774 case CombineModeExclude:
775 case CombineModeComplement:
776 write_element(element->elementdata.combine.left, buffer, filled);
777 write_element(element->elementdata.combine.right, buffer, filled);
780 write_float(buffer, filled, element->elementdata.rect.X);
781 write_float(buffer, filled, element->elementdata.rect.Y);
782 write_float(buffer, filled, element->elementdata.rect.Width);
783 write_float(buffer, filled, element->elementdata.rect.Height);
788 const GpPath* path = element->elementdata.pathdata.path;
790 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
791 sizeof(element->elementdata.pathdata.pathheader));
792 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
793 switch (element->elementdata.pathdata.pathheader.flags)
796 for (i = 0; i < path->pathdata.Count; i++)
798 write_float(buffer, filled, path->pathdata.Points[i].X);
799 write_float(buffer, filled, path->pathdata.Points[i].Y);
803 for (i = 0; i < path->pathdata.Count; i++)
805 write_packed_point(buffer, filled,
806 &path->pathdata.Points[i]);
809 write_path_types(buffer, filled, path);
812 case RegionDataEmptyRect:
813 case RegionDataInfiniteRect:
818 /*****************************************************************************
819 * GdipGetRegionData [GDIPLUS.@]
821 * Returns the header, followed by combining ops and region elements.
824 * region [I] region to retrieve from
825 * buffer [O] buffer to hold the resulting data
826 * size [I] size of the buffer
827 * needed [O] (optional) how much data was written
831 * FAILURE: InvalidParameter
834 * The header contains the size, a checksum, a version string, and the number
835 * of children. The size does not count itself or the checksum.
836 * Version is always something like 0xdbc01001 or 0xdbc01002
838 * An element is a RECT, or PATH; Combining ops are stored as their
839 * CombineMode value. Special regions (infinite, empty) emit just their
840 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
841 * their code followed by a second header for the path followed by the actual
842 * path data. Followed by the flags for each point. The pathheader contains
843 * the size of the data to follow, a version number again, followed by a count
844 * of how many points, and any special flags which may apply. 0x4000 means its
845 * a path of shorts instead of FLOAT.
847 * Combining Ops are stored in reverse order from when they were constructed;
848 * the output is a tree where the left side combining area is always taken
851 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
856 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
858 if (!(region && buffer && size))
859 return InvalidParameter;
861 memcpy(buffer, ®ion->header, sizeof(region->header));
862 filled += sizeof(region->header) / sizeof(DWORD);
863 /* With few exceptions, everything written is DWORD aligned,
864 * so use that as our base */
865 write_element(®ion->node, (DWORD*)buffer, &filled);
868 *needed = filled * sizeof(DWORD);
873 /*****************************************************************************
874 * GdipGetRegionDataSize [GDIPLUS.@]
876 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
878 TRACE("%p, %p\n", region, needed);
880 if (!(region && needed))
881 return InvalidParameter;
883 /* header.size doesn't count header.size and header.checksum */
884 *needed = region->header.size + sizeof(DWORD) * 2;
889 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
892 GpGraphics *new_graphics=NULL;
902 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
903 graphics = new_graphics;
906 ReleaseDC(0, new_hdc);
910 else if (!graphics->hdc)
912 graphics->hdc = new_hdc = GetDC(0);
917 save_state = SaveDC(graphics->hdc);
918 EndPath(graphics->hdc);
920 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
923 stat = trace_path(graphics, path);
926 *hrgn = PathToRegion(graphics->hdc);
927 stat = *hrgn ? Ok : OutOfMemory;
930 RestoreDC(graphics->hdc, save_state);
933 ReleaseDC(0, new_hdc);
935 GdipDeleteGraphics(new_graphics);
937 graphics->hdc = NULL;
943 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
945 switch (element->type)
947 case RegionDataInfiniteRect:
950 case RegionDataEmptyRect:
951 *hrgn = CreateRectRgn(0, 0, 0, 0);
952 return *hrgn ? Ok : OutOfMemory;
954 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
959 GpRectF* rc = &element->elementdata.rect;
961 stat = GdipCreatePath(FillModeAlternate, &path);
964 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
967 stat = get_path_hrgn(path, graphics, hrgn);
969 GdipDeletePath(path);
973 case CombineModeIntersect:
974 case CombineModeUnion:
976 case CombineModeExclude:
977 case CombineModeComplement:
983 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
992 /* existing region is infinite */
993 switch (element->type)
995 case CombineModeIntersect:
996 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
997 case CombineModeXor: case CombineModeExclude:
998 FIXME("cannot exclude from an infinite region\n");
1000 case CombineModeUnion: case CombineModeComplement:
1006 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1016 /* new region is infinite */
1017 switch (element->type)
1019 case CombineModeIntersect:
1022 case CombineModeXor: case CombineModeComplement:
1023 FIXME("cannot exclude from an infinite region\n");
1025 case CombineModeUnion: case CombineModeExclude:
1032 switch (element->type)
1034 case CombineModeIntersect:
1035 ret = CombineRgn(left, left, right, RGN_AND);
1037 case CombineModeUnion:
1038 ret = CombineRgn(left, left, right, RGN_OR);
1040 case CombineModeXor:
1041 ret = CombineRgn(left, left, right, RGN_XOR);
1043 case CombineModeExclude:
1044 ret = CombineRgn(left, left, right, RGN_DIFF);
1046 case CombineModeComplement:
1047 ret = CombineRgn(left, right, left, RGN_DIFF);
1053 DeleteObject(right);
1059 return GenericError;
1066 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1068 return NotImplemented;
1072 /*****************************************************************************
1073 * GdipGetRegionHRgn [GDIPLUS.@]
1075 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1077 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1079 if (!region || !hrgn)
1080 return InvalidParameter;
1082 return get_region_hrgn(®ion->node, graphics, hrgn);
1085 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1090 TRACE("(%p, %p, %p)\n", region, graphics, res);
1092 if(!region || !graphics || !res)
1093 return InvalidParameter;
1095 status = GdipGetRegionBounds(region, graphics, &rect);
1096 if (status != Ok) return status;
1098 *res = rect.Width == 0.0 && rect.Height == 0.0;
1099 TRACE("=> %d\n", *res);
1104 /*****************************************************************************
1105 * GdipIsEqualRegion [GDIPLUS.@]
1107 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1113 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1115 if(!region || !region2 || !graphics || !res)
1116 return InvalidParameter;
1118 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1121 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1123 DeleteObject(hrgn1);
1127 *res = EqualRgn(hrgn1, hrgn2);
1129 /* one of GpRegions is infinite */
1131 *res = (!hrgn1 && !hrgn2);
1133 DeleteObject(hrgn1);
1134 DeleteObject(hrgn2);
1139 /*****************************************************************************
1140 * GdipIsInfiniteRegion [GDIPLUS.@]
1142 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1144 /* I think graphics is ignored here */
1145 TRACE("(%p, %p, %p)\n", region, graphics, res);
1147 if(!region || !graphics || !res)
1148 return InvalidParameter;
1150 *res = (region->node.type == RegionDataInfiniteRect);
1155 /*****************************************************************************
1156 * GdipIsVisibleRegionRect [GDIPLUS.@]
1158 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1164 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1167 return InvalidParameter;
1169 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1178 rect.left = ceilr(x);
1179 rect.top = ceilr(y);
1180 rect.right = ceilr(x + w);
1181 rect.bottom = ceilr(y + h);
1183 *res = RectInRegion(hrgn, &rect);
1190 /*****************************************************************************
1191 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1193 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1195 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1197 return InvalidParameter;
1199 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1202 /*****************************************************************************
1203 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1205 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1210 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1213 return InvalidParameter;
1215 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1224 *res = PtInRegion(hrgn, roundr(x), roundr(y));
1231 /*****************************************************************************
1232 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1234 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1236 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1238 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1241 /*****************************************************************************
1242 * GdipSetEmpty [GDIPLUS.@]
1244 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1248 TRACE("%p\n", region);
1251 return InvalidParameter;
1253 delete_element(®ion->node);
1254 stat = init_region(region, RegionDataEmptyRect);
1259 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1263 TRACE("%p\n", region);
1266 return InvalidParameter;
1268 delete_element(®ion->node);
1269 stat = init_region(region, RegionDataInfiniteRect);
1274 /* Transforms GpRegion elements with given matrix */
1275 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1279 switch(element->type)
1281 case RegionDataEmptyRect:
1282 case RegionDataInfiniteRect:
1284 case RegionDataRect:
1286 /* We can't transform a rectangle, so convert it to a path. */
1287 GpRegion *new_region;
1290 stat = GdipCreatePath(FillModeAlternate, &path);
1293 stat = GdipAddPathRectangle(path,
1294 element->elementdata.rect.X, element->elementdata.rect.Y,
1295 element->elementdata.rect.Width, element->elementdata.rect.Height);
1298 stat = GdipCreateRegionPath(path, &new_region);
1300 GdipDeletePath(path);
1305 /* Steal the element from the created region. */
1306 memcpy(element, &new_region->node, sizeof(region_element));
1307 HeapFree(GetProcessHeap(), 0, new_region);
1312 /* Fall-through to do the actual conversion. */
1313 case RegionDataPath:
1314 stat = GdipTransformMatrixPoints(matrix,
1315 element->elementdata.pathdata.path->pathdata.Points,
1316 element->elementdata.pathdata.path->pathdata.Count);
1319 stat = transform_region_element(element->elementdata.combine.left, matrix);
1321 stat = transform_region_element(element->elementdata.combine.right, matrix);
1326 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1328 TRACE("(%p, %p)\n", region, matrix);
1330 if (!region || !matrix)
1331 return InvalidParameter;
1333 return transform_region_element(®ion->node, matrix);
1336 /* Translates GpRegion elements with specified offsets */
1337 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1341 switch(element->type)
1343 case RegionDataEmptyRect:
1344 case RegionDataInfiniteRect:
1346 case RegionDataRect:
1347 element->elementdata.rect.X += dx;
1348 element->elementdata.rect.Y += dy;
1350 case RegionDataPath:
1351 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1352 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1353 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1357 translate_region_element(element->elementdata.combine.left, dx, dy);
1358 translate_region_element(element->elementdata.combine.right, dx, dy);
1363 /*****************************************************************************
1364 * GdipTranslateRegion [GDIPLUS.@]
1366 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1368 TRACE("(%p, %f, %f)\n", region, dx, dy);
1371 return InvalidParameter;
1373 translate_region_element(®ion->node, dx, dy);
1378 /*****************************************************************************
1379 * GdipTranslateRegionI [GDIPLUS.@]
1381 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1383 TRACE("(%p, %d, %d)\n", region, dx, dy);
1385 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1388 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1390 GpRegion *region_copy;
1395 stat = GdipCloneRegion(region, ®ion_copy);
1399 stat = GdipTransformRegion(region_copy, matrix);
1402 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1408 data_size = GetRegionData(hrgn, 0, NULL);
1410 *data = GdipAlloc(data_size);
1413 GetRegionData(hrgn, data_size, *data);
1421 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1423 *data = GdipAlloc(data_size);
1427 (*data)->rdh.dwSize = sizeof(RGNDATAHEADER);
1428 (*data)->rdh.iType = RDH_RECTANGLES;
1429 (*data)->rdh.nCount = 1;
1430 (*data)->rdh.nRgnSize = sizeof(RECT);
1431 (*data)->rdh.rcBound.left = (*data)->rdh.rcBound.top = -0x400000;
1432 (*data)->rdh.rcBound.right = (*data)->rdh.rcBound.bottom = 0x400000;
1434 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1441 GdipDeleteRegion(region_copy);
1447 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1452 TRACE("(%p, %p, %p)\n", region, count, matrix);
1454 if (!region || !count || !matrix)
1455 return InvalidParameter;
1457 stat = get_region_scans_data(region, matrix, &data);
1461 *count = data->rdh.nCount;
1468 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1475 if (!region || !count || !matrix)
1476 return InvalidParameter;
1478 stat = get_region_scans_data(region, matrix, &data);
1482 *count = data->rdh.nCount;
1483 rects = (RECT*)data->Buffer;
1487 for (i=0; i<data->rdh.nCount; i++)
1489 scans[i].X = rects[i].left;
1490 scans[i].Y = rects[i].top;
1491 scans[i].Width = rects[i].right - rects[i].left;
1492 scans[i].Height = rects[i].bottom - rects[i].top;
1502 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1509 if (!region || !count || !matrix)
1510 return InvalidParameter;
1512 stat = get_region_scans_data(region, matrix, &data);
1516 *count = data->rdh.nCount;
1517 rects = (RECT*)data->Buffer;
1521 for (i=0; i<data->rdh.nCount; i++)
1523 scans[i].X = rects[i].left;
1524 scans[i].Y = rects[i].top;
1525 scans[i].Width = rects[i].right - rects[i].left;
1526 scans[i].Height = rects[i].bottom - rects[i].top;