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