1 /*
2 * Copyright (C) 2008 Google (Lei Zhang)
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24
25 #include "objbase.h"
26
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32
33 /**********************************************************
34 *
35 * Data returned by GdipGetRegionData looks something like this:
36 *
37 * struct region_data_header
38 * {
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
43 * };
44 *
45 * Then follows a sequence of combining ops and region elements.
46 *
47 * A region element is either a RECTF or some path data.
48 *
49 * Combining ops are just stored as their CombineMode value.
50 *
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).
54 *
55 * Path data is preceded by the DWORD 0x10000001. Then follows a
56 * DWORD size and then size bytes of data.
57 *
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
60 * constructed.
61 *
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
69 * stored).
70 *
71 * When a simple region (1 element) is combined, it's treated as if a
72 * single rect/path is being combined.
73 *
74 */
75
76 #define FLAGS_NOFLAGS 0x0
77 #define FLAGS_INTPATH 0x4000
78
79 /* Header size as far as header->size is concerned. This doesn't include
80 * header->size or header->checksum
81 */
82 static const INT sizeheader_size = sizeof(DWORD) * 2;
83
84 typedef struct packed_point
85 {
86 short X;
87 short Y;
88 } packed_point;
89
90 /* Everything is measured in DWORDS; round up if there's a remainder */
91 static inline INT get_pathtypes_size(const GpPath* path)
92 {
93 INT needed = path->pathdata.Count / sizeof(DWORD);
94
95 if (path->pathdata.Count % sizeof(DWORD) > 0)
96 needed++;
97
98 return needed * sizeof(DWORD);
99 }
100
101 static inline INT get_element_size(const region_element* element)
102 {
103 INT needed = sizeof(DWORD); /* DWORD for the type */
104 switch(element->type)
105 {
106 case RegionDataRect:
107 return needed + sizeof(GpRect);
108 case RegionDataPath:
109 needed += element->elementdata.pathdata.pathheader.size;
110 needed += sizeof(DWORD); /* Extra DWORD for pathheader.size */
111 return needed;
112 case RegionDataEmptyRect:
113 case RegionDataInfiniteRect:
114 return needed;
115 default:
116 needed += get_element_size(element->elementdata.combine.left);
117 needed += get_element_size(element->elementdata.combine.right);
118 return needed;
119 }
120
121 return 0;
122 }
123
124 /* Does not check parameters, caller must do that */
125 static inline GpStatus init_region(GpRegion* region, const RegionType type)
126 {
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);
132
133 return Ok;
134 }
135
136 static inline GpStatus clone_element(const region_element* element,
137 region_element** element2)
138 {
139 GpStatus stat;
140
141 /* root node is allocated with GpRegion */
142 if(!*element2){
143 *element2 = GdipAlloc(sizeof(region_element));
144 if (!*element2)
145 return OutOfMemory;
146 }
147
148 (*element2)->type = element->type;
149
150 switch (element->type)
151 {
152 case RegionDataRect:
153 (*element2)->elementdata.rect = element->elementdata.rect;
154 break;
155 case RegionDataEmptyRect:
156 case RegionDataInfiniteRect:
157 break;
158 case RegionDataPath:
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;
163 break;
164 default:
165 (*element2)->elementdata.combine.left = NULL;
166 (*element2)->elementdata.combine.right = NULL;
167
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;
174 break;
175 }
176
177 return Ok;
178
179 clone_out:
180 delete_element(*element2);
181 *element2 = NULL;
182 return stat;
183 }
184
185 /* Common code for CombineRegion*
186 * All the caller has to do is get its format into an element
187 */
188 static inline void fuse_region(GpRegion* region, region_element* left,
189 region_element* right, const CombineMode mode)
190 {
191 region->node.type = mode;
192 region->node.elementdata.combine.left = left;
193 region->node.elementdata.combine.right = right;
194
195 region->header.size = sizeheader_size + get_element_size(®ion->node);
196 region->header.num_children += 2;
197 }
198
199 /*****************************************************************************
200 * GdipCloneRegion [GDIPLUS.@]
201 *
202 * Creates a deep copy of the region
203 *
204 * PARAMS
205 * region [I] source region
206 * clone [O] resulting clone
207 *
208 * RETURNS
209 * SUCCESS: Ok
210 * FAILURE: InvalidParameter or OutOfMemory
211 */
212 GpStatus WINGDIPAPI GdipCloneRegion(GpRegion *region, GpRegion **clone)
213 {
214 region_element *element;
215
216 TRACE("%p %p\n", region, clone);
217
218 if (!(region && clone))
219 return InvalidParameter;
220
221 *clone = GdipAlloc(sizeof(GpRegion));
222 if (!*clone)
223 return OutOfMemory;
224 element = &(*clone)->node;
225
226 (*clone)->header = region->header;
227 return clone_element(®ion->node, &element);
228 }
229
230 /*****************************************************************************
231 * GdipCombineRegionPath [GDIPLUS.@]
232 */
233 GpStatus WINGDIPAPI GdipCombineRegionPath(GpRegion *region, GpPath *path, CombineMode mode)
234 {
235 GpRegion *path_region;
236 region_element *left, *right = NULL;
237 GpStatus stat;
238
239 TRACE("%p %p %d\n", region, path, mode);
240
241 if (!(region && path))
242 return InvalidParameter;
243
244 stat = GdipCreateRegionPath(path, &path_region);
245 if (stat != Ok)
246 return stat;
247
248 /* simply replace region data */
249 if(mode == CombineModeReplace){
250 delete_element(®ion->node);
251 memcpy(region, path_region, sizeof(GpRegion));
252 return Ok;
253 }
254
255 left = GdipAlloc(sizeof(region_element));
256 if (!left)
257 goto out;
258 *left = region->node;
259
260 stat = clone_element(&path_region->node, &right);
261 if (stat != Ok)
262 goto out;
263
264 fuse_region(region, left, right, mode);
265
266 GdipDeleteRegion(path_region);
267 return Ok;
268
269 out:
270 GdipFree(left);
271 GdipDeleteRegion(path_region);
272 return stat;
273 }
274
275 /*****************************************************************************
276 * GdipCombineRegionRect [GDIPLUS.@]
277 */
278 GpStatus WINGDIPAPI GdipCombineRegionRect(GpRegion *region,
279 GDIPCONST GpRectF *rect, CombineMode mode)
280 {
281 GpRegion *rect_region;
282 region_element *left, *right = NULL;
283 GpStatus stat;
284
285 TRACE("%p %p %d\n", region, rect, mode);
286
287 if (!(region && rect))
288 return InvalidParameter;
289
290 stat = GdipCreateRegionRect(rect, &rect_region);
291 if (stat != Ok)
292 return stat;
293
294 /* simply replace region data */
295 if(mode == CombineModeReplace){
296 delete_element(®ion->node);
297 memcpy(region, rect_region, sizeof(GpRegion));
298 return Ok;
299 }
300
301 left = GdipAlloc(sizeof(region_element));
302 if (!left)
303 goto out;
304 memcpy(left, ®ion->node, sizeof(region_element));
305
306 stat = clone_element(&rect_region->node, &right);
307 if (stat != Ok)
308 goto out;
309
310 fuse_region(region, left, right, mode);
311
312 GdipDeleteRegion(rect_region);
313 return Ok;
314
315 out:
316 GdipFree(left);
317 GdipDeleteRegion(rect_region);
318 return stat;
319 }
320
321 /*****************************************************************************
322 * GdipCombineRegionRectI [GDIPLUS.@]
323 */
324 GpStatus WINGDIPAPI GdipCombineRegionRectI(GpRegion *region,
325 GDIPCONST GpRect *rect, CombineMode mode)
326 {
327 GpRectF rectf;
328
329 TRACE("%p %p %d\n", region, rect, mode);
330
331 if (!rect)
332 return InvalidParameter;
333
334 rectf.X = (REAL)rect->X;
335 rectf.Y = (REAL)rect->Y;
336 rectf.Height = (REAL)rect->Height;
337 rectf.Width = (REAL)rect->Width;
338
339 return GdipCombineRegionRect(region, &rectf, mode);
340 }
341
342 /*****************************************************************************
343 * GdipCombineRegionRegion [GDIPLUS.@]
344 */
345 GpStatus WINGDIPAPI GdipCombineRegionRegion(GpRegion *region1,
346 GpRegion *region2, CombineMode mode)
347 {
348 region_element *left, *right = NULL;
349 GpStatus stat;
350 GpRegion *reg2copy;
351
352 TRACE("%p %p %d\n", region1, region2, mode);
353
354 if(!(region1 && region2))
355 return InvalidParameter;
356
357 /* simply replace region data */
358 if(mode == CombineModeReplace){
359 stat = GdipCloneRegion(region2, ®2copy);
360 if(stat != Ok) return stat;
361
362 delete_element(®ion1->node);
363 memcpy(region1, reg2copy, sizeof(GpRegion));
364 GdipFree(reg2copy);
365 return Ok;
366 }
367
368 left = GdipAlloc(sizeof(region_element));
369 if (!left)
370 return OutOfMemory;
371
372 *left = region1->node;
373 stat = clone_element(®ion2->node, &right);
374 if (stat != Ok)
375 {
376 GdipFree(left);
377 return OutOfMemory;
378 }
379
380 fuse_region(region1, left, right, mode);
381 region1->header.num_children += region2->header.num_children;
382
383 return Ok;
384 }
385
386 /*****************************************************************************
387 * GdipCreateRegion [GDIPLUS.@]
388 */
389 GpStatus WINGDIPAPI GdipCreateRegion(GpRegion **region)
390 {
391 TRACE("%p\n", region);
392
393 if(!region)
394 return InvalidParameter;
395
396 *region = GdipAlloc(sizeof(GpRegion));
397 if(!*region)
398 return OutOfMemory;
399
400 return init_region(*region, RegionDataInfiniteRect);
401 }
402
403 /*****************************************************************************
404 * GdipCreateRegionPath [GDIPLUS.@]
405 *
406 * Creates a GpRegion from a GpPath
407 *
408 * PARAMS
409 * path [I] path to base the region on
410 * region [O] pointer to the newly allocated region
411 *
412 * RETURNS
413 * SUCCESS: Ok
414 * FAILURE: InvalidParameter
415 *
416 * NOTES
417 * If a path has no floating point points, its points will be stored as shorts
418 * (INTPATH)
419 *
420 * If a path is empty, it is considered to be an INTPATH
421 */
422 GpStatus WINGDIPAPI GdipCreateRegionPath(GpPath *path, GpRegion **region)
423 {
424 region_element* element;
425 GpPoint *pointsi;
426 GpPointF *pointsf;
427
428 GpStatus stat;
429 DWORD flags = FLAGS_INTPATH;
430 INT count, i;
431
432 TRACE("%p, %p\n", path, region);
433
434 if (!(path && region))
435 return InvalidParameter;
436
437 *region = GdipAlloc(sizeof(GpRegion));
438 if(!*region)
439 return OutOfMemory;
440 stat = init_region(*region, RegionDataPath);
441 if (stat != Ok)
442 {
443 GdipDeleteRegion(*region);
444 return stat;
445 }
446 element = &(*region)->node;
447 count = path->pathdata.Count;
448
449 /* Test to see if the path is an Integer path */
450 if (count)
451 {
452 pointsi = GdipAlloc(sizeof(GpPoint) * count);
453 pointsf = GdipAlloc(sizeof(GpPointF) * count);
454 if (!(pointsi && pointsf))
455 {
456 GdipFree(pointsi);
457 GdipFree(pointsf);
458 GdipDeleteRegion(*region);
459 return OutOfMemory;
460 }
461
462 stat = GdipGetPathPointsI(path, pointsi, count);
463 if (stat != Ok)
464 {
465 GdipDeleteRegion(*region);
466 return stat;
467 }
468 stat = GdipGetPathPoints(path, pointsf, count);
469 if (stat != Ok)
470 {
471 GdipDeleteRegion(*region);
472 return stat;
473 }
474
475 for (i = 0; i < count; i++)
476 {
477 if (!(pointsi[i].X == pointsf[i].X &&
478 pointsi[i].Y == pointsf[i].Y ))
479 {
480 flags = FLAGS_NOFLAGS;
481 break;
482 }
483 }
484 GdipFree(pointsi);
485 GdipFree(pointsf);
486 }
487
488 stat = GdipClonePath(path, &element->elementdata.pathdata.path);
489 if (stat != Ok)
490 {
491 GdipDeleteRegion(*region);
492 return stat;
493 }
494
495 /* 3 for headers, once again size doesn't count itself */
496 element->elementdata.pathdata.pathheader.size = ((sizeof(DWORD) * 3));
497 switch(flags)
498 {
499 /* Floats, sent out as floats */
500 case FLAGS_NOFLAGS:
501 element->elementdata.pathdata.pathheader.size +=
502 (sizeof(DWORD) * count * 2);
503 break;
504 /* INTs, sent out as packed shorts */
505 case FLAGS_INTPATH:
506 element->elementdata.pathdata.pathheader.size +=
507 (sizeof(DWORD) * count);
508 break;
509 default:
510 FIXME("Unhandled flags (%08x). Expect wrong results.\n", flags);
511 }
512 element->elementdata.pathdata.pathheader.size += get_pathtypes_size(path);
513 element->elementdata.pathdata.pathheader.magic = VERSION_MAGIC;
514 element->elementdata.pathdata.pathheader.count = count;
515 element->elementdata.pathdata.pathheader.flags = flags;
516 (*region)->header.size = sizeheader_size + get_element_size(element);
517
518 return Ok;
519 }
520
521 /*****************************************************************************
522 * GdipCreateRegionRect [GDIPLUS.@]
523 */
524 GpStatus WINGDIPAPI GdipCreateRegionRect(GDIPCONST GpRectF *rect,
525 GpRegion **region)
526 {
527 GpStatus stat;
528
529 TRACE("%p, %p\n", rect, region);
530
531 if (!(rect && region))
532 return InvalidParameter;
533
534 *region = GdipAlloc(sizeof(GpRegion));
535 stat = init_region(*region, RegionDataRect);
536 if(stat != Ok)
537 {
538 GdipDeleteRegion(*region);
539 return stat;
540 }
541
542 (*region)->node.elementdata.rect.X = rect->X;
543 (*region)->node.elementdata.rect.Y = rect->Y;
544 (*region)->node.elementdata.rect.Width = rect->Width;
545 (*region)->node.elementdata.rect.Height = rect->Height;
546
547 return Ok;
548 }
549
550 /*****************************************************************************
551 * GdipCreateRegionRectI [GDIPLUS.@]
552 */
553 GpStatus WINGDIPAPI GdipCreateRegionRectI(GDIPCONST GpRect *rect,
554 GpRegion **region)
555 {
556 GpRectF rectf;
557
558 TRACE("%p, %p\n", rect, region);
559
560 rectf.X = (REAL)rect->X;
561 rectf.Y = (REAL)rect->Y;
562 rectf.Width = (REAL)rect->Width;
563 rectf.Height = (REAL)rect->Height;
564
565 return GdipCreateRegionRect(&rectf, region);
566 }
567
568 GpStatus WINGDIPAPI GdipCreateRegionRgnData(GDIPCONST BYTE *data, INT size, GpRegion **region)
569 {
570 FIXME("(%p, %d, %p): stub\n", data, size, region);
571
572 *region = NULL;
573 return NotImplemented;
574 }
575
576
577 /******************************************************************************
578 * GdipCreateRegionHrgn [GDIPLUS.@]
579 */
580 GpStatus WINGDIPAPI GdipCreateRegionHrgn(HRGN hrgn, GpRegion **region)
581 {
582 DWORD size;
583 LPRGNDATA buf;
584 LPRECT rect;
585 GpStatus stat;
586 GpPath* path;
587 GpRegion* local;
588 int i;
589
590 TRACE("(%p, %p)\n", hrgn, region);
591
592 if(!region || !(size = GetRegionData(hrgn, 0, NULL)))
593 return InvalidParameter;
594
595 buf = GdipAlloc(size);
596 if(!buf)
597 return OutOfMemory;
598
599 if(!GetRegionData(hrgn, size, buf)){
600 GdipFree(buf);
601 return GenericError;
602 }
603
604 if(buf->rdh.nCount == 0){
605 if((stat = GdipCreateRegion(&local)) != Ok){
606 GdipFree(buf);
607 return stat;
608 }
609 if((stat = GdipSetEmpty(local)) != Ok){
610 GdipFree(buf);
611 GdipDeleteRegion(local);
612 return stat;
613 }
614 *region = local;
615 GdipFree(buf);
616 return Ok;
617 }
618
619 if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok){
620 GdipFree(buf);
621 return stat;
622 }
623
624 rect = (LPRECT)buf->Buffer;
625 for(i = 0; i < buf->rdh.nCount; i++){
626 if((stat = GdipAddPathRectangle(path, (REAL)rect->left, (REAL)rect->top,
627 (REAL)(rect->right - rect->left), (REAL)(rect->bottom - rect->top))) != Ok){
628 GdipFree(buf);
629 GdipDeletePath(path);
630 return stat;
631 }
632 rect++;
633 }
634
635 stat = GdipCreateRegionPath(path, region);
636
637 GdipFree(buf);
638 GdipDeletePath(path);
639 return stat;
640 }
641
642 /*****************************************************************************
643 * GdipDeleteRegion [GDIPLUS.@]
644 */
645 GpStatus WINGDIPAPI GdipDeleteRegion(GpRegion *region)
646 {
647 TRACE("%p\n", region);
648
649 if (!region)
650 return InvalidParameter;
651
652 delete_element(®ion->node);
653 GdipFree(region);
654
655 return Ok;
656 }
657
658 /*****************************************************************************
659 * GdipGetRegionBounds [GDIPLUS.@]
660 */
661 GpStatus WINGDIPAPI GdipGetRegionBounds(GpRegion *region, GpGraphics *graphics, GpRectF *rect)
662 {
663 HRGN hrgn;
664 RECT r;
665 GpStatus status;
666
667 TRACE("(%p, %p, %p)\n", region, graphics, rect);
668
669 if(!region || !graphics || !rect)
670 return InvalidParameter;
671
672 /* Contrary to MSDN, native ignores the graphics transform. */
673 status = GdipGetRegionHRgn(region, NULL, &hrgn);
674 if(status != Ok)
675 return status;
676
677 /* infinite */
678 if(!hrgn){
679 rect->X = rect->Y = -(REAL)(1 << 22);
680 rect->Width = rect->Height = (REAL)(1 << 23);
681 return Ok;
682 }
683
684 if(!GetRgnBox(hrgn, &r)){
685 DeleteObject(hrgn);
686 return GenericError;
687 }
688
689 rect->X = r.left;
690 rect->Y = r.top;
691 rect->Width = r.right - r.left;
692 rect->Height = r.bottom - r.top;
693
694 return Ok;
695 }
696
697 /*****************************************************************************
698 * GdipGetRegionBoundsI [GDIPLUS.@]
699 */
700 GpStatus WINGDIPAPI GdipGetRegionBoundsI(GpRegion *region, GpGraphics *graphics, GpRect *rect)
701 {
702 GpRectF rectf;
703 GpStatus status;
704
705 TRACE("(%p, %p, %p)\n", region, graphics, rect);
706
707 if(!rect)
708 return InvalidParameter;
709
710 status = GdipGetRegionBounds(region, graphics, &rectf);
711 if(status == Ok){
712 rect->X = roundr(rectf.X);
713 rect->Y = roundr(rectf.X);
714 rect->Width = roundr(rectf.Width);
715 rect->Height = roundr(rectf.Height);
716 }
717
718 return status;
719 }
720
721 static inline void write_dword(DWORD* location, INT* offset, const DWORD write)
722 {
723 location[*offset] = write;
724 (*offset)++;
725 }
726
727 static inline void write_float(DWORD* location, INT* offset, const FLOAT write)
728 {
729 ((FLOAT*)location)[*offset] = write;
730 (*offset)++;
731 }
732
733 static inline void write_packed_point(DWORD* location, INT* offset,
734 const GpPointF* write)
735 {
736 packed_point point;
737
738 point.X = write->X;
739 point.Y = write->Y;
740 memcpy(location + *offset, &point, sizeof(packed_point));
741 (*offset)++;
742 }
743
744 static inline void write_path_types(DWORD* location, INT* offset,
745 const GpPath* path)
746 {
747 memcpy(location + *offset, path->pathdata.Types, path->pathdata.Count);
748
749 /* The unwritten parts of the DWORD (if any) must be cleared */
750 if (path->pathdata.Count % sizeof(DWORD))
751 ZeroMemory(((BYTE*)location) + (*offset * sizeof(DWORD)) +
752 path->pathdata.Count,
753 sizeof(DWORD) - path->pathdata.Count % sizeof(DWORD));
754 *offset += (get_pathtypes_size(path) / sizeof(DWORD));
755 }
756
757 static void write_element(const region_element* element, DWORD *buffer,
758 INT* filled)
759 {
760 write_dword(buffer, filled, element->type);
761 switch (element->type)
762 {
763 case CombineModeReplace:
764 case CombineModeIntersect:
765 case CombineModeUnion:
766 case CombineModeXor:
767 case CombineModeExclude:
768 case CombineModeComplement:
769 write_element(element->elementdata.combine.left, buffer, filled);
770 write_element(element->elementdata.combine.right, buffer, filled);
771 break;
772 case RegionDataRect:
773 write_float(buffer, filled, element->elementdata.rect.X);
774 write_float(buffer, filled, element->elementdata.rect.Y);
775 write_float(buffer, filled, element->elementdata.rect.Width);
776 write_float(buffer, filled, element->elementdata.rect.Height);
777 break;
778 case RegionDataPath:
779 {
780 INT i;
781 const GpPath* path = element->elementdata.pathdata.path;
782
783 memcpy(buffer + *filled, &element->elementdata.pathdata.pathheader,
784 sizeof(element->elementdata.pathdata.pathheader));
785 *filled += sizeof(element->elementdata.pathdata.pathheader) / sizeof(DWORD);
786 switch (element->elementdata.pathdata.pathheader.flags)
787 {
788 case FLAGS_NOFLAGS:
789 for (i = 0; i < path->pathdata.Count; i++)
790 {
791 write_float(buffer, filled, path->pathdata.Points[i].X);
792 write_float(buffer, filled, path->pathdata.Points[i].Y);
793 }
794 break;
795 case FLAGS_INTPATH:
796 for (i = 0; i < path->pathdata.Count; i++)
797 {
798 write_packed_point(buffer, filled,
799 &path->pathdata.Points[i]);
800 }
801 }
802 write_path_types(buffer, filled, path);
803 break;
804 }
805 case RegionDataEmptyRect:
806 case RegionDataInfiniteRect:
807 break;
808 }
809 }
810
811 /*****************************************************************************
812 * GdipGetRegionData [GDIPLUS.@]
813 *
814 * Returns the header, followed by combining ops and region elements.
815 *
816 * PARAMS
817 * region [I] region to retrieve from
818 * buffer [O] buffer to hold the resulting data
819 * size [I] size of the buffer
820 * needed [O] (optional) how much data was written
821 *
822 * RETURNS
823 * SUCCESS: Ok
824 * FAILURE: InvalidParameter
825 *
826 * NOTES
827 * The header contains the size, a checksum, a version string, and the number
828 * of children. The size does not count itself or the checksum.
829 * Version is always something like 0xdbc01001 or 0xdbc01002
830 *
831 * An element is a RECT, or PATH; Combining ops are stored as their
832 * CombineMode value. Special regions (infinite, empty) emit just their
833 * op-code; GpRectFs emit their code followed by their points; GpPaths emit
834 * their code followed by a second header for the path followed by the actual
835 * path data. Followed by the flags for each point. The pathheader contains
836 * the size of the data to follow, a version number again, followed by a count
837 * of how many points, and any special flags which may apply. 0x4000 means its
838 * a path of shorts instead of FLOAT.
839 *
840 * Combining Ops are stored in reverse order from when they were constructed;
841 * the output is a tree where the left side combining area is always taken
842 * first.
843 */
844 GpStatus WINGDIPAPI GdipGetRegionData(GpRegion *region, BYTE *buffer, UINT size,
845 UINT *needed)
846 {
847 INT filled = 0;
848
849 TRACE("%p, %p, %d, %p\n", region, buffer, size, needed);
850
851 if (!(region && buffer && size))
852 return InvalidParameter;
853
854 memcpy(buffer, ®ion->header, sizeof(region->header));
855 filled += sizeof(region->header) / sizeof(DWORD);
856 /* With few exceptions, everything written is DWORD aligned,
857 * so use that as our base */
858 write_element(®ion->node, (DWORD*)buffer, &filled);
859
860 if (needed)
861 *needed = filled * sizeof(DWORD);
862
863 return Ok;
864 }
865
866 /*****************************************************************************
867 * GdipGetRegionDataSize [GDIPLUS.@]
868 */
869 GpStatus WINGDIPAPI GdipGetRegionDataSize(GpRegion *region, UINT *needed)
870 {
871 TRACE("%p, %p\n", region, needed);
872
873 if (!(region && needed))
874 return InvalidParameter;
875
876 /* header.size doesn't count header.size and header.checksum */
877 *needed = region->header.size + sizeof(DWORD) * 2;
878
879 return Ok;
880 }
881
882 static GpStatus get_path_hrgn(GpPath *path, GpGraphics *graphics, HRGN *hrgn)
883 {
884 HDC new_hdc=NULL;
885 GpStatus stat;
886 INT save_state;
887
888 if (!graphics)
889 {
890 new_hdc = GetDC(0);
891 if (!new_hdc)
892 return OutOfMemory;
893
894 stat = GdipCreateFromHDC(new_hdc, &graphics);
895 if (stat != Ok)
896 {
897 ReleaseDC(0, new_hdc);
898 return stat;
899 }
900 }
901
902 save_state = SaveDC(graphics->hdc);
903 EndPath(graphics->hdc);
904
905 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
906 : WINDING));
907
908 stat = trace_path(graphics, path);
909 if (stat == Ok)
910 {
911 *hrgn = PathToRegion(graphics->hdc);
912 stat = *hrgn ? Ok : OutOfMemory;
913 }
914
915 RestoreDC(graphics->hdc, save_state);
916 if (new_hdc)
917 {
918 ReleaseDC(0, new_hdc);
919 GdipDeleteGraphics(graphics);
920 }
921
922 return stat;
923 }
924
925 static GpStatus get_region_hrgn(struct region_element *element, GpGraphics *graphics, HRGN *hrgn)
926 {
927 switch (element->type)
928 {
929 case RegionDataInfiniteRect:
930 *hrgn = NULL;
931 return Ok;
932 case RegionDataEmptyRect:
933 *hrgn = CreateRectRgn(0, 0, 0, 0);
934 return *hrgn ? Ok : OutOfMemory;
935 case RegionDataPath:
936 return get_path_hrgn(element->elementdata.pathdata.path, graphics, hrgn);
937 case RegionDataRect:
938 {
939 GpPath* path;
940 GpStatus stat;
941 GpRectF* rc = &element->elementdata.rect;
942
943 stat = GdipCreatePath(FillModeAlternate, &path);
944 if (stat != Ok)
945 return stat;
946 stat = GdipAddPathRectangle(path, rc->X, rc->Y, rc->Width, rc->Height);
947
948 if (stat == Ok)
949 stat = get_path_hrgn(path, graphics, hrgn);
950
951 GdipDeletePath(path);
952
953 return stat;
954 }
955 case CombineModeIntersect:
956 case CombineModeUnion:
957 case CombineModeXor:
958 case CombineModeExclude:
959 case CombineModeComplement:
960 {
961 HRGN left, right;
962 GpStatus stat;
963 int ret;
964
965 stat = get_region_hrgn(element->elementdata.combine.left, graphics, &left);
966 if (stat != Ok)
967 {
968 *hrgn = NULL;
969 return stat;
970 }
971
972 if (left == NULL)
973 {
974 /* existing region is infinite */
975 switch (element->type)
976 {
977 case CombineModeIntersect:
978 return get_region_hrgn(element->elementdata.combine.right, graphics, hrgn);
979 case CombineModeXor: case CombineModeExclude:
980 FIXME("cannot exclude from an infinite region\n");
981 /* fall-through */
982 case CombineModeUnion: case CombineModeComplement:
983 *hrgn = NULL;
984 return Ok;
985 }
986 }
987
988 stat = get_region_hrgn(element->elementdata.combine.right, graphics, &right);
989 if (stat != Ok)
990 {
991 DeleteObject(left);
992 *hrgn = NULL;
993 return stat;
994 }
995
996 if (right == NULL)
997 {
998 /* new region is infinite */
999 switch (element->type)
1000 {
1001 case CombineModeIntersect:
1002 *hrgn = left;
1003 return Ok;
1004 case CombineModeXor: case CombineModeComplement:
1005 FIXME("cannot exclude from an infinite region\n");
1006 /* fall-through */
1007 case CombineModeUnion: case CombineModeExclude:
1008 DeleteObject(left);
1009 *hrgn = NULL;
1010 return Ok;
1011 }
1012 }
1013
1014 switch (element->type)
1015 {
1016 case CombineModeIntersect:
1017 ret = CombineRgn(left, left, right, RGN_AND);
1018 break;
1019 case CombineModeUnion:
1020 ret = CombineRgn(left, left, right, RGN_OR);
1021 break;
1022 case CombineModeXor:
1023 ret = CombineRgn(left, left, right, RGN_XOR);
1024 break;
1025 case CombineModeExclude:
1026 ret = CombineRgn(left, left, right, RGN_DIFF);
1027 break;
1028 case CombineModeComplement:
1029 ret = CombineRgn(left, right, left, RGN_DIFF);
1030 break;
1031 default:
1032 ret = ERROR;
1033 }
1034
1035 DeleteObject(right);
1036
1037 if (ret == ERROR)
1038 {
1039 DeleteObject(left);
1040 *hrgn = NULL;
1041 return GenericError;
1042 }
1043
1044 *hrgn = left;
1045 return Ok;
1046 }
1047 default:
1048 FIXME("GdipGetRegionHRgn unimplemented for region type=%x\n", element->type);
1049 *hrgn = NULL;
1050 return NotImplemented;
1051 }
1052 }
1053
1054 /*****************************************************************************
1055 * GdipGetRegionHRgn [GDIPLUS.@]
1056 */
1057 GpStatus WINGDIPAPI GdipGetRegionHRgn(GpRegion *region, GpGraphics *graphics, HRGN *hrgn)
1058 {
1059 TRACE("(%p, %p, %p)\n", region, graphics, hrgn);
1060
1061 if (!region || !hrgn)
1062 return InvalidParameter;
1063
1064 return get_region_hrgn(®ion->node, graphics, hrgn);
1065 }
1066
1067 GpStatus WINGDIPAPI GdipIsEmptyRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1068 {
1069 TRACE("(%p, %p, %p)\n", region, graphics, res);
1070
1071 if(!region || !graphics || !res)
1072 return InvalidParameter;
1073
1074 *res = (region->node.type == RegionDataEmptyRect);
1075
1076 return Ok;
1077 }
1078
1079 /*****************************************************************************
1080 * GdipIsEqualRegion [GDIPLUS.@]
1081 */
1082 GpStatus WINGDIPAPI GdipIsEqualRegion(GpRegion *region, GpRegion *region2, GpGraphics *graphics,
1083 BOOL *res)
1084 {
1085 HRGN hrgn1, hrgn2;
1086 GpStatus stat;
1087
1088 TRACE("(%p, %p, %p, %p)\n", region, region2, graphics, res);
1089
1090 if(!region || !region2 || !graphics || !res)
1091 return InvalidParameter;
1092
1093 stat = GdipGetRegionHRgn(region, graphics, &hrgn1);
1094 if(stat != Ok)
1095 return stat;
1096 stat = GdipGetRegionHRgn(region2, graphics, &hrgn2);
1097 if(stat != Ok){
1098 DeleteObject(hrgn1);
1099 return stat;
1100 }
1101
1102 *res = EqualRgn(hrgn1, hrgn2);
1103
1104 /* one of GpRegions is infinite */
1105 if(*res == ERROR)
1106 *res = (!hrgn1 && !hrgn2);
1107
1108 DeleteObject(hrgn1);
1109 DeleteObject(hrgn2);
1110
1111 return Ok;
1112 }
1113
1114 /*****************************************************************************
1115 * GdipIsInfiniteRegion [GDIPLUS.@]
1116 */
1117 GpStatus WINGDIPAPI GdipIsInfiniteRegion(GpRegion *region, GpGraphics *graphics, BOOL *res)
1118 {
1119 /* I think graphics is ignored here */
1120 TRACE("(%p, %p, %p)\n", region, graphics, res);
1121
1122 if(!region || !graphics || !res)
1123 return InvalidParameter;
1124
1125 *res = (region->node.type == RegionDataInfiniteRect);
1126
1127 return Ok;
1128 }
1129
1130 /*****************************************************************************
1131 * GdipIsVisibleRegionRect [GDIPLUS.@]
1132 */
1133 GpStatus WINGDIPAPI GdipIsVisibleRegionRect(GpRegion* region, REAL x, REAL y, REAL w, REAL h, GpGraphics *graphics, BOOL *res)
1134 {
1135 HRGN hrgn;
1136 GpStatus stat;
1137 RECT rect;
1138
1139 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %p, %p)\n", region, x, y, w, h, graphics, res);
1140
1141 if(!region || !res)
1142 return InvalidParameter;
1143
1144 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1145 return stat;
1146
1147 /* infinite */
1148 if(!hrgn){
1149 *res = TRUE;
1150 return Ok;
1151 }
1152
1153 rect.left = ceilr(x);
1154 rect.top = ceilr(y);
1155 rect.right = ceilr(x + w);
1156 rect.bottom = ceilr(y + h);
1157
1158 *res = RectInRegion(hrgn, &rect);
1159
1160 DeleteObject(hrgn);
1161
1162 return Ok;
1163 }
1164
1165 /*****************************************************************************
1166 * GdipIsVisibleRegionRectI [GDIPLUS.@]
1167 */
1168 GpStatus WINGDIPAPI GdipIsVisibleRegionRectI(GpRegion* region, INT x, INT y, INT w, INT h, GpGraphics *graphics, BOOL *res)
1169 {
1170 TRACE("(%p, %d, %d, %d, %d, %p, %p)\n", region, x, y, w, h, graphics, res);
1171 if(!region || !res)
1172 return InvalidParameter;
1173
1174 return GdipIsVisibleRegionRect(region, (REAL)x, (REAL)y, (REAL)w, (REAL)h, graphics, res);
1175 }
1176
1177 /*****************************************************************************
1178 * GdipIsVisibleRegionPoint [GDIPLUS.@]
1179 */
1180 GpStatus WINGDIPAPI GdipIsVisibleRegionPoint(GpRegion* region, REAL x, REAL y, GpGraphics *graphics, BOOL *res)
1181 {
1182 HRGN hrgn;
1183 GpStatus stat;
1184
1185 TRACE("(%p, %.2f, %.2f, %p, %p)\n", region, x, y, graphics, res);
1186
1187 if(!region || !res)
1188 return InvalidParameter;
1189
1190 if((stat = GdipGetRegionHRgn(region, NULL, &hrgn)) != Ok)
1191 return stat;
1192
1193 /* infinite */
1194 if(!hrgn){
1195 *res = TRUE;
1196 return Ok;
1197 }
1198
1199 *res = PtInRegion(hrgn, roundr(x), roundr(y));
1200
1201 DeleteObject(hrgn);
1202
1203 return Ok;
1204 }
1205
1206 /*****************************************************************************
1207 * GdipIsVisibleRegionPointI [GDIPLUS.@]
1208 */
1209 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion* region, INT x, INT y, GpGraphics *graphics, BOOL *res)
1210 {
1211 TRACE("(%p, %d, %d, %p, %p)\n", region, x, y, graphics, res);
1212
1213 return GdipIsVisibleRegionPoint(region, (REAL)x, (REAL)y, graphics, res);
1214 }
1215
1216 /*****************************************************************************
1217 * GdipSetEmpty [GDIPLUS.@]
1218 */
1219 GpStatus WINGDIPAPI GdipSetEmpty(GpRegion *region)
1220 {
1221 GpStatus stat;
1222
1223 TRACE("%p\n", region);
1224
1225 if (!region)
1226 return InvalidParameter;
1227
1228 delete_element(®ion->node);
1229 stat = init_region(region, RegionDataEmptyRect);
1230
1231 return stat;
1232 }
1233
1234 GpStatus WINGDIPAPI GdipSetInfinite(GpRegion *region)
1235 {
1236 GpStatus stat;
1237
1238 TRACE("%p\n", region);
1239
1240 if (!region)
1241 return InvalidParameter;
1242
1243 delete_element(®ion->node);
1244 stat = init_region(region, RegionDataInfiniteRect);
1245
1246 return stat;
1247 }
1248
1249 GpStatus WINGDIPAPI GdipTransformRegion(GpRegion *region, GpMatrix *matrix)
1250 {
1251 FIXME("(%p, %p): stub\n", region, matrix);
1252
1253 return NotImplemented;
1254 }
1255
1256 /* Translates GpRegion elements with specified offsets */
1257 static void translate_region_element(region_element* element, REAL dx, REAL dy)
1258 {
1259 INT i;
1260
1261 switch(element->type)
1262 {
1263 case RegionDataEmptyRect:
1264 case RegionDataInfiniteRect:
1265 return;
1266 case RegionDataRect:
1267 element->elementdata.rect.X += dx;
1268 element->elementdata.rect.Y += dy;
1269 return;
1270 case RegionDataPath:
1271 for(i = 0; i < element->elementdata.pathdata.path->pathdata.Count; i++){
1272 element->elementdata.pathdata.path->pathdata.Points[i].X += dx;
1273 element->elementdata.pathdata.path->pathdata.Points[i].Y += dy;
1274 }
1275 return;
1276 default:
1277 translate_region_element(element->elementdata.combine.left, dx, dy);
1278 translate_region_element(element->elementdata.combine.right, dx, dy);
1279 return;
1280 }
1281 }
1282
1283 /*****************************************************************************
1284 * GdipTranslateRegion [GDIPLUS.@]
1285 */
1286 GpStatus WINGDIPAPI GdipTranslateRegion(GpRegion *region, REAL dx, REAL dy)
1287 {
1288 TRACE("(%p, %f, %f)\n", region, dx, dy);
1289
1290 if(!region)
1291 return InvalidParameter;
1292
1293 translate_region_element(®ion->node, dx, dy);
1294
1295 return Ok;
1296 }
1297
1298 /*****************************************************************************
1299 * GdipTranslateRegionI [GDIPLUS.@]
1300 */
1301 GpStatus WINGDIPAPI GdipTranslateRegionI(GpRegion *region, INT dx, INT dy)
1302 {
1303 TRACE("(%p, %d, %d)\n", region, dx, dy);
1304
1305 return GdipTranslateRegion(region, (REAL)dx, (REAL)dy);
1306 }
1307
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.