The Battle for Wesnoth  1.15.9+dev
font_config.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by Chris Beck<render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "font/font_config.hpp"
17 #include "font/error.hpp"
18 #include "font/sdl_ttf.hpp"
19 
20 #include "config.hpp"
21 #include "log.hpp"
22 #include "tstring.hpp"
23 
24 #include "filesystem.hpp"
25 #include "game_config.hpp"
26 
27 #include "serialization/parser.hpp"
31 #include "preferences/general.hpp"
32 
33 #include <list>
34 #include <set>
35 #include <stack>
36 #include <sstream>
37 #include <vector>
38 
39 #include <cairo-features.h>
40 
41 #ifdef CAIRO_HAS_WIN32_FONT
42 #include <windows.h>
43 #undef CAIRO_HAS_FT_FONT
44 #endif
45 
46 #ifdef CAIRO_HAS_FT_FONT
47 #include <fontconfig/fontconfig.h>
48 #endif
49 
50 #if !defined(CAIRO_HAS_FT_FONT) && !defined(CAIRO_HAS_WIN32_FONT)
51 #error unable to find font loading tools.
52 #endif
53 
54 static lg::log_domain log_font("font");
55 #define DBG_FT LOG_STREAM(debug, log_font)
56 #define LOG_FT LOG_STREAM(info, log_font)
57 #define WRN_FT LOG_STREAM(warn, log_font)
58 #define ERR_FT LOG_STREAM(err, log_font)
59 
60 namespace font {
61 
62 
63 bool check_font_file(std::string name) {
64  if(game_config::path.empty() == false) {
65  if(!filesystem::file_exists(game_config::path + "/fonts/" + name)) {
66  if(!filesystem::file_exists("fonts/" + name)) {
67  if(!filesystem::file_exists(name)) {
68  WRN_FT << "Failed opening font file '" << name << "': No such file or directory" << std::endl;
69  return false;
70  }
71  }
72  }
73  } else {
74  if(!filesystem::file_exists("fonts/" + name)) {
75  if(!filesystem::file_exists(name)) {
76  WRN_FT << "Failed opening font file '" << name << "': No such file or directory" << std::endl;
77  return false;
78  }
79  }
80  }
81  return true;
82 }
83 
84 namespace
85 {
86 bool add_font_to_fontlist(const config &fonts_config,
87  std::vector<font::subset_descriptor>& fontlist, const std::string& name)
88 {
89  const config &font = fonts_config.find_child("font", "name", name);
90  if (!font) {
91  return false;
92  }
93  //DBG_FT << "Adding a font record: " << font.debug() << std::endl;
94 
95  fontlist.push_back(font::subset_descriptor(font));
96 
97  return true;
98 }
99 
100 #ifdef CAIRO_HAS_WIN32_FONT
101 bool is_valid_font_file(const std::string& file)
102 {
103  static const std::array<std::string, 3> font_exts { ".ttf", ".ttc", ".otf" };
104 
105  for(const std::string& ext : font_exts) {
106  if(filesystem::ends_with(file, ext)) {
107  return true;
108  }
109  }
110 
111  return false;
112 }
113 #endif
114 
115 // Current font family for sanserif and monospace fonts in the game
116 
117 t_string family_order_sans;
118 t_string family_order_mono;
119 t_string family_order_light;
120 t_string family_order_script;
121 
122 } // end anon namespace
123 
124 /***
125  * Public interface
126  */
127 
129 {
130  config cfg;
131  try {
132  const std::string& cfg_path = filesystem::get_wml_location("hardwired/fonts.cfg");
133  if(cfg_path.empty()) {
134  ERR_FT << "could not resolve path to fonts.cfg, file not found\n";
135  return false;
136  }
137 
138  filesystem::scoped_istream stream = preprocess_file(cfg_path);
139  read(cfg, *stream);
140  } catch(const config::error &e) {
141  ERR_FT << "could not read fonts.cfg:\n"
142  << e.message << '\n';
143  return false;
144  }
145 
146  const config &fonts_config = cfg.child("fonts");
147  if (!fonts_config)
148  return false;
149 
150  std::set<std::string> known_fonts;
151  for (const config &font : fonts_config.child_range("font")) {
152  known_fonts.insert(font["name"]);
153  if (font.has_attribute("bold_name")) {
154  known_fonts.insert(font["bold_name"]);
155  }
156  if (font.has_attribute("italic_name")) {
157  known_fonts.insert(font["italic_name"]);
158  }
159  }
160 
161  family_order_sans = fonts_config["family_order"];
162  family_order_mono = fonts_config["family_order_monospace"];
163  family_order_light = fonts_config["family_order_light"];
164  family_order_script = fonts_config["family_order_script"];
165 
166  if(family_order_mono.empty()) {
167  ERR_FT << "No monospace font family order defined, falling back to sans serif order\n";
168  family_order_mono = family_order_sans;
169  }
170 
171  if(family_order_light.empty()) {
172  ERR_FT << "No light font family order defined, falling back to sans serif order\n";
173  family_order_light = family_order_sans;
174  }
175 
176  if(family_order_script.empty()) {
177  ERR_FT << "No script font family order defined, falling back to sans serif order\n";
178  family_order_script = family_order_sans;
179  }
180 
181  std::vector<font::subset_descriptor> fontlist;
182 
183  for(auto font : utils::split(fonts_config["order"])) {
184  add_font_to_fontlist(fonts_config, fontlist, font);
185  known_fonts.erase(font);
186  }
187 
188  for(auto kfont : known_fonts) {
189  add_font_to_fontlist(fonts_config, fontlist, kfont);
190  }
191 
192  if(fontlist.empty())
193  return false;
194 
195  sdl_ttf::set_font_list(fontlist);
196  return true;
197 }
198 
200 {
201  switch(fclass) {
202  case FONT_MONOSPACE:
203  return family_order_mono;
204  case FONT_LIGHT:
205  return family_order_light;
206  case FONT_SCRIPT:
207  return family_order_script;
208  default:
209  return family_order_sans;
210  }
211 }
212 
213 /***
214  * Manager member functions
215  */
216 
218 {
219 #ifdef CAIRO_HAS_FT_FONT
220  std::string font_path = game_config::path + "/fonts";
221  if (!FcConfigAppFontAddDir(FcConfigGetCurrent(),
222  reinterpret_cast<const FcChar8 *>(font_path.c_str())))
223  {
224  ERR_FT << "Could not load the true type fonts" << std::endl;
225  throw font::error("font config lib failed to add the font path: '" + font_path + "'");
226  }
227 
228  std::string font_file = font_path + "/fonts.conf";
229  if(!FcConfigParseAndLoad(FcConfigGetCurrent(),
230  reinterpret_cast<const FcChar8*>(font_file.c_str()),
231  FcFalse))
232  {
233  ERR_FT << "Could not load local font configuration\n";
234  throw font::error("font config lib failed to find font.conf: '" + font_file + "'");
235  }
236  else
237  {
238  LOG_FT << "Local font configuration loaded\n";
239  }
240 #endif
241 
242 #ifdef CAIRO_HAS_WIN32_FONT
243  for(const std::string& path : filesystem::get_binary_paths("fonts")) {
244  std::vector<std::string> files;
247  }
248  for(const std::string& file : files) {
249  if(is_valid_font_file(file))
250  {
251  const std::wstring wfile = unicode_cast<std::wstring>(file);
252  AddFontResourceExW(wfile.c_str(), FR_PRIVATE, nullptr);
253  }
254  }
255  }
256 #endif
257 }
258 
260 {
261 #ifdef CAIRO_HAS_FT_FONT
262  FcConfigAppFontClear(FcConfigGetCurrent());
263 #endif
264 
265 #ifdef CAIRO_HAS_WIN32_FONT
266  for(const std::string& path : filesystem::get_binary_paths("fonts")) {
267  std::vector<std::string> files;
270  for(const std::string& file : files) {
271  if(is_valid_font_file(file))
272  {
273  const std::wstring wfile = unicode_cast<std::wstring>(file);
274  RemoveFontResourceExW(wfile.c_str(), FR_PRIVATE, nullptr);
275  }
276  }
277  }
278 #endif
279 }
280 
281 
282 } // end namespace font
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:415
family_class
Font classes for get_font_families().
bool check_font_file(std::string name)
Test if a font file exists.
Definition: font_config.cpp:63
Note: Specific to sdl_ttf.
config & find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:831
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:263
bool ends_with(const std::string &str, const std::string &suffix)
ucs4_convert_impl::enableif< TD, typename TS::value_type >::type unicode_cast(const TS &source)
child_itors child_range(config_key_type key)
Definition: config.cpp:357
#define WRN_FT
Definition: font_config.cpp:57
#define LOG_FT
Definition: font_config.cpp:56
Definitions for the interface to Wesnoth Markup Language (WML).
const t_string & get_font_families(family_class fclass)
Returns the currently defined fonts.
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:626
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Populates &#39;files&#39; with all the files and &#39;dirs&#39; with all the directories in dir.
Definition: filesystem.cpp:349
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
std::unique_ptr< std::istream > scoped_istream
Definition: filesystem.hpp:36
std::string path
Definition: game_config.cpp:38
static lg::log_domain log_font("font")
bool load_font_config()
const std::vector< std::string > & get_binary_paths(const std::string &type)
Returns a vector with all possible paths to a given type of binary, e.g.
std::string get_wml_location(const std::string &filename, const std::string &current_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn&#39;t pres...
static void set_font_list(const std::vector< subset_descriptor > &fontlist)
Set the list of fonts.
Definition: sdl_ttf.cpp:471
std::string name
Definition: sdl_ttf.cpp:70
Declarations for File-IO.
#define ERR_FT
Definition: font_config.cpp:58
std::vector< std::string > split(const config_attribute_value &val)
Standard logging facilities (interface).
std::string message
Definition: exceptions.hpp:31
#define e
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines)
Function to use the WML preprocessor on a file.