]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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/assert.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: " << fromqstr(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 "
157                 << fromqstr(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 " << fromqstr(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 " << fromqstr(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 " << fromqstr(font_file)
210                                     << static_cast<const char *>
211                                         (fontID < 0 ? " FAIL" : " OK"));
212         }
213
214         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
215                 for (int i2 = 0; i2 < 2; ++i2)
216                         for (int i3 = 0; i3 < 4; ++i3)
217                                 for (int i4 = 0; i4 < 10; ++i4)
218                                         fontinfo_[i1][i2][i3][i4] = 0;
219 }
220
221
222 void FontLoader::update()
223 {
224         for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
225                 for (int i2 = 0; i2 < 2; ++i2)
226                         for (int i3 = 0; i3 < 4; ++i3)
227                                 for (int i4 = 0; i4 < 10; ++i4) {
228                                         delete fontinfo_[i1][i2][i3][i4];
229                                         fontinfo_[i1][i2][i3][i4] = 0;
230                                 }
231 }
232
233
234 FontLoader::~FontLoader()
235 {
236         update();
237 }
238
239 /////////////////////////////////////////////////
240
241
242 static QString makeFontName(QString const & family, QString const & foundry)
243 {
244         QString res = family;
245         if (!foundry.isEmpty())
246                 res += " [" + foundry + ']';
247         return res;
248 }
249
250
251 GuiFontInfo::GuiFontInfo(FontInfo const & f)
252         : metrics(QFont())
253 {
254         font.setKerning(false);
255         QString const pat = symbolFamily(f.family());
256         if (!pat.isEmpty()) {
257                 bool ok;
258                 font = symbolFont(pat, &ok);
259         } else {
260                 switch (f.family()) {
261                 case ROMAN_FAMILY: {
262                         QString family = makeFontName(toqstr(lyxrc.roman_font_name),
263                                                                                                                                                 toqstr(lyxrc.roman_font_foundry)); 
264                         font.setFamily(family);
265 #ifdef Q_WS_MACX
266 #if QT_VERSION >= 0x040300
267                         // Workaround for a Qt bug, see http://bugzilla.lyx.org/show_bug.cgi?id=3684
268                         // It is reported to Trolltech at 02/06/07 against 4.3 final.
269                         // FIXME: Add an upper version limit as soon as the bug is fixed in Qt.
270                         if (family == "Times" && !font.exactMatch())
271                                 font.setFamily("Times New Roman");
272 #endif
273 #endif
274                         break;
275                 }
276                 case SANS_FAMILY:
277                         font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
278                                                     toqstr(lyxrc.sans_font_foundry)));
279                         break;
280                 case TYPEWRITER_FAMILY:
281                         font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
282                                                     toqstr(lyxrc.typewriter_font_foundry)));
283                         break;
284                 default:
285                         break;
286                 }
287         }
288
289         switch (f.series()) {
290                 case MEDIUM_SERIES:
291                         font.setWeight(QFont::Normal);
292                         break;
293                 case BOLD_SERIES:
294                         font.setWeight(QFont::Bold);
295                         break;
296                 default:
297                         break;
298         }
299
300         switch (f.realShape()) {
301                 case ITALIC_SHAPE:
302                 case SLANTED_SHAPE:
303                         font.setItalic(true);
304                         break;
305                 default:
306                         break;
307         }
308
309         LYXERR(Debug::FONT, "Font '" << to_utf8(stateText(f))
310                 << "' matched by\n" << fromqstr(font.family()));
311
312         // Is this an exact match?
313         if (font.exactMatch())
314                 LYXERR(Debug::FONT, "This font is an exact match");
315         else
316                 LYXERR(Debug::FONT, "This font is NOT an exact match");
317
318         LYXERR(Debug::FONT, "XFLD: " << fromqstr(font.rawName()));
319
320         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
321                                * lyxrc.zoom / 100.0);
322
323         LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
324
325         if (f.realShape() != SMALLCAPS_SHAPE) {
326                 metrics = GuiFontMetrics(font);
327         } else {
328                 // handle small caps ourselves ...
329                 FontInfo smallfont = f;
330                 smallfont.decSize().decSize().setShape(UP_SHAPE);
331                 QFont font2(font);
332                 font2.setKerning(false);
333                 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
334                                * lyxrc.zoom / 100.0);
335
336                 metrics = GuiFontMetrics(font, font2);
337         }
338 }
339
340
341 bool FontLoader::available(FontInfo const & f)
342 {
343         static vector<int> cache_set(NUM_FAMILIES, false);
344         static vector<int> cache(NUM_FAMILIES, false);
345
346         FontFamily family = f.family();
347         if (cache_set[family])
348                 return cache[family];
349         cache_set[family] = true;
350
351         QString const pat = symbolFamily(family);
352         if (pat.isEmpty())
353                 // We don't care about non-symbol fonts
354                 return false;
355
356         bool ok;
357         symbolFont(pat, &ok);
358         if (!ok)
359                 return false;
360
361         cache[family] = true;
362         return true;
363 }
364
365
366 FontMetrics const & FontLoader::metrics(FontInfo const & f)
367 {
368         return fontinfo(f).metrics;
369 }
370
371
372 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
373 {
374         return fontinfo(f).metrics;
375 }
376
377
378 QFont const & getFont(FontInfo const & f)
379 {
380         return fontinfo(f).font;
381 }
382
383 } // namespace frontend
384 } // namespace lyx