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

OpenTTD Source 13.2.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 #ifdef WITH_FREETYPE
21
22 #include <ft2build.h>
23 #include FT_FREETYPE_H
24
25 extern FT_Library _library;
26
27
28 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
29 {
30 FT_Error err = FT_Err_Cannot_Open_Resource;
31
32 if (!FcInit()) {
33 ShowInfo("Unable to load font configuration");
34 return err;
35 }
36
37 auto fc_instance = FcConfigReference(nullptr);
38 assert(fc_instance != nullptr);
39
40 FcPattern *match;
41 FcPattern *pat;
42 FcFontSet *fs;
43 FcResult result;
44 char *font_style;
45 char *font_family;
46
47 /* Split & strip the font's style */
48 font_family = stredup(font_name);
49 font_style = strchr(font_family, ',');
50 if (font_style != nullptr) {
51 font_style[0] = '\0';
52 font_style++;
53 while (*font_style == ' ' || *font_style == '\t') font_style++;
54 }
55
56 /* Resolve the name and populate the information structure */
57 pat = FcNameParse((FcChar8 *)font_family);
58 if (font_style != nullptr) FcPatternAddString(pat, FC_STYLE, (FcChar8 *)font_style);
59 FcConfigSubstitute(nullptr, pat, FcMatchPattern);
60 FcDefaultSubstitute(pat);
61 fs = FcFontSetCreate();
62 match = FcFontMatch(nullptr, pat, &result);
63
64 if (fs != nullptr && match != nullptr) {
65 int i;
66 FcChar8 *family;
67 FcChar8 *style;
68 FcChar8 *file;
69 int32_t index;
70 FcFontSetAdd(fs, match);
71
72 for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
73 /* Try the new filename */
74 if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
75 FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
76 FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch &&
77 FcPatternGetInteger(fs->fonts[i], FC_INDEX, 0, &index) == FcResultMatch) {
78
79 /* The correct style? */
80 if (font_style != nullptr && strcasecmp(font_style, (char *)style) != 0) continue;
81
82 /* Font config takes the best shot, which, if the family name is spelled
83 * wrongly a 'random' font, so check whether the family name is the
84 * same as the supplied name */
85 if (strcasecmp(font_family, (char *)family) == 0) {
86 err = FT_New_Face(_library, (char *)file, index, face);
87 }
88 }
89 }
90 }
91
92 free(font_family);
93 FcPatternDestroy(pat);
94 FcFontSetDestroy(fs);
95 FcConfigDestroy(fc_instance);
96
97 return err;
98 }
99
100 #endif /* WITH_FREETYPE */
101
102
103 bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
104 {
105 bool ret = false;
106
107 if (!FcInit()) return ret;
108
109 auto fc_instance = FcConfigReference(nullptr);
110 assert(fc_instance != nullptr);
111
112 /* Fontconfig doesn't handle full language isocodes, only the part
113 * before the _ of e.g. en_GB is used, so "remove" everything after
114 * the _. */
115 char lang[16];
116 seprintf(lang, lastof(lang), ":lang=%s", language_isocode);
117 char *split = strchr(lang, '_');
118 if (split != nullptr) *split = '\0';
119
120 /* First create a pattern to match the wanted language. */
121 FcPattern *pat = FcNameParse((FcChar8 *)lang);
122 /* We only want to know the filename. */
123 FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_SPACING, FC_SLANT, FC_WEIGHT, nullptr);
124 /* Get the list of filenames matching the wanted language. */
125 FcFontSet *fs = FcFontList(nullptr, pat, os);
126
127 /* We don't need these anymore. */
128 FcObjectSetDestroy(os);
129 FcPatternDestroy(pat);
130
131 if (fs != nullptr) {
132 int best_weight = -1;
133 const char *best_font = nullptr;
134
135 for (int i = 0; i < fs->nfont; i++) {
136 FcPattern *font = fs->fonts[i];
137
138 FcChar8 *file = nullptr;
139 FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
140 if (res != FcResultMatch || file == nullptr) {
141 continue;
142 }
143
144 /* Get a font with the right spacing .*/
145 int value = 0;
146 FcPatternGetInteger(font, FC_SPACING, 0, &value);
147 if (callback->Monospace() != (value == FC_MONO) && value != FC_DUAL) continue;
148
149 /* Do not use those that explicitly say they're slanted. */
150 FcPatternGetInteger(font, FC_SLANT, 0, &value);
151 if (value != 0) continue;
152
153 /* We want the fatter font as they look better at small sizes. */
154 FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
155 if (value <= best_weight) continue;
156
157 callback->SetFontNames(settings, (const char *)file);
158
159 bool missing = callback->FindMissingGlyphs();
160 Debug(fontcache, 1, "Font \"{}\" misses{} glyphs", file, missing ? "" : " no");
161
162 if (!missing) {
163 best_weight = value;
164 best_font = (const char *)file;
165 }
166 }
167
168 if (best_font != nullptr) {
169 ret = true;
170 callback->SetFontNames(settings, best_font);
171 InitFontCache(callback->Monospace());
172 }
173
174 /* Clean up the list of filenames. */
175 FcFontSetDestroy(fs);
176 }
177
178 FcConfigDestroy(fc_instance);
179 return ret;
180 }
MissingGlyphSearcher::FindMissingGlyphs
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
Definition: strings.cpp:2060
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:243
FontCacheSettings
Settings for the four different fonts.
Definition: fontcache.h:212
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition: fontcache.cpp:128
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.
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:554
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
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:28
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:402
SetFallbackFont
bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, 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:103