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.
12 #include <cmath> // fabs()
15 #include "xfont_loader.h"
19 #include "lyxrc.h" // lyxrc.font_*
20 #include "BufferView.h"
21 #include "frontends/LyXView.h"
22 #include "frontends/lyx_gui.h"
23 #include "support/systemcall.h"
24 #include "support/filetools.h"
26 #include "lyx_forms.h"
28 using namespace lyx::support;
32 // The global fontloader
33 xfont_loader fontloader;
36 // Initialize font loader
37 xfont_loader::xfont_loader()
43 // Destroy font loader
44 xfont_loader::~xfont_loader()
50 // Update fonts after zoom, dpi, font names, or norm change
51 // For now, we just ditch all fonts we have. Later, we should
52 // reuse the ones that are already loaded.
53 void xfont_loader::update()
60 void xfont_loader::reset()
62 // Clear font infos, font structs and font metrics
63 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
64 for (int i2 = 0; i2 < 2; ++i2)
65 for (int i3 = 0; i3 < 4; ++i3) {
66 fontinfo[i1][i2][i3] = 0;
67 for (int i4 = 0; i4<10; ++i4) {
68 fontstruct[i1][i2][i3][i4] = 0;
75 void xfont_loader::unload()
78 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
79 for (int i2 = 0; i2 < 2; ++i2)
80 for (int i3 = 0; i3 < 4; ++i3) {
81 if (fontinfo[i1][i2][i3]) {
82 delete fontinfo[i1][i2][i3];
83 fontinfo[i1][i2][i3] = 0;
85 for (int i4 = 0; i4 < 10; ++i4) {
86 if (fontstruct[i1][i2][i3][i4]) {
87 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
88 fontstruct[i1][i2][i3][i4] = 0;
96 string const symbolPattern(LyXFont::FONT_FAMILY family)
99 case LyXFont::SYMBOL_FAMILY:
100 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
102 case LyXFont::CMR_FAMILY:
103 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
105 case LyXFont::CMSY_FAMILY:
106 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
108 case LyXFont::CMM_FAMILY:
109 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
111 case LyXFont::CMEX_FAMILY:
112 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
114 case LyXFont::MSA_FAMILY:
115 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
117 case LyXFont::MSB_FAMILY:
118 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
120 case LyXFont::EUFRAK_FAMILY:
121 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
123 case LyXFont::WASY_FAMILY:
124 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
131 string const fontName(string const & family, string const & foundry)
133 if (foundry.empty() || foundry == "Xft")
136 return "-"+foundry+"-"+family;
142 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
145 char ** p = XGetFontPath(fl_get_display(), &n);
146 if (std::find(p, p + n, dir) != p + n)
148 lyxerr[Debug::FONT] << "Adding " << dir
149 << " to the font path." << endl;
150 string const command = "xset fp+ " + dir;
152 if (!s.startscript(Systemcall::Wait, command))
154 lyxerr << "Unable to add " << dir << "to the font path."
163 /* Takes care of finding which font that can match the given request. Tries
164 different alternatives. */
165 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
166 LyXFont::FONT_SERIES series,
167 LyXFont::FONT_SHAPE shape)
169 // Do we have the font info already?
170 if (fontinfo[family][series][shape] != 0)
174 string pat = symbolPattern(family);
176 static bool first_time = true;
177 fontinfo[family][series][shape] = new FontInfo(pat);
178 if (family != LyXFont::SYMBOL_FAMILY &&
179 !fontinfo[family][series][shape]->exist() &&
183 delete fontinfo[family][series][shape];
184 fontinfo[family][series][shape] = new FontInfo(pat);
191 // Normal font. Let's search for an existing name that matches.
195 string norm = lyxrc.font_norm;
198 FontInfo * fi = new FontInfo;
199 fontinfo[family][series][shape] = fi;
201 for (int cfam = 0; cfam < 2; ++cfam) {
202 // Determine family name
204 case LyXFont::ROMAN_FAMILY:
206 case 0: ffamily = fontName(lyxrc.roman_font_name,
207 lyxrc.roman_font_foundry);
209 case 1: ffamily = "-*-times";
213 case LyXFont::SANS_FAMILY:
215 case 0: ffamily = fontName(lyxrc.sans_font_name,
216 lyxrc.sans_font_foundry);
218 case 1: ffamily = "-*-helvetica";
222 case LyXFont::TYPEWRITER_FAMILY:
224 case 0: ffamily = fontName(lyxrc.typewriter_font_name,
225 lyxrc.typewriter_font_foundry);
227 case 1: ffamily = "-*-courier";
234 for (int cser = 0; cser < 4; ++cser) {
235 // Determine series name
237 case LyXFont::MEDIUM_SERIES:
239 case 0: fseries = "-medium"; break;
240 case 1: fseries = "-book"; break;
241 case 2: fseries = "-light";
245 case LyXFont::BOLD_SERIES:
247 case 0: fseries = "-bold"; break;
248 case 1: fseries = "-black"; break;
249 case 2: fseries = "-demi"; break;
250 case 3: fseries = "-demibold";
257 for (int csha = 0; csha < 2; ++csha) {
258 // Determine shape name
260 case LyXFont::UP_SHAPE:
261 case LyXFont::SMALLCAPS_SHAPE:
263 case 0: fshape = "-r";
267 case LyXFont::ITALIC_SHAPE:
269 case 0: fshape = "-i"; break;
270 case 1: fshape = "-o";
274 case LyXFont::SLANTED_SHAPE:
276 case 0: fshape = "-o"; break;
277 case 1: fshape = "-i";
284 fontname = ffamily + fseries + fshape +
285 "-normal-*-*-*-*-*-*-*-" + norm;
286 fi->setPattern(fontname);
296 // A dummy fontstruct used when there is no gui.
299 XFontStruct dummyXFontStruct;
300 bool dummyXFontStructisGood = false;
305 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
306 LyXFont::FONT_SERIES series,
307 LyXFont::FONT_SHAPE shape,
308 LyXFont::FONT_SIZE size)
310 if (!lyx_gui::use_gui) {
311 if (!dummyXFontStructisGood) {
312 // no character specific info
313 dummyXFontStruct.per_char = 0;
314 // unit ascent on character displays
315 dummyXFontStruct.ascent = 1;
316 // no descent on character displays
317 dummyXFontStruct.descent = 0;
318 dummyXFontStructisGood = true;
321 return &dummyXFontStruct;
324 getFontinfo(family, series, shape);
325 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
326 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
327 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
329 string font = fontinfo[family][series][shape]->getFontname(fsize);
332 lyxerr << "No font matches request. Using 'fixed'." << endl;
333 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
337 XFontStruct * fs = 0;
339 fs = XLoadQueryFont(fl_get_display(), font.c_str());
342 if (font == "fixed") {
343 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
345 lyxerr << "Could not get font '" << font
346 << "'. Using 'fixed'." << endl;
347 fs = XLoadQueryFont(fl_get_display(), "fixed");
349 } else if (lyxerr.debugging(Debug::FONT)) {
350 // Tell user the font matching
356 // The rest of the attributes are not interesting
357 f.setEmph(LyXFont::INHERIT);
358 f.setUnderbar(LyXFont::INHERIT);
359 f.setNoun(LyXFont::INHERIT);
360 f.setColor(LColor::inherit);
361 lyxerr << "Font '" << f.stateText(0)
362 << "' matched by\n" << font << endl;
365 fontstruct[family][series][shape][size] = fs;
370 bool xfont_loader::available(LyXFont const & f)
372 if (!lyx_gui::use_gui)
375 if (!fontinfo[f.family()][f.series()][f.realShape()])
376 getFontinfo(f.family(), f.series(), f.realShape());
377 return fontinfo[f.family()][f.series()][f.realShape()]->exist();