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