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/debug.h"
23 #include "support/filetools.h"
24 #include "support/gettext.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 "support/lassert.h"
36 using namespace lyx::support;
38 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
39 "esint10", "eufm10", "msam10", "msbm10", "rsfs10", "stmary10",
41 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
45 extern docstring const stateText(FontInfo const & f,
46 bool const terse = false);
51 * Matches Fonts against
52 * actual QFont instances, and also caches metrics.
57 GuiFontInfo(FontInfo const & f);
61 /// Metrics on the font
62 GuiFontMetrics metrics;
68 FontFamily lyx_family;
73 SymbolFont symbol_fonts[] = {
74 { SYMBOL_FAMILY,"symbol", "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific"},
75 { CMR_FAMILY, "cmr10", "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
76 { CMSY_FAMILY, "cmsy10", "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
77 { CMM_FAMILY, "cmmi10", "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
78 { CMEX_FAMILY, "cmex10", "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 { MSA_FAMILY, "msam10", "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
80 { MSB_FAMILY, "msbm10", "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
81 { EUFRAK_FAMILY,"eufm10", "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
82 { RSFS_FAMILY, "rsfs10", "-*-rsfs10-medium-*-*-*-*-*-*-*-*-*-*-*" },
83 { STMARY_FAMILY,"stmary10","-*-stmary10-medium-*-*-*-*-*-*-*-*-*-*-*" },
84 { WASY_FAMILY, "wasy10", "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
85 { ESINT_FAMILY, "esint10","-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
88 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
92 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
95 // returns a reference to the pointer type (GuiFontInfo *) in the
97 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
99 // The display font and the text font are the same
100 size_t const style = (f.style() == LM_ST_DISPLAY) ? LM_ST_TEXT : f.style();
101 return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
105 // Get font info (font + metrics) for the given LyX font.
106 // if not cached, create it.
107 GuiFontInfo & fontinfo(FontInfo const & f)
109 bool const fontIsRealized =
110 (f.family() < NUM_FAMILIES) &&
111 (f.series() < NUM_SERIES) &&
112 (f.realShape() < NUM_SHAPE) &&
113 (f.size() < NUM_SIZE);
114 if (!fontIsRealized) {
115 // We can reset the font to something sensible in release mode.
117 LYXERR0("Unrealized font!");
119 f2.realize(sane_font);
120 GuiFontInfo * & fi = fontinfo_ptr(f2);
122 fi = new GuiFontInfo(f2);
125 GuiFontInfo * & fi = fontinfo_ptr(f);
127 fi = new GuiFontInfo(f);
132 QString rawName(QString const & family)
134 for (size_t i = 0; i < nr_symbol_fonts; ++i)
135 if (family == symbol_fonts[i].family)
136 return symbol_fonts[i].xlfd;
138 LYXERR(Debug::FONT, "BUG: family not found !");
143 QString symbolFamily(FontFamily family)
145 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
146 if (family == symbol_fonts[i].lyx_family)
147 return symbol_fonts[i].family;
154 bool isSymbolFamily(FontFamily family)
156 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
161 static bool isChosenFont(QFont & font, QString const & family,
162 QString const & style)
164 // QFontInfo won't find a font that has only a few glyphs at unusual
165 // positions, e.g. the original esint10 font.
166 // The workaround is to add dummy glyphs at least at all ASCII
170 LYXERR(Debug::FONT, "got: " << fi.family());
172 if (fi.family().contains(family)
173 #if QT_VERSION >= 0x040800
174 && (style.isEmpty() || fi.styleName().contains(style))
177 LYXERR(Debug::FONT, " got it ");
185 QFont symbolFont(QString const & family, bool * ok)
187 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
188 QString upper = family;
189 upper[0] = family[0].toUpper();
192 font.setFamily(family);
193 #if QT_VERSION >= 0x040800
194 font.setStyleName("LyX");
196 if (isChosenFont(font, family, "LyX")) {
197 LYXERR(Debug::FONT, "lyx!");
202 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
203 font.setStyleName(QString());
206 if (isChosenFont(font, family, QString())) {
207 LYXERR(Debug::FONT, "normal!");
212 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
213 font.setFamily(upper);
215 if (isChosenFont(font, upper, QString())) {
216 LYXERR(Debug::FONT, "upper!");
221 // A simple setFamily() fails on Qt 2
223 QString const raw = rawName(family);
224 LYXERR(Debug::FONT, "Trying " << raw << " ... ");
225 font.setRawName(raw);
227 if (isChosenFont(font, family, QString())) {
228 LYXERR(Debug::FONT, "raw version!");
233 LYXERR(Debug::FONT, " FAILED :-(");
241 FontLoader::FontLoader()
243 QString const fonts_dir =
244 toqstr(addPath(package().system_support().absFileName(), "fonts"));
246 for (int i = 0 ; i < num_math_fonts; ++i) {
247 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
248 int fontID = QFontDatabase::addApplicationFont(font_file);
250 LYXERR(Debug::FONT, "Adding font " << font_file
251 << (fontID < 0 ? " FAIL" : " OK"));
254 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
255 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
256 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
257 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
258 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
259 fontinfo_[i1][i2][i3][i4][i5] = 0;
263 void FontLoader::update()
265 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
266 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
267 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
268 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
269 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
270 delete fontinfo_[i1][i2][i3][i4][i5];
271 fontinfo_[i1][i2][i3][i4][i5] = 0;
276 FontLoader::~FontLoader()
281 /////////////////////////////////////////////////
285 QString makeFontName(QString const & family, QString const & foundry)
287 QString res = family;
288 if (!foundry.isEmpty())
289 res += " [" + foundry + ']';
294 QFont makeQFont(FontInfo const & f)
297 QString const pat = symbolFamily(f.family());
298 if (!pat.isEmpty()) {
300 font = symbolFont(pat, &ok);
302 switch (f.family()) {
304 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
305 toqstr(lyxrc.roman_font_foundry));
306 font.setFamily(family);
308 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
309 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
310 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
311 // FIXME: Check whether this is really fixed in Qt 4.8
312 if (family == "Times" && !font.exactMatch())
313 font.setFamily("Times New Roman");
319 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
320 toqstr(lyxrc.sans_font_foundry)));
322 case TYPEWRITER_FAMILY:
323 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
324 toqstr(lyxrc.typewriter_font_foundry)));
331 switch (f.series()) {
333 font.setWeight(QFont::Normal);
336 font.setWeight(QFont::Bold);
342 switch (f.realShape()) {
344 font.setStyle(QFont::StyleItalic);
347 font.setStyle(QFont::StyleOblique);
349 case SMALLCAPS_SHAPE:
350 font.setCapitalization(QFont::SmallCaps);
356 LYXERR(Debug::FONT, "Font '" << stateText(f)
357 << "' matched by\n" << font.family());
359 // Is this an exact match?
360 if (font.exactMatch())
361 LYXERR(Debug::FONT, "This font is an exact match");
363 LYXERR(Debug::FONT, "This font is NOT an exact match");
365 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
367 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
369 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
377 GuiFontInfo::GuiFontInfo(FontInfo const & f)
378 : font(makeQFont(f)), metrics(font)
382 bool FontLoader::available(FontInfo const & f)
385 static vector<int> cache_set(NUM_FAMILIES, false);
386 static vector<int> cache(NUM_FAMILIES, false);
388 FontFamily family = f.family();
390 // Apple ships a font name "Symbol", which has more or less the same
391 // glyphs as the original PostScript Symbol font, but it uses a different
392 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
393 // Since we expect the font specific encoding of the original
394 // PostScript Symbol font, we can't use the one provided on OS X.
395 // See also the discussion in bug 7954.
396 if (f.family() == SYMBOL_FAMILY)
399 if (cache_set[family])
400 return cache[family];
401 cache_set[family] = true;
403 QString const pat = symbolFamily(family);
405 // We don't care about non-symbol fonts
409 symbolFont(pat, &ok);
413 cache[family] = true;
418 bool FontLoader::canBeDisplayed(char_type c)
422 // FIXME check whether this is still needed for Qt5
424 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
425 // bug 7954, see also comment in GuiPainter::text()
433 FontMetrics const & FontLoader::metrics(FontInfo const & f)
435 return fontinfo(f).metrics;
439 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
441 return fontinfo(f).metrics;
445 QFont const & getFont(FontInfo const & f)
447 return fontinfo(f).font;
450 } // namespace frontend