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>
56 #include "support/os.h"
57 #include "support/package.h"
58 #include "support/path.h"
59 using lyx::support::addName;
60 using lyx::support::addPath;
61 using lyx::support::package;
62 namespace os = lyx::support::os;
63 string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
64 "eufm10", "msam10", "msbm10", "wasy10"};
65 const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype);
68 void FontLoader::initFontPath()
71 CFBundleRef myAppBundle = CFBundleGetMainBundle();
72 CFURLRef myAppResourcesURL, FontsURL;
75 CFStringRef filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
76 (UInt8 *) "Fonts", strlen("Fonts"),
77 kCFStringEncodingISOLatin1, false);
79 myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
80 FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
81 myAppResourcesURL, filePath, true);
82 if (lyxerr.debugging(Debug::FONT)) {
84 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
85 lyxerr << "Adding Fonts directory: " << buf << endl;
87 CFURLGetFSRef (FontsURL, &fontDirRef);
88 OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
89 NULL, NULL, &fontDirSpec, NULL);
91 lyxerr << "FSGetCatalogInfo err = " << err << endl;
92 err = FMActivateFonts (&fontDirSpec, NULL, NULL,
93 kFMLocalActivationContext);
95 lyxerr << "FMActivateFonts err = " << err << endl;
99 // Windows only: Add BaKoMa TrueType font resources
100 string const fonts_dir = addPath(package().system_support(), "fonts");
102 for (int i = 0 ; i < num_fonts_truetype ; ++i) {
103 string const font_current =
104 addName(fonts_dir, win_fonts_truetype[i] + ".ttf");
105 AddFontResource(os::external_path(font_current).c_str());
110 FontLoader::~FontLoader() {
112 // Windows only: Remove BaKoMa TrueType font resources
113 string const fonts_dir = addPath(package().system_support(), "fonts");
115 for(int i = 0 ; i < num_fonts_truetype ; ++i) {
116 string const font_current =
117 addName(fonts_dir, win_fonts_truetype[i] + ".ttf");
118 RemoveFontResource(os::external_path(font_current).c_str());
126 LyXFont::FONT_FAMILY lyx_family;
131 symbol_font symbol_fonts[] = {
132 { LyXFont::SYMBOL_FAMILY,
134 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
136 { LyXFont::CMR_FAMILY,
138 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
140 { LyXFont::CMSY_FAMILY,
142 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
144 { LyXFont::CMM_FAMILY,
146 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
148 { LyXFont::CMEX_FAMILY,
150 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
152 { LyXFont::MSA_FAMILY,
154 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
156 { LyXFont::MSB_FAMILY,
158 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
160 { LyXFont::EUFRAK_FAMILY,
162 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
164 { LyXFont::WASY_FAMILY,
166 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
169 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
172 string getRawName(string const & family)
174 for (size_t i = 0; i < nr_symbol_fonts; ++i)
175 if (family == symbol_fonts[i].family)
176 return symbol_fonts[i].xlfd;
178 lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
183 string const symbolFamily(LyXFont::FONT_FAMILY family)
185 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
186 if (family == symbol_fonts[i].lyx_family)
187 return symbol_fonts[i].family;
193 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
195 return family >= LyXFont::SYMBOL_FAMILY &&
196 family <= LyXFont::WASY_FAMILY;
200 bool isChosenFont(QFont & font, string const & family)
202 lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
206 // Note Qt lies about family quite often
207 lyxerr[Debug::FONT] << "alleged fi family: "
208 << fromqstr(fi.family()) << endl;
210 // So we check rawName first
211 if (contains(fromqstr(font.rawName()), family)) {
212 lyxerr[Debug::FONT] << " got it ";
216 // Qt 3.2 beta1 returns "xft" for all xft fonts
217 if (font.rawName() == "xft") {
218 if (contains(fromqstr(fi.family()), family)) {
219 lyxerr[Debug::FONT] << " got it (Xft) ";
228 pair<QFont, bool> const getSymbolFont(string const & family)
230 lyxerr[Debug::FONT] << "Looking for font family "
231 << family << " ... ";
232 string upper = family;
233 upper[0] = toupper(family[0]);
236 font.setFamily(toqstr(family));
238 if (isChosenFont(font, family)) {
239 lyxerr[Debug::FONT] << "normal!" << endl;
240 return make_pair<QFont, bool>(font, true);
243 font.setFamily(toqstr(upper));
245 if (isChosenFont(font, upper)) {
246 lyxerr[Debug::FONT] << "upper!" << endl;
247 return make_pair<QFont, bool>(font, true);
250 // A simple setFamily() fails on Qt 2
252 font.setRawName(toqstr(getRawName(family)));
254 if (isChosenFont(font, family)) {
255 lyxerr[Debug::FONT] << "raw version!" << endl;
256 return make_pair<QFont, bool>(font, true);
259 lyxerr[Debug::FONT] << " FAILED :-(" << endl;
260 return make_pair<QFont, bool>(font, false);
266 FontLoader::FontLoader()
268 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
269 for (int i2 = 0; i2 < 2; ++i2)
270 for (int i3 = 0; i3 < 4; ++i3)
271 for (int i4 = 0; i4 < 10; ++i4)
272 fontinfo_[i1][i2][i3][i4] = 0;
276 void FontLoader::update()
278 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
279 for (int i2 = 0; i2 < 2; ++i2)
280 for (int i3 = 0; i3 < 4; ++i3)
281 for (int i4 = 0; i4 < 10; ++i4) {
282 delete fontinfo_[i1][i2][i3][i4];
283 fontinfo_[i1][i2][i3][i4] = 0;
288 /////////////////////////////////////////////////
291 QLFontInfo::QLFontInfo(LyXFont const & f)
295 string const pat = symbolFamily(f.family());
298 boost::tie(font, tmp) = getSymbolFont(pat);
300 switch (f.family()) {
301 case LyXFont::ROMAN_FAMILY:
302 font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
303 lyxrc.roman_font_foundry)));
305 case LyXFont::SANS_FAMILY:
306 font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
307 lyxrc.sans_font_foundry)));
309 case LyXFont::TYPEWRITER_FAMILY:
310 font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
311 lyxrc.typewriter_font_foundry)));
318 font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
319 * lyxrc.zoom / 100.0);
321 switch (f.series()) {
322 case LyXFont::MEDIUM_SERIES:
323 font.setWeight(QFont::Normal);
325 case LyXFont::BOLD_SERIES:
326 font.setWeight(QFont::Bold);
332 switch (f.realShape()) {
333 case LyXFont::ITALIC_SHAPE:
334 case LyXFont::SLANTED_SHAPE:
335 font.setItalic(true);
341 if (lyxerr.debugging(Debug::FONT)) {
342 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
343 << "' matched by\n" << font.rawName() << endl;
346 lyxerr[Debug::FONT] << "The font has size: "
347 << font.pointSizeFloat() << endl;
349 // Is this an exact match?
350 if (font.exactMatch())
351 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
353 lyxerr[Debug::FONT] << "This font is NOT an exact match"
356 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
358 metrics = QFontMetrics(font);
362 int QLFontInfo::width(Uchar val)
364 // Starting with version 3.1.0, Qt/X11 does its own caching of
365 // character width, so it is not necessary to provide ours.
366 #if defined (USE_LYX_FONTCACHE)
367 QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
368 if (cit != widthcache.end())
371 int const w = metrics.width(QChar(val));
375 return metrics.width(QChar(val));
380 bool FontLoader::available(LyXFont const & f)
382 if (!lyx_gui::use_gui)
385 static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
386 static vector<int> cache(LyXFont::NUM_FAMILIES, false);
388 LyXFont::FONT_FAMILY family = f.family();
389 if (cache_set[family])
390 return cache[family];
391 cache_set[family] = true;
393 string const pat = symbolFamily(family);
395 // We don't care about non-symbol fonts
398 pair<QFont, bool> tmp = getSymbolFont(pat);
402 cache[family] = true;