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);
50 * Matches Fonts against
51 * actual QFont instances, and also caches metrics.
56 GuiFontInfo(FontInfo const & f);
60 /// Metrics on the font
61 GuiFontMetrics metrics;
67 FontFamily lyx_family;
72 SymbolFont symbol_fonts[] = {
73 { SYMBOL_FAMILY,"symbol", "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific"},
74 { CMR_FAMILY, "cmr10", "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 { CMSY_FAMILY, "cmsy10", "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
76 { CMM_FAMILY, "cmmi10", "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
77 { CMEX_FAMILY, "cmex10", "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
78 { MSA_FAMILY, "msam10", "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 { MSB_FAMILY, "msbm10", "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
80 { EUFRAK_FAMILY,"eufm10", "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
81 { RSFS_FAMILY, "rsfs10", "-*-rsfs10-medium-*-*-*-*-*-*-*-*-*-*-*" },
82 { STMARY_FAMILY,"stmary10","-*-stmary10-medium-*-*-*-*-*-*-*-*-*-*-*" },
83 { WASY_FAMILY, "wasy10", "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
84 { ESINT_FAMILY, "esint10","-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
87 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
91 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
94 // returns a reference to the pointer type (GuiFontInfo *) in the
96 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
98 // The display font and the text font are the same
99 size_t const style = (f.style() == LM_ST_DISPLAY) ? LM_ST_TEXT : f.style();
100 return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
104 // Get font info (font + metrics) for the given LyX font.
105 // if not cached, create it.
106 GuiFontInfo & fontinfo(FontInfo const & f)
108 bool const fontIsRealized =
109 (f.family() < NUM_FAMILIES) &&
110 (f.series() < NUM_SERIES) &&
111 (f.realShape() < NUM_SHAPE) &&
112 (f.size() < NUM_SIZE);
113 if (!fontIsRealized) {
114 // We can reset the font to something sensible in release mode.
116 LYXERR0("Unrealized font!");
118 f2.realize(sane_font);
119 GuiFontInfo * & fi = fontinfo_ptr(f2);
121 fi = new GuiFontInfo(f2);
124 GuiFontInfo * & fi = fontinfo_ptr(f);
126 fi = new GuiFontInfo(f);
131 QString rawName(QString const & family)
133 for (size_t i = 0; i < nr_symbol_fonts; ++i)
134 if (family == symbol_fonts[i].family)
135 return symbol_fonts[i].xlfd;
137 LYXERR(Debug::FONT, "BUG: family not found !");
142 QString symbolFamily(FontFamily family)
144 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
145 if (family == symbol_fonts[i].lyx_family)
146 return symbol_fonts[i].family;
153 bool isSymbolFamily(FontFamily family)
155 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
160 static bool isChosenFont(QFont & font, QString const & family,
161 QString const & style)
163 // QFontInfo won't find a font that has only a few glyphs at unusual
164 // positions, e.g. the original esint10 font.
165 // The workaround is to add dummy glyphs at least at all ASCII
169 LYXERR(Debug::FONT, "got: " << fi.family());
171 if (fi.family().contains(family)
172 #if QT_VERSION >= 0x040800
173 && (style.isEmpty() || fi.styleName().contains(style))
176 LYXERR(Debug::FONT, " got it ");
184 QFont symbolFont(QString const & family, bool * ok)
186 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
187 QString upper = family;
188 upper[0] = family[0].toUpper();
191 font.setFamily(family);
192 #if QT_VERSION >= 0x040800
193 font.setStyleName("LyX");
195 if (isChosenFont(font, family, "LyX")) {
196 LYXERR(Debug::FONT, "lyx!");
201 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
202 font.setStyleName(QString());
205 if (isChosenFont(font, family, QString())) {
206 LYXERR(Debug::FONT, "normal!");
211 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
212 font.setFamily(upper);
214 if (isChosenFont(font, upper, QString())) {
215 LYXERR(Debug::FONT, "upper!");
220 // A simple setFamily() fails on Qt 2
222 QString const raw = rawName(family);
223 LYXERR(Debug::FONT, "Trying " << raw << " ... ");
224 font.setRawName(raw);
226 if (isChosenFont(font, family, QString())) {
227 LYXERR(Debug::FONT, "raw version!");
232 LYXERR(Debug::FONT, " FAILED :-(");
240 FontLoader::FontLoader()
242 QString const fonts_dir =
243 toqstr(addPath(package().system_support().absFileName(), "fonts"));
245 for (int i = 0 ; i < num_math_fonts; ++i) {
246 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
247 int fontID = QFontDatabase::addApplicationFont(font_file);
249 LYXERR(Debug::FONT, "Adding font " << font_file
250 << (fontID < 0 ? " FAIL" : " OK"));
253 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
254 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
255 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
256 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
257 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
258 fontinfo_[i1][i2][i3][i4][i5] = 0;
262 void FontLoader::update()
264 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
265 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
266 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
267 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
268 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
269 delete fontinfo_[i1][i2][i3][i4][i5];
270 fontinfo_[i1][i2][i3][i4][i5] = 0;
275 FontLoader::~FontLoader()
280 /////////////////////////////////////////////////
284 QString makeFontName(QString const & family, QString const & foundry)
286 QString res = family;
287 if (!foundry.isEmpty())
288 res += " [" + foundry + ']';
293 QFont makeQFont(FontInfo const & f)
296 QString const pat = symbolFamily(f.family());
297 if (!pat.isEmpty()) {
299 font = symbolFont(pat, &ok);
301 switch (f.family()) {
303 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
304 toqstr(lyxrc.roman_font_foundry));
305 font.setFamily(family);
307 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
308 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
309 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
310 // FIXME: Check whether this is really fixed in Qt 4.8
311 if (family == "Times" && !font.exactMatch())
312 font.setFamily("Times New Roman");
318 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
319 toqstr(lyxrc.sans_font_foundry)));
321 case TYPEWRITER_FAMILY:
322 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
323 toqstr(lyxrc.typewriter_font_foundry)));
330 switch (f.series()) {
332 font.setWeight(QFont::Normal);
335 font.setWeight(QFont::Bold);
341 switch (f.realShape()) {
343 font.setStyle(QFont::StyleItalic);
346 font.setStyle(QFont::StyleOblique);
348 case SMALLCAPS_SHAPE:
349 font.setCapitalization(QFont::SmallCaps);
355 LYXERR(Debug::FONT, "Font '" << stateText(f)
356 << "' matched by\n" << font.family());
358 // Is this an exact match?
359 if (font.exactMatch())
360 LYXERR(Debug::FONT, "This font is an exact match");
362 LYXERR(Debug::FONT, "This font is NOT an exact match");
364 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
366 font.setPointSizeF(f.realSize() * lyxrc.zoom / 100.0);
368 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
376 GuiFontInfo::GuiFontInfo(FontInfo const & f)
377 : font(makeQFont(f)), metrics(font)
381 bool FontLoader::available(FontInfo const & f)
384 static vector<int> cache_set(NUM_FAMILIES, false);
385 static vector<int> cache(NUM_FAMILIES, false);
387 FontFamily family = f.family();
389 // Apple ships a font name "Symbol", which has more or less the same
390 // glyphs as the original PostScript Symbol font, but it uses a different
391 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
392 // Since we expect the font specific encoding of the original
393 // PostScript Symbol font, we can't use the one provided on OS X.
394 // See also the discussion in bug 7954.
395 if (f.family() == SYMBOL_FAMILY)
398 if (cache_set[family])
399 return cache[family];
400 cache_set[family] = true;
402 QString const pat = symbolFamily(family);
404 // We don't care about non-symbol fonts
408 symbolFont(pat, &ok);
412 cache[family] = true;
417 bool FontLoader::canBeDisplayed(char_type c)
421 // FIXME check whether this is still needed for Qt5
423 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
424 // bug 7954, see also comment in GuiPainter::text()
432 FontMetrics const & FontLoader::metrics(FontInfo const & f)
434 return fontinfo(f).metrics;
438 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
440 return fontinfo(f).metrics;
444 QFont const & getFont(FontInfo const & f)
446 return fontinfo(f).font;
449 } // namespace frontend