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 #ifndef CXX_GLOBAL_CSTD
32 /// Load font close to this size
33 string const FontInfo::getFontname(int size)
39 double error = 100000.0;
41 for (int i = 0; i < matches; ++i) {
43 // Scalable font should not be considered close
44 } else if (sizes[i] == size) {
45 lyxerr[Debug::FONT] << "Exact font match with\n"
46 << strings[i] << endl;
48 } else if (fabs(sizes[i] - size - 0.1) < error) {
49 error = fabs(sizes[i] - size - 0.1);
54 if (scalable && lyxrc.use_scalable_fonts) {
55 // We can use scalable
56 string const font = resize(strings[scaleindex], size);
57 lyxerr[Debug::FONT] << "Using scalable font to get\n"
62 // Did any fonts get close?
63 if (closestind == -1) {
64 // No, and we are not allowed to use scalables, so...
68 // We use the closest match
69 lyxerr[Debug::FONT] << "Using closest font match to get size "
71 << " with\n" << strings[closestind] << endl;
72 return strings[closestind];
76 /// Build newly sized font string
77 string const FontInfo::resize(string const & font, int size) const
80 // Find the position of the size spec
82 string::iterator before = string::iterator(0);
83 string::iterator after = string::iterator(0);
84 for (string::iterator sit = ret.begin();
85 sit != ret.end(); ++sit)
88 if (cut == 7) before = sit + 1;
94 ret.replace(before, after, tostr(size));
100 void FontInfo::setPattern(string const & pat)
108 /// Query font in X11
109 void FontInfo::query()
114 if (pattern.empty()) {
115 lyxerr << "Can not use empty font name for font query."
123 list = XListFonts(fl_get_display(), pattern.c_str(), 100, &matches);
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()