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) return Ok;
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);
172 stat = clone_element(element->elementdata.combine.right,
173 &(*element2)->elementdata.combine.right);
174 if (stat == Ok) return Ok;
179 delete_element(*element2);
184 /* Common code for CombineRegion*
185 * All the caller has to do is get its format into an element
187 static inline void fuse_region(GpRegion* region, region_element* left,
188 region_element* right, const CombineMode mode)
190 region->node.type = mode;
191 region->node.elementdata.combine.left = left;
192 region->node.elementdata.combine.right = right;
194 region->header.size = sizeheader_size + get_element_size(®ion->node);
195 region->header.num_children += 2;
198 /*****************************************************************************
199 * GdipCloneRegion [GDIPLUS.@]
201 * Creates a deep copy of the region
204 * region [I] source region
205 * clone [O] resulting clone
209 * FAILURE: InvalidParameter or OutOfMemory
211 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
213 region_element *element;
215 TRACE("%p %p\n", region, clone);
217 if (!(region && clone))
218 return InvalidParameter;
220 *clone = GdipAlloc(sizeof(GpRegion));
223 element = &(*clone)->node;
225 (*clone)->header = region->header;
226 return clone_element(®ion->node, &element);
229 /*****************************************************************************
230 * GdipCombineRegionPath [GDIPLUS.@]
232 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
234 GpRegion *path_region;
235 region_element *left, *right = NULL;
238 TRACE("%p %p %d\n", region, path, mode);
240 if (!(region && path))
241 return InvalidParameter;
243 stat = GdipCreateRegionPath(path, &path_region);
247 /* simply replace region data */
248 if(mode == CombineModeReplace){
249 delete_element(®ion->node);
250 memcpy(region, path_region, sizeof(GpRegion));
251 GdipFree(path_region);
255 left = GdipAlloc(sizeof(region_element));
258 *left = region->node;
259 stat = clone_element(&path_region->node, &right);
262 fuse_region(region, left, right, mode);
263 GdipDeleteRegion(path_region);
271 GdipDeleteRegion(path_region);
275 /*****************************************************************************
276 * GdipCombineRegionRect [GDIPLUS.@]
278 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
279 GDIPCONST GpRectF *rect, CombineMode mode)
281 GpRegion *rect_region;
282 region_element *left, *right = NULL;
285 TRACE("%p %p %d\n", region, rect, mode);
287 if (!(region && rect))
288 return InvalidParameter;
290 stat = GdipCreateRegionRect(rect, &rect_region);
294 /* simply replace region data */
295 if(mode == CombineModeReplace){
296 delete_element(®ion->node);
297 memcpy(region, rect_region, sizeof(GpRegion));
298 GdipFree(rect_region);
302 left = GdipAlloc(sizeof(region_element));
305 memcpy(left, ®ion->node, sizeof(region_element));
306 stat = clone_element(&rect_region->node, &right);
309 fuse_region(region, left, right, mode);
310 GdipDeleteRegion(rect_region);
318 GdipDeleteRegion(rect_region);
322 /*****************************************************************************
323 * GdipCombineRegionRectI [GDIPLUS.@]
325 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
326 GDIPCONST GpRect *rect, CombineMode mode)
330 TRACE("%p %p %d\n", region, rect, mode);
333 return InvalidParameter;
335 rectf.X = (REAL)rect->X;
336 rectf.Y = (REAL)rect->Y;
337 rectf.Height = (REAL)rect->Height;
338 rectf.Width = (REAL)rect->Width;
340 return GdipCombineRegionRect(region, &rectf, mode);
343 /*****************************************************************************
344 * GdipCombineRegionRegion [GDIPLUS.@]
346 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
347 GpRegion *region2, CombineMode mode)
349 region_element *left, *right = NULL;
353 TRACE("%p %p %d\n", region1, region2, mode);
355 if(!(region1 && region2))
356 return InvalidParameter;
358 /* simply replace region data */
359 if(mode == CombineModeReplace){
360 stat = GdipCloneRegion(region2, ®2copy);
361 if(stat != Ok) return stat;
363 delete_element(®ion1->node);
364 memcpy(region1, reg2copy, sizeof(GpRegion));
369 left = GdipAlloc(sizeof(region_element));
373 *left = region1->node;
374 stat = clone_element(®ion2->node, &right);
381 fuse_region(region1, left, right, mode);
382 region1->header.num_children += region2->header.num_children;
387 /*****************************************************************************
388 * GdipCreateRegion [GDIPLUS.@]
390 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
392 TRACE("%p\n", region);
395 return InvalidParameter;
397 *region = GdipAlloc(sizeof(GpRegion));
401 TRACE("=> %p\n", *region);
403 return init_region(*region, RegionDataInfiniteRect);
406 /*****************************************************************************
407 * GdipCreateRegionPath [GDIPLUS.@]
409 * Creates a GpRegion from a GpPath
412 * path [I] path to base the region on
413 * region [O] pointer to the newly allocated region
417 * FAILURE: InvalidParameter
420 * If a path has no floating point points, its points will be stored as shorts
423 * If a path is empty, it is considered to be an INTPATH
425 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
427 region_element* element;
432 DWORD flags = FLAGS_INTPATH;
435 TRACE("%p, %p\n", path, region);
437 if (!(path && region))
438 return InvalidParameter;
440 *region = GdipAlloc(sizeof(GpRegion));
443 stat = init_region(*region, RegionDataPath);
446 GdipDeleteRegion(*region);
449 element = &(*region)->node;
450 count = path->pathdata.Count;
452 /* Test to see if the path is an Integer path */
455 pointsi = GdipAlloc(sizeof(GpPoint) * count);
456 pointsf = GdipAlloc(sizeof(GpPointF) * count);
457 if (!(pointsi && pointsf))
461 GdipDeleteRegion(*region);
465 stat = GdipGetPathPointsI(path, pointsi, count);
468 GdipDeleteRegion(*region);
471 stat = GdipGetPathPoints(path, pointsf, count);
474 GdipDeleteRegion(*region);
478 for (i = 0; i < count; i++)
480 if (!(pointsi[i].X == pointsf[i].X &&
481 pointsi[i].Y == pointsf[i].Y ))
483 flags = FLAGS_NOFLAGS;
491 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
494 GdipDeleteRegion(*region);
498 /* 3 for headers, once again size doesn't count itself */
499 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
502 /* Floats, sent out as floats */
504 element->elementdata.pathdata.pathheader.size +=
505 (sizeof(DWORD) * count * 2);
507 /* INTs, sent out as packed shorts */
509 element->elementdata.pathdata.pathheader.size +=
510 (sizeof(DWORD) * count);
513 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
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);
524 /*****************************************************************************
525 * GdipCreateRegionRect [GDIPLUS.@]
527 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
532 TRACE("%p, %p\n", rect, region);
534 if (!(rect && region))
535 return InvalidParameter;
537 *region = GdipAlloc(sizeof(GpRegion));
538 stat = init_region(*region, RegionDataRect);
541 GdipDeleteRegion(*region);
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;
553 /*****************************************************************************
554 * GdipCreateRegionRectI [GDIPLUS.@]
556 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
561 TRACE("%p, %p\n", rect, region);
563 rectf.X = (REAL)rect->X;
564 rectf.Y = (REAL)rect->Y;
565 rectf.Width = (REAL)rect->Width;
566 rectf.Height = (REAL)rect->Height;
568 return GdipCreateRegionRect(&rectf, region);
571 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
573 FIXME("(%p, %d, %p): stub\n", data, size, region);
576 return NotImplemented;
580 /******************************************************************************
581 * GdipCreateRegionHrgn [GDIPLUS.@]
583 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
593 TRACE("(%p, %p)\n", hrgn, region);
595 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
596 return InvalidParameter;
598 buf = GdipAlloc(size);
602 if(!GetRegionData(hrgn, size, buf)){
607 if(buf->rdh.nCount == 0){
608 if((stat = GdipCreateRegion(&local)) != Ok){
612 if((stat = GdipSetEmpty(local)) != Ok){
614 GdipDeleteRegion(local);
622 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
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){
632 GdipDeletePath(path);
638 stat = GdipCreateRegionPath(path, region);
641 GdipDeletePath(path);
645 /*****************************************************************************
646 * GdipDeleteRegion [GDIPLUS.@]
648 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
650 TRACE("%p\n", region);
653 return InvalidParameter;
655 delete_element(®ion->node);
661 /*****************************************************************************
662 * GdipGetRegionBounds [GDIPLUS.@]
664 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
670 TRACE("(%p, %p, %p)\n", region, graphics, rect);
672 if(!region || !graphics || !rect)
673 return InvalidParameter;
675 /* Contrary to MSDN, native ignores the graphics transform. */
676 status = GdipGetRegionHRgn(region, NULL, &hrgn);
682 rect->X = rect->Y = -(REAL)(1 << 22);
683 rect->Width = rect->Height = (REAL)(1 << 23);
684 TRACE("%p => infinite\n", region);
688 if(GetRgnBox(hrgn, &r)){
691 rect->Width = r.right - r.left;
692 rect->Height = r.bottom - r.top;
693 TRACE("%p => %s\n", region, debugstr_rectf(rect));
696 status = GenericError;
703 /*****************************************************************************
704 * GdipGetRegionBoundsI [GDIPLUS.@]
706 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
711 TRACE("(%p, %p, %p)\n", region, graphics, rect);
714 return InvalidParameter;
716 status = GdipGetRegionBounds(region, graphics, &rectf);
718 rect->X = roundr(rectf.X);
719 rect->Y = roundr(rectf.X);
720 rect->Width = roundr(rectf.Width);
721 rect->Height = roundr(rectf.Height);
727 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
729 location[*offset] = write;
733 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
735 ((FLOAT*)location)[*offset] = write;
739 static inline void write_packed_point(DWORD* location, INT* offset,
740 const GpPointF* write)
746 memcpy(location + *offset, &point, sizeof(packed_point));
750 static inline void write_path_types(DWORD* location, INT* offset,
753 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
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));
763 static void write_element(const region_element* element, DWORD *buffer,
766 write_dword(buffer, filled, element->type);
767 switch (element->type)
769 case CombineModeReplace:
770 case CombineModeIntersect:
771 case CombineModeUnion:
773 case CombineModeExclude:
774 case CombineModeComplement:
775 write_element(element->elementdata.combine.left, buffer, filled);
776 write_element(element->elementdata.combine.right, buffer, filled);
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);
787 const GpPath* path = element->elementdata.pathdata.path;
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)
795 for (i = 0; i < path->pathdata.Count; i++)
797 write_float(buffer, filled, path->pathdata.Points[i].X);
798 write_float(buffer, filled, path->pathdata.Points[i].Y);
802 for (i = 0; i < path->pathdata.Count; i++)
804 write_packed_point(buffer, filled,
805 &path->pathdata.Points[i]);
808 write_path_types(buffer, filled, path);
811 case RegionDataEmptyRect:
812 case RegionDataInfiniteRect:
817 /*****************************************************************************
818 * GdipGetRegionData [GDIPLUS.@]
820 * Returns the header, followed by combining ops and region elements.
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
830 * FAILURE: InvalidParameter
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
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.
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
850 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
855 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
857 if (!(region && buffer && size))
858 return InvalidParameter;
860 memcpy(buffer, ®ion->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(®ion->node, (DWORD*)buffer, &filled);
867 *needed = filled * sizeof(DWORD);
872 /*****************************************************************************
873 * GdipGetRegionDataSize [GDIPLUS.@]
875 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
877 TRACE("%p, %p\n", region, needed);
879 if (!(region && needed))
880 return InvalidParameter;
882 /* header.size doesn't count header.size and header.checksum */
883 *needed = region->header.size + sizeof(DWORD) * 2;
888 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
891 GpGraphics *new_graphics=NULL;
901 stat = GdipCreateFromHDC(new_hdc, &new_graphics);
902 graphics = new_graphics;
905 ReleaseDC(0, new_hdc);
909 else if (!graphics->hdc)
911 graphics->hdc = new_hdc = GetDC(0);
916 save_state = SaveDC(graphics->hdc);
917 EndPath(graphics->hdc);
919 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
922 stat = trace_path(graphics, path);
925 *hrgn = PathToRegion(graphics->hdc);
926 stat = *hrgn ? Ok : OutOfMemory;
929 RestoreDC(graphics->hdc, save_state);
932 ReleaseDC(0, new_hdc);
934 GdipDeleteGraphics(new_graphics);
936 graphics->hdc = NULL;
942 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
944 switch (element->type)
946 case RegionDataInfiniteRect:
949 case RegionDataEmptyRect:
950 *hrgn = CreateRectRgn(0, 0, 0, 0);
951 return *hrgn ? Ok : OutOfMemory;
953 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
958 GpRectF* rc = &element->elementdata.rect;
960 stat = GdipCreatePath(FillModeAlternate, &path);
963 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
966 stat = get_path_hrgn(path, graphics, hrgn);
968 GdipDeletePath(path);
972 case CombineModeIntersect:
973 case CombineModeUnion:
975 case CombineModeExclude:
976 case CombineModeComplement:
982 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
991 /* existing region is infinite */
992 switch (element->type)
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");
999 case CombineModeUnion: case CombineModeComplement:
1005 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
1015 /* new region is infinite */
1016 switch (element->type)
1018 case CombineModeIntersect:
1021 case CombineModeXor: case CombineModeComplement:
1022 FIXME("cannot exclude from an infinite region\n");
1024 case CombineModeUnion: case CombineModeExclude:
1031 switch (element->type)
1033 case CombineModeIntersect:
1034 ret = CombineRgn(left, left, right, RGN_AND);
1036 case CombineModeUnion:
1037 ret = CombineRgn(left, left, right, RGN_OR);
1039 case CombineModeXor:
1040 ret = CombineRgn(left, left, right, RGN_XOR);
1042 case CombineModeExclude:
1043 ret = CombineRgn(left, left, right, RGN_DIFF);
1045 case CombineModeComplement:
1046 ret = CombineRgn(left, right, left, RGN_DIFF);
1052 DeleteObject(right);
1058 return GenericError;
1065 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1067 return NotImplemented;
1071 /*****************************************************************************
1072 * GdipGetRegionHRgn [GDIPLUS.@]
1074 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1076 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1078 if (!region || !hrgn)
1079 return InvalidParameter;
1081 return get_region_hrgn(®ion->node, graphics, hrgn);
1084 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1089 TRACE("(%p, %p, %p)\n", region, graphics, res);
1091 if(!region || !graphics || !res)
1092 return InvalidParameter;
1094 status = GdipGetRegionBounds(region, graphics, &rect);
1095 if (status != Ok) return status;
1097 *res = rect.Width == 0.0 && rect.Height == 0.0;
1098 TRACE("=> %d\n", *res);
1103 /*****************************************************************************
1104 * GdipIsEqualRegion [GDIPLUS.@]
1106 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1112 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1114 if(!region || !region2 || !graphics || !res)
1115 return InvalidParameter;
1117 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1120 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1122 DeleteObject(hrgn1);
1126 *res = EqualRgn(hrgn1, hrgn2);
1128 /* one of GpRegions is infinite */
1130 *res = (!hrgn1 && !hrgn2);
1132 DeleteObject(hrgn1);
1133 DeleteObject(hrgn2);
1138 /*****************************************************************************
1139 * GdipIsInfiniteRegion [GDIPLUS.@]
1141 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1143 /* I think graphics is ignored here */
1144 TRACE("(%p, %p, %p)\n", region, graphics, res);
1146 if(!region || !graphics || !res)
1147 return InvalidParameter;
1149 *res = (region->node.type == RegionDataInfiniteRect);
1154 /*****************************************************************************
1155 * GdipIsVisibleRegionRect [GDIPLUS.@]
1157 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1163 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1166 return InvalidParameter;
1168 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1177 rect.left = ceilr(x);
1178 rect.top = ceilr(y);
1179 rect.right = ceilr(x + w);
1180 rect.bottom = ceilr(y + h);
1182 *res = RectInRegion(hrgn, &rect);
1189 /*****************************************************************************
1190 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1192 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1194 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1196 return InvalidParameter;
1198 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1201 /*****************************************************************************
1202 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1204 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1209 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1212 return InvalidParameter;
1214 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1223 *res = PtInRegion(hrgn, roundr(x), roundr(y));
1230 /*****************************************************************************
1231 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1233 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1235 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1237 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1240 /*****************************************************************************
1241 * GdipSetEmpty [GDIPLUS.@]
1243 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1247 TRACE("%p\n", region);
1250 return InvalidParameter;
1252 delete_element(®ion->node);
1253 stat = init_region(region, RegionDataEmptyRect);
1258 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1262 TRACE("%p\n", region);
1265 return InvalidParameter;
1267 delete_element(®ion->node);
1268 stat = init_region(region, RegionDataInfiniteRect);
1273 /* Transforms GpRegion elements with given matrix */
1274 static GpStatus transform_region_element(region_element* element, GpMatrix *matrix)
1278 switch(element->type)
1280 case RegionDataEmptyRect:
1281 case RegionDataInfiniteRect:
1283 case RegionDataRect:
1285 /* We can't transform a rectangle, so convert it to a path. */
1286 GpRegion *new_region;
1289 stat = GdipCreatePath(FillModeAlternate, &path);
1292 stat = GdipAddPathRectangle(path,
1293 element->elementdata.rect.X, element->elementdata.rect.Y,
1294 element->elementdata.rect.Width, element->elementdata.rect.Height);
1297 stat = GdipCreateRegionPath(path, &new_region);
1299 GdipDeletePath(path);
1304 /* Steal the element from the created region. */
1305 memcpy(element, &new_region->node, sizeof(region_element));
1306 HeapFree(GetProcessHeap(), 0, new_region);
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);
1318 stat = transform_region_element(element->elementdata.combine.left, matrix);
1320 stat = transform_region_element(element->elementdata.combine.right, matrix);
1325 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1327 TRACE("(%p, %p)\n", region, matrix);
1329 if (!region || !matrix)
1330 return InvalidParameter;
1332 return transform_region_element(®ion->node, matrix);
1335 /* Translates GpRegion elements with specified offsets */
1336 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1340 switch(element->type)
1342 case RegionDataEmptyRect:
1343 case RegionDataInfiniteRect:
1345 case RegionDataRect:
1346 element->elementdata.rect.X += dx;
1347 element->elementdata.rect.Y += dy;
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;
1356 translate_region_element(element->elementdata.combine.left, dx, dy);
1357 translate_region_element(element->elementdata.combine.right, dx, dy);
1362 /*****************************************************************************
1363 * GdipTranslateRegion [GDIPLUS.@]
1365 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1367 TRACE("(%p, %f, %f)\n", region, dx, dy);
1370 return InvalidParameter;
1372 translate_region_element(®ion->node, dx, dy);
1377 /*****************************************************************************
1378 * GdipTranslateRegionI [GDIPLUS.@]
1380 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1382 TRACE("(%p, %d, %d)\n", region, dx, dy);
1384 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1387 static GpStatus get_region_scans_data(GpRegion *region, GpMatrix *matrix, LPRGNDATA *data)
1389 GpRegion *region_copy;
1394 stat = GdipCloneRegion(region, ®ion_copy);
1398 stat = GdipTransformRegion(region_copy, matrix);
1401 stat = GdipGetRegionHRgn(region_copy, NULL, &hrgn);
1407 data_size = GetRegionData(hrgn, 0, NULL);
1409 *data = GdipAlloc(data_size);
1412 GetRegionData(hrgn, data_size, *data);
1420 data_size = sizeof(RGNDATAHEADER) + sizeof(RECT);
1422 *data = GdipAlloc(data_size);
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;
1433 memcpy((*data)->Buffer, &(*data)->rdh.rcBound, sizeof(RECT));
1440 GdipDeleteRegion(region_copy);
1446 GpStatus WINGDIPAPI GdipGetRegionScansCount(GpRegion *region, UINT *count, GpMatrix *matrix)
1451 TRACE("(%p, %p, %p)\n", region, count, matrix);
1453 if (!region || !count || !matrix)
1454 return InvalidParameter;
1456 stat = get_region_scans_data(region, matrix, &data);
1460 *count = data->rdh.nCount;
1467 GpStatus WINGDIPAPI GdipGetRegionScansI(GpRegion *region, GpRect *scans, INT *count, GpMatrix *matrix)
1474 if (!region || !count || !matrix)
1475 return InvalidParameter;
1477 stat = get_region_scans_data(region, matrix, &data);
1481 *count = data->rdh.nCount;
1482 rects = (RECT*)data->Buffer;
1486 for (i=0; i<data->rdh.nCount; i++)
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;
1501 GpStatus WINGDIPAPI GdipGetRegionScans(GpRegion *region, GpRectF *scans, INT *count, GpMatrix *matrix)
1508 if (!region || !count || !matrix)
1509 return InvalidParameter;
1511 stat = get_region_scans_data(region, matrix, &data);
1515 *count = data->rdh.nCount;
1516 rects = (RECT*)data->Buffer;
1520 for (i=0; i<data->rdh.nCount; i++)
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;