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>
31 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
32 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
33 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
38 using support::contains;
39 using support::package;
40 using support::addPath;
41 using support::addName;
43 extern docstring const stateText(FontInfo const & f);
50 FontFamily lyx_family;
55 SymbolFont symbol_fonts[] = {
58 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
62 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
66 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
70 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
74 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
78 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
82 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
86 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
90 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
94 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
97 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont);
100 QString getRawName(QString const & family)
102 for (size_t i = 0; i < nr_symbol_fonts; ++i)
103 if (family == symbol_fonts[i].family)
104 return symbol_fonts[i].xlfd;
106 LYXERR(Debug::FONT, "BUG: family not found !");
111 QString const symbolFamily(FontFamily family)
113 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
114 if (family == symbol_fonts[i].lyx_family)
115 return symbol_fonts[i].family;
121 bool isSymbolFamily(FontFamily family)
123 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
127 static bool isChosenFont(QFont & font, QString const & family)
129 // QFontInfo won't find a font that has only a few glyphs at unusual
130 // positions, e.g. the original esint10 font.
131 // The workaround is to add dummy glyphs at least at all ASCII
135 LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
137 if (fi.family().contains(family)) {
138 LYXERR(Debug::FONT, " got it ");
146 QFont symbolFont(QString const & family, bool * ok)
148 LYXERR(Debug::FONT, "Looking for font family "
149 << fromqstr(family) << " ... ");
150 QString upper = family;
151 upper[0] = family[0].toUpper();
154 font.setKerning(false);
155 font.setFamily(family);
157 if (isChosenFont(font, family)) {
158 LYXERR(Debug::FONT, "normal!");
163 LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
164 font.setFamily(upper);
166 if (isChosenFont(font, upper)) {
167 LYXERR(Debug::FONT, "upper!");
172 // A simple setFamily() fails on Qt 2
174 QString const rawName = getRawName(family);
175 LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... ");
176 font.setRawName(rawName);
178 if (isChosenFont(font, family)) {
179 LYXERR(Debug::FONT, "raw version!");
184 LYXERR(Debug::FONT, " FAILED :-(");
192 GuiFontLoader::GuiFontLoader()
194 QString const fonts_dir =
195 toqstr(addPath(package().system_support().absFilename(), "fonts"));
197 for (int i = 0 ; i < num_math_fonts; ++i) {
198 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
199 int fontID = QFontDatabase::addApplicationFont(font_file);
201 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
202 << static_cast<const char *>
203 (fontID < 0 ? " FAIL" : " OK"));
206 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
207 for (int i2 = 0; i2 < 2; ++i2)
208 for (int i3 = 0; i3 < 4; ++i3)
209 for (int i4 = 0; i4 < 10; ++i4)
210 fontinfo_[i1][i2][i3][i4] = 0;
214 void GuiFontLoader::update()
216 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
217 for (int i2 = 0; i2 < 2; ++i2)
218 for (int i3 = 0; i3 < 4; ++i3)
219 for (int i4 = 0; i4 < 10; ++i4) {
220 delete fontinfo_[i1][i2][i3][i4];
221 fontinfo_[i1][i2][i3][i4] = 0;
227 GuiFontLoader::~GuiFontLoader()
232 /////////////////////////////////////////////////
235 static QString makeFontName(QString const & family, QString const & foundry)
237 QString res = family;
238 if (!foundry.isEmpty())
239 res += " [" + foundry + ']';
244 GuiFontInfo::GuiFontInfo(FontInfo const & f)
247 font.setKerning(false);
248 QString const pat = symbolFamily(f.family());
249 if (!pat.isEmpty()) {
251 font = symbolFont(pat, &ok);
253 switch (f.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");
270 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
271 toqstr(lyxrc.sans_font_foundry)));
273 case TYPEWRITER_FAMILY:
274 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
275 toqstr(lyxrc.typewriter_font_foundry)));
282 switch (f.series()) {
284 font.setWeight(QFont::Normal);
287 font.setWeight(QFont::Bold);
293 switch (f.realShape()) {
296 font.setItalic(true);
302 LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
303 << "' matched by\n" << fromqstr(font.family()));
305 // Is this an exact match?
306 if (font.exactMatch())
307 LYXERR(Debug::FONT, "This font is an exact match");
309 LYXERR(Debug::FONT, "This font is NOT an exact match");
311 LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
313 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
314 * lyxrc.zoom / 100.0);
316 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
318 if (f.realShape() != SMALLCAPS_SHAPE) {
319 metrics = GuiFontMetrics(font);
321 // handle small caps ourselves ...
322 FontInfo smallfont = f;
323 smallfont.decSize().decSize().setShape(UP_SHAPE);
325 font2.setKerning(false);
326 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
327 * lyxrc.zoom / 100.0);
329 metrics = GuiFontMetrics(font, font2);
334 bool GuiFontLoader::available(FontInfo const & f)
336 static std::vector<int> cache_set(NUM_FAMILIES, false);
337 static std::vector<int> cache(NUM_FAMILIES, false);
339 FontFamily family = f.family();
340 if (cache_set[family])
341 return cache[family];
342 cache_set[family] = true;
344 QString const pat = symbolFamily(family);
346 // We don't care about non-symbol fonts
350 symbolFont(pat, &ok);
354 cache[family] = true;
358 } // namespace frontend