]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiFontLoader.C
* LyXText::singleWidth(): remove unneeded Buffer argument.
[features.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
97 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
98
99
100 string getRawName(string 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 !" << endl;
107         return string();
108 }
109
110
111 string const symbolFamily(LyXFont::FONT_FAMILY 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 string();
118 }
119
120
121 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
122 {
123         return family >= LyXFont::SYMBOL_FAMILY &&
124                 family <= LyXFont::WASY_FAMILY;
125 }
126
127
128 bool isChosenFont(QFont & font, string const & family)
129 {
130         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
131
132         QFontInfo fi(font);
133
134         // Note Qt lies about family quite often
135         lyxerr[Debug::FONT] << "alleged fi family: "
136                 << fromqstr(fi.family()) << endl;
137
138         // So we check rawName first
139         if (contains(fromqstr(font.rawName()), family)) {
140                 lyxerr[Debug::FONT] << " got it ";
141                 return true;
142         }
143
144         // Qt 3.2 beta1 returns "xft" for all xft fonts
145         // Qt 4.1 returns "Multi" for all ? xft fonts
146         if (font.rawName() == "xft" || font.rawName() == "Multi") {
147                 if (contains(fromqstr(fi.family()), family)) {
148                         lyxerr[Debug::FONT] << " got it (Xft) ";
149                         return true;
150                 }
151         }
152
153         return false;
154 }
155
156
157 pair<QFont, bool> const getSymbolFont(string const & family)
158 {
159         lyxerr[Debug::FONT] << "Looking for font family "
160                 << family << " ... ";
161         string upper = family;
162         upper[0] = toupper(family[0]);
163
164         QFont font;
165         font.setKerning(false);
166         font.setFamily(toqstr(family));
167
168         if (isChosenFont(font, family)) {
169                 lyxerr[Debug::FONT] << "normal!" << endl;
170                 return make_pair<QFont, bool>(font, true);
171         }
172
173         font.setFamily(toqstr(upper));
174
175         if (isChosenFont(font, upper)) {
176                 lyxerr[Debug::FONT] << "upper!" << endl;
177                 return make_pair<QFont, bool>(font, true);
178         }
179
180         // A simple setFamily() fails on Qt 2
181
182         font.setRawName(toqstr(getRawName(family)));
183
184         if (isChosenFont(font, family)) {
185                 lyxerr[Debug::FONT] << "raw version!" << endl;
186                 return make_pair<QFont, bool>(font, true);
187         }
188
189         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
190         return make_pair<QFont, bool>(font, false);
191 }
192
193 } // namespace anon
194
195
196 GuiFontLoader::GuiFontLoader()
197 {
198         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
199                 for (int i2 = 0; i2 < 2; ++i2)
200                         for (int i3 = 0; i3 < 4; ++i3)
201                                 for (int i4 = 0; i4 < 10; ++i4)
202                                         fontinfo_[i1][i2][i3][i4] = 0;
203 }
204
205
206 void GuiFontLoader::update()
207 {
208         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
209                 for (int i2 = 0; i2 < 2; ++i2)
210                         for (int i3 = 0; i3 < 4; ++i3)
211                                 for (int i4 = 0; i4 < 10; ++i4) {
212                                         delete fontinfo_[i1][i2][i3][i4];
213                                         fontinfo_[i1][i2][i3][i4] = 0;
214                                 }
215 }
216
217
218 /////////////////////////////////////////////////
219
220
221 QLFontInfo::QLFontInfo(LyXFont const & f)
222 {
223         font.setKerning(false);
224         string const pat = symbolFamily(f.family());
225         if (!pat.empty()) {
226                 bool tmp;
227                 boost::tie(font, tmp) = getSymbolFont(pat);
228         } else {
229                 switch (f.family()) {
230                 case LyXFont::ROMAN_FAMILY:
231                         font.setFamily(toqstr(makeFontName(lyxrc.roman_font_name,
232                                                     lyxrc.roman_font_foundry)));
233                         break;
234                 case LyXFont::SANS_FAMILY:
235                         font.setFamily(toqstr(makeFontName(lyxrc.sans_font_name,
236                                                     lyxrc.sans_font_foundry)));
237                         break;
238                 case LyXFont::TYPEWRITER_FAMILY:
239                         font.setFamily(toqstr(makeFontName(lyxrc.typewriter_font_name,
240                                                     lyxrc.typewriter_font_foundry)));
241                         break;
242                 default:
243                         break;
244                 }
245         }
246
247         switch (f.series()) {
248                 case LyXFont::MEDIUM_SERIES:
249                         font.setWeight(QFont::Normal);
250                         break;
251                 case LyXFont::BOLD_SERIES:
252                         font.setWeight(QFont::Bold);
253                         break;
254                 default:
255                         break;
256         }
257
258         switch (f.realShape()) {
259                 case LyXFont::ITALIC_SHAPE:
260                 case LyXFont::SLANTED_SHAPE:
261                         font.setItalic(true);
262                         break;
263                 default:
264                         break;
265         }
266
267         if (lyxerr.debugging(Debug::FONT)) {
268                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
269                         << "' matched by\n" << fromqstr(font.rawName()) << endl;
270         }
271
272         // Is this an exact match?
273         if (font.exactMatch())
274                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
275         else
276                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
277                                     << endl;
278
279         lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl;
280
281         font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
282                                * lyxrc.zoom / 100.0);
283
284         lyxerr[Debug::FONT] << "The font has size: "
285                             << font.pointSizeF() << endl;
286
287         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
288                 metrics.reset(new GuiFontMetrics(font));
289         }
290         else {  
291                 // handle small caps ourselves ...
292                 LyXFont smallfont = f;
293                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
294                 QFont font2(font);
295                 font2.setKerning(false);
296                 font2.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
297                                * lyxrc.zoom / 100.0);
298
299                 metrics.reset(new GuiFontMetrics(font, font2));
300         }
301
302 }
303
304
305 bool GuiFontLoader::available(LyXFont const & f)
306 {
307         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
308         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
309
310         LyXFont::FONT_FAMILY family = f.family();
311         if (cache_set[family])
312                 return cache[family];
313         cache_set[family] = true;
314
315         string const pat = symbolFamily(family);
316         if (pat.empty())
317                 // We don't care about non-symbol fonts
318                 return false;
319
320         pair<QFont, bool> tmp = getSymbolFont(pat);
321         if (!tmp.second)
322                 return false;
323
324         cache[family] = true;
325         return true;
326 }
327
328 } // namespace frontend
329 } // namespace lyx