1 /**
2 * \file FontLoader.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 "FontLoader.h"
15
16 #include "FontInfo.h"
17 #include "GuiFontMetrics.h"
18 #include "qt_helpers.h"
19
20 #include "LyXRC.h"
21
22 #include "support/debug.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/Systemcall.h"
27 #include "support/Package.h"
28 #include "support/os.h"
29
30 #include <QFontInfo>
31 #include <QFontDatabase>
32
33 #include "support/lassert.h"
34
35 using namespace std;
36 using namespace lyx::support;
37
38 QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
39 "esint10", "eufm10", "msam10", "msbm10", "rsfs10", "stmary10",
40 "wasy10"};
41 int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts);
42
43 namespace lyx {
44
45 extern docstring const stateText(FontInfo const & f);
46
47 namespace frontend {
48
49 /**
50 * Matches Fonts against
51 * actual QFont instances, and also caches metrics.
52 */
53 class GuiFontInfo
54 {
55 public:
56 GuiFontInfo(FontInfo const & f);
57
58 /// The font instance
59 QFont font;
60 /// Metrics on the font
61 GuiFontMetrics metrics;
62 };
63
64 namespace {
65
66 struct SymbolFont {
67 FontFamily lyx_family;
68 QString family;
69 QString xlfd;
70 };
71
72 SymbolFont symbol_fonts[] = {
73 { SYMBOL_FAMILY,"symbol", "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific"},
74 { CMR_FAMILY, "cmr10", "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*" },
75 { CMSY_FAMILY, "cmsy10", "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*" },
76 { CMM_FAMILY, "cmmi10", "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*" },
77 { CMEX_FAMILY, "cmex10", "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*" },
78 { MSA_FAMILY, "msam10", "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*" },
79 { MSB_FAMILY, "msbm10", "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*" },
80 { EUFRAK_FAMILY,"eufm10", "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*" },
81 { RSFS_FAMILY, "rsfs10", "-*-rsfs10-medium-*-*-*-*-*-*-*-*-*-*-*" },
82 { STMARY_FAMILY,"stmary10","-*-stmary10-medium-*-*-*-*-*-*-*-*-*-*-*" },
83 { WASY_FAMILY, "wasy10", "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*" },
84 { ESINT_FAMILY, "esint10","-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" }
85 };
86
87 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
88
89 /// BUTT ugly !
90 static GuiFontInfo *
91 fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
92
93
94 // returns a reference to the pointer type (GuiFontInfo *) in the
95 // fontinfo_ table.
fontinfo_ptr(FontInfo const & f)96 GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
97 {
98 // The display font and the text font are the same
99 size_t const style = (f.style() == LM_ST_DISPLAY) ? LM_ST_TEXT : f.style();
100 return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
101 }
102
103
104 // Get font info (font + metrics) for the given LyX font.
105 // if not cached, create it.
fontinfo(FontInfo const & f)106 GuiFontInfo & fontinfo(FontInfo const & f)
107 {
108 bool const fontIsRealized =
109 (f.family() < NUM_FAMILIES) &&
110 (f.series() < NUM_SERIES) &&
111 (f.realShape() < NUM_SHAPE) &&
112 (f.size() < NUM_SIZE);
113 if (!fontIsRealized) {
114 // We can reset the font to something sensible in release mode.
115 LATTEST(false);
116 LYXERR0("Unrealized font!");
117 FontInfo f2 = f;
118 f2.realize(sane_font);
119 GuiFontInfo * & fi = fontinfo_ptr(f2);
120 if (!fi)
121 fi = new GuiFontInfo(f2);
122 return *fi;
123 }
124 GuiFontInfo * & fi = fontinfo_ptr(f);
125 if (!fi)
126 fi = new GuiFontInfo(f);
127 return *fi;
128 }
129
130
rawName(QString const & family)131 QString rawName(QString const & family)
132 {
133 for (size_t i = 0; i < nr_symbol_fonts; ++i)
134 if (family == symbol_fonts[i].family)
135 return symbol_fonts[i].xlfd;
136
137 LYXERR(Debug::FONT, "BUG: family not found !");
138 return QString();
139 }
140
141
symbolFamily(FontFamily family)142 QString symbolFamily(FontFamily family)
143 {
144 for (size_t i = 0; i < nr_symbol_fonts; ++i) {
145 if (family == symbol_fonts[i].lyx_family)
146 return symbol_fonts[i].family;
147 }
148 return QString();
149 }
150
151
152 #if 0
153 bool isSymbolFamily(FontFamily family)
154 {
155 return family >= SYMBOL_FAMILY && family <= ESINT_FAMILY;
156 }
157 #endif
158
159
isChosenFont(QFont & font,QString const & family,QString const & style)160 static bool isChosenFont(QFont & font, QString const & family,
161 QString const & style)
162 {
163 // QFontInfo won't find a font that has only a few glyphs at unusual
164 // positions, e.g. the original esint10 font.
165 // The workaround is to add dummy glyphs at least at all ASCII
166 // positions.
167 QFontInfo fi(font);
168
169 LYXERR(Debug::FONT, "got: " << fi.family());
170
171 if (fi.family().contains(family)
172 #if QT_VERSION >= 0x040800
173 && (style.isEmpty() || fi.styleName().contains(style))
174 #endif
175 ) {
176 LYXERR(Debug::FONT, " got it ");
177 return true;
178 }
179
180 return false;
181 }
182
183
symbolFont(QString const & family,bool * ok)184 QFont symbolFont(QString const & family, bool * ok)
185 {
186 LYXERR(Debug::FONT, "Looking for font family " << family << " ... ");
187 QString upper = family;
188 upper[0] = family[0].toUpper();
189
190 QFont font;
191 font.setFamily(family);
192 #if QT_VERSION >= 0x040800
193 font.setStyleName("LyX");
194
195 if (isChosenFont(font, family, "LyX")) {
196 LYXERR(Debug::FONT, "lyx!");
197 *ok = true;
198 return font;
199 }
200
201 LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
202 font.setStyleName(QString());
203 #endif
204
205 if (isChosenFont(font, family, QString())) {
206 LYXERR(Debug::FONT, "normal!");
207 *ok = true;
208 return font;
209 }
210
211 LYXERR(Debug::FONT, "Trying " << upper << " ... ");
212 font.setFamily(upper);
213
214 if (isChosenFont(font, upper, QString())) {
215 LYXERR(Debug::FONT, "upper!");
216 *ok = true;
217 return font;
218 }
219
220 // A simple setFamily() fails on Qt 2
221
222 QString const raw = rawName(family);
223 LYXERR(Debug::FONT, "Trying " << raw << " ... ");
224 font.setRawName(raw);
225
226 if (isChosenFont(font, family, QString())) {
227 LYXERR(Debug::FONT, "raw version!");
228 *ok = true;
229 return font;
230 }
231
232 LYXERR(Debug::FONT, " FAILED :-(");
233 *ok = false;
234 return font;
235 }
236
237 } // namespace
238
239
FontLoader()240 FontLoader::FontLoader()
241 {
242 QString const fonts_dir =
243 toqstr(addPath(package().system_support().absFileName(), "fonts"));
244
245 for (int i = 0 ; i < num_math_fonts; ++i) {
246 QString const font_file = fonts_dir + math_fonts[i] + ".ttf";
247 int fontID = QFontDatabase::addApplicationFont(font_file);
248
249 LYXERR(Debug::FONT, "Adding font " << font_file
250 << (fontID < 0 ? " FAIL" : " OK"));
251 }
252
253 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
254 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
255 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
256 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
257 for (int i5 = 0; i5 < NUM_STYLE; ++i5)
258 fontinfo_[i1][i2][i3][i4][i5] = 0;
259 }
260
261
update()262 void FontLoader::update()
263 {
264 for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
265 for (int i2 = 0; i2 < NUM_SERIES; ++i2)
266 for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
267 for (int i4 = 0; i4 < NUM_SIZE; ++i4)
268 for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
269 delete fontinfo_[i1][i2][i3][i4][i5];
270 fontinfo_[i1][i2][i3][i4][i5] = 0;
271 }
272 }
273
274
~FontLoader()275 FontLoader::~FontLoader()
276 {
277 update();
278 }
279
280 /////////////////////////////////////////////////
281
282 namespace {
283
makeFontName(QString const & family,QString const & foundry)284 QString makeFontName(QString const & family, QString const & foundry)
285 {
286 QString res = family;
287 if (!foundry.isEmpty())
288 res += " [" + foundry + ']';
289 return res;
290 }
291
292
makeQFont(FontInfo const & f)293 QFont makeQFont(FontInfo const & f)
294 {
295 QFont font;
296 QString const pat = symbolFamily(f.family());
297 if (!pat.isEmpty()) {
298 bool ok;
299 font = symbolFont(pat, &ok);
300 } else {
301 switch (f.family()) {
302 case ROMAN_FAMILY: {
303 QString family = makeFontName(toqstr(lyxrc.roman_font_name),
304 toqstr(lyxrc.roman_font_foundry));
305 font.setFamily(family);
306 #ifdef Q_OS_MAC
307 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
308 // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
309 // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
310 // FIXME: Check whether this is really fixed in Qt 4.8
311 if (family == "Times" && !font.exactMatch())
312 font.setFamily("Times New Roman");
313 #endif
314 #endif
315 break;
316 }
317 case SANS_FAMILY:
318 font.setFamily(makeFontName(toqstr(lyxrc.sans_font_name),
319 toqstr(lyxrc.sans_font_foundry)));
320 break;
321 case TYPEWRITER_FAMILY:
322 font.setFamily(makeFontName(toqstr(lyxrc.typewriter_font_name),
323 toqstr(lyxrc.typewriter_font_foundry)));
324 break;
325 default:
326 break;
327 }
328 }
329
330 switch (f.series()) {
331 case MEDIUM_SERIES:
332 font.setWeight(QFont::Normal);
333 break;
334 case BOLD_SERIES:
335 font.setWeight(QFont::Bold);
336 break;
337 default:
338 break;
339 }
340
341 switch (f.realShape()) {
342 case ITALIC_SHAPE:
343 font.setStyle(QFont::StyleItalic);
344 break;
345 case SLANTED_SHAPE:
346 font.setStyle(QFont::StyleOblique);
347 break;
348 case SMALLCAPS_SHAPE:
349 font.setCapitalization(QFont::SmallCaps);
350 break;
351 default:
352 break;
353 }
354
355 LYXERR(Debug::FONT, "Font '" << stateText(f)
356 << "' matched by\n" << font.family());
357
358 // Is this an exact match?
359 if (font.exactMatch())
360 LYXERR(Debug::FONT, "This font is an exact match");
361 else
362 LYXERR(Debug::FONT, "This font is NOT an exact match");
363
364 LYXERR(Debug::FONT, "XFLD: " << font.rawName());
365
366 font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
367
368 LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
369
370 return font;
371 }
372
373 } // namespace
374
375
GuiFontInfo(FontInfo const & f)376 GuiFontInfo::GuiFontInfo(FontInfo const & f)
377 : font(makeQFont(f)), metrics(font)
378 {}
379
380
available(FontInfo const & f)381 bool FontLoader::available(FontInfo const & f)
382 {
383 // FIXME THREAD
384 static vector<int> cache_set(NUM_FAMILIES, false);
385 static vector<int> cache(NUM_FAMILIES, false);
386
387 FontFamily family = f.family();
388 #ifdef Q_OS_MAC
389 // Apple ships a font name "Symbol", which has more or less the same
390 // glyphs as the original PostScript Symbol font, but it uses a different
391 // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).
392 // Since we expect the font specific encoding of the original
393 // PostScript Symbol font, we can't use the one provided on OS X.
394 // See also the discussion in bug 7954.
395 if (f.family() == SYMBOL_FAMILY)
396 return false;
397 #endif
398 if (cache_set[family])
399 return cache[family];
400 cache_set[family] = true;
401
402 QString const pat = symbolFamily(family);
403 if (pat.isEmpty())
404 // We don't care about non-symbol fonts
405 return false;
406
407 bool ok;
408 symbolFont(pat, &ok);
409 if (!ok)
410 return false;
411
412 cache[family] = true;
413 return true;
414 }
415
416
canBeDisplayed(char_type c)417 bool FontLoader::canBeDisplayed(char_type c)
418 {
419 // bug 8493
420 if (c == 0x0009)
421 // FIXME check whether this is still needed for Qt5
422 return false;
423 #if QT_VERSION < 0x050000 && defined(QT_MAC_USE_COCOA) && (QT_MAC_USE_COCOA > 0)
424 // bug 7954, see also comment in GuiPainter::text()
425 if (c == 0x00ad)
426 return false;
427 #endif
428 return true;
429 }
430
431
metrics(FontInfo const & f)432 FontMetrics const & FontLoader::metrics(FontInfo const & f)
433 {
434 return fontinfo(f).metrics;
435 }
436
437
getFontMetrics(FontInfo const & f)438 GuiFontMetrics const & getFontMetrics(FontInfo const & f)
439 {
440 return fontinfo(f).metrics;
441 }
442
443
getFont(FontInfo const & f)444 QFont const & getFont(FontInfo const & f)
445 {
446 return fontinfo(f).font;
447 }
448
449 } // namespace frontend
450 } // namespace lyx
451