]> git.lyx.org Git - lyx.git/blob - src/FontLoader.C
fix typo that put too many include paths for most people
[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-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific");
112                         return;
113
114                 case LyXFont::CMR_FAMILY:
115                         fontinfo[family][series][shape] =
116                                 new FontInfo("-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*");
117                         return;
118
119                 case LyXFont::CMSY_FAMILY:
120                         fontinfo[family][series][shape] =
121                                 new FontInfo("-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*");
122                         return;
123
124                 case LyXFont::CMM_FAMILY:
125                         fontinfo[family][series][shape] =
126                                 new FontInfo("-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*");
127                         return;
128
129                 case LyXFont::CMEX_FAMILY:
130                         fontinfo[family][series][shape] =
131                                 new FontInfo("-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*");
132                         return;
133
134                 case LyXFont::MSA_FAMILY:
135                         fontinfo[family][series][shape] =
136                                 new FontInfo("-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*");
137                         return;
138
139                 case LyXFont::MSB_FAMILY:
140                         fontinfo[family][series][shape] =
141                                 new FontInfo("-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*");
142                         return;
143
144                 case LyXFont::EUFRAK_FAMILY:
145                         fontinfo[family][series][shape] =
146                                 new FontInfo("-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*");
147                         return;
148
149                 default:
150                         break;
151         }
152
153
154         // Normal font. Let's search for an existing name that matches.
155         string ffamily;
156         string fseries;
157         string fshape;
158         string norm = lyxrc.font_norm;
159         string fontname;
160
161         FontInfo * fi = new FontInfo();
162         fontinfo[family][series][shape] = fi;
163
164         for (int cfam = 0; cfam < 2; ++cfam) {
165                 // Determine family name
166                 switch (family) {
167                 case LyXFont::ROMAN_FAMILY:
168                         switch (cfam) {
169                         case 0: ffamily = lyxrc.roman_font_name; break;
170                         case 1: ffamily = "-*-times";
171                         default: cfam = 100;
172                         }
173                         break;
174                 case LyXFont::SANS_FAMILY:
175                         switch (cfam) {
176                         case 0: ffamily = lyxrc.sans_font_name; break;
177                         case 1: ffamily = "-*-helvetica";
178                         default: cfam = 100;
179                         }
180                         break;
181                 case LyXFont::TYPEWRITER_FAMILY:
182                         switch (cfam) {
183                         case 0: ffamily = lyxrc.typewriter_font_name; break;
184                         case 1: ffamily = "-*-courier";
185                         default: cfam = 100;
186                         }
187                         break;
188                 default: ;
189                 }
190
191                 for (int cser = 0; cser < 4; ++cser) {
192                         // Determine series name
193                         switch (series) {
194                         case LyXFont::MEDIUM_SERIES:
195                                 switch (cser) {
196                                 case 0: fseries = "-medium"; break;
197                                 case 1: fseries = "-book"; break;
198                                 case 2: fseries = "-light";
199                                 default: cser = 100;
200                                 }
201                                 break;
202                         case LyXFont::BOLD_SERIES:
203                                 switch (cser) {
204                                 case 0: fseries = "-bold"; break;
205                                 case 1: fseries = "-black"; break;
206                                 case 2: fseries = "-demi"; break;
207                                 case 3: fseries = "-demibold";
208                                 default: cser = 100;
209                                 }
210                                 break;
211                         default: ;
212                         }
213
214                         for (int csha = 0; csha < 2; ++csha) {
215                                 // Determine shape name
216                                 switch (shape) {
217                                 case LyXFont::UP_SHAPE:
218                                 case LyXFont::SMALLCAPS_SHAPE:
219                                         switch (csha) {
220                                         case 0: fshape = "-r";
221                                         default: csha = 100;
222                                         }
223                                         break;
224                                 case LyXFont::ITALIC_SHAPE:
225                                         switch (csha) {
226                                         case 0: fshape = "-i"; break;
227                                         case 1: fshape = "-o";
228                                         default: csha = 100;
229                                         }
230                                         break;
231                                 case LyXFont::SLANTED_SHAPE:
232                                         switch (csha) {
233                                         case 0: fshape = "-o"; break;
234                                         case 1: fshape = "-i";
235                                         default: csha = 100;
236                                         }
237                                         break;
238                                 default: ;
239                                 }
240                                 //
241                                 fontname = ffamily + fseries + fshape +
242                                            "-normal-*-*-*-*-*-*-*-" + norm;
243                                 fi->setPattern(fontname);
244                                 if (fi->exist()) {
245                                         return;
246                                 }
247                         }
248                 }
249         }
250 }
251
252
253 // A dummy fontstruct used when there is no gui.
254 namespace {
255
256 XFontStruct dummyXFontStruct;
257 bool dummyXFontStructisGood = false;
258
259 } // namespace anon
260
261 /// Do load font
262 XFontStruct * FontLoader::doLoad(LyXFont::FONT_FAMILY family,
263                                 LyXFont::FONT_SERIES series,
264                                 LyXFont::FONT_SHAPE shape,
265                                 LyXFont::FONT_SIZE size)
266 {
267         if (!lyxrc.use_gui) {
268                 if (!dummyXFontStructisGood) {
269                         // no character specific info
270                         dummyXFontStruct.per_char = 0;
271                         // unit ascent on character displays
272                         dummyXFontStruct.ascent = 1;
273                         // no descent on character displays
274                         dummyXFontStruct.descent = 0;
275                         dummyXFontStructisGood = true;
276                 }
277
278                 return &dummyXFontStruct;
279         }
280
281         getFontinfo(family, series, shape);
282         int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
283                           (lyxrc.zoom/100.0)) / 72.27 + 0.5);
284
285         string font = fontinfo[family][series][shape]->getFontname(fsize);
286
287         if (font.empty()) {
288                 lyxerr << "No font matches request. Using 'fixed'." << endl;
289                 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
290                 font = "fixed";
291         }
292
293         XFontStruct * fs = 0;
294
295         current_view->owner()->messagePush(_("Loading font into X-Server..."));
296
297         fs = XLoadQueryFont(GUIRunTime::x11Display(), font.c_str());
298
299         if (fs == 0) {
300                 if (font == "fixed") {
301                         lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
302                 } else {
303                         lyxerr << "Could not get font. Using 'fixed'." << endl;
304                         fs = XLoadQueryFont(GUIRunTime::x11Display(), "fixed");
305                 }
306         } else if (lyxerr.debugging(Debug::FONT)) {
307                 // Tell user the font matching
308                 LyXFont f;
309                 f.setFamily(family);
310                 f.setSeries(series);
311                 f.setShape(shape);
312                 f.setSize(size);
313                 // The rest of the attributes are not interesting
314                 f.setEmph(LyXFont::INHERIT);
315                 f.setUnderbar(LyXFont::INHERIT);
316                 f.setNoun(LyXFont::INHERIT);
317                 f.setColor(LColor::inherit);
318                 lyxerr << "Font '" << f.stateText(0)
319                        << "' matched by\n" << font << endl;
320         }
321
322         current_view->owner()->messagePop();
323
324         fontstruct[family][series][shape][size] = fs;
325         return fs;
326 }
327
328
329 bool FontLoader::available(LyXFont const & f)
330 {
331         if (!lyxrc.use_gui)
332                 return false;
333
334         if (!fontinfo[f.family()][f.series()][f.realShape()])
335                 getFontinfo(f.family(), f.series(), f.realShape());
336         return fontinfo[f.family()][f.series()][f.realShape()]
337                 ->getFontname(f.size()).size();
338 }