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
85 struct region_data_header
89 struct region_header header
;
100 typedef struct packed_point
106 static inline INT
get_element_size(const region_element
* element
)
108 INT needed
= sizeof(DWORD
); /* DWORD for the type */
109 switch(element
->type
)
112 return needed
+ sizeof(GpRect
);
115 needed
+= write_path_data(element
->elementdata
.path
, NULL
);
116 needed
+= sizeof(DWORD
); /* Extra DWORD for path size */
119 case RegionDataEmptyRect
:
120 case RegionDataInfiniteRect
:
123 needed
+= get_element_size(element
->elementdata
.combine
.left
);
124 needed
+= get_element_size(element
->elementdata
.combine
.right
);
131 /* Does not check parameters, caller must do that */
132 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
134 region
->node
.type
= type
;
135 region
->num_children
= 0;
140 static inline GpStatus
clone_element(const region_element
* element
,
141 region_element
** element2
)
145 /* root node is allocated with GpRegion */
147 *element2
= heap_alloc_zero(sizeof(region_element
));
152 (*element2
)->type
= element
->type
;
154 switch (element
->type
)
157 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
159 case RegionDataEmptyRect
:
160 case RegionDataInfiniteRect
:
163 stat
= GdipClonePath(element
->elementdata
.path
, &(*element2
)->elementdata
.path
);
164 if (stat
== Ok
) return Ok
;
167 (*element2
)->elementdata
.combine
.left
= NULL
;
168 (*element2
)->elementdata
.combine
.right
= NULL
;
170 stat
= clone_element(element
->elementdata
.combine
.left
,
171 &(*element2
)->elementdata
.combine
.left
);
174 stat
= clone_element(element
->elementdata
.combine
.right
,
175 &(*element2
)->elementdata
.combine
.right
);
176 if (stat
== Ok
) return Ok
;
181 delete_element(*element2
);
186 /* Common code for CombineRegion*
187 * All the caller has to do is get its format into an element
189 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
190 region_element
* right
, const CombineMode mode
)
192 region
->node
.type
= mode
;
193 region
->node
.elementdata
.combine
.left
= left
;
194 region
->node
.elementdata
.combine
.right
= right
;
195 region
->num_children
+= 2;
198 /*****************************************************************************
199 * GdipCloneRegion [GDIPLUS.@]
201 * Creates a deep copy of the region
204 * region [I] source region
205 * clone [O] resulting clone
209 * FAILURE: InvalidParameter or OutOfMemory
211 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
213 region_element
*element
;
215 TRACE("%p %p\n", region
, clone
);
217 if (!(region
&& clone
))
218 return InvalidParameter
;
220 *clone
= heap_alloc_zero(sizeof(GpRegion
));
223 element
= &(*clone
)->node
;
225 (*clone
)->num_children
= region
->num_children
;
226 return clone_element(®ion
->node
, &element
);
229 /*****************************************************************************
230 * GdipCombineRegionPath [GDIPLUS.@]
232 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
234 GpRegion
*path_region
;
235 region_element
*left
, *right
= NULL
;
238 TRACE("%p %p %d\n", region
, path
, mode
);
240 if (!(region
&& path
))
241 return InvalidParameter
;
243 stat
= GdipCreateRegionPath(path
, &path_region
);
247 /* simply replace region data */
248 if(mode
== CombineModeReplace
){
249 delete_element(®ion
->node
);
250 memcpy(region
, path_region
, sizeof(GpRegion
));
251 heap_free(path_region
);
255 left
= heap_alloc_zero(sizeof(region_element
));
258 *left
= region
->node
;
259 stat
= clone_element(&path_region
->node
, &right
);
262 fuse_region(region
, left
, right
, mode
);
263 GdipDeleteRegion(path_region
);
271 GdipDeleteRegion(path_region
);
275 /*****************************************************************************
276 * GdipCombineRegionRect [GDIPLUS.@]
278 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
279 GDIPCONST GpRectF
*rect
, CombineMode mode
)
281 GpRegion
*rect_region
;
282 region_element
*left
, *right
= NULL
;
285 TRACE("%p %s %d\n", region
, debugstr_rectf(rect
), mode
);
287 if (!(region
&& rect
))
288 return InvalidParameter
;
290 stat
= GdipCreateRegionRect(rect
, &rect_region
);
294 /* simply replace region data */
295 if(mode
== CombineModeReplace
){
296 delete_element(®ion
->node
);
297 memcpy(region
, rect_region
, sizeof(GpRegion
));
298 heap_free(rect_region
);
302 left
= heap_alloc_zero(sizeof(region_element
));
305 memcpy(left
, ®ion
->node
, sizeof(region_element
));
306 stat
= clone_element(&rect_region
->node
, &right
);
309 fuse_region(region
, left
, right
, mode
);
310 GdipDeleteRegion(rect_region
);
318 GdipDeleteRegion(rect_region
);
322 /*****************************************************************************
323 * GdipCombineRegionRectI [GDIPLUS.@]
325 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
326 GDIPCONST GpRect
*rect
, CombineMode mode
)
330 TRACE("%p %p %d\n", region
, rect
, mode
);
333 return InvalidParameter
;
335 set_rect(&rectf
, rect
->X
, rect
->Y
, rect
->Width
, rect
->Height
);
336 return GdipCombineRegionRect(region
, &rectf
, mode
);
339 /*****************************************************************************
340 * GdipCombineRegionRegion [GDIPLUS.@]
342 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
343 GpRegion
*region2
, CombineMode mode
)
345 region_element
*left
, *right
= NULL
;
349 TRACE("%p %p %d\n", region1
, region2
, mode
);
351 if(!(region1
&& region2
))
352 return InvalidParameter
;
354 /* simply replace region data */
355 if(mode
== CombineModeReplace
){
356 stat
= GdipCloneRegion(region2
, ®2copy
);
357 if(stat
!= Ok
) return stat
;
359 delete_element(®ion1
->node
);
360 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
365 left
= heap_alloc_zero(sizeof(region_element
));
369 *left
= region1
->node
;
370 stat
= clone_element(®ion2
->node
, &right
);
377 fuse_region(region1
, left
, right
, mode
);
378 region1
->num_children
+= region2
->num_children
;
383 /*****************************************************************************
384 * GdipCreateRegion [GDIPLUS.@]
386 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
388 TRACE("%p\n", region
);
391 return InvalidParameter
;
393 *region
= heap_alloc_zero(sizeof(GpRegion
));
397 TRACE("=> %p\n", *region
);
399 return init_region(*region
, RegionDataInfiniteRect
);
402 /*****************************************************************************
403 * GdipCreateRegionPath [GDIPLUS.@]
405 * Creates a GpRegion from a GpPath
408 * path [I] path to base the region on
409 * region [O] pointer to the newly allocated region
413 * FAILURE: InvalidParameter
416 * If a path has no floating point points, its points will be stored as shorts
419 * If a path is empty, it is considered to be an INTPATH
421 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
423 region_element
* element
;
426 TRACE("%p, %p\n", path
, region
);
428 if (!(path
&& region
))
429 return InvalidParameter
;
431 *region
= heap_alloc_zero(sizeof(GpRegion
));
434 stat
= init_region(*region
, RegionDataPath
);
437 GdipDeleteRegion(*region
);
440 element
= &(*region
)->node
;
442 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
445 GdipDeleteRegion(*region
);
452 /*****************************************************************************
453 * GdipCreateRegionRect [GDIPLUS.@]
455 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
460 TRACE("%s, %p\n", debugstr_rectf(rect
), region
);
462 if (!(rect
&& region
))
463 return InvalidParameter
;
465 *region
= heap_alloc_zero(sizeof(GpRegion
));
466 stat
= init_region(*region
, RegionDataRect
);
469 GdipDeleteRegion(*region
);
473 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
474 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
475 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
476 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
481 /*****************************************************************************
482 * GdipCreateRegionRectI [GDIPLUS.@]
484 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
489 TRACE("%p, %p\n", rect
, region
);
491 set_rect(&rectf
, rect
->X
, rect
->Y
, rect
->Width
, rect
->Height
);
492 return GdipCreateRegionRect(&rectf
, region
);
495 /******************************************************************************
496 * GdipCreateRegionHrgn [GDIPLUS.@]
498 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
508 TRACE("(%p, %p)\n", hrgn
, region
);
510 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
511 return InvalidParameter
;
513 buf
= heap_alloc_zero(size
);
517 if(!GetRegionData(hrgn
, size
, buf
)){
522 if(buf
->rdh
.nCount
== 0){
523 if((stat
= GdipCreateRegion(&local
)) != Ok
){
527 if((stat
= GdipSetEmpty(local
)) != Ok
){
529 GdipDeleteRegion(local
);
537 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
542 rect
= (LPRECT
)buf
->Buffer
;
543 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
544 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
545 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
547 GdipDeletePath(path
);
553 stat
= GdipCreateRegionPath(path
, region
);
556 GdipDeletePath(path
);
560 /*****************************************************************************
561 * GdipDeleteRegion [GDIPLUS.@]
563 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
565 TRACE("%p\n", region
);
568 return InvalidParameter
;
570 delete_element(®ion
->node
);
576 /*****************************************************************************
577 * GdipGetRegionBounds [GDIPLUS.@]
579 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
585 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
587 if(!region
|| !graphics
|| !rect
)
588 return InvalidParameter
;
590 /* Contrary to MSDN, native ignores the graphics transform. */
591 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
597 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
598 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
599 TRACE("%p => infinite\n", region
);
603 if(GetRgnBox(hrgn
, &r
)){
606 rect
->Width
= r
.right
- r
.left
;
607 rect
->Height
= r
.bottom
- r
.top
;
608 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
611 status
= GenericError
;
618 /*****************************************************************************
619 * GdipGetRegionBoundsI [GDIPLUS.@]
621 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
626 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
629 return InvalidParameter
;
631 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
633 rect
->X
= gdip_round(rectf
.X
);
634 rect
->Y
= gdip_round(rectf
.Y
);
635 rect
->Width
= gdip_round(rectf
.Width
);
636 rect
->Height
= gdip_round(rectf
.Height
);
642 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
644 location
[*offset
] = write
;
648 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
650 ((FLOAT
*)location
)[*offset
] = write
;
654 static void write_element(const region_element
* element
, DWORD
*buffer
,
657 write_dword(buffer
, filled
, element
->type
);
658 switch (element
->type
)
660 case CombineModeReplace
:
661 case CombineModeIntersect
:
662 case CombineModeUnion
:
664 case CombineModeExclude
:
665 case CombineModeComplement
:
666 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
667 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
670 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
671 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
672 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
673 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
677 DWORD size
= write_path_data(element
->elementdata
.path
, buffer
+ *filled
+ 1);
678 write_dword(buffer
, filled
, size
);
679 *filled
+= size
/ sizeof(DWORD
);
682 case RegionDataEmptyRect
:
683 case RegionDataInfiniteRect
:
688 DWORD
write_region_data(const GpRegion
*region
, void *data
)
690 struct region_header
*header
= data
;
694 size
= sizeof(struct region_header
) + get_element_size(®ion
->node
);
695 if (!data
) return size
;
697 header
->magic
= VERSION_MAGIC2
;
698 header
->num_children
= region
->num_children
;
700 /* With few exceptions, everything written is DWORD aligned,
701 * so use that as our base */
702 write_element(®ion
->node
, (DWORD
*)data
, &filled
);
706 /*****************************************************************************
707 * GdipGetRegionData [GDIPLUS.@]
709 * Returns the header, followed by combining ops and region elements.
712 * region [I] region to retrieve from
713 * buffer [O] buffer to hold the resulting data
714 * size [I] size of the buffer
715 * needed [O] (optional) how much data was written
719 * FAILURE: InvalidParameter
722 * The header contains the size, a checksum, a version string, and the number
723 * of children. The size does not count itself or the checksum.
724 * Version is always something like 0xdbc01001 or 0xdbc01002
726 * An element is a RECT, or PATH; Combining ops are stored as their
727 * CombineMode value. Special regions (infinite, empty) emit just their
728 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
729 * their code followed by a second header for the path followed by the actual
730 * path data. Followed by the flags for each point. The pathheader contains
731 * the size of the data to follow, a version number again, followed by a count
732 * of how many points, and any special flags which may apply. 0x4000 means it's
733 * a path of shorts instead of FLOAT.
735 * Combining Ops are stored in reverse order from when they were constructed;
736 * the output is a tree where the left side combining area is always taken
739 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
742 struct region_data_header
*region_data_header
;
745 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
747 if (!region
|| !buffer
|| !size
)
748 return InvalidParameter
;
750 required
= FIELD_OFFSET(struct region_data_header
, header
) + write_region_data(region
, NULL
);
753 if (needed
) *needed
= size
;
754 return InsufficientBuffer
;
757 region_data_header
= (struct region_data_header
*)buffer
;
758 region_data_header
->size
= write_region_data(region
, ®ion_data_header
->header
);
759 region_data_header
->checksum
= 0;
767 static GpStatus
read_element(struct memory_buffer
*mbuf
, GpRegion
*region
, region_element
*node
, INT
*count
)
772 type
= buffer_read(mbuf
, sizeof(*type
));
773 if (!type
) return Ok
;
775 TRACE("type %#x\n", *type
);
781 case CombineModeReplace
:
782 case CombineModeIntersect
:
783 case CombineModeUnion
:
785 case CombineModeExclude
:
786 case CombineModeComplement
:
788 region_element
*left
, *right
;
790 left
= heap_alloc_zero(sizeof(region_element
));
791 if (!left
) return OutOfMemory
;
792 right
= heap_alloc_zero(sizeof(region_element
));
799 status
= read_element(mbuf
, region
, left
, count
);
802 status
= read_element(mbuf
, region
, right
, count
);
805 node
->elementdata
.combine
.left
= left
;
806 node
->elementdata
.combine
.right
= right
;
807 region
->num_children
+= 2;
821 rc
= buffer_read(mbuf
, sizeof(*rc
));
824 ERR("failed to read rect data\n");
825 return InvalidParameter
;
828 node
->elementdata
.rect
= *rc
;
836 const struct path_header
*path_header
;
839 path_header
= buffer_read(mbuf
, sizeof(*path_header
));
842 ERR("failed to read path header\n");
843 return InvalidParameter
;
845 if (!VALID_MAGIC(path_header
->magic
))
847 ERR("invalid path header magic %#x\n", path_header
->magic
);
848 return InvalidParameter
;
851 /* Windows always fails to create an empty path in a region */
852 if (!path_header
->count
)
854 TRACE("refusing to create an empty path in a region\n");
858 status
= GdipCreatePath(FillModeAlternate
, &path
);
859 if (status
) return status
;
861 node
->elementdata
.path
= path
;
863 if (!lengthen_path(path
, path_header
->count
))
866 path
->pathdata
.Count
= path_header
->count
;
868 if (path_header
->flags
& ~FLAGS_INTPATH
)
869 FIXME("unhandled path flags %#x\n", path_header
->flags
);
871 if (path_header
->flags
& FLAGS_INTPATH
)
873 const packed_point
*pt
;
876 pt
= buffer_read(mbuf
, sizeof(*pt
) * path_header
->count
);
879 ERR("failed to read packed %u path points\n", path_header
->count
);
880 return InvalidParameter
;
883 for (i
= 0; i
< path_header
->count
; i
++)
885 path
->pathdata
.Points
[i
].X
= (REAL
)pt
[i
].X
;
886 path
->pathdata
.Points
[i
].Y
= (REAL
)pt
[i
].Y
;
893 ptf
= buffer_read(mbuf
, sizeof(*ptf
) * path_header
->count
);
896 ERR("failed to read %u path points\n", path_header
->count
);
897 return InvalidParameter
;
899 memcpy(path
->pathdata
.Points
, ptf
, sizeof(*ptf
) * path_header
->count
);
902 types
= buffer_read(mbuf
, path_header
->count
);
905 ERR("failed to read %u path types\n", path_header
->count
);
906 return InvalidParameter
;
908 memcpy(path
->pathdata
.Types
, types
, path_header
->count
);
909 if (path_header
->count
& 3)
911 if (!buffer_read(mbuf
, 4 - (path_header
->count
& 3)))
913 ERR("failed to read rounding %u bytes\n", 4 - (path_header
->count
& 3));
914 return InvalidParameter
;
922 case RegionDataEmptyRect
:
923 case RegionDataInfiniteRect
:
928 FIXME("element type %#x is not supported\n", *type
);
932 return InvalidParameter
;
935 /*****************************************************************************
936 * GdipCreateRegionRgnData [GDIPLUS.@]
938 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
940 const struct region_data_header
*region_data_header
;
941 struct memory_buffer mbuf
;
945 TRACE("(%p, %d, %p)\n", data
, size
, region
);
948 return InvalidParameter
;
950 init_memory_buffer(&mbuf
, data
, size
);
952 region_data_header
= buffer_read(&mbuf
, sizeof(*region_data_header
));
953 if (!region_data_header
|| !VALID_MAGIC(region_data_header
->header
.magic
))
954 return InvalidParameter
;
956 status
= GdipCreateRegion(region
);
961 status
= read_element(&mbuf
, *region
, &(*region
)->node
, &count
);
962 if (status
== Ok
&& !count
)
963 status
= InvalidParameter
;
967 GdipDeleteRegion(*region
);
974 /*****************************************************************************
975 * GdipGetRegionDataSize [GDIPLUS.@]
977 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
979 TRACE("%p, %p\n", region
, needed
);
981 if (!(region
&& needed
))
982 return InvalidParameter
;
984 /* header.size doesn't count header.size and header.checksum */
985 *needed
= FIELD_OFFSET(struct region_data_header
, header
) + write_region_data(region
, NULL
);
990 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
993 GpGraphics
*new_graphics
=NULL
;
997 if (!path
->pathdata
.Count
) /* PathToRegion doesn't support empty paths */
999 *hrgn
= CreateRectRgn( 0, 0, 0, 0 );
1000 return *hrgn
? Ok
: OutOfMemory
;
1005 new_hdc
= CreateCompatibleDC(0);
1009 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
1010 graphics
= new_graphics
;
1017 else if (!graphics
->hdc
)
1019 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
1024 save_state
= SaveDC(graphics
->hdc
);
1025 EndPath(graphics
->hdc
);
1027 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1030 gdi_transform_acquire(graphics
);
1032 stat
= trace_path(graphics
, path
);
1035 *hrgn
= PathToRegion(graphics
->hdc
);
1036 stat
= *hrgn
? Ok
: OutOfMemory
;
1039 gdi_transform_release(graphics
);
1041 RestoreDC(graphics
->hdc
, save_state
);
1046 GdipDeleteGraphics(new_graphics
);
1048 graphics
->hdc
= NULL
;
1054 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
1056 switch (element
->type
)
1058 case RegionDataInfiniteRect
:
1061 case RegionDataEmptyRect
:
1062 *hrgn
= CreateRectRgn(0, 0, 0, 0);
1063 return *hrgn
? Ok
: OutOfMemory
;
1064 case RegionDataPath
:
1065 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
1066 case RegionDataRect
:
1070 GpRectF
* rc
= &element
->elementdata
.rect
;
1072 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1075 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
1078 stat
= get_path_hrgn(path
, graphics
, hrgn
);
1080 GdipDeletePath(path
);
1084 case CombineModeIntersect
:
1085 case CombineModeUnion
:
1086 case CombineModeXor
:
1087 case CombineModeExclude
:
1088 case CombineModeComplement
:
1094 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
1103 /* existing region is infinite */
1104 switch (element
->type
)
1106 case CombineModeIntersect
:
1107 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
1108 case CombineModeXor
: case CombineModeExclude
:
1109 left
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1111 case CombineModeUnion
: case CombineModeComplement
:
1117 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1127 /* new region is infinite */
1128 switch (element
->type
)
1130 case CombineModeIntersect
:
1133 case CombineModeXor
: case CombineModeComplement
:
1134 right
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1136 case CombineModeUnion
: case CombineModeExclude
:
1143 switch (element
->type
)
1145 case CombineModeIntersect
:
1146 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1148 case CombineModeUnion
:
1149 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1151 case CombineModeXor
:
1152 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1154 case CombineModeExclude
:
1155 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1157 case CombineModeComplement
:
1158 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1164 DeleteObject(right
);
1170 return GenericError
;
1177 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1179 return NotImplemented
;
1183 /*****************************************************************************
1184 * GdipGetRegionHRgn [GDIPLUS.@]
1186 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1188 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1190 if (!region
|| !hrgn
)
1191 return InvalidParameter
;
1193 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1196 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1201 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1203 if(!region
|| !graphics
|| !res
)
1204 return InvalidParameter
;
1206 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1207 if (status
!= Ok
) return status
;
1209 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1210 TRACE("=> %d\n", *res
);
1215 /*****************************************************************************
1216 * GdipIsEqualRegion [GDIPLUS.@]
1218 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1224 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1226 if(!region
|| !region2
|| !graphics
|| !res
)
1227 return InvalidParameter
;
1229 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1232 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1234 DeleteObject(hrgn1
);
1238 *res
= EqualRgn(hrgn1
, hrgn2
);
1240 /* one of GpRegions is infinite */
1242 *res
= (!hrgn1
&& !hrgn2
);
1244 DeleteObject(hrgn1
);
1245 DeleteObject(hrgn2
);
1250 /*****************************************************************************
1251 * GdipIsInfiniteRegion [GDIPLUS.@]
1253 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1255 /* I think graphics is ignored here */
1256 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1258 if(!region
|| !graphics
|| !res
)
1259 return InvalidParameter
;
1261 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1266 /*****************************************************************************
1267 * GdipIsVisibleRegionRect [GDIPLUS.@]
1269 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1275 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1278 return InvalidParameter
;
1280 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1289 SetRect(&rect
, ceilr(x
), ceilr(y
), ceilr(x
+ w
), ceilr(y
+ h
));
1290 *res
= RectInRegion(hrgn
, &rect
);
1297 /*****************************************************************************
1298 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1300 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1302 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1304 return InvalidParameter
;
1306 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1309 /*****************************************************************************
1310 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1312 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1317 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1320 return InvalidParameter
;
1322 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1331 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1338 /*****************************************************************************
1339 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1341 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1343 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1345 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1348 /*****************************************************************************
1349 * GdipSetEmpty [GDIPLUS.@]
1351 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1355 TRACE("%p\n", region
);
1358 return InvalidParameter
;
1360 delete_element(®ion
->node
);
1361 stat
= init_region(region
, RegionDataEmptyRect
);
1366 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1370 TRACE("%p\n", region
);
1373 return InvalidParameter
;
1375 delete_element(®ion
->node
);
1376 stat
= init_region(region
, RegionDataInfiniteRect
);
1381 /* Transforms GpRegion elements with given matrix */
1382 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1386 switch(element
->type
)
1388 case RegionDataEmptyRect
:
1389 case RegionDataInfiniteRect
:
1391 case RegionDataRect
:
1393 GpRegion
*new_region
;
1396 if (matrix
->matrix
[1] == 0.0 && matrix
->matrix
[2] == 0.0)
1400 points
[0].X
= element
->elementdata
.rect
.X
;
1401 points
[0].Y
= element
->elementdata
.rect
.Y
;
1402 points
[1].X
= element
->elementdata
.rect
.X
+ element
->elementdata
.rect
.Width
;
1403 points
[1].Y
= element
->elementdata
.rect
.Y
+ element
->elementdata
.rect
.Height
;
1405 stat
= GdipTransformMatrixPoints(matrix
, points
, 2);
1409 if (points
[0].X
> points
[1].X
)
1413 points
[0].X
= points
[1].X
;
1417 if (points
[0].Y
> points
[1].Y
)
1421 points
[0].Y
= points
[1].Y
;
1425 element
->elementdata
.rect
.X
= points
[0].X
;
1426 element
->elementdata
.rect
.Y
= points
[0].Y
;
1427 element
->elementdata
.rect
.Width
= points
[1].X
- points
[0].X
;
1428 element
->elementdata
.rect
.Height
= points
[1].Y
- points
[0].Y
;
1432 /* We can't rotate/shear a rectangle, so convert it to a path. */
1433 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1436 stat
= GdipAddPathRectangle(path
,
1437 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1438 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1441 stat
= GdipCreateRegionPath(path
, &new_region
);
1443 GdipDeletePath(path
);
1448 /* Steal the element from the created region. */
1449 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1450 heap_free(new_region
);
1455 /* Fall-through to do the actual conversion. */
1456 case RegionDataPath
:
1457 if (!element
->elementdata
.path
->pathdata
.Count
)
1460 stat
= GdipTransformMatrixPoints(matrix
,
1461 element
->elementdata
.path
->pathdata
.Points
,
1462 element
->elementdata
.path
->pathdata
.Count
);
1465 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1467 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1472 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1474 TRACE("(%p, %p)\n", region
, matrix
);
1476 if (!region
|| !matrix
)
1477 return InvalidParameter
;
1479 return transform_region_element(®ion
->node
, matrix
);
1482 /* Translates GpRegion elements with specified offsets */
1483 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1487 switch(element
->type
)
1489 case RegionDataEmptyRect
:
1490 case RegionDataInfiniteRect
:
1492 case RegionDataRect
:
1493 element
->elementdata
.rect
.X
+= dx
;
1494 element
->elementdata
.rect
.Y
+= dy
;
1496 case RegionDataPath
:
1497 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1498 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1499 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1503 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1504 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1509 /*****************************************************************************
1510 * GdipTranslateRegion [GDIPLUS.@]
1512 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1514 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1517 return InvalidParameter
;
1519 translate_region_element(®ion
->node
, dx
, dy
);
1524 /*****************************************************************************
1525 * GdipTranslateRegionI [GDIPLUS.@]
1527 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1529 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1531 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1534 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1536 GpRegion
*region_copy
;
1541 stat
= GdipCloneRegion(region
, ®ion_copy
);
1545 stat
= GdipTransformRegion(region_copy
, matrix
);
1548 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1554 data_size
= GetRegionData(hrgn
, 0, NULL
);
1556 *data
= heap_alloc_zero(data_size
);
1559 GetRegionData(hrgn
, data_size
, *data
);
1567 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1569 *data
= heap_alloc_zero(data_size
);
1573 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1574 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1575 (*data
)->rdh
.nCount
= 1;
1576 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1577 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1578 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1580 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1587 GdipDeleteRegion(region_copy
);
1593 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1598 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1600 if (!region
|| !count
|| !matrix
)
1601 return InvalidParameter
;
1603 stat
= get_region_scans_data(region
, matrix
, &data
);
1607 *count
= data
->rdh
.nCount
;
1614 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1621 if (!region
|| !count
|| !matrix
)
1622 return InvalidParameter
;
1624 stat
= get_region_scans_data(region
, matrix
, &data
);
1628 *count
= data
->rdh
.nCount
;
1629 rects
= (RECT
*)data
->Buffer
;
1633 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1635 scans
[i
].X
= rects
[i
].left
;
1636 scans
[i
].Y
= rects
[i
].top
;
1637 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1638 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1648 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1655 if (!region
|| !count
|| !matrix
)
1656 return InvalidParameter
;
1658 stat
= get_region_scans_data(region
, matrix
, &data
);
1662 *count
= data
->rdh
.nCount
;
1663 rects
= (RECT
*)data
->Buffer
;
1667 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1669 scans
[i
].X
= rects
[i
].left
;
1670 scans
[i
].Y
= rects
[i
].top
;
1671 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1672 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;