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);
48 * Matches Fonts against
49 * actual QFont instances, and also caches metrics.
54 GuiFontInfo(FontInfo const & f);
58 /// Metrics on the font
59 GuiFontMetrics metrics;
65 FontFamily lyx_family;
70 SymbolFont symbol_fonts[] = {
71 { SYMBOL_FAMILY,"symbol", "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific"},
72 { CMR_FAMILY, "cmr10", "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
73 { CMSY_FAMILY, "cmsy10", "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
74 { CMM_FAMILY, "cmmi10", "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 { CMEX_FAMILY, "cmex10", "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
76 { MSA_FAMILY, "msam10", "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
77 { MSB_FAMILY, "msbm10", "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
78 { EUFRAK_FAMILY,"eufm10", "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
79 { RSFS_FAMILY, "rsfs10", "-*-rsfs10-medium-*-*-*-*-*-*-*-*-*-*-*" },
80 { STMARY_FAMILY,"stmary10","-*-stmary10-medium-*-*-*-*-*-*-*-*-*-*-*" },
81 { WASY_FAMILY, "wasy10", "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
82 { ESINT_FAMILY, "esint10","-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
85 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
89 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
92 // returns a reference to the pointer type (GuiFontInfo *) in the
94 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
96 // The display font and the text font are the same
97 size_t const style = (f.style() == DISPLAY_STYLE) ? TEXT_STYLE : f.style();
98 return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
102 // Get font info (font + metrics) for the given LyX font.
103 // if not cached, create it.
104 GuiFontInfo & fontinfo(FontInfo const & f)
106 bool const fontIsRealized =
107 (f.family() < NUM_FAMILIES) &&
108 (f.series() < NUM_SERIES) &&
109 (f.realShape() < NUM_SHAPE) &&
110 (f.size() < NUM_SIZE);
111 if (!fontIsRealized) {
112 // We can reset the font to something sensible in release mode.
114 LYXERR0("Unrealized font!");
116 f2.realize(sane_font);
117 GuiFontInfo * & fi = fontinfo_ptr(f2);
119 fi = new GuiFontInfo(f2);
122 GuiFontInfo * & fi = fontinfo_ptr(f);
124 fi = new GuiFontInfo(f);
129 QString symbolFamily(FontFamily family)
131 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
132 if (family == symbol_fonts[i].lyx_family)
133 return symbol_fonts[i].family;
140 bool isSymbolFamily(FontFamily family)
142 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
147 static bool isChosenFont(QFont & font, QString const & family,
148 QString const & style)
150 // QFontInfo won't find a font that has only a few glyphs at unusual
151 // positions, e.g. the original esint10 font.
152 // The workaround is to add dummy glyphs at least at all ASCII
156 LYXERR(Debug::FONT, "got: " << fi.family());
158 if (fi.family().contains(family)
159 #if QT_VERSION >= 0x040800
160 && (style.isEmpty() || fi.styleName().contains(style))
163 LYXERR(Debug::FONT, " got it ");
171 QFont symbolFont(QString const & family, bool * ok)
173 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
174 QString upper = family;
175 upper[0] = family[0].toUpper();
178 font.setFamily(family);
179 #if QT_VERSION >= 0x040800
180 font.setStyleName("LyX");
182 if (isChosenFont(font, family, "LyX")) {
183 LYXERR(Debug::FONT, "lyx!");
188 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
189 font.setStyleName(QString());
192 if (isChosenFont(font, family, QString())) {
193 LYXERR(Debug::FONT, "normal!");
198 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
199 font.setFamily(upper);
201 if (isChosenFont(font, upper, QString())) {
202 LYXERR(Debug::FONT, "upper!");
207 LYXERR(Debug::FONT, "Trying " << family << " ... ");
208 font.setFamily(family);
210 if (isChosenFont(font, family, QString())) {
211 LYXERR(Debug::FONT, "raw version!");
216 LYXERR(Debug::FONT, " FAILED :-(");
224 FontLoader::FontLoader()
226 QString const fonts_dir =
227 toqstr(addPath(package().system_support().absFileName(), "fonts"));
229 for (int i = 0 ; i < num_math_fonts; ++i) {
230 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
231 int fontID = QFontDatabase::addApplicationFont(font_file);
233 LYXERR(Debug::FONT, "Adding font " << font_file
234 << (fontID < 0 ? " FAIL" : " OK"));
237 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
238 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
239 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
240 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
241 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
242 fontinfo_[i1][i2][i3][i4][i5] = 0;
246 void FontLoader::update()
248 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
249 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
250 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
251 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
252 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
253 delete fontinfo_[i1][i2][i3][i4][i5];
254 fontinfo_[i1][i2][i3][i4][i5] = 0;
259 FontLoader::~FontLoader()
264 /////////////////////////////////////////////////
268 QString makeFontName(QString const & family, QString const & foundry)
270 QString res = family;
271 if (!foundry.isEmpty())
272 res += " [" + foundry + ']';
277 QFont makeQFont(FontInfo const & f)
280 QString const pat = symbolFamily(f.family());
281 if (!pat.isEmpty()) {
283 font = symbolFont(pat, &ok);
285 switch (f.family()) {
287 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
288 toqstr(lyxrc.roman_font_foundry));
289 font.setFamily(family);
291 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
292 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
293 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
294 // FIXME: Check whether this is really fixed in Qt 4.8
295 if (family == "Times" && !font.exactMatch())
296 font.setFamily("Times New Roman");
302 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
303 toqstr(lyxrc.sans_font_foundry)));
305 case TYPEWRITER_FAMILY:
306 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
307 toqstr(lyxrc.typewriter_font_foundry)));
314 switch (f.series()) {
316 font.setWeight(QFont::Normal);
319 font.setWeight(QFont::Bold);
325 switch (f.realShape()) {
327 font.setStyle(QFont::StyleItalic);
330 font.setStyle(QFont::StyleOblique);
332 case SMALLCAPS_SHAPE:
333 font.setCapitalization(QFont::SmallCaps);
339 LYXERR(Debug::FONT, "Font '" << f.stateText()
340 << "' matched by\n" << 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 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
350 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
358 GuiFontInfo::GuiFontInfo(FontInfo const & f)
359 : font(makeQFont(f)), metrics(font)
363 bool FontLoader::available(FontInfo const & f)
366 static vector<int> cache_set(NUM_FAMILIES, false);
367 static vector<int> cache(NUM_FAMILIES, false);
369 FontFamily family = f.family();
371 // Apple ships a font name "Symbol", which has more or less the same
372 // glyphs as the original PostScript Symbol font, but it uses a different
373 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
374 // Since we expect the font specific encoding of the original
375 // PostScript Symbol font, we can't use the one provided on OS X.
376 // See also the discussion in bug 7954.
377 if (f.family() == SYMBOL_FAMILY)
380 if (cache_set[family])
381 return cache[family];
382 cache_set[family] = true;
384 QString const pat = symbolFamily(family);
386 // We don't care about non-symbol fonts
390 symbolFont(pat, &ok);
394 cache[family] = true;
399 bool FontLoader::canBeDisplayed(char_type c)
403 // FIXME check whether this is still needed for Qt5
405 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
406 // bug 7954, see also comment in GuiPainter::text()
414 FontMetrics const & FontLoader::metrics(FontInfo const & f)
416 return fontinfo(f).metrics;
420 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
422 return fontinfo(f).metrics;
426 QFont const & getFont(FontInfo const & f)
428 return fontinfo(f).font;
431 } // namespace frontend