3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
18 #include "qfont_loader.h"
22 #include "BufferView.h"
23 #include "qt_helpers.h"
26 #include <qfontmetrics.h>
28 #include "support/lstrings.h"
34 #include "support/systemcall.h"
35 #include "support/filetools.h"
41 qfont_loader::qfont_loader()
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;
55 qfont_loader::~qfont_loader()
60 void qfont_loader::update()
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;
75 QFont const & qfont_loader::get(LyXFont const & f)
77 QFont const & ret(getfontinfo(f)->font);
79 if (lyxerr.debugging(Debug::FONT)) {
80 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
81 << "' matched by\n" << ret.rawName() << endl;
84 lyxerr[Debug::FONT] << "The font has size: "
85 << ret.pointSizeFloat() << endl;
92 string const symbolPattern(LyXFont::FONT_FAMILY family)
95 case LyXFont::SYMBOL_FAMILY:
96 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
98 case LyXFont::CMR_FAMILY:
99 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
101 case LyXFont::CMSY_FAMILY:
102 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
104 case LyXFont::CMM_FAMILY:
105 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
107 case LyXFont::CMEX_FAMILY:
108 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
110 case LyXFont::MSA_FAMILY:
111 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
113 case LyXFont::MSB_FAMILY:
114 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
116 case LyXFont::EUFRAK_FAMILY:
117 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
119 case LyXFont::WASY_FAMILY:
120 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
130 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
134 char ** p = XGetFontPath(w.x11Display(), &n);
135 if (std::find(p, p + n, dir) != p + n)
137 lyxerr << "Adding " << dir << " to the font path.\n";
138 string const command = "xset fp+ " + dir;
140 if (!s.startscript(Systemcall::Wait, command))
142 lyxerr << "Unable to add font path.\n";
148 bool isAvailable(QFont const & font, LyXFont const & f) {
149 #if QT_VERSION >= 300
150 return font.exactMatch();
152 string tmp = symbolPattern(f.family());
156 return token(tmp, '-', 2) ==
157 token(font.rawName().latin1(), '-', 2);
163 qfont_loader::font_info::font_info(LyXFont const & f)
167 string pat = symbolPattern(f.family());
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) {
175 font.setRawName(pat.c_str());
179 switch (f.family()) {
180 case LyXFont::ROMAN_FAMILY:
181 font.setFamily(makeFontName(lyxrc.roman_font_name,
182 lyxrc.roman_font_foundry).c_str());
184 case LyXFont::SANS_FAMILY:
185 font.setFamily(makeFontName(lyxrc.sans_font_name,
186 lyxrc.sans_font_foundry).c_str());
188 case LyXFont::TYPEWRITER_FAMILY:
189 font.setFamily(makeFontName(lyxrc.typewriter_font_name,
190 lyxrc.typewriter_font_foundry).c_str());
196 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
197 * lyxrc.zoom / 100.0);
199 switch (f.series()) {
200 case LyXFont::MEDIUM_SERIES:
201 font.setWeight(QFont::Normal);
203 case LyXFont::BOLD_SERIES:
204 font.setWeight(QFont::Bold);
208 switch (f.realShape()) {
209 case LyXFont::ITALIC_SHAPE:
210 case LyXFont::SLANTED_SHAPE:
211 font.setItalic(true);
215 // Is this an exact match?
216 if (font.exactMatch()) {
217 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
219 lyxerr[Debug::FONT] << "This font is NOT an exact match"
223 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
225 metrics = QFontMetrics(font);
229 qfont_loader::font_info * qfont_loader::getfontinfo(LyXFont const & f)
231 if (!lyxrc.use_gui) {
235 font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
239 font_info * fi2 = new font_info(f);
240 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi2;
245 int qfont_loader::charwidth(LyXFont const & f, Uchar val)
247 font_info * fi = getfontinfo(f);
249 font_info::WidthCache::const_iterator cit = fi->widthcache.find(val);
250 if (cit != fi->widthcache.end())
253 int const w = fi->metrics.width(QChar(val));
254 fi->widthcache[val] = w;
259 bool qfont_loader::available(LyXFont const & f)
264 return isAvailable(getfontinfo(f)->font, f);