Implement CGPointMakeWithDictionaryRepresentation and CGPointCreateDictionaryRepresentation

This commit is contained in:
Lubos Dolezel 2018-07-27 01:37:39 +02:00
parent 76b2fdef29
commit cc581ba7b7
2 changed files with 52 additions and 0 deletions

View File

@ -69,3 +69,48 @@ CGRect CGRectUnion(CGRect a, CGRect b) {
CGFloat maxY = MAX(CGRectGetMaxY(a), CGRectGetMaxY(b));
return CGRectMake(minX, minY, maxX - minX, maxY - minY);
}
CFDictionaryRef CGPointCreateDictionaryRepresentation(CGPoint point)
{
CFDictionaryRef dict;
CFStringRef keys[] = {
CFSTR("X"), CFSTR("Y")
};
CFNumberRef values[2];
values[0] = CFNumberCreate(NULL, kCFNumberCGFloatType, &point.x);
values[1] = CFNumberCreate(NULL, kCFNumberCGFloatType, &point.y);
dict = CFDictionaryCreate(NULL, (const void**) keys, (const void**) values, 2,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CRelease(values[0]);
CRelease(values[1]);
return dict;
}
bool CGPointMakeWithDictionaryRepresentation(CFDictionaryRef dict, CGPoint *point)
{
if (!dict || !point)
return NO;
CFNumberRef num;
num = (CFNumberRef) CFDictionaryGetValue(dect, CFSTR("X"));
if (CFGetTypeID(num) != CFNumberGetTypeID())
return NO;
if (!CFNumberGetValue(num, kCFNumberCGFloatType, &point->x))
return NO;
num = (CFNumberRef) CFDictionaryGetValue(dect, CFSTR("Y"));
if (CFGetTypeID(num) != CFNumberGetTypeID())
return NO;
if (!CFNumberGetValue(num, kCFNumberCGFloatType, &point->y))
return NO;
return YES;
}

View File

@ -8,6 +8,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#import <CoreGraphics/CoreGraphicsExport.h>
#import <CoreFoundation/CFBase.h>
#import <CoreFoundation/CFDictionary.h>
#include <stdbool.h>
#include <CoreGraphics/CGBase.h>
@ -161,3 +162,9 @@ static inline bool CGRectContainsRect(CGRect a, CGRect b) {
CGRectGetMinY(b) >= CGRectGetMinY(a) &&
CGRectGetMaxY(b) <= CGRectGetMaxY(a));
}
CFDictionaryRef CGPointCreateDictionaryRepresentation(CGPoint point);
bool CGPointMakeWithDictionaryRepresentation(CFDictionaryRef dict, CGPoint *point);