OpenTTD Source 20251213-master-g1091fa6071
font_unix.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../../stdafx.h"
11
12#include "../../misc/autorelease.hpp"
13#include "../../debug.h"
14#include "../../fontcache.h"
15#include "../../string_func.h"
16#include "../../strings_func.h"
17#include "font_unix.h"
18
19#include <fontconfig/fontconfig.h>
20#include <ft2build.h>
21#include FT_FREETYPE_H
22
23#include "../../safeguards.h"
24
25extern FT_Library _ft_library;
26
32static const FcChar8 *ToFcString(const std::string &str)
33{
34 return reinterpret_cast<const FcChar8 *>(str.c_str());
35}
36
42static const char *FromFcString(const FcChar8 *str)
43{
44 return reinterpret_cast<const char *>(str);
45}
46
53static std::tuple<std::string, std::string> SplitFontFamilyAndStyle(std::string_view font_name)
54{
55 auto separator = font_name.find(',');
56 if (separator == std::string_view::npos) return { std::string(font_name), std::string() };
57
58 auto begin = font_name.find_first_not_of("\t ", separator + 1);
59 if (begin == std::string_view::npos) return { std::string(font_name.substr(0, separator)), std::string() };
60
61 return { std::string(font_name.substr(0, separator)), std::string(font_name.substr(begin)) };
62}
63
70FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face)
71{
72 FT_Error err = FT_Err_Cannot_Open_Resource;
73
74 if (!FcInit()) {
75 ShowInfo("Unable to load font configuration");
76 return err;
77 }
78
79 auto fc_instance = AutoRelease<FcConfig, FcConfigDestroy>(FcConfigReference(nullptr));
80 assert(fc_instance != nullptr);
81
82 /* Split & strip the font's style */
83 auto [font_family, font_style] = SplitFontFamilyAndStyle(font_name);
84
85 /* Resolve the name and populate the information structure */
86 auto pat = AutoRelease<FcPattern, FcPatternDestroy>(FcPatternCreate());
87 if (!font_family.empty()) FcPatternAddString(pat.get(), FC_FAMILY, ToFcString(font_family));
88 if (!font_style.empty()) FcPatternAddString(pat.get(), FC_STYLE, ToFcString(font_style));
89 FcConfigSubstitute(nullptr, pat.get(), FcMatchPattern);
90 FcDefaultSubstitute(pat.get());
91 auto fs = AutoRelease<FcFontSet, FcFontSetDestroy>(FcFontSetCreate());
92 FcResult result;
93 FcPattern *match = FcFontMatch(nullptr, pat.get(), &result);
94
95 if (fs == nullptr || match == nullptr) return err;
96
97 FcFontSetAdd(fs.get(), match);
98
99 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
100 FcChar8 *family;
101 FcChar8 *style;
102 FcChar8 *file;
103 int32_t index;
104
105 /* Try the new filename */
106 if (FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch) continue;
107 if (FcPatternGetString(font, FC_FAMILY, 0, &family) != FcResultMatch) continue;
108 if (FcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch) continue;
109 if (FcPatternGetInteger(font, FC_INDEX, 0, &index) != FcResultMatch) continue;
110
111 /* The correct style? */
112 if (!font_style.empty() && !StrEqualsIgnoreCase(font_style, FromFcString(style))) continue;
113
114 /* Font config takes the best shot, which, if the family name is spelled
115 * wrongly a 'random' font, so check whether the family name is the
116 * same as the supplied name */
117 if (StrEqualsIgnoreCase(font_family, FromFcString(family))) {
118 err = FT_New_Face(_ft_library, FromFcString(file), index, face);
119 if (err == FT_Err_Ok) return err;
120 }
121 }
122
123 if (err != FT_Err_Ok) {
124 ShowInfo("Unable to find '{}' font", font_name);
125 }
126
127 return err;
128}
129
135static int GetPreferredWeightDistance(int weight)
136{
137 /* Prefer a font between normal and medium weight. */
138 static constexpr int PREFERRED_WEIGHT_MIN = FC_WEIGHT_NORMAL;
139 static constexpr int PREFERRED_WEIGHT_MAX = FC_WEIGHT_MEDIUM;
140
141 if (weight < PREFERRED_WEIGHT_MIN) return std::abs(weight - PREFERRED_WEIGHT_MIN);
142 if (weight > PREFERRED_WEIGHT_MAX) return weight - PREFERRED_WEIGHT_MAX;
143 return 0;
144}
145
146bool FontConfigFindFallbackFont(const std::string &language_isocode, FontSizes fontsizes, MissingGlyphSearcher *callback)
147{
148 bool ret = false;
149
150 if (!FcInit()) return ret;
151
152 auto fc_instance = AutoRelease<FcConfig, FcConfigDestroy>(FcConfigReference(nullptr));
153 assert(fc_instance != nullptr);
154
155 /* Get set of required characters. */
156 auto chars = callback->GetRequiredGlyphs(fontsizes);
157
158 /* Fontconfig doesn't handle full language isocodes, only the part
159 * before the _ of e.g. en_GB is used, so "remove" everything after
160 * the _. */
161 std::string lang = language_isocode.empty() ? "" : fmt::format(":lang={}", language_isocode.substr(0, language_isocode.find('_')));
162
163 /* First create a pattern to match the wanted language. */
164 auto pat = AutoRelease<FcPattern, FcPatternDestroy>(FcNameParse(ToFcString(lang)));
165 /* We only want to know these attributes. */
166 auto os = AutoRelease<FcObjectSet, FcObjectSetDestroy>(FcObjectSetBuild(FC_FILE, FC_INDEX, FC_SPACING, FC_SLANT, FC_WEIGHT, FC_CHARSET, nullptr));
167 /* Get the list of filenames matching the wanted language. */
168 auto fs = AutoRelease<FcFontSet, FcFontSetDestroy>(FcFontList(nullptr, pat.get(), os.get()));
169
170 if (fs == nullptr) return ret;
171
172 int best_weight = -1;
173 std::string best_font;
174 int best_index = 0;
175
176 for (FcPattern *font : std::span(fs->fonts, fs->nfont)) {
177 FcChar8 *file = nullptr;
178 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
179 if (res != FcResultMatch || file == nullptr) continue;
180
181 /* Get a font with the right spacing .*/
182 int value = 0;
183 FcPatternGetInteger(font, FC_SPACING, 0, &value);
184 if (fontsizes.Test(FS_MONO) != (value == FC_MONO) && value != FC_DUAL) continue;
185
186 /* Do not use those that explicitly say they're slanted. */
187 FcPatternGetInteger(font, FC_SLANT, 0, &value);
188 if (value != 0) continue;
189
190 /* We want a font near to medium weight. */
191 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
192 int weight = GetPreferredWeightDistance(value);
193 if (best_weight != -1 && weight > best_weight) continue;
194
195 size_t matching_chars = 0;
196 FcCharSet *charset;
197 FcPatternGetCharSet(font, FC_CHARSET, 0, &charset);
198 for (const char32_t &c : chars) {
199 if (FcCharSetHasChar(charset, c)) ++matching_chars;
200 }
201
202 if (matching_chars < chars.size()) {
203 Debug(fontcache, 1, "Font \"{}\" misses {} glyphs", reinterpret_cast<const char *>(file), chars.size() - matching_chars);
204 continue;
205 }
206
207 /* Possible match based on attributes, get index. */
208 int32_t index;
209 res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
210 if (res != FcResultMatch) continue;
211
212 best_weight = weight;
213 best_font = FromFcString(file);
214 best_index = index;
215 }
216
217 if (best_font.empty()) return false;
218
219 FontCache::AddFallbackWithHandle(fontsizes, callback->GetLoadReason(), best_font, best_index);
220 FontCache::LoadFontCaches(fontsizes);
221
222 return true;
223}
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
static void AddFallbackWithHandle(FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, T &handle)
Add a fallback font, with OS-specific handle.
Definition fontcache.h:86
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
A searcher for missing glyphs.
virtual std::set< char32_t > GetRequiredGlyphs(FontSizes fontsizes)=0
Get set of glyphs required for the current language.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face)
Load a freetype font face with the given font name.
Definition font_unix.cpp:70
static int GetPreferredWeightDistance(int weight)
Get distance between font weight and preferred font weights.
static const char * FromFcString(const FcChar8 *str)
Get a C-style string from a FontConfig-style string.
Definition font_unix.cpp:42
static const FcChar8 * ToFcString(const std::string &str)
Get a FontConfig-style string from a std::string.
Definition font_unix.cpp:32
static std::tuple< std::string, std::string > SplitFontFamilyAndStyle(std::string_view font_name)
Split the font name into the font family and style.
Definition font_unix.cpp:53
Functions related to detecting/finding the right font.
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:252
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:323