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