]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qfont_loader.C
Added initial qt4 work by Abdelrazak Younes
[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         if (font.rawName() == "xft") {
181                 if (contains(fromqstr(fi.family()), family)) {
182                         lyxerr[Debug::FONT] << " got it (Xft) ";
183                         return true;
184                 }
185         }
186
187         return false;
188 }
189
190
191 pair<QFont, bool> const getSymbolFont(string const & family)
192 {
193         lyxerr[Debug::FONT] << "Looking for font family "
194                 << family << " ... ";
195         string upper = family;
196         upper[0] = toupper(family[0]);
197
198         QFont font;
199         font.setFamily(toqstr(family));
200
201         if (isChosenFont(font, family)) {
202                 lyxerr[Debug::FONT] << "normal!" << endl;
203                 return make_pair<QFont, bool>(font, true);
204         }
205
206         font.setFamily(toqstr(upper));
207
208         if (isChosenFont(font, upper)) {
209                 lyxerr[Debug::FONT] << "upper!" << endl;
210                 return make_pair<QFont, bool>(font, true);
211         }
212
213         // A simple setFamily() fails on Qt 2
214
215         font.setRawName(toqstr(getRawName(family)));
216
217         if (isChosenFont(font, family)) {
218                 lyxerr[Debug::FONT] << "raw version!" << endl;
219                 return make_pair<QFont, bool>(font, true);
220         }
221
222         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
223         return make_pair<QFont, bool>(font, false);
224 }
225
226 } // namespace anon
227
228
229 FontLoader::FontLoader()
230 {
231         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
232                 for (int i2 = 0; i2 < 2; ++i2)
233                         for (int i3 = 0; i3 < 4; ++i3)
234                                 for (int i4 = 0; i4 < 10; ++i4)
235                                         fontinfo_[i1][i2][i3][i4] = 0;
236 }
237
238
239 void FontLoader::update()
240 {
241         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) 
242                 for (int i2 = 0; i2 < 2; ++i2) 
243                         for (int i3 = 0; i3 < 4; ++i3) 
244                                 for (int i4 = 0; i4 < 10; ++i4) {
245                                         delete fontinfo_[i1][i2][i3][i4];
246                                         fontinfo_[i1][i2][i3][i4] = 0;
247                                 }
248 }
249
250
251 /////////////////////////////////////////////////
252
253
254 QLFontInfo::QLFontInfo(LyXFont const & f)
255         : metrics(font)
256 {
257
258         string const pat = symbolFamily(f.family());
259         if (!pat.empty()) {
260                 bool tmp;
261                 boost::tie(font, tmp) = getSymbolFont(pat);
262         } else {
263                 switch (f.family()) {
264                 case LyXFont::ROMAN_FAMILY:
265                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
266                                                     lyxrc.roman_font_foundry)));
267                         break;
268                 case LyXFont::SANS_FAMILY:
269                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
270                                                     lyxrc.sans_font_foundry)));
271                         break;
272                 case LyXFont::TYPEWRITER_FAMILY:
273                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
274                                                     lyxrc.typewriter_font_foundry)));
275                         break;
276                 default:
277                         break;
278                 }
279         }
280
281         font.setPointSizeFloat(convert<double>(lyxrc.font_sizes[f.size()])
282                                * lyxrc.zoom / 100.0);
283
284         switch (f.series()) {
285                 case LyXFont::MEDIUM_SERIES:
286                         font.setWeight(QFont::Normal);
287                         break;
288                 case LyXFont::BOLD_SERIES:
289                         font.setWeight(QFont::Bold);
290                         break;
291                 default:
292                         break;
293         }
294
295         switch (f.realShape()) {
296                 case LyXFont::ITALIC_SHAPE:
297                 case LyXFont::SLANTED_SHAPE:
298                         font.setItalic(true);
299                         break;
300                 default:
301                         break;
302         }
303
304         if (lyxerr.debugging(Debug::FONT)) {
305                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
306                         << "' matched by\n" << (const char *) font.rawName() << endl;
307         }
308
309         lyxerr[Debug::FONT] << "The font has size: "
310                             << font.pointSizeFloat() << endl;
311
312         // Is this an exact match?
313         if (font.exactMatch())
314                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
315         else
316                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
317                                     << endl;
318
319         lyxerr[Debug::FONT] << "XFLD: " << (const char *) font.rawName() << endl;
320
321         metrics = QFontMetrics(font);
322 }
323
324
325 int QLFontInfo::width(Uchar val) const
326 {
327 // Starting with version 3.1.0, Qt/X11 does its own caching of
328 // character width, so it is not necessary to provide ours.
329 #if defined (USE_LYX_FONTCACHE)
330         QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
331         if (cit != widthcache.end())
332                 return cit->second;
333
334         int const w = metrics.width(QChar(val));
335         widthcache[val] = w;
336         return w;
337 #else
338         return metrics.width(QChar(val));
339 #endif
340 }
341
342
343 bool FontLoader::available(LyXFont const & f)
344 {
345         if (!lyx_gui::use_gui)
346                 return false;
347
348         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
349         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
350
351         LyXFont::FONT_FAMILY family = f.family();
352         if (cache_set[family])
353                 return cache[family];
354         cache_set[family] = true;
355
356         string const pat = symbolFamily(family);
357         if (pat.empty())
358                 // We don't care about non-symbol fonts
359                 return false;
360
361         pair<QFont, bool> tmp = getSymbolFont(pat);
362         if (!tmp.second)
363                 return false;
364
365         cache[family] = true;
366         return true;
367 }