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 using namespace lyx::support;
33 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
34 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
35 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
40 extern docstring const stateText(FontInfo const & f);
47 FontFamily lyx_family;
52 SymbolFont symbol_fonts[] = {
55 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
59 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
63 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
67 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
71 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
75 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
87 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
91 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
94 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont);
97 QString getRawName(QString const & family)
99 for (size_t i = 0; i < nr_symbol_fonts; ++i)
100 if (family == symbol_fonts[i].family)
101 return symbol_fonts[i].xlfd;
103 LYXERR(Debug::FONT, "BUG: family not found !");
108 QString const symbolFamily(FontFamily family)
110 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
111 if (family == symbol_fonts[i].lyx_family)
112 return symbol_fonts[i].family;
118 bool isSymbolFamily(FontFamily family)
120 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
124 static bool isChosenFont(QFont & font, QString const & family)
126 // QFontInfo won't find a font that has only a few glyphs at unusual
127 // positions, e.g. the original esint10 font.
128 // The workaround is to add dummy glyphs at least at all ASCII
132 LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
134 if (fi.family().contains(family)) {
135 LYXERR(Debug::FONT, " got it ");
143 QFont symbolFont(QString const & family, bool * ok)
145 LYXERR(Debug::FONT, "Looking for font family "
146 << fromqstr(family) << " ... ");
147 QString upper = family;
148 upper[0] = family[0].toUpper();
151 font.setKerning(false);
152 font.setFamily(family);
154 if (isChosenFont(font, family)) {
155 LYXERR(Debug::FONT, "normal!");
160 LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
161 font.setFamily(upper);
163 if (isChosenFont(font, upper)) {
164 LYXERR(Debug::FONT, "upper!");
169 // A simple setFamily() fails on Qt 2
171 QString const rawName = getRawName(family);
172 LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... ");
173 font.setRawName(rawName);
175 if (isChosenFont(font, family)) {
176 LYXERR(Debug::FONT, "raw version!");
181 LYXERR(Debug::FONT, " FAILED :-(");
189 GuiFontLoader::GuiFontLoader()
191 QString const fonts_dir =
192 toqstr(addPath(package().system_support().absFilename(), "fonts"));
194 for (int i = 0 ; i < num_math_fonts; ++i) {
195 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
196 int fontID = QFontDatabase::addApplicationFont(font_file);
198 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
199 << static_cast<const char *>
200 (fontID < 0 ? " FAIL" : " OK"));
203 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
204 for (int i2 = 0; i2 < 2; ++i2)
205 for (int i3 = 0; i3 < 4; ++i3)
206 for (int i4 = 0; i4 < 10; ++i4)
207 fontinfo_[i1][i2][i3][i4] = 0;
211 void GuiFontLoader::update()
213 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
214 for (int i2 = 0; i2 < 2; ++i2)
215 for (int i3 = 0; i3 < 4; ++i3)
216 for (int i4 = 0; i4 < 10; ++i4) {
217 delete fontinfo_[i1][i2][i3][i4];
218 fontinfo_[i1][i2][i3][i4] = 0;
224 GuiFontLoader::~GuiFontLoader()
229 /////////////////////////////////////////////////
232 static QString makeFontName(QString const & family, QString const & foundry)
234 QString res = family;
235 if (!foundry.isEmpty())
236 res += " [" + foundry + ']';
241 GuiFontInfo::GuiFontInfo(FontInfo const & f)
244 font.setKerning(false);
245 QString const pat = symbolFamily(f.family());
246 if (!pat.isEmpty()) {
248 font = symbolFont(pat, &ok);
250 switch (f.family()) {
252 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
253 toqstr(lyxrc.roman_font_foundry));
254 font.setFamily(family);
256 #if QT_VERSION >= 0x040300
257 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
258 // It is reported to Trolltech at 02/06/07 against 4.3 final.
259 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
260 if (family == "Times" && !font.exactMatch())
261 font.setFamily("Times New Roman");
267 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
268 toqstr(lyxrc.sans_font_foundry)));
270 case TYPEWRITER_FAMILY:
271 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
272 toqstr(lyxrc.typewriter_font_foundry)));
279 switch (f.series()) {
281 font.setWeight(QFont::Normal);
284 font.setWeight(QFont::Bold);
290 switch (f.realShape()) {
293 font.setItalic(true);
299 LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
300 << "' matched by\n" << fromqstr(font.family()));
302 // Is this an exact match?
303 if (font.exactMatch())
304 LYXERR(Debug::FONT, "This font is an exact match");
306 LYXERR(Debug::FONT, "This font is NOT an exact match");
308 LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
310 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
311 * lyxrc.zoom / 100.0);
313 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
315 if (f.realShape() != SMALLCAPS_SHAPE) {
316 metrics = GuiFontMetrics(font);
318 // handle small caps ourselves ...
319 FontInfo smallfont = f;
320 smallfont.decSize().decSize().setShape(UP_SHAPE);
322 font2.setKerning(false);
323 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
324 * lyxrc.zoom / 100.0);
326 metrics = GuiFontMetrics(font, font2);
331 bool GuiFontLoader::available(FontInfo const & f)
333 static vector<int> cache_set(NUM_FAMILIES, false);
334 static vector<int> cache(NUM_FAMILIES, false);
336 FontFamily family = f.family();
337 if (cache_set[family])
338 return cache[family];
339 cache_set[family] = true;
341 QString const pat = symbolFamily(family);
343 // We don't care about non-symbol fonts
347 symbolFont(pat, &ok);
351 cache[family] = true;
355 } // namespace frontend