]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qfont_loader.C
95c703960eb4bca8266d93b790d1fd29ae9487dd
[lyx.git] / src / frontends / qt4 / qfont_loader.C
1 /**
2  * \file FontLoader.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "qfont_loader.h"
15 #include "qt_helpers.h"
16
17 #include "debug.h"
18 #include "lyxrc.h"
19
20 #include "frontends/lyx_gui.h"
21
22 #include "support/convert.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/systemcall.h"
26
27 #include <qfontinfo.h>
28
29 #include <boost/tuple/tuple.hpp>
30
31 #ifdef Q_WS_X11
32 #include <qwidget.h>
33 #include <X11/Xlib.h>
34 #include <algorithm>
35 #endif
36
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;
42
43 using std::endl;
44 using std::make_pair;
45
46 using std::pair;
47 using std::vector;
48 using std::string;
49
50 #ifdef Q_WS_MACX
51 #include <ApplicationServices/ApplicationServices.h>
52 #endif
53
54
55 void FontLoader::initFontPath()
56 {
57 #ifdef Q_WS_MACX
58         CFBundleRef  myAppBundle = CFBundleGetMainBundle();
59         CFURLRef  myAppResourcesURL, FontsURL;
60         FSRef  fontDirRef;
61         FSSpec  fontDirSpec;
62         CFStringRef  filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
63                                         (UInt8 *) "Fonts", strlen("Fonts"),
64                                         kCFStringEncodingISOLatin1, false);
65
66         myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
67         FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
68                         myAppResourcesURL, filePath, true);
69         if (lyxerr.debugging(Debug::FONT)) {
70                 UInt8  buf[255];
71                 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
72                         lyxerr << "Adding Fonts directory: " << buf << endl;
73         }
74         CFURLGetFSRef (FontsURL, &fontDirRef);
75         OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
76                                          NULL, NULL, &fontDirSpec, NULL);
77         if (err)
78                 lyxerr << "FSGetCatalogInfo err = " << err << endl;
79         err = FMActivateFonts (&fontDirSpec, NULL, NULL,
80                                kFMLocalActivationContext);
81         if (err)
82                 lyxerr << "FMActivateFonts err = " << err << endl;
83 #endif
84 }
85
86 namespace {
87
88 struct symbol_font {
89         LyXFont::FONT_FAMILY lyx_family;
90         string family;
91         string xlfd;
92 };
93
94 symbol_font symbol_fonts[] = {
95         { LyXFont::SYMBOL_FAMILY,
96                 "symbol",
97                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
98
99         { LyXFont::CMR_FAMILY,
100                 "cmr10",
101                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
102
103         { LyXFont::CMSY_FAMILY,
104                 "cmsy10",
105                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
106
107         { LyXFont::CMM_FAMILY,
108                 "cmmi10",
109                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
110
111         { LyXFont::CMEX_FAMILY,
112                 "cmex10",
113                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
114
115         { LyXFont::MSA_FAMILY,
116                 "msam10",
117                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
118
119         { LyXFont::MSB_FAMILY,
120                 "msbm10",
121                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
122
123         { LyXFont::EUFRAK_FAMILY,
124                 "eufm10",
125                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
126
127         { LyXFont::WASY_FAMILY,
128                 "wasy10",
129                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
130 };
131
132 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
133
134
135 string getRawName(string const & family)
136 {
137         for (size_t i = 0; i < nr_symbol_fonts; ++i)
138                 if (family == symbol_fonts[i].family)
139                         return symbol_fonts[i].xlfd;
140
141         lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
142         return string();
143 }
144
145
146 string const symbolFamily(LyXFont::FONT_FAMILY family)
147 {
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;
151         }
152         return string();
153 }
154
155
156 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
157 {
158         return family >= LyXFont::SYMBOL_FAMILY &&
159                 family <= LyXFont::WASY_FAMILY;
160 }
161
162
163 bool isChosenFont(QFont & font, string const & family)
164 {
165         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
166
167         QFontInfo fi(font);
168
169         // Note Qt lies about family quite often
170         lyxerr[Debug::FONT] << "alleged fi family: "
171                 << fromqstr(fi.family()) << endl;
172
173         // So we check rawName first
174         if (contains(fromqstr(font.rawName()), family)) {
175                 lyxerr[Debug::FONT] << " got it ";
176                 return true;
177         }
178
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) ";
184                         return true;
185                 }
186         }
187
188         return false;
189 }
190
191
192 pair<QFont, bool> const getSymbolFont(string const & family)
193 {
194         lyxerr[Debug::FONT] << "Looking for font family "
195                 << family << " ... ";
196         string upper = family;
197         upper[0] = toupper(family[0]);
198
199         QFont font;
200         font.setFamily(toqstr(family));
201
202         if (isChosenFont(font, family)) {
203                 lyxerr[Debug::FONT] << "normal!" << endl;
204                 return make_pair<QFont, bool>(font, true);
205         }
206
207         font.setFamily(toqstr(upper));
208
209         if (isChosenFont(font, upper)) {
210                 lyxerr[Debug::FONT] << "upper!" << endl;
211                 return make_pair<QFont, bool>(font, true);
212         }
213
214         // A simple setFamily() fails on Qt 2
215
216         font.setRawName(toqstr(getRawName(family)));
217
218         if (isChosenFont(font, family)) {
219                 lyxerr[Debug::FONT] << "raw version!" << endl;
220                 return make_pair<QFont, bool>(font, true);
221         }
222
223         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
224         return make_pair<QFont, bool>(font, false);
225 }
226
227 } // namespace anon
228
229
230 FontLoader::FontLoader()
231 {
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;
237 }
238
239
240 void FontLoader::update()
241 {
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;
248                                 }
249 }
250
251
252 /////////////////////////////////////////////////
253
254
255 QLFontInfo::QLFontInfo(LyXFont const & f)
256         : metrics(font)
257 {
258
259         string const pat = symbolFamily(f.family());
260         if (!pat.empty()) {
261                 bool tmp;
262                 boost::tie(font, tmp) = getSymbolFont(pat);
263         } else {
264                 switch (f.family()) {
265                 case LyXFont::ROMAN_FAMILY:
266                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
267                                                     lyxrc.roman_font_foundry)));
268                         break;
269                 case LyXFont::SANS_FAMILY:
270                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
271                                                     lyxrc.sans_font_foundry)));
272                         break;
273                 case LyXFont::TYPEWRITER_FAMILY:
274                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
275                                                     lyxrc.typewriter_font_foundry)));
276                         break;
277                 default:
278                         break;
279                 }
280         }
281
282         font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
283                                * lyxrc.zoom / 100.0);
284
285         switch (f.series()) {
286                 case LyXFont::MEDIUM_SERIES:
287                         font.setWeight(QFont::Normal);
288                         break;
289                 case LyXFont::BOLD_SERIES:
290                         font.setWeight(QFont::Bold);
291                         break;
292                 default:
293                         break;
294         }
295
296         switch (f.realShape()) {
297                 case LyXFont::ITALIC_SHAPE:
298                 case LyXFont::SLANTED_SHAPE:
299                         font.setItalic(true);
300                         break;
301                 default:
302                         break;
303         }
304
305         if (lyxerr.debugging(Debug::FONT)) {
306                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
307                         << "' matched by\n" << (const char *) font.rawName() << endl;
308         }
309
310         lyxerr[Debug::FONT] << "The font has size: "
311                             << font.pointSizeFloat() << endl;
312
313         // Is this an exact match?
314         if (font.exactMatch())
315                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
316         else
317                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
318                                     << endl;
319
320         lyxerr[Debug::FONT] << "XFLD: " << (const char *) font.rawName() << endl;
321
322         metrics = QFontMetrics(font);
323 }
324
325
326 int QLFontInfo::width(Uchar val) const
327 {
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())
333                 return cit->second;
334
335         int const w = metrics.width(QChar(val));
336         widthcache[val] = w;
337         return w;
338 #else
339         return metrics.width(QChar(val));
340 #endif
341 }
342
343
344 bool FontLoader::available(LyXFont const & f)
345 {
346         if (!lyx_gui::use_gui)
347                 return false;
348
349         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
350         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
351
352         LyXFont::FONT_FAMILY family = f.family();
353         if (cache_set[family])
354                 return cache[family];
355         cache_set[family] = true;
356
357         string const pat = symbolFamily(family);
358         if (pat.empty())
359                 // We don't care about non-symbol fonts
360                 return false;
361
362         pair<QFont, bool> tmp = getSymbolFont(pat);
363         if (!tmp.second)
364                 return false;
365
366         cache[family] = true;
367         return true;
368 }