10 #include "../../stdafx.h"
11 #include "../../debug.h"
13 #include "../../blitter/factory.hpp"
14 #include "../../fileio_func.h"
15 #include "../../fontdetection.h"
16 #include "../../string_func.h"
17 #include "../../strings_func.h"
18 #include "../../zoom_func.h"
22 #include "../../table/control_codes.h"
30 #include FT_FREETYPE_H
32 extern FT_Library _library;
37 FT_Error err = FT_Err_Cannot_Open_Resource;
40 UInt8 file_path[PATH_MAX];
49 CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontNameAttribute)), 1, &kCFTypeSetCallBacks));
53 for (CFIndex i = 0; descs.get() !=
nullptr && i < CFArrayGetCount(descs.get()) && os_err != noErr; i++) {
54 CFAutoRelease<CTFontRef> font(CTFontCreateWithFontDescriptor((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), i), 0.0,
nullptr));
56 if (CFURLGetFileSystemRepresentation(fontURL.get(),
true, file_path,
lengthof(file_path))) os_err = noErr;
59 if (os_err == noErr) {
60 Debug(fontcache, 3,
"Font path for {}: {}", font_name, file_path);
61 err = FT_New_Face(_library, (
const char *)file_path, 0, face);
75 if (strcmp(language_isocode,
"zh_TW") == 0) {
78 }
else if (strcmp(language_isocode,
"zh_CN") == 0) {
84 char *sep = strchr(lang,
'_');
85 if (sep !=
nullptr) *sep =
'\0';
90 CFStringRef lang_codes[2];
91 lang_codes[0] = CFStringCreateWithCString(kCFAllocatorDefault, lang, kCFStringEncodingUTF8);
92 lang_codes[1] = CFSTR(
"en");
93 CFArrayRef lang_arr = CFArrayCreate(kCFAllocatorDefault, (
const void **)lang_codes,
lengthof(lang_codes), &kCFTypeArrayCallBacks);
94 CFAutoRelease<CFDictionaryRef> lang_attribs(CFDictionaryCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontLanguagesAttribute)), (
const void **)&lang_arr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
97 CFRelease(lang_codes[0]);
100 CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontLanguagesAttribute)), 1, &kCFTypeSetCallBacks));
101 CFAutoRelease<CFArrayRef> descs(CTFontDescriptorCreateMatchingFontDescriptors(lang_desc.get(), mandatory_attribs.get()));
104 for (
int tries = 0; tries < 2; tries++) {
105 for (CFIndex i = 0; descs.get() !=
nullptr && i < CFArrayGetCount(descs.get()); i++) {
106 CTFontDescriptorRef font = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), i);
110 CTFontSymbolicTraits symbolic_traits;
111 CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontSymbolicTrait), kCFNumberIntType, &symbolic_traits);
114 if ((symbolic_traits & kCTFontClassMaskTrait) == (CTFontStylisticClass)kCTFontSymbolicClass || (symbolic_traits & kCTFontVerticalTrait))
continue;
116 if (symbolic_traits & kCTFontBoldTrait)
continue;
118 if (((symbolic_traits & kCTFontMonoSpaceTrait) == kCTFontMonoSpaceTrait) != callback->
Monospace())
continue;
123 CFStringGetCString(font_name.get(), name,
lengthof(name), kCFStringEncodingUTF8);
128 if (tries == 0 && (symbolic_traits & kCTFontClassMaskTrait) != (CTFontStylisticClass)kCTFontSansSerifClass)
continue;
132 if (name[0] ==
'.' || strncmp(name,
"LastResort", 10) == 0)
continue;
137 Debug(fontcache, 2,
"CT-Font for {}: {}", language_isocode, name);
158 this->SetFontSize(pixels);
172 void CoreTextFontCache::SetFontSize(
int pixels)
176 int scaled_height =
ScaleGUITrad(FontCache::GetDefaultFontHeight(this->
fs));
177 pixels = scaled_height;
181 float min_size = 0.0f;
190 uint16_t lowestRecPPEM;
191 CFDataGetBytes(data.get(), CFRangeMake(46,
sizeof(lowestRecPPEM)), (UInt8 *)&lowestRecPPEM);
192 min_size = CFSwapInt16BigToHost(lowestRecPPEM);
195 CFNumberGetValue(size.get(), kCFNumberFloatType, &min_size);
209 this->
font.reset(CTFontCreateWithFontDescriptor(this->
font_desc.get(), pixels,
nullptr));
215 this->
ascender = (int)std::ceil(CTFontGetAscent(this->
font.get()));
216 this->
descender = -(int)std::ceil(CTFontGetDescent(this->
font.get()));
222 CFStringGetCString(
font_name.get(), name,
lengthof(name), kCFStringEncodingUTF8);
225 Debug(fontcache, 2,
"Loaded font '{}' with size {}", this->
font_name, pixels);
230 assert(IsPrintable(key));
232 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
238 if (key >= 0x010000U) {
239 chars[0] = (UniChar)(((key - 0x010000U) >> 10) + 0xD800);
240 chars[1] = (UniChar)(((key - 0x010000U) & 0x3FF) + 0xDC00);
242 chars[0] = (UniChar)(key & 0xFFFF);
245 CGGlyph glyph[2] = {0, 0};
246 if (CTFontGetGlyphsForCharacters(this->
font.get(), chars, glyph, key >= 0x010000U ? 2 : 1)) {
253 const void *CoreTextFontCache::InternalGetFontTable(uint32 tag,
size_t &length)
256 if (!data)
return nullptr;
258 length = CFDataGetLength(data.get());
259 auto buf = MallocT<UInt8>(length);
261 CFDataGetBytes(data.get(), CFRangeMake(0, (CFIndex)length), buf);
265 const Sprite *CoreTextFontCache::InternalGetGlyph(
GlyphID key,
bool use_aa)
268 CGGlyph glyph = (CGGlyph)key;
269 CGRect bounds = CGRectNull;
271 bounds = CTFontGetOpticalBoundsForGlyphs(this->
font.get(), &glyph,
nullptr, 1, 0);
273 bounds = CTFontGetBoundingRectsForGlyphs(this->
font.get(), kCTFontOrientationDefault, &glyph,
nullptr, 1);
275 if (CGRectIsNull(bounds))
usererror(
"Unable to render font glyph");
277 uint bb_width = (uint)std::ceil(bounds.size.width) + 1;
278 uint bb_height = (uint)std::ceil(bounds.size.height);
282 uint width = std::max(1U, bb_width + shadow);
283 uint
height = std::max(1U, bb_height + shadow);
292 sprite.
width = width;
294 sprite.
x_offs = (int16)std::round(CGRectGetMinX(bounds));
295 sprite.
y_offs = this->
ascender - (int16)std::ceil(CGRectGetMaxY(bounds));
297 if (bounds.size.width > 0) {
301 int pitch =
Align(bb_width, 16);
302 byte *bmp = CallocT<byte>(bb_height * pitch);
305 CGContextSetAllowsAntialiasing(context.get(), use_aa);
306 CGContextSetAllowsFontSubpixelPositioning(context.get(), use_aa);
307 CGContextSetAllowsFontSubpixelQuantization(context.get(), !use_aa);
308 CGContextSetShouldSmoothFonts(context.get(),
false);
311 CGPoint pos{offset - bounds.origin.x, offset - bounds.origin.y};
312 CTFontDrawGlyphs(this->
font.get(), &glyph, &pos, 1, context.get());
316 for (uint y = 0; y < bb_height; y++) {
317 for (uint x = 0; x < bb_width; x++) {
318 if (bmp[y * pitch + x] > 0) {
319 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
m = SHADOW_COLOUR;
320 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
a = use_aa ? bmp[x + y * pitch] : 0xFF;
327 for (uint y = 0; y < bb_height; y++) {
328 for (uint x = 0; x < bb_width; x++) {
329 if (bmp[y * pitch + x] > 0) {
330 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
331 sprite.
data[x + y * sprite.
width].
a = use_aa ? bmp[x + y * pitch] : 0xFF;
337 GlyphEntry new_glyph;
339 new_glyph.
width = (byte)std::round(CTFontGetAdvancesForGlyphs(this->
font.get(), kCTFontOrientationDefault, &glyph,
nullptr, 1));
340 this->SetGlyphPtr(key, &new_glyph);
342 return new_glyph.sprite;
359 if (
settings->os_handle !=
nullptr) {
360 font_ref.reset(
static_cast<CTFontDescriptorRef
>(
const_cast<void *
>(
settings->os_handle)));
361 CFRetain(font_ref.get());
371 path.reset(CFStringCreateWithCString(kCFAllocatorDefault,
settings->font.c_str(), kCFStringEncodingUTF8));
375 if (!full_font.empty()) {
376 path.reset(CFStringCreateWithCString(kCFAllocatorDefault, full_font.c_str(), kCFStringEncodingUTF8));
382 CFAutoRelease<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(), kCFURLPOSIXPathStyle,
false));
385 if (descs && CFArrayGetCount(descs.get()) > 0) {
386 font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
387 CFRetain(font_ref.get());
389 ShowInfoF(
"Unable to load file '%s' for %s font, using default OS font selection instead",
settings->font.c_str(), FontSizeToName(fs));
402 CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontNameAttribute)), 1, &kCFTypeSetCallBacks));
403 CFAutoRelease<CFArrayRef> descs(CTFontDescriptorCreateMatchingFontDescriptors(name_desc.get(), mandatory_attribs.get()));
406 if (descs && CFArrayGetCount(descs.get()) > 0) {
407 font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
408 CFRetain(font_ref.get());
413 ShowInfoF(
"Unable to use '%s' for %s font, using sprite font instead",
settings->font.c_str(), FontSizeToName(fs));