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