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