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