2 * Copyright (C) 2008 Google (Lei Zhang)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "gdiplus_private.h"
21 /**********************************************************
23 * Data returned by GdipGetRegionData looks something like this:
25 * struct region_data_header
27 * DWORD size; size in bytes of the data - 8.
28 * DWORD magic1; probably a checksum.
29 * DWORD magic2; always seems to be 0xdbc01001 - version?
30 * DWORD num_ops; number of combining ops * 2
33 * Then follows a sequence of combining ops and region elements.
35 * A region element is either a RECTF or some path data.
37 * Combining ops are just stored as their CombineMode value.
39 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
40 * stored as 0x10000002 (with no following RECTF) and an infinite rect
41 * is stored as 0x10000003 (again with no following RECTF).
43 * Path data is preceded by the DWORD 0x10000001. Then follows a
44 * DWORD size and then size bytes of data.
46 * The combining ops are stored in the reverse order to the region
47 * elements and in the reverse order to which the region was
50 * When two or more complex regions (ie those with more than one
51 * element) are combined, the combining op for the two regions comes
52 * first, then the combining ops for the region elements in region 1,
53 * followed by the region elements for region 1, then follows the
54 * combining ops for region 2 and finally region 2's region elements.
55 * Presumably you're supposed to use the 0x1000000x header to find the
56 * end of the op list (the count of the elements in each region is not
59 * When a simple region (1 element) is combined, it's treated as if a
60 * single rect/path is being combined.
64 #define FLAGS_NOFLAGS 0x0
65 #define FLAGS_INTPATH 0x4000
67 /* Header size as far as header->size is concerned. This doesn't include
68 * header->size or header->checksum
70 static const INT sizeheader_size
= sizeof(DWORD
) * 2;
72 typedef struct packed_point
78 /* Test to see if the path could be stored as an array of shorts */
79 static BOOL
is_integer_path(const GpPath
*path
)
83 if (!path
->pathdata
.Count
) return FALSE
;
85 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
88 x
= gdip_round(path
->pathdata
.Points
[i
].X
);
89 y
= gdip_round(path
->pathdata
.Points
[i
].Y
);
90 if (path
->pathdata
.Points
[i
].X
!= (REAL
)x
|| path
->pathdata
.Points
[i
].Y
!= (REAL
)y
)
96 /* Everything is measured in DWORDS; round up if there's a remainder */
97 static inline INT
get_pathtypes_size(const GpPath
* path
)
99 INT needed
= path
->pathdata
.Count
/ sizeof(DWORD
);
101 if (path
->pathdata
.Count
% sizeof(DWORD
) > 0)
104 return needed
* sizeof(DWORD
);
107 static inline INT
get_element_size(const region_element
* element
)
109 INT needed
= sizeof(DWORD
); /* DWORD for the type */
110 switch(element
->type
)
113 return needed
+ sizeof(GpRect
);
116 const GpPath
*path
= element
->elementdata
.path
;
117 DWORD flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
118 /* 3 for headers, once again size doesn't count itself */
119 needed
+= sizeof(DWORD
) * 3;
120 if (flags
& FLAGS_INTPATH
)
121 needed
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
123 needed
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
125 needed
+= get_pathtypes_size(path
);
126 needed
+= sizeof(DWORD
); /* Extra DWORD for pathheader.size */
129 case RegionDataEmptyRect
:
130 case RegionDataInfiniteRect
:
133 needed
+= get_element_size(element
->elementdata
.combine
.left
);
134 needed
+= get_element_size(element
->elementdata
.combine
.right
);
141 /* Does not check parameters, caller must do that */
142 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
144 region
->node
.type
= type
;
145 region
->num_children
= 0;
150 static inline GpStatus
clone_element(const region_element
* element
,
151 region_element
** element2
)
155 /* root node is allocated with GpRegion */
157 *element2
= GdipAlloc(sizeof(region_element
));
162 (*element2
)->type
= element
->type
;
164 switch (element
->type
)
167 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
169 case RegionDataEmptyRect
:
170 case RegionDataInfiniteRect
:
173 stat
= GdipClonePath(element
->elementdata
.path
, &(*element2
)->elementdata
.path
);
174 if (stat
== Ok
) return Ok
;
177 (*element2
)->elementdata
.combine
.left
= NULL
;
178 (*element2
)->elementdata
.combine
.right
= NULL
;
180 stat
= clone_element(element
->elementdata
.combine
.left
,
181 &(*element2
)->elementdata
.combine
.left
);
184 stat
= clone_element(element
->elementdata
.combine
.right
,
185 &(*element2
)->elementdata
.combine
.right
);
186 if (stat
== Ok
) return Ok
;
191 delete_element(*element2
);
196 /* Common code for CombineRegion*
197 * All the caller has to do is get its format into an element
199 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
200 region_element
* right
, const CombineMode mode
)
202 region
->node
.type
= mode
;
203 region
->node
.elementdata
.combine
.left
= left
;
204 region
->node
.elementdata
.combine
.right
= right
;
205 region
->num_children
+= 2;
208 /*****************************************************************************
209 * GdipCloneRegion [GDIPLUS.@]
211 * Creates a deep copy of the region
214 * region [I] source region
215 * clone [O] resulting clone
219 * FAILURE: InvalidParameter or OutOfMemory
221 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
223 region_element
*element
;
225 TRACE("%p %p\n", region
, clone
);
227 if (!(region
&& clone
))
228 return InvalidParameter
;
230 *clone
= GdipAlloc(sizeof(GpRegion
));
233 element
= &(*clone
)->node
;
235 (*clone
)->num_children
= region
->num_children
;
236 return clone_element(®ion
->node
, &element
);
239 /*****************************************************************************
240 * GdipCombineRegionPath [GDIPLUS.@]
242 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
244 GpRegion
*path_region
;
245 region_element
*left
, *right
= NULL
;
248 TRACE("%p %p %d\n", region
, path
, mode
);
250 if (!(region
&& path
))
251 return InvalidParameter
;
253 stat
= GdipCreateRegionPath(path
, &path_region
);
257 /* simply replace region data */
258 if(mode
== CombineModeReplace
){
259 delete_element(®ion
->node
);
260 memcpy(region
, path_region
, sizeof(GpRegion
));
261 GdipFree(path_region
);
265 left
= GdipAlloc(sizeof(region_element
));
268 *left
= region
->node
;
269 stat
= clone_element(&path_region
->node
, &right
);
272 fuse_region(region
, left
, right
, mode
);
273 GdipDeleteRegion(path_region
);
281 GdipDeleteRegion(path_region
);
285 /*****************************************************************************
286 * GdipCombineRegionRect [GDIPLUS.@]
288 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
289 GDIPCONST GpRectF
*rect
, CombineMode mode
)
291 GpRegion
*rect_region
;
292 region_element
*left
, *right
= NULL
;
295 TRACE("%p %s %d\n", region
, debugstr_rectf(rect
), mode
);
297 if (!(region
&& rect
))
298 return InvalidParameter
;
300 stat
= GdipCreateRegionRect(rect
, &rect_region
);
304 /* simply replace region data */
305 if(mode
== CombineModeReplace
){
306 delete_element(®ion
->node
);
307 memcpy(region
, rect_region
, sizeof(GpRegion
));
308 GdipFree(rect_region
);
312 left
= GdipAlloc(sizeof(region_element
));
315 memcpy(left
, ®ion
->node
, sizeof(region_element
));
316 stat
= clone_element(&rect_region
->node
, &right
);
319 fuse_region(region
, left
, right
, mode
);
320 GdipDeleteRegion(rect_region
);
328 GdipDeleteRegion(rect_region
);
332 /*****************************************************************************
333 * GdipCombineRegionRectI [GDIPLUS.@]
335 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
336 GDIPCONST GpRect
*rect
, CombineMode mode
)
340 TRACE("%p %p %d\n", region
, rect
, mode
);
343 return InvalidParameter
;
345 rectf
.X
= (REAL
)rect
->X
;
346 rectf
.Y
= (REAL
)rect
->Y
;
347 rectf
.Height
= (REAL
)rect
->Height
;
348 rectf
.Width
= (REAL
)rect
->Width
;
350 return GdipCombineRegionRect(region
, &rectf
, mode
);
353 /*****************************************************************************
354 * GdipCombineRegionRegion [GDIPLUS.@]
356 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
357 GpRegion
*region2
, CombineMode mode
)
359 region_element
*left
, *right
= NULL
;
363 TRACE("%p %p %d\n", region1
, region2
, mode
);
365 if(!(region1
&& region2
))
366 return InvalidParameter
;
368 /* simply replace region data */
369 if(mode
== CombineModeReplace
){
370 stat
= GdipCloneRegion(region2
, ®2copy
);
371 if(stat
!= Ok
) return stat
;
373 delete_element(®ion1
->node
);
374 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
379 left
= GdipAlloc(sizeof(region_element
));
383 *left
= region1
->node
;
384 stat
= clone_element(®ion2
->node
, &right
);
391 fuse_region(region1
, left
, right
, mode
);
392 region1
->num_children
+= region2
->num_children
;
397 /*****************************************************************************
398 * GdipCreateRegion [GDIPLUS.@]
400 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
402 TRACE("%p\n", region
);
405 return InvalidParameter
;
407 *region
= GdipAlloc(sizeof(GpRegion
));
411 TRACE("=> %p\n", *region
);
413 return init_region(*region
, RegionDataInfiniteRect
);
416 /*****************************************************************************
417 * GdipCreateRegionPath [GDIPLUS.@]
419 * Creates a GpRegion from a GpPath
422 * path [I] path to base the region on
423 * region [O] pointer to the newly allocated region
427 * FAILURE: InvalidParameter
430 * If a path has no floating point points, its points will be stored as shorts
433 * If a path is empty, it is considered to be an INTPATH
435 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
437 region_element
* element
;
440 TRACE("%p, %p\n", path
, region
);
442 if (!(path
&& region
))
443 return InvalidParameter
;
445 *region
= GdipAlloc(sizeof(GpRegion
));
448 stat
= init_region(*region
, RegionDataPath
);
451 GdipDeleteRegion(*region
);
454 element
= &(*region
)->node
;
456 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
459 GdipDeleteRegion(*region
);
466 /*****************************************************************************
467 * GdipCreateRegionRect [GDIPLUS.@]
469 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
474 TRACE("%p, %p\n", rect
, region
);
476 if (!(rect
&& region
))
477 return InvalidParameter
;
479 *region
= GdipAlloc(sizeof(GpRegion
));
480 stat
= init_region(*region
, RegionDataRect
);
483 GdipDeleteRegion(*region
);
487 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
488 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
489 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
490 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
495 /*****************************************************************************
496 * GdipCreateRegionRectI [GDIPLUS.@]
498 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
503 TRACE("%p, %p\n", rect
, region
);
505 rectf
.X
= (REAL
)rect
->X
;
506 rectf
.Y
= (REAL
)rect
->Y
;
507 rectf
.Width
= (REAL
)rect
->Width
;
508 rectf
.Height
= (REAL
)rect
->Height
;
510 return GdipCreateRegionRect(&rectf
, region
);
513 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
515 FIXME("(%p, %d, %p): stub\n", data
, size
, region
);
518 return NotImplemented
;
522 /******************************************************************************
523 * GdipCreateRegionHrgn [GDIPLUS.@]
525 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
535 TRACE("(%p, %p)\n", hrgn
, region
);
537 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
538 return InvalidParameter
;
540 buf
= GdipAlloc(size
);
544 if(!GetRegionData(hrgn
, size
, buf
)){
549 if(buf
->rdh
.nCount
== 0){
550 if((stat
= GdipCreateRegion(&local
)) != Ok
){
554 if((stat
= GdipSetEmpty(local
)) != Ok
){
556 GdipDeleteRegion(local
);
564 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
569 rect
= (LPRECT
)buf
->Buffer
;
570 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
571 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
572 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
574 GdipDeletePath(path
);
580 stat
= GdipCreateRegionPath(path
, region
);
583 GdipDeletePath(path
);
587 /*****************************************************************************
588 * GdipDeleteRegion [GDIPLUS.@]
590 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
592 TRACE("%p\n", region
);
595 return InvalidParameter
;
597 delete_element(®ion
->node
);
603 /*****************************************************************************
604 * GdipGetRegionBounds [GDIPLUS.@]
606 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
612 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
614 if(!region
|| !graphics
|| !rect
)
615 return InvalidParameter
;
617 /* Contrary to MSDN, native ignores the graphics transform. */
618 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
624 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
625 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
626 TRACE("%p => infinite\n", region
);
630 if(GetRgnBox(hrgn
, &r
)){
633 rect
->Width
= r
.right
- r
.left
;
634 rect
->Height
= r
.bottom
- r
.top
;
635 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
638 status
= GenericError
;
645 /*****************************************************************************
646 * GdipGetRegionBoundsI [GDIPLUS.@]
648 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
653 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
656 return InvalidParameter
;
658 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
660 rect
->X
= gdip_round(rectf
.X
);
661 rect
->Y
= gdip_round(rectf
.Y
);
662 rect
->Width
= gdip_round(rectf
.Width
);
663 rect
->Height
= gdip_round(rectf
.Height
);
669 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
671 location
[*offset
] = write
;
675 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
677 ((FLOAT
*)location
)[*offset
] = write
;
681 static inline void write_packed_point(DWORD
* location
, INT
* offset
,
682 const GpPointF
* write
)
684 packed_point
*point
= (packed_point
*)(location
+ *offset
);
685 point
->X
= gdip_round(write
->X
);
686 point
->Y
= gdip_round(write
->Y
);
690 static inline void write_path_types(DWORD
* location
, INT
* offset
,
693 INT rounded_size
= get_pathtypes_size(path
);
695 memcpy(location
+ *offset
, path
->pathdata
.Types
, path
->pathdata
.Count
);
697 /* The unwritten parts of the DWORD (if any) must be cleared */
698 if (rounded_size
- path
->pathdata
.Count
)
699 ZeroMemory(((BYTE
*)location
) + (*offset
* sizeof(DWORD
)) +
700 path
->pathdata
.Count
, rounded_size
- path
->pathdata
.Count
);
701 *offset
+= rounded_size
/ sizeof(DWORD
);
704 static void write_element(const region_element
* element
, DWORD
*buffer
,
707 write_dword(buffer
, filled
, element
->type
);
708 switch (element
->type
)
710 case CombineModeReplace
:
711 case CombineModeIntersect
:
712 case CombineModeUnion
:
714 case CombineModeExclude
:
715 case CombineModeComplement
:
716 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
717 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
720 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
721 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
722 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
723 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
728 const GpPath
* path
= element
->elementdata
.path
;
737 pathheader
= (struct _pathheader
*)(buffer
+ *filled
);
739 pathheader
->flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
740 /* 3 for headers, once again size doesn't count itself */
741 pathheader
->size
= sizeof(DWORD
) * 3;
742 if (pathheader
->flags
& FLAGS_INTPATH
)
743 pathheader
->size
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
745 pathheader
->size
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
746 pathheader
->size
+= get_pathtypes_size(path
);
747 pathheader
->magic
= VERSION_MAGIC
;
748 pathheader
->count
= path
->pathdata
.Count
;
752 switch (pathheader
->flags
& FLAGS_INTPATH
)
755 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
757 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].X
);
758 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].Y
);
762 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
764 write_packed_point(buffer
, filled
,
765 &path
->pathdata
.Points
[i
]);
769 write_path_types(buffer
, filled
, path
);
772 case RegionDataEmptyRect
:
773 case RegionDataInfiniteRect
:
778 /*****************************************************************************
779 * GdipGetRegionData [GDIPLUS.@]
781 * Returns the header, followed by combining ops and region elements.
784 * region [I] region to retrieve from
785 * buffer [O] buffer to hold the resulting data
786 * size [I] size of the buffer
787 * needed [O] (optional) how much data was written
791 * FAILURE: InvalidParameter
794 * The header contains the size, a checksum, a version string, and the number
795 * of children. The size does not count itself or the checksum.
796 * Version is always something like 0xdbc01001 or 0xdbc01002
798 * An element is a RECT, or PATH; Combining ops are stored as their
799 * CombineMode value. Special regions (infinite, empty) emit just their
800 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
801 * their code followed by a second header for the path followed by the actual
802 * path data. Followed by the flags for each point. The pathheader contains
803 * the size of the data to follow, a version number again, followed by a count
804 * of how many points, and any special flags which may apply. 0x4000 means its
805 * a path of shorts instead of FLOAT.
807 * Combining Ops are stored in reverse order from when they were constructed;
808 * the output is a tree where the left side combining area is always taken
811 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
814 struct _region_header
825 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
827 if (!region
|| !buffer
|| !size
)
828 return InvalidParameter
;
830 status
= GdipGetRegionDataSize(region
, &required
);
831 if (status
!= Ok
) return status
;
834 if (needed
) *needed
= size
;
835 return InsufficientBuffer
;
838 region_header
= (struct _region_header
*)buffer
;
839 region_header
->size
= sizeheader_size
+ get_element_size(®ion
->node
);
840 region_header
->checksum
= 0;
841 region_header
->magic
= VERSION_MAGIC
;
842 region_header
->num_children
= region
->num_children
;
844 /* With few exceptions, everything written is DWORD aligned,
845 * so use that as our base */
846 write_element(®ion
->node
, (DWORD
*)buffer
, &filled
);
849 *needed
= filled
* sizeof(DWORD
);
854 /*****************************************************************************
855 * GdipGetRegionDataSize [GDIPLUS.@]
857 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
859 TRACE("%p, %p\n", region
, needed
);
861 if (!(region
&& needed
))
862 return InvalidParameter
;
864 /* header.size doesn't count header.size and header.checksum */
865 *needed
= sizeof(DWORD
) * 2 + sizeheader_size
+ get_element_size(®ion
->node
);
870 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
873 GpGraphics
*new_graphics
=NULL
;
879 new_hdc
= CreateCompatibleDC(0);
883 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
884 graphics
= new_graphics
;
891 else if (!graphics
->hdc
)
893 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
898 save_state
= SaveDC(graphics
->hdc
);
899 EndPath(graphics
->hdc
);
901 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
904 stat
= trace_path(graphics
, path
);
907 *hrgn
= PathToRegion(graphics
->hdc
);
908 stat
= *hrgn
? Ok
: OutOfMemory
;
911 RestoreDC(graphics
->hdc
, save_state
);
916 GdipDeleteGraphics(new_graphics
);
918 graphics
->hdc
= NULL
;
924 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
926 switch (element
->type
)
928 case RegionDataInfiniteRect
:
931 case RegionDataEmptyRect
:
932 *hrgn
= CreateRectRgn(0, 0, 0, 0);
933 return *hrgn
? Ok
: OutOfMemory
;
935 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
940 GpRectF
* rc
= &element
->elementdata
.rect
;
942 stat
= GdipCreatePath(FillModeAlternate
, &path
);
945 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
948 stat
= get_path_hrgn(path
, graphics
, hrgn
);
950 GdipDeletePath(path
);
954 case CombineModeIntersect
:
955 case CombineModeUnion
:
957 case CombineModeExclude
:
958 case CombineModeComplement
:
964 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
973 /* existing region is infinite */
974 switch (element
->type
)
976 case CombineModeIntersect
:
977 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
978 case CombineModeXor
: case CombineModeExclude
:
979 left
= CreateRectRgn(-4194304, -4194304, 4194304, 4194304);
981 case CombineModeUnion
: case CombineModeComplement
:
987 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
997 /* new region is infinite */
998 switch (element
->type
)
1000 case CombineModeIntersect
:
1003 case CombineModeXor
: case CombineModeComplement
:
1004 right
= CreateRectRgn(-4194304, -4194304, 4194304, 4194304);
1006 case CombineModeUnion
: case CombineModeExclude
:
1013 switch (element
->type
)
1015 case CombineModeIntersect
:
1016 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1018 case CombineModeUnion
:
1019 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1021 case CombineModeXor
:
1022 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1024 case CombineModeExclude
:
1025 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1027 case CombineModeComplement
:
1028 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1034 DeleteObject(right
);
1040 return GenericError
;
1047 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1049 return NotImplemented
;
1053 /*****************************************************************************
1054 * GdipGetRegionHRgn [GDIPLUS.@]
1056 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1058 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1060 if (!region
|| !hrgn
)
1061 return InvalidParameter
;
1063 return get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1066 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1071 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1073 if(!region
|| !graphics
|| !res
)
1074 return InvalidParameter
;
1076 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1077 if (status
!= Ok
) return status
;
1079 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1080 TRACE("=> %d\n", *res
);
1085 /*****************************************************************************
1086 * GdipIsEqualRegion [GDIPLUS.@]
1088 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1094 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1096 if(!region
|| !region2
|| !graphics
|| !res
)
1097 return InvalidParameter
;
1099 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1102 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1104 DeleteObject(hrgn1
);
1108 *res
= EqualRgn(hrgn1
, hrgn2
);
1110 /* one of GpRegions is infinite */
1112 *res
= (!hrgn1
&& !hrgn2
);
1114 DeleteObject(hrgn1
);
1115 DeleteObject(hrgn2
);
1120 /*****************************************************************************
1121 * GdipIsInfiniteRegion [GDIPLUS.@]
1123 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1125 /* I think graphics is ignored here */
1126 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1128 if(!region
|| !graphics
|| !res
)
1129 return InvalidParameter
;
1131 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1136 /*****************************************************************************
1137 * GdipIsVisibleRegionRect [GDIPLUS.@]
1139 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1145 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1148 return InvalidParameter
;
1150 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1159 rect
.left
= ceilr(x
);
1160 rect
.top
= ceilr(y
);
1161 rect
.right
= ceilr(x
+ w
);
1162 rect
.bottom
= ceilr(y
+ h
);
1164 *res
= RectInRegion(hrgn
, &rect
);
1171 /*****************************************************************************
1172 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1174 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1176 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1178 return InvalidParameter
;
1180 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1183 /*****************************************************************************
1184 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1186 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1191 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1194 return InvalidParameter
;
1196 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1205 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1212 /*****************************************************************************
1213 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1215 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1217 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1219 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1222 /*****************************************************************************
1223 * GdipSetEmpty [GDIPLUS.@]
1225 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1229 TRACE("%p\n", region
);
1232 return InvalidParameter
;
1234 delete_element(®ion
->node
);
1235 stat
= init_region(region
, RegionDataEmptyRect
);
1240 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1244 TRACE("%p\n", region
);
1247 return InvalidParameter
;
1249 delete_element(®ion
->node
);
1250 stat
= init_region(region
, RegionDataInfiniteRect
);
1255 /* Transforms GpRegion elements with given matrix */
1256 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1260 switch(element
->type
)
1262 case RegionDataEmptyRect
:
1263 case RegionDataInfiniteRect
:
1265 case RegionDataRect
:
1267 /* We can't transform a rectangle, so convert it to a path. */
1268 GpRegion
*new_region
;
1271 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1274 stat
= GdipAddPathRectangle(path
,
1275 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1276 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1279 stat
= GdipCreateRegionPath(path
, &new_region
);
1281 GdipDeletePath(path
);
1286 /* Steal the element from the created region. */
1287 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1288 GdipFree(new_region
);
1293 /* Fall-through to do the actual conversion. */
1294 case RegionDataPath
:
1295 if (!element
->elementdata
.path
->pathdata
.Count
)
1298 stat
= GdipTransformMatrixPoints(matrix
,
1299 element
->elementdata
.path
->pathdata
.Points
,
1300 element
->elementdata
.path
->pathdata
.Count
);
1303 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1305 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1310 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1312 TRACE("(%p, %p)\n", region
, matrix
);
1314 if (!region
|| !matrix
)
1315 return InvalidParameter
;
1317 return transform_region_element(®ion
->node
, matrix
);
1320 /* Translates GpRegion elements with specified offsets */
1321 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1325 switch(element
->type
)
1327 case RegionDataEmptyRect
:
1328 case RegionDataInfiniteRect
:
1330 case RegionDataRect
:
1331 element
->elementdata
.rect
.X
+= dx
;
1332 element
->elementdata
.rect
.Y
+= dy
;
1334 case RegionDataPath
:
1335 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1336 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1337 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1341 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1342 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1347 /*****************************************************************************
1348 * GdipTranslateRegion [GDIPLUS.@]
1350 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1352 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1355 return InvalidParameter
;
1357 translate_region_element(®ion
->node
, dx
, dy
);
1362 /*****************************************************************************
1363 * GdipTranslateRegionI [GDIPLUS.@]
1365 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1367 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1369 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1372 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1374 GpRegion
*region_copy
;
1379 stat
= GdipCloneRegion(region
, ®ion_copy
);
1383 stat
= GdipTransformRegion(region_copy
, matrix
);
1386 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1392 data_size
= GetRegionData(hrgn
, 0, NULL
);
1394 *data
= GdipAlloc(data_size
);
1397 GetRegionData(hrgn
, data_size
, *data
);
1405 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1407 *data
= GdipAlloc(data_size
);
1411 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1412 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1413 (*data
)->rdh
.nCount
= 1;
1414 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1415 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1416 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1418 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1425 GdipDeleteRegion(region_copy
);
1431 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1436 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1438 if (!region
|| !count
|| !matrix
)
1439 return InvalidParameter
;
1441 stat
= get_region_scans_data(region
, matrix
, &data
);
1445 *count
= data
->rdh
.nCount
;
1452 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1459 if (!region
|| !count
|| !matrix
)
1460 return InvalidParameter
;
1462 stat
= get_region_scans_data(region
, matrix
, &data
);
1466 *count
= data
->rdh
.nCount
;
1467 rects
= (RECT
*)data
->Buffer
;
1471 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1473 scans
[i
].X
= rects
[i
].left
;
1474 scans
[i
].Y
= rects
[i
].top
;
1475 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1476 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1486 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1493 if (!region
|| !count
|| !matrix
)
1494 return InvalidParameter
;
1496 stat
= get_region_scans_data(region
, matrix
, &data
);
1500 *count
= data
->rdh
.nCount
;
1501 rects
= (RECT
*)data
->Buffer
;
1505 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1507 scans
[i
].X
= rects
[i
].left
;
1508 scans
[i
].Y
= rects
[i
].top
;
1509 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1510 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;