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 "FontLoader.h"
17 #include "GuiFontMetrics.h"
18 #include "qt_helpers.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/Systemcall.h"
27 #include "support/Package.h"
28 #include "support/os.h"
31 #include <QFontDatabase>
33 #include <boost/assert.hpp>
36 using namespace lyx::support;
38 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
39 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
40 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
44 extern docstring const stateText(FontInfo const & f);
49 * Matches Fonts against
50 * actual QFont instances, and also caches metrics.
55 GuiFontInfo(FontInfo const & f);
59 /// Metrics on the font
60 GuiFontMetrics metrics;
66 FontFamily lyx_family;
71 SymbolFont symbol_fonts[] = {
74 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
78 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
82 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
86 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
90 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
94 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
98 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
102 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
106 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
110 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
113 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
116 static GuiFontInfo * fontinfo_[NUM_FAMILIES][2][4][10];
119 /// Get font info (font + metrics) for the given LyX font.
120 // if not cached, create it.
121 GuiFontInfo & fontinfo(FontInfo const & f)
123 BOOST_ASSERT(f.family() < NUM_FAMILIES);
124 BOOST_ASSERT(f.series() < 2);
125 BOOST_ASSERT(f.realShape() < 4);
126 BOOST_ASSERT(f.size() < 10);
127 // fi is a reference to the pointer type (GuiFontInfo *) in the
130 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
132 fi = new GuiFontInfo(f);
137 QString rawName(QString const & family)
139 for (size_t i = 0; i < nr_symbol_fonts; ++i)
140 if (family == symbol_fonts[i].family)
141 return symbol_fonts[i].xlfd;
143 LYXERR(Debug::FONT, "BUG: family not found !");
148 QString symbolFamily(FontFamily family)
150 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
151 if (family == symbol_fonts[i].lyx_family)
152 return symbol_fonts[i].family;
158 bool isSymbolFamily(FontFamily family)
160 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
164 static bool isChosenFont(QFont & font, QString const & family)
166 // QFontInfo won't find a font that has only a few glyphs at unusual
167 // positions, e.g. the original esint10 font.
168 // The workaround is to add dummy glyphs at least at all ASCII
172 LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
174 if (fi.family().contains(family)) {
175 LYXERR(Debug::FONT, " got it ");
183 QFont symbolFont(QString const & family, bool * ok)
185 LYXERR(Debug::FONT, "Looking for font family "
186 << fromqstr(family) << " ... ");
187 QString upper = family;
188 upper[0] = family[0].toUpper();
191 font.setKerning(false);
192 font.setFamily(family);
194 if (isChosenFont(font, family)) {
195 LYXERR(Debug::FONT, "normal!");
200 LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
201 font.setFamily(upper);
203 if (isChosenFont(font, upper)) {
204 LYXERR(Debug::FONT, "upper!");
209 // A simple setFamily() fails on Qt 2
211 QString const raw = rawName(family);
212 LYXERR(Debug::FONT, "Trying " << fromqstr(raw) << " ... ");
213 font.setRawName(raw);
215 if (isChosenFont(font, family)) {
216 LYXERR(Debug::FONT, "raw version!");
221 LYXERR(Debug::FONT, " FAILED :-(");
229 FontLoader::FontLoader()
231 QString const fonts_dir =
232 toqstr(addPath(package().system_support().absFilename(), "fonts"));
234 for (int i = 0 ; i < num_math_fonts; ++i) {
235 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
236 int fontID = QFontDatabase::addApplicationFont(font_file);
238 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
239 << static_cast<const char *>
240 (fontID < 0 ? " FAIL" : " OK"));
243 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
244 for (int i2 = 0; i2 < 2; ++i2)
245 for (int i3 = 0; i3 < 4; ++i3)
246 for (int i4 = 0; i4 < 10; ++i4)
247 fontinfo_[i1][i2][i3][i4] = 0;
251 void FontLoader::update()
253 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
254 for (int i2 = 0; i2 < 2; ++i2)
255 for (int i3 = 0; i3 < 4; ++i3)
256 for (int i4 = 0; i4 < 10; ++i4) {
257 delete fontinfo_[i1][i2][i3][i4];
258 fontinfo_[i1][i2][i3][i4] = 0;
264 FontLoader::~FontLoader()
269 /////////////////////////////////////////////////
272 static QString makeFontName(QString const & family, QString const & foundry)
274 QString res = family;
275 if (!foundry.isEmpty())
276 res += " [" + foundry + ']';
281 GuiFontInfo::GuiFontInfo(FontInfo const & f)
284 font.setKerning(false);
285 QString const pat = symbolFamily(f.family());
286 if (!pat.isEmpty()) {
288 font = symbolFont(pat, &ok);
290 switch (f.family()) {
292 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
293 toqstr(lyxrc.roman_font_foundry));
294 font.setFamily(family);
296 #if QT_VERSION >= 0x040300
297 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
298 // It is reported to Trolltech at 02/06/07 against 4.3 final.
299 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
300 if (family == "Times" && !font.exactMatch())
301 font.setFamily("Times New Roman");
307 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
308 toqstr(lyxrc.sans_font_foundry)));
310 case TYPEWRITER_FAMILY:
311 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
312 toqstr(lyxrc.typewriter_font_foundry)));
319 switch (f.series()) {
321 font.setWeight(QFont::Normal);
324 font.setWeight(QFont::Bold);
330 switch (f.realShape()) {
333 font.setItalic(true);
339 LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
340 << "' matched by\n" << fromqstr(font.family()));
342 // Is this an exact match?
343 if (font.exactMatch())
344 LYXERR(Debug::FONT, "This font is an exact match");
346 LYXERR(Debug::FONT, "This font is NOT an exact match");
348 LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
350 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
351 * lyxrc.zoom / 100.0);
353 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
355 if (f.realShape() != SMALLCAPS_SHAPE) {
356 metrics = GuiFontMetrics(font);
358 // handle small caps ourselves ...
359 FontInfo smallfont = f;
360 smallfont.decSize().decSize().setShape(UP_SHAPE);
362 font2.setKerning(false);
363 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
364 * lyxrc.zoom / 100.0);
366 metrics = GuiFontMetrics(font, font2);
371 bool FontLoader::available(FontInfo const & f)
373 static vector<int> cache_set(NUM_FAMILIES, false);
374 static vector<int> cache(NUM_FAMILIES, false);
376 FontFamily family = f.family();
377 if (cache_set[family])
378 return cache[family];
379 cache_set[family] = true;
381 QString const pat = symbolFamily(family);
383 // We don't care about non-symbol fonts
387 symbolFont(pat, &ok);
391 cache[family] = true;
396 FontMetrics const & FontLoader::metrics(FontInfo const & f)
398 return fontinfo(f).metrics;
402 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
404 return fontinfo(f).metrics;
408 QFont const & getFont(FontInfo const & f)
410 return fontinfo(f).font;
413 } // namespace frontend