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 namespace lyx::support;
33 // The global fontloader
34 xfont_loader fontloader;
37 // Initialize font loader
38 xfont_loader::xfont_loader()
44 // Destroy font loader
45 xfont_loader::~xfont_loader()
51 // Update fonts after zoom, dpi, font names, or norm change
52 // For now, we just ditch all fonts we have. Later, we should
53 // reuse the ones that are already loaded.
54 void xfont_loader::update()
61 void xfont_loader::reset()
63 // Clear font infos, font structs and font metrics
64 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
65 for (int i2 = 0; i2 < 2; ++i2)
66 for (int i3 = 0; i3 < 4; ++i3) {
67 fontinfo[i1][i2][i3] = 0;
68 for (int i4 = 0; i4<10; ++i4) {
69 fontstruct[i1][i2][i3][i4] = 0;
76 void xfont_loader::unload()
79 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
80 for (int i2 = 0; i2 < 2; ++i2)
81 for (int i3 = 0; i3 < 4; ++i3) {
82 if (fontinfo[i1][i2][i3]) {
83 delete fontinfo[i1][i2][i3];
84 fontinfo[i1][i2][i3] = 0;
86 for (int i4 = 0; i4 < 10; ++i4) {
87 if (fontstruct[i1][i2][i3][i4]) {
88 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
89 fontstruct[i1][i2][i3][i4] = 0;
97 string const symbolPattern(LyXFont::FONT_FAMILY family)
100 case LyXFont::SYMBOL_FAMILY:
101 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
103 case LyXFont::CMR_FAMILY:
104 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
106 case LyXFont::CMSY_FAMILY:
107 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
109 case LyXFont::CMM_FAMILY:
110 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
112 case LyXFont::CMEX_FAMILY:
113 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
115 case LyXFont::MSA_FAMILY:
116 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
118 case LyXFont::MSB_FAMILY:
119 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
121 case LyXFont::EUFRAK_FAMILY:
122 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
124 case LyXFont::WASY_FAMILY:
125 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
132 string const fontName(string const & family, string const & foundry)
134 if (foundry.empty() || foundry == "Xft")
137 return "-"+foundry+"-"+family;
143 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
146 char ** p = XGetFontPath(fl_get_display(), &n);
147 if (std::find(p, p + n, dir) != p + n)
149 lyxerr[Debug::FONT] << "Adding " << dir
150 << " to the font path." << endl;
151 string const command = "xset fp+ " + dir;
153 if (!s.startscript(Systemcall::Wait, command))
155 lyxerr << "Unable to add " << dir << "to the font path."
164 /* Takes care of finding which font that can match the given request. Tries
165 different alternatives. */
166 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
167 LyXFont::FONT_SERIES series,
168 LyXFont::FONT_SHAPE shape)
170 // Do we have the font info already?
171 if (fontinfo[family][series][shape] != 0)
175 string pat = symbolPattern(family);
177 static bool first_time = true;
178 fontinfo[family][series][shape] = new FontInfo(pat);
179 if (family != LyXFont::SYMBOL_FAMILY &&
180 !fontinfo[family][series][shape]->exist() &&
184 delete fontinfo[family][series][shape];
185 fontinfo[family][series][shape] = new FontInfo(pat);
192 // Normal font. Let's search for an existing name that matches.
196 string norm = lyxrc.font_norm;
199 FontInfo * fi = new FontInfo;
200 fontinfo[family][series][shape] = fi;
202 for (int cfam = 0; cfam < 2; ++cfam) {
203 // Determine family name
205 case LyXFont::ROMAN_FAMILY:
207 case 0: ffamily = fontName(lyxrc.roman_font_name,
208 lyxrc.roman_font_foundry);
210 case 1: ffamily = "-*-times";
214 case LyXFont::SANS_FAMILY:
216 case 0: ffamily = fontName(lyxrc.sans_font_name,
217 lyxrc.sans_font_foundry);
219 case 1: ffamily = "-*-helvetica";
223 case LyXFont::TYPEWRITER_FAMILY:
225 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
226 lyxrc.typewriter_font_foundry);
228 case 1: ffamily = "-*-courier";
235 for (int cser = 0; cser < 4; ++cser) {
236 // Determine series name
238 case LyXFont::MEDIUM_SERIES:
240 case 0: fseries = "-medium"; break;
241 case 1: fseries = "-book"; break;
242 case 2: fseries = "-light";
246 case LyXFont::BOLD_SERIES:
248 case 0: fseries = "-bold"; break;
249 case 1: fseries = "-black"; break;
250 case 2: fseries = "-demi"; break;
251 case 3: fseries = "-demibold";
258 for (int csha = 0; csha < 2; ++csha) {
259 // Determine shape name
261 case LyXFont::UP_SHAPE:
262 case LyXFont::SMALLCAPS_SHAPE:
264 case 0: fshape = "-r";
268 case LyXFont::ITALIC_SHAPE:
270 case 0: fshape = "-i"; break;
271 case 1: fshape = "-o";
275 case LyXFont::SLANTED_SHAPE:
277 case 0: fshape = "-o"; break;
278 case 1: fshape = "-i";
285 fontname = ffamily + fseries + fshape +
286 "-normal-*-*-*-*-*-*-*-" + norm;
287 fi->setPattern(fontname);
297 // A dummy fontstruct used when there is no gui.
300 XFontStruct dummyXFontStruct;
301 bool dummyXFontStructisGood = false;
306 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
307 LyXFont::FONT_SERIES series,
308 LyXFont::FONT_SHAPE shape,
309 LyXFont::FONT_SIZE size)
311 if (!lyx_gui::use_gui) {
312 if (!dummyXFontStructisGood) {
313 // no character specific info
314 dummyXFontStruct.per_char = 0;
315 // unit ascent on character displays
316 dummyXFontStruct.ascent = 1;
317 // no descent on character displays
318 dummyXFontStruct.descent = 0;
319 dummyXFontStructisGood = true;
322 return &dummyXFontStruct;
325 getFontinfo(family, series, shape);
326 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
327 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
328 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
330 string font = fontinfo[family][series][shape]->getFontname(fsize);
333 lyxerr << "No font matches request. Using 'fixed'." << endl;
334 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
338 XFontStruct * fs = 0;
340 fs = XLoadQueryFont(fl_get_display(), font.c_str());
343 if (font == "fixed") {
344 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
346 lyxerr << "Could not get font '" << font
347 << "'. Using 'fixed'." << endl;
348 fs = XLoadQueryFont(fl_get_display(), "fixed");
350 } else if (lyxerr.debugging(Debug::FONT)) {
351 // Tell user the font matching
357 // The rest of the attributes are not interesting
358 f.setEmph(LyXFont::INHERIT);
359 f.setUnderbar(LyXFont::INHERIT);
360 f.setNoun(LyXFont::INHERIT);
361 f.setColor(LColor::inherit);
362 lyxerr << "Font '" << f.stateText(0)
363 << "' matched by\n" << font << endl;
366 fontstruct[family][series][shape][size] = fs;
371 bool xfont_loader::available(LyXFont const & f)
373 if (!lyx_gui::use_gui)
376 if (!fontinfo[f.family()][f.series()][f.realShape()])
377 getFontinfo(f.family(), f.series(), f.realShape());
378 return fontinfo[f.family()][f.series()][f.realShape()]->exist();