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>
31 //#include <X11/Xlib.h>
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 // QFontInfo won't find a font that has only a few glyphs at unusual
135 // positions, e.g. the original esint10 font.
136 // The workaround is to add dummy glyphs at least at all ASCII
140 lyxerr[Debug::FONT] << "got: " << fromqstr(fi.family()) << endl;
142 if (contains(fromqstr(fi.family()), family)) {
143 lyxerr[Debug::FONT] << " got it ";
151 pair<QFont, bool> const getSymbolFont(string const & family)
153 lyxerr[Debug::FONT] << "Looking for font family "
154 << family << " ... ";
155 string upper = family;
156 upper[0] = toupper(family[0]);
159 font.setKerning(false);
160 font.setFamily(toqstr(family));
162 if (isChosenFont(font, family)) {
163 lyxerr[Debug::FONT] << "normal!" << endl;
164 return make_pair<QFont, bool>(font, true);
167 lyxerr[Debug::FONT] << "Trying " << upper << " ... ";
168 font.setFamily(toqstr(upper));
170 if (isChosenFont(font, upper)) {
171 lyxerr[Debug::FONT] << "upper!" << endl;
172 return make_pair<QFont, bool>(font, true);
175 // A simple setFamily() fails on Qt 2
177 string const rawName = getRawName(family);
178 lyxerr[Debug::FONT] << "Trying " << rawName << " ... ";
179 font.setRawName(toqstr(rawName));
181 if (isChosenFont(font, family)) {
182 lyxerr[Debug::FONT] << "raw version!" << endl;
183 return make_pair<QFont, bool>(font, true);
186 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
187 return make_pair<QFont, bool>(font, false);
193 GuiFontLoader::GuiFontLoader()
195 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
196 for (int i2 = 0; i2 < 2; ++i2)
197 for (int i3 = 0; i3 < 4; ++i3)
198 for (int i4 = 0; i4 < 10; ++i4)
199 fontinfo_[i1][i2][i3][i4] = 0;
203 void GuiFontLoader::update()
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 delete fontinfo_[i1][i2][i3][i4];
210 fontinfo_[i1][i2][i3][i4] = 0;
215 /////////////////////////////////////////////////
218 QLFontInfo::QLFontInfo(LyXFont const & f)
220 font.setKerning(false);
221 string const pat = symbolFamily(f.family());
224 boost::tie(font, tmp) = getSymbolFont(pat);
226 switch (f.family()) {
227 case LyXFont::ROMAN_FAMILY:
228 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
229 lyxrc.roman_font_foundry)));
231 case LyXFont::SANS_FAMILY:
232 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
233 lyxrc.sans_font_foundry)));
235 case LyXFont::TYPEWRITER_FAMILY:
236 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
237 lyxrc.typewriter_font_foundry)));
244 switch (f.series()) {
245 case LyXFont::MEDIUM_SERIES:
246 font.setWeight(QFont::Normal);
248 case LyXFont::BOLD_SERIES:
249 font.setWeight(QFont::Bold);
255 switch (f.realShape()) {
256 case LyXFont::ITALIC_SHAPE:
257 case LyXFont::SLANTED_SHAPE:
258 font.setItalic(true);
264 if (lyxerr.debugging(Debug::FONT)) {
265 lyxerr[Debug::FONT] << "Font '" << to_utf8(f.stateText(0))
266 << "' matched by\n" << fromqstr(font.family()) << endl;
269 // Is this an exact match?
270 if (font.exactMatch())
271 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
273 lyxerr[Debug::FONT] << "This font is NOT an exact match"
276 lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
278 font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
279 * lyxrc.zoom / 100.0);
281 lyxerr[Debug::FONT] << "The font has size: "
282 << font.pointSizeF() << endl;
284 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
285 metrics.reset(new GuiFontMetrics(font));
288 // handle small caps ourselves ...
289 LyXFont smallfont = f;
290 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
292 font2.setKerning(false);
293 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
294 * lyxrc.zoom / 100.0);
296 metrics.reset(new GuiFontMetrics(font, font2));
302 bool GuiFontLoader::available(LyXFont const & f)
304 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
305 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
307 LyXFont::FONT_FAMILY family = f.family();
308 if (cache_set[family])
309 return cache[family];
310 cache_set[family] = true;
312 string const pat = symbolFamily(family);
314 // We don't care about non-symbol fonts
317 pair<QFont, bool> tmp = getSymbolFont(pat);
321 cache[family] = true;
325 } // namespace frontend