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