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 rectf
.X
= (REAL
)rect
->X
;
336 rectf
.Y
= (REAL
)rect
->Y
;
337 rectf
.Height
= (REAL
)rect
->Height
;
338 rectf
.Width
= (REAL
)rect
->Width
;
340 return GdipCombineRegionRect(region
, &rectf
, mode
);
343 /*****************************************************************************
344 * GdipCombineRegionRegion [GDIPLUS.@]
346 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
347 GpRegion
*region2
, CombineMode mode
)
349 region_element
*left
, *right
= NULL
;
353 TRACE("%p %p %d\n", region1
, region2
, mode
);
355 if(!(region1
&& region2
))
356 return InvalidParameter
;
358 /* simply replace region data */
359 if(mode
== CombineModeReplace
){
360 stat
= GdipCloneRegion(region2
, ®2copy
);
361 if(stat
!= Ok
) return stat
;
363 delete_element(®ion1
->node
);
364 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
369 left
= heap_alloc_zero(sizeof(region_element
));
373 *left
= region1
->node
;
374 stat
= clone_element(®ion2
->node
, &right
);
381 fuse_region(region1
, left
, right
, mode
);
382 region1
->num_children
+= region2
->num_children
;
387 /*****************************************************************************
388 * GdipCreateRegion [GDIPLUS.@]
390 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
392 TRACE("%p\n", region
);
395 return InvalidParameter
;
397 *region
= heap_alloc_zero(sizeof(GpRegion
));
401 TRACE("=> %p\n", *region
);
403 return init_region(*region
, RegionDataInfiniteRect
);
406 /*****************************************************************************
407 * GdipCreateRegionPath [GDIPLUS.@]
409 * Creates a GpRegion from a GpPath
412 * path [I] path to base the region on
413 * region [O] pointer to the newly allocated region
417 * FAILURE: InvalidParameter
420 * If a path has no floating point points, its points will be stored as shorts
423 * If a path is empty, it is considered to be an INTPATH
425 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
427 region_element
* element
;
430 TRACE("%p, %p\n", path
, region
);
432 if (!(path
&& region
))
433 return InvalidParameter
;
435 *region
= heap_alloc_zero(sizeof(GpRegion
));
438 stat
= init_region(*region
, RegionDataPath
);
441 GdipDeleteRegion(*region
);
444 element
= &(*region
)->node
;
446 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
449 GdipDeleteRegion(*region
);
456 /*****************************************************************************
457 * GdipCreateRegionRect [GDIPLUS.@]
459 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
464 TRACE("%s, %p\n", debugstr_rectf(rect
), region
);
466 if (!(rect
&& region
))
467 return InvalidParameter
;
469 *region
= heap_alloc_zero(sizeof(GpRegion
));
470 stat
= init_region(*region
, RegionDataRect
);
473 GdipDeleteRegion(*region
);
477 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
478 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
479 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
480 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
485 /*****************************************************************************
486 * GdipCreateRegionRectI [GDIPLUS.@]
488 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
493 TRACE("%p, %p\n", rect
, region
);
495 rectf
.X
= (REAL
)rect
->X
;
496 rectf
.Y
= (REAL
)rect
->Y
;
497 rectf
.Width
= (REAL
)rect
->Width
;
498 rectf
.Height
= (REAL
)rect
->Height
;
500 return GdipCreateRegionRect(&rectf
, region
);
503 /******************************************************************************
504 * GdipCreateRegionHrgn [GDIPLUS.@]
506 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
516 TRACE("(%p, %p)\n", hrgn
, region
);
518 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
519 return InvalidParameter
;
521 buf
= heap_alloc_zero(size
);
525 if(!GetRegionData(hrgn
, size
, buf
)){
530 if(buf
->rdh
.nCount
== 0){
531 if((stat
= GdipCreateRegion(&local
)) != Ok
){
535 if((stat
= GdipSetEmpty(local
)) != Ok
){
537 GdipDeleteRegion(local
);
545 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
550 rect
= (LPRECT
)buf
->Buffer
;
551 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
552 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
553 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
555 GdipDeletePath(path
);
561 stat
= GdipCreateRegionPath(path
, region
);
564 GdipDeletePath(path
);
568 /*****************************************************************************
569 * GdipDeleteRegion [GDIPLUS.@]
571 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
573 TRACE("%p\n", region
);
576 return InvalidParameter
;
578 delete_element(®ion
->node
);
584 /*****************************************************************************
585 * GdipGetRegionBounds [GDIPLUS.@]
587 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
593 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
595 if(!region
|| !graphics
|| !rect
)
596 return InvalidParameter
;
598 /* Contrary to MSDN, native ignores the graphics transform. */
599 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
605 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
606 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
607 TRACE("%p => infinite\n", region
);
611 if(GetRgnBox(hrgn
, &r
)){
614 rect
->Width
= r
.right
- r
.left
;
615 rect
->Height
= r
.bottom
- r
.top
;
616 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
619 status
= GenericError
;
626 /*****************************************************************************
627 * GdipGetRegionBoundsI [GDIPLUS.@]
629 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
634 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
637 return InvalidParameter
;
639 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
641 rect
->X
= gdip_round(rectf
.X
);
642 rect
->Y
= gdip_round(rectf
.Y
);
643 rect
->Width
= gdip_round(rectf
.Width
);
644 rect
->Height
= gdip_round(rectf
.Height
);
650 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
652 location
[*offset
] = write
;
656 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
658 ((FLOAT
*)location
)[*offset
] = write
;
662 static void write_element(const region_element
* element
, DWORD
*buffer
,
665 write_dword(buffer
, filled
, element
->type
);
666 switch (element
->type
)
668 case CombineModeReplace
:
669 case CombineModeIntersect
:
670 case CombineModeUnion
:
672 case CombineModeExclude
:
673 case CombineModeComplement
:
674 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
675 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
678 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
679 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
680 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
681 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
685 DWORD size
= write_path_data(element
->elementdata
.path
, buffer
+ *filled
+ 1);
686 write_dword(buffer
, filled
, size
);
687 *filled
+= size
/ sizeof(DWORD
);
690 case RegionDataEmptyRect
:
691 case RegionDataInfiniteRect
:
696 DWORD
write_region_data(const GpRegion
*region
, void *data
)
698 struct region_header
*header
= data
;
702 size
= sizeof(struct region_header
) + get_element_size(®ion
->node
);
703 if (!data
) return size
;
705 header
->magic
= VERSION_MAGIC2
;
706 header
->num_children
= region
->num_children
;
708 /* With few exceptions, everything written is DWORD aligned,
709 * so use that as our base */
710 write_element(®ion
->node
, (DWORD
*)data
, &filled
);
714 /*****************************************************************************
715 * GdipGetRegionData [GDIPLUS.@]
717 * Returns the header, followed by combining ops and region elements.
720 * region [I] region to retrieve from
721 * buffer [O] buffer to hold the resulting data
722 * size [I] size of the buffer
723 * needed [O] (optional) how much data was written
727 * FAILURE: InvalidParameter
730 * The header contains the size, a checksum, a version string, and the number
731 * of children. The size does not count itself or the checksum.
732 * Version is always something like 0xdbc01001 or 0xdbc01002
734 * An element is a RECT, or PATH; Combining ops are stored as their
735 * CombineMode value. Special regions (infinite, empty) emit just their
736 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
737 * their code followed by a second header for the path followed by the actual
738 * path data. Followed by the flags for each point. The pathheader contains
739 * the size of the data to follow, a version number again, followed by a count
740 * of how many points, and any special flags which may apply. 0x4000 means it's
741 * a path of shorts instead of FLOAT.
743 * Combining Ops are stored in reverse order from when they were constructed;
744 * the output is a tree where the left side combining area is always taken
747 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
750 struct region_data_header
*region_data_header
;
753 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
755 if (!region
|| !buffer
|| !size
)
756 return InvalidParameter
;
758 required
= FIELD_OFFSET(struct region_data_header
, header
) + write_region_data(region
, NULL
);
761 if (needed
) *needed
= size
;
762 return InsufficientBuffer
;
765 region_data_header
= (struct region_data_header
*)buffer
;
766 region_data_header
->size
= write_region_data(region
, ®ion_data_header
->header
);
767 region_data_header
->checksum
= 0;
775 static GpStatus
read_element(struct memory_buffer
*mbuf
, GpRegion
*region
, region_element
*node
, INT
*count
)
780 type
= buffer_read(mbuf
, sizeof(*type
));
781 if (!type
) return Ok
;
783 TRACE("type %#x\n", *type
);
789 case CombineModeReplace
:
790 case CombineModeIntersect
:
791 case CombineModeUnion
:
793 case CombineModeExclude
:
794 case CombineModeComplement
:
796 region_element
*left
, *right
;
798 left
= heap_alloc_zero(sizeof(region_element
));
799 if (!left
) return OutOfMemory
;
800 right
= heap_alloc_zero(sizeof(region_element
));
807 status
= read_element(mbuf
, region
, left
, count
);
810 status
= read_element(mbuf
, region
, right
, count
);
813 node
->elementdata
.combine
.left
= left
;
814 node
->elementdata
.combine
.right
= right
;
815 region
->num_children
+= 2;
829 rc
= buffer_read(mbuf
, sizeof(*rc
));
832 ERR("failed to read rect data\n");
833 return InvalidParameter
;
836 node
->elementdata
.rect
= *rc
;
844 const struct path_header
*path_header
;
847 path_header
= buffer_read(mbuf
, sizeof(*path_header
));
850 ERR("failed to read path header\n");
851 return InvalidParameter
;
853 if (!VALID_MAGIC(path_header
->magic
))
855 ERR("invalid path header magic %#x\n", path_header
->magic
);
856 return InvalidParameter
;
859 /* Windows always fails to create an empty path in a region */
860 if (!path_header
->count
)
862 TRACE("refusing to create an empty path in a region\n");
866 status
= GdipCreatePath(FillModeAlternate
, &path
);
867 if (status
) return status
;
869 node
->elementdata
.path
= path
;
871 if (!lengthen_path(path
, path_header
->count
))
874 path
->pathdata
.Count
= path_header
->count
;
876 if (path_header
->flags
& ~FLAGS_INTPATH
)
877 FIXME("unhandled path flags %#x\n", path_header
->flags
);
879 if (path_header
->flags
& FLAGS_INTPATH
)
881 const packed_point
*pt
;
884 pt
= buffer_read(mbuf
, sizeof(*pt
) * path_header
->count
);
887 ERR("failed to read packed %u path points\n", path_header
->count
);
888 return InvalidParameter
;
891 for (i
= 0; i
< path_header
->count
; i
++)
893 path
->pathdata
.Points
[i
].X
= (REAL
)pt
[i
].X
;
894 path
->pathdata
.Points
[i
].Y
= (REAL
)pt
[i
].Y
;
901 ptf
= buffer_read(mbuf
, sizeof(*ptf
) * path_header
->count
);
904 ERR("failed to read %u path points\n", path_header
->count
);
905 return InvalidParameter
;
907 memcpy(path
->pathdata
.Points
, ptf
, sizeof(*ptf
) * path_header
->count
);
910 types
= buffer_read(mbuf
, path_header
->count
);
913 ERR("failed to read %u path types\n", path_header
->count
);
914 return InvalidParameter
;
916 memcpy(path
->pathdata
.Types
, types
, path_header
->count
);
917 if (path_header
->count
& 3)
919 if (!buffer_read(mbuf
, 4 - (path_header
->count
& 3)))
921 ERR("failed to read rounding %u bytes\n", 4 - (path_header
->count
& 3));
922 return InvalidParameter
;
930 case RegionDataEmptyRect
:
931 case RegionDataInfiniteRect
:
936 FIXME("element type %#x is not supported\n", *type
);
940 return InvalidParameter
;
943 /*****************************************************************************
944 * GdipCreateRegionRgnData [GDIPLUS.@]
946 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
948 const struct region_data_header
*region_data_header
;
949 struct memory_buffer mbuf
;
953 TRACE("(%p, %d, %p)\n", data
, size
, region
);
956 return InvalidParameter
;
958 init_memory_buffer(&mbuf
, data
, size
);
960 region_data_header
= buffer_read(&mbuf
, sizeof(*region_data_header
));
961 if (!region_data_header
|| !VALID_MAGIC(region_data_header
->header
.magic
))
962 return InvalidParameter
;
964 status
= GdipCreateRegion(region
);
969 status
= read_element(&mbuf
, *region
, &(*region
)->node
, &count
);
970 if (status
== Ok
&& !count
)
971 status
= InvalidParameter
;
975 GdipDeleteRegion(*region
);
982 /*****************************************************************************
983 * GdipGetRegionDataSize [GDIPLUS.@]
985 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
987 TRACE("%p, %p\n", region
, needed
);
989 if (!(region
&& needed
))
990 return InvalidParameter
;
992 /* header.size doesn't count header.size and header.checksum */
993 *needed
= FIELD_OFFSET(struct region_data_header
, header
) + write_region_data(region
, NULL
);
998 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
1001 GpGraphics
*new_graphics
=NULL
;
1005 if (!path
->pathdata
.Count
) /* PathToRegion doesn't support empty paths */
1007 *hrgn
= CreateRectRgn( 0, 0, 0, 0 );
1008 return *hrgn
? Ok
: OutOfMemory
;
1013 new_hdc
= CreateCompatibleDC(0);
1017 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
1018 graphics
= new_graphics
;
1025 else if (!graphics
->hdc
)
1027 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
1032 save_state
= SaveDC(graphics
->hdc
);
1033 EndPath(graphics
->hdc
);
1035 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1038 gdi_transform_acquire(graphics
);
1040 stat
= trace_path(graphics
, path
);
1043 *hrgn
= PathToRegion(graphics
->hdc
);
1044 stat
= *hrgn
? Ok
: OutOfMemory
;
1047 gdi_transform_release(graphics
);
1049 RestoreDC(graphics
->hdc
, save_state
);
1054 GdipDeleteGraphics(new_graphics
);
1056 graphics
->hdc
= NULL
;
1062 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
1064 switch (element
->type
)
1066 case RegionDataInfiniteRect
:
1069 case RegionDataEmptyRect
:
1070 *hrgn
= CreateRectRgn(0, 0, 0, 0);
1071 return *hrgn
? Ok
: OutOfMemory
;
1072 case RegionDataPath
:
1073 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
1074 case RegionDataRect
:
1078 GpRectF
* rc
= &element
->elementdata
.rect
;
1080 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1083 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
1086 stat
= get_path_hrgn(path
, graphics
, hrgn
);
1088 GdipDeletePath(path
);
1092 case CombineModeIntersect
:
1093 case CombineModeUnion
:
1094 case CombineModeXor
:
1095 case CombineModeExclude
:
1096 case CombineModeComplement
:
1102 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
1111 /* existing region is infinite */
1112 switch (element
->type
)
1114 case CombineModeIntersect
:
1115 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
1116 case CombineModeXor
: case CombineModeExclude
:
1117 left
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1119 case CombineModeUnion
: case CombineModeComplement
:
1125 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1135 /* new region is infinite */
1136 switch (element
->type
)
1138 case CombineModeIntersect
:
1141 case CombineModeXor
: case CombineModeComplement
:
1142 right
= CreateRectRgn(-(1 << 22), -(1 << 22), 1 << 22, 1 << 22);
1144 case CombineModeUnion
: case CombineModeExclude
:
1151 switch (element
->type
)
1153 case CombineModeIntersect
:
1154 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1156 case CombineModeUnion
:
1157 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1159 case CombineModeXor
:
1160 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1162 case CombineModeExclude
:
1163 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1165 case CombineModeComplement
:
1166 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1172 DeleteObject(right
);
1178 return GenericError
;
1185 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1187 return NotImplemented
;
1191 /*****************************************************************************
1192 * GdipGetRegionHRgn [GDIPLUS.@]
1194 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1196 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1198 if (!region
|| !hrgn
)
1199 return InvalidParameter
;
1201 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1204 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1209 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1211 if(!region
|| !graphics
|| !res
)
1212 return InvalidParameter
;
1214 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1215 if (status
!= Ok
) return status
;
1217 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1218 TRACE("=> %d\n", *res
);
1223 /*****************************************************************************
1224 * GdipIsEqualRegion [GDIPLUS.@]
1226 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1232 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1234 if(!region
|| !region2
|| !graphics
|| !res
)
1235 return InvalidParameter
;
1237 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1240 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1242 DeleteObject(hrgn1
);
1246 *res
= EqualRgn(hrgn1
, hrgn2
);
1248 /* one of GpRegions is infinite */
1250 *res
= (!hrgn1
&& !hrgn2
);
1252 DeleteObject(hrgn1
);
1253 DeleteObject(hrgn2
);
1258 /*****************************************************************************
1259 * GdipIsInfiniteRegion [GDIPLUS.@]
1261 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1263 /* I think graphics is ignored here */
1264 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1266 if(!region
|| !graphics
|| !res
)
1267 return InvalidParameter
;
1269 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1274 /*****************************************************************************
1275 * GdipIsVisibleRegionRect [GDIPLUS.@]
1277 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1283 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1286 return InvalidParameter
;
1288 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1297 SetRect(&rect
, ceilr(x
), ceilr(y
), ceilr(x
+ w
), ceilr(y
+ h
));
1298 *res
= RectInRegion(hrgn
, &rect
);
1305 /*****************************************************************************
1306 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1308 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1310 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1312 return InvalidParameter
;
1314 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1317 /*****************************************************************************
1318 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1320 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1325 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1328 return InvalidParameter
;
1330 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1339 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1346 /*****************************************************************************
1347 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1349 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1351 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1353 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1356 /*****************************************************************************
1357 * GdipSetEmpty [GDIPLUS.@]
1359 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1363 TRACE("%p\n", region
);
1366 return InvalidParameter
;
1368 delete_element(®ion
->node
);
1369 stat
= init_region(region
, RegionDataEmptyRect
);
1374 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1378 TRACE("%p\n", region
);
1381 return InvalidParameter
;
1383 delete_element(®ion
->node
);
1384 stat
= init_region(region
, RegionDataInfiniteRect
);
1389 /* Transforms GpRegion elements with given matrix */
1390 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1394 switch(element
->type
)
1396 case RegionDataEmptyRect
:
1397 case RegionDataInfiniteRect
:
1399 case RegionDataRect
:
1401 GpRegion
*new_region
;
1404 if (matrix
->matrix
[1] == 0.0 && matrix
->matrix
[2] == 0.0)
1408 points
[0].X
= element
->elementdata
.rect
.X
;
1409 points
[0].Y
= element
->elementdata
.rect
.Y
;
1410 points
[1].X
= element
->elementdata
.rect
.X
+ element
->elementdata
.rect
.Width
;
1411 points
[1].Y
= element
->elementdata
.rect
.Y
+ element
->elementdata
.rect
.Height
;
1413 stat
= GdipTransformMatrixPoints(matrix
, points
, 2);
1417 if (points
[0].X
> points
[1].X
)
1421 points
[0].X
= points
[1].X
;
1425 if (points
[0].Y
> points
[1].Y
)
1429 points
[0].Y
= points
[1].Y
;
1433 element
->elementdata
.rect
.X
= points
[0].X
;
1434 element
->elementdata
.rect
.Y
= points
[0].Y
;
1435 element
->elementdata
.rect
.Width
= points
[1].X
- points
[0].X
;
1436 element
->elementdata
.rect
.Height
= points
[1].Y
- points
[0].Y
;
1440 /* We can't rotate/shear a rectangle, so convert it to a path. */
1441 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1444 stat
= GdipAddPathRectangle(path
,
1445 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1446 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1449 stat
= GdipCreateRegionPath(path
, &new_region
);
1451 GdipDeletePath(path
);
1456 /* Steal the element from the created region. */
1457 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1458 heap_free(new_region
);
1463 /* Fall-through to do the actual conversion. */
1464 case RegionDataPath
:
1465 if (!element
->elementdata
.path
->pathdata
.Count
)
1468 stat
= GdipTransformMatrixPoints(matrix
,
1469 element
->elementdata
.path
->pathdata
.Points
,
1470 element
->elementdata
.path
->pathdata
.Count
);
1473 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1475 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1480 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1482 TRACE("(%p, %p)\n", region
, matrix
);
1484 if (!region
|| !matrix
)
1485 return InvalidParameter
;
1487 return transform_region_element(®ion
->node
, matrix
);
1490 /* Translates GpRegion elements with specified offsets */
1491 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1495 switch(element
->type
)
1497 case RegionDataEmptyRect
:
1498 case RegionDataInfiniteRect
:
1500 case RegionDataRect
:
1501 element
->elementdata
.rect
.X
+= dx
;
1502 element
->elementdata
.rect
.Y
+= dy
;
1504 case RegionDataPath
:
1505 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1506 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1507 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1511 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1512 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1517 /*****************************************************************************
1518 * GdipTranslateRegion [GDIPLUS.@]
1520 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1522 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1525 return InvalidParameter
;
1527 translate_region_element(®ion
->node
, dx
, dy
);
1532 /*****************************************************************************
1533 * GdipTranslateRegionI [GDIPLUS.@]
1535 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1537 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1539 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1542 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1544 GpRegion
*region_copy
;
1549 stat
= GdipCloneRegion(region
, ®ion_copy
);
1553 stat
= GdipTransformRegion(region_copy
, matrix
);
1556 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1562 data_size
= GetRegionData(hrgn
, 0, NULL
);
1564 *data
= heap_alloc_zero(data_size
);
1567 GetRegionData(hrgn
, data_size
, *data
);
1575 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1577 *data
= heap_alloc_zero(data_size
);
1581 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1582 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1583 (*data
)->rdh
.nCount
= 1;
1584 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1585 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1586 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1588 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1595 GdipDeleteRegion(region_copy
);
1601 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1606 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1608 if (!region
|| !count
|| !matrix
)
1609 return InvalidParameter
;
1611 stat
= get_region_scans_data(region
, matrix
, &data
);
1615 *count
= data
->rdh
.nCount
;
1622 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1629 if (!region
|| !count
|| !matrix
)
1630 return InvalidParameter
;
1632 stat
= get_region_scans_data(region
, matrix
, &data
);
1636 *count
= data
->rdh
.nCount
;
1637 rects
= (RECT
*)data
->Buffer
;
1641 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1643 scans
[i
].X
= rects
[i
].left
;
1644 scans
[i
].Y
= rects
[i
].top
;
1645 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1646 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1656 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1663 if (!region
|| !count
|| !matrix
)
1664 return InvalidParameter
;
1666 stat
= get_region_scans_data(region
, matrix
, &data
);
1670 *count
= data
->rdh
.nCount
;
1671 rects
= (RECT
*)data
->Buffer
;
1675 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1677 scans
[i
].X
= rects
[i
].left
;
1678 scans
[i
].Y
= rects
[i
].top
;
1679 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1680 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;