]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_loader.C
widthcache ting
[lyx.git] / src / frontends / qt2 / qfont_loader.C
1 /**
2  * \file qfont_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  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "qfont_loader.h"
19 #include "gettext.h"
20 #include "debug.h"
21 #include "lyxrc.h"
22 #include "BufferView.h"
23 #include "qt_helpers.h"
24
25 #include <qglobal.h>
26 #include <qfontmetrics.h>
27 #if QT_VERSION < 300
28 #include "support/lstrings.h"
29 #endif
30
31 #ifdef Q_WS_X11
32 #include <qwidget.h>
33 #include <X11/Xlib.h>
34 #include "support/systemcall.h"
35 #include "support/filetools.h"
36 #endif
37
38 using std::endl;
39
40
41 qfont_loader::qfont_loader()
42 {
43         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
44                 for (int i2 = 0; i2 < 2; ++i2) {
45                         for (int i3 = 0; i3 < 4; ++i3) {
46                                 for (int i4 = 0; i4 < 10; ++i4) {
47                                         fontinfo_[i1][i2][i3][i4] = 0;
48                                 }
49                         }
50                 }
51         }
52 }
53
54
55 qfont_loader::~qfont_loader()
56 {
57 }
58
59
60 void qfont_loader::update()
61 {
62         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
63                 for (int i2 = 0; i2 < 2; ++i2) {
64                         for (int i3 = 0; i3 < 4; ++i3) {
65                                 for (int i4 = 0; i4 < 10; ++i4) {
66                                         delete fontinfo_[i1][i2][i3][i4];
67                                         fontinfo_[i1][i2][i3][i4] = 0;
68                                 }
69                         }
70                 }
71         }
72 }
73
74
75 QFont const & qfont_loader::get(LyXFont const & f)
76 {
77         QFont const & ret(getfontinfo(f)->font);
78
79         if (lyxerr.debugging(Debug::FONT)) {
80                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
81                         << "' matched by\n" << ret.rawName() << endl;
82         }
83
84         lyxerr[Debug::FONT] << "The font has size: "
85                             << ret.pointSizeFloat() << endl;
86
87         return ret;
88 }
89
90 namespace {
91
92 string const symbolPattern(LyXFont::FONT_FAMILY family)
93 {
94         switch (family) {
95         case LyXFont::SYMBOL_FAMILY:
96                 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
97
98         case LyXFont::CMR_FAMILY:
99                 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
100
101         case LyXFont::CMSY_FAMILY:
102                 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
103
104         case LyXFont::CMM_FAMILY:
105                 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
106
107         case LyXFont::CMEX_FAMILY:
108                 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
109
110         case LyXFont::MSA_FAMILY:
111                 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
112
113         case LyXFont::MSB_FAMILY:
114                 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
115
116         case LyXFont::EUFRAK_FAMILY:
117                 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
118
119         case LyXFont::WASY_FAMILY:
120                 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
121
122         default:
123                 return string();
124         }
125 }
126
127 bool addFontPath()
128 {
129 #ifdef Q_WS_X11
130         string const dir =  OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
131         if (!dir.empty()) {
132                 QWidget w;
133                 int n;
134                 char ** p = XGetFontPath(w.x11Display(), &n);
135                 if (std::find(p, p + n, dir) != p + n)
136                         return false;
137                 lyxerr << "Adding " << dir << " to the font path.\n";
138                 string const command = "xset fp+ " + dir;
139                 Systemcall s;
140                 if (!s.startscript(Systemcall::Wait, command))
141                         return true;
142                 lyxerr << "Unable to add font path.\n";
143         }
144 #endif
145         return false;
146 }
147
148 bool isAvailable(QFont const & font, LyXFont const & f) {
149 #if QT_VERSION >= 300
150         return font.exactMatch();
151 #else
152         string tmp = symbolPattern(f.family());
153         if (tmp.empty())
154                 return false;
155         else
156                 return token(tmp, '-', 2) ==
157                         token(font.rawName().latin1(), '-', 2);
158 #endif
159 }
160
161 } // namespace anon
162
163 qfont_loader::font_info::font_info(LyXFont const & f)
164         : metrics(font)
165 {
166
167         string pat = symbolPattern(f.family());
168         if (!pat.empty()) {
169                 static bool first_time = true;
170                 font.setRawName(pat.c_str());
171                 if (f.family() != LyXFont::SYMBOL_FAMILY &&
172                     !isAvailable(font, f) && first_time) {
173                         first_time = false;
174                         if (addFontPath()) {
175                                 font.setRawName(pat.c_str());
176                         }
177                 }
178         } else
179                 switch (f.family()) {
180                 case LyXFont::ROMAN_FAMILY:
181                         font.setFamily(makeFontName(lyxrc.roman_font_name,
182                                                     lyxrc.roman_font_foundry).c_str());
183                         break;
184                 case LyXFont::SANS_FAMILY:
185                         font.setFamily(makeFontName(lyxrc.sans_font_name,
186                                                     lyxrc.sans_font_foundry).c_str());
187                         break;
188                 case LyXFont::TYPEWRITER_FAMILY:
189                         font.setFamily(makeFontName(lyxrc.typewriter_font_name,
190                                                     lyxrc.typewriter_font_foundry).c_str());
191                         break;
192                 default:
193                         break;
194         }
195
196         font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
197                                * lyxrc.zoom / 100.0);
198
199         switch (f.series()) {
200                 case LyXFont::MEDIUM_SERIES:
201                         font.setWeight(QFont::Normal);
202                         break;
203                 case LyXFont::BOLD_SERIES:
204                         font.setWeight(QFont::Bold);
205                         break;
206         }
207
208         switch (f.realShape()) {
209                 case LyXFont::ITALIC_SHAPE:
210                 case LyXFont::SLANTED_SHAPE:
211                         font.setItalic(true);
212                         break;
213         }
214
215         // Is this an exact match?
216         if (font.exactMatch()) {
217                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
218         } else {
219                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
220                                     << endl;
221         }
222
223         lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
224
225         metrics = QFontMetrics(font);
226 }
227
228
229 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
230 {
231         if (!lyxrc.use_gui) {
232                 // FIXME
233         }
234
235         font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
236         if (fi)
237                 return fi;
238  
239         font_info * fi2 = new font_info(f);
240         fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
241         return fi2;
242 }
243
244
245 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
246 {
247         font_info * fi = getfontinfo(f);
248
249         font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
250         if (cit != fi->widthcache.end())
251                 return cit->second;
252
253         int const w = fi->metrics.width(QChar(val));
254         fi->widthcache[val] = w;
255         return w;
256 }
257
258  
259 bool qfont_loader::available(LyXFont const & f)
260 {
261         if (!lyxrc.use_gui)
262                 return false;
263
264         return isAvailable(getfontinfo(f)->font, f);
265 }