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 typedef enum RegionType
78 RegionDataRect = 0x10000000,
79 RegionDataPath = 0x10000001,
80 RegionDataEmptyRect = 0x10000002,
81 RegionDataInfiniteRect = 0x10000003,
84 #define FLAGS_NOFLAGS 0x0
85 #define FLAGS_INTPATH 0x4000
87 /* Header size as far as header->size is concerned. This doesn't include
88 * header->size or header->checksum
90 static const INT sizeheader_size = sizeof(DWORD) * 2;
92 typedef struct packed_point
98 /* Everything is measured in DWORDS; round up if there's a remainder */
99 static inline INT get_pathtypes_size(const GpPath* path)
101 INT needed = path->pathdata.Count / sizeof(DWORD);
103 if (path->pathdata.Count % sizeof(DWORD) > 0)
106 return needed * sizeof(DWORD);
109 static inline INT get_element_size(const region_element* element)
111 INT needed = sizeof(DWORD); /* DWORD for the type */
112 switch(element->type)
115 return needed + sizeof(GpRect);
117 needed += element->elementdata.pathdata.pathheader.size;
118 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
120 case RegionDataEmptyRect:
121 case RegionDataInfiniteRect:
124 needed += get_element_size(element->elementdata.combine.left);
125 needed += get_element_size(element->elementdata.combine.right);
132 /* Does not check parameters, caller must do that */
133 static inline GpStatus init_region(GpRegion* region, const RegionType type)
135 region->node.type = type;
136 region->header.checksum = 0xdeadbeef;
137 region->header.magic = VERSION_MAGIC;
138 region->header.num_children = 0;
139 region->header.size = sizeheader_size + get_element_size(®ion->node);
144 static inline void delete_element(region_element* element)
146 switch(element->type)
151 GdipDeletePath(element->elementdata.pathdata.path);
153 case RegionDataEmptyRect:
154 case RegionDataInfiniteRect:
157 delete_element(element->elementdata.combine.left);
158 delete_element(element->elementdata.combine.right);
159 GdipFree(element->elementdata.combine.left);
160 GdipFree(element->elementdata.combine.right);
165 static inline GpStatus clone_element(const region_element* element,
166 region_element** element2)
170 *element2 = GdipAlloc(sizeof(region_element));
174 (*element2)->type = element->type;
176 switch (element->type)
179 (*element2)->elementdata.rect = element->elementdata.rect;
181 case RegionDataEmptyRect:
182 case RegionDataInfiniteRect:
185 (*element2)->elementdata.pathdata.pathheader = element->elementdata.pathdata.pathheader;
186 stat = GdipClonePath(element->elementdata.pathdata.path,
187 &(*element2)->elementdata.pathdata.path);
188 if (stat != Ok) goto clone_out;
191 stat = clone_element(element->elementdata.combine.left,
192 &(*element2)->elementdata.combine.left);
193 if (stat != Ok) goto clone_out;
194 stat = clone_element(element->elementdata.combine.right,
195 &(*element2)->elementdata.combine.right);
196 if (stat != Ok) goto clone_out;
203 delete_element(*element2);
208 /* Common code for CombineRegion*
209 * All the caller has to do is get its format into an element
211 static inline void fuse_region(GpRegion* region, region_element* left,
212 region_element* right, const CombineMode mode)
214 region->node.type = mode;
215 region->node.elementdata.combine.left = left;
216 region->node.elementdata.combine.right = right;
218 region->header.size = sizeheader_size + get_element_size(®ion->node);
219 region->header.num_children += 2;
222 /*****************************************************************************
223 * GdipCloneRegion [GDIPLUS.@]
225 * Creates a deep copy of the region
228 * region [I] source region
229 * clone [O] resulting clone
233 * FAILURE: InvalidParameter or OutOfMemory
235 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
237 region_element *element;
239 TRACE("%p %p\n", region, clone);
241 if (!(region && clone))
242 return InvalidParameter;
244 *clone = GdipAlloc(sizeof(GpRegion));
247 element = &(*clone)->node;
249 (*clone)->header = region->header;
250 return clone_element(®ion->node, &element);
253 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
255 GpRegion *path_region;
256 region_element *left, *right = NULL;
259 TRACE("%p %p %d\n", region, path, mode);
261 if (!(region && path))
262 return InvalidParameter;
264 stat = GdipCreateRegionPath(path, &path_region);
268 left = GdipAlloc(sizeof(region_element));
271 *left = region->node;
273 stat = clone_element(&path_region->node, &right);
277 fuse_region(region, left, right, mode);
279 GdipDeleteRegion(path_region);
284 delete_element(right);
285 GdipDeleteRegion(path_region);
289 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
290 GDIPCONST GpRectF *rect, CombineMode mode)
292 GpRegion *rect_region;
293 region_element *left, *right = NULL;
296 TRACE("%p %p %d\n", region, rect, mode);
298 if (!(region && rect))
299 return InvalidParameter;
301 stat = GdipCreateRegionRect(rect, &rect_region);
305 left = GdipAlloc(sizeof(region_element));
308 memcpy(left, ®ion->node, sizeof(region_element));
310 stat = clone_element(&rect_region->node, &right);
314 fuse_region(region, left, right, mode);
316 GdipDeleteRegion(rect_region);
321 delete_element(right);
322 GdipDeleteRegion(rect_region);
326 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
327 GDIPCONST GpRect *rect, CombineMode mode)
331 TRACE("%p %p %d\n", region, rect, mode);
334 return InvalidParameter;
336 rectf.X = (REAL)rect->X;
337 rectf.Y = (REAL)rect->Y;
338 rectf.Height = (REAL)rect->Height;
339 rectf.Width = (REAL)rect->Width;
341 return GdipCombineRegionRect(region, &rectf, mode);
344 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
345 GpRegion *region2, CombineMode mode)
347 region_element *left, *right;
350 TRACE("%p %p %d\n", region1, region2, mode);
352 if(!(region1 && region2))
353 return InvalidParameter;
355 left = GdipAlloc(sizeof(region_element));
359 *left = region1->node;
360 stat = clone_element(®ion2->node, &right);
364 delete_element(right);
368 fuse_region(region1, left, right, mode);
369 region1->header.num_children += region2->header.num_children;
374 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
377 return InvalidParameter;
379 TRACE("%p\n", region);
381 *region = GdipAlloc(sizeof(GpRegion));
385 return init_region(*region, RegionDataInfiniteRect);
388 /*****************************************************************************
389 * GdipCreateRegionPath [GDIPLUS.@]
391 * Creates a GpRegion from a GpPath
394 * path [I] path to base the region on
395 * region [O] pointer to the newly allocated region
399 * FAILURE: InvalidParameter
402 * If a path has no floating point points, its points will be stored as shorts
405 * If a path is empty, it is considered to be an INTPATH
407 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
409 region_element* element;
414 DWORD flags = FLAGS_INTPATH;
417 TRACE("%p, %p\n", path, region);
419 if (!(path && region))
420 return InvalidParameter;
422 *region = GdipAlloc(sizeof(GpRegion));
425 stat = init_region(*region, RegionDataPath);
428 GdipDeleteRegion(*region);
431 element = &(*region)->node;
432 count = path->pathdata.Count;
434 /* Test to see if the path is an Integer path */
437 pointsi = GdipAlloc(sizeof(GpPoint) * count);
438 pointsf = GdipAlloc(sizeof(GpPointF) * count);
439 if (!(pointsi && pointsf))
443 GdipDeleteRegion(*region);
447 stat = GdipGetPathPointsI(path, pointsi, count);
450 GdipDeleteRegion(*region);
453 stat = GdipGetPathPoints(path, pointsf, count);
456 GdipDeleteRegion(*region);
460 for (i = 0; i < count; i++)
462 if (!(pointsi[i].X == pointsf[i].X &&
463 pointsi[i].Y == pointsf[i].Y ))
465 flags = FLAGS_NOFLAGS;
473 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
476 GdipDeleteRegion(*region);
480 /* 3 for headers, once again size doesn't count itself */
481 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
484 /* Floats, sent out as floats */
486 element->elementdata.pathdata.pathheader.size +=
487 (sizeof(DWORD) * count * 2);
489 /* INTs, sent out as packed shorts */
491 element->elementdata.pathdata.pathheader.size +=
492 (sizeof(DWORD) * count);
495 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
497 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
498 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
499 element->elementdata.pathdata.pathheader.count = count;
500 element->elementdata.pathdata.pathheader.flags = flags;
501 (*region)->header.size = sizeheader_size + get_element_size(element);
506 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
511 TRACE("%p, %p\n", rect, region);
513 if (!(rect && region))
514 return InvalidParameter;
516 *region = GdipAlloc(sizeof(GpRegion));
517 stat = init_region(*region, RegionDataRect);
520 GdipDeleteRegion(*region);
524 (*region)->node.elementdata.rect.X = rect->X;
525 (*region)->node.elementdata.rect.Y = rect->Y;
526 (*region)->node.elementdata.rect.Width = rect->Width;
527 (*region)->node.elementdata.rect.Height = rect->Height;
532 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
537 TRACE("%p, %p\n", rect, region);
539 rectf.X = (REAL)rect->X;
540 rectf.Y = (REAL)rect->Y;
541 rectf.Width = (REAL)rect->Width;
542 rectf.Height = (REAL)rect->Height;
544 return GdipCreateRegionRect(&rectf, region);
547 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
549 FIXME("(%p, %d, %p): stub\n", data, size, region);
552 return NotImplemented;
555 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
557 FIXME("(%p, %p): stub\n", hrgn, region);
560 return NotImplemented;
563 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
565 TRACE("%p\n", region);
568 return InvalidParameter;
570 delete_element(®ion->node);
576 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
578 FIXME("(%p, %p, %p): stub\n", region, graphics, rect);
580 return NotImplemented;
583 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
585 FIXME("(%p, %p, %p): stub\n", region, graphics, rect);
587 return NotImplemented;
590 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
592 location[*offset] = write;
596 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
598 ((FLOAT*)location)[*offset] = write;
602 static inline void write_packed_point(DWORD* location, INT* offset,
603 const GpPointF* write)
609 memcpy(location + *offset, &point, sizeof(packed_point));
613 static inline void write_path_types(DWORD* location, INT* offset,
616 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
618 /* The unwritten parts of the DWORD (if any) must be cleared */
619 if (path->pathdata.Count % sizeof(DWORD))
620 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
621 path->pathdata.Count,
622 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
623 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
626 static void write_element(const region_element* element, DWORD *buffer,
629 write_dword(buffer, filled, element->type);
630 switch (element->type)
632 case CombineModeReplace:
633 case CombineModeIntersect:
634 case CombineModeUnion:
636 case CombineModeExclude:
637 case CombineModeComplement:
638 write_element(element->elementdata.combine.left, buffer, filled);
639 write_element(element->elementdata.combine.right, buffer, filled);
642 write_float(buffer, filled, element->elementdata.rect.X);
643 write_float(buffer, filled, element->elementdata.rect.Y);
644 write_float(buffer, filled, element->elementdata.rect.Width);
645 write_float(buffer, filled, element->elementdata.rect.Height);
650 const GpPath* path = element->elementdata.pathdata.path;
652 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
653 sizeof(element->elementdata.pathdata.pathheader));
654 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
655 switch (element->elementdata.pathdata.pathheader.flags)
658 for (i = 0; i < path->pathdata.Count; i++)
660 write_float(buffer, filled, path->pathdata.Points[i].X);
661 write_float(buffer, filled, path->pathdata.Points[i].Y);
665 for (i = 0; i < path->pathdata.Count; i++)
667 write_packed_point(buffer, filled,
668 &path->pathdata.Points[i]);
671 write_path_types(buffer, filled, path);
674 case RegionDataEmptyRect:
675 case RegionDataInfiniteRect:
680 /*****************************************************************************
681 * GdipGetRegionData [GDIPLUS.@]
683 * Returns the header, followed by combining ops and region elements.
686 * region [I] region to retrieve from
687 * buffer [O] buffer to hold the resulting data
688 * size [I] size of the buffer
689 * needed [O] (optional) how much data was written
693 * FAILURE: InvalidParamter
696 * The header contains the size, a checksum, a version string, and the number
697 * of children. The size does not count itself or the checksum.
698 * Version is always something like 0xdbc01001 or 0xdbc01002
700 * An element is a RECT, or PATH; Combining ops are stored as their
701 * CombineMode value. Special regions (infinite, empty) emit just their
702 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
703 * their code followed by a second header for the path followed by the actual
704 * path data. Followed by the flags for each point. The pathheader contains
705 * the size of the data to follow, a version number again, followed by a count
706 * of how many points, and any special flags which may apply. 0x4000 means its
707 * a path of shorts instead of FLOAT.
709 * Combining Ops are stored in reverse order from when they were constructed;
710 * the output is a tree where the left side combining area is always taken
713 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
718 if (!(region && buffer && size))
719 return InvalidParameter;
721 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
722 memcpy(buffer, ®ion->header, sizeof(region->header));
723 filled += sizeof(region->header) / sizeof(DWORD);
724 /* With few exceptions, everything written is DWORD aligned,
725 * so use that as our base */
726 write_element(®ion->node, (DWORD*)buffer, &filled);
729 *needed = filled * sizeof(DWORD);
734 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
736 if (!(region && needed))
737 return InvalidParameter;
739 TRACE("%p, %p\n", region, needed);
741 /* header.size doesn't count header.size and header.checksum */
742 *needed = region->header.size + sizeof(DWORD) * 2;
747 /*****************************************************************************
748 * GdipGetRegionHRgn [GDIPLUS.@]
750 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
752 FIXME("(%p, %p, %p): stub\n", region, graphics, hrgn);
755 return NotImplemented;
758 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
760 FIXME("(%p, %p, %p): stub\n", region, graphics, res);
762 return NotImplemented;
765 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
768 FIXME("(%p, %p, %p, %p): stub\n", region, region2, graphics, res);
770 return NotImplemented;
773 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
775 FIXME("(%p, %p, %p): stub\n", region, graphics, res);
777 return NotImplemented;
780 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
784 TRACE("%p\n", region);
787 return InvalidParameter;
789 delete_element(®ion->node);
790 stat = init_region(region, RegionDataEmptyRect);
795 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
800 return InvalidParameter;
802 TRACE("%p\n", region);
804 delete_element(®ion->node);
805 stat = init_region(region, RegionDataInfiniteRect);
810 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
812 FIXME("(%p, %p): stub\n", region, matrix);
814 return NotImplemented;
817 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
819 FIXME("(%p, %f, %f): stub\n", region, dx, dy);
821 return NotImplemented;
824 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
826 FIXME("(%p, %d, %d): stub\n", region, dx, dy);
828 return NotImplemented;