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