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"
19 #include "FontLoader.h"
22 #include "lyxrc.h" // lyxrc.font_*
23 #include "BufferView.h"
28 extern BufferView * current_view;
31 // The global fontloader
32 FontLoader fontloader;
35 // Initialize font loader
36 FontLoader::FontLoader()
42 // Destroy font loader
43 FontLoader::~FontLoader()
49 // Update fonts after zoom, dpi, font names, or norm change
50 // For now, we just ditch all fonts we have. Later, we should
51 // reuse the ones that are already loaded.
52 void FontLoader::update()
59 void FontLoader::reset()
61 // Clear font infos, font structs and font metrics
62 for (int i1 = 0; i1 < 4; ++i1)
63 for (int i2 = 0; i2 < 2; ++i2)
64 for (int i3 = 0; i3 < 4; ++i3) {
65 fontinfo[i1][i2][i3] = 0;
66 for (int i4 = 0; i4<10; ++i4) {
67 fontstruct[i1][i2][i3][i4] = 0;
74 void FontLoader::unload()
77 for (int i1 = 0; i1 < 4; ++i1)
78 for (int i2 = 0; i2 < 2; ++i2)
79 for (int i3 = 0; i3 < 4; ++i3) {
80 if (fontinfo[i1][i2][i3]) {
81 delete fontinfo[i1][i2][i3];
82 fontinfo[i1][i2][i3] = 0;
84 for (int i4 = 0; i4 < 10; ++i4) {
85 if (fontstruct[i1][i2][i3][i4]) {
86 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
87 fontstruct[i1][i2][i3][i4] = 0;
95 /* Takes care of finding which font that can match the given request. Tries
96 different alternatives. */
97 void FontLoader::getFontinfo(LyXFont::FONT_FAMILY family,
98 LyXFont::FONT_SERIES series,
99 LyXFont::FONT_SHAPE shape)
101 // Do we have the font info already?
102 if (fontinfo[family][series][shape] != 0)
105 // Special code for the symbol family
106 if (family == LyXFont::SYMBOL_FAMILY){
107 fontinfo[family][series][shape] = new FontInfo("-*-symbol-*");
111 // Normal font. Let's search for an existing name that matches.
115 string norm = lyxrc.font_norm;
118 FontInfo * fi = new FontInfo();
119 fontinfo[family][series][shape] = fi;
121 for (int cfam = 0; cfam < 2; ++cfam) {
122 // Determine family name
124 case LyXFont::ROMAN_FAMILY:
126 case 0: ffamily = lyxrc.roman_font_name; break;
127 case 1: ffamily = "-*-times";
131 case LyXFont::SANS_FAMILY:
133 case 0: ffamily = lyxrc.sans_font_name; break;
134 case 1: ffamily = "-*-helvetica";
138 case LyXFont::TYPEWRITER_FAMILY:
140 case 0: ffamily = lyxrc.typewriter_font_name; break;
141 case 1: ffamily = "-*-courier";
148 for (int cser = 0; cser < 4; ++cser) {
149 // Determine series name
151 case LyXFont::MEDIUM_SERIES:
153 case 0: fseries = "-medium"; break;
154 case 1: fseries = "-book"; break;
155 case 2: fseries = "-light";
159 case LyXFont::BOLD_SERIES:
161 case 0: fseries = "-bold"; break;
162 case 1: fseries = "-black"; break;
163 case 2: fseries = "-demi"; break;
164 case 3: fseries = "-demibold";
171 for (int csha = 0; csha < 2; ++csha) {
172 // Determine shape name
174 case LyXFont::UP_SHAPE:
175 case LyXFont::SMALLCAPS_SHAPE:
177 case 0: fshape = "-r";
181 case LyXFont::ITALIC_SHAPE:
183 case 0: fshape = "-i"; break;
184 case 1: fshape = "-o";
188 case LyXFont::SLANTED_SHAPE:
190 case 0: fshape = "-o"; break;
191 case 1: fshape = "-i";
198 fontname = ffamily + fseries + fshape +
199 "-normal-*-*-*-*-*-*-*-" + norm;
200 fi->setPattern(fontname);
210 // A dummy fontstruct used when there is no gui.
213 XFontStruct dummyXFontStruct;
214 bool dummyXFontStructisGood = false;
219 XFontStruct * FontLoader::doLoad(LyXFont::FONT_FAMILY family,
220 LyXFont::FONT_SERIES series,
221 LyXFont::FONT_SHAPE shape,
222 LyXFont::FONT_SIZE size)
224 if (!lyxrc.use_gui) {
225 if (!dummyXFontStructisGood) {
226 // no character specific info
227 dummyXFontStruct.per_char = 0;
228 // unit ascent on character displays
229 dummyXFontStruct.ascent = 1;
230 // no descent on character displays
231 dummyXFontStruct.descent = 0;
232 dummyXFontStructisGood = true;
235 return &dummyXFontStruct;
238 getFontinfo(family, series, shape);
239 int fsize = int( (lyxrc.font_sizes[size] * lyxrc.dpi *
240 (lyxrc.zoom/100.0) ) / 72.27 + 0.5 );
242 string font = fontinfo[family][series][shape]->getFontname(fsize);
245 lyxerr << "No font matches request. Using 'fixed'." << endl;
246 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
250 XFontStruct * fs = 0;
252 current_view->owner()->messagePush(_("Loading font into X-Server..."));
254 fs = XLoadQueryFont(fl_get_display(), font.c_str());
257 if (font == "fixed") {
258 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
260 lyxerr << "Could not get font. Using 'fixed'." << endl;
261 fs = XLoadQueryFont(fl_get_display(), "fixed");
263 } else if (lyxerr.debugging(Debug::FONT)) {
264 // Tell user the font matching
270 // The rest of the attributes are not interesting
271 f.setEmph(LyXFont::INHERIT);
272 f.setUnderbar(LyXFont::INHERIT);
273 f.setNoun(LyXFont::INHERIT);
275 f.setLatex(LyXFont::INHERIT);
277 f.setColor(LColor::inherit);
278 lyxerr << "Font '" << f.stateText(0)
279 << "' matched by\n" << font << endl;
282 current_view->owner()->messagePop();
284 fontstruct[family][series][shape][size] = fs;