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