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[Debug::FONT] << "Adding " << dir
51 << " to the font path." << endl;
52 string const command = "xset fp+ " + dir;
54 if (!s.startscript(Systemcall::Wait, command))
56 lyxerr << "Unable to add " << dir << "to the font path."
64 LyXFont::FONT_FAMILY lyx_family;
69 symbol_font symbol_fonts[] = {
70 { LyXFont::SYMBOL_FAMILY,
72 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
74 { LyXFont::CMR_FAMILY,
76 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
78 { LyXFont::CMSY_FAMILY,
80 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
82 { LyXFont::CMM_FAMILY,
84 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
86 { LyXFont::CMEX_FAMILY,
88 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
90 { LyXFont::MSA_FAMILY,
92 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
94 { LyXFont::MSB_FAMILY,
96 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
98 { LyXFont::EUFRAK_FAMILY,
100 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
102 { LyXFont::WASY_FAMILY,
104 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
107 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
110 string getRawName(string const & family)
112 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
113 if (family == symbol_fonts[i].family)
114 return symbol_fonts[i].xlfd;
116 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
121 string const symbolFamily(LyXFont::FONT_FAMILY family)
123 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
124 if (family == symbol_fonts[i].lyx_family)
125 return symbol_fonts[i].family;
131 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
133 return family >= LyXFont::SYMBOL_FAMILY &&
134 family <= LyXFont::WASY_FAMILY;
138 QFont const getSymbolFont(string const & family)
140 lyxerr[Debug::FONT] << "Looking for font family "
141 << family << " ... ";
142 string upper = family;
143 upper[0] = toupper(family[0]);
146 font.setFamily(toqstr(family));
148 // Note Qt lies about family, so we use rawName.
149 if (contains(fromqstr(font.rawName()), family)) {
150 lyxerr[Debug::FONT] << " got it !" << endl;
154 font.setFamily(toqstr(upper));
156 if (contains(fromqstr(font.rawName()), upper)) {
157 lyxerr[Debug::FONT] << " got it (uppercase version) !" << endl;
161 // A simple setFamily() fails on Qt 2
163 font.setRawName(toqstr(getRawName(family)));
165 if (contains(fromqstr(font.rawName()), family)) {
166 lyxerr[Debug::FONT] << " got it (raw version) !" << endl;
170 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
175 bool isAvailable(LyXFont const & f)
177 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
178 static std::vector<bool> cache_initialized(LyXFont::NUM_FAMILIES, false);
179 static bool first_call = true;
181 LyXFont::FONT_FAMILY lyxfamily = f.family();
182 if (cache_initialized[lyxfamily])
183 return cache[lyxfamily];
184 cache_initialized[lyxfamily] = true;
186 if (first_call && isSymbolFamily(lyxfamily)) {
191 string const tmp = symbolFamily(lyxfamily);
196 QString const family(toqstr(tmp));
198 lyxerr[Debug::FONT] << "Family " << tmp
202 // pass false for match-locale: LaTeX fonts
203 // do not have non-matching locale according
205 QStringList sl(db.families(false));
207 for (QStringList::Iterator it = sl.begin(); it != sl.end(); ++it) {
208 // Case-insensitive for Cmmi10 vs. cmmi10
209 if ((*it).lower().startsWith(family.lower())) {
212 << fromqstr(*it) << endl;
213 cache[lyxfamily] = true;
217 lyxerr[Debug::FONT] << " no." << endl;
225 qfont_loader::qfont_loader()
227 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
228 for (int i2 = 0; i2 < 2; ++i2) {
229 for (int i3 = 0; i3 < 4; ++i3) {
230 for (int i4 = 0; i4 < 10; ++i4) {
231 fontinfo_[i1][i2][i3][i4] = 0;
239 qfont_loader::~qfont_loader()
244 void qfont_loader::update()
246 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
247 for (int i2 = 0; i2 < 2; ++i2) {
248 for (int i3 = 0; i3 < 4; ++i3) {
249 for (int i4 = 0; i4 < 10; ++i4) {
250 delete fontinfo_[i1][i2][i3][i4];
251 fontinfo_[i1][i2][i3][i4] = 0;
259 QFont const & qfont_loader::get(LyXFont const & f)
261 static bool first_call = true;
263 if (first_call && isSymbolFamily(f.family())) {
268 QFont const & ret(getfontinfo(f)->font);
274 qfont_loader::font_info::font_info(LyXFont const & f)
278 string const pat = symbolFamily(f.family());
280 font = getSymbolFont(pat);
282 switch (f.family()) {
283 case LyXFont::ROMAN_FAMILY:
284 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
285 lyxrc.roman_font_foundry)));
287 case LyXFont::SANS_FAMILY:
288 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
289 lyxrc.sans_font_foundry)));
291 case LyXFont::TYPEWRITER_FAMILY:
292 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
293 lyxrc.typewriter_font_foundry)));
300 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
301 * lyxrc.zoom / 100.0);
303 switch (f.series()) {
304 case LyXFont::MEDIUM_SERIES:
305 font.setWeight(QFont::Normal);
307 case LyXFont::BOLD_SERIES:
308 font.setWeight(QFont::Bold);
314 switch (f.realShape()) {
315 case LyXFont::ITALIC_SHAPE:
316 case LyXFont::SLANTED_SHAPE:
317 font.setItalic(true);
323 if (lyxerr.debugging(Debug::FONT)) {
324 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
325 << "' matched by\n" << font.rawName() << endl;
328 lyxerr[Debug::FONT] << "The font has size: "
329 << font.pointSizeFloat() << endl;
331 // Is this an exact match?
332 if (font.exactMatch()) {
333 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
335 lyxerr[Debug::FONT] << "This font is NOT an exact match"
339 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
341 metrics = QFontMetrics(font);
345 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
347 if (!lyxrc.use_gui) {
351 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
355 font_info * fi2 = new font_info(f);
356 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
361 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
363 font_info * fi = getfontinfo(f);
365 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
366 if (cit != fi->widthcache.end())
369 int const w = fi->metrics.width(QChar(val));
370 fi->widthcache[val] = w;
375 bool qfont_loader::available(LyXFont const & f)
380 return isAvailable(f);