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 /* Test to see if the path could be stored as an array of shorts */
91 static BOOL
is_integer_path(const GpPath
*path
)
95 if (!path
->pathdata
.Count
) return FALSE
;
97 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
100 x
= gdip_round(path
->pathdata
.Points
[i
].X
);
101 y
= gdip_round(path
->pathdata
.Points
[i
].Y
);
102 if (path
->pathdata
.Points
[i
].X
!= (REAL
)x
|| path
->pathdata
.Points
[i
].Y
!= (REAL
)y
)
108 /* Everything is measured in DWORDS; round up if there's a remainder */
109 static inline INT
get_pathtypes_size(const GpPath
* path
)
111 INT needed
= path
->pathdata
.Count
/ sizeof(DWORD
);
113 if (path
->pathdata
.Count
% sizeof(DWORD
) > 0)
116 return needed
* sizeof(DWORD
);
119 static inline INT
get_element_size(const region_element
* element
)
121 INT needed
= sizeof(DWORD
); /* DWORD for the type */
122 switch(element
->type
)
125 return needed
+ sizeof(GpRect
);
128 const GpPath
*path
= element
->elementdata
.path
;
129 DWORD flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
130 /* 3 for headers, once again size doesn't count itself */
131 needed
+= sizeof(DWORD
) * 3;
132 if (flags
& FLAGS_INTPATH
)
133 needed
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
135 needed
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
137 needed
+= get_pathtypes_size(path
);
138 needed
+= sizeof(DWORD
); /* Extra DWORD for pathheader.size */
141 case RegionDataEmptyRect
:
142 case RegionDataInfiniteRect
:
145 needed
+= get_element_size(element
->elementdata
.combine
.left
);
146 needed
+= get_element_size(element
->elementdata
.combine
.right
);
153 /* Does not check parameters, caller must do that */
154 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
156 region
->node
.type
= type
;
157 region
->num_children
= 0;
162 static inline GpStatus
clone_element(const region_element
* element
,
163 region_element
** element2
)
167 /* root node is allocated with GpRegion */
169 *element2
= GdipAlloc(sizeof(region_element
));
174 (*element2
)->type
= element
->type
;
176 switch (element
->type
)
179 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
181 case RegionDataEmptyRect
:
182 case RegionDataInfiniteRect
:
185 stat
= GdipClonePath(element
->elementdata
.path
, &(*element2
)->elementdata
.path
);
186 if (stat
== Ok
) return Ok
;
189 (*element2
)->elementdata
.combine
.left
= NULL
;
190 (*element2
)->elementdata
.combine
.right
= NULL
;
192 stat
= clone_element(element
->elementdata
.combine
.left
,
193 &(*element2
)->elementdata
.combine
.left
);
196 stat
= clone_element(element
->elementdata
.combine
.right
,
197 &(*element2
)->elementdata
.combine
.right
);
198 if (stat
== Ok
) return Ok
;
203 delete_element(*element2
);
208 /* Common code for CombineRegion*
209 * All the caller has to do is get its format into an element
211 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
212 region_element
* right
, const CombineMode mode
)
214 region
->node
.type
= mode
;
215 region
->node
.elementdata
.combine
.left
= left
;
216 region
->node
.elementdata
.combine
.right
= right
;
217 region
->num_children
+= 2;
220 /*****************************************************************************
221 * GdipCloneRegion [GDIPLUS.@]
223 * Creates a deep copy of the region
226 * region [I] source region
227 * clone [O] resulting clone
231 * FAILURE: InvalidParameter or OutOfMemory
233 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
235 region_element
*element
;
237 TRACE("%p %p\n", region
, clone
);
239 if (!(region
&& clone
))
240 return InvalidParameter
;
242 *clone
= GdipAlloc(sizeof(GpRegion
));
245 element
= &(*clone
)->node
;
247 (*clone
)->num_children
= region
->num_children
;
248 return clone_element(®ion
->node
, &element
);
251 /*****************************************************************************
252 * GdipCombineRegionPath [GDIPLUS.@]
254 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
256 GpRegion
*path_region
;
257 region_element
*left
, *right
= NULL
;
260 TRACE("%p %p %d\n", region
, path
, mode
);
262 if (!(region
&& path
))
263 return InvalidParameter
;
265 stat
= GdipCreateRegionPath(path
, &path_region
);
269 /* simply replace region data */
270 if(mode
== CombineModeReplace
){
271 delete_element(®ion
->node
);
272 memcpy(region
, path_region
, sizeof(GpRegion
));
273 GdipFree(path_region
);
277 left
= GdipAlloc(sizeof(region_element
));
280 *left
= region
->node
;
281 stat
= clone_element(&path_region
->node
, &right
);
284 fuse_region(region
, left
, right
, mode
);
285 GdipDeleteRegion(path_region
);
293 GdipDeleteRegion(path_region
);
297 /*****************************************************************************
298 * GdipCombineRegionRect [GDIPLUS.@]
300 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
301 GDIPCONST GpRectF
*rect
, CombineMode mode
)
303 GpRegion
*rect_region
;
304 region_element
*left
, *right
= NULL
;
307 TRACE("%p %s %d\n", region
, debugstr_rectf(rect
), mode
);
309 if (!(region
&& rect
))
310 return InvalidParameter
;
312 stat
= GdipCreateRegionRect(rect
, &rect_region
);
316 /* simply replace region data */
317 if(mode
== CombineModeReplace
){
318 delete_element(®ion
->node
);
319 memcpy(region
, rect_region
, sizeof(GpRegion
));
320 GdipFree(rect_region
);
324 left
= GdipAlloc(sizeof(region_element
));
327 memcpy(left
, ®ion
->node
, sizeof(region_element
));
328 stat
= clone_element(&rect_region
->node
, &right
);
331 fuse_region(region
, left
, right
, mode
);
332 GdipDeleteRegion(rect_region
);
340 GdipDeleteRegion(rect_region
);
344 /*****************************************************************************
345 * GdipCombineRegionRectI [GDIPLUS.@]
347 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
348 GDIPCONST GpRect
*rect
, CombineMode mode
)
352 TRACE("%p %p %d\n", region
, rect
, mode
);
355 return InvalidParameter
;
357 rectf
.X
= (REAL
)rect
->X
;
358 rectf
.Y
= (REAL
)rect
->Y
;
359 rectf
.Height
= (REAL
)rect
->Height
;
360 rectf
.Width
= (REAL
)rect
->Width
;
362 return GdipCombineRegionRect(region
, &rectf
, mode
);
365 /*****************************************************************************
366 * GdipCombineRegionRegion [GDIPLUS.@]
368 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
369 GpRegion
*region2
, CombineMode mode
)
371 region_element
*left
, *right
= NULL
;
375 TRACE("%p %p %d\n", region1
, region2
, mode
);
377 if(!(region1
&& region2
))
378 return InvalidParameter
;
380 /* simply replace region data */
381 if(mode
== CombineModeReplace
){
382 stat
= GdipCloneRegion(region2
, ®2copy
);
383 if(stat
!= Ok
) return stat
;
385 delete_element(®ion1
->node
);
386 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
391 left
= GdipAlloc(sizeof(region_element
));
395 *left
= region1
->node
;
396 stat
= clone_element(®ion2
->node
, &right
);
403 fuse_region(region1
, left
, right
, mode
);
404 region1
->num_children
+= region2
->num_children
;
409 /*****************************************************************************
410 * GdipCreateRegion [GDIPLUS.@]
412 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
414 TRACE("%p\n", region
);
417 return InvalidParameter
;
419 *region
= GdipAlloc(sizeof(GpRegion
));
423 TRACE("=> %p\n", *region
);
425 return init_region(*region
, RegionDataInfiniteRect
);
428 /*****************************************************************************
429 * GdipCreateRegionPath [GDIPLUS.@]
431 * Creates a GpRegion from a GpPath
434 * path [I] path to base the region on
435 * region [O] pointer to the newly allocated region
439 * FAILURE: InvalidParameter
442 * If a path has no floating point points, its points will be stored as shorts
445 * If a path is empty, it is considered to be an INTPATH
447 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
449 region_element
* element
;
452 TRACE("%p, %p\n", path
, region
);
454 if (!(path
&& region
))
455 return InvalidParameter
;
457 *region
= GdipAlloc(sizeof(GpRegion
));
460 stat
= init_region(*region
, RegionDataPath
);
463 GdipDeleteRegion(*region
);
466 element
= &(*region
)->node
;
468 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
471 GdipDeleteRegion(*region
);
478 /*****************************************************************************
479 * GdipCreateRegionRect [GDIPLUS.@]
481 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
486 TRACE("%p, %p\n", rect
, region
);
488 if (!(rect
&& region
))
489 return InvalidParameter
;
491 *region
= GdipAlloc(sizeof(GpRegion
));
492 stat
= init_region(*region
, RegionDataRect
);
495 GdipDeleteRegion(*region
);
499 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
500 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
501 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
502 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
507 /*****************************************************************************
508 * GdipCreateRegionRectI [GDIPLUS.@]
510 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
515 TRACE("%p, %p\n", rect
, region
);
517 rectf
.X
= (REAL
)rect
->X
;
518 rectf
.Y
= (REAL
)rect
->Y
;
519 rectf
.Width
= (REAL
)rect
->Width
;
520 rectf
.Height
= (REAL
)rect
->Height
;
522 return GdipCreateRegionRect(&rectf
, region
);
525 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
527 FIXME("(%p, %d, %p): stub\n", data
, size
, region
);
530 return NotImplemented
;
534 /******************************************************************************
535 * GdipCreateRegionHrgn [GDIPLUS.@]
537 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
547 TRACE("(%p, %p)\n", hrgn
, region
);
549 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
550 return InvalidParameter
;
552 buf
= GdipAlloc(size
);
556 if(!GetRegionData(hrgn
, size
, buf
)){
561 if(buf
->rdh
.nCount
== 0){
562 if((stat
= GdipCreateRegion(&local
)) != Ok
){
566 if((stat
= GdipSetEmpty(local
)) != Ok
){
568 GdipDeleteRegion(local
);
576 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
581 rect
= (LPRECT
)buf
->Buffer
;
582 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
583 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
584 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
586 GdipDeletePath(path
);
592 stat
= GdipCreateRegionPath(path
, region
);
595 GdipDeletePath(path
);
599 /*****************************************************************************
600 * GdipDeleteRegion [GDIPLUS.@]
602 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
604 TRACE("%p\n", region
);
607 return InvalidParameter
;
609 delete_element(®ion
->node
);
615 /*****************************************************************************
616 * GdipGetRegionBounds [GDIPLUS.@]
618 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
624 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
626 if(!region
|| !graphics
|| !rect
)
627 return InvalidParameter
;
629 /* Contrary to MSDN, native ignores the graphics transform. */
630 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
636 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
637 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
638 TRACE("%p => infinite\n", region
);
642 if(GetRgnBox(hrgn
, &r
)){
645 rect
->Width
= r
.right
- r
.left
;
646 rect
->Height
= r
.bottom
- r
.top
;
647 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
650 status
= GenericError
;
657 /*****************************************************************************
658 * GdipGetRegionBoundsI [GDIPLUS.@]
660 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
665 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
668 return InvalidParameter
;
670 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
672 rect
->X
= gdip_round(rectf
.X
);
673 rect
->Y
= gdip_round(rectf
.Y
);
674 rect
->Width
= gdip_round(rectf
.Width
);
675 rect
->Height
= gdip_round(rectf
.Height
);
681 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
683 location
[*offset
] = write
;
687 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
689 ((FLOAT
*)location
)[*offset
] = write
;
693 static inline void write_packed_point(DWORD
* location
, INT
* offset
,
694 const GpPointF
* write
)
696 packed_point
*point
= (packed_point
*)(location
+ *offset
);
697 point
->X
= gdip_round(write
->X
);
698 point
->Y
= gdip_round(write
->Y
);
702 static inline void write_path_types(DWORD
* location
, INT
* offset
,
705 INT rounded_size
= get_pathtypes_size(path
);
707 memcpy(location
+ *offset
, path
->pathdata
.Types
, path
->pathdata
.Count
);
709 /* The unwritten parts of the DWORD (if any) must be cleared */
710 if (rounded_size
- path
->pathdata
.Count
)
711 ZeroMemory(((BYTE
*)location
) + (*offset
* sizeof(DWORD
)) +
712 path
->pathdata
.Count
, rounded_size
- path
->pathdata
.Count
);
713 *offset
+= rounded_size
/ sizeof(DWORD
);
716 static void write_element(const region_element
* element
, DWORD
*buffer
,
719 write_dword(buffer
, filled
, element
->type
);
720 switch (element
->type
)
722 case CombineModeReplace
:
723 case CombineModeIntersect
:
724 case CombineModeUnion
:
726 case CombineModeExclude
:
727 case CombineModeComplement
:
728 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
729 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
732 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
733 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
734 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
735 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
740 const GpPath
* path
= element
->elementdata
.path
;
749 pathheader
= (struct _pathheader
*)(buffer
+ *filled
);
751 pathheader
->flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
752 /* 3 for headers, once again size doesn't count itself */
753 pathheader
->size
= sizeof(DWORD
) * 3;
754 if (pathheader
->flags
& FLAGS_INTPATH
)
755 pathheader
->size
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
757 pathheader
->size
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
758 pathheader
->size
+= get_pathtypes_size(path
);
759 pathheader
->magic
= VERSION_MAGIC
;
760 pathheader
->count
= path
->pathdata
.Count
;
764 switch (pathheader
->flags
& FLAGS_INTPATH
)
767 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
769 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].X
);
770 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].Y
);
774 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
776 write_packed_point(buffer
, filled
,
777 &path
->pathdata
.Points
[i
]);
781 write_path_types(buffer
, filled
, path
);
784 case RegionDataEmptyRect
:
785 case RegionDataInfiniteRect
:
790 /*****************************************************************************
791 * GdipGetRegionData [GDIPLUS.@]
793 * Returns the header, followed by combining ops and region elements.
796 * region [I] region to retrieve from
797 * buffer [O] buffer to hold the resulting data
798 * size [I] size of the buffer
799 * needed [O] (optional) how much data was written
803 * FAILURE: InvalidParameter
806 * The header contains the size, a checksum, a version string, and the number
807 * of children. The size does not count itself or the checksum.
808 * Version is always something like 0xdbc01001 or 0xdbc01002
810 * An element is a RECT, or PATH; Combining ops are stored as their
811 * CombineMode value. Special regions (infinite, empty) emit just their
812 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
813 * their code followed by a second header for the path followed by the actual
814 * path data. Followed by the flags for each point. The pathheader contains
815 * the size of the data to follow, a version number again, followed by a count
816 * of how many points, and any special flags which may apply. 0x4000 means its
817 * a path of shorts instead of FLOAT.
819 * Combining Ops are stored in reverse order from when they were constructed;
820 * the output is a tree where the left side combining area is always taken
823 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
826 struct _region_header
837 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
839 if (!region
|| !buffer
|| !size
)
840 return InvalidParameter
;
842 status
= GdipGetRegionDataSize(region
, &required
);
843 if (status
!= Ok
) return status
;
846 if (needed
) *needed
= size
;
847 return InsufficientBuffer
;
850 region_header
= (struct _region_header
*)buffer
;
851 region_header
->size
= sizeheader_size
+ get_element_size(®ion
->node
);
852 region_header
->checksum
= 0;
853 region_header
->magic
= VERSION_MAGIC
;
854 region_header
->num_children
= region
->num_children
;
856 /* With few exceptions, everything written is DWORD aligned,
857 * so use that as our base */
858 write_element(®ion
->node
, (DWORD
*)buffer
, &filled
);
861 *needed
= filled
* sizeof(DWORD
);
866 /*****************************************************************************
867 * GdipGetRegionDataSize [GDIPLUS.@]
869 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
871 TRACE("%p, %p\n", region
, needed
);
873 if (!(region
&& needed
))
874 return InvalidParameter
;
876 /* header.size doesn't count header.size and header.checksum */
877 *needed
= sizeof(DWORD
) * 2 + sizeheader_size
+ get_element_size(®ion
->node
);
882 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
885 GpGraphics
*new_graphics
=NULL
;
891 new_hdc
= CreateCompatibleDC(0);
895 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
896 graphics
= new_graphics
;
903 else if (!graphics
->hdc
)
905 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
910 save_state
= SaveDC(graphics
->hdc
);
911 EndPath(graphics
->hdc
);
913 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
916 stat
= trace_path(graphics
, path
);
919 *hrgn
= PathToRegion(graphics
->hdc
);
920 stat
= *hrgn
? Ok
: OutOfMemory
;
923 RestoreDC(graphics
->hdc
, save_state
);
928 GdipDeleteGraphics(new_graphics
);
930 graphics
->hdc
= NULL
;
936 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
938 switch (element
->type
)
940 case RegionDataInfiniteRect
:
943 case RegionDataEmptyRect
:
944 *hrgn
= CreateRectRgn(0, 0, 0, 0);
945 return *hrgn
? Ok
: OutOfMemory
;
947 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
952 GpRectF
* rc
= &element
->elementdata
.rect
;
954 stat
= GdipCreatePath(FillModeAlternate
, &path
);
957 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
960 stat
= get_path_hrgn(path
, graphics
, hrgn
);
962 GdipDeletePath(path
);
966 case CombineModeIntersect
:
967 case CombineModeUnion
:
969 case CombineModeExclude
:
970 case CombineModeComplement
:
976 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
985 /* existing region is infinite */
986 switch (element
->type
)
988 case CombineModeIntersect
:
989 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
990 case CombineModeXor
: case CombineModeExclude
:
991 left
= CreateRectRgn(-4194304, -4194304, 4194304, 4194304);
993 case CombineModeUnion
: case CombineModeComplement
:
999 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1009 /* new region is infinite */
1010 switch (element
->type
)
1012 case CombineModeIntersect
:
1015 case CombineModeXor
: case CombineModeComplement
:
1016 right
= CreateRectRgn(-4194304, -4194304, 4194304, 4194304);
1018 case CombineModeUnion
: case CombineModeExclude
:
1025 switch (element
->type
)
1027 case CombineModeIntersect
:
1028 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1030 case CombineModeUnion
:
1031 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1033 case CombineModeXor
:
1034 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1036 case CombineModeExclude
:
1037 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1039 case CombineModeComplement
:
1040 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1046 DeleteObject(right
);
1052 return GenericError
;
1059 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1061 return NotImplemented
;
1065 /*****************************************************************************
1066 * GdipGetRegionHRgn [GDIPLUS.@]
1068 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1070 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1072 if (!region
|| !hrgn
)
1073 return InvalidParameter
;
1075 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1078 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1083 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1085 if(!region
|| !graphics
|| !res
)
1086 return InvalidParameter
;
1088 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1089 if (status
!= Ok
) return status
;
1091 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1092 TRACE("=> %d\n", *res
);
1097 /*****************************************************************************
1098 * GdipIsEqualRegion [GDIPLUS.@]
1100 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1106 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1108 if(!region
|| !region2
|| !graphics
|| !res
)
1109 return InvalidParameter
;
1111 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1114 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1116 DeleteObject(hrgn1
);
1120 *res
= EqualRgn(hrgn1
, hrgn2
);
1122 /* one of GpRegions is infinite */
1124 *res
= (!hrgn1
&& !hrgn2
);
1126 DeleteObject(hrgn1
);
1127 DeleteObject(hrgn2
);
1132 /*****************************************************************************
1133 * GdipIsInfiniteRegion [GDIPLUS.@]
1135 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1137 /* I think graphics is ignored here */
1138 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1140 if(!region
|| !graphics
|| !res
)
1141 return InvalidParameter
;
1143 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1148 /*****************************************************************************
1149 * GdipIsVisibleRegionRect [GDIPLUS.@]
1151 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1157 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1160 return InvalidParameter
;
1162 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1171 rect
.left
= ceilr(x
);
1172 rect
.top
= ceilr(y
);
1173 rect
.right
= ceilr(x
+ w
);
1174 rect
.bottom
= ceilr(y
+ h
);
1176 *res
= RectInRegion(hrgn
, &rect
);
1183 /*****************************************************************************
1184 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1186 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1188 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1190 return InvalidParameter
;
1192 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1195 /*****************************************************************************
1196 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1198 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1203 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1206 return InvalidParameter
;
1208 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1217 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1224 /*****************************************************************************
1225 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1227 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1229 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1231 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1234 /*****************************************************************************
1235 * GdipSetEmpty [GDIPLUS.@]
1237 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1241 TRACE("%p\n", region
);
1244 return InvalidParameter
;
1246 delete_element(®ion
->node
);
1247 stat
= init_region(region
, RegionDataEmptyRect
);
1252 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1256 TRACE("%p\n", region
);
1259 return InvalidParameter
;
1261 delete_element(®ion
->node
);
1262 stat
= init_region(region
, RegionDataInfiniteRect
);
1267 /* Transforms GpRegion elements with given matrix */
1268 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1272 switch(element
->type
)
1274 case RegionDataEmptyRect
:
1275 case RegionDataInfiniteRect
:
1277 case RegionDataRect
:
1279 /* We can't transform a rectangle, so convert it to a path. */
1280 GpRegion
*new_region
;
1283 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1286 stat
= GdipAddPathRectangle(path
,
1287 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1288 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1291 stat
= GdipCreateRegionPath(path
, &new_region
);
1293 GdipDeletePath(path
);
1298 /* Steal the element from the created region. */
1299 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1300 GdipFree(new_region
);
1305 /* Fall-through to do the actual conversion. */
1306 case RegionDataPath
:
1307 if (!element
->elementdata
.path
->pathdata
.Count
)
1310 stat
= GdipTransformMatrixPoints(matrix
,
1311 element
->elementdata
.path
->pathdata
.Points
,
1312 element
->elementdata
.path
->pathdata
.Count
);
1315 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1317 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1322 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1324 TRACE("(%p, %p)\n", region
, matrix
);
1326 if (!region
|| !matrix
)
1327 return InvalidParameter
;
1329 return transform_region_element(®ion
->node
, matrix
);
1332 /* Translates GpRegion elements with specified offsets */
1333 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1337 switch(element
->type
)
1339 case RegionDataEmptyRect
:
1340 case RegionDataInfiniteRect
:
1342 case RegionDataRect
:
1343 element
->elementdata
.rect
.X
+= dx
;
1344 element
->elementdata
.rect
.Y
+= dy
;
1346 case RegionDataPath
:
1347 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1348 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1349 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1353 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1354 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1359 /*****************************************************************************
1360 * GdipTranslateRegion [GDIPLUS.@]
1362 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1364 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1367 return InvalidParameter
;
1369 translate_region_element(®ion
->node
, dx
, dy
);
1374 /*****************************************************************************
1375 * GdipTranslateRegionI [GDIPLUS.@]
1377 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1379 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1381 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1384 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1386 GpRegion
*region_copy
;
1391 stat
= GdipCloneRegion(region
, ®ion_copy
);
1395 stat
= GdipTransformRegion(region_copy
, matrix
);
1398 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1404 data_size
= GetRegionData(hrgn
, 0, NULL
);
1406 *data
= GdipAlloc(data_size
);
1409 GetRegionData(hrgn
, data_size
, *data
);
1417 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1419 *data
= GdipAlloc(data_size
);
1423 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1424 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1425 (*data
)->rdh
.nCount
= 1;
1426 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1427 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1428 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1430 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1437 GdipDeleteRegion(region_copy
);
1443 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1448 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1450 if (!region
|| !count
|| !matrix
)
1451 return InvalidParameter
;
1453 stat
= get_region_scans_data(region
, matrix
, &data
);
1457 *count
= data
->rdh
.nCount
;
1464 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1471 if (!region
|| !count
|| !matrix
)
1472 return InvalidParameter
;
1474 stat
= get_region_scans_data(region
, matrix
, &data
);
1478 *count
= data
->rdh
.nCount
;
1479 rects
= (RECT
*)data
->Buffer
;
1483 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1485 scans
[i
].X
= rects
[i
].left
;
1486 scans
[i
].Y
= rects
[i
].top
;
1487 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1488 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1498 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1505 if (!region
|| !count
|| !matrix
)
1506 return InvalidParameter
;
1508 stat
= get_region_scans_data(region
, matrix
, &data
);
1512 *count
= data
->rdh
.nCount
;
1513 rects
= (RECT
*)data
->Buffer
;
1517 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1519 scans
[i
].X
= rects
[i
].left
;
1520 scans
[i
].Y
= rects
[i
].top
;
1521 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1522 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;