10 #include "../../stdafx.h"
11 #include "../../debug.h"
13 #include "../../core/math_func.hpp"
14 #include "../../blitter/factory.hpp"
15 #include "../../error_func.h"
16 #include "../../fileio_func.h"
17 #include "../../fontdetection.h"
18 #include "../../string_func.h"
19 #include "../../strings_func.h"
20 #include "../../zoom_func.h"
23 #include "../../table/control_codes.h"
32 if (language_isocode ==
"zh_TW") {
35 }
else if (language_isocode ==
"zh_CN") {
40 lang = language_isocode.substr(0, language_isocode.find(
'_'));
45 CFStringRef lang_codes[2];
46 lang_codes[0] = CFStringCreateWithCString(kCFAllocatorDefault, lang.c_str(), kCFStringEncodingUTF8);
47 lang_codes[1] = CFSTR(
"en");
48 CFArrayRef lang_arr = CFArrayCreate(kCFAllocatorDefault, (
const void **)lang_codes,
lengthof(lang_codes), &kCFTypeArrayCallBacks);
49 CFAutoRelease<CFDictionaryRef> lang_attribs(CFDictionaryCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontLanguagesAttribute)), (
const void **)&lang_arr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
52 CFRelease(lang_codes[0]);
55 CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontLanguagesAttribute)), 1, &kCFTypeSetCallBacks));
59 for (
int tries = 0; tries < 2; tries++) {
60 for (CFIndex i = 0; descs.get() !=
nullptr && i < CFArrayGetCount(descs.get()); i++) {
61 CTFontDescriptorRef font = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), i);
65 CTFontSymbolicTraits symbolic_traits;
66 CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontSymbolicTrait), kCFNumberIntType, &symbolic_traits);
69 if ((symbolic_traits & kCTFontClassMaskTrait) == (CTFontStylisticClass)kCTFontSymbolicClass || (symbolic_traits & kCTFontVerticalTrait))
continue;
71 if (symbolic_traits & kCTFontBoldTrait)
continue;
73 if (((symbolic_traits & kCTFontMonoSpaceTrait) == kCTFontMonoSpaceTrait) != callback->
Monospace())
continue;
78 CFStringGetCString(font_name.get(), name,
lengthof(name), kCFStringEncodingUTF8);
83 if (tries == 0 && (symbolic_traits & kCTFontClassMaskTrait) != (CTFontStylisticClass)kCTFontSansSerifClass)
continue;
87 if (name[0] ==
'.' || strncmp(name,
"LastResort", 10) == 0)
continue;
92 Debug(fontcache, 2,
"CT-Font for {}: {}", language_isocode, name);
113 this->SetFontSize(pixels);
127 void CoreTextFontCache::SetFontSize(
int pixels)
131 int scaled_height =
ScaleGUITrad(FontCache::GetDefaultFontHeight(this->
fs));
132 pixels = scaled_height;
136 float min_size = 0.0f;
145 uint16_t lowestRecPPEM;
146 CFDataGetBytes(data.get(), CFRangeMake(46,
sizeof(lowestRecPPEM)), (UInt8 *)&lowestRecPPEM);
147 min_size = CFSwapInt16BigToHost(lowestRecPPEM);
150 CFNumberGetValue(size.get(), kCFNumberFloatType, &min_size);
164 this->
font.reset(CTFontCreateWithFontDescriptor(this->
font_desc.get(), pixels,
nullptr));
170 this->
ascender = (int)std::ceil(CTFontGetAscent(this->
font.get()));
171 this->
descender = -(int)std::ceil(CTFontGetDescent(this->
font.get()));
177 CFStringGetCString(
font_name.get(), name,
lengthof(name), kCFStringEncodingUTF8);
180 Debug(fontcache, 2,
"Loaded font '{}' with size {}", this->
font_name, pixels);
185 assert(IsPrintable(key));
189 if (key >= 0x010000U) {
190 chars[0] = (UniChar)(((key - 0x010000U) >> 10) + 0xD800);
191 chars[1] = (UniChar)(((key - 0x010000U) & 0x3FF) + 0xDC00);
193 chars[0] = (UniChar)(key & 0xFFFF);
196 CGGlyph glyph[2] = {0, 0};
197 if (CTFontGetGlyphsForCharacters(this->
font.get(), chars, glyph, key >= 0x010000U ? 2 : 1)) {
201 if (allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
208 const void *CoreTextFontCache::InternalGetFontTable(uint32_t tag,
size_t &length)
211 if (!data)
return nullptr;
213 length = CFDataGetLength(data.get());
214 auto buf = MallocT<UInt8>(length);
216 CFDataGetBytes(data.get(), CFRangeMake(0, (CFIndex)length), buf);
220 const Sprite *CoreTextFontCache::InternalGetGlyph(
GlyphID key,
bool use_aa)
223 CGGlyph glyph = (CGGlyph)key;
224 CGRect bounds = CGRectNull;
226 bounds = CTFontGetOpticalBoundsForGlyphs(this->
font.get(), &glyph,
nullptr, 1, 0);
228 bounds = CTFontGetBoundingRectsForGlyphs(this->
font.get(), kCTFontOrientationDefault, &glyph,
nullptr, 1);
230 if (CGRectIsNull(bounds)) UserError(
"Unable to render font glyph");
232 uint bb_width = (uint)std::ceil(bounds.size.width) + 1;
233 uint bb_height = (uint)std::ceil(bounds.size.height);
237 uint width = std::max(1U, bb_width + shadow);
238 uint
height = std::max(1U, bb_height + shadow);
248 sprite.
width = width;
250 sprite.
x_offs = (int16_t)std::round(CGRectGetMinX(bounds));
251 sprite.
y_offs = this->
ascender - (int16_t)std::ceil(CGRectGetMaxY(bounds));
253 if (bounds.size.width > 0) {
257 int pitch =
Align(bb_width, 16);
258 byte *bmp = CallocT<byte>(bb_height * pitch);
261 CGContextSetAllowsAntialiasing(context.get(), use_aa);
262 CGContextSetAllowsFontSubpixelPositioning(context.get(), use_aa);
263 CGContextSetAllowsFontSubpixelQuantization(context.get(), !use_aa);
264 CGContextSetShouldSmoothFonts(context.get(),
false);
267 CGPoint pos{offset - bounds.origin.x, offset - bounds.origin.y};
268 CTFontDrawGlyphs(this->
font.get(), &glyph, &pos, 1, context.get());
272 for (uint y = 0; y < bb_height; y++) {
273 for (uint x = 0; x < bb_width; x++) {
274 if (bmp[y * pitch + x] > 0) {
275 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
m = SHADOW_COLOUR;
276 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
a = use_aa ? bmp[x + y * pitch] : 0xFF;
283 for (uint y = 0; y < bb_height; y++) {
284 for (uint x = 0; x < bb_width; x++) {
285 if (bmp[y * pitch + x] > 0) {
286 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
287 sprite.
data[x + y * sprite.
width].
a = use_aa ? bmp[x + y * pitch] : 0xFF;
293 GlyphEntry new_glyph;
295 new_glyph.
width = (byte)std::round(CTFontGetAdvancesForGlyphs(this->
font.get(), kCTFontOrientationDefault, &glyph,
nullptr, 1));
296 this->SetGlyphPtr(key, &new_glyph);
298 return new_glyph.sprite;
301 static CTFontDescriptorRef LoadFontFromFile(
const std::string &font_name)
311 path.reset(CFStringCreateWithCString(kCFAllocatorDefault, font_name.c_str(), kCFStringEncodingUTF8));
315 if (!full_font.empty()) {
316 path.reset(CFStringCreateWithCString(kCFAllocatorDefault, full_font.c_str(), kCFStringEncodingUTF8));
322 CFAutoRelease<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(), kCFURLPOSIXPathStyle,
false));
325 if (descs && CFArrayGetCount(descs.get()) > 0) {
326 CTFontDescriptorRef font_ref = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0);
349 if (
settings->os_handle !=
nullptr) {
350 font_ref.reset(
static_cast<CTFontDescriptorRef
>(
const_cast<void *
>(
settings->os_handle)));
351 CFRetain(font_ref.get());
356 font_ref.reset(LoadFontFromFile(
settings->font));
357 if (!font_ref) ShowInfo(
"Unable to load file '{}' for {} font, using default OS font selection instead",
settings->font, FontSizeToName(fs));
368 CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontNameAttribute)), 1, &kCFTypeSetCallBacks));
369 CFAutoRelease<CFArrayRef> descs(CTFontDescriptorCreateMatchingFontDescriptors(name_desc.get(), mandatory_attribs.get()));
372 if (descs && CFArrayGetCount(descs.get()) > 0) {
373 font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
374 CFRetain(font_ref.get());
379 ShowInfo(
"Unable to use '{}' for {} font, using sprite font instead",
settings->font, FontSizeToName(fs));