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
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
33 /**********************************************************
35 * Data returned by GdipGetRegionData looks something like this:
37 * struct region_data_header
39 * DWORD size; size in bytes of the data - 8.
40 * DWORD magic1; probably a checksum.
41 * DWORD magic2; always seems to be 0xdbc01001 - version?
42 * DWORD num_ops; number of combining ops * 2
45 * Then follows a sequence of combining ops and region elements.
47 * A region element is either a RECTF or some path data.
49 * Combining ops are just stored as their CombineMode value.
51 * Each RECTF is preceded by the DWORD 0x10000000. An empty rect is
52 * stored as 0x10000002 (with no following RECTF) and an infinite rect
53 * is stored as 0x10000003 (again with no following RECTF).
55 * Path data is preceded by the DWORD 0x10000001. Then follows a
56 * DWORD size and then size bytes of data.
58 * The combining ops are stored in the reverse order to the region
59 * elements and in the reverse order to which the region was
62 * When two or more complex regions (ie those with more than one
63 * element) are combined, the combining op for the two regions comes
64 * first, then the combining ops for the region elements in region 1,
65 * followed by the region elements for region 1, then follows the
66 * combining ops for region 2 and finally region 2's region elements.
67 * Presumably you're supposed to use the 0x1000000x header to find the
68 * end of the op list (the count of the elements in each region is not
71 * When a simple region (1 element) is combined, it's treated as if a
72 * single rect/path is being combined.
76 #define FLAGS_NOFLAGS 0x0
77 #define FLAGS_INTPATH 0x4000
79 /* Header size as far as header->size is concerned. This doesn't include
80 * header->size or header->checksum
82 static const INT sizeheader_size
= sizeof(DWORD
) * 2;
84 typedef struct packed_point
90 /* Everything is measured in DWORDS; round up if there's a remainder */
91 static inline INT
get_pathtypes_size(const GpPath
* path
)
93 INT needed
= path
->pathdata
.Count
/ sizeof(DWORD
);
95 if (path
->pathdata
.Count
% sizeof(DWORD
) > 0)
98 return needed
* sizeof(DWORD
);
101 static inline INT
get_element_size(const region_element
* element
)
103 INT needed
= sizeof(DWORD
); /* DWORD for the type */
104 switch(element
->type
)
107 return needed
+ sizeof(GpRect
);
109 needed
+= element
->elementdata
.pathdata
.pathheader
.size
;
110 needed
+= sizeof(DWORD
); /* Extra DWORD for pathheader.size */
112 case RegionDataEmptyRect
:
113 case RegionDataInfiniteRect
:
116 needed
+= get_element_size(element
->elementdata
.combine
.left
);
117 needed
+= get_element_size(element
->elementdata
.combine
.right
);
124 /* Does not check parameters, caller must do that */
125 static inline GpStatus
init_region(GpRegion
* region
, const RegionType type
)
127 region
->node
.type
= type
;
128 region
->header
.checksum
= 0xdeadbeef;
129 region
->header
.magic
= VERSION_MAGIC
;
130 region
->header
.num_children
= 0;
131 region
->header
.size
= sizeheader_size
+ get_element_size(®ion
->node
);
136 static inline GpStatus
clone_element(const region_element
* element
,
137 region_element
** element2
)
141 /* root node is allocated with GpRegion */
143 *element2
= GdipAlloc(sizeof(region_element
));
148 (*element2
)->type
= element
->type
;
150 switch (element
->type
)
153 (*element2
)->elementdata
.rect
= element
->elementdata
.rect
;
155 case RegionDataEmptyRect
:
156 case RegionDataInfiniteRect
:
159 (*element2
)->elementdata
.pathdata
.pathheader
= element
->elementdata
.pathdata
.pathheader
;
160 stat
= GdipClonePath(element
->elementdata
.pathdata
.path
,
161 &(*element2
)->elementdata
.pathdata
.path
);
162 if (stat
!= Ok
) goto clone_out
;
165 (*element2
)->elementdata
.combine
.left
= NULL
;
166 (*element2
)->elementdata
.combine
.right
= NULL
;
168 stat
= clone_element(element
->elementdata
.combine
.left
,
169 &(*element2
)->elementdata
.combine
.left
);
170 if (stat
!= Ok
) goto clone_out
;
171 stat
= clone_element(element
->elementdata
.combine
.right
,
172 &(*element2
)->elementdata
.combine
.right
);
173 if (stat
!= Ok
) goto clone_out
;
180 delete_element(*element2
);
185 /* Common code for CombineRegion*
186 * All the caller has to do is get its format into an element
188 static inline void fuse_region(GpRegion
* region
, region_element
* left
,
189 region_element
* right
, const CombineMode mode
)
191 region
->node
.type
= mode
;
192 region
->node
.elementdata
.combine
.left
= left
;
193 region
->node
.elementdata
.combine
.right
= right
;
195 region
->header
.size
= sizeheader_size
+ get_element_size(®ion
->node
);
196 region
->header
.num_children
+= 2;
199 /*****************************************************************************
200 * GdipCloneRegion [GDIPLUS.@]
202 * Creates a deep copy of the region
205 * region [I] source region
206 * clone [O] resulting clone
210 * FAILURE: InvalidParameter or OutOfMemory
212 GpStatus WINGDIPAPI
GdipCloneRegion(GpRegion
*region
, GpRegion
**clone
)
214 region_element
*element
;
216 TRACE("%p %p\n", region
, clone
);
218 if (!(region
&& clone
))
219 return InvalidParameter
;
221 *clone
= GdipAlloc(sizeof(GpRegion
));
224 element
= &(*clone
)->node
;
226 (*clone
)->header
= region
->header
;
227 return clone_element(®ion
->node
, &element
);
230 GpStatus WINGDIPAPI
GdipCombineRegionPath(GpRegion
*region
, GpPath
*path
, CombineMode mode
)
232 GpRegion
*path_region
;
233 region_element
*left
, *right
= NULL
;
236 TRACE("%p %p %d\n", region
, path
, mode
);
238 if (!(region
&& path
))
239 return InvalidParameter
;
241 stat
= GdipCreateRegionPath(path
, &path_region
);
245 left
= GdipAlloc(sizeof(region_element
));
248 *left
= region
->node
;
250 stat
= clone_element(&path_region
->node
, &right
);
254 fuse_region(region
, left
, right
, mode
);
256 GdipDeleteRegion(path_region
);
261 delete_element(right
);
262 GdipDeleteRegion(path_region
);
266 GpStatus WINGDIPAPI
GdipCombineRegionRect(GpRegion
*region
,
267 GDIPCONST GpRectF
*rect
, CombineMode mode
)
269 GpRegion
*rect_region
;
270 region_element
*left
, *right
= NULL
;
273 TRACE("%p %p %d\n", region
, rect
, mode
);
275 if (!(region
&& rect
))
276 return InvalidParameter
;
278 stat
= GdipCreateRegionRect(rect
, &rect_region
);
282 left
= GdipAlloc(sizeof(region_element
));
285 memcpy(left
, ®ion
->node
, sizeof(region_element
));
287 stat
= clone_element(&rect_region
->node
, &right
);
291 fuse_region(region
, left
, right
, mode
);
293 GdipDeleteRegion(rect_region
);
298 delete_element(right
);
299 GdipDeleteRegion(rect_region
);
303 GpStatus WINGDIPAPI
GdipCombineRegionRectI(GpRegion
*region
,
304 GDIPCONST GpRect
*rect
, CombineMode mode
)
308 TRACE("%p %p %d\n", region
, rect
, mode
);
311 return InvalidParameter
;
313 rectf
.X
= (REAL
)rect
->X
;
314 rectf
.Y
= (REAL
)rect
->Y
;
315 rectf
.Height
= (REAL
)rect
->Height
;
316 rectf
.Width
= (REAL
)rect
->Width
;
318 return GdipCombineRegionRect(region
, &rectf
, mode
);
321 GpStatus WINGDIPAPI
GdipCombineRegionRegion(GpRegion
*region1
,
322 GpRegion
*region2
, CombineMode mode
)
324 region_element
*left
, *right
= NULL
;
327 TRACE("%p %p %d\n", region1
, region2
, mode
);
329 if(!(region1
&& region2
))
330 return InvalidParameter
;
332 left
= GdipAlloc(sizeof(region_element
));
336 *left
= region1
->node
;
337 stat
= clone_element(®ion2
->node
, &right
);
341 delete_element(right
);
345 fuse_region(region1
, left
, right
, mode
);
346 region1
->header
.num_children
+= region2
->header
.num_children
;
351 GpStatus WINGDIPAPI
GdipCreateRegion(GpRegion
**region
)
353 TRACE("%p\n", region
);
356 return InvalidParameter
;
358 *region
= GdipAlloc(sizeof(GpRegion
));
362 return init_region(*region
, RegionDataInfiniteRect
);
365 /*****************************************************************************
366 * GdipCreateRegionPath [GDIPLUS.@]
368 * Creates a GpRegion from a GpPath
371 * path [I] path to base the region on
372 * region [O] pointer to the newly allocated region
376 * FAILURE: InvalidParameter
379 * If a path has no floating point points, its points will be stored as shorts
382 * If a path is empty, it is considered to be an INTPATH
384 GpStatus WINGDIPAPI
GdipCreateRegionPath(GpPath
*path
, GpRegion
**region
)
386 region_element
* element
;
391 DWORD flags
= FLAGS_INTPATH
;
394 TRACE("%p, %p\n", path
, region
);
396 if (!(path
&& region
))
397 return InvalidParameter
;
399 *region
= GdipAlloc(sizeof(GpRegion
));
402 stat
= init_region(*region
, RegionDataPath
);
405 GdipDeleteRegion(*region
);
408 element
= &(*region
)->node
;
409 count
= path
->pathdata
.Count
;
411 /* Test to see if the path is an Integer path */
414 pointsi
= GdipAlloc(sizeof(GpPoint
) * count
);
415 pointsf
= GdipAlloc(sizeof(GpPointF
) * count
);
416 if (!(pointsi
&& pointsf
))
420 GdipDeleteRegion(*region
);
424 stat
= GdipGetPathPointsI(path
, pointsi
, count
);
427 GdipDeleteRegion(*region
);
430 stat
= GdipGetPathPoints(path
, pointsf
, count
);
433 GdipDeleteRegion(*region
);
437 for (i
= 0; i
< count
; i
++)
439 if (!(pointsi
[i
].X
== pointsf
[i
].X
&&
440 pointsi
[i
].Y
== pointsf
[i
].Y
))
442 flags
= FLAGS_NOFLAGS
;
450 stat
= GdipClonePath(path
, &element
->elementdata
.pathdata
.path
);
453 GdipDeleteRegion(*region
);
457 /* 3 for headers, once again size doesn't count itself */
458 element
->elementdata
.pathdata
.pathheader
.size
= ((sizeof(DWORD
) * 3));
461 /* Floats, sent out as floats */
463 element
->elementdata
.pathdata
.pathheader
.size
+=
464 (sizeof(DWORD
) * count
* 2);
466 /* INTs, sent out as packed shorts */
468 element
->elementdata
.pathdata
.pathheader
.size
+=
469 (sizeof(DWORD
) * count
);
472 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags
);
474 element
->elementdata
.pathdata
.pathheader
.size
+= get_pathtypes_size(path
);
475 element
->elementdata
.pathdata
.pathheader
.magic
= VERSION_MAGIC
;
476 element
->elementdata
.pathdata
.pathheader
.count
= count
;
477 element
->elementdata
.pathdata
.pathheader
.flags
= flags
;
478 (*region
)->header
.size
= sizeheader_size
+ get_element_size(element
);
483 GpStatus WINGDIPAPI
GdipCreateRegionRect(GDIPCONST GpRectF
*rect
,
488 TRACE("%p, %p\n", rect
, region
);
490 if (!(rect
&& region
))
491 return InvalidParameter
;
493 *region
= GdipAlloc(sizeof(GpRegion
));
494 stat
= init_region(*region
, RegionDataRect
);
497 GdipDeleteRegion(*region
);
501 (*region
)->node
.elementdata
.rect
.X
= rect
->X
;
502 (*region
)->node
.elementdata
.rect
.Y
= rect
->Y
;
503 (*region
)->node
.elementdata
.rect
.Width
= rect
->Width
;
504 (*region
)->node
.elementdata
.rect
.Height
= rect
->Height
;
509 GpStatus WINGDIPAPI
GdipCreateRegionRectI(GDIPCONST GpRect
*rect
,
514 TRACE("%p, %p\n", rect
, region
);
516 rectf
.X
= (REAL
)rect
->X
;
517 rectf
.Y
= (REAL
)rect
->Y
;
518 rectf
.Width
= (REAL
)rect
->Width
;
519 rectf
.Height
= (REAL
)rect
->Height
;
521 return GdipCreateRegionRect(&rectf
, region
);
524 GpStatus WINGDIPAPI
GdipCreateRegionRgnData(GDIPCONST BYTE
*data
, INT size
, GpRegion
**region
)
526 FIXME("(%p, %d, %p): stub\n", data
, size
, region
);
529 return NotImplemented
;
532 GpStatus WINGDIPAPI
GdipCreateRegionHrgn(HRGN hrgn
, GpRegion
**region
)
534 FIXME("(%p, %p): stub\n", hrgn
, region
);
537 return NotImplemented
;
540 GpStatus WINGDIPAPI
GdipDeleteRegion(GpRegion
*region
)
542 TRACE("%p\n", region
);
545 return InvalidParameter
;
547 delete_element(®ion
->node
);
553 GpStatus WINGDIPAPI
GdipGetRegionBounds(GpRegion
*region
, GpGraphics
*graphics
, GpRectF
*rect
)
555 FIXME("(%p, %p, %p): stub\n", region
, graphics
, rect
);
557 return NotImplemented
;
560 GpStatus WINGDIPAPI
GdipGetRegionBoundsI(GpRegion
*region
, GpGraphics
*graphics
, GpRect
*rect
)
562 FIXME("(%p, %p, %p): stub\n", region
, graphics
, rect
);
564 return NotImplemented
;
567 static inline void write_dword(DWORD
* location
, INT
* offset
, const DWORD write
)
569 location
[*offset
] = write
;
573 static inline void write_float(DWORD
* location
, INT
* offset
, const FLOAT write
)
575 ((FLOAT
*)location
)[*offset
] = write
;
579 static inline void write_packed_point(DWORD
* location
, INT
* offset
,
580 const GpPointF
* write
)
586 memcpy(location
+ *offset
, &point
, sizeof(packed_point
));
590 static inline void write_path_types(DWORD
* location
, INT
* offset
,
593 memcpy(location
+ *offset
, path
->pathdata
.Types
, path
->pathdata
.Count
);
595 /* The unwritten parts of the DWORD (if any) must be cleared */
596 if (path
->pathdata
.Count
% sizeof(DWORD
))
597 ZeroMemory(((BYTE
*)location
) + (*offset
* sizeof(DWORD
)) +
598 path
->pathdata
.Count
,
599 sizeof(DWORD
) - path
->pathdata
.Count
% sizeof(DWORD
));
600 *offset
+= (get_pathtypes_size(path
) / sizeof(DWORD
));
603 static void write_element(const region_element
* element
, DWORD
*buffer
,
606 write_dword(buffer
, filled
, element
->type
);
607 switch (element
->type
)
609 case CombineModeReplace
:
610 case CombineModeIntersect
:
611 case CombineModeUnion
:
613 case CombineModeExclude
:
614 case CombineModeComplement
:
615 write_element(element
->elementdata
.combine
.left
, buffer
, filled
);
616 write_element(element
->elementdata
.combine
.right
, buffer
, filled
);
619 write_float(buffer
, filled
, element
->elementdata
.rect
.X
);
620 write_float(buffer
, filled
, element
->elementdata
.rect
.Y
);
621 write_float(buffer
, filled
, element
->elementdata
.rect
.Width
);
622 write_float(buffer
, filled
, element
->elementdata
.rect
.Height
);
627 const GpPath
* path
= element
->elementdata
.pathdata
.path
;
629 memcpy(buffer
+ *filled
, &element
->elementdata
.pathdata
.pathheader
,
630 sizeof(element
->elementdata
.pathdata
.pathheader
));
631 *filled
+= sizeof(element
->elementdata
.pathdata
.pathheader
) / sizeof(DWORD
);
632 switch (element
->elementdata
.pathdata
.pathheader
.flags
)
635 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
637 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].X
);
638 write_float(buffer
, filled
, path
->pathdata
.Points
[i
].Y
);
642 for (i
= 0; i
< path
->pathdata
.Count
; i
++)
644 write_packed_point(buffer
, filled
,
645 &path
->pathdata
.Points
[i
]);
648 write_path_types(buffer
, filled
, path
);
651 case RegionDataEmptyRect
:
652 case RegionDataInfiniteRect
:
657 /*****************************************************************************
658 * GdipGetRegionData [GDIPLUS.@]
660 * Returns the header, followed by combining ops and region elements.
663 * region [I] region to retrieve from
664 * buffer [O] buffer to hold the resulting data
665 * size [I] size of the buffer
666 * needed [O] (optional) how much data was written
670 * FAILURE: InvalidParamter
673 * The header contains the size, a checksum, a version string, and the number
674 * of children. The size does not count itself or the checksum.
675 * Version is always something like 0xdbc01001 or 0xdbc01002
677 * An element is a RECT, or PATH; Combining ops are stored as their
678 * CombineMode value. Special regions (infinite, empty) emit just their
679 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
680 * their code followed by a second header for the path followed by the actual
681 * path data. Followed by the flags for each point. The pathheader contains
682 * the size of the data to follow, a version number again, followed by a count
683 * of how many points, and any special flags which may apply. 0x4000 means its
684 * a path of shorts instead of FLOAT.
686 * Combining Ops are stored in reverse order from when they were constructed;
687 * the output is a tree where the left side combining area is always taken
690 GpStatus WINGDIPAPI
GdipGetRegionData(GpRegion
*region
, BYTE
*buffer
, UINT size
,
695 TRACE("%p, %p, %d, %p\n", region
, buffer
, size
, needed
);
697 if (!(region
&& buffer
&& size
))
698 return InvalidParameter
;
700 memcpy(buffer
, ®ion
->header
, sizeof(region
->header
));
701 filled
+= sizeof(region
->header
) / sizeof(DWORD
);
702 /* With few exceptions, everything written is DWORD aligned,
703 * so use that as our base */
704 write_element(®ion
->node
, (DWORD
*)buffer
, &filled
);
707 *needed
= filled
* sizeof(DWORD
);
712 GpStatus WINGDIPAPI
GdipGetRegionDataSize(GpRegion
*region
, UINT
*needed
)
714 TRACE("%p, %p\n", region
, needed
);
716 if (!(region
&& needed
))
717 return InvalidParameter
;
719 /* header.size doesn't count header.size and header.checksum */
720 *needed
= region
->header
.size
+ sizeof(DWORD
) * 2;
725 /*****************************************************************************
726 * GdipGetRegionHRgn [GDIPLUS.@]
728 GpStatus WINGDIPAPI
GdipGetRegionHRgn(GpRegion
*region
, GpGraphics
*graphics
, HRGN
*hrgn
)
730 FIXME("(%p, %p, %p): stub\n", region
, graphics
, hrgn
);
733 return NotImplemented
;
736 GpStatus WINGDIPAPI
GdipIsEmptyRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
738 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
740 if(!region
|| !graphics
|| !res
)
741 return InvalidParameter
;
743 *res
= (region
->node
.type
== RegionDataEmptyRect
);
748 GpStatus WINGDIPAPI
GdipIsEqualRegion(GpRegion
*region
, GpRegion
*region2
, GpGraphics
*graphics
,
751 FIXME("(%p, %p, %p, %p): stub\n", region
, region2
, graphics
, res
);
753 return NotImplemented
;
756 /* I think graphics is ignored here */
757 GpStatus WINGDIPAPI
GdipIsInfiniteRegion(GpRegion
*region
, GpGraphics
*graphics
, BOOL
*res
)
759 TRACE("(%p, %p, %p)\n", region
, graphics
, res
);
761 if(!region
|| !graphics
|| !res
)
762 return InvalidParameter
;
764 *res
= (region
->node
.type
== RegionDataInfiniteRect
);
769 GpStatus WINGDIPAPI
GdipSetEmpty(GpRegion
*region
)
773 TRACE("%p\n", region
);
776 return InvalidParameter
;
778 delete_element(®ion
->node
);
779 stat
= init_region(region
, RegionDataEmptyRect
);
784 GpStatus WINGDIPAPI
GdipSetInfinite(GpRegion
*region
)
788 TRACE("%p\n", region
);
791 return InvalidParameter
;
793 delete_element(®ion
->node
);
794 stat
= init_region(region
, RegionDataInfiniteRect
);
799 GpStatus WINGDIPAPI
GdipTransformRegion(GpRegion
*region
, GpMatrix
*matrix
)
801 FIXME("(%p, %p): stub\n", region
, matrix
);
803 return NotImplemented
;
806 GpStatus WINGDIPAPI
GdipTranslateRegion(GpRegion
*region
, REAL dx
, REAL dy
)
808 FIXME("(%p, %f, %f): stub\n", region
, dx
, dy
);
810 return NotImplemented
;
813 GpStatus WINGDIPAPI
GdipTranslateRegionI(GpRegion
*region
, INT dx
, INT dy
)
815 FIXME("(%p, %d, %d): stub\n", region
, dx
, dy
);
817 return NotImplemented
;