2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright (C) 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"
27 /// Load font close to this size
28 string FontInfo::getFontname(int size)
34 double error = 100000;
36 for (int i=0; i<matches; i++) {
38 // Scalable font should not be considered close
39 } else if (sizes[i] == size) {
40 lyxerr[Debug::FONT] << "Exact font match with\n"
41 << strings[i] << endl;
43 } else if (fabs(sizes[i] - size - 0.1) < error) {
44 error = fabs(sizes[i] - size - 0.1);
49 if (scalable && lyxrc->use_scalable_fonts) {
50 // We can use scalable
51 string font = resize(strings[scaleindex], size);
52 lyxerr[Debug::FONT] << "Using scalable font to get\n"
57 // Did any fonts get close?
58 if (closestind == -1) {
59 // No, and we are not allowed to use scalables, so...
63 // We use the closest match
64 lyxerr[Debug::FONT] << "Using closest font match to get size "
66 << " with\n" << strings[closestind] << endl;
67 return strings[closestind];
70 /// Build newly sized font string
71 string FontInfo::resize(string const & font, int size) const {
72 // Find the position of the size spec
73 #warning rewrite to use std::string constructs
74 int cut = 0, before = 0, after = 0;
75 for (string::size_type i = 0; i < font.length(); ++i) {
80 } else if (cut == 8) {
88 head.erase(before + 1, string::npos);
91 return head + tostr(size) + tail;
95 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." << endl;
114 char ** list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
122 sizes = new int[matches];
123 strings = new string[matches];
125 // We have matches. Run them through
126 for(int i=0; i<matches; i++) {
127 string name(list[i]);
128 sizes[i] = atoi(token(name, '-',7).c_str());
131 if (scaleindex == -1) {
137 XFreeFontNames(list);
142 /// Release allocated stuff
143 void FontInfo::release()