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()
17 #pragma implementation "FontInfo.h"
22 #include "lyxrc.h" // lyxrc.use_scalable_fonts
23 #include "support/lstrings.h"
26 /// Load font close to this size
27 string FontInfo::getFontname(int size)
33 double error = 100000;
35 for (int i = 0; i < matches; ++i) {
37 // Scalable font should not be considered close
38 } else if (sizes[i] == size) {
39 lyxerr[Debug::FONT] << "Exact font match with\n"
40 << strings[i] << endl;
42 } else if (fabs(sizes[i] - size - 0.1) < error) {
43 error = fabs(sizes[i] - size - 0.1);
48 if (scalable && lyxrc.use_scalable_fonts) {
49 // We can use scalable
50 string font = resize(strings[scaleindex], size);
51 lyxerr[Debug::FONT] << "Using scalable font to get\n"
56 // Did any fonts get close?
57 if (closestind == -1) {
58 // No, and we are not allowed to use scalables, so...
62 // We use the closest match
63 lyxerr[Debug::FONT] << "Using closest font match to get size "
65 << " with\n" << strings[closestind] << endl;
66 return strings[closestind];
70 /// Build newly sized font string
71 string FontInfo::resize(string const & font, int size) const
74 // Find the position of the size spec
76 string::iterator before = string::iterator(0);
77 string::iterator after = string::iterator(0);
78 for (string::iterator sit = ret.begin();
79 sit != ret.end(); ++sit)
82 if (cut == 7) before = sit + 1;
88 ret.replace(before, after, tostr(size));
94 void FontInfo::setPattern(string const & pat)
102 /// Query font in X11
103 void FontInfo::query()
108 if (pattern.empty()) {
109 lyxerr << "Can not use empty font name for font query."
115 char ** list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
123 sizes = new int[matches];
124 strings = new string[matches];
126 // We have matches. Run them through
127 for(int i = 0; i < matches; ++i) {
128 string name(list[i]);
129 sizes[i] = atoi(token(name, '-', 7).c_str());
132 if (scaleindex == -1) {
138 XFreeFontNames(list);
144 /// Release allocated stuff
145 void FontInfo::release()