2 * \file GuiFontLoader.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
9 * Full author contact details are available in file CREDITS.
14 #include "GuiFontLoader.h"
15 #include "qt_helpers.h"
20 #include "support/convert.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/Systemcall.h"
24 #include "support/Package.h"
25 #include "support/os.h"
27 #include <boost/tuple/tuple.hpp>
30 #include <QFontDatabase>
38 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
39 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
40 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
45 using support::contains;
46 using support::package;
47 using support::addPath;
48 using support::addName;
50 extern docstring const stateText(FontInfo const & f);
57 FontFamily lyx_family;
62 SymbolFont symbol_fonts[] = {
65 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
69 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
73 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
77 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
81 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
85 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
89 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
93 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
97 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
101 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
104 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont);
107 QString getRawName(QString const & family)
109 for (size_t i = 0; i < nr_symbol_fonts; ++i)
110 if (family == symbol_fonts[i].family)
111 return symbol_fonts[i].xlfd;
113 LYXERR(Debug::FONT, "BUG: family not found !");
118 QString const symbolFamily(FontFamily family)
120 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
121 if (family == symbol_fonts[i].lyx_family)
122 return symbol_fonts[i].family;
128 bool isSymbolFamily(FontFamily family)
130 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
134 static bool isChosenFont(QFont & font, QString const & family)
136 // QFontInfo won't find a font that has only a few glyphs at unusual
137 // positions, e.g. the original esint10 font.
138 // The workaround is to add dummy glyphs at least at all ASCII
142 LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
144 if (fi.family().contains(family)) {
145 LYXERR(Debug::FONT, " got it ");
153 static pair<QFont, bool> const getSymbolFont(QString const & family)
155 LYXERR(Debug::FONT, "Looking for font family "
156 << fromqstr(family) << " ... ");
157 QString upper = family;
158 upper[0] = family[0].toUpper();
161 font.setKerning(false);
162 font.setFamily(family);
164 if (isChosenFont(font, family)) {
165 LYXERR(Debug::FONT, "normal!");
166 return make_pair<QFont, bool>(font, true);
169 LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
170 font.setFamily(upper);
172 if (isChosenFont(font, upper)) {
173 LYXERR(Debug::FONT, "upper!");
174 return make_pair<QFont, bool>(font, true);
177 // A simple setFamily() fails on Qt 2
179 QString const rawName = getRawName(family);
180 LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... ");
181 font.setRawName(rawName);
183 if (isChosenFont(font, family)) {
184 LYXERR(Debug::FONT, "raw version!");
185 return make_pair<QFont, bool>(font, true);
188 LYXERR(Debug::FONT, " FAILED :-(");
189 return make_pair<QFont, bool>(font, false);
195 GuiFontLoader::GuiFontLoader()
197 QString const fonts_dir =
198 toqstr(addPath(package().system_support().absFilename(), "fonts"));
200 for (int i = 0 ; i < num_math_fonts; ++i) {
201 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
202 int fontID = QFontDatabase::addApplicationFont(font_file);
204 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
205 << static_cast<const char *>
206 (fontID < 0 ? " FAIL" : " OK"));
209 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
210 for (int i2 = 0; i2 < 2; ++i2)
211 for (int i3 = 0; i3 < 4; ++i3)
212 for (int i4 = 0; i4 < 10; ++i4)
213 fontinfo_[i1][i2][i3][i4] = 0;
217 void GuiFontLoader::update()
219 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
220 for (int i2 = 0; i2 < 2; ++i2)
221 for (int i3 = 0; i3 < 4; ++i3)
222 for (int i4 = 0; i4 < 10; ++i4) {
223 delete fontinfo_[i1][i2][i3][i4];
224 fontinfo_[i1][i2][i3][i4] = 0;
230 /////////////////////////////////////////////////
233 static QString makeFontName(QString const & family, QString const & foundry)
235 QString res = family;
236 if (!foundry.isEmpty())
237 res += " [" + foundry + ']';
242 GuiFontInfo::GuiFontInfo(FontInfo const & f)
244 font.setKerning(false);
245 QString const pat = symbolFamily(f.family());
246 if (!pat.isEmpty()) {
248 boost::tie(font, tmp) = getSymbolFont(pat);
250 switch (f.family()) {
252 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
253 toqstr(lyxrc.roman_font_foundry));
254 font.setFamily(family);
256 #if QT_VERSION >= 0x040300
257 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
258 // It is reported to Trolltech at 02/06/07 against 4.3 final.
259 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
260 if (family == "Times" && !font.exactMatch())
261 font.setFamily("Times New Roman");
267 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
268 toqstr(lyxrc.sans_font_foundry)));
270 case TYPEWRITER_FAMILY:
271 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
272 toqstr(lyxrc.typewriter_font_foundry)));
279 switch (f.series()) {
281 font.setWeight(QFont::Normal);
284 font.setWeight(QFont::Bold);
290 switch (f.realShape()) {
293 font.setItalic(true);
299 LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
300 << "' matched by\n" << fromqstr(font.family()));
302 // Is this an exact match?
303 if (font.exactMatch())
304 LYXERR(Debug::FONT, "This font is an exact match");
306 LYXERR(Debug::FONT, "This font is NOT an exact match");
308 LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
310 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
311 * lyxrc.zoom / 100.0);
313 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
315 if (f.realShape() != SMALLCAPS_SHAPE) {
316 metrics.reset(new GuiFontMetrics(font));
318 // handle small caps ourselves ...
319 FontInfo smallfont = f;
320 smallfont.decSize().decSize().setShape(UP_SHAPE);
322 font2.setKerning(false);
323 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
324 * lyxrc.zoom / 100.0);
326 metrics.reset(new GuiFontMetrics(font, font2));
332 bool GuiFontLoader::available(FontInfo const & f)
334 static vector<int> cache_set(NUM_FAMILIES, false);
335 static vector<int> cache(NUM_FAMILIES, false);
337 FontFamily family = f.family();
338 if (cache_set[family])
339 return cache[family];
340 cache_set[family] = true;
342 QString const pat = symbolFamily(family);
344 // We don't care about non-symbol fonts
347 pair<QFont, bool> tmp = getSymbolFont(pat);
351 cache[family] = true;
355 } // namespace frontend