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>
37 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
38 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
39 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
44 using support::contains;
45 using support::package;
46 using support::addPath;
47 using support::addName;
49 extern docstring const stateText(FontInfo const & f);
56 FontFamily lyx_family;
61 SymbolFont symbol_fonts[] = {
64 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
68 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
72 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
76 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
80 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
84 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
88 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
92 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
96 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
100 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
103 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont);
106 QString getRawName(QString const & family)
108 for (size_t i = 0; i < nr_symbol_fonts; ++i)
109 if (family == symbol_fonts[i].family)
110 return symbol_fonts[i].xlfd;
112 LYXERR(Debug::FONT, "BUG: family not found !");
117 QString const symbolFamily(FontFamily family)
119 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
120 if (family == symbol_fonts[i].lyx_family)
121 return symbol_fonts[i].family;
127 bool isSymbolFamily(FontFamily family)
129 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
133 static bool isChosenFont(QFont & font, QString const & family)
135 // QFontInfo won't find a font that has only a few glyphs at unusual
136 // positions, e.g. the original esint10 font.
137 // The workaround is to add dummy glyphs at least at all ASCII
141 LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
143 if (fi.family().contains(family)) {
144 LYXERR(Debug::FONT, " got it ");
152 static pair<QFont, bool> const getSymbolFont(QString const & family)
154 LYXERR(Debug::FONT, "Looking for font family "
155 << fromqstr(family) << " ... ");
156 QString upper = family;
157 upper[0] = family[0].toUpper();
160 font.setKerning(false);
161 font.setFamily(family);
163 if (isChosenFont(font, family)) {
164 LYXERR(Debug::FONT, "normal!");
165 return make_pair<QFont, bool>(font, true);
168 LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
169 font.setFamily(upper);
171 if (isChosenFont(font, upper)) {
172 LYXERR(Debug::FONT, "upper!");
173 return make_pair<QFont, bool>(font, true);
176 // A simple setFamily() fails on Qt 2
178 QString const rawName = getRawName(family);
179 LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... ");
180 font.setRawName(rawName);
182 if (isChosenFont(font, family)) {
183 LYXERR(Debug::FONT, "raw version!");
184 return make_pair<QFont, bool>(font, true);
187 LYXERR(Debug::FONT, " FAILED :-(");
188 return make_pair<QFont, bool>(font, false);
194 GuiFontLoader::GuiFontLoader()
196 QString const fonts_dir =
197 toqstr(addPath(package().system_support().absFilename(), "fonts"));
199 for (int i = 0 ; i < num_math_fonts; ++i) {
200 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
201 int fontID = QFontDatabase::addApplicationFont(font_file);
203 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
204 << static_cast<const char *>
205 (fontID < 0 ? " FAIL" : " OK"));
208 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
209 for (int i2 = 0; i2 < 2; ++i2)
210 for (int i3 = 0; i3 < 4; ++i3)
211 for (int i4 = 0; i4 < 10; ++i4)
212 fontinfo_[i1][i2][i3][i4] = 0;
216 void GuiFontLoader::update()
218 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
219 for (int i2 = 0; i2 < 2; ++i2)
220 for (int i3 = 0; i3 < 4; ++i3)
221 for (int i4 = 0; i4 < 10; ++i4) {
222 delete fontinfo_[i1][i2][i3][i4];
223 fontinfo_[i1][i2][i3][i4] = 0;
229 /////////////////////////////////////////////////
232 static QString makeFontName(QString const & family, QString const & foundry)
234 QString res = family;
235 if (!foundry.isEmpty())
236 res += " [" + foundry + ']';
241 GuiFontInfo::GuiFontInfo(FontInfo const & f)
243 font.setKerning(false);
244 QString const pat = symbolFamily(f.family());
245 if (!pat.isEmpty()) {
247 boost::tie(font, tmp) = getSymbolFont(pat);
249 switch (f.family()) {
251 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
252 toqstr(lyxrc.roman_font_foundry));
253 font.setFamily(family);
255 #if QT_VERSION >= 0x040300
256 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
257 // It is reported to Trolltech at 02/06/07 against 4.3 final.
258 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
259 if (family == "Times" && !font.exactMatch())
260 font.setFamily("Times New Roman");
266 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
267 toqstr(lyxrc.sans_font_foundry)));
269 case TYPEWRITER_FAMILY:
270 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
271 toqstr(lyxrc.typewriter_font_foundry)));
278 switch (f.series()) {
280 font.setWeight(QFont::Normal);
283 font.setWeight(QFont::Bold);
289 switch (f.realShape()) {
292 font.setItalic(true);
298 LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
299 << "' matched by\n" << fromqstr(font.family()));
301 // Is this an exact match?
302 if (font.exactMatch())
303 LYXERR(Debug::FONT, "This font is an exact match");
305 LYXERR(Debug::FONT, "This font is NOT an exact match");
307 LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
309 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
310 * lyxrc.zoom / 100.0);
312 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
314 if (f.realShape() != SMALLCAPS_SHAPE) {
315 metrics.reset(new GuiFontMetrics(font));
317 // handle small caps ourselves ...
318 FontInfo smallfont = f;
319 smallfont.decSize().decSize().setShape(UP_SHAPE);
321 font2.setKerning(false);
322 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
323 * lyxrc.zoom / 100.0);
325 metrics.reset(new GuiFontMetrics(font, font2));
331 bool GuiFontLoader::available(FontInfo const & f)
333 static vector<int> cache_set(NUM_FAMILIES, false);
334 static vector<int> cache(NUM_FAMILIES, false);
336 FontFamily family = f.family();
337 if (cache_set[family])
338 return cache[family];
339 cache_set[family] = true;
341 QString const pat = symbolFamily(family);
343 // We don't care about non-symbol fonts
346 pair<QFont, bool> tmp = getSymbolFont(pat);
350 cache[family] = true;
354 } // namespace frontend