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()
26 using namespace lyx::support;
31 #include "lyx_forms.h"
33 /// Load font close to this size
34 string const FontInfo::getFontname(int size)
40 double error = 100000.0;
42 for (int i = 0; i < matches; ++i) {
44 // Scalable font should not be considered close
45 } else if (sizes[i] == size) {
46 lyxerr[Debug::FONT] << "Exact font match with\n"
47 << strings[i] << endl;
49 } else if (abs(sizes[i] - size - 0.1) < error) {
50 error = abs(sizes[i] - size - 0.1);
55 if (scalable && (lyxrc.use_scalable_fonts || closestind == -1)) {
56 // We can use scalable
57 string const font = resize(strings[scaleindex], size);
58 lyxerr[Debug::FONT] << "Using scalable font to get\n"
63 // Did any fonts get close?
64 if (closestind == -1) {
69 // We use the closest match
70 lyxerr[Debug::FONT] << "Using closest font match to get size "
72 << " with\n" << strings[closestind] << endl;
73 return strings[closestind];
77 /// Build newly sized font string
78 string const FontInfo::resize(string const & font, int size) const
81 // Find the position of the size spec
83 string::iterator before = string::iterator(0);
84 string::iterator after = string::iterator(0);
85 for (string::iterator sit = ret.begin();
86 sit != ret.end(); ++sit)
89 if (cut == 7) before = sit + 1;
95 ret.replace(before, after, tostr(size));
101 void FontInfo::setPattern(string const & pat)
108 /// Query font in X11
109 void FontInfo::query()
114 if (pattern.empty()) {
115 lyxerr << "Cannot use empty font name for font query."
122 if (lyx_gui::use_gui)
123 list = XListFonts(fl_get_display(), pattern.c_str(),
131 sizes.reset(new int[matches]);
132 strings.reset(new string[matches]);
134 // We have matches. Run them through
135 for (int i = 0; i < matches; ++i) {
136 string name(list[i]);
137 lyxerr[Debug::FONT] << "match #" << i << ' '
139 sizes[i] = atoi(token(name, '-', 7));
142 if (scaleindex == -1) {
146 } else if (atoi(token(name, '-', 12)) == 0)
147 // Ignore bogus matches of scalable fonts.
150 XFreeFontNames(list);
156 void FontInfo::init()