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 #pragma implementation
18 #include "qfont_loader.h"
19 #include "qt_helpers.h"
22 #include "BufferView.h"
23 #include "qt_helpers.h"
26 #include <qfontmetrics.h>
27 #include <qfontdatabase.h>
28 #include <qstringlist.h>
29 #include "support/lstrings.h"
34 #include "support/systemcall.h"
35 #include "support/filetools.h"
46 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
50 char ** p = XGetFontPath(w.x11Display(), &n);
51 if (std::find(p, p + n, dir) != p + n)
53 lyxerr << "Adding " << dir << " to the font path.\n";
54 string const command = "xset fp+ " + dir;
56 if (!s.startscript(Systemcall::Wait, command))
58 lyxerr << "Unable to add font path.\n";
65 LyXFont::FONT_FAMILY lyx_family;
70 symbol_font symbol_fonts[] = {
71 { LyXFont::SYMBOL_FAMILY,
73 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
75 { LyXFont::CMR_FAMILY,
77 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
79 { LyXFont::CMSY_FAMILY,
81 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
83 { LyXFont::CMM_FAMILY,
85 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
87 { LyXFont::CMEX_FAMILY,
89 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
91 { LyXFont::MSA_FAMILY,
93 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
95 { LyXFont::MSB_FAMILY,
97 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
99 { LyXFont::EUFRAK_FAMILY,
101 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
103 { LyXFont::WASY_FAMILY,
105 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
108 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
111 string getRawName(string const & family)
113 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
114 if (family == symbol_fonts[i].family)
115 return symbol_fonts[i].xlfd;
117 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
122 string const symbolFamily(LyXFont::FONT_FAMILY family)
124 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
125 if (family == symbol_fonts[i].lyx_family)
126 return symbol_fonts[i].family;
132 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
134 return family >= LyXFont::SYMBOL_FAMILY &&
135 family <= LyXFont::WASY_FAMILY;
139 QFont const getSymbolFont(string const & family)
141 lyxerr[Debug::FONT] << "Looking for font family "
142 << family << " ... ";
143 string upper = family;
144 upper[0] = toupper(family[0]);
147 font.setFamily(toqstr(family));
149 // Note Qt lies about family, so we use rawName.
150 if (contains(fromqstr(font.rawName()), family)) {
151 lyxerr[Debug::FONT] << " got it !" << endl;
155 font.setFamily(toqstr(upper));
157 if (contains(fromqstr(font.rawName()), upper)) {
158 lyxerr[Debug::FONT] << " got it (uppercase version) !" << endl;
162 // A simple setFamily() fails on Qt 2
164 font.setRawName(toqstr(getRawName(family)));
166 if (contains(fromqstr(font.rawName()), family)) {
167 lyxerr[Debug::FONT] << " got it (raw version) !" << endl;
171 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
176 bool isAvailable(LyXFont const & f)
178 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
179 static std::vector<bool> cache_initialized(LyXFont::NUM_FAMILIES, false);
180 static bool first_call = true;
182 LyXFont::FONT_FAMILY lyxfamily = f.family();
183 if (cache_initialized[lyxfamily])
184 return cache[lyxfamily];
185 cache_initialized[lyxfamily] = true;
187 if (first_call && isSymbolFamily(lyxfamily)) {
192 string const tmp = symbolFamily(lyxfamily);
197 QString const family(toqstr(tmp));
199 lyxerr[Debug::FONT] << "Family " << tmp
203 // pass false for match-locale: LaTeX fonts
204 // do not have non-matching locale according
206 QStringList sl(db.families(false));
208 for (QStringList::Iterator it = sl.begin(); it != sl.end(); ++it) {
209 // Case-insensitive for Cmmi10 vs. cmmi10
210 if ((*it).lower().startsWith(family.lower())) {
213 << fromqstr(*it) << endl;
214 cache[lyxfamily] = true;
218 lyxerr[Debug::FONT] << " no." << endl;
226 qfont_loader::qfont_loader()
228 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
229 for (int i2 = 0; i2 < 2; ++i2) {
230 for (int i3 = 0; i3 < 4; ++i3) {
231 for (int i4 = 0; i4 < 10; ++i4) {
232 fontinfo_[i1][i2][i3][i4] = 0;
240 qfont_loader::~qfont_loader()
245 void qfont_loader::update()
247 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
248 for (int i2 = 0; i2 < 2; ++i2) {
249 for (int i3 = 0; i3 < 4; ++i3) {
250 for (int i4 = 0; i4 < 10; ++i4) {
251 delete fontinfo_[i1][i2][i3][i4];
252 fontinfo_[i1][i2][i3][i4] = 0;
260 QFont const & qfont_loader::get(LyXFont const & f)
262 static bool first_call = true;
264 if (first_call && isSymbolFamily(f.family())) {
269 QFont const & ret(getfontinfo(f)->font);
275 qfont_loader::font_info::font_info(LyXFont const & f)
279 string const pat = symbolFamily(f.family());
281 font = getSymbolFont(pat);
283 switch (f.family()) {
284 case LyXFont::ROMAN_FAMILY:
285 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
286 lyxrc.roman_font_foundry)));
288 case LyXFont::SANS_FAMILY:
289 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
290 lyxrc.sans_font_foundry)));
292 case LyXFont::TYPEWRITER_FAMILY:
293 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
294 lyxrc.typewriter_font_foundry)));
301 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
302 * lyxrc.zoom / 100.0);
304 switch (f.series()) {
305 case LyXFont::MEDIUM_SERIES:
306 font.setWeight(QFont::Normal);
308 case LyXFont::BOLD_SERIES:
309 font.setWeight(QFont::Bold);
315 switch (f.realShape()) {
316 case LyXFont::ITALIC_SHAPE:
317 case LyXFont::SLANTED_SHAPE:
318 font.setItalic(true);
324 if (lyxerr.debugging(Debug::FONT)) {
325 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
326 << "' matched by\n" << font.rawName() << endl;
329 lyxerr[Debug::FONT] << "The font has size: "
330 << font.pointSizeFloat() << endl;
332 // Is this an exact match?
333 if (font.exactMatch()) {
334 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
336 lyxerr[Debug::FONT] << "This font is NOT an exact match"
340 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
342 metrics = QFontMetrics(font);
346 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
348 if (!lyxrc.use_gui) {
352 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
356 font_info * fi2 = new font_info(f);
357 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
362 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
364 font_info * fi = getfontinfo(f);
366 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
367 if (cit != fi->widthcache.end())
370 int const w = fi->metrics.width(QChar(val));
371 fi->widthcache[val] = w;
376 bool qfont_loader::available(LyXFont const & f)
381 return isAvailable(f);