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