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 // LASSERT: Is there anything we might do here besides crash?
99 LBUFERR(f.family() < NUM_FAMILIES);
100 LBUFERR(f.series() < NUM_SERIES);
101 LBUFERR(f.realShape() < NUM_SHAPE);
102 LBUFERR(f.size() < NUM_SIZE);
103 // fi is a reference to the pointer type (GuiFontInfo *) in the
106 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
108 fi = new GuiFontInfo(f);
113 QString rawName(QString const & family)
115 for (size_t i = 0; i < nr_symbol_fonts; ++i)
116 if (family == symbol_fonts[i].family)
117 return symbol_fonts[i].xlfd;
119 LYXERR(Debug::FONT, "BUG: family not found !");
124 QString symbolFamily(FontFamily family)
126 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
127 if (family == symbol_fonts[i].lyx_family)
128 return symbol_fonts[i].family;
135 bool isSymbolFamily(FontFamily family)
137 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
142 static bool isChosenFont(QFont & font, QString const & family)
144 // QFontInfo won't find a font that has only a few glyphs at unusual
145 // positions, e.g. the original esint10 font.
146 // The workaround is to add dummy glyphs at least at all ASCII
150 LYXERR(Debug::FONT, "got: " << fi.family());
152 if (fi.family().contains(family)) {
153 LYXERR(Debug::FONT, " got it ");
161 QFont symbolFont(QString const & family, bool * ok)
163 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
164 QString upper = family;
165 upper[0] = family[0].toUpper();
168 font.setKerning(false);
169 font.setFamily(family);
171 if (isChosenFont(font, family)) {
172 LYXERR(Debug::FONT, "normal!");
177 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
178 font.setFamily(upper);
180 if (isChosenFont(font, upper)) {
181 LYXERR(Debug::FONT, "upper!");
186 // A simple setFamily() fails on Qt 2
188 QString const raw = rawName(family);
189 LYXERR(Debug::FONT, "Trying " << raw << " ... ");
190 font.setRawName(raw);
192 if (isChosenFont(font, family)) {
193 LYXERR(Debug::FONT, "raw version!");
198 LYXERR(Debug::FONT, " FAILED :-(");
206 FontLoader::FontLoader()
208 QString const fonts_dir =
209 toqstr(addPath(package().system_support().absFileName(), "fonts"));
211 for (int i = 0 ; i < num_math_fonts; ++i) {
212 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
213 int fontID = QFontDatabase::addApplicationFont(font_file);
215 LYXERR(Debug::FONT, "Adding font " << font_file
216 << (fontID < 0 ? " FAIL" : " OK"));
219 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
220 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
221 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
222 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
223 fontinfo_[i1][i2][i3][i4] = 0;
227 void FontLoader::update()
229 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
230 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
231 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
232 for (int i4 = 0; i4 < NUM_SIZE; ++i4) {
233 delete fontinfo_[i1][i2][i3][i4];
234 fontinfo_[i1][i2][i3][i4] = 0;
239 FontLoader::~FontLoader()
244 /////////////////////////////////////////////////
247 static QString makeFontName(QString const & family, QString const & foundry)
249 QString res = family;
250 if (!foundry.isEmpty())
251 res += " [" + foundry + ']';
256 GuiFontInfo::GuiFontInfo(FontInfo const & f)
259 font.setKerning(false);
260 QString const pat = symbolFamily(f.family());
261 if (!pat.isEmpty()) {
263 font = symbolFont(pat, &ok);
265 switch (f.family()) {
267 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
268 toqstr(lyxrc.roman_font_foundry));
269 font.setFamily(family);
271 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
272 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
273 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
274 // FIXME: Check whether this is really fixed in Qt 4.8
275 if (family == "Times" && !font.exactMatch())
276 font.setFamily("Times New Roman");
282 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
283 toqstr(lyxrc.sans_font_foundry)));
285 case TYPEWRITER_FAMILY:
286 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
287 toqstr(lyxrc.typewriter_font_foundry)));
294 switch (f.series()) {
296 font.setWeight(QFont::Normal);
299 font.setWeight(QFont::Bold);
305 switch (f.realShape()) {
308 font.setItalic(true);
314 LYXERR(Debug::FONT, "Font '" << stateText(f)
315 << "' matched by\n" << font.family());
317 // Is this an exact match?
318 if (font.exactMatch())
319 LYXERR(Debug::FONT, "This font is an exact match");
321 LYXERR(Debug::FONT, "This font is NOT an exact match");
323 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
325 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
326 * lyxrc.zoom / 100.0);
328 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
330 if (f.realShape() != SMALLCAPS_SHAPE) {
331 metrics = GuiFontMetrics(font);
333 // handle small caps ourselves ...
334 FontInfo smallfont = f;
335 smallfont.decSize().decSize().setShape(UP_SHAPE);
337 font2.setKerning(false);
338 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
339 * lyxrc.zoom / 100.0);
341 metrics = GuiFontMetrics(font, font2);
346 bool FontLoader::available(FontInfo const & f)
348 static vector<int> cache_set(NUM_FAMILIES, false);
349 static vector<int> cache(NUM_FAMILIES, false);
351 FontFamily family = f.family();
352 if (cache_set[family])
353 return cache[family];
354 cache_set[family] = true;
356 QString const pat = symbolFamily(family);
358 // We don't care about non-symbol fonts
362 symbolFont(pat, &ok);
366 cache[family] = true;
371 FontMetrics const & FontLoader::metrics(FontInfo const & f)
373 return fontinfo(f).metrics;
377 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
379 return fontinfo(f).metrics;
383 QFont const & getFont(FontInfo const & f)
385 return fontinfo(f).font;
388 } // namespace frontend