2 * \file GuiFontLoader.C
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>
35 using lyx::support::contains;
48 GuiFontLoader::~GuiFontLoader() {
54 LyXFont::FONT_FAMILY lyx_family;
59 symbol_font symbol_fonts[] = {
60 { LyXFont::SYMBOL_FAMILY,
62 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
64 { LyXFont::CMR_FAMILY,
66 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
68 { LyXFont::CMSY_FAMILY,
70 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
72 { LyXFont::CMM_FAMILY,
74 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
76 { LyXFont::CMEX_FAMILY,
78 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
80 { LyXFont::MSA_FAMILY,
82 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
84 { LyXFont::MSB_FAMILY,
86 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
88 { LyXFont::EUFRAK_FAMILY,
90 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
92 { LyXFont::WASY_FAMILY,
94 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
96 { LyXFont::ESINT_FAMILY,
98 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
101 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
104 string getRawName(string const & family)
106 for (size_t i = 0; i < nr_symbol_fonts; ++i)
107 if (family == symbol_fonts[i].family)
108 return symbol_fonts[i].xlfd;
110 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
115 string const symbolFamily(LyXFont::FONT_FAMILY family)
117 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
118 if (family == symbol_fonts[i].lyx_family)
119 return symbol_fonts[i].family;
125 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
127 return family >= LyXFont::SYMBOL_FAMILY &&
128 family <= LyXFont::ESINT_FAMILY;
132 bool isChosenFont(QFont & font, string const & family)
134 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
137 // QFontInfo won't find a font that has only a few glyphs at unusual
138 // positions, e.g. the original esint10 font.
139 // The workaround is to add dummy glyphs at least at all ASCII
142 // Note Qt lies about family quite often
143 lyxerr[Debug::FONT] << "alleged fi family: "
144 << fromqstr(fi.family()) << endl;
146 // So we check rawName first
147 if (contains(fromqstr(font.rawName()), family)) {
148 lyxerr[Debug::FONT] << " got it ";
152 // Qt 4.1 returns "Multi" for all ? xft fonts
153 if (font.rawName() == "xft" || font.rawName() == "Multi") {
154 if (contains(fromqstr(fi.family()), family)) {
155 lyxerr[Debug::FONT] << " got it (Xft) ";
164 pair<QFont, bool> const getSymbolFont(string const & family)
166 lyxerr[Debug::FONT] << "Looking for font family "
167 << family << " ... ";
168 string upper = family;
169 upper[0] = toupper(family[0]);
172 font.setKerning(false);
173 font.setFamily(toqstr(family));
175 if (isChosenFont(font, family)) {
176 lyxerr[Debug::FONT] << "normal!" << endl;
177 return make_pair<QFont, bool>(font, true);
180 font.setFamily(toqstr(upper));
182 if (isChosenFont(font, upper)) {
183 lyxerr[Debug::FONT] << "upper!" << endl;
184 return make_pair<QFont, bool>(font, true);
187 // A simple setFamily() fails on Qt 2
189 font.setRawName(toqstr(getRawName(family)));
191 if (isChosenFont(font, family)) {
192 lyxerr[Debug::FONT] << "raw version!" << endl;
193 return make_pair<QFont, bool>(font, true);
196 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
197 return make_pair<QFont, bool>(font, false);
203 GuiFontLoader::GuiFontLoader()
205 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
206 for (int i2 = 0; i2 < 2; ++i2)
207 for (int i3 = 0; i3 < 4; ++i3)
208 for (int i4 = 0; i4 < 10; ++i4)
209 fontinfo_[i1][i2][i3][i4] = 0;
213 void GuiFontLoader::update()
215 for (int i1 = 0; i1 < LyXFont::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 delete fontinfo_[i1][i2][i3][i4];
220 fontinfo_[i1][i2][i3][i4] = 0;
225 /////////////////////////////////////////////////
228 QLFontInfo::QLFontInfo(LyXFont const & f)
230 font.setKerning(false);
231 string const pat = symbolFamily(f.family());
234 boost::tie(font, tmp) = getSymbolFont(pat);
236 switch (f.family()) {
237 case LyXFont::ROMAN_FAMILY:
238 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
239 lyxrc.roman_font_foundry)));
241 case LyXFont::SANS_FAMILY:
242 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
243 lyxrc.sans_font_foundry)));
245 case LyXFont::TYPEWRITER_FAMILY:
246 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
247 lyxrc.typewriter_font_foundry)));
254 switch (f.series()) {
255 case LyXFont::MEDIUM_SERIES:
256 font.setWeight(QFont::Normal);
258 case LyXFont::BOLD_SERIES:
259 font.setWeight(QFont::Bold);
265 switch (f.realShape()) {
266 case LyXFont::ITALIC_SHAPE:
267 case LyXFont::SLANTED_SHAPE:
268 font.setItalic(true);
274 if (lyxerr.debugging(Debug::FONT)) {
275 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
276 << "' matched by\n" << fromqstr(font.rawName()) << endl;
279 // Is this an exact match?
280 if (font.exactMatch())
281 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
283 lyxerr[Debug::FONT] << "This font is NOT an exact match"
286 lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
288 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
289 * lyxrc.zoom / 100.0);
291 lyxerr[Debug::FONT] << "The font has size: "
292 << font.pointSizeF() << endl;
294 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
295 metrics.reset(new GuiFontMetrics(font));
298 // handle small caps ourselves ...
299 LyXFont smallfont = f;
300 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
302 font2.setKerning(false);
303 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
304 * lyxrc.zoom / 100.0);
306 metrics.reset(new GuiFontMetrics(font, font2));
312 bool GuiFontLoader::available(LyXFont const & f)
314 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
315 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
317 LyXFont::FONT_FAMILY family = f.family();
318 if (cache_set[family])
319 return cache[family];
320 cache_set[family] = true;
322 string const pat = symbolFamily(family);
324 // We don't care about non-symbol fonts
327 pair<QFont, bool> tmp = getSymbolFont(pat);
331 cache[family] = true;
335 } // namespace frontend