2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1997 Asger Alstrup
10 * ====================================================== */
13 #include <cmath> // fabs()
15 #include FORMS_H_LOCATION
18 #pragma implementation "FontInfo.h"
23 #include "lyxrc.h" // lyxrc.use_scalable_fonts
24 #include "support/lstrings.h"
28 /// Load font close to this size
29 string const FontInfo::getFontname(int size)
35 double error = 100000.0;
37 for (int i = 0; i < matches; ++i) {
39 // Scalable font should not be considered close
40 } else if (sizes[i] == size) {
41 lyxerr[Debug::FONT] << "Exact font match with\n"
42 << strings[i] << endl;
44 } else if (fabs(sizes[i] - size - 0.1) < error) {
45 error = fabs(sizes[i] - size - 0.1);
50 if (scalable && lyxrc.use_scalable_fonts) {
51 // We can use scalable
52 string font = resize(strings[scaleindex], size);
53 lyxerr[Debug::FONT] << "Using scalable font to get\n"
58 // Did any fonts get close?
59 if (closestind == -1) {
60 // No, and we are not allowed to use scalables, so...
64 // We use the closest match
65 lyxerr[Debug::FONT] << "Using closest font match to get size "
67 << " with\n" << strings[closestind] << endl;
68 return strings[closestind];
72 /// Build newly sized font string
73 string const FontInfo::resize(string const & font, int size) const
76 // Find the position of the size spec
78 string::iterator before = string::iterator(0);
79 string::iterator after = string::iterator(0);
80 for (string::iterator sit = ret.begin();
81 sit != ret.end(); ++sit)
84 if (cut == 7) before = sit + 1;
90 ret.replace(before, after, tostr(size));
96 void FontInfo::setPattern(string const & pat)
104 /// Query font in X11
105 void FontInfo::query()
110 if (pattern.empty()) {
111 lyxerr << "Can not use empty font name for font query."
119 list = XListFonts(fl_get_display(), pattern.c_str(), 100, &matches);
127 sizes = new int[matches];
128 strings = new string[matches];
130 // We have matches. Run them through
131 for (int i = 0; i < matches; ++i) {
132 string name(list[i]);
133 sizes[i] = lyx::atoi(token(name, '-', 7));
136 if (scaleindex == -1) {
142 XFreeFontNames(list);
148 void FontInfo::init()
159 /// Release allocated stuff
160 void FontInfo::release()