1999-09-27 18:44:28 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/* This file is part of
|
1999-11-15 12:01:38 +00:00
|
|
|
* ======================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
|
|
|
* Copyright (C) 1997 Asger Alstrup
|
|
|
|
* and the LyX Team.
|
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cmath> // fabs()
|
|
|
|
#include <cstdlib> // atoi()
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "FontInfo.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "FontInfo.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "lyxrc.h" // lyxrc.use_scalable_fonts
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
extern LyXRC * lyxrc;
|
|
|
|
|
|
|
|
/// Load font close to this size
|
1999-10-02 16:21:10 +00:00
|
|
|
string FontInfo::getFontname(int size)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
if (!exist())
|
1999-10-02 16:21:10 +00:00
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
int closestind = -1;
|
|
|
|
double error = 100000;
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
for (int i = 0; i < matches; ++i) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if (sizes[i] == 0) {
|
|
|
|
// Scalable font should not be considered close
|
|
|
|
} else if (sizes[i] == size) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::FONT] << "Exact font match with\n"
|
|
|
|
<< strings[i] << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return strings[i];
|
|
|
|
} else if (fabs(sizes[i] - size - 0.1) < error) {
|
|
|
|
error = fabs(sizes[i] - size - 0.1);
|
|
|
|
closestind = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scalable && lyxrc->use_scalable_fonts) {
|
|
|
|
// We can use scalable
|
1999-10-02 16:21:10 +00:00
|
|
|
string font = resize(strings[scaleindex], size);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::FONT] << "Using scalable font to get\n"
|
|
|
|
<< font << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did any fonts get close?
|
|
|
|
if (closestind == -1) {
|
|
|
|
// No, and we are not allowed to use scalables, so...
|
1999-10-02 16:21:10 +00:00
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We use the closest match
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::FONT] << "Using closest font match to get size "
|
|
|
|
<< size
|
|
|
|
<< " with\n" << strings[closestind] << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return strings[closestind];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build newly sized font string
|
1999-10-02 16:21:10 +00:00
|
|
|
string FontInfo::resize(string const & font, int size) const {
|
1999-09-27 18:44:28 +00:00
|
|
|
// Find the position of the size spec
|
1999-10-28 15:19:59 +00:00
|
|
|
#ifdef WITH_WARNINGS
|
1999-10-02 16:21:10 +00:00
|
|
|
#warning rewrite to use std::string constructs
|
1999-10-28 15:19:59 +00:00
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
int cut = 0, before = 0, after = 0;
|
|
|
|
for (string::size_type i = 0; i < font.length(); ++i) {
|
1999-09-27 18:44:28 +00:00
|
|
|
if (font[i] == '-') {
|
1999-10-02 16:21:10 +00:00
|
|
|
++cut;
|
|
|
|
if (cut == 7) {
|
1999-09-27 18:44:28 +00:00
|
|
|
before = i;
|
1999-10-02 16:21:10 +00:00
|
|
|
} else if (cut == 8) {
|
1999-09-27 18:44:28 +00:00
|
|
|
after = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string head = font;
|
|
|
|
head.erase(before + 1, string::npos);
|
|
|
|
string tail = font;
|
|
|
|
tail.erase(0, after);
|
|
|
|
return head + tostr(size) + tail;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set new pattern
|
1999-10-02 16:21:10 +00:00
|
|
|
void FontInfo::setPattern(string const & pat)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
release();
|
|
|
|
init();
|
|
|
|
pattern = pat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Query font in X11
|
|
|
|
void FontInfo::query()
|
|
|
|
{
|
|
|
|
if (queried)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pattern.empty()) {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "Can not use empty font name for font query." << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
queried = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ** list = XListFonts(fl_display, pattern.c_str(), 100, &matches);
|
|
|
|
|
|
|
|
if (list == 0) {
|
|
|
|
// No fonts matched
|
|
|
|
scalable = false;
|
|
|
|
sizes = 0;
|
|
|
|
} else {
|
|
|
|
release();
|
|
|
|
sizes = new int[matches];
|
1999-10-02 16:21:10 +00:00
|
|
|
strings = new string[matches];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// We have matches. Run them through
|
1999-12-13 00:05:34 +00:00
|
|
|
for(int i = 0; i < matches; ++i) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string name(list[i]);
|
1999-11-15 12:01:38 +00:00
|
|
|
sizes[i] = atoi(token(name, '-', 7).c_str());
|
1999-09-27 18:44:28 +00:00
|
|
|
strings[i] = name;
|
|
|
|
if (sizes[i] == 0) {
|
|
|
|
if (scaleindex == -1) {
|
|
|
|
scaleindex = i;
|
|
|
|
}
|
|
|
|
scalable = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
XFreeFontNames(list);
|
|
|
|
}
|
|
|
|
queried = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Release allocated stuff
|
|
|
|
void FontInfo::release()
|
|
|
|
{
|
|
|
|
if (sizes) {
|
|
|
|
delete [] sizes;
|
|
|
|
sizes = 0;
|
|
|
|
}
|
|
|
|
if (strings) {
|
|
|
|
delete [] strings;
|
|
|
|
strings = 0;
|
|
|
|
}
|
|
|
|
}
|