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 <qfontinfo.h>
28 #include <QFontDatabase>
30 #include <boost/tuple/tuple.hpp>
34 //#include <X11/Xlib.h>
38 using lyx::support::contains;
39 using lyx::support::package;
40 using lyx::support::addPath;
41 using lyx::support::addName;
50 #if QT_VERSION >= 0x040200
51 string const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
52 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
53 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
63 Font::FONT_FAMILY lyx_family;
68 symbol_font symbol_fonts[] = {
69 { Font::SYMBOL_FAMILY,
71 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
75 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
79 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
87 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
91 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
95 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
97 { Font::EUFRAK_FAMILY,
99 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
103 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
105 { Font::ESINT_FAMILY,
107 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
110 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
113 string getRawName(string const & family)
115 for (size_t i = 0; i < nr_symbol_fonts; ++i)
116 if (family == symbol_fonts[i].family)
117 return symbol_fonts[i].xlfd;
119 LYXERR(Debug::FONT) << "BUG: family not found !" << endl;
124 string const symbolFamily(Font::FONT_FAMILY family)
126 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
127 if (family == symbol_fonts[i].lyx_family)
128 return symbol_fonts[i].family;
134 bool isSymbolFamily(Font::FONT_FAMILY family)
136 return family >= Font::SYMBOL_FAMILY &&
137 family <= Font::ESINT_FAMILY;
141 bool isChosenFont(QFont & font, string const & family)
143 // QFontInfo won't find a font that has only a few glyphs at unusual
144 // positions, e.g. the original esint10 font.
145 // The workaround is to add dummy glyphs at least at all ASCII
149 LYXERR(Debug::FONT) << "got: " << fromqstr(fi.family()) << endl;
151 if (contains(fromqstr(fi.family()), family)) {
152 LYXERR(Debug::FONT) << " got it ";
160 pair<QFont, bool> const getSymbolFont(string const & family)
162 LYXERR(Debug::FONT) << "Looking for font family "
163 << family << " ... ";
164 string upper = family;
165 upper[0] = toupper(family[0]);
168 font.setKerning(false);
169 font.setFamily(toqstr(family));
171 if (isChosenFont(font, family)) {
172 LYXERR(Debug::FONT) << "normal!" << endl;
173 return make_pair<QFont, bool>(font, true);
176 LYXERR(Debug::FONT) << "Trying " << upper << " ... ";
177 font.setFamily(toqstr(upper));
179 if (isChosenFont(font, upper)) {
180 LYXERR(Debug::FONT) << "upper!" << endl;
181 return make_pair<QFont, bool>(font, true);
184 // A simple setFamily() fails on Qt 2
186 string const rawName = getRawName(family);
187 LYXERR(Debug::FONT) << "Trying " << rawName << " ... ";
188 font.setRawName(toqstr(rawName));
190 if (isChosenFont(font, family)) {
191 LYXERR(Debug::FONT) << "raw version!" << endl;
192 return make_pair<QFont, bool>(font, true);
195 LYXERR(Debug::FONT) << " FAILED :-(" << endl;
196 return make_pair<QFont, bool>(font, false);
202 GuiFontLoader::GuiFontLoader()
204 #if QT_VERSION >= 0x040200
205 fontID = new int[num_math_fonts];
207 string const fonts_dir =
208 addPath(package().system_support().absFilename(), "fonts");
210 for (int i = 0 ; i < num_math_fonts; ++i) {
211 string const font_file = lyx::support::os::external_path(
212 addName(fonts_dir, math_fonts[i] + ".ttf"));
213 fontID[i] = QFontDatabase::addApplicationFont(toqstr(font_file));
215 LYXERR(Debug::FONT) << "Adding font " << font_file
216 << static_cast<const char *>
217 (fontID[i] < 0 ? " FAIL" : " OK")
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 fontinfo_[i1][i2][i3][i4] = 0;
230 GuiFontLoader::~GuiFontLoader()
232 #if QT_VERSION >= 0x040200
233 for (int i = 0 ; i < num_math_fonts; ++i) {
235 QFontDatabase::removeApplicationFont(fontID[i]);
243 void GuiFontLoader::update()
245 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
246 for (int i2 = 0; i2 < 2; ++i2)
247 for (int i3 = 0; i3 < 4; ++i3)
248 for (int i4 = 0; i4 < 10; ++i4) {
249 delete fontinfo_[i1][i2][i3][i4];
250 fontinfo_[i1][i2][i3][i4] = 0;
256 /////////////////////////////////////////////////
259 QLFontInfo::QLFontInfo(Font const & f)
261 font.setKerning(false);
262 string const pat = symbolFamily(f.family());
265 boost::tie(font, tmp) = getSymbolFont(pat);
267 switch (f.family()) {
268 case Font::ROMAN_FAMILY: {
269 QString family = toqstr(makeFontName(lyxrc.roman_font_name,
270 lyxrc.roman_font_foundry));
271 font.setFamily(family);
273 #if QT_VERSION >= 0x040300
274 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
275 // It is reported to Trolltech at 02/06/07 against 4.3 final.
276 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
277 if (family == "Times" && !font.exactMatch())
278 font.setFamily(QString::fromLatin1("Times New Roman"));
283 case Font::SANS_FAMILY:
284 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
285 lyxrc.sans_font_foundry)));
287 case Font::TYPEWRITER_FAMILY:
288 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
289 lyxrc.typewriter_font_foundry)));
296 switch (f.series()) {
297 case Font::MEDIUM_SERIES:
298 font.setWeight(QFont::Normal);
300 case Font::BOLD_SERIES:
301 font.setWeight(QFont::Bold);
307 switch (f.realShape()) {
308 case Font::ITALIC_SHAPE:
309 case Font::SLANTED_SHAPE:
310 font.setItalic(true);
316 LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
317 << "' matched by\n" << fromqstr(font.family()) << endl;
319 // Is this an exact match?
320 if (font.exactMatch())
321 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
323 LYXERR(Debug::FONT) << "This font is NOT an exact match"
326 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
328 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
329 * lyxrc.zoom / 100.0);
331 LYXERR(Debug::FONT) << "The font has size: "
332 << font.pointSizeF() << endl;
334 if (f.realShape() != Font::SMALLCAPS_SHAPE) {
335 metrics.reset(new GuiFontMetrics(font));
338 // handle small caps ourselves ...
340 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
342 font2.setKerning(false);
343 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
344 * lyxrc.zoom / 100.0);
346 metrics.reset(new GuiFontMetrics(font, font2));
352 bool GuiFontLoader::available(Font const & f)
354 static vector<int> cache_set(Font::NUM_FAMILIES, false);
355 static vector<int> cache(Font::NUM_FAMILIES, false);
357 Font::FONT_FAMILY family = f.family();
358 if (cache_set[family])
359 return cache[family];
360 cache_set[family] = true;
362 string const pat = symbolFamily(family);
364 // We don't care about non-symbol fonts
367 pair<QFont, bool> tmp = getSymbolFont(pat);
371 cache[family] = true;
375 } // namespace frontend