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;
41 using lyx::support::strToDbl;
51 #include <ApplicationServices/ApplicationServices.h>
56 void qfont_loader::addToFontPath()
59 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
63 char ** p = XGetFontPath(w.x11Display(), &n);
64 if (std::find(p, p + n, dir) != p + n)
66 lyxerr[Debug::FONT] << "Adding " << dir
67 << " to the font path." << endl;
68 string const command = "xset fp+ " + QuoteName(dir);
70 if (!s.startscript(Systemcall::Wait, command))
72 lyxerr << "Unable to add " << dir << "to the font path."
78 void qfont_loader::initFontPath()
81 CFBundleRef myAppBundle = CFBundleGetMainBundle();
82 CFURLRef myAppResourcesURL, FontsURL;
85 CFStringRef filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
86 (UInt8 *) "Fonts", strlen("Fonts"),
87 kCFStringEncodingISOLatin1, false);
89 myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
90 FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
91 myAppResourcesURL, filePath, true);
92 if (lyxerr.debugging(Debug::FONT)) {
94 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
95 lyxerr << "Adding Fonts directory: " << buf << endl;
97 CFURLGetFSRef (FontsURL, &fontDirRef);
98 OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
99 NULL, NULL, &fontDirSpec, NULL);
101 lyxerr << "FSGetCatalogInfo err = " << err << endl;
102 err = FMActivateFonts (&fontDirSpec, NULL, NULL,
103 kFMLocalActivationContext);
105 lyxerr << "FMActivateFonts err = " << err << endl;
112 LyXFont::FONT_FAMILY lyx_family;
117 symbol_font symbol_fonts[] = {
118 { LyXFont::SYMBOL_FAMILY,
120 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
122 { LyXFont::CMR_FAMILY,
124 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
126 { LyXFont::CMSY_FAMILY,
128 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
130 { LyXFont::CMM_FAMILY,
132 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
134 { LyXFont::CMEX_FAMILY,
136 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
138 { LyXFont::MSA_FAMILY,
140 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
142 { LyXFont::MSB_FAMILY,
144 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
146 { LyXFont::EUFRAK_FAMILY,
148 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
150 { LyXFont::WASY_FAMILY,
152 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
155 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
158 string getRawName(string const & family)
160 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
161 if (family == symbol_fonts[i].family)
162 return symbol_fonts[i].xlfd;
164 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
169 string const symbolFamily(LyXFont::FONT_FAMILY family)
171 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
172 if (family == symbol_fonts[i].lyx_family)
173 return symbol_fonts[i].family;
179 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
181 return family >= LyXFont::SYMBOL_FAMILY &&
182 family <= LyXFont::WASY_FAMILY;
186 bool isChosenFont(QFont & font, string const & family)
188 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
192 // Note Qt lies about family quite often
193 lyxerr[Debug::FONT] << "alleged fi family: "
194 << fromqstr(fi.family()) << endl;
196 // So we check rawName first
197 if (contains(fromqstr(font.rawName()), family)) {
198 lyxerr[Debug::FONT] << " got it ";
202 // Qt 3.2 beta1 returns "xft" for all xft fonts
203 if (font.rawName() == "xft") {
204 if (contains(fromqstr(fi.family()), family)) {
205 lyxerr[Debug::FONT] << " got it (Xft) ";
214 pair<QFont, bool> const getSymbolFont(string const & family)
216 lyxerr[Debug::FONT] << "Looking for font family "
217 << family << " ... ";
218 string upper = family;
219 upper[0] = toupper(family[0]);
222 font.setFamily(toqstr(family));
224 if (isChosenFont(font, family)) {
225 lyxerr[Debug::FONT] << "normal!" << endl;
226 return make_pair<QFont, bool>(font, true);
229 font.setFamily(toqstr(upper));
231 if (isChosenFont(font, upper)) {
232 lyxerr[Debug::FONT] << "upper!" << endl;
233 return make_pair<QFont, bool>(font, true);
236 // A simple setFamily() fails on Qt 2
238 font.setRawName(toqstr(getRawName(family)));
240 if (isChosenFont(font, family)) {
241 lyxerr[Debug::FONT] << "raw version!" << endl;
242 return make_pair<QFont, bool>(font, true);
245 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
246 return make_pair<QFont, bool>(font, false);
252 qfont_loader::qfont_loader()
254 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
255 for (int i2 = 0; i2 < 2; ++i2) {
256 for (int i3 = 0; i3 < 4; ++i3) {
257 for (int i4 = 0; i4 < 10; ++i4) {
258 fontinfo_[i1][i2][i3][i4] = 0;
266 qfont_loader::~qfont_loader()
271 void qfont_loader::update()
273 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
274 for (int i2 = 0; i2 < 2; ++i2) {
275 for (int i3 = 0; i3 < 4; ++i3) {
276 for (int i4 = 0; i4 < 10; ++i4) {
277 delete fontinfo_[i1][i2][i3][i4];
278 fontinfo_[i1][i2][i3][i4] = 0;
286 QFont const & qfont_loader::get(LyXFont const & f)
288 return getfontinfo(f)->font;
292 qfont_loader::font_info::font_info(LyXFont const & f)
296 string const pat = symbolFamily(f.family());
299 boost::tie(font, tmp) = getSymbolFont(pat);
301 switch (f.family()) {
302 case LyXFont::ROMAN_FAMILY:
303 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
304 lyxrc.roman_font_foundry)));
306 case LyXFont::SANS_FAMILY:
307 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
308 lyxrc.sans_font_foundry)));
310 case LyXFont::TYPEWRITER_FAMILY:
311 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
312 lyxrc.typewriter_font_foundry)));
319 font.setPointSizeFloat(strToDbl(lyxrc.font_sizes[f.size()])
320 * lyxrc.zoom / 100.0);
322 switch (f.series()) {
323 case LyXFont::MEDIUM_SERIES:
324 font.setWeight(QFont::Normal);
326 case LyXFont::BOLD_SERIES:
327 font.setWeight(QFont::Bold);
333 switch (f.realShape()) {
334 case LyXFont::ITALIC_SHAPE:
335 case LyXFont::SLANTED_SHAPE:
336 font.setItalic(true);
342 if (lyxerr.debugging(Debug::FONT)) {
343 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
344 << "' matched by\n" << font.rawName() << endl;
347 lyxerr[Debug::FONT] << "The font has size: "
348 << font.pointSizeFloat() << endl;
350 // Is this an exact match?
351 if (font.exactMatch()) {
352 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
354 lyxerr[Debug::FONT] << "This font is NOT an exact match"
358 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
360 metrics = QFontMetrics(font);
364 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
366 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
370 font_info * fi2 = new font_info(f);
371 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
376 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
378 font_info * fi = getfontinfo(f);
380 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
381 if (cit != fi->widthcache.end())
384 int const w = fi->metrics.width(QChar(val));
385 fi->widthcache[val] = w;
390 bool qfont_loader::available(LyXFont const & f)
392 if (!lyx_gui::use_gui)
395 static vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
396 static vector<bool> cache(LyXFont::NUM_FAMILIES, false);
398 LyXFont::FONT_FAMILY family = f.family();
399 if (cache_set[family])
400 return cache[family];
401 cache_set[family] = true;
403 string const pat = symbolFamily(family);
405 pair<QFont, bool> tmp = getSymbolFont(pat);
407 cache[family] = true;
411 // If the font is a tex symbol font and it is not available,
412 // we try to add the xfonts directory to the font path.
413 static bool first_time = true;
414 if (!first_time || family == LyXFont::SYMBOL_FAMILY
415 || family == LyXFont::WASY_FAMILY)
420 tmp = getSymbolFont(pat);
422 cache[family] = true;
425 // We don't need to set cache[family] to false, as it
426 //is initialized to false;
430 // We don't care about non-symbol fonts