2 * \file GuiFontLoader.cpp
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"
15 #include "qt_helpers.h"
20 #include "support/convert.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/Systemcall.h"
24 #include "support/Package.h"
25 #include "support/os.h"
27 #include <boost/tuple/tuple.hpp>
30 #include <QFontDatabase>
32 using lyx::support::contains;
33 using lyx::support::package;
34 using lyx::support::addPath;
35 using lyx::support::addName;
43 #if QT_VERSION >= 0x040200
44 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
45 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
46 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
52 extern docstring const stateText(FontInfo const & f);
59 FontFamily lyx_family;
64 SymbolFont symbol_fonts[] = {
67 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
71 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
83 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
87 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
91 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
95 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
99 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
103 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
106 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont);
109 QString getRawName(QString const & family)
111 for (size_t i = 0; i < nr_symbol_fonts; ++i)
112 if (family == symbol_fonts[i].family)
113 return symbol_fonts[i].xlfd;
115 LYXERR(Debug::FONT) << "BUG: family not found !" << endl;
120 QString const symbolFamily(FontFamily family)
122 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
123 if (family == symbol_fonts[i].lyx_family)
124 return symbol_fonts[i].family;
130 bool isSymbolFamily(FontFamily family)
132 return family >= SYMBOL_FAMILY &&
133 family <= ESINT_FAMILY;
137 static bool isChosenFont(QFont & font, QString const & family)
139 // QFontInfo won't find a font that has only a few glyphs at unusual
140 // positions, e.g. the original esint10 font.
141 // The workaround is to add dummy glyphs at least at all ASCII
145 LYXERR(Debug::FONT) << "got: " << fromqstr(fi.family()) << endl;
147 if (fi.family().contains(family)) {
148 LYXERR(Debug::FONT) << " got it ";
156 static pair<QFont, bool> const getSymbolFont(QString const & family)
158 LYXERR(Debug::FONT) << "Looking for font family "
159 << fromqstr(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!" << endl;
169 return make_pair<QFont, bool>(font, true);
172 LYXERR(Debug::FONT) << "Trying " << fromqstr(upper) << " ... ";
173 font.setFamily(upper);
175 if (isChosenFont(font, upper)) {
176 LYXERR(Debug::FONT) << "upper!" << endl;
177 return make_pair<QFont, bool>(font, true);
180 // A simple setFamily() fails on Qt 2
182 QString const rawName = getRawName(family);
183 LYXERR(Debug::FONT) << "Trying " << fromqstr(rawName) << " ... ";
184 font.setRawName(rawName);
186 if (isChosenFont(font, family)) {
187 LYXERR(Debug::FONT) << "raw version!" << endl;
188 return make_pair<QFont, bool>(font, true);
191 LYXERR(Debug::FONT) << " FAILED :-(" << endl;
192 return make_pair<QFont, bool>(font, false);
198 GuiFontLoader::GuiFontLoader()
200 #if QT_VERSION >= 0x040200
201 QString const fonts_dir =
202 toqstr(addPath(package().system_support().absFilename(), "fonts"));
204 for (int i = 0 ; i < num_math_fonts; ++i) {
205 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
206 int fontID = QFontDatabase::addApplicationFont(font_file);
208 LYXERR(Debug::FONT) << "Adding font " << fromqstr(font_file)
209 << static_cast<const char *>
210 (fontID < 0 ? " FAIL" : " OK")
215 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
216 for (int i2 = 0; i2 < 2; ++i2)
217 for (int i3 = 0; i3 < 4; ++i3)
218 for (int i4 = 0; i4 < 10; ++i4)
219 fontinfo_[i1][i2][i3][i4] = 0;
223 void GuiFontLoader::update()
225 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
226 for (int i2 = 0; i2 < 2; ++i2)
227 for (int i3 = 0; i3 < 4; ++i3)
228 for (int i4 = 0; i4 < 10; ++i4) {
229 delete fontinfo_[i1][i2][i3][i4];
230 fontinfo_[i1][i2][i3][i4] = 0;
236 /////////////////////////////////////////////////
239 static QString makeFontName(QString const & family, QString const & foundry)
241 QString res = family;
242 if (!foundry.isEmpty())
243 res += " [" + foundry + ']';
248 GuiFontInfo::GuiFontInfo(FontInfo const & f)
250 font.setKerning(false);
251 QString const pat = symbolFamily(f.family());
252 if (!pat.isEmpty()) {
254 boost::tie(font, tmp) = getSymbolFont(pat);
256 switch (f.family()) {
258 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
259 toqstr(lyxrc.roman_font_foundry));
260 font.setFamily(family);
262 #if QT_VERSION >= 0x040300
263 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
264 // It is reported to Trolltech at 02/06/07 against 4.3 final.
265 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
266 if (family == "Times" && !font.exactMatch())
267 font.setFamily("Times New Roman");
273 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
274 toqstr(lyxrc.sans_font_foundry)));
276 case TYPEWRITER_FAMILY:
277 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
278 toqstr(lyxrc.typewriter_font_foundry)));
285 switch (f.series()) {
287 font.setWeight(QFont::Normal);
290 font.setWeight(QFont::Bold);
296 switch (f.realShape()) {
299 font.setItalic(true);
305 LYXERR(Debug::FONT) << "Font '" << to_utf8(stateText(f))
306 << "' matched by\n" << fromqstr(font.family()) << endl;
308 // Is this an exact match?
309 if (font.exactMatch())
310 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
312 LYXERR(Debug::FONT) << "This font is NOT an exact match"
315 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
317 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
318 * lyxrc.zoom / 100.0);
320 LYXERR(Debug::FONT) << "The font has size: "
321 << font.pointSizeF() << endl;
323 if (f.realShape() != SMALLCAPS_SHAPE) {
324 metrics.reset(new GuiFontMetrics(font));
327 // handle small caps ourselves ...
328 FontInfo smallfont = f;
329 smallfont.decSize().decSize().setShape(UP_SHAPE);
331 font2.setKerning(false);
332 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
333 * lyxrc.zoom / 100.0);
335 metrics.reset(new GuiFontMetrics(font, font2));
341 bool GuiFontLoader::available(FontInfo const & f)
343 static vector<int> cache_set(NUM_FAMILIES, false);
344 static vector<int> cache(NUM_FAMILIES, false);
346 FontFamily family = f.family();
347 if (cache_set[family])
348 return cache[family];
349 cache_set[family] = true;
351 QString const pat = symbolFamily(family);
353 // We don't care about non-symbol fonts
356 pair<QFont, bool> tmp = getSymbolFont(pat);
360 cache[family] = true;
364 } // namespace frontend