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 "FontLoader.h"
15 #include "qt_helpers.h"
20 #include "frontends/lyx_gui.h"
22 #include "support/convert.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/systemcall.h"
27 #include <qfontinfo.h>
29 #include <boost/tuple/tuple.hpp>
37 using lyx::support::contains;
47 FontLoader::~FontLoader() {
53 LyXFont::FONT_FAMILY lyx_family;
58 symbol_font symbol_fonts[] = {
59 { LyXFont::SYMBOL_FAMILY,
61 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
63 { LyXFont::CMR_FAMILY,
65 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
67 { LyXFont::CMSY_FAMILY,
69 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
71 { LyXFont::CMM_FAMILY,
73 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 { LyXFont::CMEX_FAMILY,
77 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 { LyXFont::MSA_FAMILY,
81 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 { LyXFont::MSB_FAMILY,
85 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
87 { LyXFont::EUFRAK_FAMILY,
89 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
91 { LyXFont::WASY_FAMILY,
93 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
96 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
99 string getRawName(string const & family)
101 for (size_t i = 0; i < nr_symbol_fonts; ++i)
102 if (family == symbol_fonts[i].family)
103 return symbol_fonts[i].xlfd;
105 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
110 string const symbolFamily(LyXFont::FONT_FAMILY family)
112 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
113 if (family == symbol_fonts[i].lyx_family)
114 return symbol_fonts[i].family;
120 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
122 return family >= LyXFont::SYMBOL_FAMILY &&
123 family <= LyXFont::WASY_FAMILY;
127 bool isChosenFont(QFont & font, string const & family)
129 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
133 // Note Qt lies about family quite often
134 lyxerr[Debug::FONT] << "alleged fi family: "
135 << fromqstr(fi.family()) << endl;
137 // So we check rawName first
138 if (contains(fromqstr(font.rawName()), family)) {
139 lyxerr[Debug::FONT] << " got it ";
143 // Qt 3.2 beta1 returns "xft" for all xft fonts
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.setFamily(toqstr(family));
166 if (isChosenFont(font, family)) {
167 lyxerr[Debug::FONT] << "normal!" << endl;
168 return make_pair<QFont, bool>(font, true);
171 font.setFamily(toqstr(upper));
173 if (isChosenFont(font, upper)) {
174 lyxerr[Debug::FONT] << "upper!" << endl;
175 return make_pair<QFont, bool>(font, true);
178 // A simple setFamily() fails on Qt 2
180 font.setRawName(toqstr(getRawName(family)));
182 if (isChosenFont(font, family)) {
183 lyxerr[Debug::FONT] << "raw version!" << endl;
184 return make_pair<QFont, bool>(font, true);
187 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
188 return make_pair<QFont, bool>(font, false);
194 FontLoader::FontLoader()
196 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
197 for (int i2 = 0; i2 < 2; ++i2)
198 for (int i3 = 0; i3 < 4; ++i3)
199 for (int i4 = 0; i4 < 10; ++i4)
200 fontinfo_[i1][i2][i3][i4] = 0;
204 void FontLoader::update()
206 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
207 for (int i2 = 0; i2 < 2; ++i2)
208 for (int i3 = 0; i3 < 4; ++i3)
209 for (int i4 = 0; i4 < 10; ++i4) {
210 delete fontinfo_[i1][i2][i3][i4];
211 fontinfo_[i1][i2][i3][i4] = 0;
216 /////////////////////////////////////////////////
219 QLFontInfo::QLFontInfo(LyXFont const & f)
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 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
247 * lyxrc.zoom / 100.0);
249 switch (f.series()) {
250 case LyXFont::MEDIUM_SERIES:
251 font.setWeight(QFont::Normal);
253 case LyXFont::BOLD_SERIES:
254 font.setWeight(QFont::Bold);
260 switch (f.realShape()) {
261 case LyXFont::ITALIC_SHAPE:
262 case LyXFont::SLANTED_SHAPE:
263 font.setItalic(true);
269 if (lyxerr.debugging(Debug::FONT)) {
270 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
271 << "' matched by\n" << fromqstr(font.rawName()) << endl;
274 lyxerr[Debug::FONT] << "The font has size: "
275 << font.pointSizeF() << endl;
277 // Is this an exact match?
278 if (font.exactMatch())
279 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
281 lyxerr[Debug::FONT] << "This font is NOT an exact match"
284 lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
286 metrics = QFontMetrics(font);
290 int QLFontInfo::width(Uchar val)
292 // Starting with version 3.1.0, Qt/X11 does its own caching of
293 // character width, so it is not necessary to provide ours.
294 #if defined (USE_LYX_FONTCACHE)
295 QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
296 if (cit != widthcache.end())
299 int const w = metrics.width(QChar(val));
303 return metrics.width(QChar(val));
308 bool FontLoader::available(LyXFont const & f)
310 if (!lyx_gui::use_gui)
313 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
314 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
316 LyXFont::FONT_FAMILY family = f.family();
317 if (cache_set[family])
318 return cache[family];
319 cache_set[family] = true;
321 string const pat = symbolFamily(family);
323 // We don't care about non-symbol fonts
326 pair<QFont, bool> tmp = getSymbolFont(pat);
330 cache[family] = true;