2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1997 Asger Alstrup
10 * ====================================================== */
13 #include <cmath> // fabs()
14 #include <cstdlib> // atoi()
16 #include FORMS_H_LOCATION
19 #pragma implementation "FontInfo.h"
24 #include "lyxrc.h" // lyxrc.use_scalable_fonts
25 #include "support/lstrings.h"
29 /// Load font close to this size
30 string FontInfo::getFontname(int size)
36 double error = 100000;
38 for (int i = 0; i < matches; ++i) {
40 // Scalable font should not be considered close
41 } else if (sizes[i] == size) {
42 lyxerr[Debug::FONT] << "Exact font match with\n"
43 << strings[i] << endl;
45 } else if (fabs(sizes[i] - size - 0.1) < error) {
46 error = fabs(sizes[i] - size - 0.1);
51 if (scalable && lyxrc.use_scalable_fonts) {
52 // We can use scalable
53 string font = resize(strings[scaleindex], size);
54 lyxerr[Debug::FONT] << "Using scalable font to get\n"
59 // Did any fonts get close?
60 if (closestind == -1) {
61 // No, and we are not allowed to use scalables, so...
65 // We use the closest match
66 lyxerr[Debug::FONT] << "Using closest font match to get size "
68 << " with\n" << strings[closestind] << endl;
69 return strings[closestind];
73 /// Build newly sized font string
74 string FontInfo::resize(string const & font, int size) const
77 // Find the position of the size spec
79 string::iterator before = string::iterator(0);
80 string::iterator after = string::iterator(0);
81 for (string::iterator sit = ret.begin();
82 sit != ret.end(); ++sit)
85 if (cut == 7) before = sit + 1;
91 ret.replace(before, after, tostr(size));
97 void FontInfo::setPattern(string const & pat)
105 /// Query font in X11
106 void FontInfo::query()
111 if (pattern.empty()) {
112 lyxerr << "Can not use empty font name for font query."
120 list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
128 sizes = new int[matches];
129 strings = new string[matches];
131 // We have matches. Run them through
132 for(int i = 0; i < matches; ++i) {
133 string name(list[i]);
134 sizes[i] = atoi(token(name, '-', 7).c_str());
137 if (scaleindex == -1) {
143 XFreeFontNames(list);
149 void FontInfo::init()
160 /// Release allocated stuff
161 void FontInfo::release()