98e0a6da067c13e0f0739550d7ef298e270daa19
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
20 #include "gdiplus_private.h"
22 /**********************************************************
24 * Data returned by GdipGetRegionData looks something like this:
26 * struct region_data_header
28 * DWORD size; size in bytes of the data - 8.
29 * DWORD magic1; probably a checksum.
30 * DWORD magic2; always seems to be 0xdbc01001 - version?
31 * DWORD num_ops; number of combining ops * 2
34 * Then follows a sequence of combining ops and region elements.
36 * A region element is either a RECTF or some path data.
38 * Combining ops are just stored as their CombineMode value.
40 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
41 * stored as 0x10000002 (with no following RECTF) and an infinite rect
42 * is stored as 0x10000003 (again with no following RECTF).
44 * Path data is preceded by the DWORD 0x10000001. Then follows a
45 * DWORD size and then size bytes of data.
47 * The combining ops are stored in the reverse order to the region
48 * elements and in the reverse order to which the region was
51 * When two or more complex regions (ie those with more than one
52 * element) are combined, the combining op for the two regions comes
53 * first, then the combining ops for the region elements in region 1,
54 * followed by the region elements for region 1, then follows the
55 * combining ops for region 2 and finally region 2's region elements.
56 * Presumably you're supposed to use the 0x1000000x header to find the
57 * end of the op list (the count of the elements in each region is not
60 * When a simple region (1 element) is combined, it's treated as if a
61 * single rect/path is being combined.
65 #define FLAGS_NOFLAGS 0x0
66 #define FLAGS_INTPATH 0x4000
90 /* Header size as far as header->size is concerned. This doesn't include
91 * header->size or header->checksum
93 static const INT sizeheader_size
= sizeof(DWORD
) * 2;
95 typedef struct packed_point
101 /* Test to see if the path could be stored as an array of shorts */
102 static BOOL
is_integer_path(const GpPath
*path
)
106 if (!path
->pathdata
.Count
) return FALSE
;
108 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
111 x
= gdip_round(path
->pathdata
.Points
[i
].X
);
112 y
= gdip_round(path
->pathdata
.Points
[i
].Y
);
113 if (path
->pathdata
.Points
[i
].X
!= (REAL
)x
|| path
->pathdata
.Points
[i
].Y
!= (REAL
)y
)
119 /* Everything is measured in DWORDS; round up if there's a remainder */
120 static inline INT
get_pathtypes_size(const GpPath
* path
)
122 INT needed
= path
->pathdata
.Count
/ sizeof(DWORD
);
124 if (path
->pathdata
.Count
% sizeof(DWORD
) > 0)
127 return needed
* sizeof(DWORD
);
130 static inline INT
get_element_size(const region_element
* element
)
132 INT needed
= sizeof(DWORD
); /* DWORD for the type */
133 switch(element
->type
)
136 return needed
+ sizeof(GpRect
);
139 const GpPath
*path
= element
->elementdata
.path
;
140 DWORD flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
141 /* 3 for headers, once again size doesn't count itself */
142 needed
+= sizeof(DWORD
) * 3;
143 if (flags
& FLAGS_INTPATH
)
144 needed
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
146 needed
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
148 needed
+= get_pathtypes_size(path
);
149 needed
+= sizeof(DWORD
); /* Extra DWORD for pathheader.size */
152 case RegionDataEmptyRect
:
153 case RegionDataInfiniteRect
:
156 needed
+= get_element_size(element
->elementdata
.combine
.left
);
157 needed
+= get_element_size(element
->elementdata
.combine
.right
);
164 /* Does not check parameters, caller must do that */
165 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
167 region
->node
.type
= type
;
168 region
->num_children
= 0;
173 static inline GpStatus
clone_element(const region_element
* element
,
174 region_element
** element2
)
178 /* root node is allocated with GpRegion */
180 *element2
= GdipAlloc(sizeof(region_element
));
185 (*element2
)->type
= element
->type
;
187 switch (element
->type
)
190 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
192 case RegionDataEmptyRect
:
193 case RegionDataInfiniteRect
:
196 stat
= GdipClonePath(element
->elementdata
.path
, &(*element2
)->elementdata
.path
);
197 if (stat
== Ok
) return Ok
;
200 (*element2
)->elementdata
.combine
.left
= NULL
;
201 (*element2
)->elementdata
.combine
.right
= NULL
;
203 stat
= clone_element(element
->elementdata
.combine
.left
,
204 &(*element2
)->elementdata
.combine
.left
);
207 stat
= clone_element(element
->elementdata
.combine
.right
,
208 &(*element2
)->elementdata
.combine
.right
);
209 if (stat
== Ok
) return Ok
;
214 delete_element(*element2
);
219 /* Common code for CombineRegion*
220 * All the caller has to do is get its format into an element
222 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
223 region_element
* right
, const CombineMode mode
)
225 region
->node
.type
= mode
;
226 region
->node
.elementdata
.combine
.left
= left
;
227 region
->node
.elementdata
.combine
.right
= right
;
228 region
->num_children
+= 2;
231 /*****************************************************************************
232 * GdipCloneRegion [GDIPLUS.@]
234 * Creates a deep copy of the region
237 * region [I] source region
238 * clone [O] resulting clone
242 * FAILURE: InvalidParameter or OutOfMemory
244 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
246 region_element
*element
;
248 TRACE("%p %p\n", region
, clone
);
250 if (!(region
&& clone
))
251 return InvalidParameter
;
253 *clone
= GdipAlloc(sizeof(GpRegion
));
256 element
= &(*clone
)->node
;
258 (*clone
)->num_children
= region
->num_children
;
259 return clone_element(®ion
->node
, &element
);
262 /*****************************************************************************
263 * GdipCombineRegionPath [GDIPLUS.@]
265 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
267 GpRegion
*path_region
;
268 region_element
*left
, *right
= NULL
;
271 TRACE("%p %p %d\n", region
, path
, mode
);
273 if (!(region
&& path
))
274 return InvalidParameter
;
276 stat
= GdipCreateRegionPath(path
, &path_region
);
280 /* simply replace region data */
281 if(mode
== CombineModeReplace
){
282 delete_element(®ion
->node
);
283 memcpy(region
, path_region
, sizeof(GpRegion
));
284 GdipFree(path_region
);
288 left
= GdipAlloc(sizeof(region_element
));
291 *left
= region
->node
;
292 stat
= clone_element(&path_region
->node
, &right
);
295 fuse_region(region
, left
, right
, mode
);
296 GdipDeleteRegion(path_region
);
304 GdipDeleteRegion(path_region
);
308 /*****************************************************************************
309 * GdipCombineRegionRect [GDIPLUS.@]
311 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
312 GDIPCONST GpRectF
*rect
, CombineMode mode
)
314 GpRegion
*rect_region
;
315 region_element
*left
, *right
= NULL
;
318 TRACE("%p %s %d\n", region
, debugstr_rectf(rect
), mode
);
320 if (!(region
&& rect
))
321 return InvalidParameter
;
323 stat
= GdipCreateRegionRect(rect
, &rect_region
);
327 /* simply replace region data */
328 if(mode
== CombineModeReplace
){
329 delete_element(®ion
->node
);
330 memcpy(region
, rect_region
, sizeof(GpRegion
));
331 GdipFree(rect_region
);
335 left
= GdipAlloc(sizeof(region_element
));
338 memcpy(left
, ®ion
->node
, sizeof(region_element
));
339 stat
= clone_element(&rect_region
->node
, &right
);
342 fuse_region(region
, left
, right
, mode
);
343 GdipDeleteRegion(rect_region
);
351 GdipDeleteRegion(rect_region
);
355 /*****************************************************************************
356 * GdipCombineRegionRectI [GDIPLUS.@]
358 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
359 GDIPCONST GpRect
*rect
, CombineMode mode
)
363 TRACE("%p %p %d\n", region
, rect
, mode
);
366 return InvalidParameter
;
368 rectf
.X
= (REAL
)rect
->X
;
369 rectf
.Y
= (REAL
)rect
->Y
;
370 rectf
.Height
= (REAL
)rect
->Height
;
371 rectf
.Width
= (REAL
)rect
->Width
;
373 return GdipCombineRegionRect(region
, &rectf
, mode
);
376 /*****************************************************************************
377 * GdipCombineRegionRegion [GDIPLUS.@]
379 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
380 GpRegion
*region2
, CombineMode mode
)
382 region_element
*left
, *right
= NULL
;
386 TRACE("%p %p %d\n", region1
, region2
, mode
);
388 if(!(region1
&& region2
))
389 return InvalidParameter
;
391 /* simply replace region data */
392 if(mode
== CombineModeReplace
){
393 stat
= GdipCloneRegion(region2
, ®2copy
);
394 if(stat
!= Ok
) return stat
;
396 delete_element(®ion1
->node
);
397 memcpy(region1
, reg2copy
, sizeof(GpRegion
));
402 left
= GdipAlloc(sizeof(region_element
));
406 *left
= region1
->node
;
407 stat
= clone_element(®ion2
->node
, &right
);
414 fuse_region(region1
, left
, right
, mode
);
415 region1
->num_children
+= region2
->num_children
;
420 /*****************************************************************************
421 * GdipCreateRegion [GDIPLUS.@]
423 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
425 TRACE("%p\n", region
);
428 return InvalidParameter
;
430 *region
= GdipAlloc(sizeof(GpRegion
));
434 TRACE("=> %p\n", *region
);
436 return init_region(*region
, RegionDataInfiniteRect
);
439 /*****************************************************************************
440 * GdipCreateRegionPath [GDIPLUS.@]
442 * Creates a GpRegion from a GpPath
445 * path [I] path to base the region on
446 * region [O] pointer to the newly allocated region
450 * FAILURE: InvalidParameter
453 * If a path has no floating point points, its points will be stored as shorts
456 * If a path is empty, it is considered to be an INTPATH
458 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
460 region_element
* element
;
463 TRACE("%p, %p\n", path
, region
);
465 if (!(path
&& region
))
466 return InvalidParameter
;
468 *region
= GdipAlloc(sizeof(GpRegion
));
471 stat
= init_region(*region
, RegionDataPath
);
474 GdipDeleteRegion(*region
);
477 element
= &(*region
)->node
;
479 stat
= GdipClonePath(path
, &element
->elementdata
.path
);
482 GdipDeleteRegion(*region
);
489 /*****************************************************************************
490 * GdipCreateRegionRect [GDIPLUS.@]
492 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
497 TRACE("%p, %p\n", rect
, region
);
499 if (!(rect
&& region
))
500 return InvalidParameter
;
502 *region
= GdipAlloc(sizeof(GpRegion
));
503 stat
= init_region(*region
, RegionDataRect
);
506 GdipDeleteRegion(*region
);
510 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
511 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
512 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
513 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
518 /*****************************************************************************
519 * GdipCreateRegionRectI [GDIPLUS.@]
521 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
526 TRACE("%p, %p\n", rect
, region
);
528 rectf
.X
= (REAL
)rect
->X
;
529 rectf
.Y
= (REAL
)rect
->Y
;
530 rectf
.Width
= (REAL
)rect
->Width
;
531 rectf
.Height
= (REAL
)rect
->Height
;
533 return GdipCreateRegionRect(&rectf
, region
);
536 static inline void init_memory_buffer(struct memory_buffer
*mbuf
, const BYTE
*buffer
, INT size
)
538 mbuf
->buffer
= buffer
;
543 static inline const void *buffer_read(struct memory_buffer
*mbuf
, INT size
)
545 if (mbuf
->size
- mbuf
->pos
>= size
)
547 const void *data
= mbuf
->buffer
+ mbuf
->pos
;
554 static GpStatus
read_element(struct memory_buffer
*mbuf
, GpRegion
*region
, region_element
*node
, INT
*count
)
559 type
= buffer_read(mbuf
, sizeof(DWORD
));
560 if (!type
) return Ok
;
562 TRACE("type %#x\n", *type
);
568 case CombineModeReplace
:
569 case CombineModeIntersect
:
570 case CombineModeUnion
:
572 case CombineModeExclude
:
573 case CombineModeComplement
:
575 region_element
*left
, *right
;
577 left
= GdipAlloc(sizeof(region_element
));
578 if (!left
) return OutOfMemory
;
579 right
= GdipAlloc(sizeof(region_element
));
586 status
= read_element(mbuf
, region
, left
, count
);
589 status
= read_element(mbuf
, region
, right
, count
);
592 node
->elementdata
.combine
.left
= left
;
593 node
->elementdata
.combine
.right
= right
;
594 region
->num_children
+= 2;
608 rc
= buffer_read(mbuf
, sizeof(GpRectF
));
611 ERR("failed to read rect data\n");
612 return InvalidParameter
;
615 node
->elementdata
.rect
= *rc
;
623 const struct path_header
*path_header
;
626 path_header
= buffer_read(mbuf
, sizeof(struct path_header
));
629 ERR("failed to read path header\n");
630 return InvalidParameter
;
632 if (path_header
->magic
!= VERSION_MAGIC
)
634 ERR("invalid path header magic %#x\n", path_header
->magic
);
635 return InvalidParameter
;
638 /* Windows always fails to create an empty path in a region */
639 if (!path_header
->count
)
641 TRACE("refusing to create an empty path in a region\n");
645 status
= GdipCreatePath(FillModeAlternate
, &path
);
646 if (status
) return status
;
648 node
->elementdata
.path
= path
;
650 if (!lengthen_path(path
, path_header
->count
))
653 path
->pathdata
.Count
= path_header
->count
;
655 if (path_header
->flags
& ~FLAGS_INTPATH
)
656 FIXME("unhandled path flags %#x\n", path_header
->flags
);
658 if (path_header
->flags
& FLAGS_INTPATH
)
660 const packed_point
*pt
;
663 pt
= buffer_read(mbuf
, sizeof(packed_point
) * path_header
->count
);
666 ERR("failed to read packed %u path points\n", path_header
->count
);
667 return InvalidParameter
;
670 for (i
= 0; i
< path_header
->count
; i
++)
672 path
->pathdata
.Points
[i
].X
= (REAL
)pt
[i
].X
;
673 path
->pathdata
.Points
[i
].Y
= (REAL
)pt
[i
].Y
;
680 ptf
= buffer_read(mbuf
, sizeof(GpPointF
) * path_header
->count
);
683 ERR("failed to read %u path points\n", path_header
->count
);
684 return InvalidParameter
;
686 memcpy(path
->pathdata
.Points
, ptf
, sizeof(GpPointF
) * path_header
->count
);
689 types
= buffer_read(mbuf
, path_header
->count
);
692 ERR("failed to read %u path types\n", path_header
->count
);
693 return InvalidParameter
;
695 memcpy(path
->pathdata
.Types
, types
, path_header
->count
);
696 if (path_header
->count
& 3)
698 if (!buffer_read(mbuf
, 4 - (path_header
->count
& 3)))
700 ERR("failed to read rounding %u bytes\n", 4 - (path_header
->count
& 3));
701 return InvalidParameter
;
709 case RegionDataEmptyRect
:
710 case RegionDataInfiniteRect
:
715 FIXME("element type %#x is not supported\n", *type
);
719 return InvalidParameter
;
722 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
725 struct memory_buffer mbuf
;
726 const struct region_header
*region_header
;
729 if (!data
|| !size
) return InvalidParameter
;
731 TRACE("%p, %d, %p\n", data
, size
, region
);
733 init_memory_buffer(&mbuf
, data
, size
);
735 region_header
= buffer_read(&mbuf
, sizeof(struct region_header
));
736 if (!region_header
|| region_header
->magic
!= VERSION_MAGIC
)
737 return InvalidParameter
;
739 status
= GdipCreateRegion(region
);
740 if (status
!= Ok
) return status
;
743 status
= read_element(&mbuf
, *region
, &(*region
)->node
, &count
);
744 if (status
== Ok
&& !count
)
745 status
= InvalidParameter
;
748 GdipDeleteRegion(*region
);
754 /******************************************************************************
755 * GdipCreateRegionHrgn [GDIPLUS.@]
757 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
767 TRACE("(%p, %p)\n", hrgn
, region
);
769 if(!region
|| !(size
= GetRegionData(hrgn
, 0, NULL
)))
770 return InvalidParameter
;
772 buf
= GdipAlloc(size
);
776 if(!GetRegionData(hrgn
, size
, buf
)){
781 if(buf
->rdh
.nCount
== 0){
782 if((stat
= GdipCreateRegion(&local
)) != Ok
){
786 if((stat
= GdipSetEmpty(local
)) != Ok
){
788 GdipDeleteRegion(local
);
796 if((stat
= GdipCreatePath(FillModeAlternate
, &path
)) != Ok
){
801 rect
= (LPRECT
)buf
->Buffer
;
802 for(i
= 0; i
< buf
->rdh
.nCount
; i
++){
803 if((stat
= GdipAddPathRectangle(path
, (REAL
)rect
->left
, (REAL
)rect
->top
,
804 (REAL
)(rect
->right
- rect
->left
), (REAL
)(rect
->bottom
- rect
->top
))) != Ok
){
806 GdipDeletePath(path
);
812 stat
= GdipCreateRegionPath(path
, region
);
815 GdipDeletePath(path
);
819 /*****************************************************************************
820 * GdipDeleteRegion [GDIPLUS.@]
822 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
824 TRACE("%p\n", region
);
827 return InvalidParameter
;
829 delete_element(®ion
->node
);
835 /*****************************************************************************
836 * GdipGetRegionBounds [GDIPLUS.@]
838 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
844 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
846 if(!region
|| !graphics
|| !rect
)
847 return InvalidParameter
;
849 /* Contrary to MSDN, native ignores the graphics transform. */
850 status
= GdipGetRegionHRgn(region
, NULL
, &hrgn
);
856 rect
->X
= rect
->Y
= -(REAL
)(1 << 22);
857 rect
->Width
= rect
->Height
= (REAL
)(1 << 23);
858 TRACE("%p => infinite\n", region
);
862 if(GetRgnBox(hrgn
, &r
)){
865 rect
->Width
= r
.right
- r
.left
;
866 rect
->Height
= r
.bottom
- r
.top
;
867 TRACE("%p => %s\n", region
, debugstr_rectf(rect
));
870 status
= GenericError
;
877 /*****************************************************************************
878 * GdipGetRegionBoundsI [GDIPLUS.@]
880 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
885 TRACE("(%p, %p, %p)\n", region
, graphics
, rect
);
888 return InvalidParameter
;
890 status
= GdipGetRegionBounds(region
, graphics
, &rectf
);
892 rect
->X
= gdip_round(rectf
.X
);
893 rect
->Y
= gdip_round(rectf
.Y
);
894 rect
->Width
= gdip_round(rectf
.Width
);
895 rect
->Height
= gdip_round(rectf
.Height
);
901 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
903 location
[*offset
] = write
;
907 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
909 ((FLOAT
*)location
)[*offset
] = write
;
913 static inline void write_packed_point(DWORD
* location
, INT
* offset
,
914 const GpPointF
* write
)
916 packed_point
*point
= (packed_point
*)(location
+ *offset
);
917 point
->X
= gdip_round(write
->X
);
918 point
->Y
= gdip_round(write
->Y
);
922 static inline void write_path_types(DWORD
* location
, INT
* offset
,
925 INT rounded_size
= get_pathtypes_size(path
);
927 memcpy(location
+ *offset
, path
->pathdata
.Types
, path
->pathdata
.Count
);
929 /* The unwritten parts of the DWORD (if any) must be cleared */
930 if (rounded_size
- path
->pathdata
.Count
)
931 ZeroMemory(((BYTE
*)location
) + (*offset
* sizeof(DWORD
)) +
932 path
->pathdata
.Count
, rounded_size
- path
->pathdata
.Count
);
933 *offset
+= rounded_size
/ sizeof(DWORD
);
936 static void write_element(const region_element
* element
, DWORD
*buffer
,
939 write_dword(buffer
, filled
, element
->type
);
940 switch (element
->type
)
942 case CombineModeReplace
:
943 case CombineModeIntersect
:
944 case CombineModeUnion
:
946 case CombineModeExclude
:
947 case CombineModeComplement
:
948 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
949 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
952 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
953 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
954 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
955 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
960 const GpPath
* path
= element
->elementdata
.path
;
961 struct path_header
*pathheader
;
963 pathheader
= (struct path_header
*)(buffer
+ *filled
);
965 pathheader
->flags
= is_integer_path(path
) ? FLAGS_INTPATH
: FLAGS_NOFLAGS
;
966 /* 3 for headers, once again size doesn't count itself */
967 pathheader
->size
= sizeof(DWORD
) * 3;
968 if (pathheader
->flags
& FLAGS_INTPATH
)
969 pathheader
->size
+= 2 * sizeof(SHORT
) * path
->pathdata
.Count
;
971 pathheader
->size
+= 2 * sizeof(FLOAT
) * path
->pathdata
.Count
;
972 pathheader
->size
+= get_pathtypes_size(path
);
973 pathheader
->magic
= VERSION_MAGIC
;
974 pathheader
->count
= path
->pathdata
.Count
;
978 switch (pathheader
->flags
& FLAGS_INTPATH
)
981 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
983 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].X
);
984 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].Y
);
988 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
990 write_packed_point(buffer
, filled
,
991 &path
->pathdata
.Points
[i
]);
995 write_path_types(buffer
, filled
, path
);
998 case RegionDataEmptyRect
:
999 case RegionDataInfiniteRect
:
1004 /*****************************************************************************
1005 * GdipGetRegionData [GDIPLUS.@]
1007 * Returns the header, followed by combining ops and region elements.
1010 * region [I] region to retrieve from
1011 * buffer [O] buffer to hold the resulting data
1012 * size [I] size of the buffer
1013 * needed [O] (optional) how much data was written
1017 * FAILURE: InvalidParameter
1020 * The header contains the size, a checksum, a version string, and the number
1021 * of children. The size does not count itself or the checksum.
1022 * Version is always something like 0xdbc01001 or 0xdbc01002
1024 * An element is a RECT, or PATH; Combining ops are stored as their
1025 * CombineMode value. Special regions (infinite, empty) emit just their
1026 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
1027 * their code followed by a second header for the path followed by the actual
1028 * path data. Followed by the flags for each point. The pathheader contains
1029 * the size of the data to follow, a version number again, followed by a count
1030 * of how many points, and any special flags which may apply. 0x4000 means its
1031 * a path of shorts instead of FLOAT.
1033 * Combining Ops are stored in reverse order from when they were constructed;
1034 * the output is a tree where the left side combining area is always taken
1037 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
1040 struct region_header
*region_header
;
1045 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
1047 if (!region
|| !buffer
|| !size
)
1048 return InvalidParameter
;
1050 status
= GdipGetRegionDataSize(region
, &required
);
1051 if (status
!= Ok
) return status
;
1052 if (size
< required
)
1054 if (needed
) *needed
= size
;
1055 return InsufficientBuffer
;
1058 region_header
= (struct region_header
*)buffer
;
1059 region_header
->size
= sizeheader_size
+ get_element_size(®ion
->node
);
1060 region_header
->checksum
= 0;
1061 region_header
->magic
= VERSION_MAGIC
;
1062 region_header
->num_children
= region
->num_children
;
1064 /* With few exceptions, everything written is DWORD aligned,
1065 * so use that as our base */
1066 write_element(®ion
->node
, (DWORD
*)buffer
, &filled
);
1069 *needed
= filled
* sizeof(DWORD
);
1074 /*****************************************************************************
1075 * GdipGetRegionDataSize [GDIPLUS.@]
1077 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
1079 TRACE("%p, %p\n", region
, needed
);
1081 if (!(region
&& needed
))
1082 return InvalidParameter
;
1084 /* header.size doesn't count header.size and header.checksum */
1085 *needed
= sizeof(DWORD
) * 2 + sizeheader_size
+ get_element_size(®ion
->node
);
1090 static GpStatus
get_path_hrgn(GpPath
*path
, GpGraphics
*graphics
, HRGN
*hrgn
)
1093 GpGraphics
*new_graphics
=NULL
;
1099 new_hdc
= CreateCompatibleDC(0);
1102 ERR("CreateCompatibleDC failed\n");
1106 stat
= GdipCreateFromHDC(new_hdc
, &new_graphics
);
1107 graphics
= new_graphics
;
1110 ERR("GdipCreateFromHDC failed: 0x%x\n", stat
);
1115 else if (!graphics
->hdc
)
1117 graphics
->hdc
= new_hdc
= CreateCompatibleDC(0);
1120 ERR("CreateCompatibleDC failed\n");
1125 save_state
= SaveDC(graphics
->hdc
);
1126 EndPath(graphics
->hdc
);
1128 SetPolyFillMode(graphics
->hdc
, (path
->fill
== FillModeAlternate
? ALTERNATE
1131 stat
= trace_path(graphics
, path
);
1134 *hrgn
= PathToRegion(graphics
->hdc
);
1137 ERR("PathToRegion failed\n");
1139 stat
= *hrgn
? Ok
: OutOfMemory
;
1143 ERR("trace_path failed: 0x%x\n", stat
);
1146 RestoreDC(graphics
->hdc
, save_state
);
1151 GdipDeleteGraphics(new_graphics
);
1153 graphics
->hdc
= NULL
;
1159 static GpStatus
get_region_hrgn(struct region_element
*element
, GpGraphics
*graphics
, HRGN
*hrgn
)
1161 switch (element
->type
)
1163 case RegionDataInfiniteRect
:
1166 case RegionDataEmptyRect
:
1167 *hrgn
= CreateRectRgn(0, 0, 0, 0);
1168 return *hrgn
? Ok
: OutOfMemory
;
1169 case RegionDataPath
:
1170 return get_path_hrgn(element
->elementdata
.path
, graphics
, hrgn
);
1171 case RegionDataRect
:
1175 GpRectF
* rc
= &element
->elementdata
.rect
;
1177 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1180 ERR("GdipCreatePath failed: 0x%x\n", stat
);
1183 stat
= GdipAddPathRectangle(path
, rc
->X
, rc
->Y
, rc
->Width
, rc
->Height
);
1186 stat
= get_path_hrgn(path
, graphics
, hrgn
);
1189 ERR("GdipAddPathRectangle failed: 0x%x\n", stat
);
1192 GdipDeletePath(path
);
1196 case CombineModeIntersect
:
1197 case CombineModeUnion
:
1198 case CombineModeXor
:
1199 case CombineModeExclude
:
1200 case CombineModeComplement
:
1206 stat
= get_region_hrgn(element
->elementdata
.combine
.left
, graphics
, &left
);
1215 /* existing region is infinite */
1216 switch (element
->type
)
1218 case CombineModeIntersect
:
1219 return get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, hrgn
);
1220 case CombineModeXor
: case CombineModeExclude
:
1221 left
= CreateRectRgn(-4194304, -4194304, 4194304, 4194304);
1223 case CombineModeUnion
: case CombineModeComplement
:
1229 stat
= get_region_hrgn(element
->elementdata
.combine
.right
, graphics
, &right
);
1239 /* new region is infinite */
1240 switch (element
->type
)
1242 case CombineModeIntersect
:
1245 case CombineModeXor
: case CombineModeComplement
:
1246 right
= CreateRectRgn(-4194304, -4194304, 4194304, 4194304);
1248 case CombineModeUnion
: case CombineModeExclude
:
1255 switch (element
->type
)
1257 case CombineModeIntersect
:
1258 ret
= CombineRgn(left
, left
, right
, RGN_AND
);
1260 case CombineModeUnion
:
1261 ret
= CombineRgn(left
, left
, right
, RGN_OR
);
1263 case CombineModeXor
:
1264 ret
= CombineRgn(left
, left
, right
, RGN_XOR
);
1266 case CombineModeExclude
:
1267 ret
= CombineRgn(left
, left
, right
, RGN_DIFF
);
1269 case CombineModeComplement
:
1270 ret
= CombineRgn(left
, right
, left
, RGN_DIFF
);
1276 DeleteObject(right
);
1282 return GenericError
;
1289 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element
->type
);
1291 return NotImplemented
;
1295 /*****************************************************************************
1296 * GdipGetRegionHRgn [GDIPLUS.@]
1298 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
1301 TRACE("(%p, %p, %p)\n", region
, graphics
, hrgn
);
1303 if (!region
|| !hrgn
)
1304 return InvalidParameter
;
1306 status
= get_region_hrgn(®ion
->node
, graphics
, hrgn
);
1309 ERR("get_region_hrgn() failed. region->node.type = 0x%x\n", region
->node
.type
);
1314 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1319 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1321 if(!region
|| !graphics
|| !res
)
1322 return InvalidParameter
;
1324 status
= GdipGetRegionBounds(region
, graphics
, &rect
);
1325 if (status
!= Ok
) return status
;
1327 *res
= rect
.Width
== 0.0 && rect
.Height
== 0.0;
1328 TRACE("=> %d\n", *res
);
1333 /*****************************************************************************
1334 * GdipIsEqualRegion [GDIPLUS.@]
1336 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
1342 TRACE("(%p, %p, %p, %p)\n", region
, region2
, graphics
, res
);
1344 if(!region
|| !region2
|| !graphics
|| !res
)
1345 return InvalidParameter
;
1347 stat
= GdipGetRegionHRgn(region
, graphics
, &hrgn1
);
1350 stat
= GdipGetRegionHRgn(region2
, graphics
, &hrgn2
);
1352 DeleteObject(hrgn1
);
1356 *res
= EqualRgn(hrgn1
, hrgn2
);
1358 /* one of GpRegions is infinite */
1360 *res
= (!hrgn1
&& !hrgn2
);
1362 DeleteObject(hrgn1
);
1363 DeleteObject(hrgn2
);
1368 /*****************************************************************************
1369 * GdipIsInfiniteRegion [GDIPLUS.@]
1371 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
1373 /* I think graphics is ignored here */
1374 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
1376 if(!region
|| !graphics
|| !res
)
1377 return InvalidParameter
;
1379 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
1384 /*****************************************************************************
1385 * GdipIsVisibleRegionRect [GDIPLUS.@]
1387 GpStatus WINGDIPAPI
GdipIsVisibleRegionRect(GpRegion
* region
, REAL x
, REAL y
, REAL w
, REAL h
, GpGraphics
*graphics
, BOOL
*res
)
1393 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1396 return InvalidParameter
;
1398 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1407 rect
.left
= ceilr(x
);
1408 rect
.top
= ceilr(y
);
1409 rect
.right
= ceilr(x
+ w
);
1410 rect
.bottom
= ceilr(y
+ h
);
1412 *res
= RectInRegion(hrgn
, &rect
);
1419 /*****************************************************************************
1420 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1422 GpStatus WINGDIPAPI
GdipIsVisibleRegionRectI(GpRegion
* region
, INT x
, INT y
, INT w
, INT h
, GpGraphics
*graphics
, BOOL
*res
)
1424 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region
, x
, y
, w
, h
, graphics
, res
);
1426 return InvalidParameter
;
1428 return GdipIsVisibleRegionRect(region
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
, graphics
, res
);
1431 /*****************************************************************************
1432 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1434 GpStatus WINGDIPAPI
GdipIsVisibleRegionPoint(GpRegion
* region
, REAL x
, REAL y
, GpGraphics
*graphics
, BOOL
*res
)
1439 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region
, x
, y
, graphics
, res
);
1442 return InvalidParameter
;
1444 if((stat
= GdipGetRegionHRgn(region
, NULL
, &hrgn
)) != Ok
)
1453 *res
= PtInRegion(hrgn
, gdip_round(x
), gdip_round(y
));
1460 /*****************************************************************************
1461 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1463 GpStatus WINGDIPAPI
GdipIsVisibleRegionPointI(GpRegion
* region
, INT x
, INT y
, GpGraphics
*graphics
, BOOL
*res
)
1465 TRACE("(%p, %d, %d, %p, %p)\n", region
, x
, y
, graphics
, res
);
1467 return GdipIsVisibleRegionPoint(region
, (REAL
)x
, (REAL
)y
, graphics
, res
);
1470 /*****************************************************************************
1471 * GdipSetEmpty [GDIPLUS.@]
1473 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
1477 TRACE("%p\n", region
);
1480 return InvalidParameter
;
1482 delete_element(®ion
->node
);
1483 stat
= init_region(region
, RegionDataEmptyRect
);
1488 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
1492 TRACE("%p\n", region
);
1495 return InvalidParameter
;
1497 delete_element(®ion
->node
);
1498 stat
= init_region(region
, RegionDataInfiniteRect
);
1503 /* Transforms GpRegion elements with given matrix */
1504 static GpStatus
transform_region_element(region_element
* element
, GpMatrix
*matrix
)
1508 switch(element
->type
)
1510 case RegionDataEmptyRect
:
1511 case RegionDataInfiniteRect
:
1513 case RegionDataRect
:
1515 /* We can't transform a rectangle, so convert it to a path. */
1516 GpRegion
*new_region
;
1519 stat
= GdipCreatePath(FillModeAlternate
, &path
);
1522 stat
= GdipAddPathRectangle(path
,
1523 element
->elementdata
.rect
.X
, element
->elementdata
.rect
.Y
,
1524 element
->elementdata
.rect
.Width
, element
->elementdata
.rect
.Height
);
1527 stat
= GdipCreateRegionPath(path
, &new_region
);
1529 GdipDeletePath(path
);
1534 /* Steal the element from the created region. */
1535 memcpy(element
, &new_region
->node
, sizeof(region_element
));
1536 GdipFree(new_region
);
1541 /* Fall-through to do the actual conversion. */
1542 case RegionDataPath
:
1543 if (!element
->elementdata
.path
->pathdata
.Count
)
1546 stat
= GdipTransformMatrixPoints(matrix
,
1547 element
->elementdata
.path
->pathdata
.Points
,
1548 element
->elementdata
.path
->pathdata
.Count
);
1551 stat
= transform_region_element(element
->elementdata
.combine
.left
, matrix
);
1553 stat
= transform_region_element(element
->elementdata
.combine
.right
, matrix
);
1558 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
1560 TRACE("(%p, %p)\n", region
, matrix
);
1562 if (!region
|| !matrix
)
1563 return InvalidParameter
;
1565 return transform_region_element(®ion
->node
, matrix
);
1568 /* Translates GpRegion elements with specified offsets */
1569 static void translate_region_element(region_element
* element
, REAL dx
, REAL dy
)
1573 switch(element
->type
)
1575 case RegionDataEmptyRect
:
1576 case RegionDataInfiniteRect
:
1578 case RegionDataRect
:
1579 element
->elementdata
.rect
.X
+= dx
;
1580 element
->elementdata
.rect
.Y
+= dy
;
1582 case RegionDataPath
:
1583 for(i
= 0; i
< element
->elementdata
.path
->pathdata
.Count
; i
++){
1584 element
->elementdata
.path
->pathdata
.Points
[i
].X
+= dx
;
1585 element
->elementdata
.path
->pathdata
.Points
[i
].Y
+= dy
;
1589 translate_region_element(element
->elementdata
.combine
.left
, dx
, dy
);
1590 translate_region_element(element
->elementdata
.combine
.right
, dx
, dy
);
1595 /*****************************************************************************
1596 * GdipTranslateRegion [GDIPLUS.@]
1598 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
1600 TRACE("(%p, %f, %f)\n", region
, dx
, dy
);
1603 return InvalidParameter
;
1605 translate_region_element(®ion
->node
, dx
, dy
);
1610 /*****************************************************************************
1611 * GdipTranslateRegionI [GDIPLUS.@]
1613 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
1615 TRACE("(%p, %d, %d)\n", region
, dx
, dy
);
1617 return GdipTranslateRegion(region
, (REAL
)dx
, (REAL
)dy
);
1620 static GpStatus
get_region_scans_data(GpRegion
*region
, GpMatrix
*matrix
, LPRGNDATA
*data
)
1622 GpRegion
*region_copy
;
1627 stat
= GdipCloneRegion(region
, ®ion_copy
);
1631 stat
= GdipTransformRegion(region_copy
, matrix
);
1634 stat
= GdipGetRegionHRgn(region_copy
, NULL
, &hrgn
);
1640 data_size
= GetRegionData(hrgn
, 0, NULL
);
1642 *data
= GdipAlloc(data_size
);
1645 GetRegionData(hrgn
, data_size
, *data
);
1653 data_size
= sizeof(RGNDATAHEADER
) + sizeof(RECT
);
1655 *data
= GdipAlloc(data_size
);
1659 (*data
)->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
1660 (*data
)->rdh
.iType
= RDH_RECTANGLES
;
1661 (*data
)->rdh
.nCount
= 1;
1662 (*data
)->rdh
.nRgnSize
= sizeof(RECT
);
1663 (*data
)->rdh
.rcBound
.left
= (*data
)->rdh
.rcBound
.top
= -0x400000;
1664 (*data
)->rdh
.rcBound
.right
= (*data
)->rdh
.rcBound
.bottom
= 0x400000;
1666 memcpy((*data
)->Buffer
, &(*data
)->rdh
.rcBound
, sizeof(RECT
));
1673 GdipDeleteRegion(region_copy
);
1679 GpStatus WINGDIPAPI
GdipGetRegionScansCount(GpRegion
*region
, UINT
*count
, GpMatrix
*matrix
)
1684 TRACE("(%p, %p, %p)\n", region
, count
, matrix
);
1686 if (!region
|| !count
|| !matrix
)
1687 return InvalidParameter
;
1689 stat
= get_region_scans_data(region
, matrix
, &data
);
1693 *count
= data
->rdh
.nCount
;
1700 GpStatus WINGDIPAPI
GdipGetRegionScansI(GpRegion
*region
, GpRect
*scans
, INT
*count
, GpMatrix
*matrix
)
1707 if (!region
|| !count
|| !matrix
)
1708 return InvalidParameter
;
1710 stat
= get_region_scans_data(region
, matrix
, &data
);
1714 *count
= data
->rdh
.nCount
;
1715 rects
= (RECT
*)data
->Buffer
;
1719 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1721 scans
[i
].X
= rects
[i
].left
;
1722 scans
[i
].Y
= rects
[i
].top
;
1723 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1724 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;
1734 GpStatus WINGDIPAPI
GdipGetRegionScans(GpRegion
*region
, GpRectF
*scans
, INT
*count
, GpMatrix
*matrix
)
1741 if (!region
|| !count
|| !matrix
)
1742 return InvalidParameter
;
1744 stat
= get_region_scans_data(region
, matrix
, &data
);
1748 *count
= data
->rdh
.nCount
;
1749 rects
= (RECT
*)data
->Buffer
;
1753 for (i
=0; i
<data
->rdh
.nCount
; i
++)
1755 scans
[i
].X
= rects
[i
].left
;
1756 scans
[i
].Y
= rects
[i
].top
;
1757 scans
[i
].Width
= rects
[i
].right
- rects
[i
].left
;
1758 scans
[i
].Height
= rects
[i
].bottom
- rects
[i
].top
;