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