2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright (C) 1997 Asger Alstrup
10 *======================================================*/
13 #include <math.h> // fabs()
14 #include <stdlib.h> // atoi()
17 #pragma implementation "FontInfo.h"
22 #include "lyxrc.h" // lyxrc.use_scalable_fonts
25 /// Load font close to this size
26 LString FontInfo::getFontname(int size)
32 double error = 100000;
34 for (int i=0; i<matches; i++) {
36 // Scalable font should not be considered close
37 } else if (sizes[i] == size) {
38 lyxerr.debug(LString("Exact font match with\n")
39 + strings[i], Error::FONT);
41 } else if (fabs(sizes[i] - size - 0.1) < error) {
42 error = fabs(sizes[i] - size - 0.1);
47 if (scalable && lyxrc->use_scalable_fonts) {
48 // We can use scalable
49 LString font = resize(strings[scaleindex], size);
50 lyxerr.debug("Using scalable font to get\n"
55 // Did any fonts get close?
56 if (closestind == -1) {
57 // No, and we are not allowed to use scalables, so...
61 // We use the closest match
62 lyxerr.debug(LString("Using closest font match to get size ") + size
63 + " with\n" + strings[closestind], Error::FONT);
64 return strings[closestind];
67 /// Build newly sized font string
68 LString FontInfo::resize(LString const & font, int size) const {
69 // Find the position of the size spec
70 int cut=0, before=0, after=0;
71 for (int i=0; i<font.length(); i++) {
84 head.substring(0, before);
86 tail.substring(after,tail.length()-1);
87 return head + size + tail;
91 void FontInfo::setPattern(LString const & pat)
99 void FontInfo::query()
104 if (pattern.empty()) {
105 lyxerr.print("Can not use empty font name for font query.");
110 char ** list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
118 sizes = new int[matches];
119 strings = new LString[matches];
121 // We have matches. Run them through
122 for(int i=0; i<matches; i++) {
123 LString name(list[i]);
124 sizes[i] = atoi(name.token('-',7).c_str());
127 if (scaleindex == -1) {
133 XFreeFontNames(list);
138 /// Release allocated stuff
139 void FontInfo::release()