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"
48 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
52 char ** p = XGetFontPath(w.x11Display(), &n);
53 if (std::find(p, p + n, dir) != p + n)
55 lyxerr[Debug::FONT] << "Adding " << dir
56 << " to the font path." << endl;
57 string const command = "xset fp+ " + dir;
59 if (!s.startscript(Systemcall::Wait, command))
61 lyxerr << "Unable to add " << dir << "to the font path."
69 LyXFont::FONT_FAMILY lyx_family;
74 symbol_font symbol_fonts[] = {
75 { LyXFont::SYMBOL_FAMILY,
77 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
79 { LyXFont::CMR_FAMILY,
81 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
83 { LyXFont::CMSY_FAMILY,
85 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
87 { LyXFont::CMM_FAMILY,
89 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
91 { LyXFont::CMEX_FAMILY,
93 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
95 { LyXFont::MSA_FAMILY,
97 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
99 { LyXFont::MSB_FAMILY,
101 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
103 { LyXFont::EUFRAK_FAMILY,
105 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
107 { LyXFont::WASY_FAMILY,
109 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
112 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
115 string getRawName(string const & family)
117 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
118 if (family == symbol_fonts[i].family)
119 return symbol_fonts[i].xlfd;
121 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
126 string const symbolFamily(LyXFont::FONT_FAMILY family)
128 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
129 if (family == symbol_fonts[i].lyx_family)
130 return symbol_fonts[i].family;
136 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
138 return family >= LyXFont::SYMBOL_FAMILY &&
139 family <= LyXFont::WASY_FAMILY;
143 pair<QFont, bool> const getSymbolFont(string const & family)
145 lyxerr[Debug::FONT] << "Looking for font family "
146 << family << " ... ";
147 string upper = family;
148 upper[0] = toupper(family[0]);
151 font.setFamily(toqstr(family));
153 // Note Qt lies about family, so we use rawName.
154 if (contains(fromqstr(font.rawName()), family)) {
155 lyxerr[Debug::FONT] << " got it !" << endl;
156 return make_pair<QFont, bool>(font, true);
159 font.setFamily(toqstr(upper));
161 if (contains(fromqstr(font.rawName()), upper)) {
162 lyxerr[Debug::FONT] << " got it (uppercase version) !" << endl;
163 return make_pair<QFont, bool>(font, true);
166 // A simple setFamily() fails on Qt 2
168 font.setRawName(toqstr(getRawName(family)));
170 if (contains(fromqstr(font.rawName()), family)) {
171 lyxerr[Debug::FONT] << " got it (raw version) !" << endl;
172 return make_pair<QFont, bool>(font, true);
175 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
176 return make_pair<QFont, bool>(font, false);
182 qfont_loader::qfont_loader()
184 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
185 for (int i2 = 0; i2 < 2; ++i2) {
186 for (int i3 = 0; i3 < 4; ++i3) {
187 for (int i4 = 0; i4 < 10; ++i4) {
188 fontinfo_[i1][i2][i3][i4] = 0;
196 qfont_loader::~qfont_loader()
201 void qfont_loader::update()
203 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
204 for (int i2 = 0; i2 < 2; ++i2) {
205 for (int i3 = 0; i3 < 4; ++i3) {
206 for (int i4 = 0; i4 < 10; ++i4) {
207 delete fontinfo_[i1][i2][i3][i4];
208 fontinfo_[i1][i2][i3][i4] = 0;
216 QFont const & qfont_loader::get(LyXFont const & f)
218 return getfontinfo(f)->font;
222 qfont_loader::font_info::font_info(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.setPointSizeFloat(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" << font.rawName() << endl;
277 lyxerr[Debug::FONT] << "The font has size: "
278 << font.pointSizeFloat() << 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"
288 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
290 metrics = QFontMetrics(font);
294 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
296 if (!lyx_gui::use_gui) {
300 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
304 font_info * fi2 = new font_info(f);
305 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
310 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
312 font_info * fi = getfontinfo(f);
314 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
315 if (cit != fi->widthcache.end())
318 int const w = fi->metrics.width(QChar(val));
319 fi->widthcache[val] = w;
324 bool qfont_loader::available(LyXFont const & f)
326 if (!lyx_gui::use_gui)
329 static vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
330 static vector<bool> cache(LyXFont::NUM_FAMILIES, false);
332 LyXFont::FONT_FAMILY family = f.family();
333 if (cache_set[family])
334 return cache[family];
335 cache_set[family] = true;
337 string const pat = symbolFamily(family);
339 pair<QFont, bool> tmp = getSymbolFont(pat);
341 cache[family] = true;
345 // If the font is a tex symbol font and it is not available,
346 // we try to add the xfonts directory to the font path.
347 static bool first_time = true;
348 if (!first_time || family == LyXFont::SYMBOL_FAMILY
349 || family == LyXFont::WASY_FAMILY)
354 tmp = getSymbolFont(pat);
356 cache[family] = true;
359 // We don't need to set cache[family] to false, as it
360 //is initialized to false;
364 // We don't care about non-symbol fonts