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 #pragma implementation
18 #include "xfont_loader.h"
22 #include "lyxrc.h" // lyxrc.font_*
23 #include "BufferView.h"
24 #include "frontends/LyXView.h"
25 #include "support/systemcall.h"
26 #include "support/filetools.h"
28 #include FORMS_H_LOCATION
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;
95 string const symbolPattern(LyXFont::FONT_FAMILY family)
98 case LyXFont::SYMBOL_FAMILY:
99 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
101 case LyXFont::CMR_FAMILY:
102 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
104 case LyXFont::CMSY_FAMILY:
105 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
107 case LyXFont::CMM_FAMILY:
108 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
110 case LyXFont::CMEX_FAMILY:
111 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
113 case LyXFont::MSA_FAMILY:
114 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
116 case LyXFont::MSB_FAMILY:
117 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
119 case LyXFont::EUFRAK_FAMILY:
120 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
122 case LyXFont::WASY_FAMILY:
123 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
133 /* Takes care of finding which font that can match the given request. Tries
134 different alternatives. */
135 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
136 LyXFont::FONT_SERIES series,
137 LyXFont::FONT_SHAPE shape)
139 // Do we have the font info already?
140 if (fontinfo[family][series][shape] != 0)
144 string pat = symbolPattern(family);
146 static bool first_time = true;
147 fontinfo[family][series][shape] = new FontInfo(pat);
148 if (family != LyXFont::SYMBOL_FAMILY &&
149 !fontinfo[family][series][shape]->exist() &&
153 OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
156 char ** p = XGetFontPath(fl_get_display(), &n);
157 if (std::find(p, p+n, dir) != p+n)
159 lyxerr << "Adding " << dir << " to the font path.\n";
160 string const command = "xset fp+ " + dir;
162 if (!s.startscript(Systemcall::Wait, command)) {
163 delete fontinfo[family][series][shape];
164 fontinfo[family][series][shape] = new FontInfo(pat);
166 lyxerr << "Unable to add font path.\n";
173 // Normal font. Let's search for an existing name that matches.
177 string norm = lyxrc.font_norm;
180 FontInfo * fi = new FontInfo;
181 fontinfo[family][series][shape] = fi;
183 for (int cfam = 0; cfam < 2; ++cfam) {
184 // Determine family name
186 case LyXFont::ROMAN_FAMILY:
188 case 0: ffamily = lyxrc.roman_font_name; break;
189 case 1: ffamily = "-*-times";
193 case LyXFont::SANS_FAMILY:
195 case 0: ffamily = lyxrc.sans_font_name; break;
196 case 1: ffamily = "-*-helvetica";
200 case LyXFont::TYPEWRITER_FAMILY:
202 case 0: ffamily = lyxrc.typewriter_font_name; break;
203 case 1: ffamily = "-*-courier";
210 for (int cser = 0; cser < 4; ++cser) {
211 // Determine series name
213 case LyXFont::MEDIUM_SERIES:
215 case 0: fseries = "-medium"; break;
216 case 1: fseries = "-book"; break;
217 case 2: fseries = "-light";
221 case LyXFont::BOLD_SERIES:
223 case 0: fseries = "-bold"; break;
224 case 1: fseries = "-black"; break;
225 case 2: fseries = "-demi"; break;
226 case 3: fseries = "-demibold";
233 for (int csha = 0; csha < 2; ++csha) {
234 // Determine shape name
236 case LyXFont::UP_SHAPE:
237 case LyXFont::SMALLCAPS_SHAPE:
239 case 0: fshape = "-r";
243 case LyXFont::ITALIC_SHAPE:
245 case 0: fshape = "-i"; break;
246 case 1: fshape = "-o";
250 case LyXFont::SLANTED_SHAPE:
252 case 0: fshape = "-o"; break;
253 case 1: fshape = "-i";
260 fontname = ffamily + fseries + fshape +
261 "-normal-*-*-*-*-*-*-*-" + norm;
262 fi->setPattern(fontname);
272 // A dummy fontstruct used when there is no gui.
275 XFontStruct dummyXFontStruct;
276 bool dummyXFontStructisGood = false;
281 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
282 LyXFont::FONT_SERIES series,
283 LyXFont::FONT_SHAPE shape,
284 LyXFont::FONT_SIZE size)
286 if (!lyxrc.use_gui) {
287 if (!dummyXFontStructisGood) {
288 // no character specific info
289 dummyXFontStruct.per_char = 0;
290 // unit ascent on character displays
291 dummyXFontStruct.ascent = 1;
292 // no descent on character displays
293 dummyXFontStruct.descent = 0;
294 dummyXFontStructisGood = true;
297 return &dummyXFontStruct;
300 getFontinfo(family, series, shape);
301 // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
302 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
303 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
305 string font = fontinfo[family][series][shape]->getFontname(fsize);
308 lyxerr << "No font matches request. Using 'fixed'." << endl;
309 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
313 XFontStruct * fs = 0;
315 fs = XLoadQueryFont(fl_get_display(), font.c_str());
318 if (font == "fixed") {
319 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
321 lyxerr << "Could not get font '" << font
322 << "'. Using 'fixed'." << endl;
323 fs = XLoadQueryFont(fl_get_display(), "fixed");
325 } else if (lyxerr.debugging(Debug::FONT)) {
326 // Tell user the font matching
332 // The rest of the attributes are not interesting
333 f.setEmph(LyXFont::INHERIT);
334 f.setUnderbar(LyXFont::INHERIT);
335 f.setNoun(LyXFont::INHERIT);
336 f.setColor(LColor::inherit);
337 lyxerr << "Font '" << f.stateText(0)
338 << "' matched by\n" << font << endl;
341 fontstruct[family][series][shape][size] = fs;
346 bool xfont_loader::available(LyXFont const & f)
351 if (!fontinfo[f.family()][f.series()][f.realShape()])
352 getFontinfo(f.family(), f.series(), f.realShape());
353 return fontinfo[f.family()][f.series()][f.realShape()]->exist();