]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xfont_loader.C
introduce namespace lyx::support
[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
15 #include "xfont_loader.h"
16 #include "FontInfo.h"
17 #include "gettext.h"
18 #include "debug.h"
19 #include "lyxrc.h"      // lyxrc.font_*
20 #include "BufferView.h"
21 #include "frontends/LyXView.h"
22 #include "frontends/lyx_gui.h"
23 #include "support/systemcall.h"
24 #include "support/filetools.h"
25
26 #include "lyx_forms.h"
27
28 using namespace lyx::support;
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 string const fontName(string const & family, string const & foundry)
132 {
133         if (foundry.empty() || foundry == "Xft")
134                 return "-*-"+family;
135         else
136                 return "-"+foundry+"-"+family;
137 }
138
139
140 bool addFontPath()
141 {
142         string const dir =  OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
143         if (!dir.empty()) {
144                 int n;
145                 char ** p = XGetFontPath(fl_get_display(), &n);
146                 if (std::find(p, p + n, dir) != p + n)
147                         return false;
148                 lyxerr[Debug::FONT] << "Adding " << dir
149                                     << " to the font path." << endl;
150                 string const command = "xset fp+ " + dir;
151                 Systemcall s;
152                 if (!s.startscript(Systemcall::Wait, command))
153                         return true;
154                 lyxerr << "Unable to add " << dir << "to the font path."
155                        << endl;
156         }
157         return false;
158 }
159
160 } // namespace anon
161
162 // Get font info
163 /* Takes care of finding which font that can match the given request. Tries
164 different alternatives. */
165 void xfont_loader::getFontinfo(LyXFont::FONT_FAMILY family,
166                              LyXFont::FONT_SERIES series,
167                              LyXFont::FONT_SHAPE shape)
168 {
169         // Do we have the font info already?
170         if (fontinfo[family][series][shape] != 0)
171                 return;
172
173         // Special fonts
174         string pat = symbolPattern(family);
175         if (!pat.empty()) {
176                 static bool first_time = true;
177                 fontinfo[family][series][shape] = new FontInfo(pat);
178                 if (family != LyXFont::SYMBOL_FAMILY &&
179                     !fontinfo[family][series][shape]->exist() &&
180                     first_time) {
181                         first_time = false;
182                         if (addFontPath()) {
183                                 delete fontinfo[family][series][shape];
184                                 fontinfo[family][series][shape] = new FontInfo(pat);
185                         }
186                 }
187                 return;
188         }
189
190
191         // Normal font. Let's search for an existing name that matches.
192         string ffamily;
193         string fseries;
194         string fshape;
195         string norm = lyxrc.font_norm;
196         string fontname;
197
198         FontInfo * fi = new FontInfo;
199         fontinfo[family][series][shape] = fi;
200
201         for (int cfam = 0; cfam < 2; ++cfam) {
202                 // Determine family name
203                 switch (family) {
204                 case LyXFont::ROMAN_FAMILY:
205                         switch (cfam) {
206                         case 0: ffamily = fontName(lyxrc.roman_font_name,
207                                                    lyxrc.roman_font_foundry);
208                                 break;
209                         case 1: ffamily = "-*-times";
210                         default: cfam = 100;
211                         }
212                         break;
213                 case LyXFont::SANS_FAMILY:
214                         switch (cfam) {
215                         case 0: ffamily = fontName(lyxrc.sans_font_name,
216                                                    lyxrc.sans_font_foundry);
217                                 break;
218                         case 1: ffamily = "-*-helvetica";
219                         default: cfam = 100;
220                         }
221                         break;
222                 case LyXFont::TYPEWRITER_FAMILY:
223                         switch (cfam) {
224                         case 0: ffamily = fontName(lyxrc.typewriter_font_name,
225                                                    lyxrc.typewriter_font_foundry);
226                                 break;
227                         case 1: ffamily = "-*-courier";
228                         default: cfam = 100;
229                         }
230                         break;
231                 default: ;
232                 }
233
234                 for (int cser = 0; cser < 4; ++cser) {
235                         // Determine series name
236                         switch (series) {
237                         case LyXFont::MEDIUM_SERIES:
238                                 switch (cser) {
239                                 case 0: fseries = "-medium"; break;
240                                 case 1: fseries = "-book"; break;
241                                 case 2: fseries = "-light";
242                                 default: cser = 100;
243                                 }
244                                 break;
245                         case LyXFont::BOLD_SERIES:
246                                 switch (cser) {
247                                 case 0: fseries = "-bold"; break;
248                                 case 1: fseries = "-black"; break;
249                                 case 2: fseries = "-demi"; break;
250                                 case 3: fseries = "-demibold";
251                                 default: cser = 100;
252                                 }
253                                 break;
254                         default: ;
255                         }
256
257                         for (int csha = 0; csha < 2; ++csha) {
258                                 // Determine shape name
259                                 switch (shape) {
260                                 case LyXFont::UP_SHAPE:
261                                 case LyXFont::SMALLCAPS_SHAPE:
262                                         switch (csha) {
263                                         case 0: fshape = "-r";
264                                         default: csha = 100;
265                                         }
266                                         break;
267                                 case LyXFont::ITALIC_SHAPE:
268                                         switch (csha) {
269                                         case 0: fshape = "-i"; break;
270                                         case 1: fshape = "-o";
271                                         default: csha = 100;
272                                         }
273                                         break;
274                                 case LyXFont::SLANTED_SHAPE:
275                                         switch (csha) {
276                                         case 0: fshape = "-o"; break;
277                                         case 1: fshape = "-i";
278                                         default: csha = 100;
279                                         }
280                                         break;
281                                 default: ;
282                                 }
283                                 //
284                                 fontname = ffamily + fseries + fshape +
285                                            "-normal-*-*-*-*-*-*-*-" + norm;
286                                 fi->setPattern(fontname);
287                                 if (fi->exist()) {
288                                         return;
289                                 }
290                         }
291                 }
292         }
293 }
294
295
296 // A dummy fontstruct used when there is no gui.
297 namespace {
298
299 XFontStruct dummyXFontStruct;
300 bool dummyXFontStructisGood = false;
301
302 } // namespace anon
303
304 /// Do load font
305 XFontStruct * xfont_loader::doLoad(LyXFont::FONT_FAMILY family,
306                                 LyXFont::FONT_SERIES series,
307                                 LyXFont::FONT_SHAPE shape,
308                                 LyXFont::FONT_SIZE size)
309 {
310         if (!lyx_gui::use_gui) {
311                 if (!dummyXFontStructisGood) {
312                         // no character specific info
313                         dummyXFontStruct.per_char = 0;
314                         // unit ascent on character displays
315                         dummyXFontStruct.ascent = 1;
316                         // no descent on character displays
317                         dummyXFontStruct.descent = 0;
318                         dummyXFontStructisGood = true;
319                 }
320
321                 return &dummyXFontStruct;
322         }
323
324         getFontinfo(family, series, shape);
325         // FIXME! CHECK! Should we use 72.0 or 72.27? (Lgb)
326         int fsize = int((lyxrc.font_sizes[size] * lyxrc.dpi *
327                           (lyxrc.zoom/100.0)) / 72.27 + 0.5);
328
329         string font = fontinfo[family][series][shape]->getFontname(fsize);
330
331         if (font.empty()) {
332                 lyxerr << "No font matches request. Using 'fixed'." << endl;
333                 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
334                 font = "fixed";
335         }
336
337         XFontStruct * fs = 0;
338
339         fs = XLoadQueryFont(fl_get_display(), font.c_str());
340
341         if (fs == 0) {
342                 if (font == "fixed") {
343                         lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
344                 } else {
345                         lyxerr << "Could not get font '" << font
346                                 << "'. Using 'fixed'." << endl;
347                         fs = XLoadQueryFont(fl_get_display(), "fixed");
348                 }
349         } else if (lyxerr.debugging(Debug::FONT)) {
350                 // Tell user the font matching
351                 LyXFont f;
352                 f.setFamily(family);
353                 f.setSeries(series);
354                 f.setShape(shape);
355                 f.setSize(size);
356                 // The rest of the attributes are not interesting
357                 f.setEmph(LyXFont::INHERIT);
358                 f.setUnderbar(LyXFont::INHERIT);
359                 f.setNoun(LyXFont::INHERIT);
360                 f.setColor(LColor::inherit);
361                 lyxerr << "Font '" << f.stateText(0)
362                        << "' matched by\n" << font << endl;
363         }
364
365         fontstruct[family][series][shape][size] = fs;
366         return fs;
367 }
368
369
370 bool xfont_loader::available(LyXFont const & f)
371 {
372         if (!lyx_gui::use_gui)
373                 return false;
374
375         if (!fontinfo[f.family()][f.series()][f.realShape()])
376                 getFontinfo(f.family(), f.series(), f.realShape());
377         return fontinfo[f.family()][f.series()][f.realShape()]->exist();
378 }