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/convert.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/systemcall.h"
27 #include <qfontinfo.h>
29 #include <boost/tuple/tuple.hpp>
37 using lyx::support::contains;
38 using lyx::support::LibFileSearch;
39 using lyx::support::OnlyPath;
40 using lyx::support::QuoteName;
41 using lyx::support::Systemcall;
51 #include <ApplicationServices/ApplicationServices.h>
55 void FontLoader::initFontPath()
58 CFBundleRef myAppBundle = CFBundleGetMainBundle();
59 CFURLRef myAppResourcesURL, FontsURL;
62 CFStringRef filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
63 (UInt8 *) "Fonts", strlen("Fonts"),
64 kCFStringEncodingISOLatin1, false);
66 myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
67 FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
68 myAppResourcesURL, filePath, true);
69 if (lyxerr.debugging(Debug::FONT)) {
71 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
72 lyxerr << "Adding Fonts directory: " << buf << endl;
74 CFURLGetFSRef (FontsURL, &fontDirRef);
75 OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
76 NULL, NULL, &fontDirSpec, NULL);
78 lyxerr << "FSGetCatalogInfo err = " << err << endl;
79 err = FMActivateFonts (&fontDirSpec, NULL, NULL,
80 kFMLocalActivationContext);
82 lyxerr << "FMActivateFonts err = " << err << endl;
89 LyXFont::FONT_FAMILY lyx_family;
94 symbol_font symbol_fonts[] = {
95 { LyXFont::SYMBOL_FAMILY,
97 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
99 { LyXFont::CMR_FAMILY,
101 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
103 { LyXFont::CMSY_FAMILY,
105 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
107 { LyXFont::CMM_FAMILY,
109 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
111 { LyXFont::CMEX_FAMILY,
113 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
115 { LyXFont::MSA_FAMILY,
117 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
119 { LyXFont::MSB_FAMILY,
121 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
123 { LyXFont::EUFRAK_FAMILY,
125 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
127 { LyXFont::WASY_FAMILY,
129 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
132 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
135 string getRawName(string const & family)
137 for (size_t i = 0; i < nr_symbol_fonts; ++i)
138 if (family == symbol_fonts[i].family)
139 return symbol_fonts[i].xlfd;
141 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
146 string const symbolFamily(LyXFont::FONT_FAMILY family)
148 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
149 if (family == symbol_fonts[i].lyx_family)
150 return symbol_fonts[i].family;
156 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
158 return family >= LyXFont::SYMBOL_FAMILY &&
159 family <= LyXFont::WASY_FAMILY;
163 bool isChosenFont(QFont & font, string const & family)
165 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
169 // Note Qt lies about family quite often
170 lyxerr[Debug::FONT] << "alleged fi family: "
171 << fromqstr(fi.family()) << endl;
173 // So we check rawName first
174 if (contains(fromqstr(font.rawName()), family)) {
175 lyxerr[Debug::FONT] << " got it ";
179 // Qt 3.2 beta1 returns "xft" for all xft fonts
180 // Qt 4.1 returns "Multi" for all ? xft fonts
181 if (font.rawName() == "xft" || font.rawName() == "Multi") {
182 if (contains(fromqstr(fi.family()), family)) {
183 lyxerr[Debug::FONT] << " got it (Xft) ";
192 pair<QFont, bool> const getSymbolFont(string const & family)
194 lyxerr[Debug::FONT] << "Looking for font family "
195 << family << " ... ";
196 string upper = family;
197 upper[0] = toupper(family[0]);
200 font.setFamily(toqstr(family));
202 if (isChosenFont(font, family)) {
203 lyxerr[Debug::FONT] << "normal!" << endl;
204 return make_pair<QFont, bool>(font, true);
207 font.setFamily(toqstr(upper));
209 if (isChosenFont(font, upper)) {
210 lyxerr[Debug::FONT] << "upper!" << endl;
211 return make_pair<QFont, bool>(font, true);
214 // A simple setFamily() fails on Qt 2
216 font.setRawName(toqstr(getRawName(family)));
218 if (isChosenFont(font, family)) {
219 lyxerr[Debug::FONT] << "raw version!" << endl;
220 return make_pair<QFont, bool>(font, true);
223 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
224 return make_pair<QFont, bool>(font, false);
230 FontLoader::FontLoader()
232 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
233 for (int i2 = 0; i2 < 2; ++i2)
234 for (int i3 = 0; i3 < 4; ++i3)
235 for (int i4 = 0; i4 < 10; ++i4)
236 fontinfo_[i1][i2][i3][i4] = 0;
240 void FontLoader::update()
242 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
243 for (int i2 = 0; i2 < 2; ++i2)
244 for (int i3 = 0; i3 < 4; ++i3)
245 for (int i4 = 0; i4 < 10; ++i4) {
246 delete fontinfo_[i1][i2][i3][i4];
247 fontinfo_[i1][i2][i3][i4] = 0;
252 /////////////////////////////////////////////////
255 QLFontInfo::QLFontInfo(LyXFont const & f)
259 string const pat = symbolFamily(f.family());
262 boost::tie(font, tmp) = getSymbolFont(pat);
264 switch (f.family()) {
265 case LyXFont::ROMAN_FAMILY:
266 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
267 lyxrc.roman_font_foundry)));
269 case LyXFont::SANS_FAMILY:
270 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
271 lyxrc.sans_font_foundry)));
273 case LyXFont::TYPEWRITER_FAMILY:
274 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
275 lyxrc.typewriter_font_foundry)));
282 font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
283 * lyxrc.zoom / 100.0);
285 switch (f.series()) {
286 case LyXFont::MEDIUM_SERIES:
287 font.setWeight(QFont::Normal);
289 case LyXFont::BOLD_SERIES:
290 font.setWeight(QFont::Bold);
296 switch (f.realShape()) {
297 case LyXFont::ITALIC_SHAPE:
298 case LyXFont::SLANTED_SHAPE:
299 font.setItalic(true);
305 if (lyxerr.debugging(Debug::FONT)) {
306 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
307 << "' matched by\n" << (const char *) font.rawName() << endl;
310 lyxerr[Debug::FONT] << "The font has size: "
311 << font.pointSizeFloat() << endl;
313 // Is this an exact match?
314 if (font.exactMatch())
315 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
317 lyxerr[Debug::FONT] << "This font is NOT an exact match"
320 lyxerr[Debug::FONT] << "XFLD: " << (const char *) font.rawName() << endl;
322 metrics = QFontMetrics(font);
326 int QLFontInfo::width(Uchar val) const
328 // Starting with version 3.1.0, Qt/X11 does its own caching of
329 // character width, so it is not necessary to provide ours.
330 #if defined (USE_LYX_FONTCACHE)
331 QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
332 if (cit != widthcache.end())
335 int const w = metrics.width(QChar(val));
339 return metrics.width(QChar(val));
344 bool FontLoader::available(LyXFont const & f)
346 if (!lyx_gui::use_gui)
349 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
350 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
352 LyXFont::FONT_FAMILY family = f.family();
353 if (cache_set[family])
354 return cache[family];
355 cache_set[family] = true;
357 string const pat = symbolFamily(family);
359 // We don't care about non-symbol fonts
362 pair<QFont, bool> tmp = getSymbolFont(pat);
366 cache[family] = true;