29 this->SetFontSize(pixels);
43void CoreTextFontCache::SetFontSize(
int pixels)
47 int scaled_height =
ScaleGUITrad(FontCache::GetDefaultFontHeight(this->
fs));
48 pixels = scaled_height;
52 float min_size = 0.0f;
61 uint16_t lowestRecPPEM;
62 CFDataGetBytes(data.get(), CFRangeMake(46,
sizeof(lowestRecPPEM)), (UInt8 *)&lowestRecPPEM);
63 min_size = CFSwapInt16BigToHost(lowestRecPPEM);
66 CFNumberGetValue(size.get(), kCFNumberFloatType, &min_size);
80 this->
font.reset(CTFontCreateWithFontDescriptor(this->
font_desc.get(), pixels,
nullptr));
85 this->
ascender = (int)std::ceil(CTFontGetAscent(this->
font.get()));
86 this->
descender = -(int)std::ceil(CTFontGetDescent(this->
font.get()));
100 assert(IsPrintable(key));
104 if (key >= 0x010000U) {
105 chars[0] = (UniChar)(((key - 0x010000U) >> 10) + 0xD800);
106 chars[1] = (UniChar)(((key - 0x010000U) & 0x3FF) + 0xDC00);
108 chars[0] = (UniChar)(key & 0xFFFF);
111 CGGlyph glyph[2] = {0, 0};
112 if (CTFontGetGlyphsForCharacters(this->
font.get(), chars, glyph, key >= 0x010000U ? 2 : 1)) {
116 if (allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
117 return this->
parent->MapCharToGlyph(key);
126 CGGlyph glyph = (CGGlyph)key;
127 CGRect bounds = CTFontGetOpticalBoundsForGlyphs(this->
font.get(), &glyph,
nullptr, 1, 0);
128 if (CGRectIsNull(bounds)) UserError(
"Unable to render font glyph");
130 uint bb_width = (uint)std::ceil(bounds.size.width) + 1;
131 uint bb_height = (uint)std::ceil(bounds.size.height);
135 uint width = std::max(1U, bb_width + shadow);
136 uint
height = std::max(1U, bb_height + shadow);
146 sprite.
width = width;
148 sprite.
x_offs = (int16_t)std::round(CGRectGetMinX(bounds));
149 sprite.
y_offs = this->
ascender - (int16_t)std::ceil(CGRectGetMaxY(bounds));
151 if (bounds.size.width > 0) {
155 int pitch =
Align(bb_width, 16);
157 const uint8_t *bmp =
static_cast<uint8_t *
>(CGBitmapContextGetData(context.get()));
159 CGContextSetAllowsAntialiasing(context.get(), use_aa);
160 CGContextSetAllowsFontSubpixelPositioning(context.get(), use_aa);
161 CGContextSetAllowsFontSubpixelQuantization(context.get(), !use_aa);
162 CGContextSetShouldSmoothFonts(context.get(),
false);
164 CGPoint pos{-bounds.origin.x, -bounds.origin.y};
165 CTFontDrawGlyphs(this->
font.get(), &glyph, &pos, 1, context.get());
169 for (uint y = 0; y < bb_height; y++) {
170 for (uint x = 0; x < bb_width; x++) {
171 if (bmp[y * pitch + x] > 0) {
172 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
m = SHADOW_COLOUR;
173 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
a = use_aa ? bmp[x + y * pitch] : 0xFF;
180 for (uint y = 0; y < bb_height; y++) {
181 for (uint x = 0; x < bb_width; x++) {
182 if (bmp[y * pitch + x] > 0) {
183 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
184 sprite.
data[x + y * sprite.
width].
a = use_aa ? bmp[x + y * pitch] : 0xFF;
194 new_glyph.
data = std::move(allocator.data);
195 new_glyph.
width = (uint8_t)std::round(CTFontGetAdvancesForGlyphs(this->
font.get(), kCTFontOrientationDefault, &glyph,
nullptr, 1));
197 return this->SetGlyphPtr(key, std::move(new_glyph)).GetSprite();
200class CoreTextFontCacheFactory :
public FontCacheFactory {
202 CoreTextFontCacheFactory() : FontCacheFactory(
"coretext",
"CoreText font loader") {}
204 std::unique_ptr<FontCache>
LoadFont(
FontSize fs,
FontType fonttype,
bool search,
const std::string &font_name,
const std::any &)
const override
210 if (!font_ref) ShowInfo(
"Unable to load file '{}' for {} font, using default OS font selection instead", font_name, FontSizeToName(fs));
212 if (!font_ref && search) {
220 CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontNameAttribute)), 1, &kCFTypeSetCallBacks));
221 CFAutoRelease<CFArrayRef> descs(CTFontDescriptorCreateMatchingFontDescriptors(name_desc.get(), mandatory_attribs.get()));
224 if (descs && CFArrayGetCount(descs.get()) > 0) {
225 font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
226 CFRetain(font_ref.get());
231 ShowInfo(
"Unable to use '{}' for {} font, using sprite font instead", font_name, FontSizeToName(fs));
243 if (language_isocode ==
"zh-TW") {
246 }
else if (language_isocode ==
"zh-CN") {
251 lang = language_isocode;
256 CFStringRef lang_codes[2];
257 lang_codes[0] = CFStringCreateWithCString(kCFAllocatorDefault, lang.c_str(), kCFStringEncodingUTF8);
258 lang_codes[1] = CFSTR(
"en");
259 CFArrayRef lang_arr = CFArrayCreate(kCFAllocatorDefault, (
const void **)lang_codes,
lengthof(lang_codes), &kCFTypeArrayCallBacks);
260 CFAutoRelease<CFDictionaryRef> lang_attribs(CFDictionaryCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontLanguagesAttribute)), (
const void **)&lang_arr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
263 CFRelease(lang_codes[0]);
266 CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault,
const_cast<const void **
>(
reinterpret_cast<const void *
const *
>(&kCTFontLanguagesAttribute)), 1, &kCFTypeSetCallBacks));
267 CFAutoRelease<CFArrayRef> descs(CTFontDescriptorCreateMatchingFontDescriptors(lang_desc.get(), mandatory_attribs.get()));
270 for (
int tries = 0; tries < 2; tries++) {
271 for (CFIndex i = 0; descs.get() !=
nullptr && i < CFArrayGetCount(descs.get()); i++) {
272 CTFontDescriptorRef font = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), i);
276 CTFontSymbolicTraits symbolic_traits;
277 CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontSymbolicTrait), kCFNumberIntType, &symbolic_traits);
280 if ((symbolic_traits & kCTFontClassMaskTrait) == (CTFontStylisticClass)kCTFontSymbolicClass || (symbolic_traits & kCTFontVerticalTrait))
continue;
282 if (symbolic_traits & kCTFontBoldTrait)
continue;
289 CFStringGetCString(font_name.get(), buffer, std::size(buffer), kCFStringEncodingUTF8);
294 if (tries == 0 && (symbolic_traits & kCTFontClassMaskTrait) != (CTFontStylisticClass)kCTFontSansSerifClass)
continue;
298 std::string_view
name{buffer};
299 if (
name.starts_with(
".") ||
name.starts_with(
"LastResort"))
continue;
324 static CTFontDescriptorRef LoadFontFromFile(
const std::string &font_name)
332 path.reset(CFStringCreateWithCString(kCFAllocatorDefault, font_name.c_str(), kCFStringEncodingUTF8));
336 if (!full_font.empty()) {
337 path.reset(CFStringCreateWithCString(kCFAllocatorDefault, full_font.c_str(), kCFStringEncodingUTF8));
343 CFAutoRelease<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(), kCFURLPOSIXPathStyle,
false));
346 if (descs && CFArrayGetCount(descs.get()) > 0) {
347 CTFontDescriptorRef font_ref = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0);
357 static CoreTextFontCacheFactory instance;
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
const std::string_view name
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
bool FindFallbackFont(const std::string &language_isocode, MissingGlyphSearcher *callback) const override
We would like to have a fallback font as the current one doesn't contain all characters we need.
std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, const std::any &) const override
Try loading a font with this factory.
CFAutoRelease< CTFontDescriptorRef > font_desc
Font descriptor excluding font size.
std::string font_name
Cached font name.
const Sprite * InternalGetGlyph(GlyphID key, bool use_aa) override
Load the glyph as a sprite.
CFAutoRelease< CTFontRef > font
CoreText font handle.
void ClearFontCache() override
Reset cached glyphs.
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
int height
The height of the font.
std::unique_ptr< FontCache > parent
The parent of this font cache.
static void AddFallback(FontSizes fontsizes, std::string_view name, const std::any &os_handle={})
Add a fallback font, with optional OS-specific handle.
const FontSize fs
The size of the font.
int descender
The descender value of the font.
static bool TryFallback(FontSizes fontsizes, const std::set< char32_t > &glyphs, const std::string &name, const std::any &os_handle={})
Test a fallback font, with optional OS-specific handle, for specific glyphs.
int ascender
The ascender value of the font.
A searcher for missing glyphs.
FontSizes missing_fontsizes
Font sizes to actually search for.
std::set< char32_t > required_glyphs
Glyphs that are required.
virtual Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
SpriteCollMap< Sprite > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
Font cache for fonts that are based on a TrueType font.
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
int used_size
Used font size.
int req_size
Requested font size.
void ClearFontCache() override
Reset cached glyphs.
static constexpr uint MAX_FONT_MIN_REC_SIZE
Upper limit for the recommended font size in case a font file contains nonsensical values.
SpriteAllocator that allocates memory via a unique_ptr array.
Control codes that are embedded in the translation strings.
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
@ Fontcache
Fontcache message facility.
@ Warning
Warning, wrong but okay if you don't know.
Error reporting related functions.
Factory to 'query' all available blitters.
bool FileExists(std::string_view filename)
Test whether the given filename exists.
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Functions for standard in/out file operations.
@ Base
Base directory for all subdirectories.
Functions related to font handling on MacOS.
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
FontType
Different types of font that can be loaded.
@ TrueType
Scalable TrueType fonts.
@ Font
A sprite used for fonts.
FontSize
Available font sizes.
@ Small
Index of the small font in the font tables.
@ Normal
Index of the normal font in the font tables.
@ Monospace
Index of the monospaced font in the font tables.
Functions related to MacOS support.
std::unique_ptr< typename std::remove_pointer< T >::type, CFDeleter< typename std::remove_pointer< T >::type > > CFAutoRelease
Specialisation of std::unique_ptr for CoreFoundation objects.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
A number of safeguards to prevent using unsafe methods.
Functions to cache sprites in memory.
@ Palette
Sprite has palette data.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Functions related to low-level strings.
Functions related to OTTD's strings.
Structure for passing information from the sprite loader to the blitter.
SpriteComponents colours
The colour components of the sprite with useful information.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
uint16_t width
Width of the sprite.
int16_t x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
Container for information about a glyph.
std::unique_ptr< std::byte[]> data
The loaded sprite.
uint8_t width
The width of the glyph.
static const int MAX_FONT_SIZE
Maximum font size.
Functions related to zooming.