2017-05-11 09:27:34 -05:00
|
|
|
// 11 may 2017
|
2017-05-19 13:12:04 -05:00
|
|
|
#import "uipriv_darwin.h"
|
2017-05-11 09:27:34 -05:00
|
|
|
|
|
|
|
struct uiOpenTypeFeatures {
|
2017-05-19 13:12:04 -05:00
|
|
|
NSMutableDictionary *tags;
|
2017-05-11 09:27:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
uiOpenTypeFeatures *uiNewOpenTypeFeatures(void)
|
|
|
|
{
|
|
|
|
uiOpenTypeFeatures *otf;
|
|
|
|
|
|
|
|
otf = uiNew(uiOpenTypeFeatures);
|
2017-05-19 13:12:04 -05:00
|
|
|
otf->tags = [NSMutableDictionary new];
|
2017-05-11 09:27:34 -05:00
|
|
|
return otf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiFreeOpenTypeFeatures(uiOpenTypeFeatures *otf)
|
|
|
|
{
|
2017-05-19 13:12:04 -05:00
|
|
|
[otf->tags release];
|
2017-05-11 09:27:34 -05:00
|
|
|
uiFree(otf);
|
|
|
|
}
|
|
|
|
|
2017-05-30 08:52:38 -05:00
|
|
|
uiOpenTypeFeatures *uiOpenTypeFeaturesClone(const uiOpenTypeFeatures *otf)
|
2017-05-11 09:27:34 -05:00
|
|
|
{
|
|
|
|
uiOpenTypeFeatures *out;
|
|
|
|
|
|
|
|
out = uiNew(uiOpenTypeFeatures);
|
2017-05-19 13:12:04 -05:00
|
|
|
out->tags = [otf->tags mutableCopy];
|
2017-05-11 09:27:34 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-05-19 13:12:04 -05:00
|
|
|
// why are there no NSNumber methods for stdint.h or the equivalent core foundation types?...
|
|
|
|
#define mkMapObject(tag) [NSNumber numberWithUnsignedLongLong:((unsigned long long) tag)]
|
|
|
|
#define mapObjectValue(num) ((uint32_t) [num unsignedLongLongValue])
|
|
|
|
|
2017-05-11 09:27:34 -05:00
|
|
|
void uiOpenTypeFeaturesAdd(uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value)
|
|
|
|
{
|
2017-05-19 13:12:04 -05:00
|
|
|
NSNumber *tn, *vn;
|
|
|
|
|
|
|
|
tn = mkMapObject(mkTag(a, b, c, d));
|
|
|
|
vn = mkMapObject(value);
|
|
|
|
[otf->tags setObject:vn forKey:tn];
|
2017-05-11 09:27:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b, char c, char d)
|
|
|
|
{
|
2017-05-19 13:12:04 -05:00
|
|
|
NSNumber *tn;
|
|
|
|
|
|
|
|
tn = mkMapObject(mkTag(a, b, c, d));
|
2017-06-18 19:10:42 -05:00
|
|
|
// documented as doing nothing if tn is not in otf->tags
|
2017-05-19 13:12:04 -05:00
|
|
|
[otf->tags removeObjectForKey:tn];
|
2017-05-11 09:27:34 -05:00
|
|
|
}
|
|
|
|
|
2017-06-19 13:50:03 -05:00
|
|
|
// TODO will the const wreck stuff up?
|
|
|
|
int uiOpenTypeFeaturesGet(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value)
|
2017-05-11 09:27:34 -05:00
|
|
|
{
|
2017-05-19 13:12:04 -05:00
|
|
|
NSNumber *tn, *vn;
|
|
|
|
|
|
|
|
tn = mkMapObject(mkTag(a, b, c, d));
|
|
|
|
vn = (NSNumber *) [otf->tags objectForKey:tn];
|
|
|
|
if (vn == nil)
|
|
|
|
return 0;
|
|
|
|
*value = mapObjectValue(vn);
|
|
|
|
// TODO release vn?
|
|
|
|
return 1;
|
2017-05-11 09:27:34 -05:00
|
|
|
}
|
|
|
|
|
2017-05-30 13:00:58 -05:00
|
|
|
void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeaturesForEachFunc f, void *data)
|
2017-05-11 09:27:34 -05:00
|
|
|
{
|
2017-05-19 13:12:04 -05:00
|
|
|
[otf->tags enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
|
|
|
|
NSNumber *tn = (NSNumber *) key;
|
|
|
|
NSNumber *vn = (NSNumber *) value;
|
|
|
|
uint32_t tag;
|
|
|
|
uint8_t a, b, c, d;
|
2017-06-06 11:47:07 -05:00
|
|
|
uiForEach ret;
|
2017-05-19 13:12:04 -05:00
|
|
|
|
|
|
|
tag = mapObjectValue(tn);
|
|
|
|
a = (uint8_t) ((tag >> 24) & 0xFF);
|
|
|
|
b = (uint8_t) ((tag >> 16) & 0xFF);
|
|
|
|
c = (uint8_t) ((tag >> 8) & 0xFF);
|
|
|
|
d = (uint8_t) (tag & 0xFF);
|
2017-06-19 13:50:03 -05:00
|
|
|
ret = (*f)(otf, (char) a, (char) b, (char) c, (char) d,
|
2017-05-19 13:12:04 -05:00
|
|
|
mapObjectValue(vn), data);
|
2017-06-06 11:47:07 -05:00
|
|
|
// TODO for all: require exact match?
|
|
|
|
if (ret == uiForEachStop)
|
|
|
|
*stop = YES;
|
2017-05-19 13:12:04 -05:00
|
|
|
}];
|
2017-05-11 09:27:34 -05:00
|
|
|
}
|
|
|
|
|
2017-05-30 08:52:38 -05:00
|
|
|
int uiOpenTypeFeaturesEqual(const uiOpenTypeFeatures *a, const uiOpenTypeFeatures *b)
|
2017-05-11 09:27:34 -05:00
|
|
|
{
|
2017-06-09 18:59:48 -05:00
|
|
|
if (a == NULL && b == NULL)
|
|
|
|
return 1;
|
|
|
|
if (a == NULL || b == NULL)
|
|
|
|
return 0;
|
2017-05-19 13:12:04 -05:00
|
|
|
return [a->tags isEqualToDictionary:b->tags];
|
2017-05-11 09:27:34 -05:00
|
|
|
}
|
|
|
|
|
2017-05-30 13:00:58 -05:00
|
|
|
// TODO explain all this
|
|
|
|
// TODO rename outerArray and innerDict (the names made sense when this was part of fontdescAppendFeatures(), but not here)
|
|
|
|
// TODO make all this use enumerateKeysAndObjects (which requires duplicating code)?
|
2017-06-19 13:50:03 -05:00
|
|
|
static uiForEach otfArrayForEachAAT(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data)
|
2017-05-30 13:00:58 -05:00
|
|
|
{
|
|
|
|
CFMutableArrayRef outerArray = (CFMutableArrayRef) data;
|
|
|
|
|
|
|
|
openTypeToAAT(a, b, c, d, value, ^(uint16_t type, uint16_t selector) {
|
|
|
|
CFDictionaryRef innerDict;
|
|
|
|
CFNumberRef numType, numSelector;
|
2017-05-30 14:24:31 -05:00
|
|
|
// not well documented, but fixed-size arrays don't support __block either (VLAs are documented as being unsupported)
|
|
|
|
const void *keys[2], *values[2];
|
2017-05-30 13:00:58 -05:00
|
|
|
|
2017-05-30 14:24:31 -05:00
|
|
|
keys[0] = kCTFontFeatureTypeIdentifierKey;
|
|
|
|
keys[1] = kCTFontFeatureSelectorIdentifierKey;
|
2017-05-30 13:00:58 -05:00
|
|
|
numType = CFNumberCreate(NULL, kCFNumberSInt16Type,
|
|
|
|
(const SInt16 *) (&type));
|
|
|
|
numSelector = CFNumberCreate(NULL, kCFNumberSInt16Type,
|
|
|
|
(const SInt16 *) (&selector));
|
|
|
|
values[0] = numType;
|
|
|
|
values[1] = numSelector;
|
|
|
|
innerDict = CFDictionaryCreate(NULL,
|
|
|
|
keys, values, 2,
|
|
|
|
// TODO are these correct?
|
|
|
|
&kCFCopyStringDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
if (innerDict == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
2017-05-30 14:24:31 -05:00
|
|
|
CFArrayAppendValue(outerArray, innerDict);
|
2017-05-30 13:00:58 -05:00
|
|
|
CFRelease(innerDict);
|
|
|
|
CFRelease(numSelector);
|
|
|
|
CFRelease(numType);
|
|
|
|
});
|
2017-06-06 11:47:07 -05:00
|
|
|
return uiForEachContinue;
|
2017-05-30 13:00:58 -05:00
|
|
|
}
|
|
|
|
|
2017-06-01 09:57:34 -05:00
|
|
|
// TODO find out which fonts differ in AAT small caps and test them with this
|
2017-06-19 13:50:03 -05:00
|
|
|
static uiForEach otfArrayForEachOT(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data)
|
2017-06-01 09:57:34 -05:00
|
|
|
{
|
|
|
|
CFMutableArrayRef outerArray = (CFMutableArrayRef) data;
|
|
|
|
CFDictionaryRef innerDict;
|
|
|
|
// TODO rename this to tagstr (and all the other variables likewise...)
|
|
|
|
CFStringRef strTag;
|
|
|
|
CFNumberRef numValue;
|
|
|
|
char tagcstr[5];
|
|
|
|
const void *keys[2], *values[2];
|
|
|
|
|
|
|
|
tagcstr[0] = a;
|
|
|
|
tagcstr[1] = b;
|
|
|
|
tagcstr[2] = c;
|
|
|
|
tagcstr[3] = d;
|
|
|
|
tagcstr[4] = '\0';
|
|
|
|
keys[0] = *FUTURE_kCTFontOpenTypeFeatureTag;
|
|
|
|
keys[1] = *FUTURE_kCTFontOpenTypeFeatureValue;
|
|
|
|
strTag = CFStringCreateWithCString(NULL, tagcstr, kCFStringEncodingUTF8);
|
|
|
|
if (strTag == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
numValue = CFNumberCreate(NULL, kCFNumberSInt32Type,
|
|
|
|
(const SInt32 *) (&value));
|
|
|
|
values[0] = strTag;
|
|
|
|
values[1] = numValue;
|
|
|
|
innerDict = CFDictionaryCreate(NULL,
|
|
|
|
keys, values, 2,
|
|
|
|
// TODO are these correct?
|
|
|
|
&kCFCopyStringDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
if (innerDict == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
CFArrayAppendValue(outerArray, innerDict);
|
|
|
|
CFRelease(innerDict);
|
|
|
|
CFRelease(numValue);
|
|
|
|
CFRelease(strTag);
|
2017-06-06 11:47:07 -05:00
|
|
|
return uiForEachContinue;
|
2017-06-01 09:57:34 -05:00
|
|
|
}
|
|
|
|
|
2017-05-30 13:00:58 -05:00
|
|
|
CFArrayRef otfToFeaturesArray(const uiOpenTypeFeatures *otf)
|
|
|
|
{
|
|
|
|
CFMutableArrayRef outerArray;
|
2017-06-01 09:57:34 -05:00
|
|
|
uiOpenTypeFeaturesForEachFunc f;
|
2017-05-30 13:00:58 -05:00
|
|
|
|
|
|
|
outerArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
|
|
|
|
if (outerArray == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
2017-06-01 09:57:34 -05:00
|
|
|
f = otfArrayForEachAAT;
|
|
|
|
if (FUTURE_kCTFontOpenTypeFeatureTag != NULL && FUTURE_kCTFontOpenTypeFeatureValue != NULL)
|
|
|
|
f = otfArrayForEachOT;
|
|
|
|
uiOpenTypeFeaturesForEach(otf, f, outerArray);
|
2017-05-30 13:00:58 -05:00
|
|
|
return outerArray;
|
|
|
|
}
|