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