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