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 /* Takes care of finding which font that can match the given request. Tries
152 different alternatives. */
153 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
154 LyXFont::FONT_SERIES series,
155 LyXFont::FONT_SHAPE shape)
157 // Do we have the font info already?
158 if (fontinfo[family][series][shape] != 0)
162 string pat = symbolPattern(family);
164 fontinfo[family][series][shape] = new FontInfo(pat);
169 // Normal font. Let's search for an existing name that matches.
173 string norm = lyxrc.font_norm;
176 FontInfo * fi = new FontInfo;
177 fontinfo[family][series][shape] = fi;
179 for (int cfam = 0; cfam < 2; ++cfam) {
180 // Determine family name
182 case LyXFont::ROMAN_FAMILY:
184 case 0: ffamily = fontName(lyxrc.roman_font_name,
185 lyxrc.roman_font_foundry);
187 case 1: ffamily = "-*-times";
191 case LyXFont::SANS_FAMILY:
193 case 0: ffamily = fontName(lyxrc.sans_font_name,
194 lyxrc.sans_font_foundry);
196 case 1: ffamily = "-*-helvetica";
200 case LyXFont::TYPEWRITER_FAMILY:
202 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
203 lyxrc.typewriter_font_foundry);
205 case 1: ffamily = "-*-courier";
212 for (int cser = 0; cser < 4; ++cser) {
213 // Determine series name
215 case LyXFont::MEDIUM_SERIES:
217 case 0: fseries = "-medium"; break;
218 case 1: fseries = "-book"; break;
219 case 2: fseries = "-light";
223 case LyXFont::BOLD_SERIES:
225 case 0: fseries = "-bold"; break;
226 case 1: fseries = "-black"; break;
227 case 2: fseries = "-demi"; break;
228 case 3: fseries = "-demibold";
235 for (int csha = 0; csha < 2; ++csha) {
236 // Determine shape name
238 case LyXFont::UP_SHAPE:
239 case LyXFont::SMALLCAPS_SHAPE:
241 case 0: fshape = "-r";
245 case LyXFont::ITALIC_SHAPE:
247 case 0: fshape = "-i"; break;
248 case 1: fshape = "-o";
252 case LyXFont::SLANTED_SHAPE:
254 case 0: fshape = "-o"; break;
255 case 1: fshape = "-i";
262 fontname = ffamily + fseries + fshape +
263 "-normal-*-*-*-*-*-*-*-" + norm;
264 fi->setPattern(fontname);
274 // A dummy fontstruct used when there is no gui.
277 XFontStruct dummyXFontStruct;
278 bool dummyXFontStructisGood = false;
283 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
284 LyXFont::FONT_SERIES series,
285 LyXFont::FONT_SHAPE shape,
286 LyXFont::FONT_SIZE size)
288 if (!lyx_gui::use_gui) {
289 if (!dummyXFontStructisGood) {
290 // no character specific info
291 dummyXFontStruct.per_char = 0;
292 // unit ascent on character displays
293 dummyXFontStruct.ascent = 1;
294 // no descent on character displays
295 dummyXFontStruct.descent = 0;
296 dummyXFontStructisGood = true;
299 return &dummyXFontStruct;
302 getFontinfo(family, series, shape);
304 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
305 int fsize = int((convert<double>(lyxrc.font_sizes[size]) * lyxrc.dpi *
306 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
308 string font = fontinfo[family][series][shape]->getFontname(fsize);
311 lyxerr << "No font matches request. Using 'fixed'." << endl;
312 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
316 XFontStruct * fs = 0;
318 fs = XLoadQueryFont(fl_get_display(), font.c_str());
321 if (font == "fixed") {
322 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
324 lyxerr << "Could not get font '" << font
325 << "'. Using 'fixed'." << endl;
326 fs = XLoadQueryFont(fl_get_display(), "fixed");
328 } else if (lyxerr.debugging(Debug::FONT)) {
329 // Tell user the font matching
335 // The rest of the attributes are not interesting
336 f.setEmph(LyXFont::INHERIT);
337 f.setUnderbar(LyXFont::INHERIT);
338 f.setNoun(LyXFont::INHERIT);
339 f.setColor(LColor::inherit);
340 lyxerr << "Font '" << f.stateText(0)
341 << "' matched by\n" << font << endl;
344 fontstruct[family][series][shape][size] = fs;
349 bool xfont_loader::available(LyXFont const & f)
351 if (!lyx_gui::use_gui)
354 if (!fontinfo[f.family()][f.series()][f.realShape()])
355 getFontinfo(f.family(), f.series(), f.realShape());
356 return fontinfo[f.family()][f.series()][f.realShape()]->exist();
359 } // namespace frontend