2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1997 Asger Alstrup
10 * ====================================================== */
13 #include <cmath> // fabs()
14 #include <cstdlib> // atoi()
17 #pragma implementation "FontLoader.h"
21 #include "FontLoader.h"
24 #include "lyxrc.h" // lyxrc.font_*
25 #include "BufferView.h"
27 #include "minibuffer.h"
29 extern BufferView * current_view;
31 // Initialize font loader
32 FontLoader::FontLoader()
37 // Destroy font loader
38 FontLoader::~FontLoader()
43 // Update fonts after zoom, dpi, font names, or norm change
44 // For now, we just ditch all fonts we have. Later, we should
45 // reuse the ones that are already loaded.
46 void FontLoader::update()
52 void FontLoader::reset()
54 // Clear font infos, font structs and font metrics
55 for (int i1 = 0; i1<4; i1++)
56 for (int i2 = 0; i2<2; i2++)
57 for (int i3 = 0; i3<4; i3++) {
58 fontinfo[i1][i2][i3] = 0;
59 for (int i4 = 0; i4<10; i4++) {
60 fontstruct[i1][i2][i3][i4] = 0;
66 void FontLoader::unload()
69 for (int i1 = 0; i1<4; i1++)
70 for (int i2 = 0; i2<2; i2++)
71 for (int i3 = 0; i3<4; i3++) {
72 if (fontinfo[i1][i2][i3]) {
73 delete fontinfo[i1][i2][i3];
74 fontinfo[i1][i2][i3] = 0;
76 for (int i4 = 0; i4<10; i4++) {
77 if (fontstruct[i1][i2][i3][i4]) {
78 XFreeFont(fl_display, fontstruct[i1][i2][i3][i4]);
79 fontstruct[i1][i2][i3][i4] = 0;
86 /* Takes care of finding which font that can match the given request. Tries
87 different alternatives. */
88 void FontLoader::getFontinfo(LyXFont::FONT_FAMILY family,
89 LyXFont::FONT_SERIES series,
90 LyXFont::FONT_SHAPE shape)
92 // Do we have the font info already?
93 if (fontinfo[family][series][shape] != 0)
96 // Special code for the symbol family
97 if (family == LyXFont::SYMBOL_FAMILY){
98 fontinfo[family][series][shape] = new FontInfo("-*-symbol-*");
102 // Normal font. Let's search for an existing name that matches.
106 string norm = lyxrc.font_norm;
109 FontInfo * fi = new FontInfo();
110 fontinfo[family][series][shape] = fi;
112 for (int cfam = 0; cfam < 2; ++cfam) {
113 // Determine family name
115 case LyXFont::ROMAN_FAMILY:
117 case 0: ffamily = lyxrc.roman_font_name; break;
118 case 1: ffamily = "-*-times";
122 case LyXFont::SANS_FAMILY:
124 case 0: ffamily = lyxrc.sans_font_name; break;
125 case 1: ffamily = "-*-helvetica";
129 case LyXFont::TYPEWRITER_FAMILY:
131 case 0: ffamily = lyxrc.typewriter_font_name; break;
132 case 1: ffamily = "-*-courier";
139 for (int cser = 0; cser < 4; ++cser) {
140 // Determine series name
142 case LyXFont::MEDIUM_SERIES:
144 case 0: fseries = "-medium"; break;
145 case 1: fseries = "-book"; break;
146 case 2: fseries = "-light";
150 case LyXFont::BOLD_SERIES:
152 case 0: fseries = "-bold"; break;
153 case 1: fseries = "-black"; break;
154 case 2: fseries = "-demi"; break;
155 case 3: fseries = "-demibold";
162 for (int csha = 0; csha < 2; ++csha) {
163 // Determine shape name
165 case LyXFont::UP_SHAPE:
166 case LyXFont::SMALLCAPS_SHAPE:
168 case 0: fshape = "-r";
172 case LyXFont::ITALIC_SHAPE:
174 case 0: fshape = "-i"; break;
175 case 1: fshape = "-o";
179 case LyXFont::SLANTED_SHAPE:
181 case 0: fshape = "-o"; break;
182 case 1: fshape = "-i";
189 fontname = ffamily + fseries + fshape +
190 "-normal-*-*-*-*-*-*-*-" + norm;
191 fi->setPattern(fontname);
201 // A dummy fontstruct used when there is no gui.
202 static XFontStruct dummyXFontStruct;
203 static bool dummyXFontStructisGood = false;
206 XFontStruct * FontLoader::doLoad(LyXFont::FONT_FAMILY family,
207 LyXFont::FONT_SERIES series,
208 LyXFont::FONT_SHAPE shape,
209 LyXFont::FONT_SIZE size)
211 if (!lyxrc.use_gui) {
212 if (!dummyXFontStructisGood) {
213 // no character specific info
214 dummyXFontStruct.per_char=0;
215 // unit ascent on character displays
216 dummyXFontStruct.ascent = 1;
217 // no descent on character displays
218 dummyXFontStruct.descent = 0;
219 dummyXFontStructisGood = true;
222 return &dummyXFontStruct;
225 getFontinfo(family, series, shape);
226 int fsize = int( (lyxrc.font_sizes[size] * lyxrc.dpi *
227 (lyxrc.zoom/100.0) ) / 72.27 + 0.5 );
229 string font = fontinfo[family][series][shape]->getFontname(fsize);
232 lyxerr << "No font matches request. Using 'fixed'." << endl;
233 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
237 XFontStruct * fs = 0;
239 current_view->owner()->getMiniBuffer()->Store();
240 current_view->owner()->getMiniBuffer()->Set(_("Loading font into X-Server..."));
241 fs = XLoadQueryFont(fl_display, font.c_str());
244 if (font == "fixed") {
245 lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
247 lyxerr << "Could not get font. Using 'fixed'." << endl;
248 fs = XLoadQueryFont(fl_display, "fixed");
250 } else if (lyxerr.debugging(Debug::FONT)) {
251 // Tell user the font matching
257 // The rest of the attributes are not interesting
258 f.setEmph(LyXFont::INHERIT);
259 f.setUnderbar(LyXFont::INHERIT);
260 f.setNoun(LyXFont::INHERIT);
261 f.setLatex(LyXFont::INHERIT);
262 f.setColor(LColor::inherit);
263 lyxerr << "Font '" << f.stateText()
264 << "' matched by\n" << font << endl;
267 current_view->owner()->getMiniBuffer()->Reset();
269 fontstruct[family][series][shape][size] = fs;