]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_loader.C
the convert patch
[lyx.git] / src / frontends / qt2 / qfont_loader.C
1 /**
2  * \file qfont_loader.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
56 void qfont_loader::addToFontPath()
57 {
58 #ifdef Q_WS_X11
59         string const dir =  OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
60         if (!dir.empty()) {
61                 QWidget w;
62                 int n;
63                 char ** p = XGetFontPath(w.x11Display(), &n);
64                 if (std::find(p, p + n, dir) != p + n)
65                         return;
66                 lyxerr[Debug::FONT] << "Adding " << dir
67                                     << " to the font path." << endl;
68                 string const command = "xset fp+ " + QuoteName(dir);
69                 Systemcall s;
70                 if (!s.startscript(Systemcall::Wait, command))
71                         return;
72                 lyxerr << "Unable to add " << dir << "to the font path."
73                        << endl;
74         }
75 #endif
76 }
77
78 void qfont_loader::initFontPath()
79 {
80 #ifdef Q_WS_MACX
81         CFBundleRef  myAppBundle = CFBundleGetMainBundle();
82         CFURLRef  myAppResourcesURL, FontsURL;
83         FSRef  fontDirRef;
84         FSSpec  fontDirSpec;
85         CFStringRef  filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
86                                         (UInt8 *) "Fonts", strlen("Fonts"),
87                                         kCFStringEncodingISOLatin1, false);
88
89         myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
90         FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
91                         myAppResourcesURL, filePath, true);
92         if (lyxerr.debugging(Debug::FONT)) {
93                 UInt8  buf[255];
94                 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
95                         lyxerr << "Adding Fonts directory: " << buf << endl;
96         }
97         CFURLGetFSRef (FontsURL, &fontDirRef);
98         OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
99                                          NULL, NULL, &fontDirSpec, NULL);
100         if (err)
101                 lyxerr << "FSGetCatalogInfo err = " << err << endl;
102         err = FMActivateFonts (&fontDirSpec, NULL, NULL,
103                                kFMLocalActivationContext);
104         if (err)
105                 lyxerr << "FMActivateFonts err = " << err << endl;
106 #endif
107 }
108
109 namespace {
110
111 struct symbol_font {
112         LyXFont::FONT_FAMILY lyx_family;
113         string family;
114         string xlfd;
115 };
116
117 symbol_font symbol_fonts[] = {
118         { LyXFont::SYMBOL_FAMILY,
119                 "symbol",
120                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
121
122         { LyXFont::CMR_FAMILY,
123                 "cmr10",
124                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
125
126         { LyXFont::CMSY_FAMILY,
127                 "cmsy10",
128                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
129
130         { LyXFont::CMM_FAMILY,
131                 "cmmi10",
132                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
133
134         { LyXFont::CMEX_FAMILY,
135                 "cmex10",
136                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
137
138         { LyXFont::MSA_FAMILY,
139                 "msam10",
140                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
141
142         { LyXFont::MSB_FAMILY,
143                 "msbm10",
144                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
145
146         { LyXFont::EUFRAK_FAMILY,
147                 "eufm10",
148                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
149
150         { LyXFont::WASY_FAMILY,
151                 "wasy10",
152                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
153 };
154
155 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
156
157
158 string getRawName(string const & family)
159 {
160         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
161                 if (family == symbol_fonts[i].family)
162                         return symbol_fonts[i].xlfd;
163         }
164         lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
165         return string();
166 }
167
168
169 string const symbolFamily(LyXFont::FONT_FAMILY family)
170 {
171         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
172                 if (family == symbol_fonts[i].lyx_family)
173                         return symbol_fonts[i].family;
174         }
175         return string();
176 }
177
178
179 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
180 {
181         return family >= LyXFont::SYMBOL_FAMILY &&
182                 family <= LyXFont::WASY_FAMILY;
183 }
184
185
186 bool isChosenFont(QFont & font, string const & family)
187 {
188         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
189
190         QFontInfo fi(font);
191
192         // Note Qt lies about family quite often
193         lyxerr[Debug::FONT] << "alleged fi family: "
194                 << fromqstr(fi.family()) << endl;
195
196         // So we check rawName first
197         if (contains(fromqstr(font.rawName()), family)) {
198                 lyxerr[Debug::FONT] << " got it ";
199                 return true;
200         }
201
202         // Qt 3.2 beta1 returns "xft" for all xft fonts
203         if (font.rawName() == "xft") {
204                 if (contains(fromqstr(fi.family()), family)) {
205                         lyxerr[Debug::FONT] << " got it (Xft) ";
206                         return true;
207                 }
208         }
209
210         return false;
211 }
212
213
214 pair<QFont, bool> const getSymbolFont(string const & family)
215 {
216         lyxerr[Debug::FONT] << "Looking for font family "
217                 << family << " ... ";
218         string upper = family;
219         upper[0] = toupper(family[0]);
220
221         QFont font;
222         font.setFamily(toqstr(family));
223
224         if (isChosenFont(font, family)) {
225                 lyxerr[Debug::FONT] << "normal!" << endl;
226                 return make_pair<QFont, bool>(font, true);
227         }
228
229         font.setFamily(toqstr(upper));
230
231         if (isChosenFont(font, upper)) {
232                 lyxerr[Debug::FONT] << "upper!" << endl;
233                 return make_pair<QFont, bool>(font, true);
234         }
235
236         // A simple setFamily() fails on Qt 2
237
238         font.setRawName(toqstr(getRawName(family)));
239
240         if (isChosenFont(font, family)) {
241                 lyxerr[Debug::FONT] << "raw version!" << endl;
242                 return make_pair<QFont, bool>(font, true);
243         }
244
245         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
246         return make_pair<QFont, bool>(font, false);
247 }
248
249 } // namespace anon
250
251
252 qfont_loader::qfont_loader()
253 {
254         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
255                 for (int i2 = 0; i2 < 2; ++i2) {
256                         for (int i3 = 0; i3 < 4; ++i3) {
257                                 for (int i4 = 0; i4 < 10; ++i4) {
258                                         fontinfo_[i1][i2][i3][i4] = 0;
259                                 }
260                         }
261                 }
262         }
263 }
264
265
266 qfont_loader::~qfont_loader()
267 {
268 }
269
270
271 void qfont_loader::update()
272 {
273         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
274                 for (int i2 = 0; i2 < 2; ++i2) {
275                         for (int i3 = 0; i3 < 4; ++i3) {
276                                 for (int i4 = 0; i4 < 10; ++i4) {
277                                         delete fontinfo_[i1][i2][i3][i4];
278                                         fontinfo_[i1][i2][i3][i4] = 0;
279                                 }
280                         }
281                 }
282         }
283 }
284
285
286 QFont const & qfont_loader::get(LyXFont const & f)
287 {
288         return getfontinfo(f)->font;
289 }
290
291
292 qfont_loader::font_info::font_info(LyXFont const & f)
293         : metrics(font)
294 {
295
296         string const pat = symbolFamily(f.family());
297         if (!pat.empty()) {
298                 bool tmp;
299                 boost::tie(font, tmp) = getSymbolFont(pat);
300         } else {
301                 switch (f.family()) {
302                 case LyXFont::ROMAN_FAMILY:
303                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
304                                                     lyxrc.roman_font_foundry)));
305                         break;
306                 case LyXFont::SANS_FAMILY:
307                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
308                                                     lyxrc.sans_font_foundry)));
309                         break;
310                 case LyXFont::TYPEWRITER_FAMILY:
311                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
312                                                     lyxrc.typewriter_font_foundry)));
313                         break;
314                 default:
315                         break;
316                 }
317         }
318
319         font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
320                                * lyxrc.zoom / 100.0);
321
322         switch (f.series()) {
323                 case LyXFont::MEDIUM_SERIES:
324                         font.setWeight(QFont::Normal);
325                         break;
326                 case LyXFont::BOLD_SERIES:
327                         font.setWeight(QFont::Bold);
328                         break;
329                 default:
330                         break;
331         }
332
333         switch (f.realShape()) {
334                 case LyXFont::ITALIC_SHAPE:
335                 case LyXFont::SLANTED_SHAPE:
336                         font.setItalic(true);
337                         break;
338                 default:
339                         break;
340         }
341
342         if (lyxerr.debugging(Debug::FONT)) {
343                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
344                         << "' matched by\n" << font.rawName() << endl;
345         }
346
347         lyxerr[Debug::FONT] << "The font has size: "
348                             << font.pointSizeFloat() << endl;
349
350         // Is this an exact match?
351         if (font.exactMatch()) {
352                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
353         } else {
354                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
355                                     << endl;
356         }
357
358         lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
359
360         metrics = QFontMetrics(font);
361 }
362
363
364 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
365 {
366         font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
367         if (fi)
368                 return fi;
369
370         font_info * fi2 = new font_info(f);
371         fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
372         return fi2;
373 }
374
375
376 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
377 {
378         font_info * fi = getfontinfo(f);
379
380         font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
381         if (cit != fi->widthcache.end())
382                 return cit->second;
383
384         int const w = fi->metrics.width(QChar(val));
385         fi->widthcache[val] = w;
386         return w;
387 }
388
389
390 bool qfont_loader::available(LyXFont const & f)
391 {
392         if (!lyx_gui::use_gui)
393                 return false;
394
395         static vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
396         static vector<bool> cache(LyXFont::NUM_FAMILIES, false);
397
398         LyXFont::FONT_FAMILY family = f.family();
399         if (cache_set[family])
400                 return cache[family];
401         cache_set[family] = true;
402
403         string const pat = symbolFamily(family);
404         if (!pat.empty()) {
405                 pair<QFont, bool> tmp = getSymbolFont(pat);
406                 if (tmp.second) {
407                         cache[family] = true;
408                         return true;
409                 }
410
411                 // If the font is a tex symbol font and it is not available,
412                 // we try to add the xfonts directory to the font path.
413                 static bool first_time = true;
414                 if (!first_time || family == LyXFont::SYMBOL_FAMILY
415                     || family == LyXFont::WASY_FAMILY)
416                         return false;
417
418                 first_time = false;
419                 addToFontPath();
420                 tmp = getSymbolFont(pat);
421                 if (tmp.second) {
422                         cache[family] = true;
423                         return true;
424                 }
425                 // We don't need to set cache[family] to false, as it
426                 //is initialized to false;
427                 return false;
428         }
429
430         // We don't care about non-symbol fonts
431         return false;
432 }