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"
18 #include "lyxrc.h" // lyxrc.font_*
20 #include "frontends/lyx_gui.h"
22 #include "support/filetools.h"
23 #include "support/systemcall.h"
25 #include "lyx_forms.h"
29 using lyx::support::LibFileSearch;
30 using lyx::support::OnlyPath;
31 using lyx::support::Systemcall;
36 // The global fontloader
37 xfont_loader fontloader;
40 // Initialize font loader
41 xfont_loader::xfont_loader()
47 // Destroy font loader
48 xfont_loader::~xfont_loader()
54 // Update fonts after zoom, dpi, font names, or norm change
55 // For now, we just ditch all fonts we have. Later, we should
56 // reuse the ones that are already loaded.
57 void xfont_loader::update()
64 void xfont_loader::reset()
66 // Clear font infos, font structs and font metrics
67 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
68 for (int i2 = 0; i2 < 2; ++i2)
69 for (int i3 = 0; i3 < 4; ++i3) {
70 fontinfo[i1][i2][i3] = 0;
71 for (int i4 = 0; i4<10; ++i4) {
72 fontstruct[i1][i2][i3][i4] = 0;
79 void xfont_loader::unload()
82 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
83 for (int i2 = 0; i2 < 2; ++i2)
84 for (int i3 = 0; i3 < 4; ++i3) {
85 if (fontinfo[i1][i2][i3]) {
86 delete fontinfo[i1][i2][i3];
87 fontinfo[i1][i2][i3] = 0;
89 for (int i4 = 0; i4 < 10; ++i4) {
90 if (fontstruct[i1][i2][i3][i4]) {
91 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
92 fontstruct[i1][i2][i3][i4] = 0;
100 string const symbolPattern(LyXFont::FONT_FAMILY family)
103 case LyXFont::SYMBOL_FAMILY:
104 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
106 case LyXFont::CMR_FAMILY:
107 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
109 case LyXFont::CMSY_FAMILY:
110 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
112 case LyXFont::CMM_FAMILY:
113 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
115 case LyXFont::CMEX_FAMILY:
116 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
118 case LyXFont::MSA_FAMILY:
119 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
121 case LyXFont::MSB_FAMILY:
122 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
124 case LyXFont::EUFRAK_FAMILY:
125 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
127 case LyXFont::WASY_FAMILY:
128 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
135 string const fontName(string const & family, string const & foundry)
137 if (foundry.empty() || foundry == "Xft")
140 return "-"+foundry+"-"+family;
146 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
149 char ** p = XGetFontPath(fl_get_display(), &n);
150 if (std::find(p, p + n, dir) != p + n)
152 lyxerr[Debug::FONT] << "Adding " << dir
153 << " to the font path." << endl;
154 string const command = "xset fp+ " + dir;
156 if (!s.startscript(Systemcall::Wait, command))
158 lyxerr << "Unable to add " << dir << "to the font path."
167 /* Takes care of finding which font that can match the given request. Tries
168 different alternatives. */
169 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
170 LyXFont::FONT_SERIES series,
171 LyXFont::FONT_SHAPE shape)
173 // Do we have the font info already?
174 if (fontinfo[family][series][shape] != 0)
178 string pat = symbolPattern(family);
180 static bool first_time = true;
181 fontinfo[family][series][shape] = new FontInfo(pat);
182 if (family != LyXFont::SYMBOL_FAMILY &&
183 !fontinfo[family][series][shape]->exist() &&
187 delete fontinfo[family][series][shape];
188 fontinfo[family][series][shape] = new FontInfo(pat);
195 // Normal font. Let's search for an existing name that matches.
199 string norm = lyxrc.font_norm;
202 FontInfo * fi = new FontInfo;
203 fontinfo[family][series][shape] = fi;
205 for (int cfam = 0; cfam < 2; ++cfam) {
206 // Determine family name
208 case LyXFont::ROMAN_FAMILY:
210 case 0: ffamily = fontName(lyxrc.roman_font_name,
211 lyxrc.roman_font_foundry);
213 case 1: ffamily = "-*-times";
217 case LyXFont::SANS_FAMILY:
219 case 0: ffamily = fontName(lyxrc.sans_font_name,
220 lyxrc.sans_font_foundry);
222 case 1: ffamily = "-*-helvetica";
226 case LyXFont::TYPEWRITER_FAMILY:
228 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
229 lyxrc.typewriter_font_foundry);
231 case 1: ffamily = "-*-courier";
238 for (int cser = 0; cser < 4; ++cser) {
239 // Determine series name
241 case LyXFont::MEDIUM_SERIES:
243 case 0: fseries = "-medium"; break;
244 case 1: fseries = "-book"; break;
245 case 2: fseries = "-light";
249 case LyXFont::BOLD_SERIES:
251 case 0: fseries = "-bold"; break;
252 case 1: fseries = "-black"; break;
253 case 2: fseries = "-demi"; break;
254 case 3: fseries = "-demibold";
261 for (int csha = 0; csha < 2; ++csha) {
262 // Determine shape name
264 case LyXFont::UP_SHAPE:
265 case LyXFont::SMALLCAPS_SHAPE:
267 case 0: fshape = "-r";
271 case LyXFont::ITALIC_SHAPE:
273 case 0: fshape = "-i"; break;
274 case 1: fshape = "-o";
278 case LyXFont::SLANTED_SHAPE:
280 case 0: fshape = "-o"; break;
281 case 1: fshape = "-i";
288 fontname = ffamily + fseries + fshape +
289 "-normal-*-*-*-*-*-*-*-" + norm;
290 fi->setPattern(fontname);
300 // A dummy fontstruct used when there is no gui.
303 XFontStruct dummyXFontStruct;
304 bool dummyXFontStructisGood = false;
309 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
310 LyXFont::FONT_SERIES series,
311 LyXFont::FONT_SHAPE shape,
312 LyXFont::FONT_SIZE size)
314 if (!lyx_gui::use_gui) {
315 if (!dummyXFontStructisGood) {
316 // no character specific info
317 dummyXFontStruct.per_char = 0;
318 // unit ascent on character displays
319 dummyXFontStruct.ascent = 1;
320 // no descent on character displays
321 dummyXFontStruct.descent = 0;
322 dummyXFontStructisGood = true;
325 return &dummyXFontStruct;
328 getFontinfo(family, series, shape);
329 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
330 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
331 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
333 string font = fontinfo[family][series][shape]->getFontname(fsize);
336 lyxerr << "No font matches request. Using 'fixed'." << endl;
337 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
341 XFontStruct * fs = 0;
343 fs = XLoadQueryFont(fl_get_display(), font.c_str());
346 if (font == "fixed") {
347 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
349 lyxerr << "Could not get font '" << font
350 << "'. Using 'fixed'." << endl;
351 fs = XLoadQueryFont(fl_get_display(), "fixed");
353 } else if (lyxerr.debugging(Debug::FONT)) {
354 // Tell user the font matching
360 // The rest of the attributes are not interesting
361 f.setEmph(LyXFont::INHERIT);
362 f.setUnderbar(LyXFont::INHERIT);
363 f.setNoun(LyXFont::INHERIT);
364 f.setColor(LColor::inherit);
365 lyxerr << "Font '" << f.stateText(0)
366 << "' matched by\n" << font << endl;
369 fontstruct[family][series][shape][size] = fs;
374 bool xfont_loader::available(LyXFont const & f)
376 if (!lyx_gui::use_gui)
379 if (!fontinfo[f.family()][f.series()][f.realShape()])
380 getFontinfo(f.family(), f.series(), f.realShape());
381 return fontinfo[f.family()][f.series()][f.realShape()]->exist();