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 // The global fontloader
31 xfont_loader fontloader;
34 // Initialize font loader
35 xfont_loader::xfont_loader()
41 // Destroy font loader
42 xfont_loader::~xfont_loader()
48 // Update fonts after zoom, dpi, font names, or norm change
49 // For now, we just ditch all fonts we have. Later, we should
50 // reuse the ones that are already loaded.
51 void xfont_loader::update()
58 void xfont_loader::reset()
60 // Clear font infos, font structs and font metrics
61 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
62 for (int i2 = 0; i2 < 2; ++i2)
63 for (int i3 = 0; i3 < 4; ++i3) {
64 fontinfo[i1][i2][i3] = 0;
65 for (int i4 = 0; i4<10; ++i4) {
66 fontstruct[i1][i2][i3][i4] = 0;
73 void xfont_loader::unload()
76 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
77 for (int i2 = 0; i2 < 2; ++i2)
78 for (int i3 = 0; i3 < 4; ++i3) {
79 if (fontinfo[i1][i2][i3]) {
80 delete fontinfo[i1][i2][i3];
81 fontinfo[i1][i2][i3] = 0;
83 for (int i4 = 0; i4 < 10; ++i4) {
84 if (fontstruct[i1][i2][i3][i4]) {
85 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
86 fontstruct[i1][i2][i3][i4] = 0;
94 /* Takes care of finding which font that can match the given request. Tries
95 different alternatives. */
96 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
97 LyXFont::FONT_SERIES series,
98 LyXFont::FONT_SHAPE shape)
100 // Do we have the font info already?
101 if (fontinfo[family][series][shape] != 0)
107 case LyXFont::SYMBOL_FAMILY:
108 fontinfo[family][series][shape] =
109 new FontInfo("-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific");
112 case LyXFont::CMR_FAMILY:
113 fontinfo[family][series][shape] =
114 new FontInfo("-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*");
117 case LyXFont::CMSY_FAMILY:
118 fontinfo[family][series][shape] =
119 new FontInfo("-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*");
122 case LyXFont::CMM_FAMILY:
123 fontinfo[family][series][shape] =
124 new FontInfo("-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*");
127 case LyXFont::CMEX_FAMILY:
128 fontinfo[family][series][shape] =
129 new FontInfo("-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*");
132 case LyXFont::MSA_FAMILY:
133 fontinfo[family][series][shape] =
134 new FontInfo("-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*");
137 case LyXFont::MSB_FAMILY:
138 fontinfo[family][series][shape] =
139 new FontInfo("-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*");
142 case LyXFont::EUFRAK_FAMILY:
143 fontinfo[family][series][shape] =
144 new FontInfo("-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*");
147 case LyXFont::WASY_FAMILY:
148 fontinfo[family][series][shape] =
149 new FontInfo("-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*");
157 // Normal font. Let's search for an existing name that matches.
161 string norm = lyxrc.font_norm;
164 FontInfo * fi = new FontInfo;
165 fontinfo[family][series][shape] = fi;
167 for (int cfam = 0; cfam < 2; ++cfam) {
168 // Determine family name
170 case LyXFont::ROMAN_FAMILY:
172 case 0: ffamily = lyxrc.roman_font_name; break;
173 case 1: ffamily = "-*-times";
177 case LyXFont::SANS_FAMILY:
179 case 0: ffamily = lyxrc.sans_font_name; break;
180 case 1: ffamily = "-*-helvetica";
184 case LyXFont::TYPEWRITER_FAMILY:
186 case 0: ffamily = lyxrc.typewriter_font_name; break;
187 case 1: ffamily = "-*-courier";
194 for (int cser = 0; cser < 4; ++cser) {
195 // Determine series name
197 case LyXFont::MEDIUM_SERIES:
199 case 0: fseries = "-medium"; break;
200 case 1: fseries = "-book"; break;
201 case 2: fseries = "-light";
205 case LyXFont::BOLD_SERIES:
207 case 0: fseries = "-bold"; break;
208 case 1: fseries = "-black"; break;
209 case 2: fseries = "-demi"; break;
210 case 3: fseries = "-demibold";
217 for (int csha = 0; csha < 2; ++csha) {
218 // Determine shape name
220 case LyXFont::UP_SHAPE:
221 case LyXFont::SMALLCAPS_SHAPE:
223 case 0: fshape = "-r";
227 case LyXFont::ITALIC_SHAPE:
229 case 0: fshape = "-i"; break;
230 case 1: fshape = "-o";
234 case LyXFont::SLANTED_SHAPE:
236 case 0: fshape = "-o"; break;
237 case 1: fshape = "-i";
244 fontname = ffamily + fseries + fshape +
245 "-normal-*-*-*-*-*-*-*-" + norm;
246 fi->setPattern(fontname);
256 // A dummy fontstruct used when there is no gui.
259 XFontStruct dummyXFontStruct;
260 bool dummyXFontStructisGood = false;
265 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
266 LyXFont::FONT_SERIES series,
267 LyXFont::FONT_SHAPE shape,
268 LyXFont::FONT_SIZE size)
270 if (!lyxrc.use_gui) {
271 if (!dummyXFontStructisGood) {
272 // no character specific info
273 dummyXFontStruct.per_char = 0;
274 // unit ascent on character displays
275 dummyXFontStruct.ascent = 1;
276 // no descent on character displays
277 dummyXFontStruct.descent = 0;
278 dummyXFontStructisGood = true;
281 return &dummyXFontStruct;
284 getFontinfo(family, series, shape);
285 int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
286 (lyxrc.zoom/100.0)) / 72.27 + 0.5);
288 string font = fontinfo[family][series][shape]->getFontname(fsize);
291 lyxerr << "No font matches request. Using 'fixed'." << endl;
292 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
296 XFontStruct * fs = 0;
298 fs = XLoadQueryFont(fl_get_display(), font.c_str());
301 if (font == "fixed") {
302 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
304 lyxerr << "Could not get font. 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();