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 
8 /** @file font_unix.cpp Functions related to font handling on Unix/Fontconfig. */
9 
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 
GetFontByFaceName(const char * font_name,FT_Face * face)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 		ShowInfoF("Unable to load font configuration");
34 	} else {
35 		FcPattern *match;
36 		FcPattern *pat;
37 		FcFontSet *fs;
38 		FcResult  result;
39 		char *font_style;
40 		char *font_family;
41 
42 		/* Split & strip the font's style */
43 		font_family = stredup(font_name);
44 		font_style = strchr(font_family, ',');
45 		if (font_style != nullptr) {
46 			font_style[0] = '\0';
47 			font_style++;
48 			while (*font_style == ' ' || *font_style == '\t') font_style++;
49 		}
50 
51 		/* Resolve the name and populate the information structure */
52 		pat = FcNameParse((FcChar8 *)font_family);
53 		if (font_style != nullptr) FcPatternAddString(pat, FC_STYLE, (FcChar8 *)font_style);
54 		FcConfigSubstitute(nullptr, pat, FcMatchPattern);
55 		FcDefaultSubstitute(pat);
56 		fs = FcFontSetCreate();
57 		match = FcFontMatch(nullptr, pat, &result);
58 
59 		if (fs != nullptr && match != nullptr) {
60 			int i;
61 			FcChar8 *family;
62 			FcChar8 *style;
63 			FcChar8 *file;
64 			FcFontSetAdd(fs, match);
65 
66 			for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
67 				/* Try the new filename */
68 				if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
69 					FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
70 					FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch) {
71 
72 					/* The correct style? */
73 					if (font_style != nullptr && strcasecmp(font_style, (char *)style) != 0) continue;
74 
75 					/* Font config takes the best shot, which, if the family name is spelled
76 					 * wrongly a 'random' font, so check whether the family name is the
77 					 * same as the supplied name */
78 					if (strcasecmp(font_family, (char *)family) == 0) {
79 						err = FT_New_Face(_library, (char *)file, 0, face);
80 					}
81 				}
82 			}
83 		}
84 
85 		free(font_family);
86 		FcPatternDestroy(pat);
87 		FcFontSetDestroy(fs);
88 		FcFini();
89 	}
90 
91 	return err;
92 }
93 
94 #endif /* WITH_FREETYPE */
95 
96 
SetFallbackFont(FreeTypeSettings * settings,const char * language_isocode,int winlangid,MissingGlyphSearcher * callback)97 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback)
98 {
99 	if (!FcInit()) return false;
100 
101 	bool ret = false;
102 
103 	/* Fontconfig doesn't handle full language isocodes, only the part
104 	 * before the _ of e.g. en_GB is used, so "remove" everything after
105 	 * the _. */
106 	char lang[16];
107 	seprintf(lang, lastof(lang), ":lang=%s", language_isocode);
108 	char *split = strchr(lang, '_');
109 	if (split != nullptr) *split = '\0';
110 
111 	/* First create a pattern to match the wanted language. */
112 	FcPattern *pat = FcNameParse((FcChar8 *)lang);
113 	/* We only want to know the filename. */
114 	FcObjectSet *os = FcObjectSetBuild(FC_FILE, FC_SPACING, FC_SLANT, FC_WEIGHT, nullptr);
115 	/* Get the list of filenames matching the wanted language. */
116 	FcFontSet *fs = FcFontList(nullptr, pat, os);
117 
118 	/* We don't need these anymore. */
119 	FcObjectSetDestroy(os);
120 	FcPatternDestroy(pat);
121 
122 	if (fs != nullptr) {
123 		int best_weight = -1;
124 		const char *best_font = nullptr;
125 
126 		for (int i = 0; i < fs->nfont; i++) {
127 			FcPattern *font = fs->fonts[i];
128 
129 			FcChar8 *file = nullptr;
130 			FcResult res = FcPatternGetString(font, FC_FILE, 0, &file);
131 			if (res != FcResultMatch || file == nullptr) {
132 				continue;
133 			}
134 
135 			/* Get a font with the right spacing .*/
136 			int value = 0;
137 			FcPatternGetInteger(font, FC_SPACING, 0, &value);
138 			if (callback->Monospace() != (value == FC_MONO) && value != FC_DUAL) continue;
139 
140 			/* Do not use those that explicitly say they're slanted. */
141 			FcPatternGetInteger(font, FC_SLANT, 0, &value);
142 			if (value != 0) continue;
143 
144 			/* We want the fatter font as they look better at small sizes. */
145 			FcPatternGetInteger(font, FC_WEIGHT, 0, &value);
146 			if (value <= best_weight) continue;
147 
148 			callback->SetFontNames(settings, (const char *)file);
149 
150 			bool missing = callback->FindMissingGlyphs();
151 			Debug(freetype, 1, "Font \"{}\" misses{} glyphs", file, missing ? "" : " no");
152 
153 			if (!missing) {
154 				best_weight = value;
155 				best_font = (const char *)file;
156 			}
157 		}
158 
159 		if (best_font != nullptr) {
160 			ret = true;
161 			callback->SetFontNames(settings, best_font);
162 			InitFreeType(callback->Monospace());
163 		}
164 
165 		/* Clean up the list of filenames. */
166 		FcFontSetDestroy(fs);
167 	}
168 
169 	FcFini();
170 	return ret;
171 }
172