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.
17 #include "lyxrc.h" // lyxrc.use_scalable_fonts
19 #include "frontends/lyx_gui.h"
21 #include "support/lstrings.h"
22 #include "support/lyxlib.h"
23 #include "support/tostr.h"
25 #include "lyx_forms.h"
29 using lyx::support::atoi;
30 using lyx::support::token;
36 /// Load font close to this size
37 string const FontInfo::getFontname(int size)
43 double error = 100000.0;
45 for (int i = 0; i < matches; ++i) {
47 // Scalable font should not be considered close
48 } else if (sizes[i] == size) {
49 lyxerr[Debug::FONT] << "Exact font match with\n"
50 << strings[i] << endl;
52 } else if (abs(sizes[i] - size - 0.1) < error) {
53 error = abs(sizes[i] - size - 0.1);
58 if (scalable && (lyxrc.use_scalable_fonts || closestind == -1)) {
59 // We can use scalable
60 string const font = resize(strings[scaleindex], size);
61 lyxerr[Debug::FONT] << "Using scalable font to get\n"
66 // Did any fonts get close?
67 if (closestind == -1) {
72 // We use the closest match
73 lyxerr[Debug::FONT] << "Using closest font match to get size "
75 << " with\n" << strings[closestind] << endl;
76 return strings[closestind];
80 /// Build newly sized font string
81 string const FontInfo::resize(string const & font, int size) const
84 // Find the position of the size spec
86 string::iterator before = string::iterator(0);
87 string::iterator after = string::iterator(0);
88 for (string::iterator sit = ret.begin();
89 sit != ret.end(); ++sit)
92 if (cut == 7) before = sit + 1;
98 ret.replace(before, after, tostr(size));
104 void FontInfo::setPattern(string const & pat)
111 /// Query font in X11
112 void FontInfo::query()
117 if (pattern.empty()) {
118 lyxerr << "Cannot use empty font name for font query."
125 if (lyx_gui::use_gui)
126 list = XListFonts(fl_get_display(), pattern.c_str(),
134 sizes.reset(new int[matches]);
135 strings.reset(new string[matches]);
137 // We have matches. Run them through
138 for (int i = 0; i < matches; ++i) {
139 string name(list[i]);
140 lyxerr[Debug::FONT] << "match #" << i << ' '
142 sizes[i] = atoi(token(name, '-', 7));
145 if (scaleindex == -1) {
149 } else if (atoi(token(name, '-', 12)) == 0)
150 // Ignore bogus matches of scalable fonts.
153 XFreeFontNames(list);
159 void FontInfo::init()