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/convert.h"
23 #include "support/filetools.h"
24 #include "support/systemcall.h"
26 #include "lyx_forms.h"
35 using support::LibFileSearch;
36 using support::OnlyPath;
37 using support::Systemcall;
41 // The global fontloader
42 xfont_loader fontloader;
45 // Initialize font loader
46 xfont_loader::xfont_loader()
52 // Destroy font loader
53 xfont_loader::~xfont_loader()
59 // Update fonts after zoom, dpi, font names, or norm change
60 // For now, we just ditch all fonts we have. Later, we should
61 // reuse the ones that are already loaded.
62 void xfont_loader::update()
69 void xfont_loader::reset()
71 // Clear font infos, font structs and font metrics
72 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
73 for (int i2 = 0; i2 < 2; ++i2)
74 for (int i3 = 0; i3 < 4; ++i3) {
75 fontinfo[i1][i2][i3] = 0;
76 for (int i4 = 0; i4<10; ++i4) {
77 fontstruct[i1][i2][i3][i4] = 0;
84 void xfont_loader::unload()
87 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
88 for (int i2 = 0; i2 < 2; ++i2)
89 for (int i3 = 0; i3 < 4; ++i3) {
90 if (fontinfo[i1][i2][i3]) {
91 delete fontinfo[i1][i2][i3];
92 fontinfo[i1][i2][i3] = 0;
94 for (int i4 = 0; i4 < 10; ++i4) {
95 if (fontstruct[i1][i2][i3][i4]) {
96 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
97 fontstruct[i1][i2][i3][i4] = 0;
105 string const symbolPattern(LyXFont::FONT_FAMILY family)
108 case LyXFont::SYMBOL_FAMILY:
109 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
111 case LyXFont::CMR_FAMILY:
112 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
114 case LyXFont::CMSY_FAMILY:
115 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
117 case LyXFont::CMM_FAMILY:
118 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
120 case LyXFont::CMEX_FAMILY:
121 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
123 case LyXFont::MSA_FAMILY:
124 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
126 case LyXFont::MSB_FAMILY:
127 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
129 case LyXFont::EUFRAK_FAMILY:
130 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
132 case LyXFont::WASY_FAMILY:
133 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
140 string const fontName(string const & family, string const & foundry)
142 if (foundry.empty() || foundry == "Xft")
145 return "-"+foundry+"-"+family;
151 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
154 char ** p = XGetFontPath(fl_get_display(), &n);
155 if (std::find(p, p + n, dir) != p + n)
157 lyxerr[Debug::FONT] << "Adding " << dir
158 << " to the font path." << endl;
159 string const command = "xset fp+ " + dir;
161 if (!s.startscript(Systemcall::Wait, command))
163 lyxerr << "Unable to add " << dir << "to the font path."
172 /* Takes care of finding which font that can match the given request. Tries
173 different alternatives. */
174 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
175 LyXFont::FONT_SERIES series,
176 LyXFont::FONT_SHAPE shape)
178 // Do we have the font info already?
179 if (fontinfo[family][series][shape] != 0)
183 string pat = symbolPattern(family);
185 static bool first_time = true;
186 fontinfo[family][series][shape] = new FontInfo(pat);
187 if (family != LyXFont::SYMBOL_FAMILY &&
188 !fontinfo[family][series][shape]->exist() &&
192 delete fontinfo[family][series][shape];
193 fontinfo[family][series][shape] = new FontInfo(pat);
200 // Normal font. Let's search for an existing name that matches.
204 string norm = lyxrc.font_norm;
207 FontInfo * fi = new FontInfo;
208 fontinfo[family][series][shape] = fi;
210 for (int cfam = 0; cfam < 2; ++cfam) {
211 // Determine family name
213 case LyXFont::ROMAN_FAMILY:
215 case 0: ffamily = fontName(lyxrc.roman_font_name,
216 lyxrc.roman_font_foundry);
218 case 1: ffamily = "-*-times";
222 case LyXFont::SANS_FAMILY:
224 case 0: ffamily = fontName(lyxrc.sans_font_name,
225 lyxrc.sans_font_foundry);
227 case 1: ffamily = "-*-helvetica";
231 case LyXFont::TYPEWRITER_FAMILY:
233 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
234 lyxrc.typewriter_font_foundry);
236 case 1: ffamily = "-*-courier";
243 for (int cser = 0; cser < 4; ++cser) {
244 // Determine series name
246 case LyXFont::MEDIUM_SERIES:
248 case 0: fseries = "-medium"; break;
249 case 1: fseries = "-book"; break;
250 case 2: fseries = "-light";
254 case LyXFont::BOLD_SERIES:
256 case 0: fseries = "-bold"; break;
257 case 1: fseries = "-black"; break;
258 case 2: fseries = "-demi"; break;
259 case 3: fseries = "-demibold";
266 for (int csha = 0; csha < 2; ++csha) {
267 // Determine shape name
269 case LyXFont::UP_SHAPE:
270 case LyXFont::SMALLCAPS_SHAPE:
272 case 0: fshape = "-r";
276 case LyXFont::ITALIC_SHAPE:
278 case 0: fshape = "-i"; break;
279 case 1: fshape = "-o";
283 case LyXFont::SLANTED_SHAPE:
285 case 0: fshape = "-o"; break;
286 case 1: fshape = "-i";
293 fontname = ffamily + fseries + fshape +
294 "-normal-*-*-*-*-*-*-*-" + norm;
295 fi->setPattern(fontname);
305 // A dummy fontstruct used when there is no gui.
308 XFontStruct dummyXFontStruct;
309 bool dummyXFontStructisGood = false;
314 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
315 LyXFont::FONT_SERIES series,
316 LyXFont::FONT_SHAPE shape,
317 LyXFont::FONT_SIZE size)
319 if (!lyx_gui::use_gui) {
320 if (!dummyXFontStructisGood) {
321 // no character specific info
322 dummyXFontStruct.per_char = 0;
323 // unit ascent on character displays
324 dummyXFontStruct.ascent = 1;
325 // no descent on character displays
326 dummyXFontStruct.descent = 0;
327 dummyXFontStructisGood = true;
330 return &dummyXFontStruct;
333 getFontinfo(family, series, shape);
334 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
335 int fsize = int((convert<double>(lyxrc.font_sizes[size]) * lyxrc.dpi *
336 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
338 string font = fontinfo[family][series][shape]->getFontname(fsize);
341 lyxerr << "No font matches request. Using 'fixed'." << endl;
342 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
346 XFontStruct * fs = 0;
348 fs = XLoadQueryFont(fl_get_display(), font.c_str());
351 if (font == "fixed") {
352 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
354 lyxerr << "Could not get font '" << font
355 << "'. Using 'fixed'." << endl;
356 fs = XLoadQueryFont(fl_get_display(), "fixed");
358 } else if (lyxerr.debugging(Debug::FONT)) {
359 // Tell user the font matching
365 // The rest of the attributes are not interesting
366 f.setEmph(LyXFont::INHERIT);
367 f.setUnderbar(LyXFont::INHERIT);
368 f.setNoun(LyXFont::INHERIT);
369 f.setColor(LColor::inherit);
370 lyxerr << "Font '" << f.stateText(0)
371 << "' matched by\n" << font << endl;
374 fontstruct[family][series][shape][size] = fs;
379 bool xfont_loader::available(LyXFont const & f)
381 if (!lyx_gui::use_gui)
384 if (!fontinfo[f.family()][f.series()][f.realShape()])
385 getFontinfo(f.family(), f.series(), f.realShape());
386 return fontinfo[f.family()][f.series()][f.realShape()]->exist();
389 } // namespace frontend