2 * Copyright (C) 2008 Google (Lei Zhang)
3 * Copyright (C) 2013 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "gdiplus_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
34 /**********************************************************
36 * Data returned by GdipGetRegionData looks something like this:
38 * struct region_data_header
40 * DWORD size; size in bytes of the data - 8.
41 * DWORD magic1; probably a checksum.
42 * DWORD magic2; always seems to be 0xdbc01001 - version?
43 * DWORD num_ops; number of combining ops * 2
46 * Then follows a sequence of combining ops and region elements.
48 * A region element is either a RECTF or some path data.
50 * Combining ops are just stored as their CombineMode value.
52 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
53 * stored as 0x10000002 (with no following RECTF) and an infinite rect
54 * is stored as 0x10000003 (again with no following RECTF).
56 * Path data is preceded by the DWORD 0x10000001. Then follows a
57 * DWORD size and then size bytes of data.
59 * The combining ops are stored in the reverse order to the region
60 * elements and in the reverse order to which the region was
63 * When two or more complex regions (ie those with more than one
64 * element) are combined, the combining op for the two regions comes
65 * first, then the combining ops for the region elements in region 1,
66 * followed by the region elements for region 1, then follows the
67 * combining ops for region 2 and finally region 2's region elements.
68 * Presumably you're supposed to use the 0x1000000x header to find the
69 * end of the op list (the count of the elements in each region is not
72 * When a simple region (1 element) is combined, it's treated as if a
73 * single rect/path is being combined.
77 #define FLAGS_INTPATH 0x4000
91 struct region_data_header
95 struct region_header header
;
106 typedef struct packed_point
112 static inline INT
get_element_size(const region_element
* element
)
114 INT needed
= sizeof(DWORD
); /* DWORD for the type */
115 switch(element
->type
)
118 return needed
+ sizeof(GpRect
);
121 needed
+= write_path_data(element
->elementdata
.path
, NULL
);
122 needed
+= sizeof(DWORD
); /* Extra DWORD for path size */
125 case RegionDataEmptyRect
:
126 case RegionDataInfiniteRect
:
129 needed
+= get_element_size(element
->elementdata
.combine
.left
);
130 needed
+= get_element_size(element
->elementdata
.combine
.right
);
137 /* Does not check parameters, caller must do that */
138 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
140 region
->node
.type
= type
;
141 region
->num_children
= 0;
146 static inline GpStatus
clone_element(const region_element
* element
,
147 region_element
** element2
)
151 /* root node is allocated with GpRegion */
153 *element2
= heap_alloc_zero(sizeof(region_element
));
158 (*element2
)->type
= element
->type
;
160 switch (element
->type
)
163 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
165 case RegionDataEmptyRect
:
166 case RegionDataInfiniteRect
:
169 stat
= GdipClonePath(element
->elementdata
.path
, &(*element2
)->elementdata
.path
);
170 if (stat
== Ok
) return Ok
;
173 (*element2
)->elementdata
.combine
.left
= NULL
;
174 (*element2
)->elementdata
.combine
.right
= NULL
;
176 stat
= clone_element(element
->elementdata
.combine
.left
,
177 &(*element2
)->elementdata
.combine
.left
);
180 stat
= clone_element(element
->elementdata
.combine
.right
,
181 &(*element2
)->elementdata
.combine
.right
);
182 if (stat
== Ok
) return Ok
;
187 delete_element(*element2
);
192 /* Common code for CombineRegion*
193 * All the caller has to do is get its format into an element
195 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
196 region_element
* right
, const CombineMode mode
)
198 region
->node
.type
= mode
;
199 region
->node
.elementdata
.combine
.left
= left
;
200 region
->node
.elementdata
.combine
.right
= right
;
201 region
->num_children
+= 2;
204 /*****************************************************************************
205 * GdipCloneRegion [GDIPLUS.@]
207 * Creates a deep copy of the region
210 * region [I] source region
211 * clone [O] resulting clone
215 * FAILURE: InvalidParameter or OutOfMemory
217 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
219 region_element
*element
;
221 TRACE("%p %p\n", region
, clone
);
223 if (!(region
&& clone
))
224 return InvalidParameter
;
226 *clone
= heap_alloc_zero(sizeof(GpRegion
));
229 element
= &(*clone
)->node
;
231 (*clone
)->num_children
= region
->num_children
;
232 return clone_element(®ion
->node
, &element
);
235 /*****************************************************************************
236 * GdipCombineRegionPath [GDIPLUS.@]
238 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
240 GpRegion
*path_region
;
241 region_element
*left
, *right
= NULL
;
244 TRACE("%p %p %d\n", region
, path
, mode
);
246 if (!(region
&& path
))
247 return InvalidParameter
;
249 stat
= GdipCreateRegionPath(path
, &path_region
);
253 /* simply replace region data */
254 if(mode
== CombineModeReplace
){
255 delete_element(®ion
->node
);
256 memcpy(region
, path_region
, sizeof(GpRegion
));
257 heap_free(path_region
);
261 left
= heap_alloc_zero(sizeof(region_element
));
264 *left
= region
->node
;
265 stat
= clone_element(&path_region
->node
, &right
);
268 fuse_region(region
, left
, right
, mode
);
269 GdipDeleteRegion(path_region
);
277 GdipDeleteRegion(path_region
);
281 /*****************************************************************************
282 * GdipCombineRegionRect [GDIPLUS.@]
284 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
285 GDIPCONST GpRectF
*rect
, CombineMode mode
)
287 GpRegion
*rect_region
;
288 region_element
*left
, *right
= NULL
;
291 TRACE("%p %s %d\n", region
, debugstr_rectf(rect
), mode
);
293 if (!(region
&& rect
))
294 return InvalidParameter
;
296 stat
= GdipCreateRegionRect(rect
, &rect_region
);
300 /* simply replace region data */
301 if(mode
== CombineModeReplace
){
302 delete_element(®ion
->node
);
303 memcpy(region
, rect_region
, sizeof(GpRegion
));
304 heap_free(rect_region
);
308 left
= heap_alloc_zero(sizeof(region_element
));
311 memcpy(left
, ®ion
->node
, sizeof(region_element
));
312 stat
= clone_element(&rect_region
->node
, &right
);
315 fuse_region(region
, left
, right
, mode
);
316 GdipDeleteRegion(rect_region
);
324 GdipDeleteRegion(rect_region
);
328 /*****************************************************************************
329 * GdipCombineRegionRectI [GDIPLUS.@]
331 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
332 GDIPCONST GpRect
*rect
, CombineMode mode
)
336 TRACE("%p %p %d\n", region
, rect
, mode
);
339 return InvalidParameter
;
341 rectf
.X
= (REAL
)rect
->X
;
342 rectf
.Y
= (REAL
)rect
->Y
;
343 rectf
.Height
= (REAL
)rect
->Height
;
344 rectf
.Width
= (REAL
)rect
->Width
;
346 return GdipCombineRegionRect(region
, &rectf
, mode
);
349 /*****************************************************************************
350 * GdipCombineRegionRegion [GDIPLUS.@]
352 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
353 GpRegion
*region2
, CombineMode mode
)
355 region_element
*left
, *right
= NULL
;
359 TRACE("%p %p %d\n", region1
, region2
, mode
);
361 if(!(region1
&& region2
))
362 return InvalidParameter
;
364 /* simply replace region data */
365 if(mode
== CombineModeReplace
){
366 stat
= GdipCloneRegion(region2
, ®2copy
);
367 if(stat
!= Ok
) return stat
;
369 delete_element(®ion1
->node
);
370 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
375 left
= heap_alloc_zero(sizeof(region_element
));
379 *left
= region1
->node
;
380 stat
= clone_element(®ion2
->node
, &right
);
387 fuse_region(region1
, left
, right
, mode
);
388 region1
->num_children
+= region2
->num_children
;
393 /*****************************************************************************
394 * GdipCreateRegion [GDIPLUS.@]
396 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
398 TRACE("%p\n", region
);
401 return InvalidParameter
;
403 *region
= heap_alloc_zero(sizeof(GpRegion
));
407 TRACE("=> %p\n", *region
);
409 return init_region(*region
, RegionDataInfiniteRect
);
412 /*****************************************************************************
413 * GdipCreateRegionPath [GDIPLUS.@]
415 * Creates a GpRegion from a GpPath
418 * path [I] path to base the region on
419 * region [O] pointer to the newly allocated region
423 * FAILURE: InvalidParameter
426 * If a path has no floating point points, its points will be stored as shorts
429 * If a path is empty, it is considered to be an INTPATH
431 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
433 region_element
* element
;
436 TRACE("%p, %p\n", path
, region
);
438 if (!(path
&& region
))
439 return InvalidParameter
;
441 *region
= heap_alloc_zero(sizeof(GpRegion
));
444 stat
= init_region(*region
, RegionDataPath
);
447 GdipDeleteRegion(*region
);
450 element
= &(*region
)->node
;
452 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
455 GdipDeleteRegion(*region
);
462 /*****************************************************************************
463 * GdipCreateRegionRect [GDIPLUS.@]
465 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
470 TRACE("%p, %p\n", rect
, region
);
472 if (!(rect
&& region
))
473 return InvalidParameter
;
475 *region
= heap_alloc_zero(sizeof(GpRegion
));
476 stat
= init_region(*region
, RegionDataRect
);
479 GdipDeleteRegion(*region
);
483 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
484 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
485 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
486 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
491 /*****************************************************************************
492 * GdipCreateRegionRectI [GDIPLUS.@]
494 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
499 TRACE("%p, %p\n", rect
, region
);
501 rectf
.X
= (REAL
)rect
->X
;
502 rectf
.Y
= (REAL
)rect
->Y
;
503 rectf
.Width
= (REAL
)rect
->Width
;
504 rectf
.Height
= (REAL
)rect
->Height
;
506 return GdipCreateRegionRect(&rectf
, region
);
509 /******************************************************************************
510 * GdipCreateRegionHrgn [GDIPLUS.@]
512 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
522 TRACE("(%p, %p)\n", hrgn
, region
);
524 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
525 return InvalidParameter
;
527 buf
= heap_alloc_zero(size
);
531 if(!GetRegionData(hrgn
, size
, buf
)){
536 if(buf
->rdh
.nCount
== 0){
537 if((stat
= GdipCreateRegion(&local
)) != Ok
){
541 if((stat
= GdipSetEmpty(local
)) != Ok
){
543 GdipDeleteRegion(local
);
551 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
556 rect
= (LPRECT
)buf
->Buffer
;
557 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
558 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
559 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
561 GdipDeletePath(path
);
567 stat
= GdipCreateRegionPath(path
, region
);
570 GdipDeletePath(path
);
574 /*****************************************************************************
575 * GdipDeleteRegion [GDIPLUS.@]
577 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
579 TRACE("%p\n", region
);
582 return InvalidParameter
;
584 delete_element(®ion
->node
);
590 /*****************************************************************************
591 * GdipGetRegionBounds [GDIPLUS.@]
593 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
599 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
601 if(!region
|| !graphics
|| !rect
)
602 return InvalidParameter
;
604 /* Contrary to MSDN, native ignores the graphics transform. */
605 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
611 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
612 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
613 TRACE("%p => infinite\n", region
);
617 if(GetRgnBox(hrgn
, &r
)){
620 rect
->Width
= r
.right
- r
.left
;
621 rect
->Height
= r
.bottom
- r
.top
;
622 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
625 status
= GenericError
;
632 /*****************************************************************************
633 * GdipGetRegionBoundsI [GDIPLUS.@]
635 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
640 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
643 return InvalidParameter
;
645 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
647 rect
->X
= gdip_round(rectf
.X
);
648 rect
->Y
= gdip_round(rectf
.Y
);
649 rect
->Width
= gdip_round(rectf
.Width
);
650 rect
->Height
= gdip_round(rectf
.Height
);
656 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
658 location
[*offset
] = write
;
662 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
664 ((FLOAT
*)location
)[*offset
] = write
;
668 static void write_element(const region_element
* element
, DWORD
*buffer
,
671 write_dword(buffer
, filled
, element
->type
);
672 switch (element
->type
)
674 case CombineModeReplace
:
675 case CombineModeIntersect
:
676 case CombineModeUnion
:
678 case CombineModeExclude
:
679 case CombineModeComplement
:
680 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
681 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
684 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
685 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
686 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
687 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
691 DWORD size
= write_path_data(element
->elementdata
.path
, buffer
+ *filled
+ 1);
692 write_dword(buffer
, filled
, size
);
693 *filled
+= size
/ sizeof(DWORD
);
696 case RegionDataEmptyRect
:
697 case RegionDataInfiniteRect
:
702 DWORD
write_region_data(const GpRegion
*region
, void *data
)
704 struct region_header
*header
= data
;
708 size
= sizeof(struct region_header
) + get_element_size(®ion
->node
);
709 if (!data
) return size
;
711 header
->magic
= VERSION_MAGIC2
;
712 header
->num_children
= region
->num_children
;
714 /* With few exceptions, everything written is DWORD aligned,
715 * so use that as our base */
716 write_element(®ion
->node
, (DWORD
*)data
, &filled
);
720 /*****************************************************************************
721 * GdipGetRegionData [GDIPLUS.@]
723 * Returns the header, followed by combining ops and region elements.
726 * region [I] region to retrieve from
727 * buffer [O] buffer to hold the resulting data
728 * size [I] size of the buffer
729 * needed [O] (optional) how much data was written
733 * FAILURE: InvalidParameter
736 * The header contains the size, a checksum, a version string, and the number
737 * of children. The size does not count itself or the checksum.
738 * Version is always something like 0xdbc01001 or 0xdbc01002
740 * An element is a RECT, or PATH; Combining ops are stored as their
741 * CombineMode value. Special regions (infinite, empty) emit just their
742 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
743 * their code followed by a second header for the path followed by the actual
744 * path data. Followed by the flags for each point. The pathheader contains
745 * the size of the data to follow, a version number again, followed by a count
746 * of how many points, and any special flags which may apply. 0x4000 means it's
747 * a path of shorts instead of FLOAT.
749 * Combining Ops are stored in reverse order from when they were constructed;
750 * the output is a tree where the left side combining area is always taken
753 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
756 struct region_data_header
*region_data_header
;
759 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
761 if (!region
|| !buffer
|| !size
)
762 return InvalidParameter
;
764 required
= FIELD_OFFSET(struct region_data_header
, header
) + write_region_data(region
, NULL
);
767 if (needed
) *needed
= size
;
768 return InsufficientBuffer
;
771 region_data_header
= (struct region_data_header
*)buffer
;
772 region_data_header
->size
= write_region_data(region
, ®ion_data_header
->header
);
773 region_data_header
->checksum
= 0;
781 static inline void init_memory_buffer(struct memory_buffer
*mbuf
, const BYTE
*buffer
, INT size
)
783 mbuf
->buffer
= buffer
;
788 static inline const void *buffer_read(struct memory_buffer
*mbuf
, INT size
)
790 if (mbuf
->size
- mbuf
->pos
>= size
)
792 const void *data
= mbuf
->buffer
+ mbuf
->pos
;
799 static GpStatus
read_element(struct memory_buffer
*mbuf
, GpRegion
*region
, region_element
*node
, INT
*count
)
804 type
= buffer_read(mbuf
, sizeof(*type
));
805 if (!type
) return Ok
;
807 TRACE("type %#x\n", *type
);
813 case CombineModeReplace
:
814 case CombineModeIntersect
:
815 case CombineModeUnion
:
817 case CombineModeExclude
:
818 case CombineModeComplement
:
820 region_element
*left
, *right
;
822 left
= heap_alloc_zero(sizeof(region_element
));
823 if (!left
) return OutOfMemory
;
824 right
= heap_alloc_zero(sizeof(region_element
));
831 status
= read_element(mbuf
, region
, left
, count
);
834 status
= read_element(mbuf
, region
, right
, count
);
837 node
->elementdata
.combine
.left
= left
;
838 node
->elementdata
.combine
.right
= right
;
839 region
->num_children
+= 2;
853 rc
= buffer_read(mbuf
, sizeof(*rc
));
856 ERR("failed to read rect data\n");
857 return InvalidParameter
;
860 node
->elementdata
.rect
= *rc
;
868 const struct path_header
*path_header
;
871 path_header
= buffer_read(mbuf
, sizeof(*path_header
));
874 ERR("failed to read path header\n");
875 return InvalidParameter
;
877 if (!VALID_MAGIC(path_header
->magic
))
879 ERR("invalid path header magic %#x\n", path_header
->magic
);
880 return InvalidParameter
;
883 /* Windows always fails to create an empty path in a region */
884 if (!path_header
->count
)
886 TRACE("refusing to create an empty path in a region\n");
890 status
= GdipCreatePath(FillModeAlternate
, &path
);
891 if (status
) return status
;
893 node
->elementdata
.path
= path
;
895 if (!lengthen_path(path
, path_header
->count
))
898 path
->pathdata
.Count
= path_header
->count
;
900 if (path_header
->flags
& ~FLAGS_INTPATH
)
901 FIXME("unhandled path flags %#x\n", path_header
->flags
);
903 if (path_header
->flags
& FLAGS_INTPATH
)
905 const packed_point
*pt
;
908 pt
= buffer_read(mbuf
, sizeof(*pt
) * path_header
->count
);
911 ERR("failed to read packed %u path points\n", path_header
->count
);
912 return InvalidParameter
;
915 for (i
= 0; i
< path_header
->count
; i
++)
917 path
->pathdata
.Points
[i
].X
= (REAL
)pt
[i
].X
;
918 path
->pathdata
.Points
[i
].Y
= (REAL
)pt
[i
].Y
;
925 ptf
= buffer_read(mbuf
, sizeof(*ptf
) * path_header
->count
);
928 ERR("failed to read %u path points\n", path_header
->count
);
929 return InvalidParameter
;
931 memcpy(path
->pathdata
.Points
, ptf
, sizeof(*ptf
) * path_header
->count
);
934 types
= buffer_read(mbuf
, path_header
->count
);
937 ERR("failed to read %u path types\n", path_header
->count
);
938 return InvalidParameter
;
940 memcpy(path
->pathdata
.Types
, types
, path_header
->count
);
941 if (path_header
->count
& 3)
943 if (!buffer_read(mbuf
, 4 - (path_header
->count
& 3)))
945 ERR("failed to read rounding %u bytes\n", 4 - (path_header
->count
& 3));
946 return InvalidParameter
;
954 case RegionDataEmptyRect
:
955 case RegionDataInfiniteRect
:
960 FIXME("element type %#x is not supported\n", *type
);
964 return InvalidParameter
;
967 /*****************************************************************************
968 * GdipCreateRegionRgnData [GDIPLUS.@]
970 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
972 const struct region_data_header
*region_data_header
;
973 struct memory_buffer mbuf
;
977 TRACE("(%p, %d, %p)\n", data
, size
, region
);
980 return InvalidParameter
;
982 init_memory_buffer(&mbuf
, data
, size
);
984 region_data_header
= buffer_read(&mbuf
, sizeof(*region_data_header
));
985 if (!region_data_header
|| !VALID_MAGIC(region_data_header
->header
.magic
))
986 return InvalidParameter
;
988 status
= GdipCreateRegion(region
);
993 status
= read_element(&mbuf
, *region
, &(*region
)->node
, &count
);
994 if (status
== Ok
&& !count
)
995 status
= InvalidParameter
;
999 GdipDeleteRegion(*region
);
1006 /*****************************************************************************
1007 * GdipGetRegionDataSize [GDIPLUS.@]
1009 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
1011 TRACE("%p, %p\n", region
, needed
);
1013 if (!(region
&& needed
))
1014 return InvalidParameter
;
1016 /* header.size doesn't count header.size and header.checksum */
1017 *needed
= FIELD_OFFSET(struct region_data_header
, header
) + write_region_data(region
, NULL
);
1022 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
1025 GpGraphics
*new_graphics
=NULL
;
1029 if (!path
->pathdata
.Count
) /* PathToRegion doesn't support empty paths */
1031 *hrgn
= CreateRectRgn( 0, 0, 0, 0 );
1032 return *hrgn
? Ok
: OutOfMemory
;
1037 new_hdc
= CreateCompatibleDC(0);
1041 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
1042 graphics
= new_graphics
;
1049 else if (!graphics
->hdc
)
1051 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
1056 save_state
= SaveDC(graphics
->hdc
);
1057 EndPath(graphics
->hdc
);
1059 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1062 gdi_transform_acquire(graphics
);
1064 stat
= trace_path(graphics
, path
);
1067 *hrgn
= PathToRegion(graphics
->hdc
);
1068 stat
= *hrgn
? Ok
: OutOfMemory
;
1071 gdi_transform_release(graphics
);
1073 RestoreDC(graphics
->hdc
, save_state
);
1078 GdipDeleteGraphics(new_graphics
);
1080 graphics
->hdc
= NULL
;
1086 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
1088 switch (element
->type
)
1090 case RegionDataInfiniteRect
:
1093 case RegionDataEmptyRect
:
1094 *hrgn
= CreateRectRgn(0, 0, 0, 0);
1095 return *hrgn
? Ok
: OutOfMemory
;
1096 case RegionDataPath
:
1097 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
1098 case RegionDataRect
:
1102 GpRectF
* rc
= &element
->elementdata
.rect
;
1104 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1107 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
1110 stat
= get_path_hrgn(path
, graphics
, hrgn
);
1112 GdipDeletePath(path
);
1116 case CombineModeIntersect
:
1117 case CombineModeUnion
:
1118 case CombineModeXor
:
1119 case CombineModeExclude
:
1120 case CombineModeComplement
:
1126 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
1135 /* existing region is infinite */
1136 switch (element
->type
)
1138 case CombineModeIntersect
:
1139 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
1140 case CombineModeXor
: case CombineModeExclude
:
1141 left
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1143 case CombineModeUnion
: case CombineModeComplement
:
1149 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1159 /* new region is infinite */
1160 switch (element
->type
)
1162 case CombineModeIntersect
:
1165 case CombineModeXor
: case CombineModeComplement
:
1166 right
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1168 case CombineModeUnion
: case CombineModeExclude
:
1175 switch (element
->type
)
1177 case CombineModeIntersect
:
1178 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1180 case CombineModeUnion
:
1181 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1183 case CombineModeXor
:
1184 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1186 case CombineModeExclude
:
1187 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1189 case CombineModeComplement
:
1190 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1196 DeleteObject(right
);
1202 return GenericError
;
1209 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1211 return NotImplemented
;
1215 /*****************************************************************************
1216 * GdipGetRegionHRgn [GDIPLUS.@]
1218 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1220 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1222 if (!region
|| !hrgn
)
1223 return InvalidParameter
;
1225 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1228 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1233 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1235 if(!region
|| !graphics
|| !res
)
1236 return InvalidParameter
;
1238 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1239 if (status
!= Ok
) return status
;
1241 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1242 TRACE("=> %d\n", *res
);
1247 /*****************************************************************************
1248 * GdipIsEqualRegion [GDIPLUS.@]
1250 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1256 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1258 if(!region
|| !region2
|| !graphics
|| !res
)
1259 return InvalidParameter
;
1261 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1264 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1266 DeleteObject(hrgn1
);
1270 *res
= EqualRgn(hrgn1
, hrgn2
);
1272 /* one of GpRegions is infinite */
1274 *res
= (!hrgn1
&& !hrgn2
);
1276 DeleteObject(hrgn1
);
1277 DeleteObject(hrgn2
);
1282 /*****************************************************************************
1283 * GdipIsInfiniteRegion [GDIPLUS.@]
1285 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1287 /* I think graphics is ignored here */
1288 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1290 if(!region
|| !graphics
|| !res
)
1291 return InvalidParameter
;
1293 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1298 /*****************************************************************************
1299 * GdipIsVisibleRegionRect [GDIPLUS.@]
1301 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1307 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1310 return InvalidParameter
;
1312 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1321 SetRect(&rect
, ceilr(x
), ceilr(y
), ceilr(x
+ w
), ceilr(y
+ h
));
1322 *res
= RectInRegion(hrgn
, &rect
);
1329 /*****************************************************************************
1330 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1332 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1334 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1336 return InvalidParameter
;
1338 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1341 /*****************************************************************************
1342 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1344 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1349 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1352 return InvalidParameter
;
1354 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1363 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1370 /*****************************************************************************
1371 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1373 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1375 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1377 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1380 /*****************************************************************************
1381 * GdipSetEmpty [GDIPLUS.@]
1383 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1387 TRACE("%p\n", region
);
1390 return InvalidParameter
;
1392 delete_element(®ion
->node
);
1393 stat
= init_region(region
, RegionDataEmptyRect
);
1398 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1402 TRACE("%p\n", region
);
1405 return InvalidParameter
;
1407 delete_element(®ion
->node
);
1408 stat
= init_region(region
, RegionDataInfiniteRect
);
1413 /* Transforms GpRegion elements with given matrix */
1414 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1418 switch(element
->type
)
1420 case RegionDataEmptyRect
:
1421 case RegionDataInfiniteRect
:
1423 case RegionDataRect
:
1425 /* We can't transform a rectangle, so convert it to a path. */
1426 GpRegion
*new_region
;
1429 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1432 stat
= GdipAddPathRectangle(path
,
1433 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1434 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1437 stat
= GdipCreateRegionPath(path
, &new_region
);
1439 GdipDeletePath(path
);
1444 /* Steal the element from the created region. */
1445 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1446 heap_free(new_region
);
1451 /* Fall-through to do the actual conversion. */
1452 case RegionDataPath
:
1453 if (!element
->elementdata
.path
->pathdata
.Count
)
1456 stat
= GdipTransformMatrixPoints(matrix
,
1457 element
->elementdata
.path
->pathdata
.Points
,
1458 element
->elementdata
.path
->pathdata
.Count
);
1461 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1463 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1468 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1470 TRACE("(%p, %p)\n", region
, matrix
);
1472 if (!region
|| !matrix
)
1473 return InvalidParameter
;
1475 return transform_region_element(®ion
->node
, matrix
);
1478 /* Translates GpRegion elements with specified offsets */
1479 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1483 switch(element
->type
)
1485 case RegionDataEmptyRect
:
1486 case RegionDataInfiniteRect
:
1488 case RegionDataRect
:
1489 element
->elementdata
.rect
.X
+= dx
;
1490 element
->elementdata
.rect
.Y
+= dy
;
1492 case RegionDataPath
:
1493 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1494 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1495 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1499 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1500 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1505 /*****************************************************************************
1506 * GdipTranslateRegion [GDIPLUS.@]
1508 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1510 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1513 return InvalidParameter
;
1515 translate_region_element(®ion
->node
, dx
, dy
);
1520 /*****************************************************************************
1521 * GdipTranslateRegionI [GDIPLUS.@]
1523 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1525 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1527 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1530 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1532 GpRegion
*region_copy
;
1537 stat
= GdipCloneRegion(region
, ®ion_copy
);
1541 stat
= GdipTransformRegion(region_copy
, matrix
);
1544 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1550 data_size
= GetRegionData(hrgn
, 0, NULL
);
1552 *data
= heap_alloc_zero(data_size
);
1555 GetRegionData(hrgn
, data_size
, *data
);
1563 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1565 *data
= heap_alloc_zero(data_size
);
1569 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1570 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1571 (*data
)->rdh
.nCount
= 1;
1572 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1573 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1574 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1576 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1583 GdipDeleteRegion(region_copy
);
1589 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1594 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1596 if (!region
|| !count
|| !matrix
)
1597 return InvalidParameter
;
1599 stat
= get_region_scans_data(region
, matrix
, &data
);
1603 *count
= data
->rdh
.nCount
;
1610 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1617 if (!region
|| !count
|| !matrix
)
1618 return InvalidParameter
;
1620 stat
= get_region_scans_data(region
, matrix
, &data
);
1624 *count
= data
->rdh
.nCount
;
1625 rects
= (RECT
*)data
->Buffer
;
1629 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1631 scans
[i
].X
= rects
[i
].left
;
1632 scans
[i
].Y
= rects
[i
].top
;
1633 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1634 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1644 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1651 if (!region
|| !count
|| !matrix
)
1652 return InvalidParameter
;
1654 stat
= get_region_scans_data(region
, matrix
, &data
);
1658 *count
= data
->rdh
.nCount
;
1659 rects
= (RECT
*)data
->Buffer
;
1663 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1665 scans
[i
].X
= rects
[i
].left
;
1666 scans
[i
].Y
= rects
[i
].top
;
1667 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1668 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;