2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright (C) 1997 Asger Alstrup
10 *======================================================*/
13 #include <math.h> // fabs()
14 #include <stdlib.h> // atoi()
17 #pragma implementation "FontLoader.h"
21 #include "FontLoader.h"
24 #include "lyxrc.h" // lyxrc.font_*
26 #include "minibuffer.h"
27 extern MiniBuffer *minibuffer;
29 // Initialize font loader
30 FontLoader::FontLoader()
35 // Destroy font loader
36 FontLoader::~FontLoader()
41 // Update fonts after zoom, dpi, font names, or norm change
42 // For now, we just ditch all fonts we have. Later, we should
43 // reuse the ones that are already loaded.
44 void FontLoader::update()
50 void FontLoader::reset()
52 // Clear font infos, font structs and font metrics
53 for (int i1=0; i1<4; i1++)
54 for (int i2=0; i2<2; i2++)
55 for (int i3=0; i3<4; i3++) {
56 fontinfo[i1][i2][i3] = 0;
57 for (int i4=0; i4<10; i4++) {
58 fontstruct[i1][i2][i3][i4] = 0;
64 void FontLoader::unload()
67 for (int i1=0; i1<4; i1++)
68 for (int i2=0; i2<2; i2++)
69 for (int i3=0; i3<4; i3++) {
70 if (fontinfo[i1][i2][i3]) {
71 delete fontinfo[i1][i2][i3];
72 fontinfo[i1][i2][i3] = 0;
74 for (int i4=0; i4<10; i4++) {
75 if (fontstruct[i1][i2][i3][i4]) {
76 XFreeFont(fl_display, fontstruct[i1][i2][i3][i4]);
77 fontstruct[i1][i2][i3][i4] = 0;
84 /* Takes care of finding which font that can match the given request. Tries
85 different alternatives. */
86 void FontLoader::getFontinfo(LyXFont::FONT_FAMILY family,
87 LyXFont::FONT_SERIES series,
88 LyXFont::FONT_SHAPE shape)
90 // Do we have the font info already?
91 if (fontinfo[family][series][shape] != 0)
94 // Special code for the symbol family
95 if (family == LyXFont::SYMBOL_FAMILY){
96 fontinfo[family][series][shape] = new FontInfo("-*-symbol-*");
100 // Normal font. Let's search for an existing name that matches.
104 LString norm = lyxrc->font_norm;
107 FontInfo * fi = new FontInfo();
108 fontinfo[family][series][shape] = fi;
110 for (int cfam=0; cfam < 2; cfam++) {
111 // Determine family name
113 case LyXFont::ROMAN_FAMILY:
115 case 0: ffamily = lyxrc->roman_font_name; break;
116 case 1: ffamily = "-*-times";
120 case LyXFont::SANS_FAMILY:
122 case 0: ffamily = lyxrc->sans_font_name; break;
123 case 1: ffamily = "-*-helvetica";
127 case LyXFont::TYPEWRITER_FAMILY:
129 case 0: ffamily = lyxrc->typewriter_font_name; break;
130 case 1: ffamily = "-*-courier";
137 for (int cser=0; cser < 4; cser++) {
138 // Determine series name
140 case LyXFont::MEDIUM_SERIES:
142 case 0: fseries = "-medium"; break;
143 case 1: fseries = "-book"; break;
144 case 2: fseries = "-light";
148 case LyXFont::BOLD_SERIES:
150 case 0: fseries = "-bold"; break;
151 case 1: fseries = "-black"; break;
152 case 2: fseries = "-demi"; break;
153 case 3: fseries = "-demibold";
160 for (int csha=0; csha < 2; csha++) {
161 // Determine shape name
163 case LyXFont::UP_SHAPE:
164 case LyXFont::SMALLCAPS_SHAPE:
166 case 0: fshape = "-r";
170 case LyXFont::ITALIC_SHAPE:
172 case 0: fshape = "-i"; break;
173 case 1: fshape = "-o";
177 case LyXFont::SLANTED_SHAPE:
179 case 0: fshape = "-o"; break;
180 case 1: fshape = "-i";
187 fontname = ffamily + fseries + fshape +
188 "-normal-*-*-*-*-*-*-*-" + norm;
189 fi->setPattern(fontname);
199 XFontStruct* FontLoader::doLoad(LyXFont::FONT_FAMILY family,
200 LyXFont::FONT_SERIES series,
201 LyXFont::FONT_SHAPE shape,
202 LyXFont::FONT_SIZE size)
204 getFontinfo(family,series,shape);
205 int fsize = (int) ( (lyxrc->font_sizes[size] * lyxrc->dpi *
206 (lyxrc->zoom/100.0) ) / 72.27 + 0.5 );
208 LString font = fontinfo[family][series][shape]->getFontname(fsize);
211 lyxerr.print("No font matches request. Using 'fixed'.");
212 lyxerr.print("Start LyX as 'lyx -dbg 515' to get more information.");
217 minibuffer->Set(_("Loading font into X-Server..."));
219 XFontStruct* fs = XLoadQueryFont(fl_display, font.c_str());
223 lyxerr.print("We're doomed. Can't get 'fixed' font.");
225 lyxerr.print("Could not get font. Using 'fixed'.");
226 fs = XLoadQueryFont(fl_display, "fixed");
229 if (lyxerr.debugging(Error::FONT)) {
230 // Tell user the font matching
236 // The rest of the attributes are not interesting
237 f.setEmph(LyXFont::INHERIT);
238 f.setUnderbar(LyXFont::INHERIT);
239 f.setNoun(LyXFont::INHERIT);
240 f.setLatex(LyXFont::INHERIT);
241 f.setColor(LyXFont::INHERIT_COLOR);
242 lyxerr.print(LString("Font '") + f.stateText()
243 + "' matched by\n" + font + '\n');
248 fontstruct[family][series][shape][size] = fs;