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>
32 using lyx::support::contains;
33 using lyx::support::package;
34 using lyx::support::addPath;
35 using lyx::support::addName;
43 #if QT_VERSION >= 0x040200
44 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
45 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
46 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
56 Font::FONT_FAMILY lyx_family;
61 SymbolFont symbol_fonts[] = {
62 { Font::SYMBOL_FAMILY,
64 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
68 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
72 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
76 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
80 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
84 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
88 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
90 { Font::EUFRAK_FAMILY,
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 !" << endl;
117 QString const symbolFamily(Font::FONT_FAMILY 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(Font::FONT_FAMILY family)
129 return family >= Font::SYMBOL_FAMILY &&
130 family <= Font::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()) << endl;
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!" << endl;
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!" << endl;
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!" << endl;
185 return make_pair<QFont, bool>(font, true);
188 LYXERR(Debug::FONT) << " FAILED :-(" << endl;
189 return make_pair<QFont, bool>(font, false);
195 GuiFontLoader::GuiFontLoader()
197 #if QT_VERSION >= 0x040200
198 QString const fonts_dir =
199 toqstr(addPath(package().system_support().absFilename(), "fonts"));
201 for (int i = 0 ; i < num_math_fonts; ++i) {
202 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
203 int fontID = QFontDatabase::addApplicationFont(font_file);
205 LYXERR(Debug::FONT) << "Adding font " << fromqstr(font_file)
206 << static_cast<const char *>
207 (fontID < 0 ? " FAIL" : " OK")
212 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1)
213 for (int i2 = 0; i2 < 2; ++i2)
214 for (int i3 = 0; i3 < 4; ++i3)
215 for (int i4 = 0; i4 < 10; ++i4)
216 fontinfo_[i1][i2][i3][i4] = 0;
220 void GuiFontLoader::update()
222 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
223 for (int i2 = 0; i2 < 2; ++i2)
224 for (int i3 = 0; i3 < 4; ++i3)
225 for (int i4 = 0; i4 < 10; ++i4) {
226 delete fontinfo_[i1][i2][i3][i4];
227 fontinfo_[i1][i2][i3][i4] = 0;
233 /////////////////////////////////////////////////
236 static QString makeFontName(QString const & family, QString const & foundry)
238 QString res = family;
239 if (!foundry.isEmpty())
240 res += " [" + foundry + ']';
245 GuiFontInfo::GuiFontInfo(Font const & f)
247 font.setKerning(false);
248 QString const pat = symbolFamily(f.family());
249 if (!pat.isEmpty()) {
251 boost::tie(font, tmp) = getSymbolFont(pat);
253 switch (f.family()) {
254 case Font::ROMAN_FAMILY: {
255 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
256 toqstr(lyxrc.roman_font_foundry));
257 font.setFamily(family);
259 #if QT_VERSION >= 0x040300
260 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
261 // It is reported to Trolltech at 02/06/07 against 4.3 final.
262 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
263 if (family == "Times" && !font.exactMatch())
264 font.setFamily("Times New Roman");
269 case Font::SANS_FAMILY:
270 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
271 toqstr(lyxrc.sans_font_foundry)));
273 case Font::TYPEWRITER_FAMILY:
274 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
275 toqstr(lyxrc.typewriter_font_foundry)));
282 switch (f.series()) {
283 case Font::MEDIUM_SERIES:
284 font.setWeight(QFont::Normal);
286 case Font::BOLD_SERIES:
287 font.setWeight(QFont::Bold);
293 switch (f.realShape()) {
294 case Font::ITALIC_SHAPE:
295 case Font::SLANTED_SHAPE:
296 font.setItalic(true);
302 LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
303 << "' matched by\n" << fromqstr(font.family()) << endl;
305 // Is this an exact match?
306 if (font.exactMatch())
307 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
309 LYXERR(Debug::FONT) << "This font is NOT an exact match"
312 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
314 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
315 * lyxrc.zoom / 100.0);
317 LYXERR(Debug::FONT) << "The font has size: "
318 << font.pointSizeF() << endl;
320 if (f.realShape() != Font::SMALLCAPS_SHAPE) {
321 metrics.reset(new GuiFontMetrics(font));
324 // handle small caps ourselves ...
326 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
328 font2.setKerning(false);
329 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
330 * lyxrc.zoom / 100.0);
332 metrics.reset(new GuiFontMetrics(font, font2));
338 bool GuiFontLoader::available(Font const & f)
340 static vector<int> cache_set(Font::NUM_FAMILIES, false);
341 static vector<int> cache(Font::NUM_FAMILIES, false);
343 Font::FONT_FAMILY family = f.family();
344 if (cache_set[family])
345 return cache[family];
346 cache_set[family] = true;
348 QString const pat = symbolFamily(family);
350 // We don't care about non-symbol fonts
353 pair<QFont, bool> tmp = getSymbolFont(pat);
357 cache[family] = true;
361 } // namespace frontend