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