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;
71 SymbolFont symbol_fonts[] = {
72 { SYMBOL_FAMILY,"symbol"},
73 { CMR_FAMILY, "cmr10"},
74 { CMSY_FAMILY, "cmsy10"},
75 { CMM_FAMILY, "cmmi10"},
76 { CMEX_FAMILY, "cmex10"},
77 { MSA_FAMILY, "msam10"},
78 { MSB_FAMILY, "msbm10"},
79 { DS_FAMILY, "dsrom10"},
80 { EUFRAK_FAMILY,"eufm10"},
81 { RSFS_FAMILY, "rsfs10"},
82 { STMARY_FAMILY,"stmary10"},
83 { WASY_FAMILY, "wasy10"},
84 { ESINT_FAMILY, "esint10"}
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() == DISPLAY_STYLE) ? TEXT_STYLE : 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.
115 LYXERR0("Unrealized font!" << f);
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 symbolFamily(FontFamily family)
133 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
134 if (family == symbol_fonts[i].lyx_family)
135 return symbol_fonts[i].family;
142 bool isSymbolFamily(FontFamily family)
144 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
149 static bool isChosenFont(QFont & font, QString const & family,
150 QString const & style)
152 // QFontInfo won't find a font that has only a few glyphs at unusual
153 // positions, e.g. the original esint10 font.
154 // The workaround is to add dummy glyphs at least at all ASCII
158 LYXERR_NOPOS(Debug::FONT, "got: " << fi.family());
160 if (fi.family().contains(family)
161 && (style.isEmpty() || fi.styleName().contains(style))) {
162 LYXERR_NOENDL(Debug::FONT, " got it ");
170 QFont symbolFont(QString const & family, bool * ok)
172 LYXERR_NOENDL(Debug::FONT, "Looking for font family " << family << " ... ");
173 QString upper = family;
174 upper[0] = family[0].toUpper();
177 if (guiApp->platformName() == "xcb"
178 || guiApp->platformName().contains("wayland")) {
179 // On *nix we have to also specify the foundry to be able to
180 // discriminate our fonts when the texlive fonts are managed by
181 // fontconfig. Unfortunately, doing the same on Windows breaks things.
182 font.setFamily(family + QLatin1String(" [LyEd]"));
184 font.setFamily(family);
186 font.setStyleStrategy(QFont::NoFontMerging);
187 font.setStyleName("LyX");
189 if (isChosenFont(font, family, "LyX")) {
190 LYXERR_NOPOS(Debug::FONT, "lyx!");
195 LYXERR_NOENDL(Debug::FONT, "Trying normal " << family << " ... ");
196 font.setStyleName(QString());
198 if (isChosenFont(font, family, QString())) {
199 LYXERR_NOPOS(Debug::FONT, "normal!");
204 LYXERR_NOENDL(Debug::FONT, "Trying " << upper << " ... ");
205 font.setFamily(upper);
207 if (isChosenFont(font, upper, QString())) {
208 LYXERR_NOPOS(Debug::FONT, "upper!");
213 LYXERR_NOPOS(Debug::FONT, " FAILED :-(");
221 FontLoader::FontLoader()
223 QString const fonts_dir =
224 toqstr(addPath(package().system_support().absFileName(), "fonts"));
226 for (int i = 0 ; i < num_math_fonts; ++i) {
227 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
228 int fontID = QFontDatabase::addApplicationFont(font_file);
230 LYXERR(Debug::FONT, "Adding font " << font_file
231 << (fontID < 0 ? " FAIL" : " OK"));
234 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
235 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
236 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
237 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
238 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
239 fontinfo_[i1][i2][i3][i4][i5] = 0;
243 void FontLoader::update()
245 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
246 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
247 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
248 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
249 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
250 delete fontinfo_[i1][i2][i3][i4][i5];
251 fontinfo_[i1][i2][i3][i4][i5] = 0;
256 FontLoader::~FontLoader()
261 /////////////////////////////////////////////////
265 QString makeFontName(QString const & family, QString const & foundry)
267 QString res = family;
268 if (!foundry.isEmpty())
269 res += " [" + foundry + ']';
274 QFont makeQFont(FontInfo const & f)
277 QString const pat = symbolFamily(f.family());
278 if (!pat.isEmpty()) {
280 font = symbolFont(pat, &ok);
282 switch (f.family()) {
284 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
285 toqstr(lyxrc.roman_font_foundry));
286 font.setFamily(family);
290 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
291 toqstr(lyxrc.sans_font_foundry)));
293 case TYPEWRITER_FAMILY:
294 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
295 toqstr(lyxrc.typewriter_font_foundry)));
302 switch (f.series()) {
304 font.setWeight(QFont::Normal);
307 font.setWeight(QFont::Bold);
313 switch (f.realShape()) {
315 font.setStyle(QFont::StyleItalic);
318 font.setStyle(QFont::StyleOblique);
320 case SMALLCAPS_SHAPE:
321 font.setCapitalization(QFont::SmallCaps);
327 LYXERR(Debug::FONT, "Font '" << f.stateText()
328 << "' matched by\n" << font.family());
330 // Is this an exact match?
331 if (font.exactMatch())
332 LYXERR(Debug::FONT, "This font is an exact match");
334 LYXERR(Debug::FONT, "This font is NOT an exact match");
336 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
338 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
346 GuiFontInfo::GuiFontInfo(FontInfo const & f)
347 : font(makeQFont(f)), metrics(font)
351 bool FontLoader::available(FontInfo const & f)
354 static vector<int> cache_set(NUM_FAMILIES, false);
355 static vector<int> cache(NUM_FAMILIES, false);
357 FontFamily family = f.family();
359 // Apple ships a font name "Symbol", which has more or less the same
360 // glyphs as the original PostScript Symbol font, but it uses a different
361 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
362 // Since we expect the font specific encoding of the original
363 // PostScript Symbol font, we can't use the one provided on OS X.
364 // See also the discussion in bug 7954.
365 if (f.family() == SYMBOL_FAMILY)
368 if (cache_set[family])
369 return cache[family];
370 cache_set[family] = true;
372 QString const pat = symbolFamily(family);
374 // We don't care about non-symbol fonts
378 symbolFont(pat, &ok);
382 cache[family] = true;
387 bool FontLoader::canBeDisplayed(char_type c)
389 // bug 8493: with Qt5 and Qt6, characters with codepoints 9, 10, 12 and
390 // 13 are rendered as spaces. We handle that in our TeX fonts by making
391 // copies of the glyphs elsewhere, but this may trigger if we are
393 if (c == 0x0009 || c == 0x000a || c == 0x000c || c == 0x000d)
399 FontMetrics const & FontLoader::metrics(FontInfo const & f)
401 return fontinfo(f).metrics;
405 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
407 return fontinfo(f).metrics;
411 QFont const & getFont(FontInfo const & f)
413 return fontinfo(f).font;
416 } // namespace frontend