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;
69 SymbolFont symbol_fonts[] = {
70 { SYMBOL_FAMILY,"symbol"},
71 { CMR_FAMILY, "cmr10"},
72 { CMSY_FAMILY, "cmsy10"},
73 { CMM_FAMILY, "cmmi10"},
74 { CMEX_FAMILY, "cmex10"},
75 { MSA_FAMILY, "msam10"},
76 { MSB_FAMILY, "msbm10"},
77 { EUFRAK_FAMILY,"eufm10"},
78 { RSFS_FAMILY, "rsfs10"},
79 { STMARY_FAMILY,"stmary10"},
80 { WASY_FAMILY, "wasy10"},
81 { ESINT_FAMILY, "esint10"}
84 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
88 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
91 // returns a reference to the pointer type (GuiFontInfo *) in the
93 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
95 // The display font and the text font are the same
96 size_t const style = (f.style() == DISPLAY_STYLE) ? TEXT_STYLE : f.style();
97 return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
101 // Get font info (font + metrics) for the given LyX font.
102 // if not cached, create it.
103 GuiFontInfo & fontinfo(FontInfo const & f)
105 bool const fontIsRealized =
106 (f.family() < NUM_FAMILIES) &&
107 (f.series() < NUM_SERIES) &&
108 (f.realShape() < NUM_SHAPE) &&
109 (f.size() < NUM_SIZE);
110 if (!fontIsRealized) {
111 // We can reset the font to something sensible in release mode.
113 LYXERR0("Unrealized font!");
115 f2.realize(sane_font);
116 GuiFontInfo * & fi = fontinfo_ptr(f2);
118 fi = new GuiFontInfo(f2);
121 GuiFontInfo * & fi = fontinfo_ptr(f);
123 fi = new GuiFontInfo(f);
128 QString symbolFamily(FontFamily family)
130 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
131 if (family == symbol_fonts[i].lyx_family)
132 return symbol_fonts[i].family;
139 bool isSymbolFamily(FontFamily family)
141 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
146 static bool isChosenFont(QFont & font, QString const & family,
147 QString const & style)
149 // QFontInfo won't find a font that has only a few glyphs at unusual
150 // positions, e.g. the original esint10 font.
151 // The workaround is to add dummy glyphs at least at all ASCII
155 LYXERR(Debug::FONT, "got: " << fi.family());
157 if (fi.family().contains(family)
158 #if QT_VERSION >= 0x040800
159 && (style.isEmpty() || fi.styleName().contains(style))
162 LYXERR(Debug::FONT, " got it ");
170 QFont symbolFont(QString const & family, bool * ok)
172 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
173 QString upper = family;
174 upper[0] = family[0].toUpper();
177 font.setFamily(family);
178 #if QT_VERSION >= 0x040800
179 font.setStyleName("LyX");
181 if (isChosenFont(font, family, "LyX")) {
182 LYXERR(Debug::FONT, "lyx!");
187 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
188 font.setStyleName(QString());
191 if (isChosenFont(font, family, QString())) {
192 LYXERR(Debug::FONT, "normal!");
197 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
198 font.setFamily(upper);
200 if (isChosenFont(font, upper, QString())) {
201 LYXERR(Debug::FONT, "upper!");
206 LYXERR(Debug::FONT, " FAILED :-(");
214 FontLoader::FontLoader()
216 QString const fonts_dir =
217 toqstr(addPath(package().system_support().absFileName(), "fonts"));
219 for (int i = 0 ; i < num_math_fonts; ++i) {
220 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
221 int fontID = QFontDatabase::addApplicationFont(font_file);
223 LYXERR(Debug::FONT, "Adding font " << font_file
224 << (fontID < 0 ? " FAIL" : " OK"));
227 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
228 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
229 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
230 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
231 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
232 fontinfo_[i1][i2][i3][i4][i5] = 0;
236 void FontLoader::update()
238 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
239 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
240 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
241 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
242 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
243 delete fontinfo_[i1][i2][i3][i4][i5];
244 fontinfo_[i1][i2][i3][i4][i5] = 0;
249 FontLoader::~FontLoader()
254 /////////////////////////////////////////////////
258 QString makeFontName(QString const & family, QString const & foundry)
260 QString res = family;
261 if (!foundry.isEmpty())
262 res += " [" + foundry + ']';
267 QFont makeQFont(FontInfo const & f)
270 QString const pat = symbolFamily(f.family());
271 if (!pat.isEmpty()) {
273 font = symbolFont(pat, &ok);
275 switch (f.family()) {
277 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
278 toqstr(lyxrc.roman_font_foundry));
279 font.setFamily(family);
281 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
282 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
283 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
284 // FIXME: Check whether this is really fixed in Qt 4.8
285 if (family == "Times" && !font.exactMatch())
286 font.setFamily("Times New Roman");
292 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
293 toqstr(lyxrc.sans_font_foundry)));
295 case TYPEWRITER_FAMILY:
296 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
297 toqstr(lyxrc.typewriter_font_foundry)));
304 switch (f.series()) {
306 font.setWeight(QFont::Normal);
309 font.setWeight(QFont::Bold);
315 switch (f.realShape()) {
317 font.setStyle(QFont::StyleItalic);
320 font.setStyle(QFont::StyleOblique);
322 case SMALLCAPS_SHAPE:
323 font.setCapitalization(QFont::SmallCaps);
329 LYXERR(Debug::FONT, "Font '" << f.stateText()
330 << "' matched by\n" << font.family());
332 // Is this an exact match?
333 if (font.exactMatch())
334 LYXERR(Debug::FONT, "This font is an exact match");
336 LYXERR(Debug::FONT, "This font is NOT an exact match");
338 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
340 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
348 GuiFontInfo::GuiFontInfo(FontInfo const & f)
349 : font(makeQFont(f)), metrics(font)
353 bool FontLoader::available(FontInfo const & f)
356 static vector<int> cache_set(NUM_FAMILIES, false);
357 static vector<int> cache(NUM_FAMILIES, false);
359 FontFamily family = f.family();
361 // Apple ships a font name "Symbol", which has more or less the same
362 // glyphs as the original PostScript Symbol font, but it uses a different
363 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
364 // Since we expect the font specific encoding of the original
365 // PostScript Symbol font, we can't use the one provided on OS X.
366 // See also the discussion in bug 7954.
367 if (f.family() == SYMBOL_FAMILY)
370 if (cache_set[family])
371 return cache[family];
372 cache_set[family] = true;
374 QString const pat = symbolFamily(family);
376 // We don't care about non-symbol fonts
380 symbolFont(pat, &ok);
384 cache[family] = true;
389 bool FontLoader::canBeDisplayed(char_type c)
393 // FIXME check whether this is still needed for Qt5
395 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
396 // bug 7954, see also comment in GuiPainter::text()
404 FontMetrics const & FontLoader::metrics(FontInfo const & f)
406 return fontinfo(f).metrics;
410 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
412 return fontinfo(f).metrics;
416 QFont const & getFont(FontInfo const & f)
418 return fontinfo(f).font;
421 } // namespace frontend