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_NOFLAGS 0x0
78 #define FLAGS_INTPATH 0x4000
102 /* Header size as far as header->size is concerned. This doesn't include
103 * header->size or header->checksum
105 static const INT sizeheader_size
= sizeof(DWORD
) * 2;
107 typedef struct packed_point
113 /* Test to see if the path could be stored as an array of shorts */
114 static BOOL
is_integer_path(const GpPath
*path
)
118 if (!path
->pathdata
.Count
) return FALSE
;
120 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
123 x
= gdip_round(path
->pathdata
.Points
[i
].X
);
124 y
= gdip_round(path
->pathdata
.Points
[i
].Y
);
125 if (path
->pathdata
.Points
[i
].X
!= (REAL
)x
|| path
->pathdata
.Points
[i
].Y
!= (REAL
)y
)
131 /* Everything is measured in DWORDS; round up if there's a remainder */
132 static inline INT
get_pathtypes_size(const GpPath
* path
)
134 INT needed
= path
->pathdata
.Count
/ sizeof(DWORD
);
136 if (path
->pathdata
.Count
% sizeof(DWORD
) > 0)
139 return needed
* sizeof(DWORD
);
142 static inline INT
get_element_size(const region_element
* element
)
144 INT needed
= sizeof(DWORD
); /* DWORD for the type */
145 switch(element
->type
)
148 return needed
+ sizeof(GpRect
);
151 const GpPath
*path
= element
->elementdata
.path
;
152 DWORD flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
153 /* 3 for headers, once again size doesn't count itself */
154 needed
+= sizeof(DWORD
) * 3;
155 if (flags
& FLAGS_INTPATH
)
156 needed
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
158 needed
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
160 needed
+= get_pathtypes_size(path
);
161 needed
+= sizeof(DWORD
); /* Extra DWORD for pathheader.size */
164 case RegionDataEmptyRect
:
165 case RegionDataInfiniteRect
:
168 needed
+= get_element_size(element
->elementdata
.combine
.left
);
169 needed
+= get_element_size(element
->elementdata
.combine
.right
);
176 /* Does not check parameters, caller must do that */
177 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
179 region
->node
.type
= type
;
180 region
->num_children
= 0;
185 static inline GpStatus
clone_element(const region_element
* element
,
186 region_element
** element2
)
190 /* root node is allocated with GpRegion */
192 *element2
= heap_alloc_zero(sizeof(region_element
));
197 (*element2
)->type
= element
->type
;
199 switch (element
->type
)
202 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
204 case RegionDataEmptyRect
:
205 case RegionDataInfiniteRect
:
208 stat
= GdipClonePath(element
->elementdata
.path
, &(*element2
)->elementdata
.path
);
209 if (stat
== Ok
) return Ok
;
212 (*element2
)->elementdata
.combine
.left
= NULL
;
213 (*element2
)->elementdata
.combine
.right
= NULL
;
215 stat
= clone_element(element
->elementdata
.combine
.left
,
216 &(*element2
)->elementdata
.combine
.left
);
219 stat
= clone_element(element
->elementdata
.combine
.right
,
220 &(*element2
)->elementdata
.combine
.right
);
221 if (stat
== Ok
) return Ok
;
226 delete_element(*element2
);
231 /* Common code for CombineRegion*
232 * All the caller has to do is get its format into an element
234 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
235 region_element
* right
, const CombineMode mode
)
237 region
->node
.type
= mode
;
238 region
->node
.elementdata
.combine
.left
= left
;
239 region
->node
.elementdata
.combine
.right
= right
;
240 region
->num_children
+= 2;
243 /*****************************************************************************
244 * GdipCloneRegion [GDIPLUS.@]
246 * Creates a deep copy of the region
249 * region [I] source region
250 * clone [O] resulting clone
254 * FAILURE: InvalidParameter or OutOfMemory
256 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
258 region_element
*element
;
260 TRACE("%p %p\n", region
, clone
);
262 if (!(region
&& clone
))
263 return InvalidParameter
;
265 *clone
= heap_alloc_zero(sizeof(GpRegion
));
268 element
= &(*clone
)->node
;
270 (*clone
)->num_children
= region
->num_children
;
271 return clone_element(®ion
->node
, &element
);
274 /*****************************************************************************
275 * GdipCombineRegionPath [GDIPLUS.@]
277 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
279 GpRegion
*path_region
;
280 region_element
*left
, *right
= NULL
;
283 TRACE("%p %p %d\n", region
, path
, mode
);
285 if (!(region
&& path
))
286 return InvalidParameter
;
288 stat
= GdipCreateRegionPath(path
, &path_region
);
292 /* simply replace region data */
293 if(mode
== CombineModeReplace
){
294 delete_element(®ion
->node
);
295 memcpy(region
, path_region
, sizeof(GpRegion
));
296 heap_free(path_region
);
300 left
= heap_alloc_zero(sizeof(region_element
));
303 *left
= region
->node
;
304 stat
= clone_element(&path_region
->node
, &right
);
307 fuse_region(region
, left
, right
, mode
);
308 GdipDeleteRegion(path_region
);
316 GdipDeleteRegion(path_region
);
320 /*****************************************************************************
321 * GdipCombineRegionRect [GDIPLUS.@]
323 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
324 GDIPCONST GpRectF
*rect
, CombineMode mode
)
326 GpRegion
*rect_region
;
327 region_element
*left
, *right
= NULL
;
330 TRACE("%p %s %d\n", region
, debugstr_rectf(rect
), mode
);
332 if (!(region
&& rect
))
333 return InvalidParameter
;
335 stat
= GdipCreateRegionRect(rect
, &rect_region
);
339 /* simply replace region data */
340 if(mode
== CombineModeReplace
){
341 delete_element(®ion
->node
);
342 memcpy(region
, rect_region
, sizeof(GpRegion
));
343 heap_free(rect_region
);
347 left
= heap_alloc_zero(sizeof(region_element
));
350 memcpy(left
, ®ion
->node
, sizeof(region_element
));
351 stat
= clone_element(&rect_region
->node
, &right
);
354 fuse_region(region
, left
, right
, mode
);
355 GdipDeleteRegion(rect_region
);
363 GdipDeleteRegion(rect_region
);
367 /*****************************************************************************
368 * GdipCombineRegionRectI [GDIPLUS.@]
370 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
371 GDIPCONST GpRect
*rect
, CombineMode mode
)
375 TRACE("%p %p %d\n", region
, rect
, mode
);
378 return InvalidParameter
;
380 rectf
.X
= (REAL
)rect
->X
;
381 rectf
.Y
= (REAL
)rect
->Y
;
382 rectf
.Height
= (REAL
)rect
->Height
;
383 rectf
.Width
= (REAL
)rect
->Width
;
385 return GdipCombineRegionRect(region
, &rectf
, mode
);
388 /*****************************************************************************
389 * GdipCombineRegionRegion [GDIPLUS.@]
391 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
392 GpRegion
*region2
, CombineMode mode
)
394 region_element
*left
, *right
= NULL
;
398 TRACE("%p %p %d\n", region1
, region2
, mode
);
400 if(!(region1
&& region2
))
401 return InvalidParameter
;
403 /* simply replace region data */
404 if(mode
== CombineModeReplace
){
405 stat
= GdipCloneRegion(region2
, ®2copy
);
406 if(stat
!= Ok
) return stat
;
408 delete_element(®ion1
->node
);
409 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
414 left
= heap_alloc_zero(sizeof(region_element
));
418 *left
= region1
->node
;
419 stat
= clone_element(®ion2
->node
, &right
);
426 fuse_region(region1
, left
, right
, mode
);
427 region1
->num_children
+= region2
->num_children
;
432 /*****************************************************************************
433 * GdipCreateRegion [GDIPLUS.@]
435 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
437 TRACE("%p\n", region
);
440 return InvalidParameter
;
442 *region
= heap_alloc_zero(sizeof(GpRegion
));
446 TRACE("=> %p\n", *region
);
448 return init_region(*region
, RegionDataInfiniteRect
);
451 /*****************************************************************************
452 * GdipCreateRegionPath [GDIPLUS.@]
454 * Creates a GpRegion from a GpPath
457 * path [I] path to base the region on
458 * region [O] pointer to the newly allocated region
462 * FAILURE: InvalidParameter
465 * If a path has no floating point points, its points will be stored as shorts
468 * If a path is empty, it is considered to be an INTPATH
470 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
472 region_element
* element
;
475 TRACE("%p, %p\n", path
, region
);
477 if (!(path
&& region
))
478 return InvalidParameter
;
480 *region
= heap_alloc_zero(sizeof(GpRegion
));
483 stat
= init_region(*region
, RegionDataPath
);
486 GdipDeleteRegion(*region
);
489 element
= &(*region
)->node
;
491 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
494 GdipDeleteRegion(*region
);
501 /*****************************************************************************
502 * GdipCreateRegionRect [GDIPLUS.@]
504 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
509 TRACE("%p, %p\n", rect
, region
);
511 if (!(rect
&& region
))
512 return InvalidParameter
;
514 *region
= heap_alloc_zero(sizeof(GpRegion
));
515 stat
= init_region(*region
, RegionDataRect
);
518 GdipDeleteRegion(*region
);
522 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
523 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
524 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
525 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
530 /*****************************************************************************
531 * GdipCreateRegionRectI [GDIPLUS.@]
533 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
538 TRACE("%p, %p\n", rect
, region
);
540 rectf
.X
= (REAL
)rect
->X
;
541 rectf
.Y
= (REAL
)rect
->Y
;
542 rectf
.Width
= (REAL
)rect
->Width
;
543 rectf
.Height
= (REAL
)rect
->Height
;
545 return GdipCreateRegionRect(&rectf
, region
);
548 /******************************************************************************
549 * GdipCreateRegionHrgn [GDIPLUS.@]
551 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
561 TRACE("(%p, %p)\n", hrgn
, region
);
563 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
564 return InvalidParameter
;
566 buf
= heap_alloc_zero(size
);
570 if(!GetRegionData(hrgn
, size
, buf
)){
575 if(buf
->rdh
.nCount
== 0){
576 if((stat
= GdipCreateRegion(&local
)) != Ok
){
580 if((stat
= GdipSetEmpty(local
)) != Ok
){
582 GdipDeleteRegion(local
);
590 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
595 rect
= (LPRECT
)buf
->Buffer
;
596 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
597 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
598 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
600 GdipDeletePath(path
);
606 stat
= GdipCreateRegionPath(path
, region
);
609 GdipDeletePath(path
);
613 /*****************************************************************************
614 * GdipDeleteRegion [GDIPLUS.@]
616 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
618 TRACE("%p\n", region
);
621 return InvalidParameter
;
623 delete_element(®ion
->node
);
629 /*****************************************************************************
630 * GdipGetRegionBounds [GDIPLUS.@]
632 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
638 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
640 if(!region
|| !graphics
|| !rect
)
641 return InvalidParameter
;
643 /* Contrary to MSDN, native ignores the graphics transform. */
644 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
650 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
651 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
652 TRACE("%p => infinite\n", region
);
656 if(GetRgnBox(hrgn
, &r
)){
659 rect
->Width
= r
.right
- r
.left
;
660 rect
->Height
= r
.bottom
- r
.top
;
661 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
664 status
= GenericError
;
671 /*****************************************************************************
672 * GdipGetRegionBoundsI [GDIPLUS.@]
674 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
679 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
682 return InvalidParameter
;
684 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
686 rect
->X
= gdip_round(rectf
.X
);
687 rect
->Y
= gdip_round(rectf
.Y
);
688 rect
->Width
= gdip_round(rectf
.Width
);
689 rect
->Height
= gdip_round(rectf
.Height
);
695 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
697 location
[*offset
] = write
;
701 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
703 ((FLOAT
*)location
)[*offset
] = write
;
707 static inline void write_packed_point(DWORD
* location
, INT
* offset
,
708 const GpPointF
* write
)
710 packed_point
*point
= (packed_point
*)(location
+ *offset
);
711 point
->X
= gdip_round(write
->X
);
712 point
->Y
= gdip_round(write
->Y
);
716 static inline void write_path_types(DWORD
* location
, INT
* offset
,
719 INT rounded_size
= get_pathtypes_size(path
);
721 memcpy(location
+ *offset
, path
->pathdata
.Types
, path
->pathdata
.Count
);
723 /* The unwritten parts of the DWORD (if any) must be cleared */
724 if (rounded_size
- path
->pathdata
.Count
)
725 ZeroMemory(((BYTE
*)location
) + (*offset
* sizeof(DWORD
)) +
726 path
->pathdata
.Count
, rounded_size
- path
->pathdata
.Count
);
727 *offset
+= rounded_size
/ sizeof(DWORD
);
730 static void write_element(const region_element
* element
, DWORD
*buffer
,
733 write_dword(buffer
, filled
, element
->type
);
734 switch (element
->type
)
736 case CombineModeReplace
:
737 case CombineModeIntersect
:
738 case CombineModeUnion
:
740 case CombineModeExclude
:
741 case CombineModeComplement
:
742 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
743 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
746 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
747 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
748 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
749 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
754 const GpPath
* path
= element
->elementdata
.path
;
755 struct path_header
*pathheader
;
757 pathheader
= (struct path_header
*)(buffer
+ *filled
);
759 pathheader
->flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
760 /* 3 for headers, once again size doesn't count itself */
761 pathheader
->size
= sizeof(DWORD
) * 3;
762 if (pathheader
->flags
& FLAGS_INTPATH
)
763 pathheader
->size
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
765 pathheader
->size
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
766 pathheader
->size
+= get_pathtypes_size(path
);
767 pathheader
->magic
= VERSION_MAGIC
;
768 pathheader
->count
= path
->pathdata
.Count
;
772 switch (pathheader
->flags
& FLAGS_INTPATH
)
775 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
777 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].X
);
778 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].Y
);
782 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
784 write_packed_point(buffer
, filled
,
785 &path
->pathdata
.Points
[i
]);
789 write_path_types(buffer
, filled
, path
);
792 case RegionDataEmptyRect
:
793 case RegionDataInfiniteRect
:
798 /*****************************************************************************
799 * GdipGetRegionData [GDIPLUS.@]
801 * Returns the header, followed by combining ops and region elements.
804 * region [I] region to retrieve from
805 * buffer [O] buffer to hold the resulting data
806 * size [I] size of the buffer
807 * needed [O] (optional) how much data was written
811 * FAILURE: InvalidParameter
814 * The header contains the size, a checksum, a version string, and the number
815 * of children. The size does not count itself or the checksum.
816 * Version is always something like 0xdbc01001 or 0xdbc01002
818 * An element is a RECT, or PATH; Combining ops are stored as their
819 * CombineMode value. Special regions (infinite, empty) emit just their
820 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
821 * their code followed by a second header for the path followed by the actual
822 * path data. Followed by the flags for each point. The pathheader contains
823 * the size of the data to follow, a version number again, followed by a count
824 * of how many points, and any special flags which may apply. 0x4000 means its
825 * a path of shorts instead of FLOAT.
827 * Combining Ops are stored in reverse order from when they were constructed;
828 * the output is a tree where the left side combining area is always taken
831 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
834 struct region_header
*region_header
;
839 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
841 if (!region
|| !buffer
|| !size
)
842 return InvalidParameter
;
844 status
= GdipGetRegionDataSize(region
, &required
);
845 if (status
!= Ok
) return status
;
848 if (needed
) *needed
= size
;
849 return InsufficientBuffer
;
852 region_header
= (struct region_header
*)buffer
;
853 region_header
->size
= sizeheader_size
+ get_element_size(®ion
->node
);
854 region_header
->checksum
= 0;
855 region_header
->magic
= VERSION_MAGIC
;
856 region_header
->num_children
= region
->num_children
;
858 /* With few exceptions, everything written is DWORD aligned,
859 * so use that as our base */
860 write_element(®ion
->node
, (DWORD
*)buffer
, &filled
);
863 *needed
= filled
* sizeof(DWORD
);
868 static inline void init_memory_buffer(struct memory_buffer
*mbuf
, const BYTE
*buffer
, INT size
)
870 mbuf
->buffer
= buffer
;
875 static inline const void *buffer_read(struct memory_buffer
*mbuf
, INT size
)
877 if (mbuf
->size
- mbuf
->pos
>= size
)
879 const void *data
= mbuf
->buffer
+ mbuf
->pos
;
886 static GpStatus
read_element(struct memory_buffer
*mbuf
, GpRegion
*region
, region_element
*node
, INT
*count
)
891 type
= buffer_read(mbuf
, sizeof(*type
));
892 if (!type
) return Ok
;
894 TRACE("type %#x\n", *type
);
900 case CombineModeReplace
:
901 case CombineModeIntersect
:
902 case CombineModeUnion
:
904 case CombineModeExclude
:
905 case CombineModeComplement
:
907 region_element
*left
, *right
;
909 left
= heap_alloc_zero(sizeof(region_element
));
910 if (!left
) return OutOfMemory
;
911 right
= heap_alloc_zero(sizeof(region_element
));
918 status
= read_element(mbuf
, region
, left
, count
);
921 status
= read_element(mbuf
, region
, right
, count
);
924 node
->elementdata
.combine
.left
= left
;
925 node
->elementdata
.combine
.right
= right
;
926 region
->num_children
+= 2;
940 rc
= buffer_read(mbuf
, sizeof(*rc
));
943 ERR("failed to read rect data\n");
944 return InvalidParameter
;
947 node
->elementdata
.rect
= *rc
;
955 const struct path_header
*path_header
;
958 path_header
= buffer_read(mbuf
, sizeof(*path_header
));
961 ERR("failed to read path header\n");
962 return InvalidParameter
;
964 if (path_header
->magic
!= VERSION_MAGIC
)
966 ERR("invalid path header magic %#x\n", path_header
->magic
);
967 return InvalidParameter
;
970 /* Windows always fails to create an empty path in a region */
971 if (!path_header
->count
)
973 TRACE("refusing to create an empty path in a region\n");
977 status
= GdipCreatePath(FillModeAlternate
, &path
);
978 if (status
) return status
;
980 node
->elementdata
.path
= path
;
982 if (!lengthen_path(path
, path_header
->count
))
985 path
->pathdata
.Count
= path_header
->count
;
987 if (path_header
->flags
& ~FLAGS_INTPATH
)
988 FIXME("unhandled path flags %#x\n", path_header
->flags
);
990 if (path_header
->flags
& FLAGS_INTPATH
)
992 const packed_point
*pt
;
995 pt
= buffer_read(mbuf
, sizeof(*pt
) * path_header
->count
);
998 ERR("failed to read packed %u path points\n", path_header
->count
);
999 return InvalidParameter
;
1002 for (i
= 0; i
< path_header
->count
; i
++)
1004 path
->pathdata
.Points
[i
].X
= (REAL
)pt
[i
].X
;
1005 path
->pathdata
.Points
[i
].Y
= (REAL
)pt
[i
].Y
;
1010 const GpPointF
*ptf
;
1012 ptf
= buffer_read(mbuf
, sizeof(*ptf
) * path_header
->count
);
1015 ERR("failed to read %u path points\n", path_header
->count
);
1016 return InvalidParameter
;
1018 memcpy(path
->pathdata
.Points
, ptf
, sizeof(*ptf
) * path_header
->count
);
1021 types
= buffer_read(mbuf
, path_header
->count
);
1024 ERR("failed to read %u path types\n", path_header
->count
);
1025 return InvalidParameter
;
1027 memcpy(path
->pathdata
.Types
, types
, path_header
->count
);
1028 if (path_header
->count
& 3)
1030 if (!buffer_read(mbuf
, 4 - (path_header
->count
& 3)))
1032 ERR("failed to read rounding %u bytes\n", 4 - (path_header
->count
& 3));
1033 return InvalidParameter
;
1041 case RegionDataEmptyRect
:
1042 case RegionDataInfiniteRect
:
1047 FIXME("element type %#x is not supported\n", *type
);
1051 return InvalidParameter
;
1054 /*****************************************************************************
1055 * GdipCreateRegionRgnData [GDIPLUS.@]
1057 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
1059 const struct region_header
*region_header
;
1060 struct memory_buffer mbuf
;
1064 TRACE("(%p, %d, %p)\n", data
, size
, region
);
1067 return InvalidParameter
;
1069 init_memory_buffer(&mbuf
, data
, size
);
1071 region_header
= buffer_read(&mbuf
, sizeof(*region_header
));
1072 if (!region_header
|| (region_header
->magic
!= VERSION_MAGIC
&&
1073 region_header
->magic
!= VERSION_MAGIC2
))
1074 return InvalidParameter
;
1076 status
= GdipCreateRegion(region
);
1081 status
= read_element(&mbuf
, *region
, &(*region
)->node
, &count
);
1082 if (status
== Ok
&& !count
)
1083 status
= InvalidParameter
;
1087 GdipDeleteRegion(*region
);
1094 /*****************************************************************************
1095 * GdipGetRegionDataSize [GDIPLUS.@]
1097 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
1099 TRACE("%p, %p\n", region
, needed
);
1101 if (!(region
&& needed
))
1102 return InvalidParameter
;
1104 /* header.size doesn't count header.size and header.checksum */
1105 *needed
= sizeof(DWORD
) * 2 + sizeheader_size
+ get_element_size(®ion
->node
);
1110 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
1113 GpGraphics
*new_graphics
=NULL
;
1119 new_hdc
= CreateCompatibleDC(0);
1123 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
1124 graphics
= new_graphics
;
1131 else if (!graphics
->hdc
)
1133 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
1138 save_state
= SaveDC(graphics
->hdc
);
1139 EndPath(graphics
->hdc
);
1141 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1144 stat
= trace_path(graphics
, path
);
1147 *hrgn
= PathToRegion(graphics
->hdc
);
1148 stat
= *hrgn
? Ok
: OutOfMemory
;
1151 RestoreDC(graphics
->hdc
, save_state
);
1156 GdipDeleteGraphics(new_graphics
);
1158 graphics
->hdc
= NULL
;
1164 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
1166 switch (element
->type
)
1168 case RegionDataInfiniteRect
:
1171 case RegionDataEmptyRect
:
1172 *hrgn
= CreateRectRgn(0, 0, 0, 0);
1173 return *hrgn
? Ok
: OutOfMemory
;
1174 case RegionDataPath
:
1175 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
1176 case RegionDataRect
:
1180 GpRectF
* rc
= &element
->elementdata
.rect
;
1182 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1185 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
1188 stat
= get_path_hrgn(path
, graphics
, hrgn
);
1190 GdipDeletePath(path
);
1194 case CombineModeIntersect
:
1195 case CombineModeUnion
:
1196 case CombineModeXor
:
1197 case CombineModeExclude
:
1198 case CombineModeComplement
:
1204 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
1213 /* existing region is infinite */
1214 switch (element
->type
)
1216 case CombineModeIntersect
:
1217 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
1218 case CombineModeXor
: case CombineModeExclude
:
1219 left
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1221 case CombineModeUnion
: case CombineModeComplement
:
1227 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1237 /* new region is infinite */
1238 switch (element
->type
)
1240 case CombineModeIntersect
:
1243 case CombineModeXor
: case CombineModeComplement
:
1244 right
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1246 case CombineModeUnion
: case CombineModeExclude
:
1253 switch (element
->type
)
1255 case CombineModeIntersect
:
1256 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1258 case CombineModeUnion
:
1259 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1261 case CombineModeXor
:
1262 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1264 case CombineModeExclude
:
1265 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1267 case CombineModeComplement
:
1268 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1274 DeleteObject(right
);
1280 return GenericError
;
1287 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1289 return NotImplemented
;
1293 /*****************************************************************************
1294 * GdipGetRegionHRgn [GDIPLUS.@]
1296 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1298 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1300 if (!region
|| !hrgn
)
1301 return InvalidParameter
;
1303 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1306 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1311 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1313 if(!region
|| !graphics
|| !res
)
1314 return InvalidParameter
;
1316 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1317 if (status
!= Ok
) return status
;
1319 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1320 TRACE("=> %d\n", *res
);
1325 /*****************************************************************************
1326 * GdipIsEqualRegion [GDIPLUS.@]
1328 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1334 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1336 if(!region
|| !region2
|| !graphics
|| !res
)
1337 return InvalidParameter
;
1339 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1342 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1344 DeleteObject(hrgn1
);
1348 *res
= EqualRgn(hrgn1
, hrgn2
);
1350 /* one of GpRegions is infinite */
1352 *res
= (!hrgn1
&& !hrgn2
);
1354 DeleteObject(hrgn1
);
1355 DeleteObject(hrgn2
);
1360 /*****************************************************************************
1361 * GdipIsInfiniteRegion [GDIPLUS.@]
1363 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1365 /* I think graphics is ignored here */
1366 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1368 if(!region
|| !graphics
|| !res
)
1369 return InvalidParameter
;
1371 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1376 /*****************************************************************************
1377 * GdipIsVisibleRegionRect [GDIPLUS.@]
1379 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1385 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1388 return InvalidParameter
;
1390 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1399 rect
.left
= ceilr(x
);
1400 rect
.top
= ceilr(y
);
1401 rect
.right
= ceilr(x
+ w
);
1402 rect
.bottom
= ceilr(y
+ h
);
1404 *res
= RectInRegion(hrgn
, &rect
);
1411 /*****************************************************************************
1412 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1414 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1416 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1418 return InvalidParameter
;
1420 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1423 /*****************************************************************************
1424 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1426 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1431 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1434 return InvalidParameter
;
1436 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1445 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1452 /*****************************************************************************
1453 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1455 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1457 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1459 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1462 /*****************************************************************************
1463 * GdipSetEmpty [GDIPLUS.@]
1465 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1469 TRACE("%p\n", region
);
1472 return InvalidParameter
;
1474 delete_element(®ion
->node
);
1475 stat
= init_region(region
, RegionDataEmptyRect
);
1480 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1484 TRACE("%p\n", region
);
1487 return InvalidParameter
;
1489 delete_element(®ion
->node
);
1490 stat
= init_region(region
, RegionDataInfiniteRect
);
1495 /* Transforms GpRegion elements with given matrix */
1496 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1500 switch(element
->type
)
1502 case RegionDataEmptyRect
:
1503 case RegionDataInfiniteRect
:
1505 case RegionDataRect
:
1507 /* We can't transform a rectangle, so convert it to a path. */
1508 GpRegion
*new_region
;
1511 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1514 stat
= GdipAddPathRectangle(path
,
1515 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1516 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1519 stat
= GdipCreateRegionPath(path
, &new_region
);
1521 GdipDeletePath(path
);
1526 /* Steal the element from the created region. */
1527 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1528 heap_free(new_region
);
1533 /* Fall-through to do the actual conversion. */
1534 case RegionDataPath
:
1535 if (!element
->elementdata
.path
->pathdata
.Count
)
1538 stat
= GdipTransformMatrixPoints(matrix
,
1539 element
->elementdata
.path
->pathdata
.Points
,
1540 element
->elementdata
.path
->pathdata
.Count
);
1543 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1545 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1550 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1552 TRACE("(%p, %p)\n", region
, matrix
);
1554 if (!region
|| !matrix
)
1555 return InvalidParameter
;
1557 return transform_region_element(®ion
->node
, matrix
);
1560 /* Translates GpRegion elements with specified offsets */
1561 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1565 switch(element
->type
)
1567 case RegionDataEmptyRect
:
1568 case RegionDataInfiniteRect
:
1570 case RegionDataRect
:
1571 element
->elementdata
.rect
.X
+= dx
;
1572 element
->elementdata
.rect
.Y
+= dy
;
1574 case RegionDataPath
:
1575 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1576 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1577 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1581 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1582 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1587 /*****************************************************************************
1588 * GdipTranslateRegion [GDIPLUS.@]
1590 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1592 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1595 return InvalidParameter
;
1597 translate_region_element(®ion
->node
, dx
, dy
);
1602 /*****************************************************************************
1603 * GdipTranslateRegionI [GDIPLUS.@]
1605 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1607 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1609 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1612 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1614 GpRegion
*region_copy
;
1619 stat
= GdipCloneRegion(region
, ®ion_copy
);
1623 stat
= GdipTransformRegion(region_copy
, matrix
);
1626 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1632 data_size
= GetRegionData(hrgn
, 0, NULL
);
1634 *data
= heap_alloc_zero(data_size
);
1637 GetRegionData(hrgn
, data_size
, *data
);
1645 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1647 *data
= heap_alloc_zero(data_size
);
1651 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1652 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1653 (*data
)->rdh
.nCount
= 1;
1654 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1655 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1656 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1658 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1665 GdipDeleteRegion(region_copy
);
1671 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1676 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1678 if (!region
|| !count
|| !matrix
)
1679 return InvalidParameter
;
1681 stat
= get_region_scans_data(region
, matrix
, &data
);
1685 *count
= data
->rdh
.nCount
;
1692 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1699 if (!region
|| !count
|| !matrix
)
1700 return InvalidParameter
;
1702 stat
= get_region_scans_data(region
, matrix
, &data
);
1706 *count
= data
->rdh
.nCount
;
1707 rects
= (RECT
*)data
->Buffer
;
1711 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1713 scans
[i
].X
= rects
[i
].left
;
1714 scans
[i
].Y
= rects
[i
].top
;
1715 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1716 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1726 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1733 if (!region
|| !count
|| !matrix
)
1734 return InvalidParameter
;
1736 stat
= get_region_scans_data(region
, matrix
, &data
);
1740 *count
= data
->rdh
.nCount
;
1741 rects
= (RECT
*)data
->Buffer
;
1745 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1747 scans
[i
].X
= rects
[i
].left
;
1748 scans
[i
].Y
= rects
[i
].top
;
1749 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1750 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;