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