]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiFontLoader.C
This commit is a big rework of the FontLoader/FontMetrics interaction. Only Qt4 for...
[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 "frontends/lyx_gui.h"
21
22 #include "support/convert.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/systemcall.h"
26
27 #include <qfontinfo.h>
28
29 #include <boost/tuple/tuple.hpp>
30
31 #ifdef Q_WS_X11
32 #include <qwidget.h>
33 #include <X11/Xlib.h>
34 #include <algorithm>
35 #endif
36
37 using lyx::support::contains;
38
39 using std::endl;
40 using std::make_pair;
41
42 using std::pair;
43 using std::vector;
44 using std::string;
45
46
47 namespace lyx {
48 namespace frontend {
49
50 GuiFontLoader::~GuiFontLoader() {
51 }
52
53 namespace {
54
55 struct symbol_font {
56         LyXFont::FONT_FAMILY lyx_family;
57         string family;
58         string xlfd;
59 };
60
61 symbol_font symbol_fonts[] = {
62         { LyXFont::SYMBOL_FAMILY,
63                 "symbol",
64                 "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific" },
65
66         { LyXFont::CMR_FAMILY,
67                 "cmr10",
68                 "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
69
70         { LyXFont::CMSY_FAMILY,
71                 "cmsy10",
72                 "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
73
74         { LyXFont::CMM_FAMILY,
75                 "cmmi10",
76                 "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
77
78         { LyXFont::CMEX_FAMILY,
79                 "cmex10",
80                 "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
81
82         { LyXFont::MSA_FAMILY,
83                 "msam10",
84                 "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
85
86         { LyXFont::MSB_FAMILY,
87                 "msbm10",
88                 "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
89
90         { LyXFont::EUFRAK_FAMILY,
91                 "eufm10",
92                 "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
93
94         { LyXFont::WASY_FAMILY,
95                 "wasy10",
96                 "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" }
97 };
98
99 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_font);
100
101
102 string getRawName(string const & family)
103 {
104         for (size_t i = 0; i < nr_symbol_fonts; ++i)
105                 if (family == symbol_fonts[i].family)
106                         return symbol_fonts[i].xlfd;
107
108         lyxerr[Debug::FONT] << "BUG: family not found !" << endl;
109         return string();
110 }
111
112
113 string const symbolFamily(LyXFont::FONT_FAMILY family)
114 {
115         for (size_t i = 0; i < nr_symbol_fonts; ++i) {
116                 if (family == symbol_fonts[i].lyx_family)
117                         return symbol_fonts[i].family;
118         }
119         return string();
120 }
121
122
123 bool isSymbolFamily(LyXFont::FONT_FAMILY family)
124 {
125         return family >= LyXFont::SYMBOL_FAMILY &&
126                 family <= LyXFont::WASY_FAMILY;
127 }
128
129
130 bool isChosenFont(QFont & font, string const & family)
131 {
132         lyxerr[Debug::FONT] << "raw: " << fromqstr(font.rawName()) << endl;
133
134         QFontInfo fi(font);
135
136         // Note Qt lies about family quite often
137         lyxerr[Debug::FONT] << "alleged fi family: "
138                 << fromqstr(fi.family()) << endl;
139
140         // So we check rawName first
141         if (contains(fromqstr(font.rawName()), family)) {
142                 lyxerr[Debug::FONT] << " got it ";
143                 return true;
144         }
145
146         // Qt 3.2 beta1 returns "xft" for all xft fonts
147         // Qt 4.1 returns "Multi" for all ? xft fonts
148         if (font.rawName() == "xft" || font.rawName() == "Multi") {
149                 if (contains(fromqstr(fi.family()), family)) {
150                         lyxerr[Debug::FONT] << " got it (Xft) ";
151                         return true;
152                 }
153         }
154
155         return false;
156 }
157
158
159 pair<QFont, bool> const getSymbolFont(string const & family)
160 {
161         lyxerr[Debug::FONT] << "Looking for font family "
162                 << family << " ... ";
163         string upper = family;
164         upper[0] = toupper(family[0]);
165
166         QFont font;
167         font.setFamily(toqstr(family));
168
169         if (isChosenFont(font, family)) {
170                 lyxerr[Debug::FONT] << "normal!" << endl;
171                 return make_pair<QFont, bool>(font, true);
172         }
173
174         font.setFamily(toqstr(upper));
175
176         if (isChosenFont(font, upper)) {
177                 lyxerr[Debug::FONT] << "upper!" << endl;
178                 return make_pair<QFont, bool>(font, true);
179         }
180
181         // A simple setFamily() fails on Qt 2
182
183         font.setRawName(toqstr(getRawName(family)));
184
185         if (isChosenFont(font, family)) {
186                 lyxerr[Debug::FONT] << "raw version!" << endl;
187                 return make_pair<QFont, bool>(font, true);
188         }
189
190         lyxerr[Debug::FONT] << " FAILED :-(" << endl;
191         return make_pair<QFont, bool>(font, false);
192 }
193
194 } // namespace anon
195
196
197 GuiFontLoader::GuiFontLoader()
198 {
199         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
200                 for (int i2 = 0; i2 < 2; ++i2)
201                         for (int i3 = 0; i3 < 4; ++i3)
202                                 for (int i4 = 0; i4 < 10; ++i4)
203                                         fontinfo_[i1][i2][i3][i4] = 0;
204 }
205
206
207 void GuiFontLoader::update()
208 {
209         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
210                 for (int i2 = 0; i2 < 2; ++i2)
211                         for (int i3 = 0; i3 < 4; ++i3)
212                                 for (int i4 = 0; i4 < 10; ++i4) {
213                                         delete fontinfo_[i1][i2][i3][i4];
214                                         fontinfo_[i1][i2][i3][i4] = 0;
215                                 }
216 }
217
218
219 /////////////////////////////////////////////////
220
221
222 QLFontInfo::QLFontInfo(LyXFont const & f)
223 {
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.setPointSizeF(convert<double>(lyxrc.font_sizes[smallfont.size()])
296                                * lyxrc.zoom / 100.0);
297
298                 metrics.reset(new GuiFontMetrics(font, font2));
299         }
300
301 }
302
303
304 bool GuiFontLoader::available(LyXFont const & f)
305 {
306         if (!lyx_gui::use_gui)
307                 return false;
308
309         static vector<int> cache_set(LyXFont::NUM_FAMILIES, false);
310         static vector<int> cache(LyXFont::NUM_FAMILIES, false);
311
312         LyXFont::FONT_FAMILY family = f.family();
313         if (cache_set[family])
314                 return cache[family];
315         cache_set[family] = true;
316
317         string const pat = symbolFamily(family);
318         if (pat.empty())
319                 // We don't care about non-symbol fonts
320                 return false;
321
322         pair<QFont, bool> tmp = getSymbolFont(pat);
323         if (!tmp.second)
324                 return false;
325
326         cache[family] = true;
327         return true;
328 }
329
330 } // namespace frontend
331 } // namespace lyx