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 "GuiFontLoader.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 <boost/assert.hpp>
36 using namespace lyx::support;
38 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
39 "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
40 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
44 extern docstring const stateText(FontInfo const & f);
51 FontFamily lyx_family;
56 SymbolFont symbol_fonts[] = {
59 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
63 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
67 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
71 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
87 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
91 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
95 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
98 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
101 static GuiFontInfo * fontinfo_[NUM_FAMILIES][2][4][10];
104 /// Get font info (font + metrics) for the given LyX font.
105 // if not cached, create it.
106 GuiFontInfo & fontinfo(FontInfo const & f)
108 BOOST_ASSERT(f.family() < NUM_FAMILIES);
109 BOOST_ASSERT(f.series() < 2);
110 BOOST_ASSERT(f.realShape() < 4);
111 BOOST_ASSERT(f.size() < 10);
112 // fi is a reference to the pointer type (GuiFontInfo *) in the
115 fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
117 fi = new GuiFontInfo(f);
122 QString rawName(QString const & family)
124 for (size_t i = 0; i < nr_symbol_fonts; ++i)
125 if (family == symbol_fonts[i].family)
126 return symbol_fonts[i].xlfd;
128 LYXERR(Debug::FONT, "BUG: family not found !");
133 QString symbolFamily(FontFamily family)
135 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
136 if (family == symbol_fonts[i].lyx_family)
137 return symbol_fonts[i].family;
143 bool isSymbolFamily(FontFamily family)
145 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
149 static bool isChosenFont(QFont & font, QString const & family)
151 // QFontInfo won't find a font that has only a few glyphs at unusual
152 // positions, e.g. the original esint10 font.
153 // The workaround is to add dummy glyphs at least at all ASCII
157 LYXERR(Debug::FONT, "got: " << fromqstr(fi.family()));
159 if (fi.family().contains(family)) {
160 LYXERR(Debug::FONT, " got it ");
168 QFont symbolFont(QString const & family, bool * ok)
170 LYXERR(Debug::FONT, "Looking for font family "
171 << fromqstr(family) << " ... ");
172 QString upper = family;
173 upper[0] = family[0].toUpper();
176 font.setKerning(false);
177 font.setFamily(family);
179 if (isChosenFont(font, family)) {
180 LYXERR(Debug::FONT, "normal!");
185 LYXERR(Debug::FONT, "Trying " << fromqstr(upper) << " ... ");
186 font.setFamily(upper);
188 if (isChosenFont(font, upper)) {
189 LYXERR(Debug::FONT, "upper!");
194 // A simple setFamily() fails on Qt 2
196 QString const rawName = rawName(family);
197 LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... ");
198 font.setRawName(rawName);
200 if (isChosenFont(font, family)) {
201 LYXERR(Debug::FONT, "raw version!");
206 LYXERR(Debug::FONT, " FAILED :-(");
214 FontLoader::FontLoader()
216 QString const fonts_dir =
217 toqstr(addPath(package().system_support().absFilename(), "fonts"));
219 for (int i = 0 ; i < num_math_fonts; ++i) {
220 QString const font_file = fonts_dir + '/' + math_fonts[i] + ".ttf";
221 int fontID = QFontDatabase::addApplicationFont(font_file);
223 LYXERR(Debug::FONT, "Adding font " << fromqstr(font_file)
224 << static_cast<const char *>
225 (fontID < 0 ? " FAIL" : " OK"));
228 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
229 for (int i2 = 0; i2 < 2; ++i2)
230 for (int i3 = 0; i3 < 4; ++i3)
231 for (int i4 = 0; i4 < 10; ++i4)
232 fontinfo_[i1][i2][i3][i4] = 0;
236 void FontLoader::update()
238 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) {
239 for (int i2 = 0; i2 < 2; ++i2)
240 for (int i3 = 0; i3 < 4; ++i3)
241 for (int i4 = 0; i4 < 10; ++i4) {
242 delete fontinfo_[i1][i2][i3][i4];
243 fontinfo_[i1][i2][i3][i4] = 0;
249 FontLoader::~FontLoader()
254 /////////////////////////////////////////////////
257 static QString makeFontName(QString const & family, QString const & foundry)
259 QString res = family;
260 if (!foundry.isEmpty())
261 res += " [" + foundry + ']';
266 GuiFontInfo::GuiFontInfo(FontInfo const & f)
269 font.setKerning(false);
270 QString const pat = symbolFamily(f.family());
271 if (!pat.isEmpty()) {
273 font = symbolFont(pat, &ok);
275 switch (f.family()) {
277 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
278 toqstr(lyxrc.roman_font_foundry));
279 font.setFamily(family);
281 #if QT_VERSION >= 0x040300
282 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
283 // It is reported to Trolltech at 02/06/07 against 4.3 final.
284 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
285 if (family == "Times" && !font.exactMatch())
286 font.setFamily("Times New Roman");
292 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
293 toqstr(lyxrc.sans_font_foundry)));
295 case TYPEWRITER_FAMILY:
296 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
297 toqstr(lyxrc.typewriter_font_foundry)));
304 switch (f.series()) {
306 font.setWeight(QFont::Normal);
309 font.setWeight(QFont::Bold);
315 switch (f.realShape()) {
318 font.setItalic(true);
324 LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
325 << "' matched by\n" << fromqstr(font.family()));
327 // Is this an exact match?
328 if (font.exactMatch())
329 LYXERR(Debug::FONT, "This font is an exact match");
331 LYXERR(Debug::FONT, "This font is NOT an exact match");
333 LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
335 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
336 * lyxrc.zoom / 100.0);
338 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
340 if (f.realShape() != SMALLCAPS_SHAPE) {
341 metrics = GuiFontMetrics(font);
343 // handle small caps ourselves ...
344 FontInfo smallfont = f;
345 smallfont.decSize().decSize().setShape(UP_SHAPE);
347 font2.setKerning(false);
348 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
349 * lyxrc.zoom / 100.0);
351 metrics = GuiFontMetrics(font, font2);
356 bool FontLoader::available(FontInfo const & f)
358 static vector<int> cache_set(NUM_FAMILIES, false);
359 static vector<int> cache(NUM_FAMILIES, false);
361 FontFamily family = f.family();
362 if (cache_set[family])
363 return cache[family];
364 cache_set[family] = true;
366 QString const pat = symbolFamily(family);
368 // We don't care about non-symbol fonts
372 symbolFont(pat, &ok);
376 cache[family] = true;
380 FontMetrics const & FontLoader::metrics(FontInfo const & f)
382 return fontinfo(f).metrics;
385 /// Get the QFont for this FontInfo
386 QFont const & getFont(FontInfo const & f)
388 return fontinfo(f).font;
391 /// Get the QFont for this FontInfo
392 GuiFontInfo const & getFontInfo(FontInfo const & f)
397 } // namespace frontend