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
16 #include "lyxrc.h" // lyxrc.use_scalable_fonts
18 #include "support/tostr.h"
19 #include "support/lstrings.h"
20 #include "support/lyxlib.h"
22 #include "frontends/lyx_gui.h"
24 #include <cmath> // abs()
29 #include "lyx_forms.h"
31 /// Load font close to this size
32 string const FontInfo::getFontname(int size)
38 double error = 100000.0;
40 for (int i = 0; i < matches; ++i) {
42 // Scalable font should not be considered close
43 } else if (sizes[i] == size) {
44 lyxerr[Debug::FONT] << "Exact font match with\n"
45 << strings[i] << endl;
47 } else if (abs(sizes[i] - size - 0.1) < error) {
48 error = abs(sizes[i] - size - 0.1);
53 if (scalable && (lyxrc.use_scalable_fonts || closestind == -1)) {
54 // We can use scalable
55 string const font = resize(strings[scaleindex], size);
56 lyxerr[Debug::FONT] << "Using scalable font to get\n"
61 // Did any fonts get close?
62 if (closestind == -1) {
67 // We use the closest match
68 lyxerr[Debug::FONT] << "Using closest font match to get size "
70 << " with\n" << strings[closestind] << endl;
71 return strings[closestind];
75 /// Build newly sized font string
76 string const FontInfo::resize(string const & font, int size) const
79 // Find the position of the size spec
81 string::iterator before = string::iterator(0);
82 string::iterator after = string::iterator(0);
83 for (string::iterator sit = ret.begin();
84 sit != ret.end(); ++sit)
87 if (cut == 7) before = sit + 1;
93 ret.replace(before, after, tostr(size));
99 void FontInfo::setPattern(string const & pat)
106 /// Query font in X11
107 void FontInfo::query()
112 if (pattern.empty()) {
113 lyxerr << "Cannot use empty font name for font query."
120 if (lyx_gui::use_gui)
121 list = XListFonts(fl_get_display(), pattern.c_str(),
129 sizes.reset(new int[matches]);
130 strings.reset(new string[matches]);
132 // We have matches. Run them through
133 for (int i = 0; i < matches; ++i) {
134 string name(list[i]);
135 lyxerr[Debug::FONT] << "match #" << i << ' '
137 sizes[i] = lyx::atoi(token(name, '-', 7));
140 if (scaleindex == -1) {
144 } else if (lyx::atoi(token(name, '-', 12)) == 0)
145 // Ignore bogus matches of scalable fonts.
148 XFreeFontNames(list);
154 void FontInfo::init()