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.
14 #include "qfont_loader.h"
15 #include "qt_helpers.h"
20 #include "frontends/lyx_gui.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/systemcall.h"
26 #include <qfontinfo.h>
28 #include <boost/tuple/tuple.hpp>
36 using lyx::support::contains;
37 using lyx::support::LibFileSearch;
38 using lyx::support::OnlyPath;
39 using lyx::support::QuoteName;
40 using lyx::support::Systemcall;
50 #include <ApplicationServices/ApplicationServices.h>
58 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
62 char ** p = XGetFontPath(w.x11Display(), &n);
63 if (std::find(p, p + n, dir) != p + n)
65 lyxerr[Debug::FONT] << "Adding " << dir
66 << " to the font path." << endl;
67 string const command = "xset fp+ " + QuoteName(dir);
69 if (!s.startscript(Systemcall::Wait, command))
71 lyxerr << "Unable to add " << dir << "to the font path."
76 CFBundleRef myAppBundle = CFBundleGetMainBundle();
77 CFURLRef myAppResourcesURL, FontsURL;
80 CFStringRef filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
81 (UInt8 *) "Fonts", strlen("Fonts"),
82 kCFStringEncodingISOLatin1, false);
84 myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
85 FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
86 myAppResourcesURL, filePath, true);
87 if (lyxerr.debugging(Debug::FONT)) {
89 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
90 lyxerr << "Adding Fonts directory: " << buf << endl;
92 CFURLGetFSRef (FontsURL, &fontDirRef);
93 OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
94 NULL, NULL, &fontDirSpec, NULL);
96 lyxerr << "FSGetCatalogInfo err = " << err << endl;
97 err = FMActivateFonts (&fontDirSpec, NULL, NULL,
98 kFMLocalActivationContext);
100 lyxerr << "FMActivateFonts err = " << err << endl;
106 LyXFont::FONT_FAMILY lyx_family;
111 symbol_font symbol_fonts[] = {
112 { LyXFont::SYMBOL_FAMILY,
114 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
116 { LyXFont::CMR_FAMILY,
118 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
120 { LyXFont::CMSY_FAMILY,
122 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
124 { LyXFont::CMM_FAMILY,
126 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
128 { LyXFont::CMEX_FAMILY,
130 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
132 { LyXFont::MSA_FAMILY,
134 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
136 { LyXFont::MSB_FAMILY,
138 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
140 { LyXFont::EUFRAK_FAMILY,
142 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
144 { LyXFont::WASY_FAMILY,
146 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
149 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
152 string getRawName(string const & family)
154 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
155 if (family == symbol_fonts[i].family)
156 return symbol_fonts[i].xlfd;
158 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
163 string const symbolFamily(LyXFont::FONT_FAMILY family)
165 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
166 if (family == symbol_fonts[i].lyx_family)
167 return symbol_fonts[i].family;
173 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
175 return family >= LyXFont::SYMBOL_FAMILY &&
176 family <= LyXFont::WASY_FAMILY;
180 bool isChosenFont(QFont & font, string const & family)
182 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
186 // Note Qt lies about family quite often
187 lyxerr[Debug::FONT] << "alleged fi family: "
188 << fromqstr(fi.family()) << endl;
190 // So we check rawName first
191 if (contains(fromqstr(font.rawName()), family)) {
192 lyxerr[Debug::FONT] << " got it ";
196 // Qt 3.2 beta1 returns "xft" for all xft fonts
197 if (font.rawName() == "xft") {
198 if (contains(fromqstr(fi.family()), family)) {
199 lyxerr[Debug::FONT] << " got it (Xft) ";
208 pair<QFont, bool> const getSymbolFont(string const & family)
210 lyxerr[Debug::FONT] << "Looking for font family "
211 << family << " ... ";
212 string upper = family;
213 upper[0] = toupper(family[0]);
216 font.setFamily(toqstr(family));
218 if (isChosenFont(font, family)) {
219 lyxerr[Debug::FONT] << "normal!" << endl;
220 return make_pair<QFont, bool>(font, true);
223 font.setFamily(toqstr(upper));
225 if (isChosenFont(font, upper)) {
226 lyxerr[Debug::FONT] << "upper!" << endl;
227 return make_pair<QFont, bool>(font, true);
230 // A simple setFamily() fails on Qt 2
232 font.setRawName(toqstr(getRawName(family)));
234 if (isChosenFont(font, family)) {
235 lyxerr[Debug::FONT] << "raw version!" << endl;
236 return make_pair<QFont, bool>(font, true);
239 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
240 return make_pair<QFont, bool>(font, false);
246 qfont_loader::qfont_loader()
248 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
249 for (int i2 = 0; i2 < 2; ++i2) {
250 for (int i3 = 0; i3 < 4; ++i3) {
251 for (int i4 = 0; i4 < 10; ++i4) {
252 fontinfo_[i1][i2][i3][i4] = 0;
260 qfont_loader::~qfont_loader()
265 void qfont_loader::update()
267 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
268 for (int i2 = 0; i2 < 2; ++i2) {
269 for (int i3 = 0; i3 < 4; ++i3) {
270 for (int i4 = 0; i4 < 10; ++i4) {
271 delete fontinfo_[i1][i2][i3][i4];
272 fontinfo_[i1][i2][i3][i4] = 0;
280 QFont const & qfont_loader::get(LyXFont const & f)
282 return getfontinfo(f)->font;
286 qfont_loader::font_info::font_info(LyXFont const & f)
290 string const pat = symbolFamily(f.family());
293 boost::tie(font, tmp) = getSymbolFont(pat);
295 switch (f.family()) {
296 case LyXFont::ROMAN_FAMILY:
297 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
298 lyxrc.roman_font_foundry)));
300 case LyXFont::SANS_FAMILY:
301 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
302 lyxrc.sans_font_foundry)));
304 case LyXFont::TYPEWRITER_FAMILY:
305 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
306 lyxrc.typewriter_font_foundry)));
313 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
314 * lyxrc.zoom / 100.0);
316 switch (f.series()) {
317 case LyXFont::MEDIUM_SERIES:
318 font.setWeight(QFont::Normal);
320 case LyXFont::BOLD_SERIES:
321 font.setWeight(QFont::Bold);
327 switch (f.realShape()) {
328 case LyXFont::ITALIC_SHAPE:
329 case LyXFont::SLANTED_SHAPE:
330 font.setItalic(true);
336 if (lyxerr.debugging(Debug::FONT)) {
337 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
338 << "' matched by\n" << font.rawName() << endl;
341 lyxerr[Debug::FONT] << "The font has size: "
342 << font.pointSizeFloat() << endl;
344 // Is this an exact match?
345 if (font.exactMatch()) {
346 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
348 lyxerr[Debug::FONT] << "This font is NOT an exact match"
352 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
354 metrics = QFontMetrics(font);
358 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
360 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
364 font_info * fi2 = new font_info(f);
365 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
370 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
372 font_info * fi = getfontinfo(f);
374 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
375 if (cit != fi->widthcache.end())
378 int const w = fi->metrics.width(QChar(val));
379 fi->widthcache[val] = w;
384 bool qfont_loader::available(LyXFont const & f)
386 if (!lyx_gui::use_gui)
389 static bool need_bundle_fonts = true;
390 if (need_bundle_fonts) {
392 need_bundle_fonts = false;
396 static vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
397 static vector<bool> cache(LyXFont::NUM_FAMILIES, false);
399 LyXFont::FONT_FAMILY family = f.family();
400 if (cache_set[family])
401 return cache[family];
402 cache_set[family] = true;
404 string const pat = symbolFamily(family);
406 pair<QFont, bool> tmp = getSymbolFont(pat);
408 cache[family] = true;
412 // If the font is a tex symbol font and it is not available,
413 // we try to add the xfonts directory to the font path.
414 static bool first_time = true;
415 if (!first_time || family == LyXFont::SYMBOL_FAMILY
416 || family == LyXFont::WASY_FAMILY)
421 tmp = getSymbolFont(pat);
423 cache[family] = true;
426 // We don't need to set cache[family] to false, as it
427 //is initialized to false;
431 // We don't care about non-symbol fonts