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"
34 using support::LibFileSearch;
35 using support::OnlyPath;
36 using support::Systemcall;
40 // The global fontloader
41 xfont_loader fontloader;
44 // Initialize font loader
45 xfont_loader::xfont_loader()
51 // Destroy font loader
52 xfont_loader::~xfont_loader()
58 // Update fonts after zoom, dpi, font names, or norm change
59 // For now, we just ditch all fonts we have. Later, we should
60 // reuse the ones that are already loaded.
61 void xfont_loader::update()
68 void xfont_loader::reset()
70 // Clear font infos, font structs and font metrics
71 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
72 for (int i2 = 0; i2 < 2; ++i2)
73 for (int i3 = 0; i3 < 4; ++i3) {
74 fontinfo[i1][i2][i3] = 0;
75 for (int i4 = 0; i4<10; ++i4) {
76 fontstruct[i1][i2][i3][i4] = 0;
83 void xfont_loader::unload()
86 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
87 for (int i2 = 0; i2 < 2; ++i2)
88 for (int i3 = 0; i3 < 4; ++i3) {
89 if (fontinfo[i1][i2][i3]) {
90 delete fontinfo[i1][i2][i3];
91 fontinfo[i1][i2][i3] = 0;
93 for (int i4 = 0; i4 < 10; ++i4) {
94 if (fontstruct[i1][i2][i3][i4]) {
95 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
96 fontstruct[i1][i2][i3][i4] = 0;
104 string const symbolPattern(LyXFont::FONT_FAMILY family)
107 case LyXFont::SYMBOL_FAMILY:
108 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
110 case LyXFont::CMR_FAMILY:
111 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
113 case LyXFont::CMSY_FAMILY:
114 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
116 case LyXFont::CMM_FAMILY:
117 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
119 case LyXFont::CMEX_FAMILY:
120 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
122 case LyXFont::MSA_FAMILY:
123 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
125 case LyXFont::MSB_FAMILY:
126 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
128 case LyXFont::EUFRAK_FAMILY:
129 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
131 case LyXFont::WASY_FAMILY:
132 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
139 string const fontName(string const & family, string const & foundry)
141 if (foundry.empty() || foundry == "Xft")
144 return "-"+foundry+"-"+family;
150 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
153 char ** p = XGetFontPath(fl_get_display(), &n);
154 if (std::find(p, p + n, dir) != p + n)
156 lyxerr[Debug::FONT] << "Adding " << dir
157 << " to the font path." << endl;
158 string const command = "xset fp+ " + dir;
160 if (!s.startscript(Systemcall::Wait, command))
162 lyxerr << "Unable to add " << dir << "to the font path."
171 /* Takes care of finding which font that can match the given request. Tries
172 different alternatives. */
173 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
174 LyXFont::FONT_SERIES series,
175 LyXFont::FONT_SHAPE shape)
177 // Do we have the font info already?
178 if (fontinfo[family][series][shape] != 0)
182 string pat = symbolPattern(family);
184 static bool first_time = true;
185 fontinfo[family][series][shape] = new FontInfo(pat);
186 if (family != LyXFont::SYMBOL_FAMILY &&
187 !fontinfo[family][series][shape]->exist() &&
191 delete fontinfo[family][series][shape];
192 fontinfo[family][series][shape] = new FontInfo(pat);
199 // Normal font. Let's search for an existing name that matches.
203 string norm = lyxrc.font_norm;
206 FontInfo * fi = new FontInfo;
207 fontinfo[family][series][shape] = fi;
209 for (int cfam = 0; cfam < 2; ++cfam) {
210 // Determine family name
212 case LyXFont::ROMAN_FAMILY:
214 case 0: ffamily = fontName(lyxrc.roman_font_name,
215 lyxrc.roman_font_foundry);
217 case 1: ffamily = "-*-times";
221 case LyXFont::SANS_FAMILY:
223 case 0: ffamily = fontName(lyxrc.sans_font_name,
224 lyxrc.sans_font_foundry);
226 case 1: ffamily = "-*-helvetica";
230 case LyXFont::TYPEWRITER_FAMILY:
232 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
233 lyxrc.typewriter_font_foundry);
235 case 1: ffamily = "-*-courier";
242 for (int cser = 0; cser < 4; ++cser) {
243 // Determine series name
245 case LyXFont::MEDIUM_SERIES:
247 case 0: fseries = "-medium"; break;
248 case 1: fseries = "-book"; break;
249 case 2: fseries = "-light";
253 case LyXFont::BOLD_SERIES:
255 case 0: fseries = "-bold"; break;
256 case 1: fseries = "-black"; break;
257 case 2: fseries = "-demi"; break;
258 case 3: fseries = "-demibold";
265 for (int csha = 0; csha < 2; ++csha) {
266 // Determine shape name
268 case LyXFont::UP_SHAPE:
269 case LyXFont::SMALLCAPS_SHAPE:
271 case 0: fshape = "-r";
275 case LyXFont::ITALIC_SHAPE:
277 case 0: fshape = "-i"; break;
278 case 1: fshape = "-o";
282 case LyXFont::SLANTED_SHAPE:
284 case 0: fshape = "-o"; break;
285 case 1: fshape = "-i";
292 fontname = ffamily + fseries + fshape +
293 "-normal-*-*-*-*-*-*-*-" + norm;
294 fi->setPattern(fontname);
304 // A dummy fontstruct used when there is no gui.
307 XFontStruct dummyXFontStruct;
308 bool dummyXFontStructisGood = false;
313 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
314 LyXFont::FONT_SERIES series,
315 LyXFont::FONT_SHAPE shape,
316 LyXFont::FONT_SIZE size)
318 if (!lyx_gui::use_gui) {
319 if (!dummyXFontStructisGood) {
320 // no character specific info
321 dummyXFontStruct.per_char = 0;
322 // unit ascent on character displays
323 dummyXFontStruct.ascent = 1;
324 // no descent on character displays
325 dummyXFontStruct.descent = 0;
326 dummyXFontStructisGood = true;
329 return &dummyXFontStruct;
332 getFontinfo(family, series, shape);
333 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
334 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
335 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
337 string font = fontinfo[family][series][shape]->getFontname(fsize);
340 lyxerr << "No font matches request. Using 'fixed'." << endl;
341 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
345 XFontStruct * fs = 0;
347 fs = XLoadQueryFont(fl_get_display(), font.c_str());
350 if (font == "fixed") {
351 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
353 lyxerr << "Could not get font '" << font
354 << "'. Using 'fixed'." << endl;
355 fs = XLoadQueryFont(fl_get_display(), "fixed");
357 } else if (lyxerr.debugging(Debug::FONT)) {
358 // Tell user the font matching
364 // The rest of the attributes are not interesting
365 f.setEmph(LyXFont::INHERIT);
366 f.setUnderbar(LyXFont::INHERIT);
367 f.setNoun(LyXFont::INHERIT);
368 f.setColor(LColor::inherit);
369 lyxerr << "Font '" << f.stateText(0)
370 << "' matched by\n" << font << endl;
373 fontstruct[family][series][shape][size] = fs;
378 bool xfont_loader::available(LyXFont const & f)
380 if (!lyx_gui::use_gui)
383 if (!fontinfo[f.family()][f.series()][f.realShape()])
384 getFontinfo(f.family(), f.series(), f.realShape());
385 return fontinfo[f.family()][f.series()][f.realShape()]->exist();
388 } // namespace frontend