]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
shuffle stuff around
[lyx.git] / src / frontends / qt4 / GuiFontLoader.cpp
1 /**
2  * \file GuiFontLoader.cpp
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 "GuiFontLoader.h"
15 #include "qt_helpers.h"
16
17 #include "debug.h"
18 #include "LyXRC.h"
19
20 #include "support/convert.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/Systemcall.h"
24 #include "support/Package.h"
25 #include "support/os.h"
26
27 #include <QFontInfo>
28 #include <QFontDatabase>
29
30 using lyx::support::contains;
31 using lyx::support::package;
32 using lyx::support::addPath;
33 using lyx::support::addName;
34
35 using std::endl;
36 using std::make_pair;
37
38 using std::pair;
39 using std::vector;
40 using std::string;
41
42 #if QT_VERSION >= 0x040200
43 string const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
44         "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
45 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
46 #endif
47
48
49 namespace lyx {
50 namespace frontend {
51
52 namespace {
53
54 struct symbol_font {
55         Font::FONT_FAMILY lyx_family;
56         string family;
57         string xlfd;
58 };
59
60 symbol_font symbol_fonts[] = {
61         { Font::SYMBOL_FAMILY,
62                 "symbol",
63                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
64
65         { Font::CMR_FAMILY,
66                 "cmr10",
67                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
68
69         { Font::CMSY_FAMILY,
70                 "cmsy10",
71                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
72
73         { Font::CMM_FAMILY,
74                 "cmmi10",
75                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
76
77         { Font::CMEX_FAMILY,
78                 "cmex10",
79                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
80
81         { Font::MSA_FAMILY,
82                 "msam10",
83                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
84
85         { Font::MSB_FAMILY,
86                 "msbm10",
87                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
88
89         { Font::EUFRAK_FAMILY,
90                 "eufm10",
91                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
92
93         { Font::WASY_FAMILY,
94                 "wasy10",
95                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
96
97         { Font::ESINT_FAMILY,
98                 "esint10",
99                 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
100 };
101
102 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
103
104
105 string getRawName(string const & family)
106 {
107         for (size_t i = 0; i < nr_symbol_fonts; ++i)
108                 if (family == symbol_fonts[i].family)
109                         return symbol_fonts[i].xlfd;
110
111         LYXERR(Debug::FONT) << "BUG: family not found !" << endl;
112         return string();
113 }
114
115
116 string const symbolFamily(Font::FONT_FAMILY family)
117 {
118         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
119                 if (family == symbol_fonts[i].lyx_family)
120                         return symbol_fonts[i].family;
121         }
122         return string();
123 }
124
125
126 bool isSymbolFamily(Font::FONT_FAMILY family)
127 {
128         return family >= Font::SYMBOL_FAMILY &&
129                family <= Font::ESINT_FAMILY;
130 }
131
132
133 bool isChosenFont(QFont & font, string const & family)
134 {
135         // QFontInfo won't find a font that has only a few glyphs at unusual
136         // positions, e.g. the original esint10 font.
137         // The workaround is to add dummy glyphs at least at all ASCII
138         // positions.
139         QFontInfo fi(font);
140
141         LYXERR(Debug::FONT) << "got: " << fromqstr(fi.family()) << endl;
142
143         if (contains(fromqstr(fi.family()), family)) {
144                 LYXERR(Debug::FONT) << " got it ";
145                 return true;
146         }
147
148         return false;
149 }
150
151
152 pair<QFont, bool> const getSymbolFont(string const & family)
153 {
154         LYXERR(Debug::FONT) << "Looking for font family "
155                 << family << " ... ";
156         string upper = family;
157         upper[0] = toupper(family[0]);
158
159         QFont font;
160         font.setKerning(false);
161         font.setFamily(toqstr(family));
162
163         if (isChosenFont(font, family)) {
164                 LYXERR(Debug::FONT) << "normal!" << endl;
165                 return make_pair<QFont, bool>(font, true);
166         }
167
168         LYXERR(Debug::FONT) << "Trying " << upper << " ... ";
169         font.setFamily(toqstr(upper));
170
171         if (isChosenFont(font, upper)) {
172                 LYXERR(Debug::FONT) << "upper!" << endl;
173                 return make_pair<QFont, bool>(font, true);
174         }
175
176         // A simple setFamily() fails on Qt 2
177
178         string const rawName = getRawName(family);
179         LYXERR(Debug::FONT) << "Trying " << rawName << " ... ";
180         font.setRawName(toqstr(rawName));
181
182         if (isChosenFont(font, family)) {
183                 LYXERR(Debug::FONT) << "raw version!" << endl;
184                 return make_pair<QFont, bool>(font, true);
185         }
186
187         LYXERR(Debug::FONT) << " FAILED :-(" << endl;
188         return make_pair<QFont, bool>(font, false);
189 }
190
191 } // namespace anon
192
193
194 GuiFontLoader::GuiFontLoader()
195 {
196 #if QT_VERSION >= 0x040200
197         fontID = new int[num_math_fonts];
198
199         string const fonts_dir =
200                 addPath(package().system_support().absFilename(), "fonts");
201
202         for (int i = 0 ; i < num_math_fonts; ++i) {
203                 string const font_file = lyx::support::os::external_path(
204                                 addName(fonts_dir, math_fonts[i] + ".ttf"));
205                 fontID[i] = QFontDatabase::addApplicationFont(toqstr(font_file));
206
207                 LYXERR(Debug::FONT) << "Adding font " << font_file
208                                     << static_cast<const char *>
209                                         (fontID[i] < 0 ? " FAIL" : " OK")
210                                     << endl;
211         }
212 #endif
213
214         for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1)
215                 for (int i2 = 0; i2 < 2; ++i2)
216                         for (int i3 = 0; i3 < 4; ++i3)
217                                 for (int i4 = 0; i4 < 10; ++i4)
218                                         fontinfo_[i1][i2][i3][i4] = 0;
219 }
220
221
222 GuiFontLoader::~GuiFontLoader()
223 {
224 #if QT_VERSION >= 0x040200
225         for (int i = 0 ; i < num_math_fonts; ++i) {
226                 if (fontID[i] >= 0)
227                         QFontDatabase::removeApplicationFont(fontID[i]);
228         }
229
230         delete [] fontID;
231 #endif
232 }
233
234
235 void GuiFontLoader::update()
236 {
237         for (int i1 = 0; i1 < Font::NUM_FAMILIES; ++i1) {
238                 for (int i2 = 0; i2 < 2; ++i2)
239                         for (int i3 = 0; i3 < 4; ++i3)
240                                 for (int i4 = 0; i4 < 10; ++i4) {
241                                         delete fontinfo_[i1][i2][i3][i4];
242                                         fontinfo_[i1][i2][i3][i4] = 0;
243                                 }
244         }
245 }
246
247
248 /////////////////////////////////////////////////
249
250
251 QLFontInfo::QLFontInfo(Font const & f)
252 {
253         font.setKerning(false);
254         string const pat = symbolFamily(f.family());
255         if (!pat.empty()) {
256                 bool tmp;
257                 boost::tie(font, tmp) = getSymbolFont(pat);
258         } else {
259                 switch (f.family()) {
260                 case Font::ROMAN_FAMILY: {
261                         QString family = toqstr(makeFontName(lyxrc.roman_font_name,
262                                                                                                                                                                          lyxrc.roman_font_foundry)); 
263                         font.setFamily(family);
264 #ifdef Q_WS_MACX
265 #if QT_VERSION >= 0x040300
266                         // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
267                         // It is reported to Trolltech at 02/06/07 against 4.3 final.
268                         // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
269                         if (family == "Times" && !font.exactMatch())
270                                 font.setFamily(QString::fromLatin1("Times New Roman"));
271 #endif
272 #endif
273                         break;
274                 }
275                 case Font::SANS_FAMILY:
276                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
277                                                     lyxrc.sans_font_foundry)));
278                         break;
279                 case Font::TYPEWRITER_FAMILY:
280                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
281                                                     lyxrc.typewriter_font_foundry)));
282                         break;
283                 default:
284                         break;
285                 }
286         }
287
288         switch (f.series()) {
289                 case Font::MEDIUM_SERIES:
290                         font.setWeight(QFont::Normal);
291                         break;
292                 case Font::BOLD_SERIES:
293                         font.setWeight(QFont::Bold);
294                         break;
295                 default:
296                         break;
297         }
298
299         switch (f.realShape()) {
300                 case Font::ITALIC_SHAPE:
301                 case Font::SLANTED_SHAPE:
302                         font.setItalic(true);
303                         break;
304                 default:
305                         break;
306         }
307
308         LYXERR(Debug::FONT) << "Font '" << to_utf8(f.stateText(0))
309                 << "' matched by\n" << fromqstr(font.family()) << endl;
310
311         // Is this an exact match?
312         if (font.exactMatch())
313                 LYXERR(Debug::FONT) << "This font is an exact match" << endl;
314         else
315                 LYXERR(Debug::FONT) << "This font is NOT an exact match"
316                                     << endl;
317
318         LYXERR(Debug::FONT) << "XFLD: " << fromqstr(font.rawName()) << endl;
319
320         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
321                                * lyxrc.zoom / 100.0);
322
323         LYXERR(Debug::FONT) << "The font has size: "
324                             << font.pointSizeF() << endl;
325
326         if (f.realShape() != Font::SMALLCAPS_SHAPE) {
327                 metrics.reset(new GuiFontMetrics(font));
328         }
329         else {
330                 // handle small caps ourselves ...
331                 Font smallfont = f;
332                 smallfont.decSize().decSize().setShape(Font::UP_SHAPE);
333                 QFont font2(font);
334                 font2.setKerning(false);
335                 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
336                                * lyxrc.zoom / 100.0);
337
338                 metrics.reset(new GuiFontMetrics(font, font2));
339         }
340
341 }
342
343
344 bool GuiFontLoader::available(Font const & f)
345 {
346         static vector<int> cache_set(Font::NUM_FAMILIES, false);
347         static vector<int> cache(Font::NUM_FAMILIES, false);
348
349         Font::FONT_FAMILY family = f.family();
350         if (cache_set[family])
351                 return cache[family];
352         cache_set[family] = true;
353
354         string const pat = symbolFamily(family);
355         if (pat.empty())
356                 // We don't care about non-symbol fonts
357                 return false;
358
359         pair<QFont, bool> tmp = getSymbolFont(pat);
360         if (!tmp.second)
361                 return false;
362
363         cache[family] = true;
364         return true;
365 }
366
367 } // namespace frontend
368 } // namespace lyx