OpenTTD Source: src/os/unix/font_unix.cpp Source File

OpenTTD Source 14.1

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../../stdafx.h"
11 #include "../../debug.h"
12 #include "../../fontdetection.h"
13 #include "../../string_func.h"
14 #include "../../strings_func.h"
15
16 #include <fontconfig/fontconfig.h>
17
18 #include "safeguards.h"
19
20 #include <ft2build.h>
21 #include FT_FREETYPE_H
22
23 extern FT_Library _library;
24
31 static std::tuple<std::string, std::string> SplitFontFamilyAndStyle(std::string_view font_name)
32 {
33 auto separator = font_name.find(',');
34 if (separator == std::string_view::npos) return { std::string(font_name), std::string() };
35
36 auto begin = font_name.find_first_not_of("\t ", separator + 1);
37 if (begin == std::string_view::npos) return { std::string(font_name.substr(0, separator)), std::string() };
38
39 return { std::string(font_name.substr(0, separator)), std::string(font_name.substr(begin)) };
40 }
41
42 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
43 {
44 FT_Error err = FT_Err_Cannot_Open_Resource;
45
46 if (!FcInit()) {
47 ShowInfo("Unable to load font configuration");
48 return err;
49 }
50
51 auto fc_instance = FcConfigReference(nullptr);
52 assert(fc_instance != nullptr);
53
54 /* Split & strip the font's style */
55 auto [font_family, font_style] = SplitFontFamilyAndStyle(font_name);
56
57 /* Resolve the name and populate the information structure */
58 FcPattern *pat = FcNameParse((FcChar8 *)font_family.data());
59 if (!font_style.empty()) FcPatternAddString(pat, FC_STYLE, (FcChar8 *)font_style.data());
60 FcConfigSubstitute(nullptr, pat, FcMatchPattern);
61 FcDefaultSubstitute(pat);
62 FcFontSet *fs = FcFontSetCreate();
63 FcResult result;
64 FcPattern *match = FcFontMatch(nullptr, pat, &result);
65
66 if (fs != nullptr && match != nullptr) {
67 FcChar8 *family;
68 FcChar8 *style;
69 FcChar8 *file;
70 int32_t index;
71 FcFontSetAdd(fs, match);
72
73 for (int i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
74 /* Try the new filename */
75 if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
76 FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
77 FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch &&
78 FcPatternGetInteger(fs->fonts[i], FC_INDEX, 0, &index) == FcResultMatch) {
79
80 /* The correct style? */
81 if (!font_style.empty() && !StrEqualsIgnoreCase(font_style, (char *)style)) continue;
82
83 /* Font config takes the best shot, which, if the family name is spelled
84 * wrongly a 'random' font, so check whether the family name is the
85 * same as the supplied name */
86 if (StrEqualsIgnoreCase(font_family, (char *)family)) {
87 err = FT_New_Face(_library, (char *)file, index, face);
88 }
89 }
90 }
91 }
92
93 FcPatternDestroy(pat);
94 FcFontSetDestroy(fs);
95 FcConfigDestroy(fc_instance);
96
97 return err;
98 }
99
100 bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int, MissingGlyphSearcher *callback)
101 {
102 bool ret = false;
103
104 if (!FcInit()) return ret;
105
106 auto fc_instance = FcConfigReference(nullptr);
107 assert(fc_instance != nullptr);
108
109 /* Fontconfig doesn't handle full language isocodes, only the part
110 * before the _ of e.g. en_GB is used, so "remove" everything after
111 * the _. */
112 std::string lang = fmt::format(":lang={}", language_isocode.substr(0, language_isocode.find('_')));
113
114 /* First create a pattern to match the wanted language. */
115 FcPattern *pat = FcNameParse((const FcChar8 *)lang.c_str());
116 /* We only want to know these attributes. */
117 FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_INDEX, FC_SPACING, FC_SLANT, FC_WEIGHT, nullptr);
118 /* Get the list of filenames matching the wanted language. */
119 FcFontSet *fs = FcFontList(nullptr, pat, os);
120
121 /* We don't need these anymore. */
122 FcObjectSetDestroy(os);
123 FcPatternDestroy(pat);
124
125 if (fs != nullptr) {
126 int best_weight = -1;
127 const char *best_font = nullptr;
128 int best_index = 0;
129
130 for (int i = 0; i < fs->nfont; i++) {
131 FcPattern *font = fs->fonts[i];
132
133 FcChar8 *file = nullptr;
134 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
135 if (res != FcResultMatch || file == nullptr) {
136 continue;
137 }
138
139 /* Get a font with the right spacing .*/
140 int value = 0;
141 FcPatternGetInteger(font, FC_SPACING, 0, &value);
142 if (callback->Monospace() != (value == FC_MONO) && value != FC_DUAL) continue;
143
144 /* Do not use those that explicitly say they're slanted. */
145 FcPatternGetInteger(font, FC_SLANT, 0, &value);
146 if (value != 0) continue;
147
148 /* We want the fatter font as they look better at small sizes. */
149 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
150 if (value <= best_weight) continue;
151
152 /* Possible match based on attributes, get index. */
153 int32_t index;
154 res = FcPatternGetInteger(font, FC_INDEX, 0, &index);
155 if (res != FcResultMatch) continue;
156
157 callback->SetFontNames(settings, (const char *)file, &index);
158
159 bool missing = callback->FindMissingGlyphs();
160 Debug(fontcache, 1, "Font \"{}\" misses{} glyphs", (char *)file, missing ? "" : " no");
161
162 if (!missing) {
163 best_weight = value;
164 best_font = (const char *)file;
165 best_index = index;
166 }
167 }
168
169 if (best_font != nullptr) {
170 ret = true;
171 callback->SetFontNames(settings, best_font, &best_index);
172 InitFontCache(callback->Monospace());
173 }
174
175 /* Clean up the list of filenames. */
176 FcFontSetDestroy(fs);
177 }
178
179 FcConfigDestroy(fc_instance);
180 return ret;
181 }
MissingGlyphSearcher::FindMissingGlyphs
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
Definition: strings.cpp:2169
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:115
SetFallbackFont
bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int, MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
Definition: font_unix.cpp:100
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SplitFontFamilyAndStyle
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:31
FontCacheSettings
Settings for the four different fonts.
Definition: fontcache.h:216
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition: fontcache.cpp:197
safeguards.h
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
MissingGlyphSearcher::Monospace
virtual bool Monospace()=0
Whether to search for a monospace font or not.
MissingGlyphSearcher::SetFontNames
virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data=nullptr)=0
Set the right font names.
StrEqualsIgnoreCase
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition: string.cpp:366
GetFontByFaceName
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
Load a freetype font face with the given font name.
Definition: font_unix.cpp:42