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"
31 #include "support/systemcall.h"
32 #include "support/filetools.h"
43 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
47 char ** p = XGetFontPath(w.x11Display(), &n);
48 if (std::find(p, p + n, dir) != p + n)
50 lyxerr << "Adding " << dir << " to the font path.\n";
51 string const command = "xset fp+ " + dir;
53 if (!s.startscript(Systemcall::Wait, command))
55 lyxerr << "Unable to add font path.\n";
62 LyXFont::FONT_FAMILY lyx_family;
67 symbol_font symbol_fonts[] = {
68 { LyXFont::SYMBOL_FAMILY,
70 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
72 { LyXFont::CMR_FAMILY,
74 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
76 { LyXFont::CMSY_FAMILY,
78 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
80 { LyXFont::CMM_FAMILY,
82 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
84 { LyXFont::CMEX_FAMILY,
86 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
88 { LyXFont::MSA_FAMILY,
90 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
92 { LyXFont::MSB_FAMILY,
94 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
96 { LyXFont::EUFRAK_FAMILY,
98 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
100 { LyXFont::WASY_FAMILY,
102 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
105 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
108 string getRawName(string const & family)
110 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
111 if (family == symbol_fonts[i].family)
112 return symbol_fonts[i].xlfd;
114 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
119 string const symbolFamily(LyXFont::FONT_FAMILY family)
121 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
122 if (family == symbol_fonts[i].lyx_family)
123 return symbol_fonts[i].family;
129 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
131 return family >= LyXFont::SYMBOL_FAMILY &&
132 family <= LyXFont::WASY_FAMILY;
136 QFont const getSymbolFont(string const & family)
138 lyxerr[Debug::FONT] << "Looking for font family "
139 << family << " ... ";
140 string upper = family;
141 upper[0] = toupper(family[0]);
144 font.setFamily(toqstr(family));
146 // Note Qt lies about family, so we use rawName.
147 if (contains(fromqstr(font.rawName()), family)) {
148 lyxerr[Debug::FONT] << " got it !" << endl;
152 font.setFamily(toqstr(upper));
154 if (contains(fromqstr(font.rawName()), upper)) {
155 lyxerr[Debug::FONT] << " got it (uppercase version) !" << endl;
159 // A simple setFamily() fails on Qt 2
161 font.setRawName(toqstr(getRawName(family)));
163 if (contains(fromqstr(font.rawName()), family)) {
164 lyxerr[Debug::FONT] << " got it (raw version) !" << endl;
168 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
173 bool isAvailable(LyXFont const & f)
175 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
176 static std::vector<bool> cache_initialized(LyXFont::NUM_FAMILIES, false);
177 static bool first_call = true;
179 LyXFont::FONT_FAMILY lyxfamily = f.family();
180 if (cache_initialized[lyxfamily])
181 return cache[lyxfamily];
182 cache_initialized[lyxfamily] = true;
184 if (first_call && isSymbolFamily(lyxfamily)) {
189 string const tmp = symbolFamily(lyxfamily);
194 QString const family(toqstr(tmp));
196 lyxerr[Debug::FONT] << "Family " << tmp
200 // pass false for match-locale: LaTeX fonts
201 // do not have non-matching locale according
203 QStringList sl(db.families(false));
205 for (QStringList::Iterator it = sl.begin(); it != sl.end(); ++it) {
206 // Case-insensitive for Cmmi10 vs. cmmi10
207 if ((*it).lower().startsWith(family.lower())) {
210 << fromqstr(*it) << endl;
211 cache[lyxfamily] = true;
215 lyxerr[Debug::FONT] << " no." << endl;
223 qfont_loader::qfont_loader()
225 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
226 for (int i2 = 0; i2 < 2; ++i2) {
227 for (int i3 = 0; i3 < 4; ++i3) {
228 for (int i4 = 0; i4 < 10; ++i4) {
229 fontinfo_[i1][i2][i3][i4] = 0;
237 qfont_loader::~qfont_loader()
242 void qfont_loader::update()
244 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
245 for (int i2 = 0; i2 < 2; ++i2) {
246 for (int i3 = 0; i3 < 4; ++i3) {
247 for (int i4 = 0; i4 < 10; ++i4) {
248 delete fontinfo_[i1][i2][i3][i4];
249 fontinfo_[i1][i2][i3][i4] = 0;
257 QFont const & qfont_loader::get(LyXFont const & f)
259 static bool first_call = true;
261 if (first_call && isSymbolFamily(f.family())) {
266 QFont const & ret(getfontinfo(f)->font);
272 qfont_loader::font_info::font_info(LyXFont const & f)
276 string const pat = symbolFamily(f.family());
278 font = getSymbolFont(pat);
280 switch (f.family()) {
281 case LyXFont::ROMAN_FAMILY:
282 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
283 lyxrc.roman_font_foundry)));
285 case LyXFont::SANS_FAMILY:
286 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
287 lyxrc.sans_font_foundry)));
289 case LyXFont::TYPEWRITER_FAMILY:
290 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
291 lyxrc.typewriter_font_foundry)));
298 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
299 * lyxrc.zoom / 100.0);
301 switch (f.series()) {
302 case LyXFont::MEDIUM_SERIES:
303 font.setWeight(QFont::Normal);
305 case LyXFont::BOLD_SERIES:
306 font.setWeight(QFont::Bold);
312 switch (f.realShape()) {
313 case LyXFont::ITALIC_SHAPE:
314 case LyXFont::SLANTED_SHAPE:
315 font.setItalic(true);
321 if (lyxerr.debugging(Debug::FONT)) {
322 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
323 << "' matched by\n" << font.rawName() << endl;
326 lyxerr[Debug::FONT] << "The font has size: "
327 << font.pointSizeFloat() << endl;
329 // Is this an exact match?
330 if (font.exactMatch()) {
331 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
333 lyxerr[Debug::FONT] << "This font is NOT an exact match"
337 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
339 metrics = QFontMetrics(font);
343 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
345 if (!lyxrc.use_gui) {
349 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
353 font_info * fi2 = new font_info(f);
354 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
359 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
361 font_info * fi = getfontinfo(f);
363 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
364 if (cit != fi->widthcache.end())
367 int const w = fi->metrics.width(QChar(val));
368 fi->widthcache[val] = w;
373 bool qfont_loader::available(LyXFont const & f)
378 return isAvailable(f);