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