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