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