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 "FontLoader.h"
18 #include "FontLoader.h"
22 #include "lyxrc.h" // lyxrc.font_*
23 #include "BufferView.h"
25 #include "frontends/GUIRunTime.h"
29 extern BufferView * current_view;
32 // The global fontloader
33 FontLoader fontloader;
36 // Initialize font loader
37 FontLoader::FontLoader()
43 // Destroy font loader
44 FontLoader::~FontLoader()
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 FontLoader::update()
60 void FontLoader::reset()
62 // Clear font infos, font structs and font metrics
63 for (int i1 = 0; i1 < 9; ++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 FontLoader::unload()
78 for (int i1 = 0; i1 < 9; ++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(GUIRunTime::x11Display(), fontstruct[i1][i2][i3][i4]);
88 fontstruct[i1][i2][i3][i4] = 0;
96 /* Takes care of finding which font that can match the given request. Tries
97 different alternatives. */
98 void FontLoader::getFontinfo(LyXFont::FONT_FAMILY family,
99 LyXFont::FONT_SERIES series,
100 LyXFont::FONT_SHAPE shape)
102 // Do we have the font info already?
103 if (fontinfo[family][series][shape] != 0)
109 case LyXFont::SYMBOL_FAMILY:
110 fontinfo[family][series][shape] =
111 new FontInfo("-*-symbol-*-*-*-*-*-*-*-*-*-*-*-*");
114 case LyXFont::CMSY_FAMILY:
115 fontinfo[family][series][shape] =
116 new FontInfo("-*-cmsy-*-*-*-*-*-*-*-*-*-*-*-*");
119 case LyXFont::CMM_FAMILY:
120 fontinfo[family][series][shape] =
121 new FontInfo("-*-cmmi-medium-*-*-*-*-*-*-*-*-*-*-*");
124 case LyXFont::CMEX_FAMILY:
125 fontinfo[family][series][shape] =
126 new FontInfo("-*-cmex-*-*-*-*-*-*-*-*-*-*-*-*");
129 case LyXFont::MSA_FAMILY:
130 fontinfo[family][series][shape] =
131 new FontInfo("-*-msam-*-*-*-*-*-*-*-*-*-*-*-*");
134 case LyXFont::MSB_FAMILY:
135 fontinfo[family][series][shape] =
136 new FontInfo("-*-msbm-*-*-*-*-*-*-*-*-*-*-*-*");
144 // Normal font. Let's search for an existing name that matches.
148 string norm = lyxrc.font_norm;
151 FontInfo * fi = new FontInfo();
152 fontinfo[family][series][shape] = fi;
154 for (int cfam = 0; cfam < 2; ++cfam) {
155 // Determine family name
157 case LyXFont::ROMAN_FAMILY:
159 case 0: ffamily = lyxrc.roman_font_name; break;
160 case 1: ffamily = "-*-times";
164 case LyXFont::SANS_FAMILY:
166 case 0: ffamily = lyxrc.sans_font_name; break;
167 case 1: ffamily = "-*-helvetica";
171 case LyXFont::TYPEWRITER_FAMILY:
173 case 0: ffamily = lyxrc.typewriter_font_name; break;
174 case 1: ffamily = "-*-courier";
181 for (int cser = 0; cser < 4; ++cser) {
182 // Determine series name
184 case LyXFont::MEDIUM_SERIES:
186 case 0: fseries = "-medium"; break;
187 case 1: fseries = "-book"; break;
188 case 2: fseries = "-light";
192 case LyXFont::BOLD_SERIES:
194 case 0: fseries = "-bold"; break;
195 case 1: fseries = "-black"; break;
196 case 2: fseries = "-demi"; break;
197 case 3: fseries = "-demibold";
204 for (int csha = 0; csha < 2; ++csha) {
205 // Determine shape name
207 case LyXFont::UP_SHAPE:
208 case LyXFont::SMALLCAPS_SHAPE:
210 case 0: fshape = "-r";
214 case LyXFont::ITALIC_SHAPE:
216 case 0: fshape = "-i"; break;
217 case 1: fshape = "-o";
221 case LyXFont::SLANTED_SHAPE:
223 case 0: fshape = "-o"; break;
224 case 1: fshape = "-i";
231 fontname = ffamily + fseries + fshape +
232 "-normal-*-*-*-*-*-*-*-" + norm;
233 fi->setPattern(fontname);
243 // A dummy fontstruct used when there is no gui.
246 XFontStruct dummyXFontStruct;
247 bool dummyXFontStructisGood = false;
252 XFontStruct * FontLoader::doLoad(LyXFont::FONT_FAMILY family,
253 LyXFont::FONT_SERIES series,
254 LyXFont::FONT_SHAPE shape,
255 LyXFont::FONT_SIZE size)
257 if (!lyxrc.use_gui) {
258 if (!dummyXFontStructisGood) {
259 // no character specific info
260 dummyXFontStruct.per_char = 0;
261 // unit ascent on character displays
262 dummyXFontStruct.ascent = 1;
263 // no descent on character displays
264 dummyXFontStruct.descent = 0;
265 dummyXFontStructisGood = true;
268 return &dummyXFontStruct;
271 getFontinfo(family, series, shape);
272 int fsize = int( (lyxrc.font_sizes[size] * lyxrc.dpi *
273 (lyxrc.zoom/100.0) ) / 72.27 + 0.5 );
275 string font = fontinfo[family][series][shape]->getFontname(fsize);
278 lyxerr << "No font matches request. Using 'fixed'." << endl;
279 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
283 XFontStruct * fs = 0;
285 current_view->owner()->messagePush(_("Loading font into X-Server..."));
287 fs = XLoadQueryFont(GUIRunTime::x11Display(), font.c_str());
290 if (font == "fixed") {
291 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
293 lyxerr << "Could not get font. Using 'fixed'." << endl;
294 fs = XLoadQueryFont(GUIRunTime::x11Display(), "fixed");
296 } else if (lyxerr.debugging(Debug::FONT)) {
297 // Tell user the font matching
303 // The rest of the attributes are not interesting
304 f.setEmph(LyXFont::INHERIT);
305 f.setUnderbar(LyXFont::INHERIT);
306 f.setNoun(LyXFont::INHERIT);
307 f.setColor(LColor::inherit);
308 lyxerr << "Font '" << f.stateText(0)
309 << "' matched by\n" << font << endl;
312 current_view->owner()->messagePop();
314 fontstruct[family][series][shape][size] = fs;
319 bool FontLoader::available(LyXFont const & f)
321 load(f.family(), f.series(), f.realShape(), f.size());
322 return fontinfo[f.family()][f.series()][f.realShape()]
323 ->getFontname(f.size()).size();