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