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