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;
47 #include <ApplicationServices/ApplicationServices.h>
51 void FontLoader::initFontPath()
54 CFBundleRef myAppBundle = CFBundleGetMainBundle();
55 CFURLRef myAppResourcesURL, FontsURL;
58 CFStringRef filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
59 (UInt8 *) "Fonts", strlen("Fonts"),
60 kCFStringEncodingISOLatin1, false);
62 myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
63 FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
64 myAppResourcesURL, filePath, true);
65 if (lyxerr.debugging(Debug::FONT)) {
67 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
68 lyxerr << "Adding Fonts directory: " << buf << endl;
70 CFURLGetFSRef (FontsURL, &fontDirRef);
71 OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
72 NULL, NULL, &fontDirSpec, NULL);
74 lyxerr << "FSGetCatalogInfo err = " << err << endl;
75 err = FMActivateFonts (&fontDirSpec, NULL, NULL,
76 kFMLocalActivationContext);
78 lyxerr << "FMActivateFonts err = " << err << endl;
85 LyXFont::FONT_FAMILY lyx_family;
90 symbol_font symbol_fonts[] = {
91 { LyXFont::SYMBOL_FAMILY,
93 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
95 { LyXFont::CMR_FAMILY,
97 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
99 { LyXFont::CMSY_FAMILY,
101 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
103 { LyXFont::CMM_FAMILY,
105 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
107 { LyXFont::CMEX_FAMILY,
109 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
111 { LyXFont::MSA_FAMILY,
113 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
115 { LyXFont::MSB_FAMILY,
117 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
119 { LyXFont::EUFRAK_FAMILY,
121 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
123 { LyXFont::WASY_FAMILY,
125 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
128 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
131 string getRawName(string const & family)
133 for (size_t i = 0; i < nr_symbol_fonts; ++i)
134 if (family == symbol_fonts[i].family)
135 return symbol_fonts[i].xlfd;
137 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
142 string const symbolFamily(LyXFont::FONT_FAMILY family)
144 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
145 if (family == symbol_fonts[i].lyx_family)
146 return symbol_fonts[i].family;
152 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
154 return family >= LyXFont::SYMBOL_FAMILY &&
155 family <= LyXFont::WASY_FAMILY;
159 bool isChosenFont(QFont & font, string const & family)
161 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
165 // Note Qt lies about family quite often
166 lyxerr[Debug::FONT] << "alleged fi family: "
167 << fromqstr(fi.family()) << endl;
169 // So we check rawName first
170 if (contains(fromqstr(font.rawName()), family)) {
171 lyxerr[Debug::FONT] << " got it ";
175 // Qt 3.2 beta1 returns "xft" for all xft fonts
176 // Qt 4.1 returns "Multi" for all ? xft fonts
177 if (font.rawName() == "xft" || font.rawName() == "Multi") {
178 if (contains(fromqstr(fi.family()), family)) {
179 lyxerr[Debug::FONT] << " got it (Xft) ";
188 pair<QFont, bool> const getSymbolFont(string const & family)
190 lyxerr[Debug::FONT] << "Looking for font family "
191 << family << " ... ";
192 string upper = family;
193 upper[0] = toupper(family[0]);
196 font.setFamily(toqstr(family));
198 if (isChosenFont(font, family)) {
199 lyxerr[Debug::FONT] << "normal!" << endl;
200 return make_pair<QFont, bool>(font, true);
203 font.setFamily(toqstr(upper));
205 if (isChosenFont(font, upper)) {
206 lyxerr[Debug::FONT] << "upper!" << endl;
207 return make_pair<QFont, bool>(font, true);
210 // A simple setFamily() fails on Qt 2
212 font.setRawName(toqstr(getRawName(family)));
214 if (isChosenFont(font, family)) {
215 lyxerr[Debug::FONT] << "raw version!" << endl;
216 return make_pair<QFont, bool>(font, true);
219 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
220 return make_pair<QFont, bool>(font, false);
226 FontLoader::FontLoader()
228 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
229 for (int i2 = 0; i2 < 2; ++i2)
230 for (int i3 = 0; i3 < 4; ++i3)
231 for (int i4 = 0; i4 < 10; ++i4)
232 fontinfo_[i1][i2][i3][i4] = 0;
236 void FontLoader::update()
238 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
239 for (int i2 = 0; i2 < 2; ++i2)
240 for (int i3 = 0; i3 < 4; ++i3)
241 for (int i4 = 0; i4 < 10; ++i4) {
242 delete fontinfo_[i1][i2][i3][i4];
243 fontinfo_[i1][i2][i3][i4] = 0;
248 /////////////////////////////////////////////////
251 QLFontInfo::QLFontInfo(LyXFont const & f)
255 string const pat = symbolFamily(f.family());
258 boost::tie(font, tmp) = getSymbolFont(pat);
260 switch (f.family()) {
261 case LyXFont::ROMAN_FAMILY:
262 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
263 lyxrc.roman_font_foundry)));
265 case LyXFont::SANS_FAMILY:
266 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
267 lyxrc.sans_font_foundry)));
269 case LyXFont::TYPEWRITER_FAMILY:
270 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
271 lyxrc.typewriter_font_foundry)));
278 font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
279 * lyxrc.zoom / 100.0);
281 switch (f.series()) {
282 case LyXFont::MEDIUM_SERIES:
283 font.setWeight(QFont::Normal);
285 case LyXFont::BOLD_SERIES:
286 font.setWeight(QFont::Bold);
292 switch (f.realShape()) {
293 case LyXFont::ITALIC_SHAPE:
294 case LyXFont::SLANTED_SHAPE:
295 font.setItalic(true);
301 if (lyxerr.debugging(Debug::FONT)) {
302 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
303 << "' matched by\n" << (const char *) font.rawName() << endl;
306 lyxerr[Debug::FONT] << "The font has size: "
307 << font.pointSizeFloat() << endl;
309 // Is this an exact match?
310 if (font.exactMatch())
311 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
313 lyxerr[Debug::FONT] << "This font is NOT an exact match"
316 lyxerr[Debug::FONT] << "XFLD: " << (const char *) font.rawName() << endl;
318 metrics = QFontMetrics(font);
322 int QLFontInfo::width(Uchar val) const
324 // Starting with version 3.1.0, Qt/X11 does its own caching of
325 // character width, so it is not necessary to provide ours.
326 #if defined (USE_LYX_FONTCACHE)
327 QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
328 if (cit != widthcache.end())
331 int const w = metrics.width(QChar(val));
335 return metrics.width(QChar(val));
340 bool FontLoader::available(LyXFont const & f)
342 if (!lyx_gui::use_gui)
345 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
346 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
348 LyXFont::FONT_FAMILY family = f.family();
349 if (cache_set[family])
350 return cache[family];
351 cache_set[family] = true;
353 string const pat = symbolFamily(family);
355 // We don't care about non-symbol fonts
358 pair<QFont, bool> tmp = getSymbolFont(pat);
362 cache[family] = true;