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"
28 #include <QFontDatabase>
30 using lyx::support::contains;
31 using lyx::support::package;
32 using lyx::support::addPath;
33 using lyx::support::addName;
42 #if QT_VERSION >= 0x040200
43 string const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
44 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
45 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
55 Font::FONT_FAMILY lyx_family;
60 symbol_font symbol_fonts[] = {
61 { Font::SYMBOL_FAMILY,
63 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
67 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
71 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
75 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
79 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
87 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
89 { Font::EUFRAK_FAMILY,
91 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
95 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
99 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
102 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
105 string getRawName(string const & family)
107 for (size_t i = 0; i < nr_symbol_fonts; ++i)
108 if (family == symbol_fonts[i].family)
109 return symbol_fonts[i].xlfd;
111 LYXERR(Debug::FONT) << "BUG: family not found !" << endl;
116 string const symbolFamily(Font::FONT_FAMILY family)
118 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
119 if (family == symbol_fonts[i].lyx_family)
120 return symbol_fonts[i].family;
126 bool isSymbolFamily(Font::FONT_FAMILY family)
128 return family >= Font::SYMBOL_FAMILY &&
129 family <= Font::ESINT_FAMILY;
133 bool isChosenFont(QFont & font, string 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()) << endl;
143 if (contains(fromqstr(fi.family()), family)) {
144 LYXERR(Debug::FONT) << " got it ";
152 pair<QFont, bool> const getSymbolFont(string const & family)
154 LYXERR(Debug::FONT) << "Looking for font family "
155 << family << " ... ";
156 string upper = family;
157 upper[0] = toupper(family[0]);
160 font.setKerning(false);
161 font.setFamily(toqstr(family));
163 if (isChosenFont(font, family)) {
164 LYXERR(Debug::FONT) << "normal!" << endl;
165 return make_pair<QFont, bool>(font, true);
168 LYXERR(Debug::FONT) << "Trying " << upper << " ... ";
169 font.setFamily(toqstr(upper));
171 if (isChosenFont(font, upper)) {
172 LYXERR(Debug::FONT) << "upper!" << endl;
173 return make_pair<QFont, bool>(font, true);
176 // A simple setFamily() fails on Qt 2
178 string const rawName = getRawName(family);
179 LYXERR(Debug::FONT) << "Trying " << rawName << " ... ";
180 font.setRawName(toqstr(rawName));
182 if (isChosenFont(font, family)) {
183 LYXERR(Debug::FONT) << "raw version!" << endl;
184 return make_pair<QFont, bool>(font, true);
187 LYXERR(Debug::FONT) << " FAILED :-(" << endl;
188 return make_pair<QFont, bool>(font, false);
194 GuiFontLoader::GuiFontLoader()
196 #if QT_VERSION >= 0x040200
197 fontID = new int[num_math_fonts];
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 fontID[i] = QFontDatabase::addApplicationFont(toqstr(font_file));
207 LYXERR(Debug::FONT) << "Adding font " << font_file
208 << static_cast<const char *>
209 (fontID[i] < 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 GuiFontLoader::~GuiFontLoader()
224 #if QT_VERSION >= 0x040200
225 for (int i = 0 ; i < num_math_fonts; ++i) {
227 QFontDatabase::removeApplicationFont(fontID[i]);
235 void GuiFontLoader::update()
237 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
238 for (int i2 = 0; i2 < 2; ++i2)
239 for (int i3 = 0; i3 < 4; ++i3)
240 for (int i4 = 0; i4 < 10; ++i4) {
241 delete fontinfo_[i1][i2][i3][i4];
242 fontinfo_[i1][i2][i3][i4] = 0;
248 /////////////////////////////////////////////////
251 QLFontInfo::QLFontInfo(Font const & f)
253 font.setKerning(false);
254 string const pat = symbolFamily(f.family());
257 boost::tie(font, tmp) = getSymbolFont(pat);
259 switch (f.family()) {
260 case Font::ROMAN_FAMILY: {
261 QString family = toqstr(makeFontName(lyxrc.roman_font_name,
262 lyxrc.roman_font_foundry));
263 font.setFamily(family);
265 #if QT_VERSION >= 0x040300
266 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
267 // It is reported to Trolltech at 02/06/07 against 4.3 final.
268 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
269 if (family == "Times" && !font.exactMatch())
270 font.setFamily(QString::fromLatin1("Times New Roman"));
275 case Font::SANS_FAMILY:
276 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
277 lyxrc.sans_font_foundry)));
279 case Font::TYPEWRITER_FAMILY:
280 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
281 lyxrc.typewriter_font_foundry)));
288 switch (f.series()) {
289 case Font::MEDIUM_SERIES:
290 font.setWeight(QFont::Normal);
292 case Font::BOLD_SERIES:
293 font.setWeight(QFont::Bold);
299 switch (f.realShape()) {
300 case Font::ITALIC_SHAPE:
301 case Font::SLANTED_SHAPE:
302 font.setItalic(true);
308 LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
309 << "' matched by\n" << fromqstr(font.family()) << endl;
311 // Is this an exact match?
312 if (font.exactMatch())
313 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
315 LYXERR(Debug::FONT) << "This font is NOT an exact match"
318 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
320 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
321 * lyxrc.zoom / 100.0);
323 LYXERR(Debug::FONT) << "The font has size: "
324 << font.pointSizeF() << endl;
326 if (f.realShape() != Font::SMALLCAPS_SHAPE) {
327 metrics.reset(new GuiFontMetrics(font));
330 // handle small caps ourselves ...
332 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
334 font2.setKerning(false);
335 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
336 * lyxrc.zoom / 100.0);
338 metrics.reset(new GuiFontMetrics(font, font2));
344 bool GuiFontLoader::available(Font const & f)
346 static vector<int> cache_set(Font::NUM_FAMILIES, false);
347 static vector<int> cache(Font::NUM_FAMILIES, false);
349 Font::FONT_FAMILY family = f.family();
350 if (cache_set[family])
351 return cache[family];
352 cache_set[family] = true;
354 string const pat = symbolFamily(family);
356 // We don't care about non-symbol fonts
359 pair<QFont, bool> tmp = getSymbolFont(pat);
363 cache[family] = true;
367 } // namespace frontend