3 * Read the file COPYING
5 * \author Asger Alstrup
7 * Full author contact details are available in file CREDITS
11 #include <cmath> // fabs()
14 #pragma implementation
17 #include "xfont_loader.h"
21 #include "lyxrc.h" // lyxrc.font_*
22 #include "BufferView.h"
23 #include "frontends/LyXView.h"
25 #include FORMS_H_LOCATION
29 // The global fontloader
30 xfont_loader fontloader;
33 // Initialize font loader
34 xfont_loader::xfont_loader()
40 // Destroy font loader
41 xfont_loader::~xfont_loader()
47 // Update fonts after zoom, dpi, font names, or norm change
48 // For now, we just ditch all fonts we have. Later, we should
49 // reuse the ones that are already loaded.
50 void xfont_loader::update()
57 void xfont_loader::reset()
59 // Clear font infos, font structs and font metrics
60 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
61 for (int i2 = 0; i2 < 2; ++i2)
62 for (int i3 = 0; i3 < 4; ++i3) {
63 fontinfo[i1][i2][i3] = 0;
64 for (int i4 = 0; i4<10; ++i4) {
65 fontstruct[i1][i2][i3][i4] = 0;
72 void xfont_loader::unload()
75 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
76 for (int i2 = 0; i2 < 2; ++i2)
77 for (int i3 = 0; i3 < 4; ++i3) {
78 if (fontinfo[i1][i2][i3]) {
79 delete fontinfo[i1][i2][i3];
80 fontinfo[i1][i2][i3] = 0;
82 for (int i4 = 0; i4 < 10; ++i4) {
83 if (fontstruct[i1][i2][i3][i4]) {
84 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
85 fontstruct[i1][i2][i3][i4] = 0;
93 /* Takes care of finding which font that can match the given request. Tries
94 different alternatives. */
95 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
96 LyXFont::FONT_SERIES series,
97 LyXFont::FONT_SHAPE shape)
99 // Do we have the font info already?
100 if (fontinfo[family][series][shape] != 0)
106 case LyXFont::SYMBOL_FAMILY:
107 fontinfo[family][series][shape] =
108 new FontInfo("-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific");
111 case LyXFont::CMR_FAMILY:
112 fontinfo[family][series][shape] =
113 new FontInfo("-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*");
116 case LyXFont::CMSY_FAMILY:
117 fontinfo[family][series][shape] =
118 new FontInfo("-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*");
121 case LyXFont::CMM_FAMILY:
122 fontinfo[family][series][shape] =
123 new FontInfo("-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*");
126 case LyXFont::CMEX_FAMILY:
127 fontinfo[family][series][shape] =
128 new FontInfo("-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*");
131 case LyXFont::MSA_FAMILY:
132 fontinfo[family][series][shape] =
133 new FontInfo("-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*");
136 case LyXFont::MSB_FAMILY:
137 fontinfo[family][series][shape] =
138 new FontInfo("-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*");
141 case LyXFont::EUFRAK_FAMILY:
142 fontinfo[family][series][shape] =
143 new FontInfo("-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*");
146 case LyXFont::WASY_FAMILY:
147 fontinfo[family][series][shape] =
148 new FontInfo("-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*");
156 // Normal font. Let's search for an existing name that matches.
160 string norm = lyxrc.font_norm;
163 FontInfo * fi = new FontInfo;
164 fontinfo[family][series][shape] = fi;
166 for (int cfam = 0; cfam < 2; ++cfam) {
167 // Determine family name
169 case LyXFont::ROMAN_FAMILY:
171 case 0: ffamily = lyxrc.roman_font_name; break;
172 case 1: ffamily = "-*-times";
176 case LyXFont::SANS_FAMILY:
178 case 0: ffamily = lyxrc.sans_font_name; break;
179 case 1: ffamily = "-*-helvetica";
183 case LyXFont::TYPEWRITER_FAMILY:
185 case 0: ffamily = lyxrc.typewriter_font_name; break;
186 case 1: ffamily = "-*-courier";
193 for (int cser = 0; cser < 4; ++cser) {
194 // Determine series name
196 case LyXFont::MEDIUM_SERIES:
198 case 0: fseries = "-medium"; break;
199 case 1: fseries = "-book"; break;
200 case 2: fseries = "-light";
204 case LyXFont::BOLD_SERIES:
206 case 0: fseries = "-bold"; break;
207 case 1: fseries = "-black"; break;
208 case 2: fseries = "-demi"; break;
209 case 3: fseries = "-demibold";
216 for (int csha = 0; csha < 2; ++csha) {
217 // Determine shape name
219 case LyXFont::UP_SHAPE:
220 case LyXFont::SMALLCAPS_SHAPE:
222 case 0: fshape = "-r";
226 case LyXFont::ITALIC_SHAPE:
228 case 0: fshape = "-i"; break;
229 case 1: fshape = "-o";
233 case LyXFont::SLANTED_SHAPE:
235 case 0: fshape = "-o"; break;
236 case 1: fshape = "-i";
243 fontname = ffamily + fseries + fshape +
244 "-normal-*-*-*-*-*-*-*-" + norm;
245 fi->setPattern(fontname);
255 // A dummy fontstruct used when there is no gui.
258 XFontStruct dummyXFontStruct;
259 bool dummyXFontStructisGood = false;
264 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
265 LyXFont::FONT_SERIES series,
266 LyXFont::FONT_SHAPE shape,
267 LyXFont::FONT_SIZE size)
269 if (!lyxrc.use_gui) {
270 if (!dummyXFontStructisGood) {
271 // no character specific info
272 dummyXFontStruct.per_char = 0;
273 // unit ascent on character displays
274 dummyXFontStruct.ascent = 1;
275 // no descent on character displays
276 dummyXFontStruct.descent = 0;
277 dummyXFontStructisGood = true;
280 return &dummyXFontStruct;
283 getFontinfo(family, series, shape);
284 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
285 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
287 string font = fontinfo[family][series][shape]->getFontname(fsize);
290 lyxerr << "No font matches request. Using 'fixed'." << endl;
291 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
295 XFontStruct * fs = 0;
297 fs = XLoadQueryFont(fl_get_display(), font.c_str());
300 if (font == "fixed") {
301 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
303 lyxerr << "Could not get font '" << font
304 << "'. Using 'fixed'." << endl;
305 fs = XLoadQueryFont(fl_get_display(), "fixed");
307 } else if (lyxerr.debugging(Debug::FONT)) {
308 // Tell user the font matching
314 // The rest of the attributes are not interesting
315 f.setEmph(LyXFont::INHERIT);
316 f.setUnderbar(LyXFont::INHERIT);
317 f.setNoun(LyXFont::INHERIT);
318 f.setColor(LColor::inherit);
319 lyxerr << "Font '" << f.stateText(0)
320 << "' matched by\n" << font << endl;
323 fontstruct[family][series][shape][size] = fs;
328 bool xfont_loader::available(LyXFont const & f)
333 if (!fontinfo[f.family()][f.series()][f.realShape()])
334 getFontinfo(f.family(), f.series(), f.realShape());
335 return fontinfo[f.family()][f.series()][f.realShape()]
336 ->getFontname(f.size()).size();