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"
26 #include "support/lstrings.h"
32 #include "support/systemcall.h"
33 #include "support/filetools.h"
39 qfont_loader::qfont_loader()
41 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
42 for (int i2 = 0; i2 < 2; ++i2) {
43 for (int i3 = 0; i3 < 4; ++i3) {
44 for (int i4 = 0; i4 < 10; ++i4) {
45 fontinfo_[i1][i2][i3][i4] = 0;
53 qfont_loader::~qfont_loader()
58 void qfont_loader::update()
60 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
61 for (int i2 = 0; i2 < 2; ++i2) {
62 for (int i3 = 0; i3 < 4; ++i3) {
63 for (int i4 = 0; i4 < 10; ++i4) {
64 delete fontinfo_[i1][i2][i3][i4];
65 fontinfo_[i1][i2][i3][i4] = 0;
73 QFont const & qfont_loader::get(LyXFont const & f)
75 QFont const & ret(getfontinfo(f)->font);
77 if (lyxerr.debugging(Debug::FONT)) {
78 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
79 << "' matched by\n" << ret.rawName() << endl;
82 lyxerr[Debug::FONT] << "The font has size: "
83 << ret.pointSizeFloat() << endl;
90 string const symbolPattern(LyXFont::FONT_FAMILY family)
93 case LyXFont::SYMBOL_FAMILY:
94 return "-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific";
96 case LyXFont::CMR_FAMILY:
97 return "-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*";
99 case LyXFont::CMSY_FAMILY:
100 return "-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*";
102 case LyXFont::CMM_FAMILY:
103 return "-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*";
105 case LyXFont::CMEX_FAMILY:
106 return "-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*";
108 case LyXFont::MSA_FAMILY:
109 return "-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*";
111 case LyXFont::MSB_FAMILY:
112 return "-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*";
114 case LyXFont::EUFRAK_FAMILY:
115 return "-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*";
117 case LyXFont::WASY_FAMILY:
118 return "-*-wasy10-medium-*-*-*-*-*-*-*-*-*-*-*";
128 string const dir = OnlyPath(LibFileSearch("xfonts", "fonts.dir"));
132 char ** p = XGetFontPath(w.x11Display(), &n);
133 if (std::find(p, p + n, dir) != p + n)
135 lyxerr << "Adding " << dir << " to the font path.\n";
136 string const command = "xset fp+ " + dir;
138 if (!s.startscript(Systemcall::Wait, command))
140 lyxerr << "Unable to add font path.\n";
146 bool isAvailable(QFont const & font, LyXFont const & f) {
147 #if QT_VERSION >= 300
148 return font.exactMatch();
150 string tmp = symbolPattern(f.family());
154 return token(tmp, '-', 2) ==
155 token(font.rawName().latin1(), '-', 2);
161 qfont_loader::font_info::font_info(LyXFont const & f)
165 string pat = symbolPattern(f.family());
167 static bool first_time = true;
168 font.setRawName(pat.c_str());
169 if (f.family() != LyXFont::SYMBOL_FAMILY &&
170 !isAvailable(font, f) && first_time) {
173 font.setRawName(pat.c_str());
177 switch (f.family()) {
178 case LyXFont::ROMAN_FAMILY:
179 font.setFamily(lyxrc.roman_font_name.c_str());
181 case LyXFont::SANS_FAMILY:
182 font.setFamily(lyxrc.sans_font_name.c_str());
184 case LyXFont::TYPEWRITER_FAMILY:
185 font.setFamily(lyxrc.typewriter_font_name.c_str());
191 font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
192 * lyxrc.zoom / 100.0);
194 switch (f.series()) {
195 case LyXFont::MEDIUM_SERIES:
196 font.setWeight(QFont::Normal);
198 case LyXFont::BOLD_SERIES:
199 font.setWeight(QFont::Bold);
203 switch (f.realShape()) {
204 case LyXFont::ITALIC_SHAPE:
205 case LyXFont::SLANTED_SHAPE:
206 font.setItalic(true);
210 // Is this an exact match?
211 if (font.exactMatch()) {
212 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
214 lyxerr[Debug::FONT] << "This font is NOT an exact match"
218 lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
220 metrics = QFontMetrics(font);
224 qfont_loader::font_info const * qfont_loader::getfontinfo(LyXFont const & f)
226 if (!lyxrc.use_gui) {
230 font_info const * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
232 fi = new font_info(f);
233 fontinfo_[f.family()][f.series()][f.realShape()][f.size()] = fi;
240 bool qfont_loader::available(LyXFont const & f)
245 return isAvailable(getfontinfo(f)->font, f);