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 "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;
50 GuiFontLoader::~GuiFontLoader() {
56 LyXFont::FONT_FAMILY lyx_family;
61 symbol_font symbol_fonts[] = {
62 { LyXFont::SYMBOL_FAMILY,
64 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
66 { LyXFont::CMR_FAMILY,
68 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
70 { LyXFont::CMSY_FAMILY,
72 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
74 { LyXFont::CMM_FAMILY,
76 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
78 { LyXFont::CMEX_FAMILY,
80 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
82 { LyXFont::MSA_FAMILY,
84 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
86 { LyXFont::MSB_FAMILY,
88 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
90 { LyXFont::EUFRAK_FAMILY,
92 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
94 { LyXFont::WASY_FAMILY,
96 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
99 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
102 string getRawName(string const & family)
104 for (size_t i = 0; i < nr_symbol_fonts; ++i)
105 if (family == symbol_fonts[i].family)
106 return symbol_fonts[i].xlfd;
108 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
113 string const symbolFamily(LyXFont::FONT_FAMILY family)
115 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
116 if (family == symbol_fonts[i].lyx_family)
117 return symbol_fonts[i].family;
123 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
125 return family >= LyXFont::SYMBOL_FAMILY &&
126 family <= LyXFont::WASY_FAMILY;
130 bool isChosenFont(QFont & font, string const & family)
132 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
136 // Note Qt lies about family quite often
137 lyxerr[Debug::FONT] << "alleged fi family: "
138 << fromqstr(fi.family()) << endl;
140 // So we check rawName first
141 if (contains(fromqstr(font.rawName()), family)) {
142 lyxerr[Debug::FONT] << " got it ";
146 // Qt 3.2 beta1 returns "xft" for all xft fonts
147 // Qt 4.1 returns "Multi" for all ? xft fonts
148 if (font.rawName() == "xft" || font.rawName() == "Multi") {
149 if (contains(fromqstr(fi.family()), family)) {
150 lyxerr[Debug::FONT] << " got it (Xft) ";
159 pair<QFont, bool> const getSymbolFont(string const & family)
161 lyxerr[Debug::FONT] << "Looking for font family "
162 << family << " ... ";
163 string upper = family;
164 upper[0] = toupper(family[0]);
167 font.setFamily(toqstr(family));
169 if (isChosenFont(font, family)) {
170 lyxerr[Debug::FONT] << "normal!" << endl;
171 return make_pair<QFont, bool>(font, true);
174 font.setFamily(toqstr(upper));
176 if (isChosenFont(font, upper)) {
177 lyxerr[Debug::FONT] << "upper!" << endl;
178 return make_pair<QFont, bool>(font, true);
181 // A simple setFamily() fails on Qt 2
183 font.setRawName(toqstr(getRawName(family)));
185 if (isChosenFont(font, family)) {
186 lyxerr[Debug::FONT] << "raw version!" << endl;
187 return make_pair<QFont, bool>(font, true);
190 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
191 return make_pair<QFont, bool>(font, false);
197 GuiFontLoader::GuiFontLoader()
199 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
200 for (int i2 = 0; i2 < 2; ++i2)
201 for (int i3 = 0; i3 < 4; ++i3)
202 for (int i4 = 0; i4 < 10; ++i4)
203 fontinfo_[i1][i2][i3][i4] = 0;
207 void GuiFontLoader::update()
209 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
210 for (int i2 = 0; i2 < 2; ++i2)
211 for (int i3 = 0; i3 < 4; ++i3)
212 for (int i4 = 0; i4 < 10; ++i4) {
213 delete fontinfo_[i1][i2][i3][i4];
214 fontinfo_[i1][i2][i3][i4] = 0;
219 /////////////////////////////////////////////////
222 QLFontInfo::QLFontInfo(LyXFont const & f)
226 string const pat = symbolFamily(f.family());
229 boost::tie(font, tmp) = getSymbolFont(pat);
231 switch (f.family()) {
232 case LyXFont::ROMAN_FAMILY:
233 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
234 lyxrc.roman_font_foundry)));
236 case LyXFont::SANS_FAMILY:
237 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
238 lyxrc.sans_font_foundry)));
240 case LyXFont::TYPEWRITER_FAMILY:
241 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
242 lyxrc.typewriter_font_foundry)));
249 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
250 * lyxrc.zoom / 100.0);
252 switch (f.series()) {
253 case LyXFont::MEDIUM_SERIES:
254 font.setWeight(QFont::Normal);
256 case LyXFont::BOLD_SERIES:
257 font.setWeight(QFont::Bold);
263 switch (f.realShape()) {
264 case LyXFont::ITALIC_SHAPE:
265 case LyXFont::SLANTED_SHAPE:
266 font.setItalic(true);
272 if (lyxerr.debugging(Debug::FONT)) {
273 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
274 << "' matched by\n" << fromqstr(font.rawName()) << endl;
277 lyxerr[Debug::FONT] << "The font has size: "
278 << font.pointSizeF() << endl;
280 // Is this an exact match?
281 if (font.exactMatch())
282 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
284 lyxerr[Debug::FONT] << "This font is NOT an exact match"
287 lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
289 metrics = QFontMetrics(font);
292 #ifdef USE_LYX_FONTCACHE
293 int QLFontInfo::width(Uchar val)
295 QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
296 if (cit != widthcache.end())
299 int const w = metrics.width(QChar(val));
306 bool GuiFontLoader::available(LyXFont const & f)
308 if (!lyx_gui::use_gui)
311 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
312 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
314 LyXFont::FONT_FAMILY family = f.family();
315 if (cache_set[family])
316 return cache[family];
317 cache_set[family] = true;
319 string const pat = symbolFamily(family);
321 // We don't care about non-symbol fonts
324 pair<QFont, bool> tmp = getSymbolFont(pat);
328 cache[family] = true;
332 } // namespace frontend