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 FORMS_H_LOCATION
19 #include "FontLoader.h"
23 #include "lyxrc.h" // lyxrc.font_*
24 #include "BufferView.h"
26 #include "frontends/GUIRunTime.h"
30 extern BufferView * current_view;
33 // The global fontloader
34 FontLoader fontloader;
37 // Initialize font loader
38 FontLoader::FontLoader()
44 // Destroy font loader
45 FontLoader::~FontLoader()
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 FontLoader::update()
61 void FontLoader::reset()
63 // Clear font infos, font structs and font metrics
64 for (int i1 = 0; i1 < 4; ++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 FontLoader::unload()
79 for (int i1 = 0; i1 < 4; ++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(GUIRunTime::x11Display(), 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 FontLoader::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)
107 // Special code for the symbol family
108 if (family == LyXFont::SYMBOL_FAMILY){
109 fontinfo[family][series][shape] = new FontInfo("-*-symbol-*");
113 // Normal font. Let's search for an existing name that matches.
117 string norm = lyxrc.font_norm;
120 FontInfo * fi = new FontInfo();
121 fontinfo[family][series][shape] = fi;
123 for (int cfam = 0; cfam < 2; ++cfam) {
124 // Determine family name
126 case LyXFont::ROMAN_FAMILY:
128 case 0: ffamily = lyxrc.roman_font_name; break;
129 case 1: ffamily = "-*-times";
133 case LyXFont::SANS_FAMILY:
135 case 0: ffamily = lyxrc.sans_font_name; break;
136 case 1: ffamily = "-*-helvetica";
140 case LyXFont::TYPEWRITER_FAMILY:
142 case 0: ffamily = lyxrc.typewriter_font_name; break;
143 case 1: ffamily = "-*-courier";
150 for (int cser = 0; cser < 4; ++cser) {
151 // Determine series name
153 case LyXFont::MEDIUM_SERIES:
155 case 0: fseries = "-medium"; break;
156 case 1: fseries = "-book"; break;
157 case 2: fseries = "-light";
161 case LyXFont::BOLD_SERIES:
163 case 0: fseries = "-bold"; break;
164 case 1: fseries = "-black"; break;
165 case 2: fseries = "-demi"; break;
166 case 3: fseries = "-demibold";
173 for (int csha = 0; csha < 2; ++csha) {
174 // Determine shape name
176 case LyXFont::UP_SHAPE:
177 case LyXFont::SMALLCAPS_SHAPE:
179 case 0: fshape = "-r";
183 case LyXFont::ITALIC_SHAPE:
185 case 0: fshape = "-i"; break;
186 case 1: fshape = "-o";
190 case LyXFont::SLANTED_SHAPE:
192 case 0: fshape = "-o"; break;
193 case 1: fshape = "-i";
200 fontname = ffamily + fseries + fshape +
201 "-normal-*-*-*-*-*-*-*-" + norm;
202 fi->setPattern(fontname);
212 // A dummy fontstruct used when there is no gui.
215 XFontStruct dummyXFontStruct;
216 bool dummyXFontStructisGood = false;
221 XFontStruct * FontLoader::doLoad(LyXFont::FONT_FAMILY family,
222 LyXFont::FONT_SERIES series,
223 LyXFont::FONT_SHAPE shape,
224 LyXFont::FONT_SIZE size)
226 if (!lyxrc.use_gui) {
227 if (!dummyXFontStructisGood) {
228 // no character specific info
229 dummyXFontStruct.per_char = 0;
230 // unit ascent on character displays
231 dummyXFontStruct.ascent = 1;
232 // no descent on character displays
233 dummyXFontStruct.descent = 0;
234 dummyXFontStructisGood = true;
237 return &dummyXFontStruct;
240 getFontinfo(family, series, shape);
241 int fsize = int( (lyxrc.font_sizes[size] * lyxrc.dpi *
242 (lyxrc.zoom/100.0) ) / 72.27 + 0.5 );
244 string font = fontinfo[family][series][shape]->getFontname(fsize);
247 lyxerr << "No font matches request. Using 'fixed'." << endl;
248 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
252 XFontStruct * fs = 0;
254 current_view->owner()->messagePush(_("Loading font into X-Server..."));
256 fs = XLoadQueryFont(GUIRunTime::x11Display(), font.c_str());
259 if (font == "fixed") {
260 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
262 lyxerr << "Could not get font. Using 'fixed'." << endl;
263 fs = XLoadQueryFont(GUIRunTime::x11Display(), "fixed");
265 } else if (lyxerr.debugging(Debug::FONT)) {
266 // Tell user the font matching
272 // The rest of the attributes are not interesting
273 f.setEmph(LyXFont::INHERIT);
274 f.setUnderbar(LyXFont::INHERIT);
275 f.setNoun(LyXFont::INHERIT);
277 f.setLatex(LyXFont::INHERIT);
279 f.setColor(LColor::inherit);
280 lyxerr << "Font '" << f.stateText(0)
281 << "' matched by\n" << font << endl;
284 current_view->owner()->messagePop();
286 fontstruct[family][series][shape][size] = fs;