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;
212 /////////////////////////////////////////////////
215 QLFontInfo::QLFontInfo(Font const & f)
217 font.setKerning(false);
218 string const pat = symbolFamily(f.family());
221 boost::tie(font, tmp) = getSymbolFont(pat);
223 switch (f.family()) {
224 case Font::ROMAN_FAMILY: {
225 QString family = toqstr(makeFontName(lyxrc.roman_font_name,
226 lyxrc.roman_font_foundry));
227 font.setFamily(family);
229 #if QT_VERSION >= 0x040300
230 // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
231 // It is reported to Trolltech at 02/06/07 against 4.3 final.
232 // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
233 if (family == "Times" && !font.exactMatch())
234 font.setFamily(QString::fromLatin1("Times New Roman"));
239 case Font::SANS_FAMILY:
240 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
241 lyxrc.sans_font_foundry)));
243 case Font::TYPEWRITER_FAMILY:
244 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
245 lyxrc.typewriter_font_foundry)));
252 switch (f.series()) {
253 case Font::MEDIUM_SERIES:
254 font.setWeight(QFont::Normal);
256 case Font::BOLD_SERIES:
257 font.setWeight(QFont::Bold);
263 switch (f.realShape()) {
264 case Font::ITALIC_SHAPE:
265 case Font::SLANTED_SHAPE:
266 font.setItalic(true);
272 LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
273 << "' matched by\n" << fromqstr(font.family()) << endl;
275 // Is this an exact match?
276 if (font.exactMatch())
277 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
279 LYXERR(Debug::FONT) << "This font is NOT an exact match"
282 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
284 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
285 * lyxrc.zoom / 100.0);
287 LYXERR(Debug::FONT) << "The font has size: "
288 << font.pointSizeF() << endl;
290 if (f.realShape() != Font::SMALLCAPS_SHAPE) {
291 metrics.reset(new GuiFontMetrics(font));
294 // handle small caps ourselves ...
296 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
298 font2.setKerning(false);
299 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
300 * lyxrc.zoom / 100.0);
302 metrics.reset(new GuiFontMetrics(font, font2));
308 bool GuiFontLoader::available(Font const & f)
310 static vector<int> cache_set(Font::NUM_FAMILIES, false);
311 static vector<int> cache(Font::NUM_FAMILIES, false);
313 Font::FONT_FAMILY family = f.family();
314 if (cache_set[family])
315 return cache[family];
316 cache_set[family] = true;
318 string const pat = symbolFamily(family);
320 // We don't care about non-symbol fonts
323 pair<QFont, bool> tmp = getSymbolFont(pat);
327 cache[family] = true;
331 } // namespace frontend