2000-07-24 13:53:19 +00:00
|
|
|
/* This file is part of
|
|
|
|
* ======================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
2000-07-24 13:53:19 +00:00
|
|
|
*
|
|
|
|
* ====================================================== */
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-24 13:53:19 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2000-04-06 09:17:40 +00:00
|
|
|
#include <cctype>
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
#include "font.h"
|
|
|
|
#include "FontLoader.h"
|
|
|
|
#include "lyxrc.h"
|
2000-07-04 20:32:37 +00:00
|
|
|
#include "encoding.h"
|
2001-04-05 12:26:41 +00:00
|
|
|
#include "language.h"
|
2000-04-04 00:19:15 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline
|
2000-04-04 00:19:15 +00:00
|
|
|
XFontStruct * getXFontstruct(LyXFont const & f)
|
|
|
|
{
|
|
|
|
return fontloader.load(f.family(), f.series(),
|
|
|
|
f.realShape(), f.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
inline
|
2000-04-04 00:19:15 +00:00
|
|
|
XID getFontID(LyXFont const & f)
|
|
|
|
{
|
|
|
|
return getXFontstruct(f)->fid;
|
|
|
|
}
|
2001-03-20 01:22:46 +00:00
|
|
|
|
|
|
|
} // namespace anon
|
2000-04-04 00:19:15 +00:00
|
|
|
|
|
|
|
int lyxfont::maxAscent(LyXFont const & f)
|
|
|
|
{
|
|
|
|
return getXFontstruct(f)->ascent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxfont::maxDescent(LyXFont const & f)
|
|
|
|
{
|
|
|
|
return getXFontstruct(f)->descent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxfont::ascent(char c, LyXFont const & f)
|
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2000-10-30 21:53:29 +00:00
|
|
|
&& uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
|
2000-04-04 00:19:15 +00:00
|
|
|
return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
|
|
|
|
else
|
|
|
|
return finfo->ascent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxfont::descent(char c, LyXFont const & f)
|
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2000-10-30 21:53:29 +00:00
|
|
|
&& uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
|
2000-04-04 00:19:15 +00:00
|
|
|
return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
|
|
|
|
else
|
|
|
|
return finfo->descent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxfont::lbearing(char c, LyXFont const & f)
|
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2000-10-30 21:53:29 +00:00
|
|
|
&& uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
|
2000-04-04 00:19:15 +00:00
|
|
|
return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxfont::rbearing(char c, LyXFont const & f)
|
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2000-10-30 21:53:29 +00:00
|
|
|
&& uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
|
2000-04-04 00:19:15 +00:00
|
|
|
return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
|
|
|
|
else
|
|
|
|
return width(c, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-27 18:13:30 +00:00
|
|
|
int lyxfont::width(char const * s, size_t n, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
if (!lyxrc.use_gui)
|
|
|
|
return n;
|
2000-07-04 20:32:37 +00:00
|
|
|
|
|
|
|
if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
|
|
|
|
XChar2b * xs = new XChar2b[n];
|
2000-07-20 10:04:27 +00:00
|
|
|
Encoding const * encoding = f.language()->encoding();
|
2000-10-11 21:06:43 +00:00
|
|
|
//LyXFont const * font = &f;
|
|
|
|
LyXFont font(f);
|
2000-07-20 10:04:27 +00:00
|
|
|
if (f.family() == LyXFont::SYMBOL_FAMILY) {
|
|
|
|
#ifdef USE_UNICODE_FOR_SYMBOLS
|
2000-10-11 21:06:43 +00:00
|
|
|
//LyXFont font2 = f;
|
|
|
|
font.setFamily(LyXFont::ROMAN_FAMILY);
|
|
|
|
font.setShape(LyXFont::UP_SHAPE);
|
|
|
|
//font = &font2;
|
2000-07-20 10:04:27 +00:00
|
|
|
#endif
|
2000-10-10 12:36:36 +00:00
|
|
|
encoding = encodings.symbol_encoding();
|
2000-07-20 10:04:27 +00:00
|
|
|
}
|
2000-10-10 12:36:36 +00:00
|
|
|
for (size_t i = 0; i < n; ++i) {
|
2000-07-20 10:04:27 +00:00
|
|
|
Uchar c = encoding->ucs(s[i]);
|
2000-07-04 20:32:37 +00:00
|
|
|
xs[i].byte1 = c >> 8;
|
|
|
|
xs[i].byte2 = c & 0xff;
|
|
|
|
}
|
2000-10-11 21:06:43 +00:00
|
|
|
int result = width(xs, n, font);
|
2000-07-04 20:32:37 +00:00
|
|
|
delete[] xs;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
|
|
|
|
return ::XTextWidth(getXFontstruct(f), s, n);
|
|
|
|
} else {
|
|
|
|
// emulate smallcaps since X doesn't support this
|
|
|
|
unsigned int result = 0;
|
|
|
|
char c;
|
|
|
|
LyXFont smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
|
2000-10-10 12:36:36 +00:00
|
|
|
for (size_t i = 0; i < n; ++i) {
|
2000-04-04 00:19:15 +00:00
|
|
|
c = s[i];
|
|
|
|
// when islower is a macro, the cast is needed (JMarc)
|
|
|
|
if (islower(static_cast<unsigned char>(c))) {
|
|
|
|
c = toupper(c);
|
|
|
|
result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
|
|
|
|
} else {
|
|
|
|
result += ::XTextWidth(getXFontstruct(f), &c, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxfont::signedWidth(string const & s, LyXFont const & f)
|
|
|
|
{
|
|
|
|
if (s.empty()) return 0;
|
2000-09-26 13:54:57 +00:00
|
|
|
if (s[0] == '-')
|
|
|
|
return -width(s.substr(1, s.length() - 1), f);
|
2000-04-04 00:19:15 +00:00
|
|
|
else
|
2000-09-26 13:54:57 +00:00
|
|
|
return width(s, f);
|
2000-04-04 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
//int lyxfont::width(wstring const & s, int n, LyXFont const & f)
|
2000-07-04 20:32:37 +00:00
|
|
|
int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
|
|
|
|
{
|
|
|
|
if (!lyxrc.use_gui)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
|
|
|
|
return ::XTextWidth16(getXFontstruct(f), s, n);
|
|
|
|
} else {
|
|
|
|
// emulate smallcaps since X doesn't support this
|
|
|
|
unsigned int result = 0;
|
2001-06-04 23:57:32 +00:00
|
|
|
static XChar2b c;
|
2000-07-04 20:32:37 +00:00
|
|
|
LyXFont smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
if (s[i].byte1 == 0 && islower(s[i].byte2)) {
|
|
|
|
c.byte2 = toupper(s[i].byte2);
|
|
|
|
result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
|
|
|
|
} else {
|
|
|
|
result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
int lyxfont::XTextWidth(LyXFont const & f, char const * str, int count)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
return ::XTextWidth(getXFontstruct(f), str, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
int lyxfont::XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
|
2000-07-04 20:32:37 +00:00
|
|
|
{
|
|
|
|
return ::XTextWidth16(getXFontstruct(f), str, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
|
|
|
|
{
|
|
|
|
::XSetFont(display, gc, getFontID(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
//} // end of namespace font
|
|
|
|
//} // end of namespace lyx
|