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"
16 #include "FontLoader.h"
18 #include "GuiFontMetrics.h"
19 #include "qt_helpers.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/Systemcall.h"
29 #include "support/Package.h"
30 #include "support/os.h"
32 #include "GuiApplication.h"
35 #include <QFontDatabase>
38 using namespace lyx::support;
40 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
41 "dsrom10", "esint10", "eufm10", "msam10", "msbm10", "rsfs10",
42 "stmary10", "wasy10"};
43 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
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;
69 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
74 SymbolFont symbol_fonts[] = {
75 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
76 { SYMBOL_FAMILY,"symbol", "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific"},
77 { CMR_FAMILY, "cmr10", "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
78 { CMSY_FAMILY, "cmsy10", "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 { CMM_FAMILY, "cmmi10", "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
80 { CMEX_FAMILY, "cmex10", "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
81 { MSA_FAMILY, "msam10", "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
82 { MSB_FAMILY, "msbm10", "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 { DS_FAMILY, "dsrom10", "-*-dsrom10-*-*-*-*-*-*-*-*-*-*-*-*" },
84 { EUFRAK_FAMILY,"eufm10", "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
85 { RSFS_FAMILY, "rsfs10", "-*-rsfs10-medium-*-*-*-*-*-*-*-*-*-*-*" },
86 { STMARY_FAMILY,"stmary10","-*-stmary10-medium-*-*-*-*-*-*-*-*-*-*-*" },
87 { WASY_FAMILY, "wasy10", "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
88 { ESINT_FAMILY, "esint10","-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
90 { SYMBOL_FAMILY,"symbol"},
91 { CMR_FAMILY, "cmr10"},
92 { CMSY_FAMILY, "cmsy10"},
93 { CMM_FAMILY, "cmmi10"},
94 { CMEX_FAMILY, "cmex10"},
95 { MSA_FAMILY, "msam10"},
96 { MSB_FAMILY, "msbm10"},
97 { DS_FAMILY, "dsrom10"},
98 { EUFRAK_FAMILY,"eufm10"},
99 { RSFS_FAMILY, "rsfs10"},
100 { STMARY_FAMILY,"stmary10"},
101 { WASY_FAMILY, "wasy10"},
102 { ESINT_FAMILY, "esint10"}
106 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
110 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
113 // returns a reference to the pointer type (GuiFontInfo *) in the
115 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
117 // The display font and the text font are the same
118 size_t const style = (f.style() == DISPLAY_STYLE) ? TEXT_STYLE : f.style();
119 return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
123 // Get font info (font + metrics) for the given LyX font.
124 // if not cached, create it.
125 GuiFontInfo & fontinfo(FontInfo const & f)
127 bool const fontIsRealized =
128 (f.family() < NUM_FAMILIES) &&
129 (f.series() < NUM_SERIES) &&
130 (f.realShape() < NUM_SHAPE) &&
131 (f.size() < NUM_SIZE);
132 if (!fontIsRealized) {
133 // We can reset the font to something sensible in release mode.
134 LYXERR0("Unrealized font!" << f);
137 f2.realize(sane_font);
138 GuiFontInfo * & fi = fontinfo_ptr(f2);
140 fi = new GuiFontInfo(f2);
143 GuiFontInfo * & fi = fontinfo_ptr(f);
145 fi = new GuiFontInfo(f);
150 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
151 QString rawName(QString const & family)
153 for (size_t i = 0; i < nr_symbol_fonts; ++i)
154 if (family == symbol_fonts[i].family)
155 return symbol_fonts[i].xlfd;
157 LYXERR(Debug::FONT, "BUG: family not found !");
163 QString symbolFamily(FontFamily family)
165 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
166 if (family == symbol_fonts[i].lyx_family)
167 return symbol_fonts[i].family;
174 bool isSymbolFamily(FontFamily family)
176 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
181 static bool isChosenFont(QFont & font, QString const & family,
182 QString const & style)
184 // QFontInfo won't find a font that has only a few glyphs at unusual
185 // positions, e.g. the original esint10 font.
186 // The workaround is to add dummy glyphs at least at all ASCII
190 LYXERR_NOPOS(Debug::FONT, "got: " << fi.family());
192 if (fi.family().contains(family)
193 #if QT_VERSION >= 0x040800
194 && (style.isEmpty() || fi.styleName().contains(style))
197 LYXERR_NOENDL(Debug::FONT, " got it ");
205 QFont symbolFont(QString const & family, bool * ok)
207 LYXERR_NOENDL(Debug::FONT, "Looking for font family " << family << " ... ");
208 QString upper = family;
209 upper[0] = family[0].toUpper();
212 if (guiApp->platformName() == "qt4x11"
213 || guiApp->platformName() == "xcb"
214 || guiApp->platformName().contains("wayland")) {
215 // On *nix we have to also specify the foundry to be able to
216 // discriminate our fonts when the texlive fonts are managed by
217 // fontconfig. Unfortunately, doing the same on Windows breaks things.
218 font.setFamily(family + QLatin1String(" [LyEd]"));
220 font.setFamily(family);
222 font.setStyleStrategy(QFont::NoFontMerging);
223 #if QT_VERSION >= 0x040800
224 font.setStyleName("LyX");
226 if (isChosenFont(font, family, "LyX")) {
227 LYXERR_NOPOS(Debug::FONT, "lyx!");
232 LYXERR_NOENDL(Debug::FONT, "Trying normal " << family << " ... ");
233 font.setStyleName(QString());
236 if (isChosenFont(font, family, QString())) {
237 LYXERR_NOPOS(Debug::FONT, "normal!");
242 LYXERR_NOENDL(Debug::FONT, "Trying " << upper << " ... ");
243 font.setFamily(upper);
245 if (isChosenFont(font, upper, QString())) {
246 LYXERR_NOPOS(Debug::FONT, "upper!");
251 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
252 // A simple setFamily() fails on Qt 2
254 QString const raw = rawName(family);
255 LYXERR_NOENDL(Debug::FONT, "Trying " << raw << " ... ");
256 font.setRawName(raw);
258 if (isChosenFont(font, family, QString())) {
259 LYXERR_NOPOS(Debug::FONT, "raw version!");
265 LYXERR_NOPOS(Debug::FONT, " FAILED :-(");
273 FontLoader::FontLoader()
275 QString const fonts_dir =
276 toqstr(addPath(package().system_support().absFileName(), "fonts"));
278 for (int i = 0 ; i < num_math_fonts; ++i) {
279 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
280 int fontID = QFontDatabase::addApplicationFont(font_file);
282 LYXERR(Debug::FONT, "Adding font " << font_file
283 << (fontID < 0 ? " FAIL" : " OK"));
286 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
287 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
288 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
289 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
290 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
291 fontinfo_[i1][i2][i3][i4][i5] = 0;
295 void FontLoader::update()
297 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
298 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
299 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
300 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
301 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
302 delete fontinfo_[i1][i2][i3][i4][i5];
303 fontinfo_[i1][i2][i3][i4][i5] = 0;
308 FontLoader::~FontLoader()
313 /////////////////////////////////////////////////
317 QString makeFontName(QString const & family, QString const & foundry)
319 QString res = family;
320 if (!foundry.isEmpty())
321 res += " [" + foundry + ']';
326 QFont makeQFont(FontInfo const & f)
329 QString const pat = symbolFamily(f.family());
330 if (!pat.isEmpty()) {
332 font = symbolFont(pat, &ok);
334 switch (f.family()) {
336 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
337 toqstr(lyxrc.roman_font_foundry));
338 font.setFamily(family);
340 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
341 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
342 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
343 // FIXME: Check whether this is really fixed in Qt 4.8
344 if (family == "Times" && !font.exactMatch())
345 font.setFamily("Times New Roman");
351 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
352 toqstr(lyxrc.sans_font_foundry)));
354 case TYPEWRITER_FAMILY:
355 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
356 toqstr(lyxrc.typewriter_font_foundry)));
363 switch (f.series()) {
365 font.setWeight(QFont::Normal);
368 font.setWeight(QFont::Bold);
374 switch (f.realShape()) {
376 font.setStyle(QFont::StyleItalic);
379 font.setStyle(QFont::StyleOblique);
381 case SMALLCAPS_SHAPE:
382 font.setCapitalization(QFont::SmallCaps);
388 LYXERR(Debug::FONT, "Font '" << f.stateText()
389 << "' matched by\n" << font.family());
391 // Is this an exact match?
392 if (font.exactMatch())
393 LYXERR(Debug::FONT, "This font is an exact match");
395 LYXERR(Debug::FONT, "This font is NOT an exact match");
397 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
398 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
401 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
403 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
411 GuiFontInfo::GuiFontInfo(FontInfo const & f)
412 : font(makeQFont(f)), metrics(font)
416 bool FontLoader::available(FontInfo const & f)
419 static vector<int> cache_set(NUM_FAMILIES, false);
420 static vector<int> cache(NUM_FAMILIES, false);
422 FontFamily family = f.family();
424 // Apple ships a font name "Symbol", which has more or less the same
425 // glyphs as the original PostScript Symbol font, but it uses a different
426 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
427 // Since we expect the font specific encoding of the original
428 // PostScript Symbol font, we can't use the one provided on OS X.
429 // See also the discussion in bug 7954.
430 if (f.family() == SYMBOL_FAMILY)
433 if (cache_set[family])
434 return cache[family];
435 cache_set[family] = true;
437 QString const pat = symbolFamily(family);
439 // We don't care about non-symbol fonts
443 symbolFont(pat, &ok);
447 cache[family] = true;
452 bool FontLoader::canBeDisplayed(char_type c)
456 // FIXME check whether this is still needed for Qt5
458 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
459 // bug 7954, see also comment in GuiPainter::text()
467 FontMetrics const & FontLoader::metrics(FontInfo const & f)
469 return fontinfo(f).metrics;
473 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
475 return fontinfo(f).metrics;
479 QFont const & getFont(FontInfo const & f)
481 return fontinfo(f).font;
484 } // namespace frontend