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 LyXFont::FONT_FAMILY lyx_family;
56 symbol_font symbol_fonts[] = {
57 { LyXFont::SYMBOL_FAMILY,
59 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
61 { LyXFont::CMR_FAMILY,
63 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
65 { LyXFont::CMSY_FAMILY,
67 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
69 { LyXFont::CMM_FAMILY,
71 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
73 { LyXFont::CMEX_FAMILY,
75 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
77 { LyXFont::MSA_FAMILY,
79 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
81 { LyXFont::MSB_FAMILY,
83 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
85 { LyXFont::EUFRAK_FAMILY,
87 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
89 { LyXFont::WASY_FAMILY,
91 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
93 { LyXFont::ESINT_FAMILY,
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(LyXFont::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(LyXFont::FONT_FAMILY family)
124 return family >= LyXFont::SYMBOL_FAMILY &&
125 family <= LyXFont::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 < LyXFont::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 < LyXFont::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(LyXFont 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 LyXFont::ROMAN_FAMILY:
225 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
226 lyxrc.roman_font_foundry)));
228 case LyXFont::SANS_FAMILY:
229 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
230 lyxrc.sans_font_foundry)));
232 case LyXFont::TYPEWRITER_FAMILY:
233 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
234 lyxrc.typewriter_font_foundry)));
241 switch (f.series()) {
242 case LyXFont::MEDIUM_SERIES:
243 font.setWeight(QFont::Normal);
245 case LyXFont::BOLD_SERIES:
246 font.setWeight(QFont::Bold);
252 switch (f.realShape()) {
253 case LyXFont::ITALIC_SHAPE:
254 case LyXFont::SLANTED_SHAPE:
255 font.setItalic(true);
261 LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
262 << "' matched by\n" << fromqstr(font.family()) << endl;
264 // Is this an exact match?
265 if (font.exactMatch())
266 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
268 LYXERR(Debug::FONT) << "This font is NOT an exact match"
271 LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
273 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
274 * lyxrc.zoom / 100.0);
276 LYXERR(Debug::FONT) << "The font has size: "
277 << font.pointSizeF() << endl;
279 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
280 metrics.reset(new GuiFontMetrics(font));
283 // handle small caps ourselves ...
284 LyXFont smallfont = f;
285 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
287 font2.setKerning(false);
288 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
289 * lyxrc.zoom / 100.0);
291 metrics.reset(new GuiFontMetrics(font, font2));
297 bool GuiFontLoader::available(LyXFont const & f)
299 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
300 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
302 LyXFont::FONT_FAMILY family = f.family();
303 if (cache_set[family])
304 return cache[family];
305 cache_set[family] = true;
307 string const pat = symbolFamily(family);
309 // We don't care about non-symbol fonts
312 pair<QFont, bool> tmp = getSymbolFont(pat);
316 cache[family] = true;
320 } // namespace frontend