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