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/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/Systemcall.h"
28 #include "support/Package.h"
29 #include "support/os.h"
32 #include <QFontDatabase>
34 #include "support/lassert.h"
37 using namespace lyx::support;
39 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
40 "esint10", "eufm10", "msam10", "msbm10", "rsfs10", "stmary10",
42 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
46 extern docstring const stateText(FontInfo const & f);
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]);
91 static GuiFontInfo * fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE];
94 // Get font info (font + metrics) for the given LyX font.
95 // if not cached, create it.
96 GuiFontInfo & fontinfo(FontInfo const & f)
98 bool const fontIsRealized =
99 (f.family() < NUM_FAMILIES) &&
100 (f.series() < NUM_SERIES) &&
101 (f.realShape() < NUM_SHAPE) &&
102 (f.size() < NUM_SIZE);
103 if (!fontIsRealized) {
104 // We can reset the font to something sensible in release mode.
106 LYXERR0("Unrealized font!");
107 // We could be fancier here, if we wanted, and just fix things where
108 // there is a problem. But it doesn't seem worth it, since we should
109 // not be here in the first place.
111 fontinfo_[sane_font.family()][sane_font.series()][sane_font.realShape()][sane_font.size()];
113 fi = new GuiFontInfo(f);
116 // fi is a reference to the pointer type (GuiFontInfo *) in the
119 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
121 fi = new GuiFontInfo(f);
126 QString rawName(QString const & family)
128 for (size_t i = 0; i < nr_symbol_fonts; ++i)
129 if (family == symbol_fonts[i].family)
130 return symbol_fonts[i].xlfd;
132 LYXERR(Debug::FONT, "BUG: family not found !");
137 QString symbolFamily(FontFamily family)
139 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
140 if (family == symbol_fonts[i].lyx_family)
141 return symbol_fonts[i].family;
148 bool isSymbolFamily(FontFamily family)
150 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
155 static bool isChosenFont(QFont & font, QString const & family,
156 QString const & style)
158 // QFontInfo won't find a font that has only a few glyphs at unusual
159 // positions, e.g. the original esint10 font.
160 // The workaround is to add dummy glyphs at least at all ASCII
164 LYXERR(Debug::FONT, "got: " << fi.family());
166 if (fi.family().contains(family)
167 #if QT_VERSION >= 0x040800
168 && (style.isEmpty() || fi.styleName().contains(style))
171 LYXERR(Debug::FONT, " got it ");
179 QFont symbolFont(QString const & family, bool * ok)
181 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
182 QString upper = family;
183 upper[0] = family[0].toUpper();
186 font.setFamily(family);
187 #if QT_VERSION >= 0x040800
188 font.setStyleName("LyX");
190 if (isChosenFont(font, family, "LyX")) {
191 LYXERR(Debug::FONT, "lyx!");
196 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
197 font.setStyleName(QString());
200 if (isChosenFont(font, family, QString())) {
201 LYXERR(Debug::FONT, "normal!");
206 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
207 font.setFamily(upper);
209 if (isChosenFont(font, upper, QString())) {
210 LYXERR(Debug::FONT, "upper!");
215 // A simple setFamily() fails on Qt 2
217 QString const raw = rawName(family);
218 LYXERR(Debug::FONT, "Trying " << raw << " ... ");
219 font.setRawName(raw);
221 if (isChosenFont(font, family, QString())) {
222 LYXERR(Debug::FONT, "raw version!");
227 LYXERR(Debug::FONT, " FAILED :-(");
235 FontLoader::FontLoader()
237 QString const fonts_dir =
238 toqstr(addPath(package().system_support().absFileName(), "fonts"));
240 for (int i = 0 ; i < num_math_fonts; ++i) {
241 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
242 int fontID = QFontDatabase::addApplicationFont(font_file);
244 LYXERR(Debug::FONT, "Adding font " << font_file
245 << (fontID < 0 ? " FAIL" : " OK"));
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 fontinfo_[i1][i2][i3][i4] = 0;
256 void FontLoader::update()
258 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
259 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
260 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
261 for (int i4 = 0; i4 < NUM_SIZE; ++i4) {
262 delete fontinfo_[i1][i2][i3][i4];
263 fontinfo_[i1][i2][i3][i4] = 0;
268 FontLoader::~FontLoader()
273 /////////////////////////////////////////////////
277 QString makeFontName(QString const & family, QString const & foundry)
279 QString res = family;
280 if (!foundry.isEmpty())
281 res += " [" + foundry + ']';
286 QFont makeQFont(FontInfo const & f)
289 QString const pat = symbolFamily(f.family());
290 if (!pat.isEmpty()) {
292 font = symbolFont(pat, &ok);
294 switch (f.family()) {
296 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
297 toqstr(lyxrc.roman_font_foundry));
298 font.setFamily(family);
300 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
301 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
302 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
303 // FIXME: Check whether this is really fixed in Qt 4.8
304 if (family == "Times" && !font.exactMatch())
305 font.setFamily("Times New Roman");
311 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
312 toqstr(lyxrc.sans_font_foundry)));
314 case TYPEWRITER_FAMILY:
315 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
316 toqstr(lyxrc.typewriter_font_foundry)));
323 switch (f.series()) {
325 font.setWeight(QFont::Normal);
328 font.setWeight(QFont::Bold);
334 switch (f.realShape()) {
336 font.setStyle(QFont::StyleItalic);
339 font.setStyle(QFont::StyleOblique);
341 case SMALLCAPS_SHAPE:
342 font.setCapitalization(QFont::SmallCaps);
348 LYXERR(Debug::FONT, "Font '" << stateText(f)
349 << "' matched by\n" << font.family());
351 // Is this an exact match?
352 if (font.exactMatch())
353 LYXERR(Debug::FONT, "This font is an exact match");
355 LYXERR(Debug::FONT, "This font is NOT an exact match");
357 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
359 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
360 * lyxrc.zoom / 100.0);
362 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
370 GuiFontInfo::GuiFontInfo(FontInfo const & f)
371 : font(makeQFont(f)), metrics(font)
375 bool FontLoader::available(FontInfo const & f)
378 static vector<int> cache_set(NUM_FAMILIES, false);
379 static vector<int> cache(NUM_FAMILIES, false);
381 FontFamily family = f.family();
383 // Apple ships a font name "Symbol", which has more or less the same
384 // glyphs as the original PostScript Symbol font, but it uses a different
385 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
386 // Since we expect the font specific encoding of the original
387 // PostScript Symbol font, we can't use the one provided on OS X.
388 // See also the discussion in bug 7954.
389 if (f.family() == SYMBOL_FAMILY)
392 if (cache_set[family])
393 return cache[family];
394 cache_set[family] = true;
396 QString const pat = symbolFamily(family);
398 // We don't care about non-symbol fonts
402 symbolFont(pat, &ok);
406 cache[family] = true;
411 bool FontLoader::canBeDisplayed(char_type c)
415 // FIXME check whether this is still needed for Qt5
417 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
418 // bug 7954, see also comment in GuiPainter::text()
426 FontMetrics const & FontLoader::metrics(FontInfo const & f)
428 return fontinfo(f).metrics;
432 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
434 return fontinfo(f).metrics;
438 QFont const & getFont(FontInfo const & f)
440 return fontinfo(f).font;
443 } // namespace frontend