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
|
|
|
|
*
|
1999-12-16 06:43:25 +00:00
|
|
|
* Copyright 1997 Asger Alstrup
|
1999-09-27 18:44:28 +00:00
|
|
|
* 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
|
|
|
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-12 10:35:05 +00:00
|
|
|
if (scalable && lyxrc.use_scalable_fonts) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Build newly sized font string
|
1999-12-16 06:43:25 +00:00
|
|
|
string FontInfo::resize(string const & font, int size) const
|
|
|
|
{
|
|
|
|
string ret(font);
|
1999-09-27 18:44:28 +00:00
|
|
|
// Find the position of the size spec
|
1999-12-16 06:43:25 +00:00
|
|
|
int cut = 0;
|
2000-01-06 02:44:26 +00:00
|
|
|
string::iterator before = string::iterator(0);
|
|
|
|
string::iterator after = string::iterator(0);
|
1999-12-16 06:43:25 +00:00
|
|
|
for (string::iterator sit = ret.begin();
|
|
|
|
sit != ret.end(); ++sit)
|
|
|
|
if ((*sit) == '-') {
|
1999-10-02 16:21:10 +00:00
|
|
|
++cut;
|
1999-12-16 06:43:25 +00:00
|
|
|
if (cut == 7) before = sit + 1;
|
|
|
|
else if (cut == 8) {
|
|
|
|
after = sit;
|
1999-09-27 18:44:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-12-16 06:43:25 +00:00
|
|
|
ret.replace(before, after, tostr(size));
|
|
|
|
return ret;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Query font in X11
|
|
|
|
void FontInfo::query()
|
|
|
|
{
|
|
|
|
if (queried)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pattern.empty()) {
|
1999-12-16 06:43:25 +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;
|
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Release allocated stuff
|
|
|
|
void FontInfo::release()
|
|
|
|
{
|
|
|
|
if (sizes) {
|
|
|
|
delete [] sizes;
|
|
|
|
sizes = 0;
|
|
|
|
}
|
|
|
|
if (strings) {
|
|
|
|
delete [] strings;
|
|
|
|
strings = 0;
|
|
|
|
}
|
|
|
|
}
|