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"
25 #include <qfontinfo.h>
27 #include <boost/tuple/tuple.hpp>
31 //#include <X11/Xlib.h>
35 using lyx::support::contains;
51 Font::FONT_FAMILY lyx_family;
56 symbol_font symbol_fonts[] = {
57 { Font::SYMBOL_FAMILY,
59 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
63 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
67 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
71 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
85 { Font::EUFRAK_FAMILY,
87 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
91 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
95 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
98 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
101 string getRawName(string const & family)
103 for (size_t i = 0; i < nr_symbol_fonts; ++i)
104 if (family == symbol_fonts[i].family)
105 return symbol_fonts[i].xlfd;
107 LYXERR(Debug::FONT) << "BUG: family not found !" << endl;
112 string const symbolFamily(Font::FONT_FAMILY family)
114 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
115 if (family == symbol_fonts[i].lyx_family)
116 return symbol_fonts[i].family;
122 bool isSymbolFamily(Font::FONT_FAMILY family)
124 return family >= Font::SYMBOL_FAMILY &&
125 family <= Font::ESINT_FAMILY;
129 bool isChosenFont(QFont & font, string const & family)
131 // QFontInfo won't find a font that has only a few glyphs at unusual
132 // positions, e.g. the original esint10 font.
133 // The workaround is to add dummy glyphs at least at all ASCII
137 LYXERR(Debug::FONT) << "got: " << fromqstr(fi.family()) << endl;
139 if (contains(fromqstr(fi.family()), family)) {
140 LYXERR(Debug::FONT) << " got it ";
148 pair<QFont, bool> const getSymbolFont(string const & family)
150 LYXERR(Debug::FONT) << "Looking for font family "
151 << family << " ... ";
152 string upper = family;
153 upper[0] = toupper(family[0]);
156 font.setKerning(false);
157 font.setFamily(toqstr(family));
159 if (isChosenFont(font, family)) {
160 LYXERR(Debug::FONT) << "normal!" << endl;
161 return make_pair<QFont, bool>(font, true);
164 LYXERR(Debug::FONT) << "Trying " << upper << " ... ";
165 font.setFamily(toqstr(upper));
167 if (isChosenFont(font, upper)) {
168 LYXERR(Debug::FONT) << "upper!" << endl;
169 return make_pair<QFont, bool>(font, true);
172 // A simple setFamily() fails on Qt 2
174 string const rawName = getRawName(family);
175 LYXERR(Debug::FONT) << "Trying " << rawName << " ... ";
176 font.setRawName(toqstr(rawName));
178 if (isChosenFont(font, family)) {
179 LYXERR(Debug::FONT) << "raw version!" << endl;
180 return make_pair<QFont, bool>(font, true);
183 LYXERR(Debug::FONT) << " FAILED :-(" << endl;
184 return make_pair<QFont, bool>(font, false);
190 GuiFontLoader::GuiFontLoader()
192 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1)
193 for (int i2 = 0; i2 < 2; ++i2)
194 for (int i3 = 0; i3 < 4; ++i3)
195 for (int i4 = 0; i4 < 10; ++i4)
196 fontinfo_[i1][i2][i3][i4] = 0;
200 void GuiFontLoader::update()
202 for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
203 for (int i2 = 0; i2 < 2; ++i2)
204 for (int i3 = 0; i3 < 4; ++i3)
205 for (int i4 = 0; i4 < 10; ++i4) {
206 delete fontinfo_[i1][i2][i3][i4];
207 fontinfo_[i1][i2][i3][i4] = 0;
213 /////////////////////////////////////////////////
216 QLFontInfo::QLFontInfo(Font const & f)
218 font.setKerning(false);
219 string const pat = symbolFamily(f.family());
222 boost::tie(font, tmp) = getSymbolFont(pat);
224 switch (f.family()) {
225 case Font::ROMAN_FAMILY: {
226 QString family = toqstr(makeFontName(lyxrc.roman_font_name,
227 lyxrc.roman_font_foundry));
228 font.setFamily(family);
230 #if QT_VERSION >= 0x040300
231 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
232 // It is reported to Trolltech at 02/06/07 against 4.3 final.
233 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
234 if (family == "Times" && !font.exactMatch())
235 font.setFamily(QString::fromLatin1("Times New Roman"));
240 case Font::SANS_FAMILY:
241 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
242 lyxrc.sans_font_foundry)));
244 case Font::TYPEWRITER_FAMILY:
245 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
246 lyxrc.typewriter_font_foundry)));
253 switch (f.series()) {
254 case Font::MEDIUM_SERIES:
255 font.setWeight(QFont::Normal);
257 case Font::BOLD_SERIES:
258 font.setWeight(QFont::Bold);
264 switch (f.realShape()) {
265 case Font::ITALIC_SHAPE:
266 case Font::SLANTED_SHAPE:
267 font.setItalic(true);
273 LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
274 << "' matched by\n" << fromqstr(font.family()) << endl;
276 // Is this an exact match?
277 if (font.exactMatch())
278 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
280 LYXERR(Debug::FONT) << "This font is NOT an exact match"
283 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
285 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
286 * lyxrc.zoom / 100.0);
288 LYXERR(Debug::FONT) << "The font has size: "
289 << font.pointSizeF() << endl;
291 if (f.realShape() != Font::SMALLCAPS_SHAPE) {
292 metrics.reset(new GuiFontMetrics(font));
295 // handle small caps ourselves ...
297 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
299 font2.setKerning(false);
300 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
301 * lyxrc.zoom / 100.0);
303 metrics.reset(new GuiFontMetrics(font, font2));
309 bool GuiFontLoader::available(Font const & f)
311 static vector<int> cache_set(Font::NUM_FAMILIES, false);
312 static vector<int> cache(Font::NUM_FAMILIES, false);
314 Font::FONT_FAMILY family = f.family();
315 if (cache_set[family])
316 return cache[family];
317 cache_set[family] = true;
319 string const pat = symbolFamily(family);
321 // We don't care about non-symbol fonts
324 pair<QFont, bool> tmp = getSymbolFont(pat);
328 cache[family] = true;
332 } // namespace frontend