1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1997 Asger Alstrup
9 * ====================================================== */
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"
26 #include FORMS_H_LOCATION
30 extern BufferView * current_view;
33 // The global fontloader
34 xfont_loader fontloader;
37 // Initialize font loader
38 xfont_loader::xfont_loader()
44 // Destroy font loader
45 xfont_loader::~xfont_loader()
51 // Update fonts after zoom, dpi, font names, or norm change
52 // For now, we just ditch all fonts we have. Later, we should
53 // reuse the ones that are already loaded.
54 void xfont_loader::update()
61 void xfont_loader::reset()
63 // Clear font infos, font structs and font metrics
64 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
65 for (int i2 = 0; i2 < 2; ++i2)
66 for (int i3 = 0; i3 < 4; ++i3) {
67 fontinfo[i1][i2][i3] = 0;
68 for (int i4 = 0; i4<10; ++i4) {
69 fontstruct[i1][i2][i3][i4] = 0;
76 void xfont_loader::unload()
79 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
80 for (int i2 = 0; i2 < 2; ++i2)
81 for (int i3 = 0; i3 < 4; ++i3) {
82 if (fontinfo[i1][i2][i3]) {
83 delete fontinfo[i1][i2][i3];
84 fontinfo[i1][i2][i3] = 0;
86 for (int i4 = 0; i4 < 10; ++i4) {
87 if (fontstruct[i1][i2][i3][i4]) {
88 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
89 fontstruct[i1][i2][i3][i4] = 0;
97 /* Takes care of finding which font that can match the given request. Tries
98 different alternatives. */
99 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
100 LyXFont::FONT_SERIES series,
101 LyXFont::FONT_SHAPE shape)
103 // Do we have the font info already?
104 if (fontinfo[family][series][shape] != 0)
110 case LyXFont::SYMBOL_FAMILY:
111 fontinfo[family][series][shape] =
112 new FontInfo("-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific");
115 case LyXFont::CMR_FAMILY:
116 fontinfo[family][series][shape] =
117 new FontInfo("-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*");
120 case LyXFont::CMSY_FAMILY:
121 fontinfo[family][series][shape] =
122 new FontInfo("-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*");
125 case LyXFont::CMM_FAMILY:
126 fontinfo[family][series][shape] =
127 new FontInfo("-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*");
130 case LyXFont::CMEX_FAMILY:
131 fontinfo[family][series][shape] =
132 new FontInfo("-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*");
135 case LyXFont::MSA_FAMILY:
136 fontinfo[family][series][shape] =
137 new FontInfo("-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*");
140 case LyXFont::MSB_FAMILY:
141 fontinfo[family][series][shape] =
142 new FontInfo("-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*");
145 case LyXFont::EUFRAK_FAMILY:
146 fontinfo[family][series][shape] =
147 new FontInfo("-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*");
150 case LyXFont::WASY_FAMILY:
151 fontinfo[family][series][shape] =
152 new FontInfo("-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*");
160 // Normal font. Let's search for an existing name that matches.
164 string norm = lyxrc.font_norm;
167 FontInfo * fi = new FontInfo;
168 fontinfo[family][series][shape] = fi;
170 for (int cfam = 0; cfam < 2; ++cfam) {
171 // Determine family name
173 case LyXFont::ROMAN_FAMILY:
175 case 0: ffamily = lyxrc.roman_font_name; break;
176 case 1: ffamily = "-*-times";
180 case LyXFont::SANS_FAMILY:
182 case 0: ffamily = lyxrc.sans_font_name; break;
183 case 1: ffamily = "-*-helvetica";
187 case LyXFont::TYPEWRITER_FAMILY:
189 case 0: ffamily = lyxrc.typewriter_font_name; break;
190 case 1: ffamily = "-*-courier";
197 for (int cser = 0; cser < 4; ++cser) {
198 // Determine series name
200 case LyXFont::MEDIUM_SERIES:
202 case 0: fseries = "-medium"; break;
203 case 1: fseries = "-book"; break;
204 case 2: fseries = "-light";
208 case LyXFont::BOLD_SERIES:
210 case 0: fseries = "-bold"; break;
211 case 1: fseries = "-black"; break;
212 case 2: fseries = "-demi"; break;
213 case 3: fseries = "-demibold";
220 for (int csha = 0; csha < 2; ++csha) {
221 // Determine shape name
223 case LyXFont::UP_SHAPE:
224 case LyXFont::SMALLCAPS_SHAPE:
226 case 0: fshape = "-r";
230 case LyXFont::ITALIC_SHAPE:
232 case 0: fshape = "-i"; break;
233 case 1: fshape = "-o";
237 case LyXFont::SLANTED_SHAPE:
239 case 0: fshape = "-o"; break;
240 case 1: fshape = "-i";
247 fontname = ffamily + fseries + fshape +
248 "-normal-*-*-*-*-*-*-*-" + norm;
249 fi->setPattern(fontname);
259 // A dummy fontstruct used when there is no gui.
262 XFontStruct dummyXFontStruct;
263 bool dummyXFontStructisGood = false;
268 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
269 LyXFont::FONT_SERIES series,
270 LyXFont::FONT_SHAPE shape,
271 LyXFont::FONT_SIZE size)
273 if (!lyxrc.use_gui) {
274 if (!dummyXFontStructisGood) {
275 // no character specific info
276 dummyXFontStruct.per_char = 0;
277 // unit ascent on character displays
278 dummyXFontStruct.ascent = 1;
279 // no descent on character displays
280 dummyXFontStruct.descent = 0;
281 dummyXFontStructisGood = true;
284 return &dummyXFontStruct;
287 getFontinfo(family, series, shape);
288 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
289 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
291 string font = fontinfo[family][series][shape]->getFontname(fsize);
294 lyxerr << "No font matches request. Using 'fixed'." << endl;
295 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
299 XFontStruct * fs = 0;
301 current_view->owner()->messagePush(_("Loading font into X-Server..."));
303 fs = XLoadQueryFont(fl_get_display(), font.c_str());
306 if (font == "fixed") {
307 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
309 lyxerr << "Could not get font. Using 'fixed'." << endl;
310 fs = XLoadQueryFont(fl_get_display(), "fixed");
312 } else if (lyxerr.debugging(Debug::FONT)) {
313 // Tell user the font matching
319 // The rest of the attributes are not interesting
320 f.setEmph(LyXFont::INHERIT);
321 f.setUnderbar(LyXFont::INHERIT);
322 f.setNoun(LyXFont::INHERIT);
323 f.setColor(LColor::inherit);
324 lyxerr << "Font '" << f.stateText(0)
325 << "' matched by\n" << font << endl;
328 current_view->owner()->messagePop();
330 fontstruct[family][series][shape][size] = fs;
335 bool xfont_loader::available(LyXFont const & f)
340 if (!fontinfo[f.family()][f.series()][f.realShape()])
341 getFontinfo(f.family(), f.series(), f.realShape());
342 return fontinfo[f.family()][f.series()][f.realShape()]
343 ->getFontname(f.size()).size();