]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.C
* qt_helpers.h:
[lyx.git] / src / frontends / qt4 / GuiFontLoader.C
1 /**
2  * \file GuiFontLoader.C
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 GuiFontLoader::~GuiFontLoader() {
49 }
50
51 namespace {
52
53 struct symbol_font {
54         LyXFont::FONT_FAMILY lyx_family;
55         string family;
56         string xlfd;
57 };
58
59 symbol_font symbol_fonts[] = {
60         { LyXFont::SYMBOL_FAMILY,
61                 "symbol",
62                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
63
64         { LyXFont::CMR_FAMILY,
65                 "cmr10",
66                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
67
68         { LyXFont::CMSY_FAMILY,
69                 "cmsy10",
70                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
71
72         { LyXFont::CMM_FAMILY,
73                 "cmmi10",
74                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75
76         { LyXFont::CMEX_FAMILY,
77                 "cmex10",
78                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
79
80         { LyXFont::MSA_FAMILY,
81                 "msam10",
82                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
83
84         { LyXFont::MSB_FAMILY,
85                 "msbm10",
86                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
87
88         { LyXFont::EUFRAK_FAMILY,
89                 "eufm10",
90                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
91
92         { LyXFont::WASY_FAMILY,
93                 "wasy10",
94                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
95
96         { LyXFont::ESINT_FAMILY,
97                 "esint10",
98                 "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
99 };
100
101 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
102
103
104 string getRawName(string const & family)
105 {
106         for (size_t i = 0; i < nr_symbol_fonts; ++i)
107                 if (family == symbol_fonts[i].family)
108                         return symbol_fonts[i].xlfd;
109
110         lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
111         return string();
112 }
113
114
115 string const symbolFamily(LyXFont::FONT_FAMILY family)
116 {
117         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
118                 if (family == symbol_fonts[i].lyx_family)
119                         return symbol_fonts[i].family;
120         }
121         return string();
122 }
123
124
125 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
126 {
127         return family >= LyXFont::SYMBOL_FAMILY &&
128                family <= LyXFont::ESINT_FAMILY;
129 }
130
131
132 bool isChosenFont(QFont & font, string const & family)
133 {
134         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
135
136         QFontInfo fi(font);
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
142         // Note Qt lies about family quite often
143         lyxerr[Debug::FONT] << "alleged fi family: "
144                 << fromqstr(fi.family()) << endl;
145
146         // So we check rawName first
147         if (contains(fromqstr(font.rawName()), family)) {
148                 lyxerr[Debug::FONT] << " got it ";
149                 return true;
150         }
151
152         // Qt 4.1 returns "Multi" for all ? xft fonts
153         if (font.rawName() == "xft" || font.rawName() == "Multi") {
154                 if (contains(fromqstr(fi.family()), family)) {
155                         lyxerr[Debug::FONT] << " got it (Xft) ";
156                         return true;
157                 }
158         }
159
160         return false;
161 }
162
163
164 pair<QFont, bool> const getSymbolFont(string const & family)
165 {
166         lyxerr[Debug::FONT] << "Looking for font family "
167                 << family << " ... ";
168         string upper = family;
169         upper[0] = toupper(family[0]);
170
171         QFont font;
172         font.setKerning(false);
173         font.setFamily(toqstr(family));
174
175         if (isChosenFont(font, family)) {
176                 lyxerr[Debug::FONT] << "normal!" << endl;
177                 return make_pair<QFont, bool>(font, true);
178         }
179
180         font.setFamily(toqstr(upper));
181
182         if (isChosenFont(font, upper)) {
183                 lyxerr[Debug::FONT] << "upper!" << endl;
184                 return make_pair<QFont, bool>(font, true);
185         }
186
187         // A simple setFamily() fails on Qt 2
188
189         font.setRawName(toqstr(getRawName(family)));
190
191         if (isChosenFont(font, family)) {
192                 lyxerr[Debug::FONT] << "raw version!" << endl;
193                 return make_pair<QFont, bool>(font, true);
194         }
195
196         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
197         return make_pair<QFont, bool>(font, false);
198 }
199
200 } // namespace anon
201
202
203 GuiFontLoader::GuiFontLoader()
204 {
205         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
206                 for (int i2 = 0; i2 < 2; ++i2)
207                         for (int i3 = 0; i3 < 4; ++i3)
208                                 for (int i4 = 0; i4 < 10; ++i4)
209                                         fontinfo_[i1][i2][i3][i4] = 0;
210 }
211
212
213 void GuiFontLoader::update()
214 {
215         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
216                 for (int i2 = 0; i2 < 2; ++i2)
217                         for (int i3 = 0; i3 < 4; ++i3)
218                                 for (int i4 = 0; i4 < 10; ++i4) {
219                                         delete fontinfo_[i1][i2][i3][i4];
220                                         fontinfo_[i1][i2][i3][i4] = 0;
221                                 }
222 }
223
224
225 /////////////////////////////////////////////////
226
227
228 QLFontInfo::QLFontInfo(LyXFont const & f)
229 {
230         font.setKerning(false);
231         string const pat = symbolFamily(f.family());
232         if (!pat.empty()) {
233                 bool tmp;
234                 boost::tie(font, tmp) = getSymbolFont(pat);
235         } else {
236                 switch (f.family()) {
237                 case LyXFont::ROMAN_FAMILY:
238                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
239                                                     lyxrc.roman_font_foundry)));
240                         break;
241                 case LyXFont::SANS_FAMILY:
242                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
243                                                     lyxrc.sans_font_foundry)));
244                         break;
245                 case LyXFont::TYPEWRITER_FAMILY:
246                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
247                                                     lyxrc.typewriter_font_foundry)));
248                         break;
249                 default:
250                         break;
251                 }
252         }
253
254         switch (f.series()) {
255                 case LyXFont::MEDIUM_SERIES:
256                         font.setWeight(QFont::Normal);
257                         break;
258                 case LyXFont::BOLD_SERIES:
259                         font.setWeight(QFont::Bold);
260                         break;
261                 default:
262                         break;
263         }
264
265         switch (f.realShape()) {
266                 case LyXFont::ITALIC_SHAPE:
267                 case LyXFont::SLANTED_SHAPE:
268                         font.setItalic(true);
269                         break;
270                 default:
271                         break;
272         }
273
274         if (lyxerr.debugging(Debug::FONT)) {
275                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
276                         << "' matched by\n" << fromqstr(font.rawName()) << endl;
277         }
278
279         // Is this an exact match?
280         if (font.exactMatch())
281                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
282         else
283                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
284                                     << endl;
285
286         lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
287
288         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
289                                * lyxrc.zoom / 100.0);
290
291         lyxerr[Debug::FONT] << "The font has size: "
292                             << font.pointSizeF() << endl;
293
294         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
295                 metrics.reset(new GuiFontMetrics(font));
296         }
297         else {  
298                 // handle small caps ourselves ...
299                 LyXFont smallfont = f;
300                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
301                 QFont font2(font);
302                 font2.setKerning(false);
303                 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
304                                * lyxrc.zoom / 100.0);
305
306                 metrics.reset(new GuiFontMetrics(font, font2));
307         }
308
309 }
310
311
312 bool GuiFontLoader::available(LyXFont const & f)
313 {
314         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
315         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
316
317         LyXFont::FONT_FAMILY family = f.family();
318         if (cache_set[family])
319                 return cache[family];
320         cache_set[family] = true;
321
322         string const pat = symbolFamily(family);
323         if (pat.empty())
324                 // We don't care about non-symbol fonts
325                 return false;
326
327         pair<QFont, bool> tmp = getSymbolFont(pat);
328         if (!tmp.second)
329                 return false;
330
331         cache[family] = true;
332         return true;
333 }
334
335 } // namespace frontend
336 } // namespace lyx