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"
30 #include <boost/tuple/tuple.hpp>
35 #include "support/systemcall.h"
36 #include "support/filetools.h"
39 using namespace lyx::support;
51 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
55 char ** p = XGetFontPath(w.x11Display(), &n);
56 if (std::find(p, p + n, dir) != p + n)
58 lyxerr[Debug::FONT] << "Adding " << dir
59 << " to the font path." << endl;
60 string const command = "xset fp+ " + dir;
62 if (!s.startscript(Systemcall::Wait, command))
64 lyxerr << "Unable to add " << dir << "to the font path."
72 LyXFont::FONT_FAMILY lyx_family;
77 symbol_font symbol_fonts[] = {
78 { LyXFont::SYMBOL_FAMILY,
80 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
82 { LyXFont::CMR_FAMILY,
84 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
86 { LyXFont::CMSY_FAMILY,
88 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
90 { LyXFont::CMM_FAMILY,
92 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
94 { LyXFont::CMEX_FAMILY,
96 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
98 { LyXFont::MSA_FAMILY,
100 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
102 { LyXFont::MSB_FAMILY,
104 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
106 { LyXFont::EUFRAK_FAMILY,
108 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
110 { LyXFont::WASY_FAMILY,
112 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
115 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
118 string getRawName(string const & family)
120 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
121 if (family == symbol_fonts[i].family)
122 return symbol_fonts[i].xlfd;
124 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
129 string const symbolFamily(LyXFont::FONT_FAMILY family)
131 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
132 if (family == symbol_fonts[i].lyx_family)
133 return symbol_fonts[i].family;
139 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
141 return family >= LyXFont::SYMBOL_FAMILY &&
142 family <= LyXFont::WASY_FAMILY;
146 bool isChosenFont(QFont & font, string const & family)
148 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
152 // Note Qt lies about family quite often
153 lyxerr[Debug::FONT] << "alleged fi family: "
154 << fromqstr(fi.family()) << endl;
156 // So we check rawName first
157 if (contains(fromqstr(font.rawName()), family)) {
158 lyxerr[Debug::FONT] << " got it ";
162 // Qt 3.2 beta1 returns "xft" for all xft fonts
163 if (font.rawName() == "xft") {
164 if (contains(fromqstr(fi.family()), family)) {
165 lyxerr[Debug::FONT] << " got it (Xft) ";
174 pair<QFont, bool> const getSymbolFont(string const & family)
176 lyxerr[Debug::FONT] << "Looking for font family "
177 << family << " ... ";
178 string upper = family;
179 upper[0] = toupper(family[0]);
182 font.setFamily(toqstr(family));
184 if (isChosenFont(font, family)) {
185 lyxerr[Debug::FONT] << "normal!" << endl;
186 return make_pair<QFont, bool>(font, true);
189 font.setFamily(toqstr(upper));
191 if (isChosenFont(font, upper)) {
192 lyxerr[Debug::FONT] << "upper!" << endl;
193 return make_pair<QFont, bool>(font, true);
196 // A simple setFamily() fails on Qt 2
198 font.setRawName(toqstr(getRawName(family)));
200 if (isChosenFont(font, family)) {
201 lyxerr[Debug::FONT] << "raw version!" << endl;
202 return make_pair<QFont, bool>(font, true);
205 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
206 return make_pair<QFont, bool>(font, false);
212 qfont_loader::qfont_loader()
214 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
215 for (int i2 = 0; i2 < 2; ++i2) {
216 for (int i3 = 0; i3 < 4; ++i3) {
217 for (int i4 = 0; i4 < 10; ++i4) {
218 fontinfo_[i1][i2][i3][i4] = 0;
226 qfont_loader::~qfont_loader()
231 void qfont_loader::update()
233 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
234 for (int i2 = 0; i2 < 2; ++i2) {
235 for (int i3 = 0; i3 < 4; ++i3) {
236 for (int i4 = 0; i4 < 10; ++i4) {
237 delete fontinfo_[i1][i2][i3][i4];
238 fontinfo_[i1][i2][i3][i4] = 0;
246 QFont const & qfont_loader::get(LyXFont const & f)
248 return getfontinfo(f)->font;
252 qfont_loader::font_info::font_info(LyXFont const & f)
256 string const pat = symbolFamily(f.family());
259 boost::tie(font, tmp) = getSymbolFont(pat);
261 switch (f.family()) {
262 case LyXFont::ROMAN_FAMILY:
263 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
264 lyxrc.roman_font_foundry)));
266 case LyXFont::SANS_FAMILY:
267 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
268 lyxrc.sans_font_foundry)));
270 case LyXFont::TYPEWRITER_FAMILY:
271 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
272 lyxrc.typewriter_font_foundry)));
279 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
280 * lyxrc.zoom / 100.0);
282 switch (f.series()) {
283 case LyXFont::MEDIUM_SERIES:
284 font.setWeight(QFont::Normal);
286 case LyXFont::BOLD_SERIES:
287 font.setWeight(QFont::Bold);
293 switch (f.realShape()) {
294 case LyXFont::ITALIC_SHAPE:
295 case LyXFont::SLANTED_SHAPE:
296 font.setItalic(true);
302 if (lyxerr.debugging(Debug::FONT)) {
303 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
304 << "' matched by\n" << font.rawName() << endl;
307 lyxerr[Debug::FONT] << "The font has size: "
308 << font.pointSizeFloat() << endl;
310 // Is this an exact match?
311 if (font.exactMatch()) {
312 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
314 lyxerr[Debug::FONT] << "This font is NOT an exact match"
318 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
320 metrics = QFontMetrics(font);
324 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
326 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
330 font_info * fi2 = new font_info(f);
331 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
336 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
338 font_info * fi = getfontinfo(f);
340 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
341 if (cit != fi->widthcache.end())
344 int const w = fi->metrics.width(QChar(val));
345 fi->widthcache[val] = w;
350 bool qfont_loader::available(LyXFont const & f)
352 if (!lyx_gui::use_gui)
355 static vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
356 static vector<bool> cache(LyXFont::NUM_FAMILIES, false);
358 LyXFont::FONT_FAMILY family = f.family();
359 if (cache_set[family])
360 return cache[family];
361 cache_set[family] = true;
363 string const pat = symbolFamily(family);
365 pair<QFont, bool> tmp = getSymbolFont(pat);
367 cache[family] = true;
371 // If the font is a tex symbol font and it is not available,
372 // we try to add the xfonts directory to the font path.
373 static bool first_time = true;
374 if (!first_time || family == LyXFont::SYMBOL_FAMILY
375 || family == LyXFont::WASY_FAMILY)
380 tmp = getSymbolFont(pat);
382 cache[family] = true;
385 // We don't need to set cache[family] to false, as it
386 //is initialized to false;
390 // We don't care about non-symbol fonts