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"
17 #include "support/debug.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>
32 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
33 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
34 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
39 using support::contains;
40 using support::package;
41 using support::addPath;
42 using support::addName;
44 extern docstring const stateText(FontInfo const & f);
51 FontFamily lyx_family;
56 SymbolFont symbol_fonts[] = {
59 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
63 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
67 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
71 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
87 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
91 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
95 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
98 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont);
101 QString getRawName(QString const & family)
103 for (size_t i = 0; i < nr_symbol_fonts; ++i)
104 if (family == symbol_fonts[i].family)
105 return symbol_fonts[i].xlfd;
107 LYXERR(Debug::FONT, "BUG: family not found !");
112 QString const symbolFamily(FontFamily family)
114 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
115 if (family == symbol_fonts[i].lyx_family)
116 return symbol_fonts[i].family;
122 bool isSymbolFamily(FontFamily family)
124 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
128 static bool isChosenFont(QFont & font, QString const & family)
130 // QFontInfo won't find a font that has only a few glyphs at unusual
131 // positions, e.g. the original esint10 font.
132 // The workaround is to add dummy glyphs at least at all ASCII
136 LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
138 if (fi.family().contains(family)) {
139 LYXERR(Debug::FONT, " got it ");
147 QFont symbolFont(QString const & family, bool * ok)
149 LYXERR(Debug::FONT, "Looking for font family "
150 << fromqstr(family) << " ... ");
151 QString upper = family;
152 upper[0] = family[0].toUpper();
155 font.setKerning(false);
156 font.setFamily(family);
158 if (isChosenFont(font, family)) {
159 LYXERR(Debug::FONT, "normal!");
164 LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
165 font.setFamily(upper);
167 if (isChosenFont(font, upper)) {
168 LYXERR(Debug::FONT, "upper!");
173 // A simple setFamily() fails on Qt 2
175 QString const rawName = getRawName(family);
176 LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... ");
177 font.setRawName(rawName);
179 if (isChosenFont(font, family)) {
180 LYXERR(Debug::FONT, "raw version!");
185 LYXERR(Debug::FONT, " FAILED :-(");
193 GuiFontLoader::GuiFontLoader()
195 QString const fonts_dir =
196 toqstr(addPath(package().system_support().absFilename(), "fonts"));
198 for (int i = 0 ; i < num_math_fonts; ++i) {
199 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
200 int fontID = QFontDatabase::addApplicationFont(font_file);
202 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
203 << static_cast<const char *>
204 (fontID < 0 ? " FAIL" : " OK"));
207 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
208 for (int i2 = 0; i2 < 2; ++i2)
209 for (int i3 = 0; i3 < 4; ++i3)
210 for (int i4 = 0; i4 < 10; ++i4)
211 fontinfo_[i1][i2][i3][i4] = 0;
215 void GuiFontLoader::update()
217 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
218 for (int i2 = 0; i2 < 2; ++i2)
219 for (int i3 = 0; i3 < 4; ++i3)
220 for (int i4 = 0; i4 < 10; ++i4) {
221 delete fontinfo_[i1][i2][i3][i4];
222 fontinfo_[i1][i2][i3][i4] = 0;
228 GuiFontLoader::~GuiFontLoader()
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(FontInfo const & f)
248 font.setKerning(false);
249 QString const pat = symbolFamily(f.family());
250 if (!pat.isEmpty()) {
252 font = symbolFont(pat, &ok);
254 switch (f.family()) {
256 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
257 toqstr(lyxrc.roman_font_foundry));
258 font.setFamily(family);
260 #if QT_VERSION >= 0x040300
261 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
262 // It is reported to Trolltech at 02/06/07 against 4.3 final.
263 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
264 if (family == "Times" && !font.exactMatch())
265 font.setFamily("Times New Roman");
271 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
272 toqstr(lyxrc.sans_font_foundry)));
274 case TYPEWRITER_FAMILY:
275 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
276 toqstr(lyxrc.typewriter_font_foundry)));
283 switch (f.series()) {
285 font.setWeight(QFont::Normal);
288 font.setWeight(QFont::Bold);
294 switch (f.realShape()) {
297 font.setItalic(true);
303 LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
304 << "' matched by\n" << fromqstr(font.family()));
306 // Is this an exact match?
307 if (font.exactMatch())
308 LYXERR(Debug::FONT, "This font is an exact match");
310 LYXERR(Debug::FONT, "This font is NOT an exact match");
312 LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
314 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
315 * lyxrc.zoom / 100.0);
317 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
319 if (f.realShape() != SMALLCAPS_SHAPE) {
320 metrics = GuiFontMetrics(font);
322 // handle small caps ourselves ...
323 FontInfo smallfont = f;
324 smallfont.decSize().decSize().setShape(UP_SHAPE);
326 font2.setKerning(false);
327 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
328 * lyxrc.zoom / 100.0);
330 metrics = GuiFontMetrics(font, font2);
335 bool GuiFontLoader::available(FontInfo const & f)
337 static vector<int> cache_set(NUM_FAMILIES, false);
338 static vector<int> cache(NUM_FAMILIES, false);
340 FontFamily family = f.family();
341 if (cache_set[family])
342 return cache[family];
343 cache_set[family] = true;
345 QString const pat = symbolFamily(family);
347 // We don't care about non-symbol fonts
351 symbolFont(pat, &ok);
355 cache[family] = true;
359 } // namespace frontend