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;
44 #if QT_VERSION >= 0x040200
45 string const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
46 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
47 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
57 Font::FONT_FAMILY lyx_family;
62 symbol_font symbol_fonts[] = {
63 { Font::SYMBOL_FAMILY,
65 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
69 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
73 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
77 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
81 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
85 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
89 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
91 { Font::EUFRAK_FAMILY,
93 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
97 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
101 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
104 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
107 string getRawName(string 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 !" << endl;
118 string const symbolFamily(Font::FONT_FAMILY 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(Font::FONT_FAMILY family)
130 return family >= Font::SYMBOL_FAMILY &&
131 family <= Font::ESINT_FAMILY;
135 bool isChosenFont(QFont & font, string const & family)
137 // QFontInfo won't find a font that has only a few glyphs at unusual
138 // positions, e.g. the original esint10 font.
139 // The workaround is to add dummy glyphs at least at all ASCII
143 LYXERR(Debug::FONT) << "got: " << fromqstr(fi.family()) << endl;
145 if (contains(fromqstr(fi.family()), family)) {
146 LYXERR(Debug::FONT) << " got it ";
154 pair<QFont, bool> const getSymbolFont(string const & family)
156 LYXERR(Debug::FONT) << "Looking for font family "
157 << family << " ... ";
158 string upper = family;
159 upper[0] = toupper(family[0]);
162 font.setKerning(false);
163 font.setFamily(toqstr(family));
165 if (isChosenFont(font, family)) {
166 LYXERR(Debug::FONT) << "normal!" << endl;
167 return make_pair<QFont, bool>(font, true);
170 LYXERR(Debug::FONT) << "Trying " << upper << " ... ";
171 font.setFamily(toqstr(upper));
173 if (isChosenFont(font, upper)) {
174 LYXERR(Debug::FONT) << "upper!" << endl;
175 return make_pair<QFont, bool>(font, true);
178 // A simple setFamily() fails on Qt 2
180 string const rawName = getRawName(family);
181 LYXERR(Debug::FONT) << "Trying " << rawName << " ... ";
182 font.setRawName(toqstr(rawName));
184 if (isChosenFont(font, family)) {
185 LYXERR(Debug::FONT) << "raw version!" << endl;
186 return make_pair<QFont, bool>(font, true);
189 LYXERR(Debug::FONT) << " FAILED :-(" << endl;
190 return make_pair<QFont, bool>(font, false);
196 GuiFontLoader::GuiFontLoader()
198 #if QT_VERSION >= 0x040200
199 string const fonts_dir =
200 addPath(package().system_support().absFilename(), "fonts");
202 for (int i = 0 ; i < num_math_fonts; ++i) {
203 string const font_file = lyx::support::os::external_path(
204 addName(fonts_dir, math_fonts[i] + ".ttf"));
205 int fontID = QFontDatabase::addApplicationFont(toqstr(font_file));
207 LYXERR(Debug::FONT) << "Adding font " << font_file
208 << static_cast<const char *>
209 (fontID < 0 ? " FAIL" : " OK")
214 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1)
215 for (int i2 = 0; i2 < 2; ++i2)
216 for (int i3 = 0; i3 < 4; ++i3)
217 for (int i4 = 0; i4 < 10; ++i4)
218 fontinfo_[i1][i2][i3][i4] = 0;
222 void GuiFontLoader::update()
224 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
225 for (int i2 = 0; i2 < 2; ++i2)
226 for (int i3 = 0; i3 < 4; ++i3)
227 for (int i4 = 0; i4 < 10; ++i4) {
228 delete fontinfo_[i1][i2][i3][i4];
229 fontinfo_[i1][i2][i3][i4] = 0;
235 /////////////////////////////////////////////////
238 static QString makeFontName(string const & family, string const & foundry)
240 QString res = toqstr(family);
241 if (!foundry.empty())
242 res += " [" + toqstr(foundry) + ']';
247 QLFontInfo::QLFontInfo(Font const & f)
249 font.setKerning(false);
250 string const pat = symbolFamily(f.family());
253 boost::tie(font, tmp) = getSymbolFont(pat);
255 switch (f.family()) {
256 case Font::ROMAN_FAMILY: {
257 QString family = makeFontName(lyxrc.roman_font_name,
258 lyxrc.roman_font_foundry);
259 font.setFamily(family);
261 #if QT_VERSION >= 0x040300
262 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
263 // It is reported to Trolltech at 02/06/07 against 4.3 final.
264 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
265 if (family == "Times" && !font.exactMatch())
266 font.setFamily(QString::fromLatin1("Times New Roman"));
271 case Font::SANS_FAMILY:
272 font.setFamily(makeFontName(lyxrc.sans_font_name,
273 lyxrc.sans_font_foundry));
275 case Font::TYPEWRITER_FAMILY:
276 font.setFamily(makeFontName(lyxrc.typewriter_font_name,
277 lyxrc.typewriter_font_foundry));
284 switch (f.series()) {
285 case Font::MEDIUM_SERIES:
286 font.setWeight(QFont::Normal);
288 case Font::BOLD_SERIES:
289 font.setWeight(QFont::Bold);
295 switch (f.realShape()) {
296 case Font::ITALIC_SHAPE:
297 case Font::SLANTED_SHAPE:
298 font.setItalic(true);
304 LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
305 << "' matched by\n" << fromqstr(font.family()) << endl;
307 // Is this an exact match?
308 if (font.exactMatch())
309 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
311 LYXERR(Debug::FONT) << "This font is NOT an exact match"
314 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
316 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
317 * lyxrc.zoom / 100.0);
319 LYXERR(Debug::FONT) << "The font has size: "
320 << font.pointSizeF() << endl;
322 if (f.realShape() != Font::SMALLCAPS_SHAPE) {
323 metrics.reset(new GuiFontMetrics(font));
326 // handle small caps ourselves ...
328 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
330 font2.setKerning(false);
331 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
332 * lyxrc.zoom / 100.0);
334 metrics.reset(new GuiFontMetrics(font, font2));
340 bool GuiFontLoader::available(Font const & f)
342 static vector<int> cache_set(Font::NUM_FAMILIES, false);
343 static vector<int> cache(Font::NUM_FAMILIES, false);
345 Font::FONT_FAMILY family = f.family();
346 if (cache_set[family])
347 return cache[family];
348 cache_set[family] = true;
350 string const pat = symbolFamily(family);
352 // We don't care about non-symbol fonts
355 pair<QFont, bool> tmp = getSymbolFont(pat);
359 cache[family] = true;
363 } // namespace frontend