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;
37 // The global fontloader
38 xfont_loader fontloader;
41 // Initialize font loader
42 xfont_loader::xfont_loader()
48 // Destroy font loader
49 xfont_loader::~xfont_loader()
55 // Update fonts after zoom, dpi, font names, or norm change
56 // For now, we just ditch all fonts we have. Later, we should
57 // reuse the ones that are already loaded.
58 void xfont_loader::update()
65 void xfont_loader::reset()
67 // Clear font infos, font structs and font metrics
68 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
69 for (int i2 = 0; i2 < 2; ++i2)
70 for (int i3 = 0; i3 < 4; ++i3) {
71 fontinfo[i1][i2][i3] = 0;
72 for (int i4 = 0; i4<10; ++i4) {
73 fontstruct[i1][i2][i3][i4] = 0;
80 void xfont_loader::unload()
83 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
84 for (int i2 = 0; i2 < 2; ++i2)
85 for (int i3 = 0; i3 < 4; ++i3) {
86 if (fontinfo[i1][i2][i3]) {
87 delete fontinfo[i1][i2][i3];
88 fontinfo[i1][i2][i3] = 0;
90 for (int i4 = 0; i4 < 10; ++i4) {
91 if (fontstruct[i1][i2][i3][i4]) {
92 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
93 fontstruct[i1][i2][i3][i4] = 0;
101 string const symbolPattern(LyXFont::FONT_FAMILY family)
104 case LyXFont::SYMBOL_FAMILY:
105 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
107 case LyXFont::CMR_FAMILY:
108 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
110 case LyXFont::CMSY_FAMILY:
111 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
113 case LyXFont::CMM_FAMILY:
114 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
116 case LyXFont::CMEX_FAMILY:
117 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
119 case LyXFont::MSA_FAMILY:
120 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
122 case LyXFont::MSB_FAMILY:
123 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
125 case LyXFont::EUFRAK_FAMILY:
126 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
128 case LyXFont::WASY_FAMILY:
129 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
136 string const fontName(string const & family, string const & foundry)
138 if (foundry.empty() || foundry == "Xft")
141 return "-"+foundry+"-"+family;
147 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
150 char ** p = XGetFontPath(fl_get_display(), &n);
151 if (std::find(p, p + n, dir) != p + n)
153 lyxerr[Debug::FONT] << "Adding " << dir
154 << " to the font path." << endl;
155 string const command = "xset fp+ " + dir;
157 if (!s.startscript(Systemcall::Wait, command))
159 lyxerr << "Unable to add " << dir << "to the font path."
168 /* Takes care of finding which font that can match the given request. Tries
169 different alternatives. */
170 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
171 LyXFont::FONT_SERIES series,
172 LyXFont::FONT_SHAPE shape)
174 // Do we have the font info already?
175 if (fontinfo[family][series][shape] != 0)
179 string pat = symbolPattern(family);
181 static bool first_time = true;
182 fontinfo[family][series][shape] = new FontInfo(pat);
183 if (family != LyXFont::SYMBOL_FAMILY &&
184 !fontinfo[family][series][shape]->exist() &&
188 delete fontinfo[family][series][shape];
189 fontinfo[family][series][shape] = new FontInfo(pat);
196 // Normal font. Let's search for an existing name that matches.
200 string norm = lyxrc.font_norm;
203 FontInfo * fi = new FontInfo;
204 fontinfo[family][series][shape] = fi;
206 for (int cfam = 0; cfam < 2; ++cfam) {
207 // Determine family name
209 case LyXFont::ROMAN_FAMILY:
211 case 0: ffamily = fontName(lyxrc.roman_font_name,
212 lyxrc.roman_font_foundry);
214 case 1: ffamily = "-*-times";
218 case LyXFont::SANS_FAMILY:
220 case 0: ffamily = fontName(lyxrc.sans_font_name,
221 lyxrc.sans_font_foundry);
223 case 1: ffamily = "-*-helvetica";
227 case LyXFont::TYPEWRITER_FAMILY:
229 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
230 lyxrc.typewriter_font_foundry);
232 case 1: ffamily = "-*-courier";
239 for (int cser = 0; cser < 4; ++cser) {
240 // Determine series name
242 case LyXFont::MEDIUM_SERIES:
244 case 0: fseries = "-medium"; break;
245 case 1: fseries = "-book"; break;
246 case 2: fseries = "-light";
250 case LyXFont::BOLD_SERIES:
252 case 0: fseries = "-bold"; break;
253 case 1: fseries = "-black"; break;
254 case 2: fseries = "-demi"; break;
255 case 3: fseries = "-demibold";
262 for (int csha = 0; csha < 2; ++csha) {
263 // Determine shape name
265 case LyXFont::UP_SHAPE:
266 case LyXFont::SMALLCAPS_SHAPE:
268 case 0: fshape = "-r";
272 case LyXFont::ITALIC_SHAPE:
274 case 0: fshape = "-i"; break;
275 case 1: fshape = "-o";
279 case LyXFont::SLANTED_SHAPE:
281 case 0: fshape = "-o"; break;
282 case 1: fshape = "-i";
289 fontname = ffamily + fseries + fshape +
290 "-normal-*-*-*-*-*-*-*-" + norm;
291 fi->setPattern(fontname);
301 // A dummy fontstruct used when there is no gui.
304 XFontStruct dummyXFontStruct;
305 bool dummyXFontStructisGood = false;
310 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
311 LyXFont::FONT_SERIES series,
312 LyXFont::FONT_SHAPE shape,
313 LyXFont::FONT_SIZE size)
315 if (!lyx_gui::use_gui) {
316 if (!dummyXFontStructisGood) {
317 // no character specific info
318 dummyXFontStruct.per_char = 0;
319 // unit ascent on character displays
320 dummyXFontStruct.ascent = 1;
321 // no descent on character displays
322 dummyXFontStruct.descent = 0;
323 dummyXFontStructisGood = true;
326 return &dummyXFontStruct;
329 getFontinfo(family, series, shape);
330 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
331 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
332 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
334 string font = fontinfo[family][series][shape]->getFontname(fsize);
337 lyxerr << "No font matches request. Using 'fixed'." << endl;
338 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
342 XFontStruct * fs = 0;
344 fs = XLoadQueryFont(fl_get_display(), font.c_str());
347 if (font == "fixed") {
348 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
350 lyxerr << "Could not get font '" << font
351 << "'. Using 'fixed'." << endl;
352 fs = XLoadQueryFont(fl_get_display(), "fixed");
354 } else if (lyxerr.debugging(Debug::FONT)) {
355 // Tell user the font matching
361 // The rest of the attributes are not interesting
362 f.setEmph(LyXFont::INHERIT);
363 f.setUnderbar(LyXFont::INHERIT);
364 f.setNoun(LyXFont::INHERIT);
365 f.setColor(LColor::inherit);
366 lyxerr << "Font '" << f.stateText(0)
367 << "' matched by\n" << font << endl;
370 fontstruct[family][series][shape][size] = fs;
375 bool xfont_loader::available(LyXFont const & f)
377 if (!lyx_gui::use_gui)
380 if (!fontinfo[f.family()][f.series()][f.realShape()])
381 getFontinfo(f.family(), f.series(), f.realShape());
382 return fontinfo[f.family()][f.series()][f.realShape()]->exist();