2017-11-01 19:14:44 -05:00
|
|
|
// 1 november 2017
|
|
|
|
#import "uipriv_darwin.h"
|
2018-03-04 17:01:08 -06:00
|
|
|
#import "attrstr.h"
|
2017-11-01 19:14:44 -05:00
|
|
|
|
2017-11-02 20:51:00 -05:00
|
|
|
// This is the part of the font style matching and normalization code
|
|
|
|
// that handles fonts that use a traits dictionary.
|
|
|
|
//
|
|
|
|
// Matching stupidity: Core Text requires an **exact match for the
|
|
|
|
// entire traits dictionary**, otherwise it will **drop ALL the traits**.
|
|
|
|
//
|
|
|
|
// Normalization stupidity: Core Text uses its own scaled values for
|
|
|
|
// weight and width, but the values are different if the font is not
|
|
|
|
// registered and if said font is TrueType or OpenType. The values
|
|
|
|
// for all other cases do have some named constants starting with
|
|
|
|
// OS X 10.11, but even these aren't very consistent in practice.
|
|
|
|
//
|
|
|
|
// Of course, none of this is documented anywhere, so I had to do
|
|
|
|
// both trial-and-error AND reverse engineering to figure out what's
|
|
|
|
// what. We'll just convert Core Text's values into libui constants
|
|
|
|
// and use those for matching.
|
|
|
|
|
2018-03-04 17:01:08 -06:00
|
|
|
static BOOL fontRegistered(uiprivFontStyleData *d)
|
2017-11-01 19:14:44 -05:00
|
|
|
{
|
2017-11-03 17:52:15 -05:00
|
|
|
if (![d hasRegistrationScope])
|
2017-11-01 19:14:44 -05:00
|
|
|
// header says this should be treated as the same as not registered
|
|
|
|
return NO;
|
|
|
|
// examination of Core Text shows this is accurate
|
2017-11-03 17:52:15 -05:00
|
|
|
return [d registrationScope] != kCTFontManagerScopeNone;
|
2017-11-01 19:14:44 -05:00
|
|
|
}
|
|
|
|
|
2017-11-01 19:23:52 -05:00
|
|
|
// Core Text does (usWidthClass / 10) - 0.5 here.
|
|
|
|
// This roughly maps to our values with increments of 0.1, except for the fact 0 and 10 are allowed by Core Text, despite being banned by TrueType and OpenType themselves.
|
|
|
|
// We'll just treat them as identical to 1 and 9, respectively.
|
2018-03-09 17:01:23 -06:00
|
|
|
static const uiTextStretch os2WidthsToStretches[] = {
|
|
|
|
uiTextStretchUltraCondensed,
|
|
|
|
uiTextStretchUltraCondensed,
|
|
|
|
uiTextStretchExtraCondensed,
|
|
|
|
uiTextStretchCondensed,
|
|
|
|
uiTextStretchSemiCondensed,
|
|
|
|
uiTextStretchNormal,
|
|
|
|
uiTextStretchSemiExpanded,
|
|
|
|
uiTextStretchExpanded,
|
|
|
|
uiTextStretchExtraExpanded,
|
|
|
|
uiTextStretchUltraExpanded,
|
|
|
|
uiTextStretchUltraExpanded,
|
2017-11-01 19:14:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static const CFStringRef exceptions[] = {
|
|
|
|
CFSTR("LucidaGrande"),
|
|
|
|
CFSTR(".LucidaGrandeUI"),
|
|
|
|
CFSTR("STHeiti"),
|
|
|
|
CFSTR("STXihei"),
|
|
|
|
CFSTR("TimesNewRomanPSMT"),
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2018-03-08 22:04:45 -06:00
|
|
|
static void trySecondaryOS2Values(uiprivFontStyleData *d, uiFontDescriptor *out, BOOL *hasWeight, BOOL *hasWidth)
|
2017-11-01 19:14:44 -05:00
|
|
|
{
|
|
|
|
CFDataRef os2;
|
|
|
|
uint16_t usWeightClass, usWidthClass;
|
|
|
|
CFStringRef psname;
|
2017-11-03 19:59:27 -05:00
|
|
|
const CFStringRef *ex;
|
2017-11-01 19:14:44 -05:00
|
|
|
|
|
|
|
*hasWeight = NO;
|
|
|
|
*hasWidth = NO;
|
|
|
|
|
|
|
|
// only applies to unregistered fonts
|
2017-11-03 17:52:15 -05:00
|
|
|
if (fontRegistered(d))
|
2017-11-01 19:14:44 -05:00
|
|
|
return;
|
|
|
|
|
2017-11-03 17:52:15 -05:00
|
|
|
os2 = [d table:kCTFontTableOS2];
|
|
|
|
if (os2 == NULL)
|
2017-11-01 19:14:44 -05:00
|
|
|
// no OS2 table, so no secondary values
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CFDataGetLength(os2) > 77) {
|
|
|
|
const UInt8 *b;
|
|
|
|
|
|
|
|
b = CFDataGetBytePtr(os2);
|
|
|
|
|
|
|
|
usWeightClass = ((uint16_t) (b[4])) << 8;
|
|
|
|
usWeightClass |= (uint16_t) (b[5]);
|
|
|
|
if (usWeightClass <= 1000) {
|
|
|
|
if (usWeightClass < 11)
|
2017-11-03 19:59:27 -05:00
|
|
|
usWeightClass *= 100;
|
2017-11-01 19:14:44 -05:00
|
|
|
*hasWeight = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
usWidthClass = ((uint16_t) (b[6])) << 8;
|
|
|
|
usWidthClass |= (uint16_t) (b[7]);
|
|
|
|
if (usWidthClass <= 10)
|
|
|
|
*hasWidth = YES;
|
|
|
|
} else {
|
|
|
|
usWeightClass = 0;
|
|
|
|
*hasWeight = YES;
|
|
|
|
|
|
|
|
usWidthClass = 0;
|
|
|
|
*hasWidth = YES;
|
|
|
|
}
|
|
|
|
if (*hasWeight)
|
|
|
|
// we can just use this directly
|
|
|
|
out->Weight = usWeightClass;
|
|
|
|
if (*hasWidth)
|
|
|
|
out->Stretch = os2WidthsToStretches[usWidthClass];
|
|
|
|
CFRelease(os2);
|
|
|
|
|
|
|
|
// don't use secondary weights in the event of special predefined names
|
2017-11-03 17:52:15 -05:00
|
|
|
psname = [d postScriptName];
|
2017-11-01 19:14:44 -05:00
|
|
|
for (ex = exceptions; *ex != NULL; ex++)
|
|
|
|
if (CFEqual(psname, *ex)) {
|
|
|
|
*hasWeight = NO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:52:15 -05:00
|
|
|
static BOOL testTTFOTFSubfamilyName(CFStringRef name, CFStringRef want)
|
|
|
|
{
|
|
|
|
CFRange range;
|
2017-11-01 19:20:12 -05:00
|
|
|
|
2017-11-03 17:52:15 -05:00
|
|
|
if (name == NULL)
|
|
|
|
return NO;
|
|
|
|
range.location = 0;
|
|
|
|
range.length = CFStringGetLength(name);
|
|
|
|
return CFStringFindWithOptions(name, want, range,
|
|
|
|
(kCFCompareCaseInsensitive | kCFCompareBackwards | kCFCompareNonliteral), NULL) != false;
|
|
|
|
}
|
2017-11-01 19:14:44 -05:00
|
|
|
|
2018-03-04 17:01:08 -06:00
|
|
|
static BOOL testTTFOTFSubfamilyNames(uiprivFontStyleData *d, CFStringRef want)
|
2017-11-01 19:14:44 -05:00
|
|
|
{
|
2017-11-03 17:52:15 -05:00
|
|
|
switch ([d fontFormat]) {
|
2017-11-01 19:14:44 -05:00
|
|
|
case kCTFontFormatOpenTypePostScript:
|
|
|
|
case kCTFontFormatOpenTypeTrueType:
|
|
|
|
case kCTFontFormatTrueType:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:52:15 -05:00
|
|
|
if (testTTFOTFSubfamilyName([d preferredSubFamilyName], want))
|
|
|
|
return YES;
|
|
|
|
if (testTTFOTFSubfamilyName([d subFamilyName], want))
|
|
|
|
return YES;
|
|
|
|
if (testTTFOTFSubfamilyName([d fullName], want))
|
|
|
|
return YES;
|
|
|
|
if (testTTFOTFSubfamilyName([d preferredFamilyName], want))
|
|
|
|
return YES;
|
|
|
|
return testTTFOTFSubfamilyName([d familyName], want);
|
2017-11-01 19:14:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// work around a bug in libFontRegistry.dylib
|
2018-03-04 17:01:08 -06:00
|
|
|
static BOOL shouldReallyBeThin(uiprivFontStyleData *d)
|
2017-11-01 19:14:44 -05:00
|
|
|
{
|
2017-11-03 19:59:27 -05:00
|
|
|
return testTTFOTFSubfamilyNames(d, CFSTR("W1"));
|
2017-11-01 19:14:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// work around a bug in libFontRegistry.dylib
|
2018-03-04 17:01:08 -06:00
|
|
|
static BOOL shouldReallyBeSemiCondensed(uiprivFontStyleData *d)
|
2017-11-01 19:14:44 -05:00
|
|
|
{
|
2017-11-03 19:59:27 -05:00
|
|
|
return testTTFOTFSubfamilyNames(d, CFSTR("Semi Condensed"));
|
2017-11-01 19:14:44 -05:00
|
|
|
}
|
|
|
|
|
2018-03-08 22:04:45 -06:00
|
|
|
void uiprivProcessFontTraits(uiprivFontStyleData *d, uiFontDescriptor *out)
|
2017-11-01 19:14:44 -05:00
|
|
|
{
|
2017-11-03 17:52:15 -05:00
|
|
|
double weight, width;
|
2017-11-01 19:14:44 -05:00
|
|
|
BOOL hasWeight, hasWidth;
|
|
|
|
|
|
|
|
hasWeight = NO;
|
|
|
|
hasWidth = NO;
|
2017-11-03 17:52:15 -05:00
|
|
|
trySecondaryOS2Values(d, out, &hasWeight, &hasWidth);
|
|
|
|
|
|
|
|
weight = [d weight];
|
|
|
|
width = [d width];
|
2017-11-01 19:14:44 -05:00
|
|
|
|
|
|
|
if (!hasWeight)
|
|
|
|
// TODO this scale is a bit lopsided
|
|
|
|
if (weight <= -0.7)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightThin;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= -0.5)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightUltraLight;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= -0.3)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightLight;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= -0.23) {
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightBook;
|
2017-11-03 17:52:15 -05:00
|
|
|
if (shouldReallyBeThin(d))
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightThin;
|
2017-11-01 19:14:44 -05:00
|
|
|
} else if (weight <= 0.0)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightNormal;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= 0.23)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightMedium;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= 0.3)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightSemiBold;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= 0.4)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightBold;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= 0.5)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightUltraBold;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (weight <= 0.7)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightHeavy;
|
2017-11-01 19:14:44 -05:00
|
|
|
else
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Weight = uiTextWeightUltraHeavy;
|
2017-11-01 19:14:44 -05:00
|
|
|
|
|
|
|
if (!hasWidth)
|
|
|
|
// TODO this scale is a bit lopsided
|
|
|
|
if (width <= -0.7) {
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchUltraCondensed;
|
2017-11-03 17:52:15 -05:00
|
|
|
if (shouldReallyBeSemiCondensed(d))
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchSemiCondensed;
|
2017-11-01 19:14:44 -05:00
|
|
|
} else if (width <= -0.5)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchExtraCondensed;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (width <= -0.2)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchCondensed;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (width <= -0.1)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchSemiCondensed;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (width <= 0.0)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchNormal;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (width <= 0.1)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchSemiExpanded;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (width <= 0.2)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchExpanded;
|
2017-11-01 19:14:44 -05:00
|
|
|
else if (width <= 0.6)
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchExtraExpanded;
|
2017-11-01 19:14:44 -05:00
|
|
|
else
|
2018-03-09 17:01:23 -06:00
|
|
|
out->Stretch = uiTextStretchUltraExpanded;
|
2017-11-01 19:14:44 -05:00
|
|
|
}
|