#include "include/core/SkTypes.h"
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
#include "src/utils/mac/SkCTFontCreateExactCopy.h"
#include "src/ports/SkTypeface_mac_ct.h"
#include "src/utils/mac/SkUniqueCFRef.h"
static void add_opsz_attr(CFMutableDictionaryRef attr, double opsz) {
SkUniqueCFRef<CFNumberRef> opszValueNumber(
CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &opsz));
CFStringRef SkCTFontOpticalSizeAttribute = CFSTR("NSCTFontOpticalSizeAttribute");
CFDictionarySetValue(attr, SkCTFontOpticalSizeAttribute, opszValueNumber.get());
}
static void add_notrak_attr(CFMutableDictionaryRef attr) {
int zero = 0;
SkUniqueCFRef<CFNumberRef> unscaledTrackingNumber(
CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &zero));
CFStringRef SkCTFontUnscaledTrackingAttribute = CFSTR("NSCTFontUnscaledTrackingAttribute");
CFDictionarySetValue(attr, SkCTFontUnscaledTrackingAttribute, unscaledTrackingNumber.get());
}
SkUniqueCFRef<CTFontRef> SkCTFontCreateExactCopy(CTFontRef baseFont, CGFloat textSize,
OpszVariation opszVariation)
{
SkUniqueCFRef<CFMutableDictionaryRef> attr(
CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
if (opszVariation.isSet) {
add_opsz_attr(attr.get(), opszVariation.value);
} else {
CFStringRef SkCTFontOpticalSizeAttribute = CFSTR("NSCTFontOpticalSizeAttribute");
SkUniqueCFRef<CFTypeRef> opsz(CTFontCopyAttribute(baseFont, SkCTFontOpticalSizeAttribute));
double opsz_val;
if (!opsz ||
CFGetTypeID(opsz.get()) != CFNumberGetTypeID() ||
!CFNumberGetValue(static_cast<CFNumberRef>(opsz.get()),kCFNumberDoubleType,&opsz_val) ||
opsz_val <= 0)
{
opsz_val = CTFontGetSize(baseFont);
}
add_opsz_attr(attr.get(), opsz_val);
}
add_notrak_attr(attr.get());
SkUniqueCFRef<CTFontDescriptorRef> desc(CTFontDescriptorCreateWithAttributes(attr.get()));
return SkUniqueCFRef<CTFontRef>(
CTFontCreateCopyWithAttributes(baseFont, textSize, nullptr, desc.get()));
}
#endif