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