]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xfont_loader.C
fix crash with "save as"
[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 #include "support/systemcall.h"
26 #include "support/filetools.h"
27
28 #include FORMS_H_LOCATION
29
30 using std::endl;
31
32 // The global fontloader
33 xfont_loader fontloader;
34
35
36 // Initialize font loader
37 xfont_loader::xfont_loader()
38 {
39         reset();
40 }
41
42
43 // Destroy font loader
44 xfont_loader::~xfont_loader()
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 xfont_loader::update()
54 {
55         unload();
56 }
57
58
59 // Reset font loader
60 void xfont_loader::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 xfont_loader::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(fl_get_display(), fontstruct[i1][i2][i3][i4]);
88                                                 fontstruct[i1][i2][i3][i4] = 0;
89                                         }
90                                 }
91                         }
92 }
93
94 namespace {
95
96 string const symbolPattern(LyXFont::FONT_FAMILY family)
97 {
98         switch (family) {
99         case LyXFont::SYMBOL_FAMILY:
100                 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
101
102         case LyXFont::CMR_FAMILY:
103                 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
104
105         case LyXFont::CMSY_FAMILY:
106                 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
107
108         case LyXFont::CMM_FAMILY:
109                 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
110
111         case LyXFont::CMEX_FAMILY:
112                 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
113
114         case LyXFont::MSA_FAMILY:
115                 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
116
117         case LyXFont::MSB_FAMILY:
118                 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
119
120         case LyXFont::EUFRAK_FAMILY:
121                 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
122
123         case LyXFont::WASY_FAMILY:
124                 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
125
126         default:
127                 return string();
128         }       
129 }
130
131 bool addFontPath()
132 {
133         string const dir =  OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
134         if (!dir.empty()) {
135                 int n;
136                 char ** p = XGetFontPath(fl_get_display(), &n);
137                 if (std::find(p, p + n, dir) != p + n)
138                         return false;
139                 lyxerr << "Adding " << dir << " to the font path.\n";
140                 string const command = "xset fp+ " + dir;
141                 Systemcall s;
142                 if (!s.startscript(Systemcall::Wait, command)) 
143                         return true;
144                 lyxerr << "Unable to add font path.\n";
145         }
146         return false;
147 }
148
149 } // namespace anon
150
151 // Get font info
152 /* Takes care of finding which font that can match the given request. Tries
153 different alternatives. */
154 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
155                              LyXFont::FONT_SERIES series,
156                              LyXFont::FONT_SHAPE shape)
157 {
158         // Do we have the font info already?
159         if (fontinfo[family][series][shape] != 0)
160                 return;
161
162         // Special fonts
163         string pat = symbolPattern(family);
164         if (!pat.empty()) {
165                 static bool first_time = true;
166                 fontinfo[family][series][shape] = new FontInfo(pat);
167                 if (family != LyXFont::SYMBOL_FAMILY &&
168                     !fontinfo[family][series][shape]->exist() &&
169                     first_time) {
170                         first_time = false;
171                         if (addFontPath()) {
172                                 delete fontinfo[family][series][shape];
173                                 fontinfo[family][series][shape] = new FontInfo(pat);    
174                         }
175                 }
176                 return;
177         }
178
179
180         // Normal font. Let's search for an existing name that matches.
181         string ffamily;
182         string fseries;
183         string fshape;
184         string norm = lyxrc.font_norm;
185         string fontname;
186
187         FontInfo * fi = new FontInfo;
188         fontinfo[family][series][shape] = fi;
189
190         for (int cfam = 0; cfam < 2; ++cfam) {
191                 // Determine family name
192                 switch (family) {
193                 case LyXFont::ROMAN_FAMILY:
194                         switch (cfam) {
195                         case 0: ffamily = lyxrc.roman_font_name; break;
196                         case 1: ffamily = "-*-times";
197                         default: cfam = 100;
198                         }
199                         break;
200                 case LyXFont::SANS_FAMILY:
201                         switch (cfam) {
202                         case 0: ffamily = lyxrc.sans_font_name; break;
203                         case 1: ffamily = "-*-helvetica";
204                         default: cfam = 100;
205                         }
206                         break;
207                 case LyXFont::TYPEWRITER_FAMILY:
208                         switch (cfam) {
209                         case 0: ffamily = lyxrc.typewriter_font_name; break;
210                         case 1: ffamily = "-*-courier";
211                         default: cfam = 100;
212                         }
213                         break;
214                 default: ;
215                 }
216
217                 for (int cser = 0; cser < 4; ++cser) {
218                         // Determine series name
219                         switch (series) {
220                         case LyXFont::MEDIUM_SERIES:
221                                 switch (cser) {
222                                 case 0: fseries = "-medium"; break;
223                                 case 1: fseries = "-book"; break;
224                                 case 2: fseries = "-light";
225                                 default: cser = 100;
226                                 }
227                                 break;
228                         case LyXFont::BOLD_SERIES:
229                                 switch (cser) {
230                                 case 0: fseries = "-bold"; break;
231                                 case 1: fseries = "-black"; break;
232                                 case 2: fseries = "-demi"; break;
233                                 case 3: fseries = "-demibold";
234                                 default: cser = 100;
235                                 }
236                                 break;
237                         default: ;
238                         }
239
240                         for (int csha = 0; csha < 2; ++csha) {
241                                 // Determine shape name
242                                 switch (shape) {
243                                 case LyXFont::UP_SHAPE:
244                                 case LyXFont::SMALLCAPS_SHAPE:
245                                         switch (csha) {
246                                         case 0: fshape = "-r";
247                                         default: csha = 100;
248                                         }
249                                         break;
250                                 case LyXFont::ITALIC_SHAPE:
251                                         switch (csha) {
252                                         case 0: fshape = "-i"; break;
253                                         case 1: fshape = "-o";
254                                         default: csha = 100;
255                                         }
256                                         break;
257                                 case LyXFont::SLANTED_SHAPE:
258                                         switch (csha) {
259                                         case 0: fshape = "-o"; break;
260                                         case 1: fshape = "-i";
261                                         default: csha = 100;
262                                         }
263                                         break;
264                                 default: ;
265                                 }
266                                 //
267                                 fontname = ffamily + fseries + fshape +
268                                            "-normal-*-*-*-*-*-*-*-" + norm;
269                                 fi->setPattern(fontname);
270                                 if (fi->exist()) {
271                                         return;
272                                 }
273                         }
274                 }
275         }
276 }
277
278
279 // A dummy fontstruct used when there is no gui.
280 namespace {
281
282 XFontStruct dummyXFontStruct;
283 bool dummyXFontStructisGood = false;
284
285 } // namespace anon
286
287 /// Do load font
288 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
289                                 LyXFont::FONT_SERIES series,
290                                 LyXFont::FONT_SHAPE shape,
291                                 LyXFont::FONT_SIZE size)
292 {
293         if (!lyxrc.use_gui) {
294                 if (!dummyXFontStructisGood) {
295                         // no character specific info
296                         dummyXFontStruct.per_char = 0;
297                         // unit ascent on character displays
298                         dummyXFontStruct.ascent = 1;
299                         // no descent on character displays
300                         dummyXFontStruct.descent = 0;
301                         dummyXFontStructisGood = true;
302                 }
303
304                 return &dummyXFontStruct;
305         }
306
307         getFontinfo(family, series, shape);
308         // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
309         int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
310                           (lyxrc.zoom/100.0)) / 72.27 + 0.5);
311
312         string font = fontinfo[family][series][shape]->getFontname(fsize);
313
314         if (font.empty()) {
315                 lyxerr << "No font matches request. Using 'fixed'." << endl;
316                 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
317                 font = "fixed";
318         }
319
320         XFontStruct * fs = 0;
321
322         fs = XLoadQueryFont(fl_get_display(), font.c_str());
323
324         if (fs == 0) {
325                 if (font == "fixed") {
326                         lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
327                 } else {
328                         lyxerr << "Could not get font '" << font
329                                 << "'. Using 'fixed'." << endl;
330                         fs = XLoadQueryFont(fl_get_display(), "fixed");
331                 }
332         } else if (lyxerr.debugging(Debug::FONT)) {
333                 // Tell user the font matching
334                 LyXFont f;
335                 f.setFamily(family);
336                 f.setSeries(series);
337                 f.setShape(shape);
338                 f.setSize(size);
339                 // The rest of the attributes are not interesting
340                 f.setEmph(LyXFont::INHERIT);
341                 f.setUnderbar(LyXFont::INHERIT);
342                 f.setNoun(LyXFont::INHERIT);
343                 f.setColor(LColor::inherit);
344                 lyxerr << "Font '" << f.stateText(0)
345                        << "' matched by\n" << font << endl;
346         }
347
348         fontstruct[family][series][shape][size] = fs;
349         return fs;
350 }
351
352
353 bool xfont_loader::available(LyXFont const & f)
354 {
355         if (!lyxrc.use_gui)
356                 return false;
357
358         if (!fontinfo[f.family()][f.series()][f.realShape()])
359                 getFontinfo(f.family(), f.series(), f.realShape());
360         return fontinfo[f.family()][f.series()][f.realShape()]->exist();
361 }