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