]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/qfont_loader.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / 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 #ifdef Q_WS_WIN
55 #include "windows.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);
66 #endif
67
68 void FontLoader::initFontPath()
69 {
70 #ifdef Q_WS_MACX
71         CFBundleRef  myAppBundle = CFBundleGetMainBundle();
72         CFURLRef  myAppResourcesURL, FontsURL;
73         FSRef  fontDirRef;
74         FSSpec  fontDirSpec;
75         CFStringRef  filePath = CFStringCreateWithBytes(kCFAllocatorDefault,
76                                         (UInt8 *) "Fonts", strlen("Fonts"),
77                                         kCFStringEncodingISOLatin1, false);
78
79         myAppResourcesURL = CFBundleCopyResourcesDirectoryURL(myAppBundle);
80         FontsURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
81                         myAppResourcesURL, filePath, true);
82         if (lyxerr.debugging(Debug::FONT)) {
83                 UInt8  buf[255];
84                 if (CFURLGetFileSystemRepresentation(FontsURL, true, buf, 255))
85                         lyxerr << "Adding Fonts directory: " << buf << endl;
86         }
87         CFURLGetFSRef (FontsURL, &fontDirRef);
88         OSStatus err = FSGetCatalogInfo (&fontDirRef, kFSCatInfoNone,
89                                          NULL, NULL, &fontDirSpec, NULL);
90         if (err)
91                 lyxerr << "FSGetCatalogInfo err = " << err << endl;
92         err = FMActivateFonts (&fontDirSpec, NULL, NULL,
93                                kFMLocalActivationContext);
94         if (err)
95                 lyxerr << "FMActivateFonts err = " << err << endl;
96 #endif
97
98 #ifdef Q_WS_WIN
99         // Windows only: Add BaKoMa TrueType font resources
100         string const fonts_dir = addPath(package().system_support(), "fonts");
101         
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());
106         }
107 #endif
108 }
109
110 FontLoader::~FontLoader() {
111 #ifdef Q_WS_WIN
112         // Windows only: Remove BaKoMa TrueType font resources
113         string const fonts_dir = addPath(package().system_support(), "fonts");
114         
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());
119         }
120 #endif
121 }
122
123 namespace {
124
125 struct symbol_font {
126         LyXFont::FONT_FAMILY lyx_family;
127         string family;
128         string xlfd;
129 };
130
131 symbol_font symbol_fonts[] = {
132         { LyXFont::SYMBOL_FAMILY,
133                 "symbol",
134                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
135
136         { LyXFont::CMR_FAMILY,
137                 "cmr10",
138                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
139
140         { LyXFont::CMSY_FAMILY,
141                 "cmsy10",
142                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
143
144         { LyXFont::CMM_FAMILY,
145                 "cmmi10",
146                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
147
148         { LyXFont::CMEX_FAMILY,
149                 "cmex10",
150                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
151
152         { LyXFont::MSA_FAMILY,
153                 "msam10",
154                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
155
156         { LyXFont::MSB_FAMILY,
157                 "msbm10",
158                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
159
160         { LyXFont::EUFRAK_FAMILY,
161                 "eufm10",
162                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
163
164         { LyXFont::WASY_FAMILY,
165                 "wasy10",
166                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
167 };
168
169 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
170
171
172 string getRawName(string const & family)
173 {
174         for (size_t i = 0; i < nr_symbol_fonts; ++i)
175                 if (family == symbol_fonts[i].family)
176                         return symbol_fonts[i].xlfd;
177
178         lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
179         return string();
180 }
181
182
183 string const symbolFamily(LyXFont::FONT_FAMILY family)
184 {
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;
188         }
189         return string();
190 }
191
192
193 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
194 {
195         return family >= LyXFont::SYMBOL_FAMILY &&
196                 family <= LyXFont::WASY_FAMILY;
197 }
198
199
200 bool isChosenFont(QFont & font, string const & family)
201 {
202         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
203
204         QFontInfo fi(font);
205
206         // Note Qt lies about family quite often
207         lyxerr[Debug::FONT] << "alleged fi family: "
208                 << fromqstr(fi.family()) << endl;
209
210         // So we check rawName first
211         if (contains(fromqstr(font.rawName()), family)) {
212                 lyxerr[Debug::FONT] << " got it ";
213                 return true;
214         }
215
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) ";
220                         return true;
221                 }
222         }
223
224         return false;
225 }
226
227
228 pair<QFont, bool> const getSymbolFont(string const & family)
229 {
230         lyxerr[Debug::FONT] << "Looking for font family "
231                 << family << " ... ";
232         string upper = family;
233         upper[0] = toupper(family[0]);
234
235         QFont font;
236         font.setFamily(toqstr(family));
237
238         if (isChosenFont(font, family)) {
239                 lyxerr[Debug::FONT] << "normal!" << endl;
240                 return make_pair<QFont, bool>(font, true);
241         }
242
243         font.setFamily(toqstr(upper));
244
245         if (isChosenFont(font, upper)) {
246                 lyxerr[Debug::FONT] << "upper!" << endl;
247                 return make_pair<QFont, bool>(font, true);
248         }
249
250         // A simple setFamily() fails on Qt 2
251
252         font.setRawName(toqstr(getRawName(family)));
253
254         if (isChosenFont(font, family)) {
255                 lyxerr[Debug::FONT] << "raw version!" << endl;
256                 return make_pair<QFont, bool>(font, true);
257         }
258
259         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
260         return make_pair<QFont, bool>(font, false);
261 }
262
263 } // namespace anon
264
265
266 FontLoader::FontLoader()
267 {
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;
273 }
274
275
276 void FontLoader::update()
277 {
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;
284                                 }
285 }
286
287
288 /////////////////////////////////////////////////
289
290
291 QLFontInfo::QLFontInfo(LyXFont const & f)
292         : metrics(font)
293 {
294
295         string const pat = symbolFamily(f.family());
296         if (!pat.empty()) {
297                 bool tmp;
298                 boost::tie(font, tmp) = getSymbolFont(pat);
299         } else {
300                 switch (f.family()) {
301                 case LyXFont::ROMAN_FAMILY:
302                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
303                                                     lyxrc.roman_font_foundry)));
304                         break;
305                 case LyXFont::SANS_FAMILY:
306                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
307                                                     lyxrc.sans_font_foundry)));
308                         break;
309                 case LyXFont::TYPEWRITER_FAMILY:
310                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
311                                                     lyxrc.typewriter_font_foundry)));
312                         break;
313                 default:
314                         break;
315                 }
316         }
317
318         font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
319                                * lyxrc.zoom / 100.0);
320
321         switch (f.series()) {
322                 case LyXFont::MEDIUM_SERIES:
323                         font.setWeight(QFont::Normal);
324                         break;
325                 case LyXFont::BOLD_SERIES:
326                         font.setWeight(QFont::Bold);
327                         break;
328                 default:
329                         break;
330         }
331
332         switch (f.realShape()) {
333                 case LyXFont::ITALIC_SHAPE:
334                 case LyXFont::SLANTED_SHAPE:
335                         font.setItalic(true);
336                         break;
337                 default:
338                         break;
339         }
340
341         if (lyxerr.debugging(Debug::FONT)) {
342                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
343                         << "' matched by\n" << font.rawName() << endl;
344         }
345
346         lyxerr[Debug::FONT] << "The font has size: "
347                             << font.pointSizeFloat() << endl;
348
349         // Is this an exact match?
350         if (font.exactMatch())
351                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
352         else
353                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
354                                     << endl;
355
356         lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
357
358         metrics = QFontMetrics(font);
359 }
360
361
362 int QLFontInfo::width(Uchar val)
363 {
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())
369                 return cit->second;
370
371         int const w = metrics.width(QChar(val));
372         widthcache[val] = w;
373         return w;
374 #else
375         return metrics.width(QChar(val));
376 #endif
377 }
378
379
380 bool FontLoader::available(LyXFont const & f)
381 {
382         if (!lyx_gui::use_gui)
383                 return false;
384
385         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
386         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
387
388         LyXFont::FONT_FAMILY family = f.family();
389         if (cache_set[family])
390                 return cache[family];
391         cache_set[family] = true;
392
393         string const pat = symbolFamily(family);
394         if (pat.empty())
395                 // We don't care about non-symbol fonts
396                 return false;
397
398         pair<QFont, bool> tmp = getSymbolFont(pat);
399         if (!tmp.second)
400                 return false;
401
402         cache[family] = true;
403         return true;
404 }