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 "support/lyxlib.h"
23 #include "frontends/GUIRunTime.h"
27 #ifndef CXX_GLOBAL_CSTD
31 /// Load font close to this size
32 string const FontInfo::getFontname(int size)
38 double error = 100000.0;
40 for (int i = 0; i < matches; ++i) {
42 // Scalable font should not be considered close
43 } else if (sizes[i] == size) {
44 lyxerr[Debug::FONT] << "Exact font match with\n"
45 << strings[i] << endl;
47 } else if (fabs(sizes[i] - size - 0.1) < error) {
48 error = fabs(sizes[i] - size - 0.1);
53 if (scalable && lyxrc.use_scalable_fonts) {
54 // We can use scalable
55 string const font = resize(strings[scaleindex], size);
56 lyxerr[Debug::FONT] << "Using scalable font to get\n"
61 // Did any fonts get close?
62 if (closestind == -1) {
63 // No, and we are not allowed to use scalables, so...
67 // We use the closest match
68 lyxerr[Debug::FONT] << "Using closest font match to get size "
70 << " with\n" << strings[closestind] << endl;
71 return strings[closestind];
75 /// Build newly sized font string
76 string const FontInfo::resize(string const & font, int size) const
79 // Find the position of the size spec
81 string::iterator before = string::iterator(0);
82 string::iterator after = string::iterator(0);
83 for (string::iterator sit = ret.begin();
84 sit != ret.end(); ++sit)
87 if (cut == 7) before = sit + 1;
93 ret.replace(before, after, tostr(size));
99 void FontInfo::setPattern(string const & pat)
107 /// Query font in X11
108 void FontInfo::query()
113 if (pattern.empty()) {
114 lyxerr << "Can not use empty font name for font query."
122 list = XListFonts(GUIRunTime::x11Display(), pattern.c_str(),
131 sizes = new int[matches];
132 strings = new string[matches];
134 // We have matches. Run them through
135 for (int i = 0; i < matches; ++i) {
136 string name(list[i]);
137 sizes[i] = lyx::atoi(token(name, '-', 7));
140 if (scaleindex == -1) {
146 XFreeFontNames(list);
152 void FontInfo::init()
163 /// Release allocated stuff
164 void FontInfo::release()