3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
8 * Full author contact details are available in file CREDITS.
13 #include "xfont_loader.h"
17 #include "lyxrc.h" // lyxrc.font_*
19 #include "frontends/lyx_gui.h"
21 #include "support/filetools.h"
22 #include "support/systemcall.h"
24 #include "lyx_forms.h"
28 using lyx::support::LibFileSearch;
29 using lyx::support::OnlyPath;
30 using lyx::support::Systemcall;
35 // The global fontloader
36 xfont_loader fontloader;
39 // Initialize font loader
40 xfont_loader::xfont_loader()
46 // Destroy font loader
47 xfont_loader::~xfont_loader()
53 // Update fonts after zoom, dpi, font names, or norm change
54 // For now, we just ditch all fonts we have. Later, we should
55 // reuse the ones that are already loaded.
56 void xfont_loader::update()
63 void xfont_loader::reset()
65 // Clear font infos, font structs and font metrics
66 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
67 for (int i2 = 0; i2 < 2; ++i2)
68 for (int i3 = 0; i3 < 4; ++i3) {
69 fontinfo[i1][i2][i3] = 0;
70 for (int i4 = 0; i4<10; ++i4) {
71 fontstruct[i1][i2][i3][i4] = 0;
78 void xfont_loader::unload()
81 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
82 for (int i2 = 0; i2 < 2; ++i2)
83 for (int i3 = 0; i3 < 4; ++i3) {
84 if (fontinfo[i1][i2][i3]) {
85 delete fontinfo[i1][i2][i3];
86 fontinfo[i1][i2][i3] = 0;
88 for (int i4 = 0; i4 < 10; ++i4) {
89 if (fontstruct[i1][i2][i3][i4]) {
90 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
91 fontstruct[i1][i2][i3][i4] = 0;
99 string const symbolPattern(LyXFont::FONT_FAMILY family)
102 case LyXFont::SYMBOL_FAMILY:
103 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
105 case LyXFont::CMR_FAMILY:
106 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
108 case LyXFont::CMSY_FAMILY:
109 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
111 case LyXFont::CMM_FAMILY:
112 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
114 case LyXFont::CMEX_FAMILY:
115 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
117 case LyXFont::MSA_FAMILY:
118 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
120 case LyXFont::MSB_FAMILY:
121 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
123 case LyXFont::EUFRAK_FAMILY:
124 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
126 case LyXFont::WASY_FAMILY:
127 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
134 string const fontName(string const & family, string const & foundry)
136 if (foundry.empty() || foundry == "Xft")
139 return "-"+foundry+"-"+family;
145 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
148 char ** p = XGetFontPath(fl_get_display(), &n);
149 if (std::find(p, p + n, dir) != p + n)
151 lyxerr[Debug::FONT] << "Adding " << dir
152 << " to the font path." << endl;
153 string const command = "xset fp+ " + dir;
155 if (!s.startscript(Systemcall::Wait, command))
157 lyxerr << "Unable to add " << dir << "to the font path."
166 /* Takes care of finding which font that can match the given request. Tries
167 different alternatives. */
168 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
169 LyXFont::FONT_SERIES series,
170 LyXFont::FONT_SHAPE shape)
172 // Do we have the font info already?
173 if (fontinfo[family][series][shape] != 0)
177 string pat = symbolPattern(family);
179 static bool first_time = true;
180 fontinfo[family][series][shape] = new FontInfo(pat);
181 if (family != LyXFont::SYMBOL_FAMILY &&
182 !fontinfo[family][series][shape]->exist() &&
186 delete fontinfo[family][series][shape];
187 fontinfo[family][series][shape] = new FontInfo(pat);
194 // Normal font. Let's search for an existing name that matches.
198 string norm = lyxrc.font_norm;
201 FontInfo * fi = new FontInfo;
202 fontinfo[family][series][shape] = fi;
204 for (int cfam = 0; cfam < 2; ++cfam) {
205 // Determine family name
207 case LyXFont::ROMAN_FAMILY:
209 case 0: ffamily = fontName(lyxrc.roman_font_name,
210 lyxrc.roman_font_foundry);
212 case 1: ffamily = "-*-times";
216 case LyXFont::SANS_FAMILY:
218 case 0: ffamily = fontName(lyxrc.sans_font_name,
219 lyxrc.sans_font_foundry);
221 case 1: ffamily = "-*-helvetica";
225 case LyXFont::TYPEWRITER_FAMILY:
227 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
228 lyxrc.typewriter_font_foundry);
230 case 1: ffamily = "-*-courier";
237 for (int cser = 0; cser < 4; ++cser) {
238 // Determine series name
240 case LyXFont::MEDIUM_SERIES:
242 case 0: fseries = "-medium"; break;
243 case 1: fseries = "-book"; break;
244 case 2: fseries = "-light";
248 case LyXFont::BOLD_SERIES:
250 case 0: fseries = "-bold"; break;
251 case 1: fseries = "-black"; break;
252 case 2: fseries = "-demi"; break;
253 case 3: fseries = "-demibold";
260 for (int csha = 0; csha < 2; ++csha) {
261 // Determine shape name
263 case LyXFont::UP_SHAPE:
264 case LyXFont::SMALLCAPS_SHAPE:
266 case 0: fshape = "-r";
270 case LyXFont::ITALIC_SHAPE:
272 case 0: fshape = "-i"; break;
273 case 1: fshape = "-o";
277 case LyXFont::SLANTED_SHAPE:
279 case 0: fshape = "-o"; break;
280 case 1: fshape = "-i";
287 fontname = ffamily + fseries + fshape +
288 "-normal-*-*-*-*-*-*-*-" + norm;
289 fi->setPattern(fontname);
299 // A dummy fontstruct used when there is no gui.
302 XFontStruct dummyXFontStruct;
303 bool dummyXFontStructisGood = false;
308 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
309 LyXFont::FONT_SERIES series,
310 LyXFont::FONT_SHAPE shape,
311 LyXFont::FONT_SIZE size)
313 if (!lyx_gui::use_gui) {
314 if (!dummyXFontStructisGood) {
315 // no character specific info
316 dummyXFontStruct.per_char = 0;
317 // unit ascent on character displays
318 dummyXFontStruct.ascent = 1;
319 // no descent on character displays
320 dummyXFontStruct.descent = 0;
321 dummyXFontStructisGood = true;
324 return &dummyXFontStruct;
327 getFontinfo(family, series, shape);
328 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
329 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
330 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
332 string font = fontinfo[family][series][shape]->getFontname(fsize);
335 lyxerr << "No font matches request. Using 'fixed'." << endl;
336 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
340 XFontStruct * fs = 0;
342 fs = XLoadQueryFont(fl_get_display(), font.c_str());
345 if (font == "fixed") {
346 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
348 lyxerr << "Could not get font '" << font
349 << "'. Using 'fixed'." << endl;
350 fs = XLoadQueryFont(fl_get_display(), "fixed");
352 } else if (lyxerr.debugging(Debug::FONT)) {
353 // Tell user the font matching
359 // The rest of the attributes are not interesting
360 f.setEmph(LyXFont::INHERIT);
361 f.setUnderbar(LyXFont::INHERIT);
362 f.setNoun(LyXFont::INHERIT);
363 f.setColor(LColor::inherit);
364 lyxerr << "Font '" << f.stateText(0)
365 << "' matched by\n" << font << endl;
368 fontstruct[family][series][shape][size] = fs;
373 bool xfont_loader::available(LyXFont const & f)
375 if (!lyx_gui::use_gui)
378 if (!fontinfo[f.family()][f.series()][f.realShape()])
379 getFontinfo(f.family(), f.series(), f.realShape());
380 return fontinfo[f.family()][f.series()][f.realShape()]->exist();