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
;
1117 if (!path
->pathdata
.Count
) /* PathToRegion doesn't support empty paths */
1119 *hrgn
= CreateRectRgn( 0, 0, 0, 0 );
1120 return *hrgn
? Ok
: OutOfMemory
;
1125 new_hdc
= CreateCompatibleDC(0);
1129 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
1130 graphics
= new_graphics
;
1137 else if (!graphics
->hdc
)
1139 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
1144 save_state
= SaveDC(graphics
->hdc
);
1145 EndPath(graphics
->hdc
);
1147 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1150 stat
= trace_path(graphics
, path
);
1153 *hrgn
= PathToRegion(graphics
->hdc
);
1154 stat
= *hrgn
? Ok
: OutOfMemory
;
1157 RestoreDC(graphics
->hdc
, save_state
);
1162 GdipDeleteGraphics(new_graphics
);
1164 graphics
->hdc
= NULL
;
1170 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
1172 switch (element
->type
)
1174 case RegionDataInfiniteRect
:
1177 case RegionDataEmptyRect
:
1178 *hrgn
= CreateRectRgn(0, 0, 0, 0);
1179 return *hrgn
? Ok
: OutOfMemory
;
1180 case RegionDataPath
:
1181 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
1182 case RegionDataRect
:
1186 GpRectF
* rc
= &element
->elementdata
.rect
;
1188 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1191 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
1194 stat
= get_path_hrgn(path
, graphics
, hrgn
);
1196 GdipDeletePath(path
);
1200 case CombineModeIntersect
:
1201 case CombineModeUnion
:
1202 case CombineModeXor
:
1203 case CombineModeExclude
:
1204 case CombineModeComplement
:
1210 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
1219 /* existing region is infinite */
1220 switch (element
->type
)
1222 case CombineModeIntersect
:
1223 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
1224 case CombineModeXor
: case CombineModeExclude
:
1225 left
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1227 case CombineModeUnion
: case CombineModeComplement
:
1233 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1243 /* new region is infinite */
1244 switch (element
->type
)
1246 case CombineModeIntersect
:
1249 case CombineModeXor
: case CombineModeComplement
:
1250 right
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1252 case CombineModeUnion
: case CombineModeExclude
:
1259 switch (element
->type
)
1261 case CombineModeIntersect
:
1262 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1264 case CombineModeUnion
:
1265 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1267 case CombineModeXor
:
1268 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1270 case CombineModeExclude
:
1271 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1273 case CombineModeComplement
:
1274 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1280 DeleteObject(right
);
1286 return GenericError
;
1293 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1295 return NotImplemented
;
1299 /*****************************************************************************
1300 * GdipGetRegionHRgn [GDIPLUS.@]
1302 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1304 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1306 if (!region
|| !hrgn
)
1307 return InvalidParameter
;
1309 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1312 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1317 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1319 if(!region
|| !graphics
|| !res
)
1320 return InvalidParameter
;
1322 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1323 if (status
!= Ok
) return status
;
1325 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1326 TRACE("=> %d\n", *res
);
1331 /*****************************************************************************
1332 * GdipIsEqualRegion [GDIPLUS.@]
1334 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1340 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1342 if(!region
|| !region2
|| !graphics
|| !res
)
1343 return InvalidParameter
;
1345 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1348 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1350 DeleteObject(hrgn1
);
1354 *res
= EqualRgn(hrgn1
, hrgn2
);
1356 /* one of GpRegions is infinite */
1358 *res
= (!hrgn1
&& !hrgn2
);
1360 DeleteObject(hrgn1
);
1361 DeleteObject(hrgn2
);
1366 /*****************************************************************************
1367 * GdipIsInfiniteRegion [GDIPLUS.@]
1369 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1371 /* I think graphics is ignored here */
1372 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1374 if(!region
|| !graphics
|| !res
)
1375 return InvalidParameter
;
1377 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1382 /*****************************************************************************
1383 * GdipIsVisibleRegionRect [GDIPLUS.@]
1385 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1391 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1394 return InvalidParameter
;
1396 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1405 SetRect(&rect
, ceilr(x
), ceilr(y
), ceilr(x
+ w
), ceilr(y
+ h
));
1406 *res
= RectInRegion(hrgn
, &rect
);
1413 /*****************************************************************************
1414 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1416 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1418 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1420 return InvalidParameter
;
1422 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1425 /*****************************************************************************
1426 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1428 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1433 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1436 return InvalidParameter
;
1438 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1447 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1454 /*****************************************************************************
1455 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1457 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1459 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1461 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1464 /*****************************************************************************
1465 * GdipSetEmpty [GDIPLUS.@]
1467 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1471 TRACE("%p\n", region
);
1474 return InvalidParameter
;
1476 delete_element(®ion
->node
);
1477 stat
= init_region(region
, RegionDataEmptyRect
);
1482 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1486 TRACE("%p\n", region
);
1489 return InvalidParameter
;
1491 delete_element(®ion
->node
);
1492 stat
= init_region(region
, RegionDataInfiniteRect
);
1497 /* Transforms GpRegion elements with given matrix */
1498 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1502 switch(element
->type
)
1504 case RegionDataEmptyRect
:
1505 case RegionDataInfiniteRect
:
1507 case RegionDataRect
:
1509 /* We can't transform a rectangle, so convert it to a path. */
1510 GpRegion
*new_region
;
1513 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1516 stat
= GdipAddPathRectangle(path
,
1517 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1518 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1521 stat
= GdipCreateRegionPath(path
, &new_region
);
1523 GdipDeletePath(path
);
1528 /* Steal the element from the created region. */
1529 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1530 heap_free(new_region
);
1535 /* Fall-through to do the actual conversion. */
1536 case RegionDataPath
:
1537 if (!element
->elementdata
.path
->pathdata
.Count
)
1540 stat
= GdipTransformMatrixPoints(matrix
,
1541 element
->elementdata
.path
->pathdata
.Points
,
1542 element
->elementdata
.path
->pathdata
.Count
);
1545 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1547 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1552 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1554 TRACE("(%p, %p)\n", region
, matrix
);
1556 if (!region
|| !matrix
)
1557 return InvalidParameter
;
1559 return transform_region_element(®ion
->node
, matrix
);
1562 /* Translates GpRegion elements with specified offsets */
1563 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1567 switch(element
->type
)
1569 case RegionDataEmptyRect
:
1570 case RegionDataInfiniteRect
:
1572 case RegionDataRect
:
1573 element
->elementdata
.rect
.X
+= dx
;
1574 element
->elementdata
.rect
.Y
+= dy
;
1576 case RegionDataPath
:
1577 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1578 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1579 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1583 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1584 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1589 /*****************************************************************************
1590 * GdipTranslateRegion [GDIPLUS.@]
1592 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1594 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1597 return InvalidParameter
;
1599 translate_region_element(®ion
->node
, dx
, dy
);
1604 /*****************************************************************************
1605 * GdipTranslateRegionI [GDIPLUS.@]
1607 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1609 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1611 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1614 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1616 GpRegion
*region_copy
;
1621 stat
= GdipCloneRegion(region
, ®ion_copy
);
1625 stat
= GdipTransformRegion(region_copy
, matrix
);
1628 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1634 data_size
= GetRegionData(hrgn
, 0, NULL
);
1636 *data
= heap_alloc_zero(data_size
);
1639 GetRegionData(hrgn
, data_size
, *data
);
1647 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1649 *data
= heap_alloc_zero(data_size
);
1653 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1654 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1655 (*data
)->rdh
.nCount
= 1;
1656 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1657 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1658 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1660 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1667 GdipDeleteRegion(region_copy
);
1673 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1678 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1680 if (!region
|| !count
|| !matrix
)
1681 return InvalidParameter
;
1683 stat
= get_region_scans_data(region
, matrix
, &data
);
1687 *count
= data
->rdh
.nCount
;
1694 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1701 if (!region
|| !count
|| !matrix
)
1702 return InvalidParameter
;
1704 stat
= get_region_scans_data(region
, matrix
, &data
);
1708 *count
= data
->rdh
.nCount
;
1709 rects
= (RECT
*)data
->Buffer
;
1713 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1715 scans
[i
].X
= rects
[i
].left
;
1716 scans
[i
].Y
= rects
[i
].top
;
1717 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1718 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1728 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1735 if (!region
|| !count
|| !matrix
)
1736 return InvalidParameter
;
1738 stat
= get_region_scans_data(region
, matrix
, &data
);
1742 *count
= data
->rdh
.nCount
;
1743 rects
= (RECT
*)data
->Buffer
;
1747 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1749 scans
[i
].X
= rects
[i
].left
;
1750 scans
[i
].Y
= rects
[i
].top
;
1751 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1752 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;