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
12 #include <cmath> // fabs()
15 #include "xfont_loader.h"
19 #include "lyxrc.h" // lyxrc.font_*
20 #include "BufferView.h"
21 #include "frontends/LyXView.h"
22 #include "support/systemcall.h"
23 #include "support/filetools.h"
25 #include FORMS_H_LOCATION
29 // The global fontloader
30 xfont_loader fontloader;
33 // Initialize font loader
34 xfont_loader::xfont_loader()
40 // Destroy font loader
41 xfont_loader::~xfont_loader()
47 // Update fonts after zoom, dpi, font names, or norm change
48 // For now, we just ditch all fonts we have. Later, we should
49 // reuse the ones that are already loaded.
50 void xfont_loader::update()
57 void xfont_loader::reset()
59 // Clear font infos, font structs and font metrics
60 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
61 for (int i2 = 0; i2 < 2; ++i2)
62 for (int i3 = 0; i3 < 4; ++i3) {
63 fontinfo[i1][i2][i3] = 0;
64 for (int i4 = 0; i4<10; ++i4) {
65 fontstruct[i1][i2][i3][i4] = 0;
72 void xfont_loader::unload()
75 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
76 for (int i2 = 0; i2 < 2; ++i2)
77 for (int i3 = 0; i3 < 4; ++i3) {
78 if (fontinfo[i1][i2][i3]) {
79 delete fontinfo[i1][i2][i3];
80 fontinfo[i1][i2][i3] = 0;
82 for (int i4 = 0; i4 < 10; ++i4) {
83 if (fontstruct[i1][i2][i3][i4]) {
84 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
85 fontstruct[i1][i2][i3][i4] = 0;
93 string const symbolPattern(LyXFont::FONT_FAMILY family)
96 case LyXFont::SYMBOL_FAMILY:
97 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
99 case LyXFont::CMR_FAMILY:
100 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
102 case LyXFont::CMSY_FAMILY:
103 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
105 case LyXFont::CMM_FAMILY:
106 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
108 case LyXFont::CMEX_FAMILY:
109 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
111 case LyXFont::MSA_FAMILY:
112 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
114 case LyXFont::MSB_FAMILY:
115 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
117 case LyXFont::EUFRAK_FAMILY:
118 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
120 case LyXFont::WASY_FAMILY:
121 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
128 string const fontName(string const & family, string const & foundry)
130 if (foundry.empty() || foundry == "Xft")
133 return "-"+foundry+"-"+family;
139 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
142 char ** p = XGetFontPath(fl_get_display(), &n);
143 if (std::find(p, p + n, dir) != p + n)
145 lyxerr[Debug::FONT] << "Adding " << dir
146 << " to the font path." << endl;
147 string const command = "xset fp+ " + dir;
149 if (!s.startscript(Systemcall::Wait, command))
151 lyxerr << "Unable to add " << dir << "to the font path."
160 /* Takes care of finding which font that can match the given request. Tries
161 different alternatives. */
162 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
163 LyXFont::FONT_SERIES series,
164 LyXFont::FONT_SHAPE shape)
166 // Do we have the font info already?
167 if (fontinfo[family][series][shape] != 0)
171 string pat = symbolPattern(family);
173 static bool first_time = true;
174 fontinfo[family][series][shape] = new FontInfo(pat);
175 if (family != LyXFont::SYMBOL_FAMILY &&
176 !fontinfo[family][series][shape]->exist() &&
180 delete fontinfo[family][series][shape];
181 fontinfo[family][series][shape] = new FontInfo(pat);
188 // Normal font. Let's search for an existing name that matches.
192 string norm = lyxrc.font_norm;
195 FontInfo * fi = new FontInfo;
196 fontinfo[family][series][shape] = fi;
198 for (int cfam = 0; cfam < 2; ++cfam) {
199 // Determine family name
201 case LyXFont::ROMAN_FAMILY:
203 case 0: ffamily = fontName(lyxrc.roman_font_name,
204 lyxrc.roman_font_foundry);
206 case 1: ffamily = "-*-times";
210 case LyXFont::SANS_FAMILY:
212 case 0: ffamily = fontName(lyxrc.sans_font_name,
213 lyxrc.sans_font_foundry);
215 case 1: ffamily = "-*-helvetica";
219 case LyXFont::TYPEWRITER_FAMILY:
221 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
222 lyxrc.typewriter_font_foundry);
224 case 1: ffamily = "-*-courier";
231 for (int cser = 0; cser < 4; ++cser) {
232 // Determine series name
234 case LyXFont::MEDIUM_SERIES:
236 case 0: fseries = "-medium"; break;
237 case 1: fseries = "-book"; break;
238 case 2: fseries = "-light";
242 case LyXFont::BOLD_SERIES:
244 case 0: fseries = "-bold"; break;
245 case 1: fseries = "-black"; break;
246 case 2: fseries = "-demi"; break;
247 case 3: fseries = "-demibold";
254 for (int csha = 0; csha < 2; ++csha) {
255 // Determine shape name
257 case LyXFont::UP_SHAPE:
258 case LyXFont::SMALLCAPS_SHAPE:
260 case 0: fshape = "-r";
264 case LyXFont::ITALIC_SHAPE:
266 case 0: fshape = "-i"; break;
267 case 1: fshape = "-o";
271 case LyXFont::SLANTED_SHAPE:
273 case 0: fshape = "-o"; break;
274 case 1: fshape = "-i";
281 fontname = ffamily + fseries + fshape +
282 "-normal-*-*-*-*-*-*-*-" + norm;
283 fi->setPattern(fontname);
293 // A dummy fontstruct used when there is no gui.
296 XFontStruct dummyXFontStruct;
297 bool dummyXFontStructisGood = false;
302 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
303 LyXFont::FONT_SERIES series,
304 LyXFont::FONT_SHAPE shape,
305 LyXFont::FONT_SIZE size)
307 if (!lyx_gui::use_gui) {
308 if (!dummyXFontStructisGood) {
309 // no character specific info
310 dummyXFontStruct.per_char = 0;
311 // unit ascent on character displays
312 dummyXFontStruct.ascent = 1;
313 // no descent on character displays
314 dummyXFontStruct.descent = 0;
315 dummyXFontStructisGood = true;
318 return &dummyXFontStruct;
321 getFontinfo(family, series, shape);
322 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
323 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
324 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
326 string font = fontinfo[family][series][shape]->getFontname(fsize);
329 lyxerr << "No font matches request. Using 'fixed'." << endl;
330 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
334 XFontStruct * fs = 0;
336 fs = XLoadQueryFont(fl_get_display(), font.c_str());
339 if (font == "fixed") {
340 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
342 lyxerr << "Could not get font '" << font
343 << "'. Using 'fixed'." << endl;
344 fs = XLoadQueryFont(fl_get_display(), "fixed");
346 } else if (lyxerr.debugging(Debug::FONT)) {
347 // Tell user the font matching
353 // The rest of the attributes are not interesting
354 f.setEmph(LyXFont::INHERIT);
355 f.setUnderbar(LyXFont::INHERIT);
356 f.setNoun(LyXFont::INHERIT);
357 f.setColor(LColor::inherit);
358 lyxerr << "Font '" << f.stateText(0)
359 << "' matched by\n" << font << endl;
362 fontstruct[family][series][shape][size] = fs;
367 bool xfont_loader::available(LyXFont const & f)
369 if (!lyx_gui::use_gui)
372 if (!fontinfo[f.family()][f.series()][f.realShape()])
373 getFontinfo(f.family(), f.series(), f.realShape());
374 return fontinfo[f.family()][f.series()][f.realShape()]->exist();