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