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-*-*-*-*-*-*-*-*-*-*-*" }
97 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
100 string getRawName(string const & family)
102 for (size_t i = 0; i < nr_symbol_fonts; ++i)
103 if (family == symbol_fonts[i].family)
104 return symbol_fonts[i].xlfd;
106 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
111 string const symbolFamily(LyXFont::FONT_FAMILY family)
113 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
114 if (family == symbol_fonts[i].lyx_family)
115 return symbol_fonts[i].family;
121 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
123 return family >= LyXFont::SYMBOL_FAMILY &&
124 family <= LyXFont::WASY_FAMILY;
128 bool isChosenFont(QFont & font, string const & family)
130 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
134 // Note Qt lies about family quite often
135 lyxerr[Debug::FONT] << "alleged fi family: "
136 << fromqstr(fi.family()) << endl;
138 // So we check rawName first
139 if (contains(fromqstr(font.rawName()), family)) {
140 lyxerr[Debug::FONT] << " got it ";
144 // Qt 3.2 beta1 returns "xft" for all xft fonts
145 // Qt 4.1 returns "Multi" for all ? xft fonts
146 if (font.rawName() == "xft" || font.rawName() == "Multi") {
147 if (contains(fromqstr(fi.family()), family)) {
148 lyxerr[Debug::FONT] << " got it (Xft) ";
157 pair<QFont, bool> const getSymbolFont(string const & family)
159 lyxerr[Debug::FONT] << "Looking for font family "
160 << family << " ... ";
161 string upper = family;
162 upper[0] = toupper(family[0]);
165 font.setKerning(false);
166 font.setFamily(toqstr(family));
168 if (isChosenFont(font, family)) {
169 lyxerr[Debug::FONT] << "normal!" << endl;
170 return make_pair<QFont, bool>(font, true);
173 font.setFamily(toqstr(upper));
175 if (isChosenFont(font, upper)) {
176 lyxerr[Debug::FONT] << "upper!" << endl;
177 return make_pair<QFont, bool>(font, true);
180 // A simple setFamily() fails on Qt 2
182 font.setRawName(toqstr(getRawName(family)));
184 if (isChosenFont(font, family)) {
185 lyxerr[Debug::FONT] << "raw version!" << endl;
186 return make_pair<QFont, bool>(font, true);
189 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
190 return make_pair<QFont, bool>(font, false);
196 GuiFontLoader::GuiFontLoader()
198 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
199 for (int i2 = 0; i2 < 2; ++i2)
200 for (int i3 = 0; i3 < 4; ++i3)
201 for (int i4 = 0; i4 < 10; ++i4)
202 fontinfo_[i1][i2][i3][i4] = 0;
206 void GuiFontLoader::update()
208 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
209 for (int i2 = 0; i2 < 2; ++i2)
210 for (int i3 = 0; i3 < 4; ++i3)
211 for (int i4 = 0; i4 < 10; ++i4) {
212 delete fontinfo_[i1][i2][i3][i4];
213 fontinfo_[i1][i2][i3][i4] = 0;
218 /////////////////////////////////////////////////
221 QLFontInfo::QLFontInfo(LyXFont const & f)
223 font.setKerning(false);
224 string const pat = symbolFamily(f.family());
227 boost::tie(font, tmp) = getSymbolFont(pat);
229 switch (f.family()) {
230 case LyXFont::ROMAN_FAMILY:
231 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
232 lyxrc.roman_font_foundry)));
234 case LyXFont::SANS_FAMILY:
235 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
236 lyxrc.sans_font_foundry)));
238 case LyXFont::TYPEWRITER_FAMILY:
239 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
240 lyxrc.typewriter_font_foundry)));
247 switch (f.series()) {
248 case LyXFont::MEDIUM_SERIES:
249 font.setWeight(QFont::Normal);
251 case LyXFont::BOLD_SERIES:
252 font.setWeight(QFont::Bold);
258 switch (f.realShape()) {
259 case LyXFont::ITALIC_SHAPE:
260 case LyXFont::SLANTED_SHAPE:
261 font.setItalic(true);
267 if (lyxerr.debugging(Debug::FONT)) {
268 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
269 << "' matched by\n" << fromqstr(font.rawName()) << endl;
272 // Is this an exact match?
273 if (font.exactMatch())
274 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
276 lyxerr[Debug::FONT] << "This font is NOT an exact match"
279 lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
281 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
282 * lyxrc.zoom / 100.0);
284 lyxerr[Debug::FONT] << "The font has size: "
285 << font.pointSizeF() << endl;
287 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
288 metrics.reset(new GuiFontMetrics(font));
291 // handle small caps ourselves ...
292 LyXFont smallfont = f;
293 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
295 font2.setKerning(false);
296 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
297 * lyxrc.zoom / 100.0);
299 metrics.reset(new GuiFontMetrics(font, font2));
305 bool GuiFontLoader::available(LyXFont const & f)
307 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
308 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
310 LyXFont::FONT_FAMILY family = f.family();
311 if (cache_set[family])
312 return cache[family];
313 cache_set[family] = true;
315 string const pat = symbolFamily(family);
317 // We don't care about non-symbol fonts
320 pair<QFont, bool> tmp = getSymbolFont(pat);
324 cache[family] = true;
328 } // namespace frontend