]> git.lyx.org Git - lyx.git/blob - src/FontLoader.C
fix static array
[lyx.git] / src / FontLoader.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 1997 Asger Alstrup
8  *           and the LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13 #include <cmath>        // fabs()
14
15 #ifdef __GNUG__
16 #pragma implementation "FontLoader.h"
17 #endif
18
19 #include "gettext.h"
20 #include "FontLoader.h"
21 #include "FontInfo.h"
22 #include "debug.h"
23 #include "lyxrc.h"      // lyxrc.font_*
24 #include "BufferView.h"
25 #include "LyXView.h"
26 #include "minibuffer.h"
27
28 using std::endl;
29
30 extern BufferView * current_view;
31
32
33 // The global fontloader
34 FontLoader fontloader;
35
36
37 // Initialize font loader
38 FontLoader::FontLoader()
39 {
40         reset();
41 }
42
43 // Destroy font loader
44 FontLoader::~FontLoader()
45 {
46         unload();
47 }
48
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()
53 {
54         unload();
55 }
56
57 // Reset font loader
58 void FontLoader::reset()
59 {
60         // Clear font infos, font structs and font metrics
61         for (int i1 = 0; i1 < 4; ++i1)
62                 for (int i2 = 0; i2 < 2; ++i2)
63                         for (int i3 = 0; i3 < 4; ++i3) {
64                                 fontinfo[i1][i2][i3] = 0;
65                                 for (int i4 = 0; i4<10; ++i4) {
66                                         fontstruct[i1][i2][i3][i4] = 0;
67                                 }
68                         }
69 }
70
71 // Unload all fonts
72 void FontLoader::unload() 
73 {
74         // Unload all fonts
75         for (int i1 = 0; i1 < 4; ++i1)
76                 for (int i2 = 0; i2 < 2; ++i2)
77                         for (int i3 = 0; i3 < 4; ++i3) {
78                                 if (fontinfo[i1][i2][i3]) {
79                                         delete fontinfo[i1][i2][i3];
80                                         fontinfo[i1][i2][i3] = 0;
81                                 }
82                                 for (int i4 = 0; i4 < 10; ++i4) {
83                                         if (fontstruct[i1][i2][i3][i4]) {
84                                                 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
85                                                 fontstruct[i1][i2][i3][i4] = 0;
86                                         }
87                                 }
88                         }
89 }
90
91 // Get font info
92 /* Takes care of finding which font that can match the given request. Tries
93 different alternatives. */
94 void FontLoader::getFontinfo(LyXFont::FONT_FAMILY family, 
95                              LyXFont::FONT_SERIES series, 
96                              LyXFont::FONT_SHAPE shape)
97 {
98         // Do we have the font info already?
99         if (fontinfo[family][series][shape] != 0)
100                 return;
101
102         // Special code for the symbol family
103         if (family == LyXFont::SYMBOL_FAMILY){
104                 fontinfo[family][series][shape] = new FontInfo("-*-symbol-*");
105                 return;
106         }
107
108         // Normal font. Let's search for an existing name that matches.
109         string ffamily;
110         string fseries;
111         string fshape;
112         string norm = lyxrc.font_norm;
113         string fontname;
114
115         FontInfo * fi = new FontInfo();
116         fontinfo[family][series][shape] = fi;
117
118         for (int cfam = 0; cfam < 2; ++cfam) {
119                 // Determine family name
120                 switch (family) {
121                 case LyXFont::ROMAN_FAMILY:
122                         switch (cfam) {
123                         case 0: ffamily = lyxrc.roman_font_name; break;
124                         case 1: ffamily = "-*-times";
125                         default: cfam = 100;
126                         }
127                         break;
128                 case LyXFont::SANS_FAMILY:
129                         switch (cfam) {
130                         case 0: ffamily = lyxrc.sans_font_name; break;
131                         case 1: ffamily = "-*-helvetica";
132                         default: cfam = 100;
133                         }
134                         break;
135                 case LyXFont::TYPEWRITER_FAMILY:
136                         switch (cfam) {
137                         case 0: ffamily = lyxrc.typewriter_font_name; break;
138                         case 1: ffamily = "-*-courier";
139                         default: cfam = 100;
140                         }
141                         break;
142                 default: ;
143                 }
144
145                 for (int cser = 0; cser < 4; ++cser) {
146                         // Determine series name
147                         switch (series) {
148                         case LyXFont::MEDIUM_SERIES:
149                                 switch (cser) {
150                                 case 0: fseries = "-medium"; break;
151                                 case 1: fseries = "-book"; break;
152                                 case 2: fseries = "-light";
153                                 default: cser = 100;
154                                 }
155                                 break;
156                         case LyXFont::BOLD_SERIES:
157                                 switch (cser) {
158                                 case 0: fseries = "-bold"; break;
159                                 case 1: fseries = "-black"; break;
160                                 case 2: fseries = "-demi"; break;
161                                 case 3: fseries = "-demibold";
162                                 default: cser = 100;
163                                 }
164                                 break;
165                         default: ;
166                         }
167
168                         for (int csha = 0; csha < 2; ++csha) {
169                                 // Determine shape name
170                                 switch (shape) {
171                                 case LyXFont::UP_SHAPE:
172                                 case LyXFont::SMALLCAPS_SHAPE:
173                                         switch (csha) {
174                                         case 0: fshape = "-r";
175                                         default: csha = 100;
176                                         }
177                                         break;
178                                 case LyXFont::ITALIC_SHAPE:
179                                         switch (csha) {
180                                         case 0: fshape = "-i"; break;
181                                         case 1: fshape = "-o";
182                                         default: csha = 100;
183                                         }
184                                         break;
185                                 case LyXFont::SLANTED_SHAPE:
186                                         switch (csha) {
187                                         case 0: fshape = "-o"; break;
188                                         case 1: fshape = "-i";
189                                         default: csha = 100;
190                                         }
191                                         break;
192                                 default: ;
193                                 }
194                                 //
195                                 fontname = ffamily + fseries + fshape +
196                                            "-normal-*-*-*-*-*-*-*-" + norm;
197                                 fi->setPattern(fontname);
198                                 if (fi->exist()) {
199                                         return;
200                                 }
201                         }
202                 }
203         }
204 }
205
206
207 // A dummy fontstruct used when there is no gui. 
208 static XFontStruct dummyXFontStruct;
209 static bool dummyXFontStructisGood = false;
210
211 /// Do load font
212 XFontStruct * FontLoader::doLoad(LyXFont::FONT_FAMILY family, 
213                                 LyXFont::FONT_SERIES series, 
214                                 LyXFont::FONT_SHAPE shape, 
215                                 LyXFont::FONT_SIZE size)
216 {
217         if (!lyxrc.use_gui) {
218                 if (!dummyXFontStructisGood) {
219                         // no character specific info
220                         dummyXFontStruct.per_char = 0; 
221                         // unit ascent on character displays
222                         dummyXFontStruct.ascent = 1; 
223                         // no descent on character displays
224                         dummyXFontStruct.descent = 0; 
225                         dummyXFontStructisGood = true;
226                 }
227
228                 return &dummyXFontStruct;
229         }
230
231         getFontinfo(family, series, shape);
232         int fsize = int( (lyxrc.font_sizes[size] * lyxrc.dpi * 
233                           (lyxrc.zoom/100.0) ) / 72.27 + 0.5 );
234
235         string font = fontinfo[family][series][shape]->getFontname(fsize);
236
237         if (font.empty()) {
238                 lyxerr << "No font matches request. Using 'fixed'." << endl;
239                 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
240                 font = "fixed";
241         }
242
243         XFontStruct * fs = 0;
244
245         current_view->owner()->getMiniBuffer()->Store();
246         current_view->owner()->getMiniBuffer()->Set(_("Loading font into X-Server..."));
247         fs = XLoadQueryFont(fl_get_display(), font.c_str());
248         
249         if (fs == 0) {
250                 if (font == "fixed") {
251                         lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
252                 } else {
253                         lyxerr << "Could not get font. Using 'fixed'." << endl;
254                         fs = XLoadQueryFont(fl_get_display(), "fixed");
255                 }
256         } else if (lyxerr.debugging(Debug::FONT)) {
257                 // Tell user the font matching
258                 LyXFont f;
259                 f.setFamily(family);
260                 f.setSeries(series);
261                 f.setShape(shape);
262                 f.setSize(size);
263                 // The rest of the attributes are not interesting
264                 f.setEmph(LyXFont::INHERIT);
265                 f.setUnderbar(LyXFont::INHERIT);
266                 f.setNoun(LyXFont::INHERIT);
267                 f.setLatex(LyXFont::INHERIT);
268                 f.setColor(LColor::inherit);
269                 lyxerr << "Font '" << f.stateText(0) 
270                        << "' matched by\n" << font << endl;
271         }
272
273         current_view->owner()->getMiniBuffer()->Reset();
274
275         fontstruct[family][series][shape][size] = fs;
276         return fs;
277 }