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