1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1997 Asger Alstrup
9 * ====================================================== */
12 #include <cmath> // fabs()
15 #pragma implementation "FontInfo.h"
20 #include "lyxrc.h" // lyxrc.use_scalable_fonts
21 #include "support/lstrings.h"
22 #include "frontends/GUIRunTime.h"
26 #ifndef CXX_GLOBAL_CSTD
30 /// Load font close to this size
31 string const FontInfo::getFontname(int size)
37 double error = 100000.0;
39 for (int i = 0; i < matches; ++i) {
41 // Scalable font should not be considered close
42 } else if (sizes[i] == size) {
43 lyxerr[Debug::FONT] << "Exact font match with\n"
44 << strings[i] << endl;
46 } else if (fabs(sizes[i] - size - 0.1) < error) {
47 error = fabs(sizes[i] - size - 0.1);
52 if (scalable && lyxrc.use_scalable_fonts) {
53 // We can use scalable
54 string const font = resize(strings[scaleindex], size);
55 lyxerr[Debug::FONT] << "Using scalable font to get\n"
60 // Did any fonts get close?
61 if (closestind == -1) {
62 // No, and we are not allowed to use scalables, so...
66 // We use the closest match
67 lyxerr[Debug::FONT] << "Using closest font match to get size "
69 << " with\n" << strings[closestind] << endl;
70 return strings[closestind];
74 /// Build newly sized font string
75 string const FontInfo::resize(string const & font, int size) const
78 // Find the position of the size spec
80 string::iterator before = string::iterator(0);
81 string::iterator after = string::iterator(0);
82 for (string::iterator sit = ret.begin();
83 sit != ret.end(); ++sit)
86 if (cut == 7) before = sit + 1;
92 ret.replace(before, after, tostr(size));
98 void FontInfo::setPattern(string const & pat)
106 /// Query font in X11
107 void FontInfo::query()
112 if (pattern.empty()) {
113 lyxerr << "Can not use empty font name for font query."
121 list = XListFonts(GUIRunTime::x11Display(), pattern.c_str(),
130 sizes = new int[matches];
131 strings = new string[matches];
133 // We have matches. Run them through
134 for (int i = 0; i < matches; ++i) {
135 string name(list[i]);
136 sizes[i] = lyx::atoi(token(name, '-', 7));
139 if (scaleindex == -1) {
145 XFreeFontNames(list);
151 void FontInfo::init()
162 /// Release allocated stuff
163 void FontInfo::release()