1999-09-27 18:44:28 +00:00
|
|
|
/* 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()
|
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"
|
2001-07-29 15:34:18 +00:00
|
|
|
#include "support/lyxlib.h"
|
2001-07-03 16:13:01 +00:00
|
|
|
#include "frontends/GUIRunTime.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-01 10:53:24 +00:00
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
using std::fabs;
|
|
|
|
#endif
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Load font close to this size
|
2000-09-14 17:53:12 +00:00
|
|
|
string const 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;
|
2000-10-25 13:10:04 +00:00
|
|
|
double error = 100000.0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
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];
|
2001-06-01 10:53:24 +00:00
|
|
|
} else if (fabs(sizes[i] - size - 0.1) < error) {
|
|
|
|
error = fabs(sizes[i] - size - 0.1);
|
1999-09-27 18:44:28 +00:00
|
|
|
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
|
2001-05-31 02:23:46 +00:00
|
|
|
string const 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
|
2000-09-14 17:53:12 +00:00
|
|
|
string const FontInfo::resize(string const & font, int size) const
|
1999-12-16 06:43:25 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-03-20 16:37:50 +00:00
|
|
|
char ** list = 0;
|
|
|
|
if (lyxrc.use_gui)
|
2001-07-03 16:13:01 +00:00
|
|
|
list = XListFonts(GUIRunTime::x11Display(), pattern.c_str(),
|
|
|
|
100, &matches);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
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
|
2000-11-04 10:00:12 +00:00
|
|
|
for (int i = 0; i < matches; ++i) {
|
1999-10-02 16:21:10 +00:00
|
|
|
string name(list[i]);
|
2000-09-26 13:54:57 +00:00
|
|
|
sizes[i] = lyx::atoi(token(name, '-', 7));
|
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
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
void FontInfo::init()
|
|
|
|
{
|
|
|
|
sizes = 0;
|
|
|
|
strings = 0;
|
|
|
|
matches = 0;
|
|
|
|
queried = false;
|
|
|
|
scalable = false;
|
|
|
|
scaleindex = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|