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/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);
45 extern docstring const stateText(FontInfo const & f);
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;
72 SymbolFont symbol_fonts[] = {
73 { SYMBOL_FAMILY,"symbol", "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific"},
74 { CMR_FAMILY, "cmr10", "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 { CMSY_FAMILY, "cmsy10", "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
76 { CMM_FAMILY, "cmmi10", "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
77 { CMEX_FAMILY, "cmex10", "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
78 { MSA_FAMILY, "msam10", "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 { MSB_FAMILY, "msbm10", "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
80 { EUFRAK_FAMILY,"eufm10", "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
81 { RSFS_FAMILY, "rsfs10", "-*-rsfs10-medium-*-*-*-*-*-*-*-*-*-*-*" },
82 { STMARY_FAMILY,"stmary10","-*-stmary10-medium-*-*-*-*-*-*-*-*-*-*-*" },
83 { WASY_FAMILY, "wasy10", "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
84 { ESINT_FAMILY, "esint10","-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
87 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
90 static GuiFontInfo * fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE];
93 /// Get font info (font + metrics) for the given LyX font.
94 // if not cached, create it.
95 GuiFontInfo & fontinfo(FontInfo const & f)
97 LASSERT(f.family() < NUM_FAMILIES, /**/);
98 LASSERT(f.series() < NUM_SERIES, /**/);
99 LASSERT(f.realShape() < NUM_SHAPE, /**/);
100 LASSERT(f.size() < NUM_SIZE, /**/);
101 // fi is a reference to the pointer type (GuiFontInfo *) in the
104 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
106 fi = new GuiFontInfo(f);
111 QString rawName(QString const & family)
113 for (size_t i = 0; i < nr_symbol_fonts; ++i)
114 if (family == symbol_fonts[i].family)
115 return symbol_fonts[i].xlfd;
117 LYXERR(Debug::FONT, "BUG: family not found !");
122 QString symbolFamily(FontFamily family)
124 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
125 if (family == symbol_fonts[i].lyx_family)
126 return symbol_fonts[i].family;
132 bool isSymbolFamily(FontFamily family)
134 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
138 static bool isChosenFont(QFont & font, QString const & family)
140 // QFontInfo won't find a font that has only a few glyphs at unusual
141 // positions, e.g. the original esint10 font.
142 // The workaround is to add dummy glyphs at least at all ASCII
146 LYXERR(Debug::FONT, "got: " << fi.family());
148 if (fi.family().contains(family)) {
149 LYXERR(Debug::FONT, " got it ");
157 QFont symbolFont(QString const & family, bool * ok)
159 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
160 QString upper = family;
161 upper[0] = family[0].toUpper();
164 font.setKerning(false);
165 font.setFamily(family);
167 if (isChosenFont(font, family)) {
168 LYXERR(Debug::FONT, "normal!");
173 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
174 font.setFamily(upper);
176 if (isChosenFont(font, upper)) {
177 LYXERR(Debug::FONT, "upper!");
182 // A simple setFamily() fails on Qt 2
184 QString const raw = rawName(family);
185 LYXERR(Debug::FONT, "Trying " << raw << " ... ");
186 font.setRawName(raw);
188 if (isChosenFont(font, family)) {
189 LYXERR(Debug::FONT, "raw version!");
194 LYXERR(Debug::FONT, " FAILED :-(");
202 FontLoader::FontLoader()
204 QString const fonts_dir =
205 toqstr(addPath(package().system_support().absFileName(), "fonts"));
207 for (int i = 0 ; i < num_math_fonts; ++i) {
208 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
209 int fontID = QFontDatabase::addApplicationFont(font_file);
211 LYXERR(Debug::FONT, "Adding font " << font_file
212 << (fontID < 0 ? " FAIL" : " OK"));
215 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
216 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
217 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
218 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
219 fontinfo_[i1][i2][i3][i4] = 0;
223 void FontLoader::update()
225 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
226 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
227 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
228 for (int i4 = 0; i4 < NUM_SIZE; ++i4) {
229 delete fontinfo_[i1][i2][i3][i4];
230 fontinfo_[i1][i2][i3][i4] = 0;
235 FontLoader::~FontLoader()
240 /////////////////////////////////////////////////
243 static QString makeFontName(QString const & family, QString const & foundry)
245 QString res = family;
246 if (!foundry.isEmpty())
247 res += " [" + foundry + ']';
252 GuiFontInfo::GuiFontInfo(FontInfo const & f)
255 font.setKerning(false);
256 QString const pat = symbolFamily(f.family());
257 if (!pat.isEmpty()) {
259 font = symbolFont(pat, &ok);
261 switch (f.family()) {
263 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
264 toqstr(lyxrc.roman_font_foundry));
265 font.setFamily(family);
267 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
268 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
269 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
270 // FIXME: Check whether this is really fixed in Qt 4.8
271 if (family == "Times" && !font.exactMatch())
272 font.setFamily("Times New Roman");
278 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
279 toqstr(lyxrc.sans_font_foundry)));
281 case TYPEWRITER_FAMILY:
282 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
283 toqstr(lyxrc.typewriter_font_foundry)));
290 switch (f.series()) {
292 font.setWeight(QFont::Normal);
295 font.setWeight(QFont::Bold);
301 switch (f.realShape()) {
304 font.setItalic(true);
310 LYXERR(Debug::FONT, "Font '" << stateText(f)
311 << "' matched by\n" << font.family());
313 // Is this an exact match?
314 if (font.exactMatch())
315 LYXERR(Debug::FONT, "This font is an exact match");
317 LYXERR(Debug::FONT, "This font is NOT an exact match");
319 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
321 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
322 * lyxrc.zoom / 100.0);
324 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
326 if (f.realShape() != SMALLCAPS_SHAPE) {
327 metrics = GuiFontMetrics(font);
329 // handle small caps ourselves ...
330 FontInfo smallfont = f;
331 smallfont.decSize().decSize().setShape(UP_SHAPE);
333 font2.setKerning(false);
334 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
335 * lyxrc.zoom / 100.0);
337 metrics = GuiFontMetrics(font, font2);
342 bool FontLoader::available(FontInfo const & f)
344 static vector<int> cache_set(NUM_FAMILIES, false);
345 static vector<int> cache(NUM_FAMILIES, false);
347 FontFamily family = f.family();
348 if (cache_set[family])
349 return cache[family];
350 cache_set[family] = true;
352 QString const pat = symbolFamily(family);
354 // We don't care about non-symbol fonts
358 symbolFont(pat, &ok);
362 cache[family] = true;
367 FontMetrics const & FontLoader::metrics(FontInfo const & f)
369 return fontinfo(f).metrics;
373 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
375 return fontinfo(f).metrics;
379 QFont const & getFont(FontInfo const & f)
381 return fontinfo(f).font;
384 } // namespace frontend