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