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 if (font.rawName() == "xft") {
181 if (contains(fromqstr(fi.family()), family)) {
182 lyxerr[Debug::FONT] << " got it (Xft) ";
191 pair<QFont, bool> const getSymbolFont(string const & family)
193 lyxerr[Debug::FONT] << "Looking for font family "
194 << family << " ... ";
195 string upper = family;
196 upper[0] = toupper(family[0]);
199 font.setFamily(toqstr(family));
201 if (isChosenFont(font, family)) {
202 lyxerr[Debug::FONT] << "normal!" << endl;
203 return make_pair<QFont, bool>(font, true);
206 font.setFamily(toqstr(upper));
208 if (isChosenFont(font, upper)) {
209 lyxerr[Debug::FONT] << "upper!" << endl;
210 return make_pair<QFont, bool>(font, true);
213 // A simple setFamily() fails on Qt 2
215 font.setRawName(toqstr(getRawName(family)));
217 if (isChosenFont(font, family)) {
218 lyxerr[Debug::FONT] << "raw version!" << endl;
219 return make_pair<QFont, bool>(font, true);
222 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
223 return make_pair<QFont, bool>(font, false);
229 FontLoader::FontLoader()
231 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
232 for (int i2 = 0; i2 < 2; ++i2)
233 for (int i3 = 0; i3 < 4; ++i3)
234 for (int i4 = 0; i4 < 10; ++i4)
235 fontinfo_[i1][i2][i3][i4] = 0;
239 void FontLoader::update()
241 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
242 for (int i2 = 0; i2 < 2; ++i2)
243 for (int i3 = 0; i3 < 4; ++i3)
244 for (int i4 = 0; i4 < 10; ++i4) {
245 delete fontinfo_[i1][i2][i3][i4];
246 fontinfo_[i1][i2][i3][i4] = 0;
251 /////////////////////////////////////////////////
254 QLFontInfo::QLFontInfo(LyXFont const & f)
258 string const pat = symbolFamily(f.family());
261 boost::tie(font, tmp) = getSymbolFont(pat);
263 switch (f.family()) {
264 case LyXFont::ROMAN_FAMILY:
265 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
266 lyxrc.roman_font_foundry)));
268 case LyXFont::SANS_FAMILY:
269 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
270 lyxrc.sans_font_foundry)));
272 case LyXFont::TYPEWRITER_FAMILY:
273 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
274 lyxrc.typewriter_font_foundry)));
281 font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
282 * lyxrc.zoom / 100.0);
284 switch (f.series()) {
285 case LyXFont::MEDIUM_SERIES:
286 font.setWeight(QFont::Normal);
288 case LyXFont::BOLD_SERIES:
289 font.setWeight(QFont::Bold);
295 switch (f.realShape()) {
296 case LyXFont::ITALIC_SHAPE:
297 case LyXFont::SLANTED_SHAPE:
298 font.setItalic(true);
304 if (lyxerr.debugging(Debug::FONT)) {
305 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
306 << "' matched by\n" << (const char *) font.rawName() << endl;
309 lyxerr[Debug::FONT] << "The font has size: "
310 << font.pointSizeFloat() << endl;
312 // Is this an exact match?
313 if (font.exactMatch())
314 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
316 lyxerr[Debug::FONT] << "This font is NOT an exact match"
319 lyxerr[Debug::FONT] << "XFLD: " << (const char *) font.rawName() << endl;
321 metrics = QFontMetrics(font);
325 int QLFontInfo::width(Uchar val) const
327 // Starting with version 3.1.0, Qt/X11 does its own caching of
328 // character width, so it is not necessary to provide ours.
329 #if defined (USE_LYX_FONTCACHE)
330 QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
331 if (cit != widthcache.end())
334 int const w = metrics.width(QChar(val));
338 return metrics.width(QChar(val));
343 bool FontLoader::available(LyXFont const & f)
345 if (!lyx_gui::use_gui)
348 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
349 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
351 LyXFont::FONT_FAMILY family = f.family();
352 if (cache_set[family])
353 return cache[family];
354 cache_set[family] = true;
356 string const pat = symbolFamily(family);
358 // We don't care about non-symbol fonts
361 pair<QFont, bool> tmp = getSymbolFont(pat);
365 cache[family] = true;