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 4.1 returns "Multi" for all ? xft fonts
145 if (font.rawName() == "xft" || font.rawName() == "Multi") {
146 if (contains(fromqstr(fi.family()), family)) {
147 lyxerr[Debug::FONT] << " got it (Xft) ";
156 pair<QFont, bool> const getSymbolFont(string const & family)
158 lyxerr[Debug::FONT] << "Looking for font family "
159 << family << " ... ";
160 string upper = family;
161 upper[0] = toupper(family[0]);
164 font.setKerning(false);
165 font.setFamily(toqstr(family));
167 if (isChosenFont(font, family)) {
168 lyxerr[Debug::FONT] << "normal!" << endl;
169 return make_pair<QFont, bool>(font, true);
172 font.setFamily(toqstr(upper));
174 if (isChosenFont(font, upper)) {
175 lyxerr[Debug::FONT] << "upper!" << endl;
176 return make_pair<QFont, bool>(font, true);
179 // A simple setFamily() fails on Qt 2
181 font.setRawName(toqstr(getRawName(family)));
183 if (isChosenFont(font, family)) {
184 lyxerr[Debug::FONT] << "raw version!" << endl;
185 return make_pair<QFont, bool>(font, true);
188 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
189 return make_pair<QFont, bool>(font, false);
195 GuiFontLoader::GuiFontLoader()
197 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
198 for (int i2 = 0; i2 < 2; ++i2)
199 for (int i3 = 0; i3 < 4; ++i3)
200 for (int i4 = 0; i4 < 10; ++i4)
201 fontinfo_[i1][i2][i3][i4] = 0;
205 void GuiFontLoader::update()
207 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
208 for (int i2 = 0; i2 < 2; ++i2)
209 for (int i3 = 0; i3 < 4; ++i3)
210 for (int i4 = 0; i4 < 10; ++i4) {
211 delete fontinfo_[i1][i2][i3][i4];
212 fontinfo_[i1][i2][i3][i4] = 0;
217 /////////////////////////////////////////////////
220 QLFontInfo::QLFontInfo(LyXFont const & f)
222 font.setKerning(false);
223 string const pat = symbolFamily(f.family());
226 boost::tie(font, tmp) = getSymbolFont(pat);
228 switch (f.family()) {
229 case LyXFont::ROMAN_FAMILY:
230 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
231 lyxrc.roman_font_foundry)));
233 case LyXFont::SANS_FAMILY:
234 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
235 lyxrc.sans_font_foundry)));
237 case LyXFont::TYPEWRITER_FAMILY:
238 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
239 lyxrc.typewriter_font_foundry)));
246 switch (f.series()) {
247 case LyXFont::MEDIUM_SERIES:
248 font.setWeight(QFont::Normal);
250 case LyXFont::BOLD_SERIES:
251 font.setWeight(QFont::Bold);
257 switch (f.realShape()) {
258 case LyXFont::ITALIC_SHAPE:
259 case LyXFont::SLANTED_SHAPE:
260 font.setItalic(true);
266 if (lyxerr.debugging(Debug::FONT)) {
267 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
268 << "' matched by\n" << fromqstr(font.rawName()) << endl;
271 // Is this an exact match?
272 if (font.exactMatch())
273 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
275 lyxerr[Debug::FONT] << "This font is NOT an exact match"
278 lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
280 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
281 * lyxrc.zoom / 100.0);
283 lyxerr[Debug::FONT] << "The font has size: "
284 << font.pointSizeF() << endl;
286 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
287 metrics.reset(new GuiFontMetrics(font));
290 // handle small caps ourselves ...
291 LyXFont smallfont = f;
292 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
294 font2.setKerning(false);
295 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
296 * lyxrc.zoom / 100.0);
298 metrics.reset(new GuiFontMetrics(font, font2));
304 bool GuiFontLoader::available(LyXFont const & f)
306 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
307 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
309 LyXFont::FONT_FAMILY family = f.family();
310 if (cache_set[family])
311 return cache[family];
312 cache_set[family] = true;
314 string const pat = symbolFamily(family);
316 // We don't care about non-symbol fonts
319 pair<QFont, bool> tmp = getSymbolFont(pat);
323 cache[family] = true;
327 } // namespace frontend