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/lstrings.h"
24 #include "support/systemcall.h"
26 #include "lyx_forms.h"
35 using support::LibFileSearch;
36 using support::strToDbl;
37 using support::OnlyPath;
38 using support::Systemcall;
42 // The global fontloader
43 xfont_loader fontloader;
46 // Initialize font loader
47 xfont_loader::xfont_loader()
53 // Destroy font loader
54 xfont_loader::~xfont_loader()
60 // Update fonts after zoom, dpi, font names, or norm change
61 // For now, we just ditch all fonts we have. Later, we should
62 // reuse the ones that are already loaded.
63 void xfont_loader::update()
70 void xfont_loader::reset()
72 // Clear font infos, font structs and font metrics
73 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
74 for (int i2 = 0; i2 < 2; ++i2)
75 for (int i3 = 0; i3 < 4; ++i3) {
76 fontinfo[i1][i2][i3] = 0;
77 for (int i4 = 0; i4<10; ++i4) {
78 fontstruct[i1][i2][i3][i4] = 0;
85 void xfont_loader::unload()
88 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
89 for (int i2 = 0; i2 < 2; ++i2)
90 for (int i3 = 0; i3 < 4; ++i3) {
91 if (fontinfo[i1][i2][i3]) {
92 delete fontinfo[i1][i2][i3];
93 fontinfo[i1][i2][i3] = 0;
95 for (int i4 = 0; i4 < 10; ++i4) {
96 if (fontstruct[i1][i2][i3][i4]) {
97 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
98 fontstruct[i1][i2][i3][i4] = 0;
106 string const symbolPattern(LyXFont::FONT_FAMILY family)
109 case LyXFont::SYMBOL_FAMILY:
110 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
112 case LyXFont::CMR_FAMILY:
113 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
115 case LyXFont::CMSY_FAMILY:
116 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
118 case LyXFont::CMM_FAMILY:
119 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
121 case LyXFont::CMEX_FAMILY:
122 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
124 case LyXFont::MSA_FAMILY:
125 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
127 case LyXFont::MSB_FAMILY:
128 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
130 case LyXFont::EUFRAK_FAMILY:
131 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
133 case LyXFont::WASY_FAMILY:
134 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
141 string const fontName(string const & family, string const & foundry)
143 if (foundry.empty() || foundry == "Xft")
146 return "-"+foundry+"-"+family;
152 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
155 char ** p = XGetFontPath(fl_get_display(), &n);
156 if (std::find(p, p + n, dir) != p + n)
158 lyxerr[Debug::FONT] << "Adding " << dir
159 << " to the font path." << endl;
160 string const command = "xset fp+ " + dir;
162 if (!s.startscript(Systemcall::Wait, command))
164 lyxerr << "Unable to add " << dir << "to the font path."
173 /* Takes care of finding which font that can match the given request. Tries
174 different alternatives. */
175 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
176 LyXFont::FONT_SERIES series,
177 LyXFont::FONT_SHAPE shape)
179 // Do we have the font info already?
180 if (fontinfo[family][series][shape] != 0)
184 string pat = symbolPattern(family);
186 static bool first_time = true;
187 fontinfo[family][series][shape] = new FontInfo(pat);
188 if (family != LyXFont::SYMBOL_FAMILY &&
189 !fontinfo[family][series][shape]->exist() &&
193 delete fontinfo[family][series][shape];
194 fontinfo[family][series][shape] = new FontInfo(pat);
201 // Normal font. Let's search for an existing name that matches.
205 string norm = lyxrc.font_norm;
208 FontInfo * fi = new FontInfo;
209 fontinfo[family][series][shape] = fi;
211 for (int cfam = 0; cfam < 2; ++cfam) {
212 // Determine family name
214 case LyXFont::ROMAN_FAMILY:
216 case 0: ffamily = fontName(lyxrc.roman_font_name,
217 lyxrc.roman_font_foundry);
219 case 1: ffamily = "-*-times";
223 case LyXFont::SANS_FAMILY:
225 case 0: ffamily = fontName(lyxrc.sans_font_name,
226 lyxrc.sans_font_foundry);
228 case 1: ffamily = "-*-helvetica";
232 case LyXFont::TYPEWRITER_FAMILY:
234 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
235 lyxrc.typewriter_font_foundry);
237 case 1: ffamily = "-*-courier";
244 for (int cser = 0; cser < 4; ++cser) {
245 // Determine series name
247 case LyXFont::MEDIUM_SERIES:
249 case 0: fseries = "-medium"; break;
250 case 1: fseries = "-book"; break;
251 case 2: fseries = "-light";
255 case LyXFont::BOLD_SERIES:
257 case 0: fseries = "-bold"; break;
258 case 1: fseries = "-black"; break;
259 case 2: fseries = "-demi"; break;
260 case 3: fseries = "-demibold";
267 for (int csha = 0; csha < 2; ++csha) {
268 // Determine shape name
270 case LyXFont::UP_SHAPE:
271 case LyXFont::SMALLCAPS_SHAPE:
273 case 0: fshape = "-r";
277 case LyXFont::ITALIC_SHAPE:
279 case 0: fshape = "-i"; break;
280 case 1: fshape = "-o";
284 case LyXFont::SLANTED_SHAPE:
286 case 0: fshape = "-o"; break;
287 case 1: fshape = "-i";
294 fontname = ffamily + fseries + fshape +
295 "-normal-*-*-*-*-*-*-*-" + norm;
296 fi->setPattern(fontname);
306 // A dummy fontstruct used when there is no gui.
309 XFontStruct dummyXFontStruct;
310 bool dummyXFontStructisGood = false;
315 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
316 LyXFont::FONT_SERIES series,
317 LyXFont::FONT_SHAPE shape,
318 LyXFont::FONT_SIZE size)
320 if (!lyx_gui::use_gui) {
321 if (!dummyXFontStructisGood) {
322 // no character specific info
323 dummyXFontStruct.per_char = 0;
324 // unit ascent on character displays
325 dummyXFontStruct.ascent = 1;
326 // no descent on character displays
327 dummyXFontStruct.descent = 0;
328 dummyXFontStructisGood = true;
331 return &dummyXFontStruct;
334 getFontinfo(family, series, shape);
335 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
336 int fsize = int((strToDbl(lyxrc.font_sizes[size]) * lyxrc.dpi *
337 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
339 string font = fontinfo[family][series][shape]->getFontname(fsize);
342 lyxerr << "No font matches request. Using 'fixed'." << endl;
343 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
347 XFontStruct * fs = 0;
349 fs = XLoadQueryFont(fl_get_display(), font.c_str());
352 if (font == "fixed") {
353 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
355 lyxerr << "Could not get font '" << font
356 << "'. Using 'fixed'." << endl;
357 fs = XLoadQueryFont(fl_get_display(), "fixed");
359 } else if (lyxerr.debugging(Debug::FONT)) {
360 // Tell user the font matching
366 // The rest of the attributes are not interesting
367 f.setEmph(LyXFont::INHERIT);
368 f.setUnderbar(LyXFont::INHERIT);
369 f.setNoun(LyXFont::INHERIT);
370 f.setColor(LColor::inherit);
371 lyxerr << "Font '" << f.stateText(0)
372 << "' matched by\n" << font << endl;
375 fontstruct[family][series][shape][size] = fs;
380 bool xfont_loader::available(LyXFont const & f)
382 if (!lyx_gui::use_gui)
385 if (!fontinfo[f.family()][f.series()][f.realShape()])
386 getFontinfo(f.family(), f.series(), f.realShape());
387 return fontinfo[f.family()][f.series()][f.realShape()]->exist();
390 } // namespace frontend