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
15 #include "qfont_loader.h"
16 #include "qt_helpers.h"
19 #include "BufferView.h"
20 #include "qt_helpers.h"
23 #include <qfontmetrics.h>
24 #include <qfontdatabase.h>
25 #include <qstringlist.h>
26 #include "support/lstrings.h"
27 #include "frontends/lyx_gui.h"
29 #include <boost/tuple/tuple.hpp>
34 #include "support/systemcall.h"
35 #include "support/filetools.h"
38 using namespace lyx::support;
50 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
54 char ** p = XGetFontPath(w.x11Display(), &n);
55 if (std::find(p, p + n, dir) != p + n)
57 lyxerr[Debug::FONT] << "Adding " << dir
58 << " to the font path." << endl;
59 string const command = "xset fp+ " + dir;
61 if (!s.startscript(Systemcall::Wait, command))
63 lyxerr << "Unable to add " << dir << "to the font path."
71 LyXFont::FONT_FAMILY lyx_family;
76 symbol_font symbol_fonts[] = {
77 { LyXFont::SYMBOL_FAMILY,
79 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
81 { LyXFont::CMR_FAMILY,
83 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
85 { LyXFont::CMSY_FAMILY,
87 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
89 { LyXFont::CMM_FAMILY,
91 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
93 { LyXFont::CMEX_FAMILY,
95 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
97 { LyXFont::MSA_FAMILY,
99 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
101 { LyXFont::MSB_FAMILY,
103 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
105 { LyXFont::EUFRAK_FAMILY,
107 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
109 { LyXFont::WASY_FAMILY,
111 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
114 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
117 string getRawName(string const & family)
119 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
120 if (family == symbol_fonts[i].family)
121 return symbol_fonts[i].xlfd;
123 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
128 string const symbolFamily(LyXFont::FONT_FAMILY family)
130 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
131 if (family == symbol_fonts[i].lyx_family)
132 return symbol_fonts[i].family;
138 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
140 return family >= LyXFont::SYMBOL_FAMILY &&
141 family <= LyXFont::WASY_FAMILY;
145 bool isChosenFont(QFont & font, string const & family)
147 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
151 // Note Qt lies about family quite often
152 lyxerr[Debug::FONT] << "alleged fi family: "
153 << fromqstr(fi.family()) << endl;
155 // So we check rawName first
156 if (contains(fromqstr(font.rawName()), family)) {
157 lyxerr[Debug::FONT] << " got it ";
161 // Qt 3.2 beta1 returns "xft" for all xft fonts
162 if (font.rawName() == "xft") {
163 if (contains(fromqstr(fi.family()), family)) {
164 lyxerr[Debug::FONT] << " got it (Xft) ";
173 pair<QFont, bool> const getSymbolFont(string const & family)
175 lyxerr[Debug::FONT] << "Looking for font family "
176 << family << " ... ";
177 string upper = family;
178 upper[0] = toupper(family[0]);
181 font.setFamily(toqstr(family));
183 if (isChosenFont(font, family)) {
184 lyxerr[Debug::FONT] << "normal!" << endl;
185 return make_pair<QFont, bool>(font, true);
188 font.setFamily(toqstr(upper));
190 if (isChosenFont(font, upper)) {
191 lyxerr[Debug::FONT] << "upper!" << endl;
192 return make_pair<QFont, bool>(font, true);
195 // A simple setFamily() fails on Qt 2
197 font.setRawName(toqstr(getRawName(family)));
199 if (isChosenFont(font, family)) {
200 lyxerr[Debug::FONT] << "raw version!" << endl;
201 return make_pair<QFont, bool>(font, true);
204 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
205 return make_pair<QFont, bool>(font, false);
211 qfont_loader::qfont_loader()
213 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
214 for (int i2 = 0; i2 < 2; ++i2) {
215 for (int i3 = 0; i3 < 4; ++i3) {
216 for (int i4 = 0; i4 < 10; ++i4) {
217 fontinfo_[i1][i2][i3][i4] = 0;
225 qfont_loader::~qfont_loader()
230 void qfont_loader::update()
232 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
233 for (int i2 = 0; i2 < 2; ++i2) {
234 for (int i3 = 0; i3 < 4; ++i3) {
235 for (int i4 = 0; i4 < 10; ++i4) {
236 delete fontinfo_[i1][i2][i3][i4];
237 fontinfo_[i1][i2][i3][i4] = 0;
245 QFont const & qfont_loader::get(LyXFont const & f)
247 return getfontinfo(f)->font;
251 qfont_loader::font_info::font_info(LyXFont const & f)
255 string const pat = symbolFamily(f.family());
258 boost::tie(font, tmp) = getSymbolFont(pat);
260 switch (f.family()) {
261 case LyXFont::ROMAN_FAMILY:
262 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
263 lyxrc.roman_font_foundry)));
265 case LyXFont::SANS_FAMILY:
266 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
267 lyxrc.sans_font_foundry)));
269 case LyXFont::TYPEWRITER_FAMILY:
270 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
271 lyxrc.typewriter_font_foundry)));
278 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
279 * lyxrc.zoom / 100.0);
281 switch (f.series()) {
282 case LyXFont::MEDIUM_SERIES:
283 font.setWeight(QFont::Normal);
285 case LyXFont::BOLD_SERIES:
286 font.setWeight(QFont::Bold);
292 switch (f.realShape()) {
293 case LyXFont::ITALIC_SHAPE:
294 case LyXFont::SLANTED_SHAPE:
295 font.setItalic(true);
301 if (lyxerr.debugging(Debug::FONT)) {
302 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
303 << "' matched by\n" << font.rawName() << endl;
306 lyxerr[Debug::FONT] << "The font has size: "
307 << font.pointSizeFloat() << endl;
309 // Is this an exact match?
310 if (font.exactMatch()) {
311 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
313 lyxerr[Debug::FONT] << "This font is NOT an exact match"
317 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
319 metrics = QFontMetrics(font);
323 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
325 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
329 font_info * fi2 = new font_info(f);
330 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
335 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
337 font_info * fi = getfontinfo(f);
339 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
340 if (cit != fi->widthcache.end())
343 int const w = fi->metrics.width(QChar(val));
344 fi->widthcache[val] = w;
349 bool qfont_loader::available(LyXFont const & f)
351 if (!lyx_gui::use_gui)
354 static vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
355 static vector<bool> cache(LyXFont::NUM_FAMILIES, false);
357 LyXFont::FONT_FAMILY family = f.family();
358 if (cache_set[family])
359 return cache[family];
360 cache_set[family] = true;
362 string const pat = symbolFamily(family);
364 pair<QFont, bool> tmp = getSymbolFont(pat);
366 cache[family] = true;
370 // If the font is a tex symbol font and it is not available,
371 // we try to add the xfonts directory to the font path.
372 static bool first_time = true;
373 if (!first_time || family == LyXFont::SYMBOL_FAMILY
374 || family == LyXFont::WASY_FAMILY)
379 tmp = getSymbolFont(pat);
381 cache[family] = true;
384 // We don't need to set cache[family] to false, as it
385 //is initialized to false;
389 // We don't care about non-symbol fonts